blob: c0ce32c134cf3e33f9f58960754e57e238f69bf9 [file] [log] [blame]
Neale Ranns6f631152017-10-03 08:20:21 -07001#!/usr/bin/env python
2import random
3import socket
4import unittest
5
6from framework import VppTestCase, VppTestRunner
7from vpp_sub_interface import VppSubInterface, VppDot1QSubint
Neale Ranns55d03782017-10-21 06:34:22 -07008from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto, VppIpMRoute, \
9 VppMRoutePath, MRouteEntryFlags, MRouteItfFlags
Neale Ranns6f631152017-10-03 08:20:21 -070010from vpp_papi_provider import L2_VTR_OP
11
12from scapy.packet import Raw
13from scapy.layers.l2 import Ether, Dot1Q, ARP
14from scapy.layers.inet import IP, UDP
15from util import ppp
Neale Rannsf068c3e2018-01-03 04:18:48 -080016from socket import AF_INET, inet_pton
Neale Ranns6f631152017-10-03 08:20:21 -070017
18
19class TestDVR(VppTestCase):
Neale Ranns62fe07c2017-10-31 12:28:22 -070020 """ Distributed Virtual Router """
Neale Ranns6f631152017-10-03 08:20:21 -070021
22 def setUp(self):
23 super(TestDVR, self).setUp()
24
25 self.create_pg_interfaces(range(4))
26 self.create_loopback_interfaces(range(1))
27
28 for i in self.pg_interfaces:
29 i.admin_up()
30
31 self.loop0.config_ip4()
32
33 def tearDown(self):
34 for i in self.pg_interfaces:
35 i.admin_down()
36 self.loop0.unconfig_ip4()
37
38 super(TestDVR, self).tearDown()
39
Neale Ranns55d03782017-10-21 06:34:22 -070040 def assert_same_mac_addr(self, tx, rx):
41 t_eth = tx[Ether]
42 for p in rx:
43 r_eth = p[Ether]
44 self.assertEqual(t_eth.src, r_eth.src)
45 self.assertEqual(t_eth.dst, r_eth.dst)
46
47 def assert_has_vlan_tag(self, tag, rx):
48 for p in rx:
49 r_1q = p[Dot1Q]
50 self.assertEqual(tag, r_1q.vlan)
51
52 def assert_has_no_tag(self, rx):
53 for p in rx:
54 self.assertFalse(p.haslayer(Dot1Q))
55
Neale Ranns6f631152017-10-03 08:20:21 -070056 def test_dvr(self):
57 """ Distributed Virtual Router """
58
59 #
60 # A packet destined to an IP address that is L2 bridged via
61 # a non-tag interface
62 #
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"
66
67 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
68 dst=self.loop0.local_mac) /
69 IP(src=any_src_addr,
70 dst=ip_non_tag_bridged) /
71 UDP(sport=1234, dport=1234) /
72 Raw('\xa5' * 100))
73 pkt_tag = (Ether(src=self.pg0.remote_mac,
74 dst=self.loop0.local_mac) /
75 IP(src=any_src_addr,
76 dst=ip_tag_bridged) /
77 UDP(sport=1234, dport=1234) /
78 Raw('\xa5' * 100))
79
80 #
81 # Two sub-interfaces so we can test VLAN tag push/pop
82 #
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()
87
88 #
89 # Put all the interfaces into a new bridge domain
90 #
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)
96
97 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg2.sw_if_index,
98 L2_VTR_OP.L2_POP_1,
99 92)
100 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
101 L2_VTR_OP.L2_POP_1,
102 93)
103
Neale Ranns6f631152017-10-03 08:20:21 -0700104 #
105 # Add routes to bridge the traffic via a tagged an nontagged interface
106 #
107 route_no_tag = VppIpRoute(
108 self, ip_non_tag_bridged, 32,
109 [VppRoutePath("0.0.0.0",
110 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800111 is_dvr=1)])
Neale Ranns6f631152017-10-03 08:20:21 -0700112 route_no_tag.add_vpp_config()
113
114 #
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.
117 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800118 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700121
122 #
123 # Add routes to bridge the traffic via a tagged interface
124 #
Neale Ranns55d03782017-10-21 06:34:22 -0700125 route_with_tag = VppIpRoute(
Neale Ranns6f631152017-10-03 08:20:21 -0700126 self, ip_tag_bridged, 32,
127 [VppRoutePath("0.0.0.0",
128 sub_if_on_pg3.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800129 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700130 route_with_tag.add_vpp_config()
Neale Ranns6f631152017-10-03 08:20:21 -0700131
132 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800133 # Inject the packet that arrives non-tag and leaves on a tagged
134 # interface
Neale Ranns6f631152017-10-03 08:20:21 -0700135 #
Neale Ranns55d03782017-10-21 06:34:22 -0700136 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700139
140 #
141 # Tag to tag
142 #
143 pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
144 dst=self.loop0.local_mac) /
145 Dot1Q(vlan=92) /
146 IP(src=any_src_addr,
147 dst=ip_tag_bridged) /
148 UDP(sport=1234, dport=1234) /
149 Raw('\xa5' * 100))
150
Neale Ranns55d03782017-10-21 06:34:22 -0700151 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700154
155 #
156 # Tag to non-Tag
157 #
158 pkt_tag_to_non_tag = (Ether(src=self.pg2.remote_mac,
159 dst=self.loop0.local_mac) /
160 Dot1Q(vlan=92) /
161 IP(src=any_src_addr,
162 dst=ip_non_tag_bridged) /
163 UDP(sport=1234, dport=1234) /
164 Raw('\xa5' * 100))
165
Neale Ranns55d03782017-10-21 06:34:22 -0700166 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)
Neale Ranns6f631152017-10-03 08:20:21 -0700169
Neale Ranns55d03782017-10-21 06:34:22 -0700170 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800171 # Add an output L3 ACL that will block the traffic
172 #
173 rule_1 = ({'is_permit': 0,
174 'is_ipv6': 0,
175 'proto': 17,
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,
185 r=[rule_1])
186
187 #
188 # Apply the ACL on the output interface
189 #
190 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
191 0,
192 [acl.acl_index])
193
194 #
195 # Send packet's that should match the ACL and be dropped
196 #
197 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
198
199 #
Neale Ranns55d03782017-10-21 06:34:22 -0700200 # cleanup
201 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800202 self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
203 0, [])
204 self.vapi.acl_del(acl.acl_index)
205
Neale Ranns55d03782017-10-21 06:34:22 -0700206 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1,
207 enable=0)
208 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1,
209 enable=0)
210 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
211 1, enable=0)
212 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
213 1, enable=0)
214 self.vapi.sw_interface_set_l2_bridge(self.loop0.sw_if_index,
215 1, bvi=1, enable=0)
216
217 #
Neale Ranns81458422018-03-12 06:59:36 -0700218 # Do a FIB dump to make sure the paths are correctly reported as DVR
219 #
220 routes = self.vapi.ip_fib_dump()
221
222 for r in routes:
223 if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
Neale Ranns81458422018-03-12 06:59:36 -0700224 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):
Neale Ranns81458422018-03-12 06:59:36 -0700228 self.assertEqual(r.path[0].sw_if_index,
229 self.pg1.sw_if_index)
230 self.assertEqual(r.path[0].is_dvr, 1)
231
232 #
Neale Ranns55d03782017-10-21 06:34:22 -0700233 # 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
236 #
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()
241
242 def test_l2_emulation(self):
243 """ L2 Emulation """
244
245 #
246 # non distinct L3 packets, in the tag/non-tag combos
247 #
248 pkt_no_tag = (Ether(src=self.pg0.remote_mac,
249 dst=self.pg1.remote_mac) /
250 IP(src="2.2.2.2",
251 dst="1.1.1.1") /
252 UDP(sport=1234, dport=1234) /
253 Raw('\xa5' * 100))
254 pkt_to_tag = (Ether(src=self.pg0.remote_mac,
255 dst=self.pg2.remote_mac) /
256 IP(src="2.2.2.2",
257 dst="1.1.1.2") /
258 UDP(sport=1234, dport=1234) /
259 Raw('\xa5' * 100))
260 pkt_from_tag = (Ether(src=self.pg3.remote_mac,
261 dst=self.pg2.remote_mac) /
262 Dot1Q(vlan=93) /
263 IP(src="2.2.2.2",
264 dst="1.1.1.1") /
265 UDP(sport=1234, dport=1234) /
266 Raw('\xa5' * 100))
267 pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
268 dst=self.pg2.remote_mac) /
269 Dot1Q(vlan=93) /
270 IP(src="2.2.2.2",
271 dst="1.1.1.2") /
272 UDP(sport=1234, dport=1234) /
273 Raw('\xa5' * 100))
274 pkt_bcast = (Ether(src=self.pg0.remote_mac,
275 dst="ff:ff:ff:ff:ff:ff") /
276 IP(src="2.2.2.2",
277 dst="255.255.255.255") /
278 UDP(sport=1234, dport=1234) /
279 Raw('\xa5' * 100))
280
281 #
282 # A couple of sub-interfaces for tags
283 #
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()
288
289 #
290 # Put all the interfaces into a new bridge domain
291 #
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,
297 L2_VTR_OP.L2_POP_1,
298 92)
299 self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
300 L2_VTR_OP.L2_POP_1,
301 93)
302
303 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800304 # Disable UU flooding, learning and ARP terminaation. makes this test
Neale Ranns55d03782017-10-21 06:34:22 -0700305 # easier as unicast packets are dropped if not extracted.
306 #
307 self.vapi.bridge_flags(1, 0, (1 << 0) | (1 << 3) | (1 << 4))
308
309 #
310 # Add a DVR route to steer traffic at L3
311 #
312 route_1 = VppIpRoute(self, "1.1.1.1", 32,
313 [VppRoutePath("0.0.0.0",
314 self.pg1.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800315 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700316 route_2 = VppIpRoute(self, "1.1.1.2", 32,
317 [VppRoutePath("0.0.0.0",
318 sub_if_on_pg2.sw_if_index,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800319 is_dvr=1)])
Neale Ranns55d03782017-10-21 06:34:22 -0700320 route_1.add_vpp_config()
321 route_2.add_vpp_config()
322
323 #
324 # packets are dropped because bridge does not flood unkown unicast
325 #
326 self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
327
328 #
329 # Enable L3 extraction on pgs
330 #
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)
335
336 #
337 # now we expect the packet forward according to the DVR route
338 #
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)
342
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)
346
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)
350
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)
354
355 #
356 # but broadcast packets are still flooded
357 #
358 self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
359
360 #
361 # cleanup
362 #
363 self.vapi.sw_interface_set_l2_emulation(self.pg0.sw_if_index,
364 enable=0)
365 self.vapi.sw_interface_set_l2_emulation(self.pg1.sw_if_index,
366 enable=0)
367 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg2.sw_if_index,
368 enable=0)
369 self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg3.sw_if_index,
370 enable=0)
371
372 self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index,
373 1, enable=0)
374 self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index,
375 1, enable=0)
376 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
377 1, enable=0)
378 self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
379 1, enable=0)
380
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()
385
Neale Ranns6f631152017-10-03 08:20:21 -0700386
387if __name__ == '__main__':
388 unittest.main(testRunner=VppTestRunner)