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