blob: a183b0ca0babf037f89d982613bd9410bc55ab82 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Matej Klotton16a14cd2016-12-07 15:09:13 +01002import random
Klement Sekeraf62ae122016-10-11 11:47:09 +02003import socket
Matej Klotton16a14cd2016-12-07 15:09:13 +01004import unittest
Klement Sekeraf62ae122016-10-11 11:47:09 +02005
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -08006from scapy.contrib.mpls import MPLS
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09007from scapy.contrib.gtp import GTP_U_Header
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -08008from scapy.layers.inet import IP, UDP, TCP, ICMP, icmptypes, icmpcodes
Neale Rannsaa7cfd02022-03-24 12:28:42 +00009from scapy.layers.inet6 import IPv6
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -080010from scapy.layers.l2 import Ether, Dot1Q, ARP
11from scapy.packet import Raw
12from six import moves
13
Dave Wallace8800f732023-08-31 00:47:44 -040014from framework import VppTestCase
15from asfframework import VppTestRunner, tag_fixme_vpp_workers
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -080016from util import ppp
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017from vpp_ip_route import (
18 VppIpRoute,
19 VppRoutePath,
20 VppIpMRoute,
21 VppMRoutePath,
22 VppMplsIpBind,
23 VppMplsTable,
24 VppIpTable,
25 FibPathType,
26 find_route,
27 VppIpInterfaceAddress,
28 find_route_in_dump,
29 find_mroute_in_dump,
30)
Neale Ranns8f5fef22020-12-21 08:29:34 +000031from vpp_ip import VppIpPuntPolicer, VppIpPuntRedirect, VppIpPathMtu
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -080032from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
Klement Sekerafd1f56a2021-11-22 21:25:57 +010033from vpp_papi import vpp_papi, VppEnum
Neale Ranns0b6a8572019-10-30 17:34:14 +000034from vpp_neighbor import VppNeighbor
Neale Ranns9efcee62019-11-26 19:30:08 +000035from vpp_lo_interface import VppLoInterface
Brian Russellc8f3cdf2021-01-19 16:57:42 +000036from vpp_policer import VppPolicer, PolicerAction
Damjan Marionf56b77a2016-10-03 19:44:57 +020037
Paul Vinciguerra4271c972019-05-14 13:25:49 -040038NUM_PKTS = 67
39
Damjan Marionf56b77a2016-10-03 19:44:57 +020040
Klement Sekeraf62ae122016-10-11 11:47:09 +020041class TestIPv4(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 """IPv4 Test Case"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020043
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070044 @classmethod
45 def setUpClass(cls):
46 super(TestIPv4, cls).setUpClass()
47
48 @classmethod
49 def tearDownClass(cls):
50 super(TestIPv4, cls).tearDownClass()
51
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010053 """
54 Perform test setup before test case.
55
56 **Config:**
57 - create 3 pg interfaces
58 - untagged pg0 interface
59 - Dot1Q subinterface on pg1
60 - Dot1AD subinterface on pg2
61 - setup interfaces:
62 - put it into UP state
63 - set IPv4 addresses
64 - resolve neighbor address using ARP
65 - configure 200 fib entries
66
67 :ivar list interfaces: pg interfaces and subinterfaces.
68 :ivar dict flows: IPv4 packet flows in test.
Matej Klotton86d87c42016-11-11 11:38:55 +010069 """
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 super(TestIPv4, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +020071
Klement Sekeraf62ae122016-10-11 11:47:09 +020072 # create 3 pg interfaces
73 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +020074
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 # create 2 subinterfaces for pg1 and pg2
76 self.sub_interfaces = [
77 VppDot1QSubint(self, self.pg1, 100),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 VppDot1ADSubint(self, self.pg2, 200, 300, 400),
79 ]
Damjan Marionf56b77a2016-10-03 19:44:57 +020080
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
82 self.flows = dict()
83 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
84 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
85 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +020086
Klement Sekeraf62ae122016-10-11 11:47:09 +020087 # packet sizes
Jan Geletye6c78ee2018-06-26 12:24:03 +020088 self.pg_if_packet_sizes = [64, 1500, 9020]
Damjan Marionf56b77a2016-10-03 19:44:57 +020089
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 self.interfaces = list(self.pg_interfaces)
91 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +020092
Klement Sekeraf62ae122016-10-11 11:47:09 +020093 # setup all interfaces
94 for i in self.interfaces:
95 i.admin_up()
96 i.config_ip4()
97 i.resolve_arp()
98
Matej Klotton86d87c42016-11-11 11:38:55 +010099 # config 2M FIB entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200100
101 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100102 """Run standard test teardown and log ``show ip arp``."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200103 super(TestIPv4, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700104
105 def show_commands_at_teardown(self):
Neale Rannscbe25aa2019-09-30 10:53:31 +0000106 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700107 # info(self.vapi.cli("show ip fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200108
Jan Geletye6c78ee2018-06-26 12:24:03 +0200109 def modify_packet(self, src_if, packet_size, pkt):
110 """Add load, set destination IP and extend packet to required packet
111 size for defined interface.
112
113 :param VppInterface src_if: Interface to create packet for.
114 :param int packet_size: Required packet size.
115 :param Scapy pkt: Packet to be modified.
116 """
Ole Troan6ed154f2019-10-15 19:31:55 +0200117 dst_if_idx = int(packet_size / 10 % 2)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200118 dst_if = self.flows[src_if][dst_if_idx]
119 info = self.create_packet_info(src_if, dst_if)
120 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121 p = pkt / Raw(payload)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200122 p[IP].dst = dst_if.remote_ip4
123 info.data = p.copy()
124 if isinstance(src_if, VppSubInterface):
125 p = src_if.add_dot1_layer(p)
126 self.extend_packet(p, packet_size)
127
128 return p
129
130 def create_stream(self, src_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100131 """Create input packet stream for defined interface.
132
133 :param VppInterface src_if: Interface to create packet stream for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100134 """
Jan Geletye6c78ee2018-06-26 12:24:03 +0200135 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200136 pkt_tmpl = (
137 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
138 / IP(src=src_if.remote_ip4)
139 / UDP(sport=1234, dport=1234)
140 )
Jan Geletye6c78ee2018-06-26 12:24:03 +0200141
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 pkts = [
143 self.modify_packet(src_if, i, pkt_tmpl)
144 for i in moves.range(
145 self.pg_if_packet_sizes[0], self.pg_if_packet_sizes[1], 10
146 )
147 ]
148 pkts_b = [
149 self.modify_packet(src_if, i, pkt_tmpl)
150 for i in moves.range(
151 self.pg_if_packet_sizes[1] + hdr_ext,
152 self.pg_if_packet_sizes[2] + hdr_ext,
153 50,
154 )
155 ]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200156 pkts.extend(pkts_b)
157
Damjan Marionf56b77a2016-10-03 19:44:57 +0200158 return pkts
159
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100161 """Verify captured input packet stream for defined interface.
162
163 :param VppInterface dst_if: Interface to verify captured packet stream
Jan Geletye6c78ee2018-06-26 12:24:03 +0200164 for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100165 :param list capture: Captured packet stream.
166 """
167 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200168 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200169 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200170 last_info[i.sw_if_index] = None
171 is_sub_if = False
172 dst_sw_if_index = dst_if.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200173 if hasattr(dst_if, "parent"):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200174 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200175 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200176 if is_sub_if:
177 # Check VLAN tags and Ethernet header
178 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200179 self.assertTrue(Dot1Q not in packet)
180 try:
181 ip = packet[IP]
182 udp = packet[UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800183 payload_info = self.payload_to_info(packet[Raw])
Damjan Marionf56b77a2016-10-03 19:44:57 +0200184 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200185 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100186 self.logger.debug(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200187 "Got packet on port %s: src=%u (id=%u)"
188 % (dst_if.name, payload_info.src, packet_index)
189 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200190 next_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200191 payload_info.src, dst_sw_if_index, last_info[payload_info.src]
192 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200193 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200194 self.assertTrue(next_info is not None)
195 self.assertEqual(packet_index, next_info.index)
196 saved_packet = next_info.data
197 # Check standard fields
198 self.assertEqual(ip.src, saved_packet[IP].src)
199 self.assertEqual(ip.dst, saved_packet[IP].dst)
200 self.assertEqual(udp.sport, saved_packet[UDP].sport)
201 self.assertEqual(udp.dport, saved_packet[UDP].dport)
202 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100203 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200204 raise
205 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200206 remaining_packet = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200207 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index]
208 )
209 self.assertTrue(
210 remaining_packet is None,
211 "Interface %s: Packet expected from interface %s "
212 "didn't arrive" % (dst_if.name, i.name),
213 )
Damjan Marionf56b77a2016-10-03 19:44:57 +0200214
215 def test_fib(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200216 """IPv4 FIB test
Matej Klotton86d87c42016-11-11 11:38:55 +0100217
218 Test scenario:
219
220 - Create IPv4 stream for pg0 interface
Jan Geletye6c78ee2018-06-26 12:24:03 +0200221 - Create IPv4 tagged streams for pg1's and pg2's sub-interface.
Matej Klotton86d87c42016-11-11 11:38:55 +0100222 - Send and verify received packets on each interface.
223 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200224
Jan Geletye6c78ee2018-06-26 12:24:03 +0200225 pkts = self.create_stream(self.pg0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200226 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200227
Klement Sekeraf62ae122016-10-11 11:47:09 +0200228 for i in self.sub_interfaces:
Jan Geletye6c78ee2018-06-26 12:24:03 +0200229 pkts = self.create_stream(i)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200230 i.parent.add_stream(pkts)
231
232 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200233 self.pg_start()
234
Klement Sekeraf62ae122016-10-11 11:47:09 +0200235 pkts = self.pg0.get_capture()
236 self.verify_capture(self.pg0, pkts)
237
238 for i in self.sub_interfaces:
239 pkts = i.parent.get_capture()
240 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200241
242
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400243class TestIPv4RouteLookup(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200244 """IPv4 Route Lookup Test Case"""
245
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400246 routes = []
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000247 tables = []
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400248
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000249 def route_lookup(self, prefix, exact, table_id=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200250 return self.vapi.api(
251 self.vapi.papi.ip_route_lookup,
252 {
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000253 "table_id": table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200254 "exact": exact,
255 "prefix": prefix,
256 },
257 )
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400258
259 @classmethod
260 def setUpClass(cls):
261 super(TestIPv4RouteLookup, cls).setUpClass()
262
263 @classmethod
264 def tearDownClass(cls):
265 super(TestIPv4RouteLookup, cls).tearDownClass()
266
267 def setUp(self):
268 super(TestIPv4RouteLookup, self).setUp()
269
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200270 drop_nh = VppRoutePath(
271 "127.0.0.1", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP
272 )
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400273
274 # Add 3 routes
275 r = VppIpRoute(self, "1.1.0.0", 16, [drop_nh])
276 r.add_vpp_config()
277 self.routes.append(r)
278
279 r = VppIpRoute(self, "1.1.1.0", 24, [drop_nh])
280 r.add_vpp_config()
281 self.routes.append(r)
282
283 r = VppIpRoute(self, "1.1.1.1", 32, [drop_nh])
284 r.add_vpp_config()
285 self.routes.append(r)
286
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000287 custom_vrf = VppIpTable(self, 200)
288 custom_vrf.add_vpp_config()
289 self.tables.append(custom_vrf)
290
291 r = VppIpRoute(self, "2.2.0.0", 16, [drop_nh], 200)
292 r.add_vpp_config()
293 self.routes.append(r)
294
295 r = VppIpRoute(self, "2.2.2.0", 24, [drop_nh], 200)
296 r.add_vpp_config()
297 self.routes.append(r)
298
299 r = VppIpRoute(self, "2.2.2.2", 32, [drop_nh], 200)
300 r.add_vpp_config()
301 self.routes.append(r)
302
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400303 def tearDown(self):
304 # Remove the routes we added
305 for r in self.routes:
306 r.remove_vpp_config()
307
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000308 for vrf in self.tables:
309 vrf.remove_vpp_config()
310
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400311 super(TestIPv4RouteLookup, self).tearDown()
312
313 def test_exact_match(self):
314 # Verify we find the host route
315 prefix = "1.1.1.1/32"
316 result = self.route_lookup(prefix, True)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200317 assert prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400318
319 # Verify we find a middle prefix route
320 prefix = "1.1.1.0/24"
321 result = self.route_lookup(prefix, True)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200322 assert prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400323
324 # Verify we do not find an available LPM.
325 with self.vapi.assert_negative_api_retval():
326 self.route_lookup("1.1.1.2/32", True)
327
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000328 # Verify we find the host route
329 prefix = "2.2.2.2/32"
330 result = self.route_lookup(prefix, True, 200)
331 assert prefix == str(result.route.prefix)
332
333 # Verify we find a middle prefix route
334 prefix = "2.2.2.0/24"
335 result = self.route_lookup(prefix, True, 200)
336 assert prefix == str(result.route.prefix)
337
338 # Verify we do not find an available LPM.
339 with self.vapi.assert_negative_api_retval():
340 self.route_lookup("2.2.2.1/32", True, 200)
341
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400342 def test_longest_prefix_match(self):
343 # verify we find lpm
344 lpm_prefix = "1.1.1.0/24"
345 result = self.route_lookup("1.1.1.2/32", False)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200346 assert lpm_prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400347
348 # Verify we find the exact when not requested
349 result = self.route_lookup(lpm_prefix, False)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200350 assert lpm_prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400351
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000352 # verify we find lpm
353 lpm_prefix = "2.2.2.0/24"
354 result = self.route_lookup("2.2.2.1/32", False, 200)
355 assert lpm_prefix == str(result.route.prefix)
356
357 # Verify we find the exact when not requested
358 result = self.route_lookup(lpm_prefix, False, 200)
359 assert lpm_prefix == str(result.route.prefix)
360
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400361 # Can't seem to delete the default route so no negative LPM test.
362
363
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500364class TestIPv4IfAddrRoute(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200365 """IPv4 Interface Addr Route Test Case"""
Matthew G Smith88d29a92019-07-17 10:01:17 -0500366
367 @classmethod
368 def setUpClass(cls):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500369 super(TestIPv4IfAddrRoute, cls).setUpClass()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500370
371 @classmethod
372 def tearDownClass(cls):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500373 super(TestIPv4IfAddrRoute, cls).tearDownClass()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500374
375 def setUp(self):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500376 super(TestIPv4IfAddrRoute, self).setUp()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500377
378 # create 1 pg interface
379 self.create_pg_interfaces(range(1))
380
381 for i in self.pg_interfaces:
382 i.admin_up()
383 i.config_ip4()
384 i.resolve_arp()
385
386 def tearDown(self):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500387 super(TestIPv4IfAddrRoute, self).tearDown()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500388 for i in self.pg_interfaces:
389 i.unconfig_ip4()
390 i.admin_down()
391
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500392 def test_ipv4_ifaddrs_same_prefix(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200393 """IPv4 Interface Addresses Same Prefix test
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500394
395 Test scenario:
396
397 - Verify no route in FIB for prefix 10.10.10.0/24
398 - Configure IPv4 address 10.10.10.10/24 on an interface
399 - Verify route in FIB for prefix 10.10.10.0/24
400 - Configure IPv4 address 10.10.10.20/24 on an interface
401 - Delete 10.10.10.10/24 from interface
402 - Verify route in FIB for prefix 10.10.10.0/24
403 - Delete 10.10.10.20/24 from interface
404 - Verify no route in FIB for prefix 10.10.10.0/24
405 """
406
407 # create two addresses, verify route not present
Neale Rannsefd7bc22019-11-11 08:32:34 +0000408 if_addr1 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.10", 24)
409 if_addr2 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.20", 24)
410 self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500411 self.assertFalse(find_route(self, "10.10.10.10", 32))
412 self.assertFalse(find_route(self, "10.10.10.20", 32))
413 self.assertFalse(find_route(self, "10.10.10.255", 32))
414 self.assertFalse(find_route(self, "10.10.10.0", 32))
415
416 # configure first address, verify route present
417 if_addr1.add_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000418 self.assertTrue(if_addr1.query_vpp_config()) # 10.10.10.10/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500419 self.assertTrue(find_route(self, "10.10.10.10", 32))
420 self.assertFalse(find_route(self, "10.10.10.20", 32))
421 self.assertTrue(find_route(self, "10.10.10.255", 32))
422 self.assertTrue(find_route(self, "10.10.10.0", 32))
423
424 # configure second address, delete first, verify route not removed
425 if_addr2.add_vpp_config()
426 if_addr1.remove_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000427 self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24
428 self.assertTrue(if_addr2.query_vpp_config()) # 10.10.10.20/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500429 self.assertFalse(find_route(self, "10.10.10.10", 32))
430 self.assertTrue(find_route(self, "10.10.10.20", 32))
431 self.assertTrue(find_route(self, "10.10.10.255", 32))
432 self.assertTrue(find_route(self, "10.10.10.0", 32))
433
434 # delete second address, verify route removed
435 if_addr2.remove_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000436 self.assertFalse(if_addr2.query_vpp_config()) # 10.10.10.20/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500437 self.assertFalse(find_route(self, "10.10.10.10", 32))
438 self.assertFalse(find_route(self, "10.10.10.20", 32))
439 self.assertFalse(find_route(self, "10.10.10.255", 32))
440 self.assertFalse(find_route(self, "10.10.10.0", 32))
441
Matthew G Smith88d29a92019-07-17 10:01:17 -0500442 def test_ipv4_ifaddr_route(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200443 """IPv4 Interface Address Route test
Matthew G Smith88d29a92019-07-17 10:01:17 -0500444
445 Test scenario:
446
447 - Create loopback
448 - Configure IPv4 address on loopback
449 - Verify that address is not in the FIB
450 - Bring loopback up
451 - Verify that address is in the FIB now
452 - Bring loopback down
453 - Verify that address is not in the FIB anymore
454 - Bring loopback up
455 - Configure IPv4 address on loopback
456 - Verify that address is in the FIB now
457 """
458
459 # create a loopback and configure IPv4
460 loopbacks = self.create_loopback_interfaces(1)
461 lo_if = self.lo_interfaces[0]
462
463 lo_if.local_ip4_prefix_len = 32
464 lo_if.config_ip4()
465
466 # The intf was down when addr was added -> entry not in FIB
467 fib4_dump = self.vapi.ip_route_dump(0)
468 self.assertFalse(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
469
470 # When intf is brought up, entry is added
471 lo_if.admin_up()
472 fib4_dump = self.vapi.ip_route_dump(0)
473 self.assertTrue(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
474
475 # When intf is brought down, entry is removed
476 lo_if.admin_down()
477 fib4_dump = self.vapi.ip_route_dump(0)
478 self.assertFalse(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
479
480 # Remove addr, bring up interface, re-add -> entry in FIB
481 lo_if.unconfig_ip4()
482 lo_if.admin_up()
483 lo_if.config_ip4()
484 fib4_dump = self.vapi.ip_route_dump(0)
485 self.assertTrue(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
486
yedgdbd366b2020-05-14 10:51:53 +0800487 def test_ipv4_ifaddr_del(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200488 """Delete an interface address that does not exist"""
yedgdbd366b2020-05-14 10:51:53 +0800489
490 loopbacks = self.create_loopback_interfaces(1)
491 lo = self.lo_interfaces[0]
492
493 lo.config_ip4()
494 lo.admin_up()
495
496 #
497 # try and remove pg0's subnet from lo
498 #
499 with self.vapi.assert_negative_api_retval():
500 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200501 sw_if_index=lo.sw_if_index, prefix=self.pg0.local_ip4_prefix, is_add=0
502 )
yedgdbd366b2020-05-14 10:51:53 +0800503
Matthew G Smith88d29a92019-07-17 10:01:17 -0500504
Jan Geletye6c78ee2018-06-26 12:24:03 +0200505class TestICMPEcho(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200506 """ICMP Echo Test Case"""
Jan Geletye6c78ee2018-06-26 12:24:03 +0200507
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700508 @classmethod
509 def setUpClass(cls):
510 super(TestICMPEcho, cls).setUpClass()
511
512 @classmethod
513 def tearDownClass(cls):
514 super(TestICMPEcho, cls).tearDownClass()
515
Jan Geletye6c78ee2018-06-26 12:24:03 +0200516 def setUp(self):
517 super(TestICMPEcho, self).setUp()
518
519 # create 1 pg interface
520 self.create_pg_interfaces(range(1))
521
522 for i in self.pg_interfaces:
523 i.admin_up()
524 i.config_ip4()
525 i.resolve_arp()
526
527 def tearDown(self):
528 super(TestICMPEcho, self).tearDown()
529 for i in self.pg_interfaces:
530 i.unconfig_ip4()
531 i.admin_down()
532
533 def test_icmp_echo(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200534 """VPP replies to ICMP Echo Request
Jan Geletye6c78ee2018-06-26 12:24:03 +0200535
536 Test scenario:
537
538 - Receive ICMP Echo Request message on pg0 interface.
539 - Check outgoing ICMP Echo Reply message on pg0 interface.
540 """
541
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200542 icmp_id = 0xB
Jan Geletye6c78ee2018-06-26 12:24:03 +0200543 icmp_seq = 5
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200544 icmp_load = b"\x0a" * 18
545 p_echo_request = (
546 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
547 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
548 / ICMP(id=icmp_id, seq=icmp_seq)
549 / Raw(load=icmp_load)
550 )
Jan Geletye6c78ee2018-06-26 12:24:03 +0200551
552 self.pg0.add_stream(p_echo_request)
553 self.pg_enable_capture(self.pg_interfaces)
554 self.pg_start()
555
556 rx = self.pg0.get_capture(1)
557 rx = rx[0]
558 ether = rx[Ether]
559 ipv4 = rx[IP]
560 icmp = rx[ICMP]
561
562 self.assertEqual(ether.src, self.pg0.local_mac)
563 self.assertEqual(ether.dst, self.pg0.remote_mac)
564
565 self.assertEqual(ipv4.src, self.pg0.local_ip4)
566 self.assertEqual(ipv4.dst, self.pg0.remote_ip4)
567
568 self.assertEqual(icmptypes[icmp.type], "echo-reply")
569 self.assertEqual(icmp.id, icmp_id)
570 self.assertEqual(icmp.seq, icmp_seq)
571 self.assertEqual(icmp[Raw].load, icmp_load)
572
573
Matej Klotton16a14cd2016-12-07 15:09:13 +0100574class TestIPv4FibCrud(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200575 """FIB - add/update/delete - ip4 routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100576
577 Test scenario:
578 - add 1k,
579 - del 100,
580 - add new 1k,
581 - del 1.5k
582
Klement Sekerada505f62017-01-04 12:58:53 +0100583 ..note:: Python API is too slow to add many routes, needs replacement.
Matej Klotton16a14cd2016-12-07 15:09:13 +0100584 """
585
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200586 def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count, start=0):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100587 """
588
589 :param start_dest_addr:
590 :param next_hop_addr:
591 :param count:
592 :return list: added ips with 32 prefix
593 """
Neale Ranns097fa662018-05-01 05:17:55 -0700594 routes = []
595 for i in range(count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200596 r = VppIpRoute(
597 self,
598 start_dest_addr % (i + start),
599 32,
600 [VppRoutePath(next_hop_addr, 0xFFFFFFFF)],
601 )
Neale Ranns097fa662018-05-01 05:17:55 -0700602 r.add_vpp_config()
603 routes.append(r)
604 return routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100605
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200606 def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count, start=0):
Neale Ranns097fa662018-05-01 05:17:55 -0700607 routes = []
608 for i in range(count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200609 r = VppIpRoute(
610 self,
611 start_dest_addr % (i + start),
612 32,
613 [VppRoutePath(next_hop_addr, 0xFFFFFFFF)],
614 )
Neale Ranns097fa662018-05-01 05:17:55 -0700615 r.remove_vpp_config()
616 routes.append(r)
617 return routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100618
Neale Ranns097fa662018-05-01 05:17:55 -0700619 def create_stream(self, src_if, dst_if, routes, count):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100620 pkts = []
621
622 for _ in range(count):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000623 dst_addr = random.choice(routes).prefix.network_address
Klement Sekeradab231a2016-12-21 08:50:14 +0100624 info = self.create_packet_info(src_if, dst_if)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100625 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200626 p = (
627 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
628 / IP(src=src_if.remote_ip4, dst=str(dst_addr))
629 / UDP(sport=1234, dport=1234)
630 / Raw(payload)
631 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100632 info.data = p.copy()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100633 self.extend_packet(p, random.choice(self.pg_if_packet_sizes))
634 pkts.append(p)
635
636 return pkts
637
638 def _find_ip_match(self, find_in, pkt):
639 for p in find_in:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200640 if self.payload_to_info(p[Raw]) == self.payload_to_info(pkt[Raw]):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100641 if p[IP].src != pkt[IP].src:
642 break
643 if p[IP].dst != pkt[IP].dst:
644 break
645 if p[UDP].sport != pkt[UDP].sport:
646 break
647 if p[UDP].dport != pkt[UDP].dport:
648 break
649 return p
650 return None
651
Matej Klotton16a14cd2016-12-07 15:09:13 +0100652 def verify_capture(self, dst_interface, received_pkts, expected_pkts):
653 self.assertEqual(len(received_pkts), len(expected_pkts))
654 to_verify = list(expected_pkts)
655 for p in received_pkts:
656 self.assertEqual(p.src, dst_interface.local_mac)
657 self.assertEqual(p.dst, dst_interface.remote_mac)
658 x = self._find_ip_match(to_verify, p)
659 to_verify.remove(x)
660 self.assertListEqual(to_verify, [])
661
Neale Ranns097fa662018-05-01 05:17:55 -0700662 def verify_route_dump(self, routes):
663 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200664 self.assertTrue(
665 find_route(self, r.prefix.network_address, r.prefix.prefixlen)
666 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100667
Neale Ranns097fa662018-05-01 05:17:55 -0700668 def verify_not_in_route_dump(self, routes):
669 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200670 self.assertFalse(
671 find_route(self, r.prefix.network_address, r.prefix.prefixlen)
672 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100673
674 @classmethod
675 def setUpClass(cls):
676 """
677 #. Create and initialize 3 pg interfaces.
678 #. initialize class attributes configured_routes and deleted_routes
679 to store information between tests.
680 """
681 super(TestIPv4FibCrud, cls).setUpClass()
682
683 try:
684 # create 3 pg interfaces
685 cls.create_pg_interfaces(range(3))
686
687 cls.interfaces = list(cls.pg_interfaces)
688
689 # setup all interfaces
690 for i in cls.interfaces:
691 i.admin_up()
692 i.config_ip4()
693 i.resolve_arp()
694
695 cls.configured_routes = []
696 cls.deleted_routes = []
697 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
698
699 except Exception:
700 super(TestIPv4FibCrud, cls).tearDownClass()
701 raise
702
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700703 @classmethod
704 def tearDownClass(cls):
705 super(TestIPv4FibCrud, cls).tearDownClass()
706
Matej Klotton16a14cd2016-12-07 15:09:13 +0100707 def setUp(self):
708 super(TestIPv4FibCrud, self).setUp()
Klement Sekeradab231a2016-12-21 08:50:14 +0100709 self.reset_packet_infos()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100710
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800711 self.configured_routes = []
712 self.deleted_routes = []
713
Matej Klotton16a14cd2016-12-07 15:09:13 +0100714 def test_1_add_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200715 """Add 1k routes"""
Matej Klotton16a14cd2016-12-07 15:09:13 +0100716
Neale Ranns097fa662018-05-01 05:17:55 -0700717 # add 100 routes check with traffic script.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200718 self.configured_routes.extend(
719 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
720 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100721
Neale Ranns097fa662018-05-01 05:17:55 -0700722 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100723
724 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200725 self.pg1, self.pg0, self.configured_routes, 100
726 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100727 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200728 self.pg2, self.pg0, self.configured_routes, 100
729 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100730 self.pg1.add_stream(self.stream_1)
731 self.pg2.add_stream(self.stream_2)
732
733 self.pg_enable_capture(self.pg_interfaces)
734 self.pg_start()
735
Klement Sekeradab231a2016-12-21 08:50:14 +0100736 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100737 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
738
Matej Klotton16a14cd2016-12-07 15:09:13 +0100739 def test_2_del_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200740 """Delete 100 routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100741
742 - delete 10 routes check with traffic script.
743 """
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800744 # config 1M FIB entries
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200745 self.configured_routes.extend(
746 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
747 )
748 self.deleted_routes.extend(
749 self.unconfig_fib_many_to_one(
750 "10.0.0.%d", self.pg0.remote_ip4, 10, start=10
751 )
752 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100753 for x in self.deleted_routes:
754 self.configured_routes.remove(x)
755
Neale Ranns097fa662018-05-01 05:17:55 -0700756 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100757
758 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200759 self.pg1, self.pg0, self.configured_routes, 100
760 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100761 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200762 self.pg2, self.pg0, self.configured_routes, 100
763 )
764 self.stream_3 = self.create_stream(self.pg1, self.pg0, self.deleted_routes, 100)
765 self.stream_4 = self.create_stream(self.pg2, self.pg0, self.deleted_routes, 100)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100766 self.pg1.add_stream(self.stream_1 + self.stream_3)
767 self.pg2.add_stream(self.stream_2 + self.stream_4)
768 self.pg_enable_capture(self.pg_interfaces)
769 self.pg_start()
770
Klement Sekeradab231a2016-12-21 08:50:14 +0100771 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100772 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
773
774 def test_3_add_new_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200775 """Add 1k routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100776
777 - re-add 5 routes check with traffic script.
778 - add 100 routes check with traffic script.
779 """
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800780 # config 1M FIB entries
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200781 self.configured_routes.extend(
782 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
783 )
784 self.deleted_routes.extend(
785 self.unconfig_fib_many_to_one(
786 "10.0.0.%d", self.pg0.remote_ip4, 10, start=10
787 )
788 )
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800789 for x in self.deleted_routes:
790 self.configured_routes.remove(x)
791
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200792 tmp = self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 5, start=10)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100793 self.configured_routes.extend(tmp)
794 for x in tmp:
795 self.deleted_routes.remove(x)
796
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200797 self.configured_routes.extend(
798 self.config_fib_many_to_one("10.0.1.%d", self.pg0.remote_ip4, 100)
799 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100800
Neale Ranns097fa662018-05-01 05:17:55 -0700801 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100802
803 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200804 self.pg1, self.pg0, self.configured_routes, 300
805 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100806 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200807 self.pg2, self.pg0, self.configured_routes, 300
808 )
809 self.stream_3 = self.create_stream(self.pg1, self.pg0, self.deleted_routes, 100)
810 self.stream_4 = self.create_stream(self.pg2, self.pg0, self.deleted_routes, 100)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100811
812 self.pg1.add_stream(self.stream_1 + self.stream_3)
813 self.pg2.add_stream(self.stream_2 + self.stream_4)
814 self.pg_enable_capture(self.pg_interfaces)
815 self.pg_start()
816
Klement Sekeradab231a2016-12-21 08:50:14 +0100817 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100818 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
819
Neale Ranns097fa662018-05-01 05:17:55 -0700820 # delete 5 routes check with traffic script.
821 # add 100 routes check with traffic script.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200822 self.deleted_routes.extend(
823 self.unconfig_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 15)
824 )
825 self.deleted_routes.extend(
826 self.unconfig_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 85)
827 )
828 self.deleted_routes.extend(
829 self.unconfig_fib_many_to_one("10.0.1.%d", self.pg0.remote_ip4, 100)
830 )
Neale Ranns097fa662018-05-01 05:17:55 -0700831 self.verify_not_in_route_dump(self.deleted_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100832
833
Neale Ranns37be7362017-02-21 17:30:26 -0800834class TestIPNull(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200835 """IPv4 routes via NULL"""
Neale Ranns37be7362017-02-21 17:30:26 -0800836
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700837 @classmethod
838 def setUpClass(cls):
839 super(TestIPNull, cls).setUpClass()
840
841 @classmethod
842 def tearDownClass(cls):
843 super(TestIPNull, cls).tearDownClass()
844
Neale Ranns37be7362017-02-21 17:30:26 -0800845 def setUp(self):
846 super(TestIPNull, self).setUp()
847
848 # create 2 pg interfaces
Neale Ranns3b93be52018-09-07 01:48:54 -0700849 self.create_pg_interfaces(range(2))
Neale Ranns37be7362017-02-21 17:30:26 -0800850
851 for i in self.pg_interfaces:
852 i.admin_up()
853 i.config_ip4()
854 i.resolve_arp()
855
856 def tearDown(self):
857 super(TestIPNull, self).tearDown()
858 for i in self.pg_interfaces:
859 i.unconfig_ip4()
860 i.admin_down()
861
862 def test_ip_null(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200863 """IP NULL route"""
Neale Ranns37be7362017-02-21 17:30:26 -0800864
865 #
866 # A route via IP NULL that will reply with ICMP unreachables
867 #
Neale Ranns097fa662018-05-01 05:17:55 -0700868 ip_unreach = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200869 self,
870 "10.0.0.1",
871 32,
872 [
873 VppRoutePath(
874 "0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_ICMP_UNREACH
875 )
876 ],
877 )
Neale Ranns37be7362017-02-21 17:30:26 -0800878 ip_unreach.add_vpp_config()
879
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200880 p_unreach = (
881 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
882 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1")
883 / UDP(sport=1234, dport=1234)
884 / Raw(b"\xa5" * 100)
885 )
Neale Ranns37be7362017-02-21 17:30:26 -0800886 self.pg0.add_stream(p_unreach)
887 self.pg_enable_capture(self.pg_interfaces)
888 self.pg_start()
889
890 rx = self.pg0.get_capture(1)
891 rx = rx[0]
892 icmp = rx[ICMP]
893
894 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
895 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-unreachable")
896 self.assertEqual(icmp.src, self.pg0.remote_ip4)
897 self.assertEqual(icmp.dst, "10.0.0.1")
898
899 #
900 # ICMP replies are rate limited. so sit and spin.
901 #
902 self.sleep(1)
903
904 #
905 # A route via IP NULL that will reply with ICMP prohibited
906 #
Neale Ranns097fa662018-05-01 05:17:55 -0700907 ip_prohibit = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200908 self,
909 "10.0.0.2",
910 32,
911 [
912 VppRoutePath(
913 "0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_ICMP_PROHIBIT
914 )
915 ],
916 )
Neale Ranns37be7362017-02-21 17:30:26 -0800917 ip_prohibit.add_vpp_config()
918
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200919 p_prohibit = (
920 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
921 / IP(src=self.pg0.remote_ip4, dst="10.0.0.2")
922 / UDP(sport=1234, dport=1234)
923 / Raw(b"\xa5" * 100)
924 )
Neale Ranns37be7362017-02-21 17:30:26 -0800925
926 self.pg0.add_stream(p_prohibit)
927 self.pg_enable_capture(self.pg_interfaces)
928 self.pg_start()
929
930 rx = self.pg0.get_capture(1)
931
932 rx = rx[0]
933 icmp = rx[ICMP]
934
935 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
936 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-prohibited")
937 self.assertEqual(icmp.src, self.pg0.remote_ip4)
938 self.assertEqual(icmp.dst, "10.0.0.2")
939
Neale Ranns3b93be52018-09-07 01:48:54 -0700940 def test_ip_drop(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200941 """IP Drop Routes"""
Neale Ranns3b93be52018-09-07 01:48:54 -0700942
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200943 p = (
944 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
945 / IP(src=self.pg0.remote_ip4, dst="1.1.1.1")
946 / UDP(sport=1234, dport=1234)
947 / Raw(b"\xa5" * 100)
948 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700949
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200950 r1 = VppIpRoute(
951 self,
952 "1.1.1.0",
953 24,
954 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
955 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700956 r1.add_vpp_config()
957
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400958 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
Neale Ranns3b93be52018-09-07 01:48:54 -0700959
960 #
961 # insert a more specific as a drop
962 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200963 r2 = VppIpRoute(
964 self,
965 "1.1.1.1",
966 32,
967 [VppRoutePath("0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP)],
968 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700969 r2.add_vpp_config()
970
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400971 self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS, "Drop Route")
Neale Ranns3b93be52018-09-07 01:48:54 -0700972 r2.remove_vpp_config()
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400973 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
Neale Ranns3b93be52018-09-07 01:48:54 -0700974
Neale Ranns37be7362017-02-21 17:30:26 -0800975
Neale Ranns180279b2017-03-16 15:49:09 -0400976class TestIPDisabled(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200977 """IPv4 disabled"""
Neale Ranns180279b2017-03-16 15:49:09 -0400978
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700979 @classmethod
980 def setUpClass(cls):
981 super(TestIPDisabled, cls).setUpClass()
982
983 @classmethod
984 def tearDownClass(cls):
985 super(TestIPDisabled, cls).tearDownClass()
986
Neale Ranns180279b2017-03-16 15:49:09 -0400987 def setUp(self):
988 super(TestIPDisabled, self).setUp()
989
990 # create 2 pg interfaces
991 self.create_pg_interfaces(range(2))
992
993 # PG0 is IP enalbed
994 self.pg0.admin_up()
995 self.pg0.config_ip4()
996 self.pg0.resolve_arp()
997
998 # PG 1 is not IP enabled
999 self.pg1.admin_up()
1000
1001 def tearDown(self):
1002 super(TestIPDisabled, self).tearDown()
1003 for i in self.pg_interfaces:
1004 i.unconfig_ip4()
1005 i.admin_down()
1006
Neale Ranns180279b2017-03-16 15:49:09 -04001007 def test_ip_disabled(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001008 """IP Disabled"""
Neale Ranns180279b2017-03-16 15:49:09 -04001009
Neale Ranns990f6942020-10-20 07:20:17 +00001010 MRouteItfFlags = VppEnum.vl_api_mfib_itf_flags_t
1011 MRouteEntryFlags = VppEnum.vl_api_mfib_entry_flags_t
1012
Neale Ranns180279b2017-03-16 15:49:09 -04001013 #
1014 # An (S,G).
1015 # one accepting interface, pg0, 2 forwarding interfaces
1016 #
1017 route_232_1_1_1 = VppIpMRoute(
1018 self,
1019 "0.0.0.0",
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001020 "232.1.1.1",
1021 32,
Neale Ranns990f6942020-10-20 07:20:17 +00001022 MRouteEntryFlags.MFIB_API_ENTRY_FLAG_NONE,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001023 [
1024 VppMRoutePath(
1025 self.pg1.sw_if_index, MRouteItfFlags.MFIB_API_ITF_FLAG_ACCEPT
1026 ),
1027 VppMRoutePath(
1028 self.pg0.sw_if_index, MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD
1029 ),
1030 ],
1031 )
Neale Ranns180279b2017-03-16 15:49:09 -04001032 route_232_1_1_1.add_vpp_config()
1033
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001034 pu = (
1035 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1036 / IP(src="10.10.10.10", dst=self.pg0.remote_ip4)
1037 / UDP(sport=1234, dport=1234)
1038 / Raw(b"\xa5" * 100)
1039 )
1040 pm = (
1041 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1042 / IP(src="10.10.10.10", dst="232.1.1.1")
1043 / UDP(sport=1234, dport=1234)
1044 / Raw(b"\xa5" * 100)
1045 )
Neale Ranns180279b2017-03-16 15:49:09 -04001046
1047 #
1048 # PG1 does not forward IP traffic
1049 #
1050 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
1051 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
1052
1053 #
1054 # IP enable PG1
1055 #
1056 self.pg1.config_ip4()
1057
1058 #
1059 # Now we get packets through
1060 #
1061 self.pg1.add_stream(pu)
1062 self.pg_enable_capture(self.pg_interfaces)
1063 self.pg_start()
1064 rx = self.pg0.get_capture(1)
1065
1066 self.pg1.add_stream(pm)
1067 self.pg_enable_capture(self.pg_interfaces)
1068 self.pg_start()
1069 rx = self.pg0.get_capture(1)
1070
1071 #
1072 # Disable PG1
1073 #
1074 self.pg1.unconfig_ip4()
1075
1076 #
1077 # PG1 does not forward IP traffic
1078 #
1079 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
1080 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
1081
1082
Neale Ranns9a69a602017-03-26 10:56:33 -07001083class TestIPSubNets(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001084 """IPv4 Subnets"""
Neale Ranns9a69a602017-03-26 10:56:33 -07001085
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001086 @classmethod
1087 def setUpClass(cls):
1088 super(TestIPSubNets, cls).setUpClass()
1089
1090 @classmethod
1091 def tearDownClass(cls):
1092 super(TestIPSubNets, cls).tearDownClass()
1093
Neale Ranns9a69a602017-03-26 10:56:33 -07001094 def setUp(self):
1095 super(TestIPSubNets, self).setUp()
1096
1097 # create a 2 pg interfaces
1098 self.create_pg_interfaces(range(2))
1099
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001100 # pg0 we will use to experiment
Neale Ranns9a69a602017-03-26 10:56:33 -07001101 self.pg0.admin_up()
1102
1103 # pg1 is setup normally
1104 self.pg1.admin_up()
1105 self.pg1.config_ip4()
1106 self.pg1.resolve_arp()
1107
1108 def tearDown(self):
1109 super(TestIPSubNets, self).tearDown()
1110 for i in self.pg_interfaces:
1111 i.admin_down()
1112
Neale Ranns9a69a602017-03-26 10:56:33 -07001113 def test_ip_sub_nets(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001114 """IP Sub Nets"""
Neale Ranns9a69a602017-03-26 10:56:33 -07001115
1116 #
1117 # Configure a covering route to forward so we know
1118 # when we are dropping
1119 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001120 cover_route = VppIpRoute(
1121 self,
1122 "10.0.0.0",
1123 8,
1124 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
1125 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001126 cover_route.add_vpp_config()
1127
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001128 p = (
1129 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1130 / IP(dst="10.10.10.10", src=self.pg0.local_ip4)
1131 / UDP(sport=1234, dport=1234)
1132 / Raw(b"\xa5" * 100)
1133 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001134
1135 self.pg1.add_stream(p)
1136 self.pg_enable_capture(self.pg_interfaces)
1137 self.pg_start()
1138 rx = self.pg1.get_capture(1)
1139
1140 #
1141 # Configure some non-/24 subnets on an IP interface
1142 #
1143 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
1144
Ole Troan9a475372019-03-05 16:58:24 +01001145 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001146 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/16"
1147 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001148
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001149 pn = (
1150 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1151 / IP(dst="10.10.0.0", src=self.pg0.local_ip4)
1152 / UDP(sport=1234, dport=1234)
1153 / Raw(b"\xa5" * 100)
1154 )
1155 pb = (
1156 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1157 / IP(dst="10.10.255.255", src=self.pg0.local_ip4)
1158 / UDP(sport=1234, dport=1234)
1159 / Raw(b"\xa5" * 100)
1160 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001161
1162 self.send_and_assert_no_replies(self.pg1, pn, "IP Network address")
1163 self.send_and_assert_no_replies(self.pg1, pb, "IP Broadcast address")
1164
1165 # remove the sub-net and we are forwarding via the cover again
Ole Troan9a475372019-03-05 16:58:24 +01001166 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001167 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/16", is_add=0
1168 )
Jakub Grajciar053204a2019-03-18 13:17:53 +01001169
Neale Ranns9a69a602017-03-26 10:56:33 -07001170 self.pg1.add_stream(pn)
1171 self.pg_enable_capture(self.pg_interfaces)
1172 self.pg_start()
1173 rx = self.pg1.get_capture(1)
1174 self.pg1.add_stream(pb)
1175 self.pg_enable_capture(self.pg_interfaces)
1176 self.pg_start()
1177 rx = self.pg1.get_capture(1)
1178
1179 #
1180 # A /31 is a special case where the 'other-side' is an attached host
1181 # packets to that peer generate ARP requests
1182 #
1183 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
1184
Ole Troan9a475372019-03-05 16:58:24 +01001185 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001186 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/31"
1187 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001188
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001189 pn = (
1190 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1191 / IP(dst="10.10.10.11", src=self.pg0.local_ip4)
1192 / UDP(sport=1234, dport=1234)
1193 / Raw(b"\xa5" * 100)
1194 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001195
1196 self.pg1.add_stream(pn)
1197 self.pg_enable_capture(self.pg_interfaces)
1198 self.pg_start()
1199 rx = self.pg0.get_capture(1)
1200 rx[ARP]
1201
1202 # remove the sub-net and we are forwarding via the cover again
Ole Troan9a475372019-03-05 16:58:24 +01001203 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001204 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/31", is_add=0
1205 )
Jakub Grajciar053204a2019-03-18 13:17:53 +01001206
Neale Ranns9a69a602017-03-26 10:56:33 -07001207 self.pg1.add_stream(pn)
1208 self.pg_enable_capture(self.pg_interfaces)
1209 self.pg_start()
1210 rx = self.pg1.get_capture(1)
1211
1212
Neale Ranns227038a2017-04-21 01:07:59 -07001213class TestIPLoadBalance(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001214 """IPv4 Load-Balancing"""
Neale Ranns227038a2017-04-21 01:07:59 -07001215
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001216 @classmethod
1217 def setUpClass(cls):
1218 super(TestIPLoadBalance, cls).setUpClass()
1219
1220 @classmethod
1221 def tearDownClass(cls):
1222 super(TestIPLoadBalance, cls).tearDownClass()
1223
Neale Ranns227038a2017-04-21 01:07:59 -07001224 def setUp(self):
1225 super(TestIPLoadBalance, self).setUp()
1226
1227 self.create_pg_interfaces(range(5))
Neale Ranns15002542017-09-10 04:39:11 -07001228 mpls_tbl = VppMplsTable(self, 0)
1229 mpls_tbl.add_vpp_config()
Neale Ranns227038a2017-04-21 01:07:59 -07001230
1231 for i in self.pg_interfaces:
1232 i.admin_up()
1233 i.config_ip4()
1234 i.resolve_arp()
Neale Ranns71275e32017-05-25 12:38:58 -07001235 i.enable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001236
1237 def tearDown(self):
Neale Ranns227038a2017-04-21 01:07:59 -07001238 for i in self.pg_interfaces:
Neale Ranns71275e32017-05-25 12:38:58 -07001239 i.disable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001240 i.unconfig_ip4()
1241 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -07001242 super(TestIPLoadBalance, self).tearDown()
Neale Ranns227038a2017-04-21 01:07:59 -07001243
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001244 def total_len(self, rxs):
1245 n = 0
1246 for rx in rxs:
1247 n += len(rx)
1248 return n
1249
Neale Ranns227038a2017-04-21 01:07:59 -07001250 def test_ip_load_balance(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001251 """IP Load-Balancing"""
Neale Ranns227038a2017-04-21 01:07:59 -07001252
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001253 fhc = VppEnum.vl_api_ip_flow_hash_config_t
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001254 fhcv2 = VppEnum.vl_api_ip_flow_hash_config_v2_t
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001255 af = VppEnum.vl_api_address_family_t
1256
Neale Ranns227038a2017-04-21 01:07:59 -07001257 #
1258 # An array of packets that differ only in the destination port
1259 #
Neale Ranns71275e32017-05-25 12:38:58 -07001260 port_ip_pkts = []
1261 port_mpls_pkts = []
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001262 port_gtp_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001263
1264 #
1265 # An array of packets that differ only in the source address
1266 #
Neale Ranns71275e32017-05-25 12:38:58 -07001267 src_ip_pkts = []
1268 src_mpls_pkts = []
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001269 src_gtp_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001270
Paul Vinciguerra4271c972019-05-14 13:25:49 -04001271 for ii in range(NUM_PKTS):
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001272 internal_src_ip_hdr = IP(dst="10.0.0.1", src="20.0.0.1")
1273
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001274 port_ip_hdr = (
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001275 internal_src_ip_hdr
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001276 / UDP(sport=1234, dport=1234 + ii)
1277 / Raw(b"\xa5" * 100)
1278 )
1279 port_ip_pkts.append(
1280 (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / port_ip_hdr)
1281 )
1282 port_mpls_pkts.append(
1283 (
1284 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1285 / MPLS(label=66, ttl=2)
1286 / port_ip_hdr
1287 )
1288 )
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001289 port_gtp_pkts.append(
1290 (
1291 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1292 / internal_src_ip_hdr
1293 / UDP(sport=2152, dport=2152, chksum=0)
1294 / GTP_U_Header(gtp_type="g_pdu", teid=200)
1295 / Raw(b"\xa5" * 100)
1296 )
1297 )
Neale Ranns71275e32017-05-25 12:38:58 -07001298
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001299 src_ip_hdr = (
1300 IP(dst="10.0.0.1", src="20.0.0.%d" % ii)
1301 / UDP(sport=1234, dport=1234)
1302 / Raw(b"\xa5" * 100)
1303 )
1304 src_ip_pkts.append(
1305 (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / src_ip_hdr)
1306 )
1307 src_mpls_pkts.append(
1308 (
1309 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1310 / MPLS(label=66, ttl=2)
1311 / src_ip_hdr
1312 )
1313 )
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001314 src_gtp_pkts.append(
1315 (
1316 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1317 / IP(dst="10.0.0.1", src="20.0.0.1")
1318 / UDP(sport=2152, dport=2152, chksum=0)
1319 / GTP_U_Header(gtp_type="g_pdu", teid=ii)
1320 / Raw(b"\xa5" * 100)
1321 )
1322 )
Neale Ranns227038a2017-04-21 01:07:59 -07001323
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001324 route_10_0_0_1 = VppIpRoute(
1325 self,
1326 "10.0.0.1",
1327 32,
1328 [
1329 VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index),
1330 VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index),
1331 ],
1332 )
Neale Ranns227038a2017-04-21 01:07:59 -07001333 route_10_0_0_1.add_vpp_config()
1334
Neale Ranns71275e32017-05-25 12:38:58 -07001335 binding = VppMplsIpBind(self, 66, "10.0.0.1", 32)
1336 binding.add_vpp_config()
1337
Neale Ranns227038a2017-04-21 01:07:59 -07001338 #
1339 # inject the packet on pg0 - expect load-balancing across the 2 paths
1340 # - since the default hash config is to use IP src,dst and port
1341 # src,dst
1342 # We are not going to ensure equal amounts of packets across each link,
1343 # since the hash algorithm is statistical and therefore this can never
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001344 # be guaranteed. But with 64 different packets we do expect some
Neale Ranns227038a2017-04-21 01:07:59 -07001345 # balancing. So instead just ensure there is traffic on each link.
1346 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001347 rx = self.send_and_expect_load_balancing(
1348 self.pg0, port_ip_pkts, [self.pg1, self.pg2]
1349 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001350 n_ip_pg0 = len(rx[0])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001351 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts, [self.pg1, self.pg2])
1352 self.send_and_expect_load_balancing(
1353 self.pg0, port_mpls_pkts, [self.pg1, self.pg2]
1354 )
1355 rx = self.send_and_expect_load_balancing(
1356 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1357 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001358 n_mpls_pg0 = len(rx[0])
1359
1360 #
1361 # change the router ID and expect the distribution changes
1362 #
1363 self.vapi.set_ip_flow_hash_router_id(router_id=0x11111111)
1364
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001365 rx = self.send_and_expect_load_balancing(
1366 self.pg0, port_ip_pkts, [self.pg1, self.pg2]
1367 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001368 self.assertNotEqual(n_ip_pg0, len(rx[0]))
1369
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001370 rx = self.send_and_expect_load_balancing(
1371 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1372 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001373 self.assertNotEqual(n_mpls_pg0, len(rx[0]))
Neale Ranns227038a2017-04-21 01:07:59 -07001374
1375 #
1376 # change the flow hash config so it's only IP src,dst
1377 # - now only the stream with differing source address will
1378 # load-balance
1379 #
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001380 self.vapi.set_ip_flow_hash_v2(
1381 af=af.ADDRESS_IP4,
1382 table_id=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001383 flow_hash_config=(
1384 fhc.IP_API_FLOW_HASH_SRC_IP
1385 | fhc.IP_API_FLOW_HASH_DST_IP
1386 | fhc.IP_API_FLOW_HASH_PROTO
1387 ),
1388 )
Neale Ranns227038a2017-04-21 01:07:59 -07001389
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001390 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts, [self.pg1, self.pg2])
1391 self.send_and_expect_load_balancing(
1392 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1393 )
Neale Ranns227038a2017-04-21 01:07:59 -07001394
Neale Ranns699bea22022-02-17 09:22:16 +00001395 self.send_and_expect_only(self.pg0, port_ip_pkts, self.pg2)
Neale Ranns227038a2017-04-21 01:07:59 -07001396
1397 #
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001398 # this case gtp v1 teid key LB
1399 #
1400 self.vapi.set_ip_flow_hash_v3(
1401 af=af.ADDRESS_IP4,
1402 table_id=0,
1403 flow_hash_config=(
1404 fhcv2.IP_API_V2_FLOW_HASH_SRC_IP
1405 | fhcv2.IP_API_V2_FLOW_HASH_PROTO
1406 | fhcv2.IP_API_V2_FLOW_HASH_GTPV1_TEID
1407 ),
1408 )
1409 self.logger.info(self.vapi.cli("show ip fib"))
1410
1411 self.send_and_expect_load_balancing(
1412 self.pg0, src_gtp_pkts, [self.pg1, self.pg2]
1413 )
1414
1415 self.send_and_expect_only(self.pg0, port_gtp_pkts, self.pg2)
1416
1417 #
Neale Ranns227038a2017-04-21 01:07:59 -07001418 # change the flow hash config back to defaults
1419 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001420 self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, proto=1, sport=1, dport=1)
Neale Ranns227038a2017-04-21 01:07:59 -07001421
1422 #
1423 # Recursive prefixes
1424 # - testing that 2 stages of load-balancing occurs and there is no
1425 # polarisation (i.e. only 2 of 4 paths are used)
1426 #
1427 port_pkts = []
1428 src_pkts = []
1429
1430 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001431 port_pkts.append(
1432 (
1433 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1434 / IP(dst="1.1.1.1", src="20.0.0.1")
1435 / UDP(sport=1234, dport=1234 + ii)
1436 / Raw(b"\xa5" * 100)
1437 )
1438 )
1439 src_pkts.append(
1440 (
1441 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1442 / IP(dst="1.1.1.1", src="20.0.0.%d" % ii)
1443 / UDP(sport=1234, dport=1234)
1444 / Raw(b"\xa5" * 100)
1445 )
1446 )
Neale Ranns227038a2017-04-21 01:07:59 -07001447
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001448 route_10_0_0_2 = VppIpRoute(
1449 self,
1450 "10.0.0.2",
1451 32,
1452 [
1453 VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index),
1454 VppRoutePath(self.pg4.remote_ip4, self.pg4.sw_if_index),
1455 ],
1456 )
Neale Ranns227038a2017-04-21 01:07:59 -07001457 route_10_0_0_2.add_vpp_config()
1458
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001459 route_1_1_1_1 = VppIpRoute(
1460 self,
1461 "1.1.1.1",
1462 32,
1463 [
1464 VppRoutePath("10.0.0.2", 0xFFFFFFFF),
1465 VppRoutePath("10.0.0.1", 0xFFFFFFFF),
1466 ],
1467 )
Neale Ranns227038a2017-04-21 01:07:59 -07001468 route_1_1_1_1.add_vpp_config()
1469
1470 #
1471 # inject the packet on pg0 - expect load-balancing across all 4 paths
1472 #
1473 self.vapi.cli("clear trace")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001474 self.send_and_expect_load_balancing(
1475 self.pg0, port_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1476 )
1477 self.send_and_expect_load_balancing(
1478 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1479 )
Neale Ranns227038a2017-04-21 01:07:59 -07001480
Neale Ranns42e6b092017-07-31 02:56:03 -07001481 #
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001482 # bring down pg1 expect LB to adjust to use only those that are up
Neale Ranns63480742019-03-13 06:41:52 -07001483 #
1484 self.pg1.link_down()
1485
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001486 rx = self.send_and_expect_load_balancing(
1487 self.pg0, src_pkts, [self.pg2, self.pg3, self.pg4]
1488 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001489 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001490
1491 #
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001492 # bring down pg2 expect LB to adjust to use only those that are up
Neale Ranns63480742019-03-13 06:41:52 -07001493 #
1494 self.pg2.link_down()
1495
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001496 rx = self.send_and_expect_load_balancing(
1497 self.pg0, src_pkts, [self.pg3, self.pg4]
1498 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001499 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001500
1501 #
1502 # bring the links back up - expect LB over all again
1503 #
1504 self.pg1.link_up()
1505 self.pg2.link_up()
1506
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001507 rx = self.send_and_expect_load_balancing(
1508 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1509 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001510 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001511
1512 #
1513 # The same link-up/down but this time admin state
1514 #
1515 self.pg1.admin_down()
1516 self.pg2.admin_down()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001517 rx = self.send_and_expect_load_balancing(
1518 self.pg0, src_pkts, [self.pg3, self.pg4]
1519 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001520 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001521 self.pg1.admin_up()
1522 self.pg2.admin_up()
1523 self.pg1.resolve_arp()
1524 self.pg2.resolve_arp()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001525 rx = self.send_and_expect_load_balancing(
1526 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1527 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001528 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001529
1530 #
Neale Ranns42e6b092017-07-31 02:56:03 -07001531 # Recursive prefixes
1532 # - testing that 2 stages of load-balancing, no choices
1533 #
1534 port_pkts = []
1535
1536 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001537 port_pkts.append(
1538 (
1539 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1540 / IP(dst="1.1.1.2", src="20.0.0.2")
1541 / UDP(sport=1234, dport=1234 + ii)
1542 / Raw(b"\xa5" * 100)
1543 )
1544 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001545
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001546 route_10_0_0_3 = VppIpRoute(
1547 self,
1548 "10.0.0.3",
1549 32,
1550 [VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index)],
1551 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001552 route_10_0_0_3.add_vpp_config()
1553
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001554 route_1_1_1_2 = VppIpRoute(
1555 self, "1.1.1.2", 32, [VppRoutePath("10.0.0.3", 0xFFFFFFFF)]
1556 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001557 route_1_1_1_2.add_vpp_config()
1558
1559 #
Neale Ranns63480742019-03-13 06:41:52 -07001560 # inject the packet on pg0 - rx only on via routes output interface
Neale Ranns42e6b092017-07-31 02:56:03 -07001561 #
1562 self.vapi.cli("clear trace")
Neale Ranns699bea22022-02-17 09:22:16 +00001563 self.send_and_expect_only(self.pg0, port_pkts, self.pg3)
Neale Ranns42e6b092017-07-31 02:56:03 -07001564
Neale Ranns63480742019-03-13 06:41:52 -07001565 #
1566 # Add a LB route in the presence of a down link - expect no
1567 # packets over the down link
1568 #
1569 self.pg3.link_down()
1570
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001571 route_10_0_0_3 = VppIpRoute(
1572 self,
1573 "10.0.0.3",
1574 32,
1575 [
1576 VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index),
1577 VppRoutePath(self.pg4.remote_ip4, self.pg4.sw_if_index),
1578 ],
1579 )
Neale Ranns63480742019-03-13 06:41:52 -07001580 route_10_0_0_3.add_vpp_config()
1581
1582 port_pkts = []
1583 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001584 port_pkts.append(
1585 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1586 / IP(dst="10.0.0.3", src="20.0.0.2")
1587 / UDP(sport=1234, dport=1234 + ii)
1588 / Raw(b"\xa5" * 100)
1589 )
Neale Ranns63480742019-03-13 06:41:52 -07001590
Neale Ranns699bea22022-02-17 09:22:16 +00001591 self.send_and_expect_only(self.pg0, port_pkts, self.pg4)
Neale Ranns63480742019-03-13 06:41:52 -07001592
1593 # bring the link back up
1594 self.pg3.link_up()
1595
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001596 rx = self.send_and_expect_load_balancing(
1597 self.pg0, port_pkts, [self.pg3, self.pg4]
1598 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001599 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001600
Neale Ranns30d0fd42017-05-30 07:30:04 -07001601
1602class TestIPVlan0(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001603 """IPv4 VLAN-0"""
Neale Ranns30d0fd42017-05-30 07:30:04 -07001604
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001605 @classmethod
1606 def setUpClass(cls):
1607 super(TestIPVlan0, cls).setUpClass()
1608
1609 @classmethod
1610 def tearDownClass(cls):
1611 super(TestIPVlan0, cls).tearDownClass()
1612
Neale Ranns30d0fd42017-05-30 07:30:04 -07001613 def setUp(self):
1614 super(TestIPVlan0, self).setUp()
1615
1616 self.create_pg_interfaces(range(2))
Neale Ranns15002542017-09-10 04:39:11 -07001617 mpls_tbl = VppMplsTable(self, 0)
1618 mpls_tbl.add_vpp_config()
Neale Ranns30d0fd42017-05-30 07:30:04 -07001619
1620 for i in self.pg_interfaces:
1621 i.admin_up()
1622 i.config_ip4()
1623 i.resolve_arp()
1624 i.enable_mpls()
1625
1626 def tearDown(self):
Neale Ranns30d0fd42017-05-30 07:30:04 -07001627 for i in self.pg_interfaces:
1628 i.disable_mpls()
1629 i.unconfig_ip4()
1630 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -07001631 super(TestIPVlan0, self).tearDown()
Neale Ranns30d0fd42017-05-30 07:30:04 -07001632
Neale Ranns30d0fd42017-05-30 07:30:04 -07001633 def test_ip_vlan_0(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001634 """IP VLAN-0"""
Neale Ranns30d0fd42017-05-30 07:30:04 -07001635
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001636 pkts = (
1637 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1638 / Dot1Q(vlan=0)
1639 / IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4)
1640 / UDP(sport=1234, dport=1234)
1641 / Raw(b"\xa5" * 100)
1642 ) * NUM_PKTS
Neale Ranns30d0fd42017-05-30 07:30:04 -07001643
1644 #
1645 # Expect that packets sent on VLAN-0 are forwarded on the
1646 # main interface.
1647 #
1648 self.send_and_expect(self.pg0, pkts, self.pg1)
1649
1650
Brian Russell318fdb82021-01-19 16:56:32 +00001651class IPPuntSetup(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001652 """Setup for IPv4 Punt Police/Redirect"""
Neale Rannsd91c1db2017-07-31 02:30:50 -07001653
Brian Russell318fdb82021-01-19 16:56:32 +00001654 def punt_setup(self):
Pavel Kotucek609e1212018-11-27 09:59:44 +01001655 self.create_pg_interfaces(range(4))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001656
1657 for i in self.pg_interfaces:
1658 i.admin_up()
1659 i.config_ip4()
1660 i.resolve_arp()
1661
Neale Ranns68577d22019-06-04 13:31:23 +00001662 # use UDP packet that have a port we need to explicitly
1663 # register to get punted.
1664 pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4
1665 af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4
1666 udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP
1667 punt_udp = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001668 "type": pt_l4,
1669 "punt": {
1670 "l4": {
1671 "af": af_ip4,
1672 "protocol": udp_proto,
1673 "port": 1234,
Neale Ranns68577d22019-06-04 13:31:23 +00001674 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001675 },
Neale Ranns68577d22019-06-04 13:31:23 +00001676 }
1677
1678 self.vapi.set_punt(is_add=1, punt=punt_udp)
1679
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001680 af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6
1681 punt_udp = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001682 "type": pt_l4,
1683 "punt": {
1684 "l4": {
1685 "af": af_ip6,
1686 "protocol": udp_proto,
1687 "port": 1236,
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001688 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001689 },
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001690 }
1691
1692 self.vapi.set_punt(is_add=1, punt=punt_udp)
1693
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001694 self.pkt = (
1695 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1696 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1697 / UDP(sport=1234, dport=1234)
1698 / Raw(b"\xa5" * 100)
1699 )
Neale Rannsd91c1db2017-07-31 02:30:50 -07001700
Brian Russell318fdb82021-01-19 16:56:32 +00001701 def punt_teardown(self):
1702 for i in self.pg_interfaces:
1703 i.unconfig_ip4()
1704 i.admin_down()
1705
1706
1707class TestIPPunt(IPPuntSetup, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001708 """IPv4 Punt Police/Redirect"""
Brian Russell318fdb82021-01-19 16:56:32 +00001709
1710 def setUp(self):
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001711 super().setUp()
1712 super().punt_setup()
Brian Russell318fdb82021-01-19 16:56:32 +00001713
1714 def tearDown(self):
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001715 super().punt_teardown()
1716 super().tearDown()
1717
1718 def test_ip_punt_api_validation(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001719 """IP punt API parameter validation"""
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001720
1721 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001722 punt = {
1723 "rx_sw_if_index": self.pg0.sw_if_index,
1724 "af": VppEnum.vl_api_address_family_t.ADDRESS_IP4,
1725 "n_paths": 1000000,
1726 "paths": [],
1727 }
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001728
1729 with self.assertRaises(vpp_papi.VPPIOError):
1730 self.vapi.add_del_ip_punt_redirect_v2(punt=punt, is_add=True)
1731
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001732 punt = {
1733 "rx_sw_if_index": self.pg0.sw_if_index,
1734 "af": VppEnum.vl_api_address_family_t.ADDRESS_IP4,
1735 "n_paths": 0,
1736 "paths": [],
1737 }
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001738
1739 self.vapi.add_del_ip_punt_redirect_v2(punt=punt, is_add=True)
Brian Russell318fdb82021-01-19 16:56:32 +00001740
1741 def test_ip_punt(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001742 """IP punt police and redirect"""
Brian Russell318fdb82021-01-19 16:56:32 +00001743
1744 pkts = self.pkt * 1025
Neale Rannsd91c1db2017-07-31 02:30:50 -07001745
1746 #
1747 # Configure a punt redirect via pg1.
1748 #
Ole Troan0bcad322018-12-11 13:04:01 +01001749 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001750 ip_punt_redirect = VppIpPuntRedirect(
1751 self, self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr
1752 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001753 ip_punt_redirect.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001754
1755 self.send_and_expect(self.pg0, pkts, self.pg1)
1756
1757 #
1758 # add a policer
1759 #
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001760 policer = VppPolicer(self, "ip4-punt", 400, 0, 10, 0, rate_type=1)
1761 policer.add_vpp_config()
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001762 ip_punt_policer = VppIpPuntPolicer(self, policer.policer_index)
1763 ip_punt_policer.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001764
1765 self.vapi.cli("clear trace")
1766 self.pg0.add_stream(pkts)
1767 self.pg_enable_capture(self.pg_interfaces)
1768 self.pg_start()
1769
1770 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001771 # the number of packet received should be greater than 0,
Neale Rannsd91c1db2017-07-31 02:30:50 -07001772 # but not equal to the number sent, since some were policed
1773 #
1774 rx = self.pg1._get_capture(1)
Brian Russelle9887262021-01-27 14:45:22 +00001775
1776 stats = policer.get_stats()
1777
1778 # Single rate policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001779 self.assertGreater(stats["conform_packets"], 0)
1780 self.assertEqual(stats["exceed_packets"], 0)
1781 self.assertGreater(stats["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00001782
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08001783 self.assertGreater(len(rx), 0)
1784 self.assertLess(len(rx), len(pkts))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001785
1786 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001787 # remove the policer. back to full rx
Neale Rannsd91c1db2017-07-31 02:30:50 -07001788 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001789 ip_punt_policer.remove_vpp_config()
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001790 policer.remove_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001791 self.send_and_expect(self.pg0, pkts, self.pg1)
1792
1793 #
1794 # remove the redirect. expect full drop.
1795 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001796 ip_punt_redirect.remove_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001797 self.send_and_assert_no_replies(self.pg0, pkts, "IP no punt config")
Neale Rannsd91c1db2017-07-31 02:30:50 -07001798
1799 #
1800 # Add a redirect that is not input port selective
1801 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001802 ip_punt_redirect = VppIpPuntRedirect(
1803 self, 0xFFFFFFFF, self.pg1.sw_if_index, nh_addr
1804 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001805 ip_punt_redirect.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001806 self.send_and_expect(self.pg0, pkts, self.pg1)
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001807 ip_punt_redirect.remove_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001808
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001809 def test_ip_punt_vrf(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001810 """IP punt/local with VRFs"""
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001811
1812 # use a punt redirect to test if for-us packets are accepted
1813 pkts = self.pkt * 1025
1814
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001815 vlans_pg0 = [VppDot1QSubint(self, self.pg0, v) for v in range(100, 104)]
1816 vlans_pg1 = [VppDot1QSubint(self, self.pg1, v) for v in range(100, 104)]
1817 tbl4 = [VppIpTable(self, v).add_vpp_config() for v in range(100, 104)]
1818 tbl6 = [VppIpTable(self, v, True).add_vpp_config() for v in range(100, 104)]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001819
1820 for v in vlans_pg0 + vlans_pg1:
1821 v.admin_up()
1822 v.set_table_ip4(v.vlan)
1823 v.set_table_ip6(v.vlan)
1824 v.config_ip4()
1825 v.config_ip6()
1826 v.resolve_arp()
1827 v.resolve_ndp()
1828
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001829 [
1830 VppIpPuntRedirect(
1831 self,
1832 vlans_pg0[i].sw_if_index,
1833 vlans_pg1[i].sw_if_index,
1834 vlans_pg1[i].remote_ip4,
1835 ).add_vpp_config()
1836 for i in range(4)
1837 ]
1838 [
1839 VppIpPuntRedirect(
1840 self,
1841 vlans_pg0[i].sw_if_index,
1842 vlans_pg1[i].sw_if_index,
1843 vlans_pg1[i].remote_ip6,
1844 ).add_vpp_config()
1845 for i in range(4)
1846 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001847
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001848 pkts = [
1849 (
1850 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1851 / Dot1Q(vlan=i.vlan)
1852 / IP(src=i.remote_ip4, dst=i.local_ip4)
1853 / UDP(sport=1234, dport=1234)
1854 / Raw(b"\xa5" * 100)
1855 )
1856 for i in vlans_pg0
1857 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001858
1859 self.send_and_expect(self.pg0, pkts, self.pg1)
1860
1861 #
1862 # IPv4
1863 #
1864
1865 # we reject packets for source addresses in the wrong vlan/VRF
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001866 pkts = [
1867 (
1868 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1869 / Dot1Q(vlan=i.vlan)
1870 / IP(src="1.1.1.1", dst=i.local_ip4)
1871 / UDP(sport=1234, dport=1234)
1872 / Raw(b"\xa5" * 100)
1873 )
1874 for i in vlans_pg0
1875 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001876 # single and dual loop
1877 self.send_and_assert_no_replies(self.pg0, [pkts[0]])
1878 self.send_and_assert_no_replies(self.pg0, pkts)
1879
Neale Rannse22a7042022-08-09 03:03:29 +00001880 self.assert_error_counter_equal("/err/ip4-local/src_lookup_miss", len(pkts) + 1)
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001881
1882 # using the same source in different tables, should reject
1883 # for the table that the source is not present in
1884 # the first packet in the stream is drop
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001885 pkts = [
1886 (
1887 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1888 / Dot1Q(vlan=i.vlan)
1889 / IP(src=vlans_pg0[0].remote_ip4, dst=i.local_ip4)
1890 / UDP(sport=1234, dport=1234)
1891 / Raw(b"\xa5" * 100)
1892 )
1893 for i in vlans_pg0
1894 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001895 # single loop accept and drop
1896 # followed by both in the same frame/loop
1897 self.send_and_expect(self.pg0, [pkts[0]], self.pg1)
1898 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1899 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1900
1901 # using the same source in different tables, should reject
1902 # for the table that the source is not present in
1903 # the first packet in the stream is accept
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001904 pkts = [
1905 (
1906 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1907 / Dot1Q(vlan=i.vlan)
1908 / IP(src=vlans_pg0[3].remote_ip4, dst=i.local_ip4)
1909 / UDP(sport=1234, dport=1234)
1910 / Raw(b"\xa5" * 100)
1911 )
1912 for i in vlans_pg0
1913 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001914
1915 # single loop accept and drop
1916 # followed by both in the same frame/loop
1917 self.send_and_expect(self.pg0, [pkts[3]], self.pg1)
1918 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1919 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1920
1921 #
1922 # IPv6
1923 #
1924
1925 # we reject packets for source addresses in the wrong vlan/VRF
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001926 pkts = [
1927 (
1928 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1929 / Dot1Q(vlan=i.vlan)
1930 / IPv6(src="1::1", dst=i.local_ip6)
1931 / UDP(sport=1236, dport=1236)
1932 / Raw(b"\xa5" * 100)
1933 )
1934 for i in vlans_pg0
1935 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001936 # single and dual loop
1937 self.send_and_assert_no_replies(self.pg0, [pkts[0]])
1938 self.send_and_assert_no_replies(self.pg0, pkts)
1939
Neale Rannse22a7042022-08-09 03:03:29 +00001940 self.assert_error_counter_equal("/err/ip6-input/src_lookup_miss", len(pkts) + 1)
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001941
1942 # using the same source in different tables, should reject
1943 # for the table that the source is not present in
1944 # the first packet in the stream is drop
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001945 pkts = [
1946 (
1947 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1948 / Dot1Q(vlan=i.vlan)
1949 / IPv6(src=vlans_pg0[0].remote_ip6, dst=i.local_ip6)
1950 / UDP(sport=1236, dport=1236)
1951 / Raw(b"\xa5" * 100)
1952 )
1953 for i in vlans_pg0
1954 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001955 # single loop accept and drop
1956 # followed by both in the same frame/loop
1957 self.send_and_expect(self.pg0, [pkts[0]], self.pg1)
1958 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1959 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1960
1961 # using the same source in different tables, should reject
1962 # for the table that the source is not present in
1963 # the first packet in the stream is accept
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001964 pkts = [
1965 (
1966 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1967 / Dot1Q(vlan=i.vlan)
1968 / IPv6(src=vlans_pg0[3].remote_ip6, dst=i.local_ip6)
1969 / UDP(sport=1236, dport=1236)
1970 / Raw(b"\xa5" * 100)
1971 )
1972 for i in vlans_pg0
1973 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001974
1975 # single loop accept and drop
1976 # followed by both in the same frame/loop
1977 self.send_and_expect(self.pg0, [pkts[3]], self.pg1)
1978 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1979 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1980
1981 for v in vlans_pg0 + vlans_pg1:
1982 v.unconfig_ip4()
1983 v.unconfig_ip6()
1984 v.set_table_ip4(0)
1985 v.set_table_ip6(0)
1986
Pavel Kotucek609e1212018-11-27 09:59:44 +01001987 def test_ip_punt_dump(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001988 """IP4 punt redirect dump"""
Pavel Kotucek609e1212018-11-27 09:59:44 +01001989
1990 #
1991 # Configure a punt redirects
1992 #
Ole Troan0bcad322018-12-11 13:04:01 +01001993 nh_address = self.pg3.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001994 ipr_03 = VppIpPuntRedirect(
1995 self, self.pg0.sw_if_index, self.pg3.sw_if_index, nh_address
1996 )
1997 ipr_13 = VppIpPuntRedirect(
1998 self, self.pg1.sw_if_index, self.pg3.sw_if_index, nh_address
1999 )
2000 ipr_23 = VppIpPuntRedirect(
2001 self, self.pg2.sw_if_index, self.pg3.sw_if_index, "0.0.0.0"
2002 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01002003 ipr_03.add_vpp_config()
2004 ipr_13.add_vpp_config()
2005 ipr_23.add_vpp_config()
Pavel Kotucek609e1212018-11-27 09:59:44 +01002006
2007 #
2008 # Dump pg0 punt redirects
2009 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01002010 self.assertTrue(ipr_03.query_vpp_config())
2011 self.assertTrue(ipr_13.query_vpp_config())
2012 self.assertTrue(ipr_23.query_vpp_config())
Pavel Kotucek609e1212018-11-27 09:59:44 +01002013
2014 #
2015 # Dump punt redirects for all interfaces
2016 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002017 punts = self.vapi.ip_punt_redirect_dump(0xFFFFFFFF)
Pavel Kotucek609e1212018-11-27 09:59:44 +01002018 self.assertEqual(len(punts), 3)
2019 for p in punts:
2020 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
Ole Troan0bcad322018-12-11 13:04:01 +01002021 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002022 self.assertEqual(str(punts[2].punt.nh), "0.0.0.0")
Pavel Kotucek609e1212018-11-27 09:59:44 +01002023
Neale Rannsd91c1db2017-07-31 02:30:50 -07002024
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002025class TestIPPuntHandoff(IPPuntSetup, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002026 """IPv4 Punt Policer thread handoff"""
2027
Klement Sekera8d815022021-03-15 16:58:10 +01002028 vpp_worker_count = 2
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002029
2030 def setUp(self):
2031 super(TestIPPuntHandoff, self).setUp()
2032 super(TestIPPuntHandoff, self).punt_setup()
2033
2034 def tearDown(self):
2035 super(TestIPPuntHandoff, self).punt_teardown()
2036 super(TestIPPuntHandoff, self).tearDown()
2037
2038 def test_ip_punt_policer_handoff(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002039 """IP4 punt policer thread handoff"""
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002040 pkts = self.pkt * NUM_PKTS
2041
2042 #
2043 # Configure a punt redirect via pg1.
2044 #
2045 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002046 ip_punt_redirect = VppIpPuntRedirect(
2047 self, self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr
2048 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002049 ip_punt_redirect.add_vpp_config()
2050
2051 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002052 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
2053 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002054 #
2055 # This policer drops no packets, we are just
2056 # testing that they get to the right thread.
2057 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002058 policer = VppPolicer(
2059 self,
2060 "ip4-punt",
2061 400,
2062 0,
2063 10,
2064 0,
2065 1,
2066 0,
2067 0,
2068 False,
2069 action_tx,
2070 action_tx,
2071 action_tx,
2072 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002073 policer.add_vpp_config()
2074 ip_punt_policer = VppIpPuntPolicer(self, policer.policer_index)
2075 ip_punt_policer.add_vpp_config()
2076
2077 for worker in [0, 1]:
2078 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
Brian Russellbb983142021-02-10 13:56:06 +00002079 self.logger.debug(self.vapi.cli("show trace max 100"))
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002080
Brian Russelle9887262021-01-27 14:45:22 +00002081 # Combined stats, all threads
2082 stats = policer.get_stats()
2083
2084 # Single rate policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002085 self.assertGreater(stats["conform_packets"], 0)
2086 self.assertEqual(stats["exceed_packets"], 0)
2087 self.assertGreater(stats["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00002088
2089 # Worker 0, should have done all the policing
2090 stats0 = policer.get_stats(worker=0)
2091 self.assertEqual(stats, stats0)
2092
2093 # Worker 1, should have handed everything off
2094 stats1 = policer.get_stats(worker=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002095 self.assertEqual(stats1["conform_packets"], 0)
2096 self.assertEqual(stats1["exceed_packets"], 0)
2097 self.assertEqual(stats1["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00002098
Brian Russellbb983142021-02-10 13:56:06 +00002099 # Bind the policer to worker 1 and repeat
2100 policer.bind_vpp_config(1, True)
2101 for worker in [0, 1]:
2102 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
2103 self.logger.debug(self.vapi.cli("show trace max 100"))
2104
2105 # The 2 workers should now have policed the same amount
2106 stats = policer.get_stats()
2107 stats0 = policer.get_stats(worker=0)
2108 stats1 = policer.get_stats(worker=1)
2109
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002110 self.assertGreater(stats0["conform_packets"], 0)
2111 self.assertEqual(stats0["exceed_packets"], 0)
2112 self.assertGreater(stats0["violate_packets"], 0)
Brian Russellbb983142021-02-10 13:56:06 +00002113
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002114 self.assertGreater(stats1["conform_packets"], 0)
2115 self.assertEqual(stats1["exceed_packets"], 0)
2116 self.assertGreater(stats1["violate_packets"], 0)
Brian Russellbb983142021-02-10 13:56:06 +00002117
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002118 self.assertEqual(
2119 stats0["conform_packets"] + stats1["conform_packets"],
2120 stats["conform_packets"],
2121 )
Brian Russellbb983142021-02-10 13:56:06 +00002122
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002123 self.assertEqual(
2124 stats0["violate_packets"] + stats1["violate_packets"],
2125 stats["violate_packets"],
2126 )
Brian Russellbb983142021-02-10 13:56:06 +00002127
2128 # Unbind the policer and repeat
2129 policer.bind_vpp_config(1, False)
2130 for worker in [0, 1]:
2131 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
2132 self.logger.debug(self.vapi.cli("show trace max 100"))
2133
2134 # The policer should auto-bind to worker 0 when packets arrive
2135 stats = policer.get_stats()
2136 stats0new = policer.get_stats(worker=0)
2137 stats1new = policer.get_stats(worker=1)
2138
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002139 self.assertGreater(stats0new["conform_packets"], stats0["conform_packets"])
2140 self.assertEqual(stats0new["exceed_packets"], 0)
2141 self.assertGreater(stats0new["violate_packets"], stats0["violate_packets"])
Brian Russellbb983142021-02-10 13:56:06 +00002142
2143 self.assertEqual(stats1, stats1new)
2144
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002145 #
2146 # Clean up
2147 #
2148 ip_punt_policer.remove_vpp_config()
2149 policer.remove_vpp_config()
2150 ip_punt_redirect.remove_vpp_config()
2151
2152
Neale Ranns054c03a2017-10-13 05:15:07 -07002153class TestIPDeag(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002154 """IPv4 Deaggregate Routes"""
Neale Ranns054c03a2017-10-13 05:15:07 -07002155
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002156 @classmethod
2157 def setUpClass(cls):
2158 super(TestIPDeag, cls).setUpClass()
2159
2160 @classmethod
2161 def tearDownClass(cls):
2162 super(TestIPDeag, cls).tearDownClass()
2163
Neale Ranns054c03a2017-10-13 05:15:07 -07002164 def setUp(self):
2165 super(TestIPDeag, self).setUp()
2166
2167 self.create_pg_interfaces(range(3))
2168
2169 for i in self.pg_interfaces:
2170 i.admin_up()
2171 i.config_ip4()
2172 i.resolve_arp()
2173
2174 def tearDown(self):
2175 super(TestIPDeag, self).tearDown()
2176 for i in self.pg_interfaces:
2177 i.unconfig_ip4()
2178 i.admin_down()
2179
Neale Ranns054c03a2017-10-13 05:15:07 -07002180 def test_ip_deag(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002181 """IP Deag Routes"""
Neale Ranns054c03a2017-10-13 05:15:07 -07002182
2183 #
2184 # Create a table to be used for:
2185 # 1 - another destination address lookup
2186 # 2 - a source address lookup
2187 #
2188 table_dst = VppIpTable(self, 1)
2189 table_src = VppIpTable(self, 2)
2190 table_dst.add_vpp_config()
2191 table_src.add_vpp_config()
2192
2193 #
2194 # Add a route in the default table to point to a deag/
2195 # second lookup in each of these tables
2196 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002197 route_to_dst = VppIpRoute(
2198 self, "1.1.1.1", 32, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=1)]
2199 )
Neale Ranns097fa662018-05-01 05:17:55 -07002200 route_to_src = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002201 self,
2202 "1.1.1.2",
2203 32,
2204 [
2205 VppRoutePath(
2206 "0.0.0.0",
2207 0xFFFFFFFF,
2208 nh_table_id=2,
2209 type=FibPathType.FIB_PATH_TYPE_SOURCE_LOOKUP,
2210 )
2211 ],
2212 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002213 route_to_dst.add_vpp_config()
2214 route_to_src.add_vpp_config()
2215
2216 #
2217 # packets to these destination are dropped, since they'll
2218 # hit the respective default routes in the second table
2219 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002220 p_dst = (
2221 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2222 / IP(src="5.5.5.5", dst="1.1.1.1")
2223 / TCP(sport=1234, dport=1234)
2224 / Raw(b"\xa5" * 100)
2225 )
2226 p_src = (
2227 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2228 / IP(src="2.2.2.2", dst="1.1.1.2")
2229 / TCP(sport=1234, dport=1234)
2230 / Raw(b"\xa5" * 100)
2231 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002232 pkts_dst = p_dst * 257
2233 pkts_src = p_src * 257
2234
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002235 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in dst table")
2236 self.send_and_assert_no_replies(self.pg0, pkts_src, "IP in src table")
Neale Ranns054c03a2017-10-13 05:15:07 -07002237
2238 #
2239 # add a route in the dst table to forward via pg1
2240 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002241 route_in_dst = VppIpRoute(
2242 self,
2243 "1.1.1.1",
2244 32,
2245 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
2246 table_id=1,
2247 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002248 route_in_dst.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -07002249
Neale Ranns054c03a2017-10-13 05:15:07 -07002250 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2251
2252 #
2253 # add a route in the src table to forward via pg2
2254 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002255 route_in_src = VppIpRoute(
2256 self,
2257 "2.2.2.2",
2258 32,
2259 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
2260 table_id=2,
2261 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002262 route_in_src.add_vpp_config()
2263 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2264
Neale Rannsce9e0b42018-08-01 12:53:17 -07002265 #
2266 # loop in the lookup DP
2267 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002268 route_loop = VppIpRoute(
2269 self, "2.2.2.3", 32, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)]
2270 )
Neale Rannsce9e0b42018-08-01 12:53:17 -07002271 route_loop.add_vpp_config()
2272
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002273 p_l = (
2274 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2275 / IP(src="2.2.2.4", dst="2.2.2.3")
2276 / TCP(sport=1234, dport=1234)
2277 / Raw(b"\xa5" * 100)
2278 )
Neale Rannsce9e0b42018-08-01 12:53:17 -07002279
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002280 self.send_and_assert_no_replies(self.pg0, p_l * 257, "IP lookup loop")
Neale Rannsce9e0b42018-08-01 12:53:17 -07002281
Neale Ranns054c03a2017-10-13 05:15:07 -07002282
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002283class TestIPInput(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002284 """IPv4 Input Exceptions"""
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002285
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002286 @classmethod
2287 def setUpClass(cls):
2288 super(TestIPInput, cls).setUpClass()
2289
2290 @classmethod
2291 def tearDownClass(cls):
2292 super(TestIPInput, cls).tearDownClass()
2293
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002294 def setUp(self):
2295 super(TestIPInput, self).setUp()
2296
2297 self.create_pg_interfaces(range(2))
2298
2299 for i in self.pg_interfaces:
2300 i.admin_up()
2301 i.config_ip4()
2302 i.resolve_arp()
2303
2304 def tearDown(self):
2305 super(TestIPInput, self).tearDown()
2306 for i in self.pg_interfaces:
2307 i.unconfig_ip4()
2308 i.admin_down()
2309
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002310 def test_ip_input(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002311 """IP Input Exceptions"""
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002312
2313 # i can't find a way in scapy to construct an IP packet
2314 # with a length less than the IP header length
2315
2316 #
2317 # Packet too short - this is forwarded
2318 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002319 p_short = (
2320 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2321 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, len=40)
2322 / UDP(sport=1234, dport=1234)
2323 / Raw(b"\xa5" * 100)
2324 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002325
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002326 rx = self.send_and_expect(self.pg0, p_short * NUM_PKTS, self.pg1)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002327
2328 #
2329 # Packet too long - this is dropped
2330 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002331 p_long = (
2332 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2333 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, len=400)
2334 / UDP(sport=1234, dport=1234)
2335 / Raw(b"\xa5" * 100)
2336 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002337
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002338 rx = self.send_and_assert_no_replies(self.pg0, p_long * NUM_PKTS, "too long")
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002339
2340 #
2341 # bad chksum - this is dropped
2342 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002343 p_chksum = (
2344 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2345 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, chksum=400)
2346 / UDP(sport=1234, dport=1234)
2347 / Raw(b"\xa5" * 100)
2348 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002349
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002350 rx = self.send_and_assert_no_replies(
2351 self.pg0, p_chksum * NUM_PKTS, "bad checksum"
2352 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002353
2354 #
2355 # bad version - this is dropped
2356 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002357 p_ver = (
2358 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2359 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, version=3)
2360 / UDP(sport=1234, dport=1234)
2361 / Raw(b"\xa5" * 100)
2362 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002363
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002364 rx = self.send_and_assert_no_replies(
2365 self.pg0, p_ver * NUM_PKTS, "funky version"
2366 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002367
2368 #
2369 # fragment offset 1 - this is dropped
2370 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002371 p_frag = (
2372 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2373 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, frag=1)
2374 / UDP(sport=1234, dport=1234)
2375 / Raw(b"\xa5" * 100)
2376 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002377
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002378 rx = self.send_and_assert_no_replies(self.pg0, p_frag * NUM_PKTS, "frag offset")
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002379
2380 #
2381 # TTL expired packet
2382 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002383 p_ttl = (
2384 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2385 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=1)
2386 / UDP(sport=1234, dport=1234)
2387 / Raw(b"\xa5" * 100)
2388 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002389
Neale Ranns5c6dd172022-02-17 09:08:47 +00002390 rxs = self.send_and_expect_some(self.pg0, p_ttl * NUM_PKTS, self.pg0)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002391
Neale Ranns5c6dd172022-02-17 09:08:47 +00002392 for rx in rxs:
2393 icmp = rx[ICMP]
2394 self.assertEqual(icmptypes[icmp.type], "time-exceeded")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002395 self.assertEqual(icmpcodes[icmp.type][icmp.code], "ttl-zero-during-transit")
Neale Ranns5c6dd172022-02-17 09:08:47 +00002396 self.assertEqual(icmp.src, self.pg0.remote_ip4)
2397 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002398
Neale Rannsffd78d12018-02-09 06:05:16 -08002399 #
2400 # MTU exceeded
2401 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002402 p_mtu = (
2403 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2404 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=10, flags="DF")
2405 / UDP(sport=1234, dport=1234)
2406 / Raw(b"\xa5" * 2000)
2407 )
Neale Rannsffd78d12018-02-09 06:05:16 -08002408
Ole Troand7231612018-06-07 10:17:57 +02002409 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1500, 0, 0, 0])
Neale Rannsffd78d12018-02-09 06:05:16 -08002410
Neale Ranns5c6dd172022-02-17 09:08:47 +00002411 rxs = self.send_and_expect_some(self.pg0, p_mtu * NUM_PKTS, self.pg0)
Neale Rannsffd78d12018-02-09 06:05:16 -08002412
Neale Ranns5c6dd172022-02-17 09:08:47 +00002413 for rx in rxs:
2414 icmp = rx[ICMP]
2415 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002416 self.assertEqual(icmpcodes[icmp.type][icmp.code], "fragmentation-needed")
Neale Rannsfbc633f2022-03-18 13:05:09 +00002417 self.assertEqual(icmp.nexthopmtu, 1500)
Neale Ranns5c6dd172022-02-17 09:08:47 +00002418 self.assertEqual(icmp.src, self.pg0.remote_ip4)
2419 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
Neale Rannsffd78d12018-02-09 06:05:16 -08002420
Ole Troand7231612018-06-07 10:17:57 +02002421 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [2500, 0, 0, 0])
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002422 rx = self.send_and_expect(self.pg0, p_mtu * NUM_PKTS, self.pg1)
Neale Rannsffd78d12018-02-09 06:05:16 -08002423
Ole Troand7231612018-06-07 10:17:57 +02002424 # Reset MTU for subsequent tests
2425 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [9000, 0, 0, 0])
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002426
Neale Rannsbe2286b2018-12-09 12:54:51 -08002427 #
2428 # source address 0.0.0.0 and 25.255.255.255 and for-us
2429 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002430 p_s0 = (
2431 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2432 / IP(src="0.0.0.0", dst=self.pg0.local_ip4)
2433 / ICMP(id=4, seq=4)
2434 / Raw(load=b"\x0a" * 18)
2435 )
Neale Rannsbe2286b2018-12-09 12:54:51 -08002436 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
2437
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002438 p_s0 = (
2439 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2440 / IP(src="255.255.255.255", dst=self.pg0.local_ip4)
2441 / ICMP(id=4, seq=4)
2442 / Raw(load=b"\x0a" * 18)
2443 )
Neale Rannsbe2286b2018-12-09 12:54:51 -08002444 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
2445
Neale Ranns1855b8e2018-07-11 10:31:26 -07002446
2447class TestIPDirectedBroadcast(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002448 """IPv4 Directed Broadcast"""
Neale Ranns1855b8e2018-07-11 10:31:26 -07002449
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002450 @classmethod
2451 def setUpClass(cls):
2452 super(TestIPDirectedBroadcast, cls).setUpClass()
2453
2454 @classmethod
2455 def tearDownClass(cls):
2456 super(TestIPDirectedBroadcast, cls).tearDownClass()
2457
Neale Ranns1855b8e2018-07-11 10:31:26 -07002458 def setUp(self):
2459 super(TestIPDirectedBroadcast, self).setUp()
2460
2461 self.create_pg_interfaces(range(2))
2462
2463 for i in self.pg_interfaces:
2464 i.admin_up()
2465
2466 def tearDown(self):
2467 super(TestIPDirectedBroadcast, self).tearDown()
2468 for i in self.pg_interfaces:
2469 i.admin_down()
2470
2471 def test_ip_input(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002472 """IP Directed Broadcast"""
Neale Ranns1855b8e2018-07-11 10:31:26 -07002473
2474 #
2475 # set the directed broadcast on pg0 first, then config IP4 addresses
2476 # for pg1 directed broadcast is always disabled
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002477 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 1)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002478
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002479 p0 = (
2480 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
2481 / IP(src="1.1.1.1", dst=self.pg0._local_ip4_bcast)
2482 / UDP(sport=1234, dport=1234)
2483 / Raw(b"\xa5" * 2000)
2484 )
2485 p1 = (
2486 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2487 / IP(src="1.1.1.1", dst=self.pg1._local_ip4_bcast)
2488 / UDP(sport=1234, dport=1234)
2489 / Raw(b"\xa5" * 2000)
2490 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002491
2492 self.pg0.config_ip4()
2493 self.pg0.resolve_arp()
2494 self.pg1.config_ip4()
2495 self.pg1.resolve_arp()
2496
2497 #
2498 # test packet is L2 broadcast
2499 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002500 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002501 self.assertTrue(rx[0][Ether].dst, "ff:ff:ff:ff:ff:ff")
2502
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002503 self.send_and_assert_no_replies(
2504 self.pg0, p1 * NUM_PKTS, "directed broadcast disabled"
2505 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002506
2507 #
2508 # toggle directed broadcast on pg0
2509 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002510 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 0)
2511 self.send_and_assert_no_replies(
2512 self.pg1, p0 * NUM_PKTS, "directed broadcast disabled"
2513 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002514
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002515 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 1)
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002516 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002517
2518 self.pg0.unconfig_ip4()
2519 self.pg1.unconfig_ip4()
2520
2521
mu.duojiao59a82952018-10-11 14:27:30 +08002522class TestIPLPM(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002523 """IPv4 longest Prefix Match"""
mu.duojiao59a82952018-10-11 14:27:30 +08002524
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002525 @classmethod
2526 def setUpClass(cls):
2527 super(TestIPLPM, cls).setUpClass()
2528
2529 @classmethod
2530 def tearDownClass(cls):
2531 super(TestIPLPM, cls).tearDownClass()
2532
mu.duojiao59a82952018-10-11 14:27:30 +08002533 def setUp(self):
2534 super(TestIPLPM, self).setUp()
2535
2536 self.create_pg_interfaces(range(4))
2537
2538 for i in self.pg_interfaces:
2539 i.admin_up()
2540 i.config_ip4()
2541 i.resolve_arp()
2542
2543 def tearDown(self):
2544 super(TestIPLPM, self).tearDown()
2545 for i in self.pg_interfaces:
2546 i.admin_down()
2547 i.unconfig_ip4()
2548
2549 def test_ip_lpm(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002550 """IP longest Prefix Match"""
mu.duojiao59a82952018-10-11 14:27:30 +08002551
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002552 s_24 = VppIpRoute(
2553 self,
2554 "10.1.2.0",
2555 24,
2556 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
2557 )
mu.duojiao59a82952018-10-11 14:27:30 +08002558 s_24.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002559 s_8 = VppIpRoute(
2560 self,
2561 "10.0.0.0",
2562 8,
2563 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
2564 )
mu.duojiao59a82952018-10-11 14:27:30 +08002565 s_8.add_vpp_config()
2566
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002567 p_8 = (
2568 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2569 / IP(src="1.1.1.1", dst="10.1.1.1")
2570 / UDP(sport=1234, dport=1234)
2571 / Raw(b"\xa5" * 2000)
2572 )
2573 p_24 = (
2574 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2575 / IP(src="1.1.1.1", dst="10.1.2.1")
2576 / UDP(sport=1234, dport=1234)
2577 / Raw(b"\xa5" * 2000)
2578 )
mu.duojiao59a82952018-10-11 14:27:30 +08002579
2580 self.logger.info(self.vapi.cli("sh ip fib mtrie"))
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002581 rx = self.send_and_expect(self.pg0, p_8 * NUM_PKTS, self.pg2)
2582 rx = self.send_and_expect(self.pg0, p_24 * NUM_PKTS, self.pg1)
mu.duojiao59a82952018-10-11 14:27:30 +08002583
2584
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002585@tag_fixme_vpp_workers
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002586class TestIPv4Frag(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002587 """IPv4 fragmentation"""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002588
2589 @classmethod
2590 def setUpClass(cls):
2591 super(TestIPv4Frag, cls).setUpClass()
2592
2593 cls.create_pg_interfaces([0, 1])
2594 cls.src_if = cls.pg0
2595 cls.dst_if = cls.pg1
2596
2597 # setup all interfaces
2598 for i in cls.pg_interfaces:
2599 i.admin_up()
2600 i.config_ip4()
2601 i.resolve_arp()
2602
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002603 @classmethod
2604 def tearDownClass(cls):
2605 super(TestIPv4Frag, cls).tearDownClass()
2606
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002607 def test_frag_large_packets(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002608 """Fragmentation of large packets"""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002609
Neale Ranns0b6a8572019-10-30 17:34:14 +00002610 self.vapi.cli("adjacency counters enable")
2611
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002612 p = (
2613 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2614 / IP(src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
2615 / UDP(sport=1234, dport=5678)
2616 / Raw()
2617 )
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002618 self.extend_packet(p, 6000, "abcde")
2619 saved_payload = p[Raw].load
2620
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002621 nbr = VppNeighbor(
2622 self,
2623 self.dst_if.sw_if_index,
2624 self.dst_if.remote_mac,
2625 self.dst_if.remote_ip4,
2626 ).add_vpp_config()
Neale Ranns0b6a8572019-10-30 17:34:14 +00002627
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002628 # Force fragmentation by setting MTU of output interface
2629 # lower than packet size
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002630 self.vapi.sw_interface_set_mtu(self.dst_if.sw_if_index, [5000, 0, 0, 0])
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002631
2632 self.pg_enable_capture()
2633 self.src_if.add_stream(p)
2634 self.pg_start()
2635
2636 # Expecting 3 fragments because size of created fragments currently
2637 # cannot be larger then VPP buffer size (which is 2048)
2638 packets = self.dst_if.get_capture(3)
2639
Neale Ranns0b6a8572019-10-30 17:34:14 +00002640 # we should show 3 packets thru the neighbor
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002641 self.assertEqual(3, nbr.get_stats()["packets"])
Neale Ranns0b6a8572019-10-30 17:34:14 +00002642
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002643 # Assume VPP sends the fragments in order
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002644 payload = b""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002645 for p in packets:
2646 payload_offset = p.frag * 8
2647 if payload_offset > 0:
2648 payload_offset -= 8 # UDP header is not in payload
2649 self.assert_equal(payload_offset, len(payload))
2650 payload += p[Raw].load
2651 self.assert_equal(payload, saved_payload, "payload")
2652
2653
Neale Ranns9db6ada2019-11-08 12:42:31 +00002654class TestIPReplace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002655 """IPv4 Table Replace"""
Neale Ranns9db6ada2019-11-08 12:42:31 +00002656
2657 @classmethod
2658 def setUpClass(cls):
2659 super(TestIPReplace, cls).setUpClass()
2660
2661 @classmethod
2662 def tearDownClass(cls):
2663 super(TestIPReplace, cls).tearDownClass()
2664
2665 def setUp(self):
2666 super(TestIPReplace, self).setUp()
2667
2668 self.create_pg_interfaces(range(4))
2669
2670 table_id = 1
2671 self.tables = []
2672
2673 for i in self.pg_interfaces:
2674 i.admin_up()
2675 i.config_ip4()
2676 i.resolve_arp()
2677 i.generate_remote_hosts(2)
2678 self.tables.append(VppIpTable(self, table_id).add_vpp_config())
2679 table_id += 1
2680
2681 def tearDown(self):
2682 super(TestIPReplace, self).tearDown()
2683 for i in self.pg_interfaces:
2684 i.admin_down()
2685 i.unconfig_ip4()
2686
2687 def test_replace(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002688 """IP Table Replace"""
Neale Ranns9db6ada2019-11-08 12:42:31 +00002689
Neale Ranns990f6942020-10-20 07:20:17 +00002690 MRouteItfFlags = VppEnum.vl_api_mfib_itf_flags_t
2691 MRouteEntryFlags = VppEnum.vl_api_mfib_entry_flags_t
Neale Ranns9db6ada2019-11-08 12:42:31 +00002692 N_ROUTES = 20
2693 links = [self.pg0, self.pg1, self.pg2, self.pg3]
2694 routes = [[], [], [], []]
2695
2696 # load up the tables with some routes
2697 for ii, t in enumerate(self.tables):
2698 for jj in range(N_ROUTES):
2699 uni = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002700 self,
2701 "10.0.0.%d" % jj,
2702 32,
2703 [
2704 VppRoutePath(
2705 links[ii].remote_hosts[0].ip4, links[ii].sw_if_index
2706 ),
2707 VppRoutePath(
2708 links[ii].remote_hosts[1].ip4, links[ii].sw_if_index
2709 ),
2710 ],
2711 table_id=t.table_id,
2712 ).add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002713 multi = VppIpMRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002714 self,
2715 "0.0.0.0",
2716 "239.0.0.%d" % jj,
2717 32,
Neale Ranns990f6942020-10-20 07:20:17 +00002718 MRouteEntryFlags.MFIB_API_ENTRY_FLAG_NONE,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002719 [
2720 VppMRoutePath(
2721 self.pg0.sw_if_index,
2722 MRouteItfFlags.MFIB_API_ITF_FLAG_ACCEPT,
2723 ),
2724 VppMRoutePath(
2725 self.pg1.sw_if_index,
2726 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2727 ),
2728 VppMRoutePath(
2729 self.pg2.sw_if_index,
2730 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2731 ),
2732 VppMRoutePath(
2733 self.pg3.sw_if_index,
2734 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2735 ),
2736 ],
2737 table_id=t.table_id,
2738 ).add_vpp_config()
2739 routes[ii].append({"uni": uni, "multi": multi})
Neale Ranns9db6ada2019-11-08 12:42:31 +00002740
2741 #
2742 # replace the tables a few times
2743 #
2744 for kk in range(3):
2745 # replace_begin each table
2746 for t in self.tables:
2747 t.replace_begin()
2748
2749 # all the routes are still there
2750 for ii, t in enumerate(self.tables):
2751 dump = t.dump()
2752 mdump = t.mdump()
2753 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002754 self.assertTrue(find_route_in_dump(dump, r["uni"], t))
2755 self.assertTrue(find_mroute_in_dump(mdump, r["multi"], t))
Neale Ranns9db6ada2019-11-08 12:42:31 +00002756
2757 # redownload the even numbered routes
2758 for ii, t in enumerate(self.tables):
2759 for jj in range(0, N_ROUTES, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002760 routes[ii][jj]["uni"].add_vpp_config()
2761 routes[ii][jj]["multi"].add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002762
2763 # signal each table replace_end
2764 for t in self.tables:
2765 t.replace_end()
2766
2767 # we should find the even routes, but not the odd
2768 for ii, t in enumerate(self.tables):
2769 dump = t.dump()
2770 mdump = t.mdump()
2771 for jj in range(0, N_ROUTES, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002772 self.assertTrue(find_route_in_dump(dump, routes[ii][jj]["uni"], t))
2773 self.assertTrue(
2774 find_mroute_in_dump(mdump, routes[ii][jj]["multi"], t)
2775 )
Neale Ranns9db6ada2019-11-08 12:42:31 +00002776 for jj in range(1, N_ROUTES - 1, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002777 self.assertFalse(find_route_in_dump(dump, routes[ii][jj]["uni"], t))
2778 self.assertFalse(
2779 find_mroute_in_dump(mdump, routes[ii][jj]["multi"], t)
2780 )
Neale Ranns9db6ada2019-11-08 12:42:31 +00002781
2782 # reload all the routes
2783 for ii, t in enumerate(self.tables):
2784 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002785 r["uni"].add_vpp_config()
2786 r["multi"].add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002787
2788 # all the routes are still there
2789 for ii, t in enumerate(self.tables):
2790 dump = t.dump()
2791 mdump = t.mdump()
2792 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002793 self.assertTrue(find_route_in_dump(dump, r["uni"], t))
2794 self.assertTrue(find_mroute_in_dump(mdump, r["multi"], t))
Neale Ranns9db6ada2019-11-08 12:42:31 +00002795
2796 #
2797 # finally flush the tables for good measure
2798 #
2799 for t in self.tables:
2800 t.flush()
2801 self.assertEqual(len(t.dump()), 5)
Neale Ranns03c254e2020-03-17 14:25:10 +00002802 self.assertEqual(len(t.mdump()), 3)
Neale Ranns9db6ada2019-11-08 12:42:31 +00002803
2804
Neale Ranns9efcee62019-11-26 19:30:08 +00002805class TestIPCover(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002806 """IPv4 Table Cover"""
Neale Ranns9efcee62019-11-26 19:30:08 +00002807
2808 @classmethod
2809 def setUpClass(cls):
2810 super(TestIPCover, cls).setUpClass()
2811
2812 @classmethod
2813 def tearDownClass(cls):
2814 super(TestIPCover, cls).tearDownClass()
2815
2816 def setUp(self):
2817 super(TestIPCover, self).setUp()
2818
2819 self.create_pg_interfaces(range(4))
2820
2821 table_id = 1
2822 self.tables = []
2823
2824 for i in self.pg_interfaces:
2825 i.admin_up()
2826 i.config_ip4()
2827 i.resolve_arp()
2828 i.generate_remote_hosts(2)
2829 self.tables.append(VppIpTable(self, table_id).add_vpp_config())
2830 table_id += 1
2831
2832 def tearDown(self):
2833 super(TestIPCover, self).tearDown()
2834 for i in self.pg_interfaces:
2835 i.admin_down()
2836 i.unconfig_ip4()
2837
2838 def test_cover(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002839 """IP Table Cover"""
Neale Ranns9efcee62019-11-26 19:30:08 +00002840
2841 # add a loop back with a /32 prefix
2842 lo = VppLoInterface(self)
2843 lo.admin_up()
2844 a = VppIpInterfaceAddress(self, lo, "127.0.0.1", 32).add_vpp_config()
2845
2846 # add a neighbour that matches the loopback's /32
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002847 nbr = VppNeighbor(
2848 self, lo.sw_if_index, lo.remote_mac, "127.0.0.1"
2849 ).add_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002850
2851 # add the default route which will be the cover for /32
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002852 r = VppIpRoute(
2853 self,
2854 "0.0.0.0",
2855 0,
2856 [VppRoutePath("127.0.0.1", lo.sw_if_index)],
2857 register=False,
2858 ).add_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002859
2860 # add/remove/add a longer mask cover
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002861 r8 = VppIpRoute(
2862 self, "127.0.0.0", 8, [VppRoutePath("127.0.0.1", lo.sw_if_index)]
2863 ).add_vpp_config()
Neale Ranns87866032020-11-25 09:14:22 +00002864 r8.remove_vpp_config()
2865 r8.add_vpp_config()
2866 r8.remove_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002867
2868 # remove the default route
2869 r.remove_vpp_config()
2870
Neale Ranns87866032020-11-25 09:14:22 +00002871 # remove the interface prefix
2872 a.remove_vpp_config()
2873
Neale Ranns59f71132020-04-08 12:19:38 +00002874
2875class TestIP4Replace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002876 """IPv4 Interface Address Replace"""
Neale Ranns59f71132020-04-08 12:19:38 +00002877
2878 @classmethod
2879 def setUpClass(cls):
2880 super(TestIP4Replace, cls).setUpClass()
2881
2882 @classmethod
2883 def tearDownClass(cls):
2884 super(TestIP4Replace, cls).tearDownClass()
2885
2886 def setUp(self):
2887 super(TestIP4Replace, self).setUp()
2888
2889 self.create_pg_interfaces(range(4))
2890
2891 for i in self.pg_interfaces:
2892 i.admin_up()
2893
2894 def tearDown(self):
2895 super(TestIP4Replace, self).tearDown()
2896 for i in self.pg_interfaces:
2897 i.admin_down()
2898
2899 def get_n_pfxs(self, intf):
2900 return len(self.vapi.ip_address_dump(intf.sw_if_index))
2901
2902 def test_replace(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002903 """IP interface address replace"""
Neale Ranns59f71132020-04-08 12:19:38 +00002904
2905 intf_pfxs = [[], [], [], []]
2906
2907 # add prefixes to each of the interfaces
2908 for i in range(len(self.pg_interfaces)):
2909 intf = self.pg_interfaces[i]
2910
2911 # 172.16.x.1/24
2912 addr = "172.16.%d.1" % intf.sw_if_index
2913 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2914 intf_pfxs[i].append(a)
2915
2916 # 172.16.x.2/24 - a different address in the same subnet as above
2917 addr = "172.16.%d.2" % intf.sw_if_index
2918 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2919 intf_pfxs[i].append(a)
2920
2921 # 172.15.x.2/24 - a different address and subnet
2922 addr = "172.15.%d.2" % intf.sw_if_index
2923 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2924 intf_pfxs[i].append(a)
2925
2926 # a dump should n_address in it
2927 for intf in self.pg_interfaces:
2928 self.assertEqual(self.get_n_pfxs(intf), 3)
2929
2930 #
2931 # remove all the address thru a replace
2932 #
2933 self.vapi.sw_interface_address_replace_begin()
2934 self.vapi.sw_interface_address_replace_end()
2935 for intf in self.pg_interfaces:
2936 self.assertEqual(self.get_n_pfxs(intf), 0)
2937
2938 #
2939 # add all the interface addresses back
2940 #
2941 for p in intf_pfxs:
2942 for v in p:
2943 v.add_vpp_config()
2944 for intf in self.pg_interfaces:
2945 self.assertEqual(self.get_n_pfxs(intf), 3)
2946
2947 #
2948 # replace again, but this time update/re-add the address on the first
2949 # two interfaces
2950 #
2951 self.vapi.sw_interface_address_replace_begin()
2952
2953 for p in intf_pfxs[:2]:
2954 for v in p:
2955 v.add_vpp_config()
2956
2957 self.vapi.sw_interface_address_replace_end()
2958
2959 # on the first two the address still exist,
2960 # on the other two they do not
2961 for intf in self.pg_interfaces[:2]:
2962 self.assertEqual(self.get_n_pfxs(intf), 3)
2963 for p in intf_pfxs[:2]:
2964 for v in p:
2965 self.assertTrue(v.query_vpp_config())
2966 for intf in self.pg_interfaces[2:]:
2967 self.assertEqual(self.get_n_pfxs(intf), 0)
2968
2969 #
2970 # add all the interface addresses back on the last two
2971 #
2972 for p in intf_pfxs[2:]:
2973 for v in p:
2974 v.add_vpp_config()
2975 for intf in self.pg_interfaces:
2976 self.assertEqual(self.get_n_pfxs(intf), 3)
2977
2978 #
2979 # replace again, this time add different prefixes on all the interfaces
2980 #
2981 self.vapi.sw_interface_address_replace_begin()
2982
2983 pfxs = []
2984 for intf in self.pg_interfaces:
2985 # 172.18.x.1/24
2986 addr = "172.18.%d.1" % intf.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002987 pfxs.append(VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config())
Neale Ranns59f71132020-04-08 12:19:38 +00002988
2989 self.vapi.sw_interface_address_replace_end()
2990
2991 # only .18 should exist on each interface
2992 for intf in self.pg_interfaces:
2993 self.assertEqual(self.get_n_pfxs(intf), 1)
2994 for pfx in pfxs:
2995 self.assertTrue(pfx.query_vpp_config())
2996
2997 #
2998 # remove everything
2999 #
3000 self.vapi.sw_interface_address_replace_begin()
3001 self.vapi.sw_interface_address_replace_end()
3002 for intf in self.pg_interfaces:
3003 self.assertEqual(self.get_n_pfxs(intf), 0)
3004
3005 #
3006 # add prefixes to each interface. post-begin add the prefix from
3007 # interface X onto interface Y. this would normally be an error
3008 # since it would generate a 'duplicate address' warning. but in
3009 # this case, since what is newly downloaded is sane, it's ok
3010 #
3011 for intf in self.pg_interfaces:
3012 # 172.18.x.1/24
3013 addr = "172.18.%d.1" % intf.sw_if_index
3014 VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
3015
3016 self.vapi.sw_interface_address_replace_begin()
3017
3018 pfxs = []
3019 for intf in self.pg_interfaces:
3020 # 172.18.x.1/24
3021 addr = "172.18.%d.1" % (intf.sw_if_index + 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003022 pfxs.append(VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config())
Neale Ranns59f71132020-04-08 12:19:38 +00003023
3024 self.vapi.sw_interface_address_replace_end()
3025
3026 self.logger.info(self.vapi.cli("sh int addr"))
3027
3028 for intf in self.pg_interfaces:
3029 self.assertEqual(self.get_n_pfxs(intf), 1)
3030 for pfx in pfxs:
3031 self.assertTrue(pfx.query_vpp_config())
3032
3033
Neale Ranns8f5fef22020-12-21 08:29:34 +00003034class TestIPv4PathMTU(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003035 """IPv4 Path MTU"""
Neale Ranns8f5fef22020-12-21 08:29:34 +00003036
3037 @classmethod
3038 def setUpClass(cls):
3039 super(TestIPv4PathMTU, cls).setUpClass()
3040
3041 cls.create_pg_interfaces(range(2))
3042
3043 # setup all interfaces
3044 for i in cls.pg_interfaces:
3045 i.admin_up()
3046 i.config_ip4()
3047 i.resolve_arp()
3048
3049 @classmethod
3050 def tearDownClass(cls):
3051 super(TestIPv4PathMTU, cls).tearDownClass()
3052
3053 def test_path_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003054 """Path MTU"""
Neale Ranns8f5fef22020-12-21 08:29:34 +00003055
3056 #
3057 # The goal here is not to test that fragmentation works correctly,
3058 # that's done elsewhere, the intent is to ensure that the Path MTU
3059 # settings are honoured.
3060 #
3061 self.vapi.cli("adjacency counters enable")
3062
3063 # set the interface MTU to a reasonable value
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003064 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1800, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003065
3066 self.pg1.generate_remote_hosts(4)
3067
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003068 p_2k = (
3069 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3070 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3071 / UDP(sport=1234, dport=5678)
3072 / Raw(b"0xa" * 640)
3073 )
3074 p_1k = (
3075 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3076 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3077 / UDP(sport=1234, dport=5678)
3078 / Raw(b"0xa" * 320)
3079 )
Neale Ranns8f5fef22020-12-21 08:29:34 +00003080
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003081 nbr = VppNeighbor(
3082 self, self.pg1.sw_if_index, self.pg1.remote_mac, self.pg1.remote_ip4
3083 ).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003084
3085 # this is now the interface MTU frags
3086 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=2)
3087 self.send_and_expect(self.pg0, [p_1k], self.pg1)
3088
3089 # drop the path MTU for this neighbour to below the interface MTU
3090 # expect more frags
3091 pmtu = VppIpPathMtu(self, self.pg1.remote_ip4, 900).add_vpp_config()
3092
3093 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3094 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3095
3096 # print/format the adj delegate
3097 self.logger.info(self.vapi.cli("sh adj 5"))
3098
3099 # increase the path MTU to more than the interface
3100 # expect to use the interface MTU
3101 pmtu.modify(8192)
3102
3103 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=2)
3104 self.send_and_expect(self.pg0, [p_1k], self.pg1)
3105
3106 # go back to an MTU from the path
3107 # wrap the call around mark-n-sweep to enusre updates clear stale
3108 self.vapi.ip_path_mtu_replace_begin()
3109 pmtu.modify(900)
3110 self.vapi.ip_path_mtu_replace_end()
3111
3112 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3113 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3114
3115 # raise the interface's MTU
3116 # should still use that of the path
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003117 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [2000, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003118 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3119 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3120
3121 # set path high and interface low
3122 pmtu.modify(2000)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003123 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [900, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003124 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3125 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3126
3127 # remove the path MTU using the mark-n-sweep semantics
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003128 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1800, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003129 self.vapi.ip_path_mtu_replace_begin()
3130 self.vapi.ip_path_mtu_replace_end()
3131
3132 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=2)
3133 self.send_and_expect(self.pg0, [p_1k], self.pg1)
3134
3135 #
3136 # set path MTU for a neighbour that doesn't exist, yet
3137 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003138 pmtu2 = VppIpPathMtu(self, self.pg1.remote_hosts[2].ip4, 900).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003139
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003140 p_2k = (
3141 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3142 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_hosts[2].ip4)
3143 / UDP(sport=1234, dport=5678)
3144 / Raw(b"0xa" * 640)
3145 )
3146 p_1k = (
3147 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3148 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_hosts[2].ip4)
3149 / UDP(sport=1234, dport=5678)
3150 / Raw(b"0xa" * 320)
3151 )
Neale Ranns8f5fef22020-12-21 08:29:34 +00003152
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003153 nbr2 = VppNeighbor(
3154 self,
3155 self.pg1.sw_if_index,
3156 self.pg1.remote_hosts[2].mac,
3157 self.pg1.remote_hosts[2].ip4,
3158 ).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003159
3160 # should frag to the path MTU
3161 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3162 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3163
3164 # remove and re-add the neighbour
3165 nbr2.remove_vpp_config()
3166 nbr2.add_vpp_config()
3167
3168 # should frag to the path MTU
3169 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3170 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3171
3172 #
3173 # set PMTUs for many peers
3174 #
3175 N_HOSTS = 16
3176 self.pg1.generate_remote_hosts(16)
3177 self.pg1.configure_ipv4_neighbors()
3178
3179 for h in range(N_HOSTS):
3180 pmtu = VppIpPathMtu(self, self.pg1.remote_hosts[h].ip4, 900)
3181 pmtu.add_vpp_config()
3182 self.assertTrue(pmtu.query_vpp_config())
3183
3184 self.logger.info(self.vapi.cli("sh ip pmtu"))
3185 dump = list(self.vapi.vpp.details_iter(self.vapi.ip_path_mtu_get))
3186 self.assertEqual(N_HOSTS, len(dump))
3187
3188 for h in range(N_HOSTS):
3189 p_2k[IP].dst = self.pg1.remote_hosts[h].ip4
3190 p_1k[IP].dst = self.pg1.remote_hosts[h].ip4
3191
3192 # should frag to the path MTU
3193 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3194 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3195
3196
Neale Ranns50bd1d32021-10-08 07:16:12 +00003197class TestIPv4ItfRebind(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003198 """IPv4 Interface Bind w/ attached routes"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003199
3200 def setUp(self):
3201 super(TestIPv4ItfRebind, self).setUp()
3202
3203 self.create_pg_interfaces(range(3))
3204
3205 def tearDown(self):
3206 super(TestIPv4ItfRebind, self).tearDown()
3207
3208 def test_rebind(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003209 """Import to no import"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003210
3211 TABLE_ID = 1
3212 tbl = VppIpTable(self, TABLE_ID).add_vpp_config()
3213 self.pg1.set_table_ip4(TABLE_ID)
3214
3215 for i in self.pg_interfaces:
3216 i.admin_up()
3217 i.config_ip4()
3218 i.resolve_arp()
3219
3220 # add an attached route via an pg0
3221 # in a different table. this prefix should import
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003222 rt = VppIpRoute(
3223 self,
3224 self.pg0.local_ip4,
3225 24,
3226 [VppRoutePath("0.0.0.0", self.pg0.sw_if_index)],
3227 table_id=TABLE_ID,
3228 ).add_vpp_config()
Neale Ranns50bd1d32021-10-08 07:16:12 +00003229
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003230 p = (
3231 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
3232 / IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4)
3233 / UDP(sport=1234, dport=5678)
3234 / Raw(b"0xa" * 640)
3235 )
Neale Ranns50bd1d32021-10-08 07:16:12 +00003236
3237 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3238 self.assertFalse(rx[0].haslayer(ARP))
3239
3240 # then bind pg0 to a new table
3241 # so the prefix no longer imports
3242 self.pg0.unconfig_ip4()
3243 self.pg0.set_table_ip4(TABLE_ID)
3244 self.pg0.config_ip4()
3245 self.pg0.resolve_arp()
3246
3247 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3248 self.assertFalse(rx[0].haslayer(ARP))
3249
3250 # revert back to imported
3251 self.pg0.unconfig_ip4()
3252 self.pg0.set_table_ip4(0)
3253 self.pg0.config_ip4()
3254 self.pg0.resolve_arp()
3255
3256 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3257 self.assertFalse(rx[0].haslayer(ARP))
3258
3259 # cleanup
3260 for i in self.pg_interfaces:
3261 i.unconfig_ip4()
3262 i.set_table_ip4(0)
3263 i.admin_down()
3264
3265 rt.remove_vpp_config()
3266 tbl.remove_vpp_config()
3267
3268 def test_delete(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003269 """Swap import tables"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003270
3271 TABLE_ID1 = 1
3272 tbl1_4 = VppIpTable(self, TABLE_ID1).add_vpp_config()
3273 tbl1_6 = VppIpTable(self, TABLE_ID1, True).add_vpp_config()
3274 TABLE_ID2 = 2
3275 tbl2_4 = VppIpTable(self, TABLE_ID2).add_vpp_config()
3276 tbl2_6 = VppIpTable(self, TABLE_ID2, True).add_vpp_config()
3277
3278 # table mappings
3279 self.pg1.set_table_ip4(TABLE_ID1)
3280 self.pg1.set_table_ip6(TABLE_ID1)
3281 self.pg2.set_table_ip4(TABLE_ID2)
3282 self.pg2.set_table_ip6(TABLE_ID2)
3283
3284 for i in self.pg_interfaces:
3285 i.admin_up()
3286 i.config_ip4()
3287 i.resolve_arp()
3288
3289 # add an attached route in the default table via pg0
3290 # this should import to table 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003291 rt4 = VppIpRoute(
3292 self,
3293 self.pg1.local_ip4,
3294 24,
3295 [VppRoutePath("0.0.0.0", self.pg1.sw_if_index)],
3296 ).add_vpp_config()
3297 rt6 = VppIpRoute(
3298 self,
3299 self.pg1.local_ip6,
3300 64,
3301 [VppRoutePath("0.0.0.0", self.pg1.sw_if_index)],
3302 ).add_vpp_config()
Neale Ranns50bd1d32021-10-08 07:16:12 +00003303
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003304 p1 = (
3305 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3306 / IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4)
3307 / UDP(sport=1234, dport=5678)
3308 / Raw(b"0xa" * 640)
3309 )
Neale Ranns50bd1d32021-10-08 07:16:12 +00003310
3311 # inject into table 0
3312 rx = self.send_and_expect(self.pg0, [p1], self.pg1)
3313 self.assertFalse(rx[0].haslayer(ARP))
3314
3315 # swap the attached interface to table 2
3316 self.pg1.unconfig_ip4()
3317 self.pg1.unconfig_ip6()
3318 self.pg1.set_table_ip4(TABLE_ID2)
3319 self.pg1.set_table_ip6(TABLE_ID2)
3320 self.pg1.config_ip4()
3321 self.pg1.config_ip6()
3322 self.pg1.resolve_arp()
3323
3324 # delete table 1
3325 tbl1_4.flush()
3326 tbl1_6.flush()
3327 tbl1_4.remove_vpp_config()
3328 tbl1_6.remove_vpp_config()
3329
3330 rx = self.send_and_expect(self.pg0, [p1], self.pg1)
3331 self.assertFalse(rx[0].haslayer(ARP))
3332
3333 for i in self.pg_interfaces:
3334 i.unconfig_ip4()
3335 i.unconfig_ip6()
3336 i.set_table_ip4(0)
3337 i.set_table_ip6(0)
3338 i.admin_down()
3339
3340
Vladislav Grishenkodea806d2024-02-20 11:58:01 +05003341class TestIP4InterfaceRx(VppTestCase):
3342 """IPv4 Interface Receive"""
3343
3344 @classmethod
3345 def setUpClass(cls):
3346 super(TestIP4InterfaceRx, cls).setUpClass()
3347
3348 @classmethod
3349 def tearDownClass(cls):
3350 super(TestIP4InterfaceRx, cls).tearDownClass()
3351
3352 def setUp(self):
3353 super(TestIP4InterfaceRx, self).setUp()
3354
3355 self.create_pg_interfaces(range(3))
3356
3357 table_id = 0
3358
3359 for i in self.pg_interfaces:
3360 i.admin_up()
3361
3362 if table_id != 0:
3363 table = VppIpTable(self, table_id)
3364 table.add_vpp_config()
3365
3366 i.set_table_ip4(table_id)
3367 i.config_ip4()
3368 i.resolve_arp()
3369 table_id += 1
3370
3371 def tearDown(self):
3372 for i in self.pg_interfaces:
3373 i.unconfig_ip4()
3374 i.admin_down()
3375 i.set_table_ip4(0)
3376
3377 super(TestIP4InterfaceRx, self).tearDown()
3378
3379 def test_interface_rx(self):
3380 """IPv4 Interface Receive"""
3381
3382 #
3383 # add a route in the default table to receive ...
3384 #
3385 route_to_dst = VppIpRoute(
3386 self,
3387 "1.1.1.0",
3388 24,
3389 [
3390 VppRoutePath(
3391 "0.0.0.0",
3392 self.pg1.sw_if_index,
3393 type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
3394 )
3395 ],
3396 )
3397 route_to_dst.add_vpp_config()
3398
3399 #
3400 # packets to these destination are dropped, since they'll
3401 # hit the respective default routes in table 1
3402 #
3403 p_dst = (
3404 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3405 / IP(src="5.5.5.5", dst="1.1.1.1")
3406 / TCP(sport=1234, dport=1234)
3407 / Raw(b"\xa5" * 100)
3408 )
3409 pkts_dst = p_dst * 10
3410
3411 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 1")
3412
3413 #
3414 # add a route in the dst table to forward via pg1
3415 #
3416 route_in_dst = VppIpRoute(
3417 self,
3418 "1.1.1.1",
3419 32,
3420 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
3421 table_id=1,
3422 )
3423 route_in_dst.add_vpp_config()
3424
3425 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
3426
3427 #
3428 # add a route in the default table to receive ...
3429 #
3430 route_to_dst = VppIpRoute(
3431 self,
3432 "1.1.1.0",
3433 24,
3434 [
3435 VppRoutePath(
3436 "0.0.0.0",
3437 self.pg2.sw_if_index,
3438 type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
3439 )
3440 ],
3441 table_id=1,
3442 )
3443 route_to_dst.add_vpp_config()
3444
3445 #
3446 # packets to these destination are dropped, since they'll
3447 # hit the respective default routes in table 2
3448 #
3449 p_dst = (
3450 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3451 / IP(src="6.6.6.6", dst="1.1.1.2")
3452 / TCP(sport=1234, dport=1234)
3453 / Raw(b"\xa5" * 100)
3454 )
3455 pkts_dst = p_dst * 10
3456
3457 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 2")
3458
3459 #
3460 # add a route in the table 2 to forward via pg2
3461 #
3462 route_in_dst = VppIpRoute(
3463 self,
3464 "1.1.1.2",
3465 32,
3466 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
3467 table_id=2,
3468 )
3469 route_in_dst.add_vpp_config()
3470
3471 self.send_and_expect(self.pg0, pkts_dst, self.pg2)
3472
3473
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003474if __name__ == "__main__":
Klement Sekeraf62ae122016-10-11 11:47:09 +02003475 unittest.main(testRunner=VppTestRunner)