0fe0bd839c49f947122c9b0d3424731942b0d4ca
[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 #include <rte_thread.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #define LCORE_ID_ANY     UINT32_MAX       /**< Any lcore. */
25
26 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
27
28 /**
29  * The lcore role (used in RTE or not).
30  */
31 enum rte_lcore_role_t {
32         ROLE_RTE,
33         ROLE_OFF,
34         ROLE_SERVICE,
35         ROLE_NON_EAL,
36 };
37
38 /**
39  * Get a lcore's role.
40  *
41  * @param lcore_id
42  *   The identifier of the lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
43  * @return
44  *   The role of the lcore.
45  */
46 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id);
47
48 /**
49  * Test if the core supplied has a specific role
50  *
51  * @param lcore_id
52  *   The identifier of the lcore, which MUST be between 0 and
53  *   RTE_MAX_LCORE-1.
54  * @param role
55  *   The role to be checked against.
56  * @return
57  *   Boolean value: positive if test is true; otherwise returns 0.
58  */
59 int
60 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
61
62 /**
63  * Return the Application thread ID of the execution unit.
64  *
65  * Note: in most cases the lcore id returned here will also correspond
66  *   to the processor id of the CPU on which the thread is pinned, this
67  *   will not be the case if the user has explicitly changed the thread to
68  *   core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10'
69  *   to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
70  *
71  * @return
72  *  Logical core ID (in EAL thread or registered non-EAL thread) or
73  *  LCORE_ID_ANY (in unregistered non-EAL thread)
74  */
75 static inline unsigned
76 rte_lcore_id(void)
77 {
78         return RTE_PER_LCORE(_lcore_id);
79 }
80
81 /**
82  * Get the id of the main lcore
83  *
84  * @return
85  *   the id of the main lcore
86  */
87 unsigned int rte_get_main_lcore(void);
88
89 /**
90  * Deprecated function the id of the main lcore
91  *
92  * @return
93  *   the id of the main lcore
94  */
95 __rte_deprecated
96 static inline unsigned int rte_get_master_lcore(void)
97 {
98         return rte_get_main_lcore();
99 }
100
101 /**
102  * Return the number of execution units (lcores) on the system.
103  *
104  * @return
105  *   the number of execution units (lcores) on the system.
106  */
107 unsigned int rte_lcore_count(void);
108
109 /**
110  * Return the index of the lcore starting from zero.
111  *
112  * When option -c or -l is given, the index corresponds
113  * to the order in the list.
114  * For example:
115  * -c 0x30, lcore 4 has index 0, and 5 has index 1.
116  * -l 22,18 lcore 22 has index 0, and 18 has index 1.
117  *
118  * @param lcore_id
119  *   The targeted lcore, or -1 for the current one.
120  * @return
121  *   The relative index, or -1 if not enabled.
122  */
123 int rte_lcore_index(int lcore_id);
124
125 /**
126  * Return the ID of the physical socket of the logical core we are
127  * running on.
128  * @return
129  *   the ID of current lcoreid's physical socket
130  */
131 unsigned int rte_socket_id(void);
132
133 /**
134  * Return number of physical sockets detected on the system.
135  *
136  * Note that number of nodes may not be correspondent to their physical id's:
137  * for example, a system may report two socket id's, but the actual socket id's
138  * may be 0 and 8.
139  *
140  * @return
141  *   the number of physical sockets as recognized by EAL
142  */
143 unsigned int
144 rte_socket_count(void);
145
146 /**
147  * Return socket id with a particular index.
148  *
149  * This will return socket id at a particular position in list of all detected
150  * physical socket id's. For example, on a machine with sockets [0, 8], passing
151  * 1 as a parameter will return 8.
152  *
153  * @param idx
154  *   index of physical socket id to return
155  *
156  * @return
157  *   - physical socket id as recognized by EAL
158  *   - -1 on error, with errno set to EINVAL
159  */
160 int
161 rte_socket_id_by_idx(unsigned int idx);
162
163 /**
164  * Get the ID of the physical socket of the specified lcore
165  *
166  * @param lcore_id
167  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
168  * @return
169  *   the ID of lcoreid's physical socket
170  */
171 unsigned int
172 rte_lcore_to_socket_id(unsigned int lcore_id);
173
174 /**
175  * @warning
176  * @b EXPERIMENTAL: this API may change without prior notice.
177  *
178  * Return the id of the lcore on a socket starting from zero.
179  *
180  * @param lcore_id
181  *   The targeted lcore, or -1 for the current one.
182  * @return
183  *   The relative index, or -1 if not enabled.
184  */
185 __rte_experimental
186 int
187 rte_lcore_to_cpu_id(int lcore_id);
188
189 /**
190  * @warning
191  * @b EXPERIMENTAL: this API may change without prior notice.
192  *
193  * Return the cpuset for a given lcore.
194  * @param lcore_id
195  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
196  * @return
197  *   The cpuset of that lcore
198  */
199 __rte_experimental
200 rte_cpuset_t
201 rte_lcore_cpuset(unsigned int lcore_id);
202
203 /**
204  * Test if an lcore is enabled.
205  *
206  * @param lcore_id
207  *   The identifier of the lcore, which MUST be between 0 and
208  *   RTE_MAX_LCORE-1.
209  * @return
210  *   True if the given lcore is enabled; false otherwise.
211  */
212 int rte_lcore_is_enabled(unsigned int lcore_id);
213
214 /**
215  * Get the next enabled lcore ID.
216  *
217  * @param i
218  *   The current lcore (reference).
219  * @param skip_main
220  *   If true, do not return the ID of the main lcore.
221  * @param wrap
222  *   If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
223  *   return RTE_MAX_LCORE.
224  * @return
225  *   The next lcore_id or RTE_MAX_LCORE if not found.
226  */
227 unsigned int rte_get_next_lcore(unsigned int i, int skip_main, int wrap);
228
229 /**
230  * Macro to browse all running lcores.
231  */
232 #define RTE_LCORE_FOREACH(i)                                            \
233         for (i = rte_get_next_lcore(-1, 0, 0);                          \
234              i < RTE_MAX_LCORE;                                         \
235              i = rte_get_next_lcore(i, 0, 0))
236
237 /**
238  * Macro to browse all running lcores except the main lcore.
239  */
240 #define RTE_LCORE_FOREACH_WORKER(i)                                     \
241         for (i = rte_get_next_lcore(-1, 1, 0);                          \
242              i < RTE_MAX_LCORE;                                         \
243              i = rte_get_next_lcore(i, 1, 0))
244
245 #define RTE_LCORE_FOREACH_SLAVE(l)                                      \
246         RTE_DEPRECATED(RTE_LCORE_FOREACH_SLAVE) RTE_LCORE_FOREACH_WORKER(l)
247
248 /**
249  * Callback prototype for initializing lcores.
250  *
251  * @param lcore_id
252  *   The lcore to consider.
253  * @param arg
254  *   An opaque pointer passed at callback registration.
255  * @return
256  *   - -1 when refusing this operation,
257  *   - 0 otherwise.
258  */
259 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg);
260
261 /**
262  * Callback prototype for uninitializing lcores.
263  *
264  * @param lcore_id
265  *   The lcore to consider.
266  * @param arg
267  *   An opaque pointer passed at callback registration.
268  */
269 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg);
270
271 /**
272  * Register callbacks invoked when initializing and uninitializing a lcore.
273  *
274  * This function calls the init callback with all initialized lcores.
275  * Any error reported by the init callback triggers a rollback calling the
276  * uninit callback for each lcore.
277  * If this step succeeds, the callbacks are put in the lcore callbacks list
278  * that will get called for each lcore allocation/release.
279  *
280  * Note: callbacks execution is serialised under a write lock protecting the
281  * lcores and callbacks list.
282  *
283  * @param name
284  *   A name serving as a small description for this callback.
285  * @param init
286  *   The callback invoked when a lcore_id is initialized.
287  *   init can be NULL.
288  * @param uninit
289  *   The callback invoked when a lcore_id is uninitialized.
290  *   uninit can be NULL.
291  * @param arg
292  *   An optional argument that gets passed to the callback when it gets
293  *   invoked.
294  * @return
295  *   On success, returns an opaque pointer for the registered object.
296  *   On failure (either memory allocation issue in the function itself or an
297  *   error is returned by the init callback itself), returns NULL.
298  */
299 __rte_experimental
300 void *
301 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init,
302         rte_lcore_uninit_cb uninit, void *arg);
303
304 /**
305  * Unregister callbacks previously registered with rte_lcore_callback_register.
306  *
307  * This function calls the uninit callback with all initialized lcores.
308  * The callbacks are then removed from the lcore callbacks list.
309  *
310  * @param handle
311  *   The handle pointer returned by a former successful call to
312  *   rte_lcore_callback_register.
313  */
314 __rte_experimental
315 void
316 rte_lcore_callback_unregister(void *handle);
317
318 /**
319  * Callback prototype for iterating over lcores.
320  *
321  * @param lcore_id
322  *   The lcore to consider.
323  * @param arg
324  *   An opaque pointer coming from the caller.
325  * @return
326  *   - 0 lets the iteration continue.
327  *   - !0 makes the iteration stop.
328  */
329 typedef int (*rte_lcore_iterate_cb)(unsigned int lcore_id, void *arg);
330
331 /**
332  * Iterate on all active lcores (ROLE_RTE, ROLE_SERVICE and ROLE_NON_EAL).
333  * No modification on the lcore states is allowed in the callback.
334  *
335  * Note: as opposed to init/uninit callbacks, iteration callbacks can be
336  * invoked in parallel as they are run under a read lock protecting the lcores
337  * and callbacks list.
338  *
339  * @param cb
340  *   The callback that gets passed each lcore.
341  * @param arg
342  *   An opaque pointer passed to cb.
343  * @return
344  *   Same return code as the callback last invocation (see rte_lcore_iterate_cb
345  *   description).
346  */
347 __rte_experimental
348 int
349 rte_lcore_iterate(rte_lcore_iterate_cb cb, void *arg);
350
351 /**
352  * List all lcores.
353  *
354  * @param f
355  *   The output stream where the dump should be sent.
356  */
357 __rte_experimental
358 void
359 rte_lcore_dump(FILE *f);
360
361 /**
362  * Set thread names.
363  *
364  * @note It fails with glibc < 2.12.
365  *
366  * @param id
367  *   Thread id.
368  * @param name
369  *   Thread name to set.
370  * @return
371  *   On success, return 0; otherwise return a negative value.
372  */
373 int rte_thread_setname(pthread_t id, const char *name);
374
375 /**
376  * Get thread name.
377  *
378  * @note It fails with glibc < 2.12.
379  *
380  * @param id
381  *   Thread id.
382  * @param name
383  *   Thread name to set.
384  * @param len
385  *   Thread name buffer length.
386  * @return
387  *   On success, return 0; otherwise return a negative value.
388  */
389 __rte_experimental
390 int rte_thread_getname(pthread_t id, char *name, size_t len);
391
392 /**
393  * Register current non-EAL thread as a lcore.
394  *
395  * @note This API is not compatible with the multi-process feature:
396  * - if a primary process registers a non-EAL thread, then no secondary process
397  *   will initialise.
398  * - if a secondary process initialises successfully, trying to register a
399  *   non-EAL thread from either primary or secondary processes will always end
400  *   up with the thread getting LCORE_ID_ANY as lcore.
401  *
402  * @return
403  *   On success, return 0; otherwise return -1 with rte_errno set.
404  */
405 __rte_experimental
406 int
407 rte_thread_register(void);
408
409 /**
410  * Unregister current thread and release lcore if one was associated.
411  */
412 __rte_experimental
413 void
414 rte_thread_unregister(void);
415
416 /**
417  * Create a control thread.
418  *
419  * Wrapper to pthread_create(), pthread_setname_np() and
420  * pthread_setaffinity_np(). The affinity of the new thread is based
421  * on the CPU affinity retrieved at the time rte_eal_init() was called,
422  * the dataplane and service lcores are then excluded.
423  *
424  * @param thread
425  *   Filled with the thread id of the new created thread.
426  * @param name
427  *   The name of the control thread (max 16 characters including '\0').
428  * @param attr
429  *   Attributes for the new thread.
430  * @param start_routine
431  *   Function to be executed by the new thread.
432  * @param arg
433  *   Argument passed to start_routine.
434  * @return
435  *   On success, returns 0; on error, it returns a negative value
436  *   corresponding to the error number.
437  */
438 int
439 rte_ctrl_thread_create(pthread_t *thread, const char *name,
440                 const pthread_attr_t *attr,
441                 void *(*start_routine)(void *), void *arg);
442
443 #ifdef __cplusplus
444 }
445 #endif
446
447
448 #endif /* _RTE_LCORE_H_ */