utils.bash 43 KB
Newer Older
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
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
}


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)
gauthier's avatar
gauthier committed
35
IPTABLES=`which iptables`
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

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
}

gauthier's avatar
gauthier committed
72
73
74
75
76
77
78
79
80
81
82
echo_fatal() {
    local my_string=""
    until [ -z "$1" ]
    do
        my_string="$my_string$1"
        shift
        done
        echo_error "$my_string"
    exit -1
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
139
140
141
142
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 () {
    if [ -f $1 ]; then
        TIMESTAMP=`date +%Y-%m-%d.%Hh_%Mm_%Ss`
        NEWLOGFILE=$1.$TIMESTAMP
        mv $1 $NEWLOGFILE
        cat /dev/null > $1
143
        sync
144
145
146
147
148
149
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        nohup gzip -f -9 $NEWLOGFILE &
    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
               declare -x OPENAIR_DIR
               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
gauthier's avatar
gauthier committed
223
224
        echo_error "Assertion failed:  \"$1\""
        echo_fatal "File \"$0\", line $lineno"
225
226
227
228
229
230
231
    fi
}

test_command_install_lib() {
  # usage: test_command_install_package searched_binary package_to_be_installed_if_binary_not_found optional_option_to_apt_get_install
  if [ ! -f $1 ]; then
      echo_warning "$2 seems to be not installed, trying..."
gauthier's avatar
gauthier committed
232
      apt-get install $3 $2 -y
233
      if [ ! -f $1 ]; then
gauthier's avatar
gauthier committed
234
          echo_fatal "$1 unavailable"
235
236
237
238
239
240
241
242
243
      fi
  fi
  echo_success "$1 available"
}


test_command_install_package() {
  # usage: test_command_install_package searched_binary package_to_be_installed_if_binary_not_found optional_option_to_apt_get_install
  if [ $# -eq 2 ]; then
gauthier's avatar
gauthier committed
244
      command -v $1 >/dev/null 2>&1 || { echo_warning "Program $1 is not installed. Trying installing it." >&2; apt-get install $2 -y; command -v $1 >/dev/null 2>&1 || { echo_fatal "Program $1 is not installed. Aborting." >&2;};}
245
246
  else
      if [ $# -eq 3 ]; then
gauthier's avatar
gauthier committed
247
          command -v $1 >/dev/null 2>&1 || { echo_warning "Program $1 is not installed. Trying installing it (apt-get install $3 $2)." >&2; apt-get install $3 $2 -y; command -v $1 >/dev/null 2>&1 || { echo_fatal "Program $1 is not installed. Aborting." >&2; };}
248
      else
gauthier's avatar
gauthier committed
249
          echo_fatal "test_command_install_package: BAD PARAMETER"
250
251
252
253
254
255
256
      fi
  fi
  echo_success "$1 available"
}

test_command_install_script() {
  # usage: test_command_install_script searched_binary script_to_be_invoked_if_binary_not_found
gauthier's avatar
gauthier committed
257
  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; };}
258
259
260
261
262
263
264
265
  echo_success "$1 available"
}

start_openswitch_daemon() {
  rmmod -s bridge
  if [[ -e "/lib/modules/`uname -r`/extra/openvswitch.ko" ]] ; then
      bash_exec "insmod /lib/modules/`uname -r`/extra/openvswitch.ko" > /dev/null 2>&1
  else
gauthier's avatar
gauthier committed
266
      echo_fatal "/lib/modules/`uname -r`/extra/openvswitch.ko not found, exiting"
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
  is_process_started "ovsdb-server"
  if [ $? -ne 0 ]
  then
      ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock --remote=db:Open_vSwitch,manager_options --pidfile --detach
      wait_process_started "ovsdb-server"
  fi
  # To be done after installation
  # ovs-vsctl    --no-wait init
  is_process_started "ovs-vswitchd"
  if [ $? -ne 0 ]
  then
      ovs-vswitchd --pidfile --detach
      wait_process_started "ovs-vswitchd"
  fi
}

stop_openswitch_daemon() {
    pkill ovs-vswitchd
    pkill ovsdb-server
    sync
    if  ! is_process_started ovs-vswitchd  ; then
        pkill -9 ovs-vswitchd
    fi
    if ! is_process_started ovsdb-server ; then
        pkill -9 ovsdb-server
    fi
    rmmod -f openvswitch
}

check_for_epc_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAI_EPC/oai_epc ]
        then
gauthier's avatar
gauthier committed
300
301
        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"
302
303
304
305
306
307
        fi
}

check_for_sgw_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAI_SGW/oai_sgw ]
    then
gauthier's avatar
gauthier committed
308
309
        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"
310
311
312
313
314
315
    fi
}

check_for_mme_executable() {
    if [ ! -f $OPENAIR3_DIR/OPENAIRMME/objs/OAISIM_MME/oaisim_mme ]
    then
gauthier's avatar
gauthier committed
316
317
        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"
318
319
320
321
322
    fi
}

check_for_root_rights() {
    if [[ $EUID -ne 0 ]]; then
gauthier's avatar
gauthier committed
323
        echo_fatal "This script must be run as root" 1>&2
324
325
326
    fi
}

gauthier's avatar
gauthier committed
327
is_openvswitch_interface() {
gauthier's avatar
gauthier committed
328
329
330
   if [ "a$1" == "a" ]; then
       return 0
   fi
gauthier's avatar
gauthier committed
331
   if [ "a${1:0:3}" == "aeth" ]; then
gauthier's avatar
gauthier committed
332
333
       return 0;
   else 
gauthier's avatar
gauthier committed
334
       if [ "a${1:0:4}" == "awlan" ]; then
gauthier's avatar
gauthier committed
335
336
           return 0;
       else
gauthier's avatar
gauthier committed
337
           if [ "a${1:0:4}" == "awifi" ]; then
gauthier's avatar
gauthier committed
338
               return 0;
339
340
341
342
           else
               if [ "a${1:0:4}" == "anone" ]; then
                   return 0;
               fi
gauthier's avatar
gauthier committed
343
344
345
346
347
           fi
       fi
   fi
   return 1;
}
348

gauthier's avatar
gauthier committed
349
is_real_interface() {
gauthier's avatar
gauthier committed
350
351
352
   if [ "a$1" == "a" ]; then
       return 0
   fi
gauthier's avatar
gauthier committed
353
   IF=`cat /etc/udev/rules.d/70-persistent-net.rules | grep $1 | sed 's/^.*NAME=//' | tr -d '"'`
gauthier's avatar
gauthier committed
354
355
   if [ "$IF" == "$1" ]; then
       if [ "a${1:0:3}" == "aeth" ]; then
gauthier's avatar
gauthier committed
356
357
           return 1;
       else 
gauthier's avatar
gauthier committed
358
           if [ "a${1:0:4}" == "awlan" ]; then
gauthier's avatar
gauthier committed
359
360
               return 1;
           else
gauthier's avatar
gauthier committed
361
               if [ "a${1:0:4}" == "awifi" ]; then
gauthier's avatar
gauthier committed
362
363
364
365
366
367
368
369
                   return 1;
               fi
           fi
       fi
   fi
   return 0
}

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
is_vlan_interface() {
   if [ "a$1" == "a" ]; then
       return 0
   fi
   if [[ $1 == *.* ]]
   then
       interface_name=`echo $1 | cut -f1 -d '.'`
       vlan=`echo $1 | 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
               return 1;
           else 
               if [ "a${interface_name:0:4}" == "awlan" ]; then
                   return 1;
               else
                   if [ "a${interface_name:0:4}" == "awifi" ]; then
                       return 1;
                   fi
               fi
           fi
       fi
   fi
   return 0
}

gauthier's avatar
gauthier committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

delete_openvswitch_interface() {
  is_openvswitch_interface $1 
  if [ $? -eq 1 ]; then
      ifconfig $1 down  > /dev/null 2>&1
      tunctl -d $1      > /dev/null 2>&1
  fi
}

create_openvswitch_interface() {
  is_openvswitch_interface $1
  if [ $? -eq 1 ]; then
      bash_exec "tunctl -t $1"
  fi
}

build_enb_vlan_network() {
    # create vlan interface
414
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1_MME
gauthier's avatar
Updates    
gauthier committed
415
    if [ $? -eq 1 ]; then
416
417
418
419
        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
gauthier's avatar
gauthier committed
420
        sync
421
        bash_exec "vconfig add $interface_name $vlan"
gauthier's avatar
gauthier committed
422
        sync
423
        bash_exec "ifconfig  $ENB_INTERFACE_NAME_FOR_S1_MME up"
424
        sync
425
        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"
gauthier's avatar
gauthier committed
426
    else
427
        echo_fatal "BAD INTERFACE NAME FOR ENB S1-MME $ENB_INTERFACE_NAME_FOR_S1_MME"' (waiting for ethx.y, wlanx.y or wifix.y)'
gauthier's avatar
gauthier committed
428
    fi;
gauthier's avatar
Updates    
gauthier committed
429
    
430
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1U
gauthier's avatar
Updates    
gauthier committed
431
    if [ $? -eq 1 ]; then
432
433
434
435
        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
gauthier's avatar
gauthier committed
436
        sync
437
        bash_exec "vconfig add $interface_name $vlan"
gauthier's avatar
gauthier committed
438
        sync
439
        bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1U up"
440
        sync
441
        bash_exec "ip -4 addr add  $ENB_IPV4_ADDRESS_FOR_S1U/$ENB_IPV4_NETMASK_FOR_S1U dev $ENB_INTERFACE_NAME_FOR_S1U"
gauthier's avatar
gauthier committed
442
443
        sync
    else
444
        echo_fatal "BAD INTERFACE NAME FOR ENB S1U $ENB_INTERFACE_NAME_FOR_S1U"' (waiting for ethx.y, wlanx.y or wifix.y)'
gauthier's avatar
gauthier committed
445
446
447
448
    fi;
}

clean_enb_vlan_network() {
449
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1_MME
gauthier's avatar
Updates    
gauthier committed
450
    if [ $? -eq 1 ]; then
451
452
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
gauthier's avatar
gauthier committed
453
    fi;
gauthier's avatar
Updates    
gauthier committed
454
    
455
    is_vlan_interface $ENB_INTERFACE_NAME_FOR_S1U
gauthier's avatar
Updates    
gauthier committed
456
    if [ $? -eq 1 ]; then
457
458
        ifconfig    $ENB_INTERFACE_NAME_FOR_S1U down > /dev/null 2>&1
        vconfig rem $ENB_INTERFACE_NAME_FOR_S1U > /dev/null 2>&1
gauthier's avatar
gauthier committed
459
460
461
462
463
464
    fi;
    sync;
}

test_enb_vlan_network() {
    # TEST INTERFACES
gauthier's avatar
gauthier committed
465
466
467
468
    #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;
gauthier's avatar
gauthier committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    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]    +--------------+
    cat $OPENAIRCN_DIR/$OBJ_DIR/Makefile | grep CFLAGS\ \=\  | grep DENABLE_USE_NETFILTER_FOR_SGI
    if [ $? -ne 0 ]
    then
        export ENABLE_USE_NETFILTER_FOR_SGI=0
    else
        export ENABLE_USE_NETFILTER_FOR_SGI=1
    fi

    cat $OPENAIRCN_DIR/$OBJ_DIR/Makefile | grep CFLAGS\ \=\  | grep DENABLE_USE_RAW_FOR_SGI
    if [ $? -ne 0 ]
    then
        export ENABLE_USE_RAW_FOR_SGI=0
    else
        export ENABLE_USE_RAW_FOR_SGI=1
    fi
    
    bash_exec "modprobe 8021q"
    
    # create vlan interface
513
    is_vlan_interface $MME_INTERFACE_NAME_FOR_S1_MME
gauthier's avatar
gauthier committed
514
    if [ $? -eq 1 ]; then
515
516
517
518
        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
gauthier's avatar
gauthier committed
519
        sync
520
        bash_exec "vconfig add $interface_name $vlan"
gauthier's avatar
gauthier committed
521
        sync
522
        bash_exec "ifconfig  $MME_INTERFACE_NAME_FOR_S1_MME up"
523
        sync
524
        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"
gauthier's avatar
gauthier committed
525
    else
526
        echo_fatal "BAD INTERFACE NAME FOR SGW S1-MME $MME_INTERFACE_NAME_FOR_S1_MME"' (waiting for ethx.y, wlanx.y or wifix.y)'
gauthier's avatar
gauthier committed
527
    fi;
gauthier's avatar
gauthier committed
528
    
529
    is_vlan_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
gauthier's avatar
gauthier committed
530
    if [ $? -eq 1 ]; then
531
532
533
534
        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
gauthier's avatar
gauthier committed
535
        sync
536
        bash_exec "vconfig add $interface_name $vlan"
gauthier's avatar
gauthier committed
537
        sync
538
        bash_exec "ifconfig  $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP up"
539
        sync
540
        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"
gauthier's avatar
gauthier committed
541
542
        sync
    else
543
        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)'
gauthier's avatar
gauthier committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    fi;
    
    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"

    if [ $ENABLE_USE_NETFILTER_FOR_SGI -eq 1 ]; then

        bash_exec "modprobe nf_conntrack"
        bash_exec "modprobe nf_conntrack_ftp"

        ######################################################
        # PREROUTING
        ######################################################
        # We restore the mark following the CONNMARK mark. In fact, it does a simple MARK=CONNMARK
        # where MARK is the standard mark (usable by tc)
        # In French: Cette option de cible restaure le paquet marqué dans la marque de connexion
        # comme défini par CONNMARK. Un masque peut aussi être défini par l'option --mask.
        # Si une option mask est placée, seules les options masquées seront placées.
        # Notez que cette option de cible n'est valide que dans la table mangle.
        bash_exec "$IPTABLES -t mangle -A PREROUTING -j CONNMARK --restore-mark"

        # TEST bash_exec "$IPTABLES -t mangle -A PREROUTING -m mark --mark 0 -i $PGW_INTERFACE_NAME_FOR_SGI -j MARK --set-mark 15"
        # We set the mark of the initial packet as value of the conntrack mark for all the packets of the connection.
        # This mark will be restore for the other packets by the first rule of POSTROUTING --restore-mark).
        bash_exec "$IPTABLES -t mangle -A PREROUTING -j CONNMARK --save-mark"


        ######################################################
        # POSTROUTING
        ######################################################

        # MARK=CONNMARK
        bash_exec "iptables -A POSTROUTING -t mangle -o tap0 -j CONNMARK --restore-mark"
        # If we’ve got a mark no need to get further[
        bash_exec "iptables -A POSTROUTING -t mangle -o tap0 -m mark ! --mark 0 -j ACCEPT"

        #bash_exec "iptables -A POSTROUTING -p tcp --dport 21 -t mangle -j MARK --set-mark 1"
        #bash_exec "iptables -A POSTROUTING -p tcp --dport 80 -t mangle -j MARK --set-mark 2"

        # We set the mark of the initial packet as value of the conntrack mark for all the packets
        # of the connection. This mark will be restore for the other packets by the first rule
        # of POSTROUTING (–restore-mark).
        bash_exec "iptables -A POSTROUTING -t mangle -j CONNMARK --save-mark"

        bash_exec "iptables -A PREROUTING  -t mangle -j CONNMARK --restore-mark"

        # We restore the mark following the CONNMARK mark.
        # In fact, it does a simple MARK=CONNMARK where MARK is the standard mark (usable by tc)
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -m mark ! --mark 0 -j CONNMARK --restore-mark"

        # If we’ve got a mark no need to get further[1]
        #TEST bash_exec "$IPTABLES -A OUTPUT -t mangle -p icmp -j MARK --set-mark 14"
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -m mark ! --mark 0 -j ACCEPT"


        # We set the mark of the initial packet as value of the conntrack mark for all the packets of the connection.
        # This mark will be restore for the other packets by the first rule of OUTPUT (–restore-mark).
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -j CONNMARK --save-mark"

        ######################################################
        # NETFILTER QUEUE
        ######################################################
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 5 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 6 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 7 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 8 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 9 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 10 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 11 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 12 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 13 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 14 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 15 -j NFQUEUE --queue-num 1"

        #echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables #To disable Iptables in the bridge.
        #Raw table: Some years ago appeared a new tables in Iptables.
        #This table can be used to avoid packets (connection really) to enter the NAT table:
        # iptables -t raw -I PREROUTING -i BRIDGE -s x.x.x.x -j NOTRACK.
        #bash_exec "$IPTABLES -t nat -A POSTROUTING -o $PGW_INTERFACE_NAME_FOR_SGI -j SNAT --to-source $PGW_IP_ADDR_FOR_SGI"
    else
        # # 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
gauthier's avatar
gauthier committed
636
            ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
gauthier's avatar
gauthier committed
637
638
639
640
            vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i > /dev/null 2>&1
            sync
            bash_exec "vconfig add $PGW_INTERFACE_NAME_FOR_SGI $i"
            sync
641
642
            bash_exec "ifconfig  $PGW_INTERFACE_NAME_FOR_SGI.$i up"
            sync
gauthier's avatar
gauthier committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
            # 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
    fi

    bash_exec "ip link set $PGW_INTERFACE_NAME_FOR_SGI promisc on"    
}

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"

692
693
    ifconfig    $MME_INTERFACE_NAME_FOR_S1_MME down > /dev/null 2>&1
    vconfig rem $MME_INTERFACE_NAME_FOR_S1_MME      > /dev/null 2>&1
gauthier's avatar
Updates    
gauthier committed
694

695
696
    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
gauthier's avatar
Updates    
gauthier committed
697
698


gauthier's avatar
gauthier committed
699
700
701
    for i in 5 6 7 8 9 10 11 12 13 14 15
    do
        # delete vlan interface
gauthier's avatar
gauthier committed
702
703
        ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
        vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i      > /dev/null 2>&1
gauthier's avatar
gauthier committed
704
    done
gauthier's avatar
gauthier committed
705
    #ip link set $PGW_INTERFACE_NAME_FOR_SGI down > /dev/null 2>&1
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
}

build_openvswitch_network() {
    start_openswitch_daemon
    # REMINDER:
    #                                                                           hss.eur
    #                                                                             |
    #        +-----------+          +------+              +-----------+           v   +----------+
    #        |  eNB      +------+   |  ovs | VLAN 1+------+    MME    +----+      +---+   HSS    |
    #        |           |cpenb0+------------------+cpmme0|           |    +------+   |          |
    #        |           +------+   |bridge|       +------+           +----+      +---+          |
    #        |           |upenb0+-------+  |              |           |               +----------+
    #        |           +------+   |   |  |              +-+-------+-+
    #        |           |          |   |  +----------------| s11mme|---+
    #        |           |          |   |                   +---+---+   |
gauthier's avatar
gauthier committed
721
    #        |           |          |   |             (optional)|       |ovs bridge is optional
722
723
724
725
726
727
728
729
730
731
732
733
734
    #        +-----------+          |   |                   +---+---+   |
    #                               +---|------------------ | s11sgw|---+        router.eur
    #                                   |                 +-+-------+-+              |   +--------------+
    #                                   |                 |  S+P-GW   |              v   |   ROUTER     |
    #                                   |  VLAN2   +------+           +-------+     +----+              +----+
    #                                   +----------+upsgw0|           |sgi    +-...-+    |              |    +---...Internet
    #                                              +------+           +-------+     +----+              +----+
    #                                                     |           |      11 VLANS    |              |
    #                                                     +-----------+   ids=[5..15]    +--------------+
    #
    ##################################################
    # build bridge between eNB and MME/SPGW
    ##################################################
gauthier's avatar
gauthier committed
735
736
737
738
739
740
741
    create_openvswitch_interface $ENB_INTERFACE_NAME_FOR_S1_MME
    create_openvswitch_interface $ENB_INTERFACE_NAME_FOR_S1U
    create_openvswitch_interface $MME_INTERFACE_NAME_FOR_S1_MME
    create_openvswitch_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
    create_openvswitch_interface $MME_INTERFACE_NAME_FOR_S11_MME
    create_openvswitch_interface $SGW_INTERFACE_NAME_FOR_S11
    
742
743
744
745
746
747
748
749
750
    bash_exec "ovs-vsctl add-br       $BRIDGE"
    bash_exec "ovs-vsctl add-port     $BRIDGE $ENB_INTERFACE_NAME_FOR_S1_MME        tag=1"
    bash_exec "ovs-vsctl add-port     $BRIDGE $MME_INTERFACE_NAME_FOR_S1_MME        tag=1"
    bash_exec "ovs-vsctl add-port     $BRIDGE $ENB_INTERFACE_NAME_FOR_S1U           tag=2"
    bash_exec "ovs-vsctl add-port     $BRIDGE $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP tag=2"
    bash_exec "ovs-vsctl add-port     $BRIDGE $MME_INTERFACE_NAME_FOR_S11_MME       tag=3"
    bash_exec "ovs-vsctl add-port     $BRIDGE $SGW_INTERFACE_NAME_FOR_S11           tag=3"

    bash_exec "ifconfig $MME_INTERFACE_NAME_FOR_S1_MME promisc up"
gauthier's avatar
gauthier committed
751
    bash_exec "ifconfig $MME_INTERFACE_NAME_FOR_S1_MME $MME_IPV4_ADDRESS_FOR_S1_MME netmask `cidr2mask $MME_IPV4_NETMASK_FOR_S1_MME` promisc up"
752
    bash_exec "ifconfig $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP promisc up"
gauthier's avatar
gauthier committed
753
    bash_exec "ifconfig $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP $SGW_IPV4_ADDRESS_FOR_S1U_S12_S4_UP netmask `cidr2mask $SGW_IPV4_NETMASK_FOR_S1U_S12_S4_UP` promisc up"
754
755

    bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1_MME promisc up"
gauthier's avatar
gauthier committed
756
    bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1_MME $ENB_IPV4_ADDRESS_FOR_S1_MME netmask `cidr2mask $ENB_IPV4_NETMASK_FOR_S1_MME` promisc up"
757
    bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1U promisc up"
gauthier's avatar
gauthier committed
758
    bash_exec "ifconfig $ENB_INTERFACE_NAME_FOR_S1U $ENB_IPV4_ADDRESS_FOR_S1U netmask `cidr2mask $ENB_IPV4_NETMASK_FOR_S1U` promisc up"
759
760

    bash_exec "ifconfig $MME_INTERFACE_NAME_FOR_S11_MME promisc up"
gauthier's avatar
gauthier committed
761
762
763
    bash_exec "ifconfig $MME_INTERFACE_NAME_FOR_S11_MME $MME_IPV4_ADDRESS_FOR_S11_MME netmask `cidr2mask $MME_IPV4_NETMASK_FOR_S11_MME` promisc up"
    bash_exec "ifconfig $SGW_INTERFACE_NAME_FOR_S11 promisc up"
    bash_exec "ifconfig $SGW_INTERFACE_NAME_FOR_S11 $SGW_IPV4_ADDRESS_FOR_S11 netmask `cidr2mask $SGW_IPV4_NETMASK_FOR_S11` promisc up"
764
765
766
767
}

test_openvswitch_network() {

gauthier's avatar
gauthier committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
    # 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;
    
    
783
    ## TEST NETWORK BETWEEN ENB-MME-SP-GW
gauthier's avatar
gauthier committed
784
785
    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
786
787
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
gauthier's avatar
gauthier committed
788
        echo_fatal 'NETWORK ERROR CONFIGURATION (openvswitch) between ENB and MME S1'
789
    else
gauthier's avatar
gauthier committed
790
        echo_success 'NETWORK TEST SUCCESS (openvswitch) between ENB and MME S1'
791
792
793
794

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

gauthier's avatar
gauthier committed
795
796
    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
797
798
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
gauthier's avatar
gauthier committed
799
        echo_fatal 'NETWORK ERROR CONFIGURATION (openvswitch) between ENB and S-GW S1-U'
800
    else
gauthier's avatar
gauthier committed
801
        echo_success 'NETWORK TEST SUCCESS (openvswitch) between ENB and S-GW S1-U'
802
803
804
    fi
    pkill iperf 2>&1 > /dev/null

gauthier's avatar
gauthier committed
805
806
    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
807
808
    if [ $? -eq 0 ]; then
        pkill iperf 2>&1 > /dev/null
gauthier's avatar
gauthier committed
809
        echo_fatal 'NETWORK ERROR CONFIGURATION (openvswitch) between MME and S-GW S11'
810
    else
gauthier's avatar
gauthier committed
811
        echo_success 'NETWORK TEST SUCCESS (openvswitch) between MME and S-GW S11'
812
813
814
815
816
    fi
    pkill iperf 2>&1 > /dev/null
    return 0
}

gauthier's avatar
gauthier committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
clean_openvswitch_network() {
    ##################################################
    # del bridge between eNB and MME/SPGW
    ##################################################
    delete_openvswitch_interface $ENB_INTERFACE_NAME_FOR_S1_MME
    delete_openvswitch_interface $ENB_INTERFACE_NAME_FOR_S1U
    delete_openvswitch_interface $MME_INTERFACE_NAME_FOR_S1_MME
    delete_openvswitch_interface $SGW_INTERFACE_NAME_FOR_S1U_S12_S4_UP
    delete_openvswitch_interface $MME_INTERFACE_NAME_FOR_S11_MME
    delete_openvswitch_interface $SGW_INTERFACE_NAME_FOR_S11
    if  is_process_started ovs-vswitchd  ; then
        ovs-vsctl del-br $BRIDGE > /dev/null 2>&1
    fi
    stop_openswitch_daemon
}

build_epc_ovs_network() {
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

    cat $OPENAIRCN_DIR/$OBJ_DIR/Makefile | grep CFLAGS\ \=\  | grep DENABLE_USE_NETFILTER_FOR_SGI
    if [ $? -ne 0 ]
    then
        export ENABLE_USE_NETFILTER_FOR_SGI=0
    else
        export ENABLE_USE_NETFILTER_FOR_SGI=1
    fi

    cat $OPENAIRCN_DIR/$OBJ_DIR/Makefile | grep CFLAGS\ \=\  | grep DENABLE_USE_RAW_FOR_SGI
    if [ $? -ne 0 ]
    then
        export ENABLE_USE_RAW_FOR_SGI=0
    else
        export ENABLE_USE_RAW_FOR_SGI=1
    fi

    build_openvswitch_network

gauthier's avatar
gauthier committed
853
    ping -c 1 router.eur > /dev/null || { echo_fatal "router.eur does not respond to ping" >&2 ; }
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
    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"

    if [ $ENABLE_USE_NETFILTER_FOR_SGI -eq 1 ]; then

        bash_exec "modprobe nf_conntrack"
        bash_exec "modprobe nf_conntrack_ftp"

        ######################################################
        # PREROUTING
        ######################################################
        # We restore the mark following the CONNMARK mark. In fact, it does a simple MARK=CONNMARK
        # where MARK is the standard mark (usable by tc)
        # In French: Cette option de cible restaure le paquet marqué dans la marque de connexion
        # comme défini par CONNMARK. Un masque peut aussi être défini par l'option --mask.
        # Si une option mask est placée, seules les options masquées seront placées.
        # Notez que cette option de cible n'est valide que dans la table mangle.
        bash_exec "$IPTABLES -t mangle -A PREROUTING -j CONNMARK --restore-mark"

        # TEST bash_exec "$IPTABLES -t mangle -A PREROUTING -m mark --mark 0 -i $PGW_INTERFACE_NAME_FOR_SGI -j MARK --set-mark 15"
        # We set the mark of the initial packet as value of the conntrack mark for all the packets of the connection.
        # This mark will be restore for the other packets by the first rule of POSTROUTING --restore-mark).
        bash_exec "$IPTABLES -t mangle -A PREROUTING -j CONNMARK --save-mark"


        ######################################################
        # POSTROUTING
        ######################################################

        # MARK=CONNMARK
        bash_exec "iptables -A POSTROUTING -t mangle -o tap0 -j CONNMARK --restore-mark"
        # If we’ve got a mark no need to get further[
        bash_exec "iptables -A POSTROUTING -t mangle -o tap0 -m mark ! --mark 0 -j ACCEPT"

        #bash_exec "iptables -A POSTROUTING -p tcp --dport 21 -t mangle -j MARK --set-mark 1"
        #bash_exec "iptables -A POSTROUTING -p tcp --dport 80 -t mangle -j MARK --set-mark 2"

        # We set the mark of the initial packet as value of the conntrack mark for all the packets
        # of the connection. This mark will be restore for the other packets by the first rule
        # of POSTROUTING (–restore-mark).
        bash_exec "iptables -A POSTROUTING -t mangle -j CONNMARK --save-mark"

        bash_exec "iptables -A PREROUTING  -t mangle -j CONNMARK --restore-mark"

        # We restore the mark following the CONNMARK mark.
        # In fact, it does a simple MARK=CONNMARK where MARK is the standard mark (usable by tc)
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -m mark ! --mark 0 -j CONNMARK --restore-mark"

        # If we’ve got a mark no need to get further[1]
        #TEST bash_exec "$IPTABLES -A OUTPUT -t mangle -p icmp -j MARK --set-mark 14"
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -m mark ! --mark 0 -j ACCEPT"


        # We set the mark of the initial packet as value of the conntrack mark for all the packets of the connection.
        # This mark will be restore for the other packets by the first rule of OUTPUT (–restore-mark).
        #bash_exec "$IPTABLES -A OUTPUT -t mangle -j CONNMARK --save-mark"




        ######################################################
        # NETFILTER QUEUE
        ######################################################
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 5 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 6 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 7 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 8 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 9 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 10 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 11 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 12 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 13 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 14 -j NFQUEUE --queue-num 1"
        bash_exec "$IPTABLES -t mangle -A PREROUTING -i $PGW_INTERFACE_NAME_FOR_SGI -m connmark  --mark 15 -j NFQUEUE --queue-num 1"

        #echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables #To disable Iptables in the bridge.
        #Raw table: Some years ago appeared a new tables in Iptables.
        #This table can be used to avoid packets (connection really) to enter the NAT table:
        # iptables -t raw -I PREROUTING -i BRIDGE -s x.x.x.x -j NOTRACK.

        #bash_exec "$IPTABLES -t nat -A POSTROUTING -o $PGW_INTERFACE_NAME_FOR_SGI -j SNAT --to-source $PGW_IP_ADDR_FOR_SGI"
    else
        # # 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
gauthier's avatar
gauthier committed
947
948
            ifconfig    $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
            vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i      > /dev/null 2>&1
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
            sync
            bash_exec "vconfig add $PGW_INTERFACE_NAME_FOR_SGI $i"
            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
    fi


    bash_exec "ip link set $PGW_INTERFACE_NAME_FOR_SGI promisc on"

    ##################################################
    # build bridge between SPGW and Internet
    ##################################################

    # # 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;}'`
    # if [ $IP_ADDR ]; then
    #   bash_exec "ip -4 addr del $IP_ADDR dev $PGW_INTERFACE_NAME_FOR_SGI"
    # fi
    #
    # # remove all ipv6 address from PGW_INTERFACE_NAME_FOR_SGI
    # IP_ADDR="not empty"
    # until [ "$IP_ADDR"x == "x" ]; do
    #   IP_ADDR=`ifconfig $PGW_INTERFACE_NAME_FOR_SGI | grep 'inet6' | head -1 | tr '\n' ' ' | sed -n '1h;1!H;${;g;s/^[ \t]*//g;s/[ \t]*$//g;p;}' | cut -d ' ' -f3`
    #   if [ $IP_ADDR ]; then
    #     bash_exec "ip -6 addr del $IP_ADDR dev $PGW_INTERFACE_NAME_FOR_SGI"
    #   fi
    # done
}

gauthier's avatar
gauthier committed
984
test_epc_ovs_network() {
985
    # Get MAC address of router.eur
gauthier's avatar
gauthier committed
986
987
    ping -c 1 hss.eur > /dev/null || { echo_fatal "hss.eur does not respond to ping" >&2 ; }
    ping -c 1 router.eur > /dev/null || { echo_fatal "router.eur does not respond to ping" >&2 ; }
988
989
990
    test_openvswitch_network
}

gauthier's avatar
gauthier committed
991
clean_epc_ovs_network() {
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
    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
gauthier's avatar
gauthier committed
1025
1026
1027
1028
    
    
    for i in 5 6 7 8 9 10 11 12 13 14 15
    do
gauthier's avatar
gauthier committed
1029
1030
        ifconfig $PGW_INTERFACE_NAME_FOR_SGI.$i down > /dev/null 2>&1
        vconfig rem $PGW_INTERFACE_NAME_FOR_SGI.$i   > /dev/null 2>&1
gauthier's avatar
gauthier committed
1031
1032
    done
    
1033
1034
1035
    clean_openvswitch_network
}

1036

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
###########################################################
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
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