| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* 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 | 3 | */ |
|---|
| 7 | 4 | #include <linux/bpf.h> |
|---|
| 8 | 5 | #include <linux/if_link.h> |
|---|
| .. | .. |
|---|
| 15 | 12 | #include <unistd.h> |
|---|
| 16 | 13 | #include <libgen.h> |
|---|
| 17 | 14 | #include <sys/resource.h> |
|---|
| 15 | +#include <net/if.h> |
|---|
| 18 | 16 | |
|---|
| 19 | 17 | #include "bpf_util.h" |
|---|
| 20 | | -#include "bpf/bpf.h" |
|---|
| 21 | | -#include "bpf/libbpf.h" |
|---|
| 18 | +#include <bpf/bpf.h> |
|---|
| 19 | +#include <bpf/libbpf.h> |
|---|
| 22 | 20 | |
|---|
| 23 | 21 | static int ifindex; |
|---|
| 24 | | -static __u32 xdp_flags; |
|---|
| 22 | +static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
|---|
| 23 | +static __u32 prog_id; |
|---|
| 25 | 24 | |
|---|
| 26 | 25 | static void int_exit(int sig) |
|---|
| 27 | 26 | { |
|---|
| 28 | | - bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
|---|
| 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"); |
|---|
| 29 | 39 | exit(0); |
|---|
| 30 | 40 | } |
|---|
| 31 | 41 | |
|---|
| .. | .. |
|---|
| 34 | 44 | static void poll_stats(int map_fd, int interval) |
|---|
| 35 | 45 | { |
|---|
| 36 | 46 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
|---|
| 37 | | - const unsigned int nr_keys = 256; |
|---|
| 38 | | - __u64 values[nr_cpus], prev[nr_keys][nr_cpus]; |
|---|
| 39 | | - __u32 key; |
|---|
| 47 | + __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; |
|---|
| 40 | 48 | int i; |
|---|
| 41 | 49 | |
|---|
| 42 | | - memset(prev, 0, sizeof(prev)); |
|---|
| 43 | | - |
|---|
| 44 | 50 | while (1) { |
|---|
| 51 | + __u32 key = UINT32_MAX; |
|---|
| 52 | + |
|---|
| 45 | 53 | sleep(interval); |
|---|
| 46 | 54 | |
|---|
| 47 | | - for (key = 0; key < nr_keys; key++) { |
|---|
| 55 | + while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { |
|---|
| 48 | 56 | __u64 sum = 0; |
|---|
| 49 | 57 | |
|---|
| 50 | 58 | assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); |
|---|
| 51 | 59 | for (i = 0; i < nr_cpus; i++) |
|---|
| 52 | | - sum += (values[i] - prev[key][i]); |
|---|
| 53 | | - if (sum) |
|---|
| 60 | + sum += values[i]; |
|---|
| 61 | + if (sum > prev[key]) |
|---|
| 54 | 62 | printf("proto %u: %10llu pkt/s\n", |
|---|
| 55 | | - key, sum / interval); |
|---|
| 56 | | - memcpy(prev[key], values, sizeof(values)); |
|---|
| 63 | + key, (sum - prev[key]) / interval); |
|---|
| 64 | + prev[key] = sum; |
|---|
| 57 | 65 | } |
|---|
| 58 | 66 | } |
|---|
| 59 | 67 | } |
|---|
| .. | .. |
|---|
| 61 | 69 | static void usage(const char *prog) |
|---|
| 62 | 70 | { |
|---|
| 63 | 71 | fprintf(stderr, |
|---|
| 64 | | - "usage: %s [OPTS] IFINDEX\n\n" |
|---|
| 72 | + "usage: %s [OPTS] IFACE\n\n" |
|---|
| 65 | 73 | "OPTS:\n" |
|---|
| 66 | 74 | " -S use skb-mode\n" |
|---|
| 67 | | - " -N enforce native mode\n", |
|---|
| 75 | + " -N enforce native mode\n" |
|---|
| 76 | + " -F force loading prog\n", |
|---|
| 68 | 77 | prog); |
|---|
| 69 | 78 | } |
|---|
| 70 | 79 | |
|---|
| .. | .. |
|---|
| 74 | 83 | struct bpf_prog_load_attr prog_load_attr = { |
|---|
| 75 | 84 | .prog_type = BPF_PROG_TYPE_XDP, |
|---|
| 76 | 85 | }; |
|---|
| 77 | | - const char *optstr = "SN"; |
|---|
| 86 | + struct bpf_prog_info info = {}; |
|---|
| 87 | + __u32 info_len = sizeof(info); |
|---|
| 88 | + const char *optstr = "FSN"; |
|---|
| 78 | 89 | int prog_fd, map_fd, opt; |
|---|
| 79 | 90 | struct bpf_object *obj; |
|---|
| 80 | 91 | struct bpf_map *map; |
|---|
| 81 | 92 | char filename[256]; |
|---|
| 93 | + int err; |
|---|
| 82 | 94 | |
|---|
| 83 | 95 | while ((opt = getopt(argc, argv, optstr)) != -1) { |
|---|
| 84 | 96 | switch (opt) { |
|---|
| .. | .. |
|---|
| 86 | 98 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
|---|
| 87 | 99 | break; |
|---|
| 88 | 100 | case 'N': |
|---|
| 89 | | - xdp_flags |= XDP_FLAGS_DRV_MODE; |
|---|
| 101 | + /* default, set below */ |
|---|
| 102 | + break; |
|---|
| 103 | + case 'F': |
|---|
| 104 | + xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
|---|
| 90 | 105 | break; |
|---|
| 91 | 106 | default: |
|---|
| 92 | 107 | usage(basename(argv[0])); |
|---|
| 93 | 108 | return 1; |
|---|
| 94 | 109 | } |
|---|
| 95 | 110 | } |
|---|
| 111 | + |
|---|
| 112 | + if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
|---|
| 113 | + xdp_flags |= XDP_FLAGS_DRV_MODE; |
|---|
| 96 | 114 | |
|---|
| 97 | 115 | if (optind == argc) { |
|---|
| 98 | 116 | usage(basename(argv[0])); |
|---|
| .. | .. |
|---|
| 104 | 122 | return 1; |
|---|
| 105 | 123 | } |
|---|
| 106 | 124 | |
|---|
| 107 | | - ifindex = strtoul(argv[optind], NULL, 0); |
|---|
| 125 | + ifindex = if_nametoindex(argv[optind]); |
|---|
| 126 | + if (!ifindex) { |
|---|
| 127 | + perror("if_nametoindex"); |
|---|
| 128 | + return 1; |
|---|
| 129 | + } |
|---|
| 108 | 130 | |
|---|
| 109 | 131 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 110 | 132 | prog_load_attr.file = filename; |
|---|
| .. | .. |
|---|
| 120 | 142 | map_fd = bpf_map__fd(map); |
|---|
| 121 | 143 | |
|---|
| 122 | 144 | if (!prog_fd) { |
|---|
| 123 | | - printf("load_bpf_file: %s\n", strerror(errno)); |
|---|
| 145 | + printf("bpf_prog_load_xattr: %s\n", strerror(errno)); |
|---|
| 124 | 146 | return 1; |
|---|
| 125 | 147 | } |
|---|
| 126 | 148 | |
|---|
| .. | .. |
|---|
| 132 | 154 | return 1; |
|---|
| 133 | 155 | } |
|---|
| 134 | 156 | |
|---|
| 157 | + err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
|---|
| 158 | + if (err) { |
|---|
| 159 | + printf("can't get prog info - %s\n", strerror(errno)); |
|---|
| 160 | + return err; |
|---|
| 161 | + } |
|---|
| 162 | + prog_id = info.id; |
|---|
| 163 | + |
|---|
| 135 | 164 | poll_stats(map_fd, 2); |
|---|
| 136 | 165 | |
|---|
| 137 | 166 | return 0; |
|---|