fix: testpmd and l3fwd check state
[csit.git] / resources / libraries / bash / function / dpdk.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2022 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function common_dirs () {
20
21     # Set global variables, create some directories (without touching content).
22     # This function assumes running in remote testbed. It might override other
23     # functions if included from common.sh.
24
25     # Variables set:
26     # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
27     # - DPDK_DIR - Path to DPDK framework.
28     # - CSIT_DIR - Path to CSIT framework.
29     # Functions called:
30     # - die - Print to stderr and exit.
31
32     set -exuo pipefail
33
34     this_file=$(readlink -e "${BASH_SOURCE[0]}") || {
35         die "Some error during locating of this source file."
36     }
37     BASH_FUNCTION_DIR=$(dirname "${this_file}") || {
38         die "Some error during dirname call."
39     }
40     CSIT_DIR=$(readlink -e "/tmp/openvpp-testing") || {
41         die "Readlink failed."
42     }
43     mkdir -p "${CSIT_DIR}/dpdk" || die "Mkdir failed."
44     DPDK_DIR=$(readlink -e "${CSIT_DIR}/dpdk") || {
45         die "Readlink failed."
46     }
47 }
48
49
50 function dpdk_bind () {
51
52     # Bind interfaces to driver.
53     #
54     # Variables read:
55     # - DPDK_DIR - Path to DPDK framework.
56     # - @ - Script cli arguments.
57     # Functions called:
58     # - die - Print to stderr and exit.
59
60     set -exuo pipefail
61
62     pushd "${DPDK_DIR}/" || die "Pushd failed"
63
64     sudo ./usertools/dpdk-devbind.py -b "${@}" || {
65         die "Bind ${@} failed"
66     }
67
68     popd || die "Popd failed"
69 }
70
71
72 function dpdk_compile () {
73
74     # Compile DPDK archive.
75     #
76     # Variables read:
77     # - DPDK_DIR - Path to DPDK framework.
78     # - CSIT_DIR - Path to CSIT framework.
79     # Variables exported:
80     # - RTE_SDK - DPDK directory.
81     # - RTE_TARGET - Make targed of DPDK framework.
82     # Functions called:
83     # - die - Print to stderr and exit.
84
85     set -exuo pipefail
86
87     pushd "${DPDK_DIR}" || die "Pushd failed"
88
89     # enable l3fwd
90     meson_options="-Dexamples=l3fwd "
91
92     # i40e specific options
93     meson_options="${meson_options} \
94         -Dc_args=-DRTE_LIBRTE_I40E_16BYTE_RX_DESC=y"
95
96     # Configure generic build - the same used by VPP
97     meson_options="${meson_options} -Dplatform=generic"
98
99     # Patch L3FWD.
100     sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128"
101     sed_rxd+="/#define RTE_TEST_RX_DESC_DEFAULT 1024/g"
102     sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512"
103     sed_txd+="/#define RTE_TEST_TX_DESC_DEFAULT 1024/g"
104     sed_file="./main.c"
105     pushd examples/l3fwd || die "Pushd failed"
106     sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
107     sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
108     popd || die "Popd failed"
109
110     # Compile using Meson and Ninja.
111     meson ${meson_options} build || {
112         die "Failed to compile DPDK!"
113     }
114     ninja -C build || die "Failed to compile DPDK!"
115 }
116
117
118 function dpdk_extract () {
119
120     # Extract DPDK framework.
121     #
122     # Variables read:
123     # - DPDK_DIR - Path to DPDK framework.
124     # - CSIT_DIR - Path to CSIT framework.
125     # Functions called:
126     # - die - Print to stderr and exit.
127
128     set -exuo pipefail
129
130     pushd "${CSIT_DIR}" || die "Pushd failed"
131     tar -xvf download_dir/dpdk*.tar.xz --strip=1 --directory "${DPDK_DIR}" || {
132         die "Failed to extract DPDK!"
133     }
134 }
135
136
137 function dpdk_kill () {
138
139     # Kill testpmd and/or l3fwd if running.
140
141     # Function will be noisy and requires custom error handling.
142     set -x
143     set +e
144
145     # Try to kill the testpmd.
146     sudo pgrep testpmd
147     if [ $? -eq "0" ]; then
148         success=false
149         sudo pkill testpmd
150         for attempt in {1..60}; do
151             echo "Checking if testpmd is still alive, attempt nr ${attempt}"
152             sudo pgrep testpmd
153             if [ $? -eq "1" ]; then
154                 success=true
155                 break
156             fi
157             echo "testpmd is still alive, waiting 1 second"
158             sleep 1
159         done
160         if [ "$success" = false ]; then
161             echo "The command sudo pkill testpmd failed"
162             sudo pkill -9 testpmd
163             exit 1
164         fi
165     else
166         echo "testpmd is not running"
167     fi
168
169     # Try to kill the l3fwd.
170     l3fwd_pid="$(pgrep l3fwd)"
171     if [ ! -z "${l3fwd_pid}" ]; then
172         success=false
173         sudo kill -15 "${l3fwd_pid}"
174         for attempt in {1..60}; do
175             echo "Checking if l3fwd is still alive, attempt nr ${attempt}"
176             l3fwd_pid="$(pgrep l3fwd)"
177             if [ -z "${l3fwd_pid}" ]; then
178                 success=true
179                 break
180             fi
181             echo "l3fwd is still alive, waiting 1 second"
182             sleep 1
183         done
184         if [ "${success}" = false ]; then
185             echo "The command sudo kill -15 l3fwd failed"
186             sudo kill -9 "${l3fwd_pid}"
187             exit 1
188         fi
189     else
190         echo "l3fwd is not running"
191     fi
192
193     # Remove hugepages
194     sudo rm -rf /dev/hugepages/* || die "Removing hugepages failed!"
195 }
196
197
198 function dpdk_l3fwd_compile () {
199
200     # Compile DPDK l3fwd sample app.
201     #
202     # Variables read:
203     # - DPDK_DIR - Path to DPDK framework.
204     # - CSIT_DIR - Path to CSIT framework.
205     # Functions called:
206     # - die - Print to stderr and exit.
207
208     set -exuo pipefail
209
210     pushd "${DPDK_DIR}" || die "Pushd failed"
211     # Patch L3FWD.
212     sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128"
213     sed_rxd+="/#define RTE_TEST_RX_DESC_DEFAULT 2048/g"
214     sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512"
215     sed_txd+="/#define RTE_TEST_TX_DESC_DEFAULT 2048/g"
216     sed_file="./main.c"
217     pushd examples/l3fwd || die "Pushd failed"
218     sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
219     sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
220     chmod +x ${1} && source ${1} || die "Patch failed"
221     popd || die "Popd failed"
222
223     ninja -C build || die "Failed to compile DPDK!"
224 }
225
226
227 function dpdk_l3fwd () {
228
229     # Run DPDK l3fwd.
230     #
231     # Variables read:
232     # - DPDK_DIR - Path to DPDK framework.
233     # Functions called:
234     # - die - Print to stderr and exit.
235
236     set -exuo pipefail
237
238     rm -f screenlog.0 || true
239     binary="${DPDK_DIR}/build/examples/dpdk-l3fwd"
240
241     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
242         die "Failed to start l3fwd"
243     }
244
245     for attempt in {1..60}; do
246         echo "Checking if l3fwd is alive, attempt nr ${attempt}"
247         if fgrep "L3FWD: entering main loop on lcore" screenlog.0; then
248             cat screenlog.0
249             exit 0
250         fi
251         sleep 1
252     done
253     cat screenlog.0
254
255     exit 1
256 }
257
258
259 function dpdk_l3fwd_check () {
260
261     # DPDK l3fwd check state.
262
263     set -exuo pipefail
264
265     for attempt in {1..60}; do
266         echo "Checking if l3fwd state is ok, attempt nr ${attempt}"
267         if fgrep "Port 0 Link up" screenlog.0 && \
268         fgrep "Port 1 Link up" screenlog.0; then
269             cat screenlog.0
270             dpdk_l3fwd_pid
271             exit 0
272         fi
273         sleep 1
274     done
275     cat screenlog.0
276
277     exit 1
278 }
279
280
281 function dpdk_l3fwd_pid () {
282     l3fwd_pid="$(pidof dpdk-l3fwd)"
283     if [ ! -z "${l3fwd_pid}" ]; then
284         echo "L3fwd process ID: ${l3fwd_pid}"
285     else
286         echo "L3fwd not running!"
287     fi
288 }
289
290
291 function dpdk_precheck () {
292
293     # Precheck system settings (nr_hugepages, max_map_count).
294     #
295     # Functions called:
296     # - die - Print to stderr and exit.
297
298     set -exuo pipefail
299
300     sys_hugepage="$(< /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)"
301     node0="/sys/devices/system/node/node0/hugepages/hugepages-2048kB/"
302     node1="/sys/devices/system/node/node1/hugepages/hugepages-2048kB/"
303     if [ ${sys_hugepage} -lt 4096 ]; then
304         echo 2048 | sudo tee "${node0}"/nr_hugepages || die
305         echo 2048 | sudo tee "${node1}"/nr_hugepages || die
306     fi
307
308     sys_map="$(< /proc/sys/vm/max_map_count)"
309     if [ ${sys_map} -lt 200000 ]; then
310         echo 200000 | sudo tee /proc/sys/vm/max_map_count || die
311     fi
312 }
313
314
315 function dpdk_testpmd () {
316
317     # Run DPDK testpmd.
318     #
319     # Variables read:
320     # - DPDK_DIR - Path to DPDK framework.
321     # Functions called:
322     # - die - Print to stderr and exit.
323
324     set -exuo pipefail
325
326     rm -f screenlog.0 || true
327     binary="${DPDK_DIR}/build/app/dpdk-testpmd"
328
329     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
330         die "Failed to start testpmd"
331     }
332
333     for attempt in {1..60}; do
334         echo "Checking if testpmd is alive, attempt nr ${attempt}"
335         if fgrep "Press enter to exit" screenlog.0; then
336             cat screenlog.0
337             dpdk_testpmd_pid
338             exit 0
339         fi
340         sleep 1
341     done
342     cat screenlog.0
343
344     exit 1
345 }
346
347
348 function dpdk_testpmd_check () {
349
350     # DPDK testpmd check links state.
351
352     set -exuo pipefail
353
354     for attempt in {1..60}; do
355         echo "Checking if testpmd links state changed, attempt nr ${attempt}"
356         if fgrep "Port 0: link state change event" screenlog.0 && \
357         fgrep "Port 1: link state change event" screenlog.0; then
358             cat screenlog.0
359             exit 0
360         fi
361         sleep 1
362     done
363     cat screenlog.0
364
365     exit 1
366 }
367
368
369 function dpdk_testpmd_pid () {
370     testpmd_pid="$(pidof dpdk-testpmd)"
371     if [ ! -z "${testpmd_pid}" ]; then
372         echo "Testpmd process ID: ${testpmd_pid}"
373     else
374         echo "Testpmd not running!"
375     fi
376 }