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