Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * main.c: main vector processing loop |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <math.h> |
| 41 | #include <vppinfra/format.h> |
| 42 | #include <vlib/vlib.h> |
| 43 | #include <vlib/threads.h> |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 44 | #include <vlib/stats/stats.h> |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 45 | #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 47 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | #define VLIB_FRAME_MAGIC (0xabadc0ed) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
| 51 | always_inline u32 * |
| 52 | vlib_frame_find_magic (vlib_frame_t * f, vlib_node_t * node) |
| 53 | { |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 54 | return (void *) f + node->magic_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 57 | static vlib_frame_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | vlib_frame_alloc_to_node (vlib_main_t * vm, u32 to_node_index, |
| 59 | u32 frame_flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | vlib_node_main_t *nm = &vm->node_main; |
| 62 | vlib_frame_size_t *fs; |
| 63 | vlib_node_t *to_node; |
| 64 | vlib_frame_t *f; |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 65 | u32 l, n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Dave Barach | a8f4ebd | 2021-02-08 07:56:22 -0500 | [diff] [blame] | 67 | ASSERT (vm == vlib_get_main ()); |
| 68 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | to_node = vlib_get_node (vm, to_node_index); |
| 70 | |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 74 | if (fs->frame_size == 0) |
| 75 | fs->frame_size = to_node->frame_size; |
| 76 | else |
| 77 | ASSERT (fs->frame_size == to_node->frame_size); |
| 78 | |
| 79 | n = fs->frame_size; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 80 | if ((l = vec_len (fs->free_frames)) > 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | { |
| 82 | /* Allocate from end of free list. */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 83 | f = fs->free_frames[l - 1]; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 84 | vec_set_len (fs->free_frames, l - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | } |
| 86 | else |
| 87 | { |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 88 | f = clib_mem_alloc_aligned_no_fail (n, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Poison frame when debugging. */ |
| 92 | if (CLIB_DEBUG > 0) |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 93 | clib_memset_u8 (f, 0xfe, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
| 95 | /* Insert magic number. */ |
| 96 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | u32 *magic; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | magic = vlib_frame_find_magic (f, to_node); |
| 100 | *magic = VLIB_FRAME_MAGIC; |
| 101 | } |
| 102 | |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 103 | f->frame_flags = VLIB_FRAME_IS_ALLOCATED | frame_flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | f->n_vectors = 0; |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 105 | f->scalar_offset = to_node->scalar_offset; |
| 106 | f->vector_offset = to_node->vector_offset; |
| 107 | f->aux_offset = to_node->aux_offset; |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 108 | f->flags = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
| 110 | fs->n_alloc_frames += 1; |
| 111 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 112 | return f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Allocate a frame for from FROM_NODE to TO_NODE via TO_NEXT_INDEX. |
| 116 | Returns frame index. */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 117 | static vlib_frame_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | vlib_frame_alloc (vlib_main_t * vm, vlib_node_runtime_t * from_node_runtime, |
| 119 | u32 to_next_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 121 | vlib_node_t *from_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
| 123 | from_node = vlib_get_node (vm, from_node_runtime->node_index); |
| 124 | ASSERT (to_next_index < vec_len (from_node->next_nodes)); |
| 125 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 126 | return vlib_frame_alloc_to_node (vm, from_node->next_nodes[to_next_index], |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | /* frame_flags */ 0); |
| 128 | } |
| 129 | |
| 130 | vlib_frame_t * |
| 131 | vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index) |
| 132 | { |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 133 | vlib_frame_t *f = vlib_frame_alloc_to_node (vm, to_node_index, |
| 134 | /* frame_flags */ |
| 135 | VLIB_FRAME_FREE_AFTER_DISPATCH); |
| 136 | return vlib_get_frame (vm, f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 139 | static inline void |
| 140 | vlib_validate_frame_indices (vlib_frame_t * f) |
| 141 | { |
| 142 | if (CLIB_DEBUG > 0) |
| 143 | { |
| 144 | int i; |
| 145 | u32 *from = vlib_frame_vector_args (f); |
| 146 | |
| 147 | /* Check for bad buffer index values */ |
| 148 | for (i = 0; i < f->n_vectors; i++) |
| 149 | { |
| 150 | if (from[i] == 0) |
| 151 | { |
| 152 | clib_warning ("BUG: buffer index 0 at index %d", i); |
| 153 | ASSERT (0); |
| 154 | } |
| 155 | else if (from[i] == 0xfefefefe) |
| 156 | { |
| 157 | clib_warning ("BUG: frame poison pattern at index %d", i); |
| 158 | ASSERT (0); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | void |
| 165 | vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 167 | vlib_pending_frame_t *p; |
| 168 | vlib_node_t *to_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
| 170 | if (f->n_vectors == 0) |
| 171 | return; |
| 172 | |
Dave Barach | a8f4ebd | 2021-02-08 07:56:22 -0500 | [diff] [blame] | 173 | ASSERT (vm == vlib_get_main ()); |
| 174 | |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 175 | vlib_validate_frame_indices (f); |
| 176 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | to_node = vlib_get_node (vm, to_node_index); |
| 178 | |
| 179 | vec_add2 (vm->node_main.pending_frames, p, 1); |
| 180 | |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 181 | f->frame_flags |= VLIB_FRAME_PENDING; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 182 | p->frame = vlib_get_frame (vm, f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | p->node_runtime_index = to_node->runtime_index; |
| 184 | p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME; |
| 185 | } |
| 186 | |
| 187 | /* Free given frame. */ |
| 188 | void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 191 | vlib_node_main_t *nm = &vm->node_main; |
| 192 | vlib_node_t *node; |
| 193 | vlib_frame_size_t *fs; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | |
Dave Barach | a8f4ebd | 2021-02-08 07:56:22 -0500 | [diff] [blame] | 195 | ASSERT (vm == vlib_get_main ()); |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 196 | ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | |
| 198 | node = vlib_get_node (vm, r->node_index); |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 199 | fs = vec_elt_at_index (nm->frame_sizes, node->frame_size_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 201 | ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
| 203 | /* No next frames may point to freed frame. */ |
| 204 | if (CLIB_DEBUG > 0) |
| 205 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 206 | vlib_next_frame_t *nf; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 207 | vec_foreach (nf, vm->node_main.next_frames) ASSERT (nf->frame != f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Damjan Marion | 296988d | 2019-02-21 20:24:54 +0100 | [diff] [blame] | 210 | f->frame_flags &= ~(VLIB_FRAME_IS_ALLOCATED | VLIB_FRAME_NO_APPEND); |
Stanislav Zaikin | 3791a03 | 2022-03-31 14:16:28 +0200 | [diff] [blame] | 211 | f->flags = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 213 | vec_add1 (fs->free_frames, f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | ASSERT (fs->n_alloc_frames > 0); |
| 215 | fs->n_alloc_frames -= 1; |
| 216 | } |
| 217 | |
| 218 | static clib_error_t * |
| 219 | show_frame_stats (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 220 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 222 | vlib_frame_size_t *fs; |
| 223 | |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 224 | vlib_cli_output (vm, "%=8s%=6s%=12s%=12s", "Thread", "Size", "# Alloc", |
| 225 | "# Free"); |
| 226 | foreach_vlib_main () |
| 227 | { |
| 228 | vlib_node_main_t *nm = &this_vlib_main->node_main; |
| 229 | vec_foreach (fs, nm->frame_sizes) |
| 230 | { |
| 231 | u32 n_alloc = fs->n_alloc_frames; |
| 232 | u32 n_free = vec_len (fs->free_frames); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
Damjan Marion | b32bd70 | 2021-12-23 17:05:02 +0100 | [diff] [blame] | 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, |
| 237 | n_alloc, n_free); |
| 238 | } |
| 239 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 244 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
| 249 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 250 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
| 252 | /* Change ownership of enqueue rights to given next node. */ |
| 253 | static void |
| 254 | vlib_next_frame_change_ownership (vlib_main_t * vm, |
| 255 | vlib_node_runtime_t * node_runtime, |
| 256 | u32 next_index) |
| 257 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 258 | vlib_node_main_t *nm = &vm->node_main; |
| 259 | vlib_next_frame_t *next_frame; |
| 260 | vlib_node_t *node, *next_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | |
| 262 | node = vec_elt (nm->nodes, node_runtime->node_index); |
| 263 | |
| 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); |
| 268 | |
| 269 | ASSERT (vec_len (node->next_nodes) == node_runtime->n_next_nodes); |
| 270 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | next_frame = |
| 272 | vlib_node_runtime_get_next_frame (vm, node_runtime, next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | next_node = vec_elt (nm->nodes, node->next_nodes[next_index]); |
| 274 | |
| 275 | if (next_node->owner_node_index != VLIB_INVALID_NODE_INDEX) |
| 276 | { |
| 277 | /* Get frame from previous owner. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | vlib_next_frame_t *owner_next_frame; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | vlib_next_frame_t tmp; |
| 280 | |
| 281 | owner_next_frame = |
| 282 | vlib_node_get_next_frame (vm, |
| 283 | next_node->owner_node_index, |
| 284 | next_node->owner_next_index); |
| 285 | |
| 286 | /* Swap target next frame with owner's. */ |
| 287 | tmp = owner_next_frame[0]; |
| 288 | owner_next_frame[0] = next_frame[0]; |
| 289 | next_frame[0] = tmp; |
| 290 | |
| 291 | /* |
| 292 | * If next_frame is already pending, we have to track down |
| 293 | * all pending frames and fix their next_frame_index fields. |
| 294 | */ |
| 295 | if (next_frame->flags & VLIB_FRAME_PENDING) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 296 | { |
| 297 | vlib_pending_frame_t *p; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 298 | if (next_frame->frame != NULL) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 299 | { |
| 300 | vec_foreach (p, nm->pending_frames) |
| 301 | { |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 302 | if (p->frame == next_frame->frame) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 303 | { |
| 304 | p->next_frame_index = |
| 305 | next_frame - vm->node_main.next_frames; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | } |
| 311 | else |
| 312 | { |
| 313 | /* No previous owner. Take ownership. */ |
| 314 | next_frame->flags |= VLIB_FRAME_OWNER; |
| 315 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 316 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | /* Record new owner. */ |
| 318 | next_node->owner_node_index = node->index; |
| 319 | next_node->owner_next_index = next_index; |
| 320 | |
| 321 | /* Now we should be owner. */ |
| 322 | ASSERT (next_frame->flags & VLIB_FRAME_OWNER); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 323 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
| 325 | /* Make sure that magic number is still there. |
| 326 | Otherwise, it is likely that caller has overrun frame arguments. */ |
| 327 | always_inline void |
| 328 | validate_frame_magic (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | vlib_frame_t * f, vlib_node_t * n, uword next_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | ASSERT (VLIB_FRAME_MAGIC == magic[0]); |
| 334 | } |
| 335 | |
| 336 | vlib_frame_t * |
| 337 | vlib_get_next_frame_internal (vlib_main_t * vm, |
| 338 | vlib_node_runtime_t * node, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 339 | u32 next_index, u32 allocate_new_next_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 341 | vlib_frame_t *f; |
| 342 | vlib_next_frame_t *nf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | u32 n_used; |
| 344 | |
| 345 | nf = vlib_node_runtime_get_next_frame (vm, node, next_index); |
| 346 | |
| 347 | /* Make sure this next frame owns right to enqueue to destination frame. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 348 | if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_OWNER))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | vlib_next_frame_change_ownership (vm, node, next_index); |
| 350 | |
| 351 | /* ??? Don't need valid flag: can use frame_index == ~0 */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 352 | if (PREDICT_FALSE (!(nf->flags & VLIB_FRAME_IS_ALLOCATED))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | { |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 354 | nf->frame = vlib_frame_alloc (vm, node, next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 355 | nf->flags |= VLIB_FRAME_IS_ALLOCATED; |
| 356 | } |
| 357 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 358 | f = nf->frame; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | |
| 360 | /* Has frame been removed from pending vector (e.g. finished dispatching)? |
| 361 | If so we can reuse frame. */ |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 362 | if ((nf->flags & VLIB_FRAME_PENDING) |
| 363 | && !(f->frame_flags & VLIB_FRAME_PENDING)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | { |
| 365 | nf->flags &= ~VLIB_FRAME_PENDING; |
| 366 | f->n_vectors = 0; |
Damjan Marion | 9162c2d | 2018-11-20 09:55:10 +0100 | [diff] [blame] | 367 | f->flags = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Damjan Marion | 296988d | 2019-02-21 20:24:54 +0100 | [diff] [blame] | 370 | /* Allocate new frame if current one is marked as no-append or |
| 371 | it is already full. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | n_used = f->n_vectors; |
Damjan Marion | 296988d | 2019-02-21 20:24:54 +0100 | [diff] [blame] | 373 | if (n_used >= VLIB_FRAME_SIZE || (allocate_new_next_frame && n_used > 0) || |
| 374 | (f->frame_flags & VLIB_FRAME_NO_APPEND)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | { |
| 376 | /* Old frame may need to be freed after dispatch, since we'll have |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 377 | two redundant frames from node -> next node. */ |
| 378 | if (!(nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | { |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 380 | vlib_frame_t *f_old = vlib_get_frame (vm, nf->frame); |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 381 | f_old->frame_flags |= VLIB_FRAME_FREE_AFTER_DISPATCH; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Allocate new frame to replace full one. */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 385 | f = nf->frame = vlib_frame_alloc (vm, node, next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | n_used = f->n_vectors; |
| 387 | } |
| 388 | |
| 389 | /* Should have free vectors in frame now. */ |
| 390 | ASSERT (n_used < VLIB_FRAME_SIZE); |
| 391 | |
| 392 | if (CLIB_DEBUG > 0) |
| 393 | { |
| 394 | validate_frame_magic (vm, f, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 395 | vlib_get_node (vm, node->node_index), next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return f; |
| 399 | } |
| 400 | |
| 401 | static void |
| 402 | vlib_put_next_frame_validate (vlib_main_t * vm, |
| 403 | vlib_node_runtime_t * rt, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 404 | u32 next_index, u32 n_vectors_left) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 406 | vlib_node_main_t *nm = &vm->node_main; |
| 407 | vlib_next_frame_t *nf; |
| 408 | vlib_frame_t *f; |
| 409 | vlib_node_runtime_t *next_rt; |
| 410 | vlib_node_t *next_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | u32 n_before, n_after; |
| 412 | |
| 413 | nf = vlib_node_runtime_get_next_frame (vm, rt, next_index); |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 414 | f = vlib_get_frame (vm, nf->frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | |
| 416 | ASSERT (n_vectors_left <= VLIB_FRAME_SIZE); |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 417 | |
| 418 | vlib_validate_frame_indices (f); |
| 419 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | n_after = VLIB_FRAME_SIZE - n_vectors_left; |
| 421 | n_before = f->n_vectors; |
| 422 | |
| 423 | ASSERT (n_after >= n_before); |
| 424 | |
| 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) |
| 429 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 430 | u8 *msg = next_node->validate_frame (vm, rt, f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | if (msg) |
| 432 | { |
| 433 | clib_warning ("%v", msg); |
| 434 | ASSERT (0); |
| 435 | } |
| 436 | vec_free (msg); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void |
| 441 | vlib_put_next_frame (vlib_main_t * vm, |
| 442 | vlib_node_runtime_t * r, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 443 | u32 next_index, u32 n_vectors_left) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 445 | vlib_node_main_t *nm = &vm->node_main; |
| 446 | vlib_next_frame_t *nf; |
| 447 | vlib_frame_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | u32 n_vectors_in_frame; |
| 449 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 450 | if (CLIB_DEBUG > 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | vlib_put_next_frame_validate (vm, r, next_index, n_vectors_left); |
| 452 | |
| 453 | nf = vlib_node_runtime_get_next_frame (vm, r, next_index); |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 454 | f = vlib_get_frame (vm, nf->frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | |
| 456 | /* Make sure that magic number is still there. Otherwise, caller |
| 457 | has overrun frame meta data. */ |
| 458 | if (CLIB_DEBUG > 0) |
| 459 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 460 | vlib_node_t *node = vlib_get_node (vm, r->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | validate_frame_magic (vm, f, node, next_index); |
| 462 | } |
| 463 | |
| 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; |
| 467 | |
| 468 | f->n_vectors = n_vectors_in_frame; |
| 469 | |
| 470 | /* If vectors were added to frame, add to pending vector. */ |
| 471 | if (PREDICT_TRUE (n_vectors_in_frame > 0)) |
| 472 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 473 | vlib_pending_frame_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | u32 v0, v1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 475 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | r->cached_next_index = next_index; |
| 477 | |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 478 | if (!(f->frame_flags & VLIB_FRAME_PENDING)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 479 | { |
| 480 | __attribute__ ((unused)) vlib_node_t *node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 482 | node = vlib_get_node (vm, r->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 484 | vec_add2 (nm->pending_frames, p, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 486 | p->frame = nf->frame; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 487 | p->node_runtime_index = nf->node_runtime_index; |
| 488 | p->next_frame_index = nf - nm->next_frames; |
| 489 | nf->flags |= VLIB_FRAME_PENDING; |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 490 | f->frame_flags |= VLIB_FRAME_PENDING; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 491 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | |
| 493 | /* Copy trace flag from next_frame and from runtime. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 494 | nf->flags |= |
| 495 | (nf->flags & VLIB_NODE_FLAG_TRACE) | (r-> |
| 496 | flags & VLIB_NODE_FLAG_TRACE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | |
| 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)) |
| 502 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 503 | vlib_node_t *node = vlib_get_node (vm, r->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | vec_elt (node->n_vectors_by_next_node, next_index) += v0; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* Sync up runtime (32 bit counters) and main node stats (64 bit counters). */ |
Arthur de Kerhor | 156158f | 2021-02-18 03:09:42 -0800 | [diff] [blame] | 510 | void |
| 511 | vlib_node_runtime_sync_stats_node (vlib_node_t *n, vlib_node_runtime_t *r, |
| 512 | uword n_calls, uword n_vectors, |
| 513 | uword n_clocks) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
| 520 | |
| 521 | r->calls_since_last_overflow = 0; |
| 522 | r->vectors_since_last_overflow = 0; |
| 523 | r->clocks_since_last_overflow = 0; |
| 524 | } |
| 525 | |
Arthur de Kerhor | 156158f | 2021-02-18 03:09:42 -0800 | [diff] [blame] | 526 | void |
| 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) |
| 529 | { |
| 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); |
| 532 | } |
| 533 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 534 | always_inline void __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | vlib_process_sync_stats (vlib_main_t * vm, |
| 536 | vlib_process_t * p, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 537 | uword n_calls, uword n_vectors, uword n_clocks) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 539 | vlib_node_runtime_t *rt = &p->node_runtime; |
| 540 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 541 | vlib_node_runtime_sync_stats (vm, rt, n_calls, n_vectors, n_clocks); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | n->stats_total.suspends += p->n_suspends; |
| 543 | p->n_suspends = 0; |
| 544 | } |
| 545 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 546 | void |
| 547 | vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 549 | vlib_node_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | |
| 551 | if (n->type == VLIB_NODE_TYPE_PROCESS) |
| 552 | { |
| 553 | /* Nothing to do for PROCESS nodes except in main thread */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 554 | if (vm != vlib_get_first_main ()) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 555 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 557 | vlib_process_t *p = vlib_get_process_from_node (vm, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | n->stats_total.suspends += p->n_suspends; |
| 559 | p->n_suspends = 0; |
| 560 | rt = &p->node_runtime; |
| 561 | } |
| 562 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 563 | rt = |
| 564 | vec_elt_at_index (vm->node_main.nodes_by_type[n->type], |
| 565 | n->runtime_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 567 | vlib_node_runtime_sync_stats (vm, rt, 0, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | |
| 569 | /* Sync up runtime next frame vector counters with main node structure. */ |
| 570 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 571 | vlib_next_frame_t *nf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | uword i; |
| 573 | for (i = 0; i < rt->n_next_nodes; i++) |
| 574 | { |
| 575 | nf = vlib_node_runtime_get_next_frame (vm, rt, i); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 576 | vec_elt (n->n_vectors_by_next_node, i) += |
| 577 | nf->vectors_since_last_overflow; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | nf->vectors_since_last_overflow = 0; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | always_inline u32 |
| 584 | vlib_node_runtime_update_stats (vlib_main_t * vm, |
| 585 | vlib_node_runtime_t * node, |
| 586 | uword n_calls, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 587 | uword n_vectors, uword n_clocks) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 588 | { |
| 589 | u32 ca0, ca1, v0, v1, cl0, cl1, r; |
| 590 | |
| 591 | cl0 = cl1 = node->clocks_since_last_overflow; |
| 592 | ca0 = ca1 = node->calls_since_last_overflow; |
| 593 | v0 = v1 = node->vectors_since_last_overflow; |
| 594 | |
| 595 | ca1 = ca0 + n_calls; |
| 596 | v1 = v0 + n_vectors; |
| 597 | cl1 = cl0 + n_clocks; |
| 598 | |
| 599 | node->calls_since_last_overflow = ca1; |
| 600 | node->clocks_since_last_overflow = cl1; |
| 601 | node->vectors_since_last_overflow = v1; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 602 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | node->max_clock_n = node->max_clock > n_clocks ? |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 604 | node->max_clock_n : n_vectors; |
| 605 | node->max_clock = node->max_clock > n_clocks ? node->max_clock : n_clocks; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | |
| 607 | r = vlib_node_runtime_update_main_loop_vector_stats (vm, node, n_vectors); |
| 608 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 609 | if (PREDICT_FALSE (ca1 < ca0 || v1 < v0 || cl1 < cl0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 610 | { |
| 611 | node->calls_since_last_overflow = ca0; |
| 612 | node->clocks_since_last_overflow = cl0; |
| 613 | node->vectors_since_last_overflow = v0; |
Dave Barach | 4d1a866 | 2018-09-10 12:31:15 -0400 | [diff] [blame] | 614 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 615 | vlib_node_runtime_sync_stats (vm, node, n_calls, n_vectors, n_clocks); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return r; |
| 619 | } |
| 620 | |
Dave Barach | 17e5d80 | 2019-05-01 11:30:13 -0400 | [diff] [blame] | 621 | always_inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | vlib_process_update_stats (vlib_main_t * vm, |
| 623 | vlib_process_t * p, |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 624 | uword n_calls, uword n_vectors, uword n_clocks) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | { |
| 626 | vlib_node_runtime_update_stats (vm, &p->node_runtime, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 627 | n_calls, n_vectors, n_clocks); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static clib_error_t * |
| 631 | vlib_cli_elog_clear (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 632 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 634 | elog_reset_buffer (&vlib_global_main.elog_main); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 638 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 639 | VLIB_CLI_COMMAND (elog_clear_cli, static) = { |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 640 | .path = "event-logger clear", |
| 641 | .short_help = "Clear the event log", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | .function = vlib_cli_elog_clear, |
| 643 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 644 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | |
| 646 | #ifdef CLIB_UNIX |
| 647 | static clib_error_t * |
| 648 | elog_save_buffer (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 649 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 651 | elog_main_t *em = &vlib_global_main.elog_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 652 | char *file, *chroot_file; |
| 653 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 654 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 655 | if (!unformat (input, "%s", &file)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | { |
| 657 | vlib_cli_output (vm, "expected file name, got `%U'", |
| 658 | format_unformat_error, input); |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | /* It's fairly hard to get "../oopsie" through unformat; just in case */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 663 | if (strstr (file, "..") || index (file, '/')) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | { |
| 665 | vlib_cli_output (vm, "illegal characters in filename '%s'", file); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | chroot_file = (char *) format (0, "/tmp/%s%c", file, 0); |
| 670 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 671 | vec_free (file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | |
| 673 | vlib_cli_output (vm, "Saving %wd of %wd events to %s", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 674 | elog_n_events_in_buffer (em), |
| 675 | elog_buffer_capacity (em), chroot_file); |
| 676 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | vlib_worker_thread_barrier_sync (vm); |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 678 | error = elog_write_file (em, chroot_file, 1 /* flush ring */ ); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 679 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | vec_free (chroot_file); |
| 681 | return error; |
| 682 | } |
| 683 | |
Dave Barach | 8148131 | 2017-05-16 09:08:14 -0400 | [diff] [blame] | 684 | void |
Dave Barach | 27d978c | 2020-11-03 09:59:06 -0500 | [diff] [blame] | 685 | vlib_post_mortem_dump (void) |
Dave Barach | 8148131 | 2017-05-16 09:08:14 -0400 | [diff] [blame] | 686 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 687 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Dave Barach | 27d978c | 2020-11-03 09:59:06 -0500 | [diff] [blame] | 688 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 689 | for (int i = 0; i < vec_len (vgm->post_mortem_callbacks); i++) |
| 690 | (vgm->post_mortem_callbacks[i]) (); |
Dave Barach | 8148131 | 2017-05-16 09:08:14 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 693 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | VLIB_CLI_COMMAND (elog_save_cli, static) = { |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 695 | .path = "event-logger save", |
| 696 | .short_help = "event-logger save <filename> (saves log in /tmp/<filename>)", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | .function = elog_save_buffer, |
| 698 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 699 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 700 | |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 701 | static clib_error_t * |
| 702 | elog_stop (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 703 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 704 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 705 | elog_main_t *em = &vlib_global_main.elog_main; |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 706 | |
| 707 | em->n_total_events_disable_limit = em->n_total_events; |
| 708 | |
| 709 | vlib_cli_output (vm, "Stopped the event logger..."); |
| 710 | return 0; |
| 711 | } |
| 712 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 713 | /* *INDENT-OFF* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 714 | VLIB_CLI_COMMAND (elog_stop_cli, static) = { |
| 715 | .path = "event-logger stop", |
| 716 | .short_help = "Stop the event-logger", |
| 717 | .function = elog_stop, |
| 718 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 719 | /* *INDENT-ON* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 720 | |
| 721 | static clib_error_t * |
| 722 | elog_restart (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 723 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 724 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 725 | elog_main_t *em = &vlib_global_main.elog_main; |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 726 | |
| 727 | em->n_total_events_disable_limit = ~0; |
| 728 | |
| 729 | vlib_cli_output (vm, "Restarted the event logger..."); |
| 730 | return 0; |
| 731 | } |
| 732 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 733 | /* *INDENT-OFF* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 734 | VLIB_CLI_COMMAND (elog_restart_cli, static) = { |
| 735 | .path = "event-logger restart", |
| 736 | .short_help = "Restart the event-logger", |
| 737 | .function = elog_restart, |
| 738 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 739 | /* *INDENT-ON* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 740 | |
| 741 | static clib_error_t * |
Dave Barach | bc867c3 | 2020-11-25 10:07:09 -0500 | [diff] [blame] | 742 | elog_resize_command_fn (vlib_main_t * vm, |
| 743 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 744 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 745 | elog_main_t *em = &vlib_global_main.elog_main; |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 746 | u32 tmp; |
| 747 | |
| 748 | /* Stop the parade */ |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 749 | elog_reset_buffer (em); |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 750 | |
| 751 | if (unformat (input, "%d", &tmp)) |
| 752 | { |
| 753 | elog_alloc (em, tmp); |
| 754 | em->n_total_events_disable_limit = ~0; |
| 755 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 756 | else |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 757 | return clib_error_return (0, "Must specify how many events in the ring"); |
| 758 | |
| 759 | vlib_cli_output (vm, "Resized ring and restarted the event logger..."); |
| 760 | return 0; |
| 761 | } |
| 762 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 763 | /* *INDENT-OFF* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 764 | VLIB_CLI_COMMAND (elog_resize_cli, static) = { |
| 765 | .path = "event-logger resize", |
| 766 | .short_help = "event-logger resize <nnn>", |
Dave Barach | bc867c3 | 2020-11-25 10:07:09 -0500 | [diff] [blame] | 767 | .function = elog_resize_command_fn, |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 768 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 769 | /* *INDENT-ON* */ |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 770 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | #endif /* CLIB_UNIX */ |
| 772 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 773 | static void |
| 774 | elog_show_buffer_internal (vlib_main_t * vm, u32 n_events_to_show) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 776 | elog_main_t *em = &vlib_global_main.elog_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 777 | elog_event_t *e, *es; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | f64 dt; |
| 779 | |
| 780 | /* Show events in VLIB time since log clock starts after VLIB clock. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 781 | dt = (em->init_time.cpu - vm->clib_time.init_cpu_time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | * vm->clib_time.seconds_per_clock; |
| 783 | |
| 784 | es = elog_peek_events (em); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 785 | vlib_cli_output (vm, "%d of %d events in buffer, logger %s", vec_len (es), |
| 786 | em->event_ring_size, |
| 787 | em->n_total_events < em->n_total_events_disable_limit ? |
| 788 | "running" : "stopped"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | vec_foreach (e, es) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 790 | { |
| 791 | vlib_cli_output (vm, "%18.9f: %U", |
| 792 | e->time + dt, format_elog_event, em, e); |
| 793 | n_events_to_show--; |
| 794 | if (n_events_to_show == 0) |
| 795 | break; |
| 796 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | vec_free (es); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 798 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static clib_error_t * |
| 802 | elog_show_buffer (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 803 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 804 | { |
| 805 | u32 n_events_to_show; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 806 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
| 808 | n_events_to_show = 250; |
| 809 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 810 | { |
| 811 | if (unformat (input, "%d", &n_events_to_show)) |
| 812 | ; |
| 813 | else if (unformat (input, "all")) |
| 814 | n_events_to_show = ~0; |
| 815 | else |
| 816 | return unformat_parse_error (input); |
| 817 | } |
| 818 | elog_show_buffer_internal (vm, n_events_to_show); |
| 819 | return error; |
| 820 | } |
| 821 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 822 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
| 827 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 828 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 829 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 830 | void |
| 831 | vlib_gdb_show_event_log (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 832 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 833 | elog_show_buffer_internal (vlib_get_main (), (u32) ~ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 836 | static inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 837 | vlib_elog_main_loop_event (vlib_main_t * vm, |
| 838 | u32 node_index, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 839 | u64 time, u32 n_vectors, u32 is_return) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 841 | vlib_main_t *evm = vlib_get_first_main (); |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 842 | elog_main_t *em = vlib_get_elog_main (); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 843 | int enabled = evm->elog_trace_graph_dispatch | |
| 844 | evm->elog_trace_graph_circuit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 845 | |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 846 | if (PREDICT_FALSE (enabled && n_vectors)) |
| 847 | { |
| 848 | if (PREDICT_FALSE (!elog_is_enabled (em))) |
| 849 | { |
| 850 | evm->elog_trace_graph_dispatch = 0; |
| 851 | evm->elog_trace_graph_circuit = 0; |
| 852 | return; |
| 853 | } |
| 854 | if (PREDICT_TRUE |
| 855 | (evm->elog_trace_graph_dispatch || |
| 856 | (evm->elog_trace_graph_circuit && |
| 857 | node_index == evm->elog_trace_graph_circuit_node_index))) |
| 858 | { |
| 859 | elog_track (em, |
| 860 | /* event type */ |
| 861 | vec_elt_at_index (is_return |
| 862 | ? evm->node_return_elog_event_types |
| 863 | : evm->node_call_elog_event_types, |
| 864 | node_index), |
| 865 | /* track */ |
| 866 | (vm->thread_index ? |
| 867 | &vlib_worker_threads[vm->thread_index].elog_track |
| 868 | : &em->default_track), |
| 869 | /* data to log */ n_vectors); |
| 870 | } |
| 871 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 874 | static inline void |
| 875 | add_trajectory_trace (vlib_buffer_t * b, u32 node_index) |
| 876 | { |
| 877 | #if VLIB_BUFFER_TRACE_TRAJECTORY > 0 |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 878 | if (PREDICT_FALSE (b->trajectory_nb >= VLIB_BUFFER_TRACE_TRAJECTORY_MAX)) |
| 879 | return; |
| 880 | b->trajectory_trace[b->trajectory_nb] = node_index; |
| 881 | b->trajectory_nb++; |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 882 | #endif |
| 883 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 884 | |
Damjan Marion | 9a332e1 | 2017-03-28 15:11:20 +0200 | [diff] [blame] | 885 | static_always_inline u64 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 890 | vlib_frame_t * frame, u64 last_time_stamp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 891 | { |
| 892 | uword n, v; |
| 893 | u64 t; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 894 | vlib_node_main_t *nm = &vm->node_main; |
| 895 | vlib_next_frame_t *nf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 896 | |
| 897 | if (CLIB_DEBUG > 0) |
| 898 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 899 | vlib_node_t *n = vlib_get_node (vm, node->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | ASSERT (n->type == type); |
| 901 | } |
| 902 | |
| 903 | /* Only non-internal nodes may be disabled. */ |
| 904 | if (type != VLIB_NODE_TYPE_INTERNAL && node->state != dispatch_state) |
| 905 | { |
| 906 | ASSERT (type != VLIB_NODE_TYPE_INTERNAL); |
| 907 | return last_time_stamp; |
| 908 | } |
| 909 | |
| 910 | if ((type == VLIB_NODE_TYPE_PRE_INPUT || type == VLIB_NODE_TYPE_INPUT) |
| 911 | && dispatch_state != VLIB_NODE_STATE_INTERRUPT) |
| 912 | { |
| 913 | u32 c = node->input_main_loops_per_call; |
| 914 | /* Only call node when count reaches zero. */ |
| 915 | if (c) |
| 916 | { |
| 917 | node->input_main_loops_per_call = c - 1; |
| 918 | return last_time_stamp; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* Speculatively prefetch next frames. */ |
| 923 | if (node->n_next_nodes > 0) |
| 924 | { |
| 925 | nf = vec_elt_at_index (nm->next_frames, node->next_frame_index); |
| 926 | CLIB_PREFETCH (nf, 4 * sizeof (nf[0]), WRITE); |
| 927 | } |
| 928 | |
| 929 | vm->cpu_time_last_node_dispatch = last_time_stamp; |
| 930 | |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 931 | vlib_elog_main_loop_event (vm, node->node_index, |
| 932 | last_time_stamp, frame ? frame->n_vectors : 0, |
| 933 | /* is_after */ 0); |
| 934 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 935 | vlib_node_runtime_perf_counter (vm, node, frame, 0, last_time_stamp, |
| 936 | VLIB_NODE_RUNTIME_PERF_BEFORE); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 937 | |
| 938 | /* |
| 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... |
| 942 | */ |
| 943 | if (VLIB_BUFFER_TRACE_TRAJECTORY && frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 945 | int i; |
| 946 | u32 *from; |
| 947 | from = vlib_frame_vector_args (frame); |
| 948 | for (i = 0; i < frame->n_vectors; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 949 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 950 | vlib_buffer_t *b = vlib_get_buffer (vm, from[i]); |
| 951 | add_trajectory_trace (b, node->node_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 952 | } |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 953 | if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0)) |
| 954 | n = node->function (vm, node, frame); |
| 955 | else |
| 956 | n = vm->dispatch_wrapper_fn (vm, node, frame); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 957 | } |
| 958 | else |
| 959 | { |
Damjan Marion | 8b60fb0 | 2020-11-27 20:15:17 +0100 | [diff] [blame] | 960 | if (PREDICT_TRUE (vm->dispatch_wrapper_fn == 0)) |
| 961 | n = node->function (vm, node, frame); |
| 962 | else |
| 963 | n = vm->dispatch_wrapper_fn (vm, node, frame); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | t = clib_cpu_time_now (); |
| 967 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 968 | vlib_node_runtime_perf_counter (vm, node, frame, n, t, |
| 969 | VLIB_NODE_RUNTIME_PERF_AFTER); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 970 | |
| 971 | vlib_elog_main_loop_event (vm, node->node_index, t, n, 1 /* is_after */ ); |
| 972 | |
| 973 | vm->main_loop_vectors_processed += n; |
| 974 | vm->main_loop_nodes_processed += n > 0; |
| 975 | |
| 976 | v = vlib_node_runtime_update_stats (vm, node, |
| 977 | /* n_calls */ 1, |
| 978 | /* n_vectors */ n, |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 979 | /* n_clocks */ t - last_time_stamp); |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 980 | |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 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)) |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 984 | { |
| 985 | /* *INDENT-OFF* */ |
| 986 | ELOG_TYPE_DECLARE (e) = |
| 987 | { |
| 988 | .function = (char *) __FUNCTION__, |
| 989 | .format = "%s vector length %d, switching to %s", |
| 990 | .format_args = "T4i4t4", |
| 991 | .n_enum_strings = 2, |
| 992 | .enum_strings = { |
| 993 | "interrupt", "polling", |
| 994 | }, |
| 995 | }; |
| 996 | /* *INDENT-ON* */ |
| 997 | struct |
| 998 | { |
| 999 | u32 node_name, vector_length, is_polling; |
| 1000 | } *ed; |
| 1001 | |
| 1002 | if ((dispatch_state == VLIB_NODE_STATE_INTERRUPT |
| 1003 | && v >= nm->polling_threshold_vector_length) && |
| 1004 | !(node->flags & |
| 1005 | VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) |
Dave Barach | 3ae2873 | 2018-11-16 17:19:00 -0500 | [diff] [blame] | 1006 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 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; |
| 1010 | node->flags &= |
| 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1015 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1016 | if (PREDICT_FALSE ( |
| 1017 | vlib_get_first_main ()->elog_trace_graph_dispatch)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1018 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1019 | vlib_worker_thread_t *w = vlib_worker_threads |
| 1020 | + vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1021 | |
Steven | 6aa75af | 2017-02-24 10:03:22 -0800 | [diff] [blame] | 1022 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, |
| 1023 | w->elog_track); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1024 | ed->node_name = n->name_elog_string; |
| 1025 | ed->vector_length = v; |
| 1026 | ed->is_polling = 1; |
| 1027 | } |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1028 | } |
| 1029 | else if (dispatch_state == VLIB_NODE_STATE_POLLING |
| 1030 | && v <= nm->interrupt_threshold_vector_length) |
| 1031 | { |
| 1032 | vlib_node_t *n = vlib_get_node (vm, node->node_index); |
| 1033 | if (node->flags & |
| 1034 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1035 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 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; |
| 1040 | node->flags &= |
| 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; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1044 | |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | vlib_worker_thread_t *w = vlib_worker_threads |
| 1049 | + vm->thread_index; |
| 1050 | node->flags |= |
| 1051 | VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE; |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1052 | if (PREDICT_FALSE ( |
| 1053 | vlib_get_first_main ()->elog_trace_graph_dispatch)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1054 | { |
Steven | 6aa75af | 2017-02-24 10:03:22 -0800 | [diff] [blame] | 1055 | ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, |
| 1056 | w->elog_track); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1057 | ed->node_name = n->name_elog_string; |
| 1058 | ed->vector_length = v; |
| 1059 | ed->is_polling = 0; |
| 1060 | } |
| 1061 | } |
| 1062 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | return t; |
| 1066 | } |
| 1067 | |
Damjan Marion | 9a332e1 | 2017-03-28 15:11:20 +0200 | [diff] [blame] | 1068 | static u64 |
Dave Barach | a626999 | 2017-06-07 08:18:49 -0400 | [diff] [blame] | 1069 | dispatch_pending_node (vlib_main_t * vm, uword pending_frame_index, |
| 1070 | u64 last_time_stamp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1071 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1072 | vlib_node_main_t *nm = &vm->node_main; |
| 1073 | vlib_frame_t *f; |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 1074 | vlib_next_frame_t *nf, nf_placeholder; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1075 | vlib_node_runtime_t *n; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1076 | vlib_frame_t *restore_frame; |
Dave Barach | a626999 | 2017-06-07 08:18:49 -0400 | [diff] [blame] | 1077 | vlib_pending_frame_t *p; |
| 1078 | |
| 1079 | /* See comment below about dangling references to nm->pending_frames */ |
| 1080 | p = nm->pending_frames + pending_frame_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1081 | |
| 1082 | n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL], |
| 1083 | p->node_runtime_index); |
| 1084 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1085 | f = vlib_get_frame (vm, p->frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1086 | if (p->next_frame_index == VLIB_PENDING_FRAME_NO_NEXT_FRAME) |
| 1087 | { |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 1088 | /* No next frame: so use placeholder on stack. */ |
| 1089 | nf = &nf_placeholder; |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1090 | nf->flags = f->frame_flags & VLIB_NODE_FLAG_TRACE; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1091 | nf->frame = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1092 | } |
| 1093 | else |
| 1094 | nf = vec_elt_at_index (nm->next_frames, p->next_frame_index); |
| 1095 | |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1096 | ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1097 | |
| 1098 | /* Force allocation of new frame while current frame is being |
| 1099 | dispatched. */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1100 | restore_frame = NULL; |
| 1101 | if (nf->frame == p->frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1102 | { |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1103 | nf->frame = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1104 | nf->flags &= ~VLIB_FRAME_IS_ALLOCATED; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1105 | if (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH)) |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1106 | restore_frame = p->frame; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* Frame must be pending. */ |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1110 | ASSERT (f->frame_flags & VLIB_FRAME_PENDING); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1111 | ASSERT (f->n_vectors > 0); |
| 1112 | |
| 1113 | /* Copy trace flag from next frame to node. |
| 1114 | Trace flag indicates that at least one vector in the dispatched |
| 1115 | frame is traced. */ |
| 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; |
| 1119 | |
| 1120 | last_time_stamp = dispatch_node (vm, n, |
| 1121 | VLIB_NODE_TYPE_INTERNAL, |
| 1122 | VLIB_NODE_STATE_POLLING, |
| 1123 | f, last_time_stamp); |
Dave Barach | a8df85c | 2019-10-01 13:34:23 -0400 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1130 | |
Damjan Marion | 296988d | 2019-02-21 20:24:54 +0100 | [diff] [blame] | 1131 | f->frame_flags &= ~(VLIB_FRAME_PENDING | VLIB_FRAME_NO_APPEND); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1132 | |
| 1133 | /* Frame is ready to be used again, so restore it. */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1134 | if (restore_frame != NULL) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1135 | { |
Dave Barach | a626999 | 2017-06-07 08:18:49 -0400 | [diff] [blame] | 1136 | /* |
| 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 |
| 1144 | * it's overspill. |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1145 | */ |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1146 | ASSERT (!(f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH)); |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1147 | |
Dave Barach | a626999 | 2017-06-07 08:18:49 -0400 | [diff] [blame] | 1148 | /* |
| 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. |
| 1152 | */ |
| 1153 | p = nm->pending_frames + pending_frame_index; |
| 1154 | |
| 1155 | /* |
| 1156 | * p->next_frame_index can change during node dispatch if node |
| 1157 | * function decides to change graph hook up. |
| 1158 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1159 | nf = vec_elt_at_index (nm->next_frames, p->next_frame_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1160 | nf->flags |= VLIB_FRAME_IS_ALLOCATED; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1161 | |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1162 | if (NULL == nf->frame) |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1163 | { |
| 1164 | /* no new frame has been assigned to this node, use the saved one */ |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1165 | nf->frame = restore_frame; |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1166 | f->n_vectors = 0; |
Stanislav Zaikin | 3791a03 | 2022-03-31 14:16:28 +0200 | [diff] [blame] | 1167 | f->flags = 0; |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1168 | } |
| 1169 | else |
| 1170 | { |
| 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 |
| 1173 | anymore */ |
| 1174 | vlib_frame_free (vm, n, f); |
| 1175 | } |
| 1176 | } |
| 1177 | else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1178 | { |
Damjan Marion | 633b6fd | 2018-09-14 14:38:53 +0200 | [diff] [blame] | 1179 | if (f->frame_flags & VLIB_FRAME_FREE_AFTER_DISPATCH) |
Neale Ranns | 8817061 | 2016-11-22 08:29:51 +0000 | [diff] [blame] | 1180 | { |
| 1181 | ASSERT (!(n->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH)); |
| 1182 | vlib_frame_free (vm, n, f); |
| 1183 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | return last_time_stamp; |
| 1187 | } |
| 1188 | |
| 1189 | always_inline uword |
| 1190 | vlib_process_stack_is_valid (vlib_process_t * p) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1191 | { |
| 1192 | return p->stack[0] == VLIB_PROCESS_STACK_MAGIC; |
| 1193 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1194 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1195 | typedef struct |
| 1196 | { |
| 1197 | vlib_main_t *vm; |
| 1198 | vlib_process_t *process; |
| 1199 | vlib_frame_t *frame; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1200 | } vlib_process_bootstrap_args_t; |
| 1201 | |
| 1202 | /* Called in process stack. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1203 | static uword |
| 1204 | vlib_process_bootstrap (uword _a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1206 | vlib_process_bootstrap_args_t *a; |
| 1207 | vlib_main_t *vm; |
| 1208 | vlib_node_runtime_t *node; |
| 1209 | vlib_frame_t *f; |
| 1210 | vlib_process_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1211 | uword n; |
| 1212 | |
| 1213 | a = uword_to_pointer (_a, vlib_process_bootstrap_args_t *); |
| 1214 | |
| 1215 | vm = a->vm; |
| 1216 | p = a->process; |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1217 | vlib_process_finish_switch_stack (vm); |
| 1218 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1219 | f = a->frame; |
| 1220 | node = &p->node_runtime; |
| 1221 | |
| 1222 | n = node->function (vm, node, f); |
| 1223 | |
| 1224 | ASSERT (vlib_process_stack_is_valid (p)); |
| 1225 | |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1226 | vlib_process_start_switch_stack (vm, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1227 | clib_longjmp (&p->return_longjmp, n); |
| 1228 | |
| 1229 | return n; |
| 1230 | } |
| 1231 | |
| 1232 | /* Called in main stack. */ |
| 1233 | static_always_inline uword |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1234 | vlib_process_startup (vlib_main_t * vm, vlib_process_t * p, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1235 | { |
| 1236 | vlib_process_bootstrap_args_t a; |
| 1237 | uword r; |
| 1238 | |
| 1239 | a.vm = vm; |
| 1240 | a.process = p; |
| 1241 | a.frame = f; |
| 1242 | |
| 1243 | r = clib_setjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_RETURN); |
| 1244 | if (r == VLIB_PROCESS_RETURN_LONGJMP_RETURN) |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1245 | { |
| 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)); |
| 1249 | } |
| 1250 | else |
| 1251 | vlib_process_finish_switch_stack (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1252 | |
| 1253 | return r; |
| 1254 | } |
| 1255 | |
| 1256 | static_always_inline uword |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1257 | vlib_process_resume (vlib_main_t * vm, vlib_process_t * p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1258 | { |
| 1259 | uword r; |
| 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) |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1265 | { |
| 1266 | vlib_process_start_switch_stack (vm, p); |
| 1267 | clib_longjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_RESUME); |
| 1268 | } |
| 1269 | else |
| 1270 | vlib_process_finish_switch_stack (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1271 | return r; |
| 1272 | } |
| 1273 | |
| 1274 | static u64 |
| 1275 | dispatch_process (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1276 | vlib_process_t * p, vlib_frame_t * f, u64 last_time_stamp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1277 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1281 | u32 old_process_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1282 | u64 t; |
| 1283 | uword n_vectors, is_suspend; |
| 1284 | |
| 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; |
| 1289 | |
| 1290 | p->flags |= VLIB_PROCESS_IS_RUNNING; |
| 1291 | |
| 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); |
| 1295 | |
| 1296 | /* Save away current process for suspend. */ |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1297 | old_process_index = nm->current_process_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1298 | nm->current_process_index = node->runtime_index; |
| 1299 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1300 | vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp, |
| 1301 | VLIB_NODE_RUNTIME_PERF_BEFORE); |
| 1302 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1303 | n_vectors = vlib_process_startup (vm, p, f); |
| 1304 | |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1305 | nm->current_process_index = old_process_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1306 | |
| 1307 | ASSERT (n_vectors != VLIB_PROCESS_RETURN_LONGJMP_RETURN); |
| 1308 | is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND; |
| 1309 | if (is_suspend) |
| 1310 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1311 | vlib_pending_frame_t *pf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1312 | |
| 1313 | n_vectors = 0; |
| 1314 | pool_get (nm->suspended_process_frames, pf); |
| 1315 | pf->node_runtime_index = node->runtime_index; |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1316 | pf->frame = f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1317 | pf->next_frame_index = ~0; |
| 1318 | |
| 1319 | p->n_suspends += 1; |
| 1320 | p->suspended_process_frame_index = pf - nm->suspended_process_frames; |
| 1321 | |
| 1322 | if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK) |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1323 | { |
| 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 */ , |
| 1330 | 0 /* timer_id */ , |
| 1331 | p->resume_clock_interval); |
| 1332 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1333 | } |
| 1334 | else |
| 1335 | p->flags &= ~VLIB_PROCESS_IS_RUNNING; |
| 1336 | |
| 1337 | t = clib_cpu_time_now (); |
| 1338 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1339 | vlib_elog_main_loop_event (vm, node_runtime->node_index, t, is_suspend, |
| 1340 | /* is_after */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1341 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1342 | vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t, |
| 1343 | VLIB_NODE_RUNTIME_PERF_AFTER); |
| 1344 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1345 | vlib_process_update_stats (vm, p, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1346 | /* n_calls */ !is_suspend, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1347 | /* n_vectors */ n_vectors, |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 1348 | /* n_clocks */ t - last_time_stamp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1349 | |
| 1350 | return t; |
| 1351 | } |
| 1352 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1353 | void |
| 1354 | vlib_start_process (vlib_main_t * vm, uword process_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1355 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1356 | vlib_node_main_t *nm = &vm->node_main; |
| 1357 | vlib_process_t *p = vec_elt (nm->processes, process_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1358 | dispatch_process (vm, p, /* frame */ 0, /* cpu_time_now */ 0); |
| 1359 | } |
| 1360 | |
| 1361 | static u64 |
| 1362 | dispatch_suspended_process (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1363 | uword process_index, u64 last_time_stamp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1364 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1365 | vlib_node_main_t *nm = &vm->node_main; |
| 1366 | vlib_node_runtime_t *node_runtime; |
| 1367 | vlib_node_t *node; |
| 1368 | vlib_frame_t *f; |
| 1369 | vlib_process_t *p; |
| 1370 | vlib_pending_frame_t *pf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1371 | u64 t, n_vectors, is_suspend; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1372 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1373 | t = last_time_stamp; |
| 1374 | |
| 1375 | p = vec_elt (nm->processes, process_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1376 | if (PREDICT_FALSE (!(p->flags & VLIB_PROCESS_IS_RUNNING))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1377 | return last_time_stamp; |
| 1378 | |
| 1379 | ASSERT (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK |
| 1380 | | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)); |
| 1381 | |
Florin Coras | 221d6f1 | 2018-11-07 20:46:38 -0800 | [diff] [blame] | 1382 | pf = pool_elt_at_index (nm->suspended_process_frames, |
| 1383 | p->suspended_process_frame_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1384 | |
| 1385 | node_runtime = &p->node_runtime; |
| 1386 | node = vlib_get_node (vm, node_runtime->node_index); |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 1387 | f = pf->frame; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1388 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1389 | vlib_elog_main_loop_event (vm, node_runtime->node_index, t, |
| 1390 | f ? f->n_vectors : 0, /* is_after */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1391 | |
| 1392 | /* Save away current process for suspend. */ |
| 1393 | nm->current_process_index = node->runtime_index; |
| 1394 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1395 | vlib_node_runtime_perf_counter (vm, node_runtime, f, 0, last_time_stamp, |
| 1396 | VLIB_NODE_RUNTIME_PERF_BEFORE); |
| 1397 | |
Damjan Marion | cea4652 | 2020-05-21 16:47:05 +0200 | [diff] [blame] | 1398 | n_vectors = vlib_process_resume (vm, p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1399 | t = clib_cpu_time_now (); |
| 1400 | |
| 1401 | nm->current_process_index = ~0; |
| 1402 | |
| 1403 | is_suspend = n_vectors == VLIB_PROCESS_RETURN_LONGJMP_SUSPEND; |
| 1404 | if (is_suspend) |
| 1405 | { |
| 1406 | /* Suspend it again. */ |
| 1407 | n_vectors = 0; |
| 1408 | p->n_suspends += 1; |
| 1409 | if (p->flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK) |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1410 | { |
| 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 */ , |
| 1415 | 0 /* timer_id */ , |
| 1416 | p->resume_clock_interval); |
| 1417 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1418 | } |
| 1419 | else |
| 1420 | { |
| 1421 | p->flags &= ~VLIB_PROCESS_IS_RUNNING; |
Florin Coras | 221d6f1 | 2018-11-07 20:46:38 -0800 | [diff] [blame] | 1422 | pool_put_index (nm->suspended_process_frames, |
| 1423 | p->suspended_process_frame_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1424 | p->suspended_process_frame_index = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | t = clib_cpu_time_now (); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1428 | vlib_elog_main_loop_event (vm, node_runtime->node_index, t, !is_suspend, |
| 1429 | /* is_after */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1430 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1431 | vlib_node_runtime_perf_counter (vm, node_runtime, f, n_vectors, t, |
| 1432 | VLIB_NODE_RUNTIME_PERF_AFTER); |
| 1433 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1434 | vlib_process_update_stats (vm, p, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1435 | /* n_calls */ !is_suspend, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1436 | /* n_vectors */ n_vectors, |
Dave Barach | ec595ef | 2019-01-24 10:34:24 -0500 | [diff] [blame] | 1437 | /* n_clocks */ t - last_time_stamp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1438 | |
| 1439 | return t; |
| 1440 | } |
| 1441 | |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 1442 | void vl_api_send_pending_rpc_requests (vlib_main_t *) __attribute__ ((weak)); |
| 1443 | void |
| 1444 | vl_api_send_pending_rpc_requests (vlib_main_t * vm) |
| 1445 | { |
| 1446 | } |
| 1447 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1448 | static_always_inline void |
| 1449 | vlib_main_or_worker_loop (vlib_main_t * vm, int is_main) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1450 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1451 | vlib_node_main_t *nm = &vm->node_main; |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1452 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1453 | uword i; |
| 1454 | u64 cpu_time_now; |
Dave Barach | 000a029 | 2020-02-17 17:07:12 -0500 | [diff] [blame] | 1455 | f64 now; |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1456 | vlib_frame_queue_main_t *fqm; |
Dave Barach | 80965f5 | 2019-03-11 09:57:38 -0400 | [diff] [blame] | 1457 | u32 frame_queue_check_counter = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1458 | |
| 1459 | /* Initialize pending node vector. */ |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1460 | if (is_main) |
| 1461 | { |
| 1462 | vec_resize (nm->pending_frames, 32); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1463 | vec_set_len (nm->pending_frames, 0); |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1464 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1465 | |
| 1466 | /* Mark time of main loop start. */ |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1467 | if (is_main) |
| 1468 | { |
| 1469 | cpu_time_now = vm->clib_time.last_cpu_time; |
| 1470 | vm->cpu_time_main_loop_start = cpu_time_now; |
| 1471 | } |
| 1472 | else |
| 1473 | cpu_time_now = clib_cpu_time_now (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1474 | |
Damjan Marion | 2c2b640 | 2017-03-28 14:16:15 +0200 | [diff] [blame] | 1475 | /* Pre-allocate interupt runtime indices and lock. */ |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1476 | vec_alloc_aligned (nm->pending_interrupts, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 2c2b640 | 2017-03-28 14:16:15 +0200 | [diff] [blame] | 1477 | |
| 1478 | /* Pre-allocate expired nodes. */ |
Steven | 7312cc7 | 2017-03-15 21:18:55 -0700 | [diff] [blame] | 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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1483 | |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 1484 | vm->cpu_id = clib_get_current_cpu_id (); |
| 1485 | vm->numa_node = clib_get_current_numa_node (); |
Florin Coras | 4c95995 | 2020-02-09 18:09:31 +0000 | [diff] [blame] | 1486 | os_set_numa_index (vm->numa_node); |
Damjan Marion | 29c0b33 | 2019-01-28 13:41:27 +0100 | [diff] [blame] | 1487 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1488 | /* Start all processes. */ |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1489 | if (is_main) |
| 1490 | { |
| 1491 | uword i; |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 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. |
| 1497 | */ |
| 1498 | vlib_worker_thread_initial_barrier_sync_and_release (vm); |
| 1499 | |
Steven | f3b5364 | 2017-05-01 14:03:02 -0700 | [diff] [blame] | 1500 | nm->current_process_index = ~0; |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1501 | for (i = 0; i < vec_len (nm->processes); i++) |
| 1502 | cpu_time_now = dispatch_process (vm, nm->processes[i], /* frame */ 0, |
| 1503 | cpu_time_now); |
| 1504 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1505 | |
| 1506 | while (1) |
| 1507 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1508 | vlib_node_runtime_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1509 | |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 1510 | if (PREDICT_FALSE (_vec_len (vm->pending_rpc_requests) > 0)) |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 1511 | { |
| 1512 | if (!is_main) |
| 1513 | vl_api_send_pending_rpc_requests (vm); |
| 1514 | } |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 1515 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1516 | if (!is_main) |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1517 | vlib_worker_thread_barrier_check (); |
| 1518 | |
| 1519 | if (PREDICT_FALSE (vm->check_frame_queues + frame_queue_check_counter)) |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1520 | { |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1521 | u32 processed = 0; |
Damjan Marion | eee099e | 2021-05-01 14:56:13 +0200 | [diff] [blame] | 1522 | vlib_frame_queue_dequeue_fn_t *fn = |
| 1523 | vlib_buffer_func_main.frame_queue_dequeue_fn; |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1524 | |
| 1525 | if (vm->check_frame_queues) |
Dave Barach | 80965f5 | 2019-03-11 09:57:38 -0400 | [diff] [blame] | 1526 | { |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1527 | frame_queue_check_counter = 100; |
| 1528 | vm->check_frame_queues = 0; |
Dave Barach | 80965f5 | 2019-03-11 09:57:38 -0400 | [diff] [blame] | 1529 | } |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1530 | |
| 1531 | vec_foreach (fqm, tm->frame_queue_mains) |
Damjan Marion | eee099e | 2021-05-01 14:56:13 +0200 | [diff] [blame] | 1532 | processed += (fn) (vm, fqm); |
Damjan Marion | f6e6c78 | 2020-09-17 09:54:07 +0200 | [diff] [blame] | 1533 | |
| 1534 | /* No handoff queue work found? */ |
| 1535 | if (processed) |
| 1536 | frame_queue_check_counter = 100; |
| 1537 | else |
| 1538 | frame_queue_check_counter--; |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1541 | if (PREDICT_FALSE (vec_len (vm->worker_thread_main_loop_callbacks))) |
| 1542 | clib_call_callbacks (vm->worker_thread_main_loop_callbacks, vm, |
| 1543 | cpu_time_now); |
| 1544 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1545 | /* Process pre-input nodes. */ |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 1546 | cpu_time_now = clib_cpu_time_now (); |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 1547 | vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_PRE_INPUT]) |
| 1548 | cpu_time_now = dispatch_node (vm, n, |
| 1549 | VLIB_NODE_TYPE_PRE_INPUT, |
| 1550 | VLIB_NODE_STATE_POLLING, |
| 1551 | /* frame */ 0, |
| 1552 | cpu_time_now); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1553 | |
| 1554 | /* Next process input nodes. */ |
| 1555 | vec_foreach (n, nm->nodes_by_type[VLIB_NODE_TYPE_INPUT]) |
| 1556 | cpu_time_now = dispatch_node (vm, n, |
| 1557 | VLIB_NODE_TYPE_INPUT, |
| 1558 | VLIB_NODE_STATE_POLLING, |
| 1559 | /* frame */ 0, |
| 1560 | cpu_time_now); |
| 1561 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1562 | if (PREDICT_TRUE (is_main && vm->queue_signal_pending == 0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1563 | vm->queue_signal_callback (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1564 | |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1565 | if (__atomic_load_n (nm->pending_interrupts, __ATOMIC_ACQUIRE)) |
Damjan Marion | 0b31630 | 2020-09-09 18:55:16 +0200 | [diff] [blame] | 1566 | { |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1567 | int int_num = -1; |
| 1568 | *nm->pending_interrupts = 0; |
Dave Barach | d47c509 | 2018-01-19 13:09:20 -0500 | [diff] [blame] | 1569 | |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1570 | while ((int_num = |
| 1571 | clib_interrupt_get_next (nm->interrupts, int_num)) != -1) |
| 1572 | { |
| 1573 | vlib_node_runtime_t *n; |
| 1574 | clib_interrupt_clear (nm->interrupts, int_num); |
| 1575 | n = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT], |
| 1576 | int_num); |
| 1577 | cpu_time_now = dispatch_node (vm, n, VLIB_NODE_TYPE_INPUT, |
| 1578 | VLIB_NODE_STATE_INTERRUPT, |
| 1579 | /* frame */ 0, cpu_time_now); |
| 1580 | } |
Damjan Marion | 1033b49 | 2020-06-03 12:20:41 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
Dave Barach | e324898 | 2018-08-14 13:47:58 -0400 | [diff] [blame] | 1583 | /* Input nodes may have added work to the pending vector. |
| 1584 | Process pending vector until there is nothing left. |
| 1585 | All pending vectors will be processed from input -> output. */ |
| 1586 | for (i = 0; i < _vec_len (nm->pending_frames); i++) |
| 1587 | cpu_time_now = dispatch_pending_node (vm, i, cpu_time_now); |
| 1588 | /* Reset pending vector for next iteration. */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1589 | vec_set_len (nm->pending_frames, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1590 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1591 | if (is_main) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1592 | { |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1593 | /* *INDENT-OFF* */ |
| 1594 | ELOG_TYPE_DECLARE (es) = |
| 1595 | { |
| 1596 | .format = "process tw start", |
| 1597 | .format_args = "", |
| 1598 | }; |
| 1599 | ELOG_TYPE_DECLARE (ee) = |
| 1600 | { |
| 1601 | .format = "process tw end: %d", |
| 1602 | .format_args = "i4", |
| 1603 | }; |
| 1604 | /* *INDENT-ON* */ |
| 1605 | |
| 1606 | struct |
| 1607 | { |
| 1608 | int nready_procs; |
| 1609 | } *ed; |
| 1610 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1611 | /* Check if process nodes have expired from timing wheel. */ |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1612 | ASSERT (nm->data_from_advancing_timing_wheel != 0); |
| 1613 | |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1614 | if (PREDICT_FALSE (vm->elog_trace_graph_dispatch)) |
| 1615 | ed = ELOG_DATA (&vlib_global_main.elog_main, es); |
| 1616 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1617 | nm->data_from_advancing_timing_wheel = |
| 1618 | TW (tw_timer_expire_timers_vec) |
| 1619 | ((TWT (tw_timer_wheel) *) nm->timing_wheel, vlib_time_now (vm), |
| 1620 | nm->data_from_advancing_timing_wheel); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1621 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1622 | ASSERT (nm->data_from_advancing_timing_wheel != 0); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1623 | |
Dave Barach | 900cbad | 2019-01-31 19:12:51 -0500 | [diff] [blame] | 1624 | if (PREDICT_FALSE (vm->elog_trace_graph_dispatch)) |
| 1625 | { |
| 1626 | ed = ELOG_DATA (&vlib_global_main.elog_main, ee); |
| 1627 | ed->nready_procs = |
| 1628 | _vec_len (nm->data_from_advancing_timing_wheel); |
| 1629 | } |
| 1630 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1631 | if (PREDICT_FALSE |
| 1632 | (_vec_len (nm->data_from_advancing_timing_wheel) > 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1633 | { |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1634 | uword i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1635 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1636 | for (i = 0; i < _vec_len (nm->data_from_advancing_timing_wheel); |
| 1637 | i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1638 | { |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1639 | u32 d = nm->data_from_advancing_timing_wheel[i]; |
| 1640 | u32 di = vlib_timing_wheel_data_get_index (d); |
| 1641 | |
| 1642 | if (vlib_timing_wheel_data_is_timed_event (d)) |
| 1643 | { |
| 1644 | vlib_signal_timed_event_data_t *te = |
| 1645 | pool_elt_at_index (nm->signal_timed_event_data_pool, |
| 1646 | di); |
| 1647 | vlib_node_t *n = |
| 1648 | vlib_get_node (vm, te->process_node_index); |
| 1649 | vlib_process_t *p = |
| 1650 | vec_elt (nm->processes, n->runtime_index); |
| 1651 | void *data; |
| 1652 | data = |
| 1653 | vlib_process_signal_event_helper (nm, n, p, |
| 1654 | te->event_type_index, |
| 1655 | te->n_data_elts, |
| 1656 | te->n_data_elt_bytes); |
| 1657 | if (te->n_data_bytes < sizeof (te->inline_event_data)) |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1658 | clib_memcpy_fast (data, te->inline_event_data, |
| 1659 | te->n_data_bytes); |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1660 | else |
| 1661 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1662 | clib_memcpy_fast (data, te->event_data_as_vector, |
| 1663 | te->n_data_bytes); |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1664 | vec_free (te->event_data_as_vector); |
| 1665 | } |
| 1666 | pool_put (nm->signal_timed_event_data_pool, te); |
| 1667 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1668 | else |
| 1669 | { |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1670 | cpu_time_now = clib_cpu_time_now (); |
| 1671 | cpu_time_now = |
| 1672 | dispatch_suspended_process (vm, di, cpu_time_now); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1673 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1674 | } |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1675 | vec_set_len (nm->data_from_advancing_timing_wheel, 0); |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1676 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1677 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1678 | vlib_increment_main_loop_counter (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1679 | /* Record time stamp in case there are no enabled nodes and above |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1680 | calls do not update time stamp. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1681 | cpu_time_now = clib_cpu_time_now (); |
Dave Barach | 000a029 | 2020-02-17 17:07:12 -0500 | [diff] [blame] | 1682 | vm->loops_this_reporting_interval++; |
| 1683 | now = clib_time_now_internal (&vm->clib_time, cpu_time_now); |
| 1684 | /* Time to update loops_per_second? */ |
| 1685 | if (PREDICT_FALSE (now >= vm->loop_interval_end)) |
| 1686 | { |
| 1687 | /* Next sample ends in 20ms */ |
| 1688 | if (vm->loop_interval_start) |
| 1689 | { |
| 1690 | f64 this_loops_per_second; |
| 1691 | |
| 1692 | this_loops_per_second = |
| 1693 | ((f64) vm->loops_this_reporting_interval) / (now - |
| 1694 | vm->loop_interval_start); |
| 1695 | |
| 1696 | vm->loops_per_second = |
| 1697 | vm->loops_per_second * vm->damping_constant + |
| 1698 | (1.0 - vm->damping_constant) * this_loops_per_second; |
| 1699 | if (vm->loops_per_second != 0.0) |
| 1700 | vm->seconds_per_loop = 1.0 / vm->loops_per_second; |
| 1701 | else |
| 1702 | vm->seconds_per_loop = 0.0; |
| 1703 | } |
| 1704 | /* New interval starts now, and ends in 20ms */ |
| 1705 | vm->loop_interval_start = now; |
| 1706 | vm->loop_interval_end = now + 2e-4; |
| 1707 | vm->loops_this_reporting_interval = 0; |
| 1708 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1709 | } |
| 1710 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1711 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1712 | static void |
| 1713 | vlib_main_loop (vlib_main_t * vm) |
| 1714 | { |
| 1715 | vlib_main_or_worker_loop (vm, /* is_main */ 1); |
| 1716 | } |
| 1717 | |
| 1718 | void |
| 1719 | vlib_worker_loop (vlib_main_t * vm) |
| 1720 | { |
| 1721 | vlib_main_or_worker_loop (vm, /* is_main */ 0); |
| 1722 | } |
| 1723 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1724 | vlib_global_main_t vlib_global_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1725 | |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1726 | void |
| 1727 | vlib_add_del_post_mortem_callback (void *cb, int is_add) |
| 1728 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1729 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1730 | int i; |
| 1731 | |
| 1732 | if (is_add == 0) |
| 1733 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1734 | for (i = vec_len (vgm->post_mortem_callbacks) - 1; i >= 0; i--) |
| 1735 | if (vgm->post_mortem_callbacks[i] == cb) |
| 1736 | vec_del1 (vgm->post_mortem_callbacks, i); |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1737 | return; |
| 1738 | } |
| 1739 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1740 | for (i = 0; i < vec_len (vgm->post_mortem_callbacks); i++) |
| 1741 | if (vgm->post_mortem_callbacks[i] == cb) |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1742 | return; |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1743 | vec_add1 (vgm->post_mortem_callbacks, cb); |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static void |
| 1747 | elog_post_mortem_dump (void) |
| 1748 | { |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 1749 | elog_main_t *em = vlib_get_elog_main (); |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1750 | |
| 1751 | u8 *filename; |
| 1752 | clib_error_t *error; |
| 1753 | |
| 1754 | filename = format (0, "/tmp/elog_post_mortem.%d%c", getpid (), 0); |
| 1755 | error = elog_write_file (em, (char *) filename, 1 /* flush ring */); |
| 1756 | if (error) |
| 1757 | clib_error_report (error); |
| 1758 | /* |
| 1759 | * We're in the middle of crashing. Don't try to free the filename. |
| 1760 | */ |
| 1761 | } |
| 1762 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1763 | static clib_error_t * |
| 1764 | vlib_main_configure (vlib_main_t * vm, unformat_input_t * input) |
| 1765 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1766 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1767 | int turn_on_mem_trace = 0; |
| 1768 | |
| 1769 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1770 | { |
| 1771 | if (unformat (input, "memory-trace")) |
| 1772 | turn_on_mem_trace = 1; |
| 1773 | |
| 1774 | else if (unformat (input, "elog-events %d", |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1775 | &vgm->configured_elog_ring_size)) |
| 1776 | vgm->configured_elog_ring_size = |
| 1777 | 1 << max_log2 (vgm->configured_elog_ring_size); |
Dave Barach | 8148131 | 2017-05-16 09:08:14 -0400 | [diff] [blame] | 1778 | else if (unformat (input, "elog-post-mortem-dump")) |
Damjan Marion | 25ab6c5 | 2021-03-05 14:41:25 +0100 | [diff] [blame] | 1779 | vlib_add_del_post_mortem_callback (elog_post_mortem_dump, |
| 1780 | /* is_add */ 1); |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 1781 | else if (unformat (input, "buffer-alloc-success-rate %f", |
| 1782 | &vm->buffer_alloc_success_rate)) |
| 1783 | { |
| 1784 | if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0) |
| 1785 | return clib_error_return |
| 1786 | (0, "Buffer fault injection not configured"); |
| 1787 | } |
| 1788 | else if (unformat (input, "buffer-alloc-success-seed %u", |
| 1789 | &vm->buffer_alloc_success_seed)) |
| 1790 | { |
| 1791 | if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR == 0) |
| 1792 | return clib_error_return |
| 1793 | (0, "Buffer fault injection not configured"); |
| 1794 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1795 | else |
| 1796 | return unformat_parse_error (input); |
| 1797 | } |
| 1798 | |
| 1799 | unformat_free (input); |
| 1800 | |
| 1801 | /* Enable memory trace as early as possible. */ |
| 1802 | if (turn_on_mem_trace) |
| 1803 | clib_mem_trace (1); |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | VLIB_EARLY_CONFIG_FUNCTION (vlib_main_configure, "vlib"); |
| 1809 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1810 | static void |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 1811 | placeholder_queue_signal_callback (vlib_main_t * vm) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1812 | { |
| 1813 | } |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 1814 | |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 1815 | #define foreach_weak_reference_stub \ |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 1816 | _(vpe_api_init) \ |
| 1817 | _(vlibmemory_init) \ |
| 1818 | _(map_api_segment_init) |
| 1819 | |
| 1820 | #define _(name) \ |
| 1821 | clib_error_t *name (vlib_main_t *vm) __attribute__((weak)); \ |
| 1822 | clib_error_t *name (vlib_main_t *vm) { return 0; } |
| 1823 | foreach_weak_reference_stub; |
| 1824 | #undef _ |
| 1825 | |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1826 | void vl_api_set_elog_main (elog_main_t * m) __attribute__ ((weak)); |
| 1827 | void |
| 1828 | vl_api_set_elog_main (elog_main_t * m) |
| 1829 | { |
| 1830 | clib_warning ("STUB"); |
| 1831 | } |
| 1832 | |
| 1833 | int vl_api_set_elog_trace_api_messages (int enable) __attribute__ ((weak)); |
| 1834 | int |
| 1835 | vl_api_set_elog_trace_api_messages (int enable) |
| 1836 | { |
| 1837 | clib_warning ("STUB"); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | int vl_api_get_elog_trace_api_messages (void) __attribute__ ((weak)); |
| 1842 | int |
| 1843 | vl_api_get_elog_trace_api_messages (void) |
| 1844 | { |
| 1845 | clib_warning ("STUB"); |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1849 | /* Main function. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1850 | int |
Eyal Bari | d334a6b | 2016-09-19 10:23:39 +0300 | [diff] [blame] | 1851 | vlib_main (vlib_main_t * volatile vm, unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1852 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1853 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Eyal Bari | d334a6b | 2016-09-19 10:23:39 +0300 | [diff] [blame] | 1854 | clib_error_t *volatile error; |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1855 | vlib_node_main_t *nm = &vm->node_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1856 | |
Dave Barach | 11fb09e | 2020-08-06 12:10:09 -0400 | [diff] [blame] | 1857 | vm->queue_signal_callback = placeholder_queue_signal_callback; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 1858 | |
Dave Barach | bc867c3 | 2020-11-25 10:07:09 -0500 | [diff] [blame] | 1859 | /* Reconfigure event log which is enabled very early */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1860 | if (vgm->configured_elog_ring_size && |
| 1861 | vgm->configured_elog_ring_size != vgm->elog_main.event_ring_size) |
| 1862 | elog_resize (&vgm->elog_main, vgm->configured_elog_ring_size); |
Damjan Marion | f553a2c | 2021-03-26 13:45:37 +0100 | [diff] [blame] | 1863 | vl_api_set_elog_main (vlib_get_elog_main ()); |
Dave Barach | b09f4d0 | 2019-07-15 16:00:03 -0400 | [diff] [blame] | 1864 | (void) vl_api_set_elog_trace_api_messages (1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1865 | |
| 1866 | /* Default name. */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1867 | if (!vgm->name) |
| 1868 | vgm->name = "VLIB"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1869 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1870 | if ((error = vlib_physmem_init (vm))) |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 1871 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1872 | clib_error_report (error); |
| 1873 | goto done; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 1874 | } |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1875 | |
Damjan Marion | 62d656a | 2022-03-09 16:10:54 +0100 | [diff] [blame] | 1876 | if ((error = vlib_log_init (vm))) |
| 1877 | { |
| 1878 | clib_error_report (error); |
| 1879 | goto done; |
| 1880 | } |
| 1881 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1882 | if ((error = vlib_stats_init (vm))) |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 1883 | { |
| 1884 | clib_error_report (error); |
| 1885 | goto done; |
| 1886 | } |
| 1887 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1888 | if ((error = vlib_buffer_main_init (vm))) |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 1889 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1890 | clib_error_report (error); |
| 1891 | goto done; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 1892 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1893 | |
| 1894 | if ((error = vlib_thread_init (vm))) |
| 1895 | { |
| 1896 | clib_error_report (error); |
| 1897 | goto done; |
| 1898 | } |
| 1899 | |
Damjan Marion | a31698b | 2021-03-10 14:35:28 +0100 | [diff] [blame] | 1900 | /* Register node ifunction variants */ |
| 1901 | vlib_register_all_node_march_variants (vm); |
| 1902 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1903 | /* Register static nodes so that init functions may use them. */ |
| 1904 | vlib_register_all_static_nodes (vm); |
| 1905 | |
| 1906 | /* Set seed for random number generator. |
| 1907 | Allow user to specify seed to make random sequence deterministic. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1908 | if (!unformat (input, "seed %wd", &vm->random_seed)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1909 | vm->random_seed = clib_cpu_time_now (); |
| 1910 | clib_random_buffer_init (&vm->random_buffer, vm->random_seed); |
| 1911 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1912 | /* Initialize node graph. */ |
| 1913 | if ((error = vlib_node_main_init (vm))) |
| 1914 | { |
| 1915 | /* Arrange for graph hook up error to not be fatal when debugging. */ |
| 1916 | if (CLIB_DEBUG > 0) |
| 1917 | clib_error_report (error); |
| 1918 | else |
| 1919 | goto done; |
| 1920 | } |
| 1921 | |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 1922 | /* Direct call / weak reference, for vlib standalone use-cases */ |
| 1923 | if ((error = vpe_api_init (vm))) |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 1924 | { |
| 1925 | clib_error_report (error); |
| 1926 | goto done; |
| 1927 | } |
| 1928 | |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 1929 | if ((error = vlibmemory_init (vm))) |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 1930 | { |
| 1931 | clib_error_report (error); |
| 1932 | goto done; |
| 1933 | } |
| 1934 | |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 1935 | if ((error = map_api_segment_init (vm))) |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 1936 | { |
| 1937 | clib_error_report (error); |
| 1938 | goto done; |
| 1939 | } |
| 1940 | |
Ole Troan | 964f93e | 2016-06-10 13:22:36 +0200 | [diff] [blame] | 1941 | /* See unix/main.c; most likely already set up */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1942 | if (vgm->init_functions_called == 0) |
| 1943 | vgm->init_functions_called = hash_create (0, /* value bytes */ 0); |
Ole Troan | 964f93e | 2016-06-10 13:22:36 +0200 | [diff] [blame] | 1944 | if ((error = vlib_call_all_init_functions (vm))) |
| 1945 | goto done; |
| 1946 | |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1947 | nm->timing_wheel = clib_mem_alloc_aligned (sizeof (TWT (tw_timer_wheel)), |
| 1948 | CLIB_CACHE_LINE_BYTES); |
| 1949 | |
| 1950 | vec_validate (nm->data_from_advancing_timing_wheel, 10); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1951 | vec_set_len (nm->data_from_advancing_timing_wheel, 0); |
Dave Barach | 5c20a01 | 2017-06-13 08:48:31 -0400 | [diff] [blame] | 1952 | |
| 1953 | /* Create the process timing wheel */ |
| 1954 | TW (tw_timer_wheel_init) ((TWT (tw_timer_wheel) *) nm->timing_wheel, |
| 1955 | 0 /* no callback */ , |
| 1956 | 10e-6 /* timer period 10us */ , |
| 1957 | ~0 /* max expirations per call */ ); |
| 1958 | |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 1959 | vec_validate (vm->pending_rpc_requests, 0); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1960 | vec_set_len (vm->pending_rpc_requests, 0); |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 1961 | vec_validate (vm->processing_rpc_requests, 0); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1962 | vec_set_len (vm->processing_rpc_requests, 0); |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 1963 | |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 1964 | /* Default params for the buffer allocator fault injector, if configured */ |
| 1965 | if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0) |
| 1966 | { |
| 1967 | vm->buffer_alloc_success_seed = 0xdeaddabe; |
| 1968 | vm->buffer_alloc_success_rate = 0.80; |
| 1969 | } |
| 1970 | |
Dave Barach | d1e17d0 | 2019-03-21 18:01:48 -0400 | [diff] [blame] | 1971 | if ((error = vlib_call_all_config_functions (vm, input, 0 /* is_early */ ))) |
| 1972 | goto done; |
| 1973 | |
Dave Barach | 000a029 | 2020-02-17 17:07:12 -0500 | [diff] [blame] | 1974 | /* |
| 1975 | * Use exponential smoothing, with a half-life of 1 second |
| 1976 | * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K) |
| 1977 | * |
| 1978 | * Sample every 20ms, aka 50 samples per second |
| 1979 | * K = exp (-1.0/20.0); |
| 1980 | * K = 0.95 |
| 1981 | */ |
| 1982 | vm->damping_constant = exp (-1.0 / 20.0); |
| 1983 | |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame] | 1984 | /* Sort per-thread init functions before we start threads */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 1985 | vlib_sort_init_exit_functions (&vgm->worker_init_function_registrations); |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame] | 1986 | |
Dave Barach | d1e17d0 | 2019-03-21 18:01:48 -0400 | [diff] [blame] | 1987 | /* Call all main loop enter functions. */ |
| 1988 | { |
| 1989 | clib_error_t *sub_error; |
| 1990 | sub_error = vlib_call_all_main_loop_enter_functions (vm); |
| 1991 | if (sub_error) |
| 1992 | clib_error_report (sub_error); |
| 1993 | } |
| 1994 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1995 | switch (clib_setjmp (&vm->main_loop_exit, VLIB_MAIN_LOOP_EXIT_NONE)) |
| 1996 | { |
| 1997 | case VLIB_MAIN_LOOP_EXIT_NONE: |
| 1998 | vm->main_loop_exit_set = 1; |
| 1999 | break; |
| 2000 | |
| 2001 | case VLIB_MAIN_LOOP_EXIT_CLI: |
| 2002 | goto done; |
| 2003 | |
| 2004 | default: |
| 2005 | error = vm->main_loop_error; |
| 2006 | goto done; |
| 2007 | } |
| 2008 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2009 | vlib_main_loop (vm); |
| 2010 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2011 | done: |
Kommula Shiva Shankar | ced43e2 | 2021-01-28 13:05:59 +0530 | [diff] [blame] | 2012 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2013 | /* Call all exit functions. */ |
| 2014 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2015 | clib_error_t *sub_error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2016 | sub_error = vlib_call_all_main_loop_exit_functions (vm); |
| 2017 | if (sub_error) |
| 2018 | clib_error_report (sub_error); |
| 2019 | } |
Kommula Shiva Shankar | ced43e2 | 2021-01-28 13:05:59 +0530 | [diff] [blame] | 2020 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2021 | |
| 2022 | if (error) |
| 2023 | clib_error_report (error); |
| 2024 | |
Pierre Pfister | c26cc72 | 2021-09-10 16:38:03 +0200 | [diff] [blame] | 2025 | return vm->main_loop_exit_status; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2026 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2027 | |
Dave Barach | ab1a50c | 2020-10-06 14:08:16 -0400 | [diff] [blame] | 2028 | vlib_main_t * |
| 2029 | vlib_get_main_not_inline (void) |
| 2030 | { |
| 2031 | return vlib_get_main (); |
| 2032 | } |
| 2033 | |
| 2034 | elog_main_t * |
| 2035 | vlib_get_elog_main_not_inline () |
| 2036 | { |
| 2037 | return &vlib_global_main.elog_main; |
| 2038 | } |
| 2039 | |
Pierre Pfister | c26cc72 | 2021-09-10 16:38:03 +0200 | [diff] [blame] | 2040 | void |
| 2041 | vlib_exit_with_status (vlib_main_t *vm, int status) |
| 2042 | { |
| 2043 | vm->main_loop_exit_status = status; |
| 2044 | __atomic_store_n (&vm->main_loop_exit_now, 1, __ATOMIC_RELEASE); |
| 2045 | } |
| 2046 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2047 | /* |
| 2048 | * fd.io coding-style-patch-verification: ON |
| 2049 | * |
| 2050 | * Local Variables: |
| 2051 | * eval: (c-set-style "gnu") |
| 2052 | * End: |
| 2053 | */ |