blob: e616408e8c7836c5b5ec3f73b9408e8a96def9f8 [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
Neale Ranns6f631152017-10-03 08:20:21 -070016
Paul Vinciguerra4271c972019-05-14 13:25:49 -040017NUM_PKTS = 67
18
Neale Ranns6f631152017-10-03 08:20:21 -070019
20class TestDVR(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020021 """Distributed Virtual Router"""
Neale Ranns6f631152017-10-03 08:20:21 -070022
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070023 @classmethod
24 def setUpClass(cls):
25 super(TestDVR, cls).setUpClass()
26
27 @classmethod
28 def tearDownClass(cls):
29 super(TestDVR, cls).tearDownClass()
30
Neale Ranns6f631152017-10-03 08:20:21 -070031 def setUp(self):
32 super(TestDVR, self).setUp()
33
34 self.create_pg_interfaces(range(4))
Klement Sekerab9ef2732018-06-24 22:49:33 +020035 self.create_loopback_interfaces(1)
Neale Ranns6f631152017-10-03 08:20:21 -070036
37 for i in self.pg_interfaces:
38 i.admin_up()
39
40 self.loop0.config_ip4()
41
42 def tearDown(self):
43 for i in self.pg_interfaces:
44 i.admin_down()
45 self.loop0.unconfig_ip4()
46
47 super(TestDVR, self).tearDown()
48
Neale Ranns55d03782017-10-21 06:34:22 -070049 def assert_same_mac_addr(self, tx, rx):
50 t_eth = tx[Ether]
51 for p in rx:
52 r_eth = p[Ether]
53 self.assertEqual(t_eth.src, r_eth.src)
54 self.assertEqual(t_eth.dst, r_eth.dst)
55
56 def assert_has_vlan_tag(self, tag, rx):
57 for p in rx:
58 r_1q = p[Dot1Q]
59 self.assertEqual(tag, r_1q.vlan)
60
61 def assert_has_no_tag(self, rx):
62 for p in rx:
63 self.assertFalse(p.haslayer(Dot1Q))
64
Neale Ranns6f631152017-10-03 08:20:21 -070065 def test_dvr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 """Distributed Virtual Router"""
Neale Ranns6f631152017-10-03 08:20:21 -070067
68 #
69 # A packet destined to an IP address that is L2 bridged via
70 # a non-tag interface
71 #
72 ip_non_tag_bridged = "10.10.10.10"
73 ip_tag_bridged = "10.10.10.11"
74 any_src_addr = "1.1.1.1"
75
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 pkt_no_tag = (
77 Ether(src=self.pg0.remote_mac, dst=self.loop0.local_mac)
78 / IP(src=any_src_addr, dst=ip_non_tag_bridged)
79 / UDP(sport=1234, dport=1234)
80 / Raw(b"\xa5" * 100)
81 )
82 pkt_tag = (
83 Ether(src=self.pg0.remote_mac, dst=self.loop0.local_mac)
84 / IP(src=any_src_addr, dst=ip_tag_bridged)
85 / UDP(sport=1234, dport=1234)
86 / Raw(b"\xa5" * 100)
87 )
Neale Ranns6f631152017-10-03 08:20:21 -070088
89 #
90 # Two sub-interfaces so we can test VLAN tag push/pop
91 #
92 sub_if_on_pg2 = VppDot1QSubint(self, self.pg2, 92)
93 sub_if_on_pg3 = VppDot1QSubint(self, self.pg3, 93)
94 sub_if_on_pg2.admin_up()
95 sub_if_on_pg3.admin_up()
96
97 #
98 # Put all the interfaces into a new bridge domain
99 #
Ole Troana5b2eec2019-03-11 19:23:25 +0100100 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1
102 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100103 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1
105 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100106 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1
108 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100109 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1
111 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100112 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 rx_sw_if_index=self.loop0.sw_if_index, bd_id=1, port_type=L2_PORT_TYPE.BVI
114 )
Neale Ranns6f631152017-10-03 08:20:21 -0700115
Ole Troane1ade682019-03-04 23:55:43 +0100116 self.vapi.l2_interface_vlan_tag_rewrite(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200117 sw_if_index=sub_if_on_pg2.sw_if_index,
118 vtr_op=L2_VTR_OP.L2_POP_1,
119 push_dot1q=92,
120 )
Ole Troane1ade682019-03-04 23:55:43 +0100121 self.vapi.l2_interface_vlan_tag_rewrite(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 sw_if_index=sub_if_on_pg3.sw_if_index,
123 vtr_op=L2_VTR_OP.L2_POP_1,
124 push_dot1q=93,
125 )
Neale Ranns6f631152017-10-03 08:20:21 -0700126
Neale Ranns6f631152017-10-03 08:20:21 -0700127 #
128 # Add routes to bridge the traffic via a tagged an nontagged interface
129 #
130 route_no_tag = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200131 self,
132 ip_non_tag_bridged,
133 32,
134 [
135 VppRoutePath(
136 "0.0.0.0", self.pg1.sw_if_index, type=FibPathType.FIB_PATH_TYPE_DVR
137 )
138 ],
139 )
Neale Ranns6f631152017-10-03 08:20:21 -0700140 route_no_tag.add_vpp_config()
141
142 #
143 # Inject the packet that arrives and leaves on a non-tagged interface
144 # Since it's 'bridged' expect that the MAC headed is unchanged.
145 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400146 rx = self.send_and_expect(self.pg0, pkt_no_tag * NUM_PKTS, self.pg1)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800147 self.assert_same_mac_addr(pkt_no_tag, rx)
148 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700149
150 #
151 # Add routes to bridge the traffic via a tagged interface
152 #
Neale Ranns55d03782017-10-21 06:34:22 -0700153 route_with_tag = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200154 self,
155 ip_tag_bridged,
156 32,
157 [
158 VppRoutePath(
159 "0.0.0.0",
160 sub_if_on_pg3.sw_if_index,
161 type=FibPathType.FIB_PATH_TYPE_DVR,
162 )
163 ],
164 )
Neale Ranns55d03782017-10-21 06:34:22 -0700165 route_with_tag.add_vpp_config()
Neale Ranns6f631152017-10-03 08:20:21 -0700166
167 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800168 # Inject the packet that arrives non-tag and leaves on a tagged
169 # interface
Neale Ranns6f631152017-10-03 08:20:21 -0700170 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400171 rx = self.send_and_expect(self.pg0, pkt_tag * NUM_PKTS, self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700172 self.assert_same_mac_addr(pkt_tag, rx)
173 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700174
175 #
176 # Tag to tag
177 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178 pkt_tag_to_tag = (
179 Ether(src=self.pg2.remote_mac, dst=self.loop0.local_mac)
180 / Dot1Q(vlan=92)
181 / IP(src=any_src_addr, dst=ip_tag_bridged)
182 / UDP(sport=1234, dport=1234)
183 / Raw(b"\xa5" * 100)
184 )
Neale Ranns6f631152017-10-03 08:20:21 -0700185
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200186 rx = self.send_and_expect(self.pg2, pkt_tag_to_tag * NUM_PKTS, self.pg3)
Neale Ranns55d03782017-10-21 06:34:22 -0700187 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
188 self.assert_has_vlan_tag(93, rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700189
190 #
191 # Tag to non-Tag
192 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200193 pkt_tag_to_non_tag = (
194 Ether(src=self.pg2.remote_mac, dst=self.loop0.local_mac)
195 / Dot1Q(vlan=92)
196 / IP(src=any_src_addr, dst=ip_non_tag_bridged)
197 / UDP(sport=1234, dport=1234)
198 / Raw(b"\xa5" * 100)
199 )
Neale Ranns6f631152017-10-03 08:20:21 -0700200
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200201 rx = self.send_and_expect(self.pg2, pkt_tag_to_non_tag * NUM_PKTS, self.pg1)
Neale Ranns55d03782017-10-21 06:34:22 -0700202 self.assert_same_mac_addr(pkt_tag_to_tag, rx)
203 self.assert_has_no_tag(rx)
Neale Ranns6f631152017-10-03 08:20:21 -0700204
Neale Ranns55d03782017-10-21 06:34:22 -0700205 #
Neale Rannsf068c3e2018-01-03 04:18:48 -0800206 # Add an output L3 ACL that will block the traffic
207 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200208 rule_1 = AclRule(
209 is_permit=0,
210 proto=17,
211 ports=1234,
212 src_prefix=IPv4Network((any_src_addr, 32)),
213 dst_prefix=IPv4Network((ip_non_tag_bridged, 32)),
214 )
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100215 acl = VppAcl(self, rules=[rule_1])
216 acl.add_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800217
218 #
219 # Apply the ACL on the output interface
220 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200221 acl_if1 = VppAclInterface(
222 self, sw_if_index=self.pg1.sw_if_index, n_input=0, acls=[acl]
223 )
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100224 acl_if1.add_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800225
226 #
227 # Send packet's that should match the ACL and be dropped
228 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229 rx = self.send_and_assert_no_replies(self.pg2, pkt_tag_to_non_tag * NUM_PKTS)
Neale Rannsf068c3e2018-01-03 04:18:48 -0800230
231 #
Neale Ranns55d03782017-10-21 06:34:22 -0700232 # cleanup
233 #
Jakub Grajciar2f8cd912020-03-27 06:55:06 +0100234 acl_if1.remove_vpp_config()
235 acl.remove_vpp_config()
Neale Rannsf068c3e2018-01-03 04:18:48 -0800236
Ole Troana5b2eec2019-03-11 19:23:25 +0100237 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200238 rx_sw_if_index=self.pg0.sw_if_index, bd_id=1, enable=0
239 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100240 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200241 rx_sw_if_index=self.pg1.sw_if_index, bd_id=1, enable=0
242 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100243 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200244 rx_sw_if_index=sub_if_on_pg2.sw_if_index, bd_id=1, enable=0
245 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100246 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200247 rx_sw_if_index=sub_if_on_pg3.sw_if_index, bd_id=1, enable=0
248 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100249 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200250 rx_sw_if_index=self.loop0.sw_if_index,
251 bd_id=1,
252 port_type=L2_PORT_TYPE.BVI,
253 enable=0,
254 )
Neale Ranns55d03782017-10-21 06:34:22 -0700255
256 #
Neale Ranns81458422018-03-12 06:59:36 -0700257 # Do a FIB dump to make sure the paths are correctly reported as DVR
258 #
Neale Ranns097fa662018-05-01 05:17:55 -0700259 routes = self.vapi.ip_route_dump(0)
Neale Ranns81458422018-03-12 06:59:36 -0700260
261 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200262 if ip_tag_bridged == str(r.route.prefix.network_address):
263 self.assertEqual(
264 r.route.paths[0].sw_if_index, sub_if_on_pg3.sw_if_index
265 )
266 self.assertEqual(r.route.paths[0].type, FibPathType.FIB_PATH_TYPE_DVR)
267 if ip_non_tag_bridged == str(r.route.prefix.network_address):
268 self.assertEqual(r.route.paths[0].sw_if_index, self.pg1.sw_if_index)
269 self.assertEqual(r.route.paths[0].type, FibPathType.FIB_PATH_TYPE_DVR)
Neale Ranns81458422018-03-12 06:59:36 -0700270
271 #
Neale Ranns55d03782017-10-21 06:34:22 -0700272 # the explicit route delete is require so it happens before
273 # the sbu-interface delete. subinterface delete is required
274 # because that object type does not use the object registry
275 #
276 route_no_tag.remove_vpp_config()
277 route_with_tag.remove_vpp_config()
278 sub_if_on_pg3.remove_vpp_config()
279 sub_if_on_pg2.remove_vpp_config()
280
Neale Ranns6f631152017-10-03 08:20:21 -0700281
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200282if __name__ == "__main__":
Neale Ranns6f631152017-10-03 08:20:21 -0700283 unittest.main(testRunner=VppTestRunner)