fix: l3fwd config
[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     # Compile using Meson and Ninja.
100     meson ${meson_options} build || {
101         die "Failed to compile DPDK!"
102     }
103     ninja -C build || die "Failed to compile DPDK!"
104 }
105
106
107 function dpdk_extract () {
108
109     # Extract DPDK framework.
110     #
111     # Variables read:
112     # - DPDK_DIR - Path to DPDK framework.
113     # - CSIT_DIR - Path to CSIT framework.
114     # Functions called:
115     # - die - Print to stderr and exit.
116
117     set -exuo pipefail
118
119     pushd "${CSIT_DIR}" || die "Pushd failed"
120     tar -xvf download_dir/dpdk*.tar.xz --strip=1 --directory "${DPDK_DIR}" || {
121         die "Failed to extract DPDK!"
122     }
123 }
124
125
126 function dpdk_kill () {
127
128     # Kill testpmd and/or l3fwd if running.
129
130     # Function will be noisy and requires custom error handling.
131     set -x
132     set +e
133
134     # Try to kill the testpmd.
135     sudo pgrep testpmd
136     if [ $? -eq "0" ]; then
137         success=false
138         sudo pkill testpmd
139         for attempt in {1..60}; do
140             echo "Checking if testpmd is still alive, attempt nr ${attempt}"
141             sudo pgrep testpmd
142             if [ $? -eq "1" ]; then
143                 success=true
144                 break
145             fi
146             echo "testpmd is still alive, waiting 1 second"
147             sleep 1
148         done
149         if [ "$success" = false ]; then
150             echo "The command sudo pkill testpmd failed"
151             sudo pkill -9 testpmd
152             exit 1
153         fi
154     else
155         echo "testpmd is not running"
156     fi
157
158     # Try to kill the l3fwd.
159     l3fwd_pid="$(pgrep l3fwd)"
160     if [ ! -z "${l3fwd_pid}" ]; then
161         success=false
162         sudo kill -15 "${l3fwd_pid}"
163         for attempt in {1..60}; do
164             echo "Checking if l3fwd is still alive, attempt nr ${attempt}"
165             l3fwd_pid="$(pgrep l3fwd)"
166             if [ -z "${l3fwd_pid}" ]; then
167                 success=true
168                 break
169             fi
170             echo "l3fwd is still alive, waiting 1 second"
171             sleep 1
172         done
173         if [ "${success}" = false ]; then
174             echo "The command sudo kill -15 l3fwd failed"
175             sudo kill -9 "${l3fwd_pid}"
176             exit 1
177         fi
178     else
179         echo "l3fwd is not running"
180     fi
181
182     # Remove hugepages
183     sudo rm -rf /dev/hugepages/* || die "Removing hugepages failed!"
184 }
185
186
187 function dpdk_l3fwd_compile () {
188
189     # Compile DPDK l3fwd sample app.
190     #
191     # Variables read:
192     # - DPDK_DIR - Path to DPDK framework.
193     # Functions called:
194     # - die - Print to stderr and exit.
195
196     set -exuo pipefail
197
198     pushd "${DPDK_DIR}" || die "Pushd failed"
199     # Patch L3FWD.
200     pushd examples/l3fwd || die "Pushd failed"
201     chmod +x ${1} && source ${1} || die "Patch failed"
202     popd || die "Popd failed"
203
204     ninja -C build || die "Failed to compile DPDK!"
205 }
206
207
208 function dpdk_l3fwd () {
209
210     # Run DPDK l3fwd.
211     #
212     # Variables read:
213     # - DPDK_DIR - Path to DPDK framework.
214     # Functions called:
215     # - die - Print to stderr and exit.
216
217     set -exuo pipefail
218
219     rm -f screenlog.0 || true
220     binary="${DPDK_DIR}/build/examples/dpdk-l3fwd"
221
222     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
223         die "Failed to start l3fwd"
224     }
225
226     for attempt in {1..60}; do
227         echo "Checking if l3fwd is alive, attempt nr ${attempt}"
228         if fgrep "L3FWD: entering main loop on lcore" screenlog.0; then
229             cat screenlog.0
230             exit 0
231         fi
232         sleep 1
233     done
234     cat screenlog.0
235
236     exit 1
237 }
238
239
240 function dpdk_l3fwd_check () {
241
242     # DPDK l3fwd check state.
243
244     set -exuo pipefail
245
246     for attempt in {1..60}; do
247         echo "Checking if l3fwd state is ok, attempt nr ${attempt}"
248         if fgrep "Link up" screenlog.0; then
249             cat screenlog.0
250             dpdk_l3fwd_pid
251             exit 0
252         fi
253         sleep 1
254     done
255     cat screenlog.0
256
257     exit 1
258 }
259
260
261 function dpdk_l3fwd_pid () {
262     l3fwd_pid="$(pidof dpdk-l3fwd)"
263     if [ ! -z "${l3fwd_pid}" ]; then
264         echo "L3fwd process ID: ${l3fwd_pid}"
265     else
266         echo "L3fwd not running!"
267     fi
268 }
269
270
271 function dpdk_precheck () {
272
273     # Precheck system settings (nr_hugepages, max_map_count).
274     #
275     # Functions called:
276     # - die - Print to stderr and exit.
277
278     set -exuo pipefail
279
280     sys_hugepage="$(< /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)"
281     node0="/sys/devices/system/node/node0/hugepages/hugepages-2048kB/"
282     node1="/sys/devices/system/node/node1/hugepages/hugepages-2048kB/"
283     if [ ${sys_hugepage} -lt 4096 ]; then
284         echo 2048 | sudo tee "${node0}"/nr_hugepages || die
285         echo 2048 | sudo tee "${node1}"/nr_hugepages || die
286     fi
287
288     sys_map="$(< /proc/sys/vm/max_map_count)"
289     if [ ${sys_map} -lt 200000 ]; then
290         echo 200000 | sudo tee /proc/sys/vm/max_map_count || die
291     fi
292 }
293
294
295 function dpdk_testpmd () {
296
297     # Run DPDK testpmd.
298     #
299     # Variables read:
300     # - DPDK_DIR - Path to DPDK framework.
301     # Functions called:
302     # - die - Print to stderr and exit.
303
304     set -exuo pipefail
305
306     rm -f screenlog.0 || true
307     binary="${DPDK_DIR}/build/app/dpdk-testpmd"
308
309     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
310         die "Failed to start testpmd"
311     }
312
313     for attempt in {1..60}; do
314         echo "Checking if testpmd is alive, attempt nr ${attempt}"
315         if fgrep "Press enter to exit" screenlog.0; then
316             cat screenlog.0
317             dpdk_testpmd_pid
318             exit 0
319         fi
320         sleep 1
321     done
322     cat screenlog.0
323
324     exit 1
325 }
326
327
328 function dpdk_testpmd_check () {
329
330     # DPDK testpmd check links state.
331
332     set -exuo pipefail
333
334     for attempt in {1..60}; do
335         echo "Checking if testpmd links state changed, attempt nr ${attempt}"
336         if fgrep "link state change event" screenlog.0; then
337             cat screenlog.0
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_pid () {
349     testpmd_pid="$(pidof dpdk-testpmd)"
350     if [ ! -z "${testpmd_pid}" ]; then
351         echo "Testpmd process ID: ${testpmd_pid}"
352     else
353         echo "Testpmd not running!"
354     fi
355 }