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