summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/test_xsk.sh
blob: 88a7483eaae4001ce9995a572f58b85aaf8f0eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright(c) 2020 Intel Corporation, Weqaar Janjua <weqaar.a.janjua@intel.com>

# AF_XDP selftests based on veth
#
# End-to-end AF_XDP over Veth test
#
# Topology:
# ---------
#                 -----------
#               _ | Process | _
#              /  -----------  \
#             /        |        \
#            /         |         \
#      -----------     |     -----------
#      | Thread1 |     |     | Thread2 |
#      -----------     |     -----------
#           |          |          |
#      -----------     |     -----------
#      |  xskX   |     |     |  xskY   |
#      -----------     |     -----------
#           |          |          |
#      -----------     |     ----------
#      |  vethX  | --------- |  vethY |
#      -----------   peer    ----------
#           |          |          |
#      namespaceX      |     namespaceY
#
# AF_XDP is an address family optimized for high performance packet processing,
# it is XDP’s user-space interface.
#
# An AF_XDP socket is linked to a single UMEM which is a region of virtual
# contiguous memory, divided into equal-sized frames.
#
# Refer to AF_XDP Kernel Documentation for detailed information:
# https://www.kernel.org/doc/html/latest/networking/af_xdp.html
#
# Prerequisites setup by script:
#
#   Set up veth interfaces as per the topology shown ^^:
#   * setup two veth interfaces and one namespace
#   ** veth<xxxx> in root namespace
#   ** veth<yyyy> in af_xdp<xxxx> namespace
#   ** namespace af_xdp<xxxx>
#   * create a spec file veth.spec that includes this run-time configuration
#   *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid
#       conflict with any existing interface
#   * tests the veth and xsk layers of the topology
#
# See the source xdpxceiver.c for information on each test
#
# Kernel configuration:
# ---------------------
# See "config" file for recommended kernel config options.
#
# Turn on XDP sockets and veth support when compiling i.e.
# 	Networking support -->
# 		Networking options -->
# 			[ * ] XDP sockets
#
# Executing Tests:
# ----------------
# Must run with CAP_NET_ADMIN capability.
#
# Run (full color-coded output):
#   sudo ./test_xsk.sh -c
#
# If running from kselftests:
#   sudo make colorconsole=1 run_tests
#
# Run (full output without color-coding):
#   sudo ./test_xsk.sh

. xsk_prereqs.sh

while getopts c flag
do
	case "${flag}" in
		c) colorconsole=1;;
	esac
done

TEST_NAME="PREREQUISITES"

URANDOM=/dev/urandom
[ ! -e "${URANDOM}" ] && { echo "${URANDOM} not found. Skipping tests."; test_exit 1 1; }

VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
VETH0=ve${VETH0_POSTFIX}
VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4)
VETH1=ve${VETH1_POSTFIX}
NS0=root
NS1=af_xdp${VETH1_POSTFIX}
MTU=1500

setup_vethPairs() {
	echo "setting up ${VETH0}: namespace: ${NS0}"
	ip netns add ${NS1}
	ip link add ${VETH0} type veth peer name ${VETH1}
	if [ -f /proc/net/if_inet6 ]; then
		echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6
	fi
	echo "setting up ${VETH1}: namespace: ${NS1}"
	ip link set ${VETH1} netns ${NS1}
	ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU}
	ip link set ${VETH0} mtu ${MTU}
	ip netns exec ${NS1} ip link set ${VETH1} up
	ip link set ${VETH0} up
}

validate_root_exec
validate_veth_support ${VETH0}
validate_ip_utility
setup_vethPairs

retval=$?
if [ $retval -ne 0 ]; then
	test_status $retval "${TEST_NAME}"
	cleanup_exit ${VETH0} ${VETH1} ${NS1}
	exit $retval
fi

echo "${VETH0}:${VETH1},${NS1}" > ${SPECFILE}

validate_veth_spec_file

echo "Spec file created: ${SPECFILE}"

test_status $retval "${TEST_NAME}"

## START TESTS

statusList=()

### TEST 1
TEST_NAME="XSK KSELFTEST FRAMEWORK"

echo "Switching interfaces [${VETH0}, ${VETH1}] to XDP Generic mode"
vethXDPgeneric ${VETH0} ${VETH1} ${NS1}

retval=$?
if [ $retval -eq 0 ]; then
	echo "Switching interfaces [${VETH0}, ${VETH1}] to XDP Native mode"
	vethXDPnative ${VETH0} ${VETH1} ${NS1}
fi

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 2
TEST_NAME="SKB NOPOLL"

vethXDPgeneric ${VETH0} ${VETH1} ${NS1}

params=("-S")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 3
TEST_NAME="SKB POLL"

vethXDPgeneric ${VETH0} ${VETH1} ${NS1}

params=("-S" "-p")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 4
TEST_NAME="DRV NOPOLL"

vethXDPnative ${VETH0} ${VETH1} ${NS1}

params=("-N")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 5
TEST_NAME="DRV POLL"

vethXDPnative ${VETH0} ${VETH1} ${NS1}

params=("-N" "-p")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 6
TEST_NAME="SKB SOCKET TEARDOWN"

vethXDPgeneric ${VETH0} ${VETH1} ${NS1}

params=("-S" "-T")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 7
TEST_NAME="DRV SOCKET TEARDOWN"

vethXDPnative ${VETH0} ${VETH1} ${NS1}

params=("-N" "-T")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 8
TEST_NAME="SKB BIDIRECTIONAL SOCKETS"

vethXDPgeneric ${VETH0} ${VETH1} ${NS1}

params=("-S" "-B")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

### TEST 9
TEST_NAME="DRV BIDIRECTIONAL SOCKETS"

vethXDPnative ${VETH0} ${VETH1} ${NS1}

params=("-N" "-B")
execxdpxceiver params

retval=$?
test_status $retval "${TEST_NAME}"
statusList+=($retval)

## END TESTS

cleanup_exit ${VETH0} ${VETH1} ${NS1}

for _status in "${statusList[@]}"
do
	if [ $_status -ne 0 ]; then
		test_exit $ksft_fail 0
	fi
done

test_exit $ksft_pass 0