| .. | .. |
|---|
| 13 | 13 | #include <stdio.h> |
|---|
| 14 | 14 | #include <stdlib.h> |
|---|
| 15 | 15 | #include <string.h> |
|---|
| 16 | +#include <net/if.h> |
|---|
| 16 | 17 | #include <sys/resource.h> |
|---|
| 17 | 18 | #include <arpa/inet.h> |
|---|
| 18 | 19 | #include <netinet/ether.h> |
|---|
| 19 | 20 | #include <unistd.h> |
|---|
| 20 | 21 | #include <time.h> |
|---|
| 21 | | -#include "bpf/bpf.h" |
|---|
| 22 | | -#include "bpf/libbpf.h" |
|---|
| 22 | +#include <bpf/bpf.h> |
|---|
| 23 | +#include <bpf/libbpf.h> |
|---|
| 23 | 24 | |
|---|
| 24 | 25 | #define STATS_INTERVAL_S 2U |
|---|
| 26 | +#define MAX_PCKT_SIZE 600 |
|---|
| 25 | 27 | |
|---|
| 26 | 28 | static int ifindex = -1; |
|---|
| 27 | | -static __u32 xdp_flags; |
|---|
| 29 | +static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
|---|
| 30 | +static __u32 prog_id; |
|---|
| 28 | 31 | |
|---|
| 29 | 32 | static void int_exit(int sig) |
|---|
| 30 | 33 | { |
|---|
| 31 | | - if (ifindex > -1) |
|---|
| 32 | | - bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
|---|
| 34 | + __u32 curr_prog_id = 0; |
|---|
| 35 | + |
|---|
| 36 | + if (ifindex > -1) { |
|---|
| 37 | + if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) { |
|---|
| 38 | + printf("bpf_get_link_xdp_id failed\n"); |
|---|
| 39 | + exit(1); |
|---|
| 40 | + } |
|---|
| 41 | + if (prog_id == curr_prog_id) |
|---|
| 42 | + bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
|---|
| 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 | + } |
|---|
| 33 | 48 | exit(0); |
|---|
| 34 | 49 | } |
|---|
| 35 | 50 | |
|---|
| .. | .. |
|---|
| 56 | 71 | printf("Start a XDP prog which send ICMP \"packet too big\" \n" |
|---|
| 57 | 72 | "messages if ingress packet is bigger then MAX_SIZE bytes\n"); |
|---|
| 58 | 73 | printf("Usage: %s [...]\n", cmd); |
|---|
| 59 | | - printf(" -i <ifindex> Interface Index\n"); |
|---|
| 74 | + printf(" -i <ifname|ifindex> Interface\n"); |
|---|
| 60 | 75 | printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n"); |
|---|
| 76 | + printf(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE); |
|---|
| 61 | 77 | printf(" -S use skb-mode\n"); |
|---|
| 62 | 78 | printf(" -N enforce native mode\n"); |
|---|
| 79 | + printf(" -F force loading prog\n"); |
|---|
| 63 | 80 | printf(" -h Display this help\n"); |
|---|
| 64 | 81 | } |
|---|
| 65 | 82 | |
|---|
| .. | .. |
|---|
| 70 | 87 | .prog_type = BPF_PROG_TYPE_XDP, |
|---|
| 71 | 88 | }; |
|---|
| 72 | 89 | unsigned char opt_flags[256] = {}; |
|---|
| 90 | + const char *optstr = "i:T:P:SNFh"; |
|---|
| 91 | + struct bpf_prog_info info = {}; |
|---|
| 92 | + __u32 info_len = sizeof(info); |
|---|
| 73 | 93 | unsigned int kill_after_s = 0; |
|---|
| 74 | | - const char *optstr = "i:T:SNh"; |
|---|
| 75 | 94 | int i, prog_fd, map_fd, opt; |
|---|
| 76 | 95 | struct bpf_object *obj; |
|---|
| 77 | | - struct bpf_map *map; |
|---|
| 96 | + __u32 max_pckt_size = 0; |
|---|
| 97 | + __u32 key = 0; |
|---|
| 78 | 98 | char filename[256]; |
|---|
| 99 | + int err; |
|---|
| 79 | 100 | |
|---|
| 80 | 101 | for (i = 0; i < strlen(optstr); i++) |
|---|
| 81 | 102 | if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') |
|---|
| .. | .. |
|---|
| 85 | 106 | |
|---|
| 86 | 107 | switch (opt) { |
|---|
| 87 | 108 | case 'i': |
|---|
| 88 | | - ifindex = atoi(optarg); |
|---|
| 109 | + ifindex = if_nametoindex(optarg); |
|---|
| 110 | + if (!ifindex) |
|---|
| 111 | + ifindex = atoi(optarg); |
|---|
| 89 | 112 | break; |
|---|
| 90 | 113 | case 'T': |
|---|
| 91 | 114 | kill_after_s = atoi(optarg); |
|---|
| 115 | + break; |
|---|
| 116 | + case 'P': |
|---|
| 117 | + max_pckt_size = atoi(optarg); |
|---|
| 92 | 118 | break; |
|---|
| 93 | 119 | case 'S': |
|---|
| 94 | 120 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
|---|
| 95 | 121 | break; |
|---|
| 96 | 122 | case 'N': |
|---|
| 97 | | - xdp_flags |= XDP_FLAGS_DRV_MODE; |
|---|
| 123 | + /* default, set below */ |
|---|
| 124 | + break; |
|---|
| 125 | + case 'F': |
|---|
| 126 | + xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
|---|
| 98 | 127 | break; |
|---|
| 99 | 128 | default: |
|---|
| 100 | 129 | usage(argv[0]); |
|---|
| .. | .. |
|---|
| 102 | 131 | } |
|---|
| 103 | 132 | opt_flags[opt] = 0; |
|---|
| 104 | 133 | } |
|---|
| 134 | + |
|---|
| 135 | + if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
|---|
| 136 | + xdp_flags |= XDP_FLAGS_DRV_MODE; |
|---|
| 105 | 137 | |
|---|
| 106 | 138 | for (i = 0; i < strlen(optstr); i++) { |
|---|
| 107 | 139 | if (opt_flags[(unsigned int)optstr[i]]) { |
|---|
| .. | .. |
|---|
| 116 | 148 | return 1; |
|---|
| 117 | 149 | } |
|---|
| 118 | 150 | |
|---|
| 151 | + if (!ifindex) { |
|---|
| 152 | + fprintf(stderr, "Invalid ifname\n"); |
|---|
| 153 | + return 1; |
|---|
| 154 | + } |
|---|
| 155 | + |
|---|
| 119 | 156 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 120 | 157 | prog_load_attr.file = filename; |
|---|
| 121 | 158 | |
|---|
| 122 | 159 | if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) |
|---|
| 123 | 160 | return 1; |
|---|
| 124 | 161 | |
|---|
| 125 | | - map = bpf_map__next(NULL, obj); |
|---|
| 126 | | - if (!map) { |
|---|
| 127 | | - printf("finding a map in obj file failed\n"); |
|---|
| 128 | | - return 1; |
|---|
| 162 | + /* static global var 'max_pcktsz' is accessible from .data section */ |
|---|
| 163 | + if (max_pckt_size) { |
|---|
| 164 | + map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data"); |
|---|
| 165 | + if (map_fd < 0) { |
|---|
| 166 | + printf("finding a max_pcktsz map in obj file failed\n"); |
|---|
| 167 | + return 1; |
|---|
| 168 | + } |
|---|
| 169 | + bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY); |
|---|
| 129 | 170 | } |
|---|
| 130 | | - map_fd = bpf_map__fd(map); |
|---|
| 131 | 171 | |
|---|
| 132 | | - if (!prog_fd) { |
|---|
| 133 | | - printf("load_bpf_file: %s\n", strerror(errno)); |
|---|
| 172 | + /* fetch icmpcnt map */ |
|---|
| 173 | + map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt"); |
|---|
| 174 | + if (map_fd < 0) { |
|---|
| 175 | + printf("finding a icmpcnt map in obj file failed\n"); |
|---|
| 134 | 176 | return 1; |
|---|
| 135 | 177 | } |
|---|
| 136 | 178 | |
|---|
| .. | .. |
|---|
| 142 | 184 | return 1; |
|---|
| 143 | 185 | } |
|---|
| 144 | 186 | |
|---|
| 145 | | - poll_stats(map_fd, kill_after_s); |
|---|
| 187 | + err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); |
|---|
| 188 | + if (err) { |
|---|
| 189 | + printf("can't get prog info - %s\n", strerror(errno)); |
|---|
| 190 | + return 1; |
|---|
| 191 | + } |
|---|
| 192 | + prog_id = info.id; |
|---|
| 146 | 193 | |
|---|
| 147 | | - bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); |
|---|
| 194 | + poll_stats(map_fd, kill_after_s); |
|---|
| 195 | + int_exit(0); |
|---|
| 148 | 196 | |
|---|
| 149 | 197 | return 0; |
|---|
| 150 | 198 | } |
|---|