Fix DPDK build on arm
[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     arch=$(uname -m) || {
88         die "Get CPU architecture failed."
89     }
90
91     # DPDK prefers "arm64" to "aarch64" and does not allow arm64 native target.
92     if [ ${arch} == "aarch64" ]; then
93         arch="arm64"
94         machine="armv8a"
95     else
96         machine="native"
97     fi
98
99     # Patch settings.
100     sed_mlx="s/^CONFIG_RTE_LIBRTE_MLX5_PMD=n/CONFIG_RTE_LIBRTE_MLX5_PMD=y/g"
101     sed_i40e="s/^CONFIG_RTE_LIBRTE_I40E_16BYTE_RX_DESC=n/CONFIG_RTE_LIBRTE_I40E_16BYTE_RX_DESC=y/g"
102     sed_file="./config/common_base"
103
104     pushd "${DPDK_DIR}" || die "Pushd failed"
105     if ( lsmod || die ) | fgrep mlx; then
106         sed -i "${sed_mlx}" "${sed_file}" || die
107     fi
108
109     sed -i "${sed_i40e}" "${sed_file}" || die "Patch failed"
110
111     sed_build_fix='s/#include <\(rte_ethdev.*.h\)>/#include "\1"/g'
112     # can't put the filename in quotes so that shell expands it
113     sed -i "${sed_build_fix}" ./lib/librte_ethdev/rte_ethdev*.h || {
114         die "DPDK build patch failed"
115     }
116
117     # Compile
118     make install T="${arch}"-"${machine}"-linuxapp-gcc -j || {
119         die "Failed to compile DPDK!"
120     }
121     popd || die "Popd failed"
122
123     # Compile the l3fwd.
124     export RTE_SDK="${DPDK_DIR}/"
125     export RTE_TARGET="${arch}-${machine}-linuxapp-gcc"
126     # Patch settings.
127     sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128/#define RTE_TEST_RX_DESC_DEFAULT 1024/g"
128     sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512/#define RTE_TEST_TX_DESC_DEFAULT 1024/g"
129     sed_file="./main.c"
130     pushd "${RTE_SDK}"/examples/l3fwd || die "Pushd failed"
131     sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
132     sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
133     make clean || die "Failed to compile l3fwd"
134     make -j || die "Failed to compile l3fwd"
135     popd || die "Popd failed"
136 }
137
138
139 function dpdk_extract () {
140
141     # Extract DPDK framework.
142     #
143     # Variables read:
144     # - DPDK_DIR - Path to DPDK framework.
145     # - CSIT_DIR - Path to CSIT framework.
146     # Functions called:
147     # - die - Print to stderr and exit.
148
149     set -exuo pipefail
150
151     pushd "${CSIT_DIR}" || die "Pushd failed"
152     tar -xvf download_dir/dpdk*.tar.xz --strip=1 --directory "${DPDK_DIR}" || {
153         die "Failed to extract DPDK!"
154     }
155 }
156
157
158 function dpdk_kill () {
159
160     # Kill testpmd and/or l3fwd if running.
161
162     # Function will be noisy and requires custom error handling.
163     set -x
164     set +e
165
166     # Try to kill the testpmd.
167     sudo pgrep testpmd
168     if [ $? -eq "0" ]; then
169         success=false
170         sudo pkill testpmd
171         for attempt in {1..60}; do
172             echo "Checking if testpmd is still alive, attempt nr ${attempt}"
173             sudo pgrep testpmd
174             if [ $? -eq "1" ]; then
175                 success=true
176                 break
177             fi
178             echo "testpmd is still alive, waiting 1 second"
179             sleep 1
180         done
181         if [ "$success" = false ]; then
182             echo "The command sudo pkill testpmd failed"
183             sudo pkill -9 testpmd
184             exit 1
185         fi
186     else
187         echo "testpmd is not running"
188     fi
189
190     # Try to kill the l3fwd.
191     l3fwd_pid="$(pgrep l3fwd)"
192     if [ ! -z "${l3fwd_pid}" ]; then
193         success=false
194         sudo kill -15 "${l3fwd_pid}"
195         for attempt in {1..60}; do
196             echo "Checking if l3fwd is still alive, attempt nr ${attempt}"
197             l3fwd_pid="$(pgrep l3fwd)"
198             if [ -z "${l3fwd_pid}" ]; then
199                 success=true
200                 break
201             fi
202             echo "l3fwd is still alive, waiting 1 second"
203             sleep 1
204         done
205         if [ "${success}" = false ]; then
206             echo "The command sudo kill -15 l3fwd failed"
207             sudo kill -9 "${l3fwd_pid}"
208             exit 1
209         fi
210     else
211         echo "l3fwd is not running"
212     fi
213
214     # Remove hugepages
215     sudo rm -rf /dev/hugepages/* || die "Removing hugepages failed!"
216 }
217
218
219 function dpdk_l3fwd_compile () {
220
221     # Compile DPDK l3fwd sample app.
222     #
223     # Variables read:
224     # - DPDK_DIR - Path to DPDK framework.
225     # - CSIT_DIR - Path to CSIT framework.
226     # Functions called:
227     # - die - Print to stderr and exit.
228
229     set -exuo pipefail
230
231     arch=$(uname -m) || {
232         die "Get CPU architecture failed."
233     }
234
235     # DPDK prefers "arm64" to "aarch64" and does not allow arm64 native target.
236     if [ ${arch} == "aarch64" ]; then
237         arch="arm64"
238         machine="armv8a"
239     else
240         machine="native"
241     fi
242
243     # Compile the l3fwd.
244     export RTE_SDK="${DPDK_DIR}/"
245     export RTE_TARGET="${arch}-${machine}-linuxapp-gcc"
246     # Patch settings.
247     sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128/#define RTE_TEST_RX_DESC_DEFAULT 2048/g"
248     sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512/#define RTE_TEST_TX_DESC_DEFAULT 2048/g"
249     sed_file="./main.c"
250     pushd "${RTE_SDK}"/examples/l3fwd || die "Pushd failed"
251     sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
252     sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
253     chmod +x ${1} && source ${1} || die "Patch failed"
254     make clean || die "Failed to compile l3fwd"
255     make -j || die "Failed to compile l3fwd"
256     popd || die "Popd failed"
257 }
258
259
260 function dpdk_l3fwd () {
261
262     # Run DPDK l3fwd.
263     #
264     # Variables read:
265     # - DPDK_DIR - Path to DPDK framework.
266     # Functions called:
267     # - die - Print to stderr and exit.
268
269     set -exuo pipefail
270
271     arch=$(uname -m) || {
272         die "Get CPU architecture failed."
273     }
274
275     # DPDK prefers "arm64" to "aarch64" and does not allow arm64 native target.
276     if [ ${arch} == "aarch64" ]; then
277         arch="arm64"
278         machine="armv8a"
279     else
280         machine="native"
281     fi
282
283     rm -f screenlog.0 || true
284     binary="${DPDK_DIR}/examples/l3fwd/build/app/l3fwd"
285
286     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
287         die "Failed to start l3fwd"
288     }
289
290     for attempt in {1..60}; do
291         echo "Checking if l3fwd is alive, attempt nr ${attempt}"
292         if fgrep "L3FWD: entering main loop on lcore" screenlog.0; then
293             exit 0
294         fi
295         sleep 1
296     done
297     cat screenlog.0
298
299     exit 1
300 }
301
302
303 function dpdk_precheck () {
304
305     # Precheck system settings (nr_hugepages, max_map_count).
306     #
307     # Functions called:
308     # - die - Print to stderr and exit.
309
310     set -exuo pipefail
311
312     sys_hugepage="$(< /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)"
313     node0="/sys/devices/system/node/node0/hugepages/hugepages-2048kB/"
314     node1="/sys/devices/system/node/node1/hugepages/hugepages-2048kB/"
315     if [ ${sys_hugepage} -lt 4096 ]; then
316         echo 2048 | sudo tee "${node0}"/nr_hugepages || die
317         echo 2048 | sudo tee "${node1}"/nr_hugepages || die
318     fi
319
320     sys_map="$(< /proc/sys/vm/max_map_count)"
321     if [ ${sys_map} -lt 200000 ]; then
322         echo 200000 | sudo tee /proc/sys/vm/max_map_count || die
323     fi
324 }
325
326
327 function dpdk_testpmd () {
328
329     # Run DPDK testpmd.
330     #
331     # Variables read:
332     # - DPDK_DIR - Path to DPDK framework.
333     # Functions called:
334     # - die - Print to stderr and exit.
335
336     set -exuo pipefail
337
338     arch=$(uname -m) || {
339         die "Get CPU architecture failed."
340     }
341
342     # DPDK prefers "arm64" to "aarch64" and does not allow arm64 native target.
343     if [ ${arch} == "aarch64" ]; then
344         arch="arm64"
345         machine="armv8a"
346     else
347         machine="native"
348     fi
349
350     rm -f screenlog.0 || true
351     binary="${DPDK_DIR}/${arch}-${machine}-linuxapp-gcc/app/testpmd"
352
353     sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
354         die "Failed to start testpmd"
355     }
356
357     for attempt in {1..60}; do
358         echo "Checking if testpmd is alive, attempt nr ${attempt}"
359          if fgrep "Press enter to exit" screenlog.0 && \
360             fgrep "Port 0: link state change event" screenlog.0 && \
361             fgrep "Port 1: link state change event" screenlog.0; then
362              cat screenlog.0
363              exit 0
364         fi
365         sleep 1
366     done
367     cat screenlog.0
368
369     exit 1
370 }