-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick.py
64 lines (55 loc) · 2.14 KB
/
click.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Wrapper for Click software routers for the Mininet network virtualizer
"""
import fileinput, shutil, os
from mininet.node import Switch
from mininet.util import quietRun
from mininet.moduledeps import moduleDeps, pathCheck
class ClickKernelSwitch( Switch ):
"""Click kernel-space switch, based on OVSKernelSwitch."""
def __init__( self, name, configPath=None, dp=None, **kwargs ):
"""Init.
name: name for switch
dp: netlink id (0, 1, 2, ...)
defaultMAC: default MAC as unsigned int; random value if None"""
Switch.__init__( self, name, **kwargs )
self.dp = 'dp%i' % dp
self.intf = self.dp
# Create a copy of the config for renaming
if not configPath:
configPath = os.path.join(os.getcwd(), "test.click")
self.configPath = configPath + ".intf"
shutil.copy(configPath, self.configPath)
@staticmethod
def setup():
"Ensure any dependencies are loaded; if not, try to load them."
pathCheck( 'click-install', 'click-uninstall',
moduleName='Click Kernel Switch')
def intfRename(self, intf, intfName):
"""
Rename interfaces in the Click config file with those generated by
Mininet.
"""
for line in fileinput.input(self.configPath, inplace=True):
print line.replace(intfName, intf)
def linkAs(self, node2, intfName, port1=None, port2=None):
"""
Link the given node to the switch on the given interface in the Click
config file.
"""
intf1, intf2 = super(ClickKernelSwitch, self).linkTo(node2, port1, port2)
self.intfRename(intf1, intfName)
quietRun('ifconfig ' + intf1 + ' up')
return intf1, intf2
def start( self, controllers ):
"""
Install the Click kernel module with the proper configuration.
"""
self.cmd("click-install " + self.configPath)
quietRun('ifconfig lo up')
def stop( self ):
"""
Remove the Click kernel module and remove the generated config file.
"""
self.cmd("click-uninstall")
os.remove(self.configPath)