Skip to content

Commit 6eab869

Browse files
yonghong-songSomasundaram Krishnasamy
authored andcommitted
bpf: add a test case to test single tp multiple bpf attachment
The bpf sample program syscall_tp is modified to show attachment of more than bpf programs for a particular kernel tracepoint. Signed-off-by: Yonghong Song <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Signed-off-by: David S. Miller <[email protected]> (cherry picked from commit a678be5) Orabug: 31667601 Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Mark Haywood <[email protected]> Signed-off-by: Somasundaram Krishnasamy <[email protected]>
1 parent 3135a91 commit 6eab869

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

samples/bpf/syscall_tp_user.c

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
* This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
2424
*/
2525

26+
static void usage(const char *cmd)
27+
{
28+
printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
29+
printf(" -i num_progs # number of progs of the test\n");
30+
printf(" -h # help\n");
31+
}
32+
2633
static void verify_map(int map_id)
2734
{
2835
__u32 key = 0;
@@ -32,22 +39,29 @@ static void verify_map(int map_id)
3239
fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
3340
return;
3441
}
35-
if (val == 0)
42+
if (val == 0) {
3643
fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
44+
return;
45+
}
46+
val = 0;
47+
if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) {
48+
fprintf(stderr, "map_update failed: %s\n", strerror(errno));
49+
return;
50+
}
3751
}
3852

39-
int main(int argc, char **argv)
53+
static int test(char *filename, int num_progs)
4054
{
41-
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
42-
char filename[256];
43-
int fd;
55+
int i, fd, map0_fds[num_progs], map1_fds[num_progs];
4456

45-
setrlimit(RLIMIT_MEMLOCK, &r);
46-
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
47-
48-
if (load_bpf_file(filename)) {
49-
fprintf(stderr, "%s", bpf_log_buf);
50-
return 1;
57+
for (i = 0; i < num_progs; i++) {
58+
if (load_bpf_file(filename)) {
59+
fprintf(stderr, "%s", bpf_log_buf);
60+
return 1;
61+
}
62+
printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]);
63+
map0_fds[i] = map_fd[0];
64+
map1_fds[i] = map_fd[1];
5165
}
5266

5367
/* current load_bpf_file has perf_event_open default pid = -1
@@ -64,8 +78,34 @@ int main(int argc, char **argv)
6478
close(fd);
6579

6680
/* verify the map */
67-
verify_map(map_fd[0]);
68-
verify_map(map_fd[1]);
81+
for (i = 0; i < num_progs; i++) {
82+
verify_map(map0_fds[i]);
83+
verify_map(map1_fds[i]);
84+
}
6985

7086
return 0;
7187
}
88+
89+
int main(int argc, char **argv)
90+
{
91+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
92+
int opt, num_progs = 1;
93+
char filename[256];
94+
95+
while ((opt = getopt(argc, argv, "i:h")) != -1) {
96+
switch (opt) {
97+
case 'i':
98+
num_progs = atoi(optarg);
99+
break;
100+
case 'h':
101+
default:
102+
usage(argv[0]);
103+
return 0;
104+
}
105+
}
106+
107+
setrlimit(RLIMIT_MEMLOCK, &r);
108+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
109+
110+
return test(filename, num_progs);
111+
}

0 commit comments

Comments
 (0)