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