xref: /openbmc/linux/samples/bpf/test_cgrp2_sock2.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
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>
23*d89af13cSDaniel T. Lee #include <bpf/libbpf.h>
24554ae6e7SDavid Ahern 
258d930450SJakub Kicinski #include "bpf_insn.h"
26554ae6e7SDavid Ahern 
usage(const char * argv0)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 
main(int argc,char ** argv)33554ae6e7SDavid Ahern int main(int argc, char **argv)
34554ae6e7SDavid Ahern {
35*d89af13cSDaniel T. Lee 	int cg_fd, err, ret = EXIT_FAILURE, filter_id = 0, prog_cnt = 0;
36*d89af13cSDaniel T. Lee 	const char *link_pin_path = "/sys/fs/bpf/test_cgrp2_sock2";
37*d89af13cSDaniel T. Lee 	struct bpf_link *link = NULL;
38*d89af13cSDaniel T. Lee 	struct bpf_program *progs[2];
39*d89af13cSDaniel T. Lee 	struct bpf_program *prog;
40*d89af13cSDaniel T. Lee 	struct bpf_object *obj;
41554ae6e7SDavid Ahern 
42554ae6e7SDavid Ahern 	if (argc < 3)
43554ae6e7SDavid Ahern 		return usage(argv[0]);
44554ae6e7SDavid Ahern 
45554ae6e7SDavid Ahern 	if (argc > 3)
46554ae6e7SDavid Ahern 		filter_id = atoi(argv[3]);
47554ae6e7SDavid Ahern 
48*d89af13cSDaniel T. Lee 	cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
49*d89af13cSDaniel T. Lee 	if (cg_fd < 0) {
50*d89af13cSDaniel T. Lee 		printf("Failed to open cgroup path: '%s'\n", strerror(errno));
51*d89af13cSDaniel T. Lee 		return ret;
52*d89af13cSDaniel T. Lee 	}
53*d89af13cSDaniel T. Lee 
54*d89af13cSDaniel T. Lee 	obj = bpf_object__open_file(argv[2], NULL);
55*d89af13cSDaniel T. Lee 	if (libbpf_get_error(obj)) {
56*d89af13cSDaniel T. Lee 		printf("ERROR: opening BPF object file failed\n");
57*d89af13cSDaniel T. Lee 		return ret;
58*d89af13cSDaniel T. Lee 	}
59*d89af13cSDaniel T. Lee 
60*d89af13cSDaniel T. Lee 	bpf_object__for_each_program(prog, obj) {
61*d89af13cSDaniel T. Lee 		progs[prog_cnt] = prog;
62*d89af13cSDaniel T. Lee 		prog_cnt++;
63*d89af13cSDaniel T. Lee 	}
64*d89af13cSDaniel T. Lee 
65ee583014SDan Carpenter 	if (filter_id >= prog_cnt) {
66554ae6e7SDavid Ahern 		printf("Invalid program id; program not found in file\n");
67*d89af13cSDaniel T. Lee 		goto cleanup;
68554ae6e7SDavid Ahern 	}
69554ae6e7SDavid Ahern 
70*d89af13cSDaniel T. Lee 	/* load BPF program */
71*d89af13cSDaniel T. Lee 	if (bpf_object__load(obj)) {
72*d89af13cSDaniel T. Lee 		printf("ERROR: loading BPF object file failed\n");
73*d89af13cSDaniel T. Lee 		goto cleanup;
74554ae6e7SDavid Ahern 	}
75554ae6e7SDavid Ahern 
76*d89af13cSDaniel T. Lee 	link = bpf_program__attach_cgroup(progs[filter_id], cg_fd);
77*d89af13cSDaniel T. Lee 	if (libbpf_get_error(link)) {
78*d89af13cSDaniel T. Lee 		printf("ERROR: bpf_program__attach failed\n");
79*d89af13cSDaniel T. Lee 		link = NULL;
80*d89af13cSDaniel T. Lee 		goto cleanup;
81*d89af13cSDaniel T. Lee 	}
82*d89af13cSDaniel T. Lee 
83*d89af13cSDaniel T. Lee 	err = bpf_link__pin(link, link_pin_path);
84*d89af13cSDaniel T. Lee 	if (err < 0) {
85*d89af13cSDaniel T. Lee 		printf("ERROR: bpf_link__pin failed: %d\n", err);
86*d89af13cSDaniel T. Lee 		goto cleanup;
87*d89af13cSDaniel T. Lee 	}
88*d89af13cSDaniel T. Lee 
89*d89af13cSDaniel T. Lee 	ret = EXIT_SUCCESS;
90*d89af13cSDaniel T. Lee 
91*d89af13cSDaniel T. Lee cleanup:
92*d89af13cSDaniel T. Lee 	bpf_link__destroy(link);
93*d89af13cSDaniel T. Lee 	bpf_object__close(obj);
94*d89af13cSDaniel T. Lee 	return ret;
95554ae6e7SDavid Ahern }
96