Source

mpi3-fortran / ompi / mca / common / verbs / common_verbs_find_ports.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2008 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006-2012 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2006-2012 Mellanox Technologies.  All rights reserved.
 * Copyright (c) 2006-2007 Los Alamos National Security, LLC.  All rights
 *                         reserved.
 * Copyright (c) 2006-2007 Voltaire All rights reserved.
 * Copyright (c) 2009      Sun Microsystems, Inc.  All rights reserved.
 * Copyright (c) 2009-2012 Oak Ridge National Laboratory.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "ompi_config.h"

#include <stdio.h>
#include <string.h>
#include <infiniband/verbs.h>
#include <stdint.h>

#include "opal_stdint.h"
#include "opal/types.h"
#include "opal/util/output.h"
#include "opal/util/argv.h"
#include "opal/class/opal_object.h"

#include "orte/util/show_help.h"
#include "orte/util/proc_info.h"
#include "orte/runtime/orte_globals.h"

#include "ompi/constants.h"

#include "common_verbs.h"

/***********************************************************************/

static void device_item_construct(ompi_common_verbs_device_item_t *di)
{
    di->device = NULL;
    di->device_name = NULL;
    di->context = NULL;
    di->destructor_free_context = true;
    memset(&di->device_attr, 0, sizeof(di->device_attr));
}


static void device_item_destruct(ompi_common_verbs_device_item_t *di)
{
    if (NULL != di->device_name) {
        free(di->device_name);
    }

    /* Only free the context if a) the device is open, and b) the
       upper layer didn't tell us not to */
    if (NULL != di->context && di->destructor_free_context) {
        ibv_close_device(di->context);
    }

    /* Zero out all the fields */
    device_item_construct(di);
}


OBJ_CLASS_INSTANCE(ompi_common_verbs_device_item_t,
                   opal_object_t,
                   device_item_construct,
                   device_item_destruct);

/***********************************************************************/

static void port_item_construct(ompi_common_verbs_port_item_t *pi)
{
    pi->device = NULL;
    pi->port_num = 0;
    memset(&pi->port_attr, 0, sizeof(pi->port_attr));
}


static void port_item_destruct(ompi_common_verbs_port_item_t *pi)
{
    OBJ_RELEASE(pi->device);
    /* Zero out all the fields */
    port_item_construct(pi);
}


OBJ_CLASS_INSTANCE(ompi_common_verbs_port_item_t,
                   opal_list_item_t,
                   port_item_construct,
                   port_item_destruct);

/***********************************************************************/

/*
 * Given a list of include or exclude items (never both), determine
 * whether we want the current port or not.
 */
static bool want_this_port(char **include_list, char **exclude_list, 
                           ompi_common_verbs_device_item_t *di, int port)
{
    int i;
    char name[1024];

    /* If we have no include or exclude list, then we unconditionally
       want the port */
    if (NULL == include_list && NULL == exclude_list) {
        return true;
    }

    /* Search the include list */
    if (NULL != include_list) {
        for (i = 0; NULL != include_list[i]; ++i) {
            /* First check if we can find the naked device name */
            if (strcmp(di->device_name, include_list[i]) == 0) {
                return true;
            }

            /* Now check for the specific port number */
            snprintf(name, sizeof(name), "%s:%d", di->device_name, port);
            if (strcmp(name, include_list[i]) == 0) {
                return true;
            }
        }

        /* Didn't find it.  So we don't want it. */
        return false;
    } 

    /* Search the exclude list */
    else {
        for (i = 0; NULL != exclude_list[i]; ++i) {
            /* First check if we can find the naked device name */
            if (strcmp(di->device_name, exclude_list[i]) == 0) {
                return false;
            }

            /* Now check for the specific port number */
            snprintf(name, sizeof(name), "%s:%d", di->device_name, port);
            if (strcmp(name, exclude_list[i]) == 0) {
                return false;
            }
        }

        /* Didn't find it.  So we want it. */
        return true;
    }

    /* Will never get here */
}

/***********************************************************************/

static const char *transport_name_to_str(enum ibv_transport_type transport_type)
{
    switch(transport_type) {
    case IBV_TRANSPORT_IB:      return "IB";
    case IBV_TRANSPORT_IWARP:   return "IWARP";
    case IBV_TRANSPORT_UNKNOWN: 
    default:                    return "unknown";
    }
}

#if defined(HAVE_IBV_LINK_LAYER_ETHERNET)
static const char *link_layer_to_str(int link_type)
{
    switch(link_type) {
    case IBV_LINK_LAYER_INFINIBAND:   return "IB";
    case IBV_LINK_LAYER_ETHERNET:     return "IWARP";
    case IBV_LINK_LAYER_UNSPECIFIED: 
    default:                          return "unspecified";
    }
}
#endif

/***********************************************************************/

static void check_sanity(char ***if_sanity_list, const char *dev_name, int port)
{
    int i;
    char tmp[BUFSIZ], **list = *if_sanity_list;
    const char *compare;

    if (NULL == if_sanity_list || NULL == *if_sanity_list) {
        return;
    }

    /* A match is found if:
       - "dev_name" is in the list and port == -1, or
       - "dev_name:port" is in the list
       If a match is found, remove that entry from the list. */
    memset(tmp, 0, sizeof(tmp));
    if (port > 0) {
        snprintf(tmp, sizeof(tmp) - 1, "%s:%d", dev_name, port);
        compare = tmp;
    } else {
        compare = dev_name;
    }

    for (i = 0; NULL != list[i]; ++i) {
        if (0 == strcmp(list[i], compare)) {
            int count = opal_argv_count(list);
            opal_argv_delete(&count, &list, i, 1);
            --i;
        }
    }
}

/***********************************************************************/

/*
 * Find a list of ibv_ports matching a set of criteria.
 */
opal_list_t *ompi_common_verbs_find_ports(const char *if_include, 
                                          const char *if_exclude, 
                                          int flags,
                                          int stream)
{
    int32_t num_devs;
    struct ibv_device **devices;
    struct ibv_device *device;
    struct ibv_context *device_context;
    struct ibv_device_attr device_attr;
    struct ibv_port_attr port_attr;
    char **if_include_list = NULL, **if_exclude_list = NULL, **if_sanity_list = NULL;
    ompi_common_verbs_device_item_t *di;
    ompi_common_verbs_port_item_t *pi;
    int rc;
    uint32_t i, j;
    opal_list_t *port_list = NULL;
    opal_list_item_t *item;
    bool want;

    /* Allocate a list to fill */
    port_list = OBJ_NEW(opal_list_t);
    if (NULL == port_list) {
        goto err_free_argv;
    }

    /* Sanity check the include/exclude params */
    if (NULL != if_include && NULL != if_exclude) {
        return port_list;
    } else if (NULL != if_include) {
        opal_output_verbose(5, stream, "finding verbs interfaces, including %s", 
                            if_include);
        if_include_list = opal_argv_split(if_include, ',');
        if_sanity_list = opal_argv_copy(if_include_list);
    } else if (NULL != if_exclude) {
        opal_output_verbose(5, stream, "finding verbs interfaces, excluding %s", 
                            if_exclude);
        if_exclude_list = opal_argv_split(if_exclude, ',');
        if_sanity_list = opal_argv_copy(if_exclude_list);
    }

    /* Query all the IBV devices on the machine.  Use an ompi
       compatibility function, because how to get this list changed
       over the history of the IBV API. */
    devices = ompi_ibv_get_device_list(&num_devs);
    if (0 == num_devs) {
        opal_output_verbose(5, stream, "no verbs interfaces found");
        goto err_free_argv;
    } else {
        opal_output_verbose(5, stream, "found %d verbs interface%s", 
                            num_devs, (num_devs != 1) ? "s" : "");
    }

    /* Now loop through all the devices.  Get the attributes for each
       port on each device to see if they match our selection
       criteria. */
    for (i = 0; (int32_t) i < num_devs; ++i) {
        /* See if this device is on the include/exclude sanity check
           list.  If it is, remove it from the sanity check list
           (i.e., we should end up with an empty list at the end if
           all entries in the sanity check list exist) */
        device = devices[i];
        check_sanity(&if_sanity_list, ibv_get_device_name(device), -1);

        device_context = ibv_open_device(device);
        opal_output_verbose(5, stream, "examining verbs interface: %s",
                            ibv_get_device_name(device));
        if (NULL == device_context) {
            orte_show_help("help-ompi-common-verbs.txt",
                           "ibv_open_device fail", true,
                           orte_process_info.nodename,
                           ibv_get_device_name(device),
                           errno, strerror(errno));
            goto err_free_port_list;
        }

        if (ibv_query_device(device_context, &device_attr)){
            orte_show_help("help-ompi-common-verbs.txt",
                           "ibv_query_device fail", true,
                           orte_process_info.nodename,
                           ibv_get_device_name(device),
                           errno, strerror(errno));
            goto err_free_port_list;
        }

        /* Now that we have the attributes of this device, remove all
           ports of this device from the sanity check list.  Note that
           IBV ports are indexed from 1, not 0. */
        for (j = 1; j <= device_attr.phys_port_cnt; j++) {
            check_sanity(&if_sanity_list, ibv_get_device_name(device), j);
        }

        /* Check the the device-specific flags to see if we want this
           device */
        want = true;
        if (flags & OMPI_COMMON_VERBS_FLAGS_TRANSPORT_IB &&
            IBV_TRANSPORT_IB != device->transport_type) {
            opal_output_verbose(5, stream, "verbs interface %s has wrong type (has %s, want IB)",
                                ibv_get_device_name(device),
                                transport_name_to_str(device->transport_type));
            want = false;
        }
        if (flags & OMPI_COMMON_VERBS_FLAGS_TRANSPORT_IWARP &&
            IBV_TRANSPORT_IWARP != device->transport_type) {
            opal_output_verbose(5, stream, "verbs interface %s has wrong type (has %s, want IWARP)",
                                ibv_get_device_name(device),
                                transport_name_to_str(device->transport_type));
            want = false;
        }

        /* Check for RC or UD QP support */
        if (flags & OMPI_COMMON_VERBS_FLAGS_RC ||
            flags & OMPI_COMMON_VERBS_FLAGS_UD) {
            rc = ompi_common_verbs_qp_test(device_context, flags);
            if (OMPI_ERR_TYPE_MISMATCH == rc) {
                want = false;
                opal_output_verbose(5, stream, 
                                    "verbs interface %s:%d: made an RC QP! we don't want RC-capable devices",
                                    ibv_get_device_name(device), j);
            } else if (OMPI_SUCCESS != rc) {
                want = false;
                opal_output_verbose(5, stream, 
                                    "verbs interface %s:%d: failed to make %s QP",
                                    ibv_get_device_name(device), j,
                                    ((flags & (OMPI_COMMON_VERBS_FLAGS_RC |
                                               OMPI_COMMON_VERBS_FLAGS_UD)) ==
                                     (OMPI_COMMON_VERBS_FLAGS_RC |
                                      OMPI_COMMON_VERBS_FLAGS_UD)) ? 
                                    "both UD and RC" :
                                    (flags & OMPI_COMMON_VERBS_FLAGS_RC) ? 
                                    "RC" : "UD");
            }
        }

        /* If we didn't want it, go to the next device */
        if (!want) {
            continue;
        }

        /* Make a device_item_t to hold the device information */
        di = OBJ_NEW(ompi_common_verbs_device_item_t);
        if (NULL == di) {
            goto err_free_port_list;
        }
        di->device = device;
        di->context = device_context;
        di->device_attr = device_attr;
        di->device_name = strdup(ibv_get_device_name(device));

        /* Note IBV ports are 1 based (not 0 based) */
        for (j = 1; j <= device_attr.phys_port_cnt; j++) {

            /* If we don't want this port (based on if_include /
               if_exclude lists), skip it */
            if (!want_this_port(if_include_list, if_exclude_list, di, j)) {
                opal_output_verbose(5, stream, "verbs interface %s:%d: rejected by include/exclude",
                                    ibv_get_device_name(device), j);
                continue;
            }

            /* Query the port */
            if (ibv_query_port(device_context, (uint8_t) j, &port_attr)) {
                orte_show_help("help-ompi-common-verbs.txt",
                               "ibv_query_port fail", true,
                               orte_process_info.nodename,
                               ibv_get_device_name(device),
                               errno, strerror(errno));
                goto err_free_port_list;
            }

            /* We definitely only want ACTIVE ports */
            if (IBV_PORT_ACTIVE != port_attr.state) {
                opal_output_verbose(5, stream, "verbs interface %s:%d: not ACTIVE",
                                    ibv_get_device_name(device), j);
                continue;
            }

            /* Check the port-specific flags to see if we want this
               port */
            want = false;
            if (0 == flags) {
                want = true;
            }

            if ((flags & (OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_IB |
                          OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_ETHERNET)) ==
                 (OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_IB |
                  OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_ETHERNET)) {
                /* If they specified both link layers, then we want this port */
                want = true;
            } else if ((flags & (OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_IB |
                                 OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_ETHERNET)) == 0) {
                /* If they specified neither link layer, then we want this port */
                want = true;
            } 
#if defined(HAVE_IBV_LINK_LAYER_ETHERNET)
            else if (flags & OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_IB) {
                if (IBV_LINK_LAYER_INFINIBAND == port_attr.link_layer) {
                    want = true;
                } else {
                    opal_output_verbose(5, stream, "verbs interface %s:%d has wrong link layer (has %s, want IB)",
                                        ibv_get_device_name(device), j,
                                        link_layer_to_str(port_attr.link_layer));
                }
            } else if (flags & OMPI_COMMON_VERBS_FLAGS_LINK_LAYER_ETHERNET) {
                if (IBV_LINK_LAYER_ETHERNET == port_attr.link_layer) {
                    want = true;
                } else {
                    opal_output_verbose(5, stream, "verbs interface %s:%d has wrong link layer (has %s, want Ethernet)",
                                        ibv_get_device_name(device), j,
                                        link_layer_to_str(port_attr.link_layer));
                }
            }
#endif

            if (!want) {
                continue;
            }

            /* If we got this far, we want the port.  Make an item for it. */
            pi = OBJ_NEW(ompi_common_verbs_port_item_t);
            if (NULL == pi) {
                goto err_free_port_list;
            }
            pi->device = di;            
            pi->port_num = j;
            pi->port_attr = port_attr;
            OBJ_RETAIN(di);

            /* Add the port item to the list */
            opal_list_append(port_list, &pi->super);
            opal_output_verbose(5, stream, "found acceptable verbs interface %s:%d",
                                ibv_get_device_name(device), j);
        }

        /* We're done with the device; if some ports are using it, its
           ref count will be > 0, and therefore the device won't be
           deleted here. */
        OBJ_RELEASE(di);
    }

    /* Sanity check that the devices specified in the if_include /
       if_exclude lists actually existed.  If this is true, then the
       sanity list will now be empty.  If there are still items left
       on the list, then they didn't exist.  Bad.  Print a warning (if
       the warning is not disabled). */
    if (0 != opal_argv_count(if_sanity_list)) {
        if (ompi_common_verbs_warn_nonexistent_if) {
            char *str = opal_argv_join(if_sanity_list, ',');
            orte_show_help("help-ompi-common-verbs.txt", "nonexistent port",
                           true, orte_process_info.nodename,
                           ((NULL != if_include) ? "in" : "ex"), str);
            free(str);

            /* Only warn once per process */
            ompi_common_verbs_warn_nonexistent_if = false;
        }
    }
    if (NULL != if_sanity_list) {
        opal_argv_free(if_sanity_list);
    }

    /* All done! */
    return port_list;

 err_free_port_list:
    for (item = opal_list_remove_first(port_list);
         item != NULL; 
         item = opal_list_remove_first(port_list)) {
        OBJ_RELEASE(item);
    }

 err_free_argv:
    if (NULL != if_sanity_list) {
        opal_argv_free(if_sanity_list);
        if_sanity_list = NULL;
    }
    opal_argv_free(if_include_list);
    if_include_list = NULL;
    opal_argv_free(if_exclude_list);
    if_exclude_list = NULL;

    return port_list;
}