blob: 6d5697deac40f0bac5454018cc2c33bf7e190af1 [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>
Benoît Ganne30a81952021-02-26 13:47:41 +010048#include <vnet/classify/pcap_classify.h>
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +000049#include <vnet/hash/hash.h>
Dave Barach1bd2c012020-04-12 08:31:39 -040050#include <vnet/interface_output.h>
Damjan Mariond154a172021-07-13 21:12:41 +020051#include <vppinfra/vector/mask_compare.h>
52#include <vppinfra/vector/compress.h>
Damjan Marion4c276f02021-11-06 13:17:31 +010053#include <vppinfra/vector/count_equal.h>
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +000054#include <vppinfra/vector/array_mask.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Dave Barachba868bb2016-08-08 09:51:21 -040056typedef struct
57{
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 u32 sw_if_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020059 u32 flags;
Mohsin Kazmi29467b52019-10-08 19:42:38 +020060 u8 data[128 - 2 * sizeof (u32)];
Dave Barachba868bb2016-08-08 09:51:21 -040061}
62interface_output_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Filip Tehlar62668772019-03-04 03:33:32 -080064#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -040065u8 *
66format_vnet_interface_output_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070067{
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040069 vlib_node_t *node = va_arg (*va, vlib_node_t *);
70 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
71 vnet_main_t *vnm = vnet_get_main ();
72 vnet_sw_interface_t *si;
Christophe Fontained3c008d2017-10-02 18:10:54 +020073 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Dave Barachba868bb2016-08-08 09:51:21 -040075 if (t->sw_if_index != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 indent = format_get_indent (s);
Dave Barachba868bb2016-08-08 09:51:21 -040078
Neale Ranns177bbdc2016-11-15 09:46:51 +000079 if (pool_is_free_index
80 (vnm->interface_main.sw_interfaces, t->sw_if_index))
81 {
82 /* the interface may have been deleted by the time the trace is printed */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020083 s = format (s, "sw_if_index: %d ", t->sw_if_index);
Neale Ranns177bbdc2016-11-15 09:46:51 +000084 }
85 else
86 {
87 si = vnet_get_sw_interface (vnm, t->sw_if_index);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020088 s =
89 format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
90 t->flags);
Neale Ranns177bbdc2016-11-15 09:46:51 +000091 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020092 s =
93 format (s, "\n%U%U", format_white_space, indent,
94 node->format_buffer ? node->format_buffer : format_hex_bytes,
95 t->data, sizeof (t->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 }
97 return s;
98}
Benoît Ganne7d2094c2020-11-09 15:23:52 +010099#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101static void
102vnet_interface_output_trace (vlib_main_t * vm,
103 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -0400104 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105{
Dave Barachba868bb2016-08-08 09:51:21 -0400106 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
108 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100109 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400110
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 while (n_left >= 4)
112 {
113 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400114 vlib_buffer_t *b0, *b1;
115 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
117 /* Prefetch next iteration. */
118 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
119 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
120
121 bi0 = from[0];
122 bi1 = from[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->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200131 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500132 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
133 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 }
135 if (b1->flags & VLIB_BUFFER_IS_TRACED)
136 {
137 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
138 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200139 t1->flags = b1->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500140 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
141 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 }
143 from += 2;
144 n_left -= 2;
145 }
146
147 while (n_left >= 1)
148 {
149 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400150 vlib_buffer_t *b0;
151 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153 bi0 = from[0];
154
155 b0 = vlib_get_buffer (vm, bi0);
156
157 if (b0->flags & VLIB_BUFFER_IS_TRACED)
158 {
159 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
160 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200161 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500162 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
163 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 }
165 from += 1;
166 n_left -= 1;
167 }
168}
169
Damjan Marion4d2726e2021-03-23 21:05:18 +0100170static_always_inline void
171vnet_interface_output_handle_offload (vlib_main_t *vm, vlib_buffer_t *b)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400172{
Aloys Augustinac6c5282021-04-15 18:12:51 +0200173 vnet_calc_checksums_inline (vm, b, b->flags & VNET_BUFFER_F_IS_IP4,
174 b->flags & VNET_BUFFER_F_IS_IP6);
Mohsin Kazmi376c2102021-11-26 17:04:07 +0100175 vnet_calc_outer_checksums_inline (vm, b);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100176}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Damjan Marion4d2726e2021-03-23 21:05:18 +0100178static_always_inline uword
179vnet_interface_output_node_inline (vlib_main_t *vm, u32 sw_if_index,
180 vlib_combined_counter_main_t *ccm,
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000181 vlib_buffer_t **b, void **p,
182 u32 config_index, u8 arc, u32 n_left,
183 int processing_level)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100184{
185 u32 n_bytes = 0;
186 u32 n_bytes0, n_bytes1, n_bytes2, n_bytes3;
187 u32 ti = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Damjan Marion4d2726e2021-03-23 21:05:18 +0100189 while (n_left >= 8)
Damjan Marion152e21d2016-11-29 14:55:43 +0100190 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100191 u32 or_flags;
Damjan Marion152e21d2016-11-29 14:55:43 +0100192
Damjan Marion4d2726e2021-03-23 21:05:18 +0100193 /* Prefetch next iteration. */
194 vlib_prefetch_buffer_header (b[4], LOAD);
195 vlib_prefetch_buffer_header (b[5], LOAD);
196 vlib_prefetch_buffer_header (b[6], LOAD);
197 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000199 if (processing_level >= 1)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100200 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100201
202 /* Be grumpy about zero length buffers for benefit of
203 driver tx function. */
204 ASSERT (b[0]->current_length > 0);
205 ASSERT (b[1]->current_length > 0);
206 ASSERT (b[2]->current_length > 0);
207 ASSERT (b[3]->current_length > 0);
208
209 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
210 n_bytes += n_bytes1 = vlib_buffer_length_in_chain (vm, b[1]);
211 n_bytes += n_bytes2 = vlib_buffer_length_in_chain (vm, b[2]);
212 n_bytes += n_bytes3 = vlib_buffer_length_in_chain (vm, b[3]);
213
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000214 if (processing_level >= 3)
215 {
216 p[0] = vlib_buffer_get_current (b[0]);
217 p[1] = vlib_buffer_get_current (b[1]);
218 p[2] = vlib_buffer_get_current (b[2]);
219 p[3] = vlib_buffer_get_current (b[3]);
220 p += 4;
221 }
222
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000223 if (processing_level >= 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100225 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
226 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
227 tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
228 tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
229 tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
230
231 /* update vlan subif tx counts, if required */
232 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
233 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
234
235 if (PREDICT_FALSE (tx_swif1 != sw_if_index))
236 vlib_increment_combined_counter (ccm, ti, tx_swif1, 1, n_bytes1);
237
238 if (PREDICT_FALSE (tx_swif2 != sw_if_index))
239 vlib_increment_combined_counter (ccm, ti, tx_swif2, 1, n_bytes2);
240
241 if (PREDICT_FALSE (tx_swif3 != sw_if_index))
242 vlib_increment_combined_counter (ccm, ti, tx_swif3, 1, n_bytes3);
243
244 if (PREDICT_FALSE (config_index != ~0))
245 {
246 vnet_buffer (b[0])->feature_arc_index = arc;
247 b[0]->current_config_index = config_index;
248 vnet_buffer (b[1])->feature_arc_index = arc;
249 b[1]->current_config_index = config_index;
250 vnet_buffer (b[2])->feature_arc_index = arc;
251 b[2]->current_config_index = config_index;
252 vnet_buffer (b[3])->feature_arc_index = arc;
253 b[3]->current_config_index = config_index;
254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 }
256
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000257 if (processing_level >= 1 && (or_flags & VNET_BUFFER_F_OFFLOAD))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100259 vnet_interface_output_handle_offload (vm, b[0]);
260 vnet_interface_output_handle_offload (vm, b[1]);
261 vnet_interface_output_handle_offload (vm, b[2]);
262 vnet_interface_output_handle_offload (vm, b[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 }
264
Damjan Marion4d2726e2021-03-23 21:05:18 +0100265 n_left -= 4;
266 b += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 }
268
Damjan Marion4d2726e2021-03-23 21:05:18 +0100269 while (n_left)
270 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100271 /* Be grumpy about zero length buffers for benefit of
272 driver tx function. */
273 ASSERT (b[0]->current_length > 0);
274
275 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100276
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000277 if (processing_level >= 3)
278 {
279 p[0] = vlib_buffer_get_current (b[0]);
280 p += 1;
281 }
282
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000283 if (processing_level >= 2)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100284 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100285 u32 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100286
Damjan Mariond4008cf2021-03-24 11:45:51 +0100287 if (PREDICT_FALSE (config_index != ~0))
288 {
289 vnet_buffer (b[0])->feature_arc_index = arc;
290 b[0]->current_config_index = config_index;
291 }
292
293 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
294 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
295 }
Damjan Marion4d2726e2021-03-23 21:05:18 +0100296
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000297 if (processing_level >= 1)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100298 vnet_interface_output_handle_offload (vm, b[0]);
299
300 n_left -= 1;
301 b += 1;
302 }
303
304 return n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305}
306
Benoît Ganne14331852021-07-15 19:21:31 +0200307static_always_inline void
308vnet_interface_pcap_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node,
309 vlib_frame_t *frame, int in_interface_ouput)
Dave Barach5ecd5a52019-02-25 15:27:28 -0500310{
Damjan Marion8fb5add2021-03-04 18:41:59 +0100311 vnet_main_t *vnm = vnet_get_main ();
Dave Barach5ecd5a52019-02-25 15:27:28 -0500312 u32 n_left_from, *from;
Benoît Ganne14331852021-07-15 19:21:31 +0200313 u32 sw_if_index = ~0, hw_if_index = ~0;
Damjan Marion8fb5add2021-03-04 18:41:59 +0100314 vnet_pcap_t *pp = &vnm->pcap;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500315
Dave Barach33909772019-09-23 10:27:27 -0400316 if (PREDICT_TRUE (pp->pcap_tx_enable == 0))
Dave Barach5ecd5a52019-02-25 15:27:28 -0500317 return;
318
Benoît Ganne14331852021-07-15 19:21:31 +0200319 if (in_interface_ouput)
320 {
321 /* interface-output is called right before interface-output-template.
322 * We only want to capture packets here if there is a per-interface
323 * filter, in case it matches the sub-interface sw_if_index.
324 * If there is no per-interface filter configured, let the
325 * interface-output-template node deal with it */
326 if (pp->pcap_sw_if_index == 0)
327 return;
328 }
329 else
Dave Barach5ecd5a52019-02-25 15:27:28 -0500330 {
331 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
332 sw_if_index = rt->sw_if_index;
333 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500334
335 n_left_from = frame->n_vectors;
336 from = vlib_frame_vector_args (frame);
337
338 while (n_left_from > 0)
339 {
340 u32 bi0 = from[0];
341 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Dave Barach9137e542019-09-13 17:47:50 -0400342 from++;
343 n_left_from--;
344
Benoît Ganne14331852021-07-15 19:21:31 +0200345 if (in_interface_ouput)
346 {
347 const u32 sii = vnet_buffer (b0)->sw_if_index[VLIB_TX];
348 if (PREDICT_FALSE (sii != sw_if_index))
349 {
350 const vnet_hw_interface_t *hi =
351 vnet_get_sup_hw_interface (vnm, sii);
352 hw_if_index = hi->sw_if_index;
353 sw_if_index = sii;
354 }
355 if (hw_if_index == sw_if_index)
356 continue; /* defer to interface-output-template */
357 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500358
Benoît Ganne30a81952021-02-26 13:47:41 +0100359 if (vnet_is_packet_pcaped (pp, b0, sw_if_index))
360 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500361 }
362}
363
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200364static_always_inline void
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000365hash_func_with_mask (void **p, u32 *hash, u32 n_packets, u32 *lookup_table,
366 u32 mask, vnet_hash_fn_t hf)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200367{
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000368 u32 n_left_from = n_packets;
369
370 hf (p, hash, n_packets);
371
372 clib_array_mask_u32 (hash, mask, n_packets);
373
374 while (n_left_from >= 4)
375 {
376 hash[0] = lookup_table[hash[0]];
377 hash[1] = lookup_table[hash[1]];
378 hash[2] = lookup_table[hash[2]];
379 hash[3] = lookup_table[hash[3]];
380
381 hash += 4;
382 n_left_from -= 4;
383 }
384
385 while (n_left_from > 0)
386 {
387 hash[0] = lookup_table[hash[0]];
388
389 hash += 1;
390 n_left_from -= 1;
391 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200392}
393
394static_always_inline void
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000395store_tx_frame_scalar_data (vnet_hw_if_tx_frame_t *copy_frame,
396 vnet_hw_if_tx_frame_t *tf)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200397{
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000398 if (copy_frame)
399 clib_memcpy_fast (tf, copy_frame, sizeof (vnet_hw_if_tx_frame_t));
400}
401
402static_always_inline u32
403enqueue_one_to_tx_node (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *ppqi,
404 u32 *from, vnet_hw_if_tx_frame_t *copy_frame,
405 u32 n_vectors, u32 n_left, u32 next_index)
406{
407 u32 tmp[VLIB_FRAME_SIZE];
Damjan Marionefeea5b2022-02-10 15:01:03 +0100408 vlib_frame_bitmap_t mask = {};
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200409 vlib_frame_t *f;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000410 vnet_hw_if_tx_frame_t *tf;
411 u32 *to;
412 u32 n_copy = 0, n_free = 0;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200413
414 f = vlib_get_next_frame_internal (vm, node, next_index, 0);
415 tf = vlib_frame_scalar_args (f);
416
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000417 if (f->n_vectors > 0 &&
418 (!copy_frame || (tf->queue_id == copy_frame->queue_id)))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200419 {
420 /* append current next frame */
421 n_free = VLIB_FRAME_SIZE - f->n_vectors;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000422 /*
423 * if frame contains enough space for worst case scenario,
424 * we can avoid use of tmp
425 */
426 if (n_free >= n_left)
427 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
428 else
429 to = tmp;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200430 }
431 else
432 {
433 if (f->n_vectors > 0)
434 {
435 /* current frame doesn't fit - grab empty one */
436 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
437 tf = vlib_frame_scalar_args (f);
438 }
439
440 /* empty frame - store scalar data */
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000441 store_tx_frame_scalar_data (copy_frame, tf);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200442 to = vlib_frame_vector_args (f);
443 n_free = VLIB_FRAME_SIZE;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200444 }
445
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000446 /*
447 * per packet queue id array
448 * compare with given queue_id, if match, copy respective buffer index from
449 * -> to
450 */
451 if (ppqi)
452 {
453 clib_mask_compare_u32 (copy_frame->queue_id, ppqi, mask, n_vectors);
454 n_copy = clib_compress_u32 (to, from, mask, n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200455
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000456 if (n_copy == 0)
457 return n_left;
458 }
459 else
460 {
461 /*
462 * no work required, just copy all buffer indices from -> to
463 */
464 n_copy = n_left;
465 vlib_buffer_copy_indices (to, from, n_copy);
466 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200467
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000468 if (to != tmp)
469 {
470 /* indices already written to frame, just close it */
471 vlib_put_next_frame (vm, node, next_index, n_free - n_copy);
472 }
473 else if (n_free >= n_copy)
474 {
475 /* enough space in the existing frame */
476 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
477 vlib_buffer_copy_indices (to, tmp, n_copy);
478 vlib_put_next_frame (vm, node, next_index, n_free - n_copy);
479 }
480 else
481 {
482 /* full frame */
483 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
484 vlib_buffer_copy_indices (to, tmp, n_free);
485 vlib_put_next_frame (vm, node, next_index, 0);
486
487 /* second frame */
488 u32 n_2nd_frame = n_copy - n_free;
489 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
490 tf = vlib_frame_scalar_args (f);
491 /* empty frame - store scalar data */
492 store_tx_frame_scalar_data (copy_frame, tf);
493 to = vlib_frame_vector_args (f);
494 vlib_buffer_copy_indices (to, tmp + n_free, n_2nd_frame);
495 vlib_put_next_frame (vm, node, next_index,
496 VLIB_FRAME_SIZE - n_2nd_frame);
497 }
498
499 return n_left - n_copy;
500}
501
502static_always_inline void
503enqueue_to_tx_node (vlib_main_t *vm, vlib_node_runtime_t *node,
504 vnet_hw_interface_t *hi, u32 next_index,
505 vnet_hw_if_output_node_runtime_t *r, u32 *from, void **p,
506 u32 n_vectors)
507{
508 u32 n_left = n_vectors;
509
510 ASSERT (n_vectors <= VLIB_FRAME_SIZE);
511
512 /*
513 * backward compatible for drivers not integrated with new tx infra.
514 */
515 if (r == 0)
516 {
517 n_left = enqueue_one_to_tx_node (vm, node, NULL, from, NULL, n_vectors,
518 n_left, next_index);
519 }
520 /*
521 * only 1 tx queue of given interface is available on given thread
522 */
523 else if (r->n_queues == 1)
524 {
525 n_left = enqueue_one_to_tx_node (vm, node, NULL, from, r->frame,
526 n_vectors, n_left, next_index);
527 }
528 /*
529 * multi tx-queues use case
530 */
531 else if (r->n_queues > 1)
532 {
533 u32 qids[VLIB_FRAME_SIZE];
534
535 hash_func_with_mask (p, qids, n_vectors, r->lookup_table,
536 vec_len (r->lookup_table) - 1, hi->hf);
537
538 for (u32 i = 0; i < r->n_queues; i++)
539 {
540 n_left = enqueue_one_to_tx_node (vm, node, qids, from, &r->frame[i],
541 n_vectors, n_left, next_index);
542 if (n_left == 0)
543 break;
544 }
545 }
546 else
547 ASSERT (0);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200548}
549
Damjan Marionf91098e2021-03-09 16:28:15 +0100550VLIB_NODE_FN (vnet_interface_output_node)
551(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400552{
553 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion4d2726e2021-03-23 21:05:18 +0100554 vnet_interface_main_t *im = &vnm->interface_main;
555 vlib_combined_counter_main_t *ccm;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400556 vnet_hw_interface_t *hi;
Damjan Marion3853b262021-03-23 18:47:34 +0100557 vnet_sw_interface_t *si;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400558 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000559 vnet_hw_if_output_node_runtime_t *r = 0;
Damjan Marion3853b262021-03-23 18:47:34 +0100560 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100561 u32 n_bytes, n_buffers = frame->n_vectors;
562 u32 config_index = ~0;
563 u32 sw_if_index = rt->sw_if_index;
564 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
565 u32 ti = vm->thread_index;
566 u8 arc = im->output_feature_arc_index;
Damjan Mariond4008cf2021-03-24 11:45:51 +0100567 int arc_or_subif = 0;
568 int do_tx_offloads = 0;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000569 void *ptr[VLIB_FRAME_SIZE], **p = ptr;
570 u8 is_parr = 0;
Damjan Marion3853b262021-03-23 18:47:34 +0100571 u32 *from;
572
573 if (node->flags & VLIB_NODE_FLAG_TRACE)
574 vnet_interface_output_trace (vm, node, frame, n_buffers);
575
576 from = vlib_frame_vector_args (frame);
577
578 if (rt->is_deleted)
579 return vlib_error_drop_buffers (
580 vm, node, from,
581 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
582 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400583
Benoît Ganne14331852021-07-15 19:21:31 +0200584 vnet_interface_pcap_tx_trace (vm, node, frame, 0 /* in_interface_ouput */);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500585
Damjan Marion3853b262021-03-23 18:47:34 +0100586 vlib_get_buffers (vm, from, bufs, n_buffers);
587
Damjan Marion4d2726e2021-03-23 21:05:18 +0100588 si = vnet_get_sw_interface (vnm, sw_if_index);
589 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Damjan Marion3853b262021-03-23 18:47:34 +0100590
591 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
592 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
593 {
594 vlib_simple_counter_main_t *cm;
595
596 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
597 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100598 vlib_increment_simple_counter (cm, ti, sw_if_index, n_buffers);
Damjan Marion3853b262021-03-23 18:47:34 +0100599
600 return vlib_error_drop_buffers (
601 vm, node, from,
602 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
603 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
604 }
605
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000606 if (hi->output_node_thread_runtimes)
607 r = vec_elt_at_index (hi->output_node_thread_runtimes, vm->thread_index);
608
609 if (r)
610 {
611 /*
612 * tx queue of given interface is not available on given thread
613 */
614 if (r->n_queues == 0)
615 return vlib_error_drop_buffers (
616 vm, node, from,
617 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
618 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_NO_TX_QUEUE);
619 /*
620 * multiple tx queues available on given thread
621 */
622 else if (r->n_queues > 1)
623 /* construct array of pointer */
624 is_parr = 1;
625 }
626
Damjan Marion4d2726e2021-03-23 21:05:18 +0100627 /* interface-output feature arc handling */
628 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
629 {
630 vnet_feature_config_main_t *fcm;
631 fcm = vnet_feature_get_config_main (arc);
632 config_index = vnet_get_feature_config_index (arc, sw_if_index);
633 vnet_get_config_data (&fcm->config_main, &config_index, &next_index, 0);
Damjan Mariond4008cf2021-03-24 11:45:51 +0100634 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100635 }
Damjan Mariond4008cf2021-03-24 11:45:51 +0100636 else if (hash_elts (hi->sub_interface_sw_if_index_by_id))
637 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100638
639 ccm = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX;
640
Nathan Skrzypczak2e558232021-05-27 19:34:59 +0200641 /* if not all three flags IP4_,TCP_,UDP_CKSUM set, do compute them
642 * here before sending to the interface */
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100643 if ((hi->caps & VNET_HW_IF_CAP_TX_CKSUM) != VNET_HW_IF_CAP_TX_CKSUM)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100644 do_tx_offloads = 1;
645
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000646 // basic processing
647 if (do_tx_offloads == 0 && arc_or_subif == 0 && is_parr == 0)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100648 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000649 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 0);
650 // basic processing + tx offloads
651 else if (do_tx_offloads == 1 && arc_or_subif == 0 && is_parr == 0)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100652 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000653 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 1);
654 // basic processing + tx offloads + vlans + arcs
655 else if (do_tx_offloads == 1 && arc_or_subif == 1 && is_parr == 0)
656 n_bytes = vnet_interface_output_node_inline (
657 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 2);
658 // basic processing + tx offloads + vlans + arcs + multi-txqs
Dave Barach2c0a4f42017-06-29 09:30:15 -0400659 else
Damjan Mariond4008cf2021-03-24 11:45:51 +0100660 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000661 vm, sw_if_index, ccm, bufs, p, config_index, arc, n_buffers, 3);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100662
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200663 from = vlib_frame_vector_args (frame);
664 if (PREDICT_TRUE (next_index == VNET_INTERFACE_OUTPUT_NEXT_TX))
665 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000666 enqueue_to_tx_node (vm, node, hi, next_index, r, from, ptr,
667 frame->n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200668 }
669 else
670 {
671 vlib_buffer_enqueue_to_single_next (vm, node, from, next_index,
672 frame->n_vectors);
673 }
Damjan Marion4d2726e2021-03-23 21:05:18 +0100674
675 /* Update main interface stats. */
676 vlib_increment_combined_counter (ccm, ti, sw_if_index, n_buffers, n_bytes);
677 return n_buffers;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400678}
Benoît Ganne7d2094c2020-11-09 15:23:52 +0100679
Damjan Marionf91098e2021-03-09 16:28:15 +0100680VLIB_REGISTER_NODE (vnet_interface_output_node) = {
681 .name = "interface-output-template",
682 .vector_size = sizeof (u32),
683};
Dave Barach2c0a4f42017-06-29 09:30:15 -0400684
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
Filip Tehlar62668772019-03-04 03:33:32 -0800686VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
687 vlib_node_runtime_t *
688 node,
689 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690{
Dave Barachba868bb2016-08-08 09:51:21 -0400691 vnet_main_t *vnm = vnet_get_main ();
692 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 u32 n_left_from, next_index;
694
Benoît Ganne14331852021-07-15 19:21:31 +0200695 vnet_interface_pcap_tx_trace (vm, node, frame, 1 /* in_interface_ouput */);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500696
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 n_left_from = frame->n_vectors;
698
Damjan Mariona3d59862018-11-10 10:23:00 +0100699 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 next_index = node->cached_next_index;
701
702 while (n_left_from > 0)
703 {
704 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
705
706 while (n_left_from >= 4 && n_left_to_next >= 2)
707 {
708 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400709 vlib_buffer_t *b0, *b1;
710 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
712 /* Prefetch next iteration. */
713 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
714 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
715
716 bi0 = from[0];
717 bi1 = from[1];
718 to_next[0] = bi0;
719 to_next[1] = bi1;
720 from += 2;
721 to_next += 2;
722 n_left_to_next -= 2;
723 n_left_from -= 2;
724
725 b0 = vlib_get_buffer (vm, bi0);
726 b1 = vlib_get_buffer (vm, bi1);
727
Dave Barachba868bb2016-08-08 09:51:21 -0400728 hi0 =
729 vnet_get_sup_hw_interface (vnm,
730 vnet_buffer (b0)->sw_if_index
731 [VLIB_TX]);
732 hi1 =
733 vnet_get_sup_hw_interface (vnm,
734 vnet_buffer (b1)->sw_if_index
735 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
John Loe5453d02018-01-23 19:21:34 -0500737 next0 = hi0->output_node_next_index;
738 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
Dave Barachba868bb2016-08-08 09:51:21 -0400740 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
741 n_left_to_next, bi0, bi1, next0,
742 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 }
744
745 while (n_left_from > 0 && n_left_to_next > 0)
746 {
747 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400748 vlib_buffer_t *b0;
749 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
751 bi0 = from[0];
752 to_next[0] = bi0;
753 from += 1;
754 to_next += 1;
755 n_left_to_next -= 1;
756 n_left_from -= 1;
757
758 b0 = vlib_get_buffer (vm, bi0);
759
Dave Barachba868bb2016-08-08 09:51:21 -0400760 hi0 =
761 vnet_get_sup_hw_interface (vnm,
762 vnet_buffer (b0)->sw_if_index
763 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
John Loe5453d02018-01-23 19:21:34 -0500765 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Dave Barachba868bb2016-08-08 09:51:21 -0400767 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
768 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 }
770
771 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
772 }
773
774 return frame->n_vectors;
775}
776
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000777typedef struct vnet_error_trace_t_
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000779 u32 sw_if_index;
Dave Barach4330c462020-06-10 17:07:32 -0400780 i8 details_valid;
781 u8 is_ip6;
782 u8 pad[2];
783 u16 mactype;
784 ip46_address_t src, dst;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000785} vnet_error_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786
Dave Barachba868bb2016-08-08 09:51:21 -0400787static u8 *
788format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000790 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000792 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
Dave Barach4330c462020-06-10 17:07:32 -0400794 /* Normal, non-catchup trace */
795 if (t->details_valid == 0)
796 {
797 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
798 vnet_get_main (), t->sw_if_index);
799 }
800 else if (t->details_valid == 1)
801 {
802 /* The trace capture code didn't understant the mactype */
803 s = format (s, "mactype 0x%4x (not decoded)", t->mactype);
804 }
805 else if (t->details_valid == 2)
806 {
807 /* Dump the src/dst addresses */
808 if (t->is_ip6 == 0)
809 s = format (s, "IP4: %U -> %U",
810 format_ip4_address, &t->src.ip4,
811 format_ip4_address, &t->dst.ip4);
812 else
813 s = format (s, "IP6: %U -> %U",
814 format_ip6_address, &t->src.ip6,
815 format_ip6_address, &t->dst.ip6);
816 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 return s;
818}
819
820static void
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000821interface_trace_buffers (vlib_main_t * vm,
822 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823{
Dave Barachba868bb2016-08-08 09:51:21 -0400824 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
826 buffers = vlib_frame_vector_args (frame);
827 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400828
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 while (n_left >= 4)
830 {
831 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400832 vlib_buffer_t *b0, *b1;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000833 vnet_error_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
835 /* Prefetch next iteration. */
836 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
837 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
838
839 bi0 = buffers[0];
840 bi1 = buffers[1];
841
842 b0 = vlib_get_buffer (vm, bi0);
843 b1 = vlib_get_buffer (vm, bi1);
844
845 if (b0->flags & VLIB_BUFFER_IS_TRACED)
846 {
Dave Barach4330c462020-06-10 17:07:32 -0400847 t0 = vlib_add_trace (vm, node, b0,
848 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000849 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400850 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851 }
852 if (b1->flags & VLIB_BUFFER_IS_TRACED)
853 {
Dave Barach4330c462020-06-10 17:07:32 -0400854 t1 = vlib_add_trace (vm, node, b1,
855 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000856 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400857 t1->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858 }
859 buffers += 2;
860 n_left -= 2;
861 }
862
863 while (n_left >= 1)
864 {
865 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400866 vlib_buffer_t *b0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000867 vnet_error_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
869 bi0 = buffers[0];
870
871 b0 = vlib_get_buffer (vm, bi0);
872
873 if (b0->flags & VLIB_BUFFER_IS_TRACED)
874 {
Dave Barach4330c462020-06-10 17:07:32 -0400875 t0 = vlib_add_trace (vm, node, b0,
876 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000877 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400878 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 }
880 buffers += 1;
881 n_left -= 1;
882 }
883}
884
Dave Barachba868bb2016-08-08 09:51:21 -0400885typedef enum
886{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 VNET_ERROR_DISPOSITION_DROP,
888 VNET_ERROR_DISPOSITION_PUNT,
889 VNET_ERROR_N_DISPOSITION,
890} vnet_error_disposition_t;
891
Dave Barach4330c462020-06-10 17:07:32 -0400892static void
893drop_catchup_trace (vlib_main_t * vm,
894 vlib_node_runtime_t * node, vlib_buffer_t * b)
895{
896 /* Can we safely rewind the buffer? If not, fagedaboudit */
897 if (b->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
898 {
899 vnet_error_trace_t *t;
900 ip4_header_t *ip4;
901 ip6_header_t *ip6;
902 ethernet_header_t *eh;
903 i16 delta;
904
905 t = vlib_add_trace (vm, node, b, sizeof (*t));
906 delta = vnet_buffer (b)->l2_hdr_offset - b->current_data;
907 vlib_buffer_advance (b, delta);
908
909 eh = vlib_buffer_get_current (b);
910 /* Save mactype */
911 t->mactype = clib_net_to_host_u16 (eh->type);
912 t->details_valid = 1;
913 switch (t->mactype)
914 {
915 case ETHERNET_TYPE_IP4:
916 ip4 = (void *) (eh + 1);
917 t->details_valid = 2;
918 t->is_ip6 = 0;
919 t->src.ip4.as_u32 = ip4->src_address.as_u32;
920 t->dst.ip4.as_u32 = ip4->dst_address.as_u32;
921 break;
922
923 case ETHERNET_TYPE_IP6:
924 ip6 = (void *) (eh + 1);
925 t->details_valid = 2;
926 t->is_ip6 = 1;
927 clib_memcpy_fast (t->src.as_u8, ip6->src_address.as_u8,
928 sizeof (ip6_address_t));
929 clib_memcpy_fast (t->dst.as_u8, ip6->dst_address.as_u8,
930 sizeof (ip6_address_t));
931 break;
932
933 default:
934 /* Dunno, do nothing, leave details_valid alone */
935 break;
936 }
937 /* Restore current data (probably unnecessary) */
938 vlib_buffer_advance (b, -delta);
939 }
940}
941
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942static_always_inline uword
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000943interface_drop_punt (vlib_main_t * vm,
944 vlib_node_runtime_t * node,
945 vlib_frame_t * frame,
946 vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000948 u32 *from, n_left, thread_index, *sw_if_index;
949 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
950 u32 sw_if_indices[VLIB_FRAME_SIZE];
Dave Barachba868bb2016-08-08 09:51:21 -0400951 vlib_simple_counter_main_t *cm;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000952 u16 nexts[VLIB_FRAME_SIZE];
Dave Barach4330c462020-06-10 17:07:32 -0400953 u32 n_trace;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000954 vnet_main_t *vnm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000956 vnm = vnet_get_main ();
957 thread_index = vm->thread_index;
958 from = vlib_frame_vector_args (frame);
959 n_left = frame->n_vectors;
960 b = bufs;
961 sw_if_index = sw_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000963 vlib_get_buffers (vm, from, bufs, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964
Dave Barach4330c462020-06-10 17:07:32 -0400965 /* "trace add error-drop NNN?" */
966 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
967 {
968 /* If pkts aren't otherwise traced... */
969 if ((node->flags & VLIB_NODE_FLAG_TRACE) == 0)
970 {
971 /* Trace them from here */
972 node->flags |= VLIB_NODE_FLAG_TRACE;
973 while (n_trace && n_left)
974 {
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200975 if (PREDICT_TRUE
976 (vlib_trace_buffer (vm, node, 0 /* next_index */ , b[0],
977 0 /* follow chain */ )))
978 {
979 /*
980 * Here we have a wireshark dissector problem.
981 * Packets may be well-formed, or not. We
982 * must not blow chunks in any case.
983 *
984 * Try to produce trace records which will help
985 * folks understand what's going on.
986 */
987 drop_catchup_trace (vm, node, b[0]);
988 n_trace--;
989 }
Dave Barach4330c462020-06-10 17:07:32 -0400990 n_left--;
991 b++;
992 }
993 }
994
995 vlib_set_trace_count (vm, node, n_trace);
996 b = bufs;
997 n_left = frame->n_vectors;
998 }
999
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001001 interface_trace_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -04001002
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001003 /* All going to drop regardless, this is just a counting exercise */
1004 clib_memset (nexts, 0, sizeof (nexts));
1005
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1007 (disposition == VNET_ERROR_DISPOSITION_PUNT
1008 ? VNET_INTERFACE_COUNTER_PUNT
1009 : VNET_INTERFACE_COUNTER_DROP));
1010
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001011 /* collect the array of interfaces first ... */
1012 while (n_left >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001014 if (n_left >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001016 /* Prefetch 8 ahead - there's not much going on in each iteration */
1017 vlib_prefetch_buffer_header (b[4], LOAD);
1018 vlib_prefetch_buffer_header (b[5], LOAD);
1019 vlib_prefetch_buffer_header (b[6], LOAD);
1020 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001022 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1023 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
1024 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
1025 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
1026
1027 sw_if_index += 4;
1028 n_left -= 4;
1029 b += 4;
1030 }
1031 while (n_left)
1032 {
1033 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1034
1035 sw_if_index += 1;
1036 n_left -= 1;
1037 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038 }
1039
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001040 /* ... then count against them in blocks */
1041 n_left = frame->n_vectors;
1042
1043 while (n_left)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044 {
Dave Barachba868bb2016-08-08 09:51:21 -04001045 vnet_sw_interface_t *sw_if0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001046 u16 off, count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001048 off = frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001050 sw_if_index = sw_if_indices + off;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001052 count = clib_count_equal_u32 (sw_if_index, n_left);
1053 n_left -= count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001055 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001057 /* Increment super-interface drop/punt counters for
1058 sub-interfaces. */
1059 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
1060 if (sw_if0->sup_sw_if_index != sw_if_index[0])
1061 vlib_increment_simple_counter
1062 (cm, thread_index, sw_if0->sup_sw_if_index, count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 }
1064
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001065 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066
1067 return frame->n_vectors;
1068}
1069
Dave Barachba868bb2016-08-08 09:51:21 -04001070static inline void
1071pcap_drop_trace (vlib_main_t * vm,
Dave Barach33909772019-09-23 10:27:27 -04001072 vnet_interface_main_t * im,
1073 vnet_pcap_t * pp, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074{
Dave Barachba868bb2016-08-08 09:51:21 -04001075 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001077 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078 u32 bi0;
1079 i16 save_current_data;
1080 u16 save_current_length;
Dave Barach9382ad92019-09-23 16:03:49 -04001081 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
1083 from = vlib_frame_vector_args (f);
1084
1085 while (n_left > 0)
1086 {
1087 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -04001088 {
1089 p1 = vlib_get_buffer (vm, from[1]);
1090 vlib_prefetch_buffer_header (p1, LOAD);
1091 }
1092
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093 bi0 = from[0];
1094 b0 = vlib_get_buffer (vm, bi0);
1095 from++;
1096 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -04001097
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -04001099 if (im->pcap_drop_filter_hash
1100 && hash_get (im->pcap_drop_filter_hash, b0->error))
1101 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102
Benoît Ganne30a81952021-02-26 13:47:41 +01001103 if (!vnet_is_packet_pcaped (pp, b0, ~0))
1104 continue; /* not matching, skip */
Dave Barachf5667c32019-09-25 11:27:46 -04001105
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 /* Trace all drops, or drops received on a specific interface */
Benoît Ganne30a81952021-02-26 13:47:41 +01001107 save_current_data = b0->current_data;
1108 save_current_length = b0->current_length;
1109
1110 /*
1111 * Typically, we'll need to rewind the buffer
1112 * if l2_hdr_offset is valid, make sure to rewind to the start of
1113 * the L2 header. This may not be the buffer start in case we pop-ed
1114 * vlan tags.
1115 * Otherwise, rewind to buffer start and hope for the best.
1116 */
1117 if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
Dave Barachba868bb2016-08-08 09:51:21 -04001118 {
Benoît Ganne30a81952021-02-26 13:47:41 +01001119 if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
1120 vlib_buffer_advance (b0, vnet_buffer (b0)->l2_hdr_offset -
1121 b0->current_data);
Dave Barachba868bb2016-08-08 09:51:21 -04001122 }
Benoît Ganne30a81952021-02-26 13:47:41 +01001123 else if (b0->current_data > 0)
1124 {
1125 vlib_buffer_advance (b0, (word) -b0->current_data);
1126 }
1127
1128 {
1129 vlib_buffer_t *last = b0;
1130 u32 error_node_index;
1131 int drop_string_len;
1132 vlib_node_t *n;
1133 /* Length of the error string */
1134 int error_string_len =
1135 clib_strnlen (em->counters_heap[b0->error].name, 128);
1136
1137 /* Dig up the drop node */
1138 error_node_index = vm->node_main.node_by_error[b0->error];
1139 n = vlib_get_node (vm, error_node_index);
1140
1141 /* Length of full drop string, w/ "nodename: " prepended */
1142 drop_string_len = error_string_len + vec_len (n->name) + 2;
1143
1144 /* Find the last buffer in the chain */
1145 while (last->flags & VLIB_BUFFER_NEXT_PRESENT)
1146 last = vlib_get_buffer (vm, last->next_buffer);
1147
1148 /*
1149 * Append <nodename>: <error-string> to the capture,
1150 * only if we can do that without allocating a new buffer.
1151 */
1152 if (PREDICT_TRUE ((last->current_data + last->current_length) <
1153 (VLIB_BUFFER_DEFAULT_DATA_SIZE - drop_string_len)))
1154 {
1155 clib_memcpy_fast (last->data + last->current_data +
1156 last->current_length,
1157 n->name, vec_len (n->name));
1158 clib_memcpy_fast (last->data + last->current_data +
1159 last->current_length + vec_len (n->name),
1160 ": ", 2);
1161 clib_memcpy_fast (last->data + last->current_data +
1162 last->current_length + vec_len (n->name) + 2,
1163 em->counters_heap[b0->error].name,
1164 error_string_len);
1165 last->current_length += drop_string_len;
1166 b0->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
1167 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
1168 last->current_length -= drop_string_len;
1169 b0->current_data = save_current_data;
1170 b0->current_length = save_current_length;
1171 continue;
1172 }
1173 }
1174
1175 /*
1176 * Didn't have space in the last buffer, here's the dropped
1177 * packet as-is
1178 */
1179 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
1180
1181 b0->current_data = save_current_data;
1182 b0->current_length = save_current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183 }
1184}
1185
Filip Tehlar62668772019-03-04 03:33:32 -08001186#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -04001187void
1188vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189{
Dave Barachba868bb2016-08-08 09:51:21 -04001190 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
1192 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001193 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
1195 if (is_add)
1196 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1197 else
1198 hash_unset (im->pcap_drop_filter_hash, error_index);
1199}
Filip Tehlar62668772019-03-04 03:33:32 -08001200#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001202VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
1203 vlib_node_runtime_t * node,
1204 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205{
Damjan Marion8fb5add2021-03-04 18:41:59 +01001206 vnet_main_t *vnm = vnet_get_main ();
Dave Barachba868bb2016-08-08 09:51:21 -04001207 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Damjan Marion8fb5add2021-03-04 18:41:59 +01001208 vnet_pcap_t *pp = &vnm->pcap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
Dave Barach33909772019-09-23 10:27:27 -04001210 if (PREDICT_FALSE (pp->pcap_drop_enable))
1211 pcap_drop_trace (vm, im, pp, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001213 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214}
1215
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001216VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
1217 vlib_node_runtime_t * node,
1218 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219{
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001220 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221}
1222
Dave Barachba868bb2016-08-08 09:51:21 -04001223/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001224VLIB_REGISTER_NODE (interface_drop) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 .name = "error-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226 .vector_size = sizeof (u32),
1227 .format_trace = format_vnet_error_trace,
Dave Barach4330c462020-06-10 17:07:32 -04001228 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001229 .n_next_nodes = 1,
1230 .next_nodes = {
1231 [0] = "drop",
1232 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233};
Dave Barachba868bb2016-08-08 09:51:21 -04001234/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235
Dave Barachba868bb2016-08-08 09:51:21 -04001236/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001237VLIB_REGISTER_NODE (interface_punt) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238 .name = "error-punt",
1239 .vector_size = sizeof (u32),
1240 .format_trace = format_vnet_error_trace,
Dave Barach4330c462020-06-10 17:07:32 -04001241 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001242 .n_next_nodes = 1,
1243 .next_nodes = {
1244 [0] = "punt",
1245 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246};
Dave Barachba868bb2016-08-08 09:51:21 -04001247/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Filip Tehlar62668772019-03-04 03:33:32 -08001249VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 .name = "interface-output",
1251 .vector_size = sizeof (u32),
1252};
1253
Damjan Marion8932e452021-04-16 11:49:26 +02001254VLIB_NODE_FN (vnet_interface_output_arc_end_node)
1255(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Damjan Marion152e21d2016-11-29 14:55:43 +01001256{
1257 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8932e452021-04-16 11:49:26 +02001258 vnet_interface_main_t *im = &vnm->interface_main;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001259 vnet_hw_interface_t *hi;
Damjan Marion8932e452021-04-16 11:49:26 +02001260 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001261 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index = sw_if_indices;
Damjan Marionefeea5b2022-02-10 15:01:03 +01001262 vlib_frame_bitmap_t used_elts = {}, mask = {};
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001263 u32 *tmp, *from, n_left, n_comp, n_p_comp, swif, off;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001264 u16 next_index;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001265 void *ptr[VLIB_FRAME_SIZE], **p = ptr;
Damjan Marion152e21d2016-11-29 14:55:43 +01001266
Damjan Marion8932e452021-04-16 11:49:26 +02001267 from = vlib_frame_vector_args (frame);
1268 n_left = frame->n_vectors;
1269 vlib_get_buffers (vm, from, bufs, n_left);
1270
1271 while (n_left >= 8)
Damjan Marion152e21d2016-11-29 14:55:43 +01001272 {
Damjan Marion8932e452021-04-16 11:49:26 +02001273 vlib_prefetch_buffer_header (b[4], LOAD);
1274 vlib_prefetch_buffer_header (b[5], LOAD);
1275 vlib_prefetch_buffer_header (b[6], LOAD);
1276 vlib_prefetch_buffer_header (b[7], LOAD);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001277
1278 p[0] = vlib_buffer_get_current (b[0]);
1279 p[1] = vlib_buffer_get_current (b[1]);
1280 p[2] = vlib_buffer_get_current (b[2]);
1281 p[3] = vlib_buffer_get_current (b[3]);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001282 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
1283 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
1284 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
1285 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
Damjan Marion152e21d2016-11-29 14:55:43 +01001286
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001287 p += 4;
Damjan Marion8932e452021-04-16 11:49:26 +02001288 b += 4;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001289 sw_if_index += 4;
Damjan Marion8932e452021-04-16 11:49:26 +02001290 n_left -= 4;
Damjan Marion152e21d2016-11-29 14:55:43 +01001291 }
Damjan Marion8932e452021-04-16 11:49:26 +02001292
1293 while (n_left)
1294 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001295 p[0] = vlib_buffer_get_current (b[0]);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001296 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001297 p++;
Damjan Marion8932e452021-04-16 11:49:26 +02001298 b++;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001299 sw_if_index++;
Damjan Marion8932e452021-04-16 11:49:26 +02001300 n_left--;
1301 }
1302
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001303 n_left = frame->n_vectors;
1304 swif = sw_if_indices[0];
1305 off = 0;
1306
1307 /* a bit ugly but it allows us to reuse stack space for temporary store
1308 * which may also improve memory latency */
1309 tmp = (u32 *) bufs;
1310
1311more:
1312 next_index = vec_elt (im->if_out_arc_end_next_index_by_sw_if_index, swif);
1313 hi = vnet_get_sup_hw_interface (vnm, swif);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001314 vnet_hw_if_output_node_runtime_t *r = 0;
1315 void *ptr_tmp[VLIB_FRAME_SIZE], **p_tmp = ptr_tmp;
1316
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001317 if (hi->output_node_thread_runtimes)
1318 r = vec_elt_at_index (hi->output_node_thread_runtimes, vm->thread_index);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001319
1320 /* compare and compress based on comparison mask */
1321 clib_mask_compare_u32 (swif, sw_if_indices, mask, frame->n_vectors);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001322 n_comp = clib_compress_u32 (tmp, from, mask, frame->n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001323
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001324 /*
1325 * tx queue of given interface is not available on given thread
1326 */
1327 if (r)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001328 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001329 if (r->n_queues == 0)
1330 {
1331 vlib_error_drop_buffers (
1332 vm, node, tmp,
1333 /* buffer stride */ 1, n_comp, VNET_INTERFACE_OUTPUT_NEXT_DROP,
1334 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_NO_TX_QUEUE);
1335 goto drop;
1336 }
1337 else if (r->n_queues > 1)
1338 {
1339 n_p_comp = clib_compress_u64 ((u64 *) p_tmp, (u64 *) ptr, mask,
1340 frame->n_vectors);
1341 ASSERT (n_p_comp == n_comp);
1342 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001343 }
1344
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001345 enqueue_to_tx_node (vm, node, hi, next_index, r, tmp, ptr_tmp, n_comp);
1346
1347drop:
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001348 n_left -= n_comp;
1349 if (n_left)
1350 {
1351 /* store comparison mask so we can find next unused element */
Damjan Marionefeea5b2022-02-10 15:01:03 +01001352 vlib_frame_bitmap_or (used_elts, mask);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001353
1354 /* fine first unused sw_if_index by scanning trough used_elts bitmap */
1355 while (PREDICT_FALSE (used_elts[off] == ~0))
1356 off++;
1357
1358 swif =
1359 sw_if_indices[(off << 6) + count_trailing_zeros (~used_elts[off])];
1360 goto more;
1361 }
1362
Damjan Marion8932e452021-04-16 11:49:26 +02001363 return frame->n_vectors;
Damjan Marion152e21d2016-11-29 14:55:43 +01001364}
1365
Damjan Marion8932e452021-04-16 11:49:26 +02001366VLIB_REGISTER_NODE (vnet_interface_output_arc_end_node) = {
1367 .name = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001368 .vector_size = sizeof (u32),
1369 .n_next_nodes = 1,
1370 .next_nodes = {
1371 [0] = "error-drop",
1372 },
1373};
1374
Damjan Marion8932e452021-04-16 11:49:26 +02001375VNET_FEATURE_ARC_INIT (interface_output, static) = {
1376 .arc_name = "interface-output",
Damjan Marion152e21d2016-11-29 14:55:43 +01001377 .start_nodes = VNET_FEATURES (0),
Damjan Marion8932e452021-04-16 11:49:26 +02001378 .last_in_arc = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001379 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1380};
1381
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001382VNET_FEATURE_INIT (span_tx, static) = {
1383 .arc_name = "interface-output",
1384 .node_name = "span-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001385 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001386};
1387
Matthew Smith537eeec2018-04-09 11:49:20 -05001388VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1389 .arc_name = "interface-output",
1390 .node_name = "ipsec-if-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001391 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
Matthew Smith537eeec2018-04-09 11:49:20 -05001392};
1393
Damjan Marion8932e452021-04-16 11:49:26 +02001394VNET_FEATURE_INIT (interface_output_arc_end, static) = {
Damjan Marion152e21d2016-11-29 14:55:43 +01001395 .arc_name = "interface-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001396 .node_name = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001397 .runs_before = 0,
1398};
Damjan Marion152e21d2016-11-29 14:55:43 +01001399
Filip Tehlar62668772019-03-04 03:33:32 -08001400#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401clib_error_t *
1402vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1403 u32 hw_if_index,
1404 u32 is_create)
1405{
Dave Barachba868bb2016-08-08 09:51:21 -04001406 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001407 u32 next_index;
1408
John Loe5453d02018-01-23 19:21:34 -05001409 if (hi->output_node_index == 0)
1410 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411
John Loe5453d02018-01-23 19:21:34 -05001412 next_index = vlib_node_add_next
1413 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1414 hi->output_node_index);
1415 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416
1417 return 0;
1418}
1419
Dave Barachba868bb2016-08-08 09:51:21 -04001420VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1421 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422
John Loe5453d02018-01-23 19:21:34 -05001423void
1424vnet_set_interface_output_node (vnet_main_t * vnm,
1425 u32 hw_if_index, u32 node_index)
1426{
1427 ASSERT (node_index);
1428 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1429 u32 next_index = vlib_node_add_next
1430 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1431 hi->output_node_next_index = next_index;
1432 hi->output_node_index = node_index;
1433}
Filip Tehlar62668772019-03-04 03:33:32 -08001434#endif /* CLIB_MARCH_VARIANT */
John Loe5453d02018-01-23 19:21:34 -05001435
Dave Barachba868bb2016-08-08 09:51:21 -04001436/*
1437 * fd.io coding-style-patch-verification: ON
1438 *
1439 * Local Variables:
1440 * eval: (c-set-style "gnu")
1441 * End:
1442 */