-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinterface.c
More file actions
2288 lines (2021 loc) · 67.1 KB
/
interface.c
File metadata and controls
2288 lines (2021 loc) · 67.1 KB
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
/* interface.c */
/*
* Userspace Software iWARP library for DPDK
*
* Authors: Patrick MacArthur <patrick@patrickmacarthur.net>
*
* Copyright (c) 2016, IBM Corporation
* Copyright (c) 2016-2018, University of New Hampshire
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of IBM nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <poll.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <ccan/list/list.h>
#include <rte_config.h>
#include <rte_arp.h>
#include <rte_cycles.h>
#include <rte_eal.h>
#include <rte_errno.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <rte_jhash.h>
#include <rte_log.h>
#include <rte_malloc.h>
#include <rte_mbuf.h>
#include <rte_udp.h>
#include "interface.h"
#include "proto.h"
#include "urdma_kabi.h"
#include "util.h"
#define IP_HDR_PROTO_UDP 17
#define RETRANSMIT_MAX 5
struct packet_context {
struct ee_state *src_ep;
size_t ddp_seg_length;
struct rdmap_packet *rdmap;
uint32_t psn;
};
struct ether_addr ether_bcast = {
.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
};
struct usiw_send_wqe_key {
uint16_t wr_opcode;
uint32_t wr_key_data;
};
struct usiw_recv_wqe_key {
uint32_t msn;
};
/** Compares two 32-bit unsigned integers using the rules in RFC 1982 with
* SERIAL_BITS=32. Returns true if and only if s1 < s2. */
static bool
serial_less_32(uint32_t s1, uint32_t s2)
{
return (s1 < s2 && s2 - s1 < (UINT32_C(1) << 31))
|| (s1 > s2 && s1 - s2 > (UINT32_C(1) << 31));
} /* serial_less_32 */
/** Compares two 32-bit unsigned integers using the rules in RFC 1982 with
* SERIAL_BITS=32. Returns true if and only if s1 > s2. */
static bool
serial_greater_32(uint32_t s1, uint32_t s2)
{
return (s1 < s2 && s2 - s1 > (UINT32_C(1) << 31))
|| (s1 > s2 && s1 - s2 < (UINT32_C(1) << 31));
} /* serial_greater_32 */
struct usiw_mr **
usiw_mr_lookup(struct usiw_mr_table *tbl, uint32_t rkey)
{
struct usiw_mr **candidate;
for (candidate = &tbl->entries[rkey % tbl->capacity];
*candidate != NULL; candidate = &(*candidate)->next) {
if ((*candidate)->mr.rkey == rkey) {
return candidate;
}
}
return NULL;
} /* usiw_mr_lookup */
void
usiw_dereg_mr_real(__attribute__((unused)) struct usiw_mr_table *tbl,
struct usiw_mr **mr)
{
struct usiw_mr *free_mr = *mr;
*mr = (*mr)->next;
free(free_mr);
} /* usiw_dereg_mr_real */
int
usiw_send_wqe_queue_init(uint32_t qpn, struct usiw_send_wqe_queue *q,
uint32_t max_send_wr, uint32_t max_send_sge)
{
size_t wqe_size;
char name[RTE_RING_NAMESIZE];
int i, ret;
snprintf(name, RTE_RING_NAMESIZE, "qpn%" PRIu32 "_send", qpn);
q->ring = rte_malloc(NULL, rte_ring_get_memsize(max_send_wr + 1),
RTE_CACHE_LINE_SIZE);
if (!q->ring)
return -rte_errno;
ret = rte_ring_init(q->ring, name, max_send_wr + 1,
RING_F_SP_ENQ|RING_F_SC_DEQ);
if (ret)
return ret;
snprintf(name, RTE_RING_NAMESIZE, "qpn%" PRIu32 "_send_free", qpn);
q->free_ring = rte_malloc(NULL, rte_ring_get_memsize(max_send_wr + 1),
RTE_CACHE_LINE_SIZE);
if (!q->free_ring)
return -rte_errno;
ret = rte_ring_init(q->free_ring, name, max_send_wr + 1,
RING_F_SP_ENQ|RING_F_SC_DEQ);
if (ret)
return ret;
wqe_size = sizeof(struct usiw_send_wqe)
+ max_send_sge * sizeof(struct iovec);
q->storage = calloc(max_send_wr, wqe_size);
if (!q->storage)
return -errno;
for (i = 0; i < max_send_wr; i++) {
rte_ring_enqueue(q->free_ring, q->storage + i * wqe_size);
}
list_head_init(&q->active_head);
rte_spinlock_init(&q->lock);
q->max_wr = max_send_wr;
q->max_sge = max_send_sge;
return 0;
} /* usiw_send_wqe_queue_init */
void
usiw_send_wqe_queue_destroy(struct usiw_send_wqe_queue *q)
{
rte_free(q->ring);
rte_free(q->free_ring);
free(q->storage);
} /* usiw_send_wqe_queue_destroy */
static void
usiw_send_wqe_queue_add_active(struct usiw_send_wqe_queue *q,
struct usiw_send_wqe *wqe)
{
list_add_tail(&q->active_head, &wqe->active);
} /* usiw_send_wqe_queue_add_active */
static void
usiw_send_wqe_queue_del_active(struct usiw_send_wqe_queue *q,
struct usiw_send_wqe *wqe)
{
list_del(&wqe->active);
} /* usiw_send_wqe_queue_del_active */
static int
usiw_send_wqe_queue_lookup(struct usiw_send_wqe_queue *q,
uint16_t wr_opcode, uint32_t wr_key_data,
struct usiw_send_wqe **wqe)
{
struct usiw_send_wqe *lptr, *next;
RTE_LOG(DEBUG, USER1, "LOOKUP active send WQE opcode=%" PRIu8 " key_data=%" PRIu32 "\n",
wr_opcode, wr_key_data);
list_for_each_safe(&q->active_head, lptr, next, active) {
if (lptr->opcode != wr_opcode) {
continue;
}
switch (lptr->opcode) {
case usiw_wr_send:
if (wr_key_data == lptr->msn) {
*wqe = lptr;
return 0;
}
break;
case usiw_wr_write:
if (wr_key_data == lptr->rkey) {
*wqe = lptr;
return 0;
}
break;
case usiw_wr_read:
if (wr_key_data == lptr->local_stag) {
*wqe = lptr;
return 0;
}
break;
}
}
return -ENOENT;
} /* usiw_send_wqe_queue_lookup */
int
usiw_recv_wqe_queue_init(uint32_t qpn, struct usiw_recv_wqe_queue *q,
uint32_t max_recv_wr, uint32_t max_recv_sge)
{
size_t wqe_size;
char name[RTE_RING_NAMESIZE];
int i, ret;
snprintf(name, RTE_RING_NAMESIZE, "qpn%" PRIu32 "_recv", qpn);
q->ring = rte_malloc(NULL, rte_ring_get_memsize(max_recv_wr + 1),
RTE_CACHE_LINE_SIZE);
if (!q->ring)
return -rte_errno;
ret = rte_ring_init(q->ring, name, max_recv_wr + 1,
RING_F_SP_ENQ|RING_F_SC_DEQ);
if (ret)
return ret;
snprintf(name, RTE_RING_NAMESIZE, "qpn%" PRIu32 "_recv_free", qpn);
q->free_ring = rte_malloc(NULL, rte_ring_get_memsize(max_recv_wr + 1),
RTE_CACHE_LINE_SIZE);
if (!q->free_ring)
return -rte_errno;
ret = rte_ring_init(q->free_ring, name, max_recv_wr + 1,
RING_F_SP_ENQ|RING_F_SC_DEQ);
if (ret)
return ret;
wqe_size = sizeof(struct usiw_recv_wqe)
+ max_recv_sge * sizeof(struct iovec);
q->storage = calloc(max_recv_wr + 1, wqe_size);
if (!q->storage)
return -errno;
for (i = 0; i < max_recv_wr; ++i) {
rte_ring_enqueue(q->free_ring, q->storage + i * wqe_size);
}
list_head_init(&q->active_head);
rte_spinlock_init(&q->lock);
q->max_wr = max_recv_wr;
q->max_sge = max_recv_sge;
q->next_msn = 1;
return 0;
} /* usiw_recv_wqe_queue_init */
void
usiw_recv_wqe_queue_destroy(struct usiw_recv_wqe_queue *q)
{
rte_free(q->ring);
rte_free(q->free_ring);
free(q->storage);
} /* usiw_recv_wqe_queue_destroy */
static void
usiw_recv_wqe_queue_add_active(struct usiw_recv_wqe_queue *q,
struct usiw_recv_wqe *wqe)
{
RTE_LOG(DEBUG, USER1, "ADD active recv WQE msn=%" PRIu32 "\n",
wqe->msn);
list_add_tail(&q->active_head, &wqe->active);
} /* usiw_recv_wqe_queue_add_active */
static void
usiw_recv_wqe_queue_del_active(struct usiw_recv_wqe_queue *q,
struct usiw_recv_wqe *wqe)
{
RTE_LOG(DEBUG, USER1, "DEL active recv WQE msn=%" PRIu32 "\n",
wqe->msn);
list_del(&wqe->active);
} /* usiw_recv_wqe_queue_del_active */
static int
usiw_recv_wqe_queue_lookup(struct usiw_recv_wqe_queue *q,
uint32_t msn, struct usiw_recv_wqe **wqe)
{
struct usiw_recv_wqe *lptr, *next;
RTE_LOG(DEBUG, USER1, "LOOKUP active recv WQE msn=%" PRIu32 "\n",
msn);
list_for_each_safe(&q->active_head, lptr, next, active) {
if (lptr->msn == msn) {
*wqe = lptr;
return 0;
}
}
return -ENOENT;
} /* usiw_recv_wqe_queue_lookup */
/* Transmits all packets currently in the transmit queue. The queue will be
* empty when this function returns.
*
* FIXME: It may be possible for this to never return if there is any error
* that prevents packets from being transmitted. */
static void
flush_tx_queue(struct usiw_qp *qp)
{
struct rte_mbuf **begin;
int ret;
begin = qp->txq;
do {
ret = rte_eth_tx_burst(qp->dev->portid, qp->shm_qp->tx_queue,
begin, qp->txq_end - begin);
if (ret > 0) {
RTE_LOG(DEBUG, USER1, "Transmitted %d packets\n", ret);
}
begin += ret;
} while (begin != qp->txq_end);
qp->txq_end = qp->txq;
} /* flush_tx_queue */
/* Prepends an Ethernet header to the frame and enqueues it on the given port.
* The ether_type should be in host byte order. */
static void
enqueue_ether_frame(struct rte_mbuf *sendmsg, unsigned int ether_type,
struct usiw_qp *qp, struct ether_addr *dst_addr)
{
struct ether_hdr *eth = (struct ether_hdr *)rte_pktmbuf_prepend(sendmsg,
sizeof(*eth));
ether_addr_copy(dst_addr, ð->d_addr);
rte_eth_macaddr_get(qp->dev->portid, ð->s_addr);
eth->ether_type = rte_cpu_to_be_16(ether_type);
sendmsg->l2_len = sizeof(*eth);
#ifdef DEBUG_PACKET_HEADERS
RTE_LOG(DEBUG, USER1, "<dev=%" PRIx16 " qp=%" PRIx16 "> Enqueue packet to transmit queue:\n",
qp->shm_qp->dev_id, qp->shm_qp->qp_id);
rte_pktmbuf_dump(stderr, sendmsg, 128);
#endif
*(qp->txq_end++) = sendmsg;
if (qp->txq_end == qp->txq + qp->shm_qp->tx_burst_size) {
RTE_LOG(DEBUG, USER1, "TX queue filled; early flush forced\n");
flush_tx_queue(qp);
}
} /* enqueue_ether_frame */
/* Appends a skeleton IPv4 header to the packet. src_addr and dst_addr are in
* network byte order. */
static struct ipv4_hdr *
prepend_ipv4_header(struct rte_mbuf *sendmsg, int next_proto_id,
uint32_t src_addr, uint32_t dst_addr)
{
struct ipv4_hdr *ip;
size_t payload_length;
payload_length = rte_pktmbuf_pkt_len(sendmsg);
ip = (struct ipv4_hdr *)rte_pktmbuf_prepend(sendmsg, sizeof(*ip));
ip->version_ihl = 0x45;
ip->type_of_service = 0;
ip->total_length = rte_cpu_to_be_16(sizeof(*ip) + payload_length);
ip->packet_id = 0;
ip->fragment_offset = 0;
ip->time_to_live = 64;
ip->next_proto_id = next_proto_id;
ip->hdr_checksum = 0;
ip->src_addr = src_addr;
ip->dst_addr = dst_addr;
sendmsg->l3_len = sizeof(*ip);
if (!(sendmsg->ol_flags & PKT_TX_IP_CKSUM)) {
ip->hdr_checksum = rte_ipv4_cksum(ip);
}
return ip;
} /* prepend_ipv4_header */
/* Appends a skeleton IPv4 header to the packet. Note that this sets the
* checksum to 0, which must either be computed in full or offloaded (in which
* case the IP psuedo-header checksum must be pre-computed by the caller).
* src_port and dst_port should be in network byte order. */
static struct udp_hdr *
prepend_udp_header(struct rte_mbuf *sendmsg, unsigned int src_port,
unsigned int dst_port)
{
struct udp_hdr *udp;
size_t payload_length;
payload_length = rte_pktmbuf_pkt_len(sendmsg);
udp = (struct udp_hdr *)rte_pktmbuf_prepend(sendmsg, sizeof(*udp));
udp->src_port = src_port;
udp->dst_port = dst_port;
udp->dgram_cksum = 0;
udp->dgram_len = rte_cpu_to_be_16(sizeof(*udp) + payload_length);
sendmsg->l4_len = sizeof(*udp);
return udp;
} /* prepend_udp_header */
/** Adds a UDP datagram to our packet TX queue to be transmitted when the queue
* is next flushed.
*
* @param qp
* The queue pair that is sending this datagram.
* @param sendmsg
* The mbuf containing the datagram to send.
* @param dest
* The address handle of the destination for this datagram.
* @param payload_checksum
* The non-complemented checksum of the packet payload. Ignored if
* checksum_offload is enabled.
*/
static void
send_udp_dgram(struct usiw_qp *qp, struct rte_mbuf *sendmsg,
uint32_t raw_cksum)
{
struct udp_hdr *udp;
struct ipv4_hdr *ip;
if (qp->dev->flags & port_checksum_offload) {
sendmsg->ol_flags
|= PKT_TX_UDP_CKSUM|PKT_TX_IPV4|PKT_TX_IP_CKSUM;
}
udp = prepend_udp_header(sendmsg, qp->shm_qp->local_udp_port,
qp->shm_qp->remote_udp_port);
ip = prepend_ipv4_header(sendmsg, IP_HDR_PROTO_UDP,
qp->dev->ipv4_addr,
qp->shm_qp->remote_ipv4_addr);
udp->dgram_cksum = rte_ipv4_phdr_cksum(ip, sendmsg->ol_flags);
if (!(sendmsg->ol_flags & PKT_TX_UDP_CKSUM)) {
raw_cksum += udp->dgram_cksum + udp->src_port
+ udp->dst_port + udp->dgram_len;
/* Add any carry bits into the checksum. */
while (raw_cksum > UINT16_MAX) {
raw_cksum = (raw_cksum >> 16) + (raw_cksum & 0xffff);
}
udp->dgram_cksum = (raw_cksum == UINT16_MAX) ? UINT16_MAX
: ~raw_cksum;
}
enqueue_ether_frame(sendmsg, ETHER_TYPE_IPv4, qp,
&qp->shm_qp->remote_ether_addr);
} /* send_udp_dgram */
static int
resend_ddp_segment(struct usiw_qp *qp, struct rte_mbuf *sendmsg,
struct ee_state *ep)
{
struct pending_datagram_info *info;
struct rte_mbuf *hdr;
struct trp_hdr *trp;
uint32_t payload_raw_cksum = 0;
info = (struct pending_datagram_info *)(sendmsg + 1);
info->next_retransmit = rte_get_timer_cycles()
+ rte_get_timer_hz() / 100;
if (info->transmit_count++ > RETRANSMIT_MAX) {
return -EIO;
}
hdr = rte_pktmbuf_alloc(qp->dev->tx_hdr_mempool);
if (!hdr) {
return -ENOMEM;
}
sendmsg = rte_pktmbuf_clone(sendmsg, sendmsg->pool);
trp = (struct trp_hdr *)rte_pktmbuf_append(hdr, sizeof(*trp));
trp->psn = rte_cpu_to_be_32(info->psn);
trp->ack_psn = rte_cpu_to_be_32(ep->recv_ack_psn);
trp->opcode = rte_cpu_to_be_16(0);
if (!(ep->trp_flags & trp_recv_missing)) {
ep->trp_flags &= ~trp_ack_update;
}
rte_pktmbuf_chain(hdr, sendmsg);
if (!qp->dev->flags & port_checksum_offload) {
payload_raw_cksum = info->ddp_raw_cksum
+ rte_raw_cksum(trp, sizeof(*trp));
}
send_udp_dgram(qp, hdr, payload_raw_cksum);
return 0;
} /* resend_ddp_segment */
static inline struct rte_mbuf **
tx_pending_entry(struct ee_state *ep, uint32_t psn)
{
int index = psn & (ep->tx_pending_size - 1);
return &ep->tx_pending[index];
} /* tx_pending_entry */
static uint32_t
send_ddp_segment(struct usiw_qp *qp, struct rte_mbuf *sendmsg,
struct read_response_state *readresp,
struct usiw_send_wqe *wqe, size_t payload_length)
{
struct pending_datagram_info *pending;
uint32_t psn = qp->remote_ep.send_next_psn++;
pending = (struct pending_datagram_info *)(sendmsg + 1);
pending->wqe = wqe;
pending->readresp = readresp;
pending->transmit_count = 0;
pending->ddp_length = payload_length;
if (!qp->dev->flags & port_checksum_offload) {
pending->ddp_raw_cksum = rte_raw_cksum(
rte_pktmbuf_mtod(sendmsg, void *),
rte_pktmbuf_data_len(sendmsg));
}
pending->psn = psn;
assert(*tx_pending_entry(&qp->remote_ep, psn) == NULL);
*tx_pending_entry(&qp->remote_ep, psn) = sendmsg;
resend_ddp_segment(qp, sendmsg, &qp->remote_ep);
return psn;
} /* send_ddp_segment */
static void
send_trp_sack(struct usiw_qp *qp)
{
struct rte_mbuf *sendmsg;
struct ee_state *ep = &qp->remote_ep;
struct trp_hdr *trp;
assert(ep->trp_flags & trp_recv_missing);
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_hdr_mempool);
trp = (struct trp_hdr *)rte_pktmbuf_append(sendmsg, sizeof(*trp));
trp->psn = rte_cpu_to_be_32(ep->recv_sack_psn.min);
trp->ack_psn = rte_cpu_to_be_32(ep->recv_sack_psn.max);
trp->opcode = rte_cpu_to_be_16(trp_sack);
ep->trp_flags &= ~trp_ack_update;
send_udp_dgram(qp, sendmsg,
(qp->dev->flags & port_checksum_offload)
? 0 : rte_raw_cksum(trp, sizeof(*trp)));
} /* send_trp_sack */
static void
send_trp_fin(struct usiw_qp *qp)
{
struct ee_state *ep = &qp->remote_ep;
struct rte_mbuf *sendmsg;
struct trp_hdr *trp;
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_hdr_mempool);
trp = (struct trp_hdr *)rte_pktmbuf_append(sendmsg, sizeof(*trp));
trp->psn = rte_cpu_to_be_32(ep->send_next_psn);
trp->ack_psn = rte_cpu_to_be_32(ep->recv_ack_psn);
trp->opcode = rte_cpu_to_be_16(trp_fin);
if (!(ep->trp_flags & trp_recv_missing)) {
ep->trp_flags &= ~trp_ack_update;
}
send_udp_dgram(qp, sendmsg,
(qp->dev->flags & port_checksum_offload)
? 0 : rte_raw_cksum(trp, sizeof(*trp)));
/* Force flush of TX queue since we are shutting down, but we still
* need the receiver to get the FIN packet */
flush_tx_queue(qp);
} /* send_trp_fin */
static void
send_trp_ack(struct usiw_qp *qp)
{
struct ee_state *ep = &qp->remote_ep;
struct rte_mbuf *sendmsg;
struct trp_hdr *trp;
assert(!(ep->trp_flags & trp_recv_missing));
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_hdr_mempool);
trp = (struct trp_hdr *)rte_pktmbuf_append(sendmsg, sizeof(*trp));
trp->psn = rte_cpu_to_be_32(ep->send_next_psn);
trp->ack_psn = rte_cpu_to_be_32(ep->recv_ack_psn);
trp->opcode = rte_cpu_to_be_16(0);
ep->trp_flags &= ~trp_ack_update;
send_udp_dgram(qp, sendmsg,
(qp->dev->flags & port_checksum_offload)
? 0 : rte_raw_cksum(trp, sizeof(*trp)));
} /* send_trp_ack */
/** Returns the given send WQE back to the free pool. It is removed from the
* active set if still_active is true. The sq lock MUST be locked when
* calling this function. */
void
qp_free_send_wqe(struct usiw_qp *qp, struct usiw_send_wqe *wqe,
bool still_active)
{
if (still_active) {
usiw_send_wqe_queue_del_active(&qp->sq, wqe);
}
rte_ring_enqueue(qp->sq.free_ring, wqe);
} /* qp_free_send_wqe */
/** Returns the given receive WQE back to the free pool. It is removed from
* the active set if still_in_hash is true. The rq lock MUST be locked when
* calling this function. */
static void
qp_free_recv_wqe(struct usiw_qp *qp, struct usiw_recv_wqe *wqe)
{
usiw_recv_wqe_queue_del_active(&qp->rq0, wqe);
rte_ring_enqueue(qp->rq0.free_ring, wqe);
} /* qp_free_recv_wqe */
int
qp_get_next_send_wqe(struct usiw_qp *qp, struct usiw_send_wqe **wqe)
{
int ret;
rte_spinlock_lock(&qp->sq.lock);
ret = rte_ring_dequeue(qp->sq.free_ring, (void **)wqe);
rte_spinlock_unlock(&qp->sq.lock);
if (ret == -ENOENT)
ret = -ENOSPC;
return ret;
} /* qp_get_next_send_wqe */
int
qp_get_next_recv_wqe(struct usiw_qp *qp, struct usiw_recv_wqe **wqe)
{
int ret;
rte_spinlock_lock(&qp->rq0.lock);
ret = rte_ring_dequeue(qp->rq0.free_ring, (void **)wqe);
rte_spinlock_unlock(&qp->rq0.lock);
if (ret == -ENOENT)
ret = -ENOSPC;
return ret;
} /* qp_get_next_recv_wqe */
/** Retrieves a free CQE from the completion queue. This acquires the lock on
* the CQ, which must be released by calling finish_post_cqe(). */
static int
get_next_cqe(struct usiw_cq *cq, struct usiw_wc **cqe)
{
void *p;
int ret;
ret = rte_ring_dequeue(cq->free_ring, &p);
if (ret < 0) {
*cqe = NULL;
return ret;
}
*cqe = p;
return 0;
} /* get_next_cqe */
/** Places a filled-in CQE into the completion queue. This releases the lock on
* the CQ, which must have been acquired previously via get_next_cqe(). */
static void
finish_post_cqe(struct usiw_cq *cq, struct usiw_wc *cqe)
{
struct urdma_cq_event event;
struct usiw_context *ctx;
ssize_t ret;
ret = rte_ring_enqueue(cq->cqe_ring, cqe);
assert(ret == 0);
ctx = usiw_get_context(cq->ib_cq.context);
assert(ctx != NULL);
if (ctx && atomic_exchange(&cq->notify_flag, false)) {
event.event_type = SIW_EVENT_COMP_POSTED;
event.cq_id = cq->cq_id;
ret = write(ctx->event_fd, &event, sizeof(event));
if (ret < 0) {
RTE_LOG(ERR, USER1, "write to event fd: %s\n",
strerror(errno));
} else if ((size_t)ret < sizeof(event)) {
RTE_LOG(ERR, USER1, "partial write to event fd: %zd/%zu bytes\n",
ret, sizeof(event));
}
}
} /* finish_post_cqe */
/** post_recv_cqe posts a CQE corresponding to a receive WQE, and frees the
* completed WQE. Locking on the CQ ensures that any operation done prior to
* this will be seen by other threads prior to the completion being delivered.
* This ensures that new operations can be posted immediately. */
static int
post_recv_cqe(struct usiw_qp *qp, struct usiw_recv_wqe *wqe,
enum ibv_wc_status status)
{
struct usiw_wc *cqe;
struct usiw_cq *cq;
int ret;
cq = qp->recv_cq;
ret = get_next_cqe(cq, &cqe);
if (ret < 0) {
RTE_LOG(NOTICE, USER1, "Failed to post recv CQE: %s\n",
strerror(-ret));
return ret;
}
cqe->wr_context = wqe->wr_context;
cqe->status = status;
cqe->opcode = IBV_WC_RECV;
cqe->byte_len = wqe->input_size;
cqe->qp_num = qp->ib_qp.qp_num;
qp_free_recv_wqe(qp, wqe);
finish_post_cqe(cq, cqe);
return 0;
} /* post_recv_cqe */
static enum ibv_wc_opcode
get_ibv_send_wc_opcode(enum usiw_send_opcode ours)
{
switch (ours) {
case usiw_wr_send:
return IBV_WC_SEND;
case usiw_wr_write:
return IBV_WC_RDMA_WRITE;
case usiw_wr_read:
return IBV_WC_RDMA_READ;
default:
assert(0);
return -1;
}
} /* get_ibv_send_wc_opcode */
/** post_send_cqe posts a CQE corresponding to a send WQE, and frees the
* completed WQE. Locking on the CQ ensures that any operation done prior to
* this will be seen by other threads prior to the completion being delivered.
* This ensures that new operations can be posted immediately. */
static int
post_send_cqe(struct usiw_qp *qp, struct usiw_send_wqe *wqe,
enum ibv_wc_status status)
{
struct usiw_wc *cqe;
struct usiw_cq *cq;
int ret;
cq = qp->send_cq;
ret = get_next_cqe(cq, &cqe);
if (ret < 0) {
RTE_LOG(NOTICE, USER1, "Failed to post send CQE: %s\n",
strerror(-ret));
return ret;
}
cqe->wr_context = wqe->wr_context;
cqe->status = status;
cqe->opcode = get_ibv_send_wc_opcode(wqe->opcode);
cqe->qp_num = qp->ib_qp.qp_num;
qp_free_send_wqe(qp, wqe, true);
finish_post_cqe(cq, cqe);
return 0;
} /* post_send_cqe */
static void
rq_flush(struct usiw_qp *qp)
{
struct usiw_recv_wqe *wqe, *next;
rte_spinlock_lock(&qp->rq0.lock);
while (rte_ring_dequeue(qp->rq0.ring, (void **)&wqe) == 0) {
wqe->msn = qp->rq0.next_msn++;
usiw_recv_wqe_queue_add_active(&qp->rq0, wqe);
}
list_for_each_safe(&qp->rq0.active_head, wqe, next, active) {
post_recv_cqe(qp, wqe, IBV_WC_WR_FLUSH_ERR);
}
rte_spinlock_unlock(&qp->rq0.lock);
} /* rq_flush */
static void
sq_flush(struct usiw_qp *qp)
{
struct usiw_send_wqe *wqe, *next;
rte_spinlock_lock(&qp->sq.lock);
while (rte_ring_dequeue(qp->sq.ring, (void **)&wqe) == 0) {
usiw_send_wqe_queue_add_active(&qp->sq, wqe);
}
list_for_each_safe(&qp->sq.active_head, wqe, next, active) {
post_send_cqe(qp, wqe, IBV_WC_WR_FLUSH_ERR);
}
rte_spinlock_unlock(&qp->sq.lock);
} /* sq_flush */
static void
memcpy_from_iov(char * restrict dest, size_t dest_size,
const struct iovec * restrict src, size_t iov_count,
size_t offset)
{
unsigned y;
size_t prev, pos, cur;
char *src_iov_base;
pos = 0;
for (y = 0, prev = 0; pos < dest_size && y < iov_count; ++y) {
if (prev <= offset && offset < prev + src[y].iov_len) {
cur = RTE_MIN(prev + src[y].iov_len - offset,
dest_size - pos);
src_iov_base = src[y].iov_base;
rte_memcpy(dest + pos, src_iov_base + offset - prev,
cur);
pos += cur;
offset += cur;
}
prev += src[y].iov_len;
}
} /* memcpy_from_iov */
static void
do_rdmap_send(struct usiw_qp *qp, struct usiw_send_wqe *wqe)
{
struct rdmap_untagged_packet *new_rdmap;
struct rte_mbuf *sendmsg;
unsigned int packet_length;
size_t payload_length;
uint16_t mtu = qp->shm_qp->mtu;
while (wqe->bytes_sent < wqe->total_length
&& serial_less_32(wqe->remote_ep->send_next_psn,
wqe->remote_ep->send_max_psn)) {
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_ddp_mempool);
payload_length = RTE_MIN(mtu, wqe->total_length
- wqe->bytes_sent);
packet_length = RDMAP_UNTAGGED_ALLOC_SIZE(payload_length);
new_rdmap = (struct rdmap_untagged_packet *)rte_pktmbuf_append(
sendmsg, packet_length);
new_rdmap->head.ddp_flags = (wqe->total_length
- wqe->bytes_sent <= mtu)
? DDP_V1_UNTAGGED_LAST_DF
: DDP_V1_UNTAGGED_DF;
new_rdmap->head.rdmap_info = rdmap_opcode_send | RDMAP_V1;
new_rdmap->head.sink_stag = rte_cpu_to_be_32(0);
new_rdmap->qn = rte_cpu_to_be_32(0);
new_rdmap->msn = rte_cpu_to_be_32(wqe->msn);
new_rdmap->mo = rte_cpu_to_be_32(wqe->bytes_sent);
if (wqe->flags & usiw_send_inline) {
memcpy(PAYLOAD_OF(new_rdmap),
(char *)wqe->iov + wqe->bytes_sent,
payload_length);
} else {
memcpy_from_iov(PAYLOAD_OF(new_rdmap), payload_length,
wqe->iov, wqe->iov_count,
wqe->bytes_sent);
}
send_ddp_segment(qp, sendmsg, NULL, wqe, payload_length);
RTE_LOG(DEBUG, USER1, "<dev=%" PRIx16 " qp=%" PRIx16 "> SEND transmit msn=%" PRIu32 " [%zu-%zu]\n",
qp->shm_qp->dev_id, qp->shm_qp->qp_id,
wqe->msn,
wqe->bytes_sent,
wqe->bytes_sent + payload_length);
wqe->bytes_sent += payload_length;
}
if (wqe->bytes_sent == wqe->total_length) {
wqe->state = SEND_WQE_WAIT;
}
} /* do_rdmap_send */
static void
do_rdmap_write(struct usiw_qp *qp, struct usiw_send_wqe *wqe)
{
struct rdmap_tagged_packet *new_rdmap;
struct rte_mbuf *sendmsg;
size_t payload_length;
uint16_t mtu = qp->shm_qp->mtu;
void *payload;
while (wqe->bytes_sent < wqe->total_length
&& serial_less_32(wqe->remote_ep->send_next_psn,
wqe->remote_ep->send_max_psn)) {
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_ddp_mempool);
payload_length = RTE_MIN(mtu, wqe->total_length
- wqe->bytes_sent);
new_rdmap = (struct rdmap_tagged_packet *)rte_pktmbuf_prepend(
sendmsg, sizeof(*new_rdmap));
new_rdmap->head.ddp_flags = (wqe->total_length
- wqe->bytes_sent <= mtu)
? DDP_V1_TAGGED_LAST_DF
: DDP_V1_TAGGED_DF;
new_rdmap->head.rdmap_info = RDMAP_V1 | rdmap_opcode_rdma_write;
new_rdmap->head.sink_stag = rte_cpu_to_be_32(wqe->rkey);
new_rdmap->offset = rte_cpu_to_be_64(wqe->remote_addr
+ wqe->bytes_sent);
payload = rte_pktmbuf_append(sendmsg, payload_length);
if (wqe->flags & usiw_send_inline) {
memcpy(payload, (char *)wqe->iov + wqe->bytes_sent,
payload_length);
} else {
memcpy_from_iov(payload, payload_length,
wqe->iov, wqe->iov_count,
wqe->bytes_sent);
}
send_ddp_segment(qp, sendmsg, NULL, wqe, payload_length);
RTE_LOG(DEBUG, USER1, "<dev=%" PRIx16 " qp=%" PRIx16 "> RDMA WRITE transmit bytes %zu through %zu\n",
qp->shm_qp->dev_id, qp->shm_qp->qp_id,
wqe->bytes_sent,
wqe->bytes_sent + payload_length);
wqe->bytes_sent += payload_length;
}
if (wqe->bytes_sent == wqe->total_length) {
wqe->state = SEND_WQE_WAIT;
}
} /* do_rdmap_write */
static void
do_rdmap_read_request(struct usiw_qp *qp, struct usiw_send_wqe *wqe)
{
struct rdmap_readreq_packet *new_rdmap;
struct rte_mbuf *sendmsg;
unsigned int packet_length;
if (wqe->state != SEND_WQE_TRANSFER) {
return;
}
if (qp->ord_active >= qp->shm_qp->ord_max) {
/* Cannot issue more than ord_max simultaneous RDMA READ
* Requests. */
return;
} else if (wqe->remote_ep->send_next_psn
== wqe->remote_ep->send_max_psn
|| serial_greater_32(wqe->remote_ep->send_next_psn,
wqe->remote_ep->send_max_psn)) {
/* We have reached the maximum number of credits we are allowed
* to send. */
return;
}
qp->ord_active++;
sendmsg = rte_pktmbuf_alloc(qp->dev->tx_ddp_mempool);
packet_length = sizeof(*new_rdmap);
new_rdmap = (struct rdmap_readreq_packet *)rte_pktmbuf_append(
sendmsg, packet_length);
new_rdmap->untagged.head.ddp_flags = DDP_V1_UNTAGGED_LAST_DF;
new_rdmap->untagged.head.rdmap_info
= rdmap_opcode_rdma_read_request | RDMAP_V1;
new_rdmap->untagged.head.sink_stag = rte_cpu_to_be_32(wqe->local_stag);