1import logging, time, threading
2from autotest_lib.client.virt.tests import file_transfer
3from autotest_lib.client.virt import virt_test_utils, virt_utils
4
5
6def run_nic_bonding(test, params, env):
7    """
8    Nic bonding test in guest.
9
10    1) Start guest with four nic models.
11    2) Setup bond0 in guest by script nic_bonding_guest.py.
12    3) Execute file transfer test between guest and host.
13    4) Repeatedly put down/up interfaces by set_link
14    5) Execute file transfer test between guest and host.
15
16    @param test: Kvm test object.
17    @param params: Dictionary with the test parameters.
18    @param env: Dictionary with test environment.
19    """
20    timeout = int(params.get("login_timeout", 1200))
21    vm = env.get_vm(params["main_vm"])
22    vm.verify_alive()
23    session_serial = vm.wait_for_serial_login(timeout=timeout)
24
25    # get params of bonding
26    modprobe_cmd = "modprobe bonding"
27    bonding_params = params.get("bonding_params")
28    if bonding_params:
29        modprobe_cmd += " %s" % bonding_params
30    session_serial.cmd(modprobe_cmd)
31
32    session_serial.cmd("ifconfig bond0 up")
33    ifnames = [virt_test_utils.get_linux_ifname(session_serial,
34                                               vm.get_mac_address(vlan))
35               for vlan, nic in enumerate(params.get("nics").split())]
36    setup_cmd = "ifenslave bond0 " + " ".join(ifnames)
37    session_serial.cmd(setup_cmd)
38    session_serial.cmd("dhclient bond0")
39
40    try:
41        logging.info("Test file transfering:")
42        file_transfer.run_file_transfer(test, params, env)
43
44        logging.info("Failover test with file transfer")
45        transfer_thread = virt_utils.Thread(file_transfer.run_file_transfer,
46                                           (test, params, env))
47        try:
48            transfer_thread.start()
49            while transfer_thread.isAlive():
50                for vlan, nic in enumerate(params.get("nics").split()):
51                    device_id = vm.get_peer(vm.netdev_id[vlan])
52                    vm.monitor.cmd("set_link %s down" % device_id)
53                    time.sleep(1)
54                    vm.monitor.cmd("set_link %s up" % device_id)
55        except:
56            transfer_thread.join(suppress_exception=True)
57            raise
58        else:
59            transfer_thread.join()
60    finally:
61        session_serial.sendline("ifenslave -d bond0 " + " ".join(ifnames))
62        session_serial.sendline("kill -9 `pgrep dhclient`")
63