-
Notifications
You must be signed in to change notification settings - Fork 32
/
krping.c
2285 lines (2020 loc) · 56.2 KB
/
krping.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) 2005 Ammasso, Inc. All rights reserved.
* Copyright (c) 2006-2009 Open Grid Computing, Inc. 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.
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/parser.h>
#include <linux/proc_fs.h>
#include <linux/inet.h>
#include <linux/list.h>
#include <linux/in.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/ktime.h>
#include <linux/random.h>
#include <linux/signal.h>
#include <linux/proc_fs.h>
#include <asm/atomic.h>
#include <asm/pci.h>
#include <rdma/ib_verbs.h>
#include <rdma/rdma_cm.h>
#include "getopt.h"
#define PFX "krping: "
static int debug = 0;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug level (0=none, 1=all)");
#define DEBUG_LOG if (debug) printk
MODULE_AUTHOR("Steve Wise");
MODULE_DESCRIPTION("RDMA ping server");
MODULE_LICENSE("Dual BSD/GPL");
static const struct krping_option krping_opts[] = {
{"count", OPT_INT, 'C'},
{"size", OPT_INT, 'S'},
{"addr", OPT_STRING, 'a'},
{"addr6", OPT_STRING, 'A'},
{"port", OPT_INT, 'p'},
{"verbose", OPT_NOPARAM, 'v'},
{"validate", OPT_NOPARAM, 'V'},
{"server", OPT_NOPARAM, 's'},
{"client", OPT_NOPARAM, 'c'},
{"server_inv", OPT_NOPARAM, 'I'},
{"wlat", OPT_NOPARAM, 'l'},
{"rlat", OPT_NOPARAM, 'L'},
{"bw", OPT_NOPARAM, 'B'},
{"duplex", OPT_NOPARAM, 'd'},
{"tos", OPT_INT, 't'},
{"txdepth", OPT_INT, 'T'},
{"poll", OPT_NOPARAM, 'P'},
{"local_dma_lkey", OPT_NOPARAM, 'Z'},
{"read_inv", OPT_NOPARAM, 'R'},
{"fr", OPT_NOPARAM, 'f'},
{NULL, 0, 0}
};
struct krping_stats {
unsigned long long send_bytes;
unsigned long long send_msgs;
unsigned long long recv_bytes;
unsigned long long recv_msgs;
unsigned long long write_bytes;
unsigned long long write_msgs;
unsigned long long read_bytes;
unsigned long long read_msgs;
};
#define htonll(x) cpu_to_be64((x))
#define ntohll(x) cpu_to_be64((x))
static DEFINE_MUTEX(krping_mutex);
/*
* List of running krping threads.
*/
static LIST_HEAD(krping_cbs);
static struct proc_dir_entry *krping_proc;
/*
* Invoke like this, one on each side, using the server's address on
* the RDMA device (iw%d):
*
* /bin/echo server,port=9999,addr=192.168.69.142,validate > /proc/krping
* /bin/echo client,port=9999,addr=192.168.69.142,validate > /proc/krping
* /bin/echo client,port=9999,addr6=2001:db8:0:f101::1,validate > /proc/krping
*
* krping "ping/pong" loop:
* client sends source rkey/addr/len
* server receives source rkey/add/len
* server rdma reads "ping" data from source
* server sends "go ahead" on rdma read completion
* client sends sink rkey/addr/len
* server receives sink rkey/addr/len
* server rdma writes "pong" data to sink
* server sends "go ahead" on rdma write completion
* <repeat loop>
*/
/*
* These states are used to signal events between the completion handler
* and the main client or server thread.
*
* Once CONNECTED, they cycle through RDMA_READ_ADV, RDMA_WRITE_ADV,
* and RDMA_WRITE_COMPLETE for each ping.
*/
enum test_state {
IDLE = 1,
CONNECT_REQUEST,
ADDR_RESOLVED,
ROUTE_RESOLVED,
CONNECTED,
RDMA_READ_ADV,
RDMA_READ_COMPLETE,
RDMA_WRITE_ADV,
RDMA_WRITE_COMPLETE,
ERROR
};
struct krping_rdma_info {
uint64_t buf;
uint32_t rkey;
uint32_t size;
};
/*
* Default max buffer size for IO...
*/
#define RPING_BUFSIZE 128*1024
#define RPING_SQ_DEPTH 64
/*
* Control block struct.
*/
struct krping_cb {
int server; /* 0 iff client */
struct ib_cq *cq;
struct ib_pd *pd;
struct ib_qp *qp;
struct ib_mr *dma_mr;
struct ib_fast_reg_page_list *page_list;
int page_list_len;
struct ib_reg_wr reg_mr_wr;
struct ib_send_wr invalidate_wr;
struct ib_mr *reg_mr;
int server_invalidate;
int read_inv;
u8 key;
struct ib_recv_wr rq_wr; /* recv work request record */
struct ib_sge recv_sgl; /* recv single SGE */
struct krping_rdma_info recv_buf __aligned(16); /* malloc'd buffer */
u64 recv_dma_addr;
DEFINE_DMA_UNMAP_ADDR(recv_mapping);
struct ib_send_wr sq_wr; /* send work requrest record */
struct ib_sge send_sgl;
struct krping_rdma_info send_buf __aligned(16); /* single send buf */
u64 send_dma_addr;
DEFINE_DMA_UNMAP_ADDR(send_mapping);
struct ib_rdma_wr rdma_sq_wr; /* rdma work request record */
struct ib_sge rdma_sgl; /* rdma single SGE */
char *rdma_buf; /* used as rdma sink */
u64 rdma_dma_addr;
DEFINE_DMA_UNMAP_ADDR(rdma_mapping);
struct ib_mr *rdma_mr;
uint32_t remote_rkey; /* remote guys RKEY */
uint64_t remote_addr; /* remote guys TO */
uint32_t remote_len; /* remote guys LEN */
char *start_buf; /* rdma read src */
u64 start_dma_addr;
DEFINE_DMA_UNMAP_ADDR(start_mapping);
struct ib_mr *start_mr;
enum test_state state; /* used for cond/signalling */
wait_queue_head_t sem;
struct krping_stats stats;
uint16_t port; /* dst port in NBO */
u8 addr[16]; /* dst addr in NBO */
char ip6_ndev_name[128]; /* IPv6 netdev name */
char *addr_str; /* dst addr string */
uint8_t addr_type; /* ADDR_FAMILY - IPv4/V6 */
int verbose; /* verbose logging */
int count; /* ping count */
int size; /* ping data size */
int validate; /* validate ping data */
int wlat; /* run wlat test */
int rlat; /* run rlat test */
int bw; /* run bw test */
int duplex; /* run bw full duplex test */
int poll; /* poll or block for rlat test */
int txdepth; /* SQ depth */
int local_dma_lkey; /* use 0 for lkey */
int frtest; /* reg test */
int tos; /* type of service */
/* CM stuff */
struct rdma_cm_id *cm_id; /* connection on client side,*/
/* listener on server side. */
struct rdma_cm_id *child_cm_id; /* connection on server side */
struct list_head list;
};
static int krping_cma_event_handler(struct rdma_cm_id *cma_id,
struct rdma_cm_event *event)
{
int ret;
struct krping_cb *cb = cma_id->context;
DEBUG_LOG("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
(cma_id == cb->cm_id) ? "parent" : "child");
switch (event->event) {
case RDMA_CM_EVENT_ADDR_RESOLVED:
cb->state = ADDR_RESOLVED;
ret = rdma_resolve_route(cma_id, 2000);
if (ret) {
printk(KERN_ERR PFX "rdma_resolve_route error %d\n",
ret);
wake_up_interruptible(&cb->sem);
}
break;
case RDMA_CM_EVENT_ROUTE_RESOLVED:
cb->state = ROUTE_RESOLVED;
wake_up_interruptible(&cb->sem);
break;
case RDMA_CM_EVENT_CONNECT_REQUEST:
cb->state = CONNECT_REQUEST;
cb->child_cm_id = cma_id;
DEBUG_LOG("child cma %p\n", cb->child_cm_id);
wake_up_interruptible(&cb->sem);
break;
case RDMA_CM_EVENT_ESTABLISHED:
DEBUG_LOG("ESTABLISHED\n");
if (!cb->server) {
cb->state = CONNECTED;
}
wake_up_interruptible(&cb->sem);
break;
case RDMA_CM_EVENT_ADDR_ERROR:
case RDMA_CM_EVENT_ROUTE_ERROR:
case RDMA_CM_EVENT_CONNECT_ERROR:
case RDMA_CM_EVENT_UNREACHABLE:
case RDMA_CM_EVENT_REJECTED:
printk(KERN_ERR PFX "cma event %d, error %d\n", event->event,
event->status);
cb->state = ERROR;
wake_up_interruptible(&cb->sem);
break;
case RDMA_CM_EVENT_DISCONNECTED:
printk(KERN_ERR PFX "DISCONNECT EVENT...\n");
cb->state = ERROR;
wake_up_interruptible(&cb->sem);
break;
case RDMA_CM_EVENT_DEVICE_REMOVAL:
printk(KERN_ERR PFX "cma detected device removal!!!!\n");
cb->state = ERROR;
wake_up_interruptible(&cb->sem);
break;
default:
printk(KERN_ERR PFX "oof bad type!\n");
wake_up_interruptible(&cb->sem);
break;
}
return 0;
}
static int server_recv(struct krping_cb *cb, struct ib_wc *wc)
{
if (wc->byte_len != sizeof(cb->recv_buf)) {
printk(KERN_ERR PFX "Received bogus data, size %d\n",
wc->byte_len);
return -1;
}
cb->remote_rkey = ntohl(cb->recv_buf.rkey);
cb->remote_addr = ntohll(cb->recv_buf.buf);
cb->remote_len = ntohl(cb->recv_buf.size);
DEBUG_LOG("Received rkey %x addr %llx len %d from peer\n",
cb->remote_rkey, (unsigned long long)cb->remote_addr,
cb->remote_len);
if (cb->state <= CONNECTED || cb->state == RDMA_WRITE_COMPLETE)
cb->state = RDMA_READ_ADV;
else
cb->state = RDMA_WRITE_ADV;
return 0;
}
static int client_recv(struct krping_cb *cb, struct ib_wc *wc)
{
if (wc->byte_len != sizeof(cb->recv_buf)) {
printk(KERN_ERR PFX "Received bogus data, size %d\n",
wc->byte_len);
return -1;
}
if (cb->state == RDMA_READ_ADV)
cb->state = RDMA_WRITE_ADV;
else
cb->state = RDMA_WRITE_COMPLETE;
return 0;
}
static void krping_cq_event_handler(struct ib_cq *cq, void *ctx)
{
struct krping_cb *cb = ctx;
struct ib_wc wc;
const struct ib_recv_wr *bad_wr;
int ret;
BUG_ON(cb->cq != cq);
if (cb->state == ERROR) {
printk(KERN_ERR PFX "cq completion in ERROR state\n");
return;
}
if (cb->frtest) {
printk(KERN_ERR PFX "cq completion event in frtest!\n");
return;
}
if (!cb->wlat && !cb->rlat && !cb->bw)
ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) {
if (wc.status) {
if (wc.status == IB_WC_WR_FLUSH_ERR) {
DEBUG_LOG("cq flushed\n");
continue;
} else {
printk(KERN_ERR PFX "cq completion failed with "
"wr_id %Lx status %d opcode %d vender_err %x\n",
wc.wr_id, wc.status, wc.opcode, wc.vendor_err);
goto error;
}
}
switch (wc.opcode) {
case IB_WC_SEND:
DEBUG_LOG("send completion\n");
cb->stats.send_bytes += cb->send_sgl.length;
cb->stats.send_msgs++;
break;
case IB_WC_RDMA_WRITE:
DEBUG_LOG("rdma write completion\n");
cb->stats.write_bytes += cb->rdma_sq_wr.wr.sg_list->length;
cb->stats.write_msgs++;
cb->state = RDMA_WRITE_COMPLETE;
wake_up_interruptible(&cb->sem);
break;
case IB_WC_RDMA_READ:
DEBUG_LOG("rdma read completion\n");
cb->stats.read_bytes += cb->rdma_sq_wr.wr.sg_list->length;
cb->stats.read_msgs++;
cb->state = RDMA_READ_COMPLETE;
wake_up_interruptible(&cb->sem);
break;
case IB_WC_RECV:
DEBUG_LOG("recv completion\n");
cb->stats.recv_bytes += sizeof(cb->recv_buf);
cb->stats.recv_msgs++;
if (cb->wlat || cb->rlat || cb->bw)
ret = server_recv(cb, &wc);
else
ret = cb->server ? server_recv(cb, &wc) :
client_recv(cb, &wc);
if (ret) {
printk(KERN_ERR PFX "recv wc error: %d\n", ret);
goto error;
}
ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post recv error: %d\n",
ret);
goto error;
}
wake_up_interruptible(&cb->sem);
break;
default:
printk(KERN_ERR PFX
"%s:%d Unexpected opcode %d, Shutting down\n",
__func__, __LINE__, wc.opcode);
goto error;
}
}
if (ret) {
printk(KERN_ERR PFX "poll error %d\n", ret);
goto error;
}
return;
error:
cb->state = ERROR;
wake_up_interruptible(&cb->sem);
}
static int krping_accept(struct krping_cb *cb)
{
struct rdma_conn_param conn_param;
int ret;
DEBUG_LOG("accepting client connection request\n");
memset(&conn_param, 0, sizeof conn_param);
conn_param.responder_resources = 1;
conn_param.initiator_depth = 1;
ret = rdma_accept(cb->child_cm_id, &conn_param);
if (ret) {
printk(KERN_ERR PFX "rdma_accept error: %d\n", ret);
return ret;
}
if (!cb->wlat && !cb->rlat && !cb->bw) {
wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
if (cb->state == ERROR) {
printk(KERN_ERR PFX "wait for CONNECTED state %d\n",
cb->state);
return -1;
}
}
return 0;
}
static void krping_setup_wr(struct krping_cb *cb)
{
cb->recv_sgl.addr = cb->recv_dma_addr;
cb->recv_sgl.length = sizeof cb->recv_buf;
cb->recv_sgl.lkey = cb->pd->local_dma_lkey;
cb->rq_wr.sg_list = &cb->recv_sgl;
cb->rq_wr.num_sge = 1;
cb->send_sgl.addr = cb->send_dma_addr;
cb->send_sgl.length = sizeof cb->send_buf;
cb->send_sgl.lkey = cb->pd->local_dma_lkey;
cb->sq_wr.opcode = IB_WR_SEND;
cb->sq_wr.send_flags = IB_SEND_SIGNALED;
cb->sq_wr.sg_list = &cb->send_sgl;
cb->sq_wr.num_sge = 1;
if (cb->server || cb->wlat || cb->rlat || cb->bw) {
cb->rdma_sgl.addr = cb->rdma_dma_addr;
cb->rdma_sq_wr.wr.send_flags = IB_SEND_SIGNALED;
cb->rdma_sq_wr.wr.sg_list = &cb->rdma_sgl;
cb->rdma_sq_wr.wr.num_sge = 1;
}
/*
* A chain of 2 WRs, INVALDATE_MR + REG_MR.
* both unsignaled. The client uses them to reregister
* the rdma buffers with a new key each iteration.
*/
cb->reg_mr_wr.wr.opcode = IB_WR_REG_MR;
cb->reg_mr_wr.mr = cb->reg_mr;
cb->invalidate_wr.next = &cb->reg_mr_wr.wr;
cb->invalidate_wr.opcode = IB_WR_LOCAL_INV;
}
static int krping_setup_buffers(struct krping_cb *cb)
{
int ret;
DEBUG_LOG(PFX "krping_setup_buffers called on cb %p\n", cb);
cb->recv_dma_addr = ib_dma_map_single(cb->pd->device,
&cb->recv_buf,
sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
if (unlikely(ib_dma_mapping_error(cb->pd->device, cb->recv_dma_addr))) {
DEBUG_LOG(PFX "recv_buf DMA map failed\n");
ret = -EINVAL;
goto bail;
}
dma_unmap_addr_set(cb, recv_mapping, cb->recv_dma_addr);
cb->send_dma_addr = ib_dma_map_single(cb->pd->device,
&cb->send_buf, sizeof(cb->send_buf),
DMA_BIDIRECTIONAL);
if (unlikely(ib_dma_mapping_error(cb->pd->device, cb->send_dma_addr))) {
DEBUG_LOG(PFX "send_buf DMA map failed\n");
ret = -EINVAL;
goto bail;
}
dma_unmap_addr_set(cb, send_mapping, cb->send_dma_addr);
cb->rdma_buf = kzalloc(cb->size, GFP_KERNEL);
if (cb->rdma_buf)
cb->rdma_dma_addr = ib_dma_map_single(cb->pd->device, cb->rdma_buf, cb->size, DMA_BIDIRECTIONAL);
if (!cb->rdma_buf || ib_dma_mapping_error(cb->pd->device, cb->rdma_dma_addr)) {
DEBUG_LOG(PFX "rdma_buf allocation failed\n");
kfree(cb->rdma_buf);
ret = -ENOMEM;
goto bail;
}
dma_unmap_addr_set(cb, rdma_mapping, cb->rdma_dma_addr);
cb->page_list_len = (((cb->size - 1) & PAGE_MASK) + PAGE_SIZE)
>> PAGE_SHIFT;
cb->reg_mr = ib_alloc_mr(cb->pd, IB_MR_TYPE_MEM_REG,
cb->page_list_len);
if (IS_ERR(cb->reg_mr)) {
ret = PTR_ERR(cb->reg_mr);
DEBUG_LOG(PFX "recv_buf reg_mr failed %d\n", ret);
goto bail;
}
DEBUG_LOG(PFX "reg rkey 0x%x page_list_len %u\n",
cb->reg_mr->rkey, cb->page_list_len);
if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
cb->start_buf = kzalloc(cb->size, GFP_KERNEL);
if (cb->start_buf)
cb->start_dma_addr = ib_dma_map_single(cb->pd->device, cb->start_buf, cb->size, DMA_BIDIRECTIONAL);
if (!cb->start_buf || ib_dma_mapping_error(cb->pd->device, cb->start_dma_addr)) {
DEBUG_LOG(PFX "start_buf malloc failed\n");
kfree(cb->start_buf);
ret = -ENOMEM;
goto bail;
}
dma_unmap_addr_set(cb, start_mapping, cb->start_dma_addr);
}
krping_setup_wr(cb);
DEBUG_LOG(PFX "allocated & registered buffers...\n");
return 0;
bail:
if (!unlikely(ib_dma_mapping_error(cb->pd->device, cb->recv_dma_addr)))
ib_dma_unmap_single(cb->pd->device,
dma_unmap_addr(cb, recv_mapping),
sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
if (!unlikely(ib_dma_mapping_error(cb->pd->device, cb->send_dma_addr)))
ib_dma_unmap_single(cb->pd->device,
dma_unmap_addr(cb, send_mapping),
sizeof(cb->send_buf), DMA_BIDIRECTIONAL);
if (cb->reg_mr && !IS_ERR(cb->reg_mr))
ib_dereg_mr(cb->reg_mr);
if (cb->rdma_mr && !IS_ERR(cb->rdma_mr))
ib_dereg_mr(cb->rdma_mr);
if (cb->dma_mr && !IS_ERR(cb->dma_mr))
ib_dereg_mr(cb->dma_mr);
if (cb->rdma_buf) {
ib_dma_unmap_single(cb->pd->device, cb->rdma_dma_addr, cb->size,
DMA_BIDIRECTIONAL);
kfree(cb->rdma_buf);
}
if (cb->start_buf) {
ib_dma_unmap_single(cb->pd->device, cb->start_dma_addr, cb->size,
DMA_BIDIRECTIONAL);
kfree(cb->start_buf);
}
return ret;
}
static void krping_free_buffers(struct krping_cb *cb)
{
DEBUG_LOG("krping_free_buffers called on cb %p\n", cb);
if (cb->dma_mr)
ib_dereg_mr(cb->dma_mr);
if (cb->rdma_mr)
ib_dereg_mr(cb->rdma_mr);
if (cb->start_mr)
ib_dereg_mr(cb->start_mr);
if (cb->reg_mr)
ib_dereg_mr(cb->reg_mr);
ib_dma_unmap_single(cb->pd->device,
dma_unmap_addr(cb, recv_mapping),
sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
ib_dma_unmap_single(cb->pd->device,
dma_unmap_addr(cb, send_mapping),
sizeof(cb->send_buf), DMA_BIDIRECTIONAL);
ib_dma_unmap_single(cb->pd->device, dma_unmap_addr(cb, rdma_dma_addr),
cb->size, DMA_BIDIRECTIONAL);
kfree(cb->rdma_buf);
if (cb->start_buf) {
ib_dma_unmap_single(cb->pd->device, dma_unmap_addr(cb, start_dma_addr),
cb->size, DMA_BIDIRECTIONAL);
kfree(cb->start_buf);
}
}
static int krping_create_qp(struct krping_cb *cb)
{
struct ib_qp_init_attr init_attr;
int ret;
memset(&init_attr, 0, sizeof(init_attr));
init_attr.cap.max_send_wr = cb->txdepth;
init_attr.cap.max_recv_wr = 2;
/* For flush_qp() */
init_attr.cap.max_send_wr++;
init_attr.cap.max_recv_wr++;
init_attr.cap.max_recv_sge = 1;
init_attr.cap.max_send_sge = 1;
init_attr.qp_type = IB_QPT_RC;
init_attr.send_cq = cb->cq;
init_attr.recv_cq = cb->cq;
init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
if (cb->server) {
ret = rdma_create_qp(cb->child_cm_id, cb->pd, &init_attr);
if (!ret)
cb->qp = cb->child_cm_id->qp;
} else {
ret = rdma_create_qp(cb->cm_id, cb->pd, &init_attr);
if (!ret)
cb->qp = cb->cm_id->qp;
}
return ret;
}
static void krping_free_qp(struct krping_cb *cb)
{
ib_destroy_qp(cb->qp);
ib_destroy_cq(cb->cq);
ib_dealloc_pd(cb->pd);
}
static int krping_setup_qp(struct krping_cb *cb, struct rdma_cm_id *cm_id)
{
int ret;
struct ib_cq_init_attr attr = {0};
cb->pd = ib_alloc_pd(cm_id->device, 0);
if (IS_ERR(cb->pd)) {
printk(KERN_ERR PFX "ib_alloc_pd failed\n");
return PTR_ERR(cb->pd);
}
DEBUG_LOG("created pd %p\n", cb->pd);
attr.cqe = cb->txdepth * 2;
attr.comp_vector = 0;
cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL,
cb, &attr);
if (IS_ERR(cb->cq)) {
printk(KERN_ERR PFX "ib_create_cq failed\n");
ret = PTR_ERR(cb->cq);
goto err1;
}
DEBUG_LOG("created cq %p\n", cb->cq);
if (!cb->wlat && !cb->rlat && !cb->bw && !cb->frtest) {
ret = ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
if (ret) {
printk(KERN_ERR PFX "ib_create_cq failed\n");
goto err2;
}
}
ret = krping_create_qp(cb);
if (ret) {
printk(KERN_ERR PFX "krping_create_qp failed: %d\n", ret);
goto err2;
}
DEBUG_LOG("created qp %p\n", cb->qp);
return 0;
err2:
ib_destroy_cq(cb->cq);
err1:
ib_dealloc_pd(cb->pd);
return ret;
}
/*
* return the (possibly rebound) rkey for the rdma buffer.
* REG mode: invalidate and rebind via reg wr.
* other modes: just return the mr rkey.
*/
static u32 krping_rdma_rkey(struct krping_cb *cb, u64 buf, int post_inv)
{
u32 rkey;
const struct ib_send_wr *bad_wr;
int ret;
struct scatterlist sg = {0};
sg_init_marker(&sg, 1);
cb->invalidate_wr.ex.invalidate_rkey = cb->reg_mr->rkey;
/*
* Update the reg key.
*/
ib_update_fast_reg_key(cb->reg_mr, ++cb->key);
cb->reg_mr_wr.key = cb->reg_mr->rkey;
/*
* Update the reg WR with new buf info.
*/
if (buf == (u64)cb->start_dma_addr)
cb->reg_mr_wr.access = IB_ACCESS_REMOTE_READ;
else
cb->reg_mr_wr.access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
sg_dma_address(&sg) = buf;
sg_dma_len(&sg) = cb->size;
ret = ib_map_mr_sg(cb->reg_mr, &sg, 1, NULL, PAGE_SIZE);
BUG_ON(ret <= 0 || ret > cb->page_list_len);
DEBUG_LOG(PFX "post_inv = %d, reg_mr new rkey 0x%x pgsz %u len %lu"
" iova_start %llx\n",
post_inv,
cb->reg_mr_wr.key,
cb->reg_mr->page_size,
(unsigned long)cb->reg_mr->length,
(unsigned long long)cb->reg_mr->iova);
if (post_inv)
ret = ib_post_send(cb->qp, &cb->invalidate_wr, &bad_wr);
else
ret = ib_post_send(cb->qp, &cb->reg_mr_wr.wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post send error %d\n", ret);
cb->state = ERROR;
}
rkey = cb->reg_mr->rkey;
return rkey;
}
static void krping_format_send(struct krping_cb *cb, u64 buf)
{
struct krping_rdma_info *info = &cb->send_buf;
u32 rkey;
/*
* Client side will do reg or mw bind before
* advertising the rdma buffer. Server side
* sends have no data.
*/
if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
rkey = krping_rdma_rkey(cb, buf, !cb->server_invalidate);
info->buf = htonll(buf);
info->rkey = htonl(rkey);
info->size = htonl(cb->size);
DEBUG_LOG("RDMA addr %llx rkey %x len %d\n",
(unsigned long long)buf, rkey, cb->size);
}
}
static void krping_test_server(struct krping_cb *cb)
{
struct ib_send_wr inv;
const struct ib_send_wr *bad_wr;
int ret;
while (1) {
/* Wait for client's Start STAG/TO/Len */
wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_ADV);
if (cb->state != RDMA_READ_ADV) {
printk(KERN_ERR PFX "wait for RDMA_READ_ADV state %d\n",
cb->state);
break;
}
DEBUG_LOG("server received sink adv\n");
cb->rdma_sq_wr.rkey = cb->remote_rkey;
cb->rdma_sq_wr.remote_addr = cb->remote_addr;
cb->rdma_sq_wr.wr.sg_list->length = cb->remote_len;
cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, !cb->read_inv);
cb->rdma_sq_wr.wr.next = NULL;
/* Issue RDMA Read. */
if (cb->read_inv)
cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ_WITH_INV;
else {
cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ;
/*
* Immediately follow the read with a
* fenced LOCAL_INV.
*/
cb->rdma_sq_wr.wr.next = &inv;
memset(&inv, 0, sizeof inv);
inv.opcode = IB_WR_LOCAL_INV;
inv.ex.invalidate_rkey = cb->reg_mr->rkey;
inv.send_flags = IB_SEND_FENCE;
}
ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post send error %d\n", ret);
break;
}
cb->rdma_sq_wr.wr.next = NULL;
DEBUG_LOG("server posted rdma read req \n");
/* Wait for read completion */
wait_event_interruptible(cb->sem,
cb->state >= RDMA_READ_COMPLETE);
if (cb->state != RDMA_READ_COMPLETE) {
printk(KERN_ERR PFX
"wait for RDMA_READ_COMPLETE state %d\n",
cb->state);
break;
}
DEBUG_LOG("server received read complete\n");
/* Display data in recv buf */
if (cb->verbose)
printk(KERN_INFO PFX
"server ping data (64B max): |%.64s|\n",
cb->rdma_buf);
/* Tell client to continue */
if (cb->server && cb->server_invalidate) {
cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
DEBUG_LOG("send-w-inv rkey 0x%x\n", cb->remote_rkey);
}
ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post send error %d\n", ret);
break;
}
DEBUG_LOG("server posted go ahead\n");
/* Wait for client's RDMA STAG/TO/Len */
wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV);
if (cb->state != RDMA_WRITE_ADV) {
printk(KERN_ERR PFX
"wait for RDMA_WRITE_ADV state %d\n",
cb->state);
break;
}
DEBUG_LOG("server received sink adv\n");
/* RDMA Write echo data */
cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE;
cb->rdma_sq_wr.rkey = cb->remote_rkey;
cb->rdma_sq_wr.remote_addr = cb->remote_addr;
cb->rdma_sq_wr.wr.sg_list->length = strlen(cb->rdma_buf) + 1;
if (cb->local_dma_lkey)
cb->rdma_sgl.lkey = cb->pd->local_dma_lkey;
else
cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 0);
DEBUG_LOG("rdma write from lkey %x laddr %llx len %d\n",
cb->rdma_sq_wr.wr.sg_list->lkey,
(unsigned long long)cb->rdma_sq_wr.wr.sg_list->addr,
cb->rdma_sq_wr.wr.sg_list->length);
ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post send error %d\n", ret);
break;
}
/* Wait for completion */
ret = wait_event_interruptible(cb->sem, cb->state >=
RDMA_WRITE_COMPLETE);
if (cb->state != RDMA_WRITE_COMPLETE) {
printk(KERN_ERR PFX
"wait for RDMA_WRITE_COMPLETE state %d\n",
cb->state);
break;
}
DEBUG_LOG("server rdma write complete \n");
cb->state = CONNECTED;
/* Tell client to begin again */
if (cb->server && cb->server_invalidate) {
cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
DEBUG_LOG("send-w-inv rkey 0x%x\n", cb->remote_rkey);
}
ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX "post send error %d\n", ret);
break;
}
DEBUG_LOG("server posted go ahead\n");
}
}
static void rlat_test(struct krping_cb *cb)
{
int scnt;
int iters = cb->count;
ktime_t start, stop;
int ret;
struct ib_wc wc;
const struct ib_send_wr *bad_wr;
int ne;
scnt = 0;
cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ;
cb->rdma_sq_wr.rkey = cb->remote_rkey;
cb->rdma_sq_wr.remote_addr = cb->remote_addr;
cb->rdma_sq_wr.wr.sg_list->length = cb->size;
start = ktime_get();
if (!cb->poll) {
cb->state = RDMA_READ_ADV;
ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
}
while (scnt < iters) {
cb->state = RDMA_READ_ADV;
ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
if (ret) {
printk(KERN_ERR PFX
"Couldn't post send: ret=%d scnt %d\n",
ret, scnt);
return;
}
do {
if (!cb->poll) {
wait_event_interruptible(cb->sem,
cb->state != RDMA_READ_ADV);
if (cb->state == RDMA_READ_COMPLETE) {
ne = 1;
ib_req_notify_cq(cb->cq,
IB_CQ_NEXT_COMP);
} else {
ne = -1;
}
} else
ne = ib_poll_cq(cb->cq, 1, &wc);
if (cb->state == ERROR) {
printk(KERN_ERR PFX
"state == ERROR...bailing scnt %d\n",
scnt);
return;
}
} while (ne == 0);
if (ne < 0) {
printk(KERN_ERR PFX "poll CQ failed %d\n", ne);
return;
}
if (cb->poll && wc.status != IB_WC_SUCCESS) {
printk(KERN_ERR PFX "Completion wth error at %s:\n",
cb->server ? "server" : "client");
printk(KERN_ERR PFX "Failed status %d: wr_id %d\n",
wc.status, (int) wc.wr_id);
return;
}
++scnt;