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> |
| 13 | #include <libbpf.h> |
| 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 | |
| 21 | #define MAX_CPUS 128 |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 22 | static int if_idx; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 23 | static char *if_name; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 24 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 25 | static __u32 prog_id; |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 26 | static struct perf_buffer *pb = NULL; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 27 | |
| 28 | static int do_attach(int idx, int fd, const char *name) |
| 29 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 30 | struct bpf_prog_info info = {}; |
| 31 | __u32 info_len = sizeof(info); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 32 | int err; |
| 33 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 34 | err = bpf_set_link_xdp_fd(idx, fd, xdp_flags); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 35 | if (err < 0) { |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 36 | printf("ERROR: failed to attach program to %s\n", name); |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 37 | return err; |
| 38 | } |
| 39 | |
| 40 | err = bpf_obj_get_info_by_fd(fd, &info, &info_len); |
| 41 | if (err) { |
| 42 | printf("can't get prog info - %s\n", strerror(errno)); |
| 43 | return err; |
| 44 | } |
| 45 | prog_id = info.id; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 46 | |
| 47 | return err; |
| 48 | } |
| 49 | |
| 50 | static int do_detach(int idx, const char *name) |
| 51 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 52 | __u32 curr_prog_id = 0; |
| 53 | int err = 0; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 54 | |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 55 | err = bpf_get_link_xdp_id(idx, &curr_prog_id, 0); |
| 56 | if (err) { |
| 57 | printf("bpf_get_link_xdp_id failed\n"); |
| 58 | return err; |
| 59 | } |
| 60 | if (prog_id == curr_prog_id) { |
| 61 | err = bpf_set_link_xdp_fd(idx, -1, 0); |
| 62 | if (err < 0) |
| 63 | printf("ERROR: failed to detach prog from %s\n", name); |
| 64 | } else if (!curr_prog_id) { |
| 65 | printf("couldn't find a prog id on a %s\n", name); |
| 66 | } else { |
| 67 | printf("program on interface changed, not removing\n"); |
| 68 | } |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 69 | |
| 70 | return err; |
| 71 | } |
| 72 | |
| 73 | #define SAMPLE_SIZE 64 |
| 74 | |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 75 | 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] | 76 | { |
| 77 | struct { |
| 78 | __u16 cookie; |
| 79 | __u16 pkt_len; |
| 80 | __u8 pkt_data[SAMPLE_SIZE]; |
| 81 | } __packed *e = data; |
| 82 | int i; |
| 83 | |
| 84 | if (e->cookie != 0xdead) { |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 85 | printf("BUG cookie %x sized %d\n", e->cookie, size); |
| 86 | return; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); |
| 90 | for (i = 0; i < 14 && i < e->pkt_len; i++) |
| 91 | printf("%02x ", e->pkt_data[i]); |
| 92 | printf("\n"); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void sig_handler(int signo) |
| 96 | { |
| 97 | do_detach(if_idx, if_name); |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 98 | perf_buffer__free(pb); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 99 | exit(0); |
| 100 | } |
| 101 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 102 | static void usage(const char *prog) |
| 103 | { |
| 104 | fprintf(stderr, |
| 105 | "%s: %s [OPTS] <ifname|ifindex>\n\n" |
| 106 | "OPTS:\n" |
| 107 | " -F force loading prog\n", |
| 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 | { |
Maciej Fijalkowski | 6a54576 | 2019-02-01 22:42:26 +0100 | [diff] [blame] | 113 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 114 | struct bpf_prog_load_attr prog_load_attr = { |
| 115 | .prog_type = BPF_PROG_TYPE_XDP, |
| 116 | }; |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 117 | struct perf_buffer_opts pb_opts = {}; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 118 | const char *optstr = "F"; |
| 119 | int prog_fd, map_fd, opt; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 120 | struct bpf_object *obj; |
| 121 | struct bpf_map *map; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 122 | char filename[256]; |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 123 | int ret, err; |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 124 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 125 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 126 | switch (opt) { |
| 127 | case 'F': |
| 128 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 129 | break; |
| 130 | default: |
| 131 | usage(basename(argv[0])); |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (optind == argc) { |
| 137 | usage(basename(argv[0])); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | |
Maciej Fijalkowski | 6a54576 | 2019-02-01 22:42:26 +0100 | [diff] [blame] | 141 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 142 | perror("setrlimit(RLIMIT_MEMLOCK)"); |
| 143 | return 1; |
| 144 | } |
| 145 | |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 146 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 147 | prog_load_attr.file = filename; |
| 148 | |
| 149 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
| 150 | return 1; |
| 151 | |
| 152 | if (!prog_fd) { |
| 153 | printf("load_bpf_file: %s\n", strerror(errno)); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | map = bpf_map__next(NULL, obj); |
| 158 | if (!map) { |
| 159 | printf("finding a map in obj file failed\n"); |
| 160 | return 1; |
| 161 | } |
| 162 | map_fd = bpf_map__fd(map); |
| 163 | |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 164 | if_idx = if_nametoindex(argv[optind]); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 165 | if (!if_idx) |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 166 | if_idx = strtoul(argv[optind], NULL, 0); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 167 | |
| 168 | if (!if_idx) { |
| 169 | fprintf(stderr, "Invalid ifname\n"); |
| 170 | return 1; |
| 171 | } |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 172 | if_name = argv[optind]; |
| 173 | err = do_attach(if_idx, prog_fd, if_name); |
Toke Høiland-Jørgensen | 1e54ad2 | 2018-06-25 14:25:02 +0200 | [diff] [blame] | 174 | if (err) |
| 175 | return err; |
| 176 | |
| 177 | if (signal(SIGINT, sig_handler) || |
| 178 | signal(SIGHUP, sig_handler) || |
| 179 | signal(SIGTERM, sig_handler)) { |
| 180 | perror("signal"); |
| 181 | return 1; |
| 182 | } |
| 183 | |
Andrii Nakryiko | f58a4d5 | 2019-07-23 14:34:43 -0700 | [diff] [blame] | 184 | pb_opts.sample_cb = print_bpf_output; |
| 185 | pb = perf_buffer__new(map_fd, 8, &pb_opts); |
| 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 | } |