4 from framework import VppTestCase, VppTestRunner
5 from vpp_ip_route import VppIpRoute, VppRoutePath
6 from vpp_l2 import L2_PORT_TYPE
7 from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint
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 """
20 super(TestDVR, cls).setUpClass()
23 def tearDownClass(cls):
24 super(TestDVR, cls).tearDownClass()
27 super(TestDVR, self).setUp()
29 self.create_pg_interfaces(range(4))
30 self.create_loopback_interfaces(1)
32 for i in self.pg_interfaces:
35 self.loop0.config_ip4()
38 for i in self.pg_interfaces:
40 self.loop0.unconfig_ip4()
42 super(TestDVR, self).tearDown()
44 def assert_same_mac_addr(self, tx, rx):
48 self.assertEqual(t_eth.src, r_eth.src)
49 self.assertEqual(t_eth.dst, r_eth.dst)
51 def assert_has_vlan_tag(self, tag, rx):
54 self.assertEqual(tag, r_1q.vlan)
56 def assert_has_no_tag(self, rx):
58 self.assertFalse(p.haslayer(Dot1Q))
61 """ Distributed Virtual Router """
64 # A packet destined to an IP address that is L2 bridged via
67 ip_non_tag_bridged = "10.10.10.10"
68 ip_tag_bridged = "10.10.10.11"
69 any_src_addr = "1.1.1.1"
71 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
72 dst=self.loop0.local_mac) /
74 dst=ip_non_tag_bridged) /
75 UDP(sport=1234, dport=1234) /
77 pkt_tag = (Ether(src=self.pg0.remote_mac,
78 dst=self.loop0.local_mac) /
81 UDP(sport=1234, dport=1234) /
85 # Two sub-interfaces so we can test VLAN tag push/pop
87 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
88 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
89 sub_if_on_pg2.admin_up()
90 sub_if_on_pg3.admin_up()
93 # Put all the interfaces into a new bridge domain
95 self.vapi.sw_interface_set_l2_bridge(
96 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
97 self.vapi.sw_interface_set_l2_bridge(
98 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
99 self.vapi.sw_interface_set_l2_bridge(
100 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
101 self.vapi.sw_interface_set_l2_bridge(
102 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
103 self.vapi.sw_interface_set_l2_bridge(
104 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
105 port_type=L2_PORT_TYPE.BVI)
107 self.vapi.l2_interface_vlan_tag_rewrite(
108 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
110 self.vapi.l2_interface_vlan_tag_rewrite(
111 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
115 # Add routes to bridge the traffic via a tagged an nontagged interface
117 route_no_tag = VppIpRoute(
118 self, ip_non_tag_bridged, 32,
119 [VppRoutePath("0.0.0.0",
120 self.pg1.sw_if_index,
122 route_no_tag.add_vpp_config()
125 # Inject the packet that arrives and leaves on a non-tagged interface
126 # Since it's 'bridged' expect that the MAC headed is unchanged.
128 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
129 self.assert_same_mac_addr(pkt_no_tag, rx)
130 self.assert_has_no_tag(rx)
133 # Add routes to bridge the traffic via a tagged interface
135 route_with_tag = VppIpRoute(
136 self, ip_tag_bridged, 32,
137 [VppRoutePath("0.0.0.0",
138 sub_if_on_pg3.sw_if_index,
140 route_with_tag.add_vpp_config()
143 # Inject the packet that arrives non-tag and leaves on a tagged
146 rx = self.send_and_expect(self.pg0, pkt_tag * 65, self.pg3)
147 self.assert_same_mac_addr(pkt_tag, rx)
148 self.assert_has_vlan_tag(93, rx)
153 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
154 dst=self.loop0.local_mac) /
157 dst=ip_tag_bridged) /
158 UDP(sport=1234, dport=1234) /
161 rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * 65, self.pg3)
162 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
163 self.assert_has_vlan_tag(93, rx)
168 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
169 dst=self.loop0.local_mac) /
172 dst=ip_non_tag_bridged) /
173 UDP(sport=1234, dport=1234) /
176 rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * 65, self.pg1)
177 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
178 self.assert_has_no_tag(rx)
181 # Add an output L3 ACL that will block the traffic
183 rule_1 = ({'is_permit': 0,
186 'srcport_or_icmptype_first': 1234,
187 'srcport_or_icmptype_last': 1234,
188 'src_ip_prefix_len': 32,
189 'src_ip_addr': inet_pton(AF_INET, any_src_addr),
190 'dstport_or_icmpcode_first': 1234,
191 'dstport_or_icmpcode_last': 1234,
192 'dst_ip_prefix_len': 32,
193 'dst_ip_addr': inet_pton(AF_INET, ip_non_tag_bridged)})
194 acl = self.vapi.acl_add_replace(acl_index=4294967295,
198 # Apply the ACL on the output interface
200 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
205 # Send packet's that should match the ACL and be dropped
207 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
212 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
214 self.vapi.acl_del(acl.acl_index)
216 self.vapi.sw_interface_set_l2_bridge(
217 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
218 self.vapi.sw_interface_set_l2_bridge(
219 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
220 self.vapi.sw_interface_set_l2_bridge(
221 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
222 self.vapi.sw_interface_set_l2_bridge(
223 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
224 self.vapi.sw_interface_set_l2_bridge(
225 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
226 port_type=L2_PORT_TYPE.BVI, enable=0)
229 # Do a FIB dump to make sure the paths are correctly reported as DVR
231 routes = self.vapi.ip_fib_dump()
234 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
235 self.assertEqual(r.path[0].sw_if_index,
236 sub_if_on_pg3.sw_if_index)
237 self.assertEqual(r.path[0].is_dvr, 1)
238 if (inet_pton(AF_INET, ip_non_tag_bridged) == r.address):
239 self.assertEqual(r.path[0].sw_if_index,
240 self.pg1.sw_if_index)
241 self.assertEqual(r.path[0].is_dvr, 1)
244 # the explicit route delete is require so it happens before
245 # the sbu-interface delete. subinterface delete is required
246 # because that object type does not use the object registry
248 route_no_tag.remove_vpp_config()
249 route_with_tag.remove_vpp_config()
250 sub_if_on_pg3.remove_vpp_config()
251 sub_if_on_pg2.remove_vpp_config()
253 def test_l2_emulation(self):
257 # non distinct L3 packets, in the tag/non-tag combos
259 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
260 dst=self.pg1.remote_mac) /
263 UDP(sport=1234, dport=1234) /
265 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
266 dst=self.pg2.remote_mac) /
269 UDP(sport=1234, dport=1234) /
271 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
272 dst=self.pg2.remote_mac) /
276 UDP(sport=1234, dport=1234) /
278 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
279 dst=self.pg2.remote_mac) /
283 UDP(sport=1234, dport=1234) /
285 pkt_bcast = (Ether(src=self.pg0.remote_mac,
286 dst="ff:ff:ff:ff:ff:ff") /
288 dst="255.255.255.255") /
289 UDP(sport=1234, dport=1234) /
293 # A couple of sub-interfaces for tags
295 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
296 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
297 sub_if_on_pg2.admin_up()
298 sub_if_on_pg3.admin_up()
301 # Put all the interfaces into a new bridge domain
303 self.vapi.sw_interface_set_l2_bridge(
304 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
305 self.vapi.sw_interface_set_l2_bridge(
306 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
307 self.vapi.sw_interface_set_l2_bridge(
308 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
309 self.vapi.sw_interface_set_l2_bridge(
310 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
311 self.vapi.l2_interface_vlan_tag_rewrite(
312 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
314 self.vapi.l2_interface_vlan_tag_rewrite(
315 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
319 # Disable UU flooding, learning and ARP termination. makes this test
320 # easier as unicast packets are dropped if not extracted.
322 self.vapi.bridge_flags(bd_id=1, is_set=0,
323 flags=(1 << 0) | (1 << 3) | (1 << 4))
326 # Add a DVR route to steer traffic at L3
328 route_1 = VppIpRoute(self, "1.1.1.1", 32,
329 [VppRoutePath("0.0.0.0",
330 self.pg1.sw_if_index,
332 route_2 = VppIpRoute(self, "1.1.1.2", 32,
333 [VppRoutePath("0.0.0.0",
334 sub_if_on_pg2.sw_if_index,
336 route_1.add_vpp_config()
337 route_2.add_vpp_config()
340 # packets are dropped because bridge does not flood unknown unicast
342 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
345 # Enable L3 extraction on pgs
347 self.vapi.l2_emulation(self.pg0.sw_if_index)
348 self.vapi.l2_emulation(self.pg1.sw_if_index)
349 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index)
350 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index)
353 # now we expect the packet forward according to the DVR route
355 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
356 self.assert_same_mac_addr(pkt_no_tag, rx)
357 self.assert_has_no_tag(rx)
359 rx = self.send_and_expect(self.pg0, pkt_to_tag * 65, self.pg2)
360 self.assert_same_mac_addr(pkt_to_tag, rx)
361 self.assert_has_vlan_tag(92, rx)
363 rx = self.send_and_expect(self.pg3, pkt_from_tag * 65, self.pg1)
364 self.assert_same_mac_addr(pkt_from_tag, rx)
365 self.assert_has_no_tag(rx)
367 rx = self.send_and_expect(self.pg3, pkt_from_to_tag * 65, self.pg2)
368 self.assert_same_mac_addr(pkt_from_tag, rx)
369 self.assert_has_vlan_tag(92, rx)
372 # but broadcast packets are still flooded
374 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
379 self.vapi.l2_emulation(self.pg0.sw_if_index,
381 self.vapi.l2_emulation(self.pg1.sw_if_index,
383 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index,
385 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index,
388 self.vapi.sw_interface_set_l2_bridge(
389 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
390 self.vapi.sw_interface_set_l2_bridge(
391 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
392 self.vapi.sw_interface_set_l2_bridge(
393 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
394 self.vapi.sw_interface_set_l2_bridge(
395 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
397 route_1.remove_vpp_config()
398 route_2.remove_vpp_config()
399 sub_if_on_pg3.remove_vpp_config()
400 sub_if_on_pg2.remove_vpp_config()
403 if __name__ == '__main__':
404 unittest.main(testRunner=VppTestRunner)