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