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