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