Skip to contentSkip to conversation

ip netconf improvements

PATCH Superseded David Ahern 2 participants 5 messages Mar 22, 2017 – Mar 23, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ip netconf improvements

Currently, ip netconf only shows data for ipv4 and ipv6 for dumps and just ipv4 for device requests. Improve the user experience by using the new kernel patch to dump all address families that have registered. For example, if mpls_router module is loaded then mpls values are displayed along with ipv4 and ipv6.

If the new feature is not supported (new iproute2 on older kernel) the kernel returns the nlmsg error EOPNOTSUPP which can be trapped and fallback to existing behavior.

David Ahern (3):
netlink: Add flag to suppress print of nlmsg error ip netconf: Show all address families by default in dumps ip netconf: Show all families on dev request

 include/libnetlink.h |  1 +
 ip/ipnetconf.c       | 36 +++++++++++++++++++++++++-----------
 lib/libnetlink.c     |  3 ++-
 3 files changed, 28 insertions(+), 12 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/3 netlink: Add flag to suppress print of nlmsg error

Allow callers of the dump API to handle nlmsg errors (e.g., an unsupported feature). Setting RTNL_HANDLE_F_SUPPRESS_NLERR in the rtnl_handle avoids unnecessary messages to the users in some case. For example,

RTNETLINK answers: Operation not supported

when probing for support of a new feature.

---
 include/libnetlink.h | 1 +
 lib/libnetlink.c     | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/libnetlink.h +1 -0
index bd0267dfcc02..c43ab0a2d9d9 100644
@@ -21,6 +21,7 @@ struct rtnl_handle {
 	int			proto;
 	FILE		       *dump_fp;
 #define RTNL_HANDLE_F_LISTEN_ALL_NSID		0x01
+#define RTNL_HANDLE_F_SUPPRESS_NLERR		0x02
 	int			flags;
 };
 
lib/libnetlink.c +2 -1
index 9303b6686e2c..5b75b2db4e0b 100644
@@ -299,7 +299,8 @@ static void rtnl_dump_error(const struct rtnl_handle *rth,
 		     errno == EOPNOTSUPP))
 			return;
 
-		perror("RTNETLINK answers");
+		if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR))
+			perror("RTNETLINK answers");
 	}
 }
 
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/3 ip netconf: Show all address families by default in dumps

In reply to David Ahern

Currently, 'ip netconf' only shows ipv4 and ipv6 netconf settings. If IPv6 is not enabled, the dump ends with
RTNETLINK answers: Operation not supported

when IPv6 request is attempted. Further, if the mpls_router module is also loaded a separate request is needed to get MPLS settings.

To make this better going forward, use the new PF_UNSPEC dump all option if the kernel supports it. If the kernel does not, it sets NLMSG_ERROR and returns EOPNOTSUPP which is trapped and we fall back to the existing output to maintain compatibility with existing kernels.

---
 ip/ipnetconf.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
ip/ipnetconf.c +12 -1
index af539f5e945c..dc0851025223 100644
@@ -19,6 +19,7 @@
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <errno.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -197,16 +198,26 @@ static int do_show(int argc, char **argv)
 		}
 		rtnl_listen(&rth, print_netconf, stdout);
 	} else {
+		rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR;
 dump:
 		if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) {
 			perror("Cannot send dump request");
 			exit(1);
 		}
 		if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) {
+			/* kernel does not support netconf dump on AF_UNSPEC;
+			 * fall back to requesting by family
+			 */
+			if (errno == EOPNOTSUPP &&
+			    filter.family == AF_UNSPEC) {
+				filter.family = AF_INET;
+				goto dump;
+			}
+			perror("RTNETLINK answers");
 			fprintf(stderr, "Dump terminated\n");
 			exit(1);
 		}
-		if (preferred_family == AF_UNSPEC) {
+		if (preferred_family == AF_UNSPEC && filter.family == AF_INET) {
 			preferred_family = AF_INET6;
 			filter.family = AF_INET6;
 			goto dump;
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/3 ip netconf: Show all families on dev request

In reply to David Ahern

Currently specifying a device to ip netconf and it dumps only values for IPv4. Change this to dump data for all families unless a specific family is given.

---
 ip/ipnetconf.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
ip/ipnetconf.c +13 -10
index dc0851025223..ab4c1d9db7c8 100644
@@ -56,6 +56,7 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 	struct netconfmsg *ncm = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[NETCONFA_MAX+1];
+	int ifindex = 0;
 
 	if (n->nlmsg_type == NLMSG_ERROR)
 		return -1;
@@ -77,6 +78,12 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 	parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
 		     NLMSG_PAYLOAD(n, sizeof(*ncm)));
 
+	if (tb[NETCONFA_IFINDEX])
+		ifindex = *((int *)rta_getattr_str(tb[NETCONFA_IFINDEX]));
+
+	if (filter.ifindex && filter.ifindex != ifindex)
+		return 0;
+
 	switch (ncm->ncm_family) {
 	case AF_INET:
 		fprintf(fp, "ipv4 ");
@@ -93,9 +100,7 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 	}
 
 	if (tb[NETCONFA_IFINDEX]) {
-		int *ifindex = (int *)rta_getattr_str(tb[NETCONFA_IFINDEX]);
-
-		switch (*ifindex) {
+		switch (ifindex) {
 		case NETCONFA_IFINDEX_ALL:
 			fprintf(fp, "all ");
 			break;
@@ -103,7 +108,7 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 			fprintf(fp, "default ");
 			break;
 		default:
-			fprintf(fp, "dev %s ", ll_index_to_name(*ifindex));
+			fprintf(fp, "dev %s ", ll_index_to_name(ifindex));
 			break;
 		}
 	}
@@ -169,8 +174,6 @@ static int do_show(int argc, char **argv)
 
 	ipnetconf_reset_filter(0);
 	filter.family = preferred_family;
-	if (filter.family == AF_UNSPEC)
-		filter.family = AF_INET;
 
 	while (argc > 0) {
 		if (strcmp(*argv, "dev") == 0) {
@@ -186,11 +189,11 @@ static int do_show(int argc, char **argv)
 	}
 
 	ll_init_map(&rth);
-	if (filter.ifindex) {
+
+	if (filter.ifindex && filter.family != AF_UNSPEC) {
 		req.ncm.ncm_family = filter.family;
-		if (filter.ifindex)
-			addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
-				  &filter.ifindex, sizeof(filter.ifindex));
+		addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
+			  &filter.ifindex, sizeof(filter.ifindex));
 
 		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
 			perror("Can not send request");
Signature · 1 line
2.1.4
#4 lore raw

Re: [iproute2 net-next 3/3] ip netconf: Show all families on dev request

Quoted 22 lines · David Ahern

Currently specifying a device to ip netconf and it dumps only values for IPv4. Change this to dump data for all families unless a specific family is given.

---
 ip/ipnetconf.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
ip/ipnetconf.c +1 -0
index dc0851025223..ab4c1d9db7c8 100644
@@ -56,6 +56,7 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 	struct netconfmsg *ncm = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[NETCONFA_MAX+1];
+	int ifindex = 0;
 
 	if (n->nlmsg_type == NLMSG_ERROR)
 		return -1;

ip/ipnetconf.c int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl, · line 82

Quoted 5 lines
ip/ipnetconf.c +2 -0
@@ -77,6 +78,12 @@ int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
 	parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm),
 		     NLMSG_PAYLOAD(n, sizeof(*ncm)));
 
+	if (tb[NETCONFA_IFINDEX])
+		ifindex = *((int *)rta_getattr_str(tb[NETCONFA_IFINDEX]));

This line is moved, but rta_getattr_u32() is probably more right.

Regards,
Nicolas