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