Statistics
| Branch: | Tag: | Revision:

mininet / examples / hwintf.py @ 220376b6

History | View | Annotate | Download (1.5 KB)

1
#!/usr/bin/python
2

    
3
"""
4
This example shows how to add an interface (for example a real
5
hardware interface) to a network after the network is created.
6
"""
7

    
8
import re, sys
9

    
10
from mininet.cli import CLI
11
from mininet.log import setLogLevel, info, error
12
from mininet.net import Mininet
13
from mininet.link import Intf
14
from mininet.topolib import TreeTopo
15
from mininet.util import quietRun
16

    
17
def checkIntf( intf ):
18
    "Make sure intf exists and is not configured."
19
    if ( ' %s:' % intf ) not in quietRun( 'ip link show' ):
20
        error( 'Error:', intf, 'does not exist!\n' )
21
        exit( 1 )
22
    ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) )
23
    if ips:
24
        error( 'Error:', intf, 'has an IP address,'
25
               'and is probably in use!\n' )
26
        exit( 1 )
27

    
28
if __name__ == '__main__':
29
    setLogLevel( 'info' )
30

    
31
    # try to get hw intf from the command line; by default, use eth1
32
    intfName = sys.argv[ 1 ] if len( sys.argv ) > 1 else 'eth1'
33
    info( '*** Connecting to hw intf: %s' % intfName )
34

    
35
    info( '*** Checking', intfName, '\n' )
36
    checkIntf( intfName )
37

    
38
    info( '*** Creating network\n' )
39
    net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) )
40

    
41
    switch = net.switches[ 0 ]
42
    info( '*** Adding hardware interface', intfName, 'to switch',
43
          switch.name, '\n' )
44
    _intf = Intf( intfName, node=switch )
45

    
46
    info( '*** Note: you may need to reconfigure the interfaces for '
47
          'the Mininet hosts:\n', net.hosts, '\n' )
48

    
49
    net.start()
50
    CLI( net )
51
    net.stop()