-
Notifications
You must be signed in to change notification settings - Fork 147
/
setup_ib.c
378 lines (314 loc) · 10.2 KB
/
setup_ib.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
#include <arpa/inet.h>
#include <unistd.h>
#include <malloc.h>
#include "sock.h"
#include "ib.h"
#include "debug.h"
#include "config.h"
#include "setup_ib.h"
struct IBRes ib_res;
int connect_qp_server ()
{
int ret = 0, n = 0, i = 0;
int num_peers = config_info.num_clients;
int sockfd = 0;
int *peer_sockfd = NULL;
struct sockaddr_in peer_addr;
socklen_t peer_addr_len = sizeof(struct sockaddr_in);
char sock_buf[64] = {'\0'};
struct QPInfo *local_qp_info = NULL;
struct QPInfo *remote_qp_info = NULL;
sockfd = sock_create_bind(config_info.sock_port);
check(sockfd > 0, "Failed to create server socket.");
listen(sockfd, 5);
peer_sockfd = (int *) calloc (num_peers, sizeof(int));
check (peer_sockfd != NULL, "Failed to allocate peer_sockfd");
for (i = 0; i < num_peers; i++) {
peer_sockfd[i] = accept(sockfd, (struct sockaddr *)&peer_addr,
&peer_addr_len);
check (peer_sockfd[i] > 0, "Failed to create peer_sockfd[%d]", i);
}
/* init local qp_info */
local_qp_info = (struct QPInfo *) calloc (num_peers,
sizeof(struct QPInfo));
check (local_qp_info != NULL, "Failed to allocate local_qp_info");
for (i = 0; i < num_peers; i++) {
local_qp_info[i].lid = ib_res.port_attr.lid;
local_qp_info[i].qp_num = ib_res.qp[i]->qp_num;
local_qp_info[i].rank = config_info.rank;
}
/* get qp_info from client */
remote_qp_info = (struct QPInfo *) calloc (num_peers,
sizeof(struct QPInfo));
check (remote_qp_info != NULL, "Failed to allocate remote_qp_info");
for (i = 0; i < num_peers; i++) {
ret = sock_get_qp_info (peer_sockfd[i], &remote_qp_info[i]);
check (ret == 0, "Failed to get qp_info from client[%d]", i);
}
/* send qp_info to client */
int peer_ind = -1;
int j = 0;
for (i = 0; i < num_peers; i++) {
peer_ind = -1;
for (j = 0; j < num_peers; j++) {
if (remote_qp_info[j].rank == i) {
peer_ind = j;
break;
}
}
ret = sock_set_qp_info (peer_sockfd[i], &local_qp_info[peer_ind]);
check (ret == 0, "Failed to send qp_info to client[%d]", peer_ind);
}
/* change send QP state to RTS */
log (LOG_SUB_HEADER, "Start of IB Config");
for (i = 0; i < num_peers; i++) {
peer_ind = -1;
for (j = 0; j < num_peers; j++) {
if (remote_qp_info[j].rank == i) {
peer_ind = j;
break;
}
}
ret = modify_qp_to_rts (ib_res.qp[peer_ind],
remote_qp_info[i].qp_num,
remote_qp_info[i].lid);
check (ret == 0, "Failed to modify qp[%d] to rts", peer_ind);
log ("\tqp[%"PRIu32"] <-> qp[%"PRIu32"]",
ib_res.qp[peer_ind]->qp_num, remote_qp_info[i].qp_num);
}
log (LOG_SUB_HEADER, "End of IB Config");
/* sync with clients */
for (i = 0; i < num_peers; i++) {
n = sock_read (peer_sockfd[i], sock_buf, sizeof(SOCK_SYNC_MSG));
check (n == sizeof(SOCK_SYNC_MSG), "Failed to receive sync from client");
}
for (i = 0; i < num_peers; i++) {
n = sock_write (peer_sockfd[i], sock_buf, sizeof(SOCK_SYNC_MSG));
check (n == sizeof(SOCK_SYNC_MSG), "Failed to write sync to client");
}
for (i = 0; i < num_peers; i++) {
close (peer_sockfd[i]);
}
free (peer_sockfd);
close (sockfd);
return 0;
error:
if (peer_sockfd != NULL) {
for (i = 0; i < num_peers; i++) {
if (peer_sockfd[i] > 0) {
close (peer_sockfd[i]);
}
}
free (peer_sockfd);
}
if (sockfd > 0) {
close (sockfd);
}
return -1;
}
int connect_qp_client ()
{
int ret = 0, n = 0, i = 0;
int num_peers = ib_res.num_qps;
int *peer_sockfd = NULL;
char sock_buf[64] = {'\0'};
struct QPInfo *local_qp_info = NULL;
struct QPInfo *remote_qp_info = NULL;
peer_sockfd = (int *) calloc (num_peers, sizeof(int));
check (peer_sockfd != NULL, "Failed to allocate peer_sockfd");
for (i = 0; i < num_peers; i++) {
peer_sockfd[i] = sock_create_connect (config_info.servers[i],
config_info.sock_port);
check (peer_sockfd[i] > 0, "Failed to create peer_sockfd[%d]", i);
}
/* init local qp_info */
local_qp_info = (struct QPInfo *) calloc (num_peers,
sizeof(struct QPInfo));
check (local_qp_info != NULL, "Failed to allocate local_qp_info");
for (i = 0; i < num_peers; i++) {
local_qp_info[i].lid = ib_res.port_attr.lid;
local_qp_info[i].qp_num = ib_res.qp[i]->qp_num;
local_qp_info[i].rank = config_info.rank;
}
/* send qp_info to server */
for (i = 0; i < num_peers; i++) {
ret = sock_set_qp_info (peer_sockfd[i], &local_qp_info[i]);
check (ret == 0, "Failed to send qp_info[%d] to server", i);
}
/* get qp_info from server */
remote_qp_info = (struct QPInfo *) calloc (num_peers,
sizeof(struct QPInfo));
check (remote_qp_info != NULL, "Failed to allocate remote_qp_info");
for (i = 0; i < num_peers; i++) {
ret = sock_get_qp_info (peer_sockfd[i], &remote_qp_info[i]);
check (ret == 0, "Failed to get qp_info[%d] from server", i);
}
/* change QP state to RTS */
/* send qp_info to client */
int peer_ind = -1;
int j = 0;
log (LOG_SUB_HEADER, "IB Config");
for (i = 0; i < num_peers; i++) {
peer_ind = -1;
for (j = 0; j < num_peers; j++) {
if (remote_qp_info[j].rank == i) {
peer_ind = j;
break;
}
}
ret = modify_qp_to_rts (ib_res.qp[peer_ind],
remote_qp_info[i].qp_num,
remote_qp_info[i].lid);
check (ret == 0, "Failed to modify qp[%d] to rts", peer_ind);
log ("\tqp[%"PRIu32"] <-> qp[%"PRIu32"]",
ib_res.qp[peer_ind]->qp_num, remote_qp_info[i].qp_num);
}
log (LOG_SUB_HEADER, "End of IB Config");
/* sync with server */
for (i = 0; i < num_peers; i++) {
n = sock_write (peer_sockfd[i], sock_buf, sizeof(SOCK_SYNC_MSG));
check (n == sizeof(SOCK_SYNC_MSG), "Failed to write sync to client[%d]", i);
}
for (i = 0; i < num_peers; i++) {
n = sock_read (peer_sockfd[i], sock_buf, sizeof(SOCK_SYNC_MSG));
check (n == sizeof(SOCK_SYNC_MSG), "Failed to receive sync from client");
}
for (i = 0; i < num_peers; i++) {
close (peer_sockfd[i]);
}
free (peer_sockfd);
free (local_qp_info);
free (remote_qp_info);
return 0;
error:
if (peer_sockfd != NULL) {
for (i = 0; i < num_peers; i++) {
if (peer_sockfd[i] > 0) {
close (peer_sockfd[i]);
}
}
free (peer_sockfd);
}
if (local_qp_info != NULL) {
free (local_qp_info);
}
if (remote_qp_info != NULL) {
free (remote_qp_info);
}
return -1;
}
int setup_ib ()
{
int ret = 0;
int i = 0;
struct ibv_device **dev_list = NULL;
memset (&ib_res, 0, sizeof(struct IBRes));
if (config_info.is_server) {
ib_res.num_qps = config_info.num_clients;
} else {
ib_res.num_qps = config_info.num_servers;
}
/* get IB device list */
dev_list = ibv_get_device_list(NULL);
check(dev_list != NULL, "Failed to get ib device list.");
/* create IB context */
ib_res.ctx = ibv_open_device(*dev_list);
check(ib_res.ctx != NULL, "Failed to open ib device.");
/* allocate protection domain */
ib_res.pd = ibv_alloc_pd(ib_res.ctx);
check(ib_res.pd != NULL, "Failed to allocate protection domain.");
/* query IB port attribute */
ret = ibv_query_port(ib_res.ctx, IB_PORT, &ib_res.port_attr);
check(ret == 0, "Failed to query IB port information.");
/* register mr */
/* set the buf_size twice as large as msg_size * num_concurr_msgs */
/* the recv buffer occupies the first half while the sending buffer */
/* occupies the second half */
/* assume all msgs are of the same content */
ib_res.ib_buf_size = config_info.msg_size * config_info.num_concurr_msgs * ib_res.num_qps;
ib_res.ib_buf = (char *) memalign (4096, ib_res.ib_buf_size);
check (ib_res.ib_buf != NULL, "Failed to allocate ib_buf");
ib_res.mr = ibv_reg_mr (ib_res.pd, (void *)ib_res.ib_buf,
ib_res.ib_buf_size,
IBV_ACCESS_LOCAL_WRITE |
IBV_ACCESS_REMOTE_READ |
IBV_ACCESS_REMOTE_WRITE);
check (ib_res.mr != NULL, "Failed to register mr");
/* query IB device attr */
ret = ibv_query_device(ib_res.ctx, &ib_res.dev_attr);
check(ret==0, "Failed to query device");
/* create cq */
ib_res.cq = ibv_create_cq (ib_res.ctx, ib_res.dev_attr.max_cqe,
NULL, NULL, 0);
check (ib_res.cq != NULL, "Failed to create cq");
/* create srq */
struct ibv_srq_init_attr srq_init_attr = {
.attr.max_wr = ib_res.dev_attr.max_srq_wr,
.attr.max_sge = 1,
};
ib_res.srq = ibv_create_srq (ib_res.pd, &srq_init_attr);
/* create qp */
struct ibv_qp_init_attr qp_init_attr = {
.send_cq = ib_res.cq,
.recv_cq = ib_res.cq,
.srq = ib_res.srq,
.cap = {
.max_send_wr = ib_res.dev_attr.max_qp_wr,
.max_recv_wr = ib_res.dev_attr.max_qp_wr,
.max_send_sge = 1,
.max_recv_sge = 1,
},
.qp_type = IBV_QPT_RC,
};
ib_res.qp = (struct ibv_qp **) calloc (ib_res.num_qps,
sizeof(struct ibv_qp *));
check (ib_res.qp != NULL, "Failed to allocate qp");
for (i = 0; i < ib_res.num_qps; i++) {
ib_res.qp[i] = ibv_create_qp (ib_res.pd, &qp_init_attr);
check (ib_res.qp[i] != NULL, "Failed to create qp[%d]", i);
}
/* connect QP */
if (config_info.is_server) {
ret = connect_qp_server ();
} else {
ret = connect_qp_client ();
}
check (ret == 0, "Failed to connect qp");
ibv_free_device_list (dev_list);
return 0;
error:
if (dev_list != NULL) {
ibv_free_device_list (dev_list);
}
return -1;
}
void close_ib_connection ()
{
int i;
if (ib_res.qp != NULL) {
for (i = 0; i < ib_res.num_qps; i++) {
if (ib_res.qp[i] != NULL) {
ibv_destroy_qp (ib_res.qp[i]);
}
}
free (ib_res.qp);
}
if (ib_res.srq != NULL) {
ibv_destroy_srq (ib_res.srq);
}
if (ib_res.cq != NULL) {
ibv_destroy_cq (ib_res.cq);
}
if (ib_res.mr != NULL) {
ibv_dereg_mr (ib_res.mr);
}
if (ib_res.pd != NULL) {
ibv_dealloc_pd (ib_res.pd);
}
if (ib_res.ctx != NULL) {
ibv_close_device (ib_res.ctx);
}
if (ib_res.ib_buf != NULL) {
free (ib_res.ib_buf);
}
}