Commit baaf680e authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Modernize xdp_noinline test w/ skeleton and __noinline

Update xdp_noinline to use BPF skeleton and force __noinline on helper
sub-programs. Also, split existing logic into v4- and v6-only to complicate
sub-program calling patterns (partially overlapped sets of functions for
entry-level BPF programs).
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200903203542.15944-14-andriin@fb.com
parent fab45be1
// 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 <network_helpers.h>
#include "test_xdp_noinline.skel.h"
void test_xdp_noinline(void) void test_xdp_noinline(void)
{ {
const char *file = "./test_xdp_noinline.o";
unsigned int nr_cpus = bpf_num_possible_cpus(); unsigned int nr_cpus = bpf_num_possible_cpus();
struct test_xdp_noinline *skel;
struct vip key = {.protocol = 6}; struct vip key = {.protocol = 6};
struct vip_meta { struct vip_meta {
__u32 flags; __u32 flags;
...@@ -25,58 +26,42 @@ void test_xdp_noinline(void) ...@@ -25,58 +26,42 @@ void test_xdp_noinline(void)
} real_def = {.dst = MAGIC_VAL}; } real_def = {.dst = MAGIC_VAL};
__u32 ch_key = 11, real_num = 3; __u32 ch_key = 11, real_num = 3;
__u32 duration, retval, size; __u32 duration, retval, size;
int err, i, prog_fd, map_fd; int err, i;
__u64 bytes = 0, pkts = 0; __u64 bytes = 0, pkts = 0;
struct bpf_object *obj;
char buf[128]; char buf[128];
u32 *magic = (u32 *)buf; u32 *magic = (u32 *)buf;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); skel = test_xdp_noinline__open_and_load();
if (CHECK_FAIL(err)) if (CHECK(!skel, "skel_open_and_load", "failed\n"))
return; return;
map_fd = bpf_find_map(__func__, obj, "vip_map"); bpf_map_update_elem(bpf_map__fd(skel->maps.vip_map), &key, &value, 0);
if (map_fd < 0) bpf_map_update_elem(bpf_map__fd(skel->maps.ch_rings), &ch_key, &real_num, 0);
goto out; bpf_map_update_elem(bpf_map__fd(skel->maps.reals), &real_num, &real_def, 0);
bpf_map_update_elem(map_fd, &key, &value, 0);
map_fd = bpf_find_map(__func__, obj, "ch_rings"); err = bpf_prog_test_run(bpf_program__fd(skel->progs.balancer_ingress_v4),
if (map_fd < 0) NUM_ITER, &pkt_v4, sizeof(pkt_v4),
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); buf, &size, &retval, &duration);
CHECK(err || retval != 1 || size != 54 || CHECK(err || retval != 1 || size != 54 ||
*magic != MAGIC_VAL, "ipv4", *magic != MAGIC_VAL, "ipv4",
"err %d errno %d retval %d size %d magic %x\n", "err %d errno %d retval %d size %d magic %x\n",
err, errno, retval, size, *magic); err, errno, retval, size, *magic);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6), err = bpf_prog_test_run(bpf_program__fd(skel->progs.balancer_ingress_v6),
NUM_ITER, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration); buf, &size, &retval, &duration);
CHECK(err || retval != 1 || size != 74 || CHECK(err || retval != 1 || size != 74 ||
*magic != MAGIC_VAL, "ipv6", *magic != MAGIC_VAL, "ipv6",
"err %d errno %d retval %d size %d magic %x\n", "err %d errno %d retval %d size %d magic %x\n",
err, errno, retval, size, *magic); err, errno, retval, size, *magic);
map_fd = bpf_find_map(__func__, obj, "stats"); bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats), &stats_key, stats);
if (map_fd < 0)
goto out;
bpf_map_lookup_elem(map_fd, &stats_key, stats);
for (i = 0; i < nr_cpus; i++) { for (i = 0; i < nr_cpus; i++) {
bytes += stats[i].bytes; bytes += stats[i].bytes;
pkts += stats[i].pkts; pkts += stats[i].pkts;
} }
if (CHECK_FAIL(bytes != MAGIC_BYTES * NUM_ITER * 2 || CHECK(bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2,
pkts != NUM_ITER * 2)) { "stats", "bytes %lld pkts %lld\n",
printf("test_xdp_noinline:FAIL:stats %lld %lld\n", (unsigned long long)bytes, (unsigned long long)pkts);
bytes, pkts); test_xdp_noinline__destroy(skel);
}
out:
bpf_object__close(obj);
} }
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h> #include <bpf/bpf_endian.h>
static __u32 rol32(__u32 word, unsigned int shift) static __always_inline __u32 rol32(__u32 word, unsigned int shift)
{ {
return (word << shift) | (word >> ((-shift) & 31)); return (word << shift) | (word >> ((-shift) & 31));
} }
...@@ -49,7 +49,7 @@ static __u32 rol32(__u32 word, unsigned int shift) ...@@ -49,7 +49,7 @@ static __u32 rol32(__u32 word, unsigned int shift)
typedef unsigned int u32; typedef unsigned int u32;
static __attribute__ ((noinline)) static __noinline
u32 jhash(const void *key, u32 length, u32 initval) u32 jhash(const void *key, u32 length, u32 initval)
{ {
u32 a, b, c; u32 a, b, c;
...@@ -86,7 +86,7 @@ u32 jhash(const void *key, u32 length, u32 initval) ...@@ -86,7 +86,7 @@ u32 jhash(const void *key, u32 length, u32 initval)
return c; return c;
} }
__attribute__ ((noinline)) __noinline
u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval) u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
{ {
a += initval; a += initval;
...@@ -96,7 +96,7 @@ u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval) ...@@ -96,7 +96,7 @@ u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
return c; return c;
} }
__attribute__ ((noinline)) __noinline
u32 jhash_2words(u32 a, u32 b, u32 initval) u32 jhash_2words(u32 a, u32 b, u32 initval)
{ {
return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2)); return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
...@@ -213,7 +213,7 @@ struct eth_hdr { ...@@ -213,7 +213,7 @@ struct eth_hdr {
unsigned short eth_proto; unsigned short eth_proto;
}; };
static inline __u64 calc_offset(bool is_ipv6, bool is_icmp) static __noinline __u64 calc_offset(bool is_ipv6, bool is_icmp)
{ {
__u64 off = sizeof(struct eth_hdr); __u64 off = sizeof(struct eth_hdr);
if (is_ipv6) { if (is_ipv6) {
...@@ -797,8 +797,8 @@ static int process_packet(void *data, __u64 off, void *data_end, ...@@ -797,8 +797,8 @@ static int process_packet(void *data, __u64 off, void *data_end,
return XDP_DROP; return XDP_DROP;
} }
__attribute__ ((section("xdp-test"), used)) SEC("xdp-test-v4")
int balancer_ingress(struct xdp_md *ctx) int balancer_ingress_v4(struct xdp_md *ctx)
{ {
void *data = (void *)(long)ctx->data; void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end; void *data_end = (void *)(long)ctx->data_end;
...@@ -812,11 +812,27 @@ int balancer_ingress(struct xdp_md *ctx) ...@@ -812,11 +812,27 @@ int balancer_ingress(struct xdp_md *ctx)
eth_proto = bpf_ntohs(eth->eth_proto); eth_proto = bpf_ntohs(eth->eth_proto);
if (eth_proto == ETH_P_IP) if (eth_proto == ETH_P_IP)
return process_packet(data, nh_off, data_end, 0, ctx); return process_packet(data, nh_off, data_end, 0, ctx);
else if (eth_proto == ETH_P_IPV6) else
return XDP_DROP;
}
SEC("xdp-test-v6")
int balancer_ingress_v6(struct xdp_md *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct eth_hdr *eth = data;
__u32 eth_proto;
__u32 nh_off;
nh_off = sizeof(struct eth_hdr);
if (data + nh_off > data_end)
return XDP_DROP;
eth_proto = bpf_ntohs(eth->eth_proto);
if (eth_proto == ETH_P_IPV6)
return process_packet(data, nh_off, data_end, 1, ctx); return process_packet(data, nh_off, data_end, 1, ctx);
else else
return XDP_DROP; return XDP_DROP;
} }
char _license[] __attribute__ ((section("license"), used)) = "GPL"; char _license[] SEC("license") = "GPL";
int _version __attribute__ ((section("version"), used)) = 1;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment