Commit | Line | Data |
---|---|---|
25763b3c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
86af8b41 | 2 | /* Copyright (c) 2016 PLUMgrid |
86af8b41 BB |
3 | */ |
4 | #include <linux/bpf.h> | |
3993f2cb | 5 | #include <linux/if_link.h> |
86af8b41 BB |
6 | #include <assert.h> |
7 | #include <errno.h> | |
8 | #include <signal.h> | |
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
86af8b41 | 12 | #include <unistd.h> |
3993f2cb | 13 | #include <libgen.h> |
6dfca831 | 14 | #include <sys/resource.h> |
dc378a1a | 15 | #include <net/if.h> |
e00c7b21 | 16 | |
e00c7b21 | 17 | #include "bpf_util.h" |
be5bca44 JK |
18 | #include "bpf/bpf.h" |
19 | #include "bpf/libbpf.h" | |
86af8b41 | 20 | |
86af8b41 | 21 | static int ifindex; |
743e568c | 22 | static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
3b7a8ec2 | 23 | static __u32 prog_id; |
86af8b41 BB |
24 | |
25 | static void int_exit(int sig) | |
26 | { | |
3b7a8ec2 MF |
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"); | |
86af8b41 BB |
39 | exit(0); |
40 | } | |
41 | ||
42 | /* simple per-protocol drop counter | |
43 | */ | |
be5bca44 | 44 | static void poll_stats(int map_fd, int interval) |
86af8b41 | 45 | { |
e00c7b21 | 46 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
d606ee5c | 47 | __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; |
86af8b41 BB |
48 | int i; |
49 | ||
86af8b41 | 50 | while (1) { |
d606ee5c MC |
51 | __u32 key = UINT32_MAX; |
52 | ||
86af8b41 BB |
53 | sleep(interval); |
54 | ||
d606ee5c | 55 | while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { |
86af8b41 BB |
56 | __u64 sum = 0; |
57 | ||
be5bca44 | 58 | assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); |
86af8b41 | 59 | for (i = 0; i < nr_cpus; i++) |
d606ee5c MC |
60 | sum += values[i]; |
61 | if (sum > prev[key]) | |
86af8b41 | 62 | printf("proto %u: %10llu pkt/s\n", |
d606ee5c MC |
63 | key, (sum - prev[key]) / interval); |
64 | prev[key] = sum; | |
86af8b41 BB |
65 | } |
66 | } | |
67 | } | |
68 | ||
3993f2cb | 69 | static void usage(const char *prog) |
86af8b41 | 70 | { |
3993f2cb | 71 | fprintf(stderr, |
dc378a1a | 72 | "usage: %s [OPTS] IFACE\n\n" |
3993f2cb | 73 | "OPTS:\n" |
0489df9a | 74 | " -S use skb-mode\n" |
743e568c MF |
75 | " -N enforce native mode\n" |
76 | " -F force loading prog\n", | |
3993f2cb DA |
77 | prog); |
78 | } | |
86af8b41 | 79 | |
3993f2cb DA |
80 | int main(int argc, char **argv) |
81 | { | |
6dfca831 | 82 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
be5bca44 JK |
83 | struct bpf_prog_load_attr prog_load_attr = { |
84 | .prog_type = BPF_PROG_TYPE_XDP, | |
85 | }; | |
3b7a8ec2 MF |
86 | struct bpf_prog_info info = {}; |
87 | __u32 info_len = sizeof(info); | |
743e568c | 88 | const char *optstr = "FSN"; |
be5bca44 JK |
89 | int prog_fd, map_fd, opt; |
90 | struct bpf_object *obj; | |
91 | struct bpf_map *map; | |
3993f2cb | 92 | char filename[256]; |
3b7a8ec2 | 93 | int err; |
3993f2cb DA |
94 | |
95 | while ((opt = getopt(argc, argv, optstr)) != -1) { | |
96 | switch (opt) { | |
97 | case 'S': | |
6387d011 | 98 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
3993f2cb | 99 | break; |
0489df9a DB |
100 | case 'N': |
101 | xdp_flags |= XDP_FLAGS_DRV_MODE; | |
102 | break; | |
743e568c MF |
103 | case 'F': |
104 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; | |
105 | break; | |
3993f2cb DA |
106 | default: |
107 | usage(basename(argv[0])); | |
108 | return 1; | |
109 | } | |
110 | } | |
86af8b41 | 111 | |
3993f2cb DA |
112 | if (optind == argc) { |
113 | usage(basename(argv[0])); | |
86af8b41 BB |
114 | return 1; |
115 | } | |
6dfca831 TD |
116 | |
117 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { | |
118 | perror("setrlimit(RLIMIT_MEMLOCK)"); | |
119 | return 1; | |
120 | } | |
121 | ||
11b36abc | 122 | ifindex = if_nametoindex(argv[optind]); |
dc378a1a MC |
123 | if (!ifindex) { |
124 | perror("if_nametoindex"); | |
125 | return 1; | |
126 | } | |
86af8b41 | 127 | |
3993f2cb | 128 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
be5bca44 | 129 | prog_load_attr.file = filename; |
86af8b41 | 130 | |
be5bca44 JK |
131 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
132 | return 1; | |
133 | ||
134 | map = bpf_map__next(NULL, obj); | |
135 | if (!map) { | |
136 | printf("finding a map in obj file failed\n"); | |
86af8b41 BB |
137 | return 1; |
138 | } | |
be5bca44 | 139 | map_fd = bpf_map__fd(map); |
86af8b41 | 140 | |
be5bca44 | 141 | if (!prog_fd) { |
86af8b41 BB |
142 | printf("load_bpf_file: %s\n", strerror(errno)); |
143 | return 1; | |
144 | } | |
145 | ||
146 | signal(SIGINT, int_exit); | |
ad990dbe | 147 | signal(SIGTERM, int_exit); |
86af8b41 | 148 | |
be5bca44 | 149 | if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { |
86af8b41 BB |
150 | printf("link set xdp fd failed\n"); |
151 | return 1; | |
152 | } | |
153 | ||
3b7a8ec2 MF |
154 | err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
155 | if (err) { | |
156 | printf("can't get prog info - %s\n", strerror(errno)); | |
157 | return err; | |
158 | } | |
159 | prog_id = info.id; | |
160 | ||
be5bca44 | 161 | poll_stats(map_fd, 2); |
86af8b41 BB |
162 | |
163 | return 0; | |
164 | } |