示例#1
0
class TestVfOffload(TestCase):

    supported_vf_driver = ['pci-stub', 'vfio-pci']

    def set_up_all(self):
        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) > 1, "Insufficient ports")
        self.vm0 = None

        # set vf assign method and vf driver
        self.vf_driver = self.get_suite_cfg()['vf_driver']
        if self.vf_driver is None:
            self.vf_driver = 'pci-stub'
        self.verify(self.vf_driver in self.supported_vf_driver, "Unsupported vf driver")
        if self.vf_driver == 'pci-stub':
            self.vf_assign_method = 'pci-assign'
        else:
            self.vf_assign_method = 'vfio-pci'
            self.dut.send_expect('modprobe vfio-pci', '#')

        self.setup_2pf_2vf_1vm_env_flag = 0
        self.setup_2pf_2vf_1vm_env(driver='')
        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.portMask = utils.create_mask([self.vm0_dut_ports[0]])
        self.vm0_testpmd = PmdOutput(self.vm_dut_0)

        
    def set_up(self):
        pass

    def setup_2pf_2vf_1vm_env(self, driver='default'):

        self.used_dut_port_0 = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_0, 1, driver=driver)
        self.sriov_vfs_port_0 = self.dut.ports_info[self.used_dut_port_0]['vfs_port']
        self.used_dut_port_1 = self.dut_ports[1]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_1, 1, driver=driver)
        self.sriov_vfs_port_1 = self.dut.ports_info[self.used_dut_port_1]['vfs_port']

        try:

            for port in self.sriov_vfs_port_0:
                port.bind_driver(self.vf_driver)

            for port in self.sriov_vfs_port_1:
                port.bind_driver(self.vf_driver)

            time.sleep(1)
            vf0_prop = {'opt_host': self.sriov_vfs_port_0[0].pci}
            vf1_prop = {'opt_host': self.sriov_vfs_port_1[0].pci}

            if driver == 'igb_uio':
                # start testpmd without the two VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s' % {'vf0': self.sriov_vfs_port_0[0].pci,
                                                       'vf1': self.sriov_vfs_port_1[0].pci}
                self.host_testpmd.start_testpmd("1S/2C/2T", eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = VM(self.dut, 'vm0', 'vf_offload')
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf0_prop)
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf1_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            self.setup_2pf_2vf_1vm_env_flag = 1
        except Exception as e:
            self.destroy_2pf_2vf_1vm_env()
            raise Exception(e)

    def destroy_2pf_2vf_1vm_env(self):
        if getattr(self, 'vm0', None):
            #destroy testpmd in vm0
            self.vm0_testpmd = None
            self.vm0_dut_ports = None
            #destroy vm0
            self.vm0.stop()
            self.dut.virt_exit()
            self.vm0 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port_0', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_0)
            port = self.dut.ports_info[self.used_dut_port_0]['port']
            port.bind_driver()
            self.used_dut_port_0 = None

        if getattr(self, 'used_dut_port_1', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_1)
            port = self.dut.ports_info[self.used_dut_port_1]['port']
            port.bind_driver()
            self.used_dut_port_1 = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver()

        self.setup_2pf_2vf_1vm_env_flag = 0

    def checksum_enablehw(self, port, dut):
        dut.send_expect("port stop all", "testpmd>")
        dut.send_expect("csum set ip hw %d" % port, "testpmd>")
        dut.send_expect("csum set udp hw %d" % port, "testpmd>")
        dut.send_expect("csum set tcp hw %d" % port, "testpmd>")
        dut.send_expect("csum set sctp hw %d" % port, "testpmd>")
        dut.send_expect("port start all", "testpmd>")

    def checksum_enablesw(self, port, dut):
        dut.send_expect("port stop all", "testpmd>")
        dut.send_expect("csum set ip sw %d" % port, "testpmd>")
        dut.send_expect("csum set udp sw %d" % port, "testpmd>")
        dut.send_expect("csum set tcp sw %d" % port, "testpmd>")
        dut.send_expect("csum set sctp sw %d" % port, "testpmd>")
        dut.send_expect("port start all", "testpmd>")

    def checksum_validate(self, packets_sent, packets_expected):
        """
        Validate the checksum.
        """
        tx_interface = self.tester.get_interface(self.tester.get_local_port(self.dut_ports[0]))
        rx_interface = self.tester.get_interface(self.tester.get_local_port(self.dut_ports[0]))

        sniff_src = self.vm0_testpmd.get_port_mac(0)
        checksum_pattern = re.compile("chksum.*=.*(0x[0-9a-z]+)")

        chksum = dict()
        result = dict()

        self.tester.send_expect("scapy", ">>> ")

        for packet_type in packets_expected.keys():
            self.tester.send_expect("p = %s" % packets_expected[packet_type], ">>>")
            out = self.tester.send_expect("p.show2()", ">>>")
            chksums = checksum_pattern.findall(out)
            chksum[packet_type] = chksums
            print packet_type, ": ", chksums

        self.tester.send_expect("exit()", "#")

        self.tester.scapy_background()
        self.tester.scapy_append('p = sniff(filter="ether src %s", iface="%s", count=%d)' % (sniff_src, rx_interface, len(packets_sent)))
        self.tester.scapy_append('nr_packets=len(p)')
        self.tester.scapy_append('reslist = [p[i].sprintf("%IP.chksum%;%TCP.chksum%;%UDP.chksum%;%SCTP.chksum%") for i in range(nr_packets)]')
        self.tester.scapy_append('import string')
        self.tester.scapy_append('RESULT = string.join(reslist, ",")')

        # Send packet.
        self.tester.scapy_foreground()

        for packet_type in packets_sent.keys():
            self.tester.scapy_append('sendp([%s], iface="%s")' % (packets_sent[packet_type], tx_interface))

        self.tester.scapy_execute()
        out = self.tester.scapy_get_result()
        packets_received = out.split(',')
        self.verify(len(packets_sent) == len(packets_received), "Unexpected Packets Drop")

        for packet_received in packets_received:
            ip_checksum, tcp_checksum, udp_checksup, sctp_checksum = packet_received.split(';')
            print "ip_checksum: ", ip_checksum, "tcp_checksum:, ", tcp_checksum, "udp_checksup: ", udp_checksup, "sctp_checksum: ", sctp_checksum

            packet_type = ''
            l4_checksum = ''
            if tcp_checksum != '??':
                packet_type = 'TCP'
                l4_checksum = tcp_checksum
            elif udp_checksup != '??':
                packet_type = 'UDP'
                l4_checksum = udp_checksup
            elif sctp_checksum != '??':
                packet_type = 'SCTP'
                l4_checksum = sctp_checksum

            if ip_checksum != '??':
                packet_type = 'IP/' + packet_type
                if chksum[packet_type] != [ip_checksum, l4_checksum]:
                    result[packet_type] = packet_type + " checksum error"
            else:
                packet_type = 'IPv6/' + packet_type
                if chksum[packet_type] != [l4_checksum]:
                    result[packet_type] = packet_type + " checksum error"

        return result

    def test_checksum_offload_enable(self):
        """
        Enable HW checksum offload.
        Send packet with incorrect checksum,
        can rx it and report the checksum error,
        verify forwarded packets have correct checksum.
        """
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK, "--portmask=%s " %
                                      (self.portMask) + "--enable-rx-cksum " + "" +
                                      "--port-topology=loop")
        self.vm0_testpmd.execute_cmd('set fwd csum')

        time.sleep(2)
        port_id_0 = 0
        mac = self.vm0_testpmd.get_port_mac(0)

        sndIP = '10.0.0.1'
        sndIPv6 = '::1'
        pkts = {'IP/UDP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IP(src="%s", chksum=0xf)/UDP(chksum=0xf)/("X"*46)' % (mac, sndIP),
                'IP/TCP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IP(src="%s", chksum=0xf)/TCP(chksum=0xf)/("X"*46)' % (mac, sndIP),
                'IP/SCTP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IP(src="%s", chksum=0xf)/SCTP(chksum=0xf)/("X"*48)' % (mac, sndIP),
                'IPv6/UDP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IPv6(src="%s")/UDP(chksum=0xf)/("X"*46)' % (mac, sndIPv6),
                'IPv6/TCP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IPv6(src="%s")/TCP(chksum=0xf)/("X"*46)' % (mac, sndIPv6)}

        expIP = sndIP
        expIPv6 = sndIPv6
        pkts_ref = {'IP/UDP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IP(src="%s")/UDP()/("X"*46)' % (mac, expIP),
                    'IP/TCP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IP(src="%s")/TCP()/("X"*46)' % (mac, expIP),
                    'IP/SCTP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IP(src="%s")/SCTP()/("X"*48)' % (mac, expIP),
                    'IPv6/UDP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IPv6(src="%s")/UDP()/("X"*46)' % (mac, expIPv6),
                    'IPv6/TCP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IPv6(src="%s")/TCP()/("X"*46)' % (mac, expIPv6)}

        if self.nic in ['redrockcanyou', 'atwood']:
            del pkts['IP/SCTP']
            del pkts_ref['IP/SCTP']

        self.checksum_enablehw(0,self.vm_dut_0)

        self.vm0_testpmd.execute_cmd('start')
        result = self.checksum_validate(pkts, pkts_ref)

        # Validate checksum on the receive packet
        out = self.vm0_testpmd.execute_cmd('stop')
        bad_ipcsum = self.vm0_testpmd.get_pmd_value("Bad-ipcsum:", out)
        bad_l4csum = self.vm0_testpmd.get_pmd_value("Bad-l4csum:", out)
        self.verify(bad_ipcsum == 3, "Bad-ipcsum check error")
        self.verify(bad_l4csum == 5, "Bad-l4csum check error")

        self.verify(len(result) == 0, string.join(result.values(), ","))

    def test_checksum_offload_disable(self):
        """
        Enable SW checksum offload.
        Send same packet with incorrect checksum and verify checksum is valid.
        """

        self.vm0_testpmd.start_testpmd(VM_CORES_MASK, "--portmask=%s " %
                                      (self.portMask) + "--enable-rx-cksum " +
                                      "--port-topology=loop")
        self.vm0_testpmd.execute_cmd('set fwd csum')

        time.sleep(2)

        mac = self.vm0_testpmd.get_port_mac(0)
        sndIP = '10.0.0.1'
        sndIPv6 = '::1'
        sndPkts = {'IP/UDP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IP(src="%s",chksum=0xf)/UDP(chksum=0xf)/("X"*46)' % (mac, sndIP),
                   'IP/TCP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IP(src="%s",chksum=0xf)/TCP(chksum=0xf)/("X"*46)' % (mac, sndIP),
                   'IPv6/UDP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IPv6(src="%s")/UDP(chksum=0xf)/("X"*46)' % (mac, sndIPv6),
                   'IPv6/TCP': 'Ether(dst="%s", src="52:00:00:00:00:00")/IPv6(src="%s")/TCP(chksum=0xf)/("X"*46)' % (mac, sndIPv6)}

        expIP = sndIP
        expIPv6 = sndIPv6
        expPkts = {'IP/UDP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IP(src="%s")/UDP()/("X"*46)' % (mac, expIP),
                   'IP/TCP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IP(src="%s")/TCP()/("X"*46)' % (mac, expIP),
                   'IPv6/UDP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IPv6(src="%s")/UDP()/("X"*46)' % (mac, expIPv6),
                   'IPv6/TCP': 'Ether(dst="02:00:00:00:00:00", src="%s")/IPv6(src="%s")/TCP()/("X"*46)' % (mac, expIPv6)}

        self.checksum_enablesw(0, self.vm_dut_0)

        self.vm0_testpmd.execute_cmd('start')
        result = self.checksum_validate(sndPkts, expPkts)

        # Validate checksum on the receive packet
        out = self.vm0_testpmd.execute_cmd('stop')
        bad_ipcsum = self.vm0_testpmd.get_pmd_value("Bad-ipcsum:", out)
        bad_l4csum = self.vm0_testpmd.get_pmd_value("Bad-l4csum:", out)
        self.verify(bad_ipcsum == 2, "Bad-ipcsum check error")
        self.verify(bad_l4csum == 4, "Bad-l4csum check error")

        self.verify(len(result) == 0, string.join(result.values(), ","))

    def tcpdump_start_sniffing(self, ifaces=[]):
        """
        Start tcpdump in the background to sniff the tester interface where
        the packets are transmitted to and from the self.dut.
        All the captured packets are going to be stored in a file for a
        post-analysis.
        """

        for iface in ifaces:
            command = ('tcpdump -w tcpdump_{0}.pcap -i {0} 2>tcpdump_{0}.out &').format(iface)
            self.tester.send_expect('rm -f tcpdump_{0}.pcap', '#').format(iface)
            self.tester.send_expect(command, '#')

    def tcpdump_stop_sniff(self):
        """
        Stop the tcpdump process running in the background.
        """
        self.tester.send_expect('killall tcpdump', '#')
        time.sleep(1)
        self.tester.send_expect('echo "Cleaning buffer"', '#')
        time.sleep(1)

    def tcpdump_command(self, command):
        """
        Send a tcpdump related command and return an integer from the output.
        """

        result = self.tester.send_expect(command, '#')
        print result
        return int(result.strip())

    def number_of_packets(self, iface):
        """
        By reading the file generated by tcpdump it counts how many packets are
        forwarded by the sample app and received in the self.tester. The sample app
        will add a known MAC address for the test to look for.
        """

        command = ('tcpdump -A -nn -e -v -r tcpdump_{iface}.pcap 2>/dev/null | ' +
                   'grep -c "seq"')
        return self.tcpdump_command(command.format(**locals()))

    def test_tso(self):
        """
        TSO IPv4 TCP, IPv6 TCP testing.
        """
        tx_interface = self.tester.get_interface(self.tester.get_local_port(self.dut_ports[0]))
        rx_interface = self.tester.get_interface(self.tester.get_local_port(self.dut_ports[1]))

        self.frame_sizes = [128, 1458]
        self.headers_size = HEADER_SIZE['eth'] + HEADER_SIZE['ip'] + HEADER_SIZE['tcp']
        padding = self.frame_sizes[0] - self.headers_size

        self.tester.send_expect("ethtool -K %s rx off tx off tso off gso off gro off lro off" % tx_interface, "# ")
        self.tester.send_expect("ip l set %s up" % tx_interface, "# ")

        self.portMask = utils.create_mask([self.vm0_dut_ports[0]])
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK, "--portmask=%s " %
                                      (self.portMask) + "--enable-rx-cksum " +
                                      "" + 
                                      "--port-topology=loop")

        mac = self.vm0_testpmd.get_port_mac(0)

        self.checksum_enablehw(0,self.vm_dut_0)
        self.vm0_testpmd.execute_cmd("tso set 800 %d" % self.vm0_dut_ports[1])
        self.vm0_testpmd.execute_cmd("set fwd csum")
        self.vm0_testpmd.execute_cmd("start")

        self.tester.scapy_foreground()
        time.sleep(5)

        # IPv4 tcp test

        self.tcpdump_start_sniffing([tx_interface, rx_interface])
        self.tester.scapy_append('sendp([Ether(dst="%s",src="52:00:00:00:00:00")/IP(src="192.168.1.1",dst="192.168.1.2")/TCP(sport=1021,dport=1021)/("X"*%s)], iface="%s")' % (mac, padding, tx_interface))
        out = self.tester.scapy_execute()
        out = self.vm0_testpmd.execute_cmd("show port stats all")
        print out
        self.tcpdump_stop_sniff()
        rx_stats = self.number_of_packets(rx_interface)
        if (rx_stats == 2):
            self.verify(1, "Pass")

        # IPv6 tcp test

        self.tcpdump_start_sniffing([tx_interface, rx_interface])
        self.tester.scapy_append('sendp([Ether(dst="%s", src="52:00:00:00:00:00")/IPv6(src="FE80:0:0:0:200:1FF:FE00:200", dst="3555:5555:6666:6666:7777:7777:8888:8888")/TCP(sport=1021,dport=1021)/("X"*%s)], iface="%s")' % (mac, padding, tx_interface))
        out = self.tester.scapy_execute()
        out = self.vm0_testpmd.execute_cmd("show port stats all")
        print out
        self.tcpdump_stop_sniff()
        rx_stats = self.number_of_packets(rx_interface)
        if (rx_stats == 2):
            self.verify(1, "Pass")


    def tear_down(self):
        self.vm0_testpmd.execute_cmd('quit', '# ')
        pass

    def tear_down_all(self):
        print "tear_down_all"
        if self.setup_2pf_2vf_1vm_env_flag == 1:
            self.destroy_2pf_2vf_1vm_env()
示例#2
0
class TestVfPortStartStop(TestCase):

    supported_vf_driver = ['pci-stub', 'vfio-pci']

    def set_up_all(self):

        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) >= 1, "Insufficient ports")
        self.vm0 = None
        self.filename = "/tmp/vf.pcap"

        # set vf assign method and vf driver
        self.vf_driver = self.get_suite_cfg()['vf_driver']
        if self.vf_driver is None:
            self.vf_driver = 'pci-stub'
        self.verify(self.vf_driver in self.supported_vf_driver, "Unspported vf driver")
        if self.vf_driver == 'pci-stub':
            self.vf_assign_method = 'pci-assign'
        else:
            self.vf_assign_method = 'vfio-pci'
            self.dut.send_expect('modprobe vfio-pci', '#')

    def set_up(self):

        self.setup_1pf_2vf_1vm_env_flag = 0

    def pktgen_prerequisites(self):
        """
        igb_uio.ko should be put in ~ before you using pktgen
        """
        out = self.tester.send_expect("ls", "#")
        self.verify("igb_uio.ko" in out, "No file igb_uio.ko, please add it in ~")
        self.tester.send_expect("modprobe uio", "#", 70)
        out = self.tester.send_expect("lsmod | grep igb_uio", "#")
        if "igb_uio" in out:
            self.tester.send_expect("rmmod -f igb_uio", "#", 70)
        self.tester.send_expect("insmod ~/igb_uio.ko", "#", 60)
        out = self.tester.send_expect("lsmod | grep igb_uio", "#")
        assert ("igb_uio" in out), "Failed to insmod igb_uio"

        total_huge_pages = self.tester.get_total_huge_pages()
        if total_huge_pages == 0:
            self.tester.mount_huge_pages()
            self.tester.set_huge_pages(2048)

    def pktgen_kill(self):
        """
        Kill all pktgen on tester.
        """
        pids = []
        pid_reg = r'p(\d+)'
        out = self.tester.alt_session.send_expect("lsof -Fp /var/run/.pg_config", "#", 20)
        if len(out):
            lines = out.split('\r\n')
            for line in lines:
                m = re.match(pid_reg, line)
                if m:
                    pids.append(m.group(1))
        for pid in pids:
            self.tester.alt_session.send_expect('kill -9 %s' % pid, '# ', 20)

    def send_and_verify(self, dst_mac, testpmd):
        """
        Generates packets by pktgen
        """
        self.testpmd_reset_status(testpmd)

        self.pktgen_prerequisites()
        # bind ports
        self.tester_tx_port = self.tester.get_local_port(self.dut_ports[0])
        self.tester_tx_pci = self.tester.ports_info[self.tester_tx_port]['pci']
        port = self.tester.ports_info[self.tester_tx_port]['port']
        self.tester_port_driver = port.get_nic_driver()
        self.tester.send_expect("./dpdk-devbind.py --bind=igb_uio %s" % self.tester_tx_pci, "#")

        src_mac = self.tester.get_mac(self.tester_tx_port) 
        if src_mac == 'N/A':
            src_mac = "02:00:00:00:01"

        self.create_pcap_file(self.filename, dst_mac, src_mac)

        self.tester.send_expect("./pktgen -c 0x1f -n 2  --proc-type auto --socket-mem 128,128 --file-prefix pg -- -P -T -m '1.0' -s 0:%s" % self.filename, "Pktgen >", 100)
        time.sleep(1)
        self.tester.send_expect("start all", "Pktgen>")
        time.sleep(1)
        self.check_port_start_stop(testpmd)
        # quit pktgen
        self.tester.send_expect("stop all", "Pktgen>")
        self.tester.send_expect("quit", "# ")

    def create_pcap_file(self, filename, dst_mac, src_mac):
        """
        Generates a valid PCAP file with the given configuration.
        """
        def_pkts = {'IP/UDP': Ether(dst="%s" % dst_mac, src="%s" % src_mac)/IP(src="127.0.0.2")/UDP()/("X"*46),
                    'IP/TCP': Ether(dst="%s" % dst_mac, src="%s" % src_mac)/IP(src="127.0.0.2")/TCP()/("X"*46),
                    'IP/SCTP': Ether(dst="%s" % dst_mac, src="%s" % src_mac)/IP(src="127.0.0.2")/SCTP()/("X"*48),
                    'IPv6/UDP': Ether(dst="%s" % dst_mac, src="%s" % src_mac)/IPv6(src="::2")/UDP()/("X"*46),
                    'IPv6/TCP': Ether(dst="%s" % dst_mac, src="%s" % src_mac)/IPv6(src="::2")/TCP()/("X"*46),}

        pkts = []
        for key in def_pkts.keys():
            pkts.append(def_pkts[key])

        wrpcap(filename, pkts)

    def testpmd_reset_status(self, testpmd):
        """
        Reset testpmd :stop forword & stop port
        """
        testpmd.execute_cmd('stop')
        testpmd.execute_cmd('port stop all')
        testpmd.execute_cmd('clear port stats all')

    def check_port_start_stop(self, testpmd, times=10):
        """
        VF port start/stop several times , check if it work well.
        """
        for i in range(times):
            out = testpmd.execute_cmd('port start all')
            self.verify("Checking link statuses" in out, "ERROR: port start all")
            testpmd.execute_cmd('start')
            time.sleep(.5)
            testpmd.execute_cmd('stop')
            out = testpmd.execute_cmd('port stop all')
            self.verify("Checking link statuses" in out, "ERROR: port stop all")

        port_id_0 = 0
        port_id_1 = 1
        vf0_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vf1_stats = self.vm0_testpmd.get_pmd_stats(port_id_1)

        vf0_rx_cnt = vf0_stats['RX-packets']
        self.verify(vf0_rx_cnt != 0, "no packet was received by vm0_VF0")

        vf0_rx_err = vf0_stats['RX-errors']
        self.verify(vf0_rx_err == 0, "vm0_VF0 rx-errors")
    
        vf1_tx_cnt = vf1_stats['TX-packets']
        self.verify(vf1_tx_cnt != 0, "no packet was transmitted by vm0_VF1")

        vf1_tx_err = vf1_stats['TX-errors']
        self.verify(vf1_tx_err == 0, "vm0_VF0 tx-errors")

    def setup_1pf_2vf_1vm_env(self, driver='default'):

        self.used_dut_port = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port, 2, driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[self.used_dut_port]['vfs_port']

        try:

            for port in self.sriov_vfs_port:
                port.bind_driver(self.vf_driver)

            time.sleep(1)

            vf0_prop = {'opt_host': self.sriov_vfs_port[0].pci}
            vf1_prop = {'opt_host': self.sriov_vfs_port[1].pci}

            if driver == 'igb_uio':
                # start testpmd without the two VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s' % {'vf0': self.sriov_vfs_port[0].pci,
                                                       'vf1': self.sriov_vfs_port[1].pci}
                self.host_testpmd.start_testpmd("1S/2C/2T", eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = QEMUKvm(self.dut, 'vm0', 'vf_port_start_stop')
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf0_prop)
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf1_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            self.setup_1pf_2vf_1vm_env_flag = 1
        except Exception as e:
            self.destroy_1pf_2vf_1vm_env()
            raise Exception(e)

    def destroy_1pf_2vf_1vm_env(self):
        if getattr(self, 'vm0', None):
            #destroy testpmd in vm0
            if getattr(self, 'vm0_testpmd', None):
                self.vm0_testpmd.execute_cmd('stop')
                self.vm0_testpmd.execute_cmd('quit', '# ')
                self.vm0_testpmd = None
            self.vm0_dut_ports = None
            #destroy vm0
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.dut.ports_info[self.used_dut_port]['port']
            port.bind_driver()
            self.used_dut_port = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver()

        self.setup_1pf_2vf_1vm_env_flag = 0

    def test_start_stop_with_kernel_1pf_2vf_1vm(self):

        self.setup_1pf_2vf_1vm_env(driver='')

        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')

        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK)
        self.vm0_testpmd.execute_cmd('set fwd mac')

        time.sleep(2)

        dst_mac = self.vm0_testpmd.get_port_mac(self.vm0_dut_ports[0])
        self.send_and_verify(dst_mac, self.vm0_testpmd) 

    def tear_down(self):

        if self.setup_1pf_2vf_1vm_env_flag == 1:
            self.destroy_1pf_2vf_1vm_env()

    def tear_down_all(self):

        self.pktgen_kill()
        if getattr(self, 'tester_port_driver', None) and \
           getattr(self, 'tester_tx_pci', None):
            self.tester.send_expect("./dpdk_nic_bind.py --bind=%s %s" \
                %(self.tester_port_driver, self.tester_tx_pci), "#")

        if getattr(self, 'vm0', None):
            self.vm0.stop()

        self.dut.virt_exit()

        for port_id in self.dut_ports:
            self.dut.destroy_sriov_vfs_by_port(port_id)
示例#3
0
class Testvf_daemon(TestCase):
    def set_up_all(self):

        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) >= 1, "Insufficient ports")
        self.vm0 = None
        self.vm1 = None
        self.env_done = False

    def set_up(self):
        self.setup_vm_env()

    def bind_nic_driver(self, ports, driver=""):
        if driver == "igb_uio":
            for port in ports:
                netdev = self.dut.ports_info[port]['port']
                driver = netdev.get_nic_driver()
                if driver != 'igb_uio':
                    netdev.bind_driver(driver='igb_uio')
        else:
            for port in ports:
                netdev = self.dut.ports_info[port]['port']
                driver_now = netdev.get_nic_driver()
                if driver == "":
                    driver = netdev.default_driver
                if driver != driver_now:
                    netdev.bind_driver(driver=driver)

    def setup_vm_env(self, driver='igb_uio'):
        """
        Create testing environment with 2VFs generated from 1PF
        """
        if self.env_done:
            return

        self.bind_nic_driver(self.dut_ports[:1], driver="igb_uio")
        self.used_dut_port = self.dut_ports[0]
        tester_port = self.tester.get_local_port(self.used_dut_port)
        self.tester_intf = self.tester.get_interface(tester_port)

        self.dut.generate_sriov_vfs_by_port(self.used_dut_port,
                                            2,
                                            driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[
            self.used_dut_port]['vfs_port']
        for port in self.sriov_vfs_port:
            port.bind_driver('pci-stub')
        time.sleep(1)
        self.dut_testpmd = PmdOutput(self.dut)
        time.sleep(1)
        vf0_prop = {'opt_host': self.sriov_vfs_port[0].pci}

        # set up VM0 ENV
        self.vm0 = QEMUKvm(self.dut, 'vm0', 'vf_daemon')
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop)
        try:
            self.vm0_dut = self.vm0.start()
            if self.vm0_dut is None:
                raise Exception("Set up VM0 ENV failed!")
        except Exception as e:
            self.destroy_vm_env()
            raise Exception(e)

        self.vm0_dut_ports = self.vm0_dut.get_ports('any')
        self.vm0_testpmd = PmdOutput(self.vm0_dut)

        vf1_prop = {'opt_host': self.sriov_vfs_port[1].pci}
        self.vm1 = QEMUKvm(self.dut, 'vm1', 'vf_daemon')
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop)
        try:
            self.vm1_dut = self.vm1.start()
            if self.vm1_dut is None:
                raise Exception("Set up VM1 ENV failed!")
        except Exception as e:
            self.destroy_vm_env()
            raise Exception(e)
        self.vm1_dut_ports = self.vm1_dut.get_ports('any')
        self.vm1_testpmd = PmdOutput(self.vm1_dut)

        self.env_done = True

    def destroy_vm_env(self):

        if getattr(self, 'vm0', None):
            self.vm0_dut.kill_all()
            self.vm0_testpmd = None
            self.vm0_dut_ports = None
            # destroy vm0
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'vm1', None):
            self.vm1_dut.kill_all()
            self.vm1_testpmd = None
            self.vm1_dut_ports = None
            # destroy vm1
            self.vm1.stop()
            self.vm1 = None

        if getattr(self, 'used_dut_port', None):
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.dut.ports_info[self.used_dut_port]['port']
            self.used_dut_port = None

        self.env_done = False

    def send_packet(self, dst_mac, vlan_id, pktsize, num):
        """
        Generate packets and send them to dut 
        """
        if vlan_id == 0:
            pkt = Packet(pkt_type='UDP', pkt_len=pktsize)
        else:
            pkt = Packet(pkt_type='VLAN_UDP', pkt_len=pktsize)
            pkt.config_layer('vlan', {'vlan': vlan_id})
        pkt.config_layer('ether', {'dst': dst_mac})
        inst = sniff_packets(self.tester_intf, timeout=5)
        pkt.send_pkt(tx_port=self.tester_intf, count=num)
        return inst

    def strip_mac(self, inst, element="src"):
        """
        Load sniff packets, strip and return mac address from dump message
        """
        pkts = load_sniff_packets(inst)
        macs = []
        for pkt in pkts:
            mac = pkt.strip_element_layer2(element)
            macs.append(mac)
        return macs

    def strip_vlan(self, inst):
        """
        Load sniff packets, strip and return vlan id from dump message
        """
        pkts = load_sniff_packets(inst)
        vlans = []
        for pkt in pkts:
            vlan = pkt.strip_element_vlan("vlan")
            vlans.append(vlan)
        return vlans

    def send_and_pmdout(self, dst_mac, vlan_id=0, pktsize=64, num=1):
        """
        Send packets to dut and return testpmd output message
        Input: dst_mac, vlan_id, packet size, packet number
        Output: testpmd output message
        """
        inst = self.send_packet(dst_mac, vlan_id, pktsize, num)
        out = self.vm0_dut.get_session_output(timeout=2)
        return out

    def send_and_vlanstrip(self, dst_mac, vlan_id=0, pktsize=64, num=1):
        """
        Send packets to dut, strip and return vlan id from dump message
        Input: dst_mac, vlan_id, packet size, packet number
        Output: vlan id stripped from dump message
        """
        inst = self.send_packet(dst_mac, vlan_id, pktsize, num)
        vlans = self.strip_vlan(inst)
        return vlans

    def send_and_macstrip(self, dst_mac, vlan_id=0, pktsize=64, num=1):
        """
        Send packets to dut, strip and return src/dst mac from dump message
        Input: dst_mac, vlan_id, packet size, packet number
        Output: src/dst mac stripped from dump message
        """
        inst = self.send_packet(dst_mac, vlan_id, pktsize, num)
        macs = self.strip_mac(inst)
        return macs

    def test_vlan_insert(self):
        """
        Insert a vlan id for a VF from PF
        If insert vlan id as 0, means disabling vlan id insertion 
        If insert vlan id as 1~4095, means enabling vlan id insertion and 
        vlan id as configured value
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        #Disable vlan insert which means insert vlan id as 0
        rx_vlan = 0
        self.dut_testpmd.execute_cmd('set vf vlan insert 0 0 %s' % rx_vlan)
        time.sleep(1)
        vlans = self.send_and_vlanstrip(self.vf0_mac)
        self.verify(rx_vlan not in vlans, "Failed to disable vlan insert!!!")

        #Enable vlan insert which means insert vlan id as 1~4095
        random_vlan = random.randint(1, MAX_VLAN - 1)
        rx_vlans = [1, random_vlan, MAX_VLAN]
        for rx_vlan in rx_vlans:
            self.dut_testpmd.execute_cmd('set vf vlan insert 0 0 %s' % rx_vlan)
            time.sleep(1)
            vlans = self.send_and_vlanstrip(self.vf0_mac)
            self.verify(rx_vlan in vlans,
                        "Failed to enable vlan insert packet!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_multicast_mode(self):
        """
        Enable/disable multicast promiscuous mode for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set vf promisc 0 0 off')
        self.dut_testpmd.execute_cmd('set vf allmulti 0 0 off')
        multi_mac = 'F3:00:33:22:11:00'
        out = self.send_and_pmdout(multi_mac)
        self.verify("received" not in out,
                    "Failed to disable vf multicast mode!!!")

        out = self.send_and_pmdout(self.vf0_mac)
        self.verify("received" in out,
                    "Failed to disable vf multicast mode!!!")
        self.verify("dst=%s" % self.vf0_mac in out,
                    "Failed to disable vf multicast mode!!!")

        self.dut_testpmd.execute_cmd('set vf allmulti 0 0 on')
        out = self.send_and_pmdout(multi_mac)
        self.verify("received" in out, "Failed to enable vf multicast mode!!!")
        self.verify("dst=%s" % multi_mac in out,
                    "Failed to enable vf multicast mode!!!")

        out = self.send_and_pmdout(self.vf0_mac)
        self.verify("received" in out, "Failed to enable vf multicast mode!!!")
        self.verify("dst=%s" % self.vf0_mac in out,
                    "Failed to enable vf multicast mode!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_promisc_mode(self):
        """
        Enable/disable promiscuous mode for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set vf promisc 0 0 off')

        wrong_mac = '9E:AC:72:49:43:11'
        out = self.send_and_pmdout(wrong_mac)
        self.verify("received" not in out,
                    "Failed to disable vf promisc mode!!!")

        out = self.send_and_pmdout(self.vf0_mac)
        self.verify("received" in out, "Failed to disable vf promisc mode!!!")
        self.verify("dst=%s" % self.vf0_mac in out,
                    "Failed to disable vf promisc mode!!!")

        self.dut_testpmd.execute_cmd('set vf promisc 0 0 on')
        out = self.send_and_pmdout(wrong_mac)
        self.verify("received" in out, "Failed to enable vf promisc mode!!!")
        self.verify("dst=%s" % wrong_mac in out,
                    "Failed to enable vf promisc mode!!!")

        out = self.send_and_pmdout(self.vf0_mac)
        self.verify("received" in out, "Failed to enable vf promisc mode!!!")
        self.verify("dst=%s" % self.vf0_mac in out,
                    "Failed to enable vf promisc mode!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_broadcast_mode(self):
        """
        Enable/disable broadcast mode for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set vf broadcast 0 0 off')

        dst_mac = 'FF:FF:FF:FF:FF:FF'
        out = self.send_and_pmdout(dst_mac)
        self.verify("received" not in out,
                    "Failed to disable vf broadcast mode!!!")

        self.dut_testpmd.execute_cmd('set vf broadcast 0 0 on')
        out = self.send_and_pmdout(dst_mac)
        self.verify("received" in out, "Failed to enable vf broadcast mode!!!")
        self.verify("dst=%s" % dst_mac in out,
                    "Failed to enable vf broadcast mode!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_vf_mtu(self):
        """
        Enable VF MTU change        
        """
        self.dut.send_expect("ifconfig %s mtu 9000" % self.tester_intf, "#")
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        pktsize = random.randint(1500, 9000)
        out = self.send_and_macstrip(self.vf0_mac, 0, pktsize)
        self.vm0_testpmd.execute_cmd('stop')
        self.verify(self.vf0_mac.lower() not in out,
                    "Failed to receive and forward this length packet!!!")

        self.vm0_testpmd.execute_cmd('port stop all')
        self.vm0_testpmd.execute_cmd('port config mtu 0 %s' % (pktsize + 100))
        self.vm0_testpmd.execute_cmd('port start all')
        self.vm0_testpmd.execute_cmd('start')
        out = self.send_and_macstrip(self.vf0_mac, 0, pktsize)
        self.vm0_testpmd.execute_cmd('stop')
        self.verify(self.vf0_mac.lower() in out,
                    "Failed to receive and forward this length packet!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()
        self.dut.send_expect("ifconfig %s mtu 1500" % self.tester_intf, "#")

    def test_vlan_tag(self):
        """
        Enable/disable vlan tag for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        random_vlan = random.randint(1, MAX_VLAN - 1)
        rx_vlans = [1, random_vlan, MAX_VLAN]
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        for rx_vlan in rx_vlans:
            self.vm0_testpmd.execute_cmd('rx_vlan add %s 0' % rx_vlan)
            self.dut_testpmd.execute_cmd('set vf vlan tag 0 0 off')
            time.sleep(1)
            out = self.send_and_macstrip(self.vf0_mac, rx_vlan)
            self.verify(self.vf0_mac.lower() not in out,
                        "Failed to disable vlan tag!!!")

            self.dut_testpmd.execute_cmd('set vf vlan tag 0 0 on')
            time.sleep(1)
            out = self.send_and_macstrip(self.vf0_mac, rx_vlan)
            self.verify(self.vf0_mac.lower() in out,
                        "Failed to enable vlan tag!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_tx_loopback(self):
        """
        Enable/disable TX loopback from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')
        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)
        self.vm1_testpmd.start_testpmd(
            VM_CORES_MASK,
            '--port-topology=chained --eth-peer=0,%s' % self.vf0_mac)

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set tx loopback 0 off')

        inst = sniff_packets(self.tester_intf, timeout=5)

        self.vm1_testpmd.execute_cmd('set burst 5')
        self.vm1_testpmd.execute_cmd('start tx_first')

        dumpout = self.strip_mac(inst, "dst")
        out = self.vm0_testpmd.execute_cmd('stop')
        self.verify(self.vf0_mac.lower() in dumpout,
                    "Failed to disable tx loopback!!!")
        self.verify("RX-packets: 0" in out, "Failed to disable tx loopback!!!")

        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set tx loopback 0 on')

        inst = sniff_packets(self.tester_intf, timeout=5)

        self.vm1_testpmd.execute_cmd('stop')
        self.vm1_testpmd.execute_cmd('start tx_first')
        dumpout = self.strip_mac(inst, "dst")
        out = self.vm0_testpmd.execute_cmd('stop')
        self.verify(self.vf0_mac.lower() not in dumpout,
                    "Failed to enable tx loopback!!!")
        self.verify("RX-packets: 5" in out, "Failed to enable tx loopback!!!")

        self.vm0_testpmd.quit()
        self.vm1_testpmd.quit()
        self.dut_testpmd.quit()

    def test_all_queues_drop(self):
        """
        Enable/disable drop enable bit for all queues from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')
        self.vm1_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('set all queues drop 0 off')
        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)
        self.vf1_mac = self.vm1_testpmd.get_port_mac(0)
        out = self.send_and_pmdout(self.vf1_mac, 0, 64, 200)
        out = self.vm1_testpmd.execute_cmd('show port stats 0')
        self.verify("RX-packets: 127" in out,
                    "Failed to let vf1 full of queues!!!")
        out = self.send_and_pmdout(self.vf0_mac, 0, 64, 20)
        out = self.vm0_testpmd.execute_cmd('show port stats 0')
        self.verify("RX-packets: 0" in out,
                    "Failed to disable all queues drop!!!")
        self.dut_testpmd.execute_cmd('set all queues drop 0 on')
        out = self.vm0_testpmd.execute_cmd('show port stats 0')
        self.verify("RX-packets: 20" in out,
                    "Failed to enable all queues drop!!!")
        out = self.send_and_pmdout(self.vf0_mac, 0, 64, 20)
        out = self.vm0_testpmd.execute_cmd('show port stats 0')
        self.verify("RX-packets: 40" in out,
                    "Failed to enable all queues drop!!!")

        self.vm0_testpmd.quit()
        self.vm1_testpmd.quit()
        self.dut_testpmd.quit()

    def test_mac_antispoof(self):
        """
        Enable/disable mac anti-spoof for a VF from PF
        """
        fake_mac = '00:11:22:33:44:55'
        self.vm0_dut.send_expect("sed -i -e '/uint64_t ol_flags = 0;/a " +\
            "\struct ether_addr fake_mac = {.addr_bytes = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55},};'" +\
            " app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.send_expect("sed -i -e '/ether_addr_copy(&addr, &eth_hdr->s_addr);/d' " +\
            " app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.send_expect("sed -i -e '/ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);/a " +\
            "\ether_addr_copy(&fake_mac, &eth_hdr->s_addr);' app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.build_install_dpdk(self.target)
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')
        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd macswap')
        self.dut_testpmd.execute_cmd('set vf mac antispoof 0 0 off')
        self.vm0_testpmd.execute_cmd('start')
        dumpout = self.send_and_macstrip(self.vf0_mac)
        out = self.vm0_testpmd.execute_cmd('stop')
        self.verify(fake_mac in dumpout, "Failed to disable vf mac anspoof!!!")
        self.verify("RX-packets: 1" in out, "Failed to receive packet!!!")
        self.verify("TX-packets: 1" in out,
                    "Failed to disable mac antispoof!!!")

        self.dut_testpmd.execute_cmd('set vf mac antispoof 0 0 on')
        out = self.vm0_testpmd.execute_cmd('start')
        dumpout = self.send_and_macstrip(self.vf0_mac)
        out = self.vm0_testpmd.execute_cmd('stop')
        self.verify(fake_mac not in dumpout,
                    "Failed to enable vf mac anspoof!!!")
        self.verify("RX-packets: 1" in out, "Failed to receive packet!!!")
        self.verify("TX-packets: 0" in out,
                    "Failed to enable mac antispoof!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()
        self.vm0_dut.send_expect("sed -i '/struct ether_addr fake_mac = {.addr_bytes = " +\
            "{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},};/d' app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.send_expect("sed -i '/ether_addr_copy(&fake_mac, &eth_hdr->s_addr);/d' " +\
            "app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.send_expect("sed -i '/ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);/a " +\
            "\ether_addr_copy(&addr, &eth_hdr->s_addr);' app/test-pmd/macswap.c", "# ", 30)
        self.vm0_dut.build_install_dpdk(self.target)

    def test_vf_mac_set(self):
        """
        Set MAC address for a VF from PF
        """
        expect_mac = 'A2:22:33:44:55:66'
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.dut_testpmd.execute_cmd('set vf mac addr 0 0 %s' % expect_mac)
        out = self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                             '--port-topology=chained')
        self.verify("%s" % expect_mac in out, "Failed to set vf mac!!!")
        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')
        out = self.send_and_macstrip(self.vf0_mac)
        self.verify(expect_mac.lower() in out,
                    "Failed to receive packet on setted vf mac!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_vlan_antispoof(self):
        """
        Enable/disable vlan antispoof for a VF from PF 
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)
        vf0_mac_lower = self.vf0_mac.lower()
        random_vlan = random.randint(1, MAX_VLAN)
        match_vlan = random_vlan
        unmatch_vlan = (random_vlan + 2) % 4096
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')
        self.dut_testpmd.execute_cmd('rx_vlan add %d port 0 vf 1' % match_vlan)
        if self.kdriver == "i40e":
            self.dut_testpmd.execute_cmd('set vf vlan stripq 0 0 off')
        else:
            self.dut_testpmd.execute_cmd('vlan set filter off 0')
            self.dut_testpmd.execute_cmd('vlan set strip off 0')
            self.vm0_testpmd.execute_cmd('vlan set strip off 0')

        self.dut_testpmd.execute_cmd('set vf vlan antispoof 0 0 off')
        time.sleep(1)
        out = self.send_and_macstrip(self.vf0_mac, match_vlan)
        self.verify(vf0_mac_lower in out,
                    "Failed to disable vlan antispoof with match vlan!!!")
        out = self.send_and_macstrip(self.vf0_mac, unmatch_vlan)
        self.verify(vf0_mac_lower in out,
                    "Failed to disable vlan antispoof with unmatch vlan!!!")
        out = self.send_and_macstrip(self.vf0_mac)
        self.verify(vf0_mac_lower in out,
                    "Failed to disable vlan antispoof with no vlan!!!")

        if self.kdriver == "ixgbe":
            self.dut_testpmd.execute_cmd('set vf mac antispoof 0 0 on')
        self.dut_testpmd.execute_cmd('set vf vlan antispoof 0 0 on')
        time.sleep(1)

        out = self.send_and_macstrip(self.vf0_mac, match_vlan)
        self.verify(vf0_mac_lower in out,
                    "Failed to enable vlan antispoof with match vlan!!!")

        out = self.send_and_macstrip(self.vf0_mac, unmatch_vlan)
        self.verify(vf0_mac_lower not in out,
                    "Failed to enable vlan antispoof with unmatch vlan!!!")

        out = self.send_and_macstrip(self.vf0_mac)
        self.verify(vf0_mac_lower not in out,
                    "Failed to enable vlan antispoof with no vlan!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_vlan_strip(self):
        """
        Enable/disable the VLAN strip for all queues in a pool for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vf0_mac = self.vm0_testpmd.get_port_mac(0)

        random_vlan = random.randint(1, MAX_VLAN - 1)
        rx_vlans = [1, random_vlan, MAX_VLAN]
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')
        for rx_vlan in rx_vlans:
            self.vm0_testpmd.execute_cmd('rx_vlan add %s 0' % rx_vlan)
            self.dut_testpmd.execute_cmd('set vf vlan stripq 0 0 off')
            time.sleep(1)
            out = self.send_and_vlanstrip(self.vf0_mac, rx_vlan)
            self.verify(rx_vlan in out, "Failed to disable strip vlan!!!")

            self.dut_testpmd.execute_cmd('set vf vlan stripq 0 0 on')
            time.sleep(1)
            out = self.send_and_vlanstrip(self.vf0_mac, rx_vlan)
            self.verify(rx_vlan not in out, "Failed to disable strip vlan!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def test_vlan_filter(self):
        """
        Add/Remove vlan filter for a VF from PF
        """
        self.dut_testpmd.start_testpmd("Default", "--port-topology=chained")
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK,
                                       '--port-topology=chained')

        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')

        wrong_mac = '9E:AC:72:49:43:11'
        out = self.send_and_pmdout(wrong_mac)
        self.verify("dst=%s" % wrong_mac in out,
                    "Failed to receive untagged packet!!!")
        random_vlan = random.randint(1, MAX_VLAN)
        out = self.send_and_pmdout(wrong_mac, random_vlan)
        self.verify("dst=%s" % wrong_mac in out,
                    "Failed to receive packet with vlan id!!!")
        self.verify("VLAN tci=%s" % hex(random_vlan) in out,
                    "Failed to receive packet with vlan id!!!")
        random_vlan = random.randint(2, MAX_VLAN - 1)
        rx_vlans = [1, random_vlan, MAX_VLAN]
        for rx_vlan in rx_vlans:
            self.dut_testpmd.execute_cmd('rx_vlan add %s port 0 vf 1' %
                                         rx_vlan)
            time.sleep(1)
            out = self.send_and_pmdout(wrong_mac, rx_vlan)
            self.verify("dst=%s" % wrong_mac in out,
                        "Failed to enable vlan filter!!!")
            self.verify("VLAN tci=%s" % hex(rx_vlan) in out,
                        "Failed to receive packet with vlan id!!!")
            wrong_rx_vlan = (rx_vlan + 1) % 4096
            #Packet for vlan id 0 is equal to untagged packet for this case
            if wrong_rx_vlan == 0:
                wrong_rx_vlan = random.randint(1, MAX_VLAN - 1)
            out = self.send_and_pmdout(wrong_mac, wrong_rx_vlan)
            self.verify("dst=%s" % wrong_mac not in out,
                        "Failed to enable vlan filter!!!")
            self.dut_testpmd.execute_cmd('rx_vlan rm %s port 0 vf 1' % rx_vlan)
            out = self.send_and_pmdout(wrong_mac, rx_vlan)
            self.verify("dst=%s" % wrong_mac in out,
                        "Failed to disable vlan filter!!!")
            self.verify("VLAN tci=%s" % hex(rx_vlan) in out,
                        "Failed to receive packet with vlan id!!!")
            out = self.send_and_pmdout(wrong_mac, wrong_rx_vlan)
            self.verify("dst=%s" % wrong_mac in out,
                        "Failed to disable vlan filter!!!")
            self.verify("VLAN tci=%s" % hex(wrong_rx_vlan) in out,
                        "Failed to receive packet with vlan id!!!")
        out = self.send_and_pmdout(wrong_mac)
        self.verify("dst=%s" % wrong_mac in out,
                    "Failed to receive untagged packet!!!")

        self.vm0_testpmd.quit()
        self.dut_testpmd.quit()

    def tear_down(self):
        self.vm0_dut.kill_all()
        self.vm1_dut.kill_all()
        pass

    def tear_down_all(self):
        self.destroy_vm_env()
        pass
示例#4
0
class TestTX_preparation(TestCase):
    #
    # Test cases.
    #

    def set_up_all(self):
        """
        Run at the start of each test suite.
        """
        self.ports = self.dut.get_ports(self.nic)
        self.verify(len(self.ports) >= 1, "Insufficient number of ports.")
        self.used_dut_port = self.ports[0]
        tester_port = self.tester.get_local_port(self.used_dut_port)
        self.tester_intf = self.tester.get_interface(tester_port)
        out = self.tester.send_expect(
            "ethtool -K %s rx off tx off tso off gso\
            off gro off lro off" % self.tester_intf, "#")
        if "Cannot change large-receive-offload" in out:
            self.tester.send_expect(
                "ethtool -K %s rx off tx off tso off gso\
            off gro off" % self.tester_intf, "#")
        self.tester.send_expect(
            "ifconfig %s mtu %s" % (self.tester_intf, Max_mtu), "#")

    def set_up(self):
        """
        Run before each test case.
        """
        self.dut_testpmd = PmdOutput(self.dut)
        # use one port test the case
        self.dut_testpmd.start_testpmd(
            "Default",
            " --portmask=1 --port-topology=chained --max-pkt-len=%s --tx-offloads=0x8000"
            % Max_mtu)
        self.dmac = self.dut_testpmd.get_port_mac(0)
        self.dut_testpmd.execute_cmd('set fwd csum')
        self.dut_testpmd.execute_cmd('set verbose 1')
        #enable ip/udp/tcp hardware checksum
        self.dut_testpmd.execute_cmd('port stop all')
        self.dut_testpmd.execute_cmd('csum set ip hw 0')
        self.dut_testpmd.execute_cmd('csum set tcp hw 0')
        self.dut_testpmd.execute_cmd('csum set udp hw 0')

    def start_tcpdump(self, rxItf):
        # only sniff form dut packet and filter lldp packet
        param = "ether[12:2]!=0x88cc and ether src %s" % self.dmac
        self.tester.send_expect("rm -rf ./getPackageByTcpdump.cap", "#")
        self.tester.send_expect(
            "tcpdump %s -i %s -n -e -vv -w\
            ./getPackageByTcpdump.cap 2> /dev/null& " % (param, rxItf), "#")

    def get_tcpdump_package(self):
        self.tester.send_expect("killall tcpdump", "#")
        return self.tester.send_expect(
            "tcpdump -nn -e -v -r ./getPackageByTcpdump.cap", "#")

    def send_packet_verify(self, tsoflag=0):
        """
        Send packet to portid and output
        """
        LrgLength = random.randint(Normal_mtu, Max_mtu - 100)
        pkts = {
            'IPv4/cksum TCP':
            'Ether(dst="%s")/IP()/TCP(flags=0x10)\
                    /Raw(RandString(50))' % self.dmac,
            'IPv4/bad IP cksum':
            'Ether(dst="%s")/IP(chksum=0x1234)\
                    /TCP(flags=0x10)/Raw(RandString(50))' % self.dmac,
            'IPv4/bad TCP cksum':
            'Ether(dst="%s")/IP()/TCP(flags=0x10,\
                    chksum=0x1234)/Raw(RandString(50))' % self.dmac,
            'IPv4/large pkt':
            'Ether(dst="%s")/IP()/TCP(flags=0x10)\
                    /Raw(RandString(%s))' % (self.dmac, LrgLength),
            'IPv4/bad cksum/large pkt':
            'Ether(dst="%s")/IP(chksum=0x1234)\
                    /TCP(flags=0x10,chksum=0x1234)/Raw(RandString(%s))' %
            (self.dmac, LrgLength),
            'IPv6/cksum TCP':
            'Ether(dst="%s")/IPv6()/TCP(flags=0x10)\
                    /Raw(RandString(50))' % self.dmac,
            'IPv6/cksum UDP':
            'Ether(dst="%s")/IPv6()/UDP()\
                    /Raw(RandString(50))' % self.dmac,
            'IPv6/bad TCP cksum':
            'Ether(dst="%s")/IPv6()/TCP(flags=0x10,\
                    chksum=0x1234)/Raw(RandString(50))' % self.dmac,
            'IPv6/large pkt':
            'Ether(dst="%s")/IPv6()/TCP(flags=0x10)\
                    /Raw(RandString(%s))' % (self.dmac, LrgLength)
        }

        for packet_type in pkts.keys():
            self.start_tcpdump(self.tester_intf)
            self.tester.scapy_append('sendp([%s], iface="%s")' %
                                     (pkts[packet_type], self.tester_intf))
            self.tester.scapy_execute()
            out = self.get_tcpdump_package()
            if packet_type == 'IPv6/cksum UDP':
                self.verify("udp sum ok" in out,
                            "Failed to check UDP checksum correctness!!!")
            else:
                self.verify("cksum" in out,
                            "Failed to check IP/TCP checksum!!!")
                self.verify(
                    "correct" in out and "incorrect" not in out,
                    "Failed to check IP/TCP/UDP checksum correctness!!!")

            if tsoflag == 1:
                if packet_type in\
                   ['IPv4/large pkt', 'IPv6/large pkt', 'IPv4/bad cksum/large pkt']:
                    segnum = LrgLength / TSO_value
                    LastLength = LrgLength % TSO_value
                    num = out.count('length %s' % TSO_value)
                    self.verify(
                        "length %s" % TSO_value in out and num == segnum,
                        "Failed to verify TSO correctness for large packets!!!"
                    )
                    if LastLength != 0:
                        num = out.count('length %s' % LastLength)
                        self.verify(
                            "length %s" % LastLength in out and num == 1,
                            "Failed to verify TSO correctness for large packets!!!"
                        )

    def test_tx_preparation_NonTSO(self):
        """
        ftag functional test
        """
        self.dut_testpmd.execute_cmd('tso set 0 0')
        self.dut_testpmd.execute_cmd('port start all')
        self.dut_testpmd.execute_cmd('start')

        self.send_packet_verify()
        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.quit()

    def test_tx_preparation_TSO(self):
        """
        ftag functional test
        """
        self.dut_testpmd.execute_cmd('tso set %s 0' % TSO_value)
        self.dut_testpmd.execute_cmd('port start all')
        self.dut_testpmd.execute_cmd('start')

        self.send_packet_verify(1)
        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.quit()

    def tear_down(self):
        """
        Run after each test case. 
        """
        pass

    def tear_down_all(self):
        """
        Run after each test suite.
        """
        self.tester.send_expect(
            "ifconfig %s mtu %s" % (self.tester_intf, Normal_mtu), "#")
        self.dut.kill_all()
示例#5
0
class TestVfMacFilter(TestCase):

    supported_vf_driver = ['pci-stub', 'vfio-pci']
    vf0_wrongmac = "00:11:22:33:48:55"
    vf0_setmac = "00:11:22:33:44:55"

    def set_up_all(self):
        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) > 1, "Insufficient ports")
        self.vm0 = None
        self.pf0_vf0_mac = "00:12:34:56:78:01"

        # set vf assign method and vf driver
        self.vf_driver = self.get_suite_cfg()['vf_driver']
        if self.vf_driver is None:
            self.vf_driver = 'pci-stub'
        self.verify(self.vf_driver in self.supported_vf_driver,
                    "Unsupported vf driver")
        if self.vf_driver == 'pci-stub':
            self.vf_assign_method = 'pci-assign'
        else:
            self.vf_assign_method = 'vfio-pci'
            self.dut.send_expect('modprobe vfio-pci', '#')

    def set_up(self):

        self.setup_2pf_2vf_1vm_env_flag = 0

    def setup_2pf_2vf_1vm_env(self, set_mac, driver='default'):

        self.used_dut_port_0 = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_0,
                                            1,
                                            driver=driver)
        self.sriov_vfs_port_0 = self.dut.ports_info[
            self.used_dut_port_0]['vfs_port']
        pf_intf0 = self.dut.ports_info[0]['port'].get_interface_name()

        if set_mac:
            self.dut.send_expect(
                "ip link set %s vf 0 mac %s" % (pf_intf0, self.pf0_vf0_mac),
                "#")

        self.used_dut_port_1 = self.dut_ports[1]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_1,
                                            1,
                                            driver=driver)
        self.sriov_vfs_port_1 = self.dut.ports_info[
            self.used_dut_port_1]['vfs_port']

        try:

            for port in self.sriov_vfs_port_0:
                port.bind_driver(self.vf_driver)

            for port in self.sriov_vfs_port_1:
                port.bind_driver(self.vf_driver)

            time.sleep(1)
            vf0_prop = {'opt_host': self.sriov_vfs_port_0[0].pci}
            vf1_prop = {'opt_host': self.sriov_vfs_port_1[0].pci}

            if driver == 'igb_uio':
                # start testpmd without the two VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s' % {
                    'vf0': self.sriov_vfs_port_0[0].pci,
                    'vf1': self.sriov_vfs_port_1[0].pci
                }
                if (self.nic in ["niantic", "sageville", "sagepond"]):
                    self.host_testpmd.start_testpmd("1S/9C/1T",
                                                    "--txq=4 --rxq=4 ",
                                                    eal_param=eal_param)
                else:
                    self.host_testpmd.start_testpmd("1S/2C/2T",
                                                    eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = VM(self.dut, 'vm0', 'vf_macfilter')
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf0_prop)
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf1_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            self.setup_2pf_2vf_1vm_env_flag = 1
        except Exception as e:
            self.destroy_2pf_2vf_1vm_env()
            raise Exception(e)

    def destroy_2pf_2vf_1vm_env(self):
        if getattr(self, 'vm0', None):
            #destroy testpmd in vm0
            if getattr(self, 'vm0_testpmd', None):
                self.vm0_testpmd.execute_cmd('stop')
                self.vm0_testpmd.execute_cmd('quit', '# ')
                self.vm0_testpmd = None
            self.vm0_dut_ports = None
            #destroy vm0
            self.vm0.stop()
            self.dut.virt_exit()
            self.vm0 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port_0', None):
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_0)
            port = self.dut.ports_info[self.used_dut_port_0]['port']
            port.bind_driver()
            self.used_dut_port_0 = None

        if getattr(self, 'used_dut_port_1', None):
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_1)
            port = self.dut.ports_info[self.used_dut_port_1]['port']
            port.bind_driver()
            self.used_dut_port_1 = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver()

        self.setup_2pf_2vf_1vm_env_flag = 0

    def test_kernel_2pf_2vf_1vm_iplink_macfilter(self):
        """
        test case for kernel pf and dpdk vf 2pf_2vf_1vm MAC filter
        scenario.
        kernel pf will first run 'ip link set pf_interface vf 0 mac
        xx:xx:xx:xx:xx:xx', then send packets with this MAC to VF, check
        if the MAC filter works. Also send the packets with wrong MAC
        address to VF, check the VF will not RX the packets.
        """
        self.setup_2pf_2vf_1vm_env(True, driver='')
        self.result_verify_iplink(True)

    def result_verify_iplink(self, set_mac):
        if set_mac == False:
            self.host_testpmd.execute_cmd('set vf mac addr 0 0 %s' %
                                          self.pf0_vf0_mac)
        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK)
        # Get VF's MAC
        pmd_vf0_mac = self.vm0_testpmd.get_port_mac(0)
        self.vm0_testpmd.execute_cmd('set promisc all off')
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        time.sleep(2)

        tgen_ports = []
        tx_port = self.tester.get_local_port(self.dut_ports[0])
        rx_port = self.tester.get_local_port(self.dut_ports[1])
        tgen_ports.append((tx_port, rx_port))
        dst_mac = self.pf0_vf0_mac
        src_mac = self.tester.get_mac(tx_port)
        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]

        print "\nfirst send packets to the PF set MAC, expected result is RX packets=TX packets\n"
        result1 = self.tester.check_random_pkts(tgen_ports,
                                                pktnum=100,
                                                allow_miss=False,
                                                params=pkt_param)
        print "\nshow port stats in testpmd for double check: \n", self.vm0_testpmd.execute_cmd(
            'show port stats all')
        self.verify(result1 != False, "VF0 failed to forward packets to VF1")

        print "\nSecondly, negative test, send packets to a wrong MAC, expected result is RX packets=0\n"
        dst_mac = self.vf0_wrongmac
        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]
        result2 = self.tester.check_random_pkts(tgen_ports,
                                                pktnum=100,
                                                allow_miss=False,
                                                params=pkt_param)
        print "\nshow port stats in testpmd for double check: \n", self.vm0_testpmd.execute_cmd(
            'show port stats all')
        self.verify(result2 != True, "VF0 failed to forward packets to VF1")

    def test_kernel_2pf_2vf_1vm_mac_add_filter(self):
        """
        test case for kernel pf and dpdk vf 2pf_2vf_1vm MAC filter
        scenario.
        kernel pf will not set MAC address and the VF will get a random
        generated MAC in the testpmd in VM, and then add VF mac address
        in the testpmd, for example, VF_MAC1 then send packets to the VF
        with the random generated MAC and the new added VF_MAC1 and the
        expected result is that all packets can be RXed and TXed.
        What's more, send packets with a wrong MAC address to the VF will
        not be received by the VF.
        """
        self.setup_2pf_2vf_1vm_env(False, driver='')
        self.send_packet_and_verify()

    def test_dpdk_2pf_2vf_1vm_mac_add_filter(self):
        """
        test case for dpdk pf and dpdk vf 2pf_2vf_1vm MAC filter scenario.
        dpdk pf will not set MAC address and the VF will get a random
        generated MAC in the testpmd in VM, and then add VF mac address
        in the testpmd, for example, VF_MAC1 then send packets to the VF
        with the random generated MAC and the new added VF_MAC1 and the
        expected result is that all packets can be RXed and TXed.
        What's more, send packets with a wrong MAC address to the VF, check
        the VF will not RX packets.
        """
        self.verify(
            self.nic.startswith('fortville') == True,
            "NIC is [%s], skip this case" % self.nic)
        self.setup_2pf_2vf_1vm_env(False, driver='igb_uio')
        self.send_packet_and_verify()

    def test_dpdk_2pf_2vf_1vm_iplink_macfilter(self):
        """
        test case for dpdk pf and dpdk vf 2pf_2vf_1vm MAC filter scenario.
        dpdk pf will not set MAC address and the VF will get a random
        generated MAC in the testpmd in VM, then send packets with this
        MAC to VF, check that all packets can be RXed and TXed, send the
        packets with a wrong MAC address to VF, check the VF will not RX
        packets.
        """
        self.setup_2pf_2vf_1vm_env(False, driver='igb_uio')
        self.result_verify_iplink(False)

    def send_packet_and_verify(self):
        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK)

        # Get VF0 port MAC address
        pmd_vf0_mac = self.vm0_testpmd.get_port_mac(0)
        self.vm0_testpmd.execute_cmd('set promisc all off')
        ret = self.vm0_testpmd.execute_cmd('mac_addr add 0 %s' %
                                           self.vf0_setmac)
        # check the operation is supported or not.
        print ret

        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        time.sleep(2)

        tgen_ports = []
        tx_port = self.tester.get_local_port(self.dut_ports[0])
        rx_port = self.tester.get_local_port(self.dut_ports[1])
        tgen_ports.append((tx_port, rx_port))
        src_mac = self.tester.get_mac(tx_port)
        dst_mac = pmd_vf0_mac
        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]

        print "\nfirst send packets to the random generated VF MAC, expected result is RX packets=TX packets\n"
        result1 = self.tester.check_random_pkts(tgen_ports,
                                                pktnum=100,
                                                allow_miss=False,
                                                params=pkt_param)
        print "\nshow port stats in testpmd for double check: \n", self.vm0_testpmd.execute_cmd(
            'show port stats all')
        self.verify(result1 != False, "VF0 failed to forward packets to VF1")

        print "\nsecondly, send packets to the new added MAC, expected result is RX packets=TX packets\n"
        dst_mac = self.vf0_setmac
        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]
        result2 = self.tester.check_random_pkts(tgen_ports,
                                                pktnum=100,
                                                allow_miss=False,
                                                params=pkt_param)
        print "\nshow port stats in testpmd for double check: \n", self.vm0_testpmd.execute_cmd(
            'show port stats all')
        self.verify(result2 != False, "VF0 failed to forward packets to VF1")

        print "\nThirdly, negative test, send packets to a wrong MAC, expected result is RX packets=0\n"
        dst_mac = self.vf0_wrongmac
        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]
        result3 = self.tester.check_random_pkts(tgen_ports,
                                                pktnum=100,
                                                allow_miss=False,
                                                params=pkt_param)
        print "\nshow port stats in testpmd for double check: \n", self.vm0_testpmd.execute_cmd(
            'show port stats all')
        self.verify(result3 != True, "VF0 failed to forward packets to VF1")

    def tear_down(self):

        if self.setup_2pf_2vf_1vm_env_flag == 1:
            self.destroy_2pf_2vf_1vm_env()

    def tear_down_all(self):

        if getattr(self, 'vm0', None):
            self.vm0.stop()

        self.dut.virt_exit()

        for port_id in self.dut_ports:
            self.dut.destroy_sriov_vfs_by_port(port_id)
示例#6
0
class TestVfPacketRxtx(TestCase):

    supported_vf_driver = ['pci-stub', 'vfio-pci']

    def set_up_all(self):

        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) > 1, "Insufficient ports")
        self.vm0 = None
        self.vm1 = None

        # set vf assign method and vf driver
        self.vf_driver = self.get_suite_cfg()['vf_driver']
        if self.vf_driver is None:
            self.vf_driver = 'pci-stub'
        self.verify(self.vf_driver in self.supported_vf_driver,
                    "Unspported vf driver")
        if self.vf_driver == 'pci-stub':
            self.vf_assign_method = 'pci-assign'
        else:
            self.vf_assign_method = 'vfio-pci'
            self.tester.send_expect('modprobe vfio-pci', '#')

    def set_up(self):

        self.setup_2pf_2vf_1vm_env_flag = 0
        self.setup_3vf_2vm_env_flag = 0

    def setup_2pf_2vf_1vm_env(self, driver='default'):

        self.used_dut_port_0 = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_0,
                                            1,
                                            driver=driver)
        self.sriov_vfs_port_0 = self.dut.ports_info[
            self.used_dut_port_0]['vfs_port']

        self.used_dut_port_1 = self.dut_ports[1]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port_1,
                                            1,
                                            driver=driver)
        self.sriov_vfs_port_1 = self.dut.ports_info[
            self.used_dut_port_1]['vfs_port']

        try:

            for port in self.sriov_vfs_port_0:
                port.bind_driver(self.vf_driver)

            for port in self.sriov_vfs_port_1:
                port.bind_driver(self.vf_driver)

            time.sleep(1)
            vf0_prop = {'opt_host': self.sriov_vfs_port_0[0].pci}
            vf1_prop = {'opt_host': self.sriov_vfs_port_1[0].pci}

            if driver == 'igb_uio':
                # start testpmd without the two VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s' % {
                    'vf0': self.sriov_vfs_port_0[0].pci,
                    'vf1': self.sriov_vfs_port_1[0].pci
                }
                if (self.nic in ["niantic", "sageville", "sagepond"]):
                    self.host_testpmd.start_testpmd("1S/9C/1T",
                                                    "--txq=4 --rxq=4 ",
                                                    eal_param=eal_param)
                else:
                    self.host_testpmd.start_testpmd("1S/5C/1T",
                                                    "",
                                                    eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = QEMUKvm(self.dut, 'vm0', 'vf_packet_rxtx')
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf0_prop)
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf1_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            self.setup_2pf_2vf_1vm_env_flag = 1
        except Exception as e:
            self.destroy_2pf_2vf_1vm_env()
            raise Exception(e)

    def destroy_2pf_2vf_1vm_env(self):
        if getattr(self, 'vm0', None):
            #destroy testpmd in vm0
            if getattr(self, 'vm0_testpmd', None):
                self.vm0_testpmd.execute_cmd('stop')
                self.vm0_testpmd.execute_cmd('quit', '# ')
                self.vm0_testpmd = None
            self.vm0_dut_ports = None
            #destroy vm0
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        self.dut.virt_exit()

        if getattr(self, 'used_dut_port_0', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_0)
            port = self.dut.ports_info[self.used_dut_port_0]['port']
            port.bind_driver()
            self.used_dut_port_0 = None

        if getattr(self, 'used_dut_port_1', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port_1)
            port = self.dut.ports_info[self.used_dut_port_1]['port']
            port.bind_driver()
            self.used_dut_port_1 = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver()

        self.setup_2pf_2vf_1vm_env_flag = 0

    def packet_rx_tx(self, driver='default'):

        if driver == 'igb_uio':
            self.setup_2pf_2vf_1vm_env(driver='igb_uio')
        else:
            self.setup_2pf_2vf_1vm_env(driver='')

        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        port_id_0 = 0
        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        out = self.vm0_testpmd.start_testpmd(VM_CORES_MASK)
        pmd_vf0_mac = self.vm0_testpmd.get_port_mac(port_id_0)
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        time.sleep(2)

        tgen_ports = []
        tx_port = self.tester.get_local_port(self.dut_ports[0])
        rx_port = self.tester.get_local_port(self.dut_ports[1])
        tgen_ports.append((tx_port, rx_port))

        dst_mac = pmd_vf0_mac
        src_mac = self.tester.get_mac(tx_port)

        pkt_param = [("ether", {'dst': dst_mac, 'src': src_mac})]

        result = self.tester.check_random_pkts(tgen_ports,
                                               allow_miss=False,
                                               params=pkt_param)
        print self.vm0_testpmd.execute_cmd('show port stats all')
        self.verify(result != False, "VF0 failed to forward packets to VF1")

######1. test case for kernel pf and dpdk vf 2pf_2vf_1vm scenario packet rx tx.

    def test_kernel_2pf_2vf_1vm(self):

        self.packet_rx_tx(driver='')

######2. test case for dpdk pf and dpdk vf 2pf_2vf_1vm scenario packet rx tx.

    def test_dpdk_2pf_2vf_1vm(self):

        self.packet_rx_tx(driver='igb_uio')

    def setup_3vf_2vm_env(self, driver='default'):

        self.used_dut_port = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port,
                                            3,
                                            driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[
            self.used_dut_port]['vfs_port']

        try:

            for port in self.sriov_vfs_port:
                print port.pci
                port.bind_driver(self.vf_driver)

            time.sleep(1)
            vf0_prop = {'opt_host': self.sriov_vfs_port[0].pci}
            vf1_prop = {'opt_host': self.sriov_vfs_port[1].pci}
            vf2_prop = {'opt_host': self.sriov_vfs_port[2].pci}

            for port_id in self.dut_ports:
                if port_id == self.used_dut_port:
                    continue
                port = self.dut.ports_info[port_id]['port']
                port.bind_driver()

            if driver == 'igb_uio':
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s -b %(vf2)s' % {
                    'vf0': self.sriov_vfs_port[0].pci,
                    'vf1': self.sriov_vfs_port[1].pci,
                    'vf2': self.sriov_vfs_port[2].pci
                }
                self.host_testpmd.start_testpmd("1S/2C/2T",
                                                eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = QEMUKvm(self.dut, 'vm0', 'vf_packet_rxtx')
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf0_prop)
            self.vm0.set_vm_device(driver=self.vf_assign_method, **vf1_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")
            # set up VM1 ENV
            self.vm1 = QEMUKvm(self.dut, 'vm1', 'vf_packet_rxtx')
            self.vm1.set_vm_device(driver=self.vf_assign_method, **vf2_prop)
            self.vm_dut_1 = self.vm1.start()
            if self.vm_dut_1 is None:
                raise Exception("Set up VM1 ENV failed!")

            self.setup_3vf_2vm_env_flag = 1
        except Exception as e:
            self.destroy_3vf_2vm_env()
            raise Exception(e)

    def destroy_3vf_2vm_env(self):
        if getattr(self, 'vm0', None):
            if getattr(self, 'vm0_testpmd', None):
                self.vm0_testpmd.execute_cmd('stop')
                self.vm0_testpmd.execute_cmd('quit', '# ')
            self.vm0_testpmd = None
            self.vm0_dut_ports = None
            self.vm_dut_0 = None
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'vm1', None):
            if getattr(self, 'vm1_testpmd', None):
                self.vm1_testpmd.execute_cmd('stop')
                self.vm1_testpmd.execute_cmd('quit', '# ')
            self.vm1_testpmd = None
            self.vm1_dut_ports = None
            self.vm_dut_1 = None
            self.vm1.stop()
            self.vm1 = None

        self.dut.virt_exit()

        if getattr(self, 'host_testpmd', None) != None:
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.dut.ports_info[self.used_dut_port]['port']
            port.bind_driver()
            self.used_dut_port = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver()

        self.setup_3vf_2vm_env_flag = 0

    def test_vf_reset(self):

        self.setup_3vf_2vm_env(driver='')

        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.vm1_dut_ports = self.vm_dut_1.get_ports('any')

        port_id_0 = 0
        port_id_1 = 1

        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK)
        self.vm0_testpmd.execute_cmd('show port info all')
        pmd0_vf0_mac = self.vm0_testpmd.get_port_mac(port_id_0)
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        time.sleep(2)

        self.vm1_testpmd = PmdOutput(self.vm_dut_1)
        self.vm1_testpmd.start_testpmd(VM_CORES_MASK)
        self.vm1_testpmd.execute_cmd('show port info all')

        tx_port = self.tester.get_local_port(self.dut_ports[0])
        rx_port = tx_port

        dst_mac = pmd0_vf0_mac
        self.tester.sendpkt_bg(tx_port, dst_mac)

        #vf port stop/start can trigger reset action
        for num in range(1000):
            self.vm1_testpmd.execute_cmd('port stop all')
            time.sleep(0.1)
            self.vm1_testpmd.execute_cmd('port start all')
            time.sleep(0.1)

        self.tester.stop_sendpkt_bg()

        pmd0_vf0_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        pmd0_vf1_stats = self.vm0_testpmd.get_pmd_stats(port_id_1)

        vf0_rx_cnt = pmd0_vf0_stats['RX-packets']
        self.verify(vf0_rx_cnt != 0, "no packet was received by vm0_VF0")

        vf0_rx_err = pmd0_vf0_stats['RX-errors']
        self.verify(vf0_rx_err == 0, "vm0_VF0 rx-errors")

        vf1_tx_cnt = pmd0_vf1_stats['TX-packets']
        self.verify(vf1_tx_cnt != 0, "no packet was transmitted by vm0_VF1")

        vf1_tx_err = pmd0_vf1_stats['TX-errors']
        self.verify(vf1_tx_err == 0, "vm0_VF0 tx-errors")

        self.verify(
            vf0_rx_cnt == vf1_tx_cnt,
            "vm0_VF0 failed to forward packets to vm0_VF1 when reset vm1_VF0 frequently"
        )

    def tear_down(self):

        if self.setup_2pf_2vf_1vm_env_flag == 1:
            self.destroy_2pf_2vf_1vm_env()
        if self.setup_3vf_2vm_env_flag == 1:
            self.destroy_3vf_2vm_env()

        if getattr(self, 'vm0', None):
            self.vm0.stop()

        if getattr(self, 'vm1', None):
            self.vm1.stop()

        self.dut.virt_exit()

        for port_id in self.dut_ports:
            self.dut.destroy_sriov_vfs_by_port(port_id)
            # DPDK-1754
            intf = self.dut.ports_info[port_id]['intf']
            self.dut.send_expect("ethtool -s %s autoneg on" % intf, "# ")

    def tear_down_all(self):
        pass
示例#7
0
class TestVfKernel(TestCase):
    def set_up_all(self):
        """
        Run at the start of each test suite.
        """
        self.dut.send_expect("service network-manager stop", "#", 60)
        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) >= 1, "Insufficient ports")
        self.cores = self.dut.get_core_list("1S/4C/1T")
        self.coremask = utils.create_mask(self.cores)

        self.dmac = self.dut.get_mac_address(self.dut_ports[0])
        txport = self.tester.get_local_port(self.dut_ports[0])
        self.tester_intf = self.tester.get_interface(txport)
        self.tester_mac = self.tester.get_mac(txport)

        self.intf = self.dut.ports_info[self.dut_ports[0]]['intf']
        self.pci = self.dut.ports_info[self.dut_ports[0]]['pci'].split(':')

        self.src_logo = '12:34:56:78:90:10'
        self.setup_vm_env()

    def set_up(self):
        """
        Run before each test case.
        """
        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "vf link down")
        pass

    def generate_pcap_pkt(self, macs, pktname='flow.pcap'):
        """
        generate pcap pkt
        """
        pkts = ''
        for mac in macs:
            pkt = "Ether(dst='%s',src='%s')/IP()/Raw(load='X'*18)," % (
                mac, self.src_logo)
            pkts += pkt
        self.tester.send_expect("rm -rf flow.pcap", "#", 10)
        self.tester.scapy_append('wrpcap("flow.pcap", [%s])' % pkts)
        self.tester.scapy_execute()

    def setup_vm_env(self):
        """
        1pf -> 6vfs , 4vf->vm0, 2vf->vm1
        """
        self.used_dut_port = self.dut_ports[0]
        self.dut.generate_sriov_vfs_by_port(self.used_dut_port,
                                            6,
                                            driver='igb_uio')
        self.sriov_vfs_port = self.dut.ports_info[
            self.used_dut_port]['vfs_port']
        for port in self.sriov_vfs_port:
            port.bind_driver('pci-stub')
        time.sleep(1)

        self.dut_testpmd = PmdOutput(self.dut)
        self.dut_testpmd.start_testpmd(
            "Default", "--rxq=4 --txq=4 --port-topology=chained")
        # dpdk-2208
        # since there is no forward engine on DPDK PF to forward or drop packet in packet pool,
        # so finally the pool will be full, then no more packet will be
        # received by VF
        self.dut_testpmd.execute_cmd("start")
        time.sleep(5)

        vf0_prop_1 = {'opt_host': self.sriov_vfs_port[0].pci}
        vf0_prop_2 = {'opt_host': self.sriov_vfs_port[1].pci}
        vf0_prop_3 = {'opt_host': self.sriov_vfs_port[2].pci}
        vf0_prop_4 = {'opt_host': self.sriov_vfs_port[3].pci}

        self.vm0 = QEMUKvm(self.dut, 'vm0', 'vf_kernel')
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop_1)
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop_2)
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop_3)
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop_4)
        try:
            self.vm0_dut = self.vm0.start()
            if self.vm0_dut is None:
                raise Exception("Set up VM ENV failed")
            else:
                self.verify(self.vm0_dut.ports_info[0]['intf'] != 'N/A',
                            "Not interface")
        except Exception as e:
            self.destroy_vm_env()
            self.logger.error("Failure for %s" % str(e))

        vf1_prop_5 = {'opt_host': self.sriov_vfs_port[4].pci}
        vf1_prop_6 = {'opt_host': self.sriov_vfs_port[5].pci}
        self.vm1 = QEMUKvm(self.dut, 'vm1', 'vf_kernel')
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop_5)
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop_6)

        try:
            self.vm1_dut = self.vm1.start()
            if self.vm1_dut is None:
                raise Exception("Set up VM1 ENV failed!")
            else:
                # fortville: PF not up ,vf will not get interface
                self.verify(self.vm1_dut.ports_info[0]['intf'] != 'N/A',
                            "Not interface")
        except Exception as e:
            self.destroy_vm_env()
            raise Exception(e)

        self.vm0_testpmd = PmdOutput(self.vm0_dut)
        self.vm1_testpmd = PmdOutput(self.vm1_dut)

        self.vm0_vf0_mac = self.vm0_dut.get_mac_address(0)
        self.vm0_vf1_mac = self.vm0_dut.get_mac_address(1)
        self.vm0_vf2_mac = self.vm0_dut.get_mac_address(2)
        self.vm0_vf3_mac = self.vm0_dut.get_mac_address(3)

        self.vm1_vf0_mac = self.vm1_dut.get_mac_address(0)
        self.vm1_vf1_mac = self.vm1_dut.get_mac_address(1)

        self.vm0_intf0 = self.vm0_dut.ports_info[0]['intf']
        self.vm0_intf1 = self.vm0_dut.ports_info[1]['intf']

        self.vm1_intf0 = self.vm1_dut.ports_info[0]['intf']

        self.vm0_dut.restore_interfaces_linux()
        self.vm1_dut.restore_interfaces_linux()

        # stop NetworkManager, this if for centos7
        # you may change it when the os no support
        self.vm0_dut.send_expect("systemctl stop NetworkManager", "# ", 60)
        self.vm1_dut.send_expect("systemctl stop NetworkManager", "# ", 60)

    def destroy_vm_env(self):
        """
        destroy vm environment
        """
        if getattr(self, 'vm0', None):
            self.vm0_dut.kill_all()
            self.vm0_dut_ports = None
            # destroy vm0
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'vm1', None):
            self.vm1_dut.kill_all()
            self.vm1_dut_ports = None
            # destroy vm1
            self.vm1.stop()
            self.vm1 = None

        self.dut.virt_exit()

        if getattr(self, 'used_dut_port', None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.dut.ports_info[self.used_dut_port]['port']
            self.used_dut_port = None

    def test_link(self):
        """
        verify the link state
        """
        for i in range(5):
            # pf up + vf up -> vf up
            self.vm0_dut.send_expect("ifconfig %s up" % self.vm0_intf0, "#")
            out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
            self.verify("Link detected: yes" in out, "Wrong link status")
            time.sleep(3)

            # pf up + vf down -> vf down
            self.vm0_dut.send_expect("ifconfig %s down" % self.vm0_intf0, "#")
            out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
            self.verify("Link detected: no" in out, "Wrong link status")
            time.sleep(3)

    def ping4(self, session, intf, ipv4):
        """
        using seesion , ping -I $intf $ip
        sometimes it failed, so we try 5 times.
        """
        for i in range(5):
            out = session.send_expect(
                "ping -w 5 -c 5 -A -I %s %s" % (intf, ipv4), "# ")
            if '64 bytes from' not in out:
                print GREEN("%s ping %s failed, retry" % (intf, ipv4))
            else:
                return True
        return False

    def test_ping(self):
        """
        verify the ping state
        """
        for i in range(5):
            random_ip = random.randint(2, 249)
            vm0_ip0 = "5.5.5.%d" % random_ip
            vm0_ip1 = "5.5.5.%d" % (random_ip + 1)
            pf_ip = "5.5.5.%d" % (random_ip + 2)

            # down-up link
            for port_info in self.vm0_dut.ports_info:
                vm0_intf = port_info['intf']
                self.verify(
                    self.check_pf_vf_link_status(self.vm0_dut, vm0_intf),
                    "VM0_vf: %s link down" % vm0_intf)

            self.vm0_dut.send_expect(
                "ifconfig %s %s netmask 255.255.255.0" %
                (self.vm0_intf0, vm0_ip0), "#")
            self.vm0_dut.send_expect(
                "ifconfig %s %s netmask 255.255.255.0" %
                (self.vm0_intf1, vm0_ip1), "#")
            self.tester.send_expect(
                "ifconfig %s %s netmask 255.255.255.0" %
                (self.tester_intf, pf_ip), "#")

            # pf ping vm0_vf0
            self.verify(self.ping4(self.tester, self.tester_intf, vm0_ip0),
                        "%s ping %s failed" % (self.tester_intf, vm0_ip0))
            # vm0_vf0 ping pf
            self.verify(self.ping4(self.vm0_dut, self.vm0_intf0, pf_ip),
                        "%s ping %s failed" % (self.vm0_intf0, pf_ip))

            # pf ping vm0_vf1
            self.verify(self.ping4(self.tester, self.tester_intf, vm0_ip1),
                        "%s ping %s failed" % (self.tester_intf, vm0_ip1))
            # vm0_pf1 ping pf
            self.verify(self.ping4(self.vm0_dut, self.vm0_intf1, pf_ip),
                        "%s ping %s failed" % (self.vm0_intf1, pf_ip))

            # clear ip
            self.vm0_dut.send_expect("ifconfig %s 0.0.0.0" % self.vm0_intf0,
                                     "#")
            self.vm0_dut.send_expect("ifconfig %s 0.0.0.0" % self.vm0_intf1,
                                     "#")
            self.tester.send_expect("ifconfig %s 0.0.0.0" % self.tester_intf,
                                    "#")

    def test_reset(self):
        """
        verify reset the vf1 impact on VF0
        """
        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "VM0_VF0 link up failed")
        self.verify(self.check_pf_vf_link_status(self.vm1_dut, self.vm1_intf0),
                    "VM1_VF0 link up failed")

        # Link down VF1 in VM1 and check no impact on VF0 status
        self.vm1_dut.send_expect("ifconfig %s down" % self.vm1_intf0, "#")
        out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
        self.verify("Link detected: yes" in out, "Wrong link status")

        # Unload VF1 kernel driver and expect no problem for VF0
        self.vm1_dut.send_expect("rmmod %svf" % self.kdriver, "#")
        out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
        self.verify("Link detected: yes" in out, "Wrong link status")
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "Unload VF1 kernel driver impact VF0")

        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "vm0_vf0 link down")

        time.sleep(10)
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "reset PF testpmd impact VF RX failure")

        self.vm1_dut.send_expect("modprobe %svf" % self.kdriver, "#")
        out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
        self.verify("Link detected: yes" in out, "Wrong link status")
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "load VF1 kernel driver impact VF0")

        self.vm1_dut.send_expect("rmmod %svf" % self.kdriver, "#")
        out = self.vm0_dut.send_expect("ethtool %s" % self.vm0_intf0, "#")
        self.verify("Link detected: yes" in out, "Wrong link status")
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "Reset VF1 kernel driver impact VF0")
        self.vm1_dut.send_expect("modprobe %svf" % self.kdriver, "#")

    def test_address(self):
        """
        verify add/delete IP/MAC address
        """
        # ipv4 test:
        random_ip = random.randint(2, 249)
        vm0_ip0 = "5.5.5.%d" % random_ip
        pf_ip = "5.5.5.%d" % (random_ip + 2)
        self.vm0_dut.send_expect(
            "ifconfig %s %s netmask 255.255.255.0" % (self.vm0_intf0, vm0_ip0),
            "#")
        self.tester.send_expect(
            "ifconfig %s %s netmask 255.255.255.0" % (self.tester_intf, pf_ip),
            "#")

        # pf ping vm0_vf0
        self.verify(self.ping4(self.tester, self.tester_intf, vm0_ip0),
                    "%s ping %s failed" % (self.tester_intf, vm0_ip0))
        # vm0_vf0 ping pf
        self.verify(self.ping4(self.vm0_dut, self.vm0_intf0, pf_ip),
                    "%s ping %s failed" % (self.vm0_intf0, pf_ip))
        # clear ip
        self.vm0_dut.send_expect("ifconfig %s 0.0.0.0" % self.vm0_intf0, "#")
        self.tester.send_expect("ifconfig %s 0.0.0.0" % self.tester_intf, "#")

        # ipv6 test:
        add_ipv6 = 'efdd::9fc8:6a6d:c232:f1c0'
        self.vm0_dut.send_expect(
            "ifconfig %s add %s" % (self.vm0_intf0, add_ipv6), "#")
        out = self.vm0_dut.send_expect("ifconfig %s " % self.vm0_intf0, "#",
                                       10)
        self.verify(add_ipv6 in out, "Failed to add ipv6 address")
        self.vm0_dut.send_expect(
            "ifconfig %s del %s" % (self.vm0_intf0, add_ipv6), "#")
        out = self.vm0_dut.send_expect("ifconfig %s " % self.vm0_intf0, "#",
                                       10)
        self.verify(add_ipv6 not in out, "Failed to del ipv6 address")

        # mac test:
        modify_mac = 'aa:bb:cc:dd:ee:ff'
        self.vm0_dut.send_expect(
            "ifconfig %s hw ether %s" % (self.vm0_intf0, modify_mac), "#")
        out = self.vm0_dut.send_expect("ifconfig %s " % self.vm0_intf0, "#",
                                       10)
        self.verify(modify_mac in out, "Failed to add mac address")
        time.sleep(5)
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, modify_mac),
            "modify mac address can't received packet")

    def verify_vm_tcpdump(self,
                          vm_dut,
                          intf,
                          mac,
                          pkt_lens=64,
                          num=1,
                          vlan_id='',
                          param=''):
        vm_dut.send_expect(
            "tcpdump -i %s %s -e ether src %s" %
            (intf, param, self.tester_mac), "tcpdump", 10)
        self.send_packet(mac, pkt_lens, num, vlan_id)
        out = vm_dut.get_session_output(timeout=10)
        vm_dut.send_expect("^C", "#", 10)
        if self.tester_mac in out:
            return True
        else:
            return False

    def send_packet(self, mac, pkt_lens=64, num=1, vlan_id=''):
        if vlan_id == '':
            pkt = Packet(pkt_type='TCP', pkt_len=pkt_lens)
            pkt.config_layer('ether', {'dst': mac, 'src': self.tester_mac})
            pkt.send_pkt(tx_port=self.tester_intf, count=num)
        else:
            pkt = Packet(pkt_type='VLAN_UDP', pkt_len=pkt_lens)
            pkt.config_layer('ether', {'dst': mac, 'src': self.tester_mac})
            pkt.config_layer('vlan', {'vlan': vlan_id})
            pkt.send_pkt(tx_port=self.tester_intf, count=num)

    def test_vlan(self):
        """
        verify add/delete vlan
        """
        vlan_ids = random.randint(1, 4095)
        self.vm0_dut.send_expect("ifconfig %s up" % self.vm0_intf0, "#")
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()

        self.vm0_dut.send_expect("modprobe 8021q", "#")
        out = self.vm0_dut.send_expect("lsmod |grep 8021q", "#")
        self.verify("8021q" in out, "modprobe 8021q failure")

        # Add random vlan id(0~4095) on kernel VF0
        self.vm0_dut.send_expect(
            "vconfig add %s %s" % (self.vm0_intf0, vlan_ids), "#")
        out = self.vm0_dut.send_expect("ls /proc/net/vlan/ ", "#")
        self.verify("%s.%s" % (self.vm0_intf0, vlan_ids) in out,
                    "take vlan id failure")

        # Send packet from tester to VF MAC with not-matching vlan id, check
        # the packet can't be received at the vlan device
        wrong_vlan = vlan_ids % 4095 + 1

        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   vm0_vf0_mac,
                                   vlan_id='%d' % wrong_vlan) == False,
            "received wrong vlan packet")

        # Send packet from tester to VF MAC with matching vlan id, check the packet can be received at the vlan device.
        # check_result = self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, self.vm0_vf0_mac, vlan_id='%d' %vlan_ids)
        check_result = self.verify_vm_tcpdump(self.vm0_dut,
                                              self.vm0_intf0,
                                              vm0_vf0_mac,
                                              vlan_id='%d' % vlan_ids)
        self.verify(check_result,
                    "can't received vlan_id=%d packet" % vlan_ids)

        # Delete configured vlan device
        self.vm0_dut.send_expect(
            "vconfig rem %s.%s" % (self.vm0_intf0, vlan_ids), "#")
        out = self.vm0_dut.send_expect("ls /proc/net/vlan/ ", "#")
        self.verify("%s.%s" % (self.vm0_intf0, vlan_ids) not in out,
                    "vlan error")
        # behavior is diffrent bettwn niantic and fortville ,because of kernel
        # driver
        if self.nic.startswith('fortville'):
            self.verify(
                self.verify_vm_tcpdump(self.vm0_dut,
                                       self.vm0_intf0,
                                       vm0_vf0_mac,
                                       vlan_id='%d' % vlan_ids) == True,
                "delete vlan error")
        else:
            self.verify(
                self.verify_vm_tcpdump(self.vm0_dut,
                                       self.vm0_intf0,
                                       vm0_vf0_mac,
                                       vlan_id='%d' % vlan_ids) == False,
                "delete vlan error")

    def test_packet_statistic(self):
        """
        verify packet statistic
        """
        time.sleep(10)
        out = self.vm0_dut.send_expect("ethtool -S %s" % self.vm0_intf0, "#")
        rx_packets_before = re.findall("\s*rx.*packets:\s*(\d*)", out)
        nb_rx_pkts_before = 0
        for i in range(len(rx_packets_before)):
            nb_rx_pkts_before += int(rx_packets_before[i])

        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   vm0_vf0_mac,
                                   num=10), "VM reveive packet failed")

        out = self.vm0_dut.send_expect("ethtool -S %s" % self.vm0_intf0, "#")
        rx_packets_after = re.findall("\s*rx.*packets:\s*(\d*)", out)
        nb_rx_pkts_after = 0
        for i in range(len(rx_packets_after)):
            nb_rx_pkts_after += int(rx_packets_after[i])

        self.verify(nb_rx_pkts_after == 10 + nb_rx_pkts_before,
                    "rx_packets calculate error")

    def check_pf_vf_link_status(self, session, intf):
        """
        sometimes pf/vf will up abnormal, retry 5 times
        """
        for i in range(5):
            # down-up get new mac form pf.
            # because  dpdk pf will give an random mac when dpdk pf restart.
            session.send_expect("ifconfig %s down" % intf, "#")
            out = session.send_expect("ifconfig %s up" % intf, "#")
            # SIOCSIFFLAGS: Network is down
            # i think the pf link abnormal
            if "Network is down" in out:
                print GREEN(out)
                print GREEN("Try again")
                self.vm0_dut.restore_interfaces_linux()
            else:
                out = session.send_expect("ethtool %s" % intf, "#")
                if "Link detected: yes" in out:
                    return True
            time.sleep(1)
        return False

    def test_mtu(self):
        """
        verify mtu change
        HW limitation on 82599, need add '--max-pkt-len=<length>' on testpmd to set mtu value, 
        all the VFs and PF share same MTU, the largest one take effect.
        """
        vm0_intf0 = self.vm0_dut.ports_info[0]['intf']
        vm0_intf1 = self.vm0_dut.ports_info[1]['intf']
        self.vm0_dut.send_expect("ifconfig %s up" % self.vm0_intf0, "#")
        out = self.vm0_dut.send_expect("ifconfig %s" % self.vm0_intf0, "#")
        self.verify('mtu 1500' in out, "modify MTU failed")
        self.tester.send_expect("ifconfig %s mtu 3000" % self.tester_intf, "#")

        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.execute_cmd('set promisc all off')
        self.dut_testpmd.execute_cmd('set fwd rxonly')
        self.dut_testpmd.execute_cmd('set verbose 1')
        self.dut_testpmd.execute_cmd('start')

        # Send one packet with length as 2000 with DPDK PF MAC as DEST MAC,
        # check that DPDK PF can't receive packet
        self.send_packet(self.dmac, pkt_lens=2000)
        out = self.dut.get_session_output(timeout=10)
        self.verify(self.dmac.upper() not in out, "PF receive error packet")

        # send one packet with length as 2000 with kernel VF MAC as DEST MAC,
        # check that Kernel VF can't receive packet
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   vm0_vf0_mac,
                                   pkt_lens=2000) == False,
            "kernel VF receive error packet")

        # Change DPDK PF mtu as 3000,check no confusion/crash on kernel VF
        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.execute_cmd('port stop all')
        self.dut_testpmd.execute_cmd('port config mtu 0 3000')
        self.dut_testpmd.execute_cmd('port start all')

        self.dut_testpmd.execute_cmd('set promisc all off')
        self.dut_testpmd.execute_cmd('set fwd rxonly')
        self.dut_testpmd.execute_cmd('set verbose 1')
        self.dut_testpmd.execute_cmd('start')

        # sleep 5s to wait vf up , because of pf down-up
        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "VM0_VF0 link down")

        # clear output
        self.dut.get_session_output(timeout=10)

        # send one packet with length as 2000 with DPDK PF MAC as DEST MAC ,
        # check that DPDK PF can receive packet
        self.send_packet(self.dmac, pkt_lens=2000)
        out = self.dut.get_session_output(timeout=10)
        self.verify(self.dmac.upper() in out, "PF can't receive packet")

        # Change kernel VF mtu as 3000,check no confusion/crash on DPDK PF
        if self.nic.startswith('fortville'):
            self.vm0_dut.send_expect("ifconfig %s mtu 3000" % self.vm0_intf0,
                                     "#")

        # send one packet with length as 2000 with kernel VF MAC as DEST MAC,
        # check Kernel VF can receive packet
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   vm0_vf0_mac,
                                   pkt_lens=2000), "VF can't receive packet")

        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.execute_cmd('port stop all')
        self.dut_testpmd.execute_cmd('port config mtu 0 1500')
        self.dut_testpmd.execute_cmd('port start all')

        self.dut_testpmd.execute_cmd('start')

        self.vm0_dut.send_expect("ifconfig %s mtu 1500" % self.vm0_intf0, "#",
                                 10)

    def test_promisc_mode(self):
        """
        verify Enable/disable promisc mode
        """
        self.verify(self.nic not in ["niantic"],
                    "%s NIC not support" % self.nic)
        wrong_mac = '01:02:03:04:05:06'
        # Set up kernel VF tcpdump with -p parameter, which means disable promisc
        # Start DPDK PF, enable promisc mode, set rxonly forwarding
        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.execute_cmd('set promisc all on')
        self.dut_testpmd.execute_cmd('start')
        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "VM0_VF0 link down")
        self.dut.get_session_output()

        # Send packet from tester to VF with correct DST MAC, check the packet
        # can be received by kernel VF
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "VM reveive packet failed")
        # Send packet from tester to PF with correct DST MAC, check the packet
        # can be received by DPDK PF
        self.send_packet(self.dmac)
        out = self.dut.get_session_output()
        self.verify(self.tester_mac.upper() in out, "PF reveive packet failed")

        # Send packet from tester with random DST MAC, check the packet can be
        # received by DPDK PF and kernel VF
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, wrong_mac),
            "VM reveive misc packet failed")
        self.send_packet(wrong_mac)
        out = self.dut.get_session_output()
        self.verify(self.tester_mac.upper() in out,
                    "PF reveive misc packet failed")

        # Send packet from tester to VF with correct DST MAC, check the packet
        # can be received by kernel VF
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut, self.vm0_intf0, vm0_vf0_mac),
            "VM reveive packet failed")
        # Send packet from tester to PF with correct DST MAC, check the packet
        # can be received by DPDK PF
        self.send_packet(self.dmac)
        out = self.dut.get_session_output()
        self.verify(self.tester_mac.upper() in out, "PF reveive packet failed")

        # Disable DPDK PF promisc mode
        self.dut_testpmd.execute_cmd('stop')
        self.dut_testpmd.execute_cmd('set promisc all off')
        self.dut_testpmd.execute_cmd('set fwd rxonly')
        self.dut_testpmd.execute_cmd('set verbose 1')
        self.dut_testpmd.execute_cmd('start')
        self.dut.get_session_output()

        # Set up kernel VF tcpdump with -p parameter, which means disable promisc mode
        # Send packet from tester with random DST MAC, check the packet can't
        # be received by DPDK PF and kernel VF
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   wrong_mac,
                                   param='-p') == False,
            "VM should not reveive misc packet")
        self.send_packet(wrong_mac)
        out = self.dut.get_session_output()
        self.verify(wrong_mac not in out, "PF should not receive misc packet")

        # Send packet from tester to VF with correct DST MAC, check the packet
        # can be received by kernel VF
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        self.verify(
            self.verify_vm_tcpdump(self.vm0_dut,
                                   self.vm0_intf0,
                                   vm0_vf0_mac,
                                   param='-p'), "VM reveive packet failed")
        # Send packet from tester to PF with correct DST MAC, check the packet
        # can be received by DPDK PF
        self.send_packet(self.dmac)
        out = self.dut.get_session_output()
        self.verify(self.tester_mac.upper() in out, "PF reveive packet failed")

    def test_rss(self):
        """
        verify kernel VF each queue can receive packets
        """
        self.verify(self.nic not in ["niantic"],
                    "%s NIC not support tcpid " % self.nic)

        # Verify kernel VF RSS using ethtool -"l" (lower case L) <devx> that the
        # default RSS setting is equal to the number of CPUs in the system and
        # that the maximum number of RSS queues displayed is correct for the
        # DUT
        self.verify(self.check_pf_vf_link_status(self.vm0_dut, self.vm0_intf0),
                    "VM0_VF0 link down")

        cpus = self.vm0_dut.send_expect(
            "cat /proc/cpuinfo| grep 'processor'| wc -l", "#")
        out = self.vm0_dut.send_expect("ethtool -l %s" % self.vm0_intf0, "#",
                                       10)
        combined = re.findall("Combined:\s*(\d*)", out)
        self.verify(cpus == combined[0], "the queues count error")

        # Run "ethtool -S <devx> | grep rx_bytes | column" to see the current
        # queue count and verify that it is correct to step 1
        out = self.vm0_dut.send_expect(
            "ethtool -S %s |grep rx-.*bytes" % self.vm0_intf0, "#")
        rx_bytes_before = re.findall("rx-.*bytes:\s*(\d*)", out)
        self.verify(
            len(rx_bytes_before) == int(combined[0]), "the queues count error")

        # Send multi-threaded traffics to the DUT with a number of threads
        # Check kernel VF each queue can receive packets
        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        for i in xrange(5):
            mythread = threading.Thread(target=self.send_packet(vm0_vf0_mac))
            mythread.start()

        out = self.vm0_dut.send_expect(
            "ethtool -S %s |grep rx-*bytes" % self.vm0_intf0, "#")
        rx_bytes_after = re.findall("rx-*.bytes:\s*(\d*)", out)
        for i in range(len(rx_bytes_after)):
            self.verify(rx_bytes_after[i] > rx_bytes_before[i],
                        "NOT each queue receive packets")

    def test_dpf_kvf_dvf(self):
        """
        Check DPDK VF0 and kernel VF1 don't impact each other and no performance drop
        """
        self.vm0_dut.send_expect("ifconfig %s up " % self.vm0_intf0, "#")
        self.vm0_dut.send_expect("ifconfig %s up " % self.vm0_intf1, "#")
        self.vm0_dut.ports_info[1]['port'].bind_driver('igb_uio')

        self.vm0_testpmd.start_testpmd("Default")
        self.vm0_testpmd.execute_cmd('set promisc all on')
        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')

        vm0_vf0_mac = self.vm0_dut.ports_info[0]['port'].get_mac_addr()
        vm0_vf1_mac = self.vm0_testpmd.get_port_mac(0)

        macs = [vm0_vf0_mac, vm0_vf1_mac]
        self.generate_pcap_pkt(macs)

        vm0_newvmsession = self.vm0_dut.new_session()
        date_old = datetime.datetime.now()
        date_new = date_old + datetime.timedelta(minutes=.5)
        while (1):
            date_now = datetime.datetime.now()
            vm0_newvmsession.send_expect(
                "tcpdump -i %s -e ether src %s " %
                (self.vm0_intf0, self.src_logo), "tcpdump")
            self.send_packets()

            out = self.vm0_dut.get_session_output(timeout=20)
            self.verify(self.src_logo in out,
                        "VM PF Confiscated to the specified package")

            put = vm0_newvmsession.send_expect("^C", "#", 10)
            rx_packet = re.findall("(\d*) packe.* captured", put)
            if rx_packet[0] == '1':
                self.verify(
                    rx_packet[0] == '1',
                    "VM KF Confiscated to the specified package\n'%s'" % put)

            if date_now >= date_new:
                break
        time.sleep(3)

    def send_packets(self):
        self.tester.scapy_foreground()
        self.tester.scapy_append("pkts=rdpcap('flow.pcap')")
        self.tester.scapy_append("sendp(pkts, iface='%s')" % self.tester_intf)
        self.tester.scapy_execute()

    def reboot_vm1(self):
        """
        reboot vm1.
        """
        self.vm1.stop()
        vf1_prop_5 = {'opt_host': self.sriov_vfs_port[4].pci}
        vf1_prop_6 = {'opt_host': self.sriov_vfs_port[5].pci}
        self.vm1 = QEMUKvm(self.dut, 'vm1', 'vf_kernel')
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop_5)
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop_6)

        try:
            self.vm1_dut = self.vm1.start()
            if self.vm1_dut is None:
                raise Exception("Set up VM1 ENV failed!")
            else:
                self.verify(self.vm1_dut.ports_info[0]['intf'] != 'N/A',
                            "Not interface")
        except Exception as e:
            self.destroy_vm_env()
            raise Exception(e)

    def test_zdpf_2kvf_2dvf_2vm(self):
        """
        Check DPDK PF 2kernel VFs 2DPDK VFs 2VMs link change impact on other VFs
        DPDK PF + 2kernel VFs + 2DPDK VFs + 2VMs
        Host one DPDK PF and create 6 VFs, pass through VF0, VF1, VF2 and VF3 to VM0, pass through VF4, VF5 to VM1, power on VM0 and VM1.
        Load host DPDK driver, VM DPDK driver and kernel driver.
        """
        for port_info in self.vm0_dut.ports_info:
            vm0_intf = port_info['intf']
            self.verify(self.check_pf_vf_link_status(self.vm0_dut, vm0_intf),
                        "VM0_vf: %s link down" % vm0_intf)

        for port_info in self.vm1_dut.ports_info:
            vm1_intf = port_info['intf']
            self.verify(self.check_pf_vf_link_status(self.vm1_dut, vm1_intf),
                        "VM1_vf: %s link down" % vm1_intf)

        # Bind kernel VF0, VF1 to igb_uio in VM0, bind kernel VF4 to igb_uio in
        # VM1
        self.vm0_dut.ports_info[0]['port'].bind_driver('igb_uio')
        self.vm0_dut.ports_info[1]['port'].bind_driver('igb_uio')
        self.vm1_dut.ports_info[0]['port'].bind_driver('igb_uio')

        # Start DPDK VF0, VF1 in VM0 and VF4 in VM1, enable promisc mode
        self.vm0_testpmd.start_testpmd("Default")
        self.vm0_testpmd.execute_cmd('set promisc all on')
        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')

        self.vm1_testpmd.start_testpmd("Default")
        self.vm1_testpmd.execute_cmd('set promisc all on')
        self.vm1_testpmd.execute_cmd('set fwd rxonly')
        self.vm1_testpmd.execute_cmd('set verbose 1')
        self.vm1_testpmd.execute_cmd('start')

        vm0_vf0_mac = self.vm0_testpmd.get_port_mac(0)
        vm0_vf1_mac = self.vm0_testpmd.get_port_mac(1)
        vm0_vf2_mac = self.vm0_dut.ports_info[2]['port'].get_mac_addr()
        vm0_vf3_mac = self.vm0_dut.ports_info[3]['port'].get_mac_addr()
        vm1_vf0_mac = self.vm1_testpmd.get_port_mac(0)
        vm1_vf1_mac = self.vm1_dut.ports_info[1]['port'].get_mac_addr()
        pf0_mac = self.dut_testpmd.get_port_mac(0)
        pf1_mac = self.dut_testpmd.get_port_mac(1)

        macs = [
            vm0_vf0_mac, vm0_vf1_mac, vm0_vf2_mac, vm0_vf3_mac, vm1_vf0_mac,
            vm1_vf1_mac, pf0_mac, pf1_mac
        ]
        self.generate_pcap_pkt(macs)

        self.send_packets()

        vm0_vf2_newvmsession = self.vm0_dut.new_session()
        vm0_vf3_newvmsession = self.vm0_dut.new_session()
        vm1_newvmsession = self.vm1_dut.new_session()

        # Set up kernel VF2, VF3 in VM0 and VF5 in VM1  tcpdump without -p
        # parameter on promisc mode
        vm0_vf2_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[2]['intf'], self.src_logo), "tcpdump", 10)
        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump", 10)
        vm1_newvmsession.send_expect(
            "tcpdump -i %s -e -p ether src %s" %
            (self.vm0_dut.ports_info[1]['intf'], self.src_logo), "tcpdump", 10)

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "VM0 PF Confiscated to the specified package")

        vm0_vf2_out = vm0_vf2_newvmsession.send_expect("^C", "#")
        vm0_vf2_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm0_vf2_out_rx_packet[0] == '1',
                    "vm0 vf2 Confiscated to the specified package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#")
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "vm0 vf3 Confiscated to the specified package")

        out = self.vm1_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "VM1 PF Confiscated to the specified package")

        vm1_vf1_out = vm1_newvmsession.send_expect("^C", "#")
        vm1_vf1_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm1_vf1_out_rx_packet[0] == '1',
                    "vm1 vf1 Confiscated to the specified package")

        # Link down DPDK VF0 and expect no impact on other VFs
        self.vm0_testpmd.quit()
        eal_param = '-b %(vf0)s' % ({'vf0': self.vm0_dut.ports_info[0]['pci']})
        self.vm0_testpmd.start_testpmd("Default", eal_param=eal_param)
        self.vm0_testpmd.execute_cmd('set promisc all on')
        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('set verbose 1')
        self.vm0_testpmd.execute_cmd('start')

        vm0_vf2_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[2]['intf'], self.src_logo), "tcpdump", 10)
        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump", 10)
        vm1_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[1]['intf'], self.src_logo), "tcpdump", 10)

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "link down impact VM0 PF receive package")

        vm0_vf2_out = vm0_vf2_newvmsession.send_expect("^C", "#")
        vm0_vf2_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm0_vf2_out_rx_packet[0] == '1',
                    "link down impact vm0 vf2 receive package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#")
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "link down impact vm0 vf3 receive package")

        out = self.vm1_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "link down impact VM1 PF receive package")

        vm1_vf1_out = vm1_newvmsession.send_expect("^C", "#")
        vm1_vf1_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm1_vf1_out_rx_packet[0] == '1',
                    "link down impact vm1 vf1 receive package")

        # Link down kernel VF2 and expect no impact on other VFs
        vm0_vf2_newvmsession.send_expect(
            "ifconfig %s down" % self.vm0_dut.ports_info[2]['intf'], "#", 10)

        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump", 10)
        vm1_newvmsession.send_expect(
            "tcpdump -i -p %s -e ether src %s" %
            (self.vm0_dut.ports_info[1]['intf'], self.src_logo), "tcpdump", 10)

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "link down kernel vf2 impact VM0 PF receive package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#")
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "link down kernel vf2 impact vm0 vf3 receive package")

        out = self.vm1_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "link down kernel vf2 impact VM1 PF receive package")

        vm1_vf1_out = vm1_newvmsession.send_expect("^C", "#")
        vm1_vf1_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm1_vf1_out_rx_packet[0] == '1',
                    "link down kernel vf2 impact vm1 vf1 receive package")

        vm0_vf2_newvmsession.send_expect(
            "ifconfig %s up" % self.vm0_dut.ports_info[2]['intf'], "#")

        # Quit VF4 DPDK testpmd and expect no impact on other VFs
        self.vm1_testpmd.quit()

        vm0_vf2_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[2]['intf'], self.src_logo), "tcpdump", 10)
        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump", 10)
        vm1_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[1]['intf'], self.src_logo), "tcpdump", 10)

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "quit vf4 DPDK testpmd impact VM0 PF receive package")

        vm0_vf2_out = vm0_vf2_newvmsession.send_expect("^C", "#")
        vm0_vf2_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm0_vf2_out_rx_packet[0] == '1',
                    "quit vf4 DPDK testpmd impact vm0 vf2 receive package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#")
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "quit vf4 DPDK testpmd impact vm0 vf3 receive package")

        vm1_vf1_out = vm1_newvmsession.send_expect("^C", "#")
        vm1_vf1_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm1_vf1_out_rx_packet[0] == '1',
                    "quit vf4 DPDK testpmd impact vm1 vf1 receive package")

        self.vm1_testpmd.start_testpmd("Default")
        self.vm1_testpmd.execute_cmd('set promisc all on')
        self.vm1_testpmd.execute_cmd('set fwd rxonly')
        self.vm1_testpmd.execute_cmd('set verbose 1')
        self.vm1_testpmd.execute_cmd('start')

        # Unload VF5 kernel driver and expect no impact on other VFs
        vm1_newvmsession.send_expect(
            "./usertools/dpdk-devbind.py -b pci-stub %s" %
            (self.vm1_dut.ports_info[1]['pci']), "#")

        vm0_vf2_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[2]['intf'], self.src_logo), "tcpdump")
        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump")

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "unload vf5 kernel driver impact VM0 PF receive package")

        vm0_vf2_out = vm0_vf2_newvmsession.send_expect("^C", "#")
        vm0_vf2_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm0_vf2_out_rx_packet[0] == '1',
                    "unload vf5 kernel driver impact vm0 vf2 receive package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#", 10)
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "unload vf5 kernel driver impact vm0 vf3 receive package")

        out = self.vm1_dut.get_session_output(timeout=20)
        self.verify(self.src_logo in out,
                    "unload vf5 kernel driver impact VM1 PF receive package")

        # Reboot VM1 and expect no impact on VFs of VM0
        self.vm1_dut.send_expect("quit", "#")
        self.reboot_vm1()

        vm0_vf2_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[2]['intf'], self.src_logo), "tcpdump")
        vm0_vf3_newvmsession.send_expect(
            "tcpdump -i %s -p -e ether src %s" %
            (self.vm0_dut.ports_info[3]['intf'], self.src_logo), "tcpdump")

        self.send_packets()

        out = self.vm0_dut.get_session_output()
        self.verify(self.src_logo in out,
                    "reboot vm1 impact VM0 PF receive package")

        vm0_vf2_out = vm0_vf2_newvmsession.send_expect("^C", "#")
        vm0_vf2_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf2_out)
        self.verify(vm0_vf2_out_rx_packet[0] == '1',
                    "reboot vm1 impact vm0 vf2 receive package")

        vm0_vf3_out = vm0_vf3_newvmsession.send_expect("^C", "#")
        vm0_vf3_out_rx_packet = re.findall("(\d*) packe.* captured",
                                           vm0_vf3_out)
        self.verify(vm0_vf3_out_rx_packet[0] == '1',
                    "reboot vm1 impact vm0 vf3 receive package")

    def test_stress(self):
        """
        Load kernel driver stress
        """
        for i in xrange(100):
            out = self.vm0_dut.send_expect("rmmod %svf" % self.kdriver, "#")
            self.verify('error' not in out,
                        "stress error for rmmod %svf:%s" % (self.kdriver, out))
            out = self.vm0_dut.send_expect("modprobe %svf" % self.kdriver, "#")
            self.verify(
                'error' not in out,
                "stress error for modprobe %svf:%s" % (self.kdriver, out))

    def tear_down(self):
        """
        Run after each test case.
        """
        self.vm0_testpmd.quit()
        self.vm0_dut.restore_interfaces_linux()
        if getattr(self, 'vm0_newvmsession', None):
            self.vm0_dut.close_session(vm0_newvmsession)
        if getattr(self, 'vm0_vf2_newvmsession', None):
            self.vm0_dut.close_session(vm0_vf2_newvmsession)
        if getattr(self, 'vm0_vf3_newvmsession', None):
            self.vm0_dut.close_session(vm0_vf3_newvmsession)

        # Sometime test failed ,we still need clear ip.
        self.vm0_dut.send_expect("ifconfig %s 0.0.0.0" % self.vm0_intf0, "#")
        self.vm0_dut.send_expect("ifconfig %s 0.0.0.0" % self.vm0_intf1, "#")
        self.tester.send_expect("ifconfig %s 0.0.0.0" % self.tester_intf, "#")
        time.sleep(5)

    def tear_down_all(self):
        """
        Run after each test suite.
        """
        self.dut_testpmd.quit()
        self.destroy_vm_env()
        self.dut.kill_all()
        time.sleep(2)