Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <linux/perf_event.h> |
| 6 | #include <linux/bpf.h> |
| 7 | #include <net/if.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | #include <sys/sysinfo.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <signal.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 13 | #include <bpf/libbpf.h> |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 14 | #include <bpf/bpf.h> |
Maciej Fijalkowski | 6a54576 | 2019-02-01 22:42:26 +0100 | [diff] [blame] | 15 | #include <sys/resource.h> |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 16 | #include <libgen.h> |
| 17 | #include <linux/if_link.h> |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include "perf-sys.h" |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 20 | |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 21 | static int if_idx; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 22 | static char *if_name; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 23 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 24 | static __u32 prog_id; |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 25 | static struct perf_buffer *pb = NULL; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 26 | |
| 27 | static int do_attach(int idx, int fd, const char *name) |
| 28 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 29 | struct bpf_prog_info info = {}; |
| 30 | __u32 info_len = sizeof(info); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 31 | int err; |
| 32 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 33 | err = bpf_set_link_xdp_fd(idx, fd, xdp_flags); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 34 | if (err < 0) { |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 35 | printf("ERROR: failed to attach program to %s\n", name); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 36 | return err; |
| 37 | } |
| 38 | |
| 39 | err = bpf_obj_get_info_by_fd(fd, &info, &info_len); |
| 40 | if (err) { |
| 41 | printf("can't get prog info - %s\n", strerror(errno)); |
| 42 | return err; |
| 43 | } |
| 44 | prog_id = info.id; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | static int do_detach(int idx, const char *name) |
| 50 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 51 | __u32 curr_prog_id = 0; |
| 52 | int err = 0; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 53 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 54 | err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 55 | if (err) { |
| 56 | printf("bpf_get_link_xdp_id failed\n"); |
| 57 | return err; |
| 58 | } |
| 59 | if (prog_id == curr_prog_id) { |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 60 | err = bpf_set_link_xdp_fd(idx, -1, xdp_flags); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 61 | if (err < 0) |
| 62 | printf("ERROR: failed to detach prog from %s\n", name); |
| 63 | } else if (!curr_prog_id) { |
| 64 | printf("couldn't find a prog id on a %s\n", name); |
| 65 | } else { |
| 66 | printf("program on interface changed, not removing\n"); |
| 67 | } |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 68 | |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | #define SAMPLE_SIZE 64 |
| 73 | |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 74 | static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size) |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 75 | { |
| 76 | struct { |
| 77 | __u16 cookie; |
| 78 | __u16 pkt_len; |
| 79 | __u8 pkt_data[SAMPLE_SIZE]; |
| 80 | } __packed *e = data; |
| 81 | int i; |
| 82 | |
| 83 | if (e->cookie != 0xdead) { |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 84 | printf("BUG cookie %x sized %d\n", e->cookie, size); |
| 85 | return; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); |
| 89 | for (i = 0; i < 14 && i < e->pkt_len; i++) |
| 90 | printf("%02x ", e->pkt_data[i]); |
| 91 | printf("\n"); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void sig_handler(int signo) |
| 95 | { |
| 96 | do_detach(if_idx, if_name); |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 97 | perf_buffer__free(pb); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 98 | exit(0); |
| 99 | } |
| 100 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 101 | static void usage(const char *prog) |
| 102 | { |
| 103 | fprintf(stderr, |
| 104 | "%s: %s [OPTS] <ifname|ifindex>\n\n" |
| 105 | "OPTS:\n" |
Wang Hai | dfdda1a | 2021-06-15 21:57:24 +0800 | [diff] [blame] | 106 | " -F force loading prog\n" |
| 107 | " -S use skb-mode\n", |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 108 | __func__, prog); |
| 109 | } |
| 110 | |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 111 | int main(int argc, char **argv) |
| 112 | { |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 113 | const char *optstr = "FS"; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 114 | int prog_fd, map_fd, opt; |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 115 | struct bpf_program *prog; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 116 | struct bpf_object *obj; |
| 117 | struct bpf_map *map; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 118 | char filename[256]; |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 119 | int ret, err; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 120 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 121 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 122 | switch (opt) { |
| 123 | case 'F': |
| 124 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 125 | break; |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 126 | case 'S': |
| 127 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
| 128 | break; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 129 | default: |
| 130 | usage(basename(argv[0])); |
| 131 | return 1; |
| 132 | } |
| 133 | } |
| 134 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 135 | if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
| 136 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 137 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 138 | if (optind == argc) { |
| 139 | usage(basename(argv[0])); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 140 | return 1; |
| 141 | } |
| 142 | |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 143 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 144 | |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 145 | obj = bpf_object__open_file(filename, NULL); |
| 146 | if (libbpf_get_error(obj)) |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 147 | return 1; |
| 148 | |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 149 | prog = bpf_object__next_program(obj, NULL); |
| 150 | bpf_program__set_type(prog, BPF_PROG_TYPE_XDP); |
| 151 | |
| 152 | err = bpf_object__load(obj); |
| 153 | if (err) |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 154 | return 1; |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 155 | |
| 156 | prog_fd = bpf_program__fd(prog); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 157 | |
Hengqi Chen | 6f2b219 | 2021-10-04 00:58:44 +0800 | [diff] [blame] | 158 | map = bpf_object__next_map(obj, NULL); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 159 | if (!map) { |
| 160 | printf("finding a map in obj file failed\n"); |
| 161 | return 1; |
| 162 | } |
| 163 | map_fd = bpf_map__fd(map); |
| 164 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 165 | if_idx = if_nametoindex(argv[optind]); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 166 | if (!if_idx) |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 167 | if_idx = strtoul(argv[optind], NULL, 0); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 168 | |
| 169 | if (!if_idx) { |
| 170 | fprintf(stderr, "Invalid ifname\n"); |
| 171 | return 1; |
| 172 | } |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 173 | if_name = argv[optind]; |
| 174 | err = do_attach(if_idx, prog_fd, if_name); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 175 | if (err) |
| 176 | return err; |
| 177 | |
| 178 | if (signal(SIGINT, sig_handler) || |
| 179 | signal(SIGHUP, sig_handler) || |
| 180 | signal(SIGTERM, sig_handler)) { |
| 181 | perror("signal"); |
| 182 | return 1; |
| 183 | } |
| 184 | |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 185 | pb = perf_buffer__new(map_fd, 8, print_bpf_output, NULL, NULL, NULL); |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 186 | err = libbpf_get_error(pb); |
| 187 | if (err) { |
| 188 | perror("perf_buffer setup failed"); |
| 189 | return 1; |
| 190 | } |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 191 | |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 192 | while ((ret = perf_buffer__poll(pb, 1000)) >= 0) { |
| 193 | } |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 194 | |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 195 | kill(0, SIGINT); |
| 196 | return ret; |
| 197 | } |