Commit graph

54 commits

Author SHA1 Message Date
David Lamparter 13a6ac5b4c lib: crash handlers must be allowed on threads
Blocking all signals on non-main threads is not the way to go, at least
the handlers for SIGSEGV, SIGBUS, SIGILL, SIGABRT and SIGFPE need to run
so we get backtraces.  Otherwise the process just exits.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2025-02-07 13:37:43 +01:00
Mark Stapp 44eb133b52 lib: serialize pthread startup
Add a new condition var and mutex to serialize pthread startup.
When a new pthread is started, it will wait very early on for the
parent pthread to permit it to run. This ensures that that the
ordering between parent and child is predictable.

Signed-off-by: Mark Stapp <mjs@cisco.com>
2024-04-11 09:00:58 -04:00
Donald Sharp 5ddf2aaa52 lib: Actually create the rcu and save it before using it
In a non-controlled startup, the rcu data structures were
not being created until after logging could happen.  This
is bad.  Move it so that the rcu data structures are
created first, before logging( HA! ) can happen.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2024-02-03 23:08:20 -05:00
Louis Scalbert 73f72c5266 lib: add ability to log from external pthread
External libraries can re-enter the FRR code through a hook function. A
crash occurs when logging from this hook function if the library has
initiated a new pthread, as the FRR RCU context is not initialized for
this thread.

Add frr_pthread_non_controlled_startup() function to initialize a valid
RCU context within a FRR pthread context, originating from an external
pthread.

Signed-off-by: Louis Scalbert <louis.scalbert@6wind.com>
2024-02-02 10:25:05 +01:00
Donald Sharp 8049f74fc6 lib: zebra.h is not using signal.h
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2024-01-09 12:50:40 -05:00
Donald Sharp ce50d11c4d *: Convert thread_master_XXX functions to event_master_XXX
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2023-03-24 08:32:17 -04:00
Donald Sharp e16d030c65 *: Convert THREAD_XXX macros to EVENT_XXX macros
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2023-03-24 08:32:17 -04:00
Donald Sharp de2754be3a *: Convert thread_fetch and thread_call to event_fetch and event_call
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2023-03-24 08:32:17 -04:00
Donald Sharp 907a2395f4 *: Convert thread_add_XXX functions to event_add_XXX
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2023-03-24 08:32:17 -04:00
Donald Sharp e6685141aa *: Rename struct thread to struct event
Effectively a massive search and replace of
`struct thread` to `struct event`.  Using the
term `thread` gives people the thought that
this event system is a pthread when it is not

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2023-03-24 08:32:17 -04:00
David Lamparter acddc0ed3c *: auto-convert to SPDX License IDs
Done with a combination of regex'ing and banging my head against a wall.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2023-02-09 14:09:11 +01:00
Donald Sharp cb1991af8c *: frr_with_mutex change to follow our standard
convert:
	frr_with_mutex(..)

to:
	frr_with_mutex (..)

To make all our code agree with what clang-format is going to produce

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2022-07-20 15:50:32 -04:00
Donald Sharp cc9f21da22 *: Change thread->func to return void instead of int
The int return value is never used.  Modify the code
base to just return a void instead.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2022-02-23 19:56:04 -05:00
David Lamparter 38554d3ae6 lib: hard-fail creating threads before fork()
Creating any threads before we fork() into the background (if `-d` is
given) is an extremely dangerous footgun;  the threads are created in
the parent and terminated when that exits.

This is extra dangerous because while testing, you'd often run the
daemon in foreground without `-d`, and everything works as expected.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2021-04-22 13:25:38 +02:00
David Lamparter bf8d3d6aca *: require semicolon after DEFINE_MTYPE & co
Back when I put this together in 2015, ISO C11 was still reasonably new
and we couldn't require it just yet.  Without ISO C11, there is no
"good" way (only bad hacks) to require a semicolon after a macro that
ends with a function definition.  And if you added one anyway, you'd get
"spurious semicolon" warnings on some compilers...

With C11, `_Static_assert()` at the end of a macro will make it so that
the semicolon is properly required, consumed, and not warned about.

Consistently requiring semicolons after "file-level" macros matches
Linux kernel coding style and helps some editors against mis-syntax'ing
these macros.

Signed-off-by: David Lamparter <equinox@diac24.net>
2021-03-17 06:18:17 +01:00
Quentin Young c7bb4f006b lib, bgpd: convert lttng tracepoints to frrtrace()
- tracepoint() -> frrtrace()
- tracelog() -> frrtracelog()
- tracepoint_enabled() -> frrtrace_enabled()

Also removes copypasta'd #ifdefs for those LTTng macros, those are
handled in lib/trace.h

Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-10-23 15:13:51 -04:00
Quentin Young 912d45a161 lib: move trace.h -> libfrr_trace.h
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-10-23 15:13:51 -04:00
Quentin Young 87879a5ecb lib: add tracepoints for pthread run, stop
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-10-23 15:13:51 -04:00
Mark Stapp f4635e33a6 lib: block signals in child pthreads
Block signals in child/additional pthreads; frr daemons generally
expect that only the main thread will handle signals.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2020-07-09 11:21:10 -04:00
David Lamparter 0bdeb5e58d lib: rewrite zlog lock-free & TLS-buffered
This is a full rewrite of the "back end" logging code.  It now uses a
lock-free list to iterate over logging targets, and the targets
themselves are as lock-free as possible.  (syslog() may have a hidden
internal mutex in the C library;  the file/fd targets use a single
write() call which should ensure atomicity kernel-side.)

Note that some functionality is lost in this patch:
- Solaris printstack() backtraces are ditched (unlikely to come back)
- the `log-filter` machinery is gone (re-added in followup commit)
- `terminal monitor` is temporarily stubbed out.  The old code had a
  race condition with VTYs going away.  It'll likely come back rewritten
  and with vtysh support.
- The `zebra_ext_log` hook is gone.  Instead, it's now much easier to
  add a "proper" logging target.

v2: TLS buffer to get some actual performance

Signed-off-by: David Lamparter <equinox@diac24.net>
2020-04-01 06:53:26 +02:00
David Lamparter 2b64873d24 *: generously apply const
const const const your boat, merrily down the stream...

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-12-02 15:01:29 +01:00
Quentin Young 9af949cc95 lib: clean up frr_pthread structs at exit
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-09-17 16:12:28 +00:00
Quentin Young 54baf432f0 lib: delete pthread from tracking list on delete
Pthreads were not being deleted from the list after destruction. This
isn't causing any bugs currently but that's just by dumb luck.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-09-16 16:12:00 +00:00
David Lamparter 00dffa8cde lib: add frr_with_mutex() block-wrapper
frr_with_mutex(...) { ... } locks and automatically unlocks the listed
mutex(es) when the block is exited.  This adds a bit of safety against
forgetting the unlock in error paths & co. and makes the code a slight
bit more readable.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2019-09-03 17:15:17 +02:00
David Lamparter 3e41733f1b lib: RCU
Please refer to doc/developer/rcu.rst for documentation.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-07-31 03:34:09 +02:00
David Lamparter eaf58ba906 lib: use MTYPE_STATIC
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2019-06-21 08:54:25 +02:00
Quentin Young 0a22ddfbb1 *: remove null check before XFREE
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-02-25 23:00:46 +00:00
Donald Sharp e77cc2a97f lib: snprintf to strlcpy in frr_pthread.c
Convert to using strlcpy to erase those pesky little
gcc warnings->errors

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-01-31 07:52:23 -05:00
David Lamparter a28436ca47
Merge pull request #3655 from rubenk/macos-fixes
A few small build fixes for Mac OS
2019-01-24 13:51:37 +01:00
Ruben Kerkhof 4d762f2607 Treewide: use ANSI function definitions
Signed-off-by: Ruben Kerkhof <ruben@rubenkerkhof.com>
2019-01-24 11:21:59 +01:00
Ruben Kerkhof a64c953a90 Fix compile error on Mac OS
CC       lib/frr_pthread.lo
lib/frr_pthread.c:128:40: error: too many arguments to function call, expected 1, have 3
        ret = pthread_setname_np(fpt->thread, fpt->os_name, NULL);
              ~~~~~~~~~~~~~~~~~~              ^~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/pthread.h:512:1: note: 'pthread_setname_np' declared here
__API_AVAILABLE(macos(10.6), ios(3.2))

Mac OS does have pthread_setname_np, but we can't use it here since it
only accepts a single argument, the thread name, and thus only works for
the current thread.

Signed-off-by: Ruben Kerkhof <ruben@rubenkerkhof.com>
2019-01-22 17:55:31 +01:00
Donald Sharp c80bedb83b lib, bgpd: Convert frr_pthread_set_name to only cause it to set os name of the thread
The current invocation of frr_pthread_set_name was causing it reset the os_name.
There is no need for this, we now always create the pthread appropriately
to have both name and os_name.  So convert this function to a simple
call through of the pthread call now.

Before(any of these changes):
sharpd@robot ~/frr1> ps -L -p 16895
  PID   LWP TTY          TIME CMD
16895 16895 ?        00:01:39 bgpd
16895 16896 ?        00:00:54
16895 16897 ?        00:00:07 bgpd_ka

After:
sharpd@donna ~/frr1> ps -L -p 1752
  PID   LWP TTY          TIME CMD
 1752  1752 ?        00:00:00 bgpd
 1752  1753 ?        00:00:00 bgpd_io
 1752  1754 ?        00:00:00 bgpd_ka

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-01-09 14:59:22 -05:00
Donald Sharp 6d0a40b5b7 lib: Cleanup thread name setting to happen at start
When we start a thread we always call fpt_run and since
the last commit we know os_name is filled with something,
therefore we can just set the name on startup.

This creates this output now for zebra:

sharpd@donna ~/frr2> ps -L -p 25643
  PID   LWP TTY          TIME CMD
25643 25643 ?        00:00:00 zebra
25643 25644 ?        00:00:00 Zebra dplane
25643 25684 ?        00:00:00 zebra_apic
sharpd@donna ~/frr2>

I removed the abstraction to frr_pthread_set_name because
it was snprintf'ing into the same buffer which was the
real bug here( the first character of os_name became null).
In the next commit I'll remove that api because
it is unneeded and was a horrible hack to get
this to work for the one place it was wanted.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-01-09 14:38:07 -05:00
Donald Sharp b8dccd94f3 lib: On frr_pthread_new save a os_name
On call of frr_pthread_new, save the os_name if given,
if not given use the name passed in( shortening to fit
in available space ) and finally if the name was not
passed in use the default value.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-01-09 14:30:43 -05:00
David Lamparter 6a154c8812 *: list_delete_and_null() -> list_delete()
Signed-off-by: David Lamparter <equinox@diac24.net>
2018-10-02 11:40:52 +02:00
David Lamparter 64777f5d87 lib: remove unused frr_pthread_yield()
OS-level yield is generally a bad and possibly dangerous idea.  If the
thread should be suspended, there should always be something to wait on,
or it turns into busy waiting.  And if it's "just giving something else
the chance to run" - that's the kernel's job to determine, and the
kernel will do so while considering priorities, cgroups, and whatnot.
Let it do its job.

Signed-off-by: David Lamparter <equinox@diac24.net>
2018-09-19 22:01:50 +02:00
David Lamparter 1ac267a2d9 lib: remove frr_pthread->id
All I can see is an unneccessary complication.  If there's some purpose
here it needs to be documented...

Signed-off-by: David Lamparter <equinox@diac24.net>
2018-09-19 22:01:46 +02:00
David Lamparter e9d938b82a lib: make pthread_set[_]name_np test OS agnostic
FreeBSD supports pthread_set_name_np() too.  Also, pthread_set_name_np()
returns void.  And NetBSD has pthread_setname_np() with an extra arg...

Signed-off-by: David Lamparter <equinox@diac24.net>
2018-09-09 01:16:25 +02:00
David Lamparter 324be174d7 build: check {malloc,pthread}_np.h for *BSD
FreeBSD has malloc_usable_size() in malloc_np.h
OpenBSD has pthread_set_name_np() in pthread_np.h

Signed-off-by: David Lamparter <equinox@diac24.net>
2018-09-09 01:16:25 +02:00
Mark Stapp c5e7bf3fa6 lib: fix use of frrpthread os_name
Fix test for presence of configured os pthread name.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
2018-08-30 11:44:45 -04:00
Chirag Shah 57019528a0 *: pthread set name abstraction
Testing Done:

 TOR#cat /proc/2670/task/2672/comm
 bgpd_ka

TOR# ps H -C bgpd -o 'pid tid cmd comm'
  PID   TID CMD                         COMMAND
  2670  2670 /usr/lib/frr/bgpd -M snmp - bgpd
  2670  2671 /usr/lib/frr/bgpd -M snmp - bgpd
  2670  2672 /usr/lib/frr/bgpd -M snmp - bgpd_ka

Signed-off-by: Chirag Shah <chirag@cumulusnetworks.com>
2018-08-29 15:41:54 -07:00
Quentin Young a6275055bf lib: add cancel point to default pthread loop
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-07-26 00:40:52 +00:00
paco 5f18adf01b
eigrpd, isisd, lib, ospfd: no effect (cppcheck)
Assignment of function parameter has no effect outside the function.

Signed-off-by: F. Aragon <paco@voltanet.io>
2018-06-19 10:19:05 +02:00
Quentin Young b1087f3507
lib: frr_pthread int size nits
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-03-09 11:20:53 -05:00
Quentin Young 985e36a609
lib: frr_pthread.c style
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-03-06 11:03:00 -05:00
Quentin Young d8a8a8de00
lib: some frr_pthread fixes
* Use atomic fixed-width thread identifiers
* Add ability to change thread name at runtime

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-03-05 18:04:54 -05:00
Quentin Young a45dc9742c
lib: streamline frr_pthreads, add default loop
Some work on FRR's pthread wrapper.

* Provide a built-in way to synchronize thread startup
* Make utility functions take frr_pthread * instead of its integer ID
* Pass frr_pthread * as pthread start function argument
* Correct some comment styling
* Rename some variables to match naming conventions in the file
* Change parameter ordering in stop function prototype to follow the
  convention in the other functions
* Default new frr_pthreads to using a vanilla event loop

For the last point, the original goal when designing the implementation
of pthreads into FRR was to be able to use the thread.c event based
system inside pthreads. This code essentially encapuslates all the
thread.c functionality into an easy to use pthread out of the box.
Creating a new frr_pthread with a null attributes field will cause the
created frr_pthread to run a thread.c event loop. The upshot of this is
that it is now possible to safely run existing functions in a pthread in
roughly 3 lines of code. It also serves as an example / starting point
for others.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-01-24 15:30:50 -05:00
Quentin Young 0bbb9e72f2
lib: add MTYPE for synchronization primitives
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-01-09 14:01:31 -05:00
Donald Sharp b2140cb7c8 lib: Add pthread_yield
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-01-06 14:08:30 -05:00
whitespace / reindent d62a17aede *: reindent
indent.py `git ls-files | pcregrep '\.[ch]$' | pcregrep -v '^(ldpd|babeld|nhrpd)/'`

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-17 14:04:07 +02:00