1 // SPDX-License-Identifier: GPL-2.0
2 #include <net/if.h>
3 #include <stdarg.h>
4
5 #include "network_helpers.h"
6 #include "test_progs.h"
7 #include "test_xsk.h"
8 #include "xsk_xdp_progs.skel.h"
9
10 #define VETH_RX "veth0"
11 #define VETH_TX "veth1"
12 #define MTU 1500
13
setup_veth(bool busy_poll)14 int setup_veth(bool busy_poll)
15 {
16 SYS(fail,
17 "ip link add %s numtxqueues 4 numrxqueues 4 type veth peer name %s numtxqueues 4 numrxqueues 4",
18 VETH_RX, VETH_TX);
19 SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_RX);
20 SYS(fail, "sysctl -wq net.ipv6.conf.%s.disable_ipv6=1", VETH_TX);
21
22 if (busy_poll) {
23 SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_RX);
24 SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_RX);
25 SYS(fail, "echo 2 > /sys/class/net/%s/napi_defer_hard_irqs", VETH_TX);
26 SYS(fail, "echo 200000 > /sys/class/net/%s/gro_flush_timeout", VETH_TX);
27 }
28
29 SYS(fail, "ip link set %s mtu %d", VETH_RX, MTU);
30 SYS(fail, "ip link set %s mtu %d", VETH_TX, MTU);
31 SYS(fail, "ip link set %s up", VETH_RX);
32 SYS(fail, "ip link set %s up", VETH_TX);
33
34 return 0;
35
36 fail:
37 return -1;
38 }
39
delete_veth(void)40 void delete_veth(void)
41 {
42 SYS_NOFAIL("ip link del %s", VETH_RX);
43 SYS_NOFAIL("ip link del %s", VETH_TX);
44 }
45
configure_ifobj(struct ifobject * tx,struct ifobject * rx)46 int configure_ifobj(struct ifobject *tx, struct ifobject *rx)
47 {
48 rx->ifindex = if_nametoindex(VETH_RX);
49 if (!ASSERT_OK_FD(rx->ifindex, "get RX ifindex"))
50 return -1;
51
52 tx->ifindex = if_nametoindex(VETH_TX);
53 if (!ASSERT_OK_FD(tx->ifindex, "get TX ifindex"))
54 return -1;
55
56 tx->shared_umem = false;
57 rx->shared_umem = false;
58
59
60 return 0;
61 }
62
test_xsk(const struct test_spec * test_to_run,enum test_mode mode)63 static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode)
64 {
65 u32 max_frags, umem_tailroom, cache_line_size;
66 struct ifobject *ifobj_tx, *ifobj_rx;
67 struct test_spec test;
68 int ret;
69
70 ifobj_tx = ifobject_create();
71 if (!ASSERT_OK_PTR(ifobj_tx, "create ifobj_tx"))
72 return;
73
74 ifobj_rx = ifobject_create();
75 if (!ASSERT_OK_PTR(ifobj_rx, "create ifobj_rx"))
76 goto delete_tx;
77
78 if (!ASSERT_OK(configure_ifobj(ifobj_tx, ifobj_rx), "conigure ifobj"))
79 goto delete_rx;
80
81 ret = get_hw_ring_size(ifobj_tx->ifname, &ifobj_tx->ring);
82 if (!ret) {
83 ifobj_tx->hw_ring_size_supp = true;
84 ifobj_tx->set_ring.default_tx = ifobj_tx->ring.tx_pending;
85 ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending;
86 }
87
88 cache_line_size = read_procfs_val(SMP_CACHE_BYTES_PATH);
89 if (!cache_line_size)
90 cache_line_size = 64;
91
92 max_frags = read_procfs_val(MAX_SKB_FRAGS_PATH);
93 if (!max_frags)
94 max_frags = 17;
95
96 ifobj_tx->max_skb_frags = max_frags;
97 ifobj_rx->max_skb_frags = max_frags;
98
99 /* 48 bytes is a part of skb_shared_info w/o frags array;
100 * 16 bytes is sizeof(skb_frag_t)
101 */
102 umem_tailroom = ALIGN(48 + (max_frags * 16), cache_line_size);
103 ifobj_tx->umem_tailroom = umem_tailroom;
104 ifobj_rx->umem_tailroom = umem_tailroom;
105
106 if (!ASSERT_OK(init_iface(ifobj_rx, worker_testapp_validate_rx), "init RX"))
107 goto delete_rx;
108 if (!ASSERT_OK(init_iface(ifobj_tx, worker_testapp_validate_tx), "init TX"))
109 goto delete_rx;
110
111 test_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]);
112
113 test.tx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
114 if (!ASSERT_OK_PTR(test.tx_pkt_stream_default, "TX pkt generation"))
115 goto delete_rx;
116 test.rx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
117 if (!ASSERT_OK_PTR(test.rx_pkt_stream_default, "RX pkt generation"))
118 goto delete_rx;
119
120
121 test_init(&test, ifobj_tx, ifobj_rx, mode, test_to_run);
122 ret = test.test_func(&test);
123 if (ret != TEST_SKIP)
124 ASSERT_OK(ret, "Run test");
125 pkt_stream_restore_default(&test);
126
127 if (ifobj_tx->hw_ring_size_supp)
128 hw_ring_size_reset(ifobj_tx);
129
130 pkt_stream_delete(test.tx_pkt_stream_default);
131 pkt_stream_delete(test.rx_pkt_stream_default);
132 xsk_xdp_progs__destroy(ifobj_tx->xdp_progs);
133 xsk_xdp_progs__destroy(ifobj_rx->xdp_progs);
134
135 delete_rx:
136 ifobject_delete(ifobj_rx);
137 delete_tx:
138 ifobject_delete(ifobj_tx);
139 }
140
test_ns_xsk_skb(void)141 void test_ns_xsk_skb(void)
142 {
143 int i;
144
145 if (!ASSERT_OK(setup_veth(false), "setup veth"))
146 return;
147
148 for (i = 0; i < ARRAY_SIZE(tests); i++) {
149 if (test__start_subtest(tests[i].name))
150 test_xsk(&tests[i], TEST_MODE_SKB);
151 }
152
153 delete_veth();
154 }
155
test_ns_xsk_drv(void)156 void test_ns_xsk_drv(void)
157 {
158 int i;
159
160 if (!ASSERT_OK(setup_veth(false), "setup veth"))
161 return;
162
163 for (i = 0; i < ARRAY_SIZE(tests); i++) {
164 if (test__start_subtest(tests[i].name))
165 test_xsk(&tests[i], TEST_MODE_DRV);
166 }
167
168 delete_veth();
169 }
170
171