4283b70a61d31b3e284354f82eabc44c7608f4e9
[vpp.git] / vlib / vlib / threads.h
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
20 vlib_main_t **vlib_mains;
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
27 typedef struct vlib_thread_registration_
28 {
29   /* constructor generated list of thread registrations */
30   struct vlib_thread_registration_ *next;
31
32   /* config parameters */
33   char *name;
34   char *short_name;
35   vlib_thread_function_t *function;
36   uword mheap_size;
37   int fixed_count;
38   u32 count;
39   int no_data_structure_clone;
40   u32 frame_queue_nelts;
41
42   /* All threads of this type run on pthreads */
43   int use_pthreads;
44   u32 first_index;
45   uword *coremask;
46 } vlib_thread_registration_t;
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 {
69   VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
70 } vlib_frame_queue_msg_type_t;
71
72 typedef struct
73 {
74   volatile u32 valid;
75   u32 msg_type;
76   u32 n_vectors;
77   u32 last_n_vectors;
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 }
85 vlib_frame_queue_elt_t;
86
87 typedef struct
88 {
89   /* First cache line */
90   volatile u32 *wait_at_barrier;
91   volatile u32 *workers_at_barrier;
92   u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
93
94   /* Second Cache Line */
95   void *thread_mheap;
96   u8 *thread_stack;
97   void (*thread_function) (void *);
98   void *thread_function_arg;
99   i64 recursion_level;
100   elog_track_t elog_track;
101   u32 instance_id;
102   vlib_thread_registration_t *registration;
103   u8 *name;
104   u64 barrier_sync_count;
105
106   long lwp;
107   int dpdk_lcore_id;
108 } vlib_worker_thread_t;
109
110 vlib_worker_thread_t *vlib_worker_threads;
111
112 typedef struct
113 {
114   /* enqueue side */
115   volatile u64 tail;
116   u64 enqueues;
117   u64 enqueue_ticks;
118   u64 enqueue_vectors;
119   u32 enqueue_full_events;
120   u32 enqueue_efd_discards;
121   u8 pad2[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32)) - (4 * sizeof (u64))];
122
123   /* dequeue side */
124   volatile u64 head;
125   u64 dequeues;
126   u64 dequeue_ticks;
127   u64 dequeue_vectors;
128   u64 trace;
129   u64 vector_threshold;
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 */
137   vlib_frame_queue_elt_t *elts;
138   u32 nelts;
139 }
140 vlib_frame_queue_t;
141
142 vlib_frame_queue_t **vlib_frame_queues;
143
144 /* Called early, in thread 0's context */
145 clib_error_t *vlib_thread_init (vlib_main_t * vm);
146
147 vlib_worker_thread_t *vlib_alloc_thread (vlib_main_t * vm);
148
149 int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
150                               u32 frame_queue_index, vlib_frame_t * frame,
151                               vlib_frame_queue_msg_type_t type);
152
153 int vlib_frame_queue_dequeue (int thread_id,
154                               vlib_main_t * vm, vlib_node_main_t * nm);
155
156 u64 dispatch_node (vlib_main_t * vm,
157                    vlib_node_runtime_t * node,
158                    vlib_node_type_t type,
159                    vlib_node_state_t dispatch_state,
160                    vlib_frame_t * frame, u64 last_time_stamp);
161
162 u64 dispatch_pending_node (vlib_main_t * vm,
163                            vlib_pending_frame_t * p, u64 last_time_stamp);
164
165 void vlib_worker_thread_node_runtime_update (void);
166
167 void vlib_create_worker_threads (vlib_main_t * vm, int n,
168                                  void (*thread_function) (void *));
169
170 void vlib_worker_thread_init (vlib_worker_thread_t * w);
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
182 void vlib_worker_thread_barrier_sync (vlib_main_t * vm);
183 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
184
185 always_inline void
186 vlib_smp_unsafe_warning (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 {
197   VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
198   VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
199 } vlib_fork_fixup_t;
200
201 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
202
203 static inline void
204 vlib_worker_thread_barrier_check (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 {
252   u16 enabled;
253   u16 queue_hi_thresh;
254   u8 ip_prec_bitmap;
255   u8 mpls_exp_bitmap;
256   u8 vlan_cos_bitmap;
257   u8 pad;
258 } vlib_efd_t;
259
260 typedef struct
261 {
262   /* Link list of registrations, built by constructors */
263   vlib_thread_registration_t *next;
264
265   /* Vector of registrations, w/ non-data-structure clones at the top */
266   vlib_thread_registration_t **registrations;
267
268   uword *thread_registrations_by_name;
269
270   vlib_worker_thread_t *worker_threads;
271
272   /*
273    * Launch all threads as pthreads,
274    * not eal_rte_launch (strict affinity) threads
275    */
276   int use_pthreads;
277
278   /* Number of vlib_main / vnet_main clones */
279   u32 n_vlib_mains;
280
281   /* Number of thread stacks to create */
282   u32 n_thread_stacks;
283
284   /* Number of pthreads */
285   u32 n_pthreads;
286
287   /* Number of DPDK eal threads */
288   u32 n_eal_threads;
289
290   /* Number of cores to skip, must match the core mask */
291   u32 skip_cores;
292
293   /* Thread prefix name */
294   u8 *thread_prefix;
295
296   /* main thread lcore */
297   u8 main_lcore;
298
299   /* Bitmap of available CPU cores */
300   uword *cpu_core_bitmap;
301
302   /* Bitmap of available CPU sockets (NUMA nodes) */
303   uword *cpu_socket_bitmap;
304
305   vlib_efd_t efd;
306
307   /* handoff node index */
308   u32 handoff_dispatch_node_index;
309
310   /* for frame queue tracing */
311   frame_queue_trace_t *frame_queue_traces;
312   frame_queue_nelt_counter_t *frame_queue_histogram;
313
314   /* worker thread initialization barrier */
315   volatile u32 worker_thread_release;
316
317 } vlib_thread_main_t;
318
319 vlib_thread_main_t vlib_thread_main;
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  */