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