1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 * https://systemd.io/PREDICTABLE_INTERFACE_NAMES
12 * When the code here is changed, man/systemd.net-naming-scheme.xml must be updated too.
18 #include <net/if_arp.h>
23 #include <linux/pci_regs.h>
25 #include "alloc-util.h"
26 #include "device-util.h"
27 #include "dirent-util.h"
31 #include "netif-naming-scheme.h"
32 #include "parse-util.h"
33 #include "proc-cmdline.h"
34 #include "stdio-util.h"
35 #include "string-util.h"
38 #include "udev-builtin.h"
40 #define ONBOARD_INDEX_MAX (16*1024-1)
55 enum netname_type type
;
61 char pci_slot
[ALTIFNAMSIZ
];
62 char pci_path
[ALTIFNAMSIZ
];
63 char pci_onboard
[ALTIFNAMSIZ
];
64 const char *pci_onboard_label
;
66 char usb_ports
[ALTIFNAMSIZ
];
67 char bcma_core
[ALTIFNAMSIZ
];
68 char ccw_busid
[ALTIFNAMSIZ
];
69 char vio_slot
[ALTIFNAMSIZ
];
70 char platform_path
[ALTIFNAMSIZ
];
71 char netdevsim_path
[ALTIFNAMSIZ
];
75 sd_device
*physfn_pcidev
;
76 char suffix
[ALTIFNAMSIZ
];
79 /* skip intermediate virtio devices */
80 static sd_device
*skip_virtio(sd_device
*dev
) {
83 /* there can only ever be one virtio bus per parent device, so we can
84 * safely ignore any virtio buses. see
85 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
86 for (parent
= dev
; parent
; ) {
87 const char *subsystem
;
89 if (sd_device_get_subsystem(parent
, &subsystem
) < 0)
92 if (!streq(subsystem
, "virtio"))
95 if (sd_device_get_parent(parent
, &parent
) < 0)
102 static int get_virtfn_info(sd_device
*dev
, struct netnames
*names
, struct virtfn_info
*ret
) {
103 _cleanup_(sd_device_unrefp
) sd_device
*physfn_pcidev
= NULL
;
104 const char *physfn_link_file
, *syspath
;
105 _cleanup_free_
char *physfn_pci_syspath
= NULL
;
106 _cleanup_free_
char *virtfn_pci_syspath
= NULL
;
108 _cleanup_closedir_
DIR *dir
= NULL
;
109 char suffix
[ALTIFNAMSIZ
];
116 r
= sd_device_get_syspath(names
->pcidev
, &syspath
);
120 /* Check if this is a virtual function. */
121 physfn_link_file
= strjoina(syspath
, "/physfn");
122 r
= chase_symlinks(physfn_link_file
, NULL
, 0, &physfn_pci_syspath
, NULL
);
126 /* Get physical function's pci device. */
127 r
= sd_device_new_from_syspath(&physfn_pcidev
, physfn_pci_syspath
);
131 /* Find the virtual function number by finding the right virtfn link. */
132 dir
= opendir(physfn_pci_syspath
);
136 FOREACH_DIRENT_ALL(dent
, dir
, break) {
137 _cleanup_free_
char *virtfn_link_file
= NULL
;
139 if (!startswith(dent
->d_name
, "virtfn"))
142 virtfn_link_file
= path_join(physfn_pci_syspath
, dent
->d_name
);
143 if (!virtfn_link_file
)
146 if (chase_symlinks(virtfn_link_file
, NULL
, 0, &virtfn_pci_syspath
, NULL
) < 0)
149 if (streq(syspath
, virtfn_pci_syspath
)) {
150 if (!snprintf_ok(suffix
, sizeof(suffix
), "v%s", &dent
->d_name
[6]))
159 ret
->physfn_pcidev
= TAKE_PTR(physfn_pcidev
);
160 strncpy(ret
->suffix
, suffix
, sizeof(ret
->suffix
));
165 /* retrieve on-board index number and label from firmware */
166 static int dev_pci_onboard(sd_device
*dev
, struct netnames
*names
) {
167 unsigned long idx
, dev_port
= 0;
168 const char *attr
, *port_name
= NULL
;
173 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
174 if (sd_device_get_sysattr_value(names
->pcidev
, "acpi_index", &attr
) < 0) {
175 /* SMBIOS type 41 — Onboard Devices Extended Information */
176 r
= sd_device_get_sysattr_value(names
->pcidev
, "index", &attr
);
181 r
= safe_atolu(attr
, &idx
);
184 if (idx
== 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX
))
187 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
188 * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
189 * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
190 * interface a system might have. Ideally the kernel would already filter this crap for us, but it
191 * doesn't currently. */
192 if (idx
> ONBOARD_INDEX_MAX
)
195 /* kernel provided port index for multiple ports on a single PCI function */
196 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0)
197 dev_port
= strtoul(attr
, NULL
, 10);
199 /* kernel provided front panel port name for multiple port PCI device */
200 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
202 s
= names
->pci_onboard
;
203 l
= sizeof(names
->pci_onboard
);
204 l
= strpcpyf(&s
, l
, "o%lu", idx
);
206 l
= strpcpyf(&s
, l
, "n%s", port_name
);
207 else if (dev_port
> 0)
208 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
210 names
->pci_onboard
[0] = '\0';
212 if (sd_device_get_sysattr_value(names
->pcidev
, "label", &names
->pci_onboard_label
) < 0)
213 names
->pci_onboard_label
= NULL
;
218 /* read the 256 bytes PCI configuration space to check the multi-function bit */
219 static bool is_pci_multifunction(sd_device
*dev
) {
220 _cleanup_close_
int fd
= -1;
221 const char *filename
, *syspath
;
224 if (sd_device_get_syspath(dev
, &syspath
) < 0)
227 filename
= strjoina(syspath
, "/config");
228 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
231 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
234 /* bit 0-6 header type, bit 7 multi/single function device */
235 return config
[PCI_HEADER_TYPE
] & 0x80;
238 static bool is_pci_ari_enabled(sd_device
*dev
) {
241 if (sd_device_get_sysattr_value(dev
, "ari_enabled", &a
) < 0)
244 return streq(a
, "1");
247 static bool is_pci_bridge(sd_device
*dev
) {
250 if (sd_device_get_sysattr_value(dev
, "modalias", &v
) < 0)
253 if (!startswith(v
, "pci:"))
262 /* PCI device subclass 04 corresponds to PCI bridge */
263 return strneq(p
+ 2, "04", 2);
266 static int parse_hotplug_slot_from_function_id(sd_device
*dev
, const char *slots
, uint32_t *ret
) {
267 uint64_t function_id
;
272 /* The <sysname>/function_id attribute is unique to the s390 PCI driver. If present, we know
273 * that the slot's directory name for this device is /sys/bus/pci/XXXXXXXX/ where XXXXXXXX is
274 * the fixed length 8 hexadecimal character string representation of function_id. Therefore we
275 * can short cut here and just check for the existence of the slot directory. As this directory
276 * has to exist, we're emitting a debug message for the unlikely case it's not found. Note that
277 * the domain part doesn't belong to the slot name here because there's a 1-to-1 relationship
278 * between PCI function and its hotplug slot. */
284 if (!naming_scheme_has(NAMING_SLOT_FUNCTION_ID
))
287 if (sd_device_get_sysattr_value(dev
, "function_id", &attr
) < 0)
290 r
= safe_atou64(attr
, &function_id
);
292 return log_device_debug_errno(dev
, r
, "Failed to parse function_id, ignoring: %s", attr
);
294 if (function_id
<= 0 || function_id
> UINT32_MAX
)
295 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
296 "Invalid function id (0x%"PRIx64
"), ignoring.",
299 if (!snprintf_ok(path
, sizeof path
, "%s/%08"PRIx64
, slots
, function_id
))
300 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(ENAMETOOLONG
),
301 "PCI slot path is too long, ignoring.");
303 if (access(path
, F_OK
) < 0)
304 return log_device_debug_errno(dev
, errno
, "Cannot access %s, ignoring: %m", path
);
306 *ret
= (uint32_t) function_id
;
310 static int dev_pci_slot(sd_device
*dev
, struct netnames
*names
) {
311 const char *sysname
, *attr
, *port_name
= NULL
, *syspath
;
312 _cleanup_(sd_device_unrefp
) sd_device
*pci
= NULL
;
313 _cleanup_closedir_
DIR *dir
= NULL
;
314 unsigned domain
, bus
, slot
, func
;
315 sd_device
*hotplug_slot_dev
;
316 unsigned long dev_port
= 0;
317 uint32_t hotplug_slot
= 0;
318 char slots
[PATH_MAX
], *s
;
322 r
= sd_device_get_sysname(names
->pcidev
, &sysname
);
326 if (sscanf(sysname
, "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
329 if (naming_scheme_has(NAMING_NPAR_ARI
) &&
330 is_pci_ari_enabled(names
->pcidev
))
331 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
332 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
333 * where the slot makes up the upper 5 bits. */
336 /* kernel provided port index for multiple ports on a single PCI function */
337 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0) {
338 dev_port
= strtoul(attr
, NULL
, 10);
339 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
340 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
341 * which thus stays initialized as 0. */
343 sd_device_get_sysattr_value(dev
, "type", &attr
) >= 0) {
346 type
= strtoul(attr
, NULL
, 10);
347 if (type
== ARPHRD_INFINIBAND
&&
348 sd_device_get_sysattr_value(dev
, "dev_id", &attr
) >= 0)
349 dev_port
= strtoul(attr
, NULL
, 16);
353 /* kernel provided front panel port name for multi-port PCI device */
354 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
356 /* compose a name based on the raw kernel's PCI bus, slot numbers */
358 l
= sizeof(names
->pci_path
);
360 l
= strpcpyf(&s
, l
, "P%u", domain
);
361 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
362 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
363 l
= strpcpyf(&s
, l
, "f%u", func
);
365 l
= strpcpyf(&s
, l
, "n%s", port_name
);
366 else if (dev_port
> 0)
367 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
369 names
->pci_path
[0] = '\0';
371 /* ACPI _SUN — slot user number */
372 r
= sd_device_new_from_subsystem_sysname(&pci
, "subsystem", "pci");
376 r
= sd_device_get_syspath(pci
, &syspath
);
379 if (!snprintf_ok(slots
, sizeof slots
, "%s/slots", syspath
))
380 return -ENAMETOOLONG
;
382 dir
= opendir(slots
);
386 hotplug_slot_dev
= names
->pcidev
;
387 while (hotplug_slot_dev
) {
390 r
= parse_hotplug_slot_from_function_id(hotplug_slot_dev
, slots
, &hotplug_slot
);
394 domain
= 0; /* See comments in parse_hotplug_slot_from_function_id(). */
398 r
= sd_device_get_sysname(hotplug_slot_dev
, &sysname
);
400 return log_device_debug_errno(hotplug_slot_dev
, r
, "Failed to get sysname: %m");
402 FOREACH_DIRENT_ALL(dent
, dir
, break) {
403 _cleanup_free_
char *address
= NULL
;
407 if (dot_or_dot_dot(dent
->d_name
))
410 r
= safe_atou32(dent
->d_name
, &i
);
414 /* match slot address with device by stripping the function */
415 if (snprintf_ok(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
) &&
416 read_one_line_file(str
, &address
) >= 0 &&
417 startswith(sysname
, address
)) {
420 /* We found the match between PCI device and slot. However, we won't use the
421 * slot index if the device is a PCI bridge, because it can have other child
422 * devices that will try to claim the same index and that would create name
424 if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT
) && is_pci_bridge(hotplug_slot_dev
))
430 if (hotplug_slot
> 0)
432 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev
, "pci", NULL
, &hotplug_slot_dev
) < 0)
437 if (hotplug_slot
> 0) {
439 l
= sizeof(names
->pci_slot
);
441 l
= strpcpyf(&s
, l
, "P%d", domain
);
442 l
= strpcpyf(&s
, l
, "s%"PRIu32
, hotplug_slot
);
443 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
444 l
= strpcpyf(&s
, l
, "f%d", func
);
446 l
= strpcpyf(&s
, l
, "n%s", port_name
);
447 else if (dev_port
> 0)
448 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
450 names
->pci_slot
[0] = '\0';
456 static int names_vio(sd_device
*dev
, struct netnames
*names
) {
458 unsigned busid
, slotid
, ethid
;
459 const char *syspath
, *subsystem
;
462 /* check if our direct parent is a VIO device with no other bus in-between */
463 r
= sd_device_get_parent(dev
, &parent
);
467 r
= sd_device_get_subsystem(parent
, &subsystem
);
470 if (!streq("vio", subsystem
))
473 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
474 * selected in the HMC), thus this provides a reliable naming (e.g.
475 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
476 * there should only ever be one bus, and then remove leading zeros. */
477 r
= sd_device_get_syspath(dev
, &syspath
);
481 if (sscanf(syspath
, "/sys/devices/vio/%4x%4x/net/eth%u", &busid
, &slotid
, ðid
) != 3)
484 xsprintf(names
->vio_slot
, "v%u", slotid
);
485 names
->type
= NET_VIO
;
489 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
490 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
491 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
493 static int names_platform(sd_device
*dev
, struct netnames
*names
, bool test
) {
496 unsigned model
, instance
, ethid
;
497 const char *syspath
, *pattern
, *validchars
, *subsystem
;
500 /* check if our direct parent is a platform device with no other bus in-between */
501 r
= sd_device_get_parent(dev
, &parent
);
505 r
= sd_device_get_subsystem(parent
, &subsystem
);
509 if (!streq("platform", subsystem
))
512 r
= sd_device_get_syspath(dev
, &syspath
);
516 /* syspath is too short, to have a valid ACPI instance */
517 if (strlen(syspath
) < sizeof _PLATFORM_TEST
)
520 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
521 if (syspath
[sizeof _PLATFORM_TEST
- 1] == ':') {
522 pattern
= _PLATFORM_PATTERN4
;
523 validchars
= UPPERCASE_LETTERS DIGITS
;
525 pattern
= _PLATFORM_PATTERN3
;
526 validchars
= UPPERCASE_LETTERS
;
529 /* Platform devices are named after ACPI table match, and instance id
530 * eg. "/sys/devices/platform/HISI00C2:00");
531 * The Vendor (3 or 4 char), followed by hexadecimal model number : instance id. */
533 DISABLE_WARNING_FORMAT_NONLITERAL
;
534 if (sscanf(syspath
, pattern
, vendor
, &model
, &instance
, ðid
) != 4)
538 if (!in_charset(vendor
, validchars
))
541 ascii_strlower(vendor
);
543 xsprintf(names
->platform_path
, "a%s%xi%u", vendor
, model
, instance
);
544 names
->type
= NET_PLATFORM
;
548 static int names_pci(sd_device
*dev
, struct netnames
*names
) {
550 struct netnames vf_names
= {};
551 struct virtfn_info vf_info
= {};
552 const char *subsystem
;
558 r
= sd_device_get_parent(dev
, &parent
);
561 /* skip virtio subsystem if present */
562 parent
= skip_virtio(parent
);
567 /* check if our direct parent is a PCI device with no other bus in-between */
568 if (sd_device_get_subsystem(parent
, &subsystem
) >= 0 &&
569 streq("pci", subsystem
)) {
570 names
->type
= NET_PCI
;
571 names
->pcidev
= parent
;
573 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
, &names
->pcidev
);
578 if (naming_scheme_has(NAMING_SR_IOV_V
) &&
579 get_virtfn_info(dev
, names
, &vf_info
) >= 0) {
580 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
581 vf_names
.pcidev
= vf_info
.physfn_pcidev
;
582 dev_pci_onboard(dev
, &vf_names
);
583 dev_pci_slot(dev
, &vf_names
);
584 if (vf_names
.pci_onboard
[0])
585 if (strlen(vf_names
.pci_onboard
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_onboard
))
586 strscpyl(names
->pci_onboard
, sizeof(names
->pci_onboard
),
587 vf_names
.pci_onboard
, vf_info
.suffix
, NULL
);
588 if (vf_names
.pci_slot
[0])
589 if (strlen(vf_names
.pci_slot
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_slot
))
590 strscpyl(names
->pci_slot
, sizeof(names
->pci_slot
),
591 vf_names
.pci_slot
, vf_info
.suffix
, NULL
);
592 if (vf_names
.pci_path
[0])
593 if (strlen(vf_names
.pci_path
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_path
))
594 strscpyl(names
->pci_path
, sizeof(names
->pci_path
),
595 vf_names
.pci_path
, vf_info
.suffix
, NULL
);
596 sd_device_unref(vf_info
.physfn_pcidev
);
598 dev_pci_onboard(dev
, names
);
599 dev_pci_slot(dev
, names
);
605 static int names_usb(sd_device
*dev
, struct netnames
*names
) {
607 char name
[256], *ports
, *config
, *interf
, *s
;
615 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &usbdev
);
619 r
= sd_device_get_sysname(usbdev
, &sysname
);
623 /* get USB port number chain, configuration, interface */
624 strscpy(name
, sizeof(name
), sysname
);
625 s
= strchr(name
, '-');
630 s
= strchr(ports
, ':');
636 s
= strchr(config
, '.');
642 /* prefix every port number in the chain with "u" */
644 while ((s
= strchr(s
, '.')))
646 s
= names
->usb_ports
;
647 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
649 /* append USB config number, suppress the common config == 1 */
650 if (!streq(config
, "1"))
651 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
653 /* append USB interface number, suppress the interface == 0 */
654 if (!streq(interf
, "0"))
655 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
657 return -ENAMETOOLONG
;
659 names
->type
= NET_USB
;
663 static int names_bcma(sd_device
*dev
, struct netnames
*names
) {
672 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
, &bcmadev
);
676 r
= sd_device_get_sysname(bcmadev
, &sysname
);
680 /* bus num:core num */
681 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
683 /* suppress the common core == 0 */
685 xsprintf(names
->bcma_core
, "b%u", core
);
687 names
->type
= NET_BCMA
;
691 static int names_ccw(sd_device
*dev
, struct netnames
*names
) {
693 const char *bus_id
, *subsys
;
701 /* Retrieve the associated CCW device */
702 r
= sd_device_get_parent(dev
, &cdev
);
706 /* skip virtio subsystem if present */
707 cdev
= skip_virtio(cdev
);
711 r
= sd_device_get_subsystem(cdev
, &subsys
);
715 /* Network devices are either single or grouped CCW devices */
716 if (!STR_IN_SET(subsys
, "ccwgroup", "ccw"))
719 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
720 * identifies the network device on the Linux on System z channel
721 * subsystem. Note that the bus-ID contains lowercase characters.
723 r
= sd_device_get_sysname(cdev
, &bus_id
);
727 /* Check the length of the bus-ID. Rely on the fact that the kernel provides a correct bus-ID;
728 * alternatively, improve this check and parse and verify each bus-ID part...
730 bus_id_len
= strlen(bus_id
);
731 if (!IN_SET(bus_id_len
, 8, 9))
734 /* Strip leading zeros from the bus id for aesthetic purposes. This
735 * keeps the ccw names stable, yet much shorter in general case of
736 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
737 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
739 bus_id_start
= strspn(bus_id
, ".0");
740 bus_id
+= bus_id_start
< bus_id_len
? bus_id_start
: bus_id_len
- 1;
742 /* Store the CCW bus-ID for use as network device name */
743 if (snprintf_ok(names
->ccw_busid
, sizeof(names
->ccw_busid
), "c%s", bus_id
))
744 names
->type
= NET_CCW
;
749 static int names_mac(sd_device
*dev
, struct netnames
*names
) {
752 unsigned a1
, a2
, a3
, a4
, a5
, a6
;
755 /* Some kinds of devices tend to have hardware addresses
756 * that are impossible to use in an iface name.
758 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
762 i
= strtoul(s
, NULL
, 0);
764 /* The persistent part of a hardware address of an InfiniBand NIC
765 * is 8 bytes long. We cannot fit this much in an iface name.
767 case ARPHRD_INFINIBAND
:
773 /* check for NET_ADDR_PERM, skip random MAC addresses */
774 r
= sd_device_get_sysattr_value(dev
, "addr_assign_type", &s
);
777 i
= strtoul(s
, NULL
, 0);
781 r
= sd_device_get_sysattr_value(dev
, "address", &s
);
784 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
787 /* skip empty MAC addresses */
788 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
797 names
->mac_valid
= true;
801 static int names_netdevsim(sd_device
*dev
, struct netnames
*names
) {
802 sd_device
*netdevsimdev
;
805 const char *port_name
= NULL
;
809 if (!naming_scheme_has(NAMING_NETDEVSIM
))
815 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "netdevsim", NULL
, &netdevsimdev
);
818 r
= sd_device_get_sysname(netdevsimdev
, &sysname
);
822 if (sscanf(sysname
, "netdevsim%u", &addr
) != 1)
825 r
= sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
829 ok
= snprintf_ok(names
->netdevsim_path
, sizeof(names
->netdevsim_path
), "i%un%s", addr
, port_name
);
833 names
->type
= NET_NETDEVSIM
;
838 /* IEEE Organizationally Unique Identifier vendor string */
839 static int ieee_oui(sd_device
*dev
, struct netnames
*names
, bool test
) {
842 if (!names
->mac_valid
)
844 /* skip commonly misused 00:00:00 (Xerox) prefix */
845 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
847 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
848 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
850 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
854 static int builtin_net_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
855 const char *s
, *p
, *devtype
, *prefix
= "en";
856 struct netnames names
= {};
860 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
861 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
865 i
= strtoul(s
, NULL
, 0);
870 case ARPHRD_INFINIBAND
:
871 if (naming_scheme_has(NAMING_INFINIBAND
))
883 /* skip stacked devices, like VLANs, ... */
884 r
= sd_device_get_sysattr_value(dev
, "ifindex", &s
);
887 r
= sd_device_get_sysattr_value(dev
, "iflink", &p
);
893 if (sd_device_get_devtype(dev
, &devtype
) >= 0) {
894 if (streq("wlan", devtype
))
896 else if (streq("wwan", devtype
))
900 udev_builtin_add_property(dev
, test
, "ID_NET_NAMING_SCHEME", naming_scheme()->name
);
902 r
= names_mac(dev
, &names
);
903 if (r
>= 0 && names
.mac_valid
) {
904 char str
[ALTIFNAMSIZ
];
906 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
907 names
.mac
[0], names
.mac
[1], names
.mac
[2],
908 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
909 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
911 ieee_oui(dev
, &names
, test
);
914 /* get path names for Linux on System z network devices */
915 if (names_ccw(dev
, &names
) >= 0 && names
.type
== NET_CCW
) {
916 char str
[ALTIFNAMSIZ
];
918 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.ccw_busid
))
919 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
923 /* get ibmveth/ibmvnic slot-based names. */
924 if (names_vio(dev
, &names
) >= 0 && names
.type
== NET_VIO
) {
925 char str
[ALTIFNAMSIZ
];
927 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.vio_slot
))
928 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
932 /* get ACPI path names for ARM64 platform devices */
933 if (names_platform(dev
, &names
, test
) >= 0 && names
.type
== NET_PLATFORM
) {
934 char str
[ALTIFNAMSIZ
];
936 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.platform_path
))
937 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
941 /* get netdevsim path names */
942 if (names_netdevsim(dev
, &names
) >= 0 && names
.type
== NET_NETDEVSIM
) {
943 char str
[ALTIFNAMSIZ
];
945 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.netdevsim_path
))
946 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
951 /* get PCI based path names, we compose only PCI based paths */
952 if (names_pci(dev
, &names
) < 0)
955 /* plain PCI device */
956 if (names
.type
== NET_PCI
) {
957 char str
[ALTIFNAMSIZ
];
959 if (names
.pci_onboard
[0] &&
960 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard
))
961 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
963 if (names
.pci_onboard_label
&&
964 snprintf_ok(str
, sizeof str
, "%s%s",
965 naming_scheme_has(NAMING_LABEL_NOPREFIX
) ? "" : prefix
,
966 names
.pci_onboard_label
))
967 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
969 if (names
.pci_path
[0] &&
970 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_path
))
971 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
973 if (names
.pci_slot
[0] &&
974 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_slot
))
975 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
980 if (names_usb(dev
, &names
) >= 0 && names
.type
== NET_USB
) {
981 char str
[ALTIFNAMSIZ
];
983 if (names
.pci_path
[0] &&
984 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
))
985 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
987 if (names
.pci_slot
[0] &&
988 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
))
989 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
994 if (names_bcma(dev
, &names
) >= 0 && names
.type
== NET_BCMA
) {
995 char str
[ALTIFNAMSIZ
];
997 if (names
.pci_path
[0] &&
998 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
))
999 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
1001 if (names
.pci_slot
[0] &&
1002 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
))
1003 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
1010 const UdevBuiltin udev_builtin_net_id
= {
1012 .cmd
= builtin_net_id
,
1013 .help
= "Network device properties",