c9162aace3c03db0e81ae97f3d46aa3ef0e80022
[demo.git] / vnfs / vCPE / scripts / v_gmux_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 MUX_GW_IP=$(cat /opt/config/mux_gw_net_ipaddr.txt)
6 MUX_GW_CIDR=$(cat /opt/config/mux_gw_net_cidr.txt)
7 MUX_TO_BNG_IP=$(cat /opt/config/mux_to_bng_net_ipaddr.txt)
8 BNG_MUX_CIDR=$(cat /opt/config/bng_mux_net_cidr.txt)
9 BRG_BNG_CIDR=$(cat /opt/config/brg_bng_net_cidr.txt)
10 BNG_TO_MUX_IP=$(cat /opt/config/bng_to_mux_net_ipaddr.txt)
11
12 # Convert Network CIDR to Netmask
13 cdr2mask () {
14         # Number of args to shift, 255..255, first non-255 byte, zeroes
15         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
16         [ $1 -gt 1 ] && shift $1 || shift
17         echo ${1-0}.${2-0}.${3-0}.${4-0}
18 }
19
20 # OpenStack network configuration
21 if [[ $CLOUD_ENV == "openstack" ]]
22 then
23     echo 127.0.0.1 $(hostname) >> /etc/hosts
24
25     # Allow remote login as root
26     mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
27     cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
28
29     MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
30
31     IP=$(cat /opt/config/oam_ipaddr.txt)
32     BITS=$(cat /opt/config/oam_cidr.txt | cut -d"/" -f2)
33     NETMASK=$(cdr2mask $BITS)
34     echo "auto eth2" >> /etc/network/interfaces
35     echo "iface eth2 inet static" >> /etc/network/interfaces
36     echo "    address $IP" >> /etc/network/interfaces
37     echo "    netmask $NETMASK" >> /etc/network/interfaces
38     echo "    mtu $MTU" >> /etc/network/interfaces
39
40     ifup eth2
41 fi
42
43 # Auto-start configuration for the VPP
44 cat > /etc/vpp/startup.conf << EOF
45
46 unix {
47   nodaemon
48   log /tmp/vpp.log
49   full-coredump
50   cli-listen localhost:5002
51   startup-config /etc/vpp/setup.gate
52 }
53
54 api-trace {
55   on
56 }
57
58 api-segment {
59   gid vpp
60 }
61
62 cpu {
63         ## In the VPP there is one main thread and optionally the user can create worker(s)
64         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
65
66         ## Manual pinning of thread(s) to CPU core(s)
67
68         ## Set logical CPU core where main thread runs
69         # main-core 1
70
71         ## Set logical CPU core(s) where worker threads are running
72         # corelist-workers 2-3,18-19
73
74         ## Automatic pinning of thread(s) to CPU core(s)
75
76         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
77         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
78         ## The main thread is automatically pinned to the first available CPU core and worker(s)
79         ## are pinned to next free CPU core(s) after core assigned to main thread
80         # skip-cores 4
81
82         ## Specify a number of workers to be created
83         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
84         ## and main thread's CPU core
85         # workers 2
86
87         ## Set scheduling policy and priority of main and worker threads
88
89         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
90         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
91         # scheduler-policy fifo
92
93         ## Scheduling priority is used only for "real-time policies (fifo and rr),
94         ## and has to be in the range of priorities supported for a particular policy
95         # scheduler-priority 50
96 }
97
98 # dpdk {
99         ## Change default settings for all intefaces
100         # dev default {
101                 ## Number of receive queues, enables RSS
102                 ## Default is 1
103                 # num-rx-queues 3
104
105                 ## Number of transmit queues, Default is equal
106                 ## to number of worker threads or 1 if no workers treads
107                 # num-tx-queues 3
108
109                 ## Number of descriptors in transmit and receive rings
110                 ## increasing or reducing number can impact performance
111                 ## Default is 1024 for both rx and tx
112                 # num-rx-desc 512
113                 # num-tx-desc 512
114
115                 ## VLAN strip offload mode for interface
116                 ## Default is off
117                 # vlan-strip-offload on
118         # }
119
120         ## Whitelist specific interface by specifying PCI address
121         # dev 0000:02:00.0
122
123         ## Whitelist specific interface by specifying PCI address and in
124         ## addition specify custom parameters for this interface
125         # dev 0000:02:00.1 {
126         #       num-rx-queues 2
127         # }
128
129         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
130         ## and uio_pci_generic (default)
131         # uio-driver vfio-pci
132
133         ## Disable mutli-segment buffers, improves performance but
134         ## disables Jumbo MTU support
135         # no-multi-seg
136
137         ## Increase number of buffers allocated, needed only in scenarios with
138         ## large number of interfaces and worker threads. Value is per CPU socket.
139         ## Default is 16384
140         # num-mbufs 128000
141
142         ## Change hugepages allocation per-socket, needed only if there is need for
143         ## larger number of mbufs. Default is 256M on each detected CPU socket
144         # socket-mem 2048,2048
145 # }
146
147 EOF
148
149 # Get list of network device PCI bus addresses
150 get_nic_pci_list() {
151     while read -r line ; do
152         if [ "$line" != "${line#*network device}" ]; then
153             echo -n "${line%% *} "
154         fi
155     done < <(lspci)
156 }
157
158 NICS=$(get_nic_pci_list)
159 NICS=`echo ${NICS} | sed 's/[0]\+\([0-9]\)/\1/g' | sed 's/[.:]/\//g'`
160
161 BNG_MUX_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 2`  # second interface in list
162 MUX_GW_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 4`   # fourth interface in list
163
164 cat > /etc/vpp/setup.gate << EOF
165 set int state ${BNG_MUX_NIC} up
166 set int ip address ${BNG_MUX_NIC} ${MUX_TO_BNG_IP}/${BNG_MUX_CIDR#*/}
167
168 set int state ${MUX_GW_NIC} up
169 set int ip address ${MUX_GW_NIC} ${MUX_GW_IP}/${MUX_GW_CIDR#*/}
170 ip route add ${BRG_BNG_CIDR} via ${BNG_TO_MUX_IP} ${BNG_MUX_NIC}
171 EOF
172
173
174 # Create systemctl service for Honeycomb
175 cat > /etc/systemd/system/honeycomb.service << EOF
176 [Unit]
177 Description=Honeycomb Agent for the VPP control plane
178 Documentation=https://wiki.fd.io/view/Honeycomb
179 Requires=vpp.service
180 After=vpp.service
181
182 [Service]
183 ExecStart=/opt/honeycomb/honeycomb
184 ExecStop=/opt/reset_ves_mode.sh
185 Restart=always
186 RestartSec=10
187
188 [Install]
189 WantedBy=multi-user.target
190 EOF
191 systemctl enable /etc/systemd/system/honeycomb.service
192
193 # Create script that is used to reset the ves mode configuration
194 # when the honeycomb service is stopped
195 cat > /opt/reset_ves_mode.sh << EOF
196 #!/bin/bash
197 mode_data=\$(curl -X GET -u admin:admin http://127.0.0.1:8183/restconf/config/vesagent:vesagent/mode 2>/dev/null | sed s/\"base-packet-loss\":[0-9]\*,/\"base-packet-loss\":0,/)
198 if [[ "\${mode_data}" != "" ]]
199 then
200     curl -X DELETE -u admin:admin http://127.0.0.1:8183/restconf/config/vesagent:vesagent/mode
201     curl -H "Content-Type:application/json" --data "\${mode_data}" -X POST -u admin:admin http://127.0.0.1:8183/restconf/config/vesagent:vesagent
202 fi
203 EOF
204     chmod a+x /opt/reset_ves_mode.sh
205
206     #Create a systemd service for auto-save
207     cat > /usr/bin/save_config << EOF
208 #!/bin/bash
209
210 #########################################################################
211 #
212 #  Copyright (c) 2017 Intel and/or its affiliates.
213
214 #  Licensed under the Apache License, Version 2.0 (the "License");
215 #  you may not use this file except in compliance with the License.
216 #  You may obtain a copy of the License at:
217
218 #      http://www.apache.org/licenses/LICENSE-2.0
219
220 #  Unless required by applicable law or agreed to in writing, software
221 #  distributed under the License is distributed on an "AS IS" BASIS,
222 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
223 #  See the License for the specific language governing permissions and
224 #  limitations under the License.
225 #
226 ##########################################################################
227
228 ############################### Variables ################################
229 VPP_SETUP_GATE=/etc/vpp/setup.gate
230
231 ############################### Functions ################################
232
233 # Write the commands to the startup scripts.
234 #
235 # We could set VPP configuration to the startup.conf.
236 # Write the configuration to the startup scripts so we could
237 # restore the system after rebooting.
238 #
239 write_startup_scripts()
240 {
241         local cmd=\${2}
242         local is_add=\${1}
243
244         if [[ \${is_add} == add ]] ;then
245                 while read -r line
246                 do
247                         if [[ \${line} == \${cmd} ]] ;then
248                                 return 0
249                         fi
250                 done < \${VPP_SETUP_GATE}
251
252                 echo "\${cmd}" >> \${VPP_SETUP_GATE}
253         else
254                 while read -r line
255                 do
256                         if [[ \${line} == \${cmd} ]] ;then
257                                 sed -i "/\${line}/d" \${VPP_SETUP_GATE}
258                                 return 0
259                         fi
260                 done < \${VPP_SETUP_GATE}
261         fi
262 }
263
264 # Save the VxLAN Tunnel Configuration to the startup script.
265 #
266 # Get the current VxLAN tunnel configuration with bash command:
267 # \$vppctl show vxlan tunnel
268 #  [0] src 10.3.0.2 dst 10.1.0.20 vni 100 sw_if_index 1 encap_fib_index 0 fib_entry_index 7 decap_next l2
269 #  [1] src 10.5.0.20 dst 10.5.0.21 vni 100 sw_if_index 2 encap_fib_index 0 fib_entry_index 8 decap_next l2
270 # Set the VxLAN Tunnel with the bash command:
271 # \$vppctl create vxlan tunnel src 10.3.0.2 dst 10.1.0.20 vni 100
272 # vxlan_tunnel0
273 save_vxlan_tunnel()
274 {
275         local src=""
276         local dst=""
277         local vni=""
278
279         vppctl show vxlan tunnel | while read line
280         do
281                 if [ "\${line}" != "" ] ;then
282                         src=\`echo \${line} | awk '{ print \$3 }'\`
283                         dst=\`echo \${line} | awk '{ print \$5 }'\`
284                         vni=\`echo \${line} | awk '{ print \$7 }'\`
285
286                         write_startup_scripts add "create vxlan tunnel src \${src} dst \${dst} vni \${vni}"
287                 fi
288         done
289 }
290
291 # Save the VxLAN tunnel L2 xconnect configuration to the startup script.
292 #
293 # Get the Current L2 Address configuration with bash command:
294 # \$vppctl show int addr
295 # local0 (dn):
296 # vxlan_tunnel0 (up):
297 #   l2 xconnect vxlan_tunnel1
298 # vxlan_tunnel1 (up):
299 #   l2 xconnect vxlan_tunnel0
300 # Save the VxLAN tunnel L2 xconnect configuration with bash command:
301 # \$vppctl set interface l2 xconnect vxlan_tunnel0 vxlan_tunnel1
302 #
303 save_vxlan_xconnect()
304 {
305         local ingress=""
306         local egress=""
307
308         vppctl show int addr | while read line
309         do
310                 if [[ \${line} == vxlan_tunnel* ]] ;then
311                         read next
312                         while [[ \${next} != l2* ]] || [[ \${next} == "" ]]
313                         do
314                                 line=\`echo \${next}\`
315                                 read next
316                         done
317                         if [[ \${next} == l2* ]] ;then
318                                 ingress=\`echo \${line} | awk '{ print \$1 }'\`
319                                 egress=\`echo \${next} | awk '{ print \$3 }'\`
320                                 write_startup_scripts add "set interface l2 xconnect \${ingress} \${egress}"
321                         fi
322                 fi
323         done
324 }
325
326 ################################# MAIN ###################################
327
328 save_vxlan_tunnel
329
330 save_vxlan_xconnect
331
332 EOF
333     chmod a+x /usr/bin/save_config
334     cat > /etc/systemd/system/autosave.service << EOF
335 [Unit]
336 Description=Run Scripts at Start and Stop
337 equires=vpp.service
338 After=vpp.service
339
340 [Service]
341 Type=oneshot
342 RemainAfterExit=true
343 ExecStop=/usr/bin/save_config
344
345 [Install]
346 WantedBy=multi-user.target
347 EOF
348 systemctl enable /etc/systemd/system/autosave.service
349
350 # Download DHCP config files
351 cd /opt
352 unzip -p -j /opt/vcpe-scripts-$SCRIPT_VERSION.zip v_gmux_init.sh > /opt/v_gmux_init.sh
353 unzip -p -j /opt/vcpe-scripts-$SCRIPT_VERSION.zip v_gmux.sh > /opt/v_gmux.sh
354 chmod +x v_gmux_init.sh
355 chmod +x v_gmux.sh
356 mv v_gmux.sh /etc/init.d
357 update-rc.d v_gmux.sh defaults
358
359 # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
360 if [[ $CLOUD_ENV != "rackspace" ]]
361 then
362     sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
363     grub-mkconfig -o /boot/grub/grub.cfg
364     sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
365     sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
366     echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
367     reboot
368 fi
369
370 ./v_gmux_init.sh
371