Skip to content

Commit a303388

Browse files
tohojoAlexei Starovoitov
authored andcommitted
selftests/bpf: Move open_netns() and close_netns() into network_helpers.c
These will also be used by the xdp_do_redirect test being added in the next commit. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20220309105346.100053-5-toke@redhat.com
1 parent 24592ad commit a303388

File tree

3 files changed

+95
-89
lines changed

3 files changed

+95
-89
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
// SPDX-License-Identifier: GPL-2.0-only
2+
#define _GNU_SOURCE
3+
24
#include <errno.h>
35
#include <stdbool.h>
46
#include <stdio.h>
57
#include <string.h>
68
#include <unistd.h>
9+
#include <sched.h>
710

811
#include <arpa/inet.h>
12+
#include <sys/mount.h>
13+
#include <sys/stat.h>
914

1015
#include <linux/err.h>
1116
#include <linux/in.h>
1217
#include <linux/in6.h>
18+
#include <linux/limits.h>
1319

1420
#include "bpf_util.h"
1521
#include "network_helpers.h"
22+
#include "test_progs.h"
1623

1724
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
1825
#define log_err(MSG, ...) ({ \
@@ -356,3 +363,82 @@ char *ping_command(int family)
356363
}
357364
return "ping";
358365
}
366+
367+
struct nstoken {
368+
int orig_netns_fd;
369+
};
370+
371+
static int setns_by_fd(int nsfd)
372+
{
373+
int err;
374+
375+
err = setns(nsfd, CLONE_NEWNET);
376+
close(nsfd);
377+
378+
if (!ASSERT_OK(err, "setns"))
379+
return err;
380+
381+
/* Switch /sys to the new namespace so that e.g. /sys/class/net
382+
* reflects the devices in the new namespace.
383+
*/
384+
err = unshare(CLONE_NEWNS);
385+
if (!ASSERT_OK(err, "unshare"))
386+
return err;
387+
388+
/* Make our /sys mount private, so the following umount won't
389+
* trigger the global umount in case it's shared.
390+
*/
391+
err = mount("none", "/sys", NULL, MS_PRIVATE, NULL);
392+
if (!ASSERT_OK(err, "remount private /sys"))
393+
return err;
394+
395+
err = umount2("/sys", MNT_DETACH);
396+
if (!ASSERT_OK(err, "umount2 /sys"))
397+
return err;
398+
399+
err = mount("sysfs", "/sys", "sysfs", 0, NULL);
400+
if (!ASSERT_OK(err, "mount /sys"))
401+
return err;
402+
403+
err = mount("bpffs", "/sys/fs/bpf", "bpf", 0, NULL);
404+
if (!ASSERT_OK(err, "mount /sys/fs/bpf"))
405+
return err;
406+
407+
return 0;
408+
}
409+
410+
struct nstoken *open_netns(const char *name)
411+
{
412+
int nsfd;
413+
char nspath[PATH_MAX];
414+
int err;
415+
struct nstoken *token;
416+
417+
token = malloc(sizeof(struct nstoken));
418+
if (!ASSERT_OK_PTR(token, "malloc token"))
419+
return NULL;
420+
421+
token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
422+
if (!ASSERT_GE(token->orig_netns_fd, 0, "open /proc/self/ns/net"))
423+
goto fail;
424+
425+
snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
426+
nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
427+
if (!ASSERT_GE(nsfd, 0, "open netns fd"))
428+
goto fail;
429+
430+
err = setns_by_fd(nsfd);
431+
if (!ASSERT_OK(err, "setns_by_fd"))
432+
goto fail;
433+
434+
return token;
435+
fail:
436+
free(token);
437+
return NULL;
438+
}
439+
440+
void close_netns(struct nstoken *token)
441+
{
442+
ASSERT_OK(setns_by_fd(token->orig_netns_fd), "setns_by_fd");
443+
free(token);
444+
}

tools/testing/selftests/bpf/network_helpers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ int make_sockaddr(int family, const char *addr_str, __u16 port,
5555
struct sockaddr_storage *addr, socklen_t *len);
5656
char *ping_command(int family);
5757

58+
struct nstoken;
59+
/**
60+
* open_netns() - Switch to specified network namespace by name.
61+
*
62+
* Returns token with which to restore the original namespace
63+
* using close_netns().
64+
*/
65+
struct nstoken *open_netns(const char *name);
66+
void close_netns(struct nstoken *token);
5867
#endif

tools/testing/selftests/bpf/prog_tests/tc_redirect.c

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@
1010
* to drop unexpected traffic.
1111
*/
1212

13-
#define _GNU_SOURCE
14-
1513
#include <arpa/inet.h>
1614
#include <linux/if.h>
1715
#include <linux/if_tun.h>
1816
#include <linux/limits.h>
1917
#include <linux/sysctl.h>
2018
#include <linux/time_types.h>
2119
#include <linux/net_tstamp.h>
22-
#include <sched.h>
2320
#include <stdbool.h>
2421
#include <stdio.h>
25-
#include <sys/mount.h>
2622
#include <sys/stat.h>
2723
#include <unistd.h>
2824

@@ -92,91 +88,6 @@ static int write_file(const char *path, const char *newval)
9288
return 0;
9389
}
9490

95-
struct nstoken {
96-
int orig_netns_fd;
97-
};
98-
99-
static int setns_by_fd(int nsfd)
100-
{
101-
int err;
102-
103-
err = setns(nsfd, CLONE_NEWNET);
104-
close(nsfd);
105-
106-
if (!ASSERT_OK(err, "setns"))
107-
return err;
108-
109-
/* Switch /sys to the new namespace so that e.g. /sys/class/net
110-
* reflects the devices in the new namespace.
111-
*/
112-
err = unshare(CLONE_NEWNS);
113-
if (!ASSERT_OK(err, "unshare"))
114-
return err;
115-
116-
/* Make our /sys mount private, so the following umount won't
117-
* trigger the global umount in case it's shared.
118-
*/
119-
err = mount("none", "/sys", NULL, MS_PRIVATE, NULL);
120-
if (!ASSERT_OK(err, "remount private /sys"))
121-
return err;
122-
123-
err = umount2("/sys", MNT_DETACH);
124-
if (!ASSERT_OK(err, "umount2 /sys"))
125-
return err;
126-
127-
err = mount("sysfs", "/sys", "sysfs", 0, NULL);
128-
if (!ASSERT_OK(err, "mount /sys"))
129-
return err;
130-
131-
err = mount("bpffs", "/sys/fs/bpf", "bpf", 0, NULL);
132-
if (!ASSERT_OK(err, "mount /sys/fs/bpf"))
133-
return err;
134-
135-
return 0;
136-
}
137-
138-
/**
139-
* open_netns() - Switch to specified network namespace by name.
140-
*
141-
* Returns token with which to restore the original namespace
142-
* using close_netns().
143-
*/
144-
static struct nstoken *open_netns(const char *name)
145-
{
146-
int nsfd;
147-
char nspath[PATH_MAX];
148-
int err;
149-
struct nstoken *token;
150-
151-
token = calloc(1, sizeof(struct nstoken));
152-
if (!ASSERT_OK_PTR(token, "malloc token"))
153-
return NULL;
154-
155-
token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
156-
if (!ASSERT_GE(token->orig_netns_fd, 0, "open /proc/self/ns/net"))
157-
goto fail;
158-
159-
snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
160-
nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
161-
if (!ASSERT_GE(nsfd, 0, "open netns fd"))
162-
goto fail;
163-
164-
err = setns_by_fd(nsfd);
165-
if (!ASSERT_OK(err, "setns_by_fd"))
166-
goto fail;
167-
168-
return token;
169-
fail:
170-
free(token);
171-
return NULL;
172-
}
173-
174-
static void close_netns(struct nstoken *token)
175-
{
176-
ASSERT_OK(setns_by_fd(token->orig_netns_fd), "setns_by_fd");
177-
free(token);
178-
}
179-
18091
static int netns_setup_namespaces(const char *verb)
18192
{
18293
const char * const *ns = namespaces;

0 commit comments

Comments
 (0)