forked from geofragkos/NetworkSoft_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overrides.py
98 lines (86 loc) · 2.83 KB
/
overrides.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Overriding functionality that is provided outside the
scope of this project and where subclassing is not possible
Authors:
Patrick Ziegler, Dresden University of Technology
"""
from importlib import __import__
from mininet.log import debug
from mininet.util import errRun, quietRun
import sys
def override(module, name):
"""
A decorator for replacing a given function of
a specified module with the decorated function
Caution:
Be aware that functions named 'name' in any given module
will be replaced!
"""
__import__(module)
def _wrapper(fn):
for m in sys.modules:
if name in dir(sys.modules[m]):
debug(f"Overriding function {name} in module {m}")
setattr(sys.modules[m], name, fn)
return fn
return _wrapper
@override("mininet.util", "makeIntfPair")
def makeIntfPairFixed(
intf1,
intf2,
addr1=None,
addr2=None,
node1=None,
node2=None,
deleteIntfs=True,
runCmd=None,
):
"""Make a veth pair connnecting new interfaces intf1 and intf2
intf1: name for interface 1
intf2: name for interface 2
addr1: MAC address for interface 1 (optional)
addr2: MAC address for interface 2 (optional)
node1: home node for interface 1 (optional)
node2: home node for interface 2 (optional)
deleteIntfs: delete intfs before creating them
runCmd: function to run shell commands (quietRun)
raises Exception on failure"""
if not runCmd:
runCmd = quietRun if not node1 else node1.cmd
runCmd2 = quietRun if not node2 else node2.cmd
if deleteIntfs:
# Delete any old interfaces with the same names
runCmd("ip link del " + intf1)
runCmd2("ip link del " + intf2)
# Create new pair
netns1 = node1.pid
netns2 = 1 if not node2 else node2.pid
if addr1 is None and addr2 is None:
cmd = "ip link add name %s netns %s " "type veth peer name %s netns %s" % (
intf1,
netns1,
intf2,
netns2,
)
else:
cmd = (
"ip link add name %s address %s netns %s "
"type veth peer name %s address %s netns %s"
% (intf1, addr1, netns1, intf2, addr2, netns2)
)
_, cmdOutput, _ = errRun(cmd)
# iproute2 changes behaviour in release 5.1
# the following workaround should be removed when
# issue in iproute2 was fixed
# [1] https://github.com/mininet/mininet/issues/884
# [2] https://lwn.net/Articles/783494/
if "No such device" in cmdOutput:
debug(
"Ignored error creating interface pair (%s,%s): %s "
% (intf1, intf2, cmdOutput)
)
cmdOutput = ""
if cmdOutput:
raise Exception(
"Error creating interface pair (%s,%s): %s " % (intf1, intf2, cmdOutput)
)