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