DPDK  17.11.10
rte_lcore.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_LCORE_H_
35 #define _RTE_LCORE_H_
36 
43 #include <rte_config.h>
44 #include <rte_per_lcore.h>
45 #include <rte_eal.h>
46 #include <rte_launch.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #define LCORE_ID_ANY UINT32_MAX
54 #if defined(__linux__)
55  typedef cpu_set_t rte_cpuset_t;
56 #elif defined(__FreeBSD__)
57 #include <pthread_np.h>
58  typedef cpuset_t rte_cpuset_t;
59 #endif
60 
64 struct lcore_config {
65  unsigned detected;
66  pthread_t thread_id;
69  lcore_function_t * volatile f;
70  void * volatile arg;
71  volatile int ret;
72  volatile enum rte_lcore_state_t state;
73  unsigned socket_id;
74  unsigned core_id;
75  int core_index;
76  rte_cpuset_t cpuset;
77  uint8_t core_role;
78 };
79 
83 extern struct lcore_config lcore_config[RTE_MAX_LCORE];
84 
85 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);
86 RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset);
93 static inline unsigned
95 {
96  return RTE_PER_LCORE(_lcore_id);
97 }
98 
105 static inline unsigned
107 {
109 }
110 
117 static inline unsigned
119 {
120  const struct rte_config *cfg = rte_eal_get_configuration();
121  return cfg->lcore_count;
122 }
123 
133 static inline int
134 rte_lcore_index(int lcore_id)
135 {
136  if (lcore_id >= RTE_MAX_LCORE)
137  return -1;
138  if (lcore_id < 0)
139  lcore_id = (int)rte_lcore_id();
140  return lcore_config[lcore_id].core_index;
141 }
142 
149 unsigned rte_socket_id(void);
150 
159 static inline unsigned
160 rte_lcore_to_socket_id(unsigned lcore_id)
161 {
162  return lcore_config[lcore_id].socket_id;
163 }
164 
174 static inline int
175 rte_lcore_is_enabled(unsigned lcore_id)
176 {
177  struct rte_config *cfg = rte_eal_get_configuration();
178  if (lcore_id >= RTE_MAX_LCORE)
179  return 0;
180  return cfg->lcore_role[lcore_id] == ROLE_RTE;
181 }
182 
196 static inline unsigned
197 rte_get_next_lcore(unsigned i, int skip_master, int wrap)
198 {
199  i++;
200  if (wrap)
201  i %= RTE_MAX_LCORE;
202 
203  while (i < RTE_MAX_LCORE) {
204  if (!rte_lcore_is_enabled(i) ||
205  (skip_master && (i == rte_get_master_lcore()))) {
206  i++;
207  if (wrap)
208  i %= RTE_MAX_LCORE;
209  continue;
210  }
211  break;
212  }
213  return i;
214 }
218 #define RTE_LCORE_FOREACH(i) \
219  for (i = rte_get_next_lcore(-1, 0, 0); \
220  i<RTE_MAX_LCORE; \
221  i = rte_get_next_lcore(i, 0, 0))
222 
226 #define RTE_LCORE_FOREACH_SLAVE(i) \
227  for (i = rte_get_next_lcore(-1, 1, 0); \
228  i<RTE_MAX_LCORE; \
229  i = rte_get_next_lcore(i, 1, 0))
230 
240 int rte_thread_set_affinity(rte_cpuset_t *cpusetp);
241 
250 void rte_thread_get_affinity(rte_cpuset_t *cpusetp);
251 
264 int rte_thread_setname(pthread_t id, const char *name);
265 
277 int
278 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
279 
280 #ifdef __cplusplus
281 }
282 #endif
283 
284 
285 #endif /* _RTE_LCORE_H_ */
int pipe_slave2master[2]
Definition: rte_lcore.h:68
void rte_thread_get_affinity(rte_cpuset_t *cpusetp)
uint32_t lcore_count
Definition: rte_eal.h:86
static unsigned rte_lcore_to_socket_id(unsigned lcore_id)
Definition: rte_lcore.h:160
static int rte_lcore_index(int lcore_id)
Definition: rte_lcore.h:134
int pipe_master2slave[2]
Definition: rte_lcore.h:67
unsigned core_id
Definition: rte_lcore.h:74
int rte_thread_setname(pthread_t id, const char *name)
static unsigned rte_lcore_count(void)
Definition: rte_lcore.h:118
rte_lcore_role_t
Definition: rte_eal.h:64
uint8_t core_role
Definition: rte_lcore.h:77
int rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
unsigned socket_id
Definition: rte_lcore.h:73
static int rte_lcore_is_enabled(unsigned lcore_id)
Definition: rte_lcore.h:175
volatile int ret
Definition: rte_lcore.h:71
rte_cpuset_t cpuset
Definition: rte_lcore.h:76
enum rte_lcore_state_t state
Definition: rte_lcore.h:72
int core_index
Definition: rte_lcore.h:75
static unsigned rte_lcore_id(void)
Definition: rte_lcore.h:94
enum rte_lcore_role_t lcore_role[RTE_MAX_LCORE]
Definition: rte_eal.h:88
struct rte_config * rte_eal_get_configuration(void)
unsigned detected
Definition: rte_lcore.h:65
lcore_function_t *volatile f
Definition: rte_lcore.h:69
static unsigned rte_get_master_lcore(void)
Definition: rte_lcore.h:106
uint32_t master_lcore
Definition: rte_eal.h:85
int( lcore_function_t)(void *)
Definition: rte_launch.h:59
pthread_t thread_id
Definition: rte_lcore.h:66
#define RTE_PER_LCORE(name)
Definition: rte_per_lcore.h:73
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:197
rte_lcore_state_t
Definition: rte_launch.h:50
void *volatile arg
Definition: rte_lcore.h:70