Skip to content

Commit 82cb5f2

Browse files
committed
udev-builtin-net_id: first parse USB or BCMA identifier, then parse PCI properties
Previously, we first generate names based on the PCI slot and ACPI onboard index, and then append an identifier based on USB or BCMA bus if it exists in between the PCI bus and the interface. However, if there exists USB or BCMA bus, the name based on the ACPI onboard index is not used. So, let's invert the order; first generate USB or BCMA identifier if the bus exists, then prepend the name with the PCI slot identifier. With this change, we can drop Names struct, and each naming logic becomes self-consistent.
1 parent 415ade0 commit 82cb5f2

File tree

1 file changed

+54
-131
lines changed

1 file changed

+54
-131
lines changed

src/udev/udev-builtin-net_id.c

Lines changed: 54 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@
4343
#define ONBOARD_14BIT_INDEX_MAX ((1U << 14) - 1)
4444
#define ONBOARD_16BIT_INDEX_MAX ((1U << 16) - 1)
4545

46-
typedef enum NetNameType {
47-
NET_UNDEF,
48-
NET_PCI,
49-
} NetNameType;
50-
51-
typedef struct NetNames {
52-
NetNameType type;
53-
54-
sd_device *pcidev;
55-
char pci_slot[ALTIFNAMSIZ];
56-
char pci_path[ALTIFNAMSIZ];
57-
char pci_onboard[ALTIFNAMSIZ];
58-
} NetNames;
59-
6046
/* skip intermediate virtio devices */
6147
static sd_device *skip_virtio(sd_device *dev) {
6248
/* there can only ever be one virtio bus per parent device, so we can
@@ -254,29 +240,31 @@ static int pci_get_onboard_index(sd_device *dev, unsigned *ret) {
254240
return 0;
255241
}
256242

257-
static int dev_pci_onboard(sd_device *dev, NetNames *names) {
243+
static int names_pci_onboard(sd_device *dev, sd_device *pci_dev, const char *prefix, const char *suffix, bool test) {
258244
_cleanup_free_ char *port = NULL;
259245
unsigned idx = 0; /* avoid false maybe-uninitialized warning */
260246
int r;
261247

262248
assert(dev);
263-
assert(names);
249+
assert(pci_dev);
250+
assert(prefix);
264251

265252
/* retrieve on-board index number from firmware */
266-
r = pci_get_onboard_index(names->pcidev, &idx);
253+
r = pci_get_onboard_index(pci_dev, &idx);
267254
if (r < 0)
268255
return r;
269256

270257
r = get_port_specifier(dev, /* fallback_to_dev_id = */ false, &port);
271258
if (r < 0)
272259
return r;
273260

274-
if (!snprintf_ok(names->pci_onboard, sizeof(names->pci_onboard), "o%u%s", idx, strempty(port)))
275-
names->pci_onboard[0] = '\0';
261+
char str[ALTIFNAMSIZ];
262+
if (snprintf_ok(str, sizeof str, "%so%u%s%s", prefix, idx, strempty(port), strempty(suffix)))
263+
udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
276264

277265
log_device_debug(dev, "Onboard index identifier: index=%u port=%s %s %s",
278266
idx, strna(port),
279-
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(names->pci_onboard));
267+
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(str));
280268

281269
return 0;
282270
}
@@ -568,15 +556,17 @@ static int get_pci_slot_specifiers(
568556
return 0;
569557
}
570558

571-
static int dev_pci_slot(sd_device *dev, NetNames *names) {
559+
static int names_pci_slot(sd_device *dev, sd_device *pci_dev, const char *prefix, const char *suffix, bool test) {
572560
_cleanup_free_ char *domain = NULL, *bus_and_slot = NULL, *func = NULL, *port = NULL;
573561
uint32_t hotplug_slot = 0; /* avoid false maybe-uninitialized warning */
562+
char str[ALTIFNAMSIZ];
574563
int r;
575564

576565
assert(dev);
577-
assert(names);
566+
assert(pci_dev);
567+
assert(prefix);
578568

579-
r = get_pci_slot_specifiers(names->pcidev, &domain, &bus_and_slot, &func);
569+
r = get_pci_slot_specifiers(pci_dev, &domain, &bus_and_slot, &func);
580570
if (r < 0)
581571
return r;
582572

@@ -585,29 +575,29 @@ static int dev_pci_slot(sd_device *dev, NetNames *names) {
585575
return r;
586576

587577
/* compose a name based on the raw kernel's PCI bus, slot numbers */
588-
if (!snprintf_ok(names->pci_path, sizeof(names->pci_path), "%s%s%s%s",
589-
strempty(domain), bus_and_slot, strempty(func), strempty(port)))
590-
names->pci_path[0] = '\0';
578+
if (snprintf_ok(str, sizeof str, "%s%s%s%s%s%s",
579+
prefix, strempty(domain), bus_and_slot, strempty(func), strempty(port), strempty(suffix)))
580+
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
591581

592582
log_device_debug(dev, "PCI path identifier: domain=%s bus_and_slot=%s func=%s port=%s %s %s",
593583
strna(domain), bus_and_slot, strna(func), strna(port),
594-
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(names->pci_path));
584+
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(str));
595585

596-
r = pci_get_hotplug_slot(names->pcidev, &hotplug_slot);
586+
r = pci_get_hotplug_slot(pci_dev, &hotplug_slot);
597587
if (r < 0)
598588
return r;
599589
if (r > 0)
600590
/* If the hotplug slot is found through the function ID, then drop the domain from the name.
601591
* See comments in parse_hotplug_slot_from_function_id(). */
602592
domain = mfree(domain);
603593

604-
if (!snprintf_ok(names->pci_slot, sizeof(names->pci_slot), "%ss%"PRIu32"%s%s",
605-
strempty(domain), hotplug_slot, strempty(func), strempty(port)))
606-
names->pci_slot[0] = '\0';
594+
if (snprintf_ok(str, sizeof str, "%s%ss%"PRIu32"%s%s%s",
595+
prefix, strempty(domain), hotplug_slot, strempty(func), strempty(port), strempty(suffix)))
596+
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
607597

608598
log_device_debug(dev, "Slot identifier: domain=%s slot=%"PRIu32" func=%s port=%s %s %s",
609599
strna(domain), hotplug_slot, strna(func), strna(port),
610-
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(names->pci_slot));
600+
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), empty_to_na(str));
611601

612602
return 0;
613603
}
@@ -851,7 +841,7 @@ static int names_devicetree(sd_device *dev, const char *prefix, bool test) {
851841
return -ENOENT;
852842
}
853843

854-
static int names_pci(sd_device *dev, const char *prefix, NetNames *names, bool test) {
844+
static int names_pci(sd_device *dev, const char *prefix, bool test) {
855845
_cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
856846
_cleanup_free_ char *virtfn_suffix = NULL;
857847
sd_device *parent;
@@ -860,58 +850,33 @@ static int names_pci(sd_device *dev, const char *prefix, NetNames *names, bool t
860850

861851
assert(dev);
862852
assert(prefix);
863-
assert(names);
864853

865854
r = sd_device_get_parent(dev, &parent);
866855
if (r < 0)
867856
return r;
857+
868858
/* skip virtio subsystem if present */
869859
parent = skip_virtio(parent);
870-
871860
if (!parent)
872861
return -ENOENT;
873862

874863
/* check if our direct parent is a PCI device with no other bus in-between */
875-
if (sd_device_get_subsystem(parent, &subsystem) >= 0 &&
876-
streq(subsystem, "pci")) {
877-
names->type = NET_PCI;
878-
names->pcidev = parent;
879-
} else {
880-
r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
881-
if (r < 0)
882-
return r;
883-
}
884-
885-
if (naming_scheme_has(NAMING_SR_IOV_V) &&
886-
get_virtfn_info(names->pcidev, &physfn_pcidev, &virtfn_suffix) >= 0) {
887-
NetNames vf_names = {};
888-
889-
/* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
890-
vf_names.pcidev = physfn_pcidev;
891-
dev_pci_onboard(dev, &vf_names);
892-
dev_pci_slot(dev, &vf_names);
893-
894-
if (vf_names.pci_onboard[0])
895-
if (strlen(vf_names.pci_onboard) + strlen(virtfn_suffix) < sizeof(names->pci_onboard))
896-
strscpyl(names->pci_onboard, sizeof(names->pci_onboard),
897-
vf_names.pci_onboard, virtfn_suffix, NULL);
898-
if (vf_names.pci_slot[0])
899-
if (strlen(vf_names.pci_slot) + strlen(virtfn_suffix) < sizeof(names->pci_slot))
900-
strscpyl(names->pci_slot, sizeof(names->pci_slot),
901-
vf_names.pci_slot, virtfn_suffix, NULL);
902-
if (vf_names.pci_path[0])
903-
if (strlen(vf_names.pci_path) + strlen(virtfn_suffix) < sizeof(names->pci_path))
904-
strscpyl(names->pci_path, sizeof(names->pci_path),
905-
vf_names.pci_path, virtfn_suffix, NULL);
864+
r = sd_device_get_subsystem(parent, &subsystem);
865+
if (r < 0)
866+
return r;
906867

907-
return 0;
908-
}
868+
if (!streq(subsystem, "pci"))
869+
return -EINVAL;
909870

910-
if (names->type == NET_PCI)
911-
(void) names_pci_onboard_label(dev, names->pcidev, prefix, test);
871+
/* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
872+
if (naming_scheme_has(NAMING_SR_IOV_V) &&
873+
get_virtfn_info(parent, &physfn_pcidev, &virtfn_suffix) >= 0)
874+
parent = physfn_pcidev;
875+
else
876+
(void) names_pci_onboard_label(dev, parent, prefix, test);
912877

913-
dev_pci_onboard(dev, names);
914-
dev_pci_slot(dev, names);
878+
(void) names_pci_onboard(dev, parent, prefix, virtfn_suffix, test);
879+
(void) names_pci_slot(dev, parent, prefix, virtfn_suffix, test);
915880
return 0;
916881
}
917882

@@ -974,14 +939,13 @@ static int get_usb_specifier(sd_device *dev, char **ret) {
974939
return 0;
975940
}
976941

977-
static int names_usb(sd_device *dev, const char *prefix, NetNames *names, bool test) {
942+
static int names_usb(sd_device *dev, const char *prefix, bool test) {
978943
_cleanup_free_ char *suffix = NULL;
979-
sd_device *usbdev;
944+
sd_device *usbdev, *pcidev;
980945
int r;
981946

982947
assert(dev);
983948
assert(prefix);
984-
assert(names);
985949

986950
/* USB device */
987951

@@ -994,19 +958,12 @@ static int names_usb(sd_device *dev, const char *prefix, NetNames *names, bool t
994958
return r;
995959

996960
/* If the USB bus is on PCI bus, then suffix the USB specifier to the name based on the PCI bus. */
997-
if (names) {
998-
char buf[ALTIFNAMSIZ];
999-
if (names->pci_path[0] &&
1000-
snprintf_ok(buf, sizeof buf, "%s%s%s", prefix, names->pci_path, suffix))
1001-
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", buf);
1002-
1003-
if (names->pci_slot[0] &&
1004-
snprintf_ok(buf, sizeof buf, "%s%s%s", prefix, names->pci_slot, suffix))
1005-
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", buf);
1006-
}
961+
r = sd_device_get_parent_with_subsystem_devtype(usbdev, "pci", NULL, &pcidev);
962+
if (r >= 0)
963+
return names_pci_slot(dev, pcidev, prefix, suffix, test);
1007964

1008-
if (!naming_scheme_has(NAMING_USB_HOST))
1009-
return 0;
965+
if (r != -ENOENT || !naming_scheme_has(NAMING_USB_HOST))
966+
return r;
1010967

1011968
/* Otherwise, e.g. on-chip asics that have USB ports, use the USB specifier as is. */
1012969
char str[ALTIFNAMSIZ];
@@ -1046,35 +1003,27 @@ static int get_bcma_specifier(sd_device *dev, char **ret) {
10461003
return 0;
10471004
}
10481005

1049-
static int names_bcma(sd_device *dev, const char *prefix, NetNames *names, bool test) {
1006+
static int names_bcma(sd_device *dev, const char *prefix, bool test) {
10501007
_cleanup_free_ char *suffix = NULL;
1051-
sd_device *bcmadev;
1008+
sd_device *bcmadev, *pcidev;
10521009
int r;
10531010

10541011
assert(dev);
10551012
assert(prefix);
1056-
assert(names);
1057-
1058-
/* Broadcom bus */
10591013

10601014
r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
10611015
if (r < 0)
10621016
return log_device_debug_errno(dev, r, "Could not get bcma parent device: %m");
10631017

1018+
r = sd_device_get_parent_with_subsystem_devtype(bcmadev, "pci", NULL, &pcidev);
1019+
if (r < 0)
1020+
return log_device_debug_errno(dev, r, "Could not get pci parent device: %m");
1021+
10641022
r = get_bcma_specifier(bcmadev, &suffix);
10651023
if (r < 0)
10661024
return r;
10671025

1068-
char str[ALTIFNAMSIZ];
1069-
if (names->pci_path[0] &&
1070-
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names->pci_path, suffix))
1071-
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
1072-
1073-
if (names->pci_slot[0] &&
1074-
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names->pci_slot, suffix))
1075-
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
1076-
1077-
return 0;
1026+
return names_pci_slot(dev, pcidev, prefix, suffix, test);
10781027
}
10791028

10801029
static int names_ccw(sd_device *dev, const char *prefix, bool test) {
@@ -1382,7 +1331,6 @@ static int device_is_stacked(sd_device *dev) {
13821331
static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
13831332
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
13841333
const char *prefix;
1385-
NetNames names = {};
13861334
int r;
13871335

13881336
/* skip stacked devices, like VLANs, ... */
@@ -1405,34 +1353,9 @@ static int builtin_net_id(UdevEvent *event, int argc, char *argv[], bool test) {
14051353
(void) names_platform(dev, prefix, test);
14061354
(void) names_netdevsim(dev, prefix, test);
14071355
(void) names_xen(dev, prefix, test);
1408-
1409-
/* get PCI based path names */
1410-
r = names_pci(dev, prefix, &names, test);
1411-
if (r == -ENOENT)
1412-
(void) names_usb(dev, prefix, NULL, test);
1413-
if (r < 0)
1414-
return 0;
1415-
1416-
/* plain PCI device */
1417-
if (names.type == NET_PCI) {
1418-
char str[ALTIFNAMSIZ];
1419-
1420-
if (names.pci_onboard[0] &&
1421-
snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard))
1422-
udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
1423-
1424-
if (names.pci_path[0] &&
1425-
snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_path))
1426-
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
1427-
1428-
if (names.pci_slot[0] &&
1429-
snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot))
1430-
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
1431-
return 0;
1432-
}
1433-
1434-
(void) names_usb(dev, prefix, &names, test);
1435-
(void) names_bcma(dev, prefix, &names, test);
1356+
(void) names_pci(dev, prefix, test);
1357+
(void) names_usb(dev, prefix, test);
1358+
(void) names_bcma(dev, prefix, test);
14361359

14371360
return 0;
14381361
}

0 commit comments

Comments
 (0)