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