-
Notifications
You must be signed in to change notification settings - Fork 0
/
tccounter.c
211 lines (181 loc) · 4.82 KB
/
tccounter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2020 Facebook */
#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_link.h>
#include <bpf/bpf.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "tccounter.skel.h"
#include "tccounter.h"
static struct env {
char ifname[IF_NAMESIZE];
int ifindex;
int interval;
bool verbose;
} env = { "", -1, 1, 0 };
const char *argp_program_version = "tccounter 0.0";
const char *argp_program_bug_address = "<[email protected]>";
const char argp_program_doc[] =
"BPF tccounter demo application.\n"
"\n"
"Trace packets at the tc hook and report packet statistics.\n"
"\n"
"USAGE: ./tccounter [-v]\n";
static const struct argp_option opts[] = {
{ "device", 'd', "ifname", 0, "Attach to device" },
{ "interval", 'i', "seconds", 0, "Interval between reports" },
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
{},
};
static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'd':
if (strlen(arg) >= IF_NAMESIZE) {
fprintf(stderr, "ERR: --device name too long\n");
return ARGP_ERR_UNKNOWN;
}
strncpy(env.ifname, arg, IF_NAMESIZE);
env.ifindex = if_nametoindex(env.ifname);
if (env.ifindex == 0) {
fprintf(stderr,
"ERR: --device name unknown err(%d):%s\n",
errno, strerror(errno));
return ARGP_ERR_UNKNOWN;
}
break;
case 'i':
env.interval = atoi(arg);
break;
case 'v':
env.verbose = true;
break;
case ARGP_KEY_ARG:
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
.doc = argp_program_doc,
};
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level == LIBBPF_DEBUG && !env.verbose)
return 0;
return vfprintf(stderr, format, args);
}
static volatile bool exiting = false;
static void sig_handler(int sig)
{
exiting = true;
}
static void print_values(int map_fd)
{
struct key *cur_key = NULL;
struct key next_key;
int next;
do {
next = bpf_map_get_next_key(map_fd, cur_key, &next_key);
if (next == -ENOENT)
break;
if (next < 0) {
fprintf(stderr, "bpf_map_get_next_key %d returned %s\n", map_fd, strerror(-next));
break;
}
struct in_addr src_addr = {
.s_addr = next_key.srcip
};
char *src_ip = inet_ntoa(src_addr);
struct value value;
int ret = bpf_map_lookup_elem(map_fd, &next_key, &value);
if (ret < 0) {
fprintf(stderr, "Failed to lookup elem with key %s: %s\n", src_ip, strerror(-ret));
break;
}
printf("%s: %lld packets, %lld bytes\n", src_ip, value.packets, value.bytes);
cur_key = &next_key;
} while (next == 0);
}
int main(int argc, char **argv)
{
struct tccounter_bpf *skel;
int err;
/* Parse command line arguments */
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);
/* Cleaner handling of Ctrl-C */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
/* Load and verify BPF application */
skel = tccounter_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open and load BPF skeleton\n");
return 1;
}
/* Load & verify BPF programs */
err = tccounter_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}
/* Attach tracepoints */
err = tccounter_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto cleanup;
}
int prog_fd = bpf_program__fd(skel->progs.count_packets);
int stats_fd = bpf_map__fd(skel->maps.packet_stats);
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook,
.ifindex = env.ifindex,
.attach_point = BPF_TC_INGRESS);
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts,
.flags = BPF_TC_F_REPLACE,
.handle = 1,
.priority = 1,
.prog_fd = prog_fd);
err = bpf_tc_hook_create(&hook);
err = err == -EEXIST ? 0 : err;
if (err) {
fprintf(stderr, "Failed to create tc hook\n");
goto cleanup;
}
err = bpf_tc_attach(&hook, &opts);
if (err) {
fprintf(stderr, "Failed to attach tc program\n");
goto cleanup;
}
while (!exiting) {
err = sleep(env.interval);
/* Ctrl-C will cause -EINTR */
if (err == -EINTR) {
err = 0;
continue;
}
printf("\033[H\033[JIncoming packets on %s\n\n", env.ifname);
print_values(stats_fd);
}
cleanup:
/* Clean up */
tccounter_bpf__destroy(skel);
return err < 0 ? -err : 0;
}