Update INFO.yaml with new PTL
[demo.git] / vnfs / vCPE / scripts / v_brgemu_install.sh
1 #!/bin/bash
2
3 SCRIPT_VERSION=$(cat /opt/config/script_version.txt)
4 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
5
6
7 # Convert Network CIDR to Netmask
8 cdr2mask () {
9         # Number of args to shift, 255..255, first non-255 byte, zeroes
10         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
11         [ $1 -gt 1 ] && shift $1 || shift
12         echo ${1-0}.${2-0}.${3-0}.${4-0}
13 }
14
15 # OpenStack network configuration
16 if [[ $CLOUD_ENV == "openstack" ]]
17 then
18   echo 127.0.0.1 $(hostname) >> /etc/hosts
19
20   # Allow remote login as root
21   mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
22   cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
23 fi
24
25
26 # Auto-start configuration for the VPP
27 cat > /etc/vpp/startup.conf << EOF
28
29 unix {
30   nodaemon
31   log /tmp/vpp.log
32   full-coredump
33   cli-listen localhost:5002
34   startup-config /etc/vpp/setup.gate
35 }
36
37 api-trace {
38   on
39 }
40
41 api-segment {
42   gid vpp
43 }
44
45 cpu {
46         ## In the VPP there is one main thread and optionally the user can create worker(s)
47         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
48
49         ## Manual pinning of thread(s) to CPU core(s)
50
51         ## Set logical CPU core where main thread runs
52         # main-core 1
53
54         ## Set logical CPU core(s) where worker threads are running
55         # corelist-workers 2-3,18-19
56
57         ## Automatic pinning of thread(s) to CPU core(s)
58
59         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
60         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
61         ## The main thread is automatically pinned to the first available CPU core and worker(s)
62         ## are pinned to next free CPU core(s) after core assigned to main thread
63         # skip-cores 4
64
65         ## Specify a number of workers to be created
66         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
67         ## and main thread's CPU core
68         # workers 2
69
70         ## Set scheduling policy and priority of main and worker threads
71
72         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
73         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
74         # scheduler-policy fifo
75
76         ## Scheduling priority is used only for "real-time policies (fifo and rr),
77         ## and has to be in the range of priorities supported for a particular policy
78         # scheduler-priority 50
79 }
80
81 # dpdk {
82         ## Change default settings for all intefaces
83         # dev default {
84                 ## Number of receive queues, enables RSS
85                 ## Default is 1
86                 # num-rx-queues 3
87
88                 ## Number of transmit queues, Default is equal
89                 ## to number of worker threads or 1 if no workers treads
90                 # num-tx-queues 3
91
92                 ## Number of descriptors in transmit and receive rings
93                 ## increasing or reducing number can impact performance
94                 ## Default is 1024 for both rx and tx
95                 # num-rx-desc 512
96                 # num-tx-desc 512
97
98                 ## VLAN strip offload mode for interface
99                 ## Default is off
100                 # vlan-strip-offload on
101         # }
102
103         ## Whitelist specific interface by specifying PCI address
104         # dev 0000:02:00.0
105         ## Whitelist specific interface by specifying PCI address and in
106         ## addition specify custom parameters for this interface
107         # dev 0000:02:00.1 {
108         #       num-rx-queues 2
109         # }
110
111         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
112         ## and uio_pci_generic (default)
113         # uio-driver vfio-pci
114
115         ## Disable mutli-segment buffers, improves performance but
116         ## disables Jumbo MTU support
117         # no-multi-seg
118
119         ## Increase number of buffers allocated, needed only in scenarios with
120         ## large number of interfaces and worker threads. Value is per CPU socket.
121         ## Default is 16384
122         # num-mbufs 128000
123
124         ## Change hugepages allocation per-socket, needed only if there is need for
125         ## larger number of mbufs. Default is 256M on each detected CPU socket
126         # socket-mem 2048,2048
127 # }
128
129 EOF
130
131     #Get list of network device PCI bus addresses
132     get_nic_pci_list() {
133         while read -r line ; do
134             if [ "$line" != "${line#*network device}" ];
135             then
136                  echo -n "${line%% *} "
137             fi
138         done < <(lspci)
139     }
140
141     NICS=$(get_nic_pci_list)
142     NICS=`echo ${NICS} | sed 's/[0]\+\([0-9]\)/\1/g' | sed 's/[.:]/\//g'`
143
144     BRG_BNG_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 2`  # second interface in list
145     echo $BRG_BNG_NIC > /opt/config/brg_nic.txt
146
147
148     cat > /etc/vpp/setup.gate << EOF
149 set int state ${BRG_BNG_NIC} up
150 set dhcp client intfc ${BRG_BNG_NIC} hostname brg-emulator
151 EOF
152
153 echo "sdnc_ip: $(cat /opt/config/sdnc_ip.txt)" > /opt/config/ip.txt
154 echo "hcip: 192.168.4.20" >> /opt/config/ip.txt
155
156
157     cat > /opt/bind_nic.sh << 'EOF'
158 #!/bin/bash
159 while :
160 do
161         if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]];
162         then
163                 echo "vpp not running"
164         else
165                 break
166         fi
167 done
168
169
170 BRG_BNG_NIC=$(cat /opt/config/brg_nic.txt)
171 sdnc_ip=$(cat /opt/config/sdnc_ip.txt)
172
173 lstack_tap=$(vppctl tap connect lstack)
174 vppctl set int state $lstack_tap up
175
176 vppctl set interface l2 bridge $lstack_tap 10 0
177 vppctl set bridge-domain arp term 10
178
179 tap0_tap=$(vppctl tap connect tap0)
180 sleep 3
181 vppctl set int state $tap0_tap up
182 vppctl set int ip addr $tap0_tap 20.0.0.40/24
183 ifconfig tap0 192.168.4.20/24
184 route add -host $sdnc_ip tap0
185 route add -host 20.0.0.40 tap0
186 vppctl ip route add 192.168.4.0/24 via $tap0_tap
187 vppctl set interface snat in $tap0_tap out ${BRG_BNG_NIC}
188 vppctl snat add interface address ${BRG_BNG_NIC}
189
190 #Get HW addr of $tap0_tap
191 while read -r hw
192 do
193     if [[ "$hw" = $tap0_tap* ]];
194     then
195         read -r hw
196         hw_addr=${hw##* }
197         break
198     fi
199 done < <(vppctl show hardware)
200
201 arp -s $sdnc_ip $hw_addr
202 arp -s 20.0.0.40 $hw_addr
203
204 #Get HW addr of tap0
205 var=$(ifconfig tap0)
206 var=${var##*HWaddr}
207 var=${var%inet*}
208 tap0_addr=${var%inet*}
209
210 vppctl set ip arp $tap0_tap 192.168.4.20 $tap0_addr
211
212 EOF
213     chmod +x /opt/bind_nic.sh
214
215     #set nat rule
216     cat > /opt/set_nat.sh << 'EOF'
217 #! /bin/bash
218
219 while :
220 do
221     if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]];
222     then
223         #echo "vpp not running"
224         continue
225     fi
226
227     flag=0
228     while read -r line
229     do
230         if [ flag = 0 ];
231         then
232             re=${line#*/[0-9]/[0-9]}
233             if [ "$line" != "$re" ];
234             then
235                 flag=1
236             else
237                 flag=0
238                 continue
239             fi
240         else
241             ip=${line%/*}
242             if [[ $ip = *\.*\.*\.* ]];
243             then
244                 #echo "ip address is $ip"
245                 if [ ! -f /opt/config/ip.txt ];
246                 then
247                     echo "file /opt/config/ip.txt doesn't exists"
248                     continue
249                 fi
250                 while read -r tap_ip
251                 do
252                     if [[ $tap_ip = hcip* ]];
253                     then
254                         tap_ip=${tap_ip#*" "}
255                         echo "hc tap ip address is $tap_ip"
256                         vppctl snat add static mapping tcp local $tap_ip 8183 external $ip 8183
257                         exit 0
258                     fi
259                 done < /opt/config/ip.txt
260             else
261                 if [[ ! $ip = */[0-9] ]];
262                 then
263                     flag=0
264                     #echo "not correct"
265                 fi
266             fi
267         fi
268     done < <(vppctl show int addr)
269     sleep 1
270 done
271 EOF
272
273     chmod +x /opt/set_nat.sh
274
275     #Create script to run bind_nic.sh and set_nat.sh
276     cat > /opt/nat_service.sh << 'EOF'
277 #! /bin/bash
278
279 sleep 15
280 /opt/bind_nic.sh
281 /opt/set_nat.sh
282
283 EOF
284     chmod +x /opt/nat_service.sh
285
286
287     # Create systemctl service for Honeycomb
288     cat > /etc/systemd/system/honeycomb.service << EOF
289 [Unit]
290 Description=Honeycomb Agent for the VPP control plane
291 Documentation=https://wiki.fd.io/view/Honeycomb
292 Requires=vpp.service
293 After=vpp.service
294
295 [Service]
296 ExecStart=/opt/honeycomb/honeycomb
297 Restart=always
298 RestartSec=10
299
300 [Install]
301 WantedBy=multi-user.target
302 EOF
303     systemctl enable /etc/systemd/system/honeycomb.service
304
305
306     # Create systemctl service for nat script
307     cat > /etc/systemd/system/nat.service << EOF
308 [Unit]
309 Description=Runs vbrg scripts for nat configuration
310 Requires=vpp.service
311 After=vpp.service
312
313 [Service]
314 RemainAfterExit=True
315 ExecStart=/opt/nat_service.sh
316 Restart=always
317
318 [Install]
319 WantedBy=multi-user.target
320 EOF
321     systemctl enable /etc/systemd/system/nat.service
322
323     # Download DHCP config files
324     cd /opt
325     unzip -p -j /opt/vcpe-scripts-$SCRIPT_VERSION.zip v_brgemu_init.sh > /opt/v_brgemu_init.sh
326     unzip -p -j /opt/vcpe-scripts-$SCRIPT_VERSION.zip v_brgemu.sh > /opt/v_brgemu.sh
327     sed -i '/# Provides:/c\# Provides:     vbrg ' /opt/v_brgemu.sh
328     chmod +x v_brgemu_init.sh
329     chmod +x v_brgemu.sh
330     mv v_brgemu.sh /etc/init.d
331     update-rc.d v_brgemu.sh defaults
332
333     # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
334     if [[ $CLOUD_ENV != "rackspace" ]]
335     then
336         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
337         grub-mkconfig -o /boot/grub/grub.cfg
338         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
339         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
340         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
341         reboot
342     fi
343
344     ./v_brgemu_init.sh
345
346 # Indicate script has finished executing
347     echo 'Execution of vBRG install script completed' > /opt/script_status.txt