FD.io VPP  v16.09
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 vlib_set_thread_name (char *name);
23 
24 /* arg is actually a vlib__thread_t * */
25 typedef void (vlib_thread_function_t) (void *arg);
26 
28 {
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
68 {
71 
72 typedef struct
73 {
74  volatile u32 valid;
78 
79  /* 256 * 4 = 1024 bytes, even mult of cache line size */
80  u32 buffer_index[VLIB_FRAME_SIZE];
81 
82  /* Pad to a cache line boundary */
83  u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof (u32)];
84 }
86 
87 typedef struct
88 {
89  /* First cache line */
90  volatile u32 *wait_at_barrier;
92  u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
93 
94  /* Second Cache Line */
95  void *thread_mheap;
97  void (*thread_function) (void *);
105 
106  long lwp;
109 
111 
112 typedef struct
113 {
114  /* enqueue side */
115  volatile u64 tail;
121  u8 pad2[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32)) - (4 * sizeof (u64))];
122 
123  /* dequeue side */
124  volatile u64 head;
130  u8 pad4[CLIB_CACHE_LINE_BYTES - (6 * sizeof (u64))];
131 
132  /* dequeue hint to enqueue side */
133  volatile u64 head_hint;
134  u8 pad5[CLIB_CACHE_LINE_BYTES - sizeof (u64)];
135 
136  /* read-only, constant, shared */
139 }
141 
143 
144 /* Called early, in thread 0's context */
146 
148 
149 int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
150  u32 frame_queue_index, vlib_frame_t * frame,
152 
153 int vlib_frame_queue_dequeue (int thread_id,
154  vlib_main_t * vm, vlib_node_main_t * nm);
155 
157  vlib_node_runtime_t * node,
159  vlib_node_state_t dispatch_state,
160  vlib_frame_t * frame, u64 last_time_stamp);
161 
163  vlib_pending_frame_t * p, 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 
185 always_inline void
187 {
188  if (CLIB_DEBUG > 0)
189  {
190  if (os_get_cpu_number ())
191  fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
192  }
193 }
194 
195 typedef enum
196 {
200 
201 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
202 
203 static inline void
205 {
206  if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
207  {
208  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
209  while (*vlib_worker_threads->wait_at_barrier)
210  ;
211  clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
212  }
213 }
214 
215 #define foreach_vlib_main(body) \
216 do { \
217  vlib_main_t ** __vlib_mains = 0, *this_vlib_main; \
218  int ii; \
219  \
220  if (vec_len (vlib_mains) == 0) \
221  vec_add1 (__vlib_mains, &vlib_global_main); \
222  else \
223  { \
224  for (ii = 0; ii < vec_len (vlib_mains); ii++) \
225  { \
226  this_vlib_main = vlib_mains[ii]; \
227  if (this_vlib_main) \
228  vec_add1 (__vlib_mains, this_vlib_main); \
229  } \
230  } \
231  \
232  for (ii = 0; ii < vec_len (__vlib_mains); ii++) \
233  { \
234  this_vlib_main = __vlib_mains[ii]; \
235  /* body uses this_vlib_main... */ \
236  (body); \
237  } \
238  vec_free (__vlib_mains); \
239 } while (0);
240 
241 
242 /* Early-Fast-Discard (EFD) */
243 #define VLIB_EFD_DISABLED 0
244 #define VLIB_EFD_DISCARD_ENABLED (1 << 0)
245 #define VLIB_EFD_MONITOR_ENABLED (1 << 1)
246 
247 #define VLIB_EFD_DEF_WORKER_HI_THRESH_PCT 90
248 
249 /* EFD worker thread settings */
250 typedef struct vlib_efd_t
251 {
258 } vlib_efd_t;
259 
260 typedef struct
261 {
262  /* Link list of registrations, built by constructors */
264 
265  /* Vector of registrations, w/ non-data-structure clones at the top */
267 
269 
271 
272  /*
273  * Launch all threads as pthreads,
274  * not eal_rte_launch (strict affinity) threads
275  */
277 
278  /* Number of vlib_main / vnet_main clones */
280 
281  /* Number of thread stacks to create */
283 
284  /* Number of pthreads */
286 
287  /* Number of DPDK eal threads */
289 
290  /* Number of cores to skip, must match the core mask */
292 
293  /* Thread prefix name */
295 
296  /* main thread lcore */
298 
299  /* Bitmap of available CPU cores */
301 
302  /* Bitmap of available CPU sockets (NUMA nodes) */
304 
306 
307  /* handoff node index */
309 
310  /* for frame queue tracing */
313 
314  /* worker thread initialization barrier */
316 
318 
320 
321 #define VLIB_REGISTER_THREAD(x,...) \
322  __VA_ARGS__ vlib_thread_registration_t x; \
323 static void __vlib_add_thread_registration_##x (void) \
324  __attribute__((__constructor__)) ; \
325 static void __vlib_add_thread_registration_##x (void) \
326 { \
327  vlib_thread_main_t * tm = &vlib_thread_main; \
328  x.next = tm->next; \
329  tm->next = &x; \
330 } \
331 __VA_ARGS__ vlib_thread_registration_t x
332 
333 #endif /* included_vlib_threads_h */
334 
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
void vlib_worker_thread_init(vlib_worker_thread_t *w)
Definition: threads.c:463
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)
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
vlib_worker_thread_t * vlib_alloc_thread(vlib_main_t *vm)
Definition: threads.c:287
void vlib_set_thread_name(char *name)
Definition: threads.c:93
void * thread_function_arg
Definition: threads.h:98
elog_track_t elog_track
Definition: threads.h:100
frame_queue_trace_t * frame_queue_traces
Definition: threads.h:311
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.h:110
struct vlib_thread_registration_ * next
Definition: threads.h:30
volatile u32 valid
Definition: threads.h:74
vlib_node_state_t
Definition: node.h:207
static void vlib_smp_unsafe_warning(void)
Definition: threads.h:186
u32 handoff_dispatch_node_index
Definition: threads.h:308
u8 mpls_exp_bitmap
Definition: threads.h:255
static void vlib_worker_thread_barrier_check(void)
Definition: threads.h:204
vlib_thread_registration_t * next
Definition: threads.h:263
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:919
#define clib_smp_atomic_add(addr, increment)
Definition: smp.h:46
#define always_inline
Definition: clib.h:84
vlib_frame_queue_msg_type_t
Definition: threads.h:67
u16 queue_hi_thresh
Definition: threads.h:253
uword * cpu_core_bitmap
Definition: threads.h:300
unsigned long u64
Definition: types.h:89
vlib_frame_queue_elt_t * elts
Definition: threads.h:137
vlib_frame_queue_t ** vlib_frame_queues
Definition: threads.h:142
vlib_fork_fixup_t
Definition: threads.h:195
void * thread_mheap
Definition: threads.h:95
volatile u64 head
Definition: threads.h:124
vlib_node_type_t
Definition: node.h:58
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1176
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_FRAME_SIZE
Definition: node.h:328
long i64
Definition: types.h:82
void vlib_worker_thread_node_runtime_update(void)
Definition: threads.c:831
struct vlib_efd_t vlib_efd_t
void vlib_worker_thread_fork_fixup(vlib_fork_fixup_t which)
Definition: threads.c:1121
volatile u64 tail
Definition: threads.h:115
u8 ip_prec_bitmap
Definition: threads.h:254
u8 vlan_cos_bitmap
Definition: threads.h:256
vlib_worker_thread_t * worker_threads
Definition: threads.h:270
int vlib_frame_queue_dequeue(int thread_id, vlib_main_t *vm, vlib_node_main_t *nm)
volatile u32 * wait_at_barrier
Definition: threads.h:90
frame_queue_nelt_counter_t * frame_queue_histogram
Definition: threads.h:312
unsigned int u32
Definition: types.h:88
struct vlib_thread_registration_ vlib_thread_registration_t
uword * thread_registrations_by_name
Definition: threads.h:268
clib_error_t * vlib_thread_init(vlib_main_t *vm)
Definition: threads.c:148
u64 uword
Definition: types.h:112
u32 enqueue_full_events
Definition: threads.h:119
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
vlib_main_t ** vlib_mains
Definition: threads.h:20
u32 enqueue_efd_discards
Definition: threads.h:120
void vlib_create_worker_threads(vlib_main_t *vm, int n, void(*thread_function)(void *))
volatile u64 head_hint
Definition: threads.h:133
word fformat(FILE *f, char *fmt,...)
Definition: format.c:452
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1144
volatile u32 * workers_at_barrier
Definition: threads.h:91
u64 dispatch_pending_node(vlib_main_t *vm, vlib_pending_frame_t *p, u64 last_time_stamp)
Definition: main.c:1087
uword * cpu_socket_bitmap
Definition: threads.h:303
void( vlib_thread_function_t)(void *arg)
Definition: threads.h:25
vlib_thread_registration_t ** registrations
Definition: threads.h:266
vlib_thread_main_t vlib_thread_main
Definition: threads.h:319
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
vlib_thread_registration_t * registration
Definition: threads.h:102
volatile u32 worker_thread_release
Definition: threads.h:315
u16 enabled
Definition: threads.h:252
vlib_efd_t efd
Definition: threads.h:305