Skip to contentSkip to conversation

configure: add missing INCLUDE to netnsid detection

PATCH In review Jiri Benc 5 participants 11 messages Apr 8, 2015 – Apr 13, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Jiri Benc

author
#0 lore raw

PATCH 1/1 configure: add missing INCLUDE to netnsid detection

Fixes d116ff34145b0 ("ip netns: Fix rtnl error while print netns list")

---
The whole check added by d116ff34145b0 is dubious, though. This should be a run time check, not a compile time one.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index 631938e97206..e54f4b6db5e8 100755
@@ -224,7 +224,7 @@ check_netnsid()
 #include <linux/rtnetlink.h>
 int test_def = RTM_GETNSID;
 EOF
-    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
+    $CC -I$INCLUDE -c $TMPDIR/netnsid.c >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
 	echo "IP_CONFIG_NETNSID:=y" >> Config
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 25 lines · Jiri Benc
Fixes d116ff34145b0 ("ip netns: Fix rtnl error while print netns list")

---
The whole check added by d116ff34145b0 is dubious, though. This should be a run time check, not a compile time one.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index 631938e97206..e54f4b6db5e8 100755
@@ -224,7 +224,7 @@ check_netnsid()
 #include <linux/rtnetlink.h>
 int test_def = RTM_GETNSID;
 EOF
-    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
+    $CC -I$INCLUDE -c $TMPDIR/netnsid.c >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
 	echo "IP_CONFIG_NETNSID:=y" >> Config
Signature · 1 line
1.8.3.1

Hi,

On 3.18 I get same RTNL error with this fix, I agree that runtime checking would be better than compile time checking, but how to do it, suppress RTNL errors ?

Regards,

#2 lore raw
Quoted 6 lines · Vadim Kochan
Quoted 5 lines · Jiri Benc
Fixes d116ff34145b0 ("ip netns: Fix rtnl error while print netns list")

---
The whole check added by d116ff34145b0 is dubious, though. This should be a run time check, not a compile time one.

[snip]

Quoted 4 lines

On 3.18 I get same RTNL error with this fix, I agree that runtime checking would be better than compile time checking, but how to do it, suppress RTNL errors ?

I also hit this issue.
This whole check is done only to bypass some fprintf(stderr) messages from rtnl_talk().
I think adding a mechanism like iplink_have_newlink() should be enough.

If nobody wants to do it, I will look at this on Monday.

Regards,
Nicolas

#3 lore raw

PATCH 1/3 Revert "configure: add missing INCLUDE to netnsid detection"

This reverts commit d059de70cafb470f77fc19a42d95f6dc442cf6a3.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index e54f4b6db5e8..631938e97206 100755
@@ -224,7 +224,7 @@ check_netnsid()
 #include <linux/rtnetlink.h>
 int test_def = RTM_GETNSID;
 EOF
-    $CC -I$INCLUDE -c $TMPDIR/netnsid.c >/dev/null 2>&1
+    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
 	echo "IP_CONFIG_NETNSID:=y" >> Config
Signature · 1 line
2.2.2
#4 lore raw

PATCH 2/3 Revert "ip netns: Fix rtnl error while print netns list"

This reverts commit d116ff34145b00db54a37e2a6282dccd8bc08225.

---
 configure    | 21 +--------------------
 ip/Makefile  |  4 ----
 ip/ipnetns.c |  7 -------
 3 files changed, 1 insertion(+), 31 deletions(-)
configure +1 -20
index 631938e97206..c3dacdba14ff 100755
@@ -201,7 +201,7 @@ check_setns()
 {
     cat >$TMPDIR/setnstest.c <<EOF
 #include <sched.h>
-int main(int argc, char **argv)
+int main(int argc, char **argv) 
 {
 	(void)setns(0,0);
 	return 0;
@@ -218,23 +218,6 @@ EOF
     rm -f $TMPDIR/setnstest.c $TMPDIR/setnstest
 }
 
-check_netnsid()
-{
-    cat >$TMPDIR/netnsid.c <<EOF
-#include <linux/rtnetlink.h>
-int test_def = RTM_GETNSID;
-EOF
-    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
-    if [ $? -eq 0 ]
-    then
-	echo "IP_CONFIG_NETNSID:=y" >> Config
-	echo "yes"
-    else
-	echo "no"
-    fi
-    rm -f $TMPDIR/netnsid.c $TMPDIR/netnsid.o
-}
-
 check_ipset()
 {
     cat >$TMPDIR/ipsettest.c <<EOF
@@ -300,8 +283,6 @@ check_ipt_lib_dir
 
 echo -n "libc has setns: "
 check_setns
-echo -n "netns has peer id suport: "
-check_netnsid
 
 echo -n "SELinux support: "
 check_selinux
ip/Makefile +0 -4
index 5637bcfebde0..2c742f305fef 100644
@@ -16,10 +16,6 @@ ifeq ($(IP_CONFIG_SETNS),y)
 	CFLAGS += -DHAVE_SETNS
 endif
 
-ifeq ($(IP_CONFIG_NETNSID),y)
-	CFLAGS += -DHAVE_NETNSID
-endif
-
 ALLOBJ=$(IPOBJ) $(RTMONOBJ)
 SCRIPTS=ifcfg rtpr routel routef
 TARGETS=ip rtmon
ip/ipnetns.c +0 -7
index 45e234a4d98c..5a213dcf46cd 100644
@@ -34,7 +34,6 @@ static int usage(void)
 	exit(-1);
 }
 
-#ifdef HAVE_NETNSID
 static int get_netnsid_from_name(const char *name)
 {
 	struct {
@@ -79,12 +78,6 @@ static int get_netnsid_from_name(const char *name)
 
 	return -1;
 }
-#else
-static int get_netnsid_from_name(const char *name)
-{
-	return -1;
-}
-#endif /* HAVE_NETNSID */
 
 static int netns_list(int argc, char **argv)
 {
Signature · 1 line
2.2.2
#5 lore raw

PATCH 3/3 ipnetns: add a runtime check for RTM_GETNSID support

In reply to Nicolas Dichtel

The goal of this patch is to test during the runtime if the command RTM_GETNSID is supported by the kernel.

---
 ip/ipnetns.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 53 insertions(+), 3 deletions(-)
ip/ipnetns.c +53 -3
index 5a213dcf46cd..12f6b90de89a 100644
@@ -34,6 +34,54 @@ static int usage(void)
 	exit(-1);
 }
 
+static int have_rtnl_getnsid = -1;
+
+static int ipnetns_accept_msg(const struct sockaddr_nl *who,
+			      struct nlmsghdr *n, void *arg)
+{
+	struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
+
+	if (n->nlmsg_type == NLMSG_ERROR &&
+	    (err->error == -EOPNOTSUPP || err->error == -EINVAL))
+		have_rtnl_getnsid = 0;
+	else
+		have_rtnl_getnsid = 1;
+	return -1;
+}
+
+static int ipnetns_have_nsid(void)
+{
+	struct {
+		struct nlmsghdr n;
+		struct rtgenmsg g;
+		char            buf[1024];
+	} req;
+	int fd;
+
+	if (have_rtnl_getnsid < 0) {
+		memset(&req, 0, sizeof(req));
+		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
+		req.n.nlmsg_flags = NLM_F_REQUEST;
+		req.n.nlmsg_type = RTM_GETNSID;
+		req.g.rtgen_family = AF_UNSPEC;
+
+		fd = open("/proc/self/ns/net", O_RDONLY);
+		if (fd < 0)
+			return fd;
+
+		addattr32(&req.n, 1024, NETNSA_FD, fd);
+
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
+		rtnl_listen(&rth, ipnetns_accept_msg, NULL);
+		close(fd);
+	}
+
+	return have_rtnl_getnsid;
+}
+
 static int get_netnsid_from_name(const char *name)
 {
 	struct {
@@ -95,9 +143,11 @@ static int netns_list(int argc, char **argv)
 		if (strcmp(entry->d_name, "..") == 0)
 			continue;
 		printf("%s", entry->d_name);
-		id = get_netnsid_from_name(entry->d_name);
-		if (id >= 0)
-			printf(" (id: %d)", id);
+		if (ipnetns_have_nsid()) {
+			id = get_netnsid_from_name(entry->d_name);
+			if (id >= 0)
+				printf(" (id: %d)", id);
+		}
 		printf("\n");
 	}
 	closedir(dir);
Signature · 1 line
2.2.2
#6 lore raw

PATCH v2 1/3 Revert "configure: add missing INCLUDE to netnsid detection"

This reverts commit d059de70cafb470f77fc19a42d95f6dc442cf6a3.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index e54f4b6db5e8..631938e97206 100755
@@ -224,7 +224,7 @@ check_netnsid()
 #include <linux/rtnetlink.h>
 int test_def = RTM_GETNSID;
 EOF
-    $CC -I$INCLUDE -c $TMPDIR/netnsid.c >/dev/null 2>&1
+    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
 	echo "IP_CONFIG_NETNSID:=y" >> Config
Signature · 1 line
2.2.2
#7 lore raw

PATCH v2 2/3 Revert "ip netns: Fix rtnl error while print netns list"

This reverts commit d116ff34145b00db54a37e2a6282dccd8bc08225.

---
 configure    | 21 +--------------------
 ip/Makefile  |  4 ----
 ip/ipnetns.c |  7 -------
 3 files changed, 1 insertion(+), 31 deletions(-)
configure +1 -20
index 631938e97206..c3dacdba14ff 100755
@@ -201,7 +201,7 @@ check_setns()
 {
     cat >$TMPDIR/setnstest.c <<EOF
 #include <sched.h>
-int main(int argc, char **argv)
+int main(int argc, char **argv) 
 {
 	(void)setns(0,0);
 	return 0;
@@ -218,23 +218,6 @@ EOF
     rm -f $TMPDIR/setnstest.c $TMPDIR/setnstest
 }
 
-check_netnsid()
-{
-    cat >$TMPDIR/netnsid.c <<EOF
-#include <linux/rtnetlink.h>
-int test_def = RTM_GETNSID;
-EOF
-    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
-    if [ $? -eq 0 ]
-    then
-	echo "IP_CONFIG_NETNSID:=y" >> Config
-	echo "yes"
-    else
-	echo "no"
-    fi
-    rm -f $TMPDIR/netnsid.c $TMPDIR/netnsid.o
-}
-
 check_ipset()
 {
     cat >$TMPDIR/ipsettest.c <<EOF
@@ -300,8 +283,6 @@ check_ipt_lib_dir
 
 echo -n "libc has setns: "
 check_setns
-echo -n "netns has peer id suport: "
-check_netnsid
 
 echo -n "SELinux support: "
 check_selinux
ip/Makefile +0 -4
index 5637bcfebde0..2c742f305fef 100644
@@ -16,10 +16,6 @@ ifeq ($(IP_CONFIG_SETNS),y)
 	CFLAGS += -DHAVE_SETNS
 endif
 
-ifeq ($(IP_CONFIG_NETNSID),y)
-	CFLAGS += -DHAVE_NETNSID
-endif
-
 ALLOBJ=$(IPOBJ) $(RTMONOBJ)
 SCRIPTS=ifcfg rtpr routel routef
 TARGETS=ip rtmon
ip/ipnetns.c +0 -7
index 45e234a4d98c..5a213dcf46cd 100644
@@ -34,7 +34,6 @@ static int usage(void)
 	exit(-1);
 }
 
-#ifdef HAVE_NETNSID
 static int get_netnsid_from_name(const char *name)
 {
 	struct {
@@ -79,12 +78,6 @@ static int get_netnsid_from_name(const char *name)
 
 	return -1;
 }
-#else
-static int get_netnsid_from_name(const char *name)
-{
-	return -1;
-}
-#endif /* HAVE_NETNSID */
 
 static int netns_list(int argc, char **argv)
 {
Signature · 1 line
2.2.2
#8 lore raw

PATCH v2 3/3 ipnetns: add a runtime check for RTM_GETNSID support

In reply to Nicolas Dichtel

The goal of this patch is to test during the runtime if the command RTM_GETNSID is supported by the kernel.

---

v2: fix error handling in ipnetns_have_nsid() when open() fails

 ip/ipnetns.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 55 insertions(+), 3 deletions(-)
ip/ipnetns.c +55 -3
index 5a213dcf46cd..24df167e9262 100644
@@ -34,6 +34,56 @@ static int usage(void)
 	exit(-1);
 }
 
+static int have_rtnl_getnsid = -1;
+
+static int ipnetns_accept_msg(const struct sockaddr_nl *who,
+			      struct nlmsghdr *n, void *arg)
+{
+	struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
+
+	if (n->nlmsg_type == NLMSG_ERROR &&
+	    (err->error == -EOPNOTSUPP || err->error == -EINVAL))
+		have_rtnl_getnsid = 0;
+	else
+		have_rtnl_getnsid = 1;
+	return -1;
+}
+
+static int ipnetns_have_nsid(void)
+{
+	struct {
+		struct nlmsghdr n;
+		struct rtgenmsg g;
+		char            buf[1024];
+	} req;
+	int fd;
+
+	if (have_rtnl_getnsid < 0) {
+		memset(&req, 0, sizeof(req));
+		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
+		req.n.nlmsg_flags = NLM_F_REQUEST;
+		req.n.nlmsg_type = RTM_GETNSID;
+		req.g.rtgen_family = AF_UNSPEC;
+
+		fd = open("/proc/self/ns/net", O_RDONLY);
+		if (fd < 0) {
+			perror("open(\"/proc/self/ns/net\")");
+			exit(1);
+		}
+
+		addattr32(&req.n, 1024, NETNSA_FD, fd);
+
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
+		rtnl_listen(&rth, ipnetns_accept_msg, NULL);
+		close(fd);
+	}
+
+	return have_rtnl_getnsid;
+}
+
 static int get_netnsid_from_name(const char *name)
 {
 	struct {
@@ -95,9 +145,11 @@ static int netns_list(int argc, char **argv)
 		if (strcmp(entry->d_name, "..") == 0)
 			continue;
 		printf("%s", entry->d_name);
-		id = get_netnsid_from_name(entry->d_name);
-		if (id >= 0)
-			printf(" (id: %d)", id);
+		if (ipnetns_have_nsid()) {
+			id = get_netnsid_from_name(entry->d_name);
+			if (id >= 0)
+				printf(" (id: %d)", id);
+		}
 		printf("\n");
 	}
 	closedir(dir);
Signature · 1 line
2.2.2

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw
In reply to Jiri Benc
Quoted 14 lines · On Wed, 8 Apr 2015 21:42:00 +0200 Jiri Benc <[email protected]> wrote:
Fixes d116ff34145b0 ("ip netns: Fix rtnl error while print netns list")

---
The whole check added by d116ff34145b0 is dubious, though. This should be a run time check, not a compile time one.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +0 -0
index 631938e97206..e54f4b6db5e8 100755

configure check_netnsid() · line 230

Quoted 8 lines
configure +1 -1
@@ -224,7 +224,7 @@ check_netnsid()
 #include <linux/rtnetlink.h>
 int test_def = RTM_GETNSID;
 EOF
-    $CC -c $TMPDIR/netnsid.c >/dev/null 2>&1
+    $CC -I$INCLUDE -c $TMPDIR/netnsid.c >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
 	echo "IP_CONFIG_NETNSID:=y" >> Config

Merged, but always up for a better solution in future.