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