Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0
0002  * Copyright (c) 2018 Facebook
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of version 2 of the GNU General Public
0006  * License as published by the Free Software Foundation.
0007  */
0008 #include <linux/bpf.h>
0009 #include <linux/if_link.h>
0010 #include <assert.h>
0011 #include <errno.h>
0012 #include <signal.h>
0013 #include <stdio.h>
0014 #include <stdlib.h>
0015 #include <string.h>
0016 #include <net/if.h>
0017 #include <arpa/inet.h>
0018 #include <netinet/ether.h>
0019 #include <unistd.h>
0020 #include <time.h>
0021 #include <bpf/bpf.h>
0022 #include <bpf/libbpf.h>
0023 
0024 #define STATS_INTERVAL_S 2U
0025 #define MAX_PCKT_SIZE 600
0026 
0027 static int ifindex = -1;
0028 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0029 static __u32 prog_id;
0030 
0031 static void int_exit(int sig)
0032 {
0033     __u32 curr_prog_id = 0;
0034 
0035     if (ifindex > -1) {
0036         if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
0037             printf("bpf_xdp_query_id failed\n");
0038             exit(1);
0039         }
0040         if (prog_id == curr_prog_id)
0041             bpf_xdp_detach(ifindex, xdp_flags, NULL);
0042         else if (!curr_prog_id)
0043             printf("couldn't find a prog id on a given iface\n");
0044         else
0045             printf("program on interface changed, not removing\n");
0046     }
0047     exit(0);
0048 }
0049 
0050 /* simple "icmp packet too big sent" counter
0051  */
0052 static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
0053 {
0054     time_t started_at = time(NULL);
0055     __u64 value = 0;
0056     int key = 0;
0057 
0058 
0059     while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
0060         sleep(STATS_INTERVAL_S);
0061 
0062         assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
0063 
0064         printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
0065     }
0066 }
0067 
0068 static void usage(const char *cmd)
0069 {
0070     printf("Start a XDP prog which send ICMP \"packet too big\" \n"
0071         "messages if ingress packet is bigger then MAX_SIZE bytes\n");
0072     printf("Usage: %s [...]\n", cmd);
0073     printf("    -i <ifname|ifindex> Interface\n");
0074     printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
0075     printf("    -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
0076     printf("    -S use skb-mode\n");
0077     printf("    -N enforce native mode\n");
0078     printf("    -F force loading prog\n");
0079     printf("    -h Display this help\n");
0080 }
0081 
0082 int main(int argc, char **argv)
0083 {
0084     unsigned char opt_flags[256] = {};
0085     const char *optstr = "i:T:P:SNFh";
0086     struct bpf_prog_info info = {};
0087     __u32 info_len = sizeof(info);
0088     unsigned int kill_after_s = 0;
0089     int i, prog_fd, map_fd, opt;
0090     struct bpf_program *prog;
0091     struct bpf_object *obj;
0092     __u32 max_pckt_size = 0;
0093     __u32 key = 0;
0094     char filename[256];
0095     int err;
0096 
0097     for (i = 0; i < strlen(optstr); i++)
0098         if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
0099             opt_flags[(unsigned char)optstr[i]] = 1;
0100 
0101     while ((opt = getopt(argc, argv, optstr)) != -1) {
0102 
0103         switch (opt) {
0104         case 'i':
0105             ifindex = if_nametoindex(optarg);
0106             if (!ifindex)
0107                 ifindex = atoi(optarg);
0108             break;
0109         case 'T':
0110             kill_after_s = atoi(optarg);
0111             break;
0112         case 'P':
0113             max_pckt_size = atoi(optarg);
0114             break;
0115         case 'S':
0116             xdp_flags |= XDP_FLAGS_SKB_MODE;
0117             break;
0118         case 'N':
0119             /* default, set below */
0120             break;
0121         case 'F':
0122             xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0123             break;
0124         default:
0125             usage(argv[0]);
0126             return 1;
0127         }
0128         opt_flags[opt] = 0;
0129     }
0130 
0131     if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0132         xdp_flags |= XDP_FLAGS_DRV_MODE;
0133 
0134     for (i = 0; i < strlen(optstr); i++) {
0135         if (opt_flags[(unsigned int)optstr[i]]) {
0136             fprintf(stderr, "Missing argument -%c\n", optstr[i]);
0137             usage(argv[0]);
0138             return 1;
0139         }
0140     }
0141 
0142     if (!ifindex) {
0143         fprintf(stderr, "Invalid ifname\n");
0144         return 1;
0145     }
0146 
0147     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0148 
0149     obj = bpf_object__open_file(filename, NULL);
0150     if (libbpf_get_error(obj))
0151         return 1;
0152 
0153     prog = bpf_object__next_program(obj, NULL);
0154     bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0155 
0156     err = bpf_object__load(obj);
0157     if (err)
0158         return 1;
0159 
0160     prog_fd = bpf_program__fd(prog);
0161 
0162     /* static global var 'max_pcktsz' is accessible from .data section */
0163     if (max_pckt_size) {
0164         map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
0165         if (map_fd < 0) {
0166             printf("finding a max_pcktsz map in obj file failed\n");
0167             return 1;
0168         }
0169         bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
0170     }
0171 
0172     /* fetch icmpcnt map */
0173     map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
0174     if (map_fd < 0) {
0175         printf("finding a icmpcnt map in obj file failed\n");
0176         return 1;
0177     }
0178 
0179     signal(SIGINT, int_exit);
0180     signal(SIGTERM, int_exit);
0181 
0182     if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
0183         printf("link set xdp fd failed\n");
0184         return 1;
0185     }
0186 
0187     err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0188     if (err) {
0189         printf("can't get prog info - %s\n", strerror(errno));
0190         return 1;
0191     }
0192     prog_id = info.id;
0193 
0194     poll_stats(map_fd, kill_after_s);
0195     int_exit(0);
0196 
0197     return 0;
0198 }