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