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