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 * Return the number of execution units (lcores) on the system.
93 * the number of execution units (lcores) on the system.
95 unsigned int rte_lcore_count(void);
98 * Return the index of the lcore starting from zero.
100 * When option -c or -l is given, the index corresponds
101 * to the order in the list.
103 * -c 0x30, lcore 4 has index 0, and 5 has index 1.
104 * -l 22,18 lcore 22 has index 0, and 18 has index 1.
107 * The targeted lcore, or -1 for the current one.
109 * The relative index, or -1 if not enabled.
111 int rte_lcore_index(int lcore_id);
114 * Return the ID of the physical socket of the logical core we are
117 * the ID of current lcoreid's physical socket
119 unsigned int rte_socket_id(void);
122 * Return number of physical sockets detected on the system.
124 * Note that number of nodes may not be correspondent to their physical id's:
125 * for example, a system may report two socket id's, but the actual socket id's
129 * the number of physical sockets as recognized by EAL
132 rte_socket_count(void);
135 * Return socket id with a particular index.
137 * This will return socket id at a particular position in list of all detected
138 * physical socket id's. For example, on a machine with sockets [0, 8], passing
139 * 1 as a parameter will return 8.
142 * index of physical socket id to return
145 * - physical socket id as recognized by EAL
146 * - -1 on error, with errno set to EINVAL
149 rte_socket_id_by_idx(unsigned int idx);
152 * Get the ID of the physical socket of the specified lcore
155 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
157 * the ID of lcoreid's physical socket
160 rte_lcore_to_socket_id(unsigned int lcore_id);
164 * @b EXPERIMENTAL: this API may change without prior notice.
166 * Return the id of the lcore on a socket starting from zero.
169 * The targeted lcore, or -1 for the current one.
171 * The relative index, or -1 if not enabled.
175 rte_lcore_to_cpu_id(int lcore_id);
177 #ifdef RTE_HAS_CPUSET
181 * @b EXPERIMENTAL: this API may change without prior notice.
183 * Return the cpuset for a given lcore.
185 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
187 * The cpuset of that lcore
191 rte_lcore_cpuset(unsigned int lcore_id);
193 #endif /* RTE_HAS_CPUSET */
196 * Test if an lcore is enabled.
199 * The identifier of the lcore, which MUST be between 0 and
202 * True if the given lcore is enabled; false otherwise.
204 int rte_lcore_is_enabled(unsigned int lcore_id);
207 * Get the next enabled lcore ID.
210 * The current lcore (reference).
212 * If true, do not return the ID of the main lcore.
214 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
215 * return RTE_MAX_LCORE.
217 * The next lcore_id or RTE_MAX_LCORE if not found.
219 unsigned int rte_get_next_lcore(unsigned int i, int skip_main, int wrap);
222 * Macro to browse all running lcores.
224 #define RTE_LCORE_FOREACH(i) \
225 for (i = rte_get_next_lcore(-1, 0, 0); \
227 i = rte_get_next_lcore(i, 0, 0))
230 * Macro to browse all running lcores except the main lcore.
232 #define RTE_LCORE_FOREACH_WORKER(i) \
233 for (i = rte_get_next_lcore(-1, 1, 0); \
235 i = rte_get_next_lcore(i, 1, 0))
238 * Callback prototype for initializing lcores.
241 * The lcore to consider.
243 * An opaque pointer passed at callback registration.
245 * - -1 when refusing this operation,
248 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg);
251 * Callback prototype for uninitializing lcores.
254 * The lcore to consider.
256 * An opaque pointer passed at callback registration.
258 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg);
261 * Register callbacks invoked when initializing and uninitializing a lcore.
263 * This function calls the init callback with all initialized lcores.
264 * Any error reported by the init callback triggers a rollback calling the
265 * uninit callback for each lcore.
266 * If this step succeeds, the callbacks are put in the lcore callbacks list
267 * that will get called for each lcore allocation/release.
269 * Note: callbacks execution is serialised under a write lock protecting the
270 * lcores and callbacks list.
273 * A name serving as a small description for this callback.
275 * The callback invoked when a lcore_id is initialized.
278 * The callback invoked when a lcore_id is uninitialized.
279 * uninit can be NULL.
281 * An optional argument that gets passed to the callback when it gets
284 * On success, returns an opaque pointer for the registered object.
285 * On failure (either memory allocation issue in the function itself or an
286 * error is returned by the init callback itself), returns NULL.
289 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init,
290 rte_lcore_uninit_cb uninit, void *arg);
293 * Unregister callbacks previously registered with rte_lcore_callback_register.
295 * This function calls the uninit callback with all initialized lcores.
296 * The callbacks are then removed from the lcore callbacks list.
299 * The handle pointer returned by a former successful call to
300 * rte_lcore_callback_register.
303 rte_lcore_callback_unregister(void *handle);
306 * Callback prototype for iterating over lcores.
309 * The lcore to consider.
311 * An opaque pointer coming from the caller.
313 * - 0 lets the iteration continue.
314 * - !0 makes the iteration stop.
316 typedef int (*rte_lcore_iterate_cb)(unsigned int lcore_id, void *arg);
319 * Iterate on all active lcores (ROLE_RTE, ROLE_SERVICE and ROLE_NON_EAL).
320 * No modification on the lcore states is allowed in the callback.
322 * Note: as opposed to init/uninit callbacks, iteration callbacks can be
323 * invoked in parallel as they are run under a read lock protecting the lcores
324 * and callbacks list.
327 * The callback that gets passed each lcore.
329 * An opaque pointer passed to cb.
331 * Same return code as the callback last invocation (see rte_lcore_iterate_cb
335 rte_lcore_iterate(rte_lcore_iterate_cb cb, void *arg);
341 * The output stream where the dump should be sent.
344 rte_lcore_dump(FILE *f);
349 * @note It fails with glibc < 2.12.
354 * Thread name to set.
356 * On success, return 0; otherwise return a negative value.
358 int rte_thread_setname(pthread_t id, const char *name);
363 * @note It fails with glibc < 2.12.
368 * Thread name to set.
370 * Thread name buffer length.
372 * On success, return 0; otherwise return a negative value.
375 int rte_thread_getname(pthread_t id, char *name, size_t len);
378 * Register current non-EAL thread as a lcore.
380 * @note This API is not compatible with the multi-process feature:
381 * - if a primary process registers a non-EAL thread, then no secondary process
383 * - if a secondary process initialises successfully, trying to register a
384 * non-EAL thread from either primary or secondary processes will always end
385 * up with the thread getting LCORE_ID_ANY as lcore.
388 * On success, return 0; otherwise return -1 with rte_errno set.
391 rte_thread_register(void);
394 * Unregister current thread and release lcore if one was associated.
397 rte_thread_unregister(void);
400 * Create a control thread.
402 * Creates a control thread with the given name and attributes. The
403 * affinity of the new thread is based on the CPU affinity retrieved
404 * at the time rte_eal_init() was called, the dataplane and service
405 * lcores are then excluded. If setting the name of the thread fails,
406 * the error is ignored and a debug message is logged.
409 * Filled with the thread id of the new created thread.
411 * The name of the control thread (max 16 characters including '\0').
413 * Attributes for the new thread.
414 * @param start_routine
415 * Function to be executed by the new thread.
417 * Argument passed to start_routine.
419 * On success, returns 0; on error, it returns a negative value
420 * corresponding to the error number.
423 rte_ctrl_thread_create(pthread_t *thread, const char *name,
424 const pthread_attr_t *attr,
425 void *(*start_routine)(void *), void *arg);
432 #endif /* _RTE_LCORE_H_ */