0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/bpf.h>
0015 #include <linux/if_link.h>
0016 #include <linux/limits.h>
0017 #include <net/if.h>
0018 #include <errno.h>
0019 #include <stdio.h>
0020 #include <stdlib.h>
0021 #include <stdbool.h>
0022 #include <string.h>
0023 #include <unistd.h>
0024 #include <fcntl.h>
0025 #include <libgen.h>
0026
0027 #include <bpf/libbpf.h>
0028 #include <bpf/bpf.h>
0029
0030 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0031
0032 static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
0033 {
0034 int err;
0035
0036 err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL);
0037 if (err < 0) {
0038 printf("ERROR: failed to attach program to %s\n", name);
0039 return err;
0040 }
0041
0042
0043 err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
0044 if (err)
0045 printf("ERROR: failed using device %s as TX-port\n", name);
0046
0047 return err;
0048 }
0049
0050 static int do_detach(int ifindex, const char *ifname, const char *app_name)
0051 {
0052 LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
0053 struct bpf_prog_info prog_info = {};
0054 char prog_name[BPF_OBJ_NAME_LEN];
0055 __u32 info_len, curr_prog_id;
0056 int prog_fd;
0057 int err = 1;
0058
0059 if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
0060 printf("ERROR: bpf_xdp_query_id failed (%s)\n",
0061 strerror(errno));
0062 return err;
0063 }
0064
0065 if (!curr_prog_id) {
0066 printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
0067 xdp_flags, ifname);
0068 return err;
0069 }
0070
0071 info_len = sizeof(prog_info);
0072 prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
0073 if (prog_fd < 0) {
0074 printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
0075 strerror(errno));
0076 return prog_fd;
0077 }
0078
0079 err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
0080 if (err) {
0081 printf("ERROR: bpf_obj_get_info_by_fd failed (%s)\n",
0082 strerror(errno));
0083 goto close_out;
0084 }
0085 snprintf(prog_name, sizeof(prog_name), "%s_prog", app_name);
0086 prog_name[BPF_OBJ_NAME_LEN - 1] = '\0';
0087
0088 if (strcmp(prog_info.name, prog_name)) {
0089 printf("ERROR: %s isn't attached to %s\n", app_name, ifname);
0090 err = 1;
0091 goto close_out;
0092 }
0093
0094 opts.old_prog_fd = prog_fd;
0095 err = bpf_xdp_detach(ifindex, xdp_flags, &opts);
0096 if (err < 0)
0097 printf("ERROR: failed to detach program from %s (%s)\n",
0098 ifname, strerror(errno));
0099
0100
0101
0102 close_out:
0103 close(prog_fd);
0104 return err;
0105 }
0106
0107 static void usage(const char *prog)
0108 {
0109 fprintf(stderr,
0110 "usage: %s [OPTS] interface-list\n"
0111 "\nOPTS:\n"
0112 " -d detach program\n"
0113 " -S use skb-mode\n"
0114 " -F force loading prog\n"
0115 " -D direct table lookups (skip fib rules)\n",
0116 prog);
0117 }
0118
0119 int main(int argc, char **argv)
0120 {
0121 const char *prog_name = "xdp_fwd";
0122 struct bpf_program *prog = NULL;
0123 struct bpf_program *pos;
0124 const char *sec_name;
0125 int prog_fd = -1, map_fd = -1;
0126 char filename[PATH_MAX];
0127 struct bpf_object *obj;
0128 int opt, i, idx, err;
0129 int attach = 1;
0130 int ret = 0;
0131
0132 while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
0133 switch (opt) {
0134 case 'd':
0135 attach = 0;
0136 break;
0137 case 'S':
0138 xdp_flags |= XDP_FLAGS_SKB_MODE;
0139 break;
0140 case 'F':
0141 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0142 break;
0143 case 'D':
0144 prog_name = "xdp_fwd_direct";
0145 break;
0146 default:
0147 usage(basename(argv[0]));
0148 return 1;
0149 }
0150 }
0151
0152 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0153 xdp_flags |= XDP_FLAGS_DRV_MODE;
0154
0155 if (optind == argc) {
0156 usage(basename(argv[0]));
0157 return 1;
0158 }
0159
0160 if (attach) {
0161 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0162
0163 if (access(filename, O_RDONLY) < 0) {
0164 printf("error accessing file %s: %s\n",
0165 filename, strerror(errno));
0166 return 1;
0167 }
0168
0169 obj = bpf_object__open_file(filename, NULL);
0170 if (libbpf_get_error(obj))
0171 return 1;
0172
0173 prog = bpf_object__next_program(obj, NULL);
0174 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0175
0176 err = bpf_object__load(obj);
0177 if (err) {
0178 printf("Does kernel support devmap lookup?\n");
0179
0180
0181
0182 return 1;
0183 }
0184
0185 bpf_object__for_each_program(pos, obj) {
0186 sec_name = bpf_program__section_name(pos);
0187 if (sec_name && !strcmp(sec_name, prog_name)) {
0188 prog = pos;
0189 break;
0190 }
0191 }
0192 prog_fd = bpf_program__fd(prog);
0193 if (prog_fd < 0) {
0194 printf("program not found: %s\n", strerror(prog_fd));
0195 return 1;
0196 }
0197 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
0198 "xdp_tx_ports"));
0199 if (map_fd < 0) {
0200 printf("map not found: %s\n", strerror(map_fd));
0201 return 1;
0202 }
0203 }
0204
0205 for (i = optind; i < argc; ++i) {
0206 idx = if_nametoindex(argv[i]);
0207 if (!idx)
0208 idx = strtoul(argv[i], NULL, 0);
0209
0210 if (!idx) {
0211 fprintf(stderr, "Invalid arg\n");
0212 return 1;
0213 }
0214 if (!attach) {
0215 err = do_detach(idx, argv[i], prog_name);
0216 if (err)
0217 ret = err;
0218 } else {
0219 err = do_attach(idx, prog_fd, map_fd, argv[i]);
0220 if (err)
0221 ret = err;
0222 }
0223 }
0224
0225 return ret;
0226 }