Skip to content

Commit

Permalink
selftests/bpf: ns create
Browse files Browse the repository at this point in the history
selfetsts/bpf: Use ns helpers in assign_reuse
selfetsts/bpf: Use ns helpers in test_tunnel
selfetsts/bpf: Use ns helpers in ns_current_pid_tgid

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
  • Loading branch information
Geliang Tang committed May 13, 2024
1 parent 12aeb52 commit d9658a0
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 109 deletions.
25 changes: 25 additions & 0 deletions tools/testing/selftests/bpf/network_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,28 @@ int unshare_netns(void)

return 0;
}

void del_netns(struct nstoken *token, const char *name)
{
if (token)
close_netns(token);

if (SYS_NOFAIL("ip netns del %s", name))
log_err("del netns %s failed", name);
}

struct nstoken *add_netns(const char *name)
{
if (SYS_NOFAIL("ip netns add %s", name)) {
log_err("add netns %s failed", name);
return NULL;
}

if (SYS_NOFAIL("ip -net %s link set dev lo up", name)) {
log_err("set dev lo up failed");
SYS_NOFAIL("ip netns del %s", name);
return NULL;
}

return open_netns(name);
}
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/network_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ struct nstoken *open_netns(const char *name);
void close_netns(struct nstoken *token);
int send_recv_data(int lfd, int fd, uint32_t total_bytes);
int unshare_netns(void);
void del_netns(struct nstoken *token, const char *name);
struct nstoken *add_netns(const char *name);

static __u16 csum_fold(__u32 csum)
{
Expand Down
12 changes: 3 additions & 9 deletions tools/testing/selftests/bpf/prog_tests/assign_reuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,9 @@ void test_assign_reuse(void)
{
struct nstoken *tok = NULL;

SYS(out, "ip netns add %s", NS_TEST);
SYS(cleanup, "ip -net %s link set dev lo up", NS_TEST);

tok = open_netns(NS_TEST);
tok = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(tok, "netns token"))
return;
goto cleanup;

if (test__start_subtest("tcpv4"))
run_assign_reuse(AF_INET, SOCK_STREAM, "127.0.0.1", PORT);
Expand All @@ -192,8 +189,5 @@ void test_assign_reuse(void)
run_assign_reuse(AF_INET6, SOCK_DGRAM, "::1", PORT);

cleanup:
close_netns(tok);
SYS_NOFAIL("ip netns delete %s", NS_TEST);
out:
return;
del_netns(tok, NS_TEST);
}
3 changes: 1 addition & 2 deletions tools/testing/selftests/bpf/prog_tests/crypto_sanity.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ void test_crypto_sanity(void)
ASSERT_OK(err, "bpf_tc_detach decrypt");

fail:
close_netns(nstoken);
del_netns(nstoken, NS_TEST);
deinit_afalg();
SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null");
crypto_sanity__destroy(skel);
}
6 changes: 2 additions & 4 deletions tools/testing/selftests/bpf/prog_tests/decap_sanity.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ void test_decap_sanity(void)
ASSERT_FALSE(skel->bss->broken_csum_start, "broken_csum_start");

fail:
if (nstoken) {
if (nstoken)
bpf_tc_hook_destroy(&qdisc_hook);
close_netns(nstoken);
}
SYS_NOFAIL("ip netns del " NS_TEST);
del_netns(nstoken, NS_TEST);
decap_sanity__destroy(skel);
}
8 changes: 2 additions & 6 deletions tools/testing/selftests/bpf/prog_tests/fib_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ void test_fib_lookup(void)
return;
prog_fd = bpf_program__fd(skel->progs.fib_lookup);

SYS(fail, "ip netns add %s", NS_TEST);

nstoken = open_netns(NS_TEST);
nstoken = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(nstoken, "open_netns"))
goto fail;

Expand Down Expand Up @@ -370,8 +368,6 @@ void test_fib_lookup(void)
}

fail:
if (nstoken)
close_netns(nstoken);
SYS_NOFAIL("ip netns del " NS_TEST);
del_netns(nstoken, NS_TEST);
fib_lookup__destroy(skel);
}
26 changes: 7 additions & 19 deletions tools/testing/selftests/bpf/prog_tests/lwt_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,18 @@
#include <linux/icmp.h>

#include "test_progs.h"
#include "network_helpers.h"

#define RUN_TEST(name) \
({ \
if (test__start_subtest(#name)) \
if (ASSERT_OK(netns_create(), "netns_create")) { \
struct nstoken *token = open_netns(NETNS); \
if (ASSERT_OK_PTR(token, "setns")) { \
test_ ## name(); \
close_netns(token); \
} \
netns_delete(); \
} \
if (test__start_subtest(#name)) { \
struct nstoken *token = add_netns(NETNS); \
if (ASSERT_OK_PTR(token, "setns")) \
test_ ## name(); \
del_netns(token, NETNS); \
} \
})

static inline int netns_create(void)
{
return system("ip netns add " NETNS);
}

static inline int netns_delete(void)
{
return system("ip netns del " NETNS ">/dev/null 2>&1");
}

static int open_tuntap(const char *dev_name, bool need_mac)
{
int err = 0;
Expand Down
3 changes: 0 additions & 3 deletions tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@

#define NETNS "ns_lwt_redirect"
#include "lwt_helpers.h"
#include "test_progs.h"
#include "network_helpers.h"

#define BPF_OBJECT "test_lwt_redirect.bpf.o"
#define INGRESS_SEC(need_mac) ((need_mac) ? "redir_ingress" : "redir_ingress_nomac")
Expand Down Expand Up @@ -308,7 +306,6 @@ static void test_lwt_redirect_dev_carrier_down(void)

static void *test_lwt_redirect_run(void *arg)
{
netns_delete();
RUN_TEST(lwt_redirect_normal);
RUN_TEST(lwt_redirect_normal_nomac);
RUN_TEST(lwt_redirect_dev_down);
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/lwt_reroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
*/
#define NETNS "ns_lwt_reroute"
#include "lwt_helpers.h"
#include "network_helpers.h"
#include <linux/net_tstamp.h>

#define BPF_OBJECT "test_lwt_reroute.bpf.o"
Expand Down Expand Up @@ -242,7 +241,6 @@ static void test_lwt_reroute_qdisc_dropped(void)

static void *test_lwt_reroute_run(void *arg)
{
netns_delete();
RUN_TEST(lwt_reroute_normal_xmit);
RUN_TEST(lwt_reroute_qdisc_dropped);
return NULL;
Expand Down
38 changes: 10 additions & 28 deletions tools/testing/selftests/bpf/prog_tests/mptcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ struct mptcp_storage {
char ca_name[TCP_CA_NAME_MAX];
};

static struct nstoken *create_netns(void)
{
SYS(fail, "ip netns add %s", NS_TEST);
SYS(fail, "ip -net %s link set dev lo up", NS_TEST);

return open_netns(NS_TEST);
fail:
return NULL;
}

static void cleanup_netns(struct nstoken *nstoken)
{
if (nstoken)
close_netns(nstoken);

SYS_NOFAIL("ip netns del %s", NS_TEST);
}

static int start_mptcp_server(int family, const char *addr_str, __u16 port,
int timeout_ms)
{
Expand Down Expand Up @@ -229,7 +211,7 @@ static void test_base(void)
if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
return;

nstoken = create_netns();
nstoken = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(nstoken, "create_netns"))
goto fail;

Expand All @@ -253,7 +235,7 @@ static void test_base(void)
close(server_fd);

fail:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
close(cgroup_fd);
}

Expand Down Expand Up @@ -345,14 +327,14 @@ static void test_mptcpify(void)
if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
return;

nstoken = create_netns();
nstoken = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(nstoken, "create_netns"))
goto fail;

ASSERT_OK(run_mptcpify(cgroup_fd), "run_mptcpify");

fail:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
close(cgroup_fd);
}

Expand Down Expand Up @@ -437,7 +419,7 @@ static void test_subflow(void)
if (!ASSERT_OK(err, "prog_attach"))
goto skel_destroy;

nstoken = create_netns();
nstoken = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(nstoken, "create_netns: mptcp_subflow"))
goto skel_destroy;

Expand All @@ -447,7 +429,7 @@ static void test_subflow(void)
run_subflow(skel->data->cc);

close_netns:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
skel_destroy:
mptcp_subflow__destroy(skel);
close_cgroup:
Expand All @@ -458,7 +440,7 @@ static struct nstoken *sched_init(char *flags, char *sched)
{
struct nstoken *nstoken;

nstoken = create_netns();
nstoken = add_netns(NS_TEST);
if (!ASSERT_OK_PTR(nstoken, "create_netns"))
return NULL;

Expand All @@ -469,7 +451,7 @@ static struct nstoken *sched_init(char *flags, char *sched)

return nstoken;
fail:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
return NULL;
}

Expand Down Expand Up @@ -530,7 +512,7 @@ static void test_default(void)
send_data_and_verify("default", WITH_DATA, WITH_DATA);

fail:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
}

static void test_bpf_sched(struct bpf_object *obj, char *sched,
Expand All @@ -557,7 +539,7 @@ static void test_bpf_sched(struct bpf_object *obj, char *sched,
send_data_and_verify(sched, addr1, addr2);

fail:
cleanup_netns(nstoken);
del_netns(nstoken, NS_TEST);
bpf_link__destroy(link);
}

Expand Down
9 changes: 2 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,14 @@ static void test_in_netns(int (*fn)(void *), void *arg)
{
struct nstoken *nstoken = NULL;

SYS(cleanup, "ip netns add ns_current_pid_tgid");
SYS(cleanup, "ip -net ns_current_pid_tgid link set dev lo up");

nstoken = open_netns("ns_current_pid_tgid");
nstoken = add_netns("ns_current_pid_tgid");
if (!ASSERT_OK_PTR(nstoken, "open_netns"))
goto cleanup;

test_ns_current_pid_tgid_new_ns(fn, arg);

cleanup:
if (nstoken)
close_netns(nstoken);
SYS_NOFAIL("ip netns del ns_current_pid_tgid");
del_netns(nstoken, "ns_current_pid_tgid");
}

/* TODO: use a different tracepoint */
Expand Down
9 changes: 2 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/sock_destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ void test_sock_destroy(void)
if (!ASSERT_OK_PTR(skel->links.sock_connect, "prog_attach"))
goto cleanup;

SYS(cleanup, "ip netns add %s", TEST_NS);
SYS(cleanup, "ip -net %s link set dev lo up", TEST_NS);

nstoken = open_netns(TEST_NS);
nstoken = add_netns(TEST_NS);
if (!ASSERT_OK_PTR(nstoken, "open_netns"))
goto cleanup;

Expand All @@ -212,9 +209,7 @@ void test_sock_destroy(void)
RUN_TESTS(sock_destroy_prog_fail);

cleanup:
if (nstoken)
close_netns(nstoken);
SYS_NOFAIL("ip netns del " TEST_NS);
del_netns(nstoken, TEST_NS);
if (cgroup_fd >= 0)
close(cgroup_fd);
sock_destroy_prog__destroy(skel);
Expand Down
9 changes: 2 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ void test_sock_iter_batch(void)
{
struct nstoken *nstoken = NULL;

SYS_NOFAIL("ip netns del " TEST_NS);
SYS(done, "ip netns add %s", TEST_NS);
SYS(done, "ip -net %s link set dev lo up", TEST_NS);

nstoken = open_netns(TEST_NS);
nstoken = add_netns(TEST_NS);
if (!ASSERT_OK_PTR(nstoken, "open_netns"))
goto done;

Expand All @@ -128,8 +124,7 @@ void test_sock_iter_batch(void)
do_test(SOCK_DGRAM, true);
do_test(SOCK_DGRAM, false);
}
close_netns(nstoken);

done:
SYS_NOFAIL("ip netns del " TEST_NS);
del_netns(nstoken, TEST_NS);
}
Loading

0 comments on commit d9658a0

Please sign in to comment.