f8e492e59dfa47da8f3649a1424dc90297898d89
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2014 6WIND S.A.
4  */
5
6 #ifndef _RTE_MBUF_H_
7 #define _RTE_MBUF_H_
8
9 /**
10  * @file
11  * RTE Mbuf
12  *
13  * The mbuf library provides the ability to create and destroy buffers
14  * that may be used by the RTE application to store message
15  * buffers. The message buffers are stored in a mempool, using the
16  * RTE mempool library.
17  *
18  * The preferred way to create a mbuf pool is to use
19  * rte_pktmbuf_pool_create(). However, in some situations, an
20  * application may want to have more control (ex: populate the pool with
21  * specific memory), in this case it is possible to use functions from
22  * rte_mempool. See how rte_pktmbuf_pool_create() is implemented for
23  * details.
24  *
25  * This library provides an API to allocate/free packet mbufs, which are
26  * used to carry network packets.
27  *
28  * To understand the concepts of packet buffers or mbufs, you
29  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
30  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
31  * http://www.kohala.com/start/tcpipiv2.html
32  */
33
34 #include <stdint.h>
35 #include <rte_compat.h>
36 #include <rte_common.h>
37 #include <rte_config.h>
38 #include <rte_mempool.h>
39 #include <rte_memory.h>
40 #include <rte_atomic.h>
41 #include <rte_prefetch.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_byteorder.h>
44 #include <rte_mbuf_ptype.h>
45 #include <rte_mbuf_core.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /**
52  * Get the name of a RX offload flag
53  *
54  * @param mask
55  *   The mask describing the flag.
56  * @return
57  *   The name of this flag, or NULL if it's not a valid RX flag.
58  */
59 const char *rte_get_rx_ol_flag_name(uint64_t mask);
60
61 /**
62  * Dump the list of RX offload flags in a buffer
63  *
64  * @param mask
65  *   The mask describing the RX flags.
66  * @param buf
67  *   The output buffer.
68  * @param buflen
69  *   The length of the buffer.
70  * @return
71  *   0 on success, (-1) on error.
72  */
73 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
74
75 /**
76  * Get the name of a TX offload flag
77  *
78  * @param mask
79  *   The mask describing the flag. Usually only one bit must be set.
80  *   Several bits can be given if they belong to the same mask.
81  *   Ex: PKT_TX_L4_MASK.
82  * @return
83  *   The name of this flag, or NULL if it's not a valid TX flag.
84  */
85 const char *rte_get_tx_ol_flag_name(uint64_t mask);
86
87 /**
88  * Dump the list of TX offload flags in a buffer
89  *
90  * @param mask
91  *   The mask describing the TX flags.
92  * @param buf
93  *   The output buffer.
94  * @param buflen
95  *   The length of the buffer.
96  * @return
97  *   0 on success, (-1) on error.
98  */
99 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
100
101 /**
102  * Prefetch the first part of the mbuf
103  *
104  * The first 64 bytes of the mbuf corresponds to fields that are used early
105  * in the receive path. If the cache line of the architecture is higher than
106  * 64B, the second part will also be prefetched.
107  *
108  * @param m
109  *   The pointer to the mbuf.
110  */
111 static inline void
112 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
113 {
114         rte_prefetch0(&m->cacheline0);
115 }
116
117 /**
118  * Prefetch the second part of the mbuf
119  *
120  * The next 64 bytes of the mbuf corresponds to fields that are used in the
121  * transmit path. If the cache line of the architecture is higher than 64B,
122  * this function does nothing as it is expected that the full mbuf is
123  * already in cache.
124  *
125  * @param m
126  *   The pointer to the mbuf.
127  */
128 static inline void
129 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
130 {
131 #if RTE_CACHE_LINE_SIZE == 64
132         rte_prefetch0(&m->cacheline1);
133 #else
134         RTE_SET_USED(m);
135 #endif
136 }
137
138
139 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
140
141 /**
142  * Return the IO address of the beginning of the mbuf data
143  *
144  * @param mb
145  *   The pointer to the mbuf.
146  * @return
147  *   The IO address of the beginning of the mbuf data
148  */
149 static inline rte_iova_t
150 rte_mbuf_data_iova(const struct rte_mbuf *mb)
151 {
152         return mb->buf_iova + mb->data_off;
153 }
154
155 __rte_deprecated
156 static inline phys_addr_t
157 rte_mbuf_data_dma_addr(const struct rte_mbuf *mb)
158 {
159         return rte_mbuf_data_iova(mb);
160 }
161
162 /**
163  * Return the default IO address of the beginning of the mbuf data
164  *
165  * This function is used by drivers in their receive function, as it
166  * returns the location where data should be written by the NIC, taking
167  * the default headroom in account.
168  *
169  * @param mb
170  *   The pointer to the mbuf.
171  * @return
172  *   The IO address of the beginning of the mbuf data
173  */
174 static inline rte_iova_t
175 rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
176 {
177         return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
178 }
179
180 __rte_deprecated
181 static inline phys_addr_t
182 rte_mbuf_data_dma_addr_default(const struct rte_mbuf *mb)
183 {
184         return rte_mbuf_data_iova_default(mb);
185 }
186
187 /**
188  * Return the mbuf owning the data buffer address of an indirect mbuf.
189  *
190  * @param mi
191  *   The pointer to the indirect mbuf.
192  * @return
193  *   The address of the direct mbuf corresponding to buffer_addr.
194  */
195 static inline struct rte_mbuf *
196 rte_mbuf_from_indirect(struct rte_mbuf *mi)
197 {
198         return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
199 }
200
201 /**
202  * Return address of buffer embedded in the given mbuf.
203  *
204  * The return value shall be same as mb->buf_addr if the mbuf is already
205  * initialized and direct. However, this API is useful if mempool of the
206  * mbuf is already known because it doesn't need to access mbuf contents in
207  * order to get the mempool pointer.
208  *
209  * @warning
210  * @b EXPERIMENTAL: This API may change without prior notice.
211  * This will be used by rte_mbuf_to_baddr() which has redundant code once
212  * experimental tag is removed.
213  *
214  * @param mb
215  *   The pointer to the mbuf.
216  * @param mp
217  *   The pointer to the mempool of the mbuf.
218  * @return
219  *   The pointer of the mbuf buffer.
220  */
221 __rte_experimental
222 static inline char *
223 rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
224 {
225         return (char *)mb + sizeof(*mb) + rte_pktmbuf_priv_size(mp);
226 }
227
228 /**
229  * Return the default address of the beginning of the mbuf data.
230  *
231  * @warning
232  * @b EXPERIMENTAL: This API may change without prior notice.
233  *
234  * @param mb
235  *   The pointer to the mbuf.
236  * @return
237  *   The pointer of the beginning of the mbuf data.
238  */
239 __rte_experimental
240 static inline char *
241 rte_mbuf_data_addr_default(__rte_unused struct rte_mbuf *mb)
242 {
243         /* gcc complains about calling this experimental function even
244          * when not using it. Hide it with ALLOW_EXPERIMENTAL_API.
245          */
246 #ifdef ALLOW_EXPERIMENTAL_API
247         return rte_mbuf_buf_addr(mb, mb->pool) + RTE_PKTMBUF_HEADROOM;
248 #else
249         return NULL;
250 #endif
251 }
252
253 /**
254  * Return address of buffer embedded in the given mbuf.
255  *
256  * @note: Accessing mempool pointer of a mbuf is expensive because the
257  * pointer is stored in the 2nd cache line of mbuf. If mempool is known, it
258  * is better not to reference the mempool pointer in mbuf but calling
259  * rte_mbuf_buf_addr() would be more efficient.
260  *
261  * @param md
262  *   The pointer to the mbuf.
263  * @return
264  *   The address of the data buffer owned by the mbuf.
265  */
266 static inline char *
267 rte_mbuf_to_baddr(struct rte_mbuf *md)
268 {
269 #ifdef ALLOW_EXPERIMENTAL_API
270         return rte_mbuf_buf_addr(md, md->pool);
271 #else
272         char *buffer_addr;
273         buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
274         return buffer_addr;
275 #endif
276 }
277
278 /**
279  * Return the starting address of the private data area embedded in
280  * the given mbuf.
281  *
282  * Note that no check is made to ensure that a private data area
283  * actually exists in the supplied mbuf.
284  *
285  * @param m
286  *   The pointer to the mbuf.
287  * @return
288  *   The starting address of the private data area of the given mbuf.
289  */
290 __rte_experimental
291 static inline void *
292 rte_mbuf_to_priv(struct rte_mbuf *m)
293 {
294         return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
295 }
296
297 /**
298  * Private data in case of pktmbuf pool.
299  *
300  * A structure that contains some pktmbuf_pool-specific data that are
301  * appended after the mempool structure (in private data).
302  */
303 struct rte_pktmbuf_pool_private {
304         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
305         uint16_t mbuf_priv_size;      /**< Size of private area in each mbuf. */
306         uint32_t flags; /**< reserved for future use. */
307 };
308
309 /**
310  * Return the flags from private data in an mempool structure.
311  *
312  * @param mp
313  *   A pointer to the mempool structure.
314  * @return
315  *   The flags from the private data structure.
316  */
317 static inline uint32_t
318 rte_pktmbuf_priv_flags(struct rte_mempool *mp)
319 {
320         struct rte_pktmbuf_pool_private *mbp_priv;
321
322         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
323         return mbp_priv->flags;
324 }
325
326 /**
327  * When set, pktmbuf mempool will hold only mbufs with pinned external
328  * buffer. The external buffer will be attached to the mbuf at the
329  * memory pool creation and will never be detached by the mbuf free calls.
330  * mbuf should not contain any room for data after the mbuf structure.
331  */
332 #define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF (1 << 0)
333
334 /**
335  * Returns non zero if given mbuf has a pinned external buffer, or zero
336  * otherwise. The pinned external buffer is allocated at pool creation
337  * time and should not be freed on mbuf freeing.
338  *
339  * External buffer is a user-provided anonymous buffer.
340  */
341 #define RTE_MBUF_HAS_PINNED_EXTBUF(mb) \
342         (rte_pktmbuf_priv_flags(mb->pool) & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
343
344 #ifdef RTE_LIBRTE_MBUF_DEBUG
345
346 /**  check mbuf type in debug mode */
347 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
348
349 #else /*  RTE_LIBRTE_MBUF_DEBUG */
350
351 /**  check mbuf type in debug mode */
352 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
353
354 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
355
356 #ifdef RTE_MBUF_REFCNT_ATOMIC
357
358 /**
359  * Reads the value of an mbuf's refcnt.
360  * @param m
361  *   Mbuf to read
362  * @return
363  *   Reference count number.
364  */
365 static inline uint16_t
366 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
367 {
368         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
369 }
370
371 /**
372  * Sets an mbuf's refcnt to a defined value.
373  * @param m
374  *   Mbuf to update
375  * @param new_value
376  *   Value set
377  */
378 static inline void
379 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
380 {
381         rte_atomic16_set(&m->refcnt_atomic, (int16_t)new_value);
382 }
383
384 /* internal */
385 static inline uint16_t
386 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
387 {
388         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
389 }
390
391 /**
392  * Adds given value to an mbuf's refcnt and returns its new value.
393  * @param m
394  *   Mbuf to update
395  * @param value
396  *   Value to add/subtract
397  * @return
398  *   Updated value
399  */
400 static inline uint16_t
401 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
402 {
403         /*
404          * The atomic_add is an expensive operation, so we don't want to
405          * call it in the case where we know we are the unique holder of
406          * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
407          * operation has to be used because concurrent accesses on the
408          * reference counter can occur.
409          */
410         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
411                 ++value;
412                 rte_mbuf_refcnt_set(m, (uint16_t)value);
413                 return (uint16_t)value;
414         }
415
416         return __rte_mbuf_refcnt_update(m, value);
417 }
418
419 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
420
421 /* internal */
422 static inline uint16_t
423 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
424 {
425         m->refcnt = (uint16_t)(m->refcnt + value);
426         return m->refcnt;
427 }
428
429 /**
430  * Adds given value to an mbuf's refcnt and returns its new value.
431  */
432 static inline uint16_t
433 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
434 {
435         return __rte_mbuf_refcnt_update(m, value);
436 }
437
438 /**
439  * Reads the value of an mbuf's refcnt.
440  */
441 static inline uint16_t
442 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
443 {
444         return m->refcnt;
445 }
446
447 /**
448  * Sets an mbuf's refcnt to the defined value.
449  */
450 static inline void
451 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
452 {
453         m->refcnt = new_value;
454 }
455
456 #endif /* RTE_MBUF_REFCNT_ATOMIC */
457
458 /**
459  * Reads the refcnt of an external buffer.
460  *
461  * @param shinfo
462  *   Shared data of the external buffer.
463  * @return
464  *   Reference count number.
465  */
466 static inline uint16_t
467 rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
468 {
469         return (uint16_t)(rte_atomic16_read(&shinfo->refcnt_atomic));
470 }
471
472 /**
473  * Set refcnt of an external buffer.
474  *
475  * @param shinfo
476  *   Shared data of the external buffer.
477  * @param new_value
478  *   Value set
479  */
480 static inline void
481 rte_mbuf_ext_refcnt_set(struct rte_mbuf_ext_shared_info *shinfo,
482         uint16_t new_value)
483 {
484         rte_atomic16_set(&shinfo->refcnt_atomic, (int16_t)new_value);
485 }
486
487 /**
488  * Add given value to refcnt of an external buffer and return its new
489  * value.
490  *
491  * @param shinfo
492  *   Shared data of the external buffer.
493  * @param value
494  *   Value to add/subtract
495  * @return
496  *   Updated value
497  */
498 static inline uint16_t
499 rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo,
500         int16_t value)
501 {
502         if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
503                 ++value;
504                 rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
505                 return (uint16_t)value;
506         }
507
508         return (uint16_t)rte_atomic16_add_return(&shinfo->refcnt_atomic, value);
509 }
510
511 /** Mbuf prefetch */
512 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
513         if ((m) != NULL)                        \
514                 rte_prefetch0(m);               \
515 } while (0)
516
517
518 /**
519  * Sanity checks on an mbuf.
520  *
521  * Check the consistency of the given mbuf. The function will cause a
522  * panic if corruption is detected.
523  *
524  * @param m
525  *   The mbuf to be checked.
526  * @param is_header
527  *   True if the mbuf is a packet header, false if it is a sub-segment
528  *   of a packet (in this case, some fields like nb_segs are not checked)
529  */
530 void
531 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
532
533 /**
534  * Sanity checks on a mbuf.
535  *
536  * Almost like rte_mbuf_sanity_check(), but this function gives the reason
537  * if corruption is detected rather than panic.
538  *
539  * @param m
540  *   The mbuf to be checked.
541  * @param is_header
542  *   True if the mbuf is a packet header, false if it is a sub-segment
543  *   of a packet (in this case, some fields like nb_segs are not checked)
544  * @param reason
545  *   A reference to a string pointer where to store the reason why a mbuf is
546  *   considered invalid.
547  * @return
548  *   - 0 if no issue has been found, reason is left untouched.
549  *   - -1 if a problem is detected, reason then points to a string describing
550  *     the reason why the mbuf is deemed invalid.
551  */
552 __rte_experimental
553 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
554                    const char **reason);
555
556 #define MBUF_RAW_ALLOC_CHECK(m) do {                            \
557         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);               \
558         RTE_ASSERT((m)->next == NULL);                          \
559         RTE_ASSERT((m)->nb_segs == 1);                          \
560         __rte_mbuf_sanity_check(m, 0);                          \
561 } while (0)
562
563 /**
564  * Allocate an uninitialized mbuf from mempool *mp*.
565  *
566  * This function can be used by PMDs (especially in RX functions) to
567  * allocate an uninitialized mbuf. The driver is responsible of
568  * initializing all the required fields. See rte_pktmbuf_reset().
569  * For standard needs, prefer rte_pktmbuf_alloc().
570  *
571  * The caller can expect that the following fields of the mbuf structure
572  * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
573  * next=NULL, pool, priv_size. The other fields must be initialized
574  * by the caller.
575  *
576  * @param mp
577  *   The mempool from which mbuf is allocated.
578  * @return
579  *   - The pointer to the new mbuf on success.
580  *   - NULL if allocation failed.
581  */
582 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
583 {
584         struct rte_mbuf *m;
585
586         if (rte_mempool_get(mp, (void **)&m) < 0)
587                 return NULL;
588         MBUF_RAW_ALLOC_CHECK(m);
589         return m;
590 }
591
592 /**
593  * Put mbuf back into its original mempool.
594  *
595  * The caller must ensure that the mbuf is direct and properly
596  * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
597  * rte_pktmbuf_prefree_seg().
598  *
599  * This function should be used with care, when optimization is
600  * required. For standard needs, prefer rte_pktmbuf_free() or
601  * rte_pktmbuf_free_seg().
602  *
603  * @param m
604  *   The mbuf to be freed.
605  */
606 static __rte_always_inline void
607 rte_mbuf_raw_free(struct rte_mbuf *m)
608 {
609         RTE_ASSERT(!RTE_MBUF_CLONED(m) &&
610                   (!RTE_MBUF_HAS_EXTBUF(m) || RTE_MBUF_HAS_PINNED_EXTBUF(m)));
611         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
612         RTE_ASSERT(m->next == NULL);
613         RTE_ASSERT(m->nb_segs == 1);
614         __rte_mbuf_sanity_check(m, 0);
615         rte_mempool_put(m->pool, m);
616 }
617
618 /**
619  * The packet mbuf constructor.
620  *
621  * This function initializes some fields in the mbuf structure that are
622  * not modified by the user once created (origin pool, buffer start
623  * address, and so on). This function is given as a callback function to
624  * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
625  *
626  * @param mp
627  *   The mempool from which mbufs originate.
628  * @param opaque_arg
629  *   A pointer that can be used by the user to retrieve useful information
630  *   for mbuf initialization. This pointer is the opaque argument passed to
631  *   rte_mempool_obj_iter() or rte_mempool_create().
632  * @param m
633  *   The mbuf to initialize.
634  * @param i
635  *   The index of the mbuf in the pool table.
636  */
637 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
638                       void *m, unsigned i);
639
640 /**
641  * A  packet mbuf pool constructor.
642  *
643  * This function initializes the mempool private data in the case of a
644  * pktmbuf pool. This private data is needed by the driver. The
645  * function must be called on the mempool before it is used, or it
646  * can be given as a callback function to rte_mempool_create() at
647  * pool creation. It can be extended by the user, for example, to
648  * provide another packet size.
649  *
650  * @param mp
651  *   The mempool from which mbufs originate.
652  * @param opaque_arg
653  *   A pointer that can be used by the user to retrieve useful information
654  *   for mbuf initialization. This pointer is the opaque argument passed to
655  *   rte_mempool_create().
656  */
657 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
658
659 /**
660  * Create a mbuf pool.
661  *
662  * This function creates and initializes a packet mbuf pool. It is
663  * a wrapper to rte_mempool functions.
664  *
665  * @param name
666  *   The name of the mbuf pool.
667  * @param n
668  *   The number of elements in the mbuf pool. The optimum size (in terms
669  *   of memory usage) for a mempool is when n is a power of two minus one:
670  *   n = (2^q - 1).
671  * @param cache_size
672  *   Size of the per-core object cache. See rte_mempool_create() for
673  *   details.
674  * @param priv_size
675  *   Size of application private are between the rte_mbuf structure
676  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
677  * @param data_room_size
678  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
679  * @param socket_id
680  *   The socket identifier where the memory should be allocated. The
681  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
682  *   reserved zone.
683  * @return
684  *   The pointer to the new allocated mempool, on success. NULL on error
685  *   with rte_errno set appropriately. Possible rte_errno values include:
686  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
687  *    - E_RTE_SECONDARY - function was called from a secondary process instance
688  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
689  *    - ENOSPC - the maximum number of memzones has already been allocated
690  *    - EEXIST - a memzone with the same name already exists
691  *    - ENOMEM - no appropriate memory area found in which to create memzone
692  */
693 struct rte_mempool *
694 rte_pktmbuf_pool_create(const char *name, unsigned n,
695         unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
696         int socket_id);
697
698 /**
699  * Create a mbuf pool with a given mempool ops name
700  *
701  * This function creates and initializes a packet mbuf pool. It is
702  * a wrapper to rte_mempool functions.
703  *
704  * @param name
705  *   The name of the mbuf pool.
706  * @param n
707  *   The number of elements in the mbuf pool. The optimum size (in terms
708  *   of memory usage) for a mempool is when n is a power of two minus one:
709  *   n = (2^q - 1).
710  * @param cache_size
711  *   Size of the per-core object cache. See rte_mempool_create() for
712  *   details.
713  * @param priv_size
714  *   Size of application private are between the rte_mbuf structure
715  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
716  * @param data_room_size
717  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
718  * @param socket_id
719  *   The socket identifier where the memory should be allocated. The
720  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
721  *   reserved zone.
722  * @param ops_name
723  *   The mempool ops name to be used for this mempool instead of
724  *   default mempool. The value can be *NULL* to use default mempool.
725  * @return
726  *   The pointer to the new allocated mempool, on success. NULL on error
727  *   with rte_errno set appropriately. Possible rte_errno values include:
728  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
729  *    - E_RTE_SECONDARY - function was called from a secondary process instance
730  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
731  *    - ENOSPC - the maximum number of memzones has already been allocated
732  *    - EEXIST - a memzone with the same name already exists
733  *    - ENOMEM - no appropriate memory area found in which to create memzone
734  */
735 struct rte_mempool *
736 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
737         unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
738         int socket_id, const char *ops_name);
739
740 /** A structure that describes the pinned external buffer segment. */
741 struct rte_pktmbuf_extmem {
742         void *buf_ptr;          /**< The virtual address of data buffer. */
743         rte_iova_t buf_iova;    /**< The IO address of the data buffer. */
744         size_t buf_len;         /**< External buffer length in bytes. */
745         uint16_t elt_size;      /**< mbuf element size in bytes. */
746 };
747
748 /**
749  * Create a mbuf pool with external pinned data buffers.
750  *
751  * This function creates and initializes a packet mbuf pool that contains
752  * only mbufs with external buffer. It is a wrapper to rte_mempool functions.
753  *
754  * @param name
755  *   The name of the mbuf pool.
756  * @param n
757  *   The number of elements in the mbuf pool. The optimum size (in terms
758  *   of memory usage) for a mempool is when n is a power of two minus one:
759  *   n = (2^q - 1).
760  * @param cache_size
761  *   Size of the per-core object cache. See rte_mempool_create() for
762  *   details.
763  * @param priv_size
764  *   Size of application private are between the rte_mbuf structure
765  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
766  * @param data_room_size
767  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
768  * @param socket_id
769  *   The socket identifier where the memory should be allocated. The
770  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
771  *   reserved zone.
772  * @param ext_mem
773  *   Pointer to the array of structures describing the external memory
774  *   for data buffers. It is caller responsibility to register this memory
775  *   with rte_extmem_register() (if needed), map this memory to appropriate
776  *   physical device, etc.
777  * @param ext_num
778  *   Number of elements in the ext_mem array.
779  * @return
780  *   The pointer to the new allocated mempool, on success. NULL on error
781  *   with rte_errno set appropriately. Possible rte_errno values include:
782  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
783  *    - E_RTE_SECONDARY - function was called from a secondary process instance
784  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
785  *    - ENOSPC - the maximum number of memzones has already been allocated
786  *    - EEXIST - a memzone with the same name already exists
787  *    - ENOMEM - no appropriate memory area found in which to create memzone
788  */
789 __rte_experimental
790 struct rte_mempool *
791 rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
792         unsigned int cache_size, uint16_t priv_size,
793         uint16_t data_room_size, int socket_id,
794         const struct rte_pktmbuf_extmem *ext_mem,
795         unsigned int ext_num);
796
797 /**
798  * Get the data room size of mbufs stored in a pktmbuf_pool
799  *
800  * The data room size is the amount of data that can be stored in a
801  * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
802  *
803  * @param mp
804  *   The packet mbuf pool.
805  * @return
806  *   The data room size of mbufs stored in this mempool.
807  */
808 static inline uint16_t
809 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
810 {
811         struct rte_pktmbuf_pool_private *mbp_priv;
812
813         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
814         return mbp_priv->mbuf_data_room_size;
815 }
816
817 /**
818  * Get the application private size of mbufs stored in a pktmbuf_pool
819  *
820  * The private size of mbuf is a zone located between the rte_mbuf
821  * structure and the data buffer where an application can store data
822  * associated to a packet.
823  *
824  * @param mp
825  *   The packet mbuf pool.
826  * @return
827  *   The private size of mbufs stored in this mempool.
828  */
829 static inline uint16_t
830 rte_pktmbuf_priv_size(struct rte_mempool *mp)
831 {
832         struct rte_pktmbuf_pool_private *mbp_priv;
833
834         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
835         return mbp_priv->mbuf_priv_size;
836 }
837
838 /**
839  * Reset the data_off field of a packet mbuf to its default value.
840  *
841  * The given mbuf must have only one segment, which should be empty.
842  *
843  * @param m
844  *   The packet mbuf's data_off field has to be reset.
845  */
846 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
847 {
848         m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
849                                         (uint16_t)m->buf_len);
850 }
851
852 /**
853  * Reset the fields of a packet mbuf to their default values.
854  *
855  * The given mbuf must have only one segment.
856  *
857  * @param m
858  *   The packet mbuf to be reset.
859  */
860 #define MBUF_INVALID_PORT UINT16_MAX
861
862 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
863 {
864         m->next = NULL;
865         m->pkt_len = 0;
866         m->tx_offload = 0;
867         m->vlan_tci = 0;
868         m->vlan_tci_outer = 0;
869         m->nb_segs = 1;
870         m->port = MBUF_INVALID_PORT;
871
872         m->ol_flags &= EXT_ATTACHED_MBUF;
873         m->packet_type = 0;
874         rte_pktmbuf_reset_headroom(m);
875
876         m->data_len = 0;
877         __rte_mbuf_sanity_check(m, 1);
878 }
879
880 /**
881  * Allocate a new mbuf from a mempool.
882  *
883  * This new mbuf contains one segment, which has a length of 0. The pointer
884  * to data is initialized to have some bytes of headroom in the buffer
885  * (if buffer size allows).
886  *
887  * @param mp
888  *   The mempool from which the mbuf is allocated.
889  * @return
890  *   - The pointer to the new mbuf on success.
891  *   - NULL if allocation failed.
892  */
893 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
894 {
895         struct rte_mbuf *m;
896         if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
897                 rte_pktmbuf_reset(m);
898         return m;
899 }
900
901 /**
902  * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
903  * values.
904  *
905  *  @param pool
906  *    The mempool from which mbufs are allocated.
907  *  @param mbufs
908  *    Array of pointers to mbufs
909  *  @param count
910  *    Array size
911  *  @return
912  *   - 0: Success
913  *   - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
914  */
915 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
916          struct rte_mbuf **mbufs, unsigned count)
917 {
918         unsigned idx = 0;
919         int rc;
920
921         rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
922         if (unlikely(rc))
923                 return rc;
924
925         /* To understand duff's device on loop unwinding optimization, see
926          * https://en.wikipedia.org/wiki/Duff's_device.
927          * Here while() loop is used rather than do() while{} to avoid extra
928          * check if count is zero.
929          */
930         switch (count % 4) {
931         case 0:
932                 while (idx != count) {
933                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
934                         rte_pktmbuf_reset(mbufs[idx]);
935                         idx++;
936                         /* fall-through */
937         case 3:
938                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
939                         rte_pktmbuf_reset(mbufs[idx]);
940                         idx++;
941                         /* fall-through */
942         case 2:
943                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
944                         rte_pktmbuf_reset(mbufs[idx]);
945                         idx++;
946                         /* fall-through */
947         case 1:
948                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
949                         rte_pktmbuf_reset(mbufs[idx]);
950                         idx++;
951                         /* fall-through */
952                 }
953         }
954         return 0;
955 }
956
957 /**
958  * Initialize shared data at the end of an external buffer before attaching
959  * to a mbuf by ``rte_pktmbuf_attach_extbuf()``. This is not a mandatory
960  * initialization but a helper function to simply spare a few bytes at the
961  * end of the buffer for shared data. If shared data is allocated
962  * separately, this should not be called but application has to properly
963  * initialize the shared data according to its need.
964  *
965  * Free callback and its argument is saved and the refcnt is set to 1.
966  *
967  * @warning
968  * The value of buf_len will be reduced to RTE_PTR_DIFF(shinfo, buf_addr)
969  * after this initialization. This shall be used for
970  * ``rte_pktmbuf_attach_extbuf()``
971  *
972  * @param buf_addr
973  *   The pointer to the external buffer.
974  * @param [in,out] buf_len
975  *   The pointer to length of the external buffer. Input value must be
976  *   larger than the size of ``struct rte_mbuf_ext_shared_info`` and
977  *   padding for alignment. If not enough, this function will return NULL.
978  *   Adjusted buffer length will be returned through this pointer.
979  * @param free_cb
980  *   Free callback function to call when the external buffer needs to be
981  *   freed.
982  * @param fcb_opaque
983  *   Argument for the free callback function.
984  *
985  * @return
986  *   A pointer to the initialized shared data on success, return NULL
987  *   otherwise.
988  */
989 static inline struct rte_mbuf_ext_shared_info *
990 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
991         rte_mbuf_extbuf_free_callback_t free_cb, void *fcb_opaque)
992 {
993         struct rte_mbuf_ext_shared_info *shinfo;
994         void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
995         void *addr;
996
997         addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
998                                    sizeof(uintptr_t));
999         if (addr <= buf_addr)
1000                 return NULL;
1001
1002         shinfo = (struct rte_mbuf_ext_shared_info *)addr;
1003         shinfo->free_cb = free_cb;
1004         shinfo->fcb_opaque = fcb_opaque;
1005         rte_mbuf_ext_refcnt_set(shinfo, 1);
1006
1007         *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
1008         return shinfo;
1009 }
1010
1011 /**
1012  * Attach an external buffer to a mbuf.
1013  *
1014  * User-managed anonymous buffer can be attached to an mbuf. When attaching
1015  * it, corresponding free callback function and its argument should be
1016  * provided via shinfo. This callback function will be called once all the
1017  * mbufs are detached from the buffer (refcnt becomes zero).
1018  *
1019  * The headroom length of the attaching mbuf will be set to zero and this
1020  * can be properly adjusted after attachment. For example, ``rte_pktmbuf_adj()``
1021  * or ``rte_pktmbuf_reset_headroom()`` might be used.
1022  *
1023  * Similarly, the packet length is initialized to 0. If the buffer contains
1024  * data, the user has to adjust ``data_len`` and the ``pkt_len`` field of
1025  * the mbuf accordingly.
1026  *
1027  * More mbufs can be attached to the same external buffer by
1028  * ``rte_pktmbuf_attach()`` once the external buffer has been attached by
1029  * this API.
1030  *
1031  * Detachment can be done by either ``rte_pktmbuf_detach_extbuf()`` or
1032  * ``rte_pktmbuf_detach()``.
1033  *
1034  * Memory for shared data must be provided and user must initialize all of
1035  * the content properly, especially free callback and refcnt. The pointer
1036  * of shared data will be stored in m->shinfo.
1037  * ``rte_pktmbuf_ext_shinfo_init_helper`` can help to simply spare a few
1038  * bytes at the end of buffer for the shared data, store free callback and
1039  * its argument and set the refcnt to 1. The following is an example:
1040  *
1041  *   struct rte_mbuf_ext_shared_info *shinfo =
1042  *          rte_pktmbuf_ext_shinfo_init_helper(buf_addr, &buf_len,
1043  *                                             free_cb, fcb_arg);
1044  *   rte_pktmbuf_attach_extbuf(m, buf_addr, buf_iova, buf_len, shinfo);
1045  *   rte_pktmbuf_reset_headroom(m);
1046  *   rte_pktmbuf_adj(m, data_len);
1047  *
1048  * Attaching an external buffer is quite similar to mbuf indirection in
1049  * replacing buffer addresses and length of a mbuf, but a few differences:
1050  * - When an indirect mbuf is attached, refcnt of the direct mbuf would be
1051  *   2 as long as the direct mbuf itself isn't freed after the attachment.
1052  *   In such cases, the buffer area of a direct mbuf must be read-only. But
1053  *   external buffer has its own refcnt and it starts from 1. Unless
1054  *   multiple mbufs are attached to a mbuf having an external buffer, the
1055  *   external buffer is writable.
1056  * - There's no need to allocate buffer from a mempool. Any buffer can be
1057  *   attached with appropriate free callback and its IO address.
1058  * - Smaller metadata is required to maintain shared data such as refcnt.
1059  *
1060  * @param m
1061  *   The pointer to the mbuf.
1062  * @param buf_addr
1063  *   The pointer to the external buffer.
1064  * @param buf_iova
1065  *   IO address of the external buffer.
1066  * @param buf_len
1067  *   The size of the external buffer.
1068  * @param shinfo
1069  *   User-provided memory for shared data of the external buffer.
1070  */
1071 static inline void
1072 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1073         rte_iova_t buf_iova, uint16_t buf_len,
1074         struct rte_mbuf_ext_shared_info *shinfo)
1075 {
1076         /* mbuf should not be read-only */
1077         RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1078         RTE_ASSERT(shinfo->free_cb != NULL);
1079
1080         m->buf_addr = buf_addr;
1081         m->buf_iova = buf_iova;
1082         m->buf_len = buf_len;
1083
1084         m->data_len = 0;
1085         m->data_off = 0;
1086
1087         m->ol_flags |= EXT_ATTACHED_MBUF;
1088         m->shinfo = shinfo;
1089 }
1090
1091 /**
1092  * Detach the external buffer attached to a mbuf, same as
1093  * ``rte_pktmbuf_detach()``
1094  *
1095  * @param m
1096  *   The mbuf having external buffer.
1097  */
1098 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1099
1100 /**
1101  * Copy dynamic fields from msrc to mdst.
1102  *
1103  * @param mdst
1104  *   The destination mbuf.
1105  * @param msrc
1106  *   The source mbuf.
1107  */
1108 static inline void
1109 rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1110 {
1111         memcpy(&mdst->dynfield1, msrc->dynfield1, sizeof(mdst->dynfield1));
1112 }
1113
1114 /* internal */
1115 static inline void
1116 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1117 {
1118         mdst->port = msrc->port;
1119         mdst->vlan_tci = msrc->vlan_tci;
1120         mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1121         mdst->tx_offload = msrc->tx_offload;
1122         mdst->hash = msrc->hash;
1123         mdst->packet_type = msrc->packet_type;
1124         mdst->timestamp = msrc->timestamp;
1125         rte_mbuf_dynfield_copy(mdst, msrc);
1126 }
1127
1128 /**
1129  * Attach packet mbuf to another packet mbuf.
1130  *
1131  * If the mbuf we are attaching to isn't a direct buffer and is attached to
1132  * an external buffer, the mbuf being attached will be attached to the
1133  * external buffer instead of mbuf indirection.
1134  *
1135  * Otherwise, the mbuf will be indirectly attached. After attachment we
1136  * refer the mbuf we attached as 'indirect', while mbuf we attached to as
1137  * 'direct'.  The direct mbuf's reference counter is incremented.
1138  *
1139  * Right now, not supported:
1140  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
1141  *  - mbuf we trying to attach (mi) is used by someone else
1142  *    e.g. it's reference counter is greater then 1.
1143  *
1144  * @param mi
1145  *   The indirect packet mbuf.
1146  * @param m
1147  *   The packet mbuf we're attaching to.
1148  */
1149 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1150 {
1151         RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1152             rte_mbuf_refcnt_read(mi) == 1);
1153
1154         if (RTE_MBUF_HAS_EXTBUF(m)) {
1155                 rte_mbuf_ext_refcnt_update(m->shinfo, 1);
1156                 mi->ol_flags = m->ol_flags;
1157                 mi->shinfo = m->shinfo;
1158         } else {
1159                 /* if m is not direct, get the mbuf that embeds the data */
1160                 rte_mbuf_refcnt_update(rte_mbuf_from_indirect(m), 1);
1161                 mi->priv_size = m->priv_size;
1162                 mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1163         }
1164
1165         __rte_pktmbuf_copy_hdr(mi, m);
1166
1167         mi->data_off = m->data_off;
1168         mi->data_len = m->data_len;
1169         mi->buf_iova = m->buf_iova;
1170         mi->buf_addr = m->buf_addr;
1171         mi->buf_len = m->buf_len;
1172
1173         mi->next = NULL;
1174         mi->pkt_len = mi->data_len;
1175         mi->nb_segs = 1;
1176
1177         __rte_mbuf_sanity_check(mi, 1);
1178         __rte_mbuf_sanity_check(m, 0);
1179 }
1180
1181 /**
1182  * @internal used by rte_pktmbuf_detach().
1183  *
1184  * Decrement the reference counter of the external buffer. When the
1185  * reference counter becomes 0, the buffer is freed by pre-registered
1186  * callback.
1187  */
1188 static inline void
1189 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1190 {
1191         RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1192         RTE_ASSERT(m->shinfo != NULL);
1193
1194         if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1195                 m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1196 }
1197
1198 /**
1199  * @internal used by rte_pktmbuf_detach().
1200  *
1201  * Decrement the direct mbuf's reference counter. When the reference
1202  * counter becomes 0, the direct mbuf is freed.
1203  */
1204 static inline void
1205 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1206 {
1207         struct rte_mbuf *md;
1208
1209         RTE_ASSERT(RTE_MBUF_CLONED(m));
1210
1211         md = rte_mbuf_from_indirect(m);
1212
1213         if (rte_mbuf_refcnt_update(md, -1) == 0) {
1214                 md->next = NULL;
1215                 md->nb_segs = 1;
1216                 rte_mbuf_refcnt_set(md, 1);
1217                 rte_mbuf_raw_free(md);
1218         }
1219 }
1220
1221 /**
1222  * Detach a packet mbuf from external buffer or direct buffer.
1223  *
1224  *  - decrement refcnt and free the external/direct buffer if refcnt
1225  *    becomes zero.
1226  *  - restore original mbuf address and length values.
1227  *  - reset pktmbuf data and data_len to their default values.
1228  *
1229  * All other fields of the given packet mbuf will be left intact.
1230  *
1231  * If the packet mbuf was allocated from the pool with pinned
1232  * external buffers the rte_pktmbuf_detach does nothing with the
1233  * mbuf of this kind, because the pinned buffers are not supposed
1234  * to be detached.
1235  *
1236  * @param m
1237  *   The indirect attached packet mbuf.
1238  */
1239 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1240 {
1241         struct rte_mempool *mp = m->pool;
1242         uint32_t mbuf_size, buf_len;
1243         uint16_t priv_size;
1244
1245         if (RTE_MBUF_HAS_EXTBUF(m)) {
1246                 /*
1247                  * The mbuf has the external attached buffer,
1248                  * we should check the type of the memory pool where
1249                  * the mbuf was allocated from to detect the pinned
1250                  * external buffer.
1251                  */
1252                 uint32_t flags = rte_pktmbuf_priv_flags(mp);
1253
1254                 if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
1255                         /*
1256                          * The pinned external buffer should not be
1257                          * detached from its backing mbuf, just exit.
1258                          */
1259                         return;
1260                 }
1261                 __rte_pktmbuf_free_extbuf(m);
1262         } else {
1263                 __rte_pktmbuf_free_direct(m);
1264         }
1265         priv_size = rte_pktmbuf_priv_size(mp);
1266         mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1267         buf_len = rte_pktmbuf_data_room_size(mp);
1268
1269         m->priv_size = priv_size;
1270         m->buf_addr = (char *)m + mbuf_size;
1271         m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1272         m->buf_len = (uint16_t)buf_len;
1273         rte_pktmbuf_reset_headroom(m);
1274         m->data_len = 0;
1275         m->ol_flags = 0;
1276 }
1277
1278 /**
1279  * @internal Handle the packet mbufs with attached pinned external buffer
1280  * on the mbuf freeing:
1281  *
1282  *  - return zero if reference counter in shinfo is one. It means there is
1283  *  no more reference to this pinned buffer and mbuf can be returned to
1284  *  the pool
1285  *
1286  *  - otherwise (if reference counter is not one), decrement reference
1287  *  counter and return non-zero value to prevent freeing the backing mbuf.
1288  *
1289  * Returns non zero if mbuf should not be freed.
1290  */
1291 static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)
1292 {
1293         struct rte_mbuf_ext_shared_info *shinfo;
1294
1295         /* Clear flags, mbuf is being freed. */
1296         m->ol_flags = EXT_ATTACHED_MBUF;
1297         shinfo = m->shinfo;
1298
1299         /* Optimize for performance - do not dec/reinit */
1300         if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1))
1301                 return 0;
1302
1303         /*
1304          * Direct usage of add primitive to avoid
1305          * duplication of comparing with one.
1306          */
1307         if (likely(rte_atomic16_add_return
1308                         (&shinfo->refcnt_atomic, -1)))
1309                 return 1;
1310
1311         /* Reinitialize counter before mbuf freeing. */
1312         rte_mbuf_ext_refcnt_set(shinfo, 1);
1313         return 0;
1314 }
1315
1316 /**
1317  * Decrease reference counter and unlink a mbuf segment
1318  *
1319  * This function does the same than a free, except that it does not
1320  * return the segment to its pool.
1321  * It decreases the reference counter, and if it reaches 0, it is
1322  * detached from its parent for an indirect mbuf.
1323  *
1324  * @param m
1325  *   The mbuf to be unlinked
1326  * @return
1327  *   - (m) if it is the last reference. It can be recycled or freed.
1328  *   - (NULL) if the mbuf still has remaining references on it.
1329  */
1330 static __rte_always_inline struct rte_mbuf *
1331 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1332 {
1333         __rte_mbuf_sanity_check(m, 0);
1334
1335         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1336
1337                 if (!RTE_MBUF_DIRECT(m)) {
1338                         rte_pktmbuf_detach(m);
1339                         if (RTE_MBUF_HAS_EXTBUF(m) &&
1340                             RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1341                             __rte_pktmbuf_pinned_extbuf_decref(m))
1342                                 return NULL;
1343                 }
1344
1345                 if (m->next != NULL) {
1346                         m->next = NULL;
1347                         m->nb_segs = 1;
1348                 }
1349
1350                 return m;
1351
1352         } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1353
1354                 if (!RTE_MBUF_DIRECT(m)) {
1355                         rte_pktmbuf_detach(m);
1356                         if (RTE_MBUF_HAS_EXTBUF(m) &&
1357                             RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1358                             __rte_pktmbuf_pinned_extbuf_decref(m))
1359                                 return NULL;
1360                 }
1361
1362                 if (m->next != NULL) {
1363                         m->next = NULL;
1364                         m->nb_segs = 1;
1365                 }
1366                 rte_mbuf_refcnt_set(m, 1);
1367
1368                 return m;
1369         }
1370         return NULL;
1371 }
1372
1373 /**
1374  * Free a segment of a packet mbuf into its original mempool.
1375  *
1376  * Free an mbuf, without parsing other segments in case of chained
1377  * buffers.
1378  *
1379  * @param m
1380  *   The packet mbuf segment to be freed.
1381  */
1382 static __rte_always_inline void
1383 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1384 {
1385         m = rte_pktmbuf_prefree_seg(m);
1386         if (likely(m != NULL))
1387                 rte_mbuf_raw_free(m);
1388 }
1389
1390 /**
1391  * Free a packet mbuf back into its original mempool.
1392  *
1393  * Free an mbuf, and all its segments in case of chained buffers. Each
1394  * segment is added back into its original mempool.
1395  *
1396  * @param m
1397  *   The packet mbuf to be freed. If NULL, the function does nothing.
1398  */
1399 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1400 {
1401         struct rte_mbuf *m_next;
1402
1403         if (m != NULL)
1404                 __rte_mbuf_sanity_check(m, 1);
1405
1406         while (m != NULL) {
1407                 m_next = m->next;
1408                 rte_pktmbuf_free_seg(m);
1409                 m = m_next;
1410         }
1411 }
1412
1413 /**
1414  * Free a bulk of packet mbufs back into their original mempools.
1415  *
1416  * Free a bulk of mbufs, and all their segments in case of chained buffers.
1417  * Each segment is added back into its original mempool.
1418  *
1419  *  @param mbufs
1420  *    Array of pointers to packet mbufs.
1421  *    The array may contain NULL pointers.
1422  *  @param count
1423  *    Array size.
1424  */
1425 __rte_experimental
1426 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1427
1428 /**
1429  * Create a "clone" of the given packet mbuf.
1430  *
1431  * Walks through all segments of the given packet mbuf, and for each of them:
1432  *  - Creates a new packet mbuf from the given pool.
1433  *  - Attaches newly created mbuf to the segment.
1434  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1435  * from the original packet mbuf.
1436  *
1437  * @param md
1438  *   The packet mbuf to be cloned.
1439  * @param mp
1440  *   The mempool from which the "clone" mbufs are allocated.
1441  * @return
1442  *   - The pointer to the new "clone" mbuf on success.
1443  *   - NULL if allocation fails.
1444  */
1445 struct rte_mbuf *
1446 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1447
1448 /**
1449  * Create a full copy of a given packet mbuf.
1450  *
1451  * Copies all the data from a given packet mbuf to a newly allocated
1452  * set of mbufs. The private data are is not copied.
1453  *
1454  * @param m
1455  *   The packet mbuf to be copiedd.
1456  * @param mp
1457  *   The mempool from which the "clone" mbufs are allocated.
1458  * @param offset
1459  *   The number of bytes to skip before copying.
1460  *   If the mbuf does not have that many bytes, it is an error
1461  *   and NULL is returned.
1462  * @param length
1463  *   The upper limit on bytes to copy.  Passing UINT32_MAX
1464  *   means all data (after offset).
1465  * @return
1466  *   - The pointer to the new "clone" mbuf on success.
1467  *   - NULL if allocation fails.
1468  */
1469 __rte_experimental
1470 struct rte_mbuf *
1471 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1472                  uint32_t offset, uint32_t length);
1473
1474 /**
1475  * Adds given value to the refcnt of all packet mbuf segments.
1476  *
1477  * Walks through all segments of given packet mbuf and for each of them
1478  * invokes rte_mbuf_refcnt_update().
1479  *
1480  * @param m
1481  *   The packet mbuf whose refcnt to be updated.
1482  * @param v
1483  *   The value to add to the mbuf's segments refcnt.
1484  */
1485 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1486 {
1487         __rte_mbuf_sanity_check(m, 1);
1488
1489         do {
1490                 rte_mbuf_refcnt_update(m, v);
1491         } while ((m = m->next) != NULL);
1492 }
1493
1494 /**
1495  * Get the headroom in a packet mbuf.
1496  *
1497  * @param m
1498  *   The packet mbuf.
1499  * @return
1500  *   The length of the headroom.
1501  */
1502 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1503 {
1504         __rte_mbuf_sanity_check(m, 0);
1505         return m->data_off;
1506 }
1507
1508 /**
1509  * Get the tailroom of a packet mbuf.
1510  *
1511  * @param m
1512  *   The packet mbuf.
1513  * @return
1514  *   The length of the tailroom.
1515  */
1516 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1517 {
1518         __rte_mbuf_sanity_check(m, 0);
1519         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1520                           m->data_len);
1521 }
1522
1523 /**
1524  * Get the last segment of the packet.
1525  *
1526  * @param m
1527  *   The packet mbuf.
1528  * @return
1529  *   The last segment of the given mbuf.
1530  */
1531 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1532 {
1533         __rte_mbuf_sanity_check(m, 1);
1534         while (m->next != NULL)
1535                 m = m->next;
1536         return m;
1537 }
1538
1539 /* deprecated */
1540 #define rte_pktmbuf_mtophys_offset(m, o) \
1541         rte_pktmbuf_iova_offset(m, o)
1542
1543 /* deprecated */
1544 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_iova(m)
1545
1546 /**
1547  * A macro that returns the length of the packet.
1548  *
1549  * The value can be read or assigned.
1550  *
1551  * @param m
1552  *   The packet mbuf.
1553  */
1554 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1555
1556 /**
1557  * A macro that returns the length of the segment.
1558  *
1559  * The value can be read or assigned.
1560  *
1561  * @param m
1562  *   The packet mbuf.
1563  */
1564 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1565
1566 /**
1567  * Prepend len bytes to an mbuf data area.
1568  *
1569  * Returns a pointer to the new
1570  * data start address. If there is not enough headroom in the first
1571  * segment, the function will return NULL, without modifying the mbuf.
1572  *
1573  * @param m
1574  *   The pkt mbuf.
1575  * @param len
1576  *   The amount of data to prepend (in bytes).
1577  * @return
1578  *   A pointer to the start of the newly prepended data, or
1579  *   NULL if there is not enough headroom space in the first segment
1580  */
1581 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1582                                         uint16_t len)
1583 {
1584         __rte_mbuf_sanity_check(m, 1);
1585
1586         if (unlikely(len > rte_pktmbuf_headroom(m)))
1587                 return NULL;
1588
1589         /* NB: elaborating the subtraction like this instead of using
1590          *     -= allows us to ensure the result type is uint16_t
1591          *     avoiding compiler warnings on gcc 8.1 at least */
1592         m->data_off = (uint16_t)(m->data_off - len);
1593         m->data_len = (uint16_t)(m->data_len + len);
1594         m->pkt_len  = (m->pkt_len + len);
1595
1596         return (char *)m->buf_addr + m->data_off;
1597 }
1598
1599 /**
1600  * Append len bytes to an mbuf.
1601  *
1602  * Append len bytes to an mbuf and return a pointer to the start address
1603  * of the added data. If there is not enough tailroom in the last
1604  * segment, the function will return NULL, without modifying the mbuf.
1605  *
1606  * @param m
1607  *   The packet mbuf.
1608  * @param len
1609  *   The amount of data to append (in bytes).
1610  * @return
1611  *   A pointer to the start of the newly appended data, or
1612  *   NULL if there is not enough tailroom space in the last segment
1613  */
1614 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1615 {
1616         void *tail;
1617         struct rte_mbuf *m_last;
1618
1619         __rte_mbuf_sanity_check(m, 1);
1620
1621         m_last = rte_pktmbuf_lastseg(m);
1622         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1623                 return NULL;
1624
1625         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1626         m_last->data_len = (uint16_t)(m_last->data_len + len);
1627         m->pkt_len  = (m->pkt_len + len);
1628         return (char*) tail;
1629 }
1630
1631 /**
1632  * Remove len bytes at the beginning of an mbuf.
1633  *
1634  * Returns a pointer to the start address of the new data area. If the
1635  * length is greater than the length of the first segment, then the
1636  * function will fail and return NULL, without modifying the mbuf.
1637  *
1638  * @param m
1639  *   The packet mbuf.
1640  * @param len
1641  *   The amount of data to remove (in bytes).
1642  * @return
1643  *   A pointer to the new start of the data.
1644  */
1645 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1646 {
1647         __rte_mbuf_sanity_check(m, 1);
1648
1649         if (unlikely(len > m->data_len))
1650                 return NULL;
1651
1652         /* NB: elaborating the addition like this instead of using
1653          *     += allows us to ensure the result type is uint16_t
1654          *     avoiding compiler warnings on gcc 8.1 at least */
1655         m->data_len = (uint16_t)(m->data_len - len);
1656         m->data_off = (uint16_t)(m->data_off + len);
1657         m->pkt_len  = (m->pkt_len - len);
1658         return (char *)m->buf_addr + m->data_off;
1659 }
1660
1661 /**
1662  * Remove len bytes of data at the end of the mbuf.
1663  *
1664  * If the length is greater than the length of the last segment, the
1665  * function will fail and return -1 without modifying the mbuf.
1666  *
1667  * @param m
1668  *   The packet mbuf.
1669  * @param len
1670  *   The amount of data to remove (in bytes).
1671  * @return
1672  *   - 0: On success.
1673  *   - -1: On error.
1674  */
1675 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1676 {
1677         struct rte_mbuf *m_last;
1678
1679         __rte_mbuf_sanity_check(m, 1);
1680
1681         m_last = rte_pktmbuf_lastseg(m);
1682         if (unlikely(len > m_last->data_len))
1683                 return -1;
1684
1685         m_last->data_len = (uint16_t)(m_last->data_len - len);
1686         m->pkt_len  = (m->pkt_len - len);
1687         return 0;
1688 }
1689
1690 /**
1691  * Test if mbuf data is contiguous.
1692  *
1693  * @param m
1694  *   The packet mbuf.
1695  * @return
1696  *   - 1, if all data is contiguous (one segment).
1697  *   - 0, if there is several segments.
1698  */
1699 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1700 {
1701         __rte_mbuf_sanity_check(m, 1);
1702         return m->nb_segs == 1;
1703 }
1704
1705 /**
1706  * @internal used by rte_pktmbuf_read().
1707  */
1708 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1709         uint32_t len, void *buf);
1710
1711 /**
1712  * Read len data bytes in a mbuf at specified offset.
1713  *
1714  * If the data is contiguous, return the pointer in the mbuf data, else
1715  * copy the data in the buffer provided by the user and return its
1716  * pointer.
1717  *
1718  * @param m
1719  *   The pointer to the mbuf.
1720  * @param off
1721  *   The offset of the data in the mbuf.
1722  * @param len
1723  *   The amount of bytes to read.
1724  * @param buf
1725  *   The buffer where data is copied if it is not contiguous in mbuf
1726  *   data. Its length should be at least equal to the len parameter.
1727  * @return
1728  *   The pointer to the data, either in the mbuf if it is contiguous,
1729  *   or in the user buffer. If mbuf is too small, NULL is returned.
1730  */
1731 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1732         uint32_t off, uint32_t len, void *buf)
1733 {
1734         if (likely(off + len <= rte_pktmbuf_data_len(m)))
1735                 return rte_pktmbuf_mtod_offset(m, char *, off);
1736         else
1737                 return __rte_pktmbuf_read(m, off, len, buf);
1738 }
1739
1740 /**
1741  * Chain an mbuf to another, thereby creating a segmented packet.
1742  *
1743  * Note: The implementation will do a linear walk over the segments to find
1744  * the tail entry. For cases when there are many segments, it's better to
1745  * chain the entries manually.
1746  *
1747  * @param head
1748  *   The head of the mbuf chain (the first packet)
1749  * @param tail
1750  *   The mbuf to put last in the chain
1751  *
1752  * @return
1753  *   - 0, on success.
1754  *   - -EOVERFLOW, if the chain segment limit exceeded
1755  */
1756 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1757 {
1758         struct rte_mbuf *cur_tail;
1759
1760         /* Check for number-of-segments-overflow */
1761         if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1762                 return -EOVERFLOW;
1763
1764         /* Chain 'tail' onto the old tail */
1765         cur_tail = rte_pktmbuf_lastseg(head);
1766         cur_tail->next = tail;
1767
1768         /* accumulate number of segments and total length.
1769          * NB: elaborating the addition like this instead of using
1770          *     -= allows us to ensure the result type is uint16_t
1771          *     avoiding compiler warnings on gcc 8.1 at least */
1772         head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1773         head->pkt_len += tail->pkt_len;
1774
1775         /* pkt_len is only set in the head */
1776         tail->pkt_len = tail->data_len;
1777
1778         return 0;
1779 }
1780
1781 /*
1782  * @warning
1783  * @b EXPERIMENTAL: This API may change without prior notice.
1784  *
1785  * For given input values generate raw tx_offload value.
1786  * Note that it is caller responsibility to make sure that input parameters
1787  * don't exceed maximum bit-field values.
1788  * @param il2
1789  *   l2_len value.
1790  * @param il3
1791  *   l3_len value.
1792  * @param il4
1793  *   l4_len value.
1794  * @param tso
1795  *   tso_segsz value.
1796  * @param ol3
1797  *   outer_l3_len value.
1798  * @param ol2
1799  *   outer_l2_len value.
1800  * @param unused
1801  *   unused value.
1802  * @return
1803  *   raw tx_offload value.
1804  */
1805 static __rte_always_inline uint64_t
1806 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1807         uint64_t ol3, uint64_t ol2, uint64_t unused)
1808 {
1809         return il2 << RTE_MBUF_L2_LEN_OFS |
1810                 il3 << RTE_MBUF_L3_LEN_OFS |
1811                 il4 << RTE_MBUF_L4_LEN_OFS |
1812                 tso << RTE_MBUF_TSO_SEGSZ_OFS |
1813                 ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1814                 ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1815                 unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1816 }
1817
1818 /**
1819  * Validate general requirements for Tx offload in mbuf.
1820  *
1821  * This function checks correctness and completeness of Tx offload settings.
1822  *
1823  * @param m
1824  *   The packet mbuf to be validated.
1825  * @return
1826  *   0 if packet is valid
1827  */
1828 static inline int
1829 rte_validate_tx_offload(const struct rte_mbuf *m)
1830 {
1831         uint64_t ol_flags = m->ol_flags;
1832
1833         /* Does packet set any of available offloads? */
1834         if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1835                 return 0;
1836
1837         /* IP checksum can be counted only for IPv4 packet */
1838         if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1839                 return -EINVAL;
1840
1841         /* IP type not set when required */
1842         if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1843                 if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1844                         return -EINVAL;
1845
1846         /* Check requirements for TSO packet */
1847         if (ol_flags & PKT_TX_TCP_SEG)
1848                 if ((m->tso_segsz == 0) ||
1849                                 ((ol_flags & PKT_TX_IPV4) &&
1850                                 !(ol_flags & PKT_TX_IP_CKSUM)))
1851                         return -EINVAL;
1852
1853         /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1854         if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1855                         !(ol_flags & PKT_TX_OUTER_IPV4))
1856                 return -EINVAL;
1857
1858         return 0;
1859 }
1860
1861 /**
1862  * @internal used by rte_pktmbuf_linearize().
1863  */
1864 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1865
1866 /**
1867  * Linearize data in mbuf.
1868  *
1869  * This function moves the mbuf data in the first segment if there is enough
1870  * tailroom. The subsequent segments are unchained and freed.
1871  *
1872  * @param mbuf
1873  *   mbuf to linearize
1874  * @return
1875  *   - 0, on success
1876  *   - -1, on error
1877  */
1878 static inline int
1879 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1880 {
1881         if (rte_pktmbuf_is_contiguous(mbuf))
1882                 return 0;
1883         return __rte_pktmbuf_linearize(mbuf);
1884 }
1885
1886 /**
1887  * Dump an mbuf structure to a file.
1888  *
1889  * Dump all fields for the given packet mbuf and all its associated
1890  * segments (in the case of a chained buffer).
1891  *
1892  * @param f
1893  *   A pointer to a file for output
1894  * @param m
1895  *   The packet mbuf.
1896  * @param dump_len
1897  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1898  *   the packet.
1899  */
1900 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1901
1902 /**
1903  * Get the value of mbuf sched queue_id field.
1904  */
1905 static inline uint32_t
1906 rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
1907 {
1908         return m->hash.sched.queue_id;
1909 }
1910
1911 /**
1912  * Get the value of mbuf sched traffic_class field.
1913  */
1914 static inline uint8_t
1915 rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
1916 {
1917         return m->hash.sched.traffic_class;
1918 }
1919
1920 /**
1921  * Get the value of mbuf sched color field.
1922  */
1923 static inline uint8_t
1924 rte_mbuf_sched_color_get(const struct rte_mbuf *m)
1925 {
1926         return m->hash.sched.color;
1927 }
1928
1929 /**
1930  * Get the values of mbuf sched queue_id, traffic_class and color.
1931  *
1932  * @param m
1933  *   Mbuf to read
1934  * @param queue_id
1935  *  Returns the queue id
1936  * @param traffic_class
1937  *  Returns the traffic class id
1938  * @param color
1939  *  Returns the colour id
1940  */
1941 static inline void
1942 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1943                         uint8_t *traffic_class,
1944                         uint8_t *color)
1945 {
1946         struct rte_mbuf_sched sched = m->hash.sched;
1947
1948         *queue_id = sched.queue_id;
1949         *traffic_class = sched.traffic_class;
1950         *color = sched.color;
1951 }
1952
1953 /**
1954  * Set the mbuf sched queue_id to the defined value.
1955  */
1956 static inline void
1957 rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
1958 {
1959         m->hash.sched.queue_id = queue_id;
1960 }
1961
1962 /**
1963  * Set the mbuf sched traffic_class id to the defined value.
1964  */
1965 static inline void
1966 rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
1967 {
1968         m->hash.sched.traffic_class = traffic_class;
1969 }
1970
1971 /**
1972  * Set the mbuf sched color id to the defined value.
1973  */
1974 static inline void
1975 rte_mbuf_sched_color_set(struct rte_mbuf *m, uint8_t color)
1976 {
1977         m->hash.sched.color = color;
1978 }
1979
1980 /**
1981  * Set the mbuf sched queue_id, traffic_class and color.
1982  *
1983  * @param m
1984  *   Mbuf to set
1985  * @param queue_id
1986  *  Queue id value to be set
1987  * @param traffic_class
1988  *  Traffic class id value to be set
1989  * @param color
1990  *  Color id to be set
1991  */
1992 static inline void
1993 rte_mbuf_sched_set(struct rte_mbuf *m, uint32_t queue_id,
1994                         uint8_t traffic_class,
1995                         uint8_t color)
1996 {
1997         m->hash.sched = (struct rte_mbuf_sched){
1998                                 .queue_id = queue_id,
1999                                 .traffic_class = traffic_class,
2000                                 .color = color,
2001                                 .reserved = 0,
2002                         };
2003 }
2004
2005 #ifdef __cplusplus
2006 }
2007 #endif
2008
2009 #endif /* _RTE_MBUF_H_ */