[dpdk-dev,1/2] mbuf: add rte_pktmbuff_reset_headroom function

Message ID 1475151633-6785-1-git-send-email-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Maxime Coquelin Sept. 29, 2016, 12:20 p.m. UTC
  Some application use rte_mbuf_raw_alloc() function to improve
performance by not resetting mbuf's fields to their default state.

This can be however problematic for mbuf consumers that need some
headroom, meaning that data_off field gets decremented after
allocation. When the mbuf is re-used afterwards, there might not
be enough room for the consumer to prepend anything, if the data_off
field is not reset to its default value.

This patch adds a new rte_pktmbuf_reset_headroom() function that
applications can call to reset the data_off field.
This patch also replaces current data_off affectations in the mbuf
lib with a call to this function.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_mbuf/rte_mbuf.h | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
  

Comments

Olivier Matz Oct. 3, 2016, 4:11 p.m. UTC | #1
Hi Maxime,

On 09/29/2016 02:20 PM, Maxime Coquelin wrote:
> Some application use rte_mbuf_raw_alloc() function to improve
> performance by not resetting mbuf's fields to their default state.
> 
> This can be however problematic for mbuf consumers that need some
> headroom, meaning that data_off field gets decremented after
> allocation. When the mbuf is re-used afterwards, there might not
> be enough room for the consumer to prepend anything, if the data_off
> field is not reset to its default value.
> 
> This patch adds a new rte_pktmbuf_reset_headroom() function that
> applications can call to reset the data_off field.
> This patch also replaces current data_off affectations in the mbuf
> lib with a call to this function.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Sounds like a good idea. Just one small comment below.

>  
>  /**
> + * Reset the data_off field of a packet mbuf to its default value.
> + *
> + * The given mbuf must have only one segment.
> + *
> + * @param m
> + *   The packet mbuf's data_off field has to be reset.
> + */
> +static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
> +{
> +	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
> +}

Maybe we should also highlight in the API comment that the segment
should be empty.


Thanks,
Olivier
  
Maxime Coquelin Oct. 4, 2016, 11:51 a.m. UTC | #2
On 10/03/2016 06:11 PM, Olivier Matz wrote:
> Hi Maxime,
>
> On 09/29/2016 02:20 PM, Maxime Coquelin wrote:
>> Some application use rte_mbuf_raw_alloc() function to improve
>> performance by not resetting mbuf's fields to their default state.
>>
>> This can be however problematic for mbuf consumers that need some
>> headroom, meaning that data_off field gets decremented after
>> allocation. When the mbuf is re-used afterwards, there might not
>> be enough room for the consumer to prepend anything, if the data_off
>> field is not reset to its default value.
>>
>> This patch adds a new rte_pktmbuf_reset_headroom() function that
>> applications can call to reset the data_off field.
>> This patch also replaces current data_off affectations in the mbuf
>> lib with a call to this function.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>
> Sounds like a good idea. Just one small comment below.
>
>>
>>  /**
>> + * Reset the data_off field of a packet mbuf to its default value.
>> + *
>> + * The given mbuf must have only one segment.
>> + *
>> + * @param m
>> + *   The packet mbuf's data_off field has to be reset.
>> + */
>> +static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
>> +{
>> +	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
>> +}
>
> Maybe we should also highlight in the API comment that the segment
> should be empty.

Good point. I'll change to:

The given mbuf must have only one segment, which should be empty.

Thanks,
Maxime
  

Patch

diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
index 23b7bf8..6a687dd 100644
--- a/lib/librte_mbuf/rte_mbuf.h
+++ b/lib/librte_mbuf/rte_mbuf.h
@@ -1387,6 +1387,19 @@  rte_pktmbuf_priv_size(struct rte_mempool *mp)
 }
 
 /**
+ * Reset the data_off field of a packet mbuf to its default value.
+ *
+ * The given mbuf must have only one segment.
+ *
+ * @param m
+ *   The packet mbuf's data_off field has to be reset.
+ */
+static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
+{
+	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
+}
+
+/**
  * Reset the fields of a packet mbuf to their default values.
  *
  * The given mbuf must have only one segment.
@@ -1406,8 +1419,7 @@  static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
 
 	m->ol_flags = 0;
 	m->packet_type = 0;
-	m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
-			RTE_PKTMBUF_HEADROOM : m->buf_len;
+	rte_pktmbuf_reset_headroom(m);
 
 	m->data_len = 0;
 	__rte_mbuf_sanity_check(m, 1);
@@ -1571,7 +1583,7 @@  static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
 	m->buf_addr = (char *)m + mbuf_size;
 	m->buf_physaddr = rte_mempool_virt2phy(mp, m) + mbuf_size;
 	m->buf_len = (uint16_t)buf_len;
-	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
+	rte_pktmbuf_reset_headroom(m);
 	m->data_len = 0;
 	m->ol_flags = 0;