Skip to content

Commit

Permalink
Merge branch 'use network helpers, part 2'
Browse files Browse the repository at this point in the history
Geliang Tang says:

====================
This patchset uses more network helpers in test_sock_addr.c, but
first of all, patch 2 is needed to make network_helpers.c independent
of test_progs.c. Then network_helpers.h can be included into
test_sock_addr.c without compile errors.

Patch 1 and patch 2 address Martin's comments for the previous series
too.

v2:
 - Only a few minor cleanups to patch 5.
====================

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
  • Loading branch information
Martin KaFai Lau committed Apr 24, 2024
2 parents 55d30cc + e4c68bb commit 5305b37
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 127 deletions.
3 changes: 2 additions & 1 deletion tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,12 @@ UNPRIV_HELPERS := $(OUTPUT)/unpriv_helpers.o
TRACE_HELPERS := $(OUTPUT)/trace_helpers.o
JSON_WRITER := $(OUTPUT)/json_writer.o
CAP_HELPERS := $(OUTPUT)/cap_helpers.o
NETWORK_HELPERS := $(OUTPUT)/network_helpers.o

$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(NETWORK_HELPERS)
$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
Expand Down
21 changes: 16 additions & 5 deletions tools/testing/selftests/bpf/network_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,25 +459,35 @@ struct nstoken *open_netns(const char *name)
struct nstoken *token;

token = calloc(1, sizeof(struct nstoken));
if (!ASSERT_OK_PTR(token, "malloc token"))
if (!token) {
log_err("Failed to malloc token");
return NULL;
}

token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
if (!ASSERT_GE(token->orig_netns_fd, 0, "open /proc/self/ns/net"))
if (token->orig_netns_fd == -1) {
log_err("Failed to open(/proc/self/ns/net)");
goto fail;
}

snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
if (!ASSERT_GE(nsfd, 0, "open netns fd"))
if (nsfd == -1) {
log_err("Failed to open(%s)", nspath);
goto fail;
}

err = setns(nsfd, CLONE_NEWNET);
close(nsfd);
if (!ASSERT_OK(err, "setns"))
if (err) {
log_err("Failed to setns(nsfd)");
goto fail;
}

return token;
fail:
if (token->orig_netns_fd != -1)
close(token->orig_netns_fd);
free(token);
return NULL;
}
Expand All @@ -487,7 +497,8 @@ void close_netns(struct nstoken *token)
if (!token)
return;

ASSERT_OK(setns(token->orig_netns_fd, CLONE_NEWNET), "setns");
if (setns(token->orig_netns_fd, CLONE_NEWNET))
log_err("Failed to setns(orig_netns_fd)");
close(token->orig_netns_fd);
free(token);
}
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/network_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef __u16 __sum16;
#include <linux/ipv6.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <linux/err.h>
#include <netinet/tcp.h>
#include <bpf/bpf_endian.h>
#include <net/if.h>
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/empty_skb.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ void test_empty_skb(void)

SYS(out, "ip netns add empty_skb");
tok = open_netns("empty_skb");
if (!ASSERT_OK_PTR(tok, "setns"))
goto out;
SYS(out, "ip link add veth0 type veth peer veth1");
SYS(out, "ip link set dev veth0 up");
SYS(out, "ip link set dev veth1 up");
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static int attach(struct ip_check_defrag *skel, bool ipv6)
int err = -1;

nstoken = open_netns(NS1);
if (!ASSERT_OK_PTR(nstoken, "setns"))
goto out;

skel->links.defrag = bpf_program__attach_netfilter(skel->progs.defrag, &opts);
if (!ASSERT_OK_PTR(skel->links.defrag, "program attach"))
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/tc_redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ static int wait_netstamp_needed_key(void)
__u64 tstamp = 0;

nstoken = open_netns(NS_DST);
if (!nstoken)
if (!ASSERT_OK_PTR(nstoken, "setns dst"))
return -1;

srv_fd = start_server(AF_INET6, SOCK_DGRAM, "::1", 0, 0);
Expand Down
4 changes: 4 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/test_tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ static void test_ipip_tunnel(enum ipip_encap encap)

/* ping from at_ns0 namespace test */
nstoken = open_netns("at_ns0");
if (!ASSERT_OK_PTR(nstoken, "setns"))
goto done;
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
if (!ASSERT_OK(err, "test_ping"))
goto done;
Expand Down Expand Up @@ -666,6 +668,8 @@ static void test_xfrm_tunnel(void)

/* ping from at_ns0 namespace test */
nstoken = open_netns("at_ns0");
if (!ASSERT_OK_PTR(nstoken, "setns"))
goto done;
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
close_netns(nstoken);
if (!ASSERT_OK(err, "test_ping"))
Expand Down
16 changes: 16 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ void test_xdp_metadata(void)
SYS(out, "ip netns add " RX_NETNS_NAME);

tok = open_netns(TX_NETNS_NAME);
if (!ASSERT_OK_PTR(tok, "setns"))
goto out;
SYS(out, "ip link add numtxqueues 1 numrxqueues 1 " TX_NAME
" type veth peer " RX_NAME " numtxqueues 1 numrxqueues 1");
SYS(out, "ip link set " RX_NAME " netns " RX_NETNS_NAME);
Expand All @@ -400,6 +402,8 @@ void test_xdp_metadata(void)
SYS(out, "ip -4 neigh add " RX_ADDR " lladdr " RX_MAC " dev " TX_NAME_VLAN);

switch_ns_to_rx(&tok);
if (!ASSERT_OK_PTR(tok, "setns rx"))
goto out;

SYS(out, "ip link set dev " RX_NAME " address " RX_MAC);
SYS(out, "ip link set dev " RX_NAME " up");
Expand Down Expand Up @@ -449,6 +453,8 @@ void test_xdp_metadata(void)
goto out;

switch_ns_to_tx(&tok);
if (!ASSERT_OK_PTR(tok, "setns tx"))
goto out;

/* Setup separate AF_XDP for TX interface nad send packet to the RX socket. */
tx_ifindex = if_nametoindex(TX_NAME);
Expand All @@ -461,20 +467,26 @@ void test_xdp_metadata(void)
goto out;

switch_ns_to_rx(&tok);
if (!ASSERT_OK_PTR(tok, "setns rx"))
goto out;

/* Verify packet sent from AF_XDP has proper metadata. */
if (!ASSERT_GE(verify_xsk_metadata(&rx_xsk, true), 0,
"verify_xsk_metadata"))
goto out;

switch_ns_to_tx(&tok);
if (!ASSERT_OK_PTR(tok, "setns tx"))
goto out;
complete_tx(&tx_xsk);

/* Now check metadata of packet, generated with network stack */
if (!ASSERT_GE(generate_packet_inet(), 0, "generate UDP packet"))
goto out;

switch_ns_to_rx(&tok);
if (!ASSERT_OK_PTR(tok, "setns rx"))
goto out;

if (!ASSERT_GE(verify_xsk_metadata(&rx_xsk, false), 0,
"verify_xsk_metadata"))
Expand All @@ -498,13 +510,17 @@ void test_xdp_metadata(void)
goto out;

switch_ns_to_tx(&tok);
if (!ASSERT_OK_PTR(tok, "setns tx"))
goto out;

/* Send packet to trigger . */
if (!ASSERT_GE(generate_packet(&tx_xsk, AF_XDP_CONSUMER_PORT), 0,
"generate freplace packet"))
goto out;

switch_ns_to_rx(&tok);
if (!ASSERT_OK_PTR(tok, "setns rx"))
goto out;

while (!retries--) {
if (bpf_obj2->bss->called)
Expand Down
Loading

0 comments on commit 5305b37

Please sign in to comment.