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