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