Skip to content

Commit 605091c

Browse files
magnus-karlssonborkmann
authored andcommitted
selftests: xsk: Introduce replacing the default packet stream
Introduce the concept of a default packet stream that is the set of packets sent by most tests. Then add the ability to replace it for a test that would like to send or receive something else through the use of the function pkt_stream_replace() and then restored with pkt_stream_restore_default(). These are then used to convert the STAT_TEST_TX_INVALID to use these new APIs. Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Link: https://lore.kernel.org/bpf/20210907071928.9750-17-magnus.karlsson@gmail.com
1 parent 8abf6f7 commit 605091c

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

tools/testing/selftests/bpf/xdpxceiver.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
390390
ifobj->umem = &ifobj->umem_arr[0];
391391
ifobj->xsk = &ifobj->xsk_arr[0];
392392
ifobj->use_poll = false;
393+
ifobj->pkt_stream = test->pkt_stream_default;
393394

394395
if (i == 0) {
395396
ifobj->rx_on = false;
@@ -418,9 +419,12 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
418419
static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
419420
struct ifobject *ifobj_rx, enum test_mode mode)
420421
{
422+
struct pkt_stream *pkt_stream;
421423
u32 i;
422424

425+
pkt_stream = test->pkt_stream_default;
423426
memset(test, 0, sizeof(*test));
427+
test->pkt_stream_default = pkt_stream;
424428

425429
for (i = 0; i < MAX_INTERFACES; i++) {
426430
struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
@@ -455,6 +459,19 @@ static struct pkt *pkt_stream_get_pkt(struct pkt_stream *pkt_stream, u32 pkt_nb)
455459
return &pkt_stream->pkts[pkt_nb];
456460
}
457461

462+
static void pkt_stream_delete(struct pkt_stream *pkt_stream)
463+
{
464+
free(pkt_stream->pkts);
465+
free(pkt_stream);
466+
}
467+
468+
static void pkt_stream_restore_default(struct test_spec *test)
469+
{
470+
pkt_stream_delete(test->ifobj_tx->pkt_stream);
471+
test->ifobj_tx->pkt_stream = test->pkt_stream_default;
472+
test->ifobj_rx->pkt_stream = test->pkt_stream_default;
473+
}
474+
458475
static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb_pkts, u32 pkt_len)
459476
{
460477
struct pkt_stream *pkt_stream;
@@ -483,6 +500,15 @@ static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb
483500
return pkt_stream;
484501
}
485502

503+
static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
504+
{
505+
struct pkt_stream *pkt_stream;
506+
507+
pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, nb_pkts, pkt_len);
508+
test->ifobj_tx->pkt_stream = pkt_stream;
509+
test->ifobj_rx->pkt_stream = pkt_stream;
510+
}
511+
486512
static struct pkt *pkt_generate(struct ifobject *ifobject, u32 pkt_nb)
487513
{
488514
struct pkt *pkt = pkt_stream_get_pkt(ifobject->pkt_stream, pkt_nb);
@@ -557,7 +583,7 @@ static bool is_pkt_valid(struct pkt *pkt, void *buffer, const struct xdp_desc *d
557583
if (iphdr->version == IP_PKT_VER && iphdr->tos == IP_PKT_TOS) {
558584
u32 seqnum = ntohl(*((u32 *)(data + PKT_HDR_SIZE)));
559585

560-
if (opt_pkt_dump && test_type != TEST_TYPE_STATS)
586+
if (opt_pkt_dump)
561587
pkt_dump(data, PKT_SIZE);
562588

563589
if (pkt->len != desc->len) {
@@ -598,9 +624,6 @@ static void complete_pkts(struct xsk_socket_info *xsk, int batch_size)
598624
unsigned int rcvd;
599625
u32 idx;
600626

601-
if (!xsk->outstanding_tx)
602-
return;
603-
604627
if (xsk_ring_prod__needs_wakeup(&xsk->tx))
605628
kick_tx(xsk);
606629

@@ -831,6 +854,7 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
831854

832855
static void testapp_cleanup_xsk_res(struct ifobject *ifobj)
833856
{
857+
print_verbose("Destroying socket\n");
834858
xsk_socket__delete(ifobj->xsk->xsk);
835859
xsk_umem__delete(ifobj->umem->umem);
836860
}
@@ -878,9 +902,6 @@ static void *worker_testapp_validate_rx(void *arg)
878902
else
879903
receive_pkts(ifobject->pkt_stream, ifobject->xsk, &fds);
880904

881-
if (test_type == TEST_TYPE_TEARDOWN)
882-
print_verbose("Destroying socket\n");
883-
884905
if (test->total_steps == test->current_step)
885906
testapp_cleanup_xsk_res(ifobject);
886907
pthread_exit(NULL);
@@ -890,19 +911,11 @@ static void testapp_validate_traffic(struct test_spec *test)
890911
{
891912
struct ifobject *ifobj_tx = test->ifobj_tx;
892913
struct ifobject *ifobj_rx = test->ifobj_rx;
893-
struct pkt_stream *pkt_stream;
894914
pthread_t t0, t1;
895915

896916
if (pthread_barrier_init(&barr, NULL, 2))
897917
exit_with_error(errno);
898918

899-
if (stat_test_type == STAT_TEST_TX_INVALID)
900-
pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, DEFAULT_PKT_CNT,
901-
XSK_UMEM__INVALID_FRAME_SIZE);
902-
else
903-
pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, DEFAULT_PKT_CNT, PKT_SIZE);
904-
ifobj_tx->pkt_stream = pkt_stream;
905-
ifobj_rx->pkt_stream = pkt_stream;
906919
test->current_step++;
907920

908921
/*Spawn RX thread */
@@ -982,7 +995,9 @@ static void testapp_bpf_res(struct test_spec *test)
982995

983996
static void testapp_stats(struct test_spec *test)
984997
{
985-
for (int i = 0; i < STAT_TEST_TYPE_MAX; i++) {
998+
int i;
999+
1000+
for (i = 0; i < STAT_TEST_TYPE_MAX; i++) {
9861001
test_spec_reset(test);
9871002
stat_test_type = i;
9881003

@@ -991,21 +1006,27 @@ static void testapp_stats(struct test_spec *test)
9911006
test_spec_set_name(test, "STAT_RX_DROPPED");
9921007
test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size -
9931008
XDP_PACKET_HEADROOM - 1;
1009+
testapp_validate_traffic(test);
9941010
break;
9951011
case STAT_TEST_RX_FULL:
9961012
test_spec_set_name(test, "STAT_RX_FULL");
9971013
test->ifobj_rx->xsk->rxqsize = RX_FULL_RXQSIZE;
1014+
testapp_validate_traffic(test);
9981015
break;
9991016
case STAT_TEST_TX_INVALID:
10001017
test_spec_set_name(test, "STAT_TX_INVALID");
1001-
continue;
1018+
pkt_stream_replace(test, DEFAULT_PKT_CNT, XSK_UMEM__INVALID_FRAME_SIZE);
1019+
testapp_validate_traffic(test);
1020+
1021+
pkt_stream_restore_default(test);
1022+
break;
10021023
case STAT_TEST_RX_FILL_EMPTY:
10031024
test_spec_set_name(test, "STAT_RX_FILL_EMPTY");
1025+
testapp_validate_traffic(test);
10041026
break;
10051027
default:
10061028
break;
10071029
}
1008-
testapp_validate_traffic(test);
10091030
}
10101031

10111032
/* To only see the whole stat set being completed unless an individual test fails. */
@@ -1105,6 +1126,7 @@ static void ifobject_delete(struct ifobject *ifobj)
11051126
int main(int argc, char **argv)
11061127
{
11071128
struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
1129+
struct pkt_stream *pkt_stream_default;
11081130
struct ifobject *ifobj_tx, *ifobj_rx;
11091131
struct test_spec test;
11101132
u32 i, j;
@@ -1133,6 +1155,12 @@ int main(int argc, char **argv)
11331155
init_iface(ifobj_rx, MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1,
11341156
worker_testapp_validate_rx);
11351157

1158+
test_spec_init(&test, ifobj_tx, ifobj_rx, 0);
1159+
pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, PKT_SIZE);
1160+
if (!pkt_stream_default)
1161+
exit_with_error(ENOMEM);
1162+
test.pkt_stream_default = pkt_stream_default;
1163+
11361164
ksft_set_plan(TEST_MODE_MAX * TEST_TYPE_MAX);
11371165

11381166
for (i = 0; i < TEST_MODE_MAX; i++)
@@ -1142,6 +1170,7 @@ int main(int argc, char **argv)
11421170
usleep(USLEEP_MAX);
11431171
}
11441172

1173+
pkt_stream_delete(pkt_stream_default);
11451174
ifobject_delete(ifobj_tx);
11461175
ifobject_delete(ifobj_rx);
11471176

tools/testing/selftests/bpf/xdpxceiver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct ifobject {
132132
struct test_spec {
133133
struct ifobject *ifobj_tx;
134134
struct ifobject *ifobj_rx;
135+
struct pkt_stream *pkt_stream_default;
135136
u16 total_steps;
136137
u16 current_step;
137138
u16 nb_sockets;

0 commit comments

Comments
 (0)