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