1import logging, threading, os, time
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.bin import utils
4from autotest_lib.client.virt.tests import file_transfer
5from autotest_lib.client.virt import virt_test_utils, virt_utils
6
7
8def run_nicdriver_unload(test, params, env):
9    """
10    Test nic driver.
11
12    1) Boot a VM.
13    2) Get the NIC driver name.
14    3) Repeatedly unload/load NIC driver.
15    4) Multi-session TCP transfer on test interface.
16    5) Check whether the test interface should still work.
17
18    @param test: KVM test object.
19    @param params: Dictionary with the test parameters.
20    @param env: Dictionary with test environment.
21    """
22    timeout = int(params.get("login_timeout", 360))
23    vm = env.get_vm(params["main_vm"])
24    vm.verify_alive()
25    session_serial = vm.wait_for_serial_login(timeout=timeout)
26
27    ethname = virt_test_utils.get_linux_ifname(session_serial,
28                                               vm.get_mac_address(0))
29
30    # get ethernet driver from '/sys' directory.
31    # ethtool can do the same thing and doesn't care about os type.
32    # if we make sure all guests have ethtool, we can make a change here.
33    sys_path = params.get("sys_path") % (ethname)
34
35    # readlink in RHEL4.8 doesn't have '-e' param, should use '-f' in RHEL4.8.
36    readlink_cmd = params.get("readlink_command", "readlink -e")
37    driver = os.path.basename(session_serial.cmd("%s %s" % (readlink_cmd,
38                                                 sys_path)).strip())
39
40    logging.info("driver is %s", driver)
41
42    try:
43        threads = []
44        for t in range(int(params.get("sessions_num", "10"))):
45            thread = virt_utils.Thread(file_transfer.run_file_transfer,
46                                      (test, params, env))
47            thread.start()
48            threads.append(thread)
49
50        time.sleep(10)
51        while threads[0].isAlive():
52            session_serial.cmd("sleep 10")
53            session_serial.cmd("ifconfig %s down" % ethname)
54            session_serial.cmd("modprobe -r %s" % driver)
55            session_serial.cmd("modprobe %s" % driver)
56            session_serial.cmd("ifconfig %s up" % ethname)
57    except:
58        for thread in threads:
59            thread.join(suppress_exception=True)
60            raise
61    else:
62        for thread in threads:
63            thread.join()
64