-
Notifications
You must be signed in to change notification settings - Fork 0
/
homa_incoming.c
1743 lines (1617 loc) · 56 KB
/
homa_incoming.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
/* Copyright (c) 2019-2022 Stanford University
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* This file contains functions that handle incoming Homa messages, including
* both receiving information for those messages and sending grants. */
#include "homa_impl.h"
#include "homa_lcache.h"
/**
* homa_message_in_init() - Constructor for homa_message_in.
* @msgin: Structure to initialize.
* @length: Total number of bytes in message.
* @incoming: The number of unscheduled bytes the sender is planning
* to transmit.
*/
void homa_message_in_init(struct homa_message_in *msgin, int length,
int incoming)
{
msgin->total_length = length;
skb_queue_head_init(&msgin->packets);
msgin->num_skbs = 0;
msgin->bytes_remaining = length;
msgin->incoming = (incoming > length) ? length : incoming;
msgin->priority = 0;
msgin->scheduled = length > incoming;
msgin->xfer_offset = 0;
msgin->xfer_skb = NULL;
if (length < HOMA_NUM_SMALL_COUNTS*64) {
INC_METRIC(small_msg_bytes[(length-1) >> 6], length);
} else if (length < HOMA_NUM_MEDIUM_COUNTS*1024) {
INC_METRIC(medium_msg_bytes[(length-1) >> 10], length);
} else {
INC_METRIC(large_msg_count, 1);
INC_METRIC(large_msg_bytes, length);
}
}
/**
* homa_add_packet() - Add an incoming packet to the contents of a
* partially received message.
* @rpc: Add the packet to the msgin for this RPC.
* @skb: The new packet. This function takes ownership of the packet
* and will free it, if it doesn't get added to msgin (because
* it provides no new data).
*/
void homa_add_packet(struct homa_rpc *rpc, struct sk_buff *skb)
{
struct data_header *h = (struct data_header *) skb->data;
int offset = ntohl(h->seg.offset);
int data_bytes = ntohl(h->seg.segment_length);
struct sk_buff *skb2;
/* Any data from the packet with offset less than this is
* of no value.*/
int floor = 0;
/* Any data with offset >= this is useless. */
int ceiling = rpc->msgin.total_length;
/* Figure out where in the list of existing packets to insert the
* new one. It doesn't necessarily go at the end, but it almost
* always will in practice, so work backwards from the end of the
* list.
*/
skb_queue_reverse_walk(&rpc->msgin.packets, skb2) {
struct data_header *h2 = (struct data_header *) skb2->data;
int offset2 = ntohl(h2->seg.offset);
int data_bytes2 = skb2->len - sizeof32(struct data_header);
if (offset2 < offset) {
floor = offset2 + data_bytes2;
break;
}
ceiling = offset2;
}
/* New packet goes right after skb2 (which may refer to the header).
* Packets shouldn't overlap in byte ranges, but the code below
* assumes they might, so it computes how many non-overlapping bytes
* are contributed by the new packet.
*/
if (unlikely(floor < offset)) {
floor = offset;
}
if (ceiling > offset + data_bytes) {
ceiling = offset + data_bytes;
}
if (floor >= ceiling) {
/* This packet is redundant. */
// char buffer[100];
// printk(KERN_NOTICE "redundant Homa packet: %s\n",
// homa_print_packet(skb, buffer, sizeof(buffer)));
INC_METRIC(redundant_packets, 1);
kfree_skb(skb);
return;
}
if (h->retransmit) {
INC_METRIC(resent_packets_used, 1);
homa_freeze(rpc, PACKET_LOST, "Freezing because of lost "
"packet, id %d, peer 0x%x");
}
__skb_insert(skb, skb2, skb2->next, &rpc->msgin.packets);
rpc->msgin.bytes_remaining -= (ceiling - floor);
rpc->msgin.num_skbs++;
}
/**
* homa_message_in_copy_data() - Extract data from an incoming message
* and copy it to buffer(s) in user space. If one call does not extract
* the entire message, the function can be called again to extract the
* next bytes.
* @msgin: The message whose data should be extracted.
* @iter: Describes the available buffer space at user-level; message
* data gets copied here.
* @max_bytes: Total number of bytes of data to extract.
*
* Return: The number of bytes copied, or a negative errno.
*/
int homa_message_in_copy_data(struct homa_message_in *msgin,
struct iov_iter *iter, int max_bytes)
{
int err;
int remaining = max_bytes;
/* Do the right thing even if packets have overlapping ranges.
* In practice, this shouldn't ever be necessary.
*/
if (msgin->xfer_offset == 0)
msgin->xfer_skb = msgin->packets.next;
skb_queue_walk_from(&msgin->packets, msgin->xfer_skb) {
struct data_header *h =
(struct data_header *) msgin->xfer_skb->data;
int this_offset = ntohl(h->seg.offset);
int this_size = ntohl(h->seg.segment_length);
if (msgin->xfer_offset != this_offset) {
BUG_ON(msgin->xfer_offset < this_offset);
this_size -= (msgin->xfer_offset - this_offset);
}
if (this_size > remaining)
this_size = remaining;
if (unlikely(this_size <= 0))
continue;
err = skb_copy_datagram_iter(msgin->xfer_skb,
sizeof(*h) + (msgin->xfer_offset - this_offset),
iter, this_size);
if (err) {
return err;
}
remaining -= this_size;
msgin->xfer_offset += this_size;
if (remaining == 0) {
break;
}
}
return max_bytes - remaining;
}
/**
* homa_get_resend_range() - Given a message for which some input data
* is missing, find the first range of missing data.
* @msgin: Message for which not all granted data has been received.
* @resend: The @offset and @length fields of this structure will be
* filled in with information about the first missing range
* in @msgin.
*/
void homa_get_resend_range(struct homa_message_in *msgin,
struct resend_header *resend)
{
struct sk_buff *skb;
int missing_bytes;
/* This will eventually be the top of the first missing range. */
int end_offset;
if (msgin->total_length < 0) {
/* Haven't received any data for this message; request
* retransmission of just the first packet (the sender
* will send at least one full packet, regardless of
* the length below).
*/
resend->offset = 0;
resend->length = htonl(100);
return;
}
missing_bytes = msgin->bytes_remaining
- (msgin->total_length - msgin->incoming);
if (missing_bytes == 0) {
resend->offset = 0;
resend->length = 0;
return;
}
end_offset = msgin->incoming;
/* Basic idea: walk backwards through the message's packets until
* we have accounted for all missing bytes; this will identify
* the first missing range.
*/
skb_queue_reverse_walk(&msgin->packets, skb) {
struct data_header *h = (struct data_header *) skb->data;
int offset = ntohl(h->seg.offset);
int pkt_length = ntohl(h->seg.segment_length);
int gap;
if (pkt_length > (msgin->total_length - offset))
pkt_length = msgin->total_length - offset;
gap = end_offset - (offset + pkt_length);
missing_bytes -= gap;
if (missing_bytes == 0) {
resend->offset = htonl(offset + pkt_length);
resend->length = htonl(gap);
return;
}
end_offset = offset;
}
/* The first packet(s) are missing. */
resend->offset = 0;
resend->length = htonl(missing_bytes);
}
/**
* homa_pkt_dispatch() - Top-level function for handling an incoming packet.
* @skb: The incoming packet. This function takes ownership of the
* packet and will ensure that it is eventually freed.
* @hsk: Homa socket that owns the packet's destination port. This socket
* is not locked, but its existence is ensured for the life
* of this method.
* @lcache: Used to manage RPC locks; must be properly initialized by
* the caller, may be modified here.
* @delta: Pointer to a value that will be incremented or decremented
* to accumulate changes that need to be made to
* homa->total_incoming.
*
* Return: None.
*/
void homa_pkt_dispatch(struct sk_buff *skb, struct homa_sock *hsk,
struct homa_lcache *lcache, int *delta)
{
struct common_header *h = (struct common_header *) skb->data;
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
struct homa_rpc *rpc;
__u64 id = homa_local_id(h->sender_id);
/* If there is an ack in the packet, handle it. Must do this
* before locking the packet's RPC, since we may need to acquire
* (other) RPC locks to handle the acks.
*/
if (h->type == DATA) {
struct data_header *dh = (struct data_header *) h;
if (dh->seg.ack.client_id != 0) {
/* homa_rpc_acked may attempt to lock the RPC, so
* make sure we don't have an RPC locked.
*/
homa_lcache_release(lcache);
homa_rpc_acked(hsk, &saddr, &dh->seg.ack);
}
}
/* Find and lock the RPC for this packet. */
rpc = homa_lcache_get(lcache, id, &saddr, ntohs(h->sport));
if (!rpc) {
/* To avoid deadlock, must release old RPC before locking new. */
homa_lcache_release(lcache);
if (!homa_is_client(id)) {
/* We are the server for this RPC. */
if (h->type == DATA) {
/* Create a new RPC if one doesn't already exist. */
rpc = homa_rpc_new_server(hsk, &saddr,
(struct data_header *) h);
if (IS_ERR(rpc)) {
printk(KERN_WARNING "homa_pkt_dispatch "
"couldn't create "
"server rpc: error %lu",
-PTR_ERR(rpc));
INC_METRIC(server_cant_create_rpcs, 1);
rpc = NULL;
goto discard;
}
} else
rpc = homa_find_server_rpc(hsk, &saddr,
ntohs(h->sport), id);
} else {
rpc = homa_find_client_rpc(hsk, id);
}
if (rpc)
homa_lcache_save(lcache, rpc);
}
if (unlikely(!rpc)) {
if ((h->type != CUTOFFS) && (h->type != NEED_ACK)
&& (h->type != ACK) && (h->type != RESEND)) {
tt_record4("Discarding packet for unknown RPC, id %u, "
"type %d, peer 0x%x:%d",
id, h->type,
tt_addr(saddr),
ntohs(h->sport));
if ((h->type != GRANT) || homa_is_client(id))
INC_METRIC(unknown_rpcs, 1);
goto discard;
}
} else {
if ((h->type == DATA) || (h->type == GRANT)
|| (h->type == BUSY))
rpc->silent_ticks = 0;
rpc->peer->outstanding_resends = 0;
}
switch (h->type) {
case DATA:
homa_data_pkt(skb, rpc, lcache, delta);
INC_METRIC(packets_received[DATA - DATA], 1);
if (hsk->dead_skbs >= 2*hsk->homa->dead_buffs_limit) {
/* We get here if neither homa_wait_for_message
* nor homa_timer can keep up with reaping dead
* RPCs. See reap.txt for details.
*/
uint64_t start = get_cycles();
/* Must unlock to avoid self-deadlock in rpc_reap. */
homa_lcache_release(lcache);
rpc = NULL;
tt_record("homa_data_pkt calling homa_rpc_reap");
homa_rpc_reap(hsk, hsk->homa->reap_limit);
INC_METRIC(data_pkt_reap_cycles, get_cycles() - start);
}
break;
case GRANT:
INC_METRIC(packets_received[GRANT - DATA], 1);
homa_grant_pkt(skb, rpc);
break;
case RESEND:
INC_METRIC(packets_received[RESEND - DATA], 1);
homa_resend_pkt(skb, rpc, hsk);
break;
case UNKNOWN:
INC_METRIC(packets_received[UNKNOWN - DATA], 1);
homa_unknown_pkt(skb, rpc);
break;
case BUSY:
INC_METRIC(packets_received[BUSY - DATA], 1);
tt_record2("received BUSY for id %d, peer 0x%x",
id, tt_addr(rpc->peer->addr));
/* Nothing to do for these packets except reset silent_ticks,
* which happened above.
*/
goto discard;
case CUTOFFS:
INC_METRIC(packets_received[CUTOFFS - DATA], 1);
homa_cutoffs_pkt(skb, hsk);
break;
case NEED_ACK:
INC_METRIC(packets_received[NEED_ACK - DATA], 1);
homa_need_ack_pkt(skb, hsk, rpc);
break;
case ACK:
INC_METRIC(packets_received[ACK - DATA], 1);
homa_ack_pkt(skb, hsk, rpc, lcache);
break;
default:
INC_METRIC(unknown_packet_types, 1);
goto discard;
}
return;
discard:
kfree_skb(skb);
}
/**
* homa_data_pkt() - Handler for incoming DATA packets
* @skb: Incoming packet; size known to be large enough for the header.
* This function now owns the packet.
* @rpc: Information about the RPC corresponding to this packet.
* @lcache: @rpc must be stored here; released if needed to unlock @rpc.
* @delta: Pointer to a value that will be incremented or decremented
* to accumulate changes that need to be made to homa->total_incoming.
*
* Return: Zero means the function completed successfully. Nonzero means
* that the RPC had to be unlocked and deleted because the socket has been
* shut down; the caller should not access the RPC anymore. Note: this method
* may change the RPC's state to RPC_READY.
*/
void homa_data_pkt(struct sk_buff *skb, struct homa_rpc *rpc,
struct homa_lcache *lcache, int *delta)
{
struct homa *homa = rpc->hsk->homa;
struct data_header *h = (struct data_header *) skb->data;
int old_remaining;
tt_record4("incoming data packet, id %d, peer 0x%x, offset %d/%d",
homa_local_id(h->common.sender_id),
tt_addr(rpc->peer->addr), ntohl(h->seg.offset),
ntohl(h->message_length));
if (rpc->state != RPC_INCOMING) {
if (homa_is_client(rpc->id)) {
if (unlikely(rpc->state != RPC_OUTGOING))
goto discard;
INC_METRIC(responses_received, 1);
rpc->state = RPC_INCOMING;
} else {
if (unlikely(rpc->msgin.total_length >= 0))
goto discard;
}
}
if (rpc->msgin.total_length < 0) {
/* First data packet for message; initialize. */
homa_message_in_init(&rpc->msgin, ntohl(h->message_length),
ntohl(h->incoming));
*delta += rpc->msgin.incoming;
}
old_remaining = rpc->msgin.bytes_remaining;
homa_add_packet(rpc, skb);
*delta -= old_remaining - rpc->msgin.bytes_remaining;
if (rpc->msgin.bytes_remaining == 0) {
homa_remove_from_grantable(homa, rpc);
homa_sock_lock(rpc->hsk, "homa_data_pkt");
/* This check is needed for the special case where
* homa_rpc_new_server already made the RPC ready.
*/
if (rpc->state == RPC_INCOMING)
homa_rpc_ready(rpc);
homa_sock_unlock(rpc->hsk);
} else if (rpc->msgin.scheduled)
homa_check_grantable(homa, rpc);
if (ntohs(h->cutoff_version) != homa->cutoff_version) {
/* The sender has out-of-date cutoffs. Note: we may need
* to resend CUTOFFS packets if one gets lost, but we don't
* want to send multiple CUTOFFS packets when a stream of
* packets arrives with stale cutoff_versions. Thus, we
* don't send CUTOFFS unless there is a version mismatch
* *and* it is been a while since the previous CUTOFFS
* packet.
*/
if (jiffies != rpc->peer->last_update_jiffies) {
struct cutoffs_header h2;
int i;
for (i = 0; i < HOMA_MAX_PRIORITIES; i++) {
h2.unsched_cutoffs[i] =
htonl(homa->unsched_cutoffs[i]);
}
h2.cutoff_version = htons(homa->cutoff_version);
homa_xmit_control(CUTOFFS, &h2, sizeof(h2), rpc);
rpc->peer->last_update_jiffies = jiffies;
}
}
return;
discard:
kfree_skb(skb);
}
/**
* homa_grant_pkt() - Handler for incoming GRANT packets
* @skb: Incoming packet; size already verified large enough for header.
* This function now owns the packet.
* @rpc: Information about the RPC corresponding to this packet.
*/
void homa_grant_pkt(struct sk_buff *skb, struct homa_rpc *rpc)
{
struct grant_header *h = (struct grant_header *) skb->data;
tt_record3("processing grant for id %llu, offset %d, priority %d",
homa_local_id(h->common.sender_id), ntohl(h->offset),
h->priority);
if (rpc->state == RPC_OUTGOING) {
int new_offset = ntohl(h->offset);
if (new_offset > rpc->msgout.granted) {
rpc->msgout.granted = new_offset;
if (new_offset > rpc->msgout.length)
rpc->msgout.granted = rpc->msgout.length;
}
rpc->msgout.sched_priority = h->priority;
homa_xmit_data(rpc, false);
}
kfree_skb(skb);
}
/**
* homa_resend_pkt() - Handler for incoming RESEND packets
* @skb: Incoming packet; size already verified large enough for header.
* This function now owns the packet.
* @rpc: Information about the RPC corresponding to this packet; must
* be locked by caller, but may be NULL if there is no RPC matching
* this packet
* @hsk: Socket on which the packet was received.
*/
void homa_resend_pkt(struct sk_buff *skb, struct homa_rpc *rpc,
struct homa_sock *hsk)
{
struct resend_header *h = (struct resend_header *) skb->data;
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
struct busy_header busy;
if (rpc == NULL) {
tt_record4("resend request for unknown id %d, peer 0x%x:%d, "
"offset %d; responding with UNKNOWN",
homa_local_id(h->common.sender_id),
tt_addr(saddr), ntohs(h->common.sport),
ntohl(h->offset));
homa_xmit_unknown(skb, hsk);
goto done;
}
tt_record4("resend request for id %llu, offset %d, length %d, prio %d",
rpc->id, ntohl(h->offset), ntohl(h->length),
h->priority);
if (!homa_is_client(rpc->id)) {
/* We are the server for this RPC. */
if (rpc->state != RPC_OUTGOING) {
tt_record2("sending BUSY from resend, id %d, state %d",
rpc->id, rpc->state);
homa_xmit_control(BUSY, &busy, sizeof(busy), rpc);
goto done;
}
}
if (rpc->msgout.next_packet && (homa_data_offset(rpc->msgout.next_packet)
< rpc->msgout.granted)) {
/* We have chosen not to transmit data from this message;
* send BUSY instead.
*/
tt_record3("sending BUSY from resend, id %d, offset %d, "
"granted %d", rpc->id,
homa_data_offset(rpc->msgout.next_packet),
rpc->msgout.granted);
homa_xmit_control(BUSY, &busy, sizeof(busy), rpc);
} else {
if (ntohl(h->length) == 0) {
/* This RESEND is from a server just trying to determine
* whether the client still cares about the RPC; return
* BUSY so the server doesn't time us out.
*/
homa_xmit_control(BUSY, &busy, sizeof(busy), rpc);
}
homa_resend_data(rpc, ntohl(h->offset),
ntohl(h->offset) + ntohl(h->length),
h->priority);
}
done:
kfree_skb(skb);
}
/**
* homa_unknown_pkt() - Handler for incoming UNKNOWN packets.
* @skb: Incoming packet; size known to be large enough for the header.
* This function now owns the packet.
* @rpc: Information about the RPC corresponding to this packet.
*/
void homa_unknown_pkt(struct sk_buff *skb, struct homa_rpc *rpc)
{
tt_record3("Received unknown for id %llu, peer %x:%d",
rpc->id, tt_addr(rpc->peer->addr), rpc->dport);
if (homa_is_client(rpc->id)) {
if ((rpc->state == RPC_READY) || (rpc->state == RPC_DEAD)) {
/* The missing data packets apparently arrived while
* the UNKNOWN was being transmitted.
*/
goto done;
}
if (rpc->state == RPC_OUTGOING) {
/* It appears that everything we've already transmitted
* has been lost; retransmit it.
*/
tt_record4("Restarting id %d to server 0x%x:%d, "
"lost %d bytes",
rpc->id, tt_addr(rpc->peer->addr),
rpc->dport,
homa_rpc_send_offset(rpc));
homa_freeze(rpc, RESTART_RPC, "Freezing because of "
"RPC restart, id %d, peer 0x%x");
homa_resend_data(rpc, 0, homa_rpc_send_offset(rpc),
homa_unsched_priority(rpc->hsk->homa,
rpc->peer, rpc->msgout.length));
goto done;
}
printk(KERN_ERR "Received unknown for RPC id %llu, peer %s:%d "
"in bogus state %d; discarding unknown\n",
rpc->id, homa_print_ipv6_addr(&rpc->peer->addr),
rpc->dport, rpc->state);
tt_record4("Discarding unknown for RPC id %d, peer 0x%x:%d: "
"bad state %d",
rpc->id, tt_addr(rpc->peer->addr), rpc->dport,
rpc->state);
} else {
if (rpc->hsk->homa->verbose)
printk(KERN_NOTICE "Freeing rpc id %llu from client "
"%s:%d: unknown to client",
rpc->id,
homa_print_ipv6_addr(&rpc->peer->addr),
rpc->dport);
homa_rpc_free(rpc);
INC_METRIC(server_rpcs_unknown, 1);
}
done:
kfree_skb(skb);
}
/**
* homa_cutoffs_pkt() - Handler for incoming CUTOFFS packets
* @skb: Incoming packet; size already verified large enough for header.
* This function now owns the packet.
* @hsk: Socket on which the packet was received.
*/
void homa_cutoffs_pkt(struct sk_buff *skb, struct homa_sock *hsk)
{
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
int i;
struct cutoffs_header *h = (struct cutoffs_header *) skb->data;
struct homa_peer *peer = homa_peer_find(&hsk->homa->peers,
&saddr, &hsk->inet);
if (!IS_ERR(peer)) {
peer->unsched_cutoffs[0] = INT_MAX;
for (i = 1; i <HOMA_MAX_PRIORITIES; i++)
peer->unsched_cutoffs[i] = ntohl(h->unsched_cutoffs[i]);
peer->cutoff_version = h->cutoff_version;
}
kfree_skb(skb);
}
/**
* homa_need_ack_pkt() - Handler for incoming NEED_ACK packets
* @skb: Incoming packet; size already verified large enough for header.
* This function now owns the packet.
* @hsk: Socket on which the packet was received.
* @rpc: The RPC named in the packet header, or NULL if no such
* RPC exists. The RPC has been locked by the caller.
*/
void homa_need_ack_pkt(struct sk_buff *skb, struct homa_sock *hsk,
struct homa_rpc *rpc)
{
struct common_header *h = (struct common_header *) skb->data;
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
struct ack_header ack;
struct homa_peer *peer;
/* Return if it's not safe for the peer to purge its state
* for this RPC, or if we can't find peer info.
*/
if (rpc != NULL) {
if (rpc->state != RPC_READY) {
tt_record4("Ignoring NEED_ACK for id %d, peer 0x%x,"
"state %d, bytes_remaining %d",
rpc->id, tt_addr(rpc->peer->addr),
rpc->state, rpc->msgin.bytes_remaining);
goto done;
}
peer = rpc->peer;
} else {
peer = homa_peer_find(&hsk->homa->peers, &saddr, &hsk->inet);
if (IS_ERR(peer))
goto done;
}
/* Send an ACK for this RPC. At the same time, include all of the
* other acks available for the peer. Note: can't use rpc below,
* since it may be NULL.
*/
ack.common.type = ACK;
ack.common.sport = h->dport;
ack.common.dport = h->sport;
ack.common.sender_id = be64_to_cpu(homa_local_id(h->sender_id));
ack.num_acks = htons(homa_peer_get_acks(peer,
NUM_PEER_UNACKED_IDS, ack.acks));
__homa_xmit_control(&ack, sizeof(ack), peer, hsk);
tt_record3("Responded to NEED_ACK for id %d, peer %0x%x with %d "
"other acks", homa_local_id(h->sender_id),
tt_addr(saddr), ntohs(ack.num_acks));
done:
kfree_skb(skb);
}
/**
* homa_ack_pkt() - Handler for incoming ACK packets
* @skb: Incoming packet; size already verified large enough for header.
* This function now owns the packet.
* @hsk: Socket on which the packet was received.
* @rpc: The RPC named in the packet header, or NULL if no such
* RPC exists. The RPC has been locked by the caller and
* recorded in @lcache.
* @lcache: Will be released here to unlock the RPC.
*/
void homa_ack_pkt(struct sk_buff *skb, struct homa_sock *hsk,
struct homa_rpc *rpc, struct homa_lcache *lcache)
{
struct ack_header *h = (struct ack_header *) skb->data;
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
int i, count;
if (rpc != NULL) {
homa_rpc_free(rpc);
homa_lcache_release(lcache);
}
count = ntohs(h->num_acks);
for (i = 0; i < count; i++)
homa_rpc_acked(hsk, &saddr, &h->acks[i]);
tt_record3("ACK received for id %d, peer 0x%x, with %d other acks",
homa_local_id(h->common.sender_id),
tt_addr(saddr), count);
kfree_skb(skb);
}
/**
* homa_check_grantable() - This function ensures that an RPC is on a
* grantable list if appropriate, and not on one otherwise. It also adjusts
* the position of the RPC upward on its list, if needed.
* @homa: Overall data about the Homa protocol implementation.
* @rpc: RPC to check; typically the status of this RPC has changed
* in a way that may affect its grantability (e.g. a packet
* just arrived for it). Must be locked.
*/
void homa_check_grantable(struct homa *homa, struct homa_rpc *rpc)
{
struct homa_rpc *candidate;
struct homa_peer *peer = rpc->peer;
struct homa_peer *peer_cand;
struct homa_message_in *msgin = &rpc->msgin;
/* No need to do anything unless this message is ready for more
* grants.
*/
if (((rpc->msgin.incoming - (rpc->msgin.total_length
- rpc->msgin.bytes_remaining)) >= homa->rtt_bytes)
|| (rpc->msgin.incoming >= rpc->msgin.total_length))
return;
homa_grantable_lock(homa);
/* Note: must check incoming again: it might have changed. */
if ((rpc->state == RPC_DEAD) || (rpc->msgin.incoming
>= rpc->msgin.total_length)) {
homa_grantable_unlock(homa);
return;
}
/* Make sure this message is in the right place in the grantable_rpcs
* list for its peer.
*/
if (list_empty(&rpc->grantable_links)) {
/* Message not yet tracked; add it in priority order to
* the peer's list.
*/
rpc->msgin.birth = get_cycles();
list_for_each_entry(candidate, &peer->grantable_rpcs,
grantable_links) {
if (candidate->msgin.bytes_remaining
> msgin->bytes_remaining) {
list_add_tail(&rpc->grantable_links,
&candidate->grantable_links);
goto position_peer;
}
}
list_add_tail(&rpc->grantable_links, &peer->grantable_rpcs);
} else while (rpc != list_first_entry(&peer->grantable_rpcs,
struct homa_rpc, grantable_links)) {
/* Message is on the list, but its priority may have
* increased because of the recent packet arrival. If so,
* adjust its position in the list.
*/
candidate = list_prev_entry(rpc, grantable_links);
/* Fewer remaining bytes wins: */
if (candidate->msgin.bytes_remaining < msgin->bytes_remaining)
goto position_peer;
/* Tie-breaker: oldest wins */
if (candidate->msgin.bytes_remaining == msgin->bytes_remaining) {
if (candidate->msgin.birth <= msgin->birth) {
goto position_peer;
}
}
__list_del_entry(&candidate->grantable_links);
list_add(&candidate->grantable_links, &rpc->grantable_links);
}
position_peer:
/* At this point rpc is positioned correctly on the list for its peer.
* However, the peer may need to be added to, or moved upward on,
* homa->grantable_peers.
*/
if (list_empty(&peer->grantable_links)) {
/* Must add peer to the overall Homa list. */
homa->num_grantable_peers++;
list_for_each_entry(peer_cand, &homa->grantable_peers,
grantable_links) {
candidate = list_first_entry(&peer_cand->grantable_rpcs,
struct homa_rpc, grantable_links);
if ((candidate->msgin.bytes_remaining
> msgin->bytes_remaining)
|| ((candidate->msgin.bytes_remaining
== msgin->bytes_remaining)
&& (candidate->msgin.birth
> msgin->birth))) {
list_add_tail(&peer->grantable_links,
&peer_cand->grantable_links);
goto done;
}
}
list_add_tail(&peer->grantable_links, &homa->grantable_peers);
goto done;
}
/* The peer is on Homa's list, but it may need to move upward. */
while (peer != list_first_entry(&homa->grantable_peers,
struct homa_peer, grantable_links)) {
struct homa_peer *prev_peer = list_prev_entry(
peer, grantable_links);
candidate = list_first_entry(&prev_peer->grantable_rpcs,
struct homa_rpc, grantable_links);
if ((candidate->msgin.bytes_remaining < msgin->bytes_remaining)
|| ((candidate->msgin.bytes_remaining
== msgin->bytes_remaining)
&& (candidate->msgin.birth <= msgin->birth)))
goto done;
__list_del_entry(&prev_peer->grantable_links);
list_add(&prev_peer->grantable_links, &peer->grantable_links);
}
done:
homa_grantable_unlock(homa);
}
/**
* homa_send_grants() - This function checks to see whether it is
* appropriate to send grants and, if so, it sends them.
* @homa: Overall data about the Homa protocol implementation.
*/
void homa_send_grants(struct homa *homa)
{
/* Some overall design notes:
* - Grant to multiple messages, as long as we can keep
* homa->total_incoming under homa->max_incoming bytes.
* - Ideally, each message should use a different priority level,
* determined by bytes_remaining (fewest bytes_remaining gets the
* highest priority). If there aren't enough scheduled priority
* levels for all of the messages, then the lowest level gets
* shared by multiple messages.
* - If there are fewer messages than priority levels, then we use
* the lowest available levels (new higher-priority messages can
* use the higher levels to achieve instantaneous preemption).
* - We only grant to one message for a given host (there's no
* point in granting to multiple, since the host will only send
* the highest priority one).
*/
struct homa_rpc *candidate;
struct homa_peer *peer, *temp;
int rank, i, window;
__u64 start;
/* The variables below keep track of grants we need to send;
* don't send any until the very end, and release the lock
* first.
*/
#ifdef __UNIT_TEST__
extern int mock_max_grants;
#define MAX_GRANTS mock_max_grants
#else
#define MAX_GRANTS 10
#endif
struct grant_header grants[MAX_GRANTS];
struct homa_rpc *rpcs[MAX_GRANTS];
int num_grants = 0;
/* How many more bytes we can grant before hitting the limit. */
int available = homa->max_incoming - atomic_read(&homa->total_incoming);
/* Total bytes in additional grants that we've given out so far. */
int granted_bytes = 0;
/* Make a local copy of homa->grantable_peers, since that variable
* could change during this function.
*/
int num_grantable_peers = homa->num_grantable_peers;
if ((num_grantable_peers == 0) || (available <= 0)) {
return;
}
/* Compute the window (how much granted-but-not-received data there
* can be for each message. This will always be at least rtt_bytes,
* but if there aren't enough messages to consume all of
* max_incoming, then increase the window size to use it up
* (except, keep rtt_bytes in reserve so we can fully grant
* a new high-priority message).
*/
if (homa->max_grant_window == 0) {
window = homa->rtt_bytes;
} else {
/* Experimental: compute the window (how much granted-but-not-
* received data there can be for any given message. This will
* always be at least rtt_bytes, but if there aren't enough
* messages to consume all of max_incoming, then increase
* the window size to use it up (except, keep rtt_bytes in
* reserve so we can fully grant a new high-priority message).
* This technique is risky because it could use up almost
* all the grants on a single non-responsive host, which
* could result in underutilization of our downlink if that
* host stops responding.
*/
window = (homa->max_incoming
- homa->rtt_bytes)/num_grantable_peers;
if (window > homa->max_grant_window)
window = homa->max_grant_window;
if (window < homa->rtt_bytes)
window = homa->rtt_bytes;
}
start = get_cycles();
homa_grantable_lock(homa);
/* Figure out which messages should receive additional grants. Consider
* only a single (highest-priority) entry for each peer.
*/
rank = 0;
list_for_each_entry_safe(peer, temp, &homa->grantable_peers,
grantable_links) {
int extra_levels, priority;
int received, new_grant, increment;
struct grant_header *grant;
rank++;
candidate = list_first_entry(&peer->grantable_rpcs,
struct homa_rpc, grantable_links);
/* Tricky synchronization issue: homa_data_pkt may be
* updating bytes_remaining while we're working here.
* So, we only read it once, right now, and we only
* make updates to total_incoming based on changes
* to msgin.incoming (not bytes_remaining). homa_data_pkt
* will update total_incoming based on bytes_remaining
* but not incoming.
*/
received = (candidate->msgin.total_length
- candidate->msgin.bytes_remaining);
new_grant = received + window;
if (new_grant > candidate->msgin.total_length)
new_grant = candidate->msgin.total_length;
increment = new_grant - candidate->msgin.incoming;
tt_record3("grant info: id %d, received %d, incoming %d",
candidate->id, received,
candidate->msgin.incoming);
if (increment <= 0)
continue;
if (available <= 0)
break;
if (increment > available) {
increment = available;
new_grant = candidate->msgin.incoming + increment;
}
/* The following line is needed to prevent spurious resends.
* Without it, if the timer fires right after we send the
* grant, it might think the RPC is slow and request a
* resend (until we send the grant, timeouts won't occur
* because there's no granted data).
*/
candidate->silent_ticks = 0;
/* Create a grant for this message. */
candidate->msgin.incoming = new_grant;
granted_bytes += increment;
available -= increment;
homa->grant_nonfifo_left -= increment;
atomic_inc(&candidate->grants_in_progress);
rpcs[num_grants] = candidate;
grant = &grants[num_grants];
num_grants++;
grant->offset = htonl(new_grant);
priority = homa->max_sched_prio - (rank - 1);
extra_levels = homa->max_sched_prio + 1 - num_grantable_peers;
if (extra_levels >= 0)
priority -= extra_levels;
if (priority < 0)
priority = 0;
grant->priority = priority;
tt_record4("sending grant for id %llu, offset %d, priority %d, "
"increment %d",
candidate->id, new_grant, priority, increment);
if (new_grant == candidate->msgin.total_length)
homa_remove_grantable_locked(homa, candidate);
if (num_grants == MAX_GRANTS)