ip: add support for alternative names
Subsystems (3)
v2 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/uapi/linux/if_link.h | 2 + include/uapi/linux/rtnetlink.h | 7 +++ include/utils.h | 1 + ip/ipaddress.c | 20 +++++++- ip/iplink.c | 87 ++++++++++++++++++++++++++++++++-- lib/ll_map.c | 41 +++++++++++++++- lib/utils.c | 19 ++++++-- man/man8/ip-link.8.in | 11 +++++ 8 files changed, 177 insertions(+), 11 deletions(-)
Signature · 1 line
2.21.0
PATCH v2 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.
---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/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 iproute2-next v2 1/2] ip: add support for alternative name addition/deletion/list
ip/iplink.c void iplink_usage(void) · line 116
Quoted 12 lines · Jiri Pirko
ip/iplink.c +3
-1@@ -111,7 +111,9 @@ void iplink_usage(void)"\n" " ip link xstats type TYPE [ ARGS ]\n" "\n" - " ip link afstats [ dev DEVICE ]\n"); +" ip link afstats [ dev DEVICE ]\n" +" ip link prop add dev DEVICE [ altname NAME .. ]\n" + " ip link prop del dev DEVICE [ altname NAME .. ]\n");
spell out 'property' here. The matches below on "prop" is fine, but the help can show the full name.
ip/iplink.c static int iplink_afstats(int argc, char **argv) · line 1686
Quoted 6 lines
ip/iplink.c +6
-0+ +if (matches (*argv,"add" ) ==0 ) { + req.n.nlmsg_flags |=NLM_F_EXCL |NLM_F_CREATE |NLM_F_APPEND ; + req.n.nlmsg_type =RTM_NEWLINKPROP ; + }else if (matches (*argv,"del" ) ==0 ) { + req.n.nlmsg_flags |=RTM_DELLINK ;
RTM_DELLINK is a command not a netlink flag.
Quoted 78 lines
ip/iplink.c +10
-0+ req.n.nlmsg_type =RTM_DELLINKPROP ; + }else if (matches (*argv,"help" ) ==0 ) { +usage (); + }else { +fprintf (stderr,"Operator required\n" ); +exit (-1 ); + } +return iplink_prop_mod (argc -1 , argv +1 , &req); +} +static void do_help (int argc,char **argv) {struct link_util *lu =NULL ;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
Re: [patch iproute2-next v2 1/2] ip: add support for alternative name addition/deletion/list
Quoted 16 lines · Mon, Oct 07, 2019 at 11:37:21PM CEST, [email protected] wrote:
ip/iplink.c void iplink_usage(void) · line 116
Quoted 12 lines · Jiri Pirko
ip/iplink.c +3
-1@@ -111,7 +111,9 @@ void iplink_usage(void)"\n" " ip link xstats type TYPE [ ARGS ]\n" "\n" - " ip link afstats [ dev DEVICE ]\n"); +" ip link afstats [ dev DEVICE ]\n" +" ip link prop add dev DEVICE [ altname NAME .. ]\n" + " ip link prop del dev DEVICE [ altname NAME .. ]\n");spell out 'property' here. The matches below on "prop" is fine, but the help can show the full name.
Okay.
Quoted 10 lines
ip/iplink.c static int iplink_afstats(int argc, char **argv) · line 1686
Quoted 8 lines
ip/iplink.c +6
-0+ +if (matches (*argv,"add" ) ==0 ) { + req.n.nlmsg_flags |=NLM_F_EXCL |NLM_F_CREATE |NLM_F_APPEND ; + req.n.nlmsg_type =RTM_NEWLINKPROP ; + }else if (matches (*argv,"del" ) ==0 ) { + req.n.nlmsg_flags |=RTM_DELLINK ;
RTM_DELLINKis a command not a netlink flag.
Will fix.
PATCH v2 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
Re: [patch iproute2-next v2 2/2] ip: allow to use alternative names as handle
ip/iplink.c int iplink_get(char *name, __u32 filt_mask) · line 1109
Quoted 6 lines · Jiri Pirko
ip/iplink.c +1
-1@@ -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,
If this trick works here ...
Quoted 33 lines
ip/iplink.c +1
-0+ name, strlen(name) + 1); }addattr32 (&req.n,sizeof (req),IFLA_EXT_MASK , filt_mask);lib/ll_map.c +13
-1@@ -70,7 +70,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; }@@ -240,6 +240,43 @@ int ll_index_to_flags(unsigned idx)return im ? im->flags :-1 ; } +static int altnametoindex (const char *name) +{ +struct { +struct nlmsghdr n; +struct ifinfomsg ifm; +char buf[1024 ]; + } req = { + .n.nlmsg_len =NLMSG_LENGTH (sizeof (struct ifinfomsg )), + .n.nlmsg_flags =NLM_F_REQUEST , + .n.nlmsg_type =RTM_GETLINK , + }; +struct rtnl_handle rth = {};
lib/ll_map.c int ll_index_to_flags(unsigned idx) · line 264
Quoted 10 lines
lib/ll_map.c +10
-0+struct nlmsghdr *answer; +struct ifinfomsg *ifm; +int rc =0 ; + +if (rtnl_open (&rth,0 ) <0 ) +return 0 ; + +addattr32 (&req.n,sizeof (req),IFLA_EXT_MASK , +RTEXT_FILTER_VF |RTEXT_FILTER_SKIP_STATS ); +addattr_l (&req.n,sizeof (req),IFLA_ALT_IFNAME , name,strlen (name) +1 );
then why is altnametoindex even needed? why not just use the same check in the current ll_link_get?
Quoted 19 lines
lib/ll_map.c +15
-0+ +if (rtnl_talk_suppress_rtnl_errmsg (&rth, &req.n, &answer) <0 ) + goto out; + + ifm =NLMSG_DATA (answer); + rc = ifm->ifi_index; + +free (answer); + +rtnl_close (&rth); +out: +return rc; +} + +unsigned ll_name_to_index (const char *name) {const struct ll_cache *im;
lib/ll_map.c unsigned ll_name_to_index(const char *name) · line 298
Quoted 5 lines
lib/ll_map.c +2
-0@@ -257,6 +294,8 @@ unsigned ll_name_to_index(const char *name) idx =if_nametoindex (name);if (idx ==0 ) idx =ll_idx_a2n (name); +if (idx ==0 ) + idx =altnametoindex (name);
And then this ordering does not need to be fixed (altname check should come before if_nametoindex.
Quoted 4 lines
lib/ll_map.c +0
-0return idx; }
Re: [patch iproute2-next v2 2/2] ip: allow to use alternative names as handle
Quoted 48 lines · Mon, Oct 07, 2019 at 11:52:46PM CEST, [email protected] wrote:
ip/iplink.c int iplink_get(char *name, __u32 filt_mask) · line 1109
Quoted 6 lines · Jiri Pirko
ip/iplink.c +1
-1@@ -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,If this trick works here ...
Quoted 38 lines
ip/iplink.c +1
-0+ name, strlen(name) + 1); }addattr32 (&req.n,sizeof (req),IFLA_EXT_MASK , filt_mask);lib/ll_map.c +18
-1@@ -70,7 +70,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; }@@ -240,6 +240,43 @@ int ll_index_to_flags(unsigned idx)return im ? im->flags :-1 ; } +static int altnametoindex (const char *name) +{ +struct { +struct nlmsghdr n; +struct ifinfomsg ifm; +char buf[1024 ]; + } req = { + .n.nlmsg_len =NLMSG_LENGTH (sizeof (struct ifinfomsg )), + .n.nlmsg_flags =NLM_F_REQUEST , + .n.nlmsg_type =RTM_GETLINK , + }; +struct rtnl_handle rth = {}; +struct nlmsghdr *answer; +struct ifinfomsg *ifm; +int rc =0 ; + +if (rtnl_open (&rth,0 ) <0 )
Quoted 8 lines
lib/ll_map.c int ll_index_to_flags(unsigned idx) · line 264
Quoted 5 lines
lib/ll_map.c +5
-0+return 0 ; + +addattr32 (&req.n,sizeof (req),IFLA_EXT_MASK , +RTEXT_FILTER_VF |RTEXT_FILTER_SKIP_STATS ); +addattr_l (&req.n,sizeof (req),IFLA_ALT_IFNAME , name,strlen (name) +1 );then why is altnametoindex even needed? why not just use the same check in the current
ll_link_get?
Good point. Reworked. Thanks!
Quoted 34 lines
Quoted 20 lines
lib/ll_map.c +15
-0+ +if (rtnl_talk_suppress_rtnl_errmsg (&rth, &req.n, &answer) <0 ) + goto out; + + ifm =NLMSG_DATA (answer); + rc = ifm->ifi_index; + +free (answer); + +rtnl_close (&rth); +out: +return rc; +} + +unsigned ll_name_to_index (const char *name) {const struct ll_cache *im;lib/ll_map.c unsigned ll_name_to_index(const char *name) · line 298
Quoted 5 lines
lib/ll_map.c +2
-0@@ -257,6 +294,8 @@ unsigned ll_name_to_index(const char *name) idx =if_nametoindex (name);if (idx ==0 ) idx =ll_idx_a2n (name); +if (idx ==0 ) + idx =altnametoindex (name);And then this ordering does not need to be fixed (altname check should come before
if_nametoindex.Quoted 4 lines
lib/ll_map.c +0
-0return idx; }