DPDK  19.05.0
rte_lcore.h
Go to the documentation of this file.
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 
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
25 #if defined(__linux__)
26 typedef cpu_set_t rte_cpuset_t;
27 #define RTE_CPU_AND(dst, src1, src2) CPU_AND(dst, src1, src2)
28 #elif defined(__FreeBSD__)
29 #include <pthread_np.h>
30 typedef cpuset_t rte_cpuset_t;
31 #define RTE_CPU_AND(dst, src1, src2) do \
32 { \
33  cpuset_t tmp; \
34  CPU_COPY(src1, &tmp); \
35  CPU_AND(&tmp, src2); \
36  CPU_COPY(&tmp, dst); \
37 } while (0)
38 #endif
39 
43 struct lcore_config {
44  unsigned detected;
45  pthread_t thread_id;
48  lcore_function_t * volatile f;
49  void * volatile arg;
50  volatile int ret;
51  volatile enum rte_lcore_state_t state;
52  unsigned socket_id;
53  unsigned core_id;
54  int core_index;
55  rte_cpuset_t cpuset;
56  uint8_t core_role;
57 };
58 
62 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
63 
64 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);
65 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset);
79 static inline unsigned
81 {
82  return RTE_PER_LCORE(_lcore_id);
83 }
84 
91 static inline unsigned
93 {
95 }
96 
103 static inline unsigned
105 {
106  const struct rte_config *cfg = rte_eal_get_configuration();
107  return cfg->lcore_count;
108 }
109 
124 static inline int
125 rte_lcore_index(int lcore_id)
126 {
127  if (lcore_id >= RTE_MAX_LCORE)
128  return -1;
129  if (lcore_id < 0)
130  lcore_id = (int)rte_lcore_id();
131  return lcore_config[lcore_id].core_index;
132 }
133 
140 unsigned rte_socket_id(void);
141 
152 unsigned int
153 rte_socket_count(void);
154 
169 int
170 rte_socket_id_by_idx(unsigned int idx);
171 
180 static inline unsigned
181 rte_lcore_to_socket_id(unsigned lcore_id)
182 {
183  return lcore_config[lcore_id].socket_id;
184 }
185 
195 static inline int
196 rte_lcore_is_enabled(unsigned lcore_id)
197 {
198  struct rte_config *cfg = rte_eal_get_configuration();
199  if (lcore_id >= RTE_MAX_LCORE)
200  return 0;
201  return cfg->lcore_role[lcore_id] == ROLE_RTE;
202 }
203 
217 static inline unsigned
218 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
219 {
220  i++;
221  if (wrap)
222  i %= RTE_MAX_LCORE;
223 
224  while (i < RTE_MAX_LCORE) {
225  if (!rte_lcore_is_enabled(i) ||
226  (skip_master && (i == rte_get_master_lcore()))) {
227  i++;
228  if (wrap)
229  i %= RTE_MAX_LCORE;
230  continue;
231  }
232  break;
233  }
234  return i;
235 }
239 #define RTE_LCORE_FOREACH(i) \
240  for (i = rte_get_next_lcore(-1, 0, 0); \
241  i<RTE_MAX_LCORE; \
242  i = rte_get_next_lcore(i, 0, 0))
243 
247 #define RTE_LCORE_FOREACH_SLAVE(i) \
248  for (i = rte_get_next_lcore(-1, 1, 0); \
249  i<RTE_MAX_LCORE; \
250  i = rte_get_next_lcore(i, 1, 0))
251 
261 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
262 
271 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
272 
285 int rte_thread_setname(pthread_t id, const char *name);
286 
309 int
310 rte_ctrl_thread_create(pthread_t *thread, const char *name,
311  const pthread_attr_t *attr,
312  void *(*start_routine)(void *), void *arg);
313 
325 int
326 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
327 
328 #ifdef __cplusplus
329 }
330 #endif
331 
332 
333 #endif /* _RTE_LCORE_H_ */