eal: set affinity for control threads
[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 #elif defined(__FreeBSD__)
28 #include <pthread_np.h>
29         typedef cpuset_t rte_cpuset_t;
30 #endif
31
32 /**
33  * Structure storing internal configuration (per-lcore)
34  */
35 struct lcore_config {
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 */
49 };
50
51 /**
52  * Internal configuration (per-lcore)
53  */
54 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
55
56 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
57 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
58
59 /**
60  * Return the Application thread ID of the execution unit.
61  *
62  * Note: in most cases the lcore id returned here will also correspond
63  *   to the processor id of the CPU on which the thread is pinned, this
64  *   will not be the case if the user has explicitly changed the thread to
65  *   core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
66  *   to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
67  *
68  * @return
69  *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
70  */
71 static inline unsigned
72 rte_lcore_id(void)
73 {
74         return RTE_PER_LCORE(_lcore_id);
75 }
76
77 /**
78  * Get the id of the master lcore
79  *
80  * @return
81  *   the id of the master lcore
82  */
83 static inline unsigned
84 rte_get_master_lcore(void)
85 {
86         return rte_eal_get_configuration()->master_lcore;
87 }
88
89 /**
90  * Return the number of execution units (lcores) on the system.
91  *
92  * @return
93  *   the number of execution units (lcores) on the system.
94  */
95 static inline unsigned
96 rte_lcore_count(void)
97 {
98         const struct rte_config *cfg = rte_eal_get_configuration();
99         return cfg->lcore_count;
100 }
101
102 /**
103  * Return the index of the lcore starting from zero.
104  *
105  * When option -c or -l is given, the index corresponds
106  * to the order in the list.
107  * For example:
108  * -c 0x30, lcore 4 has index 0, and 5 has index 1.
109  * -l 22,18 lcore 22 has index 0, and 18 has index 1.
110  *
111  * @param lcore_id
112  *   The targeted lcore, or -1 for the current one.
113  * @return
114  *   The relative index, or -1 if not enabled.
115  */
116 static inline int
117 rte_lcore_index(int lcore_id)
118 {
119         if (lcore_id >= RTE_MAX_LCORE)
120                 return -1;
121         if (lcore_id < 0)
122                 lcore_id = rte_lcore_id();
123         return lcore_config[lcore_id].core_index;
124 }
125
126 /**
127  * Return the ID of the physical socket of the logical core we are
128  * running on.
129  * @return
130  *   the ID of current lcoreid's physical socket
131  */
132 unsigned rte_socket_id(void);
133
134 /**
135  * Return number of physical sockets detected on the system.
136  *
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
139  * may be 0 and 8.
140  *
141  * @return
142  *   the number of physical sockets as recognized by EAL
143  */
144 unsigned int __rte_experimental
145 rte_socket_count(void);
146
147 /**
148  * Return socket id with a particular index.
149  *
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.
153  *
154  * @param idx
155  *   index of physical socket id to return
156  *
157  * @return
158  *   - physical socket id as recognized by EAL
159  *   - -1 on error, with errno set to EINVAL
160  */
161 int __rte_experimental
162 rte_socket_id_by_idx(unsigned int idx);
163
164 /**
165  * Get the ID of the physical socket of the specified lcore
166  *
167  * @param lcore_id
168  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
169  * @return
170  *   the ID of lcoreid's physical socket
171  */
172 static inline unsigned
173 rte_lcore_to_socket_id(unsigned lcore_id)
174 {
175         return lcore_config[lcore_id].socket_id;
176 }
177
178 /**
179  * Test if an lcore is enabled.
180  *
181  * @param lcore_id
182  *   The identifier of the lcore, which MUST be between 0 and
183  *   RTE_MAX_LCORE-1.
184  * @return
185  *   True if the given lcore is enabled; false otherwise.
186  */
187 static inline int
188 rte_lcore_is_enabled(unsigned lcore_id)
189 {
190         struct rte_config *cfg = rte_eal_get_configuration();
191         if (lcore_id >= RTE_MAX_LCORE)
192                 return 0;
193         return cfg->lcore_role[lcore_id] == ROLE_RTE;
194 }
195
196 /**
197  * Get the next enabled lcore ID.
198  *
199  * @param i
200  *   The current lcore (reference).
201  * @param skip_master
202  *   If true, do not return the ID of the master lcore.
203  * @param wrap
204  *   If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
205  *   return RTE_MAX_LCORE.
206  * @return
207  *   The next lcore_id or RTE_MAX_LCORE if not found.
208  */
209 static inline unsigned
210 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
211 {
212         i++;
213         if (wrap)
214                 i %= RTE_MAX_LCORE;
215
216         while (i < RTE_MAX_LCORE) {
217                 if (!rte_lcore_is_enabled(i) ||
218                     (skip_master && (i == rte_get_master_lcore()))) {
219                         i++;
220                         if (wrap)
221                                 i %= RTE_MAX_LCORE;
222                         continue;
223                 }
224                 break;
225         }
226         return i;
227 }
228 /**
229  * Macro to browse all running lcores.
230  */
231 #define RTE_LCORE_FOREACH(i)                                            \
232         for (i = rte_get_next_lcore(-1, 0, 0);                          \
233              i<RTE_MAX_LCORE;                                           \
234              i = rte_get_next_lcore(i, 0, 0))
235
236 /**
237  * Macro to browse all running lcores except the master lcore.
238  */
239 #define RTE_LCORE_FOREACH_SLAVE(i)                                      \
240         for (i = rte_get_next_lcore(-1, 1, 0);                          \
241              i<RTE_MAX_LCORE;                                           \
242              i = rte_get_next_lcore(i, 1, 0))
243
244 /**
245  * Set core affinity of the current thread.
246  * Support both EAL and non-EAL thread and update TLS.
247  *
248  * @param cpusetp
249  *   Point to cpu_set_t for setting current thread affinity.
250  * @return
251  *   On success, return 0; otherwise return -1;
252  */
253 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
254
255 /**
256  * Get core affinity of the current thread.
257  *
258  * @param cpusetp
259  *   Point to cpu_set_t for getting current thread cpu affinity.
260  *   It presumes input is not NULL, otherwise it causes panic.
261  *
262  */
263 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
264
265 /**
266  * Set thread names.
267  *
268  * @note It fails with glibc < 2.12.
269  *
270  * @param id
271  *   Thread id.
272  * @param name
273  *   Thread name to set.
274  * @return
275  *   On success, return 0; otherwise return a negative value.
276  */
277 int rte_thread_setname(pthread_t id, const char *name);
278
279 /**
280  * Create a control thread.
281  *
282  * Wrapper to pthread_create(), pthread_setname_np() and
283  * pthread_setaffinity_np(). The dataplane and service lcores are
284  * excluded from the affinity of the new thread.
285  *
286  * @param thread
287  *   Filled with the thread id of the new created thread.
288  * @param name
289  *   The name of the control thread (max 16 characters including '\0').
290  * @param attr
291  *   Attributes for the new thread.
292  * @param start_routine
293  *   Function to be executed by the new thread.
294  * @param arg
295  *   Argument passed to start_routine.
296  * @return
297  *   On success, returns 0; on error, it returns a negative value
298  *   corresponding to the error number.
299  */
300 __rte_experimental int
301 rte_ctrl_thread_create(pthread_t *thread, const char *name,
302                 const pthread_attr_t *attr,
303                 void *(*start_routine)(void *), void *arg);
304
305 /**
306  * Test if the core supplied has a specific role
307  *
308  * @param lcore_id
309  *   The identifier of the lcore, which MUST be between 0 and
310  *   RTE_MAX_LCORE-1.
311  * @param role
312  *   The role to be checked against.
313  * @return
314  *   On success, return 0; otherwise return a negative value.
315  */
316 int
317 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
318
319 #ifdef __cplusplus
320 }
321 #endif
322
323
324 #endif /* _RTE_LCORE_H_ */