Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * drop.c - Punt and drop nodes |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
Damjan Marion | 4c276f0 | 2021-11-06 13:17:31 +0100 | [diff] [blame] | 19 | #include <vppinfra/vector/count_equal.h> |
Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 20 | |
| 21 | typedef enum |
| 22 | { |
| 23 | ERROR_DISPOSITION_DROP, |
| 24 | ERROR_DISPOSITION_PUNT, |
| 25 | ERROR_N_DISPOSITION, |
| 26 | } error_disposition_t; |
| 27 | |
| 28 | static u8 * |
| 29 | validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index) |
| 30 | { |
Dave Barach | 687c902 | 2019-07-23 10:22:31 -0400 | [diff] [blame] | 31 | uword node_index = vlib_error_get_node (&vm->node_main, e[0]); |
| 32 | uword code = vlib_error_get_code (&vm->node_main, e[0]); |
Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 33 | vlib_node_t *n; |
| 34 | |
| 35 | if (node_index >= vec_len (vm->node_main.nodes)) |
| 36 | return format (0, "[%d], node index out of range 0x%x, error 0x%x", |
| 37 | index, node_index, e[0]); |
| 38 | |
| 39 | n = vlib_get_node (vm, node_index); |
| 40 | if (code >= n->n_errors) |
| 41 | return format (0, "[%d], code %d out of range for node %v", |
| 42 | index, code, n->name); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static u8 * |
| 48 | validate_error_frame (vlib_main_t * vm, |
| 49 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 50 | { |
| 51 | u32 *buffers = vlib_frame_vector_args (f); |
| 52 | vlib_buffer_t *b; |
| 53 | u8 *msg = 0; |
| 54 | uword i; |
| 55 | |
| 56 | for (i = 0; i < f->n_vectors; i++) |
| 57 | { |
| 58 | b = vlib_get_buffer (vm, buffers[i]); |
| 59 | msg = validate_error (vm, &b->error, i); |
| 60 | if (msg) |
| 61 | return msg; |
| 62 | } |
| 63 | |
| 64 | return msg; |
| 65 | } |
| 66 | |
| 67 | always_inline u32 |
| 68 | counter_index (vlib_main_t * vm, vlib_error_t e) |
| 69 | { |
| 70 | vlib_node_t *n; |
| 71 | u32 ci, ni; |
| 72 | |
Dave Barach | 687c902 | 2019-07-23 10:22:31 -0400 | [diff] [blame] | 73 | ni = vlib_error_get_node (&vm->node_main, e); |
Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 74 | n = vlib_get_node (vm, ni); |
| 75 | |
Dave Barach | 687c902 | 2019-07-23 10:22:31 -0400 | [diff] [blame] | 76 | ci = vlib_error_get_code (&vm->node_main, e); |
Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 77 | ASSERT (ci < n->n_errors); |
| 78 | |
| 79 | ci += n->error_heap_index; |
| 80 | |
| 81 | return ci; |
| 82 | } |
| 83 | |
| 84 | static u8 * |
| 85 | format_error_trace (u8 * s, va_list * va) |
| 86 | { |
| 87 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 88 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 89 | vlib_error_t *e = va_arg (*va, vlib_error_t *); |
| 90 | vlib_node_t *error_node; |
| 91 | vlib_error_main_t *em = &vm->error_main; |
| 92 | u32 i; |
| 93 | |
Dave Barach | 687c902 | 2019-07-23 10:22:31 -0400 | [diff] [blame] | 94 | error_node = vlib_get_node (vm, vlib_error_get_node (&vm->node_main, e[0])); |
Dave Barach | baba24e | 2019-07-31 09:05:37 -0400 | [diff] [blame] | 95 | i = counter_index (vm, vlib_error_get_code (&vm->node_main, e[0])) + |
| 96 | error_node->error_heap_index; |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 97 | s = format (s, "%v: %s", error_node->name, em->counters_heap[i].name); |
Neale Ranns | 22e1f1d | 2019-03-01 15:53:11 +0000 | [diff] [blame] | 98 | |
| 99 | return s; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | trace_errors (vlib_main_t * vm, |
| 104 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 105 | { |
| 106 | u32 n_left, *buffers; |
| 107 | |
| 108 | buffers = vlib_frame_vector_args (frame); |
| 109 | n_left = frame->n_vectors; |
| 110 | |
| 111 | while (n_left >= 4) |
| 112 | { |
| 113 | u32 bi0, bi1; |
| 114 | vlib_buffer_t *b0, *b1; |
| 115 | vlib_error_t *t0, *t1; |
| 116 | |
| 117 | /* Prefetch next iteration. */ |
| 118 | vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD); |
| 119 | vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD); |
| 120 | |
| 121 | bi0 = buffers[0]; |
| 122 | bi1 = buffers[1]; |
| 123 | |
| 124 | b0 = vlib_get_buffer (vm, bi0); |
| 125 | b1 = vlib_get_buffer (vm, bi1); |
| 126 | |
| 127 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 128 | { |
| 129 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 130 | t0[0] = b0->error; |
| 131 | } |
| 132 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 133 | { |
| 134 | t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); |
| 135 | t1[0] = b1->error; |
| 136 | } |
| 137 | buffers += 2; |
| 138 | n_left -= 2; |
| 139 | } |
| 140 | |
| 141 | while (n_left >= 1) |
| 142 | { |
| 143 | u32 bi0; |
| 144 | vlib_buffer_t *b0; |
| 145 | vlib_error_t *t0; |
| 146 | |
| 147 | bi0 = buffers[0]; |
| 148 | |
| 149 | b0 = vlib_get_buffer (vm, bi0); |
| 150 | |
| 151 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 152 | { |
| 153 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 154 | t0[0] = b0->error; |
| 155 | } |
| 156 | buffers += 1; |
| 157 | n_left -= 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static_always_inline uword |
| 162 | process_drop_punt (vlib_main_t * vm, |
| 163 | vlib_node_runtime_t * node, |
| 164 | vlib_frame_t * frame, error_disposition_t disposition) |
| 165 | { |
| 166 | u32 errors[VLIB_FRAME_SIZE], *error, *from, n_left; |
| 167 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 168 | vlib_error_main_t *em = &vm->error_main; |
| 169 | |
| 170 | from = vlib_frame_vector_args (frame); |
| 171 | n_left = frame->n_vectors; |
| 172 | b = bufs; |
| 173 | error = errors; |
| 174 | |
| 175 | vlib_get_buffers (vm, from, bufs, n_left); |
| 176 | |
| 177 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 178 | trace_errors (vm, node, frame); |
| 179 | |
| 180 | /* collect the array of error first ... */ |
| 181 | while (n_left >= 4) |
| 182 | { |
| 183 | if (n_left >= 12) |
| 184 | { |
| 185 | /* Prefetch 8 ahead - there's not much going on in each iteration */ |
| 186 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 187 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 188 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 189 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 190 | } |
| 191 | error[0] = b[0]->error; |
| 192 | error[1] = b[1]->error; |
| 193 | error[2] = b[2]->error; |
| 194 | error[3] = b[3]->error; |
| 195 | |
| 196 | error += 4; |
| 197 | n_left -= 4; |
| 198 | b += 4; |
| 199 | } |
| 200 | while (n_left) |
| 201 | { |
| 202 | error[0] = b[0]->error; |
| 203 | |
| 204 | error += 1; |
| 205 | n_left -= 1; |
| 206 | b += 1; |
| 207 | } |
| 208 | |
| 209 | /* ... then count against them in blocks */ |
| 210 | n_left = frame->n_vectors; |
| 211 | |
| 212 | while (n_left) |
| 213 | { |
| 214 | u16 off, count; |
| 215 | u32 c_index; |
| 216 | |
| 217 | off = frame->n_vectors - n_left; |
| 218 | |
| 219 | error = errors + off; |
| 220 | |
| 221 | count = clib_count_equal_u32 (error, n_left); |
| 222 | n_left -= count; |
| 223 | |
| 224 | c_index = counter_index (vm, error[0]); |
| 225 | em->counters[c_index] += count; |
| 226 | |
| 227 | vlib_error_elog_count (vm, c_index, count); |
| 228 | } |
| 229 | |
| 230 | if (disposition == ERROR_DISPOSITION_DROP || !vm->os_punt_frame) |
| 231 | { |
| 232 | vlib_buffer_free (vm, from, frame->n_vectors); |
| 233 | |
| 234 | /* If there is no punt function, free the frame as well. */ |
| 235 | if (disposition == ERROR_DISPOSITION_PUNT && !vm->os_punt_frame) |
| 236 | vlib_frame_free (vm, node, frame); |
| 237 | } |
| 238 | else |
| 239 | vm->os_punt_frame (vm, node, frame); |
| 240 | |
| 241 | return frame->n_vectors; |
| 242 | } |
| 243 | |
| 244 | VLIB_NODE_FN (error_drop_node) (vlib_main_t * vm, |
| 245 | vlib_node_runtime_t * node, |
| 246 | vlib_frame_t * frame) |
| 247 | { |
| 248 | return process_drop_punt (vm, node, frame, ERROR_DISPOSITION_DROP); |
| 249 | } |
| 250 | |
| 251 | VLIB_NODE_FN (error_punt_node) (vlib_main_t * vm, |
| 252 | vlib_node_runtime_t * node, |
| 253 | vlib_frame_t * frame) |
| 254 | { |
| 255 | return process_drop_punt (vm, node, frame, ERROR_DISPOSITION_PUNT); |
| 256 | } |
| 257 | |
| 258 | /* *INDENT-OFF* */ |
| 259 | VLIB_REGISTER_NODE (error_drop_node) = { |
| 260 | .name = "drop", |
| 261 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 262 | .vector_size = sizeof (u32), |
| 263 | .format_trace = format_error_trace, |
| 264 | .validate_frame = validate_error_frame, |
| 265 | }; |
| 266 | /* *INDENT-ON* */ |
| 267 | |
| 268 | /* *INDENT-OFF* */ |
| 269 | VLIB_REGISTER_NODE (error_punt_node) = { |
| 270 | .name = "punt", |
| 271 | .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH |
| 272 | | VLIB_NODE_FLAG_IS_PUNT), |
| 273 | .vector_size = sizeof (u32), |
| 274 | .format_trace = format_error_trace, |
| 275 | .validate_frame = validate_error_frame, |
| 276 | }; |
| 277 | /* *INDENT-ON* */ |
| 278 | |
| 279 | /* |
| 280 | * fd.io coding-style-patch-verification: ON |
| 281 | * |
| 282 | * Local Variables: |
| 283 | * eval: (c-set-style "gnu") |
| 284 | * End: |
| 285 | */ |