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