Skip to content

Commit

Permalink
Merge branch 'bpf-next/master' into for-next
Browse files Browse the repository at this point in the history
Signed-off-by: Martin KaFai Lau <[email protected]>
  • Loading branch information
Martin KaFai Lau committed Aug 15, 2024
2 parents 5cbe27f + b97ce54 commit 54d8ab0
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 647 deletions.
3 changes: 0 additions & 3 deletions tools/testing/selftests/bpf/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ test_sock
urandom_read
test_sockmap
test_lirc_mode2_user
get_cgroup_id_user
test_skb_cgroup_id_user
test_cgroup_storage
test_flow_dissector
flow_dissector_load
test_tcpnotify_user
Expand Down
8 changes: 2 additions & 6 deletions tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ endif

# Order correspond to 'make run_tests' order
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
test_sock test_sockmap get_cgroup_id_user \
test_cgroup_storage \
test_sock test_sockmap \
test_tcpnotify_user test_sysctl \
test_progs-no_alu32
TEST_INST_SUBDIRS := no_alu32
Expand Down Expand Up @@ -138,7 +137,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
test_xdp_vlan.sh test_bpftool.py

# Compile but not part of 'make run_tests'
TEST_GEN_PROGS_EXTENDED = test_skb_cgroup_id_user \
TEST_GEN_PROGS_EXTENDED = \
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
Expand Down Expand Up @@ -291,12 +290,9 @@ JSON_WRITER := $(OUTPUT)/json_writer.o
CAP_HELPERS := $(OUTPUT)/cap_helpers.o
NETWORK_HELPERS := $(OUTPUT)/network_helpers.o

$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_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)
$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
$(OUTPUT)/test_tag: $(TESTING_HELPERS)
Expand Down
151 changes: 0 additions & 151 deletions tools/testing/selftests/bpf/get_cgroup_id_user.c

This file was deleted.

141 changes: 141 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: GPL-2.0

#include "test_progs.h"
#include "network_helpers.h"
#include "cgroup_helpers.h"
#include "cgroup_ancestor.skel.h"

#define CGROUP_PATH "/skb_cgroup_test"
#define TEST_NS "cgroup_ancestor_ns"
#define NUM_CGROUP_LEVELS 4
#define WAIT_AUTO_IP_MAX_ATTEMPT 10
#define DST_ADDR "::1"
#define DST_PORT 1234
#define MAX_ASSERT_NAME 32

struct test_data {
struct cgroup_ancestor *skel;
struct bpf_tc_hook qdisc;
struct bpf_tc_opts tc_attach;
struct nstoken *ns;
};

static int send_datagram(void)
{
unsigned char buf[] = "some random test data";
struct sockaddr_in6 addr = { .sin6_family = AF_INET6,
.sin6_port = htons(DST_PORT), };
int sock, n;

if (!ASSERT_EQ(inet_pton(AF_INET6, DST_ADDR, &addr.sin6_addr), 1,
"inet_pton"))
return -1;

sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (!ASSERT_OK_FD(sock, "create socket"))
return sock;

if (!ASSERT_OK(connect(sock, &addr, sizeof(addr)), "connect")) {
close(sock);
return -1;
}

n = sendto(sock, buf, sizeof(buf), 0, (const struct sockaddr *)&addr,
sizeof(addr));
close(sock);
return ASSERT_EQ(n, sizeof(buf), "send data") ? 0 : -1;
}

static int setup_network(struct test_data *t)
{
SYS(fail, "ip netns add %s", TEST_NS);
t->ns = open_netns(TEST_NS);
if (!ASSERT_OK_PTR(t->ns, "open netns"))
goto cleanup_ns;

SYS(close_ns, "ip link set lo up");

memset(&t->qdisc, 0, sizeof(t->qdisc));
t->qdisc.sz = sizeof(t->qdisc);
t->qdisc.attach_point = BPF_TC_EGRESS;
t->qdisc.ifindex = if_nametoindex("lo");
if (!ASSERT_NEQ(t->qdisc.ifindex, 0, "if_nametoindex"))
goto close_ns;
if (!ASSERT_OK(bpf_tc_hook_create(&t->qdisc), "qdisc add"))
goto close_ns;

memset(&t->tc_attach, 0, sizeof(t->tc_attach));
t->tc_attach.sz = sizeof(t->tc_attach);
t->tc_attach.prog_fd = bpf_program__fd(t->skel->progs.log_cgroup_id);
if (!ASSERT_OK(bpf_tc_attach(&t->qdisc, &t->tc_attach), "filter add"))
goto cleanup_qdisc;

return 0;

cleanup_qdisc:
bpf_tc_hook_destroy(&t->qdisc);
close_ns:
close_netns(t->ns);
cleanup_ns:
SYS_NOFAIL("ip netns del %s", TEST_NS);
fail:
return 1;
}

static void cleanup_network(struct test_data *t)
{
bpf_tc_detach(&t->qdisc, &t->tc_attach);
bpf_tc_hook_destroy(&t->qdisc);
close_netns(t->ns);
SYS_NOFAIL("ip netns del %s", TEST_NS);
}

static void check_ancestors_ids(struct test_data *t)
{
__u64 expected_ids[NUM_CGROUP_LEVELS];
char assert_name[MAX_ASSERT_NAME];
__u32 level;

expected_ids[0] = get_cgroup_id("/.."); /* root cgroup */
expected_ids[1] = get_cgroup_id("");
expected_ids[2] = get_cgroup_id(CGROUP_PATH);
expected_ids[3] = 0; /* non-existent cgroup */

for (level = 0; level < NUM_CGROUP_LEVELS; level++) {
snprintf(assert_name, MAX_ASSERT_NAME,
"ancestor id at level %d", level);
ASSERT_EQ(t->skel->bss->cgroup_ids[level], expected_ids[level],
assert_name);
}
}

void test_cgroup_ancestor(void)
{
struct test_data t;
int cgroup_fd;

t.skel = cgroup_ancestor__open_and_load();
if (!ASSERT_OK_PTR(t.skel, "open and load"))
return;

t.skel->bss->dport = htons(DST_PORT);
cgroup_fd = cgroup_setup_and_join(CGROUP_PATH);
if (cgroup_fd < 0)
goto cleanup_progs;

if (setup_network(&t))
goto cleanup_cgroups;

if (send_datagram())
goto cleanup_network;

check_ancestors_ids(&t);

cleanup_network:
cleanup_network(&t);
cleanup_cgroups:
close(cgroup_fd);
cleanup_cgroup_environment();
cleanup_progs:
cgroup_ancestor__destroy(t.skel);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-2.0

#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "test_progs.h"
#include "cgroup_helpers.h"
#include "get_cgroup_id_kern.skel.h"

#define TEST_CGROUP "/test-bpf-get-cgroup-id/"

void test_cgroup_get_current_cgroup_id(void)
{
struct get_cgroup_id_kern *skel;
const struct timespec req = {
.tv_sec = 0,
.tv_nsec = 1,
};
int cgroup_fd;
__u64 ucgid;

cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
if (!ASSERT_OK_FD(cgroup_fd, "cgroup switch"))
return;

skel = get_cgroup_id_kern__open_and_load();
if (!ASSERT_OK_PTR(skel, "load program"))
goto cleanup_cgroup;

if (!ASSERT_OK(get_cgroup_id_kern__attach(skel), "attach bpf program"))
goto cleanup_progs;

skel->bss->expected_pid = getpid();
/* trigger the syscall on which is attached the tested prog */
if (!ASSERT_OK(syscall(__NR_nanosleep, &req, NULL), "nanosleep"))
goto cleanup_progs;

ucgid = get_cgroup_id(TEST_CGROUP);

ASSERT_EQ(skel->bss->cg_id, ucgid, "compare cgroup ids");

cleanup_progs:
get_cgroup_id_kern__destroy(skel);
cleanup_cgroup:
close(cgroup_fd);
cleanup_cgroup_environment();
}
Loading

0 comments on commit 54d8ab0

Please sign in to comment.