HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/udev/udev-builtin-net_id.c
Git blob: d368fb23372308cd199cfd59f74ad273d4f09c25
Lines 1001–1478 of 1,478
bool is_child_function = !!subfunc_suffix;
if (naming_scheme_has(NAMING_SR_IOV_V) &&
get_virtfn_info(parent, &physfn_pcidev, &virtfn_suffix) >= 0) {
parent = physfn_pcidev;
is_child_function = true;
}
if (!is_child_function)
(void) names_pci_onboard_label(event, parent, prefix);
/* Compose the final suffix in PF -> [VF ->] SF order, e.g. "v0", "S88", or "v0S88". */
if (virtfn_suffix && subfunc_suffix) {
combined_suffix = strjoin(virtfn_suffix, subfunc_suffix);
if (!combined_suffix)
return log_oom_debug();
suffix = combined_suffix;
} else
suffix = virtfn_suffix ?: subfunc_suffix;
(void) names_pci_onboard(event, parent, prefix, suffix);
(void) names_pci_slot(event, parent, prefix, suffix);
return 0;
}
static int get_usb_specifier(sd_device *dev, char **ret) {
char *ports, *config, *interf, *buf;
const char *sysname;
int r;
assert(dev);
assert(ret);
r = sd_device_get_sysname(dev, &sysname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
/* get USB port number chain, configuration, interface */
const char *s = strchr(sysname, '-');
if (!s)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"sysname \"%s\" does not have '-' in the expected place.", sysname);
ports = strdupa_safe(s + 1);
char *t = strchr(ports, ':');
if (!t)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"sysname \"%s\" does not have ':' in the expected place.", sysname);
*t = '\0';
config = t + 1;
t = strchr(config, '.');
if (!t)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"sysname \"%s\" does not have '.' in the expected place.", sysname);
*t = '\0';
interf = t + 1;
/* prefix every port number in the chain with "u" */
string_replace_char(ports, '.', 'u');
/* suppress the common config == 1 */
if (streq(config, "1"))
config = NULL;
/* suppress the interface == 0 */
if (streq(interf, "0"))
interf = NULL;
buf = strjoin("u", ports,
config ? "c" : "", strempty(config),
interf ? "i" : "", strempty(interf));
if (!buf)
return log_oom_debug();
log_device_debug(dev, "USB name identifier: ports=%s config=%s interface=%s %s %s",
ports, strna(config), strna(interf),
glyph(GLYPH_ARROW_RIGHT), buf);
*ret = buf;
return 0;
}
static int names_usb(UdevEvent *event, const char *prefix) {
sd_device *usbdev, *pcidev, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *suffix = NULL;
int r;
assert(prefix);
/* USB device */
r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
if (r < 0)
return log_device_debug_errno(dev, r, "Could not find USB parent device: %m");
r = get_usb_specifier(usbdev, &suffix);
if (r < 0)
return r;
/* If the USB bus is on PCI bus, then suffix the USB specifier to the name based on the PCI bus. */
r = sd_device_get_parent_with_subsystem_devtype(usbdev, "pci", NULL, &pcidev);
if (r >= 0)
return names_pci_slot(event, pcidev, prefix, suffix);
if (r != -ENOENT || !naming_scheme_has(NAMING_USB_HOST))
return log_device_debug_errno(usbdev, r, "Failed to get parent PCI bus: %m");
/* Otherwise, e.g. on-chip asics that have USB ports, use the USB specifier as is. */
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%s%s", prefix, suffix))
udev_builtin_add_property(event, "ID_NET_NAME_PATH", str);
return 0;
}
static int get_bcma_specifier(sd_device *dev, char **ret) {
const char *sysname;
char *buf = NULL;
unsigned core;
int r;
assert(dev);
assert(ret);
r = sd_device_get_sysname(dev, &sysname);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
/* bus num:core num */
r = sscanf(sysname, "bcma%*u:%u", &core);
if (r != 1)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"Failed to parse BCMA device information.");
/* suppress the common core == 0 */
if (core > 0 && asprintf(&buf, "b%u", core) < 0)
return log_oom_debug();
log_device_debug(dev, "BCMA core identifier: core=%u %s \"%s\"",
core, glyph(GLYPH_ARROW_RIGHT), strna(buf));
*ret = buf;
return 0;
}
static int names_bcma(UdevEvent *event, const char *prefix) {
sd_device *bcmadev, *pcidev, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *suffix = NULL;
int r;
assert(prefix);
r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
if (r < 0)
return log_device_debug_errno(dev, r, "Could not get BCMA parent device: %m");
r = sd_device_get_parent_with_subsystem_devtype(bcmadev, "pci", NULL, &pcidev);
if (r < 0)
return log_device_debug_errno(dev, r, "Could not get PCI parent device: %m");
r = get_bcma_specifier(bcmadev, &suffix);
if (r < 0)
return r;
return names_pci_slot(event, pcidev, prefix, suffix);
}
static int names_ccw(UdevEvent *event, const char *prefix) {
sd_device *cdev, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
const char *bus_id;
size_t bus_id_start, bus_id_len;
int r;
assert(prefix);
/* get path names for Linux on System z network devices */
if (get_matching_parent(dev, STRV_MAKE("ccwgroup", "ccw"), /* skip_virtio= */ true, &cdev) < 0)
return 0;
log_device_debug(dev, "Device is CCW.");
/* Retrieve bus-ID of the CCW device. The bus-ID uniquely
* identifies the network device on the Linux on System z channel
* subsystem. Note that the bus-ID contains lowercase characters.
*/
r = sd_device_get_sysname(cdev, &bus_id);
if (r < 0)
return log_device_debug_errno(cdev, r, "Failed to get sysname: %m");
/* Check the length of the bus-ID. Rely on the fact that the kernel provides a correct bus-ID;
* alternatively, improve this check and parse and verify each bus-ID part...
*/
bus_id_len = strlen(bus_id);
if (!IN_SET(bus_id_len, 8, 9))
return log_device_debug_errno(cdev, SYNTHETIC_ERRNO(EINVAL), "Invalid bus_id: '%s'", bus_id);
/* Strip leading zeros from the bus id for aesthetic purposes. This
* keeps the ccw names stable, yet much shorter in general case of
* bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
* not prepended when it is zero. Preserve the last 0 for 0.0.0000.
*/
bus_id_start = strspn(bus_id, ".0");
bus_id += bus_id_start < bus_id_len ? bus_id_start : bus_id_len - 1;
/* Use the CCW bus-ID as network device name */
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%sc%s", prefix, bus_id))
udev_builtin_add_property(event, "ID_NET_NAME_PATH", str);
log_device_debug(dev, "CCW identifier: ccw_busid=%s %s \"%s\"",
bus_id, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 0;
}
/* IEEE Organizationally Unique Identifier vendor string */
static int ieee_oui(UdevEvent *event, const struct hw_addr_data *hw_addr) {
char str[32];
assert(event);
assert(hw_addr);
if (hw_addr->length != 6)
return -EOPNOTSUPP;
/* skip commonly misused 00:00:00 (Xerox) prefix */
if (hw_addr->bytes[0] == 0 &&
hw_addr->bytes[1] == 0 &&
hw_addr->bytes[2] == 0)
return -EINVAL;
xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X",
hw_addr->bytes[0],
hw_addr->bytes[1],
hw_addr->bytes[2],
hw_addr->bytes[3],
hw_addr->bytes[4],
hw_addr->bytes[5]);
return udev_builtin_hwdb_lookup(event, NULL, str, NULL);
}
static int names_mac(UdevEvent *event, const char *prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
unsigned iftype, assign_type;
struct hw_addr_data hw_addr;
const char *s;
int r;
assert(prefix);
r = device_get_sysattr_unsigned_filtered(dev, "type", &iftype);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to read 'type' attribute: %m");
/* The persistent part of a hardware address of an InfiniBand NIC is 8 bytes long. We cannot
* fit this much in an iface name.
* TODO: but it can be used as alternative names?? */
if (iftype == ARPHRD_INFINIBAND)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EOPNOTSUPP),
"Not generating MAC name for infiniband device.");
/* check for NET_ADDR_PERM, skip random MAC addresses */
r = device_get_sysattr_unsigned_filtered(dev, "addr_assign_type", &assign_type);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to read/parse addr_assign_type: %m");
if (assign_type != NET_ADDR_PERM)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
"addr_assign_type=%u, MAC address is not permanent.", assign_type);
r = device_get_sysattr_safe_string_filtered(dev, "address", &s);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to read 'address' attribute: %m");
r = parse_hw_addr(s, &hw_addr);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse 'address' attribute: %m");
if (hw_addr.length != 6)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EOPNOTSUPP),
"Not generating MAC name for device with MAC address of length %zu.",
hw_addr.length);
char str[ALTIFNAMSIZ];
xsprintf(str, "%sx%s", prefix, HW_ADDR_TO_STR_FULL(&hw_addr, HW_ADDR_TO_STRING_NO_COLON));
udev_builtin_add_property(event, "ID_NET_NAME_MAC", str);
log_device_debug(dev, "MAC address identifier: hw_addr=%s %s %s",
HW_ADDR_TO_STR(&hw_addr),
glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
(void) ieee_oui(event, &hw_addr);
return 0;
}
static int names_netdevsim(UdevEvent *event, const char *prefix) {
sd_device *netdevsimdev, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
const char *phys_port_name;
unsigned addr;
int r;
assert(prefix);
/* get netdevsim path names */
if (!naming_scheme_has(NAMING_NETDEVSIM))
return 0;
r = sd_device_get_parent_with_subsystem_devtype(dev, "netdevsim", NULL, &netdevsimdev);
if (r < 0)
return r;
r = device_get_sysnum_unsigned(netdevsimdev, &addr);
if (r < 0)
return log_device_debug_errno(netdevsimdev, r, "Failed to get device sysnum: %m");
r = device_get_sysattr_safe_string_filtered(dev, "phys_port_name", &phys_port_name);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get 'phys_port_name' attribute: %m");
if (isempty(phys_port_name))
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EOPNOTSUPP),
"The 'phys_port_name' attribute is empty.");
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%si%un%s", prefix, addr, phys_port_name))
udev_builtin_add_property(event, "ID_NET_NAME_PATH", str);
log_device_debug(dev, "Netdevsim identifier: address=%u, port_name=%s %s %s",
addr, phys_port_name, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 0;
}
static int names_xen(UdevEvent *event, const char *prefix) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
_cleanup_free_ char *vif = NULL;
const char *p;
unsigned id;
int r;
assert(prefix);
/* get xen vif "slot" based names. */
if (!naming_scheme_has(NAMING_XEN_VIF))
return 0;
/* check if our direct parent is a Xen VIF device with no other bus in-between */
if (get_matching_parent(dev, STRV_MAKE("xen"), /* skip_virtio= */ false, NULL) < 0)
return 0;
/* Use the vif-n name to extract "n" */
r = get_first_syspath_component(dev, "/sys/devices/", &vif);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to get Xen VIF name: %m");
p = startswith(vif, "vif-");
if (!p)
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid vif name: '%s'.", vif);
r = safe_atou_full(p, SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_ZERO |
SAFE_ATO_REFUSE_LEADING_WHITESPACE | 10, &id);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to parse vif index from '%s': %m", p);
char str[ALTIFNAMSIZ];
if (snprintf_ok(str, sizeof str, "%sX%u", prefix, id))
udev_builtin_add_property(event, "ID_NET_NAME_SLOT", str);
log_device_debug(dev, "Xen identifier: id=%u %s %s",
id, glyph(GLYPH_ARROW_RIGHT), str + strlen(prefix));
return 0;
}
static int get_ifname_prefix(sd_device *dev, const char **ret) {
unsigned iftype;
int r;
assert(dev);
assert(ret);
r = device_get_sysattr_unsigned_filtered(dev, "type", &iftype);
if (r < 0)
return r;
/* handle only ARPHRD_ETHER, ARPHRD_SLIP, ARPHRD_INFINIBAND, and ARPHDR_MCTP devices */
switch (iftype) {
case ARPHRD_ETHER: {
if (device_is_devtype(dev, "wlan") > 0)
*ret = "wl";
else if (device_is_devtype(dev, "wwan") > 0)
*ret = "ww";
else
*ret = "en";
return 0;
}
case ARPHRD_INFINIBAND:
if (!naming_scheme_has(NAMING_INFINIBAND))
return -EOPNOTSUPP;
*ret = "ib";
return 0;
case ARPHRD_SLIP:
*ret = "sl";
return 0;
case ARPHRD_MCTP:
if (!naming_scheme_has(NAMING_MCTP))
return -EOPNOTSUPP;
*ret = "mc";
return 0;
default:
return -EOPNOTSUPP;
}
}
static int device_is_stacked(sd_device *dev) {
int ifindex, iflink, r;
assert(dev);
r = sd_device_get_ifindex(dev, &ifindex);
if (r < 0)
return r;
r = device_get_sysattr_int_filtered(dev, "iflink", &iflink);
if (r < 0)
return r;
return ifindex != iflink;
}
static int builtin_net_id(UdevEvent *event, int argc, char *argv[]) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
const char *prefix;
int r;
/* skip stacked devices, like VLANs, ... */
r = device_is_stacked(dev);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to check if the device is stacked: %m");
if (r > 0)
return 0;
r = get_ifname_prefix(dev, &prefix);
if (r < 0) {
log_device_debug_errno(dev, r, "Failed to determine prefix for network interface naming, ignoring: %m");
return 0;
}
udev_builtin_add_property(event, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
(void) names_mac(event, prefix);
(void) names_devicetree(event, prefix);
(void) names_ccw(event, prefix);
(void) names_vio(event, prefix);
(void) names_platform(event, prefix);
(void) names_netdevsim(event, prefix);
(void) names_xen(event, prefix);
(void) names_pci(event, prefix);
(void) names_usb(event, prefix);
(void) names_bcma(event, prefix);
return 0;
}
static int builtin_net_id_init(void) {
/* Load naming scheme here to suppress log messages in workers. */
naming_scheme();
return 0;
}
const UdevBuiltin udev_builtin_net_id = {
.name = "net_id",
.cmd = builtin_net_id,
.init = builtin_net_id_init,
.help = "Network device properties",
};