mbuf: remove too specific flags mask
[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  * - RX flags start at bit position zero, and get added to the left of previous
73  *   flags.
74  * - The most-significant 8 bits are reserved for generic mbuf flags
75  * - TX flags therefore start at bit position 55 (i.e. 63-8), and new flags get
76  *   added to the right of the previously defined flags
77  */
78 #define PKT_RX_VLAN_PKT      (1ULL << 0)  /**< RX packet is a 802.1q VLAN packet. */
79 #define PKT_RX_RSS_HASH      (1ULL << 1)  /**< RX packet with RSS hash result. */
80 #define PKT_RX_FDIR          (1ULL << 2)  /**< RX packet with FDIR match indicate. */
81 #define PKT_RX_L4_CKSUM_BAD  (1ULL << 3)  /**< L4 cksum of RX pkt. is not OK. */
82 #define PKT_RX_IP_CKSUM_BAD  (1ULL << 4)  /**< IP cksum of RX pkt. is not OK. */
83 #define PKT_RX_EIP_CKSUM_BAD (0ULL << 0)  /**< External IP header checksum error. */
84 #define PKT_RX_OVERSIZE      (0ULL << 0)  /**< Num of desc of an RX pkt oversize. */
85 #define PKT_RX_HBUF_OVERFLOW (0ULL << 0)  /**< Header buffer overflow. */
86 #define PKT_RX_RECIP_ERR     (0ULL << 0)  /**< Hardware processing error. */
87 #define PKT_RX_MAC_ERR       (0ULL << 0)  /**< MAC error. */
88 #define PKT_RX_IPV4_HDR      (1ULL << 5)  /**< RX packet with IPv4 header. */
89 #define PKT_RX_IPV4_HDR_EXT  (1ULL << 6)  /**< RX packet with extended IPv4 header. */
90 #define PKT_RX_IPV6_HDR      (1ULL << 7)  /**< RX packet with IPv6 header. */
91 #define PKT_RX_IPV6_HDR_EXT  (1ULL << 8)  /**< RX packet with extended IPv6 header. */
92 #define PKT_RX_IEEE1588_PTP  (1ULL << 9)  /**< RX IEEE1588 L2 Ethernet PT Packet. */
93 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
94 #define PKT_RX_TUNNEL_IPV4_HDR (1ULL << 11) /**< RX tunnel packet with IPv4 header.*/
95 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
96 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
97 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
98 /* add new RX flags here */
99
100 /* add new TX flags here */
101 #define PKT_TX_VXLAN_CKSUM   (1ULL << 50) /**< TX checksum of VXLAN computed by NIC */
102 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
103
104 /**
105  * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
106  * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
107  * L4 checksum offload, the user needs to:
108  *  - fill l2_len and l3_len in mbuf
109  *  - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
110  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
111  *  - calculate the pseudo header checksum and set it in the L4 header (only
112  *    for TCP or UDP). For SCTP, set the crc field to 0.
113  */
114 #define PKT_TX_L4_NO_CKSUM   (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
115 #define PKT_TX_TCP_CKSUM     (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
116 #define PKT_TX_SCTP_CKSUM    (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
117 #define PKT_TX_UDP_CKSUM     (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
118 #define PKT_TX_L4_MASK       (3ULL << 52) /**< Mask for L4 cksum offload request. */
119
120 #define PKT_TX_IP_CKSUM      (1ULL << 54) /**< IP cksum of TX pkt. computed by NIC. */
121 #define PKT_TX_IPV4_CSUM     PKT_TX_IP_CKSUM /**< Alias of PKT_TX_IP_CKSUM. */
122
123 /** Tell the NIC it's an IPv4 packet. Required for L4 checksum offload. */
124 #define PKT_TX_IPV4          PKT_RX_IPV4_HDR
125
126 /** Tell the NIC it's an IPv6 packet. Required for L4 checksum offload. */
127 #define PKT_TX_IPV6          PKT_RX_IPV6_HDR
128
129 #define PKT_TX_VLAN_PKT      (1ULL << 55) /**< TX packet is a 802.1q VLAN packet. */
130
131 /* Use final bit of flags to indicate a control mbuf */
132 #define CTRL_MBUF_FLAG       (1ULL << 63) /**< Mbuf contains control data */
133
134 /* define a set of marker types that can be used to refer to set points in the
135  * mbuf */
136 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
137 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
138                                * with a single assignment */
139 /**
140  * The generic rte_mbuf, containing a packet mbuf.
141  */
142 struct rte_mbuf {
143         MARKER cacheline0;
144
145         void *buf_addr;           /**< Virtual address of segment buffer. */
146         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
147
148         /* next 8 bytes are initialised on RX descriptor rearm */
149         MARKER64 rearm_data;
150         uint16_t buf_len;         /**< Length of segment buffer. */
151         uint16_t data_off;
152
153         /**
154          * 16-bit Reference counter.
155          * It should only be accessed using the following functions:
156          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
157          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
158          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
159          * config option.
160          */
161         union {
162 #ifdef RTE_MBUF_REFCNT
163                 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
164                 uint16_t refcnt;              /**< Non-atomically accessed refcnt */
165 #endif
166                 uint16_t refcnt_reserved;     /**< Do not use this field */
167         };
168         uint8_t nb_segs;          /**< Number of segments. */
169         uint8_t port;             /**< Input port. */
170
171         uint64_t ol_flags;        /**< Offload features. */
172
173         /* remaining bytes are set on RX when pulling packet from descriptor */
174         MARKER rx_descriptor_fields1;
175
176         /**
177          * The packet type, which is used to indicate ordinary packet and also
178          * tunneled packet format, i.e. each number is represented a type of
179          * packet.
180          */
181         uint16_t packet_type;
182
183         uint16_t data_len;        /**< Amount of data in segment buffer. */
184         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
185         uint16_t vlan_tci;        /**< VLAN Tag Control Identifier (CPU order) */
186         uint16_t reserved;
187         union {
188                 uint32_t rss;     /**< RSS hash result if RSS enabled */
189                 struct {
190                         union {
191                                 struct {
192                                         uint16_t hash;
193                                         uint16_t id;
194                                 };
195                                 uint32_t lo;
196                                 /**< Second 4 flexible bytes */
197                         };
198                         uint32_t hi;
199                         /**< First 4 flexible bytes or FD ID, dependent on
200                              PKT_RX_FDIR_* flag in ol_flags. */
201                 } fdir;           /**< Filter identifier if FDIR enabled */
202                 uint32_t sched;   /**< Hierarchical scheduler */
203                 uint32_t usr;     /**< User defined tags. See @rte_distributor_process */
204         } hash;                   /**< hash information */
205
206         /* second cache line - fields only used in slow path or on TX */
207         MARKER cacheline1 __rte_cache_aligned;
208
209         union {
210                 void *userdata;   /**< Can be used for external metadata */
211                 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
212         };
213
214         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
215         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
216
217         /* fields to support TX offloads */
218         union {
219                 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
220                 struct {
221                         uint16_t l3_len:9;      /**< L3 (IP) Header Length. */
222                         uint16_t l2_len:7;      /**< L2 (MAC) Header Length. */
223                 };
224         };
225
226         /* fields for TX offloading of tunnels */
227         union {
228                 uint16_t inner_l2_l3_len;
229                 /**< combined inner l2/l3 lengths as single var */
230                 struct {
231                         uint16_t inner_l3_len:9;
232                         /**< inner L3 (IP) Header Length. */
233                         uint16_t inner_l2_len:7;
234                         /**< inner L2 (MAC) Header Length. */
235                 };
236         };
237 } __rte_cache_aligned;
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, is_h) rte_mbuf_sanity_check(m, is_h)
275
276 /**  check mbuf type in debug mode if mbuf pointer is not null */
277 #define __rte_mbuf_sanity_check_raw(m, is_h)    do {       \
278         if ((m) != NULL)                                   \
279                 rte_mbuf_sanity_check(m, 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, 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, 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_REFCNT
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_REFCNT */
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_REFCNT */
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 is_header
402  *   True if the mbuf is a packet header, false if it is a sub-segment
403  *   of a packet (in this case, some fields like nb_segs are not checked)
404  */
405 void
406 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
407
408 /**
409  * @internal Allocate a new mbuf from mempool *mp*.
410  * The use of that function is reserved for RTE internal needs.
411  * Please use rte_pktmbuf_alloc().
412  *
413  * @param mp
414  *   The mempool from which mbuf is allocated.
415  * @return
416  *   - The pointer to the new mbuf on success.
417  *   - NULL if allocation failed.
418  */
419 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
420 {
421         struct rte_mbuf *m;
422         void *mb = NULL;
423         if (rte_mempool_get(mp, &mb) < 0)
424                 return NULL;
425         m = (struct rte_mbuf *)mb;
426 #ifdef RTE_MBUF_REFCNT
427         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
428         rte_mbuf_refcnt_set(m, 1);
429 #endif /* RTE_MBUF_REFCNT */
430         return (m);
431 }
432
433 /**
434  * @internal Put mbuf back into its original mempool.
435  * The use of that function is reserved for RTE internal needs.
436  * Please use rte_pktmbuf_free().
437  *
438  * @param m
439  *   The mbuf to be freed.
440  */
441 static inline void __attribute__((always_inline))
442 __rte_mbuf_raw_free(struct rte_mbuf *m)
443 {
444 #ifdef RTE_MBUF_REFCNT
445         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
446 #endif /* RTE_MBUF_REFCNT */
447         rte_mempool_put(m->pool, m);
448 }
449
450 /* Operations on ctrl mbuf */
451
452 /**
453  * The control mbuf constructor.
454  *
455  * This function initializes some fields in an mbuf structure that are
456  * not modified by the user once created (mbuf type, origin pool, buffer
457  * start address, and so on). This function is given as a callback function
458  * to rte_mempool_create() at pool creation time.
459  *
460  * @param mp
461  *   The mempool from which the mbuf is allocated.
462  * @param opaque_arg
463  *   A pointer that can be used by the user to retrieve useful information
464  *   for mbuf initialization. This pointer comes from the ``init_arg``
465  *   parameter of rte_mempool_create().
466  * @param m
467  *   The mbuf to initialize.
468  * @param i
469  *   The index of the mbuf in the pool table.
470  */
471 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
472                 void *m, unsigned i);
473
474 /**
475  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
476  *
477  * This new mbuf is initialized with data pointing to the beginning of
478  * buffer, and with a length of zero.
479  *
480  * @param mp
481  *   The mempool from which the mbuf is allocated.
482  * @return
483  *   - The pointer to the new mbuf on success.
484  *   - NULL if allocation failed.
485  */
486 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
487
488 /**
489  * Free a control mbuf back into its original mempool.
490  *
491  * @param m
492  *   The control mbuf to be freed.
493  */
494 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
495
496 /**
497  * A macro that returns the pointer to the carried data.
498  *
499  * The value that can be read or assigned.
500  *
501  * @param m
502  *   The control mbuf.
503  */
504 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
505
506 /**
507  * A macro that returns the length of the carried data.
508  *
509  * The value that can be read or assigned.
510  *
511  * @param m
512  *   The control mbuf.
513  */
514 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
515
516 /**
517  * Tests if an mbuf is a control mbuf
518  *
519  * @param m
520  *   The mbuf to be tested
521  * @return
522  *   - True (1) if the mbuf is a control mbuf
523  *   - False(0) otherwise
524  */
525 static inline int
526 rte_is_ctrlmbuf(struct rte_mbuf *m)
527 {
528         return (!!(m->ol_flags & CTRL_MBUF_FLAG));
529 }
530
531 /* Operations on pkt mbuf */
532
533 /**
534  * The packet mbuf constructor.
535  *
536  * This function initializes some fields in the mbuf structure that are
537  * not modified by the user once created (origin pool, buffer start
538  * address, and so on). This function is given as a callback function to
539  * rte_mempool_create() at pool creation time.
540  *
541  * @param mp
542  *   The mempool from which mbufs originate.
543  * @param opaque_arg
544  *   A pointer that can be used by the user to retrieve useful information
545  *   for mbuf initialization. This pointer comes from the ``init_arg``
546  *   parameter of rte_mempool_create().
547  * @param m
548  *   The mbuf to initialize.
549  * @param i
550  *   The index of the mbuf in the pool table.
551  */
552 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
553                       void *m, unsigned i);
554
555
556 /**
557  * A  packet mbuf pool constructor.
558  *
559  * This function initializes the mempool private data in the case of a
560  * pktmbuf pool. This private data is needed by the driver. The
561  * function is given as a callback function to rte_mempool_create() at
562  * pool creation. It can be extended by the user, for example, to
563  * provide another packet size.
564  *
565  * @param mp
566  *   The mempool from which mbufs originate.
567  * @param opaque_arg
568  *   A pointer that can be used by the user to retrieve useful information
569  *   for mbuf initialization. This pointer comes from the ``init_arg``
570  *   parameter of rte_mempool_create().
571  */
572 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
573
574 /**
575  * Reset the fields of a packet mbuf to their default values.
576  *
577  * The given mbuf must have only one segment.
578  *
579  * @param m
580  *   The packet mbuf to be resetted.
581  */
582 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
583 {
584         m->next = NULL;
585         m->pkt_len = 0;
586         m->l2_l3_len = 0;
587         m->inner_l2_l3_len = 0;
588         m->vlan_tci = 0;
589         m->nb_segs = 1;
590         m->port = 0xff;
591
592         m->ol_flags = 0;
593         m->packet_type = 0;
594         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
595                         RTE_PKTMBUF_HEADROOM : m->buf_len;
596
597         m->data_len = 0;
598         __rte_mbuf_sanity_check(m, 1);
599 }
600
601 /**
602  * Allocate a new mbuf from a mempool.
603  *
604  * This new mbuf contains one segment, which has a length of 0. The pointer
605  * to data is initialized to have some bytes of headroom in the buffer
606  * (if buffer size allows).
607  *
608  * @param mp
609  *   The mempool from which the mbuf is allocated.
610  * @return
611  *   - The pointer to the new mbuf on success.
612  *   - NULL if allocation failed.
613  */
614 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
615 {
616         struct rte_mbuf *m;
617         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
618                 rte_pktmbuf_reset(m);
619         return (m);
620 }
621
622 #ifdef RTE_MBUF_REFCNT
623
624 /**
625  * Attach packet mbuf to another packet mbuf.
626  * After attachment we refer the mbuf we attached as 'indirect',
627  * while mbuf we attached to as 'direct'.
628  * Right now, not supported:
629  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
630  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
631  *  - mbuf we trying to attach (mi) is used by someone else
632  *    e.g. it's reference counter is greater then 1.
633  *
634  * @param mi
635  *   The indirect packet mbuf.
636  * @param md
637  *   The direct packet mbuf.
638  */
639
640 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
641 {
642         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
643             RTE_MBUF_DIRECT(mi) &&
644             rte_mbuf_refcnt_read(mi) == 1);
645
646         rte_mbuf_refcnt_update(md, 1);
647         mi->buf_physaddr = md->buf_physaddr;
648         mi->buf_addr = md->buf_addr;
649         mi->buf_len = md->buf_len;
650
651         mi->next = md->next;
652         mi->data_off = md->data_off;
653         mi->data_len = md->data_len;
654         mi->port = md->port;
655         mi->vlan_tci = md->vlan_tci;
656         mi->l2_l3_len = md->l2_l3_len;
657         mi->inner_l2_l3_len = md->inner_l2_l3_len;
658         mi->hash = md->hash;
659
660         mi->next = NULL;
661         mi->pkt_len = mi->data_len;
662         mi->nb_segs = 1;
663         mi->ol_flags = md->ol_flags;
664         mi->packet_type = md->packet_type;
665
666         __rte_mbuf_sanity_check(mi, 1);
667         __rte_mbuf_sanity_check(md, 0);
668 }
669
670 /**
671  * Detach an indirect packet mbuf -
672  *  - restore original mbuf address and length values.
673  *  - reset pktmbuf data and data_len to their default values.
674  *  All other fields of the given packet mbuf will be left intact.
675  *
676  * @param m
677  *   The indirect attached packet mbuf.
678  */
679
680 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
681 {
682         const struct rte_mempool *mp = m->pool;
683         void *buf = RTE_MBUF_TO_BADDR(m);
684         uint32_t buf_len = mp->elt_size - sizeof(*m);
685         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
686
687         m->buf_addr = buf;
688         m->buf_len = (uint16_t)buf_len;
689
690         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
691                         RTE_PKTMBUF_HEADROOM : m->buf_len;
692
693         m->data_len = 0;
694 }
695
696 #endif /* RTE_MBUF_REFCNT */
697
698
699 static inline struct rte_mbuf* __attribute__((always_inline))
700 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
701 {
702         __rte_mbuf_sanity_check(m, 0);
703
704 #ifdef RTE_MBUF_REFCNT
705         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
706                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
707                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
708
709                 rte_mbuf_refcnt_set(m, 0);
710
711                 /* if this is an indirect mbuf, then
712                  *  - detach mbuf
713                  *  - free attached mbuf segment
714                  */
715                 if (unlikely (md != m)) {
716                         rte_pktmbuf_detach(m);
717                         if (rte_mbuf_refcnt_update(md, -1) == 0)
718                                 __rte_mbuf_raw_free(md);
719                 }
720 #endif
721                 return(m);
722 #ifdef RTE_MBUF_REFCNT
723         }
724         return (NULL);
725 #endif
726 }
727
728 /**
729  * Free a segment of a packet mbuf into its original mempool.
730  *
731  * Free an mbuf, without parsing other segments in case of chained
732  * buffers.
733  *
734  * @param m
735  *   The packet mbuf segment to be freed.
736  */
737 static inline void __attribute__((always_inline))
738 rte_pktmbuf_free_seg(struct rte_mbuf *m)
739 {
740         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
741                 m->next = NULL;
742                 __rte_mbuf_raw_free(m);
743         }
744 }
745
746 /**
747  * Free a packet mbuf back into its original mempool.
748  *
749  * Free an mbuf, and all its segments in case of chained buffers. Each
750  * segment is added back into its original mempool.
751  *
752  * @param m
753  *   The packet mbuf to be freed.
754  */
755 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
756 {
757         struct rte_mbuf *m_next;
758
759         __rte_mbuf_sanity_check(m, 1);
760
761         while (m != NULL) {
762                 m_next = m->next;
763                 rte_pktmbuf_free_seg(m);
764                 m = m_next;
765         }
766 }
767
768 #ifdef RTE_MBUF_REFCNT
769
770 /**
771  * Creates a "clone" of the given packet mbuf.
772  *
773  * Walks through all segments of the given packet mbuf, and for each of them:
774  *  - Creates a new packet mbuf from the given pool.
775  *  - Attaches newly created mbuf to the segment.
776  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
777  * from the original packet mbuf.
778  *
779  * @param md
780  *   The packet mbuf to be cloned.
781  * @param mp
782  *   The mempool from which the "clone" mbufs are allocated.
783  * @return
784  *   - The pointer to the new "clone" mbuf on success.
785  *   - NULL if allocation fails.
786  */
787 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
788                 struct rte_mempool *mp)
789 {
790         struct rte_mbuf *mc, *mi, **prev;
791         uint32_t pktlen;
792         uint8_t nseg;
793
794         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
795                 return (NULL);
796
797         mi = mc;
798         prev = &mi->next;
799         pktlen = md->pkt_len;
800         nseg = 0;
801
802         do {
803                 nseg++;
804                 rte_pktmbuf_attach(mi, md);
805                 *prev = mi;
806                 prev = &mi->next;
807         } while ((md = md->next) != NULL &&
808             (mi = rte_pktmbuf_alloc(mp)) != NULL);
809
810         *prev = NULL;
811         mc->nb_segs = nseg;
812         mc->pkt_len = pktlen;
813
814         /* Allocation of new indirect segment failed */
815         if (unlikely (mi == NULL)) {
816                 rte_pktmbuf_free(mc);
817                 return (NULL);
818         }
819
820         __rte_mbuf_sanity_check(mc, 1);
821         return (mc);
822 }
823
824 /**
825  * Adds given value to the refcnt of all packet mbuf segments.
826  *
827  * Walks through all segments of given packet mbuf and for each of them
828  * invokes rte_mbuf_refcnt_update().
829  *
830  * @param m
831  *   The packet mbuf whose refcnt to be updated.
832  * @param v
833  *   The value to add to the mbuf's segments refcnt.
834  */
835 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
836 {
837         __rte_mbuf_sanity_check(m, 1);
838
839         do {
840                 rte_mbuf_refcnt_update(m, v);
841         } while ((m = m->next) != NULL);
842 }
843
844 #endif /* RTE_MBUF_REFCNT */
845
846 /**
847  * Get the headroom in a packet mbuf.
848  *
849  * @param m
850  *   The packet mbuf.
851  * @return
852  *   The length of the headroom.
853  */
854 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
855 {
856         __rte_mbuf_sanity_check(m, 1);
857         return m->data_off;
858 }
859
860 /**
861  * Get the tailroom of a packet mbuf.
862  *
863  * @param m
864  *   The packet mbuf.
865  * @return
866  *   The length of the tailroom.
867  */
868 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
869 {
870         __rte_mbuf_sanity_check(m, 1);
871         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
872                           m->data_len);
873 }
874
875 /**
876  * Get the last segment of the packet.
877  *
878  * @param m
879  *   The packet mbuf.
880  * @return
881  *   The last segment of the given mbuf.
882  */
883 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
884 {
885         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
886
887         __rte_mbuf_sanity_check(m, 1);
888         while (m2->next != NULL)
889                 m2 = m2->next;
890         return m2;
891 }
892
893 /**
894  * A macro that points to the start of the data in the mbuf.
895  *
896  * The returned pointer is cast to type t. Before using this
897  * function, the user must ensure that m_headlen(m) is large enough to
898  * read its data.
899  *
900  * @param m
901  *   The packet mbuf.
902  * @param t
903  *   The type to cast the result into.
904  */
905 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
906
907 /**
908  * A macro that returns the length of the packet.
909  *
910  * The value can be read or assigned.
911  *
912  * @param m
913  *   The packet mbuf.
914  */
915 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
916
917 /**
918  * A macro that returns the length of the segment.
919  *
920  * The value can be read or assigned.
921  *
922  * @param m
923  *   The packet mbuf.
924  */
925 #define rte_pktmbuf_data_len(m) ((m)->data_len)
926
927 /**
928  * Prepend len bytes to an mbuf data area.
929  *
930  * Returns a pointer to the new
931  * data start address. If there is not enough headroom in the first
932  * segment, the function will return NULL, without modifying the mbuf.
933  *
934  * @param m
935  *   The pkt mbuf.
936  * @param len
937  *   The amount of data to prepend (in bytes).
938  * @return
939  *   A pointer to the start of the newly prepended data, or
940  *   NULL if there is not enough headroom space in the first segment
941  */
942 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
943                                         uint16_t len)
944 {
945         __rte_mbuf_sanity_check(m, 1);
946
947         if (unlikely(len > rte_pktmbuf_headroom(m)))
948                 return NULL;
949
950         m->data_off -= len;
951         m->data_len = (uint16_t)(m->data_len + len);
952         m->pkt_len  = (m->pkt_len + len);
953
954         return (char *)m->buf_addr + m->data_off;
955 }
956
957 /**
958  * Append len bytes to an mbuf.
959  *
960  * Append len bytes to an mbuf and return a pointer to the start address
961  * of the added data. If there is not enough tailroom in the last
962  * segment, the function will return NULL, without modifying the mbuf.
963  *
964  * @param m
965  *   The packet mbuf.
966  * @param len
967  *   The amount of data to append (in bytes).
968  * @return
969  *   A pointer to the start of the newly appended data, or
970  *   NULL if there is not enough tailroom space in the last segment
971  */
972 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
973 {
974         void *tail;
975         struct rte_mbuf *m_last;
976
977         __rte_mbuf_sanity_check(m, 1);
978
979         m_last = rte_pktmbuf_lastseg(m);
980         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
981                 return NULL;
982
983         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
984         m_last->data_len = (uint16_t)(m_last->data_len + len);
985         m->pkt_len  = (m->pkt_len + len);
986         return (char*) tail;
987 }
988
989 /**
990  * Remove len bytes at the beginning of an mbuf.
991  *
992  * Returns a pointer to the start address of the new data area. If the
993  * length is greater than the length of the first segment, then the
994  * function will fail and return NULL, without modifying the mbuf.
995  *
996  * @param m
997  *   The packet mbuf.
998  * @param len
999  *   The amount of data to remove (in bytes).
1000  * @return
1001  *   A pointer to the new start of the data.
1002  */
1003 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1004 {
1005         __rte_mbuf_sanity_check(m, 1);
1006
1007         if (unlikely(len > m->data_len))
1008                 return NULL;
1009
1010         m->data_len = (uint16_t)(m->data_len - len);
1011         m->data_off += len;
1012         m->pkt_len  = (m->pkt_len - len);
1013         return (char *)m->buf_addr + m->data_off;
1014 }
1015
1016 /**
1017  * Remove len bytes of data at the end of the mbuf.
1018  *
1019  * If the length is greater than the length of the last segment, the
1020  * function will fail and return -1 without modifying the mbuf.
1021  *
1022  * @param m
1023  *   The packet mbuf.
1024  * @param len
1025  *   The amount of data to remove (in bytes).
1026  * @return
1027  *   - 0: On success.
1028  *   - -1: On error.
1029  */
1030 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1031 {
1032         struct rte_mbuf *m_last;
1033
1034         __rte_mbuf_sanity_check(m, 1);
1035
1036         m_last = rte_pktmbuf_lastseg(m);
1037         if (unlikely(len > m_last->data_len))
1038                 return -1;
1039
1040         m_last->data_len = (uint16_t)(m_last->data_len - len);
1041         m->pkt_len  = (m->pkt_len - len);
1042         return 0;
1043 }
1044
1045 /**
1046  * Test if mbuf data is contiguous.
1047  *
1048  * @param m
1049  *   The packet mbuf.
1050  * @return
1051  *   - 1, if all data is contiguous (one segment).
1052  *   - 0, if there is several segments.
1053  */
1054 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1055 {
1056         __rte_mbuf_sanity_check(m, 1);
1057         return !!(m->nb_segs == 1);
1058 }
1059
1060 /**
1061  * Dump an mbuf structure to the console.
1062  *
1063  * Dump all fields for the given packet mbuf and all its associated
1064  * segments (in the case of a chained buffer).
1065  *
1066  * @param f
1067  *   A pointer to a file for output
1068  * @param m
1069  *   The packet mbuf.
1070  * @param dump_len
1071  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1072  *   the packet.
1073  */
1074 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1075
1076 #ifdef __cplusplus
1077 }
1078 #endif
1079
1080 #endif /* _RTE_MBUF_H_ */