xref: /openbmc/linux/samples/bpf/xdp_fwd_user.c (revision 349d03ff)
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/libbpf.h>
28 #include <bpf/bpf.h>
29 
30 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
31 
32 static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
33 {
34 	int err;
35 
36 	err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL);
37 	if (err < 0) {
38 		printf("ERROR: failed to attach program to %s\n", name);
39 		return err;
40 	}
41 
42 	/* Adding ifindex as a possible egress TX port */
43 	err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
44 	if (err)
45 		printf("ERROR: failed using device %s as TX-port\n", name);
46 
47 	return err;
48 }
49 
50 static int do_detach(int idx, const char *name)
51 {
52 	int err;
53 
54 	err = bpf_xdp_detach(idx, xdp_flags, NULL);
55 	if (err < 0)
56 		printf("ERROR: failed to detach program from %s\n", name);
57 
58 	/* TODO: Remember to cleanup map, when adding use of shared map
59 	 *  bpf_map_delete_elem((map_fd, &idx);
60 	 */
61 	return err;
62 }
63 
64 static void usage(const char *prog)
65 {
66 	fprintf(stderr,
67 		"usage: %s [OPTS] interface-list\n"
68 		"\nOPTS:\n"
69 		"    -d    detach program\n"
70 		"    -S    use skb-mode\n"
71 		"    -F    force loading prog\n"
72 		"    -D    direct table lookups (skip fib rules)\n",
73 		prog);
74 }
75 
76 int main(int argc, char **argv)
77 {
78 	const char *prog_name = "xdp_fwd";
79 	struct bpf_program *prog = NULL;
80 	struct bpf_program *pos;
81 	const char *sec_name;
82 	int prog_fd = -1, map_fd = -1;
83 	char filename[PATH_MAX];
84 	struct bpf_object *obj;
85 	int opt, i, idx, err;
86 	int attach = 1;
87 	int ret = 0;
88 
89 	while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
90 		switch (opt) {
91 		case 'd':
92 			attach = 0;
93 			break;
94 		case 'S':
95 			xdp_flags |= XDP_FLAGS_SKB_MODE;
96 			break;
97 		case 'F':
98 			xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
99 			break;
100 		case 'D':
101 			prog_name = "xdp_fwd_direct";
102 			break;
103 		default:
104 			usage(basename(argv[0]));
105 			return 1;
106 		}
107 	}
108 
109 	if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
110 		xdp_flags |= XDP_FLAGS_DRV_MODE;
111 
112 	if (optind == argc) {
113 		usage(basename(argv[0]));
114 		return 1;
115 	}
116 
117 	if (attach) {
118 		snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
119 
120 		if (access(filename, O_RDONLY) < 0) {
121 			printf("error accessing file %s: %s\n",
122 				filename, strerror(errno));
123 			return 1;
124 		}
125 
126 		obj = bpf_object__open_file(filename, NULL);
127 		if (libbpf_get_error(obj))
128 			return 1;
129 
130 		prog = bpf_object__next_program(obj, NULL);
131 		bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
132 
133 		err = bpf_object__load(obj);
134 		if (err) {
135 			printf("Does kernel support devmap lookup?\n");
136 			/* If not, the error message will be:
137 			 *  "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
138 			 */
139 			return 1;
140 		}
141 
142 		bpf_object__for_each_program(pos, obj) {
143 			sec_name = bpf_program__section_name(pos);
144 			if (sec_name && !strcmp(sec_name, prog_name)) {
145 				prog = pos;
146 				break;
147 			}
148 		}
149 		prog_fd = bpf_program__fd(prog);
150 		if (prog_fd < 0) {
151 			printf("program not found: %s\n", strerror(prog_fd));
152 			return 1;
153 		}
154 		map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
155 							"xdp_tx_ports"));
156 		if (map_fd < 0) {
157 			printf("map not found: %s\n", strerror(map_fd));
158 			return 1;
159 		}
160 	}
161 
162 	for (i = optind; i < argc; ++i) {
163 		idx = if_nametoindex(argv[i]);
164 		if (!idx)
165 			idx = strtoul(argv[i], NULL, 0);
166 
167 		if (!idx) {
168 			fprintf(stderr, "Invalid arg\n");
169 			return 1;
170 		}
171 		if (!attach) {
172 			err = do_detach(idx, argv[i]);
173 			if (err)
174 				ret = err;
175 		} else {
176 			err = do_attach(idx, prog_fd, map_fd, argv[i]);
177 			if (err)
178 				ret = err;
179 		}
180 	}
181 
182 	return ret;
183 }
184