Commit 3f32a115 authored by Geliang Tang's avatar Geliang Tang Committed by Daniel Borkmann

selftests/bpf: Use bpf_link attachments in test_sockmap

Switch attachments to bpf_link using bpf_program__attach_sockmap() instead
of bpf_prog_attach().

This patch adds a new array progs[] to replace prog_fd[] array, set in
populate_progs() for each program in bpf object.

And another new array links[] to save the attached bpf_link. It is
initalized as NULL in populate_progs, set as the return valuses of
bpf_program__attach_sockmap(), and detached by bpf_link__detach().
Signed-off-by: default avatarGeliang Tang <tanggeliang@kylinos.cn>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Tested-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/32cf8376a810e2e9c719f8e4cfb97132ed2d1f9c.1716446893.git.tanggeliang@kylinos.cn
parent a9f0ea17
...@@ -64,6 +64,8 @@ int failed; ...@@ -64,6 +64,8 @@ int failed;
int map_fd[9]; int map_fd[9];
struct bpf_map *maps[9]; struct bpf_map *maps[9];
int prog_fd[9]; int prog_fd[9];
struct bpf_program *progs[9];
struct bpf_link *links[9];
int txmsg_pass; int txmsg_pass;
int txmsg_redir; int txmsg_redir;
...@@ -960,43 +962,39 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test) ...@@ -960,43 +962,39 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test)
/* Attach programs to sockmap */ /* Attach programs to sockmap */
if (!txmsg_omit_skb_parser) { if (!txmsg_omit_skb_parser) {
err = bpf_prog_attach(prog_fd[0], map_fd[0], links[0] = bpf_program__attach_sockmap(progs[0], map_fd[0]);
BPF_SK_SKB_STREAM_PARSER, 0); if (!links[0]) {
if (err) {
fprintf(stderr, fprintf(stderr,
"ERROR: bpf_prog_attach (sockmap %i->%i): %d (%s)\n", "ERROR: bpf_program__attach_sockmap (sockmap %i->%i): (%s)\n",
prog_fd[0], map_fd[0], err, strerror(errno)); bpf_program__fd(progs[0]), map_fd[0], strerror(errno));
return err; return -1;
} }
} }
err = bpf_prog_attach(prog_fd[1], map_fd[0], links[1] = bpf_program__attach_sockmap(progs[1], map_fd[0]);
BPF_SK_SKB_STREAM_VERDICT, 0); if (!links[1]) {
if (err) { fprintf(stderr, "ERROR: bpf_program__attach_sockmap (sockmap): (%s)\n",
fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n", strerror(errno));
err, strerror(errno)); return -1;
return err;
} }
/* Attach programs to TLS sockmap */ /* Attach programs to TLS sockmap */
if (txmsg_ktls_skb) { if (txmsg_ktls_skb) {
if (!txmsg_omit_skb_parser) { if (!txmsg_omit_skb_parser) {
err = bpf_prog_attach(prog_fd[0], map_fd[8], links[2] = bpf_program__attach_sockmap(progs[0], map_fd[8]);
BPF_SK_SKB_STREAM_PARSER, 0); if (!links[2]) {
if (err) {
fprintf(stderr, fprintf(stderr,
"ERROR: bpf_prog_attach (TLS sockmap %i->%i): %d (%s)\n", "ERROR: bpf_program__attach_sockmap (TLS sockmap %i->%i): (%s)\n",
prog_fd[0], map_fd[8], err, strerror(errno)); bpf_program__fd(progs[0]), map_fd[8], strerror(errno));
return err; return -1;
} }
} }
err = bpf_prog_attach(prog_fd[2], map_fd[8], links[3] = bpf_program__attach_sockmap(progs[2], map_fd[8]);
BPF_SK_SKB_STREAM_VERDICT, 0); if (!links[3]) {
if (err) { fprintf(stderr, "ERROR: bpf_program__attach_sockmap (TLS sockmap): (%s)\n",
fprintf(stderr, "ERROR: bpf_prog_attach (TLS sockmap): %d (%s)\n", strerror(errno));
err, strerror(errno)); return -1;
return err;
} }
} }
...@@ -1281,10 +1279,11 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test) ...@@ -1281,10 +1279,11 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test)
out: out:
/* Detatch and zero all the maps */ /* Detatch and zero all the maps */
bpf_prog_detach2(prog_fd[3], cg_fd, BPF_CGROUP_SOCK_OPS); bpf_prog_detach2(prog_fd[3], cg_fd, BPF_CGROUP_SOCK_OPS);
bpf_prog_detach2(prog_fd[0], map_fd[0], BPF_SK_SKB_STREAM_PARSER);
bpf_prog_detach2(prog_fd[1], map_fd[0], BPF_SK_SKB_STREAM_VERDICT); for (i = 0; i < ARRAY_SIZE(links); i++) {
bpf_prog_detach2(prog_fd[0], map_fd[8], BPF_SK_SKB_STREAM_PARSER); if (links[i])
bpf_prog_detach2(prog_fd[2], map_fd[8], BPF_SK_SKB_STREAM_VERDICT); bpf_link__detach(links[i]);
}
if (tx_prog_fd > 0) if (tx_prog_fd > 0)
bpf_prog_detach2(tx_prog_fd, map_fd[1], BPF_SK_MSG_VERDICT); bpf_prog_detach2(tx_prog_fd, map_fd[1], BPF_SK_MSG_VERDICT);
...@@ -1805,6 +1804,7 @@ static int populate_progs(char *bpf_file) ...@@ -1805,6 +1804,7 @@ static int populate_progs(char *bpf_file)
i = bpf_object__load(obj); i = bpf_object__load(obj);
i = 0; i = 0;
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
progs[i] = prog;
prog_fd[i] = bpf_program__fd(prog); prog_fd[i] = bpf_program__fd(prog);
i++; i++;
} }
...@@ -1819,6 +1819,9 @@ static int populate_progs(char *bpf_file) ...@@ -1819,6 +1819,9 @@ static int populate_progs(char *bpf_file)
} }
} }
for (i = 0; i < ARRAY_SIZE(links); i++)
links[i] = NULL;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment