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