eal: factorize lcore role code
[dpdk.git] / lib / librte_eal / common / include / rte_eal.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #ifndef _RTE_EAL_H_
6 #define _RTE_EAL_H_
7
8 /**
9  * @file
10  *
11  * EAL Configuration API
12  */
13
14 #include <stdint.h>
15 #include <sched.h>
16 #include <time.h>
17
18 #include <rte_config.h>
19 #include <rte_compat.h>
20 #include <rte_per_lcore.h>
21 #include <rte_bus.h>
22
23 #include <rte_pci_dev_feature_defs.h>
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #define RTE_MAGIC 19820526 /**< Magic number written by the main partition when ready. */
30
31 /* Maximum thread_name length. */
32 #define RTE_MAX_THREAD_NAME_LEN 16
33
34 /**
35  * The lcore role (used in RTE or not).
36  */
37 enum rte_lcore_role_t {
38         ROLE_RTE,
39         ROLE_OFF,
40         ROLE_SERVICE,
41 };
42
43 /**
44  * The type of process in a linux, multi-process setup
45  */
46 enum rte_proc_type_t {
47         RTE_PROC_AUTO = -1,   /* allow auto-detection of primary/secondary */
48         RTE_PROC_PRIMARY = 0, /* set to zero, so primary is the default */
49         RTE_PROC_SECONDARY,
50
51         RTE_PROC_INVALID
52 };
53
54 /**
55  * The global RTE configuration structure.
56  */
57 struct rte_config {
58         uint32_t master_lcore;       /**< Id of the master lcore */
59         uint32_t lcore_count;        /**< Number of available logical cores. */
60         uint32_t numa_node_count;    /**< Number of detected NUMA nodes. */
61         uint32_t numa_nodes[RTE_MAX_NUMA_NODES]; /**< List of detected NUMA nodes. */
62         uint32_t service_lcore_count;/**< Number of available service cores. */
63         enum rte_lcore_role_t lcore_role[RTE_MAX_LCORE]; /**< State of cores. */
64
65         /** Primary or secondary configuration */
66         enum rte_proc_type_t process_type;
67
68         /** PA or VA mapping mode */
69         enum rte_iova_mode iova_mode;
70
71         /**
72          * Pointer to memory configuration, which may be shared across multiple
73          * DPDK instances
74          */
75         struct rte_mem_config *mem_config;
76 } __attribute__((__packed__));
77
78 /**
79  * Get the global configuration structure.
80  *
81  * @return
82  *   A pointer to the global configuration structure.
83  */
84 struct rte_config *rte_eal_get_configuration(void);
85
86 /**
87  * Get the process type in a multi-process setup
88  *
89  * @return
90  *   The process type
91  */
92 enum rte_proc_type_t rte_eal_process_type(void);
93
94 /**
95  * Request iopl privilege for all RPL.
96  *
97  * This function should be called by pmds which need access to ioports.
98
99  * @return
100  *   - On success, returns 0.
101  *   - On failure, returns -1.
102  */
103 int rte_eal_iopl_init(void);
104
105 /**
106  * Initialize the Environment Abstraction Layer (EAL).
107  *
108  * This function is to be executed on the MASTER lcore only, as soon
109  * as possible in the application's main() function.
110  *
111  * The function finishes the initialization process before main() is called.
112  * It puts the SLAVE lcores in the WAIT state.
113  *
114  * When the multi-partition feature is supported, depending on the
115  * configuration (if CONFIG_RTE_EAL_MAIN_PARTITION is disabled), this
116  * function waits to ensure that the magic number is set before
117  * returning. See also the rte_eal_get_configuration() function. Note:
118  * This behavior may change in the future.
119  *
120  * @param argc
121  *   A non-negative value.  If it is greater than 0, the array members
122  *   for argv[0] through argv[argc] (non-inclusive) shall contain pointers
123  *   to strings.
124  * @param argv
125  *   An array of strings.  The contents of the array, as well as the strings
126  *   which are pointed to by the array, may be modified by this function.
127  * @return
128  *   - On success, the number of parsed arguments, which is greater or
129  *     equal to zero. After the call to rte_eal_init(),
130  *     all arguments argv[x] with x < ret may have been modified by this
131  *     function call and should not be further interpreted by the
132  *     application.  The EAL does not take any ownership of the memory used
133  *     for either the argv array, or its members.
134  *   - On failure, -1 and rte_errno is set to a value indicating the cause
135  *     for failure.  In some instances, the application will need to be
136  *     restarted as part of clearing the issue.
137  *
138  *   Error codes returned via rte_errno:
139  *     EACCES indicates a permissions issue.
140  *
141  *     EAGAIN indicates either a bus or system resource was not available,
142  *            setup may be attempted again.
143  *
144  *     EALREADY indicates that the rte_eal_init function has already been
145  *              called, and cannot be called again.
146  *
147  *     EFAULT indicates the tailq configuration name was not found in
148  *            memory configuration.
149  *
150  *     EINVAL indicates invalid parameters were passed as argv/argc.
151  *
152  *     ENOMEM indicates failure likely caused by an out-of-memory condition.
153  *
154  *     ENODEV indicates memory setup issues.
155  *
156  *     ENOTSUP indicates that the EAL cannot initialize on this system.
157  *
158  *     EPROTO indicates that the PCI bus is either not present, or is not
159  *            readable by the eal.
160  *
161  *     ENOEXEC indicates that a service core failed to launch successfully.
162  */
163 int rte_eal_init(int argc, char **argv);
164
165 /**
166  * Clean up the Environment Abstraction Layer (EAL)
167  *
168  * This function must be called to release any internal resources that EAL has
169  * allocated during rte_eal_init(). After this call, no DPDK function calls may
170  * be made. It is expected that common usage of this function is to call it
171  * just before terminating the process.
172  *
173  * @return 0 Successfully released all internal EAL resources
174  * @return -EFAULT There was an error in releasing all resources.
175  */
176 int rte_eal_cleanup(void);
177
178 /**
179  * Check if a primary process is currently alive
180  *
181  * This function returns true when a primary process is currently
182  * active.
183  *
184  * @param config_file_path
185  *   The config_file_path argument provided should point at the location
186  *   that the primary process will create its config file. If NULL, the default
187  *   config file path is used.
188  *
189  * @return
190  *  - If alive, returns 1.
191  *  - If dead, returns 0.
192  */
193 int rte_eal_primary_proc_alive(const char *config_file_path);
194
195 #define RTE_MP_MAX_FD_NUM       8    /* The max amount of fds */
196 #define RTE_MP_MAX_NAME_LEN     64   /* The max length of action name */
197 #define RTE_MP_MAX_PARAM_LEN    256  /* The max length of param */
198 struct rte_mp_msg {
199         char name[RTE_MP_MAX_NAME_LEN];
200         int len_param;
201         int num_fds;
202         uint8_t param[RTE_MP_MAX_PARAM_LEN];
203         int fds[RTE_MP_MAX_FD_NUM];
204 };
205
206 struct rte_mp_reply {
207         int nb_sent;
208         int nb_received;
209         struct rte_mp_msg *msgs; /* caller to free */
210 };
211
212 /**
213  * Action function typedef used by other components.
214  *
215  * As we create  socket channel for primary/secondary communication, use
216  * this function typedef to register action for coming messages.
217  *
218  * @note When handling IPC request callbacks, the reply must be sent even in
219  *   cases of error handling. Simply returning success or failure will *not*
220  *   send a response to the requestor.
221  *   Implementation of error signalling mechanism is up to the application.
222  *
223  * @note No memory allocations should take place inside the callback.
224  */
225 typedef int (*rte_mp_t)(const struct rte_mp_msg *msg, const void *peer);
226
227 /**
228  * Asynchronous reply function typedef used by other components.
229  *
230  * As we create socket channel for primary/secondary communication, use
231  * this function typedef to register action for coming responses to asynchronous
232  * requests.
233  *
234  * @note When handling IPC request callbacks, the reply must be sent even in
235  *   cases of error handling. Simply returning success or failure will *not*
236  *   send a response to the requestor.
237  *   Implementation of error signalling mechanism is up to the application.
238  *
239  * @note No memory allocations should take place inside the callback.
240  */
241 typedef int (*rte_mp_async_reply_t)(const struct rte_mp_msg *request,
242                 const struct rte_mp_reply *reply);
243
244 /**
245  * @warning
246  * @b EXPERIMENTAL: this API may change without prior notice
247  *
248  * Register an action function for primary/secondary communication.
249  *
250  * Call this function to register an action, if the calling component wants
251  * to response the messages from the corresponding component in its primary
252  * process or secondary processes.
253  *
254  * @note IPC may be unsupported in certain circumstances, so caller should check
255  *    for ENOTSUP error.
256  *
257  * @param name
258  *   The name argument plays as the nonredundant key to find the action.
259  *
260  * @param action
261  *   The action argument is the function pointer to the action function.
262  *
263  * @return
264  *  - 0 on success.
265  *  - (<0) on failure.
266  */
267 __rte_experimental
268 int
269 rte_mp_action_register(const char *name, rte_mp_t action);
270
271 /**
272  * @warning
273  * @b EXPERIMENTAL: this API may change without prior notice
274  *
275  * Unregister an action function for primary/secondary communication.
276  *
277  * Call this function to unregister an action  if the calling component does
278  * not want to response the messages from the corresponding component in its
279  * primary process or secondary processes.
280  *
281  * @note IPC may be unsupported in certain circumstances, so caller should check
282  *    for ENOTSUP error.
283  *
284  * @param name
285  *   The name argument plays as the nonredundant key to find the action.
286  *
287  */
288 __rte_experimental
289 void
290 rte_mp_action_unregister(const char *name);
291
292 /**
293  * @warning
294  * @b EXPERIMENTAL: this API may change without prior notice
295  *
296  * Send a message to the peer process.
297  *
298  * This function will send a message which will be responded by the action
299  * identified by name in the peer process.
300  *
301  * @param msg
302  *   The msg argument contains the customized message.
303  *
304  * @return
305  *  - On success, return 0.
306  *  - On failure, return -1, and the reason will be stored in rte_errno.
307  */
308 __rte_experimental
309 int
310 rte_mp_sendmsg(struct rte_mp_msg *msg);
311
312 /**
313  * @warning
314  * @b EXPERIMENTAL: this API may change without prior notice
315  *
316  * Send a request to the peer process and expect a reply.
317  *
318  * This function sends a request message to the peer process, and will
319  * block until receiving reply message from the peer process.
320  *
321  * @note The caller is responsible to free reply->replies.
322  *
323  * @note This API must not be used inside memory-related or IPC callbacks, and
324  *   no memory allocations should take place inside such callback.
325  *
326  * @note IPC may be unsupported in certain circumstances, so caller should check
327  *    for ENOTSUP error.
328  *
329  * @param req
330  *   The req argument contains the customized request message.
331  *
332  * @param reply
333  *   The reply argument will be for storing all the replied messages;
334  *   the caller is responsible for free reply->msgs.
335  *
336  * @param ts
337  *   The ts argument specifies how long we can wait for the peer(s) to reply.
338  *
339  * @return
340  *  - On success, return 0.
341  *  - On failure, return -1, and the reason will be stored in rte_errno.
342  */
343 __rte_experimental
344 int
345 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
346                const struct timespec *ts);
347
348 /**
349  * @warning
350  * @b EXPERIMENTAL: this API may change without prior notice
351  *
352  * Send a request to the peer process and expect a reply in a separate callback.
353  *
354  * This function sends a request message to the peer process, and will not
355  * block. Instead, reply will be received in a separate callback.
356  *
357  * @note IPC may be unsupported in certain circumstances, so caller should check
358  *    for ENOTSUP error.
359  *
360  * @param req
361  *   The req argument contains the customized request message.
362  *
363  * @param ts
364  *   The ts argument specifies how long we can wait for the peer(s) to reply.
365  *
366  * @param clb
367  *   The callback to trigger when all responses for this request have arrived.
368  *
369  * @return
370  *  - On success, return 0.
371  *  - On failure, return -1, and the reason will be stored in rte_errno.
372  */
373 __rte_experimental
374 int
375 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
376                 rte_mp_async_reply_t clb);
377
378 /**
379  * @warning
380  * @b EXPERIMENTAL: this API may change without prior notice
381  *
382  * Send a reply to the peer process.
383  *
384  * This function will send a reply message in response to a request message
385  * received previously.
386  *
387  * @note When handling IPC request callbacks, the reply must be sent even in
388  *   cases of error handling. Simply returning success or failure will *not*
389  *   send a response to the requestor.
390  *   Implementation of error signalling mechanism is up to the application.
391  *
392  * @param msg
393  *   The msg argument contains the customized message.
394  *
395  * @param peer
396  *   The peer argument is the pointer to the peer socket path.
397  *
398  * @return
399  *  - On success, return 0.
400  *  - On failure, return -1, and the reason will be stored in rte_errno.
401  */
402 __rte_experimental
403 int
404 rte_mp_reply(struct rte_mp_msg *msg, const char *peer);
405
406 /**
407  * Usage function typedef used by the application usage function.
408  *
409  * Use this function typedef to define and call rte_set_application_usage_hook()
410  * routine.
411  */
412 typedef void    (*rte_usage_hook_t)(const char * prgname);
413
414 /**
415  * Add application usage routine callout from the eal_usage() routine.
416  *
417  * This function allows the application to include its usage message
418  * in the EAL system usage message. The routine rte_set_application_usage_hook()
419  * needs to be called before the rte_eal_init() routine in the application.
420  *
421  * This routine is optional for the application and will behave as if the set
422  * routine was never called as the default behavior.
423  *
424  * @param usage_func
425  *   The func argument is a function pointer to the application usage routine.
426  *   Called function is defined using rte_usage_hook_t typedef, which is of
427  *   the form void rte_usage_func(const char * prgname).
428  *
429  *   Calling this routine with a NULL value will reset the usage hook routine and
430  *   return the current value, which could be NULL.
431  * @return
432  *   - Returns the current value of the rte_application_usage pointer to allow
433  *     the caller to daisy chain the usage routines if needing more then one.
434  */
435 rte_usage_hook_t
436 rte_set_application_usage_hook(rte_usage_hook_t usage_func);
437
438 /**
439  * Whether EAL is using huge pages (disabled by --no-huge option).
440  * The no-huge mode is not compatible with all drivers or features.
441  *
442  * @return
443  *   Nonzero if hugepages are enabled.
444  */
445 int rte_eal_has_hugepages(void);
446
447 /**
448  * Whether EAL is using PCI bus.
449  * Disabled by --no-pci option.
450  *
451  * @return
452  *   Nonzero if the PCI bus is enabled.
453  */
454 int rte_eal_has_pci(void);
455
456 /**
457  * Whether the EAL was asked to create UIO device.
458  *
459  * @return
460  *   Nonzero if true.
461  */
462 int rte_eal_create_uio_dev(void);
463
464 /**
465  * The user-configured vfio interrupt mode.
466  *
467  * @return
468  *   Interrupt mode configured with the command line,
469  *   RTE_INTR_MODE_NONE by default.
470  */
471 enum rte_intr_mode rte_eal_vfio_intr_mode(void);
472
473 /**
474  * A wrap API for syscall gettid.
475  *
476  * @return
477  *   On success, returns the thread ID of calling process.
478  *   It is always successful.
479  */
480 int rte_sys_gettid(void);
481
482 /**
483  * Get system unique thread id.
484  *
485  * @return
486  *   On success, returns the thread ID of calling process.
487  *   It is always successful.
488  */
489 static inline int rte_gettid(void)
490 {
491         static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
492         if (RTE_PER_LCORE(_thread_id) == -1)
493                 RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
494         return RTE_PER_LCORE(_thread_id);
495 }
496
497 /**
498  * Get the iova mode
499  *
500  * @return
501  *   enum rte_iova_mode value.
502  */
503 enum rte_iova_mode rte_eal_iova_mode(void);
504
505 /**
506  * Get user provided pool ops name for mbuf
507  *
508  * @return
509  *   returns user provided pool ops name.
510  */
511 const char *
512 rte_eal_mbuf_user_pool_ops(void);
513
514 /**
515  * Get the runtime directory of DPDK
516  *
517  * @return
518  *  The runtime directory path of DPDK
519  */
520 const char *
521 rte_eal_get_runtime_dir(void);
522
523 #ifdef __cplusplus
524 }
525 #endif
526
527 #endif /* _RTE_EAL_H_ */