-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
channel.c
925 lines (766 loc) · 24.8 KB
/
channel.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
/*
* 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 Two-way thread-safe channels.
* @file io/channel.c
*
* @copyright 2016 Alan DeKok (aland@freeradius.org)
*/
RCSID("$Id$")
#include <freeradius-devel/io/channel.h>
#include <freeradius-devel/io/control.h>
#include <freeradius-devel/util/log.h>
#include <freeradius-devel/server/rad_assert.h>
/*
* Debugging, mainly for channel_test
*/
#if 0
#define MPRINT(...) fprintf(stdout, __VA_ARGS__)
#else
#define MPRINT(...)
#endif
/*
* We disable this until we fix all of the signaling issues...
*/
#define ENABLE_SKIPS (0)
#define TO_WORKER (0)
#define FROM_WORKER (1)
#if 0
#define SIGNAL_INTERVAL (1000000) //!< The minimum interval between worker signals.
#endif
/** Size of the atomic queues
*
* The queue reader MUST service the queue occasionally,
* otherwise the writer will not be able to write. If it's too
* low, the writer will fail. If it's too high, it will
* unnecessarily use memory. So we're better off putting it on
* the high side.
*
* The reader SHOULD service the queues at inter-packet latency.
* i.e. at 1M pps, the queue will get serviced every microsecond.
*/
#define ATOMIC_QUEUE_SIZE (1024)
typedef enum fr_channel_signal_t {
FR_CHANNEL_SIGNAL_ERROR = FR_CHANNEL_ERROR,
FR_CHANNEL_SIGNAL_DATA_TO_WORKER = FR_CHANNEL_DATA_READY_WORKER,
FR_CHANNEL_SIGNAL_DATA_FROM_WORKER = FR_CHANNEL_DATA_READY_NETWORK,
FR_CHANNEL_SIGNAL_OPEN = FR_CHANNEL_OPEN,
FR_CHANNEL_SIGNAL_CLOSE = FR_CHANNEL_CLOSE,
/*
* The preceding MUST be in the same order as fr_channel_event_t
*/
FR_CHANNEL_SIGNAL_DATA_DONE_WORKER,
FR_CHANNEL_SIGNAL_WORKER_SLEEPING,
} fr_channel_signal_t;
typedef struct {
fr_channel_signal_t signal; //!< the signal to send
uint64_t ack; //!< or the endpoint..
fr_channel_t *ch; //!< the channel
} fr_channel_control_t;
/** One end of a channel
*
* Consists of a kqueue descriptor, and an atomic queue.
* The atomic queue is there to get bulk data through, because it's more efficient
* than pushing 1M+ events per second through a kqueue.
*/
typedef struct {
fr_control_t *control; //!< The control plane, consisting of an atomic queue and kqueue.
fr_ring_buffer_t *rb; //!< Ring buffer for control-plane messages.
void *ctx; //!< Worker context.
fr_channel_recv_callback_t recv; //!< callback for receiving messages
void *recv_ctx; //!< context for receiving messages
int num_outstanding; //!< Number of outstanding requests with no reply.
bool must_signal; //!< we need to signal the other end
size_t num_signals; //!< Number of kevent signals we've sent.
size_t num_resignals; //!< Number of signals resent.
size_t num_kevents; //!< Number of times we've looked at kevents.
uint64_t sequence; //!< Sequence number for this channel.
uint64_t ack; //!< Sequence number of the other end.
uint64_t their_view_of_my_sequence; //!< Should be clear.
uint64_t sequence_at_last_signal; //!< When we last signaled.
uint64_t num_packets; //!< Number of actual data packets.
fr_time_t last_write; //!< Last write to the channel.
fr_time_t last_read_other; //!< Last time we successfully read a message from the other the channe;
fr_time_t message_interval; //!< Interval between messages.
fr_time_t last_sent_signal; //!< The last time when we signaled the other end.
fr_atomic_queue_t *aq; //!< The queue of messages - visible only to this channel.
} fr_channel_end_t;
typedef struct fr_channel_s fr_channel_t;
/** A full channel, which consists of two ends
*
* A channel consists of the kqueue identifiers and an atomic queue in each
* direction to allow for bidirectional communication.
*/
struct fr_channel_s {
fr_time_t cpu_time; //!< Total time used by the worker for this channel.
fr_time_t processing_time; //!< Time spent by the worker processing requests.
bool active; //!< Whether the channel is active.
bool same_thread; //!< are both ends in the same thread?
fr_channel_end_t end[2]; //!< Two ends of the channel.
};
const FR_NAME_NUMBER channel_packet_priority[] = {
{ "now", PRIORITY_NOW },
{ "high", PRIORITY_HIGH },
{ "normal", PRIORITY_NORMAL },
{ "low", PRIORITY_LOW },
{ NULL, -1 }
};
/** Create a new channel
*
* @param[in] ctx The talloc_ctx to allocate channel data in.
* @param[in] master control plane.
* @param[in] worker control plane.
* @param[in] same whether or not the channel is for the same thread
* @return
* - NULL on error
* - channel on success
*/
fr_channel_t *fr_channel_create(TALLOC_CTX *ctx, fr_control_t *master, fr_control_t *worker, bool same)
{
fr_time_t when;
fr_channel_t *ch;
ch = talloc_zero(ctx, fr_channel_t);
if (!ch) {
nomem:
fr_strerror_printf("Failed allocating memory");
return NULL;
}
ch->same_thread = same;
ch->end[TO_WORKER].aq = fr_atomic_queue_create(ch, ATOMIC_QUEUE_SIZE);
if (!ch->end[TO_WORKER].aq) {
talloc_free(ch);
goto nomem;
}
ch->end[FROM_WORKER].aq = fr_atomic_queue_create(ch, ATOMIC_QUEUE_SIZE);
if (!ch->end[FROM_WORKER].aq) {
talloc_free(ch);
goto nomem;
}
ch->end[TO_WORKER].control = worker;
ch->end[FROM_WORKER].control = master;
/*
* Create the ring buffer for the master to send
* control-plane messages to the worker, and vice-versa.
*/
ch->end[TO_WORKER].rb = fr_ring_buffer_create(ch, FR_CONTROL_MAX_MESSAGES * FR_CONTROL_MAX_SIZE);
if (!ch->end[TO_WORKER].rb) {
rb_nomem:
fr_strerror_printf_push("Failed allocating ring buffer");
talloc_free(ch);
return NULL;
}
ch->end[FROM_WORKER].rb = fr_ring_buffer_create(ch, FR_CONTROL_MAX_MESSAGES * FR_CONTROL_MAX_SIZE);
if (!ch->end[FROM_WORKER].rb) {
talloc_free(ch);
goto rb_nomem;
}
/*
* Initialize all of the timers to now.
*/
when = fr_time();
ch->end[TO_WORKER].last_write = when;
ch->end[TO_WORKER].last_read_other = when;
ch->end[TO_WORKER].last_sent_signal = when;
ch->end[FROM_WORKER].last_write = when;
ch->end[FROM_WORKER].last_read_other = when;
ch->end[FROM_WORKER].last_sent_signal = when;
ch->active = true;
return ch;
}
/** Send a message via a kq user signal
*
* Note that the caller doesn't care about data in the event, that is
* sent via the atomic queue. The kevent code takes care of
* delivering the signal once, even if it's sent by multiple master
* threads.
*
* The thread watching the KQ knows which end it is. So when it gets
* the signal (and the channel pointer) it knows to look at end[0] or
* end[1]. We also send which end in 'which' (0, 1) to further help
* the recipient.
*
* @param[in] ch the channel.
* @param[in] when the data was ready. Typically taken from the message.
* @param[in] end of the channel that the message was written to.
* @param[in] which end of the channel (0/1).
* @return
* - <0 on error
* - 0 on success
*/
static int fr_channel_data_ready(fr_channel_t *ch, fr_time_t when, fr_channel_end_t *end, fr_channel_signal_t which)
{
fr_channel_control_t cc;
end->last_sent_signal = when;
end->num_signals++;
end->must_signal = false;
cc.signal = which;
cc.ack = end->ack;
cc.ch = ch;
return fr_control_message_send(end->control, end->rb, FR_CONTROL_ID_CHANNEL, &cc, sizeof(cc));
}
#define IALPHA (8)
#define RTT(_old, _new) ((_new + ((IALPHA - 1) * _old)) / IALPHA)
/** Send a request message into the channel
*
* The message should be initialized, other than "sequence" and "ack".
*
* This function automatically calls the recv_reply callback if there is a reply.
*
* @param[in] ch the channel to send the request on.
* @param[in] cd the message to send.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_send_request(fr_channel_t *ch, fr_channel_data_t *cd)
{
uint64_t sequence;
fr_time_t when, message_interval;
fr_channel_end_t *master;
/*
* Same thread? Just call the "recv" function directly.
*/
if (ch->same_thread) {
ch->end[FROM_WORKER].recv(ch->end[FROM_WORKER].recv_ctx, ch, cd);
return 0;
}
master = &(ch->end[TO_WORKER]);
when = cd->m.when;
sequence = master->sequence + 1;
cd->live.sequence = sequence;
cd->live.ack = master->ack;
/*
* Push the message onto the queue for the other end. If
* the push fails, the caller should try another queue.
*/
if (!fr_atomic_queue_push(master->aq, cd)) {
fr_strerror_printf("Failed pushing to atomic queue");
while (fr_channel_recv_reply(ch)) {
/* do nothing */
}
return -1;
}
master->sequence = sequence;
message_interval = when - master->last_write;
if (!master->message_interval) {
master->message_interval = message_interval;
} else {
master->message_interval = RTT(master->message_interval, message_interval);
}
rad_assert(master->last_write <= when);
master->last_write = when;
master->num_outstanding++;
master->num_packets++;
MPRINT("MASTER requests %zd, num_outstanding %zd\n", master->num_packets, master->num_outstanding);
#if ENABLE_SKIPS
/*
* We just sent the first packet. There can't possibly be a reply, so don't bother looking.
*/
if (master->num_outstanding == 1) {
/*
* There is at least one old packet which is
* outstanding, look for a reply.
*/
} else if (master->num_outstanding > 1) {
while (fr_channel_recv_reply(ch)) {
/* do nothing */
}
/*
* There's no reply yet, so we still have packets outstanding.
* Or, there is a reply, and there are more packets outstanding.
* Skip the signal.
*/
if (!master->must_signal && (!*p_reply || (*p_reply && (master->num_outstanding > 1)))) {
MPRINT("MASTER SKIPS signal\n");
return 0;
}
}
#endif
/*
* Tell the other end that there is new data ready.
*
* Ignore errors on signalling. The worker already has
* the packet in its inbound queue, so at some point, it
* will pick up the message.
*/
MPRINT("MASTER SIGNALS\n");
(void) fr_channel_data_ready(ch, when, master, FR_CHANNEL_SIGNAL_DATA_TO_WORKER);
return 0;
}
/** Receive a reply message from the channel
*
* @param[in] ch the channel to read data from.
* @return
* - true if there was a message received
* - false if there are no more messages
*/
bool fr_channel_recv_reply(fr_channel_t *ch)
{
fr_channel_data_t *cd;
fr_channel_end_t *master;
fr_atomic_queue_t *aq;
rad_assert(ch->end[TO_WORKER].recv != NULL);
aq = ch->end[FROM_WORKER].aq;
master = &(ch->end[TO_WORKER]);
/*
* It's OK for the queue to be empty.
*/
if (!fr_atomic_queue_pop(aq, (void **) &cd)) return false;
/*
* We want an exponential moving average for round trip
* time, where "alpha" is a number between [0,1)
*
* RTT_new = alpha * RTT_old + (1 - alpha) * RTT_sample
*
* BUT we use fixed-point arithmetic, so we need to use inverse alpha,
* which works out to the following equation:
*
* RTT_new = (RTT_sample + (ialpha - 1) * RTT_old) / ialpha
*
* NAKs have zero processing time, so we ignore them for
* the purpose of RTT.
*/
if (cd->reply.processing_time) {
ch->processing_time = RTT(ch->processing_time, cd->reply.processing_time);
}
ch->cpu_time = cd->reply.cpu_time;
/*
* Update the outbound channel with the knowledge that
* we've received one more reply, and with the workers
* ACK.
*/
rad_assert(master->num_outstanding > 0);
rad_assert(cd->live.sequence > master->ack);
rad_assert(cd->live.sequence <= master->sequence); /* must have fewer replies than requests */
master->num_outstanding--;
master->ack = cd->live.sequence;
master->their_view_of_my_sequence = cd->live.ack;
rad_assert(master->last_read_other <= cd->m.when);
master->last_read_other = cd->m.when;
ch->end[TO_WORKER].recv(ch->end[TO_WORKER].recv_ctx, ch, cd);
return true;
}
/** Receive a request message from the channel
*
* @param[in] ch the channel
* @return
* - true if there was a message received
* - false if there are no more messages
*/
bool fr_channel_recv_request(fr_channel_t *ch)
{
fr_channel_data_t *cd;
fr_channel_end_t *worker;
fr_atomic_queue_t *aq;
aq = ch->end[TO_WORKER].aq;
worker = &(ch->end[FROM_WORKER]);
/*
* It's OK for the queue to be empty.
*/
if (!fr_atomic_queue_pop(aq, (void **) &cd)) return false;
rad_assert(cd->live.sequence > worker->ack);
rad_assert(cd->live.sequence >= worker->sequence); /* must have more requests than replies */
worker->num_outstanding++;
worker->ack = cd->live.sequence;
worker->their_view_of_my_sequence = cd->live.ack;
rad_assert(worker->last_read_other <= cd->m.when);
worker->last_read_other = cd->m.when;
ch->end[FROM_WORKER].recv(ch->end[FROM_WORKER].recv_ctx, ch, cd);
return true;
}
/** Send a reply message into the channel
*
* The message should be initialized, other than "sequence" and "ack".
*
* @param[in] ch the channel to send the reply on.
* @param[in] cd the message to send
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_send_reply(fr_channel_t *ch, fr_channel_data_t *cd)
{
uint64_t sequence;
fr_time_t when, message_interval;
fr_channel_end_t *worker;
/*
* Same thread? Just call the "recv" function directly.
*/
if (ch->same_thread) {
ch->end[TO_WORKER].recv(ch->end[TO_WORKER].recv_ctx, ch, cd);
return 0;
}
worker = &(ch->end[FROM_WORKER]);
when = cd->m.when;
sequence = worker->sequence + 1;
cd->live.sequence = sequence;
cd->live.ack = worker->ack;
if (!fr_atomic_queue_push(worker->aq, cd)) {
fr_strerror_printf("Failed pushing to atomic queue");
while (fr_channel_recv_request(ch)) {
/* nothing */
}
return -1;
}
rad_assert(worker->num_outstanding > 0);
worker->num_outstanding--;
worker->num_packets++;
MPRINT("\tWORKER replies %zd, num_outstanding %zd\n", worker->num_packets, worker->num_outstanding);
worker->sequence = sequence;
message_interval = when - worker->last_write;
worker->message_interval = RTT(worker->message_interval, message_interval);
rad_assert(worker->last_write <= when);
worker->last_write = when;
/*
* Even if we think we have no more packets to process,
* the caller may have sent us one. Go check the input
* channel.
*/
while (fr_channel_recv_request(ch)) {
/* nothing */
}
/*
* No packets outstanding, we HAVE to signal the master
* thread.
*/
if (worker->num_outstanding == 0) {
(void) fr_channel_data_ready(ch, when, worker, FR_CHANNEL_SIGNAL_DATA_DONE_WORKER);
return 0;
}
MPRINT("\twhen - last_read_other = %zd - %zd = %zd\n", when, worker->last_read_other, when - worker->last_read_other);
MPRINT("\twhen - last signal = %zd - %zd = %zd\n", when, worker->last_sent_signal, when - worker->last_sent_signal);
MPRINT("\tsequence - ack = %zd - %zd = %zd\n", worker->sequence, worker->their_view_of_my_sequence, worker->sequence - worker->their_view_of_my_sequence);
#ifdef __APPLE__
/*
* If we've sent them a signal since the last ACK, they
* will receive it, and process the packets. So we don't
* need to signal them again.
*
* But... this doesn't appear to work on the Linux
* libkqueue implementation.
*/
if (worker->sequence_at_last_signal > worker->their_view_of_my_sequence) return 0;
#endif
/*
* If we've received a new packet in the last while, OR
* we've sent a signal in the last while, then we don't
* need to send a new signal. But we DO send a signal if
* we haven't seen an ACK for a few packets.
*
* FIXME: make these limits configurable, or include
* predictions about packet processing time?
*/
rad_assert(worker->their_view_of_my_sequence <= worker->sequence);
#if 0
if (((worker->sequence - worker->their_view_of_my_sequence) <= 1000) &&
((when - worker->last_read_other < SIGNAL_INTERVAL) ||
((when - worker->last_sent_signal) < SIGNAL_INTERVAL))) {
MPRINT("\tWORKER SKIPS signal\n");
return 0;
}
#endif
MPRINT("\tWORKER SIGNALS num_outstanding %zd\n", worker->num_outstanding);
(void) fr_channel_data_ready(ch, when, worker, FR_CHANNEL_SIGNAL_DATA_FROM_WORKER);
return 0;
}
/** Don't send a reply message into the channel
*
* The message should be the one we received from the network.
*
* @param[in] ch the channel on which we're dropping a packet
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_null_reply(fr_channel_t *ch)
{
fr_channel_end_t *worker;
worker = &(ch->end[FROM_WORKER]);
worker->sequence++;
return 0;
}
/** Signal a channel that the worker is sleeping
*
* This function should be called from the workers idle loop.
* i.e. only when it has nothing else to do.
*
* @param[in] ch the channel to signal we're no longer listening on.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_worker_sleeping(fr_channel_t *ch)
{
fr_channel_end_t *worker;
fr_channel_control_t cc;
worker = &(ch->end[FROM_WORKER]);
/*
* We don't have any outstanding requests to process for
* this channel, don't signal the network thread that
* we're sleeping. It already knows.
*/
if (worker->num_outstanding == 0) return 0;
worker->num_signals++;
cc.signal = FR_CHANNEL_SIGNAL_WORKER_SLEEPING;
cc.ack = worker->ack;
cc.ch = ch;
MPRINT("\tWORKER SLEEPING num_outstanding %zd, packets in %zd, packets out %zd\n", worker->num_outstanding,
ch->end[TO_WORKER].num_packets, worker->num_packets);
return fr_control_message_send(worker->control, worker->rb, FR_CONTROL_ID_CHANNEL, &cc, sizeof(cc));
}
/** Service a control-plane message
*
* @param[in] when The current time.
* @param[out] p_channel The channel which should be serviced.
* @param[in] data The control message.
* @param[in] data_size The size of the control message.
* @return
* - FR_CHANNEL_ERROR on error
* - FR_CHANNEL_NOOP, on do nothing
* - FR_CHANNEL_DATA_READY on data ready
* - FR_CHANNEL_OPEN when a channel has been opened and sent to us
* - FR_CHANNEL_CLOSE when a channel should be closed
*/
fr_channel_event_t fr_channel_service_message(fr_time_t when, fr_channel_t **p_channel, void const *data, size_t data_size)
{
int rcode;
#if ENABLE_SKIPS
uint64_t ack;
#endif
fr_channel_control_t cc;
fr_channel_signal_t cs;
fr_channel_event_t ce = FR_CHANNEL_ERROR;
fr_channel_end_t *master;
fr_channel_t *ch;
rad_assert(data_size == sizeof(cc));
memcpy(&cc, data, data_size);
cs = cc.signal;
#if ENABLE_SKIPS
ack = cc.ack;
#endif
*p_channel = ch = cc.ch;
switch (cs) {
/*
* These all have the same numbers as the channel
* events, and have no extra processing. We just
* return them as-is.
*/
case FR_CHANNEL_SIGNAL_ERROR:
case FR_CHANNEL_SIGNAL_DATA_TO_WORKER:
case FR_CHANNEL_SIGNAL_DATA_FROM_WORKER:
case FR_CHANNEL_SIGNAL_OPEN:
case FR_CHANNEL_SIGNAL_CLOSE:
MPRINT("channel got %d\n", cs);
return (fr_channel_event_t) cs;
/*
* Only sent by the worker. Both of these
* situations are largely the same, except for
* return codes.
*/
case FR_CHANNEL_SIGNAL_DATA_DONE_WORKER:
MPRINT("channel got data_done_worker\n");
ce = FR_CHANNEL_DATA_READY_NETWORK;
ch->end[TO_WORKER].must_signal = true;
break;
case FR_CHANNEL_SIGNAL_WORKER_SLEEPING:
MPRINT("channel got worker_sleeping\n");
ce = FR_CHANNEL_NOOP;
ch->end[TO_WORKER].must_signal = true;
break;
}
/*
* Compare their ACK to the last sequence we
* sent. If it's different, we signal the worker
* to wake up.
*/
master = &ch->end[TO_WORKER];
#if ENABLE_SKIPS
if (!master->must_signal && (ack == master->sequence)) {
MPRINT("MASTER SKIPS signal AFTER CE %d num_outstanding %zd\n", cs, master->num_outstanding);
MPRINT("MASTER has ack %zd, my seq %zd my_view %zd\n", ack, master->sequence, master->their_view_of_my_sequence);
return ce;
}
/*
* The worker is sleeping or done. There are more
* packets available, so we signal it to wake up again.
*/
rad_assert(ack <= master->sequence);
#endif
/*
* We're signaling it again...
*/
master->num_resignals++;
/*
* The worker hasn't seen our last few packets. Signal
* that there is data ready.
*/
MPRINT("MASTER SIGNALS AFTER CE %d\n", cs);
rcode = fr_channel_data_ready(ch, when, master, FR_CHANNEL_SIGNAL_DATA_TO_WORKER);
if (rcode < 0) return FR_CHANNEL_ERROR;
return ce;
}
/** Service a control-plane event.
*
* The channels use control planes for internal signaling. Note that
* the caller does NOT pass the channel into this function. Instead,
* the channel is taken from the kevent.
*
* @param[in] ch The channel to service.
* @param[in] c The control plane on which we received the kev.
* @param[in] kev The kevent data, should get passed to the control plane.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_service_kevent(fr_channel_t *ch, fr_control_t *c, UNUSED struct kevent const *kev)
{
(void) talloc_get_type_abort(ch, fr_channel_t);
if (c == ch->end[TO_WORKER].control) {
ch->end[TO_WORKER].num_kevents++;
} else {
ch->end[FROM_WORKER].num_kevents++;
}
return 0;
}
/** Check if a channel is active.
*
* A channel may be closed by either end. If so, it stays alive (but
* inactive) until both ends acknowledge the close.
*
* @param[in] ch the channel
* @return
* - false the channel is closing.
* - true the channel is active
*/
bool fr_channel_active(fr_channel_t *ch)
{
return ch->active;
}
/** Signal a worker that the channel is closing
*
* @param[in] ch The channel.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_signal_worker_close(fr_channel_t *ch)
{
fr_channel_control_t cc;
(void) talloc_get_type_abort(ch, fr_channel_t);
ch->active = false;
cc.signal = FR_CHANNEL_SIGNAL_CLOSE;
cc.ack = TO_WORKER;
cc.ch = ch;
return fr_control_message_send(ch->end[TO_WORKER].control, ch->end[TO_WORKER].rb, FR_CONTROL_ID_CHANNEL, &cc, sizeof(cc));
}
/** Acknowledge that the channel is closing
*
* @param[in] ch The channel.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_worker_ack_close(fr_channel_t *ch)
{
fr_channel_control_t cc;
(void) talloc_get_type_abort(ch, fr_channel_t);
ch->active = false;
cc.signal = FR_CHANNEL_SIGNAL_CLOSE;
cc.ack = FROM_WORKER;
cc.ch = ch;
return fr_control_message_send(ch->end[FROM_WORKER].control, ch->end[FROM_WORKER].rb, FR_CONTROL_ID_CHANNEL, &cc, sizeof(cc));
}
/** Add worker-specific data to a channel
*
* @param[in] ch The channel.
* @param[in] ctx The context to add.
*/
void fr_channel_worker_ctx_add(fr_channel_t *ch, void *ctx)
{
(void) talloc_get_type_abort(ch, fr_channel_t);
ch->end[FROM_WORKER].ctx = ctx;
}
/** Get worker-specific data from a channel
*
* @param[in] ch The channel.
*/
void *fr_channel_worker_ctx_get(fr_channel_t *ch)
{
(void) talloc_get_type_abort(ch, fr_channel_t);
return ch->end[FROM_WORKER].ctx;
}
/** Add network-specific data to a channel
*
* @param[in] ch The channel.
* @param[in] ctx The context to add.
*/
void fr_channel_network_ctx_add(fr_channel_t *ch, void *ctx)
{
(void) talloc_get_type_abort(ch, fr_channel_t);
ch->end[TO_WORKER].ctx = ctx;
}
/** Get network-specific data from a channel
*
* @param[in] ch The channel.
*/
void *fr_channel_network_ctx_get(fr_channel_t *ch)
{
(void) talloc_get_type_abort(ch, fr_channel_t);
return ch->end[TO_WORKER].ctx;
}
int fr_channel_set_recv_reply(fr_channel_t *ch, void *ctx, fr_channel_recv_callback_t recv_reply)
{
ch->end[TO_WORKER].recv = recv_reply;
ch->end[TO_WORKER].recv_ctx = ctx;
return 0;
}
int fr_channel_set_recv_request(fr_channel_t *ch, void *ctx, fr_channel_recv_callback_t recv_request)
{
ch->end[FROM_WORKER].recv = recv_request;
ch->end[FROM_WORKER].recv_ctx = ctx;
return 0;
}
/** Send a channel to a worker
*
* @param[in] ch The channel.
* @return
* - <0 on error
* - 0 on success
*/
int fr_channel_signal_open(fr_channel_t *ch)
{
fr_channel_control_t cc;
cc.signal = FR_CHANNEL_SIGNAL_OPEN;
cc.ack = 0;
cc.ch = ch;
return fr_control_message_send(ch->end[TO_WORKER].control, ch->end[TO_WORKER].rb, FR_CONTROL_ID_CHANNEL, &cc, sizeof(cc));
}
void fr_channel_debug(fr_channel_t *ch, FILE *fp)
{
fprintf(fp, "to worker\n");
fprintf(fp, "\tnum_signals sent = %zu\n", ch->end[TO_WORKER].num_signals);
fprintf(fp, "\tnum_signals re-sent = %zu\n", ch->end[TO_WORKER].num_resignals);
fprintf(fp, "\tnum_kevents checked = %zu\n", ch->end[TO_WORKER].num_kevents);
fprintf(fp, "\tsequence = %"PRIu64"\n", ch->end[TO_WORKER].sequence);
fprintf(fp, "\tack = %"PRIu64"\n", ch->end[TO_WORKER].ack);
fprintf(fp, "to receive\n");
fprintf(fp, "\tnum_signals sent = %zu\n", ch->end[FROM_WORKER].num_signals);
fprintf(fp, "\tnum_kevents checked = %zu\n", ch->end[FROM_WORKER].num_kevents);
fprintf(fp, "\tsequence = %"PRIu64"\n", ch->end[FROM_WORKER].sequence);
fprintf(fp, "\tack = %"PRIu64"\n", ch->end[FROM_WORKER].ack);
}