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