Commit Graph

248 Commits

Author SHA1 Message Date
Donald Sharp
f75e802d8a *: Allow disabling of getrusage calls
getrusage, in a heavily stressed system, can account for
signficant running time due to process switching to the kernel.
Allow the end-operator to specify `--disable-cpu-time` to
avoid this call.  Additionally we cause `show thread cpu` to
not show up if this is selected.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-10-16 13:34:59 -04:00
Spantik
e985cda017 lib: Clean up the index that is being used for storing FD and events.
When POLLNVAL is received for a FD then that FD is removed from the
pfd array and also array is rearranged using memmove. When memmove
is used then unused index are not cleanedup. When a new FD takes
up that index then it ends up using stale events without any handler
set for the same.

Signed-off-by: Santosh P K <sapk@vmware.com>
2019-10-15 01:47:30 -07: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
Donald Sharp
f79f7a7bb2 *: Fix spelling errors pointed out by debian packaging
Debian packaging when run finds a bunch of spelling errors:

I: frr: spelling-error-in-binary usr/bin/vtysh occurences occurrences
I: frr: spelling-error-in-binary usr/lib/frr/bfdd Amount of times Number of times
I: frr: spelling-error-in-binary usr/lib/frr/bgpd occurences occurrences
I: frr: spelling-error-in-binary usr/lib/frr/bgpd recieved received
I: frr: spelling-error-in-binary usr/lib/frr/isisd betweeen between
I: frr: spelling-error-in-binary usr/lib/frr/ospf6d Infomation Information
I: frr: spelling-error-in-binary usr/lib/frr/ospfd missmatch mismatch
I: frr: spelling-error-in-binary usr/lib/frr/pimd bootsrap bootstrap
I: frr: spelling-error-in-binary usr/lib/frr/pimd Unknwon Unknown
I: frr: spelling-error-in-binary usr/lib/frr/zebra Requsted Requested
I: frr: spelling-error-in-binary usr/lib/frr/zebra uknown unknown
I: frr: spelling-error-in-binary usr/lib/x86_64-linux-gnu/frr/libfrr.so.0.0.0 overriden overridden

This commit fixes all of them except the bgp `recieved` issue due to
it being part of json output.  That one will need to go through
a deprecation cycle.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-08-19 10:36:53 -04:00
Donald Sharp
76eb017923
Merge pull request #4497 from opensourcerouting/rcu
RCU support
2019-08-08 08:41:07 -04:00
David Lamparter
27d29ced68 lib: use DECLARE_HEAP for timers instead of pqueue
Replaces the use of pqueue_* for the thread_master's timer list with an
instance of DECLARE_HEAP_*.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-08-01 16:59:29 +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
Donald Sharp
a0b36ae673 lib: Add function name to show thread poll
When displaying `show thread poll` data add the
function we are supposed to call when the poll
event happens.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-06-13 15:14:04 -04:00
Donald Sharp
1ef14bee7a lib: Add check for non-preexisting thread
When adding a read/write poll event and we are using a developmental
build add a bit of code to ensure that we do not already have an read
or write event scheduled.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-06-13 15:14:04 -04:00
Donald Sharp
45f3d59084 lib: Prevent infinite loop in fd handling
If we have a case where have created a fd for i/o and we have
removed the handling thread but still have the fd in the poll
data structure, there existed a case where we would get
the handle this fd return from poll but we would immediately
do nothing with it because we didn't have a thread to hand
the event to.

This leads to an infinite loop.  Prevent the infinite loop
from happening and log the problem.

We still need to find the cause of this happening.  But
let's prevent the system from melting down in the mean time.

Fixes: #2796
Signed-off-by: David Lamparter <equinox@diac24.net>
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-06-13 15:14:04 -04:00
David Lamparter
051a0be4b2 *: fix some dumb printf format warnings
Some types like `time_t` vary across platforms and always need to be
cast when printed.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2019-06-11 13:34:57 +02:00
Donald Sharp
9a8a7b0ed1 lib, pimd, sharpd: Various output string cleanups
Various compilers in our CI system were complaining about various
auto-conversions.  Let's get these cleaned up a bit more.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-06-07 09:29:45 -04:00
Donald Sharp
6c19478a3b lib: Display to end user the MAX_FDS allowed
Upon startup FRR reads in the MAX_FDS variable from
it's control files via the getrlimit call.  We then
setup code to limit the poll data structure size to
that value.  The OS also limits our FD's to that value
because that is what is set.  Provide a methodology
that a interested end user can figure this data out.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-05-24 08:24:18 -04:00
Donald Sharp
61fa0b976a lib: 'show thread cpu' help strings referenced a weird option
The 'show thread cpu' command referenced a 'b' option.  Which
is not parsed at all in the parse_filter function.  As such
I do not know what this was referencing as that it has been
removed.  Update the help strings to reflect this reality.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-05-24 08:24:18 -04:00
Renato Westphal
81fddbe7ae *: rename new ForEach macros from the typesafe API
This is necessary to avoid a name collision with std::for_each
from C++.

Fixes the compilation of the gRPC northbound module.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
2019-05-21 15:59:08 -03:00
Quentin Young
d8b87afe7c lib: hashing functions should take const arguments
It doesn't make much sense for a hash function to modify its argument,
so const the hash input.

BGP does it in a couple places, those cast away the const. Not great but
not any worse than it was.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-05-14 21:23:08 +00:00
David Lamparter
8390828dac Revert "lib: use DECLARE_SKIPLIST for timers instead of pqueue"
This reverts commit 7c198e4e1a.
2019-04-29 21:18:55 +02:00
David Lamparter
7c198e4e1a lib: use DECLARE_SKIPLIST for timers instead of pqueue
Replaces the use of pqueue_* for the thread_master's timer list with an
instance of DECLARE_SKIPLIST_*.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-04-27 19:33:45 +02:00
David Lamparter
c284542b66 lib: use DECLARE_LIST for thread_list
Replaces the open-coded thread_list with a DECLARE_LIST instantiation.
Some function prototypes are actually identical to what was previously
open-coded.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-04-27 19:33:45 +02:00
Lou Berger
e9890df15d
Merge pull request #4073 from donaldsharp/undebug_me_or_not
some cleanup across multiple daemons
2019-04-04 10:08:34 -04:00
Donald Sharp
fa0069c647 lib, pbrd, zebra: Fix size_t type printf warnings on obscure platforms
Use the correct printf formater for those obscure platforms that
we build against.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-04-03 16:39:50 -04:00
Quentin Young
c17faa4b10 lib: reduce exported var symbols
Don't need these in our DSO tables

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-04-03 20:34:18 +00: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
Quentin Young
76f0146890 *: do not check XMALLOC / XCALLOC for null ret
They never return NULL

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2019-02-25 23:00:44 +00:00
Tim Bray
e3b78da875 *: Rename backet to bucket
Presume typo from original author

Signed-off-by: Tim Bray <tim@kooky.org>
2019-02-25 16:22:36 +00:00
David Lamparter
c8a65463b2 lib: make atomic ops C++ compatible
C++ doesn't have ISO C11 stdatomic.h or "_Atomic inttype", so use
std::atomic instead to get the headers compatible.

Signed-off-by: David Lamparter <equinox@diac24.net>
2019-02-11 15:49:49 -02: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
Donald Sharp
231db9a6e1 lib: Convert RUSAGE_SELF to RUSAGE_THREAD where we can
When using getrusage, we have multiple choices about what
to call for data gathering about this particular thread of execution.

RUSAGE_SELF -> This means gather all cpu run time for all pthreads associated
with this process.

RUSAGE_THREAD -> This means gather all cpu run time for this particular
pthread.

Clearly with data gathering for slow thread as well as `show thread cpu`
it would be preferable to gather only data about the current running
pthread.  This probably was the original behavior of using RUSAGE_SELF
when we didn't have multiple pthreads.  So it didn't matter so much.

Prior to this change, 10 iterations of 1 million routes install/remove
from zebra would give us this cpu time for the dataplane pthread:

Showing statistics for pthread Zebra dplane thread
--------------------------------------------------
                      CPU (user+system): Real (wall-clock):
Active   Runtime(ms)   Invoked Avg uSec Max uSecs Avg uSec Max uSecs  Type  Thread
    0     280902.149    326541      860   2609982      550   2468910    E  dplane_thread_loop

After this change we are seeing this:

Showing statistics for pthread Zebra dplane thread
--------------------------------------------------
                      CPU (user+system): Real (wall-clock):
Active   Runtime(ms)   Invoked Avg uSec Max uSecs Avg uSec Max uSecs  Type  Thread
    0      58045.560    334944      173    277226      539   2502268    E  dplane_thread_loop

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2019-01-09 12:18:21 -05:00
Christian Franke
78ca034252 lib: add thread_timer_remain_msec function
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
2018-12-04 12:49:25 +01:00
Donald Sharp
74df8d6d9d *: Replace hash_cmp function return value to a bool
The ->hash_cmp and linked list ->cmp functions were sometimes
being used interchangeably and this really is not a good
thing.  So let's modify the hash_cmp function pointer to return
a boolean and convert everything to use the new syntax.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-10-19 13:14:45 -04:00
Quentin Young
56f67d6870
Merge pull request #3087 from opensourcerouting/bfd-memleak
bfdd: fix memory leak and echo-mode start
2018-10-03 18:43:34 -04:00
Rafael Zalamena
c4345fbf71 lib: refactor thread_execute
Don't allocate threads in the stack, but use the standardized
`thread_get` and `thread_add_unused` to avoid creating corner cases in
the thread API.

This fixes a thread mutex memory leak in FreeBSD.

Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
2018-10-03 16:32:11 -03:00
Rafael Zalamena
6655966d2c lib: fix a memory leak in FreeBSD
Two important changes:
* Centralize the thread teardown procedure;
* Save and restore thread mutex context to avoid losing the memory
  pointer;

Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
2018-10-03 16:32:11 -03: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
Quentin Young
450971aa99 *: LIB_[ERR|WARN] -> EC_LIB
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-09-13 19:34:28 +00:00
Donald Sharp
9ef9495e3a lib: Convert thread.c to use new error-code subsystem
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-09-06 20:50:58 +00:00
Donald Sharp
6ed04aa2eb lib: Limit depth of unused thread list
The master->unused list was unbounded during normal operation.
A full BGP feed on my machine left 11k threads on the unused
list, taking up over 2mb of data.  This seemed a bit excessive,
reduce to a limit of 10.

Also fix a crash that this exposed where we assumed that a thread
structure was not deleted.

Future committers can make this configurable? or modify
the value to something better for their system.  I am
dubious of the value of this.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-08-23 09:23:46 -04:00
Donald Sharp
a6f235f341 lib: Seperate out Poll data from thread memory statistics
We were storing Poll data for the read and write
memory information in MTYPE_THREAD, so a show run
would not be able to show actual amount of memory
associated with the `struct thread`.

Remove unnecessary NULL checks on malloc.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-08-23 08:03:31 -04:00
Donald Sharp
9b864cd3cd lib: Ensure read/write fd's are in range
Ensure that we can properly handle a passed in read/write
fd for using in poll.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-08-01 19:41:47 +00:00
Donald Sharp
8872626bb4 lib, vtysh: Add 'show thread poll' command
Add a 'show thread poll' command that displays the
poll information and fd's setup.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2018-06-16 20:09:45 -04:00
paco
883cc51dfe lib: fix sizeof expression (Coverity 1455482)
Signed-off-by: F. Aragon <paco@voltanet.io>
2018-06-08 19:32:21 +02:00
Russ White
e0d9c8129c
Merge pull request #2099 from qlyoung/fix-cpu-thread-hist-race
lib: fix data race in thread history collection
2018-04-24 07:59:06 -04:00
Quentin Young
fbcac826c9 lib: fix data race in thread history collection
Thread statistics are collected and stored in a hashtable shared across
threads, but while the hashtable itself is protected by a mutex, the
records themselves were not being updated safely. Change all thread
history collection to use atomic operations.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-04-22 17:11:57 -04:00
Quentin Young
4f113d6066 lib: fix dynamic stack buffer overflow
oops

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-04-20 16:40:46 -04:00
Quentin Young
d7c0a89a3a
*: use C99 standard fixed-width integer types
The following types are nonstandard:
- u_char
- u_short
- u_int
- u_long
- u_int8_t
- u_int16_t
- u_int32_t

Replace them with the C99 standard types:
- uint8_t
- unsigned short
- unsigned int
- unsigned long
- uint8_t
- uint16_t
- uint32_t

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-03-27 15:13:34 -04:00
Renato Westphal
118c115620
Merge pull request #1831 from qlyoung/frr-pthread-fixups
lib: some frr_pthread fixes
2018-03-13 18:22:22 -03:00
Lou Berger
996c93142d *: conform with COMMUNITY.md formatting rules, via 'make indent'
Signed-off-by: Lou Berger <lberger@labn.net>
2018-03-06 14:04:32 -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
42d745387a
lib: avoid crash when cancelling invalid rw job
There are some observed instances where we end up trying to cancel a rw
job based on a file descriptor that we don't have a reference on. The
specific cancel function for rw jobs assumes it's called with a file
descriptor that is valid within pollfds and will cause a segmentation
fault by buffer overrun if this is not the case.

Instead log it and move on. Since the fd does not exist this should
patch over the buggy behavior and provide additional information to help
in finding the root cause.

Reviewed-by: Donald Sharp <sharpd@cumulusnetworks.com>
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2018-01-16 15:28:26 -05:00
Donald Sharp
522f7f9923 lib: Fix thread removal from a pqueue
When we remove a thread from a pqueue, use the saved
index to go to the correct spot immediately instead of
having to search the whole queue for it.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-11-29 14:53:59 -05:00
Donald Sharp
d1667f531f lib: Display unsigned instead of signed
When displaying thread cpu data, display unsigned instead
of signed data when we get really really really large
numbers of invocations.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-10-17 20:49:15 -04:00
Donald Sharp
acdf5e2510 *: Convert list_free usage to list_delete
list_free is occassionally being used to delete the
list and accidently not deleting all the nodes.
We keep running across this usage pattern.  Let's
remove the temptation and only allow list_delete
to handle list deletion.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-10-05 10:53:17 -04:00
Donald Sharp
affe9e9983 *: Convert list_delete(struct list *) to ** to allow nulling
Convert the list_delete(struct list *) function to use
struct list **.  This is to allow the list pointer to be nulled.

I keep running into uses of this list_delete function where we
forget to set the returned pointer to NULL and attempt to use
it and then experience a crash, usually after the developer
has long since left the building.

Let's make the api explicit in it setting the list pointer
to null.

Cynical Prediction:  This code will expose a attempt
to use the NULL'ed list pointer in some obscure bit
of code.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-10-05 10:53:13 -04:00
Renato Westphal
f5129394ec Merge pull request #1242 from donaldsharp/eigrp_tm
Eigrp tm
2017-09-27 09:20:51 -03:00
Quentin Young
33844bbe43
lib: destroy threadmaster condition variable
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-09-25 16:01:08 -04:00
Donald Sharp
1a0a92ea3b lib: Set pointer to NULL after free
list_delete does not set the list pointer to NULL
Thus when we accidently use it later we happily write
off into lala land instead of crashing imediately

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-09-25 15:41:28 -04:00
Quentin Young
4e1000a188
lib: free thread master name
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-09-08 16:39:53 -04:00
Donald Sharp
bd74dc610a lib: Hash creation cleanup
1) Some hash key functions where converting pointers
directly to a 32 bit value via downcasting.  Pointers
are 64 bit on a majority of our platforms.

2) Some hashes were being created with 256 entries,
downsize the hash creation size to more appropriate
values.

3) Add hash names to hash creation so we can watch
the hash via 'show debugging hashtable'

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-09-05 14:33:01 -04:00
Quentin Young
e3c9529ee9
lib: fix thread scheduling weirdness
Restores 3.0 behavior in terms of thread scheduling & prioritization.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-08-31 19:00:00 -04:00
Quentin Young
eff09c66a4
lib: fix still reachable linked list in thread.c
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-08-08 12:54:28 -04:00
Quentin Young
bca37d172a
lib: minor style fixes
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-08-03 11:25:24 -04:00
Quentin Young
d764d2cc50
lib: flush ready queue before poll() again
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-08-02 14:29:04 -04: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
David Lamparter
2d8270596a Merge remote-tracking branch 'frr/master' into newline-redux
Lots of conflicts from CMD_WARNING_CONFIG_FAILED...

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-14 13:07:30 +02:00
David Lamparter
625e016d14 *: remove VTYNL, part 6 of 6
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-14 10:20:03 +02:00
David Lamparter
6d3c2ed4ed *: remove VTYNL, part 1 of 6
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-14 10:20:02 +02:00
David Lamparter
181039f3d7 *: ditch vty_outln(), part 2 of 2
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-14 10:19:58 +02:00
David Lamparter
5c7571d43f *: ditch vty_outln(), part 1 of 2
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-13 20:29:22 +02:00
David Lamparter
151d7c3591 lib: drop off "masters" list on master_free()
Don't leave a dangling leaked reference.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-07-11 16:10:29 +02:00
Quentin Young
dfd19ccc3a
*: update hash_create(), hash_create_size()
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-07-01 19:18:37 -04:00
Quentin Young
fe232cf819 lib: printf bugfixes & improvement
* Fix bad format specifier in thread.[ch]
* Move PRINTF_ATTRIBUTE macro to zebra.h
* Use PRINTF_ATTRIBUTE on termtable printers

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-30 16:18:20 +00:00
Jafar Al-Gharaibeh
1d0ca3ebf3 Merge pull request #770 from donaldsharp/zclient
Zclient
2017-06-30 10:35:28 -05:00
Donald Sharp
85bf0f3237 lib: Fix clang warning
Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
2017-06-30 10:29:14 -04:00
Quentin Young
26eb0e4ff5 lib: fix merge error
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 23:40:57 +00:00
Quentin Young
972a411ccc lib: more show thread cpu
Add support for naming pthreads. Also, note that we don't have any
records yet if that's the case.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 23:40:56 +00:00
Quentin Young
62f4402239 lib: make show thread... commands mt-aware
This patch fixes up show thread commands so that they know about
and operate on all extant thread_masters, since we can now have multiple
running in any given application.

This change also eliminates a heap use after free that appears when
using a single cpu_record shared among multiple threads. Since struct
thread's have pointers to bits of memory that are freed when the global
statistics hash table is freed, later accesses are invalid. By moving
the stats hash to be unique to each thread_master this problem is
sidestepped.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 23:37:08 +00:00
Quentin Young
e0bebc7c22 lib: mt-safe tracebacks
can't be using them statics anymore sonny

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 23:32:15 +00:00
Quentin Young
0693f6fc75 lib: un-static trash buffer for pipe poker
data races

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 23:32:15 +00:00
Quentin Young
96ade3ed77 *: use vty_outln
Saves 400 lines

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-29 17:31:28 +00:00
Quentin Young
7feb7d7e65 lib: tighten up exit semantics for thread_fetch()
* Account for the pipe poker in poll() by explicitly returning NULL when
  we have no events, timers or file descriptors to work with
* Add a comment explaining exactly what we are doing and why

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-16 19:56:58 +00:00
Quentin Young
8797240ea4 lib: mt-safe cancel, round deux
* Update pollfds copy as well as the original
* Keep array count for copy in thread_master
* Remove last remnants of POLLHUP in .events field
* Remove unused snmpcount (lolwut)
* Improve docs
* Add missing do_thread_cancel() call in thread_cancel_event()
* Change thread_fetch() to always enter poll() to avoid starving i/o
* Remember to free up cancel_req when destroying thread_master
* Fix dereference of null pointer
* Fix dead store to timeval
* Fix missing condition for condition variable :-)

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-16 19:56:58 +00:00
Quentin Young
63ccb9cb12 lib: add mt-safe thread_cancel
This patch implements an MT-safe version of thread_cancel() in
thread_cancel_async(). Behavior as follows:

* Cancellation requests are queued into a list

* Cancellation requests made from the same pthread as the thread_master
  owner are serviced immediately (thread_cancel())

* Cancellation requests made from a separate pthread are queued and the
  call blocks on a condition variable until the owning pthread services
  the request, at which point the condition variable is signaled and
  execution continues (thread_cancel_async())

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-16 19:56:58 +00:00
Quentin Young
a587d00bac *: remove THREAD_BACKGROUND
it's just an alias for a millisecond timer used in exactly nine places
and serves only to complicate

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-08 01:53:50 +00:00
David Lamparter
fa135ba008 Merge pull request #648 from qlyoung/fix-pollfd-stack-overflow
lib: use heap for pollfds
2017-06-06 17:25:06 +02:00
Quentin Young
82fc5591f4 lib: allow infinite sleep in poll()
If fd_poll() is called with no file descriptors, an incorrect check in
the function prelude causes it to return instantly; for a thread that
wishes to poll but has no file descriptors, this results in busy
waiting. Desired behavior is to block.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-06-06 14:23:42 +00:00
Quentin Young
95db01eb22 lib: use heap for pollfds
a bunch of pollfds can cause a stack overflow when using a stack
allocated buffer...silly me...

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-31 23:21:40 +00:00
Quentin Young
8c88ac94fa lib: make writing end of pipe nonblocking
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-31 17:30:53 +00:00
Quentin Young
a772d6eae6 lib: missed AWAKEN in r/w thread scheduler
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-30 10:27:10 -04:00
Quentin Young
3bf2673b30 lib: allow pthreads to poke poll()
When scheduling a task onto a thread master owned by another pthread, we
need to lock the thread master's mutex. However, if the pthread which
owns that thread master is in poll(), we could be stuck waiting for a
very long time. To solve this, we copy all data poll() needs and unlock
during poll(). To break the target pthread out of poll(), thread_master
has gained a pipe whose reading end is passed into poll(). After an event
that requires immediate action by the target pthread, a byte is written
into the pipe in order to wake it up.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
[DL: split off from select() removal]
2017-05-30 10:27:10 -04:00
Quentin Young
75bcb3558d lib: remove select()
poll() is present on every supported platform and does not have an upper
limit on file descriptors.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
[DL: split off from AWAKEN() change]
2017-05-30 10:27:10 -04:00
Quentin Young
48cdf1a9d6 lib: enforce thread_cancel() MT-unsafe invariant
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-19 19:10:21 +00:00
Lou Berger
745ad980fe Merge pull request #538 from qlyoung/fix-stack-access-2
lib: return thread * from thread_add*
2017-05-17 07:34:30 -04:00
David Lamparter
34f99549ab Merge pull request #549 from qlyoung/fix-poll-eof
lib: correctly handle EOF when using poll()
2017-05-17 11:25:30 +02:00
Quentin Young
6ccdc60d96 lib: correctly handle EOF when using poll()
Descriptor owner should handle EOF, not thread.c

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-16 18:29:51 +00:00
Quentin Young
56a94b3668 lib: return thread pointer from thread_add*
Allow some more flexibility in case callers wish to manage their own
thread pointers and don't require or don't want the thread to keep a
back reference to its holding pointer.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-16 15:59:51 +00:00
David Lamparter
896014f4bc *: make consistent & update GPLv2 file headers
The FSF's address changed, and we had a mixture of comment styles for
the GPL file header.  (The style with * at the beginning won out with
580 to 141 in existing files.)

Note: I've intentionally left intact other "variations" of the copyright
header, e.g. whether it says "Zebra", "Quagga", "FRR", or nothing.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2017-05-15 16:37:41 +02:00
Quentin Young
368d025eca lib: fix access to stack value
Passing stack value to thread_add_* causes thread->ref to become an
invalid pointer when the value goes out of scope

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-15 06:29:54 +00:00
Quentin Young
32d86f8b7a lib: safely check & set thread pointers
When scheduling a thread, the scheduling function returns a pointer to
the struct thread that was placed on one of the scheduling queues in the
associated thread master. This pointer is used to check whether or not
the thread is scheduled, and is passed to thread_cancel() should the
daemon need to cancel that particular task.

The thread_fetch() function is called to retrieve the next thread to
execute. However, when it returns, the aforementioned pointer is not
updated. As a result, in order for the above use cases to work, every
thread handler function must set the associated pointer to NULL. This is
bug prone, and moreover, not thread safe.

This patch changes the thread scheduling functions to return void. If
the caller needs a reference to the scheduled thread, it must pass in a
pointer to store the pointer to the thread struct in. Subsequent calls
to thread_cancel(), thread_cancel_event() or thread_fetch() will result
in that pointer being nulled before return. These operations occur
within the thread_master critical sections.

Overall this should avoid bugs introduced by thread handler funcs
forgetting to null the associated pointer, double-scheduling caused by
overwriting pointers to currently scheduled threads without performing a
nullity check, and the introduction of true kernel threads causing race
conditions within the userspace threading world.

Also removes the return value for thread_execute since it always returns
null...

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-09 20:44:22 +00:00
Quentin Young
ffa2c8986d *: remove THREAD_ON macros, add nullity check
The way thread.c is written, a caller who wishes to be able to cancel a
thread or avoid scheduling it twice must keep a reference to the thread.
Typically this is done with a long lived pointer whose value is checked
for null in order to know if the thread is currently scheduled.  The
check-and-schedule idiom is so common that several wrapper macros in
thread.h existed solely to provide it.

This patch removes those macros and adds a new parameter to all
thread_add_* functions which is a pointer to the struct thread * to
store the result of a scheduling call. If the value passed is non-null,
the thread will only be scheduled if the value is null. This helps with
consistency.

A Coccinelle spatch has been used to transform code of the form:

  if (t == NULL)
    t = thread_add_* (...)

to the form

  thread_add_* (..., &t)

The THREAD_ON macros have also been transformed to the underlying
thread.c calls.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2017-05-09 20:44:19 +00:00
Donald Sharp
6281320446 Merge remote-tracking branch 'origin/stable/3.0' 2017-05-08 16:51:21 -04:00