tom | bd8e9c8 | 2014-10-07 11:46:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | from mininet.cli import CLI |
| 3 | from mininet.net import Mininet |
| 4 | from mininet.node import RemoteController, OVSKernelSwitch |
| 5 | |
| 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 | |
| 23 | def __init__(self, cname='flare', cip='15.255.126.183', k=4, h=6, |
| 24 | proto=None): |
| 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 | |
| 32 | # We are creating the controller with local-loopback on purpose to avoid |
| 33 | # having the switches connect immediately. Instead, we'll set controller |
| 34 | # explicitly for each switch after configuring it as we want. |
| 35 | self.flare = RemoteController(cname, '127.0.0.1', 6633) |
| 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 | |
| 45 | # Create the two spine switches |
| 46 | self.spines.append(self.net.addSwitch('s1')) |
| 47 | self.spines.append(self.net.addSwitch('s2')) |
| 48 | |
| 49 | # Create two links between the spine switches |
| 50 | self.net.addLink(self.spines[0], self.spines[1]) |
| 51 | self.net.addLink(self.spines[1], self.spines[0]) |
| 52 | |
| 53 | # Now create the leaf switches, their hosts and connect them together |
| 54 | i = 1 |
| 55 | c = 0 |
| 56 | while i <= k: |
| 57 | self.leaves.append(self.net.addSwitch('s1%d' % i)) |
| 58 | for spine in self.spines: |
| 59 | self.net.addLink(self.leaves[i-1], spine) |
| 60 | |
| 61 | j = 1 |
| 62 | while j <= h: |
| 63 | self.hosts.append(self.net.addHost('h%d%d' % (i, j))) |
| 64 | self.net.addLink(self.hosts[c], self.leaves[i-1]) |
| 65 | j+=1 |
| 66 | c+=1 |
| 67 | |
| 68 | i+=1 |
| 69 | |
| 70 | def run(self): |
| 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 | |
| 95 | def pingAll(self): |
| 96 | """ PingAll to create flows - for unit testing """ |
| 97 | self.net.pingAll() |
| 98 | |
| 99 | def stop(self): |
| 100 | "Stops the topology. You should call this after run_silent" |
| 101 | self.net.stop() |