table: LPM IPv4
[dpdk.git] / lib / librte_mbuf / rte_mbuf.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_MBUF_H_
35 #define _RTE_MBUF_H_
36
37 /**
38  * @file
39  * RTE Mbuf
40  *
41  * The mbuf library provides the ability to create and destroy buffers
42  * that may be used by the RTE application to store message
43  * buffers. The message buffers are stored in a mempool, using the
44  * RTE mempool library.
45  *
46  * This library provide an API to allocate/free mbufs, manipulate
47  * control message buffer (ctrlmbuf), which are generic message
48  * buffers, and packet buffers (pktmbuf), which are used to carry
49  * network packets.
50  *
51  * To understand the concepts of packet buffers or mbufs, you
52  * should read "TCP/IP Illustrated, Volume 2: The Implementation,
53  * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
54  * http://www.kohala.com/start/tcpipiv2.html
55  *
56  * The main modification of this implementation is the use of mbuf for
57  * transports other than packets. mbufs can have other types.
58  */
59
60 #include <stdint.h>
61 #include <rte_mempool.h>
62 #include <rte_atomic.h>
63 #include <rte_prefetch.h>
64 #include <rte_branch_prediction.h>
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 /**
71  * A control message buffer.
72  */
73 struct rte_ctrlmbuf {
74         void *data;        /**< Pointer to data. */
75         uint32_t data_len; /**< Length of data. */
76 };
77
78
79 /*
80  * Packet Offload Features Flags. It also carry packet type information.
81  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
82  */
83 #define PKT_RX_VLAN_PKT      0x0001 /**< RX packet is a 802.1q VLAN packet. */
84 #define PKT_RX_RSS_HASH      0x0002 /**< RX packet with RSS hash result. */
85 #define PKT_RX_FDIR          0x0004 /**< RX packet with FDIR infos. */
86 #define PKT_RX_L4_CKSUM_BAD  0x0008 /**< L4 cksum of RX pkt. is not OK. */
87 #define PKT_RX_IP_CKSUM_BAD  0x0010 /**< IP cksum of RX pkt. is not OK. */
88 #define PKT_RX_IPV4_HDR      0x0020 /**< RX packet with IPv4 header. */
89 #define PKT_RX_IPV4_HDR_EXT  0x0040 /**< RX packet with extended IPv4 header. */
90 #define PKT_RX_IPV6_HDR      0x0080 /**< RX packet with IPv6 header. */
91 #define PKT_RX_IPV6_HDR_EXT  0x0100 /**< RX packet with extended IPv6 header. */
92 #define PKT_RX_IEEE1588_PTP  0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
93 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
94
95 #define PKT_TX_VLAN_PKT      0x0800 /**< TX packet is a 802.1q VLAN packet. */
96 #define PKT_TX_IP_CKSUM      0x1000 /**< IP cksum of TX pkt. computed by NIC. */
97 /*
98  * Bit 14~13 used for L4 packet type with checksum enabled.
99  *     00: Reserved
100  *     01: TCP checksum
101  *     10: SCTP checksum
102  *     11: UDP checksum
103  */
104 #define PKT_TX_L4_MASK       0x6000 /**< Mask bits for L4 checksum offload request. */
105 #define PKT_TX_L4_NO_CKSUM   0x0000 /**< Disable L4 cksum of TX pkt. */
106 #define PKT_TX_TCP_CKSUM     0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
107 #define PKT_TX_SCTP_CKSUM    0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
108 #define PKT_TX_UDP_CKSUM     0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
109 /* Bit 15 */
110 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
111
112 /**
113  * Bit Mask to indicate what bits required for building TX context
114  */
115 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
116
117 /** Offload features */
118 union rte_vlan_macip {
119         uint32_t data;
120         struct {
121                 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
122                 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
123                 uint16_t vlan_tci;
124                 /**< VLAN Tag Control Identifier (CPU order). */
125         } f;
126 };
127
128 /*
129  * Compare mask for vlan_macip_len.data,
130  * should be in sync with rte_vlan_macip.f layout.
131  * */
132 #define TX_VLAN_CMP_MASK        0xFFFF0000  /**< VLAN length - 16-bits. */
133 #define TX_MAC_LEN_CMP_MASK     0x0000FE00  /**< MAC length - 7-bits. */
134 #define TX_IP_LEN_CMP_MASK      0x000001FF  /**< IP  length - 9-bits. */
135 /**< MAC+IP  length. */
136 #define TX_MACIP_LEN_CMP_MASK   (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
137
138 /**
139  * A packet message buffer.
140  */
141 struct rte_pktmbuf {
142         /* valid for any segment */
143         struct rte_mbuf *next;  /**< Next segment of scattered packet. */
144         void* data;             /**< Start address of data in segment buffer. */
145         uint16_t data_len;      /**< Amount of data in segment buffer. */
146
147         /* these fields are valid for first segment only */
148         uint8_t nb_segs;        /**< Number of segments. */
149         uint8_t in_port;        /**< Input port. */
150         uint32_t pkt_len;       /**< Total pkt len: sum of all segment data_len. */
151
152         /* offload features */
153         union rte_vlan_macip vlan_macip;
154         union {
155                 uint32_t rss;       /**< RSS hash result if RSS enabled */
156                 struct {
157                         uint16_t hash;
158                         uint16_t id;
159                 } fdir;             /**< Filter identifier if FDIR enabled */
160                 uint32_t sched;     /**< Hierarchical scheduler */
161         } hash;                 /**< hash information */
162 };
163
164 /**
165  * This enum indicates the mbuf type.
166  */
167 enum rte_mbuf_type {
168         RTE_MBUF_CTRL,  /**< Control mbuf. */
169         RTE_MBUF_PKT,   /**< Packet mbuf. */
170 };
171
172 /**
173  * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
174  */
175 struct rte_mbuf {
176         struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
177         void *buf_addr;           /**< Virtual address of segment buffer. */
178         phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
179         uint16_t buf_len;         /**< Length of segment buffer. */
180 #ifdef RTE_MBUF_SCATTER_GATHER
181         /**
182          * 16-bit Reference counter.
183          * It should only be accessed using the following functions:
184          * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
185          * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
186          * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
187          * config option.
188          */
189         union {
190                 rte_atomic16_t refcnt_atomic;   /**< Atomically accessed refcnt */
191                 uint16_t refcnt;                /**< Non-atomically accessed refcnt */
192         };
193 #else
194         uint16_t refcnt_reserved;     /**< Do not use this field */
195 #endif
196         uint8_t type;                 /**< Type of mbuf. */
197         uint8_t reserved;             /**< Unused field. Required for padding. */
198         uint16_t ol_flags;            /**< Offload features. */
199
200         union {
201                 struct rte_ctrlmbuf ctrl;
202                 struct rte_pktmbuf pkt;
203         };
204
205         union {
206                 uint8_t metadata[0];
207                 uint16_t metadata16[0];
208                 uint32_t metadata32[0];
209                 uint64_t metadata64[0];
210         };
211 } __rte_cache_aligned;
212
213 #define RTE_MBUF_METADATA_UINT8(mbuf, offset)              \
214         (mbuf->metadata[offset])
215 #define RTE_MBUF_METADATA_UINT16(mbuf, offset)             \
216         (mbuf->metadata16[offset/sizeof(uint16_t)])
217 #define RTE_MBUF_METADATA_UINT32(mbuf, offset)             \
218         (mbuf->metadata32[offset/sizeof(uint32_t)])
219 #define RTE_MBUF_METADATA_UINT64(mbuf, offset)             \
220         (mbuf->metadata64[offset/sizeof(uint64_t)])
221
222 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset)          \
223         (&mbuf->metadata[offset])
224 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset)         \
225         (&mbuf->metadata16[offset/sizeof(uint16_t)])
226 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset)         \
227         (&mbuf->metadata32[offset/sizeof(uint32_t)])
228 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset)         \
229         (&mbuf->metadata64[offset/sizeof(uint64_t)])
230
231 /**
232  * Given the buf_addr returns the pointer to corresponding mbuf.
233  */
234 #define RTE_MBUF_FROM_BADDR(ba)     (((struct rte_mbuf *)(ba)) - 1)
235
236 /**
237  * Given the pointer to mbuf returns an address where it's  buf_addr
238  * should point to.
239  */
240 #define RTE_MBUF_TO_BADDR(mb)       (((struct rte_mbuf *)(mb)) + 1)
241
242 /**
243  * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
244  */
245 #define RTE_MBUF_INDIRECT(mb)   (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
246
247 /**
248  * Returns TRUE if given mbuf is direct, or FALSE otherwise.
249  */
250 #define RTE_MBUF_DIRECT(mb)     (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
251
252
253 /**
254  * Private data in case of pktmbuf pool.
255  *
256  * A structure that contains some pktmbuf_pool-specific data that are
257  * appended after the mempool structure (in private data).
258  */
259 struct rte_pktmbuf_pool_private {
260         uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
261 };
262
263 #ifdef RTE_LIBRTE_MBUF_DEBUG
264
265 /**  check mbuf type in debug mode */
266 #define __rte_mbuf_sanity_check(m, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
267
268 /**  check mbuf type in debug mode if mbuf pointer is not null */
269 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do {       \
270         if ((m) != NULL)                                   \
271                 rte_mbuf_sanity_check(m, t, is_h);          \
272 } while (0)
273
274 /**  MBUF asserts in debug mode */
275 #define RTE_MBUF_ASSERT(exp)                                         \
276 if (!(exp)) {                                                        \
277         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
278 }
279
280 #else /*  RTE_LIBRTE_MBUF_DEBUG */
281
282 /**  check mbuf type in debug mode */
283 #define __rte_mbuf_sanity_check(m, t, is_h) do { } while(0)
284
285 /**  check mbuf type in debug mode if mbuf pointer is not null */
286 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { } while(0)
287
288 /**  MBUF asserts in debug mode */
289 #define RTE_MBUF_ASSERT(exp)                do { } while(0)
290
291 #endif /*  RTE_LIBRTE_MBUF_DEBUG */
292
293 #ifdef RTE_MBUF_SCATTER_GATHER
294 #ifdef RTE_MBUF_REFCNT_ATOMIC
295
296 /**
297  * Adds given value to an mbuf's refcnt and returns its new value.
298  * @param m
299  *   Mbuf to update
300  * @param value
301  *   Value to add/subtract
302  * @return
303  *   Updated value
304  */
305 static inline uint16_t
306 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
307 {
308         return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
309 }
310
311 /**
312  * Reads the value of an mbuf's refcnt.
313  * @param m
314  *   Mbuf to read
315  * @return
316  *   Reference count number.
317  */
318 static inline uint16_t
319 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
320 {
321         return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
322 }
323
324 /**
325  * Sets an mbuf's refcnt to a defined value.
326  * @param m
327  *   Mbuf to update
328  * @param new_value
329  *   Value set
330  */
331 static inline void
332 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
333 {
334         rte_atomic16_set(&m->refcnt_atomic, new_value);
335 }
336
337 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
338
339 /**
340  * Adds given value to an mbuf's refcnt and returns its new value.
341  */
342 static inline uint16_t
343 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
344 {
345         m->refcnt = (uint16_t)(m->refcnt + value);
346         return m->refcnt;
347 }
348
349 /**
350  * Reads the value of an mbuf's refcnt.
351  */
352 static inline uint16_t
353 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
354 {
355         return m->refcnt;
356 }
357
358 /**
359  * Sets an mbuf's refcnt to the defined value.
360  */
361 static inline void
362 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
363 {
364         m->refcnt = new_value;
365 }
366
367 #endif /* RTE_MBUF_REFCNT_ATOMIC */
368
369 /** Mbuf prefetch */
370 #define RTE_MBUF_PREFETCH_TO_FREE(m) do {       \
371         if ((m) != NULL)                        \
372                 rte_prefetch0(m);               \
373 } while (0)
374
375 #else /* ! RTE_MBUF_SCATTER_GATHER */
376
377 /** Mbuf prefetch */
378 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
379
380 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
381
382 #endif /* RTE_MBUF_SCATTER_GATHER */
383
384
385 /**
386  * Sanity checks on an mbuf.
387  *
388  * Check the consistency of the given mbuf. The function will cause a
389  * panic if corruption is detected.
390  *
391  * @param m
392  *   The mbuf to be checked.
393  * @param t
394  *   The expected type of the mbuf.
395  * @param is_header
396  *   True if the mbuf is a packet header, false if it is a sub-segment
397  *   of a packet (in this case, some fields like nb_segs are not checked)
398  */
399 void
400 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
401                       int is_header);
402
403 /**
404  * @internal Allocate a new mbuf from mempool *mp*.
405  * The use of that function is reserved for RTE internal needs.
406  * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
407  *
408  * @param mp
409  *   The mempool from which mbuf is allocated.
410  * @return
411  *   - The pointer to the new mbuf on success.
412  *   - NULL if allocation failed.
413  */
414 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
415 {
416         struct rte_mbuf *m;
417         void *mb = NULL;
418         if (rte_mempool_get(mp, &mb) < 0)
419                 return NULL;
420         m = (struct rte_mbuf *)mb;
421 #ifdef RTE_MBUF_SCATTER_GATHER
422         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
423         rte_mbuf_refcnt_set(m, 1);
424 #endif /* RTE_MBUF_SCATTER_GATHER */
425         return (m);
426 }
427
428 /**
429  * @internal Put mbuf back into its original mempool.
430  * The use of that function is reserved for RTE internal needs.
431  * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
432  *
433  * @param m
434  *   The mbuf to be freed.
435  */
436 static inline void __attribute__((always_inline))
437 __rte_mbuf_raw_free(struct rte_mbuf *m)
438 {
439 #ifdef RTE_MBUF_SCATTER_GATHER
440         RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
441 #endif /* RTE_MBUF_SCATTER_GATHER */
442         rte_mempool_put(m->pool, m);
443 }
444
445 /* Operations on ctrl mbuf */
446
447 /**
448  * The control mbuf constructor.
449  *
450  * This function initializes some fields in an mbuf structure that are
451  * not modified by the user once created (mbuf type, origin pool, buffer
452  * start address, and so on). This function is given as a callback function
453  * to rte_mempool_create() at pool creation time.
454  *
455  * @param mp
456  *   The mempool from which the mbuf is allocated.
457  * @param opaque_arg
458  *   A pointer that can be used by the user to retrieve useful information
459  *   for mbuf initialization. This pointer comes from the ``init_arg``
460  *   parameter of rte_mempool_create().
461  * @param m
462  *   The mbuf to initialize.
463  * @param i
464  *   The index of the mbuf in the pool table.
465  */
466 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
467                        void *m, unsigned i);
468
469 /**
470  * Allocate a new mbuf (type is ctrl) from mempool *mp*.
471  *
472  * This new mbuf is initialized with data pointing to the beginning of
473  * buffer, and with a length of zero.
474  *
475  * @param mp
476  *   The mempool from which the mbuf is allocated.
477  * @return
478  *   - The pointer to the new mbuf on success.
479  *   - NULL if allocation failed.
480  */
481 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
482 {
483         struct rte_mbuf *m;
484         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
485                 m->ctrl.data = m->buf_addr;
486                 m->ctrl.data_len = 0;
487                 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
488         }
489         return (m);
490 }
491
492 /**
493  * Free a control mbuf back into its original mempool.
494  *
495  * @param m
496  *   The control mbuf to be freed.
497  */
498 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
499 {
500         __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
501 #ifdef RTE_MBUF_SCATTER_GATHER
502         if (rte_mbuf_refcnt_update(m, -1) == 0)
503 #endif /* RTE_MBUF_SCATTER_GATHER */
504                 __rte_mbuf_raw_free(m);
505 }
506
507 /**
508  * A macro that returns the pointer to the carried data.
509  *
510  * The value that can be read or assigned.
511  *
512  * @param m
513  *   The control mbuf.
514  */
515 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
516
517 /**
518  * A macro that returns the length of the carried data.
519  *
520  * The value that can be read or assigned.
521  *
522  * @param m
523  *   The control mbuf.
524  */
525 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
526
527 /* Operations on pkt mbuf */
528
529 /**
530  * The packet mbuf constructor.
531  *
532  * This function initializes some fields in the mbuf structure that are not
533  * modified by the user once created (mbuf type, origin pool, buffer start
534  * address, and so on). This function is given as a callback function to
535  * rte_mempool_create() at pool creation time.
536  *
537  * @param mp
538  *   The mempool from which mbufs originate.
539  * @param opaque_arg
540  *   A pointer that can be used by the user to retrieve useful information
541  *   for mbuf initialization. This pointer comes from the ``init_arg``
542  *   parameter of rte_mempool_create().
543  * @param m
544  *   The mbuf to initialize.
545  * @param i
546  *   The index of the mbuf in the pool table.
547  */
548 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
549                       void *m, unsigned i);
550
551
552 /**
553  * A  packet mbuf pool constructor.
554  *
555  * This function initializes the mempool private data in the case of a
556  * pktmbuf pool. This private data is needed by the driver. The
557  * function is given as a callback function to rte_mempool_create() at
558  * pool creation. It can be extended by the user, for example, to
559  * provide another packet size.
560  *
561  * @param mp
562  *   The mempool from which mbufs originate.
563  * @param opaque_arg
564  *   A pointer that can be used by the user to retrieve useful information
565  *   for mbuf initialization. This pointer comes from the ``init_arg``
566  *   parameter of rte_mempool_create().
567  */
568 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
569
570 /**
571  * Reset the fields of a packet mbuf to their default values.
572  *
573  * The given mbuf must have only one segment.
574  *
575  * @param m
576  *   The packet mbuf to be resetted.
577  */
578 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
579 {
580         uint32_t buf_ofs;
581
582         m->pkt.next = NULL;
583         m->pkt.pkt_len = 0;
584         m->pkt.vlan_macip.data = 0;
585         m->pkt.nb_segs = 1;
586         m->pkt.in_port = 0xff;
587
588         m->ol_flags = 0;
589         buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
590                         RTE_PKTMBUF_HEADROOM : m->buf_len;
591         m->pkt.data = (char*) m->buf_addr + buf_ofs;
592
593         m->pkt.data_len = 0;
594         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
595 }
596
597 /**
598  * Allocate a new mbuf (type is pkt) from a mempool.
599  *
600  * This new mbuf contains one segment, which has a length of 0. The pointer
601  * to data is initialized to have some bytes of headroom in the buffer
602  * (if buffer size allows).
603  *
604  * @param mp
605  *   The mempool from which the mbuf is allocated.
606  * @return
607  *   - The pointer to the new mbuf on success.
608  *   - NULL if allocation failed.
609  */
610 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
611 {
612         struct rte_mbuf *m;
613         if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
614                 rte_pktmbuf_reset(m);
615         return (m);
616 }
617
618 #ifdef RTE_MBUF_SCATTER_GATHER
619
620 /**
621  * Attach packet mbuf to another packet mbuf.
622  * After attachment we refer the mbuf we attached as 'indirect',
623  * while mbuf we attached to as 'direct'.
624  * Right now, not supported:
625  *  - attachment to indirect mbuf (e.g. - md  has to be direct).
626  *  - attachment for already indirect mbuf (e.g. - mi has to be direct).
627  *  - mbuf we trying to attach (mi) is used by someone else
628  *    e.g. it's reference counter is greater then 1.
629  *
630  * @param mi
631  *   The indirect packet mbuf.
632  * @param md
633  *   The direct packet mbuf.
634  */
635
636 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
637 {
638         RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
639             RTE_MBUF_DIRECT(mi) &&
640             rte_mbuf_refcnt_read(mi) == 1);
641
642         rte_mbuf_refcnt_update(md, 1);
643         mi->buf_physaddr = md->buf_physaddr;
644         mi->buf_addr = md->buf_addr;
645         mi->buf_len = md->buf_len;
646
647         mi->pkt = md->pkt;
648
649         mi->pkt.next = NULL;
650         mi->pkt.pkt_len = mi->pkt.data_len;
651         mi->pkt.nb_segs = 1;
652         mi->ol_flags = md->ol_flags;
653
654         __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
655         __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
656 }
657
658 /**
659  * Detach an indirect packet mbuf -
660  *  - restore original mbuf address and length values.
661  *  - reset pktmbuf data and data_len to their default values.
662  *  All other fields of the given packet mbuf will be left intact.
663  *
664  * @param m
665  *   The indirect attached packet mbuf.
666  */
667
668 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
669 {
670         const struct rte_mempool *mp = m->pool;
671         void *buf = RTE_MBUF_TO_BADDR(m);
672         uint32_t buf_ofs;
673         uint32_t buf_len = mp->elt_size - sizeof(*m);
674         m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
675
676         m->buf_addr = buf;
677         m->buf_len = (uint16_t)buf_len;
678
679         buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
680                         RTE_PKTMBUF_HEADROOM : m->buf_len;
681         m->pkt.data = (char*) m->buf_addr + buf_ofs;
682
683         m->pkt.data_len = 0;
684 }
685
686 #endif /* RTE_MBUF_SCATTER_GATHER */
687
688
689 static inline struct rte_mbuf* __attribute__((always_inline))
690 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
691 {
692         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
693
694 #ifdef RTE_MBUF_SCATTER_GATHER
695         if (likely (rte_mbuf_refcnt_read(m) == 1) ||
696                         likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
697                 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
698
699                 rte_mbuf_refcnt_set(m, 0);
700
701                 /* if this is an indirect mbuf, then
702                  *  - detach mbuf
703                  *  - free attached mbuf segment
704                  */
705                 if (unlikely (md != m)) {
706                         rte_pktmbuf_detach(m);
707                         if (rte_mbuf_refcnt_update(md, -1) == 0)
708                                 __rte_mbuf_raw_free(md);
709                 }
710 #endif
711                 return(m);
712 #ifdef RTE_MBUF_SCATTER_GATHER
713         }
714         return (NULL);
715 #endif
716 }
717
718 /**
719  * Free a segment of a packet mbuf into its original mempool.
720  *
721  * Free an mbuf, without parsing other segments in case of chained
722  * buffers.
723  *
724  * @param m
725  *   The packet mbuf segment to be freed.
726  */
727 static inline void __attribute__((always_inline))
728 rte_pktmbuf_free_seg(struct rte_mbuf *m)
729 {
730         if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
731                 __rte_mbuf_raw_free(m);
732 }
733
734 /**
735  * Free a packet mbuf back into its original mempool.
736  *
737  * Free an mbuf, and all its segments in case of chained buffers. Each
738  * segment is added back into its original mempool.
739  *
740  * @param m
741  *   The packet mbuf to be freed.
742  */
743 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
744 {
745         struct rte_mbuf *m_next;
746
747         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
748
749         while (m != NULL) {
750                 m_next = m->pkt.next;
751                 rte_pktmbuf_free_seg(m);
752                 m = m_next;
753         }
754 }
755
756 #ifdef RTE_MBUF_SCATTER_GATHER
757
758 /**
759  * Creates a "clone" of the given packet mbuf.
760  *
761  * Walks through all segments of the given packet mbuf, and for each of them:
762  *  - Creates a new packet mbuf from the given pool.
763  *  - Attaches newly created mbuf to the segment.
764  * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
765  * from the original packet mbuf.
766  *
767  * @param md
768  *   The packet mbuf to be cloned.
769  * @param mp
770  *   The mempool from which the "clone" mbufs are allocated.
771  * @return
772  *   - The pointer to the new "clone" mbuf on success.
773  *   - NULL if allocation fails.
774  */
775 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
776                 struct rte_mempool *mp)
777 {
778         struct rte_mbuf *mc, *mi, **prev;
779         uint32_t pktlen;
780         uint8_t nseg;
781
782         if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
783                 return (NULL);
784
785         mi = mc;
786         prev = &mi->pkt.next;
787         pktlen = md->pkt.pkt_len;
788         nseg = 0;
789
790         do {
791                 nseg++;
792                 rte_pktmbuf_attach(mi, md);
793                 *prev = mi;
794                 prev = &mi->pkt.next;
795         } while ((md = md->pkt.next) != NULL &&
796             (mi = rte_pktmbuf_alloc(mp)) != NULL);
797
798         *prev = NULL;
799         mc->pkt.nb_segs = nseg;
800         mc->pkt.pkt_len = pktlen;
801
802         /* Allocation of new indirect segment failed */
803         if (unlikely (mi == NULL)) {
804                 rte_pktmbuf_free(mc);
805                 return (NULL);
806         }
807
808         __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
809         return (mc);
810 }
811
812 /**
813  * Adds given value to the refcnt of all packet mbuf segments.
814  *
815  * Walks through all segments of given packet mbuf and for each of them
816  * invokes rte_mbuf_refcnt_update().
817  *
818  * @param m
819  *   The packet mbuf whose refcnt to be updated.
820  * @param v
821  *   The value to add to the mbuf's segments refcnt.
822  */
823 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
824 {
825         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
826
827         do {
828                 rte_mbuf_refcnt_update(m, v);
829         } while ((m = m->pkt.next) != NULL);
830 }
831
832 #endif /* RTE_MBUF_SCATTER_GATHER */
833
834 /**
835  * Get the headroom in a packet mbuf.
836  *
837  * @param m
838  *   The packet mbuf.
839  * @return
840  *   The length of the headroom.
841  */
842 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
843 {
844         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
845         return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
846 }
847
848 /**
849  * Get the tailroom of a packet mbuf.
850  *
851  * @param m
852  *   The packet mbuf.
853  * @return
854  *   The length of the tailroom.
855  */
856 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
857 {
858         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
859         return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
860                           m->pkt.data_len);
861 }
862
863 /**
864  * Get the last segment of the packet.
865  *
866  * @param m
867  *   The packet mbuf.
868  * @return
869  *   The last segment of the given mbuf.
870  */
871 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
872 {
873         struct rte_mbuf *m2 = (struct rte_mbuf *)m;
874
875         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
876         while (m2->pkt.next != NULL)
877                 m2 = m2->pkt.next;
878         return m2;
879 }
880
881 /**
882  * A macro that points to the start of the data in the mbuf.
883  *
884  * The returned pointer is cast to type t. Before using this
885  * function, the user must ensure that m_headlen(m) is large enough to
886  * read its data.
887  *
888  * @param m
889  *   The packet mbuf.
890  * @param t
891  *   The type to cast the result into.
892  */
893 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
894
895 /**
896  * A macro that returns the length of the packet.
897  *
898  * The value can be read or assigned.
899  *
900  * @param m
901  *   The packet mbuf.
902  */
903 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
904
905 /**
906  * A macro that returns the length of the segment.
907  *
908  * The value can be read or assigned.
909  *
910  * @param m
911  *   The packet mbuf.
912  */
913 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
914
915 /**
916  * Prepend len bytes to an mbuf data area.
917  *
918  * Returns a pointer to the new
919  * data start address. If there is not enough headroom in the first
920  * segment, the function will return NULL, without modifying the mbuf.
921  *
922  * @param m
923  *   The pkt mbuf.
924  * @param len
925  *   The amount of data to prepend (in bytes).
926  * @return
927  *   A pointer to the start of the newly prepended data, or
928  *   NULL if there is not enough headroom space in the first segment
929  */
930 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
931                                         uint16_t len)
932 {
933         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
934
935         if (unlikely(len > rte_pktmbuf_headroom(m)))
936                 return NULL;
937
938         m->pkt.data = (char*) m->pkt.data - len;
939         m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
940         m->pkt.pkt_len  = (m->pkt.pkt_len + len);
941
942         return (char*) m->pkt.data;
943 }
944
945 /**
946  * Append len bytes to an mbuf.
947  *
948  * Append len bytes to an mbuf and return a pointer to the start address
949  * of the added data. If there is not enough tailroom in the last
950  * segment, the function will return NULL, without modifying the mbuf.
951  *
952  * @param m
953  *   The packet mbuf.
954  * @param len
955  *   The amount of data to append (in bytes).
956  * @return
957  *   A pointer to the start of the newly appended data, or
958  *   NULL if there is not enough tailroom space in the last segment
959  */
960 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
961 {
962         void *tail;
963         struct rte_mbuf *m_last;
964
965         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
966
967         m_last = rte_pktmbuf_lastseg(m);
968         if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
969                 return NULL;
970
971         tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
972         m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
973         m->pkt.pkt_len  = (m->pkt.pkt_len + len);
974         return (char*) tail;
975 }
976
977 /**
978  * Remove len bytes at the beginning of an mbuf.
979  *
980  * Returns a pointer to the start address of the new data area. If the
981  * length is greater than the length of the first segment, then the
982  * function will fail and return NULL, without modifying the mbuf.
983  *
984  * @param m
985  *   The packet mbuf.
986  * @param len
987  *   The amount of data to remove (in bytes).
988  * @return
989  *   A pointer to the new start of the data.
990  */
991 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
992 {
993         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
994
995         if (unlikely(len > m->pkt.data_len))
996                 return NULL;
997
998         m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
999         m->pkt.data = ((char*) m->pkt.data + len);
1000         m->pkt.pkt_len  = (m->pkt.pkt_len - len);
1001         return (char*) m->pkt.data;
1002 }
1003
1004 /**
1005  * Remove len bytes of data at the end of the mbuf.
1006  *
1007  * If the length is greater than the length of the last segment, the
1008  * function will fail and return -1 without modifying the mbuf.
1009  *
1010  * @param m
1011  *   The packet mbuf.
1012  * @param len
1013  *   The amount of data to remove (in bytes).
1014  * @return
1015  *   - 0: On success.
1016  *   - -1: On error.
1017  */
1018 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1019 {
1020         struct rte_mbuf *m_last;
1021
1022         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1023
1024         m_last = rte_pktmbuf_lastseg(m);
1025         if (unlikely(len > m_last->pkt.data_len))
1026                 return -1;
1027
1028         m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
1029         m->pkt.pkt_len  = (m->pkt.pkt_len - len);
1030         return 0;
1031 }
1032
1033 /**
1034  * Test if mbuf data is contiguous.
1035  *
1036  * @param m
1037  *   The packet mbuf.
1038  * @return
1039  *   - 1, if all data is contiguous (one segment).
1040  *   - 0, if there is several segments.
1041  */
1042 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1043 {
1044         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1045         return !!(m->pkt.nb_segs == 1);
1046 }
1047
1048 /**
1049  * Dump an mbuf structure to the console.
1050  *
1051  * Dump all fields for the given packet mbuf and all its associated
1052  * segments (in the case of a chained buffer).
1053  *
1054  * @param f
1055  *   A pointer to a file for output
1056  * @param m
1057  *   The packet mbuf.
1058  * @param dump_len
1059  *   If dump_len != 0, also dump the "dump_len" first data bytes of
1060  *   the packet.
1061  */
1062 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1063
1064 #ifdef __cplusplus
1065 }
1066 #endif
1067
1068 #endif /* _RTE_MBUF_H_ */