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