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 <net/if.h> 17 #include <sys/resource.h> 18 #include <arpa/inet.h> 19 #include <netinet/ether.h> 20 #include <unistd.h> 21 #include <time.h> 22 #include <bpf/bpf.h> 23 #include <bpf/libbpf.h> 24 25 #define STATS_INTERVAL_S 2U 26 #define MAX_PCKT_SIZE 600 27 28 static int ifindex = -1; 29 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; 30 static __u32 prog_id; 31 32 static void int_exit(int sig) 33 { 34 __u32 curr_prog_id = 0; 35 36 if (ifindex > -1) { 37 if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) { 38 printf("bpf_xdp_query_id failed\n"); 39 exit(1); 40 } 41 if (prog_id == curr_prog_id) 42 bpf_xdp_detach(ifindex, xdp_flags, NULL); 43 else if (!curr_prog_id) 44 printf("couldn't find a prog id on a given iface\n"); 45 else 46 printf("program on interface changed, not removing\n"); 47 } 48 exit(0); 49 } 50 51 /* simple "icmp packet too big sent" counter 52 */ 53 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s) 54 { 55 time_t started_at = time(NULL); 56 __u64 value = 0; 57 int key = 0; 58 59 60 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { 61 sleep(STATS_INTERVAL_S); 62 63 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0); 64 65 printf("icmp \"packet too big\" sent: %10llu pkts\n", value); 66 } 67 } 68 69 static void usage(const char *cmd) 70 { 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"); 81 } 82 83 int main(int argc, char **argv) 84 { 85 unsigned char opt_flags[256] = {}; 86 const char *optstr = "i:T:P:SNFh"; 87 struct bpf_prog_info info = {}; 88 __u32 info_len = sizeof(info); 89 unsigned int kill_after_s = 0; 90 int i, prog_fd, map_fd, opt; 91 struct bpf_program *prog; 92 struct bpf_object *obj; 93 __u32 max_pckt_size = 0; 94 __u32 key = 0; 95 char filename[256]; 96 int err; 97 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; 101 102 while ((opt = getopt(argc, argv, optstr)) != -1) { 103 104 switch (opt) { 105 case 'i': 106 ifindex = if_nametoindex(optarg); 107 if (!ifindex) 108 ifindex = atoi(optarg); 109 break; 110 case 'T': 111 kill_after_s = atoi(optarg); 112 break; 113 case 'P': 114 max_pckt_size = atoi(optarg); 115 break; 116 case 'S': 117 xdp_flags |= XDP_FLAGS_SKB_MODE; 118 break; 119 case 'N': 120 /* default, set below */ 121 break; 122 case 'F': 123 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; 124 break; 125 default: 126 usage(argv[0]); 127 return 1; 128 } 129 opt_flags[opt] = 0; 130 } 131 132 if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) 133 xdp_flags |= XDP_FLAGS_DRV_MODE; 134 135 for (i = 0; i < strlen(optstr); i++) { 136 if (opt_flags[(unsigned int)optstr[i]]) { 137 fprintf(stderr, "Missing argument -%c\n", optstr[i]); 138 usage(argv[0]); 139 return 1; 140 } 141 } 142 143 if (!ifindex) { 144 fprintf(stderr, "Invalid ifname\n"); 145 return 1; 146 } 147 148 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 149 150 obj = bpf_object__open_file(filename, NULL); 151 if (libbpf_get_error(obj)) 152 return 1; 153 154 prog = bpf_object__next_program(obj, NULL); 155 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP); 156 157 err = bpf_object__load(obj); 158 if (err) 159 return 1; 160 161 prog_fd = bpf_program__fd(prog); 162 163 /* static global var 'max_pcktsz' is accessible from .data section */ 164 if (max_pckt_size) { 165 map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data"); 166 if (map_fd < 0) { 167 printf("finding a max_pcktsz map in obj file failed\n"); 168 return 1; 169 } 170 bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY); 171 } 172 173 /* fetch icmpcnt map */ 174 map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt"); 175 if (map_fd < 0) { 176 printf("finding a icmpcnt map in obj file failed\n"); 177 return 1; 178 } 179 180 signal(SIGINT, int_exit); 181 signal(SIGTERM, int_exit); 182 183 if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) { 184 printf("link set xdp fd failed\n"); 185 return 1; 186 } 187 188 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); 189 if (err) { 190 printf("can't get prog info - %s\n", strerror(errno)); 191 return 1; 192 } 193 prog_id = info.id; 194 195 poll_stats(map_fd, kill_after_s); 196 int_exit(0); 197 198 return 0; 199 } 200