pktgen.py 5.3 KB
Newer Older
Y
Yunping Zheng 已提交
1 2 3 4
import logging
import re
import time
import os
5 6 7

import aexpect

Y
Yunping Zheng 已提交
8 9
from autotest.client import utils
from autotest.client.shared import error
10

X
Xu Tian 已提交
11 12 13 14 15
from virttest import remote
from virttest import data_dir
from virttest import utils_net
from virttest import utils_test
from virttest import utils_misc
Y
Yunping Zheng 已提交
16 17 18


@error.context_aware
19
def run(test, params, env):
Y
Yunping Zheng 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
    """
    Run Pktgen test between host/guest

    1) Boot the main vm, or just grab it if it's already booted.
    2) Configure pktgen server(only linux)
    3) Run pktgen test, finish when timeout or env["pktgen_run"] != True

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """

    login_timeout = float(params.get("login_timeout", 360))
    error.context("Init the VM, and try to login", logging.info)
X
Xu Tian 已提交
34 35 36 37
    external_host = params.get("external_host")
    if not external_host:
        get_host_cmd = "ip route | awk '/default/ {print $3}'"
        external_host = utils.system_output(get_host_cmd)
Y
Yunping Zheng 已提交
38 39
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
X
Xu Tian 已提交
40
    session = vm.wait_for_login(timeout=login_timeout)
Y
Yunping Zheng 已提交
41 42

    error.context("Pktgen server environment prepare", logging.info)
43
    # pktgen server only support linux, since pktgen is a linux kernel module
Y
Yunping Zheng 已提交
44 45 46 47 48
    pktgen_server = params.get("pktgen_server", "localhost")
    params_server = params.object_params("pktgen_server")
    s_shell_client = params_server.get("shell_client", "ssh")
    s_shell_port = params_server.get("shell_port", "22")
    s_username = params_server.get("username", "root")
49
    s_passwd = params_server.get("password", "123456")
Y
Yunping Zheng 已提交
50 51 52
    s_shell_prompt = params_server.get("shell_prompt")

    server_session = ""
53
    # pktgen server is autotest virtual guest(only linux)
Y
Yunping Zheng 已提交
54 55 56 57 58 59 60 61 62
    if pktgen_server in params.get("vms", "vm1 vm2"):
        vm_pktgen = env.get_vm(pktgen_server)
        vm_pktgen.verify_alive()
        server_session = vm_pktgen.wait_for_login(timeout=login_timeout)
        runner = server_session.cmd_output_safe
        pktgen_ip = vm_pktgen.get_address()
        pktgen_mac = vm_pktgen.get_mac_address()
        server_interface = utils_net.get_linux_ifname(server_session,
                                                      pktgen_mac)
63
    # pktgen server is a external host assigned
Y
Yunping Zheng 已提交
64 65 66 67 68 69 70 71 72 73
    elif re.match(r"((\d){1,3}\.){3}(\d){1,3}", pktgen_server):
        pktgen_ip = pktgen_server
        server_session = remote.wait_for_login(s_shell_client, pktgen_ip,
                                               s_shell_port, s_username,
                                               s_passwd, s_shell_prompt)
        runner = server_session.cmd_output_safe
        server_interface = params.get("server_interface")
        if not server_interface:
            raise error.TestNAError("Must config server interface before test")
    else:
74
        # using host as a pktgen server
Y
Yunping Zheng 已提交
75 76 77 78 79 80
        server_interface = params.get("netdst", "switch")
        host_nic = utils_net.Interface(server_interface)
        pktgen_ip = host_nic.get_ip()
        pktgen_mac = host_nic.get_mac()
        runner = utils.system

81
    # copy pktgen_test scipt to the test server.
Y
Yunping Zheng 已提交
82 83 84 85 86 87
    local_path = os.path.join(data_dir.get_root_dir(),
                              "shared/scripts/pktgen.sh")
    remote_path = "/tmp/pktgen.sh"
    remote.scp_to_remote(pktgen_ip, s_shell_port, s_username, s_passwd,
                         local_path, remote_path)

X
Xu Tian 已提交
88
    error.context("Run pktgen test", logging.info)
Y
Yunping Zheng 已提交
89 90 91 92 93 94 95
    run_threads = params.get("pktgen_threads", 1)
    pktgen_stress_timeout = float(params.get("pktgen_test_timeout", 600))
    exec_cmd = "%s %s %s %s %s" % (remote_path, vm.get_address(),
                                   vm.get_mac_address(),
                                   server_interface, run_threads)
    try:
        env["pktgen_run"] = True
96
        try:
97 98
            # Set a run flag in env, when other case call this case as a sub
            # backgroud process, can set run flag to False to stop this case.
99 100
            start_time = time.time()
            stop_time = start_time + pktgen_stress_timeout
X
Xu Tian 已提交
101
            while (env["pktgen_run"] and time.time() < stop_time):
102
                runner(exec_cmd, timeout=pktgen_stress_timeout)
Y
Yunping Zheng 已提交
103

104
        # using ping to kill the pktgen stress
105
        except aexpect.ShellTimeoutError:
X
Xu Tian 已提交
106
            session.cmd("ping %s" % pktgen_ip, ignore_all_errors=True)
Y
Yunping Zheng 已提交
107 108 109
    finally:
        env["pktgen_run"] = False

X
Xu Tian 已提交
110 111 112
    error.context("Verify Host and guest kernel no error and call trace",
                  logging.info)
    vm.verify_kernel_crash()
113
    utils_misc.verify_dmesg()
X
Xu Tian 已提交
114 115

    error.context("Ping external host after pktgen test", logging.info)
116 117
    session_ping = vm.wait_for_login(timeout=login_timeout)
    status, output = utils_test.ping(dest=external_host, session=session_ping,
X
Xu Tian 已提交
118 119
                                     timeout=240, count=20)
    loss_ratio = utils_test.get_loss_ratio(output)
L
Lukáš Doktor 已提交
120 121
    if (loss_ratio > int(params.get("packet_lost_ratio", 5)) or
            loss_ratio == -1):
X
Xu Tian 已提交
122 123
        logging.debug("Ping %s output: %s" % (external_host, output))
        raise error.TestFail("Guest network connction unusable," +
124
                             "packet lost ratio is '%d%%'" % loss_ratio)
Y
Yunping Zheng 已提交
125 126 127 128
    if server_session:
        server_session.close()
    if session:
        session.close()
129 130
    if session_ping:
        session_ping.close()