[PATCH v6 3/3] mbuf: no need to reset all fields on reinitialized mbufs

Morten Brørup mb at smartsharesystems.com
Fri Aug 22 14:47:31 CEST 2025


The 'next' and 'nb_segs' fields are already reset on newly allocated
reinitialized mbufs (a.k.a. raw mbufs), so a simpler reset function for
reinitialized mbufs was added.

The 'ol_flags' field must indicate whether the mbuf uses an external
buffer.
Unlike the normal packet mbuf reset function, which gets this information
from the mbuf itself (by masking the RTE_MBUF_F_EXTERNAL flag), the
simpler reset function gets this information from the mempool, thereby
reducing the number of read-modify-writes on the mbuf from two to one.
The remaining read-modify-write on the mbuf in the simpler reset function
is in rte_pktmbuf_reset_headroom(), where the 'buf_len' field is read
before writing the 'data_off' field.
And I am hoping that it will be possible to eliminate that one too.

Signed-off-by: Morten Brørup <mb at smartsharesystems.com>
---
 lib/mbuf/rte_mbuf.h | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)

diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
index 49c93ab356..92a1e8ba7e 100644
--- a/lib/mbuf/rte_mbuf.h
+++ b/lib/mbuf/rte_mbuf.h
@@ -954,6 +954,39 @@ static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
 					(uint16_t)m->buf_len);
 }
 
+/**
+ * Reset the fields of a packet mbuf to their default values.
+ *
+ * The caller must ensure that the mbuf comes from the specified mempool,
+ * is direct and properly reinitialized (refcnt=1, next=NULL, nb_segs=1),
+ * as done by rte_pktmbuf_prefree_seg().
+ *
+ * This function should be used with care, when optimization is required.
+ * For standard needs, prefer rte_pktmbuf_reset().
+ *
+ * @param mp
+ *   The mempool to which the mbuf belongs.
+ * @param m
+ *   The packet mbuf to be reset.
+ */
+static inline void rte_mbuf_raw_reset(struct rte_mempool *mp, struct rte_mbuf *m)
+{
+	uint32_t flags = rte_pktmbuf_priv_flags(mp);
+
+	m->pkt_len = 0;
+	m->tx_offload = 0;
+	m->vlan_tci = 0;
+	m->vlan_tci_outer = 0;
+	m->port = RTE_MBUF_PORT_INVALID;
+
+	m->ol_flags = (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) ? RTE_MBUF_F_EXTERNAL : 0;
+	m->packet_type = 0;
+	rte_pktmbuf_reset_headroom(m);
+
+	m->data_len = 0;
+	__rte_mbuf_sanity_check(m, 1);
+}
+
 /**
  * Reset the fields of a packet mbuf to their default values.
  *
@@ -997,7 +1030,7 @@ static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
 {
 	struct rte_mbuf *m;
 	if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
-		rte_pktmbuf_reset(m);
+		rte_mbuf_raw_reset(mp, m);
 	return m;
 }
 
@@ -1033,19 +1066,19 @@ static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
 	switch (count % 4) {
 	case 0:
 		while (idx != count) {
-			rte_pktmbuf_reset(mbufs[idx]);
+			rte_mbuf_raw_reset(pool, mbufs[idx]);
 			idx++;
 			/* fall-through */
 	case 3:
-			rte_pktmbuf_reset(mbufs[idx]);
+			rte_mbuf_raw_reset(pool, mbufs[idx]);
 			idx++;
 			/* fall-through */
 	case 2:
-			rte_pktmbuf_reset(mbufs[idx]);
+			rte_mbuf_raw_reset(pool, mbufs[idx]);
 			idx++;
 			/* fall-through */
 	case 1:
-			rte_pktmbuf_reset(mbufs[idx]);
+			rte_mbuf_raw_reset(pool, mbufs[idx]);
 			idx++;
 			/* fall-through */
 		}
-- 
2.43.0



More information about the dev mailing list