ipaddress: Make print_linkinfo_brief() static
Subsystems (3)
v4 cover ipaddress: Make print_linkinfo_brief() static
With this series I propose to make print_linkinfo_brief() static in favor of print_linkinfo() as single point for linkinfo printing.
Changes presented with this series tested using following script:
\#!/bin/bash
iproute2_dir="$1"
iface='eth0.2'
pushd "$iproute2_dir" &>/dev/null
for i in new old; do
DIR="/tmp/$i"
mkdir -p "$DIR"
ln -snf ip.$i ip/ip
# normal
ip/ip link show >"$DIR/ip-link-show"
ip/ip -4 addr show >"$DIR/ip-4-addr-show"
ip/ip -6 addr show >"$DIR/ip-6-addr-show"
ip/ip addr show dev "$iface" >"$DIR/ip-addr-show-$iface"
# brief
ip/ip -br link show >"$DIR/ip-br-link-show"
ip/ip -br -4 addr show >"$DIR/ip-br-4-addr-show"
ip/ip -br -6 addr show >"$DIR/ip-br-6-addr-show"
ip/ip -br addr show dev "$iface" >"$DIR/ip-br-addr-show-$iface"
done
rm -f ip/ip
diff -urN /tmp/{old,new} |sed -n -Ee'/^(-{3}|\+{3})[[:space:]]+/!p' rc=$?
popd &>/dev/null
exit $rc
Expected results : <no output>
Actual results : <no output>
Although test coverage is far from ideal in my opinion it covers most important aspects of the changes presented by the series.
All this work is done in prepare of iplink_get() enhancements to support attribute parse that finally will be used to simplify ip/tunnel RTM_GETLINK code.
As always reviews, comments, suggestions and criticism is welcome.
v4
Print master network device name using correct color.
v3
Fixed subject line.
v2
Rebased to current iproute2-next/master. No changes.
Thanks,
Serhii
Serhey Popovych (9):
ipaddress: Abstract IFA_LABEL matching code
ipaddress: ll_map: Replace ll_idx_n2a() with ll_index_to_name()
utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n()
ipaddress: Improve print_linkinfo()
ipaddress: Simplify print_linkinfo_brief() and it's usage
lib: Correct object file dependencies
utils: Introduce and use get_ifname_rta()
utils: Introduce and use print_name_and_link() to print name@link
ipaddress: Make print_linkinfo_brief() static
bridge/link.c | 21 ++--- include/ll_map.h | 4 +- include/utils.h | 5 ++ ip/ip_common.h | 2 - ip/ipaddress.c | 231 ++++++++++++++---------------------------------------- ip/iplink.c | 5 +- lib/Makefile | 4 +- lib/ll_map.c | 31 +++++--- lib/utils.c | 68 ++++++++++++++++ 9 files changed, 166 insertions(+), 205 deletions(-)
Signature · 1 line
1.7.10.4
PATCH v4 1/9 ipaddress: Abstract IFA_LABEL matching code
There at least two places in ip/ipaddress.c where we match IFA_LABEL against filter.label if that is given.
Get rid of "common" if () statement for inet_addr_match_rta() and ifa_label_match_rta(): it is not common because first will check for filter.pfx.family != AF_UNSPEC inside and second for filter.label being non NULL.
This allows us to further simplify down code and prepare for ll_idx_n2a() replacement with ll_index_to_name() without 80 columns checkpatch notice.
--- ip/ipaddress.c | 57 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-)
ip/ipaddress.c +27 -30
@@ -1305,6 +1305,22 @@ static int get_filter(const char *arg)return 0 ; } +static int ifa_label_match_rta (int ifindex,const struct rtattr *rta) +{ +const char *label; +SPRINT_BUF (b1); + +if (!filter.label) +return 0 ; + +if (rta) + label =RTA_DATA (rta); +else + label =ll_idx_n2a (ifindex, b1); + +return fnmatch (filter.label, label,0 ); +} +int print_addrinfo (const struct sockaddr_nl *who,struct nlmsghdr *n,void *arg) {
@@ -1343,21 +1359,13 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,return 0 ;if ((filter.flags ^ ifa_flags) & filter.flagmask)return 0 ; -if (filter.label) { -SPRINT_BUF (b1); -const char *label; - -if (rta_tb[IFA_LABEL ]) - label =RTA_DATA (rta_tb[IFA_LABEL ]); -else - label =ll_idx_n2a (ifa->ifa_index, b1); -if (fnmatch (filter.label, label,0 ) !=0 ) -return 0 ; - }if (filter.family && filter.family != ifa->ifa_family)return 0 ; +if (ifa_label_match_rta (ifa->ifa_index, rta_tb[IFA_LABEL ])) +return 0 ; +if (inet_addr_match_rta (&filter.pfx, rta_tb[IFA_LOCAL ]))return 0 ;
@@ -1713,25 +1721,14 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)if ((filter.flags ^ ifa_flags) & filter.flagmask)continue ; -if (filter.pfx.family || filter.label) { -struct rtattr *rta = - tb[IFA_LOCAL ] ? : tb[IFA_ADDRESS ]; - -if (inet_addr_match_rta (&filter.pfx, rta)) -continue ; - -if (filter.label) { -SPRINT_BUF (b1); -const char *label; - -if (tb[IFA_LABEL ]) - label =RTA_DATA (tb[IFA_LABEL ]); -else - label =ll_idx_n2a (ifa->ifa_index, b1); -if (fnmatch (filter.label, label,0 ) !=0 ) -continue ; - } - } + +if (ifa_label_match_rta (ifa->ifa_index, tb[IFA_LABEL ])) +continue ; + +if (!tb[IFA_LOCAL ]) + tb[IFA_LOCAL ] = tb[IFA_ADDRESS ]; +if (inet_addr_match_rta (&filter.pfx, tb[IFA_LOCAL ])) +continue ; ok =1 ;break ;
Signature · 1 line
1.7.10.4
PATCH v4 2/9 ipaddress: ll_map: Replace ll_idx_n2a() with ll_index_to_name()
In reply to Serhey PopovychThere is no reentrancy as well as deferred result usage for all cases where ll_idx_n2a() being used: it is safe to use ll_index_to_name() that internally calls ll_idx_n2a() with static buffer to hold result.
While there print master network device name using correct color.
--- ip/ipaddress.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
ip/ipaddress.c +9 -12
@@ -813,14 +813,13 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,print_bool (PRINT_ANY ,"deleted" ,"Deleted " , true);if (tb[IFLA_LINK ]) { -SPRINT_BUF (b1);int iflink =rta_getattr_u32 (tb[IFLA_LINK ]);if (iflink ==0 ) {snprintf (buf,sizeof (buf),"%s@NONE" , name);print_null (PRINT_JSON ,"link" ,NULL ,NULL ); }else { -const char *link =ll_idx_n2a (iflink, b1); +const char *link =ll_index_to_name (iflink);print_string (PRINT_JSON ,"link" ,NULL , link);snprintf (buf,sizeof (buf),"%s@%s" , name, link);
@@ -957,12 +956,10 @@ int print_linkinfo(const struct sockaddr_nl *who,print_int (PRINT_ANY ,"link_index" ,"@if%d: " , iflink); else { -SPRINT_BUF (b1); -print_string (PRINT_ANY ,"link" ,"@%s: " , -ll_idx_n2a (iflink, b1)); +ll_index_to_name (iflink)); m_flag =ll_index_to_flags (iflink); m_flag = !(m_flag &IFF_UP ); }
@@ -984,12 +981,13 @@ int print_linkinfo(const struct sockaddr_nl *who,"qdisc %s " , rta_getattr_str(tb[IFLA_QDISC]));if (tb[IFLA_MASTER ]) { -SPRINT_BUF (b1); +int master =rta_getattr_u32 (tb[IFLA_MASTER ]); -print_string (PRINT_ANY , -"master" , -"master %s " , -ll_idx_n2a (rta_getattr_u32 (tb[IFLA_MASTER ]), b1)); +print_color_string (PRINT_ANY , +COLOR_IFNAME , +"master" , +"master %s " , +ll_index_to_name (master)); }if (tb[IFLA_OPERSTATE ])
@@ -1308,7 +1306,6 @@ static int get_filter(const char *arg)static int ifa_label_match_rta (int ifindex,const struct rtattr *rta) {const char *label; -SPRINT_BUF (b1);if (!filter.label)return 0 ;
@@ -1316,7 +1313,7 @@ static int ifa_label_match_rta(int ifindex, const struct rtattr *rta)if (rta) label =RTA_DATA (rta);else - label =ll_idx_n2a (ifindex, b1); + label =ll_index_to_name (ifindex);return fnmatch (filter.label, label,0 ); }
Signature · 1 line
1.7.10.4
PATCH v4 3/9 utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n()
In reply to Serhey PopovychNow all users of ll_idx_n2a() replaced with ll_index_to_name() we can move it's functionality to ll_index_to_name() and implement index to name conversion using snprintf() and "if%u".
Use %u specifier in "if%..." template consistently: network device indexes are always greather than zero.
Also introduce ll_idx_n2a() conterpart: ll_idx_a2n() that is used to translate name of the "if%u" form to index using sscanf().
--- include/ll_map.h | 4 +++- lib/ll_map.c | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-)
include/ll_map.h +3 -1
@@ -8,9 +8,11 @@ int ll_remember_index(const struct sockaddr_nl *who,void ll_init_map (struct rtnl_handle *rth);unsigned ll_name_to_index (const char *name);const char *ll_index_to_name (unsigned idx); -const char *ll_idx_n2a (unsigned idx,char *buf);int ll_index_to_type (unsigned idx);int ll_index_to_flags (unsigned idx);unsigned namehash (const char *str); +const char *ll_idx_n2a (unsigned int idx); +unsigned int ll_idx_a2n (const char *name); +#endif /* __LL_MAP_H__ */
lib/ll_map.c +21 -10
@@ -136,8 +136,26 @@ int ll_remember_index(const struct sockaddr_nl *who,return 0 ; } -const char *ll_idx_n2a (unsigned idx,char *buf) +const char *ll_idx_n2a (unsigned int idx) { +static char buf[IFNAMSIZ ]; + +snprintf (buf,sizeof (buf),"if%u" , idx); +return buf; +} + +unsigned int ll_idx_a2n (const char *name) +{ +unsigned int idx; + +if (sscanf (name,"if%u" , &idx) !=1 ) +return 0 ; +return idx; +} + +const char *ll_index_to_name (unsigned int idx) +{ +static char buf[IFNAMSIZ ];const struct ll_cache *im;if (idx ==0 )
@@ -148,18 +166,11 @@ const char *ll_idx_n2a(unsigned idx, char *buf)return im->name;if (if_indextoname (idx, buf) ==NULL ) -snprintf (buf,IFNAMSIZ ,"if%d" , idx); +snprintf (buf,IFNAMSIZ ,"if%u" , idx);return buf; } -const char *ll_index_to_name (unsigned idx) -{ -static char nbuf[IFNAMSIZ ]; - -return ll_idx_n2a (idx, nbuf); -} -int ll_index_to_type (unsigned idx) {const struct ll_cache *im;
@@ -196,7 +207,7 @@ unsigned ll_name_to_index(const char *name) idx =if_nametoindex (name);if (idx ==0 ) -sscanf (name,"if%u" , &idx); + idx =ll_idx_a2n (name);return idx; }
Signature · 1 line
1.7.10.4
PATCH v4 4/9 ipaddress: Improve print_linkinfo()
In reply to Serhey PopovychThere are few places to improve:
1) return -1 when entry is filtered instead of zero, which means accept entry: ipaddress_list_flush_or_save() the only user of this
2) use ll_idx_n2a() as last resort to translate name to index for "should never happen" cases when cache shouldn't be considered
3) replace open coded access to IFLA_IFNAME attribute data by RTA_DATA() with rta_getattr_str()
4) simplify ifname printing since name is never NULL, thanks to (2).
--- ip/ipaddress.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
ip/ipaddress.c +13 -17
@@ -778,14 +778,14 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len);if (tb[IFLA_IFNAME ] ==NULL ) {fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name ="<nil>" ; + name =ll_idx_n2a (ifi->ifi_index); }else { name =rta_getattr_str (tb[IFLA_IFNAME ]); }if (filter.label && (!filter.family || filter.family ==AF_PACKET ) && -fnmatch (filter.label,RTA_DATA (tb[IFLA_IFNAME ]),0 )) +fnmatch (filter.label, name,0 ))return -1 ;if (tb[IFLA_GROUP ]) {
@@ -887,6 +887,7 @@ int print_linkinfo(const struct sockaddr_nl *who,struct ifinfomsg *ifi =NLMSG_DATA (n);struct rtattr *tb[IFLA_MAX +1 ];int len = n->nlmsg_len; +const char *name;unsigned int m_flag =0 ;if (n->nlmsg_type !=RTM_NEWLINK && n->nlmsg_type !=RTM_DELLINK )
@@ -897,18 +898,22 @@ int print_linkinfo(const struct sockaddr_nl *who,return -1 ;if (filter.ifindex && ifi->ifi_index != filter.ifindex) -return 0 ; +return -1 ;if (filter.up && !(ifi->ifi_flags&IFF_UP )) -return 0 ; +return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) +if (tb[IFLA_IFNAME ] ==NULL ) {fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); + name =ll_idx_n2a (ifi->ifi_index); + }else { + name =rta_getattr_str (tb[IFLA_IFNAME ]); + }if (filter.label && (!filter.family || filter.family ==AF_PACKET ) && -fnmatch (filter.label,RTA_DATA (tb[IFLA_IFNAME ]),0 )) -return 0 ; +fnmatch (filter.label, name,0 )) +return -1 ;if (tb[IFLA_GROUP ]) {int group =rta_getattr_u32 (tb[IFLA_GROUP ]);
@@ -935,16 +940,7 @@ int print_linkinfo(const struct sockaddr_nl *who,print_bool (PRINT_ANY ,"deleted" ,"Deleted " , true);print_int (PRINT_ANY ,"ifindex" ,"%d: " , ifi->ifi_index); -if (tb[IFLA_IFNAME ]) { -print_color_string (PRINT_ANY , -COLOR_IFNAME , -"ifname" ,"%s" , -rta_getattr_str (tb[IFLA_IFNAME ])); - }else { -print_null (PRINT_JSON ,"ifname" ,NULL ,NULL ); -print_color_null (PRINT_FP ,COLOR_IFNAME , -"ifname" ,"%s" ,"<nil>" ); - } +print_color_string (PRINT_ANY ,COLOR_IFNAME ,"ifname" ,"%s" , name);if (tb[IFLA_LINK ]) {int iflink =rta_getattr_u32 (tb[IFLA_LINK ]);
Signature · 1 line
1.7.10.4
PATCH v4 5/9 ipaddress: Simplify print_linkinfo_brief() and it's usage
In reply to Serhey PopovychSimplify calling code in ipaddr_list_flush_or_save() by introducing intermediate variable of @struct nlmsghdr, drop duplicated code:print_linkinfo_brief() never returns values other than <= 0 so we can move print_selected_addrinfo() outside of each block.
--- ip/ipaddress.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
ip/ipaddress.c +14 -17
@@ -753,7 +753,7 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n) }int print_linkinfo_brief (const struct sockaddr_nl *who, -struct nlmsghdr *n,void *arg) +struct nlmsghdr *n,void *arg) {FILE *fp = (FILE *)arg;struct ifinfomsg *ifi =NLMSG_DATA (n);
@@ -2013,24 +2013,21 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)ipaddr_filter (&linfo, ainfo);for (l = linfo.head; l; l = l->next) { -int res =0 ; -struct ifinfomsg *ifi =NLMSG_DATA (&l->h); +struct nlmsghdr *n = &l->h; +struct ifinfomsg *ifi =NLMSG_DATA (n); +int res;open_json_object (NULL ); -if (brief) { -if (print_linkinfo_brief (NULL , &l->h, stdout) ==0 ) -if (filter.family !=AF_PACKET ) -print_selected_addrinfo (ifi, - ainfo->head, - stdout); - }else if (no_link || - (res =print_linkinfo (NULL , &l->h, stdout)) >=0 ) { -if (filter.family !=AF_PACKET ) -print_selected_addrinfo (ifi, - ainfo->head, stdout); -if (res >0 && !do_link && show_stats) -print_link_stats (stdout, &l->h); - } +if (brief) + res =print_linkinfo_brief (NULL , n, stdout); +else if (no_link) + res =0 ; +else + res =print_linkinfo (NULL , n, stdout); +if (res >=0 && filter.family !=AF_PACKET ) +print_selected_addrinfo (ifi, ainfo->head, stdout); +if (res >0 && !do_link && show_stats) +print_link_stats (stdout, n);close_json_object (); }fflush (stdout);
Signature · 1 line
1.7.10.4
PATCH v4 6/9 lib: Correct object file dependencies
In reply to Serhey PopovychNeither internal libnetlink nor libgenl depends on ll_map.o: prepare for upcoming changes that brings much more cleaner dependency between utils.o and ll_map.o.
--- lib/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/Makefile +2 -2
@@ -3,11 +3,11 @@ include ../config.mk CFLAGS += -fPIC -UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ +UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o \ names.o color.o bpf.o exec.o fs.o -NLOBJ=libgenl.o ll_map.o libnetlink.o +NLOBJ=libgenl.o libnetlink.o all: libnetlink.a libutil.a
Signature · 1 line
1.7.10.4
PATCH v4 7/9 utils: Introduce and use get_ifname_rta()
In reply to Serhey PopovychBe consistent in handling of IFLA_IFNAME attribute in all places: if there is no attribute report bug to stderr and use ll_idx_n2a() as last measure to get name in "if%u" format instead of "<nil>".
Use check_ifname() to validate network device name: this catches both unexpected return from kernel and ll_idx_n2a().
--- bridge/link.c | 8 ++++---- include/utils.h | 1 + ip/ipaddress.c | 20 ++++++++------------ lib/utils.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-)
bridge/link.c +4 -4
@@ -99,9 +99,10 @@ int print_linkinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = arg; -int len = n->nlmsg_len;struct ifinfomsg *ifi =NLMSG_DATA (n);struct rtattr *tb[IFLA_MAX +1 ]; +int len = n->nlmsg_len; +const char *name; len -=NLMSG_LENGTH (sizeof (*ifi));if (len <0 ) {
@@ -117,10 +118,9 @@ int print_linkinfo(const struct sockaddr_nl *who,parse_rtattr_flags (tb,IFLA_MAX ,IFLA_RTA (ifi), len,NLA_F_NESTED ); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: nil ifname\n" ); + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name)return -1 ; - }if (n->nlmsg_type ==RTM_DELLINK )fprintf (fp,"Deleted " );
include/utils.h +1 -0
@@ -183,6 +183,7 @@ void duparg(const char *, const char *) __attribute__((noreturn));void duparg2 (const char *,const char *) __attribute__((noreturn));int check_ifname (const char *);int get_ifname (char *,const char *); +const char *get_ifname_rta (int ifindex,const struct rtattr *rta);int matches (const char *arg,const char *pattern);int inet_addr_match (const inet_prefix *a,const inet_prefix *b,int bits);int inet_addr_match_rta (const inet_prefix *m,const struct rtattr *rta);
ip/ipaddress.c +8 -12
@@ -776,12 +776,10 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&
@@ -903,12 +901,10 @@ int print_linkinfo(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&
lib/utils.c +19 -0
@@ -871,6 +871,25 @@ int get_ifname(char *buf, const char *name)return ret; } +const char *get_ifname_rta (int ifindex,const struct rtattr *rta) +{ +const char *name; + +if (rta) { + name =rta_getattr_str (rta); + }else { +fprintf (stderr, +"BUG: device with ifindex %d has nil ifname\n" , + ifindex); + name =ll_idx_n2a (ifindex); + } + +if (check_ifname (name)) +return NULL ; + +return name; +} +int matches (const char *cmd,const char *pattern) {int len =strlen (cmd);
Signature · 1 line
1.7.10.4
Re: [PATCH iproute2-next v4 7/9] utils: Introduce and use get_ifname_rta()
Quoted 115 lines · Serhey Popovych
Be consistent in handling of
IFLA_IFNAMEattribute in all places: if there is no attribute report bug to stderr and usell_idx_n2a()as last measure to get name in "if%u" format instead of "<nil>".Use
check_ifname()to validate network device name: this catches both unexpected return from kernel andll_idx_n2a().Signed-off-by Serhey Popovych <[email protected]>--- bridge/link.c | 8 ++++---- include/utils.h | 1 + ip/ipaddress.c | 20 ++++++++------------ lib/utils.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-)bridge/link.c +4
-4@@ -99,9 +99,10 @@ int print_linkinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = arg; -int len = n->nlmsg_len;struct ifinfomsg *ifi =NLMSG_DATA (n);struct rtattr *tb[IFLA_MAX +1 ]; +int len = n->nlmsg_len; +const char *name; len -=NLMSG_LENGTH (sizeof (*ifi));if (len <0 ) {@@ -117,10 +118,9 @@ int print_linkinfo(const struct sockaddr_nl *who,parse_rtattr_flags (tb,IFLA_MAX ,IFLA_RTA (ifi), len,NLA_F_NESTED ); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: nil ifname\n" ); + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name)return -1 ; - }if (n->nlmsg_type ==RTM_DELLINK )fprintf (fp,"Deleted " );include/utils.h +1
-0@@ -183,6 +183,7 @@ void duparg(const char *, const char *) __attribute__((noreturn));void duparg2 (const char *,const char *) __attribute__((noreturn));int check_ifname (const char *);int get_ifname (char *,const char *); +const char *get_ifname_rta (int ifindex,const struct rtattr *rta);int matches (const char *arg,const char *pattern);int inet_addr_match (const inet_prefix *a,const inet_prefix *b,int bits);int inet_addr_match_rta (const inet_prefix *m,const struct rtattr *rta);ip/ipaddress.c +8
-12@@ -776,12 +776,10 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&@@ -903,12 +901,10 @@ int print_linkinfo(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&lib/utils.c +12
-0@@ -871,6 +871,25 @@ int get_ifname(char *buf, const char *name)return ret; } +const char *get_ifname_rta (int ifindex,const struct rtattr *rta) +{ +const char *name; + +if (rta) { + name =rta_getattr_str (rta); + }else { +fprintf (stderr, +"BUG: device with ifindex %d has nil ifname\n" , + ifindex); + name =ll_idx_n2a (ifindex); + }
lib/utils.c int get_ifname(char *buf, const char *name) · line 895
Quoted 10 lines
lib/utils.c +7
-0+ +if (check_ifname (name)) +return NULL ; + +return name; +} +int matches (const char *cmd,const char *pattern) {int len =strlen (cmd);
Getting build failures with this one:
$ make clean; make -j 8 ...
devlink
CC devlink.o
CC mnlg.o
LINK devlink../lib/libutil.a(ll_map.o): In function `ll_remember_index':
ll_map.c:(.text+0xf6): undefined reference to parse_rtattr' ll_map.c:(.text+0x1c5): undefined reference to parse_rtattr' ../lib/libutil.a(ll_map.o): In function `ll_init_map':
ll_map.c:(.text+0x47c): undefined reference to rtnl_wilddump_request' ll_map.c:(.text+0x493): undefined reference to rtnl_dump_filter_nc'
Re: [PATCH iproute2-next v4 7/9] utils: Introduce and use get_ifname_rta()
Quoted 136 lines · David Ahern
Quoted 115 lines · Serhey Popovych
Be consistent in handling of
IFLA_IFNAMEattribute in all places: if there is no attribute report bug to stderr and usell_idx_n2a()as last measure to get name in "if%u" format instead of "<nil>".Use
check_ifname()to validate network device name: this catches both unexpected return from kernel andll_idx_n2a().Signed-off-by Serhey Popovych <[email protected]>--- bridge/link.c | 8 ++++---- include/utils.h | 1 + ip/ipaddress.c | 20 ++++++++------------ lib/utils.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-)bridge/link.c +4
-4@@ -99,9 +99,10 @@ int print_linkinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = arg; -int len = n->nlmsg_len;struct ifinfomsg *ifi =NLMSG_DATA (n);struct rtattr *tb[IFLA_MAX +1 ]; +int len = n->nlmsg_len; +const char *name; len -=NLMSG_LENGTH (sizeof (*ifi));if (len <0 ) {@@ -117,10 +118,9 @@ int print_linkinfo(const struct sockaddr_nl *who,parse_rtattr_flags (tb,IFLA_MAX ,IFLA_RTA (ifi), len,NLA_F_NESTED ); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: nil ifname\n" ); + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name)return -1 ; - }if (n->nlmsg_type ==RTM_DELLINK )fprintf (fp,"Deleted " );include/utils.h +1
-0@@ -183,6 +183,7 @@ void duparg(const char *, const char *) __attribute__((noreturn));void duparg2 (const char *,const char *) __attribute__((noreturn));int check_ifname (const char *);int get_ifname (char *,const char *); +const char *get_ifname_rta (int ifindex,const struct rtattr *rta);int matches (const char *arg,const char *pattern);int inet_addr_match (const inet_prefix *a,const inet_prefix *b,int bits);int inet_addr_match_rta (const inet_prefix *m,const struct rtattr *rta);ip/ipaddress.c +8
-12@@ -776,12 +776,10 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&@@ -903,12 +901,10 @@ int print_linkinfo(const struct sockaddr_nl *who,return -1 ;parse_rtattr (tb,IFLA_MAX ,IFLA_RTA (ifi), len); -if (tb[IFLA_IFNAME ] ==NULL ) { -fprintf (stderr,"BUG: device with ifindex %d has nil ifname\n" , ifi->ifi_index); - name =ll_idx_n2a (ifi->ifi_index); - }else { - name =rta_getattr_str (tb[IFLA_IFNAME ]); - } + + name =get_ifname_rta (ifi->ifi_index, tb[IFLA_IFNAME ]); +if (!name) +return -1 ; if (filter.label && (!filter.family || filter.family == AF_PACKET) &&lib/utils.c +12
-0@@ -871,6 +871,25 @@ int get_ifname(char *buf, const char *name)return ret; } +const char *get_ifname_rta (int ifindex,const struct rtattr *rta) +{ +const char *name; + +if (rta) { + name =rta_getattr_str (rta); + }else { +fprintf (stderr, +"BUG: device with ifindex %d has nil ifname\n" , + ifindex); + name =ll_idx_n2a (ifindex); + }lib/utils.c int get_ifname(char *buf, const char *name) · line 895
Quoted 10 lines
lib/utils.c +7
-0+ +if (check_ifname (name)) +return NULL ; + +return name; +} +int matches (const char *cmd,const char *pattern) {int len =strlen (cmd);Getting build failures with this one:
$ make clean; make -j 8 ...devlink
CC devlink.o CC mnlg.o LINK devlink
Quoted 8 lines
../lib/libutil.a(ll_map.o): In function `ll_remember_index':
ll_map.c:(.text+0xf6): undefined reference toparse_rtattr' ll_map.c:(.text+0x1c5): undefined reference toparse_rtattr' ../lib/libutil.a(ll_map.o): In function `ll_init_map':
ll_map.c:(.text+0x47c): undefined reference tortnl_wilddump_request' ll_map.c:(.text+0x493): undefined reference tortnl_dump_filter_nc'
Corrected, sorry for that. It is actually caused by "lib: Correct object file dependencies".
PATCH v4 8/9 utils: Introduce and use print_name_and_link() to print name@link
In reply to Serhey PopovychThere is at least three places implementing same things: two in ipaddress.c print_linkinfo() & print_linkinfo_brief() and one in bridge/link.c.
They are diverge from each other very little: bridge/link.c does not support JSON output at the moment and print_linkinfo_brief() does not handle IFLA_LINK_NETNS case.
Introduce and use print_name_and_link() routine to handle name@link output in all possible variations; respect IFLA_LINK_NETNS attribute to handle case when link is in different namespace; use ll_idx_n2a() for interface name instead of "<nil>" to share logic with other code (e.g. ll_name_to_index() and ll_index_to_name()) supporting such template.
--- bridge/link.c | 13 +++---------- include/utils.h | 4 ++++ ip/ipaddress.c | 44 ++------------------------------------------ lib/utils.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 52 deletions(-)
bridge/link.c +3 -10
@@ -125,20 +125,13 @@ int print_linkinfo(const struct sockaddr_nl *who,if (n->nlmsg_type ==RTM_DELLINK )fprintf (fp,"Deleted " ); -fprintf (fp,"%d: %s " , ifi->ifi_index, - tb[IFLA_IFNAME ] ?rta_getattr_str (tb[IFLA_IFNAME ]) :"<nil>" ); +fprintf (fp,"%d: " , ifi->ifi_index); + +print_name_and_link ("%s: " ,COLOR_NONE , name, tb);if (tb[IFLA_OPERSTATE ])print_operstate (fp,rta_getattr_u8 (tb[IFLA_OPERSTATE ])); -if (tb[IFLA_LINK ]) { -int iflink =rta_getattr_u32 (tb[IFLA_LINK ]); - -fprintf (fp,"@%s: " , - iflink ?ll_index_to_name (iflink) :"NONE" ); - }else -fprintf (fp,": " ); -print_link_flags (fp, ifi->ifi_flags);if (tb[IFLA_MTU ])
include/utils.h +4 -0
@@ -12,6 +12,7 @@#include "libnetlink.h" #include "ll_map.h" #include "rtm_map.h" +#include "json_print.h" extern int preferred_family;extern int human_readable;
@@ -250,6 +251,9 @@ void print_escape_buf(const __u8 *buf, size_t len, const char *escape);int print_timestamp (FILE *fp);void print_nlmsg_timestamp (FILE *fp,const struct nlmsghdr *n); +unsigned int print_name_and_link (const char *fmt,enum color_attr color, +const char *name,struct rtattr *tb[]); +#define BIT (nr) (1UL << (nr))#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0]))
ip/ipaddress.c +2 -42
63 lines not on this page · raw message
lib/utils.c +49 -0
61 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Re: [PATCH iproute2-next v4 8/9] utils: Introduce and use print_name_and_link() to print name@link
bridge/link.c int print_linkinfo(const struct sockaddr_nl *who, · line 130
Quoted 3 lines · On Thu, 15 Feb 2018 20:34:07 +0200 Serhey Popovych <[email protected]>…
bridge/link.c +3
-0+fprintf (fp,"%d: " , ifi->ifi_index); + +print_name_and_link ("%s: " ,COLOR_NONE , name, tb);
This should be COLOR_IFNAME but I am working on that.
Re: [PATCH iproute2-next v4 8/9] utils: Introduce and use print_name_and_link() to print name@link
Quoted 9 lines · Stephen Hemminger
bridge/link.c int print_linkinfo(const struct sockaddr_nl *who, · line 130
Quoted 3 lines · On Thu, 15 Feb 2018 20:34:07 +0200 Serhey Popovych <[email protected]>…
bridge/link.c +3
-0+fprintf (fp,"%d: " , ifi->ifi_index); + +print_name_and_link ("%s: " ,COLOR_NONE , name, tb);This should be
COLOR_IFNAMEbut I am working on that.
Let me commit this series and adjust from there.
PATCH v4 9/9 ipaddress: Make print_linkinfo_brief() static
In reply to Serhey PopovychIt shares lot of code with print_linkinfo(): drop duplicated part, change parameters list, make it static and call from print_linkinfo() after common path.
While there move SPRINT_BUF() to the function scope from blocks to avoid duplication and use "%s" to print "\n" to help compiler optimize exit for both print_linkinfo_brief() and normal paths.
--- ip/ip_common.h | 2 -- ip/ipaddress.c | 76 ++++++++------------------------------------------------ ip/iplink.c | 5 +--- 3 files changed, 11 insertions(+), 72 deletions(-)
ip/ip_common.h +0 -2
@@ -29,8 +29,6 @@ struct link_filter {int get_operstate (const char *name);int print_linkinfo (const struct sockaddr_nl *who,struct nlmsghdr *n,void *arg); -int print_linkinfo_brief (const struct sockaddr_nl *who, -struct nlmsghdr *n,void *arg);int print_addrinfo (const struct sockaddr_nl *who,struct nlmsghdr *n,void *arg); int print_addrlabel(const struct sockaddr_nl *who,
ip/ipaddress.c +10 -66
135 lines not on this page · raw message
ip/iplink.c +1 -4
@@ -1075,10 +1075,7 @@ int iplink_get(unsigned int flags, char *name, __u32 filt_mask)return -2 ;open_json_object (NULL ); -if (brief) -print_linkinfo_brief (NULL , answer, stdout); -else -print_linkinfo (NULL , answer, stdout); +print_linkinfo (NULL , answer, stdout);close_json_object ();free (answer);
Signature · 1 line
1.7.10.4