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