The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
schedule.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: 40e679483855d5ca2ab4f75b1ea0382444dc2459 $
19 *
20 * @brief Network / worker thread scheduling
21 * @file io/schedule.c
22 *
23 * @copyright 2016 Alan DeKok (aland@freeradius.org)
24 */
25RCSID("$Id: 40e679483855d5ca2ab4f75b1ea0382444dc2459 $")
26
27#define LOG_DST sc->log
28
29#include <freeradius-devel/autoconf.h>
30
31#include <freeradius-devel/io/schedule.h>
32#include <freeradius-devel/io/thread.h>
33#include <freeradius-devel/util/dlist.h>
34#include <freeradius-devel/util/rb.h>
35#include <freeradius-devel/util/syserror.h>
36#include <freeradius-devel/server/module_rlm.h>
37#include <freeradius-devel/server/trigger.h>
38#include <freeradius-devel/util/semaphore.h>
39
40#include <pthread.h>
41
42/** Scheduler specific information for worker threads
43 *
44 * Wraps a fr_worker_t, tracking additional information that
45 * the scheduler uses.
46 */
47typedef struct {
48 fr_thread_t thread; //!< common thread structure - must be first!
49
50 int uses; //!< how many network threads are using it
51 fr_time_t cpu_time; //!< how much CPU time this worker has used
52
53 fr_schedule_t *sc; //!< the scheduler we are running under
54
55 fr_worker_t *worker; //!< the worker data structure
57
58/** Scheduler specific information for network threads
59 *
60 * Wraps a fr_network_t, tracking additional information that
61 * the scheduler uses.
62 */
63typedef struct {
64 fr_thread_t thread; //!< common thread structure - must be first!
65
66 fr_schedule_t *sc; //!< the scheduler we are running under
67
68 fr_network_t *nr; //!< the receive data structure
69
70 fr_timer_t *ev; //!< timer for stats_interval
72
73
74/**
75 * The scheduler
76 */
78 bool running; //!< is the scheduler running?
79
80 CONF_SECTION *cs; //!< thread pool configuration section
81 fr_event_list_t *el; //!< event list for single-threaded mode.
82 bool single_threaded; //!< true if running in single-threaded mode.
83
84 fr_log_t *log; //!< log destination
85 fr_log_lvl_t lvl; //!< log level
86
87 fr_schedule_config_t *config; //!< configuration
88
89 unsigned int num_workers_exited; //!< number of exited workers
90
91 fr_sem_t *worker_sem; //!< for inter-thread signaling
92 fr_sem_t *network_sem; //!< for inter-thread signaling
93 fr_sem_t *coord_sem; //!< for inter-thread signaling
94
97
98 fr_dlist_head_t workers; //!< list of workers
99 fr_dlist_head_t networks; //!< list of networks
100
101 fr_network_t *single_network; //!< for single-threaded mode
102 fr_worker_t *single_worker; //!< for single-threaded mode
103};
104
105static _Thread_local int worker_id = -1; //!< Internal ID of the current worker thread.
106
107/** Return the worker id for the current thread
108 *
109 * @return worker ID
110 */
112{
113 return worker_id;
114}
115
116/** Explicitly set the worker id for the current thread
117 *
118 * **Only to be used in test programs like unit_test_module**
119 */
121{
122 worker_id = id;
123}
124
125/** Entry point for worker threads
126 *
127 * @param[in] arg the fr_schedule_worker_t
128 * @return NULL
129 */
130static void *fr_schedule_worker_thread(void *arg)
131{
132 fr_schedule_worker_t *sw = talloc_get_type_abort(arg, fr_schedule_worker_t);
133 fr_schedule_t *sc = sw->sc;
135 char worker_name[32];
136
137 worker_id = sw->thread.id; /* Store the current worker ID */
138
139 snprintf(worker_name, sizeof(worker_name), "Worker %d", sw->thread.id);
140
141#ifdef HAVE_PTHREAD_SETNAME_NP
142# ifdef __APPLE__
143 pthread_setname_np(worker_name);
144# else
145 pthread_setname_np(pthread_self(), worker_name);
146# endif
147#endif
148
149 if (fr_thread_setup(&sw->thread, worker_name) < 0) goto fail;
150
151 sw->worker = fr_worker_alloc(sw->thread.ctx, sw->thread.el, worker_name, sc->log, sc->lvl, &sc->config->worker);
152 if (!sw->worker) {
153 PERROR("%s - Failed creating worker", worker_name);
154 goto fail;
155 }
156
157 /*
158 * @todo make this a registry
159 */
160 if (sc->worker_thread_instantiate) {
161 CONF_SECTION *cs;
162 char section_name[32];
163
164 snprintf(section_name, sizeof(section_name), "%u", sw->thread.id);
165
166 cs = cf_section_find(sc->cs, "worker", section_name);
167 if (!cs) cs = cf_section_find(sc->cs, "worker", NULL);
168
169 if (sc->worker_thread_instantiate(sw->thread.ctx, sw->thread.el, cs) < 0) {
170 PERROR("%s - Worker thread instantiation failed", worker_name);
171 goto fail;
172 }
173 }
174
175 /*
176 * Add this worker to all network threads.
177 */
178 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sn) {
179 if (unlikely(fr_network_worker_add(sn->nr, sw->worker) < 0)) {
180 PERROR("%s - Failed adding worker to network %u", worker_name, sn->thread.id);
181 goto fail; /* FIXME - Should maybe try to undo partial adds? */
182 }
183 }
184
185 /*
186 * Tell the originator that the thread has started.
187 */
188 fr_thread_start(&sw->thread, sc->worker_sem);
189
190 /*
191 * Do all of the work.
192 */
193 fr_worker(sw->worker);
194
195 status = FR_THREAD_EXITED;
196
197fail:
198 if (sw->worker) {
200 sw->worker = NULL;
201 }
202
203 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL); /* Fixme once we figure out what uctx should be */
204
205 fr_thread_exit(&sw->thread, status, sc->worker_sem);
206
207 return NULL;
208}
209
210
211static void stats_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
212{
213 fr_schedule_network_t *sn = talloc_get_type_abort(uctx, fr_schedule_network_t);
214
215 fr_network_stats_log(sn->nr, sn->sc->log);
216
217 (void) fr_timer_at(sn, tl, &sn->ev, fr_time_add(now, sn->sc->config->stats_interval), false, stats_timer, sn);
218}
219
220/** Initialize and run the network thread.
221 *
222 * @param[in] arg the fr_schedule_network_t
223 * @return NULL
224 */
225static void *fr_schedule_network_thread(void *arg)
226{
227 fr_schedule_network_t *sn = talloc_get_type_abort(arg, fr_schedule_network_t);
228 fr_schedule_t *sc = sn->sc;
230 char network_name[32];
231
232 snprintf(network_name, sizeof(network_name), "Network %d", sn->thread.id);
233
234#ifdef HAVE_PTHREAD_SETNAME_NP
235# ifdef __APPLE__
236 pthread_setname_np(network_name);
237# else
238 pthread_setname_np(pthread_self(), network_name);
239# endif
240#endif
241
242 if (fr_thread_setup(&sn->thread, network_name) < 0) goto fail;
243
244 sn->nr = fr_network_create(sn->thread.ctx, sn->thread.el, network_name, sc->log, sc->lvl, &sc->config->network);
245 if (!sn->nr) {
246 PERROR("%s - Failed creating network", network_name);
247 goto fail;
248 }
249
250 /*
251 * Tell the originator that the thread has started.
252 */
253 fr_thread_start(&sn->thread, sc->network_sem);
254
255 /*
256 * Print out statistics for this network IO handler.
257 */
258 if (fr_time_delta_ispos(sc->config->stats_interval)) {
259 (void) fr_timer_in(sn, sn->thread.el->tl, &sn->ev, sn->sc->config->stats_interval, false, stats_timer, sn);
260 }
261
262 /*
263 * Call the main event processing loop of the network
264 * thread Will not return until the worker is about
265 * to exit.
266 */
267 fr_network(sn->nr);
268
269 status = FR_THREAD_EXITED;
270
271fail:
272 fr_thread_exit(&sn->thread, status, sc->network_sem);
273
274 return NULL;
275}
276
277/** Create a scheduler and spawn the child threads.
278 *
279 * @param[in] ctx talloc context.
280 * @param[in] single_threaded no workers are spawned, everything runs in a common event loop.
281 * @param[in] el event list, only for single-threaded mode.
282 * @param[in] logger destination for all logging messages.
283 * @param[in] lvl log level.
284 * @param[in] worker_thread_instantiate callback for new worker threads.
285 * @param[in] worker_thread_detach callback to destroy resources
286 * allocated by worker_thread_instantiate.
287 * @param[in] config configuration for the scheduler
288 * @return
289 * - NULL on error
290 * - fr_schedule_t new scheduler
291 */
293 bool single_threaded,
295 fr_log_t *logger, fr_log_lvl_t lvl,
296 fr_schedule_thread_instantiate_t worker_thread_instantiate,
297 fr_schedule_thread_detach_t worker_thread_detach,
299{
300 unsigned int i;
301 fr_schedule_worker_t *sw, *next_sw;
302 fr_schedule_network_t *sn, *next_sn;
304
305 sc = talloc_zero(ctx, fr_schedule_t);
306 if (!sc) {
307 fr_strerror_const("Failed allocating memory");
308 return NULL;
309 }
310
311 /*
312 * Parse any scheduler-specific configuration.
313 */
314 if (!config) {
315 MEM(sc->config = talloc_zero(sc, fr_schedule_config_t));
316 sc->config->max_networks = 1;
317 sc->config->max_workers = 4;
318 } else {
319 sc->config = config;
320
321 if (sc->config->max_networks < 1) sc->config->max_networks = 1;
322 if (sc->config->max_networks > 64) sc->config->max_networks = 64;
323 if (sc->config->max_workers < 1) sc->config->max_workers = 1;
324 if (sc->config->max_workers > 64) sc->config->max_workers = 64;
325 }
326
327 sc->el = el;
328 sc->single_threaded = single_threaded;
329 sc->log = logger;
330 sc->lvl = lvl;
331 sc->cs = sc->config->cs;
332
333 sc->worker_thread_instantiate = worker_thread_instantiate;
334 sc->worker_thread_detach = worker_thread_detach;
335 sc->running = true;
336
337 /*
338 * If we're single-threaded, create network / worker, and insert them into the event loop.
339 */
340 if (single_threaded) {
341 sc->single_network = fr_network_create(sc, el, "Network", sc->log, sc->lvl, &sc->config->network);
342 if (!sc->single_network) {
343 PERROR("Failed creating network");
344 pre_instantiate_st_fail:
346 return NULL;
347 }
348
349 if (fr_coords_create(sc, el) < 0) {
350 PERROR("Failed creating coordinators");
351 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
352 PERROR("Failed destroying network");
353 }
354 goto pre_instantiate_st_fail;
355 }
356
357 worker_id = 0;
358 sc->single_worker = fr_worker_alloc(sc, el, "Worker", sc->log, sc->lvl, &sc->config->worker);
359 if (!sc->single_worker) {
360 PERROR("Failed creating worker");
361 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
362 PERROR("Failed destroying network");
363 }
364 goto pre_instantiate_st_fail;
365 }
366
367 /*
368 * Parent thread-specific data from the single_worker
369 */
370 if (sc->worker_thread_instantiate) {
371 CONF_SECTION *subcs;
372
373 subcs = cf_section_find(sc->cs, "worker", "0");
374 if (!subcs) subcs = cf_section_find(sc->cs, "worker", NULL);
375
376 if (sc->worker_thread_instantiate(sc->single_worker, el, subcs) < 0) {
377 PERROR("Worker thread instantiation failed");
378 destroy_both:
379 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
380 PERROR("Failed destroying network");
381 }
382 fr_worker_exit(sc->single_worker);
383 goto pre_instantiate_st_fail;
384 }
385 }
386
387 if (fr_command_register_hook(NULL, "0", sc->single_worker, cmd_worker_table) < 0) {
388 PERROR("Failed adding worker commands");
389 st_fail:
390 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
391 goto destroy_both;
392 }
393
394 if (fr_command_register_hook(NULL, "0", sc->single_network, cmd_network_table) < 0) {
395 PERROR("Failed adding network commands");
396 goto st_fail;
397 }
398
399 /*
400 * Register the worker with the network, so
401 * things like fr_network_send_request() work.
402 */
403 fr_network_worker_add_self(sc->single_network, sc->single_worker);
404 DEBUG("Scheduler created in single-threaded mode");
405
406 if (fr_event_pre_insert(el, fr_worker_pre_event, sc->single_worker) < 0) {
407 fr_strerror_const("Failed adding pre-check to event list");
408 goto st_fail;
409 }
410
411 if (fr_coord_pre_event_insert(el) < 0) {
412 fr_strerror_const("Failed adding coordinator pre-check to event list");
413 goto st_fail;
414 }
415
416 /*
417 * Add the event which processes request_t packets.
418 */
419 if (fr_event_post_insert(el, fr_worker_post_event, sc->single_worker) < 0) {
420 fr_strerror_const("Failed inserting post-processing event");
421 goto st_fail;
422 }
423
425 fr_strerror_const("Failed adding coordinator post-processing to event list");
426 goto st_fail;
427 }
428
429 return sc;
430 }
431
432 /*
433 * Create the lists which hold the workers and networks.
434 */
435 fr_dlist_init(&sc->workers, fr_schedule_worker_t, thread.entry);
436 fr_dlist_init(&sc->networks, fr_schedule_network_t, thread.entry);
437
438 sc->network_sem = fr_sem_alloc();
439 if (!sc->network_sem) {
440 sem_fail:
441 ERROR("Failed creating semaphore: %s", fr_syserror(errno));
442 fr_sem_free(sc->network_sem);
443 fr_sem_free(sc->worker_sem);
445 return NULL;
446 }
447
448 sc->worker_sem = fr_sem_alloc();
449 if (!sc->worker_sem) goto sem_fail;
450
451 sc->coord_sem = fr_sem_alloc();
452 if (!sc->coord_sem) goto sem_fail;
453
454 /*
455 * Create the network threads first.
456 */
457 for (i = 0; i < sc->config->max_networks; i++) {
458 DEBUG3("Creating %u/%u networks", i + 1, sc->config->max_networks);
459
460 /*
461 * Create a worker "glue" structure
462 */
463 sn = talloc_zero(sc, fr_schedule_network_t);
464 if (!sn) {
465 ERROR("Network %u - Failed allocating memory", i);
466 break;
467 }
468
469 sn->thread.id = i;
470 sn->sc = sc;
472
474 talloc_free(sn);
475 PERROR("Failed creating network %u", i);
476 break;
477 }
478
479 fr_dlist_insert_head(&sc->networks, sn);
480 }
481
482 /*
483 * Wait for all of the networks to signal us that either
484 * they've started, OR there's been a problem and they
485 * can't start.
486 */
487 if (fr_thread_wait_list(sc->network_sem, &sc->networks) < 0) {
489 return NULL;
490 }
491
492 /*
493 * Create the coordination threads
494 */
495 if (fr_coord_start(sc->config->max_workers, sc->coord_sem) < 0) {
497 return NULL;
498 };
499
500 /*
501 * Create all of the workers.
502 */
503 for (i = 0; i < sc->config->max_workers; i++) {
504 DEBUG3("Creating %u/%u workers", i + 1, sc->config->max_workers);
505
506 /*
507 * Create a worker "glue" structure
508 */
509 sw = talloc_zero(sc, fr_schedule_worker_t);
510 if (!sw) {
511 ERROR("Worker %u - Failed allocating memory", i);
512 break;
513 }
514
515 sw->thread.id = i;
516 sw->sc = sc;
518
520 talloc_free(sw);
521 PERROR("Failed creating worker %u", i);
522 break;
523 }
524
525 fr_dlist_insert_head(&sc->workers, sw);
526 }
527
528 /*
529 * Wait for all of the workers to signal us that either
530 * they've started, OR there's been a problem and they
531 * can't start.
532 */
533 if (fr_thread_wait_list(sc->worker_sem, &sc->workers) < 0) {
535 return NULL;
536 }
537
538 for (sw = fr_dlist_head(&sc->workers), i = 0;
539 sw != NULL;
540 sw = next_sw, i++) {
541 char buffer[32];
542
543 next_sw = fr_dlist_next(&sc->workers, sw);
544
545 snprintf(buffer, sizeof(buffer), "%d", i);
547 PERROR("Failed adding worker commands");
548 mt_fail:
550 return NULL;
551 }
552 }
553
554 for (sn = fr_dlist_head(&sc->networks), i = 0;
555 sn != NULL;
556 sn = next_sn, i++) {
557 char buffer[32];
558
559 next_sn = fr_dlist_next(&sc->networks, sn);
560
561 snprintf(buffer, sizeof(buffer), "%d", i);
563 PERROR("Failed adding network commands");
564 goto mt_fail;
565 }
566 }
567
568 if (sc) INFO("Scheduler created successfully with %u networks and %u workers",
569 sc->config->max_networks, (unsigned int)fr_dlist_num_elements(&sc->workers));
570
571 /*
572 * Instantiate thread-local data for the main thread too.
573 * In single-threaded mode this is done above. In
574 * multi-worker mode the main thread also needs module
575 * thread data so that triggers can use module xlats.
576 */
577 if (sc->worker_thread_instantiate &&
578 unlikely((sc->worker_thread_instantiate(sc, el, NULL) < 0))) {
579 PERROR("Main thread instantiation failed");
580 goto mt_fail;
581 }
582
583 return sc;
584}
585
586/** Destroy a scheduler, and tell its child threads to exit.
587 *
588 * @note This may be called with no worker or network threads in the case of a
589 * instantiation error. This function _should_ deal with that condition
590 * gracefully.
591 *
592 * @param[in] sc_to_free the scheduler
593 * @return
594 * - <0 on error
595 * - 0 on success
596 */
598{
599 fr_schedule_t *sc = *sc_to_free;
600 unsigned int i;
603 int ret;
604
605 if (!sc) return 0;
606
607 sc->running = false;
608
609
610
611 /*
612 * Single threaded mode: kill the only network / worker we have.
613 */
614 if (sc->single_threaded) {
615 /*
616 * Destroy the network side first. It tells the
617 * workers to close.
618 */
619 if (unlikely(fr_network_destroy(sc->single_network) < 0)) {
620 ERROR("Failed destroying network");
621 }
622
623 /*
624 * Add events to handle close down gracefully.
625 */
626 if (unlikely((fr_network_close_event_insert(sc->single_network) < 0) ||
627 (fr_worker_close_event_insert(sc->single_worker) < 0))) {
628 ERROR("Failed setting up close events");
629 }
630
631 /*
632 * Run the event loop so the worker gets the signal from
633 * the network and shuts down gracefully.
634 */
635 fr_event_loop(sc->el);
636
637 /*
638 * Detach worker from coordinators. This needs to be done
639 * before the worker is freed.
640 */
641 if (modules_rlm_coord_detach() > 0) {
642 /*
643 * Run the event loop again to handle coordinator detach
644 * messages, with different callbacks to determine when
645 * to exit the loop.
646 */
647 fr_network_close_event_delete(sc->single_network);
649 ERROR("Failed setting up coordinator close events");
650 }
651 fr_event_loop(sc->el);
652 }
653
654 fr_worker_exit(sc->single_worker);
656
657 goto done;
658 } else {
659 /*
660 * Detach thread-local data for the main thread.
661 * Worker threads handle their own detach, but
662 * the main thread was instantiated explicitly
663 * by fr_schedule_create.
664 */
665 if (sc->worker_thread_detach) sc->worker_thread_detach(NULL);
666 }
667
668 /*
669 * Signal each network thread to exit.
670 */
671 fr_dlist_foreach(&sc->networks, fr_schedule_network_t, sne) {
672 if (fr_network_exit(sne->nr) < 0) {
673 PERROR("Failed signaling network %i to exit", sne->thread.id);
674 }
675 }
676
677 /*
678 * If the network threads are running, tell them to exit,
679 * and wait for them to do so. Each network thread tells
680 * all of its worker threads that it's exiting. It then
681 * closes the channels. When the workers see that there
682 * are no input channels, they exit, too.
683 */
684 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->networks); i++) {
685 DEBUG2("Scheduler - Waiting for semaphore indicating network exit %u/%u", i + 1,
686 (unsigned int)fr_dlist_num_elements(&sc->networks));
687 SEM_WAIT_INTR(sc->network_sem);
688 }
689 DEBUG2("Scheduler - All networks indicated exit complete");
690
691 while ((sn = fr_dlist_pop_head(&sc->networks)) != NULL) {
692 /*
693 * Ensure that the thread has exited before
694 * cleaning up the context.
695 *
696 * This also ensures that the child threads have
697 * exited before the main thread cleans up the
698 * module instances.
699 */
700 if ((ret = pthread_join(sn->thread.pthread_id, NULL)) != 0) {
701 ERROR("Failed joining network %i: %s", sn->thread.id, fr_syserror(ret));
702 } else {
703 DEBUG2("Network %i joined (cleaned up)", sn->thread.id);
704 }
705 }
706
707 /*
708 * Wait for all worker threads to finish. THEN clean up
709 * modules. Otherwise, the modules will be removed from
710 * underneath the workers!
711 */
712 for (i = 0; i < (unsigned int)fr_dlist_num_elements(&sc->workers); i++) {
713 DEBUG2("Scheduler - Waiting for semaphore indicating worker exit %u/%u", i + 1,
714 (unsigned int)fr_dlist_num_elements(&sc->workers));
715 SEM_WAIT_INTR(sc->worker_sem);
716 }
717 DEBUG2("Scheduler - All workers indicated exit complete");
718
719 /*
720 * Clean up the exited workers.
721 */
722 while ((sw = fr_dlist_pop_head(&sc->workers)) != NULL) {
723 /*
724 * Ensure that the thread has exited before
725 * cleaning up the context.
726 *
727 * This also ensures that the child threads have
728 * exited before the main thread cleans up the
729 * module instances.
730 */
731 if ((ret = pthread_join(sw->thread.pthread_id, NULL)) != 0) {
732 ERROR("Failed joining worker %i: %s", sw->thread.id, fr_syserror(ret));
733 } else {
734 DEBUG2("Worker %i joined (cleaned up)", sw->thread.id);
735 }
736 }
737
739
740 fr_sem_free(sc->coord_sem);
741 fr_sem_free(sc->network_sem);
742 fr_sem_free(sc->worker_sem);
743
744done:
745 /*
746 * Now that all of the workers are done, we can return to
747 * the caller, and have it dlclose() the modules.
748 */
750 *sc_to_free = NULL;
751
752 return 0;
753}
754
755/** Add a fr_listen_t to a scheduler.
756 *
757 * @param[in] sc the scheduler
758 * @param[in] li the ctx and callbacks for the transport.
759 * @return
760 * - NULL on error
761 * - the fr_network_t that the socket was added to.
762 */
764{
765 fr_network_t *nr;
766
767 (void) talloc_get_type_abort(sc, fr_schedule_t);
768
769 if (sc->single_threaded) {
770 nr = sc->single_network;
771 } else {
773
774 /*
775 * @todo - round robin it among the listeners?
776 * or maybe add it to the same parent thread?
777 */
778 sn = fr_dlist_head(&sc->networks);
779 nr = sn->nr;
780 }
781
782 if (fr_network_listen_add(nr, li) < 0) return NULL;
783
784 return nr;
785}
786
787/** Add a directory NOTE_EXTEND to a scheduler.
788 *
789 * @param[in] sc the scheduler
790 * @param[in] li the ctx and callbacks for the transport.
791 * @return
792 * - NULL on error
793 * - the fr_network_t that the socket was added to.
794 */
796{
797 fr_network_t *nr;
798
799 (void) talloc_get_type_abort(sc, fr_schedule_t);
800
801 if (sc->single_threaded) {
802 nr = sc->single_network;
803 } else {
805
806 /*
807 * @todo - round robin it among the listeners?
808 * or maybe add it to the same parent thread?
809 */
810 sn = fr_dlist_head(&sc->networks);
811 nr = sn->nr;
812 }
813
814 if (fr_network_directory_add(nr, li) < 0) return NULL;
815
816 return nr;
817}
static int const char char buffer[256]
Definition acutest.h:576
#define _Thread_local
Definition atexit.h:213
#define RCSID(id)
Definition build.h:560
#define unlikely(_x)
Definition build.h:455
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1205
fr_command_register_hook_t fr_command_register_hook
Definition command.c:41
int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
Start coordinators in single threaded mode.
Definition coord.c:621
void fr_coords_destroy(void)
Clean up coordinators in single threaded mode.
Definition coord.c:603
int fr_coord_close_event_insert(fr_event_list_t *el)
Definition coord.c:905
void fr_coord_thread_join(void)
Wait for all the coordinator threads to exit.
Definition coord.c:185
int fr_coord_post_event_insert(fr_event_list_t *el)
Insert instance specific post-event callbacks.
Definition coord.c:861
int fr_coord_pre_event_insert(fr_event_list_t *el)
Insert instance specific pre-event callbacks.
Definition coord.c:836
int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
Start all registered coordinator threads in multi-threaded mode.
Definition coord.c:553
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
#define fr_dlist_foreach(_list_head, _type, _iter)
Iterate over the contents of a list.
Definition dlist.h:98
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
Definition dlist.h:654
static int fr_dlist_insert_head(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the head of a list.
Definition dlist.h:320
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
Head of a doubly linked list.
Definition dlist.h:51
talloc_free(hp)
fr_cmd_table_t cmd_network_table[]
Definition network.c:2307
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
void fr_network_close_event_delete(fr_network_t *nr)
Remove "close" events from the event loop.
Definition network.c:1849
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:1859
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
void fr_network(fr_network_t *nr)
The main network worker function.
Definition network.c:1983
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
int fr_network_exit(fr_network_t *nr)
Signal a network thread to exit.
Definition network.c:2061
void fr_network_stats_log(fr_network_t const *nr, fr_log_t const *log)
Definition network.c:2240
int fr_network_close_event_insert(fr_network_t *nr)
Add events to the loop which will exit when the number of workers has reached zero.
Definition network.c:1834
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:2094
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
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:1981
int fr_event_loop(fr_event_list_t *el)
Run an event loop.
Definition event.c:2405
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:2028
Stores all information relating to an event list.
Definition event.c:377
fr_log_lvl_t
Definition log.h:64
int modules_rlm_coord_detach(void)
Runs the coord_detach method of all registered backend modules.
static const conf_parser_t config[]
Definition base.c:162
#define DEBUG2(fmt,...)
static bool done
Definition radclient.c:80
#define INFO(fmt,...)
Definition radict.c:63
CONF_SECTION * cs
thread pool configuration section
Definition schedule.c:80
fr_thread_t thread
common thread structure - must be first!
Definition schedule.c:64
fr_timer_t * ev
timer for stats_interval
Definition schedule.c:70
static _Thread_local int worker_id
Internal ID of the current worker thread.
Definition schedule.c:105
fr_sem_t * coord_sem
for inter-thread signaling
Definition schedule.c:93
fr_schedule_t * fr_schedule_create(TALLOC_CTX *ctx, bool single_threaded, fr_event_list_t *el, fr_log_t *logger, fr_log_lvl_t lvl, fr_schedule_thread_instantiate_t worker_thread_instantiate, fr_schedule_thread_detach_t worker_thread_detach, fr_schedule_config_t *config)
Create a scheduler and spawn the child threads.
Definition schedule.c:292
fr_event_list_t * el
event list for single-threaded mode.
Definition schedule.c:81
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:66
fr_sem_t * worker_sem
for inter-thread signaling
Definition schedule.c:91
fr_worker_t * single_worker
for single-threaded mode
Definition schedule.c:102
fr_log_lvl_t lvl
log level
Definition schedule.c:85
static void stats_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Definition schedule.c:211
fr_network_t * fr_schedule_directory_add(fr_schedule_t *sc, fr_listen_t *li)
Add a directory NOTE_EXTEND to a scheduler.
Definition schedule.c:795
fr_schedule_config_t * config
configuration
Definition schedule.c:87
fr_network_t * single_network
for single-threaded mode
Definition schedule.c:101
fr_schedule_thread_instantiate_t worker_thread_instantiate
thread instantiation callback
Definition schedule.c:95
fr_network_t * fr_schedule_listen_add(fr_schedule_t *sc, fr_listen_t *li)
Add a fr_listen_t to a scheduler.
Definition schedule.c:763
fr_network_t * nr
the receive data structure
Definition schedule.c:68
fr_schedule_thread_detach_t worker_thread_detach
Definition schedule.c:96
bool running
is the scheduler running?
Definition schedule.c:78
void fr_schedule_worker_id_set(int id)
Explicitly set the worker id for the current thread.
Definition schedule.c:120
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:111
static void * fr_schedule_worker_thread(void *arg)
Entry point for worker threads.
Definition schedule.c:130
fr_sem_t * network_sem
for inter-thread signaling
Definition schedule.c:92
unsigned int num_workers_exited
number of exited workers
Definition schedule.c:89
int fr_schedule_destroy(fr_schedule_t **sc_to_free)
Destroy a scheduler, and tell its child threads to exit.
Definition schedule.c:597
bool single_threaded
true if running in single-threaded mode.
Definition schedule.c:82
fr_dlist_head_t networks
list of networks
Definition schedule.c:99
fr_log_t * log
log destination
Definition schedule.c:84
fr_dlist_head_t workers
list of workers
Definition schedule.c:98
static void * fr_schedule_network_thread(void *arg)
Initialize and run the network thread.
Definition schedule.c:225
Scheduler specific information for network threads.
Definition schedule.c:63
The scheduler.
Definition schedule.c:77
int(* fr_schedule_thread_instantiate_t)(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
Setup a new thread.
Definition schedule.h:56
void(* fr_schedule_thread_detach_t)(void *uctx)
Explicitly free resources allocated by fr_schedule_thread_instantiate_t.
Definition schedule.h:62
fr_time_delta_t stats_interval
print channel statistics
Definition schedule.h:71
void fr_sem_free(fr_sem_t *sem)
Definition semaphore.c:50
fr_sem_t * fr_sem_alloc(void)
Definition semaphore.c:30
#define SEM_WAIT_INTR(_x)
Definition semaphore.h:56
sem_t fr_sem_t
Definition semaphore.h:53
static const uchar sc[16]
Definition smbdes.c:115
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
Definition log.h:93
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:204
int fr_thread_wait_list(fr_sem_t *sem, fr_dlist_head_t *head)
Wait for multiple threads to signal readiness via a semaphore.
Definition thread.c:79
int fr_thread_create(pthread_t *thread, fr_thread_entry_t func, void *arg)
Create a joinable thread.
Definition thread.c:46
int fr_thread_setup(fr_thread_t *out, char const *name)
Common setup for child threads: block signals, allocate a talloc context, and create an event list.
Definition thread.c:112
void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem)
Signal the parent that we're done.
Definition thread.c:219
fr_thread_status_t status
running, etc.
Definition thread.h:49
fr_event_list_t * el
our event list
Definition thread.h:53
int id
unique ID for this thread
Definition thread.h:48
fr_thread_status_t
Track the child thread status.
Definition thread.h:38
@ FR_THREAD_INITIALIZING
initialized, but not running
Definition thread.h:40
@ FR_THREAD_EXITED
exited, and in the exited queue
Definition thread.h:42
@ FR_THREAD_FAIL
failed, and in the exited queue
Definition thread.h:43
TALLOC_CTX * ctx
our allocation ctx
Definition thread.h:52
pthread_t pthread_id
of this thread
Definition thread.h:50
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define fr_timer_in(...)
Definition timer.h:87
#define fr_timer_at(...)
Definition timer.h:81
static fr_event_list_t * el
#define fr_strerror_const(_msg)
Definition strerror.h:223
int fr_worker_pre_event(UNUSED fr_time_t now, UNUSED fr_time_delta_t wake, void *uctx)
Pre-event handler.
Definition worker.c:1638
fr_worker_t * fr_worker_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, char const *name, fr_log_t const *logger, fr_log_lvl_t lvl, fr_worker_config_t *config)
Create a worker.
Definition worker.c:1406
fr_cmd_table_t cmd_worker_table[]
Definition worker.c:1863
void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
Post-event handler.
Definition worker.c:1659
int fr_worker_close_event_insert(fr_worker_t *worker)
Add events to the loop which will send close ack to the network.
Definition worker.c:1682
void fr_worker(fr_worker_t *worker)
The main loop and entry point of the stand-alone worker thread.
Definition worker.c:1578
void fr_worker_exit(fr_worker_t *worker)
Tidy up a worker on normal exit.
Definition worker.c:1120
A worker which takes packets from a master, and processes them.
Definition worker.c:91
int uses
how many network threads are using it
Definition schedule.c:50
fr_schedule_t * sc
the scheduler we are running under
Definition schedule.c:53
fr_thread_t thread
common thread structure - must be first!
Definition schedule.c:48
fr_time_t cpu_time
how much CPU time this worker has used
Definition schedule.c:51
fr_worker_t * worker
the worker data structure
Definition schedule.c:55
Scheduler specific information for worker threads.
Definition schedule.c:47