enforce experimental tag at beginning of declarations
[dpdk.git] / lib / librte_eal / common / include / rte_memory.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_MEMORY_H_
6 #define _RTE_MEMORY_H_
7
8 /**
9  * @file
10  *
11  * Memory-related RTE API.
12  */
13
14 #include <stdint.h>
15 #include <stddef.h>
16 #include <stdio.h>
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #include <rte_common.h>
23 #include <rte_compat.h>
24 #include <rte_config.h>
25
26 /* forward declaration for pointers */
27 struct rte_memseg_list;
28
29 __extension__
30 enum rte_page_sizes {
31         RTE_PGSIZE_4K    = 1ULL << 12,
32         RTE_PGSIZE_64K   = 1ULL << 16,
33         RTE_PGSIZE_256K  = 1ULL << 18,
34         RTE_PGSIZE_2M    = 1ULL << 21,
35         RTE_PGSIZE_16M   = 1ULL << 24,
36         RTE_PGSIZE_256M  = 1ULL << 28,
37         RTE_PGSIZE_512M  = 1ULL << 29,
38         RTE_PGSIZE_1G    = 1ULL << 30,
39         RTE_PGSIZE_4G    = 1ULL << 32,
40         RTE_PGSIZE_16G   = 1ULL << 34,
41 };
42
43 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
44 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
45
46 #define RTE_CACHE_LINE_ROUNDUP(size) \
47         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
48 /**< Return the first cache-aligned value greater or equal to size. */
49
50 /**< Cache line size in terms of log2 */
51 #if RTE_CACHE_LINE_SIZE == 64
52 #define RTE_CACHE_LINE_SIZE_LOG2 6
53 #elif RTE_CACHE_LINE_SIZE == 128
54 #define RTE_CACHE_LINE_SIZE_LOG2 7
55 #else
56 #error "Unsupported cache line size"
57 #endif
58
59 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
60
61 /**
62  * Force alignment to cache line.
63  */
64 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
65
66 /**
67  * Force minimum cache line alignment.
68  */
69 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
70
71 typedef uint64_t phys_addr_t; /**< Physical address. */
72 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
73 /**
74  * IO virtual address type.
75  * When the physical addressing mode (IOVA as PA) is in use,
76  * the translation from an IO virtual address (IOVA) to a physical address
77  * is a direct mapping, i.e. the same value.
78  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
79  */
80 typedef uint64_t rte_iova_t;
81 #define RTE_BAD_IOVA ((rte_iova_t)-1)
82
83 /**
84  * Physical memory segment descriptor.
85  */
86 #define RTE_MEMSEG_FLAG_DO_NOT_FREE (1 << 0)
87 /**< Prevent this segment from being freed back to the OS. */
88 struct rte_memseg {
89         RTE_STD_C11
90         union {
91                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
92                 rte_iova_t iova;        /**< Start IO address. */
93         };
94         RTE_STD_C11
95         union {
96                 void *addr;         /**< Start virtual address. */
97                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
98         };
99         size_t len;               /**< Length of the segment. */
100         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
101         int32_t socket_id;          /**< NUMA socket ID. */
102         uint32_t nchannel;          /**< Number of channels. */
103         uint32_t nrank;             /**< Number of ranks. */
104         uint32_t flags;             /**< Memseg-specific flags */
105 } __rte_packed;
106
107 /**
108  * Lock page in physical memory and prevent from swapping.
109  *
110  * @param virt
111  *   The virtual address.
112  * @return
113  *   0 on success, negative on error.
114  */
115 int rte_mem_lock_page(const void *virt);
116
117 /**
118  * Get physical address of any mapped virtual address in the current process.
119  * It is found by browsing the /proc/self/pagemap special file.
120  * The page must be locked.
121  *
122  * @param virt
123  *   The virtual address.
124  * @return
125  *   The physical address or RTE_BAD_IOVA on error.
126  */
127 phys_addr_t rte_mem_virt2phy(const void *virt);
128
129 /**
130  * Get IO virtual address of any mapped virtual address in the current process.
131  *
132  * @param virt
133  *   The virtual address.
134  * @return
135  *   The IO address or RTE_BAD_IOVA on error.
136  */
137 rte_iova_t rte_mem_virt2iova(const void *virt);
138
139 /**
140  * Get virtual memory address corresponding to iova address.
141  *
142  * @note This function read-locks the memory hotplug subsystem, and thus cannot
143  *       be used within memory-related callback functions.
144  *
145  * @param iova
146  *   The iova address.
147  * @return
148  *   Virtual address corresponding to iova address (or NULL if address does not
149  *   exist within DPDK memory map).
150  */
151 __rte_experimental
152 void *
153 rte_mem_iova2virt(rte_iova_t iova);
154
155 /**
156  * Get memseg to which a particular virtual address belongs.
157  *
158  * @param virt
159  *   The virtual address.
160  * @param msl
161  *   The memseg list in which to look up based on ``virt`` address
162  *   (can be NULL).
163  * @return
164  *   Memseg pointer on success, or NULL on error.
165  */
166 __rte_experimental
167 struct rte_memseg *
168 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
169
170 /**
171  * Get memseg list corresponding to virtual memory address.
172  *
173  * @param virt
174  *   The virtual address.
175  * @return
176  *   Memseg list to which this virtual address belongs to.
177  */
178 __rte_experimental
179 struct rte_memseg_list *
180 rte_mem_virt2memseg_list(const void *virt);
181
182 /**
183  * Memseg walk function prototype.
184  *
185  * Returning 0 will continue walk
186  * Returning 1 will stop the walk
187  * Returning -1 will stop the walk and report error
188  */
189 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
190                 const struct rte_memseg *ms, void *arg);
191
192 /**
193  * Memseg contig walk function prototype. This will trigger a callback on every
194  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
195  * callback call will be [``ms->addr``, ``ms->addr + len``).
196  *
197  * Returning 0 will continue walk
198  * Returning 1 will stop the walk
199  * Returning -1 will stop the walk and report error
200  */
201 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
202                 const struct rte_memseg *ms, size_t len, void *arg);
203
204 /**
205  * Memseg list walk function prototype. This will trigger a callback on every
206  * allocated memseg list.
207  *
208  * Returning 0 will continue walk
209  * Returning 1 will stop the walk
210  * Returning -1 will stop the walk and report error
211  */
212 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
213                 void *arg);
214
215 /**
216  * Walk list of all memsegs.
217  *
218  * @note This function read-locks the memory hotplug subsystem, and thus cannot
219  *       be used within memory-related callback functions.
220  *
221  * @note This function will also walk through externally allocated segments. It
222  *       is up to the user to decide whether to skip through these segments.
223  *
224  * @param func
225  *   Iterator function
226  * @param arg
227  *   Argument passed to iterator
228  * @return
229  *   0 if walked over the entire list
230  *   1 if stopped by the user
231  *   -1 if user function reported error
232  */
233 __rte_experimental
234 int
235 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
236
237 /**
238  * Walk each VA-contiguous area.
239  *
240  * @note This function read-locks the memory hotplug subsystem, and thus cannot
241  *       be used within memory-related callback functions.
242  *
243  * @note This function will also walk through externally allocated segments. It
244  *       is up to the user to decide whether to skip through these segments.
245  *
246  * @param func
247  *   Iterator function
248  * @param arg
249  *   Argument passed to iterator
250  * @return
251  *   0 if walked over the entire list
252  *   1 if stopped by the user
253  *   -1 if user function reported error
254  */
255 __rte_experimental
256 int
257 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
258
259 /**
260  * Walk each allocated memseg list.
261  *
262  * @note This function read-locks the memory hotplug subsystem, and thus cannot
263  *       be used within memory-related callback functions.
264  *
265  * @note This function will also walk through externally allocated segments. It
266  *       is up to the user to decide whether to skip through these segments.
267  *
268  * @param func
269  *   Iterator function
270  * @param arg
271  *   Argument passed to iterator
272  * @return
273  *   0 if walked over the entire list
274  *   1 if stopped by the user
275  *   -1 if user function reported error
276  */
277 __rte_experimental
278 int
279 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
280
281 /**
282  * Walk list of all memsegs without performing any locking.
283  *
284  * @note This function does not perform any locking, and is only safe to call
285  *       from within memory-related callback functions.
286  *
287  * @param func
288  *   Iterator function
289  * @param arg
290  *   Argument passed to iterator
291  * @return
292  *   0 if walked over the entire list
293  *   1 if stopped by the user
294  *   -1 if user function reported error
295  */
296 __rte_experimental
297 int
298 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
299
300 /**
301  * Walk each VA-contiguous area without performing any locking.
302  *
303  * @note This function does not perform any locking, and is only safe to call
304  *       from within memory-related callback functions.
305  *
306  * @param func
307  *   Iterator function
308  * @param arg
309  *   Argument passed to iterator
310  * @return
311  *   0 if walked over the entire list
312  *   1 if stopped by the user
313  *   -1 if user function reported error
314  */
315 __rte_experimental
316 int
317 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
318
319 /**
320  * Walk each allocated memseg list without performing any locking.
321  *
322  * @note This function does not perform any locking, and is only safe to call
323  *       from within memory-related callback functions.
324  *
325  * @param func
326  *   Iterator function
327  * @param arg
328  *   Argument passed to iterator
329  * @return
330  *   0 if walked over the entire list
331  *   1 if stopped by the user
332  *   -1 if user function reported error
333  */
334 __rte_experimental
335 int
336 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
337
338 /**
339  * Return file descriptor associated with a particular memseg (if available).
340  *
341  * @note This function read-locks the memory hotplug subsystem, and thus cannot
342  *       be used within memory-related callback functions.
343  *
344  * @note This returns an internal file descriptor. Performing any operations on
345  *       this file descriptor is inherently dangerous, so it should be treated
346  *       as read-only for all intents and purposes.
347  *
348  * @param ms
349  *   A pointer to memseg for which to get file descriptor.
350  *
351  * @return
352  *   Valid file descriptor in case of success.
353  *   -1 in case of error, with ``rte_errno`` set to the following values:
354  *     - EINVAL  - ``ms`` pointer was NULL or did not point to a valid memseg
355  *     - ENODEV  - ``ms`` fd is not available
356  *     - ENOENT  - ``ms`` is an unused segment
357  *     - ENOTSUP - segment fd's are not supported
358  */
359 __rte_experimental
360 int
361 rte_memseg_get_fd(const struct rte_memseg *ms);
362
363 /**
364  * Return file descriptor associated with a particular memseg (if available).
365  *
366  * @note This function does not perform any locking, and is only safe to call
367  *       from within memory-related callback functions.
368  *
369  * @note This returns an internal file descriptor. Performing any operations on
370  *       this file descriptor is inherently dangerous, so it should be treated
371  *       as read-only for all intents and purposes.
372  *
373  * @param ms
374  *   A pointer to memseg for which to get file descriptor.
375  *
376  * @return
377  *   Valid file descriptor in case of success.
378  *   -1 in case of error, with ``rte_errno`` set to the following values:
379  *     - EINVAL  - ``ms`` pointer was NULL or did not point to a valid memseg
380  *     - ENODEV  - ``ms`` fd is not available
381  *     - ENOENT  - ``ms`` is an unused segment
382  *     - ENOTSUP - segment fd's are not supported
383  */
384 __rte_experimental
385 int
386 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
387
388 /**
389  * Get offset into segment file descriptor associated with a particular memseg
390  * (if available).
391  *
392  * @note This function read-locks the memory hotplug subsystem, and thus cannot
393  *       be used within memory-related callback functions.
394  *
395  * @param ms
396  *   A pointer to memseg for which to get file descriptor.
397  * @param offset
398  *   A pointer to offset value where the result will be stored.
399  *
400  * @return
401  *   Valid file descriptor in case of success.
402  *   -1 in case of error, with ``rte_errno`` set to the following values:
403  *     - EINVAL  - ``ms`` pointer was NULL or did not point to a valid memseg
404  *     - EINVAL  - ``offset`` pointer was NULL
405  *     - ENODEV  - ``ms`` fd is not available
406  *     - ENOENT  - ``ms`` is an unused segment
407  *     - ENOTSUP - segment fd's are not supported
408  */
409 __rte_experimental
410 int
411 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
412
413 /**
414  * Get offset into segment file descriptor associated with a particular memseg
415  * (if available).
416  *
417  * @note This function does not perform any locking, and is only safe to call
418  *       from within memory-related callback functions.
419  *
420  * @param ms
421  *   A pointer to memseg for which to get file descriptor.
422  * @param offset
423  *   A pointer to offset value where the result will be stored.
424  *
425  * @return
426  *   Valid file descriptor in case of success.
427  *   -1 in case of error, with ``rte_errno`` set to the following values:
428  *     - EINVAL  - ``ms`` pointer was NULL or did not point to a valid memseg
429  *     - EINVAL  - ``offset`` pointer was NULL
430  *     - ENODEV  - ``ms`` fd is not available
431  *     - ENOENT  - ``ms`` is an unused segment
432  *     - ENOTSUP - segment fd's are not supported
433  */
434 __rte_experimental
435 int
436 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
437                 size_t *offset);
438
439 /**
440  * @warning
441  * @b EXPERIMENTAL: this API may change without prior notice
442  *
443  * Register external memory chunk with DPDK.
444  *
445  * @note Using this API is mutually exclusive with ``rte_malloc`` family of
446  *   API's.
447  *
448  * @note This API will not perform any DMA mapping. It is expected that user
449  *   will do that themselves.
450  *
451  * @note Before accessing this memory in other processes, it needs to be
452  *   attached in each of those processes by calling ``rte_extmem_attach`` in
453  *   each other process.
454  *
455  * @param va_addr
456  *   Start of virtual area to register. Must be aligned by ``page_sz``.
457  * @param len
458  *   Length of virtual area to register. Must be aligned by ``page_sz``.
459  * @param iova_addrs
460  *   Array of page IOVA addresses corresponding to each page in this memory
461  *   area. Can be NULL, in which case page IOVA addresses will be set to
462  *   RTE_BAD_IOVA.
463  * @param n_pages
464  *   Number of elements in the iova_addrs array. Ignored if  ``iova_addrs``
465  *   is NULL.
466  * @param page_sz
467  *   Page size of the underlying memory
468  *
469  * @return
470  *   - 0 on success
471  *   - -1 in case of error, with rte_errno set to one of the following:
472  *     EINVAL - one of the parameters was invalid
473  *     EEXIST - memory chunk is already registered
474  *     ENOSPC - no more space in internal config to store a new memory chunk
475  */
476 __rte_experimental
477 int
478 rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[],
479                 unsigned int n_pages, size_t page_sz);
480
481 /**
482  * @warning
483  * @b EXPERIMENTAL: this API may change without prior notice
484  *
485  * Unregister external memory chunk with DPDK.
486  *
487  * @note Using this API is mutually exclusive with ``rte_malloc`` family of
488  *   API's.
489  *
490  * @note This API will not perform any DMA unmapping. It is expected that user
491  *   will do that themselves.
492  *
493  * @note Before calling this function, all other processes must call
494  *   ``rte_extmem_detach`` to detach from the memory area.
495  *
496  * @param va_addr
497  *   Start of virtual area to unregister
498  * @param len
499  *   Length of virtual area to unregister
500  *
501  * @return
502  *   - 0 on success
503  *   - -1 in case of error, with rte_errno set to one of the following:
504  *     EINVAL - one of the parameters was invalid
505  *     ENOENT - memory chunk was not found
506  */
507 __rte_experimental
508 int
509 rte_extmem_unregister(void *va_addr, size_t len);
510
511 /**
512  * @warning
513  * @b EXPERIMENTAL: this API may change without prior notice
514  *
515  * Attach to external memory chunk registered in another process.
516  *
517  * @note Using this API is mutually exclusive with ``rte_malloc`` family of
518  *   API's.
519  *
520  * @note This API will not perform any DMA mapping. It is expected that user
521  *   will do that themselves.
522  *
523  * @param va_addr
524  *   Start of virtual area to register
525  * @param len
526  *   Length of virtual area to register
527  *
528  * @return
529  *   - 0 on success
530  *   - -1 in case of error, with rte_errno set to one of the following:
531  *     EINVAL - one of the parameters was invalid
532  *     ENOENT - memory chunk was not found
533  */
534 __rte_experimental
535 int
536 rte_extmem_attach(void *va_addr, size_t len);
537
538 /**
539  * @warning
540  * @b EXPERIMENTAL: this API may change without prior notice
541  *
542  * Detach from external memory chunk registered in another process.
543  *
544  * @note Using this API is mutually exclusive with ``rte_malloc`` family of
545  *   API's.
546  *
547  * @note This API will not perform any DMA unmapping. It is expected that user
548  *   will do that themselves.
549  *
550  * @param va_addr
551  *   Start of virtual area to unregister
552  * @param len
553  *   Length of virtual area to unregister
554  *
555  * @return
556  *   - 0 on success
557  *   - -1 in case of error, with rte_errno set to one of the following:
558  *     EINVAL - one of the parameters was invalid
559  *     ENOENT - memory chunk was not found
560  */
561 __rte_experimental
562 int
563 rte_extmem_detach(void *va_addr, size_t len);
564
565 /**
566  * Dump the physical memory layout to a file.
567  *
568  * @note This function read-locks the memory hotplug subsystem, and thus cannot
569  *       be used within memory-related callback functions.
570  *
571  * @param f
572  *   A pointer to a file for output
573  */
574 void rte_dump_physmem_layout(FILE *f);
575
576 /**
577  * Get the total amount of available physical memory.
578  *
579  * @note This function read-locks the memory hotplug subsystem, and thus cannot
580  *       be used within memory-related callback functions.
581  *
582  * @return
583  *    The total amount of available physical memory in bytes.
584  */
585 uint64_t rte_eal_get_physmem_size(void);
586
587 /**
588  * Get the number of memory channels.
589  *
590  * @return
591  *   The number of memory channels on the system. The value is 0 if unknown
592  *   or not the same on all devices.
593  */
594 unsigned rte_memory_get_nchannel(void);
595
596 /**
597  * Get the number of memory ranks.
598  *
599  * @return
600  *   The number of memory ranks on the system. The value is 0 if unknown or
601  *   not the same on all devices.
602  */
603 unsigned rte_memory_get_nrank(void);
604
605 /**
606  * @warning
607  * @b EXPERIMENTAL: this API may change without prior notice
608  *
609  * Check if all currently allocated memory segments are compliant with
610  * supplied DMA address width.
611  *
612  *  @param maskbits
613  *    Address width to check against.
614  */
615 __rte_experimental
616 int rte_mem_check_dma_mask(uint8_t maskbits);
617
618 /**
619  * @warning
620  * @b EXPERIMENTAL: this API may change without prior notice
621  *
622  * Check if all currently allocated memory segments are compliant with
623  * supplied DMA address width. This function will use
624  * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying
625  * memory_hotplug_lock will not be acquired avoiding deadlock during
626  * memory initialization.
627  *
628  * This function is just for EAL core memory internal use. Drivers should
629  * use the previous rte_mem_check_dma_mask.
630  *
631  *  @param maskbits
632  *    Address width to check against.
633  */
634 __rte_experimental
635 int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits);
636
637 /**
638  * @warning
639  * @b EXPERIMENTAL: this API may change without prior notice
640  *
641  *  Set dma mask to use once memory initialization is done. Previous functions
642  *  rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be
643  *  used safely until memory has been initialized.
644  */
645 __rte_experimental
646 void rte_mem_set_dma_mask(uint8_t maskbits);
647
648 /**
649  * Drivers based on uio will not load unless physical
650  * addresses are obtainable. It is only possible to get
651  * physical addresses when running as a privileged user.
652  *
653  * @return
654  *   1 if the system is able to obtain physical addresses.
655  *   0 if using DMA addresses through an IOMMU.
656  */
657 int rte_eal_using_phys_addrs(void);
658
659
660 /**
661  * Enum indicating which kind of memory event has happened. Used by callbacks to
662  * distinguish between memory allocations and deallocations.
663  */
664 enum rte_mem_event {
665         RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
666         RTE_MEM_EVENT_FREE,      /**< Deallocation event. */
667 };
668 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
669 /**< maximum length of callback name */
670
671 /**
672  * Function typedef used to register callbacks for memory events.
673  */
674 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
675                 const void *addr, size_t len, void *arg);
676
677 /**
678  * Function used to register callbacks for memory events.
679  *
680  * @note callbacks will happen while memory hotplug subsystem is write-locked,
681  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
682  *       deadlock when called from within such callbacks.
683  *
684  * @note mem event callbacks not being supported is an expected error condition,
685  *       so user code needs to handle this situation. In these cases, return
686  *       value will be -1, and rte_errno will be set to ENOTSUP.
687  *
688  * @param name
689  *   Name associated with specified callback to be added to the list.
690  *
691  * @param clb
692  *   Callback function pointer.
693  *
694  * @param arg
695  *   Argument to pass to the callback.
696  *
697  * @return
698  *   0 on successful callback register
699  *   -1 on unsuccessful callback register, with rte_errno value indicating
700  *   reason for failure.
701  */
702 __rte_experimental
703 int
704 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
705                 void *arg);
706
707 /**
708  * Function used to unregister callbacks for memory events.
709  *
710  * @param name
711  *   Name associated with specified callback to be removed from the list.
712  *
713  * @param arg
714  *   Argument to look for among callbacks with specified callback name.
715  *
716  * @return
717  *   0 on successful callback unregister
718  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
719  *   reason for failure.
720  */
721 __rte_experimental
722 int
723 rte_mem_event_callback_unregister(const char *name, void *arg);
724
725
726 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
727 /**< maximum length of alloc validator name */
728 /**
729  * Function typedef used to register memory allocation validation callbacks.
730  *
731  * Returning 0 will allow allocation attempt to continue. Returning -1 will
732  * prevent allocation from succeeding.
733  */
734 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
735                 size_t cur_limit, size_t new_len);
736
737 /**
738  * @brief Register validator callback for memory allocations.
739  *
740  * Callbacks registered by this function will be called right before memory
741  * allocator is about to trigger allocation of more pages from the system if
742  * said allocation will bring total memory usage above specified limit on
743  * specified socket. User will be able to cancel pending allocation if callback
744  * returns -1.
745  *
746  * @note callbacks will happen while memory hotplug subsystem is write-locked,
747  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
748  *       deadlock when called from within such callbacks.
749  *
750  * @note validator callbacks not being supported is an expected error condition,
751  *       so user code needs to handle this situation. In these cases, return
752  *       value will be -1, and rte_errno will be set to ENOTSUP.
753  *
754  * @param name
755  *   Name associated with specified callback to be added to the list.
756  *
757  * @param clb
758  *   Callback function pointer.
759  *
760  * @param socket_id
761  *   Socket ID on which to watch for allocations.
762  *
763  * @param limit
764  *   Limit above which to trigger callbacks.
765  *
766  * @return
767  *   0 on successful callback register
768  *   -1 on unsuccessful callback register, with rte_errno value indicating
769  *   reason for failure.
770  */
771 __rte_experimental
772 int
773 rte_mem_alloc_validator_register(const char *name,
774                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
775
776 /**
777  * @brief Unregister validator callback for memory allocations.
778  *
779  * @param name
780  *   Name associated with specified callback to be removed from the list.
781  *
782  * @param socket_id
783  *   Socket ID on which to watch for allocations.
784  *
785  * @return
786  *   0 on successful callback unregister
787  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
788  *   reason for failure.
789  */
790 __rte_experimental
791 int
792 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
793
794 #ifdef __cplusplus
795 }
796 #endif
797
798 #endif /* _RTE_MEMORY_H_ */