[PATCH v2] sched: fix queue stats read for mixed subports
Alexander.Chesnokov at kaspersky.com
Alexander.Chesnokov at kaspersky.com
Thu Nov 26 06:16:33 CET 2026
From: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>
rte_sched_queue_read_stats() checks the queue ID against the total
number of queues of the port, summed over the subports. The queue
address is computed the other way: as a fixed shift by the capacity
of n_pipes_per_subport pipes. Subports of the same port may enable
different numbers of pipes, and both directions of that mismatch
are wrong.
A queue ID inside the hole of a smaller subport passes the total
count check, decodes into that smaller subport, and the stats copy
and the clear then write outside its arrays. A queue ID of a larger
subport past the cumulative total is rejected by the total count
check although it encodes an enabled queue; the queues of the pipes
past that total stay unreadable. The damage of the first kind
surfaces later, at the first use of the overwritten data; ASAN does
not see it because the subport arrays share one allocation.
Decode the subport ID without the wraparound mask and check it
against port->n_subports_per_port, then check the queue index
against the number of queues of the subport that owns it, and
reject queue IDs that fail either check with -EINVAL. For
homogeneous subports the accepted set is the same as before: IDs
below 2^qmask * n_subports decode exactly as before and larger IDs
are rejected, previously by the total count and now by the subport
ID check. Every queue ID produced by rte_sched_port_pkt_write()
decodes back to the queue it was encoded from. The total count
helper has no consumers left and is removed.
Add a sched_hetero_subports_autotest case with a port of two
subports: one pipe enabled on subport 0 and eight on subport 1.
Queue IDs inside the hole of the small subport and IDs beyond the
last subport are rejected. Queue IDs of the first and the last
queue of both subports read cleanly, including the queue IDs past
the cumulative total. A packet enqueued through the first queue of
pipe 1 of subport 1 (queue ID 144) shows up in the queue stats
read for that queue ID.
Fixes: 831104f0e84f ("sched: update queue stats read for config flexibility")
Cc: stable at dpdk.org
Signed-off-by: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>
Cc: jasvinder.singh at intel.com
---
v2:
- Replace the total queue count check kept in v1, which still
rejected the valid queue IDs of the pipes past the cumulative
total, with the subport ID check on the unmasked shift plus the
queue index check against the owning subport; drop the now
unused total count helper.
- Commit message rewritten to describe both directions of the
queue ID mismatch.
- Test: assert the queue IDs past the cumulative total readable,
round-trip a packet through queue ID 144, fix the last queue
comment, free the dequeued mbuf and the mempool.
app/test/test_sched.c | 116 ++++++++++++++++++++++++++++++++++++++++++
lib/sched/rte_sched.c | 30 +++++------
2 files changed, 128 insertions(+), 18 deletions(-)
diff --git a/app/test/test_sched.c b/app/test/test_sched.c
index 96b48f1897..55bf5a8c44 100644
--- a/app/test/test_sched.c
+++ b/app/test/test_sched.c
@@ -22,6 +22,13 @@ test_sched(void)
printf("sched not supported on Windows, skipping test\n");
return TEST_SKIPPED;
}
+
+static int
+test_sched_hetero_subports(void)
+{
+ printf("sched not supported on Windows, skipping test\n");
+ return TEST_SKIPPED;
+}
#else
#include <rte_sched.h>
@@ -213,6 +220,115 @@ test_sched(void)
return 0;
}
+/*
+ * Port with mixed subports: subport 0 enables 1 pipe, subport 1 enables
+ * 8. Queue IDs valid for the queue stats API follow the subport shift
+ * layout of rte_sched_port_pkt_write().
+ */
+static int
+test_sched_hetero_subports(void)
+{
+ struct rte_sched_port_params port_params = port_param;
+ struct rte_sched_subport_params sp[2];
+ struct rte_sched_queue_stats queue_stats;
+ struct rte_mbuf *mbufs[1];
+ struct rte_mempool *mp;
+ struct rte_sched_port *port;
+ uint16_t qlen;
+ uint32_t i;
+ int err;
+
+ port_params.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
+ port_params.n_subports_per_port = 2;
+ port_params.n_pipes_per_subport = 8;
+
+ sp[0] = subport_param[0];
+ sp[0].n_pipes_per_subport_enabled = 1;
+ sp[1] = sp[0];
+ sp[1].n_pipes_per_subport_enabled = 8;
+
+ mp = create_mempool();
+ TEST_ASSERT_NOT_NULL(mp, "Error creating mempool\n");
+
+ port = rte_sched_port_config(&port_params);
+ TEST_ASSERT_NOT_NULL(port, "Error config sched port\n");
+
+ for (i = 0; i < 2; i++) {
+ err = rte_sched_subport_config(port, i, &sp[i], 0);
+ TEST_ASSERT_SUCCESS(err,
+ "Error config sched subport %u, err=%d\n", i, err);
+ }
+
+ for (i = 0; i < sp[0].n_pipes_per_subport_enabled; i++) {
+ err = rte_sched_pipe_config(port, 0, i, 0);
+ TEST_ASSERT_SUCCESS(err,
+ "Error config sched subport 0 pipe %u, err=%d\n",
+ i, err);
+ }
+
+ for (i = 0; i < sp[1].n_pipes_per_subport_enabled; i++) {
+ err = rte_sched_pipe_config(port, 1, i, 0);
+ TEST_ASSERT_SUCCESS(err,
+ "Error config sched subport 1 pipe %u, err=%d\n",
+ i, err);
+ }
+
+ /* Queue ID in the hole of the small subport 0 is rejected */
+ err = rte_sched_queue_read_stats(port, 92, &queue_stats, &qlen);
+ TEST_ASSERT_FAIL(err, "Queue ID 92 accepted, err=%d\n", err);
+
+ /* Queue ID beyond the last subport is rejected */
+ err = rte_sched_queue_read_stats(port, 256, &queue_stats, &qlen);
+ TEST_ASSERT_FAIL(err, "Queue ID 256 accepted, err=%d\n", err);
+
+ /* First and last queue of the small subport 0 read cleanly */
+ err = rte_sched_queue_read_stats(port, 0, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 0 rejected, err=%d\n", err);
+ err = rte_sched_queue_read_stats(port, 15, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 15 rejected, err=%d\n", err);
+
+ /* First queue of the large subport 1, past the cumulative total */
+ err = rte_sched_queue_read_stats(port, 128, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 128 rejected, err=%d\n", err);
+
+ /* First queue of pipe 1 of subport 1, past the cumulative total */
+ err = rte_sched_queue_read_stats(port, 144, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 144 rejected, err=%d\n", err);
+
+ /* Last queue of the large subport 1 */
+ err = rte_sched_queue_read_stats(port, 255, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 255 rejected, err=%d\n", err);
+
+ /*
+ * A packet through the first queue of pipe 1 of subport 1
+ * (queue ID 144) shows up in the queue that the stats API reads
+ * for that ID
+ */
+ mbufs[0] = rte_pktmbuf_alloc(mp);
+ TEST_ASSERT_NOT_NULL(mbufs[0], "Packet allocation failed\n");
+ mbufs[0]->pkt_len = 60;
+ mbufs[0]->data_len = 60;
+ rte_sched_port_pkt_write(port, mbufs[0], 1, 1, 0, 0, RTE_COLOR_GREEN);
+
+ err = rte_sched_port_enqueue(port, mbufs, 1);
+ TEST_ASSERT_EQUAL(err, 1, "Wrong enqueue, err=%d\n", err);
+
+ err = rte_sched_port_dequeue(port, mbufs, 1);
+ TEST_ASSERT_EQUAL(err, 1, "Wrong dequeue, err=%d\n", err);
+ rte_pktmbuf_free(mbufs[0]);
+
+ err = rte_sched_queue_read_stats(port, 144, &queue_stats, &qlen);
+ TEST_ASSERT_SUCCESS(err, "Queue ID 144 rejected, err=%d\n", err);
+ TEST_ASSERT_EQUAL(queue_stats.n_pkts, 1, "Wrong queue stats\n");
+ TEST_ASSERT_EQUAL(qlen, 0, "Wrong queue length\n");
+
+ rte_sched_port_free(port);
+ rte_mempool_free(mp);
+
+ return 0;
+}
#endif /* !RTE_EXEC_ENV_WINDOWS */
REGISTER_FAST_TEST(sched_autotest, NOHUGE_OK, ASAN_OK, test_sched);
+REGISTER_FAST_TEST(sched_hetero_subports_autotest, NOHUGE_OK,
+ ASAN_OK, test_sched_hetero_subports);
diff --git a/lib/sched/rte_sched.c b/lib/sched/rte_sched.c
index daa31e8b99..56becd0576 100644
--- a/lib/sched/rte_sched.c
+++ b/lib/sched/rte_sched.c
@@ -279,17 +279,6 @@ struct rte_sched_subport *subport, uint32_t qindex)
return subport->qsize[tc];
}
-static inline uint32_t
-rte_sched_port_queues_per_port(struct rte_sched_port *port)
-{
- uint32_t n_queues = 0, i;
-
- for (i = 0; i < port->n_subports_per_port; i++)
- n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
-
- return n_queues;
-}
-
static inline uint16_t
rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
{
@@ -1758,12 +1747,6 @@ rte_sched_queue_read_stats(struct rte_sched_port *port,
return -EINVAL;
}
- if (queue_id >= rte_sched_port_queues_per_port(port)) {
- SCHED_LOG(ERR,
- "%s: Incorrect value for queue id", __func__);
- return -EINVAL;
- }
-
if (stats == NULL) {
SCHED_LOG(ERR,
"%s: Incorrect value for parameter stats", __func__);
@@ -1776,10 +1759,21 @@ rte_sched_queue_read_stats(struct rte_sched_port *port,
return -EINVAL;
}
subport_qmask = port->n_pipes_per_subport_log2 + 4;
- subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
+ subport_id = queue_id >> subport_qmask;
+ if (subport_id >= port->n_subports_per_port) {
+ SCHED_LOG(ERR,
+ "%s: Incorrect value for queue id", __func__);
+ return -EINVAL;
+ }
s = port->subports[subport_id];
subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
+ if (subport_qindex >= rte_sched_subport_pipe_queues(s)) {
+ SCHED_LOG(ERR,
+ "%s: Incorrect value for queue id", __func__);
+ return -EINVAL;
+ }
+
q = s->queue + subport_qindex;
qe = s->queue_extra + subport_qindex;
--
2.53.0
More information about the stable
mailing list