1import logging, os, signal
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.bin import utils
4from autotest_lib.client.virt import aexpect, virt_utils
5
6def run_netperf(test, params, env):
7    """
8    Network stress test with netperf.
9
10    1) Boot up a VM with multiple nics.
11    2) Launch netserver on guest.
12    3) Execute multiple netperf clients on host in parallel
13       with different protocols.
14    4) Output the test result.
15
16    @param test: KVM test object.
17    @param params: Dictionary with the test parameters.
18    @param env: Dictionary with test environment.
19    """
20    vm = env.get_vm(params["main_vm"])
21    vm.verify_alive()
22    login_timeout = int(params.get("login_timeout", 360))
23    session = vm.wait_for_login(timeout=login_timeout)
24    session.close()
25    session_serial = vm.wait_for_serial_login(timeout=login_timeout)
26
27    netperf_dir = os.path.join(os.environ['AUTODIR'], "tests/netperf2")
28    setup_cmd = params.get("setup_cmd")
29
30    firewall_flush = "iptables -F"
31    session_serial.cmd_output(firewall_flush)
32    try:
33        utils.run("iptables -F")
34    except:
35        pass
36
37    for i in params.get("netperf_files").split():
38        vm.copy_files_to(os.path.join(netperf_dir, i), "/tmp")
39
40    try:
41        session_serial.cmd(firewall_flush)
42    except aexpect.ShellError:
43        logging.warning("Could not flush firewall rules on guest")
44
45    session_serial.cmd(setup_cmd % "/tmp", timeout=200)
46    session_serial.cmd(params.get("netserver_cmd") % "/tmp")
47
48    if "tcpdump" in env and env["tcpdump"].is_alive():
49        # Stop the background tcpdump process
50        try:
51            logging.debug("Stopping the background tcpdump")
52            env["tcpdump"].close()
53        except:
54            pass
55
56    def netperf(i=0):
57        guest_ip = vm.get_address(i)
58        logging.info("Netperf_%s: netserver %s" % (i, guest_ip))
59        result_file = os.path.join(test.resultsdir, "output_%s_%s"
60                                   % (test.iteration, i ))
61        list_fail = []
62        result = open(result_file, "w")
63        result.write("Netperf test results\n")
64
65        for p in params.get("protocols").split():
66            packet_size = params.get("packet_size", "1500")
67            for size in packet_size.split():
68                cmd = params.get("netperf_cmd") % (netperf_dir, p,
69                                                   guest_ip, size)
70                logging.info("Netperf_%s: protocol %s" % (i, p))
71                try:
72                    netperf_output = utils.system_output(cmd,
73                                                         retain_output=True)
74                    result.write("%s\n" % netperf_output)
75                except:
76                    logging.error("Test of protocol %s failed", p)
77                    list_fail.append(p)
78
79        result.close()
80        if list_fail:
81            raise error.TestFail("Some netperf tests failed: %s" %
82                                 ", ".join(list_fail))
83
84    try:
85        logging.info("Setup and run netperf clients on host")
86        utils.run(setup_cmd % netperf_dir)
87
88        bg = []
89        nic_num = len(params.get("nics").split())
90        for i in range(nic_num):
91            bg.append(virt_utils.Thread(netperf, (i,)))
92            bg[i].start()
93
94        completed = False
95        while not completed:
96            completed = True
97            for b in bg:
98                if b.isAlive():
99                    completed = False
100    finally:
101        try:
102            for b in bg:
103                if b:
104                    b.join()
105        finally:
106            session_serial.cmd_output("killall netserver")
107