]>
Commit | Line | Data |
---|---|---|
832622e6 JF |
1 | /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com> |
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 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * General Public License for more details. | |
11 | */ | |
12 | #include <linux/bpf.h> | |
13 | #include <assert.h> | |
14 | #include <errno.h> | |
15 | #include <signal.h> | |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <unistd.h> | |
20 | ||
21 | #include "bpf_load.h" | |
22 | #include "bpf_util.h" | |
23 | #include "libbpf.h" | |
24 | ||
25 | static int ifindex_in; | |
26 | static int ifindex_out; | |
27 | ||
28 | static void int_exit(int sig) | |
29 | { | |
30 | set_link_xdp_fd(ifindex_in, -1, 0); | |
31 | exit(0); | |
32 | } | |
33 | ||
34 | /* simple per-protocol drop counter | |
35 | */ | |
36 | static void poll_stats(int interval, int ifindex) | |
37 | { | |
38 | unsigned int nr_cpus = bpf_num_possible_cpus(); | |
39 | __u64 values[nr_cpus], prev[nr_cpus]; | |
40 | ||
41 | memset(prev, 0, sizeof(prev)); | |
42 | ||
43 | while (1) { | |
44 | __u64 sum = 0; | |
45 | __u32 key = 0; | |
46 | int i; | |
47 | ||
48 | sleep(interval); | |
49 | assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0); | |
50 | for (i = 0; i < nr_cpus; i++) | |
51 | sum += (values[i] - prev[i]); | |
52 | if (sum) | |
53 | printf("ifindex %i: %10llu pkt/s\n", | |
54 | ifindex, sum / interval); | |
55 | memcpy(prev, values, sizeof(values)); | |
56 | } | |
57 | } | |
58 | ||
59 | int main(int ac, char **argv) | |
60 | { | |
61 | char filename[256]; | |
62 | int ret, key = 0; | |
63 | ||
64 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | |
65 | ||
66 | if (ac != 3) { | |
67 | printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]); | |
68 | return 1; | |
69 | } | |
70 | ||
71 | ifindex_in = strtoul(argv[1], NULL, 0); | |
72 | ifindex_out = strtoul(argv[2], NULL, 0); | |
73 | ||
74 | if (load_bpf_file(filename)) { | |
75 | printf("%s", bpf_log_buf); | |
76 | return 1; | |
77 | } | |
78 | ||
79 | if (!prog_fd[0]) { | |
80 | printf("load_bpf_file: %s\n", strerror(errno)); | |
81 | return 1; | |
82 | } | |
83 | ||
84 | signal(SIGINT, int_exit); | |
85 | ||
86 | if (set_link_xdp_fd(ifindex_in, prog_fd[0], 0) < 0) { | |
87 | printf("link set xdp fd failed\n"); | |
88 | return 1; | |
89 | } | |
90 | ||
91 | /* bpf redirect port */ | |
92 | ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0); | |
93 | if (ret) { | |
94 | perror("bpf_update_elem"); | |
95 | goto out; | |
96 | } | |
97 | ||
98 | poll_stats(2, ifindex_out); | |
99 | ||
100 | out: | |
101 | return 0; | |
102 | } |