9c0d9bc2b34d36640546086d4c2fd88c7cb17580
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _RTE_MBUF_H_
36 #define _RTE_MBUF_H_
37
38 /**
39  * @file
40  * RTE Mbuf
41  *
42  * The mbuf library provides the ability to create and destroy buffers
43  * that may be used by the RTE application to store message
44  * buffers. The message buffers are stored in a mempool, using the
45  * RTE mempool library.
46  *
47  * The preferred way to create a mbuf pool is to use
48  * rte_pktmbuf_pool_create(). However, in some situations, an
49  * application may want to have more control (ex: populate the pool with
50  * specific memory), in this case it is possible to use functions from
51  * rte_mempool. See how rte_pktmbuf_pool_create() is implemented for
52  * details.
53  *
54  * This library provides an API to allocate/free packet mbufs, which are
55  * used to carry network packets.
56  *
57  * To understand the concepts of packet buffers or mbufs, you
58  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
59  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
60  * http://www.kohala.com/start/tcpipiv2.html
61  */
62
63 #include <stdint.h>
64 #include <rte_compat.h>
65 #include <rte_common.h>
66 #include <rte_config.h>
67 #include <rte_mempool.h>
68 #include <rte_memory.h>
69 #include <rte_atomic.h>
70 #include <rte_prefetch.h>
71 #include <rte_branch_prediction.h>
72 #include <rte_mbuf_ptype.h>
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 /*
79  * Packet Offload Features Flags. It also carry packet type information.
80  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
81  *
82  * - RX flags start at bit position zero, and get added to the left of previous
83  *   flags.
84  * - The most-significant 3 bits are reserved for generic mbuf flags
85  * - TX flags therefore start at bit position 60 (i.e. 63-3), and new flags get
86  *   added to the right of the previously defined flags i.e. they should count
87  *   downwards, not upwards.
88  *
89  * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
90  * rte_get_tx_ol_flag_name().
91  */
92
93 /**
94  * The RX packet is a 802.1q VLAN packet, and the tci has been
95  * saved in in mbuf->vlan_tci.
96  * If the flag PKT_RX_VLAN_STRIPPED is also present, the VLAN
97  * header has been stripped from mbuf data, else it is still
98  * present.
99  */
100 #define PKT_RX_VLAN          (1ULL << 0)
101
102 #define PKT_RX_RSS_HASH      (1ULL << 1)  /**< RX packet with RSS hash result. */
103 #define PKT_RX_FDIR          (1ULL << 2)  /**< RX packet with FDIR match indicate. */
104
105 /**
106  * Deprecated.
107  * Checking this flag alone is deprecated: check the 2 bits of
108  * PKT_RX_L4_CKSUM_MASK.
109  * This flag was set when the L4 checksum of a packet was detected as
110  * wrong by the hardware.
111  */
112 #define PKT_RX_L4_CKSUM_BAD  (1ULL << 3)
113
114 /**
115  * Deprecated.
116  * Checking this flag alone is deprecated: check the 2 bits of
117  * PKT_RX_IP_CKSUM_MASK.
118  * This flag was set when the IP checksum of a packet was detected as
119  * wrong by the hardware.
120  */
121 #define PKT_RX_IP_CKSUM_BAD  (1ULL << 4)
122
123 #define PKT_RX_EIP_CKSUM_BAD (1ULL << 5)  /**< External IP header checksum error. */
124
125 /**
126  * A vlan has been stripped by the hardware and its tci is saved in
127  * mbuf->vlan_tci. This can only happen if vlan stripping is enabled
128  * in the RX configuration of the PMD.
129  * When PKT_RX_VLAN_STRIPPED is set, PKT_RX_VLAN must also be set.
130  */
131 #define PKT_RX_VLAN_STRIPPED (1ULL << 6)
132
133 /**
134  * Mask of bits used to determine the status of RX IP checksum.
135  * - PKT_RX_IP_CKSUM_UNKNOWN: no information about the RX IP checksum
136  * - PKT_RX_IP_CKSUM_BAD: the IP checksum in the packet is wrong
137  * - PKT_RX_IP_CKSUM_GOOD: the IP checksum in the packet is valid
138  * - PKT_RX_IP_CKSUM_NONE: the IP checksum is not correct in the packet
139  *   data, but the integrity of the IP header is verified.
140  */
141 #define PKT_RX_IP_CKSUM_MASK ((1ULL << 4) | (1ULL << 7))
142
143 #define PKT_RX_IP_CKSUM_UNKNOWN 0
144 #define PKT_RX_IP_CKSUM_BAD     (1ULL << 4)
145 #define PKT_RX_IP_CKSUM_GOOD    (1ULL << 7)
146 #define PKT_RX_IP_CKSUM_NONE    ((1ULL << 4) | (1ULL << 7))
147
148 /**
149  * Mask of bits used to determine the status of RX L4 checksum.
150  * - PKT_RX_L4_CKSUM_UNKNOWN: no information about the RX L4 checksum
151  * - PKT_RX_L4_CKSUM_BAD: the L4 checksum in the packet is wrong
152  * - PKT_RX_L4_CKSUM_GOOD: the L4 checksum in the packet is valid
153  * - PKT_RX_L4_CKSUM_NONE: the L4 checksum is not correct in the packet
154  *   data, but the integrity of the L4 data is verified.
155  */
156 #define PKT_RX_L4_CKSUM_MASK ((1ULL << 3) | (1ULL << 8))
157
158 #define PKT_RX_L4_CKSUM_UNKNOWN 0
159 #define PKT_RX_L4_CKSUM_BAD     (1ULL << 3)
160 #define PKT_RX_L4_CKSUM_GOOD    (1ULL << 8)
161 #define PKT_RX_L4_CKSUM_NONE    ((1ULL << 3) | (1ULL << 8))
162
163 #define PKT_RX_IEEE1588_PTP  (1ULL << 9)  /**< RX IEEE1588 L2 Ethernet PT Packet. */
164 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
165 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
166 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
167
168 /**
169  * The 2 vlans have been stripped by the hardware and their tci are
170  * saved in mbuf->vlan_tci (inner) and mbuf->vlan_tci_outer (outer).
171  * This can only happen if vlan stripping is enabled in the RX
172  * configuration of the PMD. If this flag is set,
173  * When PKT_RX_QINQ_STRIPPED is set, the flags (PKT_RX_VLAN |
174  * PKT_RX_VLAN_STRIPPED | PKT_RX_QINQ) must also be set.
175  */
176 #define PKT_RX_QINQ_STRIPPED (1ULL << 15)
177
178 /**
179  * When packets are coalesced by a hardware or virtual driver, this flag
180  * can be set in the RX mbuf, meaning that the m->tso_segsz field is
181  * valid and is set to the segment size of original packets.
182  */
183 #define PKT_RX_LRO           (1ULL << 16)
184
185 /**
186  * Indicate that the timestamp field in the mbuf is valid.
187  */
188 #define PKT_RX_TIMESTAMP     (1ULL << 17)
189
190 /**
191  * Indicate that security offload processing was applied on the RX packet.
192  */
193 #define PKT_RX_SEC_OFFLOAD              (1ULL << 18)
194
195 /**
196  * Indicate that security offload processing failed on the RX packet.
197  */
198 #define PKT_RX_SEC_OFFLOAD_FAILED       (1ULL << 19)
199
200 /**
201  * The RX packet is a double VLAN, and the outer tci has been
202  * saved in in mbuf->vlan_tci_outer.
203  * If the flag PKT_RX_QINQ_STRIPPED is also present, both VLANs
204  * headers have been stripped from mbuf data, else they are still
205  * present.
206  */
207 #define PKT_RX_QINQ          (1ULL << 20)
208
209 /* add new RX flags here */
210
211 /* add new TX flags here */
212
213 /**
214  * UDP Fragmentation Offload flag. This flag is used for enabling UDP
215  * fragmentation in SW or in HW. When use UFO, mbuf->tso_segsz is used
216  * to store the MSS of UDP fragments.
217  */
218 #define PKT_TX_UDP_SEG  (1ULL << 42)
219
220 /**
221  * Request security offload processing on the TX packet.
222  */
223 #define PKT_TX_SEC_OFFLOAD              (1ULL << 43)
224
225 /**
226  * Offload the MACsec. This flag must be set by the application to enable
227  * this offload feature for a packet to be transmitted.
228  */
229 #define PKT_TX_MACSEC        (1ULL << 44)
230
231 /**
232  * Bits 45:48 used for the tunnel type.
233  * When doing Tx offload like TSO or checksum, the HW needs to configure the
234  * tunnel type into the HW descriptors.
235  */
236 #define PKT_TX_TUNNEL_VXLAN   (0x1ULL << 45)
237 #define PKT_TX_TUNNEL_GRE     (0x2ULL << 45)
238 #define PKT_TX_TUNNEL_IPIP    (0x3ULL << 45)
239 #define PKT_TX_TUNNEL_GENEVE  (0x4ULL << 45)
240 /**< TX packet with MPLS-in-UDP RFC 7510 header. */
241 #define PKT_TX_TUNNEL_MPLSINUDP (0x5ULL << 45)
242 /* add new TX TUNNEL type here */
243 #define PKT_TX_TUNNEL_MASK    (0xFULL << 45)
244
245 /**
246  * Second VLAN insertion (QinQ) flag.
247  */
248 #define PKT_TX_QINQ        (1ULL << 49)   /**< TX packet with double VLAN inserted. */
249 /* this old name is deprecated */
250 #define PKT_TX_QINQ_PKT    PKT_TX_QINQ
251
252 /**
253  * TCP segmentation offload. To enable this offload feature for a
254  * packet to be transmitted on hardware supporting TSO:
255  *  - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
256  *    PKT_TX_TCP_CKSUM)
257  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
258  *  - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
259  *    to 0 in the packet
260  *  - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
261  *  - calculate the pseudo header checksum without taking ip_len in account,
262  *    and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
263  *    rte_ipv6_phdr_cksum() that can be used as helpers.
264  */
265 #define PKT_TX_TCP_SEG       (1ULL << 50)
266
267 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
268
269 /**
270  * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
271  * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
272  * L4 checksum offload, the user needs to:
273  *  - fill l2_len and l3_len in mbuf
274  *  - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
275  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
276  *  - calculate the pseudo header checksum and set it in the L4 header (only
277  *    for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
278  *    For SCTP, set the crc field to 0.
279  */
280 #define PKT_TX_L4_NO_CKSUM   (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
281 #define PKT_TX_TCP_CKSUM     (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
282 #define PKT_TX_SCTP_CKSUM    (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
283 #define PKT_TX_UDP_CKSUM     (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
284 #define PKT_TX_L4_MASK       (3ULL << 52) /**< Mask for L4 cksum offload request. */
285
286 /**
287  * Offload the IP checksum in the hardware. The flag PKT_TX_IPV4 should
288  * also be set by the application, although a PMD will only check
289  * PKT_TX_IP_CKSUM.
290  *  - set the IP checksum field in the packet to 0
291  *  - fill the mbuf offload information: l2_len, l3_len
292  */
293 #define PKT_TX_IP_CKSUM      (1ULL << 54)
294
295 /**
296  * Packet is IPv4. This flag must be set when using any offload feature
297  * (TSO, L3 or L4 checksum) to tell the NIC that the packet is an IPv4
298  * packet. If the packet is a tunneled packet, this flag is related to
299  * the inner headers.
300  */
301 #define PKT_TX_IPV4          (1ULL << 55)
302
303 /**
304  * Packet is IPv6. This flag must be set when using an offload feature
305  * (TSO or L4 checksum) to tell the NIC that the packet is an IPv6
306  * packet. If the packet is a tunneled packet, this flag is related to
307  * the inner headers.
308  */
309 #define PKT_TX_IPV6          (1ULL << 56)
310
311 /**
312  * TX packet is a 802.1q VLAN packet.
313  */
314 #define PKT_TX_VLAN          (1ULL << 57)
315 /* this old name is deprecated */
316 #define PKT_TX_VLAN_PKT      PKT_TX_VLAN
317
318 /**
319  * Offload the IP checksum of an external header in the hardware. The
320  * flag PKT_TX_OUTER_IPV4 should also be set by the application, alto ugh
321  * a PMD will only check PKT_TX_IP_CKSUM.  The IP checksum field in the
322  * packet must be set to 0.
323  *  - set the outer IP checksum field in the packet to 0
324  *  - fill the mbuf offload information: outer_l2_len, outer_l3_len
325  */
326 #define PKT_TX_OUTER_IP_CKSUM   (1ULL << 58)
327
328 /**
329  * Packet outer header is IPv4. This flag must be set when using any
330  * outer offload feature (L3 or L4 checksum) to tell the NIC that the
331  * outer header of the tunneled packet is an IPv4 packet.
332  */
333 #define PKT_TX_OUTER_IPV4   (1ULL << 59)
334
335 /**
336  * Packet outer header is IPv6. This flag must be set when using any
337  * outer offload feature (L4 checksum) to tell the NIC that the outer
338  * header of the tunneled packet is an IPv6 packet.
339  */
340 #define PKT_TX_OUTER_IPV6    (1ULL << 60)
341
342 /**
343  * Bitmask of all supported packet Tx offload features flags,
344  * which can be set for packet.
345  */
346 #define PKT_TX_OFFLOAD_MASK (    \
347                 PKT_TX_IP_CKSUM |        \
348                 PKT_TX_L4_MASK |         \
349                 PKT_TX_OUTER_IP_CKSUM |  \
350                 PKT_TX_TCP_SEG |         \
351                 PKT_TX_IEEE1588_TMST |   \
352                 PKT_TX_QINQ_PKT |        \
353                 PKT_TX_VLAN_PKT |        \
354                 PKT_TX_TUNNEL_MASK |     \
355                 PKT_TX_MACSEC |          \
356                 PKT_TX_SEC_OFFLOAD)
357
358 #define __RESERVED           (1ULL << 61) /**< reserved for future mbuf use */
359
360 #define IND_ATTACHED_MBUF    (1ULL << 62) /**< Indirect attached mbuf */
361
362 /* Use final bit of flags to indicate a control mbuf */
363 #define CTRL_MBUF_FLAG       (1ULL << 63) /**< Mbuf contains control data */
364
365 /** Alignment constraint of mbuf private area. */
366 #define RTE_MBUF_PRIV_ALIGN 8
367
368 /**
369  * Get the name of a RX offload flag
370  *
371  * @param mask
372  *   The mask describing the flag.
373  * @return
374  *   The name of this flag, or NULL if it's not a valid RX flag.
375  */
376 const char *rte_get_rx_ol_flag_name(uint64_t mask);
377
378 /**
379  * Dump the list of RX offload flags in a buffer
380  *
381  * @param mask
382  *   The mask describing the RX flags.
383  * @param buf
384  *   The output buffer.
385  * @param buflen
386  *   The length of the buffer.
387  * @return
388  *   0 on success, (-1) on error.
389  */
390 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
391
392 /**
393  * Get the name of a TX offload flag
394  *
395  * @param mask
396  *   The mask describing the flag. Usually only one bit must be set.
397  *   Several bits can be given if they belong to the same mask.
398  *   Ex: PKT_TX_L4_MASK.
399  * @return
400  *   The name of this flag, or NULL if it's not a valid TX flag.
401  */
402 const char *rte_get_tx_ol_flag_name(uint64_t mask);
403
404 /**
405  * Dump the list of TX offload flags in a buffer
406  *
407  * @param mask
408  *   The mask describing the TX flags.
409  * @param buf
410  *   The output buffer.
411  * @param buflen
412  *   The length of the buffer.
413  * @return
414  *   0 on success, (-1) on error.
415  */
416 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
417
418 /**
419  * Some NICs need at least 2KB buffer to RX standard Ethernet frame without
420  * splitting it into multiple segments.
421  * So, for mbufs that planned to be involved into RX/TX, the recommended
422  * minimal buffer length is 2KB + RTE_PKTMBUF_HEADROOM.
423  */
424 #define RTE_MBUF_DEFAULT_DATAROOM       2048
425 #define RTE_MBUF_DEFAULT_BUF_SIZE       \
426         (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
427
428 /* define a set of marker types that can be used to refer to set points in the
429  * mbuf */
430 __extension__
431 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
432 __extension__
433 typedef uint8_t  MARKER8[0];  /**< generic marker with 1B alignment */
434 __extension__
435 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
436                                * with a single assignment */
437
438 /**
439  * The generic rte_mbuf, containing a packet mbuf.
440  */
441 struct rte_mbuf {
442         MARKER cacheline0;
443
444         void *buf_addr;           /**< Virtual address of segment buffer. */
445         /**
446          * Physical address of segment buffer.
447          * Force alignment to 8-bytes, so as to ensure we have the exact
448          * same mbuf cacheline0 layout for 32-bit and 64-bit. This makes
449          * working on vector drivers easier.
450          */
451         RTE_STD_C11
452         union {
453                 rte_iova_t buf_iova;
454                 rte_iova_t buf_physaddr; /**< deprecated */
455         } __rte_aligned(sizeof(rte_iova_t));
456
457         /* next 8 bytes are initialised on RX descriptor rearm */
458         MARKER64 rearm_data;
459         uint16_t data_off;
460
461         /**
462          * Reference counter. Its size should at least equal to the size
463          * of port field (16 bits), to support zero-copy broadcast.
464          * It should only be accessed using the following functions:
465          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
466          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
467          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
468          * config option.
469          */
470         RTE_STD_C11
471         union {
472                 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
473                 uint16_t refcnt;              /**< Non-atomically accessed refcnt */
474         };
475         uint16_t nb_segs;         /**< Number of segments. */
476
477         /** Input port (16 bits to support more than 256 virtual ports). */
478         uint16_t port;
479
480         uint64_t ol_flags;        /**< Offload features. */
481
482         /* remaining bytes are set on RX when pulling packet from descriptor */
483         MARKER rx_descriptor_fields1;
484
485         /*
486          * The packet type, which is the combination of outer/inner L2, L3, L4
487          * and tunnel types. The packet_type is about data really present in the
488          * mbuf. Example: if vlan stripping is enabled, a received vlan packet
489          * would have RTE_PTYPE_L2_ETHER and not RTE_PTYPE_L2_VLAN because the
490          * vlan is stripped from the data.
491          */
492         RTE_STD_C11
493         union {
494                 uint32_t packet_type; /**< L2/L3/L4 and tunnel information. */
495                 struct {
496                         uint32_t l2_type:4; /**< (Outer) L2 type. */
497                         uint32_t l3_type:4; /**< (Outer) L3 type. */
498                         uint32_t l4_type:4; /**< (Outer) L4 type. */
499                         uint32_t tun_type:4; /**< Tunnel type. */
500                         RTE_STD_C11
501                         union {
502                                 uint8_t inner_esp_next_proto;
503                                 /**< ESP next protocol type, valid if
504                                  * RTE_PTYPE_TUNNEL_ESP tunnel type is set
505                                  * on both Tx and Rx.
506                                  */
507                                 __extension__
508                                 struct {
509                                         uint8_t inner_l2_type:4;
510                                         /**< Inner L2 type. */
511                                         uint8_t inner_l3_type:4;
512                                         /**< Inner L3 type. */
513                                 };
514                         };
515                         uint32_t inner_l4_type:4; /**< Inner L4 type. */
516                 };
517         };
518
519         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
520         uint16_t data_len;        /**< Amount of data in segment buffer. */
521         /** VLAN TCI (CPU order), valid if PKT_RX_VLAN is set. */
522         uint16_t vlan_tci;
523
524         union {
525                 uint32_t rss;     /**< RSS hash result if RSS enabled */
526                 struct {
527                         RTE_STD_C11
528                         union {
529                                 struct {
530                                         uint16_t hash;
531                                         uint16_t id;
532                                 };
533                                 uint32_t lo;
534                                 /**< Second 4 flexible bytes */
535                         };
536                         uint32_t hi;
537                         /**< First 4 flexible bytes or FD ID, dependent on
538                              PKT_RX_FDIR_* flag in ol_flags. */
539                 } fdir;           /**< Filter identifier if FDIR enabled */
540                 struct {
541                         uint32_t lo;
542                         uint32_t hi;
543                 } sched;          /**< Hierarchical scheduler */
544                 uint32_t usr;     /**< User defined tags. See rte_distributor_process() */
545         } hash;                   /**< hash information */
546
547         /** Outer VLAN TCI (CPU order), valid if PKT_RX_QINQ is set. */
548         uint16_t vlan_tci_outer;
549
550         uint16_t buf_len;         /**< Length of segment buffer. */
551
552         /** Valid if PKT_RX_TIMESTAMP is set. The unit and time reference
553          * are not normalized but are always the same for a given port.
554          */
555         uint64_t timestamp;
556
557         /* second cache line - fields only used in slow path or on TX */
558         MARKER cacheline1 __rte_cache_min_aligned;
559
560         RTE_STD_C11
561         union {
562                 void *userdata;   /**< Can be used for external metadata */
563                 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
564         };
565
566         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
567         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
568
569         /* fields to support TX offloads */
570         RTE_STD_C11
571         union {
572                 uint64_t tx_offload;       /**< combined for easy fetch */
573                 __extension__
574                 struct {
575                         uint64_t l2_len:7;
576                         /**< L2 (MAC) Header Length for non-tunneling pkt.
577                          * Outer_L4_len + ... + Inner_L2_len for tunneling pkt.
578                          */
579                         uint64_t l3_len:9; /**< L3 (IP) Header Length. */
580                         uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
581                         uint64_t tso_segsz:16; /**< TCP TSO segment size */
582
583                         /* fields for TX offloading of tunnels */
584                         uint64_t outer_l3_len:9; /**< Outer L3 (IP) Hdr Length. */
585                         uint64_t outer_l2_len:7; /**< Outer L2 (MAC) Hdr Length. */
586
587                         /* uint64_t unused:8; */
588                 };
589         };
590
591         /** Size of the application private data. In case of an indirect
592          * mbuf, it stores the direct mbuf private data size. */
593         uint16_t priv_size;
594
595         /** Timesync flags for use with IEEE1588. */
596         uint16_t timesync;
597
598         /** Sequence number. See also rte_reorder_insert(). */
599         uint32_t seqn;
600
601 } __rte_cache_aligned;
602
603 /**< Maximum number of nb_segs allowed. */
604 #define RTE_MBUF_MAX_NB_SEGS    UINT16_MAX
605
606 /**
607  * Prefetch the first part of the mbuf
608  *
609  * The first 64 bytes of the mbuf corresponds to fields that are used early
610  * in the receive path. If the cache line of the architecture is higher than
611  * 64B, the second part will also be prefetched.
612  *
613  * @param m
614  *   The pointer to the mbuf.
615  */
616 static inline void
617 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
618 {
619         rte_prefetch0(&m->cacheline0);
620 }
621
622 /**
623  * Prefetch the second part of the mbuf
624  *
625  * The next 64 bytes of the mbuf corresponds to fields that are used in the
626  * transmit path. If the cache line of the architecture is higher than 64B,
627  * this function does nothing as it is expected that the full mbuf is
628  * already in cache.
629  *
630  * @param m
631  *   The pointer to the mbuf.
632  */
633 static inline void
634 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
635 {
636 #if RTE_CACHE_LINE_SIZE == 64
637         rte_prefetch0(&m->cacheline1);
638 #else
639         RTE_SET_USED(m);
640 #endif
641 }
642
643
644 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
645
646 /**
647  * Return the IO address of the beginning of the mbuf data
648  *
649  * @param mb
650  *   The pointer to the mbuf.
651  * @return
652  *   The IO address of the beginning of the mbuf data
653  */
654 static inline rte_iova_t
655 rte_mbuf_data_iova(const struct rte_mbuf *mb)
656 {
657         return mb->buf_iova + mb->data_off;
658 }
659
660 __rte_deprecated
661 static inline phys_addr_t
662 rte_mbuf_data_dma_addr(const struct rte_mbuf *mb)
663 {
664         return rte_mbuf_data_iova(mb);
665 }
666
667 /**
668  * Return the default IO address of the beginning of the mbuf data
669  *
670  * This function is used by drivers in their receive function, as it
671  * returns the location where data should be written by the NIC, taking
672  * the default headroom in account.
673  *
674  * @param mb
675  *   The pointer to the mbuf.
676  * @return
677  *   The IO address of the beginning of the mbuf data
678  */
679 static inline rte_iova_t
680 rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
681 {
682         return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
683 }
684
685 __rte_deprecated
686 static inline phys_addr_t
687 rte_mbuf_data_dma_addr_default(const struct rte_mbuf *mb)
688 {
689         return rte_mbuf_data_iova_default(mb);
690 }
691
692 /**
693  * Return the mbuf owning the data buffer address of an indirect mbuf.
694  *
695  * @param mi
696  *   The pointer to the indirect mbuf.
697  * @return
698  *   The address of the direct mbuf corresponding to buffer_addr.
699  */
700 static inline struct rte_mbuf *
701 rte_mbuf_from_indirect(struct rte_mbuf *mi)
702 {
703         return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
704 }
705
706 /**
707  * Return the buffer address embedded in the given mbuf.
708  *
709  * @param md
710  *   The pointer to the mbuf.
711  * @return
712  *   The address of the data buffer owned by the mbuf.
713  */
714 static inline char *
715 rte_mbuf_to_baddr(struct rte_mbuf *md)
716 {
717         char *buffer_addr;
718         buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
719         return buffer_addr;
720 }
721
722 /**
723  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
724  */
725 #define RTE_MBUF_INDIRECT(mb)   ((mb)->ol_flags & IND_ATTACHED_MBUF)
726
727 /**
728  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
729  */
730 #define RTE_MBUF_DIRECT(mb)     (!RTE_MBUF_INDIRECT(mb))
731
732 /**
733  * Private data in case of pktmbuf pool.
734  *
735  * A structure that contains some pktmbuf_pool-specific data that are
736  * appended after the mempool structure (in private data).
737  */
738 struct rte_pktmbuf_pool_private {
739         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
740         uint16_t mbuf_priv_size;      /**< Size of private area in each mbuf. */
741 };
742
743 #ifdef RTE_LIBRTE_MBUF_DEBUG
744
745 /**  check mbuf type in debug mode */
746 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
747
748 #else /*  RTE_LIBRTE_MBUF_DEBUG */
749
750 /**  check mbuf type in debug mode */
751 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
752
753 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
754
755 #ifdef RTE_MBUF_REFCNT_ATOMIC
756
757 /**
758  * Reads the value of an mbuf's refcnt.
759  * @param m
760  *   Mbuf to read
761  * @return
762  *   Reference count number.
763  */
764 static inline uint16_t
765 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
766 {
767         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
768 }
769
770 /**
771  * Sets an mbuf's refcnt to a defined value.
772  * @param m
773  *   Mbuf to update
774  * @param new_value
775  *   Value set
776  */
777 static inline void
778 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
779 {
780         rte_atomic16_set(&m->refcnt_atomic, new_value);
781 }
782
783 /* internal */
784 static inline uint16_t
785 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
786 {
787         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
788 }
789
790 /**
791  * Adds given value to an mbuf's refcnt and returns its new value.
792  * @param m
793  *   Mbuf to update
794  * @param value
795  *   Value to add/subtract
796  * @return
797  *   Updated value
798  */
799 static inline uint16_t
800 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
801 {
802         /*
803          * The atomic_add is an expensive operation, so we don't want to
804          * call it in the case where we know we are the uniq holder of
805          * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
806          * operation has to be used because concurrent accesses on the
807          * reference counter can occur.
808          */
809         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
810                 rte_mbuf_refcnt_set(m, 1 + value);
811                 return 1 + value;
812         }
813
814         return __rte_mbuf_refcnt_update(m, value);
815 }
816
817 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
818
819 /* internal */
820 static inline uint16_t
821 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
822 {
823         m->refcnt = (uint16_t)(m->refcnt + value);
824         return m->refcnt;
825 }
826
827 /**
828  * Adds given value to an mbuf's refcnt and returns its new value.
829  */
830 static inline uint16_t
831 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
832 {
833         return __rte_mbuf_refcnt_update(m, value);
834 }
835
836 /**
837  * Reads the value of an mbuf's refcnt.
838  */
839 static inline uint16_t
840 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
841 {
842         return m->refcnt;
843 }
844
845 /**
846  * Sets an mbuf's refcnt to the defined value.
847  */
848 static inline void
849 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
850 {
851         m->refcnt = new_value;
852 }
853
854 #endif /* RTE_MBUF_REFCNT_ATOMIC */
855
856 /** Mbuf prefetch */
857 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
858         if ((m) != NULL)                        \
859                 rte_prefetch0(m);               \
860 } while (0)
861
862
863 /**
864  * Sanity checks on an mbuf.
865  *
866  * Check the consistency of the given mbuf. The function will cause a
867  * panic if corruption is detected.
868  *
869  * @param m
870  *   The mbuf to be checked.
871  * @param is_header
872  *   True if the mbuf is a packet header, false if it is a sub-segment
873  *   of a packet (in this case, some fields like nb_segs are not checked)
874  */
875 void
876 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
877
878 #define MBUF_RAW_ALLOC_CHECK(m) do {                            \
879         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);               \
880         RTE_ASSERT((m)->next == NULL);                          \
881         RTE_ASSERT((m)->nb_segs == 1);                          \
882         __rte_mbuf_sanity_check(m, 0);                          \
883 } while (0)
884
885 /**
886  * Allocate an uninitialized mbuf from mempool *mp*.
887  *
888  * This function can be used by PMDs (especially in RX functions) to
889  * allocate an uninitialized mbuf. The driver is responsible of
890  * initializing all the required fields. See rte_pktmbuf_reset().
891  * For standard needs, prefer rte_pktmbuf_alloc().
892  *
893  * The caller can expect that the following fields of the mbuf structure
894  * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
895  * next=NULL, pool, priv_size. The other fields must be initialized
896  * by the caller.
897  *
898  * @param mp
899  *   The mempool from which mbuf is allocated.
900  * @return
901  *   - The pointer to the new mbuf on success.
902  *   - NULL if allocation failed.
903  */
904 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
905 {
906         struct rte_mbuf *m;
907
908         if (rte_mempool_get(mp, (void **)&m) < 0)
909                 return NULL;
910         MBUF_RAW_ALLOC_CHECK(m);
911         return m;
912 }
913
914 /**
915  * Put mbuf back into its original mempool.
916  *
917  * The caller must ensure that the mbuf is direct and properly
918  * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
919  * rte_pktmbuf_prefree_seg().
920  *
921  * This function should be used with care, when optimization is
922  * required. For standard needs, prefer rte_pktmbuf_free() or
923  * rte_pktmbuf_free_seg().
924  *
925  * @param m
926  *   The mbuf to be freed.
927  */
928 static __rte_always_inline void
929 rte_mbuf_raw_free(struct rte_mbuf *m)
930 {
931         RTE_ASSERT(RTE_MBUF_DIRECT(m));
932         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
933         RTE_ASSERT(m->next == NULL);
934         RTE_ASSERT(m->nb_segs == 1);
935         __rte_mbuf_sanity_check(m, 0);
936         rte_mempool_put(m->pool, m);
937 }
938
939 /* compat with older versions */
940 __rte_deprecated
941 static inline void
942 __rte_mbuf_raw_free(struct rte_mbuf *m)
943 {
944         rte_mbuf_raw_free(m);
945 }
946
947 /* Operations on ctrl mbuf */
948
949 /**
950  * The control mbuf constructor.
951  *
952  * This function initializes some fields in an mbuf structure that are
953  * not modified by the user once created (mbuf type, origin pool, buffer
954  * start address, and so on). This function is given as a callback function
955  * to rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
956  *
957  * @param mp
958  *   The mempool from which the mbuf is allocated.
959  * @param opaque_arg
960  *   A pointer that can be used by the user to retrieve useful information
961  *   for mbuf initialization. This pointer is the opaque argument passed to
962  *   rte_mempool_obj_iter() or rte_mempool_create().
963  * @param m
964  *   The mbuf to initialize.
965  * @param i
966  *   The index of the mbuf in the pool table.
967  */
968 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
969                 void *m, unsigned i);
970
971 /**
972  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
973  *
974  * This new mbuf is initialized with data pointing to the beginning of
975  * buffer, and with a length of zero.
976  *
977  * @param mp
978  *   The mempool from which the mbuf is allocated.
979  * @return
980  *   - The pointer to the new mbuf on success.
981  *   - NULL if allocation failed.
982  */
983 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
984
985 /**
986  * Free a control mbuf back into its original mempool.
987  *
988  * @param m
989  *   The control mbuf to be freed.
990  */
991 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
992
993 /**
994  * A macro that returns the pointer to the carried data.
995  *
996  * The value that can be read or assigned.
997  *
998  * @param m
999  *   The control mbuf.
1000  */
1001 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
1002
1003 /**
1004  * A macro that returns the length of the carried data.
1005  *
1006  * The value that can be read or assigned.
1007  *
1008  * @param m
1009  *   The control mbuf.
1010  */
1011 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
1012
1013 /**
1014  * Tests if an mbuf is a control mbuf
1015  *
1016  * @param m
1017  *   The mbuf to be tested
1018  * @return
1019  *   - True (1) if the mbuf is a control mbuf
1020  *   - False(0) otherwise
1021  */
1022 static inline int
1023 rte_is_ctrlmbuf(struct rte_mbuf *m)
1024 {
1025         return !!(m->ol_flags & CTRL_MBUF_FLAG);
1026 }
1027
1028 /* Operations on pkt mbuf */
1029
1030 /**
1031  * The packet mbuf constructor.
1032  *
1033  * This function initializes some fields in the mbuf structure that are
1034  * not modified by the user once created (origin pool, buffer start
1035  * address, and so on). This function is given as a callback function to
1036  * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
1037  *
1038  * @param mp
1039  *   The mempool from which mbufs originate.
1040  * @param opaque_arg
1041  *   A pointer that can be used by the user to retrieve useful information
1042  *   for mbuf initialization. This pointer is the opaque argument passed to
1043  *   rte_mempool_obj_iter() or rte_mempool_create().
1044  * @param m
1045  *   The mbuf to initialize.
1046  * @param i
1047  *   The index of the mbuf in the pool table.
1048  */
1049 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
1050                       void *m, unsigned i);
1051
1052
1053 /**
1054  * A  packet mbuf pool constructor.
1055  *
1056  * This function initializes the mempool private data in the case of a
1057  * pktmbuf pool. This private data is needed by the driver. The
1058  * function must be called on the mempool before it is used, or it
1059  * can be given as a callback function to rte_mempool_create() at
1060  * pool creation. It can be extended by the user, for example, to
1061  * provide another packet size.
1062  *
1063  * @param mp
1064  *   The mempool from which mbufs originate.
1065  * @param opaque_arg
1066  *   A pointer that can be used by the user to retrieve useful information
1067  *   for mbuf initialization. This pointer is the opaque argument passed to
1068  *   rte_mempool_create().
1069  */
1070 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
1071
1072 /**
1073  * Create a mbuf pool.
1074  *
1075  * This function creates and initializes a packet mbuf pool. It is
1076  * a wrapper to rte_mempool functions.
1077  *
1078  * @param name
1079  *   The name of the mbuf pool.
1080  * @param n
1081  *   The number of elements in the mbuf pool. The optimum size (in terms
1082  *   of memory usage) for a mempool is when n is a power of two minus one:
1083  *   n = (2^q - 1).
1084  * @param cache_size
1085  *   Size of the per-core object cache. See rte_mempool_create() for
1086  *   details.
1087  * @param priv_size
1088  *   Size of application private are between the rte_mbuf structure
1089  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
1090  * @param data_room_size
1091  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
1092  * @param socket_id
1093  *   The socket identifier where the memory should be allocated. The
1094  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
1095  *   reserved zone.
1096  * @return
1097  *   The pointer to the new allocated mempool, on success. NULL on error
1098  *   with rte_errno set appropriately. Possible rte_errno values include:
1099  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
1100  *    - E_RTE_SECONDARY - function was called from a secondary process instance
1101  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
1102  *    - ENOSPC - the maximum number of memzones has already been allocated
1103  *    - EEXIST - a memzone with the same name already exists
1104  *    - ENOMEM - no appropriate memory area found in which to create memzone
1105  */
1106 struct rte_mempool *
1107 rte_pktmbuf_pool_create(const char *name, unsigned n,
1108         unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
1109         int socket_id);
1110
1111 /**
1112  * Create a mbuf pool with a given mempool ops name
1113  *
1114  * This function creates and initializes a packet mbuf pool. It is
1115  * a wrapper to rte_mempool functions.
1116  *
1117  * @param name
1118  *   The name of the mbuf pool.
1119  * @param n
1120  *   The number of elements in the mbuf pool. The optimum size (in terms
1121  *   of memory usage) for a mempool is when n is a power of two minus one:
1122  *   n = (2^q - 1).
1123  * @param cache_size
1124  *   Size of the per-core object cache. See rte_mempool_create() for
1125  *   details.
1126  * @param priv_size
1127  *   Size of application private are between the rte_mbuf structure
1128  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
1129  * @param data_room_size
1130  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
1131  * @param socket_id
1132  *   The socket identifier where the memory should be allocated. The
1133  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
1134  *   reserved zone.
1135  * @param ops_name
1136  *   The mempool ops name to be used for this mempool instead of
1137  *   default mempool. The value can be *NULL* to use default mempool.
1138  * @return
1139  *   The pointer to the new allocated mempool, on success. NULL on error
1140  *   with rte_errno set appropriately. Possible rte_errno values include:
1141  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
1142  *    - E_RTE_SECONDARY - function was called from a secondary process instance
1143  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
1144  *    - ENOSPC - the maximum number of memzones has already been allocated
1145  *    - EEXIST - a memzone with the same name already exists
1146  *    - ENOMEM - no appropriate memory area found in which to create memzone
1147  */
1148 struct rte_mempool * __rte_experimental
1149 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
1150         unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
1151         int socket_id, const char *ops_name);
1152
1153 /**
1154  * Get the data room size of mbufs stored in a pktmbuf_pool
1155  *
1156  * The data room size is the amount of data that can be stored in a
1157  * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
1158  *
1159  * @param mp
1160  *   The packet mbuf pool.
1161  * @return
1162  *   The data room size of mbufs stored in this mempool.
1163  */
1164 static inline uint16_t
1165 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
1166 {
1167         struct rte_pktmbuf_pool_private *mbp_priv;
1168
1169         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1170         return mbp_priv->mbuf_data_room_size;
1171 }
1172
1173 /**
1174  * Get the application private size of mbufs stored in a pktmbuf_pool
1175  *
1176  * The private size of mbuf is a zone located between the rte_mbuf
1177  * structure and the data buffer where an application can store data
1178  * associated to a packet.
1179  *
1180  * @param mp
1181  *   The packet mbuf pool.
1182  * @return
1183  *   The private size of mbufs stored in this mempool.
1184  */
1185 static inline uint16_t
1186 rte_pktmbuf_priv_size(struct rte_mempool *mp)
1187 {
1188         struct rte_pktmbuf_pool_private *mbp_priv;
1189
1190         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1191         return mbp_priv->mbuf_priv_size;
1192 }
1193
1194 /**
1195  * Reset the data_off field of a packet mbuf to its default value.
1196  *
1197  * The given mbuf must have only one segment, which should be empty.
1198  *
1199  * @param m
1200  *   The packet mbuf's data_off field has to be reset.
1201  */
1202 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
1203 {
1204         m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
1205 }
1206
1207 /**
1208  * Reset the fields of a packet mbuf to their default values.
1209  *
1210  * The given mbuf must have only one segment.
1211  *
1212  * @param m
1213  *   The packet mbuf to be resetted.
1214  */
1215 #define MBUF_INVALID_PORT UINT16_MAX
1216
1217 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
1218 {
1219         m->next = NULL;
1220         m->pkt_len = 0;
1221         m->tx_offload = 0;
1222         m->vlan_tci = 0;
1223         m->vlan_tci_outer = 0;
1224         m->nb_segs = 1;
1225         m->port = MBUF_INVALID_PORT;
1226
1227         m->ol_flags = 0;
1228         m->packet_type = 0;
1229         rte_pktmbuf_reset_headroom(m);
1230
1231         m->data_len = 0;
1232         __rte_mbuf_sanity_check(m, 1);
1233 }
1234
1235 /**
1236  * Allocate a new mbuf from a mempool.
1237  *
1238  * This new mbuf contains one segment, which has a length of 0. The pointer
1239  * to data is initialized to have some bytes of headroom in the buffer
1240  * (if buffer size allows).
1241  *
1242  * @param mp
1243  *   The mempool from which the mbuf is allocated.
1244  * @return
1245  *   - The pointer to the new mbuf on success.
1246  *   - NULL if allocation failed.
1247  */
1248 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
1249 {
1250         struct rte_mbuf *m;
1251         if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
1252                 rte_pktmbuf_reset(m);
1253         return m;
1254 }
1255
1256 /**
1257  * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
1258  * values.
1259  *
1260  *  @param pool
1261  *    The mempool from which mbufs are allocated.
1262  *  @param mbufs
1263  *    Array of pointers to mbufs
1264  *  @param count
1265  *    Array size
1266  *  @return
1267  *   - 0: Success
1268  *   - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
1269  */
1270 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
1271          struct rte_mbuf **mbufs, unsigned count)
1272 {
1273         unsigned idx = 0;
1274         int rc;
1275
1276         rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
1277         if (unlikely(rc))
1278                 return rc;
1279
1280         /* To understand duff's device on loop unwinding optimization, see
1281          * https://en.wikipedia.org/wiki/Duff's_device.
1282          * Here while() loop is used rather than do() while{} to avoid extra
1283          * check if count is zero.
1284          */
1285         switch (count % 4) {
1286         case 0:
1287                 while (idx != count) {
1288                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1289                         rte_pktmbuf_reset(mbufs[idx]);
1290                         idx++;
1291                         /* fall-through */
1292         case 3:
1293                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1294                         rte_pktmbuf_reset(mbufs[idx]);
1295                         idx++;
1296                         /* fall-through */
1297         case 2:
1298                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1299                         rte_pktmbuf_reset(mbufs[idx]);
1300                         idx++;
1301                         /* fall-through */
1302         case 1:
1303                         MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1304                         rte_pktmbuf_reset(mbufs[idx]);
1305                         idx++;
1306                         /* fall-through */
1307                 }
1308         }
1309         return 0;
1310 }
1311
1312 /**
1313  * Attach packet mbuf to another packet mbuf.
1314  *
1315  * After attachment we refer the mbuf we attached as 'indirect',
1316  * while mbuf we attached to as 'direct'.
1317  * The direct mbuf's reference counter is incremented.
1318  *
1319  * Right now, not supported:
1320  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
1321  *  - mbuf we trying to attach (mi) is used by someone else
1322  *    e.g. it's reference counter is greater then 1.
1323  *
1324  * @param mi
1325  *   The indirect packet mbuf.
1326  * @param m
1327  *   The packet mbuf we're attaching to.
1328  */
1329 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1330 {
1331         struct rte_mbuf *md;
1332
1333         RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1334             rte_mbuf_refcnt_read(mi) == 1);
1335
1336         /* if m is not direct, get the mbuf that embeds the data */
1337         if (RTE_MBUF_DIRECT(m))
1338                 md = m;
1339         else
1340                 md = rte_mbuf_from_indirect(m);
1341
1342         rte_mbuf_refcnt_update(md, 1);
1343         mi->priv_size = m->priv_size;
1344         mi->buf_iova = m->buf_iova;
1345         mi->buf_addr = m->buf_addr;
1346         mi->buf_len = m->buf_len;
1347
1348         mi->data_off = m->data_off;
1349         mi->data_len = m->data_len;
1350         mi->port = m->port;
1351         mi->vlan_tci = m->vlan_tci;
1352         mi->vlan_tci_outer = m->vlan_tci_outer;
1353         mi->tx_offload = m->tx_offload;
1354         mi->hash = m->hash;
1355
1356         mi->next = NULL;
1357         mi->pkt_len = mi->data_len;
1358         mi->nb_segs = 1;
1359         mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1360         mi->packet_type = m->packet_type;
1361         mi->timestamp = m->timestamp;
1362
1363         __rte_mbuf_sanity_check(mi, 1);
1364         __rte_mbuf_sanity_check(m, 0);
1365 }
1366
1367 /**
1368  * Detach an indirect packet mbuf.
1369  *
1370  *  - restore original mbuf address and length values.
1371  *  - reset pktmbuf data and data_len to their default values.
1372  *  - decrement the direct mbuf's reference counter. When the
1373  *  reference counter becomes 0, the direct mbuf is freed.
1374  *
1375  * All other fields of the given packet mbuf will be left intact.
1376  *
1377  * @param m
1378  *   The indirect attached packet mbuf.
1379  */
1380 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1381 {
1382         struct rte_mbuf *md = rte_mbuf_from_indirect(m);
1383         struct rte_mempool *mp = m->pool;
1384         uint32_t mbuf_size, buf_len, priv_size;
1385
1386         priv_size = rte_pktmbuf_priv_size(mp);
1387         mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1388         buf_len = rte_pktmbuf_data_room_size(mp);
1389
1390         m->priv_size = priv_size;
1391         m->buf_addr = (char *)m + mbuf_size;
1392         m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1393         m->buf_len = (uint16_t)buf_len;
1394         rte_pktmbuf_reset_headroom(m);
1395         m->data_len = 0;
1396         m->ol_flags = 0;
1397
1398         if (rte_mbuf_refcnt_update(md, -1) == 0) {
1399                 md->next = NULL;
1400                 md->nb_segs = 1;
1401                 rte_mbuf_refcnt_set(md, 1);
1402                 rte_mbuf_raw_free(md);
1403         }
1404 }
1405
1406 /**
1407  * Decrease reference counter and unlink a mbuf segment
1408  *
1409  * This function does the same than a free, except that it does not
1410  * return the segment to its pool.
1411  * It decreases the reference counter, and if it reaches 0, it is
1412  * detached from its parent for an indirect mbuf.
1413  *
1414  * @param m
1415  *   The mbuf to be unlinked
1416  * @return
1417  *   - (m) if it is the last reference. It can be recycled or freed.
1418  *   - (NULL) if the mbuf still has remaining references on it.
1419  */
1420 static __rte_always_inline struct rte_mbuf *
1421 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1422 {
1423         __rte_mbuf_sanity_check(m, 0);
1424
1425         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1426
1427                 if (RTE_MBUF_INDIRECT(m))
1428                         rte_pktmbuf_detach(m);
1429
1430                 if (m->next != NULL) {
1431                         m->next = NULL;
1432                         m->nb_segs = 1;
1433                 }
1434
1435                 return m;
1436
1437         } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1438
1439                 if (RTE_MBUF_INDIRECT(m))
1440                         rte_pktmbuf_detach(m);
1441
1442                 if (m->next != NULL) {
1443                         m->next = NULL;
1444                         m->nb_segs = 1;
1445                 }
1446                 rte_mbuf_refcnt_set(m, 1);
1447
1448                 return m;
1449         }
1450         return NULL;
1451 }
1452
1453 /* deprecated, replaced by rte_pktmbuf_prefree_seg() */
1454 __rte_deprecated
1455 static inline struct rte_mbuf *
1456 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1457 {
1458         return rte_pktmbuf_prefree_seg(m);
1459 }
1460
1461 /**
1462  * Free a segment of a packet mbuf into its original mempool.
1463  *
1464  * Free an mbuf, without parsing other segments in case of chained
1465  * buffers.
1466  *
1467  * @param m
1468  *   The packet mbuf segment to be freed.
1469  */
1470 static __rte_always_inline void
1471 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1472 {
1473         m = rte_pktmbuf_prefree_seg(m);
1474         if (likely(m != NULL))
1475                 rte_mbuf_raw_free(m);
1476 }
1477
1478 /**
1479  * Free a packet mbuf back into its original mempool.
1480  *
1481  * Free an mbuf, and all its segments in case of chained buffers. Each
1482  * segment is added back into its original mempool.
1483  *
1484  * @param m
1485  *   The packet mbuf to be freed. If NULL, the function does nothing.
1486  */
1487 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1488 {
1489         struct rte_mbuf *m_next;
1490
1491         if (m != NULL)
1492                 __rte_mbuf_sanity_check(m, 1);
1493
1494         while (m != NULL) {
1495                 m_next = m->next;
1496                 rte_pktmbuf_free_seg(m);
1497                 m = m_next;
1498         }
1499 }
1500
1501 /**
1502  * Creates a "clone" of the given packet mbuf.
1503  *
1504  * Walks through all segments of the given packet mbuf, and for each of them:
1505  *  - Creates a new packet mbuf from the given pool.
1506  *  - Attaches newly created mbuf to the segment.
1507  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1508  * from the original packet mbuf.
1509  *
1510  * @param md
1511  *   The packet mbuf to be cloned.
1512  * @param mp
1513  *   The mempool from which the "clone" mbufs are allocated.
1514  * @return
1515  *   - The pointer to the new "clone" mbuf on success.
1516  *   - NULL if allocation fails.
1517  */
1518 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
1519                 struct rte_mempool *mp)
1520 {
1521         struct rte_mbuf *mc, *mi, **prev;
1522         uint32_t pktlen;
1523         uint16_t nseg;
1524
1525         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
1526                 return NULL;
1527
1528         mi = mc;
1529         prev = &mi->next;
1530         pktlen = md->pkt_len;
1531         nseg = 0;
1532
1533         do {
1534                 nseg++;
1535                 rte_pktmbuf_attach(mi, md);
1536                 *prev = mi;
1537                 prev = &mi->next;
1538         } while ((md = md->next) != NULL &&
1539             (mi = rte_pktmbuf_alloc(mp)) != NULL);
1540
1541         *prev = NULL;
1542         mc->nb_segs = nseg;
1543         mc->pkt_len = pktlen;
1544
1545         /* Allocation of new indirect segment failed */
1546         if (unlikely (mi == NULL)) {
1547                 rte_pktmbuf_free(mc);
1548                 return NULL;
1549         }
1550
1551         __rte_mbuf_sanity_check(mc, 1);
1552         return mc;
1553 }
1554
1555 /**
1556  * Adds given value to the refcnt of all packet mbuf segments.
1557  *
1558  * Walks through all segments of given packet mbuf and for each of them
1559  * invokes rte_mbuf_refcnt_update().
1560  *
1561  * @param m
1562  *   The packet mbuf whose refcnt to be updated.
1563  * @param v
1564  *   The value to add to the mbuf's segments refcnt.
1565  */
1566 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1567 {
1568         __rte_mbuf_sanity_check(m, 1);
1569
1570         do {
1571                 rte_mbuf_refcnt_update(m, v);
1572         } while ((m = m->next) != NULL);
1573 }
1574
1575 /**
1576  * Get the headroom in a packet mbuf.
1577  *
1578  * @param m
1579  *   The packet mbuf.
1580  * @return
1581  *   The length of the headroom.
1582  */
1583 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1584 {
1585         __rte_mbuf_sanity_check(m, 0);
1586         return m->data_off;
1587 }
1588
1589 /**
1590  * Get the tailroom of a packet mbuf.
1591  *
1592  * @param m
1593  *   The packet mbuf.
1594  * @return
1595  *   The length of the tailroom.
1596  */
1597 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1598 {
1599         __rte_mbuf_sanity_check(m, 0);
1600         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1601                           m->data_len);
1602 }
1603
1604 /**
1605  * Get the last segment of the packet.
1606  *
1607  * @param m
1608  *   The packet mbuf.
1609  * @return
1610  *   The last segment of the given mbuf.
1611  */
1612 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1613 {
1614         __rte_mbuf_sanity_check(m, 1);
1615         while (m->next != NULL)
1616                 m = m->next;
1617         return m;
1618 }
1619
1620 /**
1621  * A macro that points to an offset into the data in the mbuf.
1622  *
1623  * The returned pointer is cast to type t. Before using this
1624  * function, the user must ensure that the first segment is large
1625  * enough to accommodate its data.
1626  *
1627  * @param m
1628  *   The packet mbuf.
1629  * @param o
1630  *   The offset into the mbuf data.
1631  * @param t
1632  *   The type to cast the result into.
1633  */
1634 #define rte_pktmbuf_mtod_offset(m, t, o)        \
1635         ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
1636
1637 /**
1638  * A macro that points to the start of the data in the mbuf.
1639  *
1640  * The returned pointer is cast to type t. Before using this
1641  * function, the user must ensure that the first segment is large
1642  * enough to accommodate its data.
1643  *
1644  * @param m
1645  *   The packet mbuf.
1646  * @param t
1647  *   The type to cast the result into.
1648  */
1649 #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
1650
1651 /**
1652  * A macro that returns the IO address that points to an offset of the
1653  * start of the data in the mbuf
1654  *
1655  * @param m
1656  *   The packet mbuf.
1657  * @param o
1658  *   The offset into the data to calculate address from.
1659  */
1660 #define rte_pktmbuf_iova_offset(m, o) \
1661         (rte_iova_t)((m)->buf_iova + (m)->data_off + (o))
1662
1663 /* deprecated */
1664 #define rte_pktmbuf_mtophys_offset(m, o) \
1665         rte_pktmbuf_iova_offset(m, o)
1666
1667 /**
1668  * A macro that returns the IO address that points to the start of the
1669  * data in the mbuf
1670  *
1671  * @param m
1672  *   The packet mbuf.
1673  */
1674 #define rte_pktmbuf_iova(m) rte_pktmbuf_iova_offset(m, 0)
1675
1676 /* deprecated */
1677 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_iova(m)
1678
1679 /**
1680  * A macro that returns the length of the packet.
1681  *
1682  * The value can be read or assigned.
1683  *
1684  * @param m
1685  *   The packet mbuf.
1686  */
1687 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1688
1689 /**
1690  * A macro that returns the length of the segment.
1691  *
1692  * The value can be read or assigned.
1693  *
1694  * @param m
1695  *   The packet mbuf.
1696  */
1697 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1698
1699 /**
1700  * Prepend len bytes to an mbuf data area.
1701  *
1702  * Returns a pointer to the new
1703  * data start address. If there is not enough headroom in the first
1704  * segment, the function will return NULL, without modifying the mbuf.
1705  *
1706  * @param m
1707  *   The pkt mbuf.
1708  * @param len
1709  *   The amount of data to prepend (in bytes).
1710  * @return
1711  *   A pointer to the start of the newly prepended data, or
1712  *   NULL if there is not enough headroom space in the first segment
1713  */
1714 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1715                                         uint16_t len)
1716 {
1717         __rte_mbuf_sanity_check(m, 1);
1718
1719         if (unlikely(len > rte_pktmbuf_headroom(m)))
1720                 return NULL;
1721
1722         m->data_off -= len;
1723         m->data_len = (uint16_t)(m->data_len + len);
1724         m->pkt_len  = (m->pkt_len + len);
1725
1726         return (char *)m->buf_addr + m->data_off;
1727 }
1728
1729 /**
1730  * Append len bytes to an mbuf.
1731  *
1732  * Append len bytes to an mbuf and return a pointer to the start address
1733  * of the added data. If there is not enough tailroom in the last
1734  * segment, the function will return NULL, without modifying the mbuf.
1735  *
1736  * @param m
1737  *   The packet mbuf.
1738  * @param len
1739  *   The amount of data to append (in bytes).
1740  * @return
1741  *   A pointer to the start of the newly appended data, or
1742  *   NULL if there is not enough tailroom space in the last segment
1743  */
1744 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1745 {
1746         void *tail;
1747         struct rte_mbuf *m_last;
1748
1749         __rte_mbuf_sanity_check(m, 1);
1750
1751         m_last = rte_pktmbuf_lastseg(m);
1752         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1753                 return NULL;
1754
1755         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1756         m_last->data_len = (uint16_t)(m_last->data_len + len);
1757         m->pkt_len  = (m->pkt_len + len);
1758         return (char*) tail;
1759 }
1760
1761 /**
1762  * Remove len bytes at the beginning of an mbuf.
1763  *
1764  * Returns a pointer to the start address of the new data area. If the
1765  * length is greater than the length of the first segment, then the
1766  * function will fail and return NULL, without modifying the mbuf.
1767  *
1768  * @param m
1769  *   The packet mbuf.
1770  * @param len
1771  *   The amount of data to remove (in bytes).
1772  * @return
1773  *   A pointer to the new start of the data.
1774  */
1775 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1776 {
1777         __rte_mbuf_sanity_check(m, 1);
1778
1779         if (unlikely(len > m->data_len))
1780                 return NULL;
1781
1782         m->data_len = (uint16_t)(m->data_len - len);
1783         m->data_off += len;
1784         m->pkt_len  = (m->pkt_len - len);
1785         return (char *)m->buf_addr + m->data_off;
1786 }
1787
1788 /**
1789  * Remove len bytes of data at the end of the mbuf.
1790  *
1791  * If the length is greater than the length of the last segment, the
1792  * function will fail and return -1 without modifying the mbuf.
1793  *
1794  * @param m
1795  *   The packet mbuf.
1796  * @param len
1797  *   The amount of data to remove (in bytes).
1798  * @return
1799  *   - 0: On success.
1800  *   - -1: On error.
1801  */
1802 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1803 {
1804         struct rte_mbuf *m_last;
1805
1806         __rte_mbuf_sanity_check(m, 1);
1807
1808         m_last = rte_pktmbuf_lastseg(m);
1809         if (unlikely(len > m_last->data_len))
1810                 return -1;
1811
1812         m_last->data_len = (uint16_t)(m_last->data_len - len);
1813         m->pkt_len  = (m->pkt_len - len);
1814         return 0;
1815 }
1816
1817 /**
1818  * Test if mbuf data is contiguous.
1819  *
1820  * @param m
1821  *   The packet mbuf.
1822  * @return
1823  *   - 1, if all data is contiguous (one segment).
1824  *   - 0, if there is several segments.
1825  */
1826 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1827 {
1828         __rte_mbuf_sanity_check(m, 1);
1829         return !!(m->nb_segs == 1);
1830 }
1831
1832 /**
1833  * @internal used by rte_pktmbuf_read().
1834  */
1835 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1836         uint32_t len, void *buf);
1837
1838 /**
1839  * Read len data bytes in a mbuf at specified offset.
1840  *
1841  * If the data is contiguous, return the pointer in the mbuf data, else
1842  * copy the data in the buffer provided by the user and return its
1843  * pointer.
1844  *
1845  * @param m
1846  *   The pointer to the mbuf.
1847  * @param off
1848  *   The offset of the data in the mbuf.
1849  * @param len
1850  *   The amount of bytes to read.
1851  * @param buf
1852  *   The buffer where data is copied if it is not contiguous in mbuf
1853  *   data. Its length should be at least equal to the len parameter.
1854  * @return
1855  *   The pointer to the data, either in the mbuf if it is contiguous,
1856  *   or in the user buffer. If mbuf is too small, NULL is returned.
1857  */
1858 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1859         uint32_t off, uint32_t len, void *buf)
1860 {
1861         if (likely(off + len <= rte_pktmbuf_data_len(m)))
1862                 return rte_pktmbuf_mtod_offset(m, char *, off);
1863         else
1864                 return __rte_pktmbuf_read(m, off, len, buf);
1865 }
1866
1867 /**
1868  * Chain an mbuf to another, thereby creating a segmented packet.
1869  *
1870  * Note: The implementation will do a linear walk over the segments to find
1871  * the tail entry. For cases when there are many segments, it's better to
1872  * chain the entries manually.
1873  *
1874  * @param head
1875  *   The head of the mbuf chain (the first packet)
1876  * @param tail
1877  *   The mbuf to put last in the chain
1878  *
1879  * @return
1880  *   - 0, on success.
1881  *   - -EOVERFLOW, if the chain segment limit exceeded
1882  */
1883 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1884 {
1885         struct rte_mbuf *cur_tail;
1886
1887         /* Check for number-of-segments-overflow */
1888         if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1889                 return -EOVERFLOW;
1890
1891         /* Chain 'tail' onto the old tail */
1892         cur_tail = rte_pktmbuf_lastseg(head);
1893         cur_tail->next = tail;
1894
1895         /* accumulate number of segments and total length. */
1896         head->nb_segs += tail->nb_segs;
1897         head->pkt_len += tail->pkt_len;
1898
1899         /* pkt_len is only set in the head */
1900         tail->pkt_len = tail->data_len;
1901
1902         return 0;
1903 }
1904
1905 /**
1906  * Validate general requirements for Tx offload in mbuf.
1907  *
1908  * This function checks correctness and completeness of Tx offload settings.
1909  *
1910  * @param m
1911  *   The packet mbuf to be validated.
1912  * @return
1913  *   0 if packet is valid
1914  */
1915 static inline int
1916 rte_validate_tx_offload(const struct rte_mbuf *m)
1917 {
1918         uint64_t ol_flags = m->ol_flags;
1919         uint64_t inner_l3_offset = m->l2_len;
1920
1921         /* Does packet set any of available offloads? */
1922         if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1923                 return 0;
1924
1925         if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
1926                 inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
1927
1928         /* Headers are fragmented */
1929         if (rte_pktmbuf_data_len(m) < inner_l3_offset + m->l3_len + m->l4_len)
1930                 return -ENOTSUP;
1931
1932         /* IP checksum can be counted only for IPv4 packet */
1933         if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1934                 return -EINVAL;
1935
1936         /* IP type not set when required */
1937         if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1938                 if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1939                         return -EINVAL;
1940
1941         /* Check requirements for TSO packet */
1942         if (ol_flags & PKT_TX_TCP_SEG)
1943                 if ((m->tso_segsz == 0) ||
1944                                 ((ol_flags & PKT_TX_IPV4) &&
1945                                 !(ol_flags & PKT_TX_IP_CKSUM)))
1946                         return -EINVAL;
1947
1948         /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1949         if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1950                         !(ol_flags & PKT_TX_OUTER_IPV4))
1951                 return -EINVAL;
1952
1953         return 0;
1954 }
1955
1956 /**
1957  * Linearize data in mbuf.
1958  *
1959  * This function moves the mbuf data in the first segment if there is enough
1960  * tailroom. The subsequent segments are unchained and freed.
1961  *
1962  * @param mbuf
1963  *   mbuf to linearize
1964  * @return
1965  *   - 0, on success
1966  *   - -1, on error
1967  */
1968 static inline int
1969 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1970 {
1971         int seg_len, copy_len;
1972         struct rte_mbuf *m;
1973         struct rte_mbuf *m_next;
1974         char *buffer;
1975
1976         if (rte_pktmbuf_is_contiguous(mbuf))
1977                 return 0;
1978
1979         /* Extend first segment to the total packet length */
1980         copy_len = rte_pktmbuf_pkt_len(mbuf) - rte_pktmbuf_data_len(mbuf);
1981
1982         if (unlikely(copy_len > rte_pktmbuf_tailroom(mbuf)))
1983                 return -1;
1984
1985         buffer = rte_pktmbuf_mtod_offset(mbuf, char *, mbuf->data_len);
1986         mbuf->data_len = (uint16_t)(mbuf->pkt_len);
1987
1988         /* Append data from next segments to the first one */
1989         m = mbuf->next;
1990         while (m != NULL) {
1991                 m_next = m->next;
1992
1993                 seg_len = rte_pktmbuf_data_len(m);
1994                 rte_memcpy(buffer, rte_pktmbuf_mtod(m, char *), seg_len);
1995                 buffer += seg_len;
1996
1997                 rte_pktmbuf_free_seg(m);
1998                 m = m_next;
1999         }
2000
2001         mbuf->next = NULL;
2002         mbuf->nb_segs = 1;
2003
2004         return 0;
2005 }
2006
2007 /**
2008  * Dump an mbuf structure to a file.
2009  *
2010  * Dump all fields for the given packet mbuf and all its associated
2011  * segments (in the case of a chained buffer).
2012  *
2013  * @param f
2014  *   A pointer to a file for output
2015  * @param m
2016  *   The packet mbuf.
2017  * @param dump_len
2018  *   If dump_len != 0, also dump the "dump_len" first data bytes of
2019  *   the packet.
2020  */
2021 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
2022
2023 #ifdef __cplusplus
2024 }
2025 #endif
2026
2027 #endif /* _RTE_MBUF_H_ */