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