Skip to content

Commit ab734b4

Browse files
author
Martin KaFai Lau
committed
Merge branch 'selftests-xsk-add-tests-for-xdp-tail-adjustment-in-af_xdp'
Tushar Vyavahare says: ==================== selftests/xsk: Add tests for XDP tail adjustment in AF_XDP This patch series adds tests to validate the XDP tail adjustment functionality, focusing on its use within the AF_XDP context. The tests verify dynamic packet size manipulation using the bpf_xdp_adjust_tail() helper function, covering both single and multi-buffer scenarios. v1 -> v2: 1. Retain and extend stream replacement: Keep `pkt_stream_replace` unchanged. Add `pkt_stream_replace_ifobject` for targeted ifobject handling. 2. Consolidate patches: Merge patches 2 to 6 for tail adjustment tests and check. v2 -> v3: 1. Introduce `adjust_value` to replace `count` for clearer communication with userspace. v3 -> v4: 1. Remove `testapp_adjust_tail_common()`. [Maciej] 2. Add comments and modify code for buffer resizing logic in test cases (shrink/grow by specific byte sizes for testing purposes). [Maciej] ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents 0f681b0 + 4b30209 commit ab734b4

File tree

4 files changed

+163
-8
lines changed

4 files changed

+163
-8
lines changed

tools/testing/selftests/bpf/progs/xsk_xdp_progs.c

+50
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
66
#include <linux/if_ether.h>
7+
#include <linux/ip.h>
8+
#include <linux/errno.h>
79
#include "xsk_xdp_common.h"
810

911
struct {
@@ -14,6 +16,7 @@ struct {
1416
} xsk SEC(".maps");
1517

1618
static unsigned int idx;
19+
int adjust_value = 0;
1720
int count = 0;
1821

1922
SEC("xdp.frags") int xsk_def_prog(struct xdp_md *xdp)
@@ -70,4 +73,51 @@ SEC("xdp") int xsk_xdp_shared_umem(struct xdp_md *xdp)
7073
return bpf_redirect_map(&xsk, idx, XDP_DROP);
7174
}
7275

76+
SEC("xdp.frags") int xsk_xdp_adjust_tail(struct xdp_md *xdp)
77+
{
78+
__u32 buff_len, curr_buff_len;
79+
int ret;
80+
81+
buff_len = bpf_xdp_get_buff_len(xdp);
82+
if (buff_len == 0)
83+
return XDP_DROP;
84+
85+
ret = bpf_xdp_adjust_tail(xdp, adjust_value);
86+
if (ret < 0) {
87+
/* Handle unsupported cases */
88+
if (ret == -EOPNOTSUPP) {
89+
/* Set adjust_value to -EOPNOTSUPP to indicate to userspace that this case
90+
* is unsupported
91+
*/
92+
adjust_value = -EOPNOTSUPP;
93+
return bpf_redirect_map(&xsk, 0, XDP_DROP);
94+
}
95+
96+
return XDP_DROP;
97+
}
98+
99+
curr_buff_len = bpf_xdp_get_buff_len(xdp);
100+
if (curr_buff_len != buff_len + adjust_value)
101+
return XDP_DROP;
102+
103+
if (curr_buff_len > buff_len) {
104+
__u32 *pkt_data = (void *)(long)xdp->data;
105+
__u32 len, words_to_end, seq_num;
106+
107+
len = curr_buff_len - PKT_HDR_ALIGN;
108+
words_to_end = len / sizeof(*pkt_data) - 1;
109+
seq_num = words_to_end;
110+
111+
/* Convert sequence number to network byte order. Store this in the last 4 bytes of
112+
* the packet. Use 'adjust_value' to determine the position at the end of the
113+
* packet for storing the sequence number.
114+
*/
115+
seq_num = __constant_htonl(words_to_end);
116+
bpf_xdp_store_bytes(xdp, curr_buff_len - sizeof(seq_num), &seq_num,
117+
sizeof(seq_num));
118+
}
119+
120+
return bpf_redirect_map(&xsk, 0, XDP_DROP);
121+
}
122+
73123
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/xsk_xdp_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define XSK_XDP_COMMON_H_
55

66
#define MAX_SOCKETS 2
7+
#define PKT_HDR_ALIGN (sizeof(struct ethhdr) + 2) /* Just to align the data in the packet */
78

89
struct xdp_info {
910
__u64 count;

tools/testing/selftests/bpf/xskxceiver.c

+110-8
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
524524
test->nb_sockets = 1;
525525
test->fail = false;
526526
test->set_ring = false;
527+
test->adjust_tail = false;
528+
test->adjust_tail_support = false;
527529
test->mtu = MAX_ETH_PKT_SIZE;
528530
test->xdp_prog_rx = ifobj_rx->xdp_progs->progs.xsk_def_prog;
529531
test->xskmap_rx = ifobj_rx->xdp_progs->maps.xsk;
@@ -757,14 +759,15 @@ static struct pkt_stream *pkt_stream_clone(struct pkt_stream *pkt_stream)
757759
return pkt_stream_generate(pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
758760
}
759761

760-
static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
762+
static void pkt_stream_replace_ifobject(struct ifobject *ifobj, u32 nb_pkts, u32 pkt_len)
761763
{
762-
struct pkt_stream *pkt_stream;
764+
ifobj->xsk->pkt_stream = pkt_stream_generate(nb_pkts, pkt_len);
765+
}
763766

764-
pkt_stream = pkt_stream_generate(nb_pkts, pkt_len);
765-
test->ifobj_tx->xsk->pkt_stream = pkt_stream;
766-
pkt_stream = pkt_stream_generate(nb_pkts, pkt_len);
767-
test->ifobj_rx->xsk->pkt_stream = pkt_stream;
767+
static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
768+
{
769+
pkt_stream_replace_ifobject(test->ifobj_tx, nb_pkts, pkt_len);
770+
pkt_stream_replace_ifobject(test->ifobj_rx, nb_pkts, pkt_len);
768771
}
769772

770773
static void __pkt_stream_replace_half(struct ifobject *ifobj, u32 pkt_len,
@@ -991,6 +994,31 @@ static bool is_metadata_correct(struct pkt *pkt, void *buffer, u64 addr)
991994
return true;
992995
}
993996

997+
static bool is_adjust_tail_supported(struct xsk_xdp_progs *skel_rx)
998+
{
999+
struct bpf_map *data_map;
1000+
int adjust_value = 0;
1001+
int key = 0;
1002+
int ret;
1003+
1004+
data_map = bpf_object__find_map_by_name(skel_rx->obj, "xsk_xdp_.bss");
1005+
if (!data_map || !bpf_map__is_internal(data_map)) {
1006+
ksft_print_msg("Error: could not find bss section of XDP program\n");
1007+
exit_with_error(errno);
1008+
}
1009+
1010+
ret = bpf_map_lookup_elem(bpf_map__fd(data_map), &key, &adjust_value);
1011+
if (ret) {
1012+
ksft_print_msg("Error: bpf_map_lookup_elem failed with error %d\n", ret);
1013+
exit_with_error(errno);
1014+
}
1015+
1016+
/* Set the 'adjust_value' variable to -EOPNOTSUPP in the XDP program if the adjust_tail
1017+
* helper is not supported. Skip the adjust_tail test case in this scenario.
1018+
*/
1019+
return adjust_value != -EOPNOTSUPP;
1020+
}
1021+
9941022
static bool is_frag_valid(struct xsk_umem_info *umem, u64 addr, u32 len, u32 expected_pkt_nb,
9951023
u32 bytes_processed)
9961024
{
@@ -1767,8 +1795,13 @@ static void *worker_testapp_validate_rx(void *arg)
17671795

17681796
if (!err && ifobject->validation_func)
17691797
err = ifobject->validation_func(ifobject);
1770-
if (err)
1771-
report_failure(test);
1798+
1799+
if (err) {
1800+
if (test->adjust_tail && !is_adjust_tail_supported(ifobject->xdp_progs))
1801+
test->adjust_tail_support = false;
1802+
else
1803+
report_failure(test);
1804+
}
17721805

17731806
pthread_exit(NULL);
17741807
}
@@ -2515,6 +2548,71 @@ static int testapp_hw_sw_max_ring_size(struct test_spec *test)
25152548
return testapp_validate_traffic(test);
25162549
}
25172550

2551+
static int testapp_xdp_adjust_tail(struct test_spec *test, int adjust_value)
2552+
{
2553+
struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
2554+
struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
2555+
2556+
test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_adjust_tail,
2557+
skel_tx->progs.xsk_xdp_adjust_tail,
2558+
skel_rx->maps.xsk, skel_tx->maps.xsk);
2559+
2560+
skel_rx->bss->adjust_value = adjust_value;
2561+
2562+
return testapp_validate_traffic(test);
2563+
}
2564+
2565+
static int testapp_adjust_tail(struct test_spec *test, u32 value, u32 pkt_len)
2566+
{
2567+
int ret;
2568+
2569+
test->adjust_tail_support = true;
2570+
test->adjust_tail = true;
2571+
test->total_steps = 1;
2572+
2573+
pkt_stream_replace_ifobject(test->ifobj_tx, DEFAULT_BATCH_SIZE, pkt_len);
2574+
pkt_stream_replace_ifobject(test->ifobj_rx, DEFAULT_BATCH_SIZE, pkt_len + value);
2575+
2576+
ret = testapp_xdp_adjust_tail(test, value);
2577+
if (ret)
2578+
return ret;
2579+
2580+
if (!test->adjust_tail_support) {
2581+
ksft_test_result_skip("%s %sResize pkt with bpf_xdp_adjust_tail() not supported\n",
2582+
mode_string(test), busy_poll_string(test));
2583+
return TEST_SKIP;
2584+
}
2585+
2586+
return 0;
2587+
}
2588+
2589+
static int testapp_adjust_tail_shrink(struct test_spec *test)
2590+
{
2591+
/* Shrink by 4 bytes for testing purpose */
2592+
return testapp_adjust_tail(test, -4, MIN_PKT_SIZE * 2);
2593+
}
2594+
2595+
static int testapp_adjust_tail_shrink_mb(struct test_spec *test)
2596+
{
2597+
test->mtu = MAX_ETH_JUMBO_SIZE;
2598+
/* Shrink by the frag size */
2599+
return testapp_adjust_tail(test, -XSK_UMEM__MAX_FRAME_SIZE, XSK_UMEM__LARGE_FRAME_SIZE * 2);
2600+
}
2601+
2602+
static int testapp_adjust_tail_grow(struct test_spec *test)
2603+
{
2604+
/* Grow by 4 bytes for testing purpose */
2605+
return testapp_adjust_tail(test, 4, MIN_PKT_SIZE * 2);
2606+
}
2607+
2608+
static int testapp_adjust_tail_grow_mb(struct test_spec *test)
2609+
{
2610+
test->mtu = MAX_ETH_JUMBO_SIZE;
2611+
/* Grow by (frag_size - last_frag_Size) - 1 to stay inside the last fragment */
2612+
return testapp_adjust_tail(test, (XSK_UMEM__MAX_FRAME_SIZE / 2) - 1,
2613+
XSK_UMEM__LARGE_FRAME_SIZE * 2);
2614+
}
2615+
25182616
static void run_pkt_test(struct test_spec *test)
25192617
{
25202618
int ret;
@@ -2621,6 +2719,10 @@ static const struct test_spec tests[] = {
26212719
{.name = "TOO_MANY_FRAGS", .test_func = testapp_too_many_frags},
26222720
{.name = "HW_SW_MIN_RING_SIZE", .test_func = testapp_hw_sw_min_ring_size},
26232721
{.name = "HW_SW_MAX_RING_SIZE", .test_func = testapp_hw_sw_max_ring_size},
2722+
{.name = "XDP_ADJUST_TAIL_SHRINK", .test_func = testapp_adjust_tail_shrink},
2723+
{.name = "XDP_ADJUST_TAIL_SHRINK_MULTI_BUFF", .test_func = testapp_adjust_tail_shrink_mb},
2724+
{.name = "XDP_ADJUST_TAIL_GROW", .test_func = testapp_adjust_tail_grow},
2725+
{.name = "XDP_ADJUST_TAIL_GROW_MULTI_BUFF", .test_func = testapp_adjust_tail_grow_mb},
26242726
};
26252727

26262728
static void print_tests(void)

tools/testing/selftests/bpf/xskxceiver.h

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ struct test_spec {
173173
u16 nb_sockets;
174174
bool fail;
175175
bool set_ring;
176+
bool adjust_tail;
177+
bool adjust_tail_support;
176178
enum test_mode mode;
177179
char name[MAX_TEST_NAME_SIZE];
178180
};

0 commit comments

Comments
 (0)