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 #if defined(__linux__)
26 typedef cpu_set_t rte_cpuset_t;
27 #define RTE_CPU_AND(dst, src1, src2) CPU_AND(dst, src1, src2)
28 #elif defined(__FreeBSD__)
29 #include <pthread_np.h>
30 typedef cpuset_t rte_cpuset_t;
31 #define RTE_CPU_AND(dst, src1, src2) do \
34 CPU_COPY(src1, &tmp); \
35 CPU_AND(&tmp, src2); \
36 CPU_COPY(&tmp, dst); \
41 * Structure storing internal configuration (per-lcore)
44 unsigned detected; /**< true if lcore was detected */
45 pthread_t thread_id; /**< pthread identifier */
46 int pipe_master2slave[2]; /**< communication pipe with master */
47 int pipe_slave2master[2]; /**< communication pipe with master */
48 lcore_function_t * volatile f; /**< function to call */
49 void * volatile arg; /**< argument of function */
50 volatile int ret; /**< return value of function */
51 volatile enum rte_lcore_state_t state; /**< lcore state */
52 unsigned socket_id; /**< physical socket id for this lcore */
53 unsigned core_id; /**< core number on socket for this lcore */
54 int core_index; /**< relative index, starting from 0 */
55 rte_cpuset_t cpuset; /**< cpu set which the lcore affinity to */
56 uint8_t core_role; /**< role of core eg: OFF, RTE, SERVICE */
60 * Internal configuration (per-lcore)
62 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
64 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
65 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
68 * Return the Application thread ID of the execution unit.
70 * Note: in most cases the lcore id returned here will also correspond
71 * to the processor id of the CPU on which the thread is pinned, this
72 * will not be the case if the user has explicitly changed the thread to
73 * core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
74 * to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
77 * Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
79 static inline unsigned
82 return RTE_PER_LCORE(_lcore_id);
86 * Get the id of the master lcore
89 * the id of the master lcore
91 static inline unsigned
92 rte_get_master_lcore(void)
94 return rte_eal_get_configuration()->master_lcore;
98 * Return the number of execution units (lcores) on the system.
101 * the number of execution units (lcores) on the system.
103 static inline unsigned
104 rte_lcore_count(void)
106 const struct rte_config *cfg = rte_eal_get_configuration();
107 return cfg->lcore_count;
111 * Return the index of the lcore starting from zero.
113 * When option -c or -l is given, the index corresponds
114 * to the order in the list.
116 * -c 0x30, lcore 4 has index 0, and 5 has index 1.
117 * -l 22,18 lcore 22 has index 0, and 18 has index 1.
120 * The targeted lcore, or -1 for the current one.
122 * The relative index, or -1 if not enabled.
124 int rte_lcore_index(int lcore_id);
127 * Return the ID of the physical socket of the logical core we are
130 * the ID of current lcoreid's physical socket
132 unsigned int rte_socket_id(void);
135 * Return number of physical sockets detected on the system.
137 * Note that number of nodes may not be correspondent to their physical id's:
138 * for example, a system may report two socket id's, but the actual socket id's
142 * the number of physical sockets as recognized by EAL
145 rte_socket_count(void);
148 * Return socket id with a particular index.
150 * This will return socket id at a particular position in list of all detected
151 * physical socket id's. For example, on a machine with sockets [0, 8], passing
152 * 1 as a parameter will return 8.
155 * index of physical socket id to return
158 * - physical socket id as recognized by EAL
159 * - -1 on error, with errno set to EINVAL
162 rte_socket_id_by_idx(unsigned int idx);
165 * Get the ID of the physical socket of the specified lcore
168 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
170 * the ID of lcoreid's physical socket
173 rte_lcore_to_socket_id(unsigned int lcore_id);
177 * @b EXPERIMENTAL: this API may change without prior notice.
179 * Return the id of the lcore on a socket starting from zero.
182 * The targeted lcore, or -1 for the current one.
184 * The relative index, or -1 if not enabled.
186 __rte_experimental int
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
199 __rte_experimental rte_cpuset_t
200 rte_lcore_cpuset(unsigned int lcore_id);
203 * Test if an lcore is enabled.
206 * The identifier of the lcore, which MUST be between 0 and
209 * True if the given lcore is enabled; false otherwise.
212 rte_lcore_is_enabled(unsigned int lcore_id)
214 struct rte_config *cfg = rte_eal_get_configuration();
215 if (lcore_id >= RTE_MAX_LCORE)
217 return cfg->lcore_role[lcore_id] == ROLE_RTE;
221 * Get the next enabled lcore ID.
224 * The current lcore (reference).
226 * If true, do not return the ID of the master lcore.
228 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
229 * return RTE_MAX_LCORE.
231 * The next lcore_id or RTE_MAX_LCORE if not found.
233 static inline unsigned int
234 rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
240 while (i < RTE_MAX_LCORE) {
241 if (!rte_lcore_is_enabled(i) ||
242 (skip_master && (i == rte_get_master_lcore()))) {
253 * Macro to browse all running lcores.
255 #define RTE_LCORE_FOREACH(i) \
256 for (i = rte_get_next_lcore(-1, 0, 0); \
258 i = rte_get_next_lcore(i, 0, 0))
261 * Macro to browse all running lcores except the master lcore.
263 #define RTE_LCORE_FOREACH_SLAVE(i) \
264 for (i = rte_get_next_lcore(-1, 1, 0); \
266 i = rte_get_next_lcore(i, 1, 0))
269 * Set core affinity of the current thread.
270 * Support both EAL and non-EAL thread and update TLS.
273 * Point to cpu_set_t for setting current thread affinity.
275 * On success, return 0; otherwise return -1;
277 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
280 * Get core affinity of the current thread.
283 * Point to cpu_set_t for getting current thread cpu affinity.
284 * It presumes input is not NULL, otherwise it causes panic.
287 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
292 * @note It fails with glibc < 2.12.
297 * Thread name to set.
299 * On success, return 0; otherwise return a negative value.
301 int rte_thread_setname(pthread_t id, const char *name);
304 * Create a control thread.
306 * Wrapper to pthread_create(), pthread_setname_np() and
307 * pthread_setaffinity_np(). The affinity of the new thread is based
308 * on the CPU affinity retrieved at the time rte_eal_init() was called,
309 * the dataplane and service lcores are then excluded.
312 * Filled with the thread id of the new created thread.
314 * The name of the control thread (max 16 characters including '\0').
316 * Attributes for the new thread.
317 * @param start_routine
318 * Function to be executed by the new thread.
320 * Argument passed to start_routine.
322 * On success, returns 0; on error, it returns a negative value
323 * corresponding to the error number.
326 rte_ctrl_thread_create(pthread_t *thread, const char *name,
327 const pthread_attr_t *attr,
328 void *(*start_routine)(void *), void *arg);
331 * Test if the core supplied has a specific role
334 * The identifier of the lcore, which MUST be between 0 and
337 * The role to be checked against.
339 * Boolean value: positive if test is true; otherwise returns 0.
342 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
349 #endif /* _RTE_LCORE_H_ */