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);
189 #ifdef RTE_HAS_CPUSET
193 * @b EXPERIMENTAL: this API may change without prior notice.
195 * Return the cpuset for a given lcore.
197 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
199 * The cpuset of that lcore
203 rte_lcore_cpuset(unsigned int lcore_id);
205 #endif /* RTE_HAS_CPUSET */
208 * Test if an lcore is enabled.
211 * The identifier of the lcore, which MUST be between 0 and
214 * True if the given lcore is enabled; false otherwise.
216 int rte_lcore_is_enabled(unsigned int lcore_id);
219 * Get the next enabled lcore ID.
222 * The current lcore (reference).
224 * If true, do not return the ID of the main lcore.
226 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
227 * return RTE_MAX_LCORE.
229 * The next lcore_id or RTE_MAX_LCORE if not found.
231 unsigned int rte_get_next_lcore(unsigned int i, int skip_main, int wrap);
234 * Macro to browse all running lcores.
236 #define RTE_LCORE_FOREACH(i) \
237 for (i = rte_get_next_lcore(-1, 0, 0); \
239 i = rte_get_next_lcore(i, 0, 0))
242 * Macro to browse all running lcores except the main lcore.
244 #define RTE_LCORE_FOREACH_WORKER(i) \
245 for (i = rte_get_next_lcore(-1, 1, 0); \
247 i = rte_get_next_lcore(i, 1, 0))
249 #define RTE_LCORE_FOREACH_SLAVE(l) \
250 RTE_DEPRECATED(RTE_LCORE_FOREACH_SLAVE) RTE_LCORE_FOREACH_WORKER(l)
253 * Callback prototype for initializing lcores.
256 * The lcore to consider.
258 * An opaque pointer passed at callback registration.
260 * - -1 when refusing this operation,
263 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg);
266 * Callback prototype for uninitializing lcores.
269 * The lcore to consider.
271 * An opaque pointer passed at callback registration.
273 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg);
276 * Register callbacks invoked when initializing and uninitializing a lcore.
278 * This function calls the init callback with all initialized lcores.
279 * Any error reported by the init callback triggers a rollback calling the
280 * uninit callback for each lcore.
281 * If this step succeeds, the callbacks are put in the lcore callbacks list
282 * that will get called for each lcore allocation/release.
284 * Note: callbacks execution is serialised under a write lock protecting the
285 * lcores and callbacks list.
288 * A name serving as a small description for this callback.
290 * The callback invoked when a lcore_id is initialized.
293 * The callback invoked when a lcore_id is uninitialized.
294 * uninit can be NULL.
296 * An optional argument that gets passed to the callback when it gets
299 * On success, returns an opaque pointer for the registered object.
300 * On failure (either memory allocation issue in the function itself or an
301 * error is returned by the init callback itself), returns NULL.
305 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init,
306 rte_lcore_uninit_cb uninit, void *arg);
309 * Unregister callbacks previously registered with rte_lcore_callback_register.
311 * This function calls the uninit callback with all initialized lcores.
312 * The callbacks are then removed from the lcore callbacks list.
315 * The handle pointer returned by a former successful call to
316 * rte_lcore_callback_register.
320 rte_lcore_callback_unregister(void *handle);
323 * Callback prototype for iterating over lcores.
326 * The lcore to consider.
328 * An opaque pointer coming from the caller.
330 * - 0 lets the iteration continue.
331 * - !0 makes the iteration stop.
333 typedef int (*rte_lcore_iterate_cb)(unsigned int lcore_id, void *arg);
336 * Iterate on all active lcores (ROLE_RTE, ROLE_SERVICE and ROLE_NON_EAL).
337 * No modification on the lcore states is allowed in the callback.
339 * Note: as opposed to init/uninit callbacks, iteration callbacks can be
340 * invoked in parallel as they are run under a read lock protecting the lcores
341 * and callbacks list.
344 * The callback that gets passed each lcore.
346 * An opaque pointer passed to cb.
348 * Same return code as the callback last invocation (see rte_lcore_iterate_cb
353 rte_lcore_iterate(rte_lcore_iterate_cb cb, void *arg);
359 * The output stream where the dump should be sent.
363 rte_lcore_dump(FILE *f);
368 * @note It fails with glibc < 2.12.
373 * Thread name to set.
375 * On success, return 0; otherwise return a negative value.
377 int rte_thread_setname(pthread_t id, const char *name);
382 * @note It fails with glibc < 2.12.
387 * Thread name to set.
389 * Thread name buffer length.
391 * On success, return 0; otherwise return a negative value.
394 int rte_thread_getname(pthread_t id, char *name, size_t len);
397 * Register current non-EAL thread as a lcore.
399 * @note This API is not compatible with the multi-process feature:
400 * - if a primary process registers a non-EAL thread, then no secondary process
402 * - if a secondary process initialises successfully, trying to register a
403 * non-EAL thread from either primary or secondary processes will always end
404 * up with the thread getting LCORE_ID_ANY as lcore.
407 * On success, return 0; otherwise return -1 with rte_errno set.
411 rte_thread_register(void);
414 * Unregister current thread and release lcore if one was associated.
418 rte_thread_unregister(void);
421 * Create a control thread.
423 * Wrapper to pthread_create(), pthread_setname_np() and
424 * pthread_setaffinity_np(). The affinity of the new thread is based
425 * on the CPU affinity retrieved at the time rte_eal_init() was called,
426 * the dataplane and service lcores are then excluded.
429 * Filled with the thread id of the new created thread.
431 * The name of the control thread (max 16 characters including '\0').
433 * Attributes for the new thread.
434 * @param start_routine
435 * Function to be executed by the new thread.
437 * Argument passed to start_routine.
439 * On success, returns 0; on error, it returns a negative value
440 * corresponding to the error number.
443 rte_ctrl_thread_create(pthread_t *thread, const char *name,
444 const pthread_attr_t *attr,
445 void *(*start_routine)(void *), void *arg);
452 #endif /* _RTE_LCORE_H_ */