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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * main.c: main vector processing loop
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
41 #include <vppinfra/format.h>
42 #include <vlib/vlib.h>
43 #include <vlib/threads.h>
44 #include <vlib/stats/stats.h>
45 #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
47 #include <vlib/unix/unix.h>
49 #define VLIB_FRAME_MAGIC (0xabadc0ed)
52 vlib_frame_find_magic (vlib_frame_t * f, vlib_node_t * node)
54 return (void *) f + node->magic_offset;
58 vlib_frame_alloc_to_node (vlib_main_t * vm, u32 to_node_index,
61 vlib_node_main_t *nm = &vm->node_main;
62 vlib_frame_size_t *fs;
67 ASSERT (vm == vlib_get_main ());
69 to_node = vlib_get_node (vm, to_node_index);
71 vec_validate (nm->frame_sizes, to_node->frame_size_index);
72 fs = vec_elt_at_index (nm->frame_sizes, to_node->frame_size_index);
74 if (fs->frame_size == 0)
75 fs->frame_size = to_node->frame_size;
77 ASSERT (fs->frame_size == to_node->frame_size);
80 if ((l = vec_len (fs->free_frames)) > 0)
82 /* Allocate from end of free list. */
83 f = fs->free_frames[l - 1];
84 vec_set_len (fs->free_frames, l - 1);
88 f = clib_mem_alloc_aligned_no_fail (n, CLIB_CACHE_LINE_BYTES);
91 /* Poison frame when debugging. */
93 clib_memset_u8 (f, 0xfe, n);
95 /* Insert magic number. */
99 magic = vlib_frame_find_magic (f, to_node);
100 *magic = VLIB_FRAME_MAGIC;
103 f->frame_flags = VLIB_FRAME_IS_ALLOCATED | frame_flags;
105 f->scalar_offset = to_node->scalar_offset;
106 f->vector_offset = to_node->vector_offset;
107 f->aux_offset = to_node->aux_offset;
110 fs->n_alloc_frames += 1;
115 /* Allocate a frame for from FROM_NODE to TO_NODE via TO_NEXT_INDEX.
116 Returns frame index. */
117 static vlib_frame_t *
118 vlib_frame_alloc (vlib_main_t * vm, vlib_node_runtime_t * from_node_runtime,
121 vlib_node_t *from_node;
123 from_node = vlib_get_node (vm, from_node_runtime->node_index);
124 ASSERT (to_next_index < vec_len (from_node->next_nodes));
126 return vlib_frame_alloc_to_node (vm, from_node->next_nodes[to_next_index],
127 /* frame_flags */ 0);
131 vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index)
133 vlib_frame_t *f = vlib_frame_alloc_to_node (vm, to_node_index,
135 VLIB_FRAME_FREE_AFTER_DISPATCH);
136 return vlib_get_frame (vm, f);
140 vlib_validate_frame_indices (vlib_frame_t * f)
145 u32 *from = vlib_frame_vector_args (f);
147 /* Check for bad buffer index values */
148 for (i = 0; i < f->n_vectors; i++)
152 clib_warning ("BUG: buffer index 0 at index %d", i);
155 else if (from[i] == 0xfefefefe)
157 clib_warning ("BUG: frame poison pattern at index %d", i);
165 vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index, vlib_frame_t * f)
167 vlib_pending_frame_t *p;
168 vlib_node_t *to_node;
170 if (f->n_vectors == 0)
173 ASSERT (vm == vlib_get_main ());
175 vlib_validate_frame_indices (f);
177 to_node = vlib_get_node (vm, to_node_index);
179 vec_add2 (vm->node_main.pending_frames, p, 1);
181 f->frame_flags |= VLIB_FRAME_PENDING;
182 p->frame = vlib_get_frame (vm, f);
183 p->node_runtime_index = to_node->runtime_index;
184 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
187 /* Free given frame. */
189 vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f)
191 vlib_node_main_t *nm = &vm->node_main;
193 vlib_frame_size_t *fs;
195 ASSERT (vm == vlib_get_main ());
196 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
198 node = vlib_get_node (vm, r->node_index);
199 fs = vec_elt_at_index (nm->frame_sizes, node->frame_size_index);
201 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
203 /* No next frames may point to freed frame. */
206 vlib_next_frame_t *nf;
207 vec_foreach (nf, vm->node_main.next_frames) ASSERT (nf->frame != f);
210 f->frame_flags &= ~(VLIB_FRAME_IS_ALLOCATED | VLIB_FRAME_NO_APPEND);
213 vec_add1 (fs->free_frames, f);
214 ASSERT (fs->n_alloc_frames > 0);
215 fs->n_alloc_frames -= 1;
218 static clib_error_t *
219 show_frame_stats (vlib_main_t * vm,
220 unformat_input_t * input, vlib_cli_command_t * cmd)
222 vlib_frame_size_t *fs;
224 vlib_cli_output (vm, "%=8s%=6s%=12s%=12s", "Thread", "Size", "# Alloc",
228 vlib_node_main_t *nm = &this_vlib_main->node_main;
229 vec_foreach (fs, nm->frame_sizes)
231 u32 n_alloc = fs->n_alloc_frames;
232 u32 n_free = vec_len (fs->free_frames);
234 if (n_alloc + n_free > 0)
235 vlib_cli_output (vm, "%=8d%=6d%=12d%=12d",
236 this_vlib_main->thread_index, fs->frame_size,
245 VLIB_CLI_COMMAND (show_frame_stats_cli, static) = {
246 .path = "show vlib frame-allocation",
247 .short_help = "Show node dispatch frame statistics",
248 .function = show_frame_stats,
252 /* Change ownership of enqueue rights to given next node. */
254 vlib_next_frame_change_ownership (vlib_main_t * vm,
255 vlib_node_runtime_t * node_runtime,
258 vlib_node_main_t *nm = &vm->node_main;
259 vlib_next_frame_t *next_frame;
260 vlib_node_t *node, *next_node;
262 node = vec_elt (nm->nodes, node_runtime->node_index);
264 /* Only internal & input nodes are allowed to call other nodes. */
265 ASSERT (node->type == VLIB_NODE_TYPE_INTERNAL
266 || node->type == VLIB_NODE_TYPE_INPUT
267 || node->type == VLIB_NODE_TYPE_PROCESS);
269 ASSERT (vec_len (node->next_nodes) == node_runtime->n_next_nodes);
272 vlib_node_runtime_get_next_frame (vm, node_runtime, next_index);
273 next_node = vec_elt (nm->nodes, node->next_nodes[next_index]);
275 if (next_node->owner_node_index != VLIB_INVALID_NODE_INDEX)
277 /* Get frame from previous owner. */
278 vlib_next_frame_t *owner_next_frame;
279 vlib_next_frame_t tmp;
282 vlib_node_get_next_frame (vm,
283 next_node->owner_node_index,
284 next_node->owner_next_index);
286 /* Swap target next frame with owner's. */
287 tmp = owner_next_frame[0];
288 owner_next_frame[0] = next_frame[0];
292 * If next_frame is already pending, we have to track down
293 * all pending frames and fix their next_frame_index fields.
295 if (next_frame->flags & VLIB_FRAME_PENDING)
297 vlib_pending_frame_t *p;
298 if (next_frame->frame != NULL)
300 vec_foreach (p, nm->pending_frames)
302 if (p->frame == next_frame->frame)
304 p->next_frame_index =
305 next_frame - vm->node_main.next_frames;
313 /* No previous owner. Take ownership. */
314 next_frame->flags |= VLIB_FRAME_OWNER;
317 /* Record new owner. */
318 next_node->owner_node_index = node->index;
319 next_node->owner_next_index = next_index;
321 /* Now we should be owner. */
322 ASSERT (next_frame->flags & VLIB_FRAME_OWNER);
325 /* Make sure that magic number is still there.
326 Otherwise, it is likely that caller has overrun frame arguments. */
328 validate_frame_magic (vlib_main_t * vm,
329 vlib_frame_t * f, vlib_node_t * n, uword next_index)
331 vlib_node_t *next_node = vlib_get_node (vm, n->next_nodes[next_index]);
332 u32 *magic = vlib_frame_find_magic (f, next_node);
333 ASSERT (VLIB_FRAME_MAGIC == magic[0]);
337 vlib_get_next_frame_internal (vlib_main_t * vm,
338 vlib_node_runtime_t * node,
339 u32 next_index, u32 allocate_new_next_frame)
342 vlib_next_frame_t *nf;
345 nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
347 /* Make sure this next frame owns right to enqueue to destination frame. */
348 if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_OWNER)))
349 vlib_next_frame_change_ownership (vm, node, next_index);
351 /* ??? Don't need valid flag: can use frame_index == ~0 */
352 if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_IS_ALLOCATED)))
354 nf->frame = vlib_frame_alloc (vm, node, next_index);
355 nf->flags |= VLIB_FRAME_IS_ALLOCATED;
360 /* Has frame been removed from pending vector (e.g. finished dispatching)?
361 If so we can reuse frame. */
362 if ((nf->flags & VLIB_FRAME_PENDING)
363 && !(f->frame_flags & VLIB_FRAME_PENDING))
365 nf->flags &= ~VLIB_FRAME_PENDING;
370 /* Allocate new frame if current one is marked as no-append or
371 it is already full. */
372 n_used = f->n_vectors;
373 if (n_used >= VLIB_FRAME_SIZE || (allocate_new_next_frame && n_used > 0) ||
374 (f->frame_flags & VLIB_FRAME_NO_APPEND))
376 /* Old frame may need to be freed after dispatch, since we'll have
377 two redundant frames from node -> next node. */
378 if (!(nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH))
380 vlib_frame_t *f_old = vlib_get_frame (vm, nf->frame);
381 f_old->frame_flags |= VLIB_FRAME_FREE_AFTER_DISPATCH;
384 /* Allocate new frame to replace full one. */
385 f = nf->frame = vlib_frame_alloc (vm, node, next_index);
386 n_used = f->n_vectors;
389 /* Should have free vectors in frame now. */
390 ASSERT (n_used < VLIB_FRAME_SIZE);
394 validate_frame_magic (vm, f,
395 vlib_get_node (vm, node->node_index), next_index);
402 vlib_put_next_frame_validate (vlib_main_t * vm,
403 vlib_node_runtime_t * rt,
404 u32 next_index, u32 n_vectors_left)
406 vlib_node_main_t *nm = &vm->node_main;
407 vlib_next_frame_t *nf;
409 vlib_node_runtime_t *next_rt;
410 vlib_node_t *next_node;
411 u32 n_before, n_after;
413 nf = vlib_node_runtime_get_next_frame (vm, rt, next_index);
414 f = vlib_get_frame (vm, nf->frame);
416 ASSERT (n_vectors_left <= VLIB_FRAME_SIZE);
418 vlib_validate_frame_indices (f);
420 n_after = VLIB_FRAME_SIZE - n_vectors_left;
421 n_before = f->n_vectors;
423 ASSERT (n_after >= n_before);
425 next_rt = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
426 nf->node_runtime_index);
427 next_node = vlib_get_node (vm, next_rt->node_index);
428 if (n_after > 0 && next_node->validate_frame)
430 u8 *msg = next_node->validate_frame (vm, rt, f);
433 clib_warning ("%v", msg);
441 vlib_put_next_frame (vlib_main_t * vm,
442 vlib_node_runtime_t * r,
443 u32 next_index, u32 n_vectors_left)
445 vlib_node_main_t *nm = &vm->node_main;
446 vlib_next_frame_t *nf;
448 u32 n_vectors_in_frame;
451 vlib_put_next_frame_validate (vm, r, next_index, n_vectors_left);
453 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
454 f = vlib_get_frame (vm, nf->frame);
456 /* Make sure that magic number is still there. Otherwise, caller
457 has overrun frame meta data. */
460 vlib_node_t *node = vlib_get_node (vm, r->node_index);
461 validate_frame_magic (vm, f, node, next_index);
464 /* Convert # of vectors left -> number of vectors there. */
465 ASSERT (n_vectors_left <= VLIB_FRAME_SIZE);
466 n_vectors_in_frame = VLIB_FRAME_SIZE - n_vectors_left;
468 f->n_vectors = n_vectors_in_frame;
470 /* If vectors were added to frame, add to pending vector. */
471 if (PREDICT_TRUE (n_vectors_in_frame > 0))
473 vlib_pending_frame_t *p;
476 r->cached_next_index = next_index;
478 if (!(f->frame_flags & VLIB_FRAME_PENDING))
480 __attribute__ ((unused)) vlib_node_t *node;
482 node = vlib_get_node (vm, r->node_index);
484 vec_add2 (nm->pending_frames, p, 1);
486 p->frame = nf->frame;
487 p->node_runtime_index = nf->node_runtime_index;
488 p->next_frame_index = nf - nm->next_frames;
489 nf->flags |= VLIB_FRAME_PENDING;
490 f->frame_flags |= VLIB_FRAME_PENDING;
493 /* Copy trace flag from next_frame and from runtime. */
495 (nf->flags & VLIB_NODE_FLAG_TRACE) | (r->
496 flags & VLIB_NODE_FLAG_TRACE);
498 v0 = nf->vectors_since_last_overflow;
499 v1 = v0 + n_vectors_in_frame;
500 nf->vectors_since_last_overflow = v1;
501 if (PREDICT_FALSE (v1 < v0))
503 vlib_node_t *node = vlib_get_node (vm, r->node_index);
504 vec_elt (node->n_vectors_by_next_node, next_index) += v0;
509 /* Sync up runtime (32 bit counters) and main node stats (64 bit counters). */
511 vlib_node_runtime_sync_stats_node (vlib_node_t *n, vlib_node_runtime_t *r,
512 uword n_calls, uword n_vectors,
515 n->stats_total.calls += n_calls + r->calls_since_last_overflow;
516 n->stats_total.vectors += n_vectors + r->vectors_since_last_overflow;
517 n->stats_total.clocks += n_clocks + r->clocks_since_last_overflow;
518 n->stats_total.max_clock = r->max_clock;
519 n->stats_total.max_clock_n = r->max_clock_n;
521 r->calls_since_last_overflow = 0;
522 r->vectors_since_last_overflow = 0;
523 r->clocks_since_last_overflow = 0;
527 vlib_node_runtime_sync_stats (vlib_main_t *vm, vlib_node_runtime_t *r,
528 uword n_calls, uword n_vectors, uword n_clocks)
530 vlib_node_t *n = vlib_get_node (vm, r->node_index);
531 vlib_node_runtime_sync_stats_node (n, r, n_calls, n_vectors, n_clocks);
534 always_inline void __attribute__ ((unused))
535 vlib_process_sync_stats (vlib_main_t * vm,
537 uword n_calls, uword n_vectors, uword n_clocks)
539 vlib_node_runtime_t *rt = &p->node_runtime;
540 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
541 vlib_node_runtime_sync_stats (vm, rt, n_calls, n_vectors, n_clocks);
542 n->stats_total.suspends += p->n_suspends;
547 vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n)
549 vlib_node_runtime_t *rt;
551 if (n->type == VLIB_NODE_TYPE_PROCESS)
553 /* Nothing to do for PROCESS nodes except in main thread */
554 if (vm != vlib_get_first_main ())
557 vlib_process_t *p = vlib_get_process_from_node (vm, n);
558 n->stats_total.suspends += p->n_suspends;
560 rt = &p->node_runtime;
564 vec_elt_at_index (vm->node_main.nodes_by_type[n->type],
567 vlib_node_runtime_sync_stats (vm, rt, 0, 0, 0);
569 /* Sync up runtime next frame vector counters with main node structure. */
571 vlib_next_frame_t *nf;
573 for (i = 0; i < rt->n_next_nodes; i++)
575 nf = vlib_node_runtime_get_next_frame (vm, rt, i);
576 vec_elt (n->n_vectors_by_next_node, i) +=
577 nf->vectors_since_last_overflow;
578 nf->vectors_since_last_overflow = 0;
584 vlib_node_runtime_update_stats (vlib_main_t * vm,
585 vlib_node_runtime_t * node,
587 uword n_vectors, uword n_clocks)
589 u32 ca0, ca1, v0, v1, cl0, cl1, r;
591 cl0 = cl1 = node->clocks_since_last_overflow;
592 ca0 = ca1 = node->calls_since_last_overflow;
593 v0 = v1 = node->vectors_since_last_overflow;
597 cl1 = cl0 + n_clocks;
599 node->calls_since_last_overflow = ca1;
600 node->clocks_since_last_overflow = cl1;
601 node->vectors_since_last_overflow = v1;
603 node->max_clock_n = node->max_clock > n_clocks ?
604 node->max_clock_n : n_vectors;
605 node->max_clock = node->max_clock > n_clocks ? node->max_clock : n_clocks;
607 r = vlib_node_runtime_update_main_loop_vector_stats (vm, node, n_vectors);
609 if (PREDICT_FALSE (ca1 < ca0 || v1 < v0 || cl1 < cl0))
611 node->calls_since_last_overflow = ca0;
612 node->clocks_since_last_overflow = cl0;
613 node->vectors_since_last_overflow = v0;
615 vlib_node_runtime_sync_stats (vm, node, n_calls, n_vectors, n_clocks);
622 vlib_process_update_stats (vlib_main_t * vm,
624 uword n_calls, uword n_vectors, uword n_clocks)
626 vlib_node_runtime_update_stats (vm, &p->node_runtime,
627 n_calls, n_vectors, n_clocks);
630 static clib_error_t *
631 vlib_cli_elog_clear (vlib_main_t * vm,
632 unformat_input_t * input, vlib_cli_command_t * cmd)
634 elog_reset_buffer (&vlib_global_main.elog_main);
639 VLIB_CLI_COMMAND (elog_clear_cli, static) = {
640 .path = "event-logger clear",
641 .short_help = "Clear the event log",
642 .function = vlib_cli_elog_clear,
647 static clib_error_t *
648 elog_save_buffer (vlib_main_t * vm,
649 unformat_input_t * input, vlib_cli_command_t * cmd)
651 elog_main_t *em = &vlib_global_main.elog_main;
652 char *file, *chroot_file;
653 clib_error_t *error = 0;
655 if (!unformat (input, "%s", &file))
657 vlib_cli_output (vm, "expected file name, got `%U'",
658 format_unformat_error, input);
662 /* It's fairly hard to get "../oopsie" through unformat; just in case */
663 if (strstr (file, "..") || index (file, '/'))
665 vlib_cli_output (vm, "illegal characters in filename '%s'", file);
669 chroot_file = (char *) format (0, "/tmp/%s%c", file, 0);
673 vlib_cli_output (vm, "Saving %wd of %wd events to %s",
674 elog_n_events_in_buffer (em),
675 elog_buffer_capacity (em), chroot_file);
677 vlib_worker_thread_barrier_sync (vm);
678 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
679 vlib_worker_thread_barrier_release (vm);
680 vec_free (chroot_file);
685 vlib_post_mortem_dump (void)
687 vlib_global_main_t *vgm = vlib_get_global_main ();
689 for (int i = 0; i < vec_len (vgm->post_mortem_callbacks); i++)
690 (vgm->post_mortem_callbacks[i]) ();
694 VLIB_CLI_COMMAND (elog_save_cli, static) = {
695 .path = "event-logger save",
696 .short_help = "event-logger save <filename> (saves log in /tmp/<filename>)",
697 .function = elog_save_buffer,
701 static clib_error_t *
702 elog_stop (vlib_main_t * vm,
703 unformat_input_t * input, vlib_cli_command_t * cmd)
705 elog_main_t *em = &vlib_global_main.elog_main;
707 em->n_total_events_disable_limit = em->n_total_events;
709 vlib_cli_output (vm, "Stopped the event logger...");
714 VLIB_CLI_COMMAND (elog_stop_cli, static) = {
715 .path = "event-logger stop",
716 .short_help = "Stop the event-logger",
717 .function = elog_stop,
721 static clib_error_t *
722 elog_restart (vlib_main_t * vm,
723 unformat_input_t * input, vlib_cli_command_t * cmd)
725 elog_main_t *em = &vlib_global_main.elog_main;
727 em->n_total_events_disable_limit = ~0;
729 vlib_cli_output (vm, "Restarted the event logger...");
734 VLIB_CLI_COMMAND (elog_restart_cli, static) = {
735 .path = "event-logger restart",
736 .short_help = "Restart the event-logger",
737 .function = elog_restart,
741 static clib_error_t *
742 elog_resize_command_fn (vlib_main_t * vm,
743 unformat_input_t * input, vlib_cli_command_t * cmd)
745 elog_main_t *em = &vlib_global_main.elog_main;
748 /* Stop the parade */
749 elog_reset_buffer (em);
751 if (unformat (input, "%d", &tmp))
753 elog_alloc (em, tmp);
754 em->n_total_events_disable_limit = ~0;
757 return clib_error_return (0, "Must specify how many events in the ring");
759 vlib_cli_output (vm, "Resized ring and restarted the event logger...");
764 VLIB_CLI_COMMAND (elog_resize_cli, static) = {
765 .path = "event-logger resize",
766 .short_help = "event-logger resize <nnn>",
767 .function = elog_resize_command_fn,
771 #endif /* CLIB_UNIX */
774 elog_show_buffer_internal (vlib_main_t * vm, u32 n_events_to_show)
776 elog_main_t *em = &vlib_global_main.elog_main;
777 elog_event_t *e, *es;
780 /* Show events in VLIB time since log clock starts after VLIB clock. */
781 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
782 * vm->clib_time.seconds_per_clock;
784 es = elog_peek_events (em);
785 vlib_cli_output (vm, "%d of %d events in buffer, logger %s", vec_len (es),
787 em->n_total_events < em->n_total_events_disable_limit ?
788 "running" : "stopped");
791 vlib_cli_output (vm, "%18.9f: %U",
792 e->time + dt, format_elog_event, em, e);
794 if (n_events_to_show == 0)
801 static clib_error_t *
802 elog_show_buffer (vlib_main_t * vm,
803 unformat_input_t * input, vlib_cli_command_t * cmd)
805 u32 n_events_to_show;
806 clib_error_t *error = 0;
808 n_events_to_show = 250;
809 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
811 if (unformat (input, "%d", &n_events_to_show))
813 else if (unformat (input, "all"))
814 n_events_to_show = ~0;
816 return unformat_parse_error (input);
818 elog_show_buffer_internal (vm, n_events_to_show);
823 VLIB_CLI_COMMAND (elog_show_cli, static) = {
824 .path = "show event-logger",
825 .short_help = "Show event logger info",
826 .function = elog_show_buffer,
831 vlib_gdb_show_event_log (void)
833 elog_show_buffer_internal (vlib_get_main (), (u32) ~ 0);
837 vlib_elog_main_loop_event (vlib_main_t * vm,
839 u64 time, u32 n_vectors, u32 is_return)
841 vlib_main_t *evm = vlib_get_first_main ();
842 elog_main_t *em = vlib_get_elog_main ();
843 int enabled = evm->elog_trace_graph_dispatch |
844 evm->elog_trace_graph_circuit;
846 if (PREDICT_FALSE (enabled && n_vectors))
848 if (PREDICT_FALSE (!elog_is_enabled (em)))
850 evm->elog_trace_graph_dispatch = 0;
851 evm->elog_trace_graph_circuit = 0;
855 (evm->elog_trace_graph_dispatch ||
856 (evm->elog_trace_graph_circuit &&
857 node_index == evm->elog_trace_graph_circuit_node_index)))
861 vec_elt_at_index (is_return
862 ? evm->node_return_elog_event_types
863 : evm->node_call_elog_event_types,
867 &vlib_worker_threads[vm->thread_index].elog_track
868 : &em->default_track),
869 /* data to log */ n_vectors);
875 add_trajectory_trace (vlib_buffer_t * b, u32 node_index)
877 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
878 if (PREDICT_FALSE (b->trajectory_nb >= VLIB_BUFFER_TRACE_TRAJECTORY_MAX))
880 b->trajectory_trace[b->trajectory_nb] = node_index;
885 static_always_inline u64
886 dispatch_node (vlib_main_t * vm,
887 vlib_node_runtime_t * node,
888 vlib_node_type_t type,
889 vlib_node_state_t dispatch_state,
890 vlib_frame_t * frame, u64 last_time_stamp)
894 vlib_node_main_t *nm = &vm->node_main;
895 vlib_next_frame_t *nf;
899 vlib_node_t *n = vlib_get_node (vm, node->node_index);
900 ASSERT (n->type == type);
903 /* Only non-internal nodes may be disabled. */
904 if (type != VLIB_NODE_TYPE_INTERNAL && node->state != dispatch_state)
906 ASSERT (type != VLIB_NODE_TYPE_INTERNAL);
907 return last_time_stamp;
910 if ((type == VLIB_NODE_TYPE_PRE_INPUT || type == VLIB_NODE_TYPE_INPUT)
911 && dispatch_state != VLIB_NODE_STATE_INTERRUPT)
913 u32 c = node->input_main_loops_per_call;
914 /* Only call node when count reaches zero. */
917 node->input_main_loops_per_call = c - 1;
918 return last_time_stamp;
922 /* Speculatively prefetch next frames. */
923 if (node->n_next_nodes > 0)
925 nf = vec_elt_at_index (nm->next_frames, node->next_frame_index);
926 CLIB_PREFETCH (nf, 4 * sizeof (nf[0]), WRITE);
929 vm->cpu_time_last_node_dispatch = last_time_stamp;
931 vlib_elog_main_loop_event (vm, node->node_index,
932 last_time_stamp, frame ? frame->n_vectors : 0,
935 vlib_node_runtime_perf_counter (vm, node, frame, 0, last_time_stamp,
936 VLIB_NODE_RUNTIME_PERF_BEFORE);
939 * Turn this on if you run into
940 * "bad monkey" contexts, and you want to know exactly
941 * which nodes they've visited... See ixge.c...
943 if (VLIB_BUFFER_TRACE_TRAJECTORY && frame)
947 from = vlib_frame_vector_args (frame);
948 for (i = 0; i < frame->n_vectors; i++)
950 vlib_buffer_t *b = vlib_get_buffer (vm, from[i]);
951 add_trajectory_trace (b, node->node_index);
953 if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0))
954 n = node->function (vm, node, frame);
956 n = vm->dispatch_wrapper_fn (vm, node, frame);
960 if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0))
961 n = node->function (vm, node, frame);
963 n = vm->dispatch_wrapper_fn (vm, node, frame);
966 t = clib_cpu_time_now ();
968 vlib_node_runtime_perf_counter (vm, node, frame, n, t,
969 VLIB_NODE_RUNTIME_PERF_AFTER);
971 vlib_elog_main_loop_event (vm, node->node_index, t, n, 1 /* is_after */ );
973 vm->main_loop_vectors_processed += n;
974 vm->main_loop_nodes_processed += n > 0;
976 v = vlib_node_runtime_update_stats (vm, node,
979 /* n_clocks */ t - last_time_stamp);
981 /* When in adaptive mode and vector rate crosses threshold switch to
982 polling mode and vice versa. */
983 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_ADAPTIVE_MODE))
986 ELOG_TYPE_DECLARE (e) =
988 .function = (char *) __FUNCTION__,
989 .format = "%s vector length %d, switching to %s",
990 .format_args = "T4i4t4",
993 "interrupt", "polling",
999 u32 node_name, vector_length, is_polling;
1002 if ((dispatch_state == VLIB_NODE_STATE_INTERRUPT
1003 && v >= nm->polling_threshold_vector_length) &&
1005 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1007 vlib_node_t *n = vlib_get_node (vm, node->node_index);
1008 n->state = VLIB_NODE_STATE_POLLING;
1009 node->state = VLIB_NODE_STATE_POLLING;
1011 ~VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
1012 node->flags |= VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
1013 nm->input_node_counts_by_state[VLIB_NODE_STATE_INTERRUPT] -= 1;
1014 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] += 1;
1017 vlib_get_first_main ()->elog_trace_graph_dispatch))
1019 vlib_worker_thread_t *w = vlib_worker_threads
1022 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
1024 ed->node_name = n->name_elog_string;
1025 ed->vector_length = v;
1029 else if (dispatch_state == VLIB_NODE_STATE_POLLING
1030 && v <= nm->interrupt_threshold_vector_length)
1032 vlib_node_t *n = vlib_get_node (vm, node->node_index);
1034 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE)
1036 /* Switch to interrupt mode after dispatch in polling one more time.
1037 This allows driver to re-enable interrupts. */
1038 n->state = VLIB_NODE_STATE_INTERRUPT;
1039 node->state = VLIB_NODE_STATE_INTERRUPT;
1041 ~VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
1042 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] -= 1;
1043 nm->input_node_counts_by_state[VLIB_NODE_STATE_INTERRUPT] += 1;
1048 vlib_worker_thread_t *w = vlib_worker_threads
1051 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
1053 vlib_get_first_main ()->elog_trace_graph_dispatch))
1055 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
1057 ed->node_name = n->name_elog_string;
1058 ed->vector_length = v;
1069 dispatch_pending_node (vlib_main_t * vm, uword pending_frame_index,
1070 u64 last_time_stamp)
1072 vlib_node_main_t *nm = &vm->node_main;
1074 vlib_next_frame_t *nf, nf_placeholder;
1075 vlib_node_runtime_t *n;
1076 vlib_frame_t *restore_frame;
1077 vlib_pending_frame_t *p;
1079 /* See comment below about dangling references to nm->pending_frames */
1080 p = nm->pending_frames + pending_frame_index;
1082 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1083 p->node_runtime_index);
1085 f = vlib_get_frame (vm, p->frame);
1086 if (p->next_frame_index == VLIB_PENDING_FRAME_NO_NEXT_FRAME)
1088 /* No next frame: so use placeholder on stack. */
1089 nf = &nf_placeholder;
1090 nf->flags = f->frame_flags & VLIB_NODE_FLAG_TRACE;
1094 nf = vec_elt_at_index (nm->next_frames, p->next_frame_index);
1096 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
1098 /* Force allocation of new frame while current frame is being
1100 restore_frame = NULL;
1101 if (nf->frame == p->frame)
1104 nf->flags &= ~VLIB_FRAME_IS_ALLOCATED;
1105 if (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH))
1106 restore_frame = p->frame;
1109 /* Frame must be pending. */
1110 ASSERT (f->frame_flags & VLIB_FRAME_PENDING);
1111 ASSERT (f->n_vectors > 0);
1113 /* Copy trace flag from next frame to node.
1114 Trace flag indicates that at least one vector in the dispatched
1116 n->flags &= ~VLIB_NODE_FLAG_TRACE;
1117 n->flags |= (nf->flags & VLIB_FRAME_TRACE) ? VLIB_NODE_FLAG_TRACE : 0;
1118 nf->flags &= ~VLIB_FRAME_TRACE;
1120 last_time_stamp = dispatch_node (vm, n,
1121 VLIB_NODE_TYPE_INTERNAL,
1122 VLIB_NODE_STATE_POLLING,
1123 f, last_time_stamp);
1124 /* Internal node vector-rate accounting, for summary stats */
1125 vm->internal_node_vectors += f->n_vectors;
1126 vm->internal_node_calls++;
1127 vm->internal_node_last_vectors_per_main_loop =
1128 (f->n_vectors > vm->internal_node_last_vectors_per_main_loop) ?
1129 f->n_vectors : vm->internal_node_last_vectors_per_main_loop;
1131 f->frame_flags &= ~(VLIB_FRAME_PENDING | VLIB_FRAME_NO_APPEND);
1133 /* Frame is ready to be used again, so restore it. */
1134 if (restore_frame != NULL)
1137 * We musn't restore a frame that is flagged to be freed. This
1138 * shouldn't happen since frames to be freed post dispatch are
1139 * those used when the to-node frame becomes full i.e. they form a
1140 * sort of queue of frames to a single node. If we get here then
1141 * the to-node frame and the pending frame *were* the same, and so
1142 * we removed the to-node frame. Therefore this frame is no
1143 * longer part of the queue for that node and hence it cannot be
1146 ASSERT (!(f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH));
1149 * NB: dispatching node n can result in the creation and scheduling
1150 * of new frames, and hence in the reallocation of nm->pending_frames.
1151 * Recompute p, or no supper. This was broken for more than 10 years.
1153 p = nm->pending_frames + pending_frame_index;
1156 * p->next_frame_index can change during node dispatch if node
1157 * function decides to change graph hook up.
1159 nf = vec_elt_at_index (nm->next_frames, p->next_frame_index);
1160 nf->flags |= VLIB_FRAME_IS_ALLOCATED;
1162 if (NULL == nf->frame)
1164 /* no new frame has been assigned to this node, use the saved one */
1165 nf->frame = restore_frame;
1171 /* The node has gained a frame, implying packets from the current frame
1172 were re-queued to this same node. we don't need the saved one
1174 vlib_frame_free (vm, n, f);
1179 if (f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH)
1181 ASSERT (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH));
1182 vlib_frame_free (vm, n, f);
1186 return last_time_stamp;
1190 vlib_process_stack_is_valid (vlib_process_t * p)
1192 return p->stack[0] == VLIB_PROCESS_STACK_MAGIC;
1198 vlib_process_t *process;
1199 vlib_frame_t *frame;
1200 } vlib_process_bootstrap_args_t;
1202 /* Called in process stack. */
1204 vlib_process_bootstrap (uword _a)
1206 vlib_process_bootstrap_args_t *a;
1208 vlib_node_runtime_t *node;
1213 a = uword_to_pointer (_a, vlib_process_bootstrap_args_t *);
1217 vlib_process_finish_switch_stack (vm);
1220 node = &p->node_runtime;
1222 n = node->function (vm, node, f);
1224 ASSERT (vlib_process_stack_is_valid (p));
1226 vlib_process_start_switch_stack (vm, 0);
1227 clib_longjmp (&p->return_longjmp, n);
1232 /* Called in main stack. */
1233 static_always_inline uword
1234 vlib_process_startup (vlib_main_t * vm, vlib_process_t * p, vlib_frame_t * f)
1236 vlib_process_bootstrap_args_t a;
1243 r = clib_setjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1244 if (r == VLIB_PROCESS_RETURN_LONGJMP_RETURN)
1246 vlib_process_start_switch_stack (vm, p);
1247 r = clib_calljmp (vlib_process_bootstrap, pointer_to_uword (&a),
1248 (void *) p->stack + (1 << p->log2_n_stack_bytes));
1251 vlib_process_finish_switch_stack (vm);
1256 static_always_inline uword
1257 vlib_process_resume (vlib_main_t * vm, vlib_process_t * p)
1260 p->flags &= ~(VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1261 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
1262 | VLIB_PROCESS_RESUME_PENDING);
1263 r = clib_setjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1264 if (r == VLIB_PROCESS_RETURN_LONGJMP_RETURN)
1266 vlib_process_start_switch_stack (vm, p);
1267 clib_longjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_RESUME);
1270 vlib_process_finish_switch_stack (vm);
1275 dispatch_process (vlib_main_t * vm,
1276 vlib_process_t * p, vlib_frame_t * f, u64 last_time_stamp)
1278 vlib_node_main_t *nm = &vm->node_main;
1279 vlib_node_runtime_t *node_runtime = &p->node_runtime;
1280 vlib_node_t *node = vlib_get_node (vm, node_runtime->node_index);
1281 u32 old_process_index;
1283 uword n_vectors, is_suspend;
1285 if (node->state != VLIB_NODE_STATE_POLLING
1286 || (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1287 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)))
1288 return last_time_stamp;
1290 p->flags |= VLIB_PROCESS_IS_RUNNING;
1292 t = last_time_stamp;
1293 vlib_elog_main_loop_event (vm, node_runtime->node_index, t,
1294 f ? f->n_vectors : 0, /* is_after */ 0);
1296 /* Save away current process for suspend. */
1297 old_process_index = nm->current_process_index;
1298 nm->current_process_index = node->runtime_index;
1300 vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp,
1301 VLIB_NODE_RUNTIME_PERF_BEFORE);
1303 n_vectors = vlib_process_startup (vm, p, f);
1305 nm->current_process_index = old_process_index;
1307 ASSERT (n_vectors != VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1308 is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND;
1311 vlib_pending_frame_t *pf;
1314 pool_get (nm->suspended_process_frames, pf);
1315 pf->node_runtime_index = node->runtime_index;
1317 pf->next_frame_index = ~0;
1320 p->suspended_process_frame_index = pf - nm->suspended_process_frames;
1322 if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
1324 TWT (tw_timer_wheel) * tw =
1325 (TWT (tw_timer_wheel) *) nm->timing_wheel;
1326 p->stop_timer_handle =
1327 TW (tw_timer_start) (tw,
1328 vlib_timing_wheel_data_set_suspended_process
1329 (node->runtime_index) /* [sic] pool idex */ ,
1331 p->resume_clock_interval);
1335 p->flags &= ~VLIB_PROCESS_IS_RUNNING;
1337 t = clib_cpu_time_now ();
1339 vlib_elog_main_loop_event (vm, node_runtime->node_index, t, is_suspend,
1342 vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t,
1343 VLIB_NODE_RUNTIME_PERF_AFTER);
1345 vlib_process_update_stats (vm, p,
1346 /* n_calls */ !is_suspend,
1347 /* n_vectors */ n_vectors,
1348 /* n_clocks */ t - last_time_stamp);
1354 vlib_start_process (vlib_main_t * vm, uword process_index)
1356 vlib_node_main_t *nm = &vm->node_main;
1357 vlib_process_t *p = vec_elt (nm->processes, process_index);
1358 dispatch_process (vm, p, /* frame */ 0, /* cpu_time_now */ 0);
1362 dispatch_suspended_process (vlib_main_t * vm,
1363 uword process_index, u64 last_time_stamp)
1365 vlib_node_main_t *nm = &vm->node_main;
1366 vlib_node_runtime_t *node_runtime;
1370 vlib_pending_frame_t *pf;
1371 u64 t, n_vectors, is_suspend;
1373 t = last_time_stamp;
1375 p = vec_elt (nm->processes, process_index);
1376 if (PREDICT_FALSE (!(p->flags & VLIB_PROCESS_IS_RUNNING)))
1377 return last_time_stamp;
1379 ASSERT (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1380 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT));
1382 pf = pool_elt_at_index (nm->suspended_process_frames,
1383 p->suspended_process_frame_index);
1385 node_runtime = &p->node_runtime;
1386 node = vlib_get_node (vm, node_runtime->node_index);
1389 vlib_elog_main_loop_event (vm, node_runtime->node_index, t,
1390 f ? f->n_vectors : 0, /* is_after */ 0);
1392 /* Save away current process for suspend. */
1393 nm->current_process_index = node->runtime_index;
1395 vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp,
1396 VLIB_NODE_RUNTIME_PERF_BEFORE);
1398 n_vectors = vlib_process_resume (vm, p);
1399 t = clib_cpu_time_now ();
1401 nm->current_process_index = ~0;
1403 is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND;
1406 /* Suspend it again. */
1409 if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
1411 p->stop_timer_handle =
1412 TW (tw_timer_start) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
1413 vlib_timing_wheel_data_set_suspended_process
1414 (node->runtime_index) /* [sic] pool idex */ ,
1416 p->resume_clock_interval);
1421 p->flags &= ~VLIB_PROCESS_IS_RUNNING;
1422 pool_put_index (nm->suspended_process_frames,
1423 p->suspended_process_frame_index);
1424 p->suspended_process_frame_index = ~0;
1427 t = clib_cpu_time_now ();
1428 vlib_elog_main_loop_event (vm, node_runtime->node_index, t, !is_suspend,
1431 vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t,
1432 VLIB_NODE_RUNTIME_PERF_AFTER);
1434 vlib_process_update_stats (vm, p,
1435 /* n_calls */ !is_suspend,
1436 /* n_vectors */ n_vectors,
1437 /* n_clocks */ t - last_time_stamp);
1442 void vl_api_send_pending_rpc_requests (vlib_main_t *) __attribute__ ((weak));
1444 vl_api_send_pending_rpc_requests (vlib_main_t * vm)
1448 static_always_inline void
1449 vlib_main_or_worker_loop (vlib_main_t * vm, int is_main)
1451 vlib_node_main_t *nm = &vm->node_main;
1452 vlib_thread_main_t *tm = vlib_get_thread_main ();
1456 vlib_frame_queue_main_t *fqm;
1457 u32 frame_queue_check_counter = 0;
1459 /* Initialize pending node vector. */
1462 vec_resize (nm->pending_frames, 32);
1463 vec_set_len (nm->pending_frames, 0);
1466 /* Mark time of main loop start. */
1469 cpu_time_now = vm->clib_time.last_cpu_time;
1470 vm->cpu_time_main_loop_start = cpu_time_now;
1473 cpu_time_now = clib_cpu_time_now ();
1475 /* Pre-allocate interupt runtime indices and lock. */
1476 vec_alloc_aligned (nm->pending_interrupts, 1, CLIB_CACHE_LINE_BYTES);
1478 /* Pre-allocate expired nodes. */
1479 if (!nm->polling_threshold_vector_length)
1480 nm->polling_threshold_vector_length = 10;
1481 if (!nm->interrupt_threshold_vector_length)
1482 nm->interrupt_threshold_vector_length = 5;
1484 vm->cpu_id = clib_get_current_cpu_id ();
1485 vm->numa_node = clib_get_current_numa_node ();
1486 os_set_numa_index (vm->numa_node);
1488 /* Start all processes. */
1494 * Perform an initial barrier sync. Pays no attention to
1495 * the barrier sync hold-down timer scheme, which won't work
1496 * at this point in time.
1498 vlib_worker_thread_initial_barrier_sync_and_release (vm);
1500 nm->current_process_index = ~0;
1501 for (i = 0; i < vec_len (nm->processes); i++)
1502 cpu_time_now = dispatch_process (vm, nm->processes[i], /* frame */ 0,
1508 vlib_node_runtime_t *n;
1510 if (PREDICT_FALSE (_vec_len (vm->pending_rpc_requests) > 0))
1513 vl_api_send_pending_rpc_requests (vm);
1517 vlib_worker_thread_barrier_check ();
1519 if (PREDICT_FALSE (vm->check_frame_queues + frame_queue_check_counter))
1522 vlib_frame_queue_dequeue_fn_t *fn;
1524 if (vm->check_frame_queues)
1526 frame_queue_check_counter = 100;
1527 vm->check_frame_queues = 0;
1530 vec_foreach (fqm, tm->frame_queue_mains)
1532 fn = fqm->frame_queue_dequeue_fn;
1533 processed += (fn) (vm, fqm);
1536 /* No handoff queue work found? */
1538 frame_queue_check_counter = 100;
1540 frame_queue_check_counter--;
1543 if (PREDICT_FALSE (vec_len (vm->worker_thread_main_loop_callbacks)))
1544 clib_call_callbacks (vm->worker_thread_main_loop_callbacks, vm,
1547 /* Process pre-input nodes. */
1548 cpu_time_now = clib_cpu_time_now ();
1549 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1550 cpu_time_now = dispatch_node (vm, n,
1551 VLIB_NODE_TYPE_PRE_INPUT,
1552 VLIB_NODE_STATE_POLLING,
1556 /* Next process input nodes. */
1557 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1558 cpu_time_now = dispatch_node (vm, n,
1559 VLIB_NODE_TYPE_INPUT,
1560 VLIB_NODE_STATE_POLLING,
1564 if (PREDICT_TRUE (is_main && vm->queue_signal_pending == 0))
1565 vm->queue_signal_callback (vm);
1567 if (__atomic_load_n (nm->pending_interrupts, __ATOMIC_ACQUIRE))
1570 *nm->pending_interrupts = 0;
1573 clib_interrupt_get_next (nm->interrupts, int_num)) != -1)
1575 vlib_node_runtime_t *n;
1576 clib_interrupt_clear (nm->interrupts, int_num);
1577 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1579 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1580 VLIB_NODE_STATE_INTERRUPT,
1581 /* frame */ 0, cpu_time_now);
1585 /* Input nodes may have added work to the pending vector.
1586 Process pending vector until there is nothing left.
1587 All pending vectors will be processed from input -> output. */
1588 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1589 cpu_time_now = dispatch_pending_node (vm, i, cpu_time_now);
1590 /* Reset pending vector for next iteration. */
1591 vec_set_len (nm->pending_frames, 0);
1596 ELOG_TYPE_DECLARE (es) =
1598 .format = "process tw start",
1601 ELOG_TYPE_DECLARE (ee) =
1603 .format = "process tw end: %d",
1604 .format_args = "i4",
1613 /* Check if process nodes have expired from timing wheel. */
1614 ASSERT (nm->data_from_advancing_timing_wheel != 0);
1616 if (PREDICT_FALSE (vm->elog_trace_graph_dispatch))
1617 ed = ELOG_DATA (&vlib_global_main.elog_main, es);
1619 nm->data_from_advancing_timing_wheel =
1620 TW (tw_timer_expire_timers_vec)
1621 ((TWT (tw_timer_wheel) *) nm->timing_wheel, vlib_time_now (vm),
1622 nm->data_from_advancing_timing_wheel);
1624 ASSERT (nm->data_from_advancing_timing_wheel != 0);
1626 if (PREDICT_FALSE (vm->elog_trace_graph_dispatch))
1628 ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
1630 _vec_len (nm->data_from_advancing_timing_wheel);
1634 (_vec_len (nm->data_from_advancing_timing_wheel) > 0))
1638 for (i = 0; i < _vec_len (nm->data_from_advancing_timing_wheel);
1641 u32 d = nm->data_from_advancing_timing_wheel[i];
1642 u32 di = vlib_timing_wheel_data_get_index (d);
1644 if (vlib_timing_wheel_data_is_timed_event (d))
1646 vlib_signal_timed_event_data_t *te =
1647 pool_elt_at_index (nm->signal_timed_event_data_pool,
1650 vlib_get_node (vm, te->process_node_index);
1652 vec_elt (nm->processes, n->runtime_index);
1655 vlib_process_signal_event_helper (nm, n, p,
1656 te->event_type_index,
1658 te->n_data_elt_bytes);
1659 if (te->n_data_bytes < sizeof (te->inline_event_data))
1660 clib_memcpy_fast (data, te->inline_event_data,
1664 clib_memcpy_fast (data, te->event_data_as_vector,
1666 vec_free (te->event_data_as_vector);
1668 pool_put (nm->signal_timed_event_data_pool, te);
1672 cpu_time_now = clib_cpu_time_now ();
1674 dispatch_suspended_process (vm, di, cpu_time_now);
1677 vec_set_len (nm->data_from_advancing_timing_wheel, 0);
1680 vlib_increment_main_loop_counter (vm);
1681 /* Record time stamp in case there are no enabled nodes and above
1682 calls do not update time stamp. */
1683 cpu_time_now = clib_cpu_time_now ();
1684 vm->loops_this_reporting_interval++;
1685 now = clib_time_now_internal (&vm->clib_time, cpu_time_now);
1686 /* Time to update loops_per_second? */
1687 if (PREDICT_FALSE (now >= vm->loop_interval_end))
1689 /* Next sample ends in 20ms */
1690 if (vm->loop_interval_start)
1692 f64 this_loops_per_second;
1694 this_loops_per_second =
1695 ((f64) vm->loops_this_reporting_interval) / (now -
1696 vm->loop_interval_start);
1698 vm->loops_per_second =
1699 vm->loops_per_second * vm->damping_constant +
1700 (1.0 - vm->damping_constant) * this_loops_per_second;
1701 if (vm->loops_per_second != 0.0)
1702 vm->seconds_per_loop = 1.0 / vm->loops_per_second;
1704 vm->seconds_per_loop = 0.0;
1706 /* New interval starts now, and ends in 20ms */
1707 vm->loop_interval_start = now;
1708 vm->loop_interval_end = now + 2e-4;
1709 vm->loops_this_reporting_interval = 0;
1715 vlib_main_loop (vlib_main_t * vm)
1717 vlib_main_or_worker_loop (vm, /* is_main */ 1);
1721 vlib_worker_loop (vlib_main_t * vm)
1723 vlib_main_or_worker_loop (vm, /* is_main */ 0);
1726 vlib_global_main_t vlib_global_main;
1729 vlib_add_del_post_mortem_callback (void *cb, int is_add)
1731 vlib_global_main_t *vgm = vlib_get_global_main ();
1736 for (i = vec_len (vgm->post_mortem_callbacks) - 1; i >= 0; i--)
1737 if (vgm->post_mortem_callbacks[i] == cb)
1738 vec_del1 (vgm->post_mortem_callbacks, i);
1742 for (i = 0; i < vec_len (vgm->post_mortem_callbacks); i++)
1743 if (vgm->post_mortem_callbacks[i] == cb)
1745 vec_add1 (vgm->post_mortem_callbacks, cb);
1749 elog_post_mortem_dump (void)
1751 elog_main_t *em = vlib_get_elog_main ();
1754 clib_error_t *error;
1756 filename = format (0, "/tmp/elog_post_mortem.%d%c", getpid (), 0);
1757 error = elog_write_file (em, (char *) filename, 1 /* flush ring */);
1759 clib_error_report (error);
1761 * We're in the middle of crashing. Don't try to free the filename.
1765 static clib_error_t *
1766 vlib_main_configure (vlib_main_t * vm, unformat_input_t * input)
1768 vlib_global_main_t *vgm = vlib_get_global_main ();
1769 int turn_on_mem_trace = 0;
1771 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1773 if (unformat (input, "memory-trace"))
1774 turn_on_mem_trace = 1;
1776 else if (unformat (input, "elog-events %d",
1777 &vgm->configured_elog_ring_size))
1778 vgm->configured_elog_ring_size =
1779 1 << max_log2 (vgm->configured_elog_ring_size);
1780 else if (unformat (input, "elog-post-mortem-dump"))
1781 vlib_add_del_post_mortem_callback (elog_post_mortem_dump,
1783 else if (unformat (input, "buffer-alloc-success-rate %f",
1784 &vm->buffer_alloc_success_rate))
1786 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0)
1787 return clib_error_return
1788 (0, "Buffer fault injection not configured");
1790 else if (unformat (input, "buffer-alloc-success-seed %u",
1791 &vm->buffer_alloc_success_seed))
1793 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0)
1794 return clib_error_return
1795 (0, "Buffer fault injection not configured");
1798 return unformat_parse_error (input);
1801 unformat_free (input);
1803 /* Enable memory trace as early as possible. */
1804 if (turn_on_mem_trace)
1810 VLIB_EARLY_CONFIG_FUNCTION (vlib_main_configure, "vlib");
1813 placeholder_queue_signal_callback (vlib_main_t * vm)
1817 #define foreach_weak_reference_stub \
1819 _(vlibmemory_init) \
1820 _(map_api_segment_init)
1823 clib_error_t *name (vlib_main_t *vm) __attribute__((weak)); \
1824 clib_error_t *name (vlib_main_t *vm) { return 0; }
1825 foreach_weak_reference_stub;
1828 void vl_api_set_elog_main (elog_main_t * m) __attribute__ ((weak));
1830 vl_api_set_elog_main (elog_main_t * m)
1832 clib_warning ("STUB");
1835 int vl_api_set_elog_trace_api_messages (int enable) __attribute__ ((weak));
1837 vl_api_set_elog_trace_api_messages (int enable)
1839 clib_warning ("STUB");
1843 int vl_api_get_elog_trace_api_messages (void) __attribute__ ((weak));
1845 vl_api_get_elog_trace_api_messages (void)
1847 clib_warning ("STUB");
1851 /* Main function. */
1853 vlib_main (vlib_main_t * volatile vm, unformat_input_t * input)
1855 vlib_global_main_t *vgm = vlib_get_global_main ();
1856 clib_error_t *volatile error;
1857 vlib_node_main_t *nm = &vm->node_main;
1859 vm->queue_signal_callback = placeholder_queue_signal_callback;
1861 /* Reconfigure event log which is enabled very early */
1862 if (vgm->configured_elog_ring_size &&
1863 vgm->configured_elog_ring_size != vgm->elog_main.event_ring_size)
1864 elog_resize (&vgm->elog_main, vgm->configured_elog_ring_size);
1865 vl_api_set_elog_main (vlib_get_elog_main ());
1866 (void) vl_api_set_elog_trace_api_messages (1);
1872 if ((error = vlib_physmem_init (vm)))
1874 clib_error_report (error);
1878 if ((error = vlib_log_init (vm)))
1880 clib_error_report (error);
1884 if ((error = vlib_stats_init (vm)))
1886 clib_error_report (error);
1890 if ((error = vlib_buffer_main_init (vm)))
1892 clib_error_report (error);
1896 if ((error = vlib_thread_init (vm)))
1898 clib_error_report (error);
1902 /* Register node ifunction variants */
1903 vlib_register_all_node_march_variants (vm);
1905 /* Register static nodes so that init functions may use them. */
1906 vlib_register_all_static_nodes (vm);
1908 /* Set seed for random number generator.
1909 Allow user to specify seed to make random sequence deterministic. */
1910 if (!unformat (input, "seed %wd", &vm->random_seed))
1911 vm->random_seed = clib_cpu_time_now ();
1912 clib_random_buffer_init (&vm->random_buffer, vm->random_seed);
1914 /* Initialize node graph. */
1915 if ((error = vlib_node_main_init (vm)))
1917 /* Arrange for graph hook up error to not be fatal when debugging. */
1919 clib_error_report (error);
1924 /* Direct call / weak reference, for vlib standalone use-cases */
1925 if ((error = vpe_api_init (vm)))
1927 clib_error_report (error);
1931 if ((error = vlibmemory_init (vm)))
1933 clib_error_report (error);
1937 if ((error = map_api_segment_init (vm)))
1939 clib_error_report (error);
1943 /* See unix/main.c; most likely already set up */
1944 if (vgm->init_functions_called == 0)
1945 vgm->init_functions_called = hash_create (0, /* value bytes */ 0);
1946 if ((error = vlib_call_all_init_functions (vm)))
1949 nm->timing_wheel = clib_mem_alloc_aligned (sizeof (TWT (tw_timer_wheel)),
1950 CLIB_CACHE_LINE_BYTES);
1952 vec_validate (nm->data_from_advancing_timing_wheel, 10);
1953 vec_set_len (nm->data_from_advancing_timing_wheel, 0);
1955 /* Create the process timing wheel */
1956 TW (tw_timer_wheel_init) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
1957 0 /* no callback */ ,
1958 10e-6 /* timer period 10us */ ,
1959 ~0 /* max expirations per call */ );
1961 vec_validate (vm->pending_rpc_requests, 0);
1962 vec_set_len (vm->pending_rpc_requests, 0);
1963 vec_validate (vm->processing_rpc_requests, 0);
1964 vec_set_len (vm->processing_rpc_requests, 0);
1966 /* Default params for the buffer allocator fault injector, if configured */
1967 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
1969 vm->buffer_alloc_success_seed = 0xdeaddabe;
1970 vm->buffer_alloc_success_rate = 0.80;
1973 if ((error = vlib_call_all_config_functions (vm, input, 0 /* is_early */ )))
1977 * Use exponential smoothing, with a half-life of 1 second
1978 * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K)
1980 * Sample every 20ms, aka 50 samples per second
1981 * K = exp (-1.0/20.0);
1984 vm->damping_constant = exp (-1.0 / 20.0);
1986 /* Sort per-thread init functions before we start threads */
1987 vlib_sort_init_exit_functions (&vgm->worker_init_function_registrations);
1989 /* Call all main loop enter functions. */
1991 clib_error_t *sub_error;
1992 sub_error = vlib_call_all_main_loop_enter_functions (vm);
1994 clib_error_report (sub_error);
1997 switch (clib_setjmp (&vm->main_loop_exit, VLIB_MAIN_LOOP_EXIT_NONE))
1999 case VLIB_MAIN_LOOP_EXIT_NONE:
2000 vm->main_loop_exit_set = 1;
2003 case VLIB_MAIN_LOOP_EXIT_CLI:
2007 error = vm->main_loop_error;
2011 vlib_main_loop (vm);
2014 vlib_worker_thread_barrier_sync (vm);
2015 /* Call all exit functions. */
2017 clib_error_t *sub_error;
2018 sub_error = vlib_call_all_main_loop_exit_functions (vm);
2020 clib_error_report (sub_error);
2022 vlib_worker_thread_barrier_release (vm);
2025 clib_error_report (error);
2027 return vm->main_loop_exit_status;
2031 vlib_get_main_not_inline (void)
2033 return vlib_get_main ();
2037 vlib_get_elog_main_not_inline ()
2039 return &vlib_global_main.elog_main;
2043 vlib_exit_with_status (vlib_main_t *vm, int status)
2045 vm->main_loop_exit_status = status;
2046 __atomic_store_n (&vm->main_loop_exit_now, 1, __ATOMIC_RELEASE);
2050 * fd.io coding-style-patch-verification: ON
2053 * eval: (c-set-style "gnu")