-
Notifications
You must be signed in to change notification settings - Fork 146
/
ib.c
121 lines (104 loc) · 2.88 KB
/
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
#include <arpa/inet.h>
#include <unistd.h>
#include "ib.h"
#include "debug.h"
int modify_qp_to_rts (struct ibv_qp *qp, uint32_t target_qp_num, uint16_t target_lid)
{
int ret = 0;
/* change QP state to INIT */
{
struct ibv_qp_attr qp_attr = {
.qp_state = IBV_QPS_INIT,
.pkey_index = 0,
.port_num = IB_PORT,
.qp_access_flags = IBV_ACCESS_LOCAL_WRITE |
IBV_ACCESS_REMOTE_READ |
IBV_ACCESS_REMOTE_ATOMIC |
IBV_ACCESS_REMOTE_WRITE,
};
ret = ibv_modify_qp (qp, &qp_attr,
IBV_QP_STATE | IBV_QP_PKEY_INDEX |
IBV_QP_PORT | IBV_QP_ACCESS_FLAGS);
check (ret == 0, "Failed to modify qp to INIT.");
}
/* Change QP state to RTR */
{
struct ibv_qp_attr qp_attr = {
.qp_state = IBV_QPS_RTR,
.path_mtu = IB_MTU,
.dest_qp_num = target_qp_num,
.rq_psn = 0,
.max_dest_rd_atomic = 1,
.min_rnr_timer = 12,
.ah_attr.is_global = 0,
.ah_attr.dlid = target_lid,
.ah_attr.sl = IB_SL,
.ah_attr.src_path_bits = 0,
.ah_attr.port_num = IB_PORT,
};
ret = ibv_modify_qp(qp, &qp_attr,
IBV_QP_STATE | IBV_QP_AV |
IBV_QP_PATH_MTU | IBV_QP_DEST_QPN |
IBV_QP_RQ_PSN | IBV_QP_MAX_DEST_RD_ATOMIC |
IBV_QP_MIN_RNR_TIMER);
check (ret == 0, "Failed to change qp to rtr.");
}
/* Change QP state to RTS */
{
struct ibv_qp_attr qp_attr = {
.qp_state = IBV_QPS_RTS,
.timeout = 14,
.retry_cnt = 7,
.rnr_retry = 7,
.sq_psn = 0,
.max_rd_atomic = 1,
};
ret = ibv_modify_qp (qp, &qp_attr,
IBV_QP_STATE | IBV_QP_TIMEOUT |
IBV_QP_RETRY_CNT | IBV_QP_RNR_RETRY |
IBV_QP_SQ_PSN | IBV_QP_MAX_QP_RD_ATOMIC);
check (ret == 0, "Failed to modify qp to RTS.");
}
return 0;
error:
return -1;
}
int post_send (uint32_t req_size, uint32_t lkey, uint64_t wr_id,
uint32_t imm_data, struct ibv_qp *qp, char *buf)
{
int ret = 0;
struct ibv_send_wr *bad_send_wr;
struct ibv_sge list = {
.addr = (uintptr_t) buf,
.length = req_size,
.lkey = lkey
};
struct ibv_send_wr send_wr = {
.wr_id = wr_id,
.sg_list = &list,
.num_sge = 1,
.opcode = IBV_WR_SEND_WITH_IMM,
.send_flags = IBV_SEND_SIGNALED,
.imm_data = htonl (imm_data)
};
ret = ibv_post_send (qp, &send_wr, &bad_send_wr);
return ret;
}
int post_srq_recv (uint32_t req_size, uint32_t lkey, uint64_t wr_id,
struct ibv_srq *srq, char *buf)
{
int ret = 0;
struct ibv_recv_wr *bad_recv_wr;
struct ibv_sge list = {
.addr = (uintptr_t) buf,
.length = req_size,
.lkey = lkey
};
struct ibv_recv_wr recv_wr = {
.wr_id = wr_id,
.sg_list = &list,
.num_sge = 1
};
ret = ibv_post_srq_recv (srq, &recv_wr, &bad_recv_wr);
return ret;
}