| 1 |
/* |
| 2 |
* Copyright (c) 2022 Canon Medical Research USA, Inc. All rights reserved. |
| 3 |
* Copyright (c) 2015-2016 Intel Corporation, Inc. All rights reserved. |
| 4 |
* |
| 5 |
* This software is available to you under a choice of one of two |
| 6 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 7 |
* General Public License (GPL) Version 2, available from the file |
| 8 |
* COPYING in the main directory of this source tree, or the |
| 9 |
* BSD license below: |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms, with or |
| 12 |
* without modification, are permitted provided that the following |
| 13 |
* conditions are met: |
| 14 |
* |
| 15 |
* - Redistributions of source code must retain the above |
| 16 |
* copyright notice, this list of conditions and the following |
| 17 |
* disclaimer. |
| 18 |
* |
| 19 |
* - Redistributions in binary form must reproduce the above |
| 20 |
* copyright notice, this list of conditions and the following |
| 21 |
* disclaimer in the documentation and/or other materials |
| 22 |
* provided with the distribution. |
| 23 |
* |
| 24 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 |
* SOFTWARE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <assert.h> |
| 35 |
#include <initguid.h> |
| 36 |
#include <guiddef.h> |
| 37 |
|
| 38 |
#include <ws2spi.h> |
| 39 |
#include "ndspi.h" |
| 40 |
#include "verbs_nd.h" |
| 41 |
|
| 42 |
/* Adapters must be sorted by nd_adapter::address. */ |
| 43 |
static size_t nd_remove_dups(struct nd_adapter *adapters, size_t num) |
| 44 |
{ |
| 45 |
struct nd_adapter *dst = adapters; |
| 46 |
struct nd_adapter *src = adapters + 1; |
| 47 |
|
| 48 |
size_t i; |
| 49 |
size_t n = 1; |
| 50 |
|
| 51 |
if (num < 2) |
| 52 |
return num; |
| 53 |
|
| 54 |
for (i = 1; i < num; i++) { |
| 55 |
if (!ofi_equals_ipaddr(&dst->address.addr, |
| 56 |
&src->address.addr)) { |
| 57 |
++dst; |
| 58 |
if (dst != src) |
| 59 |
memcpy(dst, src, sizeof(struct nd_adapter)); |
| 60 |
n++; |
| 61 |
} |
| 62 |
++src; |
| 63 |
} |
| 64 |
|
| 65 |
return n; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* nd_strerror generates a string message based on err value (GetLastError). |
| 70 |
* Returned string is valid until the next call of nd_strerror |
| 71 |
*/ |
| 72 |
static char *nd_strerror(DWORD err, HMODULE module) |
| 73 |
{ |
| 74 |
static char *message = NULL; |
| 75 |
size_t size; |
| 76 |
|
| 77 |
/* if message is allocated - free it */ |
| 78 |
if (message) |
| 79 |
LocalFree(message); |
| 80 |
|
| 81 |
size = FormatMessageA( |
| 82 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 83 |
FORMAT_MESSAGE_IGNORE_INSERTS | |
| 84 |
(module ? FORMAT_MESSAGE_FROM_HMODULE : 0), |
| 85 |
module, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 86 |
(LPSTR)&message, 0, NULL); |
| 87 |
|
| 88 |
return size ? message : (char *)""; |
| 89 |
} |
| 90 |
|
| 91 |
static char *nd_get_last_error_str(HRESULT hr, char *errmsg, |
| 92 |
SIZE_T max_msg_len) |
| 93 |
{ |
| 94 |
LPVOID lpMsgBuf; |
| 95 |
DWORD dw = (DWORD)hr; |
| 96 |
errno_t rc; |
| 97 |
|
| 98 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 99 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 100 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 101 |
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 102 |
(LPTSTR)&lpMsgBuf, 0, NULL); |
| 103 |
|
| 104 |
strcpy_s(errmsg, max_msg_len, "NTStatus: "); |
| 105 |
rc = strncat_s(errmsg, max_msg_len - strlen(errmsg), lpMsgBuf, |
| 106 |
_TRUNCATE); |
| 107 |
|
| 108 |
LocalFree(lpMsgBuf); |
| 109 |
|
| 110 |
return errmsg; |
| 111 |
} |
| 112 |
|
| 113 |
#define NDERR(err, str) \ |
| 114 |
case err: \ |
| 115 |
str = #err; \ |
| 116 |
break |
| 117 |
|
| 118 |
char *nd_error_str(HRESULT hr) |
| 119 |
{ |
| 120 |
static char lerr[128]; |
| 121 |
char *err_str = NULL; |
| 122 |
|
| 123 |
switch (hr) { |
| 124 |
NDERR(ND_SUCCESS, err_str); |
| 125 |
NDERR(ND_TIMEOUT, err_str); |
| 126 |
NDERR(ND_PENDING, err_str); |
| 127 |
NDERR(ND_BUFFER_OVERFLOW, err_str); |
| 128 |
NDERR(ND_DEVICE_BUSY, err_str); |
| 129 |
NDERR(ND_NO_MORE_ENTRIES, err_str); |
| 130 |
NDERR(ND_UNSUCCESSFUL, err_str); |
| 131 |
NDERR(ND_ACCESS_VIOLATION, err_str); |
| 132 |
NDERR(ND_INVALID_HANDLE, err_str); |
| 133 |
NDERR(ND_INVALID_DEVICE_REQUEST, err_str); |
| 134 |
NDERR(ND_INVALID_PARAMETER, err_str); |
| 135 |
NDERR(ND_NO_MEMORY, err_str); |
| 136 |
NDERR(ND_INVALID_PARAMETER_MIX, err_str); |
| 137 |
NDERR(ND_DATA_OVERRUN, err_str); |
| 138 |
NDERR(ND_SHARING_VIOLATION, err_str); |
| 139 |
NDERR(ND_INSUFFICIENT_RESOURCES, err_str); |
| 140 |
NDERR(ND_DEVICE_NOT_READY, err_str); |
| 141 |
NDERR(ND_IO_TIMEOUT, err_str); |
| 142 |
NDERR(ND_NOT_SUPPORTED, err_str); |
| 143 |
NDERR(ND_INTERNAL_ERROR, err_str); |
| 144 |
NDERR(ND_INVALID_PARAMETER_1, err_str); |
| 145 |
NDERR(ND_INVALID_PARAMETER_2, err_str); |
| 146 |
NDERR(ND_INVALID_PARAMETER_3, err_str); |
| 147 |
NDERR(ND_INVALID_PARAMETER_4, err_str); |
| 148 |
NDERR(ND_INVALID_PARAMETER_5, err_str); |
| 149 |
NDERR(ND_INVALID_PARAMETER_6, err_str); |
| 150 |
NDERR(ND_INVALID_PARAMETER_7, err_str); |
| 151 |
NDERR(ND_INVALID_PARAMETER_8, err_str); |
| 152 |
NDERR(ND_INVALID_PARAMETER_9, err_str); |
| 153 |
NDERR(ND_INVALID_PARAMETER_10, err_str); |
| 154 |
NDERR(ND_CANCELED, err_str); |
| 155 |
NDERR(ND_REMOTE_ERROR, err_str); |
| 156 |
NDERR(ND_INVALID_ADDRESS, err_str); |
| 157 |
NDERR(ND_INVALID_DEVICE_STATE, err_str); |
| 158 |
NDERR(ND_INVALID_BUFFER_SIZE, err_str); |
| 159 |
NDERR(ND_TOO_MANY_ADDRESSES, err_str); |
| 160 |
NDERR(ND_ADDRESS_ALREADY_EXISTS, err_str); |
| 161 |
NDERR(ND_CONNECTION_REFUSED, err_str); |
| 162 |
NDERR(ND_CONNECTION_INVALID, err_str); |
| 163 |
NDERR(ND_CONNECTION_ACTIVE, err_str); |
| 164 |
NDERR(ND_HOST_UNREACHABLE, err_str); |
| 165 |
NDERR(ND_CONNECTION_ABORTED, err_str); |
| 166 |
NDERR(ND_DEVICE_REMOVED, err_str); |
| 167 |
NDERR(ND_DISCONNECTED, err_str); |
| 168 |
default: |
| 169 |
err_str = nd_get_last_error_str(hr, lerr, sizeof(lerr)); |
| 170 |
if (err_str == NULL) { |
| 171 |
_snprintf(lerr, sizeof(lerr), "Unknown ND error %#08ld", |
| 172 |
hr); |
| 173 |
err_str = lerr; |
| 174 |
} |
| 175 |
break; |
| 176 |
} |
| 177 |
return err_str; |
| 178 |
} |
| 179 |
|
| 180 |
struct nd_infrastructure nd_infra = { 0 }; |
| 181 |
|
| 182 |
static void nd_free_infra() |
| 183 |
{ |
| 184 |
size_t i; |
| 185 |
int ret = 0; |
| 186 |
struct nd_adapter *adapter = NULL; |
| 187 |
struct nd_factory *factory = NULL; |
| 188 |
struct nd_module *module = NULL; |
| 189 |
|
| 190 |
if (nd_infra.adapters.count) { |
| 191 |
assert(nd_infra.adapters.adapter); |
| 192 |
for (i = 0; i < nd_infra.adapters.count; i++) { |
| 193 |
adapter = &nd_infra.adapters.adapter[i]; |
| 194 |
if (adapter->adapter) { |
| 195 |
// I'd like to have the following code, but cannot since it fails |
| 196 |
// because the ND provider dll has already been unloaded. |
| 197 |
// ret = adapter->adapter->lpVtbl->Release(adapter->adapter); |
| 198 |
// assert(!ret); |
| 199 |
ret = adapter->factory->provider->lpVtbl->Release( |
| 200 |
adapter->factory->provider); |
| 201 |
assert(ret == 1); |
| 202 |
adapter->adapter = NULL; |
| 203 |
} |
| 204 |
if (adapter->name) { |
| 205 |
free((void *)adapter->name); |
| 206 |
adapter->name = NULL; |
| 207 |
} |
| 208 |
} |
| 209 |
free(nd_infra.adapters.adapter); |
| 210 |
free(nd_infra.adapters.adapter_list); |
| 211 |
nd_infra.adapters.adapter = NULL; |
| 212 |
nd_infra.adapters.adapter_list = NULL; |
| 213 |
nd_infra.adapters.device_list = NULL; |
| 214 |
nd_infra.adapters.context_list = NULL; |
| 215 |
nd_infra.adapters.count = 0; |
| 216 |
} |
| 217 |
|
| 218 |
if (nd_infra.class_factories.count) { |
| 219 |
assert(nd_infra.class_factories.factory); |
| 220 |
for (i = 0; i < nd_infra.class_factories.count; i++) { |
| 221 |
factory = &nd_infra.class_factories.factory[i]; |
| 222 |
if (factory->provider) { |
| 223 |
ret = factory->provider->lpVtbl->Release( |
| 224 |
factory->provider); |
| 225 |
assert(!ret); |
| 226 |
factory->provider = NULL; |
| 227 |
} |
| 228 |
if (factory->class_factory) { |
| 229 |
ret = factory->class_factory->lpVtbl->Release( |
| 230 |
factory->class_factory); |
| 231 |
assert(!ret); |
| 232 |
factory->class_factory = NULL; |
| 233 |
} |
| 234 |
assert(factory->module); |
| 235 |
if (factory->addr_list) { |
| 236 |
free(factory->addr_list); |
| 237 |
factory->addr_list = NULL; |
| 238 |
} |
| 239 |
} |
| 240 |
free(nd_infra.class_factories.factory); |
| 241 |
nd_infra.class_factories.factory = NULL; |
| 242 |
nd_infra.class_factories.count = 0; |
| 243 |
} |
| 244 |
|
| 245 |
if (nd_infra.providers.count) { |
| 246 |
assert(nd_infra.providers.modules); |
| 247 |
for (i = 0; i < nd_infra.providers.count; i++) { |
| 248 |
module = &nd_infra.providers.modules[i]; |
| 249 |
assert(module->path); |
| 250 |
free((void *)module->path); |
| 251 |
} |
| 252 |
free(nd_infra.providers.modules); |
| 253 |
nd_infra.providers.modules = NULL; |
| 254 |
nd_infra.providers.count = 0; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static HRESULT nd_alloc_infra(size_t cnt) |
| 259 |
{ |
| 260 |
memset(&nd_infra, 0, sizeof(*(&nd_infra))); |
| 261 |
|
| 262 |
nd_infra.providers.modules = |
| 263 |
malloc(cnt * sizeof(*nd_infra.providers.modules)); |
| 264 |
if (!nd_infra.providers.modules) |
| 265 |
return ND_NO_MEMORY; |
| 266 |
|
| 267 |
nd_infra.class_factories.factory = |
| 268 |
malloc(cnt * sizeof(*nd_infra.class_factories.factory)); |
| 269 |
if (!nd_infra.class_factories.factory) { |
| 270 |
nd_free_infra(); |
| 271 |
return ND_NO_MEMORY; |
| 272 |
} |
| 273 |
|
| 274 |
return S_OK; |
| 275 |
} |
| 276 |
|
| 277 |
static wchar_t *nd_get_provider_path(const WSAPROTOCOL_INFOW *proto) |
| 278 |
{ |
| 279 |
int len = 0, lenex, err, res; |
| 280 |
wchar_t *prov, *provex; |
| 281 |
|
| 282 |
assert(proto); |
| 283 |
|
| 284 |
res = WSCGetProviderPath((GUID *)&proto->ProviderId, NULL, &len, &err); |
| 285 |
if (err != WSAEFAULT || !len) |
| 286 |
return NULL; |
| 287 |
|
| 288 |
prov = malloc(len * sizeof(*prov)); |
| 289 |
if (!prov) |
| 290 |
return NULL; |
| 291 |
|
| 292 |
res = WSCGetProviderPath((GUID *)&proto->ProviderId, prov, &len, &err); |
| 293 |
if (res) |
| 294 |
goto fn1; |
| 295 |
|
| 296 |
lenex = ExpandEnvironmentStringsW(prov, NULL, 0); |
| 297 |
if (!lenex) |
| 298 |
goto fn1; |
| 299 |
|
| 300 |
provex = malloc(lenex * sizeof(*provex)); |
| 301 |
if (!provex) |
| 302 |
goto fn1; |
| 303 |
|
| 304 |
lenex = ExpandEnvironmentStringsW(prov, provex, lenex); |
| 305 |
if (!lenex) |
| 306 |
goto fn2; |
| 307 |
|
| 308 |
free(prov); |
| 309 |
return provex; |
| 310 |
|
| 311 |
fn2: |
| 312 |
free(provex); |
| 313 |
fn1: |
| 314 |
free(prov); |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
#define ND_PROTO_FLAG \ |
| 319 |
(XP1_GUARANTEED_DELIVERY | XP1_GUARANTEED_ORDER | \ |
| 320 |
XP1_MESSAGE_ORIENTED | XP1_CONNECT_DATA) |
| 321 |
|
| 322 |
static int nd_is_valid_proto(const WSAPROTOCOL_INFOW *proto) |
| 323 |
{ |
| 324 |
assert(proto); |
| 325 |
|
| 326 |
if ((proto->dwServiceFlags1 & ND_PROTO_FLAG) != ND_PROTO_FLAG) |
| 327 |
return 0; |
| 328 |
|
| 329 |
if (!(proto->iAddressFamily == AF_INET || |
| 330 |
proto->iAddressFamily == AF_INET6)) |
| 331 |
return 0; |
| 332 |
|
| 333 |
if (proto->iSocketType != -1) |
| 334 |
return 0; |
| 335 |
|
| 336 |
if (proto->iProtocol || proto->iProtocolMaxOffset) |
| 337 |
return 0; |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
static struct nd_module *nd_search_module(const wchar_t *path) |
| 342 |
{ |
| 343 |
size_t i; |
| 344 |
size_t j; |
| 345 |
|
| 346 |
for (i = 0; i < nd_infra.providers.count; i++) { |
| 347 |
if (path && nd_file_exists(path) && |
| 348 |
!nd_is_directory(path)) { |
| 349 |
for (j = 0; j < nd_infra.providers.count; j++) { |
| 350 |
if (nd_is_same_file( |
| 351 |
path, nd_infra.providers.modules[j] |
| 352 |
.path)) { |
| 353 |
return &nd_infra.providers.modules[j]; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
|
| 361 |
static struct nd_module *nd_create_module(const wchar_t *path) |
| 362 |
{ |
| 363 |
struct nd_module *module; |
| 364 |
HMODULE hmodule; |
| 365 |
dll_can_unload_now unload; |
| 366 |
dll_get_class_object getclass; |
| 367 |
|
| 368 |
assert(nd_infra.providers.modules); |
| 369 |
|
| 370 |
module = nd_search_module(path); |
| 371 |
if (module) |
| 372 |
return module; |
| 373 |
|
| 374 |
/* ok, this is not duplicate. try to |
| 375 |
load it and get class factory*/ |
| 376 |
hmodule = LoadLibraryW(path); |
| 377 |
if (!hmodule) { |
| 378 |
VRB_WARN( |
| 379 |
FI_LOG_CORE, |
| 380 |
"nd_create_module: provider : %S, failed to load: %s\n", |
| 381 |
path, nd_strerror(GetLastError(), 0)); |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
unload = (dll_can_unload_now)GetProcAddress(hmodule, "DllCanUnloadNow"); |
| 386 |
getclass = (dll_get_class_object)GetProcAddress(hmodule, |
| 387 |
"DllGetClassObject"); |
| 388 |
if (!unload || !getclass) { |
| 389 |
VRB_WARN( |
| 390 |
FI_LOG_CORE, |
| 391 |
"nd_create_module: provider: %S, failed to import interface\n", |
| 392 |
path); |
| 393 |
goto fn_noiface; |
| 394 |
} |
| 395 |
|
| 396 |
module = &nd_infra.providers.modules[nd_infra.providers.count]; |
| 397 |
nd_infra.providers.count++; |
| 398 |
|
| 399 |
module->path = _wcsdup(path); |
| 400 |
module->module = hmodule; |
| 401 |
module->can_unload_now = unload; |
| 402 |
module->get_class_object = getclass; |
| 403 |
|
| 404 |
return module; |
| 405 |
|
| 406 |
fn_noiface: |
| 407 |
FreeLibrary(hmodule); |
| 408 |
return NULL; |
| 409 |
} |
| 410 |
|
| 411 |
#define ND_GUID_FORMAT \ |
| 412 |
"%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX" |
| 413 |
#define ND_GUID_ARG(guid) \ |
| 414 |
(guid).Data1, (guid).Data2, (guid).Data3, (guid).Data4[0], \ |
| 415 |
(guid).Data4[1], (guid).Data4[2], (guid).Data4[3], \ |
| 416 |
(guid).Data4[4], (guid).Data4[5], (guid).Data4[6], \ |
| 417 |
(guid).Data4[7] |
| 418 |
|
| 419 |
static HRESULT nd_create_factory(const WSAPROTOCOL_INFOW *proto) |
| 420 |
{ |
| 421 |
wchar_t *path; |
| 422 |
struct nd_module *module; |
| 423 |
IClassFactory *factory; |
| 424 |
HRESULT hr; |
| 425 |
struct nd_factory *ftr; |
| 426 |
|
| 427 |
assert(proto); |
| 428 |
assert(nd_is_valid_proto(proto)); |
| 429 |
assert(nd_infra.class_factories.factory); |
| 430 |
|
| 431 |
path = nd_get_provider_path(proto); |
| 432 |
if (path) |
| 433 |
VRB_INFO(FI_LOG_CORE, |
| 434 |
"nd_create_factory: provider " ND_GUID_FORMAT |
| 435 |
" path: %S \n", |
| 436 |
ND_GUID_ARG(proto->ProviderId), path); |
| 437 |
else /* can't get provider path. just return */ |
| 438 |
return S_OK; |
| 439 |
|
| 440 |
module = nd_create_module(path); |
| 441 |
free(path); |
| 442 |
if (!module) |
| 443 |
return S_OK; |
| 444 |
|
| 445 |
assert(module->get_class_object); |
| 446 |
hr = module->get_class_object(&proto->ProviderId, &IID_IClassFactory, |
| 447 |
(void **)&factory); |
| 448 |
if (FAILED(hr)) |
| 449 |
return hr; |
| 450 |
|
| 451 |
ftr = &nd_infra.class_factories.factory[nd_infra.class_factories.count]; |
| 452 |
nd_infra.class_factories.count++; |
| 453 |
ftr->class_factory = factory; |
| 454 |
ftr->module = module; |
| 455 |
ftr->protocol = *proto; |
| 456 |
|
| 457 |
return S_OK; |
| 458 |
} |
| 459 |
|
| 460 |
static int nd_adapter_cmp(const void *adapter1, const void *adapter2) |
| 461 |
{ |
| 462 |
return nd_addr_cmp(&((struct nd_adapter *)adapter1)->address, |
| 463 |
&((struct nd_adapter *)adapter2)->address); |
| 464 |
} |
| 465 |
|
| 466 |
static HRESULT nd_create_adapter(void) |
| 467 |
{ |
| 468 |
size_t addr_count = 0; |
| 469 |
HRESULT hr; |
| 470 |
struct nd_factory *factory = NULL; |
| 471 |
struct nd_adapter *adapter = NULL; |
| 472 |
ULONG listsize = 0; |
| 473 |
wchar_t *saddr; |
| 474 |
DWORD addrlen = 0; |
| 475 |
UINT64 id; |
| 476 |
int res; |
| 477 |
ULONG linfo = sizeof(adapter->info); |
| 478 |
ND2_ADAPTER_INFO *info = NULL; |
| 479 |
|
| 480 |
for (size_t i = 0; i < nd_infra.class_factories.count; i++) { |
| 481 |
factory = &nd_infra.class_factories.factory[i]; |
| 482 |
assert(factory->class_factory); |
| 483 |
|
| 484 |
hr = factory->class_factory->lpVtbl->CreateInstance( |
| 485 |
factory->class_factory, NULL, &IID_IND2Provider, |
| 486 |
(void **)&factory->provider); |
| 487 |
if (FAILED(hr)) |
| 488 |
goto err1; |
| 489 |
listsize = 0; |
| 490 |
hr = factory->provider->lpVtbl->QueryAddressList( |
| 491 |
factory->provider, NULL, &listsize); |
| 492 |
if (hr != ND_BUFFER_OVERFLOW) |
| 493 |
goto err1; |
| 494 |
if (!listsize) { |
| 495 |
factory->provider->lpVtbl->Release(factory->provider); |
| 496 |
factory->provider = NULL; |
| 497 |
continue; |
| 498 |
} |
| 499 |
|
| 500 |
factory->addr_list = malloc(listsize); |
| 501 |
if (!factory->addr_list) { |
| 502 |
hr = ND_NO_MEMORY; |
| 503 |
goto err1; |
| 504 |
} |
| 505 |
|
| 506 |
hr = factory->provider->lpVtbl->QueryAddressList( |
| 507 |
factory->provider, factory->addr_list, &listsize); |
| 508 |
if (FAILED(hr)) |
| 509 |
goto err1; |
| 510 |
|
| 511 |
for (size_t j = 0; j < factory->addr_list->iAddressCount; j++) { |
| 512 |
if (nd_is_valid_addr( |
| 513 |
factory->addr_list->Address[j].lpSockaddr)) |
| 514 |
addr_count++; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if (!addr_count) { |
| 519 |
hr = E_NOINTERFACE; |
| 520 |
goto err1; |
| 521 |
} |
| 522 |
|
| 523 |
nd_infra.adapters.adapter = |
| 524 |
malloc(addr_count * sizeof(*nd_infra.adapters.adapter)); |
| 525 |
if (!nd_infra.adapters.adapter) { |
| 526 |
hr = ND_NO_MEMORY; |
| 527 |
goto err1; |
| 528 |
} |
| 529 |
|
| 530 |
/* put all available valid addresses into common array */ |
| 531 |
for (size_t i = 0; i < nd_infra.class_factories.count; i++) { |
| 532 |
factory = &nd_infra.class_factories.factory[i]; |
| 533 |
for (size_t j = 0; j < factory->addr_list->iAddressCount; j++) { |
| 534 |
if (nd_is_valid_addr( |
| 535 |
factory->addr_list->Address[j].lpSockaddr)) { |
| 536 |
adapter = &nd_infra.adapters.adapter |
| 537 |
[nd_infra.adapters.count]; |
| 538 |
assert((int)sizeof(adapter->address) >= |
| 539 |
factory->addr_list->Address[j] |
| 540 |
.iSockaddrLength); |
| 541 |
memcpy(&adapter->address, |
| 542 |
factory->addr_list->Address[j].lpSockaddr, |
| 543 |
factory->addr_list->Address[j] |
| 544 |
.iSockaddrLength); |
| 545 |
adapter->factory = factory; |
| 546 |
nd_infra.adapters.count++; |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (!nd_infra.adapters.count) { |
| 552 |
hr = E_NOINTERFACE; |
| 553 |
goto err2; |
| 554 |
} |
| 555 |
|
| 556 |
/* sort adapters by addresses to set IP4 addresses first. then remove |
| 557 |
duplicates */ |
| 558 |
qsort(nd_infra.adapters.adapter, nd_infra.adapters.count, |
| 559 |
sizeof(struct nd_adapter), nd_addr_cmp); |
| 560 |
nd_infra.adapters.count = nd_remove_dups(nd_infra.adapters.adapter, |
| 561 |
nd_infra.adapters.count); |
| 562 |
|
| 563 |
// Allocate memory for adapter_list, device_list, and context_list all at once. |
| 564 |
nd_infra.adapters.adapter_list = |
| 565 |
calloc(nd_infra.adapters.count + 1, |
| 566 |
sizeof(*nd_infra.adapters.adapter_list) + |
| 567 |
sizeof(*nd_infra.adapters.device_list) + |
| 568 |
sizeof(*nd_infra.adapters.context_list)); |
| 569 |
if (!nd_infra.adapters.adapter_list) { |
| 570 |
hr = ND_NO_MEMORY; |
| 571 |
goto err2; |
| 572 |
} |
| 573 |
|
| 574 |
nd_infra.adapters.device_list = |
| 575 |
(struct ibv_device **)&nd_infra.adapters |
| 576 |
.adapter_list[nd_infra.adapters.count + 1]; |
| 577 |
nd_infra.adapters.context_list = |
| 578 |
(struct ibv_context **)&nd_infra.adapters |
| 579 |
.adapter_list[(nd_infra.adapters.count + 1) * 2]; |
| 580 |
|
| 581 |
for (size_t i = 0; i < nd_infra.adapters.count; i++) { |
| 582 |
adapter = &nd_infra.adapters.adapter[i]; |
| 583 |
nd_infra.adapters.adapter_list[i] = adapter; |
| 584 |
nd_infra.adapters.device_list[i] = &adapter->device; |
| 585 |
nd_infra.adapters.context_list[i] = &adapter->context; |
| 586 |
factory = adapter->factory; |
| 587 |
|
| 588 |
assert(factory); |
| 589 |
assert(factory->provider); |
| 590 |
|
| 591 |
assert(adapter->address.addr.sa_family == AF_INET || |
| 592 |
adapter->address.addr.sa_family == AF_INET6); |
| 593 |
|
| 594 |
hr = factory->provider->lpVtbl->ResolveAddress( |
| 595 |
factory->provider, &adapter->address.addr, |
| 596 |
(ULONG)ofi_sizeofaddr(&adapter->address.addr), &id); |
| 597 |
if (FAILED(hr)) |
| 598 |
goto err2; |
| 599 |
|
| 600 |
hr = factory->provider->lpVtbl->OpenAdapter( |
| 601 |
factory->provider, &IID_IND2Adapter, id, |
| 602 |
(void **)&adapter->adapter); |
| 603 |
if (FAILED(hr)) |
| 604 |
goto err2; |
| 605 |
|
| 606 |
adapter->info.InfoVersion = ND_VERSION_2; |
| 607 |
hr = adapter->adapter->lpVtbl->Query(adapter->adapter, |
| 608 |
&adapter->info, &linfo); |
| 609 |
if (FAILED(hr) && hr == ND_BUFFER_OVERFLOW) { |
| 610 |
info = malloc(linfo); |
| 611 |
if (!info) { |
| 612 |
hr = ND_NO_MEMORY; |
| 613 |
goto err2; |
| 614 |
} |
| 615 |
info->InfoVersion = ND_VERSION_2; |
| 616 |
hr = adapter->adapter->lpVtbl->Query(adapter->adapter, |
| 617 |
info, &linfo); |
| 618 |
if (!FAILED(hr)) |
| 619 |
adapter->info = *info; |
| 620 |
free(info); |
| 621 |
if (FAILED(hr)) |
| 622 |
goto err2; |
| 623 |
} else if (FAILED(hr)) |
| 624 |
goto err2; |
| 625 |
|
| 626 |
/* generate adapter's name */ |
| 627 |
addrlen = 0; |
| 628 |
res = WSAAddressToStringW( |
| 629 |
&adapter->address.addr, |
| 630 |
(ULONG)ofi_sizeofaddr(&adapter->address.addr), NULL, |
| 631 |
NULL, &addrlen); |
| 632 |
if (res == SOCKET_ERROR && WSAGetLastError() == WSAEFAULT && |
| 633 |
addrlen) { |
| 634 |
saddr = malloc((addrlen + 1) * sizeof(*saddr)); |
| 635 |
if (!saddr) { |
| 636 |
hr = ND_NO_MEMORY; |
| 637 |
goto err2; |
| 638 |
} |
| 639 |
WSAAddressToStringW( |
| 640 |
&adapter->address.addr, |
| 641 |
(ULONG)ofi_sizeofaddr(&adapter->address.addr), |
| 642 |
NULL, saddr, &addrlen); |
| 643 |
} else { |
| 644 |
saddr = _wcsdup(L"unknown"); |
| 645 |
} |
| 646 |
|
| 647 |
asprintf((char **)&adapter->name, "%S", saddr); |
| 648 |
free(saddr); |
| 649 |
|
| 650 |
adapter->device.transport_type = IBV_TRANSPORT_IB; |
| 651 |
strncpy(&adapter->device.name[0], adapter->name, |
| 652 |
min(strlen(adapter->name) + 1, |
| 653 |
sizeof(adapter->device.name))); |
| 654 |
adapter->device.name[IBV_SYSFS_NAME_MAX - 1] = 0; |
| 655 |
|
| 656 |
adapter->context.device = &adapter->device; |
| 657 |
adapter->context.num_comp_vectors = 0; |
| 658 |
} |
| 659 |
|
| 660 |
return S_OK; |
| 661 |
|
| 662 |
err2: |
| 663 |
if (!nd_infra.adapters.count) |
| 664 |
free(nd_infra.adapters.adapter); |
| 665 |
err1: |
| 666 |
nd_free_infra(); |
| 667 |
return hr; |
| 668 |
} |
| 669 |
|
| 670 |
static HRESULT nd_init() |
| 671 |
{ |
| 672 |
DWORD proto_len = 0; |
| 673 |
HRESULT hr = ND_INTERNAL_ERROR; |
| 674 |
int i, protonum, err; |
| 675 |
size_t prov_count = 0; |
| 676 |
WSAPROTOCOL_INFOW *proto = 0; |
| 677 |
int ret; |
| 678 |
|
| 679 |
memset(&nd_infra, 0, sizeof(nd_infra)); |
| 680 |
|
| 681 |
ret = WSCEnumProtocols(NULL, NULL, &proto_len, &err); |
| 682 |
if (ret != SOCKET_ERROR || err != WSAENOBUFS) { |
| 683 |
hr = ND_NO_MEMORY; |
| 684 |
goto fn_exit; |
| 685 |
} |
| 686 |
|
| 687 |
proto = malloc(proto_len); |
| 688 |
if (!proto) { |
| 689 |
hr = ND_NO_MEMORY; |
| 690 |
goto fn_exit; |
| 691 |
} |
| 692 |
|
| 693 |
protonum = WSCEnumProtocols(NULL, proto, &proto_len, &err); |
| 694 |
if (protonum == SOCKET_ERROR) { |
| 695 |
hr = ND_INTERNAL_ERROR; |
| 696 |
goto fn_protofail; |
| 697 |
} |
| 698 |
|
| 699 |
/* calculating number of valid protocols. this number is used |
| 700 |
as maximum of existing providers and class factories */ |
| 701 |
for (i = 0; i < protonum; i++) { |
| 702 |
if (nd_is_valid_proto(&proto[i])) |
| 703 |
prov_count++; |
| 704 |
} |
| 705 |
|
| 706 |
if (!prov_count) { |
| 707 |
hr = E_NOINTERFACE; |
| 708 |
goto fn_protofail; |
| 709 |
} |
| 710 |
|
| 711 |
hr = nd_alloc_infra(prov_count); |
| 712 |
if (hr != S_OK) |
| 713 |
goto fn_protofail; |
| 714 |
|
| 715 |
for (i = 0; i < protonum; i++) { |
| 716 |
if (nd_is_valid_proto(&proto[i])) |
| 717 |
nd_create_factory(&proto[i]); |
| 718 |
} |
| 719 |
|
| 720 |
/* ok, factories are created, now list all available addresses, try to |
| 721 |
create adapters & collect adapter's info */ |
| 722 |
hr = nd_create_adapter(); |
| 723 |
|
| 724 |
fn_protofail: |
| 725 |
free(proto); |
| 726 |
fn_exit: |
| 727 |
return hr; |
| 728 |
} |
| 729 |
|
| 730 |
static bool nd_startup_done = false; |
| 731 |
|
| 732 |
/* We don't need exclusive execution here because this function |
| 733 |
* is called from OFI init routine which is single thread |
| 734 |
*/ |
| 735 |
HRESULT nd_startup() |
| 736 |
{ |
| 737 |
WSADATA data; |
| 738 |
HRESULT hr; |
| 739 |
int ret; |
| 740 |
|
| 741 |
if (nd_startup_done) |
| 742 |
return S_OK; |
| 743 |
|
| 744 |
VRB_INFO(FI_LOG_CORE, "nd_startup: starting initialization\n"); |
| 745 |
|
| 746 |
ret = WSAStartup(MAKEWORD(2, 2), &data); |
| 747 |
if (ret) |
| 748 |
return HRESULT_FROM_WIN32(ret); |
| 749 |
|
| 750 |
VRB_INFO(FI_LOG_CORE, "nd_startup: WSAStartup complete\n"); |
| 751 |
|
| 752 |
hr = nd_init(); |
| 753 |
|
| 754 |
nd_startup_done = true; |
| 755 |
|
| 756 |
return hr; |
| 757 |
} |
| 758 |
|
| 759 |
void nd_shutdown() |
| 760 |
{ |
| 761 |
if (!nd_startup_done) |
| 762 |
return; |
| 763 |
|
| 764 |
VRB_INFO(FI_LOG_CORE, "nd_shutdown: shutdown WSA\n"); |
| 765 |
|
| 766 |
nd_free_infra(); |
| 767 |
nd_startup_done = false; |
| 768 |
|
| 769 |
WSACleanup(); |
| 770 |
} |