1# SPDX-License-Identifier: BSD-3-Clause 2# Copyright(c) 2023-2024 University of New Hampshire 3 4"""Multi-segment packet scattering testing suite. 5 6This testing suite tests the support of transmitting and receiving scattered packets. 7This is shown by the Poll Mode Driver being able to forward 8scattered multi-segment packets composed of multiple non-contiguous memory buffers. 9To ensure the receipt of scattered packets, 10the DMA rings of the port's Rx queues must be configured 11with mbuf data buffers whose size is less than the maximum length. 12 13If it is the case that the Poll Mode Driver can forward scattered packets which it receives, 14then this suffices to show the Poll Mode Driver is capable of both receiving and transmitting 15scattered packets. 16""" 17 18import struct 19from dataclasses import asdict 20 21from scapy.layers.inet import IP # type: ignore[import-untyped] 22from scapy.layers.l2 import Ether # type: ignore[import-untyped] 23from scapy.packet import Raw # type: ignore[import-untyped] 24from scapy.utils import hexstr # type: ignore[import-untyped] 25 26from framework.params.testpmd import SimpleForwardingModes, TestPmdParams 27from framework.remote_session.testpmd_shell import TestPmdShell 28from framework.test_suite import TestSuite 29 30 31class TestPmdBufferScatter(TestSuite): 32 """DPDK PMD packet scattering test suite. 33 34 Configure the Rx queues to have mbuf data buffers 35 whose sizes are smaller than the maximum packet size. 36 Specifically, set mbuf data buffers to have a size of 2048 37 to fit a full 1512-byte (CRC included) ethernet frame in a mono-segment packet. 38 The testing of scattered packets is done by sending a packet 39 whose length is greater than the size of the configured size of mbuf data buffers. 40 There are a total of 5 packets sent within test cases 41 which have lengths less than, equal to, and greater than the mbuf size. 42 There are multiple packets sent with lengths greater than the mbuf size 43 in order to test cases such as: 44 45 1. A single byte of the CRC being in a second buffer 46 while the remaining 3 bytes are stored in the first buffer alongside packet data. 47 2. The entire CRC being stored in a second buffer 48 while all of the packet data is stored in the first. 49 3. Most of the packet data being stored in the first buffer 50 and a single byte of packet data stored in a second buffer alongside the CRC. 51 """ 52 53 def set_up_suite(self) -> None: 54 """Set up the test suite. 55 56 Setup: 57 Verify that we have at least 2 port links in the current test run 58 and increase the MTU of both ports on the traffic generator to 9000 59 to support larger packet sizes. 60 """ 61 self.verify( 62 len(self._port_links) > 1, 63 "There must be at least two port links to run the scatter test suite", 64 ) 65 66 self.tg_node.main_session.configure_port_mtu(9000, self._tg_port_egress) 67 self.tg_node.main_session.configure_port_mtu(9000, self._tg_port_ingress) 68 69 def scatter_pktgen_send_packet(self, pktsize: int) -> str: 70 """Generate and send a packet to the SUT then capture what is forwarded back. 71 72 Generate an IP packet of a specific length and send it to the SUT, 73 then capture the resulting received packet and extract its payload. 74 The desired length of the packet is met by packing its payload 75 with the letter "X" in hexadecimal. 76 77 Args: 78 pktsize: Size of the packet to generate and send. 79 80 Returns: 81 The payload of the received packet as a string. 82 """ 83 packet = Ether() / IP() / Raw() 84 packet.getlayer(2).load = "" 85 payload_len = pktsize - len(packet) - 4 86 payload = ["58"] * payload_len 87 # pack the payload 88 for X_in_hex in payload: 89 packet.load += struct.pack("=B", int("%s%s" % (X_in_hex[0], X_in_hex[1]), 16)) 90 received_packets = self.send_packet_and_capture(packet) 91 self.verify(len(received_packets) > 0, "Did not receive any packets.") 92 load = hexstr(received_packets[0].getlayer(2), onlyhex=1) 93 94 return load 95 96 def pmd_scatter(self, mbsize: int) -> None: 97 """Testpmd support of receiving and sending scattered multi-segment packets. 98 99 Support for scattered packets is shown by sending 5 packets of differing length 100 where the length of the packet is calculated by taking mbuf-size + an offset. 101 The offsets used in the test are -1, 0, 1, 4, 5 respectively. 102 103 Test: 104 Start testpmd and run functional test with preset mbsize. 105 """ 106 testpmd = self.sut_node.create_interactive_shell( 107 TestPmdShell, 108 app_params=TestPmdParams( 109 forward_mode=SimpleForwardingModes.mac, 110 mbcache=200, 111 mbuf_size=[mbsize], 112 max_pkt_len=9000, 113 tx_offloads=0x00008000, 114 **asdict(self.sut_node.create_eal_parameters()), 115 ), 116 privileged=True, 117 ) 118 testpmd.start() 119 120 for offset in [-1, 0, 1, 4, 5]: 121 recv_payload = self.scatter_pktgen_send_packet(mbsize + offset) 122 self._logger.debug(f"Payload of scattered packet after forwarding: \n{recv_payload}") 123 self.verify( 124 ("58 " * 8).strip() in recv_payload, 125 f"Payload of scattered packet did not match expected payload with offset {offset}.", 126 ) 127 testpmd.stop() 128 129 def test_scatter_mbuf_2048(self) -> None: 130 """Run the :meth:`pmd_scatter` test with `mbsize` set to 2048.""" 131 self.pmd_scatter(mbsize=2048) 132 133 def tear_down_suite(self) -> None: 134 """Tear down the test suite. 135 136 Teardown: 137 Set the MTU of the tg_node back to a more standard size of 1500. 138 """ 139 self.tg_node.main_session.configure_port_mtu(1500, self._tg_port_egress) 140 self.tg_node.main_session.configure_port_mtu(1500, self._tg_port_ingress) 141