]>
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> | |
24251c26 | 13 | #include <linux/if_link.h> |
832622e6 JF |
14 | #include <assert.h> |
15 | #include <errno.h> | |
16 | #include <signal.h> | |
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
306da4e6 | 19 | #include <stdbool.h> |
832622e6 JF |
20 | #include <string.h> |
21 | #include <unistd.h> | |
24251c26 | 22 | #include <libgen.h> |
832622e6 JF |
23 | |
24 | #include "bpf_load.h" | |
25 | #include "bpf_util.h" | |
26 | #include "libbpf.h" | |
27 | ||
28 | static int ifindex_in; | |
29 | static int ifindex_out; | |
306da4e6 | 30 | static bool ifindex_out_xdp_dummy_attached = true; |
832622e6 | 31 | |
24251c26 AG |
32 | static __u32 xdp_flags; |
33 | ||
832622e6 JF |
34 | static void int_exit(int sig) |
35 | { | |
24251c26 | 36 | set_link_xdp_fd(ifindex_in, -1, xdp_flags); |
306da4e6 JDB |
37 | if (ifindex_out_xdp_dummy_attached) |
38 | set_link_xdp_fd(ifindex_out, -1, xdp_flags); | |
832622e6 JF |
39 | exit(0); |
40 | } | |
41 | ||
832622e6 JF |
42 | static void poll_stats(int interval, int ifindex) |
43 | { | |
44 | unsigned int nr_cpus = bpf_num_possible_cpus(); | |
45 | __u64 values[nr_cpus], prev[nr_cpus]; | |
46 | ||
47 | memset(prev, 0, sizeof(prev)); | |
48 | ||
49 | while (1) { | |
50 | __u64 sum = 0; | |
51 | __u32 key = 0; | |
52 | int i; | |
53 | ||
54 | sleep(interval); | |
55 | assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0); | |
56 | for (i = 0; i < nr_cpus; i++) | |
57 | sum += (values[i] - prev[i]); | |
58 | if (sum) | |
59 | printf("ifindex %i: %10llu pkt/s\n", | |
60 | ifindex, sum / interval); | |
61 | memcpy(prev, values, sizeof(values)); | |
62 | } | |
63 | } | |
64 | ||
24251c26 | 65 | static void usage(const char *prog) |
832622e6 | 66 | { |
24251c26 AG |
67 | fprintf(stderr, |
68 | "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n" | |
69 | "OPTS:\n" | |
70 | " -S use skb-mode\n" | |
71 | " -N enforce native mode\n", | |
72 | prog); | |
73 | } | |
832622e6 | 74 | |
832622e6 | 75 | |
24251c26 AG |
76 | int main(int argc, char **argv) |
77 | { | |
78 | const char *optstr = "SN"; | |
79 | char filename[256]; | |
80 | int ret, opt, key = 0; | |
81 | ||
82 | while ((opt = getopt(argc, argv, optstr)) != -1) { | |
83 | switch (opt) { | |
84 | case 'S': | |
85 | xdp_flags |= XDP_FLAGS_SKB_MODE; | |
86 | break; | |
87 | case 'N': | |
88 | xdp_flags |= XDP_FLAGS_DRV_MODE; | |
89 | break; | |
90 | default: | |
91 | usage(basename(argv[0])); | |
92 | return 1; | |
93 | } | |
94 | } | |
95 | ||
96 | if (optind == argc) { | |
832622e6 JF |
97 | printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]); |
98 | return 1; | |
99 | } | |
100 | ||
24251c26 AG |
101 | ifindex_in = strtoul(argv[optind], NULL, 0); |
102 | ifindex_out = strtoul(argv[optind + 1], NULL, 0); | |
103 | printf("input: %d output: %d\n", ifindex_in, ifindex_out); | |
104 | ||
105 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | |
832622e6 JF |
106 | |
107 | if (load_bpf_file(filename)) { | |
108 | printf("%s", bpf_log_buf); | |
109 | return 1; | |
110 | } | |
111 | ||
112 | if (!prog_fd[0]) { | |
113 | printf("load_bpf_file: %s\n", strerror(errno)); | |
114 | return 1; | |
115 | } | |
116 | ||
24251c26 | 117 | if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) { |
306da4e6 | 118 | printf("ERROR: link set xdp fd failed on %d\n", ifindex_in); |
832622e6 JF |
119 | return 1; |
120 | } | |
121 | ||
306da4e6 JDB |
122 | /* Loading dummy XDP prog on out-device */ |
123 | if (set_link_xdp_fd(ifindex_out, prog_fd[1], | |
124 | (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) { | |
125 | printf("WARN: link set xdp fd failed on %d\n", ifindex_out); | |
126 | ifindex_out_xdp_dummy_attached = false; | |
127 | } | |
128 | ||
129 | signal(SIGINT, int_exit); | |
130 | signal(SIGTERM, int_exit); | |
131 | ||
832622e6 JF |
132 | /* bpf redirect port */ |
133 | ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0); | |
134 | if (ret) { | |
135 | perror("bpf_update_elem"); | |
136 | goto out; | |
137 | } | |
138 | ||
139 | poll_stats(2, ifindex_out); | |
140 | ||
141 | out: | |
142 | return 0; | |
143 | } |