Skip to content

Commit 54d8ab0

Browse files
author
Martin KaFai Lau
committed
Merge branch 'bpf-next/master' into for-next
Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 5cbe27f + b97ce54 commit 54d8ab0

13 files changed

+353
-647
lines changed

tools/testing/selftests/bpf/.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ test_sock
1919
urandom_read
2020
test_sockmap
2121
test_lirc_mode2_user
22-
get_cgroup_id_user
23-
test_skb_cgroup_id_user
24-
test_cgroup_storage
2522
test_flow_dissector
2623
flow_dissector_load
2724
test_tcpnotify_user

tools/testing/selftests/bpf/Makefile

+2-6
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ endif
6767

6868
# Order correspond to 'make run_tests' order
6969
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
70-
test_sock test_sockmap get_cgroup_id_user \
71-
test_cgroup_storage \
70+
test_sock test_sockmap \
7271
test_tcpnotify_user test_sysctl \
7372
test_progs-no_alu32
7473
TEST_INST_SUBDIRS := no_alu32
@@ -138,7 +137,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
138137
test_xdp_vlan.sh test_bpftool.py
139138

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

294-
$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
295293
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
296294
$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
297295
$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
298-
$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
299-
$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
300296
$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
301297
$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
302298
$(OUTPUT)/test_tag: $(TESTING_HELPERS)

tools/testing/selftests/bpf/get_cgroup_id_user.c

-151
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "test_progs.h"
4+
#include "network_helpers.h"
5+
#include "cgroup_helpers.h"
6+
#include "cgroup_ancestor.skel.h"
7+
8+
#define CGROUP_PATH "/skb_cgroup_test"
9+
#define TEST_NS "cgroup_ancestor_ns"
10+
#define NUM_CGROUP_LEVELS 4
11+
#define WAIT_AUTO_IP_MAX_ATTEMPT 10
12+
#define DST_ADDR "::1"
13+
#define DST_PORT 1234
14+
#define MAX_ASSERT_NAME 32
15+
16+
struct test_data {
17+
struct cgroup_ancestor *skel;
18+
struct bpf_tc_hook qdisc;
19+
struct bpf_tc_opts tc_attach;
20+
struct nstoken *ns;
21+
};
22+
23+
static int send_datagram(void)
24+
{
25+
unsigned char buf[] = "some random test data";
26+
struct sockaddr_in6 addr = { .sin6_family = AF_INET6,
27+
.sin6_port = htons(DST_PORT), };
28+
int sock, n;
29+
30+
if (!ASSERT_EQ(inet_pton(AF_INET6, DST_ADDR, &addr.sin6_addr), 1,
31+
"inet_pton"))
32+
return -1;
33+
34+
sock = socket(AF_INET6, SOCK_DGRAM, 0);
35+
if (!ASSERT_OK_FD(sock, "create socket"))
36+
return sock;
37+
38+
if (!ASSERT_OK(connect(sock, &addr, sizeof(addr)), "connect")) {
39+
close(sock);
40+
return -1;
41+
}
42+
43+
n = sendto(sock, buf, sizeof(buf), 0, (const struct sockaddr *)&addr,
44+
sizeof(addr));
45+
close(sock);
46+
return ASSERT_EQ(n, sizeof(buf), "send data") ? 0 : -1;
47+
}
48+
49+
static int setup_network(struct test_data *t)
50+
{
51+
SYS(fail, "ip netns add %s", TEST_NS);
52+
t->ns = open_netns(TEST_NS);
53+
if (!ASSERT_OK_PTR(t->ns, "open netns"))
54+
goto cleanup_ns;
55+
56+
SYS(close_ns, "ip link set lo up");
57+
58+
memset(&t->qdisc, 0, sizeof(t->qdisc));
59+
t->qdisc.sz = sizeof(t->qdisc);
60+
t->qdisc.attach_point = BPF_TC_EGRESS;
61+
t->qdisc.ifindex = if_nametoindex("lo");
62+
if (!ASSERT_NEQ(t->qdisc.ifindex, 0, "if_nametoindex"))
63+
goto close_ns;
64+
if (!ASSERT_OK(bpf_tc_hook_create(&t->qdisc), "qdisc add"))
65+
goto close_ns;
66+
67+
memset(&t->tc_attach, 0, sizeof(t->tc_attach));
68+
t->tc_attach.sz = sizeof(t->tc_attach);
69+
t->tc_attach.prog_fd = bpf_program__fd(t->skel->progs.log_cgroup_id);
70+
if (!ASSERT_OK(bpf_tc_attach(&t->qdisc, &t->tc_attach), "filter add"))
71+
goto cleanup_qdisc;
72+
73+
return 0;
74+
75+
cleanup_qdisc:
76+
bpf_tc_hook_destroy(&t->qdisc);
77+
close_ns:
78+
close_netns(t->ns);
79+
cleanup_ns:
80+
SYS_NOFAIL("ip netns del %s", TEST_NS);
81+
fail:
82+
return 1;
83+
}
84+
85+
static void cleanup_network(struct test_data *t)
86+
{
87+
bpf_tc_detach(&t->qdisc, &t->tc_attach);
88+
bpf_tc_hook_destroy(&t->qdisc);
89+
close_netns(t->ns);
90+
SYS_NOFAIL("ip netns del %s", TEST_NS);
91+
}
92+
93+
static void check_ancestors_ids(struct test_data *t)
94+
{
95+
__u64 expected_ids[NUM_CGROUP_LEVELS];
96+
char assert_name[MAX_ASSERT_NAME];
97+
__u32 level;
98+
99+
expected_ids[0] = get_cgroup_id("/.."); /* root cgroup */
100+
expected_ids[1] = get_cgroup_id("");
101+
expected_ids[2] = get_cgroup_id(CGROUP_PATH);
102+
expected_ids[3] = 0; /* non-existent cgroup */
103+
104+
for (level = 0; level < NUM_CGROUP_LEVELS; level++) {
105+
snprintf(assert_name, MAX_ASSERT_NAME,
106+
"ancestor id at level %d", level);
107+
ASSERT_EQ(t->skel->bss->cgroup_ids[level], expected_ids[level],
108+
assert_name);
109+
}
110+
}
111+
112+
void test_cgroup_ancestor(void)
113+
{
114+
struct test_data t;
115+
int cgroup_fd;
116+
117+
t.skel = cgroup_ancestor__open_and_load();
118+
if (!ASSERT_OK_PTR(t.skel, "open and load"))
119+
return;
120+
121+
t.skel->bss->dport = htons(DST_PORT);
122+
cgroup_fd = cgroup_setup_and_join(CGROUP_PATH);
123+
if (cgroup_fd < 0)
124+
goto cleanup_progs;
125+
126+
if (setup_network(&t))
127+
goto cleanup_cgroups;
128+
129+
if (send_datagram())
130+
goto cleanup_network;
131+
132+
check_ancestors_ids(&t);
133+
134+
cleanup_network:
135+
cleanup_network(&t);
136+
cleanup_cgroups:
137+
close(cgroup_fd);
138+
cleanup_cgroup_environment();
139+
cleanup_progs:
140+
cgroup_ancestor__destroy(t.skel);
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <sys/stat.h>
4+
#include <sys/sysmacros.h>
5+
#include "test_progs.h"
6+
#include "cgroup_helpers.h"
7+
#include "get_cgroup_id_kern.skel.h"
8+
9+
#define TEST_CGROUP "/test-bpf-get-cgroup-id/"
10+
11+
void test_cgroup_get_current_cgroup_id(void)
12+
{
13+
struct get_cgroup_id_kern *skel;
14+
const struct timespec req = {
15+
.tv_sec = 0,
16+
.tv_nsec = 1,
17+
};
18+
int cgroup_fd;
19+
__u64 ucgid;
20+
21+
cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
22+
if (!ASSERT_OK_FD(cgroup_fd, "cgroup switch"))
23+
return;
24+
25+
skel = get_cgroup_id_kern__open_and_load();
26+
if (!ASSERT_OK_PTR(skel, "load program"))
27+
goto cleanup_cgroup;
28+
29+
if (!ASSERT_OK(get_cgroup_id_kern__attach(skel), "attach bpf program"))
30+
goto cleanup_progs;
31+
32+
skel->bss->expected_pid = getpid();
33+
/* trigger the syscall on which is attached the tested prog */
34+
if (!ASSERT_OK(syscall(__NR_nanosleep, &req, NULL), "nanosleep"))
35+
goto cleanup_progs;
36+
37+
ucgid = get_cgroup_id(TEST_CGROUP);
38+
39+
ASSERT_EQ(skel->bss->cg_id, ucgid, "compare cgroup ids");
40+
41+
cleanup_progs:
42+
get_cgroup_id_kern__destroy(skel);
43+
cleanup_cgroup:
44+
close(cgroup_fd);
45+
cleanup_cgroup_environment();
46+
}

0 commit comments

Comments
 (0)