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 | |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 51 | |
| 52 | s1 = self.addSwitch( 's1' ) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 53 | |
| 54 | self.addLink(s1, host1) |
| 55 | self.addLink(s1, host2) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 56 | self.addLink(s1, host3) |
| 57 | self.addLink(s1, host4) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | topos = { 'mytopo': ( lambda: MyTopo() ) } |
| 61 | |
| 62 | def setupNetwork(): |
| 63 | "Create network" |
| 64 | topo = MyTopo() |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 65 | network = Mininet(topo=topo, autoSetMacs=True, autoStaticArp=True, controller=None) |
GlennRC | 6844994 | 2015-10-16 16:03:12 -0700 | [diff] [blame] | 66 | network.start() |
| 67 | CLI( network ) |
| 68 | network.stop() |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | setLogLevel('info') |
| 72 | #setLogLevel('debug') |
| 73 | setupNetwork() |