ovs-test.in 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#! @PYTHON@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
ovs test utility that allows to do tests between remote hosts
"""

19
import fcntl
20
import math
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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
import os
import select
import signal
import socket
import subprocess
import sys
import time
import xmlrpclib

import argparse
import twisted

import ovstest.args as args
import ovstest.rpcserver as rpcserver

DEFAULT_TEST_BRIDGE = "ovstestbr0"
DEFAULT_TEST_PORT = "ovstestport0"
DEFAULT_TEST_TUN = "ovstestport1"


def rpc_client(ip, port):
    return xmlrpclib.Server("http://%s:%u/" % (ip, port), allow_none=True)


def sigint_intercept():
    """
    Intercept SIGINT from child (the local ovs-test server process).
    """
    signal.signal(signal.SIGINT, signal.SIG_IGN)


def start_local_server(port):
    """
    This function spawns an ovs-test server that listens on specified port
    and blocks till the spawned ovs-test server is ready to accept XML RPC
    connections.
    """
    p = subprocess.Popen(["ovs-test", "-s", str(port)],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         preexec_fn = sigint_intercept)
    fcntl.fcntl( p.stdout.fileno(),fcntl.F_SETFL,
        fcntl.fcntl(p.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)

    while p.poll() is None:
        fd = select.select([p.stdout.fileno()], [], [])[0]
        if fd:
            out = p.stdout.readline()
            if out.startswith("Starting RPC server"):
                break
    if p.poll() is not None:
        raise RuntimeError("Couldn't start local instance of ovs-test server")
    return p


def get_datagram_sizes(mtu1, mtu2):
    """
    This function calculates all the "interesting" datagram sizes so that
    we test both - receive and send side with different packets sizes.
    """
    s1 = set([8, mtu1 - 100, mtu1 - 28, mtu1])
    s2 = set([8, mtu2 - 100, mtu2 - 28, mtu2])
    return sorted(s1.union(s2))


def ip_from_cidr(string):
    """
    This function removes the netmask (if present) from the given string and
    returns the IP address.
    """
    token = string.split("/")
    return token[0]
92 93 94


def bandwidth_to_string(bwidth):
95 96
    """Convert bandwidth from long to string and add units."""
    bwidth = bwidth * 8  # Convert back to bits/second
97 98 99 100 101 102 103 104 105 106 107
    if bwidth >= 10000000:
        return str(int(bwidth / 1000000)) + "Mbps"
    elif bwidth > 10000:
        return str(int(bwidth / 1000)) + "Kbps"
    else:
        return str(int(bwidth)) + "bps"


def collect_information(node):
    """Print information about hosts that will do testing"""
    print "Node %s:%u " % (node[0], node[1])
108 109 110 111
    server = rpc_client(node[0], node[1])
    interface_name = server.get_interface(node[0])
    phys_iface = None
    uname = server.uname()
112 113
    mtu = 1500

114
    if not interface_name:
115
        print ("Could not find interface that has %s IP address."
116
               "Make sure that you specified correct Outer IP." % (node[0]))
117
    else:
118 119 120 121 122 123 124 125 126 127
        if server.is_ovs_bridge(interface_name):
            phys_iface = server.get_iface_from_bridge(interface_name)
        else:
            phys_iface = interface_name

    if phys_iface:
        driver = server.get_driver(phys_iface)
        mtu = server.get_interface_mtu(phys_iface)

        print "Will be using %s (%s) with MTU %u" % (phys_iface, node[0],
128
                                                    mtu)
129 130
        if not driver:
            print "Unable to get driver information from ethtool."
131
        else:
132
            print "On this host %s has %s." % (phys_iface, driver)
133

134
    if not uname:
135 136 137 138
        print "Unable to retrieve kernel information. Is this Linux?"
    else:
        print "Running kernel %s." % uname
    print "\n"
139

140 141 142
    return mtu


143
def do_udp_tests(receiver, sender, tbwidth, duration, port_sizes):
144
    """Schedule UDP tests between receiver and sender"""
145 146
    server1 = rpc_client(receiver[0], receiver[1])
    server2 = rpc_client(sender[0], sender[1])
147 148 149 150 151 152 153 154 155

    udpformat = '{0:>15} {1:>15} {2:>15} {3:>15} {4:>15}'

    print ("UDP test from %s:%u to %s:%u with target bandwidth %s" %
                            (sender[0], sender[1], receiver[0], receiver[1],
                             bandwidth_to_string(tbwidth)))
    print udpformat.format("Datagram Size", "Snt Datagrams", "Rcv Datagrams",
                            "Datagram Loss", "Bandwidth")

156
    for size in port_sizes:
157 158 159 160 161 162 163 164 165 166 167
        listen_handle = -1
        send_handle = -1
        try:
            packetcnt = (tbwidth * duration) / size

            listen_handle = server1.create_udp_listener(receiver[3])
            if listen_handle == -1:
                print ("Server could not open UDP listening socket on port"
                        " %u. Try to restart the server.\n" % receiver[3])
                return
            send_handle = server2.create_udp_sender(
168 169 170
                                            (ip_from_cidr(receiver[2]),
                                             receiver[3]), packetcnt, size,
                                             duration)
171

172 173
            # Using sleep here because there is no other synchronization source
            # that would notify us when all sent packets were received
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
            time.sleep(duration + 1)

            rcv_packets = server1.get_udp_listener_results(listen_handle)
            snt_packets = server2.get_udp_sender_results(send_handle)

            loss = math.ceil(((snt_packets - rcv_packets) * 10000.0) /
                                                        snt_packets) / 100
            bwidth = (rcv_packets * size) / duration

            print udpformat.format(size, snt_packets, rcv_packets,
                                '%.2f%%' % loss, bandwidth_to_string(bwidth))
        finally:
            if listen_handle != -1:
                server1.close_udp_listener(listen_handle)
            if send_handle != -1:
                server2.close_udp_sender(send_handle)
    print "\n"


def do_tcp_tests(receiver, sender, duration):
    """Schedule TCP tests between receiver and sender"""
195 196
    server1 = rpc_client(receiver[0], receiver[1])
    server2 = rpc_client(sender[0], sender[1])
197 198 199 200 201 202 203 204 205 206 207 208 209 210

    tcpformat = '{0:>15} {1:>15} {2:>15}'
    print "TCP test from %s:%u to %s:%u (full speed)" % (sender[0], sender[1],
                                                    receiver[0], receiver[1])
    print tcpformat.format("Snt Bytes", "Rcv Bytes", "Bandwidth")

    listen_handle = -1
    send_handle = -1
    try:
        listen_handle = server1.create_tcp_listener(receiver[3])
        if listen_handle == -1:
            print ("Server was unable to open TCP listening socket on port"
                    " %u. Try to restart the server.\n" % receiver[3])
            return
211 212
        send_handle = server2.create_tcp_sender(ip_from_cidr(receiver[2]),
                                                receiver[3], duration)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

        time.sleep(duration + 1)

        rcv_bytes = long(server1.get_tcp_listener_results(listen_handle))
        snt_bytes = long(server2.get_tcp_sender_results(send_handle))

        bwidth = rcv_bytes / duration

        print tcpformat.format(snt_bytes, rcv_bytes,
                               bandwidth_to_string(bwidth))
    finally:
        if listen_handle != -1:
            server1.close_tcp_listener(listen_handle)
        if send_handle != -1:
            server2.close_tcp_sender(send_handle)
    print "\n"


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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
def do_l3_tests(node1, node2, bandwidth, duration, ps, type):
    """
    Do L3 tunneling tests.
    """
    server1 = rpc_client(node1[0], node1[1])
    server2 = rpc_client(node2[0], node2[1])
    servers_with_bridges = []
    try:
        server1.create_bridge(DEFAULT_TEST_BRIDGE)
        servers_with_bridges.append(server1)
        server2.create_bridge(DEFAULT_TEST_BRIDGE)
        servers_with_bridges.append(server2)

        server1.interface_up(DEFAULT_TEST_BRIDGE)
        server2.interface_up(DEFAULT_TEST_BRIDGE)

        server1.interface_assign_ip(DEFAULT_TEST_BRIDGE, node1[2], None)
        server2.interface_assign_ip(DEFAULT_TEST_BRIDGE, node2[2], None)

        server1.add_port_to_bridge(DEFAULT_TEST_BRIDGE, DEFAULT_TEST_TUN)
        server2.add_port_to_bridge(DEFAULT_TEST_BRIDGE, DEFAULT_TEST_TUN)

        server1.ovs_vsctl_set("Interface", DEFAULT_TEST_TUN, "type",
                              None, type)
        server2.ovs_vsctl_set("Interface", DEFAULT_TEST_TUN, "type",
                              None, type)
        server1.ovs_vsctl_set("Interface", DEFAULT_TEST_TUN, "options",
                              "remote_ip", node2[0])
        server2.ovs_vsctl_set("Interface", DEFAULT_TEST_TUN, "options",
                              "remote_ip", node1[0])

        do_udp_tests(node1, node2, bandwidth, duration, ps)
        do_udp_tests(node2, node1, bandwidth, duration, ps)
        do_tcp_tests(node1, node2, duration)
        do_tcp_tests(node2, node1, duration)

    finally:
        for server in servers_with_bridges:
            server.del_bridge(DEFAULT_TEST_BRIDGE)



def do_vlan_tests(node1, node2, bandwidth, duration, ps, tag):
    """
    Do VLAN tests between node1 and node2.
    """
    server1 = rpc_client(node1[0], node1[1])
    server2 = rpc_client(node2[0], node2[1])

    br_name1 = None
    br_name2 = None

    servers_with_test_ports = []

    try:
        interface_node1 = server1.get_interface(node1[0])
        interface_node2 = server2.get_interface(node2[0])

        if server1.is_ovs_bridge(interface_node1):
            br_name1 = interface_node1
        else:
            br_name1 = DEFAULT_TEST_BRIDGE
            server1.create_test_bridge(br_name1, interface_node1)

        if server2.is_ovs_bridge(interface_node2):
            br_name2 = interface_node2
        else:
            br_name2 = DEFAULT_TEST_BRIDGE
            server2.create_test_bridge(br_name2, interface_node2)

        server1.add_port_to_bridge(br_name1, DEFAULT_TEST_PORT)
        servers_with_test_ports.append(server1)
        server2.add_port_to_bridge(br_name2, DEFAULT_TEST_PORT)
        servers_with_test_ports.append(server2)

        server1.ovs_vsctl_set("Port", DEFAULT_TEST_PORT, "tag", None, tag)
        server2.ovs_vsctl_set("Port", DEFAULT_TEST_PORT, "tag", None, tag)

        server1.ovs_vsctl_set("Interface", DEFAULT_TEST_PORT, "type", None,
                              "internal")
        server2.ovs_vsctl_set("Interface", DEFAULT_TEST_PORT, "type", None,
                              "internal")

        server1.interface_assign_ip(DEFAULT_TEST_PORT, node1[2], None)
        server2.interface_assign_ip(DEFAULT_TEST_PORT, node2[2], None)

        server1.interface_up(DEFAULT_TEST_PORT)
        server2.interface_up(DEFAULT_TEST_PORT)

        do_udp_tests(node1, node2, bandwidth, duration, ps)
        do_udp_tests(node2, node1, bandwidth, duration, ps)
        do_tcp_tests(node1, node2, duration)
        do_tcp_tests(node2, node1, duration)

    finally:
        for server in servers_with_test_ports:
            server.del_port_from_bridge(DEFAULT_TEST_PORT)
        if br_name1 == DEFAULT_TEST_BRIDGE:
            server1.del_test_bridge(br_name1, interface_node1)
        if br_name2 == DEFAULT_TEST_BRIDGE:
            server2.del_test_bridge(br_name2, interface_node2)


def do_direct_tests(node1, node2, bandwidth, duration, ps):
    """
    Do tests between outer IPs without involving Open vSwitch
    """
    n1 = (node1[0], node1[1], node1[0], node1[3])
    n2 = (node2[0], node2[1], node2[0], node2[3])

    do_udp_tests(n1, n2, bandwidth, duration, ps)
    do_udp_tests(n2, n1, bandwidth, duration, ps)
    do_tcp_tests(n1, n2, duration)
    do_tcp_tests(n2, n1, duration)


347
if __name__ == '__main__':
348
    local_server = None
349 350 351
    try:
        ovs_args = args.ovs_initialize_args()

352 353
        if ovs_args.port is not None:  # Start in pure server mode
            rpcserver.start_rpc_server(ovs_args.port)
354

355
        elif ovs_args.servers is not None:  # Run in client mode
356 357 358
            node1 = ovs_args.servers[0]
            node2 = ovs_args.servers[1]

359 360 361 362 363 364 365 366 367 368 369
            # Verify whether client will need to spawn a local instance of
            # ovs-test server by looking at the first OuterIP. if it is a
            # 127.0.0.1 then spawn local ovs-test server.
            if node1[0] == "127.0.0.1":
                local_server = start_local_server(node1[1])
                # We must determine the IP address that local ovs-test server
                # will use:
                me = rpc_client(node1[0], node1[1])
                my_ip = me.get_my_address_from(node2[0], node2[1])
                node1 = (my_ip, node1[1], node1[2], node1[3])

370
            mtu_node2 = collect_information(node2)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
            mtu_node1 = collect_information(node1)

            bandwidth = ovs_args.targetBandwidth
            interval = ovs_args.testInterval
            ps = get_datagram_sizes(mtu_node1, mtu_node2)

            direct = ovs_args.direct
            vlan_tag = ovs_args.vlanTag
            tunnel_modes = ovs_args.tunnelModes

            if direct is not None:
                print "Performing direct tests"
                do_direct_tests(node2, node1, bandwidth, interval, ps)

            if vlan_tag is not None:
                print "Performing VLAN tests"
                do_vlan_tests(node2, node1, bandwidth, interval, ps, vlan_tag)

            for tmode in tunnel_modes:
                print "Performing", tmode, "tests"
                do_l3_tests(node2, node1, bandwidth, interval, ps, tmode)
392 393 394

    except KeyboardInterrupt:
        pass
395 396
    except xmlrpclib.Fault:
        print "Couldn't establish XMLRPC control channel"
397 398
    except socket.error:
        print "Couldn't establish XMLRPC control channel"
399 400 401 402 403 404 405
    except xmlrpclib.ProtocolError:
        print "XMLRPC control channel was abruptly terminated"
    except twisted.internet.error.CannotListenError:
        print "Couldn't start XMLRPC server on port %u" % ovs_args.port
    finally:
        if local_server is not None:
            local_server.terminate()