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 
do_attach(int idx,int prog_fd,int map_fd,const char * name)32 static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
33 {
34 	int err;
35 
36 	err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
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 
do_detach(int idx,const char * name)50 static int do_detach(int idx, const char *name)
51 {
52 	int err;
53 
54 	err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
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 
usage(const char * prog)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 
main(int argc,char ** argv)76 int main(int argc, char **argv)
77 {
78 	struct bpf_prog_load_attr prog_load_attr = {
79 		.prog_type	= BPF_PROG_TYPE_XDP,
80 	};
81 	const char *prog_name = "xdp_fwd";
82 	struct bpf_program *prog;
83 	int prog_fd, map_fd = -1;
84 	char filename[PATH_MAX];
85 	struct bpf_object *obj;
86 	int opt, i, idx, err;
87 	int attach = 1;
88 	int ret = 0;
89 
90 	while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
91 		switch (opt) {
92 		case 'd':
93 			attach = 0;
94 			break;
95 		case 'S':
96 			xdp_flags |= XDP_FLAGS_SKB_MODE;
97 			break;
98 		case 'F':
99 			xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
100 			break;
101 		case 'D':
102 			prog_name = "xdp_fwd_direct";
103 			break;
104 		default:
105 			usage(basename(argv[0]));
106 			return 1;
107 		}
108 	}
109 
110 	if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
111 		xdp_flags |= XDP_FLAGS_DRV_MODE;
112 
113 	if (optind == argc) {
114 		usage(basename(argv[0]));
115 		return 1;
116 	}
117 
118 	if (attach) {
119 		snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
120 		prog_load_attr.file = filename;
121 
122 		if (access(filename, O_RDONLY) < 0) {
123 			printf("error accessing file %s: %s\n",
124 				filename, strerror(errno));
125 			return 1;
126 		}
127 
128 		err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
129 		if (err) {
130 			printf("Does kernel support devmap lookup?\n");
131 			/* If not, the error message will be:
132 			 *  "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
133 			 */
134 			return 1;
135 		}
136 
137 		prog = bpf_object__find_program_by_title(obj, prog_name);
138 		prog_fd = bpf_program__fd(prog);
139 		if (prog_fd < 0) {
140 			printf("program not found: %s\n", strerror(prog_fd));
141 			return 1;
142 		}
143 		map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
144 							"xdp_tx_ports"));
145 		if (map_fd < 0) {
146 			printf("map not found: %s\n", strerror(map_fd));
147 			return 1;
148 		}
149 	}
150 
151 	for (i = optind; i < argc; ++i) {
152 		idx = if_nametoindex(argv[i]);
153 		if (!idx)
154 			idx = strtoul(argv[i], NULL, 0);
155 
156 		if (!idx) {
157 			fprintf(stderr, "Invalid arg\n");
158 			return 1;
159 		}
160 		if (!attach) {
161 			err = do_detach(idx, argv[i]);
162 			if (err)
163 				ret = err;
164 		} else {
165 			err = do_attach(idx, prog_fd, map_fd, argv[i]);
166 			if (err)
167 				ret = err;
168 		}
169 	}
170 
171 	return ret;
172 }
173