2017-03-31 12:45:41 +08:00
|
|
|
/* Copyright (c) 2017 Facebook
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
typedef __u16 __sum16;
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <linux/tcp.h>
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <bpf/libbpf.h>
|
2017-03-31 12:45:42 +08:00
|
|
|
#include "test_iptunnel_common.h"
|
2017-03-31 12:45:43 +08:00
|
|
|
#include "bpf_util.h"
|
2017-05-02 03:58:21 +08:00
|
|
|
#include "bpf_endian.h"
|
2017-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
static int error_cnt, pass_cnt;
|
|
|
|
|
2017-03-31 12:45:43 +08:00
|
|
|
#define MAGIC_BYTES 123
|
|
|
|
|
2017-03-31 12:45:41 +08:00
|
|
|
/* ipv4 test vector */
|
|
|
|
static struct {
|
|
|
|
struct ethhdr eth;
|
|
|
|
struct iphdr iph;
|
|
|
|
struct tcphdr tcp;
|
|
|
|
} __packed pkt_v4 = {
|
2017-04-27 07:39:34 +08:00
|
|
|
.eth.h_proto = bpf_htons(ETH_P_IP),
|
2017-03-31 12:45:41 +08:00
|
|
|
.iph.ihl = 5,
|
|
|
|
.iph.protocol = 6,
|
2017-04-27 07:39:34 +08:00
|
|
|
.iph.tot_len = bpf_htons(MAGIC_BYTES),
|
2017-03-31 12:45:41 +08:00
|
|
|
.tcp.urg_ptr = 123,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ipv6 test vector */
|
|
|
|
static struct {
|
|
|
|
struct ethhdr eth;
|
|
|
|
struct ipv6hdr iph;
|
|
|
|
struct tcphdr tcp;
|
|
|
|
} __packed pkt_v6 = {
|
2017-04-27 07:39:34 +08:00
|
|
|
.eth.h_proto = bpf_htons(ETH_P_IPV6),
|
2017-03-31 12:45:41 +08:00
|
|
|
.iph.nexthdr = 6,
|
2017-04-27 07:39:34 +08:00
|
|
|
.iph.payload_len = bpf_htons(MAGIC_BYTES),
|
2017-03-31 12:45:41 +08:00
|
|
|
.tcp.urg_ptr = 123,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CHECK(condition, tag, format...) ({ \
|
|
|
|
int __ret = !!(condition); \
|
|
|
|
if (__ret) { \
|
|
|
|
error_cnt++; \
|
|
|
|
printf("%s:FAIL:%s ", __func__, tag); \
|
|
|
|
printf(format); \
|
|
|
|
} else { \
|
|
|
|
pass_cnt++; \
|
|
|
|
printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
|
|
|
static int bpf_prog_load(const char *file, enum bpf_prog_type type,
|
|
|
|
struct bpf_object **pobj, int *prog_fd)
|
|
|
|
{
|
|
|
|
struct bpf_program *prog;
|
|
|
|
struct bpf_object *obj;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
obj = bpf_object__open(file);
|
|
|
|
if (IS_ERR(obj)) {
|
|
|
|
error_cnt++;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = bpf_program__next(NULL, obj);
|
|
|
|
if (!prog) {
|
|
|
|
bpf_object__close(obj);
|
|
|
|
error_cnt++;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpf_program__set_type(prog, type);
|
|
|
|
err = bpf_object__load(obj);
|
|
|
|
if (err) {
|
|
|
|
bpf_object__close(obj);
|
|
|
|
error_cnt++;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pobj = obj;
|
|
|
|
*prog_fd = bpf_program__fd(prog);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:45:42 +08:00
|
|
|
static int bpf_find_map(const char *test, struct bpf_object *obj,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct bpf_map *map;
|
|
|
|
|
|
|
|
map = bpf_object__find_map_by_name(obj, name);
|
|
|
|
if (!map) {
|
|
|
|
printf("%s:FAIL:map '%s' not found\n", test, name);
|
|
|
|
error_cnt++;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bpf_map__fd(map);
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:45:41 +08:00
|
|
|
static void test_pkt_access(void)
|
|
|
|
{
|
|
|
|
const char *file = "./test_pkt_access.o";
|
|
|
|
struct bpf_object *obj;
|
|
|
|
__u32 duration, retval;
|
|
|
|
int err, prog_fd;
|
|
|
|
|
|
|
|
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, 100000, &pkt_v4, sizeof(pkt_v4),
|
|
|
|
NULL, NULL, &retval, &duration);
|
|
|
|
CHECK(err || errno || retval, "ipv4",
|
|
|
|
"err %d errno %d retval %d duration %d\n",
|
|
|
|
err, errno, retval, duration);
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, 100000, &pkt_v6, sizeof(pkt_v6),
|
|
|
|
NULL, NULL, &retval, &duration);
|
|
|
|
CHECK(err || errno || retval, "ipv6",
|
|
|
|
"err %d errno %d retval %d duration %d\n",
|
|
|
|
err, errno, retval, duration);
|
|
|
|
bpf_object__close(obj);
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:45:42 +08:00
|
|
|
static void test_xdp(void)
|
|
|
|
{
|
|
|
|
struct vip key4 = {.protocol = 6, .family = AF_INET};
|
|
|
|
struct vip key6 = {.protocol = 6, .family = AF_INET6};
|
|
|
|
struct iptnl_info value4 = {.family = AF_INET};
|
|
|
|
struct iptnl_info value6 = {.family = AF_INET6};
|
|
|
|
const char *file = "./test_xdp.o";
|
|
|
|
struct bpf_object *obj;
|
|
|
|
char buf[128];
|
|
|
|
struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr);
|
|
|
|
struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
|
|
|
|
__u32 duration, retval, size;
|
|
|
|
int err, prog_fd, map_fd;
|
|
|
|
|
|
|
|
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
map_fd = bpf_find_map(__func__, obj, "vip2tnl");
|
|
|
|
if (map_fd < 0)
|
|
|
|
goto out;
|
|
|
|
bpf_map_update_elem(map_fd, &key4, &value4, 0);
|
|
|
|
bpf_map_update_elem(map_fd, &key6, &value6, 0);
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
|
|
|
|
buf, &size, &retval, &duration);
|
|
|
|
|
|
|
|
CHECK(err || errno || retval != XDP_TX || size != 74 ||
|
|
|
|
iph->protocol != IPPROTO_IPIP, "ipv4",
|
|
|
|
"err %d errno %d retval %d size %d\n",
|
|
|
|
err, errno, retval, size);
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
|
|
|
|
buf, &size, &retval, &duration);
|
|
|
|
CHECK(err || errno || retval != XDP_TX || size != 114 ||
|
|
|
|
iph6->nexthdr != IPPROTO_IPV6, "ipv6",
|
|
|
|
"err %d errno %d retval %d size %d\n",
|
|
|
|
err, errno, retval, size);
|
|
|
|
out:
|
|
|
|
bpf_object__close(obj);
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:45:43 +08:00
|
|
|
#define MAGIC_VAL 0x1234
|
|
|
|
#define NUM_ITER 100000
|
|
|
|
#define VIP_NUM 5
|
|
|
|
|
|
|
|
static void test_l4lb(void)
|
|
|
|
{
|
|
|
|
unsigned int nr_cpus = bpf_num_possible_cpus();
|
|
|
|
const char *file = "./test_l4lb.o";
|
|
|
|
struct vip key = {.protocol = 6};
|
|
|
|
struct vip_meta {
|
|
|
|
__u32 flags;
|
|
|
|
__u32 vip_num;
|
|
|
|
} value = {.vip_num = VIP_NUM};
|
|
|
|
__u32 stats_key = VIP_NUM;
|
|
|
|
struct vip_stats {
|
|
|
|
__u64 bytes;
|
|
|
|
__u64 pkts;
|
|
|
|
} stats[nr_cpus];
|
|
|
|
struct real_definition {
|
|
|
|
union {
|
|
|
|
__be32 dst;
|
|
|
|
__be32 dstv6[4];
|
|
|
|
};
|
|
|
|
__u8 flags;
|
|
|
|
} real_def = {.dst = MAGIC_VAL};
|
|
|
|
__u32 ch_key = 11, real_num = 3;
|
|
|
|
__u32 duration, retval, size;
|
|
|
|
int err, i, prog_fd, map_fd;
|
|
|
|
__u64 bytes = 0, pkts = 0;
|
|
|
|
struct bpf_object *obj;
|
|
|
|
char buf[128];
|
|
|
|
u32 *magic = (u32 *)buf;
|
|
|
|
|
|
|
|
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
map_fd = bpf_find_map(__func__, obj, "vip_map");
|
|
|
|
if (map_fd < 0)
|
|
|
|
goto out;
|
|
|
|
bpf_map_update_elem(map_fd, &key, &value, 0);
|
|
|
|
|
|
|
|
map_fd = bpf_find_map(__func__, obj, "ch_rings");
|
|
|
|
if (map_fd < 0)
|
|
|
|
goto out;
|
|
|
|
bpf_map_update_elem(map_fd, &ch_key, &real_num, 0);
|
|
|
|
|
|
|
|
map_fd = bpf_find_map(__func__, obj, "reals");
|
|
|
|
if (map_fd < 0)
|
|
|
|
goto out;
|
|
|
|
bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
|
|
|
|
buf, &size, &retval, &duration);
|
|
|
|
CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 ||
|
|
|
|
*magic != MAGIC_VAL, "ipv4",
|
|
|
|
"err %d errno %d retval %d size %d magic %x\n",
|
|
|
|
err, errno, retval, size, *magic);
|
|
|
|
|
|
|
|
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
|
|
|
|
buf, &size, &retval, &duration);
|
|
|
|
CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 ||
|
|
|
|
*magic != MAGIC_VAL, "ipv6",
|
|
|
|
"err %d errno %d retval %d size %d magic %x\n",
|
|
|
|
err, errno, retval, size, *magic);
|
|
|
|
|
|
|
|
map_fd = bpf_find_map(__func__, obj, "stats");
|
|
|
|
if (map_fd < 0)
|
|
|
|
goto out;
|
|
|
|
bpf_map_lookup_elem(map_fd, &stats_key, stats);
|
|
|
|
for (i = 0; i < nr_cpus; i++) {
|
|
|
|
bytes += stats[i].bytes;
|
|
|
|
pkts += stats[i].pkts;
|
|
|
|
}
|
|
|
|
if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
|
|
|
|
error_cnt++;
|
|
|
|
printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
bpf_object__close(obj);
|
|
|
|
}
|
|
|
|
|
2017-05-03 10:58:14 +08:00
|
|
|
static void test_tcp_estats(void)
|
|
|
|
{
|
|
|
|
const char *file = "./test_tcp_estats.o";
|
|
|
|
int err, prog_fd;
|
|
|
|
struct bpf_object *obj;
|
|
|
|
__u32 duration = 0;
|
|
|
|
|
|
|
|
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
|
|
|
|
CHECK(err, "", "err %d errno %d\n", err, errno);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bpf_object__close(obj);
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:45:41 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
|
|
|
|
|
|
|
|
setrlimit(RLIMIT_MEMLOCK, &rinf);
|
|
|
|
|
|
|
|
test_pkt_access();
|
2017-03-31 12:45:42 +08:00
|
|
|
test_xdp();
|
2017-03-31 12:45:43 +08:00
|
|
|
test_l4lb();
|
2017-05-03 10:58:14 +08:00
|
|
|
test_tcp_estats();
|
2017-03-31 12:45:41 +08:00
|
|
|
|
|
|
|
printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
|
|
|
|
return 0;
|
|
|
|
}
|