Skip to contentSkip to conversation

ip: add support for alternative names

PATCH Superseded Jiri Pirko 2 participants 7 messages Oct 9, 2019 – Oct 17, 2019

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jiri Pirko

author
#0 lore raw

v3 cover ip: add support for alternative names

From: Jiri Pirko <[email protected]>

This patchset adds support for alternative names manipulation and usage.

Jiri Pirko (2):
ip: add support for alternative name addition/deletion/list
ip: allow to use alternative names as handle

 include/utils.h       |  1 +
 ip/ipaddress.c        | 20 +++++++++-
 ip/iplink.c           | 86 +++++++++++++++++++++++++++++++++++++++++--
 lib/ll_map.c          |  8 ++--
 lib/utils.c           | 19 +++++++---
 man/man8/ip-link.8.in | 11 ++++++
 6 files changed, 132 insertions(+), 13 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH v3 1/2 ip: add support for alternative name addition/deletion/list

From: Jiri Pirko <[email protected]>

Implement addition/deletion of lists of properties, currently alternative ifnames. Also extent the ip link show command to list them.

---
v2->v3:
- s/prop/property/ on cmdline
- removed RTM_DELLINK flag set
v1->v2:
- s/\"NULL\"/NULL/ in print_string arg 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/utils.h       |  1 +
 ip/ipaddress.c        | 20 +++++++++--
 ip/iplink.c           | 81 ++++++++++++++++++++++++++++++++++++++++++-
 lib/utils.c           | 19 +++++++---
 man/man8/ip-link.8.in | 11 ++++++
 5 files changed, 124 insertions(+), 8 deletions(-)
include/utils.h +1 -0
index 794d36053634..001491a1eb40 100644
@@ -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
index bc8f5ba13c33..b72eb7a1f808 100644
@@ -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 +80 -1

99 lines not on this page · raw message

lib/utils.c +14 -5
index 95d46ff210aa..bbb3bdcfa80b 100644
@@ -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
index a8ae72d2b097..f800a18a45fc 100644
@@ -244,6 +244,17 @@ ip-link \- network device configuration
 .IR VLAN-QOS " ] ["
 .B proto
 .IR VLAN-PROTO " ] ]"
+.in -8
+
+.ti -8
+.BI "ip link property add"
+.RB "[ " altname
+.IR NAME " .. ]"
+
+.ti -8
+.BI "ip link property del"
+.RB "[ " altname
+.IR NAME " .. ]"
 
 .SH "DESCRIPTION"
 .SS ip link add - add virtual link
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v3 2/2 ip: allow to use alternative names as handle

In reply to Jiri Pirko

From: 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.

---
v2->v3:
- removed altnametoindex and doing IFLA_IFNAME/IFLA_ALT_IFNAME in ll_link_get() instead.
rfc->v1:
- added patch description

---
 ip/iplink.c  | 5 +++--
 lib/ll_map.c | 8 +++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
ip/iplink.c +3 -2
index bf90fad1b3ea..47f73988c2d5 100644
@@ -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 +5 -3
index e0ed54bf77c9..5c612f952be4 100644
@@ -22,6 +22,7 @@
 #include "libnetlink.h"
 #include "ll_map.h"
 #include "list.h"
+#include "utils.h"
 
 struct ll_cache {
 	struct hlist_node idx_hash;
@@ -70,7 +71,7 @@ static struct ll_cache *ll_get_by_name(const char *name)
 		struct ll_cache *im
 			= container_of(n, struct ll_cache, name_hash);
 
-		if (strncmp(im->name, name, IFNAMSIZ) == 0)
+		if (strcmp(im->name, name) == 0)
 			return im;
 	}
 
@@ -174,8 +175,9 @@ static int ll_link_get(const char *name, int index)
 
 	addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask);
 	if (name)
-		addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name,
-			  strlen(name) + 1);
+		addattr_l(&req.n, sizeof(req),
+			  !check_ifname(name) ? IFLA_IFNAME : IFLA_ALT_IFNAME,
+			  name, strlen(name) + 1);
 
 	if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0)
 		goto out;
Signature · 1 line
2.21.0
#3 lore raw

Re: [patch iproute2-next v3 2/2] ip: allow to use alternative names as handle

Quoted 5 lines · Jiri Pirko

From: 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.

you don't add altnames to the name_hash, so the lookup can not find a match based on altname.

author
#4 lore raw

Re: [patch iproute2-next v3 2/2] ip: allow to use alternative names as handle

Quoted 9 lines · Tue, Oct 15, 2019 at 08:34:56PM CEST, [email protected] wrote:
Quoted 5 lines · Jiri Pirko

From: 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.

you don't add altnames to the name_hash, so the lookup can not find a match based on altname.

you are right, it is always going to fall back to ll_link_get(). I will do another patch to add the altnames to name_hash. It can go in separatelly or I can add that to this patchset. Up to you.

Quoted 1 line
#5 lore raw

Re: [patch iproute2-next v3 2/2] ip: allow to use alternative names as handle

Quoted 8 lines · Jiri Pirko
Quoted 7 lines · Tue, Oct 15, 2019 at 08:34:56PM CEST, [email protected] wrote:
Quoted 5 lines · Jiri Pirko

From: 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.

Quoted 6 lines
Quoted 2 lines

you don't add altnames to the name_hash, so the lookup can not find a match based on altname.

you are right, it is always going to fall back to ll_link_get(). I will do another patch to add the altnames to name_hash. It can go in separatelly or I can add that to this patchset. Up to you.

this patch claims to do the lookup, so it should actually be able to do a lookup.

author
#6 lore raw

Re: [patch iproute2-next v3 2/2] ip: allow to use alternative names as handle

Quoted 12 lines · Thu, Oct 17, 2019 at 05:34:54PM CEST, [email protected] wrote:
Quoted 11 lines · Jiri Pirko
Quoted 9 lines · Tue, Oct 15, 2019 at 08:34:56PM CEST, [email protected] wrote:
Quoted 5 lines · Jiri Pirko

From: 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.

you don't add altnames to the name_hash, so the lookup can not find a match based on altname.

Quoted 6 lines
Quoted 3 lines

you are right, it is always going to fall back to ll_link_get(). I will do another patch to add the altnames to name_hash. It can go in separatelly or I can add that to this patchset. Up to you.

this patch claims to do the lookup, so it should actually be able to do a lookup.

Allright.