1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 * Predictable network interface device names based on:
5 * - firmware/bios-provided index numbers for on-board devices
6 * - firmware-provided pci-express hotplug slot index number
7 * - physical/geographical location of the hardware
8 * - the interface's MAC address
10 * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames
12 * Two character prefixes based on the type of interface:
15 * sl — serial line IP (slip)
20 * b<number> — BCMA bus core number
21 * c<bus_id> — bus id of a grouped CCW or CCW device,
22 * with all leading zeros stripped [s390]
23 * o<index>[n<phys_port_name>|d<dev_port>]
24 * — on-board device index number
25 * s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
26 * — hotplug slot index number
27 * x<MAC> — MAC address
28 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
29 * — PCI geographical location
30 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
31 * — USB port number chain
32 * v<slot> - VIO slot number (IBM PowerVM)
33 * a<vendor><model>i<instance> — Platform bus ACPI instance id
35 * All multi-function PCI devices will carry the [f<function>] number in the
36 * device name, including the function 0 device.
38 * SR-IOV virtual devices are named based on the name of the parent interface,
39 * with a suffix of "v<N>", where <N> is the virtual device number.
41 * When using PCI geography, The PCI domain is only prepended when it is not 0.
43 * For USB devices the full chain of port numbers of hubs is composed. If the
44 * name gets longer than the maximum number of 15 characters, the name is not
46 * The usual USB configuration == 1 and interface == 0 values are suppressed.
48 * PCI Ethernet card with firmware index "1":
49 * ID_NET_NAME_ONBOARD=eno1
50 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
52 * PCI Ethernet card in hotplug slot with firmware index number:
53 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
54 * ID_NET_NAME_MAC=enx000000000466
55 * ID_NET_NAME_PATH=enp5s0
56 * ID_NET_NAME_SLOT=ens1
58 * PCI Ethernet multi-function card with 2 ports:
59 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
60 * ID_NET_NAME_MAC=enx78e7d1ea46da
61 * ID_NET_NAME_PATH=enp2s0f0
62 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
63 * ID_NET_NAME_MAC=enx78e7d1ea46dc
64 * ID_NET_NAME_PATH=enp2s0f1
67 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
68 * ID_NET_NAME_MAC=wlx0024d7e31130
69 * ID_NET_NAME_PATH=wlp3s0
71 * PCI IB host adapter with 2 ports:
72 * /sys/devices/pci0000:00/0000:00:03.0/0000:15:00.0/net/ibp21s0f0
73 * ID_NET_NAME_PATH=ibp21s0f0
74 * /sys/devices/pci0000:00/0000:00:03.0/0000:15:00.1/net/ibp21s0f1
75 * ID_NET_NAME_PATH=ibp21s0f1
77 * USB built-in 3G modem:
78 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
79 * ID_NET_NAME_MAC=wwx028037ec0200
80 * ID_NET_NAME_PATH=wwp0s29u1u4i6
83 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
84 * ID_NET_NAME_MAC=enxd626b3450fb5
85 * ID_NET_NAME_PATH=enp0s29u1u2
87 * s390 grouped CCW interface:
88 * /sys/devices/css0/0.0.0007/0.0.f5f0/group_device/net/encf5f0
89 * ID_NET_NAME_MAC=enx026d3c00000a
90 * ID_NET_NAME_PATH=encf5f0
96 #include <net/if_arp.h>
102 #include <linux/pci_regs.h>
104 #include "alloc-util.h"
105 #include "dirent-util.h"
109 #include "parse-util.h"
110 #include "proc-cmdline.h"
111 #include "stdio-util.h"
112 #include "string-util.h"
114 #include "strxcpyx.h"
115 #include "udev-builtin.h"
117 #define ONBOARD_INDEX_MAX (16*1024-1)
119 /* So here's the deal: net_id is supposed to be an excercise in providing stable names for network devices. However, we
120 * also want to keep updating the naming scheme used in future versions of net_id. These two goals of course are
121 * contradictory: on one hand we want things to not change and on the other hand we want them to improve. Our way out
122 * of this dilemma is to introduce the "naming scheme" concept: each time we improve the naming logic we define a new
123 * flag for it. Then, we keep a list of schemes, each identified by a name associated with the flags it implements. Via
124 * a kernel command line and environment variable we then allow the user to pick the scheme they want us to follow:
125 * installers could "freeze" the used scheme at the moment of installation this way.
127 * Developers: each time you tweak the naming logic here, define a new flag below, and condition the tweak with
128 * it. Each time we do a release we'll then add a new scheme entry and include all newly defined flags.
130 * Note that this is only half a solution to the problem though: not only udev/net_id gets updated all the time, the
131 * kernel gets too. And thus a kernel that previously didn't expose some sysfs attribute we look for might eventually
132 * do, and thus affect our naming scheme too. Thus, enforcing a naming scheme will make interfacing more stable across
133 * OS versions, but not fully stabilize them. */
134 typedef enum NamingSchemeFlags
{
135 /* First, the individual features */
136 NAMING_SR_IOV_V
= 1 << 0, /* Use "v" suffix for SR-IOV, see 609948c7043a40008b8299529c978ed8e11de8f6*/
137 NAMING_NPAR_ARI
= 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
138 NAMING_INFINIBAND
= 1 << 2, /* Use "ib" prefix for infiniband, see 938d30aa98df887797c9e05074a562ddacdcdf5e */
139 NAMING_ZERO_ACPI_INDEX
= 1 << 3, /* Allow zero acpi_index field, see d81186ef4f6a888a70f20a1e73a812d6acb9e22f */
141 /* And now the masks that combine the features above */
143 NAMING_V239
= NAMING_V238
|NAMING_SR_IOV_V
|NAMING_NPAR_ARI
,
144 NAMING_V240
= NAMING_V239
|NAMING_INFINIBAND
|NAMING_ZERO_ACPI_INDEX
,
146 _NAMING_SCHEME_FLAGS_INVALID
= -1,
149 typedef struct NamingScheme
{
151 NamingSchemeFlags flags
;
154 static const NamingScheme naming_schemes
[] = {
155 { "v238", NAMING_V238
},
156 { "v239", NAMING_V239
},
157 { "v240", NAMING_V240
},
158 /* … add more schemes here, as the logic to name devices is updated … */
173 enum netname_type type
;
179 char pci_slot
[IFNAMSIZ
];
180 char pci_path
[IFNAMSIZ
];
181 char pci_onboard
[IFNAMSIZ
];
182 const char *pci_onboard_label
;
184 char usb_ports
[IFNAMSIZ
];
185 char bcma_core
[IFNAMSIZ
];
186 char ccw_busid
[IFNAMSIZ
];
187 char vio_slot
[IFNAMSIZ
];
188 char platform_path
[IFNAMSIZ
];
192 sd_device
*physfn_pcidev
;
193 char suffix
[IFNAMSIZ
];
196 static const NamingScheme
* naming_scheme_from_name(const char *name
) {
199 if (streq(name
, "latest"))
200 return naming_schemes
+ ELEMENTSOF(naming_schemes
) - 1;
202 for (i
= 0; i
< ELEMENTSOF(naming_schemes
); i
++)
203 if (streq(naming_schemes
[i
].name
, name
))
204 return naming_schemes
+ i
;
209 static const NamingScheme
* naming_scheme(void) {
210 static const NamingScheme
*cache
= NULL
;
211 _cleanup_free_
char *buffer
= NULL
;
217 /* Acquire setting from the kernel command line */
218 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer
);
220 /* Also acquire it from an env var */
221 e
= getenv("NET_NAMING_SCHEME");
224 /* If prefixed with ':' the kernel cmdline takes precedence */
227 k
= e
; /* Otherwise the env var takes precedence */
232 cache
= naming_scheme_from_name(k
);
234 log_info("Using interface naming scheme '%s'.", cache
->name
);
238 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k
);
241 cache
= naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME
);
243 log_info("Using default interface naming scheme '%s'.", cache
->name
);
248 static bool naming_scheme_has(NamingSchemeFlags flags
) {
249 return FLAGS_SET(naming_scheme()->flags
, flags
);
252 /* skip intermediate virtio devices */
253 static sd_device
*skip_virtio(sd_device
*dev
) {
256 /* there can only ever be one virtio bus per parent device, so we can
257 * safely ignore any virtio buses. see
258 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
259 for (parent
= dev
; parent
; ) {
260 const char *subsystem
;
262 if (sd_device_get_subsystem(parent
, &subsystem
) < 0)
265 if (!streq(subsystem
, "virtio"))
268 if (sd_device_get_parent(parent
, &parent
) < 0)
275 static int get_virtfn_info(sd_device
*dev
, struct netnames
*names
, struct virtfn_info
*ret
) {
276 _cleanup_(sd_device_unrefp
) sd_device
*physfn_pcidev
= NULL
;
277 const char *physfn_link_file
, *syspath
;
278 _cleanup_free_
char *physfn_pci_syspath
= NULL
;
279 _cleanup_free_
char *virtfn_pci_syspath
= NULL
;
281 _cleanup_closedir_
DIR *dir
= NULL
;
282 char suffix
[IFNAMSIZ
];
289 r
= sd_device_get_syspath(names
->pcidev
, &syspath
);
293 /* Check if this is a virtual function. */
294 physfn_link_file
= strjoina(syspath
, "/physfn");
295 r
= chase_symlinks(physfn_link_file
, NULL
, 0, &physfn_pci_syspath
);
299 /* Get physical function's pci device. */
300 r
= sd_device_new_from_syspath(&physfn_pcidev
, physfn_pci_syspath
);
304 /* Find the virtual function number by finding the right virtfn link. */
305 dir
= opendir(physfn_pci_syspath
);
309 FOREACH_DIRENT_ALL(dent
, dir
, break) {
310 _cleanup_free_
char *virtfn_link_file
= NULL
;
312 if (!startswith(dent
->d_name
, "virtfn"))
315 virtfn_link_file
= strjoin(physfn_pci_syspath
, "/", dent
->d_name
);
316 if (!virtfn_link_file
)
319 if (chase_symlinks(virtfn_link_file
, NULL
, 0, &virtfn_pci_syspath
) < 0)
322 if (streq(syspath
, virtfn_pci_syspath
)) {
323 if (!snprintf_ok(suffix
, sizeof(suffix
), "v%s", &dent
->d_name
[6]))
332 ret
->physfn_pcidev
= TAKE_PTR(physfn_pcidev
);
333 strncpy(ret
->suffix
, suffix
, sizeof(ret
->suffix
));
338 /* retrieve on-board index number and label from firmware */
339 static int dev_pci_onboard(sd_device
*dev
, struct netnames
*names
) {
340 unsigned long idx
, dev_port
= 0;
341 const char *attr
, *port_name
= NULL
;
346 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
347 if (sd_device_get_sysattr_value(names
->pcidev
, "acpi_index", &attr
) < 0) {
348 /* SMBIOS type 41 — Onboard Devices Extended Information */
349 r
= sd_device_get_sysattr_value(names
->pcidev
, "index", &attr
);
354 r
= safe_atolu(attr
, &idx
);
357 if (idx
== 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX
))
360 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
361 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
362 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
363 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
364 if (idx
> ONBOARD_INDEX_MAX
)
367 /* kernel provided port index for multiple ports on a single PCI function */
368 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0)
369 dev_port
= strtoul(attr
, NULL
, 10);
371 /* kernel provided front panel port name for multiple port PCI device */
372 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
374 s
= names
->pci_onboard
;
375 l
= sizeof(names
->pci_onboard
);
376 l
= strpcpyf(&s
, l
, "o%lu", idx
);
378 l
= strpcpyf(&s
, l
, "n%s", port_name
);
379 else if (dev_port
> 0)
380 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
382 names
->pci_onboard
[0] = '\0';
384 if (sd_device_get_sysattr_value(names
->pcidev
, "label", &names
->pci_onboard_label
) < 0)
385 names
->pci_onboard_label
= NULL
;
390 /* read the 256 bytes PCI configuration space to check the multi-function bit */
391 static bool is_pci_multifunction(sd_device
*dev
) {
392 _cleanup_close_
int fd
= -1;
393 const char *filename
, *syspath
;
396 if (sd_device_get_syspath(dev
, &syspath
) < 0)
399 filename
= strjoina(syspath
, "/config");
400 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
403 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
406 /* bit 0-6 header type, bit 7 multi/single function device */
407 return config
[PCI_HEADER_TYPE
] & 0x80;
410 static bool is_pci_ari_enabled(sd_device
*dev
) {
413 if (sd_device_get_sysattr_value(dev
, "ari_enabled", &a
) < 0)
416 return streq(a
, "1");
419 static int dev_pci_slot(sd_device
*dev
, struct netnames
*names
) {
420 unsigned long dev_port
= 0;
421 unsigned domain
, bus
, slot
, func
, hotplug_slot
= 0;
424 const char *sysname
, *attr
, *port_name
= NULL
, *syspath
;
425 _cleanup_(sd_device_unrefp
) sd_device
*pci
= NULL
;
426 sd_device
*hotplug_slot_dev
;
427 char slots
[PATH_MAX
];
428 _cleanup_closedir_
DIR *dir
= NULL
;
432 r
= sd_device_get_sysname(names
->pcidev
, &sysname
);
436 if (sscanf(sysname
, "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
439 if (naming_scheme_has(NAMING_NPAR_ARI
) &&
440 is_pci_ari_enabled(names
->pcidev
))
441 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
442 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
443 * where the slot makes up the upper 5 bits. */
446 /* kernel provided port index for multiple ports on a single PCI function */
447 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0) {
448 dev_port
= strtoul(attr
, NULL
, 10);
449 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
450 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
451 * which thus stays initialized as 0. */
453 sd_device_get_sysattr_value(dev
, "type", &attr
) >= 0) {
456 type
= strtoul(attr
, NULL
, 10);
457 if (type
== ARPHRD_INFINIBAND
&&
458 sd_device_get_sysattr_value(dev
, "dev_id", &attr
) >= 0)
459 dev_port
= strtoul(attr
, NULL
, 16);
463 /* kernel provided front panel port name for multi-port PCI device */
464 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
466 /* compose a name based on the raw kernel's PCI bus, slot numbers */
468 l
= sizeof(names
->pci_path
);
470 l
= strpcpyf(&s
, l
, "P%u", domain
);
471 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
472 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
473 l
= strpcpyf(&s
, l
, "f%u", func
);
475 l
= strpcpyf(&s
, l
, "n%s", port_name
);
476 else if (dev_port
> 0)
477 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
479 names
->pci_path
[0] = '\0';
481 /* ACPI _SUN — slot user number */
482 r
= sd_device_new_from_subsystem_sysname(&pci
, "subsystem", "pci");
486 r
= sd_device_get_syspath(pci
, &syspath
);
489 if (!snprintf_ok(slots
, sizeof slots
, "%s/slots", syspath
))
490 return -ENAMETOOLONG
;
492 dir
= opendir(slots
);
496 hotplug_slot_dev
= names
->pcidev
;
497 while (hotplug_slot_dev
) {
498 if (sd_device_get_sysname(hotplug_slot_dev
, &sysname
) < 0)
501 FOREACH_DIRENT_ALL(dent
, dir
, break) {
504 _cleanup_free_
char *address
= NULL
;
506 if (dent
->d_name
[0] == '.')
508 r
= safe_atou_full(dent
->d_name
, 10, &i
);
512 /* match slot address with device by stripping the function */
513 if (snprintf_ok(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
) &&
514 read_one_line_file(str
, &address
) >= 0 &&
515 startswith(sysname
, address
)) {
520 if (hotplug_slot
> 0)
522 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev
, "pci", NULL
, &hotplug_slot_dev
) < 0)
527 if (hotplug_slot
> 0) {
529 l
= sizeof(names
->pci_slot
);
531 l
= strpcpyf(&s
, l
, "P%d", domain
);
532 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
533 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
534 l
= strpcpyf(&s
, l
, "f%d", func
);
536 l
= strpcpyf(&s
, l
, "n%s", port_name
);
537 else if (dev_port
> 0)
538 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
540 names
->pci_slot
[0] = '\0';
546 static int names_vio(sd_device
*dev
, struct netnames
*names
) {
548 unsigned busid
, slotid
, ethid
;
549 const char *syspath
, *subsystem
;
552 /* check if our direct parent is a VIO device with no other bus in-between */
553 r
= sd_device_get_parent(dev
, &parent
);
557 r
= sd_device_get_subsystem(parent
, &subsystem
);
560 if (!streq("vio", subsystem
))
563 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
564 * selected in the HMC), thus this provides a reliable naming (e.g.
565 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
566 * there should only ever be one bus, and then remove leading zeros. */
567 r
= sd_device_get_syspath(dev
, &syspath
);
571 if (sscanf(syspath
, "/sys/devices/vio/%4x%4x/net/eth%u", &busid
, &slotid
, ðid
) != 3)
574 xsprintf(names
->vio_slot
, "v%u", slotid
);
575 names
->type
= NET_VIO
;
579 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
580 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
581 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
583 static int names_platform(sd_device
*dev
, struct netnames
*names
, bool test
) {
586 unsigned model
, instance
, ethid
;
587 const char *syspath
, *pattern
, *validchars
, *subsystem
;
590 /* check if our direct parent is a platform device with no other bus in-between */
591 r
= sd_device_get_parent(dev
, &parent
);
595 r
= sd_device_get_subsystem(parent
, &subsystem
);
599 if (!streq("platform", subsystem
))
602 r
= sd_device_get_syspath(dev
, &syspath
);
606 /* syspath is too short, to have a valid ACPI instance */
607 if (strlen(syspath
) < sizeof _PLATFORM_TEST
)
610 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
611 if (syspath
[sizeof _PLATFORM_TEST
- 1] == ':') {
612 pattern
= _PLATFORM_PATTERN4
;
613 validchars
= UPPERCASE_LETTERS DIGITS
;
615 pattern
= _PLATFORM_PATTERN3
;
616 validchars
= UPPERCASE_LETTERS
;
619 /* Platform devices are named after ACPI table match, and instance id
620 * eg. "/sys/devices/platform/HISI00C2:00");
621 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
624 #pragma GCC diagnostic push
625 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
626 if (sscanf(syspath
, pattern
, vendor
, &model
, &instance
, ðid
) != 4)
628 #pragma GCC diagnostic pop
630 if (!in_charset(vendor
, validchars
))
633 ascii_strlower(vendor
);
635 xsprintf(names
->platform_path
, "a%s%xi%u", vendor
, model
, instance
);
636 names
->type
= NET_PLATFORM
;
640 static int names_pci(sd_device
*dev
, struct netnames
*names
) {
642 struct netnames vf_names
= {};
643 struct virtfn_info vf_info
= {};
644 const char *subsystem
;
650 r
= sd_device_get_parent(dev
, &parent
);
653 /* skip virtio subsystem if present */
654 parent
= skip_virtio(parent
);
659 /* check if our direct parent is a PCI device with no other bus in-between */
660 if (sd_device_get_subsystem(parent
, &subsystem
) >= 0 &&
661 streq("pci", subsystem
)) {
662 names
->type
= NET_PCI
;
663 names
->pcidev
= parent
;
665 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
, &names
->pcidev
);
670 if (naming_scheme_has(NAMING_SR_IOV_V
) &&
671 get_virtfn_info(dev
, names
, &vf_info
) >= 0) {
672 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
673 vf_names
.pcidev
= vf_info
.physfn_pcidev
;
674 dev_pci_onboard(dev
, &vf_names
);
675 dev_pci_slot(dev
, &vf_names
);
676 if (vf_names
.pci_onboard
[0])
677 if (strlen(vf_names
.pci_onboard
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_onboard
))
678 strscpyl(names
->pci_onboard
, sizeof(names
->pci_onboard
),
679 vf_names
.pci_onboard
, vf_info
.suffix
, NULL
);
680 if (vf_names
.pci_slot
[0])
681 if (strlen(vf_names
.pci_slot
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_slot
))
682 strscpyl(names
->pci_slot
, sizeof(names
->pci_slot
),
683 vf_names
.pci_slot
, vf_info
.suffix
, NULL
);
684 if (vf_names
.pci_path
[0])
685 if (strlen(vf_names
.pci_path
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_path
))
686 strscpyl(names
->pci_path
, sizeof(names
->pci_path
),
687 vf_names
.pci_path
, vf_info
.suffix
, NULL
);
688 sd_device_unref(vf_info
.physfn_pcidev
);
690 dev_pci_onboard(dev
, names
);
691 dev_pci_slot(dev
, names
);
697 static int names_usb(sd_device
*dev
, struct netnames
*names
) {
699 char name
[256], *ports
, *config
, *interf
, *s
;
707 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &usbdev
);
711 r
= sd_device_get_sysname(usbdev
, &sysname
);
715 /* get USB port number chain, configuration, interface */
716 strscpy(name
, sizeof(name
), sysname
);
717 s
= strchr(name
, '-');
722 s
= strchr(ports
, ':');
728 s
= strchr(config
, '.');
734 /* prefix every port number in the chain with "u" */
736 while ((s
= strchr(s
, '.')))
738 s
= names
->usb_ports
;
739 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
741 /* append USB config number, suppress the common config == 1 */
742 if (!streq(config
, "1"))
743 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
745 /* append USB interface number, suppress the interface == 0 */
746 if (!streq(interf
, "0"))
747 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
749 return -ENAMETOOLONG
;
751 names
->type
= NET_USB
;
755 static int names_bcma(sd_device
*dev
, struct netnames
*names
) {
764 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
, &bcmadev
);
768 r
= sd_device_get_sysname(bcmadev
, &sysname
);
772 /* bus num:core num */
773 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
775 /* suppress the common core == 0 */
777 xsprintf(names
->bcma_core
, "b%u", core
);
779 names
->type
= NET_BCMA
;
783 static int names_ccw(sd_device
*dev
, struct netnames
*names
) {
785 const char *bus_id
, *subsys
;
793 /* Retrieve the associated CCW device */
794 r
= sd_device_get_parent(dev
, &cdev
);
798 /* skip virtio subsystem if present */
799 cdev
= skip_virtio(cdev
);
803 r
= sd_device_get_subsystem(cdev
, &subsys
);
807 /* Network devices are either single or grouped CCW devices */
808 if (!STR_IN_SET(subsys
, "ccwgroup", "ccw"))
811 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
812 * identifies the network device on the Linux on System z channel
813 * subsystem. Note that the bus-ID contains lowercase characters.
815 r
= sd_device_get_sysname(cdev
, &bus_id
);
819 /* Check the length of the bus-ID. Rely on that the kernel provides
820 * a correct bus-ID; alternatively, improve this check and parse and
821 * verify each bus-ID part...
823 bus_id_len
= strlen(bus_id
);
824 if (!IN_SET(bus_id_len
, 8, 9))
827 /* Strip leading zeros from the bus id for aesthetic purposes. This
828 * keeps the ccw names stable, yet much shorter in general case of
829 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
830 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
832 bus_id_start
= strspn(bus_id
, ".0");
833 bus_id
+= bus_id_start
< bus_id_len
? bus_id_start
: bus_id_len
- 1;
835 /* Store the CCW bus-ID for use as network device name */
836 if (snprintf_ok(names
->ccw_busid
, sizeof(names
->ccw_busid
), "c%s", bus_id
))
837 names
->type
= NET_CCW
;
842 static int names_mac(sd_device
*dev
, struct netnames
*names
) {
845 unsigned a1
, a2
, a3
, a4
, a5
, a6
;
848 /* Some kinds of devices tend to have hardware addresses
849 * that are impossible to use in an iface name.
851 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
855 i
= strtoul(s
, NULL
, 0);
857 /* The persistent part of a hardware address of an InfiniBand NIC
858 * is 8 bytes long. We cannot fit this much in an iface name.
860 case ARPHRD_INFINIBAND
:
866 /* check for NET_ADDR_PERM, skip random MAC addresses */
867 r
= sd_device_get_sysattr_value(dev
, "addr_assign_type", &s
);
870 i
= strtoul(s
, NULL
, 0);
874 r
= sd_device_get_sysattr_value(dev
, "address", &s
);
877 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
880 /* skip empty MAC addresses */
881 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
890 names
->mac_valid
= true;
894 /* IEEE Organizationally Unique Identifier vendor string */
895 static int ieee_oui(sd_device
*dev
, struct netnames
*names
, bool test
) {
898 if (!names
->mac_valid
)
900 /* skip commonly misused 00:00:00 (Xerox) prefix */
901 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
903 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
904 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
906 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
910 static int builtin_net_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
911 const char *s
, *p
, *devtype
, *prefix
= "en";
912 struct netnames names
= {};
916 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
917 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
921 i
= strtoul(s
, NULL
, 0);
926 case ARPHRD_INFINIBAND
:
927 if (naming_scheme_has(NAMING_INFINIBAND
))
939 /* skip stacked devices, like VLANs, ... */
940 r
= sd_device_get_sysattr_value(dev
, "ifindex", &s
);
943 r
= sd_device_get_sysattr_value(dev
, "iflink", &p
);
949 if (sd_device_get_devtype(dev
, &devtype
) >= 0) {
950 if (streq("wlan", devtype
))
952 else if (streq("wwan", devtype
))
956 udev_builtin_add_property(dev
, test
, "ID_NET_NAMING_SCHEME", naming_scheme()->name
);
958 r
= names_mac(dev
, &names
);
959 if (r
>= 0 && names
.mac_valid
) {
962 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
963 names
.mac
[0], names
.mac
[1], names
.mac
[2],
964 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
965 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
967 ieee_oui(dev
, &names
, test
);
970 /* get path names for Linux on System z network devices */
971 if (names_ccw(dev
, &names
) >= 0 && names
.type
== NET_CCW
) {
974 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.ccw_busid
))
975 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
979 /* get ibmveth/ibmvnic slot-based names. */
980 if (names_vio(dev
, &names
) >= 0 && names
.type
== NET_VIO
) {
983 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.vio_slot
))
984 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
988 /* get ACPI path names for ARM64 platform devices */
989 if (names_platform(dev
, &names
, test
) >= 0 && names
.type
== NET_PLATFORM
) {
992 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.platform_path
))
993 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
997 /* get PCI based path names, we compose only PCI based paths */
998 if (names_pci(dev
, &names
) < 0)
1001 /* plain PCI device */
1002 if (names
.type
== NET_PCI
) {
1005 if (names
.pci_onboard
[0] &&
1006 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard
))
1007 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
1009 if (names
.pci_onboard_label
&&
1010 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard_label
))
1011 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
1013 if (names
.pci_path
[0] &&
1014 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_path
))
1015 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
1017 if (names
.pci_slot
[0] &&
1018 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_slot
))
1019 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
1024 if (names_usb(dev
, &names
) >= 0 && names
.type
== NET_USB
) {
1027 if (names
.pci_path
[0] &&
1028 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
))
1029 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
1031 if (names
.pci_slot
[0] &&
1032 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
))
1033 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
1038 if (names_bcma(dev
, &names
) >= 0 && names
.type
== NET_BCMA
) {
1041 if (names
.pci_path
[0] &&
1042 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
))
1043 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
1045 if (names
.pci_slot
[0] &&
1046 snprintf(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
))
1047 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
1054 const struct udev_builtin udev_builtin_net_id
= {
1056 .cmd
= builtin_net_id
,
1057 .help
= "Network device properties",