6 from framework import VppTestCase, VppTestRunner
7 from vpp_sub_interface import VppSubInterface, VppDot1QSubint
8 from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto, VppIpMRoute, \
9 VppMRoutePath, MRouteEntryFlags, MRouteItfFlags
10 from vpp_papi_provider import L2_VTR_OP
12 from scapy.packet import Raw
13 from scapy.layers.l2 import Ether, Dot1Q, ARP
14 from scapy.layers.inet import IP, UDP
16 from socket import AF_INET, inet_pton
19 class TestDVR(VppTestCase):
20 """ Distributed Virtual Router """
23 super(TestDVR, self).setUp()
25 self.create_pg_interfaces(range(4))
26 self.create_loopback_interfaces(range(1))
28 for i in self.pg_interfaces:
31 self.loop0.config_ip4()
34 for i in self.pg_interfaces:
36 self.loop0.unconfig_ip4()
38 super(TestDVR, self).tearDown()
40 def assert_same_mac_addr(self, tx, rx):
44 self.assertEqual(t_eth.src, r_eth.src)
45 self.assertEqual(t_eth.dst, r_eth.dst)
47 def assert_has_vlan_tag(self, tag, rx):
50 self.assertEqual(tag, r_1q.vlan)
52 def assert_has_no_tag(self, rx):
54 self.assertFalse(p.haslayer(Dot1Q))
57 """ Distributed Virtual Router """
60 # A packet destined to an IP address that is L2 bridged via
63 ip_non_tag_bridged = "10.10.10.10"
64 ip_tag_bridged = "10.10.10.11"
65 any_src_addr = "1.1.1.1"
67 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
68 dst=self.loop0.local_mac) /
70 dst=ip_non_tag_bridged) /
71 UDP(sport=1234, dport=1234) /
73 pkt_tag = (Ether(src=self.pg0.remote_mac,
74 dst=self.loop0.local_mac) /
77 UDP(sport=1234, dport=1234) /
81 # Two sub-interfaces so we can test VLAN tag push/pop
83 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
84 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
85 sub_if_on_pg2.admin_up()
86 sub_if_on_pg3.admin_up()
89 # Put all the interfaces into a new bridge domain
91 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1)
92 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1)
93 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index, 1)
94 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index, 1)
95 self.vapi.sw_interface_set_l2_bridge(self.loop0.sw_if_index, 1, bvi=1)
97 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg2.sw_if_index,
100 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
105 # Add routes to bridge the traffic via a tagged an nontagged interface
107 route_no_tag = VppIpRoute(
108 self, ip_non_tag_bridged, 32,
109 [VppRoutePath("0.0.0.0",
110 self.pg1.sw_if_index,
112 route_no_tag.add_vpp_config()
115 # Inject the packet that arrives and leaves on a non-tagged interface
116 # Since it's 'bridged' expect that the MAC headed is unchanged.
118 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
119 self.assert_same_mac_addr(pkt_no_tag, rx)
120 self.assert_has_no_tag(rx)
123 # Add routes to bridge the traffic via a tagged interface
125 route_with_tag = VppIpRoute(
126 self, ip_tag_bridged, 32,
127 [VppRoutePath("0.0.0.0",
128 sub_if_on_pg3.sw_if_index,
130 route_with_tag.add_vpp_config()
133 # Inject the packet that arrives non-tag and leaves on a tagged
136 rx = self.send_and_expect(self.pg0, pkt_tag * 65, self.pg3)
137 self.assert_same_mac_addr(pkt_tag, rx)
138 self.assert_has_vlan_tag(93, rx)
143 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
144 dst=self.loop0.local_mac) /
147 dst=ip_tag_bridged) /
148 UDP(sport=1234, dport=1234) /
151 rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * 65, self.pg3)
152 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
153 self.assert_has_vlan_tag(93, rx)
158 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
159 dst=self.loop0.local_mac) /
162 dst=ip_non_tag_bridged) /
163 UDP(sport=1234, dport=1234) /
166 rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * 65, self.pg1)
167 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
168 self.assert_has_no_tag(rx)
171 # Add an output L3 ACL that will block the traffic
173 rule_1 = ({'is_permit': 0,
176 'srcport_or_icmptype_first': 1234,
177 'srcport_or_icmptype_last': 1234,
178 'src_ip_prefix_len': 32,
179 'src_ip_addr': inet_pton(AF_INET, any_src_addr),
180 'dstport_or_icmpcode_first': 1234,
181 'dstport_or_icmpcode_last': 1234,
182 'dst_ip_prefix_len': 32,
183 'dst_ip_addr': inet_pton(AF_INET, ip_non_tag_bridged)})
184 acl = self.vapi.acl_add_replace(acl_index=4294967295,
188 # Apply the ACL on the output interface
190 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
195 # Send packet's that should match the ACL and be dropped
197 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
202 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
204 self.vapi.acl_del(acl.acl_index)
206 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1,
208 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1,
210 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
212 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
214 self.vapi.sw_interface_set_l2_bridge(self.loop0.sw_if_index,
218 # Do a FIB dump to make sure the paths are correctly reported as DVR
220 routes = self.vapi.ip_fib_dump()
223 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
224 self.assertEqual(r.path[0].sw_if_index,
225 sub_if_on_pg3.sw_if_index)
226 self.assertEqual(r.path[0].is_dvr, 1)
227 if (inet_pton(AF_INET, ip_non_tag_bridged) == r.address):
228 self.assertEqual(r.path[0].sw_if_index,
229 self.pg1.sw_if_index)
230 self.assertEqual(r.path[0].is_dvr, 1)
233 # the explicit route delete is require so it happens before
234 # the sbu-interface delete. subinterface delete is required
235 # because that object type does not use the object registry
237 route_no_tag.remove_vpp_config()
238 route_with_tag.remove_vpp_config()
239 sub_if_on_pg3.remove_vpp_config()
240 sub_if_on_pg2.remove_vpp_config()
242 def test_l2_emulation(self):
246 # non distinct L3 packets, in the tag/non-tag combos
248 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
249 dst=self.pg1.remote_mac) /
252 UDP(sport=1234, dport=1234) /
254 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
255 dst=self.pg2.remote_mac) /
258 UDP(sport=1234, dport=1234) /
260 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
261 dst=self.pg2.remote_mac) /
265 UDP(sport=1234, dport=1234) /
267 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
268 dst=self.pg2.remote_mac) /
272 UDP(sport=1234, dport=1234) /
274 pkt_bcast = (Ether(src=self.pg0.remote_mac,
275 dst="ff:ff:ff:ff:ff:ff") /
277 dst="255.255.255.255") /
278 UDP(sport=1234, dport=1234) /
282 # A couple of sub-interfaces for tags
284 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
285 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
286 sub_if_on_pg2.admin_up()
287 sub_if_on_pg3.admin_up()
290 # Put all the interfaces into a new bridge domain
292 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1)
293 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1)
294 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index, 1)
295 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index, 1)
296 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg2.sw_if_index,
299 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
304 # Disable UU flooding, learning and ARP terminaation. makes this test
305 # easier as unicast packets are dropped if not extracted.
307 self.vapi.bridge_flags(1, 0, (1 << 0) | (1 << 3) | (1 << 4))
310 # Add a DVR route to steer traffic at L3
312 route_1 = VppIpRoute(self, "1.1.1.1", 32,
313 [VppRoutePath("0.0.0.0",
314 self.pg1.sw_if_index,
316 route_2 = VppIpRoute(self, "1.1.1.2", 32,
317 [VppRoutePath("0.0.0.0",
318 sub_if_on_pg2.sw_if_index,
320 route_1.add_vpp_config()
321 route_2.add_vpp_config()
324 # packets are dropped because bridge does not flood unknown unicast
326 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
329 # Enable L3 extraction on pgs
331 self.vapi.sw_interface_set_l2_emulation(self.pg0.sw_if_index)
332 self.vapi.sw_interface_set_l2_emulation(self.pg1.sw_if_index)
333 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg2.sw_if_index)
334 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg3.sw_if_index)
337 # now we expect the packet forward according to the DVR route
339 rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
340 self.assert_same_mac_addr(pkt_no_tag, rx)
341 self.assert_has_no_tag(rx)
343 rx = self.send_and_expect(self.pg0, pkt_to_tag * 65, self.pg2)
344 self.assert_same_mac_addr(pkt_to_tag, rx)
345 self.assert_has_vlan_tag(92, rx)
347 rx = self.send_and_expect(self.pg3, pkt_from_tag * 65, self.pg1)
348 self.assert_same_mac_addr(pkt_from_tag, rx)
349 self.assert_has_no_tag(rx)
351 rx = self.send_and_expect(self.pg3, pkt_from_to_tag * 65, self.pg2)
352 self.assert_same_mac_addr(pkt_from_tag, rx)
353 self.assert_has_vlan_tag(92, rx)
356 # but broadcast packets are still flooded
358 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
363 self.vapi.sw_interface_set_l2_emulation(self.pg0.sw_if_index,
365 self.vapi.sw_interface_set_l2_emulation(self.pg1.sw_if_index,
367 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg2.sw_if_index,
369 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg3.sw_if_index,
372 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index,
374 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index,
376 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
378 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
381 route_1.remove_vpp_config()
382 route_2.remove_vpp_config()
383 sub_if_on_pg3.remove_vpp_config()
384 sub_if_on_pg2.remove_vpp_config()
387 if __name__ == '__main__':
388 unittest.main(testRunner=VppTestRunner)