blob: fb9391a5ec623a2d1b4abb7a1d67b12894d2a524 [file] [log] [blame]
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +05301// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc. */
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +02003static const char *__doc__=
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +05304"XDP monitor tool, based on tracepoints\n";
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +02005
6static const char *__doc_err_only__=
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +05307" NOTICE: Only tracking XDP redirect errors\n"
8" Enable redirect success stats via '-s/--stats'\n"
9" (which comes with a per packet processing overhead)\n";
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020010
11#include <errno.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdbool.h>
15#include <stdint.h>
16#include <string.h>
17#include <ctype.h>
18#include <unistd.h>
19#include <locale.h>
Jesper Dangaard Brouerc4eb7f42017-10-06 10:41:51 +020020#include <sys/resource.h>
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020021#include <getopt.h>
22#include <net/if.h>
23#include <time.h>
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090024#include <signal.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070025#include <bpf/bpf.h>
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090026#include <bpf/libbpf.h>
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020027#include "bpf_util.h"
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053028#include "xdp_sample_user.h"
29#include "xdp_monitor.skel.h"
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020030
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053031static int mask = SAMPLE_REDIRECT_ERR_CNT | SAMPLE_CPUMAP_ENQUEUE_CNT |
32 SAMPLE_CPUMAP_KTHREAD_CNT | SAMPLE_EXCEPTION_CNT |
33 SAMPLE_DEVMAP_XMIT_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090034
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053035DEFINE_SAMPLE_INIT(xdp_monitor);
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020036
37static const struct option long_options[] = {
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053038 { "help", no_argument, NULL, 'h' },
39 { "stats", no_argument, NULL, 's' },
40 { "interval", required_argument, NULL, 'i' },
41 { "verbose", no_argument, NULL, 'v' },
42 {}
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020043};
44
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020045int main(int argc, char **argv)
46{
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053047 unsigned long interval = 2;
48 int ret = EXIT_FAIL_OPTION;
49 struct xdp_monitor *skel;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020050 bool errors_only = true;
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053051 int longindex = 0, opt;
52 bool error = true;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020053
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020054 /* Parse commands line args */
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053055 while ((opt = getopt_long(argc, argv, "si:vh",
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020056 long_options, &longindex)) != -1) {
57 switch (opt) {
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020058 case 's':
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053059 errors_only = false;
60 mask |= SAMPLE_REDIRECT_CNT;
61 break;
62 case 'i':
63 interval = strtoul(optarg, NULL, 0);
64 break;
65 case 'v':
66 sample_switch_mode();
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020067 break;
68 case 'h':
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053069 error = false;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020070 default:
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053071 sample_usage(argv, long_options, __doc__, mask, error);
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090072 return ret;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020073 }
74 }
75
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053076 skel = xdp_monitor__open();
77 if (!skel) {
78 fprintf(stderr, "Failed to xdp_monitor__open: %s\n",
79 strerror(errno));
80 ret = EXIT_FAIL_BPF;
81 goto end;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +020082 }
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090083
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053084 ret = sample_init_pre_load(skel);
85 if (ret < 0) {
86 fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
87 ret = EXIT_FAIL_BPF;
88 goto end_destroy;
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090089 }
90
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053091 ret = xdp_monitor__load(skel);
92 if (ret < 0) {
93 fprintf(stderr, "Failed to xdp_monitor__load: %s\n", strerror(errno));
94 ret = EXIT_FAIL_BPF;
95 goto end_destroy;
Daniel T. Lee8ac91df2020-10-11 03:17:32 +090096 }
97
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +053098 ret = sample_init(skel, mask);
99 if (ret < 0) {
100 fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
101 ret = EXIT_FAIL_BPF;
102 goto end_destroy;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +0200103 }
104
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530105 if (errors_only)
106 printf("%s", __doc_err_only__);
107
108 ret = sample_run(interval, NULL, NULL);
109 if (ret < 0) {
110 fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
111 ret = EXIT_FAIL;
112 goto end_destroy;
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +0200113 }
Kumar Kartikeya Dwivedi6e1051a2021-08-21 05:50:02 +0530114 ret = EXIT_OK;
115end_destroy:
116 xdp_monitor__destroy(skel);
117end:
118 sample_exit(ret);
Jesper Dangaard Brouer3ffab542017-08-29 16:38:11 +0200119}