Jonathan Hart | 65048cd | 2014-12-01 14:12:57 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from mininet.topo import Topo |
| 4 | from mininet.net import Mininet |
| 5 | from mininet.cli import CLI |
| 6 | from mininet.log import setLogLevel, info, debug |
| 7 | from mininet.node import Host, RemoteController |
| 8 | |
| 9 | QUAGGA_DIR = '/usr/lib/quagga' |
| 10 | # Must exist and be owned by quagga user (quagga:quagga by default on Ubuntu) |
| 11 | QUAGGA_RUN_DIR = '/var/run/quagga' |
| 12 | CONFIG_DIR = 'configs' |
| 13 | |
| 14 | class SdnIpHost(Host): |
| 15 | def __init__(self, name, ip, route, *args, **kwargs): |
| 16 | Host.__init__(self, name, ip=ip, *args, **kwargs) |
| 17 | |
| 18 | self.route = route |
| 19 | |
| 20 | def config(self, **kwargs): |
| 21 | Host.config(self, **kwargs) |
| 22 | |
| 23 | debug("configuring route %s" % self.route) |
| 24 | |
| 25 | self.cmd('ip route add default via %s' % self.route) |
| 26 | |
| 27 | class Router(Host): |
| 28 | def __init__(self, name, quaggaConfFile, zebraConfFile, intfDict, *args, **kwargs): |
| 29 | Host.__init__(self, name, *args, **kwargs) |
| 30 | |
| 31 | self.quaggaConfFile = quaggaConfFile |
| 32 | self.zebraConfFile = zebraConfFile |
| 33 | self.intfDict = intfDict |
| 34 | |
| 35 | def config(self, **kwargs): |
| 36 | Host.config(self, **kwargs) |
| 37 | self.cmd('sysctl net.ipv4.ip_forward=1') |
| 38 | |
| 39 | for intf, attrs in self.intfDict.items(): |
| 40 | self.cmd('ip addr flush dev %s' % intf) |
| 41 | if 'mac' in attrs: |
| 42 | self.cmd('ip link set %s down' % intf) |
| 43 | self.cmd('ip link set %s address %s' % (intf, attrs['mac'])) |
| 44 | self.cmd('ip link set %s up ' % intf) |
| 45 | for addr in attrs['ipAddrs']: |
| 46 | self.cmd('ip addr add %s dev %s' % (addr, intf)) |
| 47 | |
| 48 | self.cmd('/usr/lib/quagga/zebra -d -f %s -z %s/zebra%s.api -i %s/zebra%s.pid' % (self.zebraConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name)) |
| 49 | self.cmd('/usr/lib/quagga/bgpd -d -f %s -z %s/zebra%s.api -i %s/bgpd%s.pid' % (self.quaggaConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name)) |
| 50 | |
| 51 | |
| 52 | def terminate(self): |
| 53 | self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (self.name, self.name)) |
| 54 | |
| 55 | Host.terminate(self) |
| 56 | |
| 57 | |
| 58 | class SdnIpTopo( Topo ): |
| 59 | "SDN-IP tutorial topology" |
| 60 | |
| 61 | def build( self ): |
| 62 | s1 = self.addSwitch('s1', dpid='00000000000000a1') |
| 63 | s2 = self.addSwitch('s2', dpid='00000000000000a2') |
| 64 | s3 = self.addSwitch('s3', dpid='00000000000000a3') |
| 65 | s4 = self.addSwitch('s4', dpid='00000000000000a4') |
| 66 | s5 = self.addSwitch('s5', dpid='00000000000000a5') |
| 67 | s6 = self.addSwitch('s6', dpid='00000000000000a6') |
| 68 | |
| 69 | zebraConf = '%s/zebra.conf' % CONFIG_DIR |
| 70 | |
| 71 | # Switches we want to attach our routers to, in the correct order |
| 72 | attachmentSwitches = [s1, s2, s5, s6] |
| 73 | |
| 74 | for i in range(1, 4+1): |
| 75 | name = 'r%s' % i |
| 76 | |
| 77 | eth0 = { 'mac' : '00:00:00:00:0%s:01' % i, |
| 78 | 'ipAddrs' : ['10.0.%s.1/24' % i] } |
| 79 | eth1 = { 'ipAddrs' : ['192.168.%s.254/24' % i] } |
| 80 | intfs = { '%s-eth0' % name : eth0, |
| 81 | '%s-eth1' % name : eth1 } |
| 82 | |
| 83 | quaggaConf = '%s/quagga%s.conf' % (CONFIG_DIR, i) |
| 84 | |
| 85 | router = self.addHost(name, cls=Router, quaggaConfFile=quaggaConf, |
| 86 | zebraConfFile=zebraConf, intfDict=intfs) |
| 87 | |
| 88 | host = self.addHost('h%s' % i, cls=SdnIpHost, |
| 89 | ip='192.168.%s.1/24' % i, |
| 90 | route='192.168.%s.254' % i) |
| 91 | |
| 92 | self.addLink(router, attachmentSwitches[i-1]) |
| 93 | self.addLink(router, host) |
| 94 | |
| 95 | # Set up the internal BGP speaker |
| 96 | bgpEth0 = { 'mac':'00:00:00:00:00:01', |
| 97 | 'ipAddrs' : ['10.0.1.101/24', |
| 98 | '10.0.2.101/24', |
| 99 | '10.0.3.101/24', |
| 100 | '10.0.4.101/24',] } |
| 101 | bgpEth1 = { 'ipAddrs' : ['10.10.10.1/24'] } |
| 102 | bgpIntfs = { 'bgp-eth0' : bgpEth0, |
| 103 | 'bgp-eth1' : bgpEth1 } |
| 104 | |
| 105 | bgp = self.addHost( "bgp", cls=Router, |
| 106 | quaggaConfFile = '%s/quagga-sdn.conf' % CONFIG_DIR, |
| 107 | zebraConfFile = zebraConf, |
| 108 | intfDict=bgpIntfs ) |
| 109 | |
| 110 | self.addLink( bgp, s3 ) |
| 111 | |
| 112 | # Connect BGP speaker to the root namespace so it can peer with ONOS |
| 113 | root = self.addHost( 'root', inNamespace=False, ip='10.10.10.2/24' ) |
| 114 | self.addLink( root, bgp ) |
| 115 | |
| 116 | |
| 117 | # Wire up the switches in the topology |
| 118 | self.addLink( s1, s2 ) |
| 119 | self.addLink( s1, s3 ) |
| 120 | self.addLink( s2, s4 ) |
| 121 | self.addLink( s3, s4 ) |
| 122 | self.addLink( s3, s5 ) |
| 123 | self.addLink( s4, s6 ) |
| 124 | self.addLink( s5, s6 ) |
| 125 | |
| 126 | topos = { 'sdnip' : SdnIpTopo } |
| 127 | |
| 128 | if __name__ == '__main__': |
| 129 | setLogLevel('debug') |
| 130 | topo = SDNTopo() |
| 131 | |
| 132 | net = Mininet(topo=topo, controller=RemoteController) |
| 133 | |
| 134 | net.start() |
| 135 | |
| 136 | CLI(net) |
| 137 | |
| 138 | net.stop() |
| 139 | |
| 140 | info("done\n") |