Remove obsolete bootstraps
[csit.git] / resources / libraries / bash / shell / dpdk_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 function dpdk_utils.dpdk_delete {
16     # Deletes the DPDK directory
17     # DPDK install directory
18     dpdk_install_dir=$1
19     # DPDK install version
20     dpdk_install_ver=$2
21
22     [ -d ${dpdk_install_dir}/${dpdk_install_ver} ] && \
23         sudo rm -r ${dpdk_install_dir}/${dpdk_install_ver} && \
24         echo "${dpdk_install_dir}/${dpdk_install_ver} removed"
25 }
26
27 function dpdk_utils.dpdk_install {
28     # Downloads and installs DPDK
29     # DPDK install directory
30     dpdk_install_dir=$1
31     # DPDK install version
32     dpdk_install_ver=$2
33     # DPDK compile target
34     dpdk_target=x86_64-native-linuxapp-gcc
35     # Force install (if true then remove previous installation; default false)
36     force_install=${3:-false}
37
38     if [ "$force_install" = true ]; then
39         # Cleanup DPDK DIR
40         dpdk_utils.dpdk_delete ${dpdk_install_dir} ${dpdk_install_ver}
41     else
42         # Test if DPDK was installed previously
43         test -d ${dpdk_install_dir}/${dpdk_install_ver} && \
44             { echo "DPDK ${dpdk_install_ver} ready"; exit 0; }
45     fi
46
47     # Download the DPDK package if no local copy exists
48     if [ ! -f ${dpdk_install_dir}/${dpdk_install_ver}.tar.xz ]; then
49         sudo wget -e use_proxy=yes -P ${dpdk_install_dir} -q \
50             fast.dpdk.org/rel/${dpdk_install_ver}.tar.xz || \
51             { echo "Failed to download ${dpdk_install_ver}"; exit 1; }
52     fi
53
54     # Create DPDK install dir if not exists and extract
55     sudo mkdir -p ${dpdk_install_dir} || \
56         { echo "Failed to create ${dpdk_install_dir}"; exit 1; }
57     sudo tar -xJf ${dpdk_install_dir}/${dpdk_install_ver}.tar.xz \
58         -C ${dpdk_install_dir} || \
59         { echo "Failed to extract ${dpdk_install_ver}.tar.xz"; exit 1; }
60
61     cd ${dpdk_install_dir}/${dpdk_install_ver}
62
63     # Compile and install the DPDK
64     sudo make install T=${dpdk_target} -j DESTDIR=install || \
65         { echo "Installation of ${dpdk_install_ver} failed"; exit 1; }
66
67     echo "DPDK ${dpdk_install_ver} ready"
68 }
69
70 function dpdk_utils.load_modules {
71     # Loads kernel modules and bind interfaces to drivers
72     # Use igb_uio [true|false]
73     use_igb_uio=${1:-false}
74     # DPDK install directory
75     dpdk_install_dir=$2
76     # DPDK install version
77     dpdk_install_ver=$3
78
79     sudo modprobe uio
80     sudo modprobe uio_pci_generic
81
82     if [ "${use_igb_uio}" = true ]; then
83         sudo rmmod igb_uio
84         # Try to insert IGB_UIO module
85         sudo insmod ${dpdk_install_dir}/${dpdk_install_ver}/x86_64-native-linuxapp-gcc/kmod/igb_uio.ko
86         # If failed then download/compile DPDK
87         if [ $? -ne 0 ]; then
88             dpdk_utils.dpdk_install ${dpdk_install_dir} ${dpdk_install_ver} true
89             sudo insmod ${dpdk_install_dir}/${dpdk_install_ver}/x86_64-native-linuxapp-gcc/kmod/igb_uio.ko
90         fi
91     fi
92 }