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