/
async_event1.c
229 lines (198 loc) · 6.29 KB
/
async_event1.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
/*
* Copyright (c) 2012 Dotan Barak - RDMAmojo. All rights reserved.
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, for any purpose,
* including, but not limited, to commercial and educational, as
* long as an acknowledgment appears in derived source files.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* async_event.c -- program that reads the async events and prints them.
*
* Compilation:
* gcc async_event.c -o async_event -libverbs
*
* Running:
* ./async_event -d <RDMA device>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <infiniband/verbs.h>
static struct ibv_context *get_device_context(const char *device_name)
{
struct ibv_device **device_list;
struct ibv_context *ctx = NULL;
int num_devices;
int i;
device_list = ibv_get_device_list(&num_devices);
if (!device_list) {
fprintf(stderr, "Error, ibv_get_device_list() failed\n");
return NULL;
}
for (i = 0; i < num_devices; ++ i) {
/* if this isn't the requested device */
if (strcmp(ibv_get_device_name(device_list[i]), device_name))
continue;
ctx = ibv_open_device(device_list[i]);
if (!ctx) {
fprintf(stderr, "Error, failed to open the device '%s'\n",
ibv_get_device_name(device_list[i]));
goto out;
}
printf("The device '%s' was detected\n", device_name);
break;
}
out:
ibv_free_device_list(device_list);
return ctx;
}
static void print_async_event(struct ibv_context *ctx, struct ibv_async_event *event)
{
switch (event->event_type) {
/* QP events */
case IBV_EVENT_QP_FATAL:
printf("QP fatal event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_QP_REQ_ERR:
printf("QP Requestor error for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_QP_ACCESS_ERR:
printf("QP access error event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_COMM_EST:
printf("QP communication established event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_SQ_DRAINED:
printf("QP Send Queue drained event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_PATH_MIG:
printf("QP Path migration loaded event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_PATH_MIG_ERR:
printf("QP Path migration error event for QP with handle %p\n", event->element.qp);
break;
case IBV_EVENT_QP_LAST_WQE_REACHED:
printf("QP last WQE reached event for QP with handle %p\n", event->element.qp);
break;
/* CQ events */
case IBV_EVENT_CQ_ERR:
printf("CQ error for CQ with handle %p\n", event->element.cq);
break;
/* SRQ events */
case IBV_EVENT_SRQ_ERR:
printf("SRQ error for SRQ with handle %p\n", event->element.srq);
break;
case IBV_EVENT_SRQ_LIMIT_REACHED:
printf("SRQ limit reached event for SRQ with handle %p\n", event->element.srq);
break;
/* Port events */
case IBV_EVENT_PORT_ACTIVE:
printf("Port active event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_PORT_ERR:
printf("Port error event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_LID_CHANGE:
printf("LID change event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_PKEY_CHANGE:
printf("P_Key table change event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_GID_CHANGE:
printf("GID table change event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_SM_CHANGE:
printf("SM change event for port number %d\n", event->element.port_num);
break;
case IBV_EVENT_CLIENT_REREGISTER:
printf("Client reregister event for port number %d\n", event->element.port_num);
break;
/* RDMA device events */
case IBV_EVENT_DEVICE_FATAL:
printf("Fatal error event for device %s\n", ibv_get_device_name(ctx->device));
break;
default:
printf("Unknown event (%d)\n", event->event_type);
}
}
static void usage(const char *argv0)
{
printf("Usage: %s print asynchronous events\n", argv0);
printf("\n");
printf("Options:\n");
printf(" -h print this help screen and exit\n");
printf(" -d <dev> use RDMA device <dev>\n");
}
int main(int argc, char *argv[])
{
char *device_name = NULL;
struct ibv_async_event event;
struct ibv_context *ctx;
int ret = 0;
/* parse command line options */
while (1) {
int c;
c = getopt(argc, argv, "d:h");
if (c == -1)
break;
switch (c) {
case 'd':
device_name = strdup(optarg);
if (!device_name) {
fprintf(stderr, "Error, failed to allocate memory for the device name\n");
return -1;
}
break;
case 'h':
usage(argv[0]);
exit(1);
default:
fprintf(stderr, "Bad command line was used\n\n");
usage(argv[0]);
exit(1);
}
}
if (!device_name) {
fprintf(stderr, "Error, the device name is mandatory\n");
return -1;
}
ctx = get_device_context(device_name);
if (!ctx) {
fprintf(stderr, "Error, the context of the device name '%s' could not be opened\n", device_name);
free(device_name);
return -1;
}
printf("Listening on events for the device '%s'\n", device_name);
while (1) {
/* wait for the next async event */
ret = ibv_get_async_event(ctx, &event);
if (ret) {
fprintf(stderr, "Error, ibv_get_async_event() failed\n");
goto out;
}
/* print the event */
print_async_event(ctx, &event);
/* ack the event */
ibv_ack_async_event(&event);
}
ret = 0;
out:
if (ibv_close_device(ctx)) {
fprintf(stderr, "Error, failed to close the context of the device '%s'\n", device_name);
return -1;
}
printf("The context of the device name '%s' was closed\n", device_name);
free(device_name);
return ret;
}