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