1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * API for lcore and socket manipulation
14 #include <rte_per_lcore.h>
16 #include <rte_launch.h>
22 #define LCORE_ID_ANY UINT32_MAX /**< Any lcore. */
24 #if defined(__linux__)
25 typedef cpu_set_t rte_cpuset_t;
26 #elif defined(__FreeBSD__)
27 #include <pthread_np.h>
28 typedef cpuset_t rte_cpuset_t;
32 * Structure storing internal configuration (per-lcore)
35 unsigned detected; /**< true if lcore was detected */
36 pthread_t thread_id; /**< pthread identifier */
37 int pipe_master2slave[2]; /**< communication pipe with master */
38 int pipe_slave2master[2]; /**< communication pipe with master */
39 lcore_function_t * volatile f; /**< function to call */
40 void * volatile arg; /**< argument of function */
41 volatile int ret; /**< return value of function */
42 volatile enum rte_lcore_state_t state; /**< lcore state */
43 unsigned socket_id; /**< physical socket id for this lcore */
44 unsigned core_id; /**< core number on socket for this lcore */
45 int core_index; /**< relative index, starting from 0 */
46 rte_cpuset_t cpuset; /**< cpu set which the lcore affinity to */
47 uint8_t core_role; /**< role of core eg: OFF, RTE, SERVICE */
51 * Internal configuration (per-lcore)
53 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
55 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
56 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
59 * Return the ID of the execution unit we are running on.
61 * Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
63 static inline unsigned
66 return RTE_PER_LCORE(_lcore_id);
70 * Get the id of the master lcore
73 * the id of the master lcore
75 static inline unsigned
76 rte_get_master_lcore(void)
78 return rte_eal_get_configuration()->master_lcore;
82 * Return the number of execution units (lcores) on the system.
85 * the number of execution units (lcores) on the system.
87 static inline unsigned
90 const struct rte_config *cfg = rte_eal_get_configuration();
91 return cfg->lcore_count;
95 * Return the index of the lcore starting from zero.
96 * The order is physical or given by command line (-l option).
99 * The targeted lcore, or -1 for the current one.
101 * The relative index, or -1 if not enabled.
104 rte_lcore_index(int lcore_id)
106 if (lcore_id >= RTE_MAX_LCORE)
109 lcore_id = rte_lcore_id();
110 return lcore_config[lcore_id].core_index;
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 rte_socket_id(void);
122 * Get the ID of the physical socket of the specified lcore
125 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
127 * the ID of lcoreid's physical socket
129 static inline unsigned
130 rte_lcore_to_socket_id(unsigned lcore_id)
132 return lcore_config[lcore_id].socket_id;
136 * Test if an lcore is enabled.
139 * The identifier of the lcore, which MUST be between 0 and
142 * True if the given lcore is enabled; false otherwise.
145 rte_lcore_is_enabled(unsigned lcore_id)
147 struct rte_config *cfg = rte_eal_get_configuration();
148 if (lcore_id >= RTE_MAX_LCORE)
150 return cfg->lcore_role[lcore_id] == ROLE_RTE;
154 * Get the next enabled lcore ID.
157 * The current lcore (reference).
159 * If true, do not return the ID of the master lcore.
161 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
162 * return RTE_MAX_LCORE.
164 * The next lcore_id or RTE_MAX_LCORE if not found.
166 static inline unsigned
167 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
173 while (i < RTE_MAX_LCORE) {
174 if (!rte_lcore_is_enabled(i) ||
175 (skip_master && (i == rte_get_master_lcore()))) {
186 * Macro to browse all running lcores.
188 #define RTE_LCORE_FOREACH(i) \
189 for (i = rte_get_next_lcore(-1, 0, 0); \
191 i = rte_get_next_lcore(i, 0, 0))
194 * Macro to browse all running lcores except the master lcore.
196 #define RTE_LCORE_FOREACH_SLAVE(i) \
197 for (i = rte_get_next_lcore(-1, 1, 0); \
199 i = rte_get_next_lcore(i, 1, 0))
202 * Set core affinity of the current thread.
203 * Support both EAL and non-EAL thread and update TLS.
206 * Point to cpu_set_t for setting current thread affinity.
208 * On success, return 0; otherwise return -1;
210 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
213 * Get core affinity of the current thread.
216 * Point to cpu_set_t for getting current thread cpu affinity.
217 * It presumes input is not NULL, otherwise it causes panic.
220 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
225 * @note It fails with glibc < 2.12.
230 * Thread name to set.
232 * On success, return 0; otherwise return a negative value.
234 int rte_thread_setname(pthread_t id, const char *name);
237 * Test if the core supplied has a specific role
240 * The identifier of the lcore, which MUST be between 0 and
243 * The role to be checked against.
245 * On success, return 0; otherwise return a negative value.
248 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
255 #endif /* _RTE_LCORE_H_ */