Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2016 PLUMgrid |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | */ |
| 7 | #include <linux/bpf.h> |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 8 | #include <linux/if_link.h> |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <errno.h> |
| 11 | #include <signal.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 15 | #include <unistd.h> |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 16 | #include <libgen.h> |
Tushar Dave | 6dfca831 | 2017-10-27 16:12:30 -0700 | [diff] [blame] | 17 | #include <sys/resource.h> |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame^] | 18 | #include <net/if.h> |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 19 | |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 20 | #include "bpf_util.h" |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 21 | #include "bpf/bpf.h" |
| 22 | #include "bpf/libbpf.h" |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 23 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 24 | static int ifindex; |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 25 | static __u32 xdp_flags; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 26 | |
| 27 | static void int_exit(int sig) |
| 28 | { |
Eric Leblond | b259c2f | 2018-01-30 21:55:04 +0100 | [diff] [blame] | 29 | bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 30 | exit(0); |
| 31 | } |
| 32 | |
| 33 | /* simple per-protocol drop counter |
| 34 | */ |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 35 | static void poll_stats(int map_fd, int interval) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 36 | { |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 37 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 38 | __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 39 | int i; |
| 40 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 41 | while (1) { |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 42 | __u32 key = UINT32_MAX; |
| 43 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 44 | sleep(interval); |
| 45 | |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 46 | while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 47 | __u64 sum = 0; |
| 48 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 49 | assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 50 | for (i = 0; i < nr_cpus; i++) |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 51 | sum += values[i]; |
| 52 | if (sum > prev[key]) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 53 | printf("proto %u: %10llu pkt/s\n", |
Matteo Croce | d606ee5 | 2018-12-01 01:23:05 +0100 | [diff] [blame] | 54 | key, (sum - prev[key]) / interval); |
| 55 | prev[key] = sum; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 60 | static void usage(const char *prog) |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 61 | { |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 62 | fprintf(stderr, |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame^] | 63 | "usage: %s [OPTS] IFACE\n\n" |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 64 | "OPTS:\n" |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 65 | " -S use skb-mode\n" |
| 66 | " -N enforce native mode\n", |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 67 | prog); |
| 68 | } |
| 69 | |
| 70 | int main(int argc, char **argv) |
| 71 | { |
Tushar Dave | 6dfca831 | 2017-10-27 16:12:30 -0700 | [diff] [blame] | 72 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 73 | struct bpf_prog_load_attr prog_load_attr = { |
| 74 | .prog_type = BPF_PROG_TYPE_XDP, |
| 75 | }; |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 76 | const char *optstr = "SN"; |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 77 | int prog_fd, map_fd, opt; |
| 78 | struct bpf_object *obj; |
| 79 | struct bpf_map *map; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 80 | char filename[256]; |
| 81 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 82 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 83 | switch (opt) { |
| 84 | case 'S': |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame] | 85 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 86 | break; |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 87 | case 'N': |
| 88 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 89 | break; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 90 | default: |
| 91 | usage(basename(argv[0])); |
| 92 | return 1; |
| 93 | } |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 94 | } |
| 95 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 96 | if (optind == argc) { |
| 97 | usage(basename(argv[0])); |
| 98 | return 1; |
| 99 | } |
Tushar Dave | 6dfca831 | 2017-10-27 16:12:30 -0700 | [diff] [blame] | 100 | |
| 101 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 102 | perror("setrlimit(RLIMIT_MEMLOCK)"); |
| 103 | return 1; |
| 104 | } |
| 105 | |
Matteo Croce | dc378a1a | 2018-12-01 01:23:06 +0100 | [diff] [blame^] | 106 | ifindex = if_nametoindex(argv[1]); |
| 107 | if (!ifindex) { |
| 108 | perror("if_nametoindex"); |
| 109 | return 1; |
| 110 | } |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 111 | |
| 112 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 113 | prog_load_attr.file = filename; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 114 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 115 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
| 116 | return 1; |
| 117 | |
| 118 | map = bpf_map__next(NULL, obj); |
| 119 | if (!map) { |
| 120 | printf("finding a map in obj file failed\n"); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 121 | return 1; |
| 122 | } |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 123 | map_fd = bpf_map__fd(map); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 124 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 125 | if (!prog_fd) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 126 | printf("load_bpf_file: %s\n", strerror(errno)); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | signal(SIGINT, int_exit); |
Andy Gospodarek | ad990db | 2017-05-11 15:52:30 -0400 | [diff] [blame] | 131 | signal(SIGTERM, int_exit); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 132 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 133 | if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 134 | printf("link set xdp fd failed\n"); |
| 135 | return 1; |
| 136 | } |
| 137 | |
Jakub Kicinski | be5bca4 | 2018-05-10 10:24:43 -0700 | [diff] [blame] | 138 | poll_stats(map_fd, 2); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |