Revert "[SAMPLEVNF] Adding UDP Replay VNF"
[samplevnf.git] / tools / vnf_build.sh
1 #! /bin/bash
2 #
3 # Copyright (c) 2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 cd $(dirname ${BASH_SOURCE[0]})/..
19 export VNF_CORE=$PWD
20 echo "------------------------------------------------------------------------------"
21 echo " VNF_CORE exported as $VNF_CORE"
22 echo "------------------------------------------------------------------------------"
23
24 HUGEPGSZ=`cat /proc/meminfo  | grep Hugepagesize | cut -d : -f 2 | tr -d ' '`
25 MODPROBE="/sbin/modprobe"
26 INSMOD="/sbin/insmod"
27 DPDK_DOWNLOAD="http://dpdk.org/browse/dpdk/snapshot/dpdk-16.04.zip"
28 DPDK_DIR=$VNF_CORE/dpdk
29
30 #
31 # Sets QUIT variable so script will finish.
32 #
33 quit()
34 {
35         QUIT=$1
36 }
37
38 # Shortcut for quit.
39 q()
40 {
41         quit
42 }
43
44 setup_http_proxy()
45 {
46         while true; do
47                 echo
48                 read -p "Enter Proxy : " proxy
49                 export http_proxy=$proxy
50                 export https_proxy=$proxy
51                 echo "Acquire::http::proxy \"$http_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
52                 echo "Acquire::https::proxy \"$http_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
53
54                 wget -T 20 -t 3 --spider http://www.google.com > /dev/null 2>&1
55                 if [ "$?" != 0 ]; then
56                         echo -e "No Internet connection. Proxy incorrect? Try again"
57                         echo -e "eg: http://<proxy>:<port>"
58                         exit 1
59                 fi
60         return
61         done
62         echo "Network connectivity successful."
63 }
64
65 step_1()
66 {
67         TITLE="Environment setup."
68         CONFIG_NUM=1
69         TEXT[1]="Check OS and network connection"
70         FUNC[1]="setup_env"
71 }
72 setup_env()
73 {
74         # a. Check for OS dependencies
75         source /etc/os-release
76         if [[ $VERSION_ID != "16.04" ]] ; then
77                 echo "WARNING: It is recommended to use Ubuntu 16.04..Your version is "$VERSION_ID
78         else
79                 echo "Ubuntu 16.04 OS requirement met..."
80         fi
81         echo
82         echo "Checking network connectivity..."
83         # b. Check for internet connections
84         wget -T 20 -t 3 --spider http://www.google.com > /dev/null 2>&1
85         if [ "$?" != 0 ]; then
86                 while true; do
87                         read -p "No Internet connection. Are you behind a proxy (y/n)? " yn
88                         case $yn in
89                                 [Yy]* ) $SETUP_PROXY ; return;;
90                                 [Nn]* ) echo "Please check your internet connection..." ; exit;;
91                                 * ) "Please answer yes or no.";;
92                         esac
93                 done
94         fi
95         echo "Network connectivity successful."
96 }
97
98 step_2()
99 {
100         TITLE="Download and Install"
101         CONFIG_NUM=1
102         TEXT[1]="Agree to download"
103         FUNC[1]="get_agreement_download"
104         TEXT[2]="Download packages"
105         FUNC[2]="install_libs"
106         TEXT[3]="Download DPDK zip"
107         FUNC[3]="download_dpdk_zip"
108         TEXT[4]="Build and Install DPDK"
109         FUNC[4]="install_dpdk"
110         TEXT[5]="Setup hugepages"
111         FUNC[5]="setup_hugepages"
112 }
113 get_agreement_download()
114 {
115         echo
116         echo "List of packages needed for VNFs build and installation:"
117         echo "-------------------------------------------------------"
118         echo "1. DPDK version 16.04"
119         echo "2. build-essential"
120         echo "3. linux-headers-generic"
121         echo "4. git"
122         echo "5. unzip"
123         echo "6. libpcap-dev"
124         echo "7. make"
125         echo "8. and other library dependencies"
126         while true; do
127                 read -p "We need download above mentioned package. Press (y/n) to continue? " yn
128                 case $yn in
129                         [Yy]* )
130                                 touch .agree
131                                 return;;
132                         [Nn]* ) exit;;
133                         * ) "Please answer yes or no.";;
134                 esac
135         done
136 }
137
138 install_libs()
139 {
140         echo "Install libs needed to build and run VNFs..."
141         file_name=".agree"
142         if [ ! -e "$file_name" ]; then
143                 echo "Please choose option '2.Agree to download' first"
144                 return
145         fi
146         file_name=".download"
147         if [ -e "$file_name" ]; then
148                 clear
149                 return
150         fi
151         sudo apt-get update
152         sudo apt-get -y install build-essential linux-headers-$(uname -r) git unzip libpcap0.8-dev gcc \
153                 make libc6 libc6-dev g++-multilib libzmq3-dev libcurl4-openssl-dev
154         touch .download
155 }
156
157 download_dpdk_zip()
158 {
159         echo "Download DPDK zip"
160         file_name=".agree"
161         if [ ! -e "$file_name" ]; then
162                 echo "Please choose option '2.Agree to download' first"
163                 return
164         fi
165         rm -rf $DPDK_DIR
166         if [ ! -e ${DPDK_DOWNLOAD##*/} ] ; then
167                 wget ${DPDK_DOWNLOAD}
168         fi
169         unzip -o ${DPDK_DOWNLOAD##*/}
170         mv $VNF_CORE/dpdk-16.04 $VNF_CORE/dpdk
171 }
172
173 install_dpdk()
174 {
175         echo "Build DPDK"
176
177         if [ ! -d "$DPDK_DIR" ]; then
178      echo "Please choose option '4 Download DPDK zip'"
179      return
180         fi
181
182         export RTE_TARGET=x86_64-native-linuxapp-gcc
183
184         pushd $DPDK_DIR
185         echo "Apply dpdk custom patches..."
186         patch -p0 < $VNF_CORE/patches/dpdk_custom_patch/rte_pipeline.patch
187         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-link-management.patch
188         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-Rx-hang-when-disable-LLDP.patch
189         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-link-status-change-interrupt.patch
190         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-VF-bonded-device-link-down.patch
191
192         make -j install T=$RTE_TARGET
193         if [ $? -ne 0 ] ; then
194                 echo "Failed to build dpdk, please check the errors."
195                 return
196         fi
197         sudo modinfo igb_uio
198         if [ $? -ne 0 ] ; then
199                 sudo $MODPROBE -v uio
200                 sudo $INSMOD $RTE_TARGET/kmod/igb_uio.ko
201                 sudo cp -f $RTE_TARGET/kmod/igb_uio.ko /lib/modules/$(uname -r)
202                 echo "uio" | sudo tee -a /etc/modules
203                 echo "igb_uio" | sudo tee -a /etc/modules
204                 sudo depmod
205         fi
206         popd
207 }
208
209 setup_hugepages()
210 {
211         #----
212         Pages=16
213         if [[ "$HUGEPGSZ" = "2048kB" ]] ; then
214                 Pages=8192
215         fi
216         if [ ! "`grep nr_hugepages /etc/sysctl.conf`" ] ; then
217                 echo "vm.nr_hugepages=$Pages" | sudo tee /etc/sysctl.conf
218         fi
219         sudo sysctl -p
220
221         sudo service procps start
222
223         grep -s '/dev/hugepages' /proc/mounts
224         if [ $? -ne 0 ] ; then
225                 echo "Creating /mnt/huge and mounting as hugetlbfs"
226                 sudo mkdir -p /mnt/huge
227                 sudo mount -t hugetlbfs nodev /mnt/huge
228                 echo "nodev /mnt/huge hugetlbfs defaults 0 0" | sudo tee -a /etc/fstab > /dev/null
229         fi
230 }
231
232 step_3()
233 {
234         TITLE="Build VNFs"
235         CONFIG_NUM=1
236                                 TEXT[1]="Build all VNFs (vACL, vCGNAPT, vFW)"
237         FUNC[1]="build_vnfs"
238 }
239
240 build_vnfs()
241 {
242
243         if [ ! -d "$DPDK_DIR" ]; then
244      echo "Please choose option '4 Download DPDK zip'"
245      return
246         fi
247
248         if [ ! -d "$DPDK_DIR/x86_64-native-linuxapp-gcc" ]; then
249      echo "Please choose option '5 Build and Install DPDK'"
250      return
251         fi
252
253         export RTE_SDK=$DPDK_DIR
254         export RTE_TARGET=x86_64-native-linuxapp-gcc
255         pushd $VNF_CORE
256         make clean
257         make || { echo -e "\nVNF: Make failed\n"; }
258         popd
259 }
260
261 #--- Add non intractive option to build vnfs
262 if [[ "$1" = "--silient" ]];then
263     pushd $VNF_CORE
264
265     echo "Setup proxy if needed..."
266     http_proxy=$2
267     https_proxy=$3
268     if [[ "$http_proxy" != "" ]]; then
269          export http_proxy=$http_proxy
270          export https_proxy=$http_proxy
271     fi
272
273     if [[ "$https_proxy" != "" ]]; then
274          export https_proxy=$https_proxy
275     fi
276
277     echo "Install required libraries..."
278     touch .agree
279     install_libs
280
281     echo "Download dpdk for VNF build..."
282     download_dpdk_zip
283
284     echo "Build dpdk..."
285     install_dpdk
286
287     echo "Setup hugepages..."
288     setup_hugepages
289
290     echo "build VNFS..."
291     build_vnfs
292
293     popd
294     exit
295 fi
296
297 SETUP_PROXY="setup_http_proxy"
298 STEPS[1]="step_1"
299 STEPS[2]="step_2"
300 STEPS[3]="step_3"
301
302 QUIT=0
303
304 clear
305
306 echo -n "Checking for user permission.. "
307 sudo -n true
308 if [ $? -ne 0 ]; then
309    echo "Password-less sudo user must run this script" 1>&2
310    exit 1
311 fi
312 echo "Done"
313 clear
314
315 while [ "$QUIT" == "0" ]; do
316         OPTION_NUM=1
317         for s in $(seq ${#STEPS[@]}) ; do
318                 ${STEPS[s]}
319
320                 echo "----------------------------------------------------------"
321                 echo " Step $s: ${TITLE}"
322                 echo "----------------------------------------------------------"
323
324                 for i in $(seq ${#TEXT[@]}) ; do
325                         echo "[$OPTION_NUM] ${TEXT[i]}"
326                         OPTIONS[$OPTION_NUM]=${FUNC[i]}
327                         let "OPTION_NUM+=1"
328                 done
329
330                 # Clear TEXT and FUNC arrays before next step
331                 unset TEXT
332                 unset FUNC
333
334                 echo ""
335         done
336
337         echo "[$OPTION_NUM] Exit Script"
338         OPTIONS[$OPTION_NUM]="quit"
339         echo ""
340         echo -n "Option: "
341         read our_entry
342         echo ""
343         ${OPTIONS[our_entry]} ${our_entry}
344
345         if [ "$QUIT" == "0" ] ; then
346                 echo
347                 echo -n "Press enter to continue ..."; read
348                 clear
349                 continue
350                 exit
351         fi
352         echo "Installation successfully complete."
353 done