-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
worker.c
1608 lines (1334 loc) · 42.4 KB
/
worker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @brief Worker thread functions.
* @file io/worker.c
*
* The "worker" thread is the one responsible for the bulk of the
* work done when processing a request. Workers are spawned by the
* scheduler, and create a kqueue (KQ) and control-plane
* Atomic Queue (AQ) for control-plane communication.
*
* When a network thread discovers that it needs more workers, it
* asks the scheduler for a KQ/AQ combination. The network thread
* then creates a channel dedicated to that worker, and sends the
* channel to the worker in a "new channel" message. The worker
* receives the channel, and sends an ACK back to the network thread.
*
* The network thread then sends the worker new packets, which the
* worker receives and processes.
*
* The lifecycle of a packet MUST be carefully managed. Initially,
* messages are put into the "to_decode" heap. If the messages sit
* in the heap for too long, they are localized and put into the
* "localized" heap. Each heap is ordered by (priority, time), so
* that high priority packets take precedence over low priority
* packets.
*
* Both queues have linked lists of received packets, ordered by
* time. This list is used to clean up packets which have been in
* the heap for "too long", in fr_worker_check_timeouts().
*
* When a packet is decoded, it is put into the "runnable" heap, and
* also into the "time_order" heap. The main loop fr_worker() then
* pulls new requests off of this heap and runs them. The
* fr_worker_check_timeouts() function also checks the tail of the
* "time_order" heap, and ages out requests which have been active
* for "too long".
*
* A request may return one of FR_IO_YIELD,
* FR_IO_REPLY, or FR_IO_DONE. If a request is
* yeilded, it is placed onto the yielded list in the worker
* "tracking" data structure.
*
* @copyright 2016 Alan DeKok <aland@freeradius.org>
*/
RCSID("$Id$")
#include <freeradius-devel/io/worker.h>
#include <freeradius-devel/io/channel.h>
#include <freeradius-devel/io/message.h>
#include <freeradius-devel/io/listen.h>
#include <freeradius-devel/io/schedule.h>
/**
* Track things by priority and time.
*/
typedef struct fr_worker_heap_t {
fr_dlist_t list; //!< list of things, ordered by time.
fr_heap_t *heap; //!< heap, ordered by priority
} fr_worker_heap_t;
#ifndef NDEBUG
static void fr_worker_verify(fr_worker_t *worker);
#define WORKER_VERIFY fr_worker_verify(worker)
#else
#define WORKER_VERIFY
#endif
/*
* Define our own debugging.
*/
#undef DEBUG
#undef DEBUG2
#undef DEBUG3
#undef ERROR
#undef RDEBUG
#undef RDEBUG2
#undef RDEBUG3
DIAG_OFF(unused-macros)
#define DEBUG(fmt, ...) if (worker->lvl) fr_log(worker->log, L_DBG, fmt, ## __VA_ARGS__)
#define DEBUG2(fmt, ...) if (worker->lvl >= L_DBG_LVL_2) fr_log(worker->log, L_DBG, fmt, ## __VA_ARGS__)
#define DEBUG3(fmt, ...) if (worker->lvl >= L_DBG_LVL_3) fr_log(worker->log, L_DBG, fmt, ## __VA_ARGS__)
#define ERROR(fmt, ...) fr_log(worker->log, L_ERR, fmt, ## __VA_ARGS__)
#define RDEBUG(fmt, ...) if (worker->lvl) fr_log(worker->log, L_DBG, "(%s) " fmt, request->name, ## __VA_ARGS__)
DIAG_ON(unused-macros)
/**
* A worker which takes packets from a master, and processes them.
*/
struct fr_worker_t {
char const *name; //!< name of this worker
int kq; //!< my kq
fr_log_t const *log; //!< log destination
fr_log_lvl_t lvl; //!< log level
fr_atomic_queue_t *aq_control; //!< atomic queue for control messages sent to me
uintptr_t aq_ident; //!< identifier for control-plane events
fr_control_t *control; //!< the control plane
fr_event_list_t *el; //!< our event list
uint64_t number; //!< for requests
int num_channels; //!< actual number of channels
int max_channels; //!< maximum number of channels
int message_set_size; //!< default start number of messages
int ring_buffer_size; //!< default start size for the ring buffers
int max_request_time; //!< maximum time a request can be processed
size_t talloc_pool_size; //!< for each REQUEST
fr_time_t checked_timeout; //!< when we last checked the tails of the queues
fr_worker_heap_t to_decode; //!< messages from the master, to be decoded or localized
fr_worker_heap_t localized; //!< localized messages to be decoded
fr_heap_t *runnable; //!< current runnable requests which we've spent time processing
fr_heap_t *time_order; //!< time ordered heap of requests
rbtree_t *dedup; //!< de-dup tree
int num_requests; //!< number of requests processed by this worker
int num_decoded; //!< number of messages which have been decoded
int num_replies; //!< number of messages which were replied to
int num_timeouts; //!< number of messages which timed out
int num_active; //!< number of active requests
fr_time_tracking_t tracking; //!< how much time the worker has spent doing things.
bool was_sleeping; //!< used to suppress multiple sleep signals in a row
bool exiting; //!< are we exiting?
fr_time_t next_cleanup; //!< when we next do the max_request_time checks
fr_event_timer_t const *ev_cleanup; //!< timer for max_request_time
fr_channel_t **channel; //!< list of channels
};
static void fr_worker_post_event(fr_event_list_t *el, struct timeval *now, void *uctx);
/*
* We need wrapper macros because we have multiple instances of
* the same code.
*/
#define WORKER_HEAP_INIT(_name, _func, _type, _member) do { \
FR_DLIST_INIT(worker->_name.list); \
worker->_name.heap = fr_heap_create(worker, _func, _type, _member); \
if (!worker->_name.heap) { \
(void) fr_event_user_delete(worker->el, fr_worker_evfilt_user, worker); \
talloc_free(worker); \
goto nomem; \
} \
} while (0)
#define WORKER_HEAP_INSERT(_name, _var, _member) do { \
fr_dlist_insert_head(&worker->_name.list, &_var->_member); \
(void) fr_heap_insert(worker->_name.heap, _var); \
} while (0)
#define WORKER_HEAP_POP(_name, _var, _member) do { \
_var = fr_heap_pop(worker->_name.heap); \
if (_var) fr_dlist_remove(&_var->_member); \
} while (0)
#define WORKER_HEAP_EXTRACT(_name, _var, _member) do { \
(void) fr_heap_extract(worker->_name.heap, _var); \
fr_dlist_remove(&_var->_member); \
} while (0)
/** Drain the input channel
*
* @param[in] worker the worker
* @param[in] ch the channel to drain
* @param[in] cd the message (if any) to start with
*/
static bool fr_worker_drain_input(fr_worker_t *worker, fr_channel_t *ch, fr_channel_data_t *cd)
{
if (!cd) {
cd = fr_channel_recv_request(ch);
if (!cd) {
DEBUG3("\t--> empty-ack");
return false;
}
}
do {
worker->num_requests++;
DEBUG3("\t%sreceived request %d", worker->name, worker->num_requests);
cd->channel.ch = ch;
WORKER_HEAP_INSERT(to_decode, cd, request.list);
} while ((cd = fr_channel_recv_request(ch)) != NULL);
return true;
}
/** Handle a worker control message for a channel
*
* @param[in] ctx the worker
* @param[in] data the message
* @param[in] data_size size of the data
* @param[in] now the current time
*/
static void fr_worker_channel_callback(void *ctx, void const *data, size_t data_size, fr_time_t now)
{
int i;
bool ok, was_sleeping;
fr_channel_t *ch;
fr_message_set_t *ms;
fr_channel_event_t ce;
fr_worker_t *worker = ctx;
was_sleeping = worker->was_sleeping;
worker->was_sleeping = false;
/*
* We were woken up by a signal to do something. We're
* not sleeping.
*/
ce = fr_channel_service_message(now, &ch, data, data_size);
switch (ce) {
case FR_CHANNEL_ERROR:
DEBUG3("\t--> error");
return;
case FR_CHANNEL_EMPTY:
DEBUG3("\t--> ...");
return;
case FR_CHANNEL_NOOP:
DEBUG3("\t--> noop");
return;
case FR_CHANNEL_DATA_READY_NETWORK:
rad_assert(0 == 1);
DEBUG3("\t--> ??? network");
break;
case FR_CHANNEL_DATA_READY_WORKER:
rad_assert(ch != NULL);
DEBUG3("\t--> data");
if (!fr_worker_drain_input(worker, ch, NULL)) {
worker->was_sleeping = was_sleeping;
}
break;
case FR_CHANNEL_OPEN:
DEBUG3("\t--> channel open");
rad_assert(ch != NULL);
ok = false;
for (i = 0; i < worker->max_channels; i++) {
rad_assert(worker->channel[i] != ch);
if (worker->channel[i] != NULL) continue;
worker->channel[i] = ch;
DEBUG3("\t%sreceived channel %p into array entry %d", worker->name, ch, i);
ms = fr_message_set_create(worker, worker->message_set_size,
sizeof(fr_channel_data_t),
worker->ring_buffer_size);
rad_assert(ms != NULL);
fr_channel_worker_ctx_add(ch, ms);
worker->num_channels++;
ok = true;
break;
}
fr_cond_assert(ok);
break;
case FR_CHANNEL_CLOSE:
DEBUG3("\t--> channel close");
rad_assert(ch != NULL);
ok = false;
for (i = 0; i < worker->max_channels; i++) {
if (!worker->channel[i]) continue;
if (worker->channel[i] != ch) continue;
/*
* @todo check the status, and
* put the channel into a
* "closing" list if we can't
* close it right now. Then,
* wake up after a time and try
* to close it again.
*/
(void) fr_channel_worker_ack_close(ch);
ms = fr_channel_worker_ctx_get(ch);
rad_assert(ms != NULL);
fr_message_set_gc(ms);
talloc_free(ms);
worker->channel[i] = NULL;
rad_assert(worker->num_channels > 0);
worker->num_channels--;
ok = true;
break;
}
fr_cond_assert(ok);
break;
}
}
/** Service a control-plane event.
*
* @param[in] kq the kq to service
* @param[in] kev the kevent to service
* @param[in] ctx the fr_worker_t
*/
static void fr_worker_evfilt_user(UNUSED int kq, UNUSED struct kevent const *kev, void *ctx)
{
fr_time_t now;
fr_worker_t *worker = ctx;
char data[256];
talloc_get_type_abort(worker, fr_worker_t);
now = fr_time();
/*
* Service all available control-plane events
*/
fr_control_service(worker->control, data, sizeof(data), now);
}
/** Send a NAK to the network thread
*
* The network thread believes that a worker is running a request until that request has been NAK'd.
*
* @param[in] worker the worker
* @param[in] cd the message to NAK
* @param[in] now when the message is NAKd
*/
static void fr_worker_nak(fr_worker_t *worker, fr_channel_data_t *cd, fr_time_t now)
{
size_t size;
fr_channel_data_t *reply;
fr_channel_t *ch;
fr_message_set_t *ms;
fr_listen_t const *listen;
worker->num_timeouts++;
/*
* Cache the outbound channel. We'll need it later.
*/
ch = cd->channel.ch;
listen = cd->listen;
ms = fr_channel_worker_ctx_get(ch);
rad_assert(ms != NULL);
size = listen->app_io->default_reply_size;
if (!size) size = listen->app_io->default_message_size;
/*
* Allocate a default message size.
*/
reply = (fr_channel_data_t *) fr_message_reserve(ms, size);
rad_assert(reply != NULL);
/*
* Encode a NAK
*/
if (listen->app_io->nak) {
size = listen->app_io->nak(listen->app_io_instance, cd->packet_ctx, cd->m.data,
cd->m.data_size, reply->m.data, reply->m.rb_size);
} else {
size = 1; /* rely on them to figure it the heck out */
}
(void) fr_message_alloc(ms, &reply->m, size);
/*
* Fill in the NAK.
*/
reply->m.when = now;
reply->reply.cpu_time = worker->tracking.running;
reply->reply.processing_time = 10; /* @todo - set to something better? */
reply->reply.request_time = cd->m.when;
reply->listen = cd->listen;
reply->packet_ctx = cd->packet_ctx;
/*
* Mark the original message as done.
*/
fr_message_done(&cd->m);
/*
* Send the reply, which also polls the request queue.
*/
if (fr_channel_send_reply(ch, reply, &cd) < 0) {
DEBUG2("\t%sfails sending reply to channel", worker->name);
cd = NULL;
}
worker->num_replies++;
if (cd) (void) fr_worker_drain_input(worker, ch, cd);
}
static void worker_reset_timer(fr_worker_t *worker);
/** Reply to a request
*
* And clean it up.
*
* @param[in] worker the worker
* @param[in] request the request to process
* @param[in] size maximum size of the reply data
*/
static void fr_worker_send_reply(fr_worker_t *worker, REQUEST *request, size_t size)
{
fr_channel_data_t *reply, *cd;
fr_channel_t *ch;
fr_message_set_t *ms;
REQUEST_VERIFY(request);
/*
* If we're sending a reply, then it's no longer runnable.
*/
rad_assert(request->runnable_id < 0);
/*
* If it's a detached request, don't send a real reply.
* Just toss the request.
*/
if (request->async->detached) {
fr_time_tracking_end(&request->async->tracking, fr_time(), &worker->tracking);
goto finished;
}
/*
* Allocate and send the reply.
*/
ch = request->async->channel;
rad_assert(ch != NULL);
ms = fr_channel_worker_ctx_get(ch);
rad_assert(ms != NULL);
reply = (fr_channel_data_t *) fr_message_reserve(ms, size);
rad_assert(reply != NULL);
/*
* Encode it, if required.
*/
if (size) {
ssize_t slen = 0;
fr_listen_t const *listen = request->async->listen;
if (listen->app->encode) {
slen = listen->app->encode(listen->app_instance, request,
reply->m.data, reply->m.rb_size);
} else if (listen->app_io->encode) {
slen = listen->app_io->encode(listen->app_io_instance, request,
reply->m.data, reply->m.rb_size);
}
if (slen < 0) {
DEBUG2("\t%sfails encode", worker->name);
*reply->m.data = 0;
slen = 1;
}
/*
* Resize the buffer to the actual packet size.
*/
cd = (fr_channel_data_t *) fr_message_alloc(ms, &reply->m, slen);
rad_assert(cd == reply);
}
/*
* The request is done. Track that.
*/
fr_time_tracking_end(&request->async->tracking, fr_time(), &worker->tracking);
rad_assert(worker->num_active > 0);
worker->num_active--;
/*
* Nothing to do, delete max_request_time timers.
*/
if (!worker->num_active) {
talloc_const_free(worker->ev_cleanup);
worker->ev_cleanup = NULL;
}
/*
* Fill in the rest of the fields in the channel message.
*
* sequence / ack will be filled in by fr_channel_send_reply()
*/
reply->m.when = request->async->tracking.when;
reply->reply.cpu_time = worker->tracking.running;
reply->reply.processing_time = request->async->tracking.running;
reply->reply.request_time = request->async->recv_time;
reply->listen = request->async->listen;
reply->packet_ctx = request->async->packet_ctx;
RDEBUG("finished request.");
/*
* Send the reply, which also polls the request queue.
*/
if (fr_channel_send_reply(ch, reply, &cd) < 0) {
DEBUG2("\t%sfails sending reply", worker->name);
cd = NULL;
}
worker->num_replies++;
/*
* Drain the incoming TO_WORKER queue. We do this every
* time we're done processing a request.
*/
if (cd) (void) fr_worker_drain_input(worker, ch, cd);
/*
* @todo Use a talloc pool for the request. Clean it up,
* and insert it back into a slab allocator.
*/
if (request->time_order_id >= 0) (void) fr_heap_extract(worker->time_order, request);
if (request->runnable_id >= 0) (void) fr_heap_extract(worker->runnable, request);
finished:
rad_assert(request->time_order_id < 0);
rad_assert(request->runnable_id < 0);
#ifndef NDEBUG
request->async->original_recv_time = NULL;
request->async->el = NULL;
request->async->process = NULL;
fr_dlist_remove(&request->async->tracking.list);
request->async->channel = NULL;
request->async->packet_ctx = NULL;
request->async->listen = NULL;
#endif
DEBUG3("freeing request");
talloc_free(request);
}
/** Tell a request that it's stopped.
*
*/
static void worker_stop_request(fr_worker_t *worker, REQUEST *request, fr_time_t now)
{
fr_time_tracking_resume(&request->async->tracking, now);
(void) request->async->process(request, FR_IO_ACTION_DONE);
/*
* The request is ALWAYS in the time_order list. It MAY
* be in the runnable list, but if not, no worries. It
* MAY be in the dedup list, but if not, no worries.
*/
if (request->time_order_id >= 0) (void) fr_heap_extract(worker->time_order, request);
if (request->runnable_id >= 0) (void) fr_heap_extract(worker->runnable, request);
(void) rbtree_deletebydata(worker->dedup, request);
#ifndef NDEBUG
request->async->process = NULL;
#endif
}
/** Enforce max_request_time
*
* Run periodically, and tries to clean up old requests. In the
* interest of not updating the timer for every packet, the requests
* are given a 1 second leeway.
*
* @param[in] el the event list
* @param[in] when the current time
* @param[in] uctx the fr_worker_t
*/
static void fr_worker_max_request_time(UNUSED fr_event_list_t *el, UNUSED struct timeval *when, void *uctx)
{
fr_time_t now = fr_time();
REQUEST *request;
fr_worker_t *worker = talloc_get_type_abort(uctx, fr_worker_t);
DEBUG2("TIMER - worker max_request_time - %d active requests", worker->num_active);
/*
* Look at the oldest requests, and see if they need to
* be deleted.
*/
while ((request = fr_heap_peek_tail(worker->time_order)) != NULL) {
REQUEST_VERIFY(request);
/*
* Waiting too long, delete it.
*/
RDEBUG("request has reached max_request_time - telling it to stop.");
worker_stop_request(worker, request, now);
/*
* Tell the network side that this request is done.
*/
fr_worker_send_reply(worker, request, 1);
}
/*
* There are still active requests. Reset the timer.
*/
if (worker->num_active) worker_reset_timer(worker);
}
/** See when we next need to service the time_order heap for "too old"
* packets.
*
*/
static void worker_reset_timer(fr_worker_t *worker)
{
struct timeval when;
fr_time_t cleanup;
REQUEST *request;
request = fr_heap_peek_tail(worker->time_order);
if (!request) return;
rad_assert(worker->num_active > 0);
cleanup = worker->max_request_time;
cleanup *= NANOSEC;
cleanup += request->async->recv_time;
fr_time_to_timeval(&when, cleanup);
/*
* Suppress the timer update if it's within 1s of the
* previous one.
*/
if (worker->ev_cleanup) {
if ((cleanup > worker->next_cleanup) &&
(cleanup - worker->next_cleanup) <= NANOSEC) return;
}
worker->next_cleanup = cleanup;
fr_time_to_timeval(&when, cleanup);
DEBUG2("Resetting worker %i cleanup timer to +%ds", worker->max_request_time, fr_schedule_worker_id());
if (fr_event_timer_insert(worker, worker->el, &worker->ev_cleanup,
&when, fr_worker_max_request_time, worker) < 0) {
ERROR("Failed inserting max_request_time timer.");
}
}
/** Check timeouts on the various queues
*
* This function checks and enforces timeouts on the multiple worker
* queues. The high priority events can starve low priority ones.
* When that happens, the low priority events will be in the queues for
* "too long", and will need to be cleaned up.
*
* @param[in] worker the worker
* @param[in] now the current time
*/
static void fr_worker_check_timeouts(fr_worker_t *worker, fr_time_t now)
{
fr_dlist_t *entry;
fr_time_t waiting;
/*
* Check the "localized" queue for old packets.
*
* We check it before the "to_decode" list, so that we
* don't check packets twice.
*/
while ((entry = FR_DLIST_TAIL(worker->localized.list)) != NULL) {
fr_channel_data_t *cd;
cd = fr_ptr_to_type(fr_channel_data_t, request.list, entry);
waiting = now - cd->m.when;
if (waiting < ((worker->max_request_time - 2) * (fr_time_t) NANOSEC)) break;
/*
* Waiting too long, delete it.
*/
WORKER_HEAP_EXTRACT(localized, cd, request.list);
DEBUG3("TIMEOUT: Extracting packet from localized list");
fr_worker_nak(worker, cd, now);
}
/*
* Check the "to_decode" queue for old packets.
*/
while ((entry = FR_DLIST_TAIL(worker->to_decode.list)) != NULL) {
fr_message_t *lm;
fr_channel_data_t *cd;
cd = fr_ptr_to_type(fr_channel_data_t, request.list, entry);
waiting = now - cd->m.when;
if (waiting < (NANOSEC / 100)) break;
/*
* Waiting too long, delete it.
*/
if (waiting > NANOSEC) {
WORKER_HEAP_EXTRACT(to_decode, cd, request.list);
DEBUG3("TIMEOUT: Extracting packet from to_decode list");
nak:
fr_worker_nak(worker, cd, now);
continue;
}
/*
* 0.01 to 1s. Localize it.
*/
WORKER_HEAP_EXTRACT(to_decode, cd, request.list);
lm = fr_message_localize(worker, &cd->m, sizeof(*cd));
if (!lm) {
DEBUG3("TIMEOUT: Failed localizing message from to_decode list: %s", fr_strerror());
goto nak;
}
cd = (fr_channel_data_t *) lm;
WORKER_HEAP_INSERT(localized, cd, request.list);
}
}
/** Get a runnable request
*
* @param[in] worker the worker
* @param[in] now the current time
* @return
* - NULL on nothing to run
* - REQUEST the runnable request
*/
static REQUEST *fr_worker_get_request(fr_worker_t *worker, fr_time_t now)
{
bool is_dup;
int ret = -1;
fr_channel_data_t *cd;
REQUEST *request;
fr_listen_t const *listen;
#ifndef HAVE_TALLOC_POOLED_OBJECT
TALLOC_CTX *ctx;
#endif
/*
* Grab a runnable request, and resume it.
*/
request = fr_heap_pop(worker->runnable);
if (request) {
DEBUG3("Worker %i found runnable request", fr_schedule_worker_id());
REQUEST_VERIFY(request);
rad_assert(request->runnable_id < 0);
fr_time_tracking_resume(&request->async->tracking, now);
return request;
}
/*
* Find either a localized message, or one which is in
* the "to_decode" queue.
*/
do {
WORKER_HEAP_POP(localized, cd, request.list);
if (!cd) {
WORKER_HEAP_POP(to_decode, cd, request.list);
}
if (!cd) {
DEBUG3("Worker %i localized and decode lists are empty", fr_schedule_worker_id());
return NULL;
}
DEBUG3("Worker %i found request to decode", fr_schedule_worker_id());
worker->num_decoded++;
} while (!cd);
ctx = request = request_alloc(NULL);
if (!request) goto nak;
request->el = worker->el;
request->backlog = worker->runnable;
request->packet = fr_radius_alloc(request, false);
fr_time_to_timeval(&request->packet->timestamp, *cd->request.recv_time); /* Legacy - Remove once everything looks at request->async */
rad_assert(request->packet != NULL);
request->reply = fr_radius_alloc(request, false);
rad_assert(request->reply != NULL);
request->async = talloc_zero(request, fr_async_t);
request->server_cs = cd->listen->server_cs;
/*
* Receive a message to the worker queue, and decode it
* to a request.
*/
rad_assert(cd->listen != NULL);
/*
* Update the transport-specific fields.
*
* Note that the message "when" time MUST be copied from
* the original recv time. We use "when" here, instead
* of *cd->request.recv_time, on the odd chance that a
* new packet arrived while we were getting around to
* processing this message.
*/
request->async->channel = cd->channel.ch;
request->async->original_recv_time = cd->request.recv_time;
request->async->recv_time = *request->async->original_recv_time;
request->async->el = worker->el;
request->number = worker->number++;
request->name = talloc_typed_asprintf(request, "%" PRIu64 , request->number);
request->async->listen = cd->listen;
request->async->packet_ctx = cd->packet_ctx;
listen = request->async->listen;
/*
* Now that the "request" structure has been initialized, go decode the packet.
*
* Note that this also sets the "async process" function.
*/
if (listen->app->decode) {
ret = listen->app->decode(listen->app_instance, request, cd->m.data, cd->m.data_size);
} else if (listen->app_io->decode) {
ret = listen->app_io->decode(listen->app_io_instance, request, cd->m.data, cd->m.data_size);
}
if (ret < 0) {
talloc_free(ctx);
nak:
fr_worker_nak(worker, cd, now);
return NULL;
}
/*
* Call the main protocol handler to set the right async
* process function.
*/
listen->app->entry_point_set(listen->app_instance, request);
if (!request->async->process) {
RERROR("Protocol failed to set 'process' function");
fr_worker_nak(worker, cd, now);
return NULL;
}
/*
* We're done with this message.
*/
is_dup = cd->request.is_dup;
fr_message_done(&cd->m);
/*
* Look for conflicting / duplicate packets, but only if
* requested to do so.
*/
if (request->async->listen->app_io->track_duplicates) {
REQUEST *old;
old = rbtree_finddata(worker->dedup, request);
if (!old) {
/*
* Ignore duplicate packets where we've
* already sent the reply.
*/
if (is_dup) {
RDEBUG("Got duplicate packet notice after we had sent a reply - ignoring");
fr_channel_null_reply(request->async->channel);
return NULL;
}
goto insert_new;
}
rad_assert(old->async->listen == request->async->listen);
rad_assert(old->async->channel == request->async->channel);
/*
* There's a new packet. Do we keep the old one,
* or the new one? This decision is made by
* checking the recv_time, which is a
* nanosecond-resolution timer. If the time is
* identical, then the new packet is the same as
* the old one.
*
* If the new packet is a duplicate of the old
* one, then we can just discard the new one. We
* have to tell the channel that we've "eaten"
* this reply, so the sequence number should
* increase.
*
* @todo - fix the channel code to do queue
* depth, and not sequence / ack.
*/
if (old->async->recv_time == request->async->recv_time) {
RWARN("Discarding duplicate of request (%"PRIu64")", old->number);
fr_channel_null_reply(request->async->channel);
talloc_free(request);
/*
* Signal there's a dup, and ignore the
* return code. We don't bother replying
* here, as an FD event or timer will
* wake up the request, and cause it to
* continue.
*
* @todo - the old request is NOT
* running, but is yielded. It MAY clean
* itself up, or do something...
*/
(void) old->async->process(old, FR_IO_ACTION_DUP);
return NULL;
}
/*
* Stop the old request, and decrement the number
* of active requests.
*/
RWARN("Got duplicate of request (%" PRIu64 "), telling old request to stop", old->number);
worker_stop_request(worker, old, now);
rad_assert(worker->num_active > 0);
worker->num_active--;
talloc_free(old);
insert_new:
(void) rbtree_insert(worker->dedup, request);
}
/*
* New requests are inserted into the time order heap in
* strict time priority. Once they are in the list, they
* are only removed when the request is done / free'd.
*/
rad_assert(request->time_order_id < 0);
(void) fr_heap_insert(worker->time_order, request);
/*
* Bootstrap the async state machine with the initial
* state of the request.
*/
fr_time_tracking_start(&request->async->tracking, now);
worker->num_active++;
rad_assert(request->runnable_id < 0);
worker_reset_timer(worker);
return request;
}
/** Run a request
*
* Until it either yields, or is done.
*
* This function is also responsible for sending replies, and
* cleaning up the request.
*
* @param[in] worker the worker
* @param[in] request the request to process
*/
static void fr_worker_run_request(fr_worker_t *worker, REQUEST *request)
{
ssize_t size = 0;
fr_io_final_t final;