blob: b02c531510ed9a0b88963f749a5d32e8dc0b3c16 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* Copyright (c) 2016 PLUMgrid
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#include <linux/bpf.h>
8#include <linux/if_link.h>
9#include <assert.h>
10#include <errno.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16#include <libgen.h>
17#include <sys/resource.h>
18
19#include "bpf_util.h"
20#include "bpf/bpf.h"
21#include "bpf/libbpf.h"
22
23static int ifindex;
24static __u32 xdp_flags;
25
26static void int_exit(int sig)
27{
28 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
29 exit(0);
30}
31
32/* simple per-protocol drop counter
33 */
34static void poll_stats(int map_fd, int interval)
35{
36 unsigned int nr_cpus = bpf_num_possible_cpus();
37 const unsigned int nr_keys = 256;
38 __u64 values[nr_cpus], prev[nr_keys][nr_cpus];
39 __u32 key;
40 int i;
41
42 memset(prev, 0, sizeof(prev));
43
44 while (1) {
45 sleep(interval);
46
47 for (key = 0; key < nr_keys; key++) {
48 __u64 sum = 0;
49
50 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
51 for (i = 0; i < nr_cpus; i++)
52 sum += (values[i] - prev[key][i]);
53 if (sum)
54 printf("proto %u: %10llu pkt/s\n",
55 key, sum / interval);
56 memcpy(prev[key], values, sizeof(values));
57 }
58 }
59}
60
61static void usage(const char *prog)
62{
63 fprintf(stderr,
64 "usage: %s [OPTS] IFINDEX\n\n"
65 "OPTS:\n"
66 " -S use skb-mode\n"
67 " -N enforce native mode\n",
68 prog);
69}
70
71int main(int argc, char **argv)
72{
73 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
74 struct bpf_prog_load_attr prog_load_attr = {
75 .prog_type = BPF_PROG_TYPE_XDP,
76 };
77 const char *optstr = "SN";
78 int prog_fd, map_fd, opt;
79 struct bpf_object *obj;
80 struct bpf_map *map;
81 char filename[256];
82
83 while ((opt = getopt(argc, argv, optstr)) != -1) {
84 switch (opt) {
85 case 'S':
86 xdp_flags |= XDP_FLAGS_SKB_MODE;
87 break;
88 case 'N':
89 xdp_flags |= XDP_FLAGS_DRV_MODE;
90 break;
91 default:
92 usage(basename(argv[0]));
93 return 1;
94 }
95 }
96
97 if (optind == argc) {
98 usage(basename(argv[0]));
99 return 1;
100 }
101
102 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
103 perror("setrlimit(RLIMIT_MEMLOCK)");
104 return 1;
105 }
106
107 ifindex = strtoul(argv[optind], NULL, 0);
108
109 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
110 prog_load_attr.file = filename;
111
112 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
113 return 1;
114
115 map = bpf_map__next(NULL, obj);
116 if (!map) {
117 printf("finding a map in obj file failed\n");
118 return 1;
119 }
120 map_fd = bpf_map__fd(map);
121
122 if (!prog_fd) {
123 printf("load_bpf_file: %s\n", strerror(errno));
124 return 1;
125 }
126
127 signal(SIGINT, int_exit);
128 signal(SIGTERM, int_exit);
129
130 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
131 printf("link set xdp fd failed\n");
132 return 1;
133 }
134
135 poll_stats(map_fd, 2);
136
137 return 0;
138}