Decouple worker thread code from dpdk, enable worker threads in vpp_lite
[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
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 */
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     VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
69 } vlib_frame_queue_msg_type_t;
70
71 typedef struct {
72   volatile u32 valid;
73   u32 msg_type;         
74   u32 n_vectors;
75   u32 last_n_vectors;
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)];
82 } vlib_frame_queue_elt_t;
83
84 typedef struct {
85   /* First cache line */
86   volatile u32 *wait_at_barrier;
87   volatile u32 *workers_at_barrier;
88   u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
89
90   /* Second Cache Line */
91   void *thread_mheap;
92   u8 * thread_stack;
93   void (*thread_function)(void *);
94   void * thread_function_arg;
95   i64 recursion_level; 
96   elog_track_t elog_track; 
97   u32 instance_id;
98   vlib_thread_registration_t *registration;
99   u8 *name;
100   u64 barrier_sync_count;
101
102   long lwp;
103   int dpdk_lcore_id;
104 } vlib_worker_thread_t;
105
106 vlib_worker_thread_t *vlib_worker_threads;
107
108 typedef struct {
109   /* enqueue side */
110   volatile u64 tail;
111   u64 enqueues;
112   u64 enqueue_ticks;
113   u64 enqueue_vectors;
114   u32 enqueue_full_events;
115   u32 enqueue_efd_discards;
116   u8 pad2[CLIB_CACHE_LINE_BYTES 
117           - (2 * sizeof(u32))
118           - (4 * sizeof(u64))];
119
120   /* dequeue side */
121   volatile u64 head;
122   u64 dequeues;
123   u64 dequeue_ticks;
124   u64 dequeue_vectors;
125   u64 trace;
126   u64 vector_threshold;
127   u8 pad4[CLIB_CACHE_LINE_BYTES 
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 */
135   vlib_frame_queue_elt_t *elts;
136   u32 nelts;
137 } vlib_frame_queue_t;
138
139 vlib_frame_queue_t **vlib_frame_queues;
140
141 /* Called early, in thread 0's context */
142 clib_error_t * vlib_thread_init (vlib_main_t * vm);
143
144 vlib_worker_thread_t * vlib_alloc_thread (vlib_main_t * vm);
145
146 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
147                               u32 frame_queue_index, vlib_frame_t *frame,
148                               vlib_frame_queue_msg_type_t type);
149
150 int vlib_frame_queue_dequeue (int thread_id, 
151                               vlib_main_t *vm, 
152                               vlib_node_main_t *nm);
153
154 u64 dispatch_node (vlib_main_t * vm,
155                    vlib_node_runtime_t * node,
156                    vlib_node_type_t type,
157                    vlib_node_state_t dispatch_state,
158                    vlib_frame_t * frame,
159                    u64 last_time_stamp);
160
161 u64 dispatch_pending_node (vlib_main_t * vm,
162                            vlib_pending_frame_t * p,
163                            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 vlib_smp_unsafe_warning (void)
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 {
195     VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
196     VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
197 } vlib_fork_fixup_t;
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 {
248   u16 enabled;
249   u16 queue_hi_thresh;
250   u8  ip_prec_bitmap;
251   u8  mpls_exp_bitmap;
252   u8  vlan_cos_bitmap;
253   u8  pad;
254 } vlib_efd_t;
255
256 typedef struct {
257   /* Link list of registrations, built by constructors */
258   vlib_thread_registration_t * next;
259   
260   /* Vector of registrations, w/ non-data-structure clones at the top */
261   vlib_thread_registration_t ** registrations;
262
263   uword * thread_registrations_by_name;
264
265   vlib_worker_thread_t * worker_threads;
266
267   /* 
268    * Launch all threads as pthreads, 
269    * not eal_rte_launch (strict affinity) threads 
270    */
271   int use_pthreads;
272
273   /* Number of vlib_main / vnet_main clones */
274   u32 n_vlib_mains;
275
276   /* Number of thread stacks to create */
277   u32 n_thread_stacks;
278
279   /* Number of pthreads */
280   u32 n_pthreads;
281
282   /* Number of DPDK eal threads */
283   u32 n_eal_threads;
284
285   /* Number of cores to skip, must match the core mask */
286   u32 skip_cores;
287
288   /* Thread prefix name */
289   u8 *thread_prefix;
290
291   /* main thread lcore */
292   u8 main_lcore;
293
294   /* Bitmap of available CPU cores */
295   uword * cpu_core_bitmap;
296
297   /* Bitmap of available CPU sockets (NUMA nodes) */
298   uword * cpu_socket_bitmap;
299
300   vlib_efd_t efd;
301
302   /* handoff node index */
303   u32 handoff_dispatch_node_index;
304
305   /* for frame queue tracing */
306   frame_queue_trace_t        *frame_queue_traces;
307   frame_queue_nelt_counter_t *frame_queue_histogram;
308
309   /* worker thread initialization barrier */
310   volatile u32 worker_thread_release;
311
312 } vlib_thread_main_t;
313
314 vlib_thread_main_t vlib_thread_main;
315
316 #define VLIB_REGISTER_THREAD(x,...)                     \
317   __VA_ARGS__ vlib_thread_registration_t x;             \
318 static void __vlib_add_thread_registration_##x (void)   \
319   __attribute__((__constructor__)) ;                    \
320 static void __vlib_add_thread_registration_##x (void)   \
321 {                                                       \
322   vlib_thread_main_t * tm = &vlib_thread_main;          \
323   x.next = tm->next;                                    \
324   tm->next = &x;                                        \
325 }                                                       \
326 __VA_ARGS__ vlib_thread_registration_t x 
327
328 #endif /* included_vlib_threads_h */