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>
23 #define LCORE_ID_ANY UINT32_MAX /**< Any lcore. */
25 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
28 * The lcore role (used in RTE or not).
30 enum rte_lcore_role_t {
41 * The identifier of the lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
43 * The role of the lcore.
45 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id);
48 * Test if the core supplied has a specific role
51 * The identifier of the lcore, which MUST be between 0 and
54 * The role to be checked against.
56 * Boolean value: positive if test is true; otherwise returns 0.
59 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
62 * Return the Application thread ID of the execution unit.
64 * Note: in most cases the lcore id returned here will also correspond
65 * to the processor id of the CPU on which the thread is pinned, this
66 * will not be the case if the user has explicitly changed the thread to
67 * core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
68 * to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
71 * Logical core ID (in EAL thread or registered non-EAL thread) or
72 * LCORE_ID_ANY (in unregistered non-EAL thread)
74 static inline unsigned
77 return RTE_PER_LCORE(_lcore_id);
81 * Get the id of the master lcore
84 * the id of the master lcore
86 unsigned int rte_get_master_lcore(void);
89 * Return the number of execution units (lcores) on the system.
92 * the number of execution units (lcores) on the system.
94 unsigned int rte_lcore_count(void);
97 * Return the index of the lcore starting from zero.
99 * When option -c or -l is given, the index corresponds
100 * to the order in the list.
102 * -c 0x30, lcore 4 has index 0, and 5 has index 1.
103 * -l 22,18 lcore 22 has index 0, and 18 has index 1.
106 * The targeted lcore, or -1 for the current one.
108 * The relative index, or -1 if not enabled.
110 int rte_lcore_index(int lcore_id);
113 * Return the ID of the physical socket of the logical core we are
116 * the ID of current lcoreid's physical socket
118 unsigned int rte_socket_id(void);
121 * Return number of physical sockets detected on the system.
123 * Note that number of nodes may not be correspondent to their physical id's:
124 * for example, a system may report two socket id's, but the actual socket id's
128 * the number of physical sockets as recognized by EAL
131 rte_socket_count(void);
134 * Return socket id with a particular index.
136 * This will return socket id at a particular position in list of all detected
137 * physical socket id's. For example, on a machine with sockets [0, 8], passing
138 * 1 as a parameter will return 8.
141 * index of physical socket id to return
144 * - physical socket id as recognized by EAL
145 * - -1 on error, with errno set to EINVAL
148 rte_socket_id_by_idx(unsigned int idx);
151 * Get the ID of the physical socket of the specified lcore
154 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
156 * the ID of lcoreid's physical socket
159 rte_lcore_to_socket_id(unsigned int lcore_id);
163 * @b EXPERIMENTAL: this API may change without prior notice.
165 * Return the id of the lcore on a socket starting from zero.
168 * The targeted lcore, or -1 for the current one.
170 * The relative index, or -1 if not enabled.
174 rte_lcore_to_cpu_id(int lcore_id);
178 * @b EXPERIMENTAL: this API may change without prior notice.
180 * Return the cpuset for a given lcore.
182 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
184 * The cpuset of that lcore
188 rte_lcore_cpuset(unsigned int lcore_id);
191 * Test if an lcore is enabled.
194 * The identifier of the lcore, which MUST be between 0 and
197 * True if the given lcore is enabled; false otherwise.
199 int rte_lcore_is_enabled(unsigned int lcore_id);
202 * Get the next enabled lcore ID.
205 * The current lcore (reference).
207 * If true, do not return the ID of the master lcore.
209 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
210 * return RTE_MAX_LCORE.
212 * The next lcore_id or RTE_MAX_LCORE if not found.
214 unsigned int rte_get_next_lcore(unsigned int i, int skip_master, int wrap);
217 * Macro to browse all running lcores.
219 #define RTE_LCORE_FOREACH(i) \
220 for (i = rte_get_next_lcore(-1, 0, 0); \
222 i = rte_get_next_lcore(i, 0, 0))
225 * Macro to browse all running lcores except the master lcore.
227 #define RTE_LCORE_FOREACH_SLAVE(i) \
228 for (i = rte_get_next_lcore(-1, 1, 0); \
230 i = rte_get_next_lcore(i, 1, 0))
233 * Callback prototype for initializing lcores.
236 * The lcore to consider.
238 * An opaque pointer passed at callback registration.
240 * - -1 when refusing this operation,
243 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg);
246 * Callback prototype for uninitializing lcores.
249 * The lcore to consider.
251 * An opaque pointer passed at callback registration.
253 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg);
256 * Register callbacks invoked when initializing and uninitializing a lcore.
258 * This function calls the init callback with all initialized lcores.
259 * Any error reported by the init callback triggers a rollback calling the
260 * uninit callback for each lcore.
261 * If this step succeeds, the callbacks are put in the lcore callbacks list
262 * that will get called for each lcore allocation/release.
264 * Note: callbacks execution is serialised under a lock protecting the lcores
265 * and callbacks list.
268 * A name serving as a small description for this callback.
270 * The callback invoked when a lcore_id is initialized.
273 * The callback invoked when a lcore_id is uninitialized.
274 * uninit can be NULL.
276 * An optional argument that gets passed to the callback when it gets
279 * On success, returns an opaque pointer for the registered object.
280 * On failure (either memory allocation issue in the function itself or an
281 * error is returned by the init callback itself), returns NULL.
285 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init,
286 rte_lcore_uninit_cb uninit, void *arg);
289 * Unregister callbacks previously registered with rte_lcore_callback_register.
291 * This function calls the uninit callback with all initialized lcores.
292 * The callbacks are then removed from the lcore callbacks list.
295 * The handle pointer returned by a former successful call to
296 * rte_lcore_callback_register.
300 rte_lcore_callback_unregister(void *handle);
303 * Set core affinity of the current thread.
304 * Support both EAL and non-EAL thread and update TLS.
307 * Point to cpu_set_t for setting current thread affinity.
309 * On success, return 0; otherwise return -1;
311 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
314 * Get core affinity of the current thread.
317 * Point to cpu_set_t for getting current thread cpu affinity.
318 * It presumes input is not NULL, otherwise it causes panic.
321 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
326 * @note It fails with glibc < 2.12.
331 * Thread name to set.
333 * On success, return 0; otherwise return a negative value.
335 int rte_thread_setname(pthread_t id, const char *name);
340 * @note It fails with glibc < 2.12.
345 * Thread name to set.
347 * Thread name buffer length.
349 * On success, return 0; otherwise return a negative value.
352 int rte_thread_getname(pthread_t id, char *name, size_t len);
355 * Register current non-EAL thread as a lcore.
357 * @note This API is not compatible with the multi-process feature:
358 * - if a primary process registers a non-EAL thread, then no secondary process
360 * - if a secondary process initialises successfully, trying to register a
361 * non-EAL thread from either primary or secondary processes will always end
362 * up with the thread getting LCORE_ID_ANY as lcore.
365 * On success, return 0; otherwise return -1 with rte_errno set.
369 rte_thread_register(void);
372 * Unregister current thread and release lcore if one was associated.
376 rte_thread_unregister(void);
379 * Create a control thread.
381 * Wrapper to pthread_create(), pthread_setname_np() and
382 * pthread_setaffinity_np(). The affinity of the new thread is based
383 * on the CPU affinity retrieved at the time rte_eal_init() was called,
384 * the dataplane and service lcores are then excluded.
387 * Filled with the thread id of the new created thread.
389 * The name of the control thread (max 16 characters including '\0').
391 * Attributes for the new thread.
392 * @param start_routine
393 * Function to be executed by the new thread.
395 * Argument passed to start_routine.
397 * On success, returns 0; on error, it returns a negative value
398 * corresponding to the error number.
401 rte_ctrl_thread_create(pthread_t *thread, const char *name,
402 const pthread_attr_t *attr,
403 void *(*start_routine)(void *), void *arg);
410 #endif /* _RTE_LCORE_H_ */