mbuf: move packet type definitions in a new file
[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 #include <rte_mbuf_ptype.h>
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 /*
70  * Packet Offload Features Flags. It also carry packet type information.
71  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
72  *
73  * - RX flags start at bit position zero, and get added to the left of previous
74  *   flags.
75  * - The most-significant 3 bits are reserved for generic mbuf flags
76  * - TX flags therefore start at bit position 60 (i.e. 63-3), and new flags get
77  *   added to the right of the previously defined flags i.e. they should count
78  *   downwards, not upwards.
79  *
80  * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
81  * rte_get_tx_ol_flag_name().
82  */
83
84 /**
85  * RX packet is a 802.1q VLAN packet. This flag was set by PMDs when
86  * the packet is recognized as a VLAN, but the behavior between PMDs
87  * was not the same. This flag is kept for some time to avoid breaking
88  * applications and should be replaced by PKT_RX_VLAN_STRIPPED.
89  */
90 #define PKT_RX_VLAN_PKT      (1ULL << 0)
91
92 #define PKT_RX_RSS_HASH      (1ULL << 1)  /**< RX packet with RSS hash result. */
93 #define PKT_RX_FDIR          (1ULL << 2)  /**< RX packet with FDIR match indicate. */
94 #define PKT_RX_L4_CKSUM_BAD  (1ULL << 3)  /**< L4 cksum of RX pkt. is not OK. */
95 #define PKT_RX_IP_CKSUM_BAD  (1ULL << 4)  /**< IP cksum of RX pkt. is not OK. */
96 #define PKT_RX_EIP_CKSUM_BAD (1ULL << 5)  /**< External IP header checksum error. */
97
98 /**
99  * A vlan has been stripped by the hardware and its tci is saved in
100  * mbuf->vlan_tci. This can only happen if vlan stripping is enabled
101  * in the RX configuration of the PMD.
102  */
103 #define PKT_RX_VLAN_STRIPPED (1ULL << 6)
104
105 /* hole, some bits can be reused here  */
106
107 #define PKT_RX_IEEE1588_PTP  (1ULL << 9)  /**< RX IEEE1588 L2 Ethernet PT Packet. */
108 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
109 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
110 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
111
112 /**
113  * The 2 vlans have been stripped by the hardware and their tci are
114  * saved in mbuf->vlan_tci (inner) and mbuf->vlan_tci_outer (outer).
115  * This can only happen if vlan stripping is enabled in the RX
116  * configuration of the PMD. If this flag is set, PKT_RX_VLAN_STRIPPED
117  * must also be set.
118  */
119 #define PKT_RX_QINQ_STRIPPED (1ULL << 15)
120
121 /**
122  * Deprecated.
123  * RX packet with double VLAN stripped.
124  * This flag is replaced by PKT_RX_QINQ_STRIPPED.
125  */
126 #define PKT_RX_QINQ_PKT      PKT_RX_QINQ_STRIPPED
127
128 /* add new RX flags here */
129
130 /* add new TX flags here */
131
132 /**
133  * Bits 45:48 used for the tunnel type.
134  * When doing Tx offload like TSO or checksum, the HW needs to configure the
135  * tunnel type into the HW descriptors.
136  */
137 #define PKT_TX_TUNNEL_VXLAN   (0x1ULL << 45)
138 #define PKT_TX_TUNNEL_GRE     (0x2ULL << 45)
139 #define PKT_TX_TUNNEL_IPIP    (0x3ULL << 45)
140 #define PKT_TX_TUNNEL_GENEVE  (0x4ULL << 45)
141 /* add new TX TUNNEL type here */
142 #define PKT_TX_TUNNEL_MASK    (0xFULL << 45)
143
144 /**
145  * Second VLAN insertion (QinQ) flag.
146  */
147 #define PKT_TX_QINQ_PKT    (1ULL << 49)   /**< TX packet with double VLAN inserted. */
148
149 /**
150  * TCP segmentation offload. To enable this offload feature for a
151  * packet to be transmitted on hardware supporting TSO:
152  *  - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
153  *    PKT_TX_TCP_CKSUM)
154  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
155  *  - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
156  *    to 0 in the packet
157  *  - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
158  *  - calculate the pseudo header checksum without taking ip_len in account,
159  *    and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
160  *    rte_ipv6_phdr_cksum() that can be used as helpers.
161  */
162 #define PKT_TX_TCP_SEG       (1ULL << 50)
163
164 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
165
166 /**
167  * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
168  * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
169  * L4 checksum offload, the user needs to:
170  *  - fill l2_len and l3_len in mbuf
171  *  - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
172  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
173  *  - calculate the pseudo header checksum and set it in the L4 header (only
174  *    for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
175  *    For SCTP, set the crc field to 0.
176  */
177 #define PKT_TX_L4_NO_CKSUM   (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
178 #define PKT_TX_TCP_CKSUM     (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
179 #define PKT_TX_SCTP_CKSUM    (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
180 #define PKT_TX_UDP_CKSUM     (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
181 #define PKT_TX_L4_MASK       (3ULL << 52) /**< Mask for L4 cksum offload request. */
182
183 /**
184  * Offload the IP checksum in the hardware. The flag PKT_TX_IPV4 should
185  * also be set by the application, although a PMD will only check
186  * PKT_TX_IP_CKSUM.
187  *  - set the IP checksum field in the packet to 0
188  *  - fill the mbuf offload information: l2_len, l3_len
189  */
190 #define PKT_TX_IP_CKSUM      (1ULL << 54)
191
192 /**
193  * Packet is IPv4. This flag must be set when using any offload feature
194  * (TSO, L3 or L4 checksum) to tell the NIC that the packet is an IPv4
195  * packet. If the packet is a tunneled packet, this flag is related to
196  * the inner headers.
197  */
198 #define PKT_TX_IPV4          (1ULL << 55)
199
200 /**
201  * Packet is IPv6. This flag must be set when using an offload feature
202  * (TSO or L4 checksum) to tell the NIC that the packet is an IPv6
203  * packet. If the packet is a tunneled packet, this flag is related to
204  * the inner headers.
205  */
206 #define PKT_TX_IPV6          (1ULL << 56)
207
208 #define PKT_TX_VLAN_PKT      (1ULL << 57) /**< TX packet is a 802.1q VLAN packet. */
209
210 /**
211  * Offload the IP checksum of an external header in the hardware. The
212  * flag PKT_TX_OUTER_IPV4 should also be set by the application, alto ugh
213  * a PMD will only check PKT_TX_IP_CKSUM.  The IP checksum field in the
214  * packet must be set to 0.
215  *  - set the outer IP checksum field in the packet to 0
216  *  - fill the mbuf offload information: outer_l2_len, outer_l3_len
217  */
218 #define PKT_TX_OUTER_IP_CKSUM   (1ULL << 58)
219
220 /**
221  * Packet outer header is IPv4. This flag must be set when using any
222  * outer offload feature (L3 or L4 checksum) to tell the NIC that the
223  * outer header of the tunneled packet is an IPv4 packet.
224  */
225 #define PKT_TX_OUTER_IPV4   (1ULL << 59)
226
227 /**
228  * Packet outer header is IPv6. This flag must be set when using any
229  * outer offload feature (L4 checksum) to tell the NIC that the outer
230  * header of the tunneled packet is an IPv6 packet.
231  */
232 #define PKT_TX_OUTER_IPV6    (1ULL << 60)
233
234 #define __RESERVED           (1ULL << 61) /**< reserved for future mbuf use */
235
236 #define IND_ATTACHED_MBUF    (1ULL << 62) /**< Indirect attached mbuf */
237
238 /* Use final bit of flags to indicate a control mbuf */
239 #define CTRL_MBUF_FLAG       (1ULL << 63) /**< Mbuf contains control data */
240
241 /** Alignment constraint of mbuf private area. */
242 #define RTE_MBUF_PRIV_ALIGN 8
243
244 /**
245  * Get the name of a RX offload flag
246  *
247  * @param mask
248  *   The mask describing the flag.
249  * @return
250  *   The name of this flag, or NULL if it's not a valid RX flag.
251  */
252 const char *rte_get_rx_ol_flag_name(uint64_t mask);
253
254 /**
255  * Get the name of a TX offload flag
256  *
257  * @param mask
258  *   The mask describing the flag. Usually only one bit must be set.
259  *   Several bits can be given if they belong to the same mask.
260  *   Ex: PKT_TX_L4_MASK.
261  * @return
262  *   The name of this flag, or NULL if it's not a valid TX flag.
263  */
264 const char *rte_get_tx_ol_flag_name(uint64_t mask);
265
266 /**
267  * Some NICs need at least 2KB buffer to RX standard Ethernet frame without
268  * splitting it into multiple segments.
269  * So, for mbufs that planned to be involved into RX/TX, the recommended
270  * minimal buffer length is 2KB + RTE_PKTMBUF_HEADROOM.
271  */
272 #define RTE_MBUF_DEFAULT_DATAROOM       2048
273 #define RTE_MBUF_DEFAULT_BUF_SIZE       \
274         (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
275
276 /* define a set of marker types that can be used to refer to set points in the
277  * mbuf */
278 __extension__
279 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
280 __extension__
281 typedef uint8_t  MARKER8[0];  /**< generic marker with 1B alignment */
282 __extension__
283 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
284                                * with a single assignment */
285
286 /**
287  * The generic rte_mbuf, containing a packet mbuf.
288  */
289 struct rte_mbuf {
290         MARKER cacheline0;
291
292         void *buf_addr;           /**< Virtual address of segment buffer. */
293         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
294
295         uint16_t buf_len;         /**< Length of segment buffer. */
296
297         /* next 6 bytes are initialised on RX descriptor rearm */
298         MARKER8 rearm_data;
299         uint16_t data_off;
300
301         /**
302          * 16-bit Reference counter.
303          * It should only be accessed using the following functions:
304          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
305          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
306          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
307          * config option.
308          */
309         RTE_STD_C11
310         union {
311                 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
312                 uint16_t refcnt;              /**< Non-atomically accessed refcnt */
313         };
314         uint8_t nb_segs;          /**< Number of segments. */
315         uint8_t port;             /**< Input port. */
316
317         uint64_t ol_flags;        /**< Offload features. */
318
319         /* remaining bytes are set on RX when pulling packet from descriptor */
320         MARKER rx_descriptor_fields1;
321
322         /*
323          * The packet type, which is the combination of outer/inner L2, L3, L4
324          * and tunnel types. The packet_type is about data really present in the
325          * mbuf. Example: if vlan stripping is enabled, a received vlan packet
326          * would have RTE_PTYPE_L2_ETHER and not RTE_PTYPE_L2_VLAN because the
327          * vlan is stripped from the data.
328          */
329         RTE_STD_C11
330         union {
331                 uint32_t packet_type; /**< L2/L3/L4 and tunnel information. */
332                 struct {
333                         uint32_t l2_type:4; /**< (Outer) L2 type. */
334                         uint32_t l3_type:4; /**< (Outer) L3 type. */
335                         uint32_t l4_type:4; /**< (Outer) L4 type. */
336                         uint32_t tun_type:4; /**< Tunnel type. */
337                         uint32_t inner_l2_type:4; /**< Inner L2 type. */
338                         uint32_t inner_l3_type:4; /**< Inner L3 type. */
339                         uint32_t inner_l4_type:4; /**< Inner L4 type. */
340                 };
341         };
342
343         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
344         uint16_t data_len;        /**< Amount of data in segment buffer. */
345         /** VLAN TCI (CPU order), valid if PKT_RX_VLAN_STRIPPED is set. */
346         uint16_t vlan_tci;
347
348         union {
349                 uint32_t rss;     /**< RSS hash result if RSS enabled */
350                 struct {
351                         RTE_STD_C11
352                         union {
353                                 struct {
354                                         uint16_t hash;
355                                         uint16_t id;
356                                 };
357                                 uint32_t lo;
358                                 /**< Second 4 flexible bytes */
359                         };
360                         uint32_t hi;
361                         /**< First 4 flexible bytes or FD ID, dependent on
362                              PKT_RX_FDIR_* flag in ol_flags. */
363                 } fdir;           /**< Filter identifier if FDIR enabled */
364                 struct {
365                         uint32_t lo;
366                         uint32_t hi;
367                 } sched;          /**< Hierarchical scheduler */
368                 uint32_t usr;     /**< User defined tags. See rte_distributor_process() */
369         } hash;                   /**< hash information */
370
371         uint32_t seqn; /**< Sequence number. See also rte_reorder_insert() */
372
373         /** Outer VLAN TCI (CPU order), valid if PKT_RX_QINQ_STRIPPED is set. */
374         uint16_t vlan_tci_outer;
375
376         /* second cache line - fields only used in slow path or on TX */
377         MARKER cacheline1 __rte_cache_min_aligned;
378
379         RTE_STD_C11
380         union {
381                 void *userdata;   /**< Can be used for external metadata */
382                 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
383         };
384
385         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
386         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
387
388         /* fields to support TX offloads */
389         RTE_STD_C11
390         union {
391                 uint64_t tx_offload;       /**< combined for easy fetch */
392                 __extension__
393                 struct {
394                         uint64_t l2_len:7;
395                         /**< L2 (MAC) Header Length for non-tunneling pkt.
396                          * Outer_L4_len + ... + Inner_L2_len for tunneling pkt.
397                          */
398                         uint64_t l3_len:9; /**< L3 (IP) Header Length. */
399                         uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
400                         uint64_t tso_segsz:16; /**< TCP TSO segment size */
401
402                         /* fields for TX offloading of tunnels */
403                         uint64_t outer_l3_len:9; /**< Outer L3 (IP) Hdr Length. */
404                         uint64_t outer_l2_len:7; /**< Outer L2 (MAC) Hdr Length. */
405
406                         /* uint64_t unused:8; */
407                 };
408         };
409
410         /** Size of the application private data. In case of an indirect
411          * mbuf, it stores the direct mbuf private data size. */
412         uint16_t priv_size;
413
414         /** Timesync flags for use with IEEE1588. */
415         uint16_t timesync;
416 } __rte_cache_aligned;
417
418 /**
419  * Prefetch the first part of the mbuf
420  *
421  * The first 64 bytes of the mbuf corresponds to fields that are used early
422  * in the receive path. If the cache line of the architecture is higher than
423  * 64B, the second part will also be prefetched.
424  *
425  * @param m
426  *   The pointer to the mbuf.
427  */
428 static inline void
429 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
430 {
431         rte_prefetch0(&m->cacheline0);
432 }
433
434 /**
435  * Prefetch the second part of the mbuf
436  *
437  * The next 64 bytes of the mbuf corresponds to fields that are used in the
438  * transmit path. If the cache line of the architecture is higher than 64B,
439  * this function does nothing as it is expected that the full mbuf is
440  * already in cache.
441  *
442  * @param m
443  *   The pointer to the mbuf.
444  */
445 static inline void
446 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
447 {
448 #if RTE_CACHE_LINE_SIZE == 64
449         rte_prefetch0(&m->cacheline1);
450 #else
451         RTE_SET_USED(m);
452 #endif
453 }
454
455
456 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
457
458 /**
459  * Return the DMA address of the beginning of the mbuf data
460  *
461  * @param mb
462  *   The pointer to the mbuf.
463  * @return
464  *   The physical address of the beginning of the mbuf data
465  */
466 static inline phys_addr_t
467 rte_mbuf_data_dma_addr(const struct rte_mbuf *mb)
468 {
469         return mb->buf_physaddr + mb->data_off;
470 }
471
472 /**
473  * Return the default DMA address of the beginning of the mbuf data
474  *
475  * This function is used by drivers in their receive function, as it
476  * returns the location where data should be written by the NIC, taking
477  * the default headroom in account.
478  *
479  * @param mb
480  *   The pointer to the mbuf.
481  * @return
482  *   The physical address of the beginning of the mbuf data
483  */
484 static inline phys_addr_t
485 rte_mbuf_data_dma_addr_default(const struct rte_mbuf *mb)
486 {
487         return mb->buf_physaddr + RTE_PKTMBUF_HEADROOM;
488 }
489
490 /**
491  * Return the mbuf owning the data buffer address of an indirect mbuf.
492  *
493  * @param mi
494  *   The pointer to the indirect mbuf.
495  * @return
496  *   The address of the direct mbuf corresponding to buffer_addr.
497  */
498 static inline struct rte_mbuf *
499 rte_mbuf_from_indirect(struct rte_mbuf *mi)
500 {
501         return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
502 }
503
504 /**
505  * Return the buffer address embedded in the given mbuf.
506  *
507  * @param md
508  *   The pointer to the mbuf.
509  * @return
510  *   The address of the data buffer owned by the mbuf.
511  */
512 static inline char *
513 rte_mbuf_to_baddr(struct rte_mbuf *md)
514 {
515         char *buffer_addr;
516         buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
517         return buffer_addr;
518 }
519
520 /**
521  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
522  */
523 #define RTE_MBUF_INDIRECT(mb)   ((mb)->ol_flags & IND_ATTACHED_MBUF)
524
525 /**
526  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
527  */
528 #define RTE_MBUF_DIRECT(mb)     (!RTE_MBUF_INDIRECT(mb))
529
530 /**
531  * Private data in case of pktmbuf pool.
532  *
533  * A structure that contains some pktmbuf_pool-specific data that are
534  * appended after the mempool structure (in private data).
535  */
536 struct rte_pktmbuf_pool_private {
537         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
538         uint16_t mbuf_priv_size;      /**< Size of private area in each mbuf. */
539 };
540
541 #ifdef RTE_LIBRTE_MBUF_DEBUG
542
543 /**  check mbuf type in debug mode */
544 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
545
546 #else /*  RTE_LIBRTE_MBUF_DEBUG */
547
548 /**  check mbuf type in debug mode */
549 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
550
551 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
552
553 #ifdef RTE_MBUF_REFCNT_ATOMIC
554
555 /**
556  * Reads the value of an mbuf's refcnt.
557  * @param m
558  *   Mbuf to read
559  * @return
560  *   Reference count number.
561  */
562 static inline uint16_t
563 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
564 {
565         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
566 }
567
568 /**
569  * Sets an mbuf's refcnt to a defined value.
570  * @param m
571  *   Mbuf to update
572  * @param new_value
573  *   Value set
574  */
575 static inline void
576 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
577 {
578         rte_atomic16_set(&m->refcnt_atomic, new_value);
579 }
580
581 /**
582  * Adds given value to an mbuf's refcnt and returns its new value.
583  * @param m
584  *   Mbuf to update
585  * @param value
586  *   Value to add/subtract
587  * @return
588  *   Updated value
589  */
590 static inline uint16_t
591 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
592 {
593         /*
594          * The atomic_add is an expensive operation, so we don't want to
595          * call it in the case where we know we are the uniq holder of
596          * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
597          * operation has to be used because concurrent accesses on the
598          * reference counter can occur.
599          */
600         if (likely(rte_mbuf_refcnt_read(m) == 1)) {
601                 rte_mbuf_refcnt_set(m, 1 + value);
602                 return 1 + value;
603         }
604
605         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
606 }
607
608 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
609
610 /**
611  * Adds given value to an mbuf's refcnt and returns its new value.
612  */
613 static inline uint16_t
614 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
615 {
616         m->refcnt = (uint16_t)(m->refcnt + value);
617         return m->refcnt;
618 }
619
620 /**
621  * Reads the value of an mbuf's refcnt.
622  */
623 static inline uint16_t
624 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
625 {
626         return m->refcnt;
627 }
628
629 /**
630  * Sets an mbuf's refcnt to the defined value.
631  */
632 static inline void
633 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
634 {
635         m->refcnt = new_value;
636 }
637
638 #endif /* RTE_MBUF_REFCNT_ATOMIC */
639
640 /** Mbuf prefetch */
641 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
642         if ((m) != NULL)                        \
643                 rte_prefetch0(m);               \
644 } while (0)
645
646
647 /**
648  * Sanity checks on an mbuf.
649  *
650  * Check the consistency of the given mbuf. The function will cause a
651  * panic if corruption is detected.
652  *
653  * @param m
654  *   The mbuf to be checked.
655  * @param is_header
656  *   True if the mbuf is a packet header, false if it is a sub-segment
657  *   of a packet (in this case, some fields like nb_segs are not checked)
658  */
659 void
660 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
661
662 /**
663  * Allocate an unitialized mbuf from mempool *mp*.
664  *
665  * This function can be used by PMDs (especially in RX functions) to
666  * allocate an unitialized mbuf. The driver is responsible of
667  * initializing all the required fields. See rte_pktmbuf_reset().
668  * For standard needs, prefer rte_pktmbuf_alloc().
669  *
670  * @param mp
671  *   The mempool from which mbuf is allocated.
672  * @return
673  *   - The pointer to the new mbuf on success.
674  *   - NULL if allocation failed.
675  */
676 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
677 {
678         struct rte_mbuf *m;
679         void *mb = NULL;
680
681         if (rte_mempool_get(mp, &mb) < 0)
682                 return NULL;
683         m = (struct rte_mbuf *)mb;
684         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 0);
685         rte_mbuf_refcnt_set(m, 1);
686         __rte_mbuf_sanity_check(m, 0);
687
688         return m;
689 }
690
691 /**
692  * @internal Put mbuf back into its original mempool.
693  * The use of that function is reserved for RTE internal needs.
694  * Please use rte_pktmbuf_free().
695  *
696  * @param m
697  *   The mbuf to be freed.
698  */
699 static inline void __attribute__((always_inline))
700 __rte_mbuf_raw_free(struct rte_mbuf *m)
701 {
702         RTE_ASSERT(rte_mbuf_refcnt_read(m) == 0);
703         rte_mempool_put(m->pool, m);
704 }
705
706 /* Operations on ctrl mbuf */
707
708 /**
709  * The control mbuf constructor.
710  *
711  * This function initializes some fields in an mbuf structure that are
712  * not modified by the user once created (mbuf type, origin pool, buffer
713  * start address, and so on). This function is given as a callback function
714  * to rte_mempool_create() at pool creation time.
715  *
716  * @param mp
717  *   The mempool from which the mbuf is allocated.
718  * @param opaque_arg
719  *   A pointer that can be used by the user to retrieve useful information
720  *   for mbuf initialization. This pointer comes from the ``init_arg``
721  *   parameter of rte_mempool_create().
722  * @param m
723  *   The mbuf to initialize.
724  * @param i
725  *   The index of the mbuf in the pool table.
726  */
727 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
728                 void *m, unsigned i);
729
730 /**
731  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
732  *
733  * This new mbuf is initialized with data pointing to the beginning of
734  * buffer, and with a length of zero.
735  *
736  * @param mp
737  *   The mempool from which the mbuf is allocated.
738  * @return
739  *   - The pointer to the new mbuf on success.
740  *   - NULL if allocation failed.
741  */
742 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
743
744 /**
745  * Free a control mbuf back into its original mempool.
746  *
747  * @param m
748  *   The control mbuf to be freed.
749  */
750 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
751
752 /**
753  * A macro that returns the pointer to the carried data.
754  *
755  * The value that can be read or assigned.
756  *
757  * @param m
758  *   The control mbuf.
759  */
760 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
761
762 /**
763  * A macro that returns the length of the carried data.
764  *
765  * The value that can be read or assigned.
766  *
767  * @param m
768  *   The control mbuf.
769  */
770 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
771
772 /**
773  * Tests if an mbuf is a control mbuf
774  *
775  * @param m
776  *   The mbuf to be tested
777  * @return
778  *   - True (1) if the mbuf is a control mbuf
779  *   - False(0) otherwise
780  */
781 static inline int
782 rte_is_ctrlmbuf(struct rte_mbuf *m)
783 {
784         return !!(m->ol_flags & CTRL_MBUF_FLAG);
785 }
786
787 /* Operations on pkt mbuf */
788
789 /**
790  * The packet mbuf constructor.
791  *
792  * This function initializes some fields in the mbuf structure that are
793  * not modified by the user once created (origin pool, buffer start
794  * address, and so on). This function is given as a callback function to
795  * rte_mempool_create() at pool creation time.
796  *
797  * @param mp
798  *   The mempool from which mbufs originate.
799  * @param opaque_arg
800  *   A pointer that can be used by the user to retrieve useful information
801  *   for mbuf initialization. This pointer comes from the ``init_arg``
802  *   parameter of rte_mempool_create().
803  * @param m
804  *   The mbuf to initialize.
805  * @param i
806  *   The index of the mbuf in the pool table.
807  */
808 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
809                       void *m, unsigned i);
810
811
812 /**
813  * A  packet mbuf pool constructor.
814  *
815  * This function initializes the mempool private data in the case of a
816  * pktmbuf pool. This private data is needed by the driver. The
817  * function is given as a callback function to rte_mempool_create() at
818  * pool creation. It can be extended by the user, for example, to
819  * provide another packet size.
820  *
821  * @param mp
822  *   The mempool from which mbufs originate.
823  * @param opaque_arg
824  *   A pointer that can be used by the user to retrieve useful information
825  *   for mbuf initialization. This pointer comes from the ``init_arg``
826  *   parameter of rte_mempool_create().
827  */
828 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
829
830 /**
831  * Create a mbuf pool.
832  *
833  * This function creates and initializes a packet mbuf pool. It is
834  * a wrapper to rte_mempool_create() with the proper packet constructor
835  * and mempool constructor.
836  *
837  * @param name
838  *   The name of the mbuf pool.
839  * @param n
840  *   The number of elements in the mbuf pool. The optimum size (in terms
841  *   of memory usage) for a mempool is when n is a power of two minus one:
842  *   n = (2^q - 1).
843  * @param cache_size
844  *   Size of the per-core object cache. See rte_mempool_create() for
845  *   details.
846  * @param priv_size
847  *   Size of application private are between the rte_mbuf structure
848  *   and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
849  * @param data_room_size
850  *   Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
851  * @param socket_id
852  *   The socket identifier where the memory should be allocated. The
853  *   value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
854  *   reserved zone.
855  * @return
856  *   The pointer to the new allocated mempool, on success. NULL on error
857  *   with rte_errno set appropriately. Possible rte_errno values include:
858  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
859  *    - E_RTE_SECONDARY - function was called from a secondary process instance
860  *    - EINVAL - cache size provided is too large, or priv_size is not aligned.
861  *    - ENOSPC - the maximum number of memzones has already been allocated
862  *    - EEXIST - a memzone with the same name already exists
863  *    - ENOMEM - no appropriate memory area found in which to create memzone
864  */
865 struct rte_mempool *
866 rte_pktmbuf_pool_create(const char *name, unsigned n,
867         unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
868         int socket_id);
869
870 /**
871  * Get the data room size of mbufs stored in a pktmbuf_pool
872  *
873  * The data room size is the amount of data that can be stored in a
874  * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
875  *
876  * @param mp
877  *   The packet mbuf pool.
878  * @return
879  *   The data room size of mbufs stored in this mempool.
880  */
881 static inline uint16_t
882 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
883 {
884         struct rte_pktmbuf_pool_private *mbp_priv;
885
886         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
887         return mbp_priv->mbuf_data_room_size;
888 }
889
890 /**
891  * Get the application private size of mbufs stored in a pktmbuf_pool
892  *
893  * The private size of mbuf is a zone located between the rte_mbuf
894  * structure and the data buffer where an application can store data
895  * associated to a packet.
896  *
897  * @param mp
898  *   The packet mbuf pool.
899  * @return
900  *   The private size of mbufs stored in this mempool.
901  */
902 static inline uint16_t
903 rte_pktmbuf_priv_size(struct rte_mempool *mp)
904 {
905         struct rte_pktmbuf_pool_private *mbp_priv;
906
907         mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
908         return mbp_priv->mbuf_priv_size;
909 }
910
911 /**
912  * Reset the data_off field of a packet mbuf to its default value.
913  *
914  * The given mbuf must have only one segment, which should be empty.
915  *
916  * @param m
917  *   The packet mbuf's data_off field has to be reset.
918  */
919 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
920 {
921         m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
922 }
923
924 /**
925  * Reset the fields of a packet mbuf to their default values.
926  *
927  * The given mbuf must have only one segment.
928  *
929  * @param m
930  *   The packet mbuf to be resetted.
931  */
932 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
933 {
934         m->next = NULL;
935         m->pkt_len = 0;
936         m->tx_offload = 0;
937         m->vlan_tci = 0;
938         m->vlan_tci_outer = 0;
939         m->nb_segs = 1;
940         m->port = 0xff;
941
942         m->ol_flags = 0;
943         m->packet_type = 0;
944         rte_pktmbuf_reset_headroom(m);
945
946         m->data_len = 0;
947         __rte_mbuf_sanity_check(m, 1);
948 }
949
950 /**
951  * Allocate a new mbuf from a mempool.
952  *
953  * This new mbuf contains one segment, which has a length of 0. The pointer
954  * to data is initialized to have some bytes of headroom in the buffer
955  * (if buffer size allows).
956  *
957  * @param mp
958  *   The mempool from which the mbuf is allocated.
959  * @return
960  *   - The pointer to the new mbuf on success.
961  *   - NULL if allocation failed.
962  */
963 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
964 {
965         struct rte_mbuf *m;
966         if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
967                 rte_pktmbuf_reset(m);
968         return m;
969 }
970
971 /**
972  * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
973  * values.
974  *
975  *  @param pool
976  *    The mempool from which mbufs are allocated.
977  *  @param mbufs
978  *    Array of pointers to mbufs
979  *  @param count
980  *    Array size
981  *  @return
982  *   - 0: Success
983  */
984 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
985          struct rte_mbuf **mbufs, unsigned count)
986 {
987         unsigned idx = 0;
988         int rc;
989
990         rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
991         if (unlikely(rc))
992                 return rc;
993
994         /* To understand duff's device on loop unwinding optimization, see
995          * https://en.wikipedia.org/wiki/Duff's_device.
996          * Here while() loop is used rather than do() while{} to avoid extra
997          * check if count is zero.
998          */
999         switch (count % 4) {
1000         case 0:
1001                 while (idx != count) {
1002                         RTE_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0);
1003                         rte_mbuf_refcnt_set(mbufs[idx], 1);
1004                         rte_pktmbuf_reset(mbufs[idx]);
1005                         idx++;
1006         case 3:
1007                         RTE_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0);
1008                         rte_mbuf_refcnt_set(mbufs[idx], 1);
1009                         rte_pktmbuf_reset(mbufs[idx]);
1010                         idx++;
1011         case 2:
1012                         RTE_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0);
1013                         rte_mbuf_refcnt_set(mbufs[idx], 1);
1014                         rte_pktmbuf_reset(mbufs[idx]);
1015                         idx++;
1016         case 1:
1017                         RTE_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0);
1018                         rte_mbuf_refcnt_set(mbufs[idx], 1);
1019                         rte_pktmbuf_reset(mbufs[idx]);
1020                         idx++;
1021                 }
1022         }
1023         return 0;
1024 }
1025
1026 /**
1027  * Attach packet mbuf to another packet mbuf.
1028  *
1029  * After attachment we refer the mbuf we attached as 'indirect',
1030  * while mbuf we attached to as 'direct'.
1031  * The direct mbuf's reference counter is incremented.
1032  *
1033  * Right now, not supported:
1034  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
1035  *  - mbuf we trying to attach (mi) is used by someone else
1036  *    e.g. it's reference counter is greater then 1.
1037  *
1038  * @param mi
1039  *   The indirect packet mbuf.
1040  * @param m
1041  *   The packet mbuf we're attaching to.
1042  */
1043 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1044 {
1045         struct rte_mbuf *md;
1046
1047         RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1048             rte_mbuf_refcnt_read(mi) == 1);
1049
1050         /* if m is not direct, get the mbuf that embeds the data */
1051         if (RTE_MBUF_DIRECT(m))
1052                 md = m;
1053         else
1054                 md = rte_mbuf_from_indirect(m);
1055
1056         rte_mbuf_refcnt_update(md, 1);
1057         mi->priv_size = m->priv_size;
1058         mi->buf_physaddr = m->buf_physaddr;
1059         mi->buf_addr = m->buf_addr;
1060         mi->buf_len = m->buf_len;
1061
1062         mi->next = m->next;
1063         mi->data_off = m->data_off;
1064         mi->data_len = m->data_len;
1065         mi->port = m->port;
1066         mi->vlan_tci = m->vlan_tci;
1067         mi->vlan_tci_outer = m->vlan_tci_outer;
1068         mi->tx_offload = m->tx_offload;
1069         mi->hash = m->hash;
1070
1071         mi->next = NULL;
1072         mi->pkt_len = mi->data_len;
1073         mi->nb_segs = 1;
1074         mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1075         mi->packet_type = m->packet_type;
1076
1077         __rte_mbuf_sanity_check(mi, 1);
1078         __rte_mbuf_sanity_check(m, 0);
1079 }
1080
1081 /**
1082  * Detach an indirect packet mbuf.
1083  *
1084  *  - restore original mbuf address and length values.
1085  *  - reset pktmbuf data and data_len to their default values.
1086  *  - decrement the direct mbuf's reference counter. When the
1087  *  reference counter becomes 0, the direct mbuf is freed.
1088  *
1089  * All other fields of the given packet mbuf will be left intact.
1090  *
1091  * @param m
1092  *   The indirect attached packet mbuf.
1093  */
1094 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1095 {
1096         struct rte_mbuf *md = rte_mbuf_from_indirect(m);
1097         struct rte_mempool *mp = m->pool;
1098         uint32_t mbuf_size, buf_len, priv_size;
1099
1100         priv_size = rte_pktmbuf_priv_size(mp);
1101         mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1102         buf_len = rte_pktmbuf_data_room_size(mp);
1103
1104         m->priv_size = priv_size;
1105         m->buf_addr = (char *)m + mbuf_size;
1106         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + mbuf_size;
1107         m->buf_len = (uint16_t)buf_len;
1108         rte_pktmbuf_reset_headroom(m);
1109         m->data_len = 0;
1110         m->ol_flags = 0;
1111
1112         if (rte_mbuf_refcnt_update(md, -1) == 0)
1113                 __rte_mbuf_raw_free(md);
1114 }
1115
1116 static inline struct rte_mbuf* __attribute__((always_inline))
1117 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1118 {
1119         __rte_mbuf_sanity_check(m, 0);
1120
1121         if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
1122                 /* if this is an indirect mbuf, it is detached. */
1123                 if (RTE_MBUF_INDIRECT(m))
1124                         rte_pktmbuf_detach(m);
1125                 return m;
1126         }
1127         return NULL;
1128 }
1129
1130 /**
1131  * Free a segment of a packet mbuf into its original mempool.
1132  *
1133  * Free an mbuf, without parsing other segments in case of chained
1134  * buffers.
1135  *
1136  * @param m
1137  *   The packet mbuf segment to be freed.
1138  */
1139 static inline void __attribute__((always_inline))
1140 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1141 {
1142         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
1143                 m->next = NULL;
1144                 __rte_mbuf_raw_free(m);
1145         }
1146 }
1147
1148 /**
1149  * Free a packet mbuf back into its original mempool.
1150  *
1151  * Free an mbuf, and all its segments in case of chained buffers. Each
1152  * segment is added back into its original mempool.
1153  *
1154  * @param m
1155  *   The packet mbuf to be freed.
1156  */
1157 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1158 {
1159         struct rte_mbuf *m_next;
1160
1161         __rte_mbuf_sanity_check(m, 1);
1162
1163         while (m != NULL) {
1164                 m_next = m->next;
1165                 rte_pktmbuf_free_seg(m);
1166                 m = m_next;
1167         }
1168 }
1169
1170 /**
1171  * Creates a "clone" of the given packet mbuf.
1172  *
1173  * Walks through all segments of the given packet mbuf, and for each of them:
1174  *  - Creates a new packet mbuf from the given pool.
1175  *  - Attaches newly created mbuf to the segment.
1176  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1177  * from the original packet mbuf.
1178  *
1179  * @param md
1180  *   The packet mbuf to be cloned.
1181  * @param mp
1182  *   The mempool from which the "clone" mbufs are allocated.
1183  * @return
1184  *   - The pointer to the new "clone" mbuf on success.
1185  *   - NULL if allocation fails.
1186  */
1187 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
1188                 struct rte_mempool *mp)
1189 {
1190         struct rte_mbuf *mc, *mi, **prev;
1191         uint32_t pktlen;
1192         uint8_t nseg;
1193
1194         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
1195                 return NULL;
1196
1197         mi = mc;
1198         prev = &mi->next;
1199         pktlen = md->pkt_len;
1200         nseg = 0;
1201
1202         do {
1203                 nseg++;
1204                 rte_pktmbuf_attach(mi, md);
1205                 *prev = mi;
1206                 prev = &mi->next;
1207         } while ((md = md->next) != NULL &&
1208             (mi = rte_pktmbuf_alloc(mp)) != NULL);
1209
1210         *prev = NULL;
1211         mc->nb_segs = nseg;
1212         mc->pkt_len = pktlen;
1213
1214         /* Allocation of new indirect segment failed */
1215         if (unlikely (mi == NULL)) {
1216                 rte_pktmbuf_free(mc);
1217                 return NULL;
1218         }
1219
1220         __rte_mbuf_sanity_check(mc, 1);
1221         return mc;
1222 }
1223
1224 /**
1225  * Adds given value to the refcnt of all packet mbuf segments.
1226  *
1227  * Walks through all segments of given packet mbuf and for each of them
1228  * invokes rte_mbuf_refcnt_update().
1229  *
1230  * @param m
1231  *   The packet mbuf whose refcnt to be updated.
1232  * @param v
1233  *   The value to add to the mbuf's segments refcnt.
1234  */
1235 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1236 {
1237         __rte_mbuf_sanity_check(m, 1);
1238
1239         do {
1240                 rte_mbuf_refcnt_update(m, v);
1241         } while ((m = m->next) != NULL);
1242 }
1243
1244 /**
1245  * Get the headroom in a packet mbuf.
1246  *
1247  * @param m
1248  *   The packet mbuf.
1249  * @return
1250  *   The length of the headroom.
1251  */
1252 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1253 {
1254         __rte_mbuf_sanity_check(m, 1);
1255         return m->data_off;
1256 }
1257
1258 /**
1259  * Get the tailroom of a packet mbuf.
1260  *
1261  * @param m
1262  *   The packet mbuf.
1263  * @return
1264  *   The length of the tailroom.
1265  */
1266 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1267 {
1268         __rte_mbuf_sanity_check(m, 1);
1269         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1270                           m->data_len);
1271 }
1272
1273 /**
1274  * Get the last segment of the packet.
1275  *
1276  * @param m
1277  *   The packet mbuf.
1278  * @return
1279  *   The last segment of the given mbuf.
1280  */
1281 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1282 {
1283         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
1284
1285         __rte_mbuf_sanity_check(m, 1);
1286         while (m2->next != NULL)
1287                 m2 = m2->next;
1288         return m2;
1289 }
1290
1291 /**
1292  * A macro that points to an offset into the data in the mbuf.
1293  *
1294  * The returned pointer is cast to type t. Before using this
1295  * function, the user must ensure that the first segment is large
1296  * enough to accommodate its data.
1297  *
1298  * @param m
1299  *   The packet mbuf.
1300  * @param o
1301  *   The offset into the mbuf data.
1302  * @param t
1303  *   The type to cast the result into.
1304  */
1305 #define rte_pktmbuf_mtod_offset(m, t, o)        \
1306         ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
1307
1308 /**
1309  * A macro that points to the start of the data in the mbuf.
1310  *
1311  * The returned pointer is cast to type t. Before using this
1312  * function, the user must ensure that the first segment is large
1313  * enough to accommodate its data.
1314  *
1315  * @param m
1316  *   The packet mbuf.
1317  * @param t
1318  *   The type to cast the result into.
1319  */
1320 #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
1321
1322 /**
1323  * A macro that returns the physical address that points to an offset of the
1324  * start of the data in the mbuf
1325  *
1326  * @param m
1327  *   The packet mbuf.
1328  * @param o
1329  *   The offset into the data to calculate address from.
1330  */
1331 #define rte_pktmbuf_mtophys_offset(m, o) \
1332         (phys_addr_t)((m)->buf_physaddr + (m)->data_off + (o))
1333
1334 /**
1335  * A macro that returns the physical address that points to the start of the
1336  * data in the mbuf
1337  *
1338  * @param m
1339  *   The packet mbuf.
1340  */
1341 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_mtophys_offset(m, 0)
1342
1343 /**
1344  * A macro that returns the length of the packet.
1345  *
1346  * The value can be read or assigned.
1347  *
1348  * @param m
1349  *   The packet mbuf.
1350  */
1351 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1352
1353 /**
1354  * A macro that returns the length of the segment.
1355  *
1356  * The value can be read or assigned.
1357  *
1358  * @param m
1359  *   The packet mbuf.
1360  */
1361 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1362
1363 /**
1364  * Prepend len bytes to an mbuf data area.
1365  *
1366  * Returns a pointer to the new
1367  * data start address. If there is not enough headroom in the first
1368  * segment, the function will return NULL, without modifying the mbuf.
1369  *
1370  * @param m
1371  *   The pkt mbuf.
1372  * @param len
1373  *   The amount of data to prepend (in bytes).
1374  * @return
1375  *   A pointer to the start of the newly prepended data, or
1376  *   NULL if there is not enough headroom space in the first segment
1377  */
1378 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1379                                         uint16_t len)
1380 {
1381         __rte_mbuf_sanity_check(m, 1);
1382
1383         if (unlikely(len > rte_pktmbuf_headroom(m)))
1384                 return NULL;
1385
1386         m->data_off -= len;
1387         m->data_len = (uint16_t)(m->data_len + len);
1388         m->pkt_len  = (m->pkt_len + len);
1389
1390         return (char *)m->buf_addr + m->data_off;
1391 }
1392
1393 /**
1394  * Append len bytes to an mbuf.
1395  *
1396  * Append len bytes to an mbuf and return a pointer to the start address
1397  * of the added data. If there is not enough tailroom in the last
1398  * segment, the function will return NULL, without modifying the mbuf.
1399  *
1400  * @param m
1401  *   The packet mbuf.
1402  * @param len
1403  *   The amount of data to append (in bytes).
1404  * @return
1405  *   A pointer to the start of the newly appended data, or
1406  *   NULL if there is not enough tailroom space in the last segment
1407  */
1408 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1409 {
1410         void *tail;
1411         struct rte_mbuf *m_last;
1412
1413         __rte_mbuf_sanity_check(m, 1);
1414
1415         m_last = rte_pktmbuf_lastseg(m);
1416         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1417                 return NULL;
1418
1419         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1420         m_last->data_len = (uint16_t)(m_last->data_len + len);
1421         m->pkt_len  = (m->pkt_len + len);
1422         return (char*) tail;
1423 }
1424
1425 /**
1426  * Remove len bytes at the beginning of an mbuf.
1427  *
1428  * Returns a pointer to the start address of the new data area. If the
1429  * length is greater than the length of the first segment, then the
1430  * function will fail and return NULL, without modifying the mbuf.
1431  *
1432  * @param m
1433  *   The packet mbuf.
1434  * @param len
1435  *   The amount of data to remove (in bytes).
1436  * @return
1437  *   A pointer to the new start of the data.
1438  */
1439 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1440 {
1441         __rte_mbuf_sanity_check(m, 1);
1442
1443         if (unlikely(len > m->data_len))
1444                 return NULL;
1445
1446         m->data_len = (uint16_t)(m->data_len - len);
1447         m->data_off += len;
1448         m->pkt_len  = (m->pkt_len - len);
1449         return (char *)m->buf_addr + m->data_off;
1450 }
1451
1452 /**
1453  * Remove len bytes of data at the end of the mbuf.
1454  *
1455  * If the length is greater than the length of the last segment, the
1456  * function will fail and return -1 without modifying the mbuf.
1457  *
1458  * @param m
1459  *   The packet mbuf.
1460  * @param len
1461  *   The amount of data to remove (in bytes).
1462  * @return
1463  *   - 0: On success.
1464  *   - -1: On error.
1465  */
1466 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1467 {
1468         struct rte_mbuf *m_last;
1469
1470         __rte_mbuf_sanity_check(m, 1);
1471
1472         m_last = rte_pktmbuf_lastseg(m);
1473         if (unlikely(len > m_last->data_len))
1474                 return -1;
1475
1476         m_last->data_len = (uint16_t)(m_last->data_len - len);
1477         m->pkt_len  = (m->pkt_len - len);
1478         return 0;
1479 }
1480
1481 /**
1482  * Test if mbuf data is contiguous.
1483  *
1484  * @param m
1485  *   The packet mbuf.
1486  * @return
1487  *   - 1, if all data is contiguous (one segment).
1488  *   - 0, if there is several segments.
1489  */
1490 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1491 {
1492         __rte_mbuf_sanity_check(m, 1);
1493         return !!(m->nb_segs == 1);
1494 }
1495
1496 /**
1497  * @internal used by rte_pktmbuf_read().
1498  */
1499 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1500         uint32_t len, void *buf);
1501
1502 /**
1503  * Read len data bytes in a mbuf at specified offset.
1504  *
1505  * If the data is contiguous, return the pointer in the mbuf data, else
1506  * copy the data in the buffer provided by the user and return its
1507  * pointer.
1508  *
1509  * @param m
1510  *   The pointer to the mbuf.
1511  * @param off
1512  *   The offset of the data in the mbuf.
1513  * @param len
1514  *   The amount of bytes to read.
1515  * @param buf
1516  *   The buffer where data is copied if it is not contigous in mbuf
1517  *   data. Its length should be at least equal to the len parameter.
1518  * @return
1519  *   The pointer to the data, either in the mbuf if it is contiguous,
1520  *   or in the user buffer. If mbuf is too small, NULL is returned.
1521  */
1522 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1523         uint32_t off, uint32_t len, void *buf)
1524 {
1525         if (likely(off + len <= rte_pktmbuf_data_len(m)))
1526                 return rte_pktmbuf_mtod_offset(m, char *, off);
1527         else
1528                 return __rte_pktmbuf_read(m, off, len, buf);
1529 }
1530
1531 /**
1532  * Chain an mbuf to another, thereby creating a segmented packet.
1533  *
1534  * Note: The implementation will do a linear walk over the segments to find
1535  * the tail entry. For cases when there are many segments, it's better to
1536  * chain the entries manually.
1537  *
1538  * @param head
1539  *   The head of the mbuf chain (the first packet)
1540  * @param tail
1541  *   The mbuf to put last in the chain
1542  *
1543  * @return
1544  *   - 0, on success.
1545  *   - -EOVERFLOW, if the chain is full (256 entries)
1546  */
1547 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1548 {
1549         struct rte_mbuf *cur_tail;
1550
1551         /* Check for number-of-segments-overflow */
1552         if (head->nb_segs + tail->nb_segs >= 1 << (sizeof(head->nb_segs) * 8))
1553                 return -EOVERFLOW;
1554
1555         /* Chain 'tail' onto the old tail */
1556         cur_tail = rte_pktmbuf_lastseg(head);
1557         cur_tail->next = tail;
1558
1559         /* accumulate number of segments and total length. */
1560         head->nb_segs = (uint8_t)(head->nb_segs + tail->nb_segs);
1561         head->pkt_len += tail->pkt_len;
1562
1563         /* pkt_len is only set in the head */
1564         tail->pkt_len = tail->data_len;
1565
1566         return 0;
1567 }
1568
1569 /**
1570  * Dump an mbuf structure to the console.
1571  *
1572  * Dump all fields for the given packet mbuf and all its associated
1573  * segments (in the case of a chained buffer).
1574  *
1575  * @param f
1576  *   A pointer to a file for output
1577  * @param m
1578  *   The packet mbuf.
1579  * @param dump_len
1580  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1581  *   the packet.
1582  */
1583 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1584
1585 #ifdef __cplusplus
1586 }
1587 #endif
1588
1589 #endif /* _RTE_MBUF_H_ */