3b70166cb443cea37ed3080af2d7e1d40b802a7b
[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="Not initialized"
28 CIVETWEB_DOWNLOAD="Not initialized"
29 DPDK_DIR=$VNF_CORE/dpdk
30 DPDK_RTE_VER="17.02"
31
32 #
33 # Sets QUIT variable so script will finish.
34 #
35 quit()
36 {
37         QUIT=$1
38 }
39
40 # Shortcut for quit.
41 q()
42 {
43         quit
44 }
45
46 setup_http_proxy()
47 {
48         while true; do
49                 echo
50                 read -p "Enter Proxy : " proxy
51                 export http_proxy=$proxy
52                 export https_proxy=$proxy
53                 echo "Acquire::http::proxy \"$http_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
54                 echo "Acquire::https::proxy \"$http_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
55
56                 wget -T 20 -t 3 --spider http://www.google.com > /dev/null 2>&1
57                 if [ "$?" != 0 ]; then
58                         echo -e "No Internet connection. Proxy incorrect? Try again"
59                         echo -e "eg: http://<proxy>:<port>"
60                         exit 1
61                 fi
62         return
63         done
64         echo "Network connectivity successful."
65 }
66
67 step_1()
68 {
69         TITLE="Environment setup."
70         CONFIG_NUM=1
71         TEXT[1]="Check OS and network connection"
72         FUNC[1]="setup_env"
73         TEXT[2]="Select DPDK RTE version"
74         FUNC[2]="select_dpdk_rte_ver"
75 }
76 setup_env()
77 {
78         # a. Check for OS dependencies
79         source /etc/os-release
80         if [[ $VERSION_ID != "16.04" ]] ; then
81                 echo "WARNING: It is recommended to use Ubuntu 16.04..Your version is "$VERSION_ID
82         else
83                 echo "Ubuntu 16.04 OS requirement met..."
84         fi
85         echo
86         echo "Checking network connectivity..."
87         # b. Check for internet connections
88         wget -T 20 -t 3 --spider http://www.google.com > /dev/null 2>&1
89         if [ "$?" != 0 ]; then
90                 while true; do
91                         read -p "No Internet connection. Are you behind a proxy (y/n)? " yn
92                         case $yn in
93                                 [Yy]* ) $SETUP_PROXY ; return;;
94                                 [Nn]* ) echo "Please check your internet connection..." ; exit;;
95                                 * ) "Please answer yes or no.";;
96                         esac
97                 done
98         fi
99         echo "Network connectivity successful."
100 }
101 select_dpdk_rte_ver()
102 {
103
104         TITLE="Select the DPDK RTE version"
105         CONFIG_NUM=1
106         echo "[1] DPDK 16.04"
107         echo "[2] DPDK 16.11"
108         echo "[3] DPDK 17.02"
109         echo "[4] DPDK 17.05"
110         echo
111
112         while true; do
113                 read -p "Select DPDK version to be used: " yn
114                         case $yn in
115                                 [1]* ) DPDK_RTE_VER=16.04 ; return;;
116                                 [2]* ) DPDK_RTE_VER=16.11 ; return;;
117                                 [3]* ) DPDK_RTE_VER=17.02 ; return;;
118                                 [4]* ) DPDK_RTE_VER=17.05 ; return;;
119                                 * ) echo " Invalid selection...";;
120                         esac
121         done
122 }
123
124 step_2()
125 {
126         TITLE="Download and Install"
127         CONFIG_NUM=1
128         TEXT[1]="Agree to download"
129         FUNC[1]="get_agreement_download"
130         TEXT[2]="Download packages"
131         FUNC[2]="install_libs"
132         TEXT[3]="Download DPDK zip"
133         FUNC[3]="download_dpdk_zip"
134         TEXT[4]="Build and Install DPDK"
135         FUNC[4]="install_dpdk"
136         TEXT[5]="Setup hugepages"
137         FUNC[5]="setup_hugepages"
138         TEXT[6]="Download civetweb"
139         FUNC[6]="download_civetweb_zip"
140 }
141 get_agreement_download()
142 {
143         echo
144         echo "List of packages needed for VNFs build and installation:"
145         echo "-------------------------------------------------------"
146         echo "1. DPDK version $DPDK_RTE_VER"
147         echo "2. build-essential"
148         echo "3. linux-headers-generic"
149         echo "4. git"
150         echo "5. unzip"
151         echo "6. libpcap-dev"
152         echo "7. make"
153         echo "8. and other library dependencies"
154         while true; do
155                 read -p "We need download above mentioned package. Press (y/n) to continue? " yn
156                 case $yn in
157                         [Yy]* )
158                                 touch .agree
159                                 return;;
160                         [Nn]* ) exit;;
161                         * ) "Please answer yes or no.";;
162                 esac
163         done
164 }
165
166 install_libs()
167 {
168         echo "Install libs needed to build and run VNFs..."
169         file_name=".agree"
170         if [ ! -e "$file_name" ]; then
171                 echo "Please choose option '2.Agree to download' first"
172                 return
173         fi
174         file_name=".download"
175         if [ -e "$file_name" ]; then
176                 clear
177                 return
178         fi
179         sudo apt-get update
180         sudo apt-get -y install build-essential linux-headers-$(uname -r) git unzip libpcap0.8-dev gcc \
181                 make libc6 libc6-dev g++-multilib libzmq3-dev libcurl4-openssl-dev net-tools wget gcc unzip \
182                 libpcap-dev libncurses-dev libedit-dev pciutils liblua5.2-dev libncursesw5-dev libjson0 \
183                 libjson0-dev libssl-dev
184         touch .download
185 }
186
187 download_dpdk_zip()
188 {
189         echo "Download DPDK zip"
190         file_name=".agree"
191         if [ ! -e "$file_name" ]; then
192                 echo "Please choose option '2.Agree to download' first"
193                 return
194         fi
195         DPDK_DOWNLOAD="http://dpdk.org/browse/dpdk/snapshot/dpdk-$DPDK_RTE_VER.zip"
196         rm -rf $DPDK_DIR
197         if [ ! -e ${DPDK_DOWNLOAD##*/} ] ; then
198                 wget ${DPDK_DOWNLOAD}
199         fi
200         unzip -o ${DPDK_DOWNLOAD##*/}
201         mv $VNF_CORE/dpdk-$DPDK_RTE_VER $VNF_CORE/dpdk
202 }
203
204 download_civetweb_zip()
205 {
206         echo "Download CIVETWEB zip"
207         CIVETWEB_DOWNLOAD="https://sourceforge.net/projects/civetweb/files/1.9/CivetWeb_V1.9.zip"
208         if [ ! -e ${CIVETWEB_DOWNLOAD##*/} ] ; then
209                 wget ${CIVETWEB_DOWNLOAD}
210         fi
211         unzip -o ${CIVETWEB_DOWNLOAD##*/}
212         mv $VNF_CORE/civetweb-master $VNF_CORE/civetweb
213         pushd $VNF_CORE/civetweb
214         make lib
215         popd
216 }
217
218 install_dpdk()
219 {
220         echo "Build DPDK"
221
222         if [ ! -d "$DPDK_DIR" ]; then
223      echo "Please choose option '4 Download DPDK zip'"
224      return
225         fi
226
227         export RTE_TARGET=x86_64-native-linuxapp-gcc
228
229         pushd $DPDK_DIR
230
231         if [ $DPDK_RTE_VER == "16.04" ] ; then
232                 echo "Apply dpdk custom patches..."
233                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-link-management.patch
234                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-Rx-hang-when-disable-LLDP.patch
235                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-link-status-change-interrupt.patch
236                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/i40e-fix-VF-bonded-device-link-down.patch
237                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/disable-acl-debug-logs.patch
238                         patch -p1 < $VNF_CORE/patches/dpdk_custom_patch/set-log-level-to-info.patch
239         fi
240
241         make -j16 install T=$RTE_TARGET
242         if [ $? -ne 0 ] ; then
243                 echo "Failed to build dpdk, please check the errors."
244                 return
245         fi
246         sudo modinfo igb_uio
247         if [ $? -ne 0 ] ; then
248                 sudo $MODPROBE -v uio
249                 sudo $INSMOD $RTE_TARGET/kmod/igb_uio.ko
250                 sudo cp -f $RTE_TARGET/kmod/igb_uio.ko /lib/modules/$(uname -r)
251                 echo "uio" | sudo tee -a /etc/modules
252                 echo "igb_uio" | sudo tee -a /etc/modules
253                 sudo depmod
254         fi
255         popd
256 }
257
258 setup_hugepages()
259 {
260         #----
261         Pages=16
262         if [[ "$HUGEPGSZ" = "2048kB" ]] ; then
263                 Pages=8192
264         fi
265         if [ ! "`grep nr_hugepages /etc/sysctl.conf`" ] ; then
266                 echo "vm.nr_hugepages=$Pages" | sudo tee /etc/sysctl.conf
267         fi
268         sudo sysctl -p
269
270         sudo service procps start
271
272         grep -s '/dev/hugepages' /proc/mounts
273         if [ $? -ne 0 ] ; then
274                 echo "Creating /mnt/huge and mounting as hugetlbfs"
275                 sudo mkdir -p /mnt/huge
276                 sudo mount -t hugetlbfs nodev /mnt/huge
277                 echo "nodev /mnt/huge hugetlbfs defaults 0 0" | sudo tee -a /etc/fstab > /dev/null
278         fi
279 }
280
281 step_3()
282 {
283         TITLE="Build VNFs"
284         CONFIG_NUM=1
285                                 TEXT[1]="Build all VNFs (vACL, vCGNAPT, vFW, UDP_Replay)"
286         FUNC[1]="build_vnfs"
287 }
288
289 build_vnfs()
290 {
291
292         if [ ! -d "$DPDK_DIR" ]; then
293      echo "Please choose option '4 Download DPDK zip'"
294      return
295         fi
296
297         if [ ! -d "$DPDK_DIR/x86_64-native-linuxapp-gcc" ]; then
298      echo "Please choose option '5 Build and Install DPDK'"
299      return
300         fi
301
302         export RTE_SDK=$DPDK_DIR
303         export RTE_TARGET=x86_64-native-linuxapp-gcc
304         pushd $VNF_CORE
305         make clean
306         make || { echo -e "\nVNF: Make failed\n"; }
307         popd
308 }
309
310 non_interactive()
311 {
312   #--- Add non intractive option to build vnfs
313   if [[ "$1" = "true" ]];then
314                   DPDK_VER=("" "16.04" "16.11" "17.02" "17.05")
315                   member="$2"
316                   for item in "${DPDK_VER[@]}"; do
317                            if [[ "$member" == "$item" ]]; then
318                       DPDK_RTE_VER="$member"
319                     fi
320     done
321     pushd $VNF_CORE
322
323     echo "Install required libraries..."
324     touch .agree
325     install_libs
326
327     echo "Download dpdk for VNF build..."
328     download_dpdk_zip
329
330     echo "Download civetweb for VNF build..."
331     download_civetweb_zip
332
333     echo "Build dpdk..."
334     install_dpdk
335
336     echo "Setup hugepages..."
337     setup_hugepages
338
339     echo "build VNFS..."
340     build_vnfs
341
342     popd
343     exit
344   fi
345 }
346
347 interactive()
348 {
349   SETUP_PROXY="setup_http_proxy"
350   STEPS[1]="step_1"
351   STEPS[2]="step_2"
352   STEPS[3]="step_3"
353
354   QUIT=0
355
356   while [ "$QUIT" == "0" ]; do
357     OPTION_NUM=1
358     for s in $(seq ${#STEPS[@]}) ; do
359       ${STEPS[s]}
360
361       echo "----------------------------------------------------------"
362       echo " Step $s: ${TITLE}"
363       echo "----------------------------------------------------------"
364
365       for i in $(seq ${#TEXT[@]}) ; do
366               echo "[$OPTION_NUM] ${TEXT[i]}"
367               OPTIONS[$OPTION_NUM]=${FUNC[i]}
368               let "OPTION_NUM+=1"
369       done
370
371       # Clear TEXT and FUNC arrays before next step
372       unset TEXT
373       unset FUNC
374
375       echo ""
376     done
377
378     echo "[$OPTION_NUM] Exit Script"
379     OPTIONS[$OPTION_NUM]="quit"
380     echo ""
381     echo -n "Option: "
382     read our_entry
383     echo ""
384     ${OPTIONS[our_entry]} ${our_entry}
385
386     if [ "$QUIT" == "0" ] ; then
387       echo
388       echo -n "Press enter to continue ..."; read
389       clear
390       continue
391       exit
392     fi
393     echo "Installation successfully complete."
394   done
395 }
396
397 # -- main script
398 clear
399 sudo -n true
400 if [ $? -ne 0 ]; then
401   echo -n "Checking for user permission.. "
402   echo "Password-less sudo user must run this script" 1>&2
403   exit 1
404 fi
405
406 NON_INTERACTIVE=false
407 INTERACTIVE=true
408 DPDK_VERSION=$DPDK_RTE_VER
409
410 for i in "$@"
411 do
412 case $i in
413                 -s|--silient)
414                 NON_INTERACTIVE=true
415   INTERACTIVE=false
416                 ;;
417                 -i|--interactive)
418                 INTERACTIVE=true
419                 ;;
420                 -p=*|--proxy=*)
421                 export http_proxy="${i#*=}"
422                 export https_proxy="${i#*=}"
423                 ;;
424                 -d=*|--dpdk=*)
425                 DPDK_VERSION="${i#*=}"
426                 ;;
427                 -h|--help)
428                 echo "CommandLine options:"
429                 echo "===================="
430                 echo "1. Intractive mode:"
431                 echo "./tools/vnf_build.sh or ./tools/vnf_build.sh -i"
432                 echo
433                 echo "1. Non-Intractive mode:"
434                 echo "./tools/vnf_build.sh -s [Default dpdk 17.02]"
435                 echo "If system is behind proxy use -p=<proxy> and to use different dpdk version use -d=<dpdk>"
436                 echo "eg: ./tools/vnf_build.sh -s -p=http://proxy.com -d=17.05"
437                 echo 'Note:- supported dpdk version ("16.04" "16.11" "17.02" "17.05")'
438                 echo
439                 exit
440                 ;;
441                 --default)
442                 INTERACTIVE=true
443                 ;;
444                 *)
445   ;;
446 esac
447 done
448
449 if [[ "$INTERACTIVE" == "true" ]]; then
450                         interactive
451                         exit
452 fi
453
454 if [[ "$NON_INTERACTIVE" == "true" ]]; then
455                         non_interactive $NON_INTERACTIVE $DPDK_VERSION
456                         exit
457 fi