aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/network.sh
blob: e50f52a68b94f0020d46fb088047ae0862500cc5 (plain)
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
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
72
73
74
75
76
77
78
79
80
81
82
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
143
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
#!/bin/bash
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
function clear_forward_rejct_rules()
{
    while sudo iptables -nL FORWARD --line-number|grep -E 'REJECT +all +-- +0.0.0.0/0 +0.0.0.0/0 +reject-with icmp-port-unreachable'|head -1|awk '{print $1}'|xargs sudo iptables -D FORWARD; do :; done
}

function setup_bridge_net()
{
    net_name=$1
    nic=$2

    sudo ifconfig $nic up

    sudo virsh net-destroy $net_name
    sudo virsh net-undefine $net_name

    sed -e "s/REPLACE_NAME/$net_name/g" \
        -e "s/REPLACE_NIC/$nic/g" \
    $COMPASS_DIR/deploy/template/network/bridge_nic.xml \
    > $WORK_DIR/network/$net_name.xml

    sudo virsh net-define $WORK_DIR/network/$net_name.xml
    sudo virsh net-start $net_name
}

function recover_bridge_net()
{
    net_name=$1

    sudo virsh net-start $net_name
}

function save_network_info()
{
    sudo ovs-vsctl list-br |grep br-external
    br_exist=$?
    external_nic=`ip route |grep '^default'|awk '{print $5F}'`
    route_info=`ip route |grep -Eo '^default via [^ ]+'`
    ip_info=`ip addr show $external_nic|grep -Eo '[^ ]+ brd [^ ]+ '`
    if [ $br_exist -eq 0 ]; then
        if [ "$external_nic" != "br-external" ]; then
            sudo ip link set br-external up
            sudo ovs-vsctl --may-exist add-port br-external $external_nic
            sudo ip addr flush $external_nic
            sudo ip addr add $ip_info dev br-external
            sudo ip route add $route_info dev br-external
        fi
    else
        sudo ovs-vsctl add-br br-external
        sudo ip link set br-external up
        sudo ovs-vsctl add-port br-external $external_nic
        sudo ip addr flush $external_nic
        sudo ip addr add $ip_info dev br-external
        sudo ip route add $route_info dev br-external
    fi

    # Configure OS_MGMT_NIC when openstack external network and mgmt network use different nics
    if [[ x"$OS_MGMT_NIC" != "x" ]]; then
        sudo ovs-vsctl --may-exist add-port br-external $OS_MGMT_NIC
        sudo ip link set $OS_MGMT_NIC up
        sudo ip addr flush $OS_MGMT_NIC
    fi
}

function setup_bridge_external()
{
    sudo virsh net-destroy external
    sudo virsh net-undefine external

    #save_network_info
    sed -e "s/REPLACE_NAME/external/g" \
        -e "s/REPLACE_OVS/br-external_nat/g" \
    $COMPASS_DIR/deploy/template/network/bridge_ovs.xml \
    > $WORK_DIR/network/external.xml

    sudo virsh net-define $WORK_DIR/network/external.xml
    sudo virsh net-start external

    python $COMPASS_DIR/deploy/setup_vnic.py
}

function recover_bridge_external()
{
    sudo virsh net-start external

    python $COMPASS_DIR/deploy/setup_vnic.py
}

function setup_nat_net() {
    net_name=$1
    gw=$2
    mask=$3
    ip_start=$4
    ip_end=$5

    sudo virsh net-destroy $net_name
    sudo virsh net-undefine $net_name
    # create install network
    sed -e "s/REPLACE_BRIDGE/br_$net_name/g" \
        -e "s/REPLACE_NAME/$net_name/g" \
        -e "s/REPLACE_GATEWAY/$gw/g" \
        -e "s/REPLACE_MASK/$mask/g" \
        -e "s/REPLACE_START/$ip_start/g" \
        -e "s/REPLACE_END/$ip_end/g" \
        $COMPASS_DIR/deploy/template/network/nat.xml \
        > $WORK_DIR/network/$net_name.xml

    sudo virsh net-define $WORK_DIR/network/$net_name.xml
    sudo virsh net-start $net_name
}

function recover_nat_net() {
    net_name=$1

    sudo virsh net-start $net_name
}

function setup_virtual_net() {
  setup_nat_net install $INSTALL_GW $INSTALL_MASK
  setup_nat_net external_nat $EXT_NAT_GW $EXT_NAT_MASK $EXT_NAT_IP_START $EXT_NAT_IP_END
}

function recover_virtual_net() {
  recover_nat_net install
}

function setup_baremetal_net() {
  if [[ -z $INSTALL_NIC ]]; then
    exit 1
  fi
  sudo ifconfig $INSTALL_NIC up
  sudo ifconfig $INSTALL_NIC $INSTALL_GW
}

function recover_baremetal_net() {
  if [[ -z $INSTALL_NIC ]]; then
    exit 1
  fi
  recover_bridge_net install
}

function setup_network_boot_scripts() {
    sudo cp $COMPASS_DIR/deploy/network.sh /usr/sbin/network_setup
    sudo chmod +777 /usr/sbin/network_setup
    sudo cat << EOF >> /usr/sbin/network_setup

sleep 2
#save_network_info
clear_forward_rejct_rules
EOF
    sudo chmod 755 /usr/sbin/network_setup

    egrep -R "^/usr/sbin/network_setup" /etc/rc.local
    if [[ $? != 0 ]]; then
        sudo sed -i '/^exit 0/i\/usr\/sbin\/network_setup' /etc/rc.local
    fi
}

function create_nets() {

    # create install network
    setup_"$TYPE"_net

    # create external network
#    setup_bridge_external
    clear_forward_rejct_rules

    setup_network_boot_scripts
}

function recover_nets() {
    recover_nat_net mgmt

    # recover install network
    recover_"$TYPE"_net

    # recover external network
    recover_bridge_external
    clear_forward_rejct_rules
}
exporting the generated JAR to ``/verify/service/build`` (overwrite the existing file) #. under ``/verify/src/main/webapp/json/`` create a file ``<type>.json``. This file represents a JSON schema (see `here <http://json-schema.org/>`__ the official documentation). For compatibility with the other functions it is mandatory to support an array as the root of the configuration, but feel free to specify all the other constraints as needed. A sample of ``<type>.json`` to describe an empty configuration could be the following: ``json { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Type", "description": "This is a generic type", "type": "array", "items": { "type": "object" }, "minItems": 0, "maxItems": 0, "uniqueItems": true }`` #. in the package ``it.polito.escape.verify.validation`` (i.e. under ``src/main/java/it/polito/escape/verify/validation``) create a new class file named ``<Type>Validator.java`` (please pay attention to the naming convention here: ``<Type>`` is the function type used in the previous step capitalized, followed by the suffix ``Validator``) which implements ``ValidationInterface``. This class represents a custom validator for the newly introduced type and allows for more complex constraints, which is not possible to express through a JSON schema file. The validate method that has to be implemented is given the following objects: - ``Graph graph`` represents the nffg that the object node belongs to; - ``Node node`` represents the node that the object configuration belongs to; - ``Configuration configuration`` represents the parsed configuration. It is sufficient to call the method ``getConfiguration`` on the ``configuration`` object to get a ``JsonNode`` (Jackson's class) and iterate over the various fields. In case a configuration is not valid please throw a new ``ValidationException`` passing a descriptive failure message. Adding a custom validator is not strictly necessary whenever a JSON schema is thought to be sufficient. Note though that, other than the mandatory validation against a schema, whenever a custom validator is not found a default validation is triggered, i.e. the value of every JSON property must refer to the name of an existing node in the working graph/nffg. If this is not the desired behavior it is suggested to write a custom validator with looser constraints. #. customize the class generator and add the support for the newly introduced type: - open the file ``/verify/service/src/tests/j-verigraph-generator/config.py`` and edit the following dictionaries: - ``devices_to_classes`` --> add the following entry: ``"<type>" : "<Type>"`` if you followed these instructions carefully the name of the class implementing the function ``<type>`` should be ``<Type>.java`` under the package ``mcnet.netobjs``. - ``devices_to_configuration_methods`` --> add the following entry: ``"<type>" : "configurationMethod"`` if ``<type>`` is a middlebox it should have a configuration method contained in the implementation ``<Type>.java`` under the package ``mcnet.netobjs``. - ``devices_initialization``: add the following entry: ``"<type>" : ["param1", "param2"]`` if ``<type>`` requires any parameter when it gets instanciated please enter them in the form of a list. Make sure that these parameters refer to existing keys contained in the configuration schema file (see step 3). For instance the type ``webclient`` requires the name of a webserver it wants to communicate with. This parameter is passed in the configuration of a weblient by setting a property ``webserver`` to the name of the desired webserver. The value of this property gets extracted and used by the test generator automatically. - ``convert_configuration_property_to_ip`` --> add the following entry: ``"<type>" : ["key", "value"]`` Note that both ``key`` and ``value`` are optional and it is critical to set them only if needed. Since the Z3 provider used for testing works with IP addresses in this dictionary you have to indicate whether it is needed an automatic convertion from names to IP addresses: - in case the keyword ``key`` is used every key of the JSON configuration parsed will be prepended with the string ``ip_``; - in case the keyword ``value`` is used every value of the JSON configuration parsed will be prepended with the string ``ip_``; - in case the list does not contain neither ``key`` nor ``value`` the original configuration won't be touched. - open the file ``/verify/service/src/tests/j-verigraph-generator/test_class_generator.py`` and under the "switch" case in the form of a series of ifs used to configure middle-boxes that starts at line #239 add a branch like the following with the logic to generate the Java code for --> ``elif nodes_types[i] == "<type>":`` You can take inspiration from the other branches to see how to serialize Java code. Note that this addition to the "switch" statement is not needed if ``<type>`` is not a middlebox or it does not need to be configured. #. Restart the web service.