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