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