xref: /openbmc/linux/samples/bpf/xdp_fwd_user.c (revision ba61bb17)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  */
13 
14 #include <linux/bpf.h>
15 #include <linux/if_link.h>
16 #include <linux/limits.h>
17 #include <net/if.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <libgen.h>
26 
27 #include "bpf_load.h"
28 #include "bpf_util.h"
29 #include <bpf/bpf.h>
30 
31 
32 static int do_attach(int idx, int fd, const char *name)
33 {
34 	int err;
35 
36 	err = bpf_set_link_xdp_fd(idx, fd, 0);
37 	if (err < 0)
38 		printf("ERROR: failed to attach program to %s\n", name);
39 
40 	return err;
41 }
42 
43 static int do_detach(int idx, const char *name)
44 {
45 	int err;
46 
47 	err = bpf_set_link_xdp_fd(idx, -1, 0);
48 	if (err < 0)
49 		printf("ERROR: failed to detach program from %s\n", name);
50 
51 	return err;
52 }
53 
54 static void usage(const char *prog)
55 {
56 	fprintf(stderr,
57 		"usage: %s [OPTS] interface-list\n"
58 		"\nOPTS:\n"
59 		"    -d    detach program\n"
60 		"    -D    direct table lookups (skip fib rules)\n",
61 		prog);
62 }
63 
64 int main(int argc, char **argv)
65 {
66 	char filename[PATH_MAX];
67 	int opt, i, idx, err;
68 	int prog_id = 0;
69 	int attach = 1;
70 	int ret = 0;
71 
72 	while ((opt = getopt(argc, argv, ":dD")) != -1) {
73 		switch (opt) {
74 		case 'd':
75 			attach = 0;
76 			break;
77 		case 'D':
78 			prog_id = 1;
79 			break;
80 		default:
81 			usage(basename(argv[0]));
82 			return 1;
83 		}
84 	}
85 
86 	if (optind == argc) {
87 		usage(basename(argv[0]));
88 		return 1;
89 	}
90 
91 	if (attach) {
92 		snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
93 
94 		if (access(filename, O_RDONLY) < 0) {
95 			printf("error accessing file %s: %s\n",
96 				filename, strerror(errno));
97 			return 1;
98 		}
99 
100 		if (load_bpf_file(filename)) {
101 			printf("%s", bpf_log_buf);
102 			return 1;
103 		}
104 
105 		if (!prog_fd[prog_id]) {
106 			printf("load_bpf_file: %s\n", strerror(errno));
107 			return 1;
108 		}
109 	}
110 	if (attach) {
111 		for (i = 1; i < 64; ++i)
112 			bpf_map_update_elem(map_fd[0], &i, &i, 0);
113 	}
114 
115 	for (i = optind; i < argc; ++i) {
116 		idx = if_nametoindex(argv[i]);
117 		if (!idx)
118 			idx = strtoul(argv[i], NULL, 0);
119 
120 		if (!idx) {
121 			fprintf(stderr, "Invalid arg\n");
122 			return 1;
123 		}
124 		if (!attach) {
125 			err = do_detach(idx, argv[i]);
126 			if (err)
127 				ret = err;
128 		} else {
129 			err = do_attach(idx, prog_fd[prog_id], argv[i]);
130 			if (err)
131 				ret = err;
132 		}
133 	}
134 
135 	return ret;
136 }
137