Deprecate use of sites/raw for vnf HEAT templates
[demo.git] / vnfs / vCPE / scripts / v_bng_install.sh
1 #!/bin/bash
2 set -o xtrace  # print commands during script execution
3 set -o errexit # exit on command errors
4
5 REPO_URL_ARTIFACTS=$(cat /opt/config/repo_url_artifacts.txt)
6 DEMO_ARTIFACTS_VERSION=$(cat /opt/config/demo_artifacts_version.txt)
7 INSTALL_SCRIPT_VERSION=$(cat /opt/config/install_script_version.txt)
8 VPP_SOURCE_REPO_URL=$(cat /opt/config/vpp_source_repo_url.txt)
9 VPP_SOURCE_REPO_RELEASE_TAG=$(cat /opt/config/vpp_source_repo_release_tag.txt)
10 VPP_PATCH_URL=$(cat /opt/config/vpp_patch_url.txt)
11 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
12 BNG_GMUX_NET_CIDR=$(cat /opt/config/bng_gmux_net_cidr.txt)
13 BNG_GMUX_NET_IPADDR=$(cat /opt/config/bng_gmux_net_ipaddr.txt)
14 BRGEMU_BNG_NET_CIDR=$(cat /opt/config/brgemu_bng_net_cidr.txt)
15 BRGEMU_BNG_NET_IPADDR=$(cat /opt/config/brgemu_bng_net_ipaddr.txt)
16 CPE_SIGNAL_NET_CIDR=$(cat /opt/config/cpe_signal_net_cidr.txt)
17 CPE_SIGNAL_NET_IPADDR=$(cat /opt/config/cpe_signal_net_ipaddr.txt)
18 SDNC_IP_ADDR=$(cat /opt/config/sdnc_ip_addr.txt)
19
20 # Build states are:
21 # 'build' - just build the code
22 # 'done' - code is build, install and setup
23 # 'auto' - bulid, install and setup
24 BUILD_STATE="auto"
25 if [[ -f /opt/config/compile_state.txt ]]
26 then
27     BUILD_STATE=$(cat /opt/config/compile_state.txt)
28 fi
29
30 # Convert Network CIDR to Netmask
31 cdr2mask () {
32         # Number of args to shift, 255..255, first non-255 byte, zeroes
33         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
34         [ $1 -gt 1 ] && shift $1 || shift
35         echo ${1-0}.${2-0}.${3-0}.${4-0}
36 }
37
38 # OpenStack network configuration
39 if [[ $BUILD_STATE != "build" ]]
40 then
41     if [[ $CLOUD_ENV == "openstack" ]]
42     then
43         echo 127.0.0.1 $(hostname) >> /etc/hosts
44
45         # Allow remote login as root
46         mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
47         cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
48
49         MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
50
51         IP=$(cat /opt/config/oam_ipaddr.txt)
52         BITS=$(cat /opt/config/oam_cidr.txt | cut -d"/" -f2)
53         NETMASK=$(cdr2mask $BITS)
54         echo "auto eth2" >> /etc/network/interfaces
55         echo "iface eth2 inet static" >> /etc/network/interfaces
56         echo "    address $IP" >> /etc/network/interfaces
57         echo "    netmask $NETMASK" >> /etc/network/interfaces
58         echo "    mtu $MTU" >> /etc/network/interfaces
59
60         # eth2 probably doesn't exist yet and we should reboot after this anyway
61         # ifup eth2
62     fi
63 fi  # endif BUILD_STATE != "build"
64
65 if [[ $BUILD_STATE != "done" ]]
66 then
67     # Enable IPV4 forwarding through kernel
68     sed -i 's/^.*\(net.ipv4.ip_forward\).*/\1=1/g' /etc/sysctl.conf
69     sysctl -p /etc/sysctl.conf
70
71     # Download required dependencies
72     echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
73     echo "deb-src http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
74     apt-get update
75     apt-get install --allow-unauthenticated -y wget openjdk-8-jdk apt-transport-https ca-certificates g++ libcurl4-gnutls-dev
76     sleep 1
77
78     # Install the tools required for download codes
79     apt-get install -y expect git patch make autoconf libtool linux-image-extra-`uname -r`
80
81     #Download and build the VPP codes
82     cd /opt
83     git clone ${VPP_SOURCE_REPO_URL} -b ${VPP_SOURCE_REPO_RELEASE_TAG} vpp
84     wget -O Vpp-Integrate-FreeRADIUS-Client-for-vBNG.patch ${VPP_PATCH_URL}
85     cd vpp
86     # The patch will place a "dummy" version of dhcp.api.h so the build will succeed
87     mkdir -p build-root/build-vpp-native/vpp/vnet/dhcp/
88     patch -p1 < ../Vpp-Integrate-FreeRADIUS-Client-for-vBNG.patch
89     UNATTENDED='y' make install-dep
90
91     # Install the FreeRADIUS client since we need the lib
92     cd /opt
93     git clone https://github.com/FreeRADIUS/freeradius-client.git
94     cd freeradius-client
95     ./configure
96     make && make install
97     cd /usr/local/lib && ln -s -f libfreeradius-client.so.2.0.0 libfreeradiusclient.so
98     ldconfig
99
100     cd /opt/vpp/build-root
101     ./bootstrap.sh
102     make V=0 PLATFORM=vpp TAG=vpp install-deb
103
104     # install additional dependencies for vpp
105     apt-get install -y python-cffi python-ply python-pycparser
106
107     # Install the VPP package
108     cd /opt/vpp/build-root
109     dpkg -i *.deb
110     systemctl stop vpp
111
112     # Disable automatic upgrades
113     if [[ $CLOUD_ENV != "rackspace" ]]
114     then
115         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
116         sed -i 's/\(APT::Periodic::Unattended-Upgrade\) "1"/\1 "0"/' /etc/apt/apt.conf.d/20auto-upgrades
117     fi
118
119 fi  # endif BUILD_STATE != "done"
120
121 if [[ $BUILD_STATE != "build" ]]
122 then
123     # Auto-start configuration for the VPP
124     cat > /etc/vpp/startup.conf << EOF
125
126 unix {
127   nodaemon
128   log /tmp/vpp.log
129   full-coredump
130   cli-listen localhost:5002
131   startup-config /etc/vpp/setup.gate
132 }
133
134 api-trace {
135   on
136 }
137
138 api-segment {
139   gid vpp
140 }
141
142 cpu {
143         ## In the VPP there is one main thread and optionally the user can create worker(s)
144         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
145
146         ## Manual pinning of thread(s) to CPU core(s)
147
148         ## Set logical CPU core where main thread runs
149         # main-core 1
150
151         ## Set logical CPU core(s) where worker threads are running
152         # corelist-workers 2-3,18-19
153
154         ## Automatic pinning of thread(s) to CPU core(s)
155
156         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
157         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
158         ## The main thread is automatically pinned to the first available CPU core and worker(s)
159         ## are pinned to next free CPU core(s) after core assigned to main thread
160         # skip-cores 4
161
162         ## Specify a number of workers to be created
163         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
164         ## and main thread's CPU core
165         # workers 2
166
167         ## Set scheduling policy and priority of main and worker threads
168
169         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
170         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
171         # scheduler-policy fifo
172
173         ## Scheduling priority is used only for "real-time policies (fifo and rr),
174         ## and has to be in the range of priorities supported for a particular policy
175         # scheduler-priority 50
176 }
177
178 # dpdk {
179         ## Change default settings for all intefaces
180         # dev default {
181                 ## Number of receive queues, enables RSS
182                 ## Default is 1
183                 # num-rx-queues 3
184
185                 ## Number of transmit queues, Default is equal
186                 ## to number of worker threads or 1 if no workers treads
187                 # num-tx-queues 3
188
189                 ## Number of descriptors in transmit and receive rings
190                 ## increasing or reducing number can impact performance
191                 ## Default is 1024 for both rx and tx
192                 # num-rx-desc 512
193                 # num-tx-desc 512
194
195                 ## VLAN strip offload mode for interface
196                 ## Default is off
197                 # vlan-strip-offload on
198         # }
199
200         ## Whitelist specific interface by specifying PCI address
201         # dev 0000:02:00.0
202
203         ## Whitelist specific interface by specifying PCI address and in
204         ## addition specify custom parameters for this interface
205         # dev 0000:02:00.1 {
206         #       num-rx-queues 2
207         # }
208
209         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
210         ## and uio_pci_generic (default)
211         # uio-driver vfio-pci
212
213         ## Disable mutli-segment buffers, improves performance but
214         ## disables Jumbo MTU support
215         # no-multi-seg
216
217         ## Increase number of buffers allocated, needed only in scenarios with
218         ## large number of interfaces and worker threads. Value is per CPU socket.
219         ## Default is 16384
220         # num-mbufs 128000
221
222         ## Change hugepages allocation per-socket, needed only if there is need for
223         ## larger number of mbufs. Default is 256M on each detected CPU socket
224         # socket-mem 2048,2048
225 # }
226
227 EOF
228
229     get_nic_pci_list() {
230                 while read -r line ; do
231                         if [ "$line" != "${line#*network device}" ]; then
232                                 echo -n "${line%% *} "
233                         fi
234                 done < <(lspci)
235     }
236
237     NICS=$(get_nic_pci_list)
238     NICS=`echo ${NICS} | sed 's/[0]\+\([0-9]\)/\1/g' | sed 's/[.:]/\//g'`
239
240     BRGEMU_BNG_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 2` # second interface in list
241     CPE_SIGNAL_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 4` # fourth interface in list
242     BNG_GMUX_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 5` # fifth interface in list
243
244     cat > /etc/vpp/setup.gate << EOF
245 set int state ${BRGEMU_BNG_NIC} up
246 set interface ip address ${BRGEMU_BNG_NIC} ${BRGEMU_BNG_NET_IPADDR}/${BRGEMU_BNG_NET_CIDR#*/}
247
248 set int state ${CPE_SIGNAL_NIC} up
249 set interface ip address ${CPE_SIGNAL_NIC} ${CPE_SIGNAL_NET_IPADDR}/${CPE_SIGNAL_NET_CIDR#*/}
250
251 set int state ${BNG_GMUX_NIC} up
252 set interface ip address ${BNG_GMUX_NIC} ${BNG_GMUX_NET_IPADDR}/${BNG_GMUX_NET_CIDR#*/}
253
254 set vbng dhcp4 remote 10.4.0.1 local ${CPE_SIGNAL_NET_IPADDR}
255 set vbng aaa config /etc/vpp/vbng-aaa.cfg nas-port 5060
256
257 tap connect tap0 address 192.168.40.40/24
258 set int state tap-0 up
259 set int ip address tap-0 192.168.40.41/24
260 ip route add ${SDNC_IP_ADDR}/32 via 192.168.40.40 tap-0
261
262 EOF
263
264     cat > /etc/vpp/vbng-aaa.cfg << EOF
265 # General settings
266
267 # specify which authentication comes first respectively which
268 # authentication is used. possible values are: "radius" and "local".
269 # if you specify "radius,local" then the RADIUS server is asked
270 # first then the local one. if only one keyword is specified only
271 # this server is asked.
272 auth_order      radius,local
273
274 # maximum login tries a user has
275 login_tries     2
276
277 # timeout for all login tries
278 # if this time is exceeded the user is kicked out
279 login_timeout   5
280
281 # name of the nologin file which when it exists disables logins.
282 # it may be extended by the ttyname which will result in
283 # a terminal specific lock (e.g. /etc/nologin.ttyS2 will disable
284 # logins on /dev/ttyS2)
285 nologin /etc/nologin
286
287 # name of the issue file. it's only display when no username is passed
288 # on the radlogin command line
289 issue   /usr/local/etc/radiusclient/issue
290
291 # RADIUS settings
292
293 # RADIUS server to use for authentication requests. this config
294 # item can appear more then one time. if multiple servers are
295 # defined they are tried in a round robin fashion if one
296 # server is not answering.
297 # optionally you can specify a the port number on which is remote
298 # RADIUS listens separated by a colon from the hostname. if
299 # no port is specified /etc/services is consulted of the radius
300 # service. if this fails also a compiled in default is used.
301 #authserver     10.4.0.2
302 authserver      localhost
303
304 # RADIUS server to use for accouting requests. All that I
305 # said for authserver applies, too. 
306 #
307 #acctserver     10.4.0.2
308 acctserver      localhost
309
310 # file holding shared secrets used for the communication
311 # between the RADIUS client and server
312 servers         /usr/local/etc/radiusclient/servers
313
314 # dictionary of allowed attributes and values
315 # just like in the normal RADIUS distributions
316 dictionary      /usr/local/etc/radiusclient/dictionary
317
318 # program to call for a RADIUS authenticated login
319 login_radius    /usr/local/sbin/login.radius
320
321 # file which holds sequence number for communication with the
322 # RADIUS server
323 seqfile         /var/run/radius.seq
324
325 # file which specifies mapping between ttyname and NAS-Port attribute
326 mapfile         /usr/local/etc/radiusclient/port-id-map
327
328 # default authentication realm to append to all usernames if no
329 # realm was explicitly specified by the user
330 # the radiusd directly form Livingston doesnt use any realms, so leave
331 # it blank then
332 default_realm
333
334 # time to wait for a reply from the RADIUS server
335 radius_timeout  10
336
337 # resend request this many times before trying the next server
338 radius_retries  3
339
340 # The length of time in seconds that we skip a nonresponsive RADIUS
341 # server for transaction requests.  Server(s) being in the "dead" state
342 # are tried only after all other non-dead servers have been tried and
343 # failed or timeouted.  The deadtime interval starts when the server
344 # does not respond to an authentication/accounting request transmissions. 
345 # When the interval expires, the "dead" server would be re-tried again,
346 # and if it's still down then it will be considered "dead" for another
347 # such interval and so on. This option is no-op if there is only one
348 # server in the list. Set to 0 in order to disable the feature.
349 radius_deadtime 0
350
351 # local address from which radius packets have to be sent
352 bindaddr *
353
354 # LOCAL settings
355
356 # program to execute for local login
357 # it must support the -f flag for preauthenticated login
358 login_local     /bin/login
359 EOF
360
361     cat >> /usr/local/etc/radiusclient/dictionary << EOF
362
363 #
364 #       DHCP Proxy/Relay attributes
365 #
366 ATTRIBUTE       DHCP-Agent-Circuit-Id   82.1    integer
367 ATTRIBUTE       DHCP-Agent-Remote-Id    82.2    string
368 ATTRIBUTE       DHCP-Relay-Circuit-Id   82.1    integer
369 ATTRIBUTE       DHCP-Relay-Remote-Id    82.2    string
370
371 EOF
372
373     cat >> /usr/local/etc/radiusclient/servers << EOF
374 10.4.0.2                                        testing123
375 localhost/localhost                             testing123
376
377 EOF
378
379     # Download DHCP config files
380     cd /opt
381     unzip -p -j /opt/vcpe-scripts-$INSTALL_SCRIPT_VERSION.zip v_bng_init.sh > /opt/v_bng_init.sh
382     unzip -p -j /opt/vcpe-scripts-$INSTALL_SCRIPT_VERSION.zip v_bng.sh > /opt/v_bng.sh
383     chmod +x v_bng_init.sh
384     chmod +x v_bng.sh
385     sed -i 's/^\(# Provides:\).*/\1 v_bng/g' ./v_bng.sh
386     mv v_bng.sh /etc/init.d
387     update-rc.d v_bng.sh defaults
388     
389     # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
390     if [[ $CLOUD_ENV != "rackspace" ]]
391     then
392         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
393         grub-mkconfig -o /boot/grub/grub.cfg
394         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
395         touch /etc/udev/rules.d/70-persistent-net.rules
396         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
397         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
398         reboot
399     fi
400
401     ./v_bng_init.sh
402 fi # endif BUILD_STATE != "build"
403