mbuf: add named points inside the structure
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_MBUF_H_
35 #define _RTE_MBUF_H_
36
37 /**
38  * @file
39  * RTE Mbuf
40  *
41  * The mbuf library provides the ability to create and destroy buffers
42  * that may be used by the RTE application to store message
43  * buffers. The message buffers are stored in a mempool, using the
44  * RTE mempool library.
45  *
46  * This library provide an API to allocate/free packet mbufs, which are
47  * used to carry network packets.
48  *
49  * To understand the concepts of packet buffers or mbufs, you
50  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
51  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
52  * http://www.kohala.com/start/tcpipiv2.html
53  */
54
55 #include <stdint.h>
56 #include <rte_mempool.h>
57 #include <rte_atomic.h>
58 #include <rte_prefetch.h>
59 #include <rte_branch_prediction.h>
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
66 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
67
68 /*
69  * Packet Offload Features Flags. It also carry packet type information.
70  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
71  */
72 #define PKT_RX_VLAN_PKT      0x0001 /**< RX packet is a 802.1q VLAN packet. */
73 #define PKT_RX_RSS_HASH      0x0002 /**< RX packet with RSS hash result. */
74 #define PKT_RX_FDIR          0x0004 /**< RX packet with FDIR infos. */
75 #define PKT_RX_L4_CKSUM_BAD  0x0008 /**< L4 cksum of RX pkt. is not OK. */
76 #define PKT_RX_IP_CKSUM_BAD  0x0010 /**< IP cksum of RX pkt. is not OK. */
77 #define PKT_RX_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
78 #define PKT_RX_OVERSIZE      0x0000 /**< Num of desc of an RX pkt oversize. */
79 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
80 #define PKT_RX_RECIP_ERR     0x0000 /**< Hardware processing error. */
81 #define PKT_RX_MAC_ERR       0x0000 /**< MAC error. */
82 #define PKT_RX_IPV4_HDR      0x0020 /**< RX packet with IPv4 header. */
83 #define PKT_RX_IPV4_HDR_EXT  0x0040 /**< RX packet with extended IPv4 header. */
84 #define PKT_RX_IPV6_HDR      0x0080 /**< RX packet with IPv6 header. */
85 #define PKT_RX_IPV6_HDR_EXT  0x0100 /**< RX packet with extended IPv6 header. */
86 #define PKT_RX_IEEE1588_PTP  0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
87 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
88
89 #define PKT_TX_VLAN_PKT      0x0800 /**< TX packet is a 802.1q VLAN packet. */
90 #define PKT_TX_IP_CKSUM      0x1000 /**< IP cksum of TX pkt. computed by NIC. */
91 #define PKT_TX_IPV4_CSUM     0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
92 #define PKT_TX_IPV4          PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
93 #define PKT_TX_IPV6          PKT_RX_IPV6_HDR /**< IPv6 packet */
94
95 /*
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         /* fields only used in slow path or on TX */
180         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
181         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
182
183 } __rte_cache_aligned;
184
185 /**
186  * Given the buf_addr returns the pointer to corresponding mbuf.
187  */
188 #define RTE_MBUF_FROM_BADDR(ba)     (((struct rte_mbuf *)(ba)) - 1)
189
190 /**
191  * Given the pointer to mbuf returns an address where it's  buf_addr
192  * should point to.
193  */
194 #define RTE_MBUF_TO_BADDR(mb)       (((struct rte_mbuf *)(mb)) + 1)
195
196 /**
197  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
198  */
199 #define RTE_MBUF_INDIRECT(mb)   (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
200
201 /**
202  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
203  */
204 #define RTE_MBUF_DIRECT(mb)     (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
205
206
207 /**
208  * Private data in case of pktmbuf pool.
209  *
210  * A structure that contains some pktmbuf_pool-specific data that are
211  * appended after the mempool structure (in private data).
212  */
213 struct rte_pktmbuf_pool_private {
214         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
215 };
216
217 #ifdef RTE_LIBRTE_MBUF_DEBUG
218
219 /**  check mbuf type in debug mode */
220 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
221
222 /**  check mbuf type in debug mode if mbuf pointer is not null */
223 #define __rte_mbuf_sanity_check_raw(m, is_h)    do {       \
224         if ((m) != NULL)                                   \
225                 rte_mbuf_sanity_check(m, is_h);          \
226 } while (0)
227
228 /**  MBUF asserts in debug mode */
229 #define RTE_MBUF_ASSERT(exp)                                         \
230 if (!(exp)) {                                                        \
231         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
232 }
233
234 #else /*  RTE_LIBRTE_MBUF_DEBUG */
235
236 /**  check mbuf type in debug mode */
237 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
238
239 /**  check mbuf type in debug mode if mbuf pointer is not null */
240 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
241
242 /**  MBUF asserts in debug mode */
243 #define RTE_MBUF_ASSERT(exp)                do { } while (0)
244
245 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
246
247 #ifdef RTE_MBUF_REFCNT
248 #ifdef RTE_MBUF_REFCNT_ATOMIC
249
250 /**
251  * Adds given value to an mbuf's refcnt and returns its new value.
252  * @param m
253  *   Mbuf to update
254  * @param value
255  *   Value to add/subtract
256  * @return
257  *   Updated value
258  */
259 static inline uint16_t
260 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
261 {
262         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
263 }
264
265 /**
266  * Reads the value of an mbuf's refcnt.
267  * @param m
268  *   Mbuf to read
269  * @return
270  *   Reference count number.
271  */
272 static inline uint16_t
273 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
274 {
275         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
276 }
277
278 /**
279  * Sets an mbuf's refcnt to a defined value.
280  * @param m
281  *   Mbuf to update
282  * @param new_value
283  *   Value set
284  */
285 static inline void
286 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
287 {
288         rte_atomic16_set(&m->refcnt_atomic, new_value);
289 }
290
291 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
292
293 /**
294  * Adds given value to an mbuf's refcnt and returns its new value.
295  */
296 static inline uint16_t
297 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
298 {
299         m->refcnt = (uint16_t)(m->refcnt + value);
300         return m->refcnt;
301 }
302
303 /**
304  * Reads the value of an mbuf's refcnt.
305  */
306 static inline uint16_t
307 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
308 {
309         return m->refcnt;
310 }
311
312 /**
313  * Sets an mbuf's refcnt to the defined value.
314  */
315 static inline void
316 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
317 {
318         m->refcnt = new_value;
319 }
320
321 #endif /* RTE_MBUF_REFCNT_ATOMIC */
322
323 /** Mbuf prefetch */
324 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
325         if ((m) != NULL)                        \
326                 rte_prefetch0(m);               \
327 } while (0)
328
329 #else /* ! RTE_MBUF_REFCNT */
330
331 /** Mbuf prefetch */
332 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
333
334 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
335
336 #endif /* RTE_MBUF_REFCNT */
337
338
339 /**
340  * Sanity checks on an mbuf.
341  *
342  * Check the consistency of the given mbuf. The function will cause a
343  * panic if corruption is detected.
344  *
345  * @param m
346  *   The mbuf to be checked.
347  * @param is_header
348  *   True if the mbuf is a packet header, false if it is a sub-segment
349  *   of a packet (in this case, some fields like nb_segs are not checked)
350  */
351 void
352 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
353
354 /**
355  * @internal Allocate a new mbuf from mempool *mp*.
356  * The use of that function is reserved for RTE internal needs.
357  * Please use rte_pktmbuf_alloc().
358  *
359  * @param mp
360  *   The mempool from which mbuf is allocated.
361  * @return
362  *   - The pointer to the new mbuf on success.
363  *   - NULL if allocation failed.
364  */
365 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
366 {
367         struct rte_mbuf *m;
368         void *mb = NULL;
369         if (rte_mempool_get(mp, &mb) < 0)
370                 return NULL;
371         m = (struct rte_mbuf *)mb;
372 #ifdef RTE_MBUF_REFCNT
373         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
374         rte_mbuf_refcnt_set(m, 1);
375 #endif /* RTE_MBUF_REFCNT */
376         return (m);
377 }
378
379 /**
380  * @internal Put mbuf back into its original mempool.
381  * The use of that function is reserved for RTE internal needs.
382  * Please use rte_pktmbuf_free().
383  *
384  * @param m
385  *   The mbuf to be freed.
386  */
387 static inline void __attribute__((always_inline))
388 __rte_mbuf_raw_free(struct rte_mbuf *m)
389 {
390 #ifdef RTE_MBUF_REFCNT
391         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
392 #endif /* RTE_MBUF_REFCNT */
393         rte_mempool_put(m->pool, m);
394 }
395
396 /* Operations on ctrl mbuf */
397
398 /**
399  * The control mbuf constructor.
400  *
401  * This function initializes some fields in an mbuf structure that are
402  * not modified by the user once created (mbuf type, origin pool, buffer
403  * start address, and so on). This function is given as a callback function
404  * to rte_mempool_create() at pool creation time.
405  *
406  * @param mp
407  *   The mempool from which the mbuf is allocated.
408  * @param opaque_arg
409  *   A pointer that can be used by the user to retrieve useful information
410  *   for mbuf initialization. This pointer comes from the ``init_arg``
411  *   parameter of rte_mempool_create().
412  * @param m
413  *   The mbuf to initialize.
414  * @param i
415  *   The index of the mbuf in the pool table.
416  */
417 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
418                 void *m, unsigned i);
419
420 /**
421  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
422  *
423  * This new mbuf is initialized with data pointing to the beginning of
424  * buffer, and with a length of zero.
425  *
426  * @param mp
427  *   The mempool from which the mbuf is allocated.
428  * @return
429  *   - The pointer to the new mbuf on success.
430  *   - NULL if allocation failed.
431  */
432 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
433
434 /**
435  * Free a control mbuf back into its original mempool.
436  *
437  * @param m
438  *   The control mbuf to be freed.
439  */
440 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
441
442 /**
443  * A macro that returns the pointer to the carried data.
444  *
445  * The value that can be read or assigned.
446  *
447  * @param m
448  *   The control mbuf.
449  */
450 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
451
452 /**
453  * A macro that returns the length of the carried data.
454  *
455  * The value that can be read or assigned.
456  *
457  * @param m
458  *   The control mbuf.
459  */
460 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
461
462 /**
463  * Tests if an mbuf is a control mbuf
464  *
465  * @param m
466  *   The mbuf to be tested
467  * @return
468  *   - True (1) if the mbuf is a control mbuf
469  *   - False(0) otherwise
470  */
471 static inline int
472 rte_is_ctrlmbuf(struct rte_mbuf *m)
473 {
474         return (!!(m->ol_flags & CTRL_MBUF_FLAG));
475 }
476
477 /* Operations on pkt mbuf */
478
479 /**
480  * The packet mbuf constructor.
481  *
482  * This function initializes some fields in the mbuf structure that are
483  * not modified by the user once created (origin pool, buffer start
484  * address, and so on). This function is given as a callback function to
485  * rte_mempool_create() at pool creation time.
486  *
487  * @param mp
488  *   The mempool from which mbufs originate.
489  * @param opaque_arg
490  *   A pointer that can be used by the user to retrieve useful information
491  *   for mbuf initialization. This pointer comes from the ``init_arg``
492  *   parameter of rte_mempool_create().
493  * @param m
494  *   The mbuf to initialize.
495  * @param i
496  *   The index of the mbuf in the pool table.
497  */
498 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
499                       void *m, unsigned i);
500
501
502 /**
503  * A  packet mbuf pool constructor.
504  *
505  * This function initializes the mempool private data in the case of a
506  * pktmbuf pool. This private data is needed by the driver. The
507  * function is given as a callback function to rte_mempool_create() at
508  * pool creation. It can be extended by the user, for example, to
509  * provide another packet size.
510  *
511  * @param mp
512  *   The mempool from which mbufs originate.
513  * @param opaque_arg
514  *   A pointer that can be used by the user to retrieve useful information
515  *   for mbuf initialization. This pointer comes from the ``init_arg``
516  *   parameter of rte_mempool_create().
517  */
518 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
519
520 /**
521  * Reset the fields of a packet mbuf to their default values.
522  *
523  * The given mbuf must have only one segment.
524  *
525  * @param m
526  *   The packet mbuf to be resetted.
527  */
528 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
529 {
530         m->next = NULL;
531         m->pkt_len = 0;
532         m->l2_l3_len = 0;
533         m->vlan_tci = 0;
534         m->nb_segs = 1;
535         m->port = 0xff;
536
537         m->ol_flags = 0;
538         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
539                         RTE_PKTMBUF_HEADROOM : m->buf_len;
540
541         m->data_len = 0;
542         __rte_mbuf_sanity_check(m, 1);
543 }
544
545 /**
546  * Allocate a new mbuf from a mempool.
547  *
548  * This new mbuf contains one segment, which has a length of 0. The pointer
549  * to data is initialized to have some bytes of headroom in the buffer
550  * (if buffer size allows).
551  *
552  * @param mp
553  *   The mempool from which the mbuf is allocated.
554  * @return
555  *   - The pointer to the new mbuf on success.
556  *   - NULL if allocation failed.
557  */
558 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
559 {
560         struct rte_mbuf *m;
561         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
562                 rte_pktmbuf_reset(m);
563         return (m);
564 }
565
566 #ifdef RTE_MBUF_REFCNT
567
568 /**
569  * Attach packet mbuf to another packet mbuf.
570  * After attachment we refer the mbuf we attached as 'indirect',
571  * while mbuf we attached to as 'direct'.
572  * Right now, not supported:
573  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
574  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
575  *  - mbuf we trying to attach (mi) is used by someone else
576  *    e.g. it's reference counter is greater then 1.
577  *
578  * @param mi
579  *   The indirect packet mbuf.
580  * @param md
581  *   The direct packet mbuf.
582  */
583
584 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
585 {
586         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
587             RTE_MBUF_DIRECT(mi) &&
588             rte_mbuf_refcnt_read(mi) == 1);
589
590         rte_mbuf_refcnt_update(md, 1);
591         mi->buf_physaddr = md->buf_physaddr;
592         mi->buf_addr = md->buf_addr;
593         mi->buf_len = md->buf_len;
594
595         mi->next = md->next;
596         mi->data_off = md->data_off;
597         mi->data_len = md->data_len;
598         mi->port = md->port;
599         mi->vlan_tci = md->vlan_tci;
600         mi->l2_l3_len = md->l2_l3_len;
601         mi->hash = md->hash;
602
603         mi->next = NULL;
604         mi->pkt_len = mi->data_len;
605         mi->nb_segs = 1;
606         mi->ol_flags = md->ol_flags;
607
608         __rte_mbuf_sanity_check(mi, 1);
609         __rte_mbuf_sanity_check(md, 0);
610 }
611
612 /**
613  * Detach an indirect packet mbuf -
614  *  - restore original mbuf address and length values.
615  *  - reset pktmbuf data and data_len to their default values.
616  *  All other fields of the given packet mbuf will be left intact.
617  *
618  * @param m
619  *   The indirect attached packet mbuf.
620  */
621
622 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
623 {
624         const struct rte_mempool *mp = m->pool;
625         void *buf = RTE_MBUF_TO_BADDR(m);
626         uint32_t buf_len = mp->elt_size - sizeof(*m);
627         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
628
629         m->buf_addr = buf;
630         m->buf_len = (uint16_t)buf_len;
631
632         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
633                         RTE_PKTMBUF_HEADROOM : m->buf_len;
634
635         m->data_len = 0;
636 }
637
638 #endif /* RTE_MBUF_REFCNT */
639
640
641 static inline struct rte_mbuf* __attribute__((always_inline))
642 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
643 {
644         __rte_mbuf_sanity_check(m, 0);
645
646 #ifdef RTE_MBUF_REFCNT
647         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
648                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
649                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
650
651                 rte_mbuf_refcnt_set(m, 0);
652
653                 /* if this is an indirect mbuf, then
654                  *  - detach mbuf
655                  *  - free attached mbuf segment
656                  */
657                 if (unlikely (md != m)) {
658                         rte_pktmbuf_detach(m);
659                         if (rte_mbuf_refcnt_update(md, -1) == 0)
660                                 __rte_mbuf_raw_free(md);
661                 }
662 #endif
663                 return(m);
664 #ifdef RTE_MBUF_REFCNT
665         }
666         return (NULL);
667 #endif
668 }
669
670 /**
671  * Free a segment of a packet mbuf into its original mempool.
672  *
673  * Free an mbuf, without parsing other segments in case of chained
674  * buffers.
675  *
676  * @param m
677  *   The packet mbuf segment to be freed.
678  */
679 static inline void __attribute__((always_inline))
680 rte_pktmbuf_free_seg(struct rte_mbuf *m)
681 {
682         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
683                 __rte_mbuf_raw_free(m);
684 }
685
686 /**
687  * Free a packet mbuf back into its original mempool.
688  *
689  * Free an mbuf, and all its segments in case of chained buffers. Each
690  * segment is added back into its original mempool.
691  *
692  * @param m
693  *   The packet mbuf to be freed.
694  */
695 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
696 {
697         struct rte_mbuf *m_next;
698
699         __rte_mbuf_sanity_check(m, 1);
700
701         while (m != NULL) {
702                 m_next = m->next;
703                 rte_pktmbuf_free_seg(m);
704                 m = m_next;
705         }
706 }
707
708 #ifdef RTE_MBUF_REFCNT
709
710 /**
711  * Creates a "clone" of the given packet mbuf.
712  *
713  * Walks through all segments of the given packet mbuf, and for each of them:
714  *  - Creates a new packet mbuf from the given pool.
715  *  - Attaches newly created mbuf to the segment.
716  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
717  * from the original packet mbuf.
718  *
719  * @param md
720  *   The packet mbuf to be cloned.
721  * @param mp
722  *   The mempool from which the "clone" mbufs are allocated.
723  * @return
724  *   - The pointer to the new "clone" mbuf on success.
725  *   - NULL if allocation fails.
726  */
727 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
728                 struct rte_mempool *mp)
729 {
730         struct rte_mbuf *mc, *mi, **prev;
731         uint32_t pktlen;
732         uint8_t nseg;
733
734         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
735                 return (NULL);
736
737         mi = mc;
738         prev = &mi->next;
739         pktlen = md->pkt_len;
740         nseg = 0;
741
742         do {
743                 nseg++;
744                 rte_pktmbuf_attach(mi, md);
745                 *prev = mi;
746                 prev = &mi->next;
747         } while ((md = md->next) != NULL &&
748             (mi = rte_pktmbuf_alloc(mp)) != NULL);
749
750         *prev = NULL;
751         mc->nb_segs = nseg;
752         mc->pkt_len = pktlen;
753
754         /* Allocation of new indirect segment failed */
755         if (unlikely (mi == NULL)) {
756                 rte_pktmbuf_free(mc);
757                 return (NULL);
758         }
759
760         __rte_mbuf_sanity_check(mc, 1);
761         return (mc);
762 }
763
764 /**
765  * Adds given value to the refcnt of all packet mbuf segments.
766  *
767  * Walks through all segments of given packet mbuf and for each of them
768  * invokes rte_mbuf_refcnt_update().
769  *
770  * @param m
771  *   The packet mbuf whose refcnt to be updated.
772  * @param v
773  *   The value to add to the mbuf's segments refcnt.
774  */
775 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
776 {
777         __rte_mbuf_sanity_check(m, 1);
778
779         do {
780                 rte_mbuf_refcnt_update(m, v);
781         } while ((m = m->next) != NULL);
782 }
783
784 #endif /* RTE_MBUF_REFCNT */
785
786 /**
787  * Get the headroom in a packet mbuf.
788  *
789  * @param m
790  *   The packet mbuf.
791  * @return
792  *   The length of the headroom.
793  */
794 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
795 {
796         __rte_mbuf_sanity_check(m, 1);
797         return m->data_off;
798 }
799
800 /**
801  * Get the tailroom of a packet mbuf.
802  *
803  * @param m
804  *   The packet mbuf.
805  * @return
806  *   The length of the tailroom.
807  */
808 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
809 {
810         __rte_mbuf_sanity_check(m, 1);
811         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
812                           m->data_len);
813 }
814
815 /**
816  * Get the last segment of the packet.
817  *
818  * @param m
819  *   The packet mbuf.
820  * @return
821  *   The last segment of the given mbuf.
822  */
823 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
824 {
825         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
826
827         __rte_mbuf_sanity_check(m, 1);
828         while (m2->next != NULL)
829                 m2 = m2->next;
830         return m2;
831 }
832
833 /**
834  * A macro that points to the start of the data in the mbuf.
835  *
836  * The returned pointer is cast to type t. Before using this
837  * function, the user must ensure that m_headlen(m) is large enough to
838  * read its data.
839  *
840  * @param m
841  *   The packet mbuf.
842  * @param t
843  *   The type to cast the result into.
844  */
845 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
846
847 /**
848  * A macro that returns the length of the packet.
849  *
850  * The value can be read or assigned.
851  *
852  * @param m
853  *   The packet mbuf.
854  */
855 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
856
857 /**
858  * A macro that returns the length of the segment.
859  *
860  * The value can be read or assigned.
861  *
862  * @param m
863  *   The packet mbuf.
864  */
865 #define rte_pktmbuf_data_len(m) ((m)->data_len)
866
867 /**
868  * Prepend len bytes to an mbuf data area.
869  *
870  * Returns a pointer to the new
871  * data start address. If there is not enough headroom in the first
872  * segment, the function will return NULL, without modifying the mbuf.
873  *
874  * @param m
875  *   The pkt mbuf.
876  * @param len
877  *   The amount of data to prepend (in bytes).
878  * @return
879  *   A pointer to the start of the newly prepended data, or
880  *   NULL if there is not enough headroom space in the first segment
881  */
882 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
883                                         uint16_t len)
884 {
885         __rte_mbuf_sanity_check(m, 1);
886
887         if (unlikely(len > rte_pktmbuf_headroom(m)))
888                 return NULL;
889
890         m->data_off -= len;
891         m->data_len = (uint16_t)(m->data_len + len);
892         m->pkt_len  = (m->pkt_len + len);
893
894         return (char *)m->buf_addr + m->data_off;
895 }
896
897 /**
898  * Append len bytes to an mbuf.
899  *
900  * Append len bytes to an mbuf and return a pointer to the start address
901  * of the added data. If there is not enough tailroom in the last
902  * segment, the function will return NULL, without modifying the mbuf.
903  *
904  * @param m
905  *   The packet mbuf.
906  * @param len
907  *   The amount of data to append (in bytes).
908  * @return
909  *   A pointer to the start of the newly appended data, or
910  *   NULL if there is not enough tailroom space in the last segment
911  */
912 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
913 {
914         void *tail;
915         struct rte_mbuf *m_last;
916
917         __rte_mbuf_sanity_check(m, 1);
918
919         m_last = rte_pktmbuf_lastseg(m);
920         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
921                 return NULL;
922
923         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
924         m_last->data_len = (uint16_t)(m_last->data_len + len);
925         m->pkt_len  = (m->pkt_len + len);
926         return (char*) tail;
927 }
928
929 /**
930  * Remove len bytes at the beginning of an mbuf.
931  *
932  * Returns a pointer to the start address of the new data area. If the
933  * length is greater than the length of the first segment, then the
934  * function will fail and return NULL, without modifying the mbuf.
935  *
936  * @param m
937  *   The packet mbuf.
938  * @param len
939  *   The amount of data to remove (in bytes).
940  * @return
941  *   A pointer to the new start of the data.
942  */
943 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
944 {
945         __rte_mbuf_sanity_check(m, 1);
946
947         if (unlikely(len > m->data_len))
948                 return NULL;
949
950         m->data_len = (uint16_t)(m->data_len - len);
951         m->data_off += len;
952         m->pkt_len  = (m->pkt_len - len);
953         return (char *)m->buf_addr + m->data_off;
954 }
955
956 /**
957  * Remove len bytes of data at the end of the mbuf.
958  *
959  * If the length is greater than the length of the last segment, the
960  * function will fail and return -1 without modifying the mbuf.
961  *
962  * @param m
963  *   The packet mbuf.
964  * @param len
965  *   The amount of data to remove (in bytes).
966  * @return
967  *   - 0: On success.
968  *   - -1: On error.
969  */
970 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
971 {
972         struct rte_mbuf *m_last;
973
974         __rte_mbuf_sanity_check(m, 1);
975
976         m_last = rte_pktmbuf_lastseg(m);
977         if (unlikely(len > m_last->data_len))
978                 return -1;
979
980         m_last->data_len = (uint16_t)(m_last->data_len - len);
981         m->pkt_len  = (m->pkt_len - len);
982         return 0;
983 }
984
985 /**
986  * Test if mbuf data is contiguous.
987  *
988  * @param m
989  *   The packet mbuf.
990  * @return
991  *   - 1, if all data is contiguous (one segment).
992  *   - 0, if there is several segments.
993  */
994 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
995 {
996         __rte_mbuf_sanity_check(m, 1);
997         return !!(m->nb_segs == 1);
998 }
999
1000 /**
1001  * Dump an mbuf structure to the console.
1002  *
1003  * Dump all fields for the given packet mbuf and all its associated
1004  * segments (in the case of a chained buffer).
1005  *
1006  * @param f
1007  *   A pointer to a file for output
1008  * @param m
1009  *   The packet mbuf.
1010  * @param dump_len
1011  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1012  *   the packet.
1013  */
1014 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1015
1016 #ifdef __cplusplus
1017 }
1018 #endif
1019
1020 #endif /* _RTE_MBUF_H_ */