nfvbenchvm: refactor wait for VPP service
[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 wait_vpp_service() {
186     # Wait for at most wait_max=$1 seconds until VPP service is ready.  Exit
187     # with code 1 if timeout is reached.
188     # 
189     # Because VPP systemd unit has Type=simple, systemctl will report the
190     # service to be active has soon as it is forked.  This does not mean that
191     # the service is ready, and actually it takes some times before vppctl can
192     # succesfully connect to VPP client socket /run/vpp/cli.sock.
193     local wait_max=$1
194
195     local wait_time=0
196     while ! vppctl show int; do
197         if [[ $wait_time -ge $wait_max ]]; then
198             # Log error to both system log and standard error output
199             logger -s "NFVBENCHVM ERROR: VPP service still not ready after $wait_max seconds." \
200                       "Exiting $(basename $0)."
201             exit 1
202         fi
203         sleep 1
204         wait_time=$(( wait_time + 1 ))
205     done
206 }
207
208 if [ $PCI_ADDRESS_1 ] && [ $PCI_ADDRESS_2 ]; then
209     logger "NFVBENCHVM: Using pci $PCI_ADDRESS_1 ($INTF_MAC1)"
210     logger "NFVBENCHVM: Using pci $PCI_ADDRESS_2 ($INTF_MAC2)"
211     # active uio_pci_generic driver
212     modprobe uio_pci_generic
213     # Configure the forwarder
214     if [ "$FORWARDER" == "testpmd" ]; then
215         echo "Configuring testpmd..."
216         mkdir /dpdk
217         echo "set promisc all off" > /dpdk/testpmd_cmd.txt
218         # Binding ports to DPDK VFIO or UIO
219         dpdk-devbind -b vfio-pci $PCI_ADDRESS_1 || dpdk-devbind -b uio_pci_generic $PCI_ADDRESS_1
220         dpdk-devbind -b vfio-pci $PCI_ADDRESS_2 || dpdk-devbind -b uio_pci_generic $PCI_ADDRESS_2
221         screen -dmSL testpmd testpmd \
222                             -c $CORE_MASK \
223                             -n 4 \
224                             -- \
225                                 --nb-ports=2 \
226                                 --burst=32 \
227                                 --txd=256 \
228                                 --rxd=1024 \
229                                 --eth-peer=0,$TG_MAC1 \
230                                 --eth-peer=1,$TG_MAC2 \
231                                 --forward-mode=mac \
232                                 --nb-cores=$WORKER_CORES \
233                                 --txq=$VIF_MQ_SIZE \
234                                 --rxq=$VIF_MQ_SIZE \
235                                 --max-pkt-len=9000 \
236                                 --cmdline-file=/dpdk/testpmd_cmd.txt
237         echo "testpmd running in screen 'testpmd'"
238         logger "NFVBENCHVM: testpmd running in screen 'testpmd'"
239     elif [ "$FORWARDER" == "vpp" ]; then
240         echo "Configuring vpp..."
241         cp /vpp/startup.conf /etc/vpp/startup.conf
242         cp /vpp/vm.conf /etc/vpp/vm.conf
243
244         sed -i "s/{{PCI_ADDRESS_1}}/$PCI_ADDRESS_1/g" /etc/vpp/startup.conf
245         sed -i "s/{{PCI_ADDRESS_2}}/$PCI_ADDRESS_2/g" /etc/vpp/startup.conf
246         sed -i "s/{{WORKER_CORES}}/$WORKER_CORES/g" /etc/vpp/startup.conf
247         sed -i "s/{{VIF_MQ_SIZE}}/${VIF_MQ_SIZE}/g" /etc/vpp/startup.conf
248         sed -i "s/{{NUM_MBUFS}}/${NUM_MBUFS}/g" /etc/vpp/startup.conf
249         systemctl start vpp
250         # Wait until VPP service is ready for at most 30 seconds
251         wait_vpp_service 30
252
253         VPPCTL_OUTPUT=$(vppctl show int)
254         INTFS=$(echo "$VPPCTL_OUTPUT" | grep Ethernet | xargs)
255         INTF_1=$(echo $INTFS | awk '{ print $1 }')
256         INTF_2=$(echo $INTFS | awk '{ print $4 }')
257         if [[ -z "$INTF_1" ]] || [[ -z "$INTF_2" ]]; then
258             # Log error to both system log and standard error output
259             logger -s "NFVBENCHVM DEBUG: \"vppctl show int\" output:"
260             logger -s "NFVBENCHVM DEBUG: $VPPCTL_OUTPUT"
261             logger -s "NFVBENCHVM ERROR: vppctl does not show the two Ethernet interfaces we expect." \
262                       "Exiting $(basename $0)."
263             exit 1
264         fi
265         if [ -z "${TG_MAC1}" ]; then
266             # vm.conf does not support lines commented with #, so
267             # we need to remove the line to set the static ARP entry.
268             sed -i "/{{TG_MAC1}}/d" /etc/vpp/vm.conf
269         else
270             sed -i "s/{{TG_MAC1}}/${TG_MAC1}/g" /etc/vpp/vm.conf
271         fi
272         if [ -z "${TG_MAC2}" ]; then
273             sed -i "/{{TG_MAC2}}/d" /etc/vpp/vm.conf
274         else
275             sed -i "s/{{TG_MAC2}}/${TG_MAC2}/g" /etc/vpp/vm.conf
276         fi
277         sed -i "s/{{INTF_1}}/${INTF_1//\//\/}/g" /etc/vpp/vm.conf
278         sed -i "s/{{INTF_2}}/${INTF_2//\//\/}/g" /etc/vpp/vm.conf
279         sed -i "s/{{VNF_GATEWAY1_CIDR}}/${VNF_GATEWAY1_CIDR//\//\/}/g" /etc/vpp/vm.conf
280         sed -i "s/{{VNF_GATEWAY2_CIDR}}/${VNF_GATEWAY2_CIDR//\//\/}/g" /etc/vpp/vm.conf
281         sed -i "s/{{TG_NET1}}/${TG_NET1//\//\/}/g" /etc/vpp/vm.conf
282         sed -i "s/{{TG_NET2}}/${TG_NET2//\//\/}/g" /etc/vpp/vm.conf
283         sed -i "s/{{TG_GATEWAY1_IP}}/${TG_GATEWAY1_IP}/g" /etc/vpp/vm.conf
284         sed -i "s/{{TG_GATEWAY2_IP}}/${TG_GATEWAY2_IP}/g" /etc/vpp/vm.conf
285         systemctl restart vpp
286         logger "NFVBENCHVM: vpp service restarted"
287     else
288         echo "ERROR: Unknown forwarder value. Accepted values: testpmd or vpp"
289         exit 1
290     fi
291 else
292     echo "ERROR: Cannot find PCI Address from MAC"
293     echo "$INTF_MAC1: $PCI_ADDRESS_1"
294     echo "$INTF_MAC2: $PCI_ADDRESS_2"
295     logger "NFVBENCHVM ERROR: Cannot find PCI Address from MAC"
296 fi
297
298 exit 0