Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / mininet / examples / vlanhost.py
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 # pylint: disable=arguments-differ
74
75 class VLANStarTopo( Topo ):
76     """Example topology that uses host in multiple VLANs
77
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
81        the switch."""
82
83     def build( self, k=2, n=2, vlanBase=100 ):
84         s1 = self.addSwitch( 's1' )
85         for i in range( k ):
86             vlan = vlanBase + i
87             for j in range(n):
88                 name = 'h%d-%d' % ( j+1, vlan )
89                 h = self.addHost( name, cls=VLANHost, vlan=vlan )
90                 self.addLink( h, s1 )
91         for j in range( n ):
92             h = self.addHost( 'h%d' % (j+1) )
93             self.addLink( h, s1 )
94
95
96 def exampleCustomTags():
97     """Simple example that exercises VLANStarTopo"""
98
99     net = Mininet( topo=VLANStarTopo() )
100     net.start()
101     CLI( net )
102     net.stop()
103
104 if __name__ == '__main__':
105     import sys
106     from functools import partial
107
108     from mininet.net import Mininet
109     from mininet.cli import CLI
110     from mininet.topo import SingleSwitchTopo
111     from mininet.log import setLogLevel
112
113     setLogLevel( 'info' )
114
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" )
119         exit()
120
121     if len( sys.argv ) >= 2:
122         exampleAllHosts( vlan=int( sys.argv[ 1 ] ) )
123     else:
124         exampleCustomTags()