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