L2 BD: introduce a BD interface on which to send UU packets
[vpp.git] / test / test_dvr.py
1 #!/usr/bin/env python
2 import unittest
3
4 from framework import VppTestCase, VppTestRunner
5 from vpp_sub_interface import VppDot1QSubint
6 from vpp_ip_route import VppIpRoute, VppRoutePath
7 from vpp_papi_provider import L2_VTR_OP, L2_PORT_TYPE
8
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
13
14
15 class TestDVR(VppTestCase):
16     """ Distributed Virtual Router """
17
18     def setUp(self):
19         super(TestDVR, self).setUp()
20
21         self.create_pg_interfaces(range(4))
22         self.create_loopback_interfaces(1)
23
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
36     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
52     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         #
87         self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1)
88         self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1)
89         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index, 1)
90         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index, 1)
91         self.vapi.sw_interface_set_l2_bridge(self.loop0.sw_if_index, 1,
92                                              port_type=L2_PORT_TYPE.BVI)
93
94         self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg2.sw_if_index,
95                                                   L2_VTR_OP.L2_POP_1,
96                                                   92)
97         self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
98                                                   L2_VTR_OP.L2_POP_1,
99                                                   93)
100
101         #
102         # Add routes to bridge the traffic via a tagged an nontagged interface
103         #
104         route_no_tag = VppIpRoute(
105             self, ip_non_tag_bridged, 32,
106             [VppRoutePath("0.0.0.0",
107                           self.pg1.sw_if_index,
108                           is_dvr=1)])
109         route_no_tag.add_vpp_config()
110
111         #
112         # Inject the packet that arrives and leaves on a non-tagged interface
113         # Since it's 'bridged' expect that the MAC headed is unchanged.
114         #
115         rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
116         self.assert_same_mac_addr(pkt_no_tag, rx)
117         self.assert_has_no_tag(rx)
118
119         #
120         # Add routes to bridge the traffic via a tagged interface
121         #
122         route_with_tag = VppIpRoute(
123             self, ip_tag_bridged, 32,
124             [VppRoutePath("0.0.0.0",
125                           sub_if_on_pg3.sw_if_index,
126                           is_dvr=1)])
127         route_with_tag.add_vpp_config()
128
129         #
130         # Inject the packet that arrives non-tag and leaves on a tagged
131         # interface
132         #
133         rx = self.send_and_expect(self.pg0, pkt_tag * 65, self.pg3)
134         self.assert_same_mac_addr(pkt_tag, rx)
135         self.assert_has_vlan_tag(93, rx)
136
137         #
138         # Tag to tag
139         #
140         pkt_tag_to_tag = (Ether(src=self.pg2.remote_mac,
141                                 dst=self.loop0.local_mac) /
142                           Dot1Q(vlan=92) /
143                           IP(src=any_src_addr,
144                              dst=ip_tag_bridged) /
145                           UDP(sport=1234, dport=1234) /
146                           Raw('\xa5' * 100))
147
148         rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * 65, self.pg3)
149         self.assert_same_mac_addr(pkt_tag_to_tag, rx)
150         self.assert_has_vlan_tag(93, rx)
151
152         #
153         # Tag to non-Tag
154         #
155         pkt_tag_to_non_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_non_tag_bridged) /
160                               UDP(sport=1234, dport=1234) /
161                               Raw('\xa5' * 100))
162
163         rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * 65, self.pg1)
164         self.assert_same_mac_addr(pkt_tag_to_tag, rx)
165         self.assert_has_no_tag(rx)
166
167         #
168         # Add an output L3 ACL that will block the traffic
169         #
170         rule_1 = ({'is_permit': 0,
171                    'is_ipv6': 0,
172                    'proto': 17,
173                    'srcport_or_icmptype_first': 1234,
174                    'srcport_or_icmptype_last': 1234,
175                    'src_ip_prefix_len': 32,
176                    'src_ip_addr': inet_pton(AF_INET, any_src_addr),
177                    'dstport_or_icmpcode_first': 1234,
178                    'dstport_or_icmpcode_last': 1234,
179                    'dst_ip_prefix_len': 32,
180                    'dst_ip_addr': inet_pton(AF_INET, ip_non_tag_bridged)})
181         acl = self.vapi.acl_add_replace(acl_index=4294967295,
182                                         r=[rule_1])
183
184         #
185         # Apply the ACL on the output interface
186         #
187         self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
188                                              0,
189                                              [acl.acl_index])
190
191         #
192         # Send packet's that should match the ACL and be dropped
193         #
194         rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * 65)
195
196         #
197         # cleanup
198         #
199         self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
200                                              0, [])
201         self.vapi.acl_del(acl.acl_index)
202
203         self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1,
204                                              enable=0)
205         self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1,
206                                              enable=0)
207         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
208                                              1, enable=0)
209         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
210                                              1, enable=0)
211         self.vapi.sw_interface_set_l2_bridge(self.loop0.sw_if_index,
212                                              1, port_type=L2_PORT_TYPE.BVI,
213                                              enable=0)
214
215         #
216         # Do a FIB dump to make sure the paths are correctly reported as DVR
217         #
218         routes = self.vapi.ip_fib_dump()
219
220         for r in routes:
221             if (inet_pton(AF_INET, ip_tag_bridged) == r.address):
222                 self.assertEqual(r.path[0].sw_if_index,
223                                  sub_if_on_pg3.sw_if_index)
224                 self.assertEqual(r.path[0].is_dvr, 1)
225             if (inet_pton(AF_INET, ip_non_tag_bridged) == r.address):
226                 self.assertEqual(r.path[0].sw_if_index,
227                                  self.pg1.sw_if_index)
228                 self.assertEqual(r.path[0].is_dvr, 1)
229
230         #
231         # the explicit route delete is require so it happens before
232         # the sbu-interface delete. subinterface delete is required
233         # because that object type does not use the object registry
234         #
235         route_no_tag.remove_vpp_config()
236         route_with_tag.remove_vpp_config()
237         sub_if_on_pg3.remove_vpp_config()
238         sub_if_on_pg2.remove_vpp_config()
239
240     def test_l2_emulation(self):
241         """ L2 Emulation """
242
243         #
244         # non distinct L3 packets, in the tag/non-tag combos
245         #
246         pkt_no_tag = (Ether(src=self.pg0.remote_mac,
247                             dst=self.pg1.remote_mac) /
248                       IP(src="2.2.2.2",
249                          dst="1.1.1.1") /
250                       UDP(sport=1234, dport=1234) /
251                       Raw('\xa5' * 100))
252         pkt_to_tag = (Ether(src=self.pg0.remote_mac,
253                             dst=self.pg2.remote_mac) /
254                       IP(src="2.2.2.2",
255                          dst="1.1.1.2") /
256                       UDP(sport=1234, dport=1234) /
257                       Raw('\xa5' * 100))
258         pkt_from_tag = (Ether(src=self.pg3.remote_mac,
259                               dst=self.pg2.remote_mac) /
260                         Dot1Q(vlan=93) /
261                         IP(src="2.2.2.2",
262                            dst="1.1.1.1") /
263                         UDP(sport=1234, dport=1234) /
264                         Raw('\xa5' * 100))
265         pkt_from_to_tag = (Ether(src=self.pg3.remote_mac,
266                                  dst=self.pg2.remote_mac) /
267                            Dot1Q(vlan=93) /
268                            IP(src="2.2.2.2",
269                               dst="1.1.1.2") /
270                            UDP(sport=1234, dport=1234) /
271                            Raw('\xa5' * 100))
272         pkt_bcast = (Ether(src=self.pg0.remote_mac,
273                            dst="ff:ff:ff:ff:ff:ff") /
274                      IP(src="2.2.2.2",
275                         dst="255.255.255.255") /
276                      UDP(sport=1234, dport=1234) /
277                      Raw('\xa5' * 100))
278
279         #
280         # A couple of sub-interfaces for tags
281         #
282         sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
283         sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
284         sub_if_on_pg2.admin_up()
285         sub_if_on_pg3.admin_up()
286
287         #
288         # Put all the interfaces into a new bridge domain
289         #
290         self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index, 1)
291         self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index, 1)
292         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index, 1)
293         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index, 1)
294         self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg2.sw_if_index,
295                                                   L2_VTR_OP.L2_POP_1,
296                                                   92)
297         self.vapi.sw_interface_set_l2_tag_rewrite(sub_if_on_pg3.sw_if_index,
298                                                   L2_VTR_OP.L2_POP_1,
299                                                   93)
300
301         #
302         # Disable UU flooding, learning and ARP terminaation. makes this test
303         # easier as unicast packets are dropped if not extracted.
304         #
305         self.vapi.bridge_flags(1, 0, (1 << 0) | (1 << 3) | (1 << 4))
306
307         #
308         # Add a DVR route to steer traffic at L3
309         #
310         route_1 = VppIpRoute(self, "1.1.1.1", 32,
311                              [VppRoutePath("0.0.0.0",
312                                            self.pg1.sw_if_index,
313                                            is_dvr=1)])
314         route_2 = VppIpRoute(self, "1.1.1.2", 32,
315                              [VppRoutePath("0.0.0.0",
316                                            sub_if_on_pg2.sw_if_index,
317                                            is_dvr=1)])
318         route_1.add_vpp_config()
319         route_2.add_vpp_config()
320
321         #
322         # packets are dropped because bridge does not flood unknown unicast
323         #
324         self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
325
326         #
327         # Enable L3 extraction on pgs
328         #
329         self.vapi.sw_interface_set_l2_emulation(self.pg0.sw_if_index)
330         self.vapi.sw_interface_set_l2_emulation(self.pg1.sw_if_index)
331         self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg2.sw_if_index)
332         self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg3.sw_if_index)
333
334         #
335         # now we expect the packet forward according to the DVR route
336         #
337         rx = self.send_and_expect(self.pg0, pkt_no_tag * 65, self.pg1)
338         self.assert_same_mac_addr(pkt_no_tag, rx)
339         self.assert_has_no_tag(rx)
340
341         rx = self.send_and_expect(self.pg0, pkt_to_tag * 65, self.pg2)
342         self.assert_same_mac_addr(pkt_to_tag, rx)
343         self.assert_has_vlan_tag(92, rx)
344
345         rx = self.send_and_expect(self.pg3, pkt_from_tag * 65, self.pg1)
346         self.assert_same_mac_addr(pkt_from_tag, rx)
347         self.assert_has_no_tag(rx)
348
349         rx = self.send_and_expect(self.pg3, pkt_from_to_tag * 65, self.pg2)
350         self.assert_same_mac_addr(pkt_from_tag, rx)
351         self.assert_has_vlan_tag(92, rx)
352
353         #
354         # but broadcast packets are still flooded
355         #
356         self.send_and_expect(self.pg0, pkt_bcast * 33, self.pg2)
357
358         #
359         # cleanup
360         #
361         self.vapi.sw_interface_set_l2_emulation(self.pg0.sw_if_index,
362                                                 enable=0)
363         self.vapi.sw_interface_set_l2_emulation(self.pg1.sw_if_index,
364                                                 enable=0)
365         self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg2.sw_if_index,
366                                                 enable=0)
367         self.vapi.sw_interface_set_l2_emulation(sub_if_on_pg3.sw_if_index,
368                                                 enable=0)
369
370         self.vapi.sw_interface_set_l2_bridge(self.pg0.sw_if_index,
371                                              1, enable=0)
372         self.vapi.sw_interface_set_l2_bridge(self.pg1.sw_if_index,
373                                              1, enable=0)
374         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg2.sw_if_index,
375                                              1, enable=0)
376         self.vapi.sw_interface_set_l2_bridge(sub_if_on_pg3.sw_if_index,
377                                              1, enable=0)
378
379         route_1.remove_vpp_config()
380         route_2.remove_vpp_config()
381         sub_if_on_pg3.remove_vpp_config()
382         sub_if_on_pg2.remove_vpp_config()
383
384
385 if __name__ == '__main__':
386     unittest.main(testRunner=VppTestRunner)