summaryrefslogtreecommitdiff
path: root/source4/cluster/ctdb/ib/ibwrapper.c
blob: 31acbc4a2d121380c08514e1627ea4ef57a8fb75 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
/*
 * Unix SMB/CIFS implementation.
 * Wrap Infiniband calls.
 *
 * Copyright (C) Sven Oehme <oehmes@de.ibm.com> 2006
 *
 * Major code contributions by Peter Somogyi <psomogyi@gamax.hu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <assert.h>
#include <unistd.h>

#include "includes.h"
#include "lib/events/events.h"
#include "ibwrapper.h"

#include <infiniband/kern-abi.h>
#include <rdma/rdma_cma_abi.h>
#include <rdma/rdma_cma.h>

#include "ibwrapper_internal.h"
#include "lib/util/dlinklist.h"

#define IBW_LASTERR_BUFSIZE 512
static char ibw_lasterr[IBW_LASTERR_BUFSIZE];

#define IBW_MAX_SEND_WR 256
#define IBW_MAX_RECV_WR 1024
#define IBW_RECV_BUFSIZE 256
#define IBW_RECV_THRESHOLD (1 * 1024 * 1024)

static void ibw_event_handler_verbs(struct event_context *ev,
	struct fd_event *fde, uint16_t flags, void *private_data);
static int ibw_fill_cq(struct ibw_conn *conn);
static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc);
static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc);
static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len);

static void *ibw_alloc_mr(struct ibw_ctx_priv *pctx, struct ibw_conn_priv *pconn,
	uint32_t n, struct ibv_mr **ppmr)
{
	void *buf;

	DEBUG(10, ("ibw_alloc_mr(cmid=%p, n=%u)\n", pconn->cm_id, n));
	buf = memalign(pctx->pagesize, n);
	if (!buf) {
		sprintf(ibw_lasterr, "couldn't allocate memory\n");
		return NULL;
	}

	*ppmr = ibv_reg_mr(pconn->pd, buf, n, IBV_ACCESS_LOCAL_WRITE);
	if (!*ppmr) {
		sprintf(ibw_lasterr, "couldn't allocate mr\n");
		free(buf);
		return NULL;
	}

	return buf;
}

static void ibw_free_mr(char **ppbuf, struct ibv_mr **ppmr)
{
	DEBUG(10, ("ibw_free_mr(%p %p)\n", *ppbuf, *ppmr));
	if (*ppmr!=NULL) {
		ibv_dereg_mr(*ppmr);
		*ppmr = NULL;
	}
	if (*ppbuf) {
		free(*ppbuf);
		*ppbuf = NULL;
	}
}

static int ibw_init_memory(struct ibw_conn *conn)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_opts *opts = &pctx->opts;
	int	i;
	struct ibw_wr	*p;

	DEBUG(10, ("ibw_init_memory(cmid: %p)\n", pconn->cm_id));
	pconn->buf_send = ibw_alloc_mr(pctx, pconn,
		opts->max_send_wr * opts->recv_bufsize, &pconn->mr_send);
	if (!pconn->buf_send) {
		sprintf(ibw_lasterr, "couldn't allocate work send buf\n");
		return -1;
	}

	pconn->buf_recv = ibw_alloc_mr(pctx, pconn,
		opts->max_recv_wr * opts->recv_bufsize, &pconn->mr_recv);
	if (!pconn->buf_recv) {
		sprintf(ibw_lasterr, "couldn't allocate work recv buf\n");
		return -1;
	}

	pconn->wr_index = talloc_size(pconn, opts->max_send_wr * sizeof(struct ibw_wr *));
	assert(pconn->wr_index!=NULL);

	for(i=0; i<opts->max_send_wr; i++) {
		p = pconn->wr_index[i] = talloc_zero(pconn, struct ibw_wr);
		p->buf = pconn->buf_send + (i * opts->recv_bufsize);
		p->wr_id = i;

		DLIST_ADD(pconn->wr_list_avail, p);
	}

	return 0;
}

static int ibw_ctx_priv_destruct(struct ibw_ctx_priv *pctx)
{
	DEBUG(10, ("ibw_ctx_priv_destruct(%p)\n", pctx));

	/* destroy cm */
	if (pctx->cm_channel) {
		rdma_destroy_event_channel(pctx->cm_channel);
		pctx->cm_channel = NULL;
	}
	if (pctx->cm_channel_event) {
		/* TODO: do we have to do this here? */
		talloc_free(pctx->cm_channel_event);
		pctx->cm_channel_event = NULL;
	}
	if (pctx->cm_id) {
		rdma_destroy_id(pctx->cm_id);
		pctx->cm_id = NULL;
	}

	return 0;
}

static int ibw_ctx_destruct(struct ibw_ctx *ctx)
{
	DEBUG(10, ("ibw_ctx_destruct(%p)\n", ctx));
	return 0;
}

static int ibw_conn_priv_destruct(struct ibw_conn_priv *pconn)
{
	DEBUG(10, ("ibw_conn_priv_destruct(%p, cmid: %p)\n",
		pconn, pconn->cm_id));

	/* pconn->wr_index is freed by talloc */
	/* pconn->wr_index[i] are freed by talloc */

	/* destroy verbs */
	if (pconn->cm_id!=NULL && pconn->cm_id->qp!=NULL) {
		rdma_destroy_qp(pconn->cm_id);
		pconn->cm_id->qp = NULL;
	}

	if (pconn->cq!=NULL) {
		ibv_destroy_cq(pconn->cq);
		pconn->cq = NULL;
	}

	if (pconn->verbs_channel!=NULL) {
		ibv_destroy_comp_channel(pconn->verbs_channel);
		pconn->verbs_channel = NULL;
	}

	/* must be freed here because its order is important */
	if (pconn->verbs_channel_event) {
		talloc_free(pconn->verbs_channel_event);
		pconn->verbs_channel_event = NULL;
	}

	/* free memory regions */
	ibw_free_mr(&pconn->buf_send, &pconn->mr_send);
	ibw_free_mr(&pconn->buf_recv, &pconn->mr_recv);

	if (pconn->pd) {
		ibv_dealloc_pd(pconn->pd);
		pconn->pd = NULL;
		DEBUG(10, ("pconn=%p pd deallocated\n", pconn));
	}

	if (pconn->cm_id) {
		rdma_destroy_id(pconn->cm_id);
		pconn->cm_id = NULL;
		DEBUG(10, ("pconn=%p cm_id destroyed\n", pconn));
	}

	return 0;
}

static int ibw_wr_destruct(struct ibw_wr *wr)
{
	if (wr->buf_large!=NULL)
		ibw_free_mr(&wr->buf_large, &wr->mr_large);
	return 0;
}

static int ibw_conn_destruct(struct ibw_conn *conn)
{
	DEBUG(10, ("ibw_conn_destruct(%p)\n", conn));
	
	/* important here: ctx is a talloc _parent_ */
	DLIST_REMOVE(conn->ctx->conn_list, conn);
	return 0;
}

struct ibw_conn *ibw_conn_new(struct ibw_ctx *ctx, TALLOC_CTX *mem_ctx)
{
	struct ibw_conn *conn;
	struct ibw_conn_priv *pconn;

	assert(ctx!=NULL);

	conn = talloc_zero(mem_ctx, struct ibw_conn);
	assert(conn!=NULL);
	talloc_set_destructor(conn, ibw_conn_destruct);

	pconn = talloc_zero(conn, struct ibw_conn_priv);
	assert(pconn!=NULL);
	talloc_set_destructor(pconn, ibw_conn_priv_destruct);

	conn->ctx = ctx;
	conn->internal = (void *)pconn;

	DLIST_ADD(ctx->conn_list, conn);

	return conn;
}

static int ibw_setup_cq_qp(struct ibw_conn *conn)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibv_qp_init_attr init_attr;
	struct ibv_qp_attr attr;
	int rc;

	DEBUG(10, ("ibw_setup_cq_qp(cmid: %p)\n", pconn->cm_id));

	/* init verbs */
	pconn->verbs_channel = ibv_create_comp_channel(pconn->cm_id->verbs);
	if (!pconn->verbs_channel) {
		sprintf(ibw_lasterr, "ibv_create_comp_channel failed %d\n", errno);
		return -1;
	}
	DEBUG(10, ("created channel %p\n", pconn->verbs_channel));

	pconn->verbs_channel_event = event_add_fd(pctx->ectx, NULL, /* not pconn or conn */
		pconn->verbs_channel->fd, EVENT_FD_READ, ibw_event_handler_verbs, conn);

	pconn->pd = ibv_alloc_pd(pconn->cm_id->verbs);
	if (!pconn->pd) {
		sprintf(ibw_lasterr, "ibv_alloc_pd failed %d\n", errno);
		return -1;
	}
	DEBUG(10, ("created pd %p\n", pconn->pd));

	/* init mr */
	if (ibw_init_memory(conn))
		return -1;

	/* init cq */
	pconn->cq = ibv_create_cq(pconn->cm_id->verbs,
		pctx->opts.max_recv_wr + pctx->opts.max_send_wr,
		conn, pconn->verbs_channel, 0);
	if (pconn->cq==NULL) {
		sprintf(ibw_lasterr, "ibv_create_cq failed\n");
		return -1;
	}

	rc = ibv_req_notify_cq(pconn->cq, 0);
	if (rc) {
		sprintf(ibw_lasterr, "ibv_req_notify_cq failed with %d\n", rc);
		return rc;
	}

	/* init qp */
	memset(&init_attr, 0, sizeof(init_attr));
	init_attr.cap.max_send_wr = pctx->opts.max_send_wr;
	init_attr.cap.max_recv_wr = pctx->opts.max_recv_wr;
	init_attr.cap.max_recv_sge = 1;
	init_attr.cap.max_send_sge = 1;
	init_attr.qp_type = IBV_QPT_RC;
	init_attr.send_cq = pconn->cq;
	init_attr.recv_cq = pconn->cq;

	rc = rdma_create_qp(pconn->cm_id, pconn->pd, &init_attr);
	if (rc) {
		sprintf(ibw_lasterr, "rdma_create_qp failed with %d\n", rc);
		return rc;
	}
	/* elase result is in pconn->cm_id->qp */

	rc = ibv_query_qp(pconn->cm_id->qp, &attr, IBV_QP_PATH_MTU, &init_attr);
	if (rc) {
		sprintf(ibw_lasterr, "ibv_query_qp failed with %d\n", rc);
		return rc;
	}

	return ibw_fill_cq(conn);
}

static int ibw_refill_cq_recv(struct ibw_conn *conn)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	int	rc;
	struct ibv_sge list = {
		.addr 	= (uintptr_t) NULL, /* filled below */
		.length = pctx->opts.recv_bufsize,
		.lkey 	= pconn->mr_recv->lkey /* always the same */
	};
	struct ibv_recv_wr wr = {
		.wr_id 	    = 0, /* filled below */
		.sg_list    = &list,
		.num_sge    = 1,
	};
	struct ibv_recv_wr *bad_wr;

	DEBUG(10, ("ibw_refill_cq_recv(cmid: %p)\n", pconn->cm_id));

	list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
	wr.wr_id = pconn->recv_index;
	pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;

	rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
	if (rc) {
		sprintf(ibw_lasterr, "refill/ibv_post_recv failed with %d\n", rc);
		DEBUG(0, (ibw_lasterr));
		return -2;
	}

	return 0;
}

static int ibw_fill_cq(struct ibw_conn *conn)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	int	i, rc;
	struct ibv_sge list = {
		.addr 	= (uintptr_t) NULL, /* filled below */
		.length = pctx->opts.recv_bufsize,
		.lkey 	= pconn->mr_recv->lkey /* always the same */
	};
	struct ibv_recv_wr wr = {
		.wr_id 	    = 0, /* filled below */
		.sg_list    = &list,
		.num_sge    = 1,
	};
	struct ibv_recv_wr *bad_wr;

	DEBUG(10, ("ibw_fill_cq(cmid: %p)\n", pconn->cm_id));

	for(i = pctx->opts.max_recv_wr; i!=0; i--) {
		list.addr = (uintptr_t) pconn->buf_recv + pctx->opts.recv_bufsize * pconn->recv_index;
		wr.wr_id = pconn->recv_index;
		pconn->recv_index = (pconn->recv_index + 1) % pctx->opts.max_recv_wr;

		rc = ibv_post_recv(pconn->cm_id->qp, &wr, &bad_wr);
		if (rc) {
			sprintf(ibw_lasterr, "fill/ibv_post_recv failed with %d\n", rc);
			DEBUG(0, (ibw_lasterr));
			return -2;
		}
	}

	return 0;
}

static int ibw_manage_connect(struct ibw_conn *conn)
{
	struct rdma_conn_param conn_param;
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	int	rc;

	DEBUG(10, ("ibw_manage_connect(cmid: %p)\n", pconn->cm_id));

	if (ibw_setup_cq_qp(conn))
		return -1;

	/* cm connect */
	memset(&conn_param, 0, sizeof conn_param);
	conn_param.responder_resources = 1;
	conn_param.initiator_depth = 1;
	conn_param.retry_count = 10;

	rc = rdma_connect(pconn->cm_id, &conn_param);
	if (rc)
		sprintf(ibw_lasterr, "rdma_connect error %d\n", rc);

	return rc;
}

static void ibw_event_handler_cm(struct event_context *ev,
	struct fd_event *fde, uint16_t flags, void *private_data)
{
	int	rc;
	struct ibw_ctx	*ctx = talloc_get_type(private_data, struct ibw_ctx);
	struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn *conn = NULL;
	struct ibw_conn_priv *pconn = NULL;
	struct rdma_cm_id *cma_id = NULL;
	struct rdma_cm_event *event = NULL;

	assert(ctx!=NULL);

	rc = rdma_get_cm_event(pctx->cm_channel, &event);
	if (rc) {
		ctx->state = IBWS_ERROR;
		event = NULL;
		sprintf(ibw_lasterr, "rdma_get_cm_event error %d\n", rc);
		goto error;
	}
	cma_id = event->id;

	DEBUG(10, ("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
		  (cma_id == pctx->cm_id) ? "parent" : "child"));

	switch (event->event) {
	case RDMA_CM_EVENT_ADDR_RESOLVED:
		DEBUG(11, ("RDMA_CM_EVENT_ADDR_RESOLVED\n"));
		/* continuing from ibw_connect ... */
		rc = rdma_resolve_route(cma_id, 2000);
		if (rc) {
			sprintf(ibw_lasterr, "rdma_resolve_route error %d\n", rc);
			goto error;
		}
		/* continued at RDMA_CM_EVENT_ROUTE_RESOLVED */
		break;

	case RDMA_CM_EVENT_ROUTE_RESOLVED:
		DEBUG(11, ("RDMA_CM_EVENT_ROUTE_RESOLVED\n"));
		/* after RDMA_CM_EVENT_ADDR_RESOLVED: */
		assert(cma_id->context!=NULL);
		conn = talloc_get_type(cma_id->context, struct ibw_conn);

		rc = ibw_manage_connect(conn);
		if (rc)
			goto error;

		break;

	case RDMA_CM_EVENT_CONNECT_REQUEST:
		DEBUG(11, ("RDMA_CM_EVENT_CONNECT_REQUEST\n"));
		ctx->state = IBWS_CONNECT_REQUEST;
		conn = ibw_conn_new(ctx, ctx);
		pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
		pconn->cm_id = cma_id; /* !!! event will be freed but id not */
		cma_id->context = (void *)conn;
		DEBUG(10, ("pconn->cm_id %p\n", pconn->cm_id));

		if (ibw_setup_cq_qp(conn))
			goto error;

		conn->state = IBWC_INIT;
		pctx->connstate_func(ctx, conn);

		/* continued at ibw_accept when invoked by the func above */
		if (!pconn->is_accepted) {
			rc = rdma_reject(cma_id, NULL, 0);
			if (rc)
				DEBUG(0, ("rdma_reject failed with rc=%d\n", rc));
			talloc_free(conn);
			DEBUG(10, ("pconn->cm_id %p wasn't accepted\n", pconn->cm_id));
		}

		/* TODO: clarify whether if it's needed by upper layer: */
		ctx->state = IBWS_READY;
		pctx->connstate_func(ctx, NULL);

		/* NOTE: more requests can arrive until RDMA_CM_EVENT_ESTABLISHED ! */
		break;

	case RDMA_CM_EVENT_ESTABLISHED:
		/* expected after ibw_accept and ibw_connect[not directly] */
		DEBUG(1, ("ESTABLISHED (conn: %p)\n", cma_id->context));
		conn = talloc_get_type(cma_id->context, struct ibw_conn);
		assert(conn!=NULL); /* important assumption */

		DEBUG(10, ("ibw_setup_cq_qp succeeded (cmid=%p)\n", cma_id));

		/* client conn is up */
		conn->state = IBWC_CONNECTED;

		/* both ctx and conn have changed */
		pctx->connstate_func(ctx, conn);
		break;

	case RDMA_CM_EVENT_ADDR_ERROR:
		sprintf(ibw_lasterr, "RDMA_CM_EVENT_ADDR_ERROR, error %d\n", event->status);
	case RDMA_CM_EVENT_ROUTE_ERROR:
		sprintf(ibw_lasterr, "RDMA_CM_EVENT_ROUTE_ERROR, error %d\n", event->status);
	case RDMA_CM_EVENT_CONNECT_ERROR:
		sprintf(ibw_lasterr, "RDMA_CM_EVENT_CONNECT_ERROR, error %d\n", event->status);
	case RDMA_CM_EVENT_UNREACHABLE:
		sprintf(ibw_lasterr, "RDMA_CM_EVENT_UNREACHABLE, error %d\n", event->status);
		goto error;
	case RDMA_CM_EVENT_REJECTED:
		sprintf(ibw_lasterr, "RDMA_CM_EVENT_REJECTED, error %d\n", event->status);
		DEBUG(1, ("cm event handler: %s", ibw_lasterr));
		conn = talloc_get_type(cma_id->context, struct ibw_conn);
		if (conn) {
			/* must be done BEFORE connstate */
			if ((rc=rdma_ack_cm_event(event)))
				DEBUG(0, ("reject/rdma_ack_cm_event failed with %d\n", rc));
			event = NULL; /* not to touch cma_id or conn */
			conn->state = IBWC_ERROR;
			/* it should free the conn */
			pctx->connstate_func(NULL, conn);
		}
		break; /* this is not strictly an error */

	case RDMA_CM_EVENT_DISCONNECTED:
		DEBUG(11, ("RDMA_CM_EVENT_DISCONNECTED\n"));
		if ((rc=rdma_ack_cm_event(event)))
			DEBUG(0, ("disc/rdma_ack_cm_event failed with %d\n", rc));
		event = NULL; /* don't ack more */

		if (cma_id!=pctx->cm_id) {
			DEBUG(0, ("client DISCONNECT event cm_id=%p\n", cma_id));
			conn = talloc_get_type(cma_id->context, struct ibw_conn);
			conn->state = IBWC_DISCONNECTED;
			pctx->connstate_func(NULL, conn);
		}
		break;

	case RDMA_CM_EVENT_DEVICE_REMOVAL:
		sprintf(ibw_lasterr, "cma detected device removal!\n");
		goto error;

	default:
		sprintf(ibw_lasterr, "unknown event %d\n", event->event);
		goto error;
	}

	if (event!=NULL && (rc=rdma_ack_cm_event(event))) {
		sprintf(ibw_lasterr, "rdma_ack_cm_event failed with %d\n", rc);
		goto error;
	}

	return;
error:
	DEBUG(0, ("cm event handler: %s", ibw_lasterr));

	if (event!=NULL) {
		if (cma_id!=NULL && cma_id!=pctx->cm_id) {
			conn = talloc_get_type(cma_id->context, struct ibw_conn);
			if (conn) {
				conn->state = IBWC_ERROR;
				pctx->connstate_func(NULL, conn);
			}
		} else {
			ctx->state = IBWS_ERROR;
			pctx->connstate_func(ctx, NULL);
		}

		if ((rc=rdma_ack_cm_event(event))!=0) {
			DEBUG(0, ("rdma_ack_cm_event failed with %d\n", rc));
		}
	}

	return;
}

static void ibw_event_handler_verbs(struct event_context *ev,
	struct fd_event *fde, uint16_t flags, void *private_data)
{
	struct ibw_conn	*conn = talloc_get_type(private_data, struct ibw_conn);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);

	struct ibv_wc wc;
	int rc;
	struct ibv_cq *ev_cq;
	void          *ev_ctx;

	DEBUG(10, ("ibw_event_handler_verbs(%u)\n", (uint32_t)flags));

	/* TODO: check whether if it's good to have more channels here... */
	rc = ibv_get_cq_event(pconn->verbs_channel, &ev_cq, &ev_ctx);
	if (rc) {
		sprintf(ibw_lasterr, "Failed to get cq_event with %d\n", rc);
		goto error;
	}
	if (ev_cq != pconn->cq) {
		sprintf(ibw_lasterr, "ev_cq(%p) != pconn->cq(%p)\n", ev_cq, pconn->cq);
		goto error;
	}
	rc = ibv_req_notify_cq(pconn->cq, 0);
	if (rc) {
		sprintf(ibw_lasterr, "Couldn't request CQ notification (%d)\n", rc);
		goto error;
	}

	while((rc=ibv_poll_cq(pconn->cq, 1, &wc))==1) {
		if (wc.status) {
			sprintf(ibw_lasterr, "cq completion failed status=%d, opcode=%d, rc=%d\n",
				wc.status, wc.opcode, rc);
			goto error;
		}

		switch(wc.opcode) {
		case IBV_WC_SEND:
			DEBUG(10, ("send completion\n"));
			if (ibw_wc_send(conn, &wc))
				goto error;
			break;

		case IBV_WC_RDMA_WRITE:
			DEBUG(10, ("rdma write completion\n"));
			break;
	
		case IBV_WC_RDMA_READ:
			DEBUG(10, ("rdma read completion\n"));
			break;

		case IBV_WC_RECV:
			DEBUG(10, ("recv completion\n"));
			if (ibw_wc_recv(conn, &wc))
				goto error;
			break;

		default:
			sprintf(ibw_lasterr, "unknown completion %d\n", wc.opcode);
			goto error;
		}
	}
	if (rc!=0) {
		sprintf(ibw_lasterr, "ibv_poll_cq error %d\n", rc);
		goto error;
	}

	ibv_ack_cq_events(pconn->cq, 1);

	return;
error:
	ibv_ack_cq_events(pconn->cq, 1);

	DEBUG(0, (ibw_lasterr));
	
	if (conn->state!=IBWC_ERROR) {
		conn->state = IBWC_ERROR;
		pctx->connstate_func(NULL, conn);
	}
}

static int ibw_process_queue(struct ibw_conn *conn)
{
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_ctx_priv *pctx;
	struct ibw_wr	*p;
	int	rc;
	uint32_t	msg_size;

	if (pconn->queue==NULL)
		return 0; /* NOP */

	p = pconn->queue;

	/* we must have at least 1 fragment to send */
	assert(p->queued_ref_cnt>0);
	p->queued_ref_cnt--;

	pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	msg_size = (p->queued_ref_cnt) ? pctx->opts.recv_bufsize : p->queued_rlen;

	assert(p->queued_msg!=NULL);
	assert(msg_size!=0);

	DEBUG(10, ("ibw_process_queue refcnt=%d msgsize=%u\n",
		p->queued_ref_cnt, msg_size));

	rc = ibw_send_packet(conn, p->queued_msg, p, msg_size);

	/* was this the last fragment? */
	if (p->queued_ref_cnt) {
		p->queued_msg += pctx->opts.recv_bufsize;
	} else {
		DLIST_REMOVE2(pconn->queue, p, qprev, qnext);
		p->queued_msg = NULL;
	}

	return rc;
}

static int ibw_wc_send(struct ibw_conn *conn, struct ibv_wc *wc)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_wr	*p;
	int	send_index;

	DEBUG(10, ("ibw_wc_send(cmid: %p, wr_id: %u, bl: %u)\n",
		pconn->cm_id, (uint32_t)wc->wr_id, (uint32_t)wc->byte_len));

	assert(pconn->cm_id->qp->qp_num==wc->qp_num);
	assert(wc->wr_id >= pctx->opts.max_recv_wr);
	send_index = wc->wr_id - pctx->opts.max_recv_wr;
	pconn->wr_sent--;

	if (send_index < pctx->opts.max_send_wr) {
		DEBUG(10, ("ibw_wc_send#1 %u\n", (int)wc->wr_id));
		p = pconn->wr_index[send_index];
		if (p->buf_large!=NULL) {
			if (p->ref_cnt) {
				/* awaiting more of it... */
				p->ref_cnt--;
			} else {
				ibw_free_mr(&p->buf_large, &p->mr_large);
				DLIST_REMOVE(pconn->wr_list_used, p);
				DLIST_ADD(pconn->wr_list_avail, p);
			}
		} else { /* nasty - but necessary */
			DLIST_REMOVE(pconn->wr_list_used, p);
			DLIST_ADD(pconn->wr_list_avail, p);
		}
	} else { /* "extra" request - not optimized */
		DEBUG(10, ("ibw_wc_send#2 %u\n", (int)wc->wr_id));
		for(p=pconn->extra_sent; p!=NULL; p=p->next)
			if ((p->wr_id + pctx->opts.max_recv_wr)==(int)wc->wr_id)
				break;
		if (p==NULL) {
			sprintf(ibw_lasterr, "failed to find wr_id %d\n", (int)wc->wr_id);
				return -1;
		}
		if (p->ref_cnt) {
			p->ref_cnt--;
		} else {
			ibw_free_mr(&p->buf_large, &p->mr_large);
			DLIST_REMOVE(pconn->extra_sent, p);
			DLIST_ADD(pconn->extra_avail, p);
		}
	}

	return ibw_process_queue(conn);
}

static int ibw_append_to_part(struct ibw_conn_priv *pconn,
	struct ibw_part *part, char **pp, uint32_t add_len, int info)
{
	DEBUG(10, ("ibw_append_to_part: cmid=%p, (bs=%u, len=%u, tr=%u), al=%u, i=%u\n",
		pconn->cm_id, part->bufsize, part->len, part->to_read, add_len, info));

	/* allocate more if necessary - it's an "evergrowing" buffer... */
	if (part->len + add_len > part->bufsize) {
		if (part->buf==NULL) {
			assert(part->len==0);
			part->buf = talloc_size(pconn, add_len);
			if (part->buf==NULL) {
				sprintf(ibw_lasterr, "recv talloc_size error (%u) #%d\n",
					add_len, info);
				return -1;
			}
			part->bufsize = add_len;
		} else {
			part->buf = talloc_realloc_size(pconn,
				part->buf, part->len + add_len);
			if (part->buf==NULL) {
				sprintf(ibw_lasterr, "recv realloc error (%u + %u) #%d\n",
					part->len, add_len, info);
				return -1;
			}
		}
		part->bufsize = part->len + add_len;
	}

	/* consume pp */
	memcpy(part->buf + part->len, *pp, add_len);
	*pp += add_len;
	part->len += add_len;
	part->to_read -= add_len;

	return 0;
}

static int ibw_wc_mem_threshold(struct ibw_conn_priv *pconn,
	struct ibw_part *part, uint32_t threshold)
{
	DEBUG(10, ("ibw_wc_mem_threshold: cmid=%p, (bs=%u, len=%u, tr=%u), thr=%u\n",
		pconn->cm_id, part->bufsize, part->len, part->to_read, threshold));

	if (part->bufsize > threshold) {
		DEBUG(3, ("ibw_wc_mem_threshold: cmid=%p, %u > %u\n",
			pconn->cm_id, part->bufsize, threshold));
		talloc_free(part->buf);
		part->buf = talloc_size(pconn, threshold);
		if (part->buf==NULL) {
			sprintf(ibw_lasterr, "talloc_size failed\n");
			return -1;
		}
		part->bufsize = threshold;
	}
	return 0;
}

static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_part	*part = &pconn->part;
	char	*p;
	uint32_t	remain = wc->byte_len;

	DEBUG(10, ("ibw_wc_recv: cmid=%p, wr_id: %u, bl: %u\n",
		pconn->cm_id, (uint32_t)wc->wr_id, remain));

	assert(pconn->cm_id->qp->qp_num==wc->qp_num);
	assert((int)wc->wr_id < pctx->opts.max_recv_wr);
	assert(wc->byte_len <= pctx->opts.recv_bufsize);

	p = pconn->buf_recv + ((int)wc->wr_id * pctx->opts.recv_bufsize);

	while(remain) {
		/* here always true: (part->len!=0 && part->to_read!=0) ||
			(part->len==0 && part->to_read==0) */
		if (part->len) { /* is there a partial msg to be continued? */
			int read_len = (part->to_read<=remain) ? part->to_read : remain;
			if (ibw_append_to_part(pconn, part, &p, read_len, 421))
				goto error;
			remain -= read_len;

			if (part->len<=sizeof(uint32_t) && part->to_read==0) {
				assert(part->len==sizeof(uint32_t));
				/* set it again now... */
				part->to_read = *((uint32_t *)(part->buf)); /* TODO: ntohl */
				if (part->to_read<sizeof(uint32_t)) {
					sprintf(ibw_lasterr, "got msglen=%u #2\n", part->to_read);
					goto error;
				}
				part->to_read -= sizeof(uint32_t); /* it's already read */
			}

			if (part->to_read==0) {
				pctx->receive_func(conn, part->buf, part->len);
				part->len = 0; /* tells not having partial data (any more) */
				if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
					goto error;
			}
		} else {
			if (remain>=sizeof(uint32_t)) {
				uint32_t msglen = *(uint32_t *)p; /* TODO: ntohl */
				if (msglen<sizeof(uint32_t)) {
					sprintf(ibw_lasterr, "got msglen=%u\n", msglen);
					goto error;
				}

				/* mostly awaited case: */
				if (msglen<=remain) {
					pctx->receive_func(conn, p, msglen);
					p += msglen;
					remain -= msglen;
				} else {
					part->to_read = msglen;
					/* part->len is already 0 */
					if (ibw_append_to_part(pconn, part, &p, remain, 422))
						goto error;
					remain = 0; /* to be continued ... */
					/* part->to_read > 0 here */
				}
			} else { /* edge case: */
				part->to_read = sizeof(uint32_t);
				/* part->len is already 0 */
				if (ibw_append_to_part(pconn, part, &p, remain, 423))
					goto error;
				remain = 0;
				/* part->to_read > 0 here */
			}
		}
	} /* <remain> is always decreased at least by 1 */

	if (ibw_refill_cq_recv(conn))
		goto error;

	return 0;

error:
	DEBUG(0, ("ibw_wc_recv error: %s", ibw_lasterr));
	return -1;
}

static int ibw_process_init_attrs(struct ibw_initattr *attr, int nattr, struct ibw_opts *opts)
{
	int	i;
	const char *name, *value;

	DEBUG(10, ("ibw_process_init_attrs: nattr: %d\n", nattr));

	opts->max_send_wr = IBW_MAX_SEND_WR;
	opts->max_recv_wr = IBW_MAX_RECV_WR;
	opts->recv_bufsize = IBW_RECV_BUFSIZE;
	opts->recv_threshold = IBW_RECV_THRESHOLD;

	for(i=0; i<nattr; i++) {
		name = attr[i].name;
		value = attr[i].value;

		assert(name!=NULL && value!=NULL);
		if (strcmp(name, "max_send_wr")==0)
			opts->max_send_wr = atoi(value);
		else if (strcmp(name, "max_recv_wr")==0)
			opts->max_recv_wr = atoi(value);
		else if (strcmp(name, "recv_bufsize")==0)
			opts->recv_bufsize = atoi(value);
		else if (strcmp(name, "recv_threshold")==0)
			opts->recv_threshold = atoi(value);
		else {
			sprintf(ibw_lasterr, "ibw_init: unknown name %s\n", name);
			return -1;
		}
	}
	return 0;
}

struct ibw_ctx *ibw_init(struct ibw_initattr *attr, int nattr,
	void *ctx_userdata,
	ibw_connstate_fn_t ibw_connstate,
	ibw_receive_fn_t ibw_receive,
	struct event_context *ectx)
{
	struct ibw_ctx *ctx = talloc_zero(NULL, struct ibw_ctx);
	struct ibw_ctx_priv *pctx;
	int	rc;

	DEBUG(10, ("ibw_init(ctx_userdata: %p, ectx: %p)\n", ctx_userdata, ectx));

	/* initialize basic data structures */
	memset(ibw_lasterr, 0, IBW_LASTERR_BUFSIZE);

	assert(ctx!=NULL);
	ibw_lasterr[0] = '\0';
	talloc_set_destructor(ctx, ibw_ctx_destruct);
	ctx->ctx_userdata = ctx_userdata;

	pctx = talloc_zero(ctx, struct ibw_ctx_priv);
	talloc_set_destructor(pctx, ibw_ctx_priv_destruct);
	ctx->internal = (void *)pctx;
	assert(pctx!=NULL);

	pctx->connstate_func = ibw_connstate;
	pctx->receive_func = ibw_receive;

	pctx->ectx = ectx;

	/* process attributes */
	if (ibw_process_init_attrs(attr, nattr, &pctx->opts))
		goto cleanup;

	/* init cm */
	pctx->cm_channel = rdma_create_event_channel();
	if (!pctx->cm_channel) {
		sprintf(ibw_lasterr, "rdma_create_event_channel error %d\n", errno);
		goto cleanup;
	}

	pctx->cm_channel_event = event_add_fd(pctx->ectx, pctx,
		pctx->cm_channel->fd, EVENT_FD_READ, ibw_event_handler_cm, ctx);

#if RDMA_USER_CM_MAX_ABI_VERSION >= 2
	rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx, RDMA_PS_TCP);
#else
	rc = rdma_create_id(pctx->cm_channel, &pctx->cm_id, ctx);
#endif
	if (rc) {
		rc = errno;
		sprintf(ibw_lasterr, "rdma_create_id error %d\n", rc);
		goto cleanup;
	}
	DEBUG(10, ("created cm_id %p\n", pctx->cm_id));

	pctx->pagesize = sysconf(_SC_PAGESIZE);

	return ctx;
	/* don't put code here */
cleanup:
	DEBUG(0, (ibw_lasterr));

	if (ctx)
		talloc_free(ctx);

	return NULL;
}

int ibw_stop(struct ibw_ctx *ctx)
{
	struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
	struct ibw_conn *p;

	DEBUG(10, ("ibw_stop\n"));

	for(p=ctx->conn_list; p!=NULL; p=p->next) {
		if (ctx->state==IBWC_ERROR || ctx->state==IBWC_CONNECTED) {
			if (ibw_disconnect(p))
				return -1;
		}
	}

	ctx->state = IBWS_STOPPED;
	pctx->connstate_func(ctx, NULL);

	return 0;
}

int ibw_bind(struct ibw_ctx *ctx, struct sockaddr_in *my_addr)
{
	struct ibw_ctx_priv *pctx = (struct ibw_ctx_priv *)ctx->internal;
	int	rc;

	DEBUG(10, ("ibw_bind: addr=%s, port=%u\n",
		inet_ntoa(my_addr->sin_addr), ntohs(my_addr->sin_port)));
	rc = rdma_bind_addr(pctx->cm_id, (struct sockaddr *) my_addr);
	if (rc) {
		sprintf(ibw_lasterr, "rdma_bind_addr error %d\n", rc);
		DEBUG(0, (ibw_lasterr));
		return rc;
	}
	DEBUG(10, ("rdma_bind_addr successful\n"));

	return 0;
}

int ibw_listen(struct ibw_ctx *ctx, int backlog)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(ctx->internal, struct ibw_ctx_priv);
	int	rc;

	DEBUG(10, ("ibw_listen\n"));
	rc = rdma_listen(pctx->cm_id, backlog);
	if (rc) {
		sprintf(ibw_lasterr, "rdma_listen failed: %d\n", rc);
		DEBUG(0, (ibw_lasterr));
		return rc;
	}

	return 0;
}

int ibw_accept(struct ibw_ctx *ctx, struct ibw_conn *conn, void *conn_userdata)
{
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct rdma_conn_param	conn_param;
	int	rc;

	DEBUG(10, ("ibw_accept: cmid=%p\n", pconn->cm_id));
	conn->conn_userdata = conn_userdata;

	memset(&conn_param, 0, sizeof(struct rdma_conn_param));
	conn_param.responder_resources = 1;
	conn_param.initiator_depth = 1;
	rc = rdma_accept(pconn->cm_id, &conn_param);
	if (rc) {
		sprintf(ibw_lasterr, "rdma_accept failed %d\n", rc);
		DEBUG(0, (ibw_lasterr));
		return -1;;
	}

	pconn->is_accepted = 1;

	/* continued at RDMA_CM_EVENT_ESTABLISHED */

	return 0;
}

int ibw_connect(struct ibw_conn *conn, struct sockaddr_in *serv_addr, void *conn_userdata)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = NULL;
	int	rc;

	assert(conn!=NULL);

	conn->conn_userdata = conn_userdata;
	pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	DEBUG(10, ("ibw_connect: addr=%s, port=%u\n", inet_ntoa(serv_addr->sin_addr),
		ntohs(serv_addr->sin_port)));

	/* clean previous - probably half - initialization */
	if (ibw_conn_priv_destruct(pconn)) {
		DEBUG(0, ("ibw_connect/ibw_pconn_destruct failed for cm_id=%p\n", pconn->cm_id));
		return -1;
	}

	/* init cm */
#if RDMA_USER_CM_MAX_ABI_VERSION >= 2
	rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn, RDMA_PS_TCP);
#else
	rc = rdma_create_id(pctx->cm_channel, &pconn->cm_id, conn);
#endif
	if (rc) {
		rc = errno;
		sprintf(ibw_lasterr, "ibw_connect/rdma_create_id error %d\n", rc);
		talloc_free(conn);
		return -1;
	}
	DEBUG(10, ("ibw_connect: rdma_create_id succeeded, cm_id=%p\n", pconn->cm_id));

	rc = rdma_resolve_addr(pconn->cm_id, NULL, (struct sockaddr *) serv_addr, 2000);
	if (rc) {
		sprintf(ibw_lasterr, "rdma_resolve_addr error %d\n", rc);
		DEBUG(0, (ibw_lasterr));
		talloc_free(conn);
		return -1;
	}

	/* continued at RDMA_CM_EVENT_ADDR_RESOLVED */

	return 0;
}

int ibw_disconnect(struct ibw_conn *conn)
{
	int	rc;
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);

	DEBUG(10, ("ibw_disconnect: cmid=%p\n", pconn->cm_id));

	assert(pconn!=NULL);

	switch(conn->state) {
	case IBWC_ERROR:
		ibw_conn_priv_destruct(pconn); /* do this here right now */
		break;
	case IBWC_CONNECTED:
		rc = rdma_disconnect(pconn->cm_id);
		if (rc) {
			sprintf(ibw_lasterr, "ibw_disconnect failed with %d\n", rc);
			DEBUG(0, (ibw_lasterr));
			return rc;
		}
		break;
	default:
		DEBUG(9, ("invalid state for disconnect: %d\n", conn->state));
		break;
	}

	return 0;
}

int ibw_alloc_send_buf(struct ibw_conn *conn, void **buf, void **key, uint32_t len)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_wr *p = pconn->wr_list_avail;

	if (p!=NULL) {
		DEBUG(10, ("ibw_alloc_send_buf#1: cmid=%p, len=%d\n", pconn->cm_id, len));

		DLIST_REMOVE(pconn->wr_list_avail, p);
		DLIST_ADD(pconn->wr_list_used, p);

		if (len <= pctx->opts.recv_bufsize) {
			*buf = (void *)p->buf;
		} else {
			p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
			if (p->buf_large==NULL) {
				sprintf(ibw_lasterr, "ibw_alloc_mr#1 failed\n");
				goto error;
			}
			*buf = (void *)p->buf_large;
		}
		/* p->wr_id is already filled in ibw_init_memory */
	} else {
		DEBUG(10, ("ibw_alloc_send_buf#2: cmid=%p, len=%d\n", pconn->cm_id, len));
		/* not optimized */
		p = pconn->extra_avail;
		if (!p) {
			p = pconn->extra_avail = talloc_zero(pconn, struct ibw_wr);
			talloc_set_destructor(p, ibw_wr_destruct);
			if (p==NULL) {
				sprintf(ibw_lasterr, "talloc_zero failed (emax: %u)\n", pconn->extra_max);
				goto error;
			}
			p->wr_id = pctx->opts.max_send_wr + pconn->extra_max;
			pconn->extra_max++;
			switch(pconn->extra_max) {
				case 1: DEBUG(2, ("warning: queue performed\n")); break;
				case 10: DEBUG(0, ("warning: queue reached 10\n")); break;
				case 100: DEBUG(0, ("warning: queue reached 100\n")); break;
				case 1000: DEBUG(0, ("warning: queue reached 1000\n")); break;
				default: break;
			}
		}

		p->buf_large = ibw_alloc_mr(pctx, pconn, len, &p->mr_large);
		if (p->buf_large==NULL) {
			sprintf(ibw_lasterr, "ibw_alloc_mr#2 failed\n");
			goto error;
		}
		*buf = (void *)p->buf_large;

		DLIST_REMOVE(pconn->extra_avail, p);
		/* we don't have prepared index for this, so that
		 * we will have to find this by wr_id later on */
		DLIST_ADD(pconn->extra_sent, p);
	}

	*key = (void *)p;

	return 0;
error:
	DEBUG(0, ("ibw_alloc_send_buf error: %s", ibw_lasterr));
	return -1;
}


static int ibw_send_packet(struct ibw_conn *conn, void *buf, struct ibw_wr *p, uint32_t len)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	int	rc;

	/* can we send it right now? */
	if (pconn->wr_sent<pctx->opts.max_send_wr) {
		struct ibv_send_wr *bad_wr;
		struct ibv_sge list = {
			.addr 	= (uintptr_t)buf,
			.length = len,
			.lkey 	= pconn->mr_send->lkey
		};
		struct ibv_send_wr wr = {
			.wr_id 	    = p->wr_id + pctx->opts.max_recv_wr,
			.sg_list    = &list,
			.num_sge    = 1,
			.opcode     = IBV_WR_SEND,
			.send_flags = IBV_SEND_SIGNALED,
		};

		if (p->buf_large==NULL) {
			DEBUG(10, ("ibw_send#normal(cmid: %p, wrid: %u, n: %d)\n",
				pconn->cm_id, (uint32_t)wr.wr_id, len));
		} else {
			DEBUG(10, ("ibw_send#large(cmid: %p, wrid: %u, n: %d)\n",
				pconn->cm_id, (uint32_t)wr.wr_id, len));
			list.lkey = p->mr_large->lkey;
		}

		rc = ibv_post_send(pconn->cm_id->qp, &wr, &bad_wr);
		if (rc) {
			sprintf(ibw_lasterr, "ibv_post_send error %d (%d)\n",
				rc, pconn->wr_sent);
			goto error;
		}

		pconn->wr_sent++;

		return rc;
	} /* else put the request into our own queue: */

	DEBUG(10, ("ibw_send#queued(cmid: %p, len: %u)\n", pconn->cm_id, len));

	/* TODO: clarify how to continue when state==IBWC_STOPPED */

	/* to be sent by ibw_wc_send */
	/* regardless "normal" or [a part of] "large" packet */
	if (!p->queued_ref_cnt) {
		DLIST_ADD_END2(pconn->queue, p, struct ibw_wr *,
			qprev, qnext); /* TODO: optimize */
		p->queued_msg = buf;
	}
	p->queued_ref_cnt++;
	p->queued_rlen = len; /* last wins; see ibw_wc_send */

	return 0;
error:
	DEBUG(0, (ibw_lasterr));
	return -1;
}

int ibw_send(struct ibw_conn *conn, void *buf, void *key, uint32_t len)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);
	int	rc;

	assert(len>=sizeof(uint32_t));
	assert((*((uint32_t *)buf)==len)); /* TODO: htonl */

	if (len > pctx->opts.recv_bufsize) {
		struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
		int	rlen = len;
		char	*packet = (char *)buf;
		uint32_t	recv_bufsize = pctx->opts.recv_bufsize;

		DEBUG(10, ("ibw_send#frag(cmid: %p, buf: %p, len: %u)\n",
			pconn->cm_id, buf, len));

		/* single threaded => no race here: */
		assert(p->ref_cnt==0);
		while(rlen > recv_bufsize) {
			rc = ibw_send_packet(conn, packet, p, recv_bufsize);
			if (rc)
				return rc;
			packet += recv_bufsize;
			rlen -= recv_bufsize;
			p->ref_cnt++; /* not good to have it in ibw_send_packet */
		}
		if (rlen) {
			rc = ibw_send_packet(conn, packet, p, rlen);
			p->ref_cnt++; /* not good to have it in ibw_send_packet */
		}
		p->ref_cnt--; /* for the same handling */
	} else {
		assert(p->ref_cnt==0);
		assert(p->queued_ref_cnt==0);

		rc = ibw_send_packet(conn, buf, p, len);
	}
	return rc;
}

int ibw_cancel_send_buf(struct ibw_conn *conn, void *buf, void *key)
{
	struct ibw_ctx_priv *pctx = talloc_get_type(conn->ctx->internal, struct ibw_ctx_priv);
	struct ibw_conn_priv *pconn = talloc_get_type(conn->internal, struct ibw_conn_priv);
	struct ibw_wr *p = talloc_get_type(key, struct ibw_wr);

	assert(p!=NULL);
	assert(buf!=NULL);
	assert(conn!=NULL);

	if (p->buf_large!=NULL)
		ibw_free_mr(&p->buf_large, &p->mr_large);

	/* parallel case */
	if (p->wr_id < pctx->opts.max_send_wr) {
		DEBUG(10, ("ibw_cancel_send_buf#1 %u", (int)p->wr_id));
		DLIST_REMOVE(pconn->wr_list_used, p);
		DLIST_ADD(pconn->wr_list_avail, p);
	} else { /* "extra" packet */
		DEBUG(10, ("ibw_cancel_send_buf#2 %u", (int)p->wr_id));
		DLIST_REMOVE(pconn->extra_sent, p);
		DLIST_ADD(pconn->extra_avail, p);
	}

	return 0;
}

const char *ibw_getLastError(void)
{
	return ibw_lasterr;
}