lib: remove extra parenthesis after return
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
index 42db8e3..eed481e 100644 (file)
@@ -217,6 +217,16 @@ const char *rte_get_rx_ol_flag_name(uint64_t mask);
  */
 const char *rte_get_tx_ol_flag_name(uint64_t mask);
 
+/**
+ * Some NICs need at least 2KB buffer to RX standard Ethernet frame without
+ * splitting it into multiple segments.
+ * So, for mbufs that planned to be involved into RX/TX, the recommended
+ * minimal buffer length is 2KB + RTE_PKTMBUF_HEADROOM.
+ */
+#define        RTE_MBUF_DEFAULT_DATAROOM       2048
+#define        RTE_MBUF_DEFAULT_BUF_SIZE       \
+       (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
+
 /* define a set of marker types that can be used to refer to set points in the
  * mbuf */
 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
@@ -318,18 +328,49 @@ struct rte_mbuf {
                        /* uint64_t unused:8; */
                };
        };
+
+       /** Size of the application private data. In case of an indirect
+        * mbuf, it stores the direct mbuf private data size. */
+       uint16_t priv_size;
 } __rte_cache_aligned;
 
+static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
+
 /**
- * Given the buf_addr returns the pointer to corresponding mbuf.
+ * Return the mbuf owning the data buffer address of an indirect mbuf.
+ *
+ * @param mi
+ *   The pointer to the indirect mbuf.
+ * @return
+ *   The address of the direct mbuf corresponding to buffer_addr.
  */
-#define RTE_MBUF_FROM_BADDR(ba)     (((struct rte_mbuf *)(ba)) - 1)
+static inline struct rte_mbuf *
+rte_mbuf_from_indirect(struct rte_mbuf *mi)
+{
+       struct rte_mbuf *md;
+
+       /* mi->buf_addr and mi->priv_size correspond to buffer and
+        * private size of the direct mbuf */
+       md = (struct rte_mbuf *)((char *)mi->buf_addr - sizeof(*mi) -
+               mi->priv_size);
+       return md;
+}
 
 /**
- * Given the pointer to mbuf returns an address where it's  buf_addr
- * should point to.
+ * Return the buffer address embedded in the given mbuf.
+ *
+ * @param md
+ *   The pointer to the mbuf.
+ * @return
+ *   The address of the data buffer owned by the mbuf.
  */
-#define RTE_MBUF_TO_BADDR(mb)       (((struct rte_mbuf *)(mb)) + 1)
+static inline char *
+rte_mbuf_to_baddr(struct rte_mbuf *md)
+{
+       char *buffer_addr;
+       buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
+       return buffer_addr;
+}
 
 /**
  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
@@ -384,21 +425,6 @@ if (!(exp)) {                                                        \
 
 #ifdef RTE_MBUF_REFCNT_ATOMIC
 
-/**
- * Adds given value to an mbuf's refcnt and returns its new value.
- * @param m
- *   Mbuf to update
- * @param value
- *   Value to add/subtract
- * @return
- *   Updated value
- */
-static inline uint16_t
-rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
-{
-       return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
-}
-
 /**
  * Reads the value of an mbuf's refcnt.
  * @param m
@@ -425,6 +451,33 @@ rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
        rte_atomic16_set(&m->refcnt_atomic, new_value);
 }
 
+/**
+ * Adds given value to an mbuf's refcnt and returns its new value.
+ * @param m
+ *   Mbuf to update
+ * @param value
+ *   Value to add/subtract
+ * @return
+ *   Updated value
+ */
+static inline uint16_t
+rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
+{
+       /*
+        * The atomic_add is an expensive operation, so we don't want to
+        * call it in the case where we know we are the uniq holder of
+        * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
+        * operation has to be used because concurrent accesses on the
+        * reference counter can occur.
+        */
+       if (likely(rte_mbuf_refcnt_read(m) == 1)) {
+               rte_mbuf_refcnt_set(m, 1 + value);
+               return 1 + value;
+       }
+
+       return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
+}
+
 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
 
 /**
@@ -499,7 +552,7 @@ static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
        m = (struct rte_mbuf *)mb;
        RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
        rte_mbuf_refcnt_set(m, 1);
-       return (m);
+       return m;
 }
 
 /**
@@ -595,7 +648,7 @@ void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
 static inline int
 rte_is_ctrlmbuf(struct rte_mbuf *m)
 {
-       return (!!(m->ol_flags & CTRL_MBUF_FLAG));
+       return !!(m->ol_flags & CTRL_MBUF_FLAG);
 }
 
 /* Operations on pkt mbuf */
@@ -766,56 +819,64 @@ 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);
-       return (m);
+       return m;
 }
 
 /**
  * Attach packet mbuf to another packet mbuf.
+ *
  * After attachment we refer the mbuf we attached as 'indirect',
  * while mbuf we attached to as 'direct'.
  * Right now, not supported:
- *  - attachment to indirect mbuf (e.g. - md  has to be direct).
  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
  *  - mbuf we trying to attach (mi) is used by someone else
  *    e.g. it's reference counter is greater then 1.
  *
  * @param mi
  *   The indirect packet mbuf.
- * @param md
- *   The direct packet mbuf.
+ * @param m
+ *   The packet mbuf we're attaching to.
  */
-
-static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
+static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
 {
-       RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
-           RTE_MBUF_DIRECT(mi) &&
+       struct rte_mbuf *md;
+
+       RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(mi) &&
            rte_mbuf_refcnt_read(mi) == 1);
 
+       /* if m is not direct, get the mbuf that embeds the data */
+       if (RTE_MBUF_DIRECT(m))
+               md = m;
+       else
+               md = rte_mbuf_from_indirect(m);
+
        rte_mbuf_refcnt_update(md, 1);
-       mi->buf_physaddr = md->buf_physaddr;
-       mi->buf_addr = md->buf_addr;
-       mi->buf_len = md->buf_len;
-
-       mi->next = md->next;
-       mi->data_off = md->data_off;
-       mi->data_len = md->data_len;
-       mi->port = md->port;
-       mi->vlan_tci = md->vlan_tci;
-       mi->tx_offload = md->tx_offload;
-       mi->hash = md->hash;
+       mi->priv_size = m->priv_size;
+       mi->buf_physaddr = m->buf_physaddr;
+       mi->buf_addr = m->buf_addr;
+       mi->buf_len = m->buf_len;
+
+       mi->next = m->next;
+       mi->data_off = m->data_off;
+       mi->data_len = m->data_len;
+       mi->port = m->port;
+       mi->vlan_tci = m->vlan_tci;
+       mi->tx_offload = m->tx_offload;
+       mi->hash = m->hash;
 
        mi->next = NULL;
        mi->pkt_len = mi->data_len;
        mi->nb_segs = 1;
-       mi->ol_flags = md->ol_flags | IND_ATTACHED_MBUF;
-       mi->packet_type = md->packet_type;
+       mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
+       mi->packet_type = m->packet_type;
 
        __rte_mbuf_sanity_check(mi, 1);
-       __rte_mbuf_sanity_check(md, 0);
+       __rte_mbuf_sanity_check(m, 0);
 }
 
 /**
- * Detach an indirect packet mbuf -
+ * Detach an indirect packet mbuf.
+ *
  *  - restore original mbuf address and length values.
  *  - reset pktmbuf data and data_len to their default values.
  *  All other fields of the given packet mbuf will be left intact.
@@ -823,22 +884,21 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
  * @param m
  *   The indirect attached packet mbuf.
  */
-
 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
 {
-       const struct rte_mempool *mp = m->pool;
-       void *buf = RTE_MBUF_TO_BADDR(m);
-       uint32_t buf_len = mp->elt_size - sizeof(*m);
-       m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
+       struct rte_mempool *mp = m->pool;
+       uint32_t mbuf_size, buf_len, priv_size;
 
-       m->buf_addr = buf;
-       m->buf_len = (uint16_t)buf_len;
-
-       m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
-                       RTE_PKTMBUF_HEADROOM : m->buf_len;
+       priv_size = rte_pktmbuf_priv_size(mp);
+       mbuf_size = sizeof(struct rte_mbuf) + priv_size;
+       buf_len = rte_pktmbuf_data_room_size(mp);
 
+       m->priv_size = priv_size;
+       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);
        m->data_len = 0;
-
        m->ol_flags = 0;
 }
 
@@ -847,34 +907,21 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
        __rte_mbuf_sanity_check(m, 0);
 
-       /*
-        * Check to see if this is the last reference to the mbuf.
-        * Note: the double check here is deliberate. If the ref_cnt is "atomic"
-        * the call to "refcnt_update" is a very expensive operation, so we
-        * don't want to call it in the case where we know we are the holder
-        * of the last reference to this mbuf i.e. ref_cnt == 1.
-        * If however, ref_cnt != 1, it's still possible that we may still be
-        * the final decrementer of the count, so we need to check that
-        * result also, to make sure the mbuf is freed properly.
-        */
-       if (likely (rte_mbuf_refcnt_read(m) == 1) ||
-                       likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
-
-               rte_mbuf_refcnt_set(m, 0);
+       if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
 
                /* if this is an indirect mbuf, then
                 *  - detach mbuf
                 *  - free attached mbuf segment
                 */
                if (RTE_MBUF_INDIRECT(m)) {
-                       struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
+                       struct rte_mbuf *md = rte_mbuf_from_indirect(m);
                        rte_pktmbuf_detach(m);
                        if (rte_mbuf_refcnt_update(md, -1) == 0)
                                __rte_mbuf_raw_free(md);
                }
-               return(m);
+               return m;
        }
-       return (NULL);
+       return NULL;
 }
 
 /**
@@ -942,7 +989,7 @@ static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
        uint8_t nseg;
 
        if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
-               return (NULL);
+               return NULL;
 
        mi = mc;
        prev = &mi->next;
@@ -964,11 +1011,11 @@ static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
        /* Allocation of new indirect segment failed */
        if (unlikely (mi == NULL)) {
                rte_pktmbuf_free(mc);
-               return (NULL);
+               return NULL;
        }
 
        __rte_mbuf_sanity_check(mc, 1);
-       return (mc);
+       return mc;
 }
 
 /**