mbuf: use 2 bytes for port and nb segments
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
index bfce9f4..323a1ac 100644 (file)
  * buffers. The message buffers are stored in a mempool, using the
  * RTE mempool library.
  *
+ * The preferred way to create a mbuf pool is to use
+ * rte_pktmbuf_pool_create(). However, in some situations, an
+ * application may want to have more control (ex: populate the pool with
+ * specific memory), in this case it is possible to use functions from
+ * rte_mempool. See how rte_pktmbuf_pool_create() is implemented for
+ * details.
+ *
  * This library provides an API to allocate/free packet mbufs, which are
  * used to carry network packets.
  *
@@ -298,9 +305,11 @@ extern "C" {
                PKT_TX_L4_MASK |         \
                PKT_TX_OUTER_IP_CKSUM |  \
                PKT_TX_TCP_SEG |         \
+               PKT_TX_IEEE1588_TMST |   \
                PKT_TX_QINQ_PKT |        \
                PKT_TX_VLAN_PKT |        \
-               PKT_TX_TUNNEL_MASK)
+               PKT_TX_TUNNEL_MASK |     \
+               PKT_TX_MACSEC)
 
 #define __RESERVED           (1ULL << 61) /**< reserved for future mbuf use */
 
@@ -391,14 +400,13 @@ struct rte_mbuf {
        void *buf_addr;           /**< Virtual address of segment buffer. */
        phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
 
-       uint16_t buf_len;         /**< Length of segment buffer. */
-
-       /* next 6 bytes are initialised on RX descriptor rearm */
-       MARKER8 rearm_data;
+       /* next 8 bytes are initialised on RX descriptor rearm */
+       MARKER64 rearm_data;
        uint16_t data_off;
 
        /**
-        * 16-bit Reference counter.
+        * Reference counter. Its size should at least equal to the size
+        * of port field (16 bits), to support zero-copy broadcast.
         * It should only be accessed using the following functions:
         * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
         * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
@@ -410,8 +418,10 @@ struct rte_mbuf {
                rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
                uint16_t refcnt;              /**< Non-atomically accessed refcnt */
        };
-       uint8_t nb_segs;          /**< Number of segments. */
-       uint8_t port;             /**< Input port. */
+       uint16_t nb_segs;         /**< Number of segments. */
+
+       /** Input port (16 bits to support more than 256 virtual ports). */
+       uint16_t port;
 
        uint64_t ol_flags;        /**< Offload features. */
 
@@ -472,6 +482,7 @@ struct rte_mbuf {
        /** Outer VLAN TCI (CPU order), valid if PKT_RX_QINQ_STRIPPED is set. */
        uint16_t vlan_tci_outer;
 
+       uint16_t buf_len;         /**< Length of segment buffer. */
        /* second cache line - fields only used in slow path or on TX */
        MARKER cacheline1 __rte_cache_min_aligned;
 
@@ -766,6 +777,11 @@ rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
  * initializing all the required fields. See rte_pktmbuf_reset().
  * For standard needs, prefer rte_pktmbuf_alloc().
  *
+ * The caller can expect that the following fields of the mbuf structure
+ * are initialized: buf_addr, buf_physaddr, buf_len, refcnt=1, nb_segs=1,
+ * next=NULL, pool, priv_size. The other fields must be initialized
+ * by the caller.
+ *
  * @param mp
  *   The mempool from which mbuf is allocated.
  * @return
@@ -780,28 +796,47 @@ static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
        if (rte_mempool_get(mp, &mb) < 0)
                return NULL;
        m = (struct rte_mbuf *)mb;
-       RTE_ASSERT(rte_mbuf_refcnt_read(m) == 0);
-       rte_mbuf_refcnt_set(m, 1);
+       RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
+       RTE_ASSERT(m->next == NULL);
+       RTE_ASSERT(m->nb_segs == 1);
        __rte_mbuf_sanity_check(m, 0);
 
        return m;
 }
 
 /**
- * @internal Put mbuf back into its original mempool.
- * The use of that function is reserved for RTE internal needs.
- * Please use rte_pktmbuf_free().
+ * Put mbuf back into its original mempool.
+ *
+ * The caller must ensure that the mbuf 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_free() or
+ * rte_pktmbuf_free_seg().
  *
  * @param m
  *   The mbuf to be freed.
  */
 static inline void __attribute__((always_inline))
-__rte_mbuf_raw_free(struct rte_mbuf *m)
+rte_mbuf_raw_free(struct rte_mbuf *m)
 {
-       RTE_ASSERT(rte_mbuf_refcnt_read(m) == 0);
+       RTE_ASSERT(RTE_MBUF_DIRECT(m));
+       RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
+       RTE_ASSERT(m->next == NULL);
+       RTE_ASSERT(m->nb_segs == 1);
+       __rte_mbuf_sanity_check(m, 0);
        rte_mempool_put(m->pool, m);
 }
 
+/* compat with older versions */
+__rte_deprecated
+static inline void
+__rte_mbuf_raw_free(struct rte_mbuf *m)
+{
+       rte_mbuf_raw_free(m);
+}
+
 /* Operations on ctrl mbuf */
 
 /**
@@ -810,14 +845,14 @@ __rte_mbuf_raw_free(struct rte_mbuf *m)
  * This function initializes some fields in an mbuf structure that are
  * not modified by the user once created (mbuf type, origin pool, buffer
  * start address, and so on). This function is given as a callback function
- * to rte_mempool_create() at pool creation time.
+ * to rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
  *
  * @param mp
  *   The mempool from which the mbuf is allocated.
  * @param opaque_arg
  *   A pointer that can be used by the user to retrieve useful information
- *   for mbuf initialization. This pointer comes from the ``init_arg``
- *   parameter of rte_mempool_create().
+ *   for mbuf initialization. This pointer is the opaque argument passed to
+ *   rte_mempool_obj_iter() or rte_mempool_create().
  * @param m
  *   The mbuf to initialize.
  * @param i
@@ -891,14 +926,14 @@ rte_is_ctrlmbuf(struct rte_mbuf *m)
  * This function initializes some fields in the mbuf structure that are
  * not modified by the user once created (origin pool, buffer start
  * address, and so on). This function is given as a callback function to
- * rte_mempool_create() at pool creation time.
+ * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
  *
  * @param mp
  *   The mempool from which mbufs originate.
  * @param opaque_arg
  *   A pointer that can be used by the user to retrieve useful information
- *   for mbuf initialization. This pointer comes from the ``init_arg``
- *   parameter of rte_mempool_create().
+ *   for mbuf initialization. This pointer is the opaque argument passed to
+ *   rte_mempool_obj_iter() or rte_mempool_create().
  * @param m
  *   The mbuf to initialize.
  * @param i
@@ -913,7 +948,8 @@ void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
  *
  * This function initializes the mempool private data in the case of a
  * pktmbuf pool. This private data is needed by the driver. The
- * function is given as a callback function to rte_mempool_create() at
+ * function must be called on the mempool before it is used, or it
+ * can be given as a callback function to rte_mempool_create() at
  * pool creation. It can be extended by the user, for example, to
  * provide another packet size.
  *
@@ -921,8 +957,8 @@ void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
  *   The mempool from which mbufs originate.
  * @param opaque_arg
  *   A pointer that can be used by the user to retrieve useful information
- *   for mbuf initialization. This pointer comes from the ``init_arg``
- *   parameter of rte_mempool_create().
+ *   for mbuf initialization. This pointer is the opaque argument passed to
+ *   rte_mempool_create().
  */
 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
 
@@ -930,8 +966,7 @@ void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
  * Create a mbuf pool.
  *
  * This function creates and initializes a packet mbuf pool. It is
- * a wrapper to rte_mempool_create() with the proper packet constructor
- * and mempool constructor.
+ * a wrapper to rte_mempool functions.
  *
  * @param name
  *   The name of the mbuf pool.
@@ -1158,7 +1193,6 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
        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;
@@ -1208,24 +1242,71 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
        m->data_len = 0;
        m->ol_flags = 0;
 
-       if (rte_mbuf_refcnt_update(md, -1) == 0)
-               __rte_mbuf_raw_free(md);
+       if (rte_mbuf_refcnt_update(md, -1) == 0) {
+               md->next = NULL;
+               md->nb_segs = 1;
+               rte_mbuf_refcnt_set(md, 1);
+               rte_mbuf_raw_free(md);
+       }
 }
 
-static inline struct rte_mbuf* __attribute__((always_inline))
-__rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
+/**
+ * Decrease reference counter and unlink a mbuf segment
+ *
+ * This function does the same than a free, except that it does not
+ * return the segment to its pool.
+ * It decreases the reference counter, and if it reaches 0, it is
+ * detached from its parent for an indirect mbuf.
+ *
+ * @param m
+ *   The mbuf to be unlinked
+ * @return
+ *   - (m) if it is the last reference. It can be recycled or freed.
+ *   - (NULL) if the mbuf still has remaining references on it.
+ */
+__attribute__((always_inline))
+static inline struct rte_mbuf *
+rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
        __rte_mbuf_sanity_check(m, 0);
 
-       if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
-               /* if this is an indirect mbuf, it is detached. */
+       if (likely(rte_mbuf_refcnt_read(m) == 1)) {
+
+               if (RTE_MBUF_INDIRECT(m))
+                       rte_pktmbuf_detach(m);
+
+               if (m->next != NULL) {
+                       m->next = NULL;
+                       m->nb_segs = 1;
+               }
+
+               return m;
+
+       } else if (rte_atomic16_add_return(&m->refcnt_atomic, -1) == 0) {
+
+
                if (RTE_MBUF_INDIRECT(m))
                        rte_pktmbuf_detach(m);
+
+               if (m->next != NULL) {
+                       m->next = NULL;
+                       m->nb_segs = 1;
+               }
+               rte_mbuf_refcnt_set(m, 1);
+
                return m;
        }
        return NULL;
 }
 
+/* deprecated, replaced by rte_pktmbuf_prefree_seg() */
+__rte_deprecated
+static inline struct rte_mbuf *
+__rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
+{
+       return rte_pktmbuf_prefree_seg(m);
+}
+
 /**
  * Free a segment of a packet mbuf into its original mempool.
  *
@@ -1238,10 +1319,9 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 static inline void __attribute__((always_inline))
 rte_pktmbuf_free_seg(struct rte_mbuf *m)
 {
-       if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
-               m->next = NULL;
-               __rte_mbuf_raw_free(m);
-       }
+       m = rte_pktmbuf_prefree_seg(m);
+       if (likely(m != NULL))
+               rte_mbuf_raw_free(m);
 }
 
 /**