mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
Merge pull request #14360 from yuwata/udev-alternative-names-policy
udev: introduce AlternativeNamesPolicy= setting
This commit is contained in:
commit
0d9ac4ea57
@ -356,12 +356,24 @@
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term><varname>AlternativeNamesPolicy=</varname></term>
|
||||
<listitem>
|
||||
<para>A space-separated list of policies by which the interface's alternative names
|
||||
should be set. Each of the policies may fail, and all successful policies are used. The
|
||||
available policies are <literal>database</literal>, <literal>onboard</literal>,
|
||||
<literal>slot</literal>, <literal>path</literal>, and <literal>mac</literal>. If the
|
||||
kernel does not support the alternative names, then this setting will be ignored.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term><varname>AlternativeName=</varname></term>
|
||||
<listitem>
|
||||
<para>The alternative interface name to use. This option can be specified multiple times.
|
||||
If the empty string is assigned to this option, the list is reset, and all prior assignments
|
||||
have no effect.</para>
|
||||
have no effect. If the kernel does not support the alternative names, then this setting will
|
||||
be ignored.</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
|
@ -12,4 +12,5 @@ OriginalName=*
|
||||
|
||||
[Link]
|
||||
NamePolicy=keep kernel database onboard slot path
|
||||
AlternativeNamesPolicy=database onboard slot path
|
||||
MACAddressPolicy=persistent
|
||||
|
@ -19,40 +19,41 @@ struct ConfigPerfItem;
|
||||
%struct-type
|
||||
%includes
|
||||
%%
|
||||
Match.MACAddress, config_parse_hwaddrs, 0, offsetof(link_config, match_mac)
|
||||
Match.OriginalName, config_parse_match_ifnames, 0, offsetof(link_config, match_name)
|
||||
Match.Path, config_parse_match_strv, 0, offsetof(link_config, match_path)
|
||||
Match.Driver, config_parse_match_strv, 0, offsetof(link_config, match_driver)
|
||||
Match.Type, config_parse_match_strv, 0, offsetof(link_config, match_type)
|
||||
Match.Property, config_parse_match_property, 0, offsetof(link_config, match_property)
|
||||
Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(link_config, conditions)
|
||||
Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(link_config, conditions)
|
||||
Match.KernelCommandLine, config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, conditions)
|
||||
Match.KernelVersion, config_parse_net_condition, CONDITION_KERNEL_VERSION, offsetof(link_config, conditions)
|
||||
Match.Architecture, config_parse_net_condition, CONDITION_ARCHITECTURE, offsetof(link_config, conditions)
|
||||
Link.Description, config_parse_string, 0, offsetof(link_config, description)
|
||||
Link.MACAddressPolicy, config_parse_mac_address_policy, 0, offsetof(link_config, mac_address_policy)
|
||||
Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac)
|
||||
Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy)
|
||||
Link.Name, config_parse_ifname, 0, offsetof(link_config, name)
|
||||
Link.AlternativeName, config_parse_ifnames, 1, offsetof(link_config, alternative_names)
|
||||
Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias)
|
||||
Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu)
|
||||
Link.BitsPerSecond, config_parse_si_size, 0, offsetof(link_config, speed)
|
||||
Link.Duplex, config_parse_duplex, 0, offsetof(link_config, duplex)
|
||||
Link.AutoNegotiation, config_parse_tristate, 0, offsetof(link_config, autonegotiation)
|
||||
Link.WakeOnLan, config_parse_wol, 0, offsetof(link_config, wol)
|
||||
Link.Port, config_parse_port, 0, offsetof(link_config, port)
|
||||
Link.GenericSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GSO])
|
||||
Link.TCPSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO])
|
||||
Link.TCP6SegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO6])
|
||||
Link.UDPSegmentationOffload, config_parse_warn_compat, DISABLED_LEGACY, 0
|
||||
Link.GenericReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GRO])
|
||||
Link.LargeReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_LRO])
|
||||
Link.RxChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.TxChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.OtherChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.CombinedChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.Advertise, config_parse_advertise, 0, offsetof(link_config, advertise)
|
||||
Link.RxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring)
|
||||
Link.TxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring)
|
||||
Match.MACAddress, config_parse_hwaddrs, 0, offsetof(link_config, match_mac)
|
||||
Match.OriginalName, config_parse_match_ifnames, 0, offsetof(link_config, match_name)
|
||||
Match.Path, config_parse_match_strv, 0, offsetof(link_config, match_path)
|
||||
Match.Driver, config_parse_match_strv, 0, offsetof(link_config, match_driver)
|
||||
Match.Type, config_parse_match_strv, 0, offsetof(link_config, match_type)
|
||||
Match.Property, config_parse_match_property, 0, offsetof(link_config, match_property)
|
||||
Match.Host, config_parse_net_condition, CONDITION_HOST, offsetof(link_config, conditions)
|
||||
Match.Virtualization, config_parse_net_condition, CONDITION_VIRTUALIZATION, offsetof(link_config, conditions)
|
||||
Match.KernelCommandLine, config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, conditions)
|
||||
Match.KernelVersion, config_parse_net_condition, CONDITION_KERNEL_VERSION, offsetof(link_config, conditions)
|
||||
Match.Architecture, config_parse_net_condition, CONDITION_ARCHITECTURE, offsetof(link_config, conditions)
|
||||
Link.Description, config_parse_string, 0, offsetof(link_config, description)
|
||||
Link.MACAddressPolicy, config_parse_mac_address_policy, 0, offsetof(link_config, mac_address_policy)
|
||||
Link.MACAddress, config_parse_hwaddr, 0, offsetof(link_config, mac)
|
||||
Link.NamePolicy, config_parse_name_policy, 0, offsetof(link_config, name_policy)
|
||||
Link.Name, config_parse_ifname, 0, offsetof(link_config, name)
|
||||
Link.AlternativeName, config_parse_ifnames, 1, offsetof(link_config, alternative_names)
|
||||
Link.AlternativeNamesPolicy, config_parse_alternative_names_policy, 0, offsetof(link_config, alternative_names_policy)
|
||||
Link.Alias, config_parse_ifalias, 0, offsetof(link_config, alias)
|
||||
Link.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(link_config, mtu)
|
||||
Link.BitsPerSecond, config_parse_si_size, 0, offsetof(link_config, speed)
|
||||
Link.Duplex, config_parse_duplex, 0, offsetof(link_config, duplex)
|
||||
Link.AutoNegotiation, config_parse_tristate, 0, offsetof(link_config, autonegotiation)
|
||||
Link.WakeOnLan, config_parse_wol, 0, offsetof(link_config, wol)
|
||||
Link.Port, config_parse_port, 0, offsetof(link_config, port)
|
||||
Link.GenericSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GSO])
|
||||
Link.TCPSegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO])
|
||||
Link.TCP6SegmentationOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_TSO6])
|
||||
Link.UDPSegmentationOffload, config_parse_warn_compat, DISABLED_LEGACY, 0
|
||||
Link.GenericReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_GRO])
|
||||
Link.LargeReceiveOffload, config_parse_tristate, 0, offsetof(link_config, features[NET_DEV_FEAT_LRO])
|
||||
Link.RxChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.TxChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.OtherChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.CombinedChannels, config_parse_channel, 0, offsetof(link_config, channels)
|
||||
Link.Advertise, config_parse_advertise, 0, offsetof(link_config, advertise)
|
||||
Link.RxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring)
|
||||
Link.TxBufferSize, config_parse_nic_buffer_size, 0, offsetof(link_config, ring)
|
||||
|
@ -59,6 +59,7 @@ static void link_config_free(link_config *link) {
|
||||
free(link->name_policy);
|
||||
free(link->name);
|
||||
strv_free(link->alternative_names);
|
||||
free(link->alternative_names_policy);
|
||||
free(link->alias);
|
||||
|
||||
free(link);
|
||||
@ -326,6 +327,7 @@ static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr
|
||||
|
||||
int link_config_apply(link_config_ctx *ctx, link_config *config,
|
||||
sd_device *device, const char **name) {
|
||||
_cleanup_strv_free_ char **altnames = NULL;
|
||||
struct ether_addr generated_mac;
|
||||
struct ether_addr *mac = NULL;
|
||||
const char *new_name = NULL;
|
||||
@ -401,7 +403,7 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
|
||||
}
|
||||
|
||||
if (ctx->enable_name_policy && config->name_policy)
|
||||
for (NamePolicy *p = config->name_policy; !new_name && *p != _NAMEPOLICY_INVALID; p++) {
|
||||
for (NamePolicy *p = config->name_policy; *p != _NAMEPOLICY_INVALID; p++) {
|
||||
policy = *p;
|
||||
|
||||
switch (policy) {
|
||||
@ -438,6 +440,8 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
|
||||
default:
|
||||
assert_not_reached("invalid policy");
|
||||
}
|
||||
if (ifname_valid(new_name))
|
||||
break;
|
||||
}
|
||||
|
||||
if (new_name)
|
||||
@ -459,9 +463,52 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name);
|
||||
|
||||
r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names);
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
|
||||
if (config->alternative_names) {
|
||||
altnames = strv_copy(config->alternative_names);
|
||||
if (!altnames)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (config->alternative_names_policy)
|
||||
for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) {
|
||||
const char *n;
|
||||
|
||||
switch (*p) {
|
||||
case NAMEPOLICY_DATABASE:
|
||||
(void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n);
|
||||
break;
|
||||
case NAMEPOLICY_ONBOARD:
|
||||
(void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n);
|
||||
break;
|
||||
case NAMEPOLICY_SLOT:
|
||||
(void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n);
|
||||
break;
|
||||
case NAMEPOLICY_PATH:
|
||||
(void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n);
|
||||
break;
|
||||
case NAMEPOLICY_MAC:
|
||||
(void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n);
|
||||
break;
|
||||
default:
|
||||
assert_not_reached("invalid policy");
|
||||
}
|
||||
if (!isempty(n)) {
|
||||
r = strv_extend(&altnames, n);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
|
||||
if (new_name)
|
||||
strv_remove(altnames, new_name);
|
||||
strv_remove(altnames, old_name);
|
||||
strv_uniq(altnames);
|
||||
|
||||
r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, altnames);
|
||||
if (r == -EOPNOTSUPP)
|
||||
log_debug_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s, ignoring: %m", old_name);
|
||||
else if (r < 0)
|
||||
return log_warning_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s: %m", old_name);
|
||||
|
||||
*name = new_name;
|
||||
|
||||
@ -509,3 +556,16 @@ DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
|
||||
DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
|
||||
_NAMEPOLICY_INVALID,
|
||||
"Failed to parse interface name policy");
|
||||
|
||||
static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
|
||||
[NAMEPOLICY_DATABASE] = "database",
|
||||
[NAMEPOLICY_ONBOARD] = "onboard",
|
||||
[NAMEPOLICY_SLOT] = "slot",
|
||||
[NAMEPOLICY_PATH] = "path",
|
||||
[NAMEPOLICY_MAC] = "mac",
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
|
||||
DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy,
|
||||
_NAMEPOLICY_INVALID,
|
||||
"Failed to parse alternative names policy");
|
||||
|
@ -47,6 +47,7 @@ struct link_config {
|
||||
struct ether_addr *mac;
|
||||
MACAddressPolicy mac_address_policy;
|
||||
NamePolicy *name_policy;
|
||||
NamePolicy *alternative_names_policy;
|
||||
char *name;
|
||||
char **alternative_names;
|
||||
char *alias;
|
||||
@ -79,6 +80,9 @@ int link_get_driver(link_config_ctx *ctx, sd_device *device, char **ret);
|
||||
const char *name_policy_to_string(NamePolicy p) _const_;
|
||||
NamePolicy name_policy_from_string(const char *p) _pure_;
|
||||
|
||||
const char *alternative_names_policy_to_string(NamePolicy p) _const_;
|
||||
NamePolicy alternative_names_policy_from_string(const char *p) _pure_;
|
||||
|
||||
const char *mac_address_policy_to_string(MACAddressPolicy p) _const_;
|
||||
MACAddressPolicy mac_address_policy_from_string(const char *p) _pure_;
|
||||
|
||||
@ -87,3 +91,4 @@ const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, GPERF_LEN
|
||||
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_mac_address_policy);
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_name_policy);
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_alternative_names_policy);
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/pci_regs.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
@ -56,22 +57,22 @@ struct netnames {
|
||||
bool mac_valid;
|
||||
|
||||
sd_device *pcidev;
|
||||
char pci_slot[IFNAMSIZ];
|
||||
char pci_path[IFNAMSIZ];
|
||||
char pci_onboard[IFNAMSIZ];
|
||||
char pci_slot[ALTIFNAMSIZ];
|
||||
char pci_path[ALTIFNAMSIZ];
|
||||
char pci_onboard[ALTIFNAMSIZ];
|
||||
const char *pci_onboard_label;
|
||||
|
||||
char usb_ports[IFNAMSIZ];
|
||||
char bcma_core[IFNAMSIZ];
|
||||
char ccw_busid[IFNAMSIZ];
|
||||
char vio_slot[IFNAMSIZ];
|
||||
char platform_path[IFNAMSIZ];
|
||||
char netdevsim_path[IFNAMSIZ];
|
||||
char usb_ports[ALTIFNAMSIZ];
|
||||
char bcma_core[ALTIFNAMSIZ];
|
||||
char ccw_busid[ALTIFNAMSIZ];
|
||||
char vio_slot[ALTIFNAMSIZ];
|
||||
char platform_path[ALTIFNAMSIZ];
|
||||
char netdevsim_path[ALTIFNAMSIZ];
|
||||
};
|
||||
|
||||
struct virtfn_info {
|
||||
sd_device *physfn_pcidev;
|
||||
char suffix[IFNAMSIZ];
|
||||
char suffix[ALTIFNAMSIZ];
|
||||
};
|
||||
|
||||
/* skip intermediate virtio devices */
|
||||
@ -104,7 +105,7 @@ static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn
|
||||
_cleanup_free_ char *virtfn_pci_syspath = NULL;
|
||||
struct dirent *dent;
|
||||
_cleanup_closedir_ DIR *dir = NULL;
|
||||
char suffix[IFNAMSIZ];
|
||||
char suffix[ALTIFNAMSIZ];
|
||||
int r;
|
||||
|
||||
assert(dev);
|
||||
@ -819,7 +820,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
r = names_mac(dev, &names);
|
||||
if (r >= 0 && names.mac_valid) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
|
||||
names.mac[0], names.mac[1], names.mac[2],
|
||||
@ -831,7 +832,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* get path names for Linux on System z network devices */
|
||||
if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid))
|
||||
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
|
||||
@ -840,7 +841,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* get ibmveth/ibmvnic slot-based names. */
|
||||
if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot))
|
||||
udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
|
||||
@ -849,7 +850,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* get ACPI path names for ARM64 platform devices */
|
||||
if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path))
|
||||
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
|
||||
@ -858,7 +859,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* get netdevsim path names */
|
||||
if (names_netdevsim(dev, &names) >= 0 && names.type == NET_NETDEVSIM) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.netdevsim_path))
|
||||
udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
|
||||
@ -872,7 +873,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* plain PCI device */
|
||||
if (names.type == NET_PCI) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (names.pci_onboard[0] &&
|
||||
snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard))
|
||||
@ -896,7 +897,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* USB device */
|
||||
if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (names.pci_path[0] &&
|
||||
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports))
|
||||
@ -910,7 +911,7 @@ static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
|
||||
/* Broadcom bus */
|
||||
if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
|
||||
char str[IFNAMSIZ];
|
||||
char str[ALTIFNAMSIZ];
|
||||
|
||||
if (names.pci_path[0] &&
|
||||
snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.bcma_core))
|
||||
|
@ -16,6 +16,7 @@ MACAddressPolicy=
|
||||
MACAddress=
|
||||
NamePolicy=
|
||||
Name=
|
||||
AlternativeNamesPolicy=
|
||||
AlternativeName=
|
||||
Alias=
|
||||
MTUBytes=
|
||||
|
Loading…
Reference in New Issue
Block a user