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