first public release
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_MBUF_H_
37 #define _RTE_MBUF_H_
38
39 /**
40  * @file
41  * RTE Mbuf
42  *
43  * The mbuf library provides the ability to create and destroy buffers
44  * that may be used by the RTE application to store message
45  * buffers. The message buffers are stored in a mempool, using the
46  * RTE mempool library.
47  *
48  * This library provide an API to allocate/free mbufs, manipulate
49  * control message buffer (ctrlmbuf), which are generic message
50  * buffers, and packet buffers (pktmbuf), which are used to carry
51  * network packets.
52  *
53  * To understand the concepts of packet buffers or mbufs, you
54  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
55  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
56  * http://www.kohala.com/start/tcpipiv2.html
57  *
58  * The main modification of this implementation is the use of mbuf for
59  * transports other than packets. mbufs can have other types.
60  */
61
62 #include <stdint.h>
63
64 #include <rte_mempool.h>
65 #include <rte_atomic.h>
66 #include <rte_prefetch.h>
67 #include <rte_branch_prediction.h>
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 /**
74  * A control message buffer.
75  */
76 struct rte_ctrlmbuf {
77         void *data;        /**< Pointer to data. */
78         uint32_t data_len; /**< Length of data. */
79 };
80
81
82 /*
83  * Packet Offload Features Flags. It also carry packet type information.
84  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
85  */
86 #define PKT_RX_VLAN_PKT      0x0001 /**< RX packet is a 802.1q VLAN packet. */
87 #define PKT_RX_RSS_HASH      0x0002 /**< RX packet with RSS hash result. */
88 #define PKT_RX_FDIR          0x0004 /**< RX packet with FDIR infos. */
89 #define PKT_RX_L4_CKSUM_BAD  0x0008 /**< L4 cksum of RX pkt. is not OK. */
90 #define PKT_RX_IP_CKSUM_BAD  0x0010 /**< IP cksum of RX pkt. is not OK. */
91 #define PKT_RX_IPV4_HDR      0x0020 /**< RX packet with IPv4 header. */
92 #define PKT_RX_IPV4_HDR_EXT  0x0040 /**< RX packet with extended IPv4 header. */
93 #define PKT_RX_IPV6_HDR      0x0080 /**< RX packet with IPv6 header. */
94 #define PKT_RX_IPV6_HDR_EXT  0x0100 /**< RX packet with extended IPv6 header. */
95 #define PKT_RX_IEEE1588_PTP  0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
96 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
97
98 #define PKT_TX_VLAN_PKT      0x0800 /**< TX packet is a 802.1q VLAN packet. */
99 #define PKT_TX_IP_CKSUM      0x1000 /**< IP cksum of TX pkt. computed by NIC. */
100 /*
101  * Bit 14~13 used for L4 packet type with checksum enabled.
102  *     00: Reserved
103  *     01: TCP checksum
104  *     10: SCTP checksum
105  *     11: UDP checksum
106  */
107 #define PKT_TX_L4_MASK       0x6000 /**< Mask bits for L4 checksum offload request. */
108 #define PKT_TX_L4_NO_CKSUM   0x0000 /**< Disable L4 cksum of TX pkt. */
109 #define PKT_TX_TCP_CKSUM     0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
110 #define PKT_TX_SCTP_CKSUM    0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
111 #define PKT_TX_UDP_CKSUM     0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
112 /* Bit 15 */
113 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
114
115 /**
116  * Bit Mask to indicate what bits required for building TX context
117  */
118 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
119
120 /* Compare mask for vlan_macip_lens, used for context build up */
121 #define TX_VLAN_CMP_MASK        0xFFFF0000  /**< VLAN length - 16-bits. */
122 #define TX_MAC_LEN_CMP_MASK     0x0000FE00  /**< MAC length - 7-bits. */
123 #define TX_IP_LEN_CMP_MASK      0x000001FF  /**< IP  length - 9-bits. */
124 /** MAC+IP length. */ 
125 #define TX_MACIP_LEN_CMP_MASK   (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK) 
126
127 /**
128  * A packet message buffer.
129  */
130 struct rte_pktmbuf {
131         /* valid for any segment */
132         struct rte_mbuf *next;  /**< Next segment of scattered packet. */
133         void* data;             /**< Start address of data in segment buffer. */
134         uint16_t data_len;      /**< Amount of data in segment buffer. */
135
136         /* these fields are valid for first segment only */
137         uint8_t nb_segs;        /**< Number of segments. */
138         uint8_t in_port;        /**< Input port. */
139         uint32_t pkt_len;       /**< Total pkt len: sum of all segment data_len. */
140
141         /* offload features */
142         uint16_t vlan_tci;      /**< VLAN Tag Control Identifier (CPU order). */
143         uint16_t l2_len:7;      /**< L2 (MAC) Header Length. */
144         uint16_t l3_len:9;      /**< L3 (IP) Header Length. */
145         union {
146                 uint32_t rss;       /**< RSS hash result if RSS enabled */
147                 struct {
148                         uint16_t hash;
149                         uint16_t id;
150                 } fdir;             /**< Filter identifier if FDIR enabled */
151         } hash;                 /**< hash information */
152 };
153
154 /**
155  * This enum indicates the mbuf type.
156  */
157 enum rte_mbuf_type {
158         RTE_MBUF_CTRL,  /**< Control mbuf. */
159         RTE_MBUF_PKT,   /**< Packet mbuf. */
160 };
161
162 /**
163  * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
164  */
165 struct rte_mbuf {
166         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
167         void *buf_addr;           /**< Virtual address of segment buffer. */
168         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
169         uint16_t buf_len;         /**< Length of segment buffer. */
170 #ifdef RTE_MBUF_SCATTER_GATHER
171         /**
172          * 16-bit Reference counter.
173          * It should only be accessed using the following functions:
174          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
175          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
176          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
177          * config option.
178          */
179         union {
180                 rte_atomic16_t refcnt_atomic;   /**< Atomically accessed refcnt */
181                 uint16_t refcnt;                /**< Non-atomically accessed refcnt */
182         };
183 #else
184         uint16_t refcnt_reserved;     /**< Do not use this field */
185 #endif
186         uint8_t type;                 /**< Type of mbuf. */
187         uint8_t reserved;             /**< Unused field. Required for padding. */
188         uint16_t ol_flags;            /**< Offload features. */
189
190         union {
191                 struct rte_ctrlmbuf ctrl;
192                 struct rte_pktmbuf pkt;
193         };
194 } __rte_cache_aligned;
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, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
232
233 /**  check mbuf type in debug mode if mbuf pointer is not null */
234 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do {       \
235         if ((m) != NULL)                                   \
236                 rte_mbuf_sanity_check(m, t, 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, t, 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, t, 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_SCATTER_GATHER
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_SCATTER_GATHER */
341
342 /** Mbuf prefetch */
343 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
344
345 #endif /* RTE_MBUF_SCATTER_GATHER */
346
347
348 /**
349  * Sanity checks on an mbuf.
350  *
351  * Check the consistency of the given mbuf. The function will cause a
352  * panic if corruption is detected.
353  *
354  * @param m
355  *   The mbuf to be checked.
356  * @param t
357  *   The expected type of the mbuf.
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, enum rte_mbuf_type t,
364                       int is_header);
365
366 /**
367  * @internal Allocate a new mbuf from mempool *mp*.
368  * The use of that function is reserved for RTE internal needs.
369  * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
370  *
371  * @param mp
372  *   The mempool from which mbuf is allocated.
373  * @return
374  *   - The pointer to the new mbuf on success.
375  *   - NULL if allocation failed.
376  */
377 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
378 {
379         struct rte_mbuf *m;
380         void *mb = NULL;
381         if (rte_mempool_get(mp, &mb) < 0)
382                 return NULL;
383         m = (struct rte_mbuf *)mb;
384 #ifdef RTE_MBUF_SCATTER_GATHER
385         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
386         rte_mbuf_refcnt_set(m, 1);
387 #endif /* RTE_MBUF_SCATTER_GATHER */
388         return (m);
389 }
390
391 /**
392  * @internal Put mbuf back into its original mempool.
393  * The use of that function is reserved for RTE internal needs.
394  * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
395  *
396  * @param m
397  *   The mbuf to be freed.
398  */
399 static inline void __rte_mbuf_raw_free(struct rte_mbuf *m)
400 {
401 #ifdef RTE_MBUF_SCATTER_GATHER
402         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
403 #endif /* RTE_MBUF_SCATTER_GATHER */
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 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
444 {
445         struct rte_mbuf *m;
446         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
447                 m->ctrl.data = m->buf_addr;
448                 m->ctrl.data_len = 0;
449                 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
450         }
451         return (m);
452 }
453
454 /**
455  * Free a control mbuf back into its original mempool.
456  *
457  * @param m
458  *   The control mbuf to be freed.
459  */
460 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
461 {
462         __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
463 #ifdef RTE_MBUF_SCATTER_GATHER
464         if (rte_mbuf_refcnt_update(m, -1) == 0)
465 #endif /* RTE_MBUF_SCATTER_GATHER */
466                 __rte_mbuf_raw_free(m);
467 }
468
469 /**
470  * A macro that returns the pointer to the carried data.
471  *
472  * The value that can be read or assigned.
473  *
474  * @param m
475  *   The control mbuf.
476  */
477 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
478
479 /**
480  * A macro that returns the length of the carried data.
481  *
482  * The value that can be read or assigned.
483  *
484  * @param m
485  *   The control mbuf.
486  */
487 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
488
489 /* Operations on pkt mbuf */
490
491 /**
492  * The packet mbuf constructor.
493  *
494  * This function initializes some fields in the mbuf structure that are not
495  * modified by the user once created (mbuf type, origin pool, buffer start
496  * address, and so on). This function is given as a callback function to
497  * rte_mempool_create() at pool creation time.
498  *
499  * @param mp
500  *   The mempool from which mbufs originate.
501  * @param opaque_arg
502  *   A pointer that can be used by the user to retrieve useful information
503  *   for mbuf initialization. This pointer comes from the ``init_arg``
504  *   parameter of rte_mempool_create().
505  * @param m
506  *   The mbuf to initialize.
507  * @param i
508  *   The index of the mbuf in the pool table.
509  */
510 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
511                       void *m, unsigned i);
512
513
514 /**
515  * A  packet mbuf pool constructor.
516  *
517  * This function initializes the mempool private data in the case of a
518  * pktmbuf pool. This private data is needed by the driver. The
519  * function is given as a callback function to rte_mempool_create() at
520  * pool creation. It can be extended by the user, for example, to
521  * provide another packet size.
522  *
523  * @param mp
524  *   The mempool from which mbufs originate.
525  * @param opaque_arg
526  *   A pointer that can be used by the user to retrieve useful information
527  *   for mbuf initialization. This pointer comes from the ``init_arg``
528  *   parameter of rte_mempool_create().
529  */
530 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
531
532 /**
533  * Reset the fields of a packet mbuf to their default values.
534  *
535  * The given mbuf must have only one segment.
536  *
537  * @param m
538  *   The packet mbuf to be resetted.
539  */
540 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
541 {
542         uint32_t buf_ofs;
543
544         m->pkt.next = NULL;
545         m->pkt.pkt_len = 0;
546         m->pkt.l2_len = 0;
547         m->pkt.l3_len = 0;
548         m->pkt.vlan_tci = 0;
549         m->pkt.nb_segs = 1;
550         m->pkt.in_port = 0xff;
551
552         m->ol_flags = 0;
553         buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
554                         RTE_PKTMBUF_HEADROOM : m->buf_len;
555         m->pkt.data = (char*) m->buf_addr + buf_ofs;
556
557         m->pkt.data_len = 0;
558         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
559 }
560
561 /**
562  * Allocate a new mbuf (type is pkt) from a mempool.
563  *
564  * This new mbuf contains one segment, which has a length of 0. The pointer
565  * to data is initialized to have some bytes of headroom in the buffer
566  * (if buffer size allows).
567  *
568  * @param mp
569  *   The mempool from which the mbuf is allocated.
570  * @return
571  *   - The pointer to the new mbuf on success.
572  *   - NULL if allocation failed.
573  */
574 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
575 {
576         struct rte_mbuf *m;
577         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
578                 rte_pktmbuf_reset(m);
579         return (m);
580 }
581
582 #ifdef RTE_MBUF_SCATTER_GATHER
583
584 /**
585  * Attach packet mbuf to another packet mbuf.
586  * After attachment we refer the mbuf we attached as 'indirect',
587  * while mbuf we attached to as 'direct'.
588  * Right now, not supported:
589  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
590  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
591  *  - mbuf we trying to attach (mi) is used by someone else
592  *    e.g. it's reference counter is greater then 1.
593  *
594  * @param mi
595  *   The indirect packet mbuf.
596  * @param md
597  *   The direct packet mbuf.
598  */
599
600 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
601 {
602         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
603             RTE_MBUF_DIRECT(mi) &&
604             rte_mbuf_refcnt_read(mi) == 1);
605
606         rte_mbuf_refcnt_update(md, 1);
607         mi->buf_physaddr = md->buf_physaddr;
608         mi->buf_addr = md->buf_addr;
609         mi->buf_len = md->buf_len;
610
611         mi->pkt = md->pkt;
612
613         mi->pkt.next = NULL;
614         mi->pkt.pkt_len = mi->pkt.data_len;
615         mi->pkt.nb_segs = 1;
616
617         __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
618         __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
619 }
620
621 /**
622  * Detach an indirect packet mbuf -
623  *  - restore original mbuf address and length values.
624  *  - reset pktmbuf data and data_len to their default values.
625  *  All other fields of the given packet mbuf will be left intact.
626  *
627  * @param m
628  *   The indirect attached packet mbuf.
629  */
630
631 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
632 {
633         const struct rte_mempool *mp = m->pool;
634         void *buf = RTE_MBUF_TO_BADDR(m);
635         uint32_t buf_ofs;
636         uint32_t buf_len = mp->elt_size - sizeof(*m);
637         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
638
639         m->buf_addr = buf;
640         m->buf_len = (uint16_t)buf_len;
641
642         buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
643                         RTE_PKTMBUF_HEADROOM : m->buf_len;
644         m->pkt.data = (char*) m->buf_addr + buf_ofs;
645
646         m->pkt.data_len = 0;
647 }
648
649 #endif /* RTE_MBUF_SCATTER_GATHER */
650
651 /**
652  * Free a segment of a packet mbuf into its original mempool.
653  *
654  * Free an mbuf, without parsing other segments in case of chained
655  * buffers.
656  *
657  * @param m
658  *   The packet mbuf segment to be freed.
659  */
660 static inline void rte_pktmbuf_free_seg(struct rte_mbuf *m)
661 {
662         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
663
664 #ifdef RTE_MBUF_SCATTER_GATHER
665         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
666                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
667                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
668
669                 rte_mbuf_refcnt_set(m, 0);
670
671                 /* if this is an indirect mbuf, then
672                  *  - detach mbuf
673                  *  - free attached mbuf segment
674                  */
675                 if (unlikely (md != m)) {
676                         rte_pktmbuf_detach(m);
677                         if (rte_mbuf_refcnt_update(md, -1) == 0)
678                                 __rte_mbuf_raw_free(md);
679                 }
680 #endif
681                 __rte_mbuf_raw_free(m);
682 #ifdef RTE_MBUF_SCATTER_GATHER
683         }
684 #endif
685 }
686
687 /**
688  * Free a packet mbuf back into its original mempool.
689  *
690  * Free an mbuf, and all its segments in case of chained buffers. Each
691  * segment is added back into its original mempool.
692  *
693  * @param m
694  *   The packet mbuf to be freed.
695  */
696 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
697 {
698         struct rte_mbuf *m_next;
699
700         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
701
702         while (m != NULL) {
703                 m_next = m->pkt.next;
704                 rte_pktmbuf_free_seg(m);
705                 m = m_next;
706         }
707 }
708
709 #ifdef RTE_MBUF_SCATTER_GATHER
710
711 /**
712  * Creates a "clone" of the given packet mbuf.
713  *
714  * Walks through all segments of the given packet mbuf, and for each of them:
715  *  - Creates a new packet mbuf from the given pool.
716  *  - Attaches newly created mbuf to the segment.
717  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
718  * from the original packet mbuf.
719  *
720  * @param md
721  *   The packet mbuf to be cloned.
722  * @param mp
723  *   The mempool from which the "clone" mbufs are allocated.
724  * @return
725  *   - The pointer to the new "clone" mbuf on success.
726  *   - NULL if allocation fails.
727  */
728 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
729                 struct rte_mempool *mp)
730 {
731         struct rte_mbuf *mc, *mi, **prev;
732         uint32_t pktlen;
733         uint8_t nseg;
734
735         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
736                 return (NULL);
737
738         mi = mc;
739         prev = &mi->pkt.next;
740         pktlen = md->pkt.pkt_len;
741         nseg = 0;
742
743         do {
744                 nseg++;
745                 rte_pktmbuf_attach(mi, md);
746                 *prev = mi;
747                 prev = &mi->pkt.next;
748         } while ((md = md->pkt.next) != NULL &&
749             (mi = rte_pktmbuf_alloc(mp)) != NULL);
750
751         *prev = NULL;
752         mc->pkt.nb_segs = nseg;
753         mc->pkt.pkt_len = pktlen;
754
755         /* Allocation of new indirect segment failed */
756         if (unlikely (mi == NULL)) {
757                 rte_pktmbuf_free(mc);
758                 return (NULL);
759         }
760
761         __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
762         return (mc);
763 }
764
765 /**
766  * Adds given value to the refcnt of all packet mbuf segments.
767  *
768  * Walks through all segments of given packet mbuf and for each of them
769  * invokes rte_mbuf_refcnt_update().
770  *
771  * @param m
772  *   The packet mbuf whose refcnt to be updated.
773  * @param v
774  *   The value to add to the mbuf's segments refcnt.
775  */
776 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
777 {
778         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
779
780         do {
781                 rte_mbuf_refcnt_update(m, v);
782         } while ((m = m->pkt.next) != NULL);
783 }
784
785 #endif /* RTE_MBUF_SCATTER_GATHER */
786
787 /**
788  * Get the headroom in a packet mbuf.
789  *
790  * @param m
791  *   The packet mbuf.
792  * @return
793  *   The length of the headroom.
794  */
795 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
796 {
797         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
798         return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
799 }
800
801 /**
802  * Get the tailroom of a packet mbuf.
803  *
804  * @param m
805  *   The packet mbuf.
806  * @return
807  *   The length of the tailroom.
808  */
809 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
810 {
811         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
812         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
813                           m->pkt.data_len);
814 }
815
816 /**
817  * Get the last segment of the packet.
818  *
819  * @param m
820  *   The packet mbuf.
821  * @return
822  *   The last segment of the given mbuf.
823  */
824 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
825 {
826         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
827
828         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
829         while (m2->pkt.next != NULL)
830                 m2 = m2->pkt.next;
831         return m2;
832 }
833
834 /**
835  * A macro that points to the start of the data in the mbuf.
836  *
837  * The returned pointer is cast to type t. Before using this
838  * function, the user must ensure that m_headlen(m) is large enough to
839  * read its data.
840  *
841  * @param m
842  *   The packet mbuf.
843  * @param t
844  *   The type to cast the result into.
845  */
846 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
847
848 /**
849  * A macro that returns the length of the packet.
850  *
851  * The value can be read or assigned.
852  *
853  * @param m
854  *   The packet mbuf.
855  */
856 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
857
858 /**
859  * A macro that returns the length of the segment.
860  *
861  * The value can be read or assigned.
862  *
863  * @param m
864  *   The packet mbuf.
865  */
866 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
867
868 /**
869  * Prepend len bytes to an mbuf data area.
870  *
871  * Returns a pointer to the new
872  * data start address. If there is not enough headroom in the first
873  * segment, the function will return NULL, without modifying the mbuf.
874  *
875  * @param m
876  *   The pkt mbuf.
877  * @param len
878  *   The amount of data to prepend (in bytes).
879  * @return
880  *   A pointer to the start of the newly prepended data, or
881  *   NULL if there is not enough headroom space in the first segment
882  */
883 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
884                                         uint16_t len)
885 {
886         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
887
888         if (unlikely(len > rte_pktmbuf_headroom(m)))
889                 return NULL;
890
891         m->pkt.data = (char*) m->pkt.data - len;
892         m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
893         m->pkt.pkt_len  = (m->pkt.pkt_len + len);
894
895         return (char*) m->pkt.data;
896 }
897
898 /**
899  * Append len bytes to an mbuf.
900  *
901  * Append len bytes to an mbuf and return a pointer to the start address
902  * of the added data. If there is not enough tailroom in the last
903  * segment, the function will return NULL, without modifying the mbuf.
904  *
905  * @param m
906  *   The packet mbuf.
907  * @param len
908  *   The amount of data to append (in bytes).
909  * @return
910  *   A pointer to the start of the newly appended data, or
911  *   NULL if there is not enough tailroom space in the last segment
912  */
913 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
914 {
915         void *tail;
916         struct rte_mbuf *m_last;
917
918         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
919
920         m_last = rte_pktmbuf_lastseg(m);
921         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
922                 return NULL;
923
924         tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
925         m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
926         m->pkt.pkt_len  = (m->pkt.pkt_len + len);
927         return (char*) tail;
928 }
929
930 /**
931  * Remove len bytes at the beginning of an mbuf.
932  *
933  * Returns a pointer to the start address of the new data area. If the
934  * length is greater than the length of the first segment, then the
935  * function will fail and return NULL, without modifying the mbuf.
936  *
937  * @param m
938  *   The packet mbuf.
939  * @param len
940  *   The amount of data to remove (in bytes).
941  * @return
942  *   A pointer to the new start of the data.
943  */
944 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
945 {
946         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
947
948         if (unlikely(len > m->pkt.data_len))
949                 return NULL;
950
951         m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
952         m->pkt.data = ((char*) m->pkt.data + len);
953         m->pkt.pkt_len  = (m->pkt.pkt_len - len);
954         return (char*) m->pkt.data;
955 }
956
957 /**
958  * Remove len bytes of data at the end of the mbuf.
959  *
960  * If the length is greater than the length of the last segment, the
961  * function will fail and return -1 without modifying the mbuf.
962  *
963  * @param m
964  *   The packet mbuf.
965  * @param len
966  *   The amount of data to remove (in bytes).
967  * @return
968  *   - 0: On success.
969  *   - -1: On error.
970  */
971 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
972 {
973         struct rte_mbuf *m_last;
974
975         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
976
977         m_last = rte_pktmbuf_lastseg(m);
978         if (unlikely(len > m_last->pkt.data_len))
979                 return -1;
980
981         m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
982         m->pkt.pkt_len  = (m->pkt.pkt_len - len);
983         return 0;
984 }
985
986 /**
987  * Test if mbuf data is contiguous.
988  *
989  * @param m
990  *   The packet mbuf.
991  * @return
992  *   - 1, if all data is contiguous (one segment).
993  *   - 0, if there is several segments.
994  */
995 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
996 {
997         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
998         return !!(m->pkt.nb_segs == 1);
999 }
1000
1001 /**
1002  * Dump an mbuf structure to the console.
1003  *
1004  * Dump all fields for the given packet mbuf and all its associated
1005  * segments (in the case of a chained buffer).
1006  *
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(const struct rte_mbuf *m, unsigned dump_len);
1014
1015 #ifdef __cplusplus
1016 }
1017 #endif
1018
1019 #endif /* _RTE_MBUF_H_ */