1 /* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2018 Facebook
3 *
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.
7 */
8 #include <linux/bpf.h>
9 #include <linux/if_link.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/resource.h>
17 #include <arpa/inet.h>
18 #include <netinet/ether.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include "bpf/bpf.h"
22 #include "bpf/libbpf.h"
23
24 #define STATS_INTERVAL_S 2U
25
26 static int ifindex = -1;
27 static __u32 xdp_flags;
28
int_exit(int sig)29 static void int_exit(int sig)
30 {
31 if (ifindex > -1)
32 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
33 exit(0);
34 }
35
36 /* simple "icmp packet too big sent" counter
37 */
poll_stats(unsigned int map_fd,unsigned int kill_after_s)38 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
39 {
40 time_t started_at = time(NULL);
41 __u64 value = 0;
42 int key = 0;
43
44
45 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
46 sleep(STATS_INTERVAL_S);
47
48 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
49
50 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
51 }
52 }
53
usage(const char * cmd)54 static void usage(const char *cmd)
55 {
56 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
57 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
58 printf("Usage: %s [...]\n", cmd);
59 printf(" -i <ifindex> Interface Index\n");
60 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
61 printf(" -S use skb-mode\n");
62 printf(" -N enforce native mode\n");
63 printf(" -h Display this help\n");
64 }
65
main(int argc,char ** argv)66 int main(int argc, char **argv)
67 {
68 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
69 struct bpf_prog_load_attr prog_load_attr = {
70 .prog_type = BPF_PROG_TYPE_XDP,
71 };
72 unsigned char opt_flags[256] = {};
73 unsigned int kill_after_s = 0;
74 const char *optstr = "i:T:SNh";
75 int i, prog_fd, map_fd, opt;
76 struct bpf_object *obj;
77 struct bpf_map *map;
78 char filename[256];
79
80 for (i = 0; i < strlen(optstr); i++)
81 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
82 opt_flags[(unsigned char)optstr[i]] = 1;
83
84 while ((opt = getopt(argc, argv, optstr)) != -1) {
85
86 switch (opt) {
87 case 'i':
88 ifindex = atoi(optarg);
89 break;
90 case 'T':
91 kill_after_s = atoi(optarg);
92 break;
93 case 'S':
94 xdp_flags |= XDP_FLAGS_SKB_MODE;
95 break;
96 case 'N':
97 xdp_flags |= XDP_FLAGS_DRV_MODE;
98 break;
99 default:
100 usage(argv[0]);
101 return 1;
102 }
103 opt_flags[opt] = 0;
104 }
105
106 for (i = 0; i < strlen(optstr); i++) {
107 if (opt_flags[(unsigned int)optstr[i]]) {
108 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
109 usage(argv[0]);
110 return 1;
111 }
112 }
113
114 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
115 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
116 return 1;
117 }
118
119 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
120 prog_load_attr.file = filename;
121
122 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
123 return 1;
124
125 map = bpf_map__next(NULL, obj);
126 if (!map) {
127 printf("finding a map in obj file failed\n");
128 return 1;
129 }
130 map_fd = bpf_map__fd(map);
131
132 if (!prog_fd) {
133 printf("load_bpf_file: %s\n", strerror(errno));
134 return 1;
135 }
136
137 signal(SIGINT, int_exit);
138 signal(SIGTERM, int_exit);
139
140 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
141 printf("link set xdp fd failed\n");
142 return 1;
143 }
144
145 poll_stats(map_fd, kill_after_s);
146
147 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
148
149 return 0;
150 }
151