Showing
2 changed files
with
29 additions
and
80 deletions
tools/test/topos/tower.py
100644 → 100755
1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
2 | + | ||
3 | +from mininet.topo import Topo | ||
2 | from mininet.cli import CLI | 4 | from mininet.cli import CLI |
3 | from mininet.net import Mininet | 5 | from mininet.net import Mininet |
4 | from mininet.node import RemoteController, OVSKernelSwitch | 6 | from mininet.node import RemoteController, OVSKernelSwitch |
7 | +from mininet.log import setLogLevel | ||
5 | 8 | ||
6 | -MAC = 12 | ||
7 | -DPID = 16 | ||
8 | - | ||
9 | -def string_to_hex(s, length): | ||
10 | - """ Convert a string like 00:00 in to hex 0x0000 format""" | ||
11 | - tmp = '{0:#x}'.format(int(s.replace(':', '').lstrip('0'),length)) | ||
12 | - return tmp | ||
13 | - | ||
14 | -def hex_to_string(h, length): | ||
15 | - """Convert a hex number from 0x0000 to 00:00 format""" | ||
16 | - tmp = h.lstrip('0x').zfill(length) | ||
17 | - tmp = ':'.join(a+b for a,b in zip(tmp[::2], tmp[1::2])) | ||
18 | - return tmp | ||
19 | - | ||
20 | -class Tower(object): | ||
21 | - """ Create a tower topology from semi-scratch in Mininet """ | ||
22 | 9 | ||
23 | - def __init__(self, cname='flare', cip='15.255.126.183', k=4, h=6, | 10 | +class TowerTopo( Topo ): |
24 | - proto=None): | 11 | + """Create a tower topology""" |
25 | - """Create tower topology for mininet | ||
26 | - cname: controller name | ||
27 | - cip: controller ip | ||
28 | - k: number of leaf switches | ||
29 | - h: number of hosts perl leaf switch | ||
30 | - """ | ||
31 | 12 | ||
32 | - # We are creating the controller with local-loopback on purpose to avoid | 13 | + def build( self, k=4, h=6 ): |
33 | - # having the switches connect immediately. Instead, we'll set controller | 14 | + spines = [] |
34 | - # explicitly for each switch after configuring it as we want. | 15 | + leaves = [] |
35 | - self.flare = RemoteController(cname, '127.0.0.1', 6633) | 16 | + hosts = [] |
36 | - self.net = Mininet(controller=self.flare, switch = OVSKernelSwitch, | ||
37 | - build=False) | ||
38 | - | ||
39 | - self.cip = cip | ||
40 | - self.spines = [] | ||
41 | - self.leaves = [] | ||
42 | - self.hosts = [] | ||
43 | - self.proto = proto | ||
44 | 17 | ||
45 | # Create the two spine switches | 18 | # Create the two spine switches |
46 | - self.spines.append(self.net.addSwitch('s1')) | 19 | + spines.append(self.addSwitch('s1')) |
47 | - self.spines.append(self.net.addSwitch('s2')) | 20 | + spines.append(self.addSwitch('s2')) |
48 | 21 | ||
49 | # Create two links between the spine switches | 22 | # Create two links between the spine switches |
50 | - self.net.addLink(self.spines[0], self.spines[1]) | 23 | + self.addLink(spines[0], spines[1]) |
51 | - self.net.addLink(self.spines[1], self.spines[0]) | 24 | + #TODO add second link between spines when multi-link topos are supported |
25 | + #self.addLink(spines[0], spines[1]) | ||
52 | 26 | ||
53 | # Now create the leaf switches, their hosts and connect them together | 27 | # Now create the leaf switches, their hosts and connect them together |
54 | i = 1 | 28 | i = 1 |
55 | c = 0 | 29 | c = 0 |
56 | while i <= k: | 30 | while i <= k: |
57 | - self.leaves.append(self.net.addSwitch('s1%d' % i)) | 31 | + leaves.append(self.addSwitch('s1%d' % i)) |
58 | - for spine in self.spines: | 32 | + for spine in spines: |
59 | - self.net.addLink(self.leaves[i-1], spine) | 33 | + self.addLink(leaves[i-1], spine) |
60 | 34 | ||
61 | j = 1 | 35 | j = 1 |
62 | while j <= h: | 36 | while j <= h: |
63 | - self.hosts.append(self.net.addHost('h%d%d' % (i, j))) | 37 | + hosts.append(self.addHost('h%d%d' % (i, j))) |
64 | - self.net.addLink(self.hosts[c], self.leaves[i-1]) | 38 | + self.addLink(hosts[c], leaves[i-1]) |
65 | j+=1 | 39 | j+=1 |
66 | c+=1 | 40 | c+=1 |
67 | 41 | ||
68 | i+=1 | 42 | i+=1 |
69 | 43 | ||
70 | - def run(self): | 44 | +topos = { 'tower': TowerTopo } |
71 | - """ Runs the created network topology and launches mininet cli""" | ||
72 | - self.run_silent() | ||
73 | - CLI(self.net) | ||
74 | - self.net.stop() | ||
75 | - | ||
76 | - def run_silent(self): | ||
77 | - """ Runs silently - for unit testing """ | ||
78 | - self.net.build() | ||
79 | - | ||
80 | - # Start the switches, configure them with desired protocols and only | ||
81 | - # then set the controller | ||
82 | - for sw in self.spines: | ||
83 | - sw.start([self.flare]) | ||
84 | - if self.proto: | ||
85 | - sw.cmd('ovs-vsctl set bridge %(sw)s protocols=%(proto)s' % \ | ||
86 | - { 'sw': sw.name, 'proto': self.proto}) | ||
87 | - sw.cmdPrint('ovs-vsctl set-controller %(sw)s tcp:%(ctl)s:6633' % \ | ||
88 | - {'sw': sw.name, 'ctl': self.cip}) | ||
89 | - | ||
90 | - for sw in self.leaves: | ||
91 | - sw.start([self.flare]) | ||
92 | - sw.cmdPrint('ovs-vsctl set-controller %(sw)s tcp:%(ctl)s:6633' % \ | ||
93 | - {'sw': sw.name, 'ctl': self.cip}) | ||
94 | 45 | ||
95 | - def pingAll(self): | 46 | +def run(): |
96 | - """ PingAll to create flows - for unit testing """ | 47 | + topo = TowerTopo() |
97 | - self.net.pingAll() | 48 | + net = Mininet( topo=topo, controller=RemoteController, autoSetMacs=True ) |
49 | + net.start() | ||
50 | + CLI( net ) | ||
51 | + net.stop() | ||
98 | 52 | ||
99 | - def stop(self): | 53 | +if __name__ == '__main__': |
100 | - "Stops the topology. You should call this after run_silent" | 54 | + setLogLevel( 'info' ) |
101 | - self.net.stop() | 55 | + run() | ... | ... |
-
Please register or login to post a comment