utils.bash 38 KB
Newer Older
Lionel Gauthier's avatar
Lionel Gauthier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
################################################################################
# Eurecom OpenAirInterface core network
# Copyright(c) 1999 - 2014 Eurecom
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#
# The full GNU General Public License is included in this distribution in
# the file called "COPYING".
#
# Contact Information
# Openair Admin: openair_admin@eurecom.fr
# Openair Tech : openair_tech@eurecom.fr
# Forums       : http://forums.eurecom.fsr/openairinterface
# Address      : EURECOM,
#                Campus SophiaTech,
#                450 Route des Chappes,
#                CS 50193
#                06904 Biot Sophia Antipolis cedex,
#                FRANCE
################################################################################
# file utils.bash
# brief
# author Lionel Gauthier
# company Eurecom
# email: lionel.gauthier@eurecom.fr
#
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
cidr2mask() {
  local i mask=""
  local full_octets=$(($1/8))
  local partial_octet=$(($1%8))

  for ((i=0;i<4;i+=1)); do
    if [ $i -lt $full_octets ]; then
      mask+=255
    elif [ $i -eq $full_octets ]; then
      mask+=$((256 - 2**(8-$partial_octet)))
    else
      mask+=0
    fi
    test $i -lt 3 && mask+=.
  done

  echo $mask
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
# example: netcalc 192.168.12.100 255.255.255.0
netcalc(){
    local IFS='.' ip i
    local -a oct msk
    
    read -ra oct <<<"$1"
    read -ra msk <<<"$2"

    for i in ${!oct[@]}; do
        ip+=( "$(( oct[i] & msk[i] ))" )
    done
    
    echo "${ip[*]}"
}

# example: s
bcastcalc(){

    local IFS='.' ip i
    local -a oct msk
    
    read -ra oct <<<"$1"
    read -ra msk <<<"$2"

    for i in ${!oct[@]}; do
        ip+=( "$(( oct[i] + ( 255 - ( oct[i] | msk[i] ) ) ))" )
    done

    echo "${ip[*]}"
}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

black='\E[30m'
red='\E[31m'
green='\E[32m'
yellow='\E[33m'
blue='\E[34m'
magenta='\E[35m'
cyan='\E[36m'
white='\E[37m'
reset_color='\E[00m'

ROOT_UID=0
E_NOTROOT=67

HOSTNAME=$(hostname -f)
Lionel Gauthier's avatar
Lionel Gauthier committed
102
IPTABLES=`which iptables`
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

trim ()
{
    echo "$1" | sed -n '1h;1!H;${;g;s/^[ \t]*//g;s/[ \t]*$//g;p;}'
}

trim2()
{
    local var=$@
    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
    echo -n "$var"
}

cecho()   # Color-echo
# arg1 = message
# arg2 = color
{
    local default_msg="No Message."
    message=${1:-$default_msg}
    color=${2:-$green}
    echo -e -n "$color$message$reset_color"
    echo
    return
}

echo_error() {
    local my_string=""
    until [ -z "$1" ]
    do
        my_string="$my_string$1"
        shift
        done
        cecho "$my_string" $red
}

Lionel Gauthier's avatar
Lionel Gauthier committed
139 140 141 142 143 144 145 146 147 148 149
echo_fatal() {
    local my_string=""
    until [ -z "$1" ]
    do
        my_string="$my_string$1"
        shift
        done
        echo_error "$my_string"
    exit -1
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
echo_warning() {
    local my_string=""
    until [ -z "$1" ]
    do
        my_string="$my_string$1"
        shift
    done
    cecho "$my_string" $yellow
}

echo_success() {
    local my_string=""
    until [ -z "$1" ]
    do
        my_string="$my_string$1"
        shift
    done
    cecho "$my_string" $green
}

bash_exec() {
    output=$($1 2>&1)
    result=$?
    if [ $result -eq 0 ]
    then
        echo_success "$1"
    else
        echo_error "$1: $output"
    fi
}

extract() {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)    tar xvjf $1        ;;
            *.tar.gz)     tar xvzf $1        ;;
            *.bz2)        bunzip2  $1        ;;
            *.rar)        unrar    $1        ;;
            *.gz)         gunzip   $1        ;;
            *.tar)        tar xvf  $1        ;;
            *.tbz2)       tar xvjf $1        ;;
            *.tgz)        tar xvzf $1        ;;
            *.zip)        unzip    $1        ;;
            *.Z)          uncompress $1      ;;
            *.7z)         7z x     $1        ;;
            *)            echo_error "'$1' cannot be extracted via >extract<" ; return 1;;
        esac
    else
        echo_error "'$1' is not a valid file"
        return 1
    fi
    return 0
}

rotate_log_file () {
Lionel Gauthier's avatar
 
Lionel Gauthier committed
205 206 207 208 209 210 211 212 213 214
    FULLPATH=$1
    if [ -f $FULLPATH ]; then
        FILENAME=${FULLPATH##*/}
        FILEEXTENSION=${FILENAME##*.}
        BASEDIRECTORY=${FULLPATH%$FILENAME}
        if [ "a$BASEDIRECTORY" == "a" ]; then
            BASEDIRECTORY='.'
        fi
        FILENAME_NO_EXT=$(echo "$FILENAME" | sed 's/\.[^\.]*$//')

Lionel Gauthier's avatar
 
Lionel Gauthier committed
215 216
        # use file last modification time
        TIMESTAMP=`date -r $FULLPATH +%Y-%m-%d.%Hh_%Mm_%Ss`
Lionel Gauthier's avatar
 
Lionel Gauthier committed
217 218 219 220
        
        NEWLOGFILE=$TIMESTAMP.$FILENAME_NO_EXT.$FILEEXTENSION
        mv $FULLPATH /tmp/$NEWLOGFILE
        cat /dev/null > $FULLPATH
221
        sync
Lionel Gauthier's avatar
 
Lionel Gauthier committed
222
        gzip -c --name -f -9 /tmp/$NEWLOGFILE > $BASEDIRECTORY/$NEWLOGFILE.gz &
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    fi
}

set_openair() {
    path=`pwd`
    declare -i length_path
    declare -i index
    length_path=${#path}

    for i in 'openair1' 'openair2' 'openair3' 'openair-cn' 'targets'
    do
        index=`echo $path | grep -b -o $i | cut -d: -f1`
        #echo ${path%$token*}
        if [[ $index -lt $length_path  && index -gt 0 ]]
           then
               index=`expr $index - 1`
               openair_path=`echo $path | cut -c1-$index`
               #openair_path=`echo ${path:0:$index}`
               export OPENAIR_DIR=$openair_path
               export OPENAIR_HOME=$openair_path
               export OPENAIR1_DIR=$openair_path/openair1
               export OPENAIR2_DIR=$openair_path/openair2
               export OPENAIR3_DIR=$openair_path/openair3
               export OPENAIRCN_DIR=$openair_path/openair-cn
               export OPENAIR_TARGETS=$openair_path/targets
               return 0
           fi
    done
    return -1
}

wait_process_started () {
    if  [ -z "$1" ]
    then
        echo_error "WAITING FOR PROCESS START: NO PROCESS"
        return 1
    fi
    ps -C $1 > /dev/null 2>&1
    while [ $? -ne 0 ]; do
        echo_warning "WAITING FOR $1 START"
        sleep 2
        ps -C $1 > /dev/null 2>&1
    done
    echo_success "PROCESS $1 STARTED"
    return 0
}

is_process_started () {
    if  [ -z "$1" ]
    then
        echo_error "WAITING FOR PROCESS START: NO PROCESS"
        return 1
    fi
    ps -C $1 > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo_success "PROCESS $1 NOT STARTED"
        return 1
    fi
    echo_success "PROCESS $1 STARTED"
    return 0
}

assert() {
    # If condition false
    # exit from script with error message
    E_PARAM_ERR=98
    E_PARAM_FAILED=99

    if [ -z "$2" ] # Not enought parameters passed.
    then
        return $E_PARAM_ERR
    fi

    lineno=$2
    if [ ! $1 ]
    then
Lionel Gauthier's avatar
Lionel Gauthier committed
300 301
        echo_error "Assertion failed:  \"$1\""
        echo_fatal "File \"$0\", line $lineno"
302 303 304
    fi
}

305

Lionel Gauthier's avatar
Lionel Gauthier committed
306 307 308 309 310 311 312
test_install_package() {
  # usage: test_install_package package_name
  if [ $# -eq 1 ]; then
      dpkg -s "$1" > /dev/null 2>&1 && {
          echo "$1 is installed."
      } || {
          echo "$1 is not installed."
Lionel Gauthier's avatar
 
Lionel Gauthier committed
313
          apt-get install --assume-yes $1 
Lionel Gauthier's avatar
Lionel Gauthier committed
314
      }
315 316 317 318 319
  fi
}

test_command_install_script() {
  # usage: test_command_install_script searched_binary script_to_be_invoked_if_binary_not_found
Lionel Gauthier's avatar
Lionel Gauthier committed
320
  command -v $1 >/dev/null 2>&1 || { echo_warning "Program $1 is not installed. Trying installing it." >&2; bash $2; command -v $1 >/dev/null 2>&1 || { echo_fatal "Program $1 is not installed. Aborting." >&2; };}
321 322 323 324
  echo_success "$1 available"
}


Lionel Gauthier's avatar
 
Lionel Gauthier committed
325

326 327 328 329

check_for_epc_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAI_EPC/oai_epc ]
        then
Lionel Gauthier's avatar
Lionel Gauthier committed
330 331
        echo_error "Cannot find oai_epc executable object in directory $OPENAIR3_DIR/OPENAIRMME/objs/OAI_EPC/"
        echo_fatal "Please make sure you have compiled OAI EPC with --enable-standalone-epc option"
332 333 334 335 336 337
        fi
}

check_for_sgw_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAI_SGW/oai_sgw ]
    then
Lionel Gauthier's avatar
Lionel Gauthier committed
338 339
        echo_error "Cannot find oai_sgw executable object in directory $OPENAIR3_DIR/OPENAIRMME/objs/OAI_SGW/"
        echo_fatal "Please make sure you have compiled OAI EPC without --enable-standalone-epc option"
340 341 342 343 344 345
    fi
}

check_for_mme_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAISIM_MME/oaisim_mme ]
    then
Lionel Gauthier's avatar
Lionel Gauthier committed
346 347
        echo_error "Cannot find oai_sgw executable object in directory $OPENAIR3_DIR/OPENAIRMME/objs/OAISIM_MME/"
        echo_fatal "Please make sure you have compiled OAI EPC without --enable-standalone-epc option"
348 349 350 351 352
    fi
}

check_for_root_rights() {
    if [[ $EUID -ne 0 ]]; then
Lionel Gauthier's avatar
Lionel Gauthier committed
353
        echo_fatal "This script must be run as root" 1>&2
354 355 356 357
    fi
}


Lionel Gauthier's avatar
Lionel Gauthier committed
358
is_real_interface() {
Lionel Gauthier's avatar
 
Lionel Gauthier committed
359 360 361 362 363 364
    my_bool=1
    for var in "$@"
    do
        if [ "a$var" == "a" ]; then
           return 0
        fi
365 366 367
        if [ "a$var" == "anone" ]; then
           return 0
        fi
Lionel Gauthier's avatar
 
Lionel Gauthier committed
368 369 370 371 372 373 374 375 376 377 378 379
        IF=`cat /etc/udev/rules.d/70-persistent-net.rules | grep $var | sed 's/^.*NAME=//' | tr -d '"'`
        if [ "$IF" == "$var" ]; then
            if [ "a${var:0:3}" != "aeth" ]; then
                if [ "a${var:0:4}" != "awlan" ]; then
                    if [ "a${var:0:4}" != "awifi" ]; then
                        my_bool=0;
                    fi
                fi
            fi
        fi
    done
    return $my_bool
Lionel Gauthier's avatar
Lionel Gauthier committed
380 381
}

382
is_vlan_interface() {
Lionel Gauthier's avatar
 
Lionel Gauthier committed
383 384 385 386
    my_bool=1
    for var in "$@"
    do
        if [ "a$var" == "a" ]; then
Lionel Gauthier's avatar
 
Lionel Gauthier committed
387
            return 0
Lionel Gauthier's avatar
 
Lionel Gauthier committed
388 389 390 391 392 393 394 395 396 397
        fi
        if [[ $var == *.* ]]
        then
            interface_name=`echo $var | cut -f1 -d '.'`
            vlan=`echo $var | cut -f2 -d '.'`
            IF=`cat /etc/udev/rules.d/70-persistent-net.rules | grep $interface_name | sed 's/^.*NAME=//' | tr -d '"'`
            if [ "$IF" == "$interface_name" ]; then
                if [ "a${interface_name:0:3}" != "aeth" ]; then
                    if [ "a${interface_name:0:4}" != "awlan" ]; then
                        if [ "a${interface_name:0:4}" != "awifi" ]; then
Lionel Gauthier's avatar
 
Lionel Gauthier committed
398
                            return 0;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
399 400 401 402
                        fi
                    fi
                fi
            fi
Lionel Gauthier's avatar
 
Lionel Gauthier committed
403 404
        else
            return 0;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
405 406 407
        fi
    done
    return $my_bool
408 409
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
is_tun_interface() {
    my_bool=1
    for var in "$@"
    do
        if [ "a$var" == "a" ]; then
            return 0
        fi
        bus_info=`ethtool -i $var`
        if [[ "$IF" != *tun* ]]; then
            return 0;
        fi
    done
    return $my_bool
}

Lionel Gauthier's avatar
Lionel Gauthier committed
425

Lionel Gauthier's avatar
 
Lionel Gauthier committed
426 427
delete_tun_interface() {
  is_tun_interface $1 
Lionel Gauthier's avatar
Lionel Gauthier committed
428
  if [ $? -eq 1 ]; then
Lionel Gauthier's avatar
 
Lionel Gauthier committed
429 430
      ip link set $1 down  > /dev/null 2>&1
      openvpn --mktun --dev $1  > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
431 432 433
  fi
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
434 435 436 437

create_tun_interface() {
  openvpn --mktun --dev $1
  #ip link set $1 up
Lionel Gauthier's avatar
Lionel Gauthier committed
438 439
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
440

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
# arg1 = interface name
# arg2 = ipv4 addr cidr
# arg3 = netmask cidr
set_interface_up() {
    interface=$1
    address=$2
    cidr_netmask=$3
    bash_exec "ifconfig  $interface up"
    sync
    netmask=`cidr2mask $cidr_netmask`
    broadcast=`bcastcalc $address $netmask`
    bash_exec "ip -4 addr add  $address/$cidr_netmask broadcast $broadcast dev $interface"
    sync
}

Lionel Gauthier's avatar
Lionel Gauthier committed
456 457
build_enb_vlan_network() {
    # create vlan interface
458
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1_MME
Lionel Gauthier's avatar
Lionel Gauthier committed
459
    if [ $? -eq 1 ]; then
460 461 462 463
        interface_name=`echo $ENB_INTERFACE_NAME_FOR_S1_MME | cut -f1 -d '.'`
        vlan=`echo $ENB_INTERFACE_NAME_FOR_S1_MME | cut -f2 -d '.'`
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
464
        sync
465
        bash_exec "vconfig add $interface_name $vlan"
Lionel Gauthier's avatar
Lionel Gauthier committed
466
        sync
467 468 469 470
        #bash_exec "ifconfig  $ENB_INTERFACE_NAME_FOR_S1_MME up"
        #sync
        #bash_exec "ip -4 addr add  $ENB_IPV4_ADDRESS_FOR_S1_MME/$ENB_IPV4_NETMASK_FOR_S1_MME dev $ENB_INTERFACE_NAME_FOR_S1_MME"
        set_interface_up $ENB_INTERFACE_NAME_FOR_S1_MME $ENB_IPV4_ADDRESS_FOR_S1_MME $ENB_IPV4_NETMASK_FOR_S1_MME
Lionel Gauthier's avatar
Lionel Gauthier committed
471
    else
472
        echo_fatal "BAD INTERFACE NAME FOR ENB S1-MME $ENB_INTERFACE_NAME_FOR_S1_MME"' (waiting for ethx.y, wlanx.y or wifix.y)'
Lionel Gauthier's avatar
Lionel Gauthier committed
473
    fi;
Lionel Gauthier's avatar
Lionel Gauthier committed
474
    
475
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1U
Lionel Gauthier's avatar
Lionel Gauthier committed
476
    if [ $? -eq 1 ]; then
477 478 479 480
        interface_name=`echo $ENB_INTERFACE_NAME_FOR_S1U | cut -f1 -d '.'`
        vlan=`echo $ENB_INTERFACE_NAME_FOR_S1U | cut -f2 -d '.'`
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1U down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1U      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
481
        sync
482
        bash_exec "vconfig add $interface_name $vlan"
Lionel Gauthier's avatar
Lionel Gauthier committed
483
        sync
484 485 486 487
        #bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1U up"
        #sync
        #bash_exec "ip -4 addr add  $ENB_IPV4_ADDRESS_FOR_S1U/$ENB_IPV4_NETMASK_FOR_S1U dev $ENB_INTERFACE_NAME_FOR_S1U"
        set_interface_up $ENB_INTERFACE_NAME_FOR_S1U $ENB_IPV4_ADDRESS_FOR_S1U $ENB_IPV4_NETMASK_FOR_S1U
Lionel Gauthier's avatar
Lionel Gauthier committed
488 489
        sync
    else
490
        echo_fatal "BAD INTERFACE NAME FOR ENB S1U $ENB_INTERFACE_NAME_FOR_S1U"' (waiting for ethx.y, wlanx.y or wifix.y)'
Lionel Gauthier's avatar
Lionel Gauthier committed
491 492 493 494
    fi;
}

clean_enb_vlan_network() {
495
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1_MME
Lionel Gauthier's avatar
Lionel Gauthier committed
496
    if [ $? -eq 1 ]; then
Lionel Gauthier's avatar
 
Lionel Gauthier committed
497
        echo_success "Found VLAN interface $ENB_INTERFACE_NAME_FOR_S1_MME ... deleting"
498 499
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
500
    fi;
Lionel Gauthier's avatar
Lionel Gauthier committed
501
    
502
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1U
Lionel Gauthier's avatar
Lionel Gauthier committed
503
    if [ $? -eq 1 ]; then
Lionel Gauthier's avatar
 
Lionel Gauthier committed
504
        echo_success "Found VLAN interface $ENB_INTERFACE_NAME_FOR_S1U ... deleting"
505 506
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1U down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1U > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
507 508
    fi;
    sync;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
509
    clean_network
Lionel Gauthier's avatar
Lionel Gauthier committed
510 511
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
512

Lionel Gauthier's avatar
Lionel Gauthier committed
513 514
test_enb_vlan_network() {
    # TEST INTERFACES
Lionel Gauthier's avatar
Lionel Gauthier committed
515 516 517 518
    #ping -q -c 1 $MME_IPV4_ADDRESS_FOR_S1_MME > /dev/null 2>&1
    #if [ $? -ne 0 ]; then echo_fatal "PING MME S1_MME ERROR, ADDRESS IS $MME_IPV4_ADDRESS_FOR_S1_MME"; fi;
    #ping -q -c 1 $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP > /dev/null 2>&1
    #if [ $? -ne 0 ]; then echo_fatal "PING SGW S1U ERROR, ADDRESS IS $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP"; fi;
Lionel Gauthier's avatar
Lionel Gauthier committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    return 0
}


build_mme_spgw_vlan_network() {
    #                                                                           hss.eur
    #                                                                             |
    #        +-----------+                                +-----------+           v   +----------+
    #        |  eNB      +------+            VLAN 1+------+    MME    +----+      +---+   HSS    |
    #        |           |ethx.1+------------------+ethy.1|           |    +------+   |          |
    #        |           +------+                  +------+           +----+      +---+          |
    #        |           |ethx.2+-------+                 |           |               +----------+
    #        |           +------+       |                 +-+-------+-+
    #        |           |              |                   | s11mme|    
    #        |           |              |                   +---+---+    
    #        |           |              |             (optional)|   VLAN 3
    #        +-----------+              |                   +---+---+    
    #                                   |                   | s11sgw|            router.eur
    #                                   |                 +-+-------+-+              |   +--------------+
    #                                   |                 |  S+P-GW   |              v   |   ROUTER     |
    #                                   |  VLAN2   +------+           +-------+     +----+              +----+
    #                                   +----------+ethy.2|           |sgi    +-...-+    |              |    +---...Internet
    #                                              +------+           +-------+     +----+              +----+
    #                                                     |           |      11 VLANS    |              |
    #                                                     +-----------+   ids=[5..15]    +--------------+
    
    bash_exec "modprobe 8021q"
    
    # create vlan interface
548
    is_vlan_interface $MME_INTERFACE_NAME_FOR_S1_MME
Lionel Gauthier's avatar
Lionel Gauthier committed
549
    if [ $? -eq 1 ]; then
550 551 552 553
        interface_name=`echo $MME_INTERFACE_NAME_FOR_S1_MME | cut -f1 -d '.'`
        vlan=`echo $MME_INTERFACE_NAME_FOR_S1_MME | cut -f2 -d '.'`
        ifconfig    $MME_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
        vconfig rem $MME_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
554
        sync
555
        bash_exec "vconfig add $interface_name $vlan"
Lionel Gauthier's avatar
Lionel Gauthier committed
556
        sync
557 558 559 560
        #bash_exec "ifconfig  $MME_INTERFACE_NAME_FOR_S1_MME up"
        #sync
        #"bash_exec "ip -4 addr add  $MME_IPV4_ADDRESS_FOR_S1_MME/$MME_IPV4_NETMASK_FOR_S1_MME dev $MME_INTERFACE_NAME_FOR_S1_MME"
        set_interface_up $MME_INTERFACE_NAME_FOR_S1_MME $MME_IPV4_ADDRESS_FOR_S1_MME $MME_IPV4_NETMASK_FOR_S1_MME
Lionel Gauthier's avatar
Lionel Gauthier committed
561
    else
562
        echo_fatal "BAD INTERFACE NAME FOR SGW S1-MME $MME_INTERFACE_NAME_FOR_S1_MME"' (waiting for ethx.y, wlanx.y or wifix.y)'
Lionel Gauthier's avatar
Lionel Gauthier committed
563
    fi;
Lionel Gauthier's avatar
Lionel Gauthier committed
564
    
565
    is_vlan_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
Lionel Gauthier's avatar
Lionel Gauthier committed
566
    if [ $? -eq 1 ]; then
567 568 569 570
        interface_name=`echo $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP | cut -f1 -d '.'`
        vlan=`echo $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP | cut -f2 -d '.'`
        ifconfig    $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP down > /dev/null 2>&1
        vconfig rem $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
571
        sync
572
        bash_exec "vconfig add $interface_name $vlan"
Lionel Gauthier's avatar
Lionel Gauthier committed
573
        sync
574 575 576 577 578
        #bash_exec "ifconfig  $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP up"
        #sync
        #bash_exec "ip -4 addr add  $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP/$SGW_IPV4_NETMASK_FOR_S1U_S12_S4_UP dev $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP"
        #sync
        set_interface_up $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP $SGW_IPV4_NETMASK_FOR_S1U_S12_S4_UP
Lionel Gauthier's avatar
Lionel Gauthier committed
579
    else
580
        echo_fatal "BAD INTERFACE NAME FOR SGW S1U $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP"' (waiting for ethx.y, wlanx.y or wifix.y)'
Lionel Gauthier's avatar
Lionel Gauthier committed
581 582
    fi;
    
Lionel Gauthier's avatar
Lionel Gauthier committed
583 584 585 586 587 588 589 590
    #
    is_real_interface $PGW_INTERFACE_NAME_FOR_SGI
    if [ $? -eq 1 ]; then
        ping -c 1 router.eur > /dev/null || { echo_fatal "router.eur does not respond to ping" >&2 ; }
        IP_ROUTER=`python -c 'import socket; print socket.gethostbyname("router.eur")'`
        export MAC_ROUTER=`ip neigh show | grep $IP_ROUTER | cut -d ' '  -f5 | tr -d ':'`
        echo_success "ROUTER MAC ADDRESS= $MAC_ROUTER"

Lionel Gauthier's avatar
 
Lionel Gauthier committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

        # # get ipv4 address from PGW_INTERFACE_NAME_FOR_SGI
        #IP_ADDR=`ifconfig $PGW_INTERFACE_NAME_FOR_SGI | awk '/inet addr/ {split ($2,A,":"); print A[2]}' | tr '\n' ' ' | sed -n '1h;1!H;${;g;s/^[ \t]*//g;s/[ \t]*$//g;p;}'`

        #NETWORK=`echo $IP_ADDR | cut -d . -f 1,2,3`

        bash_exec "modprobe 8021q"

        for i in 5 6 7 8 9 10 11 12 13 14 15
        do
            # create vlan interface
            ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
            vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i > /dev/null 2>&1
            sync
            bash_exec "vconfig add $PGW_INTERFACE_NAME_FOR_SGI $i"
            sync
            bash_exec "ifconfig  $PGW_INTERFACE_NAME_FOR_SGI.$i up"
            sync
            # configure vlan interface
            #CIDR=$NETWORK'.'$i'/24'
            base=200
            NET=$(( $i + $base ))
            CIDR='10.0.'$NET'.2/8'
            bash_exec "ip -4 addr add  $CIDR dev $PGW_INTERFACE_NAME_FOR_SGI.$i"
        done
Lionel Gauthier's avatar
Lionel Gauthier committed
616 617

        bash_exec "ip link set $PGW_INTERFACE_NAME_FOR_SGI promisc on"
Lionel Gauthier's avatar
Lionel Gauthier committed
618
    else
Lionel Gauthier's avatar
Lionel Gauthier committed
619
        echo_warning "SGI interface disabled by config file"
Lionel Gauthier's avatar
Lionel Gauthier committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    fi
}

clean_epc_vlan_network() {
    bash_exec "modprobe tun"
    bash_exec "modprobe ip_tables"
    bash_exec "modprobe iptable_nat"
    bash_exec "modprobe x_tables"

    bash_exec "$IPTABLES -P INPUT ACCEPT"
    bash_exec "$IPTABLES -F INPUT"
    bash_exec "$IPTABLES -P OUTPUT ACCEPT"
    bash_exec "$IPTABLES -F OUTPUT"
    bash_exec "$IPTABLES -P FORWARD ACCEPT"
    bash_exec "$IPTABLES -F FORWARD"
    bash_exec "$IPTABLES -t raw    -F"
    bash_exec "$IPTABLES -t nat    -F"
    bash_exec "$IPTABLES -t mangle -F"
    bash_exec "$IPTABLES -t filter -F"

    bash_exec "ip route flush cache"

    echo "   Disabling forwarding"
    bash_exec "sysctl -w net.ipv4.ip_forward=0"
    assert "  `sysctl -n net.ipv4.ip_forward` -eq 0" $LINENO

    echo "   Enabling DynamicAddr.."
    bash_exec "sysctl -w net.ipv4.ip_dynaddr=1"
    assert "  `sysctl -n net.ipv4.ip_dynaddr` -eq 1" $LINENO

    bash_exec "sysctl -w net.ipv4.conf.all.log_martians=1"
    assert "  `sysctl -n net.ipv4.conf.all.log_martians` -eq 1" $LINENO


    echo "   Disabling reverse path filtering"
    bash_exec "sysctl -w net.ipv4.conf.all.rp_filter=0"
    assert "  `sysctl -n net.ipv4.conf.all.rp_filter` -eq 0" $LINENO

    bash_exec "modprobe 8021q"

660 661
    ifconfig    $MME_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
    vconfig rem $MME_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
662

663 664
    ifconfig    $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP down > /dev/null 2>&1
    vconfig rem $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
665 666


Lionel Gauthier's avatar
Lionel Gauthier committed
667 668 669
    for i in 5 6 7 8 9 10 11 12 13 14 15
    do
        # delete vlan interface
Lionel Gauthier's avatar
Lionel Gauthier committed
670 671
        ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
        vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i      > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
672
    done
Lionel Gauthier's avatar
Lionel Gauthier committed
673
    #ip link set $PGW_INTERFACE_NAME_FOR_SGI down > /dev/null 2>&1
Lionel Gauthier's avatar
 
Lionel Gauthier committed
674
    clean_network
675 676
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
677
build_tun_network() {
678 679 680
    # REMINDER:
    #                                                                           hss.eur
    #                                                                             |
Lionel Gauthier's avatar
 
Lionel Gauthier committed
681 682 683 684 685 686 687 688 689 690 691
    #        +-----------+                                +-----------+           v   +----------+
    #        |  eNB      +------+                  +------+    MME    +----+      +---+   HSS    |
    #        |           |cpenb0+------------------+cpmme0|           |s6am+------+s6a|          |
    #        |           +------+                  +------+           +----+      +---+          |
    #        |           |upenb0+-------+                 |           |               +----------+
    #        |           +------+       |                 +-+-------+-+
    #        |           |              |                   | s11mme| 
    #        |           |              |                   +---+---+ 
    #        |           |              |             (optional)| 
    #        +-----------+              |                   +---+---+ 
    #                                   |                   | s11sgw|             router.eur
692 693
    #                                   |                 +-+-------+-+              |   +--------------+
    #                                   |                 |  S+P-GW   |              v   |   ROUTER     |
Lionel Gauthier's avatar
 
Lionel Gauthier committed
694
    #                                   |          +------+           +-------+     +----+              +----+
695 696 697 698 699
    #                                   +----------+upsgw0|           |sgi    +-...-+    |              |    +---...Internet
    #                                              +------+           +-------+     +----+              +----+
    #                                                     |           |      11 VLANS    |              |
    #                                                     +-----------+   ids=[5..15]    +--------------+
    #
Lionel Gauthier's avatar
 
Lionel Gauthier committed
700
    bash_exec "modprobe tun"
701
    ##################################################
Lionel Gauthier's avatar
 
Lionel Gauthier committed
702
    # build network between eNB and MME/SPGW and HSS
703
    ##################################################
Lionel Gauthier's avatar
 
Lionel Gauthier committed
704 705 706 707 708 709 710 711
    create_tun_interface $ENB_INTERFACE_NAME_FOR_S1_MME
    create_tun_interface $ENB_INTERFACE_NAME_FOR_S1U
    create_tun_interface $MME_INTERFACE_NAME_FOR_S1_MME
    create_tun_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
    create_tun_interface $MME_INTERFACE_NAME_FOR_S11_MME
    create_tun_interface $SGW_INTERFACE_NAME_FOR_S11
    create_tun_interface $MME_INTERFACE_NAME_FOR_S6A
    create_tun_interface $HSS_INTERFACE_NAME_FOR_S6A
Lionel Gauthier's avatar
Lionel Gauthier committed
712
    
Lionel Gauthier's avatar
 
Lionel Gauthier committed
713 714 715
    set_interface_up $MME_INTERFACE_NAME_FOR_S1_MME        $MME_IPV4_ADDRESS_FOR_S1_MME        $MME_IPV4_NETMASK_FOR_S1_MME
    set_interface_up $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP $SGW_IPV4_NETMASK_FOR_S1U_S12_S4_UP

Lionel Gauthier's avatar
 
Lionel Gauthier committed
716
    set_interface_up $ENB_INTERFACE_NAME_FOR_S1_MME        $ENB_IPV4_ADDRESS_FOR_S1_MME        $ENB_IPV4_NETMASK_FOR_S1_MME
Lionel Gauthier's avatar
 
Lionel Gauthier committed
717 718 719 720 721 722 723 724 725 726
    set_interface_up $ENB_INTERFACE_NAME_FOR_S1U           $ENB_IPV4_ADDRESS_FOR_S1U           $ENB_IPV4_NETMASK_FOR_S1U

    set_interface_up $MME_INTERFACE_NAME_FOR_S11_MME       $MME_IPV4_ADDRESS_FOR_S11_MME       $MME_IPV4_NETMASK_FOR_S11_MME
    set_interface_up $SGW_INTERFACE_NAME_FOR_S11           $SGW_IPV4_ADDRESS_FOR_S11           $SGW_IPV4_NETMASK_FOR_S11

    set_interface_up $MME_INTERFACE_NAME_FOR_S11_MME       $MME_IPV4_ADDRESS_FOR_S11_MME       $MME_IPV4_NETMASK_FOR_S11_MME
    set_interface_up $SGW_INTERFACE_NAME_FOR_S11           $SGW_IPV4_ADDRESS_FOR_S11           $SGW_IPV4_NETMASK_FOR_S11

    set_interface_up $MME_INTERFACE_NAME_FOR_S6A           $MME_IPV4_ADDRESS_FOR_S6A           $MME_IPV4_NETMASK_FOR_S6A
    set_interface_up $HSS_INTERFACE_NAME_FOR_S6A           $HSS_IPV4_ADDRESS_FOR_S6A           $HSS_IPV4_NETMASK_FOR_S6A
727 728
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
729
test_tun_network() {
730

Lionel Gauthier's avatar
Lionel Gauthier committed
731 732 733 734 735 736 737 738 739 740 741 742 743
    # TEST INTERFACES
    ping -q -c 1 $MME_IPV4_ADDRESS_FOR_S1_MME > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $MME_INTERFACE_NAME_FOR_S1_MME ERROR, ADDRESS IS $MME_IPV4_ADDRESS_FOR_S1_MME"; fi;
    ping -q -c 1 $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP ERROR, ADDRESS IS $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP"; fi;
    ping -q -c 1 $ENB_IPV4_ADDRESS_FOR_S1_MME > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $ENB_INTERFACE_NAME_FOR_S1_MME ADDR ERROR, ADDRESS IS $ENB_IPV4_ADDRESS_FOR_S1_MME"; fi;
    ping -q -c 1 $ENB_IPV4_ADDRESS_FOR_S1U > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $ENB_INTERFACE_NAME_FOR_S1U ERROR, ADDRESS IS $ENB_IPV4_ADDRESS_FOR_S1U"; fi;
    ping -q -c 1 $MME_IPV4_ADDRESS_FOR_S11_MME > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $MME_INTERFACE_NAME_FOR_S11_MME ERROR, ADDRESS IS $MME_IPV4_ADDRESS_FOR_S11_MME"; fi;
    ping -q -c 1 $SGW_IPV4_ADDRESS_FOR_S11 > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $SGW_INTERFACE_NAME_FOR_S11 ERROR, ADDRESS IS $SGW_IPV4_ADDRESS_FOR_S11"; fi;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
744 745 746 747
    ping -q -c 1 $MME_IPV4_ADDRESS_FOR_S6A > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $MME_INTERFACE_NAME_FOR_S6A ERROR, ADDRESS IS $MME_IPV4_ADDRESS_FOR_S6A"; fi;
    ping -q -c 1 $HSS_IPV4_ADDRESS_FOR_S6A > /dev/null 2>&1
    if [ $? -ne 0 ]; then echo_fatal "PING INTERFACE $HSS_INTERFACE_NAME_FOR_S6A ERROR, ADDRESS IS $HSS_IPV4_ADDRESS_FOR_S6A"; fi;
Lionel Gauthier's avatar
Lionel Gauthier committed
748 749
    
    
750
    ## TEST NETWORK BETWEEN ENB-MME-SP-GW
Lionel Gauthier's avatar
Lionel Gauthier committed
751 752
    iperf  --bind $MME_IPV4_ADDRESS_FOR_S1_MME -u -s 2>&1  > /dev/null &
    iperf  --bind $ENB_IPV4_ADDRESS_FOR_S1_MME -u --num 1K -c $MME_IPV4_ADDRESS_FOR_S1_MME 2>&1 | grep -i WARNING > /dev/null
753 754
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
Lionel Gauthier's avatar
 
Lionel Gauthier committed
755
        echo_fatal 'NETWORK ERROR CONFIGURATION (tun) between ENB and MME S1'
756
    else
Lionel Gauthier's avatar
 
Lionel Gauthier committed
757
        echo_success 'NETWORK TEST SUCCESS (tun) between ENB and MME S1'
758 759 760 761

    fi
    pkill iperf 2>&1 > /dev/null

Lionel Gauthier's avatar
Lionel Gauthier committed
762 763
    iperf  --bind $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP -u -s 2>&1  > /dev/null &
    iperf  --bind $ENB_IPV4_ADDRESS_FOR_S1U -u --num 1K -c $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP 2>&1 | grep -i WARNING > /dev/null
764 765
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
Lionel Gauthier's avatar
 
Lionel Gauthier committed
766
        echo_fatal 'NETWORK ERROR CONFIGURATION (tun) between ENB and S-GW S1-U'
767
    else
Lionel Gauthier's avatar
 
Lionel Gauthier committed
768
        echo_success 'NETWORK TEST SUCCESS (tun) between ENB and S-GW S1-U'
769 770 771
    fi
    pkill iperf 2>&1 > /dev/null

Lionel Gauthier's avatar
Lionel Gauthier committed
772 773
    iperf  --bind $SGW_IPV4_ADDRESS_FOR_S11 -u -s 2>&1  > /dev/null &
    iperf  --bind $MME_IPV4_ADDRESS_FOR_S11_MME -u --num 1K -c $SGW_IPV4_ADDRESS_FOR_S11 2>&1 | grep -i WARNING > /dev/null
774 775
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
Lionel Gauthier's avatar
 
Lionel Gauthier committed
776 777 778 779 780 781 782 783 784 785 786
        echo_fatal 'NETWORK ERROR CONFIGURATION (tun) between MME and S-GW S11'
    else
        echo_success 'NETWORK TEST SUCCESS (tun) between MME and S-GW S11'
    fi
    pkill iperf 2>&1 > /dev/null
    
    iperf  --bind $HSS_IPV4_ADDRESS_FOR_S6A -u -s 2>&1  > /dev/null &
    iperf  --bind $MME_IPV4_ADDRESS_FOR_S6A -u --num 1K -c $HSS_IPV4_ADDRESS_FOR_S6A 2>&1 | grep -i WARNING > /dev/null
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
        echo_fatal 'NETWORK ERROR CONFIGURATION (tun) between MME and HSS S6A'
787
    else
Lionel Gauthier's avatar
Lionel Gauthier committed
788
        echo_success 'NETWORK TEST SUCCESS (openvswitch) between MME and S-GW S11'
789 790
    fi
    pkill iperf 2>&1 > /dev/null
Lionel Gauthier's avatar
 
Lionel Gauthier committed
791 792 793 794


    # Get MAC address of router.eur
    ping -c 1 hss.eur > /dev/null || { echo_fatal "hss.eur does not respond to ping" >&2 ; }
Lionel Gauthier's avatar
 
Lionel Gauthier committed
795
#TEMP    ping -c 1 router.eur > /dev/null || { echo_fatal "router.eur does not respond to ping" >&2 ; }
796 797 798
    return 0
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
799
clean_tun_network() {
Lionel Gauthier's avatar
 
Lionel Gauthier committed
800
    bash_exec "modprobe tun"
Lionel Gauthier's avatar
Lionel Gauthier committed
801
    ##################################################
Lionel Gauthier's avatar
 
Lionel Gauthier committed
802
    # del interfaces eNB and MME/SPGW and HSS
Lionel Gauthier's avatar
Lionel Gauthier committed
803
    ##################################################
Lionel Gauthier's avatar
 
Lionel Gauthier committed
804 805 806 807 808 809 810 811
    delete_tun_interface $ENB_INTERFACE_NAME_FOR_S1_MME
    delete_tun_interface $ENB_INTERFACE_NAME_FOR_S1U
    delete_tun_interface $MME_INTERFACE_NAME_FOR_S1_MME
    delete_tun_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
    delete_tun_interface $MME_INTERFACE_NAME_FOR_S11_MME
    delete_tun_interface $SGW_INTERFACE_NAME_FOR_S11
    delete_tun_interface $MME_INTERFACE_NAME_FOR_S6A
    delete_tun_interface $HSS_INTERFACE_NAME_FOR_S6A
Lionel Gauthier's avatar
Lionel Gauthier committed
812 813
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
814
build_epc_tun_network() {
815

Lionel Gauthier's avatar
 
Lionel Gauthier committed
816
    build_tun_network
817

Lionel Gauthier's avatar
 
Lionel Gauthier committed
818 819 820 821
#TEMP    ping -c 1 router.eur > /dev/null || { echo_fatal "router.eur does not respond to ping" >&2 ; }
#TEMP    IP_ROUTER=`python -c 'import socket; print socket.gethostbyname("router.eur")'`
#TEMP    export MAC_ROUTER=`ip neigh show | grep $IP_ROUTER | cut -d ' '  -f5 | tr -d ':'`
#TEMP    echo_success "ROUTER MAC ADDRESS= $MAC_ROUTER"
822

Lionel Gauthier's avatar
 
Lionel Gauthier committed
823
    bash_exec "modprobe 8021q"
824

Lionel Gauthier's avatar
 
Lionel Gauthier committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    for i in 5 6 7 8 9 10 11 12 13 14 15
    do
        # create vlan interface
        ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
        vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i      > /dev/null 2>&1
        sync
        bash_exec "vconfig add $PGW_INTERFACE_NAME_FOR_SGI $i"
        sync
        bash_exec "ifconfig  $PGW_INTERFACE_NAME_FOR_SGI.$i up"
        sync
        # configure vlan interface
        #CIDR=$NETWORK'.'$i'/24'
        base=200
        NET=$(( $i + $base ))
        CIDR='10.0.'$NET'.2/8'
        bash_exec "ip -4 addr add  $CIDR dev $PGW_INTERFACE_NAME_FOR_SGI.$i"
    done
842 843 844 845 846 847 848


    bash_exec "ip link set $PGW_INTERFACE_NAME_FOR_SGI promisc on"

}


Lionel Gauthier's avatar
Lionel Gauthier committed
849
clean_epc_ovs_network() {
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    bash_exec "modprobe tun"
    bash_exec "modprobe ip_tables"
    bash_exec "modprobe iptable_nat"
    bash_exec "modprobe x_tables"

    bash_exec "$IPTABLES -P INPUT ACCEPT"
    bash_exec "$IPTABLES -F INPUT"
    bash_exec "$IPTABLES -P OUTPUT ACCEPT"
    bash_exec "$IPTABLES -F OUTPUT"
    bash_exec "$IPTABLES -P FORWARD ACCEPT"
    bash_exec "$IPTABLES -F FORWARD"
    bash_exec "$IPTABLES -t raw    -F"
    bash_exec "$IPTABLES -t nat    -F"
    bash_exec "$IPTABLES -t mangle -F"
    bash_exec "$IPTABLES -t filter -F"

    bash_exec "ip route flush cache"

    echo "   Disabling forwarding"
    bash_exec "sysctl -w net.ipv4.ip_forward=0"
    assert "  `sysctl -n net.ipv4.ip_forward` -eq 0" $LINENO

    echo "   Enabling DynamicAddr.."
    bash_exec "sysctl -w net.ipv4.ip_dynaddr=1"
    assert "  `sysctl -n net.ipv4.ip_dynaddr` -eq 1" $LINENO

    bash_exec "sysctl -w net.ipv4.conf.all.log_martians=1"
    assert "  `sysctl -n net.ipv4.conf.all.log_martians` -eq 1" $LINENO


    echo "   Disabling reverse path filtering"
    bash_exec "sysctl -w net.ipv4.conf.all.rp_filter=0"
    assert "  `sysctl -n net.ipv4.conf.all.rp_filter` -eq 0" $LINENO
Lionel Gauthier's avatar
Lionel Gauthier committed
883 884 885 886
    
    
    for i in 5 6 7 8 9 10 11 12 13 14 15
    do
Lionel Gauthier's avatar
Lionel Gauthier committed
887 888
        ifconfig $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
        vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i   > /dev/null 2>&1
Lionel Gauthier's avatar
Lionel Gauthier committed
889 890
    done
    
Lionel Gauthier's avatar
 
Lionel Gauthier committed
891
    clean_network
Lionel Gauthier's avatar
 
Lionel Gauthier committed
892
    clean_tun_network
893 894
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
895 896 897 898 899 900 901 902 903 904 905 906
clean_network() {
  interfaces=`ifconfig | grep HWaddr | cut -d " " -f1-2 | tr -d '\n'`
  for interface in $interfaces
  do
      is_vlan_interface $interface
      if [ $? -eq 1 ]; then
         echo_success "Found VLAN interface $interface ... deleting"
         ifconfig    $interface down > /dev/null 2>&1
         vconfig rem $interface      > /dev/null 2>&1
      fi
  done
}
907

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
check_s6a_certificate() {
    if [ -d /usr/local/etc/freeDiameter ]
    then
        if [ -f /usr/local/etc/freeDiameter/user.cert.pem ]
        then
            full_hostname=`cat /usr/local/etc/freeDiameter/user.cert.pem | grep "Subject" | grep "CN" | cut -d '=' -f6`
            if [ a$full_hostname == a`hostname`.eur ]
            then
                echo_success "S6A: Found valid certificate in /usr/local/etc/freeDiameter"
                return 1
            fi
        fi
    fi
    echo_error "S6A: Did not find valid certificate in /usr/local/etc/freeDiameter"
    echo_warning "S6A: generatting new certificate in /usr/local/etc/freeDiameter..."
    cd $OPENAIRCN_DIR/S6A/freediameter
    ./make_certs.sh
    check_s6a_certificate
    return 1
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
929 930 931 932 933 934 935
check_install_epc_software() {
    test_install_package autoconf
    test_install_package automake
    test_install_package bison
    test_install_package build-essential
    test_install_package cmake
    test_install_package cmake-curses-gui
Lionel Gauthier's avatar
 
Lionel Gauthier committed
936
    test_install_package ethtool
Lionel Gauthier's avatar
 
Lionel Gauthier committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    test_install_package flex
    test_install_package g++
    test_install_package gawk
    test_install_package gcc
    test_install_package gccxml
    test_install_package gdb 
    test_install_package guile-2.0-dev
    test_install_package iperf
    test_install_package iproute
    test_install_package iptables
    test_install_package libatlas-base-dev
    test_install_package libatlas-dev
    test_install_package libblas
    test_install_package libblas-dev
    test_install_package libconfig-dev
    test_install_package libforms-bin
    test_install_package libforms-dev
    test_install_package libgcrypt11-dev
    test_install_package libgmp-dev
    test_install_package libgtk-3-dev
    test_install_package libidn11-dev
    test_install_package libidn2-0-dev
    test_install_package libmysqlclient-dev
    test_install_package libpgm-dev
    test_install_package libpthread-stubs0-dev
    test_install_package libsctp1
    test_install_package libsctp1
    test_install_package libsctp-dev
    test_install_package libsctp-dev
    test_install_package libtasn1-3-dev
    test_install_package libxml2
    test_install_package libxml2-dev
    test_install_package libxml2-dev
    test_install_package linux-headers-`uname -r`
    test_install_package make
    test_install_package openssl
Lionel Gauthier's avatar
 
Lionel Gauthier committed
973
    test_install_package openvpn
Lionel Gauthier's avatar
 
Lionel Gauthier committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    test_install_package python-dev
    test_install_package subversion
    test_install_package swig
    test_install_package tshark
    test_install_package uml-utilities
    test_install_package unzip
    test_install_package valgrind
    test_install_package vlan

    if [ ! -d /usr/local/etc/freeDiameter ]
        then
           # This script make certificates also
            cd $OPENAIRCN_DIR/S6A/freediameter && ./install_freediameter.sh
        else
            echo_success "freediameter is installed"
            check_s6a_certificate
    fi

    test_command_install_script   "asn1c" "$OPENAIRCN_DIR/SCRIPTS/install_asn1c_0.9.24.modified.bash"

    # One mor check about version of asn1c
    ASN1C_COMPILER_REQUIRED_VERSION_MESSAGE="ASN.1 Compiler, v0.9.24"
    ASN1C_COMPILER_VERSION_MESSAGE=`asn1c -h 2>&1 | grep -i ASN\.1\ Compiler`
    ##ASN1C_COMPILER_VERSION_MESSAGE=`trim $ASN1C_COMPILER_VERSION_MESSAGE`
    if [ "$ASN1C_COMPILER_VERSION_MESSAGE" != "$ASN1C_COMPILER_REQUIRED_VERSION_MESSAGE" ]
    then
        diff <(echo -n "$ASN1C_COMPILER_VERSION_MESSAGE") <(echo -n "$ASN1C_COMPILER_REQUIRED_VERSION_MESSAGE")
        echo_error "Version of asn1c is not the required one, do you want to install the required one (overwrite installation) ? (Y/n)"
        echo_error "$ASN1C_COMPILER_VERSION_MESSAGE"
        while read -r -n 1 -s answer; do
            if [[ $answer = [YyNn] ]]; then
                [[ $answer = [Yy] ]] && $OPENAIRCN_DIR/SCRIPTS/install_asn1c_0.9.24.modified.bash
                [[ $answer = [Nn] ]] && echo_error "Version of asn1c is not the required one, exiting." && exit 1
                break
            fi
        done
    fi
}

Lionel Gauthier's avatar
 
Lionel Gauthier committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
compile_epc() {
    cd $OPENAIRCN_DIR
    OBJ_DIR=`find . -maxdepth 1 -type d -iname obj*`
    if [ ! -n "$OBJ_DIR" ]
    then
        OBJ_DIR="objs"
        bash_exec "mkdir -m 777 ./$OBJ_DIR"
        echo_success "Created $OBJ_DIR directory"
    else
        OBJ_DIR=`basename $OBJ_DIR`
    fi
    if [ ! -f $OBJ_DIR/Makefile ]
    then
        if [ ! -n "m4" ]
        then
            mkdir -m 777 m4
        fi
        echo_success "Invoking autogen"
        bash_exec "./autogen.sh"
        cd ./$OBJ_DIR
        echo_success "Invoking configure"
        ../configure --enable-standalone-epc --enable-raw-socket-for-sgi  LDFLAGS=-L/usr/local/lib
    else
        cd ./$OBJ_DIR
    fi

    pkill oai_epc
    pkill tshark

    if [ -f Makefile ]
    then
        echo_success "Compiling..."
        make -j `cat /proc/cpuinfo | grep processor | wc -l`
        if [ $? -ne 0 ]; then
            echo_error "Build failed, exiting"
            exit 1
        fi
    else
        echo_error "Configure failed, exiting"
        exit 1
    fi
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
###########################################################
IPTABLES=/sbin/iptables
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
declare -x OPENAIR_DIR=""
declare -x OPENAIR1_DIR=""
declare -x OPENAIR2_DIR=""
declare -x OPENAIR3_DIR=""
declare -x OPENAIRCN_DIR=""
declare -x OPENAIR_TARGETS=""
###########################################################

set_openair
cecho "OPENAIR_DIR     = $OPENAIR_DIR" $green
1069
cecho "OPENAIR_HOME    = $OPENAIR_HOME" $green
1070 1071 1072 1073 1074
cecho "OPENAIR1_DIR    = $OPENAIR1_DIR" $green
cecho "OPENAIR2_DIR    = $OPENAIR2_DIR" $green
cecho "OPENAIR3_DIR    = $OPENAIR3_DIR" $green
cecho "OPENAIRCN_DIR   = $OPENAIRCN_DIR" $green
cecho "OPENAIR_TARGETS = $OPENAIR_TARGETS" $green