GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Custom topology for Mininet |
| 5 | """ |
| 6 | from mininet.topo import Topo |
| 7 | from mininet.net import Mininet |
| 8 | from mininet.node import Host, RemoteController |
| 9 | from mininet.node import Node |
| 10 | from mininet.node import CPULimitedHost |
| 11 | from mininet.link import TCLink |
| 12 | from mininet.cli import CLI |
| 13 | from mininet.log import setLogLevel |
| 14 | from mininet.util import dumpNodeConnections |
| 15 | from mininet.node import ( UserSwitch, OVSSwitch, IVSSwitch ) |
| 16 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 17 | class VLANHost( Host ): |
| 18 | def config( self, vlan=100, **params ): |
| 19 | r = super( Host, self ).config( **params ) |
| 20 | intf = self.defaultIntf() |
| 21 | self.cmd( 'ifconfig %s inet 0' % intf ) |
| 22 | self.cmd( 'vconfig add %s %d' % ( intf, vlan ) ) |
| 23 | self.cmd( 'ifconfig %s.%d inet %s' % ( intf, vlan, params['ip'] ) ) |
| 24 | newName = '%s.%d' % ( intf, vlan ) |
| 25 | intf.name = newName |
| 26 | self.nameToIntf[ newName ] = intf |
| 27 | return r |
| 28 | |
| 29 | class IPv6Host( Host ): |
| 30 | def config( self, v6Addr='1000:1/64', **params ): |
| 31 | r = super( Host, self ).config( **params ) |
| 32 | intf = self.defaultIntf() |
| 33 | self.cmd( 'ifconfig %s inet 0' % intf ) |
| 34 | self.cmd( 'ip -6 addr add %s dev %s' % ( v6Addr, intf ) ) |
| 35 | return r |
| 36 | |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 37 | class MyTopo( Topo ): |
| 38 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 39 | def __init__( self, **opts ): |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 40 | # Initialize topology |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 41 | Topo.__init__( self, **opts) |
| 42 | |
| 43 | # IPv4 hosts |
| 44 | host1=self.addHost( 'h1', ip='10.0.0.1/24' ) |
| 45 | host2=self.addHost( 'h2', ip='10.0.0.2/24' ) |
| 46 | |
| 47 | # VLAN hosts |
| 48 | host3=self.addHost( 'h3', ip='10.0.0.3/24', cls=VLANHost, vlan=10 ) |
| 49 | host4=self.addHost( 'h4', ip='10.0.0.4/24', cls=VLANHost, vlan=10 ) |
| 50 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 51 | #IPv6 hosts |
| 52 | host5=self.addHost( 'h5', ip='10.0.0.5/24', cls=IPv6Host, v6Addr='1000::5/64') |
| 53 | host6=self.addHost( 'h6', ip='10.0.0.6/24', cls=IPv6Host, v6Addr='1000::6/64') |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 54 | |
| 55 | s1 = self.addSwitch( 's1' ) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 56 | |
| 57 | self.addLink(s1, host1) |
| 58 | self.addLink(s1, host2) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 59 | self.addLink(s1, host3) |
| 60 | self.addLink(s1, host4) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 61 | self.addLink(s1, host5) |
| 62 | self.addLink(s1, host6) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | topos = { 'mytopo': ( lambda: MyTopo() ) } |
| 66 | |
| 67 | def setupNetwork(): |
| 68 | "Create network" |
| 69 | topo = MyTopo() |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 70 | network = Mininet(topo=topo, autoSetMacs=True, autoStaticArp=True, controller=None) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 71 | network.start() |
| 72 | CLI( network ) |
| 73 | network.stop() |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | setLogLevel('info') |
| 77 | #setLogLevel('debug') |
| 78 | setupNetwork() |