Skip to content

Commit 399e351

Browse files
committed
Merge branch 'xdp_monitor-improve'
Jesper Dangaard Brouer says: ==================== Improve xdp_monitor samples/bpf Here are some improvements to the xdp_monitor tool currently located under samples/bpf/. Once the tools library libbpf become more feature complete, xdp_monitor should be converted to use it, and be moved into tools/bpf/xdp/ or tools/xdp/. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents c6a1575 + c4eb7f4 commit 399e351

File tree

2 files changed

+139
-40
lines changed

2 files changed

+139
-40
lines changed

samples/bpf/xdp_monitor_kern.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ struct bpf_map_def SEC("maps") redirect_err_cnt = {
1313
/* TODO: have entries for all possible errno's */
1414
};
1515

16+
#define XDP_UNKNOWN XDP_REDIRECT + 1
17+
struct bpf_map_def SEC("maps") exception_cnt = {
18+
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
19+
.key_size = sizeof(u32),
20+
.value_size = sizeof(u64),
21+
.max_entries = XDP_UNKNOWN + 1,
22+
};
23+
1624
/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
1725
* Code in: kernel/include/trace/events/xdp.h
1826
*/
1927
struct xdp_redirect_ctx {
20-
unsigned short common_type; // offset:0; size:2; signed:0;
21-
unsigned char common_flags; // offset:2; size:1; signed:0;
22-
unsigned char common_preempt_count;// offset:3; size:1; signed:0;
23-
int common_pid; // offset:4; size:4; signed:1;
24-
25-
int prog_id; // offset:8; size:4; signed:1;
26-
u32 act; // offset:12 size:4; signed:0;
27-
int ifindex; // offset:16 size:4; signed:1;
28-
int err; // offset:20 size:4; signed:1;
29-
int to_ifindex; // offset:24 size:4; signed:1;
30-
u32 map_id; // offset:28 size:4; signed:0;
31-
int map_index; // offset:32 size:4; signed:1;
32-
}; // offset:36
28+
u64 __pad; // First 8 bytes are not accessible by bpf code
29+
int prog_id; // offset:8; size:4; signed:1;
30+
u32 act; // offset:12 size:4; signed:0;
31+
int ifindex; // offset:16 size:4; signed:1;
32+
int err; // offset:20 size:4; signed:1;
33+
int to_ifindex; // offset:24 size:4; signed:1;
34+
u32 map_id; // offset:28 size:4; signed:0;
35+
int map_index; // offset:32 size:4; signed:1;
36+
}; // offset:36
3337

3438
enum {
3539
XDP_REDIRECT_SUCCESS = 0,
@@ -48,7 +52,7 @@ int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
4852

4953
cnt = bpf_map_lookup_elem(&redirect_err_cnt, &key);
5054
if (!cnt)
51-
return 0;
55+
return 1;
5256
*cnt += 1;
5357

5458
return 0; /* Indicate event was filtered (no further processing)*/
@@ -86,3 +90,31 @@ int trace_xdp_redirect_map(struct xdp_redirect_ctx *ctx)
8690
{
8791
return xdp_redirect_collect_stat(ctx);
8892
}
93+
94+
/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_exception/format
95+
* Code in: kernel/include/trace/events/xdp.h
96+
*/
97+
struct xdp_exception_ctx {
98+
u64 __pad; // First 8 bytes are not accessible by bpf code
99+
int prog_id; // offset:8; size:4; signed:1;
100+
u32 act; // offset:12; size:4; signed:0;
101+
int ifindex; // offset:16; size:4; signed:1;
102+
};
103+
104+
SEC("tracepoint/xdp/xdp_exception")
105+
int trace_xdp_exception(struct xdp_exception_ctx *ctx)
106+
{
107+
u64 *cnt;;
108+
u32 key;
109+
110+
key = ctx->act;
111+
if (key > XDP_REDIRECT)
112+
key = XDP_UNKNOWN;
113+
114+
cnt = bpf_map_lookup_elem(&exception_cnt, &key);
115+
if (!cnt)
116+
return 1;
117+
*cnt += 1;
118+
119+
return 0;
120+
}

samples/bpf/xdp_monitor_user.c

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static const char *__doc_err_only__=
2020
#include <unistd.h>
2121
#include <locale.h>
2222

23+
#include <sys/resource.h>
2324
#include <getopt.h>
2425
#include <net/if.h>
2526
#include <time.h>
@@ -89,6 +90,23 @@ static const char *err2str(int err)
8990
return redir_names[err];
9091
return NULL;
9192
}
93+
/* enum xdp_action */
94+
#define XDP_UNKNOWN XDP_REDIRECT + 1
95+
#define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
96+
static const char *xdp_action_names[XDP_ACTION_MAX] = {
97+
[XDP_ABORTED] = "XDP_ABORTED",
98+
[XDP_DROP] = "XDP_DROP",
99+
[XDP_PASS] = "XDP_PASS",
100+
[XDP_TX] = "XDP_TX",
101+
[XDP_REDIRECT] = "XDP_REDIRECT",
102+
[XDP_UNKNOWN] = "XDP_UNKNOWN",
103+
};
104+
static const char *action2str(int action)
105+
{
106+
if (action < XDP_ACTION_MAX)
107+
return xdp_action_names[action];
108+
return NULL;
109+
}
92110

93111
struct record {
94112
__u64 counter;
@@ -97,46 +115,80 @@ struct record {
97115

98116
struct stats_record {
99117
struct record xdp_redir[REDIR_RES_MAX];
118+
struct record xdp_exception[XDP_ACTION_MAX];
100119
};
101120

102121
static void stats_print_headers(bool err_only)
103122
{
104123
if (err_only)
105124
printf("\n%s\n", __doc_err_only__);
106125

107-
printf("%-14s %-10s %-18s %-9s\n",
108-
"XDP_REDIRECT", "pps ", "pps-human-readable", "measure-period");
126+
printf("%-14s %-11s %-10s %-18s %-9s\n",
127+
"ACTION", "result", "pps ", "pps-human-readable", "measure-period");
128+
}
129+
130+
static double calc_period(struct record *r, struct record *p)
131+
{
132+
double period_ = 0;
133+
__u64 period = 0;
134+
135+
period = r->timestamp - p->timestamp;
136+
if (period > 0)
137+
period_ = ((double) period / NANOSEC_PER_SEC);
138+
139+
return period_;
140+
}
141+
142+
static double calc_pps(struct record *r, struct record *p, double period)
143+
{
144+
__u64 packets = 0;
145+
double pps = 0;
146+
147+
if (period > 0) {
148+
packets = r->counter - p->counter;
149+
pps = packets / period;
150+
}
151+
return pps;
109152
}
110153

111154
static void stats_print(struct stats_record *rec,
112155
struct stats_record *prev,
113156
bool err_only)
114157
{
158+
double period = 0, pps = 0;
159+
struct record *r, *p;
115160
int i = 0;
116161

162+
char *fmt = "%-14s %-11s %-10.0f %'-18.0f %f\n";
163+
164+
/* tracepoint: xdp:xdp_redirect_* */
117165
if (err_only)
118166
i = REDIR_ERROR;
119167

120168
for (; i < REDIR_RES_MAX; i++) {
121-
struct record *r = &rec->xdp_redir[i];
122-
struct record *p = &prev->xdp_redir[i];
123-
__u64 period = 0;
124-
__u64 packets = 0;
125-
double pps = 0;
126-
double period_ = 0;
169+
r = &rec->xdp_redir[i];
170+
p = &prev->xdp_redir[i];
127171

128172
if (p->timestamp) {
129-
packets = r->counter - p->counter;
130-
period = r->timestamp - p->timestamp;
131-
if (period > 0) {
132-
period_ = ((double) period / NANOSEC_PER_SEC);
133-
pps = packets / period_;
134-
}
173+
period = calc_period(r, p);
174+
pps = calc_pps(r, p, period);
135175
}
176+
printf(fmt, "XDP_REDIRECT", err2str(i), pps, pps, period);
177+
}
136178

137-
printf("%-14s %-10.0f %'-18.0f %f\n",
138-
err2str(i), pps, pps, period_);
179+
/* tracepoint: xdp:xdp_exception */
180+
for (i = 0; i < XDP_ACTION_MAX; i++) {
181+
r = &rec->xdp_exception[i];
182+
p = &prev->xdp_exception[i];
183+
if (p->timestamp) {
184+
period = calc_period(r, p);
185+
pps = calc_pps(r, p, period);
186+
}
187+
if (pps > 0)
188+
printf(fmt, action2str(i), "Exception",
189+
pps, pps, period);
139190
}
191+
printf("\n");
140192
}
141193

142194
static __u64 get_key32_value64_percpu(int fd, __u32 key)
@@ -160,25 +212,33 @@ static __u64 get_key32_value64_percpu(int fd, __u32 key)
160212
return sum;
161213
}
162214

163-
static bool stats_collect(int fd, struct stats_record *rec)
215+
static bool stats_collect(struct stats_record *rec)
164216
{
217+
int fd;
165218
int i;
166219

167220
/* TODO: Detect if someone unloaded the perf event_fd's, as
168221
* this can happen by someone running perf-record -e
169222
*/
170223

224+
fd = map_data[0].fd; /* map0: redirect_err_cnt */
171225
for (i = 0; i < REDIR_RES_MAX; i++) {
172226
rec->xdp_redir[i].timestamp = gettime();
173227
rec->xdp_redir[i].counter = get_key32_value64_percpu(fd, i);
174228
}
229+
230+
fd = map_data[1].fd; /* map1: exception_cnt */
231+
for (i = 0; i < XDP_ACTION_MAX; i++) {
232+
rec->xdp_exception[i].timestamp = gettime();
233+
rec->xdp_exception[i].counter = get_key32_value64_percpu(fd, i);
234+
}
235+
175236
return true;
176237
}
177238

178239
static void stats_poll(int interval, bool err_only)
179240
{
180241
struct stats_record rec, prev;
181-
int map_fd;
182242

183243
memset(&rec, 0, sizeof(rec));
184244

@@ -190,16 +250,17 @@ static void stats_poll(int interval, bool err_only)
190250
printf("\n%s", __doc__);
191251

192252
/* TODO Need more advanced stats on error types */
193-
if (verbose)
194-
printf(" - Stats map: %s\n", map_data[0].name);
195-
map_fd = map_data[0].fd;
196-
197-
stats_print_headers(err_only);
253+
if (verbose) {
254+
printf(" - Stats map0: %s\n", map_data[0].name);
255+
printf(" - Stats map1: %s\n", map_data[1].name);
256+
printf("\n");
257+
}
198258
fflush(stdout);
199259

200260
while (1) {
201261
memcpy(&prev, &rec, sizeof(rec));
202-
stats_collect(map_fd, &rec);
262+
stats_collect(&rec);
263+
stats_print_headers(err_only);
203264
stats_print(&rec, &prev, err_only);
204265
fflush(stdout);
205266
sleep(interval);
@@ -235,6 +296,7 @@ static void print_bpf_prog_info(void)
235296

236297
int main(int argc, char **argv)
237298
{
299+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
238300
int longindex = 0, opt;
239301
int ret = EXIT_SUCCESS;
240302
char bpf_obj_file[256];
@@ -265,13 +327,18 @@ int main(int argc, char **argv)
265327
}
266328
}
267329

330+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
331+
perror("setrlimit(RLIMIT_MEMLOCK)");
332+
return EXIT_FAILURE;
333+
}
334+
268335
if (load_bpf_file(bpf_obj_file)) {
269336
printf("ERROR - bpf_log_buf: %s", bpf_log_buf);
270-
return 1;
337+
return EXIT_FAILURE;
271338
}
272339
if (!prog_fd[0]) {
273340
printf("ERROR - load_bpf_file: %s\n", strerror(errno));
274-
return 1;
341+
return EXIT_FAILURE;
275342
}
276343

277344
if (debug) {

0 commit comments

Comments
 (0)