|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright(c) 2025 Arm Limited |
| 3 | + |
| 4 | +"""RSS testing suite. |
| 5 | +
|
| 6 | +Tests different hashing algorithms by checking if packets are routed to correct queues. |
| 7 | +Tests updating the RETA (Redirection Table) key to verify it takes effect and follows |
| 8 | +set size constraints. |
| 9 | +Tests RETA behavior under changing number of queues. |
| 10 | +""" |
| 11 | + |
| 12 | +import random |
| 13 | + |
| 14 | +from scapy.layers.inet import IP, UDP |
| 15 | +from scapy.layers.l2 import Ether |
| 16 | + |
| 17 | +from api.capabilities import ( |
| 18 | + LinkTopology, |
| 19 | + NicCapability, |
| 20 | + requires_link_topology, |
| 21 | + requires_nic_capability, |
| 22 | +) |
| 23 | +from api.packet import send_packets_and_capture |
| 24 | +from api.test import verify |
| 25 | +from api.testpmd import TestPmd |
| 26 | +from api.testpmd.config import SimpleForwardingModes |
| 27 | +from api.testpmd.types import ( |
| 28 | + FlowRule, |
| 29 | + RSSOffloadTypesFlag, |
| 30 | + TestPmdVerbosePacket, |
| 31 | +) |
| 32 | +from framework.exception import InteractiveCommandExecutionError |
| 33 | +from framework.test_suite import BaseConfig, TestSuite, func_test |
| 34 | +from framework.utils import StrEnum |
| 35 | + |
| 36 | + |
| 37 | +class Config(BaseConfig): |
| 38 | + """Default configuration for Per Test Suite config.""" |
| 39 | + |
| 40 | + NUM_QUEUES: int = 4 |
| 41 | + |
| 42 | + ACTUAL_KEY_SIZE: int = 52 |
| 43 | + |
| 44 | + ACTUAL_RETA_SIZE: int = 512 |
| 45 | + |
| 46 | + |
| 47 | +class HashAlgorithm(StrEnum): |
| 48 | + """Enum of hashing algorithms.""" |
| 49 | + |
| 50 | + DEFAULT = "default" |
| 51 | + SIMPLE_XOR = "simple_xor" |
| 52 | + TOEPLITZ = "toeplitz" |
| 53 | + SYMMETRIC_TOEPLITZ = "symmetric_toeplitz" |
| 54 | + |
| 55 | + |
| 56 | +@requires_link_topology(LinkTopology.ONE_LINK) |
| 57 | +@requires_nic_capability(NicCapability.PORT_RX_OFFLOAD_RSS_HASH) |
| 58 | +class TestPmdRss(TestSuite): |
| 59 | + """PMD RSS test suite.""" |
| 60 | + |
| 61 | + config: Config |
| 62 | + |
| 63 | + def _verify_hash_queue( |
| 64 | + self, |
| 65 | + reta: list[int], |
| 66 | + received_packets: list[TestPmdVerbosePacket], |
| 67 | + verify_packet_pairs: bool, |
| 68 | + ) -> None: |
| 69 | + """Verifies the packet hash corresponds to the packet queue. |
| 70 | +
|
| 71 | + Given the received packets in the verbose output, iterate through each packet. |
| 72 | + Use the hash to index into RETA and get its intended queue. |
| 73 | + Verify the intended queue is the same as the actual queue the packet was received in. |
| 74 | + If the hash algorithm is symmetric, verify that pairs of packets have the same hash, |
| 75 | + as the pairs of packets sent have "mirrored" L4 ports. |
| 76 | + e.g. received_packets[0, 1, 2, 3, ...] hash(0) = hash(1), hash(2) = hash(3), ... |
| 77 | +
|
| 78 | + Args: |
| 79 | + reta: Used to get predicted queue based on hash. |
| 80 | + received_packets: Packets received in the verbose output of testpmd. |
| 81 | + verify_packet_pairs: Verify pairs of packets have the same hash. |
| 82 | +
|
| 83 | + Raises: |
| 84 | + InteractiveCommandExecutionError: If packet_hash is None. |
| 85 | + """ |
| 86 | + # List of packet hashes, used for symmetric algorithms |
| 87 | + hash_list = [] |
| 88 | + for packet in received_packets: |
| 89 | + # Ignore stray packets |
| 90 | + if packet.port_id != 0 or packet.src_mac != "02:00:00:00:00:00": |
| 91 | + continue |
| 92 | + # Get packet hash |
| 93 | + packet_hash = packet.rss_hash |
| 94 | + if packet_hash is None: |
| 95 | + raise InteractiveCommandExecutionError( |
| 96 | + "Packet sent by the Traffic Generator has no RSS hash attribute." |
| 97 | + ) |
| 98 | + |
| 99 | + packet_queue = packet.rss_queue |
| 100 | + |
| 101 | + # Calculate the predicted packet queue |
| 102 | + predicted_queue = reta[packet_hash % len(reta)] |
| 103 | + verify( |
| 104 | + predicted_queue == packet_queue, |
| 105 | + "Packet sent by the Traffic Generator assigned to incorrect queue by the RSS.", |
| 106 | + ) |
| 107 | + |
| 108 | + if verify_packet_pairs: |
| 109 | + hash_list.append(packet_hash) |
| 110 | + |
| 111 | + if verify_packet_pairs: |
| 112 | + # Go through pairs of hashes in list and verify they are the same |
| 113 | + for odd_hash, even_hash in zip(hash_list[0::2], hash_list[1::2]): |
| 114 | + verify( |
| 115 | + odd_hash == even_hash, |
| 116 | + "Packet pair do not have same hash. Hash algorithm is not symmetric.", |
| 117 | + ) |
| 118 | + |
| 119 | + def _send_test_packets( |
| 120 | + self, |
| 121 | + testpmd: TestPmd, |
| 122 | + send_additional_mirrored_packet: bool = False, |
| 123 | + ) -> list[TestPmdVerbosePacket]: |
| 124 | + """Sends test packets. |
| 125 | +
|
| 126 | + Send 10 packets from the TG to SUT, parsing the verbose output and returning it. |
| 127 | + If the algorithm chosen is symmetric, send an additional packet for each initial |
| 128 | + packet sent, which has the L4 src and dst swapped. |
| 129 | +
|
| 130 | + Args: |
| 131 | + testpmd: Used to send packets and send commands to testpmd. |
| 132 | + send_additional_mirrored_packet: Send an additional mirrored packet for each packet |
| 133 | + sent. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + TestPmdVerbosePacket: List of packets. |
| 137 | + """ |
| 138 | + # Create test packets |
| 139 | + packets = [] |
| 140 | + for i in range(10): |
| 141 | + packets.append( |
| 142 | + Ether(src="02:00:00:00:00:00", dst="11:00:00:00:00:00") |
| 143 | + / IP() |
| 144 | + / UDP(sport=i, dport=i + 1), |
| 145 | + ) |
| 146 | + if send_additional_mirrored_packet: # If symmetric, send the inverse packets |
| 147 | + packets.append( |
| 148 | + Ether(src="02:00:00:00:00:00", dst="11:00:00:00:00:00") |
| 149 | + / IP() |
| 150 | + / UDP(sport=i + 1, dport=i), |
| 151 | + ) |
| 152 | + |
| 153 | + # Set verbose packet information and start packet capture |
| 154 | + testpmd.set_verbose(level=3) |
| 155 | + testpmd.start() |
| 156 | + testpmd.start_all_ports() |
| 157 | + send_packets_and_capture(packets) |
| 158 | + |
| 159 | + # Stop packet capture and revert verbose packet information |
| 160 | + testpmd_shell_out = testpmd.stop() |
| 161 | + testpmd.set_verbose(level=0) |
| 162 | + # Parse the packets and return them |
| 163 | + return testpmd.extract_verbose_output(testpmd_shell_out) |
| 164 | + |
| 165 | + def _setup_rss_environment( |
| 166 | + self, |
| 167 | + testpmd: TestPmd, |
| 168 | + ) -> None: |
| 169 | + """Sets up the testpmd environment for RSS test suites. |
| 170 | +
|
| 171 | + Sets the testpmd forward mode to rx_only and RSS on the NIC to UDP. |
| 172 | +
|
| 173 | + Args: |
| 174 | + testpmd: Where the environment will be set. |
| 175 | + """ |
| 176 | + # Set forward mode to receive only, to remove forwarded packets from verbose output |
| 177 | + testpmd.set_forward_mode(SimpleForwardingModes.rxonly) |
| 178 | + |
| 179 | + # Reset RSS settings and only RSS udp packets |
| 180 | + testpmd.port_config_all_rss_offload_type(RSSOffloadTypesFlag.udp) |
| 181 | + |
| 182 | + def _configure_reta_presets(self, testpmd: TestPmd, queue_number: int) -> list[list[int]]: |
| 183 | + """Build deterministic RETA presets. |
| 184 | +
|
| 185 | + Sequential pattern [0,1,2,...] repeated to RETA size, |
| 186 | + reversed pattern [...,2,1,0] repeated to RETA size, |
| 187 | + and constant pattern of queue 0. |
| 188 | +
|
| 189 | + Args: |
| 190 | + testpmd: The testpmd instance that will be used to set the RSS environment. |
| 191 | + queue_number: Number of queues available. |
| 192 | +
|
| 193 | + Returns: |
| 194 | + List of RETA tables. |
| 195 | +
|
| 196 | + Raises: |
| 197 | + InteractiveCommandExecutionError: If size of RETA table for driver is None. |
| 198 | + """ |
| 199 | + reta_size = testpmd.show_port_info(port_id=0).redirection_table_size |
| 200 | + if reta_size is None: |
| 201 | + raise InteractiveCommandExecutionError("Size of RETA table for driver is None.") |
| 202 | + |
| 203 | + seq = [(i % queue_number) for i in range(reta_size)] |
| 204 | + rev = [queue_number - 1 - (i % queue_number) for i in range(reta_size)] |
| 205 | + const0 = [0 for _ in range(reta_size)] |
| 206 | + return [seq, rev, const0] |
| 207 | + |
| 208 | + def _apply_reta(self, testpmd: TestPmd, reta: list[int]) -> None: |
| 209 | + """Program a RETA into testpmd.""" |
| 210 | + for i, q in enumerate(reta): |
| 211 | + testpmd.port_config_rss_reta(port_id=0, hash_index=i, queue_id=q) |
| 212 | + |
| 213 | + def _verify_rss_hash_function( |
| 214 | + self, |
| 215 | + testpmd: TestPmd, |
| 216 | + hash_algorithm: HashAlgorithm, |
| 217 | + flow_rule: FlowRule, |
| 218 | + reta: list[int], |
| 219 | + ) -> None: |
| 220 | + """Verifies hash function are working by sending test packets and checking the packet queue. |
| 221 | +
|
| 222 | + Args: |
| 223 | + testpmd: The testpmd instance that will be used to set the rss environment. |
| 224 | + hash_algorithm: The hash algorithm to be tested. |
| 225 | + flow_rule: The flow rule that is to be validated and then created. |
| 226 | + reta: Will be used to calculate the predicted packet queues. |
| 227 | + """ |
| 228 | + is_symmetric = hash_algorithm == HashAlgorithm.SYMMETRIC_TOEPLITZ |
| 229 | + self._setup_rss_environment(testpmd) |
| 230 | + testpmd.flow_create(flow_rule, port_id=0) |
| 231 | + # Send udp packets and ensure hash corresponds with queue |
| 232 | + parsed_output = self._send_test_packets( |
| 233 | + testpmd, send_additional_mirrored_packet=is_symmetric |
| 234 | + ) |
| 235 | + self._verify_hash_queue(reta, parsed_output, is_symmetric) |
| 236 | + |
| 237 | + @func_test |
| 238 | + def key_hash_algorithm(self) -> None: |
| 239 | + """Hashing algorithm test. |
| 240 | +
|
| 241 | + Steps: |
| 242 | + Setup RSS environment using the chosen algorithm. |
| 243 | + Send test packets for each flow rule. |
| 244 | +
|
| 245 | + Verify: |
| 246 | + Packet hash corresponds to the packet queue. |
| 247 | +
|
| 248 | + Raises: |
| 249 | + InteractiveCommandExecutionError: If size of RETA table for driver is None. |
| 250 | + InteractiveCommandExecutionError: If there are no valid flow rules that can be created. |
| 251 | + """ |
| 252 | + failed_attempts: int = 0 |
| 253 | + for algorithm in HashAlgorithm: |
| 254 | + flow_rule = FlowRule( |
| 255 | + group_id=0, |
| 256 | + direction="ingress", |
| 257 | + pattern=["eth / ipv4 / udp"], |
| 258 | + actions=[f"rss types ipv4-udp end queues end func {algorithm.name.lower()}"], |
| 259 | + ) |
| 260 | + with TestPmd( |
| 261 | + rx_queues=self.config.NUM_QUEUES, |
| 262 | + tx_queues=self.config.NUM_QUEUES, |
| 263 | + ) as testpmd: |
| 264 | + reta_tables = self._configure_reta_presets(testpmd, self.config.NUM_QUEUES) |
| 265 | + for reta in reta_tables: |
| 266 | + self._apply_reta(testpmd, reta) |
| 267 | + if not testpmd.flow_validate(flow_rule, port_id=0): |
| 268 | + # Queues need to be specified in the flow rule on some NICs |
| 269 | + queue_ids = " ".join([str(x) for x in reta]) |
| 270 | + flow_rule.actions = [ |
| 271 | + f"rss types ipv4-udp end queues {queue_ids} end func " |
| 272 | + + algorithm.name.lower() |
| 273 | + ] |
| 274 | + |
| 275 | + if not testpmd.flow_validate(flow_rule, port_id=0): |
| 276 | + failed_attempts += 1 |
| 277 | + if failed_attempts == len(HashAlgorithm): |
| 278 | + raise InteractiveCommandExecutionError( |
| 279 | + "No Valid flow rule could be created." |
| 280 | + ) |
| 281 | + # if neither rule format is valid then the algorithm is not supported, |
| 282 | + # move to next one |
| 283 | + continue |
| 284 | + self._verify_rss_hash_function(testpmd, algorithm, flow_rule, reta) |
| 285 | + |
| 286 | + @func_test |
| 287 | + def update_key_set_hash_key_short_long(self) -> None: |
| 288 | + """Set hash key short long test. |
| 289 | +
|
| 290 | + Steps: |
| 291 | + Fetch the hash key size. |
| 292 | + Create two random hash keys one key too short and one too long. |
| 293 | +
|
| 294 | + Verify: |
| 295 | + Verify that it is not possible to set the shorter hash key. |
| 296 | + Verify that it is not possible to set the longer hash key. |
| 297 | +
|
| 298 | + Raises: |
| 299 | + InteractiveCommandExecutionError: If port info dose not contain hash key size. |
| 300 | + """ |
| 301 | + with TestPmd( |
| 302 | + memory_channels=4, |
| 303 | + rx_queues=self.config.NUM_QUEUES, |
| 304 | + tx_queues=self.config.NUM_QUEUES, |
| 305 | + ) as testpmd: |
| 306 | + # Get RETA and key size |
| 307 | + port_info = testpmd.show_port_info(port_id=0) |
| 308 | + |
| 309 | + # Get hash key size |
| 310 | + key_size = port_info.hash_key_size |
| 311 | + if key_size is None: |
| 312 | + raise InteractiveCommandExecutionError("Port info does not contain hash key size.") |
| 313 | + |
| 314 | + # Create 2 hash keys based on the NIC capabilities |
| 315 | + short_key = "".join( |
| 316 | + [random.choice("0123456789ABCDEF") for n in range(key_size * 2 - 2)] |
| 317 | + ) |
| 318 | + long_key = "".join([random.choice("0123456789ABCDEF") for n in range(key_size * 2 + 2)]) |
| 319 | + |
| 320 | + # Verify a short key cannot be set |
| 321 | + short_key_out = testpmd.port_config_rss_hash_key( |
| 322 | + 0, RSSOffloadTypesFlag.ipv4_udp, short_key, False |
| 323 | + ) |
| 324 | + verify( |
| 325 | + "invalid" in short_key_out, |
| 326 | + "Able to set hash key shorter than specified.", |
| 327 | + ) |
| 328 | + |
| 329 | + # Verify a long key cannot be set |
| 330 | + long_key_out = testpmd.port_config_rss_hash_key( |
| 331 | + 0, RSSOffloadTypesFlag.ipv4_udp, long_key, False |
| 332 | + ) |
| 333 | + verify("invalid" in long_key_out, "Able to set hash key longer than specified.") |
| 334 | + |
| 335 | + @func_test |
| 336 | + def update_key_reported_key_size(self) -> None: |
| 337 | + """Verify reported hash key size is the same as the NIC capabilities. |
| 338 | +
|
| 339 | + Steps: |
| 340 | + Fetch the hash key size and compare to the actual key size. |
| 341 | +
|
| 342 | + Verify: |
| 343 | + Reported key size is the same as the actual key size. |
| 344 | + """ |
| 345 | + with TestPmd() as testpmd: |
| 346 | + reported_key_size = testpmd.show_port_info(port_id=0).hash_key_size |
| 347 | + verify( |
| 348 | + reported_key_size == self.config.ACTUAL_KEY_SIZE, |
| 349 | + "Reported key size is not the same as the config file.", |
| 350 | + ) |
| 351 | + |
| 352 | + @func_test |
| 353 | + def reta_key_reta_queues(self) -> None: |
| 354 | + """RETA rx/tx queues test. |
| 355 | +
|
| 356 | + Steps: |
| 357 | + For each queue size setup RSS environment and send Test packets. |
| 358 | +
|
| 359 | + Verify: |
| 360 | + Packet hash corresponds to hash queue. |
| 361 | +
|
| 362 | + Raises: |
| 363 | + InteractiveCommandExecutionError: If size of RETA table for driver is None. |
| 364 | + """ |
| 365 | + queues_numbers = [2, 9, 16] |
| 366 | + for queue_number in queues_numbers: |
| 367 | + with TestPmd( |
| 368 | + rx_queues=queue_number, |
| 369 | + tx_queues=queue_number, |
| 370 | + ) as testpmd: |
| 371 | + reta_tables = self._configure_reta_presets(testpmd, queue_number) |
| 372 | + for reta in reta_tables: |
| 373 | + self._apply_reta(testpmd, reta) |
| 374 | + self._setup_rss_environment(testpmd) |
| 375 | + |
| 376 | + # Send UDP packets and ensure hash corresponds with queue |
| 377 | + parsed_output = self._send_test_packets(testpmd) |
| 378 | + self._verify_hash_queue(reta, parsed_output, False) |
| 379 | + |
| 380 | + @func_test |
| 381 | + def reta_key_reported_reta_size(self) -> None: |
| 382 | + """Reported RETA size test. |
| 383 | +
|
| 384 | + Steps: |
| 385 | + Fetch reported reta size. |
| 386 | +
|
| 387 | + Verify: |
| 388 | + Reported RETA size is equal to the actual RETA size. |
| 389 | + """ |
| 390 | + with TestPmd( |
| 391 | + rx_queues=self.config.NUM_QUEUES, |
| 392 | + tx_queues=self.config.NUM_QUEUES, |
| 393 | + ) as testpmd: |
| 394 | + reported_reta_size = testpmd.show_port_info(port_id=0).redirection_table_size |
| 395 | + verify( |
| 396 | + reported_reta_size == self.config.ACTUAL_RETA_SIZE, |
| 397 | + "Reported RETA size is not the same as the config file.", |
| 398 | + ) |
0 commit comments