nfvbenchvm: set mgmt interface MTU=1500 by default
[nfvbench.git] / nfvbenchvm / dib / elements / nfvbenchvm / static / etc / rc.d / rc.local.loopvm
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 forwarder..."
19 eval $(cat $NFVBENCH_CONF)
20 touch /nfvbench_configured.flag
21
22 # WE assume there are at least 2 cores available for the VM
23 CPU_CORES=$(grep -c ^processor /proc/cpuinfo)
24
25 # We need at least 1 admin core. 
26 if [ $CPU_CORES -le 2 ]; then
27     ADMIN_CORES=1
28 else
29     # If the number of cores is even we
30     # reserve 2 cores for admin (second being idle) so the number of
31     # workers is either 1 (if CPU_CORES is 2) or always even
32     if (( $CPU_CORES % 2 )); then
33         ADMIN_CORES=1
34     else
35         ADMIN_CORES=2
36     fi
37 fi
38 # 2 vcpus: AW (core 0: Admin, core 1: Worker)
39 # 3 vcpus: AWW (core 0: Admin, core 1,2: Worker)
40 # 4 vcpus: AWWU (core 0: Admin, core 1,2: Worker, core 3: Unused)
41 # 5 vcpus: AWWWW
42 # 6 vcpus: AWWWWU
43 WORKER_CORES=$(expr $CPU_CORES - $ADMIN_CORES)
44 # worker cores are all cores except the admin core (core 0) and the eventual unused core
45 # AW -> 1
46 # AWW -> 1,2
47 # AWWU -> 1,2
48 WORKER_CORE_LIST=$(seq -s, $ADMIN_CORES $WORKER_CORES)
49 # always use all cores
50 CORE_MASK=0x$(echo "obase=16; 2 ^ $CPU_CORES - 1" | bc)
51
52 logger "NFVBENCHVM: CPU_CORES=$CPU_CORES, ADMIN_CORES=$ADMIN_CORES, WORKER_CORES=$WORKER_CORES ($WORKER_CORE_LIST)"
53
54 # CPU isolation optimizations
55 echo 1 > /sys/bus/workqueue/devices/writeback/cpumask
56 echo 1 > /sys/devices/virtual/workqueue/cpumask
57 echo 1 > /proc/irq/default_smp_affinity
58 for irq in `ls /proc/irq/`; do
59     if [ -f /proc/irq/$irq/smp_affinity ]; then
60         echo 1 > /proc/irq/$irq/smp_affinity
61     fi
62 done
63
64 # Isolate all cores that are reserved for workers
65 tuna -c $WORKER_CORE_LIST --isolate
66
67 NET_PATH=/sys/class/net
68
69 get_pci_address() {
70     # device mapping for CentOS Linux 7:
71     # lspci:
72     #   00.03.0 Ethernet controller: Red Hat, Inc. Virtio network device
73     #   00.04.0 Ethernet controller: Red Hat, Inc. Virtio network device
74     # /sys/class/net:
75     # /sys/class/net/eth0 -> ../../devices/pci0000:00/0000:00:03.0/virtio0/net/eth0
76     # /sys/class/net/eth1 -> ../../devices/pci0000:00/0000:00:04.0/virtio1/net/eth1
77
78     mac=$1
79     for f in $(ls $NET_PATH/); do
80         if grep -q "$mac" $NET_PATH/$f/address; then
81             pci_addr=$(readlink $NET_PATH/$f | cut -d "/" -f5)
82             # some virtual interfaces match on MAC and do not have a PCI address
83             if [ "$pci_addr" -a "$pci_addr" != "N/A" ]; then
84                 # Found matching interface
85                 logger "NFVBENCHVM: found interface $f ($pci_addr) matching $mac"
86                 break
87             else
88                 pci_addr=""
89             fi
90         fi;
91     done
92     if [ -z "$pci_addr" ]; then
93         echo "ERROR: Cannot find pci address for MAC $mac" >&2
94         logger "NFVBENCHVM ERROR: Cannot find pci address for MAC $mac"
95         return 1
96     fi
97     echo $pci_addr
98     return 0
99 }
100
101 get_eth_port() {
102     # device mapping for CentOS Linux 7:
103     # lspci:
104     #   00.03.0 Ethernet controller: Red Hat, Inc. Virtio network device
105     #   00.04.0 Ethernet controller: Red Hat, Inc. Virtio network device
106     # /sys/class/net:
107     # /sys/class/net/eth0 -> ../../devices/pci0000:00/0000:00:03.0/virtio0/net/eth0
108     # /sys/class/net/eth1 -> ../../devices/pci0000:00/0000:00:04.0/virtio1/net/eth1
109
110     mac=$1
111     for f in $(ls $NET_PATH/); do
112         if grep -q "$mac" $NET_PATH/$f/address; then
113             eth_port=$(readlink $NET_PATH/$f | cut -d "/" -f8)
114             # some virtual interfaces match on MAC and do not have a PCI address
115             if [ "$eth_port" -a "$eth_port" != "N/A" ]; then
116                 # Found matching interface
117                 logger "NFVBENCHVM: found interface $f ($eth_port) matching $mac"
118                 break
119             else
120                 eth_port=""
121             fi
122         fi;
123     done
124     if [ -z "$eth_port" ]; then
125         echo "ERROR: Cannot find eth port for MAC $mac" >&2
126         logger "NFVBENCHVM ERROR: Cannot find eth port for MAC $mac"
127         return 1
128     fi
129     echo $eth_port
130     return 0
131 }
132
133 # Set VM MANAGEMENT port up and running
134 if [ $INTF_MGMT_CIDR ] && [ $INTF_MGMT_IP_GW ]; then
135     if [ $INTF_MAC_MGMT ]; then
136         ETH_PORT=$(get_eth_port $INTF_MAC_MGMT)
137     else
138         ETH_PORT="eth0"
139     fi
140
141     # By default, configure the MTU of the management interface to the
142     # conservative value of 1500: this will reduce the risk to get an
143     # unmanageable VM in some setups.
144     #
145     # To set the MTU to a different value, configure the INTF_MGMT_MTU variable
146     # in /etc/nfvbenchvm.conf.  If INTF_MGMT_MTU is set to the special value
147     # "auto", the MTU will not be configured and it will keep the value set by
148     # the hypervisor ("legacy" nfvbenchvm behavior).  If INTF_MGMT_MTU is unset,
149     # the MTU will be set to 1500.  In other cases, the MTU will be set to the
150     # value of INTF_MGMT_MTU.
151     #
152     if [[ -z "$INTF_MGMT_MTU" ]]; then
153         ip link set $ETH_PORT mtu 1500
154     elif [[ "$INTF_MGMT_MTU" != "auto" ]]; then
155         ip link set $ETH_PORT mtu $INTF_MGMT_MTU
156     fi
157
158     ip addr add $INTF_MGMT_CIDR dev $ETH_PORT
159     ip link set $ETH_PORT up
160     ip route add default via $INTF_MGMT_IP_GW dev $ETH_PORT
161 else
162     echo "INFO: VM management IP Addresses missing in $NFVBENCH_CONF"
163 fi
164
165 # Set dynamically interfaces mac values, if VM is spawn without using NFVBench
166 # and management interface is used on eth0
167 if [ -z "$INTF_MAC1" ] && [ -z "$INTF_MAC2" ]; then
168     INTF_MAC1=$(ip l show eth1 | grep -o -Ei '([a-fA-F0-9:]{17}|[a-fA-F0-9]{12}$)' | head -1)
169     INTF_MAC2=$(ip l show eth2 | grep -o -Ei '([a-fA-F0-9:]{17}|[a-fA-F0-9]{12}$)' | head -1)
170 fi
171
172
173 # Sometimes the interfaces on the loopback VM will use different drivers, e.g.
174 # one from vswitch which is virtio based, one is from SRIOV VF. In this case,
175 # we have to make sure the forwarder uses them in the right order, which is
176 # especially important if the VM is in a PVVP chain.
177 if [ $INTF_MAC1 ] && [ $INTF_MAC2 ]; then
178     PCI_ADDRESS_1=$(get_pci_address $INTF_MAC1)
179     PCI_ADDRESS_2=$(get_pci_address $INTF_MAC2)
180 else
181     echo "ERROR: VM MAC Addresses missing in $NFVBENCH_CONF"
182     logger "NFVBENCHVM ERROR: VM MAC Addresses missing in $NFVBENCH_CONF"
183 fi
184
185 if [ $PCI_ADDRESS_1 ] && [ $PCI_ADDRESS_2 ]; then
186     logger "NFVBENCHVM: Using pci $PCI_ADDRESS_1 ($INTF_MAC1)"
187     logger "NFVBENCHVM: Using pci $PCI_ADDRESS_2 ($INTF_MAC2)"
188     # active uio_pci_generic driver
189     modprobe uio_pci_generic
190     # Configure the forwarder
191     if [ "$FORWARDER" == "testpmd" ]; then
192         echo "Configuring testpmd..."
193         mkdir /dpdk
194         echo "set promisc all off" > /dpdk/testpmd_cmd.txt
195         # Binding ports to DPDK VFIO or UIO
196         dpdk-devbind -b vfio-pci $PCI_ADDRESS_1 || dpdk-devbind -b uio_pci_generic $PCI_ADDRESS_1
197         dpdk-devbind -b vfio-pci $PCI_ADDRESS_2 || dpdk-devbind -b uio_pci_generic $PCI_ADDRESS_2
198         screen -dmSL testpmd testpmd \
199                             -c $CORE_MASK \
200                             -n 4 \
201                             -- \
202                                 --nb-ports=2 \
203                                 --burst=32 \
204                                 --txd=256 \
205                                 --rxd=1024 \
206                                 --eth-peer=0,$TG_MAC1 \
207                                 --eth-peer=1,$TG_MAC2 \
208                                 --forward-mode=mac \
209                                 --nb-cores=$WORKER_CORES \
210                                 --txq=$VIF_MQ_SIZE \
211                                 --rxq=$VIF_MQ_SIZE \
212                                 --max-pkt-len=9000 \
213                                 --cmdline-file=/dpdk/testpmd_cmd.txt
214         echo "testpmd running in screen 'testpmd'"
215         logger "NFVBENCHVM: testpmd running in screen 'testpmd'"
216     elif [ "$FORWARDER" == "vpp" ]; then
217         echo "Configuring vpp..."
218         cp /vpp/startup.conf /etc/vpp/startup.conf
219         cp /vpp/vm.conf /etc/vpp/vm.conf
220
221         sed -i "s/{{PCI_ADDRESS_1}}/$PCI_ADDRESS_1/g" /etc/vpp/startup.conf
222         sed -i "s/{{PCI_ADDRESS_2}}/$PCI_ADDRESS_2/g" /etc/vpp/startup.conf
223         sed -i "s/{{WORKER_CORES}}/$WORKER_CORES/g" /etc/vpp/startup.conf
224         sed -i "s/{{VIF_MQ_SIZE}}/${VIF_MQ_SIZE}/g" /etc/vpp/startup.conf
225         sed -i "s/{{NUM_MBUFS}}/${NUM_MBUFS}/g" /etc/vpp/startup.conf
226         service vpp start
227         sleep 10
228
229         INTFS=`vppctl show int | grep Ethernet | xargs`
230         INTF_1=`echo $INTFS | awk '{ print $1 }'`
231         INTF_2=`echo $INTFS | awk '{ print $4 }'`
232         if [ -z "${TG_MAC1}" ]; then
233             # vm.conf does not support lines commented with #, so
234             # we need to remove the line to set the static ARP entry.
235             sed -i "/{{TG_MAC1}}/d" /etc/vpp/vm.conf
236         else
237             sed -i "s/{{TG_MAC1}}/${TG_MAC1}/g" /etc/vpp/vm.conf
238         fi
239         if [ -z "${TG_MAC2}" ]; then
240             sed -i "/{{TG_MAC2}}/d" /etc/vpp/vm.conf
241         else
242             sed -i "s/{{TG_MAC2}}/${TG_MAC2}/g" /etc/vpp/vm.conf
243         fi
244         sed -i "s/{{INTF_1}}/${INTF_1//\//\/}/g" /etc/vpp/vm.conf
245         sed -i "s/{{INTF_2}}/${INTF_2//\//\/}/g" /etc/vpp/vm.conf
246         sed -i "s/{{VNF_GATEWAY1_CIDR}}/${VNF_GATEWAY1_CIDR//\//\/}/g" /etc/vpp/vm.conf
247         sed -i "s/{{VNF_GATEWAY2_CIDR}}/${VNF_GATEWAY2_CIDR//\//\/}/g" /etc/vpp/vm.conf
248         sed -i "s/{{TG_NET1}}/${TG_NET1//\//\/}/g" /etc/vpp/vm.conf
249         sed -i "s/{{TG_NET2}}/${TG_NET2//\//\/}/g" /etc/vpp/vm.conf
250         sed -i "s/{{TG_GATEWAY1_IP}}/${TG_GATEWAY1_IP}/g" /etc/vpp/vm.conf
251         sed -i "s/{{TG_GATEWAY2_IP}}/${TG_GATEWAY2_IP}/g" /etc/vpp/vm.conf
252         service vpp restart
253         logger "NFVBENCHVM: vpp service restarted"
254     else
255         echo "ERROR: Unknown forwarder value. Accepted values: testpmd or vpp"
256         exit 1
257     fi
258 else
259     echo "ERROR: Cannot find PCI Address from MAC"
260     echo "$INTF_MAC1: $PCI_ADDRESS_1"
261     echo "$INTF_MAC2: $PCI_ADDRESS_2"
262     logger "NFVBENCHVM ERROR: Cannot find PCI Address from MAC"
263 fi
264
265 exit 0