Skip to contentSkip to conversation

containers: added netns/netns_interfaces.c

PATCH In review Matus Marhefka 2 participants 8 messages Aug 27, 2014 – Oct 27, 2014 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 containers: added netns/netns_interfaces.c

* Tests that a separate network namespace can only communicate over the devices it sees

---
 runtest/containers                                 |   1 +
 testcases/kernel/containers/netns/.gitignore       |   1 +
 testcases/kernel/containers/netns/Makefile         |   3 +-
 .../kernel/containers/netns/netns_interfaces.c     | 205 +++++++++++++++++++++
 4 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/containers/netns/netns_interfaces.c
runtest/containers +1 -0
index 7d01a44..01ed4cc 100644
@@ -29,6 +29,7 @@ netns_crtchild_delchild netns_crtchild_delchild
 netns_par_chld_ipv6 netns_par_chld_ipv6
 netns_par_chld_ftp netns_par_chld_ftp.sh
 netns_netlink netns_netlink
+netns_interfaces netns_interfaces
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
testcases/kernel/containers/netns/.gitignore +1 -0
index 65f96be..a134677 100644
@@ -6,3 +6,4 @@
 /netns_sysfsview
 /netns_two_children_ns
 /netns_netlink
+/netns_interfaces
testcases/kernel/containers/netns/Makefile +2 -1
index eea0d88..cc8827f 100644
@@ -31,7 +31,8 @@ LDLIBS			+= -lclone
 MAKE_TARGETS		:= netns_create_container netns_crtchild \
 			   netns_crtchild_delchild netns_par_chld_ftp \
 			   netns_par_chld_ipv6 netns_sysfsview \
-			   netns_two_children_ns netns_netlink
+			   netns_two_children_ns netns_netlink \
+			   netns_interfaces
 
 $(MAKE_TARGETS): %: common.o %.o
 
testcases/kernel/containers/netns/netns_interfaces.c new file +205 -0

205 lines not on this page · raw message

Signature · 11 lines
1.8.3.1


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

PATCH v2 1/1 containers: added netns/netns_devices.sh and netns/netns_devices2.sh

* Tests that a separate network namespace can configure and communicate over the devices it sees
* netns/netns_devices.sh -- uses netlink(7) for configuration
* netns/netns_devices2.sh -- uses ioctl(2) for configuration

---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/netns/netns_devices.sh | 110 ++++++++++++++++++++
 .../kernel/containers/netns/netns_devices2.sh      | 112 +++++++++++++++++++++
 3 files changed, 224 insertions(+)
 create mode 100755 testcases/kernel/containers/netns/netns_devices.sh
 create mode 100755 testcases/kernel/containers/netns/netns_devices2.sh
runtest/containers +2 -0
index 7d01a44..69eac82 100644
@@ -29,6 +29,8 @@ netns_crtchild_delchild netns_crtchild_delchild
 netns_par_chld_ipv6 netns_par_chld_ipv6
 netns_par_chld_ftp netns_par_chld_ftp.sh
 netns_netlink netns_netlink
+netns_devices netns_devices.sh
+netns_devices2 netns_devices2.sh
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
testcases/kernel/containers/netns/netns_devices.sh new file +110 -0

110 lines not on this page · raw message

testcases/kernel/containers/netns/netns_devices2.sh new file +112 -0

112 lines not on this page · raw message

Signature · 11 lines
1.8.3.1


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#2 lore raw

PATCH v3 1/1 containers: added netns/netns_devices.sh and netns/netns_devices2.sh

In reply to Matus Marhefka

* Tests that a separate network namespace can configure and communicate over the devices it sees
* netns/netns_devices.sh -- uses netlink(7) for configuration
* netns/netns_devices2.sh -- uses ioctl(2) for configuration

---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/netns/netns_devices.sh | 110 +++++++++++++++++++++
 .../kernel/containers/netns/netns_devices2.sh      | 110 +++++++++++++++++++++
 3 files changed, 222 insertions(+)
 create mode 100755 testcases/kernel/containers/netns/netns_devices.sh
 create mode 100755 testcases/kernel/containers/netns/netns_devices2.sh
runtest/containers +2 -0
index 7d01a44..69eac82 100644
@@ -29,6 +29,8 @@ netns_crtchild_delchild netns_crtchild_delchild
 netns_par_chld_ipv6 netns_par_chld_ipv6
 netns_par_chld_ftp netns_par_chld_ftp.sh
 netns_netlink netns_netlink
+netns_devices netns_devices.sh
+netns_devices2 netns_devices2.sh
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
testcases/kernel/containers/netns/netns_devices.sh new file +110 -0

110 lines not on this page · raw message

testcases/kernel/containers/netns/netns_devices2.sh new file +110 -0

110 lines not on this page · raw message

Signature · 11 lines
1.8.3.1


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#3 lore raw

Re: [LTP] [PATCH v3] containers: added netns/netns_devices.sh and netns/netns_devices2.sh

Hi!

testcases/kernel/containers/netns/netns_devices.sh line 52

Quoted 3 lines
testcases/kernel/containers/netns/netns_devices.sh new file +3 -0
+# creates a pair of virtual ethernet devices
+ip link add veth0 type veth peer name veth1 &>/dev/null || \
+	tst_brkm TBROK "unable to create veth pair devices"

The &> operator is bashism and the testcases should be runable with other shells too. Generally it's a good idea to try to run them with dash or bussy box sh before submission.

And also I'm not 100% sure that it's a good idea to hide the nature of the problem. Are these commands too verbose by default?

Quoted 24 lines
testcases/kernel/containers/netns/netns_devices.sh new file +24 -0
+# creates a new network namespace "myns" (man 8 ip-netns)
+ip netns add myns &>/dev/null || \
+	tst_brkm TBROK "unable to create a new network namespace"
+
+# adds device veth1 to myns namespace
+ip link set veth1 netns myns &>/dev/null || \
+	tst_brkm TBROK "unable to add device veth1 to the network namespace myns"
+
+
+# TEST CASE #1
+# setup for veth0 device
+ip address add $IP0/24 dev veth0 || \
+	tst_brkm TBROK "adding address to veth0 failed"
+ip link set dev veth0 up || \
+	tst_brkm TBROK "enabling veth0 device failed"
+
+# setup for veth1 (which is inside myns namespace)
+ip netns exec myns ip address add $IP1/24 dev veth1 || \
+	tst_brkm TBROK "adding address to veth1 failed"
+ip netns exec myns ip link set dev veth1 up || \
+	tst_brkm TBROK "enabling veth1 device failed"
+
+ping -q -c 2 -I veth0 $IP1 &>/dev/null
+ret=$?

testcases/kernel/containers/netns/netns_devices.sh line 87

Quoted 10 lines
testcases/kernel/containers/netns/netns_devices.sh new file +10 -0
+failed=0
+if [ $ret -ne 0 ]; then
+	failed=1
+fi
+
+ip netns exec myns ping -q -c 2 -I veth1 $IP0 &>/dev/null
+ret=$?
+if [ $ret -ne 0 ]; then
+	failed=1
+fi

It would be cleaner to do tst_resm in both cases and declare that the test has three testcases.

Quoted 78 lines
testcases/kernel/containers/netns/netns_devices.sh new file +22 -0
+if [ $failed -eq 0 ]; then
+	tst_resm TPASS "netlink configuration and communication over veth devices"
+else
+	tst_resm TFAIL "netlink configuration and communication over veth devices"
+fi
+
+
+# TEST CASE #2
+# enables lo device
+ip netns exec myns ip link set dev lo up || \
+	tst_brkm TBROK "enabling lo device failed"
+
+ip netns exec myns ping -q -c 2 -I lo 127.0.0.1 &>/dev/null
+ret=$?
+if [ $ret -eq 0 ]; then
+	tst_resm TPASS "netlink configuration and communication over lo device"
+else
+	tst_resm TFAIL "netlink configuration and communication over lo device"
+fi
+
+
+tst_exit
testcases/kernel/containers/netns/netns_devices2.sh new file +50 -0
new file mode 100755
index 0000000..ed3f5eb
@@ -0,0 +1,110 @@
+#!/bin/sh
+#==============================================================================
+# Copyright (c) 2014 Red Hat, Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of version 2 the GNU General Public License as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#==============================================================================
+# File: netns_devices2.sh
+#
+# Tests that a separate network namespace can configure and communicate
+# over the devices it sees. Tests are done using ifconfig command which
+# uses ioctl(2) to control devices. There are two test cases:
+# 1. communication over paired veth (virtual ethernet) devices
+#    from two different network namespaces (each namespace has
+#    one device)
+# 2. communication over the lo (localhost) device in a separate
+#    network namespace
+#
+
+TCID=netns_devices2
+TST_TOTAL=2
+. test.sh
+IP0=192.168.0.1
+IP1=192.168.0.2
+
+
+cleanup()
+{
+	# removes veth0 device (which also removes paired veth1 device)
+	ip link delete veth0
+	# removes the network namespace myns
+	ip netns del myns
+}
+
+
+# SETUP
+tst_require_root
+tst_check_cmds ip ifconfig
+TST_CLEANUP=cleanup
+
+# creates a pair of virtual ethernet devices

testcases/kernel/containers/netns/netns_devices2.sh line 60

Quoted 10 lines
testcases/kernel/containers/netns/netns_devices2.sh new file +10 -0
+ip link add veth0 type veth peer name veth1 &>/dev/null || \
+	tst_brkm TBROK "unable to create veth pair devices"
+
+# creates a new network namespace "myns" (man 8 ip-netns)
+ip netns add myns &>/dev/null || \
+	tst_brkm TBROK "unable to create a new network namespace"
+
+# adds device veth1 to myns namespace
+ip link set veth1 netns myns &>/dev/null || \
+	tst_brkm TBROK "unable to add device veth1 to the network namespace myns"

Here the &>/dev/null as well.

Quoted 21 lines
testcases/kernel/containers/netns/netns_devices2.sh new file +21 -0
+# TEST CASE #1
+# setup for veth0 device
+ifconfig veth0 $IP0/24 || \
+	tst_brkm TBROK "adding address to veth0 failed"
+ifconfig veth0 up || \
+	tst_brkm TBROK "enabling veth0 device failed"
+
+# setup for veth1 (which is inside myns namespace)
+ip netns exec myns ifconfig veth1 $IP1/24 || \
+	tst_brkm TBROK "adding address to veth1 failed"
+ip netns exec myns ifconfig veth1 up || \
+	tst_brkm TBROK "enabling veth1 device failed"
+
+ping -q -c 2 -I veth0 $IP1 &>/dev/null
+ret=$?
+failed=0
+if [ $ret -ne 0 ]; then
+	failed=1
+fi
+
+ip netns exec myns ping -q -c 2 -I veth1 $IP0 &>/dev/null

testcases/kernel/containers/netns/netns_devices2.sh line 93

Quoted 10 lines
testcases/kernel/containers/netns/netns_devices2.sh new file +10 -0
+ret=$?
+if [ $ret -ne 0 ]; then
+	failed=1
+fi
+
+if [ $failed -eq 0 ]; then
+	tst_resm TPASS "ioctl configuration and communication over veth devices"
+else
+	tst_resm TFAIL "ioctl configuration and communication over veth devices"
+fi

Here the same applies as well.

Quoted 15 lines
testcases/kernel/containers/netns/netns_devices2.sh new file +15 -0
+# TEST CASE #2
+# enables lo device
+ip netns exec myns ifconfig lo up || \
+	tst_brkm TBROK "enabling lo device failed"
+
+ip netns exec myns ping -q -c 2 -I lo 127.0.0.1 &>/dev/null
+ret=$?
+if [ $ret -eq 0 ]; then
+	tst_resm TPASS "ioctl configuration and communication over lo device"
+else
+	tst_resm TFAIL "ioctl configuration and communication over lo device"
+fi
+
+
+tst_exit
Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#4 lore raw

PATCH v4 1/1 containers: added netns/netns_devices.sh and netns/netns_devices2.sh

In reply to Matus Marhefka

* Tests that a separate network namespace can configure and communicate over the devices it sees
* netns/netns_devices.sh -- uses netlink(7) for configuration
* netns/netns_devices2.sh -- uses ioctl(2) for configuration

---
 runtest/containers                                 |   2 +
 testcases/kernel/containers/netns/netns_devices.sh | 115 +++++++++++++++++++++
 .../kernel/containers/netns/netns_devices2.sh      | 115 +++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100755 testcases/kernel/containers/netns/netns_devices.sh
 create mode 100755 testcases/kernel/containers/netns/netns_devices2.sh
runtest/containers +2 -0
index 7d01a44..69eac82 100644
@@ -29,6 +29,8 @@ netns_crtchild_delchild netns_crtchild_delchild
 netns_par_chld_ipv6 netns_par_chld_ipv6
 netns_par_chld_ftp netns_par_chld_ftp.sh
 netns_netlink netns_netlink
+netns_devices netns_devices.sh
+netns_devices2 netns_devices2.sh
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
testcases/kernel/containers/netns/netns_devices.sh new file +115 -0

115 lines not on this page · raw message

testcases/kernel/containers/netns/netns_devices2.sh new file +115 -0

115 lines not on this page · raw message

Signature · 13 lines
1.8.3.1


------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#5 lore raw

Re: [LTP] [PATCH v4] containers: added netns/netns_devices.sh and netns/netns_devices2.sh

Hi!
Pushed, thanks.

Signature · 8 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#6 lore raw
In reply to Matus Marhefka

Hi!

Quoted 13 lines
testcases/kernel/containers/netns/netns_interfaces.c new file +13 -0
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "usctest.h"
+#include "test.h"
+#include "safe_macros.h"
+#include "netns_helper.h"
+
+
+/* by convention a named network namespace is an object
+ * at /var/run/netns/NAME that can be opened. (man 8 ip-netns) */

testcases/kernel/containers/netns/netns_interfaces.c line 50

Quoted 10 lines
testcases/kernel/containers/netns/netns_interfaces.c new file +10 -0
+#define NETNS     "/var/run/netns"
+char *TCID	= "netns_sendintf";
+int TST_TOTAL	= 3;
+int pipefd[2];
+
+
+static void cleanup(void)
+{
+	close(pipefd[0]);
+	close(pipefd[1]);

This should be ported to the checkpoint interface.

Quoted 79 lines
testcases/kernel/containers/netns/netns_interfaces.c new file +79 -0
+	/* removes veth0 device (which also removes paired veth1 device) */
+	if (WEXITSTATUS(system("ip link delete veth0")))
+		perror("system");
+	/* removes the network namespace myns */
+	if (WEXITSTATUS(system("ip netns del myns")))
+		perror("system");
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_netns();
+}
+
+int child_func(void)
+{
+	int status, fd;
+	char c;
+
+	fd = open(NETNS"/myns", O_RDONLY);
+	if (fd == -1) {
+		perror("open");
+		return 1;
+	}
+
+	/* associates child with the namespace referred by fd (myns) */
+	if (setns(fd, 0) == -1) {
+		perror("setns");
+		return 1;
+	}
+
+	/* setup for veth1 device */
+	if (WEXITSTATUS(system("ip address add 192.168.0.2/24 dev veth1"))) {
+		perror("system");
+		return 1;
+	}
+	if (WEXITSTATUS(system("ip link set dev veth1 up"))) {
+		perror("system");
+		return 1;
+	}
+
+	/* waits for parent to confirm that veth0 device setup is done */
+	if (read(pipefd[0], &c, 1) == -1) {
+		perror("read");
+		return 1;
+	}
+
+	/* ping veth0 address through veth1 device */
+	if (WEXITSTATUS(
+	    system("ping -q -c 2 -I veth1 192.168.0.1 &>/dev/null")))
+		return 1;
+
+	return 0;
+}
+
+static void test(void)
+{
+	pid_t pid;
+	int status, ret = 0;
+
+	/* creates a pipe for synchronization between parent and child */
+	SAFE_PIPE(cleanup, pipefd);
+
+	/* unshares a network and a mount namespace */
+	if (unshare(CLONE_NEWNET|CLONE_NEWNS) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
+
+
+	/* TEST CASE #1 */
+	/* creates a pair of virtual ethernet devices */
+	if (WEXITSTATUS(system("ip link add veth0 type veth peer name veth1")))
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+	/* creates a new network namespace "myns" (man 8 ip-netns) */
+	if (WEXITSTATUS(system("ip netns add myns")))
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+	/* adds device veth1 to myns namespace */
+	if (WEXITSTATUS(system("ip link set veth1 netns myns")))
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+

testcases/kernel/containers/netns/netns_interfaces.c line 140

Quoted 10 lines
testcases/kernel/containers/netns/netns_interfaces.c new file +10 -0
+
+	pid = fork();
+	if (pid < 0) {  /* error */
+		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
+	}
+	if (pid == 0) { /* child */
+		_exit(child_func());
+	}
+
+	/* parent */

And also freed from trivial comments like these.

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://p.sf.net/sfu/Zoho
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

#8 lore raw

Hi!

Quoted 5 lines

consider this patch discarded as it was replaced with newer version:
[PATCH v4] containers: added netns/netns_devices.sh and netns/netns_devices2.sh

the main reason was use of many system() calls in code what looks rather ugly, so I decided to rewrite it into shell script and also extend the test.

Ack.

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://p.sf.net/sfu/Zoho
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list