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