lib: fix missing includes in exported headers
[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 #elif defined(__FreeBSD__)
28 #include <pthread_np.h>
29         typedef cpuset_t rte_cpuset_t;
30 #endif
31
32 /**
33  * Structure storing internal configuration (per-lcore)
34  */
35 struct lcore_config {
36         unsigned detected;         /**< true if lcore was detected */
37         pthread_t thread_id;       /**< pthread identifier */
38         int pipe_master2slave[2];  /**< communication pipe with master */
39         int pipe_slave2master[2];  /**< communication pipe with master */
40         lcore_function_t * volatile f;         /**< function to call */
41         void * volatile arg;       /**< argument of function */
42         volatile int ret;          /**< return value of function */
43         volatile enum rte_lcore_state_t state; /**< lcore state */
44         unsigned socket_id;        /**< physical socket id for this lcore */
45         unsigned core_id;          /**< core number on socket for this lcore */
46         int core_index;            /**< relative index, starting from 0 */
47         rte_cpuset_t cpuset;       /**< cpu set which the lcore affinity to */
48         uint8_t core_role;         /**< role of core eg: OFF, RTE, SERVICE */
49 };
50
51 /**
52  * Internal configuration (per-lcore)
53  */
54 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
55
56 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
57 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
58
59 /**
60  * Return the ID of the execution unit we are running on.
61  * @return
62  *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
63  */
64 static inline unsigned
65 rte_lcore_id(void)
66 {
67         return RTE_PER_LCORE(_lcore_id);
68 }
69
70 /**
71  * Get the id of the master lcore
72  *
73  * @return
74  *   the id of the master lcore
75  */
76 static inline unsigned
77 rte_get_master_lcore(void)
78 {
79         return rte_eal_get_configuration()->master_lcore;
80 }
81
82 /**
83  * Return the number of execution units (lcores) on the system.
84  *
85  * @return
86  *   the number of execution units (lcores) on the system.
87  */
88 static inline unsigned
89 rte_lcore_count(void)
90 {
91         const struct rte_config *cfg = rte_eal_get_configuration();
92         return cfg->lcore_count;
93 }
94
95 /**
96  * Return the index of the lcore starting from zero.
97  * The order is physical or given by command line (-l option).
98  *
99  * @param lcore_id
100  *   The targeted lcore, or -1 for the current one.
101  * @return
102  *   The relative index, or -1 if not enabled.
103  */
104 static inline int
105 rte_lcore_index(int lcore_id)
106 {
107         if (lcore_id >= RTE_MAX_LCORE)
108                 return -1;
109         if (lcore_id < 0)
110                 lcore_id = rte_lcore_id();
111         return lcore_config[lcore_id].core_index;
112 }
113
114 /**
115  * Return the ID of the physical socket of the logical core we are
116  * running on.
117  * @return
118  *   the ID of current lcoreid's physical socket
119  */
120 unsigned rte_socket_id(void);
121
122 /**
123  * Get the ID of the physical socket of the specified lcore
124  *
125  * @param lcore_id
126  *   the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1.
127  * @return
128  *   the ID of lcoreid's physical socket
129  */
130 static inline unsigned
131 rte_lcore_to_socket_id(unsigned lcore_id)
132 {
133         return lcore_config[lcore_id].socket_id;
134 }
135
136 /**
137  * Test if an lcore is enabled.
138  *
139  * @param lcore_id
140  *   The identifier of the lcore, which MUST be between 0 and
141  *   RTE_MAX_LCORE-1.
142  * @return
143  *   True if the given lcore is enabled; false otherwise.
144  */
145 static inline int
146 rte_lcore_is_enabled(unsigned lcore_id)
147 {
148         struct rte_config *cfg = rte_eal_get_configuration();
149         if (lcore_id >= RTE_MAX_LCORE)
150                 return 0;
151         return cfg->lcore_role[lcore_id] == ROLE_RTE;
152 }
153
154 /**
155  * Get the next enabled lcore ID.
156  *
157  * @param i
158  *   The current lcore (reference).
159  * @param skip_master
160  *   If true, do not return the ID of the master lcore.
161  * @param wrap
162  *   If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise,
163  *   return RTE_MAX_LCORE.
164  * @return
165  *   The next lcore_id or RTE_MAX_LCORE if not found.
166  */
167 static inline unsigned
168 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
169 {
170         i++;
171         if (wrap)
172                 i %= RTE_MAX_LCORE;
173
174         while (i < RTE_MAX_LCORE) {
175                 if (!rte_lcore_is_enabled(i) ||
176                     (skip_master && (i == rte_get_master_lcore()))) {
177                         i++;
178                         if (wrap)
179                                 i %= RTE_MAX_LCORE;
180                         continue;
181                 }
182                 break;
183         }
184         return i;
185 }
186 /**
187  * Macro to browse all running lcores.
188  */
189 #define RTE_LCORE_FOREACH(i)                                            \
190         for (i = rte_get_next_lcore(-1, 0, 0);                          \
191              i<RTE_MAX_LCORE;                                           \
192              i = rte_get_next_lcore(i, 0, 0))
193
194 /**
195  * Macro to browse all running lcores except the master lcore.
196  */
197 #define RTE_LCORE_FOREACH_SLAVE(i)                                      \
198         for (i = rte_get_next_lcore(-1, 1, 0);                          \
199              i<RTE_MAX_LCORE;                                           \
200              i = rte_get_next_lcore(i, 1, 0))
201
202 /**
203  * Set core affinity of the current thread.
204  * Support both EAL and non-EAL thread and update TLS.
205  *
206  * @param cpusetp
207  *   Point to cpu_set_t for setting current thread affinity.
208  * @return
209  *   On success, return 0; otherwise return -1;
210  */
211 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
212
213 /**
214  * Get core affinity of the current thread.
215  *
216  * @param cpusetp
217  *   Point to cpu_set_t for getting current thread cpu affinity.
218  *   It presumes input is not NULL, otherwise it causes panic.
219  *
220  */
221 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
222
223 /**
224  * Set thread names.
225  *
226  * @note It fails with glibc < 2.12.
227  *
228  * @param id
229  *   Thread id.
230  * @param name
231  *   Thread name to set.
232  * @return
233  *   On success, return 0; otherwise return a negative value.
234  */
235 int rte_thread_setname(pthread_t id, const char *name);
236
237 /**
238  * Test if the core supplied has a specific role
239  *
240  * @param lcore_id
241  *   The identifier of the lcore, which MUST be between 0 and
242  *   RTE_MAX_LCORE-1.
243  * @param role
244  *   The role to be checked against.
245  * @return
246  *   On success, return 0; otherwise return a negative value.
247  */
248 int
249 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
250
251 #ifdef __cplusplus
252 }
253 #endif
254
255
256 #endif /* _RTE_LCORE_H_ */