@@ -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