loop_topology_mininet   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A single_domain() 0 39 1
A int2dpid() 0 12 2
1
#!/usr/bin/python
2
3
"""
4
This example creates a single-controller network in a loop topology by
5
using the net.add*() API and manually starting the switches and controllers.
6
"""
7
8
from mininet.clean import Cleanup
9
from mininet.net import Mininet
10
from mininet.node import RemoteController
11
from mininet.cli import CLI
12
from mininet.log import setLogLevel
13
14
15
ip = '192.168.56.1'
16
17
18
def int2dpid(dpid):
19
    """
20
21
    :param dpid:
22
    :return:
23
    """
24
    try:
25
        dpid = hex(dpid)[2:]
26
        dpid = '0' * (16 - len(dpid)) + dpid
27
        return dpid
28
    except IndexError:
29
        raise Exception('Unable to derive default datapath ID - '
30
                        'please either specify a dpid or use a '
31
                        'canonical switch name such as s23.')
32
33
34
def single_domain():
35
    """Create a network from semi-scratch with multiple controllers."""
36
    net = Mininet(topo=None, build=False)
37
38
    # Add switches
39
    s1 = net.addSwitch('s1', listenPort=6601, dpid=int2dpid(1))
40
    s2 = net.addSwitch('s2', listenPort=6602, dpid=int2dpid(2))
41
    s3 = net.addSwitch('s3', listenPort=6603, dpid=int2dpid(3))
42
    s4 = net.addSwitch('s4', listenPort=6604, dpid=int2dpid(4))
43
    s5 = net.addSwitch('s5', listenPort=6605, dpid=int2dpid(5))
44
45
    # Add links
46
    net.addLink(s1, s2, port1=2, port2=2)
47
    net.addLink(s2, s3, port1=3, port2=3)
48
    net.addLink(s3, s4, port1=4, port2=4)
49
    net.addLink(s4, s5, port1=5, port2=5)
50
    net.addLink(s5, s1, port1=6, port2=6)
51
    net.addLink(s1, s3, port1=7, port2=7)
52
53
    # Add hosts
54
    h1 = net.addHost('h1', mac='dd:00:00:00:00:11')
55
    h2 = net.addHost('h2', mac='dd:00:00:00:00:22')
56
    h3 = net.addHost('h3', mac='dd:00:00:00:00:33')
57
    h4 = net.addHost('h4', mac='dd:00:00:00:00:44')
58
    h5 = net.addHost('h5', mac='dd:00:00:00:00:55')
59
60
    # Add links to switches
61
    net.addLink(s1, h1, port1=1, port2=1)
62
    net.addLink(s2, h2, port1=1, port2=1)
63
    net.addLink(s3, h3, port1=1, port2=1)
64
    net.addLink(s4, h4, port1=1, port2=1)
65
    net.addLink(s5, h5, port1=1, port2=1)
66
67
    net.addController('ctrl', controller=RemoteController, ip=ip, port=6633)
68
69
    net.build()
70
    net.start()
71
    CLI(net)
72
    net.stop()
73
74
75
if __name__ == '__main__':
76
    setLogLevel('info')  # for CLI output
77
    Cleanup.cleanup()
78
    single_domain()
79