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 #elif defined(__FreeBSD__)
28 #include <pthread_np.h>
29 typedef cpuset_t rte_cpuset_t;
33 * Structure storing internal configuration (per-lcore)
36 unsigned detected; /**< true if lcore was detected */
37 pthread_t thread_id; /**< pthread identifier */
38 int pipe_master2slave[2]; /**< communication pipe with master */
39 int pipe_slave2master[2]; /**< communication pipe with master */
40 lcore_function_t * volatile f; /**< function to call */
41 void * volatile arg; /**< argument of function */
42 volatile int ret; /**< return value of function */
43 volatile enum rte_lcore_state_t state; /**< lcore state */
44 unsigned socket_id; /**< physical socket id for this lcore */
45 unsigned core_id; /**< core number on socket for this lcore */
46 int core_index; /**< relative index, starting from 0 */
47 rte_cpuset_t cpuset; /**< cpu set which the lcore affinity to */
48 uint8_t core_role; /**< role of core eg: OFF, RTE, SERVICE */
52 * Internal configuration (per-lcore)
54 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
56 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
57 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
60 * Return the ID of the execution unit we are running on.
62 * Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
64 static inline unsigned
67 return RTE_PER_LCORE(_lcore_id);
71 * Get the id of the master lcore
74 * the id of the master lcore
76 static inline unsigned
77 rte_get_master_lcore(void)
79 return rte_eal_get_configuration()->master_lcore;
83 * Return the number of execution units (lcores) on the system.
86 * the number of execution units (lcores) on the system.
88 static inline unsigned
91 const struct rte_config *cfg = rte_eal_get_configuration();
92 return cfg->lcore_count;
96 * Return the index of the lcore starting from zero.
97 * The order is physical or given by command line (-l option).
100 * The targeted lcore, or -1 for the current one.
102 * The relative index, or -1 if not enabled.
105 rte_lcore_index(int lcore_id)
107 if (lcore_id >= RTE_MAX_LCORE)
110 lcore_id = rte_lcore_id();
111 return lcore_config[lcore_id].core_index;
115 * Return the ID of the physical socket of the logical core we are
118 * the ID of current lcoreid's physical socket
120 unsigned rte_socket_id(void);
123 * Get the ID of the physical socket of the specified lcore
126 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
128 * the ID of lcoreid's physical socket
130 static inline unsigned
131 rte_lcore_to_socket_id(unsigned lcore_id)
133 return lcore_config[lcore_id].socket_id;
137 * Test if an lcore is enabled.
140 * The identifier of the lcore, which MUST be between 0 and
143 * True if the given lcore is enabled; false otherwise.
146 rte_lcore_is_enabled(unsigned lcore_id)
148 struct rte_config *cfg = rte_eal_get_configuration();
149 if (lcore_id >= RTE_MAX_LCORE)
151 return cfg->lcore_role[lcore_id] == ROLE_RTE;
155 * Get the next enabled lcore ID.
158 * The current lcore (reference).
160 * If true, do not return the ID of the master lcore.
162 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
163 * return RTE_MAX_LCORE.
165 * The next lcore_id or RTE_MAX_LCORE if not found.
167 static inline unsigned
168 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
174 while (i < RTE_MAX_LCORE) {
175 if (!rte_lcore_is_enabled(i) ||
176 (skip_master && (i == rte_get_master_lcore()))) {
187 * Macro to browse all running lcores.
189 #define RTE_LCORE_FOREACH(i) \
190 for (i = rte_get_next_lcore(-1, 0, 0); \
192 i = rte_get_next_lcore(i, 0, 0))
195 * Macro to browse all running lcores except the master lcore.
197 #define RTE_LCORE_FOREACH_SLAVE(i) \
198 for (i = rte_get_next_lcore(-1, 1, 0); \
200 i = rte_get_next_lcore(i, 1, 0))
203 * Set core affinity of the current thread.
204 * Support both EAL and non-EAL thread and update TLS.
207 * Point to cpu_set_t for setting current thread affinity.
209 * On success, return 0; otherwise return -1;
211 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
214 * Get core affinity of the current thread.
217 * Point to cpu_set_t for getting current thread cpu affinity.
218 * It presumes input is not NULL, otherwise it causes panic.
221 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
226 * @note It fails with glibc < 2.12.
231 * Thread name to set.
233 * On success, return 0; otherwise return a negative value.
235 int rte_thread_setname(pthread_t id, const char *name);
238 * Test if the core supplied has a specific role
241 * The identifier of the lcore, which MUST be between 0 and
244 * The role to be checked against.
246 * On success, return 0; otherwise return a negative value.
249 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
256 #endif /* _RTE_LCORE_H_ */