Skip to content

Commit 6d198a8

Browse files
tvyavahaborkmann
authored andcommitted
selftests/xsk: Add a test for shared umem feature
Add a new test for testing shared umem feature. This is accomplished by adding a new XDP program and using the multiple sockets. The new XDP program redirects the packets based on the destination MAC address. Signed-off-by: Tushar Vyavahare <tushar.vyavahare@intel.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Magnus Karlsson <magnus.karlsson@intel.com> Link: https://lore.kernel.org/bpf/20230927135241.2287547-9-tushar.vyavahare@intel.com
1 parent fc2cb86 commit 6d198a8

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
6+
#include <linux/if_ether.h>
67
#include "xsk_xdp_common.h"
78

89
struct {
910
__uint(type, BPF_MAP_TYPE_XSKMAP);
10-
__uint(max_entries, 1);
11+
__uint(max_entries, 2);
1112
__uint(key_size, sizeof(int));
1213
__uint(value_size, sizeof(int));
1314
} xsk SEC(".maps");
@@ -52,4 +53,21 @@ SEC("xdp.frags") int xsk_xdp_populate_metadata(struct xdp_md *xdp)
5253
return bpf_redirect_map(&xsk, 0, XDP_DROP);
5354
}
5455

56+
SEC("xdp") int xsk_xdp_shared_umem(struct xdp_md *xdp)
57+
{
58+
void *data = (void *)(long)xdp->data;
59+
void *data_end = (void *)(long)xdp->data_end;
60+
struct ethhdr *eth = data;
61+
62+
if (eth + 1 > data_end)
63+
return XDP_DROP;
64+
65+
/* Redirecting packets based on the destination MAC address */
66+
idx = ((unsigned int)(eth->h_dest[5])) / 2;
67+
if (idx > MAX_SOCKETS)
68+
return XDP_DROP;
69+
70+
return bpf_redirect_map(&xsk, idx, XDP_DROP);
71+
}
72+
5573
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/xsk_xdp_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef XSK_XDP_COMMON_H_
44
#define XSK_XDP_COMMON_H_
55

6+
#define MAX_SOCKETS 2
7+
68
struct xdp_info {
79
__u64 count;
810
} __attribute__((aligned(32)));

tools/testing/selftests/bpf/xskxceiver.c

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ static u32 pkt_get_buffer_len(struct xsk_umem_info *umem, u32 len)
651651
return ceil_u32(len, umem->frame_size) * umem->frame_size;
652652
}
653653

654-
static struct pkt_stream *pkt_stream_generate(u32 nb_pkts, u32 pkt_len)
654+
static struct pkt_stream *__pkt_stream_generate(u32 nb_pkts, u32 pkt_len, u32 nb_start, u32 nb_off)
655655
{
656656
struct pkt_stream *pkt_stream;
657657
u32 i;
@@ -666,12 +666,17 @@ static struct pkt_stream *pkt_stream_generate(u32 nb_pkts, u32 pkt_len)
666666
struct pkt *pkt = &pkt_stream->pkts[i];
667667

668668
pkt_set(pkt_stream, pkt, 0, pkt_len);
669-
pkt->pkt_nb = i;
669+
pkt->pkt_nb = nb_start + i * nb_off;
670670
}
671671

672672
return pkt_stream;
673673
}
674674

675+
static struct pkt_stream *pkt_stream_generate(u32 nb_pkts, u32 pkt_len)
676+
{
677+
return __pkt_stream_generate(nb_pkts, pkt_len, 0, 1);
678+
}
679+
675680
static struct pkt_stream *pkt_stream_clone(struct pkt_stream *pkt_stream)
676681
{
677682
return pkt_stream_generate(pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
@@ -721,6 +726,24 @@ static void pkt_stream_receive_half(struct test_spec *test)
721726
pkt_stream->nb_valid_entries /= 2;
722727
}
723728

729+
static void pkt_stream_even_odd_sequence(struct test_spec *test)
730+
{
731+
struct pkt_stream *pkt_stream;
732+
u32 i;
733+
734+
for (i = 0; i < test->nb_sockets; i++) {
735+
pkt_stream = test->ifobj_tx->xsk_arr[i].pkt_stream;
736+
pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2,
737+
pkt_stream->pkts[0].len, i, 2);
738+
test->ifobj_tx->xsk_arr[i].pkt_stream = pkt_stream;
739+
740+
pkt_stream = test->ifobj_rx->xsk_arr[i].pkt_stream;
741+
pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2,
742+
pkt_stream->pkts[0].len, i, 2);
743+
test->ifobj_rx->xsk_arr[i].pkt_stream = pkt_stream;
744+
}
745+
}
746+
724747
static u64 pkt_get_addr(struct pkt *pkt, struct xsk_umem_info *umem)
725748
{
726749
if (!pkt->valid)
@@ -1584,6 +1607,7 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
15841607
LIBBPF_OPTS(bpf_xdp_query_opts, opts);
15851608
void *bufs;
15861609
int ret;
1610+
u32 i;
15871611

15881612
if (ifobject->umem->unaligned_mode)
15891613
mmap_flags |= MAP_HUGETLB | MAP_HUGE_2MB;
@@ -1608,9 +1632,12 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
16081632

16091633
xsk_populate_fill_ring(ifobject->umem, ifobject->xsk->pkt_stream, ifobject->use_fill_ring);
16101634

1611-
ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, 0);
1612-
if (ret)
1613-
exit_with_error(errno);
1635+
for (i = 0; i < test->nb_sockets; i++) {
1636+
ifobject->xsk = &ifobject->xsk_arr[i];
1637+
ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, i);
1638+
if (ret)
1639+
exit_with_error(errno);
1640+
}
16141641
}
16151642

16161643
static void *worker_testapp_validate_tx(void *arg)
@@ -2111,6 +2138,23 @@ static int testapp_xdp_metadata_copy(struct test_spec *test)
21112138
return testapp_validate_traffic(test);
21122139
}
21132140

2141+
static int testapp_xdp_shared_umem(struct test_spec *test)
2142+
{
2143+
struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
2144+
struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
2145+
2146+
test->total_steps = 1;
2147+
test->nb_sockets = 2;
2148+
2149+
test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_shared_umem,
2150+
skel_tx->progs.xsk_xdp_shared_umem,
2151+
skel_rx->maps.xsk, skel_tx->maps.xsk);
2152+
2153+
pkt_stream_even_odd_sequence(test);
2154+
2155+
return testapp_validate_traffic(test);
2156+
}
2157+
21142158
static int testapp_poll_txq_tmout(struct test_spec *test)
21152159
{
21162160
test->ifobj_tx->use_poll = true;
@@ -2412,6 +2456,7 @@ static const struct test_spec tests[] = {
24122456
{.name = "STAT_FILL_EMPTY", .test_func = testapp_stats_fill_empty},
24132457
{.name = "XDP_PROG_CLEANUP", .test_func = testapp_xdp_prog_cleanup},
24142458
{.name = "XDP_DROP_HALF", .test_func = testapp_xdp_drop},
2459+
{.name = "XDP_SHARED_UMEM", .test_func = testapp_xdp_shared_umem},
24152460
{.name = "XDP_METADATA_COPY", .test_func = testapp_xdp_metadata},
24162461
{.name = "XDP_METADATA_COPY_MULTI_BUFF", .test_func = testapp_xdp_metadata_mb},
24172462
{.name = "SEND_RECEIVE_9K_PACKETS", .test_func = testapp_send_receive_mb},

tools/testing/selftests/bpf/xskxceiver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <limits.h>
99

1010
#include "xsk_xdp_progs.skel.h"
11+
#include "xsk_xdp_common.h"
1112

1213
#ifndef SOL_XDP
1314
#define SOL_XDP 283
@@ -35,7 +36,6 @@
3536
#define TEST_SKIP 2
3637
#define MAX_INTERFACES 2
3738
#define MAX_INTERFACE_NAME_CHARS 16
38-
#define MAX_SOCKETS 2
3939
#define MAX_TEST_NAME_SIZE 48
4040
#define MAX_TEARDOWN_ITER 10
4141
#define PKT_HDR_SIZE (sizeof(struct ethhdr) + 2) /* Just to align the data in the packet */

0 commit comments

Comments
 (0)