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 = XDP_FLAGS_UPDATE_IF_NOEXIST; 28 static __u32 prog_id; 29 30 static void int_exit(int sig) 31 { 32 __u32 curr_prog_id = 0; 33 34 if (ifindex > -1) { 35 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { 36 printf("bpf_get_link_xdp_id failed\n"); 37 exit(1); 38 } 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"); 43 else 44 printf("program on interface changed, not removing\n"); 45 } 46 exit(0); 47 } 48 49 /* simple "icmp packet too big sent" counter 50 */ 51 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s) 52 { 53 time_t started_at = time(NULL); 54 __u64 value = 0; 55 int key = 0; 56 57 58 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { 59 sleep(STATS_INTERVAL_S); 60 61 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0); 62 63 printf("icmp \"packet too big\" sent: %10llu pkts\n", value); 64 } 65 } 66 67 static void usage(const char *cmd) 68 { 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"); 78 } 79 80 int main(int argc, char **argv) 81 { 82 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 83 struct bpf_prog_load_attr prog_load_attr = { 84 .prog_type = BPF_PROG_TYPE_XDP, 85 }; 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; 93 struct bpf_map *map; 94 char filename[256]; 95 int err; 96 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; 100 101 while ((opt = getopt(argc, argv, optstr)) != -1) { 102 103 switch (opt) { 104 case 'i': 105 ifindex = atoi(optarg); 106 break; 107 case 'T': 108 kill_after_s = atoi(optarg); 109 break; 110 case 'S': 111 xdp_flags |= XDP_FLAGS_SKB_MODE; 112 break; 113 case 'N': 114 xdp_flags |= XDP_FLAGS_DRV_MODE; 115 break; 116 case 'F': 117 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; 118 break; 119 default: 120 usage(argv[0]); 121 return 1; 122 } 123 opt_flags[opt] = 0; 124 } 125 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]); 129 usage(argv[0]); 130 return 1; 131 } 132 } 133 134 if (setrlimit(RLIMIT_MEMLOCK, &r)) { 135 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); 136 return 1; 137 } 138 139 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 140 prog_load_attr.file = filename; 141 142 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) 143 return 1; 144 145 map = bpf_map__next(NULL, obj); 146 if (!map) { 147 printf("finding a map in obj file failed\n"); 148 return 1; 149 } 150 map_fd = bpf_map__fd(map); 151 152 if (!prog_fd) { 153 printf("load_bpf_file: %s\n", strerror(errno)); 154 return 1; 155 } 156 157 signal(SIGINT, int_exit); 158 signal(SIGTERM, int_exit); 159 160 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { 161 printf("link set xdp fd failed\n"); 162 return 1; 163 } 164 165 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); 166 if (err) { 167 printf("can't get prog info - %s\n", strerror(errno)); 168 return 1; 169 } 170 prog_id = info.id; 171 172 poll_stats(map_fd, kill_after_s); 173 int_exit(0); 174 175 return 0; 176 } 177