1 // SPDX-License-Identifier: GPL-2.0
2 /* eBPF example program:
6 * The eBPF program loads a filter from file and attaches the
7 * program to a cgroup using BPF_PROG_ATTACH
21 #include <linux/bpf.h>
26 static int usage(const char *argv0
)
28 printf("Usage: %s cg-path filter-path [filter-id]\n", argv0
);
32 int main(int argc
, char **argv
)
34 int cg_fd
, ret
, filter_id
= 0;
37 return usage(argv
[0]);
39 cg_fd
= open(argv
[1], O_DIRECTORY
| O_RDONLY
);
41 printf("Failed to open cgroup path: '%s'\n", strerror(errno
));
45 if (load_bpf_file(argv
[2]))
48 printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf
);
51 filter_id
= atoi(argv
[3]);
53 if (filter_id
> prog_cnt
) {
54 printf("Invalid program id; program not found in file\n");
58 ret
= bpf_prog_attach(prog_fd
[filter_id
], cg_fd
,
59 BPF_CGROUP_INET_SOCK_CREATE
, 0);
61 printf("Failed to attach prog to cgroup: '%s'\n",