Add support to use vswitch to handle V2V in PVVP SRIOV scenario
[nfvbench.git] / nfvbenchvm / dib / elements / nfvbenchvm / static / etc / rc.d / rc.local
1 #!/bin/bash
2
3 touch /var/lock/subsys/local
4
5 # Waiting for cloud-init to generate $TESTPMD_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 NICS=`lspci -D | grep Ethernet | cut -d' ' -f1 | xargs`
22 PCI_ADDRESS_1=`echo $NICS | awk '{ print $1 }'`
23 PCI_ADDRESS_2=`echo $NICS | awk '{ print $2 }'`
24 CPU_CORES=`grep -c ^processor /proc/cpuinfo`
25 CPU_MASKS=0x`echo "obase=16; 2 ^ $CPU_CORES - 1" | bc`
26 WORKER_CORES=`expr $CPU_CORES - 1`
27
28 # CPU isolation optimizations
29 echo 1 > /sys/bus/workqueue/devices/writeback/cpumask
30 echo 1 > /sys/devices/virtual/workqueue/cpumask
31 echo 1 > /proc/irq/default_smp_affinity
32 for irq in `ls /proc/irq/`; do
33     echo 1 > /proc/irq/$irq/smp_affinity
34 done
35 tuna -c $(seq -s, 1 1 $WORKER_CORES) --isolate
36
37 # Sometimes the interfaces on the loopback VM will use different drivers, e.g.
38 # one from vswitch which is virtio based, one is from SRIOV VF. In this case,
39 # we have to make sure the forwarder uses them in the right order, which is
40 # especially important if the VM is in a PVVP chain.
41 if [ -z $INTF_MAC1 ] && [ -z $INTF_MAC2 ]; then
42     NET_PATH=/sys/class/net
43     EXP_INTF_1=$(for f in $(ls $NET_PATH/); do if [ ! $(grep -o "$INTF_MAC1" $NET_PATH/$f/address) ]; then break; fi; done)
44     EXP_PCI_ADDRESS_1=$(basename $(readlink $NET_PATH/$EXP_INTF_1/device))
45     EXP_INTF_2=$(for f in $(ls $NET_PATH/); do if [ ! $(grep -o "$INTF_MAC2" $NET_PATH/$f/address) ]; then break; fi; done)
46     EXP_PCI_ADDRESS_2=$(basename $(readlink $NET_PATH/$EXP_INTF_2/device))
47     if [ "$PCI_ADDRESS_1" == "$EXP_PCI_ADDRESS_2" ] && [ "$PCI_ADDRESS_2" == "$EXP_PCI_ADDRESS_1" ]; then
48         # Interfaces are not coming in the expected order:
49         #     (1) Swap the MAC in the case of testpmd;
50         #     (2) Swap the interface order in the case of VPP;
51         TEMP=$PCI_ADDRESS_1; PCI_ADDRESS_1=$PCI_ADDRESS_2; PCI_ADDRESS_2=$TEMP
52         TEMP=$TG_MAC1; TG_MAC1=$TG_MAC2; TG_MAC2=$TEMP
53     fi
54 fi
55
56 # Configure the forwarder
57 if [ "$FORWARDER" == "testpmd" ]; then
58     echo "Configuring testpmd..."
59     if [ -z "`lsmod | grep igb_uio`" ]; then
60         modprobe uio
61         insmod /dpdk/igb_uio.ko
62     fi
63     # Binding ports to DPDK
64     /dpdk/dpdk-devbind.py -b igb_uio $PCI_ADDRESS_1
65     /dpdk/dpdk-devbind.py -b igb_uio $PCI_ADDRESS_2
66     screen -dmSL testpmd /dpdk/testpmd \
67                          -c $CPU_MASKS \
68                          -n 4 \
69                          -- \
70                              --burst=32 \
71                              --txd=256 \
72                              --rxd=1024 \
73                              --txqflags=0xf00 \
74                              --disable-hw-vlan \
75                              --eth-peer=0,$TG_MAC1 \
76                              --eth-peer=1,$TG_MAC2 \
77                              --forward-mode=mac \
78                              --nb-cores=$WORKER_CORES \
79                              --max-pkt-len=9000 \
80                              --cmdline-file=/dpdk/testpmd_cmd.txt
81 else
82     echo "Configuring vpp..."
83     cp /vpp/startup.conf /etc/vpp/startup.conf
84     cp /vpp/vm.conf /etc/vpp/vm.conf
85
86     sed -i "s/{{PCI_ADDRESS_1}}/$PCI_ADDRESS_1/g" /etc/vpp/startup.conf
87     sed -i "s/{{PCI_ADDRESS_2}}/$PCI_ADDRESS_2/g" /etc/vpp/startup.conf
88     sed -i "s/{{WORKER_CORES}}/$WORKER_CORES/g" /etc/vpp/startup.conf
89     service vpp start
90
91     INTFS=`vppctl show int | grep Ethernet | xargs`
92     INTF_1=`echo $INTFS | awk '{ print $1 }'`
93     INTF_2=`echo $INTFS | awk '{ print $4 }'`
94     sed -i "s/{{INTF_1}}/${INTF_1//\//\/}/g" /etc/vpp/vm.conf
95     sed -i "s/{{INTF_2}}/${INTF_2//\//\/}/g" /etc/vpp/vm.conf
96     sed -i "s/{{VNF_GATEWAY1_CIDR}}/${VNF_GATEWAY1_CIDR//\//\/}/g" /etc/vpp/vm.conf
97     sed -i "s/{{VNF_GATEWAY2_CIDR}}/${VNF_GATEWAY2_CIDR//\//\/}/g" /etc/vpp/vm.conf
98     sed -i "s/{{TG_MAC1}}/${TG_MAC1}/g" /etc/vpp/vm.conf
99     sed -i "s/{{TG_MAC2}}/${TG_MAC2}/g" /etc/vpp/vm.conf
100     sed -i "s/{{TG_NET1}}/${TG_NET1//\//\/}/g" /etc/vpp/vm.conf
101     sed -i "s/{{TG_NET2}}/${TG_NET2//\//\/}/g" /etc/vpp/vm.conf
102     sed -i "s/{{TG_GATEWAY1_IP}}/${TG_GATEWAY1_IP}/g" /etc/vpp/vm.conf
103     sed -i "s/{{TG_GATEWAY2_IP}}/${TG_GATEWAY2_IP}/g" /etc/vpp/vm.conf
104     service vpp restart
105 fi