1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2018 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
9 #include <linux/if_link.h>
16 #include <sys/resource.h>
17 #include <arpa/inet.h>
18 #include <netinet/ether.h>
25 #define STATS_INTERVAL_S 2U
27 static int ifindex = -1;
28 static __u32 xdp_flags;
30 static void int_exit(int sig)
33 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
37 /* simple "icmp packet too big sent" counter
39 static void poll_stats(unsigned int kill_after_s)
41 time_t started_at = time(NULL);
46 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
47 sleep(STATS_INTERVAL_S);
49 assert(bpf_map_lookup_elem(map_fd[0], &key, &value) == 0);
51 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
55 static void usage(const char *cmd)
57 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
58 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
59 printf("Usage: %s [...]\n", cmd);
60 printf(" -i <ifindex> Interface Index\n");
61 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
62 printf(" -S use skb-mode\n");
63 printf(" -N enforce native mode\n");
64 printf(" -h Display this help\n");
67 int main(int argc, char **argv)
69 unsigned char opt_flags[256] = {};
70 unsigned int kill_after_s = 0;
71 const char *optstr = "i:T:SNh";
72 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
78 for (i = 0; i < strlen(optstr); i++)
79 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
80 opt_flags[(unsigned char)optstr[i]] = 1;
82 while ((opt = getopt(argc, argv, optstr)) != -1) {
86 ifindex = atoi(optarg);
89 kill_after_s = atoi(optarg);
92 xdp_flags |= XDP_FLAGS_SKB_MODE;
95 xdp_flags |= XDP_FLAGS_DRV_MODE;
104 for (i = 0; i < strlen(optstr); i++) {
105 if (opt_flags[(unsigned int)optstr[i]]) {
106 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
112 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
113 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
117 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
119 if (load_bpf_file(filename)) {
120 printf("%s", bpf_log_buf);
125 printf("load_bpf_file: %s\n", strerror(errno));
129 signal(SIGINT, int_exit);
130 signal(SIGTERM, int_exit);
132 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
133 printf("link set xdp fd failed\n");
137 poll_stats(kill_after_s);
139 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);