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