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