Statistics
| Branch: | Tag: | Revision:

mininet / examples / vlanhost.py @ 6b8d3538

History | View | Annotate | Download (3.3 KB)

1
#!/usr/bin/env python
2
"""
3
vlanhost.py: Host subclass that uses a VLAN tag for the default interface.
4

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

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

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

17
        ....
18

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

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

25
"""
26

    
27
from mininet.node import Host
28
from mininet.topo import Topo
29

    
30
class VLANHost( Host ):
31

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

    
36
        r = super( Host, self ).config( **params )
37

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

    
52
        return r
53

    
54
hosts = { 'vlan': VLANHost }
55

    
56

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

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

    
70
class VLANStarTopo( Topo ):
71
    """Example topology that uses host in multiple VLANs
72

73
       The topology has a single switch. There are k VLANs with
74
       n hosts in each, all connected to the single switch. There
75
       are also n hosts that are not in any VLAN, also connected to
76
       the switch."""
77

    
78
    def build( self, k=2, n=2, vlanBase=100 ):
79
        s1 = self.addSwitch( 's1' )
80
        for i in range( k ):
81
            vlan = vlanBase + i
82
            for j in range(n):
83
                name = 'h%d-%d' % ( j+1, vlan )
84
                h = self.addHost( name, cls=VLANHost, vlan=vlan )
85
                self.addLink( h, s1 )
86
        for j in range( n ):
87
            h = self.addHost( 'h%d' % (j+1) )
88
            self.addLink( h, s1 )
89

    
90

    
91
def exampleCustomTags( vlan ):
92
    """Simple example that exercises VLANStarTopo"""
93

    
94
    net = Mininet( topo=VLANStarTopo() )
95
    net.start()
96
    CLI( net )
97
    net.stop()
98

    
99
if __name__ == '__main__':
100
    import sys
101
    from functools import partial
102

    
103
    from mininet.net import Mininet
104
    from mininet.cli import CLI
105
    from mininet.topo import SingleSwitchTopo
106
    from mininet.log import setLogLevel
107

    
108
    setLogLevel( 'info' )
109

    
110
    try:
111
        vlan = int( sys.argv[ 1 ] )
112
    except Exception:
113
        vlan = None
114

    
115
    if vlan:
116
        exampleAllHosts( vlan )
117
    else:
118
        exampleCustomTags( vlan )