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