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> |
| 19 | |
| 20 | typedef enum |
| 21 | { |
| 22 | ERROR_DISPOSITION_DROP, |
| 23 | ERROR_DISPOSITION_PUNT, |
| 24 | ERROR_N_DISPOSITION, |
| 25 | } error_disposition_t; |
| 26 | |
| 27 | static u8 * |
| 28 | validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index) |
| 29 | { |
| 30 | uword node_index = vlib_error_get_node (e[0]); |
| 31 | uword code = vlib_error_get_code (e[0]); |
| 32 | vlib_node_t *n; |
| 33 | |
| 34 | if (node_index >= vec_len (vm->node_main.nodes)) |
| 35 | return format (0, "[%d], node index out of range 0x%x, error 0x%x", |
| 36 | index, node_index, e[0]); |
| 37 | |
| 38 | n = vlib_get_node (vm, node_index); |
| 39 | if (code >= n->n_errors) |
| 40 | return format (0, "[%d], code %d out of range for node %v", |
| 41 | index, code, n->name); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static u8 * |
| 47 | validate_error_frame (vlib_main_t * vm, |
| 48 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 49 | { |
| 50 | u32 *buffers = vlib_frame_vector_args (f); |
| 51 | vlib_buffer_t *b; |
| 52 | u8 *msg = 0; |
| 53 | uword i; |
| 54 | |
| 55 | for (i = 0; i < f->n_vectors; i++) |
| 56 | { |
| 57 | b = vlib_get_buffer (vm, buffers[i]); |
| 58 | msg = validate_error (vm, &b->error, i); |
| 59 | if (msg) |
| 60 | return msg; |
| 61 | } |
| 62 | |
| 63 | return msg; |
| 64 | } |
| 65 | |
| 66 | always_inline u32 |
| 67 | counter_index (vlib_main_t * vm, vlib_error_t e) |
| 68 | { |
| 69 | vlib_node_t *n; |
| 70 | u32 ci, ni; |
| 71 | |
| 72 | ni = vlib_error_get_node (e); |
| 73 | n = vlib_get_node (vm, ni); |
| 74 | |
| 75 | ci = vlib_error_get_code (e); |
| 76 | ASSERT (ci < n->n_errors); |
| 77 | |
| 78 | ci += n->error_heap_index; |
| 79 | |
| 80 | return ci; |
| 81 | } |
| 82 | |
| 83 | static u8 * |
| 84 | format_error_trace (u8 * s, va_list * va) |
| 85 | { |
| 86 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 87 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 88 | vlib_error_t *e = va_arg (*va, vlib_error_t *); |
| 89 | vlib_node_t *error_node; |
| 90 | vlib_error_main_t *em = &vm->error_main; |
| 91 | u32 i; |
| 92 | |
| 93 | error_node = vlib_get_node (vm, vlib_error_get_node (e[0])); |
| 94 | i = counter_index (vm, e[0]); |
| 95 | s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]); |
| 96 | |
| 97 | return s; |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | trace_errors (vlib_main_t * vm, |
| 102 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 103 | { |
| 104 | u32 n_left, *buffers; |
| 105 | |
| 106 | buffers = vlib_frame_vector_args (frame); |
| 107 | n_left = frame->n_vectors; |
| 108 | |
| 109 | while (n_left >= 4) |
| 110 | { |
| 111 | u32 bi0, bi1; |
| 112 | vlib_buffer_t *b0, *b1; |
| 113 | vlib_error_t *t0, *t1; |
| 114 | |
| 115 | /* Prefetch next iteration. */ |
| 116 | vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD); |
| 117 | vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD); |
| 118 | |
| 119 | bi0 = buffers[0]; |
| 120 | bi1 = buffers[1]; |
| 121 | |
| 122 | b0 = vlib_get_buffer (vm, bi0); |
| 123 | b1 = vlib_get_buffer (vm, bi1); |
| 124 | |
| 125 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 126 | { |
| 127 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 128 | t0[0] = b0->error; |
| 129 | } |
| 130 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 131 | { |
| 132 | t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); |
| 133 | t1[0] = b1->error; |
| 134 | } |
| 135 | buffers += 2; |
| 136 | n_left -= 2; |
| 137 | } |
| 138 | |
| 139 | while (n_left >= 1) |
| 140 | { |
| 141 | u32 bi0; |
| 142 | vlib_buffer_t *b0; |
| 143 | vlib_error_t *t0; |
| 144 | |
| 145 | bi0 = buffers[0]; |
| 146 | |
| 147 | b0 = vlib_get_buffer (vm, bi0); |
| 148 | |
| 149 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 150 | { |
| 151 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
| 152 | t0[0] = b0->error; |
| 153 | } |
| 154 | buffers += 1; |
| 155 | n_left -= 1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static_always_inline uword |
| 160 | process_drop_punt (vlib_main_t * vm, |
| 161 | vlib_node_runtime_t * node, |
| 162 | vlib_frame_t * frame, error_disposition_t disposition) |
| 163 | { |
| 164 | u32 errors[VLIB_FRAME_SIZE], *error, *from, n_left; |
| 165 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 166 | vlib_error_main_t *em = &vm->error_main; |
| 167 | |
| 168 | from = vlib_frame_vector_args (frame); |
| 169 | n_left = frame->n_vectors; |
| 170 | b = bufs; |
| 171 | error = errors; |
| 172 | |
| 173 | vlib_get_buffers (vm, from, bufs, n_left); |
| 174 | |
| 175 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 176 | trace_errors (vm, node, frame); |
| 177 | |
| 178 | /* collect the array of error first ... */ |
| 179 | while (n_left >= 4) |
| 180 | { |
| 181 | if (n_left >= 12) |
| 182 | { |
| 183 | /* Prefetch 8 ahead - there's not much going on in each iteration */ |
| 184 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 185 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 186 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 187 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 188 | } |
| 189 | error[0] = b[0]->error; |
| 190 | error[1] = b[1]->error; |
| 191 | error[2] = b[2]->error; |
| 192 | error[3] = b[3]->error; |
| 193 | |
| 194 | error += 4; |
| 195 | n_left -= 4; |
| 196 | b += 4; |
| 197 | } |
| 198 | while (n_left) |
| 199 | { |
| 200 | error[0] = b[0]->error; |
| 201 | |
| 202 | error += 1; |
| 203 | n_left -= 1; |
| 204 | b += 1; |
| 205 | } |
| 206 | |
| 207 | /* ... then count against them in blocks */ |
| 208 | n_left = frame->n_vectors; |
| 209 | |
| 210 | while (n_left) |
| 211 | { |
| 212 | u16 off, count; |
| 213 | u32 c_index; |
| 214 | |
| 215 | off = frame->n_vectors - n_left; |
| 216 | |
| 217 | error = errors + off; |
| 218 | |
| 219 | count = clib_count_equal_u32 (error, n_left); |
| 220 | n_left -= count; |
| 221 | |
| 222 | c_index = counter_index (vm, error[0]); |
| 223 | em->counters[c_index] += count; |
| 224 | |
| 225 | vlib_error_elog_count (vm, c_index, count); |
| 226 | } |
| 227 | |
| 228 | if (disposition == ERROR_DISPOSITION_DROP || !vm->os_punt_frame) |
| 229 | { |
| 230 | vlib_buffer_free (vm, from, frame->n_vectors); |
| 231 | |
| 232 | /* If there is no punt function, free the frame as well. */ |
| 233 | if (disposition == ERROR_DISPOSITION_PUNT && !vm->os_punt_frame) |
| 234 | vlib_frame_free (vm, node, frame); |
| 235 | } |
| 236 | else |
| 237 | vm->os_punt_frame (vm, node, frame); |
| 238 | |
| 239 | return frame->n_vectors; |
| 240 | } |
| 241 | |
| 242 | VLIB_NODE_FN (error_drop_node) (vlib_main_t * vm, |
| 243 | vlib_node_runtime_t * node, |
| 244 | vlib_frame_t * frame) |
| 245 | { |
| 246 | return process_drop_punt (vm, node, frame, ERROR_DISPOSITION_DROP); |
| 247 | } |
| 248 | |
| 249 | VLIB_NODE_FN (error_punt_node) (vlib_main_t * vm, |
| 250 | vlib_node_runtime_t * node, |
| 251 | vlib_frame_t * frame) |
| 252 | { |
| 253 | return process_drop_punt (vm, node, frame, ERROR_DISPOSITION_PUNT); |
| 254 | } |
| 255 | |
| 256 | /* *INDENT-OFF* */ |
| 257 | VLIB_REGISTER_NODE (error_drop_node) = { |
| 258 | .name = "drop", |
| 259 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 260 | .vector_size = sizeof (u32), |
| 261 | .format_trace = format_error_trace, |
| 262 | .validate_frame = validate_error_frame, |
| 263 | }; |
| 264 | /* *INDENT-ON* */ |
| 265 | |
| 266 | /* *INDENT-OFF* */ |
| 267 | VLIB_REGISTER_NODE (error_punt_node) = { |
| 268 | .name = "punt", |
| 269 | .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH |
| 270 | | VLIB_NODE_FLAG_IS_PUNT), |
| 271 | .vector_size = sizeof (u32), |
| 272 | .format_trace = format_error_trace, |
| 273 | .validate_frame = validate_error_frame, |
| 274 | }; |
| 275 | /* *INDENT-ON* */ |
| 276 | |
| 277 | /* |
| 278 | * fd.io coding-style-patch-verification: ON |
| 279 | * |
| 280 | * Local Variables: |
| 281 | * eval: (c-set-style "gnu") |
| 282 | * End: |
| 283 | */ |