FD.io VPP  v16.06
Vector Packet Processing
threads.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_vlib_threads_h
16 #define included_vlib_threads_h
17 
18 #include <vlib/main.h>
19 
21 
22 void
23 vlib_set_thread_name (char *name);
24 
25 /* arg is actually a vlib__thread_t * */
26 typedef void (vlib_thread_function_t) (void * arg);
27 
28 typedef struct vlib_thread_registration_ {
29  /* constructor generated list of thread registrations */
31 
32  /* config parameters */
33  char * name;
34  char * short_name;
41 
42  /* All threads of this type run on pthreads */
47 
48 /*
49  * Frames have their cpu / vlib_main_t index in the low-order N bits
50  * Make VLIB_MAX_CPUS a power-of-two, please...
51  */
52 
53 #ifndef VLIB_MAX_CPUS
54 #define VLIB_MAX_CPUS 256
55 #endif
56 
57 #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS
58 #error Please increase number of per-cpu mheaps
59 #endif
60 
61 #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */
62 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
63 
64 #define VLIB_LOG2_THREAD_STACK_SIZE (20)
65 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
66 
67 typedef enum {
70 
71 typedef struct {
72  volatile u32 valid;
76 
77  /* 256 * 4 = 1024 bytes, even mult of cache line size */
78  u32 buffer_index[VLIB_FRAME_SIZE];
79 
80  /* Pad to a cache line boundary */
81  u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof(u32)];
83 
84 typedef struct {
85  /* First cache line */
86  volatile u32 *wait_at_barrier;
88  u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
89 
90  /* Second Cache Line */
91  void *thread_mheap;
93  void (*thread_function)(void *);
99  u8 *name;
101 
102  long lwp;
105 
107 
108 typedef struct {
109  /* enqueue side */
110  volatile u64 tail;
117  - (2 * sizeof(u32))
118  - (4 * sizeof(u64))];
119 
120  /* dequeue side */
121  volatile u64 head;
128  - (6 * sizeof(u64))];
129 
130  /* dequeue hint to enqueue side */
131  volatile u64 head_hint;
132  u8 pad5 [CLIB_CACHE_LINE_BYTES - sizeof(u64)];
133 
134  /* read-only, constant, shared */
138 
140 
141 /* Called early, in thread 0's context */
143 
145 
146 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
147  u32 frame_queue_index, vlib_frame_t *frame,
149 
150 int vlib_frame_queue_dequeue (int thread_id,
151  vlib_main_t *vm,
152  vlib_node_main_t *nm);
153 
155  vlib_node_runtime_t * node,
157  vlib_node_state_t dispatch_state,
158  vlib_frame_t * frame,
159  u64 last_time_stamp);
160 
163  u64 last_time_stamp);
164 
166 
167 void vlib_create_worker_threads (vlib_main_t *vm, int n,
168  void (*thread_function)(void *));
169 
171 
172 /* Check for a barrier sync request every 30ms */
173 #define BARRIER_SYNC_DELAY (0.030000)
174 
175 #if CLIB_DEBUG > 0
176 /* long barrier timeout, for gdb... */
177 #define BARRIER_SYNC_TIMEOUT (600.1)
178 #else
179 #define BARRIER_SYNC_TIMEOUT (1.0)
180 #endif
181 
184 
186 {
187  if (CLIB_DEBUG > 0)
188  {
189  if (os_get_cpu_number())
190  fformat(stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
191  }
192 }
193 
194 typedef enum {
198 
199 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
200 
201 static inline void vlib_worker_thread_barrier_check (void)
202 {
203  if (PREDICT_FALSE(*vlib_worker_threads->wait_at_barrier))
204  {
205  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
206  while (*vlib_worker_threads->wait_at_barrier)
207  ;
208  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
209  }
210 }
211 
212 #define foreach_vlib_main(body) \
213 do { \
214  vlib_main_t ** __vlib_mains = 0, *this_vlib_main; \
215  int ii; \
216  \
217  if (vec_len (vlib_mains) == 0) \
218  vec_add1 (__vlib_mains, &vlib_global_main); \
219  else \
220  { \
221  for (ii = 0; ii < vec_len (vlib_mains); ii++) \
222  { \
223  this_vlib_main = vlib_mains[ii]; \
224  if (this_vlib_main) \
225  vec_add1 (__vlib_mains, this_vlib_main); \
226  } \
227  } \
228  \
229  for (ii = 0; ii < vec_len (__vlib_mains); ii++) \
230  { \
231  this_vlib_main = __vlib_mains[ii]; \
232  /* body uses this_vlib_main... */ \
233  (body); \
234  } \
235  vec_free (__vlib_mains); \
236 } while (0);
237 
238 
239 /* Early-Fast-Discard (EFD) */
240 #define VLIB_EFD_DISABLED 0
241 #define VLIB_EFD_DISCARD_ENABLED (1 << 0)
242 #define VLIB_EFD_MONITOR_ENABLED (1 << 1)
243 
244 #define VLIB_EFD_DEF_WORKER_HI_THRESH_PCT 90
245 
246 /* EFD worker thread settings */
247 typedef struct vlib_efd_t {
254 } vlib_efd_t;
255 
256 typedef struct {
257  /* Link list of registrations, built by constructors */
259 
260  /* Vector of registrations, w/ non-data-structure clones at the top */
262 
264 
266 
267  /* thread / cpu / io thread parameters */
269 
270  /*
271  * Launch all threads as pthreads,
272  * not eal_rte_launch (strict affinity) threads
273  */
275 
276  /* Number of vlib_main / vnet_main clones */
278 
279  /* Number of thread stacks to create */
281 
282  /* Number of pthreads */
284 
285  /* Number of DPDK eal threads */
287 
288  /* Number of cores to skip, must match the core mask */
290 
291  /* Thread prefix name */
293 
294  /* main thread lcore */
296 
297  /* Bitmap of available CPU cores */
299 
300  /* Bitmap of available CPU sockets (NUMA nodes) */
302 
304 
306 
308 
309 #define VLIB_REGISTER_THREAD(x,...) \
310  __VA_ARGS__ vlib_thread_registration_t x; \
311 static void __vlib_add_thread_registration_##x (void) \
312  __attribute__((__constructor__)) ; \
313 static void __vlib_add_thread_registration_##x (void) \
314 { \
315  vlib_thread_main_t * tm = &vlib_thread_main; \
316  x.next = tm->next; \
317  tm->next = &x; \
318 } \
319 __VA_ARGS__ vlib_thread_registration_t x
320 
321 #endif /* included_vlib_threads_h */
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
void vlib_worker_thread_fork_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1078
static void vlib_worker_thread_barrier_check(void)
Definition: threads.h:201
void * thread_function_arg
Definition: threads.h:94
elog_track_t elog_track
Definition: threads.h:96
struct vlib_thread_registration_ * next
Definition: threads.h:30
volatile u32 valid
Definition: threads.h:72
int vlib_frame_queue_enqueue(vlib_main_t *vm, u32 node_runtime_index, u32 frame_queue_index, vlib_frame_t *frame, vlib_frame_queue_msg_type_t type)
vlib_node_state_t
Definition: node.h:175
vlib_thread_main_t vlib_thread_main
Definition: threads.h:307
u8 mpls_exp_bitmap
Definition: threads.h:251
vlib_thread_registration_t * next
Definition: threads.h:258
#define clib_smp_atomic_add(addr, increment)
Definition: smp.h:110
void vlib_worker_thread_init(vlib_worker_thread_t *w)
Definition: threads.c:439
#define always_inline
Definition: clib.h:84
u16 queue_hi_thresh
Definition: threads.h:249
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1131
uword * cpu_core_bitmap
Definition: threads.h:298
u64 dispatch_pending_node(vlib_main_t *vm, vlib_pending_frame_t *p, u64 last_time_stamp)
Definition: main.c:1076
unsigned long u64
Definition: types.h:89
vlib_frame_queue_elt_t * elts
Definition: threads.h:135
void * thread_mheap
Definition: threads.h:91
volatile u64 head
Definition: threads.h:121
vlib_node_type_t
Definition: node.h:57
uword os_get_cpu_number(void)
Definition: unix-misc.c:206
vlib_main_t ** vlib_mains
Definition: threads.h:20
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_FRAME_SIZE
Definition: node.h:292
vlib_frame_queue_t ** vlib_frame_queues
Definition: threads.h:139
u32 main_thread_is_io_node
Definition: threads.h:268
long i64
Definition: types.h:82
vlib_fork_fixup_t
Definition: threads.h:194
clib_error_t * vlib_thread_init(vlib_main_t *vm)
Definition: threads.c:132
vlib_worker_thread_t * vlib_alloc_thread(vlib_main_t *vm)
Definition: threads.c:269
struct vlib_thread_registration_ vlib_thread_registration_t
volatile u64 tail
Definition: threads.h:110
u8 ip_prec_bitmap
Definition: threads.h:250
u8 vlan_cos_bitmap
Definition: threads.h:252
vlib_worker_thread_t * worker_threads
Definition: threads.h:265
volatile u32 * wait_at_barrier
Definition: threads.h:86
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.h:106
struct vlib_efd_t vlib_efd_t
unsigned int u32
Definition: types.h:88
void vlib_set_thread_name(char *name)
Definition: threads.c:85
uword * thread_registrations_by_name
Definition: threads.h:263
void( vlib_thread_function_t)(void *arg)
Definition: threads.h:26
u64 dispatch_node(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_node_type_t type, vlib_node_state_t dispatch_state, vlib_frame_t *frame, u64 last_time_stamp)
Definition: main.c:918
u64 uword
Definition: types.h:112
vlib_frame_queue_msg_type_t
Definition: threads.h:67
void vlib_worker_thread_node_runtime_update(void)
Definition: threads.c:796
u32 enqueue_full_events
Definition: threads.h:114
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
u32 enqueue_efd_discards
Definition: threads.h:115
volatile u64 head_hint
Definition: threads.h:131
word fformat(FILE *f, char *fmt,...)
Definition: format.c:437
volatile u32 * workers_at_barrier
Definition: threads.h:87
int vlib_frame_queue_dequeue(int thread_id, vlib_main_t *vm, vlib_node_main_t *nm)
void vlib_create_worker_threads(vlib_main_t *vm, int n, void(*thread_function)(void *))
uword * cpu_socket_bitmap
Definition: threads.h:301
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1100
vlib_thread_registration_t ** registrations
Definition: threads.h:261
always_inline void vlib_smp_unsafe_warning(void)
Definition: threads.h:185
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
vlib_thread_registration_t * registration
Definition: threads.h:98
u16 enabled
Definition: threads.h:248
vlib_efd_t efd
Definition: threads.h:303