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