18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci * Copyright (c) 2018 Facebook 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 58c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public 68c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/bpf.h> 98c2ecf20Sopenharmony_ci#include <linux/if_link.h> 108c2ecf20Sopenharmony_ci#include <assert.h> 118c2ecf20Sopenharmony_ci#include <errno.h> 128c2ecf20Sopenharmony_ci#include <signal.h> 138c2ecf20Sopenharmony_ci#include <stdio.h> 148c2ecf20Sopenharmony_ci#include <stdlib.h> 158c2ecf20Sopenharmony_ci#include <string.h> 168c2ecf20Sopenharmony_ci#include <net/if.h> 178c2ecf20Sopenharmony_ci#include <sys/resource.h> 188c2ecf20Sopenharmony_ci#include <arpa/inet.h> 198c2ecf20Sopenharmony_ci#include <netinet/ether.h> 208c2ecf20Sopenharmony_ci#include <unistd.h> 218c2ecf20Sopenharmony_ci#include <time.h> 228c2ecf20Sopenharmony_ci#include <bpf/bpf.h> 238c2ecf20Sopenharmony_ci#include <bpf/libbpf.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define STATS_INTERVAL_S 2U 268c2ecf20Sopenharmony_ci#define MAX_PCKT_SIZE 600 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic int ifindex = -1; 298c2ecf20Sopenharmony_cistatic __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; 308c2ecf20Sopenharmony_cistatic __u32 prog_id; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic void int_exit(int sig) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci __u32 curr_prog_id = 0; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (ifindex > -1) { 378c2ecf20Sopenharmony_ci if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { 388c2ecf20Sopenharmony_ci printf("bpf_get_link_xdp_id failed\n"); 398c2ecf20Sopenharmony_ci exit(1); 408c2ecf20Sopenharmony_ci } 418c2ecf20Sopenharmony_ci if (prog_id == curr_prog_id) 428c2ecf20Sopenharmony_ci bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); 438c2ecf20Sopenharmony_ci else if (!curr_prog_id) 448c2ecf20Sopenharmony_ci printf("couldn't find a prog id on a given iface\n"); 458c2ecf20Sopenharmony_ci else 468c2ecf20Sopenharmony_ci printf("program on interface changed, not removing\n"); 478c2ecf20Sopenharmony_ci } 488c2ecf20Sopenharmony_ci exit(0); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* simple "icmp packet too big sent" counter 528c2ecf20Sopenharmony_ci */ 538c2ecf20Sopenharmony_cistatic void poll_stats(unsigned int map_fd, unsigned int kill_after_s) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci time_t started_at = time(NULL); 568c2ecf20Sopenharmony_ci __u64 value = 0; 578c2ecf20Sopenharmony_ci int key = 0; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { 618c2ecf20Sopenharmony_ci sleep(STATS_INTERVAL_S); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci printf("icmp \"packet too big\" sent: %10llu pkts\n", value); 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic void usage(const char *cmd) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci printf("Start a XDP prog which send ICMP \"packet too big\" \n" 728c2ecf20Sopenharmony_ci "messages if ingress packet is bigger then MAX_SIZE bytes\n"); 738c2ecf20Sopenharmony_ci printf("Usage: %s [...]\n", cmd); 748c2ecf20Sopenharmony_ci printf(" -i <ifname|ifindex> Interface\n"); 758c2ecf20Sopenharmony_ci printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n"); 768c2ecf20Sopenharmony_ci printf(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE); 778c2ecf20Sopenharmony_ci printf(" -S use skb-mode\n"); 788c2ecf20Sopenharmony_ci printf(" -N enforce native mode\n"); 798c2ecf20Sopenharmony_ci printf(" -F force loading prog\n"); 808c2ecf20Sopenharmony_ci printf(" -h Display this help\n"); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciint main(int argc, char **argv) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 868c2ecf20Sopenharmony_ci struct bpf_prog_load_attr prog_load_attr = { 878c2ecf20Sopenharmony_ci .prog_type = BPF_PROG_TYPE_XDP, 888c2ecf20Sopenharmony_ci }; 898c2ecf20Sopenharmony_ci unsigned char opt_flags[256] = {}; 908c2ecf20Sopenharmony_ci const char *optstr = "i:T:P:SNFh"; 918c2ecf20Sopenharmony_ci struct bpf_prog_info info = {}; 928c2ecf20Sopenharmony_ci __u32 info_len = sizeof(info); 938c2ecf20Sopenharmony_ci unsigned int kill_after_s = 0; 948c2ecf20Sopenharmony_ci int i, prog_fd, map_fd, opt; 958c2ecf20Sopenharmony_ci struct bpf_object *obj; 968c2ecf20Sopenharmony_ci __u32 max_pckt_size = 0; 978c2ecf20Sopenharmony_ci __u32 key = 0; 988c2ecf20Sopenharmony_ci char filename[256]; 998c2ecf20Sopenharmony_ci int err; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci for (i = 0; i < strlen(optstr); i++) 1028c2ecf20Sopenharmony_ci if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') 1038c2ecf20Sopenharmony_ci opt_flags[(unsigned char)optstr[i]] = 1; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci while ((opt = getopt(argc, argv, optstr)) != -1) { 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci switch (opt) { 1088c2ecf20Sopenharmony_ci case 'i': 1098c2ecf20Sopenharmony_ci ifindex = if_nametoindex(optarg); 1108c2ecf20Sopenharmony_ci if (!ifindex) 1118c2ecf20Sopenharmony_ci ifindex = atoi(optarg); 1128c2ecf20Sopenharmony_ci break; 1138c2ecf20Sopenharmony_ci case 'T': 1148c2ecf20Sopenharmony_ci kill_after_s = atoi(optarg); 1158c2ecf20Sopenharmony_ci break; 1168c2ecf20Sopenharmony_ci case 'P': 1178c2ecf20Sopenharmony_ci max_pckt_size = atoi(optarg); 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci case 'S': 1208c2ecf20Sopenharmony_ci xdp_flags |= XDP_FLAGS_SKB_MODE; 1218c2ecf20Sopenharmony_ci break; 1228c2ecf20Sopenharmony_ci case 'N': 1238c2ecf20Sopenharmony_ci /* default, set below */ 1248c2ecf20Sopenharmony_ci break; 1258c2ecf20Sopenharmony_ci case 'F': 1268c2ecf20Sopenharmony_ci xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; 1278c2ecf20Sopenharmony_ci break; 1288c2ecf20Sopenharmony_ci default: 1298c2ecf20Sopenharmony_ci usage(argv[0]); 1308c2ecf20Sopenharmony_ci return 1; 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci opt_flags[opt] = 0; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) 1368c2ecf20Sopenharmony_ci xdp_flags |= XDP_FLAGS_DRV_MODE; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci for (i = 0; i < strlen(optstr); i++) { 1398c2ecf20Sopenharmony_ci if (opt_flags[(unsigned int)optstr[i]]) { 1408c2ecf20Sopenharmony_ci fprintf(stderr, "Missing argument -%c\n", optstr[i]); 1418c2ecf20Sopenharmony_ci usage(argv[0]); 1428c2ecf20Sopenharmony_ci return 1; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (setrlimit(RLIMIT_MEMLOCK, &r)) { 1478c2ecf20Sopenharmony_ci perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); 1488c2ecf20Sopenharmony_ci return 1; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (!ifindex) { 1528c2ecf20Sopenharmony_ci fprintf(stderr, "Invalid ifname\n"); 1538c2ecf20Sopenharmony_ci return 1; 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 1578c2ecf20Sopenharmony_ci prog_load_attr.file = filename; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) 1608c2ecf20Sopenharmony_ci return 1; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci /* static global var 'max_pcktsz' is accessible from .data section */ 1638c2ecf20Sopenharmony_ci if (max_pckt_size) { 1648c2ecf20Sopenharmony_ci map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data"); 1658c2ecf20Sopenharmony_ci if (map_fd < 0) { 1668c2ecf20Sopenharmony_ci printf("finding a max_pcktsz map in obj file failed\n"); 1678c2ecf20Sopenharmony_ci return 1; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY); 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* fetch icmpcnt map */ 1738c2ecf20Sopenharmony_ci map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt"); 1748c2ecf20Sopenharmony_ci if (map_fd < 0) { 1758c2ecf20Sopenharmony_ci printf("finding a icmpcnt map in obj file failed\n"); 1768c2ecf20Sopenharmony_ci return 1; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci signal(SIGINT, int_exit); 1808c2ecf20Sopenharmony_ci signal(SIGTERM, int_exit); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { 1838c2ecf20Sopenharmony_ci printf("link set xdp fd failed\n"); 1848c2ecf20Sopenharmony_ci return 1; 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); 1888c2ecf20Sopenharmony_ci if (err) { 1898c2ecf20Sopenharmony_ci printf("can't get prog info - %s\n", strerror(errno)); 1908c2ecf20Sopenharmony_ci return 1; 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci prog_id = info.id; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci poll_stats(map_fd, kill_after_s); 1958c2ecf20Sopenharmony_ci int_exit(0); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci return 0; 1988c2ecf20Sopenharmony_ci} 199