c86e9711afd30ecc7a4c841f6c665bd174bf3e0d
[demo.git] / vnfs / vCPE / scripts / v_brgemu_install.sh
1 #!/bin/bash
2
3 REPO_URL_ARTIFACTS=$(cat /opt/config/repo_url_artifacts.txt)
4 DEMO_ARTIFACTS_VERSION=$(cat /opt/config/demo_artifacts_version.txt)
5 INSTALL_SCRIPT_VERSION=$(cat /opt/config/install_script_version.txt)
6 VPP_SOURCE_REPO_URL=$(cat /opt/config/vpp_source_repo_url.txt)
7 VPP_SOURCE_REPO_RELEASE_TAG=$(cat /opt/config/vpp_source_repo_release_tag.txt)
8 VPP_PATCH_URL=$(cat /opt/config/vpp_patch_url.txt)
9 HC2VPP_SOURCE_REPO_URL=$(cat /opt/config/hc2vpp_source_repo_url.txt)
10 HC2VPP_SOURCE_REPO_RELEASE_TAG=$(cat /opt/config/hc2vpp_source_repo_release_tag.txt)
11 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
12
13 # Build states are:
14 # 'build' - just build the code
15 # 'done' - code is build, install and setup
16 # 'auto' - bulid, install and setup
17 BUILD_STATE="auto"
18
19 if [[ -f /opt/config/compile_state.txt ]]
20 then
21     BUILD_STATE=$(cat /opt/config/compile_state.txt)
22 fi
23
24 # Convert Network CIDR to Netmask
25 cdr2mask () {
26         # Number of args to shift, 255..255, first non-255 byte, zeroes
27         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
28         [ $1 -gt 1 ] && shift $1 || shift
29         echo ${1-0}.${2-0}.${3-0}.${4-0}
30 }
31
32 # OpenStack network configuration
33 if [[ $BUILD_STATE != "build" ]]
34 then
35     if [[ $CLOUD_ENV == "openstack" ]]
36     then
37         echo 127.0.0.1 $(hostname) >> /etc/hosts
38
39         # Allow remote login as root
40         mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
41         cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
42     fi
43 fi  # endif BUILD_STATE != "build"
44
45 if [[ $BUILD_STATE != "done" ]]
46 then
47     # Download required dependencies
48     echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
49     echo "deb-src http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
50     apt-get update
51     apt-get install --allow-unauthenticated -y wget openjdk-8-jdk apt-transport-https ca-certificates g++ libcurl4-gnutls-dev
52     sleep 1
53
54     # Install the tools required for download codes
55     apt-get install -y expect git patch make linux-image-extra-`uname -r`
56     #Download and build the VPP codes
57     cd /opt
58     git clone ${VPP_SOURCE_REPO_URL} -b ${VPP_SOURCE_REPO_RELEASE_TAG} vpp
59     wget -O VPP-Add-Option82-Nat-Filter-For-vBRG.patch ${VPP_PATCH_URL}
60
61     cd vpp
62     patch -p1 < ../VPP-Add-Option82-Nat-Filter-For-vBRG.patch
63     expect -c "
64             set timeout 60;
65             spawn make install-dep;
66             expect {
67                     \"Do you want to continue?*\" {send \"Y\r\"; interact}
68             }
69     "
70
71     cd build-root
72     ./bootstrap.sh
73     make V=0 PLATFORM=vpp TAG=vpp install-deb
74
75     # Install the VPP package
76     dpkg -i *.deb
77     systemctl stop vpp
78 fi  # endif BUILD_STATE != "done"
79
80 if [[ $BUILD_STATE != "build" ]]
81 then
82     # Auto-start configuration for the VPP
83     cat > /etc/vpp/startup.conf << EOF
84
85 unix {
86   nodaemon
87   log /tmp/vpp.log
88   full-coredump
89   cli-listen localhost:5002
90   startup-config /etc/vpp/setup.gate
91 }
92
93 api-trace {
94   on
95 }
96
97 api-segment {
98   gid vpp
99 }
100
101 cpu {
102         ## In the VPP there is one main thread and optionally the user can create worker(s)
103         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
104
105         ## Manual pinning of thread(s) to CPU core(s)
106
107         ## Set logical CPU core where main thread runs
108         # main-core 1
109
110         ## Set logical CPU core(s) where worker threads are running
111         # corelist-workers 2-3,18-19
112
113         ## Automatic pinning of thread(s) to CPU core(s)
114
115         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
116         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
117         ## The main thread is automatically pinned to the first available CPU core and worker(s)
118         ## are pinned to next free CPU core(s) after core assigned to main thread
119         # skip-cores 4
120
121         ## Specify a number of workers to be created
122         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
123         ## and main thread's CPU core
124         # workers 2
125
126         ## Set scheduling policy and priority of main and worker threads
127
128         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
129         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
130         # scheduler-policy fifo
131
132         ## Scheduling priority is used only for "real-time policies (fifo and rr),
133         ## and has to be in the range of priorities supported for a particular policy
134         # scheduler-priority 50
135 }
136
137 # dpdk {
138         ## Change default settings for all intefaces
139         # dev default {
140                 ## Number of receive queues, enables RSS
141                 ## Default is 1
142                 # num-rx-queues 3
143
144                 ## Number of transmit queues, Default is equal
145                 ## to number of worker threads or 1 if no workers treads
146                 # num-tx-queues 3
147
148                 ## Number of descriptors in transmit and receive rings
149                 ## increasing or reducing number can impact performance
150                 ## Default is 1024 for both rx and tx
151                 # num-rx-desc 512
152                 # num-tx-desc 512
153
154                 ## VLAN strip offload mode for interface
155                 ## Default is off
156                 # vlan-strip-offload on
157         # }
158
159         ## Whitelist specific interface by specifying PCI address
160         # dev 0000:02:00.0
161         ## Whitelist specific interface by specifying PCI address and in
162         ## addition specify custom parameters for this interface
163         # dev 0000:02:00.1 {
164         #       num-rx-queues 2
165         # }
166
167         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
168         ## and uio_pci_generic (default)
169         # uio-driver vfio-pci
170
171         ## Disable mutli-segment buffers, improves performance but
172         ## disables Jumbo MTU support
173         # no-multi-seg
174
175         ## Increase number of buffers allocated, needed only in scenarios with
176         ## large number of interfaces and worker threads. Value is per CPU socket.
177         ## Default is 16384
178         # num-mbufs 128000
179
180         ## Change hugepages allocation per-socket, needed only if there is need for
181         ## larger number of mbufs. Default is 256M on each detected CPU socket
182         # socket-mem 2048,2048
183 # }
184
185 EOF
186
187     #Get list of network device PCI bus addresses
188     get_nic_pci_list() {
189         while read -r line ; do
190             if [ "$line" != "${line#*network device}" ];
191             then
192                  echo -n "${line%% *} "
193             fi
194         done < <(lspci)
195     }
196
197     NICS=$(get_nic_pci_list)
198     NICS=`echo ${NICS} | sed 's/[0]\+\([0-9]\)/\1/g' | sed 's/[.:]/\//g'`
199
200     BRG_BNG_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 2`  # second interface in list
201     echo $BRG_BNG_NIC > /opt/config/brg_nic.txt
202
203
204     cat > /etc/vpp/setup.gate << EOF
205 set int state ${BRG_BNG_NIC} up
206 set dhcp client intfc ${BRG_BNG_NIC} hostname brg-emulator
207 EOF
208
209 echo "sdnc_ip: $(cat /opt/config/sdnc_ip.txt)" > /opt/config/ip.txt
210 echo "hcip: 192.168.4.20" >> /opt/config/ip.txt
211
212
213     cat > /opt/bind_nic.sh << 'EOF'
214 #!/bin/bash
215 while :
216 do
217         if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]];
218         then
219                 echo "vpp not running"
220         else
221                 break
222         fi
223 done
224
225
226 BRG_BNG_NIC=$(cat /opt/config/brg_nic.txt)
227 sdnc_ip=$(cat /opt/config/sdnc_ip.txt)
228
229 lstack_tap=$(vppctl tap connect lstack)
230 vppctl set int state $lstack_tap up
231
232 vppctl set interface l2 bridge $lstack_tap 10 0
233 vppctl set bridge-domain arp term 10
234
235 tap0_tap=$(vppctl tap connect tap0)
236 sleep 3
237 vppctl set int state $tap0_tap up
238 vppctl set int ip addr $tap0_tap 20.0.0.40/24
239 ifconfig tap0 192.168.4.20/24
240 route add -host $sdnc_ip tap0
241 route add -host 20.0.0.40 tap0
242 vppctl ip route add 192.168.4.0/24 via $tap0_tap
243 vppctl set interface snat in $tap0_tap out ${BRG_BNG_NIC}
244 vppctl snat add interface address ${BRG_BNG_NIC}
245
246 #Get HW addr of $tap0_tap
247 while read -r hw
248 do
249     if [[ "$hw" = $tap0_tap* ]];
250     then
251         read -r hw
252         hw_addr=${hw##* }
253         break
254     fi
255 done < <(vppctl show hardware)
256
257 arp -s $sdnc_ip $hw_addr
258 arp -s 20.0.0.40 $hw_addr
259
260 #Get HW addr of tap0
261 var=$(ifconfig tap0)
262 var=${var##*HWaddr}
263 var=${var%inet*}
264 tap0_addr=${var%inet*}
265
266 vppctl set ip arp $tap0_tap 192.168.4.20 $tap0_addr
267
268 EOF
269     chmod +x /opt/bind_nic.sh
270
271     #set nat rule
272     cat > /opt/set_nat.sh << 'EOF'
273 #! /bin/bash
274
275 while :
276 do
277     if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]];
278     then
279         #echo "vpp not running"
280         continue
281     fi
282
283     flag=0
284     while read -r line
285     do
286         if [ flag = 0 ];
287         then
288             re=${line#*/[0-9]/[0-9]}
289             if [ "$line" != "$re" ];
290             then
291                 flag=1
292             else
293                 flag=0
294                 continue
295             fi
296         else
297             ip=${line%/*}
298             if [[ $ip = *\.*\.*\.* ]];
299             then
300                 #echo "ip address is $ip"
301                 if [ ! -f /opt/config/ip.txt ];
302                 then
303                     echo "file /opt/config/ip.txt doesn't exists"
304                     continue
305                 fi
306                 while read -r tap_ip
307                 do
308                     if [[ $tap_ip = hcip* ]];
309                     then
310                         tap_ip=${tap_ip#*" "}
311                         echo "hc tap ip address is $tap_ip"
312                         vppctl snat add static mapping tcp local $tap_ip 8183 external $ip 8183
313                         exit 0
314                     fi
315                 done < /opt/config/ip.txt
316             else
317                 if [[ ! $ip = */[0-9] ]];
318                 then
319                     flag=0
320                     #echo "not correct"
321                 fi
322             fi
323         fi
324     done < <(vppctl show int addr)
325     sleep 1
326 done
327 EOF
328
329     chmod +x /opt/set_nat.sh
330
331     #Create script to run bind_nic.sh and set_nat.sh
332     cat > /opt/nat_service.sh << 'EOF'
333 #! /bin/bash
334
335 sleep 15
336 /opt/bind_nic.sh
337 /opt/set_nat.sh
338
339 EOF
340     chmod +x /opt/nat_service.sh
341
342 fi  # endif BUILD_STATE != "build"
343
344 if [[ $BUILD_STATE != "done" ]]
345 then
346
347     # Download and install HC2VPP from source
348     cd /opt
349     git clone ${HC2VPP_SOURCE_REPO_URL} -b ${HC2VPP_SOURCE_REPO_RELEASE_TAG} hc2vpp
350
351     apt-get -f -y install
352     apt-get install -y maven
353     mkdir -p /root/.m2
354     cat > ~/.m2/settings.xml << EOF
355 <?xml version="1.0" encoding="UTF-8"?>
356 <!-- vi: set et smarttab sw=2 tabstop=2: -->
357 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
358   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
359   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
360
361   <profiles>
362     <profile>
363    <id>fd.io-release</id>
364       <repositories>
365         <repository>
366           <id>fd.io-mirror</id>
367           <name>fd.io-mirror</name>
368           <url>https://nexus.fd.io/content/groups/public/</url>
369           <releases>
370             <enabled>true</enabled>
371             <updatePolicy>never</updatePolicy>
372           </releases>
373           <snapshots>
374             <enabled>false</enabled>
375           </snapshots>
376         </repository>
377       </repositories>
378       <pluginRepositories>
379         <pluginRepository>
380           <id>fd.io-mirror</id>
381           <name>fd.io-mirror</name>
382           <url>https://nexus.fd.io/content/repositories/public/</url>
383           <releases>
384             <enabled>true</enabled>
385             <updatePolicy>never</updatePolicy>
386           </releases>
387           <snapshots>
388             <enabled>false</enabled>
389           </snapshots>
390         </pluginRepository>
391       </pluginRepositories>
392     </profile>
393
394     <profile>
395       <id>fd.io-snapshots</id>
396       <repositories>
397         <repository>
398           <id>fd.io-snapshot</id>
399           <name>fd.io-snapshot</name>
400           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
401           <releases>
402             <enabled>false</enabled>
403           </releases>
404           <snapshots>
405             <enabled>true</enabled>
406           </snapshots>
407         </repository>
408       </repositories>
409       <pluginRepositories>
410         <pluginRepository>
411             <id>fd.io-snapshot</id>
412           <name>fd.io-snapshot</name>
413           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
414           <releases>
415             <enabled>false</enabled>
416           </releases>
417           <snapshots>
418             <enabled>true</enabled>
419           </snapshots>
420         </pluginRepository>
421       </pluginRepositories>
422     </profile>
423     <profile>
424       <id>opendaylight-snapshots</id>
425       <repositories>
426         <repository>
427           <id>opendaylight-snapshot</id>
428           <name>opendaylight-snapshot</name>
429           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
430           <releases>
431             <enabled>false</enabled>
432           </releases>
433           <snapshots>
434             <enabled>true</enabled>
435           </snapshots>
436         </repository>
437       </repositories>
438       <pluginRepositories>
439         <pluginRepository>
440           <id>opendaylight-shapshot</id>
441           <name>opendaylight-snapshot</name>
442           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
443           <releases>
444             <enabled>false</enabled>
445           </releases>
446           <snapshots>
447             <enabled>true</enabled>
448           </snapshots>
449         </pluginRepository>
450       </pluginRepositories>
451     </profile>
452   </profiles>
453
454   <activeProfiles>
455     <activeProfile>fd.io-release</activeProfile>
456     <activeProfile>fd.io-snapshots</activeProfile>
457     <activeProfile>opendaylight-snapshots</activeProfile>
458   </activeProfiles>
459 </settings>
460 EOF
461
462     cd hc2vpp
463     mvn clean install
464     l_version=$(cat pom.xml | grep "<version>" | head -1)
465     l_version=$(echo "${l_version%<*}")
466     l_version=$(echo "${l_version#*>}")
467     mv vpp-integration/minimal-distribution/target/vpp-integration-distribution-${l_version}-hc/vpp-integration-distribution-${l_version} /opt/honeycomb
468     sed -i 's/127.0.0.1/0.0.0.0/g' /opt/honeycomb/config/honeycomb.json
469
470     # Disable automatic upgrades
471     if [[ $CLOUD_ENV != "rackspace" ]]
472     then
473         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
474         sed -i 's/\(APT::Periodic::Unattended-Upgrade\) "1"/\1 "0"/' /etc/apt/apt.conf.d/20auto-upgrades
475     fi
476 fi  # endif BUILD_STATE != "done"
477
478 if [[ $BUILD_STATE != "build" ]]
479 then
480     # Create systemctl service for Honeycomb
481     cat > /etc/systemd/system/honeycomb.service << EOF
482 [Unit]
483 Description=Honeycomb Agent for the VPP control plane
484 Documentation=https://wiki.fd.io/view/Honeycomb
485 Requires=vpp.service
486 After=vpp.service
487
488 [Service]
489 ExecStart=/opt/honeycomb/honeycomb
490 Restart=always
491 RestartSec=10
492
493 [Install]
494 WantedBy=multi-user.target
495 EOF
496     systemctl enable /etc/systemd/system/honeycomb.service
497
498
499     # Create systemctl service for nat script
500     cat > /etc/systemd/system/nat.service << EOF
501 [Unit]
502 Description=Runs vbrg scripts for nat configuration
503 Requires=vpp.service
504 After=vpp.service
505
506 [Service]
507 RemainAfterExit=True
508 ExecStart=/opt/nat_service.sh
509 Restart=always
510
511 [Install]
512 WantedBy=multi-user.target
513 EOF
514     systemctl enable /etc/systemd/system/nat.service
515
516     # Download DHCP config files
517     cd /opt
518     unzip -p -j /opt/vcpe-scripts-$INSTALL_SCRIPT_VERSION.zip v_brgemu_init.sh > /opt/v_brgemu_init.sh
519     unzip -p -j /opt/vcpe-scripts-$INSTALL_SCRIPT_VERSION.zip v_brgemu.sh > /opt/v_brgemu.sh
520     sed -i '/# Provides:/c\# Provides:     vbrg ' /opt/v_brgemu.sh
521     chmod +x v_brgemu_init.sh
522     chmod +x v_brgemu.sh
523     mv v_brgemu.sh /etc/init.d
524     update-rc.d v_brgemu.sh defaults
525
526     # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
527     if [[ $CLOUD_ENV != "rackspace" ]]
528     then
529         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
530         grub-mkconfig -o /boot/grub/grub.cfg
531         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
532         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
533         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
534         reboot
535     fi
536
537     ./v_brgemu_init.sh
538 fi  # endif BUILD_STATE != "build"