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