blob: ae7864cc3e68a7e12314aeca1015531172ca3244 [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
Paul Vinciguerra4271c972019-05-14 13:25:49 -040014NUM_PKTS = 67
15
Neale Ranns6f631152017-10-03 08:20:21 -070016
17class TestDVR(VppTestCase):
Neale Ranns62fe07c2017-10-31 12:28:22 -070018 """ Distributed Virtual Router """
Neale Ranns6f631152017-10-03 08:20:21 -070019
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070020 @classmethod
21 def setUpClass(cls):
22 super(TestDVR, cls).setUpClass()
23
24 @classmethod
25 def tearDownClass(cls):
26 super(TestDVR, cls).tearDownClass()
27
Neale Ranns6f631152017-10-03 08:20:21 -070028 def setUp(self):
29 super(TestDVR, self).setUp()
30
31 self.create_pg_interfaces(range(4))
Klement Sekerab9ef2732018-06-24 22:49:33 +020032 self.create_loopback_interfaces(1)
Neale Ranns6f631152017-10-03 08:20:21 -070033
34 for i in self.pg_interfaces:
35 i.admin_up()
36
37 self.loop0.config_ip4()
38
39 def tearDown(self):
40 for i in self.pg_interfaces:
41 i.admin_down()
42 self.loop0.unconfig_ip4()
43
44 super(TestDVR, self).tearDown()
45
Neale Ranns55d03782017-10-21 06:34:22 -070046 def assert_same_mac_addr(self, tx, rx):
47 t_eth = tx[Ether]
48 for p in rx:
49 r_eth = p[Ether]
50 self.assertEqual(t_eth.src, r_eth.src)
51 self.assertEqual(t_eth.dst, r_eth.dst)
52
53 def assert_has_vlan_tag(self, tag, rx):
54 for p in rx:
55 r_1q = p[Dot1Q]
56 self.assertEqual(tag, r_1q.vlan)
57
58 def assert_has_no_tag(self, rx):
59 for p in rx:
60 self.assertFalse(p.haslayer(Dot1Q))
61
Neale Ranns6f631152017-10-03 08:20:21 -070062 def test_dvr(self):
63 """ Distributed Virtual Router """
64
65 #
66 # A packet destined to an IP address that is L2 bridged via
67 # a non-tag interface
68 #
69 ip_non_tag_bridged = "10.10.10.10"
70 ip_tag_bridged = "10.10.10.11"
71 any_src_addr = "1.1.1.1"
72
73 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
74 dst=self.loop0.local_mac) /
75 IP(src=any_src_addr,
76 dst=ip_non_tag_bridged) /
77 UDP(sport=1234, dport=1234) /
78 Raw('\xa5' * 100))
79 pkt_tag = (Ether(src=self.pg0.remote_mac,
80 dst=self.loop0.local_mac) /
81 IP(src=any_src_addr,
82 dst=ip_tag_bridged) /
83 UDP(sport=1234, dport=1234) /
84 Raw('\xa5' * 100))
85
86 #
87 # Two sub-interfaces so we can test VLAN tag push/pop
88 #
89 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
90 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
91 sub_if_on_pg2.admin_up()
92 sub_if_on_pg3.admin_up()
93
94 #
95 # Put all the interfaces into a new bridge domain
96 #
Ole Troana5b2eec2019-03-11 19:23:25 +010097 self.vapi.sw_interface_set_l2_bridge(
98 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
99 self.vapi.sw_interface_set_l2_bridge(
100 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
101 self.vapi.sw_interface_set_l2_bridge(
102 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
103 self.vapi.sw_interface_set_l2_bridge(
104 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
105 self.vapi.sw_interface_set_l2_bridge(
106 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
107 port_type=L2_PORT_TYPE.BVI)
Neale Ranns6f631152017-10-03 08:20:21 -0700108
Ole Troane1ade682019-03-04 23:55:43 +0100109 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100110 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
111 push_dot1q=92)
Ole Troane1ade682019-03-04 23:55:43 +0100112 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100113 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
114 push_dot1q=93)
Neale Ranns6f631152017-10-03 08:20:21 -0700115
Neale Ranns6f631152017-10-03 08:20:21 -0700116 #
117 # Add routes to bridge the traffic via a tagged an nontagged interface
118 #
119 route_no_tag = VppIpRoute(
120 self, ip_non_tag_bridged, 32,
121 [VppRoutePath("0.0.0.0",
122 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800123 is_dvr=1)])
Neale Ranns6f631152017-10-03 08:20:21 -0700124 route_no_tag.add_vpp_config()
125
126 #
127 # Inject the packet that arrives and leaves on a non-tagged interface
128 # Since it's 'bridged' expect that the MAC headed is unchanged.
129 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400130 rx = self.send_and_expect(self.pg0, pkt_no_tag * NUM_PKTS, self.pg1)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800131 self.assert_same_mac_addr(pkt_no_tag, rx)
132 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700133
134 #
135 # Add routes to bridge the traffic via a tagged interface
136 #
Neale Ranns55d03782017-10-21 06:34:22 -0700137 route_with_tag = VppIpRoute(
Neale Ranns6f631152017-10-03 08:20:21 -0700138 self, ip_tag_bridged, 32,
139 [VppRoutePath("0.0.0.0",
140 sub_if_on_pg3.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800141 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700142 route_with_tag.add_vpp_config()
Neale Ranns6f631152017-10-03 08:20:21 -0700143
144 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800145 # Inject the packet that arrives non-tag and leaves on a tagged
146 # interface
Neale Ranns6f631152017-10-03 08:20:21 -0700147 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400148 rx = self.send_and_expect(self.pg0, pkt_tag * NUM_PKTS, self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700149 self.assert_same_mac_addr(pkt_tag, rx)
150 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700151
152 #
153 # Tag to tag
154 #
155 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
156 dst=self.loop0.local_mac) /
157 Dot1Q(vlan=92) /
158 IP(src=any_src_addr,
159 dst=ip_tag_bridged) /
160 UDP(sport=1234, dport=1234) /
161 Raw('\xa5' * 100))
162
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400163 rx = self.send_and_expect(self.pg2,
164 pkt_tag_to_tag * NUM_PKTS,
165 self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700166 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
167 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700168
169 #
170 # Tag to non-Tag
171 #
172 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
173 dst=self.loop0.local_mac) /
174 Dot1Q(vlan=92) /
175 IP(src=any_src_addr,
176 dst=ip_non_tag_bridged) /
177 UDP(sport=1234, dport=1234) /
178 Raw('\xa5' * 100))
179
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400180 rx = self.send_and_expect(self.pg2,
181 pkt_tag_to_non_tag * NUM_PKTS,
182 self.pg1)
Neale Ranns55d03782017-10-21 06:34:22 -0700183 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
184 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700185
Neale Ranns55d03782017-10-21 06:34:22 -0700186 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800187 # Add an output L3 ACL that will block the traffic
188 #
189 rule_1 = ({'is_permit': 0,
190 'is_ipv6': 0,
191 'proto': 17,
192 'srcport_or_icmptype_first': 1234,
193 'srcport_or_icmptype_last': 1234,
194 'src_ip_prefix_len': 32,
195 'src_ip_addr': inet_pton(AF_INET, any_src_addr),
196 'dstport_or_icmpcode_first': 1234,
197 'dstport_or_icmpcode_last': 1234,
198 'dst_ip_prefix_len': 32,
199 'dst_ip_addr': inet_pton(AF_INET, ip_non_tag_bridged)})
200 acl = self.vapi.acl_add_replace(acl_index=4294967295,
201 r=[rule_1])
202
203 #
204 # Apply the ACL on the output interface
205 #
206 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
207 0,
208 [acl.acl_index])
209
210 #
211 # Send packet's that should match the ACL and be dropped
212 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400213 rx = self.send_and_assert_no_replies(self.pg2,
214 pkt_tag_to_non_tag * NUM_PKTS)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800215
216 #
Neale Ranns55d03782017-10-21 06:34:22 -0700217 # cleanup
218 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800219 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
220 0, [])
221 self.vapi.acl_del(acl.acl_index)
222
Ole Troana5b2eec2019-03-11 19:23:25 +0100223 self.vapi.sw_interface_set_l2_bridge(
224 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
225 self.vapi.sw_interface_set_l2_bridge(
226 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
227 self.vapi.sw_interface_set_l2_bridge(
228 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
229 self.vapi.sw_interface_set_l2_bridge(
230 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
231 self.vapi.sw_interface_set_l2_bridge(
232 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1,
233 port_type=L2_PORT_TYPE.BVI, enable=0)
Neale Ranns55d03782017-10-21 06:34:22 -0700234
235 #
Neale Ranns81458422018-03-12 06:59:36 -0700236 # Do a FIB dump to make sure the paths are correctly reported as DVR
237 #
238 routes = self.vapi.ip_fib_dump()
239
240 for r in routes:
241 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
Neale Ranns81458422018-03-12 06:59:36 -0700242 self.assertEqual(r.path[0].sw_if_index,
243 sub_if_on_pg3.sw_if_index)
244 self.assertEqual(r.path[0].is_dvr, 1)
245 if (inet_pton(AF_INET, ip_non_tag_bridged) == r.address):
Neale Ranns81458422018-03-12 06:59:36 -0700246 self.assertEqual(r.path[0].sw_if_index,
247 self.pg1.sw_if_index)
248 self.assertEqual(r.path[0].is_dvr, 1)
249
250 #
Neale Ranns55d03782017-10-21 06:34:22 -0700251 # the explicit route delete is require so it happens before
252 # the sbu-interface delete. subinterface delete is required
253 # because that object type does not use the object registry
254 #
255 route_no_tag.remove_vpp_config()
256 route_with_tag.remove_vpp_config()
257 sub_if_on_pg3.remove_vpp_config()
258 sub_if_on_pg2.remove_vpp_config()
259
260 def test_l2_emulation(self):
261 """ L2 Emulation """
262
263 #
264 # non distinct L3 packets, in the tag/non-tag combos
265 #
266 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
267 dst=self.pg1.remote_mac) /
268 IP(src="2.2.2.2",
269 dst="1.1.1.1") /
270 UDP(sport=1234, dport=1234) /
271 Raw('\xa5' * 100))
272 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
273 dst=self.pg2.remote_mac) /
274 IP(src="2.2.2.2",
275 dst="1.1.1.2") /
276 UDP(sport=1234, dport=1234) /
277 Raw('\xa5' * 100))
278 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
279 dst=self.pg2.remote_mac) /
280 Dot1Q(vlan=93) /
281 IP(src="2.2.2.2",
282 dst="1.1.1.1") /
283 UDP(sport=1234, dport=1234) /
284 Raw('\xa5' * 100))
285 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
286 dst=self.pg2.remote_mac) /
287 Dot1Q(vlan=93) /
288 IP(src="2.2.2.2",
289 dst="1.1.1.2") /
290 UDP(sport=1234, dport=1234) /
291 Raw('\xa5' * 100))
292 pkt_bcast = (Ether(src=self.pg0.remote_mac,
293 dst="ff:ff:ff:ff:ff:ff") /
294 IP(src="2.2.2.2",
295 dst="255.255.255.255") /
296 UDP(sport=1234, dport=1234) /
297 Raw('\xa5' * 100))
298
299 #
300 # A couple of sub-interfaces for tags
301 #
302 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
303 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
304 sub_if_on_pg2.admin_up()
305 sub_if_on_pg3.admin_up()
306
307 #
308 # Put all the interfaces into a new bridge domain
309 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100310 self.vapi.sw_interface_set_l2_bridge(
311 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1)
312 self.vapi.sw_interface_set_l2_bridge(
313 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1)
314 self.vapi.sw_interface_set_l2_bridge(
315 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1)
316 self.vapi.sw_interface_set_l2_bridge(
317 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1)
Ole Troane1ade682019-03-04 23:55:43 +0100318 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100319 sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
320 push_dot1q=92)
Ole Troane1ade682019-03-04 23:55:43 +0100321 self.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100322 sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
323 push_dot1q=93)
Neale Ranns55d03782017-10-21 06:34:22 -0700324
325 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700326 # Disable UU flooding, learning and ARP termination. makes this test
Neale Ranns55d03782017-10-21 06:34:22 -0700327 # easier as unicast packets are dropped if not extracted.
328 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100329 self.vapi.bridge_flags(bd_id=1, is_set=0,
330 flags=(1 << 0) | (1 << 3) | (1 << 4))
Neale Ranns55d03782017-10-21 06:34:22 -0700331
332 #
333 # Add a DVR route to steer traffic at L3
334 #
335 route_1 = VppIpRoute(self, "1.1.1.1", 32,
336 [VppRoutePath("0.0.0.0",
337 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800338 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700339 route_2 = VppIpRoute(self, "1.1.1.2", 32,
340 [VppRoutePath("0.0.0.0",
341 sub_if_on_pg2.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800342 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700343 route_1.add_vpp_config()
344 route_2.add_vpp_config()
345
346 #
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300347 # packets are dropped because bridge does not flood unknown unicast
Neale Ranns55d03782017-10-21 06:34:22 -0700348 #
349 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
350
351 #
352 # Enable L3 extraction on pgs
353 #
Ole Troane1ade682019-03-04 23:55:43 +0100354 self.vapi.l2_emulation(self.pg0.sw_if_index)
355 self.vapi.l2_emulation(self.pg1.sw_if_index)
356 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index)
357 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index)
Neale Ranns55d03782017-10-21 06:34:22 -0700358
359 #
360 # now we expect the packet forward according to the DVR route
361 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400362 rx = self.send_and_expect(self.pg0, pkt_no_tag * NUM_PKTS, self.pg1)
Neale Ranns55d03782017-10-21 06:34:22 -0700363 self.assert_same_mac_addr(pkt_no_tag, rx)
364 self.assert_has_no_tag(rx)
365
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400366 rx = self.send_and_expect(self.pg0, pkt_to_tag * NUM_PKTS, self.pg2)
Neale Ranns55d03782017-10-21 06:34:22 -0700367 self.assert_same_mac_addr(pkt_to_tag, rx)
368 self.assert_has_vlan_tag(92, rx)
369
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400370 rx = self.send_and_expect(self.pg3, pkt_from_tag * NUM_PKTS, self.pg1)
Neale Ranns55d03782017-10-21 06:34:22 -0700371 self.assert_same_mac_addr(pkt_from_tag, rx)
372 self.assert_has_no_tag(rx)
373
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400374 rx = self.send_and_expect(self.pg3,
375 pkt_from_to_tag * NUM_PKTS,
376 self.pg2)
Neale Ranns55d03782017-10-21 06:34:22 -0700377 self.assert_same_mac_addr(pkt_from_tag, rx)
378 self.assert_has_vlan_tag(92, rx)
379
380 #
381 # but broadcast packets are still flooded
382 #
383 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
384
385 #
386 # cleanup
387 #
Ole Troane1ade682019-03-04 23:55:43 +0100388 self.vapi.l2_emulation(self.pg0.sw_if_index,
389 enable=0)
390 self.vapi.l2_emulation(self.pg1.sw_if_index,
391 enable=0)
392 self.vapi.l2_emulation(sub_if_on_pg2.sw_if_index,
393 enable=0)
394 self.vapi.l2_emulation(sub_if_on_pg3.sw_if_index,
395 enable=0)
Neale Ranns55d03782017-10-21 06:34:22 -0700396
Ole Troana5b2eec2019-03-11 19:23:25 +0100397 self.vapi.sw_interface_set_l2_bridge(
398 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0)
399 self.vapi.sw_interface_set_l2_bridge(
400 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0)
401 self.vapi.sw_interface_set_l2_bridge(
402 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0)
403 self.vapi.sw_interface_set_l2_bridge(
404 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0)
Neale Ranns55d03782017-10-21 06:34:22 -0700405
406 route_1.remove_vpp_config()
407 route_2.remove_vpp_config()
408 sub_if_on_pg3.remove_vpp_config()
409 sub_if_on_pg2.remove_vpp_config()
410
Neale Ranns6f631152017-10-03 08:20:21 -0700411
412if __name__ == '__main__':
413 unittest.main(testRunner=VppTestRunner)