mbuf: use reserved space for double vlan
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
index ab6de67..80419df 100644 (file)
@@ -54,6 +54,7 @@
  */
 
 #include <stdint.h>
+#include <rte_common.h>
 #include <rte_mempool.h>
 #include <rte_memory.h>
 #include <rte_atomic.h>
@@ -101,10 +102,16 @@ extern "C" {
 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
+#define PKT_RX_QINQ_PKT      (1ULL << 15)  /**< RX packet with double VLAN stripped. */
 /* add new RX flags here */
 
 /* add new TX flags here */
 
+/**
+ * Second VLAN insertion (QinQ) flag.
+ */
+#define PKT_TX_QINQ_PKT    (1ULL << 49)   /**< TX packet with double VLAN inserted. */
+
 /**
  * TCP segmentation offload. To enable this offload feature for a
  * packet to be transmitted on hardware supporting TSO:
@@ -279,7 +286,7 @@ struct rte_mbuf {
        uint16_t data_len;        /**< Amount of data in segment buffer. */
        uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
        uint16_t vlan_tci;        /**< VLAN Tag Control Identifier (CPU order) */
-       uint16_t reserved;
+       uint16_t vlan_tci_outer;  /**< Outer VLAN Tag Control Identifier (CPU order) */
        union {
                uint32_t rss;     /**< RSS hash result if RSS enabled */
                struct {
@@ -347,13 +354,7 @@ static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
 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;
+       return RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
 /**
@@ -425,21 +426,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
@@ -466,6 +452,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 */
 
 /**
@@ -540,7 +553,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;
 }
 
 /**
@@ -636,7 +649,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 */
@@ -777,6 +790,7 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
        m->pkt_len = 0;
        m->tx_offload = 0;
        m->vlan_tci = 0;
+       m->vlan_tci_outer = 0;
        m->nb_segs = 1;
        m->port = 0xff;
 
@@ -807,7 +821,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);
-       return (m);
+       return m;
 }
 
 /**
@@ -849,6 +863,7 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
        mi->data_len = m->data_len;
        mi->port = m->port;
        mi->vlan_tci = m->vlan_tci;
+       mi->vlan_tci_outer = m->vlan_tci_outer;
        mi->tx_offload = m->tx_offload;
        mi->hash = m->hash;
 
@@ -895,20 +910,7 @@ __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
@@ -920,9 +922,9 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
                        if (rte_mbuf_refcnt_update(md, -1) == 0)
                                __rte_mbuf_raw_free(md);
                }
-               return(m);
+               return m;
        }
-       return (NULL);
+       return NULL;
 }
 
 /**
@@ -990,7 +992,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;
@@ -1012,11 +1014,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;
 }
 
 /**
@@ -1086,19 +1088,36 @@ static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
        return m2;
 }
 
+/**
+ * A macro that points to an offset into the data in the mbuf.
+ *
+ * The returned pointer is cast to type t. Before using this
+ * function, the user must ensure that the first segment is large
+ * enough to accommodate its data.
+ *
+ * @param m
+ *   The packet mbuf.
+ * @param o
+ *   The offset into the mbuf data.
+ * @param t
+ *   The type to cast the result into.
+ */
+#define rte_pktmbuf_mtod_offset(m, t, o)       \
+       ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
+
 /**
  * A macro that points to the start of the data in the mbuf.
  *
  * The returned pointer is cast to type t. Before using this
- * function, the user must ensure that m_headlen(m) is large enough to
- * read its data.
+ * function, the user must ensure that the first segment is large
+ * enough to accommodate its data.
  *
  * @param m
  *   The packet mbuf.
  * @param t
  *   The type to cast the result into.
  */
-#define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
+#define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
 
 /**
  * A macro that returns the length of the packet.