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 #define RTE_CPU_OR(dst, src1, src2) CPU_OR(dst, src1, src2)
29 #define RTE_CPU_FILL(set) do \
33 for (i = 0; i < CPU_SETSIZE; i++) \
36 #define RTE_CPU_NOT(dst, src) do \
40 CPU_XOR(dst, &tmp, src); \
42 #elif defined(__FreeBSD__)
43 #include <pthread_np.h>
44 typedef cpuset_t rte_cpuset_t;
45 #define RTE_CPU_AND(dst, src1, src2) do \
48 CPU_COPY(src1, &tmp); \
49 CPU_AND(&tmp, src2); \
50 CPU_COPY(&tmp, dst); \
52 #define RTE_CPU_OR(dst, src1, src2) do \
55 CPU_COPY(src1, &tmp); \
57 CPU_COPY(&tmp, dst); \
59 #define RTE_CPU_FILL(set) CPU_FILL(set)
60 #define RTE_CPU_NOT(dst, src) do \
64 CPU_NAND(&tmp, src); \
65 CPU_COPY(&tmp, dst); \
70 * Structure storing internal configuration (per-lcore)
73 unsigned detected; /**< true if lcore was detected */
74 pthread_t thread_id; /**< pthread identifier */
75 int pipe_master2slave[2]; /**< communication pipe with master */
76 int pipe_slave2master[2]; /**< communication pipe with master */
77 lcore_function_t * volatile f; /**< function to call */
78 void * volatile arg; /**< argument of function */
79 volatile int ret; /**< return value of function */
80 volatile enum rte_lcore_state_t state; /**< lcore state */
81 unsigned socket_id; /**< physical socket id for this lcore */
82 unsigned core_id; /**< core number on socket for this lcore */
83 int core_index; /**< relative index, starting from 0 */
84 rte_cpuset_t cpuset; /**< cpu set which the lcore affinity to */
85 uint8_t core_role; /**< role of core eg: OFF, RTE, SERVICE */
89 * Internal configuration (per-lcore)
91 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
93 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */
94 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
97 * Return the Application thread ID of the execution unit.
99 * Note: in most cases the lcore id returned here will also correspond
100 * to the processor id of the CPU on which the thread is pinned, this
101 * will not be the case if the user has explicitly changed the thread to
102 * core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
103 * to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
106 * Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
108 static inline unsigned
111 return RTE_PER_LCORE(_lcore_id);
115 * Get the id of the master lcore
118 * the id of the master lcore
120 static inline unsigned
121 rte_get_master_lcore(void)
123 return rte_eal_get_configuration()->master_lcore;
127 * Return the number of execution units (lcores) on the system.
130 * the number of execution units (lcores) on the system.
132 static inline unsigned
133 rte_lcore_count(void)
135 const struct rte_config *cfg = rte_eal_get_configuration();
136 return cfg->lcore_count;
140 * Return the index of the lcore starting from zero.
142 * When option -c or -l is given, the index corresponds
143 * to the order in the list.
145 * -c 0x30, lcore 4 has index 0, and 5 has index 1.
146 * -l 22,18 lcore 22 has index 0, and 18 has index 1.
149 * The targeted lcore, or -1 for the current one.
151 * The relative index, or -1 if not enabled.
153 int rte_lcore_index(int lcore_id);
156 * Return the ID of the physical socket of the logical core we are
159 * the ID of current lcoreid's physical socket
161 unsigned int rte_socket_id(void);
164 * Return number of physical sockets detected on the system.
166 * Note that number of nodes may not be correspondent to their physical id's:
167 * for example, a system may report two socket id's, but the actual socket id's
171 * the number of physical sockets as recognized by EAL
174 rte_socket_count(void);
177 * Return socket id with a particular index.
179 * This will return socket id at a particular position in list of all detected
180 * physical socket id's. For example, on a machine with sockets [0, 8], passing
181 * 1 as a parameter will return 8.
184 * index of physical socket id to return
187 * - physical socket id as recognized by EAL
188 * - -1 on error, with errno set to EINVAL
191 rte_socket_id_by_idx(unsigned int idx);
194 * Get the ID of the physical socket of the specified lcore
197 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
199 * the ID of lcoreid's physical socket
202 rte_lcore_to_socket_id(unsigned int lcore_id);
206 * @b EXPERIMENTAL: this API may change without prior notice.
208 * Return the id of the lcore on a socket starting from zero.
211 * The targeted lcore, or -1 for the current one.
213 * The relative index, or -1 if not enabled.
217 rte_lcore_to_cpu_id(int lcore_id);
221 * @b EXPERIMENTAL: this API may change without prior notice.
223 * Return the cpuset for a given lcore.
225 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
227 * The cpuset of that lcore
231 rte_lcore_cpuset(unsigned int lcore_id);
234 * Test if an lcore is enabled.
237 * The identifier of the lcore, which MUST be between 0 and
240 * True if the given lcore is enabled; false otherwise.
243 rte_lcore_is_enabled(unsigned int lcore_id)
245 struct rte_config *cfg = rte_eal_get_configuration();
246 if (lcore_id >= RTE_MAX_LCORE)
248 return cfg->lcore_role[lcore_id] == ROLE_RTE;
252 * Get the next enabled lcore ID.
255 * The current lcore (reference).
257 * If true, do not return the ID of the master lcore.
259 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
260 * return RTE_MAX_LCORE.
262 * The next lcore_id or RTE_MAX_LCORE if not found.
264 static inline unsigned int
265 rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
271 while (i < RTE_MAX_LCORE) {
272 if (!rte_lcore_is_enabled(i) ||
273 (skip_master && (i == rte_get_master_lcore()))) {
284 * Macro to browse all running lcores.
286 #define RTE_LCORE_FOREACH(i) \
287 for (i = rte_get_next_lcore(-1, 0, 0); \
289 i = rte_get_next_lcore(i, 0, 0))
292 * Macro to browse all running lcores except the master lcore.
294 #define RTE_LCORE_FOREACH_SLAVE(i) \
295 for (i = rte_get_next_lcore(-1, 1, 0); \
297 i = rte_get_next_lcore(i, 1, 0))
300 * Set core affinity of the current thread.
301 * Support both EAL and non-EAL thread and update TLS.
304 * Point to cpu_set_t for setting current thread affinity.
306 * On success, return 0; otherwise return -1;
308 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
311 * Get core affinity of the current thread.
314 * Point to cpu_set_t for getting current thread cpu affinity.
315 * It presumes input is not NULL, otherwise it causes panic.
318 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
323 * @note It fails with glibc < 2.12.
328 * Thread name to set.
330 * On success, return 0; otherwise return a negative value.
332 int rte_thread_setname(pthread_t id, const char *name);
335 * Create a control thread.
337 * Wrapper to pthread_create(), pthread_setname_np() and
338 * pthread_setaffinity_np(). The affinity of the new thread is based
339 * on the CPU affinity retrieved at the time rte_eal_init() was called,
340 * the dataplane and service lcores are then excluded.
343 * Filled with the thread id of the new created thread.
345 * The name of the control thread (max 16 characters including '\0').
347 * Attributes for the new thread.
348 * @param start_routine
349 * Function to be executed by the new thread.
351 * Argument passed to start_routine.
353 * On success, returns 0; on error, it returns a negative value
354 * corresponding to the error number.
357 rte_ctrl_thread_create(pthread_t *thread, const char *name,
358 const pthread_attr_t *attr,
359 void *(*start_routine)(void *), void *arg);
362 * Test if the core supplied has a specific role
365 * The identifier of the lcore, which MUST be between 0 and
368 * The role to be checked against.
370 * Boolean value: positive if test is true; otherwise returns 0.
373 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
380 #endif /* _RTE_LCORE_H_ */