Merge "Add ODL honeycomb VPP agent extensions to vppjapi jni java library."
[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   /* All threads of this type run on pthreads */
41   int use_pthreads;
42   u32 first_index;
43   uword * coremask;
44 } vlib_thread_registration_t;
45
46 #define VLIB_MAX_CPUS 32
47
48 /* 
49  * Objects passed around by "index" are cache-line aligned.
50  * We can stick the owner CPU into the low 6 bits.
51  */
52 #if VLIB_MAX_CPUS > 64
53 #error VLIB_MAX_CPUS must be <= 64
54 #endif
55
56 #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */
57 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
58
59 #define VLIB_LOG2_THREAD_STACK_SIZE (20)
60 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
61
62 typedef enum {
63     VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
64 } vlib_frame_queue_msg_type_t;
65
66 typedef struct {
67   volatile u32 valid;
68   u32 msg_type;         
69   u32 n_vectors;
70   u32 last_n_vectors;
71
72   /* 256 * 4 = 1024 bytes, even mult of cache line size */
73   u32 buffer_index[VLIB_FRAME_SIZE];
74
75   /* Pad to a cache line boundary */
76   u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof(u32)];
77 } vlib_frame_queue_elt_t;
78
79 typedef struct {
80   /* First cache line */
81   volatile u32 *wait_at_barrier;
82   volatile u32 *workers_at_barrier;
83   u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
84
85   /* Second Cache Line */
86   void *thread_mheap;
87   u8 * thread_stack;
88   void (*thread_function)(void *);
89   void * thread_function_arg;
90   i64 recursion_level; 
91   elog_track_t elog_track; 
92   u32 instance_id;
93   vlib_thread_registration_t *registration;
94   u8 *name;
95
96   long lwp;
97   int dpdk_lcore_id;
98 } vlib_worker_thread_t;
99
100 vlib_worker_thread_t *vlib_worker_threads;
101
102 typedef struct {
103   /* enqueue side */
104   volatile u64 tail;
105   u64 enqueues;
106   u64 enqueue_ticks;
107   u64 enqueue_vectors;
108   u32 enqueue_full_events;
109   u32 enqueue_efd_discards;
110   u8 pad2[CLIB_CACHE_LINE_BYTES 
111           - (2 * sizeof(u32))
112           - (4 * sizeof(u64))];
113
114   /* dequeue side */
115   volatile u64 head;
116   u64 dequeues;
117   u64 dequeue_ticks;
118   u64 dequeue_vectors;
119   u64 trace;
120   u64 vector_threshold;
121   u8 pad4[CLIB_CACHE_LINE_BYTES 
122           - (6 * sizeof(u64))];
123
124   /* dequeue hint to enqueue side */
125   volatile u64 head_hint;
126   u8 pad5 [CLIB_CACHE_LINE_BYTES - sizeof(u64)];
127
128   /* read-only, constant, shared */
129   vlib_frame_queue_elt_t *elts;
130   u32 nelts;
131 } vlib_frame_queue_t;
132
133 vlib_frame_queue_t **vlib_frame_queues;
134
135 /* Called early, in thread 0's context */
136 clib_error_t * vlib_thread_init (vlib_main_t * vm);
137
138 vlib_worker_thread_t * vlib_alloc_thread (vlib_main_t * vm);
139
140 int vlib_frame_queue_enqueue (vlib_main_t *vm, u32 node_runtime_index,
141                               u32 frame_queue_index, vlib_frame_t *frame,
142                               vlib_frame_queue_msg_type_t type);
143
144 int vlib_frame_queue_dequeue (int thread_id, 
145                               vlib_main_t *vm, 
146                               vlib_node_main_t *nm);
147
148 u64 dispatch_node (vlib_main_t * vm,
149                    vlib_node_runtime_t * node,
150                    vlib_node_type_t type,
151                    vlib_node_state_t dispatch_state,
152                    vlib_frame_t * frame,
153                    u64 last_time_stamp);
154
155 u64 dispatch_pending_node (vlib_main_t * vm,
156                            vlib_pending_frame_t * p,
157                            u64 last_time_stamp);
158
159 void vlib_worker_thread_node_runtime_update(void);
160
161 void vlib_create_worker_threads (vlib_main_t *vm, int n, 
162                                  void (*thread_function)(void *));
163
164 void vlib_worker_thread_init (vlib_worker_thread_t * w);
165
166 /* Check for a barrier sync request every 30ms */
167 #define BARRIER_SYNC_DELAY (0.030000)
168
169 #if CLIB_DEBUG > 0
170 /* long barrier timeout, for gdb... */
171 #define BARRIER_SYNC_TIMEOUT (600.1)
172 #else
173 #define BARRIER_SYNC_TIMEOUT (1.0)
174 #endif
175
176 void vlib_worker_thread_barrier_sync(vlib_main_t *vm);
177 void vlib_worker_thread_barrier_release(vlib_main_t *vm);
178
179 always_inline void vlib_smp_unsafe_warning (void)
180 {
181   if (CLIB_DEBUG > 0)
182     {
183       if (os_get_cpu_number())
184         fformat(stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
185     }
186 }
187
188 typedef enum {
189     VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
190     VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
191 } vlib_fork_fixup_t;
192
193 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
194
195 static inline void vlib_worker_thread_barrier_check (void)
196 {
197     if (PREDICT_FALSE(*vlib_worker_threads->wait_at_barrier))
198     {
199         clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
200         while (*vlib_worker_threads->wait_at_barrier)
201             ;
202         clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
203     }
204 }
205
206 #define foreach_vlib_main(body)                                         \
207 do {                                                                    \
208     vlib_main_t ** __vlib_mains = 0, *this_vlib_main;                   \
209     int ii;                                                             \
210                                                                         \
211     if (vec_len (vlib_mains) == 0)                                      \
212         vec_add1 (__vlib_mains, &vlib_global_main);                     \
213     else                                                                \
214     {                                                                   \
215         for (ii = 0; ii < vec_len (vlib_mains); ii++)                   \
216         {                                                               \
217             this_vlib_main = vlib_mains[ii];                            \
218             if (this_vlib_main)                                         \
219                 vec_add1 (__vlib_mains, this_vlib_main);                \
220         }                                                               \
221     }                                                                   \
222                                                                         \
223     for (ii = 0; ii < vec_len (__vlib_mains); ii++)                     \
224     {                                                                   \
225         this_vlib_main = __vlib_mains[ii];                              \
226         /* body uses this_vlib_main... */                               \
227         (body);                                                         \
228     }                                                                   \
229     vec_free (__vlib_mains);                                            \
230 } while (0);
231
232
233 /* Early-Fast-Discard (EFD) */
234 #define VLIB_EFD_DISABLED                   0
235 #define VLIB_EFD_DISCARD_ENABLED            (1 << 0)
236 #define VLIB_EFD_MONITOR_ENABLED            (1 << 1)
237
238 #define VLIB_EFD_DEF_WORKER_HI_THRESH_PCT   90
239
240 /* EFD worker thread settings */
241 typedef struct vlib_efd_t {
242   u16 enabled;
243   u16 queue_hi_thresh;
244   u8  ip_prec_bitmap;
245   u8  mpls_exp_bitmap;
246   u8  vlan_cos_bitmap;
247   u8  pad;
248 } vlib_efd_t;
249
250 typedef struct {
251   /* Link list of registrations, built by constructors */
252   vlib_thread_registration_t * next;
253   
254   /* Vector of registrations, w/ non-data-structure clones at the top */
255   vlib_thread_registration_t ** registrations;
256
257   uword * thread_registrations_by_name;
258
259   vlib_worker_thread_t * worker_threads;
260
261   /* thread / cpu / io thread parameters */
262   u32 main_thread_is_io_node;
263
264   /* 
265    * Launch all threads as pthreads, 
266    * not eal_rte_launch (strict affinity) threads 
267    */
268   int use_pthreads;
269
270   /* Number of vlib_main / vnet_main clones */
271   u32 n_vlib_mains;
272
273   /* Number of thread stacks to create */
274   u32 n_thread_stacks;
275
276   /* Number of pthreads */
277   u32 n_pthreads;
278
279   /* Number of DPDK eal threads */
280   u32 n_eal_threads;
281
282   /* Number of cores to skip, must match the core mask */
283   u32 skip_cores;
284
285   /* Thread prefix name */
286   u8 *thread_prefix;
287
288   /* main thread lcore */
289   u8 main_lcore;
290
291   /* Bitmap of available CPU cores */
292   uword * cpu_core_bitmap;
293
294   /* Bitmap of available CPU sockets (NUMA nodes) */
295   uword * cpu_socket_bitmap;
296
297   vlib_efd_t efd;
298   
299 } vlib_thread_main_t;
300
301 vlib_thread_main_t vlib_thread_main;
302
303 #define VLIB_REGISTER_THREAD(x,...)                     \
304   __VA_ARGS__ vlib_thread_registration_t x;             \
305 static void __vlib_add_thread_registration_##x (void)   \
306   __attribute__((__constructor__)) ;                    \
307 static void __vlib_add_thread_registration_##x (void)   \
308 {                                                       \
309   vlib_thread_main_t * tm = &vlib_thread_main;          \
310   x.next = tm->next;                                    \
311   tm->next = &x;                                        \
312 }                                                       \
313 __VA_ARGS__ vlib_thread_registration_t x 
314
315 #endif /* included_vlib_threads_h */