1 /* Copyright (c) 2017 Facebook 2 * 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of version 2 of the GNU General Public 5 * License as published by the Free Software Foundation. 6 */ 7 #include <stdio.h> 8 #include <unistd.h> 9 #include <errno.h> 10 #include <string.h> 11 #include <assert.h> 12 #include <stdlib.h> 13 14 #include <linux/types.h> 15 typedef __u16 __sum16; 16 #include <arpa/inet.h> 17 #include <linux/if_ether.h> 18 #include <linux/if_packet.h> 19 #include <linux/ip.h> 20 #include <linux/ipv6.h> 21 #include <linux/tcp.h> 22 23 #include <sys/wait.h> 24 #include <sys/resource.h> 25 26 #include <linux/bpf.h> 27 #include <linux/err.h> 28 #include <bpf/bpf.h> 29 #include <bpf/libbpf.h> 30 #include "test_iptunnel_common.h" 31 #include "bpf_util.h" 32 33 #define _htons __builtin_bswap16 34 35 static int error_cnt, pass_cnt; 36 37 #define MAGIC_BYTES 123 38 39 /* ipv4 test vector */ 40 static struct { 41 struct ethhdr eth; 42 struct iphdr iph; 43 struct tcphdr tcp; 44 } __packed pkt_v4 = { 45 .eth.h_proto = _htons(ETH_P_IP), 46 .iph.ihl = 5, 47 .iph.protocol = 6, 48 .iph.tot_len = _htons(MAGIC_BYTES), 49 .tcp.urg_ptr = 123, 50 }; 51 52 /* ipv6 test vector */ 53 static struct { 54 struct ethhdr eth; 55 struct ipv6hdr iph; 56 struct tcphdr tcp; 57 } __packed pkt_v6 = { 58 .eth.h_proto = _htons(ETH_P_IPV6), 59 .iph.nexthdr = 6, 60 .iph.payload_len = _htons(MAGIC_BYTES), 61 .tcp.urg_ptr = 123, 62 }; 63 64 #define CHECK(condition, tag, format...) ({ \ 65 int __ret = !!(condition); \ 66 if (__ret) { \ 67 error_cnt++; \ 68 printf("%s:FAIL:%s ", __func__, tag); \ 69 printf(format); \ 70 } else { \ 71 pass_cnt++; \ 72 printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\ 73 } \ 74 }) 75 76 static int bpf_prog_load(const char *file, enum bpf_prog_type type, 77 struct bpf_object **pobj, int *prog_fd) 78 { 79 struct bpf_program *prog; 80 struct bpf_object *obj; 81 int err; 82 83 obj = bpf_object__open(file); 84 if (IS_ERR(obj)) { 85 error_cnt++; 86 return -ENOENT; 87 } 88 89 prog = bpf_program__next(NULL, obj); 90 if (!prog) { 91 bpf_object__close(obj); 92 error_cnt++; 93 return -ENOENT; 94 } 95 96 bpf_program__set_type(prog, type); 97 err = bpf_object__load(obj); 98 if (err) { 99 bpf_object__close(obj); 100 error_cnt++; 101 return -EINVAL; 102 } 103 104 *pobj = obj; 105 *prog_fd = bpf_program__fd(prog); 106 return 0; 107 } 108 109 static int bpf_find_map(const char *test, struct bpf_object *obj, 110 const char *name) 111 { 112 struct bpf_map *map; 113 114 map = bpf_object__find_map_by_name(obj, name); 115 if (!map) { 116 printf("%s:FAIL:map '%s' not found\n", test, name); 117 error_cnt++; 118 return -1; 119 } 120 return bpf_map__fd(map); 121 } 122 123 static void test_pkt_access(void) 124 { 125 const char *file = "./test_pkt_access.o"; 126 struct bpf_object *obj; 127 __u32 duration, retval; 128 int err, prog_fd; 129 130 err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); 131 if (err) 132 return; 133 134 err = bpf_prog_test_run(prog_fd, 100000, &pkt_v4, sizeof(pkt_v4), 135 NULL, NULL, &retval, &duration); 136 CHECK(err || errno || retval, "ipv4", 137 "err %d errno %d retval %d duration %d\n", 138 err, errno, retval, duration); 139 140 err = bpf_prog_test_run(prog_fd, 100000, &pkt_v6, sizeof(pkt_v6), 141 NULL, NULL, &retval, &duration); 142 CHECK(err || errno || retval, "ipv6", 143 "err %d errno %d retval %d duration %d\n", 144 err, errno, retval, duration); 145 bpf_object__close(obj); 146 } 147 148 static void test_xdp(void) 149 { 150 struct vip key4 = {.protocol = 6, .family = AF_INET}; 151 struct vip key6 = {.protocol = 6, .family = AF_INET6}; 152 struct iptnl_info value4 = {.family = AF_INET}; 153 struct iptnl_info value6 = {.family = AF_INET6}; 154 const char *file = "./test_xdp.o"; 155 struct bpf_object *obj; 156 char buf[128]; 157 struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr); 158 struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); 159 __u32 duration, retval, size; 160 int err, prog_fd, map_fd; 161 162 err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); 163 if (err) 164 return; 165 166 map_fd = bpf_find_map(__func__, obj, "vip2tnl"); 167 if (map_fd < 0) 168 goto out; 169 bpf_map_update_elem(map_fd, &key4, &value4, 0); 170 bpf_map_update_elem(map_fd, &key6, &value6, 0); 171 172 err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), 173 buf, &size, &retval, &duration); 174 175 CHECK(err || errno || retval != XDP_TX || size != 74 || 176 iph->protocol != IPPROTO_IPIP, "ipv4", 177 "err %d errno %d retval %d size %d\n", 178 err, errno, retval, size); 179 180 err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6), 181 buf, &size, &retval, &duration); 182 CHECK(err || errno || retval != XDP_TX || size != 114 || 183 iph6->nexthdr != IPPROTO_IPV6, "ipv6", 184 "err %d errno %d retval %d size %d\n", 185 err, errno, retval, size); 186 out: 187 bpf_object__close(obj); 188 } 189 190 #define MAGIC_VAL 0x1234 191 #define NUM_ITER 100000 192 #define VIP_NUM 5 193 194 static void test_l4lb(void) 195 { 196 unsigned int nr_cpus = bpf_num_possible_cpus(); 197 const char *file = "./test_l4lb.o"; 198 struct vip key = {.protocol = 6}; 199 struct vip_meta { 200 __u32 flags; 201 __u32 vip_num; 202 } value = {.vip_num = VIP_NUM}; 203 __u32 stats_key = VIP_NUM; 204 struct vip_stats { 205 __u64 bytes; 206 __u64 pkts; 207 } stats[nr_cpus]; 208 struct real_definition { 209 union { 210 __be32 dst; 211 __be32 dstv6[4]; 212 }; 213 __u8 flags; 214 } real_def = {.dst = MAGIC_VAL}; 215 __u32 ch_key = 11, real_num = 3; 216 __u32 duration, retval, size; 217 int err, i, prog_fd, map_fd; 218 __u64 bytes = 0, pkts = 0; 219 struct bpf_object *obj; 220 char buf[128]; 221 u32 *magic = (u32 *)buf; 222 223 err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); 224 if (err) 225 return; 226 227 map_fd = bpf_find_map(__func__, obj, "vip_map"); 228 if (map_fd < 0) 229 goto out; 230 bpf_map_update_elem(map_fd, &key, &value, 0); 231 232 map_fd = bpf_find_map(__func__, obj, "ch_rings"); 233 if (map_fd < 0) 234 goto out; 235 bpf_map_update_elem(map_fd, &ch_key, &real_num, 0); 236 237 map_fd = bpf_find_map(__func__, obj, "reals"); 238 if (map_fd < 0) 239 goto out; 240 bpf_map_update_elem(map_fd, &real_num, &real_def, 0); 241 242 err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4), 243 buf, &size, &retval, &duration); 244 CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 || 245 *magic != MAGIC_VAL, "ipv4", 246 "err %d errno %d retval %d size %d magic %x\n", 247 err, errno, retval, size, *magic); 248 249 err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6), 250 buf, &size, &retval, &duration); 251 CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 || 252 *magic != MAGIC_VAL, "ipv6", 253 "err %d errno %d retval %d size %d magic %x\n", 254 err, errno, retval, size, *magic); 255 256 map_fd = bpf_find_map(__func__, obj, "stats"); 257 if (map_fd < 0) 258 goto out; 259 bpf_map_lookup_elem(map_fd, &stats_key, stats); 260 for (i = 0; i < nr_cpus; i++) { 261 bytes += stats[i].bytes; 262 pkts += stats[i].pkts; 263 } 264 if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) { 265 error_cnt++; 266 printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts); 267 } 268 out: 269 bpf_object__close(obj); 270 } 271 272 int main(void) 273 { 274 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 275 276 setrlimit(RLIMIT_MEMLOCK, &rinf); 277 278 test_pkt_access(); 279 test_xdp(); 280 test_l4lb(); 281 282 printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); 283 return 0; 284 } 285