blob: 9760f2f98a293bfb9253205debafb25b2b993911 [file] [log] [blame]
Neale Ranns6f631152017-10-03 08:20:21 -07001#!/usr/bin/env python
Neale Ranns6f631152017-10-03 08:20:21 -07002import unittest
3
4from framework import VppTestCase, VppTestRunner
Klement Sekerab9ef2732018-06-24 22:49:33 +02005from vpp_ip_route import VppIpRoute, VppRoutePath
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -07006from vpp_l2 import L2_PORT_TYPE
7from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint
Neale Ranns6f631152017-10-03 08:20:21 -07008
9from scapy.packet import Raw
Klement Sekerab9ef2732018-06-24 22:49:33 +020010from scapy.layers.l2 import Ether, Dot1Q
Neale Ranns6f631152017-10-03 08:20:21 -070011from scapy.layers.inet import IP, UDP
Neale Rannsf068c3e2018-01-03 04:18:48 -080012from socket import AF_INET, inet_pton
Neale Ranns6f631152017-10-03 08:20:21 -070013
14
15class TestDVR(VppTestCase):
Neale Ranns62fe07c2017-10-31 12:28:22 -070016 """ Distributed Virtual Router """
Neale Ranns6f631152017-10-03 08:20:21 -070017
18 def setUp(self):
19 super(TestDVR, self).setUp()
20
21 self.create_pg_interfaces(range(4))
Klement Sekerab9ef2732018-06-24 22:49:33 +020022 self.create_loopback_interfaces(1)
Neale Ranns6f631152017-10-03 08:20:21 -070023
24 for i in self.pg_interfaces:
25 i.admin_up()
26
27 self.loop0.config_ip4()
28
29 def tearDown(self):
30 for i in self.pg_interfaces:
31 i.admin_down()
32 self.loop0.unconfig_ip4()
33
34 super(TestDVR, self).tearDown()
35
Neale Ranns55d03782017-10-21 06:34:22 -070036 def assert_same_mac_addr(self, tx, rx):
37 t_eth = tx[Ether]
38 for p in rx:
39 r_eth = p[Ether]
40 self.assertEqual(t_eth.src, r_eth.src)
41 self.assertEqual(t_eth.dst, r_eth.dst)
42
43 def assert_has_vlan_tag(self, tag, rx):
44 for p in rx:
45 r_1q = p[Dot1Q]
46 self.assertEqual(tag, r_1q.vlan)
47
48 def assert_has_no_tag(self, rx):
49 for p in rx:
50 self.assertFalse(p.haslayer(Dot1Q))
51
Neale Ranns6f631152017-10-03 08:20:21 -070052 def test_dvr(self):
53 """ Distributed Virtual Router """
54
55 #
56 # A packet destined to an IP address that is L2 bridged via
57 # a non-tag interface
58 #
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"
62
63 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
64 dst=self.loop0.local_mac) /
65 IP(src=any_src_addr,
66 dst=ip_non_tag_bridged) /
67 UDP(sport=1234, dport=1234) /
68 Raw('\xa5' * 100))
69 pkt_tag = (Ether(src=self.pg0.remote_mac,
70 dst=self.loop0.local_mac) /
71 IP(src=any_src_addr,
72 dst=ip_tag_bridged) /
73 UDP(sport=1234, dport=1234) /
74 Raw('\xa5' * 100))
75
76 #
77 # Two sub-interfaces so we can test VLAN tag push/pop
78 #
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()
83
84 #
85 # Put all the interfaces into a new bridge domain
86 #
Ole Troana5b2eec2019-03-11 19:23:25 +010087 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)
Neale Ranns6f631152017-10-03 08:20:21 -070098
Ole Troane1ade682019-03-04 23:55:43 +010099 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100100 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
101 push_dot1q=92)
Ole Troane1ade682019-03-04 23:55:43 +0100102 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100103 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
104 push_dot1q=93)
Neale Ranns6f631152017-10-03 08:20:21 -0700105
Neale Ranns6f631152017-10-03 08:20:21 -0700106 #
107 # Add routes to bridge the traffic via a tagged an nontagged interface
108 #
109 route_no_tag = VppIpRoute(
110 self, ip_non_tag_bridged, 32,
111 [VppRoutePath("0.0.0.0",
112 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800113 is_dvr=1)])
Neale Ranns6f631152017-10-03 08:20:21 -0700114 route_no_tag.add_vpp_config()
115
116 #
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.
119 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800120 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700123
124 #
125 # Add routes to bridge the traffic via a tagged interface
126 #
Neale Ranns55d03782017-10-21 06:34:22 -0700127 route_with_tag = VppIpRoute(
Neale Ranns6f631152017-10-03 08:20:21 -0700128 self, ip_tag_bridged, 32,
129 [VppRoutePath("0.0.0.0",
130 sub_if_on_pg3.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800131 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700132 route_with_tag.add_vpp_config()
Neale Ranns6f631152017-10-03 08:20:21 -0700133
134 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800135 # Inject the packet that arrives non-tag and leaves on a tagged
136 # interface
Neale Ranns6f631152017-10-03 08:20:21 -0700137 #
Neale Ranns55d03782017-10-21 06:34:22 -0700138 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700141
142 #
143 # Tag to tag
144 #
145 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
146 dst=self.loop0.local_mac) /
147 Dot1Q(vlan=92) /
148 IP(src=any_src_addr,
149 dst=ip_tag_bridged) /
150 UDP(sport=1234, dport=1234) /
151 Raw('\xa5' * 100))
152
Neale Ranns55d03782017-10-21 06:34:22 -0700153 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700156
157 #
158 # Tag to non-Tag
159 #
160 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
161 dst=self.loop0.local_mac) /
162 Dot1Q(vlan=92) /
163 IP(src=any_src_addr,
164 dst=ip_non_tag_bridged) /
165 UDP(sport=1234, dport=1234) /
166 Raw('\xa5' * 100))
167
Neale Ranns55d03782017-10-21 06:34:22 -0700168 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700171
Neale Ranns55d03782017-10-21 06:34:22 -0700172 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800173 # Add an output L3 ACL that will block the traffic
174 #
175 rule_1 = ({'is_permit': 0,
176 'is_ipv6': 0,
177 'proto': 17,
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,
187 r=[rule_1])
188
189 #
190 # Apply the ACL on the output interface
191 #
192 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
193 0,
194 [acl.acl_index])
195
196 #
197 # Send packet's that should match the ACL and be dropped
198 #
199 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
200
201 #
Neale Ranns55d03782017-10-21 06:34:22 -0700202 # cleanup
203 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800204 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
205 0, [])
206 self.vapi.acl_del(acl.acl_index)
207
Ole Troana5b2eec2019-03-11 19:23:25 +0100208 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)
Neale Ranns55d03782017-10-21 06:34:22 -0700219
220 #
Neale Ranns81458422018-03-12 06:59:36 -0700221 # Do a FIB dump to make sure the paths are correctly reported as DVR
222 #
223 routes = self.vapi.ip_fib_dump()
224
225 for r in routes:
226 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
Neale Ranns81458422018-03-12 06:59:36 -0700227 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):
Neale Ranns81458422018-03-12 06:59:36 -0700231 self.assertEqual(r.path[0].sw_if_index,
232 self.pg1.sw_if_index)
233 self.assertEqual(r.path[0].is_dvr, 1)
234
235 #
Neale Ranns55d03782017-10-21 06:34:22 -0700236 # 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
239 #
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()
244
245 def test_l2_emulation(self):
246 """ L2 Emulation """
247
248 #
249 # non distinct L3 packets, in the tag/non-tag combos
250 #
251 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
252 dst=self.pg1.remote_mac) /
253 IP(src="2.2.2.2",
254 dst="1.1.1.1") /
255 UDP(sport=1234, dport=1234) /
256 Raw('\xa5' * 100))
257 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
258 dst=self.pg2.remote_mac) /
259 IP(src="2.2.2.2",
260 dst="1.1.1.2") /
261 UDP(sport=1234, dport=1234) /
262 Raw('\xa5' * 100))
263 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
264 dst=self.pg2.remote_mac) /
265 Dot1Q(vlan=93) /
266 IP(src="2.2.2.2",
267 dst="1.1.1.1") /
268 UDP(sport=1234, dport=1234) /
269 Raw('\xa5' * 100))
270 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
271 dst=self.pg2.remote_mac) /
272 Dot1Q(vlan=93) /
273 IP(src="2.2.2.2",
274 dst="1.1.1.2") /
275 UDP(sport=1234, dport=1234) /
276 Raw('\xa5' * 100))
277 pkt_bcast = (Ether(src=self.pg0.remote_mac,
278 dst="ff:ff:ff:ff:ff:ff") /
279 IP(src="2.2.2.2",
280 dst="255.255.255.255") /
281 UDP(sport=1234, dport=1234) /
282 Raw('\xa5' * 100))
283
284 #
285 # A couple of sub-interfaces for tags
286 #
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()
291
292 #
293 # Put all the interfaces into a new bridge domain
294 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100295 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)
Ole Troane1ade682019-03-04 23:55:43 +0100303 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100304 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
305 push_dot1q=92)
Ole Troane1ade682019-03-04 23:55:43 +0100306 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100307 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
308 push_dot1q=93)
Neale Ranns55d03782017-10-21 06:34:22 -0700309
310 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700311 # Disable UU flooding, learning and ARP termination. makes this test
Neale Ranns55d03782017-10-21 06:34:22 -0700312 # easier as unicast packets are dropped if not extracted.
313 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100314 self.vapi.bridge_flags(bd_id=1, is_set=0,
315 flags=(1 << 0) | (1 << 3) | (1 << 4))
Neale Ranns55d03782017-10-21 06:34:22 -0700316
317 #
318 # Add a DVR route to steer traffic at L3
319 #
320 route_1 = VppIpRoute(self, "1.1.1.1", 32,
321 [VppRoutePath("0.0.0.0",
322 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800323 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700324 route_2 = VppIpRoute(self, "1.1.1.2", 32,
325 [VppRoutePath("0.0.0.0",
326 sub_if_on_pg2.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800327 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700328 route_1.add_vpp_config()
329 route_2.add_vpp_config()
330
331 #
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300332 # packets are dropped because bridge does not flood unknown unicast
Neale Ranns55d03782017-10-21 06:34:22 -0700333 #
334 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
335
336 #
337 # Enable L3 extraction on pgs
338 #
Ole Troane1ade682019-03-04 23:55:43 +0100339 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)
Neale Ranns55d03782017-10-21 06:34:22 -0700343
344 #
345 # now we expect the packet forward according to the DVR route
346 #
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)
350
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)
354
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)
358
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)
362
363 #
364 # but broadcast packets are still flooded
365 #
366 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
367
368 #
369 # cleanup
370 #
Ole Troane1ade682019-03-04 23:55:43 +0100371 self.vapi.l2_emulation(self.pg0.sw_if_index,
372 enable=0)
373 self.vapi.l2_emulation(self.pg1.sw_if_index,
374 enable=0)
375 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index,
376 enable=0)
377 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index,
378 enable=0)
Neale Ranns55d03782017-10-21 06:34:22 -0700379
Ole Troana5b2eec2019-03-11 19:23:25 +0100380 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)
Neale Ranns55d03782017-10-21 06:34:22 -0700388
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()
393
Neale Ranns6f631152017-10-03 08:20:21 -0700394
395if __name__ == '__main__':
396 unittest.main(testRunner=VppTestRunner)