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