Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc. */ |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 3 | static const char *__doc__= |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 4 | "XDP monitor tool, based on tracepoints\n"; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 5 | |
| 6 | static const char *__doc_err_only__= |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 7 | " 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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 10 | |
| 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 Brouer | c4eb7f4 | 2017-10-06 10:41:51 +0200 | [diff] [blame] | 20 | #include <sys/resource.h> |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 21 | #include <getopt.h> |
| 22 | #include <net/if.h> |
| 23 | #include <time.h> |
Daniel T. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 24 | #include <signal.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 25 | #include <bpf/bpf.h> |
Daniel T. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 26 | #include <bpf/libbpf.h> |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 27 | #include "bpf_util.h" |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 28 | #include "xdp_sample_user.h" |
| 29 | #include "xdp_monitor.skel.h" |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 30 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 31 | static 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. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 34 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 35 | DEFINE_SAMPLE_INIT(xdp_monitor); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 36 | |
| 37 | static const struct option long_options[] = { |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 38 | { "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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 45 | int main(int argc, char **argv) |
| 46 | { |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 47 | unsigned long interval = 2; |
| 48 | int ret = EXIT_FAIL_OPTION; |
| 49 | struct xdp_monitor *skel; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 50 | bool errors_only = true; |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 51 | int longindex = 0, opt; |
| 52 | bool error = true; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 53 | |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 54 | /* Parse commands line args */ |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 55 | while ((opt = getopt_long(argc, argv, "si:vh", |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 56 | long_options, &longindex)) != -1) { |
| 57 | switch (opt) { |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 58 | case 's': |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 59 | 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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 67 | break; |
| 68 | case 'h': |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 69 | error = false; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 70 | default: |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 71 | sample_usage(argv, long_options, __doc__, mask, error); |
Daniel T. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 72 | return ret; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 76 | 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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 82 | } |
Daniel T. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 83 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 84 | 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. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 91 | 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. Lee | 8ac91df | 2020-10-11 03:17:32 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 98 | 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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 105 | 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 Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 113 | } |
Kumar Kartikeya Dwivedi | 6e1051a | 2021-08-21 05:50:02 +0530 | [diff] [blame] | 114 | ret = EXIT_OK; |
| 115 | end_destroy: |
| 116 | xdp_monitor__destroy(skel); |
| 117 | end: |
| 118 | sample_exit(ret); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 119 | } |