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