1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
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 <linux/if_link.h>
14 #include <assert.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <libgen.h>
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;
30 static bool ifindex_out_xdp_dummy_attached = true;
31
32 static __u32 xdp_flags;
33
int_exit(int sig)34 static void int_exit(int sig)
35 {
36 set_link_xdp_fd(ifindex_in, -1, xdp_flags);
37 if (ifindex_out_xdp_dummy_attached)
38 set_link_xdp_fd(ifindex_out, -1, xdp_flags);
39 exit(0);
40 }
41
poll_stats(int interval,int ifindex)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
usage(const char * prog)65 static void usage(const char *prog)
66 {
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 }
74
main(int argc,char ** argv)75 int main(int argc, char **argv)
76 {
77 const char *optstr = "SN";
78 char filename[256];
79 int ret, opt, key = 0;
80
81 while ((opt = getopt(argc, argv, optstr)) != -1) {
82 switch (opt) {
83 case 'S':
84 xdp_flags |= XDP_FLAGS_SKB_MODE;
85 break;
86 case 'N':
87 xdp_flags |= XDP_FLAGS_DRV_MODE;
88 break;
89 default:
90 usage(basename(argv[0]));
91 return 1;
92 }
93 }
94
95 if (optind == argc) {
96 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
97 return 1;
98 }
99
100 ifindex_in = strtoul(argv[optind], NULL, 0);
101 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
102 printf("input: %d output: %d\n", ifindex_in, ifindex_out);
103
104 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
105
106 if (load_bpf_file(filename)) {
107 printf("%s", bpf_log_buf);
108 return 1;
109 }
110
111 if (!prog_fd[0]) {
112 printf("load_bpf_file: %s\n", strerror(errno));
113 return 1;
114 }
115
116 if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
117 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
118 return 1;
119 }
120
121 /* Loading dummy XDP prog on out-device */
122 if (set_link_xdp_fd(ifindex_out, prog_fd[1],
123 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
124 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
125 ifindex_out_xdp_dummy_attached = false;
126 }
127
128 signal(SIGINT, int_exit);
129 signal(SIGTERM, int_exit);
130
131 printf("map[0] (vports) = %i, map[1] (map) = %i, map[2] (count) = %i\n",
132 map_fd[0], map_fd[1], map_fd[2]);
133
134 /* populate virtual to physical port map */
135 ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
136 if (ret) {
137 perror("bpf_update_elem");
138 goto out;
139 }
140
141 poll_stats(2, ifindex_out);
142
143 out:
144 return 0;
145 }
146