eal: add channel for multi-process communication
[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
17 #include <rte_config.h>
18 #include <rte_compat.h>
19 #include <rte_per_lcore.h>
20 #include <rte_bus.h>
21
22 #include <rte_pci_dev_feature_defs.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define RTE_MAGIC 19820526 /**< Magic number written by the main partition when ready. */
29
30 /* Maximum thread_name length. */
31 #define RTE_MAX_THREAD_NAME_LEN 16
32
33 /**
34  * The lcore role (used in RTE or not).
35  */
36 enum rte_lcore_role_t {
37         ROLE_RTE,
38         ROLE_OFF,
39         ROLE_SERVICE,
40 };
41
42 /**
43  * The type of process in a linuxapp, multi-process setup
44  */
45 enum rte_proc_type_t {
46         RTE_PROC_AUTO = -1,   /* allow auto-detection of primary/secondary */
47         RTE_PROC_PRIMARY = 0, /* set to zero, so primary is the default */
48         RTE_PROC_SECONDARY,
49
50         RTE_PROC_INVALID
51 };
52
53 /**
54  * The global RTE configuration structure.
55  */
56 struct rte_config {
57         uint32_t master_lcore;       /**< Id of the master lcore */
58         uint32_t lcore_count;        /**< Number of available logical cores. */
59         uint32_t service_lcore_count;/**< Number of available service cores. */
60         enum rte_lcore_role_t lcore_role[RTE_MAX_LCORE]; /**< State of cores. */
61
62         /** Primary or secondary configuration */
63         enum rte_proc_type_t process_type;
64
65         /** PA or VA mapping mode */
66         enum rte_iova_mode iova_mode;
67
68         /**
69          * Pointer to memory configuration, which may be shared across multiple
70          * DPDK instances
71          */
72         struct rte_mem_config *mem_config;
73 } __attribute__((__packed__));
74
75 /**
76  * Get the global configuration structure.
77  *
78  * @return
79  *   A pointer to the global configuration structure.
80  */
81 struct rte_config *rte_eal_get_configuration(void);
82
83 /**
84  * Get a lcore's role.
85  *
86  * @param lcore_id
87  *   The identifier of the lcore.
88  * @return
89  *   The role of the lcore.
90  */
91 enum rte_lcore_role_t rte_eal_lcore_role(unsigned lcore_id);
92
93
94 /**
95  * Get the process type in a multi-process setup
96  *
97  * @return
98  *   The process type
99  */
100 enum rte_proc_type_t rte_eal_process_type(void);
101
102 /**
103  * Request iopl privilege for all RPL.
104  *
105  * This function should be called by pmds which need access to ioports.
106
107  * @return
108  *   - On success, returns 0.
109  *   - On failure, returns -1.
110  */
111 int rte_eal_iopl_init(void);
112
113 /**
114  * Initialize the Environment Abstraction Layer (EAL).
115  *
116  * This function is to be executed on the MASTER lcore only, as soon
117  * as possible in the application's main() function.
118  *
119  * The function finishes the initialization process before main() is called.
120  * It puts the SLAVE lcores in the WAIT state.
121  *
122  * When the multi-partition feature is supported, depending on the
123  * configuration (if CONFIG_RTE_EAL_MAIN_PARTITION is disabled), this
124  * function waits to ensure that the magic number is set before
125  * returning. See also the rte_eal_get_configuration() function. Note:
126  * This behavior may change in the future.
127  *
128  * @param argc
129  *   A non-negative value.  If it is greater than 0, the array members
130  *   for argv[0] through argv[argc] (non-inclusive) shall contain pointers
131  *   to strings.
132  * @param argv
133  *   An array of strings.  The contents of the array, as well as the strings
134  *   which are pointed to by the array, may be modified by this function.
135  * @return
136  *   - On success, the number of parsed arguments, which is greater or
137  *     equal to zero. After the call to rte_eal_init(),
138  *     all arguments argv[x] with x < ret may have been modified by this
139  *     function call and should not be further interpreted by the
140  *     application.  The EAL does not take any ownership of the memory used
141  *     for either the argv array, or its members.
142  *   - On failure, -1 and rte_errno is set to a value indicating the cause
143  *     for failure.  In some instances, the application will need to be
144  *     restarted as part of clearing the issue.
145  *
146  *   Error codes returned via rte_errno:
147  *     EACCES indicates a permissions issue.
148  *
149  *     EAGAIN indicates either a bus or system resource was not available,
150  *            setup may be attempted again.
151  *
152  *     EALREADY indicates that the rte_eal_init function has already been
153  *              called, and cannot be called again.
154  *
155  *     EFAULT indicates the tailq configuration name was not found in
156  *            memory configuration.
157  *
158  *     EINVAL indicates invalid parameters were passed as argv/argc.
159  *
160  *     ENOMEM indicates failure likely caused by an out-of-memory condition.
161  *
162  *     ENODEV indicates memory setup issues.
163  *
164  *     ENOTSUP indicates that the EAL cannot initialize on this system.
165  *
166  *     EPROTO indicates that the PCI bus is either not present, or is not
167  *            readable by the eal.
168  *
169  *     ENOEXEC indicates that a service core failed to launch successfully.
170  */
171 int rte_eal_init(int argc, char **argv);
172
173 /**
174  * @warning
175  * @b EXPERIMENTAL: this API may change without prior notice
176  *
177  * Clean up the Environment Abstraction Layer (EAL)
178  *
179  * This function must be called to release any internal resources that EAL has
180  * allocated during rte_eal_init(). After this call, no DPDK function calls may
181  * be made. It is expected that common usage of this function is to call it
182  * just before terminating the process.
183  *
184  * @return 0 Successfully released all internal EAL resources
185  * @return -EFAULT There was an error in releasing all resources.
186  */
187 int __rte_experimental rte_eal_cleanup(void);
188
189 /**
190  * Check if a primary process is currently alive
191  *
192  * This function returns true when a primary process is currently
193  * active.
194  *
195  * @param config_file_path
196  *   The config_file_path argument provided should point at the location
197  *   that the primary process will create its config file. If NULL, the default
198  *   config file path is used.
199  *
200  * @return
201  *  - If alive, returns 1.
202  *  - If dead, returns 0.
203  */
204 int rte_eal_primary_proc_alive(const char *config_file_path);
205
206 #define RTE_MP_MAX_FD_NUM       8    /* The max amount of fds */
207 #define RTE_MP_MAX_NAME_LEN     64   /* The max length of action name */
208 #define RTE_MP_MAX_PARAM_LEN    256  /* The max length of param */
209 struct rte_mp_msg {
210         char name[RTE_MP_MAX_NAME_LEN];
211         int len_param;
212         int num_fds;
213         uint8_t param[RTE_MP_MAX_PARAM_LEN];
214         int fds[RTE_MP_MAX_FD_NUM];
215 };
216
217 /**
218  * Action function typedef used by other components.
219  *
220  * As we create  socket channel for primary/secondary communication, use
221  * this function typedef to register action for coming messages.
222  */
223 typedef int (*rte_mp_t)(const struct rte_mp_msg *msg);
224
225 /**
226  * @warning
227  * @b EXPERIMENTAL: this API may change without prior notice
228  *
229  * Register an action function for primary/secondary communication.
230  *
231  * Call this function to register an action, if the calling component wants
232  * to response the messages from the corresponding component in its primary
233  * process or secondary processes.
234  *
235  * @param name
236  *   The name argument plays as the nonredundant key to find the action.
237  *
238  * @param action
239  *   The action argument is the function pointer to the action function.
240  *
241  * @return
242  *  - 0 on success.
243  *  - (<0) on failure.
244  */
245 int __rte_experimental
246 rte_mp_action_register(const char *name, rte_mp_t action);
247
248 /**
249  * @warning
250  * @b EXPERIMENTAL: this API may change without prior notice
251  *
252  * Unregister an action function for primary/secondary communication.
253  *
254  * Call this function to unregister an action  if the calling component does
255  * not want to response the messages from the corresponding component in its
256  * primary process or secondary processes.
257  *
258  * @param name
259  *   The name argument plays as the nonredundant key to find the action.
260  *
261  */
262 void __rte_experimental
263 rte_mp_action_unregister(const char *name);
264
265 /**
266  * @warning
267  * @b EXPERIMENTAL: this API may change without prior notice
268  *
269  * Send a message to the peer process.
270  *
271  * This function will send a message which will be responsed by the action
272  * identified by name in the peer process.
273  *
274  * @param msg
275  *   The msg argument contains the customized message.
276  *
277  * @return
278  *  - On success, return 0.
279  *  - On failure, return -1, and the reason will be stored in rte_errno.
280  */
281 int __rte_experimental
282 rte_mp_sendmsg(struct rte_mp_msg *msg);
283
284 /**
285  * Usage function typedef used by the application usage function.
286  *
287  * Use this function typedef to define and call rte_set_application_usage_hook()
288  * routine.
289  */
290 typedef void    (*rte_usage_hook_t)(const char * prgname);
291
292 /**
293  * Add application usage routine callout from the eal_usage() routine.
294  *
295  * This function allows the application to include its usage message
296  * in the EAL system usage message. The routine rte_set_application_usage_hook()
297  * needs to be called before the rte_eal_init() routine in the application.
298  *
299  * This routine is optional for the application and will behave as if the set
300  * routine was never called as the default behavior.
301  *
302  * @param usage_func
303  *   The func argument is a function pointer to the application usage routine.
304  *   Called function is defined using rte_usage_hook_t typedef, which is of
305  *   the form void rte_usage_func(const char * prgname).
306  *
307  *   Calling this routine with a NULL value will reset the usage hook routine and
308  *   return the current value, which could be NULL.
309  * @return
310  *   - Returns the current value of the rte_application_usage pointer to allow
311  *     the caller to daisy chain the usage routines if needing more then one.
312  */
313 rte_usage_hook_t
314 rte_set_application_usage_hook(rte_usage_hook_t usage_func);
315
316 /**
317  * macro to get the lock of tailq in mem_config
318  */
319 #define RTE_EAL_TAILQ_RWLOCK         (&rte_eal_get_configuration()->mem_config->qlock)
320
321 /**
322  * macro to get the multiple lock of mempool shared by mutiple-instance
323  */
324 #define RTE_EAL_MEMPOOL_RWLOCK            (&rte_eal_get_configuration()->mem_config->mplock)
325
326 /**
327  * Whether EAL is using huge pages (disabled by --no-huge option).
328  * The no-huge mode cannot be used with UIO poll-mode drivers like igb/ixgbe.
329  * It is useful for NIC drivers (e.g. librte_pmd_mlx4, librte_pmd_vmxnet3) or
330  * crypto drivers (e.g. librte_crypto_nitrox) provided by third-parties such
331  * as 6WIND.
332  *
333  * @return
334  *   Nonzero if hugepages are enabled.
335  */
336 int rte_eal_has_hugepages(void);
337
338 /**
339  * Whether EAL is using PCI bus.
340  * Disabled by --no-pci option.
341  *
342  * @return
343  *   Nonzero if the PCI bus is enabled.
344  */
345 int rte_eal_has_pci(void);
346
347 /**
348  * Whether the EAL was asked to create UIO device.
349  *
350  * @return
351  *   Nonzero if true.
352  */
353 int rte_eal_create_uio_dev(void);
354
355 /**
356  * The user-configured vfio interrupt mode.
357  *
358  * @return
359  *   Interrupt mode configured with the command line,
360  *   RTE_INTR_MODE_NONE by default.
361  */
362 enum rte_intr_mode rte_eal_vfio_intr_mode(void);
363
364 /**
365  * A wrap API for syscall gettid.
366  *
367  * @return
368  *   On success, returns the thread ID of calling process.
369  *   It is always successful.
370  */
371 int rte_sys_gettid(void);
372
373 /**
374  * Get system unique thread id.
375  *
376  * @return
377  *   On success, returns the thread ID of calling process.
378  *   It is always successful.
379  */
380 static inline int rte_gettid(void)
381 {
382         static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
383         if (RTE_PER_LCORE(_thread_id) == -1)
384                 RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
385         return RTE_PER_LCORE(_thread_id);
386 }
387
388 /**
389  * Get the iova mode
390  *
391  * @return
392  *   enum rte_iova_mode value.
393  */
394 enum rte_iova_mode rte_eal_iova_mode(void);
395
396 /**
397  * Get default pool ops name for mbuf
398  *
399  * @return
400  *   returns default pool ops name.
401  */
402 const char *
403 rte_eal_mbuf_default_mempool_ops(void);
404
405 #ifdef __cplusplus
406 }
407 #endif
408
409 #endif /* _RTE_EAL_H_ */