eal: make lcore config private
[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 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
26 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
27
28 /**
29  * Return the Application thread ID of the execution unit.
30  *
31  * Note: in most cases the lcore id returned here will also correspond
32  *   to the processor id of the CPU on which the thread is pinned, this
33  *   will not be the case if the user has explicitly changed the thread to
34  *   core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
35  *   to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
36  *
37  * @return
38  *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
39  */
40 static inline unsigned
41 rte_lcore_id(void)
42 {
43         return RTE_PER_LCORE(_lcore_id);
44 }
45
46 /**
47  * Get the id of the master lcore
48  *
49  * @return
50  *   the id of the master lcore
51  */
52 static inline unsigned
53 rte_get_master_lcore(void)
54 {
55         return rte_eal_get_configuration()->master_lcore;
56 }
57
58 /**
59  * Return the number of execution units (lcores) on the system.
60  *
61  * @return
62  *   the number of execution units (lcores) on the system.
63  */
64 static inline unsigned
65 rte_lcore_count(void)
66 {
67         const struct rte_config *cfg = rte_eal_get_configuration();
68         return cfg->lcore_count;
69 }
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 static inline int
175 rte_lcore_is_enabled(unsigned int lcore_id)
176 {
177         struct rte_config *cfg = rte_eal_get_configuration();
178         if (lcore_id >= RTE_MAX_LCORE)
179                 return 0;
180         return cfg->lcore_role[lcore_id] == ROLE_RTE;
181 }
182
183 /**
184  * Get the next enabled lcore ID.
185  *
186  * @param i
187  *   The current lcore (reference).
188  * @param skip_master
189  *   If true, do not return the ID of the master lcore.
190  * @param wrap
191  *   If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
192  *   return RTE_MAX_LCORE.
193  * @return
194  *   The next lcore_id or RTE_MAX_LCORE if not found.
195  */
196 static inline unsigned int
197 rte_get_next_lcore(unsigned int i, int skip_master, int wrap)
198 {
199         i++;
200         if (wrap)
201                 i %= RTE_MAX_LCORE;
202
203         while (i < RTE_MAX_LCORE) {
204                 if (!rte_lcore_is_enabled(i) ||
205                     (skip_master && (i == rte_get_master_lcore()))) {
206                         i++;
207                         if (wrap)
208                                 i %= RTE_MAX_LCORE;
209                         continue;
210                 }
211                 break;
212         }
213         return i;
214 }
215 /**
216  * Macro to browse all running lcores.
217  */
218 #define RTE_LCORE_FOREACH(i)                                            \
219         for (i = rte_get_next_lcore(-1, 0, 0);                          \
220              i<RTE_MAX_LCORE;                                           \
221              i = rte_get_next_lcore(i, 0, 0))
222
223 /**
224  * Macro to browse all running lcores except the master lcore.
225  */
226 #define RTE_LCORE_FOREACH_SLAVE(i)                                      \
227         for (i = rte_get_next_lcore(-1, 1, 0);                          \
228              i<RTE_MAX_LCORE;                                           \
229              i = rte_get_next_lcore(i, 1, 0))
230
231 /**
232  * Set core affinity of the current thread.
233  * Support both EAL and non-EAL thread and update TLS.
234  *
235  * @param cpusetp
236  *   Point to cpu_set_t for setting current thread affinity.
237  * @return
238  *   On success, return 0; otherwise return -1;
239  */
240 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
241
242 /**
243  * Get core affinity of the current thread.
244  *
245  * @param cpusetp
246  *   Point to cpu_set_t for getting current thread cpu affinity.
247  *   It presumes input is not NULL, otherwise it causes panic.
248  *
249  */
250 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
251
252 /**
253  * Set thread names.
254  *
255  * @note It fails with glibc < 2.12.
256  *
257  * @param id
258  *   Thread id.
259  * @param name
260  *   Thread name to set.
261  * @return
262  *   On success, return 0; otherwise return a negative value.
263  */
264 int rte_thread_setname(pthread_t id, const char *name);
265
266 /**
267  * Create a control thread.
268  *
269  * Wrapper to pthread_create(), pthread_setname_np() and
270  * pthread_setaffinity_np(). The affinity of the new thread is based
271  * on the CPU affinity retrieved at the time rte_eal_init() was called,
272  * the dataplane and service lcores are then excluded.
273  *
274  * @param thread
275  *   Filled with the thread id of the new created thread.
276  * @param name
277  *   The name of the control thread (max 16 characters including '\0').
278  * @param attr
279  *   Attributes for the new thread.
280  * @param start_routine
281  *   Function to be executed by the new thread.
282  * @param arg
283  *   Argument passed to start_routine.
284  * @return
285  *   On success, returns 0; on error, it returns a negative value
286  *   corresponding to the error number.
287  */
288 int
289 rte_ctrl_thread_create(pthread_t *thread, const char *name,
290                 const pthread_attr_t *attr,
291                 void *(*start_routine)(void *), void *arg);
292
293 /**
294  * Test if the core supplied has a specific role
295  *
296  * @param lcore_id
297  *   The identifier of the lcore, which MUST be between 0 and
298  *   RTE_MAX_LCORE-1.
299  * @param role
300  *   The role to be checked against.
301  * @return
302  *   Boolean value: positive if test is true; otherwise returns 0.
303  */
304 int
305 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
306
307 #ifdef __cplusplus
308 }
309 #endif
310
311
312 #endif /* _RTE_LCORE_H_ */