blob: f88e1d7093d62d0c52fffa1da0a1f972d02561a8 [file] [log] [blame]
David Ahernfe616052018-05-09 20:34:27 -07001// 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
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070027#include "bpf/libbpf.h"
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070028#include <bpf/bpf.h>
David Ahernfe616052018-05-09 20:34:27 -070029
30
31static int do_attach(int idx, int fd, const char *name)
32{
33 int err;
34
35 err = bpf_set_link_xdp_fd(idx, fd, 0);
36 if (err < 0)
37 printf("ERROR: failed to attach program to %s\n", name);
38
39 return err;
40}
41
42static int do_detach(int idx, const char *name)
43{
44 int err;
45
46 err = bpf_set_link_xdp_fd(idx, -1, 0);
47 if (err < 0)
48 printf("ERROR: failed to detach program from %s\n", name);
49
50 return err;
51}
52
53static void usage(const char *prog)
54{
55 fprintf(stderr,
56 "usage: %s [OPTS] interface-list\n"
57 "\nOPTS:\n"
58 " -d detach program\n"
59 " -D direct table lookups (skip fib rules)\n",
60 prog);
61}
62
63int main(int argc, char **argv)
64{
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070065 struct bpf_prog_load_attr prog_load_attr = {
66 .prog_type = BPF_PROG_TYPE_XDP,
67 };
68 const char *prog_name = "xdp_fwd";
69 struct bpf_program *prog;
David Ahernfe616052018-05-09 20:34:27 -070070 char filename[PATH_MAX];
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070071 struct bpf_object *obj;
David Ahernfe616052018-05-09 20:34:27 -070072 int opt, i, idx, err;
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070073 int prog_fd, map_fd;
David Ahernfe616052018-05-09 20:34:27 -070074 int attach = 1;
75 int ret = 0;
76
77 while ((opt = getopt(argc, argv, ":dD")) != -1) {
78 switch (opt) {
79 case 'd':
80 attach = 0;
81 break;
82 case 'D':
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070083 prog_name = "xdp_fwd_direct";
David Ahernfe616052018-05-09 20:34:27 -070084 break;
85 default:
86 usage(basename(argv[0]));
87 return 1;
88 }
89 }
90
91 if (optind == argc) {
92 usage(basename(argv[0]));
93 return 1;
94 }
95
96 if (attach) {
97 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070098 prog_load_attr.file = filename;
David Ahernfe616052018-05-09 20:34:27 -070099
100 if (access(filename, O_RDONLY) < 0) {
101 printf("error accessing file %s: %s\n",
102 filename, strerror(errno));
103 return 1;
104 }
105
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700106 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
107 return 1;
108
109 prog = bpf_object__find_program_by_title(obj, prog_name);
110 prog_fd = bpf_program__fd(prog);
111 if (prog_fd < 0) {
112 printf("program not found: %s\n", strerror(prog_fd));
David Ahernfe616052018-05-09 20:34:27 -0700113 return 1;
114 }
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700115 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
116 "tx_port"));
117 if (map_fd < 0) {
118 printf("map not found: %s\n", strerror(map_fd));
David Ahernfe616052018-05-09 20:34:27 -0700119 return 1;
120 }
121 }
122 if (attach) {
123 for (i = 1; i < 64; ++i)
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700124 bpf_map_update_elem(map_fd, &i, &i, 0);
David Ahernfe616052018-05-09 20:34:27 -0700125 }
126
127 for (i = optind; i < argc; ++i) {
128 idx = if_nametoindex(argv[i]);
129 if (!idx)
130 idx = strtoul(argv[i], NULL, 0);
131
132 if (!idx) {
133 fprintf(stderr, "Invalid arg\n");
134 return 1;
135 }
136 if (!attach) {
137 err = do_detach(idx, argv[i]);
138 if (err)
139 ret = err;
140 } else {
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700141 err = do_attach(idx, prog_fd, argv[i]);
David Ahernfe616052018-05-09 20:34:27 -0700142 if (err)
143 ret = err;
144 }
145 }
146
147 return ret;
148}