ovs_basic.py 10.2 KB
Newer Older
L
Lucas Meneghel Rodrigues 已提交
1 2 3
import logging
import time
import os
4
from virttest import utils_misc, aexpect, utils_net, openvswitch, ovs_utils
5
from virttest import versionable_class, data_dir
6
from autotest.client.shared import error
J
Jiří Župka 已提交
7 8 9 10 11 12 13 14


def allow_iperf_firewall(machine):
    machine.cmd("iptables -I INPUT -p tcp --dport 5001 --j ACCEPT")
    machine.cmd("iptables -I INPUT -p udp --dport 5001 --j ACCEPT")


class MiniSubtest(object):
L
Lucas Meneghel Rodrigues 已提交
15

J
Jiří Župka 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    def __new__(cls, *args, **kargs):
        self = super(MiniSubtest, cls).__new__(cls)
        ret = None
        if args is None:
            args = []
        try:
            if hasattr(self, "setup"):
                self.setup(*args, **kargs)

            ret = self.test(*args, **kargs)
        finally:
            if hasattr(self, "clean"):
                self.clean(*args, **kargs)
        return ret


class InfrastructureInit(MiniSubtest):
L
Lucas Meneghel Rodrigues 已提交
33

J
Jiří Župka 已提交
34 35
    def setup(self, test, params, env):
        self.br0_name = "br0-%s" % (utils_misc.generate_random_string(3))
36
        while self.br0_name in utils_net.get_net_if():
J
Jiří Župka 已提交
37 38 39 40 41 42 43 44 45 46 47
            self.br0_name = "br0-%s" % (utils_misc.generate_random_string(3))
        self.br0_ip = params.get("bridge_ip", "192.168.250.1")

        self.ovs = None

        error.context("Try to log into guest.")
        self.vms = [env.get_vm(vm) for vm in params.get("vms").split()]
        for vm in self.vms:
            vm.verify_alive()

        error.context("Start OpenVSwitch.")
48
        self.ovs = versionable_class.factory(openvswitch.OpenVSwitchSystem)()
J
Jiří Župka 已提交
49 50 51 52
        self.ovs.init_system()
        self.ovs.check()
        error.context("Add new bridge %s." % (self.br0_name))
        self.ovs.add_br(self.br0_name)
53 54 55
        utils_net.set_net_if_ip(self.br0_name, self.br0_ip)
        utils_net.bring_up_ifname(self.br0_name)
        self.dns_pidf = (utils_net.check_add_dnsmasq_to_br(self.br0_name,
L
Lucas Meneghel Rodrigues 已提交
56
                                                           test.tmpdir))
J
Jiří Župka 已提交
57
        error.context("Add new ports from vms %s to bridge %s." %
L
Lucas Meneghel Rodrigues 已提交
58
                     (self.vms, self.br0_name))
J
Jiří Župka 已提交
59 60

        for vm in self.vms:
61
            utils_net.change_iface_bridge(vm.virtnet[1],
L
Lucas Meneghel Rodrigues 已提交
62 63
                                          self.br0_name,
                                          self.ovs)
J
Jiří Župka 已提交
64 65 66 67 68 69

        logging.debug(self.ovs.status())
        self.host = ovs_utils.Machine(src=test.srcdir)
        self.mvms = [ovs_utils.Machine(vm) for vm in self.vms]
        self.machines = [self.host] + self.mvms

L
Lucas Meneghel Rodrigues 已提交
70
        # ForAllP(self.mvms).cmd("dhclinet")
J
Jiří Župka 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

        time.sleep(5)
        utils_misc.ForAllP(self.machines).fill_addrs()

    def clean(self, test, params, env):
        if self.ovs:
            try:
                if not self.dns_pidf is None:
                    utils_misc.signal_program(self.dns_pidf[0:-4],
                                              pid_files_dir=test.tmpdir)
            except:
                pass
            try:
                self.ovs.del_br(self.br0_name)
            except Exception:
                pass
            if self.ovs.cleanup:
                self.ovs.clean()


@error.context_aware
92
def run(test, params, env):
J
Jiří Župka 已提交
93 94 95 96
    """
    Run basic test of OpenVSwitch driver.
    """
    class test_ping(InfrastructureInit):
L
Lucas Meneghel Rodrigues 已提交
97

J
Jiří Župka 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        def test(self, test, params, env):
            count = params.get("ping_count", 10)
            for mvm in self.mvms:
                for p_mvm in self.mvms:
                    addr = None
                    if p_mvm.is_virtual():
                        addr = p_mvm.virtnet[1].ip["ipv6"][0]
                    else:
                        addr = p_mvm.addrs[self.br0_name]["ipv6"][0]

                    if p_mvm.is_virtual():
                        mvm.ping(addr, 1, count)
                    else:
                        mvm.ping(addr, self.br0_name, count)

    class test_iperf(InfrastructureInit):

L
Lucas Meneghel Rodrigues 已提交
115 116 117 118 119 120 121
        def start_servers(self):
            utils_misc.ForAllP(
                self.machines).cmd_in_src("%s -s &> /dev/null &" %
                                          (self.iperf_b_path))
            utils_misc.ForAllP(
                self.machines).cmd_in_src("%s -s -u &> /dev/null &" %
                                          (self.iperf_b_path))
J
Jiří Župka 已提交
122 123 124

        def iperf_client(self, machine, server_ip, add_params):
            out = machine.cmd_in_src("%s -c %s %s" %
L
Lucas Meneghel Rodrigues 已提交
125 126 127
                                    (self.iperf_b_path,
                                     server_ip,
                                     add_params))
J
Jiří Župka 已提交
128 129 130 131 132 133 134 135
            return " ".join(out.splitlines()[-1].split()[6:8])

        def test_bandwidth(self, add_params=None):
            if add_params is None:
                add_params = ""

            speeds = []
            speeds.append(self.iperf_client(self.mvms[0],
L
Lucas Meneghel Rodrigues 已提交
136 137 138
                                            self.host.addrs[
                                                self.br0_name]["ipv4"][0],
                                            add_params))
J
Jiří Župka 已提交
139 140

            speeds.append(self.iperf_client(self.host,
L
Lucas Meneghel Rodrigues 已提交
141 142 143
                                            self.mvms[0].virtnet[
                                                1].ip["ipv4"][0],
                                            add_params))
J
Jiří Župka 已提交
144 145

            speeds.append(self.iperf_client(self.mvms[0],
L
Lucas Meneghel Rodrigues 已提交
146 147 148
                                            self.mvms[1].virtnet[
                                                1].ip["ipv4"][0],
                                            add_params))
J
Jiří Župka 已提交
149 150 151 152

            return speeds

        def test(self, test, params, env):
153
            iperf_src_path = os.path.join(data_dir.get_deps_dir(), "iperf")
J
Jiří Župka 已提交
154 155 156
            self.iperf_b_path = os.path.join("iperf-2.0.4", "src", "iperf")

            error.context("Install iperf to vms machine.")
L
Lucas Meneghel Rodrigues 已提交
157 158
            utils_misc.ForAllP(
                self.machines).compile_autotools_app_tar(iperf_src_path,
J
Jiří Župka 已提交
159 160 161 162 163 164 165
                                                         "iperf-2.0.4.tar.gz")

            allow_iperf_firewall(self.host)
            utils_misc.ForAllP(self.mvms).cmd("iptables -F")

            self.start_servers()

L
Lucas Meneghel Rodrigues 已提交
166
            # Test TCP bandwidth
J
Jiří Župka 已提交
167 168 169 170 171 172
            error.context("Test iperf bandwidth tcp.")
            speeds = self.test_bandwidth()
            logging.info("TCP Bandwidth from vm->host: %s", speeds[0])
            logging.info("TCP Bandwidth from host->vm: %s", speeds[1])
            logging.info("TCP Bandwidth from vm->vm: %s", speeds[2])

L
Lucas Meneghel Rodrigues 已提交
173
            # test udp bandwidth limited to 1Gb
J
Jiří Župka 已提交
174 175 176 177 178 179 180 181 182 183 184
            error.context("Test iperf bandwidth udp.")
            speeds = self.test_bandwidth("-u -b 1G")
            logging.info("UDP Bandwidth from vm->host: %s", speeds[0])
            logging.info("UDP Bandwidth from host->vm: %s", speeds[1])
            logging.info("UDP Bandwidth from vm->vm: %s", speeds[2])

        def clean(self, test, params, env):
            self.host.cmd("killall -9 iperf")
            super(test_iperf, self).clean(test, params, env)

    class test_vlan_ping(InfrastructureInit):
L
Lucas Meneghel Rodrigues 已提交
185

J
Jiří Župka 已提交
186 187 188
        def test(self, test, params, env):
            count = params.get("ping_count", 10)
            ret = utils_misc.ForAllPSE(self.mvms).ping(
L
Lucas Meneghel Rodrigues 已提交
189 190
                self.host.addrs[self.br0_name]["ipv6"][0],
                1, count)
J
Jiří Župka 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
            for ret, vm in zip(ret, self.mvms):
                if "exception" in ret:
                    raise error.TestError("VM %s can't ping host:\n %s" %
                                          (vm.name, ret.exception))

            error.context("Add OpenVSwitch device to vlan.")
            self.ovs.add_port_tag(self.mvms[0].virtnet[1].ifname, "1")
            self.ovs.add_port_tag(self.mvms[1].virtnet[1].ifname, "1")
            self.ovs.add_port_tag(self.mvms[2].virtnet[1].ifname, "2")
            self.ovs.add_port_tag(self.mvms[3].virtnet[1].ifname, "2")

            error.context("Ping all devices in vlan.")
            self.mvms[2].ping(self.mvms[3].virtnet[1].ip["ipv6"][0], 1, 2)
            self.mvms[3].ping(self.mvms[2].virtnet[1].ip["ipv6"][0], 1, 2)

            self.mvms[0].ping(self.mvms[1].virtnet[1].ip["ipv6"][0], 1, 1)
            self.mvms[1].ping(self.mvms[0].virtnet[1].ip["ipv6"][0], 1, 1)

            try:
                self.mvms[0].ping(self.mvms[2].virtnet[1].ip["ipv6"][0],
L
Lucas Meneghel Rodrigues 已提交
211
                                  1, 2)
J
Jiří Župka 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
                raise error.TestError("VM %s can't ping host:\n %s" %
                                      (vm.name, ret.exception))
            except (error.CmdError, aexpect.ShellError):
                pass

            self.mvms[0].add_vlan_iface(self.mvms[0].virtnet[1].g_nic_name, 1)
            self.mvms[0].add_vlan_iface(self.mvms[0].virtnet[1].g_nic_name, 2)

            self.ovs.add_port_tag(self.mvms[0].virtnet[1].ifname, "[]")
            self.ovs.add_port_trunk(self.mvms[0].virtnet[1].ifname, [1, 2])

            time.sleep(1)
            error.context("Ping all devices in vlan.")
            self.mvms[0].ping(self.mvms[1].virtnet[1].ip["ipv6"][0], 1,
L
Lucas Meneghel Rodrigues 已提交
226
                              count, vlan=1)
J
Jiří Župka 已提交
227
            self.mvms[0].ping(self.mvms[2].virtnet[1].ip["ipv6"][0], 1,
L
Lucas Meneghel Rodrigues 已提交
228
                              count, vlan=2)
J
Jiří Župka 已提交
229
            self.mvms[1].ping(self.mvms[0].virtnet[1].ip["ipv6"][0], 1,
L
Lucas Meneghel Rodrigues 已提交
230
                              count)
J
Jiří Župka 已提交
231
            self.mvms[2].ping(self.mvms[0].virtnet[1].ip["ipv6"][0], 1,
L
Lucas Meneghel Rodrigues 已提交
232
                              count)
J
Jiří Župka 已提交
233 234 235

            try:
                self.mvms[0].ping(self.mvms[2].virtnet[1].ip["ipv6"][0],
L
Lucas Meneghel Rodrigues 已提交
236
                                  1, 2)
J
Jiří Župka 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                raise error.TestError("VM %s shouldn't be able to ping"
                                      " host:\n %s" % (vm.name, ret.exception))
            except (error.CmdError, aexpect.ShellError):
                pass

            for i in range(0, 4095, 10):
                self.ovs.add_port_tag(self.mvms[0].virtnet[1].ifname, "[]")
                self.ovs.add_port_trunk(self.mvms[0].virtnet[1].ifname, [i])

            self.ovs.add_port_trunk(self.mvms[0].virtnet[1].ifname,
                                    range(4095))

            self.ovs.add_port_trunk(self.mvms[0].virtnet[1].ifname, [1])

            self.mvms[0].ping(self.mvms[1].virtnet[1].ip["ipv6"][0], 1,
L
Lucas Meneghel Rodrigues 已提交
252
                              count, vlan=1)
J
Jiří Župka 已提交
253 254 255 256 257 258 259 260

    test_type = "test_" + params.get("test_type")
    if (test_type in locals()):
        tests_group = locals()[test_type]
        tests_group(test, params, env)
    else:
        raise error.TestFail("Test type '%s' is not defined in"
                             " OpenVSwitch basic test" % test_type)