Skip to content

Commit 1379fd3

Browse files
sargundavem330
authored andcommitted
samples: bpf: Refactor test_cgrp2_attach -- use getopt, and add mode
This patch modifies test_cgrp2_attach to use getopt so we can use standard command line parsing. It also adds an option to run the program in detach only mode. This does not attach a new filter at the cgroup, but only runs the detach command. Lastly, it changes the attach code to not detach and then attach. It relies on the 'hotswap' behaviour of CGroup BPF programs to be able to change in-place. If detach-then-attach behaviour needs to be tested, the example can be run in detach only mode prior to attachment. Signed-off-by: Sargun Dhillon <sargun@sargun.me> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 377fa64 commit 1379fd3

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

samples/bpf/test_cgrp2_attach.c

+50-30
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* incremented on each iteration by the number of bytes stored in
1111
* the skb.
1212
*
13-
* - Detaches any eBPF program previously attached to the cgroup
14-
*
1513
* - Attaches the new program to a cgroup using BPF_PROG_ATTACH
1614
*
1715
* - Every second, reads map[0] and map[1] to see how many bytes and
@@ -75,35 +73,16 @@ static int prog_load(int map_fd, int verdict)
7573

7674
static int usage(const char *argv0)
7775
{
78-
printf("Usage: %s <cg-path> <egress|ingress> [drop]\n", argv0);
76+
printf("Usage: %s [-d] [-D] <cg-path> <egress|ingress>\n", argv0);
77+
printf(" -d Drop Traffic\n");
78+
printf(" -D Detach filter, and exit\n");
7979
return EXIT_FAILURE;
8080
}
8181

82-
int main(int argc, char **argv)
82+
static int attach_filter(int cg_fd, int type, int verdict)
8383
{
84-
int cg_fd, map_fd, prog_fd, key, ret;
84+
int prog_fd, map_fd, ret, key;
8585
long long pkt_cnt, byte_cnt;
86-
enum bpf_attach_type type;
87-
int verdict = 1;
88-
89-
if (argc < 3)
90-
return usage(argv[0]);
91-
92-
if (strcmp(argv[2], "ingress") == 0)
93-
type = BPF_CGROUP_INET_INGRESS;
94-
else if (strcmp(argv[2], "egress") == 0)
95-
type = BPF_CGROUP_INET_EGRESS;
96-
else
97-
return usage(argv[0]);
98-
99-
if (argc > 3 && strcmp(argv[3], "drop") == 0)
100-
verdict = 0;
101-
102-
cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
103-
if (cg_fd < 0) {
104-
printf("Failed to open cgroup path: '%s'\n", strerror(errno));
105-
return EXIT_FAILURE;
106-
}
10786

10887
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY,
10988
sizeof(key), sizeof(byte_cnt),
@@ -121,16 +100,12 @@ int main(int argc, char **argv)
121100
return EXIT_FAILURE;
122101
}
123102

124-
ret = bpf_prog_detach(cg_fd, type);
125-
printf("bpf_prog_detach() returned '%s' (%d)\n", strerror(errno), errno);
126-
127103
ret = bpf_prog_attach(prog_fd, cg_fd, type);
128104
if (ret < 0) {
129105
printf("Failed to attach prog to cgroup: '%s'\n",
130106
strerror(errno));
131107
return EXIT_FAILURE;
132108
}
133-
134109
while (1) {
135110
key = MAP_KEY_PACKETS;
136111
assert(bpf_lookup_elem(map_fd, &key, &pkt_cnt) == 0);
@@ -145,3 +120,48 @@ int main(int argc, char **argv)
145120

146121
return EXIT_SUCCESS;
147122
}
123+
124+
int main(int argc, char **argv)
125+
{
126+
int detach_only = 0, verdict = 1;
127+
enum bpf_attach_type type;
128+
int opt, cg_fd, ret;
129+
130+
while ((opt = getopt(argc, argv, "Dd")) != -1) {
131+
switch (opt) {
132+
case 'd':
133+
verdict = 0;
134+
break;
135+
case 'D':
136+
detach_only = 1;
137+
break;
138+
default:
139+
return usage(argv[0]);
140+
}
141+
}
142+
143+
if (argc - optind < 2)
144+
return usage(argv[0]);
145+
146+
if (strcmp(argv[optind + 1], "ingress") == 0)
147+
type = BPF_CGROUP_INET_INGRESS;
148+
else if (strcmp(argv[optind + 1], "egress") == 0)
149+
type = BPF_CGROUP_INET_EGRESS;
150+
else
151+
return usage(argv[0]);
152+
153+
cg_fd = open(argv[optind], O_DIRECTORY | O_RDONLY);
154+
if (cg_fd < 0) {
155+
printf("Failed to open cgroup path: '%s'\n", strerror(errno));
156+
return EXIT_FAILURE;
157+
}
158+
159+
if (detach_only) {
160+
ret = bpf_prog_detach(cg_fd, type);
161+
printf("bpf_prog_detach() returned '%s' (%d)\n",
162+
strerror(errno), errno);
163+
} else
164+
ret = attach_filter(cg_fd, type, verdict);
165+
166+
return ret;
167+
}

0 commit comments

Comments
 (0)