Statistics
| Branch: | Tag: | Revision:

mininet / examples / vlanhost.py @ 18aab5b7

History | View | Annotate | Download (3.58 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
from mininet.util import quietRun
30
from mininet.log import error
31

    
32
class VLANHost( Host ):
33

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

    
38
        r = super( VLANHost, self ).config( **params )
39

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

    
54
        return r
55

    
56
hosts = { 'vlan': VLANHost }
57

    
58

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

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

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

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

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

    
92

    
93
def exampleCustomTags( vlan ):
94
    """Simple example that exercises VLANStarTopo"""
95

    
96
    net = Mininet( topo=VLANStarTopo() )
97
    net.start()
98
    CLI( net )
99
    net.stop()
100

    
101
if __name__ == '__main__':
102
    import sys
103
    from functools import partial
104

    
105
    from mininet.net import Mininet
106
    from mininet.cli import CLI
107
    from mininet.topo import SingleSwitchTopo
108
    from mininet.log import setLogLevel
109

    
110
    setLogLevel( 'info' )
111

    
112
    if not quietRun( 'which vconfig' ):
113
        error( "Cannot find command 'vconfig'\nThe packge",
114
               "'vlan' is required in Ubuntu or Debian,",
115
               "or 'vconfig' in Fedora\n" )
116
        exit()
117
    try:
118
        vlan = int( sys.argv[ 1 ] )
119
    except Exception:
120
        vlan = None
121

    
122
    if vlan:
123
        exampleAllHosts( vlan )
124
    else:
125
        exampleCustomTags( vlan )