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