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 <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
46 #include <vlib/unix/unix.h>
48 /* Actually allocate a few extra slots of vector data to support
49 speculative vector enqueues which overflow vector data in next frame. */
50 #define VLIB_FRAME_SIZE_ALLOC (VLIB_FRAME_SIZE + 4)
53 vlib_frame_bytes (u32 n_scalar_bytes, u32 n_vector_bytes)
57 /* Make room for vlib_frame_t plus scalar arguments. */
58 n_bytes = vlib_frame_vector_byte_offset (n_scalar_bytes);
60 /* Make room for vector arguments.
61 Allocate a few extra slots of vector data to support
62 speculative vector enqueues which overflow vector data in next frame. */
63 #define VLIB_FRAME_SIZE_EXTRA 4
64 n_bytes += (VLIB_FRAME_SIZE + VLIB_FRAME_SIZE_EXTRA) * n_vector_bytes;
66 /* Magic number is first 32bit number after vector data.
67 Used to make sure that vector data is never overrun. */
68 #define VLIB_FRAME_MAGIC (0xabadc0ed)
69 n_bytes += sizeof (u32);
71 /* Pad to cache line. */
72 n_bytes = round_pow2 (n_bytes, CLIB_CACHE_LINE_BYTES);
78 vlib_frame_find_magic (vlib_frame_t * f, vlib_node_t * node)
82 p += vlib_frame_vector_byte_offset (node->scalar_size);
84 p += (VLIB_FRAME_SIZE + VLIB_FRAME_SIZE_EXTRA) * node->vector_size;
89 static inline vlib_frame_size_t *
90 get_frame_size_info (vlib_node_main_t * nm,
91 u32 n_scalar_bytes, u32 n_vector_bytes)
93 #ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
94 uword key = (n_scalar_bytes << 16) | n_vector_bytes;
97 p = hash_get (nm->frame_size_hash, key);
102 i = vec_len (nm->frame_sizes);
103 vec_validate (nm->frame_sizes, i);
104 hash_set (nm->frame_size_hash, key, i);
107 return vec_elt_at_index (nm->frame_sizes, i);
109 ASSERT (vlib_frame_bytes (n_scalar_bytes, n_vector_bytes)
110 == (vlib_frame_bytes (0, 4)));
111 return vec_elt_at_index (nm->frame_sizes, 0);
115 static vlib_frame_t *
116 vlib_frame_alloc_to_node (vlib_main_t * vm, u32 to_node_index,
119 vlib_node_main_t *nm = &vm->node_main;
120 vlib_frame_size_t *fs;
121 vlib_node_t *to_node;
123 u32 l, n, scalar_size, vector_size;
125 ASSERT (vm == vlib_get_main ());
127 to_node = vlib_get_node (vm, to_node_index);
129 scalar_size = to_node->scalar_size;
130 vector_size = to_node->vector_size;
132 fs = get_frame_size_info (nm, scalar_size, vector_size);
133 n = vlib_frame_bytes (scalar_size, vector_size);
134 if ((l = vec_len (fs->free_frames)) > 0)
136 /* Allocate from end of free list. */
137 f = fs->free_frames[l - 1];
138 _vec_len (fs->free_frames) = l - 1;
142 f = clib_mem_alloc_aligned_no_fail (n, VLIB_FRAME_ALIGN);
145 /* Poison frame when debugging. */
147 clib_memset (f, 0xfe, n);
149 /* Insert magic number. */
153 magic = vlib_frame_find_magic (f, to_node);
154 *magic = VLIB_FRAME_MAGIC;
157 f->frame_flags = VLIB_FRAME_IS_ALLOCATED | frame_flags;
159 f->scalar_size = scalar_size;
160 f->vector_size = vector_size;
163 fs->n_alloc_frames += 1;
168 /* Allocate a frame for from FROM_NODE to TO_NODE via TO_NEXT_INDEX.
169 Returns frame index. */
170 static vlib_frame_t *
171 vlib_frame_alloc (vlib_main_t * vm, vlib_node_runtime_t * from_node_runtime,
174 vlib_node_t *from_node;
176 from_node = vlib_get_node (vm, from_node_runtime->node_index);
177 ASSERT (to_next_index < vec_len (from_node->next_nodes));
179 return vlib_frame_alloc_to_node (vm, from_node->next_nodes[to_next_index],
180 /* frame_flags */ 0);
184 vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index)
186 vlib_frame_t *f = vlib_frame_alloc_to_node (vm, to_node_index,
188 VLIB_FRAME_FREE_AFTER_DISPATCH);
189 return vlib_get_frame (vm, f);
193 vlib_validate_frame_indices (vlib_frame_t * f)
198 u32 *from = vlib_frame_vector_args (f);
200 /* Check for bad buffer index values */
201 for (i = 0; i < f->n_vectors; i++)
205 clib_warning ("BUG: buffer index 0 at index %d", i);
208 else if (from[i] == 0xfefefefe)
210 clib_warning ("BUG: frame poison pattern at index %d", i);
218 vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index, vlib_frame_t * f)
220 vlib_pending_frame_t *p;
221 vlib_node_t *to_node;
223 if (f->n_vectors == 0)
226 ASSERT (vm == vlib_get_main ());
228 vlib_validate_frame_indices (f);
230 to_node = vlib_get_node (vm, to_node_index);
232 vec_add2 (vm->node_main.pending_frames, p, 1);
234 f->frame_flags |= VLIB_FRAME_PENDING;
235 p->frame = vlib_get_frame (vm, f);
236 p->node_runtime_index = to_node->runtime_index;
237 p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME;
240 /* Free given frame. */
242 vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f)
244 vlib_node_main_t *nm = &vm->node_main;
246 vlib_frame_size_t *fs;
248 ASSERT (vm == vlib_get_main ());
249 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
251 node = vlib_get_node (vm, r->node_index);
252 fs = get_frame_size_info (nm, node->scalar_size, node->vector_size);
254 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
256 /* No next frames may point to freed frame. */
259 vlib_next_frame_t *nf;
260 vec_foreach (nf, vm->node_main.next_frames) ASSERT (nf->frame != f);
263 f->frame_flags &= ~(VLIB_FRAME_IS_ALLOCATED | VLIB_FRAME_NO_APPEND);
265 vec_add1 (fs->free_frames, f);
266 ASSERT (fs->n_alloc_frames > 0);
267 fs->n_alloc_frames -= 1;
270 static clib_error_t *
271 show_frame_stats (vlib_main_t * vm,
272 unformat_input_t * input, vlib_cli_command_t * cmd)
274 vlib_node_main_t *nm = &vm->node_main;
275 vlib_frame_size_t *fs;
277 vlib_cli_output (vm, "%=6s%=12s%=12s", "Size", "# Alloc", "# Free");
278 vec_foreach (fs, nm->frame_sizes)
280 u32 n_alloc = fs->n_alloc_frames;
281 u32 n_free = vec_len (fs->free_frames);
283 if (n_alloc + n_free > 0)
284 vlib_cli_output (vm, "%=6d%=12d%=12d",
285 fs - nm->frame_sizes, n_alloc, n_free);
292 VLIB_CLI_COMMAND (show_frame_stats_cli, static) = {
293 .path = "show vlib frame-allocation",
294 .short_help = "Show node dispatch frame statistics",
295 .function = show_frame_stats,
299 /* Change ownership of enqueue rights to given next node. */
301 vlib_next_frame_change_ownership (vlib_main_t * vm,
302 vlib_node_runtime_t * node_runtime,
305 vlib_node_main_t *nm = &vm->node_main;
306 vlib_next_frame_t *next_frame;
307 vlib_node_t *node, *next_node;
309 node = vec_elt (nm->nodes, node_runtime->node_index);
311 /* Only internal & input nodes are allowed to call other nodes. */
312 ASSERT (node->type == VLIB_NODE_TYPE_INTERNAL
313 || node->type == VLIB_NODE_TYPE_INPUT
314 || node->type == VLIB_NODE_TYPE_PROCESS);
316 ASSERT (vec_len (node->next_nodes) == node_runtime->n_next_nodes);
319 vlib_node_runtime_get_next_frame (vm, node_runtime, next_index);
320 next_node = vec_elt (nm->nodes, node->next_nodes[next_index]);
322 if (next_node->owner_node_index != VLIB_INVALID_NODE_INDEX)
324 /* Get frame from previous owner. */
325 vlib_next_frame_t *owner_next_frame;
326 vlib_next_frame_t tmp;
329 vlib_node_get_next_frame (vm,
330 next_node->owner_node_index,
331 next_node->owner_next_index);
333 /* Swap target next frame with owner's. */
334 tmp = owner_next_frame[0];
335 owner_next_frame[0] = next_frame[0];
339 * If next_frame is already pending, we have to track down
340 * all pending frames and fix their next_frame_index fields.
342 if (next_frame->flags & VLIB_FRAME_PENDING)
344 vlib_pending_frame_t *p;
345 if (next_frame->frame != NULL)
347 vec_foreach (p, nm->pending_frames)
349 if (p->frame == next_frame->frame)
351 p->next_frame_index =
352 next_frame - vm->node_main.next_frames;
360 /* No previous owner. Take ownership. */
361 next_frame->flags |= VLIB_FRAME_OWNER;
364 /* Record new owner. */
365 next_node->owner_node_index = node->index;
366 next_node->owner_next_index = next_index;
368 /* Now we should be owner. */
369 ASSERT (next_frame->flags & VLIB_FRAME_OWNER);
372 /* Make sure that magic number is still there.
373 Otherwise, it is likely that caller has overrun frame arguments. */
375 validate_frame_magic (vlib_main_t * vm,
376 vlib_frame_t * f, vlib_node_t * n, uword next_index)
378 vlib_node_t *next_node = vlib_get_node (vm, n->next_nodes[next_index]);
379 u32 *magic = vlib_frame_find_magic (f, next_node);
380 ASSERT (VLIB_FRAME_MAGIC == magic[0]);
384 vlib_get_next_frame_internal (vlib_main_t * vm,
385 vlib_node_runtime_t * node,
386 u32 next_index, u32 allocate_new_next_frame)
389 vlib_next_frame_t *nf;
392 nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
394 /* Make sure this next frame owns right to enqueue to destination frame. */
395 if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_OWNER)))
396 vlib_next_frame_change_ownership (vm, node, next_index);
398 /* ??? Don't need valid flag: can use frame_index == ~0 */
399 if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_IS_ALLOCATED)))
401 nf->frame = vlib_frame_alloc (vm, node, next_index);
402 nf->flags |= VLIB_FRAME_IS_ALLOCATED;
407 /* Has frame been removed from pending vector (e.g. finished dispatching)?
408 If so we can reuse frame. */
409 if ((nf->flags & VLIB_FRAME_PENDING)
410 && !(f->frame_flags & VLIB_FRAME_PENDING))
412 nf->flags &= ~VLIB_FRAME_PENDING;
417 /* Allocate new frame if current one is marked as no-append or
418 it is already full. */
419 n_used = f->n_vectors;
420 if (n_used >= VLIB_FRAME_SIZE || (allocate_new_next_frame && n_used > 0) ||
421 (f->frame_flags & VLIB_FRAME_NO_APPEND))
423 /* Old frame may need to be freed after dispatch, since we'll have
424 two redundant frames from node -> next node. */
425 if (!(nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH))
427 vlib_frame_t *f_old = vlib_get_frame (vm, nf->frame);
428 f_old->frame_flags |= VLIB_FRAME_FREE_AFTER_DISPATCH;
431 /* Allocate new frame to replace full one. */
432 f = nf->frame = vlib_frame_alloc (vm, node, next_index);
433 n_used = f->n_vectors;
436 /* Should have free vectors in frame now. */
437 ASSERT (n_used < VLIB_FRAME_SIZE);
441 validate_frame_magic (vm, f,
442 vlib_get_node (vm, node->node_index), next_index);
449 vlib_put_next_frame_validate (vlib_main_t * vm,
450 vlib_node_runtime_t * rt,
451 u32 next_index, u32 n_vectors_left)
453 vlib_node_main_t *nm = &vm->node_main;
454 vlib_next_frame_t *nf;
456 vlib_node_runtime_t *next_rt;
457 vlib_node_t *next_node;
458 u32 n_before, n_after;
460 nf = vlib_node_runtime_get_next_frame (vm, rt, next_index);
461 f = vlib_get_frame (vm, nf->frame);
463 ASSERT (n_vectors_left <= VLIB_FRAME_SIZE);
465 vlib_validate_frame_indices (f);
467 n_after = VLIB_FRAME_SIZE - n_vectors_left;
468 n_before = f->n_vectors;
470 ASSERT (n_after >= n_before);
472 next_rt = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
473 nf->node_runtime_index);
474 next_node = vlib_get_node (vm, next_rt->node_index);
475 if (n_after > 0 && next_node->validate_frame)
477 u8 *msg = next_node->validate_frame (vm, rt, f);
480 clib_warning ("%v", msg);
488 vlib_put_next_frame (vlib_main_t * vm,
489 vlib_node_runtime_t * r,
490 u32 next_index, u32 n_vectors_left)
492 vlib_node_main_t *nm = &vm->node_main;
493 vlib_next_frame_t *nf;
495 u32 n_vectors_in_frame;
498 vlib_put_next_frame_validate (vm, r, next_index, n_vectors_left);
500 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
501 f = vlib_get_frame (vm, nf->frame);
503 /* Make sure that magic number is still there. Otherwise, caller
504 has overrun frame meta data. */
507 vlib_node_t *node = vlib_get_node (vm, r->node_index);
508 validate_frame_magic (vm, f, node, next_index);
511 /* Convert # of vectors left -> number of vectors there. */
512 ASSERT (n_vectors_left <= VLIB_FRAME_SIZE);
513 n_vectors_in_frame = VLIB_FRAME_SIZE - n_vectors_left;
515 f->n_vectors = n_vectors_in_frame;
517 /* If vectors were added to frame, add to pending vector. */
518 if (PREDICT_TRUE (n_vectors_in_frame > 0))
520 vlib_pending_frame_t *p;
523 r->cached_next_index = next_index;
525 if (!(f->frame_flags & VLIB_FRAME_PENDING))
527 __attribute__ ((unused)) vlib_node_t *node;
528 vlib_node_t *next_node;
529 vlib_node_runtime_t *next_runtime;
531 node = vlib_get_node (vm, r->node_index);
532 next_node = vlib_get_next_node (vm, r->node_index, next_index);
533 next_runtime = vlib_node_get_runtime (vm, next_node->index);
535 vec_add2 (nm->pending_frames, p, 1);
537 p->frame = nf->frame;
538 p->node_runtime_index = nf->node_runtime_index;
539 p->next_frame_index = nf - nm->next_frames;
540 nf->flags |= VLIB_FRAME_PENDING;
541 f->frame_flags |= VLIB_FRAME_PENDING;
544 * If we're going to dispatch this frame on another thread,
545 * force allocation of a new frame. Otherwise, we create
546 * a dangling frame reference. Each thread has its own copy of
547 * the next_frames vector.
549 if (0 && r->thread_index != next_runtime->thread_index)
552 nf->flags &= ~(VLIB_FRAME_PENDING | VLIB_FRAME_IS_ALLOCATED);
556 /* Copy trace flag from next_frame and from runtime. */
558 (nf->flags & VLIB_NODE_FLAG_TRACE) | (r->
559 flags & VLIB_NODE_FLAG_TRACE);
561 v0 = nf->vectors_since_last_overflow;
562 v1 = v0 + n_vectors_in_frame;
563 nf->vectors_since_last_overflow = v1;
564 if (PREDICT_FALSE (v1 < v0))
566 vlib_node_t *node = vlib_get_node (vm, r->node_index);
567 vec_elt (node->n_vectors_by_next_node, next_index) += v0;
572 /* Sync up runtime (32 bit counters) and main node stats (64 bit counters). */
574 vlib_node_runtime_sync_stats (vlib_main_t * vm,
575 vlib_node_runtime_t * r,
576 uword n_calls, uword n_vectors, uword n_clocks)
578 vlib_node_t *n = vlib_get_node (vm, r->node_index);
580 n->stats_total.calls += n_calls + r->calls_since_last_overflow;
581 n->stats_total.vectors += n_vectors + r->vectors_since_last_overflow;
582 n->stats_total.clocks += n_clocks + r->clocks_since_last_overflow;
583 n->stats_total.max_clock = r->max_clock;
584 n->stats_total.max_clock_n = r->max_clock_n;
586 r->calls_since_last_overflow = 0;
587 r->vectors_since_last_overflow = 0;
588 r->clocks_since_last_overflow = 0;
591 always_inline void __attribute__ ((unused))
592 vlib_process_sync_stats (vlib_main_t * vm,
594 uword n_calls, uword n_vectors, uword n_clocks)
596 vlib_node_runtime_t *rt = &p->node_runtime;
597 vlib_node_t *n = vlib_get_node (vm, rt->node_index);
598 vlib_node_runtime_sync_stats (vm, rt, n_calls, n_vectors, n_clocks);
599 n->stats_total.suspends += p->n_suspends;
604 vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n)
606 vlib_node_runtime_t *rt;
608 if (n->type == VLIB_NODE_TYPE_PROCESS)
610 /* Nothing to do for PROCESS nodes except in main thread */
611 if (vm != &vlib_global_main)
614 vlib_process_t *p = vlib_get_process_from_node (vm, n);
615 n->stats_total.suspends += p->n_suspends;
617 rt = &p->node_runtime;
621 vec_elt_at_index (vm->node_main.nodes_by_type[n->type],
624 vlib_node_runtime_sync_stats (vm, rt, 0, 0, 0);
626 /* Sync up runtime next frame vector counters with main node structure. */
628 vlib_next_frame_t *nf;
630 for (i = 0; i < rt->n_next_nodes; i++)
632 nf = vlib_node_runtime_get_next_frame (vm, rt, i);
633 vec_elt (n->n_vectors_by_next_node, i) +=
634 nf->vectors_since_last_overflow;
635 nf->vectors_since_last_overflow = 0;
641 vlib_node_runtime_update_stats (vlib_main_t * vm,
642 vlib_node_runtime_t * node,
644 uword n_vectors, uword n_clocks)
646 u32 ca0, ca1, v0, v1, cl0, cl1, r;
648 cl0 = cl1 = node->clocks_since_last_overflow;
649 ca0 = ca1 = node->calls_since_last_overflow;
650 v0 = v1 = node->vectors_since_last_overflow;
654 cl1 = cl0 + n_clocks;
656 node->calls_since_last_overflow = ca1;
657 node->clocks_since_last_overflow = cl1;
658 node->vectors_since_last_overflow = v1;
660 node->max_clock_n = node->max_clock > n_clocks ?
661 node->max_clock_n : n_vectors;
662 node->max_clock = node->max_clock > n_clocks ? node->max_clock : n_clocks;
664 r = vlib_node_runtime_update_main_loop_vector_stats (vm, node, n_vectors);
666 if (PREDICT_FALSE (ca1 < ca0 || v1 < v0 || cl1 < cl0))
668 node->calls_since_last_overflow = ca0;
669 node->clocks_since_last_overflow = cl0;
670 node->vectors_since_last_overflow = v0;
672 vlib_node_runtime_sync_stats (vm, node, n_calls, n_vectors, n_clocks);
679 vlib_process_update_stats (vlib_main_t * vm,
681 uword n_calls, uword n_vectors, uword n_clocks)
683 vlib_node_runtime_update_stats (vm, &p->node_runtime,
684 n_calls, n_vectors, n_clocks);
687 static clib_error_t *
688 vlib_cli_elog_clear (vlib_main_t * vm,
689 unformat_input_t * input, vlib_cli_command_t * cmd)
691 elog_reset_buffer (&vm->elog_main);
696 VLIB_CLI_COMMAND (elog_clear_cli, static) = {
697 .path = "event-logger clear",
698 .short_help = "Clear the event log",
699 .function = vlib_cli_elog_clear,
704 static clib_error_t *
705 elog_save_buffer (vlib_main_t * vm,
706 unformat_input_t * input, vlib_cli_command_t * cmd)
708 elog_main_t *em = &vm->elog_main;
709 char *file, *chroot_file;
710 clib_error_t *error = 0;
712 if (!unformat (input, "%s", &file))
714 vlib_cli_output (vm, "expected file name, got `%U'",
715 format_unformat_error, input);
719 /* It's fairly hard to get "../oopsie" through unformat; just in case */
720 if (strstr (file, "..") || index (file, '/'))
722 vlib_cli_output (vm, "illegal characters in filename '%s'", file);
726 chroot_file = (char *) format (0, "/tmp/%s%c", file, 0);
730 vlib_cli_output (vm, "Saving %wd of %wd events to %s",
731 elog_n_events_in_buffer (em),
732 elog_buffer_capacity (em), chroot_file);
734 vlib_worker_thread_barrier_sync (vm);
735 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
736 vlib_worker_thread_barrier_release (vm);
737 vec_free (chroot_file);
742 vlib_post_mortem_dump (void)
744 vlib_main_t *vm = &vlib_global_main;
746 for (int i = 0; i < vec_len (vm->post_mortem_callbacks); i++)
747 (vm->post_mortem_callbacks[i]) ();
751 VLIB_CLI_COMMAND (elog_save_cli, static) = {
752 .path = "event-logger save",
753 .short_help = "event-logger save <filename> (saves log in /tmp/<filename>)",
754 .function = elog_save_buffer,
758 static clib_error_t *
759 elog_stop (vlib_main_t * vm,
760 unformat_input_t * input, vlib_cli_command_t * cmd)
762 elog_main_t *em = &vm->elog_main;
764 em->n_total_events_disable_limit = em->n_total_events;
766 vlib_cli_output (vm, "Stopped the event logger...");
771 VLIB_CLI_COMMAND (elog_stop_cli, static) = {
772 .path = "event-logger stop",
773 .short_help = "Stop the event-logger",
774 .function = elog_stop,
778 static clib_error_t *
779 elog_restart (vlib_main_t * vm,
780 unformat_input_t * input, vlib_cli_command_t * cmd)
782 elog_main_t *em = &vm->elog_main;
784 em->n_total_events_disable_limit = ~0;
786 vlib_cli_output (vm, "Restarted the event logger...");
791 VLIB_CLI_COMMAND (elog_restart_cli, static) = {
792 .path = "event-logger restart",
793 .short_help = "Restart the event-logger",
794 .function = elog_restart,
798 static clib_error_t *
799 elog_resize_command_fn (vlib_main_t * vm,
800 unformat_input_t * input, vlib_cli_command_t * cmd)
802 elog_main_t *em = &vm->elog_main;
805 /* Stop the parade */
806 elog_reset_buffer (&vm->elog_main);
808 if (unformat (input, "%d", &tmp))
810 elog_alloc (em, tmp);
811 em->n_total_events_disable_limit = ~0;
814 return clib_error_return (0, "Must specify how many events in the ring");
816 vlib_cli_output (vm, "Resized ring and restarted the event logger...");
821 VLIB_CLI_COMMAND (elog_resize_cli, static) = {
822 .path = "event-logger resize",
823 .short_help = "event-logger resize <nnn>",
824 .function = elog_resize_command_fn,
828 #endif /* CLIB_UNIX */
831 elog_show_buffer_internal (vlib_main_t * vm, u32 n_events_to_show)
833 elog_main_t *em = &vm->elog_main;
834 elog_event_t *e, *es;
837 /* Show events in VLIB time since log clock starts after VLIB clock. */
838 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
839 * vm->clib_time.seconds_per_clock;
841 es = elog_peek_events (em);
842 vlib_cli_output (vm, "%d of %d events in buffer, logger %s", vec_len (es),
844 em->n_total_events < em->n_total_events_disable_limit ?
845 "running" : "stopped");
848 vlib_cli_output (vm, "%18.9f: %U",
849 e->time + dt, format_elog_event, em, e);
851 if (n_events_to_show == 0)
858 static clib_error_t *
859 elog_show_buffer (vlib_main_t * vm,
860 unformat_input_t * input, vlib_cli_command_t * cmd)
862 u32 n_events_to_show;
863 clib_error_t *error = 0;
865 n_events_to_show = 250;
866 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
868 if (unformat (input, "%d", &n_events_to_show))
870 else if (unformat (input, "all"))
871 n_events_to_show = ~0;
873 return unformat_parse_error (input);
875 elog_show_buffer_internal (vm, n_events_to_show);
880 VLIB_CLI_COMMAND (elog_show_cli, static) = {
881 .path = "show event-logger",
882 .short_help = "Show event logger info",
883 .function = elog_show_buffer,
888 vlib_gdb_show_event_log (void)
890 elog_show_buffer_internal (vlib_get_main (), (u32) ~ 0);
894 vlib_elog_main_loop_event (vlib_main_t * vm,
896 u64 time, u32 n_vectors, u32 is_return)
898 vlib_main_t *evm = &vlib_global_main;
899 elog_main_t *em = &evm->elog_main;
900 int enabled = evm->elog_trace_graph_dispatch |
901 evm->elog_trace_graph_circuit;
903 if (PREDICT_FALSE (enabled && n_vectors))
905 if (PREDICT_FALSE (!elog_is_enabled (em)))
907 evm->elog_trace_graph_dispatch = 0;
908 evm->elog_trace_graph_circuit = 0;
912 (evm->elog_trace_graph_dispatch ||
913 (evm->elog_trace_graph_circuit &&
914 node_index == evm->elog_trace_graph_circuit_node_index)))
918 vec_elt_at_index (is_return
919 ? evm->node_return_elog_event_types
920 : evm->node_call_elog_event_types,
924 &vlib_worker_threads[vm->thread_index].elog_track
925 : &em->default_track),
926 /* data to log */ n_vectors);
931 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
932 void (*vlib_buffer_trace_trajectory_cb) (vlib_buffer_t * b, u32 node_index);
933 void (*vlib_buffer_trace_trajectory_init_cb) (vlib_buffer_t * b);
936 vlib_buffer_trace_trajectory_init (vlib_buffer_t * b)
938 if (PREDICT_TRUE (vlib_buffer_trace_trajectory_init_cb != 0))
940 (*vlib_buffer_trace_trajectory_init_cb) (b);
947 add_trajectory_trace (vlib_buffer_t * b, u32 node_index)
949 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
950 if (PREDICT_TRUE (vlib_buffer_trace_trajectory_cb != 0))
952 (*vlib_buffer_trace_trajectory_cb) (b, node_index);
957 static_always_inline u64
958 dispatch_node (vlib_main_t * vm,
959 vlib_node_runtime_t * node,
960 vlib_node_type_t type,
961 vlib_node_state_t dispatch_state,
962 vlib_frame_t * frame, u64 last_time_stamp)
966 vlib_node_main_t *nm = &vm->node_main;
967 vlib_next_frame_t *nf;
971 vlib_node_t *n = vlib_get_node (vm, node->node_index);
972 ASSERT (n->type == type);
975 /* Only non-internal nodes may be disabled. */
976 if (type != VLIB_NODE_TYPE_INTERNAL && node->state != dispatch_state)
978 ASSERT (type != VLIB_NODE_TYPE_INTERNAL);
979 return last_time_stamp;
982 if ((type == VLIB_NODE_TYPE_PRE_INPUT || type == VLIB_NODE_TYPE_INPUT)
983 && dispatch_state != VLIB_NODE_STATE_INTERRUPT)
985 u32 c = node->input_main_loops_per_call;
986 /* Only call node when count reaches zero. */
989 node->input_main_loops_per_call = c - 1;
990 return last_time_stamp;
994 /* Speculatively prefetch next frames. */
995 if (node->n_next_nodes > 0)
997 nf = vec_elt_at_index (nm->next_frames, node->next_frame_index);
998 CLIB_PREFETCH (nf, 4 * sizeof (nf[0]), WRITE);
1001 vm->cpu_time_last_node_dispatch = last_time_stamp;
1003 vlib_elog_main_loop_event (vm, node->node_index,
1004 last_time_stamp, frame ? frame->n_vectors : 0,
1007 vlib_node_runtime_perf_counter (vm, node, frame, 0, last_time_stamp,
1008 VLIB_NODE_RUNTIME_PERF_BEFORE);
1011 * Turn this on if you run into
1012 * "bad monkey" contexts, and you want to know exactly
1013 * which nodes they've visited... See ixge.c...
1015 if (VLIB_BUFFER_TRACE_TRAJECTORY && frame)
1019 from = vlib_frame_vector_args (frame);
1020 for (i = 0; i < frame->n_vectors; i++)
1022 vlib_buffer_t *b = vlib_get_buffer (vm, from[i]);
1023 add_trajectory_trace (b, node->node_index);
1025 if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0))
1026 n = node->function (vm, node, frame);
1028 n = vm->dispatch_wrapper_fn (vm, node, frame);
1032 if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0))
1033 n = node->function (vm, node, frame);
1035 n = vm->dispatch_wrapper_fn (vm, node, frame);
1038 t = clib_cpu_time_now ();
1040 vlib_node_runtime_perf_counter (vm, node, frame, n, t,
1041 VLIB_NODE_RUNTIME_PERF_AFTER);
1043 vlib_elog_main_loop_event (vm, node->node_index, t, n, 1 /* is_after */ );
1045 vm->main_loop_vectors_processed += n;
1046 vm->main_loop_nodes_processed += n > 0;
1048 v = vlib_node_runtime_update_stats (vm, node,
1051 /* n_clocks */ t - last_time_stamp);
1053 /* When in interrupt mode and vector rate crosses threshold switch to
1055 if (PREDICT_FALSE ((dispatch_state == VLIB_NODE_STATE_INTERRUPT)
1056 || (dispatch_state == VLIB_NODE_STATE_POLLING
1059 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))))
1062 ELOG_TYPE_DECLARE (e) =
1064 .function = (char *) __FUNCTION__,
1065 .format = "%s vector length %d, switching to %s",
1066 .format_args = "T4i4t4",
1067 .n_enum_strings = 2,
1069 "interrupt", "polling",
1075 u32 node_name, vector_length, is_polling;
1078 if ((dispatch_state == VLIB_NODE_STATE_INTERRUPT
1079 && v >= nm->polling_threshold_vector_length) &&
1081 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1083 vlib_node_t *n = vlib_get_node (vm, node->node_index);
1084 n->state = VLIB_NODE_STATE_POLLING;
1085 node->state = VLIB_NODE_STATE_POLLING;
1087 ~VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
1088 node->flags |= VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
1089 nm->input_node_counts_by_state[VLIB_NODE_STATE_INTERRUPT] -= 1;
1090 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] += 1;
1092 if (PREDICT_FALSE (vlib_global_main.elog_trace_graph_dispatch))
1094 vlib_worker_thread_t *w = vlib_worker_threads
1097 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
1099 ed->node_name = n->name_elog_string;
1100 ed->vector_length = v;
1104 else if (dispatch_state == VLIB_NODE_STATE_POLLING
1105 && v <= nm->interrupt_threshold_vector_length)
1107 vlib_node_t *n = vlib_get_node (vm, node->node_index);
1109 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE)
1111 /* Switch to interrupt mode after dispatch in polling one more time.
1112 This allows driver to re-enable interrupts. */
1113 n->state = VLIB_NODE_STATE_INTERRUPT;
1114 node->state = VLIB_NODE_STATE_INTERRUPT;
1116 ~VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
1117 nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] -= 1;
1118 nm->input_node_counts_by_state[VLIB_NODE_STATE_INTERRUPT] += 1;
1123 vlib_worker_thread_t *w = vlib_worker_threads
1126 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
1127 if (PREDICT_FALSE (vlib_global_main.elog_trace_graph_dispatch))
1129 ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
1131 ed->node_name = n->name_elog_string;
1132 ed->vector_length = v;
1143 dispatch_pending_node (vlib_main_t * vm, uword pending_frame_index,
1144 u64 last_time_stamp)
1146 vlib_node_main_t *nm = &vm->node_main;
1148 vlib_next_frame_t *nf, nf_placeholder;
1149 vlib_node_runtime_t *n;
1150 vlib_frame_t *restore_frame;
1151 vlib_pending_frame_t *p;
1153 /* See comment below about dangling references to nm->pending_frames */
1154 p = nm->pending_frames + pending_frame_index;
1156 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL],
1157 p->node_runtime_index);
1159 f = vlib_get_frame (vm, p->frame);
1160 if (p->next_frame_index == VLIB_PENDING_FRAME_NO_NEXT_FRAME)
1162 /* No next frame: so use placeholder on stack. */
1163 nf = &nf_placeholder;
1164 nf->flags = f->frame_flags & VLIB_NODE_FLAG_TRACE;
1168 nf = vec_elt_at_index (nm->next_frames, p->next_frame_index);
1170 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
1172 /* Force allocation of new frame while current frame is being
1174 restore_frame = NULL;
1175 if (nf->frame == p->frame)
1178 nf->flags &= ~VLIB_FRAME_IS_ALLOCATED;
1179 if (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH))
1180 restore_frame = p->frame;
1183 /* Frame must be pending. */
1184 ASSERT (f->frame_flags & VLIB_FRAME_PENDING);
1185 ASSERT (f->n_vectors > 0);
1187 /* Copy trace flag from next frame to node.
1188 Trace flag indicates that at least one vector in the dispatched
1190 n->flags &= ~VLIB_NODE_FLAG_TRACE;
1191 n->flags |= (nf->flags & VLIB_FRAME_TRACE) ? VLIB_NODE_FLAG_TRACE : 0;
1192 nf->flags &= ~VLIB_FRAME_TRACE;
1194 last_time_stamp = dispatch_node (vm, n,
1195 VLIB_NODE_TYPE_INTERNAL,
1196 VLIB_NODE_STATE_POLLING,
1197 f, last_time_stamp);
1198 /* Internal node vector-rate accounting, for summary stats */
1199 vm->internal_node_vectors += f->n_vectors;
1200 vm->internal_node_calls++;
1201 vm->internal_node_last_vectors_per_main_loop =
1202 (f->n_vectors > vm->internal_node_last_vectors_per_main_loop) ?
1203 f->n_vectors : vm->internal_node_last_vectors_per_main_loop;
1205 f->frame_flags &= ~(VLIB_FRAME_PENDING | VLIB_FRAME_NO_APPEND);
1207 /* Frame is ready to be used again, so restore it. */
1208 if (restore_frame != NULL)
1211 * We musn't restore a frame that is flagged to be freed. This
1212 * shouldn't happen since frames to be freed post dispatch are
1213 * those used when the to-node frame becomes full i.e. they form a
1214 * sort of queue of frames to a single node. If we get here then
1215 * the to-node frame and the pending frame *were* the same, and so
1216 * we removed the to-node frame. Therefore this frame is no
1217 * longer part of the queue for that node and hence it cannot be
1220 ASSERT (!(f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH));
1223 * NB: dispatching node n can result in the creation and scheduling
1224 * of new frames, and hence in the reallocation of nm->pending_frames.
1225 * Recompute p, or no supper. This was broken for more than 10 years.
1227 p = nm->pending_frames + pending_frame_index;
1230 * p->next_frame_index can change during node dispatch if node
1231 * function decides to change graph hook up.
1233 nf = vec_elt_at_index (nm->next_frames, p->next_frame_index);
1234 nf->flags |= VLIB_FRAME_IS_ALLOCATED;
1236 if (NULL == nf->frame)
1238 /* no new frame has been assigned to this node, use the saved one */
1239 nf->frame = restore_frame;
1244 /* The node has gained a frame, implying packets from the current frame
1245 were re-queued to this same node. we don't need the saved one
1247 vlib_frame_free (vm, n, f);
1252 if (f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH)
1254 ASSERT (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH));
1255 vlib_frame_free (vm, n, f);
1259 return last_time_stamp;
1263 vlib_process_stack_is_valid (vlib_process_t * p)
1265 return p->stack[0] == VLIB_PROCESS_STACK_MAGIC;
1271 vlib_process_t *process;
1272 vlib_frame_t *frame;
1273 } vlib_process_bootstrap_args_t;
1275 /* Called in process stack. */
1277 vlib_process_bootstrap (uword _a)
1279 vlib_process_bootstrap_args_t *a;
1281 vlib_node_runtime_t *node;
1286 a = uword_to_pointer (_a, vlib_process_bootstrap_args_t *);
1290 vlib_process_finish_switch_stack (vm);
1293 node = &p->node_runtime;
1295 n = node->function (vm, node, f);
1297 ASSERT (vlib_process_stack_is_valid (p));
1299 vlib_process_start_switch_stack (vm, 0);
1300 clib_longjmp (&p->return_longjmp, n);
1305 /* Called in main stack. */
1306 static_always_inline uword
1307 vlib_process_startup (vlib_main_t * vm, vlib_process_t * p, vlib_frame_t * f)
1309 vlib_process_bootstrap_args_t a;
1316 r = clib_setjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1317 if (r == VLIB_PROCESS_RETURN_LONGJMP_RETURN)
1319 vlib_process_start_switch_stack (vm, p);
1320 r = clib_calljmp (vlib_process_bootstrap, pointer_to_uword (&a),
1321 (void *) p->stack + (1 << p->log2_n_stack_bytes));
1324 vlib_process_finish_switch_stack (vm);
1329 static_always_inline uword
1330 vlib_process_resume (vlib_main_t * vm, vlib_process_t * p)
1333 p->flags &= ~(VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1334 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
1335 | VLIB_PROCESS_RESUME_PENDING);
1336 r = clib_setjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1337 if (r == VLIB_PROCESS_RETURN_LONGJMP_RETURN)
1339 vlib_process_start_switch_stack (vm, p);
1340 clib_longjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_RESUME);
1343 vlib_process_finish_switch_stack (vm);
1348 dispatch_process (vlib_main_t * vm,
1349 vlib_process_t * p, vlib_frame_t * f, u64 last_time_stamp)
1351 vlib_node_main_t *nm = &vm->node_main;
1352 vlib_node_runtime_t *node_runtime = &p->node_runtime;
1353 vlib_node_t *node = vlib_get_node (vm, node_runtime->node_index);
1354 u32 old_process_index;
1356 uword n_vectors, is_suspend;
1358 if (node->state != VLIB_NODE_STATE_POLLING
1359 || (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1360 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)))
1361 return last_time_stamp;
1363 p->flags |= VLIB_PROCESS_IS_RUNNING;
1365 t = last_time_stamp;
1366 vlib_elog_main_loop_event (vm, node_runtime->node_index, t,
1367 f ? f->n_vectors : 0, /* is_after */ 0);
1369 /* Save away current process for suspend. */
1370 old_process_index = nm->current_process_index;
1371 nm->current_process_index = node->runtime_index;
1373 vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp,
1374 VLIB_NODE_RUNTIME_PERF_BEFORE);
1376 n_vectors = vlib_process_startup (vm, p, f);
1378 nm->current_process_index = old_process_index;
1380 ASSERT (n_vectors != VLIB_PROCESS_RETURN_LONGJMP_RETURN);
1381 is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND;
1384 vlib_pending_frame_t *pf;
1387 pool_get (nm->suspended_process_frames, pf);
1388 pf->node_runtime_index = node->runtime_index;
1390 pf->next_frame_index = ~0;
1393 p->suspended_process_frame_index = pf - nm->suspended_process_frames;
1395 if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
1397 TWT (tw_timer_wheel) * tw =
1398 (TWT (tw_timer_wheel) *) nm->timing_wheel;
1399 p->stop_timer_handle =
1400 TW (tw_timer_start) (tw,
1401 vlib_timing_wheel_data_set_suspended_process
1402 (node->runtime_index) /* [sic] pool idex */ ,
1404 p->resume_clock_interval);
1408 p->flags &= ~VLIB_PROCESS_IS_RUNNING;
1410 t = clib_cpu_time_now ();
1412 vlib_elog_main_loop_event (vm, node_runtime->node_index, t, is_suspend,
1415 vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t,
1416 VLIB_NODE_RUNTIME_PERF_AFTER);
1418 vlib_process_update_stats (vm, p,
1419 /* n_calls */ !is_suspend,
1420 /* n_vectors */ n_vectors,
1421 /* n_clocks */ t - last_time_stamp);
1427 vlib_start_process (vlib_main_t * vm, uword process_index)
1429 vlib_node_main_t *nm = &vm->node_main;
1430 vlib_process_t *p = vec_elt (nm->processes, process_index);
1431 dispatch_process (vm, p, /* frame */ 0, /* cpu_time_now */ 0);
1435 dispatch_suspended_process (vlib_main_t * vm,
1436 uword process_index, u64 last_time_stamp)
1438 vlib_node_main_t *nm = &vm->node_main;
1439 vlib_node_runtime_t *node_runtime;
1443 vlib_pending_frame_t *pf;
1444 u64 t, n_vectors, is_suspend;
1446 t = last_time_stamp;
1448 p = vec_elt (nm->processes, process_index);
1449 if (PREDICT_FALSE (!(p->flags & VLIB_PROCESS_IS_RUNNING)))
1450 return last_time_stamp;
1452 ASSERT (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
1453 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT));
1455 pf = pool_elt_at_index (nm->suspended_process_frames,
1456 p->suspended_process_frame_index);
1458 node_runtime = &p->node_runtime;
1459 node = vlib_get_node (vm, node_runtime->node_index);
1462 vlib_elog_main_loop_event (vm, node_runtime->node_index, t,
1463 f ? f->n_vectors : 0, /* is_after */ 0);
1465 /* Save away current process for suspend. */
1466 nm->current_process_index = node->runtime_index;
1468 vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp,
1469 VLIB_NODE_RUNTIME_PERF_BEFORE);
1471 n_vectors = vlib_process_resume (vm, p);
1472 t = clib_cpu_time_now ();
1474 nm->current_process_index = ~0;
1476 is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND;
1479 /* Suspend it again. */
1482 if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
1484 p->stop_timer_handle =
1485 TW (tw_timer_start) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
1486 vlib_timing_wheel_data_set_suspended_process
1487 (node->runtime_index) /* [sic] pool idex */ ,
1489 p->resume_clock_interval);
1494 p->flags &= ~VLIB_PROCESS_IS_RUNNING;
1495 pool_put_index (nm->suspended_process_frames,
1496 p->suspended_process_frame_index);
1497 p->suspended_process_frame_index = ~0;
1500 t = clib_cpu_time_now ();
1501 vlib_elog_main_loop_event (vm, node_runtime->node_index, t, !is_suspend,
1504 vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t,
1505 VLIB_NODE_RUNTIME_PERF_AFTER);
1507 vlib_process_update_stats (vm, p,
1508 /* n_calls */ !is_suspend,
1509 /* n_vectors */ n_vectors,
1510 /* n_clocks */ t - last_time_stamp);
1515 void vl_api_send_pending_rpc_requests (vlib_main_t *) __attribute__ ((weak));
1517 vl_api_send_pending_rpc_requests (vlib_main_t * vm)
1521 static_always_inline void
1522 vlib_main_or_worker_loop (vlib_main_t * vm, int is_main)
1524 vlib_node_main_t *nm = &vm->node_main;
1525 vlib_thread_main_t *tm = vlib_get_thread_main ();
1529 vlib_frame_queue_main_t *fqm;
1530 u32 frame_queue_check_counter = 0;
1532 /* Initialize pending node vector. */
1535 vec_resize (nm->pending_frames, 32);
1536 _vec_len (nm->pending_frames) = 0;
1539 /* Mark time of main loop start. */
1542 cpu_time_now = vm->clib_time.last_cpu_time;
1543 vm->cpu_time_main_loop_start = cpu_time_now;
1546 cpu_time_now = clib_cpu_time_now ();
1548 /* Pre-allocate interupt runtime indices and lock. */
1549 vec_alloc_aligned (nm->pending_interrupts, 1, CLIB_CACHE_LINE_BYTES);
1551 /* Pre-allocate expired nodes. */
1552 if (!nm->polling_threshold_vector_length)
1553 nm->polling_threshold_vector_length = 10;
1554 if (!nm->interrupt_threshold_vector_length)
1555 nm->interrupt_threshold_vector_length = 5;
1557 vm->cpu_id = clib_get_current_cpu_id ();
1558 vm->numa_node = clib_get_current_numa_node ();
1559 os_set_numa_index (vm->numa_node);
1561 /* Start all processes. */
1567 * Perform an initial barrier sync. Pays no attention to
1568 * the barrier sync hold-down timer scheme, which won't work
1569 * at this point in time.
1571 vlib_worker_thread_initial_barrier_sync_and_release (vm);
1573 nm->current_process_index = ~0;
1574 for (i = 0; i < vec_len (nm->processes); i++)
1575 cpu_time_now = dispatch_process (vm, nm->processes[i], /* frame */ 0,
1581 vlib_node_runtime_t *n;
1583 if (PREDICT_FALSE (_vec_len (vm->pending_rpc_requests) > 0))
1586 vl_api_send_pending_rpc_requests (vm);
1590 vlib_worker_thread_barrier_check ();
1592 if (PREDICT_FALSE (vm->check_frame_queues + frame_queue_check_counter))
1596 if (vm->check_frame_queues)
1598 frame_queue_check_counter = 100;
1599 vm->check_frame_queues = 0;
1602 vec_foreach (fqm, tm->frame_queue_mains)
1603 processed += vlib_frame_queue_dequeue (vm, fqm);
1605 /* No handoff queue work found? */
1607 frame_queue_check_counter = 100;
1609 frame_queue_check_counter--;
1612 if (PREDICT_FALSE (vec_len (vm->worker_thread_main_loop_callbacks)))
1613 clib_call_callbacks (vm->worker_thread_main_loop_callbacks, vm,
1616 /* Process pre-input nodes. */
1617 cpu_time_now = clib_cpu_time_now ();
1618 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT])
1619 cpu_time_now = dispatch_node (vm, n,
1620 VLIB_NODE_TYPE_PRE_INPUT,
1621 VLIB_NODE_STATE_POLLING,
1625 /* Next process input nodes. */
1626 vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT])
1627 cpu_time_now = dispatch_node (vm, n,
1628 VLIB_NODE_TYPE_INPUT,
1629 VLIB_NODE_STATE_POLLING,
1633 if (PREDICT_TRUE (is_main && vm->queue_signal_pending == 0))
1634 vm->queue_signal_callback (vm);
1636 if (__atomic_load_n (nm->pending_interrupts, __ATOMIC_ACQUIRE))
1639 *nm->pending_interrupts = 0;
1642 clib_interrupt_get_next (nm->interrupts, int_num)) != -1)
1644 vlib_node_runtime_t *n;
1645 clib_interrupt_clear (nm->interrupts, int_num);
1646 n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT],
1648 cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT,
1649 VLIB_NODE_STATE_INTERRUPT,
1650 /* frame */ 0, cpu_time_now);
1654 /* Input nodes may have added work to the pending vector.
1655 Process pending vector until there is nothing left.
1656 All pending vectors will be processed from input -> output. */
1657 for (i = 0; i < _vec_len (nm->pending_frames); i++)
1658 cpu_time_now = dispatch_pending_node (vm, i, cpu_time_now);
1659 /* Reset pending vector for next iteration. */
1660 _vec_len (nm->pending_frames) = 0;
1665 ELOG_TYPE_DECLARE (es) =
1667 .format = "process tw start",
1670 ELOG_TYPE_DECLARE (ee) =
1672 .format = "process tw end: %d",
1673 .format_args = "i4",
1682 /* Check if process nodes have expired from timing wheel. */
1683 ASSERT (nm->data_from_advancing_timing_wheel != 0);
1685 if (PREDICT_FALSE (vm->elog_trace_graph_dispatch))
1686 ed = ELOG_DATA (&vlib_global_main.elog_main, es);
1688 nm->data_from_advancing_timing_wheel =
1689 TW (tw_timer_expire_timers_vec)
1690 ((TWT (tw_timer_wheel) *) nm->timing_wheel, vlib_time_now (vm),
1691 nm->data_from_advancing_timing_wheel);
1693 ASSERT (nm->data_from_advancing_timing_wheel != 0);
1695 if (PREDICT_FALSE (vm->elog_trace_graph_dispatch))
1697 ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
1699 _vec_len (nm->data_from_advancing_timing_wheel);
1703 (_vec_len (nm->data_from_advancing_timing_wheel) > 0))
1707 for (i = 0; i < _vec_len (nm->data_from_advancing_timing_wheel);
1710 u32 d = nm->data_from_advancing_timing_wheel[i];
1711 u32 di = vlib_timing_wheel_data_get_index (d);
1713 if (vlib_timing_wheel_data_is_timed_event (d))
1715 vlib_signal_timed_event_data_t *te =
1716 pool_elt_at_index (nm->signal_timed_event_data_pool,
1719 vlib_get_node (vm, te->process_node_index);
1721 vec_elt (nm->processes, n->runtime_index);
1724 vlib_process_signal_event_helper (nm, n, p,
1725 te->event_type_index,
1727 te->n_data_elt_bytes);
1728 if (te->n_data_bytes < sizeof (te->inline_event_data))
1729 clib_memcpy_fast (data, te->inline_event_data,
1733 clib_memcpy_fast (data, te->event_data_as_vector,
1735 vec_free (te->event_data_as_vector);
1737 pool_put (nm->signal_timed_event_data_pool, te);
1741 cpu_time_now = clib_cpu_time_now ();
1743 dispatch_suspended_process (vm, di, cpu_time_now);
1746 _vec_len (nm->data_from_advancing_timing_wheel) = 0;
1749 vlib_increment_main_loop_counter (vm);
1750 /* Record time stamp in case there are no enabled nodes and above
1751 calls do not update time stamp. */
1752 cpu_time_now = clib_cpu_time_now ();
1753 vm->loops_this_reporting_interval++;
1754 now = clib_time_now_internal (&vm->clib_time, cpu_time_now);
1755 /* Time to update loops_per_second? */
1756 if (PREDICT_FALSE (now >= vm->loop_interval_end))
1758 /* Next sample ends in 20ms */
1759 if (vm->loop_interval_start)
1761 f64 this_loops_per_second;
1763 this_loops_per_second =
1764 ((f64) vm->loops_this_reporting_interval) / (now -
1765 vm->loop_interval_start);
1767 vm->loops_per_second =
1768 vm->loops_per_second * vm->damping_constant +
1769 (1.0 - vm->damping_constant) * this_loops_per_second;
1770 if (vm->loops_per_second != 0.0)
1771 vm->seconds_per_loop = 1.0 / vm->loops_per_second;
1773 vm->seconds_per_loop = 0.0;
1775 /* New interval starts now, and ends in 20ms */
1776 vm->loop_interval_start = now;
1777 vm->loop_interval_end = now + 2e-4;
1778 vm->loops_this_reporting_interval = 0;
1784 vlib_main_loop (vlib_main_t * vm)
1786 vlib_main_or_worker_loop (vm, /* is_main */ 1);
1790 vlib_worker_loop (vlib_main_t * vm)
1792 vlib_main_or_worker_loop (vm, /* is_main */ 0);
1795 vlib_main_t vlib_global_main;
1798 vlib_add_del_post_mortem_callback (void *cb, int is_add)
1800 vlib_main_t *vm = &vlib_global_main;
1805 for (i = vec_len (vm->post_mortem_callbacks) - 1; i >= 0; i--)
1806 if (vm->post_mortem_callbacks[i] == cb)
1807 vec_del1 (vm->post_mortem_callbacks, i);
1811 for (i = 0; i < vec_len (vm->post_mortem_callbacks); i++)
1812 if (vm->post_mortem_callbacks[i] == cb)
1814 vec_add1 (vm->post_mortem_callbacks, cb);
1818 elog_post_mortem_dump (void)
1820 vlib_main_t *vm = &vlib_global_main;
1821 elog_main_t *em = &vm->elog_main;
1824 clib_error_t *error;
1826 filename = format (0, "/tmp/elog_post_mortem.%d%c", getpid (), 0);
1827 error = elog_write_file (em, (char *) filename, 1 /* flush ring */);
1829 clib_error_report (error);
1831 * We're in the middle of crashing. Don't try to free the filename.
1835 static clib_error_t *
1836 vlib_main_configure (vlib_main_t * vm, unformat_input_t * input)
1838 int turn_on_mem_trace = 0;
1840 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1842 if (unformat (input, "memory-trace"))
1843 turn_on_mem_trace = 1;
1845 else if (unformat (input, "elog-events %d",
1846 &vm->configured_elog_ring_size))
1847 vm->configured_elog_ring_size =
1848 1 << max_log2 (vm->configured_elog_ring_size);
1849 else if (unformat (input, "elog-post-mortem-dump"))
1850 vlib_add_del_post_mortem_callback (elog_post_mortem_dump,
1852 else if (unformat (input, "buffer-alloc-success-rate %f",
1853 &vm->buffer_alloc_success_rate))
1855 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0)
1856 return clib_error_return
1857 (0, "Buffer fault injection not configured");
1859 else if (unformat (input, "buffer-alloc-success-seed %u",
1860 &vm->buffer_alloc_success_seed))
1862 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0)
1863 return clib_error_return
1864 (0, "Buffer fault injection not configured");
1867 return unformat_parse_error (input);
1870 unformat_free (input);
1872 /* Enable memory trace as early as possible. */
1873 if (turn_on_mem_trace)
1879 VLIB_EARLY_CONFIG_FUNCTION (vlib_main_configure, "vlib");
1882 placeholder_queue_signal_callback (vlib_main_t * vm)
1886 #define foreach_weak_reference_stub \
1887 _(vlib_map_stat_segment_init) \
1889 _(vlibmemory_init) \
1890 _(map_api_segment_init)
1893 clib_error_t *name (vlib_main_t *vm) __attribute__((weak)); \
1894 clib_error_t *name (vlib_main_t *vm) { return 0; }
1895 foreach_weak_reference_stub;
1898 void vl_api_set_elog_main (elog_main_t * m) __attribute__ ((weak));
1900 vl_api_set_elog_main (elog_main_t * m)
1902 clib_warning ("STUB");
1905 int vl_api_set_elog_trace_api_messages (int enable) __attribute__ ((weak));
1907 vl_api_set_elog_trace_api_messages (int enable)
1909 clib_warning ("STUB");
1913 int vl_api_get_elog_trace_api_messages (void) __attribute__ ((weak));
1915 vl_api_get_elog_trace_api_messages (void)
1917 clib_warning ("STUB");
1921 /* Main function. */
1923 vlib_main (vlib_main_t * volatile vm, unformat_input_t * input)
1925 clib_error_t *volatile error;
1926 vlib_node_main_t *nm = &vm->node_main;
1928 vm->queue_signal_callback = placeholder_queue_signal_callback;
1930 /* Reconfigure event log which is enabled very early */
1931 if (vm->configured_elog_ring_size &&
1932 vm->configured_elog_ring_size != vm->elog_main.event_ring_size)
1933 elog_resize (&vm->elog_main, vm->configured_elog_ring_size);
1934 vl_api_set_elog_main (&vm->elog_main);
1935 (void) vl_api_set_elog_trace_api_messages (1);
1941 if ((error = vlib_physmem_init (vm)))
1943 clib_error_report (error);
1947 if ((error = vlib_map_stat_segment_init (vm)))
1949 clib_error_report (error);
1953 if ((error = vlib_buffer_main_init (vm)))
1955 clib_error_report (error);
1959 if ((error = vlib_thread_init (vm)))
1961 clib_error_report (error);
1965 /* Register node ifunction variants */
1966 vlib_register_all_node_march_variants (vm);
1968 /* Register static nodes so that init functions may use them. */
1969 vlib_register_all_static_nodes (vm);
1971 /* Set seed for random number generator.
1972 Allow user to specify seed to make random sequence deterministic. */
1973 if (!unformat (input, "seed %wd", &vm->random_seed))
1974 vm->random_seed = clib_cpu_time_now ();
1975 clib_random_buffer_init (&vm->random_buffer, vm->random_seed);
1977 /* Initialize node graph. */
1978 if ((error = vlib_node_main_init (vm)))
1980 /* Arrange for graph hook up error to not be fatal when debugging. */
1982 clib_error_report (error);
1987 /* Direct call / weak reference, for vlib standalone use-cases */
1988 if ((error = vpe_api_init (vm)))
1990 clib_error_report (error);
1994 if ((error = vlibmemory_init (vm)))
1996 clib_error_report (error);
2000 if ((error = map_api_segment_init (vm)))
2002 clib_error_report (error);
2006 /* See unix/main.c; most likely already set up */
2007 if (vm->init_functions_called == 0)
2008 vm->init_functions_called = hash_create (0, /* value bytes */ 0);
2009 if ((error = vlib_call_all_init_functions (vm)))
2012 nm->timing_wheel = clib_mem_alloc_aligned (sizeof (TWT (tw_timer_wheel)),
2013 CLIB_CACHE_LINE_BYTES);
2015 vec_validate (nm->data_from_advancing_timing_wheel, 10);
2016 _vec_len (nm->data_from_advancing_timing_wheel) = 0;
2018 /* Create the process timing wheel */
2019 TW (tw_timer_wheel_init) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
2020 0 /* no callback */ ,
2021 10e-6 /* timer period 10us */ ,
2022 ~0 /* max expirations per call */ );
2024 vec_validate (vm->pending_rpc_requests, 0);
2025 _vec_len (vm->pending_rpc_requests) = 0;
2026 vec_validate (vm->processing_rpc_requests, 0);
2027 _vec_len (vm->processing_rpc_requests) = 0;
2029 /* Default params for the buffer allocator fault injector, if configured */
2030 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
2032 vm->buffer_alloc_success_seed = 0xdeaddabe;
2033 vm->buffer_alloc_success_rate = 0.80;
2036 if ((error = vlib_call_all_config_functions (vm, input, 0 /* is_early */ )))
2040 * Use exponential smoothing, with a half-life of 1 second
2041 * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K)
2043 * Sample every 20ms, aka 50 samples per second
2044 * K = exp (-1.0/20.0);
2047 vm->damping_constant = exp (-1.0 / 20.0);
2049 /* Sort per-thread init functions before we start threads */
2050 vlib_sort_init_exit_functions (&vm->worker_init_function_registrations);
2052 /* Call all main loop enter functions. */
2054 clib_error_t *sub_error;
2055 sub_error = vlib_call_all_main_loop_enter_functions (vm);
2057 clib_error_report (sub_error);
2060 switch (clib_setjmp (&vm->main_loop_exit, VLIB_MAIN_LOOP_EXIT_NONE))
2062 case VLIB_MAIN_LOOP_EXIT_NONE:
2063 vm->main_loop_exit_set = 1;
2066 case VLIB_MAIN_LOOP_EXIT_CLI:
2070 error = vm->main_loop_error;
2074 vlib_main_loop (vm);
2077 vlib_worker_thread_barrier_sync (vm);
2078 /* Call all exit functions. */
2080 clib_error_t *sub_error;
2081 sub_error = vlib_call_all_main_loop_exit_functions (vm);
2083 clib_error_report (sub_error);
2085 vlib_worker_thread_barrier_release (vm);
2088 clib_error_report (error);
2094 vlib_get_main_not_inline (void)
2096 return vlib_get_main ();
2100 vlib_get_elog_main_not_inline ()
2102 return &vlib_global_main.elog_main;
2106 * fd.io coding-style-patch-verification: ON
2109 * eval: (c-set-style "gnu")