Kunihiro Ishiguro | f5df284 | 2015-01-15 12:59:36 -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, OVSSwitch |
| 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 | self.cmd('sysctl net.ipv6.conf.all.forwarding=1') |
| 39 | |
| 40 | for intf, attrs in self.intfDict.items(): |
| 41 | self.cmd('ip addr flush dev %s' % intf) |
| 42 | if 'mac' in attrs: |
| 43 | self.cmd('ip link set %s down' % intf) |
| 44 | self.cmd('ip link set %s address %s' % (intf, attrs['mac'])) |
| 45 | self.cmd('ip link set %s up ' % intf) |
| 46 | for addr in attrs['ipAddrs']: |
| 47 | self.cmd('ip addr add %s dev %s' % (addr, intf)) |
| 48 | |
| 49 | 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)) |
| 50 | 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)) |
| 51 | |
| 52 | |
| 53 | def terminate(self): |
| 54 | self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (self.name, self.name)) |
| 55 | |
| 56 | Host.terminate(self) |
| 57 | |
| 58 | class SdnSwitch(OVSSwitch): |
| 59 | def __init__(self, name, dpid, *args, **kwargs): |
| 60 | OVSSwitch.__init__(self, name, dpid=dpid, *args, **kwargs) |
| 61 | |
| 62 | def start(self, controllers): |
| 63 | OVSSwitch.start(self, controllers) |
| 64 | self.cmd("ovs-vsctl set Bridge %s protocols=OpenFlow13" % self.name) |
| 65 | |
| 66 | |
| 67 | class SdnIpTopo( Topo ): |
| 68 | "SDN-IP tutorial topology" |
| 69 | |
| 70 | def build( self ): |
| 71 | s1 = self.addSwitch('s1', cls=SdnSwitch, dpid='00000000000000a1') |
| 72 | s2 = self.addSwitch('s2', cls=SdnSwitch, dpid='00000000000000a2') |
| 73 | s3 = self.addSwitch('s3', cls=SdnSwitch, dpid='00000000000000a3') |
| 74 | s4 = self.addSwitch('s4', cls=SdnSwitch, dpid='00000000000000a4') |
| 75 | s5 = self.addSwitch('s5', cls=SdnSwitch, dpid='00000000000000a5') |
| 76 | s6 = self.addSwitch('s6', cls=SdnSwitch, dpid='00000000000000a6') |
| 77 | |
| 78 | zebraConf = '%s/zebra.conf' % CONFIG_DIR |
| 79 | |
| 80 | # Switches we want to attach our routers to, in the correct order |
| 81 | attachmentSwitches = [s1, s2, s5, s6] |
| 82 | |
| 83 | for i in range(1, 4+1): |
| 84 | name = 'r%s' % i |
| 85 | |
| 86 | eth0 = { 'mac' : '00:00:00:00:0%s:01' % i, |
| 87 | 'ipAddrs' : ['10.0.%s.1/24' % i] } |
| 88 | eth1 = { 'ipAddrs' : ['192.168.%s.254/24' % i] } |
| 89 | intfs = { '%s-eth0' % name : eth0, |
| 90 | '%s-eth1' % name : eth1 } |
| 91 | |
| 92 | quaggaConf = '%s/quagga%s.conf' % (CONFIG_DIR, i) |
| 93 | |
| 94 | router = self.addHost(name, cls=Router, quaggaConfFile=quaggaConf, |
| 95 | zebraConfFile=zebraConf, intfDict=intfs) |
| 96 | |
| 97 | host = self.addHost('h%s' % i, cls=SdnIpHost, |
| 98 | ip='192.168.%s.1/24' % i, |
| 99 | route='192.168.%s.254' % i) |
| 100 | |
| 101 | self.addLink(router, attachmentSwitches[i-1]) |
| 102 | self.addLink(router, host) |
| 103 | |
| 104 | # Set up the internal BGP speaker |
| 105 | bgpEth0 = { 'mac':'00:00:00:00:00:01', |
| 106 | 'ipAddrs' : ['10.0.1.101/24', |
| 107 | '10.0.2.101/24', |
| 108 | '10.0.3.101/24', |
| 109 | '10.0.4.101/24',] } |
| 110 | bgpEth1 = { 'ipAddrs' : ['10.10.10.1/24'] } |
| 111 | bgpIntfs = { 'bgp-eth0' : bgpEth0, |
| 112 | 'bgp-eth1' : bgpEth1 } |
| 113 | |
| 114 | bgp = self.addHost( "bgp", cls=Router, |
| 115 | quaggaConfFile = '%s/quagga-sdn.conf' % CONFIG_DIR, |
| 116 | zebraConfFile = zebraConf, |
| 117 | intfDict=bgpIntfs ) |
| 118 | |
| 119 | self.addLink( bgp, s3 ) |
| 120 | |
| 121 | # Connect BGP speaker to the root namespace so it can peer with ONOS |
| 122 | root = self.addHost( 'root', inNamespace=False, ip='10.10.10.2/24' ) |
| 123 | self.addLink( root, bgp ) |
| 124 | |
| 125 | |
| 126 | # Wire up the switches in the topology |
| 127 | self.addLink( s1, s2 ) |
| 128 | self.addLink( s1, s3 ) |
| 129 | self.addLink( s2, s4 ) |
| 130 | self.addLink( s3, s4 ) |
| 131 | self.addLink( s3, s5 ) |
| 132 | self.addLink( s4, s6 ) |
| 133 | self.addLink( s5, s6 ) |
| 134 | |
| 135 | topos = { 'sdnip' : SdnIpTopo } |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | setLogLevel('debug') |
| 139 | topo = SdnIpTopo() |
| 140 | |
| 141 | net = Mininet(topo=topo, controller=RemoteController) |
| 142 | |
| 143 | net.start() |
| 144 | |
| 145 | CLI(net) |
| 146 | |
| 147 | net.stop() |
| 148 | |
| 149 | info("done\n") |