selftests/bpf: Test XDP LRU hash rolling-window map updates
Subsystems (6)
RFC 1/1 selftests/bpf: Test XDP LRU hash rolling-window map updates
test_lru_map covers LRU_HASH from syscall helpers only. Existing XDP selftests parse packets but do not store a modulo-bounded window in a map value.
Add a small XDP program that writes packet lengths into BPF_MAP_TYPE_LRU_HASH at seq % AGGREGATION_WINDOW, plus a test_progs case that uses bpf_prog_test_run to check wrap, parse-fail isolation, and 5-tuple isolation.
--- .../selftests/bpf/prog_tests/xdp_lru_window.c | 171 ++++++++++++++++++ .../selftests/bpf/progs/xdp_lru_window.c | 81 +++++++++ tools/testing/selftests/bpf/xdp_lru_window.h | 28 +++ 3 files changed, 280 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_lru_window.c create mode 100644 tools/testing/selftests/bpf/progs/xdp_lru_window.c create mode 100644 tools/testing/selftests/bpf/xdp_lru_window.h
tools/testing/selftests/bpf/prog_tests/xdp_lru_window.c new file +171 -0
@@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <network_helpers.h> +#include "xdp_lru_window.h" +#include "xdp_lru_window.skel.h" + +#define SRC_IP 0x0a000001 +#define DST_IP 0x0a000002 +#define SRC_PORT 12345 +#define DST_PORT 80 +#define ALT_DST_PORT 81 + +static struct xdp_lru_window *skel; +static int prog_fd, map_fd; + +static void fill_pkt (struct ipv4_packet *pkt,__u16 dport) +{ + *pkt = pkt_v4; + pkt->iph.saddr =htonl (SRC_IP ); + pkt->iph.daddr =htonl (DST_IP ); + pkt->tcp.source =htons (SRC_PORT ); + pkt->tcp.dest =htons (dport); +} + +static void fill_key (struct xdp_lru_window_key *key,__u16 dport) +{ +memset (key,0 ,sizeof (*key)); + key->saddr =htonl (SRC_IP ); + key->daddr =htonl (DST_IP ); + key->sport =htons (SRC_PORT ); + key->dport =htons (dport); + key->proto =IPPROTO_TCP ; +} + +static int run_pkt (const void *data,__u32 len,int *retval) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts, + .data_in = data, + .data_size_in = len, + .repeat = 1, + ); +int err; + + err =bpf_prog_test_run_opts (prog_fd, &opts); +if (!ASSERT_OK (err,"test_run" )) +return err; +if (retval) + *retval = opts.retval; +return 0 ; +} + +static int inject (int n,__u16 dport) +{ +struct ipv4_packet pkt; +int i, retval; + +fill_pkt (&pkt, dport); +for (i =0 ; i < n; i++) { +if (run_pkt (&pkt,sizeof (pkt), &retval)) +return -1 ; +if (!ASSERT_EQ (retval,XDP_PASS ,"retval" )) +return -1 ; + } +return 0 ; +} + +static void reset_map (void ) +{ +struct xdp_lru_window_key key, next; +int err; + + err =bpf_map_get_next_key (map_fd,NULL , &next); +while (!err) { + key = next; + err =bpf_map_get_next_key (map_fd, &key, &next); +bpf_map_delete_elem (map_fd, &key); + } +} + +static void test_one_and_wrap (void ) +{ +struct xdp_lru_window_state st; +struct xdp_lru_window_key key; +int i, n; + +reset_map (); +if (inject (1 ,DST_PORT )) +return ; +fill_key (&key,DST_PORT ); +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &st),"lookup" )) +return ; +ASSERT_EQ (st.seq,1 ,"seq" ); +ASSERT_EQ (st.pkt_len[0 ],sizeof (struct ipv4_packet ),"len0" ); + + n =AGGREGATION_WINDOW +5 ; +if (inject (n -1 ,DST_PORT )) +return ; +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &st),"lookup wrap" )) +return ; +ASSERT_EQ (st.seq, n,"seq wrap" ); +for (i =0 ; i <AGGREGATION_WINDOW ; i++) +ASSERT_EQ (st.pkt_len[i],sizeof (struct ipv4_packet ),"slot" ); +} + +static void test_trunc (void ) +{ +struct xdp_lru_window_state before, after; +struct xdp_lru_window_key key, next; +unsigned char short_pkt[sizeof (struct ethhdr )] = {}; +int err, retval; + +reset_map (); + err =run_pkt (short_pkt,sizeof (short_pkt), &retval); +if (err) +return ; +ASSERT_EQ (retval,XDP_PASS ,"trunc retval" ); + err =bpf_map_get_next_key (map_fd,NULL , &next); +ASSERT_EQ (err, -ENOENT ,"trunc no insert" ); + +if (inject (1 ,DST_PORT )) +return ; +fill_key (&key,DST_PORT ); +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &before),"setup" )) +return ; +if (run_pkt (short_pkt,sizeof (short_pkt), &retval)) +return ; +ASSERT_EQ (retval,XDP_PASS ,"trunc2 retval" ); +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &after),"after" )) +return ; +ASSERT_EQ (after.seq, before.seq,"trunc no mutate" ); +} + +static void test_isolate (void ) +{ +struct xdp_lru_window_state a, b; +struct xdp_lru_window_key key; + +reset_map (); +if (inject (2 ,DST_PORT ) ||inject (1 ,ALT_DST_PORT )) +return ; +fill_key (&key,DST_PORT ); +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &a),"flow a" )) +return ; +fill_key (&key,ALT_DST_PORT ); +if (!ASSERT_OK (bpf_map_lookup_elem (map_fd, &key, &b),"flow b" )) +return ; +ASSERT_EQ (a.seq,2 ,"seq a" ); +ASSERT_EQ (b.seq,1 ,"seq b" ); +} + +void test_xdp_lru_window (void ) +{ + skel =xdp_lru_window__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.xdp_lru_window); + map_fd =bpf_map__fd (skel->maps.flow_table); +if (!ASSERT_GE (prog_fd,0 ,"prog_fd" ) || + !ASSERT_GE (map_fd,0 ,"map_fd" )) + goto out; + +if (test__start_subtest ("wrap" )) +test_one_and_wrap (); +if (test__start_subtest ("trunc" )) +test_trunc (); +if (test__start_subtest ("isolate" )) +test_isolate (); +out: +xdp_lru_window__destroy (skel); +}
tools/testing/selftests/bpf/progs/xdp_lru_window.c new file +81 -0
81 lines not on this page · raw message
tools/testing/selftests/bpf/xdp_lru_window.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __XDP_LRU_WINDOW_H +#define __XDP_LRU_WINDOW_H + +/* +* ABI for the XDP LRU rolling-window selftest. Existing test_lru_map +* coverage never enters XDP; XDP parse tests do not store a modulo +* index into an LRU map value. +*/ + +#define AGGREGATION_WINDOW 50 +#define XDP_LRU_WINDOW_FLOWS 64 + +struct xdp_lru_window_key { +__be32 saddr; +__be32 daddr; +__be16 sport; +__be16 dport; +__u8 proto; +__u8 pad[3 ]; +}; + +struct xdp_lru_window_state { +__u32 seq; +__u32 pkt_len[AGGREGATION_WINDOW ]; +}; + +#endif /* __XDP_LRU_WINDOW_H */
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] bpf: unnecessary FD validation for skeleton-generated fields --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: unnecessary FD validation for skeleton-generated fields
--
commit 382196b03e5bc2ae66b47677cc5a9357a9560542
Author: Anil Kaushik <[email protected]>
selftests/bpf: Test XDP LRU hash rolling-window map updates
This commit adds a small XDP program that writes packet lengths into BPF_MAP_TYPE_LRU_HASH at seq % AGGREGATION_WINDOW. It also adds a test_progs case using bpf_prog_test_run to check wrap, parse-fail isolation, and 5-tuple isolation.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/xdp_lru_window.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/xdp_lru_window.c line 161
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/xdp_lru_window.c new file +11
-0+void test_xdp_lru_window (void ) +{ + skel =xdp_lru_window__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.xdp_lru_window); + map_fd =bpf_map__fd (skel->maps.flow_table); +if (!ASSERT_GE (prog_fd,0 ,"prog_fd" ) || + !ASSERT_GE (map_fd,0 ,"map_fd" )) + goto out;
[Severity: Low]
Are these ASSERT_GE() checks necessary in test_xdp_lru_window()?
Since the skeleton is successfully initialized with xdp_lru_window__open_and_load(), the BPF selftest skeleton framework guarantees that all generated map and program fields contain valid file descriptors.
Validating prog_fd and map_fd manually here appears redundant and contrary to the established skeleton API contract. Could we remove this check?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1