forked from linux-rdma/rdma-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsocket.c
4670 lines (4048 loc) · 108 KB
/
rsocket.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) 2008-2019 Intel Corporation. All rights reserved.
*
* 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
* OpenIB.org 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.
*
* 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.
*
*/
#define _GNU_SOURCE
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <endian.h>
#include <stdarg.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <netinet/tcp.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <search.h>
#include <time.h>
#include <byteswap.h>
#include <util/compiler.h>
#include <util/util.h>
#include <ccan/container_of.h>
#include <rdma/rdma_cma.h>
#include <rdma/rdma_verbs.h>
#include <rdma/rsocket.h>
#include "cma.h"
#include "indexer.h"
#define RS_OLAP_START_SIZE 2048
#define RS_MAX_TRANSFER 65536
#define RS_SNDLOWAT 2048
#define RS_QP_MIN_SIZE 16
#define RS_QP_MAX_SIZE 0xFFFE
#define RS_QP_CTRL_SIZE 4 /* must be power of 2 */
#define RS_CONN_RETRIES 6
#define RS_SGL_SIZE 2
static struct index_map idm;
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t svc_mut = PTHREAD_MUTEX_INITIALIZER;
struct rsocket;
enum {
RS_SVC_NOOP,
RS_SVC_ADD_DGRAM,
RS_SVC_REM_DGRAM,
RS_SVC_ADD_KEEPALIVE,
RS_SVC_REM_KEEPALIVE,
RS_SVC_MOD_KEEPALIVE,
RS_SVC_ADD_CM,
RS_SVC_REM_CM,
};
struct rs_svc_msg {
uint32_t cmd;
uint32_t status;
struct rsocket *rs;
};
struct rs_svc {
pthread_t id;
int sock[2];
int cnt;
int size;
int context_size;
void *(*run)(void *svc);
struct rsocket **rss;
void *contexts;
};
static struct pollfd *udp_svc_fds;
static void *udp_svc_run(void *arg);
static struct rs_svc udp_svc = {
.context_size = sizeof(*udp_svc_fds),
.run = udp_svc_run
};
static uint64_t *tcp_svc_timeouts;
static void *tcp_svc_run(void *arg);
static struct rs_svc tcp_svc = {
.context_size = sizeof(*tcp_svc_timeouts),
.run = tcp_svc_run
};
static void *cm_svc_run(void *arg);
static struct rs_svc listen_svc = {
.context_size = sizeof(struct pollfd),
.run = cm_svc_run
};
static struct rs_svc connect_svc = {
.context_size = sizeof(struct pollfd),
.run = cm_svc_run
};
static uint32_t pollcnt;
static bool suspendpoll;
static int pollsignal = -1;
static uint16_t def_iomap_size = 0;
static uint16_t def_inline = 64;
static uint16_t def_sqsize = 384;
static uint16_t def_rqsize = 384;
static uint32_t def_mem = (1 << 17);
static uint32_t def_wmem = (1 << 17);
static uint32_t polling_time = 10;
static int wake_up_interval = 5000;
/*
* Immediate data format is determined by the upper bits
* bit 31: message type, 0 - data, 1 - control
* bit 30: buffers updated, 0 - target, 1 - direct-receive
* bit 29: more data, 0 - end of transfer, 1 - more data available
*
* for data transfers:
* bits [28:0]: bytes transferred
* for control messages:
* SGL, CTRL
* bits [28-0]: receive credits granted
* IOMAP_SGL
* bits [28-16]: reserved, bits [15-0]: index
*/
enum {
RS_OP_DATA,
RS_OP_RSVD_DATA_MORE,
RS_OP_WRITE, /* opcode is not transmitted over the network */
RS_OP_RSVD_DRA_MORE,
RS_OP_SGL,
RS_OP_RSVD,
RS_OP_IOMAP_SGL,
RS_OP_CTRL
};
#define rs_msg_set(op, data) ((op << 29) | (uint32_t) (data))
#define rs_msg_op(imm_data) (imm_data >> 29)
#define rs_msg_data(imm_data) (imm_data & 0x1FFFFFFF)
#define RS_MSG_SIZE sizeof(uint32_t)
#define RS_WR_ID_FLAG_RECV (((uint64_t) 1) << 63)
#define RS_WR_ID_FLAG_MSG_SEND (((uint64_t) 1) << 62) /* See RS_OPT_MSG_SEND */
#define rs_send_wr_id(data) ((uint64_t) data)
#define rs_recv_wr_id(data) (RS_WR_ID_FLAG_RECV | (uint64_t) data)
#define rs_wr_is_recv(wr_id) (wr_id & RS_WR_ID_FLAG_RECV)
#define rs_wr_is_msg_send(wr_id) (wr_id & RS_WR_ID_FLAG_MSG_SEND)
#define rs_wr_data(wr_id) ((uint32_t) wr_id)
enum {
RS_CTRL_DISCONNECT,
RS_CTRL_KEEPALIVE,
RS_CTRL_SHUTDOWN
};
struct rs_msg {
uint32_t op;
uint32_t data;
};
struct ds_qp;
struct ds_rmsg {
struct ds_qp *qp;
uint32_t offset;
uint32_t length;
};
struct ds_smsg {
struct ds_smsg *next;
};
struct rs_sge {
uint64_t addr;
uint32_t key;
uint32_t length;
};
struct rs_iomap {
uint64_t offset;
struct rs_sge sge;
};
struct rs_iomap_mr {
uint64_t offset;
struct ibv_mr *mr;
dlist_entry entry;
_Atomic(int) refcnt;
int index; /* -1 if mapping is local and not in iomap_list */
};
#define RS_MAX_CTRL_MSG (sizeof(struct rs_sge))
#define rs_host_is_net() (__BYTE_ORDER == __BIG_ENDIAN)
#define RS_CONN_FLAG_NET (1 << 0)
#define RS_CONN_FLAG_IOMAP (1 << 1)
struct rs_conn_data {
uint8_t version;
uint8_t flags;
__be16 credits;
uint8_t reserved[3];
uint8_t target_iomap_size;
struct rs_sge target_sgl;
struct rs_sge data_buf;
};
struct rs_conn_private_data {
union {
struct rs_conn_data conn_data;
struct {
struct ib_connect_hdr ib_hdr;
struct rs_conn_data conn_data;
} af_ib;
};
};
/*
* rsocket states are ordered as passive, connecting, connected, disconnected.
*/
enum rs_state {
rs_init,
rs_bound = 0x0001,
rs_listening = 0x0002,
rs_opening = 0x0004,
rs_resolving_addr = rs_opening | 0x0010,
rs_resolving_route = rs_opening | 0x0020,
rs_connecting = rs_opening | 0x0040,
rs_accepting = rs_opening | 0x0080,
rs_connected = 0x0100,
rs_writable = 0x0200,
rs_readable = 0x0400,
rs_connect_rdwr = rs_connected | rs_readable | rs_writable,
rs_connect_error = 0x0800,
rs_disconnected = 0x1000,
rs_error = 0x2000,
};
#define RS_OPT_SWAP_SGL (1 << 0)
/*
* iWarp does not support RDMA write with immediate data. For iWarp, we
* transfer rsocket messages as inline sends.
*/
#define RS_OPT_MSG_SEND (1 << 1)
#define RS_OPT_UDP_SVC (1 << 2)
#define RS_OPT_KEEPALIVE (1 << 3)
#define RS_OPT_CM_SVC (1 << 4)
union socket_addr {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
};
struct ds_header {
uint8_t version;
uint8_t length;
__be16 port;
union {
__be32 ipv4;
struct {
__be32 flowinfo;
uint8_t addr[16];
} ipv6;
} addr;
};
#define DS_IPV4_HDR_LEN 8
#define DS_IPV6_HDR_LEN 24
struct ds_dest {
union socket_addr addr; /* must be first */
struct ds_qp *qp;
struct ibv_ah *ah;
uint32_t qpn;
};
struct ds_qp {
dlist_entry list;
struct rsocket *rs;
struct rdma_cm_id *cm_id;
struct ds_header hdr;
struct ds_dest dest;
struct ibv_mr *smr;
struct ibv_mr *rmr;
uint8_t *rbuf;
int cq_armed;
};
struct rsocket {
int type;
int index;
fastlock_t slock;
fastlock_t rlock;
fastlock_t cq_lock;
fastlock_t cq_wait_lock;
fastlock_t map_lock; /* acquire slock first if needed */
union {
/* data stream */
struct {
struct rdma_cm_id *cm_id;
uint64_t tcp_opts;
unsigned int keepalive_time;
int accept_queue[2];
unsigned int ctrl_seqno;
unsigned int ctrl_max_seqno;
uint16_t sseq_no;
uint16_t sseq_comp;
uint16_t rseq_no;
uint16_t rseq_comp;
int remote_sge;
struct rs_sge remote_sgl;
struct rs_sge remote_iomap;
struct ibv_mr *target_mr;
int target_sge;
int target_iomap_size;
void *target_buffer_list;
volatile struct rs_sge *target_sgl;
struct rs_iomap *target_iomap;
int rbuf_msg_index;
int rbuf_bytes_avail;
int rbuf_free_offset;
int rbuf_offset;
struct ibv_mr *rmr;
uint8_t *rbuf;
int sbuf_bytes_avail;
struct ibv_mr *smr;
struct ibv_sge ssgl[2];
};
/* datagram */
struct {
struct ds_qp *qp_list;
void *dest_map;
struct ds_dest *conn_dest;
int udp_sock;
int epfd;
int rqe_avail;
struct ds_smsg *smsg_free;
};
};
int opts;
int fd_flags;
uint64_t so_opts;
uint64_t ipv6_opts;
void *optval;
size_t optlen;
int state;
int cq_armed;
int retries;
int err;
int sqe_avail;
uint32_t sbuf_size;
uint16_t sq_size;
uint16_t sq_inline;
uint32_t rbuf_size;
uint16_t rq_size;
int rmsg_head;
int rmsg_tail;
union {
struct rs_msg *rmsg;
struct ds_rmsg *dmsg;
};
uint8_t *sbuf;
struct rs_iomap_mr *remote_iomappings;
dlist_entry iomap_list;
dlist_entry iomap_queue;
int iomap_pending;
int unack_cqe;
};
#define DS_UDP_TAG 0x55555555
struct ds_udp_header {
__be32 tag;
uint8_t version;
uint8_t op;
uint8_t length;
uint8_t reserved;
__be32 qpn; /* lower 8-bits reserved */
union {
__be32 ipv4;
uint8_t ipv6[16];
} addr;
};
#define DS_UDP_IPV4_HDR_LEN 16
#define DS_UDP_IPV6_HDR_LEN 28
#define ds_next_qp(qp) container_of((qp)->list.next, struct ds_qp, list)
static void write_all(int fd, const void *msg, size_t len)
{
// FIXME: if fd is a socket this really needs to handle EINTR and other conditions.
ssize_t __attribute__((unused)) rc = write(fd, msg, len);
assert(rc == len);
}
static void read_all(int fd, void *msg, size_t len)
{
// FIXME: if fd is a socket this really needs to handle EINTR and other conditions.
ssize_t __attribute__((unused)) rc = read(fd, msg, len);
assert(rc == len);
}
static uint64_t rs_time_us(void)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000000 + now.tv_nsec / 1000;
}
static void ds_insert_qp(struct rsocket *rs, struct ds_qp *qp)
{
if (!rs->qp_list)
dlist_init(&qp->list);
else
dlist_insert_head(&qp->list, &rs->qp_list->list);
rs->qp_list = qp;
}
static void ds_remove_qp(struct rsocket *rs, struct ds_qp *qp)
{
if (qp->list.next != &qp->list) {
rs->qp_list = ds_next_qp(qp);
dlist_remove(&qp->list);
} else {
rs->qp_list = NULL;
}
}
static int rs_notify_svc(struct rs_svc *svc, struct rsocket *rs, int cmd)
{
struct rs_svc_msg msg;
int ret;
pthread_mutex_lock(&svc_mut);
if (!svc->cnt) {
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, svc->sock);
if (ret)
goto unlock;
ret = pthread_create(&svc->id, NULL, svc->run, svc);
if (ret) {
ret = ERR(ret);
goto closepair;
}
}
msg.cmd = cmd;
msg.status = EINVAL;
msg.rs = rs;
write_all(svc->sock[0], &msg, sizeof(msg));
read_all(svc->sock[0], &msg, sizeof(msg));
ret = rdma_seterrno(msg.status);
if (svc->cnt)
goto unlock;
pthread_join(svc->id, NULL);
closepair:
close(svc->sock[0]);
close(svc->sock[1]);
unlock:
pthread_mutex_unlock(&svc_mut);
return ret;
}
static int ds_compare_addr(const void *dst1, const void *dst2)
{
const struct sockaddr *sa1, *sa2;
size_t len;
sa1 = (const struct sockaddr *) dst1;
sa2 = (const struct sockaddr *) dst2;
len = (sa1->sa_family == AF_INET6 && sa2->sa_family == AF_INET6) ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
return memcmp(dst1, dst2, len);
}
static int rs_value_to_scale(int value, int bits)
{
return value <= (1 << (bits - 1)) ?
value : (1 << (bits - 1)) | (value >> bits);
}
static int rs_scale_to_value(int value, int bits)
{
return value <= (1 << (bits - 1)) ?
value : (value & ~(1 << (bits - 1))) << bits;
}
/* gcc > ~5 will not allow (void)fscanf to suppress -Wunused-result, but this
will do it. In this case ignoring the result is OK (but horribly
unfriendly to user) since the library has a sane default. */
#define failable_fscanf(f, fmt, ...) \
{ \
int rc = fscanf(f, fmt, __VA_ARGS__); \
(void) rc; \
}
static void rs_configure(void)
{
FILE *f;
static int init;
if (init)
return;
pthread_mutex_lock(&mut);
if (init)
goto out;
if (ucma_init())
goto out;
ucma_ib_init();
if ((f = fopen(RS_CONF_DIR "/polling_time", "r"))) {
failable_fscanf(f, "%u", &polling_time);
fclose(f);
}
f = fopen(RS_CONF_DIR "/wake_up_interval", "r");
if (f) {
failable_fscanf(f, "%d", &wake_up_interval);
fclose(f);
}
if ((f = fopen(RS_CONF_DIR "/inline_default", "r"))) {
failable_fscanf(f, "%hu", &def_inline);
fclose(f);
}
if ((f = fopen(RS_CONF_DIR "/sqsize_default", "r"))) {
failable_fscanf(f, "%hu", &def_sqsize);
fclose(f);
}
if ((f = fopen(RS_CONF_DIR "/rqsize_default", "r"))) {
failable_fscanf(f, "%hu", &def_rqsize);
fclose(f);
}
if ((f = fopen(RS_CONF_DIR "/mem_default", "r"))) {
failable_fscanf(f, "%u", &def_mem);
fclose(f);
if (def_mem < 1)
def_mem = 1;
}
if ((f = fopen(RS_CONF_DIR "/wmem_default", "r"))) {
failable_fscanf(f, "%u", &def_wmem);
fclose(f);
if (def_wmem < RS_SNDLOWAT)
def_wmem = RS_SNDLOWAT << 1;
}
if ((f = fopen(RS_CONF_DIR "/iomap_size", "r"))) {
failable_fscanf(f, "%hu", &def_iomap_size);
fclose(f);
/* round to supported values */
def_iomap_size = (uint8_t) rs_value_to_scale(
(uint16_t) rs_scale_to_value(def_iomap_size, 8), 8);
}
init = 1;
out:
pthread_mutex_unlock(&mut);
}
static int rs_insert(struct rsocket *rs, int index)
{
pthread_mutex_lock(&mut);
rs->index = idm_set(&idm, index, rs);
pthread_mutex_unlock(&mut);
return rs->index;
}
static void rs_remove(struct rsocket *rs)
{
pthread_mutex_lock(&mut);
idm_clear(&idm, rs->index);
pthread_mutex_unlock(&mut);
}
/* We only inherit from listening sockets */
static struct rsocket *rs_alloc(struct rsocket *inherited_rs, int type)
{
struct rsocket *rs;
rs = calloc(1, sizeof(*rs));
if (!rs)
return NULL;
rs->type = type;
rs->index = -1;
if (type == SOCK_DGRAM) {
rs->udp_sock = -1;
rs->epfd = -1;
}
if (inherited_rs) {
rs->sbuf_size = inherited_rs->sbuf_size;
rs->rbuf_size = inherited_rs->rbuf_size;
rs->sq_inline = inherited_rs->sq_inline;
rs->sq_size = inherited_rs->sq_size;
rs->rq_size = inherited_rs->rq_size;
if (type == SOCK_STREAM) {
rs->ctrl_max_seqno = inherited_rs->ctrl_max_seqno;
rs->target_iomap_size = inherited_rs->target_iomap_size;
}
} else {
rs->sbuf_size = def_wmem;
rs->rbuf_size = def_mem;
rs->sq_inline = def_inline;
rs->sq_size = def_sqsize;
rs->rq_size = def_rqsize;
if (type == SOCK_STREAM) {
rs->ctrl_max_seqno = RS_QP_CTRL_SIZE;
rs->target_iomap_size = def_iomap_size;
}
}
fastlock_init(&rs->slock);
fastlock_init(&rs->rlock);
fastlock_init(&rs->cq_lock);
fastlock_init(&rs->cq_wait_lock);
fastlock_init(&rs->map_lock);
dlist_init(&rs->iomap_list);
dlist_init(&rs->iomap_queue);
return rs;
}
static int rs_set_nonblocking(struct rsocket *rs, int arg)
{
struct ds_qp *qp;
int ret = 0;
if (rs->type == SOCK_STREAM) {
if (rs->cm_id->recv_cq_channel)
ret = fcntl(rs->cm_id->recv_cq_channel->fd, F_SETFL, arg);
if (rs->state == rs_listening)
ret = fcntl(rs->accept_queue[0], F_SETFL, arg);
else if (!ret && rs->state < rs_connected)
ret = fcntl(rs->cm_id->channel->fd, F_SETFL, arg);
} else {
ret = fcntl(rs->epfd, F_SETFL, arg);
if (!ret && rs->qp_list) {
qp = rs->qp_list;
do {
ret = fcntl(qp->cm_id->recv_cq_channel->fd,
F_SETFL, arg);
qp = ds_next_qp(qp);
} while (qp != rs->qp_list && !ret);
}
}
return ret;
}
static void rs_set_qp_size(struct rsocket *rs)
{
uint16_t max_size;
max_size = min(ucma_max_qpsize(rs->cm_id), RS_QP_MAX_SIZE);
if (rs->sq_size > max_size)
rs->sq_size = max_size;
else if (rs->sq_size < RS_QP_MIN_SIZE)
rs->sq_size = RS_QP_MIN_SIZE;
if (rs->rq_size > max_size)
rs->rq_size = max_size;
else if (rs->rq_size < RS_QP_MIN_SIZE)
rs->rq_size = RS_QP_MIN_SIZE;
}
static void ds_set_qp_size(struct rsocket *rs)
{
uint16_t max_size;
max_size = min(ucma_max_qpsize(NULL), RS_QP_MAX_SIZE);
if (rs->sq_size > max_size)
rs->sq_size = max_size;
if (rs->rq_size > max_size)
rs->rq_size = max_size;
if (rs->rq_size > (rs->rbuf_size / RS_SNDLOWAT))
rs->rq_size = rs->rbuf_size / RS_SNDLOWAT;
else
rs->rbuf_size = rs->rq_size * RS_SNDLOWAT;
if (rs->sq_size > (rs->sbuf_size / RS_SNDLOWAT))
rs->sq_size = rs->sbuf_size / RS_SNDLOWAT;
else
rs->sbuf_size = rs->sq_size * RS_SNDLOWAT;
}
static int rs_init_bufs(struct rsocket *rs)
{
uint32_t total_rbuf_size, total_sbuf_size;
size_t len;
rs->rmsg = calloc(rs->rq_size + 1, sizeof(*rs->rmsg));
if (!rs->rmsg)
return ERR(ENOMEM);
total_sbuf_size = rs->sbuf_size;
if (rs->sq_inline < RS_MAX_CTRL_MSG)
total_sbuf_size += RS_MAX_CTRL_MSG * RS_QP_CTRL_SIZE;
rs->sbuf = calloc(total_sbuf_size, 1);
if (!rs->sbuf)
return ERR(ENOMEM);
rs->smr = rdma_reg_msgs(rs->cm_id, rs->sbuf, total_sbuf_size);
if (!rs->smr)
return -1;
len = sizeof(*rs->target_sgl) * RS_SGL_SIZE +
sizeof(*rs->target_iomap) * rs->target_iomap_size;
rs->target_buffer_list = malloc(len);
if (!rs->target_buffer_list)
return ERR(ENOMEM);
rs->target_mr = rdma_reg_write(rs->cm_id, rs->target_buffer_list, len);
if (!rs->target_mr)
return -1;
memset(rs->target_buffer_list, 0, len);
rs->target_sgl = rs->target_buffer_list;
if (rs->target_iomap_size)
rs->target_iomap = (struct rs_iomap *) (rs->target_sgl + RS_SGL_SIZE);
total_rbuf_size = rs->rbuf_size;
if (rs->opts & RS_OPT_MSG_SEND)
total_rbuf_size += rs->rq_size * RS_MSG_SIZE;
rs->rbuf = calloc(total_rbuf_size, 1);
if (!rs->rbuf)
return ERR(ENOMEM);
rs->rmr = rdma_reg_write(rs->cm_id, rs->rbuf, total_rbuf_size);
if (!rs->rmr)
return -1;
rs->ssgl[0].addr = rs->ssgl[1].addr = (uintptr_t) rs->sbuf;
rs->sbuf_bytes_avail = rs->sbuf_size;
rs->ssgl[0].lkey = rs->ssgl[1].lkey = rs->smr->lkey;
rs->rbuf_free_offset = rs->rbuf_size >> 1;
rs->rbuf_bytes_avail = rs->rbuf_size >> 1;
rs->sqe_avail = rs->sq_size - rs->ctrl_max_seqno;
rs->rseq_comp = rs->rq_size >> 1;
return 0;
}
static int ds_init_bufs(struct ds_qp *qp)
{
qp->rbuf = calloc(qp->rs->rbuf_size + sizeof(struct ibv_grh), 1);
if (!qp->rbuf)
return ERR(ENOMEM);
qp->smr = rdma_reg_msgs(qp->cm_id, qp->rs->sbuf, qp->rs->sbuf_size);
if (!qp->smr)
return -1;
qp->rmr = rdma_reg_msgs(qp->cm_id, qp->rbuf, qp->rs->rbuf_size +
sizeof(struct ibv_grh));
if (!qp->rmr)
return -1;
return 0;
}
/*
* If a user is waiting on a datagram rsocket through poll or select, then
* we need the first completion to generate an event on the related epoll fd
* in order to signal the user. We arm the CQ on creation for this purpose
*/
static int rs_create_cq(struct rsocket *rs, struct rdma_cm_id *cm_id)
{
cm_id->recv_cq_channel = ibv_create_comp_channel(cm_id->verbs);
if (!cm_id->recv_cq_channel)
return -1;
cm_id->recv_cq = ibv_create_cq(cm_id->verbs, rs->sq_size + rs->rq_size,
cm_id, cm_id->recv_cq_channel, 0);
if (!cm_id->recv_cq)
goto err1;
if (rs->fd_flags & O_NONBLOCK) {
if (set_fd_nonblock(cm_id->recv_cq_channel->fd, true))
goto err2;
}
ibv_req_notify_cq(cm_id->recv_cq, 0);
cm_id->send_cq_channel = cm_id->recv_cq_channel;
cm_id->send_cq = cm_id->recv_cq;
return 0;
err2:
ibv_destroy_cq(cm_id->recv_cq);
cm_id->recv_cq = NULL;
err1:
ibv_destroy_comp_channel(cm_id->recv_cq_channel);
cm_id->recv_cq_channel = NULL;
return -1;
}
static inline int rs_post_recv(struct rsocket *rs)
{
struct ibv_recv_wr wr, *bad;
struct ibv_sge sge;
wr.next = NULL;
if (!(rs->opts & RS_OPT_MSG_SEND)) {
wr.wr_id = rs_recv_wr_id(0);
wr.sg_list = NULL;
wr.num_sge = 0;
} else {
wr.wr_id = rs_recv_wr_id(rs->rbuf_msg_index);
sge.addr = (uintptr_t) rs->rbuf + rs->rbuf_size +
(rs->rbuf_msg_index * RS_MSG_SIZE);
sge.length = RS_MSG_SIZE;
sge.lkey = rs->rmr->lkey;
wr.sg_list = &sge;
wr.num_sge = 1;
if(++rs->rbuf_msg_index == rs->rq_size)
rs->rbuf_msg_index = 0;
}
return rdma_seterrno(ibv_post_recv(rs->cm_id->qp, &wr, &bad));
}
static inline int ds_post_recv(struct rsocket *rs, struct ds_qp *qp, uint32_t offset)
{
struct ibv_recv_wr wr, *bad;
struct ibv_sge sge[2];
sge[0].addr = (uintptr_t) qp->rbuf + rs->rbuf_size;
sge[0].length = sizeof(struct ibv_grh);
sge[0].lkey = qp->rmr->lkey;
sge[1].addr = (uintptr_t) qp->rbuf + offset;
sge[1].length = RS_SNDLOWAT;
sge[1].lkey = qp->rmr->lkey;
wr.wr_id = rs_recv_wr_id(offset);
wr.next = NULL;
wr.sg_list = sge;
wr.num_sge = 2;
return rdma_seterrno(ibv_post_recv(qp->cm_id->qp, &wr, &bad));
}
static int rs_create_ep(struct rsocket *rs)
{
struct ibv_qp_init_attr qp_attr;
int i, ret;
rs_set_qp_size(rs);
if (rs->cm_id->verbs->device->transport_type == IBV_TRANSPORT_IWARP)
rs->opts |= RS_OPT_MSG_SEND;
ret = rs_create_cq(rs, rs->cm_id);
if (ret)
return ret;
memset(&qp_attr, 0, sizeof qp_attr);
qp_attr.qp_context = rs;
qp_attr.send_cq = rs->cm_id->send_cq;
qp_attr.recv_cq = rs->cm_id->recv_cq;
qp_attr.qp_type = IBV_QPT_RC;
qp_attr.sq_sig_all = 1;
qp_attr.cap.max_send_wr = rs->sq_size;
qp_attr.cap.max_recv_wr = rs->rq_size;
qp_attr.cap.max_send_sge = 2;
qp_attr.cap.max_recv_sge = 1;
qp_attr.cap.max_inline_data = rs->sq_inline;
ret = rdma_create_qp(rs->cm_id, NULL, &qp_attr);
if (ret)
return ret;
rs->sq_inline = qp_attr.cap.max_inline_data;
if ((rs->opts & RS_OPT_MSG_SEND) && (rs->sq_inline < RS_MSG_SIZE))
return ERR(ENOTSUP);
ret = rs_init_bufs(rs);
if (ret)
return ret;
for (i = 0; i < rs->rq_size; i++) {
ret = rs_post_recv(rs);
if (ret)
return ret;
}
return 0;
}
static void rs_release_iomap_mr(struct rs_iomap_mr *iomr)
{
if (atomic_fetch_sub(&iomr->refcnt, 1) != 1)
return;
dlist_remove(&iomr->entry);
ibv_dereg_mr(iomr->mr);
if (iomr->index >= 0)
iomr->mr = NULL;
else
free(iomr);
}
static void rs_free_iomappings(struct rsocket *rs)
{
struct rs_iomap_mr *iomr;
while (!dlist_empty(&rs->iomap_list)) {
iomr = container_of(rs->iomap_list.next,
struct rs_iomap_mr, entry);
riounmap(rs->index, iomr->mr->addr, iomr->mr->length);
}
while (!dlist_empty(&rs->iomap_queue)) {
iomr = container_of(rs->iomap_queue.next,
struct rs_iomap_mr, entry);
riounmap(rs->index, iomr->mr->addr, iomr->mr->length);
}
}
static void ds_free_qp(struct ds_qp *qp)
{
if (qp->smr)
rdma_dereg_mr(qp->smr);
if (qp->rbuf) {
if (qp->rmr)
rdma_dereg_mr(qp->rmr);
free(qp->rbuf);
}
if (qp->cm_id) {
if (qp->cm_id->qp) {
tdelete(&qp->dest.addr, &qp->rs->dest_map, ds_compare_addr);
epoll_ctl(qp->rs->epfd, EPOLL_CTL_DEL,
qp->cm_id->recv_cq_channel->fd, NULL);
rdma_destroy_qp(qp->cm_id);
}
rdma_destroy_id(qp->cm_id);
}
free(qp);
}
static void ds_free(struct rsocket *rs)
{
struct ds_qp *qp;
if (rs->udp_sock >= 0)
close(rs->udp_sock);
if (rs->index >= 0)