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