blob: 3fda1d9b3b6936a197a606b91b2a7c1ebd2256dd [file] [log] [blame]
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001/*
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 Marion4c276f02021-11-06 13:17:31 +010019#include <vppinfra/vector/count_equal.h>
Neale Ranns22e1f1d2019-03-01 15:53:11 +000020
21typedef enum
22{
23 ERROR_DISPOSITION_DROP,
24 ERROR_DISPOSITION_PUNT,
25 ERROR_N_DISPOSITION,
26} error_disposition_t;
27
28static u8 *
29validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
30{
Dave Barach687c9022019-07-23 10:22:31 -040031 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 Ranns22e1f1d2019-03-01 15:53:11 +000033 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
47static u8 *
48validate_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
67always_inline u32
68counter_index (vlib_main_t * vm, vlib_error_t e)
69{
70 vlib_node_t *n;
71 u32 ci, ni;
72
Dave Barach687c9022019-07-23 10:22:31 -040073 ni = vlib_error_get_node (&vm->node_main, e);
Neale Ranns22e1f1d2019-03-01 15:53:11 +000074 n = vlib_get_node (vm, ni);
75
Dave Barach687c9022019-07-23 10:22:31 -040076 ci = vlib_error_get_code (&vm->node_main, e);
Pim van Pelt96158832022-06-03 21:05:26 +000077 if (ci >= n->n_errors)
78 return CLIB_U32_MAX;
Neale Ranns22e1f1d2019-03-01 15:53:11 +000079
80 ci += n->error_heap_index;
81
82 return ci;
83}
84
85static u8 *
86format_error_trace (u8 * s, va_list * va)
87{
88 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
89 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
90 vlib_error_t *e = va_arg (*va, vlib_error_t *);
91 vlib_node_t *error_node;
92 vlib_error_main_t *em = &vm->error_main;
93 u32 i;
94
Dave Barach687c9022019-07-23 10:22:31 -040095 error_node = vlib_get_node (vm, vlib_error_get_node (&vm->node_main, e[0]));
lijinhuieb2a4182024-02-22 11:16:10 +080096 i = counter_index (vm, vlib_error_get_code (&vm->node_main, e[0]));
Pim van Pelt96158832022-06-03 21:05:26 +000097 if (i != CLIB_U32_MAX)
lijinhuieb2a4182024-02-22 11:16:10 +080098 {
99 i += error_node->error_heap_index;
100 s = format (s, "%v: %s", error_node->name, em->counters_heap[i].desc);
101 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000102
103 return s;
104}
105
106static void
107trace_errors (vlib_main_t * vm,
108 vlib_node_runtime_t * node, vlib_frame_t * frame)
109{
110 u32 n_left, *buffers;
111
112 buffers = vlib_frame_vector_args (frame);
113 n_left = frame->n_vectors;
114
115 while (n_left >= 4)
116 {
117 u32 bi0, bi1;
118 vlib_buffer_t *b0, *b1;
119 vlib_error_t *t0, *t1;
120
121 /* Prefetch next iteration. */
122 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
123 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
124
125 bi0 = buffers[0];
126 bi1 = buffers[1];
127
128 b0 = vlib_get_buffer (vm, bi0);
129 b1 = vlib_get_buffer (vm, bi1);
130
131 if (b0->flags & VLIB_BUFFER_IS_TRACED)
132 {
133 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
134 t0[0] = b0->error;
135 }
136 if (b1->flags & VLIB_BUFFER_IS_TRACED)
137 {
138 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
139 t1[0] = b1->error;
140 }
141 buffers += 2;
142 n_left -= 2;
143 }
144
145 while (n_left >= 1)
146 {
147 u32 bi0;
148 vlib_buffer_t *b0;
149 vlib_error_t *t0;
150
151 bi0 = buffers[0];
152
153 b0 = vlib_get_buffer (vm, bi0);
154
155 if (b0->flags & VLIB_BUFFER_IS_TRACED)
156 {
157 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
158 t0[0] = b0->error;
159 }
160 buffers += 1;
161 n_left -= 1;
162 }
163}
164
165static_always_inline uword
166process_drop_punt (vlib_main_t * vm,
167 vlib_node_runtime_t * node,
168 vlib_frame_t * frame, error_disposition_t disposition)
169{
170 u32 errors[VLIB_FRAME_SIZE], *error, *from, n_left;
171 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
172 vlib_error_main_t *em = &vm->error_main;
173
174 from = vlib_frame_vector_args (frame);
175 n_left = frame->n_vectors;
176 b = bufs;
177 error = errors;
178
179 vlib_get_buffers (vm, from, bufs, n_left);
180
181 if (node->flags & VLIB_NODE_FLAG_TRACE)
182 trace_errors (vm, node, frame);
183
184 /* collect the array of error first ... */
185 while (n_left >= 4)
186 {
187 if (n_left >= 12)
188 {
189 /* Prefetch 8 ahead - there's not much going on in each iteration */
190 vlib_prefetch_buffer_header (b[4], LOAD);
191 vlib_prefetch_buffer_header (b[5], LOAD);
192 vlib_prefetch_buffer_header (b[6], LOAD);
193 vlib_prefetch_buffer_header (b[7], LOAD);
194 }
195 error[0] = b[0]->error;
196 error[1] = b[1]->error;
197 error[2] = b[2]->error;
198 error[3] = b[3]->error;
199
200 error += 4;
201 n_left -= 4;
202 b += 4;
203 }
204 while (n_left)
205 {
206 error[0] = b[0]->error;
207
208 error += 1;
209 n_left -= 1;
210 b += 1;
211 }
212
213 /* ... then count against them in blocks */
214 n_left = frame->n_vectors;
215
216 while (n_left)
217 {
218 u16 off, count;
219 u32 c_index;
220
221 off = frame->n_vectors - n_left;
222
223 error = errors + off;
224
225 count = clib_count_equal_u32 (error, n_left);
226 n_left -= count;
227
228 c_index = counter_index (vm, error[0]);
Pim van Pelt96158832022-06-03 21:05:26 +0000229 if (c_index != CLIB_U32_MAX)
230 em->counters[c_index] += count;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000231
232 vlib_error_elog_count (vm, c_index, count);
233 }
234
235 if (disposition == ERROR_DISPOSITION_DROP || !vm->os_punt_frame)
236 {
237 vlib_buffer_free (vm, from, frame->n_vectors);
238
239 /* If there is no punt function, free the frame as well. */
240 if (disposition == ERROR_DISPOSITION_PUNT && !vm->os_punt_frame)
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000241 vlib_frame_free (vm, frame);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000242 }
243 else
244 vm->os_punt_frame (vm, node, frame);
245
246 return frame->n_vectors;
247}
248
249VLIB_NODE_FN (error_drop_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_DROP);
254}
255
256VLIB_NODE_FN (error_punt_node) (vlib_main_t * vm,
257 vlib_node_runtime_t * node,
258 vlib_frame_t * frame)
259{
260 return process_drop_punt (vm, node, frame, ERROR_DISPOSITION_PUNT);
261}
262
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000263VLIB_REGISTER_NODE (error_drop_node) = {
264 .name = "drop",
265 .flags = VLIB_NODE_FLAG_IS_DROP,
266 .vector_size = sizeof (u32),
267 .format_trace = format_error_trace,
268 .validate_frame = validate_error_frame,
269};
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000270
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000271VLIB_REGISTER_NODE (error_punt_node) = {
272 .name = "punt",
273 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
274 | VLIB_NODE_FLAG_IS_PUNT),
275 .vector_size = sizeof (u32),
276 .format_trace = format_error_trace,
277 .validate_frame = validate_error_frame,
278};
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000279
280/*
281 * fd.io coding-style-patch-verification: ON
282 *
283 * Local Variables:
284 * eval: (c-set-style "gnu")
285 * End:
286 */