1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * API for lcore and socket manipulation
14 #include <rte_config.h>
15 #include <rte_per_lcore.h>
17 #include <rte_launch.h>
18 #include <rte_thread.h>
24 #define LCORE_ID_ANY UINT32_MAX /**< Any lcore. */
26 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
29 * The lcore role (used in RTE or not).
31 enum rte_lcore_role_t {
42 * The identifier of the lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
44 * The role of the lcore.
46 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id);
49 * Test if the core supplied has a specific role
52 * The identifier of the lcore, which MUST be between 0 and
55 * The role to be checked against.
57 * Boolean value: positive if test is true; otherwise returns 0.
60 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
63 * Return the Application thread ID of the execution unit.
65 * Note: in most cases the lcore id returned here will also correspond
66 * to the processor id of the CPU on which the thread is pinned, this
67 * will not be the case if the user has explicitly changed the thread to
68 * core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
69 * to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
72 * Logical core ID (in EAL thread or registered non-EAL thread) or
73 * LCORE_ID_ANY (in unregistered non-EAL thread)
75 static inline unsigned
78 return RTE_PER_LCORE(_lcore_id);
82 * Get the id of the main lcore
85 * the id of the main lcore
87 unsigned int rte_get_main_lcore(void);
90 * Deprecated function the id of the main lcore
93 * the id of the main lcore
96 static inline unsigned int rte_get_master_lcore(void)
98 return rte_get_main_lcore();
102 * Return the number of execution units (lcores) on the system.
105 * the number of execution units (lcores) on the system.
107 unsigned int rte_lcore_count(void);
110 * Return the index of the lcore starting from zero.
112 * When option -c or -l is given, the index corresponds
113 * to the order in the list.
115 * -c 0x30, lcore 4 has index 0, and 5 has index 1.
116 * -l 22,18 lcore 22 has index 0, and 18 has index 1.
119 * The targeted lcore, or -1 for the current one.
121 * The relative index, or -1 if not enabled.
123 int rte_lcore_index(int lcore_id);
126 * Return the ID of the physical socket of the logical core we are
129 * the ID of current lcoreid's physical socket
131 unsigned int rte_socket_id(void);
134 * Return number of physical sockets detected on the system.
136 * Note that number of nodes may not be correspondent to their physical id's:
137 * for example, a system may report two socket id's, but the actual socket id's
141 * the number of physical sockets as recognized by EAL
144 rte_socket_count(void);
147 * Return socket id with a particular index.
149 * This will return socket id at a particular position in list of all detected
150 * physical socket id's. For example, on a machine with sockets [0, 8], passing
151 * 1 as a parameter will return 8.
154 * index of physical socket id to return
157 * - physical socket id as recognized by EAL
158 * - -1 on error, with errno set to EINVAL
161 rte_socket_id_by_idx(unsigned int idx);
164 * Get the ID of the physical socket of the specified lcore
167 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
169 * the ID of lcoreid's physical socket
172 rte_lcore_to_socket_id(unsigned int lcore_id);
176 * @b EXPERIMENTAL: this API may change without prior notice.
178 * Return the id of the lcore on a socket starting from zero.
181 * The targeted lcore, or -1 for the current one.
183 * The relative index, or -1 if not enabled.
187 rte_lcore_to_cpu_id(int lcore_id);
191 * @b EXPERIMENTAL: this API may change without prior notice.
193 * Return the cpuset for a given lcore.
195 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
197 * The cpuset of that lcore
201 rte_lcore_cpuset(unsigned int lcore_id);
204 * Test if an lcore is enabled.
207 * The identifier of the lcore, which MUST be between 0 and
210 * True if the given lcore is enabled; false otherwise.
212 int rte_lcore_is_enabled(unsigned int lcore_id);
215 * Get the next enabled lcore ID.
218 * The current lcore (reference).
220 * If true, do not return the ID of the main lcore.
222 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
223 * return RTE_MAX_LCORE.
225 * The next lcore_id or RTE_MAX_LCORE if not found.
227 unsigned int rte_get_next_lcore(unsigned int i, int skip_main, int wrap);
230 * Macro to browse all running lcores.
232 #define RTE_LCORE_FOREACH(i) \
233 for (i = rte_get_next_lcore(-1, 0, 0); \
235 i = rte_get_next_lcore(i, 0, 0))
238 * Macro to browse all running lcores except the main lcore.
240 #define RTE_LCORE_FOREACH_WORKER(i) \
241 for (i = rte_get_next_lcore(-1, 1, 0); \
243 i = rte_get_next_lcore(i, 1, 0))
245 #define RTE_LCORE_FOREACH_SLAVE(l) \
246 RTE_DEPRECATED(RTE_LCORE_FOREACH_SLAVE) RTE_LCORE_FOREACH_WORKER(l)
249 * Callback prototype for initializing lcores.
252 * The lcore to consider.
254 * An opaque pointer passed at callback registration.
256 * - -1 when refusing this operation,
259 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg);
262 * Callback prototype for uninitializing lcores.
265 * The lcore to consider.
267 * An opaque pointer passed at callback registration.
269 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg);
272 * Register callbacks invoked when initializing and uninitializing a lcore.
274 * This function calls the init callback with all initialized lcores.
275 * Any error reported by the init callback triggers a rollback calling the
276 * uninit callback for each lcore.
277 * If this step succeeds, the callbacks are put in the lcore callbacks list
278 * that will get called for each lcore allocation/release.
280 * Note: callbacks execution is serialised under a write lock protecting the
281 * lcores and callbacks list.
284 * A name serving as a small description for this callback.
286 * The callback invoked when a lcore_id is initialized.
289 * The callback invoked when a lcore_id is uninitialized.
290 * uninit can be NULL.
292 * An optional argument that gets passed to the callback when it gets
295 * On success, returns an opaque pointer for the registered object.
296 * On failure (either memory allocation issue in the function itself or an
297 * error is returned by the init callback itself), returns NULL.
301 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init,
302 rte_lcore_uninit_cb uninit, void *arg);
305 * Unregister callbacks previously registered with rte_lcore_callback_register.
307 * This function calls the uninit callback with all initialized lcores.
308 * The callbacks are then removed from the lcore callbacks list.
311 * The handle pointer returned by a former successful call to
312 * rte_lcore_callback_register.
316 rte_lcore_callback_unregister(void *handle);
319 * Callback prototype for iterating over lcores.
322 * The lcore to consider.
324 * An opaque pointer coming from the caller.
326 * - 0 lets the iteration continue.
327 * - !0 makes the iteration stop.
329 typedef int (*rte_lcore_iterate_cb)(unsigned int lcore_id, void *arg);
332 * Iterate on all active lcores (ROLE_RTE, ROLE_SERVICE and ROLE_NON_EAL).
333 * No modification on the lcore states is allowed in the callback.
335 * Note: as opposed to init/uninit callbacks, iteration callbacks can be
336 * invoked in parallel as they are run under a read lock protecting the lcores
337 * and callbacks list.
340 * The callback that gets passed each lcore.
342 * An opaque pointer passed to cb.
344 * Same return code as the callback last invocation (see rte_lcore_iterate_cb
349 rte_lcore_iterate(rte_lcore_iterate_cb cb, void *arg);
355 * The output stream where the dump should be sent.
359 rte_lcore_dump(FILE *f);
364 * @note It fails with glibc < 2.12.
369 * Thread name to set.
371 * On success, return 0; otherwise return a negative value.
373 int rte_thread_setname(pthread_t id, const char *name);
378 * @note It fails with glibc < 2.12.
383 * Thread name to set.
385 * Thread name buffer length.
387 * On success, return 0; otherwise return a negative value.
390 int rte_thread_getname(pthread_t id, char *name, size_t len);
393 * Register current non-EAL thread as a lcore.
395 * @note This API is not compatible with the multi-process feature:
396 * - if a primary process registers a non-EAL thread, then no secondary process
398 * - if a secondary process initialises successfully, trying to register a
399 * non-EAL thread from either primary or secondary processes will always end
400 * up with the thread getting LCORE_ID_ANY as lcore.
403 * On success, return 0; otherwise return -1 with rte_errno set.
407 rte_thread_register(void);
410 * Unregister current thread and release lcore if one was associated.
414 rte_thread_unregister(void);
417 * Create a control thread.
419 * Wrapper to pthread_create(), pthread_setname_np() and
420 * pthread_setaffinity_np(). The affinity of the new thread is based
421 * on the CPU affinity retrieved at the time rte_eal_init() was called,
422 * the dataplane and service lcores are then excluded.
425 * Filled with the thread id of the new created thread.
427 * The name of the control thread (max 16 characters including '\0').
429 * Attributes for the new thread.
430 * @param start_routine
431 * Function to be executed by the new thread.
433 * Argument passed to start_routine.
435 * On success, returns 0; on error, it returns a negative value
436 * corresponding to the error number.
439 rte_ctrl_thread_create(pthread_t *thread, const char *name,
440 const pthread_attr_t *attr,
441 void *(*start_routine)(void *), void *arg);
448 #endif /* _RTE_LCORE_H_ */