The kernel rejects a ring whose block size is not a multiple of the
page size or exceeds INT_MAX, whose frame size is smaller than
TPACKET2_HDRLEN or not a multiple of TPACKET_ALIGNMENT, or whose frame
count is not a multiple of frames per block. Probe accepted these and
failed later in setsockopt(PACKET_RX_RING) with only EINVAL.

Check them at probe next to the existing frame size test, together with
the minimum frame size for Rx, and document the constraints.

Signed-off-by: Scott Mitchell <[email protected]>
---
 app/test/test_pmd_af_packet.c             | 41 +++++++++++++++++++++++
 doc/guides/nics/af_packet.rst             |  8 +++--
 drivers/net/af_packet/rte_eth_af_packet.c | 20 +++++------
 3 files changed, 56 insertions(+), 13 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 6a4806acc7..986bb094df 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -1256,6 +1256,46 @@ test_af_packet_frame_config(void)
        return TEST_SUCCESS;
 }
 
+/*
+ * Test: Ring geometry the kernel would reject is refused at probe
+ */
+static int
+test_af_packet_invalid_frame_config(void)
+{
+       const unsigned int pagesize = getpagesize();
+       /* framecnt 0 means one block, so only the named constraint is broken */
+       const struct {
+               unsigned int blocksz, framesz, framecnt;
+       } bad[] = {
+               { pagesize + 2048, 2048, 0 },   /* blocksz not a multiple of 
the page size */
+               { 1u << 31, 2048, 0 },          /* blocksz above INT_MAX */
+               { pagesize, 64, 0 },            /* framesz too small for a 
minimum Rx frame */
+               { pagesize, 2040, 0 },          /* framesz not a multiple of 
TPACKET_ALIGNMENT */
+               { pagesize, 2048, pagesize / 2048 + 1 }, /* framecnt not a 
block multiple */
+       };
+       char devargs[128];
+       unsigned int i, framecnt;
+       int ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       for (i = 0; i < RTE_DIM(bad); i++) {
+               framecnt = bad[i].framecnt != 0 ? bad[i].framecnt :
+                          bad[i].blocksz / bad[i].framesz;
+               snprintf(devargs, sizeof(devargs), 
"iface=%s,blocksz=%u,framesz=%u,framecnt=%u",
+                        TAP_DEV_NAME, bad[i].blocksz, bad[i].framesz, 
framecnt);
+               ret = rte_vdev_init("net_af_packet_bad_frame", devargs);
+               if (ret == 0)
+                       rte_vdev_uninit("net_af_packet_bad_frame");
+               TEST_ASSERT(ret != 0, "Expected failure with %s", devargs);
+       }
+
+       return TEST_SUCCESS;
+}
+
 /*
  * Test: Qdisc bypass configuration
  */
@@ -1791,6 +1831,7 @@ static struct unit_test_suite af_packet_test_suite = {
                TEST_CASE(test_af_packet_invalid_bad_iface),
                TEST_CASE(test_af_packet_invalid_qpairs),
                TEST_CASE(test_af_packet_frame_config),
+               TEST_CASE(test_af_packet_invalid_frame_config),
                TEST_CASE(test_af_packet_frame_layout),
                TEST_CASE(test_af_packet_frame_max_size),
                TEST_CASE(test_af_packet_qdisc_bypass),
diff --git a/doc/guides/nics/af_packet.rst b/doc/guides/nics/af_packet.rst
index d7fb4162fd..10ba325e81 100644
--- a/doc/guides/nics/af_packet.rst
+++ b/doc/guides/nics/af_packet.rst
@@ -27,10 +27,12 @@ Some of these, in turn, will be used to configure the 
PACKET_MMAP settings.
     Possible choices: hash, lb, cpu, rollover, rnd, qm (optional, default 
hash);
 *   ``ignore_outgoing`` - ``rte_eth_rx_burst()`` returns only incoming packets
     (optional, disabled by default);
-*   ``blocksz`` - PACKET_MMAP block size (optional, default 4096);
+*   ``blocksz`` - PACKET_MMAP block size (optional, default page size; Note:
+    multiple of page size, less than 2GB);
 *   ``framesz`` - PACKET_MMAP frame size (optional, default 2048B; Note: 
multiple
-    of 16B);
-*   ``framecnt`` - PACKET_MMAP frame count (optional, default 512).
+    of 16B, at least 144B);
+*   ``framecnt`` - PACKET_MMAP frame count (optional, default 512; Note: 
multiple
+    of ``blocksz`` / ``framesz``).
 
 For details regarding ``fanout_mode`` argument, you can consult the
 `PACKET_FANOUT documentation 
<https://www.man7.org/linux/man-pages/man7/packet.7.html>`_.
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c 
b/drivers/net/af_packet/rte_eth_af_packet.c
index 2e75175aad..344d09eea6 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -1285,6 +1285,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        unsigned k_idx;
        unsigned int blockcount;
        unsigned int blocksize;
+       unsigned int frames_per_block;
        unsigned int framesize = DFLT_FRAME_SIZE;
        unsigned int framecount = DFLT_FRAME_COUNT;
        unsigned int qpairs = 1;
@@ -1371,19 +1372,18 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                return -1;
        }
 
-       /* A frame must hold a minimum Ethernet frame after the Rx offset */
-       if (framesize < ETH_AF_PACKET_RX_FRAME_OVERHEAD + RTE_ETHER_MIN_LEN) {
-               PMD_LOG(ERR, "%s: framesz %u below minimum %zu", name, 
framesize,
-                       ETH_AF_PACKET_RX_FRAME_OVERHEAD + RTE_ETHER_MIN_LEN);
-               return -1;
-       }
-
-       blockcount = framecount / (blocksize / framesize);
-       if (!blockcount) {
+       /* Constraints the kernel enforces in packet_set_ring(), and room for a 
minimum Rx frame */
+       frames_per_block = blocksize / framesize;
+       if (blocksize % getpagesize() != 0 || blocksize > (unsigned int)INT_MAX 
||
+           framesize < ETH_AF_PACKET_RX_FRAME_OVERHEAD + RTE_ETHER_MIN_LEN ||
+           framesize % TPACKET_ALIGNMENT != 0 ||
+           framecount % frames_per_block != 0) {
                PMD_LOG(ERR,
-                       "%s: invalid AF_PACKET MMAP parameters", name);
+                       "%s: invalid AF_PACKET MMAP parameters: blocksz=%u 
framesz=%u framecnt=%u",
+                       name, blocksize, framesize, framecount);
                return -1;
        }
+       blockcount = framecount / frames_per_block;
 
        PMD_LOG(DEBUG, "%s: AF_PACKET MMAP parameters:", name);
        PMD_LOG(DEBUG, "%s:\tblock size %d", name, blocksize);
-- 
2.54.0 (Apple Git-157)

Reply via email to