1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
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.
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.
12 #include <linux/bpf.h>
13 #include <linux/if_link.h>
23 #include <sys/resource.h>
29 static int ifindex_in
;
30 static int ifindex_out
;
31 static bool ifindex_out_xdp_dummy_attached
= true;
33 static __u32 xdp_flags
;
35 static void int_exit(int sig
)
37 set_link_xdp_fd(ifindex_in
, -1, xdp_flags
);
38 if (ifindex_out_xdp_dummy_attached
)
39 set_link_xdp_fd(ifindex_out
, -1, xdp_flags
);
43 static void poll_stats(int interval
, int ifindex
)
45 unsigned int nr_cpus
= bpf_num_possible_cpus();
46 __u64 values
[nr_cpus
], prev
[nr_cpus
];
48 memset(prev
, 0, sizeof(prev
));
56 assert(bpf_map_lookup_elem(map_fd
[1], &key
, values
) == 0);
57 for (i
= 0; i
< nr_cpus
; i
++)
58 sum
+= (values
[i
] - prev
[i
]);
60 printf("ifindex %i: %10llu pkt/s\n",
61 ifindex
, sum
/ interval
);
62 memcpy(prev
, values
, sizeof(values
));
66 static void usage(const char *prog
)
69 "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
72 " -N enforce native mode\n",
76 int main(int argc
, char **argv
)
78 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
79 const char *optstr
= "SN";
81 int ret
, opt
, key
= 0;
83 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
86 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
89 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
92 usage(basename(argv
[0]));
98 printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv
[0]);
102 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
103 perror("setrlimit(RLIMIT_MEMLOCK)");
107 ifindex_in
= strtoul(argv
[optind
], NULL
, 0);
108 ifindex_out
= strtoul(argv
[optind
+ 1], NULL
, 0);
109 printf("input: %d output: %d\n", ifindex_in
, ifindex_out
);
111 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
113 if (load_bpf_file(filename
)) {
114 printf("%s", bpf_log_buf
);
119 printf("load_bpf_file: %s\n", strerror(errno
));
123 if (set_link_xdp_fd(ifindex_in
, prog_fd
[0], xdp_flags
) < 0) {
124 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in
);
128 /* Loading dummy XDP prog on out-device */
129 if (set_link_xdp_fd(ifindex_out
, prog_fd
[1],
130 (xdp_flags
| XDP_FLAGS_UPDATE_IF_NOEXIST
)) < 0) {
131 printf("WARN: link set xdp fd failed on %d\n", ifindex_out
);
132 ifindex_out_xdp_dummy_attached
= false;
135 signal(SIGINT
, int_exit
);
136 signal(SIGTERM
, int_exit
);
138 printf("map[0] (vports) = %i, map[1] (map) = %i, map[2] (count) = %i\n",
139 map_fd
[0], map_fd
[1], map_fd
[2]);
141 /* populate virtual to physical port map */
142 ret
= bpf_map_update_elem(map_fd
[0], &key
, &ifindex_out
, 0);
144 perror("bpf_update_elem");
148 poll_stats(2, ifindex_out
);