nfvbenchvm: set mgmt interface MTU=1500 by default
[nfvbench.git] / nfvbenchvm / dib / elements / nfvbenchvm / static / etc / rc.d / rc.local.generator
1 #!/bin/bash
2
3 touch /var/lock/subsys/local
4
5 # Waiting for cloud-init to generate $NFVBENCH_CONF, retry 60 seconds
6 NFVBENCH_CONF=/etc/nfvbenchvm.conf
7 retry=30
8 until [ $retry -eq 0 ]; do
9     if [ -f $NFVBENCH_CONF ]; then break; fi
10     retry=$[$retry-1]
11     sleep 2
12 done
13 if [ ! -f $NFVBENCH_CONF ]; then
14     exit 0
15 fi
16
17 # Parse and obtain all configurations
18 echo "Generating configurations for NFVbench and TRex..."
19 eval $(cat $NFVBENCH_CONF)
20 touch /nfvbench_configured.flag
21
22 # Add DNS entry
23 if [ $DNS_SERVERS ]; then
24     IFS="," read -a dns <<< $DNS_SERVERS
25     for d in "${dns[@]}"; do
26         echo "nameserver $d" >> /etc/resolv.conf
27     done
28 fi
29
30 # CPU isolation optimizations
31 echo 1 > /sys/bus/workqueue/devices/writeback/cpumask
32 echo 1 > /sys/devices/virtual/workqueue/cpumask
33 echo 1 > /proc/irq/default_smp_affinity
34 for irq in `ls /proc/irq/`; do
35     if [ -f /proc/irq/$irq/smp_affinity ]; then
36         echo 1 > /proc/irq/$irq/smp_affinity
37     fi
38 done
39
40 NET_PATH=/sys/class/net
41
42 get_eth_port() {
43     # device mapping for CentOS Linux 7:
44     # lspci:
45     #   00.03.0 Ethernet controller: Red Hat, Inc. Virtio network device
46     #   00.04.0 Ethernet controller: Red Hat, Inc. Virtio network device
47     # /sys/class/net:
48     # /sys/class/net/eth0 -> ../../devices/pci0000:00/0000:00:03.0/virtio0/net/eth0
49     # /sys/class/net/eth1 -> ../../devices/pci0000:00/0000:00:04.0/virtio1/net/eth1
50
51     mac=$1
52     for f in $(ls $NET_PATH/); do
53         if grep -q "$mac" $NET_PATH/$f/address; then
54             eth_port=$(readlink $NET_PATH/$f | cut -d "/" -f8)
55             # some virtual interfaces match on MAC and do not have a PCI address
56             if [ "$eth_port" -a "$eth_port" != "N/A" ]; then
57                 # Found matching interface
58                 logger "NFVBENCHVM: found interface $f ($eth_port) matching $mac"
59                 break
60             else
61                 eth_port=""
62             fi
63         fi;
64     done
65     if [ -z "$eth_port" ]; then
66         echo "ERROR: Cannot find eth port for MAC $mac" >&2
67         logger "NFVBENCHVM ERROR: Cannot find eth port for MAC $mac"
68         return 1
69     fi
70     echo $eth_port
71     return 0
72 }
73
74 # Set VM MANAGEMENT port up and running
75 if [ $INTF_MGMT_CIDR ] && [ $INTF_MGMT_IP_GW ]; then
76     if [ $INTF_MAC_MGMT ]; then
77         ETH_PORT=$(get_eth_port $INTF_MAC_MGMT)
78     elif [ "$CLOUD_DETAIL" ] && [ "$PORT_MGMT_NAME" ]; then
79         $INTF_MAC_MGMT=$(openstack --os-cloud $CLOUD_DETAIL port list | grep $PORT_MGMT_NAME | grep -o -Ei '([a-fA-F0-9:]{17}|[a-fA-F0-9]{12}$)' | head -1)
80         ETH_PORT=$(get_eth_port $INTF_MAC_MGMT)
81     else
82         ETH_PORT=""
83     fi
84     if [ -z "$ETH_PORT" ]; then
85         echo "ERROR: Cannot find eth port for management port" >&2
86         logger "NFVBENCHVM ERROR: Cannot find eth port for management port"
87         return 1
88     fi
89
90     # By default, configure the MTU of the management interface to the
91     # conservative value of 1500: this will reduce the risk to get an
92     # unmanageable VM in some setups.
93     #
94     # To set the MTU to a different value, configure the INTF_MGMT_MTU variable
95     # in /etc/nfvbenchvm.conf.  If INTF_MGMT_MTU is set to the special value
96     # "auto", the MTU will not be configured and it will keep the value set by
97     # the hypervisor ("legacy" nfvbenchvm behavior).  If INTF_MGMT_MTU is unset,
98     # the MTU will be set to 1500.  In other cases, the MTU will be set to the
99     # value of INTF_MGMT_MTU.
100     #
101     if [[ -z "$INTF_MGMT_MTU" ]]; then
102         ip link set $ETH_PORT mtu 1500
103     elif [[ "$INTF_MGMT_MTU" != "auto" ]]; then
104         ip link set $ETH_PORT mtu $INTF_MGMT_MTU
105     fi
106
107     ip addr add $INTF_MGMT_CIDR dev $ETH_PORT
108     ip link set $ETH_PORT up
109     ip route add default via $INTF_MGMT_IP_GW dev $ETH_PORT
110 else
111     echo "INFO: VM management IP Addresses missing in $NFVBENCH_CONF"
112 fi
113
114 /nfvbench/configure-nfvbench.sh
115
116 if [ $ACTION ]; then
117     /nfvbench/start-nfvbench.sh $ACTION
118 else
119     /nfvbench/start-nfvbench.sh
120 fi
121
122 exit 0