2021-08-21 00:20:02 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc. */
|
2017-08-29 14:38:11 +00:00
|
|
|
static const char *__doc__=
|
2021-08-21 00:20:02 +00:00
|
|
|
"XDP monitor tool, based on tracepoints\n";
|
2017-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
static const char *__doc_err_only__=
|
2021-08-21 00:20:02 +00:00
|
|
|
" NOTICE: Only tracking XDP redirect errors\n"
|
|
|
|
" Enable redirect success stats via '-s/--stats'\n"
|
|
|
|
" (which comes with a per packet processing overhead)\n";
|
2017-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
2017-10-06 08:41:51 +00:00
|
|
|
#include <sys/resource.h>
|
2017-08-29 14:38:11 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <time.h>
|
2020-10-10 18:17:32 +00:00
|
|
|
#include <signal.h>
|
2018-05-15 05:35:02 +00:00
|
|
|
#include <bpf/bpf.h>
|
2020-10-10 18:17:32 +00:00
|
|
|
#include <bpf/libbpf.h>
|
2017-08-29 14:38:11 +00:00
|
|
|
#include "bpf_util.h"
|
2021-08-21 00:20:02 +00:00
|
|
|
#include "xdp_sample_user.h"
|
|
|
|
#include "xdp_monitor.skel.h"
|
2017-08-29 14:38:11 +00:00
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
static int mask = SAMPLE_REDIRECT_ERR_CNT | SAMPLE_CPUMAP_ENQUEUE_CNT |
|
|
|
|
SAMPLE_CPUMAP_KTHREAD_CNT | SAMPLE_EXCEPTION_CNT |
|
|
|
|
SAMPLE_DEVMAP_XMIT_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
|
2020-10-10 18:17:32 +00:00
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
DEFINE_SAMPLE_INIT(xdp_monitor);
|
2017-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
static const struct option long_options[] = {
|
2021-08-21 00:20:02 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "stats", no_argument, NULL, 's' },
|
|
|
|
{ "interval", required_argument, NULL, 'i' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{}
|
2018-01-19 16:15:50 +00:00
|
|
|
};
|
2017-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2021-08-21 00:20:02 +00:00
|
|
|
unsigned long interval = 2;
|
|
|
|
int ret = EXIT_FAIL_OPTION;
|
|
|
|
struct xdp_monitor *skel;
|
2017-08-29 14:38:11 +00:00
|
|
|
bool errors_only = true;
|
2021-08-21 00:20:02 +00:00
|
|
|
int longindex = 0, opt;
|
|
|
|
bool error = true;
|
2017-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
/* Parse commands line args */
|
2021-08-21 00:20:02 +00:00
|
|
|
while ((opt = getopt_long(argc, argv, "si:vh",
|
2017-08-29 14:38:11 +00:00
|
|
|
long_options, &longindex)) != -1) {
|
|
|
|
switch (opt) {
|
2021-08-21 00:20:02 +00:00
|
|
|
case 's':
|
2017-08-29 14:38:11 +00:00
|
|
|
errors_only = false;
|
2021-08-21 00:20:02 +00:00
|
|
|
mask |= SAMPLE_REDIRECT_CNT;
|
2017-08-29 14:38:11 +00:00
|
|
|
break;
|
2021-08-21 00:20:02 +00:00
|
|
|
case 'i':
|
|
|
|
interval = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
sample_switch_mode();
|
2017-08-29 14:38:11 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2021-08-21 00:20:02 +00:00
|
|
|
error = false;
|
2017-08-29 14:38:11 +00:00
|
|
|
default:
|
2021-08-21 00:20:02 +00:00
|
|
|
sample_usage(argv, long_options, __doc__, mask, error);
|
2020-10-10 18:17:32 +00:00
|
|
|
return ret;
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
skel = xdp_monitor__open();
|
|
|
|
if (!skel) {
|
|
|
|
fprintf(stderr, "Failed to xdp_monitor__open: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end;
|
2020-10-10 18:17:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
ret = sample_init_pre_load(skel);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|
2020-10-10 18:17:32 +00:00
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
ret = xdp_monitor__load(skel);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to xdp_monitor__load: %s\n", strerror(errno));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
ret = sample_init(skel, mask);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
if (errors_only)
|
|
|
|
printf("%s", __doc_err_only__);
|
2020-10-10 18:17:32 +00:00
|
|
|
|
2021-08-21 00:20:02 +00:00
|
|
|
ret = sample_run(interval, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL;
|
|
|
|
goto end_destroy;
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|
2021-08-21 00:20:02 +00:00
|
|
|
ret = EXIT_OK;
|
|
|
|
end_destroy:
|
|
|
|
xdp_monitor__destroy(skel);
|
|
|
|
end:
|
|
|
|
sample_exit(ret);
|
2017-08-29 14:38:11 +00:00
|
|
|
}
|