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