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