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>
23 #include <bpf/libbpf.h>
25 #define STATS_INTERVAL_S 2U
26 #define MAX_PCKT_SIZE 600
28 static int ifindex
= -1;
29 static __u32 xdp_flags
= XDP_FLAGS_UPDATE_IF_NOEXIST
;
32 static void int_exit(int sig
)
34 __u32 curr_prog_id
= 0;
37 if (bpf_get_link_xdp_id(ifindex
, &curr_prog_id
, xdp_flags
)) {
38 printf("bpf_get_link_xdp_id failed\n");
41 if (prog_id
== curr_prog_id
)
42 bpf_set_link_xdp_fd(ifindex
, -1, xdp_flags
);
43 else if (!curr_prog_id
)
44 printf("couldn't find a prog id on a given iface\n");
46 printf("program on interface changed, not removing\n");
51 /* simple "icmp packet too big sent" counter
53 static void poll_stats(unsigned int map_fd
, unsigned int kill_after_s
)
55 time_t started_at
= time(NULL
);
60 while (!kill_after_s
|| time(NULL
) - started_at
<= kill_after_s
) {
61 sleep(STATS_INTERVAL_S
);
63 assert(bpf_map_lookup_elem(map_fd
, &key
, &value
) == 0);
65 printf("icmp \"packet too big\" sent: %10llu pkts\n", value
);
69 static void usage(const char *cmd
)
71 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
72 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
73 printf("Usage: %s [...]\n", cmd
);
74 printf(" -i <ifname|ifindex> Interface\n");
75 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
76 printf(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE
);
77 printf(" -S use skb-mode\n");
78 printf(" -N enforce native mode\n");
79 printf(" -F force loading prog\n");
80 printf(" -h Display this help\n");
83 int main(int argc
, char **argv
)
85 struct bpf_prog_load_attr prog_load_attr
= {
86 .prog_type
= BPF_PROG_TYPE_XDP
,
88 unsigned char opt_flags
[256] = {};
89 const char *optstr
= "i:T:P:SNFh";
90 struct bpf_prog_info info
= {};
91 __u32 info_len
= sizeof(info
);
92 unsigned int kill_after_s
= 0;
93 int i
, prog_fd
, map_fd
, opt
;
94 struct bpf_object
*obj
;
95 __u32 max_pckt_size
= 0;
100 for (i
= 0; i
< strlen(optstr
); i
++)
101 if (optstr
[i
] != 'h' && 'a' <= optstr
[i
] && optstr
[i
] <= 'z')
102 opt_flags
[(unsigned char)optstr
[i
]] = 1;
104 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
108 ifindex
= if_nametoindex(optarg
);
110 ifindex
= atoi(optarg
);
113 kill_after_s
= atoi(optarg
);
116 max_pckt_size
= atoi(optarg
);
119 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
122 /* default, set below */
125 xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
134 if (!(xdp_flags
& XDP_FLAGS_SKB_MODE
))
135 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
137 for (i
= 0; i
< strlen(optstr
); i
++) {
138 if (opt_flags
[(unsigned int)optstr
[i
]]) {
139 fprintf(stderr
, "Missing argument -%c\n", optstr
[i
]);
146 fprintf(stderr
, "Invalid ifname\n");
150 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
151 prog_load_attr
.file
= filename
;
153 if (bpf_prog_load_xattr(&prog_load_attr
, &obj
, &prog_fd
))
156 /* static global var 'max_pcktsz' is accessible from .data section */
158 map_fd
= bpf_object__find_map_fd_by_name(obj
, "xdp_adju.data");
160 printf("finding a max_pcktsz map in obj file failed\n");
163 bpf_map_update_elem(map_fd
, &key
, &max_pckt_size
, BPF_ANY
);
166 /* fetch icmpcnt map */
167 map_fd
= bpf_object__find_map_fd_by_name(obj
, "icmpcnt");
169 printf("finding a icmpcnt map in obj file failed\n");
173 signal(SIGINT
, int_exit
);
174 signal(SIGTERM
, int_exit
);
176 if (bpf_set_link_xdp_fd(ifindex
, prog_fd
, xdp_flags
) < 0) {
177 printf("link set xdp fd failed\n");
181 err
= bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
);
183 printf("can't get prog info - %s\n", strerror(errno
));
188 poll_stats(map_fd
, kill_after_s
);