blob: fb322604edac07bff037f11382a56b20b6f99312 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 * interface_output.c: interface output node
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 <vnet/vnet.h>
Dave Barach2c0a4f42017-06-29 09:30:15 -040041#include <vnet/ip/icmp46_packet.h>
Dave Barach4330c462020-06-10 17:07:32 -040042#include <vnet/ethernet/packet.h>
Florin Corasb040f982020-10-20 14:59:43 -070043#include <vnet/ip/format.h>
Dave Barach2c0a4f42017-06-29 09:30:15 -040044#include <vnet/ip/ip4.h>
45#include <vnet/ip/ip6.h>
46#include <vnet/udp/udp_packet.h>
Damjan Marion152e21d2016-11-29 14:55:43 +010047#include <vnet/feature/feature.h>
Dave Barach9137e542019-09-13 17:47:50 -040048#include <vnet/classify/trace_classify.h>
Dave Barach1bd2c012020-04-12 08:31:39 -040049#include <vnet/interface_output.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Dave Barachba868bb2016-08-08 09:51:21 -040051typedef struct
52{
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 u32 sw_if_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020054 u32 flags;
Mohsin Kazmi29467b52019-10-08 19:42:38 +020055 u8 data[128 - 2 * sizeof (u32)];
Dave Barachba868bb2016-08-08 09:51:21 -040056}
57interface_output_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
Filip Tehlar62668772019-03-04 03:33:32 -080059#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -040060u8 *
61format_vnet_interface_output_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040064 vlib_node_t *node = va_arg (*va, vlib_node_t *);
65 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
66 vnet_main_t *vnm = vnet_get_main ();
67 vnet_sw_interface_t *si;
Christophe Fontained3c008d2017-10-02 18:10:54 +020068 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
Dave Barachba868bb2016-08-08 09:51:21 -040070 if (t->sw_if_index != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 indent = format_get_indent (s);
Dave Barachba868bb2016-08-08 09:51:21 -040073
Neale Ranns177bbdc2016-11-15 09:46:51 +000074 if (pool_is_free_index
75 (vnm->interface_main.sw_interfaces, t->sw_if_index))
76 {
77 /* the interface may have been deleted by the time the trace is printed */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020078 s = format (s, "sw_if_index: %d ", t->sw_if_index);
Neale Ranns177bbdc2016-11-15 09:46:51 +000079 }
80 else
81 {
82 si = vnet_get_sw_interface (vnm, t->sw_if_index);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020083 s =
84 format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
85 t->flags);
Neale Ranns177bbdc2016-11-15 09:46:51 +000086 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020087 s =
88 format (s, "\n%U%U", format_white_space, indent,
89 node->format_buffer ? node->format_buffer : format_hex_bytes,
90 t->data, sizeof (t->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 }
92 return s;
93}
BenoƮt Ganne7d2094c2020-11-09 15:23:52 +010094#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96static void
97vnet_interface_output_trace (vlib_main_t * vm,
98 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -040099 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Dave Barachba868bb2016-08-08 09:51:21 -0400101 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100104 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400105
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 while (n_left >= 4)
107 {
108 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400109 vlib_buffer_t *b0, *b1;
110 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
112 /* Prefetch next iteration. */
113 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
114 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
115
116 bi0 = from[0];
117 bi1 = from[1];
118
119 b0 = vlib_get_buffer (vm, bi0);
120 b1 = vlib_get_buffer (vm, bi1);
121
122 if (b0->flags & VLIB_BUFFER_IS_TRACED)
123 {
124 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
125 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200126 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500127 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
128 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 }
130 if (b1->flags & VLIB_BUFFER_IS_TRACED)
131 {
132 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
133 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200134 t1->flags = b1->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500135 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
136 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 }
138 from += 2;
139 n_left -= 2;
140 }
141
142 while (n_left >= 1)
143 {
144 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400145 vlib_buffer_t *b0;
146 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
148 bi0 = from[0];
149
150 b0 = vlib_get_buffer (vm, bi0);
151
152 if (b0->flags & VLIB_BUFFER_IS_TRACED)
153 {
154 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
155 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200156 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500157 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
158 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 }
160 from += 1;
161 n_left -= 1;
162 }
163}
164
Damjan Marion4d2726e2021-03-23 21:05:18 +0100165static_always_inline void
166vnet_interface_output_handle_offload (vlib_main_t *vm, vlib_buffer_t *b)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400167{
Damjan Marion4d2726e2021-03-23 21:05:18 +0100168 if (b->flags & VNET_BUFFER_F_OFFLOAD)
169 vnet_calc_checksums_inline (vm, b, b->flags & VNET_BUFFER_F_IS_IP4,
170 b->flags & VNET_BUFFER_F_IS_IP6);
171}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
Damjan Marion4d2726e2021-03-23 21:05:18 +0100173static_always_inline uword
174vnet_interface_output_node_inline (vlib_main_t *vm, u32 sw_if_index,
175 vlib_combined_counter_main_t *ccm,
176 vlib_buffer_t **b, u32 config_index, u8 arc,
Damjan Mariond4008cf2021-03-24 11:45:51 +0100177 u32 n_left, int do_tx_offloads,
178 int arc_or_subif)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100179{
180 u32 n_bytes = 0;
181 u32 n_bytes0, n_bytes1, n_bytes2, n_bytes3;
182 u32 ti = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Damjan Marion4d2726e2021-03-23 21:05:18 +0100184 while (n_left >= 8)
Damjan Marion152e21d2016-11-29 14:55:43 +0100185 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100186 u32 or_flags;
Damjan Marion152e21d2016-11-29 14:55:43 +0100187
Damjan Marion4d2726e2021-03-23 21:05:18 +0100188 /* Prefetch next iteration. */
189 vlib_prefetch_buffer_header (b[4], LOAD);
190 vlib_prefetch_buffer_header (b[5], LOAD);
191 vlib_prefetch_buffer_header (b[6], LOAD);
192 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Damjan Mariond4008cf2021-03-24 11:45:51 +0100194 if (do_tx_offloads)
195 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100196
197 /* Be grumpy about zero length buffers for benefit of
198 driver tx function. */
199 ASSERT (b[0]->current_length > 0);
200 ASSERT (b[1]->current_length > 0);
201 ASSERT (b[2]->current_length > 0);
202 ASSERT (b[3]->current_length > 0);
203
204 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
205 n_bytes += n_bytes1 = vlib_buffer_length_in_chain (vm, b[1]);
206 n_bytes += n_bytes2 = vlib_buffer_length_in_chain (vm, b[2]);
207 n_bytes += n_bytes3 = vlib_buffer_length_in_chain (vm, b[3]);
208
Damjan Mariond4008cf2021-03-24 11:45:51 +0100209 if (arc_or_subif)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100211 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
212 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
213 tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
214 tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
215 tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
216
217 /* update vlan subif tx counts, if required */
218 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
219 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
220
221 if (PREDICT_FALSE (tx_swif1 != sw_if_index))
222 vlib_increment_combined_counter (ccm, ti, tx_swif1, 1, n_bytes1);
223
224 if (PREDICT_FALSE (tx_swif2 != sw_if_index))
225 vlib_increment_combined_counter (ccm, ti, tx_swif2, 1, n_bytes2);
226
227 if (PREDICT_FALSE (tx_swif3 != sw_if_index))
228 vlib_increment_combined_counter (ccm, ti, tx_swif3, 1, n_bytes3);
229
230 if (PREDICT_FALSE (config_index != ~0))
231 {
232 vnet_buffer (b[0])->feature_arc_index = arc;
233 b[0]->current_config_index = config_index;
234 vnet_buffer (b[1])->feature_arc_index = arc;
235 b[1]->current_config_index = config_index;
236 vnet_buffer (b[2])->feature_arc_index = arc;
237 b[2]->current_config_index = config_index;
238 vnet_buffer (b[3])->feature_arc_index = arc;
239 b[3]->current_config_index = config_index;
240 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 }
242
Damjan Marion4d2726e2021-03-23 21:05:18 +0100243 if (do_tx_offloads && (or_flags & VNET_BUFFER_F_OFFLOAD))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100245 vnet_interface_output_handle_offload (vm, b[0]);
246 vnet_interface_output_handle_offload (vm, b[1]);
247 vnet_interface_output_handle_offload (vm, b[2]);
248 vnet_interface_output_handle_offload (vm, b[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 }
250
Damjan Marion4d2726e2021-03-23 21:05:18 +0100251 n_left -= 4;
252 b += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 }
254
Damjan Marion4d2726e2021-03-23 21:05:18 +0100255 while (n_left)
256 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100257 /* Be grumpy about zero length buffers for benefit of
258 driver tx function. */
259 ASSERT (b[0]->current_length > 0);
260
261 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100262
Damjan Mariond4008cf2021-03-24 11:45:51 +0100263 if (arc_or_subif)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100264 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100265 u32 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100266
Damjan Mariond4008cf2021-03-24 11:45:51 +0100267 if (PREDICT_FALSE (config_index != ~0))
268 {
269 vnet_buffer (b[0])->feature_arc_index = arc;
270 b[0]->current_config_index = config_index;
271 }
272
273 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
274 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
275 }
Damjan Marion4d2726e2021-03-23 21:05:18 +0100276
277 if (do_tx_offloads)
278 vnet_interface_output_handle_offload (vm, b[0]);
279
280 n_left -= 1;
281 b += 1;
282 }
283
284 return n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285}
286
Dave Barach5ecd5a52019-02-25 15:27:28 -0500287static_always_inline void vnet_interface_pcap_tx_trace
288 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
289 int sw_if_index_from_buffer)
290{
Damjan Marion8fb5add2021-03-04 18:41:59 +0100291 vnet_main_t *vnm = vnet_get_main ();
Dave Barach5ecd5a52019-02-25 15:27:28 -0500292 u32 n_left_from, *from;
293 u32 sw_if_index;
Damjan Marion8fb5add2021-03-04 18:41:59 +0100294 vnet_pcap_t *pp = &vnm->pcap;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500295
Dave Barach33909772019-09-23 10:27:27 -0400296 if (PREDICT_TRUE (pp->pcap_tx_enable == 0))
Dave Barach5ecd5a52019-02-25 15:27:28 -0500297 return;
298
299 if (sw_if_index_from_buffer == 0)
300 {
301 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
302 sw_if_index = rt->sw_if_index;
303 }
304 else
305 sw_if_index = ~0;
306
307 n_left_from = frame->n_vectors;
308 from = vlib_frame_vector_args (frame);
309
310 while (n_left_from > 0)
311 {
Dave Barach9137e542019-09-13 17:47:50 -0400312 int classify_filter_result;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500313 u32 bi0 = from[0];
314 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Dave Barach9137e542019-09-13 17:47:50 -0400315 from++;
316 n_left_from--;
317
Dave Barachf5667c32019-09-25 11:27:46 -0400318 if (pp->filter_classify_table_index != ~0)
Dave Barach9137e542019-09-13 17:47:50 -0400319 {
320 classify_filter_result =
321 vnet_is_packet_traced_inline
Dave Barachf5667c32019-09-25 11:27:46 -0400322 (b0, pp->filter_classify_table_index, 0 /* full classify */ );
Dave Barach9137e542019-09-13 17:47:50 -0400323 if (classify_filter_result)
Dave Barach33909772019-09-23 10:27:27 -0400324 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barach9137e542019-09-13 17:47:50 -0400325 continue;
326 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500327
328 if (sw_if_index_from_buffer)
329 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
330
Dave Barach33909772019-09-23 10:27:27 -0400331 if (pp->pcap_sw_if_index == 0 || pp->pcap_sw_if_index == sw_if_index)
Dave Barachd28437c2019-11-20 09:28:31 -0500332 {
333 vnet_main_t *vnm = vnet_get_main ();
334 vnet_hw_interface_t *hi =
335 vnet_get_sup_hw_interface (vnm, sw_if_index);
336 /* Capture pkt if not filtered, or if filter hits */
337 if (hi->trace_classify_table_index == ~0 ||
338 vnet_is_packet_traced_inline
339 (b0, hi->trace_classify_table_index, 0 /* full classify */ ))
340 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
341 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500342 }
343}
344
Damjan Marionf91098e2021-03-09 16:28:15 +0100345VLIB_NODE_FN (vnet_interface_output_node)
346(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400347{
348 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion4d2726e2021-03-23 21:05:18 +0100349 vnet_interface_main_t *im = &vnm->interface_main;
350 vlib_combined_counter_main_t *ccm;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400351 vnet_hw_interface_t *hi;
Damjan Marion3853b262021-03-23 18:47:34 +0100352 vnet_sw_interface_t *si;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400353 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
Damjan Marion3853b262021-03-23 18:47:34 +0100354 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100355 u32 n_bytes, n_buffers = frame->n_vectors;
356 u32 config_index = ~0;
357 u32 sw_if_index = rt->sw_if_index;
358 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
359 u32 ti = vm->thread_index;
360 u8 arc = im->output_feature_arc_index;
Damjan Mariond4008cf2021-03-24 11:45:51 +0100361 int arc_or_subif = 0;
362 int do_tx_offloads = 0;
Damjan Marion3853b262021-03-23 18:47:34 +0100363 u32 *from;
364
365 if (node->flags & VLIB_NODE_FLAG_TRACE)
366 vnet_interface_output_trace (vm, node, frame, n_buffers);
367
368 from = vlib_frame_vector_args (frame);
369
370 if (rt->is_deleted)
371 return vlib_error_drop_buffers (
372 vm, node, from,
373 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
374 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400375
Dave Barach5ecd5a52019-02-25 15:27:28 -0500376 vnet_interface_pcap_tx_trace (vm, node, frame,
377 0 /* sw_if_index_from_buffer */ );
378
Damjan Marion3853b262021-03-23 18:47:34 +0100379 vlib_get_buffers (vm, from, bufs, n_buffers);
380
Damjan Marion4d2726e2021-03-23 21:05:18 +0100381 si = vnet_get_sw_interface (vnm, sw_if_index);
382 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Damjan Marion3853b262021-03-23 18:47:34 +0100383
384 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
385 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
386 {
387 vlib_simple_counter_main_t *cm;
388
389 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
390 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100391 vlib_increment_simple_counter (cm, ti, sw_if_index, n_buffers);
Damjan Marion3853b262021-03-23 18:47:34 +0100392
393 return vlib_error_drop_buffers (
394 vm, node, from,
395 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
396 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
397 }
398
Damjan Marion4d2726e2021-03-23 21:05:18 +0100399 /* interface-output feature arc handling */
400 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
401 {
402 vnet_feature_config_main_t *fcm;
403 fcm = vnet_feature_get_config_main (arc);
404 config_index = vnet_get_feature_config_index (arc, sw_if_index);
405 vnet_get_config_data (&fcm->config_main, &config_index, &next_index, 0);
Damjan Mariond4008cf2021-03-24 11:45:51 +0100406 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100407 }
Damjan Mariond4008cf2021-03-24 11:45:51 +0100408 else if (hash_elts (hi->sub_interface_sw_if_index_by_id))
409 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100410
411 ccm = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX;
412
Damjan Mariond4008cf2021-03-24 11:45:51 +0100413 if ((hi->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TX_CKSUM) == 0)
414 do_tx_offloads = 1;
415
416 if (do_tx_offloads == 0 && arc_or_subif == 0)
417 n_bytes = vnet_interface_output_node_inline (
418 vm, sw_if_index, ccm, bufs, config_index, arc, n_buffers, 0, 0);
Damjan Marion4f81d442021-03-30 14:34:44 +0200419 else if (do_tx_offloads == 0 && arc_or_subif == 1)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100420 n_bytes = vnet_interface_output_node_inline (
421 vm, sw_if_index, ccm, bufs, config_index, arc, n_buffers, 0, 1);
Damjan Marion4f81d442021-03-30 14:34:44 +0200422 else if (do_tx_offloads == 1 && arc_or_subif == 0)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100423 n_bytes = vnet_interface_output_node_inline (
424 vm, sw_if_index, ccm, bufs, config_index, arc, n_buffers, 1, 0);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400425 else
Damjan Mariond4008cf2021-03-24 11:45:51 +0100426 n_bytes = vnet_interface_output_node_inline (
427 vm, sw_if_index, ccm, bufs, config_index, arc, n_buffers, 1, 1);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100428
429 vlib_buffer_enqueue_to_single_next (vm, node, vlib_frame_vector_args (frame),
430 next_index, frame->n_vectors);
431
432 /* Update main interface stats. */
433 vlib_increment_combined_counter (ccm, ti, sw_if_index, n_buffers, n_bytes);
434 return n_buffers;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400435}
BenoƮt Ganne7d2094c2020-11-09 15:23:52 +0100436
Damjan Marionf91098e2021-03-09 16:28:15 +0100437VLIB_REGISTER_NODE (vnet_interface_output_node) = {
438 .name = "interface-output-template",
439 .vector_size = sizeof (u32),
440};
Dave Barach2c0a4f42017-06-29 09:30:15 -0400441
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
Filip Tehlar62668772019-03-04 03:33:32 -0800443VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
444 vlib_node_runtime_t *
445 node,
446 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447{
Dave Barachba868bb2016-08-08 09:51:21 -0400448 vnet_main_t *vnm = vnet_get_main ();
449 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 u32 n_left_from, next_index;
451
Dave Barach5ecd5a52019-02-25 15:27:28 -0500452 vnet_interface_pcap_tx_trace (vm, node, frame,
453 1 /* sw_if_index_from_buffer */ );
454
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 n_left_from = frame->n_vectors;
456
Damjan Mariona3d59862018-11-10 10:23:00 +0100457 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 next_index = node->cached_next_index;
459
460 while (n_left_from > 0)
461 {
462 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
463
464 while (n_left_from >= 4 && n_left_to_next >= 2)
465 {
466 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400467 vlib_buffer_t *b0, *b1;
468 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470 /* Prefetch next iteration. */
471 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
472 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
473
474 bi0 = from[0];
475 bi1 = from[1];
476 to_next[0] = bi0;
477 to_next[1] = bi1;
478 from += 2;
479 to_next += 2;
480 n_left_to_next -= 2;
481 n_left_from -= 2;
482
483 b0 = vlib_get_buffer (vm, bi0);
484 b1 = vlib_get_buffer (vm, bi1);
485
Dave Barachba868bb2016-08-08 09:51:21 -0400486 hi0 =
487 vnet_get_sup_hw_interface (vnm,
488 vnet_buffer (b0)->sw_if_index
489 [VLIB_TX]);
490 hi1 =
491 vnet_get_sup_hw_interface (vnm,
492 vnet_buffer (b1)->sw_if_index
493 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
John Loe5453d02018-01-23 19:21:34 -0500495 next0 = hi0->output_node_next_index;
496 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Dave Barachba868bb2016-08-08 09:51:21 -0400498 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
499 n_left_to_next, bi0, bi1, next0,
500 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 }
502
503 while (n_left_from > 0 && n_left_to_next > 0)
504 {
505 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400506 vlib_buffer_t *b0;
507 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
509 bi0 = from[0];
510 to_next[0] = bi0;
511 from += 1;
512 to_next += 1;
513 n_left_to_next -= 1;
514 n_left_from -= 1;
515
516 b0 = vlib_get_buffer (vm, bi0);
517
Dave Barachba868bb2016-08-08 09:51:21 -0400518 hi0 =
519 vnet_get_sup_hw_interface (vnm,
520 vnet_buffer (b0)->sw_if_index
521 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
John Loe5453d02018-01-23 19:21:34 -0500523 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barachba868bb2016-08-08 09:51:21 -0400525 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
526 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 }
528
529 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
530 }
531
532 return frame->n_vectors;
533}
534
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000535typedef struct vnet_error_trace_t_
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000537 u32 sw_if_index;
Dave Barach4330c462020-06-10 17:07:32 -0400538 i8 details_valid;
539 u8 is_ip6;
540 u8 pad[2];
541 u16 mactype;
542 ip46_address_t src, dst;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000543} vnet_error_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
Dave Barachba868bb2016-08-08 09:51:21 -0400545static u8 *
546format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000548 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000550 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Dave Barach4330c462020-06-10 17:07:32 -0400552 /* Normal, non-catchup trace */
553 if (t->details_valid == 0)
554 {
555 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
556 vnet_get_main (), t->sw_if_index);
557 }
558 else if (t->details_valid == 1)
559 {
560 /* The trace capture code didn't understant the mactype */
561 s = format (s, "mactype 0x%4x (not decoded)", t->mactype);
562 }
563 else if (t->details_valid == 2)
564 {
565 /* Dump the src/dst addresses */
566 if (t->is_ip6 == 0)
567 s = format (s, "IP4: %U -> %U",
568 format_ip4_address, &t->src.ip4,
569 format_ip4_address, &t->dst.ip4);
570 else
571 s = format (s, "IP6: %U -> %U",
572 format_ip6_address, &t->src.ip6,
573 format_ip6_address, &t->dst.ip6);
574 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 return s;
576}
577
578static void
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000579interface_trace_buffers (vlib_main_t * vm,
580 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581{
Dave Barachba868bb2016-08-08 09:51:21 -0400582 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
584 buffers = vlib_frame_vector_args (frame);
585 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400586
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 while (n_left >= 4)
588 {
589 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400590 vlib_buffer_t *b0, *b1;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000591 vnet_error_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
593 /* Prefetch next iteration. */
594 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
595 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
596
597 bi0 = buffers[0];
598 bi1 = buffers[1];
599
600 b0 = vlib_get_buffer (vm, bi0);
601 b1 = vlib_get_buffer (vm, bi1);
602
603 if (b0->flags & VLIB_BUFFER_IS_TRACED)
604 {
Dave Barach4330c462020-06-10 17:07:32 -0400605 t0 = vlib_add_trace (vm, node, b0,
606 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000607 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400608 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 }
610 if (b1->flags & VLIB_BUFFER_IS_TRACED)
611 {
Dave Barach4330c462020-06-10 17:07:32 -0400612 t1 = vlib_add_trace (vm, node, b1,
613 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000614 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400615 t1->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 }
617 buffers += 2;
618 n_left -= 2;
619 }
620
621 while (n_left >= 1)
622 {
623 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400624 vlib_buffer_t *b0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000625 vnet_error_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
627 bi0 = buffers[0];
628
629 b0 = vlib_get_buffer (vm, bi0);
630
631 if (b0->flags & VLIB_BUFFER_IS_TRACED)
632 {
Dave Barach4330c462020-06-10 17:07:32 -0400633 t0 = vlib_add_trace (vm, node, b0,
634 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000635 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400636 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 }
638 buffers += 1;
639 n_left -= 1;
640 }
641}
642
Dave Barachba868bb2016-08-08 09:51:21 -0400643typedef enum
644{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 VNET_ERROR_DISPOSITION_DROP,
646 VNET_ERROR_DISPOSITION_PUNT,
647 VNET_ERROR_N_DISPOSITION,
648} vnet_error_disposition_t;
649
Dave Barach4330c462020-06-10 17:07:32 -0400650static void
651drop_catchup_trace (vlib_main_t * vm,
652 vlib_node_runtime_t * node, vlib_buffer_t * b)
653{
654 /* Can we safely rewind the buffer? If not, fagedaboudit */
655 if (b->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
656 {
657 vnet_error_trace_t *t;
658 ip4_header_t *ip4;
659 ip6_header_t *ip6;
660 ethernet_header_t *eh;
661 i16 delta;
662
663 t = vlib_add_trace (vm, node, b, sizeof (*t));
664 delta = vnet_buffer (b)->l2_hdr_offset - b->current_data;
665 vlib_buffer_advance (b, delta);
666
667 eh = vlib_buffer_get_current (b);
668 /* Save mactype */
669 t->mactype = clib_net_to_host_u16 (eh->type);
670 t->details_valid = 1;
671 switch (t->mactype)
672 {
673 case ETHERNET_TYPE_IP4:
674 ip4 = (void *) (eh + 1);
675 t->details_valid = 2;
676 t->is_ip6 = 0;
677 t->src.ip4.as_u32 = ip4->src_address.as_u32;
678 t->dst.ip4.as_u32 = ip4->dst_address.as_u32;
679 break;
680
681 case ETHERNET_TYPE_IP6:
682 ip6 = (void *) (eh + 1);
683 t->details_valid = 2;
684 t->is_ip6 = 1;
685 clib_memcpy_fast (t->src.as_u8, ip6->src_address.as_u8,
686 sizeof (ip6_address_t));
687 clib_memcpy_fast (t->dst.as_u8, ip6->dst_address.as_u8,
688 sizeof (ip6_address_t));
689 break;
690
691 default:
692 /* Dunno, do nothing, leave details_valid alone */
693 break;
694 }
695 /* Restore current data (probably unnecessary) */
696 vlib_buffer_advance (b, -delta);
697 }
698}
699
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700static_always_inline uword
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000701interface_drop_punt (vlib_main_t * vm,
702 vlib_node_runtime_t * node,
703 vlib_frame_t * frame,
704 vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000706 u32 *from, n_left, thread_index, *sw_if_index;
707 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
708 u32 sw_if_indices[VLIB_FRAME_SIZE];
Dave Barachba868bb2016-08-08 09:51:21 -0400709 vlib_simple_counter_main_t *cm;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000710 u16 nexts[VLIB_FRAME_SIZE];
Dave Barach4330c462020-06-10 17:07:32 -0400711 u32 n_trace;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000712 vnet_main_t *vnm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000714 vnm = vnet_get_main ();
715 thread_index = vm->thread_index;
716 from = vlib_frame_vector_args (frame);
717 n_left = frame->n_vectors;
718 b = bufs;
719 sw_if_index = sw_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000721 vlib_get_buffers (vm, from, bufs, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
Dave Barach4330c462020-06-10 17:07:32 -0400723 /* "trace add error-drop NNN?" */
724 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
725 {
726 /* If pkts aren't otherwise traced... */
727 if ((node->flags & VLIB_NODE_FLAG_TRACE) == 0)
728 {
729 /* Trace them from here */
730 node->flags |= VLIB_NODE_FLAG_TRACE;
731 while (n_trace && n_left)
732 {
BenoƮt Ganne9a3973e2020-10-02 19:36:57 +0200733 if (PREDICT_TRUE
734 (vlib_trace_buffer (vm, node, 0 /* next_index */ , b[0],
735 0 /* follow chain */ )))
736 {
737 /*
738 * Here we have a wireshark dissector problem.
739 * Packets may be well-formed, or not. We
740 * must not blow chunks in any case.
741 *
742 * Try to produce trace records which will help
743 * folks understand what's going on.
744 */
745 drop_catchup_trace (vm, node, b[0]);
746 n_trace--;
747 }
Dave Barach4330c462020-06-10 17:07:32 -0400748 n_left--;
749 b++;
750 }
751 }
752
753 vlib_set_trace_count (vm, node, n_trace);
754 b = bufs;
755 n_left = frame->n_vectors;
756 }
757
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000759 interface_trace_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400760
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000761 /* All going to drop regardless, this is just a counting exercise */
762 clib_memset (nexts, 0, sizeof (nexts));
763
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
765 (disposition == VNET_ERROR_DISPOSITION_PUNT
766 ? VNET_INTERFACE_COUNTER_PUNT
767 : VNET_INTERFACE_COUNTER_DROP));
768
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000769 /* collect the array of interfaces first ... */
770 while (n_left >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000772 if (n_left >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000774 /* Prefetch 8 ahead - there's not much going on in each iteration */
775 vlib_prefetch_buffer_header (b[4], LOAD);
776 vlib_prefetch_buffer_header (b[5], LOAD);
777 vlib_prefetch_buffer_header (b[6], LOAD);
778 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000780 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
781 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
782 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
783 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
784
785 sw_if_index += 4;
786 n_left -= 4;
787 b += 4;
788 }
789 while (n_left)
790 {
791 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
792
793 sw_if_index += 1;
794 n_left -= 1;
795 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 }
797
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000798 /* ... then count against them in blocks */
799 n_left = frame->n_vectors;
800
801 while (n_left)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 {
Dave Barachba868bb2016-08-08 09:51:21 -0400803 vnet_sw_interface_t *sw_if0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000804 u16 off, count;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000806 off = frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000808 sw_if_index = sw_if_indices + off;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000810 count = clib_count_equal_u32 (sw_if_index, n_left);
811 n_left -= count;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000813 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000815 /* Increment super-interface drop/punt counters for
816 sub-interfaces. */
817 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
818 if (sw_if0->sup_sw_if_index != sw_if_index[0])
819 vlib_increment_simple_counter
820 (cm, thread_index, sw_if0->sup_sw_if_index, count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 }
822
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000823 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
825 return frame->n_vectors;
826}
827
Dave Barachba868bb2016-08-08 09:51:21 -0400828static inline void
829pcap_drop_trace (vlib_main_t * vm,
Dave Barach33909772019-09-23 10:27:27 -0400830 vnet_interface_main_t * im,
831 vnet_pcap_t * pp, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832{
Dave Barachba868bb2016-08-08 09:51:21 -0400833 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400835 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836 u32 bi0;
837 i16 save_current_data;
838 u16 save_current_length;
Dave Barach9382ad92019-09-23 16:03:49 -0400839 vlib_error_main_t *em = &vm->error_main;
Dave Barachf5667c32019-09-25 11:27:46 -0400840 int do_trace = 0;
841
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
843 from = vlib_frame_vector_args (f);
844
845 while (n_left > 0)
846 {
847 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -0400848 {
849 p1 = vlib_get_buffer (vm, from[1]);
850 vlib_prefetch_buffer_header (p1, LOAD);
851 }
852
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 bi0 = from[0];
854 b0 = vlib_get_buffer (vm, bi0);
855 from++;
856 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -0400857
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -0400859 if (im->pcap_drop_filter_hash
860 && hash_get (im->pcap_drop_filter_hash, b0->error))
861 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
Dave Barachf5667c32019-09-25 11:27:46 -0400863 do_trace = (pp->pcap_sw_if_index == 0) ||
864 pp->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX];
865
866 if (PREDICT_FALSE
867 (do_trace == 0 && pp->filter_classify_table_index != ~0))
868 {
869 do_trace = vnet_is_packet_traced_inline
870 (b0, pp->filter_classify_table_index, 0 /* full classify */ );
871 }
872
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 /* Trace all drops, or drops received on a specific interface */
Dave Barachf5667c32019-09-25 11:27:46 -0400874 if (do_trace)
Dave Barachba868bb2016-08-08 09:51:21 -0400875 {
876 save_current_data = b0->current_data;
877 save_current_length = b0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878
Dave Barachba868bb2016-08-08 09:51:21 -0400879 /*
880 * Typically, we'll need to rewind the buffer
BenoƮt Ganne4e323cb2019-09-17 17:30:35 +0200881 * if l2_hdr_offset is valid, make sure to rewind to the start of
882 * the L2 header. This may not be the buffer start in case we pop-ed
883 * vlan tags.
884 * Otherwise, rewind to buffer start and hope for the best.
Dave Barachba868bb2016-08-08 09:51:21 -0400885 */
BenoƮt Ganne4e323cb2019-09-17 17:30:35 +0200886 if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
887 {
888 if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
889 vlib_buffer_advance (b0,
890 vnet_buffer (b0)->l2_hdr_offset -
891 b0->current_data);
892 }
893 else if (b0->current_data > 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400894 vlib_buffer_advance (b0, (word) - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895
Dave Barach9382ad92019-09-23 16:03:49 -0400896 {
897 vlib_buffer_t *last = b0;
898 u32 error_node_index;
899 int drop_string_len;
900 vlib_node_t *n;
901 /* Length of the error string */
902 int error_string_len =
Ole Troan148c7b72020-10-07 18:05:37 +0200903 clib_strnlen (em->counters_heap[b0->error].name, 128);
Dave Barach9382ad92019-09-23 16:03:49 -0400904
905 /* Dig up the drop node */
906 error_node_index = vm->node_main.node_by_error[b0->error];
907 n = vlib_get_node (vm, error_node_index);
908
909 /* Length of full drop string, w/ "nodename: " prepended */
910 drop_string_len = error_string_len + vec_len (n->name) + 2;
911
912 /* Find the last buffer in the chain */
913 while (last->flags & VLIB_BUFFER_NEXT_PRESENT)
914 last = vlib_get_buffer (vm, last->next_buffer);
915
916 /*
917 * Append <nodename>: <error-string> to the capture,
918 * only if we can do that without allocating a new buffer.
919 */
920 if (PREDICT_TRUE ((last->current_data + last->current_length)
921 < (VLIB_BUFFER_DEFAULT_DATA_SIZE
922 - drop_string_len)))
923 {
924 clib_memcpy_fast (last->data + last->current_data +
925 last->current_length, n->name,
926 vec_len (n->name));
927 clib_memcpy_fast (last->data + last->current_data +
928 last->current_length + vec_len (n->name),
929 ": ", 2);
930 clib_memcpy_fast (last->data + last->current_data +
931 last->current_length + vec_len (n->name) +
Ole Troan148c7b72020-10-07 18:05:37 +0200932 2, em->counters_heap[b0->error].name,
Dave Barach9382ad92019-09-23 16:03:49 -0400933 error_string_len);
934 last->current_length += drop_string_len;
935 b0->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
936 pcap_add_buffer (&pp->pcap_main, vm, bi0,
937 pp->max_bytes_per_pkt);
938 last->current_length -= drop_string_len;
939 b0->current_data = save_current_data;
940 b0->current_length = save_current_length;
941 continue;
942 }
943 }
944
945 /*
946 * Didn't have space in the last buffer, here's the dropped
947 * packet as-is
948 */
Dave Barach33909772019-09-23 10:27:27 -0400949 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barachba868bb2016-08-08 09:51:21 -0400950
951 b0->current_data = save_current_data;
952 b0->current_length = save_current_length;
953 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 }
955}
956
Filip Tehlar62668772019-03-04 03:33:32 -0800957#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -0400958void
959vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960{
Dave Barachba868bb2016-08-08 09:51:21 -0400961 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
963 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400964 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
966 if (is_add)
967 hash_set (im->pcap_drop_filter_hash, error_index, 1);
968 else
969 hash_unset (im->pcap_drop_filter_hash, error_index);
970}
Filip Tehlar62668772019-03-04 03:33:32 -0800971#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000973VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
974 vlib_node_runtime_t * node,
975 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976{
Damjan Marion8fb5add2021-03-04 18:41:59 +0100977 vnet_main_t *vnm = vnet_get_main ();
Dave Barachba868bb2016-08-08 09:51:21 -0400978 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Damjan Marion8fb5add2021-03-04 18:41:59 +0100979 vnet_pcap_t *pp = &vnm->pcap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980
Dave Barach33909772019-09-23 10:27:27 -0400981 if (PREDICT_FALSE (pp->pcap_drop_enable))
982 pcap_drop_trace (vm, im, pp, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000984 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985}
986
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000987VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
988 vlib_node_runtime_t * node,
989 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000991 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992}
993
Dave Barachba868bb2016-08-08 09:51:21 -0400994/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000995VLIB_REGISTER_NODE (interface_drop) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996 .name = "error-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 .vector_size = sizeof (u32),
998 .format_trace = format_vnet_error_trace,
Dave Barach4330c462020-06-10 17:07:32 -0400999 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001000 .n_next_nodes = 1,
1001 .next_nodes = {
1002 [0] = "drop",
1003 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004};
Dave Barachba868bb2016-08-08 09:51:21 -04001005/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
Dave Barachba868bb2016-08-08 09:51:21 -04001007/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001008VLIB_REGISTER_NODE (interface_punt) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009 .name = "error-punt",
1010 .vector_size = sizeof (u32),
1011 .format_trace = format_vnet_error_trace,
Dave Barach4330c462020-06-10 17:07:32 -04001012 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001013 .n_next_nodes = 1,
1014 .next_nodes = {
1015 [0] = "punt",
1016 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017};
Dave Barachba868bb2016-08-08 09:51:21 -04001018/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019
Dave Barachba868bb2016-08-08 09:51:21 -04001020/* *INDENT-OFF* */
Filip Tehlar62668772019-03-04 03:33:32 -08001021VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 .name = "interface-output",
1023 .vector_size = sizeof (u32),
1024};
Dave Barachba868bb2016-08-08 09:51:21 -04001025/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026
Damjan Marion152e21d2016-11-29 14:55:43 +01001027static uword
1028interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1029 vlib_frame_t * from_frame)
1030{
1031 vnet_main_t *vnm = vnet_get_main ();
1032 u32 last_sw_if_index = ~0;
1033 vlib_frame_t *to_frame = 0;
1034 vnet_hw_interface_t *hw = 0;
1035 u32 *from, *to_next = 0;
1036 u32 n_left_from;
1037
1038 from = vlib_frame_vector_args (from_frame);
1039 n_left_from = from_frame->n_vectors;
1040 while (n_left_from > 0)
1041 {
1042 u32 bi0;
1043 vlib_buffer_t *b0;
1044 u32 sw_if_index0;
1045
1046 bi0 = from[0];
1047 from++;
1048 n_left_from--;
1049 b0 = vlib_get_buffer (vm, bi0);
1050 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1051
1052 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1053 {
1054 if (to_frame)
1055 {
1056 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1057 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1058 }
1059 last_sw_if_index = sw_if_index0;
1060 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1061 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1062 to_next = vlib_frame_vector_args (to_frame);
1063 }
1064
1065 to_next[0] = bi0;
1066 to_next++;
1067 to_frame->n_vectors++;
1068 }
1069 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1070 return from_frame->n_vectors;
1071}
1072
1073/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +02001074VLIB_REGISTER_NODE (interface_tx) = {
Damjan Marion152e21d2016-11-29 14:55:43 +01001075 .function = interface_tx_node_fn,
1076 .name = "interface-tx",
1077 .vector_size = sizeof (u32),
1078 .n_next_nodes = 1,
1079 .next_nodes = {
1080 [0] = "error-drop",
1081 },
1082};
1083
1084VNET_FEATURE_ARC_INIT (interface_output, static) =
1085{
1086 .arc_name = "interface-output",
1087 .start_nodes = VNET_FEATURES (0),
Dave Baracha25def72018-11-26 11:04:45 -05001088 .last_in_arc = "interface-tx",
Damjan Marion152e21d2016-11-29 14:55:43 +01001089 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1090};
1091
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001092VNET_FEATURE_INIT (span_tx, static) = {
1093 .arc_name = "interface-output",
1094 .node_name = "span-output",
1095 .runs_before = VNET_FEATURES ("interface-tx"),
1096};
1097
Matthew Smith537eeec2018-04-09 11:49:20 -05001098VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1099 .arc_name = "interface-output",
1100 .node_name = "ipsec-if-output",
1101 .runs_before = VNET_FEATURES ("interface-tx"),
1102};
1103
Damjan Marion152e21d2016-11-29 14:55:43 +01001104VNET_FEATURE_INIT (interface_tx, static) = {
1105 .arc_name = "interface-output",
1106 .node_name = "interface-tx",
1107 .runs_before = 0,
1108};
1109/* *INDENT-ON* */
1110
Filip Tehlar62668772019-03-04 03:33:32 -08001111#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112clib_error_t *
1113vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1114 u32 hw_if_index,
1115 u32 is_create)
1116{
Dave Barachba868bb2016-08-08 09:51:21 -04001117 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 u32 next_index;
1119
John Loe5453d02018-01-23 19:21:34 -05001120 if (hi->output_node_index == 0)
1121 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122
John Loe5453d02018-01-23 19:21:34 -05001123 next_index = vlib_node_add_next
1124 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1125 hi->output_node_index);
1126 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127
1128 return 0;
1129}
1130
Dave Barachba868bb2016-08-08 09:51:21 -04001131VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1132 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133
John Loe5453d02018-01-23 19:21:34 -05001134void
1135vnet_set_interface_output_node (vnet_main_t * vnm,
1136 u32 hw_if_index, u32 node_index)
1137{
1138 ASSERT (node_index);
1139 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1140 u32 next_index = vlib_node_add_next
1141 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1142 hi->output_node_next_index = next_index;
1143 hi->output_node_index = node_index;
1144}
Filip Tehlar62668772019-03-04 03:33:32 -08001145#endif /* CLIB_MARCH_VARIANT */
John Loe5453d02018-01-23 19:21:34 -05001146
Dave Barachba868bb2016-08-08 09:51:21 -04001147/*
1148 * fd.io coding-style-patch-verification: ON
1149 *
1150 * Local Variables:
1151 * eval: (c-set-style "gnu")
1152 * End:
1153 */