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 <arpa/inet.h>
18 #include <netinet/ether.h>
22 #include <bpf/libbpf.h>
24 #define STATS_INTERVAL_S 2U
25 #define MAX_PCKT_SIZE 600
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_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
37 printf("bpf_xdp_query_id failed\n");
40 if (prog_id == curr_prog_id)
41 bpf_xdp_detach(ifindex, xdp_flags, NULL);
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(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
76 printf(" -S use skb-mode\n");
77 printf(" -N enforce native mode\n");
78 printf(" -F force loading prog\n");
79 printf(" -h Display this help\n");
82 int main(int argc, char **argv)
84 unsigned char opt_flags[256] = {};
85 const char *optstr = "i:T:P:SNFh";
86 struct bpf_prog_info info = {};
87 __u32 info_len = sizeof(info);
88 unsigned int kill_after_s = 0;
89 int i, prog_fd, map_fd, opt;
90 struct bpf_program *prog;
91 struct bpf_object *obj;
92 __u32 max_pckt_size = 0;
97 for (i = 0; i < strlen(optstr); i++)
98 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
99 opt_flags[(unsigned char)optstr[i]] = 1;
101 while ((opt = getopt(argc, argv, optstr)) != -1) {
105 ifindex = if_nametoindex(optarg);
107 ifindex = atoi(optarg);
110 kill_after_s = atoi(optarg);
113 max_pckt_size = atoi(optarg);
116 xdp_flags |= XDP_FLAGS_SKB_MODE;
119 /* default, set below */
122 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
131 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
132 xdp_flags |= XDP_FLAGS_DRV_MODE;
134 for (i = 0; i < strlen(optstr); i++) {
135 if (opt_flags[(unsigned int)optstr[i]]) {
136 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
143 fprintf(stderr, "Invalid ifname\n");
147 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
149 obj = bpf_object__open_file(filename, NULL);
150 if (libbpf_get_error(obj))
153 prog = bpf_object__next_program(obj, NULL);
154 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
156 err = bpf_object__load(obj);
160 prog_fd = bpf_program__fd(prog);
162 /* static global var 'max_pcktsz' is accessible from .data section */
164 map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
166 printf("finding a max_pcktsz map in obj file failed\n");
169 bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
172 /* fetch icmpcnt map */
173 map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
175 printf("finding a icmpcnt map in obj file failed\n");
179 signal(SIGINT, int_exit);
180 signal(SIGTERM, int_exit);
182 if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
183 printf("link set xdp fd failed\n");
187 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
189 printf("can't get prog info - %s\n", strerror(errno));
194 poll_stats(map_fd, kill_after_s);