public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] containers: added netns/netns_netlink.c
@ 2014-08-26 12:44 Matus Marhefka
  2014-08-27 11:29 ` [LTP] [PATCH v2] " Matus Marhefka
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Matus Marhefka @ 2014-08-26 12:44 UTC (permalink / raw)
  To: ltp-list

* Tests a netlink interface inside a new network namespace

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                |   1 +
 testcases/kernel/containers/.gitignore            |   1 +
 testcases/kernel/containers/netns/Makefile        |   2 +-
 testcases/kernel/containers/netns/netns_netlink.c | 175 ++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/containers/netns/netns_netlink.c

diff --git a/runtest/containers b/runtest/containers
index 8e8e067..7d01a44 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -28,6 +28,7 @@ netns_two_children_ns netns_two_children_ns
 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
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
index 4a98373..95daf12 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -3,3 +3,4 @@ mountns/mountns01
 mountns/mountns02
 mountns/mountns03
 mountns/mountns04
+netns/netns_netlink
diff --git a/testcases/kernel/containers/netns/Makefile b/testcases/kernel/containers/netns/Makefile
index cdda23b..eea0d88 100644
--- a/testcases/kernel/containers/netns/Makefile
+++ b/testcases/kernel/containers/netns/Makefile
@@ -31,7 +31,7 @@ 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_two_children_ns netns_netlink
 
 $(MAKE_TARGETS): %: common.o %.o
 
diff --git a/testcases/kernel/containers/netns/netns_netlink.c b/testcases/kernel/containers/netns/netns_netlink.c
new file mode 100644
index 0000000..5f5d41f
--- /dev/null
+++ b/testcases/kernel/containers/netns/netns_netlink.c
@@ -0,0 +1,175 @@
+/* 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_netlink.c
+ *
+ * Tests a netlink interface inside a new network namespace.
+ * Description:
+ * 1. Unshares a network namespace (so network related actions
+ *    have no effect on a real system)
+ * 2. Forks a child which creates a NETLINK_ROUTE netlink socket
+ *    and listens to RTMGRP_LINK (network interface create/delete/up/down)
+ *    multicast group.
+ * 4. Child then waits for parent approval to receive data from socket
+ * 3. Parent creates a new TAP interface (dummy0) and immediately
+ *    removes it (which should generate some data in child's netlink socket).
+ *    Then it allows child to continue.
+ * 4. As the child was listening to RTMGRP_LINK multicast group, it should
+ *    detect the new interface creation (by reading data from netlink socket),
+ *    if so, the test passes, otherwise it fails.
+ */
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <asm/types.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "usctest.h"
+#include "test.h"
+#include "safe_macros.h"
+#include "netns_helper.h"
+
+
+char *TCID	= "netns_netlink";
+int TST_TOTAL	= 1;
+int pipefd[2];
+
+
+static void cleanup(void)
+{
+	close(pipefd[0]);
+	close(pipefd[1]);
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_netns();
+}
+
+int child_func(void)
+{
+	int fd, len;
+	struct sockaddr_nl sa;
+	char c, buffer[4096];
+	struct nlmsghdr *nlh;
+
+	/* child will listen to a network interface create/delete/up/down
+	 * events */
+	memset(&sa, 0, sizeof(sa));
+	sa.nl_family = AF_NETLINK;
+	sa.nl_groups = RTMGRP_LINK;
+	
+	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+	if (fd == -1) {
+		perror("socket");
+		return 1;
+	}
+	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
+		perror("bind");
+		return 1;
+	}
+
+	/* waits for parent to create an interface */
+	read(pipefd[0], &c, 1);
+
+	nlh = (struct nlmsghdr *) buffer;
+	/* non-blocking recv call */
+	while ((len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT)) > 0) {
+		/* stop receiving only on interface create event */
+		if (nlh->nlmsg_type == RTM_NEWLINK)
+			break;
+	}
+
+	if (len == -1) {
+		perror("recv");
+		return 1;
+	}
+
+	return 0;
+}
+
+static void test(void)
+{
+	pid_t pid;
+	int status;
+
+	/* creates a pipe for synchronization between parent and child */
+	SAFE_PIPE(cleanup, pipefd);
+
+	/* unshares the network namespace */
+	if (unshare(CLONE_NEWNET) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
+
+	pid = fork();
+	if (pid < 0) {  /* error */
+		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
+	}
+	if (pid == 0) { /* child */
+		_exit(child_func());
+	}
+
+	/* parent */
+	/* creates TAP network interface dummy0 */
+	if (system("ip tuntap add dev dummy0 mode tap") == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* removes previously created dummy0 device */
+	if (system("ip tuntap del mode tap dummy0") == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* allow child to continue */
+	SAFE_WRITE(cleanup, 0, pipefd[1], "0", 1);
+
+
+	SAFE_WAITPID(cleanup, pid, &status, 0);
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+		tst_resm(TFAIL, "netlink interface fail");
+		return;
+	}
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	tst_resm(TPASS, "netlink interface pass");
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		test();
+		cleanup();
+		/* To get rid of "resource temporarily unavalable" errors
+		 * when testing with -i option */
+		sleep(1);
+	}
+
+	tst_exit();
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v2] containers: added netns/netns_netlink.c
  2014-08-26 12:44 [LTP] [PATCH] containers: added netns/netns_netlink.c Matus Marhefka
@ 2014-08-27 11:29 ` Matus Marhefka
  2014-09-24  9:16 ` [LTP] [PATCH] " chrubis
  2014-10-02 14:17 ` [LTP] [PATCH v3] " Matus Marhefka
  2 siblings, 0 replies; 5+ messages in thread
From: Matus Marhefka @ 2014-08-27 11:29 UTC (permalink / raw)
  To: ltp-list

* Tests a netlink interface inside a new network namespace

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                |   1 +
 testcases/kernel/containers/netns/.gitignore      |   1 +
 testcases/kernel/containers/netns/Makefile        |   2 +-
 testcases/kernel/containers/netns/netns_netlink.c | 175 ++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/containers/netns/netns_netlink.c

diff --git a/runtest/containers b/runtest/containers
index 8e8e067..7d01a44 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -28,6 +28,7 @@ netns_two_children_ns netns_two_children_ns
 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
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
diff --git a/testcases/kernel/containers/netns/.gitignore b/testcases/kernel/containers/netns/.gitignore
index e096adc..65f96be 100644
--- a/testcases/kernel/containers/netns/.gitignore
+++ b/testcases/kernel/containers/netns/.gitignore
@@ -5,3 +5,4 @@
 /netns_par_chld_ipv6
 /netns_sysfsview
 /netns_two_children_ns
+/netns_netlink
diff --git a/testcases/kernel/containers/netns/Makefile b/testcases/kernel/containers/netns/Makefile
index cdda23b..eea0d88 100644
--- a/testcases/kernel/containers/netns/Makefile
+++ b/testcases/kernel/containers/netns/Makefile
@@ -31,7 +31,7 @@ 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_two_children_ns netns_netlink
 
 $(MAKE_TARGETS): %: common.o %.o
 
diff --git a/testcases/kernel/containers/netns/netns_netlink.c b/testcases/kernel/containers/netns/netns_netlink.c
new file mode 100644
index 0000000..fbeda4d
--- /dev/null
+++ b/testcases/kernel/containers/netns/netns_netlink.c
@@ -0,0 +1,175 @@
+/* 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_netlink.c
+ *
+ * Tests a netlink interface inside a new network namespace.
+ * Description:
+ * 1. Unshares a network namespace (so network related actions
+ *    have no effect on a real system)
+ * 2. Forks a child which creates a NETLINK_ROUTE netlink socket
+ *    and listens to RTMGRP_LINK (network interface create/delete/up/down)
+ *    multicast group.
+ * 4. Child then waits for parent approval to receive data from socket
+ * 3. Parent creates a new TAP interface (dummy0) and immediately
+ *    removes it (which should generate some data in child's netlink socket).
+ *    Then it allows child to continue.
+ * 4. As the child was listening to RTMGRP_LINK multicast group, it should
+ *    detect the new interface creation (by reading data from netlink socket),
+ *    if so, the test passes, otherwise it fails.
+ */
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <asm/types.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "usctest.h"
+#include "test.h"
+#include "safe_macros.h"
+#include "netns_helper.h"
+
+
+char *TCID	= "netns_netlink";
+int TST_TOTAL	= 1;
+int pipefd[2];
+
+
+static void cleanup(void)
+{
+	close(pipefd[0]);
+	close(pipefd[1]);
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_netns();
+}
+
+int child_func(void)
+{
+	int fd, len;
+	struct sockaddr_nl sa;
+	char c, buffer[4096];
+	struct nlmsghdr *nlh;
+
+	/* child will listen to a network interface create/delete/up/down
+	 * events */
+	memset(&sa, 0, sizeof(sa));
+	sa.nl_family = AF_NETLINK;
+	sa.nl_groups = RTMGRP_LINK;
+	
+	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+	if (fd == -1) {
+		perror("socket");
+		return 1;
+	}
+	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
+		perror("bind");
+		return 1;
+	}
+
+	/* waits for parent to create an interface */
+	read(pipefd[0], &c, 1);
+
+	nlh = (struct nlmsghdr *) buffer;
+	/* non-blocking recv call */
+	while ((len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT)) > 0) {
+		/* stop receiving only on interface create event */
+		if (nlh->nlmsg_type == RTM_NEWLINK)
+			break;
+	}
+
+	if (len == -1) {
+		perror("recv");
+		return 1;
+	}
+
+	return 0;
+}
+
+static void test(void)
+{
+	pid_t pid;
+	int status;
+
+	/* creates a pipe for synchronization between parent and child */
+	SAFE_PIPE(cleanup, pipefd);
+
+	/* unshares the network namespace */
+	if (unshare(CLONE_NEWNET) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
+
+	pid = fork();
+	if (pid < 0) {  /* error */
+		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
+	}
+	if (pid == 0) { /* child */
+		_exit(child_func());
+	}
+
+	/* parent */
+	/* creates TAP network interface dummy0 */
+	if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* removes previously created dummy0 device */
+	if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* allow child to continue */
+	SAFE_WRITE(cleanup, 0, pipefd[1], "0", 1);
+
+
+	SAFE_WAITPID(cleanup, pid, &status, 0);
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+		tst_resm(TFAIL, "netlink interface fail");
+		return;
+	}
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	tst_resm(TPASS, "netlink interface pass");
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		test();
+		cleanup();
+		/* To get rid of "resource temporarily unavalable" errors
+		 * when testing with -i option */
+		sleep(1);
+	}
+
+	tst_exit();
+}
-- 
1.8.3.1


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH] containers: added netns/netns_netlink.c
  2014-08-26 12:44 [LTP] [PATCH] containers: added netns/netns_netlink.c Matus Marhefka
  2014-08-27 11:29 ` [LTP] [PATCH v2] " Matus Marhefka
@ 2014-09-24  9:16 ` chrubis
  2014-10-02 14:17 ` [LTP] [PATCH v3] " Matus Marhefka
  2 siblings, 0 replies; 5+ messages in thread
From: chrubis @ 2014-09-24  9:16 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
> * Tests a netlink interface inside a new network namespace
> 
> Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
> ---
>  runtest/containers                                |   1 +
>  testcases/kernel/containers/.gitignore            |   1 +
>  testcases/kernel/containers/netns/Makefile        |   2 +-
>  testcases/kernel/containers/netns/netns_netlink.c | 175 ++++++++++++++++++++++
>  4 files changed, 178 insertions(+), 1 deletion(-)
>  create mode 100644 testcases/kernel/containers/netns/netns_netlink.c
> 
> diff --git a/runtest/containers b/runtest/containers
> index 8e8e067..7d01a44 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -28,6 +28,7 @@ netns_two_children_ns netns_two_children_ns
>  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
>  
>  shmnstest_none shmnstest none
>  shmnstest_clone shmnstest clone
> diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
> index 4a98373..95daf12 100644
> --- a/testcases/kernel/containers/.gitignore
> +++ b/testcases/kernel/containers/.gitignore
> @@ -3,3 +3,4 @@ mountns/mountns01
>  mountns/mountns02
>  mountns/mountns03
>  mountns/mountns04
> +netns/netns_netlink
> diff --git a/testcases/kernel/containers/netns/Makefile b/testcases/kernel/containers/netns/Makefile
> index cdda23b..eea0d88 100644
> --- a/testcases/kernel/containers/netns/Makefile
> +++ b/testcases/kernel/containers/netns/Makefile
> @@ -31,7 +31,7 @@ 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_two_children_ns netns_netlink
>  
>  $(MAKE_TARGETS): %: common.o %.o
>  
> diff --git a/testcases/kernel/containers/netns/netns_netlink.c b/testcases/kernel/containers/netns/netns_netlink.c
> new file mode 100644
> index 0000000..5f5d41f
> --- /dev/null
> +++ b/testcases/kernel/containers/netns/netns_netlink.c
> @@ -0,0 +1,175 @@
> +/* 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_netlink.c
> + *
> + * Tests a netlink interface inside a new network namespace.
> + * Description:
> + * 1. Unshares a network namespace (so network related actions
> + *    have no effect on a real system)
> + * 2. Forks a child which creates a NETLINK_ROUTE netlink socket
> + *    and listens to RTMGRP_LINK (network interface create/delete/up/down)
> + *    multicast group.
> + * 4. Child then waits for parent approval to receive data from socket
> + * 3. Parent creates a new TAP interface (dummy0) and immediately
> + *    removes it (which should generate some data in child's netlink socket).
> + *    Then it allows child to continue.
> + * 4. As the child was listening to RTMGRP_LINK multicast group, it should
> + *    detect the new interface creation (by reading data from netlink socket),
> + *    if so, the test passes, otherwise it fails.
> + */
> +
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <asm/types.h>
> +#include <sys/socket.h>
> +#include <linux/netlink.h>
> +#include <linux/rtnetlink.h>
> +#include <unistd.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#include "usctest.h"
> +#include "test.h"
> +#include "safe_macros.h"
> +#include "netns_helper.h"
> +
> +
> +char *TCID	= "netns_netlink";
> +int TST_TOTAL	= 1;
> +int pipefd[2];
> +
> +
> +static void cleanup(void)
> +{
> +	close(pipefd[0]);
> +	close(pipefd[1]);
> +}
> +
> +static void setup(void)
> +{
> +	tst_require_root(NULL);
> +	check_netns();
> +}
> +
> +int child_func(void)
> +{
> +	int fd, len;
> +	struct sockaddr_nl sa;
> +	char c, buffer[4096];
> +	struct nlmsghdr *nlh;
> +
> +	/* child will listen to a network interface create/delete/up/down
> +	 * events */
> +	memset(&sa, 0, sizeof(sa));
> +	sa.nl_family = AF_NETLINK;
> +	sa.nl_groups = RTMGRP_LINK;
> +	
> +	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
> +	if (fd == -1) {
> +		perror("socket");
> +		return 1;
> +	}
> +	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
> +		perror("bind");
> +		return 1;
> +	}
> +
> +	/* waits for parent to create an interface */
> +	read(pipefd[0], &c, 1);
> +
> +	nlh = (struct nlmsghdr *) buffer;
> +	/* non-blocking recv call */
> +	while ((len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT)) > 0) {
> +		/* stop receiving only on interface create event */
> +		if (nlh->nlmsg_type == RTM_NEWLINK)
> +			break;
> +	}
> +
> +	if (len == -1) {
> +		perror("recv");
> +		return 1;
> +	}
> +
> +	return 0;
> +}
> +
> +static void test(void)
> +{
> +	pid_t pid;
> +	int status;
> +
> +	/* creates a pipe for synchronization between parent and child */
> +	SAFE_PIPE(cleanup, pipefd);

Use the CHECKPOINT interface instead.

> +	/* unshares the network namespace */
> +	if (unshare(CLONE_NEWNET) == -1)
> +		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
> +
> +	pid = fork();

Use tst_fork() instead.

> +	if (pid < 0) {  /* error */
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
> +	}
> +	if (pid == 0) { /* child */
> +		_exit(child_func());
> +	}
> +
> +	/* parent */

No obvious comments like /* error */, /* child */, /* parent */, etc.
please.

> +	/* creates TAP network interface dummy0 */
> +	if (system("ip tuntap add dev dummy0 mode tap") == -1)
> +		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
> +
> +	/* removes previously created dummy0 device */
> +	if (system("ip tuntap del mode tap dummy0") == -1)
> +		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
> +
> +	/* allow child to continue */
> +	SAFE_WRITE(cleanup, 0, pipefd[1], "0", 1);
> +
> +
> +	SAFE_WAITPID(cleanup, pid, &status, 0);
> +	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
> +		tst_resm(TFAIL, "netlink interface fail");
> +		return;
> +	}
> +	if (WIFSIGNALED(status)) {
> +		tst_resm(TFAIL, "child was killed with signal %s",
> +			 tst_strsig(WTERMSIG(status)));
> +		return;
> +	}
> +
> +	tst_resm(TPASS, "netlink interface pass");
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	const char *msg;
> +	int lc;
> +
> +	msg = parse_opts(argc, argv, NULL, NULL);
> +	if (msg != NULL)
> +		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
> +
> +	setup();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +		test();
> +		cleanup();
> +		/* To get rid of "resource temporarily unavalable" errors
> +		 * when testing with -i option */
> +		sleep(1);

		I do not like this sleep(), moreover looking into the
		child_func() you forget to close the socked fd, is this
		still needed even if the fd is closed there?
> +	}
> +
> +	tst_exit();
> +}

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
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
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v3] containers: added netns/netns_netlink.c
  2014-08-26 12:44 [LTP] [PATCH] containers: added netns/netns_netlink.c Matus Marhefka
  2014-08-27 11:29 ` [LTP] [PATCH v2] " Matus Marhefka
  2014-09-24  9:16 ` [LTP] [PATCH] " chrubis
@ 2014-10-02 14:17 ` Matus Marhefka
  2014-10-15 12:54   ` Cyril Hrubis
  2 siblings, 1 reply; 5+ messages in thread
From: Matus Marhefka @ 2014-10-02 14:17 UTC (permalink / raw)
  To: ltp-list

* Tests a netlink interface inside a new network namespace

Signed-off-by: Matus Marhefka <mmarhefk@redhat.com>
---
 runtest/containers                                |   1 +
 testcases/kernel/containers/netns/.gitignore      |   1 +
 testcases/kernel/containers/netns/Makefile        |   2 +-
 testcases/kernel/containers/netns/netns_netlink.c | 184 ++++++++++++++++++++++
 4 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/containers/netns/netns_netlink.c

diff --git a/runtest/containers b/runtest/containers
index 8e8e067..7d01a44 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -28,6 +28,7 @@ netns_two_children_ns netns_two_children_ns
 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
 
 shmnstest_none shmnstest none
 shmnstest_clone shmnstest clone
diff --git a/testcases/kernel/containers/netns/.gitignore b/testcases/kernel/containers/netns/.gitignore
index e096adc..65f96be 100644
--- a/testcases/kernel/containers/netns/.gitignore
+++ b/testcases/kernel/containers/netns/.gitignore
@@ -5,3 +5,4 @@
 /netns_par_chld_ipv6
 /netns_sysfsview
 /netns_two_children_ns
+/netns_netlink
diff --git a/testcases/kernel/containers/netns/Makefile b/testcases/kernel/containers/netns/Makefile
index cdda23b..eea0d88 100644
--- a/testcases/kernel/containers/netns/Makefile
+++ b/testcases/kernel/containers/netns/Makefile
@@ -31,7 +31,7 @@ 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_two_children_ns netns_netlink
 
 $(MAKE_TARGETS): %: common.o %.o
 
diff --git a/testcases/kernel/containers/netns/netns_netlink.c b/testcases/kernel/containers/netns/netns_netlink.c
new file mode 100644
index 0000000..b365a9a
--- /dev/null
+++ b/testcases/kernel/containers/netns/netns_netlink.c
@@ -0,0 +1,184 @@
+/* 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_netlink.c
+ *
+ * Tests a netlink interface inside a new network namespace.
+ * Description:
+ * 1. Unshares a network namespace (so network related actions
+ *    have no effect on a real system)
+ * 2. Forks a child which creates a NETLINK_ROUTE netlink socket
+ *    and listens to RTMGRP_LINK (network interface create/delete/up/down)
+ *    multicast group.
+ * 4. Child then waits for parent approval to receive data from socket
+ * 3. Parent creates a new TAP interface (dummy0) and immediately
+ *    removes it (which should generate some data in child's netlink socket).
+ *    Then it allows child to continue.
+ * 4. As the child was listening to RTMGRP_LINK multicast group, it should
+ *    detect the new interface creation/deletion (by reading data from netlink
+ *    socket), if so, the test passes, otherwise it fails.
+ */
+
+#define _GNU_SOURCE
+#include <sys/wait.h>
+#include <asm/types.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "usctest.h"
+#include "test.h"
+#include "safe_macros.h"
+#include "netns_helper.h"
+
+
+#define MAX_TRIES 1000
+char *TCID	= "netns_netlink";
+int TST_TOTAL	= 1;
+struct tst_checkpoint checkpoint;
+
+
+static void cleanup(void)
+{
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_require_root(NULL);
+	check_netns();
+	tst_tmpdir();
+	TST_CHECKPOINT_INIT(&checkpoint);
+}
+
+int child_func(void)
+{
+	int fd, len, event_found, tries;
+	struct sockaddr_nl sa;
+	char buffer[4096];
+	struct nlmsghdr *nlh;
+
+	/* child will listen to a network interface create/delete/up/down
+	 * events */
+	memset(&sa, 0, sizeof(sa));
+	sa.nl_family = AF_NETLINK;
+	sa.nl_groups = RTMGRP_LINK;
+	
+	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+	if (fd == -1) {
+		perror("socket");
+		return 1;
+	}
+	if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
+		perror("bind");
+		close(fd);
+		return 1;
+	}
+
+	/* waits for parent to create an interface */
+	TST_CHECKPOINT_CHILD_WAIT(&checkpoint);
+
+	/* To get rid of "resource temporarily unavailable" errors
+	 * when testing with -i option */
+	tries = 0;
+	event_found = 0;
+	nlh = (struct nlmsghdr *) buffer;
+	while (tries < MAX_TRIES) {
+		len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT);
+		if (len > 0) {
+			/* stop receiving only on interface create/delete
+			 * event */
+			if (nlh->nlmsg_type == RTM_NEWLINK ||
+			    nlh->nlmsg_type == RTM_DELLINK) {
+				event_found++;
+				break;
+			}
+		}
+		usleep(10000);
+		tries++;
+	}
+
+	close(fd);
+
+	if (!event_found) {
+		perror("recv");
+		return 1;
+	}
+
+	return 0;
+}
+
+static void test(void)
+{
+	pid_t pid;
+	int status;
+
+	/* unshares the network namespace */
+	if (unshare(CLONE_NEWNET) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
+
+	pid = tst_fork();
+	if (pid < 0) {
+		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
+	}
+	if (pid == 0) {
+		_exit(child_func());
+	}
+
+	/* creates TAP network interface dummy0 */
+	if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* removes previously created dummy0 device */
+	if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup, "system failed");
+
+	/* allow child to continue */
+	TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);
+
+
+	SAFE_WAITPID(cleanup, pid, &status, 0);
+	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
+		tst_resm(TFAIL, "netlink interface fail");
+		return;
+	}
+	if (WIFSIGNALED(status)) {
+		tst_resm(TFAIL, "child was killed with signal %s",
+			 tst_strsig(WTERMSIG(status)));
+		return;
+	}
+
+	tst_resm(TPASS, "netlink interface pass");
+}
+
+int main(int argc, char *argv[])
+{
+	const char *msg;
+	int lc;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++)
+		test();
+
+	cleanup();
+	tst_exit();
+}
-- 
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
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v3] containers: added netns/netns_netlink.c
  2014-10-02 14:17 ` [LTP] [PATCH v3] " Matus Marhefka
@ 2014-10-15 12:54   ` Cyril Hrubis
  0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2014-10-15 12:54 UTC (permalink / raw)
  To: Matus Marhefka; +Cc: ltp-list

Hi!
> * Tests a netlink interface inside a new network namespace

Pushed with a change from TST_CHECKPOINT_INIT to TST_CHECKPOINT_CREATE
in the setup to match changes in the checkpoint interface, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
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
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-10-15 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-26 12:44 [LTP] [PATCH] containers: added netns/netns_netlink.c Matus Marhefka
2014-08-27 11:29 ` [LTP] [PATCH v2] " Matus Marhefka
2014-09-24  9:16 ` [LTP] [PATCH] " chrubis
2014-10-02 14:17 ` [LTP] [PATCH v3] " Matus Marhefka
2014-10-15 12:54   ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox