eal: fix control thread affinity with --lcores
[dpdk.git] / lib / librte_eal / common / include / rte_lcore.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_LCORE_H_
6 #define _RTE_LCORE_H_
7
8 /**
9  * @file
10  *
11  * API for lcore and socket manipulation
12  *
13  */
14 #include <rte_config.h>
15 #include <rte_per_lcore.h>
16 #include <rte_eal.h>
17 #include <rte_launch.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 #define LCORE_ID_ANY     UINT32_MAX       /**< Any lcore. */
24
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 \
30 { \
31         unsigned int i; \
32         CPU_ZERO(set); \
33         for (i = 0; i < CPU_SETSIZE; i++) \
34                 CPU_SET(i, set); \
35 } while (0)
36 #define RTE_CPU_NOT(dst, src) do \
37 { \
38         cpu_set_t tmp; \
39         RTE_CPU_FILL(&tmp); \
40         CPU_XOR(dst, &tmp, src); \
41 } while (0)
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 \
46 { \
47         cpuset_t tmp; \
48         CPU_COPY(src1, &tmp); \
49         CPU_AND(&tmp, src2); \
50         CPU_COPY(&tmp, dst); \
51 } while (0)
52 #define RTE_CPU_OR(dst, src1, src2) do \
53 { \
54         cpuset_t tmp; \
55         CPU_COPY(src1, &tmp); \
56         CPU_OR(&tmp, src2); \
57         CPU_COPY(&tmp, dst); \
58 } while (0)
59 #define RTE_CPU_FILL(set) CPU_FILL(set)
60 #define RTE_CPU_NOT(dst, src) do \
61 { \
62         cpuset_t tmp; \
63         CPU_FILL(&tmp); \
64         CPU_NAND(&tmp, src); \
65         CPU_COPY(&tmp, dst); \
66 } while (0)
67 #endif
68
69 /**
70  * Structure storing internal configuration (per-lcore)
71  */
72 struct lcore_config {
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 */
86 };
87
88 /**
89  * Internal configuration (per-lcore)
90  */
91 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
92
93 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
94 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
95
96 /**
97  * Return the Application thread ID of the execution unit.
98  *
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..
104  *
105  * @return
106  *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
107  */
108 static inline unsigned
109 rte_lcore_id(void)
110 {
111         return RTE_PER_LCORE(_lcore_id);
112 }
113
114 /**
115  * Get the id of the master lcore
116  *
117  * @return
118  *   the id of the master lcore
119  */
120 static inline unsigned
121 rte_get_master_lcore(void)
122 {
123         return rte_eal_get_configuration()->master_lcore;
124 }
125
126 /**
127  * Return the number of execution units (lcores) on the system.
128  *
129  * @return
130  *   the number of execution units (lcores) on the system.
131  */
132 static inline unsigned
133 rte_lcore_count(void)
134 {
135         const struct rte_config *cfg = rte_eal_get_configuration();
136         return cfg->lcore_count;
137 }
138
139 /**
140  * Return the index of the lcore starting from zero.
141  *
142  * When option -c or -l is given, the index corresponds
143  * to the order in the list.
144  * For example:
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.
147  *
148  * @param lcore_id
149  *   The targeted lcore, or -1 for the current one.
150  * @return
151  *   The relative index, or -1 if not enabled.
152  */
153 int rte_lcore_index(int lcore_id);
154
155 /**
156  * Return the ID of the physical socket of the logical core we are
157  * running on.
158  * @return
159  *   the ID of current lcoreid's physical socket
160  */
161 unsigned int rte_socket_id(void);
162
163 /**
164  * Return number of physical sockets detected on the system.
165  *
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
168  * may be 0 and 8.
169  *
170  * @return
171  *   the number of physical sockets as recognized by EAL
172  */
173 unsigned int
174 rte_socket_count(void);
175
176 /**
177  * Return socket id with a particular index.
178  *
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.
182  *
183  * @param idx
184  *   index of physical socket id to return
185  *
186  * @return
187  *   - physical socket id as recognized by EAL
188  *   - -1 on error, with errno set to EINVAL
189  */
190 int
191 rte_socket_id_by_idx(unsigned int idx);
192
193 /**
194  * Get the ID of the physical socket of the specified lcore
195  *
196  * @param lcore_id
197  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
198  * @return
199  *   the ID of lcoreid's physical socket
200  */
201 unsigned int
202 rte_lcore_to_socket_id(unsigned int lcore_id);
203
204 /**
205  * @warning
206  * @b EXPERIMENTAL: this API may change without prior notice.
207  *
208  * Return the id of the lcore on a socket starting from zero.
209  *
210  * @param lcore_id
211  *   The targeted lcore, or -1 for the current one.
212  * @return
213  *   The relative index, or -1 if not enabled.
214  */
215 __rte_experimental
216 int
217 rte_lcore_to_cpu_id(int lcore_id);
218
219 /**
220  * @warning
221  * @b EXPERIMENTAL: this API may change without prior notice.
222  *
223  * Return the cpuset for a given lcore.
224  * @param lcore_id
225  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
226  * @return
227  *   The cpuset of that lcore
228  */
229 __rte_experimental
230 rte_cpuset_t
231 rte_lcore_cpuset(unsigned int lcore_id);
232
233 /**
234  * Test if an lcore is enabled.
235  *
236  * @param lcore_id
237  *   The identifier of the lcore, which MUST be between 0 and
238  *   RTE_MAX_LCORE-1.
239  * @return
240  *   True if the given lcore is enabled; false otherwise.
241  */
242 static inline int
243 rte_lcore_is_enabled(unsigned int lcore_id)
244 {
245         struct rte_config *cfg = rte_eal_get_configuration();
246         if (lcore_id >= RTE_MAX_LCORE)
247                 return 0;
248         return cfg->lcore_role[lcore_id] == ROLE_RTE;
249 }
250
251 /**
252  * Get the next enabled lcore ID.
253  *
254  * @param i
255  *   The current lcore (reference).
256  * @param skip_master
257  *   If true, do not return the ID of the master lcore.
258  * @param wrap
259  *   If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
260  *   return RTE_MAX_LCORE.
261  * @return
262  *   The next lcore_id or RTE_MAX_LCORE if not found.
263  */
264 static inline unsigned int
265 rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
266 {
267         i++;
268         if (wrap)
269                 i %= RTE_MAX_LCORE;
270
271         while (i < RTE_MAX_LCORE) {
272                 if (!rte_lcore_is_enabled(i) ||
273                     (skip_master && (i == rte_get_master_lcore()))) {
274                         i++;
275                         if (wrap)
276                                 i %= RTE_MAX_LCORE;
277                         continue;
278                 }
279                 break;
280         }
281         return i;
282 }
283 /**
284  * Macro to browse all running lcores.
285  */
286 #define RTE_LCORE_FOREACH(i)                                            \
287         for (i = rte_get_next_lcore(-1, 0, 0);                          \
288              i<RTE_MAX_LCORE;                                           \
289              i = rte_get_next_lcore(i, 0, 0))
290
291 /**
292  * Macro to browse all running lcores except the master lcore.
293  */
294 #define RTE_LCORE_FOREACH_SLAVE(i)                                      \
295         for (i = rte_get_next_lcore(-1, 1, 0);                          \
296              i<RTE_MAX_LCORE;                                           \
297              i = rte_get_next_lcore(i, 1, 0))
298
299 /**
300  * Set core affinity of the current thread.
301  * Support both EAL and non-EAL thread and update TLS.
302  *
303  * @param cpusetp
304  *   Point to cpu_set_t for setting current thread affinity.
305  * @return
306  *   On success, return 0; otherwise return -1;
307  */
308 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
309
310 /**
311  * Get core affinity of the current thread.
312  *
313  * @param cpusetp
314  *   Point to cpu_set_t for getting current thread cpu affinity.
315  *   It presumes input is not NULL, otherwise it causes panic.
316  *
317  */
318 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
319
320 /**
321  * Set thread names.
322  *
323  * @note It fails with glibc < 2.12.
324  *
325  * @param id
326  *   Thread id.
327  * @param name
328  *   Thread name to set.
329  * @return
330  *   On success, return 0; otherwise return a negative value.
331  */
332 int rte_thread_setname(pthread_t id, const char *name);
333
334 /**
335  * Create a control thread.
336  *
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.
341  *
342  * @param thread
343  *   Filled with the thread id of the new created thread.
344  * @param name
345  *   The name of the control thread (max 16 characters including '\0').
346  * @param attr
347  *   Attributes for the new thread.
348  * @param start_routine
349  *   Function to be executed by the new thread.
350  * @param arg
351  *   Argument passed to start_routine.
352  * @return
353  *   On success, returns 0; on error, it returns a negative value
354  *   corresponding to the error number.
355  */
356 int
357 rte_ctrl_thread_create(pthread_t *thread, const char *name,
358                 const pthread_attr_t *attr,
359                 void *(*start_routine)(void *), void *arg);
360
361 /**
362  * Test if the core supplied has a specific role
363  *
364  * @param lcore_id
365  *   The identifier of the lcore, which MUST be between 0 and
366  *   RTE_MAX_LCORE-1.
367  * @param role
368  *   The role to be checked against.
369  * @return
370  *   Boolean value: positive if test is true; otherwise returns 0.
371  */
372 int
373 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
374
375 #ifdef __cplusplus
376 }
377 #endif
378
379
380 #endif /* _RTE_LCORE_H_ */