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