DPDK  18.11.11
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 #define RTE_CPU_OR(dst, src1, src2) CPU_OR(dst, src1, src2)
29 #define RTE_CPU_FILL(set) do \
30 { \
31  unsigned int i; \
32  CPU_ZERO(set); \
33  for (i = 0; i < CPU_SETSIZE; i++) \
34  CPU_SET(i, set); \
35 } while (0)
36 #define RTE_CPU_NOT(dst, src) do \
37 { \
38  cpu_set_t tmp; \
39  RTE_CPU_FILL(&tmp); \
40  CPU_XOR(dst, &tmp, src); \
41 } while (0)
42 #elif defined(__FreeBSD__)
43 #include <pthread_np.h>
44 typedef cpuset_t rte_cpuset_t;
45 #define RTE_CPU_AND(dst, src1, src2) do \
46 { \
47  cpuset_t tmp; \
48  CPU_COPY(src1, &tmp); \
49  CPU_AND(&tmp, src2); \
50  CPU_COPY(&tmp, dst); \
51 } while (0)
52 #define RTE_CPU_OR(dst, src1, src2) do \
53 { \
54  cpuset_t tmp; \
55  CPU_COPY(src1, &tmp); \
56  CPU_OR(&tmp, src2); \
57  CPU_COPY(&tmp, dst); \
58 } while (0)
59 #define RTE_CPU_FILL(set) CPU_FILL(set)
60 #define RTE_CPU_NOT(dst, src) do \
61 { \
62  cpuset_t tmp; \
63  CPU_FILL(&tmp); \
64  CPU_NAND(&tmp, src); \
65  CPU_COPY(&tmp, dst); \
66 } while (0)
67 #endif
68 
72 struct lcore_config {
73  unsigned detected;
74  pthread_t thread_id;
77  lcore_function_t * volatile f;
78  void * volatile arg;
79  volatile int ret;
80  volatile enum rte_lcore_state_t state;
81  unsigned socket_id;
82  unsigned core_id;
83  int core_index;
84  rte_cpuset_t cpuset;
85  uint8_t core_role;
86 };
87 
91 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
92 
93 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);
94 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset);
108 static inline unsigned
110 {
111  return RTE_PER_LCORE(_lcore_id);
112 }
113 
120 static inline unsigned
122 {
124 }
125 
132 static inline unsigned
134 {
135  const struct rte_config *cfg = rte_eal_get_configuration();
136  return cfg->lcore_count;
137 }
138 
153 static inline int
154 rte_lcore_index(int lcore_id)
155 {
156  if (lcore_id >= RTE_MAX_LCORE)
157  return -1;
158  if (lcore_id < 0)
159  lcore_id = (int)rte_lcore_id();
160  return lcore_config[lcore_id].core_index;
161 }
162 
169 unsigned rte_socket_id(void);
170 
181 unsigned int __rte_experimental
182 rte_socket_count(void);
183 
198 int __rte_experimental
199 rte_socket_id_by_idx(unsigned int idx);
200 
209 static inline unsigned
210 rte_lcore_to_socket_id(unsigned lcore_id)
211 {
212  return lcore_config[lcore_id].socket_id;
213 }
214 
224 static inline int
225 rte_lcore_is_enabled(unsigned lcore_id)
226 {
227  struct rte_config *cfg = rte_eal_get_configuration();
228  if (lcore_id >= RTE_MAX_LCORE)
229  return 0;
230  return cfg->lcore_role[lcore_id] == ROLE_RTE;
231 }
232 
246 static inline unsigned
247 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
248 {
249  i++;
250  if (wrap)
251  i %= RTE_MAX_LCORE;
252 
253  while (i < RTE_MAX_LCORE) {
254  if (!rte_lcore_is_enabled(i) ||
255  (skip_master && (i == rte_get_master_lcore()))) {
256  i++;
257  if (wrap)
258  i %= RTE_MAX_LCORE;
259  continue;
260  }
261  break;
262  }
263  return i;
264 }
268 #define RTE_LCORE_FOREACH(i) \
269  for (i = rte_get_next_lcore(-1, 0, 0); \
270  i<RTE_MAX_LCORE; \
271  i = rte_get_next_lcore(i, 0, 0))
272 
276 #define RTE_LCORE_FOREACH_SLAVE(i) \
277  for (i = rte_get_next_lcore(-1, 1, 0); \
278  i<RTE_MAX_LCORE; \
279  i = rte_get_next_lcore(i, 1, 0))
280 
290 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
291 
300 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
301 
314 int rte_thread_setname(pthread_t id, const char *name);
315 
338 __rte_experimental int
339 rte_ctrl_thread_create(pthread_t *thread, const char *name,
340  const pthread_attr_t *attr,
341  void *(*start_routine)(void *), void *arg);
342 
354 int
355 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
356 
357 #ifdef __cplusplus
358 }
359 #endif
360 
361 
362 #endif /* _RTE_LCORE_H_ */
int pipe_slave2master[2]
Definition: rte_lcore.h:76
void rte_thread_get_affinity(rte_cpuset_t *cpusetp)
uint32_t lcore_count
Definition: rte_eal.h:59
static unsigned rte_lcore_to_socket_id(unsigned lcore_id)
Definition: rte_lcore.h:210
static int rte_lcore_index(int lcore_id)
Definition: rte_lcore.h:154
int pipe_master2slave[2]
Definition: rte_lcore.h:75
unsigned core_id
Definition: rte_lcore.h:82
int rte_thread_setname(pthread_t id, const char *name)
static unsigned rte_lcore_count(void)
Definition: rte_lcore.h:133
rte_lcore_role_t
Definition: rte_eal.h:37
uint8_t core_role
Definition: rte_lcore.h:85
int rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
int __rte_experimental rte_socket_id_by_idx(unsigned int idx)
unsigned socket_id
Definition: rte_lcore.h:81
static int rte_lcore_is_enabled(unsigned lcore_id)
Definition: rte_lcore.h:225
__rte_experimental int rte_ctrl_thread_create(pthread_t *thread, const char *name, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
volatile int ret
Definition: rte_lcore.h:79
rte_cpuset_t cpuset
Definition: rte_lcore.h:84
enum rte_lcore_state_t state
Definition: rte_lcore.h:80
int core_index
Definition: rte_lcore.h:83
static unsigned rte_lcore_id(void)
Definition: rte_lcore.h:109
enum rte_lcore_role_t lcore_role[RTE_MAX_LCORE]
Definition: rte_eal.h:63
struct rte_config * rte_eal_get_configuration(void)
unsigned detected
Definition: rte_lcore.h:73
lcore_function_t *volatile f
Definition: rte_lcore.h:77
unsigned int __rte_experimental rte_socket_count(void)
static unsigned rte_get_master_lcore(void)
Definition: rte_lcore.h:121
uint32_t master_lcore
Definition: rte_eal.h:58
int( lcore_function_t)(void *)
Definition: rte_launch.h:30
pthread_t thread_id
Definition: rte_lcore.h:74
#define RTE_PER_LCORE(name)
Definition: rte_per_lcore.h:44
RTE_DECLARE_PER_LCORE(unsigned, _lcore_id)
int rte_thread_set_affinity(rte_cpuset_t *cpusetp)
unsigned rte_socket_id(void)
static unsigned rte_get_next_lcore(unsigned i, int skip_master, int wrap)
Definition: rte_lcore.h:247
rte_lcore_state_t
Definition: rte_launch.h:21
void *volatile arg
Definition: rte_lcore.h:78