mbuf: add definitions of unified packet types
[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  * This library provide an API to allocate/free packet mbufs, which are
48  * used to carry network packets.
49  *
50  * To understand the concepts of packet buffers or mbufs, you
51  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
52  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
53  * http://www.kohala.com/start/tcpipiv2.html
54  */
55
56 #include <stdint.h>
57 #include <rte_common.h>
58 #include <rte_mempool.h>
59 #include <rte_memory.h>
60 #include <rte_atomic.h>
61 #include <rte_prefetch.h>
62 #include <rte_branch_prediction.h>
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 /* deprecated options */
69 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
70 #pragma GCC poison RTE_MBUF_REFCNT
71
72 /*
73  * Packet Offload Features Flags. It also carry packet type information.
74  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
75  *
76  * - RX flags start at bit position zero, and get added to the left of previous
77  *   flags.
78  * - The most-significant 8 bits are reserved for generic mbuf flags
79  * - TX flags therefore start at bit position 55 (i.e. 63-8), and new flags get
80  *   added to the right of the previously defined flags
81  *
82  * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
83  * rte_get_tx_ol_flag_name().
84  */
85 #define PKT_RX_VLAN_PKT      (1ULL << 0)  /**< RX packet is a 802.1q VLAN packet. */
86 #define PKT_RX_RSS_HASH      (1ULL << 1)  /**< RX packet with RSS hash result. */
87 #define PKT_RX_FDIR          (1ULL << 2)  /**< RX packet with FDIR match indicate. */
88 #define PKT_RX_L4_CKSUM_BAD  (1ULL << 3)  /**< L4 cksum of RX pkt. is not OK. */
89 #define PKT_RX_IP_CKSUM_BAD  (1ULL << 4)  /**< IP cksum of RX pkt. is not OK. */
90 #define PKT_RX_EIP_CKSUM_BAD (0ULL << 0)  /**< External IP header checksum error. */
91 #define PKT_RX_OVERSIZE      (0ULL << 0)  /**< Num of desc of an RX pkt oversize. */
92 #define PKT_RX_HBUF_OVERFLOW (0ULL << 0)  /**< Header buffer overflow. */
93 #define PKT_RX_RECIP_ERR     (0ULL << 0)  /**< Hardware processing error. */
94 #define PKT_RX_MAC_ERR       (0ULL << 0)  /**< MAC error. */
95 #define PKT_RX_IPV4_HDR      (1ULL << 5)  /**< RX packet with IPv4 header. */
96 #define PKT_RX_IPV4_HDR_EXT  (1ULL << 6)  /**< RX packet with extended IPv4 header. */
97 #define PKT_RX_IPV6_HDR      (1ULL << 7)  /**< RX packet with IPv6 header. */
98 #define PKT_RX_IPV6_HDR_EXT  (1ULL << 8)  /**< RX packet with extended IPv6 header. */
99 #define PKT_RX_IEEE1588_PTP  (1ULL << 9)  /**< RX IEEE1588 L2 Ethernet PT Packet. */
100 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
101 #define PKT_RX_TUNNEL_IPV4_HDR (1ULL << 11) /**< RX tunnel packet with IPv4 header.*/
102 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
103 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
104 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
105 #define PKT_RX_QINQ_PKT      (1ULL << 15)  /**< RX packet with double VLAN stripped. */
106 /* add new RX flags here */
107
108 /* add new TX flags here */
109
110 /**
111  * Second VLAN insertion (QinQ) flag.
112  */
113 #define PKT_TX_QINQ_PKT    (1ULL << 49)   /**< TX packet with double VLAN inserted. */
114
115 /**
116  * TCP segmentation offload. To enable this offload feature for a
117  * packet to be transmitted on hardware supporting TSO:
118  *  - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
119  *    PKT_TX_TCP_CKSUM)
120  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
121  *  - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
122  *    to 0 in the packet
123  *  - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
124  *  - calculate the pseudo header checksum without taking ip_len in account,
125  *    and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
126  *    rte_ipv6_phdr_cksum() that can be used as helpers.
127  */
128 #define PKT_TX_TCP_SEG       (1ULL << 50)
129
130 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
131
132 /**
133  * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
134  * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
135  * L4 checksum offload, the user needs to:
136  *  - fill l2_len and l3_len in mbuf
137  *  - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
138  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
139  *  - calculate the pseudo header checksum and set it in the L4 header (only
140  *    for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
141  *    For SCTP, set the crc field to 0.
142  */
143 #define PKT_TX_L4_NO_CKSUM   (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
144 #define PKT_TX_TCP_CKSUM     (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
145 #define PKT_TX_SCTP_CKSUM    (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
146 #define PKT_TX_UDP_CKSUM     (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
147 #define PKT_TX_L4_MASK       (3ULL << 52) /**< Mask for L4 cksum offload request. */
148
149 /**
150  * Offload the IP checksum in the hardware. The flag PKT_TX_IPV4 should
151  * also be set by the application, although a PMD will only check
152  * PKT_TX_IP_CKSUM.
153  *  - set the IP checksum field in the packet to 0
154  *  - fill the mbuf offload information: l2_len, l3_len
155  */
156 #define PKT_TX_IP_CKSUM      (1ULL << 54)
157
158 /**
159  * Packet is IPv4. This flag must be set when using any offload feature
160  * (TSO, L3 or L4 checksum) to tell the NIC that the packet is an IPv4
161  * packet. If the packet is a tunneled packet, this flag is related to
162  * the inner headers.
163  */
164 #define PKT_TX_IPV4          (1ULL << 55)
165
166 /**
167  * Packet is IPv6. This flag must be set when using an offload feature
168  * (TSO or L4 checksum) to tell the NIC that the packet is an IPv6
169  * packet. If the packet is a tunneled packet, this flag is related to
170  * the inner headers.
171  */
172 #define PKT_TX_IPV6          (1ULL << 56)
173
174 #define PKT_TX_VLAN_PKT      (1ULL << 57) /**< TX packet is a 802.1q VLAN packet. */
175
176 /**
177  * Offload the IP checksum of an external header in the hardware. The
178  * flag PKT_TX_OUTER_IPV4 should also be set by the application, alto ugh
179  * a PMD will only check PKT_TX_IP_CKSUM.  The IP checksum field in the
180  * packet must be set to 0.
181  *  - set the outer IP checksum field in the packet to 0
182  *  - fill the mbuf offload information: outer_l2_len, outer_l3_len
183  */
184 #define PKT_TX_OUTER_IP_CKSUM   (1ULL << 58)
185
186 /**
187  * Packet outer header is IPv4. This flag must be set when using any
188  * outer offload feature (L3 or L4 checksum) to tell the NIC that the
189  * outer header of the tunneled packet is an IPv4 packet.
190  */
191 #define PKT_TX_OUTER_IPV4   (1ULL << 59)
192
193 /**
194  * Packet outer header is IPv6. This flag must be set when using any
195  * outer offload feature (L4 checksum) to tell the NIC that the outer
196  * header of the tunneled packet is an IPv6 packet.
197  */
198 #define PKT_TX_OUTER_IPV6    (1ULL << 60)
199
200 #define IND_ATTACHED_MBUF    (1ULL << 62) /**< Indirect attached mbuf */
201
202 /* Use final bit of flags to indicate a control mbuf */
203 #define CTRL_MBUF_FLAG       (1ULL << 63) /**< Mbuf contains control data */
204
205 #ifdef RTE_NEXT_ABI
206 /*
207  * 32 bits are divided into several fields to mark packet types. Note that
208  * each field is indexical.
209  * - Bit 3:0 is for L2 types.
210  * - Bit 7:4 is for L3 or outer L3 (for tunneling case) types.
211  * - Bit 11:8 is for L4 or outer L4 (for tunneling case) types.
212  * - Bit 15:12 is for tunnel types.
213  * - Bit 19:16 is for inner L2 types.
214  * - Bit 23:20 is for inner L3 types.
215  * - Bit 27:24 is for inner L4 types.
216  * - Bit 31:28 is reserved.
217  *
218  * To be compatible with Vector PMD, RTE_PTYPE_L3_IPV4, RTE_PTYPE_L3_IPV4_EXT,
219  * RTE_PTYPE_L3_IPV6, RTE_PTYPE_L3_IPV6_EXT, RTE_PTYPE_L4_TCP, RTE_PTYPE_L4_UDP
220  * and RTE_PTYPE_L4_SCTP should be kept as below in a contiguous 7 bits.
221  *
222  * Note that L3 types values are selected for checking IPV4/IPV6 header from
223  * performance point of view. Reading annotations of RTE_ETH_IS_IPV4_HDR and
224  * RTE_ETH_IS_IPV6_HDR is needed for any future changes of L3 type values.
225  *
226  * Note that the packet types of the same packet recognized by different
227  * hardware may be different, as different hardware may have different
228  * capability of packet type recognition.
229  *
230  * examples:
231  * <'ether type'=0x0800
232  * | 'version'=4, 'protocol'=0x29
233  * | 'version'=6, 'next header'=0x3A
234  * | 'ICMPv6 header'>
235  * will be recognized on i40e hardware as packet type combination of,
236  * RTE_PTYPE_L2_ETHER |
237  * RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
238  * RTE_PTYPE_TUNNEL_IP |
239  * RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
240  * RTE_PTYPE_INNER_L4_ICMP.
241  *
242  * <'ether type'=0x86DD
243  * | 'version'=6, 'next header'=0x2F
244  * | 'GRE header'
245  * | 'version'=6, 'next header'=0x11
246  * | 'UDP header'>
247  * will be recognized on i40e hardware as packet type combination of,
248  * RTE_PTYPE_L2_ETHER |
249  * RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
250  * RTE_PTYPE_TUNNEL_GRENAT |
251  * RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
252  * RTE_PTYPE_INNER_L4_UDP.
253  */
254 #define RTE_PTYPE_UNKNOWN                   0x00000000
255 /**
256  * Ethernet packet type.
257  * It is used for outer packet for tunneling cases.
258  *
259  * Packet format:
260  * <'ether type'=[0x0800|0x86DD]>
261  */
262 #define RTE_PTYPE_L2_ETHER                  0x00000001
263 /**
264  * Ethernet packet type for time sync.
265  *
266  * Packet format:
267  * <'ether type'=0x88F7>
268  */
269 #define RTE_PTYPE_L2_ETHER_TIMESYNC         0x00000002
270 /**
271  * ARP (Address Resolution Protocol) packet type.
272  *
273  * Packet format:
274  * <'ether type'=0x0806>
275  */
276 #define RTE_PTYPE_L2_ETHER_ARP              0x00000003
277 /**
278  * LLDP (Link Layer Discovery Protocol) packet type.
279  *
280  * Packet format:
281  * <'ether type'=0x88CC>
282  */
283 #define RTE_PTYPE_L2_ETHER_LLDP             0x00000004
284 /**
285  * Mask of layer 2 packet types.
286  * It is used for outer packet for tunneling cases.
287  */
288 #define RTE_PTYPE_L2_MASK                   0x0000000f
289 /**
290  * IP (Internet Protocol) version 4 packet type.
291  * It is used for outer packet for tunneling cases, and does not contain any
292  * header option.
293  *
294  * Packet format:
295  * <'ether type'=0x0800
296  * | 'version'=4, 'ihl'=5>
297  */
298 #define RTE_PTYPE_L3_IPV4                   0x00000010
299 /**
300  * IP (Internet Protocol) version 4 packet type.
301  * It is used for outer packet for tunneling cases, and contains header
302  * options.
303  *
304  * Packet format:
305  * <'ether type'=0x0800
306  * | 'version'=4, 'ihl'=[6-15], 'options'>
307  */
308 #define RTE_PTYPE_L3_IPV4_EXT               0x00000030
309 /**
310  * IP (Internet Protocol) version 6 packet type.
311  * It is used for outer packet for tunneling cases, and does not contain any
312  * extension header.
313  *
314  * Packet format:
315  * <'ether type'=0x86DD
316  * | 'version'=6, 'next header'=0x3B>
317  */
318 #define RTE_PTYPE_L3_IPV6                   0x00000040
319 /**
320  * IP (Internet Protocol) version 4 packet type.
321  * It is used for outer packet for tunneling cases, and may or maynot contain
322  * header options.
323  *
324  * Packet format:
325  * <'ether type'=0x0800
326  * | 'version'=4, 'ihl'=[5-15], <'options'>>
327  */
328 #define RTE_PTYPE_L3_IPV4_EXT_UNKNOWN       0x00000090
329 /**
330  * IP (Internet Protocol) version 6 packet type.
331  * It is used for outer packet for tunneling cases, and contains extension
332  * headers.
333  *
334  * Packet format:
335  * <'ether type'=0x86DD
336  * | 'version'=6, 'next header'=[0x0|0x2B|0x2C|0x32|0x33|0x3C|0x87],
337  *   'extension headers'>
338  */
339 #define RTE_PTYPE_L3_IPV6_EXT               0x000000c0
340 /**
341  * IP (Internet Protocol) version 6 packet type.
342  * It is used for outer packet for tunneling cases, and may or maynot contain
343  * extension headers.
344  *
345  * Packet format:
346  * <'ether type'=0x86DD
347  * | 'version'=6, 'next header'=[0x3B|0x0|0x2B|0x2C|0x32|0x33|0x3C|0x87],
348  *   <'extension headers'>>
349  */
350 #define RTE_PTYPE_L3_IPV6_EXT_UNKNOWN       0x000000e0
351 /**
352  * Mask of layer 3 packet types.
353  * It is used for outer packet for tunneling cases.
354  */
355 #define RTE_PTYPE_L3_MASK                   0x000000f0
356 /**
357  * TCP (Transmission Control Protocol) packet type.
358  * It is used for outer packet for tunneling cases.
359  *
360  * Packet format:
361  * <'ether type'=0x0800
362  * | 'version'=4, 'protocol'=6, 'MF'=0>
363  * or,
364  * <'ether type'=0x86DD
365  * | 'version'=6, 'next header'=6>
366  */
367 #define RTE_PTYPE_L4_TCP                    0x00000100
368 /**
369  * UDP (User Datagram Protocol) packet type.
370  * It is used for outer packet for tunneling cases.
371  *
372  * Packet format:
373  * <'ether type'=0x0800
374  * | 'version'=4, 'protocol'=17, 'MF'=0>
375  * or,
376  * <'ether type'=0x86DD
377  * | 'version'=6, 'next header'=17>
378  */
379 #define RTE_PTYPE_L4_UDP                    0x00000200
380 /**
381  * Fragmented IP (Internet Protocol) packet type.
382  * It is used for outer packet for tunneling cases.
383  *
384  * It refers to those packets of any IP types, which can be recognized as
385  * fragmented. A fragmented packet cannot be recognized as any other L4 types
386  * (RTE_PTYPE_L4_TCP, RTE_PTYPE_L4_UDP, RTE_PTYPE_L4_SCTP, RTE_PTYPE_L4_ICMP,
387  * RTE_PTYPE_L4_NONFRAG).
388  *
389  * Packet format:
390  * <'ether type'=0x0800
391  * | 'version'=4, 'MF'=1>
392  * or,
393  * <'ether type'=0x86DD
394  * | 'version'=6, 'next header'=44>
395  */
396 #define RTE_PTYPE_L4_FRAG                   0x00000300
397 /**
398  * SCTP (Stream Control Transmission Protocol) packet type.
399  * It is used for outer packet for tunneling cases.
400  *
401  * Packet format:
402  * <'ether type'=0x0800
403  * | 'version'=4, 'protocol'=132, 'MF'=0>
404  * or,
405  * <'ether type'=0x86DD
406  * | 'version'=6, 'next header'=132>
407  */
408 #define RTE_PTYPE_L4_SCTP                   0x00000400
409 /**
410  * ICMP (Internet Control Message Protocol) packet type.
411  * It is used for outer packet for tunneling cases.
412  *
413  * Packet format:
414  * <'ether type'=0x0800
415  * | 'version'=4, 'protocol'=1, 'MF'=0>
416  * or,
417  * <'ether type'=0x86DD
418  * | 'version'=6, 'next header'=1>
419  */
420 #define RTE_PTYPE_L4_ICMP                   0x00000500
421 /**
422  * Non-fragmented IP (Internet Protocol) packet type.
423  * It is used for outer packet for tunneling cases.
424  *
425  * It refers to those packets of any IP types, while cannot be recognized as
426  * any of above L4 types (RTE_PTYPE_L4_TCP, RTE_PTYPE_L4_UDP,
427  * RTE_PTYPE_L4_FRAG, RTE_PTYPE_L4_SCTP, RTE_PTYPE_L4_ICMP).
428  *
429  * Packet format:
430  * <'ether type'=0x0800
431  * | 'version'=4, 'protocol'!=[6|17|132|1], 'MF'=0>
432  * or,
433  * <'ether type'=0x86DD
434  * | 'version'=6, 'next header'!=[6|17|44|132|1]>
435  */
436 #define RTE_PTYPE_L4_NONFRAG                0x00000600
437 /**
438  * Mask of layer 4 packet types.
439  * It is used for outer packet for tunneling cases.
440  */
441 #define RTE_PTYPE_L4_MASK                   0x00000f00
442 /**
443  * IP (Internet Protocol) in IP (Internet Protocol) tunneling packet type.
444  *
445  * Packet format:
446  * <'ether type'=0x0800
447  * | 'version'=4, 'protocol'=[4|41]>
448  * or,
449  * <'ether type'=0x86DD
450  * | 'version'=6, 'next header'=[4|41]>
451  */
452 #define RTE_PTYPE_TUNNEL_IP                 0x00001000
453 /**
454  * GRE (Generic Routing Encapsulation) tunneling packet type.
455  *
456  * Packet format:
457  * <'ether type'=0x0800
458  * | 'version'=4, 'protocol'=47>
459  * or,
460  * <'ether type'=0x86DD
461  * | 'version'=6, 'next header'=47>
462  */
463 #define RTE_PTYPE_TUNNEL_GRE                0x00002000
464 /**
465  * VXLAN (Virtual eXtensible Local Area Network) tunneling packet type.
466  *
467  * Packet format:
468  * <'ether type'=0x0800
469  * | 'version'=4, 'protocol'=17
470  * | 'destination port'=4798>
471  * or,
472  * <'ether type'=0x86DD
473  * | 'version'=6, 'next header'=17
474  * | 'destination port'=4798>
475  */
476 #define RTE_PTYPE_TUNNEL_VXLAN              0x00003000
477 /**
478  * NVGRE (Network Virtualization using Generic Routing Encapsulation) tunneling
479  * packet type.
480  *
481  * Packet format:
482  * <'ether type'=0x0800
483  * | 'version'=4, 'protocol'=47
484  * | 'protocol type'=0x6558>
485  * or,
486  * <'ether type'=0x86DD
487  * | 'version'=6, 'next header'=47
488  * | 'protocol type'=0x6558'>
489  */
490 #define RTE_PTYPE_TUNNEL_NVGRE              0x00004000
491 /**
492  * GENEVE (Generic Network Virtualization Encapsulation) tunneling packet type.
493  *
494  * Packet format:
495  * <'ether type'=0x0800
496  * | 'version'=4, 'protocol'=17
497  * | 'destination port'=6081>
498  * or,
499  * <'ether type'=0x86DD
500  * | 'version'=6, 'next header'=17
501  * | 'destination port'=6081>
502  */
503 #define RTE_PTYPE_TUNNEL_GENEVE             0x00005000
504 /**
505  * Tunneling packet type of Teredo, VXLAN (Virtual eXtensible Local Area
506  * Network) or GRE (Generic Routing Encapsulation) could be recognized as this
507  * packet type, if they can not be recognized independently as of hardware
508  * capability.
509  */
510 #define RTE_PTYPE_TUNNEL_GRENAT             0x00006000
511 /**
512  * Mask of tunneling packet types.
513  */
514 #define RTE_PTYPE_TUNNEL_MASK               0x0000f000
515 /**
516  * Ethernet packet type.
517  * It is used for inner packet type only.
518  *
519  * Packet format (inner only):
520  * <'ether type'=[0x800|0x86DD]>
521  */
522 #define RTE_PTYPE_INNER_L2_ETHER            0x00010000
523 /**
524  * Ethernet packet type with VLAN (Virtual Local Area Network) tag.
525  *
526  * Packet format (inner only):
527  * <'ether type'=[0x800|0x86DD], vlan=[1-4095]>
528  */
529 #define RTE_PTYPE_INNER_L2_ETHER_VLAN       0x00020000
530 /**
531  * Mask of inner layer 2 packet types.
532  */
533 #define RTE_PTYPE_INNER_L2_MASK             0x000f0000
534 /**
535  * IP (Internet Protocol) version 4 packet type.
536  * It is used for inner packet only, and does not contain any header option.
537  *
538  * Packet format (inner only):
539  * <'ether type'=0x0800
540  * | 'version'=4, 'ihl'=5>
541  */
542 #define RTE_PTYPE_INNER_L3_IPV4             0x00100000
543 /**
544  * IP (Internet Protocol) version 4 packet type.
545  * It is used for inner packet only, and contains header options.
546  *
547  * Packet format (inner only):
548  * <'ether type'=0x0800
549  * | 'version'=4, 'ihl'=[6-15], 'options'>
550  */
551 #define RTE_PTYPE_INNER_L3_IPV4_EXT         0x00200000
552 /**
553  * IP (Internet Protocol) version 6 packet type.
554  * It is used for inner packet only, and does not contain any extension header.
555  *
556  * Packet format (inner only):
557  * <'ether type'=0x86DD
558  * | 'version'=6, 'next header'=0x3B>
559  */
560 #define RTE_PTYPE_INNER_L3_IPV6             0x00300000
561 /**
562  * IP (Internet Protocol) version 4 packet type.
563  * It is used for inner packet only, and may or maynot contain header options.
564  *
565  * Packet format (inner only):
566  * <'ether type'=0x0800
567  * | 'version'=4, 'ihl'=[5-15], <'options'>>
568  */
569 #define RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN 0x00400000
570 /**
571  * IP (Internet Protocol) version 6 packet type.
572  * It is used for inner packet only, and contains extension headers.
573  *
574  * Packet format (inner only):
575  * <'ether type'=0x86DD
576  * | 'version'=6, 'next header'=[0x0|0x2B|0x2C|0x32|0x33|0x3C|0x87],
577  *   'extension headers'>
578  */
579 #define RTE_PTYPE_INNER_L3_IPV6_EXT         0x00500000
580 /**
581  * IP (Internet Protocol) version 6 packet type.
582  * It is used for inner packet only, and may or maynot contain extension
583  * headers.
584  *
585  * Packet format (inner only):
586  * <'ether type'=0x86DD
587  * | 'version'=6, 'next header'=[0x3B|0x0|0x2B|0x2C|0x32|0x33|0x3C|0x87],
588  *   <'extension headers'>>
589  */
590 #define RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN 0x00600000
591 /**
592  * Mask of inner layer 3 packet types.
593  */
594 #define RTE_PTYPE_INNER_INNER_L3_MASK       0x00f00000
595 /**
596  * TCP (Transmission Control Protocol) packet type.
597  * It is used for inner packet only.
598  *
599  * Packet format (inner only):
600  * <'ether type'=0x0800
601  * | 'version'=4, 'protocol'=6, 'MF'=0>
602  * or,
603  * <'ether type'=0x86DD
604  * | 'version'=6, 'next header'=6>
605  */
606 #define RTE_PTYPE_INNER_L4_TCP              0x01000000
607 /**
608  * UDP (User Datagram Protocol) packet type.
609  * It is used for inner packet only.
610  *
611  * Packet format (inner only):
612  * <'ether type'=0x0800
613  * | 'version'=4, 'protocol'=17, 'MF'=0>
614  * or,
615  * <'ether type'=0x86DD
616  * | 'version'=6, 'next header'=17>
617  */
618 #define RTE_PTYPE_INNER_L4_UDP              0x02000000
619 /**
620  * Fragmented IP (Internet Protocol) packet type.
621  * It is used for inner packet only, and may or maynot have layer 4 packet.
622  *
623  * Packet format (inner only):
624  * <'ether type'=0x0800
625  * | 'version'=4, 'MF'=1>
626  * or,
627  * <'ether type'=0x86DD
628  * | 'version'=6, 'next header'=44>
629  */
630 #define RTE_PTYPE_INNER_L4_FRAG             0x03000000
631 /**
632  * SCTP (Stream Control Transmission Protocol) packet type.
633  * It is used for inner packet only.
634  *
635  * Packet format (inner only):
636  * <'ether type'=0x0800
637  * | 'version'=4, 'protocol'=132, 'MF'=0>
638  * or,
639  * <'ether type'=0x86DD
640  * | 'version'=6, 'next header'=132>
641  */
642 #define RTE_PTYPE_INNER_L4_SCTP             0x04000000
643 /**
644  * ICMP (Internet Control Message Protocol) packet type.
645  * It is used for inner packet only.
646  *
647  * Packet format (inner only):
648  * <'ether type'=0x0800
649  * | 'version'=4, 'protocol'=1, 'MF'=0>
650  * or,
651  * <'ether type'=0x86DD
652  * | 'version'=6, 'next header'=1>
653  */
654 #define RTE_PTYPE_INNER_L4_ICMP             0x05000000
655 /**
656  * Non-fragmented IP (Internet Protocol) packet type.
657  * It is used for inner packet only, and may or maynot have other unknown layer
658  * 4 packet types.
659  *
660  * Packet format (inner only):
661  * <'ether type'=0x0800
662  * | 'version'=4, 'protocol'!=[6|17|132|1], 'MF'=0>
663  * or,
664  * <'ether type'=0x86DD
665  * | 'version'=6, 'next header'!=[6|17|44|132|1]>
666  */
667 #define RTE_PTYPE_INNER_L4_NONFRAG          0x06000000
668 /**
669  * Mask of inner layer 4 packet types.
670  */
671 #define RTE_PTYPE_INNER_L4_MASK             0x0f000000
672
673 /**
674  * Check if the (outer) L3 header is IPv4. To avoid comparing IPv4 types one by
675  * one, bit 4 is selected to be used for IPv4 only. Then checking bit 4 can
676  * determin if it is an IPV4 packet.
677  */
678 #define  RTE_ETH_IS_IPV4_HDR(ptype) ((ptype) & RTE_PTYPE_L3_IPV4)
679
680 /**
681  * Check if the (outer) L3 header is IPv4. To avoid comparing IPv4 types one by
682  * one, bit 6 is selected to be used for IPv4 only. Then checking bit 6 can
683  * determin if it is an IPV4 packet.
684  */
685 #define  RTE_ETH_IS_IPV6_HDR(ptype) ((ptype) & RTE_PTYPE_L3_IPV6)
686
687 /* Check if it is a tunneling packet */
688 #define RTE_ETH_IS_TUNNEL_PKT(ptype) ((ptype) & RTE_PTYPE_TUNNEL_MASK)
689 #endif /* RTE_NEXT_ABI */
690
691 /**
692  * Get the name of a RX offload flag
693  *
694  * @param mask
695  *   The mask describing the flag.
696  * @return
697  *   The name of this flag, or NULL if it's not a valid RX flag.
698  */
699 const char *rte_get_rx_ol_flag_name(uint64_t mask);
700
701 /**
702  * Get the name of a TX offload flag
703  *
704  * @param mask
705  *   The mask describing the flag. Usually only one bit must be set.
706  *   Several bits can be given if they belong to the same mask.
707  *   Ex: PKT_TX_L4_MASK.
708  * @return
709  *   The name of this flag, or NULL if it's not a valid TX flag.
710  */
711 const char *rte_get_tx_ol_flag_name(uint64_t mask);
712
713 /**
714  * Some NICs need at least 2KB buffer to RX standard Ethernet frame without
715  * splitting it into multiple segments.
716  * So, for mbufs that planned to be involved into RX/TX, the recommended
717  * minimal buffer length is 2KB + RTE_PKTMBUF_HEADROOM.
718  */
719 #define RTE_MBUF_DEFAULT_DATAROOM       2048
720 #define RTE_MBUF_DEFAULT_BUF_SIZE       \
721         (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
722
723 /* define a set of marker types that can be used to refer to set points in the
724  * mbuf */
725 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
726 typedef uint8_t  MARKER8[0];  /**< generic marker with 1B alignment */
727 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
728                                * with a single assignment */
729
730 /**
731  * The generic rte_mbuf, containing a packet mbuf.
732  */
733 struct rte_mbuf {
734         MARKER cacheline0;
735
736         void *buf_addr;           /**< Virtual address of segment buffer. */
737         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
738
739         uint16_t buf_len;         /**< Length of segment buffer. */
740
741         /* next 6 bytes are initialised on RX descriptor rearm */
742         MARKER8 rearm_data;
743         uint16_t data_off;
744
745         /**
746          * 16-bit Reference counter.
747          * It should only be accessed using the following functions:
748          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
749          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
750          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
751          * config option.
752          */
753         union {
754                 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
755                 uint16_t refcnt;              /**< Non-atomically accessed refcnt */
756         };
757         uint8_t nb_segs;          /**< Number of segments. */
758         uint8_t port;             /**< Input port. */
759
760         uint64_t ol_flags;        /**< Offload features. */
761
762         /* remaining bytes are set on RX when pulling packet from descriptor */
763         MARKER rx_descriptor_fields1;
764
765 #ifdef RTE_NEXT_ABI
766         /*
767          * The packet type, which is the combination of outer/inner L2, L3, L4
768          * and tunnel types.
769          */
770         union {
771                 uint32_t packet_type; /**< L2/L3/L4 and tunnel information. */
772                 struct {
773                         uint32_t l2_type:4; /**< (Outer) L2 type. */
774                         uint32_t l3_type:4; /**< (Outer) L3 type. */
775                         uint32_t l4_type:4; /**< (Outer) L4 type. */
776                         uint32_t tun_type:4; /**< Tunnel type. */
777                         uint32_t inner_l2_type:4; /**< Inner L2 type. */
778                         uint32_t inner_l3_type:4; /**< Inner L3 type. */
779                         uint32_t inner_l4_type:4; /**< Inner L4 type. */
780                 };
781         };
782
783         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
784         uint16_t data_len;        /**< Amount of data in segment buffer. */
785         uint16_t vlan_tci;        /**< VLAN Tag Control Identifier (CPU order) */
786 #else /* RTE_NEXT_ABI */
787         /**
788          * The packet type, which is used to indicate ordinary packet and also
789          * tunneled packet format, i.e. each number is represented a type of
790          * packet.
791          */
792         uint16_t packet_type;
793
794         uint16_t data_len;        /**< Amount of data in segment buffer. */
795         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
796         uint16_t vlan_tci;        /**< VLAN Tag Control Identifier (CPU order) */
797         uint16_t vlan_tci_outer;  /**< Outer VLAN Tag Control Identifier (CPU order) */
798 #endif /* RTE_NEXT_ABI */
799         union {
800                 uint32_t rss;     /**< RSS hash result if RSS enabled */
801                 struct {
802                         union {
803                                 struct {
804                                         uint16_t hash;
805                                         uint16_t id;
806                                 };
807                                 uint32_t lo;
808                                 /**< Second 4 flexible bytes */
809                         };
810                         uint32_t hi;
811                         /**< First 4 flexible bytes or FD ID, dependent on
812                              PKT_RX_FDIR_* flag in ol_flags. */
813                 } fdir;           /**< Filter identifier if FDIR enabled */
814                 uint32_t sched;   /**< Hierarchical scheduler */
815                 uint32_t usr;     /**< User defined tags. See rte_distributor_process() */
816         } hash;                   /**< hash information */
817
818         uint32_t seqn; /**< Sequence number. See also rte_reorder_insert() */
819 #ifdef RTE_NEXT_ABI
820         uint16_t vlan_tci_outer;  /**< Outer VLAN Tag Control Identifier (CPU order) */
821 #endif /* RTE_NEXT_ABI */
822
823         /* second cache line - fields only used in slow path or on TX */
824         MARKER cacheline1 __rte_cache_aligned;
825
826         union {
827                 void *userdata;   /**< Can be used for external metadata */
828                 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
829         };
830
831         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
832         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
833
834         /* fields to support TX offloads */
835         union {
836                 uint64_t tx_offload;       /**< combined for easy fetch */
837                 struct {
838                         uint64_t l2_len:7; /**< L2 (MAC) Header Length. */
839                         uint64_t l3_len:9; /**< L3 (IP) Header Length. */
840                         uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
841                         uint64_t tso_segsz:16; /**< TCP TSO segment size */
842
843                         /* fields for TX offloading of tunnels */
844                         uint64_t outer_l3_len:9; /**< Outer L3 (IP) Hdr Length. */
845                         uint64_t outer_l2_len:7; /**< Outer L2 (MAC) Hdr Length. */
846
847                         /* uint64_t unused:8; */
848                 };
849         };
850
851         /** Size of the application private data. In case of an indirect
852          * mbuf, it stores the direct mbuf private data size. */
853         uint16_t priv_size;
854
855         /** Timesync flags for use with IEEE1588. */
856         uint16_t timesync;
857 } __rte_cache_aligned;
858
859 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
860
861 /**
862  * Return the mbuf owning the data buffer address of an indirect mbuf.
863  *
864  * @param mi
865  *   The pointer to the indirect mbuf.
866  * @return
867  *   The address of the direct mbuf corresponding to buffer_addr.
868  */
869 static inline struct rte_mbuf *
870 rte_mbuf_from_indirect(struct rte_mbuf *mi)
871 {
872         return RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
873 }
874
875 /**
876  * Return the buffer address embedded in the given mbuf.
877  *
878  * @param md
879  *   The pointer to the mbuf.
880  * @return
881  *   The address of the data buffer owned by the mbuf.
882  */
883 static inline char *
884 rte_mbuf_to_baddr(struct rte_mbuf *md)
885 {
886         char *buffer_addr;
887         buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
888         return buffer_addr;
889 }
890
891 /**
892  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
893  */
894 #define RTE_MBUF_INDIRECT(mb)   ((mb)->ol_flags & IND_ATTACHED_MBUF)
895
896 /**
897  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
898  */
899 #define RTE_MBUF_DIRECT(mb)     (!RTE_MBUF_INDIRECT(mb))
900
901 /**
902  * Private data in case of pktmbuf pool.
903  *
904  * A structure that contains some pktmbuf_pool-specific data that are
905  * appended after the mempool structure (in private data).
906  */
907 struct rte_pktmbuf_pool_private {
908         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
909         uint16_t mbuf_priv_size;      /**< Size of private area in each mbuf. */
910 };
911
912 #ifdef RTE_LIBRTE_MBUF_DEBUG
913
914 /**  check mbuf type in debug mode */
915 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
916
917 /**  check mbuf type in debug mode if mbuf pointer is not null */
918 #define __rte_mbuf_sanity_check_raw(m, is_h)    do {       \
919         if ((m) != NULL)                                   \
920                 rte_mbuf_sanity_check(m, is_h);          \
921 } while (0)
922
923 /**  MBUF asserts in debug mode */
924 #define RTE_MBUF_ASSERT(exp)                                         \
925 if (!(exp)) {                                                        \
926         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
927 }
928
929 #else /*  RTE_LIBRTE_MBUF_DEBUG */
930
931 /**  check mbuf type in debug mode */
932 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
933
934 /**  check mbuf type in debug mode if mbuf pointer is not null */
935 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
936
937 /**  MBUF asserts in debug mode */
938 #define RTE_MBUF_ASSERT(exp)                do { } while (0)
939
940 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
941
942 #ifdef RTE_MBUF_REFCNT_ATOMIC
943
944 /**
945  * Reads the value of an mbuf's refcnt.
946  * @param m
947  *   Mbuf to read
948  * @return
949  *   Reference count number.
950  */
951 static inline uint16_t
952 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
953 {
954         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
955 }
956
957 /**
958  * Sets an mbuf's refcnt to a defined value.
959  * @param m
960  *   Mbuf to update
961  * @param new_value
962  *   Value set
963  */
964 static inline void
965 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
966 {
967         rte_atomic16_set(&m->refcnt_atomic, new_value);
968 }
969
970 /**
971  * Adds given value to an mbuf's refcnt and returns its new value.
972  * @param m
973  *   Mbuf to update
974  * @param value
975  *   Value to add/subtract
976  * @return
977  *   Updated value
978  */
979 static inline uint16_t
980 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
981 {
982         /*
983          * The atomic_add is an expensive operation, so we don't want to
984          * call it in the case where we know we are the uniq holder of
985          * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
986          * operation has to be used because concurrent accesses on the
987          * reference counter can occur.
988          */
989         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
990                 rte_mbuf_refcnt_set(m, 1 + value);
991                 return 1 + value;
992         }
993
994         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
995 }
996
997 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
998
999 /**
1000  * Adds given value to an mbuf's refcnt and returns its new value.
1001  */
1002 static inline uint16_t
1003 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
1004 {
1005         m->refcnt = (uint16_t)(m->refcnt + value);
1006         return m->refcnt;
1007 }
1008
1009 /**
1010  * Reads the value of an mbuf's refcnt.
1011  */
1012 static inline uint16_t
1013 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
1014 {
1015         return m->refcnt;
1016 }
1017
1018 /**
1019  * Sets an mbuf's refcnt to the defined value.
1020  */
1021 static inline void
1022 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
1023 {
1024         m->refcnt = new_value;
1025 }
1026
1027 #endif /* RTE_MBUF_REFCNT_ATOMIC */
1028
1029 /** Mbuf prefetch */
1030 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
1031         if ((m) != NULL)                        \
1032                 rte_prefetch0(m);               \
1033 } while (0)
1034
1035
1036 /**
1037  * Sanity checks on an mbuf.
1038  *
1039  * Check the consistency of the given mbuf. The function will cause a
1040  * panic if corruption is detected.
1041  *
1042  * @param m
1043  *   The mbuf to be checked.
1044  * @param is_header
1045  *   True if the mbuf is a packet header, false if it is a sub-segment
1046  *   of a packet (in this case, some fields like nb_segs are not checked)
1047  */
1048 void
1049 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
1050
1051 /**
1052  * @internal Allocate a new mbuf from mempool *mp*.
1053  * The use of that function is reserved for RTE internal needs.
1054  * Please use rte_pktmbuf_alloc().
1055  *
1056  * @param mp
1057  *   The mempool from which mbuf is allocated.
1058  * @return
1059  *   - The pointer to the new mbuf on success.
1060  *   - NULL if allocation failed.
1061  */
1062 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
1063 {
1064         struct rte_mbuf *m;
1065         void *mb = NULL;
1066         if (rte_mempool_get(mp, &mb) < 0)
1067                 return NULL;
1068         m = (struct rte_mbuf *)mb;
1069         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
1070         rte_mbuf_refcnt_set(m, 1);
1071         return m;
1072 }
1073
1074 /**
1075  * @internal Put mbuf back into its original mempool.
1076  * The use of that function is reserved for RTE internal needs.
1077  * Please use rte_pktmbuf_free().
1078  *
1079  * @param m
1080  *   The mbuf to be freed.
1081  */
1082 static inline void __attribute__((always_inline))
1083 __rte_mbuf_raw_free(struct rte_mbuf *m)
1084 {
1085         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
1086         rte_mempool_put(m->pool, m);
1087 }
1088
1089 /* Operations on ctrl mbuf */
1090
1091 /**
1092  * The control mbuf constructor.
1093  *
1094  * This function initializes some fields in an mbuf structure that are
1095  * not modified by the user once created (mbuf type, origin pool, buffer
1096  * start address, and so on). This function is given as a callback function
1097  * to rte_mempool_create() at pool creation time.
1098  *
1099  * @param mp
1100  *   The mempool from which the mbuf is allocated.
1101  * @param opaque_arg
1102  *   A pointer that can be used by the user to retrieve useful information
1103  *   for mbuf initialization. This pointer comes from the ``init_arg``
1104  *   parameter of rte_mempool_create().
1105  * @param m
1106  *   The mbuf to initialize.
1107  * @param i
1108  *   The index of the mbuf in the pool table.
1109  */
1110 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
1111                 void *m, unsigned i);
1112
1113 /**
1114  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
1115  *
1116  * This new mbuf is initialized with data pointing to the beginning of
1117  * buffer, and with a length of zero.
1118  *
1119  * @param mp
1120  *   The mempool from which the mbuf is allocated.
1121  * @return
1122  *   - The pointer to the new mbuf on success.
1123  *   - NULL if allocation failed.
1124  */
1125 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
1126
1127 /**
1128  * Free a control mbuf back into its original mempool.
1129  *
1130  * @param m
1131  *   The control mbuf to be freed.
1132  */
1133 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
1134
1135 /**
1136  * A macro that returns the pointer to the carried data.
1137  *
1138  * The value that can be read or assigned.
1139  *
1140  * @param m
1141  *   The control mbuf.
1142  */
1143 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
1144
1145 /**
1146  * A macro that returns the length of the carried data.
1147  *
1148  * The value that can be read or assigned.
1149  *
1150  * @param m
1151  *   The control mbuf.
1152  */
1153 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
1154
1155 /**
1156  * Tests if an mbuf is a control mbuf
1157  *
1158  * @param m
1159  *   The mbuf to be tested
1160  * @return
1161  *   - True (1) if the mbuf is a control mbuf
1162  *   - False(0) otherwise
1163  */
1164 static inline int
1165 rte_is_ctrlmbuf(struct rte_mbuf *m)
1166 {
1167         return !!(m->ol_flags & CTRL_MBUF_FLAG);
1168 }
1169
1170 /* Operations on pkt mbuf */
1171
1172 /**
1173  * The packet mbuf constructor.
1174  *
1175  * This function initializes some fields in the mbuf structure that are
1176  * not modified by the user once created (origin pool, buffer start
1177  * address, and so on). This function is given as a callback function to
1178  * rte_mempool_create() at pool creation time.
1179  *
1180  * @param mp
1181  *   The mempool from which mbufs originate.
1182  * @param opaque_arg
1183  *   A pointer that can be used by the user to retrieve useful information
1184  *   for mbuf initialization. This pointer comes from the ``init_arg``
1185  *   parameter of rte_mempool_create().
1186  * @param m
1187  *   The mbuf to initialize.
1188  * @param i
1189  *   The index of the mbuf in the pool table.
1190  */
1191 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
1192                       void *m, unsigned i);
1193
1194
1195 /**
1196  * A  packet mbuf pool constructor.
1197  *
1198  * This function initializes the mempool private data in the case of a
1199  * pktmbuf pool. This private data is needed by the driver. The
1200  * function is given as a callback function to rte_mempool_create() at
1201  * pool creation. It can be extended by the user, for example, to
1202  * provide another packet size.
1203  *
1204  * @param mp
1205  *   The mempool from which mbufs originate.
1206  * @param opaque_arg
1207  *   A pointer that can be used by the user to retrieve useful information
1208  *   for mbuf initialization. This pointer comes from the ``init_arg``
1209  *   parameter of rte_mempool_create().
1210  */
1211 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
1212
1213 /**
1214  * Create a mbuf pool.
1215  *
1216  * This function creates and initializes a packet mbuf pool. It is
1217  * a wrapper to rte_mempool_create() with the proper packet constructor
1218  * and mempool constructor.
1219  *
1220  * @param name
1221  *   The name of the mbuf pool.
1222  * @param n
1223  *   The number of elements in the mbuf pool. The optimum size (in terms
1224  *   of memory usage) for a mempool is when n is a power of two minus one:
1225  *   n = (2^q - 1).
1226  * @param cache_size
1227  *   Size of the per-core object cache. See rte_mempool_create() for
1228  *   details.
1229  * @param priv_size
1230  *   Size of application private are between the rte_mbuf structure
1231  *   and the data buffer.
1232  * @param data_room_size
1233  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
1234  * @param socket_id
1235  *   The socket identifier where the memory should be allocated. The
1236  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
1237  *   reserved zone.
1238  * @return
1239  *   The pointer to the new allocated mempool, on success. NULL on error
1240  *   with rte_errno set appropriately. Possible rte_errno values include:
1241  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
1242  *    - E_RTE_SECONDARY - function was called from a secondary process instance
1243  *    - EINVAL - cache size provided is too large
1244  *    - ENOSPC - the maximum number of memzones has already been allocated
1245  *    - EEXIST - a memzone with the same name already exists
1246  *    - ENOMEM - no appropriate memory area found in which to create memzone
1247  */
1248 struct rte_mempool *
1249 rte_pktmbuf_pool_create(const char *name, unsigned n,
1250         unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
1251         int socket_id);
1252
1253 /**
1254  * Get the data room size of mbufs stored in a pktmbuf_pool
1255  *
1256  * The data room size is the amount of data that can be stored in a
1257  * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
1258  *
1259  * @param mp
1260  *   The packet mbuf pool.
1261  * @return
1262  *   The data room size of mbufs stored in this mempool.
1263  */
1264 static inline uint16_t
1265 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
1266 {
1267         struct rte_pktmbuf_pool_private *mbp_priv;
1268
1269         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1270         return mbp_priv->mbuf_data_room_size;
1271 }
1272
1273 /**
1274  * Get the application private size of mbufs stored in a pktmbuf_pool
1275  *
1276  * The private size of mbuf is a zone located between the rte_mbuf
1277  * structure and the data buffer where an application can store data
1278  * associated to a packet.
1279  *
1280  * @param mp
1281  *   The packet mbuf pool.
1282  * @return
1283  *   The private size of mbufs stored in this mempool.
1284  */
1285 static inline uint16_t
1286 rte_pktmbuf_priv_size(struct rte_mempool *mp)
1287 {
1288         struct rte_pktmbuf_pool_private *mbp_priv;
1289
1290         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1291         return mbp_priv->mbuf_priv_size;
1292 }
1293
1294 /**
1295  * Reset the fields of a packet mbuf to their default values.
1296  *
1297  * The given mbuf must have only one segment.
1298  *
1299  * @param m
1300  *   The packet mbuf to be resetted.
1301  */
1302 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
1303 {
1304         m->next = NULL;
1305         m->pkt_len = 0;
1306         m->tx_offload = 0;
1307         m->vlan_tci = 0;
1308         m->vlan_tci_outer = 0;
1309         m->nb_segs = 1;
1310         m->port = 0xff;
1311
1312         m->ol_flags = 0;
1313         m->packet_type = 0;
1314         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
1315                         RTE_PKTMBUF_HEADROOM : m->buf_len;
1316
1317         m->data_len = 0;
1318         __rte_mbuf_sanity_check(m, 1);
1319 }
1320
1321 /**
1322  * Allocate a new mbuf from a mempool.
1323  *
1324  * This new mbuf contains one segment, which has a length of 0. The pointer
1325  * to data is initialized to have some bytes of headroom in the buffer
1326  * (if buffer size allows).
1327  *
1328  * @param mp
1329  *   The mempool from which the mbuf is allocated.
1330  * @return
1331  *   - The pointer to the new mbuf on success.
1332  *   - NULL if allocation failed.
1333  */
1334 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
1335 {
1336         struct rte_mbuf *m;
1337         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
1338                 rte_pktmbuf_reset(m);
1339         return m;
1340 }
1341
1342 /**
1343  * Attach packet mbuf to another packet mbuf.
1344  *
1345  * After attachment we refer the mbuf we attached as 'indirect',
1346  * while mbuf we attached to as 'direct'.
1347  * Right now, not supported:
1348  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
1349  *  - mbuf we trying to attach (mi) is used by someone else
1350  *    e.g. it's reference counter is greater then 1.
1351  *
1352  * @param mi
1353  *   The indirect packet mbuf.
1354  * @param m
1355  *   The packet mbuf we're attaching to.
1356  */
1357 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1358 {
1359         struct rte_mbuf *md;
1360
1361         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(mi) &&
1362             rte_mbuf_refcnt_read(mi) == 1);
1363
1364         /* if m is not direct, get the mbuf that embeds the data */
1365         if (RTE_MBUF_DIRECT(m))
1366                 md = m;
1367         else
1368                 md = rte_mbuf_from_indirect(m);
1369
1370         rte_mbuf_refcnt_update(md, 1);
1371         mi->priv_size = m->priv_size;
1372         mi->buf_physaddr = m->buf_physaddr;
1373         mi->buf_addr = m->buf_addr;
1374         mi->buf_len = m->buf_len;
1375
1376         mi->next = m->next;
1377         mi->data_off = m->data_off;
1378         mi->data_len = m->data_len;
1379         mi->port = m->port;
1380         mi->vlan_tci = m->vlan_tci;
1381         mi->vlan_tci_outer = m->vlan_tci_outer;
1382         mi->tx_offload = m->tx_offload;
1383         mi->hash = m->hash;
1384
1385         mi->next = NULL;
1386         mi->pkt_len = mi->data_len;
1387         mi->nb_segs = 1;
1388         mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1389         mi->packet_type = m->packet_type;
1390
1391         __rte_mbuf_sanity_check(mi, 1);
1392         __rte_mbuf_sanity_check(m, 0);
1393 }
1394
1395 /**
1396  * Detach an indirect packet mbuf.
1397  *
1398  *  - restore original mbuf address and length values.
1399  *  - reset pktmbuf data and data_len to their default values.
1400  *  All other fields of the given packet mbuf will be left intact.
1401  *
1402  * @param m
1403  *   The indirect attached packet mbuf.
1404  */
1405 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1406 {
1407         struct rte_mempool *mp = m->pool;
1408         uint32_t mbuf_size, buf_len, priv_size;
1409
1410         priv_size = rte_pktmbuf_priv_size(mp);
1411         mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1412         buf_len = rte_pktmbuf_data_room_size(mp);
1413
1414         m->priv_size = priv_size;
1415         m->buf_addr = (char *)m + mbuf_size;
1416         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + mbuf_size;
1417         m->buf_len = (uint16_t)buf_len;
1418         m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
1419         m->data_len = 0;
1420         m->ol_flags = 0;
1421 }
1422
1423 static inline struct rte_mbuf* __attribute__((always_inline))
1424 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1425 {
1426         __rte_mbuf_sanity_check(m, 0);
1427
1428         if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
1429
1430                 /* if this is an indirect mbuf, then
1431                  *  - detach mbuf
1432                  *  - free attached mbuf segment
1433                  */
1434                 if (RTE_MBUF_INDIRECT(m)) {
1435                         struct rte_mbuf *md = rte_mbuf_from_indirect(m);
1436                         rte_pktmbuf_detach(m);
1437                         if (rte_mbuf_refcnt_update(md, -1) == 0)
1438                                 __rte_mbuf_raw_free(md);
1439                 }
1440                 return m;
1441         }
1442         return NULL;
1443 }
1444
1445 /**
1446  * Free a segment of a packet mbuf into its original mempool.
1447  *
1448  * Free an mbuf, without parsing other segments in case of chained
1449  * buffers.
1450  *
1451  * @param m
1452  *   The packet mbuf segment to be freed.
1453  */
1454 static inline void __attribute__((always_inline))
1455 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1456 {
1457         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
1458                 m->next = NULL;
1459                 __rte_mbuf_raw_free(m);
1460         }
1461 }
1462
1463 /**
1464  * Free a packet mbuf back into its original mempool.
1465  *
1466  * Free an mbuf, and all its segments in case of chained buffers. Each
1467  * segment is added back into its original mempool.
1468  *
1469  * @param m
1470  *   The packet mbuf to be freed.
1471  */
1472 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1473 {
1474         struct rte_mbuf *m_next;
1475
1476         __rte_mbuf_sanity_check(m, 1);
1477
1478         while (m != NULL) {
1479                 m_next = m->next;
1480                 rte_pktmbuf_free_seg(m);
1481                 m = m_next;
1482         }
1483 }
1484
1485 /**
1486  * Creates a "clone" of the given packet mbuf.
1487  *
1488  * Walks through all segments of the given packet mbuf, and for each of them:
1489  *  - Creates a new packet mbuf from the given pool.
1490  *  - Attaches newly created mbuf to the segment.
1491  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1492  * from the original packet mbuf.
1493  *
1494  * @param md
1495  *   The packet mbuf to be cloned.
1496  * @param mp
1497  *   The mempool from which the "clone" mbufs are allocated.
1498  * @return
1499  *   - The pointer to the new "clone" mbuf on success.
1500  *   - NULL if allocation fails.
1501  */
1502 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
1503                 struct rte_mempool *mp)
1504 {
1505         struct rte_mbuf *mc, *mi, **prev;
1506         uint32_t pktlen;
1507         uint8_t nseg;
1508
1509         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
1510                 return NULL;
1511
1512         mi = mc;
1513         prev = &mi->next;
1514         pktlen = md->pkt_len;
1515         nseg = 0;
1516
1517         do {
1518                 nseg++;
1519                 rte_pktmbuf_attach(mi, md);
1520                 *prev = mi;
1521                 prev = &mi->next;
1522         } while ((md = md->next) != NULL &&
1523             (mi = rte_pktmbuf_alloc(mp)) != NULL);
1524
1525         *prev = NULL;
1526         mc->nb_segs = nseg;
1527         mc->pkt_len = pktlen;
1528
1529         /* Allocation of new indirect segment failed */
1530         if (unlikely (mi == NULL)) {
1531                 rte_pktmbuf_free(mc);
1532                 return NULL;
1533         }
1534
1535         __rte_mbuf_sanity_check(mc, 1);
1536         return mc;
1537 }
1538
1539 /**
1540  * Adds given value to the refcnt of all packet mbuf segments.
1541  *
1542  * Walks through all segments of given packet mbuf and for each of them
1543  * invokes rte_mbuf_refcnt_update().
1544  *
1545  * @param m
1546  *   The packet mbuf whose refcnt to be updated.
1547  * @param v
1548  *   The value to add to the mbuf's segments refcnt.
1549  */
1550 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1551 {
1552         __rte_mbuf_sanity_check(m, 1);
1553
1554         do {
1555                 rte_mbuf_refcnt_update(m, v);
1556         } while ((m = m->next) != NULL);
1557 }
1558
1559 /**
1560  * Get the headroom in a packet mbuf.
1561  *
1562  * @param m
1563  *   The packet mbuf.
1564  * @return
1565  *   The length of the headroom.
1566  */
1567 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1568 {
1569         __rte_mbuf_sanity_check(m, 1);
1570         return m->data_off;
1571 }
1572
1573 /**
1574  * Get the tailroom of a packet mbuf.
1575  *
1576  * @param m
1577  *   The packet mbuf.
1578  * @return
1579  *   The length of the tailroom.
1580  */
1581 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1582 {
1583         __rte_mbuf_sanity_check(m, 1);
1584         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1585                           m->data_len);
1586 }
1587
1588 /**
1589  * Get the last segment of the packet.
1590  *
1591  * @param m
1592  *   The packet mbuf.
1593  * @return
1594  *   The last segment of the given mbuf.
1595  */
1596 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1597 {
1598         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
1599
1600         __rte_mbuf_sanity_check(m, 1);
1601         while (m2->next != NULL)
1602                 m2 = m2->next;
1603         return m2;
1604 }
1605
1606 /**
1607  * A macro that points to an offset into the data in the mbuf.
1608  *
1609  * The returned pointer is cast to type t. Before using this
1610  * function, the user must ensure that the first segment is large
1611  * enough to accommodate its data.
1612  *
1613  * @param m
1614  *   The packet mbuf.
1615  * @param o
1616  *   The offset into the mbuf data.
1617  * @param t
1618  *   The type to cast the result into.
1619  */
1620 #define rte_pktmbuf_mtod_offset(m, t, o)        \
1621         ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
1622
1623 /**
1624  * A macro that points to the start of the data in the mbuf.
1625  *
1626  * The returned pointer is cast to type t. Before using this
1627  * function, the user must ensure that the first segment is large
1628  * enough to accommodate its data.
1629  *
1630  * @param m
1631  *   The packet mbuf.
1632  * @param t
1633  *   The type to cast the result into.
1634  */
1635 #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
1636
1637 /**
1638  * A macro that returns the length of the packet.
1639  *
1640  * The value can be read or assigned.
1641  *
1642  * @param m
1643  *   The packet mbuf.
1644  */
1645 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1646
1647 /**
1648  * A macro that returns the length of the segment.
1649  *
1650  * The value can be read or assigned.
1651  *
1652  * @param m
1653  *   The packet mbuf.
1654  */
1655 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1656
1657 /**
1658  * Prepend len bytes to an mbuf data area.
1659  *
1660  * Returns a pointer to the new
1661  * data start address. If there is not enough headroom in the first
1662  * segment, the function will return NULL, without modifying the mbuf.
1663  *
1664  * @param m
1665  *   The pkt mbuf.
1666  * @param len
1667  *   The amount of data to prepend (in bytes).
1668  * @return
1669  *   A pointer to the start of the newly prepended data, or
1670  *   NULL if there is not enough headroom space in the first segment
1671  */
1672 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1673                                         uint16_t len)
1674 {
1675         __rte_mbuf_sanity_check(m, 1);
1676
1677         if (unlikely(len > rte_pktmbuf_headroom(m)))
1678                 return NULL;
1679
1680         m->data_off -= len;
1681         m->data_len = (uint16_t)(m->data_len + len);
1682         m->pkt_len  = (m->pkt_len + len);
1683
1684         return (char *)m->buf_addr + m->data_off;
1685 }
1686
1687 /**
1688  * Append len bytes to an mbuf.
1689  *
1690  * Append len bytes to an mbuf and return a pointer to the start address
1691  * of the added data. If there is not enough tailroom in the last
1692  * segment, the function will return NULL, without modifying the mbuf.
1693  *
1694  * @param m
1695  *   The packet mbuf.
1696  * @param len
1697  *   The amount of data to append (in bytes).
1698  * @return
1699  *   A pointer to the start of the newly appended data, or
1700  *   NULL if there is not enough tailroom space in the last segment
1701  */
1702 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1703 {
1704         void *tail;
1705         struct rte_mbuf *m_last;
1706
1707         __rte_mbuf_sanity_check(m, 1);
1708
1709         m_last = rte_pktmbuf_lastseg(m);
1710         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1711                 return NULL;
1712
1713         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1714         m_last->data_len = (uint16_t)(m_last->data_len + len);
1715         m->pkt_len  = (m->pkt_len + len);
1716         return (char*) tail;
1717 }
1718
1719 /**
1720  * Remove len bytes at the beginning of an mbuf.
1721  *
1722  * Returns a pointer to the start address of the new data area. If the
1723  * length is greater than the length of the first segment, then the
1724  * function will fail and return NULL, without modifying the mbuf.
1725  *
1726  * @param m
1727  *   The packet mbuf.
1728  * @param len
1729  *   The amount of data to remove (in bytes).
1730  * @return
1731  *   A pointer to the new start of the data.
1732  */
1733 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1734 {
1735         __rte_mbuf_sanity_check(m, 1);
1736
1737         if (unlikely(len > m->data_len))
1738                 return NULL;
1739
1740         m->data_len = (uint16_t)(m->data_len - len);
1741         m->data_off += len;
1742         m->pkt_len  = (m->pkt_len - len);
1743         return (char *)m->buf_addr + m->data_off;
1744 }
1745
1746 /**
1747  * Remove len bytes of data at the end of the mbuf.
1748  *
1749  * If the length is greater than the length of the last segment, the
1750  * function will fail and return -1 without modifying the mbuf.
1751  *
1752  * @param m
1753  *   The packet mbuf.
1754  * @param len
1755  *   The amount of data to remove (in bytes).
1756  * @return
1757  *   - 0: On success.
1758  *   - -1: On error.
1759  */
1760 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1761 {
1762         struct rte_mbuf *m_last;
1763
1764         __rte_mbuf_sanity_check(m, 1);
1765
1766         m_last = rte_pktmbuf_lastseg(m);
1767         if (unlikely(len > m_last->data_len))
1768                 return -1;
1769
1770         m_last->data_len = (uint16_t)(m_last->data_len - len);
1771         m->pkt_len  = (m->pkt_len - len);
1772         return 0;
1773 }
1774
1775 /**
1776  * Test if mbuf data is contiguous.
1777  *
1778  * @param m
1779  *   The packet mbuf.
1780  * @return
1781  *   - 1, if all data is contiguous (one segment).
1782  *   - 0, if there is several segments.
1783  */
1784 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1785 {
1786         __rte_mbuf_sanity_check(m, 1);
1787         return !!(m->nb_segs == 1);
1788 }
1789
1790 /**
1791  * Dump an mbuf structure to the console.
1792  *
1793  * Dump all fields for the given packet mbuf and all its associated
1794  * segments (in the case of a chained buffer).
1795  *
1796  * @param f
1797  *   A pointer to a file for output
1798  * @param m
1799  *   The packet mbuf.
1800  * @param dump_len
1801  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1802  *   the packet.
1803  */
1804 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1805
1806 #ifdef __cplusplus
1807 }
1808 #endif
1809
1810 #endif /* _RTE_MBUF_H_ */