ovs_basic.py 9.9 KB
Newer Older
1 2 3
import logging, time, os
from virttest import utils_misc, aexpect, utils_net, openvswitch, ovs_utils
from autotest.client.shared import error
J
Jiří Župka 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


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):
    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):
    def setup(self, test, params, env):
        self.br0_name = "br0-%s" % (utils_misc.generate_random_string(3))
31
        while self.br0_name in utils_net.get_net_if():
J
Jiří Župka 已提交
32 33 34 35 36 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.")
        self.ovs = openvswitch.OpenVSwitchSystem()
        self.ovs.init_system()
        self.ovs.check()
        error.context("Add new bridge %s." % (self.br0_name))
        self.ovs.add_br(self.br0_name)
48 49 50
        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,
J
Jiří Župka 已提交
51 52 53 54 55
                                                            test.tmpdir))
        error.context("Add new ports from vms %s to bridge %s." %
                        (self.vms, self.br0_name))

        for vm in self.vms:
56
            utils_net.change_iface_bridge(vm.virtnet[1],
J
Jiří Župka 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                                           self.br0_name,
                                           self.ovs)


        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

        #ForAllP(self.mvms).cmd("dhclinet")

        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
def run_ovs_basic(test, params, env):
    """
    Run basic test of OpenVSwitch driver.
    """
    class test_ping(InfrastructureInit):
        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):
        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))


        def iperf_client(self, machine, server_ip, add_params):
            out = machine.cmd_in_src("%s -c %s %s" %
                                (self.iperf_b_path,
                                 server_ip,
                                 add_params))
            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],
                                    self.host.addrs[self.br0_name]["ipv4"][0],
                                    add_params))


            speeds.append(self.iperf_client(self.host,
                                    self.mvms[0].virtnet[1].ip["ipv4"][0],
                                    add_params))


            speeds.append(self.iperf_client(self.mvms[0],
                                         self.mvms[1].virtnet[1].ip["ipv4"][0],
                                         add_params))

            return speeds


        def test(self, test, params, env):
            iperf_src_path = os.path.join(test.virtdir, "deps")
            self.iperf_b_path = os.path.join("iperf-2.0.4", "src", "iperf")

            error.context("Install iperf to vms machine.")
            utils_misc.ForAllP(self.machines).compile_autotools_app_tar(iperf_src_path,
                                                         "iperf-2.0.4.tar.gz")

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

            self.start_servers()

            #Test TCP bandwidth
            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])

            #test udp bandwidth limited to 1Gb
            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):
        def test(self, test, params, env):
            count = params.get("ping_count", 10)
            ret = utils_misc.ForAllPSE(self.mvms).ping(
                                    self.host.addrs[self.br0_name]["ipv6"][0],
                                    1, count)
            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],
                                     1, 2)
                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,
                                 count, vlan=1)
            self.mvms[0].ping(self.mvms[2].virtnet[1].ip["ipv6"][0], 1,
                                 count, vlan=2)
            self.mvms[1].ping(self.mvms[0].virtnet[1].ip["ipv6"][0], 1,
                                 count)
            self.mvms[2].ping(self.mvms[0].virtnet[1].ip["ipv6"][0], 1,
                                 count)

            try:
                self.mvms[0].ping(self.mvms[2].virtnet[1].ip["ipv6"][0],
                                     1, 2)
                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,
                                count, vlan=1)


    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)