xref: /openbmc/linux/samples/bpf/test_cgrp2_sock2.c (revision ee583014a9d8cc48cb4969f87cc02c12b966fabc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2554ae6e7SDavid Ahern /* eBPF example program:
3554ae6e7SDavid Ahern  *
4554ae6e7SDavid Ahern  * - Loads eBPF program
5554ae6e7SDavid Ahern  *
6554ae6e7SDavid Ahern  *   The eBPF program loads a filter from file and attaches the
7554ae6e7SDavid Ahern  *   program to a cgroup using BPF_PROG_ATTACH
8554ae6e7SDavid Ahern  */
9554ae6e7SDavid Ahern 
10554ae6e7SDavid Ahern #define _GNU_SOURCE
11554ae6e7SDavid Ahern 
12554ae6e7SDavid Ahern #include <stdio.h>
13554ae6e7SDavid Ahern #include <stdlib.h>
14554ae6e7SDavid Ahern #include <stddef.h>
15554ae6e7SDavid Ahern #include <string.h>
16554ae6e7SDavid Ahern #include <unistd.h>
17554ae6e7SDavid Ahern #include <assert.h>
18554ae6e7SDavid Ahern #include <errno.h>
19554ae6e7SDavid Ahern #include <fcntl.h>
20554ae6e7SDavid Ahern #include <net/if.h>
21554ae6e7SDavid Ahern #include <linux/bpf.h>
228d930450SJakub Kicinski #include <bpf/bpf.h>
23554ae6e7SDavid Ahern 
248d930450SJakub Kicinski #include "bpf_insn.h"
25554ae6e7SDavid Ahern #include "bpf_load.h"
26554ae6e7SDavid Ahern 
27554ae6e7SDavid Ahern static int usage(const char *argv0)
28554ae6e7SDavid Ahern {
29554ae6e7SDavid Ahern 	printf("Usage: %s cg-path filter-path [filter-id]\n", argv0);
30554ae6e7SDavid Ahern 	return EXIT_FAILURE;
31554ae6e7SDavid Ahern }
32554ae6e7SDavid Ahern 
33554ae6e7SDavid Ahern int main(int argc, char **argv)
34554ae6e7SDavid Ahern {
35554ae6e7SDavid Ahern 	int cg_fd, ret, filter_id = 0;
36554ae6e7SDavid Ahern 
37554ae6e7SDavid Ahern 	if (argc < 3)
38554ae6e7SDavid Ahern 		return usage(argv[0]);
39554ae6e7SDavid Ahern 
40554ae6e7SDavid Ahern 	cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
41554ae6e7SDavid Ahern 	if (cg_fd < 0) {
42554ae6e7SDavid Ahern 		printf("Failed to open cgroup path: '%s'\n", strerror(errno));
43554ae6e7SDavid Ahern 		return EXIT_FAILURE;
44554ae6e7SDavid Ahern 	}
45554ae6e7SDavid Ahern 
46554ae6e7SDavid Ahern 	if (load_bpf_file(argv[2]))
47554ae6e7SDavid Ahern 		return EXIT_FAILURE;
48554ae6e7SDavid Ahern 
49554ae6e7SDavid Ahern 	printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
50554ae6e7SDavid Ahern 
51554ae6e7SDavid Ahern 	if (argc > 3)
52554ae6e7SDavid Ahern 		filter_id = atoi(argv[3]);
53554ae6e7SDavid Ahern 
54*ee583014SDan Carpenter 	if (filter_id >= prog_cnt) {
55554ae6e7SDavid Ahern 		printf("Invalid program id; program not found in file\n");
56554ae6e7SDavid Ahern 		return EXIT_FAILURE;
57554ae6e7SDavid Ahern 	}
58554ae6e7SDavid Ahern 
59554ae6e7SDavid Ahern 	ret = bpf_prog_attach(prog_fd[filter_id], cg_fd,
607f677633SAlexei Starovoitov 			      BPF_CGROUP_INET_SOCK_CREATE, 0);
61554ae6e7SDavid Ahern 	if (ret < 0) {
62554ae6e7SDavid Ahern 		printf("Failed to attach prog to cgroup: '%s'\n",
63554ae6e7SDavid Ahern 		       strerror(errno));
64554ae6e7SDavid Ahern 		return EXIT_FAILURE;
65554ae6e7SDavid Ahern 	}
66554ae6e7SDavid Ahern 
67554ae6e7SDavid Ahern 	return EXIT_SUCCESS;
68554ae6e7SDavid Ahern }
69