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