DPDK  22.11.7-rc1
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_prefetch.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_mbuf_ptype.h>
42 #include <rte_mbuf_core.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
56 const char *rte_get_rx_ol_flag_name(uint64_t mask);
57 
70 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
71 
82 const char *rte_get_tx_ol_flag_name(uint64_t mask);
83 
96 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
97 
108 static inline void
110 {
111  rte_prefetch0(&m->cacheline0);
112 }
113 
125 static inline void
127 {
128 #if RTE_CACHE_LINE_SIZE == 64
129  rte_prefetch0(&m->cacheline1);
130 #else
131  RTE_SET_USED(m);
132 #endif
133 }
134 
135 
136 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
137 
146 static inline rte_iova_t
147 rte_mbuf_iova_get(const struct rte_mbuf *m)
148 {
149 #if RTE_IOVA_AS_PA
150  return m->buf_iova;
151 #else
152  return (rte_iova_t)m->buf_addr;
153 #endif
154 }
155 
164 static inline void
166 {
167 #if RTE_IOVA_AS_PA
168  m->buf_iova = iova;
169 #else
170  RTE_SET_USED(m);
171  RTE_SET_USED(iova);
172 #endif
173 }
174 
183 static inline rte_iova_t
184 rte_mbuf_data_iova(const struct rte_mbuf *mb)
185 {
186  return rte_mbuf_iova_get(mb) + mb->data_off;
187 }
188 
201 static inline rte_iova_t
203 {
204  return rte_mbuf_iova_get(mb) + RTE_PKTMBUF_HEADROOM;
205 }
206 
215 static inline struct rte_mbuf *
217 {
218  return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
219 }
220 
236 static inline char *
237 rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
238 {
239  return (char *)mb + sizeof(*mb) + rte_pktmbuf_priv_size(mp);
240 }
241 
250 static inline char *
252 {
253  return rte_mbuf_buf_addr(mb, mb->pool) + RTE_PKTMBUF_HEADROOM;
254 }
255 
269 static inline char *
271 {
272  return rte_mbuf_buf_addr(md, md->pool);
273 }
274 
287 static inline void *
289 {
290  return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
291 }
292 
301  uint16_t mbuf_priv_size;
302  uint32_t flags;
303 };
304 
313 static inline uint32_t
315 {
316  struct rte_pktmbuf_pool_private *mbp_priv;
317 
318  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
319  return mbp_priv->flags;
320 }
321 
328 #define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF (1 << 0)
329 
337 #define RTE_MBUF_HAS_PINNED_EXTBUF(mb) \
338  (rte_pktmbuf_priv_flags(mb->pool) & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
339 
340 #ifdef RTE_LIBRTE_MBUF_DEBUG
341 
343 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
344 
345 #else /* RTE_LIBRTE_MBUF_DEBUG */
346 
348 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
349 
350 #endif /* RTE_LIBRTE_MBUF_DEBUG */
351 
352 #ifdef RTE_MBUF_REFCNT_ATOMIC
353 
361 static inline uint16_t
362 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
363 {
364  return __atomic_load_n(&m->refcnt, __ATOMIC_RELAXED);
365 }
366 
374 static inline void
375 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
376 {
377  __atomic_store_n(&m->refcnt, new_value, __ATOMIC_RELAXED);
378 }
379 
380 /* internal */
381 static inline uint16_t
382 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
383 {
384  return __atomic_add_fetch(&m->refcnt, (uint16_t)value,
385  __ATOMIC_ACQ_REL);
386 }
387 
397 static inline uint16_t
398 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
399 {
400  /*
401  * The atomic_add is an expensive operation, so we don't want to
402  * call it in the case where we know we are the unique holder of
403  * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
404  * operation has to be used because concurrent accesses on the
405  * reference counter can occur.
406  */
407  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
408  ++value;
409  rte_mbuf_refcnt_set(m, (uint16_t)value);
410  return (uint16_t)value;
411  }
412 
413  return __rte_mbuf_refcnt_update(m, value);
414 }
415 
416 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
417 
418 /* internal */
419 static inline uint16_t
420 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
421 {
422  m->refcnt = (uint16_t)(m->refcnt + value);
423  return m->refcnt;
424 }
425 
429 static inline uint16_t
430 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
431 {
432  return __rte_mbuf_refcnt_update(m, value);
433 }
434 
438 static inline uint16_t
440 {
441  return m->refcnt;
442 }
443 
447 static inline void
448 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
449 {
450  m->refcnt = new_value;
451 }
452 
453 #endif /* RTE_MBUF_REFCNT_ATOMIC */
454 
463 static inline uint16_t
465 {
466  return __atomic_load_n(&shinfo->refcnt, __ATOMIC_RELAXED);
467 }
468 
477 static inline void
479  uint16_t new_value)
480 {
481  __atomic_store_n(&shinfo->refcnt, new_value, __ATOMIC_RELAXED);
482 }
483 
495 static inline uint16_t
497  int16_t value)
498 {
499  if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
500  ++value;
501  rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
502  return (uint16_t)value;
503  }
504 
505  return __atomic_add_fetch(&shinfo->refcnt, (uint16_t)value,
506  __ATOMIC_ACQ_REL);
507 }
508 
510 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
511  if ((m) != NULL) \
512  rte_prefetch0(m); \
513 } while (0)
514 
515 
528 void
529 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
530 
550 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
551  const char **reason);
552 
565 static __rte_always_inline void
567 {
568  RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
569  RTE_ASSERT(m->next == NULL);
570  RTE_ASSERT(m->nb_segs == 1);
572 }
573 
575 #define MBUF_RAW_ALLOC_CHECK(m) __rte_mbuf_raw_sanity_check(m)
576 
596 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
597 {
598  struct rte_mbuf *m;
599 
600  if (rte_mempool_get(mp, (void **)&m) < 0)
601  return NULL;
603  return m;
604 }
605 
620 static __rte_always_inline void
622 {
623  RTE_ASSERT(!RTE_MBUF_CLONED(m) &&
626  rte_mempool_put(m->pool, m);
627 }
628 
651 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
652  void *m, unsigned i);
653 
674 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
675 
709 struct rte_mempool *
710 rte_pktmbuf_pool_create(const char *name, unsigned n,
711  unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
712  int socket_id);
713 
750 struct rte_mempool *
751 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
752  unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
753  int socket_id, const char *ops_name);
754 
757  void *buf_ptr;
759  size_t buf_len;
760  uint16_t elt_size;
761 };
762 
803 __rte_experimental
804 struct rte_mempool *
805 rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
806  unsigned int cache_size, uint16_t priv_size,
807  uint16_t data_room_size, int socket_id,
808  const struct rte_pktmbuf_extmem *ext_mem,
809  unsigned int ext_num);
810 
822 static inline uint16_t
824 {
825  struct rte_pktmbuf_pool_private *mbp_priv;
826 
827  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
828  return mbp_priv->mbuf_data_room_size;
829 }
830 
843 static inline uint16_t
845 {
846  struct rte_pktmbuf_pool_private *mbp_priv;
847 
848  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
849  return mbp_priv->mbuf_priv_size;
850 }
851 
860 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
861 {
862  m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
863  (uint16_t)m->buf_len);
864 }
865 
874 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
875 {
876  m->next = NULL;
877  m->pkt_len = 0;
878  m->tx_offload = 0;
879  m->vlan_tci = 0;
880  m->vlan_tci_outer = 0;
881  m->nb_segs = 1;
883 
885  m->packet_type = 0;
887 
888  m->data_len = 0;
890 }
891 
905 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
906 {
907  struct rte_mbuf *m;
908  if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
910  return m;
911 }
912 
927 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
928  struct rte_mbuf **mbufs, unsigned count)
929 {
930  unsigned idx = 0;
931  int rc;
932 
933  rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
934  if (unlikely(rc))
935  return rc;
936 
937  /* To understand duff's device on loop unwinding optimization, see
938  * https://en.wikipedia.org/wiki/Duff's_device.
939  * Here while() loop is used rather than do() while{} to avoid extra
940  * check if count is zero.
941  */
942  switch (count % 4) {
943  case 0:
944  while (idx != count) {
945  __rte_mbuf_raw_sanity_check(mbufs[idx]);
946  rte_pktmbuf_reset(mbufs[idx]);
947  idx++;
948  /* fall-through */
949  case 3:
950  __rte_mbuf_raw_sanity_check(mbufs[idx]);
951  rte_pktmbuf_reset(mbufs[idx]);
952  idx++;
953  /* fall-through */
954  case 2:
955  __rte_mbuf_raw_sanity_check(mbufs[idx]);
956  rte_pktmbuf_reset(mbufs[idx]);
957  idx++;
958  /* fall-through */
959  case 1:
960  __rte_mbuf_raw_sanity_check(mbufs[idx]);
961  rte_pktmbuf_reset(mbufs[idx]);
962  idx++;
963  /* fall-through */
964  }
965  }
966  return 0;
967 }
968 
1001 static inline struct rte_mbuf_ext_shared_info *
1002 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
1004 {
1005  struct rte_mbuf_ext_shared_info *shinfo;
1006  void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
1007  void *addr;
1008 
1009  addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
1010  sizeof(uintptr_t));
1011  if (addr <= buf_addr)
1012  return NULL;
1013 
1014  shinfo = (struct rte_mbuf_ext_shared_info *)addr;
1015  shinfo->free_cb = free_cb;
1016  shinfo->fcb_opaque = fcb_opaque;
1017  rte_mbuf_ext_refcnt_set(shinfo, 1);
1018 
1019  *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
1020  return shinfo;
1021 }
1022 
1083 static inline void
1084 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1085  rte_iova_t buf_iova, uint16_t buf_len,
1086  struct rte_mbuf_ext_shared_info *shinfo)
1087 {
1088  /* mbuf should not be read-only */
1089  RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1090  RTE_ASSERT(shinfo->free_cb != NULL);
1091 
1092  m->buf_addr = buf_addr;
1093  rte_mbuf_iova_set(m, buf_iova);
1094  m->buf_len = buf_len;
1095 
1096  m->data_len = 0;
1097  m->data_off = 0;
1098 
1100  m->shinfo = shinfo;
1101 }
1102 
1110 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1111 
1120 static inline void
1121 rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1122 {
1123 #if !RTE_IOVA_AS_PA
1124  mdst->dynfield2 = msrc->dynfield2;
1125 #endif
1126  memcpy(&mdst->dynfield1, msrc->dynfield1, sizeof(mdst->dynfield1));
1127 }
1128 
1129 /* internal */
1130 static inline void
1131 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1132 {
1133  mdst->port = msrc->port;
1134  mdst->vlan_tci = msrc->vlan_tci;
1135  mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1136  mdst->tx_offload = msrc->tx_offload;
1137  mdst->hash = msrc->hash;
1138  mdst->packet_type = msrc->packet_type;
1139  rte_mbuf_dynfield_copy(mdst, msrc);
1140 }
1141 
1163 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1164 {
1165  RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1166  rte_mbuf_refcnt_read(mi) == 1);
1167 
1168  if (RTE_MBUF_HAS_EXTBUF(m)) {
1170  mi->ol_flags = m->ol_flags;
1171  mi->shinfo = m->shinfo;
1172  } else {
1173  /* if m is not direct, get the mbuf that embeds the data */
1175  mi->priv_size = m->priv_size;
1177  }
1178 
1179  __rte_pktmbuf_copy_hdr(mi, m);
1180 
1181  mi->data_off = m->data_off;
1182  mi->data_len = m->data_len;
1184  mi->buf_addr = m->buf_addr;
1185  mi->buf_len = m->buf_len;
1186 
1187  mi->next = NULL;
1188  mi->pkt_len = mi->data_len;
1189  mi->nb_segs = 1;
1190 
1191  __rte_mbuf_sanity_check(mi, 1);
1193 }
1194 
1202 static inline void
1203 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1204 {
1205  RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1206  RTE_ASSERT(m->shinfo != NULL);
1207 
1208  if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1209  m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1210 }
1211 
1218 static inline void
1219 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1220 {
1221  struct rte_mbuf *md;
1222 
1223  RTE_ASSERT(RTE_MBUF_CLONED(m));
1224 
1225  md = rte_mbuf_from_indirect(m);
1226 
1227  if (rte_mbuf_refcnt_update(md, -1) == 0) {
1228  md->next = NULL;
1229  md->nb_segs = 1;
1230  rte_mbuf_refcnt_set(md, 1);
1231  rte_mbuf_raw_free(md);
1232  }
1233 }
1234 
1253 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1254 {
1255  struct rte_mempool *mp = m->pool;
1256  uint32_t mbuf_size, buf_len;
1257  uint16_t priv_size;
1258 
1259  if (RTE_MBUF_HAS_EXTBUF(m)) {
1260  /*
1261  * The mbuf has the external attached buffer,
1262  * we should check the type of the memory pool where
1263  * the mbuf was allocated from to detect the pinned
1264  * external buffer.
1265  */
1266  uint32_t flags = rte_pktmbuf_priv_flags(mp);
1267 
1268  if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
1269  /*
1270  * The pinned external buffer should not be
1271  * detached from its backing mbuf, just exit.
1272  */
1273  return;
1274  }
1275  __rte_pktmbuf_free_extbuf(m);
1276  } else {
1277  __rte_pktmbuf_free_direct(m);
1278  }
1279  priv_size = rte_pktmbuf_priv_size(mp);
1280  mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1281  buf_len = rte_pktmbuf_data_room_size(mp);
1282 
1283  m->priv_size = priv_size;
1284  m->buf_addr = (char *)m + mbuf_size;
1285  rte_mbuf_iova_set(m, rte_mempool_virt2iova(m) + mbuf_size);
1286  m->buf_len = (uint16_t)buf_len;
1288  m->data_len = 0;
1289  m->ol_flags = 0;
1290 }
1291 
1305 static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)
1306 {
1307  struct rte_mbuf_ext_shared_info *shinfo;
1308 
1309  /* Clear flags, mbuf is being freed. */
1311  shinfo = m->shinfo;
1312 
1313  /* Optimize for performance - do not dec/reinit */
1314  if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1))
1315  return 0;
1316 
1317  /*
1318  * Direct usage of add primitive to avoid
1319  * duplication of comparing with one.
1320  */
1321  if (likely(__atomic_add_fetch(&shinfo->refcnt, (uint16_t)-1,
1322  __ATOMIC_ACQ_REL)))
1323  return 1;
1324 
1325  /* Reinitialize counter before mbuf freeing. */
1326  rte_mbuf_ext_refcnt_set(shinfo, 1);
1327  return 0;
1328 }
1329 
1344 static __rte_always_inline struct rte_mbuf *
1346 {
1348 
1349  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1350 
1351  if (!RTE_MBUF_DIRECT(m)) {
1352  rte_pktmbuf_detach(m);
1353  if (RTE_MBUF_HAS_EXTBUF(m) &&
1355  __rte_pktmbuf_pinned_extbuf_decref(m))
1356  return NULL;
1357  }
1358 
1359  if (m->next != NULL)
1360  m->next = NULL;
1361  if (m->nb_segs != 1)
1362  m->nb_segs = 1;
1363 
1364  return m;
1365 
1366  } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1367 
1368  if (!RTE_MBUF_DIRECT(m)) {
1369  rte_pktmbuf_detach(m);
1370  if (RTE_MBUF_HAS_EXTBUF(m) &&
1372  __rte_pktmbuf_pinned_extbuf_decref(m))
1373  return NULL;
1374  }
1375 
1376  if (m->next != NULL)
1377  m->next = NULL;
1378  if (m->nb_segs != 1)
1379  m->nb_segs = 1;
1380  rte_mbuf_refcnt_set(m, 1);
1381 
1382  return m;
1383  }
1384  return NULL;
1385 }
1386 
1396 static __rte_always_inline void
1398 {
1399  m = rte_pktmbuf_prefree_seg(m);
1400  if (likely(m != NULL))
1401  rte_mbuf_raw_free(m);
1402 }
1403 
1413 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1414 {
1415  struct rte_mbuf *m_next;
1416 
1417  if (m != NULL)
1419 
1420  while (m != NULL) {
1421  m_next = m->next;
1423  m = m_next;
1424  }
1425 }
1426 
1439 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1440 
1458 struct rte_mbuf *
1459 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1460 
1482 struct rte_mbuf *
1483 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1484  uint32_t offset, uint32_t length);
1485 
1497 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1498 {
1500 
1501  do {
1502  rte_mbuf_refcnt_update(m, v);
1503  } while ((m = m->next) != NULL);
1504 }
1505 
1514 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1515 {
1517  return m->data_off;
1518 }
1519 
1528 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1529 {
1531  return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1532  m->data_len);
1533 }
1534 
1543 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1544 {
1546  while (m->next != NULL)
1547  m = m->next;
1548  return m;
1549 }
1550 
1559 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1560 
1569 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1570 
1586 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1587  uint16_t len)
1588 {
1590 
1591  if (unlikely(len > rte_pktmbuf_headroom(m)))
1592  return NULL;
1593 
1594  /* NB: elaborating the subtraction like this instead of using
1595  * -= allows us to ensure the result type is uint16_t
1596  * avoiding compiler warnings on gcc 8.1 at least */
1597  m->data_off = (uint16_t)(m->data_off - len);
1598  m->data_len = (uint16_t)(m->data_len + len);
1599  m->pkt_len = (m->pkt_len + len);
1600 
1601  return (char *)m->buf_addr + m->data_off;
1602 }
1603 
1619 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1620 {
1621  void *tail;
1622  struct rte_mbuf *m_last;
1623 
1625 
1626  m_last = rte_pktmbuf_lastseg(m);
1627  if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1628  return NULL;
1629 
1630  tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1631  m_last->data_len = (uint16_t)(m_last->data_len + len);
1632  m->pkt_len = (m->pkt_len + len);
1633  return (char*) tail;
1634 }
1635 
1650 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1651 {
1653 
1654  if (unlikely(len > m->data_len))
1655  return NULL;
1656 
1657  /* NB: elaborating the addition like this instead of using
1658  * += allows us to ensure the result type is uint16_t
1659  * avoiding compiler warnings on gcc 8.1 at least */
1660  m->data_len = (uint16_t)(m->data_len - len);
1661  m->data_off = (uint16_t)(m->data_off + len);
1662  m->pkt_len = (m->pkt_len - len);
1663  return (char *)m->buf_addr + m->data_off;
1664 }
1665 
1680 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1681 {
1682  struct rte_mbuf *m_last;
1683 
1685 
1686  m_last = rte_pktmbuf_lastseg(m);
1687  if (unlikely(len > m_last->data_len))
1688  return -1;
1689 
1690  m_last->data_len = (uint16_t)(m_last->data_len - len);
1691  m->pkt_len = (m->pkt_len - len);
1692  return 0;
1693 }
1694 
1704 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1705 {
1707  return m->nb_segs == 1;
1708 }
1709 
1713 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1714  uint32_t len, void *buf);
1715 
1736 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1737  uint32_t off, uint32_t len, void *buf)
1738 {
1739  if (likely(off + len <= rte_pktmbuf_data_len(m)))
1740  return rte_pktmbuf_mtod_offset(m, char *, off);
1741  else
1742  return __rte_pktmbuf_read(m, off, len, buf);
1743 }
1744 
1761 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1762 {
1763  struct rte_mbuf *cur_tail;
1764 
1765  /* Check for number-of-segments-overflow */
1766  if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1767  return -EOVERFLOW;
1768 
1769  /* Chain 'tail' onto the old tail */
1770  cur_tail = rte_pktmbuf_lastseg(head);
1771  cur_tail->next = tail;
1772 
1773  /* accumulate number of segments and total length.
1774  * NB: elaborating the addition like this instead of using
1775  * -= allows us to ensure the result type is uint16_t
1776  * avoiding compiler warnings on gcc 8.1 at least */
1777  head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1778  head->pkt_len += tail->pkt_len;
1779 
1780  /* pkt_len is only set in the head */
1781  tail->pkt_len = tail->data_len;
1782 
1783  return 0;
1784 }
1785 
1807 static __rte_always_inline uint64_t
1808 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1809  uint64_t ol3, uint64_t ol2, uint64_t unused)
1810 {
1811  return il2 << RTE_MBUF_L2_LEN_OFS |
1812  il3 << RTE_MBUF_L3_LEN_OFS |
1813  il4 << RTE_MBUF_L4_LEN_OFS |
1814  tso << RTE_MBUF_TSO_SEGSZ_OFS |
1815  ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1816  ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1817  unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1818 }
1819 
1830 static inline int
1832 {
1833  uint64_t ol_flags = m->ol_flags;
1834 
1835  /* Does packet set any of available offloads? */
1836  if (!(ol_flags & RTE_MBUF_F_TX_OFFLOAD_MASK))
1837  return 0;
1838 
1839  /* IP checksum can be counted only for IPv4 packet */
1840  if ((ol_flags & RTE_MBUF_F_TX_IP_CKSUM) && (ol_flags & RTE_MBUF_F_TX_IPV6))
1841  return -EINVAL;
1842 
1843  /* IP type not set when required */
1844  if (ol_flags & (RTE_MBUF_F_TX_L4_MASK | RTE_MBUF_F_TX_TCP_SEG))
1845  if (!(ol_flags & (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IPV6)))
1846  return -EINVAL;
1847 
1848  /* Check requirements for TSO packet */
1849  if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
1850  if ((m->tso_segsz == 0) ||
1851  ((ol_flags & RTE_MBUF_F_TX_IPV4) &&
1852  !(ol_flags & RTE_MBUF_F_TX_IP_CKSUM)))
1853  return -EINVAL;
1854 
1855  /* RTE_MBUF_F_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1856  if ((ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) &&
1857  !(ol_flags & RTE_MBUF_F_TX_OUTER_IPV4))
1858  return -EINVAL;
1859 
1860  return 0;
1861 }
1862 
1866 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1867 
1880 static inline int
1882 {
1883  if (rte_pktmbuf_is_contiguous(mbuf))
1884  return 0;
1885  return __rte_pktmbuf_linearize(mbuf);
1886 }
1887 
1902 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1903 
1907 static inline uint32_t
1909 {
1910  return m->hash.sched.queue_id;
1911 }
1912 
1916 static inline uint8_t
1918 {
1919  return m->hash.sched.traffic_class;
1920 }
1921 
1925 static inline uint8_t
1927 {
1928  return m->hash.sched.color;
1929 }
1930 
1943 static inline void
1944 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1945  uint8_t *traffic_class,
1946  uint8_t *color)
1947 {
1948  struct rte_mbuf_sched sched = m->hash.sched;
1949 
1950  *queue_id = sched.queue_id;
1951  *traffic_class = sched.traffic_class;
1952  *color = sched.color;
1953 }
1954 
1958 static inline void
1960 {
1961  m->hash.sched.queue_id = queue_id;
1962 }
1963 
1967 static inline void
1969 {
1970  m->hash.sched.traffic_class = traffic_class;
1971 }
1972 
1976 static inline void
1978 {
1979  m->hash.sched.color = color;
1980 }
1981 
1994 static inline void
1996  uint8_t traffic_class,
1997  uint8_t color)
1998 {
1999  m->hash.sched = (struct rte_mbuf_sched){
2000  .queue_id = queue_id,
2001  .traffic_class = traffic_class,
2002  .color = color,
2003  .reserved = 0,
2004  };
2005 }
2006 
2007 #ifdef __cplusplus
2008 }
2009 #endif
2010 
2011 #endif /* _RTE_MBUF_H_ */
static void rte_pktmbuf_reset(struct rte_mbuf *m)
Definition: rte_mbuf.h:874
struct rte_mbuf_ext_shared_info * shinfo
static rte_iova_t rte_mbuf_data_iova(const struct rte_mbuf *mb)
Definition: rte_mbuf.h:184
struct rte_mbuf * next
uint16_t mbuf_data_room_size
Definition: rte_mbuf.h:300
void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count)
uint16_t vlan_tci_outer
#define __rte_always_inline
Definition: rte_common.h:255
#define RTE_MBUF_F_TX_OFFLOAD_MASK
static struct rte_mbuf * rte_pktmbuf_alloc(struct rte_mempool *mp)
Definition: rte_mbuf.h:905
static void rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
Definition: rte_mbuf.h:1968
static uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp)
Definition: rte_mbuf.h:844
static int rte_validate_tx_offload(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1831
uint32_t queue_id
#define likely(x)
static void rte_pktmbuf_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:1413
static uint32_t rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1908
#define RTE_PTR_ALIGN_FLOOR(ptr, align)
Definition: rte_common.h:319
void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
uint64_t rte_iova_t
Definition: rte_common.h:458
static __rte_always_inline void rte_pktmbuf_free_seg(struct rte_mbuf *m)
Definition: rte_mbuf.h:1397
void * buf_addr
static struct rte_mbuf * rte_mbuf_from_indirect(struct rte_mbuf *mi)
Definition: rte_mbuf.h:216
uint16_t data_len
rte_mbuf_extbuf_free_callback_t free_cb
static int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
Definition: rte_mbuf.h:1761
static uint8_t rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1917
#define __rte_unused
Definition: rte_common.h:120
static void rte_mbuf_iova_set(struct rte_mbuf *m, rte_iova_t iova)
Definition: rte_mbuf.h:165
#define RTE_MBUF_F_TX_IPV6
uint64_t tso_segsz
struct rte_mbuf * rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, uint32_t offset, uint32_t length)
static __rte_always_inline void __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m)
Definition: rte_mbuf.h:566
void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
static uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1514
static int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs, unsigned count)
Definition: rte_mbuf.h:927
static void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
Definition: rte_mbuf.h:860
static void * rte_mbuf_to_priv(struct rte_mbuf *m)
Definition: rte_mbuf.h:288
uint32_t cache_size
Definition: rte_mempool.h:231
static void rte_mbuf_prefetch_part2(struct rte_mbuf *m)
Definition: rte_mbuf.h:126
char name[RTE_MEMPOOL_NAMESIZE]
Definition: rte_mempool.h:220
static uint16_t rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo, int16_t value)
Definition: rte_mbuf.h:496
uint16_t nb_segs
uint16_t port
int rte_mbuf_check(const struct rte_mbuf *m, int is_header, const char **reason)
#define rte_pktmbuf_mtod_offset(m, t, o)
static __rte_always_inline struct rte_mbuf * rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
Definition: rte_mbuf.h:1345
static int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1704
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:290
int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
#define RTE_MBUF_MAX_NB_SEGS
static void rte_mbuf_sched_set(struct rte_mbuf *m, uint32_t queue_id, uint8_t traffic_class, uint8_t color)
Definition: rte_mbuf.h:1995
static uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1528
static __rte_always_inline void rte_mbuf_raw_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:621
static void rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id, uint8_t *traffic_class, uint8_t *color)
Definition: rte_mbuf.h:1944
#define unlikely(x)
uint16_t priv_size
static __rte_always_inline uint64_t rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso, uint64_t ol3, uint64_t ol2, uint64_t unused)
Definition: rte_mbuf.h:1808
static void rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
Definition: rte_mbuf.h:1959
static char * rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
Definition: rte_mbuf.h:237
void rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
static void rte_mbuf_sched_color_set(struct rte_mbuf *m, uint8_t color)
Definition: rte_mbuf.h:1977
void(* rte_mbuf_extbuf_free_callback_t)(void *addr, void *opaque)
#define RTE_MIN(a, b)
Definition: rte_common.h:613
uint64_t dynfield2
#define __rte_mbuf_sanity_check(m, is_h)
Definition: rte_mbuf.h:348
const char * rte_get_tx_ol_flag_name(uint64_t mask)
static uint16_t rte_mbuf_refcnt_read(const struct rte_mbuf *m)
Definition: rte_mbuf.h:439
static int rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
Definition: rte_mbuf.h:1881
static __rte_always_inline int rte_mempool_get(struct rte_mempool *mp, void **obj_p)
Definition: rte_mempool.h:1669
static uint32_t rte_pktmbuf_priv_flags(struct rte_mempool *mp)
Definition: rte_mbuf.h:314
uint16_t elt_size
Definition: rte_mbuf.h:760
uint16_t refcnt
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1650
#define RTE_MBUF_DIRECT(mb)
#define RTE_MBUF_CLONED(mb)
static void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
Definition: rte_mbuf.h:1163
static __rte_always_inline int rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
Definition: rte_mempool.h:1640
#define RTE_MBUF_F_TX_OUTER_IP_CKSUM
uint64_t ol_flags
static void rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
Definition: rte_mbuf.h:1121
static void rte_pktmbuf_detach(struct rte_mbuf *m)
Definition: rte_mbuf.h:1253
static uint16_t rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
Definition: rte_mbuf.h:464
uint32_t pkt_len
uint16_t buf_len
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1569
#define RTE_MBUF_F_TX_L4_MASK
static uint16_t rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
Definition: rte_mbuf.h:430
uint32_t packet_type
#define RTE_MBUF_F_TX_IP_CKSUM
static uint16_t rte_pktmbuf_data_room_size(struct rte_mempool *mp)
Definition: rte_mbuf.h:823
#define RTE_MBUF_F_TX_TCP_SEG
#define RTE_MBUF_F_INDIRECT
#define RTE_MBUF_PORT_INVALID
const char * rte_get_rx_ol_flag_name(uint64_t mask)
static struct rte_mbuf_ext_shared_info * rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len, rte_mbuf_extbuf_free_callback_t free_cb, void *fcb_opaque)
Definition: rte_mbuf.h:1002
#define RTE_MBUF_F_TX_OUTER_IPV4
void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg, void *m, unsigned i)
#define RTE_MBUF_HAS_EXTBUF(mb)
struct rte_mempool * pool
static void rte_mbuf_ext_refcnt_set(struct rte_mbuf_ext_shared_info *shinfo, uint16_t new_value)
Definition: rte_mbuf.h:478
static char * rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1619
static void rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
Definition: rte_mbuf.h:448
struct rte_mempool * rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n, unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id, const char *ops_name)
static rte_iova_t rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
Definition: rte_mbuf.h:202
static int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1680
static char * rte_mbuf_to_baddr(struct rte_mbuf *md)
Definition: rte_mbuf.h:270
static const void * rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off, uint32_t len, void *buf)
Definition: rte_mbuf.h:1736
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1586
static struct rte_mbuf * rte_mbuf_raw_alloc(struct rte_mempool *mp)
Definition: rte_mbuf.h:596
static rte_iova_t rte_mbuf_iova_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:147
static void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
Definition: rte_mbuf.h:1497
#define RTE_PTR_SUB(ptr, x)
Definition: rte_common.h:295
static struct rte_mbuf * rte_pktmbuf_lastseg(struct rte_mbuf *m)
Definition: rte_mbuf.h:1543
__rte_experimental struct rte_mempool * rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n, unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id, const struct rte_pktmbuf_extmem *ext_mem, unsigned int ext_num)
static uint8_t rte_mbuf_sched_color_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1926
#define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF
Definition: rte_mbuf.h:328
static char * rte_mbuf_data_addr_default(struct rte_mbuf *mb)
Definition: rte_mbuf.h:251
static rte_iova_t rte_mempool_virt2iova(const void *elt)
Definition: rte_mempool.h:1794
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1477
rte_iova_t buf_iova
Definition: rte_mbuf.h:758
uint8_t traffic_class
#define RTE_MBUF_F_TX_IPV4
#define RTE_PTR_DIFF(ptr1, ptr2)
Definition: rte_common.h:302
static void rte_mbuf_prefetch_part1(struct rte_mbuf *m)
Definition: rte_mbuf.h:109
#define RTE_MBUF_F_EXTERNAL
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1822
uint64_t tx_offload
uint16_t vlan_tci
#define RTE_MBUF_HAS_PINNED_EXTBUF(mb)
Definition: rte_mbuf.h:337
struct rte_mbuf * rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp)
#define RTE_SET_USED(x)
Definition: rte_common.h:135
int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
static void rte_prefetch0(const volatile void *p)
struct rte_mempool * rte_pktmbuf_pool_create(const char *name, unsigned n, unsigned cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id)
uint32_t dynfield1[9]
static void rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr, rte_iova_t buf_iova, uint16_t buf_len, struct rte_mbuf_ext_shared_info *shinfo)
Definition: rte_mbuf.h:1084