-
Notifications
You must be signed in to change notification settings - Fork 44
/
homa_incoming.c
1524 lines (1401 loc) · 46.2 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
// SPDX-License-Identifier: BSD-2-Clause
/* 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_grant.h"
#include "homa_offload.h"
#include "homa_peer.h"
#include "homa_pool.h"
/**
* homa_message_in_init() - Constructor for homa_message_in.
* @rpc: RPC whose msgin structure should be initialized.
* @length: Total number of bytes in message.
* @unsched: The number of unscheduled bytes the sender is planning
* to transmit.
* Return: Zero for successful initialization, or a negative errno
* if rpc->msgin could not be initialized.
*/
int homa_message_in_init(struct homa_rpc *rpc, int length, int unsched)
{
int err;
rpc->msgin.length = length;
skb_queue_head_init(&rpc->msgin.packets);
rpc->msgin.recv_end = 0;
INIT_LIST_HEAD(&rpc->msgin.gaps);
rpc->msgin.bytes_remaining = length;
rpc->msgin.granted = (unsched > length) ? length : unsched;
rpc->msgin.rec_incoming = 0;
atomic_set(&rpc->msgin.rank, -1);
rpc->msgin.priority = 0;
rpc->msgin.resend_all = 0;
rpc->msgin.num_bpages = 0;
err = homa_pool_allocate(rpc);
if (err != 0)
return err;
if (rpc->msgin.num_bpages == 0) {
/* The RPC is now queued waiting for buffer space, so we're
* going to discard all of its packets.
*/
rpc->msgin.granted = 0;
}
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);
}
return 0;
}
/**
* homa_gap_new() - Create a new gap and add it to a list.
* @next: Add the new gap just before this list element.
* @start: Offset of first byte covered by the gap.
* @end: Offset of byte just after the last one covered by the gap.
* Return: Pointer to the new gap.
*/
struct homa_gap *homa_gap_new(struct list_head *next, int start, int end)
{
struct homa_gap *gap;
gap = kmalloc(sizeof(struct homa_gap), GFP_KERNEL);
gap->start = start;
gap->end = end;
gap->time = get_cycles();
list_add_tail(&gap->links, next);
return gap;
}
/**
* homa_gap_retry() - Send RESEND requests for all of the unreceived
* gaps in a message.
* @rpc: RPC to check; must be locked by caller.
*/
void homa_gap_retry(struct homa_rpc *rpc)
{
struct homa_gap *gap;
struct resend_header resend;
list_for_each_entry(gap, &rpc->msgin.gaps, links) {
resend.offset = htonl(gap->start);
resend.length = htonl(gap->end - gap->start);
resend.priority = rpc->hsk->homa->num_priorities - 1;
tt_record3("homa_gap_retry sending RESEND for id %d, start %d, end %d",
rpc->id, gap->start, gap->end);
homa_xmit_control(RESEND, &resend, sizeof(resend), rpc);
}
}
/**
* 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
* (the packet will either be freed or added to rpc->msgin.packets).
*/
void homa_add_packet(struct homa_rpc *rpc, struct sk_buff *skb)
{
struct data_header *h = (struct data_header *) skb->data;
int start = ntohl(h->seg.offset);
int length = homa_data_len(skb);
int end = start + length;
struct homa_gap *gap, *dummy, *gap2;
if ((start + length) > rpc->msgin.length) {
tt_record3("Packet extended past message end; id %d, offset %d, length %d",
rpc->id, start, length);
goto discard;
}
if (start == rpc->msgin.recv_end) {
/* Common case: packet is sequential. */
rpc->msgin.recv_end += length;
goto keep;
}
if (start > rpc->msgin.recv_end) {
/* Packet creates a new gap. */
homa_gap_new(&rpc->msgin.gaps, rpc->msgin.recv_end, start);
rpc->msgin.recv_end = end;
goto keep;
}
/* Must now check to see if the packet fills in part or all of
* an existing gap.
*/
list_for_each_entry_safe(gap, dummy, &rpc->msgin.gaps, links) {
/* Is packet at the start of this gap? */
if (start <= gap->start) {
if (end <= gap->start)
continue;
if (start < gap->start) {
tt_record4("Packet overlaps gap start: id %d, start %d, end %d, gap_start %d",
rpc->id, start, end, gap->start);
goto discard;
}
if (end > gap->end) {
tt_record4("Packet overlaps gap end: id %d, start %d, end %d, gap_end %d",
rpc->id, start, end, gap->start);
goto discard;
}
gap->start = end;
if (gap->start >= gap->end) {
list_del(&gap->links);
kfree(gap);
}
goto keep;
}
/* Is packet at the end of this gap? BTW, at this point we know
* the packet can't cover the entire gap.
*/
if (end >= gap->end) {
if (start >= gap->end)
continue;
if (end > gap->end) {
tt_record4("Packet overlaps gap end: id %d, start %d, end %d, gap_end %d",
rpc->id, start, end, gap->start);
goto discard;
}
gap->end = start;
goto keep;
}
/* Packet is in the middle of the gap; must split the gap. */
gap2 = homa_gap_new(&gap->links, gap->start, start);
gap2->time = gap->time;
gap->start = end;
goto keep;
}
discard:
if (h->retransmit)
INC_METRIC(resent_discards, 1);
else
INC_METRIC(packet_discards, 1);
tt_record4("homa_add_packet discarding packet for id %d, offset %d, length %d, retransmit %d",
rpc->id, start, length, h->retransmit);
kfree_skb(skb);
return;
keep:
if (h->retransmit)
INC_METRIC(resent_packets_used, 1);
__skb_queue_tail(&rpc->msgin.packets, skb);
rpc->msgin.bytes_remaining -= length;
}
/**
* homa_copy_to_user() - Copy as much data as possible from incoming
* packet buffers to buffers in user space.
* @rpc: RPC for which data should be copied. Must be locked by caller.
* Return: Zero for success or a negative errno if there is an error.
* It is possible for the RPC to be freed while this function
* executes (it releases and reacquires the RPC lock). If that
* happens, -EINVAL will be returned and the state of @rpc
* will be RPC_DEAD.
*/
int homa_copy_to_user(struct homa_rpc *rpc)
{
#ifdef __UNIT_TEST__
#define MAX_SKBS 3
#else
#define MAX_SKBS 20
#endif
struct sk_buff *skbs[MAX_SKBS];
int n = 0; /* Number of filled entries in skbs. */
int error = 0;
int start_offset = 0;
int end_offset = 0;
int i;
__u64 start;
/* Tricky note: we can't hold the RPC lock while we're actually
* copying to user space, because (a) it's illegal to hold a spinlock
* while copying to user space and (b) we'd like for homa_softirq
* to add more packets to the RPC while we're copying these out.
* So, collect a bunch of packets to copy, then release the lock,
* copy them, and reacquire the lock.
*/
while (true) {
struct sk_buff *skb;
if (rpc->state == RPC_DEAD) {
error = -EINVAL;
break;
}
skb = __skb_dequeue(&rpc->msgin.packets);
if (skb != NULL) {
skbs[n] = skb;
n++;
if (n < MAX_SKBS)
continue;
}
if (n == 0)
break;
/* At this point we've collected a batch of packets (or
* run out of packets); copy any available packets out to
* user space.
*/
atomic_or(RPC_COPYING_TO_USER, &rpc->flags);
homa_rpc_unlock(rpc);
tt_record1("starting copy to user space for id %d",
rpc->id);
/* Each iteration of this loop copies out one skb. */
for (i = 0; i < n; i++) {
struct data_header *h = (struct data_header *)
skbs[i]->data;
int offset = ntohl(h->seg.offset);
int pkt_length = homa_data_len(skbs[i]);
int copied = 0;
char *dst;
struct iov_iter iter;
int buf_bytes, chunk_size;
/* Each iteration of this loop copies to one
* user buffer.
*/
while (copied < pkt_length) {
chunk_size = pkt_length - copied;
dst = homa_pool_get_buffer(rpc, offset + copied,
&buf_bytes);
if (buf_bytes < chunk_size) {
if (buf_bytes == 0) {
/* skb has data beyond message
* end?
*/
break;
}
chunk_size = buf_bytes;
}
error = import_ubuf(READ, dst, chunk_size,
&iter);
if (error)
goto free_skbs;
error = skb_copy_datagram_iter(skbs[i],
sizeof(*h) + copied, &iter,
chunk_size);
if (error)
goto free_skbs;
copied += chunk_size;
}
if (end_offset == 0) {
start_offset = offset;
} else if (end_offset != offset) {
tt_record3("copied out bytes %d-%d for id %d",
start_offset, end_offset,
rpc->id);
start_offset = offset;
}
end_offset = offset + pkt_length;
}
free_skbs:
if (end_offset != 0) {
tt_record3("copied out bytes %d-%d for id %d",
start_offset, end_offset, rpc->id);
end_offset = 0;
}
start = get_cycles();
for (i = 0; i < n; i++)
kfree_skb(skbs[i]);
INC_METRIC(skb_free_cycles, get_cycles() - start);
INC_METRIC(skb_frees, n);
tt_record2("finished freeing %d skbs for id %d",
n, rpc->id);
n = 0;
atomic_or(APP_NEEDS_LOCK, &rpc->flags);
homa_rpc_lock(rpc, "homa_copy_to_user");
atomic_andnot(APP_NEEDS_LOCK|RPC_COPYING_TO_USER, &rpc->flags);
if (error)
break;
}
if (error)
tt_record2("homa_copy_to_user returning error %d for id %d",
-error, rpc->id);
return error;
}
/**
* homa_dispatch_pkts() - Top-level function that processes a batch of packets,
* all related to the same RPC.
* @skb: First packet in the batch, linked through skb->next.
* @homa: Overall information about the Homa transport.
*/
void homa_dispatch_pkts(struct sk_buff *skb, struct homa *homa)
{
const struct in6_addr saddr = skb_canonical_ipv6_saddr(skb);
struct data_header *h = (struct data_header *) skb->data;
__u64 id = homa_local_id(h->common.sender_id);
int dport = ntohs(h->common.dport);
struct homa_sock *hsk;
struct homa_rpc *rpc = NULL;
struct sk_buff *next;
#ifdef __UNIT_TEST__
#define MAX_ACKS 2
#else
#define MAX_ACKS 10
#endif
/* Used to collect acks from data packets so we can process them
* all at the end (can't process them inline because that may
* require locking conflicting RPCs). If we run out of space just
* ignore the extra acks; they'll be regenerated later through the
* explicit mechanism.
*/
struct homa_ack acks[MAX_ACKS];
int num_acks = 0;
/* Find the appropriate socket.*/
hsk = homa_sock_find(homa->port_map, dport);
if (!hsk) {
if (skb_is_ipv6(skb))
icmp6_send(skb, ICMPV6_DEST_UNREACH,
ICMPV6_PORT_UNREACH, 0, NULL,
IP6CB(skb));
else
icmp_send(skb, ICMP_DEST_UNREACH,
ICMP_PORT_UNREACH, 0);
tt_record3("Discarding packet(s) for unknown port %u, id %llu, type %d",
dport, homa_local_id(h->common.sender_id),
h->common.type);
while (skb != NULL) {
next = skb->next;
kfree_skb(skb);
skb = next;
}
return;
}
/* Each iteration through the following loop processes one packet. */
for (; skb != NULL; skb = next) {
h = (struct data_header *) skb->data;
next = skb->next;
/* Relinquish the RPC lock temporarily if it's needed
* elsewhere.
*/
if (rpc != NULL) {
int flags = atomic_read(&rpc->flags);
if (flags & APP_NEEDS_LOCK) {
homa_rpc_unlock(rpc);
tt_record2("softirq released lock for id %d, flags 0x%x", rpc->id, flags);
homa_spin(200);
rpc = NULL;
}
}
/* Find and lock the RPC if we haven't already done so. */
if (rpc == NULL) {
if (!homa_is_client(id)) {
/* We are the server for this RPC. */
if (h->common.type == DATA) {
int created;
/* Create a new RPC if one doesn't
* already exist.
*/
rpc = homa_rpc_new_server(hsk, &saddr,
h, &created);
if (IS_ERR(rpc)) {
pr_warn("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->common.sport),
id);
} else {
rpc = homa_find_client_rpc(hsk, id);
}
}
if (unlikely(!rpc)) {
if ((h->common.type != CUTOFFS)
&& (h->common.type != NEED_ACK)
&& (h->common.type != ACK)
&& (h->common.type != RESEND)) {
tt_record4("Discarding packet for unknown RPC, id %u, type %d, peer 0x%x:%d",
id, h->common.type,
tt_addr(saddr),
ntohs(h->common.sport));
if ((h->common.type != GRANT) || homa_is_client(id))
INC_METRIC(unknown_rpcs, 1);
goto discard;
}
} else {
if ((h->common.type == DATA) || (h->common.type == GRANT)
|| (h->common.type == BUSY))
rpc->silent_ticks = 0;
rpc->peer->outstanding_resends = 0;
}
switch (h->common.type) {
case DATA:
if (h->ack.client_id != 0) {
/* Save the ack for processing later, when we
* have released the RPC lock.
*/
if (num_acks < MAX_ACKS) {
acks[num_acks] = h->ack;
num_acks++;
}
}
homa_data_pkt(skb, rpc);
INC_METRIC(packets_received[DATA - DATA], 1);
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);
rpc = NULL;
/* It isn't safe to process more packets once we've
* released the RPC lock (this should never happen).
*/
BUG_ON(next != NULL);
break;
default:
INC_METRIC(unknown_packet_types, 1);
goto discard;
}
continue;
discard:
kfree_skb(skb);
}
if (rpc != NULL)
homa_grant_check_rpc(rpc);
while (num_acks > 0) {
num_acks--;
homa_rpc_acked(hsk, &saddr, &acks[num_acks]);
}
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();
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);
}
}
/**
* 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.
* Must be locked by the caller.
*/
void homa_data_pkt(struct sk_buff *skb, struct homa_rpc *rpc)
{
struct homa *homa = rpc->hsk->homa;
struct data_header *h = (struct data_header *) skb->data;
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) && homa_is_client(rpc->id)) {
if (unlikely(rpc->state != RPC_OUTGOING))
goto discard;
INC_METRIC(responses_received, 1);
rpc->state = RPC_INCOMING;
tt_record2("Incoming message for id %d has %d unscheduled bytes",
rpc->id, ntohl(h->incoming));
if (homa_message_in_init(rpc, ntohl(h->message_length),
ntohl(h->incoming)) != 0)
goto discard;
} else if (rpc->state != RPC_INCOMING) {
/* Must be server; note that homa_rpc_new_server already
* initialized msgin and allocated buffers.
*/
if (unlikely(rpc->msgin.length >= 0))
goto discard;
}
if (rpc->msgin.num_bpages == 0) {
/* Drop packets that arrive when we can't allocate buffer
* space. If we keep them around, packet buffer usage can
* exceed available cache space, resulting in poor
* performance.
*/
tt_record4("Dropping packet because no buffer space available: id %d, offset %d, length %d, old incoming %d",
rpc->id, ntohl(h->seg.offset),
homa_data_len(skb),
rpc->msgin.granted);
INC_METRIC(dropped_data_no_bufs, homa_data_len(skb));
goto discard;
}
homa_add_packet(rpc, skb);
if ((skb_queue_len(&rpc->msgin.packets) != 0)
&& !(atomic_read(&rpc->flags) & RPC_PKTS_READY)) {
atomic_or(RPC_PKTS_READY, &rpc->flags);
homa_sock_lock(rpc->hsk, "homa_data_pkt");
homa_rpc_handoff(rpc);
homa_sock_unlock(rpc->hsk);
}
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);
UNIT_LOG("; ", "homa_data_pkt discarded packet");
}
/**
* 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;
int new_offset = ntohl(h->offset);
tt_record4("processing grant for id %llu, offset %d, priority %d, increment %d",
homa_local_id(h->common.sender_id), ntohl(h->offset),
h->priority, new_offset - rpc->msgout.granted);
if (rpc->state == RPC_OUTGOING) {
if (h->resend_all)
homa_resend_data(rpc, 0, rpc->msgout.next_xmit_offset,
h->priority);
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) && rpc->state != RPC_OUTGOING) {
/* We are the server for this RPC and don't yet have a
* response packet, so just send BUSY.
*/
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_xmit_offset < 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, rpc->msgout.next_xmit_offset,
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_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,
rpc->msgout.next_xmit_offset);
homa_freeze(rpc, RESTART_RPC, "Freezing because of RPC restart, id %d, peer 0x%x");
homa_resend_data(rpc, 0, rpc->msgout.next_xmit_offset,
homa_unsched_priority(rpc->hsk->homa,
rpc->peer, rpc->msgout.length));
goto done;
}
pr_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)
pr_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);
__u64 id = homa_local_id(h->sender_id);
struct ack_header ack;
struct homa_peer *peer;
tt_record1("Received NEED_ACK for id %d", id);
/* Return if it's not safe for the peer to purge its state
* for this RPC (the RPC still exists and we haven't received
* the entire response), or if we can't find peer info.
*/
if ((rpc != NULL) && ((rpc->state != RPC_INCOMING)
|| rpc->msgin.bytes_remaining)) {
tt_record3("NEED_ACK arrived for id %d before message received, state %d, remaining %d",
rpc->id, rpc->state, rpc->msgin.bytes_remaining);
homa_freeze(rpc, NEED_ACK_MISSING_DATA,
"Freezing because NEED_ACK received before message complete, id %d, peer 0x%x");
goto done;
} 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.flags = HOMA_TCP_FLAGS;
ack.common.urgent = htons(HOMA_TCP_URGENT);
ack.common.sender_id = cpu_to_be64(id);
ack.num_acks = htons(homa_peer_get_acks(peer,
HOMA_MAX_ACKS_PER_PKT, 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",
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 but will
* be unlocked here.
*/
void homa_ack_pkt(struct sk_buff *skb, struct homa_sock *hsk,
struct homa_rpc *rpc)
{
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) {
tt_record1("homa_ack_pkt freeing rpc id %d", rpc->id);
homa_rpc_free(rpc);
homa_rpc_unlock(rpc);
}
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_choose_fifo_grant() - This function is invoked occasionally to give
* a high-priority grant to the oldest incoming message. We do this in
* order to reduce the starvation that SRPT can cause for long messages.
* Note: this method is obsolete and should never be invoked; it's code is
* being retained until fifo grants are reimplemented using the new grant
* mechanism.
* @homa: Overall data about the Homa protocol implementation. The
* grantable_lock must be held by the caller.
* Return: An RPC to which to send a FIFO grant, or NULL if there is
* no appropriate RPC. This method doesn't actually send a grant,
* but it updates @msgin.granted to reflect the desired grant.
* Also updates homa->total_incoming.
*/
struct homa_rpc *homa_choose_fifo_grant(struct homa *homa)
{
struct homa_rpc *rpc, *oldest;
__u64 oldest_birth;
int granted;
oldest = NULL;
oldest_birth = ~0;
/* Find the oldest message that doesn't currently have an
* outstanding "pity grant".
*/
list_for_each_entry(rpc, &homa->grantable_rpcs, grantable_links) {
int received, on_the_way;
if (rpc->msgin.birth >= oldest_birth)
continue;
received = (rpc->msgin.length
- rpc->msgin.bytes_remaining);
on_the_way = rpc->msgin.granted - received;
if (on_the_way > homa->unsched_bytes) {
/* The last "pity" grant hasn't been used
* up yet.
*/
continue;
}
oldest = rpc;
oldest_birth = rpc->msgin.birth;
}
if (oldest == NULL)
return NULL;
INC_METRIC(fifo_grants, 1);
if ((oldest->msgin.length - oldest->msgin.bytes_remaining)
== oldest->msgin.granted)
INC_METRIC(fifo_grants_no_incoming, 1);
oldest->silent_ticks = 0;
granted = homa->fifo_grant_increment;
oldest->msgin.granted += granted;
if (oldest->msgin.granted >= oldest->msgin.length) {
granted -= oldest->msgin.granted - oldest->msgin.length;
oldest->msgin.granted = oldest->msgin.length;
// homa_remove_grantable_locked(homa, oldest);
}
/* Try to update homa->total_incoming; if we can't lock
* the RPC, just skip it (waiting could deadlock), and it
* will eventually get updated elsewhere.
*/
if (homa_bucket_try_lock(oldest->bucket, oldest->id,
"homa_choose_fifo_grant")) {
homa_grant_update_incoming(oldest, homa);
homa_rpc_unlock(oldest);
}
if (oldest->msgin.granted < (oldest->msgin.length
- oldest->msgin.bytes_remaining)) {
/* We've already received all of the bytes in the new
* grant; most likely this means that the sender sent extra
* data after the last fifo grant (e.g. by rounding up to a
* TSO packet). Don't send this grant.
*/
return NULL;
}
return oldest;
}
/**
* homa_rpc_abort() - Terminate an RPC.
* @rpc: RPC to be terminated. Must be locked by caller.
* @error: A negative errno value indicating the error that caused the abort.
* If this is a client RPC, the error will be returned to the
* application; if it's a server RPC, the error is ignored and
* we just free the RPC.
*/
void homa_rpc_abort(struct homa_rpc *rpc, int error)
{
if (!homa_is_client(rpc->id)) {
INC_METRIC(server_rpc_discards, 1);
tt_record3("aborting server RPC: peer 0x%x, id %d, error %d",
tt_addr(rpc->peer->addr), rpc->id, error);
homa_rpc_free(rpc);
return;
}
tt_record3("aborting client RPC: peer 0x%x, id %d, error %d",
tt_addr(rpc->peer->addr), rpc->id, error);
rpc->error = error;
homa_sock_lock(rpc->hsk, "homa_rpc_abort");
if (!rpc->hsk->shutdown)
homa_rpc_handoff(rpc);
homa_sock_unlock(rpc->hsk);
}
/**
* homa_abort_rpcs() - Abort all RPCs to/from a particular peer.
* @homa: Overall data about the Homa protocol implementation.
* @addr: Address (network order) of the destination whose RPCs are
* to be aborted.
* @port: If nonzero, then RPCs will only be aborted if they were
* targeted at this server port.
* @error: Negative errno value indicating the reason for the abort.
*/
void homa_abort_rpcs(struct homa *homa, const struct in6_addr *addr,
int port, int error)
{
struct homa_socktab_scan scan;
struct homa_sock *hsk;
struct homa_rpc *rpc, *tmp;
rcu_read_lock();
for (hsk = homa_socktab_start_scan(homa->port_map, &scan);
hsk != NULL; hsk = homa_socktab_next(&scan)) {
/* Skip the (expensive) lock acquisition if there's no
* work to do.
*/
if (list_empty(&hsk->active_rpcs))
continue;
if (!homa_protect_rpcs(hsk))
continue;
list_for_each_entry_safe(rpc, tmp, &hsk->active_rpcs,
active_links) {