4 from framework import VppTestCase, VppTestRunner
5 from vpp_sub_interface import VppDot1QSubint
6 from vpp_ip_route import VppIpRoute, VppRoutePath
7 from vpp_papi_provider import L2_VTR_OP, L2_PORT_TYPE
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether, Dot1Q
11 from scapy.layers.inet import IP, UDP
12 from socket import AF_INET, inet_pton
15 class TestDVR(VppTestCase):
16 """ Distributed Virtual Router """
19 super(TestDVR, self).setUp()
21 self.create_pg_interfaces(range(4))
22 self.create_loopback_interfaces(1)
24 for i in self.pg_interfaces:
27 self.loop0.config_ip4()
30 for i in self.pg_interfaces:
32 self.loop0.unconfig_ip4()
34 super(TestDVR, self).tearDown()
36 def assert_same_mac_addr(self, tx, rx):
40 self.assertEqual(t_eth.src, r_eth.src)
41 self.assertEqual(t_eth.dst, r_eth.dst)
43 def assert_has_vlan_tag(self, tag, rx):
46 self.assertEqual(tag, r_1q.vlan)
48 def assert_has_no_tag(self, rx):
50 self.assertFalse(p.haslayer(Dot1Q))
53 """ Distributed Virtual Router """
56 # A packet destined to an IP address that is L2 bridged via
59 ip_non_tag_bridged = "10.10.10.10"
60 ip_tag_bridged = "10.10.10.11"
61 any_src_addr = "1.1.1.1"
63 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
64 dst=self.loop0.local_mac) /
66 dst=ip_non_tag_bridged) /
67 UDP(sport=1234, dport=1234) /
69 pkt_tag = (Ether(src=self.pg0.remote_mac,
70 dst=self.loop0.local_mac) /
73 UDP(sport=1234, dport=1234) /
77 # Two sub-interfaces so we can test VLAN tag push/pop
79 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
80 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
81 sub_if_on_pg2.admin_up()
82 sub_if_on_pg3.admin_up()
85 # Put all the interfaces into a new bridge domain
87 self.vapi.sw_interface_set_l2_bridge(
88 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
89 self.vapi.sw_interface_set_l2_bridge(
90 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
91 self.vapi.sw_interface_set_l2_bridge(
92 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
93 self.vapi.sw_interface_set_l2_bridge(
94 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
95 self.vapi.sw_interface_set_l2_bridge(
96 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
97 port_type=L2_PORT_TYPE.BVI)
99 self.vapi.l2_interface_vlan_tag_rewrite(
100 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
102 self.vapi.l2_interface_vlan_tag_rewrite(
103 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
107 # Add routes to bridge the traffic via a tagged an nontagged interface
109 route_no_tag = VppIpRoute(
110 self, ip_non_tag_bridged, 32,
111 [VppRoutePath("0.0.0.0",
112 self.pg1.sw_if_index,
114 route_no_tag.add_vpp_config()
117 # Inject the packet that arrives and leaves on a non-tagged interface
118 # Since it's 'bridged' expect that the MAC headed is unchanged.
120 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
121 self.assert_same_mac_addr(pkt_no_tag, rx)
122 self.assert_has_no_tag(rx)
125 # Add routes to bridge the traffic via a tagged interface
127 route_with_tag = VppIpRoute(
128 self, ip_tag_bridged, 32,
129 [VppRoutePath("0.0.0.0",
130 sub_if_on_pg3.sw_if_index,
132 route_with_tag.add_vpp_config()
135 # Inject the packet that arrives non-tag and leaves on a tagged
138 rx = self.send_and_expect(self.pg0, pkt_tag * 65, self.pg3)
139 self.assert_same_mac_addr(pkt_tag, rx)
140 self.assert_has_vlan_tag(93, rx)
145 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
146 dst=self.loop0.local_mac) /
149 dst=ip_tag_bridged) /
150 UDP(sport=1234, dport=1234) /
153 rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * 65, self.pg3)
154 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
155 self.assert_has_vlan_tag(93, rx)
160 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
161 dst=self.loop0.local_mac) /
164 dst=ip_non_tag_bridged) /
165 UDP(sport=1234, dport=1234) /
168 rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * 65, self.pg1)
169 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
170 self.assert_has_no_tag(rx)
173 # Add an output L3 ACL that will block the traffic
175 rule_1 = ({'is_permit': 0,
178 'srcport_or_icmptype_first': 1234,
179 'srcport_or_icmptype_last': 1234,
180 'src_ip_prefix_len': 32,
181 'src_ip_addr': inet_pton(AF_INET, any_src_addr),
182 'dstport_or_icmpcode_first': 1234,
183 'dstport_or_icmpcode_last': 1234,
184 'dst_ip_prefix_len': 32,
185 'dst_ip_addr': inet_pton(AF_INET, ip_non_tag_bridged)})
186 acl = self.vapi.acl_add_replace(acl_index=4294967295,
190 # Apply the ACL on the output interface
192 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
197 # Send packet's that should match the ACL and be dropped
199 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
204 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
206 self.vapi.acl_del(acl.acl_index)
208 self.vapi.sw_interface_set_l2_bridge(
209 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
210 self.vapi.sw_interface_set_l2_bridge(
211 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
212 self.vapi.sw_interface_set_l2_bridge(
213 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
214 self.vapi.sw_interface_set_l2_bridge(
215 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
216 self.vapi.sw_interface_set_l2_bridge(
217 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
218 port_type=L2_PORT_TYPE.BVI, enable=0)
221 # Do a FIB dump to make sure the paths are correctly reported as DVR
223 routes = self.vapi.ip_fib_dump()
226 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
227 self.assertEqual(r.path[0].sw_if_index,
228 sub_if_on_pg3.sw_if_index)
229 self.assertEqual(r.path[0].is_dvr, 1)
230 if (inet_pton(AF_INET, ip_non_tag_bridged) == r.address):
231 self.assertEqual(r.path[0].sw_if_index,
232 self.pg1.sw_if_index)
233 self.assertEqual(r.path[0].is_dvr, 1)
236 # the explicit route delete is require so it happens before
237 # the sbu-interface delete. subinterface delete is required
238 # because that object type does not use the object registry
240 route_no_tag.remove_vpp_config()
241 route_with_tag.remove_vpp_config()
242 sub_if_on_pg3.remove_vpp_config()
243 sub_if_on_pg2.remove_vpp_config()
245 def test_l2_emulation(self):
249 # non distinct L3 packets, in the tag/non-tag combos
251 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
252 dst=self.pg1.remote_mac) /
255 UDP(sport=1234, dport=1234) /
257 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
258 dst=self.pg2.remote_mac) /
261 UDP(sport=1234, dport=1234) /
263 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
264 dst=self.pg2.remote_mac) /
268 UDP(sport=1234, dport=1234) /
270 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
271 dst=self.pg2.remote_mac) /
275 UDP(sport=1234, dport=1234) /
277 pkt_bcast = (Ether(src=self.pg0.remote_mac,
278 dst="ff:ff:ff:ff:ff:ff") /
280 dst="255.255.255.255") /
281 UDP(sport=1234, dport=1234) /
285 # A couple of sub-interfaces for tags
287 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
288 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
289 sub_if_on_pg2.admin_up()
290 sub_if_on_pg3.admin_up()
293 # Put all the interfaces into a new bridge domain
295 self.vapi.sw_interface_set_l2_bridge(
296 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
297 self.vapi.sw_interface_set_l2_bridge(
298 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
299 self.vapi.sw_interface_set_l2_bridge(
300 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
301 self.vapi.sw_interface_set_l2_bridge(
302 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
303 self.vapi.l2_interface_vlan_tag_rewrite(
304 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
306 self.vapi.l2_interface_vlan_tag_rewrite(
307 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
311 # Disable UU flooding, learning and ARP terminaation. makes this test
312 # easier as unicast packets are dropped if not extracted.
314 self.vapi.bridge_flags(bd_id=1, is_set=0,
315 flags=(1 << 0) | (1 << 3) | (1 << 4))
318 # Add a DVR route to steer traffic at L3
320 route_1 = VppIpRoute(self, "1.1.1.1", 32,
321 [VppRoutePath("0.0.0.0",
322 self.pg1.sw_if_index,
324 route_2 = VppIpRoute(self, "1.1.1.2", 32,
325 [VppRoutePath("0.0.0.0",
326 sub_if_on_pg2.sw_if_index,
328 route_1.add_vpp_config()
329 route_2.add_vpp_config()
332 # packets are dropped because bridge does not flood unknown unicast
334 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
337 # Enable L3 extraction on pgs
339 self.vapi.l2_emulation(self.pg0.sw_if_index)
340 self.vapi.l2_emulation(self.pg1.sw_if_index)
341 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index)
342 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index)
345 # now we expect the packet forward according to the DVR route
347 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
348 self.assert_same_mac_addr(pkt_no_tag, rx)
349 self.assert_has_no_tag(rx)
351 rx = self.send_and_expect(self.pg0, pkt_to_tag * 65, self.pg2)
352 self.assert_same_mac_addr(pkt_to_tag, rx)
353 self.assert_has_vlan_tag(92, rx)
355 rx = self.send_and_expect(self.pg3, pkt_from_tag * 65, self.pg1)
356 self.assert_same_mac_addr(pkt_from_tag, rx)
357 self.assert_has_no_tag(rx)
359 rx = self.send_and_expect(self.pg3, pkt_from_to_tag * 65, self.pg2)
360 self.assert_same_mac_addr(pkt_from_tag, rx)
361 self.assert_has_vlan_tag(92, rx)
364 # but broadcast packets are still flooded
366 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
371 self.vapi.l2_emulation(self.pg0.sw_if_index,
373 self.vapi.l2_emulation(self.pg1.sw_if_index,
375 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index,
377 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index,
380 self.vapi.sw_interface_set_l2_bridge(
381 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
382 self.vapi.sw_interface_set_l2_bridge(
383 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
384 self.vapi.sw_interface_set_l2_bridge(
385 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
386 self.vapi.sw_interface_set_l2_bridge(
387 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
389 route_1.remove_vpp_config()
390 route_2.remove_vpp_config()
391 sub_if_on_pg3.remove_vpp_config()
392 sub_if_on_pg2.remove_vpp_config()
395 if __name__ == '__main__':
396 unittest.main(testRunner=VppTestRunner)