Statistics
| Branch: | Tag: | Revision:

mininet / mininet / nodelib.py @ 3e1100b7

History | View | Annotate | Download (4 KB)

1 3878c000 Bob Lantz
"""
2
Node Library for Mininet
3

4 4015e066 Brian O'Connor
This contains additional Node types which you may find to be useful.
5 3878c000 Bob Lantz
"""
6
7 4015e066 Brian O'Connor
from mininet.node import Node, Switch
8 3878c000 Bob Lantz
from mininet.log import setLogLevel, info
9
10
class LinuxBridge( Switch ):
11
    "Linux Bridge (with optional spanning tree)"
12
13
    nextPrio = 100  # next bridge priority for spanning tree
14
15
    def __init__( self, name, stp=False, prio=None, **kwargs ):
16
        """stp: use spanning tree protocol? (default False)
17
           prio: optional explicit bridge priority for STP"""
18
        self.stp = stp
19
        if prio:
20
            self.prio = prio
21
        else:
22
            self.prio = LinuxBridge.nextPrio
23
            LinuxBridge.nextPrio += 1
24
        Switch.__init__( self, name, **kwargs )
25
26 ece509d5 Bob Lantz
    def connected( self ):
27
        "Are we forwarding yet?"
28
        if self.stp:
29
            return 'forwarding' in self.cmd( 'brctl showstp', self )
30
        else:
31
            return True
32
    
33 3878c000 Bob Lantz
    def start( self, controllers ):
34
        self.cmd( 'ifconfig', self, 'down' )
35
        self.cmd( 'brctl delbr', self )
36
        self.cmd( 'brctl addbr', self )
37
        if self.stp:
38
            self.cmd( 'brctl setbridgeprio', self.prio )
39
            self.cmd( 'brctl stp', self, 'on' )
40
        for i in self.intfList():
41
            if self.name in i.name:
42
                self.cmd( 'brctl addif', self, i )
43
        self.cmd( 'ifconfig', self, 'up' )
44
45
    def stop( self ):
46
        self.cmd( 'ifconfig', self, 'down' )
47
        self.cmd( 'brctl delbr', self )
48
49 4015e066 Brian O'Connor
class NAT( Node ):
50
    """NAT: Provides connectivity to external network"""
51
52
    def __init__( self, name, inetIntf='eth0', subnet='10.0/8', localIntf=None, **params):
53
        super( NAT, self ).__init__( name, **params )
54
55
        """Start NAT/forwarding between Mininet and external network
56
        inetIntf: interface for internet access
57
        subnet: Mininet subnet (default 10.0/8)="""
58
        self.inetIntf = inetIntf
59
        self.subnet = subnet
60
        self.localIntf = localIntf
61
62
    def config( self, **params ):
63
        super( NAT, self).config( **params )
64
        """Configure the NAT and iptables"""
65
66
        if not self.localIntf:
67
            self.localIntf =  self.defaultIntf()
68
69
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
70
71
        # Flush any currently active rules
72
        # TODO: is this safe?
73
        self.cmd( 'iptables -F' )
74
        self.cmd( 'iptables -t nat -F' )
75
76
        # Create default entries for unmatched traffic
77
        self.cmd( 'iptables -P INPUT ACCEPT' )
78
        self.cmd( 'iptables -P OUTPUT ACCEPT' )
79
        self.cmd( 'iptables -P FORWARD DROP' )
80
81
        # Configure NAT
82
        self.cmd( 'iptables -I FORWARD -i', self.localIntf, '-d', self.subnet, '-j DROP' )
83
        self.cmd( 'iptables -A FORWARD -i', self.localIntf, '-s', self.subnet, '-j ACCEPT' )
84
        self.cmd( 'iptables -A FORWARD -i', self.inetIntf, '-d', self.subnet, '-j ACCEPT' )
85
        self.cmd( 'iptables -t nat -A POSTROUTING -o ', self.inetIntf, '-j MASQUERADE' )
86
87
        # Instruct the kernel to perform forwarding
88
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )
89
90
        # Prevent network-manager from messing with our interface
91
        # by specifying manual configuration in /etc/network/interfaces
92
        intf = self.localIntf
93
        cfile = '/etc/network/interfaces'
94
        line = '\niface %s inet manual\n' % intf
95
        config = open( cfile ).read()
96
        if ( line ) not in config:
97
            info( '*** Adding "' + line.strip() + '" to ' + cfile )
98
            with open( cfile, 'a' ) as f:
99
                f.write( line )
100
        # Probably need to restart network-manager to be safe -
101
        # hopefully this won't disconnect you
102
        self.cmd( 'service network-manager restart' )
103
104
    def terminate( self ):
105
        """Stop NAT/forwarding between Mininet and external network"""
106
        # Flush any currently active rules
107
        # TODO: is this safe?
108
        self.cmd( 'iptables -F' )
109
        self.cmd( 'iptables -t nat -F' )
110
111
        # Instruct the kernel to stop forwarding
112
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
113
114
        super( NAT, self ).terminate()