blob: ac3b30ba71da34d312774fe744f2637308c44c4a [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Matej Klotton86d87c42016-11-11 11:38:55 +01002"""IRB Test Case HLD:
3
4**config**
5 - L2 MAC learning enabled in l2bd
6 - 2 routed interfaces untagged, bvi (Bridge Virtual Interface)
7 - 2 bridged interfaces in l2bd with bvi
8
9**test**
10 - sending ip4 eth pkts between routed interfaces
11 - 2 routed interfaces
12 - 2 bridged interfaces
13
14 - 64B, 512B, 1518B, 9200B (ether_size)
15
16 - burst of pkts per interface
17 - 257pkts per burst
18 - routed pkts hitting different FIB entries
19 - bridged pkts hitting different MAC entries
20
21**verify**
22 - all packets received correctly
23
24"""
25
Matej Klotton0178d522016-11-04 11:11:44 +010026import unittest
Matej Klotton86d87c42016-11-11 11:38:55 +010027from random import choice
Matej Klotton0178d522016-11-04 11:11:44 +010028
29from scapy.packet import Raw
30from scapy.layers.l2 import Ether
31from scapy.layers.inet import IP, UDP
Matej Klotton0178d522016-11-04 11:11:44 +010032
33from framework import VppTestCase, VppTestRunner
Ole Troan8006c6a2018-12-17 12:02:26 +010034from vpp_papi import MACAddress
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -070035from vpp_l2 import L2_PORT_TYPE
Matej Klotton0178d522016-11-04 11:11:44 +010036
Matej Klotton0178d522016-11-04 11:11:44 +010037
38class TestIpIrb(VppTestCase):
Matej Klotton86d87c42016-11-11 11:38:55 +010039 """IRB Test Case"""
Matej Klotton0178d522016-11-04 11:11:44 +010040
41 @classmethod
42 def setUpClass(cls):
Matej Klotton86d87c42016-11-11 11:38:55 +010043 """
44 #. Create BD with MAC learning enabled and put interfaces to this BD.
Neale Ranns192b13f2019-03-15 02:16:20 -070045 #. Configure IPv4 addresses on BVI interface and routed interface.
46 #. Configure MAC address binding to IPv4 neighbors on bvi0.
Matej Klotton86d87c42016-11-11 11:38:55 +010047 #. Configure MAC address on pg2.
Neale Ranns192b13f2019-03-15 02:16:20 -070048 #. BVI interface has remote hosts, one half of hosts are
Matej Klotton86d87c42016-11-11 11:38:55 +010049 behind pg0 second behind pg1.
50 """
Matej Klotton0178d522016-11-04 11:11:44 +010051 super(TestIpIrb, cls).setUpClass()
52
53 cls.pg_if_packet_sizes = [64, 512, 1518, 9018] # packet sizes
54 cls.bd_id = 10
55 cls.remote_hosts_count = 250
56
Neale Ranns192b13f2019-03-15 02:16:20 -070057 # create 3 pg interfaces, 1 BVI interface
Matej Klotton0178d522016-11-04 11:11:44 +010058 cls.create_pg_interfaces(range(3))
Neale Ranns192b13f2019-03-15 02:16:20 -070059 cls.create_bvi_interfaces(1)
Matej Klotton0178d522016-11-04 11:11:44 +010060
61 cls.interfaces = list(cls.pg_interfaces)
Neale Ranns192b13f2019-03-15 02:16:20 -070062 cls.interfaces.extend(cls.bvi_interfaces)
Matej Klotton0178d522016-11-04 11:11:44 +010063
64 for i in cls.interfaces:
65 i.admin_up()
66
67 # Create BD with MAC learning enabled and put interfaces to this BD
68 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020069 rx_sw_if_index=cls.bvi0.sw_if_index,
70 bd_id=cls.bd_id,
71 port_type=L2_PORT_TYPE.BVI,
72 )
73 cls.vapi.sw_interface_set_l2_bridge(
74 rx_sw_if_index=cls.pg0.sw_if_index, bd_id=cls.bd_id
75 )
76 cls.vapi.sw_interface_set_l2_bridge(
77 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.bd_id
78 )
Matej Klotton0178d522016-11-04 11:11:44 +010079
Neale Ranns192b13f2019-03-15 02:16:20 -070080 # Configure IPv4 addresses on BVI interface and routed interface
81 cls.bvi0.config_ip4()
Matej Klotton0178d522016-11-04 11:11:44 +010082 cls.pg2.config_ip4()
83
Neale Ranns192b13f2019-03-15 02:16:20 -070084 # Configure MAC address binding to IPv4 neighbors on bvi0
85 cls.bvi0.generate_remote_hosts(cls.remote_hosts_count)
86 cls.bvi0.configure_ipv4_neighbors()
Matej Klotton0178d522016-11-04 11:11:44 +010087 # configure MAC address on pg2
88 cls.pg2.resolve_arp()
89
Neale Ranns192b13f2019-03-15 02:16:20 -070090 # BVI interface has remote hosts, one half of hosts are behind
Matej Klotton86d87c42016-11-11 11:38:55 +010091 # pg0 second behind pg1
Matej Klotton0178d522016-11-04 11:11:44 +010092 half = cls.remote_hosts_count // 2
Neale Ranns192b13f2019-03-15 02:16:20 -070093 cls.pg0.remote_hosts = cls.bvi0.remote_hosts[:half]
94 cls.pg1.remote_hosts = cls.bvi0.remote_hosts[half:]
Matej Klotton0178d522016-11-04 11:11:44 +010095
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070096 @classmethod
97 def tearDownClass(cls):
98 super(TestIpIrb, cls).tearDownClass()
99
Matej Klotton0178d522016-11-04 11:11:44 +0100100 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100101 """Run standard test teardown and log ``show l2patch``,
102 ``show l2fib verbose``,``show bridge-domain <bd_id> detail``,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000103 ``show ip neighbors``.
Matej Klotton86d87c42016-11-11 11:38:55 +0100104 """
Matej Klotton0178d522016-11-04 11:11:44 +0100105 super(TestIpIrb, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700106
107 def show_commands_at_teardown(self):
108 self.logger.info(self.vapi.cli("show l2patch"))
109 self.logger.info(self.vapi.cli("show l2fib verbose"))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 self.logger.info(self.vapi.cli("show bridge-domain %s detail" % self.bd_id))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000111 self.logger.info(self.vapi.cli("show ip neighbors"))
Matej Klotton0178d522016-11-04 11:11:44 +0100112
113 def create_stream(self, src_ip_if, dst_ip_if, packet_sizes):
114 pkts = []
115 for i in range(0, 257):
116 remote_dst_host = choice(dst_ip_if.remote_hosts)
Klement Sekeradab231a2016-12-21 08:50:14 +0100117 info = self.create_packet_info(src_ip_if, dst_ip_if)
Matej Klotton0178d522016-11-04 11:11:44 +0100118 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 p = (
120 Ether(dst=src_ip_if.local_mac, src=src_ip_if.remote_mac)
121 / IP(src=src_ip_if.remote_ip4, dst=remote_dst_host.ip4)
122 / UDP(sport=1234, dport=1234)
123 / Raw(payload)
124 )
Matej Klotton0178d522016-11-04 11:11:44 +0100125 info.data = p.copy()
126 size = packet_sizes[(i // 2) % len(packet_sizes)]
127 self.extend_packet(p, size)
128 pkts.append(p)
129 return pkts
130
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200131 def create_stream_l2_to_ip(self, src_l2_if, src_ip_if, dst_ip_if, packet_sizes):
Matej Klotton0178d522016-11-04 11:11:44 +0100132 pkts = []
133 for i in range(0, 257):
Klement Sekeradab231a2016-12-21 08:50:14 +0100134 info = self.create_packet_info(src_ip_if, dst_ip_if)
Matej Klotton0178d522016-11-04 11:11:44 +0100135 payload = self.info_to_payload(info)
136
137 host = choice(src_l2_if.remote_hosts)
138
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200139 p = (
140 Ether(src=host.mac, dst=src_ip_if.local_mac)
141 / IP(src=host.ip4, dst=dst_ip_if.remote_ip4)
142 / UDP(sport=1234, dport=1234)
143 / Raw(payload)
144 )
Matej Klotton0178d522016-11-04 11:11:44 +0100145
146 info.data = p.copy()
147 size = packet_sizes[(i // 2) % len(packet_sizes)]
148 self.extend_packet(p, size)
149
150 pkts.append(p)
151 return pkts
152
153 def verify_capture_l2_to_ip(self, dst_ip_if, src_ip_if, capture):
154 last_info = dict()
155 for i in self.interfaces:
156 last_info[i.sw_if_index] = None
157
158 dst_ip_sw_if_index = dst_ip_if.sw_if_index
159
160 for packet in capture:
161 ip = packet[IP]
162 udp = packet[IP][UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800163 payload_info = self.payload_to_info(packet[IP][UDP][Raw])
Matej Klotton0178d522016-11-04 11:11:44 +0100164
165 self.assertEqual(payload_info.dst, dst_ip_sw_if_index)
166
167 next_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200168 payload_info.src, dst_ip_sw_if_index, last_info[payload_info.src]
169 )
Matej Klotton0178d522016-11-04 11:11:44 +0100170 last_info[payload_info.src] = next_info
171 self.assertTrue(next_info is not None)
172 saved_packet = next_info.data
173 self.assertTrue(next_info is not None)
174
175 # MAC: src, dst
176 self.assertEqual(packet.src, dst_ip_if.local_mac)
177 self.assertEqual(packet.dst, dst_ip_if.remote_mac)
178
179 # IP: src, dst
180 host = src_ip_if.host_by_ip4(ip.src)
181 self.assertIsNotNone(host)
182 self.assertEqual(ip.dst, saved_packet[IP].dst)
183 self.assertEqual(ip.dst, dst_ip_if.remote_ip4)
184
185 # UDP:
186 self.assertEqual(udp.sport, saved_packet[UDP].sport)
187 self.assertEqual(udp.dport, saved_packet[UDP].dport)
188
189 def verify_capture(self, dst_ip_if, src_ip_if, capture):
190 last_info = dict()
191 for i in self.interfaces:
192 last_info[i.sw_if_index] = None
193
194 dst_ip_sw_if_index = dst_ip_if.sw_if_index
195
196 for packet in capture:
197 ip = packet[IP]
198 udp = packet[IP][UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800199 payload_info = self.payload_to_info(packet[IP][UDP][Raw])
Matej Klotton0178d522016-11-04 11:11:44 +0100200 packet_index = payload_info.index
201
202 self.assertEqual(payload_info.dst, dst_ip_sw_if_index)
203
204 next_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200205 payload_info.src, dst_ip_sw_if_index, last_info[payload_info.src]
206 )
Matej Klotton0178d522016-11-04 11:11:44 +0100207 last_info[payload_info.src] = next_info
208 self.assertTrue(next_info is not None)
209 self.assertEqual(packet_index, next_info.index)
210 saved_packet = next_info.data
211 self.assertTrue(next_info is not None)
212
213 # MAC: src, dst
214 self.assertEqual(packet.src, dst_ip_if.local_mac)
215 host = dst_ip_if.host_by_mac(packet.dst)
216
217 # IP: src, dst
218 self.assertEqual(ip.src, src_ip_if.remote_ip4)
219 self.assertEqual(ip.dst, saved_packet[IP].dst)
220 self.assertEqual(ip.dst, host.ip4)
221
222 # UDP:
223 self.assertEqual(udp.sport, saved_packet[UDP].sport)
224 self.assertEqual(udp.dport, saved_packet[UDP].dport)
225
226 def test_ip4_irb_1(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200227 """IPv4 IRB test 1
Matej Klotton0178d522016-11-04 11:11:44 +0100228
229 Test scenario:
Matej Klotton86d87c42016-11-11 11:38:55 +0100230 - ip traffic from pg2 interface must ends in both pg0 and pg1
Neale Ranns192b13f2019-03-15 02:16:20 -0700231 - arp entry present in bvi0 interface for destination IP
232 - no l2 entry configured, pg0 and pg1 are same
Matej Klotton0178d522016-11-04 11:11:44 +0100233 """
234
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200235 stream = self.create_stream(self.pg2, self.bvi0, self.pg_if_packet_sizes)
Matej Klotton0178d522016-11-04 11:11:44 +0100236 self.pg2.add_stream(stream)
237
238 self.pg_enable_capture(self.pg_interfaces)
239 self.pg_start()
240
Neale Ranns192b13f2019-03-15 02:16:20 -0700241 packet_count = self.get_packet_count_for_if_idx(self.bvi0.sw_if_index)
Klement Sekeradab231a2016-12-21 08:50:14 +0100242
243 rcvd1 = self.pg0.get_capture(packet_count)
244 rcvd2 = self.pg1.get_capture(packet_count)
Matej Klotton0178d522016-11-04 11:11:44 +0100245
Neale Ranns192b13f2019-03-15 02:16:20 -0700246 self.verify_capture(self.bvi0, self.pg2, rcvd1)
247 self.verify_capture(self.bvi0, self.pg2, rcvd2)
Matej Klotton0178d522016-11-04 11:11:44 +0100248
249 self.assertListEqual(rcvd1.res, rcvd2.res)
250
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700251 def send_and_verify_l2_to_ip(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100252 stream1 = self.create_stream_l2_to_ip(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200253 self.pg0, self.bvi0, self.pg2, self.pg_if_packet_sizes
254 )
Matej Klotton0178d522016-11-04 11:11:44 +0100255 stream2 = self.create_stream_l2_to_ip(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200256 self.pg1, self.bvi0, self.pg2, self.pg_if_packet_sizes
257 )
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700258 self.vapi.cli("clear trace")
Matej Klotton0178d522016-11-04 11:11:44 +0100259 self.pg0.add_stream(stream1)
260 self.pg1.add_stream(stream2)
261
262 self.pg_enable_capture(self.pg_interfaces)
263 self.pg_start()
264
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700265 rcvd = self.pg2.get_capture(514)
Neale Ranns192b13f2019-03-15 02:16:20 -0700266 self.verify_capture_l2_to_ip(self.pg2, self.bvi0, rcvd)
Matej Klotton0178d522016-11-04 11:11:44 +0100267
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700268 def test_ip4_irb_2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 """IPv4 IRB test 2
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700270
271 Test scenario:
272 - ip traffic from pg0 and pg1 ends on pg2
273 """
274 self.send_and_verify_l2_to_ip()
275
276 # change the BVI's mac and resed traffic
Neale Ranns192b13f2019-03-15 02:16:20 -0700277 self.bvi0.set_mac(MACAddress("00:00:00:11:11:33"))
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700278
279 self.send_and_verify_l2_to_ip()
280 # check it wasn't flooded
281 self.pg1.assert_nothing_captured(remark="UU Flood")
282
Matej Klotton0178d522016-11-04 11:11:44 +0100283
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200284if __name__ == "__main__":
Matej Klotton0178d522016-11-04 11:11:44 +0100285 unittest.main(testRunner=VppTestRunner)