net: introduce alternative names for network interfaces
Subsystems (4)
cover net: introduce alternative names for network interfaces
From: Jiri Pirko <[email protected]>
In the past, there was repeatedly discussed the IFNAMSIZ (16) limit for netdevice name length. Now when we have PF and VF representors with port names like "pfXvfY", it became quite common to hit this limit:
0123456789012345
enp131s0f1npf0vf6
enp131s0f1npf0vf22
Udev cannot rename these interfaces out-of-the-box and user needs to create custom rules to handle them.
Also, udev has multiple schemes of netdev names. From udev code:
* Type of names:
* b<number> - BCMA bus core number * c<bus_id> - bus id of a grouped CCW or CCW device, * with all leading zeros stripped [s390] * o<index>[n<phys_port_name>|d<dev_port>] * - on-board device index number * s<slot>[f<function>][n<phys_port_name>|d<dev_port>] * - hotplug slot index number * x<MAC> - MAC address * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>] * - PCI geographical location * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>] * - USB port number chain * v<slot> - VIO slot number (IBM PowerVM) * a<vendor><model>i<instance> - Platform bus ACPI instance id * i<addr>n<phys_port_name> - Netdevsim bus address and port name
One device can be often renamed by multiple patterns at the same time (e.g. pci address/mac).
This patchset introduces alternative names for network interfaces. Main goal is to:
1) Overcome the IFNAMSIZ limitation (altname limitation is 128 bytes)
2) Allow to have multiple names at the same time (multiple udev patterns)
3) Allow to use alternative names as handle for commands
The patchset introduces two new commands to add/delete list of properties. Currently only alternative names are implemented but the ifrastructure could be easily extended later on. This is very similar to the list of vlan and tunnels being added/deleted to/from bridge ports.
See following examples.
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff-> Add alternative names for dummy0:
$ ip link prop add dummy0 altname someothername
$ ip link prop add dummy0 altname someotherveryveryveryverylongname
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname someothername
altname someotherveryveryveryverylongname
$ ip link show someotherveryveryveryverylongname
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname someothername
altname someotherveryveryveryverylongname-> Add bridge brx, add it's alternative name and use alternative names to
do enslavement.
$ ip link add name brx type bridge
$ ip link prop add brx altname mypersonalsuperspecialbridge
$ ip link set someotherveryveryveryverylongname master mypersonalsuperspecialbridge
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname someothername
altname someotherveryveryveryverylongname
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname mypersonalsuperspecialbridge-> Add ipv4 address to the bridge using alternative name:
$ ip addr add 192.168.0.1/24 dev mypersonalsuperspecialbridge
$ ip addr show mypersonalsuperspecialbridge
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname mypersonalsuperspecialbridge
inet 192.168.0.1/24 scope global brx
valid_lft forever preferred_lft forever-> Delete one of dummy0 alternative names:
$ ip link prop del dummy0 altname someotherveryveryveryverylongname
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname someothername
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname mypersonalsuperspecialbridge-> Add multiple alternative names at once
$ ip link prop add dummy0 altname a altname b altname c altname d
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname someothername
altname a
altname b
altname c
altname d
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
altname mypersonalsuperspecialbridgeJiri Pirko (7):
net: procfs: use index hashlist instead of name hashlist
net: introduce name_node struct to be used in hashlist
net: rtnetlink: add linkprop commands to add and delete alternative
ifnames
net: rtnetlink: put alternative names to getlink message
net: rtnetlink: unify the code in __rtnl_newlink get dev with the rest
net: rtnetlink: introduce helper to get net_device instance by ifname
net: rtnetlink: add possibility to use alternative names as message
handle
include/linux/netdevice.h | 14 ++- include/uapi/linux/if.h | 1 + include/uapi/linux/if_link.h | 2 + include/uapi/linux/rtnetlink.h | 7 ++ net/core/dev.c | 153 +++++++++++++++++++++--- net/core/net-procfs.c | 4 +- net/core/rtnetlink.c | 206 +++++++++++++++++++++++++++++---- security/selinux/nlmsgtab.c | 4 +- 8 files changed, 348 insertions(+), 43 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/7 net: procfs: use index hashlist instead of name hashlist
From: Jiri Pirko <[email protected]>
Name hashlist is going to be used for more than just dev->name, so use rather index hashlist for iteration over net_device instances.
--- net/core/net-procfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/net-procfs.c +2 -2
@@ -20,8 +20,8 @@ static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loffstruct hlist_head *h;unsigned int count =0 , offset =get_offset (*pos); - h = &net->dev_name_head[get_bucket (*pos)]; -hlist_for_each_entry_rcu (dev, h, name_hlist) { + h = &net->dev_index_head[get_bucket (*pos)]; +hlist_for_each_entry_rcu (dev, h, index_hlist) {if (++count == offset)return dev; }
Signature · 1 line
2.21.0
PATCH 2/7 net: introduce name_node struct to be used in hashlist
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce name_node structure to hold name of device and put it into hashlist instead of putting there struct net_device directly. Add a necessary infrastructure to manipulate the hashlist. This prepares the code to use the same hashlist for alternative names introduced later in this set.
---rfc->v1:
- make char *name const in struct netdev_name_node and in netdev_name_node_alloc()
- use INIT_HLIST_NODE in netdev_name_node_alloc() and change kzalloc to kmalloc.
- added patch description
--- include/linux/netdevice.h | 10 +++- net/core/dev.c | 97 +++++++++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 20 deletions(-)
include/linux/netdevice.h +8 -2
@@ -925,6 +925,12 @@ struct dev_ifalias {struct devlink ;struct tlsdev_ops ; +struct netdev_name_node { +struct hlist_node hlist; +struct net_device *dev; +const char *name; +}; +/* * This structure defines the management hooks for network devices. * The following hooks can be defined; unless noted otherwise, they are
@@ -1564,7 +1570,7 @@ enum netdev_priv_flags {* (i.e. as seen by users in the "Space.c" file). It is the name * of the interface. * -* @name_hlist: Device name hash chain, please keep it close to name[] +* @name_node: Name hashlist node * @ifalias: SNMP alias * @mem_end: Shared memory end * @mem_start: Shared memory start
@@ -1774,7 +1780,7 @@ enum netdev_priv_flags {struct net_device {char name[IFNAMSIZ ]; -struct hlist_node name_hlist; +struct netdev_name_node *name_node;struct dev_ifalias __rcu *ifalias;/* * I/O specific fields
net/core/dev.c +79 -18
158 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH 3/7 net: rtnetlink: add linkprop commands to add and delete alternative ifnames
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Add two commands to add and delete list of link properties. Implement the first property type along - alternative ifnames.
Each net device can have multiple alternative names.
---rfc->v1:
- make netdev_name_node_alt_create/destroy() char *name arg const
- converted the patch to add/del list of link properties
--- include/linux/netdevice.h | 4 ++ include/uapi/linux/if.h | 1 + include/uapi/linux/if_link.h | 2 + include/uapi/linux/rtnetlink.h | 7 +++ net/core/dev.c | 58 ++++++++++++++++++- net/core/rtnetlink.c | 103 +++++++++++++++++++++++++++++++++ security/selinux/nlmsgtab.c | 4 +- 7 files changed, 177 insertions(+), 2 deletions(-)
include/linux/netdevice.h +4 -0
@@ -927,10 +927,14 @@ struct tlsdev_ops;struct netdev_name_node {struct hlist_node hlist; +struct list_head list;struct net_device *dev;const char *name; }; +int netdev_name_node_alt_create (struct net_device *dev,const char *name); +int netdev_name_node_alt_destroy (struct net_device *dev,const char *name); +/* * This structure defines the management hooks for network devices. * The following hooks can be defined; unless noted otherwise, they are
include/uapi/linux/if.h +1 -0
@@ -33,6 +33,7 @@#define IFNAMSIZ 16#endif /* __UAPI_DEF_IF_IFNAMSIZ */ #define IFALIASZ 256 +#define ALTIFNAMSIZ 128#include <linux/hdlc/ioctl.h> /* For glibc compatibility. An empty enum does not compile. */
include/uapi/linux/if_link.h +2 -0
@@ -167,6 +167,8 @@ enum {IFLA_NEW_IFINDEX ,IFLA_MIN_MTU ,IFLA_MAX_MTU , +IFLA_PROP_LIST , +IFLA_ALT_IFNAME ,/* Alternative ifname */ __IFLA_MAX };
include/uapi/linux/rtnetlink.h +7 -0
@@ -164,6 +164,13 @@ enum { RTM_GETNEXTHOP,#define RTM_GETNEXTHOP RTM_GETNEXTHOP + RTM_NEWLINKPROP = 108, +#define RTM_NEWLINKPROP RTM_NEWLINKPROP + RTM_DELLINKPROP, +#define RTM_DELLINKPROP RTM_DELLINKPROP + RTM_GETLINKPROP, +#define RTM_GETLINKPROP RTM_GETLINKPROP + __RTM_MAX,#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) };
net/core/dev.c +57 -1
76 lines not on this page · raw message
net/core/rtnetlink.c +103 -0
121 lines not on this page · raw message
security/selinux/nlmsgtab.c +3 -1
@@ -83,6 +83,8 @@ static const struct nlmsg_perm nlmsg_route_perms[] = { RTM_NEWNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_DELNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ }, + { RTM_NEWLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, + { RTM_DELLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, }; static const struct nlmsg_perm nlmsg_tcpdiag_perms[] =
@@ -166,7 +168,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)* structures at the top of this file with the new mappings * before updating the BUILD_BUG_ON() macro! */ -BUILD_BUG_ON (RTM_MAX != (RTM_NEWNEXTHOP +3 )); +BUILD_BUG_ON (RTM_MAX != (RTM_NEWLINKPROP +3 )); err =nlmsg_perm (nlmsg_type, perm, nlmsg_route_perms,sizeof (nlmsg_route_perms));break ;
Signature · 1 line
2.21.0
PATCH 4/7 net: rtnetlink: put alternative names to getlink message
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Extend exiting getlink info message with list of properties. Now the only ones are alternative names.
---rfc->v1:
- move size initialization after early return in rtnl_alt_ifname_list_size()/rtnl_prop_list_size()
- converted to generic property list
--- net/core/rtnetlink.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
net/core/rtnetlink.c +53 -0
@@ -980,6 +980,19 @@ static size_t rtnl_xdp_size(void)return xdp_size; } +static size_t rtnl_prop_list_size (const struct net_device *dev) +{ +struct netdev_name_node *name_node; +size_t size; + +if (list_empty (&dev->name_node->list)) +return 0 ; + size =nla_total_size (0 ); +list_for_each_entry (name_node, &dev->name_node->list, list) + size +=nla_total_size (ALTIFNAMSIZ ); +return size; +} + static noinline size_t if_nlmsg_size(const struct net_device *dev,u32 ext_filter_mask) {
@@ -1027,6 +1040,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev, +nla_total_size (4 )/* IFLA_CARRIER_DOWN_COUNT */ +nla_total_size (4 )/* IFLA_MIN_MTU */ +nla_total_size (4 )/* IFLA_MAX_MTU */ + +rtnl_prop_list_size (dev) +0 ; }
@@ -1584,6 +1598,42 @@ static int rtnl_fill_link_af(struct sk_buff *skb,return 0 ; } +static int rtnl_fill_alt_ifnames (struct sk_buff *skb, +const struct net_device *dev) +{ +struct netdev_name_node *name_node; +int count =0 ; + +list_for_each_entry (name_node, &dev->name_node->list, list) { +if (nla_put_string (skb,IFLA_ALT_IFNAME , name_node->name)) +return -EMSGSIZE ; + count++; + } +return count; +} + +static int rtnl_fill_prop_list (struct sk_buff *skb, +const struct net_device *dev) +{ +struct nlattr *prop_list; +int ret; + + prop_list =nla_nest_start (skb,IFLA_PROP_LIST ); +if (!prop_list) +return -EMSGSIZE ; + + ret =rtnl_fill_alt_ifnames (skb, dev); +if (ret <=0 ) + goto nest_cancel; + +nla_nest_end (skb, prop_list); +return 0 ; + +nest_cancel: +nla_nest_cancel (skb, prop_list); +return ret; +} + static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, struct net *src_net, int type, u32 pid, u32 seq, u32 change,
@@ -1697,6 +1747,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, goto nla_put_failure_rcu;rcu_read_unlock (); +if (rtnl_fill_prop_list (skb, dev)) + goto nla_put_failure; +nlmsg_end (skb, nlh);return 0 ;
Signature · 1 line
2.21.0
PATCH 5/7 net: rtnetlink: unify the code in __rtnl_newlink get dev with the rest
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
__rtnl_newlink() code flow is a bit different around tb[IFLA_IFNAME] processing comparing to the other places. Change that to be unified with the rest.
--- net/core/rtnetlink.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
net/core/rtnetlink.c +4 -6
@@ -3080,12 +3080,10 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index); -else { -if (ifname[0 ]) - dev =__dev_get_by_name (net, ifname); -else - dev =NULL ; - } +else if (tb[IFLA_IFNAME ]) + dev =__dev_get_by_name (net, ifname); +else + dev =NULL ;if (dev) { master_dev =netdev_master_upper_dev_get (dev);
Signature · 1 line
2.21.0
PATCH 6/7 net: rtnetlink: introduce helper to get net_device instance by ifname
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce helper function rtnl_get_dev() that gets net_device structure instance pointer according to passed ifname or ifname attribute.
--- net/core/rtnetlink.c | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-)
net/core/rtnetlink.c +25 -20
@@ -2778,6 +2778,23 @@ static int do_setlink(const struct sk_buff *skb,return err; } +static struct net_device *rtnl_dev_get (struct net *net, +struct nlattr *ifname_attr, +char *ifname) +{ +char buffer[IFNAMSIZ ]; + +if (!ifname) { + ifname = buffer; +if (ifname_attr) +nla_strlcpy (ifname, ifname_attr,IFNAMSIZ ); +else +return NULL ; + } + +return __dev_get_by_name (net, ifname); +} +static int rtnl_setlink (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) {
@@ -2807,7 +2824,7 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index);else if (tb[IFLA_IFNAME ]) - dev =__dev_get_by_name (net, ifname); + dev =rtnl_dev_get (net,NULL , ifname);else goto errout;
@@ -2880,7 +2897,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,struct net *tgt_net = net;struct net_device *dev =NULL ;struct ifinfomsg *ifm; -char ifname[IFNAMSIZ ];struct nlattr *tb[IFLA_MAX +1 ];int err;int netnsid =-1 ;
@@ -2894,9 +2910,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,if (err <0 )return err; -if (tb[IFLA_IFNAME ]) -nla_strlcpy (ifname, tb[IFLA_IFNAME ],IFNAMSIZ ); -if (tb[IFLA_TARGET_NETNSID ]) { netnsid =nla_get_s32 (tb[IFLA_TARGET_NETNSID ]); tgt_net =rtnl_get_net_ns_capable (NETLINK_CB (skb).sk, netnsid);
@@ -2909,7 +2922,7 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,if (ifm->ifi_index >0 ) dev =__dev_get_by_index (tgt_net, ifm->ifi_index);else if (tb[IFLA_IFNAME ]) - dev =__dev_get_by_name (tgt_net, ifname); + dev =rtnl_dev_get (net, tb[IFLA_IFNAME ],NULL );else if (tb[IFLA_GROUP ]) err =rtnl_group_dellink (tgt_net,nla_get_u32 (tb[IFLA_GROUP ]));else
@@ -3081,7 +3094,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index);else if (tb[IFLA_IFNAME ]) - dev =__dev_get_by_name (net, ifname); + dev =rtnl_dev_get (net,NULL , ifname);else dev =NULL ;
@@ -3363,7 +3376,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,struct net *net =sock_net (skb->sk);struct net *tgt_net = net;struct ifinfomsg *ifm; -char ifname[IFNAMSIZ ];struct nlattr *tb[IFLA_MAX +1 ];struct net_device *dev =NULL ;struct sk_buff *nskb;
@@ -3386,9 +3398,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,return PTR_ERR (tgt_net); } -if (tb[IFLA_IFNAME ]) -nla_strlcpy (ifname, tb[IFLA_IFNAME ],IFNAMSIZ ); -if (tb[IFLA_EXT_MASK ]) ext_filter_mask =nla_get_u32 (tb[IFLA_EXT_MASK ]);
@@ -3397,7 +3406,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,if (ifm->ifi_index >0 ) dev =__dev_get_by_index (tgt_net, ifm->ifi_index);else if (tb[IFLA_IFNAME ]) - dev =__dev_get_by_name (tgt_net, ifname); + dev =rtnl_dev_get (tgt_net, tb[IFLA_IFNAME ],NULL );else goto out;
@@ -3480,16 +3489,12 @@ static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,return err; ifm =nlmsg_data (nlh); -if (ifm->ifi_index >0 ) { +if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index); - }else if (tb[IFLA_IFNAME ]) { -char ifname[IFNAMSIZ ]; - -nla_strlcpy (ifname, tb[IFLA_IFNAME ],IFNAMSIZ ); - dev =__dev_get_by_name (net, ifname); - }else { +else if (tb[IFLA_IFNAME ]) + dev =rtnl_dev_get (net, tb[IFLA_IFNAME ],NULL ); +else return -EINVAL ; - }if (!dev)return -ENODEV ;
Signature · 1 line
2.21.0
PATCH 7/7 net: rtnetlink: add possibility to use alternative names as message handle
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Extend the basic rtnetlink commands to use alternative interface names as a handle instead of ifindex and ifname.
---rfc->v1:
- rebased on top of changes in the previous patches
--- net/core/rtnetlink.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)
net/core/rtnetlink.c +18 -11
@@ -2780,14 +2780,17 @@ static int do_setlink(const struct sk_buff *skb,static struct net_device *rtnl_dev_get (struct net *net,struct nlattr *ifname_attr, +struct nlattr *altifname_attr,char *ifname) { -char buffer[IFNAMSIZ ]; +char buffer[ALTIFNAMSIZ ];if (!ifname) { ifname = buffer;if (ifname_attr)nla_strlcpy (ifname, ifname_attr,IFNAMSIZ ); +else if (altifname_attr) +nla_strlcpy (ifname, altifname_attr,ALTIFNAMSIZ );else return NULL ; }
@@ -2823,8 +2826,8 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index); -else if (tb[IFLA_IFNAME ]) - dev =rtnl_dev_get (net,NULL , ifname); +else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) + dev =rtnl_dev_get (net,NULL , tb[IFLA_ALT_IFNAME ], ifname);else goto errout;
@@ -2921,8 +2924,9 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (tgt_net, ifm->ifi_index); -else if (tb[IFLA_IFNAME ]) - dev =rtnl_dev_get (net, tb[IFLA_IFNAME ],NULL ); +else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) + dev =rtnl_dev_get (net, tb[IFLA_IFNAME ], + tb[IFLA_ALT_IFNAME ],NULL );else if (tb[IFLA_GROUP ]) err =rtnl_group_dellink (tgt_net,nla_get_u32 (tb[IFLA_GROUP ]));else
@@ -3093,8 +3097,8 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index); -else if (tb[IFLA_IFNAME ]) - dev =rtnl_dev_get (net,NULL , ifname); +else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) + dev =rtnl_dev_get (net,NULL , tb[IFLA_ALT_IFNAME ], ifname);else dev =NULL ;
@@ -3358,6 +3362,7 @@ static int rtnl_valid_getlink_req(struct sk_buff *skb,switch (i) {case IFLA_IFNAME : +case IFLA_ALT_IFNAME :case IFLA_EXT_MASK :case IFLA_TARGET_NETNSID :break ;
@@ -3405,8 +3410,9 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (tgt_net, ifm->ifi_index); -else if (tb[IFLA_IFNAME ]) - dev =rtnl_dev_get (tgt_net, tb[IFLA_IFNAME ],NULL ); +else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) + dev =rtnl_dev_get (tgt_net, tb[IFLA_IFNAME ], + tb[IFLA_ALT_IFNAME ],NULL );else goto out;
@@ -3491,8 +3497,9 @@ static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) dev =__dev_get_by_index (net, ifm->ifi_index); -else if (tb[IFLA_IFNAME ]) - dev =rtnl_dev_get (net, tb[IFLA_IFNAME ],NULL ); +else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) + dev =rtnl_dev_get (net, tb[IFLA_IFNAME ], + tb[IFLA_ALT_IFNAME ],NULL );else return -EINVAL ;
Signature · 1 line
2.21.0
PATCH 1/2 ip: add support for alternative name addition/deletion/list
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Implement addition/deletion of lists of properties, currently alternative ifnames. Also extent the ip link show command to list them.
---rfc->v1:
- convert to generic property lists
- added patch description
- use ll_name_to_index() and ifindex as handle
- fixed parsing with nested flag on
- adjusted output
- added arg name to check_altifname header prototype
- added help and man parts
--- include/uapi/linux/if_link.h | 2 + include/uapi/linux/rtnetlink.h | 7 +++ include/utils.h | 1 + ip/ipaddress.c | 20 ++++++++- ip/iplink.c | 82 +++++++++++++++++++++++++++++++++- lib/utils.c | 19 +++++--- man/man8/ip-link.8.in | 11 +++++ 7 files changed, 134 insertions(+), 8 deletions(-)
include/uapi/linux/if_link.h +2 -0
@@ -167,6 +167,8 @@ enum {IFLA_NEW_IFINDEX ,IFLA_MIN_MTU ,IFLA_MAX_MTU , +IFLA_PROP_LIST , +IFLA_ALT_IFNAME ,/* Alternative ifname */ __IFLA_MAX };
include/uapi/linux/rtnetlink.h +7 -0
@@ -164,6 +164,13 @@ enum { RTM_GETNEXTHOP,#define RTM_GETNEXTHOP RTM_GETNEXTHOP + RTM_NEWLINKPROP = 108, +#define RTM_NEWLINKPROP RTM_NEWLINKPROP + RTM_DELLINKPROP, +#define RTM_DELLINKPROP RTM_DELLINKPROP + RTM_GETLINKPROP, +#define RTM_GETLINKPROP RTM_GETLINKPROP + __RTM_MAX,#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) };
include/utils.h +1 -0
@@ -196,6 +196,7 @@ void duparg(const char *, const char *) __attribute__((noreturn));void duparg2 (const char *,const char *) __attribute__((noreturn));int nodev (const char *dev);int check_ifname (const char *); +int check_altifname (const char *name);int get_ifname (char *,const char *);const char *get_ifname_rta (int ifindex,const struct rtattr *rta);bool matches (const char *prefix,const char *string);
ip/ipaddress.c +18 -2
@@ -879,7 +879,7 @@ int print_linkinfo(struct nlmsghdr *n, void *arg)if (filter.up && !(ifi->ifi_flags&IFF_UP ))return -1 ; -parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); +parse_rtattr_flags (tb,IFLA_MAX ,IFLA_RTA (ifi), len,NLA_F_NESTED ); name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]);if (!name)
@@ -1139,7 +1139,23 @@ int print_linkinfo(struct nlmsghdr *n, void *arg)close_json_array (PRINT_JSON ,NULL ); } -print_string (PRINT_FP ,NULL ,"%s" ,"\n" ); +if (tb[IFLA_PROP_LIST ]) { +struct rtattr *i, *proplist = tb[IFLA_PROP_LIST ]; +int rem =RTA_PAYLOAD (proplist); + +open_json_array (PRINT_JSON ,"altnames" ); +for (i =RTA_DATA (proplist);RTA_OK (i, rem); + i =RTA_NEXT (i, rem)) { +if (i->rta_type !=IFLA_ALT_IFNAME ) +continue ; +print_string (PRINT_FP ,"NULL" ,"%s altname " , _SL_); +print_string (PRINT_ANY ,NULL , +"%s" ,rta_getattr_str (i)); + } +close_json_array (PRINT_JSON ,NULL ); + } + +print_string (PRINT_FP ,NULL ,"%s" , _SL_);fflush (fp);return 1 ; }
ip/iplink.c +81 -1
100 lines not on this page · raw message
lib/utils.c +14 -5
@@ -824,14 +824,10 @@ int nodev(const char *dev)return -1 ; } -int check_ifname (const char *name) +static int __check_ifname (const char *name) { -/* These checks mimic kernel checks in dev_valid_name */ if (*name =='\0' )return -1 ; -if (strlen (name) >=IFNAMSIZ ) -return -1 ; -while (*name) {if (*name =='/' ||isspace (*name))return -1 ;
@@ -840,6 +836,19 @@ int check_ifname(const char *name)return 0 ; } +int check_ifname (const char *name) +{ +/* These checks mimic kernel checks in dev_valid_name */ +if (strlen (name) >=IFNAMSIZ ) +return -1 ; +return __check_ifname (name); +} + +int check_altifname (const char *name) +{ +return __check_ifname (name); +} +/* buf is assumed to be IFNAMSIZ */ int get_ifname (char *buf,const char *name) {
man/man8/ip-link.8.in +11 -0
@@ -244,6 +244,17 @@ ip-link \- network device configuration .IR VLAN-QOS " ] [" .B proto .IR VLAN-PROTO " ] ]" +.in -8 + +.ti -8 +.BI "ip link prop add" +.RB "[ " altname +.IR NAME " .. ]" + +.ti -8 +.BI "ip link prop del" +.RB "[ " altname +.IR NAME " .. ]" .SH "DESCRIPTION" .SS ip link add - add virtual link
Signature · 1 line
2.21.0
Re: [patch net-next 1/2] ip: add support for alternative name addition/deletion/list
In subject of this and the next one should be iproute2-next, of course.
Re: [patch net-next 1/2] ip: add support for alternative name addition/deletion/list
In reply to Jiri Pirkoip/ipaddress.c int print_linkinfo(struct nlmsghdr *n, void *arg) · line 1151
Quoted 6 lines · On Mon, 30 Sep 2019 11:59:02 +0200 Jiri Pirko <[email protected]> wrote:
ip/ipaddress.c +6
-0+open_json_array (PRINT_JSON ,"altnames" ); +for (i =RTA_DATA (proplist);RTA_OK (i, rem); + i =RTA_NEXT (i, rem)) { +if (i->rta_type !=IFLA_ALT_IFNAME ) +continue ; +print_string (PRINT_FP ,"NULL" ,"%s altname " , _SL_);
You can pass real NULL versus quoted NULL when doing print to file only
Re: [patch net-next 1/2] ip: add support for alternative name addition/deletion/list
Quoted 11 lines · Mon, Sep 30, 2019 at 06:27:18PM CEST, [email protected] wrote:
ip/ipaddress.c int print_linkinfo(struct nlmsghdr *n, void *arg) · line 1151
Quoted 6 lines · On Mon, 30 Sep 2019 11:59:02 +0200 Jiri Pirko <[email protected]> wrote:
ip/ipaddress.c +6
-0+open_json_array (PRINT_JSON ,"altnames" ); +for (i =RTA_DATA (proplist);RTA_OK (i, rem); + i =RTA_NEXT (i, rem)) { +if (i->rta_type !=IFLA_ALT_IFNAME ) +continue ; +print_string (PRINT_FP ,"NULL" ,"%s altname " , _SL_);You can pass real NULL versus quoted NULL when doing print to file only
Okay, will do.
PATCH 2/2 ip: allow to use alternative names as handle
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Extend ll_name_to_index() to get the index of a netdevice using alternative interface name. Allow alternative long names to pass checks in couple of ip link/addr commands.
---rfc->v1:
- added patch description
--- ip/iplink.c | 5 +++-- lib/ll_map.c | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-)
ip/iplink.c +3 -2
@@ -931,7 +931,7 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)NEXT_ARG ();if (dev != name)duparg2 ("dev" , *argv); -if (check_ifname (*argv)) +if (check_altifname (*argv))invarg ("\"dev\" not a valid ifname" , *argv); dev = *argv; }
@@ -1106,7 +1106,8 @@ int iplink_get(char *name, __u32 filt_mask)if (name) {addattr_l (&req.n,sizeof (req), -IFLA_IFNAME , name,strlen (name) +1 ); + !check_ifname (name) ?IFLA_IFNAME :IFLA_ALT_IFNAME , + name,strlen (name) +1 ); }addattr32 (&req.n,sizeof (req),IFLA_EXT_MASK , filt_mask);
lib/ll_map.c +40 -1
59 lines not on this page · raw message
Signature · 1 line
2.21.0
David Miller
maintainerFrom: Jiri Pirko <[email protected]>
Date: Mon, 30 Sep 2019 11:48:13 +0200
Quoted 5 lines
This patchset introduces alternative names for network interfaces. Main goal is to:
1) Overcome the IFNAMSIZ limitation (altname limitation is 128 bytes)
2) Allow to have multiple names at the same time (multiple udev patterns)
3) Allow to use alternative names as handle for commands
Ok, let's see where this goes.
Series applied, thanks Jiri.