Skip to content

Commit d89af13

Browse files
DanielTimLeeanakryiko
authored andcommitted
samples: bpf: Refactor test_cgrp2_sock2 program with libbpf
This commit refactors the existing cgroup program with libbpf bpf loader. The original test_cgrp2_sock2 has keeped the bpf program attached to the cgroup hierarchy even after the exit of user program. To implement the same functionality with libbpf, this commit uses the BPF_LINK_PINNING to pin the link attachment even after it is closed. Since this uses LINK instead of ATTACH, detach of bpf program from cgroup with 'test_cgrp2_sock' is not used anymore. The code to mount the bpf was added to the .sh file in case the bpff was not mounted on /sys/fs/bpf. Additionally, to fix the problem that shell script cannot find the binary object from the current path, relative path './' has been added in front of binary. Fixes: 554ae6e ("samples/bpf: add userspace example for prohibiting sockets") Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20201124090310.24374-3-danieltimlee@gmail.com
1 parent c5815ac commit d89af13

3 files changed

Lines changed: 62 additions & 22 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test_overhead-objs := bpf_load.o test_overhead_user.o
8282
test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
8383
test_cgrp2_attach-objs := test_cgrp2_attach.o
8484
test_cgrp2_sock-objs := test_cgrp2_sock.o
85-
test_cgrp2_sock2-objs := bpf_load.o test_cgrp2_sock2.o
85+
test_cgrp2_sock2-objs := test_cgrp2_sock2.o
8686
xdp1-objs := xdp1_user.o
8787
# reuse xdp1 source intentionally
8888
xdp2-objs := xdp1_user.o

‎samples/bpf/test_cgrp2_sock2.c‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#include <net/if.h>
2121
#include <linux/bpf.h>
2222
#include <bpf/bpf.h>
23+
#include <bpf/libbpf.h>
2324

2425
#include "bpf_insn.h"
25-
#include "bpf_load.h"
2626

2727
static int usage(const char *argv0)
2828
{
@@ -32,37 +32,64 @@ static int usage(const char *argv0)
3232

3333
int main(int argc, char **argv)
3434
{
35-
int cg_fd, ret, filter_id = 0;
35+
int cg_fd, err, ret = EXIT_FAILURE, filter_id = 0, prog_cnt = 0;
36+
const char *link_pin_path = "/sys/fs/bpf/test_cgrp2_sock2";
37+
struct bpf_link *link = NULL;
38+
struct bpf_program *progs[2];
39+
struct bpf_program *prog;
40+
struct bpf_object *obj;
3641

3742
if (argc < 3)
3843
return usage(argv[0]);
3944

45+
if (argc > 3)
46+
filter_id = atoi(argv[3]);
47+
4048
cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
4149
if (cg_fd < 0) {
4250
printf("Failed to open cgroup path: '%s'\n", strerror(errno));
43-
return EXIT_FAILURE;
51+
return ret;
4452
}
4553

46-
if (load_bpf_file(argv[2]))
47-
return EXIT_FAILURE;
48-
49-
printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
54+
obj = bpf_object__open_file(argv[2], NULL);
55+
if (libbpf_get_error(obj)) {
56+
printf("ERROR: opening BPF object file failed\n");
57+
return ret;
58+
}
5059

51-
if (argc > 3)
52-
filter_id = atoi(argv[3]);
60+
bpf_object__for_each_program(prog, obj) {
61+
progs[prog_cnt] = prog;
62+
prog_cnt++;
63+
}
5364

5465
if (filter_id >= prog_cnt) {
5566
printf("Invalid program id; program not found in file\n");
56-
return EXIT_FAILURE;
67+
goto cleanup;
68+
}
69+
70+
/* load BPF program */
71+
if (bpf_object__load(obj)) {
72+
printf("ERROR: loading BPF object file failed\n");
73+
goto cleanup;
5774
}
5875

59-
ret = bpf_prog_attach(prog_fd[filter_id], cg_fd,
60-
BPF_CGROUP_INET_SOCK_CREATE, 0);
61-
if (ret < 0) {
62-
printf("Failed to attach prog to cgroup: '%s'\n",
63-
strerror(errno));
64-
return EXIT_FAILURE;
76+
link = bpf_program__attach_cgroup(progs[filter_id], cg_fd);
77+
if (libbpf_get_error(link)) {
78+
printf("ERROR: bpf_program__attach failed\n");
79+
link = NULL;
80+
goto cleanup;
6581
}
6682

67-
return EXIT_SUCCESS;
83+
err = bpf_link__pin(link, link_pin_path);
84+
if (err < 0) {
85+
printf("ERROR: bpf_link__pin failed: %d\n", err);
86+
goto cleanup;
87+
}
88+
89+
ret = EXIT_SUCCESS;
90+
91+
cleanup:
92+
bpf_link__destroy(link);
93+
bpf_object__close(obj);
94+
return ret;
6895
}

‎samples/bpf/test_cgrp2_sock2.sh‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/bin/bash
22
# SPDX-License-Identifier: GPL-2.0
33

4+
BPFFS=/sys/fs/bpf
5+
LINK_PIN=$BPFFS/test_cgrp2_sock2
6+
47
function config_device {
58
ip netns add at_ns0
69
ip link add veth0 type veth peer name veth0b
@@ -21,16 +24,22 @@ function config_cgroup {
2124
echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
2225
}
2326

27+
function config_bpffs {
28+
if mount | grep $BPFFS > /dev/null; then
29+
echo "bpffs already mounted"
30+
else
31+
echo "bpffs not mounted. Mounting..."
32+
mount -t bpf none $BPFFS
33+
fi
34+
}
2435

2536
function attach_bpf {
26-
test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
37+
./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
2738
[ $? -ne 0 ] && exit 1
2839
}
2940

3041
function cleanup {
31-
if [ -d /tmp/cgroupv2/foo ]; then
32-
test_cgrp2_sock -d /tmp/cgroupv2/foo
33-
fi
42+
rm -rf $LINK_PIN
3443
ip link del veth0b
3544
ip netns delete at_ns0
3645
umount /tmp/cgroupv2
@@ -42,6 +51,7 @@ cleanup 2>/dev/null
4251
set -e
4352
config_device
4453
config_cgroup
54+
config_bpffs
4555
set +e
4656

4757
#
@@ -62,6 +72,9 @@ if [ $? -eq 0 ]; then
6272
exit 1
6373
fi
6474

75+
rm -rf $LINK_PIN
76+
sleep 1 # Wait for link detach
77+
6578
#
6679
# Test 2 - fail ping
6780
#

0 commit comments

Comments
 (0)