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> |
| 25 | |
| 26 | #include <linux/bpf.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <bpf/bpf.h> |
| 29 | #include <bpf/libbpf.h> |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame^] | 30 | #include "test_iptunnel_common.h" |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 31 | |
| 32 | #define _htons __builtin_bswap16 |
| 33 | |
| 34 | static int error_cnt, pass_cnt; |
| 35 | |
| 36 | /* ipv4 test vector */ |
| 37 | static struct { |
| 38 | struct ethhdr eth; |
| 39 | struct iphdr iph; |
| 40 | struct tcphdr tcp; |
| 41 | } __packed pkt_v4 = { |
| 42 | .eth.h_proto = _htons(ETH_P_IP), |
| 43 | .iph.ihl = 5, |
| 44 | .iph.protocol = 6, |
| 45 | .tcp.urg_ptr = 123, |
| 46 | }; |
| 47 | |
| 48 | /* ipv6 test vector */ |
| 49 | static struct { |
| 50 | struct ethhdr eth; |
| 51 | struct ipv6hdr iph; |
| 52 | struct tcphdr tcp; |
| 53 | } __packed pkt_v6 = { |
| 54 | .eth.h_proto = _htons(ETH_P_IPV6), |
| 55 | .iph.nexthdr = 6, |
| 56 | .tcp.urg_ptr = 123, |
| 57 | }; |
| 58 | |
| 59 | #define CHECK(condition, tag, format...) ({ \ |
| 60 | int __ret = !!(condition); \ |
| 61 | if (__ret) { \ |
| 62 | error_cnt++; \ |
| 63 | printf("%s:FAIL:%s ", __func__, tag); \ |
| 64 | printf(format); \ |
| 65 | } else { \ |
| 66 | pass_cnt++; \ |
| 67 | printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\ |
| 68 | } \ |
| 69 | }) |
| 70 | |
| 71 | static int bpf_prog_load(const char *file, enum bpf_prog_type type, |
| 72 | struct bpf_object **pobj, int *prog_fd) |
| 73 | { |
| 74 | struct bpf_program *prog; |
| 75 | struct bpf_object *obj; |
| 76 | int err; |
| 77 | |
| 78 | obj = bpf_object__open(file); |
| 79 | if (IS_ERR(obj)) { |
| 80 | error_cnt++; |
| 81 | return -ENOENT; |
| 82 | } |
| 83 | |
| 84 | prog = bpf_program__next(NULL, obj); |
| 85 | if (!prog) { |
| 86 | bpf_object__close(obj); |
| 87 | error_cnt++; |
| 88 | return -ENOENT; |
| 89 | } |
| 90 | |
| 91 | bpf_program__set_type(prog, type); |
| 92 | err = bpf_object__load(obj); |
| 93 | if (err) { |
| 94 | bpf_object__close(obj); |
| 95 | error_cnt++; |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | *pobj = obj; |
| 100 | *prog_fd = bpf_program__fd(prog); |
| 101 | return 0; |
| 102 | } |
| 103 | |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame^] | 104 | static int bpf_find_map(const char *test, struct bpf_object *obj, |
| 105 | const char *name) |
| 106 | { |
| 107 | struct bpf_map *map; |
| 108 | |
| 109 | map = bpf_object__find_map_by_name(obj, name); |
| 110 | if (!map) { |
| 111 | printf("%s:FAIL:map '%s' not found\n", test, name); |
| 112 | error_cnt++; |
| 113 | return -1; |
| 114 | } |
| 115 | return bpf_map__fd(map); |
| 116 | } |
| 117 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 118 | static void test_pkt_access(void) |
| 119 | { |
| 120 | const char *file = "./test_pkt_access.o"; |
| 121 | struct bpf_object *obj; |
| 122 | __u32 duration, retval; |
| 123 | int err, prog_fd; |
| 124 | |
| 125 | err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); |
| 126 | if (err) |
| 127 | return; |
| 128 | |
| 129 | err = bpf_prog_test_run(prog_fd, 100000, &pkt_v4, sizeof(pkt_v4), |
| 130 | NULL, NULL, &retval, &duration); |
| 131 | CHECK(err || errno || retval, "ipv4", |
| 132 | "err %d errno %d retval %d duration %d\n", |
| 133 | err, errno, retval, duration); |
| 134 | |
| 135 | err = bpf_prog_test_run(prog_fd, 100000, &pkt_v6, sizeof(pkt_v6), |
| 136 | NULL, NULL, &retval, &duration); |
| 137 | CHECK(err || errno || retval, "ipv6", |
| 138 | "err %d errno %d retval %d duration %d\n", |
| 139 | err, errno, retval, duration); |
| 140 | bpf_object__close(obj); |
| 141 | } |
| 142 | |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame^] | 143 | static void test_xdp(void) |
| 144 | { |
| 145 | struct vip key4 = {.protocol = 6, .family = AF_INET}; |
| 146 | struct vip key6 = {.protocol = 6, .family = AF_INET6}; |
| 147 | struct iptnl_info value4 = {.family = AF_INET}; |
| 148 | struct iptnl_info value6 = {.family = AF_INET6}; |
| 149 | const char *file = "./test_xdp.o"; |
| 150 | struct bpf_object *obj; |
| 151 | char buf[128]; |
| 152 | struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr); |
| 153 | struct iphdr *iph = (void *)buf + sizeof(struct ethhdr); |
| 154 | __u32 duration, retval, size; |
| 155 | int err, prog_fd, map_fd; |
| 156 | |
| 157 | err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); |
| 158 | if (err) |
| 159 | return; |
| 160 | |
| 161 | map_fd = bpf_find_map(__func__, obj, "vip2tnl"); |
| 162 | if (map_fd < 0) |
| 163 | goto out; |
| 164 | bpf_map_update_elem(map_fd, &key4, &value4, 0); |
| 165 | bpf_map_update_elem(map_fd, &key6, &value6, 0); |
| 166 | |
| 167 | err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), |
| 168 | buf, &size, &retval, &duration); |
| 169 | |
| 170 | CHECK(err || errno || retval != XDP_TX || size != 74 || |
| 171 | iph->protocol != IPPROTO_IPIP, "ipv4", |
| 172 | "err %d errno %d retval %d size %d\n", |
| 173 | err, errno, retval, size); |
| 174 | |
| 175 | err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6), |
| 176 | buf, &size, &retval, &duration); |
| 177 | CHECK(err || errno || retval != XDP_TX || size != 114 || |
| 178 | iph6->nexthdr != IPPROTO_IPV6, "ipv6", |
| 179 | "err %d errno %d retval %d size %d\n", |
| 180 | err, errno, retval, size); |
| 181 | out: |
| 182 | bpf_object__close(obj); |
| 183 | } |
| 184 | |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 185 | int main(void) |
| 186 | { |
| 187 | struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; |
| 188 | |
| 189 | setrlimit(RLIMIT_MEMLOCK, &rinf); |
| 190 | |
| 191 | test_pkt_access(); |
Alexei Starovoitov | 8d48f5e | 2017-03-30 21:45:42 -0700 | [diff] [blame^] | 192 | test_xdp(); |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 193 | |
| 194 | printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); |
| 195 | return 0; |
| 196 | } |