Changeset View
Changeset View
Standalone View
Standalone View
contrib/ofed/libibverbs/examples/asyncwatch.c
Show All 29 Lines | |||||
* SOFTWARE. | * SOFTWARE. | ||||
*/ | */ | ||||
#if HAVE_CONFIG_H | #if HAVE_CONFIG_H | ||||
# include <config.h> | # include <config.h> | ||||
#endif /* HAVE_CONFIG_H */ | #endif /* HAVE_CONFIG_H */ | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <endian.h> | |||||
#include <byteswap.h> | |||||
#include <getopt.h> | |||||
#include <string.h> | |||||
#include <infiniband/verbs.h> | #include <infiniband/verbs.h> | ||||
static const char *event_name_str(enum ibv_event_type event_type) | static const char *event_name_str(enum ibv_event_type event_type) | ||||
{ | { | ||||
switch (event_type) { | switch (event_type) { | ||||
case IBV_EVENT_DEVICE_FATAL: | case IBV_EVENT_DEVICE_FATAL: | ||||
return "IBV_EVENT_DEVICE_FATAL"; | return "IBV_EVENT_DEVICE_FATAL"; | ||||
Show All 23 Lines | static const char *event_name_str(enum ibv_event_type event_type) | ||||
case IBV_EVENT_SRQ_ERR: | case IBV_EVENT_SRQ_ERR: | ||||
case IBV_EVENT_SRQ_LIMIT_REACHED: | case IBV_EVENT_SRQ_LIMIT_REACHED: | ||||
case IBV_EVENT_QP_LAST_WQE_REACHED: | case IBV_EVENT_QP_LAST_WQE_REACHED: | ||||
default: | default: | ||||
return "unexpected"; | return "unexpected"; | ||||
} | } | ||||
} | } | ||||
static void usage(const char *argv0) | |||||
{ | |||||
printf("Usage:\n"); | |||||
printf(" %s start an asyncwatch process\n", argv0); | |||||
printf("\n"); | |||||
printf("Options:\n"); | |||||
printf(" -d, --ib-dev=<dev> use IB device <dev> (default first device found)\n"); | |||||
} | |||||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||||
{ | { | ||||
struct ibv_device **dev_list; | struct ibv_device **dev_list; | ||||
struct ibv_context *context; | struct ibv_context *context; | ||||
struct ibv_async_event event; | struct ibv_async_event event; | ||||
char *ib_devname = NULL; | |||||
int i = 0; | |||||
/* Force line-buffering in case stdout is redirected */ | /* Force line-buffering in case stdout is redirected */ | ||||
setvbuf(stdout, NULL, _IOLBF, 0); | setvbuf(stdout, NULL, _IOLBF, 0); | ||||
while (1) { | |||||
int c; | |||||
static struct option long_options[] = { | |||||
{ .name = "ib-dev", .has_arg = 1, .val = 'd' }, | |||||
{ .name = "help", .has_arg = 0, .val = 'h' }, | |||||
{ 0 } | |||||
}; | |||||
c = getopt_long(argc, argv, "d:h", long_options, NULL); | |||||
if (c == -1) | |||||
break; | |||||
switch (c) { | |||||
case 'd': | |||||
ib_devname = strdupa(optarg); | |||||
break; | |||||
case 'h': | |||||
/* fall through */ | |||||
default: | |||||
usage(argv[0]); | |||||
return 1; | |||||
} | |||||
} | |||||
dev_list = ibv_get_device_list(NULL); | dev_list = ibv_get_device_list(NULL); | ||||
if (!dev_list) { | if (!dev_list) { | ||||
perror("Failed to get IB devices list"); | perror("Failed to get IB devices list"); | ||||
return 1; | return 1; | ||||
} | } | ||||
if (ib_devname) { | |||||
for (; dev_list[i]; ++i) { | |||||
if (!strcmp(ibv_get_device_name(dev_list[i]), ib_devname)) | |||||
break; | |||||
} | |||||
} | |||||
if (!*dev_list) { | if (!dev_list[i]) { | ||||
fprintf(stderr, "No IB devices found\n"); | fprintf(stderr, "IB device %s not found\n", | ||||
ib_devname ? ib_devname : ""); | |||||
return 1; | return 1; | ||||
} | } | ||||
context = ibv_open_device(*dev_list); | context = ibv_open_device(dev_list[i]); | ||||
if (!context) { | if (!context) { | ||||
fprintf(stderr, "Couldn't get context for %s\n", | fprintf(stderr, "Couldn't get context for %s\n", | ||||
ibv_get_device_name(*dev_list)); | ibv_get_device_name(dev_list[i])); | ||||
return 1; | return 1; | ||||
} | } | ||||
printf("%s: async event FD %d\n", | printf("%s: async event FD %d\n", | ||||
ibv_get_device_name(*dev_list), context->async_fd); | ibv_get_device_name(dev_list[i]), context->async_fd); | ||||
while (1) { | while (1) { | ||||
if (ibv_get_async_event(context, &event)) | if (ibv_get_async_event(context, &event)) | ||||
return 1; | return 1; | ||||
printf(" event_type %s (%d), port %d\n", | printf(" event_type %s (%d), port %d\n", | ||||
event_name_str(event.event_type), | event_name_str(event.event_type), | ||||
event.event_type, event.element.port_num); | event.event_type, event.element.port_num); | ||||
ibv_ack_async_event(&event); | ibv_ack_async_event(&event); | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } |