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