Statistics
| Branch: | Tag: | Revision:

mininet / examples / vlanhost.py @ 49654212

History | View | Annotate | Download (3.56 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
    "Host connected to VLAN interface"
34

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

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

    
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
51
        intf.name = newName
52
        # add VLAN interface to host's name to intf map
53
        self.nameToIntf[ newName ] = intf
54

    
55
        return r
56

    
57
hosts = { 'vlan': VLANHost }
58

    
59

    
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
65

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

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

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

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

    
93

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

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

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

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

    
111
    setLogLevel( 'info' )
112

    
113
    if not quietRun( 'which vconfig' ):
114
        error( "Cannot find command 'vconfig'\nThe package",
115
               "'vlan' is required in Ubuntu or Debian,",
116
               "or 'vconfig' in Fedora\n" )
117
        exit()
118

    
119
    if len( sys.argv ) >= 2:
120
        exampleAllHosts( vlan=int( sys.argv[ 1 ] ) )
121
    else:
122
        exampleCustomTags()