DPDK  19.05.0
rte_mempool.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2016 6WIND S.A.
4  */
5 
6 #ifndef _RTE_MEMPOOL_H_
7 #define _RTE_MEMPOOL_H_
8 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <sys/queue.h>
42 
43 #include <rte_config.h>
44 #include <rte_spinlock.h>
45 #include <rte_log.h>
46 #include <rte_debug.h>
47 #include <rte_lcore.h>
48 #include <rte_memory.h>
49 #include <rte_branch_prediction.h>
50 #include <rte_ring.h>
51 #include <rte_memcpy.h>
52 #include <rte_common.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL
59 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL
60 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL
62 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
63 
66 struct rte_mempool_debug_stats {
67  uint64_t put_bulk;
68  uint64_t put_objs;
69  uint64_t get_success_bulk;
70  uint64_t get_success_objs;
71  uint64_t get_fail_bulk;
72  uint64_t get_fail_objs;
74  uint64_t get_success_blks;
76  uint64_t get_fail_blks;
78 #endif
79 
84  uint32_t size;
85  uint32_t flushthresh;
86  uint32_t len;
87  /*
88  * Cache is allocated to this size to allow it to overflow in certain
89  * cases to avoid needless emptying of cache.
90  */
91  void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3];
93 
98  uint32_t elt_size;
99  uint32_t header_size;
100  uint32_t trailer_size;
101  uint32_t total_size;
103 };
104 
106 #define RTE_MEMPOOL_NAMESIZE (RTE_RING_NAMESIZE - \
107  sizeof(RTE_MEMPOOL_MZ_PREFIX) + 1)
108 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
109 
110 /* "MP_<name>" */
111 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
112 
113 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
114 
116 #define MEMPOOL_PG_NUM_DEFAULT 1
117 
118 #ifndef RTE_MEMPOOL_ALIGN
119 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
120 #endif
121 
122 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
123 
135  struct rte_mempool *mp;
137  union {
140  };
141 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
142  uint64_t cookie;
143 #endif
144 };
145 
149 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
150 
151 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
152 
159 struct rte_mempool_objtlr {
160  uint64_t cookie;
161 };
162 
163 #endif
164 
168 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
169 
174  void *opaque);
175 
184  struct rte_mempool *mp;
185  void *addr;
187  union {
190  };
191  size_t len;
193  void *opaque;
194 };
195 
207  unsigned int contig_block_size;
209 
213 struct rte_mempool {
214  /*
215  * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
216  * compatibility requirements, it could be changed to
217  * RTE_MEMPOOL_NAMESIZE next time the ABI changes
218  */
221  union {
222  void *pool_data;
223  uint64_t pool_id;
224  };
225  void *pool_config;
226  const struct rte_memzone *mz;
227  unsigned int flags;
228  int socket_id;
229  uint32_t size;
230  uint32_t cache_size;
233  uint32_t elt_size;
234  uint32_t header_size;
235  uint32_t trailer_size;
237  unsigned private_data_size;
245  int32_t ops_index;
246 
249  uint32_t populated_size;
250  struct rte_mempool_objhdr_list elt_list;
251  uint32_t nb_mem_chunks;
252  struct rte_mempool_memhdr_list mem_list;
254 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
255 
256  struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
257 #endif
259 
260 #define MEMPOOL_F_NO_SPREAD 0x0001
261 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002
262 #define MEMPOOL_F_SP_PUT 0x0004
263 #define MEMPOOL_F_SC_GET 0x0008
264 #define MEMPOOL_F_POOL_CREATED 0x0010
265 #define MEMPOOL_F_NO_IOVA_CONTIG 0x0020
266 #define MEMPOOL_F_NO_PHYS_CONTIG MEMPOOL_F_NO_IOVA_CONTIG /* deprecated */
267 
278 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
279 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
280  unsigned __lcore_id = rte_lcore_id(); \
281  if (__lcore_id < RTE_MAX_LCORE) { \
282  mp->stats[__lcore_id].name##_objs += n; \
283  mp->stats[__lcore_id].name##_bulk += 1; \
284  } \
285  } while(0)
286 #define __MEMPOOL_CONTIG_BLOCKS_STAT_ADD(mp, name, n) do { \
287  unsigned int __lcore_id = rte_lcore_id(); \
288  if (__lcore_id < RTE_MAX_LCORE) { \
289  mp->stats[__lcore_id].name##_blks += n; \
290  mp->stats[__lcore_id].name##_bulk += 1; \
291  } \
292  } while (0)
293 #else
294 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
295 #define __MEMPOOL_CONTIG_BLOCKS_STAT_ADD(mp, name, n) do {} while (0)
296 #endif
297 
306 #define MEMPOOL_HEADER_SIZE(mp, cs) \
307  (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
308  (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
309 
310 /* return the header of a mempool object (internal) */
311 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
312 {
313  return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
314  sizeof(struct rte_mempool_objhdr));
315 }
316 
326 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
327 {
328  struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
329  return hdr->mp;
330 }
331 
332 /* return the trailer of a mempool object (internal) */
333 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
334 {
335  struct rte_mempool *mp = rte_mempool_from_obj(obj);
336  return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
337 }
338 
353 void rte_mempool_check_cookies(const struct rte_mempool *mp,
354  void * const *obj_table_const, unsigned n, int free);
355 
356 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
357 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
358  rte_mempool_check_cookies(mp, obj_table_const, n, free)
359 #else
360 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
361 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
362 
382  void * const *first_obj_table_const, unsigned int n, int free);
383 
384 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
385 #define __mempool_contig_blocks_check_cookies(mp, first_obj_table_const, n, \
386  free) \
387  rte_mempool_contig_blocks_check_cookies(mp, first_obj_table_const, n, \
388  free)
389 #else
390 #define __mempool_contig_blocks_check_cookies(mp, first_obj_table_const, n, \
391  free) \
392  do {} while (0)
393 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
394 
395 #define RTE_MEMPOOL_OPS_NAMESIZE 32
407 typedef int (*rte_mempool_alloc_t)(struct rte_mempool *mp);
408 
412 typedef void (*rte_mempool_free_t)(struct rte_mempool *mp);
413 
417 typedef int (*rte_mempool_enqueue_t)(struct rte_mempool *mp,
418  void * const *obj_table, unsigned int n);
419 
423 typedef int (*rte_mempool_dequeue_t)(struct rte_mempool *mp,
424  void **obj_table, unsigned int n);
425 
433  void **first_obj_table, unsigned int n);
434 
438 typedef unsigned (*rte_mempool_get_count)(const struct rte_mempool *mp);
439 
463 typedef ssize_t (*rte_mempool_calc_mem_size_t)(const struct rte_mempool *mp,
464  uint32_t obj_num, uint32_t pg_shift,
465  size_t *min_chunk_size, size_t *align);
466 
486 ssize_t rte_mempool_op_calc_mem_size_default(const struct rte_mempool *mp,
487  uint32_t obj_num, uint32_t pg_shift,
488  size_t *min_chunk_size, size_t *align);
489 
502 typedef void (rte_mempool_populate_obj_cb_t)(struct rte_mempool *mp,
503  void *opaque, void *vaddr, rte_iova_t iova);
504 
533 typedef int (*rte_mempool_populate_t)(struct rte_mempool *mp,
534  unsigned int max_objs,
535  void *vaddr, rte_iova_t iova, size_t len,
536  rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg);
537 
543  unsigned int max_objs,
544  void *vaddr, rte_iova_t iova, size_t len,
545  rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg);
546 
553 typedef int (*rte_mempool_get_info_t)(const struct rte_mempool *mp,
554  struct rte_mempool_info *info);
555 
556 
584 
585 #define RTE_MEMPOOL_MAX_OPS_IDX 16
596 struct rte_mempool_ops_table {
598  uint32_t num_ops;
604 
607 
617 static inline struct rte_mempool_ops *
618 rte_mempool_get_ops(int ops_index)
619 {
620  RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
621 
622  return &rte_mempool_ops_table.ops[ops_index];
623 }
624 
634 int
635 rte_mempool_ops_alloc(struct rte_mempool *mp);
636 
650 static inline int
651 rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
652  void **obj_table, unsigned n)
653 {
654  struct rte_mempool_ops *ops;
655 
656  ops = rte_mempool_get_ops(mp->ops_index);
657  return ops->dequeue(mp, obj_table, n);
658 }
659 
673 static inline int
674 rte_mempool_ops_dequeue_contig_blocks(struct rte_mempool *mp,
675  void **first_obj_table, unsigned int n)
676 {
677  struct rte_mempool_ops *ops;
678 
679  ops = rte_mempool_get_ops(mp->ops_index);
680  RTE_ASSERT(ops->dequeue_contig_blocks != NULL);
681  return ops->dequeue_contig_blocks(mp, first_obj_table, n);
682 }
683 
697 static inline int
698 rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
699  unsigned n)
700 {
701  struct rte_mempool_ops *ops;
702 
703  ops = rte_mempool_get_ops(mp->ops_index);
704  return ops->enqueue(mp, obj_table, n);
705 }
706 
715 unsigned
716 rte_mempool_ops_get_count(const struct rte_mempool *mp);
717 
737 ssize_t rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
738  uint32_t obj_num, uint32_t pg_shift,
739  size_t *min_chunk_size, size_t *align);
740 
764 int rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
765  void *vaddr, rte_iova_t iova, size_t len,
767  void *obj_cb_arg);
768 
784 __rte_experimental
785 int rte_mempool_ops_get_info(const struct rte_mempool *mp,
786  struct rte_mempool_info *info);
787 
794 void
795 rte_mempool_ops_free(struct rte_mempool *mp);
796 
814 int
815 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
816  void *pool_config);
817 
828 int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
829 
835 #define MEMPOOL_REGISTER_OPS(ops) \
836  RTE_INIT(mp_hdlr_init_##ops) \
837  { \
838  rte_mempool_register_ops(&ops); \
839  }
840 
846 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
847  void *opaque, void *obj, unsigned obj_idx);
848 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
849 
855 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
856  void *opaque, struct rte_mempool_memhdr *memhdr,
857  unsigned mem_idx);
858 
865 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
866 
946 struct rte_mempool *
947 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
948  unsigned cache_size, unsigned private_data_size,
949  rte_mempool_ctor_t *mp_init, void *mp_init_arg,
950  rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
951  int socket_id, unsigned flags);
952 
987 struct rte_mempool *
988 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
989  unsigned cache_size, unsigned private_data_size,
990  int socket_id, unsigned flags);
1001 void
1002 rte_mempool_free(struct rte_mempool *mp);
1003 
1031 int rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
1032  rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
1033  void *opaque);
1034 
1059 int
1060 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
1061  size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
1062  void *opaque);
1063 
1078 
1092 int rte_mempool_populate_anon(struct rte_mempool *mp);
1093 
1109 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
1110  rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
1111 
1127 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
1128  rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
1129 
1138 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
1139 
1154 struct rte_mempool_cache *
1155 rte_mempool_cache_create(uint32_t size, int socket_id);
1156 
1163 void
1165 
1176 static __rte_always_inline struct rte_mempool_cache *
1177 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1178 {
1179  if (mp->cache_size == 0)
1180  return NULL;
1181 
1182  if (lcore_id >= RTE_MAX_LCORE)
1183  return NULL;
1184 
1185  return &mp->local_cache[lcore_id];
1186 }
1187 
1196 static __rte_always_inline void
1198  struct rte_mempool *mp)
1199 {
1200  if (cache == NULL)
1201  cache = rte_mempool_default_cache(mp, rte_lcore_id());
1202  if (cache == NULL || cache->len == 0)
1203  return;
1204  rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
1205  cache->len = 0;
1206 }
1207 
1220 static __rte_always_inline void
1221 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1222  unsigned int n, struct rte_mempool_cache *cache)
1223 {
1224  void **cache_objs;
1225 
1226  /* increment stat now, adding in mempool always success */
1227  __MEMPOOL_STAT_ADD(mp, put, n);
1228 
1229  /* No cache provided or if put would overflow mem allocated for cache */
1230  if (unlikely(cache == NULL || n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1231  goto ring_enqueue;
1232 
1233  cache_objs = &cache->objs[cache->len];
1234 
1235  /*
1236  * The cache follows the following algorithm
1237  * 1. Add the objects to the cache
1238  * 2. Anything greater than the cache min value (if it crosses the
1239  * cache flush threshold) is flushed to the ring.
1240  */
1241 
1242  /* Add elements back into the cache */
1243  rte_memcpy(&cache_objs[0], obj_table, sizeof(void *) * n);
1244 
1245  cache->len += n;
1246 
1247  if (cache->len >= cache->flushthresh) {
1248  rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1249  cache->len - cache->size);
1250  cache->len = cache->size;
1251  }
1252 
1253  return;
1254 
1255 ring_enqueue:
1256 
1257  /* push remaining objects in ring */
1258 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1259  if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1260  rte_panic("cannot put objects in mempool\n");
1261 #else
1262  rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1263 #endif
1264 }
1265 
1266 
1279 static __rte_always_inline void
1280 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1281  unsigned int n, struct rte_mempool_cache *cache)
1282 {
1283  __mempool_check_cookies(mp, obj_table, n, 0);
1284  __mempool_generic_put(mp, obj_table, n, cache);
1285 }
1286 
1301 static __rte_always_inline void
1302 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1303  unsigned int n)
1304 {
1305  struct rte_mempool_cache *cache;
1306  cache = rte_mempool_default_cache(mp, rte_lcore_id());
1307  rte_mempool_generic_put(mp, obj_table, n, cache);
1308 }
1309 
1322 static __rte_always_inline void
1323 rte_mempool_put(struct rte_mempool *mp, void *obj)
1324 {
1325  rte_mempool_put_bulk(mp, &obj, 1);
1326 }
1327 
1342 static __rte_always_inline int
1343 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1344  unsigned int n, struct rte_mempool_cache *cache)
1345 {
1346  int ret;
1347  uint32_t index, len;
1348  void **cache_objs;
1349 
1350  /* No cache provided or cannot be satisfied from cache */
1351  if (unlikely(cache == NULL || n >= cache->size))
1352  goto ring_dequeue;
1353 
1354  cache_objs = cache->objs;
1355 
1356  /* Can this be satisfied from the cache? */
1357  if (cache->len < n) {
1358  /* No. Backfill the cache first, and then fill from it */
1359  uint32_t req = n + (cache->size - cache->len);
1360 
1361  /* How many do we require i.e. number to fill the cache + the request */
1362  ret = rte_mempool_ops_dequeue_bulk(mp,
1363  &cache->objs[cache->len], req);
1364  if (unlikely(ret < 0)) {
1365  /*
1366  * In the off chance that we are buffer constrained,
1367  * where we are not able to allocate cache + n, go to
1368  * the ring directly. If that fails, we are truly out of
1369  * buffers.
1370  */
1371  goto ring_dequeue;
1372  }
1373 
1374  cache->len += req;
1375  }
1376 
1377  /* Now fill in the response ... */
1378  for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1379  *obj_table = cache_objs[len];
1380 
1381  cache->len -= n;
1382 
1383  __MEMPOOL_STAT_ADD(mp, get_success, n);
1384 
1385  return 0;
1386 
1387 ring_dequeue:
1388 
1389  /* get remaining objects from ring */
1390  ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1391 
1392  if (ret < 0)
1393  __MEMPOOL_STAT_ADD(mp, get_fail, n);
1394  else
1395  __MEMPOOL_STAT_ADD(mp, get_success, n);
1396 
1397  return ret;
1398 }
1399 
1420 static __rte_always_inline int
1421 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1422  unsigned int n, struct rte_mempool_cache *cache)
1423 {
1424  int ret;
1425  ret = __mempool_generic_get(mp, obj_table, n, cache);
1426  if (ret == 0)
1427  __mempool_check_cookies(mp, obj_table, n, 1);
1428  return ret;
1429 }
1430 
1453 static __rte_always_inline int
1454 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
1455 {
1456  struct rte_mempool_cache *cache;
1457  cache = rte_mempool_default_cache(mp, rte_lcore_id());
1458  return rte_mempool_generic_get(mp, obj_table, n, cache);
1459 }
1460 
1481 static __rte_always_inline int
1482 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1483 {
1484  return rte_mempool_get_bulk(mp, obj_p, 1);
1485 }
1486 
1511 static __rte_always_inline int
1512 __rte_experimental
1514  void **first_obj_table, unsigned int n)
1515 {
1516  int ret;
1517 
1518  ret = rte_mempool_ops_dequeue_contig_blocks(mp, first_obj_table, n);
1519  if (ret == 0) {
1520  __MEMPOOL_CONTIG_BLOCKS_STAT_ADD(mp, get_success, n);
1521  __mempool_contig_blocks_check_cookies(mp, first_obj_table, n,
1522  1);
1523  } else {
1524  __MEMPOOL_CONTIG_BLOCKS_STAT_ADD(mp, get_fail, n);
1525  }
1526 
1527  return ret;
1528 }
1529 
1542 unsigned int rte_mempool_avail_count(const struct rte_mempool *mp);
1543 
1556 unsigned int
1557 rte_mempool_in_use_count(const struct rte_mempool *mp);
1558 
1572 static inline int
1574 {
1575  return !!(rte_mempool_avail_count(mp) == mp->size);
1576 }
1577 
1591 static inline int
1593 {
1594  return !!(rte_mempool_avail_count(mp) == 0);
1595 }
1596 
1607 static inline rte_iova_t
1608 rte_mempool_virt2iova(const void *elt)
1609 {
1610  const struct rte_mempool_objhdr *hdr;
1611  hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1612  sizeof(*hdr));
1613  return hdr->iova;
1614 }
1615 
1626 void rte_mempool_audit(struct rte_mempool *mp);
1627 
1636 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1637 {
1638  return (char *)mp +
1640 }
1641 
1648 void rte_mempool_list_dump(FILE *f);
1649 
1662 struct rte_mempool *rte_mempool_lookup(const char *name);
1663 
1681 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1682  struct rte_mempool_objsz *sz);
1683 
1692 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1693  void *arg);
1694 
1695 #ifdef __cplusplus
1696 }
1697 #endif
1698 
1699 #endif /* _RTE_MEMPOOL_H_ */