[12/12] test: check veth devices from two network namespaces

Submitted by Andrei Vagin on Feb. 28, 2017, 11:53 p.m.

Details

Message ID 1488325988-28456-13-git-send-email-avagin@openvz.org
State New
Series "Dump and restore internal veth devices"
Headers show

Commit Message

Andrei Vagin Feb. 28, 2017, 11:53 p.m.
From: Andrei Vagin <avagin@virtuozzo.com>

We shave a test case for external veth devices. This test case
checks veth devices which are living in two dumped network
namespaces.

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 test/zdtm/static/Makefile            |   2 +
 test/zdtm/static/netns_sub_veth.c    | 117 +++++++++++++++++++++++++++++++++++
 test/zdtm/static/netns_sub_veth.desc |   6 ++
 3 files changed, 125 insertions(+)
 create mode 100644 test/zdtm/static/netns_sub_veth.c
 create mode 100644 test/zdtm/static/netns_sub_veth.desc

Patch hide | download patch | download mbox

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index b4d8134..681de00 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -177,6 +177,7 @@  TST_NOFILE	:=				\
 		netns_sub			\
 		userns00			\
 		userns01			\
+		netns_sub_veth			\
 #		jobctl00			\
 
 ifneq ($(SRCARCH),arm)
@@ -445,6 +446,7 @@  stopped01:	override CFLAGS += -DZDTM_STOPPED_KILL
 stopped02:	override CFLAGS += -DZDTM_STOPPED_TKILL
 stopped12:	override CFLAGS += -DZDTM_STOPPED_KILL -DZDTM_STOPPED_TKILL
 clone_fs:		override LDLIBS += -pthread
+netns_sub_veth:		override LDLIBS += -lnl-3 -l nl-route-3 -I/usr/include/libnl3
 
 socket-tcp-fin-wait1:	override CFLAGS += -D ZDTM_TCP_FIN_WAIT1
 socket-tcp-fin-wait2:	override CFLAGS += -D ZDTM_TCP_FIN_WAIT2
diff --git a/test/zdtm/static/netns_sub_veth.c b/test/zdtm/static/netns_sub_veth.c
new file mode 100644
index 0000000..9537121
--- /dev/null
+++ b/test/zdtm/static/netns_sub_veth.c
@@ -0,0 +1,117 @@ 
+#define _GNU_SOURCE
+#include <sched.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <linux/if.h>
+
+#include <netinet/ether.h>
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Check dump and restore a few network namespaces";
+
+int main(int argc, char **argv)
+{
+	task_waiter_t lock;
+	pid_t pid[2];
+	int status = -1, ret, i;
+        struct rtnl_link *link = NULL, *new;
+	struct nl_sock *sk;
+	int has_index = 1;
+
+	test_init(argc, argv);
+	task_waiter_init(&lock);
+
+	for (i = 0; i < 2; i++) {
+		pid[i] = fork();
+		if (pid[i] < 0) {
+			pr_perror("fork");
+			return -1;
+		}
+		if (pid[i] == 0) {
+			unshare(CLONE_NEWNET);
+
+			task_waiter_complete(&lock, i);
+			test_waitsig();
+
+			return 0;
+		}
+		task_waiter_wait4(&lock, i);
+	}
+
+	sk = nl_socket_alloc();
+	if (sk == NULL)
+		return -1;
+
+	ret = nl_connect(sk, NETLINK_ROUTE);
+	if (ret < 0) {
+		nl_socket_free(sk);
+		pr_err("Unable to connect socket: %s", nl_geterror(ret));
+		return -1;
+	}
+
+	if (system("ip link add name zdtmbr type bridge"))
+		return -1;
+
+	for (i = 0; i < 2; i++) {
+		char cmd[4096];
+
+		snprintf(cmd, sizeof(cmd), "ip link add name zdtm%d index %d netns %d type veth peer name zdtm%d index %d",
+				i, i * 10 + 12, pid[i], i, i * 10 + 12);
+		if (system(cmd)) {
+			has_index = 0;
+			snprintf(cmd, sizeof(cmd), "ip link add name zdtm%d netns %d type veth peer name zdtm%d", i, pid[i], i);
+			if (system(cmd))
+				return 1;
+		}
+		snprintf(cmd, sizeof(cmd), "ip link set dev zdtm%d master zdtmbr", i);
+		if (system(cmd))
+			return 1;
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	for (i = 0; i < 2; i++) {
+		link = rtnl_link_alloc();
+		new = rtnl_link_alloc();
+		if (has_index)
+			rtnl_link_set_ifindex(link, i * 10 + 12);
+		else {
+			char name[43];
+			snprintf(name, sizeof(name), "zdtm%d", i);
+			rtnl_link_set_name(link, name);
+			rtnl_link_set_name(new, name);
+		}
+		rtnl_link_set_flags(new, IFF_UP);
+		ret = rtnl_link_change(sk, link, new, 0);
+		if (ret) {
+			fail("Unable to up the link: %s", nl_geterror(ret));
+			return 1;
+		}
+	}
+
+	for (i = 0; i < 2; i++) {
+		kill(pid[i], SIGTERM);
+		waitpid(pid[i], &status, 0);
+		if (status) {
+			fail();
+			return 1;
+		}
+	}
+
+	pass();
+	return 0;
+}
diff --git a/test/zdtm/static/netns_sub_veth.desc b/test/zdtm/static/netns_sub_veth.desc
new file mode 100644
index 0000000..ea9e15c
--- /dev/null
+++ b/test/zdtm/static/netns_sub_veth.desc
@@ -0,0 +1,6 @@ 
+{
+	'deps': ['/sbin/ip', '/bin/sh'],
+	'flags': 'suid',
+	'flavor': 'ns uns',
+	'feature': 'link_nsid',
+}