mirror of https://gitee.com/openkylin/linux.git
selftests/bpf: Move existing common networking parts into network_helpers
1. Move pkt_v4 and pkt_v6 into network_helpers and adjust the users. 2. Copy-paste spin_lock_thread into two tests that use it. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Andrey Ignatov <rdna@fb.com> Link: https://lore.kernel.org/bpf/20200508174611.228805-3-sdf@google.com
This commit is contained in:
parent
33181bb8e8
commit
488a23b89d
|
@ -14,6 +14,23 @@
|
||||||
#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
|
#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
|
||||||
__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
|
__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
|
||||||
|
|
||||||
|
struct ipv4_packet pkt_v4 = {
|
||||||
|
.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
|
||||||
|
.iph.ihl = 5,
|
||||||
|
.iph.protocol = IPPROTO_TCP,
|
||||||
|
.iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
|
||||||
|
.tcp.urg_ptr = 123,
|
||||||
|
.tcp.doff = 5,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ipv6_packet pkt_v6 = {
|
||||||
|
.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
|
||||||
|
.iph.nexthdr = IPPROTO_TCP,
|
||||||
|
.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
|
||||||
|
.tcp.urg_ptr = 123,
|
||||||
|
.tcp.doff = 5,
|
||||||
|
};
|
||||||
|
|
||||||
int start_server(int family, int type)
|
int start_server(int family, int type)
|
||||||
{
|
{
|
||||||
struct sockaddr_storage addr = {};
|
struct sockaddr_storage addr = {};
|
||||||
|
|
|
@ -3,6 +3,35 @@
|
||||||
#define __NETWORK_HELPERS_H
|
#define __NETWORK_HELPERS_H
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <linux/types.h>
|
||||||
|
typedef __u16 __sum16;
|
||||||
|
#include <linux/if_ether.h>
|
||||||
|
#include <linux/if_packet.h>
|
||||||
|
#include <linux/ip.h>
|
||||||
|
#include <linux/ipv6.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
#include <bpf/bpf_endian.h>
|
||||||
|
|
||||||
|
#define MAGIC_VAL 0x1234
|
||||||
|
#define NUM_ITER 100000
|
||||||
|
#define VIP_NUM 5
|
||||||
|
#define MAGIC_BYTES 123
|
||||||
|
|
||||||
|
/* ipv4 test vector */
|
||||||
|
struct ipv4_packet {
|
||||||
|
struct ethhdr eth;
|
||||||
|
struct iphdr iph;
|
||||||
|
struct tcphdr tcp;
|
||||||
|
} __packed;
|
||||||
|
extern struct ipv4_packet pkt_v4;
|
||||||
|
|
||||||
|
/* ipv6 test vector */
|
||||||
|
struct ipv6_packet {
|
||||||
|
struct ethhdr eth;
|
||||||
|
struct ipv6hdr iph;
|
||||||
|
struct tcphdr tcp;
|
||||||
|
} __packed;
|
||||||
|
extern struct ipv6_packet pkt_v6;
|
||||||
|
|
||||||
int start_server(int family, int type);
|
int start_server(int family, int type);
|
||||||
int connect_to_fd(int family, int type, int server_fd);
|
int connect_to_fd(int family, int type, int server_fd);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
/* Copyright (c) 2019 Facebook */
|
/* Copyright (c) 2019 Facebook */
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
static void test_fexit_bpf2bpf_common(const char *obj_file,
|
static void test_fexit_bpf2bpf_common(const char *obj_file,
|
||||||
const char *target_obj_file,
|
const char *target_obj_file,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
#include <error.h>
|
#include <error.h>
|
||||||
#include <linux/if.h>
|
#include <linux/if.h>
|
||||||
#include <linux/if_tun.h>
|
#include <linux/if_tun.h>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_flow_dissector_load_bytes(void)
|
void test_flow_dissector_load_bytes(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
static void test_global_data_number(struct bpf_object *obj, __u32 duration)
|
static void test_global_data_number(struct bpf_object *obj, __u32 duration)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
struct meta {
|
struct meta {
|
||||||
int ifindex;
|
int ifindex;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
static void test_l4lb(const char *file)
|
static void test_l4lb(const char *file)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
|
static void *spin_lock_thread(void *arg)
|
||||||
|
{
|
||||||
|
__u32 duration, retval;
|
||||||
|
int err, prog_fd = *(u32 *) arg;
|
||||||
|
|
||||||
|
err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
|
||||||
|
NULL, NULL, &retval, &duration);
|
||||||
|
CHECK(err || retval, "",
|
||||||
|
"err %d errno %d retval %d duration %d\n",
|
||||||
|
err, errno, retval, duration);
|
||||||
|
pthread_exit(arg);
|
||||||
|
}
|
||||||
|
|
||||||
static void *parallel_map_access(void *arg)
|
static void *parallel_map_access(void *arg)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_pkt_access(void)
|
void test_pkt_access(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_pkt_md_access(void)
|
void test_pkt_md_access(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_prog_run_xattr(void)
|
void test_prog_run_xattr(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
QUEUE,
|
QUEUE,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
static void sigalrm_handler(int s) {}
|
static void sigalrm_handler(int s) {}
|
||||||
static struct sigaction sigalrm_action = {
|
static struct sigaction sigalrm_action = {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_skb_ctx(void)
|
void test_skb_ctx(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
|
static void *spin_lock_thread(void *arg)
|
||||||
|
{
|
||||||
|
__u32 duration, retval;
|
||||||
|
int err, prog_fd = *(u32 *) arg;
|
||||||
|
|
||||||
|
err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
|
||||||
|
NULL, NULL, &retval, &duration);
|
||||||
|
CHECK(err || retval, "",
|
||||||
|
"err %d errno %d retval %d duration %d\n",
|
||||||
|
err, errno, retval, duration);
|
||||||
|
pthread_exit(arg);
|
||||||
|
}
|
||||||
|
|
||||||
void test_spinlock(void)
|
void test_spinlock(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_xdp(void)
|
void test_xdp(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_xdp_adjust_tail(void)
|
void test_xdp_adjust_tail(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include "test_xdp.skel.h"
|
#include "test_xdp.skel.h"
|
||||||
#include "test_xdp_bpf2bpf.skel.h"
|
#include "test_xdp_bpf2bpf.skel.h"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include <network_helpers.h>
|
||||||
|
|
||||||
void test_xdp_noinline(void)
|
void test_xdp_noinline(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -222,23 +222,6 @@ int test__join_cgroup(const char *path)
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ipv4_packet pkt_v4 = {
|
|
||||||
.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
|
|
||||||
.iph.ihl = 5,
|
|
||||||
.iph.protocol = IPPROTO_TCP,
|
|
||||||
.iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
|
|
||||||
.tcp.urg_ptr = 123,
|
|
||||||
.tcp.doff = 5,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ipv6_packet pkt_v6 = {
|
|
||||||
.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
|
|
||||||
.iph.nexthdr = IPPROTO_TCP,
|
|
||||||
.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
|
|
||||||
.tcp.urg_ptr = 123,
|
|
||||||
.tcp.doff = 5,
|
|
||||||
};
|
|
||||||
|
|
||||||
int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
|
int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
|
||||||
{
|
{
|
||||||
struct bpf_map *map;
|
struct bpf_map *map;
|
||||||
|
@ -358,19 +341,6 @@ int extract_build_id(char *build_id, size_t size)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *spin_lock_thread(void *arg)
|
|
||||||
{
|
|
||||||
__u32 duration, retval;
|
|
||||||
int err, prog_fd = *(u32 *) arg;
|
|
||||||
|
|
||||||
err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
|
|
||||||
NULL, NULL, &retval, &duration);
|
|
||||||
CHECK(err || retval, "",
|
|
||||||
"err %d errno %d retval %d duration %d\n",
|
|
||||||
err, errno, retval, duration);
|
|
||||||
pthread_exit(arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* extern declarations for test funcs */
|
/* extern declarations for test funcs */
|
||||||
#define DEFINE_TEST(name) extern void test_##name(void);
|
#define DEFINE_TEST(name) extern void test_##name(void);
|
||||||
#include <prog_tests/tests.h>
|
#include <prog_tests/tests.h>
|
||||||
|
|
|
@ -87,24 +87,6 @@ extern void test__skip(void);
|
||||||
extern void test__fail(void);
|
extern void test__fail(void);
|
||||||
extern int test__join_cgroup(const char *path);
|
extern int test__join_cgroup(const char *path);
|
||||||
|
|
||||||
#define MAGIC_BYTES 123
|
|
||||||
|
|
||||||
/* ipv4 test vector */
|
|
||||||
struct ipv4_packet {
|
|
||||||
struct ethhdr eth;
|
|
||||||
struct iphdr iph;
|
|
||||||
struct tcphdr tcp;
|
|
||||||
} __packed;
|
|
||||||
extern struct ipv4_packet pkt_v4;
|
|
||||||
|
|
||||||
/* ipv6 test vector */
|
|
||||||
struct ipv6_packet {
|
|
||||||
struct ethhdr eth;
|
|
||||||
struct ipv6hdr iph;
|
|
||||||
struct tcphdr tcp;
|
|
||||||
} __packed;
|
|
||||||
extern struct ipv6_packet pkt_v6;
|
|
||||||
|
|
||||||
#define PRINT_FAIL(format...) \
|
#define PRINT_FAIL(format...) \
|
||||||
({ \
|
({ \
|
||||||
test__fail(); \
|
test__fail(); \
|
||||||
|
@ -143,10 +125,6 @@ extern struct ipv6_packet pkt_v6;
|
||||||
#define CHECK_ATTR(condition, tag, format...) \
|
#define CHECK_ATTR(condition, tag, format...) \
|
||||||
_CHECK(condition, tag, tattr.duration, format)
|
_CHECK(condition, tag, tattr.duration, format)
|
||||||
|
|
||||||
#define MAGIC_VAL 0x1234
|
|
||||||
#define NUM_ITER 100000
|
|
||||||
#define VIP_NUM 5
|
|
||||||
|
|
||||||
static inline __u64 ptr_to_u64(const void *ptr)
|
static inline __u64 ptr_to_u64(const void *ptr)
|
||||||
{
|
{
|
||||||
return (__u64) (unsigned long) ptr;
|
return (__u64) (unsigned long) ptr;
|
||||||
|
@ -156,7 +134,6 @@ int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
|
||||||
int compare_map_keys(int map1_fd, int map2_fd);
|
int compare_map_keys(int map1_fd, int map2_fd);
|
||||||
int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
|
int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
|
||||||
int extract_build_id(char *build_id, size_t size);
|
int extract_build_id(char *build_id, size_t size);
|
||||||
void *spin_lock_thread(void *arg);
|
|
||||||
|
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
|
#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
|
||||||
|
|
Loading…
Reference in New Issue