adfcb51a1d1466f91f2979f50b1c2f0417193c93
[groupbasedpolicy.git] / util / testOfOverlay / mininet_gbp.py
1
2 from mininet.topo import Topo
3 from mininet.node import RemoteController
4 from mininet.net import Mininet
5 from mininet.util import dumpNodeConnections
6 from mininet.log import setLogLevel
7 from mininet.node import Node
8
9 import re
10 import time
11 from subprocess import call
12 from subprocess import check_output
13
14 def addController(sw, ip):
15     call(['ovs-vsctl', 'set-controller', sw, 'tcp:%s:6653' % ip ])
16
17 def addSwitch(net, name, dpid=None):
18     switch = net.addSwitch(name, dpid=dpid)
19     return switch
20
21 def addHost(net, switch, name, ip, mac):
22     host = net.addHost(name, ip=ip, mac=mac)
23     net.addLink(host, switch)
24
25 def setOFVersion(sw, version='OpenFlow13,OpenFlow12,OpenFlow10'):
26     call(['ovs-vsctl', 'set', 'bridge', sw, 'protocols={}'.format(version)])
27
28 def addTunnel(sw, sourceIp=None):
29     ifaceName = '{}_vxlan0'.format(sw)
30     cmd = ['ovs-vsctl', 'add-port', sw, ifaceName,
31            '--', 'set', 'Interface', ifaceName,
32            'type=vxlan', 
33            'options:remote_ip=flow',
34            'options:key=flow']
35     if sourceIp is not None:
36         cmd.append('options:source_ip={}'.format(sourceIp))
37     call(cmd)
38
39 #ovs-ofctl dump-ports-desc s1 -OOpenFlow13
40
41 def startMininet(switches, hosts, contIP='127.0.0.1'):
42     setLogLevel('info')
43
44     net = Mininet(controller=None,
45                   autoSetMacs=True,
46                   listenPort=6634)
47
48     swobjs = {}
49     swports = {}
50
51     for sw in switches:
52         swobj = addSwitch(net, sw['name'])
53         swobjs[sw['name']] = swobj
54         swports[sw['name']] = 0;
55     for host in hosts:
56         if host['switch'] not in swobjs:
57             continue
58         sw = swobjs[host['switch']]
59         swports[host['switch']] += 1;
60         port = swports[host['switch']]
61         addHost(net, sw, host['name'], host['ip'], host['mac'])
62         host['port'] = port
63
64     try:
65         net.start()
66         for sw in switches:
67             addTunnel(sw['name'], sw['tunnelIp'])
68
69         for host in net.hosts:
70             gw = re.sub(r'.\d+$', ".1", host.IP())
71             host.cmd('route add default gw {}'.format(gw))
72
73         # ODL is very fragile so let's give it some time
74         time.sleep(1)
75
76         # This is a workaround for a bug encountered during
77         # the Helium release. Setting the vSwitch from 1.0
78         # to 1.3 while it was connected to the controller
79         # exposed a bug in the openflowplugin, which resulted
80         # in the controller missing some of the ports on the
81         # vswitch. This change avoids the bug by switching 
82         # the version before connecting the switch to the
83         # controller.
84         for sw in switches:
85             setOFVersion(sw['name'])
86             addController(sw['name'], contIP)
87
88         return net
89     except Exception, e:
90         net.stop()
91         raise e