bd26764a2526d50afc80e22284bdddf917cdd582
[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 };
307
308 #ifdef RTE_LIBRTE_MBUF_DEBUG
309
310 /**  check mbuf type in debug mode */
311 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
312
313 #else /*  RTE_LIBRTE_MBUF_DEBUG */
314
315 /**  check mbuf type in debug mode */
316 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
317
318 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
319
320 #ifdef RTE_MBUF_REFCNT_ATOMIC
321
322 /**
323  * Reads the value of an mbuf's refcnt.
324  * @param m
325  *   Mbuf to read
326  * @return
327  *   Reference count number.
328  */
329 static inline uint16_t
330 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
331 {
332         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
333 }
334
335 /**
336  * Sets an mbuf's refcnt to a defined value.
337  * @param m
338  *   Mbuf to update
339  * @param new_value
340  *   Value set
341  */
342 static inline void
343 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
344 {
345         rte_atomic16_set(&m->refcnt_atomic, (int16_t)new_value);
346 }
347
348 /* internal */
349 static inline uint16_t
350 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
351 {
352         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
353 }
354
355 /**
356  * Adds given value to an mbuf's refcnt and returns its new value.
357  * @param m
358  *   Mbuf to update
359  * @param value
360  *   Value to add/subtract
361  * @return
362  *   Updated value
363  */
364 static inline uint16_t
365 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
366 {
367         /*
368          * The atomic_add is an expensive operation, so we don't want to
369          * call it in the case where we know we are the unique holder of
370          * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
371          * operation has to be used because concurrent accesses on the
372          * reference counter can occur.
373          */
374         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
375                 ++value;
376                 rte_mbuf_refcnt_set(m, (uint16_t)value);
377                 return (uint16_t)value;
378         }
379
380         return __rte_mbuf_refcnt_update(m, value);
381 }
382
383 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
384
385 /* internal */
386 static inline uint16_t
387 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
388 {
389         m->refcnt = (uint16_t)(m->refcnt + value);
390         return m->refcnt;
391 }
392
393 /**
394  * Adds given value to an mbuf's refcnt and returns its new value.
395  */
396 static inline uint16_t
397 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
398 {
399         return __rte_mbuf_refcnt_update(m, value);
400 }
401
402 /**
403  * Reads the value of an mbuf's refcnt.
404  */
405 static inline uint16_t
406 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
407 {
408         return m->refcnt;
409 }
410
411 /**
412  * Sets an mbuf's refcnt to the defined value.
413  */
414 static inline void
415 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
416 {
417         m->refcnt = new_value;
418 }
419
420 #endif /* RTE_MBUF_REFCNT_ATOMIC */
421
422 /**
423  * Reads the refcnt of an external buffer.
424  *
425  * @param shinfo
426  *   Shared data of the external buffer.
427  * @return
428  *   Reference count number.
429  */
430 static inline uint16_t
431 rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
432 {
433         return (uint16_t)(rte_atomic16_read(&shinfo->refcnt_atomic));
434 }
435
436 /**
437  * Set refcnt of an external buffer.
438  *
439  * @param shinfo
440  *   Shared data of the external buffer.
441  * @param new_value
442  *   Value set
443  */
444 static inline void
445 rte_mbuf_ext_refcnt_set(struct rte_mbuf_ext_shared_info *shinfo,
446         uint16_t new_value)
447 {
448         rte_atomic16_set(&shinfo->refcnt_atomic, (int16_t)new_value);
449 }
450
451 /**
452  * Add given value to refcnt of an external buffer and return its new
453  * value.
454  *
455  * @param shinfo
456  *   Shared data of the external buffer.
457  * @param value
458  *   Value to add/subtract
459  * @return
460  *   Updated value
461  */
462 static inline uint16_t
463 rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo,
464         int16_t value)
465 {
466         if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
467                 ++value;
468                 rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
469                 return (uint16_t)value;
470         }
471
472         return (uint16_t)rte_atomic16_add_return(&shinfo->refcnt_atomic, value);
473 }
474
475 /** Mbuf prefetch */
476 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
477         if ((m) != NULL)                        \
478                 rte_prefetch0(m);               \
479 } while (0)
480
481
482 /**
483  * Sanity checks on an mbuf.
484  *
485  * Check the consistency of the given mbuf. The function will cause a
486  * panic if corruption is detected.
487  *
488  * @param m
489  *   The mbuf to be checked.
490  * @param is_header
491  *   True if the mbuf is a packet header, false if it is a sub-segment
492  *   of a packet (in this case, some fields like nb_segs are not checked)
493  */
494 void
495 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
496
497 /**
498  * Sanity checks on a mbuf.
499  *
500  * Almost like rte_mbuf_sanity_check(), but this function gives the reason
501  * if corruption is detected rather than panic.
502  *
503  * @param m
504  *   The mbuf to be checked.
505  * @param is_header
506  *   True if the mbuf is a packet header, false if it is a sub-segment
507  *   of a packet (in this case, some fields like nb_segs are not checked)
508  * @param reason
509  *   A reference to a string pointer where to store the reason why a mbuf is
510  *   considered invalid.
511  * @return
512  *   - 0 if no issue has been found, reason is left untouched.
513  *   - -1 if a problem is detected, reason then points to a string describing
514  *     the reason why the mbuf is deemed invalid.
515  */
516 __rte_experimental
517 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
518                    const char **reason);
519
520 #define MBUF_RAW_ALLOC_CHECK(m) do {                            \
521         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);               \
522         RTE_ASSERT((m)->next == NULL);                          \
523         RTE_ASSERT((m)->nb_segs == 1);                          \
524         __rte_mbuf_sanity_check(m, 0);                          \
525 } while (0)
526
527 /**
528  * Allocate an uninitialized mbuf from mempool *mp*.
529  *
530  * This function can be used by PMDs (especially in RX functions) to
531  * allocate an uninitialized mbuf. The driver is responsible of
532  * initializing all the required fields. See rte_pktmbuf_reset().
533  * For standard needs, prefer rte_pktmbuf_alloc().
534  *
535  * The caller can expect that the following fields of the mbuf structure
536  * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
537  * next=NULL, pool, priv_size. The other fields must be initialized
538  * by the caller.
539  *
540  * @param mp
541  *   The mempool from which mbuf is allocated.
542  * @return
543  *   - The pointer to the new mbuf on success.
544  *   - NULL if allocation failed.
545  */
546 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
547 {
548         struct rte_mbuf *m;
549
550         if (rte_mempool_get(mp, (void **)&m) < 0)
551                 return NULL;
552         MBUF_RAW_ALLOC_CHECK(m);
553         return m;
554 }
555
556 /**
557  * Put mbuf back into its original mempool.
558  *
559  * The caller must ensure that the mbuf is direct and properly
560  * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
561  * rte_pktmbuf_prefree_seg().
562  *
563  * This function should be used with care, when optimization is
564  * required. For standard needs, prefer rte_pktmbuf_free() or
565  * rte_pktmbuf_free_seg().
566  *
567  * @param m
568  *   The mbuf to be freed.
569  */
570 static __rte_always_inline void
571 rte_mbuf_raw_free(struct rte_mbuf *m)
572 {
573         RTE_ASSERT(RTE_MBUF_DIRECT(m));
574         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
575         RTE_ASSERT(m->next == NULL);
576         RTE_ASSERT(m->nb_segs == 1);
577         __rte_mbuf_sanity_check(m, 0);
578         rte_mempool_put(m->pool, m);
579 }
580
581 /**
582  * The packet mbuf constructor.
583  *
584  * This function initializes some fields in the mbuf structure that are
585  * not modified by the user once created (origin pool, buffer start
586  * address, and so on). This function is given as a callback function to
587  * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
588  *
589  * @param mp
590  *   The mempool from which mbufs originate.
591  * @param opaque_arg
592  *   A pointer that can be used by the user to retrieve useful information
593  *   for mbuf initialization. This pointer is the opaque argument passed to
594  *   rte_mempool_obj_iter() or rte_mempool_create().
595  * @param m
596  *   The mbuf to initialize.
597  * @param i
598  *   The index of the mbuf in the pool table.
599  */
600 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
601                       void *m, unsigned i);
602
603
604 /**
605  * A  packet mbuf pool constructor.
606  *
607  * This function initializes the mempool private data in the case of a
608  * pktmbuf pool. This private data is needed by the driver. The
609  * function must be called on the mempool before it is used, or it
610  * can be given as a callback function to rte_mempool_create() at
611  * pool creation. It can be extended by the user, for example, to
612  * provide another packet size.
613  *
614  * @param mp
615  *   The mempool from which mbufs originate.
616  * @param opaque_arg
617  *   A pointer that can be used by the user to retrieve useful information
618  *   for mbuf initialization. This pointer is the opaque argument passed to
619  *   rte_mempool_create().
620  */
621 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
622
623 /**
624  * Create a mbuf pool.
625  *
626  * This function creates and initializes a packet mbuf pool. It is
627  * a wrapper to rte_mempool functions.
628  *
629  * @param name
630  *   The name of the mbuf pool.
631  * @param n
632  *   The number of elements in the mbuf pool. The optimum size (in terms
633  *   of memory usage) for a mempool is when n is a power of two minus one:
634  *   n = (2^q - 1).
635  * @param cache_size
636  *   Size of the per-core object cache. See rte_mempool_create() for
637  *   details.
638  * @param priv_size
639  *   Size of application private are between the rte_mbuf structure
640  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
641  * @param data_room_size
642  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
643  * @param socket_id
644  *   The socket identifier where the memory should be allocated. The
645  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
646  *   reserved zone.
647  * @return
648  *   The pointer to the new allocated mempool, on success. NULL on error
649  *   with rte_errno set appropriately. Possible rte_errno values include:
650  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
651  *    - E_RTE_SECONDARY - function was called from a secondary process instance
652  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
653  *    - ENOSPC - the maximum number of memzones has already been allocated
654  *    - EEXIST - a memzone with the same name already exists
655  *    - ENOMEM - no appropriate memory area found in which to create memzone
656  */
657 struct rte_mempool *
658 rte_pktmbuf_pool_create(const char *name, unsigned n,
659         unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
660         int socket_id);
661
662 /**
663  * Create a mbuf pool with a given mempool ops name
664  *
665  * This function creates and initializes a packet mbuf pool. It is
666  * a wrapper to rte_mempool functions.
667  *
668  * @param name
669  *   The name of the mbuf pool.
670  * @param n
671  *   The number of elements in the mbuf pool. The optimum size (in terms
672  *   of memory usage) for a mempool is when n is a power of two minus one:
673  *   n = (2^q - 1).
674  * @param cache_size
675  *   Size of the per-core object cache. See rte_mempool_create() for
676  *   details.
677  * @param priv_size
678  *   Size of application private are between the rte_mbuf structure
679  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
680  * @param data_room_size
681  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
682  * @param socket_id
683  *   The socket identifier where the memory should be allocated. The
684  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
685  *   reserved zone.
686  * @param ops_name
687  *   The mempool ops name to be used for this mempool instead of
688  *   default mempool. The value can be *NULL* to use default mempool.
689  * @return
690  *   The pointer to the new allocated mempool, on success. NULL on error
691  *   with rte_errno set appropriately. Possible rte_errno values include:
692  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
693  *    - E_RTE_SECONDARY - function was called from a secondary process instance
694  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
695  *    - ENOSPC - the maximum number of memzones has already been allocated
696  *    - EEXIST - a memzone with the same name already exists
697  *    - ENOMEM - no appropriate memory area found in which to create memzone
698  */
699 struct rte_mempool *
700 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
701         unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
702         int socket_id, const char *ops_name);
703
704 /**
705  * Get the data room size of mbufs stored in a pktmbuf_pool
706  *
707  * The data room size is the amount of data that can be stored in a
708  * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
709  *
710  * @param mp
711  *   The packet mbuf pool.
712  * @return
713  *   The data room size of mbufs stored in this mempool.
714  */
715 static inline uint16_t
716 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
717 {
718         struct rte_pktmbuf_pool_private *mbp_priv;
719
720         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
721         return mbp_priv->mbuf_data_room_size;
722 }
723
724 /**
725  * Get the application private size of mbufs stored in a pktmbuf_pool
726  *
727  * The private size of mbuf is a zone located between the rte_mbuf
728  * structure and the data buffer where an application can store data
729  * associated to a packet.
730  *
731  * @param mp
732  *   The packet mbuf pool.
733  * @return
734  *   The private size of mbufs stored in this mempool.
735  */
736 static inline uint16_t
737 rte_pktmbuf_priv_size(struct rte_mempool *mp)
738 {
739         struct rte_pktmbuf_pool_private *mbp_priv;
740
741         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
742         return mbp_priv->mbuf_priv_size;
743 }
744
745 /**
746  * Reset the data_off field of a packet mbuf to its default value.
747  *
748  * The given mbuf must have only one segment, which should be empty.
749  *
750  * @param m
751  *   The packet mbuf's data_off field has to be reset.
752  */
753 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
754 {
755         m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
756                                         (uint16_t)m->buf_len);
757 }
758
759 /**
760  * Reset the fields of a packet mbuf to their default values.
761  *
762  * The given mbuf must have only one segment.
763  *
764  * @param m
765  *   The packet mbuf to be reset.
766  */
767 #define MBUF_INVALID_PORT UINT16_MAX
768
769 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
770 {
771         m->next = NULL;
772         m->pkt_len = 0;
773         m->tx_offload = 0;
774         m->vlan_tci = 0;
775         m->vlan_tci_outer = 0;
776         m->nb_segs = 1;
777         m->port = MBUF_INVALID_PORT;
778
779         m->ol_flags = 0;
780         m->packet_type = 0;
781         rte_pktmbuf_reset_headroom(m);
782
783         m->data_len = 0;
784         __rte_mbuf_sanity_check(m, 1);
785 }
786
787 /**
788  * Allocate a new mbuf from a mempool.
789  *
790  * This new mbuf contains one segment, which has a length of 0. The pointer
791  * to data is initialized to have some bytes of headroom in the buffer
792  * (if buffer size allows).
793  *
794  * @param mp
795  *   The mempool from which the mbuf is allocated.
796  * @return
797  *   - The pointer to the new mbuf on success.
798  *   - NULL if allocation failed.
799  */
800 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
801 {
802         struct rte_mbuf *m;
803         if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
804                 rte_pktmbuf_reset(m);
805         return m;
806 }
807
808 /**
809  * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
810  * values.
811  *
812  *  @param pool
813  *    The mempool from which mbufs are allocated.
814  *  @param mbufs
815  *    Array of pointers to mbufs
816  *  @param count
817  *    Array size
818  *  @return
819  *   - 0: Success
820  *   - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
821  */
822 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
823          struct rte_mbuf **mbufs, unsigned count)
824 {
825         unsigned idx = 0;
826         int rc;
827
828         rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
829         if (unlikely(rc))
830                 return rc;
831
832         /* To understand duff's device on loop unwinding optimization, see
833          * https://en.wikipedia.org/wiki/Duff's_device.
834          * Here while() loop is used rather than do() while{} to avoid extra
835          * check if count is zero.
836          */
837         switch (count % 4) {
838         case 0:
839                 while (idx != count) {
840                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
841                         rte_pktmbuf_reset(mbufs[idx]);
842                         idx++;
843                         /* fall-through */
844         case 3:
845                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
846                         rte_pktmbuf_reset(mbufs[idx]);
847                         idx++;
848                         /* fall-through */
849         case 2:
850                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
851                         rte_pktmbuf_reset(mbufs[idx]);
852                         idx++;
853                         /* fall-through */
854         case 1:
855                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
856                         rte_pktmbuf_reset(mbufs[idx]);
857                         idx++;
858                         /* fall-through */
859                 }
860         }
861         return 0;
862 }
863
864 /**
865  * Initialize shared data at the end of an external buffer before attaching
866  * to a mbuf by ``rte_pktmbuf_attach_extbuf()``. This is not a mandatory
867  * initialization but a helper function to simply spare a few bytes at the
868  * end of the buffer for shared data. If shared data is allocated
869  * separately, this should not be called but application has to properly
870  * initialize the shared data according to its need.
871  *
872  * Free callback and its argument is saved and the refcnt is set to 1.
873  *
874  * @warning
875  * The value of buf_len will be reduced to RTE_PTR_DIFF(shinfo, buf_addr)
876  * after this initialization. This shall be used for
877  * ``rte_pktmbuf_attach_extbuf()``
878  *
879  * @param buf_addr
880  *   The pointer to the external buffer.
881  * @param [in,out] buf_len
882  *   The pointer to length of the external buffer. Input value must be
883  *   larger than the size of ``struct rte_mbuf_ext_shared_info`` and
884  *   padding for alignment. If not enough, this function will return NULL.
885  *   Adjusted buffer length will be returned through this pointer.
886  * @param free_cb
887  *   Free callback function to call when the external buffer needs to be
888  *   freed.
889  * @param fcb_opaque
890  *   Argument for the free callback function.
891  *
892  * @return
893  *   A pointer to the initialized shared data on success, return NULL
894  *   otherwise.
895  */
896 static inline struct rte_mbuf_ext_shared_info *
897 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
898         rte_mbuf_extbuf_free_callback_t free_cb, void *fcb_opaque)
899 {
900         struct rte_mbuf_ext_shared_info *shinfo;
901         void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
902         void *addr;
903
904         addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
905                                    sizeof(uintptr_t));
906         if (addr <= buf_addr)
907                 return NULL;
908
909         shinfo = (struct rte_mbuf_ext_shared_info *)addr;
910         shinfo->free_cb = free_cb;
911         shinfo->fcb_opaque = fcb_opaque;
912         rte_mbuf_ext_refcnt_set(shinfo, 1);
913
914         *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
915         return shinfo;
916 }
917
918 /**
919  * Attach an external buffer to a mbuf.
920  *
921  * User-managed anonymous buffer can be attached to an mbuf. When attaching
922  * it, corresponding free callback function and its argument should be
923  * provided via shinfo. This callback function will be called once all the
924  * mbufs are detached from the buffer (refcnt becomes zero).
925  *
926  * The headroom for the attaching mbuf will be set to zero and this can be
927  * properly adjusted after attachment. For example, ``rte_pktmbuf_adj()``
928  * or ``rte_pktmbuf_reset_headroom()`` might be used.
929  *
930  * More mbufs can be attached to the same external buffer by
931  * ``rte_pktmbuf_attach()`` once the external buffer has been attached by
932  * this API.
933  *
934  * Detachment can be done by either ``rte_pktmbuf_detach_extbuf()`` or
935  * ``rte_pktmbuf_detach()``.
936  *
937  * Memory for shared data must be provided and user must initialize all of
938  * the content properly, especially free callback and refcnt. The pointer
939  * of shared data will be stored in m->shinfo.
940  * ``rte_pktmbuf_ext_shinfo_init_helper`` can help to simply spare a few
941  * bytes at the end of buffer for the shared data, store free callback and
942  * its argument and set the refcnt to 1. The following is an example:
943  *
944  *   struct rte_mbuf_ext_shared_info *shinfo =
945  *          rte_pktmbuf_ext_shinfo_init_helper(buf_addr, &buf_len,
946  *                                             free_cb, fcb_arg);
947  *   rte_pktmbuf_attach_extbuf(m, buf_addr, buf_iova, buf_len, shinfo);
948  *   rte_pktmbuf_reset_headroom(m);
949  *   rte_pktmbuf_adj(m, data_len);
950  *
951  * Attaching an external buffer is quite similar to mbuf indirection in
952  * replacing buffer addresses and length of a mbuf, but a few differences:
953  * - When an indirect mbuf is attached, refcnt of the direct mbuf would be
954  *   2 as long as the direct mbuf itself isn't freed after the attachment.
955  *   In such cases, the buffer area of a direct mbuf must be read-only. But
956  *   external buffer has its own refcnt and it starts from 1. Unless
957  *   multiple mbufs are attached to a mbuf having an external buffer, the
958  *   external buffer is writable.
959  * - There's no need to allocate buffer from a mempool. Any buffer can be
960  *   attached with appropriate free callback and its IO address.
961  * - Smaller metadata is required to maintain shared data such as refcnt.
962  *
963  * @param m
964  *   The pointer to the mbuf.
965  * @param buf_addr
966  *   The pointer to the external buffer.
967  * @param buf_iova
968  *   IO address of the external buffer.
969  * @param buf_len
970  *   The size of the external buffer.
971  * @param shinfo
972  *   User-provided memory for shared data of the external buffer.
973  */
974 static inline void
975 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
976         rte_iova_t buf_iova, uint16_t buf_len,
977         struct rte_mbuf_ext_shared_info *shinfo)
978 {
979         /* mbuf should not be read-only */
980         RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
981         RTE_ASSERT(shinfo->free_cb != NULL);
982
983         m->buf_addr = buf_addr;
984         m->buf_iova = buf_iova;
985         m->buf_len = buf_len;
986
987         m->data_len = 0;
988         m->data_off = 0;
989
990         m->ol_flags |= EXT_ATTACHED_MBUF;
991         m->shinfo = shinfo;
992 }
993
994 /**
995  * Detach the external buffer attached to a mbuf, same as
996  * ``rte_pktmbuf_detach()``
997  *
998  * @param m
999  *   The mbuf having external buffer.
1000  */
1001 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1002
1003 /* internal */
1004 static inline void
1005 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1006 {
1007         mdst->port = msrc->port;
1008         mdst->vlan_tci = msrc->vlan_tci;
1009         mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1010         mdst->tx_offload = msrc->tx_offload;
1011         mdst->hash = msrc->hash;
1012         mdst->packet_type = msrc->packet_type;
1013         mdst->timestamp = msrc->timestamp;
1014 }
1015
1016 /**
1017  * Attach packet mbuf to another packet mbuf.
1018  *
1019  * If the mbuf we are attaching to isn't a direct buffer and is attached to
1020  * an external buffer, the mbuf being attached will be attached to the
1021  * external buffer instead of mbuf indirection.
1022  *
1023  * Otherwise, the mbuf will be indirectly attached. After attachment we
1024  * refer the mbuf we attached as 'indirect', while mbuf we attached to as
1025  * 'direct'.  The direct mbuf's reference counter is incremented.
1026  *
1027  * Right now, not supported:
1028  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
1029  *  - mbuf we trying to attach (mi) is used by someone else
1030  *    e.g. it's reference counter is greater then 1.
1031  *
1032  * @param mi
1033  *   The indirect packet mbuf.
1034  * @param m
1035  *   The packet mbuf we're attaching to.
1036  */
1037 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1038 {
1039         RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1040             rte_mbuf_refcnt_read(mi) == 1);
1041
1042         if (RTE_MBUF_HAS_EXTBUF(m)) {
1043                 rte_mbuf_ext_refcnt_update(m->shinfo, 1);
1044                 mi->ol_flags = m->ol_flags;
1045                 mi->shinfo = m->shinfo;
1046         } else {
1047                 /* if m is not direct, get the mbuf that embeds the data */
1048                 rte_mbuf_refcnt_update(rte_mbuf_from_indirect(m), 1);
1049                 mi->priv_size = m->priv_size;
1050                 mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1051         }
1052
1053         __rte_pktmbuf_copy_hdr(mi, m);
1054
1055         mi->data_off = m->data_off;
1056         mi->data_len = m->data_len;
1057         mi->buf_iova = m->buf_iova;
1058         mi->buf_addr = m->buf_addr;
1059         mi->buf_len = m->buf_len;
1060
1061         mi->next = NULL;
1062         mi->pkt_len = mi->data_len;
1063         mi->nb_segs = 1;
1064
1065         __rte_mbuf_sanity_check(mi, 1);
1066         __rte_mbuf_sanity_check(m, 0);
1067 }
1068
1069 /**
1070  * @internal used by rte_pktmbuf_detach().
1071  *
1072  * Decrement the reference counter of the external buffer. When the
1073  * reference counter becomes 0, the buffer is freed by pre-registered
1074  * callback.
1075  */
1076 static inline void
1077 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1078 {
1079         RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1080         RTE_ASSERT(m->shinfo != NULL);
1081
1082         if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1083                 m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1084 }
1085
1086 /**
1087  * @internal used by rte_pktmbuf_detach().
1088  *
1089  * Decrement the direct mbuf's reference counter. When the reference
1090  * counter becomes 0, the direct mbuf is freed.
1091  */
1092 static inline void
1093 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1094 {
1095         struct rte_mbuf *md;
1096
1097         RTE_ASSERT(RTE_MBUF_CLONED(m));
1098
1099         md = rte_mbuf_from_indirect(m);
1100
1101         if (rte_mbuf_refcnt_update(md, -1) == 0) {
1102                 md->next = NULL;
1103                 md->nb_segs = 1;
1104                 rte_mbuf_refcnt_set(md, 1);
1105                 rte_mbuf_raw_free(md);
1106         }
1107 }
1108
1109 /**
1110  * Detach a packet mbuf from external buffer or direct buffer.
1111  *
1112  *  - decrement refcnt and free the external/direct buffer if refcnt
1113  *    becomes zero.
1114  *  - restore original mbuf address and length values.
1115  *  - reset pktmbuf data and data_len to their default values.
1116  *
1117  * All other fields of the given packet mbuf will be left intact.
1118  *
1119  * @param m
1120  *   The indirect attached packet mbuf.
1121  */
1122 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1123 {
1124         struct rte_mempool *mp = m->pool;
1125         uint32_t mbuf_size, buf_len;
1126         uint16_t priv_size;
1127
1128         if (RTE_MBUF_HAS_EXTBUF(m))
1129                 __rte_pktmbuf_free_extbuf(m);
1130         else
1131                 __rte_pktmbuf_free_direct(m);
1132
1133         priv_size = rte_pktmbuf_priv_size(mp);
1134         mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1135         buf_len = rte_pktmbuf_data_room_size(mp);
1136
1137         m->priv_size = priv_size;
1138         m->buf_addr = (char *)m + mbuf_size;
1139         m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1140         m->buf_len = (uint16_t)buf_len;
1141         rte_pktmbuf_reset_headroom(m);
1142         m->data_len = 0;
1143         m->ol_flags = 0;
1144 }
1145
1146 /**
1147  * Decrease reference counter and unlink a mbuf segment
1148  *
1149  * This function does the same than a free, except that it does not
1150  * return the segment to its pool.
1151  * It decreases the reference counter, and if it reaches 0, it is
1152  * detached from its parent for an indirect mbuf.
1153  *
1154  * @param m
1155  *   The mbuf to be unlinked
1156  * @return
1157  *   - (m) if it is the last reference. It can be recycled or freed.
1158  *   - (NULL) if the mbuf still has remaining references on it.
1159  */
1160 static __rte_always_inline struct rte_mbuf *
1161 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1162 {
1163         __rte_mbuf_sanity_check(m, 0);
1164
1165         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1166
1167                 if (!RTE_MBUF_DIRECT(m))
1168                         rte_pktmbuf_detach(m);
1169
1170                 if (m->next != NULL) {
1171                         m->next = NULL;
1172                         m->nb_segs = 1;
1173                 }
1174
1175                 return m;
1176
1177         } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1178
1179                 if (!RTE_MBUF_DIRECT(m))
1180                         rte_pktmbuf_detach(m);
1181
1182                 if (m->next != NULL) {
1183                         m->next = NULL;
1184                         m->nb_segs = 1;
1185                 }
1186                 rte_mbuf_refcnt_set(m, 1);
1187
1188                 return m;
1189         }
1190         return NULL;
1191 }
1192
1193 /**
1194  * Free a segment of a packet mbuf into its original mempool.
1195  *
1196  * Free an mbuf, without parsing other segments in case of chained
1197  * buffers.
1198  *
1199  * @param m
1200  *   The packet mbuf segment to be freed.
1201  */
1202 static __rte_always_inline void
1203 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1204 {
1205         m = rte_pktmbuf_prefree_seg(m);
1206         if (likely(m != NULL))
1207                 rte_mbuf_raw_free(m);
1208 }
1209
1210 /**
1211  * Free a packet mbuf back into its original mempool.
1212  *
1213  * Free an mbuf, and all its segments in case of chained buffers. Each
1214  * segment is added back into its original mempool.
1215  *
1216  * @param m
1217  *   The packet mbuf to be freed. If NULL, the function does nothing.
1218  */
1219 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1220 {
1221         struct rte_mbuf *m_next;
1222
1223         if (m != NULL)
1224                 __rte_mbuf_sanity_check(m, 1);
1225
1226         while (m != NULL) {
1227                 m_next = m->next;
1228                 rte_pktmbuf_free_seg(m);
1229                 m = m_next;
1230         }
1231 }
1232
1233 /**
1234  * Free a bulk of packet mbufs back into their original mempools.
1235  *
1236  * Free a bulk of mbufs, and all their segments in case of chained buffers.
1237  * Each segment is added back into its original mempool.
1238  *
1239  *  @param mbufs
1240  *    Array of pointers to packet mbufs.
1241  *    The array may contain NULL pointers.
1242  *  @param count
1243  *    Array size.
1244  */
1245 __rte_experimental
1246 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1247
1248 /**
1249  * Create a "clone" of the given packet mbuf.
1250  *
1251  * Walks through all segments of the given packet mbuf, and for each of them:
1252  *  - Creates a new packet mbuf from the given pool.
1253  *  - Attaches newly created mbuf to the segment.
1254  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1255  * from the original packet mbuf.
1256  *
1257  * @param md
1258  *   The packet mbuf to be cloned.
1259  * @param mp
1260  *   The mempool from which the "clone" mbufs are allocated.
1261  * @return
1262  *   - The pointer to the new "clone" mbuf on success.
1263  *   - NULL if allocation fails.
1264  */
1265 struct rte_mbuf *
1266 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1267
1268 /**
1269  * Create a full copy of a given packet mbuf.
1270  *
1271  * Copies all the data from a given packet mbuf to a newly allocated
1272  * set of mbufs. The private data are is not copied.
1273  *
1274  * @param m
1275  *   The packet mbuf to be copiedd.
1276  * @param mp
1277  *   The mempool from which the "clone" mbufs are allocated.
1278  * @param offset
1279  *   The number of bytes to skip before copying.
1280  *   If the mbuf does not have that many bytes, it is an error
1281  *   and NULL is returned.
1282  * @param length
1283  *   The upper limit on bytes to copy.  Passing UINT32_MAX
1284  *   means all data (after offset).
1285  * @return
1286  *   - The pointer to the new "clone" mbuf on success.
1287  *   - NULL if allocation fails.
1288  */
1289 __rte_experimental
1290 struct rte_mbuf *
1291 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1292                  uint32_t offset, uint32_t length);
1293
1294 /**
1295  * Adds given value to the refcnt of all packet mbuf segments.
1296  *
1297  * Walks through all segments of given packet mbuf and for each of them
1298  * invokes rte_mbuf_refcnt_update().
1299  *
1300  * @param m
1301  *   The packet mbuf whose refcnt to be updated.
1302  * @param v
1303  *   The value to add to the mbuf's segments refcnt.
1304  */
1305 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1306 {
1307         __rte_mbuf_sanity_check(m, 1);
1308
1309         do {
1310                 rte_mbuf_refcnt_update(m, v);
1311         } while ((m = m->next) != NULL);
1312 }
1313
1314 /**
1315  * Get the headroom in a packet mbuf.
1316  *
1317  * @param m
1318  *   The packet mbuf.
1319  * @return
1320  *   The length of the headroom.
1321  */
1322 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1323 {
1324         __rte_mbuf_sanity_check(m, 0);
1325         return m->data_off;
1326 }
1327
1328 /**
1329  * Get the tailroom of a packet mbuf.
1330  *
1331  * @param m
1332  *   The packet mbuf.
1333  * @return
1334  *   The length of the tailroom.
1335  */
1336 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1337 {
1338         __rte_mbuf_sanity_check(m, 0);
1339         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1340                           m->data_len);
1341 }
1342
1343 /**
1344  * Get the last segment of the packet.
1345  *
1346  * @param m
1347  *   The packet mbuf.
1348  * @return
1349  *   The last segment of the given mbuf.
1350  */
1351 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1352 {
1353         __rte_mbuf_sanity_check(m, 1);
1354         while (m->next != NULL)
1355                 m = m->next;
1356         return m;
1357 }
1358
1359 /* deprecated */
1360 #define rte_pktmbuf_mtophys_offset(m, o) \
1361         rte_pktmbuf_iova_offset(m, o)
1362
1363 /* deprecated */
1364 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_iova(m)
1365
1366 /**
1367  * A macro that returns the length of the packet.
1368  *
1369  * The value can be read or assigned.
1370  *
1371  * @param m
1372  *   The packet mbuf.
1373  */
1374 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1375
1376 /**
1377  * A macro that returns the length of the segment.
1378  *
1379  * The value can be read or assigned.
1380  *
1381  * @param m
1382  *   The packet mbuf.
1383  */
1384 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1385
1386 /**
1387  * Prepend len bytes to an mbuf data area.
1388  *
1389  * Returns a pointer to the new
1390  * data start address. If there is not enough headroom in the first
1391  * segment, the function will return NULL, without modifying the mbuf.
1392  *
1393  * @param m
1394  *   The pkt mbuf.
1395  * @param len
1396  *   The amount of data to prepend (in bytes).
1397  * @return
1398  *   A pointer to the start of the newly prepended data, or
1399  *   NULL if there is not enough headroom space in the first segment
1400  */
1401 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1402                                         uint16_t len)
1403 {
1404         __rte_mbuf_sanity_check(m, 1);
1405
1406         if (unlikely(len > rte_pktmbuf_headroom(m)))
1407                 return NULL;
1408
1409         /* NB: elaborating the subtraction like this instead of using
1410          *     -= allows us to ensure the result type is uint16_t
1411          *     avoiding compiler warnings on gcc 8.1 at least */
1412         m->data_off = (uint16_t)(m->data_off - len);
1413         m->data_len = (uint16_t)(m->data_len + len);
1414         m->pkt_len  = (m->pkt_len + len);
1415
1416         return (char *)m->buf_addr + m->data_off;
1417 }
1418
1419 /**
1420  * Append len bytes to an mbuf.
1421  *
1422  * Append len bytes to an mbuf and return a pointer to the start address
1423  * of the added data. If there is not enough tailroom in the last
1424  * segment, the function will return NULL, without modifying the mbuf.
1425  *
1426  * @param m
1427  *   The packet mbuf.
1428  * @param len
1429  *   The amount of data to append (in bytes).
1430  * @return
1431  *   A pointer to the start of the newly appended data, or
1432  *   NULL if there is not enough tailroom space in the last segment
1433  */
1434 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1435 {
1436         void *tail;
1437         struct rte_mbuf *m_last;
1438
1439         __rte_mbuf_sanity_check(m, 1);
1440
1441         m_last = rte_pktmbuf_lastseg(m);
1442         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1443                 return NULL;
1444
1445         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1446         m_last->data_len = (uint16_t)(m_last->data_len + len);
1447         m->pkt_len  = (m->pkt_len + len);
1448         return (char*) tail;
1449 }
1450
1451 /**
1452  * Remove len bytes at the beginning of an mbuf.
1453  *
1454  * Returns a pointer to the start address of the new data area. If the
1455  * length is greater than the length of the first segment, then the
1456  * function will fail and return NULL, without modifying the mbuf.
1457  *
1458  * @param m
1459  *   The packet mbuf.
1460  * @param len
1461  *   The amount of data to remove (in bytes).
1462  * @return
1463  *   A pointer to the new start of the data.
1464  */
1465 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1466 {
1467         __rte_mbuf_sanity_check(m, 1);
1468
1469         if (unlikely(len > m->data_len))
1470                 return NULL;
1471
1472         /* NB: elaborating the addition like this instead of using
1473          *     += allows us to ensure the result type is uint16_t
1474          *     avoiding compiler warnings on gcc 8.1 at least */
1475         m->data_len = (uint16_t)(m->data_len - len);
1476         m->data_off = (uint16_t)(m->data_off + len);
1477         m->pkt_len  = (m->pkt_len - len);
1478         return (char *)m->buf_addr + m->data_off;
1479 }
1480
1481 /**
1482  * Remove len bytes of data at the end of the mbuf.
1483  *
1484  * If the length is greater than the length of the last segment, the
1485  * function will fail and return -1 without modifying the mbuf.
1486  *
1487  * @param m
1488  *   The packet mbuf.
1489  * @param len
1490  *   The amount of data to remove (in bytes).
1491  * @return
1492  *   - 0: On success.
1493  *   - -1: On error.
1494  */
1495 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1496 {
1497         struct rte_mbuf *m_last;
1498
1499         __rte_mbuf_sanity_check(m, 1);
1500
1501         m_last = rte_pktmbuf_lastseg(m);
1502         if (unlikely(len > m_last->data_len))
1503                 return -1;
1504
1505         m_last->data_len = (uint16_t)(m_last->data_len - len);
1506         m->pkt_len  = (m->pkt_len - len);
1507         return 0;
1508 }
1509
1510 /**
1511  * Test if mbuf data is contiguous.
1512  *
1513  * @param m
1514  *   The packet mbuf.
1515  * @return
1516  *   - 1, if all data is contiguous (one segment).
1517  *   - 0, if there is several segments.
1518  */
1519 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1520 {
1521         __rte_mbuf_sanity_check(m, 1);
1522         return !!(m->nb_segs == 1);
1523 }
1524
1525 /**
1526  * @internal used by rte_pktmbuf_read().
1527  */
1528 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1529         uint32_t len, void *buf);
1530
1531 /**
1532  * Read len data bytes in a mbuf at specified offset.
1533  *
1534  * If the data is contiguous, return the pointer in the mbuf data, else
1535  * copy the data in the buffer provided by the user and return its
1536  * pointer.
1537  *
1538  * @param m
1539  *   The pointer to the mbuf.
1540  * @param off
1541  *   The offset of the data in the mbuf.
1542  * @param len
1543  *   The amount of bytes to read.
1544  * @param buf
1545  *   The buffer where data is copied if it is not contiguous in mbuf
1546  *   data. Its length should be at least equal to the len parameter.
1547  * @return
1548  *   The pointer to the data, either in the mbuf if it is contiguous,
1549  *   or in the user buffer. If mbuf is too small, NULL is returned.
1550  */
1551 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1552         uint32_t off, uint32_t len, void *buf)
1553 {
1554         if (likely(off + len <= rte_pktmbuf_data_len(m)))
1555                 return rte_pktmbuf_mtod_offset(m, char *, off);
1556         else
1557                 return __rte_pktmbuf_read(m, off, len, buf);
1558 }
1559
1560 /**
1561  * Chain an mbuf to another, thereby creating a segmented packet.
1562  *
1563  * Note: The implementation will do a linear walk over the segments to find
1564  * the tail entry. For cases when there are many segments, it's better to
1565  * chain the entries manually.
1566  *
1567  * @param head
1568  *   The head of the mbuf chain (the first packet)
1569  * @param tail
1570  *   The mbuf to put last in the chain
1571  *
1572  * @return
1573  *   - 0, on success.
1574  *   - -EOVERFLOW, if the chain segment limit exceeded
1575  */
1576 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1577 {
1578         struct rte_mbuf *cur_tail;
1579
1580         /* Check for number-of-segments-overflow */
1581         if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1582                 return -EOVERFLOW;
1583
1584         /* Chain 'tail' onto the old tail */
1585         cur_tail = rte_pktmbuf_lastseg(head);
1586         cur_tail->next = tail;
1587
1588         /* accumulate number of segments and total length.
1589          * NB: elaborating the addition 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         head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1593         head->pkt_len += tail->pkt_len;
1594
1595         /* pkt_len is only set in the head */
1596         tail->pkt_len = tail->data_len;
1597
1598         return 0;
1599 }
1600
1601 /*
1602  * @warning
1603  * @b EXPERIMENTAL: This API may change without prior notice.
1604  *
1605  * For given input values generate raw tx_offload value.
1606  * Note that it is caller responsibility to make sure that input parameters
1607  * don't exceed maximum bit-field values.
1608  * @param il2
1609  *   l2_len value.
1610  * @param il3
1611  *   l3_len value.
1612  * @param il4
1613  *   l4_len value.
1614  * @param tso
1615  *   tso_segsz value.
1616  * @param ol3
1617  *   outer_l3_len value.
1618  * @param ol2
1619  *   outer_l2_len value.
1620  * @param unused
1621  *   unused value.
1622  * @return
1623  *   raw tx_offload value.
1624  */
1625 static __rte_always_inline uint64_t
1626 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1627         uint64_t ol3, uint64_t ol2, uint64_t unused)
1628 {
1629         return il2 << RTE_MBUF_L2_LEN_OFS |
1630                 il3 << RTE_MBUF_L3_LEN_OFS |
1631                 il4 << RTE_MBUF_L4_LEN_OFS |
1632                 tso << RTE_MBUF_TSO_SEGSZ_OFS |
1633                 ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1634                 ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1635                 unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1636 }
1637
1638 /**
1639  * Validate general requirements for Tx offload in mbuf.
1640  *
1641  * This function checks correctness and completeness of Tx offload settings.
1642  *
1643  * @param m
1644  *   The packet mbuf to be validated.
1645  * @return
1646  *   0 if packet is valid
1647  */
1648 static inline int
1649 rte_validate_tx_offload(const struct rte_mbuf *m)
1650 {
1651         uint64_t ol_flags = m->ol_flags;
1652
1653         /* Does packet set any of available offloads? */
1654         if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1655                 return 0;
1656
1657         /* IP checksum can be counted only for IPv4 packet */
1658         if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1659                 return -EINVAL;
1660
1661         /* IP type not set when required */
1662         if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1663                 if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1664                         return -EINVAL;
1665
1666         /* Check requirements for TSO packet */
1667         if (ol_flags & PKT_TX_TCP_SEG)
1668                 if ((m->tso_segsz == 0) ||
1669                                 ((ol_flags & PKT_TX_IPV4) &&
1670                                 !(ol_flags & PKT_TX_IP_CKSUM)))
1671                         return -EINVAL;
1672
1673         /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1674         if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1675                         !(ol_flags & PKT_TX_OUTER_IPV4))
1676                 return -EINVAL;
1677
1678         return 0;
1679 }
1680
1681 /**
1682  * @internal used by rte_pktmbuf_linearize().
1683  */
1684 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1685
1686 /**
1687  * Linearize data in mbuf.
1688  *
1689  * This function moves the mbuf data in the first segment if there is enough
1690  * tailroom. The subsequent segments are unchained and freed.
1691  *
1692  * @param mbuf
1693  *   mbuf to linearize
1694  * @return
1695  *   - 0, on success
1696  *   - -1, on error
1697  */
1698 static inline int
1699 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1700 {
1701         if (rte_pktmbuf_is_contiguous(mbuf))
1702                 return 0;
1703         return __rte_pktmbuf_linearize(mbuf);
1704 }
1705
1706 /**
1707  * Dump an mbuf structure to a file.
1708  *
1709  * Dump all fields for the given packet mbuf and all its associated
1710  * segments (in the case of a chained buffer).
1711  *
1712  * @param f
1713  *   A pointer to a file for output
1714  * @param m
1715  *   The packet mbuf.
1716  * @param dump_len
1717  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1718  *   the packet.
1719  */
1720 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1721
1722 /**
1723  * Get the value of mbuf sched queue_id field.
1724  */
1725 static inline uint32_t
1726 rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
1727 {
1728         return m->hash.sched.queue_id;
1729 }
1730
1731 /**
1732  * Get the value of mbuf sched traffic_class field.
1733  */
1734 static inline uint8_t
1735 rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
1736 {
1737         return m->hash.sched.traffic_class;
1738 }
1739
1740 /**
1741  * Get the value of mbuf sched color field.
1742  */
1743 static inline uint8_t
1744 rte_mbuf_sched_color_get(const struct rte_mbuf *m)
1745 {
1746         return m->hash.sched.color;
1747 }
1748
1749 /**
1750  * Get the values of mbuf sched queue_id, traffic_class and color.
1751  *
1752  * @param m
1753  *   Mbuf to read
1754  * @param queue_id
1755  *  Returns the queue id
1756  * @param traffic_class
1757  *  Returns the traffic class id
1758  * @param color
1759  *  Returns the colour id
1760  */
1761 static inline void
1762 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1763                         uint8_t *traffic_class,
1764                         uint8_t *color)
1765 {
1766         struct rte_mbuf_sched sched = m->hash.sched;
1767
1768         *queue_id = sched.queue_id;
1769         *traffic_class = sched.traffic_class;
1770         *color = sched.color;
1771 }
1772
1773 /**
1774  * Set the mbuf sched queue_id to the defined value.
1775  */
1776 static inline void
1777 rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
1778 {
1779         m->hash.sched.queue_id = queue_id;
1780 }
1781
1782 /**
1783  * Set the mbuf sched traffic_class id to the defined value.
1784  */
1785 static inline void
1786 rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
1787 {
1788         m->hash.sched.traffic_class = traffic_class;
1789 }
1790
1791 /**
1792  * Set the mbuf sched color id to the defined value.
1793  */
1794 static inline void
1795 rte_mbuf_sched_color_set(struct rte_mbuf *m, uint8_t color)
1796 {
1797         m->hash.sched.color = color;
1798 }
1799
1800 /**
1801  * Set the mbuf sched queue_id, traffic_class and color.
1802  *
1803  * @param m
1804  *   Mbuf to set
1805  * @param queue_id
1806  *  Queue id value to be set
1807  * @param traffic_class
1808  *  Traffic class id value to be set
1809  * @param color
1810  *  Color id to be set
1811  */
1812 static inline void
1813 rte_mbuf_sched_set(struct rte_mbuf *m, uint32_t queue_id,
1814                         uint8_t traffic_class,
1815                         uint8_t color)
1816 {
1817         m->hash.sched = (struct rte_mbuf_sched){
1818                                 .queue_id = queue_id,
1819                                 .traffic_class = traffic_class,
1820                                 .color = color,
1821                                 .reserved = 0,
1822                         };
1823 }
1824
1825 #ifdef __cplusplus
1826 }
1827 #endif
1828
1829 #endif /* _RTE_MBUF_H_ */