DPDK  20.11.10
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_prefetch.h>
41 #include <rte_branch_prediction.h>
42 #include <rte_byteorder.h>
43 #include <rte_mbuf_ptype.h>
44 #include <rte_mbuf_core.h>
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
58 const char *rte_get_rx_ol_flag_name(uint64_t mask);
59 
72 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
73 
84 const char *rte_get_tx_ol_flag_name(uint64_t mask);
85 
98 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
99 
110 static inline void
112 {
113  rte_prefetch0(&m->cacheline0);
114 }
115 
127 static inline void
129 {
130 #if RTE_CACHE_LINE_SIZE == 64
131  rte_prefetch0(&m->cacheline1);
132 #else
133  RTE_SET_USED(m);
134 #endif
135 }
136 
137 
138 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
139 
148 static inline rte_iova_t
149 rte_mbuf_data_iova(const struct rte_mbuf *mb)
150 {
151  return mb->buf_iova + mb->data_off;
152 }
153 
166 static inline rte_iova_t
168 {
169  return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
170 }
171 
180 static inline struct rte_mbuf *
182 {
183  return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
184 }
185 
206 __rte_experimental
207 static inline char *
208 rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
209 {
210  return (char *)mb + sizeof(*mb) + rte_pktmbuf_priv_size(mp);
211 }
212 
224 __rte_experimental
225 static inline char *
227 {
228  /* gcc complains about calling this experimental function even
229  * when not using it. Hide it with ALLOW_EXPERIMENTAL_API.
230  */
231 #ifdef ALLOW_EXPERIMENTAL_API
232  return rte_mbuf_buf_addr(mb, mb->pool) + RTE_PKTMBUF_HEADROOM;
233 #else
234  return NULL;
235 #endif
236 }
237 
251 static inline char *
253 {
254 #ifdef ALLOW_EXPERIMENTAL_API
255  return rte_mbuf_buf_addr(md, md->pool);
256 #else
257  char *buffer_addr;
258  buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
259  return buffer_addr;
260 #endif
261 }
262 
275 __rte_experimental
276 static inline void *
278 {
279  return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
280 }
281 
290  uint16_t mbuf_priv_size;
291  uint32_t flags;
292 };
293 
302 static inline uint32_t
304 {
305  struct rte_pktmbuf_pool_private *mbp_priv;
306 
307  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
308  return mbp_priv->flags;
309 }
310 
317 #define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF (1 << 0)
318 
326 #define RTE_MBUF_HAS_PINNED_EXTBUF(mb) \
327  (rte_pktmbuf_priv_flags(mb->pool) & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
328 
329 #ifdef RTE_LIBRTE_MBUF_DEBUG
330 
332 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
333 
334 #else /* RTE_LIBRTE_MBUF_DEBUG */
335 
337 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
338 
339 #endif /* RTE_LIBRTE_MBUF_DEBUG */
340 
341 #ifdef RTE_MBUF_REFCNT_ATOMIC
342 
350 static inline uint16_t
351 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
352 {
353  return __atomic_load_n(&m->refcnt, __ATOMIC_RELAXED);
354 }
355 
363 static inline void
364 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
365 {
366  __atomic_store_n(&m->refcnt, new_value, __ATOMIC_RELAXED);
367 }
368 
369 /* internal */
370 static inline uint16_t
371 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
372 {
373  return __atomic_add_fetch(&m->refcnt, (uint16_t)value,
374  __ATOMIC_ACQ_REL);
375 }
376 
386 static inline uint16_t
387 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
388 {
389  /*
390  * The atomic_add is an expensive operation, so we don't want to
391  * call it in the case where we know we are the unique holder of
392  * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
393  * operation has to be used because concurrent accesses on the
394  * reference counter can occur.
395  */
396  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
397  ++value;
398  rte_mbuf_refcnt_set(m, (uint16_t)value);
399  return (uint16_t)value;
400  }
401 
402  return __rte_mbuf_refcnt_update(m, value);
403 }
404 
405 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
406 
407 /* internal */
408 static inline uint16_t
409 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
410 {
411  m->refcnt = (uint16_t)(m->refcnt + value);
412  return m->refcnt;
413 }
414 
418 static inline uint16_t
419 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
420 {
421  return __rte_mbuf_refcnt_update(m, value);
422 }
423 
427 static inline uint16_t
429 {
430  return m->refcnt;
431 }
432 
436 static inline void
437 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
438 {
439  m->refcnt = new_value;
440 }
441 
442 #endif /* RTE_MBUF_REFCNT_ATOMIC */
443 
452 static inline uint16_t
454 {
455  return __atomic_load_n(&shinfo->refcnt, __ATOMIC_RELAXED);
456 }
457 
466 static inline void
468  uint16_t new_value)
469 {
470  __atomic_store_n(&shinfo->refcnt, new_value, __ATOMIC_RELAXED);
471 }
472 
484 static inline uint16_t
486  int16_t value)
487 {
488  if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
489  ++value;
490  rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
491  return (uint16_t)value;
492  }
493 
494  return __atomic_add_fetch(&shinfo->refcnt, (uint16_t)value,
495  __ATOMIC_ACQ_REL);
496 }
497 
499 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
500  if ((m) != NULL) \
501  rte_prefetch0(m); \
502 } while (0)
503 
504 
517 void
518 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
519 
539 __rte_experimental
540 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
541  const char **reason);
542 
555 static __rte_always_inline void
557 {
558  RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
559  RTE_ASSERT(m->next == NULL);
560  RTE_ASSERT(m->nb_segs == 1);
562 }
563 
565 #define MBUF_RAW_ALLOC_CHECK(m) __rte_mbuf_raw_sanity_check(m)
566 
586 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
587 {
588  struct rte_mbuf *m;
589 
590  if (rte_mempool_get(mp, (void **)&m) < 0)
591  return NULL;
593  return m;
594 }
595 
610 static __rte_always_inline void
612 {
613  RTE_ASSERT(!RTE_MBUF_CLONED(m) &&
616  rte_mempool_put(m->pool, m);
617 }
618 
638 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
639  void *m, unsigned i);
640 
658 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
659 
693 struct rte_mempool *
694 rte_pktmbuf_pool_create(const char *name, unsigned n,
695  unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
696  int socket_id);
697 
734 struct rte_mempool *
735 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
736  unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
737  int socket_id, const char *ops_name);
738 
741  void *buf_ptr;
743  size_t buf_len;
744  uint16_t elt_size;
745 };
746 
787 __rte_experimental
788 struct rte_mempool *
789 rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
790  unsigned int cache_size, uint16_t priv_size,
791  uint16_t data_room_size, int socket_id,
792  const struct rte_pktmbuf_extmem *ext_mem,
793  unsigned int ext_num);
794 
806 static inline uint16_t
808 {
809  struct rte_pktmbuf_pool_private *mbp_priv;
810 
811  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
812  return mbp_priv->mbuf_data_room_size;
813 }
814 
827 static inline uint16_t
829 {
830  struct rte_pktmbuf_pool_private *mbp_priv;
831 
832  mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
833  return mbp_priv->mbuf_priv_size;
834 }
835 
844 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
845 {
846  m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
847  (uint16_t)m->buf_len);
848 }
849 
858 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
859 {
860  m->next = NULL;
861  m->pkt_len = 0;
862  m->tx_offload = 0;
863  m->vlan_tci = 0;
864  m->vlan_tci_outer = 0;
865  m->nb_segs = 1;
867 
869  m->packet_type = 0;
871 
872  m->data_len = 0;
874 }
875 
889 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
890 {
891  struct rte_mbuf *m;
892  if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
894  return m;
895 }
896 
911 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
912  struct rte_mbuf **mbufs, unsigned count)
913 {
914  unsigned idx = 0;
915  int rc;
916 
917  rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
918  if (unlikely(rc))
919  return rc;
920 
921  /* To understand duff's device on loop unwinding optimization, see
922  * https://en.wikipedia.org/wiki/Duff's_device.
923  * Here while() loop is used rather than do() while{} to avoid extra
924  * check if count is zero.
925  */
926  switch (count % 4) {
927  case 0:
928  while (idx != count) {
929  __rte_mbuf_raw_sanity_check(mbufs[idx]);
930  rte_pktmbuf_reset(mbufs[idx]);
931  idx++;
932  /* fall-through */
933  case 3:
934  __rte_mbuf_raw_sanity_check(mbufs[idx]);
935  rte_pktmbuf_reset(mbufs[idx]);
936  idx++;
937  /* fall-through */
938  case 2:
939  __rte_mbuf_raw_sanity_check(mbufs[idx]);
940  rte_pktmbuf_reset(mbufs[idx]);
941  idx++;
942  /* fall-through */
943  case 1:
944  __rte_mbuf_raw_sanity_check(mbufs[idx]);
945  rte_pktmbuf_reset(mbufs[idx]);
946  idx++;
947  /* fall-through */
948  }
949  }
950  return 0;
951 }
952 
985 static inline struct rte_mbuf_ext_shared_info *
986 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
988 {
989  struct rte_mbuf_ext_shared_info *shinfo;
990  void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
991  void *addr;
992 
993  addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
994  sizeof(uintptr_t));
995  if (addr <= buf_addr)
996  return NULL;
997 
998  shinfo = (struct rte_mbuf_ext_shared_info *)addr;
999  shinfo->free_cb = free_cb;
1000  shinfo->fcb_opaque = fcb_opaque;
1001  rte_mbuf_ext_refcnt_set(shinfo, 1);
1002 
1003  *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
1004  return shinfo;
1005 }
1006 
1067 static inline void
1068 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1069  rte_iova_t buf_iova, uint16_t buf_len,
1070  struct rte_mbuf_ext_shared_info *shinfo)
1071 {
1072  /* mbuf should not be read-only */
1073  RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1074  RTE_ASSERT(shinfo->free_cb != NULL);
1075 
1076  m->buf_addr = buf_addr;
1077  m->buf_iova = buf_iova;
1078  m->buf_len = buf_len;
1079 
1080  m->data_len = 0;
1081  m->data_off = 0;
1082 
1084  m->shinfo = shinfo;
1085 }
1086 
1094 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1095 
1104 static inline void
1105 rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1106 {
1107  memcpy(&mdst->dynfield1, msrc->dynfield1, sizeof(mdst->dynfield1));
1108 }
1109 
1110 /* internal */
1111 static inline void
1112 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1113 {
1114  mdst->port = msrc->port;
1115  mdst->vlan_tci = msrc->vlan_tci;
1116  mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1117  mdst->tx_offload = msrc->tx_offload;
1118  mdst->hash = msrc->hash;
1119  mdst->packet_type = msrc->packet_type;
1120  rte_mbuf_dynfield_copy(mdst, msrc);
1121 }
1122 
1144 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1145 {
1146  RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1147  rte_mbuf_refcnt_read(mi) == 1);
1148 
1149  if (RTE_MBUF_HAS_EXTBUF(m)) {
1151  mi->ol_flags = m->ol_flags;
1152  mi->shinfo = m->shinfo;
1153  } else {
1154  /* if m is not direct, get the mbuf that embeds the data */
1156  mi->priv_size = m->priv_size;
1157  mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1158  }
1159 
1160  __rte_pktmbuf_copy_hdr(mi, m);
1161 
1162  mi->data_off = m->data_off;
1163  mi->data_len = m->data_len;
1164  mi->buf_iova = m->buf_iova;
1165  mi->buf_addr = m->buf_addr;
1166  mi->buf_len = m->buf_len;
1167 
1168  mi->next = NULL;
1169  mi->pkt_len = mi->data_len;
1170  mi->nb_segs = 1;
1171 
1172  __rte_mbuf_sanity_check(mi, 1);
1174 }
1175 
1183 static inline void
1184 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1185 {
1186  RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1187  RTE_ASSERT(m->shinfo != NULL);
1188 
1189  if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1190  m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1191 }
1192 
1199 static inline void
1200 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1201 {
1202  struct rte_mbuf *md;
1203 
1204  RTE_ASSERT(RTE_MBUF_CLONED(m));
1205 
1206  md = rte_mbuf_from_indirect(m);
1207 
1208  if (rte_mbuf_refcnt_update(md, -1) == 0) {
1209  md->next = NULL;
1210  md->nb_segs = 1;
1211  rte_mbuf_refcnt_set(md, 1);
1212  rte_mbuf_raw_free(md);
1213  }
1214 }
1215 
1234 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1235 {
1236  struct rte_mempool *mp = m->pool;
1237  uint32_t mbuf_size, buf_len;
1238  uint16_t priv_size;
1239 
1240  if (RTE_MBUF_HAS_EXTBUF(m)) {
1241  /*
1242  * The mbuf has the external attached buffer,
1243  * we should check the type of the memory pool where
1244  * the mbuf was allocated from to detect the pinned
1245  * external buffer.
1246  */
1247  uint32_t flags = rte_pktmbuf_priv_flags(mp);
1248 
1249  if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
1250  /*
1251  * The pinned external buffer should not be
1252  * detached from its backing mbuf, just exit.
1253  */
1254  return;
1255  }
1256  __rte_pktmbuf_free_extbuf(m);
1257  } else {
1258  __rte_pktmbuf_free_direct(m);
1259  }
1260  priv_size = rte_pktmbuf_priv_size(mp);
1261  mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1262  buf_len = rte_pktmbuf_data_room_size(mp);
1263 
1264  m->priv_size = priv_size;
1265  m->buf_addr = (char *)m + mbuf_size;
1266  m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1267  m->buf_len = (uint16_t)buf_len;
1269  m->data_len = 0;
1270  m->ol_flags = 0;
1271 }
1272 
1286 static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)
1287 {
1288  struct rte_mbuf_ext_shared_info *shinfo;
1289 
1290  /* Clear flags, mbuf is being freed. */
1292  shinfo = m->shinfo;
1293 
1294  /* Optimize for performance - do not dec/reinit */
1295  if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1))
1296  return 0;
1297 
1298  /*
1299  * Direct usage of add primitive to avoid
1300  * duplication of comparing with one.
1301  */
1302  if (likely(__atomic_add_fetch(&shinfo->refcnt, (uint16_t)-1,
1303  __ATOMIC_ACQ_REL)))
1304  return 1;
1305 
1306  /* Reinitialize counter before mbuf freeing. */
1307  rte_mbuf_ext_refcnt_set(shinfo, 1);
1308  return 0;
1309 }
1310 
1325 static __rte_always_inline struct rte_mbuf *
1327 {
1329 
1330  if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1331 
1332  if (!RTE_MBUF_DIRECT(m)) {
1333  rte_pktmbuf_detach(m);
1334  if (RTE_MBUF_HAS_EXTBUF(m) &&
1336  __rte_pktmbuf_pinned_extbuf_decref(m))
1337  return NULL;
1338  }
1339 
1340  if (m->next != NULL)
1341  m->next = NULL;
1342  if (m->nb_segs != 1)
1343  m->nb_segs = 1;
1344 
1345  return m;
1346 
1347  } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1348 
1349  if (!RTE_MBUF_DIRECT(m)) {
1350  rte_pktmbuf_detach(m);
1351  if (RTE_MBUF_HAS_EXTBUF(m) &&
1353  __rte_pktmbuf_pinned_extbuf_decref(m))
1354  return NULL;
1355  }
1356 
1357  if (m->next != NULL)
1358  m->next = NULL;
1359  if (m->nb_segs != 1)
1360  m->nb_segs = 1;
1361  rte_mbuf_refcnt_set(m, 1);
1362 
1363  return m;
1364  }
1365  return NULL;
1366 }
1367 
1377 static __rte_always_inline void
1379 {
1380  m = rte_pktmbuf_prefree_seg(m);
1381  if (likely(m != NULL))
1382  rte_mbuf_raw_free(m);
1383 }
1384 
1394 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1395 {
1396  struct rte_mbuf *m_next;
1397 
1398  if (m != NULL)
1400 
1401  while (m != NULL) {
1402  m_next = m->next;
1404  m = m_next;
1405  }
1406 }
1407 
1420 __rte_experimental
1421 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1422 
1440 struct rte_mbuf *
1441 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1442 
1464 __rte_experimental
1465 struct rte_mbuf *
1466 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1467  uint32_t offset, uint32_t length);
1468 
1480 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1481 {
1483 
1484  do {
1485  rte_mbuf_refcnt_update(m, v);
1486  } while ((m = m->next) != NULL);
1487 }
1488 
1497 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1498 {
1500  return m->data_off;
1501 }
1502 
1511 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1512 {
1514  return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1515  m->data_len);
1516 }
1517 
1526 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1527 {
1529  while (m->next != NULL)
1530  m = m->next;
1531  return m;
1532 }
1533 
1542 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1543 
1552 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1553 
1569 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1570  uint16_t len)
1571 {
1573 
1574  if (unlikely(len > rte_pktmbuf_headroom(m)))
1575  return NULL;
1576 
1577  /* NB: elaborating the subtraction like this instead of using
1578  * -= allows us to ensure the result type is uint16_t
1579  * avoiding compiler warnings on gcc 8.1 at least */
1580  m->data_off = (uint16_t)(m->data_off - len);
1581  m->data_len = (uint16_t)(m->data_len + len);
1582  m->pkt_len = (m->pkt_len + len);
1583 
1584  return (char *)m->buf_addr + m->data_off;
1585 }
1586 
1602 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1603 {
1604  void *tail;
1605  struct rte_mbuf *m_last;
1606 
1608 
1609  m_last = rte_pktmbuf_lastseg(m);
1610  if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1611  return NULL;
1612 
1613  tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1614  m_last->data_len = (uint16_t)(m_last->data_len + len);
1615  m->pkt_len = (m->pkt_len + len);
1616  return (char*) tail;
1617 }
1618 
1633 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1634 {
1636 
1637  if (unlikely(len > m->data_len))
1638  return NULL;
1639 
1640  /* NB: elaborating the addition like this instead of using
1641  * += allows us to ensure the result type is uint16_t
1642  * avoiding compiler warnings on gcc 8.1 at least */
1643  m->data_len = (uint16_t)(m->data_len - len);
1644  m->data_off = (uint16_t)(m->data_off + len);
1645  m->pkt_len = (m->pkt_len - len);
1646  return (char *)m->buf_addr + m->data_off;
1647 }
1648 
1663 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1664 {
1665  struct rte_mbuf *m_last;
1666 
1668 
1669  m_last = rte_pktmbuf_lastseg(m);
1670  if (unlikely(len > m_last->data_len))
1671  return -1;
1672 
1673  m_last->data_len = (uint16_t)(m_last->data_len - len);
1674  m->pkt_len = (m->pkt_len - len);
1675  return 0;
1676 }
1677 
1687 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1688 {
1690  return m->nb_segs == 1;
1691 }
1692 
1696 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1697  uint32_t len, void *buf);
1698 
1719 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1720  uint32_t off, uint32_t len, void *buf)
1721 {
1722  if (likely(off + len <= rte_pktmbuf_data_len(m)))
1723  return rte_pktmbuf_mtod_offset(m, char *, off);
1724  else
1725  return __rte_pktmbuf_read(m, off, len, buf);
1726 }
1727 
1744 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1745 {
1746  struct rte_mbuf *cur_tail;
1747 
1748  /* Check for number-of-segments-overflow */
1749  if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1750  return -EOVERFLOW;
1751 
1752  /* Chain 'tail' onto the old tail */
1753  cur_tail = rte_pktmbuf_lastseg(head);
1754  cur_tail->next = tail;
1755 
1756  /* accumulate number of segments and total length.
1757  * NB: elaborating the addition like this instead of using
1758  * -= allows us to ensure the result type is uint16_t
1759  * avoiding compiler warnings on gcc 8.1 at least */
1760  head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1761  head->pkt_len += tail->pkt_len;
1762 
1763  /* pkt_len is only set in the head */
1764  tail->pkt_len = tail->data_len;
1765 
1766  return 0;
1767 }
1768 
1769 /*
1770  * @warning
1771  * @b EXPERIMENTAL: This API may change without prior notice.
1772  *
1773  * For given input values generate raw tx_offload value.
1774  * Note that it is caller responsibility to make sure that input parameters
1775  * don't exceed maximum bit-field values.
1776  * @param il2
1777  * l2_len value.
1778  * @param il3
1779  * l3_len value.
1780  * @param il4
1781  * l4_len value.
1782  * @param tso
1783  * tso_segsz value.
1784  * @param ol3
1785  * outer_l3_len value.
1786  * @param ol2
1787  * outer_l2_len value.
1788  * @param unused
1789  * unused value.
1790  * @return
1791  * raw tx_offload value.
1792  */
1793 static __rte_always_inline uint64_t
1794 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1795  uint64_t ol3, uint64_t ol2, uint64_t unused)
1796 {
1797  return il2 << RTE_MBUF_L2_LEN_OFS |
1798  il3 << RTE_MBUF_L3_LEN_OFS |
1799  il4 << RTE_MBUF_L4_LEN_OFS |
1800  tso << RTE_MBUF_TSO_SEGSZ_OFS |
1801  ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1802  ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1803  unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1804 }
1805 
1816 static inline int
1818 {
1819  uint64_t ol_flags = m->ol_flags;
1820 
1821  /* Does packet set any of available offloads? */
1822  if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1823  return 0;
1824 
1825  /* IP checksum can be counted only for IPv4 packet */
1826  if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1827  return -EINVAL;
1828 
1829  /* IP type not set when required */
1830  if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1831  if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1832  return -EINVAL;
1833 
1834  /* Check requirements for TSO packet */
1835  if (ol_flags & PKT_TX_TCP_SEG)
1836  if ((m->tso_segsz == 0) ||
1837  ((ol_flags & PKT_TX_IPV4) &&
1838  !(ol_flags & PKT_TX_IP_CKSUM)))
1839  return -EINVAL;
1840 
1841  /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1842  if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1843  !(ol_flags & PKT_TX_OUTER_IPV4))
1844  return -EINVAL;
1845 
1846  return 0;
1847 }
1848 
1852 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1853 
1866 static inline int
1868 {
1869  if (rte_pktmbuf_is_contiguous(mbuf))
1870  return 0;
1871  return __rte_pktmbuf_linearize(mbuf);
1872 }
1873 
1888 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1889 
1893 static inline uint32_t
1895 {
1896  return m->hash.sched.queue_id;
1897 }
1898 
1902 static inline uint8_t
1904 {
1905  return m->hash.sched.traffic_class;
1906 }
1907 
1911 static inline uint8_t
1913 {
1914  return m->hash.sched.color;
1915 }
1916 
1929 static inline void
1930 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1931  uint8_t *traffic_class,
1932  uint8_t *color)
1933 {
1934  struct rte_mbuf_sched sched = m->hash.sched;
1935 
1936  *queue_id = sched.queue_id;
1937  *traffic_class = sched.traffic_class;
1938  *color = sched.color;
1939 }
1940 
1944 static inline void
1946 {
1947  m->hash.sched.queue_id = queue_id;
1948 }
1949 
1953 static inline void
1955 {
1956  m->hash.sched.traffic_class = traffic_class;
1957 }
1958 
1962 static inline void
1964 {
1965  m->hash.sched.color = color;
1966 }
1967 
1980 static inline void
1982  uint8_t traffic_class,
1983  uint8_t color)
1984 {
1985  m->hash.sched = (struct rte_mbuf_sched){
1986  .queue_id = queue_id,
1987  .traffic_class = traffic_class,
1988  .color = color,
1989  .reserved = 0,
1990  };
1991 }
1992 
1993 #ifdef __cplusplus
1994 }
1995 #endif
1996 
1997 #endif /* _RTE_MBUF_H_ */
static void rte_pktmbuf_reset(struct rte_mbuf *m)
Definition: rte_mbuf.h:858
struct rte_mbuf_ext_shared_info * shinfo
static rte_iova_t rte_mbuf_data_iova(const struct rte_mbuf *mb)
Definition: rte_mbuf.h:149
struct rte_mbuf * next
uint16_t mbuf_data_room_size
Definition: rte_mbuf.h:289
uint16_t vlan_tci_outer
#define __rte_always_inline
Definition: rte_common.h:231
static struct rte_mbuf * rte_pktmbuf_alloc(struct rte_mempool *mp)
Definition: rte_mbuf.h:889
static void rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
Definition: rte_mbuf.h:1954
static uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp)
Definition: rte_mbuf.h:828
static int rte_validate_tx_offload(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1817
__rte_experimental struct rte_mbuf * rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, uint32_t offset, uint32_t length)
uint32_t queue_id
#define likely(x)
static void rte_pktmbuf_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:1394
#define PKT_TX_OUTER_IP_CKSUM
static uint32_t rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1894
#define RTE_PTR_ALIGN_FLOOR(ptr, align)
Definition: rte_common.h:282
void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
uint64_t rte_iova_t
Definition: rte_common.h:423
static __rte_always_inline void rte_pktmbuf_free_seg(struct rte_mbuf *m)
Definition: rte_mbuf.h:1378
void * buf_addr
static struct rte_mbuf * rte_mbuf_from_indirect(struct rte_mbuf *mi)
Definition: rte_mbuf.h:181
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:1744
__rte_experimental void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count)
static uint8_t rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1903
#define PKT_TX_IPV4
static __rte_experimental char * rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
Definition: rte_mbuf.h:208
#define __rte_unused
Definition: rte_common.h:121
__rte_experimental int rte_mbuf_check(const struct rte_mbuf *m, int is_header, const char **reason)
uint64_t tso_segsz
static __rte_always_inline void __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m)
Definition: rte_mbuf.h:556
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:1497
static int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs, unsigned count)
Definition: rte_mbuf.h:911
static void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
Definition: rte_mbuf.h:844
uint32_t cache_size
Definition: rte_mempool.h:225
static void rte_mbuf_prefetch_part2(struct rte_mbuf *m)
Definition: rte_mbuf.h:128
#define PKT_TX_OUTER_IPV4
static uint16_t rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo, int16_t value)
Definition: rte_mbuf.h:485
uint16_t nb_segs
#define IND_ATTACHED_MBUF
uint16_t port
#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:1326
static int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1687
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:253
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:1981
static uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
Definition: rte_mbuf.h:1511
static __rte_always_inline void rte_mbuf_raw_free(struct rte_mbuf *m)
Definition: rte_mbuf.h:611
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:1930
#define unlikely(x)
uint16_t priv_size
static void rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
Definition: rte_mbuf.h:1945
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:1963
void(* rte_mbuf_extbuf_free_callback_t)(void *addr, void *opaque)
#define RTE_MIN(a, b)
Definition: rte_common.h:578
#define __rte_mbuf_sanity_check(m, is_h)
Definition: rte_mbuf.h:337
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:428
#define PKT_TX_TCP_SEG
static int rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
Definition: rte_mbuf.h:1867
static __rte_always_inline int rte_mempool_get(struct rte_mempool *mp, void **obj_p)
Definition: rte_mempool.h:1577
static uint32_t rte_pktmbuf_priv_flags(struct rte_mempool *mp)
Definition: rte_mbuf.h:303
uint16_t elt_size
Definition: rte_mbuf.h:744
uint16_t refcnt
static char * rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1633
static __rte_experimental void * rte_mbuf_to_priv(struct rte_mbuf *m)
Definition: rte_mbuf.h:277
#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:1144
static __rte_always_inline int rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
Definition: rte_mempool.h:1548
#define PKT_TX_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:1105
static void rte_pktmbuf_detach(struct rte_mbuf *m)
Definition: rte_mbuf.h:1234
static uint16_t rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
Definition: rte_mbuf.h:453
uint32_t pkt_len
uint16_t buf_len
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1552
static uint16_t rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
Definition: rte_mbuf.h:419
uint32_t packet_type
static uint16_t rte_pktmbuf_data_room_size(struct rte_mempool *mp)
Definition: rte_mbuf.h:807
#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:986
#define PKT_TX_IPV6
#define PKT_TX_L4_MASK
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:467
static char * rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1602
static void rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
Definition: rte_mbuf.h:437
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:167
static int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1663
static char * rte_mbuf_to_baddr(struct rte_mbuf *md)
Definition: rte_mbuf.h:252
static const void * rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off, uint32_t len, void *buf)
Definition: rte_mbuf.h:1719
static char * rte_pktmbuf_prepend(struct rte_mbuf *m, uint16_t len)
Definition: rte_mbuf.h:1569
static struct rte_mbuf * rte_mbuf_raw_alloc(struct rte_mempool *mp)
Definition: rte_mbuf.h:586
static void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
Definition: rte_mbuf.h:1480
#define RTE_PTR_SUB(ptr, x)
Definition: rte_common.h:258
static struct rte_mbuf * rte_pktmbuf_lastseg(struct rte_mbuf *m)
Definition: rte_mbuf.h:1526
static __rte_experimental char * rte_mbuf_data_addr_default(__rte_unused struct rte_mbuf *mb)
Definition: rte_mbuf.h:226
__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:1912
#define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF
Definition: rte_mbuf.h:317
static rte_iova_t rte_mempool_virt2iova(const void *elt)
Definition: rte_mempool.h:1700
static __rte_always_inline void rte_mempool_put(struct rte_mempool *mp, void *obj)
Definition: rte_mempool.h:1395
rte_iova_t buf_iova
Definition: rte_mbuf.h:742
uint8_t traffic_class
#define RTE_PTR_DIFF(ptr1, ptr2)
Definition: rte_common.h:265
static void rte_mbuf_prefetch_part1(struct rte_mbuf *m)
Definition: rte_mbuf.h:111
#define PKT_TX_OFFLOAD_MASK
static void * rte_mempool_get_priv(struct rte_mempool *mp)
Definition: rte_mempool.h:1728
uint64_t tx_offload
char name[RTE_MEMZONE_NAMESIZE]
Definition: rte_mempool.h:214
uint16_t vlan_tci
#define RTE_MBUF_HAS_PINNED_EXTBUF(mb)
Definition: rte_mbuf.h:326
struct rte_mbuf * rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp)
#define RTE_SET_USED(x)
Definition: rte_common.h:136
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]
#define EXT_ATTACHED_MBUF
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:1068