DPDK  18.05.1
rte_mbuf.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2014 6WIND S.A.
4  */
5 
6 #ifndef _RTE_MBUF_H_
7 #define _RTE_MBUF_H_
8 
34 #include <stdint.h>
35 #include <rte_compat.h>
36 #include <rte_common.h>
37 #include <rte_config.h>
38 #include <rte_mempool.h>
39 #include <rte_memory.h>
40 #include <rte_atomic.h>
41 #include <rte_prefetch.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_mbuf_ptype.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /*
50  * Packet Offload Features Flags. It also carry packet type information.
51  * Critical resources. Both rx/tx shared these bits. Be cautious on any change
52  *
53  * - RX flags start at bit position zero, and get added to the left of previous
54  * flags.
55  * - The most-significant 3 bits are reserved for generic mbuf flags
56  * - TX flags therefore start at bit position 60 (i.e. 63-3), and new flags get
57  * added to the right of the previously defined flags i.e. they should count
58  * downwards, not upwards.
59  *
60  * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
61  * rte_get_tx_ol_flag_name().
62  */
63 
71 #define PKT_RX_VLAN (1ULL << 0)
72 
73 #define PKT_RX_RSS_HASH (1ULL << 1)
74 #define PKT_RX_FDIR (1ULL << 2)
83 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
84 
92 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
93 
94 #define PKT_RX_EIP_CKSUM_BAD (1ULL << 5)
102 #define PKT_RX_VLAN_STRIPPED (1ULL << 6)
103 
112 #define PKT_RX_IP_CKSUM_MASK ((1ULL << 4) | (1ULL << 7))
113 
114 #define PKT_RX_IP_CKSUM_UNKNOWN 0
115 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
116 #define PKT_RX_IP_CKSUM_GOOD (1ULL << 7)
117 #define PKT_RX_IP_CKSUM_NONE ((1ULL << 4) | (1ULL << 7))
118 
127 #define PKT_RX_L4_CKSUM_MASK ((1ULL << 3) | (1ULL << 8))
128 
129 #define PKT_RX_L4_CKSUM_UNKNOWN 0
130 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
131 #define PKT_RX_L4_CKSUM_GOOD (1ULL << 8)
132 #define PKT_RX_L4_CKSUM_NONE ((1ULL << 3) | (1ULL << 8))
133 
134 #define PKT_RX_IEEE1588_PTP (1ULL << 9)
135 #define PKT_RX_IEEE1588_TMST (1ULL << 10)
136 #define PKT_RX_FDIR_ID (1ULL << 13)
137 #define PKT_RX_FDIR_FLX (1ULL << 14)
147 #define PKT_RX_QINQ_STRIPPED (1ULL << 15)
148 
154 #define PKT_RX_LRO (1ULL << 16)
155 
159 #define PKT_RX_TIMESTAMP (1ULL << 17)
160 
164 #define PKT_RX_SEC_OFFLOAD (1ULL << 18)
165 
169 #define PKT_RX_SEC_OFFLOAD_FAILED (1ULL << 19)
170 
178 #define PKT_RX_QINQ (1ULL << 20)
179 
180 /* add new RX flags here */
181 
182 /* add new TX flags here */
183 
189 #define PKT_TX_UDP_SEG (1ULL << 42)
190 
194 #define PKT_TX_SEC_OFFLOAD (1ULL << 43)
195 
200 #define PKT_TX_MACSEC (1ULL << 44)
201 
210 #define PKT_TX_TUNNEL_VXLAN (0x1ULL << 45)
211 #define PKT_TX_TUNNEL_GRE (0x2ULL << 45)
212 #define PKT_TX_TUNNEL_IPIP (0x3ULL << 45)
213 #define PKT_TX_TUNNEL_GENEVE (0x4ULL << 45)
214 
215 #define PKT_TX_TUNNEL_MPLSINUDP (0x5ULL << 45)
216 #define PKT_TX_TUNNEL_VXLAN_GPE (0x6ULL << 45)
217 
228 #define PKT_TX_TUNNEL_IP (0xDULL << 45)
229 
241 #define PKT_TX_TUNNEL_UDP (0xEULL << 45)
242 /* add new TX TUNNEL type here */
243 #define PKT_TX_TUNNEL_MASK (0xFULL << 45)
244 
248 #define PKT_TX_QINQ (1ULL << 49)
249 /* this old name is deprecated */
250 #define PKT_TX_QINQ_PKT PKT_TX_QINQ
251 
261 #define PKT_TX_TCP_SEG (1ULL << 50)
262 
263 #define PKT_TX_IEEE1588_TMST (1ULL << 51)
273 #define PKT_TX_L4_NO_CKSUM (0ULL << 52)
274 #define PKT_TX_TCP_CKSUM (1ULL << 52)
275 #define PKT_TX_SCTP_CKSUM (2ULL << 52)
276 #define PKT_TX_UDP_CKSUM (3ULL << 52)
277 #define PKT_TX_L4_MASK (3ULL << 52)
285 #define PKT_TX_IP_CKSUM (1ULL << 54)
286 
293 #define PKT_TX_IPV4 (1ULL << 55)
294 
301 #define PKT_TX_IPV6 (1ULL << 56)
302 
306 #define PKT_TX_VLAN (1ULL << 57)
307 /* this old name is deprecated */
308 #define PKT_TX_VLAN_PKT PKT_TX_VLAN
309 
316 #define PKT_TX_OUTER_IP_CKSUM (1ULL << 58)
317 
323 #define PKT_TX_OUTER_IPV4 (1ULL << 59)
324 
330 #define PKT_TX_OUTER_IPV6 (1ULL << 60)
331 
336 #define PKT_TX_OFFLOAD_MASK ( \
337  PKT_TX_IP_CKSUM | \
338  PKT_TX_L4_MASK | \
339  PKT_TX_OUTER_IP_CKSUM | \
340  PKT_TX_TCP_SEG | \
341  PKT_TX_IEEE1588_TMST | \
342  PKT_TX_QINQ_PKT | \
343  PKT_TX_VLAN_PKT | \
344  PKT_TX_TUNNEL_MASK | \
345  PKT_TX_MACSEC | \
346  PKT_TX_SEC_OFFLOAD)
347 
351 #define EXT_ATTACHED_MBUF (1ULL << 61)
352 
353 #define IND_ATTACHED_MBUF (1ULL << 62)
356 #define RTE_MBUF_PRIV_ALIGN 8
357 
366 const char *rte_get_rx_ol_flag_name(uint64_t mask);
367 
380 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
381 
392 const char *rte_get_tx_ol_flag_name(uint64_t mask);
393 
406 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
407 
414 #define RTE_MBUF_DEFAULT_DATAROOM 2048
415 #define RTE_MBUF_DEFAULT_BUF_SIZE \
416  (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
417 
418 /* define a set of marker types that can be used to refer to set points in the
419  * mbuf */
420 __extension__
421 typedef void *MARKER[0];
422 __extension__
423 typedef uint8_t MARKER8[0];
424 __extension__
425 typedef uint64_t MARKER64[0];
431 struct rte_mbuf {
432  MARKER cacheline0;
433 
434  void *buf_addr;
442  union {
443  rte_iova_t buf_iova;
445  } __rte_aligned(sizeof(rte_iova_t));
446 
447  /* next 8 bytes are initialised on RX descriptor rearm */
448  MARKER64 rearm_data;
449  uint16_t data_off;
450 
461  union {
463  uint16_t refcnt;
464  };
465  uint16_t nb_segs;
468  uint16_t port;
469 
470  uint64_t ol_flags;
472  /* remaining bytes are set on RX when pulling packet from descriptor */
473  MARKER rx_descriptor_fields1;
474 
475  /*
476  * The packet type, which is the combination of outer/inner L2, L3, L4
477  * and tunnel types. The packet_type is about data really present in the
478  * mbuf. Example: if vlan stripping is enabled, a received vlan packet
479  * would have RTE_PTYPE_L2_ETHER and not RTE_PTYPE_L2_VLAN because the
480  * vlan is stripped from the data.
481  */
483  union {
484  uint32_t packet_type;
485  struct {
486  uint32_t l2_type:4;
487  uint32_t l3_type:4;
488  uint32_t l4_type:4;
489  uint32_t tun_type:4;
491  union {
492  uint8_t inner_esp_next_proto;
497  __extension__
498  struct {
499  uint8_t inner_l2_type:4;
501  uint8_t inner_l3_type:4;
503  };
504  };
505  uint32_t inner_l4_type:4;
506  };
507  };
508 
509  uint32_t pkt_len;
510  uint16_t data_len;
512  uint16_t vlan_tci;
513 
514  union {
515  uint32_t rss;
516  struct {
518  union {
519  struct {
520  uint16_t hash;
521  uint16_t id;
522  };
523  uint32_t lo;
525  };
526  uint32_t hi;
529  } fdir;
530  struct {
531  uint32_t lo;
532  uint32_t hi;
533  } sched;
534  uint32_t usr;
535  } hash;
538  uint16_t vlan_tci_outer;
539 
540  uint16_t buf_len;
545  uint64_t timestamp;
546 
547  /* second cache line - fields only used in slow path or on TX */
548  MARKER cacheline1 __rte_cache_min_aligned;
549 
551  union {
552  void *userdata;
553  uint64_t udata64;
554  };
555 
556  struct rte_mempool *pool;
557  struct rte_mbuf *next;
559  /* fields to support TX offloads */
561  union {
562  uint64_t tx_offload;
563  __extension__
564  struct {
565  uint64_t l2_len:7;
569  uint64_t l3_len:9;
570  uint64_t l4_len:8;
571  uint64_t tso_segsz:16;
573  /* fields for TX offloading of tunnels */
574  uint64_t outer_l3_len:9;
575  uint64_t outer_l2_len:7;
577  /* uint64_t unused:8; */
578  };
579  };
580 
583  uint16_t priv_size;
584 
586  uint16_t timesync;
587 
589  uint32_t seqn;
590 
595 
597 
601 typedef void (*rte_mbuf_extbuf_free_callback_t)(void *addr, void *opaque);
602 
608  void *fcb_opaque;
610 };
611 
613 #define RTE_MBUF_MAX_NB_SEGS UINT16_MAX
614 
625 static inline void
627 {
628  rte_prefetch0(&m->cacheline0);
629 }
630 
642 static inline void
644 {
645 #if RTE_CACHE_LINE_SIZE == 64
646  rte_prefetch0(&m->cacheline1);
647 #else
648  RTE_SET_USED(m);
649 #endif
650 }
651 
652 
653 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
654 
663 static inline rte_iova_t
664 rte_mbuf_data_iova(const struct rte_mbuf *mb)
665 {
666  return mb->buf_iova + mb->data_off;
667 }
668 
669 __rte_deprecated
670 static inline phys_addr_t
671 rte_mbuf_data_dma_addr(const struct rte_mbuf *mb)
672 {
673  return rte_mbuf_data_iova(mb);
674 }
675 
688 static inline rte_iova_t
690 {
691  return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
692 }
693 
694 __rte_deprecated
695 static inline phys_addr_t
696 rte_mbuf_data_dma_addr_default(const struct rte_mbuf *mb)
697 {
698  return rte_mbuf_data_iova_default(mb);
699 }
700 
709 static inline struct rte_mbuf *
711 {
712  return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
713 }
714 
723 static inline char *
725 {
726  char *buffer_addr;
727  buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
728  return buffer_addr;
729 }
730 
738 #define RTE_MBUF_CLONED(mb) ((mb)->ol_flags & IND_ATTACHED_MBUF)
739 
744 #define RTE_MBUF_INDIRECT(mb) RTE_MBUF_CLONED(mb)
745 
751 #define RTE_MBUF_HAS_EXTBUF(mb) ((mb)->ol_flags & EXT_ATTACHED_MBUF)
752 
759 #define RTE_MBUF_DIRECT(mb) \
760  (!((mb)->ol_flags & (IND_ATTACHED_MBUF | EXT_ATTACHED_MBUF)))
761 
770  uint16_t mbuf_priv_size;
771 };
772 
773 #ifdef RTE_LIBRTE_MBUF_DEBUG
774 
776 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
777 
778 #else /* RTE_LIBRTE_MBUF_DEBUG */
779 
781 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
782 
783 #endif /* RTE_LIBRTE_MBUF_DEBUG */
784 
785 #ifdef RTE_MBUF_REFCNT_ATOMIC
786 
794 static inline uint16_t
795 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
796 {
797  return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
798 }
799 
807 static inline void
808 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
809 {
810  rte_atomic16_set(&m->refcnt_atomic, (int16_t)new_value);
811 }
812 
813 /* internal */
814 static inline uint16_t
815 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
816 {
817  return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
818 }
819 
829 static inline uint16_t
830 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
831 {
832  /*
833  * The atomic_add is an expensive operation, so we don't want to
834  * call it in the case where we know we are the uniq holder of
835  * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
836  * operation has to be used because concurrent accesses on the
837  * reference counter can occur.
838  */
839  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
840  ++value;
841  rte_mbuf_refcnt_set(m, (uint16_t)value);
842  return (uint16_t)value;
843  }
844 
845  return __rte_mbuf_refcnt_update(m, value);
846 }
847 
848 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
849 
850 /* internal */
851 static inline uint16_t
852 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
853 {
854  m->refcnt = (uint16_t)(m->refcnt + value);
855  return m->refcnt;
856 }
857 
861 static inline uint16_t
862 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
863 {
864  return __rte_mbuf_refcnt_update(m, value);
865 }
866 
870 static inline uint16_t
872 {
873  return m->refcnt;
874 }
875 
879 static inline void
880 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
881 {
882  m->refcnt = new_value;
883 }
884 
885 #endif /* RTE_MBUF_REFCNT_ATOMIC */
886 
895 static inline uint16_t
897 {
898  return (uint16_t)(rte_atomic16_read(&shinfo->refcnt_atomic));
899 }
900 
909 static inline void
911  uint16_t new_value)
912 {
913  rte_atomic16_set(&shinfo->refcnt_atomic, (int16_t)new_value);
914 }
915 
927 static inline uint16_t
929  int16_t value)
930 {
931  if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
932  ++value;
933  rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
934  return (uint16_t)value;
935  }
936 
937  return (uint16_t)rte_atomic16_add_return(&shinfo->refcnt_atomic, value);
938 }
939 
941 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
942  if ((m) != NULL) \
943  rte_prefetch0(m); \
944 } while (0)
945 
946 
959 void
960 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
961 
962 #define MBUF_RAW_ALLOC_CHECK(m) do { \
963  RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1); \
964  RTE_ASSERT((m)->next == NULL); \
965  RTE_ASSERT((m)->nb_segs == 1); \
966  __rte_mbuf_sanity_check(m, 0); \
967 } while (0)
968 
988 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
989 {
990  struct rte_mbuf *m;
991 
992  if (rte_mempool_get(mp, (void **)&m) < 0)
993  return NULL;
994  MBUF_RAW_ALLOC_CHECK(m);
995  return m;
996 }
997 
1012 static __rte_always_inline void
1014 {
1015  RTE_ASSERT(RTE_MBUF_DIRECT(m));
1016  RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
1017  RTE_ASSERT(m->next == NULL);
1018  RTE_ASSERT(m->nb_segs == 1);
1020  rte_mempool_put(m->pool, m);
1021 }
1022 
1023 /* compat with older versions */
1024 __rte_deprecated
1025 static inline void
1026 __rte_mbuf_raw_free(struct rte_mbuf *m)
1027 {
1028  rte_mbuf_raw_free(m);
1029 }
1030 
1050 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
1051  void *m, unsigned i);
1052 
1053 
1071 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
1072 
1107 struct rte_mempool *
1108 rte_pktmbuf_pool_create(const char *name, unsigned n,
1109  unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
1110  int socket_id);
1111 
1149 struct rte_mempool * __rte_experimental
1150 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
1151  unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
1152  int socket_id, const char *ops_name);
1153 
1165 static inline uint16_t
1167 {
1168  struct rte_pktmbuf_pool_private *mbp_priv;
1169 
1170  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1171  return mbp_priv->mbuf_data_room_size;
1172 }
1173 
1186 static inline uint16_t
1188 {
1189  struct rte_pktmbuf_pool_private *mbp_priv;
1190 
1191  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1192  return mbp_priv->mbuf_priv_size;
1193 }
1194 
1203 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
1204 {
1205  m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
1206  (uint16_t)m->buf_len);
1207 }
1208 
1217 #define MBUF_INVALID_PORT UINT16_MAX
1218 
1219 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
1220 {
1221  m->next = NULL;
1222  m->pkt_len = 0;
1223  m->tx_offload = 0;
1224  m->vlan_tci = 0;
1225  m->vlan_tci_outer = 0;
1226  m->nb_segs = 1;
1227  m->port = MBUF_INVALID_PORT;
1228 
1229  m->ol_flags = 0;
1230  m->packet_type = 0;
1232 
1233  m->data_len = 0;
1235 }
1236 
1250 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
1251 {
1252  struct rte_mbuf *m;
1253  if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
1254  rte_pktmbuf_reset(m);
1255  return m;
1256 }
1257 
1272 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
1273  struct rte_mbuf **mbufs, unsigned count)
1274 {
1275  unsigned idx = 0;
1276  int rc;
1277 
1278  rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
1279  if (unlikely(rc))
1280  return rc;
1281 
1282  /* To understand duff's device on loop unwinding optimization, see
1283  * https://en.wikipedia.org/wiki/Duff's_device.
1284  * Here while() loop is used rather than do() while{} to avoid extra
1285  * check if count is zero.
1286  */
1287  switch (count % 4) {
1288  case 0:
1289  while (idx != count) {
1290  MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1291  rte_pktmbuf_reset(mbufs[idx]);
1292  idx++;
1293  /* fall-through */
1294  case 3:
1295  MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1296  rte_pktmbuf_reset(mbufs[idx]);
1297  idx++;
1298  /* fall-through */
1299  case 2:
1300  MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1301  rte_pktmbuf_reset(mbufs[idx]);
1302  idx++;
1303  /* fall-through */
1304  case 1:
1305  MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1306  rte_pktmbuf_reset(mbufs[idx]);
1307  idx++;
1308  /* fall-through */
1309  }
1310  }
1311  return 0;
1312 }
1313 
1346 static inline struct rte_mbuf_ext_shared_info *
1347 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
1349 {
1350  struct rte_mbuf_ext_shared_info *shinfo;
1351  void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
1352  void *addr;
1353 
1354  addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
1355  sizeof(uintptr_t));
1356  if (addr <= buf_addr)
1357  return NULL;
1358 
1359  shinfo = (struct rte_mbuf_ext_shared_info *)addr;
1360  shinfo->free_cb = free_cb;
1361  shinfo->fcb_opaque = fcb_opaque;
1362  rte_mbuf_ext_refcnt_set(shinfo, 1);
1363 
1364  *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
1365  return shinfo;
1366 }
1367 
1431 static inline void __rte_experimental
1432 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1433  rte_iova_t buf_iova, uint16_t buf_len,
1434  struct rte_mbuf_ext_shared_info *shinfo)
1435 {
1436  /* mbuf should not be read-only */
1437  RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1438  RTE_ASSERT(shinfo->free_cb != NULL);
1439 
1440  m->buf_addr = buf_addr;
1441  m->buf_iova = buf_iova;
1442  m->buf_len = buf_len;
1443 
1444  m->data_len = 0;
1445  m->data_off = 0;
1446 
1448  m->shinfo = shinfo;
1449 }
1450 
1458 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1459 
1481 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1482 {
1483  RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1484  rte_mbuf_refcnt_read(mi) == 1);
1485 
1486  if (RTE_MBUF_HAS_EXTBUF(m)) {
1488  mi->ol_flags = m->ol_flags;
1489  mi->shinfo = m->shinfo;
1490  } else {
1491  /* if m is not direct, get the mbuf that embeds the data */
1493  mi->priv_size = m->priv_size;
1494  mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1495  }
1496 
1497  mi->buf_iova = m->buf_iova;
1498  mi->buf_addr = m->buf_addr;
1499  mi->buf_len = m->buf_len;
1500 
1501  mi->data_off = m->data_off;
1502  mi->data_len = m->data_len;
1503  mi->port = m->port;
1504  mi->vlan_tci = m->vlan_tci;
1505  mi->vlan_tci_outer = m->vlan_tci_outer;
1506  mi->tx_offload = m->tx_offload;
1507  mi->hash = m->hash;
1508 
1509  mi->next = NULL;
1510  mi->pkt_len = mi->data_len;
1511  mi->nb_segs = 1;
1512  mi->packet_type = m->packet_type;
1513  mi->timestamp = m->timestamp;
1514 
1515  __rte_mbuf_sanity_check(mi, 1);
1517 }
1518 
1526 static inline void
1527 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1528 {
1529  RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1530  RTE_ASSERT(m->shinfo != NULL);
1531 
1532  if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1533  m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1534 }
1535 
1542 static inline void
1543 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1544 {
1545  struct rte_mbuf *md;
1546 
1547  RTE_ASSERT(RTE_MBUF_INDIRECT(m));
1548 
1549  md = rte_mbuf_from_indirect(m);
1550 
1551  if (rte_mbuf_refcnt_update(md, -1) == 0) {
1552  md->next = NULL;
1553  md->nb_segs = 1;
1554  rte_mbuf_refcnt_set(md, 1);
1555  rte_mbuf_raw_free(md);
1556  }
1557 }
1558 
1572 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1573 {
1574  struct rte_mempool *mp = m->pool;
1575  uint32_t mbuf_size, buf_len;
1576  uint16_t priv_size;
1577 
1578  if (RTE_MBUF_HAS_EXTBUF(m))
1579  __rte_pktmbuf_free_extbuf(m);
1580  else
1581  __rte_pktmbuf_free_direct(m);
1582 
1583  priv_size = rte_pktmbuf_priv_size(mp);
1584  mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1585  buf_len = rte_pktmbuf_data_room_size(mp);
1586 
1587  m->priv_size = priv_size;
1588  m->buf_addr = (char *)m + mbuf_size;
1589  m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1590  m->buf_len = (uint16_t)buf_len;
1592  m->data_len = 0;
1593  m->ol_flags = 0;
1594 }
1595 
1610 static __rte_always_inline struct rte_mbuf *
1612 {
1614 
1615  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1616 
1617  if (!RTE_MBUF_DIRECT(m))
1618  rte_pktmbuf_detach(m);
1619 
1620  if (m->next != NULL) {
1621  m->next = NULL;
1622  m->nb_segs = 1;
1623  }
1624 
1625  return m;
1626 
1627  } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1628 
1629  if (!RTE_MBUF_DIRECT(m))
1630  rte_pktmbuf_detach(m);
1631 
1632  if (m->next != NULL) {
1633  m->next = NULL;
1634  m->nb_segs = 1;
1635  }
1636  rte_mbuf_refcnt_set(m, 1);
1637 
1638  return m;
1639  }
1640  return NULL;
1641 }
1642 
1643 /* deprecated, replaced by rte_pktmbuf_prefree_seg() */
1644 __rte_deprecated
1645 static inline struct rte_mbuf *
1646 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1647 {
1648  return rte_pktmbuf_prefree_seg(m);
1649 }
1650 
1660 static __rte_always_inline void
1662 {
1663  m = rte_pktmbuf_prefree_seg(m);
1664  if (likely(m != NULL))
1665  rte_mbuf_raw_free(m);
1666 }
1667 
1677 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1678 {
1679  struct rte_mbuf *m_next;
1680 
1681  if (m != NULL)
1683 
1684  while (m != NULL) {
1685  m_next = m->next;
1687  m = m_next;
1688  }
1689 }
1690 
1708 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
1709  struct rte_mempool *mp)
1710 {
1711  struct rte_mbuf *mc, *mi, **prev;
1712  uint32_t pktlen;
1713  uint16_t nseg;
1714 
1715  if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
1716  return NULL;
1717 
1718  mi = mc;
1719  prev = &mi->next;
1720  pktlen = md->pkt_len;
1721  nseg = 0;
1722 
1723  do {
1724  nseg++;
1725  rte_pktmbuf_attach(mi, md);
1726  *prev = mi;
1727  prev = &mi->next;
1728  } while ((md = md->next) != NULL &&
1729  (mi = rte_pktmbuf_alloc(mp)) != NULL);
1730 
1731  *prev = NULL;
1732  mc->nb_segs = nseg;
1733  mc->pkt_len = pktlen;
1734 
1735  /* Allocation of new indirect segment failed */
1736  if (unlikely (mi == NULL)) {
1737  rte_pktmbuf_free(mc);
1738  return NULL;
1739  }
1740 
1741  __rte_mbuf_sanity_check(mc, 1);
1742  return mc;
1743 }
1744 
1756 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1757 {
1759 
1760  do {
1761  rte_mbuf_refcnt_update(m, v);
1762  } while ((m = m->next) != NULL);
1763 }
1764 
1773 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1774 {
1776  return m->data_off;
1777 }
1778 
1787 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1788 {
1790  return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1791  m->data_len);
1792 }
1793 
1802 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1803 {
1805  while (m->next != NULL)
1806  m = m->next;
1807  return m;
1808 }
1809 
1824 #define rte_pktmbuf_mtod_offset(m, t, o) \
1825  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
1826 
1839 #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
1840 
1850 #define rte_pktmbuf_iova_offset(m, o) \
1851  (rte_iova_t)((m)->buf_iova + (m)->data_off + (o))
1852 
1853 /* deprecated */
1854 #define rte_pktmbuf_mtophys_offset(m, o) \
1855  rte_pktmbuf_iova_offset(m, o)
1856 
1864 #define rte_pktmbuf_iova(m) rte_pktmbuf_iova_offset(m, 0)
1865 
1866 /* deprecated */
1867 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_iova(m)
1868 
1877 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1878 
1887 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1888 
1904 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1905  uint16_t len)
1906 {
1908 
1909  if (unlikely(len > rte_pktmbuf_headroom(m)))
1910  return NULL;
1911 
1912  /* NB: elaborating the subtraction like this instead of using
1913  * -= allows us to ensure the result type is uint16_t
1914  * avoiding compiler warnings on gcc 8.1 at least */
1915  m->data_off = (uint16_t)(m->data_off - len);
1916  m->data_len = (uint16_t)(m->data_len + len);
1917  m->pkt_len = (m->pkt_len + len);
1918 
1919  return (char *)m->buf_addr + m->data_off;
1920 }
1921 
1937 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1938 {
1939  void *tail;
1940  struct rte_mbuf *m_last;
1941 
1943 
1944  m_last = rte_pktmbuf_lastseg(m);
1945  if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1946  return NULL;
1947 
1948  tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1949  m_last->data_len = (uint16_t)(m_last->data_len + len);
1950  m->pkt_len = (m->pkt_len + len);
1951  return (char*) tail;
1952 }
1953 
1968 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1969 {
1971 
1972  if (unlikely(len > m->data_len))
1973  return NULL;
1974 
1975  /* NB: elaborating the addition like this instead of using
1976  * += allows us to ensure the result type is uint16_t
1977  * avoiding compiler warnings on gcc 8.1 at least */
1978  m->data_len = (uint16_t)(m->data_len - len);
1979  m->data_off = (uint16_t)(m->data_off + len);
1980  m->pkt_len = (m->pkt_len - len);
1981  return (char *)m->buf_addr + m->data_off;
1982 }
1983 
1998 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1999 {
2000  struct rte_mbuf *m_last;
2001 
2003 
2004  m_last = rte_pktmbuf_lastseg(m);
2005  if (unlikely(len > m_last->data_len))
2006  return -1;
2007 
2008  m_last->data_len = (uint16_t)(m_last->data_len - len);
2009  m->pkt_len = (m->pkt_len - len);
2010  return 0;
2011 }
2012 
2022 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
2023 {
2025  return !!(m->nb_segs == 1);
2026 }
2027 
2031 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
2032  uint32_t len, void *buf);
2033 
2054 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
2055  uint32_t off, uint32_t len, void *buf)
2056 {
2057  if (likely(off + len <= rte_pktmbuf_data_len(m)))
2058  return rte_pktmbuf_mtod_offset(m, char *, off);
2059  else
2060  return __rte_pktmbuf_read(m, off, len, buf);
2061 }
2062 
2079 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
2080 {
2081  struct rte_mbuf *cur_tail;
2082 
2083  /* Check for number-of-segments-overflow */
2084  if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
2085  return -EOVERFLOW;
2086 
2087  /* Chain 'tail' onto the old tail */
2088  cur_tail = rte_pktmbuf_lastseg(head);
2089  cur_tail->next = tail;
2090 
2091  /* accumulate number of segments and total length.
2092  * NB: elaborating the addition like this instead of using
2093  * -= allows us to ensure the result type is uint16_t
2094  * avoiding compiler warnings on gcc 8.1 at least */
2095  head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
2096  head->pkt_len += tail->pkt_len;
2097 
2098  /* pkt_len is only set in the head */
2099  tail->pkt_len = tail->data_len;
2100 
2101  return 0;
2102 }
2103 
2114 static inline int
2116 {
2117  uint64_t ol_flags = m->ol_flags;
2118  uint64_t inner_l3_offset = m->l2_len;
2119 
2120  /* Does packet set any of available offloads? */
2121  if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
2122  return 0;
2123 
2124  if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
2125  /* NB: elaborating the addition like this instead of using
2126  * += gives the result uint64_t type instead of int,
2127  * avoiding compiler warnings on gcc 8.1 at least */
2128  inner_l3_offset = inner_l3_offset + m->outer_l2_len +
2129  m->outer_l3_len;
2130 
2131  /* Headers are fragmented */
2132  if (rte_pktmbuf_data_len(m) < inner_l3_offset + m->l3_len + m->l4_len)
2133  return -ENOTSUP;
2134 
2135  /* IP checksum can be counted only for IPv4 packet */
2136  if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
2137  return -EINVAL;
2138 
2139  /* IP type not set when required */
2140  if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
2141  if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
2142  return -EINVAL;
2143 
2144  /* Check requirements for TSO packet */
2145  if (ol_flags & PKT_TX_TCP_SEG)
2146  if ((m->tso_segsz == 0) ||
2147  ((ol_flags & PKT_TX_IPV4) &&
2148  !(ol_flags & PKT_TX_IP_CKSUM)))
2149  return -EINVAL;
2150 
2151  /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
2152  if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
2153  !(ol_flags & PKT_TX_OUTER_IPV4))
2154  return -EINVAL;
2155 
2156  return 0;
2157 }
2158 
2171 static inline int
2173 {
2174  size_t seg_len, copy_len;
2175  struct rte_mbuf *m;
2176  struct rte_mbuf *m_next;
2177  char *buffer;
2178 
2179  if (rte_pktmbuf_is_contiguous(mbuf))
2180  return 0;
2181 
2182  /* Extend first segment to the total packet length */
2183  copy_len = rte_pktmbuf_pkt_len(mbuf) - rte_pktmbuf_data_len(mbuf);
2184 
2185  if (unlikely(copy_len > rte_pktmbuf_tailroom(mbuf)))
2186  return -1;
2187 
2188  buffer = rte_pktmbuf_mtod_offset(mbuf, char *, mbuf->data_len);
2189  mbuf->data_len = (uint16_t)(mbuf->pkt_len);
2190 
2191  /* Append data from next segments to the first one */
2192  m = mbuf->next;
2193  while (m != NULL) {
2194  m_next = m->next;
2195 
2196  seg_len = rte_pktmbuf_data_len(m);
2197  rte_memcpy(buffer, rte_pktmbuf_mtod(m, char *), seg_len);
2198  buffer += seg_len;
2199 
2201  m = m_next;
2202  }
2203 
2204  mbuf->next = NULL;
2205  mbuf->nb_segs = 1;
2206 
2207  return 0;
2208 }
2209 
2224 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
2225 
2226 #ifdef __cplusplus
2227 }
2228 #endif
2229 
2230 #endif /* _RTE_MBUF_H_ */