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