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