aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/bash/function/dpdk.sh
blob: f013683659e6aac64510f53cd94548f3669739a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env bash

# Copyright (c) 2022 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -exuo pipefail


function common_dirs () {

    # Set global variables, create some directories (without touching content).
    # This function assumes running in remote testbed. It might override other
    # functions if included from common.sh.

    # Variables set:
    # - BASH_FUNCTION_DIR - Path to existing directory this file is located in.
    # - DPDK_DIR - Path to DPDK framework.
    # - CSIT_DIR - Path to CSIT framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    this_file=$(readlink -e "${BASH_SOURCE[0]}") || {
        die "Some error during locating of this source file."
    }
    BASH_FUNCTION_DIR=$(dirname "${this_file}") || {
        die "Some error during dirname call."
    }
    CSIT_DIR=$(readlink -e "/tmp/openvpp-testing") || {
        die "Readlink failed."
    }
    mkdir -p "${CSIT_DIR}/dpdk" || die "Mkdir failed."
    DPDK_DIR=$(readlink -e "${CSIT_DIR}/dpdk") || {
        die "Readlink failed."
    }
}


function dpdk_bind () {

    # Bind interfaces to driver.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # - @ - Script cli arguments.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    pushd "${DPDK_DIR}/" || die "Pushd failed"

    sudo ./usertools/dpdk-devbind.py -b "${@}" || {
        die "Bind ${@} failed"
    }

    popd || die "Popd failed"
}


function dpdk_compile () {

    # Compile DPDK archive.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # - CSIT_DIR - Path to CSIT framework.
    # Variables exported:
    # - RTE_SDK - DPDK directory.
    # - RTE_TARGET - Make targed of DPDK framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    pushd "${DPDK_DIR}" || die "Pushd failed"

    # enable l3fwd
    meson_options="-Dexamples=l3fwd "

    # i40e specific options
    meson_options="${meson_options} \
        -Dc_args=-DRTE_LIBRTE_I40E_16BYTE_RX_DESC=y"

    # Configure generic build - the same used by VPP
    meson_options="${meson_options} -Dplatform=generic"

    # Patch L3FWD.
    sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128"
    sed_rxd+="/#define RTE_TEST_RX_DESC_DEFAULT 1024/g"
    sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512"
    sed_txd+="/#define RTE_TEST_TX_DESC_DEFAULT 1024/g"
    sed_file="./main.c"
    pushd examples/l3fwd || die "Pushd failed"
    sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
    sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
    popd || die "Popd failed"

    # Compile using Meson and Ninja.
    meson ${meson_options} build || {
        die "Failed to compile DPDK!"
    }
    ninja -C build || die "Failed to compile DPDK!"
}


function dpdk_extract () {

    # Extract DPDK framework.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # - CSIT_DIR - Path to CSIT framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    pushd "${CSIT_DIR}" || die "Pushd failed"
    tar -xvf download_dir/dpdk*.tar.xz --strip=1 --directory "${DPDK_DIR}" || {
        die "Failed to extract DPDK!"
    }
}


function dpdk_kill () {

    # Kill testpmd and/or l3fwd if running.

    # Function will be noisy and requires custom error handling.
    set -x
    set +e

    # Try to kill the testpmd.
    sudo pgrep testpmd
    if [ $? -eq "0" ]; then
        success=false
        sudo pkill testpmd
        for attempt in {1..60}; do
            echo "Checking if testpmd is still alive, attempt nr ${attempt}"
            sudo pgrep testpmd
            if [ $? -eq "1" ]; then
                success=true
                break
            fi
            echo "testpmd is still alive, waiting 1 second"
            sleep 1
        done
        if [ "$success" = false ]; then
            echo "The command sudo pkill testpmd failed"
            sudo pkill -9 testpmd
            exit 1
        fi
    else
        echo "testpmd is not running"
    fi

    # Try to kill the l3fwd.
    l3fwd_pid="$(pgrep l3fwd)"
    if [ ! -z "${l3fwd_pid}" ]; then
        success=false
        sudo kill -15 "${l3fwd_pid}"
        for attempt in {1..60}; do
            echo "Checking if l3fwd is still alive, attempt nr ${attempt}"
            l3fwd_pid="$(pgrep l3fwd)"
            if [ -z "${l3fwd_pid}" ]; then
                success=true
                break
            fi
            echo "l3fwd is still alive, waiting 1 second"
            sleep 1
        done
        if [ "${success}" = false ]; then
            echo "The command sudo kill -15 l3fwd failed"
            sudo kill -9 "${l3fwd_pid}"
            exit 1
        fi
    else
        echo "l3fwd is not running"
    fi

    # Remove hugepages
    sudo rm -rf /dev/hugepages/* || die "Removing hugepages failed!"
}


function dpdk_l3fwd_compile () {

    # Compile DPDK l3fwd sample app.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # - CSIT_DIR - Path to CSIT framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    pushd "${DPDK_DIR}" || die "Pushd failed"
    # Patch L3FWD.
    sed_rxd="s/^#define RTE_TEST_RX_DESC_DEFAULT 128"
    sed_rxd+="/#define RTE_TEST_RX_DESC_DEFAULT 2048/g"
    sed_txd="s/^#define RTE_TEST_TX_DESC_DEFAULT 512"
    sed_txd+="/#define RTE_TEST_TX_DESC_DEFAULT 2048/g"
    sed_file="./main.c"
    pushd examples/l3fwd || die "Pushd failed"
    sed -i "${sed_rxd}" "${sed_file}" || die "Patch failed"
    sed -i "${sed_txd}" "${sed_file}" || die "Patch failed"
    chmod +x ${1} && source ${1} || die "Patch failed"
    popd || die "Popd failed"

    ninja -C build || die "Failed to compile DPDK!"
}


function dpdk_l3fwd () {

    # Run DPDK l3fwd.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    rm -f screenlog.0 || true
    binary="${DPDK_DIR}/build/examples/dpdk-l3fwd"

    sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
        die "Failed to start l3fwd"
    }

    for attempt in {1..60}; do
        echo "Checking if l3fwd is alive, attempt nr ${attempt}"
        if fgrep "L3FWD: entering main loop on lcore" screenlog.0; then
            cat screenlog.0
            exit 0
        fi
        sleep 1
    done
    cat screenlog.0

    exit 1
}


function dpdk_l3fwd_check () {

    # DPDK l3fwd check state.

    set -exuo pipefail

    for attempt in {1..60}; do
        echo "Checking if l3fwd state is ok, attempt nr ${attempt}"
        if fgrep "Link up" screenlog.0; then
            cat screenlog.0
            dpdk_l3fwd_pid
            exit 0
        fi
        sleep 1
    done
    cat screenlog.0

    exit 1
}


function dpdk_l3fwd_pid () {
    l3fwd_pid="$(pidof dpdk-l3fwd)"
    if [ ! -z "${l3fwd_pid}" ]; then
        echo "L3fwd process ID: ${l3fwd_pid}"
    else
        echo "L3fwd not running!"
    fi
}


function dpdk_precheck () {

    # Precheck system settings (nr_hugepages, max_map_count).
    #
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    sys_hugepage="$(< /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)"
    node0="/sys/devices/system/node/node0/hugepages/hugepages-2048kB/"
    node1="/sys/devices/system/node/node1/hugepages/hugepages-2048kB/"
    if [ ${sys_hugepage} -lt 4096 ]; then
        echo 2048 | sudo tee "${node0}"/nr_hugepages || die
        echo 2048 | sudo tee "${node1}"/nr_hugepages || die
    fi

    sys_map="$(< /proc/sys/vm/max_map_count)"
    if [ ${sys_map} -lt 200000 ]; then
        echo 200000 | sudo tee /proc/sys/vm/max_map_count || die
    fi
}


function dpdk_testpmd () {

    # Run DPDK testpmd.
    #
    # Variables read:
    # - DPDK_DIR - Path to DPDK framework.
    # Functions called:
    # - die - Print to stderr and exit.

    set -exuo pipefail

    rm -f screenlog.0 || true
    binary="${DPDK_DIR}/build/app/dpdk-testpmd"

    sudo sh -c "screen -dmSL DPDK-test ${binary} ${@}" || {
        die "Failed to start testpmd"
    }

    for attempt in {1..60}; do
        echo "Checking if testpmd is alive, attempt nr ${attempt}"
        if fgrep "Press enter to exit" screenlog.0; then
            cat screenlog.0
            dpdk_testpmd_pid
            exit 0
        fi
        sleep 1
    done
    cat screenlog.0

    exit 1
}


function dpdk_testpmd_check () {

    # DPDK testpmd check links state.

    set -exuo pipefail

    for attempt in {1..60}; do
        echo "Checking if testpmd links state changed, attempt nr ${attempt}"
        if fgrep "link state change event" screenlog.0; then
            cat screenlog.0
            exit 0
        fi
        sleep 1
    done
    cat screenlog.0

    exit 1
}


function dpdk_testpmd_pid () {
    testpmd_pid="$(pidof dpdk-testpmd)"
    if [ ! -z "${testpmd_pid}" ]; then
        echo "Testpmd process ID: ${testpmd_pid}"
    else
        echo "Testpmd not running!"
    fi
}