forked from linux-rdma/rdma-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdma_rename.c
677 lines (578 loc) · 13 KB
/
rdma_rename.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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
/* Copyright (c) 2019, Mellanox Technologies. All rights reserved. See COPYING file */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <syslog.h>
#include <rdma/rdma_netlink.h>
#include <netlink/netlink.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <linux/pci_regs.h>
#include <util/rdma_nl.h>
/*
* Rename modes:
* NAME_FALLBACK - Try to name devices in the following order:
* by->onboard -> by-pci -> by-guid -> kernel
* NAME_KERNEL - leave name as kernel provided
* NAME_PCI - based on PCI/slot/function location
* NAME_GUID - based on node GUID
* NAME_ONBOARD - based on-board device index
* NAME_FIXED - rename the device to the fixed named in the next argument
*
* The stable names are combination of device type technology and rename mode.
* Infiniband - ib*
* RoCE - roce*
* iWARP - iw*
* OPA - opa*
* Default (unknown protocol) - rdma*
*
* Example:
* NAME_PCI
* pci = 0000:00:0c.4
* Device type = IB
* mlx5_0 -> ibp0s12f4
* NAME_GUID
* GUID = 5254:00c0:fe12:3455
* Device type = RoCE
* mlx5_0 -> rocex525400c0fe123455
* NAME_ONBOARD
* Index = 3
* Device type = OPA
* hfi1_1 -> opao3
*/
struct data {
const char *curr;
char *prefix;
uint64_t node_guid;
char *name;
int idx;
};
static bool debug_mode;
#define pr_err(args...) syslog(LOG_ERR, ##args)
#define pr_dbg(args...) \
do { \
if (debug_mode) \
syslog(LOG_ERR, ##args); \
} while (0)
#define ONBOARD_INDEX_MAX (16*1024-1)
static int by_onboard(struct data *d)
{
char *index = NULL;
char *acpi = NULL;
unsigned int o;
FILE *fp;
int ret;
/*
* ACPI_DSM - device specific method for naming
* PCI or PCI Express device
*/
ret = asprintf(&acpi, "/sys/class/infiniband/%s/device/acpi_index",
d->curr);
if (ret < 0)
return -ENOMEM;
/* SMBIOS type 41 - Onboard Devices Extended Information */
ret = asprintf(&index, "/sys/class/infiniband/%s/device/index", d->curr);
if (ret < 0) {
index = NULL;
ret = -ENOMEM;
goto out;
}
fp = fopen(acpi, "r");
if (!fp)
fp = fopen(index, "r");
if (!fp) {
pr_dbg("%s: Device is not embedded onboard\n", d->curr);
ret = -ENOENT;
goto out;
}
ret = fscanf(fp, "%u", &o);
fclose(fp);
/* https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L263 */
if (!ret || o > ONBOARD_INDEX_MAX) {
pr_err("%s: Onboard index %d and ret %d\n", d->curr, o, ret);
ret = -ENOENT;
goto out;
}
ret = asprintf(&d->name, "%so%u", d->prefix, o);
if (ret < 0) {
pr_err("%s: Failed to allocate name with prefix %s and onboard index %d\n",
d->curr, d->prefix, o);
ret = -ENOENT;
d->name = NULL;
goto out;
}
ret = 0;
out:
free(index);
free(acpi);
return ret;
}
static int find_sun(char *devname, char *pci)
{
char bof[256], tmp[256];
struct dirent *dent;
char *slots;
DIR *dir;
int ret;
ret = asprintf(&slots, "%s/subsystem/slots", devname);
if (ret < 0)
return 0;
ret = 0;
dir = opendir(slots);
if (!dir)
goto err_dir;
if (sscanf(pci, "%s.%s", bof, tmp) != 2)
goto out;
while ((dent = readdir(dir))) {
char *str, address[256];
FILE *fp;
int i;
if (dent->d_name[0] == '.')
continue;
i = atoi(dent->d_name);
if (i <= 0)
continue;
ret = asprintf(&str, "%s/%s/address", slots, dent->d_name);
if (ret < 0) {
ret = 0;
goto out;
}
fp = fopen(str, "r");
free(str);
if (!fp) {
ret = 0;
goto out;
}
ret = fscanf(fp, "%255s", address);
fclose(fp);
if (ret != 1) {
ret = 0;
goto out;
}
if (!strcmp(bof, address)) {
ret = i;
break;
}
}
out:
closedir(dir);
err_dir:
free(slots);
return ret;
}
static int is_pci_multifunction(char *devname)
{
char c[64] = {};
char *config;
FILE *fp;
int ret;
ret = asprintf(&config, "%s/config", devname);
if (ret < 0)
return 0;
fp = fopen(config, "r");
free(config);
if (!fp)
return 0;
ret = fread(c, 1, sizeof(c), fp);
fclose(fp);
if (ret != sizeof(c))
return 0;
/* bit 0-6 header type, bit 7 multi/single function device */
return c[PCI_HEADER_TYPE] & 0x80;
}
static int is_pci_ari_enabled(char *devname)
{
int ret, a;
char *ari;
FILE *fp;
ret = asprintf(&ari, "%s/ari_enabled", devname);
if (ret < 0)
return 0;
fp = fopen(ari, "r");
free(ari);
if (!fp)
return 0;
ret = fscanf(fp, "%d", &a);
fclose(fp);
return (ret) ? a == 1 : 0;
}
struct pci_info {
char *pcidev;
unsigned int domain;
unsigned int bus;
unsigned int slot;
unsigned int func;
unsigned int sun;
unsigned int vf;
bool valid_vf;
};
static int fill_pci_info(struct data *d, struct pci_info *p)
{
char buf[256] = {};
char *pci;
int ret;
ret = readlink(p->pcidev, buf, sizeof(buf)-1);
if (ret == -1 || ret == sizeof(buf))
return -EINVAL;
buf[ret] = 0;
pci = basename(buf);
/*
* pci = 0000:00:0c.0
*/
ret = sscanf(pci, "%x:%x:%x.%u", &p->domain, &p->bus, &p->slot,
&p->func);
if (ret != 4) {
pr_err("%s: Failed to read PCI BOF\n", d->curr);
return -ENOENT;
}
if (is_pci_ari_enabled(p->pcidev)) {
/*
* ARI devices support up to 256 functions on a single device
* ("slot"), and interpret the traditional 5-bit slot and 3-bit
* function number as a single 8-bit function number, where the
* slot makes up the upper 5 bits.
*
* https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L344
*/
p->func += p->slot * 8;
pr_dbg("%s: This is ARI device, new PCI BOF is %04x:%02x:%02x.%u\n",
d->curr, p->domain, p->bus, p->slot, p->func);
}
p->sun = find_sun(p->pcidev, pci);
return 0;
}
static int get_virtfn_info(struct data *d, struct pci_info *p)
{
struct pci_info vf = {};
char *physfn_pcidev;
struct dirent *dent;
DIR *dir;
int ret;
/* Check if this is a virtual function. */
ret = asprintf(&physfn_pcidev, "%s/physfn", p->pcidev);
if (ret < 0)
return -ENOMEM;
/* We are VF, get VF number and replace pcidev to point to PF */
dir = opendir(physfn_pcidev);
if (!dir) {
/*
* -ENOENT means that we are already in PF
* and pcidev points to right PCI.
*/
ret = (errno == ENOENT) ? 0 : -ENOMEM;
goto err_free;
}
p->valid_vf = true;
vf.pcidev = p->pcidev;
ret = fill_pci_info(d, &vf);
if (ret)
goto err_dir;
while ((dent = readdir(dir))) {
const char *s = "virtfn";
struct pci_info v = {};
if (strncmp(dent->d_name, s, strlen(s)) ||
strlen(dent->d_name) == strlen(s))
continue;
ret = asprintf(&v.pcidev, "%s/%s", physfn_pcidev, dent->d_name);
if (ret < 0) {
ret = -ENOMEM;
goto err_dir;
}
ret = fill_pci_info(d, &v);
free(v.pcidev);
if (ret) {
ret = -ENOMEM;
goto err_dir;
}
if (vf.func == v.func &&
vf.slot == v.slot) {
p->vf = atoi(&dent->d_name[6]);
break;
}
}
p->pcidev = physfn_pcidev;
closedir(dir);
return 0;
err_dir:
closedir(dir);
err_free:
free(physfn_pcidev);
return ret;
}
static int by_pci(struct data *d)
{
struct pci_info p = {};
char *subsystem;
char buf[256] = {};
char *subs;
int ret;
ret = asprintf(&subsystem, "/sys/class/infiniband/%s/device/subsystem",
d->curr);
if (ret < 0)
return -ENOMEM;
ret = readlink(subsystem, buf, sizeof(buf)-1);
if (ret == -1 || ret == sizeof(buf)) {
ret = -EINVAL;
goto out;
}
buf[ret] = 0;
subs = basename(buf);
if (strcmp(subs, "pci")) {
/* Ball out virtual devices */
pr_dbg("%s: Non-PCI device (%s) was detected\n", d->curr, subs);
ret = -EINVAL;
goto out;
}
/* Real devices */
ret = asprintf(&p.pcidev, "/sys/class/infiniband/%s/device", d->curr);
if (ret < 0) {
ret = -ENOMEM;
p.pcidev = NULL;
goto out;
}
ret = get_virtfn_info(d, &p);
if (ret)
goto out;
ret = fill_pci_info(d, &p);
if (ret) {
pr_err("%s: Failed to fill PCI device information\n", d->curr);
goto out;
}
d->name = calloc(256, sizeof(char));
if (!d->name) {
ret = -ENOMEM;
goto out;
}
ret = sprintf(d->name, "%s", d->prefix);
if (ret == -1) {
ret = -EINVAL;
goto out;
}
if (p.domain > 0) {
ret = sprintf(buf, "P%u", p.domain);
if (ret == -1) {
ret = -ENOMEM;
goto out;
}
strcat(d->name, buf);
}
if (p.sun > 0)
ret = sprintf(buf, "s%u", p.sun);
else
ret = sprintf(buf, "p%us%u", p.bus, p.slot);
if (ret == -1) {
ret = -ENOMEM;
goto out;
}
strcat(d->name, buf);
if (p.func > 0 || is_pci_multifunction(p.pcidev)) {
ret = sprintf(buf, "f%u", p.func);
if (ret == -1) {
ret = -ENOMEM;
goto out;
}
strcat(d->name, buf);
if (p.valid_vf) {
ret = sprintf(buf, "v%u", p.vf);
if (ret == -1) {
ret = -ENOMEM;
goto out;
}
strcat(d->name, buf);
}
}
ret = 0;
out:
free(p.pcidev);
free(subsystem);
if (ret) {
free(d->name);
d->name = NULL;
}
return ret;
}
static int by_guid(struct data *d)
{
uint16_t vp[4];
int ret = -1;
if (!d->node_guid)
/* virtual devices start without GUID */
goto out;
memcpy(vp, &d->node_guid, sizeof(uint64_t));
ret = asprintf(&d->name, "%sx%04x%04x%04x%04x", d->prefix, vp[3], vp[2],
vp[1], vp[0]);
out:
if (ret == -1) {
d->name = NULL;
return -ENOMEM;
}
return 0;
}
static int set_fixed_name(struct data *d, char *name)
{
int ret;
ret = asprintf(&d->name, "%s", name);
if (ret == -1) {
d->name = NULL;
return -ENOMEM;
}
return 0;
}
static int device_rename(struct nl_sock *nl, struct data *d)
{
struct nlmsghdr *hdr;
struct nl_msg *msg;
int ret = -1;
msg = nlmsg_alloc();
if (!msg)
return -ENOMEM;
hdr = nlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ,
RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_SET),
0, 0);
if (!hdr) {
ret = -ENOMEM;
goto nla_put_failure;
}
NLA_PUT_U32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, d->idx);
NLA_PUT_STRING(msg, RDMA_NLDEV_ATTR_DEV_NAME, d->name);
ret = nl_send_auto(nl, msg);
if (ret < 0)
return ret;
nla_put_failure:
nlmsg_free(msg);
return (ret < 0) ? ret : 0;
}
static int get_nldata_cb(struct nl_msg *msg, void *data)
{
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
struct nlmsghdr *hdr = nlmsg_hdr(msg);
struct data *d = data;
int ret;
ret = nlmsg_parse(hdr, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, rdmanl_policy);
if (ret < 0)
return NL_STOP;
if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
!tb[RDMA_NLDEV_ATTR_NODE_GUID])
return NL_STOP;
ret = strcmp(d->curr, nla_get_string(tb[RDMA_NLDEV_ATTR_DEV_NAME]));
if (ret)
return NL_OK;
if (tb[RDMA_NLDEV_ATTR_DEV_PROTOCOL])
d->prefix = strdup(
nla_get_string(tb[RDMA_NLDEV_ATTR_DEV_PROTOCOL]));
if (!d->prefix)
ret = asprintf(&d->prefix, "rdma");
if (ret < 0)
return NL_STOP;
d->idx = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
d->node_guid = nla_get_u64(tb[RDMA_NLDEV_ATTR_NODE_GUID]);
return NL_STOP;
}
enum name_policy {
NAME_KERNEL = 1 << 0,
NAME_PCI = 1 << 1,
NAME_GUID = 1 << 2,
NAME_ONBOARD = 1 << 3,
NAME_FIXED = 1 << 4,
NAME_ERROR = 1 << 8
};
static int str2policy(const char *np)
{
if (!strcmp(np, "NAME_KERNEL"))
return NAME_KERNEL;
if (!strcmp(np, "NAME_PCI"))
return NAME_PCI;
if (!strcmp(np, "NAME_GUID"))
return NAME_GUID;
if (!strcmp(np, "NAME_ONBOARD"))
return NAME_ONBOARD;
if (!strcmp(np, "NAME_FIXED"))
return NAME_FIXED;
if (!strcmp(np, "NAME_FALLBACK"))
return NAME_ONBOARD | NAME_PCI;
return NAME_ERROR;
};
int main(int argc, char **argv)
{
struct data d = { .idx = -1 };
struct nl_sock *nl;
int ret = -1;
int np, opt;
if (argc < 3)
goto err;
while ((opt = getopt(argc, argv, "v")) >= 0) {
switch (opt) {
case 'v':
debug_mode = true;
break;
default:
goto err;
}
}
argc -= optind;
argv += optind;
d.curr = argv[0];
np = str2policy(argv[1]);
if (np & NAME_ERROR) {
pr_err("%s: Unknown policy %s\n", d.curr, argv[1]);
goto err;
}
if (np & NAME_FIXED && argc < 3) {
pr_err("%s: No name specified\n", d.curr);
goto err;
}
pr_dbg("%s: Requested policy is %s\n", d.curr, argv[1]);
if (np & NAME_KERNEL) {
pr_dbg("%s: Leave kernel names, do nothing\n", d.curr);
/* Do nothing */
exit(0);
}
nl = rdmanl_socket_alloc();
if (!nl) {
pr_err("%s: Failed to allocate netlink socket\n", d.curr);
goto err;
}
if (rdmanl_get_devices(nl, get_nldata_cb, &d)) {
pr_err("%s: Failed to connect to NETLINK_RDMA\n", d.curr);
goto out;
}
if (d.idx == -1 || !d.prefix) {
pr_err("%s: Failed to get current device name and index\n",
d.curr);
goto out;
}
ret = -1;
if (np & NAME_ONBOARD)
ret = by_onboard(&d);
if (ret && (np & NAME_PCI))
ret = by_pci(&d);
if (ret && (np & NAME_GUID))
ret = by_guid(&d);
if (ret && (np & NAME_FIXED))
ret = set_fixed_name(&d, argv[2]);
if (ret)
goto out;
ret = device_rename(nl, &d);
if (ret) {
pr_err("%s: Device rename to %s failed with error %d\n", d.curr,
d.name, ret);
goto out;
}
pr_dbg("%s: Successfully renamed device to be %s\n", d.curr, d.name);
printf("%s\n", d.name);
free(d.name);
out:
free(d.prefix);
nl_socket_free(nl);
err:
ret = (ret) ? 1 : 0;
exit(ret);
}