Use thread local storage for thread index
[vpp.git] / src / vlib / main.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 /*
16  * main.h: VLIB main data structure
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_vlib_main_h
41 #define included_vlib_main_h
42
43 #include <vppinfra/elog.h>
44 #include <vppinfra/format.h>
45 #include <vppinfra/longjmp.h>
46 #include <vppinfra/pool.h>
47 #include <vppinfra/random_buffer.h>
48 #include <vppinfra/time.h>
49
50 #include <pthread.h>
51
52
53 /* By default turn off node/error event logging.
54    Override with -DVLIB_ELOG_MAIN_LOOP */
55 #ifndef VLIB_ELOG_MAIN_LOOP
56 #define VLIB_ELOG_MAIN_LOOP 0
57 #endif
58
59 typedef struct vlib_main_t
60 {
61   /* Instruction level timing state. */
62   clib_time_t clib_time;
63
64   /* Time stamp of last node dispatch. */
65   u64 cpu_time_last_node_dispatch;
66
67   /* Time stamp when main loop was entered (time 0). */
68   u64 cpu_time_main_loop_start;
69
70   /* Incremented once for each main loop. */
71   u32 main_loop_count;
72
73   /* Count of vectors processed this main loop. */
74   u32 main_loop_vectors_processed;
75   u32 main_loop_nodes_processed;
76
77   /* Circular buffer of input node vector counts.
78      Indexed by low bits of
79      (main_loop_count >> VLIB_LOG2_INPUT_VECTORS_PER_MAIN_LOOP). */
80   u32 vector_counts_per_main_loop[2];
81   u32 node_counts_per_main_loop[2];
82
83   /* Every so often we switch to the next counter. */
84 #define VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE 7
85
86   /* Jump target to exit main loop with given code. */
87   u32 main_loop_exit_set;
88   clib_longjmp_t main_loop_exit;
89 #define VLIB_MAIN_LOOP_EXIT_NONE 0
90 #define VLIB_MAIN_LOOP_EXIT_PANIC 1
91   /* Exit via CLI. */
92 #define VLIB_MAIN_LOOP_EXIT_CLI 2
93
94   /* Error marker to use when exiting main loop. */
95   clib_error_t *main_loop_error;
96
97   /* Name for e.g. syslog. */
98   char *name;
99
100   /* Start and size of CLIB heap. */
101   void *heap_base;
102   uword heap_size;
103
104   vlib_buffer_main_t *buffer_main;
105
106   vlib_physmem_main_t physmem_main;
107
108   /* Allocate/free buffer memory for DMA transfers, descriptor rings, etc.
109      buffer memory is guaranteed to be cache-aligned. */
110   void *(*os_physmem_alloc_aligned) (vlib_physmem_main_t * pm,
111                                      uword n_bytes, uword alignment);
112   void (*os_physmem_free) (void *x);
113
114   /* Node graph main structure. */
115   vlib_node_main_t node_main;
116
117   /* Command line interface. */
118   vlib_cli_main_t cli_main;
119
120   /* Packet trace buffer. */
121   vlib_trace_main_t trace_main;
122
123   /* Error handling. */
124   vlib_error_main_t error_main;
125
126   /* Punt packets to underlying operating system for when fast switching
127      code does not know what to do. */
128   void (*os_punt_frame) (struct vlib_main_t * vm,
129                          struct vlib_node_runtime_t * node,
130                          vlib_frame_t * frame);
131
132   /* Multicast distribution.  Set to zero for MC disabled. */
133   mc_main_t *mc_main;
134
135   /* Stream index to use for distribution when MC is enabled. */
136   u32 mc_stream_index;
137
138   vlib_one_time_waiting_process_t *procs_waiting_for_mc_stream_join;
139
140   /* Event logger. */
141   elog_main_t elog_main;
142
143   /* Node call and return event types. */
144   elog_event_type_t *node_call_elog_event_types;
145   elog_event_type_t *node_return_elog_event_types;
146
147   elog_event_type_t *error_elog_event_types;
148
149   /* Seed for random number generator. */
150   uword random_seed;
151
152   /* Buffer of random data for various uses. */
153   clib_random_buffer_t random_buffer;
154
155   /* Hash table to record which init functions have been called. */
156   uword *init_functions_called;
157
158   /* to compare with node runtime */
159   u32 thread_index;
160
161   void **mbuf_alloc_list;
162
163   /* List of init functions to call, setup by constructors */
164   _vlib_init_function_list_elt_t *init_function_registrations;
165   _vlib_init_function_list_elt_t *worker_init_function_registrations;
166   _vlib_init_function_list_elt_t *main_loop_enter_function_registrations;
167   _vlib_init_function_list_elt_t *main_loop_exit_function_registrations;
168   _vlib_init_function_list_elt_t *api_init_function_registrations;
169   vlib_config_function_runtime_t *config_function_registrations;
170   mc_serialize_msg_t *mc_msg_registrations;     /* mc_main is a pointer... */
171
172   /* control-plane API queue signal pending, length indication */
173   volatile u32 queue_signal_pending;
174   volatile u32 api_queue_nonempty;
175   void (*queue_signal_callback) (struct vlib_main_t *);
176   u8 **argv;
177
178   /* debugging */
179   volatile int parked_at_barrier;
180 } vlib_main_t;
181
182 /* Global main structure. */
183 extern vlib_main_t vlib_global_main;
184
185 void vlib_worker_loop (vlib_main_t * vm);
186
187 always_inline f64
188 vlib_time_now (vlib_main_t * vm)
189 {
190   return clib_time_now (&vm->clib_time);
191 }
192
193 always_inline f64
194 vlib_time_now_ticks (vlib_main_t * vm, u64 n)
195 {
196   return clib_time_now_internal (&vm->clib_time, n);
197 }
198
199 /* Busy wait for specified time. */
200 always_inline void
201 vlib_time_wait (vlib_main_t * vm, f64 wait)
202 {
203   f64 t = vlib_time_now (vm);
204   f64 limit = t + wait;
205   while (t < limit)
206     t = vlib_time_now (vm);
207 }
208
209 /* Time a piece of code. */
210 #define vlib_time_code(vm,body)                 \
211 do {                                            \
212     f64 _t[2];                                  \
213     _t[0] = vlib_time_now (vm);                 \
214     do { body; } while (0);                     \
215     _t[1] = vlib_time_now (vm);                 \
216     clib_warning ("%.7e", _t[1] - _t[0]);       \
217 } while (0)
218
219 #define vlib_wait_with_timeout(vm,suspend_time,timeout_time,test)       \
220 ({                                                                      \
221     uword __vlib_wait_with_timeout = 0;                                 \
222     f64 __vlib_wait_time = 0;                                           \
223     while (! (__vlib_wait_with_timeout = (test))                        \
224            && __vlib_wait_time < (timeout_time))                        \
225       {                                                                 \
226         vlib_process_suspend (vm, suspend_time);                        \
227         __vlib_wait_time += suspend_time;                               \
228       }                                                                 \
229     __vlib_wait_with_timeout;                                           \
230 })
231
232 always_inline void
233 vlib_panic_with_error (vlib_main_t * vm, clib_error_t * error)
234 {
235   vm->main_loop_error = error;
236   clib_longjmp (&vm->main_loop_exit, VLIB_MAIN_LOOP_EXIT_PANIC);
237 }
238
239 #define vlib_panic_with_msg(vm,args...) \
240   vlib_panic_with_error (vm, clib_error_return (0, args))
241
242 always_inline void
243 vlib_panic (vlib_main_t * vm)
244 {
245   vlib_panic_with_error (vm, 0);
246 }
247
248 always_inline u32
249 vlib_vector_input_stats_index (vlib_main_t * vm, word delta)
250 {
251   u32 i;
252   i = vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE;
253   ASSERT (is_pow2 (ARRAY_LEN (vm->vector_counts_per_main_loop)));
254   return (i + delta) & (ARRAY_LEN (vm->vector_counts_per_main_loop) - 1);
255 }
256
257 /* Estimate input rate based on previous
258    2^VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE
259    samples. */
260 always_inline u32
261 vlib_last_vectors_per_main_loop (vlib_main_t * vm)
262 {
263   u32 i = vlib_vector_input_stats_index (vm, -1);
264   u32 n = vm->vector_counts_per_main_loop[i];
265   return n >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE;
266 }
267
268 /* Total ave vector count per iteration of main loop. */
269 always_inline f64
270 vlib_last_vectors_per_main_loop_as_f64 (vlib_main_t * vm)
271 {
272   u32 i = vlib_vector_input_stats_index (vm, -1);
273   u32 v = vm->vector_counts_per_main_loop[i];
274   return (f64) v / (f64) (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE);
275 }
276
277 /* Total ave vectors/node count per iteration of main loop. */
278 always_inline f64
279 vlib_last_vector_length_per_node (vlib_main_t * vm)
280 {
281   u32 i = vlib_vector_input_stats_index (vm, -1);
282   u32 v = vm->vector_counts_per_main_loop[i];
283   u32 n = vm->node_counts_per_main_loop[i];
284   return n == 0 ? 0 : (f64) v / (f64) n;
285 }
286
287 extern u32 wraps;
288
289 always_inline void
290 vlib_increment_main_loop_counter (vlib_main_t * vm)
291 {
292   u32 i, c, n, v, is_wrap;
293
294   c = vm->main_loop_count++;
295
296   is_wrap = (c & pow2_mask (VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)) == 0;
297
298   if (is_wrap)
299     wraps++;
300
301   i = vlib_vector_input_stats_index (vm, /* delta */ is_wrap);
302
303   v = is_wrap ? 0 : vm->vector_counts_per_main_loop[i];
304   n = is_wrap ? 0 : vm->node_counts_per_main_loop[i];
305
306   v += vm->main_loop_vectors_processed;
307   n += vm->main_loop_nodes_processed;
308   vm->main_loop_vectors_processed = 0;
309   vm->main_loop_nodes_processed = 0;
310   vm->vector_counts_per_main_loop[i] = v;
311   vm->node_counts_per_main_loop[i] = n;
312 }
313
314 always_inline void vlib_set_queue_signal_callback
315   (vlib_main_t * vm, void (*fp) (vlib_main_t *))
316 {
317   vm->queue_signal_callback = fp;
318 }
319
320 /* Main routine. */
321 int vlib_main (vlib_main_t * vm, unformat_input_t * input);
322
323 /* Thread stacks, for os_get_cpu_number */
324 extern u8 **vlib_thread_stacks;
325
326 /* Number of thread stacks that the application needs */
327 u32 vlib_app_num_thread_stacks_needed (void) __attribute__ ((weak));
328
329 extern void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
330
331 #endif /* included_vlib_main_h */
332
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */