Skip to content

Commit

Permalink
selftests/bpf: Use netns helpers in lwt tests
Browse files Browse the repository at this point in the history
This patch uses netns helpers create_netns() and cleanup_netns() in
lwt_helpers.h instead of using the local function netns_create() and
netns_delete().

For using these helpers. network_helpers.h needs to be included in
lwt_helpers.h. Then '#include "network_helpers.h"' in lwt_redirect.c
and lwt_reroute.c can be dropped.

Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang committed Oct 12, 2024
1 parent 7026bdd commit 6951dc7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
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,34 +9,22 @@
#include <linux/icmp.h>

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

#define log_err(MSG, ...) \
fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
__FILE__, __LINE__, strerror(errno), ##__VA_ARGS__)

#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 = create_netns(NETNS); \
if (ASSERT_OK_PTR(token, "setns")) \
test_ ## name(); \
cleanup_netns(token); \
} \
})

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
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +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 @@ -307,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 @@ -51,7 +51,6 @@
#define NETNS "ns_lwt_reroute"
#include <netinet/in.h>
#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 @@ -243,7 +242,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

0 comments on commit 6951dc7

Please sign in to comment.