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