3 vlanhost.py: Host subclass that uses a VLAN tag for the default interface.
6 This class depends on the "vlan" package
7 $ sudo apt-get install vlan
9 Usage (example uses VLAN ID=1000):
10 From the command line:
11 sudo mn --custom vlanhost.py --host vlan,vlan=1000
13 From a script (see exampleUsage function below):
14 from functools import partial
15 from vlanhost import VLANHost
19 host = partial( VLANHost, vlan=1000 )
20 net = Mininet( host=host, ... )
22 Directly running this script:
23 sudo python vlanhost.py 1000
27 from mininet.node import Host
28 from mininet.topo import Topo
29 from mininet.util import quietRun
30 from mininet.log import error
32 class VLANHost( Host ):
33 "Host connected to VLAN interface"
35 def config( self, vlan=100, **params ):
36 """Configure VLANHost according to (optional) parameters:
37 vlan: VLAN ID for default interface"""
39 r = super( VLANHost, self ).config( **params )
41 intf = self.defaultIntf()
42 # remove IP from default, "physical" interface
43 self.cmd( 'ifconfig %s inet 0' % intf )
44 # create VLAN interface
45 self.cmd( 'vconfig add %s %d' % ( intf, vlan ) )
46 # assign the host's IP to the VLAN interface
47 self.cmd( 'ifconfig %s.%d inet %s' % ( intf, vlan, params['ip'] ) )
48 # update the intf name and host's intf map
49 newName = '%s.%d' % ( intf, vlan )
50 # update the (Mininet) interface to refer to VLAN interface name
52 # add VLAN interface to host's name to intf map
53 self.nameToIntf[ newName ] = intf
57 hosts = { 'vlan': VLANHost }
60 def exampleAllHosts( vlan ):
61 """Simple example of how VLANHost can be used in a script"""
62 # This is where the magic happens...
63 host = partial( VLANHost, vlan=vlan )
64 # vlan (type: int): VLAN ID to be used by all hosts
66 # Start a basic network using our VLANHost
67 topo = SingleSwitchTopo( k=2 )
68 net = Mininet( host=host, topo=topo )
73 # pylint: disable=arguments-differ
75 class VLANStarTopo( Topo ):
76 """Example topology that uses host in multiple VLANs
78 The topology has a single switch. There are k VLANs with
79 n hosts in each, all connected to the single switch. There
80 are also n hosts that are not in any VLAN, also connected to
83 def build( self, k=2, n=2, vlanBase=100 ):
84 s1 = self.addSwitch( 's1' )
88 name = 'h%d-%d' % ( j+1, vlan )
89 h = self.addHost( name, cls=VLANHost, vlan=vlan )
92 h = self.addHost( 'h%d' % (j+1) )
96 def exampleCustomTags():
97 """Simple example that exercises VLANStarTopo"""
99 net = Mininet( topo=VLANStarTopo() )
104 if __name__ == '__main__':
106 from functools import partial
108 from mininet.net import Mininet
109 from mininet.cli import CLI
110 from mininet.topo import SingleSwitchTopo
111 from mininet.log import setLogLevel
113 setLogLevel( 'info' )
115 if not quietRun( 'which vconfig' ):
116 error( "Cannot find command 'vconfig'\nThe package",
117 "'vlan' is required in Ubuntu or Debian,",
118 "or 'vconfig' in Fedora\n" )
121 if len( sys.argv ) >= 2:
122 exampleAllHosts( vlan=int( sys.argv[ 1 ] ) )