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>
17 #include <sys/resource.h>
18 #include <arpa/inet.h>
19 #include <netinet/ether.h>
25 #define STATS_INTERVAL_S 2U
27 static int ifindex = -1;
28 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
31 static void int_exit(int sig)
33 __u32 curr_prog_id = 0;
36 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
37 printf("bpf_get_link_xdp_id failed\n");
40 if (prog_id == curr_prog_id)
41 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
42 else if (!curr_prog_id)
43 printf("couldn't find a prog id on a given iface\n");
45 printf("program on interface changed, not removing\n");
50 /* simple "icmp packet too big sent" counter
52 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
54 time_t started_at = time(NULL);
59 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
60 sleep(STATS_INTERVAL_S);
62 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
64 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
68 static void usage(const char *cmd)
70 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
71 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
72 printf("Usage: %s [...]\n", cmd);
73 printf(" -i <ifname|ifindex> Interface\n");
74 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
75 printf(" -S use skb-mode\n");
76 printf(" -N enforce native mode\n");
77 printf(" -F force loading prog\n");
78 printf(" -h Display this help\n");
81 int main(int argc, char **argv)
83 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
84 struct bpf_prog_load_attr prog_load_attr = {
85 .prog_type = BPF_PROG_TYPE_XDP,
87 unsigned char opt_flags[256] = {};
88 const char *optstr = "i:T:SNFh";
89 struct bpf_prog_info info = {};
90 __u32 info_len = sizeof(info);
91 unsigned int kill_after_s = 0;
92 int i, prog_fd, map_fd, opt;
93 struct bpf_object *obj;
98 for (i = 0; i < strlen(optstr); i++)
99 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
100 opt_flags[(unsigned char)optstr[i]] = 1;
102 while ((opt = getopt(argc, argv, optstr)) != -1) {
106 ifindex = if_nametoindex(optarg);
108 ifindex = atoi(optarg);
111 kill_after_s = atoi(optarg);
114 xdp_flags |= XDP_FLAGS_SKB_MODE;
117 xdp_flags |= XDP_FLAGS_DRV_MODE;
120 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
129 for (i = 0; i < strlen(optstr); i++) {
130 if (opt_flags[(unsigned int)optstr[i]]) {
131 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
137 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
138 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
143 fprintf(stderr, "Invalid ifname\n");
147 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
148 prog_load_attr.file = filename;
150 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
153 map = bpf_map__next(NULL, obj);
155 printf("finding a map in obj file failed\n");
158 map_fd = bpf_map__fd(map);
161 printf("load_bpf_file: %s\n", strerror(errno));
165 signal(SIGINT, int_exit);
166 signal(SIGTERM, int_exit);
168 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
169 printf("link set xdp fd failed\n");
173 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
175 printf("can't get prog info - %s\n", strerror(errno));
180 poll_stats(map_fd, kill_after_s);