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