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