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 | * node_format.c: node formatting |
| 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 <vlib/vlib.h> |
| 41 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 42 | u8 * |
| 43 | format_vlib_node_graph (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 45 | vlib_node_main_t *nm = va_arg (*va, vlib_node_main_t *); |
| 46 | vlib_node_t *n = va_arg (*va, vlib_node_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | int i, j; |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 48 | u32 indent; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 49 | typedef struct |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u32 next_node; |
| 52 | u32 next_slot; |
| 53 | u32 prev_node; |
| 54 | } tmp_t; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 55 | tmp_t *tmps = 0; |
| 56 | tmp_t empty = {.next_node = ~0,.prev_node = ~0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | if (!n) |
| 59 | return format (s, "%=26s%=26s%=26s", "Name", "Next", "Previous"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
| 61 | s = format (s, "%-26v", n->name); |
| 62 | |
| 63 | indent = format_get_indent (s); |
| 64 | |
| 65 | for (i = j = 0; i < vec_len (n->next_nodes); i++) |
| 66 | { |
| 67 | if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX) |
| 68 | continue; |
| 69 | vec_validate_init_empty (tmps, j, empty); |
| 70 | tmps[j].next_node = n->next_nodes[i]; |
| 71 | tmps[j].next_slot = i; |
| 72 | j++; |
| 73 | } |
| 74 | |
| 75 | j = 0; |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 76 | clib_bitmap_foreach (i, n->prev_node_bitmap) { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | vec_validate_init_empty (tmps, j, empty); |
| 78 | tmps[j].prev_node = i; |
| 79 | j++; |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 80 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
| 82 | for (i = 0; i < vec_len (tmps); i++) |
| 83 | { |
| 84 | if (i > 0) |
| 85 | s = format (s, "\n%U", format_white_space, indent); |
| 86 | |
| 87 | if (tmps[i].next_node != ~0) |
| 88 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | vlib_node_t *x; |
| 90 | u8 *t = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | x = vec_elt (nm->nodes, tmps[i].next_node); |
| 93 | t = format (t, "%v [%d]", x->name, tmps[i].next_slot); |
| 94 | s = format (s, "%=26v", t); |
| 95 | vec_free (t); |
| 96 | } |
| 97 | else |
| 98 | s = format (s, "%26s", ""); |
| 99 | |
| 100 | if (tmps[i].prev_node != ~0) |
| 101 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | vlib_node_t *x; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | x = vec_elt (nm->nodes, tmps[i].prev_node); |
| 104 | s = format (s, "%=26v", x->name); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | vec_free (tmps); |
| 109 | |
| 110 | return s; |
| 111 | } |
| 112 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 113 | u8 * |
| 114 | format_vlib_node_and_next (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 116 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 117 | vlib_node_t *n = va_arg (*va, vlib_node_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | u32 next_index = va_arg (*va, u32); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 119 | vlib_node_t *n_next; |
| 120 | u32 *ni; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | ni = vec_elt_at_index (n->next_nodes, next_index); |
| 123 | n_next = vlib_get_node (vm, ni[0]); |
| 124 | return format (s, "%v -> %v", n->name, n_next->name); |
| 125 | } |
| 126 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 127 | u8 * |
| 128 | format_vlib_node_name (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 130 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | u32 node_index = va_arg (*va, u32); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 132 | vlib_node_t *n = vlib_get_node (vm, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
| 134 | return format (s, "%v", n->name); |
| 135 | } |
| 136 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | u8 * |
| 138 | format_vlib_next_node_name (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | u32 node_index = va_arg (*va, u32); |
| 142 | u32 next_index = va_arg (*va, u32); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 143 | vlib_node_t *next = vlib_get_next_node (vm, node_index, next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | return format (s, "%v", next->name); |
| 145 | } |
| 146 | |
| 147 | /* Parse node name -> node index. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | uword |
| 149 | unformat_vlib_node (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 151 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 152 | u32 *result = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
| 154 | return unformat_user (input, unformat_hash_vec_string, |
| 155 | vm->node_main.node_by_name, result); |
| 156 | } |
| 157 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 158 | u8 * |
| 159 | format_vlib_time (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | { |
| 161 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 162 | f64 time = va_arg (*va, f64); |
| 163 | return format (s, "%12.4f", time); |
| 164 | } |
| 165 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 166 | u8 * |
| 167 | format_vlib_cpu_time (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 169 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | u64 cpu_time = va_arg (*va, u64); |
| 171 | f64 dt; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 172 | |
| 173 | dt = |
| 174 | (cpu_time - |
| 175 | vm->clib_time.init_cpu_time) * vm->clib_time.seconds_per_clock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | return format (s, "%U", format_vlib_time, vm, dt); |
| 177 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | |
Damjan Marion | a31698b | 2021-03-10 14:35:28 +0100 | [diff] [blame] | 179 | uword |
| 180 | unformat_vlib_node_variant (unformat_input_t *input, va_list *args) |
| 181 | { |
| 182 | vlib_main_t *vm = vlib_get_main (); |
| 183 | u32 *march_variant = va_arg (*args, u32 *); |
| 184 | uword *p; |
| 185 | u8 *str = 0; |
| 186 | |
| 187 | if (unformat (input, "%s", &str) == 0) |
| 188 | return 0; |
| 189 | |
| 190 | p = hash_get (vm->node_main.node_fn_march_variant_by_suffix, str); |
| 191 | |
| 192 | vec_free (str); |
| 193 | |
| 194 | if (p) |
| 195 | *march_variant = p[0]; |
| 196 | |
| 197 | return p ? 1 : 0; |
| 198 | } |
| 199 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 200 | /* |
| 201 | * fd.io coding-style-patch-verification: ON |
| 202 | * |
| 203 | * Local Variables: |
| 204 | * eval: (c-set-style "gnu") |
| 205 | * End: |
| 206 | */ |