xref: /dpdk/dts/tests/TestSuite_pmd_buffer_scatter.py (revision 64fdb622e3f15da32dee0feffb18e552ff14c044)
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-untyped]
21from scapy.layers.l2 import Ether  # type: ignore[import-untyped]
22from scapy.packet import Raw  # type: ignore[import-untyped]
23from scapy.utils import hexstr  # type: ignore[import-untyped]
24
25from framework.params.testpmd import SimpleForwardingModes
26from framework.remote_session.testpmd_shell import TestPmdShell
27from framework.test_suite import TestSuite, func_test
28from framework.testbed_model.capability import NicCapability, requires
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            Increase the MTU of both ports on the traffic generator to 9000
58            to support larger packet sizes.
59        """
60        self.tg_node.main_session.configure_port_mtu(9000, self._tg_port_egress)
61        self.tg_node.main_session.configure_port_mtu(9000, self._tg_port_ingress)
62
63    def scatter_pktgen_send_packet(self, pktsize: int) -> str:
64        """Generate and send a packet to the SUT then capture what is forwarded back.
65
66        Generate an IP packet of a specific length and send it to the SUT,
67        then capture the resulting received packet and extract its payload.
68        The desired length of the packet is met by packing its payload
69        with the letter "X" in hexadecimal.
70
71        Args:
72            pktsize: Size of the packet to generate and send.
73
74        Returns:
75            The payload of the received packet as a string.
76        """
77        packet = Ether() / IP() / Raw()
78        packet.getlayer(2).load = ""
79        payload_len = pktsize - len(packet) - 4
80        payload = ["58"] * payload_len
81        # pack the payload
82        for X_in_hex in payload:
83            packet.load += struct.pack("=B", int("%s%s" % (X_in_hex[0], X_in_hex[1]), 16))
84        received_packets = self.send_packet_and_capture(packet)
85        self.verify(len(received_packets) > 0, "Did not receive any packets.")
86        load = hexstr(received_packets[0].getlayer(2), onlyhex=1)
87
88        return load
89
90    def pmd_scatter(self, mbsize: int) -> None:
91        """Testpmd support of receiving and sending scattered multi-segment packets.
92
93        Support for scattered packets is shown by sending 5 packets of differing length
94        where the length of the packet is calculated by taking mbuf-size + an offset.
95        The offsets used in the test are -1, 0, 1, 4, 5 respectively.
96
97        Test:
98            Start testpmd and run functional test with preset mbsize.
99        """
100        with TestPmdShell(
101            self.sut_node,
102            forward_mode=SimpleForwardingModes.mac,
103            mbcache=200,
104            mbuf_size=[mbsize],
105            max_pkt_len=9000,
106            tx_offloads=0x00008000,
107        ) as testpmd:
108            testpmd.start()
109
110            for offset in [-1, 0, 1, 4, 5]:
111                recv_payload = self.scatter_pktgen_send_packet(mbsize + offset)
112                self._logger.debug(
113                    f"Payload of scattered packet after forwarding: \n{recv_payload}"
114                )
115                self.verify(
116                    ("58 " * 8).strip() in recv_payload,
117                    "Payload of scattered packet did not match expected payload with offset "
118                    f"{offset}.",
119                )
120
121    @requires(NicCapability.SCATTERED_RX_ENABLED)
122    @func_test
123    def test_scatter_mbuf_2048(self) -> None:
124        """Run the :meth:`pmd_scatter` test with `mbsize` set to 2048."""
125        self.pmd_scatter(mbsize=2048)
126
127    def tear_down_suite(self) -> None:
128        """Tear down the test suite.
129
130        Teardown:
131            Set the MTU of the tg_node back to a more standard size of 1500.
132        """
133        self.tg_node.main_session.configure_port_mtu(1500, self._tg_port_egress)
134        self.tg_node.main_session.configure_port_mtu(1500, self._tg_port_ingress)
135