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