ixgbe: support TCP segmentation offload
[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_mempool.h>
58 #include <rte_atomic.h>
59 #include <rte_prefetch.h>
60 #include <rte_branch_prediction.h>
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
67 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
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 8 bits are reserved for generic mbuf flags
76  * - TX flags therefore start at bit position 55 (i.e. 63-8), and new flags get
77  *   added to the right of the previously defined flags
78  *
79  * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
80  * rte_get_tx_ol_flag_name().
81  */
82 #define PKT_RX_VLAN_PKT      (1ULL << 0)  /**< RX packet is a 802.1q VLAN packet. */
83 #define PKT_RX_RSS_HASH      (1ULL << 1)  /**< RX packet with RSS hash result. */
84 #define PKT_RX_FDIR          (1ULL << 2)  /**< RX packet with FDIR match indicate. */
85 #define PKT_RX_L4_CKSUM_BAD  (1ULL << 3)  /**< L4 cksum of RX pkt. is not OK. */
86 #define PKT_RX_IP_CKSUM_BAD  (1ULL << 4)  /**< IP cksum of RX pkt. is not OK. */
87 #define PKT_RX_EIP_CKSUM_BAD (0ULL << 0)  /**< External IP header checksum error. */
88 #define PKT_RX_OVERSIZE      (0ULL << 0)  /**< Num of desc of an RX pkt oversize. */
89 #define PKT_RX_HBUF_OVERFLOW (0ULL << 0)  /**< Header buffer overflow. */
90 #define PKT_RX_RECIP_ERR     (0ULL << 0)  /**< Hardware processing error. */
91 #define PKT_RX_MAC_ERR       (0ULL << 0)  /**< MAC error. */
92 #define PKT_RX_IPV4_HDR      (1ULL << 5)  /**< RX packet with IPv4 header. */
93 #define PKT_RX_IPV4_HDR_EXT  (1ULL << 6)  /**< RX packet with extended IPv4 header. */
94 #define PKT_RX_IPV6_HDR      (1ULL << 7)  /**< RX packet with IPv6 header. */
95 #define PKT_RX_IPV6_HDR_EXT  (1ULL << 8)  /**< RX packet with extended IPv6 header. */
96 #define PKT_RX_IEEE1588_PTP  (1ULL << 9)  /**< RX IEEE1588 L2 Ethernet PT Packet. */
97 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
98 #define PKT_RX_TUNNEL_IPV4_HDR (1ULL << 11) /**< RX tunnel packet with IPv4 header.*/
99 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
100 #define PKT_RX_FDIR_ID       (1ULL << 13) /**< FD id reported if FDIR match. */
101 #define PKT_RX_FDIR_FLX      (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
102 /* add new RX flags here */
103
104 /* add new TX flags here */
105 #define PKT_TX_VXLAN_CKSUM   (1ULL << 50) /**< TX checksum of VXLAN computed by NIC */
106 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
107
108 /**
109  * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
110  * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
111  * L4 checksum offload, the user needs to:
112  *  - fill l2_len and l3_len in mbuf
113  *  - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
114  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
115  *  - calculate the pseudo header checksum and set it in the L4 header (only
116  *    for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
117  *    For SCTP, set the crc field to 0.
118  */
119 #define PKT_TX_L4_NO_CKSUM   (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
120 #define PKT_TX_TCP_CKSUM     (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
121 #define PKT_TX_SCTP_CKSUM    (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
122 #define PKT_TX_UDP_CKSUM     (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
123 #define PKT_TX_L4_MASK       (3ULL << 52) /**< Mask for L4 cksum offload request. */
124
125 #define PKT_TX_IP_CKSUM      (1ULL << 54) /**< IP cksum of TX pkt. computed by NIC. */
126 #define PKT_TX_IPV4_CSUM     PKT_TX_IP_CKSUM /**< Alias of PKT_TX_IP_CKSUM. */
127
128 /** Tell the NIC it's an IPv4 packet. Required for L4 checksum offload or TSO. */
129 #define PKT_TX_IPV4          PKT_RX_IPV4_HDR
130
131 /** Tell the NIC it's an IPv6 packet. Required for L4 checksum offload or TSO. */
132 #define PKT_TX_IPV6          PKT_RX_IPV6_HDR
133
134 #define PKT_TX_VLAN_PKT      (1ULL << 55) /**< TX packet is a 802.1q VLAN packet. */
135
136 /**
137  * TCP segmentation offload. To enable this offload feature for a
138  * packet to be transmitted on hardware supporting TSO:
139  *  - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
140  *    PKT_TX_TCP_CKSUM)
141  *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
142  *  - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
143  *    to 0 in the packet
144  *  - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
145  *  - calculate the pseudo header checksum without taking ip_len in accound,
146  *    and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
147  *    rte_ipv6_phdr_cksum() that can be used as helpers.
148  */
149 #define PKT_TX_TCP_SEG       (1ULL << 49)
150
151 /* Use final bit of flags to indicate a control mbuf */
152 #define CTRL_MBUF_FLAG       (1ULL << 63) /**< Mbuf contains control data */
153
154 /**
155  * Get the name of a RX offload flag
156  *
157  * @param mask
158  *   The mask describing the flag.
159  * @return
160  *   The name of this flag, or NULL if it's not a valid RX flag.
161  */
162 const char *rte_get_rx_ol_flag_name(uint64_t mask);
163
164 /**
165  * Get the name of a TX offload flag
166  *
167  * @param mask
168  *   The mask describing the flag. Usually only one bit must be set.
169  *   Several bits can be given if they belong to the same mask.
170  *   Ex: PKT_TX_L4_MASK.
171  * @return
172  *   The name of this flag, or NULL if it's not a valid TX flag.
173  */
174 const char *rte_get_tx_ol_flag_name(uint64_t mask);
175
176 /* define a set of marker types that can be used to refer to set points in the
177  * mbuf */
178 typedef void    *MARKER[0];   /**< generic marker for a point in a structure */
179 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
180                                * with a single assignment */
181 /**
182  * The generic rte_mbuf, containing a packet mbuf.
183  */
184 struct rte_mbuf {
185         MARKER cacheline0;
186
187         void *buf_addr;           /**< Virtual address of segment buffer. */
188         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
189
190         /* next 8 bytes are initialised on RX descriptor rearm */
191         MARKER64 rearm_data;
192         uint16_t buf_len;         /**< Length of segment buffer. */
193         uint16_t data_off;
194
195         /**
196          * 16-bit Reference counter.
197          * It should only be accessed using the following functions:
198          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
199          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
200          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
201          * config option.
202          */
203         union {
204 #ifdef RTE_MBUF_REFCNT
205                 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
206                 uint16_t refcnt;              /**< Non-atomically accessed refcnt */
207 #endif
208                 uint16_t refcnt_reserved;     /**< Do not use this field */
209         };
210         uint8_t nb_segs;          /**< Number of segments. */
211         uint8_t port;             /**< Input port. */
212
213         uint64_t ol_flags;        /**< Offload features. */
214
215         /* remaining bytes are set on RX when pulling packet from descriptor */
216         MARKER rx_descriptor_fields1;
217
218         /**
219          * The packet type, which is used to indicate ordinary packet and also
220          * tunneled packet format, i.e. each number is represented a type of
221          * packet.
222          */
223         uint16_t packet_type;
224
225         uint16_t data_len;        /**< Amount of data in segment buffer. */
226         uint32_t pkt_len;         /**< Total pkt len: sum of all segments. */
227         uint16_t vlan_tci;        /**< VLAN Tag Control Identifier (CPU order) */
228         uint16_t reserved;
229         union {
230                 uint32_t rss;     /**< RSS hash result if RSS enabled */
231                 struct {
232                         union {
233                                 struct {
234                                         uint16_t hash;
235                                         uint16_t id;
236                                 };
237                                 uint32_t lo;
238                                 /**< Second 4 flexible bytes */
239                         };
240                         uint32_t hi;
241                         /**< First 4 flexible bytes or FD ID, dependent on
242                              PKT_RX_FDIR_* flag in ol_flags. */
243                 } fdir;           /**< Filter identifier if FDIR enabled */
244                 uint32_t sched;   /**< Hierarchical scheduler */
245                 uint32_t usr;     /**< User defined tags. See @rte_distributor_process */
246         } hash;                   /**< hash information */
247
248         /* second cache line - fields only used in slow path or on TX */
249         MARKER cacheline1 __rte_cache_aligned;
250
251         union {
252                 void *userdata;   /**< Can be used for external metadata */
253                 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
254         };
255
256         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
257         struct rte_mbuf *next;    /**< Next segment of scattered packet. */
258
259         /* fields to support TX offloads */
260         union {
261                 uint64_t tx_offload;       /**< combined for easy fetch */
262                 struct {
263                         uint64_t l2_len:7; /**< L2 (MAC) Header Length. */
264                         uint64_t l3_len:9; /**< L3 (IP) Header Length. */
265                         uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
266                         uint64_t tso_segsz:16; /**< TCP TSO segment size */
267
268                         /* fields for TX offloading of tunnels */
269                         uint64_t inner_l3_len:9; /**< inner L3 (IP) Hdr Length. */
270                         uint64_t inner_l2_len:7; /**< inner L2 (MAC) Hdr Length. */
271
272                         /* uint64_t unused:8; */
273                 };
274         };
275 } __rte_cache_aligned;
276
277 /**
278  * Given the buf_addr returns the pointer to corresponding mbuf.
279  */
280 #define RTE_MBUF_FROM_BADDR(ba)     (((struct rte_mbuf *)(ba)) - 1)
281
282 /**
283  * Given the pointer to mbuf returns an address where it's  buf_addr
284  * should point to.
285  */
286 #define RTE_MBUF_TO_BADDR(mb)       (((struct rte_mbuf *)(mb)) + 1)
287
288 /**
289  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
290  */
291 #define RTE_MBUF_INDIRECT(mb)   (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
292
293 /**
294  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
295  */
296 #define RTE_MBUF_DIRECT(mb)     (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
297
298
299 /**
300  * Private data in case of pktmbuf pool.
301  *
302  * A structure that contains some pktmbuf_pool-specific data that are
303  * appended after the mempool structure (in private data).
304  */
305 struct rte_pktmbuf_pool_private {
306         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
307 };
308
309 #ifdef RTE_LIBRTE_MBUF_DEBUG
310
311 /**  check mbuf type in debug mode */
312 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
313
314 /**  check mbuf type in debug mode if mbuf pointer is not null */
315 #define __rte_mbuf_sanity_check_raw(m, is_h)    do {       \
316         if ((m) != NULL)                                   \
317                 rte_mbuf_sanity_check(m, is_h);          \
318 } while (0)
319
320 /**  MBUF asserts in debug mode */
321 #define RTE_MBUF_ASSERT(exp)                                         \
322 if (!(exp)) {                                                        \
323         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
324 }
325
326 #else /*  RTE_LIBRTE_MBUF_DEBUG */
327
328 /**  check mbuf type in debug mode */
329 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
330
331 /**  check mbuf type in debug mode if mbuf pointer is not null */
332 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
333
334 /**  MBUF asserts in debug mode */
335 #define RTE_MBUF_ASSERT(exp)                do { } while (0)
336
337 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
338
339 #ifdef RTE_MBUF_REFCNT
340 #ifdef RTE_MBUF_REFCNT_ATOMIC
341
342 /**
343  * Adds given value to an mbuf's refcnt and returns its new value.
344  * @param m
345  *   Mbuf to update
346  * @param value
347  *   Value to add/subtract
348  * @return
349  *   Updated value
350  */
351 static inline uint16_t
352 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
353 {
354         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
355 }
356
357 /**
358  * Reads the value of an mbuf's refcnt.
359  * @param m
360  *   Mbuf to read
361  * @return
362  *   Reference count number.
363  */
364 static inline uint16_t
365 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
366 {
367         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
368 }
369
370 /**
371  * Sets an mbuf's refcnt to a defined value.
372  * @param m
373  *   Mbuf to update
374  * @param new_value
375  *   Value set
376  */
377 static inline void
378 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
379 {
380         rte_atomic16_set(&m->refcnt_atomic, new_value);
381 }
382
383 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
384
385 /**
386  * Adds given value to an mbuf's refcnt and returns its new value.
387  */
388 static inline uint16_t
389 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
390 {
391         m->refcnt = (uint16_t)(m->refcnt + value);
392         return m->refcnt;
393 }
394
395 /**
396  * Reads the value of an mbuf's refcnt.
397  */
398 static inline uint16_t
399 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
400 {
401         return m->refcnt;
402 }
403
404 /**
405  * Sets an mbuf's refcnt to the defined value.
406  */
407 static inline void
408 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
409 {
410         m->refcnt = new_value;
411 }
412
413 #endif /* RTE_MBUF_REFCNT_ATOMIC */
414
415 /** Mbuf prefetch */
416 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
417         if ((m) != NULL)                        \
418                 rte_prefetch0(m);               \
419 } while (0)
420
421 #else /* ! RTE_MBUF_REFCNT */
422
423 /** Mbuf prefetch */
424 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
425
426 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
427
428 #endif /* RTE_MBUF_REFCNT */
429
430
431 /**
432  * Sanity checks on an mbuf.
433  *
434  * Check the consistency of the given mbuf. The function will cause a
435  * panic if corruption is detected.
436  *
437  * @param m
438  *   The mbuf to be checked.
439  * @param is_header
440  *   True if the mbuf is a packet header, false if it is a sub-segment
441  *   of a packet (in this case, some fields like nb_segs are not checked)
442  */
443 void
444 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
445
446 /**
447  * @internal Allocate a new mbuf from mempool *mp*.
448  * The use of that function is reserved for RTE internal needs.
449  * Please use rte_pktmbuf_alloc().
450  *
451  * @param mp
452  *   The mempool from which mbuf is allocated.
453  * @return
454  *   - The pointer to the new mbuf on success.
455  *   - NULL if allocation failed.
456  */
457 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
458 {
459         struct rte_mbuf *m;
460         void *mb = NULL;
461         if (rte_mempool_get(mp, &mb) < 0)
462                 return NULL;
463         m = (struct rte_mbuf *)mb;
464 #ifdef RTE_MBUF_REFCNT
465         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
466         rte_mbuf_refcnt_set(m, 1);
467 #endif /* RTE_MBUF_REFCNT */
468         return (m);
469 }
470
471 /**
472  * @internal Put mbuf back into its original mempool.
473  * The use of that function is reserved for RTE internal needs.
474  * Please use rte_pktmbuf_free().
475  *
476  * @param m
477  *   The mbuf to be freed.
478  */
479 static inline void __attribute__((always_inline))
480 __rte_mbuf_raw_free(struct rte_mbuf *m)
481 {
482 #ifdef RTE_MBUF_REFCNT
483         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
484 #endif /* RTE_MBUF_REFCNT */
485         rte_mempool_put(m->pool, m);
486 }
487
488 /* Operations on ctrl mbuf */
489
490 /**
491  * The control mbuf constructor.
492  *
493  * This function initializes some fields in an mbuf structure that are
494  * not modified by the user once created (mbuf type, origin pool, buffer
495  * start address, and so on). This function is given as a callback function
496  * to rte_mempool_create() at pool creation time.
497  *
498  * @param mp
499  *   The mempool from which the mbuf is allocated.
500  * @param opaque_arg
501  *   A pointer that can be used by the user to retrieve useful information
502  *   for mbuf initialization. This pointer comes from the ``init_arg``
503  *   parameter of rte_mempool_create().
504  * @param m
505  *   The mbuf to initialize.
506  * @param i
507  *   The index of the mbuf in the pool table.
508  */
509 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
510                 void *m, unsigned i);
511
512 /**
513  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
514  *
515  * This new mbuf is initialized with data pointing to the beginning of
516  * buffer, and with a length of zero.
517  *
518  * @param mp
519  *   The mempool from which the mbuf is allocated.
520  * @return
521  *   - The pointer to the new mbuf on success.
522  *   - NULL if allocation failed.
523  */
524 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
525
526 /**
527  * Free a control mbuf back into its original mempool.
528  *
529  * @param m
530  *   The control mbuf to be freed.
531  */
532 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
533
534 /**
535  * A macro that returns the pointer to the carried data.
536  *
537  * The value that can be read or assigned.
538  *
539  * @param m
540  *   The control mbuf.
541  */
542 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
543
544 /**
545  * A macro that returns the length of the carried data.
546  *
547  * The value that can be read or assigned.
548  *
549  * @param m
550  *   The control mbuf.
551  */
552 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
553
554 /**
555  * Tests if an mbuf is a control mbuf
556  *
557  * @param m
558  *   The mbuf to be tested
559  * @return
560  *   - True (1) if the mbuf is a control mbuf
561  *   - False(0) otherwise
562  */
563 static inline int
564 rte_is_ctrlmbuf(struct rte_mbuf *m)
565 {
566         return (!!(m->ol_flags & CTRL_MBUF_FLAG));
567 }
568
569 /* Operations on pkt mbuf */
570
571 /**
572  * The packet mbuf constructor.
573  *
574  * This function initializes some fields in the mbuf structure that are
575  * not modified by the user once created (origin pool, buffer start
576  * address, and so on). This function is given as a callback function to
577  * rte_mempool_create() at pool creation time.
578  *
579  * @param mp
580  *   The mempool from which mbufs originate.
581  * @param opaque_arg
582  *   A pointer that can be used by the user to retrieve useful information
583  *   for mbuf initialization. This pointer comes from the ``init_arg``
584  *   parameter of rte_mempool_create().
585  * @param m
586  *   The mbuf to initialize.
587  * @param i
588  *   The index of the mbuf in the pool table.
589  */
590 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
591                       void *m, unsigned i);
592
593
594 /**
595  * A  packet mbuf pool constructor.
596  *
597  * This function initializes the mempool private data in the case of a
598  * pktmbuf pool. This private data is needed by the driver. The
599  * function is given as a callback function to rte_mempool_create() at
600  * pool creation. It can be extended by the user, for example, to
601  * provide another packet size.
602  *
603  * @param mp
604  *   The mempool from which mbufs originate.
605  * @param opaque_arg
606  *   A pointer that can be used by the user to retrieve useful information
607  *   for mbuf initialization. This pointer comes from the ``init_arg``
608  *   parameter of rte_mempool_create().
609  */
610 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
611
612 /**
613  * Reset the fields of a packet mbuf to their default values.
614  *
615  * The given mbuf must have only one segment.
616  *
617  * @param m
618  *   The packet mbuf to be resetted.
619  */
620 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
621 {
622         m->next = NULL;
623         m->pkt_len = 0;
624         m->tx_offload = 0;
625         m->vlan_tci = 0;
626         m->nb_segs = 1;
627         m->port = 0xff;
628
629         m->ol_flags = 0;
630         m->packet_type = 0;
631         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
632                         RTE_PKTMBUF_HEADROOM : m->buf_len;
633
634         m->data_len = 0;
635         __rte_mbuf_sanity_check(m, 1);
636 }
637
638 /**
639  * Allocate a new mbuf from a mempool.
640  *
641  * This new mbuf contains one segment, which has a length of 0. The pointer
642  * to data is initialized to have some bytes of headroom in the buffer
643  * (if buffer size allows).
644  *
645  * @param mp
646  *   The mempool from which the mbuf is allocated.
647  * @return
648  *   - The pointer to the new mbuf on success.
649  *   - NULL if allocation failed.
650  */
651 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
652 {
653         struct rte_mbuf *m;
654         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
655                 rte_pktmbuf_reset(m);
656         return (m);
657 }
658
659 #ifdef RTE_MBUF_REFCNT
660
661 /**
662  * Attach packet mbuf to another packet mbuf.
663  * After attachment we refer the mbuf we attached as 'indirect',
664  * while mbuf we attached to as 'direct'.
665  * Right now, not supported:
666  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
667  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
668  *  - mbuf we trying to attach (mi) is used by someone else
669  *    e.g. it's reference counter is greater then 1.
670  *
671  * @param mi
672  *   The indirect packet mbuf.
673  * @param md
674  *   The direct packet mbuf.
675  */
676
677 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
678 {
679         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
680             RTE_MBUF_DIRECT(mi) &&
681             rte_mbuf_refcnt_read(mi) == 1);
682
683         rte_mbuf_refcnt_update(md, 1);
684         mi->buf_physaddr = md->buf_physaddr;
685         mi->buf_addr = md->buf_addr;
686         mi->buf_len = md->buf_len;
687
688         mi->next = md->next;
689         mi->data_off = md->data_off;
690         mi->data_len = md->data_len;
691         mi->port = md->port;
692         mi->vlan_tci = md->vlan_tci;
693         mi->tx_offload = md->tx_offload;
694         mi->hash = md->hash;
695
696         mi->next = NULL;
697         mi->pkt_len = mi->data_len;
698         mi->nb_segs = 1;
699         mi->ol_flags = md->ol_flags;
700         mi->packet_type = md->packet_type;
701
702         __rte_mbuf_sanity_check(mi, 1);
703         __rte_mbuf_sanity_check(md, 0);
704 }
705
706 /**
707  * Detach an indirect packet mbuf -
708  *  - restore original mbuf address and length values.
709  *  - reset pktmbuf data and data_len to their default values.
710  *  All other fields of the given packet mbuf will be left intact.
711  *
712  * @param m
713  *   The indirect attached packet mbuf.
714  */
715
716 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
717 {
718         const struct rte_mempool *mp = m->pool;
719         void *buf = RTE_MBUF_TO_BADDR(m);
720         uint32_t buf_len = mp->elt_size - sizeof(*m);
721         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
722
723         m->buf_addr = buf;
724         m->buf_len = (uint16_t)buf_len;
725
726         m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
727                         RTE_PKTMBUF_HEADROOM : m->buf_len;
728
729         m->data_len = 0;
730 }
731
732 #endif /* RTE_MBUF_REFCNT */
733
734
735 static inline struct rte_mbuf* __attribute__((always_inline))
736 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
737 {
738         __rte_mbuf_sanity_check(m, 0);
739
740 #ifdef RTE_MBUF_REFCNT
741         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
742                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
743                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
744
745                 rte_mbuf_refcnt_set(m, 0);
746
747                 /* if this is an indirect mbuf, then
748                  *  - detach mbuf
749                  *  - free attached mbuf segment
750                  */
751                 if (unlikely (md != m)) {
752                         rte_pktmbuf_detach(m);
753                         if (rte_mbuf_refcnt_update(md, -1) == 0)
754                                 __rte_mbuf_raw_free(md);
755                 }
756 #endif
757                 return(m);
758 #ifdef RTE_MBUF_REFCNT
759         }
760         return (NULL);
761 #endif
762 }
763
764 /**
765  * Free a segment of a packet mbuf into its original mempool.
766  *
767  * Free an mbuf, without parsing other segments in case of chained
768  * buffers.
769  *
770  * @param m
771  *   The packet mbuf segment to be freed.
772  */
773 static inline void __attribute__((always_inline))
774 rte_pktmbuf_free_seg(struct rte_mbuf *m)
775 {
776         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
777                 m->next = NULL;
778                 __rte_mbuf_raw_free(m);
779         }
780 }
781
782 /**
783  * Free a packet mbuf back into its original mempool.
784  *
785  * Free an mbuf, and all its segments in case of chained buffers. Each
786  * segment is added back into its original mempool.
787  *
788  * @param m
789  *   The packet mbuf to be freed.
790  */
791 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
792 {
793         struct rte_mbuf *m_next;
794
795         __rte_mbuf_sanity_check(m, 1);
796
797         while (m != NULL) {
798                 m_next = m->next;
799                 rte_pktmbuf_free_seg(m);
800                 m = m_next;
801         }
802 }
803
804 #ifdef RTE_MBUF_REFCNT
805
806 /**
807  * Creates a "clone" of the given packet mbuf.
808  *
809  * Walks through all segments of the given packet mbuf, and for each of them:
810  *  - Creates a new packet mbuf from the given pool.
811  *  - Attaches newly created mbuf to the segment.
812  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
813  * from the original packet mbuf.
814  *
815  * @param md
816  *   The packet mbuf to be cloned.
817  * @param mp
818  *   The mempool from which the "clone" mbufs are allocated.
819  * @return
820  *   - The pointer to the new "clone" mbuf on success.
821  *   - NULL if allocation fails.
822  */
823 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
824                 struct rte_mempool *mp)
825 {
826         struct rte_mbuf *mc, *mi, **prev;
827         uint32_t pktlen;
828         uint8_t nseg;
829
830         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
831                 return (NULL);
832
833         mi = mc;
834         prev = &mi->next;
835         pktlen = md->pkt_len;
836         nseg = 0;
837
838         do {
839                 nseg++;
840                 rte_pktmbuf_attach(mi, md);
841                 *prev = mi;
842                 prev = &mi->next;
843         } while ((md = md->next) != NULL &&
844             (mi = rte_pktmbuf_alloc(mp)) != NULL);
845
846         *prev = NULL;
847         mc->nb_segs = nseg;
848         mc->pkt_len = pktlen;
849
850         /* Allocation of new indirect segment failed */
851         if (unlikely (mi == NULL)) {
852                 rte_pktmbuf_free(mc);
853                 return (NULL);
854         }
855
856         __rte_mbuf_sanity_check(mc, 1);
857         return (mc);
858 }
859
860 /**
861  * Adds given value to the refcnt of all packet mbuf segments.
862  *
863  * Walks through all segments of given packet mbuf and for each of them
864  * invokes rte_mbuf_refcnt_update().
865  *
866  * @param m
867  *   The packet mbuf whose refcnt to be updated.
868  * @param v
869  *   The value to add to the mbuf's segments refcnt.
870  */
871 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
872 {
873         __rte_mbuf_sanity_check(m, 1);
874
875         do {
876                 rte_mbuf_refcnt_update(m, v);
877         } while ((m = m->next) != NULL);
878 }
879
880 #endif /* RTE_MBUF_REFCNT */
881
882 /**
883  * Get the headroom in a packet mbuf.
884  *
885  * @param m
886  *   The packet mbuf.
887  * @return
888  *   The length of the headroom.
889  */
890 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
891 {
892         __rte_mbuf_sanity_check(m, 1);
893         return m->data_off;
894 }
895
896 /**
897  * Get the tailroom of a packet mbuf.
898  *
899  * @param m
900  *   The packet mbuf.
901  * @return
902  *   The length of the tailroom.
903  */
904 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
905 {
906         __rte_mbuf_sanity_check(m, 1);
907         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
908                           m->data_len);
909 }
910
911 /**
912  * Get the last segment of the packet.
913  *
914  * @param m
915  *   The packet mbuf.
916  * @return
917  *   The last segment of the given mbuf.
918  */
919 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
920 {
921         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
922
923         __rte_mbuf_sanity_check(m, 1);
924         while (m2->next != NULL)
925                 m2 = m2->next;
926         return m2;
927 }
928
929 /**
930  * A macro that points to the start of the data in the mbuf.
931  *
932  * The returned pointer is cast to type t. Before using this
933  * function, the user must ensure that m_headlen(m) is large enough to
934  * read its data.
935  *
936  * @param m
937  *   The packet mbuf.
938  * @param t
939  *   The type to cast the result into.
940  */
941 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
942
943 /**
944  * A macro that returns the length of the packet.
945  *
946  * The value can be read or assigned.
947  *
948  * @param m
949  *   The packet mbuf.
950  */
951 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
952
953 /**
954  * A macro that returns the length of the segment.
955  *
956  * The value can be read or assigned.
957  *
958  * @param m
959  *   The packet mbuf.
960  */
961 #define rte_pktmbuf_data_len(m) ((m)->data_len)
962
963 /**
964  * Prepend len bytes to an mbuf data area.
965  *
966  * Returns a pointer to the new
967  * data start address. If there is not enough headroom in the first
968  * segment, the function will return NULL, without modifying the mbuf.
969  *
970  * @param m
971  *   The pkt mbuf.
972  * @param len
973  *   The amount of data to prepend (in bytes).
974  * @return
975  *   A pointer to the start of the newly prepended data, or
976  *   NULL if there is not enough headroom space in the first segment
977  */
978 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
979                                         uint16_t len)
980 {
981         __rte_mbuf_sanity_check(m, 1);
982
983         if (unlikely(len > rte_pktmbuf_headroom(m)))
984                 return NULL;
985
986         m->data_off -= len;
987         m->data_len = (uint16_t)(m->data_len + len);
988         m->pkt_len  = (m->pkt_len + len);
989
990         return (char *)m->buf_addr + m->data_off;
991 }
992
993 /**
994  * Append len bytes to an mbuf.
995  *
996  * Append len bytes to an mbuf and return a pointer to the start address
997  * of the added data. If there is not enough tailroom in the last
998  * segment, the function will return NULL, without modifying the mbuf.
999  *
1000  * @param m
1001  *   The packet mbuf.
1002  * @param len
1003  *   The amount of data to append (in bytes).
1004  * @return
1005  *   A pointer to the start of the newly appended data, or
1006  *   NULL if there is not enough tailroom space in the last segment
1007  */
1008 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1009 {
1010         void *tail;
1011         struct rte_mbuf *m_last;
1012
1013         __rte_mbuf_sanity_check(m, 1);
1014
1015         m_last = rte_pktmbuf_lastseg(m);
1016         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1017                 return NULL;
1018
1019         tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1020         m_last->data_len = (uint16_t)(m_last->data_len + len);
1021         m->pkt_len  = (m->pkt_len + len);
1022         return (char*) tail;
1023 }
1024
1025 /**
1026  * Remove len bytes at the beginning of an mbuf.
1027  *
1028  * Returns a pointer to the start address of the new data area. If the
1029  * length is greater than the length of the first segment, then the
1030  * function will fail and return NULL, without modifying the mbuf.
1031  *
1032  * @param m
1033  *   The packet mbuf.
1034  * @param len
1035  *   The amount of data to remove (in bytes).
1036  * @return
1037  *   A pointer to the new start of the data.
1038  */
1039 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1040 {
1041         __rte_mbuf_sanity_check(m, 1);
1042
1043         if (unlikely(len > m->data_len))
1044                 return NULL;
1045
1046         m->data_len = (uint16_t)(m->data_len - len);
1047         m->data_off += len;
1048         m->pkt_len  = (m->pkt_len - len);
1049         return (char *)m->buf_addr + m->data_off;
1050 }
1051
1052 /**
1053  * Remove len bytes of data at the end of the mbuf.
1054  *
1055  * If the length is greater than the length of the last segment, the
1056  * function will fail and return -1 without modifying the mbuf.
1057  *
1058  * @param m
1059  *   The packet mbuf.
1060  * @param len
1061  *   The amount of data to remove (in bytes).
1062  * @return
1063  *   - 0: On success.
1064  *   - -1: On error.
1065  */
1066 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1067 {
1068         struct rte_mbuf *m_last;
1069
1070         __rte_mbuf_sanity_check(m, 1);
1071
1072         m_last = rte_pktmbuf_lastseg(m);
1073         if (unlikely(len > m_last->data_len))
1074                 return -1;
1075
1076         m_last->data_len = (uint16_t)(m_last->data_len - len);
1077         m->pkt_len  = (m->pkt_len - len);
1078         return 0;
1079 }
1080
1081 /**
1082  * Test if mbuf data is contiguous.
1083  *
1084  * @param m
1085  *   The packet mbuf.
1086  * @return
1087  *   - 1, if all data is contiguous (one segment).
1088  *   - 0, if there is several segments.
1089  */
1090 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1091 {
1092         __rte_mbuf_sanity_check(m, 1);
1093         return !!(m->nb_segs == 1);
1094 }
1095
1096 /**
1097  * Dump an mbuf structure to the console.
1098  *
1099  * Dump all fields for the given packet mbuf and all its associated
1100  * segments (in the case of a chained buffer).
1101  *
1102  * @param f
1103  *   A pointer to a file for output
1104  * @param m
1105  *   The packet mbuf.
1106  * @param dump_len
1107  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1108  *   the packet.
1109  */
1110 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1111
1112 #ifdef __cplusplus
1113 }
1114 #endif
1115
1116 #endif /* _RTE_MBUF_H_ */