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>
22 #include "bpf/libbpf.h"
24 #define STATS_INTERVAL_S 2U
26 static int ifindex = -1;
27 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
30 static void int_exit(int sig)
32 __u32 curr_prog_id = 0;
35 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
36 printf("bpf_get_link_xdp_id failed\n");
39 if (prog_id == curr_prog_id)
40 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
41 else if (!curr_prog_id)
42 printf("couldn't find a prog id on a given iface\n");
44 printf("program on interface changed, not removing\n");
49 /* simple "icmp packet too big sent" counter
51 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
53 time_t started_at = time(NULL);
58 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
59 sleep(STATS_INTERVAL_S);
61 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
63 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
67 static void usage(const char *cmd)
69 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
70 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
71 printf("Usage: %s [...]\n", cmd);
72 printf(" -i <ifindex> Interface Index\n");
73 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
74 printf(" -S use skb-mode\n");
75 printf(" -N enforce native mode\n");
76 printf(" -F force loading prog\n");
77 printf(" -h Display this help\n");
80 int main(int argc, char **argv)
82 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
83 struct bpf_prog_load_attr prog_load_attr = {
84 .prog_type = BPF_PROG_TYPE_XDP,
86 unsigned char opt_flags[256] = {};
87 const char *optstr = "i:T:SNFh";
88 struct bpf_prog_info info = {};
89 __u32 info_len = sizeof(info);
90 unsigned int kill_after_s = 0;
91 int i, prog_fd, map_fd, opt;
92 struct bpf_object *obj;
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 = atoi(optarg);
108 kill_after_s = atoi(optarg);
111 xdp_flags |= XDP_FLAGS_SKB_MODE;
114 xdp_flags |= XDP_FLAGS_DRV_MODE;
117 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
126 for (i = 0; i < strlen(optstr); i++) {
127 if (opt_flags[(unsigned int)optstr[i]]) {
128 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
134 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
135 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
139 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
140 prog_load_attr.file = filename;
142 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
145 map = bpf_map__next(NULL, obj);
147 printf("finding a map in obj file failed\n");
150 map_fd = bpf_map__fd(map);
153 printf("load_bpf_file: %s\n", strerror(errno));
157 signal(SIGINT, int_exit);
158 signal(SIGTERM, int_exit);
160 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
161 printf("link set xdp fd failed\n");
165 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
167 printf("can't get prog info - %s\n", strerror(errno));
172 poll_stats(map_fd, kill_after_s);