Typos. A bunch of typos I've been collecting.
[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(
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)
98
99         self.vapi.l2_interface_vlan_tag_rewrite(
100             sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
101             push_dot1q=92)
102         self.vapi.l2_interface_vlan_tag_rewrite(
103             sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
104             push_dot1q=93)
105
106         #
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,
113                           is_dvr=1)])
114         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         #
120         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)
123
124         #
125         # Add routes to bridge the traffic via a tagged interface
126         #
127         route_with_tag = VppIpRoute(
128             self, ip_tag_bridged, 32,
129             [VppRoutePath("0.0.0.0",
130                           sub_if_on_pg3.sw_if_index,
131                           is_dvr=1)])
132         route_with_tag.add_vpp_config()
133
134         #
135         # Inject the packet that arrives non-tag and leaves on a tagged
136         # interface
137         #
138         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)
141
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
153         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)
156
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
168         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)
171
172         #
173         # 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         #
202         # cleanup
203         #
204         self.vapi.acl_interface_set_acl_list(self.pg1.sw_if_index,
205                                              0, [])
206         self.vapi.acl_del(acl.acl_index)
207
208         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)
219
220         #
221         # 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):
227                 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):
231                 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         #
236         # 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         #
295         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)
303         self.vapi.l2_interface_vlan_tag_rewrite(
304             sw_if_index=sub_if_on_pg2.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
305             push_dot1q=92)
306         self.vapi.l2_interface_vlan_tag_rewrite(
307             sw_if_index=sub_if_on_pg3.sw_if_index, vtr_op=L2_VTR_OP.L2_POP_1,
308             push_dot1q=93)
309
310         #
311         # Disable UU flooding, learning and ARP termination. makes this test
312         # easier as unicast packets are dropped if not extracted.
313         #
314         self.vapi.bridge_flags(bd_id=1, is_set=0,
315                                flags=(1 << 0) | (1 << 3) | (1 << 4))
316
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,
323                                            is_dvr=1)])
324         route_2 = VppIpRoute(self, "1.1.1.2", 32,
325                              [VppRoutePath("0.0.0.0",
326                                            sub_if_on_pg2.sw_if_index,
327                                            is_dvr=1)])
328         route_1.add_vpp_config()
329         route_2.add_vpp_config()
330
331         #
332         # packets are dropped because bridge does not flood unknown unicast
333         #
334         self.send_and_assert_no_replies(self.pg0, pkt_no_tag)
335
336         #
337         # Enable L3 extraction on pgs
338         #
339         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)
343
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         #
371         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)
379
380         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)
388
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
394
395 if __name__ == '__main__':
396     unittest.main(testRunner=VppTestRunner)