Statistics
| Branch: | Tag: | Revision:

mininet / examples / vlanhost.py @ fe8358ad

History | View | Annotate | Download (2.3 KB)

1
"""
2
vlanhost.py: Host subclass that uses a VLAN tag for the default interface.
3

4
Dependencies:
5
    This class depends on the "vlan" package
6
    $ sudo apt-get install vlan
7

8
Usage (example uses VLAN ID=1000):
9
    From the command line:
10
        sudo mn --custom vlanhost.py --host vlan,vlan=1000
11

12
    From a script (see exampleUsage function below):
13
        from functools import partial
14
        from vlanhost import VLANHost
15

16
        ....
17

18
        host = partial( VLANHost, vlan=1000 )
19
        net = Mininet( host=host, ... )
20

21
    Directly running this script:
22
        sudo python vlanhost.py 1000
23

24
"""
25

    
26
from mininet.node import Host
27

    
28
class VLANHost( Host ):
29

    
30
   def config( self, vlan=100, **params ):
31
        """Configure VLANHost according to (optional) parameters:
32
           vlan: VLAN ID for default interface"""
33

    
34
        r = super( Host, self ).config( **params )
35

    
36
        intf = self.defaultIntf()
37
        # remove IP from default, "physical" interface
38
        self.cmd( 'ifconfig %s inet 0' % intf )
39
        # create VLAN interface 
40
        self.cmd( 'vconfig add %s %d' % ( intf, vlan ) )
41
        # assign the host's IP to the VLAN interface
42
        self.cmd( 'ifconfig %s.%d inet %s' % ( intf, vlan, params['ip'] ) )
43
        # update the intf name and host's intf map
44
        newName = '%s.%d' % ( intf, vlan )
45
        # update the (Mininet) interface to refer to VLAN interface name
46
        intf.name = newName
47
        # add VLAN interface to host's name to intf map
48
        self.nameToIntf[ newName ] = intf
49

    
50
        return r
51

    
52
hosts = { 'vlan': VLANHost }
53

    
54

    
55
def exampleUsage( vlan ):
56
    """Simple example of how VLANHost can be used in a script"""
57
    # This is where the magic happens...
58
    host = partial( VLANHost, vlan=vlan )
59
    # vlan (type: int): VLAN ID to be used by all hosts
60

    
61
    # Start a basic network using our VLANHost
62
    topo = SingleSwitchTopo( k=2 )
63
    net = Mininet( host=host, topo=topo )
64
    net.start()
65
    CLI( net )
66
    net.stop()
67

    
68
if __name__ == '__main__':
69
    import sys
70
    from functools import partial
71

    
72
    from mininet.net import Mininet
73
    from mininet.cli import CLI
74
    from mininet.topo import SingleSwitchTopo
75

    
76
    try:
77
        vlan = int( sys.argv[ 1 ] )
78
    except Exception:
79
        print 'Usage: vlanhost.py <vlan id>\n'
80
        print 'Using VLAN ID=100...'
81
        vlan = 100
82

    
83
    exampleUsage( vlan )