The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
network.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: f7ffbb3265ec824569bd31d56010085b1e92a499 $
19 *
20 * @brief Receiver of socket data, which sends messages to the workers.
21 * @file io/network.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: f7ffbb3265ec824569bd31d56010085b1e92a499 $")
26
27#define LOG_PREFIX nr->name
28
29#define LOG_DST nr->log
30
31#include <freeradius-devel/util/event.h>
32#include <freeradius-devel/util/misc.h>
33#include <freeradius-devel/util/rand.h>
34#include <freeradius-devel/util/rb.h>
35#include <freeradius-devel/util/syserror.h>
36#include <freeradius-devel/util/atexit.h>
37#include <freeradius-devel/util/talloc.h>
38
39#include <freeradius-devel/io/channel.h>
40#include <freeradius-devel/io/control.h>
41#include <freeradius-devel/io/listen.h>
42#include <freeradius-devel/io/network.h>
43#include <freeradius-devel/io/queue.h>
44#include <freeradius-devel/io/ring_buffer.h>
45#include <freeradius-devel/io/worker.h>
46
47#define MAX_WORKERS 64
48
49static _Thread_local fr_ring_buffer_t *fr_network_rb;
50
57
58/** Associate a worker thread with a network thread
59 *
60 */
61typedef struct {
62 fr_heap_index_t heap_id; //!< workers are in a heap
63 fr_time_delta_t cpu_time; //!< how much CPU time this worker has spent
64 fr_time_delta_t predicted; //!< predicted processing time for one packet
65
66 bool blocked; //!< is this worker blocked?
67
68 fr_channel_t *channel; //!< channel to the worker
69 fr_worker_t *worker; //!< worker pointer
72
73typedef struct {
74 fr_rb_node_t listen_node; //!< rbtree node for looking up by listener.
75 fr_rb_node_t num_node; //!< rbtree node for looking up by number.
76
77 fr_network_t *nr; //!< O(N) issues in talloc
78 int number; //!< unique ID
79 fr_heap_index_t heap_id; //!< for the sockets_by_num heap
80
81 fr_event_filter_t filter; //!< what type of filter it is
82
83 bool dead; //!< is it dead?
84 bool blocked; //!< is it blocked?
85
86 unsigned int outstanding; //!< number of outstanding packets sent to the worker
87 fr_listen_t *listen; //!< I/O ctx and functions.
88
89 fr_message_set_t *ms; //!< message buffers for this socket.
90 fr_channel_data_t *cd; //!< cached in case of allocation & read error
91 size_t leftover; //!< leftover data from a previous read
92 size_t written; //!< however much we did in a partial write
93
94 fr_channel_data_t *pending; //!< the currently pending partial packet
95 fr_heap_t *waiting; //!< packets waiting to be written
98
99/*
100 * We have an array of workers, so we can index the workers in
101 * O(1) time. remove the heap of "workers ordered by CPU time"
102 * when we send a packet to a worker, just update the predicted
103 * CPU time in place. when we receive a reply from a worker,
104 * just update the predicted CPU time in place.
105 *
106 * when we need to choose a worker, pick 2 at random, and then
107 * choose the one with the lowe cpu time. For background, see
108 * "Power of Two-Choices" and
109 * https://www.eecs.harvard.edu/~michaelm/postscripts/mythesis.pdf
110 * https://www.eecs.harvard.edu/~michaelm/postscripts/tpds2001.pdf
111 */
113 char const *name; //!< Network ID for logging.
114
115 pthread_t thread_id; //!< for self
116
117 bool suspended; //!< whether or not we're suspended.
118
119 fr_log_t const *log; //!< log destination
120 fr_log_lvl_t lvl; //!< debug log level
121
122 fr_atomic_queue_t *aq_control; //!< atomic queue for control messages sent to me
123
124 fr_control_t *control; //!< the control plane
125
126 fr_ring_buffer_t *rb; //!< ring buffer for my control-plane messages
127
128 fr_event_list_t *el; //!< our event list
129
130 fr_heap_t *replies; //!< replies from the worker, ordered by priority / origin time
131
133
134 fr_rb_tree_t *sockets; //!< list of sockets we're managing, ordered by the listener
135 fr_rb_tree_t *sockets_by_num; //!< ordered by number;
136
137 int num_workers; //!< number of active workers
138 int num_blocked; //!< number of blocked workers
139 int num_pending_workers; //!< number of workers we're waiting to start.
140 int max_workers; //!< maximum number of allowed workers
141 int num_sockets; //!< actually a counter...
142
143 int signal_pipe[2]; //!< Pipe for signalling the worker in an orderly way.
144 ///< This is more deterministic than using async signals.
145
146 bool exiting; //!< are we exiting?
147
148 fr_network_config_t config; //!< configuration
150};
151
152static void fr_network_post_event(fr_event_list_t *el, fr_time_t now, void *uctx);
153static int fr_network_pre_event(fr_time_t now, fr_time_delta_t wake, void *uctx);
155static void fr_network_read(UNUSED fr_event_list_t *el, int sockfd, UNUSED int flags, void *ctx);
156
157static int8_t reply_cmp(void const *one, void const *two)
158{
159 fr_channel_data_t const *a = one, *b = two;
160 int ret;
161
162 ret = CMP(a->priority, b->priority);
163 if (ret != 0) return ret;
164
165 return fr_time_cmp(a->m.when, b->m.when);
166}
167
168static int8_t waiting_cmp(void const *one, void const *two)
169{
170 fr_channel_data_t const *a = one, *b = two;
171 int ret;
172
173 ret = CMP(a->priority, b->priority);
174 if (ret != 0) return ret;
175
176 return fr_time_cmp(a->reply.request_time, b->reply.request_time);
177}
178
179static int8_t socket_listen_cmp(void const *one, void const *two)
180{
181 fr_network_socket_t const *a = one, *b = two;
182
183 return CMP(a->listen, b->listen);
184}
185
186static int8_t socket_num_cmp(void const *one, void const *two)
187{
188 fr_network_socket_t const *a = one, *b = two;
189
190 return CMP(a->number, b->number);
191}
192
193/*
194 * Explicitly cleanup the memory allocated to the ring buffer,
195 * just in case valgrind complains about it.
196 */
197static int _fr_network_rb_free(void *arg)
198{
199 return talloc_free(arg);
200}
201
202/** Initialise thread local storage
203 *
204 * @return fr_ring_buffer_t for messages
205 */
207{
209
210 rb = fr_network_rb;
211 if (rb) return rb;
212
214 if (!rb) {
215 fr_perror("Failed allocating memory for network ring buffer");
216 return NULL;
217 }
218
220
221 return rb;
222}
223
224static inline bool is_network_thread(fr_network_t const *nr)
225{
226 return (pthread_equal(pthread_self(), nr->thread_id) != 0);
227}
228
230
231/** Add a fr_listen_t to a network
232 *
233 * @param nr the network
234 * @param li the listener
235 */
237{
239
240 /*
241 * Associate the protocol dictionary with the listener, so that the decode functions can check /
242 * use it.
243 *
244 * A virtual server may start off with a "dictionary" block, and therefore define a local
245 * dictionary. So the "root" dictionary of a virtual server may not be a protocol dict.
246 */
247 fr_assert(li->server_cs != NULL);
249
250 fr_assert(li->dict != NULL);
251
252 /*
253 * Skip a bunch of work if we're already in the network thread.
254 */
255 if (is_network_thread(nr) && !li->needs_full_setup) {
256 return fr_network_listen_add_self(nr, li);
257 }
258
259 rb = fr_network_rb_init();
260 if (!rb) return -1;
261
262 return fr_control_message_send(nr->control, rb, FR_CONTROL_ID_LISTEN, &li, sizeof(li));
263}
264
265
266/** Delete a socket from a network. MUST be called only by the listener itself!.
267 *
268 * @param nr the network
269 * @param li the listener
270 */
272{
274
276
277 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
278 if (!s) return -1;
279
281
282 return 0;
283}
284
285/** Add a "watch directory" call to a network
286 *
287 * @param nr the network
288 * @param li the listener
289 */
291{
293
294 rb = fr_network_rb_init();
295 if (!rb) return -1;
296
297 return fr_control_message_send(nr->control, rb, FR_CONTROL_ID_DIRECTORY, &li, sizeof(li));
298}
299
300/** Add a worker to a network in a different thread
301 *
302 * @param nr the network
303 * @param worker the worker
304 */
306{
308
309 rb = fr_network_rb_init();
310 if (!rb) return -1;
311
312 (void) talloc_get_type_abort(nr, fr_network_t);
313 (void) talloc_get_type_abort(worker, fr_worker_t);
314
315 return fr_control_message_send(nr->control, rb, FR_CONTROL_ID_WORKER, &worker, sizeof(worker));
316}
317
318static void fr_network_worker_started_callback(void *ctx, void const *data, size_t data_size, fr_time_t now);
319
320/** Add a worker to a network in the same thread
321 *
322 * @param nr the network
323 * @param worker the worker
324 */
326{
327 fr_network_worker_started_callback(nr, &worker, sizeof(worker), fr_time_wrap(0));
328}
329
330
331/** Signal the network to read from a listener
332 *
333 * @param nr the network
334 * @param li the listener to read from
335 */
337{
339
340 (void) talloc_get_type_abort(nr, fr_network_t);
342
343 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
344 if (!s) return;
345
346 /*
347 * Go read the socket.
348 */
349 fr_network_read(nr->el, s->listen->fd, 0, s);
350}
351
352
353/** Inject a packet for a listener to write
354 *
355 * @param nr the network
356 * @param li the listener where the packet is being injected
357 * @param packet the packet to be written
358 * @param packet_len the length of the packet
359 * @param packet_ctx The packet context to write
360 * @param request_time when the packet was received.
361 */
362void fr_network_listen_write(fr_network_t *nr, fr_listen_t *li, uint8_t const *packet, size_t packet_len,
363 void *packet_ctx, fr_time_t request_time)
364{
365 fr_message_t *lm;
367
368 cd = (fr_channel_data_t) {
369 .m = (fr_message_t) {
371 .data_size = packet_len,
372 .when = request_time,
373 },
374
375 .channel = {
376 .heap_id = FR_HEAP_INDEX_INVALID,
377 },
378
379 .listen = li,
380 .priority = PRIORITY_NOW,
381 .reply.request_time = request_time,
382 };
383
384 memcpy(&cd.m.data, &packet, sizeof(packet)); /* const issues */
385 memcpy(&cd.packet_ctx, &packet_ctx, sizeof(packet_ctx)); /* const issues */
386
387 /*
388 * Localize the message and insert it into the heap of pending messages.
389 */
390 lm = fr_message_localize(nr, &cd.m, sizeof(cd));
391 if (!lm) return;
392
393 if (fr_heap_insert(&nr->replies, lm) < 0) {
394 fr_message_done(lm);
395 }
396}
397
398
399/** Inject a packet for a listener to read
400 *
401 * @param nr the network
402 * @param li the listener where the packet is being injected
403 * @param packet the packet to be injected
404 * @param packet_len the length of the packet
405 * @param recv_time when the packet was received.
406 * @return
407 * - <0 on error
408 * - 0 on success
409 */
410int fr_network_listen_inject(fr_network_t *nr, fr_listen_t *li, uint8_t const *packet, size_t packet_len, fr_time_t recv_time)
411{
412 int rcode;
414 fr_network_inject_t my_inject;
415
416 /*
417 * Can't inject to injection-less destinations.
418 */
419 if (!li->app_io->inject) {
420 fr_strerror_const("Listener cannot accept injected packet");
421 return -1;
422 }
423
424 /*
425 * Avoid a bounce through the event loop if we're being called from the network thread.
426 */
427 if (is_network_thread(nr)) {
429
430 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
431 if (!s) {
432 fr_strerror_const("Listener was not found for injected packet");
433 return -1;
434 }
435
436 /*
437 * Inject the packet. The master.c mod_read() routine will then take care of avoiding
438 * IO, and instead return the packet to the network side.
439 */
440 if (li->app_io->inject(li, packet, packet_len, recv_time) == 0) {
441 (void) fr_network_read(nr->el, li->fd, 0, s);
442 }
443
444 return 0;
445 }
446
447 rb = fr_network_rb_init();
448 if (!rb) return -1;
449
450 my_inject.listen = li;
451 MEM(my_inject.packet = talloc_memdup(NULL, packet, packet_len));
452 my_inject.packet_len = packet_len;
453 my_inject.recv_time = recv_time;
454
455 rcode = fr_control_message_send(nr->control, rb, FR_CONTROL_ID_INJECT, &my_inject, sizeof(my_inject));
456 if (rcode < 0) talloc_free(my_inject.packet);
457
458 return rcode;
459}
460
463 { 0 }
464};
465
468 { 0 }
469};
470
473 { 0 }
474};
475
478 { 0 }
479};
480
482{
485
486 if (nr->suspended) return;
487
488 for (s = fr_rb_iter_init_inorder(nr->sockets, &iter);
489 s != NULL;
490 s = fr_rb_iter_next_inorder(nr->sockets, &iter)) {
492 }
493 nr->suspended = true;
494}
495
497{
500
501 if (!nr->suspended) return;
502
503 for (s = fr_rb_iter_init_inorder(nr->sockets, &iter);
504 s != NULL;
505 s = fr_rb_iter_next_inorder(nr->sockets, &iter)) {
507 }
508 nr->suspended = false;
509}
510
511#define IALPHA (8)
512#define RTT(_old, _new) fr_time_delta_wrap((fr_time_delta_unwrap(_new) + (fr_time_delta_unwrap(_old) * (IALPHA - 1))) / IALPHA)
513
514/** Callback which handles a message being received on the network side.
515 *
516 * @param[in] ctx the network
517 * @param[in] ch the channel that the message is on.
518 * @param[in] cd the message (if any) to start with
519 */
521{
522 fr_network_t *nr = ctx;
523 fr_network_worker_t *worker;
524
525 cd->channel.ch = ch;
526
527 /*
528 * Update stats for the worker.
529 */
531 worker->stats.out++;
532 worker->cpu_time = cd->reply.cpu_time;
533 if (!fr_time_delta_ispos(worker->predicted)) {
534 worker->predicted = cd->reply.processing_time;
535 } else {
536 worker->predicted = RTT(worker->predicted, cd->reply.processing_time);
537 }
538
539 /*
540 * Unblock the worker.
541 */
542 if (worker->blocked) {
543 worker->blocked = false;
544 nr->num_blocked--;
546 }
547
548 /*
549 * Ensure that heap insert works.
550 */
551 cd->channel.heap_id = FR_HEAP_INDEX_INVALID;
552 if (fr_heap_insert(&nr->replies, cd) < 0) {
553 fr_message_done(&cd->m);
554 fr_assert(0 == 1);
555 }
556}
557
558/** Handle a network control message callback for a channel
559 *
560 * This is called from the event loop when we get a notification
561 * from the event signalling pipe.
562 *
563 * @param[in] ctx the network
564 * @param[in] data the message
565 * @param[in] data_size size of the data
566 * @param[in] now the current time
567 */
568static void fr_network_channel_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
569{
571 fr_channel_t *ch;
572 fr_network_t *nr = ctx;
573
574 ce = fr_channel_service_message(now, &ch, data, data_size);
575 DEBUG3("Channel %s",
576 fr_table_str_by_value(channel_signals, ce, "<INVALID>"));
577 switch (ce) {
578 case FR_CHANNEL_ERROR:
579 return;
580
581 case FR_CHANNEL_EMPTY:
582 return;
583
584 case FR_CHANNEL_NOOP:
585 break;
586
588 fr_assert(ch != NULL);
589 while (fr_channel_recv_reply(ch));
590 break;
591
593 fr_assert(0 == 1);
594 break;
595
596 case FR_CHANNEL_OPEN:
597 fr_assert(0 == 1);
598 break;
599
600 case FR_CHANNEL_CLOSE:
601 {
602 fr_network_worker_t *w = talloc_get_type_abort(fr_channel_requestor_uctx_get(ch),
604 int i;
605
606 /*
607 * Remove this worker from the array
608 */
609 DEBUG3("Worker acked our close request");
610 for (i = 0; i < nr->num_workers; i++) {
611 if (nr->workers[i] == w) {
612 if (i == (nr->num_workers - 1)) break;
613
614 /*
615 * Close the hole...
616 */
617 memmove(&nr->workers[i], &nr->workers[i + 1],
618 (uint8_t *) &nr->workers[nr->num_workers] - (uint8_t *) &nr->workers[i + 1]);
619 break;
620 }
621 }
622 nr->num_workers--;
623 nr->workers[nr->num_workers] = NULL; /* over-write now unused pointer */
624 }
625 break;
626 }
627}
628
629#define OUTSTANDING(_x) ((_x)->stats.in - (_x)->stats.out)
630
631/** Send a message on the "best" channel.
632 *
633 * @param nr the network
634 * @param cd the message we've received
635 */
637{
638 fr_network_worker_t *worker;
639
640 (void) talloc_get_type_abort(nr, fr_network_t);
641
642 if (!nr->num_workers) {
643 RATE_LIMIT_GLOBAL(ERROR, "Failed sending packet to worker - "
644 "No workers are available");
645 return -1;
646 }
647
648retry:
649 if (nr->num_workers == 1) {
650 worker = nr->workers[0];
651 if (worker->blocked) {
652 RATE_LIMIT_GLOBAL(ERROR, "Failed sending packet to worker - "
653 "In single-threaded mode and worker is blocked");
654 drop:
655 worker->stats.dropped++;
656 return -1;
657 }
658
659 } else if (nr->num_blocked == 0) {
660 int64_t cmp;
661 uint32_t one, two;
662
663 one = fr_rand() % nr->num_workers;
664 do {
665 two = fr_rand() % nr->num_workers;
666 } while (two == one);
667
668 /*
669 * Choose a worker based on minimizing the amount
670 * of future work it's being asked to do.
671 *
672 * If both workers have the same number of
673 * outstanding requests, then choose the worker
674 * which has used the least total CPU time.
675 */
676 cmp = (OUTSTANDING(nr->workers[one]) - OUTSTANDING(nr->workers[two]));
677 if (cmp < 0) {
678 worker = nr->workers[one];
679
680 } else if (cmp > 0) {
681 worker = nr->workers[two];
682
683 } else if (fr_time_delta_lt(nr->workers[one]->cpu_time, nr->workers[two]->cpu_time)) {
684 worker = nr->workers[one];
685
686 } else {
687 worker = nr->workers[two];
688 }
689 } else {
690 int i;
691 uint64_t min_outstanding = UINT64_MAX;
692 fr_network_worker_t *found = NULL;
693
694 /*
695 * Some workers are blocked. Pick the worker
696 * with the least amount of future work to do.
697 */
698 for (i = 0; i < nr->num_workers; i++) {
699 uint64_t outstanding;
700
701 worker = nr->workers[i];
702 if (worker->blocked) continue;
703
704 outstanding = OUTSTANDING(worker);
705 if ((outstanding < min_outstanding) || !found) {
706 found = worker;
707 min_outstanding = outstanding;
708
709 } else if (outstanding == min_outstanding) {
710 /*
711 * Queue lengths are the same.
712 * Choose this worker if it's
713 * less busy than the previous one we found.
714 */
715 if (fr_time_delta_lt(worker->cpu_time, found->cpu_time)) {
716 found = worker;
717 }
718 }
719 }
720
721 if (!found) {
722 RATE_LIMIT_GLOBAL(PERROR, "Failed sending packet to worker - Couldn't find active worker, "
723 "%u/%u workers are blocked", nr->num_blocked, nr->num_workers);
724 return -1;
725 }
726
727 worker = found;
728 }
729
730 (void) talloc_get_type_abort(worker, fr_network_worker_t);
731
732 /*
733 * Too many outstanding packets for this worker. Drop
734 * the request.
735 *
736 * If the worker we've picked has too many outstanding
737 * packets, then we have either only one worker, in which
738 * cae we should drop the packet. Or, we were unable to
739 * find a worker with smaller than max_outstanding
740 * packets. In which case all of the workers are likely
741 * at max_outstanding.
742 *
743 * In both cases, we should just drop the new packet.
744 */
745 fr_assert(worker->stats.in >= worker->stats.out);
746 if (nr->config.max_outstanding &&
747 (OUTSTANDING(worker) >= nr->config.max_outstanding)) {
748 RATE_LIMIT_GLOBAL(PERROR, "max_outstanding reached - dropping packet");
749 goto drop;
750 }
751
752 /*
753 * Send the message to the channel. If we fail, drop the
754 * packet. The only reason for failure is that the
755 * worker isn't servicing it's input queue. When that
756 * happens, we have no idea what to do, and the whole
757 * thing falls over.
758 */
759 if (fr_channel_send_request(worker->channel, cd) < 0) {
760 worker->stats.dropped++;
761 worker->blocked = true;
762 nr->num_blocked++;
763
764 RATE_LIMIT_GLOBAL(PERROR, "Failed sending packet to worker - %u/%u workers are blocked",
765 nr->num_blocked, nr->num_workers);
766
767 if (nr->num_blocked == nr->num_workers) {
769 return -1;
770 }
771 goto retry;
772 }
773
774 worker->stats.in++;
775
776 /*
777 * We're projecting that the worker will use more CPU
778 * time to process this request. The CPU time will be
779 * updated with a more accurate number when we receive a
780 * reply from this channel.
781 */
782 worker->cpu_time = fr_time_delta_add(worker->cpu_time, worker->predicted);
783
784 return 0;
785}
786
787
788/** Send a packet to the worker.
789 *
790 * MUST only be called from the network thread.
791 *
792 * @param nr the network
793 * @param parent the parent listener
794 * @param li the listener that the packet was "read" from. Can be "parent"
795 * @param buffer the packet to send
796 * @param buflen size of the packet to send
797 * @param recv_time of the packet
798 * @param packet_ctx for the packet
799 * @return
800 * - <0 on error
801 * - 0 on success
802 */
804 const uint8_t *buffer, size_t buflen, fr_time_t recv_time, void *packet_ctx)
805{
808
809 (void) talloc_get_type_abort(nr, fr_network_t);
811
812 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
813 if (!s) return -1;
814
815 cd = (fr_channel_data_t *) fr_message_alloc(s->ms, NULL, buflen);
816 if (!cd) return -1;
817
818 cd->listen = parent;
820 cd->packet_ctx = packet_ctx;
821 cd->request.recv_time = recv_time;
822 memcpy(cd->m.data, buffer, buflen);
823 cd->m.when = fr_time();
824
825 if (fr_network_send_request(nr, cd) < 0) {
827 fr_message_done(&cd->m);
828 nr->stats.dropped++;
829 s->stats.dropped++;
830 return -1;
831 }
832
833 s->outstanding++;
834 return 0;
835}
836
837/** Get the number of outstanding packets
838 *
839 * @param nr the network
840 * @param li the listener that the packet was "read" from
841 * @return
842 * - <0 on error
843 * - the number of outstanding packets
844*/
847
848 (void) talloc_get_type_abort(nr, fr_network_t);
850
851 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
852 if (!s) return -1;
853
854 return s->outstanding;
855}
856
857/*
858 * Mark it as dead, but DON'T free it until all of the replies
859 * have come in.
860 */
862{
863 int i;
864
865 if (s->dead) return;
866
867 s->dead = true;
868
869 /*
870 * This FD is no longer part of the event loop.
871 */
872 fr_event_fd_delete(nr->el, s->listen->fd, s->filter);
873
874 for (i = 0; i < nr->max_workers; i++) {
875 if (!nr->workers[i]) continue;
876
877 (void) fr_worker_listen_cancel(nr->workers[i]->worker, s->listen);
878 }
879
880 /*
881 * If there are no outstanding packets, then we can free
882 * it now.
883 */
884 if (!s->outstanding) {
885 talloc_free(s);
886 return;
887 }
888
889 /*
890 * There are still outstanding packets. Leave it in the
891 * socket tree, so that replies from the worker can find
892 * it. When we've received all of the replies, then
893 * fr_network_post_event() will clean up this socket.
894 */
895}
896
897/** Read a packet from the network.
898 *
899 * @param[in] el the event list.
900 * @param[in] sockfd the socket which is ready to read.
901 * @param[in] flags from kevent.
902 * @param[in] ctx the network socket context.
903 */
904static void fr_network_read(UNUSED fr_event_list_t *el, int sockfd, UNUSED int flags, void *ctx)
905{
906 int num_messages = 0;
907 fr_network_socket_t *s = ctx;
908 fr_network_t *nr = s->nr;
909 ssize_t data_size;
910 fr_channel_data_t *cd, *next;
911
912 if (!fr_cond_assert_msg(s->listen->fd == sockfd, "Expected listen->fd (%u) to be equal event fd (%u)",
913 s->listen->fd, sockfd)) return;
914
915 DEBUG3("Reading data from FD %u", sockfd);
916
917 if (!s->cd) {
919 if (!cd) {
920 ERROR("Failed allocating message size %zd! - Closing socket",
923 return;
924 }
925 } else {
926 cd = s->cd;
927 }
928
929 fr_assert(cd->m.data != NULL);
930
931next_message:
932 /*
933 * Poll this socket, but not too often. We have to go
934 * service other sockets, too.
935 */
936 if (num_messages > 16) {
937 s->cd = cd;
938 return;
939 }
940
942
943 /*
944 * Read data from the network.
945 *
946 * Return of 0 means "no data", which is fine for UDP.
947 * For TCP, if an underlying read() on the TCP socket
948 * returns 0, (which signals that the FD is no longer
949 * usable) this function should return -1, so that the
950 * network side knows that it needs to close the
951 * connection.
952 */
953 data_size = s->listen->app_io->read(s->listen, &cd->packet_ctx, &cd->request.recv_time,
954 cd->m.data, cd->m.rb_size, &s->leftover);
955 if (data_size == 0) {
956 /*
957 * Cache the message for later. This is
958 * important for stream sockets, which can do
959 * partial reads into the current buffer. We
960 * need to be able to give the same buffer back
961 * to the stream socket for subsequent reads.
962 *
963 * Since we have a message set for each
964 * fr_io_socket_t, no "head of line"
965 * blocking issues can happen for stream sockets.
966 */
967 s->cd = cd;
968 return;
969 }
970
971 /*
972 * Error: close the connection, and remove the fr_listen_t
973 */
974 if (data_size < 0) {
975// fr_log(nr->log, L_DBG_ERR, "error from transport read on socket %d", sockfd);
977 return;
978 }
979 s->cd = NULL;
980
981 DEBUG3("Read %zd byte(s) from FD %u", data_size, sockfd);
982 if (s->listen->read_hexdump) HEXDUMP2(cd->m.data, data_size, "%s read ", s->listen->name);
983 nr->stats.in++;
984 s->stats.in++;
985
986 /*
987 * Initialize the rest of the fields of the channel data.
988 *
989 * We always use "now" as the time of the message, as the
990 * packet MAY be a duplicate packet magically resurrected
991 * from the past. i.e. If the read routines are doing
992 * dedup, then they notice that the packet is a
993 * duplicate. In that case, they send over a copy of the
994 * packet, BUT with the original timestamp. This
995 * information tells the worker that the packet is a
996 * duplicate.
997 */
998 cd->m.when = fr_time();
999 cd->listen = s->listen;
1000
1001 /*
1002 * Nothing in the buffer yet. Allocate room for one
1003 * packet.
1004 */
1005 if ((cd->m.data_size == 0) && (!s->leftover)) {
1006
1007 (void) fr_message_alloc(s->ms, &cd->m, data_size);
1008 next = NULL;
1009
1010 } else {
1011 /*
1012 * There are leftover bytes in the buffer, feed
1013 * them to the next round of reading.
1014 */
1015 if (s->leftover) {
1016 next = (fr_channel_data_t *) fr_message_alloc_reserve(s->ms, &cd->m, data_size, s->leftover,
1018 if (!next) {
1019 PERROR("Failed reserving partial packet.");
1020 // @todo - probably close the socket...
1021 fr_assert(0 == 1);
1022 }
1023 } else {
1024 next = NULL;
1025 }
1026 }
1027
1028 /*
1029 * Set the priority. Which incidentally also checks if
1030 * we're allowed to read this particular kind of packet.
1031 *
1032 * That check is because the app_io handlers just read
1033 * packets, and don't really have access to the parent
1034 * "list of allowed packet types". So we have to do the
1035 * work here in a callback.
1036 *
1037 * That should probably be fixed...
1038 */
1039 if (s->listen->app->priority) {
1040 int priority;
1041
1042 priority = s->listen->app->priority(s->listen->app_instance, cd->m.data, data_size);
1043 if (priority <= 0) goto discard;
1044
1045 cd->priority = priority;
1046 }
1047
1048 if (fr_network_send_request(nr, cd) < 0) {
1049 discard:
1050 talloc_free(cd->packet_ctx); /* not sure what else to do here */
1051 fr_message_done(&cd->m);
1052 nr->stats.dropped++;
1053 s->stats.dropped++;
1054
1055 } else {
1056 /*
1057 * One more packet sent to a worker.
1058 */
1059 s->outstanding++;
1060 }
1061
1062 /*
1063 * If there is a next message, go read it from the buffer.
1064 *
1065 * @todo - note that this calls read(), even if the
1066 * app_io has paused the reader. We likely want to be
1067 * able to check that, too. We might just remove this
1068 * "goto"...
1069 */
1070 if (next) {
1071 cd = next;
1072 num_messages++;
1073 goto next_message;
1074 }
1075}
1076
1077int fr_network_sendto_worker(fr_network_t *nr, fr_listen_t *li, void *packet_ctx, uint8_t const *data, size_t data_len, fr_time_t recv_time)
1078{
1081
1082 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
1083 if (!s) return -1;
1084
1085 cd = (fr_channel_data_t *) fr_message_alloc(s->ms, NULL, data_len);
1086 if (!cd) return -1;
1087
1088 s->stats.in++;
1089
1091
1092 cd->m.when = recv_time;
1093 cd->listen = li;
1094 cd->packet_ctx = packet_ctx;
1095
1096 memcpy(cd->m.data, data, data_len);
1097
1098 if (fr_network_send_request(nr, cd) < 0) {
1099 talloc_free(packet_ctx);
1100 fr_message_done(&cd->m);
1101 nr->stats.dropped++;
1102 s->stats.dropped++;
1103 return -1;
1104 }
1105
1106 /*
1107 * One more packet sent to a worker.
1108 */
1109 s->outstanding++;
1110 return 0;
1111}
1112
1113
1114/** Get a notification that a vnode changed
1115 *
1116 * @param[in] el the event list.
1117 * @param[in] sockfd the socket which is ready to read.
1118 * @param[in] fflags from kevent.
1119 * @param[in] ctx the network socket context.
1120 */
1121static void fr_network_vnode_extend(UNUSED fr_event_list_t *el, int sockfd, int fflags, void *ctx)
1122{
1123 fr_network_socket_t *s = ctx;
1124 fr_network_t *nr = s->nr;
1125
1126 if (!fr_cond_assert(s->listen->fd == sockfd)) return;
1127
1128 DEBUG3("network vnode");
1129
1130 /*
1131 * Tell the IO handler that something has happened to the
1132 * file.
1133 */
1134 s->listen->app_io->vnode(s->listen, fflags);
1135}
1136
1137
1138/** Handle errors for a socket.
1139 *
1140 * @param[in] el the event list
1141 * @param[in] sockfd the socket which has a fatal error.
1142 * @param[in] flags returned by kevent.
1143 * @param[in] fd_errno returned by kevent.
1144 * @param[in] ctx the network socket context.
1145 */
1147 int fd_errno, void *ctx)
1148{
1149 fr_network_socket_t *s = ctx;
1150 fr_network_t *nr = s->nr;
1151
1152 if (s->listen->app_io->error) {
1153 s->listen->app_io->error(s->listen);
1154
1155 } else if (flags & EV_EOF) {
1156 DEBUG2("Socket %s closed by peer", s->listen->name);
1157
1158 } else {
1159 ERROR("Socket %s errored - %s", s->listen->name, fr_syserror(fd_errno));
1160 }
1161
1163}
1164
1165
1166/** Write packets to the network.
1167 *
1168 * @param el the event list
1169 * @param sockfd the socket which is ready to write
1170 * @param flags returned by kevent.
1171 * @param ctx the network socket context.
1172 */
1173static void fr_network_write(UNUSED fr_event_list_t *el, UNUSED int sockfd, UNUSED int flags, void *ctx)
1174{
1175 fr_network_socket_t *s = ctx;
1176 fr_listen_t *li = s->listen;
1177 fr_network_t *nr = s->nr;
1179
1180 (void) talloc_get_type_abort(nr, fr_network_t);
1181
1182 /*
1183 * Start with the currently pending message, and then
1184 * work through the priority heap.
1185 */
1186 if (s->pending) {
1187 cd = s->pending;
1188 s->pending = NULL;
1189
1190 } else {
1191 cd = fr_heap_pop(&s->waiting);
1192 }
1193
1194 while (cd != NULL) {
1195 int rcode;
1196
1197 fr_assert(li == cd->listen);
1198 if (li->write_hexdump) HEXDUMP2(cd->m.data, cd->m.data_size, "%s writing ", li->name);
1199 rcode = li->app_io->write(li, cd->packet_ctx,
1200 cd->reply.request_time,
1201 cd->m.data, cd->m.data_size, s->written);
1202
1203 /*
1204 * Write of 0 bytes means an OS bug, and we just discard this packet.
1205 */
1206 if (rcode == 0) {
1207 RATE_LIMIT_GLOBAL(ERROR, "Discarding packet due to write returning zero for socket %s",
1208 s->listen->name);
1209 goto discard;
1210 }
1211
1212 /*
1213 * Or we have a write error.
1214 */
1215 if (rcode < 0) {
1216 /*
1217 * Stop processing the heap, and set the
1218 * pending message to the current one.
1219 */
1220 if (errno == EWOULDBLOCK) {
1221 save_pending:
1222 fr_assert(!s->pending);
1223
1224 if (cd->m.status != FR_MESSAGE_LOCALIZED) {
1225 fr_message_t *lm;
1226
1227 lm = fr_message_localize(s, &cd->m, sizeof(*cd));
1228 if (!lm) {
1229 ERROR("Failed saving pending packet");
1230 goto dead;
1231 }
1232
1233 cd = (fr_channel_data_t *) lm;
1234 }
1235
1236 if (!s->blocked) {
1238 PERROR("Failed adding write callback to event loop");
1239 goto dead;
1240 }
1241
1242 s->blocked = true;
1243 }
1244
1245 s->pending = cd;
1246 return;
1247 }
1248
1249 PERROR("Failed writing to socket %s", s->listen->name);
1250
1251 /*
1252 * As a special hack, check for something
1253 * that will never be returned from a
1254 * real write() routine. Which then
1255 * signals to us that we have to close
1256 * the socket, but NOT complain about it.
1257 */
1258 if ((errno == ECONNREFUSED) || (errno == ECONNRESET)) goto dead;
1259
1260 if (li->app_io->error) li->app_io->error(li);
1261
1262 dead:
1263 fr_message_done(&cd->m);
1265 return;
1266 }
1267
1268 /*
1269 * If we've done a partial write, localize the message and continue.
1270 */
1271 if ((size_t) rcode < cd->m.data_size) {
1272 s->written = rcode;
1273 goto save_pending;
1274 }
1275
1276 discard:
1277 s->written = 0;
1278
1279 /*
1280 * Reset for the next message.
1281 */
1282 fr_message_done(&cd->m);
1283 nr->stats.out++;
1284 s->stats.out++;
1285
1286 /*
1287 * Grab the net entry.
1288 */
1289 cd = fr_heap_pop(&s->waiting);
1290 }
1291
1292 /*
1293 * We've successfully written all of the packets. Remove
1294 * the write callback.
1295 */
1297 PERROR("Failed removing write callback from event loop");
1299 }
1300
1301 s->blocked = false;
1302}
1303
1305{
1306 fr_network_t *nr = s->nr;
1308
1309 fr_assert(s->outstanding == 0);
1310
1311 fr_rb_delete(nr->sockets, s);
1313
1314 if (!s->dead) fr_event_fd_delete(nr->el, s->listen->fd, s->filter);
1315
1316 if (s->listen->app_io->close) {
1317 s->listen->app_io->close(s->listen);
1318 } else {
1319 close(s->listen->fd);
1320 }
1321
1322 if (s->pending) {
1324 s->pending = NULL;
1325 }
1326
1327 /*
1328 * Clean up any queued entries.
1329 */
1330 while ((cd = fr_heap_pop(&s->waiting)) != NULL) {
1331 fr_message_done(&cd->m);
1332 }
1333
1334 /* s->waiting is already talloc parented from s */
1335 talloc_free(s->listen);
1336
1337 return 0;
1338}
1339
1340
1341/** Handle a network control message callback for a new listener
1342 *
1343 * @param[in] ctx the network
1344 * @param[in] data the message
1345 * @param[in] data_size size of the data
1346 * @param[in] now the current time
1347 */
1348static void fr_network_listen_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
1349{
1350 fr_network_t *nr = talloc_get_type_abort(ctx, fr_network_t);
1351 fr_listen_t *li;
1352
1353 fr_assert(data_size == sizeof(li));
1354
1355 if (data_size != sizeof(li)) return;
1356
1357 li = talloc_get_type_abort(*((void * const *)data), fr_listen_t);
1358
1359 (void) fr_network_listen_add_self(nr, li);
1360}
1361
1362static void fr_network_limit_ringbuffer(fr_network_socket_t *s, int *num_messages_p, size_t *size_p)
1363{
1364 int num_messages;
1365 size_t size;
1366
1367 num_messages = s->listen->num_messages;
1368 if (num_messages < 8) num_messages = 8;
1369 if (num_messages > (1 << 20)) num_messages = (1 << 20);
1370
1371 size = s->listen->default_message_size * num_messages;
1372 if (size < (1 << 17)) size = (1 << 17);
1373 if (size > (100 * 1024 * 1024)) size = (100 * 1024 * 1024);
1374
1375 *num_messages_p = num_messages;
1376 *size_p = size;
1377}
1378
1380{
1382 fr_app_io_t const *app_io;
1383 size_t size;
1384 int num_messages;
1385
1386 fr_assert(li->app_io != NULL);
1387
1388 /*
1389 * Non-socket listeners just get told about the event
1390 * list, and nothing else.
1391 */
1392 if (li->non_socket_listener) {
1393 fr_assert(li->app_io->event_list_set != NULL);
1394 fr_assert(!li->app_io->read);
1395 fr_assert(!li->app_io->write);
1396
1397 li->app_io->event_list_set(li, nr->el, nr);
1398
1399 /*
1400 * We use fr_log() here to avoid the "Network - " prefix.
1401 */
1402 fr_log(nr->log, L_DBG, __FILE__, __LINE__, "Listener %s bound to virtual server %s",
1403 li->name, cf_section_name2(li->server_cs));
1404
1405 return 0;
1406 }
1407
1408 s = talloc_zero(nr, fr_network_socket_t);
1409 fr_assert(s != NULL);
1410 talloc_steal(s, li);
1411
1412 s->nr = nr;
1413 s->listen = li;
1414 s->number = nr->num_sockets++;
1415
1416 MEM(s->waiting = fr_heap_alloc(s, waiting_cmp, fr_channel_data_t, channel.heap_id, 0));
1417
1418 talloc_set_destructor(s, _network_socket_free);
1419
1420 /*
1421 * Put reasonable limits on the ring buffer size. Then
1422 * round it up to the nearest power of 2, which is
1423 * required by the ring buffer code.
1424 */
1425 fr_network_limit_ringbuffer(s, &num_messages, &size);
1426
1427 /*
1428 * Allocate the ring buffer for messages and packets.
1429 */
1430 s->ms = fr_message_set_create(s, num_messages,
1431 sizeof(fr_channel_data_t),
1432 size, false);
1433 if (!s->ms) {
1434 PERROR("Failed creating message buffers for network IO");
1435 talloc_free(s);
1436 return -1;
1437 }
1438
1439 app_io = s->listen->app_io;
1441
1442 if (fr_event_fd_insert(nr, NULL, nr->el, s->listen->fd,
1446 s) < 0) {
1447 PERROR("Failed adding new socket to network event loop");
1448 talloc_free(s);
1449 return -1;
1450 }
1451
1452 /*
1453 * Start of with write updates being paused. We don't
1454 * care about being able to write if there's nothing to
1455 * write.
1456 */
1458
1459 /*
1460 * Add the listener before calling the app_io, so that
1461 * the app_io can find the listener which we're adding
1462 * here.
1463 */
1464 (void) fr_rb_insert(nr->sockets, s);
1465 (void) fr_rb_insert(nr->sockets_by_num, s);
1466
1467 if (app_io->event_list_set) app_io->event_list_set(s->listen, nr->el, nr);
1468
1469 /*
1470 * We use fr_log() here to avoid the "Network - " prefix.
1471 */
1472 fr_log(nr->log, L_DBG, __FILE__, __LINE__, "Listening on %s bound to virtual server %s",
1474
1475 DEBUG3("Using new socket %s with FD %d", s->listen->name, s->listen->fd);
1476
1477 return 0;
1478}
1479
1480/** Handle a network control message callback for a new "watch directory"
1481 *
1482 * @param[in] ctx the network
1483 * @param[in] data the message
1484 * @param[in] data_size size of the data
1485 * @param[in] now the current time
1486 */
1487static void fr_network_directory_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
1488{
1489 int num_messages;
1490 size_t size;
1491 fr_network_t *nr = talloc_get_type_abort(ctx, fr_network_t);
1492 fr_listen_t *li = talloc_get_type_abort(*((void * const *)data), fr_listen_t);
1494 fr_app_io_t const *app_io;
1496
1497 if (!fr_cond_assert(data_size == sizeof(li))) return;
1498
1499 memcpy(&li, data, sizeof(li));
1500
1501 s = talloc_zero(nr, fr_network_socket_t);
1502 fr_assert(s != NULL);
1503 talloc_steal(s, li);
1504
1505 s->nr = nr;
1506 s->listen = li;
1507 s->number = nr->num_sockets++;
1508
1509 MEM(s->waiting = fr_heap_alloc(s, waiting_cmp, fr_channel_data_t, channel.heap_id, 0));
1510
1511 talloc_set_destructor(s, _network_socket_free);
1512
1513 /*
1514 * Allocate the ring buffer for messages and packets.
1515 */
1516 fr_network_limit_ringbuffer(s, &num_messages, &size);
1517
1518 s->ms = fr_message_set_create(s, num_messages,
1519 sizeof(fr_channel_data_t),
1520 size, false);
1521 if (!s->ms) {
1522 PERROR("Failed creating message buffers for directory IO");
1523 talloc_free(s);
1524 return;
1525 }
1526
1527 app_io = s->listen->app_io;
1528
1529 if (app_io->event_list_set) app_io->event_list_set(s->listen, nr->el, nr);
1530
1532
1533 if (fr_event_filter_insert(nr, NULL, nr->el, s->listen->fd, s->filter,
1534 &funcs,
1535 app_io->error ? fr_network_error : NULL,
1536 s) < 0) {
1537 PERROR("Failed adding directory monitor event loop");
1538 talloc_free(s);
1539 return;
1540 }
1541
1542 (void) fr_rb_insert(nr->sockets, s);
1543 (void) fr_rb_insert(nr->sockets_by_num, s);
1544
1545 DEBUG3("Using new socket with FD %d", s->listen->fd);
1546}
1547
1548/** Handle a network control message callback for a new worker
1549 *
1550 * @param[in] ctx the network
1551 * @param[in] data the message
1552 * @param[in] data_size size of the data
1553 * @param[in] now the current time
1554 */
1555static void fr_network_worker_started_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
1556{
1557 int i;
1558 fr_network_t *nr = ctx;
1559 fr_worker_t *worker;
1561
1562 fr_assert(data_size == sizeof(worker));
1563
1564 if (nr->num_workers >= nr->max_workers) {
1565 ERROR("Too many workers");
1566 return;
1567 }
1568
1569 memcpy(&worker, data, data_size);
1570 (void) talloc_get_type_abort(worker, fr_worker_t);
1571
1572 MEM(w = talloc_zero(nr, fr_network_worker_t));
1573
1574 w->worker = worker;
1575 w->channel = fr_worker_channel_create(worker, w, nr->control);
1577 fr_fatal_assert_msg(w->channel, "Failed creating new channel");
1578
1581
1582 /*
1583 * Insert the worker into the array of workers.
1584 */
1585 for (i = 0; i < nr->max_workers; i++) {
1586 if (nr->workers[i]) continue;
1587
1588 nr->workers[i] = w;
1589 nr->num_workers++;
1590 return;
1591 }
1592}
1593
1594/** Handle a network control message callback for a packet sent to a socket
1595 *
1596 * @param[in] ctx the network
1597 * @param[in] data the message
1598 * @param[in] data_size size of the data
1599 * @param[in] now the current time
1600 */
1601static void fr_network_inject_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
1602{
1603 fr_network_t *nr = ctx;
1604 fr_network_inject_t my_inject;
1606
1607 fr_assert(data_size == sizeof(my_inject));
1608
1609 memcpy(&my_inject, data, data_size);
1610 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = my_inject.listen });
1611 if (!s) {
1612 talloc_free(my_inject.packet); /* MUST be it's own TALLOC_CTX */
1613 return;
1614 }
1615
1616 /*
1617 * Inject the packet, and then read it back from the
1618 * network.
1619 */
1620 if (s->listen->app_io->inject(s->listen, my_inject.packet, my_inject.packet_len, my_inject.recv_time) == 0) {
1621 fr_network_read(nr->el, s->listen->fd, 0, s);
1622 }
1623
1624 talloc_free(my_inject.packet);
1625}
1626
1627/** Run the event loop 'pre' callback
1628 *
1629 * This function MUST DO NO WORK. All it does is check if there's
1630 * work, and tell the event code to return to the main loop if
1631 * there's work to do.
1632 *
1633 * @param[in] now the current time.
1634 * @param[in] wake the time when the event loop will wake up.
1635 * @param[in] uctx the network
1636 */
1638{
1639 fr_network_t *nr = talloc_get_type_abort(uctx, fr_network_t);
1640
1641 if (fr_heap_num_elements(nr->replies) > 0) return 1;
1642
1643 return 0;
1644}
1645
1646/** Handle replies after all FD and timer events have been serviced
1647 *
1648 * @param el the event loop
1649 * @param now the current time (mostly)
1650 * @param uctx the fr_network_t
1651 */
1653{
1655 fr_network_t *nr = talloc_get_type_abort(uctx, fr_network_t);
1656
1657 /*
1658 * Pull the replies off of our global heap, and try to
1659 * push them to the individual sockets.
1660 */
1661 while ((cd = fr_heap_pop(&nr->replies)) != NULL) {
1662 fr_listen_t *li;
1664
1665 li = cd->listen;
1666
1667 /*
1668 * @todo - cache this somewhere so we don't need
1669 * to do an rbtree lookup for every packet.
1670 */
1671 s = fr_rb_find(nr->sockets, &(fr_network_socket_t){ .listen = li });
1672
1673 /*
1674 * This shouldn't happen, but be safe...
1675 */
1676 if (!s) {
1677 fr_message_done(&cd->m);
1678 continue;
1679 }
1680
1681 if (cd->m.status != FR_MESSAGE_LOCALIZED) {
1682 fr_assert(s->outstanding > 0);
1683 s->outstanding--;
1684 }
1685
1686 /*
1687 * Just mark the message done, and skip it.
1688 */
1689 if (s->dead) {
1690 fr_message_done(&cd->m);
1691
1692 /*
1693 * No more packets, it's safe to delete
1694 * the socket.
1695 */
1696 if (!s->outstanding) talloc_free(s);
1697
1698 continue;
1699 }
1700
1701 /*
1702 * No data to write to the socket, so we skip the message.
1703 */
1704 if (!cd->m.data_size) {
1705 fr_message_done(&cd->m);
1706 continue;
1707 }
1708
1709 (void) fr_heap_insert(&s->waiting, cd);
1710
1711 /*
1712 * No pending message, write it. If there is a pending write, the message will be left
1713 * in the waiting queue.
1714 */
1715 if (!s->pending) {
1716 fr_assert(!s->blocked);
1717 fr_network_write(nr->el, s->listen->fd, 0, s);
1718 }
1719 }
1720}
1721
1722/** Stop a network thread in an orderly way
1723 *
1724 * @param[in] nr the network to stop
1725 */
1727{
1729
1730 (void) talloc_get_type_abort(nr, fr_network_t);
1731
1732 /*
1733 * Close the network sockets
1734 */
1735 {
1736 fr_network_socket_t **sockets;
1737 size_t len;
1738 size_t i;
1739
1740 if (fr_rb_flatten_inorder(nr, (void ***)&sockets, nr->sockets) < 0) return -1;
1741 len = talloc_array_length(sockets);
1742
1743 for (i = 0; i < len; i++) {
1744 /*
1745 * Force to zero so we don't trigger asserts
1746 * if packets are being processed and the
1747 * server exits.
1748 */
1749 sockets[i]->outstanding = 0;
1750 talloc_free(sockets[i]);
1751 }
1752
1753 talloc_free(sockets);
1754 }
1755
1756
1757 /*
1758 * Clean up all outstanding replies.
1759 *
1760 * We can't do this after signalling the
1761 * workers to close, because they free
1762 * their message sets, and we end up
1763 * getting random use-after-free errors
1764 * as there's a race between the network
1765 * popping replies, and the workers
1766 * freeing their message sets.
1767 *
1768 * This isn't perfect, and we might still
1769 * lose some replies, but it's good enough
1770 * for now.
1771 *
1772 * @todo - call transport "done" for the reply, so that
1773 * it knows the replies are done, too.
1774 */
1775 while ((cd = fr_heap_pop(&nr->replies)) != NULL) {
1776 fr_message_done(&cd->m);
1777 }
1778
1779 /*
1780 * Signal the workers that we're closing
1781 *
1782 * nr->num_workers is decremented every
1783 * time a worker closes a socket.
1784 *
1785 * When nr->num_workers == 0, the event
1786 * loop (fr_network()) will exit.
1787 */
1788 {
1789 int i;
1790
1791 for (i = 0; i < nr->num_workers; i++) {
1792 fr_network_worker_t *worker = nr->workers[i];
1793
1795 }
1796 }
1797
1800 nr->exiting = true;
1802
1803 return 0;
1804}
1805
1806/** Read handler for signal pipe
1807 *
1808 */
1809static void _signal_pipe_read(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
1810{
1811 fr_network_t *nr = talloc_get_type_abort(uctx, fr_network_t);
1812 uint8_t buff;
1813
1814 if (read(fd, &buff, sizeof(buff)) < 0) {
1815 ERROR("Failed reading signal - %s", fr_syserror(errno));
1816 return;
1817 }
1818
1819 fr_assert(buff == 1);
1820
1821 /*
1822 * fr_network_stop() will signal the workers
1823 * to exit (by closing their channels).
1824 *
1825 * When we get the ack, we decrement our
1826 * nr->num_workers counter.
1827 *
1828 * When the counter reaches 0, the event loop
1829 * exits.
1830 */
1831 DEBUG2("Signalled to exit");
1832
1833 if (unlikely(fr_network_destroy(nr) < 0)) {
1834 PERROR("Failed stopping network");
1835 }
1836}
1837
1838/** The main network worker function.
1839 *
1840 * @param[in] nr the network data structure to run.
1841 */
1843{
1844 /*
1845 * Run until we're told to exit AND the number of
1846 * workers has dropped to zero.
1847 *
1848 * This is important as if we exit too early we
1849 * free the channels out from underneath the
1850 * workers and they read uninitialised memory.
1851 *
1852 * Whenever a worker ACKs our close notification
1853 * nr->num_workers is decremented, so when
1854 * nr->num_workers == 0, all workers have ACKd
1855 * our close and are no longer using the channel.
1856 */
1857 while (likely(!(nr->exiting && (nr->num_workers == 0)))) {
1858 bool wait_for_event;
1859 int num_events;
1860
1861 /*
1862 * There are runnable requests. We still service
1863 * the event loop, but we don't wait for events.
1864 */
1865 wait_for_event = (fr_heap_num_elements(nr->replies) == 0);
1866
1867 /*
1868 * Check the event list. If there's an error
1869 * (e.g. exit), we stop looping and clean up.
1870 */
1871 DEBUG4("Gathering events - %s", wait_for_event ? "will wait" : "Will not wait");
1872 num_events = fr_event_corral(nr->el, fr_time(), wait_for_event);
1873 DEBUG4("%u event(s) pending%s",
1874 num_events == -1 ? 0 : num_events, num_events == -1 ? " - event loop exiting" : "");
1875 if (num_events < 0) break;
1876
1877 /*
1878 * Service outstanding events.
1879 */
1880 if (num_events > 0) {
1881 DEBUG4("Servicing event(s)");
1882 fr_event_service(nr->el);
1883 }
1884 }
1885 return;
1886}
1887
1888/** Signal a network thread to exit
1889 *
1890 * @note Request to exit will be processed asynchronously.
1891 *
1892 * @param[in] nr the network data structure to manage
1893 * @return
1894 * - 0 on success.
1895 * - -1 on failure.
1896 */
1898{
1899 if (write(nr->signal_pipe[1], &(uint8_t){ 0x01 }, 1) < 0) {
1900 fr_strerror_printf("Failed signalling network thread to exit - %s", fr_syserror(errno));
1901 return -1;
1902 }
1903
1904 return 0;
1905}
1906
1907/** Free any resources associated with a network thread
1908 *
1909 */
1911{
1912 if (nr->signal_pipe[0] >= 0) close(nr->signal_pipe[0]);
1913 if (nr->signal_pipe[1] >= 0) close(nr->signal_pipe[1]);
1914
1915 return 0;
1916}
1917
1918/** Create a network
1919 *
1920 * @param[in] ctx The talloc ctx
1921 * @param[in] el The event list
1922 * @param[in] name Networker identifier.
1923 * @param[in] logger The destination for all logging messages
1924 * @param[in] lvl Log level
1925 * @param[in] config configuration structure.
1926 * @return
1927 * - NULL on error
1928 * - fr_network_t on success
1929 */
1930fr_network_t *fr_network_create(TALLOC_CTX *ctx, fr_event_list_t *el, char const *name,
1931 fr_log_t const *logger, fr_log_lvl_t lvl,
1933{
1934 fr_network_t *nr;
1935
1936 nr = talloc_zero(ctx, fr_network_t);
1937 if (!nr) {
1938 fr_strerror_const("Failed allocating memory");
1939 return NULL;
1940 }
1941 talloc_set_destructor(nr, _fr_network_free);
1942
1943 nr->name = talloc_strdup(nr, name);
1944
1945 nr->thread_id = pthread_self();
1946 nr->el = el;
1947 nr->log = logger;
1948 nr->lvl = lvl;
1949
1951 nr->num_workers = 0;
1952 nr->signal_pipe[0] = -1;
1953 nr->signal_pipe[1] = -1;
1954 if (config) nr->config = *config;
1955
1956 nr->aq_control = fr_atomic_queue_alloc(nr, 1024);
1957 if (!nr->aq_control) {
1958 talloc_free(nr);
1959 return NULL;
1960 }
1961
1962 nr->control = fr_control_create(nr, el, nr->aq_control);
1963 if (!nr->control) {
1964 fr_strerror_const_push("Failed creating control queue");
1965 fail:
1966 talloc_free(nr);
1967 return NULL;
1968 }
1969
1970 /*
1971 * @todo - rely on thread-local variables. And then the
1972 * various users of this can check if (rb == nr->rb), and
1973 * if so, skip the whole control plane / kevent /
1974 * whatever roundabout thing.
1975 */
1977 if (!nr->rb) {
1978 fr_strerror_const_push("Failed creating ring buffer");
1979 fail2:
1980 talloc_free(nr->control);
1981 goto fail;
1982 }
1983
1985 fr_strerror_const_push("Failed adding channel callback");
1986 goto fail2;
1987 }
1988
1990 fr_strerror_const_push("Failed adding socket callback");
1991 goto fail2;
1992 }
1993
1995 fr_strerror_const_push("Failed adding socket callback");
1996 goto fail2;
1997 }
1998
2000 fr_strerror_const_push("Failed adding worker callback");
2001 goto fail2;
2002 }
2003
2005 fr_strerror_const_push("Failed adding packet injection callback");
2006 goto fail2;
2007 }
2008
2009 /*
2010 * Create the various heaps.
2011 */
2013 if (!nr->sockets) {
2014 fr_strerror_const_push("Failed creating listen tree for sockets");
2015 goto fail2;
2016 }
2017
2019 if (!nr->sockets_by_num) {
2020 fr_strerror_const_push("Failed creating number tree for sockets");
2021 goto fail2;
2022 }
2023
2024 nr->replies = fr_heap_alloc(nr, reply_cmp, fr_channel_data_t, channel.heap_id, 0);
2025 if (!nr->replies) {
2026 fr_strerror_const_push("Failed creating heap for replies");
2027 goto fail2;
2028 }
2029
2030 if (fr_event_pre_insert(nr->el, fr_network_pre_event, nr) < 0) {
2031 fr_strerror_const("Failed adding pre-check to event list");
2032 goto fail2;
2033 }
2034
2035 if (fr_event_post_insert(nr->el, fr_network_post_event, nr) < 0) {
2036 fr_strerror_const("Failed inserting post-processing event");
2037 goto fail2;
2038 }
2039
2040 if (pipe(nr->signal_pipe) < 0) {
2041 fr_strerror_printf("Failed initialising signal pipe - %s", fr_syserror(errno));
2042 goto fail2;
2043 }
2044 if (fr_nonblock(nr->signal_pipe[0]) < 0) goto fail2;
2045 if (fr_nonblock(nr->signal_pipe[1]) < 0) goto fail2;
2046
2047 if (fr_event_fd_insert(nr, NULL, nr->el, nr->signal_pipe[0], _signal_pipe_read, NULL, NULL, nr) < 0) {
2048 fr_strerror_const("Failed inserting event for signal pipe");
2049 goto fail2;
2050 }
2051
2052 return nr;
2053}
2054
2055int fr_network_stats(fr_network_t const *nr, int num, uint64_t *stats)
2056{
2057 if (num < 0) return -1;
2058 if (num == 0) return 0;
2059
2060 stats[0] = nr->stats.in;
2061 if (num >= 2) stats[1] = nr->stats.out;
2062 if (num >= 3) stats[2] = nr->stats.dup;
2063 if (num >= 4) stats[3] = nr->stats.dropped;
2064 if (num >= 5) stats[4] = nr->num_workers;
2065
2066 if (num <= 5) return num;
2067
2068 return 5;
2069}
2070
2071void fr_network_stats_log(fr_network_t const *nr, fr_log_t const *log)
2072{
2073 int i;
2074
2075 /*
2076 * Dump all of the channel statistics.
2077 */
2078 for (i = 0; i < nr->max_workers; i++) {
2079 if (!nr->workers[i]) continue;
2080
2081 fr_channel_stats_log(nr->workers[i]->channel, log, __FILE__, __LINE__);
2082 }
2083}
2084
2085static int cmd_stats_self(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
2086{
2087 fr_network_t const *nr = ctx;
2088
2089 fprintf(fp, "count.in\t%" PRIu64 "\n", nr->stats.in);
2090 fprintf(fp, "count.out\t%" PRIu64 "\n", nr->stats.out);
2091 fprintf(fp, "count.dup\t%" PRIu64 "\n", nr->stats.dup);
2092 fprintf(fp, "count.dropped\t%" PRIu64 "\n", nr->stats.dropped);
2093 fprintf(fp, "count.sockets\t%u\n", fr_rb_num_elements(nr->sockets));
2094
2095 return 0;
2096}
2097
2098static int cmd_socket_list(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
2099{
2100 fr_network_t const *nr = ctx;
2103
2104 // @todo - note that this isn't thread-safe!
2105
2106 for (s = fr_rb_iter_init_inorder(nr->sockets, &iter);
2107 s != NULL;
2108 s = fr_rb_iter_next_inorder(nr->sockets, &iter)) {
2109 if (!s->listen->app_io->get_name) {
2110 fprintf(fp, "%s\n", s->listen->app_io->common.name);
2111 } else {
2112 fprintf(fp, "%d\t%s\n", s->number, s->listen->app_io->get_name(s->listen));
2113 }
2114 }
2115 return 0;
2116}
2117
2118static int cmd_stats_socket(FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
2119{
2120 fr_network_t const *nr = ctx;
2122
2123 s = fr_rb_find(nr->sockets_by_num, &(fr_network_socket_t){ .number = info->box[0]->vb_uint32 });
2124 if (!s) {
2125 fprintf(fp_err, "No such socket number '%s'.\n", info->argv[0]);
2126 return -1;
2127 }
2128
2129 fprintf(fp, "count.in\t%" PRIu64 "\n", s->stats.in);
2130 fprintf(fp, "count.out\t%" PRIu64 "\n", s->stats.out);
2131 fprintf(fp, "count.dup\t%" PRIu64 "\n", s->stats.dup);
2132 fprintf(fp, "count.dropped\t%" PRIu64 "\n", s->stats.dropped);
2133
2134 return 0;
2135}
2136
2137
2139 {
2140 .parent = "stats",
2141 .name = "network",
2142 .help = "Statistics for network threads.",
2143 .read_only = true
2144 },
2145
2146 {
2147 .parent = "stats network",
2148 .add_name = true,
2149 .name = "self",
2150 .func = cmd_stats_self,
2151 .help = "Show statistics for a specific network thread.",
2152 .read_only = true
2153 },
2154
2155 {
2156 .parent = "stats network",
2157 .add_name = true,
2158 .name = "socket",
2159 .syntax = "INTEGER",
2160 .func = cmd_stats_socket,
2161 .help = "Show statistics for a specific socket",
2162 .read_only = true
2163 },
2164
2165 {
2166 .parent = "show",
2167 .name = "network",
2168 .help = "Show information about network threads.",
2169 .read_only = true
2170 },
2171
2172 {
2173 .parent = "show network",
2174 .add_name = true,
2175 .name = "socket",
2176 .syntax = "list",
2177 .func = cmd_socket_list,
2178 .help = "List the sockets associated with this network thread.",
2179 .read_only = true
2180 },
2181
2183};
static int const char char buffer[256]
Definition acutest.h:578
fr_io_close_t close
Close the transport.
Definition app_io.h:60
fr_io_data_read_t read
Read from a socket to a data buffer.
Definition app_io.h:47
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
fr_io_signal_t error
There was an error on the socket.
Definition app_io.h:59
fr_app_event_list_set_t event_list_set
Called by the network thread to pass an event list for use by the app_io_t.
Definition app_io.h:36
fr_io_data_inject_t inject
Inject a packet into a socket.
Definition app_io.h:50
fr_io_data_vnode_t vnode
Handle notifications that the VNODE has changed.
Definition app_io.h:52
fr_io_data_write_t write
Write from a data buffer to a socket.
Definition app_io.h:48
fr_io_name_t get_name
get the socket name
Definition app_io.h:70
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
fr_app_priority_get_t priority
Assign a priority to the packet.
Definition application.h:90
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:221
fr_atomic_queue_t * fr_atomic_queue_alloc(TALLOC_CTX *ctx, size_t size)
Create fixed-size atomic queue.
Structure to hold the atomic queue.
#define RCSID(id)
Definition build.h:487
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
void * fr_channel_requestor_uctx_get(fr_channel_t *ch)
Get network-specific data from a channel.
Definition channel.c:920
fr_table_num_sorted_t const channel_signals[]
Definition channel.c:153
bool fr_channel_recv_reply(fr_channel_t *ch)
Receive a reply message from the channel.
Definition channel.c:408
int fr_channel_signal_responder_close(fr_channel_t *ch)
Signal a responder that the channel is closing.
Definition channel.c:824
int fr_channel_send_request(fr_channel_t *ch, fr_channel_data_t *cd)
Send a request message into the channel.
Definition channel.c:306
int fr_channel_set_recv_reply(fr_channel_t *ch, void *uctx, fr_channel_recv_callback_t recv_reply)
Definition channel.c:928
fr_channel_event_t fr_channel_service_message(fr_time_t when, fr_channel_t **p_channel, void const *data, size_t data_size)
Service a control-plane message.
Definition channel.c:685
void fr_channel_requestor_uctx_add(fr_channel_t *ch, void *uctx)
Add network-specific data to a channel.
Definition channel.c:908
void fr_channel_stats_log(fr_channel_t const *ch, fr_log_t const *log, char const *file, int line)
Definition channel.c:961
A full channel, which consists of two ends.
Definition channel.c:144
fr_message_t m
the message header
Definition channel.h:105
fr_channel_event_t
Definition channel.h:67
@ FR_CHANNEL_NOOP
Definition channel.h:74
@ FR_CHANNEL_EMPTY
Definition channel.h:75
@ FR_CHANNEL_CLOSE
Definition channel.h:72
@ FR_CHANNEL_ERROR
Definition channel.h:68
@ FR_CHANNEL_DATA_READY_REQUESTOR
Definition channel.h:70
@ FR_CHANNEL_OPEN
Definition channel.h:71
@ FR_CHANNEL_DATA_READY_RESPONDER
Definition channel.h:69
void * packet_ctx
Packet specific context for holding client information, and other proto_* specific information that n...
Definition channel.h:142
fr_listen_t * listen
for tracking packet transport, etc.
Definition channel.h:146
#define PRIORITY_NORMAL
Definition channel.h:151
#define PRIORITY_NOW
Definition channel.h:149
uint32_t priority
Priority of this packet.
Definition channel.h:140
Channel information which is added to a message.
Definition channel.h:104
char const * parent
e.g. "show module"
Definition command.h:52
#define CMD_TABLE_END
Definition command.h:62
char const ** argv
text version of commands
Definition command.h:42
#define FR_CONTROL_ID_INJECT
Definition control.h:60
#define FR_CONTROL_ID_DIRECTORY
Definition control.h:59
#define FR_CONTROL_ID_CHANNEL
Definition control.h:56
#define FR_CONTROL_ID_LISTEN
Definition control.h:57
#define FR_CONTROL_ID_WORKER
Definition control.h:58
#define FR_CONTROL_MAX_SIZE
Definition control.h:51
#define FR_CONTROL_MAX_MESSAGES
Definition control.h:50
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:148
#define fr_fatal_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:176
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
static int sockfd
Definition dhcpclient.c:56
fr_dict_t const * fr_dict_proto_dict(fr_dict_t const *dict)
Definition dict_util.c:5277
#define fr_event_fd_insert(...)
Definition event.h:248
fr_event_filter_t
The type of filter to install for an FD.
Definition event.h:83
@ FR_EVENT_FILTER_VNODE
Filter for vnode subfilters.
Definition event.h:85
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:84
#define fr_event_filter_update(...)
Definition event.h:240
#define fr_event_filter_insert(...)
Definition event.h:235
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
Definition event.h:132
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
Definition event.h:116
fr_event_fd_cb_t extend
Additional files were added to a directory.
Definition event.h:199
Callbacks for the FR_EVENT_FILTER_IO filter.
Definition event.h:189
Structure describing a modification to a filter's state.
Definition event.h:97
Callbacks for the FR_EVENT_FILTER_VNODE filter.
Definition event.h:196
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:146
void * fr_heap_pop(fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:325
unsigned int fr_heap_index_t
Definition heap.h:80
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
Definition heap.h:100
static unsigned int fr_heap_num_elements(fr_heap_t *h)
Return the number of elements in the heap.
Definition heap.h:179
#define FR_HEAP_INDEX_INVALID
Definition heap.h:83
The main heap structure.
Definition heap.h:66
talloc_free(hp)
uint64_t out
Definition base.h:43
uint64_t dup
Definition base.h:44
uint64_t dropped
Definition base.h:45
uint64_t in
Definition base.h:42
int fr_control_callback_add(fr_control_t *c, uint32_t id, void *ctx, fr_control_callback_t callback)
Register a callback for an ID.
Definition control.c:428
int fr_control_message_send(fr_control_t *c, fr_ring_buffer_t *rb, uint32_t id, void *data, size_t data_size)
Send a control-plane message.
Definition control.c:340
fr_control_t * fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_queue_t *aq)
Create a control-plane signaling path.
Definition control.c:149
The control structure.
Definition control.c:79
bool read_hexdump
Do we debug hexdump packets as they're read.
Definition listen.h:53
size_t num_messages
for the message ring buffer
Definition listen.h:57
bool non_socket_listener
special internal listener that does not use sockets.
Definition listen.h:47
char const * name
printable name for this socket - set by open
Definition listen.h:29
void const * app_instance
Definition listen.h:39
size_t default_message_size
copied from app_io, but may be changed
Definition listen.h:56
bool write_hexdump
Do we debug hexdump packets as they're written.
Definition listen.h:54
fr_app_t const * app
Definition listen.h:38
CONF_SECTION * server_cs
CONF_SECTION of the server.
Definition listen.h:42
bool no_write_callback
sometimes we don't need to do writes
Definition listen.h:46
int fd
file descriptor for this socket - set by open
Definition listen.h:28
fr_dict_t const * dict
dictionary for this listener
Definition listen.h:30
bool needs_full_setup
Set to true to avoid the short cut when adding the listener.
Definition listen.h:48
fr_app_io_t const * app_io
I/O path functions.
Definition listen.h:32
fr_ring_buffer_t * rb
ring buffer for my control-plane messages
Definition network.c:126
fr_cmd_table_t cmd_network_table[]
Definition network.c:2138
size_t written
however much we did in a partial write
Definition network.c:92
int fr_network_listen_send_packet(fr_network_t *nr, fr_listen_t *parent, fr_listen_t *li, const uint8_t *buffer, size_t buflen, fr_time_t recv_time, void *packet_ctx)
Send a packet to the worker.
Definition network.c:803
fr_atomic_queue_t * aq_control
atomic queue for control messages sent to me
Definition network.c:122
static int cmd_stats_socket(FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
Definition network.c:2118
int fr_network_listen_add(fr_network_t *nr, fr_listen_t *li)
Add a fr_listen_t to a network.
Definition network.c:236
bool suspended
whether or not we're suspended.
Definition network.c:117
int fr_network_worker_add(fr_network_t *nr, fr_worker_t *worker)
Add a worker to a network in a different thread.
Definition network.c:305
int fr_network_destroy(fr_network_t *nr)
Stop a network thread in an orderly way.
Definition network.c:1726
fr_network_t * nr
O(N) issues in talloc.
Definition network.c:77
fr_io_stats_t stats
Definition network.c:70
fr_listen_t * listen
Definition network.c:52
static void fr_network_listen_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
Handle a network control message callback for a new listener.
Definition network.c:1348
uint8_t * packet
Definition network.c:53
static int cmd_stats_self(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
Definition network.c:2085
fr_log_t const * log
log destination
Definition network.c:119
int fr_network_directory_add(fr_network_t *nr, fr_listen_t *li)
Add a "watch directory" call to a network.
Definition network.c:290
static int _fr_network_free(fr_network_t *nr)
Free any resources associated with a network thread.
Definition network.c:1910
static void fr_network_inject_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
Handle a network control message callback for a packet sent to a socket.
Definition network.c:1601
fr_heap_index_t heap_id
for the sockets_by_num heap
Definition network.c:79
#define RTT(_old, _new)
Definition network.c:512
void fr_network(fr_network_t *nr)
The main network worker function.
Definition network.c:1842
fr_message_set_t * ms
message buffers for this socket.
Definition network.c:89
int fr_network_listen_delete(fr_network_t *nr, fr_listen_t *li)
Delete a socket from a network.
Definition network.c:271
int num_blocked
number of blocked workers
Definition network.c:138
static void fr_network_worker_started_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
ssize_t fr_network_listen_outstanding(fr_network_t *nr, fr_listen_t *li)
Get the number of outstanding packets.
Definition network.c:845
char const * name
Network ID for logging.
Definition network.c:113
void fr_network_worker_add_self(fr_network_t *nr, fr_worker_t *worker)
Add a worker to a network in the same thread.
Definition network.c:325
unsigned int outstanding
number of outstanding packets sent to the worker
Definition network.c:86
static _Thread_local fr_ring_buffer_t * fr_network_rb
Definition network.c:49
int number
unique ID
Definition network.c:78
static fr_event_update_t const resume_write[]
Definition network.c:476
fr_time_delta_t predicted
predicted processing time for one packet
Definition network.c:64
static int fr_network_pre_event(fr_time_t now, fr_time_delta_t wake, void *uctx)
static fr_event_update_t const pause_read[]
Definition network.c:461
fr_worker_t * worker
worker pointer
Definition network.c:69
int fr_network_sendto_worker(fr_network_t *nr, fr_listen_t *li, void *packet_ctx, uint8_t const *data, size_t data_len, fr_time_t recv_time)
Definition network.c:1077
int fr_network_exit(fr_network_t *nr)
Signal a network thread to exit.
Definition network.c:1897
#define MAX_WORKERS
Definition network.c:47
int fr_network_listen_inject(fr_network_t *nr, fr_listen_t *li, uint8_t const *packet, size_t packet_len, fr_time_t recv_time)
Inject a packet for a listener to read.
Definition network.c:410
fr_listen_t * listen
I/O ctx and functions.
Definition network.c:87
int num_sockets
actually a counter...
Definition network.c:141
fr_rb_node_t listen_node
rbtree node for looking up by listener.
Definition network.c:74
static void fr_network_vnode_extend(UNUSED fr_event_list_t *el, int sockfd, int fflags, void *ctx)
Get a notification that a vnode changed.
Definition network.c:1121
static void _signal_pipe_read(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Read handler for signal pipe.
Definition network.c:1809
#define OUTSTANDING(_x)
Definition network.c:629
static int8_t reply_cmp(void const *one, void const *two)
Definition network.c:157
int num_workers
number of active workers
Definition network.c:137
static int8_t socket_num_cmp(void const *one, void const *two)
Definition network.c:186
int num_pending_workers
number of workers we're waiting to start.
Definition network.c:139
fr_rb_tree_t * sockets
list of sockets we're managing, ordered by the listener
Definition network.c:134
pthread_t thread_id
for self
Definition network.c:115
fr_log_lvl_t lvl
debug log level
Definition network.c:120
int signal_pipe[2]
Pipe for signalling the worker in an orderly way.
Definition network.c:143
fr_channel_data_t * pending
the currently pending partial packet
Definition network.c:94
static int8_t socket_listen_cmp(void const *one, void const *two)
Definition network.c:179
static void fr_network_write(UNUSED fr_event_list_t *el, UNUSED int sockfd, UNUSED int flags, void *ctx)
Write packets to the network.
Definition network.c:1173
fr_event_list_t * el
our event list
Definition network.c:128
fr_heap_t * replies
replies from the worker, ordered by priority / origin time
Definition network.c:130
static int fr_network_send_request(fr_network_t *nr, fr_channel_data_t *cd)
Send a message on the "best" channel.
Definition network.c:636
static fr_event_update_t const resume_read[]
Definition network.c:466
void fr_network_stats_log(fr_network_t const *nr, fr_log_t const *log)
Definition network.c:2071
fr_heap_t * waiting
packets waiting to be written
Definition network.c:95
int fr_network_stats(fr_network_t const *nr, int num, uint64_t *stats)
Definition network.c:2055
fr_heap_index_t heap_id
workers are in a heap
Definition network.c:62
bool blocked
is this worker blocked?
Definition network.c:66
static void fr_network_read(UNUSED fr_event_list_t *el, int sockfd, UNUSED int flags, void *ctx)
Read a packet from the network.
Definition network.c:904
static bool is_network_thread(fr_network_t const *nr)
Definition network.c:224
fr_rb_node_t num_node
rbtree node for looking up by number.
Definition network.c:75
static void fr_network_error(UNUSED fr_event_list_t *el, UNUSED int sockfd, int flags, int fd_errno, void *ctx)
Handle errors for a socket.
Definition network.c:1146
fr_io_stats_t stats
Definition network.c:96
static fr_ring_buffer_t * fr_network_rb_init(void)
Initialise thread local storage.
Definition network.c:206
fr_channel_data_t * cd
cached in case of allocation & read error
Definition network.c:90
static int fr_network_listen_add_self(fr_network_t *nr, fr_listen_t *listen)
Definition network.c:1379
static void fr_network_suspend(fr_network_t *nr)
Definition network.c:481
bool dead
is it dead?
Definition network.c:83
size_t leftover
leftover data from a previous read
Definition network.c:91
static void fr_network_post_event(fr_event_list_t *el, fr_time_t now, void *uctx)
fr_network_worker_t * workers[MAX_WORKERS]
each worker
Definition network.c:149
fr_time_t recv_time
Definition network.c:55
static void fr_network_unsuspend(fr_network_t *nr)
Definition network.c:496
fr_rb_tree_t * sockets_by_num
ordered by number;
Definition network.c:135
fr_network_config_t config
configuration
Definition network.c:148
void fr_network_listen_read(fr_network_t *nr, fr_listen_t *li)
Signal the network to read from a listener.
Definition network.c:336
static int8_t waiting_cmp(void const *one, void const *two)
Definition network.c:168
fr_io_stats_t stats
Definition network.c:132
static void fr_network_limit_ringbuffer(fr_network_socket_t *s, int *num_messages_p, size_t *size_p)
Definition network.c:1362
static int _fr_network_rb_free(void *arg)
Definition network.c:197
static void fr_network_recv_reply(void *ctx, fr_channel_t *ch, fr_channel_data_t *cd)
Callback which handles a message being received on the network side.
Definition network.c:520
int max_workers
maximum number of allowed workers
Definition network.c:140
void fr_network_listen_write(fr_network_t *nr, fr_listen_t *li, uint8_t const *packet, size_t packet_len, void *packet_ctx, fr_time_t request_time)
Inject a packet for a listener to write.
Definition network.c:362
bool exiting
are we exiting?
Definition network.c:146
fr_event_filter_t filter
what type of filter it is
Definition network.c:81
static void fr_network_socket_dead(fr_network_t *nr, fr_network_socket_t *s)
Definition network.c:861
static void fr_network_directory_callback(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
Handle a network control message callback for a new "watch directory".
Definition network.c:1487
fr_channel_t * channel
channel to the worker
Definition network.c:68
static fr_event_update_t const pause_write[]
Definition network.c:471
fr_network_t * fr_network_create(TALLOC_CTX *ctx, fr_event_list_t *el, char const *name, fr_log_t const *logger, fr_log_lvl_t lvl, fr_network_config_t const *config)
Create a network.
Definition network.c:1930
static int _network_socket_free(fr_network_socket_t *s)
Definition network.c:1304
static void fr_network_channel_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
Handle a network control message callback for a channel.
Definition network.c:568
static int cmd_socket_list(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
Definition network.c:2098
fr_time_delta_t cpu_time
how much CPU time this worker has spent
Definition network.c:63
fr_control_t * control
the control plane
Definition network.c:124
bool blocked
is it blocked?
Definition network.c:84
Associate a worker thread with a network thread.
Definition network.c:61
uint32_t max_outstanding
Definition network.h:46
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
#define DEBUG4(_fmt,...)
Definition log.h:267
#define HEXDUMP2(_data, _len, _fmt,...)
Definition log.h:734
#define RATE_LIMIT_GLOBAL(_log, _fmt,...)
Rate limit messages using a global limiting entry.
Definition log.h:653
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
Definition event.c:2177
int fr_event_pre_delete(fr_event_list_t *el, fr_event_status_cb_t callback, void *uctx)
Delete a pre-event callback from the event list.
Definition event.c:1975
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Definition event.c:2045
int fr_event_post_delete(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Delete a post-event callback from the event list.
Definition event.c:2023
#define fr_time()
Definition event.c:60
int fr_event_pre_insert(fr_event_list_t *el, fr_event_status_cb_t callback, void *uctx)
Add a pre-event callback to the event list.
Definition event.c:1953
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
int fr_event_post_insert(fr_event_list_t *el, fr_event_post_cb_t callback, void *uctx)
Add a post-event callback to the event list.
Definition event.c:2000
Stores all information relating to an event list.
Definition event.c:377
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
Definition log.c:577
fr_log_lvl_t
Definition log.h:67
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:59
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_message_set_t * fr_message_set_create(TALLOC_CTX *ctx, int num_messages, size_t message_size, size_t ring_buffer_size, bool unlimited_size)
Create a message set.
Definition message.c:128
int fr_message_done(fr_message_t *m)
Mark a message as done.
Definition message.c:196
fr_message_t * fr_message_alloc(fr_message_set_t *ms, fr_message_t *m, size_t actual_packet_size)
Allocate packet data for a message.
Definition message.c:1001
fr_message_t * fr_message_localize(TALLOC_CTX *ctx, fr_message_t *m, size_t message_size)
Localize a message by copying it to local storage.
Definition message.c:248
fr_message_t * fr_message_reserve(fr_message_set_t *ms, size_t reserve_size)
Reserve a message.
Definition message.c:947
fr_message_t * fr_message_alloc_reserve(fr_message_set_t *ms, fr_message_t *m, size_t actual_packet_size, size_t leftover, size_t reserve_size)
Allocate packet data for a message, and reserve a new message.
Definition message.c:1090
A Message set, composed of message headers and ring buffer data.
Definition message.c:95
size_t rb_size
cache-aligned size in the ring buffer
Definition message.h:51
fr_time_t when
when this message was sent
Definition message.h:47
uint8_t * data
pointer to the data in the ring buffer
Definition message.h:49
size_t data_size
size of the data in the ring buffer
Definition message.h:50
@ FR_MESSAGE_USED
Definition message.h:39
@ FR_MESSAGE_LOCALIZED
Definition message.h:40
fr_message_status_t status
free, used, done, etc.
Definition message.h:45
int fr_nonblock(UNUSED int fd)
Definition misc.c:294
static const conf_parser_t config[]
Definition base.c:169
#define fr_assert(_expr)
Definition rad_assert.h:38
#define DEBUG2(fmt,...)
static fr_app_io_t app_io
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:105
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:781
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:824
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:246
int fr_rb_flatten_inorder(TALLOC_CTX *ctx, void **out[], fr_rb_tree_t *tree)
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:321
The main red black tree structure.
Definition rb.h:73
fr_ring_buffer_t * fr_ring_buffer_create(TALLOC_CTX *ctx, size_t size)
Create a ring buffer.
Definition ring_buffer.c:64
static char const * name
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
Definition log.h:96
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
#define talloc_get_type_abort_const
Definition talloc.h:113
#define talloc_strdup(_ctx, _str)
Definition talloc.h:145
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
Definition time.h:575
static fr_time_delta_t fr_time_delta_add(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:255
#define fr_time_delta_lt(_a, _b)
Definition time.h:285
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_delta_ispos(_a)
Definition time.h:290
static int8_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:916
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
static fr_event_list_t * el
static fr_slen_t parent
Definition pair.h:858
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:732
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
static fr_slen_t data
Definition value.h:1340
fr_dict_t const * virtual_server_dict_by_cs(CONF_SECTION const *server_cs)
Return the namespace for the virtual server specified by a config section.
fr_channel_t * fr_worker_channel_create(fr_worker_t *worker, TALLOC_CTX *ctx, fr_control_t *master)
Create a channel to the worker.
Definition worker.c:1625
int fr_worker_listen_cancel(fr_worker_t *worker, fr_listen_t const *li)
Definition worker.c:1652
A worker which takes packets from a master, and processes them.
Definition worker.c:98