diff mbox series

[net-next,rfc,4/7] net: rtnetlink: put alternative names to getlink message

Message ID 20190719110029.29466-5-jiri@resnulli.us
State RFC
Delegated to: David Miller
Headers show
Series net: introduce alternative names for network interfaces | expand

Commit Message

Jiri Pirko July 19, 2019, 11 a.m. UTC
From: Jiri Pirko <jiri@mellanox.com>

Extend exiting getlink info message with list of alternative names.

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
---
 include/uapi/linux/if_link.h |  2 ++
 net/core/rtnetlink.c         | 41 ++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

Comments

Jakub Kicinski July 20, 2019, 3:59 a.m. UTC | #1
On Fri, 19 Jul 2019 13:00:26 +0200, Jiri Pirko wrote:
> diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
> index 7a2010b16e10..f11a2367037d 100644
> --- a/net/core/rtnetlink.c
> +++ b/net/core/rtnetlink.c
> @@ -980,6 +980,18 @@ static size_t rtnl_xdp_size(void)
>  	return xdp_size;
>  }
>  
> +static size_t rtnl_alt_ifname_list_size(const struct net_device *dev)
> +{
> +	struct netdev_name_node *name_node;
> +	size_t size = nla_total_size(0);
> +
> +	if (list_empty(&dev->name_node->list))
> +		return 0;

Nit: it would make the intent a tiny bit clearer if 

	size = nla_total_size(0);

was after this early return.

> +	list_for_each_entry(name_node, &dev->name_node->list, list)
> +		size += nla_total_size(ALTIFNAMSIZ);

Since we have the structure I wonder if it would be worthwhile to store
the exact size in it?

> +	return size;
> +}
> +
Jiri Pirko July 20, 2019, 7:17 a.m. UTC | #2
Sat, Jul 20, 2019 at 05:59:14AM CEST, jakub.kicinski@netronome.com wrote:
>On Fri, 19 Jul 2019 13:00:26 +0200, Jiri Pirko wrote:
>> diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
>> index 7a2010b16e10..f11a2367037d 100644
>> --- a/net/core/rtnetlink.c
>> +++ b/net/core/rtnetlink.c
>> @@ -980,6 +980,18 @@ static size_t rtnl_xdp_size(void)
>>  	return xdp_size;
>>  }
>>  
>> +static size_t rtnl_alt_ifname_list_size(const struct net_device *dev)
>> +{
>> +	struct netdev_name_node *name_node;
>> +	size_t size = nla_total_size(0);
>> +
>> +	if (list_empty(&dev->name_node->list))
>> +		return 0;
>
>Nit: it would make the intent a tiny bit clearer if 
>
>	size = nla_total_size(0);
>
>was after this early return.

Sure.


>
>> +	list_for_each_entry(name_node, &dev->name_node->list, list)
>> +		size += nla_total_size(ALTIFNAMSIZ);
>
>Since we have the structure I wonder if it would be worthwhile to store

Which structure?


>the exact size in it?
>
>> +	return size;
>> +}
>> +
diff mbox series

Patch

diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h
index 92268946e04a..038361f9847b 100644
--- a/include/uapi/linux/if_link.h
+++ b/include/uapi/linux/if_link.h
@@ -168,6 +168,8 @@  enum {
 	IFLA_MIN_MTU,
 	IFLA_MAX_MTU,
 	IFLA_ALT_IFNAME_MOD, /* Alternative ifname to add/delete */
+	IFLA_ALT_IFNAME_LIST, /* nest */
+	IFLA_ALT_IFNAME, /* Alternative ifname */
 	__IFLA_MAX
 };
 
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 7a2010b16e10..f11a2367037d 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -980,6 +980,18 @@  static size_t rtnl_xdp_size(void)
 	return xdp_size;
 }
 
+static size_t rtnl_alt_ifname_list_size(const struct net_device *dev)
+{
+	struct netdev_name_node *name_node;
+	size_t size = nla_total_size(0);
+
+	if (list_empty(&dev->name_node->list))
+		return 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 +1039,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_alt_ifname_list_size(dev)
 	       + 0;
 }
 
@@ -1584,6 +1597,31 @@  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;
+	struct nlattr *alt_ifname_list;
+
+	if (list_empty(&dev->name_node->list))
+		return 0;
+
+	alt_ifname_list = nla_nest_start_noflag(skb, IFLA_ALT_IFNAME_LIST);
+	if (!alt_ifname_list)
+		return -EMSGSIZE;
+
+	list_for_each_entry(name_node, &dev->name_node->list, list)
+		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
+			goto nla_put_failure;
+
+	nla_nest_end(skb, alt_ifname_list);
+	return 0;
+
+nla_put_failure:
+	nla_nest_cancel(skb, alt_ifname_list);
+	return -EMSGSIZE;
+}
+
 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 +1735,9 @@  static int rtnl_fill_ifinfo(struct sk_buff *skb,
 		goto nla_put_failure_rcu;
 	rcu_read_unlock();
 
+	if (rtnl_fill_alt_ifnames(skb, dev))
+		goto nla_put_failure;
+
 	nlmsg_end(skb, nlh);
 	return 0;