Utils.cc 14.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// $Id$

/*************************************************************************
 * XDAQ Components for Distributed Data Acquisition                      *
 * Copyright (C) 2000-2014, CERN.                                        *
 * All rights reserved.                                                  *
 * Authors: L.Orsini, A.Petrucci, A.Forrest								 *
 * For the licensing terms see LICENSE.                                  *
 * For the list of contributors see CREDITS.                             *
 *************************************************************************/

#include "ibvla/Utils.h"

#include <infiniband/verbs.h>

#include <errno.h>
#include <string.h>
#include <sstream>

static struct ibv_device **devlist = 0;

22
std::list<ibvla::Device> ibvla::getDeviceList () 
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
#ifdef SUSPENDED
	int rc = ibv_fork_init();
	if (rc)
	{
		std::stringstream ss;
		ss << "Failure to initialize fork for ibv";
		XCEPT_RAISE(ibvla::exception::Exception, ss.str());
	}
#endif
	errno = 0;

	int num_devices;

	if (devlist == 0)
	{
		devlist = ibv_get_device_list(&num_devices);
	}
	if (devlist == 0)
	{
		std::stringstream ss;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		ss << "Failed to get device list: ";
		if (errno == EPERM)
		{
			ss << "Permission denied, " << strerror(errno) << "(" << errno << ")";
		}
		else if (errno == ENOMEM)
		{
			ss << "Insufficient memory to complete the operation, " << strerror(errno) << "(" << errno << ")";
		}
		else if (errno == ENOSYS)
		{
			ss << "No kernel support for RDMA, " << strerror(errno) << "(" << errno << ")";
		}
		else
		{
			ss << "Not understood error, errno = " << errno;
		}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		XCEPT_RAISE(ibvla::exception::Exception, ss.str());
	}

	//std::cout << "Copying struct then freeing. This might not work, as freeing them may make them unusable in the future." << std::endl;

	std::list<Device> devices;
	for (int i = 0; i < num_devices; i++)
	{
		ibvla::Device d(devlist[i]);
		devices.push_back(d);
	}

	return devices;
}

76
void ibvla::freeDeviceList (std::list<ibvla::Device> & dlist) 
77
78
79
80
81
82
83
84
85
86
87
88
89
{

	if (devlist != 0)
	{
		ibv_free_device_list(devlist);
		devlist = 0;
	}
	else
	{
		XCEPT_RAISE(ibvla::exception::Exception, "Invalid device list");
	}
}

90
ibvla::Context ibvla::createContext (ibvla::Device & d) 
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
	errno = 0;

	ibv_context *ctx = ibv_open_device(d.device_);

	if (ctx == 0)
	{
		std::stringstream ss;
		ss << "Could not create context for device '" << d.getName() << "', GUID = ' 0x" << std::hex << ntohll(d.getGUID()) << std::dec << "'";
		XCEPT_RAISE(ibvla::exception::Exception, ss.str());
	}

	return ibvla::Context(ctx);
}

106
void ibvla::destroyContext (ibvla::Context & c) 
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
{
	int i = ibv_close_device(c.context_);

	if (i == -1)
	{
		std::stringstream ss;
		ss << "Failure to destroy context";
		XCEPT_RAISE(ibvla::exception::Exception, ss.str());
	}
}

ibv_mtu ibvla::mtu_to_enum (unsigned mtu)
{
	switch (mtu)
	{
		case 256:
			return IBV_MTU_256;
		case 512:
			return IBV_MTU_512;
		case 1024:
			return IBV_MTU_1024;
		case 2048:
			return IBV_MTU_2048;
		case 4096:
			return IBV_MTU_4096;
		default:
			return IBV_MTU_4096;
	}
}

// returns a description of the event in relation to the actual event that occurred
std::string ibvla::toString (struct ibv_async_event *event)
{
	std::stringstream ss;
	switch (event->event_type)
	{
		/* QP events */
		case IBV_EVENT_QP_FATAL:
			ss << "QP fatal event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_QP_REQ_ERR:
			ss << "QP Requestor error for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_QP_ACCESS_ERR:
			ss << "QP access error event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_COMM_EST:
			ss << "QP communication established event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_SQ_DRAINED:
			ss << "QP Send Queue drained event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_PATH_MIG:
			ss << "QP Path migration loaded event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_PATH_MIG_ERR:
			ss << "QP Path migration error event for QP #" << event->element.qp->qp_num;
			break;
		case IBV_EVENT_QP_LAST_WQE_REACHED:
			ss << "QP last WQE reached event for QP #" << event->element.qp->qp_num;
			break;

			/* CQ events */
		case IBV_EVENT_CQ_ERR:
			ss << "CQ error for CQ with handle " << event->element.cq; // %p
			break;

			/* SRQ events */
		case IBV_EVENT_SRQ_ERR:
			ss << "SRQ error for SRQ with handle " << event->element.srq; // %p
			break;
		case IBV_EVENT_SRQ_LIMIT_REACHED:
			ss << "SRQ limit reached event for SRQ with handle " << event->element.srq; // %p
			break;

			/* Port events */
		case IBV_EVENT_PORT_ACTIVE:
			ss << "Port active event for port number " << event->element.port_num; // %d
			break;
		case IBV_EVENT_PORT_ERR:
			ss << "Port error event for port number " << event->element.port_num;
			break;
		case IBV_EVENT_LID_CHANGE:
			ss << "LID change event for port number " << event->element.port_num;
			break;
		case IBV_EVENT_PKEY_CHANGE:
			ss << "P_Key table change event for port number " << event->element.port_num;
			break;
		case IBV_EVENT_GID_CHANGE:
			ss << "GID table change event for port number " << event->element.port_num;
			break;
		case IBV_EVENT_SM_CHANGE:
			ss << "SM change event for port number " << event->element.port_num;
			break;
		case IBV_EVENT_CLIENT_REREGISTER:
			ss << "Client reregister event for port number " << event->element.port_num;
			break;

			/* RDMA device events */
		case IBV_EVENT_DEVICE_FATAL:
			ss << "Fatal error event for device (IBV_EVENT_DEVICE_FATAL)";
			break;

		default:
			ss << "Unknown event (" << event->event_type << ")";
	}

	return ss.str();
}

// returns a general description of the event
std::string ibvla::asyncEventToString (size_t event)
{
	std::stringstream ss;
	switch (event)
	{
		/* QP events */
		case IBV_EVENT_QP_FATAL:
			ss << "QP fatal event";
			break;
		case IBV_EVENT_QP_REQ_ERR:
			ss << "QP Requestor error";
			break;
		case IBV_EVENT_QP_ACCESS_ERR:
			ss << "QP access error";
			break;
		case IBV_EVENT_COMM_EST:
			ss << "QP communication established";
			break;
		case IBV_EVENT_SQ_DRAINED:
			ss << "QP Send Queue drained";
			break;
		case IBV_EVENT_PATH_MIG:
			ss << "QP Path migration loaded";
			break;
		case IBV_EVENT_PATH_MIG_ERR:
			ss << "QP Path migration error";
			break;
		case IBV_EVENT_QP_LAST_WQE_REACHED:
			ss << "QP last WQE reached";
			break;

			/* CQ events */
		case IBV_EVENT_CQ_ERR:
			ss << "CQ error"; // %p
			break;

			/* SRQ events */
		case IBV_EVENT_SRQ_ERR:
			ss << "SRQ error"; // %p
			break;
		case IBV_EVENT_SRQ_LIMIT_REACHED:
			ss << "SRQ limit reached"; // %p
			break;

			/* Port events */
		case IBV_EVENT_PORT_ACTIVE:
			ss << "Port active event"; // %d
			break;
		case IBV_EVENT_PORT_ERR:
			ss << "Port error event";
			break;
		case IBV_EVENT_LID_CHANGE:
			ss << "LID change event";
			break;
		case IBV_EVENT_PKEY_CHANGE:
			ss << "P_Key table change event";
			break;
		case IBV_EVENT_GID_CHANGE:
			ss << "GID table change event";
			break;
		case IBV_EVENT_SM_CHANGE:
			ss << "SM change event";
			break;
		case IBV_EVENT_CLIENT_REREGISTER:
			ss << "Client reregister event";
			break;

			/* RDMA device events */
		case IBV_EVENT_DEVICE_FATAL:
			ss << "Fatal error event for device";
			break;

		default:
			ss << "Unknown event";
	}

	return ss.str();
}

// returns the event name as documented (e.g. IBV_EVENT_CQ_ERR)
std::string ibvla::asyncEventToStringLiteral (size_t event)
{
	std::stringstream ss;
	switch (event)
	{
		/* QP events */
		case IBV_EVENT_QP_FATAL:
			ss << "IBV_EVENT_QP_FATAL"; // %p
			break;
		case IBV_EVENT_QP_REQ_ERR:
			ss << "IBV_EVENT_QP_REQ_ERR";
			break;
		case IBV_EVENT_QP_ACCESS_ERR:
			ss << "IBV_EVENT_QP_ACCESS_ERR";
			break;
		case IBV_EVENT_COMM_EST:
			ss << "IBV_EVENT_COMM_EST";
			break;
		case IBV_EVENT_SQ_DRAINED:
			ss << "IBV_EVENT_SQ_DRAINED";
			break;
		case IBV_EVENT_PATH_MIG:
			ss << "IBV_EVENT_PATH_MIG";
			break;
		case IBV_EVENT_PATH_MIG_ERR:
			ss << "IBV_EVENT_PATH_MIG_ERR";
			break;
		case IBV_EVENT_QP_LAST_WQE_REACHED:
			ss << "IBV_EVENT_QP_LAST_WQE_REACHED";
			break;

			/* CQ events */
		case IBV_EVENT_CQ_ERR:
			ss << "IBV_EVENT_CQ_ERR"; // %p
			break;

			/* SRQ events */
		case IBV_EVENT_SRQ_ERR:
			ss << "IBV_EVENT_SRQ_ERR"; // %p
			break;
		case IBV_EVENT_SRQ_LIMIT_REACHED:
			ss << "IBV_EVENT_SRQ_LIMIT_REACHED"; // %p
			break;

			/* Port events */
		case IBV_EVENT_PORT_ACTIVE:
			ss << "IBV_EVENT_PORT_ACTIVE"; // %d
			break;
		case IBV_EVENT_PORT_ERR:
			ss << "IBV_EVENT_PORT_ERR";
			break;
		case IBV_EVENT_LID_CHANGE:
			ss << "IBV_EVENT_LID_CHANGE";
			break;
		case IBV_EVENT_PKEY_CHANGE:
			ss << "IBV_EVENT_PKEY_CHANGE";
			break;
		case IBV_EVENT_GID_CHANGE:
			ss << "IBV_EVENT_GID_CHANGE";
			break;
		case IBV_EVENT_SM_CHANGE:
			ss << "IBV_EVENT_SM_CHANGE";
			break;
		case IBV_EVENT_CLIENT_REREGISTER:
			ss << "IBV_EVENT_CLIENT_REREGISTER";
			break;

			/* RDMA device events */
		case IBV_EVENT_DEVICE_FATAL:
			ss << "IBV_EVENT_DEVICE_FATAL";
			break;

		default:
			ss << "Unknown event";
	}

	return ss.str();
}

// returns a description of the work completion in relation to the actual event that occurred
std::string ibvla::toString (struct ibv_wc *wc)
{
	return workCompletionToString(wc->status); // returns a description
}

// returns a general description of the event
std::string ibvla::workCompletionToString (size_t event)
{
	//return ibv_wc_status_str((enum ibv_wc_status) event); // returns a description
	std::stringstream ss;
	switch (event)
	{
		case IBV_WC_LOC_LEN_ERR:
		{
			ss << "Posted receive buffer is not big enough for the incoming message";
		}
			break;
		case IBV_WC_LOC_QP_OP_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_LOC_EEC_OP_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_LOC_PROT_ERR:
		{
			ss << "Send block size is smaller than the data to be sent";
		}
			break;
		case IBV_WC_WR_FLUSH_ERR:
		{
			/*
			 This event is generated when an invalid remote error is thrown when the responder detects an
			 invalid request. It may be that the operation is not supported by the request queue or there is insufficient
			 buffer space to receive the request.
			 */
			// KNOWN CAUSES :
			// 1. buffer->sge_list.length != remote buffer->sge_list.length
			// 2. work requested posted to a queue pair for sending with inactive remote qp and a work request has already returned a IBV_WC_RETRY_EXC_ERR error
			ss << "Work request flushed from QP with error code";
		}
			break;
		case IBV_WC_MW_BIND_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_BAD_RESP_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_LOC_ACCESS_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_REM_INV_REQ_ERR:
		{
			ss << "The receive buffer is smaller than the incoming send";
		}
			break;
		case IBV_WC_REM_ACCESS_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_REM_OP_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_RETRY_EXC_ERR:
		{
			/*
			 This event is generated when a sender is unable to receive feedback from the receiver. This means
			 that either the receiver just never ACKs sender messages in a specified time period, or it has been
			 disconnected or it is in a bad state which prevents it from responding.
			 */
			// For us, probably remote application has terminated
			ss << "Failed to communicate with remote host, communication timed out";
		}
			break;
		case IBV_WC_RNR_RETRY_EXC_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_LOC_RDD_VIOL_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_REM_INV_RD_REQ_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_REM_ABORT_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_INV_EECN_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_INV_EEC_STATE_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_FATAL_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_RESP_TIMEOUT_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		case IBV_WC_GENERAL_ERR:
		{
			ss << ibv_wc_status_str((enum ibv_wc_status) event);
		}
			break;
		default:
		{
			ss << "Unknown error";
		}
			break;
	}
	return ss.str();
}

// returns the event name as documented (e.g. IBV_WC_LOC_LEN_ERR)
std::string ibvla::workCompletionToStringLiteral (size_t event)
{
	std::stringstream ss;
	switch (event)
	{
		case IBV_WC_LOC_LEN_ERR:
		{
			ss << "IBV_WC_LOC_LEN_ERR";
		}
			break;
		case IBV_WC_LOC_QP_OP_ERR:
		{
			ss << "IBV_WC_LOC_QP_OP_ERR";
		}
			break;
		case IBV_WC_LOC_EEC_OP_ERR:
		{
			ss << "IBV_WC_LOC_EEC_OP_ERR";
		}
			break;
		case IBV_WC_LOC_PROT_ERR:
		{
			ss << "IBV_WC_LOC_PROT_ERR";
		}
			break;
		case IBV_WC_WR_FLUSH_ERR:
		{
			ss << "IBV_WC_WR_FLUSH_ERR";
		}
			break;
		case IBV_WC_MW_BIND_ERR:
		{
			ss << "IBV_WC_MW_BIND_ERR";
		}
			break;
		case IBV_WC_BAD_RESP_ERR:
		{
			ss << "IBV_WC_BAD_RESP_ERR";
		}
			break;
		case IBV_WC_LOC_ACCESS_ERR:
		{
			ss << "IBV_WC_LOC_ACCESS_ERR";
		}
			break;
		case IBV_WC_REM_INV_REQ_ERR:
		{
			ss << "IBV_WC_REM_INV_REQ_ERR";
		}
			break;
		case IBV_WC_REM_ACCESS_ERR:
		{
			ss << "IBV_WC_REM_ACCESS_ERR";
		}
			break;
		case IBV_WC_REM_OP_ERR:
		{
			ss << "IBV_WC_REM_OP_ERR";
		}
			break;
		case IBV_WC_RETRY_EXC_ERR:
		{
			ss << "IBV_WC_RETRY_EXC_ERR";
		}
			break;
		case IBV_WC_RNR_RETRY_EXC_ERR:
		{
			ss << "IBV_WC_RETRY_EXC_ERR";
		}
			break;
		case IBV_WC_LOC_RDD_VIOL_ERR:
		{
			ss << "IBV_WC_LOC_RDD_VIOL_ERR";
		}
			break;
		case IBV_WC_REM_INV_RD_REQ_ERR:
		{
			ss << "IBV_WC_REM_INV_RD_REQ_ERR";
		}
			break;
		case IBV_WC_REM_ABORT_ERR:
		{
			ss << "IBV_WC_REM_ABORT_ERR";
		}
			break;
		case IBV_WC_INV_EECN_ERR:
		{
			ss << "IBV_WC_INV_EECN_ERR";
		}
			break;
		case IBV_WC_INV_EEC_STATE_ERR:
		{
			ss << "IBV_WC_INV_EEC_STATE_ERR";
		}
			break;
		case IBV_WC_FATAL_ERR:
		{
			ss << "IBV_WC_FATAL_ERR";
		}
			break;
		case IBV_WC_RESP_TIMEOUT_ERR:
		{
			ss << "IBV_WC_FATAL_ERR";
		}
			break;
		case IBV_WC_GENERAL_ERR:
		{
			ss << "IBV_WC_GENERAL_ERR";
		}
			break;
		default:
			ss << "Unknown event";
	}
	return ss.str();
}

// returns a string representation of the QP state
std::string ibvla::stateToString (ibvla::QueuePair & qp)
{
	switch (qp.qp_->state)
	{
		case IBV_QPS_RESET:
			return "Reset";
		case IBV_QPS_INIT:
			return "Initialized";
		case IBV_QPS_RTR:
			return "Ready To Receive";
		case IBV_QPS_RTS:
			return "Ready To Send";
		case IBV_QPS_SQD:
			return "Send Queue Drain";
		case IBV_QPS_SQE:
			return "Send Queue Error";
		case IBV_QPS_ERR:
			return "Error";
		default:
			return "Unknown";
	}
}