Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 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> |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 27 | |
| 28 | #include <linux/bpf.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <bpf/bpf.h> |
| 31 | #include <bpf/libbpf.h> |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 32 | #include "test_iptunnel_common.h" |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 33 | #include "bpf_util.h" |
David S. Miller | e06422c | 2017-05-01 12:58:21 -0700 | [diff] [blame] | 34 | #include "bpf_endian.h" |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 35 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 36 | static int error_cnt, pass_cnt; |
| 37 | |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 38 | #define MAGIC_BYTES 123 |
| 39 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 40 | /* ipv4 test vector */ |
| 41 | static struct { |
| 42 | struct ethhdr eth; |
| 43 | struct iphdr iph; |
| 44 | struct tcphdr tcp; |
| 45 | } __packed pkt_v4 = { |
Daniel Borkmann | 43bcf70 | 2017-04-27 01:39:34 +0200 | [diff] [blame] | 46 | .eth.h_proto = bpf_htons(ETH_P_IP), |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 47 | .iph.ihl = 5, |
| 48 | .iph.protocol = 6, |
Daniel Borkmann | 43bcf70 | 2017-04-27 01:39:34 +0200 | [diff] [blame] | 49 | .iph.tot_len = bpf_htons(MAGIC_BYTES), |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 50 | .tcp.urg_ptr = 123, |
| 51 | }; |
| 52 | |
| 53 | /* ipv6 test vector */ |
| 54 | static struct { |
| 55 | struct ethhdr eth; |
| 56 | struct ipv6hdr iph; |
| 57 | struct tcphdr tcp; |
| 58 | } __packed pkt_v6 = { |
Daniel Borkmann | 43bcf70 | 2017-04-27 01:39:34 +0200 | [diff] [blame] | 59 | .eth.h_proto = bpf_htons(ETH_P_IPV6), |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 60 | .iph.nexthdr = 6, |
Daniel Borkmann | 43bcf70 | 2017-04-27 01:39:34 +0200 | [diff] [blame] | 61 | .iph.payload_len = bpf_htons(MAGIC_BYTES), |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 62 | .tcp.urg_ptr = 123, |
| 63 | }; |
| 64 | |
| 65 | #define CHECK(condition, tag, format...) ({ \ |
| 66 | int __ret = !!(condition); \ |
| 67 | if (__ret) { \ |
| 68 | error_cnt++; \ |
| 69 | printf("%s:FAIL:%s ", __func__, tag); \ |
| 70 | printf(format); \ |
| 71 | } else { \ |
| 72 | pass_cnt++; \ |
| 73 | printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\ |
| 74 | } \ |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 75 | __ret; \ |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 76 | }) |
| 77 | |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 78 | static int bpf_find_map(const char *test, struct bpf_object *obj, |
| 79 | const char *name) |
| 80 | { |
| 81 | struct bpf_map *map; |
| 82 | |
| 83 | map = bpf_object__find_map_by_name(obj, name); |
| 84 | if (!map) { |
| 85 | printf("%s:FAIL:map '%s' not found\n", test, name); |
| 86 | error_cnt++; |
| 87 | return -1; |
| 88 | } |
| 89 | return bpf_map__fd(map); |
| 90 | } |
| 91 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 92 | static void test_pkt_access(void) |
| 93 | { |
| 94 | const char *file = "./test_pkt_access.o"; |
| 95 | struct bpf_object *obj; |
| 96 | __u32 duration, retval; |
| 97 | int err, prog_fd; |
| 98 | |
| 99 | err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 100 | if (err) { |
| 101 | error_cnt++; |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 102 | return; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 103 | } |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 104 | |
| 105 | err = bpf_prog_test_run(prog_fd, 100000, &pkt_v4, sizeof(pkt_v4), |
| 106 | NULL, NULL, &retval, &duration); |
| 107 | CHECK(err || errno || retval, "ipv4", |
| 108 | "err %d errno %d retval %d duration %d\n", |
| 109 | err, errno, retval, duration); |
| 110 | |
| 111 | err = bpf_prog_test_run(prog_fd, 100000, &pkt_v6, sizeof(pkt_v6), |
| 112 | NULL, NULL, &retval, &duration); |
| 113 | CHECK(err || errno || retval, "ipv6", |
| 114 | "err %d errno %d retval %d duration %d\n", |
| 115 | err, errno, retval, duration); |
| 116 | bpf_object__close(obj); |
| 117 | } |
| 118 | |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 119 | static void test_xdp(void) |
| 120 | { |
| 121 | struct vip key4 = {.protocol = 6, .family = AF_INET}; |
| 122 | struct vip key6 = {.protocol = 6, .family = AF_INET6}; |
| 123 | struct iptnl_info value4 = {.family = AF_INET}; |
| 124 | struct iptnl_info value6 = {.family = AF_INET6}; |
| 125 | const char *file = "./test_xdp.o"; |
| 126 | struct bpf_object *obj; |
| 127 | char buf[128]; |
| 128 | struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr); |
| 129 | struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); |
| 130 | __u32 duration, retval, size; |
| 131 | int err, prog_fd, map_fd; |
| 132 | |
| 133 | err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 134 | if (err) { |
| 135 | error_cnt++; |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 136 | return; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 137 | } |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 138 | |
| 139 | map_fd = bpf_find_map(__func__, obj, "vip2tnl"); |
| 140 | if (map_fd < 0) |
| 141 | goto out; |
| 142 | bpf_map_update_elem(map_fd, &key4, &value4, 0); |
| 143 | bpf_map_update_elem(map_fd, &key6, &value6, 0); |
| 144 | |
| 145 | err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), |
| 146 | buf, &size, &retval, &duration); |
| 147 | |
| 148 | CHECK(err || errno || retval != XDP_TX || size != 74 || |
| 149 | iph->protocol != IPPROTO_IPIP, "ipv4", |
| 150 | "err %d errno %d retval %d size %d\n", |
| 151 | err, errno, retval, size); |
| 152 | |
| 153 | err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6), |
| 154 | buf, &size, &retval, &duration); |
| 155 | CHECK(err || errno || retval != XDP_TX || size != 114 || |
| 156 | iph6->nexthdr != IPPROTO_IPV6, "ipv6", |
| 157 | "err %d errno %d retval %d size %d\n", |
| 158 | err, errno, retval, size); |
| 159 | out: |
| 160 | bpf_object__close(obj); |
| 161 | } |
| 162 | |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 163 | #define MAGIC_VAL 0x1234 |
| 164 | #define NUM_ITER 100000 |
| 165 | #define VIP_NUM 5 |
| 166 | |
| 167 | static void test_l4lb(void) |
| 168 | { |
| 169 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
| 170 | const char *file = "./test_l4lb.o"; |
| 171 | struct vip key = {.protocol = 6}; |
| 172 | struct vip_meta { |
| 173 | __u32 flags; |
| 174 | __u32 vip_num; |
| 175 | } value = {.vip_num = VIP_NUM}; |
| 176 | __u32 stats_key = VIP_NUM; |
| 177 | struct vip_stats { |
| 178 | __u64 bytes; |
| 179 | __u64 pkts; |
| 180 | } stats[nr_cpus]; |
| 181 | struct real_definition { |
| 182 | union { |
| 183 | __be32 dst; |
| 184 | __be32 dstv6[4]; |
| 185 | }; |
| 186 | __u8 flags; |
| 187 | } real_def = {.dst = MAGIC_VAL}; |
| 188 | __u32 ch_key = 11, real_num = 3; |
| 189 | __u32 duration, retval, size; |
| 190 | int err, i, prog_fd, map_fd; |
| 191 | __u64 bytes = 0, pkts = 0; |
| 192 | struct bpf_object *obj; |
| 193 | char buf[128]; |
| 194 | u32 *magic = (u32 *)buf; |
| 195 | |
| 196 | err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 197 | if (err) { |
| 198 | error_cnt++; |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 199 | return; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 200 | } |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 201 | |
| 202 | map_fd = bpf_find_map(__func__, obj, "vip_map"); |
| 203 | if (map_fd < 0) |
| 204 | goto out; |
| 205 | bpf_map_update_elem(map_fd, &key, &value, 0); |
| 206 | |
| 207 | map_fd = bpf_find_map(__func__, obj, "ch_rings"); |
| 208 | if (map_fd < 0) |
| 209 | goto out; |
| 210 | bpf_map_update_elem(map_fd, &ch_key, &real_num, 0); |
| 211 | |
| 212 | map_fd = bpf_find_map(__func__, obj, "reals"); |
| 213 | if (map_fd < 0) |
| 214 | goto out; |
| 215 | bpf_map_update_elem(map_fd, &real_num, &real_def, 0); |
| 216 | |
| 217 | err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4), |
| 218 | buf, &size, &retval, &duration); |
| 219 | CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 || |
| 220 | *magic != MAGIC_VAL, "ipv4", |
| 221 | "err %d errno %d retval %d size %d magic %x\n", |
| 222 | err, errno, retval, size, *magic); |
| 223 | |
| 224 | err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6), |
| 225 | buf, &size, &retval, &duration); |
| 226 | CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 || |
| 227 | *magic != MAGIC_VAL, "ipv6", |
| 228 | "err %d errno %d retval %d size %d magic %x\n", |
| 229 | err, errno, retval, size, *magic); |
| 230 | |
| 231 | map_fd = bpf_find_map(__func__, obj, "stats"); |
| 232 | if (map_fd < 0) |
| 233 | goto out; |
| 234 | bpf_map_lookup_elem(map_fd, &stats_key, stats); |
| 235 | for (i = 0; i < nr_cpus; i++) { |
| 236 | bytes += stats[i].bytes; |
| 237 | pkts += stats[i].pkts; |
| 238 | } |
| 239 | if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) { |
| 240 | error_cnt++; |
| 241 | printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts); |
| 242 | } |
| 243 | out: |
| 244 | bpf_object__close(obj); |
| 245 | } |
| 246 | |
Yonghong Song | 6ead18f | 2017-05-02 19:58:14 -0700 | [diff] [blame] | 247 | static void test_tcp_estats(void) |
| 248 | { |
| 249 | const char *file = "./test_tcp_estats.o"; |
| 250 | int err, prog_fd; |
| 251 | struct bpf_object *obj; |
| 252 | __u32 duration = 0; |
| 253 | |
| 254 | err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); |
| 255 | CHECK(err, "", "err %d errno %d\n", err, errno); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 256 | if (err) { |
| 257 | error_cnt++; |
Yonghong Song | 6ead18f | 2017-05-02 19:58:14 -0700 | [diff] [blame] | 258 | return; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 259 | } |
Yonghong Song | 6ead18f | 2017-05-02 19:58:14 -0700 | [diff] [blame] | 260 | |
| 261 | bpf_object__close(obj); |
| 262 | } |
| 263 | |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 264 | static inline __u64 ptr_to_u64(const void *ptr) |
| 265 | { |
| 266 | return (__u64) (unsigned long) ptr; |
| 267 | } |
| 268 | |
| 269 | static void test_bpf_obj_id(void) |
| 270 | { |
| 271 | const __u64 array_magic_value = 0xfaceb00c; |
| 272 | const __u32 array_key = 0; |
| 273 | const int nr_iters = 2; |
| 274 | const char *file = "./test_obj_id.o"; |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 275 | const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable"; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 276 | |
| 277 | struct bpf_object *objs[nr_iters]; |
| 278 | int prog_fds[nr_iters], map_fds[nr_iters]; |
| 279 | /* +1 to test for the info_len returned by kernel */ |
| 280 | struct bpf_prog_info prog_infos[nr_iters + 1]; |
| 281 | struct bpf_map_info map_infos[nr_iters + 1]; |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 282 | char jited_insns[128], xlated_insns[128], zeros[128]; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 283 | __u32 i, next_id, info_len, nr_id_found, duration = 0; |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 284 | int sysctl_fd, jit_enabled = 0, err = 0; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 285 | __u64 array_value; |
| 286 | |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 287 | sysctl_fd = open(jit_sysctl, 0, O_RDONLY); |
| 288 | if (sysctl_fd != -1) { |
| 289 | char tmpc; |
| 290 | |
| 291 | if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1) |
| 292 | jit_enabled = (tmpc != '0'); |
| 293 | close(sysctl_fd); |
| 294 | } |
| 295 | |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 296 | err = bpf_prog_get_fd_by_id(0); |
| 297 | CHECK(err >= 0 || errno != ENOENT, |
| 298 | "get-fd-by-notexist-prog-id", "err %d errno %d\n", err, errno); |
| 299 | |
| 300 | err = bpf_map_get_fd_by_id(0); |
| 301 | CHECK(err >= 0 || errno != ENOENT, |
| 302 | "get-fd-by-notexist-map-id", "err %d errno %d\n", err, errno); |
| 303 | |
| 304 | for (i = 0; i < nr_iters; i++) |
| 305 | objs[i] = NULL; |
| 306 | |
| 307 | /* Check bpf_obj_get_info_by_fd() */ |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 308 | bzero(zeros, sizeof(zeros)); |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 309 | for (i = 0; i < nr_iters; i++) { |
| 310 | err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER, |
| 311 | &objs[i], &prog_fds[i]); |
| 312 | /* test_obj_id.o is a dumb prog. It should never fail |
| 313 | * to load. |
| 314 | */ |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 315 | if (err) |
| 316 | error_cnt++; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 317 | assert(!err); |
| 318 | |
| 319 | /* Check getting prog info */ |
| 320 | info_len = sizeof(struct bpf_prog_info) * 2; |
Jakub Kicinski | d777b2d | 2017-07-25 15:16:12 -0700 | [diff] [blame] | 321 | bzero(&prog_infos[i], info_len); |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 322 | bzero(jited_insns, sizeof(jited_insns)); |
| 323 | bzero(xlated_insns, sizeof(xlated_insns)); |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 324 | prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns); |
| 325 | prog_infos[i].jited_prog_len = sizeof(jited_insns); |
| 326 | prog_infos[i].xlated_prog_insns = ptr_to_u64(xlated_insns); |
| 327 | prog_infos[i].xlated_prog_len = sizeof(xlated_insns); |
| 328 | err = bpf_obj_get_info_by_fd(prog_fds[i], &prog_infos[i], |
| 329 | &info_len); |
| 330 | if (CHECK(err || |
| 331 | prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER || |
| 332 | info_len != sizeof(struct bpf_prog_info) || |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 333 | (jit_enabled && !prog_infos[i].jited_prog_len) || |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 334 | (jit_enabled && |
| 335 | !memcmp(jited_insns, zeros, sizeof(zeros))) || |
| 336 | !prog_infos[i].xlated_prog_len || |
| 337 | !memcmp(xlated_insns, zeros, sizeof(zeros)), |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 338 | "get-prog-info(fd)", |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 339 | "err %d errno %d i %d type %d(%d) info_len %u(%lu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d\n", |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 340 | err, errno, i, |
| 341 | prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER, |
| 342 | info_len, sizeof(struct bpf_prog_info), |
Martin KaFai Lau | fad0743 | 2017-06-08 22:30:16 -0700 | [diff] [blame] | 343 | jit_enabled, |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 344 | prog_infos[i].jited_prog_len, |
Jakub Kicinski | 7cadf2c | 2017-08-25 14:39:57 -0700 | [diff] [blame] | 345 | prog_infos[i].xlated_prog_len, |
| 346 | !!memcmp(jited_insns, zeros, sizeof(zeros)), |
| 347 | !!memcmp(xlated_insns, zeros, sizeof(zeros)))) |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 348 | goto done; |
| 349 | |
| 350 | map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id"); |
| 351 | assert(map_fds[i] >= 0); |
| 352 | err = bpf_map_update_elem(map_fds[i], &array_key, |
| 353 | &array_magic_value, 0); |
| 354 | assert(!err); |
| 355 | |
| 356 | /* Check getting map info */ |
| 357 | info_len = sizeof(struct bpf_map_info) * 2; |
Jakub Kicinski | d777b2d | 2017-07-25 15:16:12 -0700 | [diff] [blame] | 358 | bzero(&map_infos[i], info_len); |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 359 | err = bpf_obj_get_info_by_fd(map_fds[i], &map_infos[i], |
| 360 | &info_len); |
| 361 | if (CHECK(err || |
| 362 | map_infos[i].type != BPF_MAP_TYPE_ARRAY || |
| 363 | map_infos[i].key_size != sizeof(__u32) || |
| 364 | map_infos[i].value_size != sizeof(__u64) || |
| 365 | map_infos[i].max_entries != 1 || |
| 366 | map_infos[i].map_flags != 0 || |
| 367 | info_len != sizeof(struct bpf_map_info), |
| 368 | "get-map-info(fd)", |
| 369 | "err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n", |
| 370 | err, errno, |
| 371 | map_infos[i].type, BPF_MAP_TYPE_ARRAY, |
| 372 | info_len, sizeof(struct bpf_map_info), |
| 373 | map_infos[i].key_size, |
| 374 | map_infos[i].value_size, |
| 375 | map_infos[i].max_entries, |
| 376 | map_infos[i].map_flags)) |
| 377 | goto done; |
| 378 | } |
| 379 | |
| 380 | /* Check bpf_prog_get_next_id() */ |
| 381 | nr_id_found = 0; |
| 382 | next_id = 0; |
| 383 | while (!bpf_prog_get_next_id(next_id, &next_id)) { |
Jakub Kicinski | d777b2d | 2017-07-25 15:16:12 -0700 | [diff] [blame] | 384 | struct bpf_prog_info prog_info = {}; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 385 | int prog_fd; |
| 386 | |
| 387 | info_len = sizeof(prog_info); |
| 388 | |
| 389 | prog_fd = bpf_prog_get_fd_by_id(next_id); |
| 390 | if (prog_fd < 0 && errno == ENOENT) |
| 391 | /* The bpf_prog is in the dead row */ |
| 392 | continue; |
| 393 | if (CHECK(prog_fd < 0, "get-prog-fd(next_id)", |
| 394 | "prog_fd %d next_id %d errno %d\n", |
| 395 | prog_fd, next_id, errno)) |
| 396 | break; |
| 397 | |
| 398 | for (i = 0; i < nr_iters; i++) |
| 399 | if (prog_infos[i].id == next_id) |
| 400 | break; |
| 401 | |
| 402 | if (i == nr_iters) |
| 403 | continue; |
| 404 | |
| 405 | nr_id_found++; |
| 406 | |
| 407 | err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len); |
Jakub Kicinski | d777b2d | 2017-07-25 15:16:12 -0700 | [diff] [blame] | 408 | prog_infos[i].jited_prog_insns = 0; |
| 409 | prog_infos[i].xlated_prog_insns = 0; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 410 | CHECK(err || info_len != sizeof(struct bpf_prog_info) || |
| 411 | memcmp(&prog_info, &prog_infos[i], info_len), |
| 412 | "get-prog-info(next_id->fd)", |
| 413 | "err %d errno %d info_len %u(%lu) memcmp %d\n", |
| 414 | err, errno, info_len, sizeof(struct bpf_prog_info), |
| 415 | memcmp(&prog_info, &prog_infos[i], info_len)); |
| 416 | |
| 417 | close(prog_fd); |
| 418 | } |
| 419 | CHECK(nr_id_found != nr_iters, |
| 420 | "check total prog id found by get_next_id", |
| 421 | "nr_id_found %u(%u)\n", |
| 422 | nr_id_found, nr_iters); |
| 423 | |
| 424 | /* Check bpf_map_get_next_id() */ |
| 425 | nr_id_found = 0; |
| 426 | next_id = 0; |
| 427 | while (!bpf_map_get_next_id(next_id, &next_id)) { |
Jakub Kicinski | d777b2d | 2017-07-25 15:16:12 -0700 | [diff] [blame] | 428 | struct bpf_map_info map_info = {}; |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 429 | int map_fd; |
| 430 | |
| 431 | info_len = sizeof(map_info); |
| 432 | |
| 433 | map_fd = bpf_map_get_fd_by_id(next_id); |
| 434 | if (map_fd < 0 && errno == ENOENT) |
| 435 | /* The bpf_map is in the dead row */ |
| 436 | continue; |
| 437 | if (CHECK(map_fd < 0, "get-map-fd(next_id)", |
| 438 | "map_fd %d next_id %u errno %d\n", |
| 439 | map_fd, next_id, errno)) |
| 440 | break; |
| 441 | |
| 442 | for (i = 0; i < nr_iters; i++) |
| 443 | if (map_infos[i].id == next_id) |
| 444 | break; |
| 445 | |
| 446 | if (i == nr_iters) |
| 447 | continue; |
| 448 | |
| 449 | nr_id_found++; |
| 450 | |
| 451 | err = bpf_map_lookup_elem(map_fd, &array_key, &array_value); |
| 452 | assert(!err); |
| 453 | |
| 454 | err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len); |
| 455 | CHECK(err || info_len != sizeof(struct bpf_map_info) || |
| 456 | memcmp(&map_info, &map_infos[i], info_len) || |
| 457 | array_value != array_magic_value, |
| 458 | "check get-map-info(next_id->fd)", |
| 459 | "err %d errno %d info_len %u(%lu) memcmp %d array_value %llu(%llu)\n", |
| 460 | err, errno, info_len, sizeof(struct bpf_map_info), |
| 461 | memcmp(&map_info, &map_infos[i], info_len), |
| 462 | array_value, array_magic_value); |
| 463 | |
| 464 | close(map_fd); |
| 465 | } |
| 466 | CHECK(nr_id_found != nr_iters, |
| 467 | "check total map id found by get_next_id", |
| 468 | "nr_id_found %u(%u)\n", |
| 469 | nr_id_found, nr_iters); |
| 470 | |
| 471 | done: |
| 472 | for (i = 0; i < nr_iters; i++) |
| 473 | bpf_object__close(objs[i]); |
| 474 | } |
| 475 | |
Yonghong Song | 18f3d6b | 2017-06-13 15:52:14 -0700 | [diff] [blame] | 476 | static void test_pkt_md_access(void) |
| 477 | { |
| 478 | const char *file = "./test_pkt_md_access.o"; |
| 479 | struct bpf_object *obj; |
| 480 | __u32 duration, retval; |
| 481 | int err, prog_fd; |
| 482 | |
| 483 | err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 484 | if (err) { |
| 485 | error_cnt++; |
Yonghong Song | 18f3d6b | 2017-06-13 15:52:14 -0700 | [diff] [blame] | 486 | return; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 487 | } |
Yonghong Song | 18f3d6b | 2017-06-13 15:52:14 -0700 | [diff] [blame] | 488 | |
| 489 | err = bpf_prog_test_run(prog_fd, 10, &pkt_v4, sizeof(pkt_v4), |
| 490 | NULL, NULL, &retval, &duration); |
| 491 | CHECK(err || retval, "", |
| 492 | "err %d errno %d retval %d duration %d\n", |
| 493 | err, errno, retval, duration); |
| 494 | |
| 495 | bpf_object__close(obj); |
| 496 | } |
| 497 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 498 | int main(void) |
| 499 | { |
| 500 | struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; |
| 501 | |
| 502 | setrlimit(RLIMIT_MEMLOCK, &rinf); |
| 503 | |
| 504 | test_pkt_access(); |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame] | 505 | test_xdp(); |
Alexei Starovoitov | 3782161 | 2017-03-30 21:45:43 -0700 | [diff] [blame] | 506 | test_l4lb(); |
Yonghong Song | 6ead18f | 2017-05-02 19:58:14 -0700 | [diff] [blame] | 507 | test_tcp_estats(); |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 508 | test_bpf_obj_id(); |
Yonghong Song | 18f3d6b | 2017-06-13 15:52:14 -0700 | [diff] [blame] | 509 | test_pkt_md_access(); |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 510 | |
| 511 | printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); |
Jesper Dangaard Brouer | efe5f9c | 2017-06-13 15:17:19 +0200 | [diff] [blame] | 512 | return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS; |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 513 | } |