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> |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 17 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 18 | #include "bpf_load.h" |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 19 | #include "bpf_util.h" |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 20 | #include "libbpf.h" |
| 21 | |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 22 | static int ifindex; |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame^] | 23 | static __u32 xdp_flags; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 24 | |
| 25 | static void int_exit(int sig) |
| 26 | { |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame^] | 27 | set_link_xdp_fd(ifindex, -1, xdp_flags); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 28 | exit(0); |
| 29 | } |
| 30 | |
| 31 | /* simple per-protocol drop counter |
| 32 | */ |
| 33 | static void poll_stats(int interval) |
| 34 | { |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 35 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 36 | const unsigned int nr_keys = 256; |
| 37 | __u64 values[nr_cpus], prev[nr_keys][nr_cpus]; |
| 38 | __u32 key; |
| 39 | int i; |
| 40 | |
| 41 | memset(prev, 0, sizeof(prev)); |
| 42 | |
| 43 | while (1) { |
| 44 | sleep(interval); |
| 45 | |
| 46 | for (key = 0; key < nr_keys; key++) { |
| 47 | __u64 sum = 0; |
| 48 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 49 | assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 50 | for (i = 0; i < nr_cpus; i++) |
| 51 | sum += (values[i] - prev[key][i]); |
| 52 | if (sum) |
| 53 | printf("proto %u: %10llu pkt/s\n", |
| 54 | key, sum / interval); |
| 55 | memcpy(prev[key], values, sizeof(values)); |
| 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, |
| 63 | "usage: %s [OPTS] IFINDEX\n\n" |
| 64 | "OPTS:\n" |
| 65 | " -S use skb-mode\n", |
| 66 | prog); |
| 67 | } |
| 68 | |
| 69 | int main(int argc, char **argv) |
| 70 | { |
| 71 | const char *optstr = "S"; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 72 | char filename[256]; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 73 | int opt; |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 74 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 75 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 76 | switch (opt) { |
| 77 | case 'S': |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame^] | 78 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | usage(basename(argv[0])); |
| 82 | return 1; |
| 83 | } |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Ahern | 3993f2c | 2017-04-27 09:11:13 -0700 | [diff] [blame] | 86 | if (optind == argc) { |
| 87 | usage(basename(argv[0])); |
| 88 | return 1; |
| 89 | } |
| 90 | ifindex = strtoul(argv[optind], NULL, 0); |
| 91 | |
| 92 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 93 | |
| 94 | if (load_bpf_file(filename)) { |
| 95 | printf("%s", bpf_log_buf); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | if (!prog_fd[0]) { |
| 100 | printf("load_bpf_file: %s\n", strerror(errno)); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | signal(SIGINT, int_exit); |
| 105 | |
Jesper Dangaard Brouer | 6387d01 | 2017-05-01 11:26:15 +0200 | [diff] [blame^] | 106 | if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { |
Brenden Blanco | 86af8b4 | 2016-07-19 12:16:51 -0700 | [diff] [blame] | 107 | printf("link set xdp fd failed\n"); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | poll_stats(2); |
| 112 | |
| 113 | return 0; |
| 114 | } |