| 1 |
/* |
| 2 |
* Copyright (c) 2022 Canon Medical Research USA, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This software is available to you under a choice of one of two |
| 5 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 6 |
* General Public License (GPL) Version 2, available from the file |
| 7 |
* COPYING in the main directory of this source tree, or the |
| 8 |
* BSD license below: |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or |
| 11 |
* without modification, are permitted provided that the following |
| 12 |
* conditions are met: |
| 13 |
* |
| 14 |
* - Redistributions of source code must retain the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer. |
| 17 |
* |
| 18 |
* - Redistributions in binary form must reproduce the above |
| 19 |
* copyright notice, this list of conditions and the following |
| 20 |
* disclaimer in the documentation and/or other materials |
| 21 |
* provided with the distribution. |
| 22 |
* |
| 23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
* SOFTWARE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <assert.h> |
| 34 |
#include "ndspi.h" |
| 35 |
#include "verbs_nd.h" |
| 36 |
|
| 37 |
struct ibv_context **rdma_get_devices(int *num_devices) |
| 38 |
{ |
| 39 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 40 |
|
| 41 |
if (num_devices) { |
| 42 |
assert(nd_infra.adapters.count <= INT_MAX); |
| 43 |
*num_devices = (int)nd_infra.adapters.count; |
| 44 |
} |
| 45 |
return nd_infra.adapters.context_list; |
| 46 |
} |
| 47 |
|
| 48 |
void rdma_free_devices(struct ibv_context **list) |
| 49 |
{ |
| 50 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 51 |
// Since we are keeping state statically, there is nothing to free. |
| 52 |
} |
| 53 |
|
| 54 |
struct rdma_event_channel *rdma_create_event_channel(void) |
| 55 |
{ |
| 56 |
struct nd_event_channel *channel_nd; |
| 57 |
int ret; |
| 58 |
|
| 59 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 60 |
|
| 61 |
channel_nd = calloc(1, sizeof(*channel_nd)); |
| 62 |
if (!channel_nd) { |
| 63 |
errno = ENOMEM; |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
ret = dlistfd_head_init(&channel_nd->q); |
| 68 |
if (ret) { |
| 69 |
free(channel_nd); |
| 70 |
errno = ret; |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
channel_nd->channel.fd = fd_signal_get(&channel_nd->q.signal); |
| 74 |
|
| 75 |
return &channel_nd->channel; |
| 76 |
} |
| 77 |
|
| 78 |
void rdma_destroy_event_channel(struct rdma_event_channel *channel) |
| 79 |
{ |
| 80 |
struct nd_event_channel *channel_nd; |
| 81 |
struct nd_cm_event *entry_nd; |
| 82 |
struct rdma_cm_event *event; |
| 83 |
|
| 84 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 85 |
|
| 86 |
if (!channel) { |
| 87 |
errno = EINVAL; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
channel_nd = container_of(channel, struct nd_event_channel, channel); |
| 92 |
while (!dlistfd_empty(&channel_nd->q)) { |
| 93 |
entry_nd = container_of(channel_nd->q.list.next, |
| 94 |
struct nd_cm_event, entry); |
| 95 |
event = &entry_nd->event; |
| 96 |
dlistfd_remove(channel_nd->q.list.next, &channel_nd->q); |
| 97 |
|
| 98 |
if (event->param.conn.private_data) { |
| 99 |
free((void *)event->param.conn.private_data); |
| 100 |
} |
| 101 |
free(event); |
| 102 |
} |
| 103 |
dlistfd_head_free(&channel_nd->q); |
| 104 |
|
| 105 |
free(channel_nd); |
| 106 |
} |
| 107 |
|
| 108 |
int rdma_create_id(struct rdma_event_channel *channel, struct rdma_cm_id **id, |
| 109 |
void *context, enum rdma_port_space ps) |
| 110 |
{ |
| 111 |
struct nd_cm_id *id_nd; |
| 112 |
|
| 113 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 114 |
|
| 115 |
if (!id) { |
| 116 |
errno = EINVAL; |
| 117 |
return -1; |
| 118 |
} |
| 119 |
if (ps != RDMA_PS_TCP) { |
| 120 |
errno = EINVAL; |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
id_nd = calloc(1, sizeof(*id_nd)); |
| 125 |
if (!id_nd) { |
| 126 |
errno = ENOMEM; |
| 127 |
return -1; |
| 128 |
} |
| 129 |
*id = &id_nd->id; |
| 130 |
|
| 131 |
(*id)->channel = channel; |
| 132 |
(*id)->context = context; |
| 133 |
(*id)->ps = ps; |
| 134 |
(*id)->qp_type = IBV_QPT_RC; |
| 135 |
|
| 136 |
id_nd->ov_file = INVALID_HANDLE_VALUE; |
| 137 |
ofi_atomic_initialize32(&id_nd->cm_events_pending, 0); |
| 138 |
|
| 139 |
ofi_mutex_init(&id_nd->connect_event.base.lock); |
| 140 |
pthread_cond_init(&id_nd->connect_event.base.cond, 0); |
| 141 |
id_nd->connect_event.base.event_cb = nd_cm_connect_ack; |
| 142 |
id_nd->connect_event.base.error_cb = nd_cm_connect_nack; |
| 143 |
id_nd->connect_event.id = *id; |
| 144 |
id_nd->connect_event.channel = channel; |
| 145 |
|
| 146 |
ofi_mutex_init(&id_nd->peer_event.base.lock); |
| 147 |
pthread_cond_init(&id_nd->peer_event.base.cond, 0); |
| 148 |
id_nd->peer_event.base.event_cb = nd_cm_connect_ack; |
| 149 |
id_nd->peer_event.base.error_cb = nd_cm_connect_nack; |
| 150 |
id_nd->peer_event.id = *id; |
| 151 |
id_nd->peer_event.channel = channel; |
| 152 |
|
| 153 |
ofi_mutex_init(&id_nd->listen_event.base.lock); |
| 154 |
pthread_cond_init(&id_nd->listen_event.base.cond, 0); |
| 155 |
id_nd->listen_event.base.event_cb = nd_cm_connreq_event; |
| 156 |
id_nd->listen_event.base.error_cb = nd_cm_connreq_error; |
| 157 |
id_nd->listen_event.listen_id = *id; |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
static void remove_cm_id_events_from_channel(struct rdma_cm_id *id) |
| 163 |
{ |
| 164 |
struct nd_cm_event *entry_nd; |
| 165 |
struct dlist_entry *tmp; |
| 166 |
struct nd_event_channel *ch_nd = |
| 167 |
container_of(id->channel, struct nd_event_channel, channel); |
| 168 |
|
| 169 |
dlist_foreach_container_safe(&ch_nd->q.list, struct nd_cm_event, |
| 170 |
entry_nd, entry, tmp) |
| 171 |
{ |
| 172 |
if (entry_nd->event.id == id) { |
| 173 |
dlistfd_remove(&entry_nd->entry, &ch_nd->q); |
| 174 |
free((void *)entry_nd->event.param.conn.private_data); |
| 175 |
free(&entry_nd->event); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
int rdma_destroy_id(struct rdma_cm_id *id) |
| 181 |
{ |
| 182 |
struct nd_cm_id *id_nd; |
| 183 |
int32_t compare; |
| 184 |
int ret = 0; |
| 185 |
ULONG refcnt; |
| 186 |
|
| 187 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 188 |
|
| 189 |
if (!id) { |
| 190 |
errno = EINVAL; |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 195 |
if (nd_cancel_pending(&id_nd->connect_event.base, |
| 196 |
(IND2Overlapped *)id_nd->connector) || |
| 197 |
nd_cancel_pending(&id_nd->peer_event.base, |
| 198 |
(IND2Overlapped *)id_nd->connector) || |
| 199 |
nd_cancel_pending(&id_nd->listen_event.base, |
| 200 |
(IND2Overlapped *)id_nd->listener)) |
| 201 |
return -1; |
| 202 |
|
| 203 |
while (compare = ofi_atomic_get32(&id_nd->cm_events_pending)) { |
| 204 |
WaitOnAddress(&id_nd->cm_events_pending, &compare, |
| 205 |
sizeof(id_nd->cm_events_pending), INFINITE); |
| 206 |
} |
| 207 |
|
| 208 |
if (id->channel) { |
| 209 |
remove_cm_id_events_from_channel(id); |
| 210 |
} |
| 211 |
|
| 212 |
if (id_nd->listener) { |
| 213 |
ret = id_nd->listener->lpVtbl->Release(id_nd->listener); |
| 214 |
assert(!ret); |
| 215 |
} |
| 216 |
if (id_nd->connector) { |
| 217 |
refcnt = id_nd->connector->lpVtbl->Release(id_nd->connector); |
| 218 |
assert(!refcnt); |
| 219 |
} |
| 220 |
|
| 221 |
if (id->qp) { |
| 222 |
ibv_destroy_qp(id->qp); |
| 223 |
id->qp = NULL; |
| 224 |
} |
| 225 |
// Should also destroy any associated SRQ here. |
| 226 |
|
| 227 |
if (id_nd->ov_file != INVALID_HANDLE_VALUE) { |
| 228 |
ret = CloseHandle(id_nd->ov_file); |
| 229 |
assert(ret); |
| 230 |
} |
| 231 |
|
| 232 |
pthread_cond_destroy(&id_nd->listen_event.base.cond); |
| 233 |
ofi_mutex_destroy(&id_nd->listen_event.base.lock); |
| 234 |
pthread_cond_destroy(&id_nd->peer_event.base.cond); |
| 235 |
ofi_mutex_destroy(&id_nd->peer_event.base.lock); |
| 236 |
pthread_cond_destroy(&id_nd->connect_event.base.cond); |
| 237 |
ofi_mutex_destroy(&id_nd->connect_event.base.lock); |
| 238 |
free(id_nd); |
| 239 |
|
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
int rdma_migrate_id(struct rdma_cm_id *id, struct rdma_event_channel *channel) |
| 244 |
{ |
| 245 |
struct nd_cm_id *id_nd; |
| 246 |
|
| 247 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 248 |
|
| 249 |
if ((!id) || (!channel)) { |
| 250 |
errno = EINVAL; |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
id->channel = channel; |
| 255 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 256 |
id_nd->connect_event.channel = channel; |
| 257 |
id_nd->peer_event.channel = channel; |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) |
| 262 |
{ |
| 263 |
char name[INET6_ADDRSTRLEN]; |
| 264 |
struct nd_adapter *adapter = NULL; |
| 265 |
int i; |
| 266 |
struct nd_cm_id *id_nd; |
| 267 |
HRESULT hr; |
| 268 |
int ret; |
| 269 |
|
| 270 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 271 |
|
| 272 |
if ((!id) || (!addr)) { |
| 273 |
errno = EINVAL; |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
if (!inet_ntop(addr->sa_family, ofi_get_ipaddr(addr), name, |
| 278 |
INET6_ADDRSTRLEN)) { |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
for (i = 0; i < nd_infra.adapters.count; ++i) { |
| 283 |
if (!strncmp(name, nd_infra.adapters.adapter[i].name, |
| 284 |
strlen(name)) || |
| 285 |
!strncmp(name, "0.0.0.0", strlen(name))) { |
| 286 |
id->verbs = nd_infra.adapters.context_list[i]; |
| 287 |
adapter = nd_infra.adapters.adapter_list[i]; |
| 288 |
id->route.addr.src_addr = *addr; |
| 289 |
break; |
| 290 |
} |
| 291 |
if (!strncmp(name, "127.0.0.1", strlen(name)) && |
| 292 |
(nd_infra.adapters.adapter[i].info.AdapterFlags & |
| 293 |
ND_ADAPTER_FLAG_LOOPBACK_CONNECTIONS_SUPPORTED)) { |
| 294 |
id->route.addr.src_addr = *addr; |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
if (i == nd_infra.adapters.count) { |
| 299 |
errno = ENODEV; |
| 300 |
return -1; |
| 301 |
} |
| 302 |
if (!adapter) { |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 307 |
hr = adapter->adapter->lpVtbl->CreateOverlappedFile(adapter->adapter, |
| 308 |
&id_nd->ov_file); |
| 309 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 310 |
FI_LOG_EP_CTRL, |
| 311 |
"IND2Adapter::CreateOverlappedFile: hr=0x%08lx\n", hr); |
| 312 |
if (FAILED(hr)) { |
| 313 |
errno = hresult2fi(hr); |
| 314 |
return -1; |
| 315 |
} |
| 316 |
|
| 317 |
assert(id_nd->ov_file && id_nd->ov_file != INVALID_HANDLE_VALUE); |
| 318 |
|
| 319 |
BindIoCompletionCallback(id_nd->ov_file, nd_io_cb, 0); |
| 320 |
|
| 321 |
hr = adapter->adapter->lpVtbl->CreateConnector( |
| 322 |
adapter->adapter, &IID_IND2Connector, id_nd->ov_file, |
| 323 |
(void **)&id_nd->connector); |
| 324 |
id_nd->listen_event.connector = id_nd->connector; |
| 325 |
id_nd->connect_event.connector = id_nd->connector; |
| 326 |
id_nd->peer_event.connector = id_nd->connector; |
| 327 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 328 |
FI_LOG_EP_CTRL, "IND2Adapter::CreateConnector: hr=0x%08lx\n", |
| 329 |
hr); |
| 330 |
if (FAILED(hr)) { |
| 331 |
ret = CloseHandle(id_nd->ov_file); |
| 332 |
assert(ret); |
| 333 |
errno = hresult2fi(hr); |
| 334 |
return -1; |
| 335 |
} |
| 336 |
|
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, |
| 341 |
struct sockaddr *dst_addr, int timeout_ms) |
| 342 |
{ |
| 343 |
struct nd_cm_id *id_nd; |
| 344 |
HRESULT hr; |
| 345 |
ULONG addrlen; |
| 346 |
|
| 347 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 348 |
|
| 349 |
if ((!id) || (!dst_addr)) { |
| 350 |
errno = EINVAL; |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
if (!id->verbs) { |
| 355 |
if (!src_addr) { |
| 356 |
if (nd_infra.adapters.count == 0) { |
| 357 |
errno = ENODEV; |
| 358 |
return -1; |
| 359 |
} |
| 360 |
src_addr = &nd_infra.adapters.adapter[0].address.addr; |
| 361 |
} |
| 362 |
|
| 363 |
if (rdma_bind_addr(id, src_addr)) { |
| 364 |
return -1; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 369 |
hr = id_nd->connector->lpVtbl->Bind(id_nd->connector, |
| 370 |
&id->route.addr.src_addr, |
| 371 |
sizeof(id->route.addr.src_addr)); |
| 372 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 373 |
FI_LOG_EP_CTRL, "IND2Connector::Bind: hr=0x%08lx\n", hr); |
| 374 |
if (FAILED(hr)) { |
| 375 |
errno = hresult2fi(hr); |
| 376 |
return -1; |
| 377 |
} |
| 378 |
|
| 379 |
addrlen = sizeof(id->route.addr.src_addr); |
| 380 |
hr = id_nd->connector->lpVtbl->GetLocalAddress( |
| 381 |
id_nd->connector, &id->route.addr.src_addr, &addrlen); |
| 382 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 383 |
FI_LOG_EP_CTRL, "IND2Connector::GetLocalAddress: hr=0x%08lx\n", |
| 384 |
hr); |
| 385 |
if (FAILED(hr)) { |
| 386 |
errno = hresult2fi(hr); |
| 387 |
return -1; |
| 388 |
} |
| 389 |
|
| 390 |
memcpy(&id->route.addr.dst_addr, dst_addr, ofi_sizeofaddr(dst_addr)); |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
| 395 |
{ |
| 396 |
struct nd_cm_event *entry_nd; |
| 397 |
struct nd_event_channel *ch_nd; |
| 398 |
|
| 399 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 400 |
|
| 401 |
if ((!id) || (!id->channel)) { |
| 402 |
errno = EINVAL; |
| 403 |
return -1; |
| 404 |
} |
| 405 |
|
| 406 |
entry_nd = calloc(1, sizeof(*entry_nd)); |
| 407 |
if (!entry_nd) { |
| 408 |
errno = ENOMEM; |
| 409 |
return -1; |
| 410 |
} |
| 411 |
|
| 412 |
entry_nd->event.id = id; |
| 413 |
entry_nd->event.listen_id = NULL; |
| 414 |
entry_nd->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
| 415 |
entry_nd->event.status = 0; |
| 416 |
|
| 417 |
ch_nd = container_of(id->channel, struct nd_event_channel, channel); |
| 418 |
dlistfd_insert_tail(&entry_nd->entry, &ch_nd->q); |
| 419 |
VRB_DBG(FI_LOG_EQ, "EQ SET event:%p %p %d\n", entry_nd->event.id, |
| 420 |
entry_nd->event.listen_id, entry_nd->event.event); |
| 421 |
|
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
int rdma_listen(struct rdma_cm_id *id, int backlog) |
| 426 |
{ |
| 427 |
struct nd_adapter *adapter; |
| 428 |
struct nd_cm_id *id_nd; |
| 429 |
HRESULT hr; |
| 430 |
ULONG addrlen; |
| 431 |
|
| 432 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 433 |
|
| 434 |
if (!id) { |
| 435 |
errno = EINVAL; |
| 436 |
return -1; |
| 437 |
} |
| 438 |
|
| 439 |
adapter = nd_get_adapter_by_context(id->verbs); |
| 440 |
if (!adapter) { |
| 441 |
errno = ENODEV; |
| 442 |
return -1; |
| 443 |
} |
| 444 |
|
| 445 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 446 |
if (id_nd->listener) { |
| 447 |
errno = EADDRINUSE; |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
hr = adapter->adapter->lpVtbl->CreateListener( |
| 452 |
adapter->adapter, &IID_IND2Listener, id_nd->ov_file, |
| 453 |
(void **)&id_nd->listener); |
| 454 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 455 |
FI_LOG_EP_CTRL, "IND2Adapter::CreateListener: hr=0x%08lx\n", hr); |
| 456 |
if (FAILED(hr)) { |
| 457 |
errno = hresult2fi(hr); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
hr = id_nd->listener->lpVtbl->Bind(id_nd->listener, |
| 462 |
&id->route.addr.src_addr, |
| 463 |
sizeof(id->route.addr.src_addr)); |
| 464 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 465 |
FI_LOG_EP_CTRL, "IND2Listener::Bind: hr=0x%08lx\n", hr); |
| 466 |
if (FAILED(hr)) { |
| 467 |
errno = hresult2fi(hr); |
| 468 |
goto err1; |
| 469 |
} |
| 470 |
|
| 471 |
addrlen = sizeof(id->route.addr.src_addr); |
| 472 |
hr = id_nd->listener->lpVtbl->GetLocalAddress( |
| 473 |
id_nd->listener, &id->route.addr.src_addr, &addrlen); |
| 474 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 475 |
FI_LOG_EP_CTRL, "IND2Listener::GetLocalAddress: hr=0x%08lx\n", |
| 476 |
hr); |
| 477 |
if (FAILED(hr)) { |
| 478 |
errno = hresult2fi(hr); |
| 479 |
goto err1; |
| 480 |
} |
| 481 |
|
| 482 |
hr = id_nd->listener->lpVtbl->Listen(id_nd->listener, backlog); |
| 483 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 484 |
FI_LOG_EP_CTRL, "IND2Listener::Listen: hr=0x%08lx\n", hr); |
| 485 |
if (FAILED(hr)) { |
| 486 |
errno = hresult2fi(hr); |
| 487 |
goto err1; |
| 488 |
} |
| 489 |
|
| 490 |
ofi_mutex_lock(&id_nd->listen_event.base.lock); |
| 491 |
++id_nd->listen_event.base.cb_pending; |
| 492 |
hr = id_nd->listener->lpVtbl->GetConnectionRequest( |
| 493 |
id_nd->listener, (IUnknown *)id_nd->connector, |
| 494 |
&id_nd->listen_event.base.ov); |
| 495 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 496 |
FI_LOG_EP_CTRL, |
| 497 |
"IND2Listener::GetConnectionRequest: hr=0x%08lx; ov=%p\n", hr, |
| 498 |
&id_nd->listen_event.base.ov); |
| 499 |
if (FAILED(hr)) { |
| 500 |
--id_nd->listen_event.base.cb_pending; |
| 501 |
errno = hresult2fi(hr); |
| 502 |
ofi_mutex_unlock(&id_nd->listen_event.base.lock); |
| 503 |
goto err1; |
| 504 |
} |
| 505 |
ofi_mutex_unlock(&id_nd->listen_event.base.lock); |
| 506 |
|
| 507 |
return 0; |
| 508 |
err1: |
| 509 |
id_nd->listener->lpVtbl->Release(id_nd->listener); |
| 510 |
id_nd->listener = NULL; |
| 511 |
return -1; |
| 512 |
} |
| 513 |
|
| 514 |
int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
| 515 |
{ |
| 516 |
struct nd_cm_id *id_nd; |
| 517 |
struct nd_qp *qp_nd; |
| 518 |
HRESULT hr; |
| 519 |
int ret = 0; |
| 520 |
|
| 521 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 522 |
|
| 523 |
if ((!id) || (!conn_param)) { |
| 524 |
errno = EINVAL; |
| 525 |
return -1; |
| 526 |
} |
| 527 |
|
| 528 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 529 |
qp_nd = container_of(id->qp, struct nd_qp, qp); |
| 530 |
|
| 531 |
id_nd->connect_event.type = ND_CM_CONNECT; |
| 532 |
|
| 533 |
ofi_mutex_lock(&id_nd->connect_event.base.lock); |
| 534 |
++id_nd->connect_event.base.cb_pending; |
| 535 |
hr = id_nd->connector->lpVtbl->Connect( |
| 536 |
id_nd->connector, (IUnknown *)qp_nd->nd2qp, |
| 537 |
&id->route.addr.dst_addr, sizeof(id->route.addr.dst_addr), |
| 538 |
conn_param->responder_resources, conn_param->initiator_depth, |
| 539 |
conn_param->private_data, conn_param->private_data_len, |
| 540 |
&id_nd->connect_event.base.ov); |
| 541 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 542 |
FI_LOG_EP_CTRL, "IND2Connector::Connect: hr=0x%08lx; ov=%p\n", |
| 543 |
hr, &id_nd->connect_event.base.ov); |
| 544 |
if (FAILED(hr)) { |
| 545 |
--id_nd->connect_event.base.cb_pending; |
| 546 |
errno = hresult2fi(hr); |
| 547 |
ret = -1; |
| 548 |
} |
| 549 |
ofi_mutex_unlock(&id_nd->connect_event.base.lock); |
| 550 |
|
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
| 555 |
{ |
| 556 |
struct nd_cm_id *id_nd; |
| 557 |
struct nd_qp *qp_nd; |
| 558 |
HRESULT hr; |
| 559 |
int ret = 0; |
| 560 |
|
| 561 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 562 |
|
| 563 |
if ((!id) || (!conn_param)) { |
| 564 |
errno = EINVAL; |
| 565 |
return -1; |
| 566 |
} |
| 567 |
|
| 568 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 569 |
qp_nd = container_of(id->qp, struct nd_qp, qp); |
| 570 |
|
| 571 |
id_nd->connect_event.type = ND_CM_ACCEPT; |
| 572 |
|
| 573 |
ofi_mutex_lock(&id_nd->connect_event.base.lock); |
| 574 |
++id_nd->connect_event.base.cb_pending; |
| 575 |
hr = id_nd->connector->lpVtbl->Accept( |
| 576 |
id_nd->connector, (IUnknown *)qp_nd->nd2qp, |
| 577 |
conn_param->responder_resources, conn_param->initiator_depth, |
| 578 |
conn_param->private_data, conn_param->private_data_len, |
| 579 |
&id_nd->connect_event.base.ov); |
| 580 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 581 |
FI_LOG_EP_CTRL, "IND2Connector::Accept: hr=0x%08lx; ov=%p\n", hr, |
| 582 |
&id_nd->connect_event.base.ov); |
| 583 |
if (FAILED(hr)) { |
| 584 |
--id_nd->connect_event.base.cb_pending; |
| 585 |
errno = hresult2fi(hr); |
| 586 |
ret = -1; |
| 587 |
} |
| 588 |
ofi_mutex_unlock(&id_nd->connect_event.base.lock); |
| 589 |
|
| 590 |
return ret; |
| 591 |
} |
| 592 |
|
| 593 |
int rdma_reject(struct rdma_cm_id *id, const void *private_data, |
| 594 |
uint8_t private_data_len) |
| 595 |
{ |
| 596 |
struct nd_cm_id *id_nd; |
| 597 |
HRESULT hr; |
| 598 |
|
| 599 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 600 |
|
| 601 |
if (!id) { |
| 602 |
errno = EINVAL; |
| 603 |
return -1; |
| 604 |
} |
| 605 |
|
| 606 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 607 |
hr = id_nd->connector->lpVtbl->Reject(id_nd->connector, private_data, |
| 608 |
private_data_len); |
| 609 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 610 |
FI_LOG_EP_CTRL, "IND2Connector::Reject: hr=0x%08lx\n", hr); |
| 611 |
if (FAILED(hr)) { |
| 612 |
errno = hresult2fi(hr); |
| 613 |
return -1; |
| 614 |
} |
| 615 |
|
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
int rdma_disconnect(struct rdma_cm_id *id) |
| 620 |
{ |
| 621 |
struct nd_cm_id *id_nd; |
| 622 |
HRESULT hr; |
| 623 |
int ret = 0; |
| 624 |
|
| 625 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 626 |
|
| 627 |
if (!id) { |
| 628 |
errno = EINVAL; |
| 629 |
return -1; |
| 630 |
} |
| 631 |
|
| 632 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 633 |
|
| 634 |
if (nd_cancel_pending(&id_nd->peer_event.base, |
| 635 |
(IND2Overlapped *)id_nd->connector)) |
| 636 |
return -1; |
| 637 |
|
| 638 |
ofi_mutex_lock(&id_nd->connect_event.base.lock); |
| 639 |
id_nd->connect_event.type = ND_CM_DISCONNECT; |
| 640 |
++id_nd->connect_event.base.cb_pending; |
| 641 |
hr = id_nd->connector->lpVtbl->Disconnect( |
| 642 |
id_nd->connector, &id_nd->connect_event.base.ov); |
| 643 |
FI_LOG(&vrb_prov, FAILED(hr) ? FI_LOG_WARN : FI_LOG_DEBUG, |
| 644 |
FI_LOG_EP_CTRL, "IND2Connector::Disconnect: hr=0x%08lx, ov=%p\n", |
| 645 |
hr, &id_nd->connect_event.base.ov); |
| 646 |
if (FAILED(hr)) { |
| 647 |
--id_nd->connect_event.base.cb_pending; |
| 648 |
errno = hresult2fi(hr); |
| 649 |
ret = -1; |
| 650 |
} |
| 651 |
ofi_mutex_unlock(&id_nd->connect_event.base.lock); |
| 652 |
|
| 653 |
return ret; |
| 654 |
} |
| 655 |
|
| 656 |
int rdma_get_cm_event(struct rdma_event_channel *channel, |
| 657 |
struct rdma_cm_event **event) |
| 658 |
{ |
| 659 |
struct nd_event_channel *ch_nd; |
| 660 |
char byte; |
| 661 |
int ret; |
| 662 |
struct nd_cm_event *entry_nd; |
| 663 |
struct nd_cm_id *id_nd; |
| 664 |
|
| 665 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 666 |
|
| 667 |
if ((!channel) || (!event)) { |
| 668 |
errno = EINVAL; |
| 669 |
return -1; |
| 670 |
} |
| 671 |
|
| 672 |
ch_nd = container_of(channel, struct nd_event_channel, channel); |
| 673 |
|
| 674 |
// Check to see if the channel fd has been set to |
| 675 |
// non-blocking mode and we would block. |
| 676 |
if ((ofi_recv_socket(ch_nd->channel.fd, &byte, 1, MSG_PEEK) == -1) && |
| 677 |
(WSAGetLastError() == WSAEWOULDBLOCK)) { |
| 678 |
errno = EAGAIN; |
| 679 |
return -1; |
| 680 |
} |
| 681 |
|
| 682 |
ret = dlistfd_wait_avail(&ch_nd->q, -1); |
| 683 |
if (ret != 1) { |
| 684 |
errno = ret; |
| 685 |
return -1; |
| 686 |
} |
| 687 |
entry_nd = container_of(ch_nd->q.list.next, struct nd_cm_event, entry); |
| 688 |
*event = &entry_nd->event; |
| 689 |
if (entry_nd->event.id) { |
| 690 |
id_nd = container_of(entry_nd->event.id, struct nd_cm_id, id); |
| 691 |
ofi_atomic_inc32(&id_nd->cm_events_pending); |
| 692 |
} |
| 693 |
VRB_DBG(FI_LOG_EQ, "EQ GET event:%p %p %d\n", entry_nd->event.id, |
| 694 |
entry_nd->event.listen_id, entry_nd->event.event); |
| 695 |
dlistfd_remove(ch_nd->q.list.next, &ch_nd->q); |
| 696 |
|
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
int rdma_ack_cm_event(struct rdma_cm_event *event) |
| 701 |
{ |
| 702 |
struct nd_cm_id *id_nd; |
| 703 |
|
| 704 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 705 |
|
| 706 |
if (!event) { |
| 707 |
errno = EINVAL; |
| 708 |
return -1; |
| 709 |
} |
| 710 |
|
| 711 |
if (event->id) { |
| 712 |
VRB_DBG(FI_LOG_EQ, "EQ ACK event:%p %p %d\n", event->id, |
| 713 |
event->listen_id, event->event); |
| 714 |
id_nd = container_of(event->id, struct nd_cm_id, id); |
| 715 |
ofi_atomic_dec32(&id_nd->cm_events_pending); |
| 716 |
WakeByAddressAll(&id_nd->cm_events_pending); |
| 717 |
} |
| 718 |
|
| 719 |
if (event->param.conn.private_data) { |
| 720 |
free((void *)event->param.conn.private_data); |
| 721 |
} |
| 722 |
free(event); |
| 723 |
|
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
int rdma_create_qp(struct rdma_cm_id *id, struct ibv_pd *pd, |
| 728 |
struct ibv_qp_init_attr *qp_init_attr) |
| 729 |
{ |
| 730 |
struct nd_cm_id *id_nd; |
| 731 |
struct nd_qp *qp_nd; |
| 732 |
|
| 733 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 734 |
|
| 735 |
if ((!id) || (!qp_init_attr)) { |
| 736 |
errno = EINVAL; |
| 737 |
return -1; |
| 738 |
} |
| 739 |
|
| 740 |
if ((!pd) && (!id->pd)) { |
| 741 |
errno = EINVAL; |
| 742 |
return -1; |
| 743 |
} |
| 744 |
|
| 745 |
if (!qp_init_attr->recv_cq || !qp_init_attr->send_cq) { |
| 746 |
errno = EINVAL; |
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
if (id->qp) { |
| 751 |
return 0; |
| 752 |
} |
| 753 |
|
| 754 |
if (pd) { |
| 755 |
id->pd = pd; |
| 756 |
} |
| 757 |
|
| 758 |
id->qp = ibv_create_qp(id->pd, qp_init_attr); |
| 759 |
if (!id->qp) { |
| 760 |
return -1; |
| 761 |
} |
| 762 |
id_nd = container_of(id, struct nd_cm_id, id); |
| 763 |
qp_nd = container_of(id->qp, struct nd_qp, qp); |
| 764 |
id_nd->connect_event.qp = qp_nd->nd2qp; |
| 765 |
id_nd->peer_event.qp = qp_nd->nd2qp; |
| 766 |
|
| 767 |
return 0; |
| 768 |
} |
| 769 |
|
| 770 |
void rdma_destroy_ep(struct rdma_cm_id *id) |
| 771 |
{ |
| 772 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 773 |
rdma_destroy_id(id); |
| 774 |
} |
| 775 |
|
| 776 |
__be16 rdma_get_src_port(struct rdma_cm_id *id) // Used with XRC |
| 777 |
{ |
| 778 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
const char *rdma_event_str(enum rdma_cm_event_type event) // Used with XRC |
| 783 |
{ |
| 784 |
VRB_TRACE(FI_LOG_FABRIC, "\n"); |
| 785 |
return NULL; |
| 786 |
} |