36bcc0b5a49227580c40c6a1d0bb16ca468cef05
[dpdk.git] / lib / eal / common / eal_private.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #ifndef _EAL_PRIVATE_H_
6 #define _EAL_PRIVATE_H_
7
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12
13 #include <rte_dev.h>
14 #include <rte_lcore.h>
15 #include <rte_memory.h>
16
17 #include "eal_internal_cfg.h"
18
19 /**
20  * Structure storing internal configuration (per-lcore)
21  */
22 struct lcore_config {
23         pthread_t thread_id;       /**< pthread identifier */
24         int pipe_main2worker[2];   /**< communication pipe with main */
25         int pipe_worker2main[2];   /**< communication pipe with main */
26
27         lcore_function_t * volatile f; /**< function to call */
28         void * volatile arg;       /**< argument of function */
29         volatile int ret;          /**< return value of function */
30
31         volatile enum rte_lcore_state_t state; /**< lcore state */
32         unsigned int socket_id;    /**< physical socket id for this lcore */
33         unsigned int core_id;      /**< core number on socket for this lcore */
34         int core_index;            /**< relative index, starting from 0 */
35         uint8_t core_role;         /**< role of core eg: OFF, RTE, SERVICE */
36
37         rte_cpuset_t cpuset;       /**< cpu set which the lcore affinity to */
38 };
39
40 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
41
42 /**
43  * The global RTE configuration structure.
44  */
45 struct rte_config {
46         uint32_t main_lcore;         /**< Id of the main lcore */
47         uint32_t lcore_count;        /**< Number of available logical cores. */
48         uint32_t numa_node_count;    /**< Number of detected NUMA nodes. */
49         uint32_t numa_nodes[RTE_MAX_NUMA_NODES]; /**< List of detected NUMA nodes. */
50         uint32_t service_lcore_count;/**< Number of available service cores. */
51         enum rte_lcore_role_t lcore_role[RTE_MAX_LCORE]; /**< State of cores. */
52
53         /** Primary or secondary configuration */
54         enum rte_proc_type_t process_type;
55
56         /** PA or VA mapping mode */
57         enum rte_iova_mode iova_mode;
58
59         /**
60          * Pointer to memory configuration, which may be shared across multiple
61          * DPDK instances
62          */
63         struct rte_mem_config *mem_config;
64 } __rte_packed;
65
66 /**
67  * Get the global configuration structure.
68  *
69  * @return
70  *   A pointer to the global configuration structure.
71  */
72 struct rte_config *rte_eal_get_configuration(void);
73
74 /**
75  * Initialize the memzone subsystem (private to eal).
76  *
77  * @return
78  *   - 0 on success
79  *   - Negative on error
80  */
81 int rte_eal_memzone_init(void);
82
83 /**
84  * Fill configuration with number of physical and logical processors
85  *
86  * This function is private to EAL.
87  *
88  * Parse /proc/cpuinfo to get the number of physical and logical
89  * processors on the machine.
90  *
91  * @return
92  *   0 on success, negative on error
93  */
94 int rte_eal_cpu_init(void);
95
96 /**
97  * Create memseg lists
98  *
99  * This function is private to EAL.
100  *
101  * Preallocate virtual memory.
102  *
103  * @return
104  *   0 on success, negative on error
105  */
106 int rte_eal_memseg_init(void);
107
108 /**
109  * Map memory
110  *
111  * This function is private to EAL.
112  *
113  * Fill configuration structure with these infos, and return 0 on success.
114  *
115  * @return
116  *   0 on success, negative on error
117  */
118 int rte_eal_memory_init(void);
119
120 /**
121  * Configure timers
122  *
123  * This function is private to EAL.
124  *
125  * Mmap memory areas used by HPET (high precision event timer) that will
126  * provide our time reference, and configure the TSC frequency also for it
127  * to be used as a reference.
128  *
129  * @return
130  *   0 on success, negative on error
131  */
132 int rte_eal_timer_init(void);
133
134 /**
135  * Init tail queues for non-EAL library structures. This is to allow
136  * the rings, mempools, etc. lists to be shared among multiple processes
137  *
138  * This function is private to EAL
139  *
140  * @return
141  *    0 on success, negative on error
142  */
143 int rte_eal_tailqs_init(void);
144
145 /**
146  * Init interrupt handling.
147  *
148  * This function is private to EAL.
149  *
150  * @return
151  *  0 on success, negative on error
152  */
153 int rte_eal_intr_init(void);
154
155 /**
156  * Init alarm mechanism. This is to allow a callback be called after
157  * specific time.
158  *
159  * This function is private to EAL.
160  *
161  * @return
162  *  0 on success, negative on error
163  */
164 int rte_eal_alarm_init(void);
165
166 /**
167  * Alarm mechanism cleanup.
168  *
169  * This function is private to EAL.
170  *
171  * @return
172  *  0 on success, negative on error
173  */
174 void rte_eal_alarm_cleanup(void);
175
176 /**
177  * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
178  * etc.) loaded.
179  *
180  * @param module_name
181  *      The module's name which need to be checked
182  *
183  * @return
184  *      -1 means some error happens(NULL pointer or open failure)
185  *      0  means the module not loaded
186  *      1  means the module loaded
187  */
188 int rte_eal_check_module(const char *module_name);
189
190 /**
191  * Memory reservation flags.
192  */
193 enum eal_mem_reserve_flags {
194         /**
195          * Reserve hugepages. May be unsupported by some platforms.
196          */
197         EAL_RESERVE_HUGEPAGES = 1 << 0,
198         /**
199          * Force reserving memory at the requested address.
200          * This can be a destructive action depending on the implementation.
201          *
202          * @see RTE_MAP_FORCE_ADDRESS for description of possible consequences
203          *      (although implementations are not required to use it).
204          */
205         EAL_RESERVE_FORCE_ADDRESS = 1 << 1
206 };
207
208 /**
209  * Get virtual area of specified size from the OS.
210  *
211  * This function is private to the EAL.
212  *
213  * @param requested_addr
214  *   Address where to request address space.
215  * @param size
216  *   Size of requested area.
217  * @param page_sz
218  *   Page size on which to align requested virtual area.
219  * @param flags
220  *   EAL_VIRTUAL_AREA_* flags.
221  * @param reserve_flags
222  *   Extra flags passed directly to eal_mem_reserve().
223  *
224  * @return
225  *   Virtual area address if successful.
226  *   NULL if unsuccessful.
227  */
228
229 #define EAL_VIRTUAL_AREA_ADDR_IS_HINT (1 << 0)
230 /**< don't fail if cannot get exact requested address. */
231 #define EAL_VIRTUAL_AREA_ALLOW_SHRINK (1 << 1)
232 /**< try getting smaller sized (decrement by page size) virtual areas if cannot
233  * get area of requested size.
234  */
235 #define EAL_VIRTUAL_AREA_UNMAP (1 << 2)
236 /**< immediately unmap reserved virtual area. */
237 void *
238 eal_get_virtual_area(void *requested_addr, size_t *size,
239                 size_t page_sz, int flags, int reserve_flags);
240
241 /**
242  * Initialize a memory segment list and create its backing storage.
243  *
244  * @param msl
245  *  Memory segment list to be filled.
246  * @param name
247  *  Name for the backing storage.
248  * @param page_sz
249  *  Size of segment pages in the MSL.
250  * @param n_segs
251  *  Number of segments.
252  * @param socket_id
253  *  Socket ID. Must not be SOCKET_ID_ANY.
254  * @param heap
255  *  Mark MSL as pointing to a heap.
256  * @return
257  *  0 on success, (-1) on failure and rte_errno is set.
258  */
259 int
260 eal_memseg_list_init_named(struct rte_memseg_list *msl, const char *name,
261         uint64_t page_sz, int n_segs, int socket_id, bool heap);
262
263 /**
264  * Initialize memory segment list and create its backing storage
265  * with a name corresponding to MSL parameters.
266  *
267  * @param type_msl_idx
268  *  Index of the MSL among other MSLs of the same socket and page size.
269  *
270  * @see eal_memseg_list_init_named for remaining parameters description.
271  */
272 int
273 eal_memseg_list_init(struct rte_memseg_list *msl, uint64_t page_sz,
274         int n_segs, int socket_id, int type_msl_idx, bool heap);
275
276 /**
277  * Reserve VA space for a memory segment list
278  * previously initialized with eal_memseg_list_init().
279  *
280  * @param msl
281  *  Initialized memory segment list with page size defined.
282  * @param reserve_flags
283  *  Extra memory reservation flags. Can be 0 if unnecessary.
284  * @return
285  *  0 on success, (-1) on failure and rte_errno is set.
286  */
287 int
288 eal_memseg_list_alloc(struct rte_memseg_list *msl, int reserve_flags);
289
290 /**
291  * Populate MSL, each segment is one page long.
292  *
293  * @param msl
294  *  Initialized memory segment list with page size defined.
295  * @param addr
296  *  Starting address of list segments.
297  * @param n_segs
298  *  Number of segments to populate.
299  */
300 void
301 eal_memseg_list_populate(struct rte_memseg_list *msl, void *addr, int n_segs);
302
303 /**
304  * Distribute available memory between MSLs.
305  *
306  * @return
307  *  0 on success, (-1) on failure.
308  */
309 int
310 eal_dynmem_memseg_lists_init(void);
311
312 /**
313  * Preallocate hugepages for dynamic allocation.
314  *
315  * @return
316  *  0 on success, (-1) on failure.
317  */
318 int
319 eal_dynmem_hugepage_init(void);
320
321 /**
322  * Given the list of hugepage sizes and the number of pages thereof,
323  * calculate the best number of pages of each size to fulfill the request
324  * for RAM on each NUMA node.
325  *
326  * @param memory
327  *  Amounts of memory requested for each NUMA node of RTE_MAX_NUMA_NODES.
328  * @param hp_info
329  *  Information about hugepages of different size.
330  * @param hp_used
331  *  Receives information about used hugepages of each size.
332  * @param num_hp_info
333  *  Number of elements in hp_info and hp_used.
334  * @return
335  *  0 on success, (-1) on failure.
336  */
337 int
338 eal_dynmem_calc_num_pages_per_socket(
339                 uint64_t *memory, struct hugepage_info *hp_info,
340                 struct hugepage_info *hp_used, unsigned int num_hp_info);
341
342 /**
343  * Get cpu core_id.
344  *
345  * This function is private to the EAL.
346  */
347 unsigned eal_cpu_core_id(unsigned lcore_id);
348
349 /**
350  * Check if cpu is present.
351  *
352  * This function is private to the EAL.
353  */
354 int eal_cpu_detected(unsigned lcore_id);
355
356 /**
357  * Set TSC frequency from precise value or estimation
358  *
359  * This function is private to the EAL.
360  */
361 void set_tsc_freq(void);
362
363 /**
364  * Get precise TSC frequency from system
365  *
366  * This function is private to the EAL.
367  */
368 uint64_t get_tsc_freq(void);
369
370 /**
371  * Get TSC frequency if the architecture supports.
372  *
373  * This function is private to the EAL.
374  *
375  * @return
376  *   The number of TSC cycles in one second.
377  *   Returns zero if the architecture support is not available.
378  */
379 uint64_t get_tsc_freq_arch(void);
380
381 /**
382  * Allocate a free lcore to associate to a non-EAL thread.
383  *
384  * @return
385  *   - the id of a lcore with role ROLE_NON_EAL on success.
386  *   - RTE_MAX_LCORE if none was available or initializing was refused (see
387  *     rte_lcore_callback_register).
388  */
389 unsigned int eal_lcore_non_eal_allocate(void);
390
391 /**
392  * Release the lcore used by a non-EAL thread.
393  * Counterpart of eal_lcore_non_eal_allocate().
394  *
395  * @param lcore_id
396  *   The lcore with role ROLE_NON_EAL to release.
397  */
398 void eal_lcore_non_eal_release(unsigned int lcore_id);
399
400 /**
401  * Prepare physical memory mapping
402  * i.e. hugepages on Linux and
403  *      contigmem on BSD.
404  *
405  * This function is private to the EAL.
406  */
407 int rte_eal_hugepage_init(void);
408
409 /**
410  * Creates memory mapping in secondary process
411  * i.e. hugepages on Linux and
412  *      contigmem on BSD.
413  *
414  * This function is private to the EAL.
415  */
416 int rte_eal_hugepage_attach(void);
417
418 /**
419  * Detaches all memory mappings from a process.
420  *
421  * This function is private to the EAL.
422  */
423 int rte_eal_memory_detach(void);
424
425 /**
426  * Find a bus capable of identifying a device.
427  *
428  * @param str
429  *   A device identifier (PCI address, virtual PMD name, ...).
430  *
431  * @return
432  *   A valid bus handle if found.
433  *   NULL if no bus is able to parse this device.
434  */
435 struct rte_bus *rte_bus_find_by_device_name(const char *str);
436
437 /**
438  * Create the unix channel for primary/secondary communication.
439  *
440  * @return
441  *   0 on success;
442  *   (<0) on failure.
443  */
444 int rte_mp_channel_init(void);
445
446 /**
447  * Primary/secondary communication cleanup.
448  */
449 void rte_mp_channel_cleanup(void);
450
451 /**
452  * @internal
453  * Parse a device string and store its information in an
454  * rte_devargs structure.
455  *
456  * A device description is split by layers of abstraction of the device:
457  * bus, class and driver. Each layer will offer a set of properties that
458  * can be applied either to configure or recognize a device.
459  *
460  * This function will parse those properties and prepare the rte_devargs
461  * to be given to each layers for processing.
462  *
463  * Note: if the "data" field of the devargs points to devstr,
464  * then no dynamic allocation is performed and the rte_devargs
465  * can be safely discarded.
466  *
467  * Otherwise ``data`` will hold a workable copy of devstr, that will be
468  * used by layers descriptors within rte_devargs. In this case,
469  * any rte_devargs should be cleaned-up before being freed.
470  *
471  * @param da
472  *   rte_devargs structure to fill.
473  *
474  * @param devstr
475  *   Device string.
476  *
477  * @return
478  *   0 on success.
479  *   Negative errno values on error (rte_errno is set).
480  */
481 int
482 rte_devargs_layers_parse(struct rte_devargs *devargs,
483                          const char *devstr);
484
485 /*
486  * probe a device at local process.
487  *
488  * @param devargs
489  *   Device arguments including bus, class and driver properties.
490  * @param new_dev
491  *   new device be probed as output.
492  * @return
493  *   0 on success, negative on error.
494  */
495 int local_dev_probe(const char *devargs, struct rte_device **new_dev);
496
497 /**
498  * Hotplug remove a given device from a specific bus at local process.
499  *
500  * @param dev
501  *   Data structure of the device to remove.
502  * @return
503  *   0 on success, negative on error.
504  */
505 int local_dev_remove(struct rte_device *dev);
506
507 /**
508  * Iterate over all buses to find the corresponding bus to handle the sigbus
509  * error.
510  * @param failure_addr
511  *      Pointer of the fault address of the sigbus error.
512  *
513  * @return
514  *       0 success to handle the sigbus.
515  *      -1 failed to handle the sigbus
516  *       1 no bus can handler the sigbus
517  */
518 int rte_bus_sigbus_handler(const void *failure_addr);
519
520 /**
521  * @internal
522  * Register the sigbus handler.
523  *
524  * @return
525  *   - On success, zero.
526  *   - On failure, a negative value.
527  */
528 int
529 dev_sigbus_handler_register(void);
530
531 /**
532  * @internal
533  * Unregister the sigbus handler.
534  *
535  * @return
536  *   - On success, zero.
537  *   - On failure, a negative value.
538  */
539 int
540 dev_sigbus_handler_unregister(void);
541
542 /**
543  * Get OS-specific EAL mapping base address.
544  */
545 uint64_t
546 eal_get_baseaddr(void);
547
548 void *
549 eal_malloc_no_trace(const char *type, size_t size, unsigned int align);
550
551 void eal_free_no_trace(void *addr);
552
553 /** Options for eal_file_open(). */
554 enum eal_open_flags {
555         /** Open file for reading. */
556         EAL_OPEN_READONLY = 0x00,
557         /** Open file for reading and writing. */
558         EAL_OPEN_READWRITE = 0x02,
559         /**
560          * Create the file if it doesn't exist.
561          * New files are only accessible to the owner (0600 equivalent).
562          */
563         EAL_OPEN_CREATE = 0x04
564 };
565
566 /**
567  * Open or create a file.
568  *
569  * @param path
570  *  Path to the file.
571  * @param flags
572  *  A combination of eal_open_flags controlling operation and FD behavior.
573  * @return
574  *  Open file descriptor on success, (-1) on failure and rte_errno is set.
575  */
576 int
577 eal_file_open(const char *path, int flags);
578
579 /** File locking operation. */
580 enum eal_flock_op {
581         EAL_FLOCK_SHARED,    /**< Acquire a shared lock. */
582         EAL_FLOCK_EXCLUSIVE, /**< Acquire an exclusive lock. */
583         EAL_FLOCK_UNLOCK     /**< Release a previously taken lock. */
584 };
585
586 /** Behavior on file locking conflict. */
587 enum eal_flock_mode {
588         EAL_FLOCK_WAIT,  /**< Wait until the file gets unlocked to lock it. */
589         EAL_FLOCK_RETURN /**< Return immediately if the file is locked. */
590 };
591
592 /**
593  * Lock or unlock the file.
594  *
595  * On failure @code rte_errno @endcode is set to the error code
596  * specified by POSIX flock(3) description.
597  *
598  * @param fd
599  *  Opened file descriptor.
600  * @param op
601  *  Operation to perform.
602  * @param mode
603  *  Behavior on conflict.
604  * @return
605  *  0 on success, (-1) on failure.
606  */
607 int
608 eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode);
609
610 /**
611  * Truncate or extend the file to the specified size.
612  *
613  * On failure @code rte_errno @endcode is set to the error code
614  * specified by POSIX ftruncate(3) description.
615  *
616  * @param fd
617  *  Opened file descriptor.
618  * @param size
619  *  Desired file size.
620  * @return
621  *  0 on success, (-1) on failure.
622  */
623 int
624 eal_file_truncate(int fd, ssize_t size);
625
626 /**
627  * Reserve a region of virtual memory.
628  *
629  * Use eal_mem_free() to free reserved memory.
630  *
631  * @param requested_addr
632  *  A desired reservation address which must be page-aligned.
633  *  The system might not respect it.
634  *  NULL means the address will be chosen by the system.
635  * @param size
636  *  Reservation size. Must be a multiple of system page size.
637  * @param flags
638  *  Reservation options, a combination of eal_mem_reserve_flags.
639  * @returns
640  *  Starting address of the reserved area on success, NULL on failure.
641  *  Callers must not access this memory until remapping it.
642  */
643 void *
644 eal_mem_reserve(void *requested_addr, size_t size, int flags);
645
646 /**
647  * Free memory obtained by eal_mem_reserve() and possibly allocated.
648  *
649  * If *virt* and *size* describe a part of the reserved region,
650  * only this part of the region is freed (accurately up to the system
651  * page size). If *virt* points to allocated memory, *size* must match
652  * the one specified on allocation. The behavior is undefined
653  * if the memory pointed by *virt* is obtained from another source
654  * than listed above.
655  *
656  * @param virt
657  *  A virtual address in a region previously reserved.
658  * @param size
659  *  Number of bytes to unreserve.
660  */
661 void
662 eal_mem_free(void *virt, size_t size);
663
664 /**
665  * Configure memory region inclusion into dumps.
666  *
667  * @param virt
668  *  Starting address of the region.
669  * @param size
670  *  Size of the region.
671  * @param dump
672  *  True to include memory into dumps, false to exclude.
673  * @return
674  *  0 on success, (-1) on failure and rte_errno is set.
675  */
676 int
677 eal_mem_set_dump(void *virt, size_t size, bool dump);
678
679 /**
680  * Sets the runtime directory of DPDK
681  *
682  * @param run_dir
683  *   The new runtime directory path of DPDK
684  * @param size
685  *   The size of the new runtime directory path in bytes.
686  * @return
687  *   0 on success, (-1) on failure.
688  */
689 int
690 eal_set_runtime_dir(char *run_dir, size_t size);
691
692 /**
693  * Get the internal configuration structure.
694  *
695  * @return
696  *   A pointer to the internal configuration structure.
697  */
698 struct internal_config *
699 eal_get_internal_configuration(void);
700
701 /**
702  * Get the current value of the rte_application_usage pointer
703  *
704  * @return
705  *   Pointer to the current value of rte_application_usage .
706  */
707 rte_usage_hook_t
708 eal_get_application_usage_hook(void);
709
710 /**
711  * Instruct primary process that a secondary process wants to attach.
712  */
713 bool __rte_mp_enable(void);
714
715 /**
716  * Init per-lcore info in current thread.
717  *
718  * @param lcore_id
719  *   identifier of lcore.
720  * @param cpuset
721  *   CPU affinity for this thread.
722  */
723 void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
724
725 /**
726  * Uninitialize per-lcore info for current thread.
727  */
728 void __rte_thread_uninit(void);
729
730 /**
731  * asprintf(3) replacement for Windows.
732  */
733 #ifdef RTE_EXEC_ENV_WINDOWS
734 __rte_format_printf(2, 3)
735 int eal_asprintf(char **buffer, const char *format, ...);
736
737 #define asprintf(buffer, format, ...) \
738                 eal_asprintf(buffer, format, ##__VA_ARGS__)
739 #endif
740
741 #endif /* _EAL_PRIVATE_H_ */