Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2016 PLUMgrid |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 5 | #include <linux/if_link.h> |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 6 | #include <assert.h> |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 12 | #include <unistd.h> |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 13 | #include <libgen.h> |
Tushar Dave | 6dfca831 | 2017-10-27 16:12:30 -0700 | [diff] [blame] | 14 | #include <sys/resource.h> |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame] | 15 | #include <net/if.h> |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 16 | |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 17 | #include "bpf_util.h" |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 18 | #include <bpf/bpf.h> |
| 19 | #include <bpf/libbpf.h> |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 20 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 21 | static int ifindex; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 22 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 23 | static __u32 prog_id; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 24 | |
| 25 | static void int_exit(int sig) |
| 26 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 27 | __u32 curr_prog_id = 0; |
| 28 | |
| 29 | if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { |
| 30 | printf("bpf_get_link_xdp_id failed\n"); |
| 31 | exit(1); |
| 32 | } |
| 33 | if (prog_id == curr_prog_id) |
| 34 | bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
| 35 | else if (!curr_prog_id) |
| 36 | printf("couldn't find a prog id on a given interface\n"); |
| 37 | else |
| 38 | printf("program on interface changed, not removing\n"); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 39 | exit(0); |
| 40 | } |
| 41 | |
| 42 | /* simple per-protocol drop counter |
| 43 | */ |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 44 | static void poll_stats(int map_fd, int interval) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 45 | { |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 46 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 47 | __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 48 | int i; |
| 49 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 50 | while (1) { |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 51 | __u32 key = UINT32_MAX; |
| 52 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 53 | sleep(interval); |
| 54 | |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 55 | while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 56 | __u64 sum = 0; |
| 57 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 58 | assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 59 | for (i = 0; i < nr_cpus; i++) |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 60 | sum += values[i]; |
| 61 | if (sum > prev[key]) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 62 | printf("proto %u: %10llu pkt/s\n", |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 63 | key, (sum - prev[key]) / interval); |
| 64 | prev[key] = sum; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 69 | static void usage(const char *prog) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 70 | { |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 71 | fprintf(stderr, |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame] | 72 | "usage: %s [OPTS] IFACE\n\n" |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 73 | "OPTS:\n" |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 74 | " -S use skb-mode\n" |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 75 | " -N enforce native mode\n" |
| 76 | " -F force loading prog\n", |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 77 | prog); |
| 78 | } |
| 79 | |
| 80 | int main(int argc, char **argv) |
| 81 | { |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 82 | struct bpf_prog_load_attr prog_load_attr = { |
| 83 | .prog_type = BPF_PROG_TYPE_XDP, |
| 84 | }; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 85 | struct bpf_prog_info info = {}; |
| 86 | __u32 info_len = sizeof(info); |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 87 | const char *optstr = "FSN"; |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 88 | int prog_fd, map_fd, opt; |
| 89 | struct bpf_object *obj; |
| 90 | struct bpf_map *map; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 91 | char filename[256]; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 92 | int err; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 93 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 94 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 95 | switch (opt) { |
| 96 | case 'S': |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 97 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 98 | break; |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 99 | case 'N': |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 100 | /* default, set below */ |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 101 | break; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 102 | case 'F': |
| 103 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 104 | break; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 105 | default: |
| 106 | usage(basename(argv[0])); |
| 107 | return 1; |
| 108 | } |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 111 | if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
| 112 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 113 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 114 | if (optind == argc) { |
| 115 | usage(basename(argv[0])); |
| 116 | return 1; |
| 117 | } |
Tushar Dave | 6dfca831 | 2017-10-27 16:12:30 -0700 | [diff] [blame] | 118 | |
Ioana Ciornei | 11b36ab | 2019-01-09 17:06:55 +0000 | [diff] [blame] | 119 | ifindex = if_nametoindex(argv[optind]); |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame] | 120 | if (!ifindex) { |
| 121 | perror("if_nametoindex"); |
| 122 | return 1; |
| 123 | } |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 124 | |
| 125 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 126 | prog_load_attr.file = filename; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 127 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 128 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
| 129 | return 1; |
| 130 | |
| 131 | map = bpf_map__next(NULL, obj); |
| 132 | if (!map) { |
| 133 | printf("finding a map in obj file failed\n"); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 134 | return 1; |
| 135 | } |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 136 | map_fd = bpf_map__fd(map); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 137 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 138 | if (!prog_fd) { |
Daniel T. Lee | afbe3c2 | 2019-11-07 09:51:52 +0900 | [diff] [blame] | 139 | printf("bpf_prog_load_xattr: %s\n", strerror(errno)); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | signal(SIGINT, int_exit); |
Andy Gospodarek | ad990db | 2017-05-11 15:52:30 -0400 | [diff] [blame] | 144 | signal(SIGTERM, int_exit); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 145 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 146 | if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 147 | printf("link set xdp fd failed\n"); |
| 148 | return 1; |
| 149 | } |
| 150 | |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 151 | err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
| 152 | if (err) { |
| 153 | printf("can't get prog info - %s\n", strerror(errno)); |
| 154 | return err; |
| 155 | } |
| 156 | prog_id = info.id; |
| 157 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 158 | poll_stats(map_fd, 2); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |