Skip to content

Commit 3c2bdfe

Browse files
committed
Add docstring to Switch&Device API functions
1 parent cf13ab8 commit 3c2bdfe

2 files changed

Lines changed: 70 additions & 9 deletions

File tree

src/device.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,24 @@ def get_device_veth_name(self):
8484
return 'veth-{}'.format(self._dev_name)
8585

8686
def run_from_namespace(self, cmd):
87+
"""
88+
Runs a shell command from the device's context (i.e - from
89+
the network namespace that associated to the device).
90+
91+
:param cmd: the shell command to execute
92+
93+
:return str: The result of the command (stdout/stderr)
94+
"""
8795
out, err = run_shell_cmd('ip netns exec {ns} {cmd}'.format(ns=self._dev_name, cmd=cmd))
8896
return out.decode('utf-8') if err == b'' else err.decode('utf-8')
8997

9098
def setup_namespace(self):
91-
''' Safe to call more then once. '''
99+
"""
100+
Sets up the network namespace of the device.
101+
102+
:note: The `Switch` will call this when the device will be connected to it
103+
(using `connect_device_*`), but it is safe to call this more then once.
104+
"""
92105
if self._ns_is_set:
93106
return True
94107

@@ -99,7 +112,12 @@ def setup_namespace(self):
99112
return True
100113

101114
def term(self):
102-
''' Safe to call more then once. '''
115+
"""
116+
Cleans the device's network namespace, and the created veth interfaces.
117+
118+
:note: The `Switch` will call this when the device will be disconnected from it
119+
(using `disconnect_device`), but it is safe to call this more then once.
120+
"""
103121
if self._ns_is_set:
104122
shell_run_and_check('ip netns del {}'.format(self._dev_name))
105123
# We've created the switch's veth interface, so we are responsible for

src/switch.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,39 @@ def _set_trunk_connection(self, dev, vlan):
5151

5252
return True
5353

54+
def set_manipulation(self, cb, punt_policies_bpf=NO_FILTER):
55+
"""
56+
Sets the manipulation routine.
57+
58+
Before the switch checks to which device a packet should be forwarded,
59+
it sends the packet the a manipulation routine.
60+
You can pass a callback that manipulate the packet, for example, change the
61+
destination and source addresses and the vlan tag, in order to perform VLAN-Hopping,
62+
or NAT.
63+
64+
In addition, In case you want to emulate "Punt-Policies" (decide what packets
65+
should be forwarded to the manipulation callback), you can pass a bpf filter.
66+
As mentioned, only packets that are filtered by this bpf filter will be forwarded
67+
to the manipultion routine before continuing the switch's flow.
68+
69+
:param cb: A callback the the manipulation routine.
70+
:param punt_policies_bpf: The "Punt-Policies". Default is no-filter.
71+
"""
72+
if validate_manipulation_cb(cb):
73+
self._connections.set_manipulation(cb, punt_policies_bpf)
74+
return True
75+
76+
return False
77+
5478
def connect_device_access(self, dev, vlan):
79+
"""
80+
Connects device to switch in access mode (meaning - both device and switch
81+
will send untagged packets. Note that the switch will still make sure
82+
that packets from one vlan will not be forwarded to other vlans).
83+
84+
:param dev: A `Device` instance that should be connected to the switch.
85+
:param vlan: The vlan that the device will be associated to.
86+
"""
5587
if not dev.setup_namespace():
5688
raise NamespaceCreationException("failed to create network namespace for {}".format(
5789
dev.get_name))
@@ -67,6 +99,14 @@ def connect_device_access(self, dev, vlan):
6799
self._connections.append_device(dev, vlan, PortType.ACCESS)
68100

69101
def connect_device_trunk(self, dev, vlan):
102+
"""
103+
Connects device to switch in trunk mode (meaning - a vlan interface
104+
will be created for both switch and device. Both will send and recieve tagged
105+
packets, with dot1q layer).
106+
107+
:param dev: A `Device` instance that should be connected to the switch.
108+
:param vlan: The vlan that the device will be associated to.
109+
"""
70110
if not dev.setup_namespace():
71111
raise NamespaceCreationException("failed to create network namespace for {}".format(
72112
dev.get_name))
@@ -82,19 +122,22 @@ def connect_device_trunk(self, dev, vlan):
82122
self._connections.append_device(dev, vlan, PortType.TRUNK)
83123

84124
def disconnect_device(self, dev):
125+
"""
126+
Disconnects a device from switch.
127+
All interfaces and namespaces that were created will be cleaned up.
128+
129+
:param dev: The `Device` instance to disconnect from the switch.
130+
"""
131+
85132
# Delete switch's bridge interface
86133
shell_run_and_check('ip link del br-{}'.format(dev.get_name))
87134

88135
self._connections.remove_device(dev)
89136

90137
dev.term()
91138

92-
def set_manipulation(self, cb, bpf_filter=NO_FILTER):
93-
if validate_manipulation_cb(cb):
94-
self._connections.set_manipulation(cb, bpf_filter)
95-
return True
96-
97-
return False
98-
99139
def term(self):
140+
"""
141+
Terminates all connections, and cleans up all left-over namespaces and interfaces.
142+
"""
100143
self._connections.stop_connections_thread()

0 commit comments

Comments
 (0)