blob: dcc17f040eba4989eb8a3dadc95bf6f003f67d69 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Neale Ranns6f631152017-10-03 08:20:21 -07002import unittest
3
Dave Wallace8800f732023-08-31 00:47:44 -04004from framework import VppTestCase
5from asfframework import VppTestRunner
Neale Ranns097fa662018-05-01 05:17:55 -07006from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathType
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -07007from vpp_l2 import L2_PORT_TYPE
8from vpp_sub_interface import L2_VTR_OP, VppDot1QSubint
Jakub Grajciar2f8cd912020-03-27 06:55:06 +01009from vpp_acl import AclRule, VppAcl, VppAclInterface
Neale Ranns6f631152017-10-03 08:20:21 -070010
11from scapy.packet import Raw
Klement Sekerab9ef2732018-06-24 22:49:33 +020012from scapy.layers.l2 import Ether, Dot1Q
Neale Ranns6f631152017-10-03 08:20:21 -070013from scapy.layers.inet import IP, UDP
Dave Wallace8800f732023-08-31 00:47:44 -040014from socket import AF_INET
Jakub Grajciar2f8cd912020-03-27 06:55:06 +010015from ipaddress import IPv4Network
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000016from config import config
Neale Ranns6f631152017-10-03 08:20:21 -070017
Paul Vinciguerra4271c972019-05-14 13:25:49 -040018NUM_PKTS = 67
19
Neale Ranns6f631152017-10-03 08:20:21 -070020
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000021@unittest.skipIf("acl" in config.excluded_plugins, "Exclude tests requiring ACL plugin")
Neale Ranns6f631152017-10-03 08:20:21 -070022class TestDVR(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020023 """Distributed Virtual Router"""
Neale Ranns6f631152017-10-03 08:20:21 -070024
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070025 @classmethod
26 def setUpClass(cls):
27 super(TestDVR, cls).setUpClass()
28
29 @classmethod
30 def tearDownClass(cls):
31 super(TestDVR, cls).tearDownClass()
32
Neale Ranns6f631152017-10-03 08:20:21 -070033 def setUp(self):
34 super(TestDVR, self).setUp()
35
36 self.create_pg_interfaces(range(4))
Klement Sekerab9ef2732018-06-24 22:49:33 +020037 self.create_loopback_interfaces(1)
Neale Ranns6f631152017-10-03 08:20:21 -070038
39 for i in self.pg_interfaces:
40 i.admin_up()
41
42 self.loop0.config_ip4()
43
44 def tearDown(self):
45 for i in self.pg_interfaces:
46 i.admin_down()
47 self.loop0.unconfig_ip4()
48
49 super(TestDVR, self).tearDown()
50
Neale Ranns55d03782017-10-21 06:34:22 -070051 def assert_same_mac_addr(self, tx, rx):
52 t_eth = tx[Ether]
53 for p in rx:
54 r_eth = p[Ether]
55 self.assertEqual(t_eth.src, r_eth.src)
56 self.assertEqual(t_eth.dst, r_eth.dst)
57
58 def assert_has_vlan_tag(self, tag, rx):
59 for p in rx:
60 r_1q = p[Dot1Q]
61 self.assertEqual(tag, r_1q.vlan)
62
63 def assert_has_no_tag(self, rx):
64 for p in rx:
65 self.assertFalse(p.haslayer(Dot1Q))
66
Neale Ranns6f631152017-10-03 08:20:21 -070067 def test_dvr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 """Distributed Virtual Router"""
Neale Ranns6f631152017-10-03 08:20:21 -070069
70 #
71 # A packet destined to an IP address that is L2 bridged via
72 # a non-tag interface
73 #
74 ip_non_tag_bridged = "10.10.10.10"
75 ip_tag_bridged = "10.10.10.11"
76 any_src_addr = "1.1.1.1"
77
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 pkt_no_tag = (
79 Ether(src=self.pg0.remote_mac, dst=self.loop0.local_mac)
80 / IP(src=any_src_addr, dst=ip_non_tag_bridged)
81 / UDP(sport=1234, dport=1234)
82 / Raw(b"\xa5" * 100)
83 )
84 pkt_tag = (
85 Ether(src=self.pg0.remote_mac, dst=self.loop0.local_mac)
86 / IP(src=any_src_addr, dst=ip_tag_bridged)
87 / UDP(sport=1234, dport=1234)
88 / Raw(b"\xa5" * 100)
89 )
Neale Ranns6f631152017-10-03 08:20:21 -070090
91 #
92 # Two sub-interfaces so we can test VLAN tag push/pop
93 #
94 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
95 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
96 sub_if_on_pg2.admin_up()
97 sub_if_on_pg3.admin_up()
98
99 #
100 # Put all the interfaces into a new bridge domain
101 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100102 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1
104 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100105 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1
107 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100108 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200109 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1
110 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100111 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1
113 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100114 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1, port_type=L2_PORT_TYPE.BVI
116 )
Neale Ranns6f631152017-10-03 08:20:21 -0700117
Ole Troane1ade682019-03-04 23:55:43 +0100118 self.vapi.l2_interface_vlan_tag_rewrite(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 sw_if_index=sub_if_on_pg2.sw_if_index,
120 vtr_op=L2_VTR_OP.L2_POP_1,
121 push_dot1q=92,
122 )
Ole Troane1ade682019-03-04 23:55:43 +0100123 self.vapi.l2_interface_vlan_tag_rewrite(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 sw_if_index=sub_if_on_pg3.sw_if_index,
125 vtr_op=L2_VTR_OP.L2_POP_1,
126 push_dot1q=93,
127 )
Neale Ranns6f631152017-10-03 08:20:21 -0700128
Neale Ranns6f631152017-10-03 08:20:21 -0700129 #
130 # Add routes to bridge the traffic via a tagged an nontagged interface
131 #
132 route_no_tag = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 self,
134 ip_non_tag_bridged,
135 32,
136 [
137 VppRoutePath(
138 "0.0.0.0", self.pg1.sw_if_index, type=FibPathType.FIB_PATH_TYPE_DVR
139 )
140 ],
141 )
Neale Ranns6f631152017-10-03 08:20:21 -0700142 route_no_tag.add_vpp_config()
143
144 #
145 # Inject the packet that arrives and leaves on a non-tagged interface
146 # Since it's 'bridged' expect that the MAC headed is unchanged.
147 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400148 rx = self.send_and_expect(self.pg0, pkt_no_tag * NUM_PKTS, self.pg1)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800149 self.assert_same_mac_addr(pkt_no_tag, rx)
150 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700151
152 #
153 # Add routes to bridge the traffic via a tagged interface
154 #
Neale Ranns55d03782017-10-21 06:34:22 -0700155 route_with_tag = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200156 self,
157 ip_tag_bridged,
158 32,
159 [
160 VppRoutePath(
161 "0.0.0.0",
162 sub_if_on_pg3.sw_if_index,
163 type=FibPathType.FIB_PATH_TYPE_DVR,
164 )
165 ],
166 )
Neale Ranns55d03782017-10-21 06:34:22 -0700167 route_with_tag.add_vpp_config()
Neale Ranns6f631152017-10-03 08:20:21 -0700168
169 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800170 # Inject the packet that arrives non-tag and leaves on a tagged
171 # interface
Neale Ranns6f631152017-10-03 08:20:21 -0700172 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400173 rx = self.send_and_expect(self.pg0, pkt_tag * NUM_PKTS, self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700174 self.assert_same_mac_addr(pkt_tag, rx)
175 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700176
177 #
178 # Tag to tag
179 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200180 pkt_tag_to_tag = (
181 Ether(src=self.pg2.remote_mac, dst=self.loop0.local_mac)
182 / Dot1Q(vlan=92)
183 / IP(src=any_src_addr, dst=ip_tag_bridged)
184 / UDP(sport=1234, dport=1234)
185 / Raw(b"\xa5" * 100)
186 )
Neale Ranns6f631152017-10-03 08:20:21 -0700187
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200188 rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * NUM_PKTS, self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700189 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
190 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700191
192 #
193 # Tag to non-Tag
194 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 pkt_tag_to_non_tag = (
196 Ether(src=self.pg2.remote_mac, dst=self.loop0.local_mac)
197 / Dot1Q(vlan=92)
198 / IP(src=any_src_addr, dst=ip_non_tag_bridged)
199 / UDP(sport=1234, dport=1234)
200 / Raw(b"\xa5" * 100)
201 )
Neale Ranns6f631152017-10-03 08:20:21 -0700202
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200203 rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * NUM_PKTS, self.pg1)
Neale Ranns55d03782017-10-21 06:34:22 -0700204 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
205 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700206
Neale Ranns55d03782017-10-21 06:34:22 -0700207 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800208 # Add an output L3 ACL that will block the traffic
209 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200210 rule_1 = AclRule(
211 is_permit=0,
212 proto=17,
213 ports=1234,
214 src_prefix=IPv4Network((any_src_addr, 32)),
215 dst_prefix=IPv4Network((ip_non_tag_bridged, 32)),
216 )
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100217 acl = VppAcl(self, rules=[rule_1])
218 acl.add_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800219
220 #
221 # Apply the ACL on the output interface
222 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200223 acl_if1 = VppAclInterface(
224 self, sw_if_index=self.pg1.sw_if_index, n_input=0, acls=[acl]
225 )
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100226 acl_if1.add_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800227
228 #
229 # Send packet's that should match the ACL and be dropped
230 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200231 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * NUM_PKTS)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800232
233 #
Neale Ranns55d03782017-10-21 06:34:22 -0700234 # cleanup
235 #
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100236 acl_if1.remove_vpp_config()
237 acl.remove_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800238
Ole Troana5b2eec2019-03-11 19:23:25 +0100239 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200240 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0
241 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100242 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200243 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0
244 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100245 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200246 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0
247 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100248 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200249 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0
250 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100251 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200252 rx_sw_if_index=self.loop0.sw_if_index,
253 bd_id=1,
254 port_type=L2_PORT_TYPE.BVI,
255 enable=0,
256 )
Neale Ranns55d03782017-10-21 06:34:22 -0700257
258 #
Neale Ranns81458422018-03-12 06:59:36 -0700259 # Do a FIB dump to make sure the paths are correctly reported as DVR
260 #
Neale Ranns097fa662018-05-01 05:17:55 -0700261 routes = self.vapi.ip_route_dump(0)
Neale Ranns81458422018-03-12 06:59:36 -0700262
263 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200264 if ip_tag_bridged == str(r.route.prefix.network_address):
265 self.assertEqual(
266 r.route.paths[0].sw_if_index, sub_if_on_pg3.sw_if_index
267 )
268 self.assertEqual(r.route.paths[0].type, FibPathType.FIB_PATH_TYPE_DVR)
269 if ip_non_tag_bridged == str(r.route.prefix.network_address):
270 self.assertEqual(r.route.paths[0].sw_if_index, self.pg1.sw_if_index)
271 self.assertEqual(r.route.paths[0].type, FibPathType.FIB_PATH_TYPE_DVR)
Neale Ranns81458422018-03-12 06:59:36 -0700272
273 #
Neale Ranns55d03782017-10-21 06:34:22 -0700274 # the explicit route delete is require so it happens before
275 # the sbu-interface delete. subinterface delete is required
276 # because that object type does not use the object registry
277 #
278 route_no_tag.remove_vpp_config()
279 route_with_tag.remove_vpp_config()
280 sub_if_on_pg3.remove_vpp_config()
281 sub_if_on_pg2.remove_vpp_config()
282
Neale Ranns6f631152017-10-03 08:20:21 -0700283
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200284if __name__ == "__main__":
Neale Ranns6f631152017-10-03 08:20:21 -0700285 unittest.main(testRunner=VppTestRunner)