mbuf: expand ol_flags field to 64-bits
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_MBUF_H_
35 #define _RTE_MBUF_H_
36
37 /**
38  * @file
39  * RTE Mbuf
40  *
41  * The mbuf library provides the ability to create and destroy buffers
42  * that may be used by the RTE application to store message
43  * buffers. The message buffers are stored in a mempool, using the
44  * RTE mempool library.
45  *
46  * This library provide an API to allocate/free packet mbufs, which are
47  * used to carry network packets.
48  *
49  * To understand the concepts of packet buffers or mbufs, you
50  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
51  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
52  * http://www.kohala.com/start/tcpipiv2.html
53  */
54
55 #include <stdint.h>
56 #include <rte_mempool.h>
57 #include <rte_atomic.h>
58 #include <rte_prefetch.h>
59 #include <rte_branch_prediction.h>
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
66 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
67
68 /*
69  * Packet Offload Features Flags. It also carry packet type information.
70  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
71  */
72 #define PKT_RX_VLAN_PKT      0x0001 /**< RX packet is a 802.1q VLAN packet. */
73 #define PKT_RX_RSS_HASH      0x0002 /**< RX packet with RSS hash result. */
74 #define PKT_RX_FDIR          0x0004 /**< RX packet with FDIR infos. */
75 #define PKT_RX_L4_CKSUM_BAD  0x0008 /**< L4 cksum of RX pkt. is not OK. */
76 #define PKT_RX_IP_CKSUM_BAD  0x0010 /**< IP cksum of RX pkt. is not OK. */
77 #define PKT_RX_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
78 #define PKT_RX_OVERSIZE      0x0000 /**< Num of desc of an RX pkt oversize. */
79 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
80 #define PKT_RX_RECIP_ERR     0x0000 /**< Hardware processing error. */
81 #define PKT_RX_MAC_ERR       0x0000 /**< MAC error. */
82 #define PKT_RX_IPV4_HDR      0x0020 /**< RX packet with IPv4 header. */
83 #define PKT_RX_IPV4_HDR_EXT  0x0040 /**< RX packet with extended IPv4 header. */
84 #define PKT_RX_IPV6_HDR      0x0080 /**< RX packet with IPv6 header. */
85 #define PKT_RX_IPV6_HDR_EXT  0x0100 /**< RX packet with extended IPv6 header. */
86 #define PKT_RX_IEEE1588_PTP  0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
87 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
88
89 #define PKT_TX_VLAN_PKT      0x0800 /**< TX packet is a 802.1q VLAN packet. */
90 #define PKT_TX_IP_CKSUM      0x1000 /**< IP cksum of TX pkt. computed by NIC. */
91 #define PKT_TX_IPV4_CSUM     0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
92 #define PKT_TX_IPV4          PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
93 #define PKT_TX_IPV6          PKT_RX_IPV6_HDR /**< IPv6 packet */
94 /*
95  * Bit 14~13 used for L4 packet type with checksum enabled.
96  *     00: Reserved
97  *     01: TCP checksum
98  *     10: SCTP checksum
99  *     11: UDP checksum
100  */
101 #define PKT_TX_L4_MASK       0x6000 /**< Mask bits for L4 checksum offload request. */
102 #define PKT_TX_L4_NO_CKSUM   0x0000 /**< Disable L4 cksum of TX pkt. */
103 #define PKT_TX_TCP_CKSUM     0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
104 #define PKT_TX_SCTP_CKSUM    0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
105 #define PKT_TX_UDP_CKSUM     0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
106 /* Bit 15 */
107 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
108
109 /**
110  * Bit Mask to indicate what bits required for building TX context
111  */
112 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
113
114 /**
115  * The generic rte_mbuf, containing a packet mbuf.
116  */
117 struct rte_mbuf {
118         void *buf_addr;           /**< Virtual address of segment buffer. */
119         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
120
121         /* next 8 bytes are initialised on RX descriptor rearm */
122         uint16_t buf_len;         /**< Length of segment buffer. */
123         uint16_t data_off;
124
125 #ifdef RTE_MBUF_REFCNT
126         /**
127          * 16-bit Reference counter.
128          * It should only be accessed using the following functions:
129          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
130          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
131          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
132          * config option.
133          */
134         union {
135                 rte_atomic16_t refcnt_atomic;   /**< Atomically accessed refcnt */
136                 uint16_t refcnt;                /**< Non-atomically accessed refcnt */
137         };
138 #else
139         uint16_t refcnt_reserved;     /**< Do not use this field */
140 #endif
141         uint8_t nb_segs;        /**< Number of segments. */
142         uint8_t port;           /**< Input port. */
143
144         uint64_t ol_flags;      /**< Offload features. */
145
146         /* remaining bytes are set on RX when pulling packet from descriptor */
147         uint16_t reserved2;     /**< Unused field. Required for padding */
148         uint16_t data_len;      /**< Amount of data in segment buffer. */
149         uint32_t pkt_len;       /**< Total pkt len: sum of all segments. */
150         union {
151                 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
152                 struct {
153                         uint16_t l3_len:9;      /**< L3 (IP) Header Length. */
154                         uint16_t l2_len:7;      /**< L2 (MAC) Header Length. */
155                 };
156         };
157         uint16_t vlan_tci;      /**< VLAN Tag Control Identifier (CPU order). */
158         union {
159                 uint32_t rss;       /**< RSS hash result if RSS enabled */
160                 struct {
161                         uint16_t hash;
162                         uint16_t id;
163                 } fdir;             /**< Filter identifier if FDIR enabled */
164                 uint32_t sched;     /**< Hierarchical scheduler */
165         } hash;                 /**< hash information */
166
167         /* fields only used in slow path or on TX */
168         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
169         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
170
171         union {
172                 uint8_t metadata[0];
173                 uint16_t metadata16[0];
174                 uint32_t metadata32[0];
175                 uint64_t metadata64[0];
176         } __rte_cache_aligned;
177 } __rte_cache_aligned;
178
179 #define RTE_MBUF_METADATA_UINT8(mbuf, offset)              \
180         (mbuf->metadata[offset])
181 #define RTE_MBUF_METADATA_UINT16(mbuf, offset)             \
182         (mbuf->metadata16[offset/sizeof(uint16_t)])
183 #define RTE_MBUF_METADATA_UINT32(mbuf, offset)             \
184         (mbuf->metadata32[offset/sizeof(uint32_t)])
185 #define RTE_MBUF_METADATA_UINT64(mbuf, offset)             \
186         (mbuf->metadata64[offset/sizeof(uint64_t)])
187
188 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset)          \
189         (&mbuf->metadata[offset])
190 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset)         \
191         (&mbuf->metadata16[offset/sizeof(uint16_t)])
192 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset)         \
193         (&mbuf->metadata32[offset/sizeof(uint32_t)])
194 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset)         \
195         (&mbuf->metadata64[offset/sizeof(uint64_t)])
196
197 /**
198  * Given the buf_addr returns the pointer to corresponding mbuf.
199  */
200 #define RTE_MBUF_FROM_BADDR(ba)     (((struct rte_mbuf *)(ba)) - 1)
201
202 /**
203  * Given the pointer to mbuf returns an address where it's  buf_addr
204  * should point to.
205  */
206 #define RTE_MBUF_TO_BADDR(mb)       (((struct rte_mbuf *)(mb)) + 1)
207
208 /**
209  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
210  */
211 #define RTE_MBUF_INDIRECT(mb)   (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
212
213 /**
214  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
215  */
216 #define RTE_MBUF_DIRECT(mb)     (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
217
218
219 /**
220  * Private data in case of pktmbuf pool.
221  *
222  * A structure that contains some pktmbuf_pool-specific data that are
223  * appended after the mempool structure (in private data).
224  */
225 struct rte_pktmbuf_pool_private {
226         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
227 };
228
229 #ifdef RTE_LIBRTE_MBUF_DEBUG
230
231 /**  check mbuf type in debug mode */
232 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
233
234 /**  check mbuf type in debug mode if mbuf pointer is not null */
235 #define __rte_mbuf_sanity_check_raw(m, is_h)    do {       \
236         if ((m) != NULL)                                   \
237                 rte_mbuf_sanity_check(m, is_h);          \
238 } while (0)
239
240 /**  MBUF asserts in debug mode */
241 #define RTE_MBUF_ASSERT(exp)                                         \
242 if (!(exp)) {                                                        \
243         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
244 }
245
246 #else /*  RTE_LIBRTE_MBUF_DEBUG */
247
248 /**  check mbuf type in debug mode */
249 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
250
251 /**  check mbuf type in debug mode if mbuf pointer is not null */
252 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
253
254 /**  MBUF asserts in debug mode */
255 #define RTE_MBUF_ASSERT(exp)                do { } while (0)
256
257 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
258
259 #ifdef RTE_MBUF_REFCNT
260 #ifdef RTE_MBUF_REFCNT_ATOMIC
261
262 /**
263  * Adds given value to an mbuf's refcnt and returns its new value.
264  * @param m
265  *   Mbuf to update
266  * @param value
267  *   Value to add/subtract
268  * @return
269  *   Updated value
270  */
271 static inline uint16_t
272 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
273 {
274         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
275 }
276
277 /**
278  * Reads the value of an mbuf's refcnt.
279  * @param m
280  *   Mbuf to read
281  * @return
282  *   Reference count number.
283  */
284 static inline uint16_t
285 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
286 {
287         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
288 }
289
290 /**
291  * Sets an mbuf's refcnt to a defined value.
292  * @param m
293  *   Mbuf to update
294  * @param new_value
295  *   Value set
296  */
297 static inline void
298 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
299 {
300         rte_atomic16_set(&m->refcnt_atomic, new_value);
301 }
302
303 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
304
305 /**
306  * Adds given value to an mbuf's refcnt and returns its new value.
307  */
308 static inline uint16_t
309 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
310 {
311         m->refcnt = (uint16_t)(m->refcnt + value);
312         return m->refcnt;
313 }
314
315 /**
316  * Reads the value of an mbuf's refcnt.
317  */
318 static inline uint16_t
319 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
320 {
321         return m->refcnt;
322 }
323
324 /**
325  * Sets an mbuf's refcnt to the defined value.
326  */
327 static inline void
328 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
329 {
330         m->refcnt = new_value;
331 }
332
333 #endif /* RTE_MBUF_REFCNT_ATOMIC */
334
335 /** Mbuf prefetch */
336 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
337         if ((m) != NULL)                        \
338                 rte_prefetch0(m);               \
339 } while (0)
340
341 #else /* ! RTE_MBUF_REFCNT */
342
343 /** Mbuf prefetch */
344 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
345
346 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
347
348 #endif /* RTE_MBUF_REFCNT */
349
350
351 /**
352  * Sanity checks on an mbuf.
353  *
354  * Check the consistency of the given mbuf. The function will cause a
355  * panic if corruption is detected.
356  *
357  * @param m
358  *   The mbuf to be checked.
359  * @param is_header
360  *   True if the mbuf is a packet header, false if it is a sub-segment
361  *   of a packet (in this case, some fields like nb_segs are not checked)
362  */
363 void
364 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
365
366 /**
367  * @internal Allocate a new mbuf from mempool *mp*.
368  * The use of that function is reserved for RTE internal needs.
369  * Please use rte_pktmbuf_alloc().
370  *
371  * @param mp
372  *   The mempool from which mbuf is allocated.
373  * @return
374  *   - The pointer to the new mbuf on success.
375  *   - NULL if allocation failed.
376  */
377 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
378 {
379         struct rte_mbuf *m;
380         void *mb = NULL;
381         if (rte_mempool_get(mp, &mb) < 0)
382                 return NULL;
383         m = (struct rte_mbuf *)mb;
384 #ifdef RTE_MBUF_REFCNT
385         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
386         rte_mbuf_refcnt_set(m, 1);
387 #endif /* RTE_MBUF_REFCNT */
388         return (m);
389 }
390
391 /**
392  * @internal Put mbuf back into its original mempool.
393  * The use of that function is reserved for RTE internal needs.
394  * Please use rte_pktmbuf_free().
395  *
396  * @param m
397  *   The mbuf to be freed.
398  */
399 static inline void __attribute__((always_inline))
400 __rte_mbuf_raw_free(struct rte_mbuf *m)
401 {
402 #ifdef RTE_MBUF_REFCNT
403         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
404 #endif /* RTE_MBUF_REFCNT */
405         rte_mempool_put(m->pool, m);
406 }
407
408 /* Operations on ctrl mbuf */
409
410 /**
411  * The control mbuf constructor.
412  *
413  * This function initializes some fields in an mbuf structure that are
414  * not modified by the user once created (mbuf type, origin pool, buffer
415  * start address, and so on). This function is given as a callback function
416  * to rte_mempool_create() at pool creation time.
417  *
418  * @param mp
419  *   The mempool from which the mbuf is allocated.
420  * @param opaque_arg
421  *   A pointer that can be used by the user to retrieve useful information
422  *   for mbuf initialization. This pointer comes from the ``init_arg``
423  *   parameter of rte_mempool_create().
424  * @param m
425  *   The mbuf to initialize.
426  * @param i
427  *   The index of the mbuf in the pool table.
428  */
429 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
430                 void *m, unsigned i);
431
432 /**
433  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
434  *
435  * This new mbuf is initialized with data pointing to the beginning of
436  * buffer, and with a length of zero.
437  *
438  * @param mp
439  *   The mempool from which the mbuf is allocated.
440  * @return
441  *   - The pointer to the new mbuf on success.
442  *   - NULL if allocation failed.
443  */
444 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
445
446 /**
447  * Free a control mbuf back into its original mempool.
448  *
449  * @param m
450  *   The control mbuf to be freed.
451  */
452 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
453
454 /**
455  * A macro that returns the pointer to the carried data.
456  *
457  * The value that can be read or assigned.
458  *
459  * @param m
460  *   The control mbuf.
461  */
462 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
463
464 /**
465  * A macro that returns the length of the carried data.
466  *
467  * The value that can be read or assigned.
468  *
469  * @param m
470  *   The control mbuf.
471  */
472 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
473
474 /* Operations on pkt mbuf */
475
476 /**
477  * The packet mbuf constructor.
478  *
479  * This function initializes some fields in the mbuf structure that are
480  * not modified by the user once created (origin pool, buffer start
481  * address, and so on). This function is given as a callback function to
482  * rte_mempool_create() at pool creation time.
483  *
484  * @param mp
485  *   The mempool from which mbufs originate.
486  * @param opaque_arg
487  *   A pointer that can be used by the user to retrieve useful information
488  *   for mbuf initialization. This pointer comes from the ``init_arg``
489  *   parameter of rte_mempool_create().
490  * @param m
491  *   The mbuf to initialize.
492  * @param i
493  *   The index of the mbuf in the pool table.
494  */
495 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
496                       void *m, unsigned i);
497
498
499 /**
500  * A  packet mbuf pool constructor.
501  *
502  * This function initializes the mempool private data in the case of a
503  * pktmbuf pool. This private data is needed by the driver. The
504  * function is given as a callback function to rte_mempool_create() at
505  * pool creation. It can be extended by the user, for example, to
506  * provide another packet size.
507  *
508  * @param mp
509  *   The mempool from which mbufs originate.
510  * @param opaque_arg
511  *   A pointer that can be used by the user to retrieve useful information
512  *   for mbuf initialization. This pointer comes from the ``init_arg``
513  *   parameter of rte_mempool_create().
514  */
515 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
516
517 /**
518  * Reset the fields of a packet mbuf to their default values.
519  *
520  * The given mbuf must have only one segment.
521  *
522  * @param m
523  *   The packet mbuf to be resetted.
524  */
525 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
526 {
527         m->next = NULL;
528         m->pkt_len = 0;
529         m->l2_l3_len = 0;
530         m->vlan_tci = 0;
531         m->nb_segs = 1;
532         m->port = 0xff;
533
534         m->ol_flags = 0;
535         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
536                         RTE_PKTMBUF_HEADROOM : m->buf_len;
537
538         m->data_len = 0;
539         __rte_mbuf_sanity_check(m, 1);
540 }
541
542 /**
543  * Allocate a new mbuf from a mempool.
544  *
545  * This new mbuf contains one segment, which has a length of 0. The pointer
546  * to data is initialized to have some bytes of headroom in the buffer
547  * (if buffer size allows).
548  *
549  * @param mp
550  *   The mempool from which the mbuf is allocated.
551  * @return
552  *   - The pointer to the new mbuf on success.
553  *   - NULL if allocation failed.
554  */
555 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
556 {
557         struct rte_mbuf *m;
558         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
559                 rte_pktmbuf_reset(m);
560         return (m);
561 }
562
563 #ifdef RTE_MBUF_REFCNT
564
565 /**
566  * Attach packet mbuf to another packet mbuf.
567  * After attachment we refer the mbuf we attached as 'indirect',
568  * while mbuf we attached to as 'direct'.
569  * Right now, not supported:
570  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
571  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
572  *  - mbuf we trying to attach (mi) is used by someone else
573  *    e.g. it's reference counter is greater then 1.
574  *
575  * @param mi
576  *   The indirect packet mbuf.
577  * @param md
578  *   The direct packet mbuf.
579  */
580
581 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
582 {
583         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
584             RTE_MBUF_DIRECT(mi) &&
585             rte_mbuf_refcnt_read(mi) == 1);
586
587         rte_mbuf_refcnt_update(md, 1);
588         mi->buf_physaddr = md->buf_physaddr;
589         mi->buf_addr = md->buf_addr;
590         mi->buf_len = md->buf_len;
591
592         mi->next = md->next;
593         mi->data_off = md->data_off;
594         mi->data_len = md->data_len;
595         mi->port = md->port;
596         mi->vlan_tci = md->vlan_tci;
597         mi->l2_l3_len = md->l2_l3_len;
598         mi->hash = md->hash;
599
600         mi->next = NULL;
601         mi->pkt_len = mi->data_len;
602         mi->nb_segs = 1;
603         mi->ol_flags = md->ol_flags;
604
605         __rte_mbuf_sanity_check(mi, 1);
606         __rte_mbuf_sanity_check(md, 0);
607 }
608
609 /**
610  * Detach an indirect packet mbuf -
611  *  - restore original mbuf address and length values.
612  *  - reset pktmbuf data and data_len to their default values.
613  *  All other fields of the given packet mbuf will be left intact.
614  *
615  * @param m
616  *   The indirect attached packet mbuf.
617  */
618
619 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
620 {
621         const struct rte_mempool *mp = m->pool;
622         void *buf = RTE_MBUF_TO_BADDR(m);
623         uint32_t buf_len = mp->elt_size - sizeof(*m);
624         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
625
626         m->buf_addr = buf;
627         m->buf_len = (uint16_t)buf_len;
628
629         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
630                         RTE_PKTMBUF_HEADROOM : m->buf_len;
631
632         m->data_len = 0;
633 }
634
635 #endif /* RTE_MBUF_REFCNT */
636
637
638 static inline struct rte_mbuf* __attribute__((always_inline))
639 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
640 {
641         __rte_mbuf_sanity_check(m, 0);
642
643 #ifdef RTE_MBUF_REFCNT
644         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
645                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
646                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
647
648                 rte_mbuf_refcnt_set(m, 0);
649
650                 /* if this is an indirect mbuf, then
651                  *  - detach mbuf
652                  *  - free attached mbuf segment
653                  */
654                 if (unlikely (md != m)) {
655                         rte_pktmbuf_detach(m);
656                         if (rte_mbuf_refcnt_update(md, -1) == 0)
657                                 __rte_mbuf_raw_free(md);
658                 }
659 #endif
660                 return(m);
661 #ifdef RTE_MBUF_REFCNT
662         }
663         return (NULL);
664 #endif
665 }
666
667 /**
668  * Free a segment of a packet mbuf into its original mempool.
669  *
670  * Free an mbuf, without parsing other segments in case of chained
671  * buffers.
672  *
673  * @param m
674  *   The packet mbuf segment to be freed.
675  */
676 static inline void __attribute__((always_inline))
677 rte_pktmbuf_free_seg(struct rte_mbuf *m)
678 {
679         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
680                 __rte_mbuf_raw_free(m);
681 }
682
683 /**
684  * Free a packet mbuf back into its original mempool.
685  *
686  * Free an mbuf, and all its segments in case of chained buffers. Each
687  * segment is added back into its original mempool.
688  *
689  * @param m
690  *   The packet mbuf to be freed.
691  */
692 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
693 {
694         struct rte_mbuf *m_next;
695
696         __rte_mbuf_sanity_check(m, 1);
697
698         while (m != NULL) {
699                 m_next = m->next;
700                 rte_pktmbuf_free_seg(m);
701                 m = m_next;
702         }
703 }
704
705 #ifdef RTE_MBUF_REFCNT
706
707 /**
708  * Creates a "clone" of the given packet mbuf.
709  *
710  * Walks through all segments of the given packet mbuf, and for each of them:
711  *  - Creates a new packet mbuf from the given pool.
712  *  - Attaches newly created mbuf to the segment.
713  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
714  * from the original packet mbuf.
715  *
716  * @param md
717  *   The packet mbuf to be cloned.
718  * @param mp
719  *   The mempool from which the "clone" mbufs are allocated.
720  * @return
721  *   - The pointer to the new "clone" mbuf on success.
722  *   - NULL if allocation fails.
723  */
724 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
725                 struct rte_mempool *mp)
726 {
727         struct rte_mbuf *mc, *mi, **prev;
728         uint32_t pktlen;
729         uint8_t nseg;
730
731         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
732                 return (NULL);
733
734         mi = mc;
735         prev = &mi->next;
736         pktlen = md->pkt_len;
737         nseg = 0;
738
739         do {
740                 nseg++;
741                 rte_pktmbuf_attach(mi, md);
742                 *prev = mi;
743                 prev = &mi->next;
744         } while ((md = md->next) != NULL &&
745             (mi = rte_pktmbuf_alloc(mp)) != NULL);
746
747         *prev = NULL;
748         mc->nb_segs = nseg;
749         mc->pkt_len = pktlen;
750
751         /* Allocation of new indirect segment failed */
752         if (unlikely (mi == NULL)) {
753                 rte_pktmbuf_free(mc);
754                 return (NULL);
755         }
756
757         __rte_mbuf_sanity_check(mc, 1);
758         return (mc);
759 }
760
761 /**
762  * Adds given value to the refcnt of all packet mbuf segments.
763  *
764  * Walks through all segments of given packet mbuf and for each of them
765  * invokes rte_mbuf_refcnt_update().
766  *
767  * @param m
768  *   The packet mbuf whose refcnt to be updated.
769  * @param v
770  *   The value to add to the mbuf's segments refcnt.
771  */
772 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
773 {
774         __rte_mbuf_sanity_check(m, 1);
775
776         do {
777                 rte_mbuf_refcnt_update(m, v);
778         } while ((m = m->next) != NULL);
779 }
780
781 #endif /* RTE_MBUF_REFCNT */
782
783 /**
784  * Get the headroom in a packet mbuf.
785  *
786  * @param m
787  *   The packet mbuf.
788  * @return
789  *   The length of the headroom.
790  */
791 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
792 {
793         __rte_mbuf_sanity_check(m, 1);
794         return m->data_off;
795 }
796
797 /**
798  * Get the tailroom of a packet mbuf.
799  *
800  * @param m
801  *   The packet mbuf.
802  * @return
803  *   The length of the tailroom.
804  */
805 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
806 {
807         __rte_mbuf_sanity_check(m, 1);
808         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
809                           m->data_len);
810 }
811
812 /**
813  * Get the last segment of the packet.
814  *
815  * @param m
816  *   The packet mbuf.
817  * @return
818  *   The last segment of the given mbuf.
819  */
820 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
821 {
822         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
823
824         __rte_mbuf_sanity_check(m, 1);
825         while (m2->next != NULL)
826                 m2 = m2->next;
827         return m2;
828 }
829
830 /**
831  * A macro that points to the start of the data in the mbuf.
832  *
833  * The returned pointer is cast to type t. Before using this
834  * function, the user must ensure that m_headlen(m) is large enough to
835  * read its data.
836  *
837  * @param m
838  *   The packet mbuf.
839  * @param t
840  *   The type to cast the result into.
841  */
842 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
843
844 /**
845  * A macro that returns the length of the packet.
846  *
847  * The value can be read or assigned.
848  *
849  * @param m
850  *   The packet mbuf.
851  */
852 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
853
854 /**
855  * A macro that returns the length of the segment.
856  *
857  * The value can be read or assigned.
858  *
859  * @param m
860  *   The packet mbuf.
861  */
862 #define rte_pktmbuf_data_len(m) ((m)->data_len)
863
864 /**
865  * Prepend len bytes to an mbuf data area.
866  *
867  * Returns a pointer to the new
868  * data start address. If there is not enough headroom in the first
869  * segment, the function will return NULL, without modifying the mbuf.
870  *
871  * @param m
872  *   The pkt mbuf.
873  * @param len
874  *   The amount of data to prepend (in bytes).
875  * @return
876  *   A pointer to the start of the newly prepended data, or
877  *   NULL if there is not enough headroom space in the first segment
878  */
879 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
880                                         uint16_t len)
881 {
882         __rte_mbuf_sanity_check(m, 1);
883
884         if (unlikely(len > rte_pktmbuf_headroom(m)))
885                 return NULL;
886
887         m->data_off -= len;
888         m->data_len = (uint16_t)(m->data_len + len);
889         m->pkt_len  = (m->pkt_len + len);
890
891         return (char *)m->buf_addr + m->data_off;
892 }
893
894 /**
895  * Append len bytes to an mbuf.
896  *
897  * Append len bytes to an mbuf and return a pointer to the start address
898  * of the added data. If there is not enough tailroom in the last
899  * segment, the function will return NULL, without modifying the mbuf.
900  *
901  * @param m
902  *   The packet mbuf.
903  * @param len
904  *   The amount of data to append (in bytes).
905  * @return
906  *   A pointer to the start of the newly appended data, or
907  *   NULL if there is not enough tailroom space in the last segment
908  */
909 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
910 {
911         void *tail;
912         struct rte_mbuf *m_last;
913
914         __rte_mbuf_sanity_check(m, 1);
915
916         m_last = rte_pktmbuf_lastseg(m);
917         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
918                 return NULL;
919
920         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
921         m_last->data_len = (uint16_t)(m_last->data_len + len);
922         m->pkt_len  = (m->pkt_len + len);
923         return (char*) tail;
924 }
925
926 /**
927  * Remove len bytes at the beginning of an mbuf.
928  *
929  * Returns a pointer to the start address of the new data area. If the
930  * length is greater than the length of the first segment, then the
931  * function will fail and return NULL, without modifying the mbuf.
932  *
933  * @param m
934  *   The packet mbuf.
935  * @param len
936  *   The amount of data to remove (in bytes).
937  * @return
938  *   A pointer to the new start of the data.
939  */
940 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
941 {
942         __rte_mbuf_sanity_check(m, 1);
943
944         if (unlikely(len > m->data_len))
945                 return NULL;
946
947         m->data_len = (uint16_t)(m->data_len - len);
948         m->data_off += len;
949         m->pkt_len  = (m->pkt_len - len);
950         return (char *)m->buf_addr + m->data_off;
951 }
952
953 /**
954  * Remove len bytes of data at the end of the mbuf.
955  *
956  * If the length is greater than the length of the last segment, the
957  * function will fail and return -1 without modifying the mbuf.
958  *
959  * @param m
960  *   The packet mbuf.
961  * @param len
962  *   The amount of data to remove (in bytes).
963  * @return
964  *   - 0: On success.
965  *   - -1: On error.
966  */
967 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
968 {
969         struct rte_mbuf *m_last;
970
971         __rte_mbuf_sanity_check(m, 1);
972
973         m_last = rte_pktmbuf_lastseg(m);
974         if (unlikely(len > m_last->data_len))
975                 return -1;
976
977         m_last->data_len = (uint16_t)(m_last->data_len - len);
978         m->pkt_len  = (m->pkt_len - len);
979         return 0;
980 }
981
982 /**
983  * Test if mbuf data is contiguous.
984  *
985  * @param m
986  *   The packet mbuf.
987  * @return
988  *   - 1, if all data is contiguous (one segment).
989  *   - 0, if there is several segments.
990  */
991 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
992 {
993         __rte_mbuf_sanity_check(m, 1);
994         return !!(m->nb_segs == 1);
995 }
996
997 /**
998  * Dump an mbuf structure to the console.
999  *
1000  * Dump all fields for the given packet mbuf and all its associated
1001  * segments (in the case of a chained buffer).
1002  *
1003  * @param f
1004  *   A pointer to a file for output
1005  * @param m
1006  *   The packet mbuf.
1007  * @param dump_len
1008  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1009  *   the packet.
1010  */
1011 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1012
1013 #ifdef __cplusplus
1014 }
1015 #endif
1016
1017 #endif /* _RTE_MBUF_H_ */