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