blob: f4b428df4d378f3d726931523912affd309e3f77 [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
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000037from config import config
Damjan Marionf56b77a2016-10-03 19:44:57 +020038
Paul Vinciguerra4271c972019-05-14 13:25:49 -040039NUM_PKTS = 67
40
Damjan Marionf56b77a2016-10-03 19:44:57 +020041
Klement Sekeraf62ae122016-10-11 11:47:09 +020042class TestIPv4(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 """IPv4 Test Case"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020044
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070045 @classmethod
46 def setUpClass(cls):
47 super(TestIPv4, cls).setUpClass()
48
49 @classmethod
50 def tearDownClass(cls):
51 super(TestIPv4, cls).tearDownClass()
52
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010054 """
55 Perform test setup before test case.
56
57 **Config:**
58 - create 3 pg interfaces
59 - untagged pg0 interface
60 - Dot1Q subinterface on pg1
61 - Dot1AD subinterface on pg2
62 - setup interfaces:
63 - put it into UP state
64 - set IPv4 addresses
65 - resolve neighbor address using ARP
66 - configure 200 fib entries
67
68 :ivar list interfaces: pg interfaces and subinterfaces.
69 :ivar dict flows: IPv4 packet flows in test.
Matej Klotton86d87c42016-11-11 11:38:55 +010070 """
Klement Sekeraf62ae122016-10-11 11:47:09 +020071 super(TestIPv4, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +020072
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 # create 3 pg interfaces
74 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +020075
Klement Sekeraf62ae122016-10-11 11:47:09 +020076 # create 2 subinterfaces for pg1 and pg2
77 self.sub_interfaces = [
78 VppDot1QSubint(self, self.pg1, 100),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 VppDot1ADSubint(self, self.pg2, 200, 300, 400),
80 ]
Damjan Marionf56b77a2016-10-03 19:44:57 +020081
Klement Sekeraf62ae122016-10-11 11:47:09 +020082 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
83 self.flows = dict()
84 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
85 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
86 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +020087
Klement Sekeraf62ae122016-10-11 11:47:09 +020088 # packet sizes
Jan Geletye6c78ee2018-06-26 12:24:03 +020089 self.pg_if_packet_sizes = [64, 1500, 9020]
Damjan Marionf56b77a2016-10-03 19:44:57 +020090
Klement Sekeraf62ae122016-10-11 11:47:09 +020091 self.interfaces = list(self.pg_interfaces)
92 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +020093
Klement Sekeraf62ae122016-10-11 11:47:09 +020094 # setup all interfaces
95 for i in self.interfaces:
96 i.admin_up()
97 i.config_ip4()
98 i.resolve_arp()
99
Matej Klotton86d87c42016-11-11 11:38:55 +0100100 # config 2M FIB entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200101
102 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100103 """Run standard test teardown and log ``show ip arp``."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200104 super(TestIPv4, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700105
106 def show_commands_at_teardown(self):
Neale Rannscbe25aa2019-09-30 10:53:31 +0000107 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700108 # info(self.vapi.cli("show ip fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200109
Jan Geletye6c78ee2018-06-26 12:24:03 +0200110 def modify_packet(self, src_if, packet_size, pkt):
111 """Add load, set destination IP and extend packet to required packet
112 size for defined interface.
113
114 :param VppInterface src_if: Interface to create packet for.
115 :param int packet_size: Required packet size.
116 :param Scapy pkt: Packet to be modified.
117 """
Ole Troan6ed154f2019-10-15 19:31:55 +0200118 dst_if_idx = int(packet_size / 10 % 2)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200119 dst_if = self.flows[src_if][dst_if_idx]
120 info = self.create_packet_info(src_if, dst_if)
121 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 p = pkt / Raw(payload)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200123 p[IP].dst = dst_if.remote_ip4
124 info.data = p.copy()
125 if isinstance(src_if, VppSubInterface):
126 p = src_if.add_dot1_layer(p)
127 self.extend_packet(p, packet_size)
128
129 return p
130
131 def create_stream(self, src_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100132 """Create input packet stream for defined interface.
133
134 :param VppInterface src_if: Interface to create packet stream for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100135 """
Jan Geletye6c78ee2018-06-26 12:24:03 +0200136 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200137 pkt_tmpl = (
138 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
139 / IP(src=src_if.remote_ip4)
140 / UDP(sport=1234, dport=1234)
141 )
Jan Geletye6c78ee2018-06-26 12:24:03 +0200142
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200143 pkts = [
144 self.modify_packet(src_if, i, pkt_tmpl)
145 for i in moves.range(
146 self.pg_if_packet_sizes[0], self.pg_if_packet_sizes[1], 10
147 )
148 ]
149 pkts_b = [
150 self.modify_packet(src_if, i, pkt_tmpl)
151 for i in moves.range(
152 self.pg_if_packet_sizes[1] + hdr_ext,
153 self.pg_if_packet_sizes[2] + hdr_ext,
154 50,
155 )
156 ]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200157 pkts.extend(pkts_b)
158
Damjan Marionf56b77a2016-10-03 19:44:57 +0200159 return pkts
160
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100162 """Verify captured input packet stream for defined interface.
163
164 :param VppInterface dst_if: Interface to verify captured packet stream
Jan Geletye6c78ee2018-06-26 12:24:03 +0200165 for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100166 :param list capture: Captured packet stream.
167 """
168 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200169 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200170 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200171 last_info[i.sw_if_index] = None
172 is_sub_if = False
173 dst_sw_if_index = dst_if.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200174 if hasattr(dst_if, "parent"):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200175 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200176 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200177 if is_sub_if:
178 # Check VLAN tags and Ethernet header
179 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200180 self.assertTrue(Dot1Q not in packet)
181 try:
182 ip = packet[IP]
183 udp = packet[UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800184 payload_info = self.payload_to_info(packet[Raw])
Damjan Marionf56b77a2016-10-03 19:44:57 +0200185 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200186 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100187 self.logger.debug(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200188 "Got packet on port %s: src=%u (id=%u)"
189 % (dst_if.name, payload_info.src, packet_index)
190 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200191 next_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192 payload_info.src, dst_sw_if_index, last_info[payload_info.src]
193 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200194 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200195 self.assertTrue(next_info is not None)
196 self.assertEqual(packet_index, next_info.index)
197 saved_packet = next_info.data
198 # Check standard fields
199 self.assertEqual(ip.src, saved_packet[IP].src)
200 self.assertEqual(ip.dst, saved_packet[IP].dst)
201 self.assertEqual(udp.sport, saved_packet[UDP].sport)
202 self.assertEqual(udp.dport, saved_packet[UDP].dport)
203 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100204 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200205 raise
206 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200207 remaining_packet = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200208 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index]
209 )
210 self.assertTrue(
211 remaining_packet is None,
212 "Interface %s: Packet expected from interface %s "
213 "didn't arrive" % (dst_if.name, i.name),
214 )
Damjan Marionf56b77a2016-10-03 19:44:57 +0200215
216 def test_fib(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200217 """IPv4 FIB test
Matej Klotton86d87c42016-11-11 11:38:55 +0100218
219 Test scenario:
220
221 - Create IPv4 stream for pg0 interface
Jan Geletye6c78ee2018-06-26 12:24:03 +0200222 - Create IPv4 tagged streams for pg1's and pg2's sub-interface.
Matej Klotton86d87c42016-11-11 11:38:55 +0100223 - Send and verify received packets on each interface.
224 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200225
Jan Geletye6c78ee2018-06-26 12:24:03 +0200226 pkts = self.create_stream(self.pg0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200227 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200228
Klement Sekeraf62ae122016-10-11 11:47:09 +0200229 for i in self.sub_interfaces:
Jan Geletye6c78ee2018-06-26 12:24:03 +0200230 pkts = self.create_stream(i)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200231 i.parent.add_stream(pkts)
232
233 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200234 self.pg_start()
235
Klement Sekeraf62ae122016-10-11 11:47:09 +0200236 pkts = self.pg0.get_capture()
237 self.verify_capture(self.pg0, pkts)
238
239 for i in self.sub_interfaces:
240 pkts = i.parent.get_capture()
241 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200242
243
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400244class TestIPv4RouteLookup(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200245 """IPv4 Route Lookup Test Case"""
246
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400247 routes = []
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000248 tables = []
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400249
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000250 def route_lookup(self, prefix, exact, table_id=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200251 return self.vapi.api(
252 self.vapi.papi.ip_route_lookup,
253 {
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000254 "table_id": table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200255 "exact": exact,
256 "prefix": prefix,
257 },
258 )
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400259
260 @classmethod
261 def setUpClass(cls):
262 super(TestIPv4RouteLookup, cls).setUpClass()
263
264 @classmethod
265 def tearDownClass(cls):
266 super(TestIPv4RouteLookup, cls).tearDownClass()
267
268 def setUp(self):
269 super(TestIPv4RouteLookup, self).setUp()
270
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200271 drop_nh = VppRoutePath(
272 "127.0.0.1", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP
273 )
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400274
275 # Add 3 routes
276 r = VppIpRoute(self, "1.1.0.0", 16, [drop_nh])
277 r.add_vpp_config()
278 self.routes.append(r)
279
280 r = VppIpRoute(self, "1.1.1.0", 24, [drop_nh])
281 r.add_vpp_config()
282 self.routes.append(r)
283
284 r = VppIpRoute(self, "1.1.1.1", 32, [drop_nh])
285 r.add_vpp_config()
286 self.routes.append(r)
287
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000288 custom_vrf = VppIpTable(self, 200)
289 custom_vrf.add_vpp_config()
290 self.tables.append(custom_vrf)
291
292 r = VppIpRoute(self, "2.2.0.0", 16, [drop_nh], 200)
293 r.add_vpp_config()
294 self.routes.append(r)
295
296 r = VppIpRoute(self, "2.2.2.0", 24, [drop_nh], 200)
297 r.add_vpp_config()
298 self.routes.append(r)
299
300 r = VppIpRoute(self, "2.2.2.2", 32, [drop_nh], 200)
301 r.add_vpp_config()
302 self.routes.append(r)
303
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400304 def tearDown(self):
305 # Remove the routes we added
306 for r in self.routes:
307 r.remove_vpp_config()
308
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000309 for vrf in self.tables:
310 vrf.remove_vpp_config()
311
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400312 super(TestIPv4RouteLookup, self).tearDown()
313
314 def test_exact_match(self):
315 # Verify we find the host route
316 prefix = "1.1.1.1/32"
317 result = self.route_lookup(prefix, True)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200318 assert prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400319
320 # Verify we find a middle prefix route
321 prefix = "1.1.1.0/24"
322 result = self.route_lookup(prefix, True)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200323 assert prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400324
325 # Verify we do not find an available LPM.
326 with self.vapi.assert_negative_api_retval():
327 self.route_lookup("1.1.1.2/32", True)
328
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000329 # Verify we find the host route
330 prefix = "2.2.2.2/32"
331 result = self.route_lookup(prefix, True, 200)
332 assert prefix == str(result.route.prefix)
333
334 # Verify we find a middle prefix route
335 prefix = "2.2.2.0/24"
336 result = self.route_lookup(prefix, True, 200)
337 assert prefix == str(result.route.prefix)
338
339 # Verify we do not find an available LPM.
340 with self.vapi.assert_negative_api_retval():
341 self.route_lookup("2.2.2.1/32", True, 200)
342
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400343 def test_longest_prefix_match(self):
344 # verify we find lpm
345 lpm_prefix = "1.1.1.0/24"
346 result = self.route_lookup("1.1.1.2/32", False)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200347 assert lpm_prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400348
349 # Verify we find the exact when not requested
350 result = self.route_lookup(lpm_prefix, False)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200351 assert lpm_prefix == str(result.route.prefix)
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400352
Mohsin Kazmi5f694322024-04-19 09:10:46 +0000353 # verify we find lpm
354 lpm_prefix = "2.2.2.0/24"
355 result = self.route_lookup("2.2.2.1/32", False, 200)
356 assert lpm_prefix == str(result.route.prefix)
357
358 # Verify we find the exact when not requested
359 result = self.route_lookup(lpm_prefix, False, 200)
360 assert lpm_prefix == str(result.route.prefix)
361
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400362 # Can't seem to delete the default route so no negative LPM test.
363
364
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500365class TestIPv4IfAddrRoute(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200366 """IPv4 Interface Addr Route Test Case"""
Matthew G Smith88d29a92019-07-17 10:01:17 -0500367
368 @classmethod
369 def setUpClass(cls):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500370 super(TestIPv4IfAddrRoute, cls).setUpClass()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500371
372 @classmethod
373 def tearDownClass(cls):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500374 super(TestIPv4IfAddrRoute, cls).tearDownClass()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500375
376 def setUp(self):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500377 super(TestIPv4IfAddrRoute, self).setUp()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500378
379 # create 1 pg interface
380 self.create_pg_interfaces(range(1))
381
382 for i in self.pg_interfaces:
383 i.admin_up()
384 i.config_ip4()
385 i.resolve_arp()
386
387 def tearDown(self):
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500388 super(TestIPv4IfAddrRoute, self).tearDown()
Matthew G Smith88d29a92019-07-17 10:01:17 -0500389 for i in self.pg_interfaces:
390 i.unconfig_ip4()
391 i.admin_down()
392
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500393 def test_ipv4_ifaddrs_same_prefix(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200394 """IPv4 Interface Addresses Same Prefix test
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500395
396 Test scenario:
397
398 - Verify no route in FIB for prefix 10.10.10.0/24
399 - Configure IPv4 address 10.10.10.10/24 on an interface
400 - Verify route in FIB for prefix 10.10.10.0/24
401 - Configure IPv4 address 10.10.10.20/24 on an interface
402 - Delete 10.10.10.10/24 from interface
403 - Verify route in FIB for prefix 10.10.10.0/24
404 - Delete 10.10.10.20/24 from interface
405 - Verify no route in FIB for prefix 10.10.10.0/24
406 """
407
408 # create two addresses, verify route not present
Neale Rannsefd7bc22019-11-11 08:32:34 +0000409 if_addr1 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.10", 24)
410 if_addr2 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.20", 24)
411 self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500412 self.assertFalse(find_route(self, "10.10.10.10", 32))
413 self.assertFalse(find_route(self, "10.10.10.20", 32))
414 self.assertFalse(find_route(self, "10.10.10.255", 32))
415 self.assertFalse(find_route(self, "10.10.10.0", 32))
416
417 # configure first address, verify route present
418 if_addr1.add_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000419 self.assertTrue(if_addr1.query_vpp_config()) # 10.10.10.10/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500420 self.assertTrue(find_route(self, "10.10.10.10", 32))
421 self.assertFalse(find_route(self, "10.10.10.20", 32))
422 self.assertTrue(find_route(self, "10.10.10.255", 32))
423 self.assertTrue(find_route(self, "10.10.10.0", 32))
424
425 # configure second address, delete first, verify route not removed
426 if_addr2.add_vpp_config()
427 if_addr1.remove_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000428 self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24
429 self.assertTrue(if_addr2.query_vpp_config()) # 10.10.10.20/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500430 self.assertFalse(find_route(self, "10.10.10.10", 32))
431 self.assertTrue(find_route(self, "10.10.10.20", 32))
432 self.assertTrue(find_route(self, "10.10.10.255", 32))
433 self.assertTrue(find_route(self, "10.10.10.0", 32))
434
435 # delete second address, verify route removed
436 if_addr2.remove_vpp_config()
Neale Rannsefd7bc22019-11-11 08:32:34 +0000437 self.assertFalse(if_addr2.query_vpp_config()) # 10.10.10.20/24
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500438 self.assertFalse(find_route(self, "10.10.10.10", 32))
439 self.assertFalse(find_route(self, "10.10.10.20", 32))
440 self.assertFalse(find_route(self, "10.10.10.255", 32))
441 self.assertFalse(find_route(self, "10.10.10.0", 32))
442
Matthew G Smith88d29a92019-07-17 10:01:17 -0500443 def test_ipv4_ifaddr_route(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200444 """IPv4 Interface Address Route test
Matthew G Smith88d29a92019-07-17 10:01:17 -0500445
446 Test scenario:
447
448 - Create loopback
449 - Configure IPv4 address on loopback
450 - Verify that address is not in the FIB
451 - Bring loopback up
452 - Verify that address is in the FIB now
453 - Bring loopback down
454 - Verify that address is not in the FIB anymore
455 - Bring loopback up
456 - Configure IPv4 address on loopback
457 - Verify that address is in the FIB now
458 """
459
460 # create a loopback and configure IPv4
461 loopbacks = self.create_loopback_interfaces(1)
462 lo_if = self.lo_interfaces[0]
463
464 lo_if.local_ip4_prefix_len = 32
465 lo_if.config_ip4()
466
467 # The intf was down when addr was added -> entry not in FIB
468 fib4_dump = self.vapi.ip_route_dump(0)
469 self.assertFalse(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
470
471 # When intf is brought up, entry is added
472 lo_if.admin_up()
473 fib4_dump = self.vapi.ip_route_dump(0)
474 self.assertTrue(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
475
476 # When intf is brought down, entry is removed
477 lo_if.admin_down()
478 fib4_dump = self.vapi.ip_route_dump(0)
479 self.assertFalse(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
480
481 # Remove addr, bring up interface, re-add -> entry in FIB
482 lo_if.unconfig_ip4()
483 lo_if.admin_up()
484 lo_if.config_ip4()
485 fib4_dump = self.vapi.ip_route_dump(0)
486 self.assertTrue(lo_if.is_ip4_entry_in_fib_dump(fib4_dump))
487
yedgdbd366b2020-05-14 10:51:53 +0800488 def test_ipv4_ifaddr_del(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200489 """Delete an interface address that does not exist"""
yedgdbd366b2020-05-14 10:51:53 +0800490
491 loopbacks = self.create_loopback_interfaces(1)
492 lo = self.lo_interfaces[0]
493
494 lo.config_ip4()
495 lo.admin_up()
496
497 #
498 # try and remove pg0's subnet from lo
499 #
500 with self.vapi.assert_negative_api_retval():
501 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200502 sw_if_index=lo.sw_if_index, prefix=self.pg0.local_ip4_prefix, is_add=0
503 )
yedgdbd366b2020-05-14 10:51:53 +0800504
Matthew G Smith88d29a92019-07-17 10:01:17 -0500505
Dmitry Valter34fa0ce2024-03-11 10:38:46 +0000506@unittest.skipIf(
507 "ping" in config.excluded_plugins, "Exclude tests requiring Ping plugin"
508)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200509class TestICMPEcho(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200510 """ICMP Echo Test Case"""
Jan Geletye6c78ee2018-06-26 12:24:03 +0200511
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700512 @classmethod
513 def setUpClass(cls):
514 super(TestICMPEcho, cls).setUpClass()
515
516 @classmethod
517 def tearDownClass(cls):
518 super(TestICMPEcho, cls).tearDownClass()
519
Jan Geletye6c78ee2018-06-26 12:24:03 +0200520 def setUp(self):
521 super(TestICMPEcho, self).setUp()
522
523 # create 1 pg interface
524 self.create_pg_interfaces(range(1))
525
526 for i in self.pg_interfaces:
527 i.admin_up()
528 i.config_ip4()
529 i.resolve_arp()
530
531 def tearDown(self):
532 super(TestICMPEcho, self).tearDown()
533 for i in self.pg_interfaces:
534 i.unconfig_ip4()
535 i.admin_down()
536
537 def test_icmp_echo(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200538 """VPP replies to ICMP Echo Request
Jan Geletye6c78ee2018-06-26 12:24:03 +0200539
540 Test scenario:
541
542 - Receive ICMP Echo Request message on pg0 interface.
543 - Check outgoing ICMP Echo Reply message on pg0 interface.
544 """
545
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200546 icmp_id = 0xB
Jan Geletye6c78ee2018-06-26 12:24:03 +0200547 icmp_seq = 5
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200548 icmp_load = b"\x0a" * 18
549 p_echo_request = (
550 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
551 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
552 / ICMP(id=icmp_id, seq=icmp_seq)
553 / Raw(load=icmp_load)
554 )
Jan Geletye6c78ee2018-06-26 12:24:03 +0200555
556 self.pg0.add_stream(p_echo_request)
557 self.pg_enable_capture(self.pg_interfaces)
558 self.pg_start()
559
560 rx = self.pg0.get_capture(1)
561 rx = rx[0]
562 ether = rx[Ether]
563 ipv4 = rx[IP]
564 icmp = rx[ICMP]
565
566 self.assertEqual(ether.src, self.pg0.local_mac)
567 self.assertEqual(ether.dst, self.pg0.remote_mac)
568
569 self.assertEqual(ipv4.src, self.pg0.local_ip4)
570 self.assertEqual(ipv4.dst, self.pg0.remote_ip4)
571
572 self.assertEqual(icmptypes[icmp.type], "echo-reply")
573 self.assertEqual(icmp.id, icmp_id)
574 self.assertEqual(icmp.seq, icmp_seq)
575 self.assertEqual(icmp[Raw].load, icmp_load)
576
577
Matej Klotton16a14cd2016-12-07 15:09:13 +0100578class TestIPv4FibCrud(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200579 """FIB - add/update/delete - ip4 routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100580
581 Test scenario:
582 - add 1k,
583 - del 100,
584 - add new 1k,
585 - del 1.5k
586
Klement Sekerada505f62017-01-04 12:58:53 +0100587 ..note:: Python API is too slow to add many routes, needs replacement.
Matej Klotton16a14cd2016-12-07 15:09:13 +0100588 """
589
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200590 def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count, start=0):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100591 """
592
593 :param start_dest_addr:
594 :param next_hop_addr:
595 :param count:
596 :return list: added ips with 32 prefix
597 """
Neale Ranns097fa662018-05-01 05:17:55 -0700598 routes = []
599 for i in range(count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200600 r = VppIpRoute(
601 self,
602 start_dest_addr % (i + start),
603 32,
604 [VppRoutePath(next_hop_addr, 0xFFFFFFFF)],
605 )
Neale Ranns097fa662018-05-01 05:17:55 -0700606 r.add_vpp_config()
607 routes.append(r)
608 return routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100609
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200610 def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count, start=0):
Neale Ranns097fa662018-05-01 05:17:55 -0700611 routes = []
612 for i in range(count):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200613 r = VppIpRoute(
614 self,
615 start_dest_addr % (i + start),
616 32,
617 [VppRoutePath(next_hop_addr, 0xFFFFFFFF)],
618 )
Neale Ranns097fa662018-05-01 05:17:55 -0700619 r.remove_vpp_config()
620 routes.append(r)
621 return routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100622
Neale Ranns097fa662018-05-01 05:17:55 -0700623 def create_stream(self, src_if, dst_if, routes, count):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100624 pkts = []
625
626 for _ in range(count):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000627 dst_addr = random.choice(routes).prefix.network_address
Klement Sekeradab231a2016-12-21 08:50:14 +0100628 info = self.create_packet_info(src_if, dst_if)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100629 payload = self.info_to_payload(info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200630 p = (
631 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
632 / IP(src=src_if.remote_ip4, dst=str(dst_addr))
633 / UDP(sport=1234, dport=1234)
634 / Raw(payload)
635 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100636 info.data = p.copy()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100637 self.extend_packet(p, random.choice(self.pg_if_packet_sizes))
638 pkts.append(p)
639
640 return pkts
641
642 def _find_ip_match(self, find_in, pkt):
643 for p in find_in:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200644 if self.payload_to_info(p[Raw]) == self.payload_to_info(pkt[Raw]):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100645 if p[IP].src != pkt[IP].src:
646 break
647 if p[IP].dst != pkt[IP].dst:
648 break
649 if p[UDP].sport != pkt[UDP].sport:
650 break
651 if p[UDP].dport != pkt[UDP].dport:
652 break
653 return p
654 return None
655
Matej Klotton16a14cd2016-12-07 15:09:13 +0100656 def verify_capture(self, dst_interface, received_pkts, expected_pkts):
657 self.assertEqual(len(received_pkts), len(expected_pkts))
658 to_verify = list(expected_pkts)
659 for p in received_pkts:
660 self.assertEqual(p.src, dst_interface.local_mac)
661 self.assertEqual(p.dst, dst_interface.remote_mac)
662 x = self._find_ip_match(to_verify, p)
663 to_verify.remove(x)
664 self.assertListEqual(to_verify, [])
665
Neale Ranns097fa662018-05-01 05:17:55 -0700666 def verify_route_dump(self, routes):
667 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200668 self.assertTrue(
669 find_route(self, r.prefix.network_address, r.prefix.prefixlen)
670 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100671
Neale Ranns097fa662018-05-01 05:17:55 -0700672 def verify_not_in_route_dump(self, routes):
673 for r in routes:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200674 self.assertFalse(
675 find_route(self, r.prefix.network_address, r.prefix.prefixlen)
676 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100677
678 @classmethod
679 def setUpClass(cls):
680 """
681 #. Create and initialize 3 pg interfaces.
682 #. initialize class attributes configured_routes and deleted_routes
683 to store information between tests.
684 """
685 super(TestIPv4FibCrud, cls).setUpClass()
686
687 try:
688 # create 3 pg interfaces
689 cls.create_pg_interfaces(range(3))
690
691 cls.interfaces = list(cls.pg_interfaces)
692
693 # setup all interfaces
694 for i in cls.interfaces:
695 i.admin_up()
696 i.config_ip4()
697 i.resolve_arp()
698
699 cls.configured_routes = []
700 cls.deleted_routes = []
701 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
702
703 except Exception:
704 super(TestIPv4FibCrud, cls).tearDownClass()
705 raise
706
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700707 @classmethod
708 def tearDownClass(cls):
709 super(TestIPv4FibCrud, cls).tearDownClass()
710
Matej Klotton16a14cd2016-12-07 15:09:13 +0100711 def setUp(self):
712 super(TestIPv4FibCrud, self).setUp()
Klement Sekeradab231a2016-12-21 08:50:14 +0100713 self.reset_packet_infos()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100714
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800715 self.configured_routes = []
716 self.deleted_routes = []
717
Matej Klotton16a14cd2016-12-07 15:09:13 +0100718 def test_1_add_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200719 """Add 1k routes"""
Matej Klotton16a14cd2016-12-07 15:09:13 +0100720
Neale Ranns097fa662018-05-01 05:17:55 -0700721 # add 100 routes check with traffic script.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200722 self.configured_routes.extend(
723 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
724 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100725
Neale Ranns097fa662018-05-01 05:17:55 -0700726 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100727
728 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200729 self.pg1, self.pg0, self.configured_routes, 100
730 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100731 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200732 self.pg2, self.pg0, self.configured_routes, 100
733 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100734 self.pg1.add_stream(self.stream_1)
735 self.pg2.add_stream(self.stream_2)
736
737 self.pg_enable_capture(self.pg_interfaces)
738 self.pg_start()
739
Klement Sekeradab231a2016-12-21 08:50:14 +0100740 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100741 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
742
Matej Klotton16a14cd2016-12-07 15:09:13 +0100743 def test_2_del_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200744 """Delete 100 routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100745
746 - delete 10 routes check with traffic script.
747 """
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800748 # config 1M FIB entries
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200749 self.configured_routes.extend(
750 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
751 )
752 self.deleted_routes.extend(
753 self.unconfig_fib_many_to_one(
754 "10.0.0.%d", self.pg0.remote_ip4, 10, start=10
755 )
756 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100757 for x in self.deleted_routes:
758 self.configured_routes.remove(x)
759
Neale Ranns097fa662018-05-01 05:17:55 -0700760 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100761
762 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200763 self.pg1, self.pg0, self.configured_routes, 100
764 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100765 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200766 self.pg2, self.pg0, self.configured_routes, 100
767 )
768 self.stream_3 = self.create_stream(self.pg1, self.pg0, self.deleted_routes, 100)
769 self.stream_4 = self.create_stream(self.pg2, self.pg0, self.deleted_routes, 100)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100770 self.pg1.add_stream(self.stream_1 + self.stream_3)
771 self.pg2.add_stream(self.stream_2 + self.stream_4)
772 self.pg_enable_capture(self.pg_interfaces)
773 self.pg_start()
774
Klement Sekeradab231a2016-12-21 08:50:14 +0100775 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100776 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
777
778 def test_3_add_new_routes(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200779 """Add 1k routes
Matej Klotton16a14cd2016-12-07 15:09:13 +0100780
781 - re-add 5 routes check with traffic script.
782 - add 100 routes check with traffic script.
783 """
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800784 # config 1M FIB entries
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200785 self.configured_routes.extend(
786 self.config_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 100)
787 )
788 self.deleted_routes.extend(
789 self.unconfig_fib_many_to_one(
790 "10.0.0.%d", self.pg0.remote_ip4, 10, start=10
791 )
792 )
Paul Vinciguerra4eed7472018-12-16 14:52:36 -0800793 for x in self.deleted_routes:
794 self.configured_routes.remove(x)
795
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200796 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 +0100797 self.configured_routes.extend(tmp)
798 for x in tmp:
799 self.deleted_routes.remove(x)
800
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200801 self.configured_routes.extend(
802 self.config_fib_many_to_one("10.0.1.%d", self.pg0.remote_ip4, 100)
803 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100804
Neale Ranns097fa662018-05-01 05:17:55 -0700805 self.verify_route_dump(self.configured_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100806
807 self.stream_1 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200808 self.pg1, self.pg0, self.configured_routes, 300
809 )
Matej Klotton16a14cd2016-12-07 15:09:13 +0100810 self.stream_2 = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200811 self.pg2, self.pg0, self.configured_routes, 300
812 )
813 self.stream_3 = self.create_stream(self.pg1, self.pg0, self.deleted_routes, 100)
814 self.stream_4 = self.create_stream(self.pg2, self.pg0, self.deleted_routes, 100)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100815
816 self.pg1.add_stream(self.stream_1 + self.stream_3)
817 self.pg2.add_stream(self.stream_2 + self.stream_4)
818 self.pg_enable_capture(self.pg_interfaces)
819 self.pg_start()
820
Klement Sekeradab231a2016-12-21 08:50:14 +0100821 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100822 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
823
Neale Ranns097fa662018-05-01 05:17:55 -0700824 # delete 5 routes check with traffic script.
825 # add 100 routes check with traffic script.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200826 self.deleted_routes.extend(
827 self.unconfig_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 15)
828 )
829 self.deleted_routes.extend(
830 self.unconfig_fib_many_to_one("10.0.0.%d", self.pg0.remote_ip4, 85)
831 )
832 self.deleted_routes.extend(
833 self.unconfig_fib_many_to_one("10.0.1.%d", self.pg0.remote_ip4, 100)
834 )
Neale Ranns097fa662018-05-01 05:17:55 -0700835 self.verify_not_in_route_dump(self.deleted_routes)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100836
837
Neale Ranns37be7362017-02-21 17:30:26 -0800838class TestIPNull(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200839 """IPv4 routes via NULL"""
Neale Ranns37be7362017-02-21 17:30:26 -0800840
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700841 @classmethod
842 def setUpClass(cls):
843 super(TestIPNull, cls).setUpClass()
844
845 @classmethod
846 def tearDownClass(cls):
847 super(TestIPNull, cls).tearDownClass()
848
Neale Ranns37be7362017-02-21 17:30:26 -0800849 def setUp(self):
850 super(TestIPNull, self).setUp()
851
852 # create 2 pg interfaces
Neale Ranns3b93be52018-09-07 01:48:54 -0700853 self.create_pg_interfaces(range(2))
Neale Ranns37be7362017-02-21 17:30:26 -0800854
855 for i in self.pg_interfaces:
856 i.admin_up()
857 i.config_ip4()
858 i.resolve_arp()
859
860 def tearDown(self):
861 super(TestIPNull, self).tearDown()
862 for i in self.pg_interfaces:
863 i.unconfig_ip4()
864 i.admin_down()
865
866 def test_ip_null(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200867 """IP NULL route"""
Neale Ranns37be7362017-02-21 17:30:26 -0800868
869 #
870 # A route via IP NULL that will reply with ICMP unreachables
871 #
Neale Ranns097fa662018-05-01 05:17:55 -0700872 ip_unreach = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200873 self,
874 "10.0.0.1",
875 32,
876 [
877 VppRoutePath(
878 "0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_ICMP_UNREACH
879 )
880 ],
881 )
Neale Ranns37be7362017-02-21 17:30:26 -0800882 ip_unreach.add_vpp_config()
883
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200884 p_unreach = (
885 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
886 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1")
887 / UDP(sport=1234, dport=1234)
888 / Raw(b"\xa5" * 100)
889 )
Neale Ranns37be7362017-02-21 17:30:26 -0800890 self.pg0.add_stream(p_unreach)
891 self.pg_enable_capture(self.pg_interfaces)
892 self.pg_start()
893
894 rx = self.pg0.get_capture(1)
895 rx = rx[0]
896 icmp = rx[ICMP]
897
898 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
899 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-unreachable")
900 self.assertEqual(icmp.src, self.pg0.remote_ip4)
901 self.assertEqual(icmp.dst, "10.0.0.1")
902
903 #
904 # ICMP replies are rate limited. so sit and spin.
905 #
906 self.sleep(1)
907
908 #
909 # A route via IP NULL that will reply with ICMP prohibited
910 #
Neale Ranns097fa662018-05-01 05:17:55 -0700911 ip_prohibit = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200912 self,
913 "10.0.0.2",
914 32,
915 [
916 VppRoutePath(
917 "0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_ICMP_PROHIBIT
918 )
919 ],
920 )
Neale Ranns37be7362017-02-21 17:30:26 -0800921 ip_prohibit.add_vpp_config()
922
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200923 p_prohibit = (
924 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
925 / IP(src=self.pg0.remote_ip4, dst="10.0.0.2")
926 / UDP(sport=1234, dport=1234)
927 / Raw(b"\xa5" * 100)
928 )
Neale Ranns37be7362017-02-21 17:30:26 -0800929
930 self.pg0.add_stream(p_prohibit)
931 self.pg_enable_capture(self.pg_interfaces)
932 self.pg_start()
933
934 rx = self.pg0.get_capture(1)
935
936 rx = rx[0]
937 icmp = rx[ICMP]
938
939 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
940 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-prohibited")
941 self.assertEqual(icmp.src, self.pg0.remote_ip4)
942 self.assertEqual(icmp.dst, "10.0.0.2")
943
Neale Ranns3b93be52018-09-07 01:48:54 -0700944 def test_ip_drop(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200945 """IP Drop Routes"""
Neale Ranns3b93be52018-09-07 01:48:54 -0700946
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200947 p = (
948 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
949 / IP(src=self.pg0.remote_ip4, dst="1.1.1.1")
950 / UDP(sport=1234, dport=1234)
951 / Raw(b"\xa5" * 100)
952 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700953
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200954 r1 = VppIpRoute(
955 self,
956 "1.1.1.0",
957 24,
958 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
959 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700960 r1.add_vpp_config()
961
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400962 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
Neale Ranns3b93be52018-09-07 01:48:54 -0700963
964 #
965 # insert a more specific as a drop
966 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200967 r2 = VppIpRoute(
968 self,
969 "1.1.1.1",
970 32,
971 [VppRoutePath("0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP)],
972 )
Neale Ranns3b93be52018-09-07 01:48:54 -0700973 r2.add_vpp_config()
974
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400975 self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS, "Drop Route")
Neale Ranns3b93be52018-09-07 01:48:54 -0700976 r2.remove_vpp_config()
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400977 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
Neale Ranns3b93be52018-09-07 01:48:54 -0700978
Dmitry Valtere95687b2023-12-20 10:47:35 +0000979 t = VppIpTable(self, 2, False)
980 t.add_vpp_config()
981 r3 = VppIpRoute(
982 self,
983 "1.1.1.0",
984 31,
985 [VppRoutePath("0.0.0.0", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP)],
986 table_id=2,
987 )
988 r3.add_vpp_config()
989 r3.remove_vpp_config()
990 t.remove_vpp_config()
991
Neale Ranns37be7362017-02-21 17:30:26 -0800992
Neale Ranns180279b2017-03-16 15:49:09 -0400993class TestIPDisabled(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200994 """IPv4 disabled"""
Neale Ranns180279b2017-03-16 15:49:09 -0400995
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700996 @classmethod
997 def setUpClass(cls):
998 super(TestIPDisabled, cls).setUpClass()
999
1000 @classmethod
1001 def tearDownClass(cls):
1002 super(TestIPDisabled, cls).tearDownClass()
1003
Neale Ranns180279b2017-03-16 15:49:09 -04001004 def setUp(self):
1005 super(TestIPDisabled, self).setUp()
1006
1007 # create 2 pg interfaces
1008 self.create_pg_interfaces(range(2))
1009
1010 # PG0 is IP enalbed
1011 self.pg0.admin_up()
1012 self.pg0.config_ip4()
1013 self.pg0.resolve_arp()
1014
1015 # PG 1 is not IP enabled
1016 self.pg1.admin_up()
1017
1018 def tearDown(self):
1019 super(TestIPDisabled, self).tearDown()
1020 for i in self.pg_interfaces:
1021 i.unconfig_ip4()
1022 i.admin_down()
1023
Neale Ranns180279b2017-03-16 15:49:09 -04001024 def test_ip_disabled(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001025 """IP Disabled"""
Neale Ranns180279b2017-03-16 15:49:09 -04001026
Neale Ranns990f6942020-10-20 07:20:17 +00001027 MRouteItfFlags = VppEnum.vl_api_mfib_itf_flags_t
1028 MRouteEntryFlags = VppEnum.vl_api_mfib_entry_flags_t
1029
Neale Ranns180279b2017-03-16 15:49:09 -04001030 #
1031 # An (S,G).
1032 # one accepting interface, pg0, 2 forwarding interfaces
1033 #
1034 route_232_1_1_1 = VppIpMRoute(
1035 self,
1036 "0.0.0.0",
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001037 "232.1.1.1",
1038 32,
Neale Ranns990f6942020-10-20 07:20:17 +00001039 MRouteEntryFlags.MFIB_API_ENTRY_FLAG_NONE,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001040 [
1041 VppMRoutePath(
1042 self.pg1.sw_if_index, MRouteItfFlags.MFIB_API_ITF_FLAG_ACCEPT
1043 ),
1044 VppMRoutePath(
1045 self.pg0.sw_if_index, MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD
1046 ),
1047 ],
1048 )
Neale Ranns180279b2017-03-16 15:49:09 -04001049 route_232_1_1_1.add_vpp_config()
1050
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001051 pu = (
1052 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1053 / IP(src="10.10.10.10", dst=self.pg0.remote_ip4)
1054 / UDP(sport=1234, dport=1234)
1055 / Raw(b"\xa5" * 100)
1056 )
1057 pm = (
1058 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1059 / IP(src="10.10.10.10", dst="232.1.1.1")
1060 / UDP(sport=1234, dport=1234)
1061 / Raw(b"\xa5" * 100)
1062 )
Neale Ranns180279b2017-03-16 15:49:09 -04001063
1064 #
1065 # PG1 does not forward IP traffic
1066 #
1067 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
1068 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
1069
1070 #
1071 # IP enable PG1
1072 #
1073 self.pg1.config_ip4()
1074
1075 #
1076 # Now we get packets through
1077 #
1078 self.pg1.add_stream(pu)
1079 self.pg_enable_capture(self.pg_interfaces)
1080 self.pg_start()
1081 rx = self.pg0.get_capture(1)
1082
1083 self.pg1.add_stream(pm)
1084 self.pg_enable_capture(self.pg_interfaces)
1085 self.pg_start()
1086 rx = self.pg0.get_capture(1)
1087
1088 #
1089 # Disable PG1
1090 #
1091 self.pg1.unconfig_ip4()
1092
1093 #
1094 # PG1 does not forward IP traffic
1095 #
1096 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
1097 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
1098
1099
Neale Ranns9a69a602017-03-26 10:56:33 -07001100class TestIPSubNets(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001101 """IPv4 Subnets"""
Neale Ranns9a69a602017-03-26 10:56:33 -07001102
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001103 @classmethod
1104 def setUpClass(cls):
1105 super(TestIPSubNets, cls).setUpClass()
1106
1107 @classmethod
1108 def tearDownClass(cls):
1109 super(TestIPSubNets, cls).tearDownClass()
1110
Neale Ranns9a69a602017-03-26 10:56:33 -07001111 def setUp(self):
1112 super(TestIPSubNets, self).setUp()
1113
1114 # create a 2 pg interfaces
1115 self.create_pg_interfaces(range(2))
1116
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001117 # pg0 we will use to experiment
Neale Ranns9a69a602017-03-26 10:56:33 -07001118 self.pg0.admin_up()
1119
1120 # pg1 is setup normally
1121 self.pg1.admin_up()
1122 self.pg1.config_ip4()
1123 self.pg1.resolve_arp()
1124
1125 def tearDown(self):
1126 super(TestIPSubNets, self).tearDown()
1127 for i in self.pg_interfaces:
1128 i.admin_down()
1129
Neale Ranns9a69a602017-03-26 10:56:33 -07001130 def test_ip_sub_nets(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001131 """IP Sub Nets"""
Neale Ranns9a69a602017-03-26 10:56:33 -07001132
1133 #
1134 # Configure a covering route to forward so we know
1135 # when we are dropping
1136 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001137 cover_route = VppIpRoute(
1138 self,
1139 "10.0.0.0",
1140 8,
1141 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
1142 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001143 cover_route.add_vpp_config()
1144
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001145 p = (
1146 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1147 / IP(dst="10.10.10.10", src=self.pg0.local_ip4)
1148 / UDP(sport=1234, dport=1234)
1149 / Raw(b"\xa5" * 100)
1150 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001151
1152 self.pg1.add_stream(p)
1153 self.pg_enable_capture(self.pg_interfaces)
1154 self.pg_start()
1155 rx = self.pg1.get_capture(1)
1156
1157 #
1158 # Configure some non-/24 subnets on an IP interface
1159 #
1160 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
1161
Ole Troan9a475372019-03-05 16:58:24 +01001162 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001163 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/16"
1164 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001165
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001166 pn = (
1167 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1168 / IP(dst="10.10.0.0", src=self.pg0.local_ip4)
1169 / UDP(sport=1234, dport=1234)
1170 / Raw(b"\xa5" * 100)
1171 )
1172 pb = (
1173 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1174 / IP(dst="10.10.255.255", src=self.pg0.local_ip4)
1175 / UDP(sport=1234, dport=1234)
1176 / Raw(b"\xa5" * 100)
1177 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001178
1179 self.send_and_assert_no_replies(self.pg1, pn, "IP Network address")
1180 self.send_and_assert_no_replies(self.pg1, pb, "IP Broadcast address")
1181
1182 # remove the sub-net and we are forwarding via the cover again
Ole Troan9a475372019-03-05 16:58:24 +01001183 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001184 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/16", is_add=0
1185 )
Jakub Grajciar053204a2019-03-18 13:17:53 +01001186
Neale Ranns9a69a602017-03-26 10:56:33 -07001187 self.pg1.add_stream(pn)
1188 self.pg_enable_capture(self.pg_interfaces)
1189 self.pg_start()
1190 rx = self.pg1.get_capture(1)
1191 self.pg1.add_stream(pb)
1192 self.pg_enable_capture(self.pg_interfaces)
1193 self.pg_start()
1194 rx = self.pg1.get_capture(1)
1195
1196 #
1197 # A /31 is a special case where the 'other-side' is an attached host
1198 # packets to that peer generate ARP requests
1199 #
1200 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
1201
Ole Troan9a475372019-03-05 16:58:24 +01001202 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001203 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/31"
1204 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001205
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001206 pn = (
1207 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
1208 / IP(dst="10.10.10.11", src=self.pg0.local_ip4)
1209 / UDP(sport=1234, dport=1234)
1210 / Raw(b"\xa5" * 100)
1211 )
Neale Ranns9a69a602017-03-26 10:56:33 -07001212
1213 self.pg1.add_stream(pn)
1214 self.pg_enable_capture(self.pg_interfaces)
1215 self.pg_start()
1216 rx = self.pg0.get_capture(1)
1217 rx[ARP]
1218
1219 # remove the sub-net and we are forwarding via the cover again
Ole Troan9a475372019-03-05 16:58:24 +01001220 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001221 sw_if_index=self.pg0.sw_if_index, prefix="10.10.10.10/31", is_add=0
1222 )
Jakub Grajciar053204a2019-03-18 13:17:53 +01001223
Neale Ranns9a69a602017-03-26 10:56:33 -07001224 self.pg1.add_stream(pn)
1225 self.pg_enable_capture(self.pg_interfaces)
1226 self.pg_start()
1227 rx = self.pg1.get_capture(1)
1228
1229
Neale Ranns227038a2017-04-21 01:07:59 -07001230class TestIPLoadBalance(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001231 """IPv4 Load-Balancing"""
Neale Ranns227038a2017-04-21 01:07:59 -07001232
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001233 @classmethod
1234 def setUpClass(cls):
1235 super(TestIPLoadBalance, cls).setUpClass()
1236
1237 @classmethod
1238 def tearDownClass(cls):
1239 super(TestIPLoadBalance, cls).tearDownClass()
1240
Neale Ranns227038a2017-04-21 01:07:59 -07001241 def setUp(self):
1242 super(TestIPLoadBalance, self).setUp()
1243
1244 self.create_pg_interfaces(range(5))
Neale Ranns15002542017-09-10 04:39:11 -07001245 mpls_tbl = VppMplsTable(self, 0)
1246 mpls_tbl.add_vpp_config()
Neale Ranns227038a2017-04-21 01:07:59 -07001247
1248 for i in self.pg_interfaces:
1249 i.admin_up()
1250 i.config_ip4()
1251 i.resolve_arp()
Neale Ranns71275e32017-05-25 12:38:58 -07001252 i.enable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001253
1254 def tearDown(self):
Neale Ranns227038a2017-04-21 01:07:59 -07001255 for i in self.pg_interfaces:
Neale Ranns71275e32017-05-25 12:38:58 -07001256 i.disable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001257 i.unconfig_ip4()
1258 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -07001259 super(TestIPLoadBalance, self).tearDown()
Neale Ranns227038a2017-04-21 01:07:59 -07001260
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001261 def total_len(self, rxs):
1262 n = 0
1263 for rx in rxs:
1264 n += len(rx)
1265 return n
1266
Neale Ranns227038a2017-04-21 01:07:59 -07001267 def test_ip_load_balance(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001268 """IP Load-Balancing"""
Neale Ranns227038a2017-04-21 01:07:59 -07001269
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001270 fhc = VppEnum.vl_api_ip_flow_hash_config_t
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001271 fhcv2 = VppEnum.vl_api_ip_flow_hash_config_v2_t
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001272 af = VppEnum.vl_api_address_family_t
1273
Neale Ranns227038a2017-04-21 01:07:59 -07001274 #
1275 # An array of packets that differ only in the destination port
1276 #
Neale Ranns71275e32017-05-25 12:38:58 -07001277 port_ip_pkts = []
1278 port_mpls_pkts = []
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001279 port_gtp_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001280
1281 #
1282 # An array of packets that differ only in the source address
1283 #
Neale Ranns71275e32017-05-25 12:38:58 -07001284 src_ip_pkts = []
1285 src_mpls_pkts = []
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001286 src_gtp_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001287
Paul Vinciguerra4271c972019-05-14 13:25:49 -04001288 for ii in range(NUM_PKTS):
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001289 internal_src_ip_hdr = IP(dst="10.0.0.1", src="20.0.0.1")
1290
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001291 port_ip_hdr = (
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001292 internal_src_ip_hdr
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001293 / UDP(sport=1234, dport=1234 + ii)
1294 / Raw(b"\xa5" * 100)
1295 )
1296 port_ip_pkts.append(
1297 (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / port_ip_hdr)
1298 )
1299 port_mpls_pkts.append(
1300 (
1301 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1302 / MPLS(label=66, ttl=2)
1303 / port_ip_hdr
1304 )
1305 )
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001306 port_gtp_pkts.append(
1307 (
1308 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1309 / internal_src_ip_hdr
1310 / UDP(sport=2152, dport=2152, chksum=0)
1311 / GTP_U_Header(gtp_type="g_pdu", teid=200)
1312 / Raw(b"\xa5" * 100)
1313 )
1314 )
Neale Ranns71275e32017-05-25 12:38:58 -07001315
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001316 src_ip_hdr = (
1317 IP(dst="10.0.0.1", src="20.0.0.%d" % ii)
1318 / UDP(sport=1234, dport=1234)
1319 / Raw(b"\xa5" * 100)
1320 )
1321 src_ip_pkts.append(
1322 (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / src_ip_hdr)
1323 )
1324 src_mpls_pkts.append(
1325 (
1326 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1327 / MPLS(label=66, ttl=2)
1328 / src_ip_hdr
1329 )
1330 )
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001331 src_gtp_pkts.append(
1332 (
1333 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1334 / IP(dst="10.0.0.1", src="20.0.0.1")
1335 / UDP(sport=2152, dport=2152, chksum=0)
1336 / GTP_U_Header(gtp_type="g_pdu", teid=ii)
1337 / Raw(b"\xa5" * 100)
1338 )
1339 )
Neale Ranns227038a2017-04-21 01:07:59 -07001340
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001341 route_10_0_0_1 = VppIpRoute(
1342 self,
1343 "10.0.0.1",
1344 32,
1345 [
1346 VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index),
1347 VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index),
1348 ],
1349 )
Neale Ranns227038a2017-04-21 01:07:59 -07001350 route_10_0_0_1.add_vpp_config()
1351
Neale Ranns71275e32017-05-25 12:38:58 -07001352 binding = VppMplsIpBind(self, 66, "10.0.0.1", 32)
1353 binding.add_vpp_config()
1354
Neale Ranns227038a2017-04-21 01:07:59 -07001355 #
1356 # inject the packet on pg0 - expect load-balancing across the 2 paths
1357 # - since the default hash config is to use IP src,dst and port
1358 # src,dst
1359 # We are not going to ensure equal amounts of packets across each link,
1360 # since the hash algorithm is statistical and therefore this can never
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001361 # be guaranteed. But with 64 different packets we do expect some
Neale Ranns227038a2017-04-21 01:07:59 -07001362 # balancing. So instead just ensure there is traffic on each link.
1363 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001364 rx = self.send_and_expect_load_balancing(
1365 self.pg0, port_ip_pkts, [self.pg1, self.pg2]
1366 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001367 n_ip_pg0 = len(rx[0])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001368 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts, [self.pg1, self.pg2])
1369 self.send_and_expect_load_balancing(
1370 self.pg0, port_mpls_pkts, [self.pg1, self.pg2]
1371 )
1372 rx = self.send_and_expect_load_balancing(
1373 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1374 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001375 n_mpls_pg0 = len(rx[0])
1376
1377 #
1378 # change the router ID and expect the distribution changes
1379 #
1380 self.vapi.set_ip_flow_hash_router_id(router_id=0x11111111)
1381
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001382 rx = self.send_and_expect_load_balancing(
1383 self.pg0, port_ip_pkts, [self.pg1, self.pg2]
1384 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001385 self.assertNotEqual(n_ip_pg0, len(rx[0]))
1386
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001387 rx = self.send_and_expect_load_balancing(
1388 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1389 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001390 self.assertNotEqual(n_mpls_pg0, len(rx[0]))
Neale Ranns227038a2017-04-21 01:07:59 -07001391
1392 #
1393 # change the flow hash config so it's only IP src,dst
1394 # - now only the stream with differing source address will
1395 # load-balance
1396 #
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00001397 self.vapi.set_ip_flow_hash_v2(
1398 af=af.ADDRESS_IP4,
1399 table_id=0,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001400 flow_hash_config=(
1401 fhc.IP_API_FLOW_HASH_SRC_IP
1402 | fhc.IP_API_FLOW_HASH_DST_IP
1403 | fhc.IP_API_FLOW_HASH_PROTO
1404 ),
1405 )
Neale Ranns227038a2017-04-21 01:07:59 -07001406
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001407 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts, [self.pg1, self.pg2])
1408 self.send_and_expect_load_balancing(
1409 self.pg0, src_mpls_pkts, [self.pg1, self.pg2]
1410 )
Neale Ranns227038a2017-04-21 01:07:59 -07001411
Neale Ranns699bea22022-02-17 09:22:16 +00001412 self.send_and_expect_only(self.pg0, port_ip_pkts, self.pg2)
Neale Ranns227038a2017-04-21 01:07:59 -07001413
1414 #
Takeru Hayasakab23c6f42023-01-17 04:45:58 +09001415 # this case gtp v1 teid key LB
1416 #
1417 self.vapi.set_ip_flow_hash_v3(
1418 af=af.ADDRESS_IP4,
1419 table_id=0,
1420 flow_hash_config=(
1421 fhcv2.IP_API_V2_FLOW_HASH_SRC_IP
1422 | fhcv2.IP_API_V2_FLOW_HASH_PROTO
1423 | fhcv2.IP_API_V2_FLOW_HASH_GTPV1_TEID
1424 ),
1425 )
1426 self.logger.info(self.vapi.cli("show ip fib"))
1427
1428 self.send_and_expect_load_balancing(
1429 self.pg0, src_gtp_pkts, [self.pg1, self.pg2]
1430 )
1431
1432 self.send_and_expect_only(self.pg0, port_gtp_pkts, self.pg2)
1433
1434 #
Neale Ranns227038a2017-04-21 01:07:59 -07001435 # change the flow hash config back to defaults
1436 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001437 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 -07001438
1439 #
1440 # Recursive prefixes
1441 # - testing that 2 stages of load-balancing occurs and there is no
1442 # polarisation (i.e. only 2 of 4 paths are used)
1443 #
1444 port_pkts = []
1445 src_pkts = []
1446
1447 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001448 port_pkts.append(
1449 (
1450 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1451 / IP(dst="1.1.1.1", src="20.0.0.1")
1452 / UDP(sport=1234, dport=1234 + ii)
1453 / Raw(b"\xa5" * 100)
1454 )
1455 )
1456 src_pkts.append(
1457 (
1458 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1459 / IP(dst="1.1.1.1", src="20.0.0.%d" % ii)
1460 / UDP(sport=1234, dport=1234)
1461 / Raw(b"\xa5" * 100)
1462 )
1463 )
Neale Ranns227038a2017-04-21 01:07:59 -07001464
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001465 route_10_0_0_2 = VppIpRoute(
1466 self,
1467 "10.0.0.2",
1468 32,
1469 [
1470 VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index),
1471 VppRoutePath(self.pg4.remote_ip4, self.pg4.sw_if_index),
1472 ],
1473 )
Neale Ranns227038a2017-04-21 01:07:59 -07001474 route_10_0_0_2.add_vpp_config()
1475
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001476 route_1_1_1_1 = VppIpRoute(
1477 self,
1478 "1.1.1.1",
1479 32,
1480 [
1481 VppRoutePath("10.0.0.2", 0xFFFFFFFF),
1482 VppRoutePath("10.0.0.1", 0xFFFFFFFF),
1483 ],
1484 )
Neale Ranns227038a2017-04-21 01:07:59 -07001485 route_1_1_1_1.add_vpp_config()
1486
1487 #
1488 # inject the packet on pg0 - expect load-balancing across all 4 paths
1489 #
1490 self.vapi.cli("clear trace")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001491 self.send_and_expect_load_balancing(
1492 self.pg0, port_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1493 )
1494 self.send_and_expect_load_balancing(
1495 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1496 )
Neale Ranns227038a2017-04-21 01:07:59 -07001497
Neale Ranns42e6b092017-07-31 02:56:03 -07001498 #
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001499 # bring down pg1 expect LB to adjust to use only those that are up
Neale Ranns63480742019-03-13 06:41:52 -07001500 #
1501 self.pg1.link_down()
1502
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001503 rx = self.send_and_expect_load_balancing(
1504 self.pg0, src_pkts, [self.pg2, self.pg3, self.pg4]
1505 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001506 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001507
1508 #
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001509 # bring down pg2 expect LB to adjust to use only those that are up
Neale Ranns63480742019-03-13 06:41:52 -07001510 #
1511 self.pg2.link_down()
1512
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001513 rx = self.send_and_expect_load_balancing(
1514 self.pg0, src_pkts, [self.pg3, self.pg4]
1515 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001516 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001517
1518 #
1519 # bring the links back up - expect LB over all again
1520 #
1521 self.pg1.link_up()
1522 self.pg2.link_up()
1523
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001524 rx = self.send_and_expect_load_balancing(
1525 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1526 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001527 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001528
1529 #
1530 # The same link-up/down but this time admin state
1531 #
1532 self.pg1.admin_down()
1533 self.pg2.admin_down()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001534 rx = self.send_and_expect_load_balancing(
1535 self.pg0, src_pkts, [self.pg3, self.pg4]
1536 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001537 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001538 self.pg1.admin_up()
1539 self.pg2.admin_up()
1540 self.pg1.resolve_arp()
1541 self.pg2.resolve_arp()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001542 rx = self.send_and_expect_load_balancing(
1543 self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]
1544 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001545 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001546
1547 #
Neale Ranns42e6b092017-07-31 02:56:03 -07001548 # Recursive prefixes
1549 # - testing that 2 stages of load-balancing, no choices
1550 #
1551 port_pkts = []
1552
1553 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001554 port_pkts.append(
1555 (
1556 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1557 / IP(dst="1.1.1.2", src="20.0.0.2")
1558 / UDP(sport=1234, dport=1234 + ii)
1559 / Raw(b"\xa5" * 100)
1560 )
1561 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001562
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001563 route_10_0_0_3 = VppIpRoute(
1564 self,
1565 "10.0.0.3",
1566 32,
1567 [VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index)],
1568 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001569 route_10_0_0_3.add_vpp_config()
1570
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001571 route_1_1_1_2 = VppIpRoute(
1572 self, "1.1.1.2", 32, [VppRoutePath("10.0.0.3", 0xFFFFFFFF)]
1573 )
Neale Ranns42e6b092017-07-31 02:56:03 -07001574 route_1_1_1_2.add_vpp_config()
1575
1576 #
Neale Ranns63480742019-03-13 06:41:52 -07001577 # inject the packet on pg0 - rx only on via routes output interface
Neale Ranns42e6b092017-07-31 02:56:03 -07001578 #
1579 self.vapi.cli("clear trace")
Neale Ranns699bea22022-02-17 09:22:16 +00001580 self.send_and_expect_only(self.pg0, port_pkts, self.pg3)
Neale Ranns42e6b092017-07-31 02:56:03 -07001581
Neale Ranns63480742019-03-13 06:41:52 -07001582 #
1583 # Add a LB route in the presence of a down link - expect no
1584 # packets over the down link
1585 #
1586 self.pg3.link_down()
1587
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001588 route_10_0_0_3 = VppIpRoute(
1589 self,
1590 "10.0.0.3",
1591 32,
1592 [
1593 VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index),
1594 VppRoutePath(self.pg4.remote_ip4, self.pg4.sw_if_index),
1595 ],
1596 )
Neale Ranns63480742019-03-13 06:41:52 -07001597 route_10_0_0_3.add_vpp_config()
1598
1599 port_pkts = []
1600 for ii in range(257):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001601 port_pkts.append(
1602 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1603 / IP(dst="10.0.0.3", src="20.0.0.2")
1604 / UDP(sport=1234, dport=1234 + ii)
1605 / Raw(b"\xa5" * 100)
1606 )
Neale Ranns63480742019-03-13 06:41:52 -07001607
Neale Ranns699bea22022-02-17 09:22:16 +00001608 self.send_and_expect_only(self.pg0, port_pkts, self.pg4)
Neale Ranns63480742019-03-13 06:41:52 -07001609
1610 # bring the link back up
1611 self.pg3.link_up()
1612
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001613 rx = self.send_and_expect_load_balancing(
1614 self.pg0, port_pkts, [self.pg3, self.pg4]
1615 )
Neale Ranns3d5f08a2021-01-22 16:12:38 +00001616 self.assertEqual(len(src_pkts), self.total_len(rx))
Neale Ranns63480742019-03-13 06:41:52 -07001617
Neale Ranns30d0fd42017-05-30 07:30:04 -07001618
1619class TestIPVlan0(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001620 """IPv4 VLAN-0"""
Neale Ranns30d0fd42017-05-30 07:30:04 -07001621
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001622 @classmethod
1623 def setUpClass(cls):
1624 super(TestIPVlan0, cls).setUpClass()
1625
1626 @classmethod
1627 def tearDownClass(cls):
1628 super(TestIPVlan0, cls).tearDownClass()
1629
Neale Ranns30d0fd42017-05-30 07:30:04 -07001630 def setUp(self):
1631 super(TestIPVlan0, self).setUp()
1632
1633 self.create_pg_interfaces(range(2))
Neale Ranns15002542017-09-10 04:39:11 -07001634 mpls_tbl = VppMplsTable(self, 0)
1635 mpls_tbl.add_vpp_config()
Neale Ranns30d0fd42017-05-30 07:30:04 -07001636
1637 for i in self.pg_interfaces:
1638 i.admin_up()
1639 i.config_ip4()
1640 i.resolve_arp()
1641 i.enable_mpls()
1642
1643 def tearDown(self):
Neale Ranns30d0fd42017-05-30 07:30:04 -07001644 for i in self.pg_interfaces:
1645 i.disable_mpls()
1646 i.unconfig_ip4()
1647 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -07001648 super(TestIPVlan0, self).tearDown()
Neale Ranns30d0fd42017-05-30 07:30:04 -07001649
Neale Ranns30d0fd42017-05-30 07:30:04 -07001650 def test_ip_vlan_0(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001651 """IP VLAN-0"""
Neale Ranns30d0fd42017-05-30 07:30:04 -07001652
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001653 pkts = (
1654 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1655 / Dot1Q(vlan=0)
1656 / IP(dst=self.pg1.remote_ip4, src=self.pg0.remote_ip4)
1657 / UDP(sport=1234, dport=1234)
1658 / Raw(b"\xa5" * 100)
1659 ) * NUM_PKTS
Neale Ranns30d0fd42017-05-30 07:30:04 -07001660
1661 #
1662 # Expect that packets sent on VLAN-0 are forwarded on the
1663 # main interface.
1664 #
1665 self.send_and_expect(self.pg0, pkts, self.pg1)
1666
1667
Brian Russell318fdb82021-01-19 16:56:32 +00001668class IPPuntSetup(object):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001669 """Setup for IPv4 Punt Police/Redirect"""
Neale Rannsd91c1db2017-07-31 02:30:50 -07001670
Brian Russell318fdb82021-01-19 16:56:32 +00001671 def punt_setup(self):
Pavel Kotucek609e1212018-11-27 09:59:44 +01001672 self.create_pg_interfaces(range(4))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001673
1674 for i in self.pg_interfaces:
1675 i.admin_up()
1676 i.config_ip4()
1677 i.resolve_arp()
1678
Neale Ranns68577d22019-06-04 13:31:23 +00001679 # use UDP packet that have a port we need to explicitly
1680 # register to get punted.
1681 pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4
1682 af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4
1683 udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP
1684 punt_udp = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001685 "type": pt_l4,
1686 "punt": {
1687 "l4": {
1688 "af": af_ip4,
1689 "protocol": udp_proto,
1690 "port": 1234,
Neale Ranns68577d22019-06-04 13:31:23 +00001691 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001692 },
Neale Ranns68577d22019-06-04 13:31:23 +00001693 }
1694
1695 self.vapi.set_punt(is_add=1, punt=punt_udp)
1696
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001697 af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6
1698 punt_udp = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001699 "type": pt_l4,
1700 "punt": {
1701 "l4": {
1702 "af": af_ip6,
1703 "protocol": udp_proto,
1704 "port": 1236,
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001705 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001706 },
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001707 }
1708
1709 self.vapi.set_punt(is_add=1, punt=punt_udp)
1710
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001711 self.pkt = (
1712 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1713 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
1714 / UDP(sport=1234, dport=1234)
1715 / Raw(b"\xa5" * 100)
1716 )
Neale Rannsd91c1db2017-07-31 02:30:50 -07001717
Brian Russell318fdb82021-01-19 16:56:32 +00001718 def punt_teardown(self):
1719 for i in self.pg_interfaces:
1720 i.unconfig_ip4()
1721 i.admin_down()
1722
1723
1724class TestIPPunt(IPPuntSetup, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001725 """IPv4 Punt Police/Redirect"""
Brian Russell318fdb82021-01-19 16:56:32 +00001726
1727 def setUp(self):
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001728 super().setUp()
1729 super().punt_setup()
Brian Russell318fdb82021-01-19 16:56:32 +00001730
1731 def tearDown(self):
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001732 super().punt_teardown()
1733 super().tearDown()
1734
1735 def test_ip_punt_api_validation(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001736 """IP punt API parameter validation"""
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001737
1738 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001739 punt = {
1740 "rx_sw_if_index": self.pg0.sw_if_index,
1741 "af": VppEnum.vl_api_address_family_t.ADDRESS_IP4,
1742 "n_paths": 1000000,
1743 "paths": [],
1744 }
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001745
1746 with self.assertRaises(vpp_papi.VPPIOError):
1747 self.vapi.add_del_ip_punt_redirect_v2(punt=punt, is_add=True)
1748
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001749 punt = {
1750 "rx_sw_if_index": self.pg0.sw_if_index,
1751 "af": VppEnum.vl_api_address_family_t.ADDRESS_IP4,
1752 "n_paths": 0,
1753 "paths": [],
1754 }
Klement Sekerafd1f56a2021-11-22 21:25:57 +01001755
1756 self.vapi.add_del_ip_punt_redirect_v2(punt=punt, is_add=True)
Brian Russell318fdb82021-01-19 16:56:32 +00001757
1758 def test_ip_punt(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001759 """IP punt police and redirect"""
Brian Russell318fdb82021-01-19 16:56:32 +00001760
1761 pkts = self.pkt * 1025
Neale Rannsd91c1db2017-07-31 02:30:50 -07001762
1763 #
1764 # Configure a punt redirect via pg1.
1765 #
Ole Troan0bcad322018-12-11 13:04:01 +01001766 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001767 ip_punt_redirect = VppIpPuntRedirect(
1768 self, self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr
1769 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001770 ip_punt_redirect.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001771
1772 self.send_and_expect(self.pg0, pkts, self.pg1)
1773
1774 #
1775 # add a policer
1776 #
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001777 policer = VppPolicer(self, "ip4-punt", 400, 0, 10, 0, rate_type=1)
1778 policer.add_vpp_config()
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001779 ip_punt_policer = VppIpPuntPolicer(self, policer.policer_index)
1780 ip_punt_policer.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001781
1782 self.vapi.cli("clear trace")
1783 self.pg0.add_stream(pkts)
1784 self.pg_enable_capture(self.pg_interfaces)
1785 self.pg_start()
1786
1787 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001788 # the number of packet received should be greater than 0,
Neale Rannsd91c1db2017-07-31 02:30:50 -07001789 # but not equal to the number sent, since some were policed
1790 #
1791 rx = self.pg1._get_capture(1)
Brian Russelle9887262021-01-27 14:45:22 +00001792
1793 stats = policer.get_stats()
1794
1795 # Single rate policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001796 self.assertGreater(stats["conform_packets"], 0)
1797 self.assertEqual(stats["exceed_packets"], 0)
1798 self.assertGreater(stats["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00001799
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08001800 self.assertGreater(len(rx), 0)
1801 self.assertLess(len(rx), len(pkts))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001802
1803 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001804 # remove the policer. back to full rx
Neale Rannsd91c1db2017-07-31 02:30:50 -07001805 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001806 ip_punt_policer.remove_vpp_config()
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001807 policer.remove_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001808 self.send_and_expect(self.pg0, pkts, self.pg1)
1809
1810 #
1811 # remove the redirect. expect full drop.
1812 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001813 ip_punt_redirect.remove_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001814 self.send_and_assert_no_replies(self.pg0, pkts, "IP no punt config")
Neale Rannsd91c1db2017-07-31 02:30:50 -07001815
1816 #
1817 # Add a redirect that is not input port selective
1818 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001819 ip_punt_redirect = VppIpPuntRedirect(
1820 self, 0xFFFFFFFF, self.pg1.sw_if_index, nh_addr
1821 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001822 ip_punt_redirect.add_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001823 self.send_and_expect(self.pg0, pkts, self.pg1)
Jakub Grajciar2df2f752020-12-01 11:23:44 +01001824 ip_punt_redirect.remove_vpp_config()
Neale Rannsd91c1db2017-07-31 02:30:50 -07001825
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001826 def test_ip_punt_vrf(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001827 """IP punt/local with VRFs"""
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001828
1829 # use a punt redirect to test if for-us packets are accepted
1830 pkts = self.pkt * 1025
1831
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001832 vlans_pg0 = [VppDot1QSubint(self, self.pg0, v) for v in range(100, 104)]
1833 vlans_pg1 = [VppDot1QSubint(self, self.pg1, v) for v in range(100, 104)]
1834 tbl4 = [VppIpTable(self, v).add_vpp_config() for v in range(100, 104)]
1835 tbl6 = [VppIpTable(self, v, True).add_vpp_config() for v in range(100, 104)]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001836
1837 for v in vlans_pg0 + vlans_pg1:
1838 v.admin_up()
1839 v.set_table_ip4(v.vlan)
1840 v.set_table_ip6(v.vlan)
1841 v.config_ip4()
1842 v.config_ip6()
1843 v.resolve_arp()
1844 v.resolve_ndp()
1845
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001846 [
1847 VppIpPuntRedirect(
1848 self,
1849 vlans_pg0[i].sw_if_index,
1850 vlans_pg1[i].sw_if_index,
1851 vlans_pg1[i].remote_ip4,
1852 ).add_vpp_config()
1853 for i in range(4)
1854 ]
1855 [
1856 VppIpPuntRedirect(
1857 self,
1858 vlans_pg0[i].sw_if_index,
1859 vlans_pg1[i].sw_if_index,
1860 vlans_pg1[i].remote_ip6,
1861 ).add_vpp_config()
1862 for i in range(4)
1863 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001864
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001865 pkts = [
1866 (
1867 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1868 / Dot1Q(vlan=i.vlan)
1869 / IP(src=i.remote_ip4, dst=i.local_ip4)
1870 / UDP(sport=1234, dport=1234)
1871 / Raw(b"\xa5" * 100)
1872 )
1873 for i in vlans_pg0
1874 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001875
1876 self.send_and_expect(self.pg0, pkts, self.pg1)
1877
1878 #
1879 # IPv4
1880 #
1881
1882 # we reject packets for source addresses in the wrong vlan/VRF
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001883 pkts = [
1884 (
1885 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1886 / Dot1Q(vlan=i.vlan)
1887 / IP(src="1.1.1.1", dst=i.local_ip4)
1888 / UDP(sport=1234, dport=1234)
1889 / Raw(b"\xa5" * 100)
1890 )
1891 for i in vlans_pg0
1892 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001893 # single and dual loop
1894 self.send_and_assert_no_replies(self.pg0, [pkts[0]])
1895 self.send_and_assert_no_replies(self.pg0, pkts)
1896
Neale Rannse22a7042022-08-09 03:03:29 +00001897 self.assert_error_counter_equal("/err/ip4-local/src_lookup_miss", len(pkts) + 1)
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001898
1899 # using the same source in different tables, should reject
1900 # for the table that the source is not present in
1901 # the first packet in the stream is drop
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001902 pkts = [
1903 (
1904 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1905 / Dot1Q(vlan=i.vlan)
1906 / IP(src=vlans_pg0[0].remote_ip4, dst=i.local_ip4)
1907 / UDP(sport=1234, dport=1234)
1908 / Raw(b"\xa5" * 100)
1909 )
1910 for i in vlans_pg0
1911 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001912 # single loop accept and drop
1913 # followed by both in the same frame/loop
1914 self.send_and_expect(self.pg0, [pkts[0]], self.pg1)
1915 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1916 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1917
1918 # using the same source in different tables, should reject
1919 # for the table that the source is not present in
1920 # the first packet in the stream is accept
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001921 pkts = [
1922 (
1923 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1924 / Dot1Q(vlan=i.vlan)
1925 / IP(src=vlans_pg0[3].remote_ip4, dst=i.local_ip4)
1926 / UDP(sport=1234, dport=1234)
1927 / Raw(b"\xa5" * 100)
1928 )
1929 for i in vlans_pg0
1930 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001931
1932 # single loop accept and drop
1933 # followed by both in the same frame/loop
1934 self.send_and_expect(self.pg0, [pkts[3]], self.pg1)
1935 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1936 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1937
1938 #
1939 # IPv6
1940 #
1941
1942 # we reject packets for source addresses in the wrong vlan/VRF
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001943 pkts = [
1944 (
1945 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1946 / Dot1Q(vlan=i.vlan)
1947 / IPv6(src="1::1", dst=i.local_ip6)
1948 / UDP(sport=1236, dport=1236)
1949 / Raw(b"\xa5" * 100)
1950 )
1951 for i in vlans_pg0
1952 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001953 # single and dual loop
1954 self.send_and_assert_no_replies(self.pg0, [pkts[0]])
1955 self.send_and_assert_no_replies(self.pg0, pkts)
1956
Neale Rannse22a7042022-08-09 03:03:29 +00001957 self.assert_error_counter_equal("/err/ip6-input/src_lookup_miss", len(pkts) + 1)
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001958
1959 # using the same source in different tables, should reject
1960 # for the table that the source is not present in
1961 # the first packet in the stream is drop
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001962 pkts = [
1963 (
1964 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1965 / Dot1Q(vlan=i.vlan)
1966 / IPv6(src=vlans_pg0[0].remote_ip6, dst=i.local_ip6)
1967 / UDP(sport=1236, dport=1236)
1968 / Raw(b"\xa5" * 100)
1969 )
1970 for i in vlans_pg0
1971 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001972 # single loop accept and drop
1973 # followed by both in the same frame/loop
1974 self.send_and_expect(self.pg0, [pkts[0]], self.pg1)
1975 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1976 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1977
1978 # using the same source in different tables, should reject
1979 # for the table that the source is not present in
1980 # the first packet in the stream is accept
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001981 pkts = [
1982 (
1983 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
1984 / Dot1Q(vlan=i.vlan)
1985 / IPv6(src=vlans_pg0[3].remote_ip6, dst=i.local_ip6)
1986 / UDP(sport=1236, dport=1236)
1987 / Raw(b"\xa5" * 100)
1988 )
1989 for i in vlans_pg0
1990 ]
Neale Rannsaa7cfd02022-03-24 12:28:42 +00001991
1992 # single loop accept and drop
1993 # followed by both in the same frame/loop
1994 self.send_and_expect(self.pg0, [pkts[3]], self.pg1)
1995 self.send_and_assert_no_replies(self.pg0, [pkts[1]])
1996 self.send_and_expect(self.pg0, pkts * 4, self.pg1, n_rx=4)
1997
1998 for v in vlans_pg0 + vlans_pg1:
1999 v.unconfig_ip4()
2000 v.unconfig_ip6()
2001 v.set_table_ip4(0)
2002 v.set_table_ip6(0)
2003
Pavel Kotucek609e1212018-11-27 09:59:44 +01002004 def test_ip_punt_dump(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002005 """IP4 punt redirect dump"""
Pavel Kotucek609e1212018-11-27 09:59:44 +01002006
2007 #
2008 # Configure a punt redirects
2009 #
Ole Troan0bcad322018-12-11 13:04:01 +01002010 nh_address = self.pg3.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002011 ipr_03 = VppIpPuntRedirect(
2012 self, self.pg0.sw_if_index, self.pg3.sw_if_index, nh_address
2013 )
2014 ipr_13 = VppIpPuntRedirect(
2015 self, self.pg1.sw_if_index, self.pg3.sw_if_index, nh_address
2016 )
2017 ipr_23 = VppIpPuntRedirect(
2018 self, self.pg2.sw_if_index, self.pg3.sw_if_index, "0.0.0.0"
2019 )
Jakub Grajciar2df2f752020-12-01 11:23:44 +01002020 ipr_03.add_vpp_config()
2021 ipr_13.add_vpp_config()
2022 ipr_23.add_vpp_config()
Pavel Kotucek609e1212018-11-27 09:59:44 +01002023
2024 #
2025 # Dump pg0 punt redirects
2026 #
Jakub Grajciar2df2f752020-12-01 11:23:44 +01002027 self.assertTrue(ipr_03.query_vpp_config())
2028 self.assertTrue(ipr_13.query_vpp_config())
2029 self.assertTrue(ipr_23.query_vpp_config())
Pavel Kotucek609e1212018-11-27 09:59:44 +01002030
2031 #
2032 # Dump punt redirects for all interfaces
2033 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002034 punts = self.vapi.ip_punt_redirect_dump(0xFFFFFFFF)
Pavel Kotucek609e1212018-11-27 09:59:44 +01002035 self.assertEqual(len(punts), 3)
2036 for p in punts:
2037 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
Ole Troan0bcad322018-12-11 13:04:01 +01002038 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002039 self.assertEqual(str(punts[2].punt.nh), "0.0.0.0")
Pavel Kotucek609e1212018-11-27 09:59:44 +01002040
Neale Rannsd91c1db2017-07-31 02:30:50 -07002041
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002042class TestIPPuntHandoff(IPPuntSetup, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002043 """IPv4 Punt Policer thread handoff"""
2044
Klement Sekera8d815022021-03-15 16:58:10 +01002045 vpp_worker_count = 2
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002046
2047 def setUp(self):
2048 super(TestIPPuntHandoff, self).setUp()
2049 super(TestIPPuntHandoff, self).punt_setup()
2050
2051 def tearDown(self):
2052 super(TestIPPuntHandoff, self).punt_teardown()
2053 super(TestIPPuntHandoff, self).tearDown()
2054
2055 def test_ip_punt_policer_handoff(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002056 """IP4 punt policer thread handoff"""
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002057 pkts = self.pkt * NUM_PKTS
2058
2059 #
2060 # Configure a punt redirect via pg1.
2061 #
2062 nh_addr = self.pg1.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002063 ip_punt_redirect = VppIpPuntRedirect(
2064 self, self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr
2065 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002066 ip_punt_redirect.add_vpp_config()
2067
2068 action_tx = PolicerAction(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002069 VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
2070 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002071 #
2072 # This policer drops no packets, we are just
2073 # testing that they get to the right thread.
2074 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002075 policer = VppPolicer(
2076 self,
2077 "ip4-punt",
2078 400,
2079 0,
2080 10,
2081 0,
2082 1,
2083 0,
2084 0,
2085 False,
2086 action_tx,
2087 action_tx,
2088 action_tx,
2089 )
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002090 policer.add_vpp_config()
2091 ip_punt_policer = VppIpPuntPolicer(self, policer.policer_index)
2092 ip_punt_policer.add_vpp_config()
2093
2094 for worker in [0, 1]:
2095 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
Brian Russellbb983142021-02-10 13:56:06 +00002096 self.logger.debug(self.vapi.cli("show trace max 100"))
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002097
Brian Russelle9887262021-01-27 14:45:22 +00002098 # Combined stats, all threads
2099 stats = policer.get_stats()
2100
2101 # Single rate policer - expect conform, violate but no exceed
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002102 self.assertGreater(stats["conform_packets"], 0)
2103 self.assertEqual(stats["exceed_packets"], 0)
2104 self.assertGreater(stats["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00002105
2106 # Worker 0, should have done all the policing
2107 stats0 = policer.get_stats(worker=0)
2108 self.assertEqual(stats, stats0)
2109
2110 # Worker 1, should have handed everything off
2111 stats1 = policer.get_stats(worker=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002112 self.assertEqual(stats1["conform_packets"], 0)
2113 self.assertEqual(stats1["exceed_packets"], 0)
2114 self.assertEqual(stats1["violate_packets"], 0)
Brian Russelle9887262021-01-27 14:45:22 +00002115
Brian Russellbb983142021-02-10 13:56:06 +00002116 # Bind the policer to worker 1 and repeat
2117 policer.bind_vpp_config(1, True)
2118 for worker in [0, 1]:
2119 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
2120 self.logger.debug(self.vapi.cli("show trace max 100"))
2121
2122 # The 2 workers should now have policed the same amount
2123 stats = policer.get_stats()
2124 stats0 = policer.get_stats(worker=0)
2125 stats1 = policer.get_stats(worker=1)
2126
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002127 self.assertGreater(stats0["conform_packets"], 0)
2128 self.assertEqual(stats0["exceed_packets"], 0)
2129 self.assertGreater(stats0["violate_packets"], 0)
Brian Russellbb983142021-02-10 13:56:06 +00002130
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002131 self.assertGreater(stats1["conform_packets"], 0)
2132 self.assertEqual(stats1["exceed_packets"], 0)
2133 self.assertGreater(stats1["violate_packets"], 0)
Brian Russellbb983142021-02-10 13:56:06 +00002134
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002135 self.assertEqual(
2136 stats0["conform_packets"] + stats1["conform_packets"],
2137 stats["conform_packets"],
2138 )
Brian Russellbb983142021-02-10 13:56:06 +00002139
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002140 self.assertEqual(
2141 stats0["violate_packets"] + stats1["violate_packets"],
2142 stats["violate_packets"],
2143 )
Brian Russellbb983142021-02-10 13:56:06 +00002144
2145 # Unbind the policer and repeat
2146 policer.bind_vpp_config(1, False)
2147 for worker in [0, 1]:
2148 self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
2149 self.logger.debug(self.vapi.cli("show trace max 100"))
2150
2151 # The policer should auto-bind to worker 0 when packets arrive
2152 stats = policer.get_stats()
2153 stats0new = policer.get_stats(worker=0)
2154 stats1new = policer.get_stats(worker=1)
2155
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002156 self.assertGreater(stats0new["conform_packets"], stats0["conform_packets"])
2157 self.assertEqual(stats0new["exceed_packets"], 0)
2158 self.assertGreater(stats0new["violate_packets"], stats0["violate_packets"])
Brian Russellbb983142021-02-10 13:56:06 +00002159
2160 self.assertEqual(stats1, stats1new)
2161
Brian Russellc8f3cdf2021-01-19 16:57:42 +00002162 #
2163 # Clean up
2164 #
2165 ip_punt_policer.remove_vpp_config()
2166 policer.remove_vpp_config()
2167 ip_punt_redirect.remove_vpp_config()
2168
2169
Neale Ranns054c03a2017-10-13 05:15:07 -07002170class TestIPDeag(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002171 """IPv4 Deaggregate Routes"""
Neale Ranns054c03a2017-10-13 05:15:07 -07002172
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002173 @classmethod
2174 def setUpClass(cls):
2175 super(TestIPDeag, cls).setUpClass()
2176
2177 @classmethod
2178 def tearDownClass(cls):
2179 super(TestIPDeag, cls).tearDownClass()
2180
Neale Ranns054c03a2017-10-13 05:15:07 -07002181 def setUp(self):
2182 super(TestIPDeag, self).setUp()
2183
2184 self.create_pg_interfaces(range(3))
2185
2186 for i in self.pg_interfaces:
2187 i.admin_up()
2188 i.config_ip4()
2189 i.resolve_arp()
2190
2191 def tearDown(self):
2192 super(TestIPDeag, self).tearDown()
2193 for i in self.pg_interfaces:
2194 i.unconfig_ip4()
2195 i.admin_down()
2196
Neale Ranns054c03a2017-10-13 05:15:07 -07002197 def test_ip_deag(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002198 """IP Deag Routes"""
Neale Ranns054c03a2017-10-13 05:15:07 -07002199
2200 #
2201 # Create a table to be used for:
2202 # 1 - another destination address lookup
2203 # 2 - a source address lookup
2204 #
2205 table_dst = VppIpTable(self, 1)
2206 table_src = VppIpTable(self, 2)
2207 table_dst.add_vpp_config()
2208 table_src.add_vpp_config()
2209
2210 #
2211 # Add a route in the default table to point to a deag/
2212 # second lookup in each of these tables
2213 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002214 route_to_dst = VppIpRoute(
2215 self, "1.1.1.1", 32, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=1)]
2216 )
Neale Ranns097fa662018-05-01 05:17:55 -07002217 route_to_src = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002218 self,
2219 "1.1.1.2",
2220 32,
2221 [
2222 VppRoutePath(
2223 "0.0.0.0",
2224 0xFFFFFFFF,
2225 nh_table_id=2,
2226 type=FibPathType.FIB_PATH_TYPE_SOURCE_LOOKUP,
2227 )
2228 ],
2229 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002230 route_to_dst.add_vpp_config()
2231 route_to_src.add_vpp_config()
2232
2233 #
2234 # packets to these destination are dropped, since they'll
2235 # hit the respective default routes in the second table
2236 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002237 p_dst = (
2238 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2239 / IP(src="5.5.5.5", dst="1.1.1.1")
2240 / TCP(sport=1234, dport=1234)
2241 / Raw(b"\xa5" * 100)
2242 )
2243 p_src = (
2244 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2245 / IP(src="2.2.2.2", dst="1.1.1.2")
2246 / TCP(sport=1234, dport=1234)
2247 / Raw(b"\xa5" * 100)
2248 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002249 pkts_dst = p_dst * 257
2250 pkts_src = p_src * 257
2251
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002252 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in dst table")
2253 self.send_and_assert_no_replies(self.pg0, pkts_src, "IP in src table")
Neale Ranns054c03a2017-10-13 05:15:07 -07002254
2255 #
2256 # add a route in the dst table to forward via pg1
2257 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002258 route_in_dst = VppIpRoute(
2259 self,
2260 "1.1.1.1",
2261 32,
2262 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
2263 table_id=1,
2264 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002265 route_in_dst.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -07002266
Neale Ranns054c03a2017-10-13 05:15:07 -07002267 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2268
2269 #
2270 # add a route in the src table to forward via pg2
2271 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002272 route_in_src = VppIpRoute(
2273 self,
2274 "2.2.2.2",
2275 32,
2276 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
2277 table_id=2,
2278 )
Neale Ranns054c03a2017-10-13 05:15:07 -07002279 route_in_src.add_vpp_config()
2280 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2281
Neale Rannsce9e0b42018-08-01 12:53:17 -07002282 #
2283 # loop in the lookup DP
2284 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002285 route_loop = VppIpRoute(
2286 self, "2.2.2.3", 32, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)]
2287 )
Neale Rannsce9e0b42018-08-01 12:53:17 -07002288 route_loop.add_vpp_config()
2289
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002290 p_l = (
2291 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2292 / IP(src="2.2.2.4", dst="2.2.2.3")
2293 / TCP(sport=1234, dport=1234)
2294 / Raw(b"\xa5" * 100)
2295 )
Neale Rannsce9e0b42018-08-01 12:53:17 -07002296
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002297 self.send_and_assert_no_replies(self.pg0, p_l * 257, "IP lookup loop")
Neale Rannsce9e0b42018-08-01 12:53:17 -07002298
Neale Ranns054c03a2017-10-13 05:15:07 -07002299
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002300class TestIPInput(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002301 """IPv4 Input Exceptions"""
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002302
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002303 @classmethod
2304 def setUpClass(cls):
2305 super(TestIPInput, cls).setUpClass()
2306
2307 @classmethod
2308 def tearDownClass(cls):
2309 super(TestIPInput, cls).tearDownClass()
2310
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002311 def setUp(self):
2312 super(TestIPInput, self).setUp()
2313
2314 self.create_pg_interfaces(range(2))
2315
2316 for i in self.pg_interfaces:
2317 i.admin_up()
2318 i.config_ip4()
2319 i.resolve_arp()
2320
2321 def tearDown(self):
2322 super(TestIPInput, self).tearDown()
2323 for i in self.pg_interfaces:
2324 i.unconfig_ip4()
2325 i.admin_down()
2326
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002327 def test_ip_input(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002328 """IP Input Exceptions"""
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002329
2330 # i can't find a way in scapy to construct an IP packet
2331 # with a length less than the IP header length
2332
2333 #
2334 # Packet too short - this is forwarded
2335 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002336 p_short = (
2337 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2338 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, len=40)
2339 / UDP(sport=1234, dport=1234)
2340 / Raw(b"\xa5" * 100)
2341 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002342
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002343 rx = self.send_and_expect(self.pg0, p_short * NUM_PKTS, self.pg1)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002344
2345 #
2346 # Packet too long - this is dropped
2347 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002348 p_long = (
2349 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2350 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, len=400)
2351 / UDP(sport=1234, dport=1234)
2352 / Raw(b"\xa5" * 100)
2353 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002354
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002355 rx = self.send_and_assert_no_replies(self.pg0, p_long * NUM_PKTS, "too long")
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002356
2357 #
2358 # bad chksum - this is dropped
2359 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002360 p_chksum = (
2361 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2362 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, chksum=400)
2363 / UDP(sport=1234, dport=1234)
2364 / Raw(b"\xa5" * 100)
2365 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002366
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002367 rx = self.send_and_assert_no_replies(
2368 self.pg0, p_chksum * NUM_PKTS, "bad checksum"
2369 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002370
2371 #
2372 # bad version - this is dropped
2373 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002374 p_ver = (
2375 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2376 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, version=3)
2377 / UDP(sport=1234, dport=1234)
2378 / Raw(b"\xa5" * 100)
2379 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002380
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002381 rx = self.send_and_assert_no_replies(
2382 self.pg0, p_ver * NUM_PKTS, "funky version"
2383 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002384
2385 #
2386 # fragment offset 1 - this is dropped
2387 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002388 p_frag = (
2389 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2390 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, frag=1)
2391 / UDP(sport=1234, dport=1234)
2392 / Raw(b"\xa5" * 100)
2393 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002394
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002395 rx = self.send_and_assert_no_replies(self.pg0, p_frag * NUM_PKTS, "frag offset")
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002396
2397 #
2398 # TTL expired packet
2399 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002400 p_ttl = (
2401 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2402 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=1)
2403 / UDP(sport=1234, dport=1234)
2404 / Raw(b"\xa5" * 100)
2405 )
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002406
Neale Ranns5c6dd172022-02-17 09:08:47 +00002407 rxs = self.send_and_expect_some(self.pg0, p_ttl * NUM_PKTS, self.pg0)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002408
Neale Ranns5c6dd172022-02-17 09:08:47 +00002409 for rx in rxs:
2410 icmp = rx[ICMP]
2411 self.assertEqual(icmptypes[icmp.type], "time-exceeded")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002412 self.assertEqual(icmpcodes[icmp.type][icmp.code], "ttl-zero-during-transit")
Neale Ranns5c6dd172022-02-17 09:08:47 +00002413 self.assertEqual(icmp.src, self.pg0.remote_ip4)
2414 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002415
Neale Rannsffd78d12018-02-09 06:05:16 -08002416 #
2417 # MTU exceeded
2418 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002419 p_mtu = (
2420 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2421 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, ttl=10, flags="DF")
2422 / UDP(sport=1234, dport=1234)
2423 / Raw(b"\xa5" * 2000)
2424 )
Neale Rannsffd78d12018-02-09 06:05:16 -08002425
Ole Troand7231612018-06-07 10:17:57 +02002426 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1500, 0, 0, 0])
Neale Rannsffd78d12018-02-09 06:05:16 -08002427
Neale Ranns5c6dd172022-02-17 09:08:47 +00002428 rxs = self.send_and_expect_some(self.pg0, p_mtu * NUM_PKTS, self.pg0)
Neale Rannsffd78d12018-02-09 06:05:16 -08002429
Neale Ranns5c6dd172022-02-17 09:08:47 +00002430 for rx in rxs:
2431 icmp = rx[ICMP]
2432 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002433 self.assertEqual(icmpcodes[icmp.type][icmp.code], "fragmentation-needed")
Neale Rannsfbc633f2022-03-18 13:05:09 +00002434 self.assertEqual(icmp.nexthopmtu, 1500)
Neale Ranns5c6dd172022-02-17 09:08:47 +00002435 self.assertEqual(icmp.src, self.pg0.remote_ip4)
2436 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
Neale Rannsffd78d12018-02-09 06:05:16 -08002437
Ole Troand7231612018-06-07 10:17:57 +02002438 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [2500, 0, 0, 0])
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002439 rx = self.send_and_expect(self.pg0, p_mtu * NUM_PKTS, self.pg1)
Neale Rannsffd78d12018-02-09 06:05:16 -08002440
Ole Troand7231612018-06-07 10:17:57 +02002441 # Reset MTU for subsequent tests
2442 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [9000, 0, 0, 0])
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002443
Neale Rannsbe2286b2018-12-09 12:54:51 -08002444 #
2445 # source address 0.0.0.0 and 25.255.255.255 and for-us
2446 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002447 p_s0 = (
2448 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2449 / IP(src="0.0.0.0", dst=self.pg0.local_ip4)
2450 / ICMP(id=4, seq=4)
2451 / Raw(load=b"\x0a" * 18)
2452 )
Neale Rannsbe2286b2018-12-09 12:54:51 -08002453 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
2454
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002455 p_s0 = (
2456 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2457 / IP(src="255.255.255.255", dst=self.pg0.local_ip4)
2458 / ICMP(id=4, seq=4)
2459 / Raw(load=b"\x0a" * 18)
2460 )
Neale Rannsbe2286b2018-12-09 12:54:51 -08002461 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
2462
Neale Ranns1855b8e2018-07-11 10:31:26 -07002463
2464class TestIPDirectedBroadcast(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002465 """IPv4 Directed Broadcast"""
Neale Ranns1855b8e2018-07-11 10:31:26 -07002466
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002467 @classmethod
2468 def setUpClass(cls):
2469 super(TestIPDirectedBroadcast, cls).setUpClass()
2470
2471 @classmethod
2472 def tearDownClass(cls):
2473 super(TestIPDirectedBroadcast, cls).tearDownClass()
2474
Neale Ranns1855b8e2018-07-11 10:31:26 -07002475 def setUp(self):
2476 super(TestIPDirectedBroadcast, self).setUp()
2477
2478 self.create_pg_interfaces(range(2))
2479
2480 for i in self.pg_interfaces:
2481 i.admin_up()
2482
2483 def tearDown(self):
2484 super(TestIPDirectedBroadcast, self).tearDown()
2485 for i in self.pg_interfaces:
2486 i.admin_down()
2487
2488 def test_ip_input(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002489 """IP Directed Broadcast"""
Neale Ranns1855b8e2018-07-11 10:31:26 -07002490
2491 #
2492 # set the directed broadcast on pg0 first, then config IP4 addresses
2493 # for pg1 directed broadcast is always disabled
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002494 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 1)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002495
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002496 p0 = (
2497 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
2498 / IP(src="1.1.1.1", dst=self.pg0._local_ip4_bcast)
2499 / UDP(sport=1234, dport=1234)
2500 / Raw(b"\xa5" * 2000)
2501 )
2502 p1 = (
2503 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2504 / IP(src="1.1.1.1", dst=self.pg1._local_ip4_bcast)
2505 / UDP(sport=1234, dport=1234)
2506 / Raw(b"\xa5" * 2000)
2507 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002508
2509 self.pg0.config_ip4()
2510 self.pg0.resolve_arp()
2511 self.pg1.config_ip4()
2512 self.pg1.resolve_arp()
2513
2514 #
2515 # test packet is L2 broadcast
2516 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002517 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002518 self.assertTrue(rx[0][Ether].dst, "ff:ff:ff:ff:ff:ff")
2519
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002520 self.send_and_assert_no_replies(
2521 self.pg0, p1 * NUM_PKTS, "directed broadcast disabled"
2522 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002523
2524 #
2525 # toggle directed broadcast on pg0
2526 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002527 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 0)
2528 self.send_and_assert_no_replies(
2529 self.pg1, p0 * NUM_PKTS, "directed broadcast disabled"
2530 )
Neale Ranns1855b8e2018-07-11 10:31:26 -07002531
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002532 self.vapi.sw_interface_set_ip_directed_broadcast(self.pg0.sw_if_index, 1)
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002533 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002534
2535 self.pg0.unconfig_ip4()
2536 self.pg1.unconfig_ip4()
2537
2538
mu.duojiao59a82952018-10-11 14:27:30 +08002539class TestIPLPM(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002540 """IPv4 longest Prefix Match"""
mu.duojiao59a82952018-10-11 14:27:30 +08002541
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002542 @classmethod
2543 def setUpClass(cls):
2544 super(TestIPLPM, cls).setUpClass()
2545
2546 @classmethod
2547 def tearDownClass(cls):
2548 super(TestIPLPM, cls).tearDownClass()
2549
mu.duojiao59a82952018-10-11 14:27:30 +08002550 def setUp(self):
2551 super(TestIPLPM, self).setUp()
2552
2553 self.create_pg_interfaces(range(4))
2554
2555 for i in self.pg_interfaces:
2556 i.admin_up()
2557 i.config_ip4()
2558 i.resolve_arp()
2559
2560 def tearDown(self):
2561 super(TestIPLPM, self).tearDown()
2562 for i in self.pg_interfaces:
2563 i.admin_down()
2564 i.unconfig_ip4()
2565
2566 def test_ip_lpm(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002567 """IP longest Prefix Match"""
mu.duojiao59a82952018-10-11 14:27:30 +08002568
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002569 s_24 = VppIpRoute(
2570 self,
2571 "10.1.2.0",
2572 24,
2573 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
2574 )
mu.duojiao59a82952018-10-11 14:27:30 +08002575 s_24.add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002576 s_8 = VppIpRoute(
2577 self,
2578 "10.0.0.0",
2579 8,
2580 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
2581 )
mu.duojiao59a82952018-10-11 14:27:30 +08002582 s_8.add_vpp_config()
2583
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002584 p_8 = (
2585 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2586 / IP(src="1.1.1.1", dst="10.1.1.1")
2587 / UDP(sport=1234, dport=1234)
2588 / Raw(b"\xa5" * 2000)
2589 )
2590 p_24 = (
2591 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
2592 / IP(src="1.1.1.1", dst="10.1.2.1")
2593 / UDP(sport=1234, dport=1234)
2594 / Raw(b"\xa5" * 2000)
2595 )
mu.duojiao59a82952018-10-11 14:27:30 +08002596
2597 self.logger.info(self.vapi.cli("sh ip fib mtrie"))
Paul Vinciguerra4271c972019-05-14 13:25:49 -04002598 rx = self.send_and_expect(self.pg0, p_8 * NUM_PKTS, self.pg2)
2599 rx = self.send_and_expect(self.pg0, p_24 * NUM_PKTS, self.pg1)
mu.duojiao59a82952018-10-11 14:27:30 +08002600
2601
Andrew Yourtchenko8dc0d482021-01-29 13:17:19 +00002602@tag_fixme_vpp_workers
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002603class TestIPv4Frag(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002604 """IPv4 fragmentation"""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002605
2606 @classmethod
2607 def setUpClass(cls):
2608 super(TestIPv4Frag, cls).setUpClass()
2609
2610 cls.create_pg_interfaces([0, 1])
2611 cls.src_if = cls.pg0
2612 cls.dst_if = cls.pg1
2613
2614 # setup all interfaces
2615 for i in cls.pg_interfaces:
2616 i.admin_up()
2617 i.config_ip4()
2618 i.resolve_arp()
2619
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002620 @classmethod
2621 def tearDownClass(cls):
2622 super(TestIPv4Frag, cls).tearDownClass()
2623
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002624 def test_frag_large_packets(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002625 """Fragmentation of large packets"""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002626
Neale Ranns0b6a8572019-10-30 17:34:14 +00002627 self.vapi.cli("adjacency counters enable")
2628
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002629 p = (
2630 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2631 / IP(src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
2632 / UDP(sport=1234, dport=5678)
2633 / Raw()
2634 )
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002635 self.extend_packet(p, 6000, "abcde")
2636 saved_payload = p[Raw].load
2637
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002638 nbr = VppNeighbor(
2639 self,
2640 self.dst_if.sw_if_index,
2641 self.dst_if.remote_mac,
2642 self.dst_if.remote_ip4,
2643 ).add_vpp_config()
Neale Ranns0b6a8572019-10-30 17:34:14 +00002644
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002645 # Force fragmentation by setting MTU of output interface
2646 # lower than packet size
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002647 self.vapi.sw_interface_set_mtu(self.dst_if.sw_if_index, [5000, 0, 0, 0])
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002648
2649 self.pg_enable_capture()
2650 self.src_if.add_stream(p)
2651 self.pg_start()
2652
2653 # Expecting 3 fragments because size of created fragments currently
2654 # cannot be larger then VPP buffer size (which is 2048)
2655 packets = self.dst_if.get_capture(3)
2656
Neale Ranns0b6a8572019-10-30 17:34:14 +00002657 # we should show 3 packets thru the neighbor
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002658 self.assertEqual(3, nbr.get_stats()["packets"])
Neale Ranns0b6a8572019-10-30 17:34:14 +00002659
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002660 # Assume VPP sends the fragments in order
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002661 payload = b""
Juraj Sloboda68b7cb82018-10-16 12:18:21 +02002662 for p in packets:
2663 payload_offset = p.frag * 8
2664 if payload_offset > 0:
2665 payload_offset -= 8 # UDP header is not in payload
2666 self.assert_equal(payload_offset, len(payload))
2667 payload += p[Raw].load
2668 self.assert_equal(payload, saved_payload, "payload")
2669
2670
Neale Ranns9db6ada2019-11-08 12:42:31 +00002671class TestIPReplace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002672 """IPv4 Table Replace"""
Neale Ranns9db6ada2019-11-08 12:42:31 +00002673
2674 @classmethod
2675 def setUpClass(cls):
2676 super(TestIPReplace, cls).setUpClass()
2677
2678 @classmethod
2679 def tearDownClass(cls):
2680 super(TestIPReplace, cls).tearDownClass()
2681
2682 def setUp(self):
2683 super(TestIPReplace, self).setUp()
2684
2685 self.create_pg_interfaces(range(4))
2686
2687 table_id = 1
2688 self.tables = []
2689
2690 for i in self.pg_interfaces:
2691 i.admin_up()
2692 i.config_ip4()
2693 i.resolve_arp()
2694 i.generate_remote_hosts(2)
2695 self.tables.append(VppIpTable(self, table_id).add_vpp_config())
2696 table_id += 1
2697
2698 def tearDown(self):
2699 super(TestIPReplace, self).tearDown()
2700 for i in self.pg_interfaces:
2701 i.admin_down()
2702 i.unconfig_ip4()
2703
2704 def test_replace(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002705 """IP Table Replace"""
Neale Ranns9db6ada2019-11-08 12:42:31 +00002706
Neale Ranns990f6942020-10-20 07:20:17 +00002707 MRouteItfFlags = VppEnum.vl_api_mfib_itf_flags_t
2708 MRouteEntryFlags = VppEnum.vl_api_mfib_entry_flags_t
Neale Ranns9db6ada2019-11-08 12:42:31 +00002709 N_ROUTES = 20
2710 links = [self.pg0, self.pg1, self.pg2, self.pg3]
2711 routes = [[], [], [], []]
2712
2713 # load up the tables with some routes
2714 for ii, t in enumerate(self.tables):
2715 for jj in range(N_ROUTES):
2716 uni = VppIpRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002717 self,
2718 "10.0.0.%d" % jj,
2719 32,
2720 [
2721 VppRoutePath(
2722 links[ii].remote_hosts[0].ip4, links[ii].sw_if_index
2723 ),
2724 VppRoutePath(
2725 links[ii].remote_hosts[1].ip4, links[ii].sw_if_index
2726 ),
2727 ],
2728 table_id=t.table_id,
2729 ).add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002730 multi = VppIpMRoute(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002731 self,
2732 "0.0.0.0",
2733 "239.0.0.%d" % jj,
2734 32,
Neale Ranns990f6942020-10-20 07:20:17 +00002735 MRouteEntryFlags.MFIB_API_ENTRY_FLAG_NONE,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002736 [
2737 VppMRoutePath(
2738 self.pg0.sw_if_index,
2739 MRouteItfFlags.MFIB_API_ITF_FLAG_ACCEPT,
2740 ),
2741 VppMRoutePath(
2742 self.pg1.sw_if_index,
2743 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2744 ),
2745 VppMRoutePath(
2746 self.pg2.sw_if_index,
2747 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2748 ),
2749 VppMRoutePath(
2750 self.pg3.sw_if_index,
2751 MRouteItfFlags.MFIB_API_ITF_FLAG_FORWARD,
2752 ),
2753 ],
2754 table_id=t.table_id,
2755 ).add_vpp_config()
2756 routes[ii].append({"uni": uni, "multi": multi})
Neale Ranns9db6ada2019-11-08 12:42:31 +00002757
2758 #
2759 # replace the tables a few times
2760 #
2761 for kk in range(3):
2762 # replace_begin each table
2763 for t in self.tables:
2764 t.replace_begin()
2765
2766 # all the routes are still there
2767 for ii, t in enumerate(self.tables):
2768 dump = t.dump()
2769 mdump = t.mdump()
2770 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002771 self.assertTrue(find_route_in_dump(dump, r["uni"], t))
2772 self.assertTrue(find_mroute_in_dump(mdump, r["multi"], t))
Neale Ranns9db6ada2019-11-08 12:42:31 +00002773
2774 # redownload the even numbered routes
2775 for ii, t in enumerate(self.tables):
2776 for jj in range(0, N_ROUTES, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002777 routes[ii][jj]["uni"].add_vpp_config()
2778 routes[ii][jj]["multi"].add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002779
2780 # signal each table replace_end
2781 for t in self.tables:
2782 t.replace_end()
2783
2784 # we should find the even routes, but not the odd
2785 for ii, t in enumerate(self.tables):
2786 dump = t.dump()
2787 mdump = t.mdump()
2788 for jj in range(0, N_ROUTES, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002789 self.assertTrue(find_route_in_dump(dump, routes[ii][jj]["uni"], t))
2790 self.assertTrue(
2791 find_mroute_in_dump(mdump, routes[ii][jj]["multi"], t)
2792 )
Neale Ranns9db6ada2019-11-08 12:42:31 +00002793 for jj in range(1, N_ROUTES - 1, 2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002794 self.assertFalse(find_route_in_dump(dump, routes[ii][jj]["uni"], t))
2795 self.assertFalse(
2796 find_mroute_in_dump(mdump, routes[ii][jj]["multi"], t)
2797 )
Neale Ranns9db6ada2019-11-08 12:42:31 +00002798
2799 # reload all the routes
2800 for ii, t in enumerate(self.tables):
2801 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002802 r["uni"].add_vpp_config()
2803 r["multi"].add_vpp_config()
Neale Ranns9db6ada2019-11-08 12:42:31 +00002804
2805 # all the routes are still there
2806 for ii, t in enumerate(self.tables):
2807 dump = t.dump()
2808 mdump = t.mdump()
2809 for r in routes[ii]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002810 self.assertTrue(find_route_in_dump(dump, r["uni"], t))
2811 self.assertTrue(find_mroute_in_dump(mdump, r["multi"], t))
Neale Ranns9db6ada2019-11-08 12:42:31 +00002812
2813 #
2814 # finally flush the tables for good measure
2815 #
2816 for t in self.tables:
2817 t.flush()
2818 self.assertEqual(len(t.dump()), 5)
Neale Ranns03c254e2020-03-17 14:25:10 +00002819 self.assertEqual(len(t.mdump()), 3)
Neale Ranns9db6ada2019-11-08 12:42:31 +00002820
2821
Neale Ranns9efcee62019-11-26 19:30:08 +00002822class TestIPCover(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002823 """IPv4 Table Cover"""
Neale Ranns9efcee62019-11-26 19:30:08 +00002824
2825 @classmethod
2826 def setUpClass(cls):
2827 super(TestIPCover, cls).setUpClass()
2828
2829 @classmethod
2830 def tearDownClass(cls):
2831 super(TestIPCover, cls).tearDownClass()
2832
2833 def setUp(self):
2834 super(TestIPCover, self).setUp()
2835
2836 self.create_pg_interfaces(range(4))
2837
2838 table_id = 1
2839 self.tables = []
2840
2841 for i in self.pg_interfaces:
2842 i.admin_up()
2843 i.config_ip4()
2844 i.resolve_arp()
2845 i.generate_remote_hosts(2)
2846 self.tables.append(VppIpTable(self, table_id).add_vpp_config())
2847 table_id += 1
2848
2849 def tearDown(self):
2850 super(TestIPCover, self).tearDown()
2851 for i in self.pg_interfaces:
2852 i.admin_down()
2853 i.unconfig_ip4()
2854
2855 def test_cover(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002856 """IP Table Cover"""
Neale Ranns9efcee62019-11-26 19:30:08 +00002857
2858 # add a loop back with a /32 prefix
2859 lo = VppLoInterface(self)
2860 lo.admin_up()
2861 a = VppIpInterfaceAddress(self, lo, "127.0.0.1", 32).add_vpp_config()
2862
2863 # add a neighbour that matches the loopback's /32
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002864 nbr = VppNeighbor(
2865 self, lo.sw_if_index, lo.remote_mac, "127.0.0.1"
2866 ).add_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002867
2868 # add the default route which will be the cover for /32
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002869 r = VppIpRoute(
2870 self,
2871 "0.0.0.0",
2872 0,
2873 [VppRoutePath("127.0.0.1", lo.sw_if_index)],
2874 register=False,
2875 ).add_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002876
2877 # add/remove/add a longer mask cover
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002878 r8 = VppIpRoute(
2879 self, "127.0.0.0", 8, [VppRoutePath("127.0.0.1", lo.sw_if_index)]
2880 ).add_vpp_config()
Neale Ranns87866032020-11-25 09:14:22 +00002881 r8.remove_vpp_config()
2882 r8.add_vpp_config()
2883 r8.remove_vpp_config()
Neale Ranns9efcee62019-11-26 19:30:08 +00002884
2885 # remove the default route
2886 r.remove_vpp_config()
2887
Neale Ranns87866032020-11-25 09:14:22 +00002888 # remove the interface prefix
2889 a.remove_vpp_config()
2890
Neale Ranns59f71132020-04-08 12:19:38 +00002891
2892class TestIP4Replace(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002893 """IPv4 Interface Address Replace"""
Neale Ranns59f71132020-04-08 12:19:38 +00002894
2895 @classmethod
2896 def setUpClass(cls):
2897 super(TestIP4Replace, cls).setUpClass()
2898
2899 @classmethod
2900 def tearDownClass(cls):
2901 super(TestIP4Replace, cls).tearDownClass()
2902
2903 def setUp(self):
2904 super(TestIP4Replace, self).setUp()
2905
2906 self.create_pg_interfaces(range(4))
2907
2908 for i in self.pg_interfaces:
2909 i.admin_up()
2910
2911 def tearDown(self):
2912 super(TestIP4Replace, self).tearDown()
2913 for i in self.pg_interfaces:
2914 i.admin_down()
2915
2916 def get_n_pfxs(self, intf):
2917 return len(self.vapi.ip_address_dump(intf.sw_if_index))
2918
2919 def test_replace(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02002920 """IP interface address replace"""
Neale Ranns59f71132020-04-08 12:19:38 +00002921
2922 intf_pfxs = [[], [], [], []]
2923
2924 # add prefixes to each of the interfaces
2925 for i in range(len(self.pg_interfaces)):
2926 intf = self.pg_interfaces[i]
2927
2928 # 172.16.x.1/24
2929 addr = "172.16.%d.1" % intf.sw_if_index
2930 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2931 intf_pfxs[i].append(a)
2932
2933 # 172.16.x.2/24 - a different address in the same subnet as above
2934 addr = "172.16.%d.2" % intf.sw_if_index
2935 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2936 intf_pfxs[i].append(a)
2937
2938 # 172.15.x.2/24 - a different address and subnet
2939 addr = "172.15.%d.2" % intf.sw_if_index
2940 a = VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
2941 intf_pfxs[i].append(a)
2942
2943 # a dump should n_address in it
2944 for intf in self.pg_interfaces:
2945 self.assertEqual(self.get_n_pfxs(intf), 3)
2946
2947 #
2948 # remove all the address thru a replace
2949 #
2950 self.vapi.sw_interface_address_replace_begin()
2951 self.vapi.sw_interface_address_replace_end()
2952 for intf in self.pg_interfaces:
2953 self.assertEqual(self.get_n_pfxs(intf), 0)
2954
2955 #
2956 # add all the interface addresses back
2957 #
2958 for p in intf_pfxs:
2959 for v in p:
2960 v.add_vpp_config()
2961 for intf in self.pg_interfaces:
2962 self.assertEqual(self.get_n_pfxs(intf), 3)
2963
2964 #
2965 # replace again, but this time update/re-add the address on the first
2966 # two interfaces
2967 #
2968 self.vapi.sw_interface_address_replace_begin()
2969
2970 for p in intf_pfxs[:2]:
2971 for v in p:
2972 v.add_vpp_config()
2973
2974 self.vapi.sw_interface_address_replace_end()
2975
2976 # on the first two the address still exist,
2977 # on the other two they do not
2978 for intf in self.pg_interfaces[:2]:
2979 self.assertEqual(self.get_n_pfxs(intf), 3)
2980 for p in intf_pfxs[:2]:
2981 for v in p:
2982 self.assertTrue(v.query_vpp_config())
2983 for intf in self.pg_interfaces[2:]:
2984 self.assertEqual(self.get_n_pfxs(intf), 0)
2985
2986 #
2987 # add all the interface addresses back on the last two
2988 #
2989 for p in intf_pfxs[2:]:
2990 for v in p:
2991 v.add_vpp_config()
2992 for intf in self.pg_interfaces:
2993 self.assertEqual(self.get_n_pfxs(intf), 3)
2994
2995 #
2996 # replace again, this time add different prefixes on all the interfaces
2997 #
2998 self.vapi.sw_interface_address_replace_begin()
2999
3000 pfxs = []
3001 for intf in self.pg_interfaces:
3002 # 172.18.x.1/24
3003 addr = "172.18.%d.1" % intf.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003004 pfxs.append(VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config())
Neale Ranns59f71132020-04-08 12:19:38 +00003005
3006 self.vapi.sw_interface_address_replace_end()
3007
3008 # only .18 should exist on each interface
3009 for intf in self.pg_interfaces:
3010 self.assertEqual(self.get_n_pfxs(intf), 1)
3011 for pfx in pfxs:
3012 self.assertTrue(pfx.query_vpp_config())
3013
3014 #
3015 # remove everything
3016 #
3017 self.vapi.sw_interface_address_replace_begin()
3018 self.vapi.sw_interface_address_replace_end()
3019 for intf in self.pg_interfaces:
3020 self.assertEqual(self.get_n_pfxs(intf), 0)
3021
3022 #
3023 # add prefixes to each interface. post-begin add the prefix from
3024 # interface X onto interface Y. this would normally be an error
3025 # since it would generate a 'duplicate address' warning. but in
3026 # this case, since what is newly downloaded is sane, it's ok
3027 #
3028 for intf in self.pg_interfaces:
3029 # 172.18.x.1/24
3030 addr = "172.18.%d.1" % intf.sw_if_index
3031 VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config()
3032
3033 self.vapi.sw_interface_address_replace_begin()
3034
3035 pfxs = []
3036 for intf in self.pg_interfaces:
3037 # 172.18.x.1/24
3038 addr = "172.18.%d.1" % (intf.sw_if_index + 1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003039 pfxs.append(VppIpInterfaceAddress(self, intf, addr, 24).add_vpp_config())
Neale Ranns59f71132020-04-08 12:19:38 +00003040
3041 self.vapi.sw_interface_address_replace_end()
3042
3043 self.logger.info(self.vapi.cli("sh int addr"))
3044
3045 for intf in self.pg_interfaces:
3046 self.assertEqual(self.get_n_pfxs(intf), 1)
3047 for pfx in pfxs:
3048 self.assertTrue(pfx.query_vpp_config())
3049
3050
Neale Ranns8f5fef22020-12-21 08:29:34 +00003051class TestIPv4PathMTU(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003052 """IPv4 Path MTU"""
Neale Ranns8f5fef22020-12-21 08:29:34 +00003053
3054 @classmethod
3055 def setUpClass(cls):
3056 super(TestIPv4PathMTU, cls).setUpClass()
3057
3058 cls.create_pg_interfaces(range(2))
3059
3060 # setup all interfaces
3061 for i in cls.pg_interfaces:
3062 i.admin_up()
3063 i.config_ip4()
3064 i.resolve_arp()
3065
3066 @classmethod
3067 def tearDownClass(cls):
3068 super(TestIPv4PathMTU, cls).tearDownClass()
3069
3070 def test_path_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003071 """Path MTU"""
Neale Ranns8f5fef22020-12-21 08:29:34 +00003072
3073 #
3074 # The goal here is not to test that fragmentation works correctly,
3075 # that's done elsewhere, the intent is to ensure that the Path MTU
3076 # settings are honoured.
3077 #
3078 self.vapi.cli("adjacency counters enable")
3079
3080 # set the interface MTU to a reasonable value
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003081 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1800, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003082
3083 self.pg1.generate_remote_hosts(4)
3084
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003085 p_2k = (
3086 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3087 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3088 / UDP(sport=1234, dport=5678)
3089 / Raw(b"0xa" * 640)
3090 )
3091 p_1k = (
3092 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3093 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
3094 / UDP(sport=1234, dport=5678)
3095 / Raw(b"0xa" * 320)
3096 )
Neale Ranns8f5fef22020-12-21 08:29:34 +00003097
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003098 nbr = VppNeighbor(
3099 self, self.pg1.sw_if_index, self.pg1.remote_mac, self.pg1.remote_ip4
3100 ).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003101
3102 # this is now the interface MTU frags
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 # drop the path MTU for this neighbour to below the interface MTU
3107 # expect more frags
3108 pmtu = VppIpPathMtu(self, self.pg1.remote_ip4, 900).add_vpp_config()
3109
3110 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3111 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3112
3113 # print/format the adj delegate
3114 self.logger.info(self.vapi.cli("sh adj 5"))
3115
3116 # increase the path MTU to more than the interface
3117 # expect to use the interface MTU
3118 pmtu.modify(8192)
3119
3120 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=2)
3121 self.send_and_expect(self.pg0, [p_1k], self.pg1)
3122
3123 # go back to an MTU from the path
3124 # wrap the call around mark-n-sweep to enusre updates clear stale
3125 self.vapi.ip_path_mtu_replace_begin()
3126 pmtu.modify(900)
3127 self.vapi.ip_path_mtu_replace_end()
3128
3129 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3130 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3131
3132 # raise the interface's MTU
3133 # should still use that of the path
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003134 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [2000, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003135 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3136 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3137
3138 # set path high and interface low
3139 pmtu.modify(2000)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003140 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [900, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003141 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3142 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3143
3144 # remove the path MTU using the mark-n-sweep semantics
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003145 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1800, 0, 0, 0])
Neale Ranns8f5fef22020-12-21 08:29:34 +00003146 self.vapi.ip_path_mtu_replace_begin()
3147 self.vapi.ip_path_mtu_replace_end()
3148
3149 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=2)
3150 self.send_and_expect(self.pg0, [p_1k], self.pg1)
3151
3152 #
3153 # set path MTU for a neighbour that doesn't exist, yet
3154 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003155 pmtu2 = VppIpPathMtu(self, self.pg1.remote_hosts[2].ip4, 900).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003156
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003157 p_2k = (
3158 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3159 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_hosts[2].ip4)
3160 / UDP(sport=1234, dport=5678)
3161 / Raw(b"0xa" * 640)
3162 )
3163 p_1k = (
3164 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3165 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_hosts[2].ip4)
3166 / UDP(sport=1234, dport=5678)
3167 / Raw(b"0xa" * 320)
3168 )
Neale Ranns8f5fef22020-12-21 08:29:34 +00003169
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003170 nbr2 = VppNeighbor(
3171 self,
3172 self.pg1.sw_if_index,
3173 self.pg1.remote_hosts[2].mac,
3174 self.pg1.remote_hosts[2].ip4,
3175 ).add_vpp_config()
Neale Ranns8f5fef22020-12-21 08:29:34 +00003176
3177 # should frag to the path MTU
3178 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3179 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3180
3181 # remove and re-add the neighbour
3182 nbr2.remove_vpp_config()
3183 nbr2.add_vpp_config()
3184
3185 # should frag to the path MTU
3186 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3187 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3188
3189 #
3190 # set PMTUs for many peers
3191 #
3192 N_HOSTS = 16
3193 self.pg1.generate_remote_hosts(16)
3194 self.pg1.configure_ipv4_neighbors()
3195
3196 for h in range(N_HOSTS):
3197 pmtu = VppIpPathMtu(self, self.pg1.remote_hosts[h].ip4, 900)
3198 pmtu.add_vpp_config()
3199 self.assertTrue(pmtu.query_vpp_config())
3200
3201 self.logger.info(self.vapi.cli("sh ip pmtu"))
3202 dump = list(self.vapi.vpp.details_iter(self.vapi.ip_path_mtu_get))
3203 self.assertEqual(N_HOSTS, len(dump))
3204
3205 for h in range(N_HOSTS):
3206 p_2k[IP].dst = self.pg1.remote_hosts[h].ip4
3207 p_1k[IP].dst = self.pg1.remote_hosts[h].ip4
3208
3209 # should frag to the path MTU
3210 self.send_and_expect(self.pg0, [p_2k], self.pg1, n_rx=3)
3211 self.send_and_expect(self.pg0, [p_1k], self.pg1, n_rx=2)
3212
3213
Neale Ranns50bd1d32021-10-08 07:16:12 +00003214class TestIPv4ItfRebind(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003215 """IPv4 Interface Bind w/ attached routes"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003216
3217 def setUp(self):
3218 super(TestIPv4ItfRebind, self).setUp()
3219
3220 self.create_pg_interfaces(range(3))
3221
3222 def tearDown(self):
3223 super(TestIPv4ItfRebind, self).tearDown()
3224
3225 def test_rebind(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003226 """Import to no import"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003227
3228 TABLE_ID = 1
3229 tbl = VppIpTable(self, TABLE_ID).add_vpp_config()
3230 self.pg1.set_table_ip4(TABLE_ID)
3231
3232 for i in self.pg_interfaces:
3233 i.admin_up()
3234 i.config_ip4()
3235 i.resolve_arp()
3236
3237 # add an attached route via an pg0
3238 # in a different table. this prefix should import
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003239 rt = VppIpRoute(
3240 self,
3241 self.pg0.local_ip4,
3242 24,
3243 [VppRoutePath("0.0.0.0", self.pg0.sw_if_index)],
3244 table_id=TABLE_ID,
3245 ).add_vpp_config()
Neale Ranns50bd1d32021-10-08 07:16:12 +00003246
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003247 p = (
3248 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
3249 / IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4)
3250 / UDP(sport=1234, dport=5678)
3251 / Raw(b"0xa" * 640)
3252 )
Neale Ranns50bd1d32021-10-08 07:16:12 +00003253
3254 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3255 self.assertFalse(rx[0].haslayer(ARP))
3256
3257 # then bind pg0 to a new table
3258 # so the prefix no longer imports
3259 self.pg0.unconfig_ip4()
3260 self.pg0.set_table_ip4(TABLE_ID)
3261 self.pg0.config_ip4()
3262 self.pg0.resolve_arp()
3263
3264 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3265 self.assertFalse(rx[0].haslayer(ARP))
3266
3267 # revert back to imported
3268 self.pg0.unconfig_ip4()
3269 self.pg0.set_table_ip4(0)
3270 self.pg0.config_ip4()
3271 self.pg0.resolve_arp()
3272
3273 rx = self.send_and_expect(self.pg1, [p], self.pg0)
3274 self.assertFalse(rx[0].haslayer(ARP))
3275
3276 # cleanup
3277 for i in self.pg_interfaces:
3278 i.unconfig_ip4()
3279 i.set_table_ip4(0)
3280 i.admin_down()
3281
3282 rt.remove_vpp_config()
3283 tbl.remove_vpp_config()
3284
3285 def test_delete(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003286 """Swap import tables"""
Neale Ranns50bd1d32021-10-08 07:16:12 +00003287
3288 TABLE_ID1 = 1
3289 tbl1_4 = VppIpTable(self, TABLE_ID1).add_vpp_config()
3290 tbl1_6 = VppIpTable(self, TABLE_ID1, True).add_vpp_config()
3291 TABLE_ID2 = 2
3292 tbl2_4 = VppIpTable(self, TABLE_ID2).add_vpp_config()
3293 tbl2_6 = VppIpTable(self, TABLE_ID2, True).add_vpp_config()
3294
3295 # table mappings
3296 self.pg1.set_table_ip4(TABLE_ID1)
3297 self.pg1.set_table_ip6(TABLE_ID1)
3298 self.pg2.set_table_ip4(TABLE_ID2)
3299 self.pg2.set_table_ip6(TABLE_ID2)
3300
3301 for i in self.pg_interfaces:
3302 i.admin_up()
3303 i.config_ip4()
3304 i.resolve_arp()
3305
3306 # add an attached route in the default table via pg0
3307 # this should import to table 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003308 rt4 = VppIpRoute(
3309 self,
3310 self.pg1.local_ip4,
3311 24,
3312 [VppRoutePath("0.0.0.0", self.pg1.sw_if_index)],
3313 ).add_vpp_config()
3314 rt6 = VppIpRoute(
3315 self,
3316 self.pg1.local_ip6,
3317 64,
3318 [VppRoutePath("0.0.0.0", self.pg1.sw_if_index)],
3319 ).add_vpp_config()
Neale Ranns50bd1d32021-10-08 07:16:12 +00003320
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003321 p1 = (
3322 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
3323 / IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4)
3324 / UDP(sport=1234, dport=5678)
3325 / Raw(b"0xa" * 640)
3326 )
Neale Ranns50bd1d32021-10-08 07:16:12 +00003327
3328 # inject into table 0
3329 rx = self.send_and_expect(self.pg0, [p1], self.pg1)
3330 self.assertFalse(rx[0].haslayer(ARP))
3331
3332 # swap the attached interface to table 2
3333 self.pg1.unconfig_ip4()
3334 self.pg1.unconfig_ip6()
3335 self.pg1.set_table_ip4(TABLE_ID2)
3336 self.pg1.set_table_ip6(TABLE_ID2)
3337 self.pg1.config_ip4()
3338 self.pg1.config_ip6()
3339 self.pg1.resolve_arp()
3340
3341 # delete table 1
3342 tbl1_4.flush()
3343 tbl1_6.flush()
3344 tbl1_4.remove_vpp_config()
3345 tbl1_6.remove_vpp_config()
3346
3347 rx = self.send_and_expect(self.pg0, [p1], self.pg1)
3348 self.assertFalse(rx[0].haslayer(ARP))
3349
3350 for i in self.pg_interfaces:
3351 i.unconfig_ip4()
3352 i.unconfig_ip6()
3353 i.set_table_ip4(0)
3354 i.set_table_ip6(0)
3355 i.admin_down()
3356
3357
Vladislav Grishenkodea806d2024-02-20 11:58:01 +05003358class TestIP4InterfaceRx(VppTestCase):
3359 """IPv4 Interface Receive"""
3360
3361 @classmethod
3362 def setUpClass(cls):
3363 super(TestIP4InterfaceRx, cls).setUpClass()
3364
3365 @classmethod
3366 def tearDownClass(cls):
3367 super(TestIP4InterfaceRx, cls).tearDownClass()
3368
3369 def setUp(self):
3370 super(TestIP4InterfaceRx, self).setUp()
3371
3372 self.create_pg_interfaces(range(3))
3373
3374 table_id = 0
3375
3376 for i in self.pg_interfaces:
3377 i.admin_up()
3378
3379 if table_id != 0:
3380 table = VppIpTable(self, table_id)
3381 table.add_vpp_config()
3382
3383 i.set_table_ip4(table_id)
3384 i.config_ip4()
3385 i.resolve_arp()
3386 table_id += 1
3387
3388 def tearDown(self):
3389 for i in self.pg_interfaces:
3390 i.unconfig_ip4()
3391 i.admin_down()
3392 i.set_table_ip4(0)
3393
3394 super(TestIP4InterfaceRx, self).tearDown()
3395
3396 def test_interface_rx(self):
3397 """IPv4 Interface Receive"""
3398
3399 #
3400 # add a route in the default table to receive ...
3401 #
3402 route_to_dst = VppIpRoute(
3403 self,
3404 "1.1.1.0",
3405 24,
3406 [
3407 VppRoutePath(
3408 "0.0.0.0",
3409 self.pg1.sw_if_index,
3410 type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
3411 )
3412 ],
3413 )
3414 route_to_dst.add_vpp_config()
3415
3416 #
3417 # packets to these destination are dropped, since they'll
3418 # hit the respective default routes in table 1
3419 #
3420 p_dst = (
3421 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3422 / IP(src="5.5.5.5", dst="1.1.1.1")
3423 / TCP(sport=1234, dport=1234)
3424 / Raw(b"\xa5" * 100)
3425 )
3426 pkts_dst = p_dst * 10
3427
3428 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 1")
3429
3430 #
3431 # add a route in the dst table to forward via pg1
3432 #
3433 route_in_dst = VppIpRoute(
3434 self,
3435 "1.1.1.1",
3436 32,
3437 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
3438 table_id=1,
3439 )
3440 route_in_dst.add_vpp_config()
3441
3442 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
3443
3444 #
3445 # add a route in the default table to receive ...
3446 #
3447 route_to_dst = VppIpRoute(
3448 self,
3449 "1.1.1.0",
3450 24,
3451 [
3452 VppRoutePath(
3453 "0.0.0.0",
3454 self.pg2.sw_if_index,
3455 type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
3456 )
3457 ],
3458 table_id=1,
3459 )
3460 route_to_dst.add_vpp_config()
3461
3462 #
3463 # packets to these destination are dropped, since they'll
3464 # hit the respective default routes in table 2
3465 #
3466 p_dst = (
3467 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
3468 / IP(src="6.6.6.6", dst="1.1.1.2")
3469 / TCP(sport=1234, dport=1234)
3470 / Raw(b"\xa5" * 100)
3471 )
3472 pkts_dst = p_dst * 10
3473
3474 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 2")
3475
3476 #
3477 # add a route in the table 2 to forward via pg2
3478 #
3479 route_in_dst = VppIpRoute(
3480 self,
3481 "1.1.1.2",
3482 32,
3483 [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)],
3484 table_id=2,
3485 )
3486 route_in_dst.add_vpp_config()
3487
3488 self.send_and_expect(self.pg0, pkts_dst, self.pg2)
3489
3490
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02003491if __name__ == "__main__":
Klement Sekeraf62ae122016-10-11 11:47:09 +02003492 unittest.main(testRunner=VppTestRunner)