-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdma_common.c
226 lines (210 loc) · 6.76 KB
/
rdma_common.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
// Program : rdma_common.c
// Author : Sousuke Kanamoto
#include "rdma_common.h"
extern struct ibv_mr* rdma_gpubuffer_alloc(struct ibv_pd *pd, uint32_t length, enum ibv_access_flags permission);
extern struct ibv_mr* rdma_gpubuffer_alloc_adress(struct ibv_pd *pd, void* addr, uint32_t length, enum ibv_access_flags permission);
extern void rdma_gpubuffer_free();
extern void rdma_gpubuffer_free_addr(struct ibv_mr* mr);
extern void kernel_start();
extern void kernel_start_addr(void *addr);
extern int cuAlloc(void* addr, int length);
extern int cuFree(void* addr);
extern int cuCopy(void* dst, void* src, size_t size);
void show_rdma_cmid(struct rdma_cm_id *id)
{
if(!id){
rdma_error("Passed ptr is NULL\n");
return;
}
printf("RDMA cm id at %p \n", id);
if(id->verbs && id->verbs->device)
printf("dev_ctx: %p (device name: %s) \n", id->verbs,
id->verbs->device->name);
if(id->channel){
printf("cm event channel %p\n", id->channel);
printf("QP: %p, port_space %x, port_num %u \n", id->qp,
id->ps,
id->port_num);
}
}
void show_rdma_buffer_attr(struct rdma_buffer_attr *attr){
if(!attr){
rdma_error("Passed attr is NULL\n");
return;
}
printf("--------------------------------------------------------------\n");
printf("buffer attr, addr: %p , len: %u , stag : 0x%x \n",
(void*) attr->address,
(unsigned int) attr->length,
attr->stag.local_stag);
printf("--------------------------------------------------------------\n");
}
void free_gpubuffer_rdma(struct ibv_mr *mr){
rdma_gpubuffer_free_addr(mr);
}
struct ibv_mr* rdma_buffer_alloc_gpu(struct ibv_pd *pd, uint32_t length,
enum ibv_access_flags permission)
{
struct ibv_mr *mr = NULL;
mr = rdma_gpubuffer_alloc(pd, length, permission);
return mr;
}
struct ibv_mr* rdma_buffer_alloc_gpu_adress(struct ibv_pd *pd, void* addr,uint32_t length,
enum ibv_access_flags permission)
{
struct ibv_mr *mr = NULL;
mr = rdma_gpubuffer_alloc_adress(pd, addr, length, permission);
return mr;
}
int cudaAlloc(void** addr, int length){
return cuAlloc(addr, length);
}
int cudaFreeAddr(void* addr){
return cuFree(addr);
}
int cudaCopy(void* dst, void* src, size_t size){
return cuCopy(dst, src, size);
}
struct ibv_mr *rdma_buffer_register(struct ibv_pd *pd,
void *addr, uint32_t length,
enum ibv_access_flags permission)
{
struct ibv_mr *mr = NULL;
if (!pd) {
rdma_error("Protection domain is NULL, ignoring \n");
return NULL;
}
mr = ibv_reg_mr(pd, addr, length, permission);
if (!mr) {
rdma_error("Failed to create mr on buffer, errno: %d \n", -errno);
return NULL;
}
debug("Registered: %p , len: %u , stag: 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
return mr;
}
void rdma_buffer_free_gpu()
{
rdma_gpubuffer_free();
}
void rdma_buffer_deregister(struct ibv_mr *mr)
{
if (!mr) {
rdma_error("Passed memory region is NULL, ignoring\n");
return;
}
debug("Deregistered: %p , len: %u , stag : 0x%x \n",
mr->addr,
(unsigned int) mr->length,
mr->lkey);
ibv_dereg_mr(mr);
}
int process_rdma_cm_event(struct rdma_event_channel *echannel,
enum rdma_cm_event_type expected_event,
struct rdma_cm_event **cm_event)
{
int ret = 1;
ret = rdma_get_cm_event(echannel, cm_event);
if (ret) {
rdma_error("Failed to retrieve a cm event, errno: %d \n",
-errno);
return -errno;
}
/* lets see, if it was a good event */
if(0 != (*cm_event)->status){
rdma_error("CM event has non zero status: %d\n", (*cm_event)->status);
ret = -((*cm_event)->status);
/* important, we acknowledge the event */
rdma_ack_cm_event(*cm_event);
return ret;
}
/* if it was a good event, was it of the expected type */
if ((*cm_event)->event != expected_event) {
rdma_error("Unexpected event received: %s [ expecting: %s ]",
rdma_event_str((*cm_event)->event),
rdma_event_str(expected_event));
/* important, we acknowledge the event */
rdma_ack_cm_event(*cm_event);
return -1; // unexpected event :(
}
debug("A new %s type event is received \n", rdma_event_str((*cm_event)->event));
/* The caller must acknowledge the event */
return ret;
}
int process_work_completion_events (struct ibv_comp_channel *comp_channel,struct ibv_wc *wc, int max_wc)
{
struct ibv_cq *cq_ptr = NULL;
void *context = NULL;
int ret = -1, i, total_wc = 0;
/* We wait for the notification on the CQ channel */
ret = ibv_get_cq_event(comp_channel, /* IO channel where we are expecting the notification */
&cq_ptr, /* which CQ has an activity. This should be the same as CQ we created before */
&context); /* Associated CQ user context, which we did set */
if (ret) {
rdma_error("Failed to get next CQ event due to %d \n", -errno);
return -errno;
}
/* Request for more notifications. */
ret = ibv_req_notify_cq(cq_ptr, 0);
if (ret){
rdma_error("Failed to request further notifications %d \n", -errno);
return -errno;
}
/* We got notification. We reap the work completion (WC) element. It is
* unlikely but a good practice it write the CQ polling code that
* can handle zero WCs. ibv_poll_cq can return zero. Same logic as
* MUTEX conditional variables in pthread programming. */
total_wc = 0;
do {
ret = ibv_poll_cq(cq_ptr /* the CQ, we got notification for */,
max_wc - total_wc /* number of remaining WC elements*/,
wc + total_wc/* where to store */);
if (ret < 0) {
rdma_error("Failed to poll cq for wc due to %d \n", ret);
/* ret is errno here */
return ret;
}
total_wc += ret;
} while (total_wc < max_wc);
/* Now we check validity and status of I/O work completions */
for( i = 0 ; i < total_wc ; i++) {
if (wc[i].status != IBV_WC_SUCCESS) {
rdma_error("Work completion (WC) has error status: %d (means: %s) at index %d\n",
-wc[i].status,
ibv_wc_status_str(wc[i].status),
i);
/* return negative value */
return -(wc[i].status);
}
}
debug("%d WC are completed \n", total_wc);
/* Similar to connection management events, we need to acknowledge CQ events */
ibv_ack_cq_events(cq_ptr,
1 /* we received one event notification. This is not
number of WC elements */);
return total_wc;
}
/* Code acknowledgment: rping.c from librdmacm/examples */
int get_addr(char *dst, struct sockaddr *addr)
{
struct addrinfo *res;
int ret = -1;
ret = getaddrinfo(dst, NULL, NULL, &res);
if (ret) {
rdma_error("getaddrinfo failed - invalid hostname or IP address\n");
return ret;
}
memcpy(addr, res->ai_addr, sizeof(struct sockaddr_in));
freeaddrinfo(res);
return ret;
}
void write_rdma_buffer()
{
kernel_start();
}
void write_rdma_buffer_addr(void *addr)
{
kernel_start_addr(addr);
}