blob: 47844dcd68a5f168f18cd5ec4d5819b48e0f6287 [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);
luoyaozu863d1c82022-11-16 16:26:53 +080088 s = format (s, "%U flags 0x%08x", format_vnet_sw_interface_name, vnm,
89 si, t->flags);
Neale Ranns177bbdc2016-11-15 09:46:51 +000090 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020091 s =
92 format (s, "\n%U%U", format_white_space, indent,
93 node->format_buffer ? node->format_buffer : format_hex_bytes,
94 t->data, sizeof (t->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 }
96 return s;
97}
Benoît Ganne7d2094c2020-11-09 15:23:52 +010098#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100static void
101vnet_interface_output_trace (vlib_main_t * vm,
102 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -0400103 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104{
Dave Barachba868bb2016-08-08 09:51:21 -0400105 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100108 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400109
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 while (n_left >= 4)
111 {
112 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400113 vlib_buffer_t *b0, *b1;
114 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
116 /* Prefetch next iteration. */
117 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
118 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
119
120 bi0 = from[0];
121 bi1 = from[1];
122
123 b0 = vlib_get_buffer (vm, bi0);
124 b1 = vlib_get_buffer (vm, bi1);
125
126 if (b0->flags & VLIB_BUFFER_IS_TRACED)
127 {
128 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
129 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200130 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500131 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
132 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 }
134 if (b1->flags & VLIB_BUFFER_IS_TRACED)
135 {
136 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
137 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200138 t1->flags = b1->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500139 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
140 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 }
142 from += 2;
143 n_left -= 2;
144 }
145
146 while (n_left >= 1)
147 {
148 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400149 vlib_buffer_t *b0;
150 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
152 bi0 = from[0];
153
154 b0 = vlib_get_buffer (vm, bi0);
155
156 if (b0->flags & VLIB_BUFFER_IS_TRACED)
157 {
158 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
159 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200160 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500161 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
162 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 }
164 from += 1;
165 n_left -= 1;
166 }
167}
168
Damjan Marion4d2726e2021-03-23 21:05:18 +0100169static_always_inline void
170vnet_interface_output_handle_offload (vlib_main_t *vm, vlib_buffer_t *b)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400171{
Mohsin Kazmif21710c2022-04-01 13:42:20 +0000172 if (b->flags & VNET_BUFFER_F_GSO)
173 return;
Aloys Augustinac6c5282021-04-15 18:12:51 +0200174 vnet_calc_checksums_inline (vm, b, b->flags & VNET_BUFFER_F_IS_IP4,
175 b->flags & VNET_BUFFER_F_IS_IP6);
Mohsin Kazmi376c2102021-11-26 17:04:07 +0100176 vnet_calc_outer_checksums_inline (vm, b);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100177}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Damjan Marion4d2726e2021-03-23 21:05:18 +0100179static_always_inline uword
180vnet_interface_output_node_inline (vlib_main_t *vm, u32 sw_if_index,
181 vlib_combined_counter_main_t *ccm,
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000182 vlib_buffer_t **b, void **p,
183 u32 config_index, u8 arc, u32 n_left,
184 int processing_level)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100185{
186 u32 n_bytes = 0;
187 u32 n_bytes0, n_bytes1, n_bytes2, n_bytes3;
188 u32 ti = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Damjan Marion4d2726e2021-03-23 21:05:18 +0100190 while (n_left >= 8)
Damjan Marion152e21d2016-11-29 14:55:43 +0100191 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100192 u32 or_flags;
Damjan Marion152e21d2016-11-29 14:55:43 +0100193
Damjan Marion4d2726e2021-03-23 21:05:18 +0100194 /* Prefetch next iteration. */
195 vlib_prefetch_buffer_header (b[4], LOAD);
196 vlib_prefetch_buffer_header (b[5], LOAD);
197 vlib_prefetch_buffer_header (b[6], LOAD);
198 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000200 if (processing_level >= 1)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100201 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100202
203 /* Be grumpy about zero length buffers for benefit of
204 driver tx function. */
205 ASSERT (b[0]->current_length > 0);
206 ASSERT (b[1]->current_length > 0);
207 ASSERT (b[2]->current_length > 0);
208 ASSERT (b[3]->current_length > 0);
209
210 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
211 n_bytes += n_bytes1 = vlib_buffer_length_in_chain (vm, b[1]);
212 n_bytes += n_bytes2 = vlib_buffer_length_in_chain (vm, b[2]);
213 n_bytes += n_bytes3 = vlib_buffer_length_in_chain (vm, b[3]);
214
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000215 if (processing_level >= 3)
216 {
217 p[0] = vlib_buffer_get_current (b[0]);
218 p[1] = vlib_buffer_get_current (b[1]);
219 p[2] = vlib_buffer_get_current (b[2]);
220 p[3] = vlib_buffer_get_current (b[3]);
221 p += 4;
222 }
223
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000224 if (processing_level >= 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100226 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
227 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
228 tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
229 tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
230 tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
231
232 /* update vlan subif tx counts, if required */
233 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
234 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
235
236 if (PREDICT_FALSE (tx_swif1 != sw_if_index))
237 vlib_increment_combined_counter (ccm, ti, tx_swif1, 1, n_bytes1);
238
239 if (PREDICT_FALSE (tx_swif2 != sw_if_index))
240 vlib_increment_combined_counter (ccm, ti, tx_swif2, 1, n_bytes2);
241
242 if (PREDICT_FALSE (tx_swif3 != sw_if_index))
243 vlib_increment_combined_counter (ccm, ti, tx_swif3, 1, n_bytes3);
244
245 if (PREDICT_FALSE (config_index != ~0))
246 {
247 vnet_buffer (b[0])->feature_arc_index = arc;
248 b[0]->current_config_index = config_index;
249 vnet_buffer (b[1])->feature_arc_index = arc;
250 b[1]->current_config_index = config_index;
251 vnet_buffer (b[2])->feature_arc_index = arc;
252 b[2]->current_config_index = config_index;
253 vnet_buffer (b[3])->feature_arc_index = arc;
254 b[3]->current_config_index = config_index;
255 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 }
257
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000258 if (processing_level >= 1 && (or_flags & VNET_BUFFER_F_OFFLOAD))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100260 vnet_interface_output_handle_offload (vm, b[0]);
261 vnet_interface_output_handle_offload (vm, b[1]);
262 vnet_interface_output_handle_offload (vm, b[2]);
263 vnet_interface_output_handle_offload (vm, b[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 }
265
Damjan Marion4d2726e2021-03-23 21:05:18 +0100266 n_left -= 4;
267 b += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 }
269
Damjan Marion4d2726e2021-03-23 21:05:18 +0100270 while (n_left)
271 {
Damjan Marion4d2726e2021-03-23 21:05:18 +0100272 /* Be grumpy about zero length buffers for benefit of
273 driver tx function. */
274 ASSERT (b[0]->current_length > 0);
275
276 n_bytes += n_bytes0 = vlib_buffer_length_in_chain (vm, b[0]);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100277
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000278 if (processing_level >= 3)
279 {
280 p[0] = vlib_buffer_get_current (b[0]);
281 p += 1;
282 }
283
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000284 if (processing_level >= 2)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100285 {
Damjan Mariond4008cf2021-03-24 11:45:51 +0100286 u32 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100287
Damjan Mariond4008cf2021-03-24 11:45:51 +0100288 if (PREDICT_FALSE (config_index != ~0))
289 {
290 vnet_buffer (b[0])->feature_arc_index = arc;
291 b[0]->current_config_index = config_index;
292 }
293
294 if (PREDICT_FALSE (tx_swif0 != sw_if_index))
295 vlib_increment_combined_counter (ccm, ti, tx_swif0, 1, n_bytes0);
296 }
Damjan Marion4d2726e2021-03-23 21:05:18 +0100297
Mohsin Kazmi8f3415d2021-10-14 12:21:16 +0000298 if (processing_level >= 1)
Damjan Marion4d2726e2021-03-23 21:05:18 +0100299 vnet_interface_output_handle_offload (vm, b[0]);
300
301 n_left -= 1;
302 b += 1;
303 }
304
305 return n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306}
307
Benoît Ganne14331852021-07-15 19:21:31 +0200308static_always_inline void
309vnet_interface_pcap_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node,
310 vlib_frame_t *frame, int in_interface_ouput)
Dave Barach5ecd5a52019-02-25 15:27:28 -0500311{
Damjan Marion8fb5add2021-03-04 18:41:59 +0100312 vnet_main_t *vnm = vnet_get_main ();
Dave Barach5ecd5a52019-02-25 15:27:28 -0500313 u32 n_left_from, *from;
Benoît Ganne14331852021-07-15 19:21:31 +0200314 u32 sw_if_index = ~0, hw_if_index = ~0;
Damjan Marion8fb5add2021-03-04 18:41:59 +0100315 vnet_pcap_t *pp = &vnm->pcap;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500316
Dave Barach33909772019-09-23 10:27:27 -0400317 if (PREDICT_TRUE (pp->pcap_tx_enable == 0))
Dave Barach5ecd5a52019-02-25 15:27:28 -0500318 return;
319
Benoît Ganne14331852021-07-15 19:21:31 +0200320 if (in_interface_ouput)
321 {
322 /* interface-output is called right before interface-output-template.
323 * We only want to capture packets here if there is a per-interface
324 * filter, in case it matches the sub-interface sw_if_index.
325 * If there is no per-interface filter configured, let the
326 * interface-output-template node deal with it */
327 if (pp->pcap_sw_if_index == 0)
328 return;
329 }
330 else
Dave Barach5ecd5a52019-02-25 15:27:28 -0500331 {
332 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
333 sw_if_index = rt->sw_if_index;
334 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500335
336 n_left_from = frame->n_vectors;
337 from = vlib_frame_vector_args (frame);
338
339 while (n_left_from > 0)
340 {
341 u32 bi0 = from[0];
342 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Dave Barach9137e542019-09-13 17:47:50 -0400343 from++;
344 n_left_from--;
345
Benoît Ganne14331852021-07-15 19:21:31 +0200346 if (in_interface_ouput)
347 {
348 const u32 sii = vnet_buffer (b0)->sw_if_index[VLIB_TX];
349 if (PREDICT_FALSE (sii != sw_if_index))
350 {
351 const vnet_hw_interface_t *hi =
352 vnet_get_sup_hw_interface (vnm, sii);
353 hw_if_index = hi->sw_if_index;
354 sw_if_index = sii;
355 }
356 if (hw_if_index == sw_if_index)
357 continue; /* defer to interface-output-template */
358 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500359
Benoît Ganne30a81952021-02-26 13:47:41 +0100360 if (vnet_is_packet_pcaped (pp, b0, sw_if_index))
361 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500362 }
363}
364
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200365static_always_inline void
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000366hash_func_with_mask (void **p, u32 *hash, u32 n_packets, u32 *lookup_table,
367 u32 mask, vnet_hash_fn_t hf)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200368{
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000369 u32 n_left_from = n_packets;
370
371 hf (p, hash, n_packets);
372
373 clib_array_mask_u32 (hash, mask, n_packets);
374
375 while (n_left_from >= 4)
376 {
377 hash[0] = lookup_table[hash[0]];
378 hash[1] = lookup_table[hash[1]];
379 hash[2] = lookup_table[hash[2]];
380 hash[3] = lookup_table[hash[3]];
381
382 hash += 4;
383 n_left_from -= 4;
384 }
385
386 while (n_left_from > 0)
387 {
388 hash[0] = lookup_table[hash[0]];
389
390 hash += 1;
391 n_left_from -= 1;
392 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200393}
394
395static_always_inline void
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000396store_tx_frame_scalar_data (vnet_hw_if_tx_frame_t *copy_frame,
397 vnet_hw_if_tx_frame_t *tf)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200398{
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000399 if (copy_frame)
400 clib_memcpy_fast (tf, copy_frame, sizeof (vnet_hw_if_tx_frame_t));
401}
402
403static_always_inline u32
404enqueue_one_to_tx_node (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *ppqi,
405 u32 *from, vnet_hw_if_tx_frame_t *copy_frame,
406 u32 n_vectors, u32 n_left, u32 next_index)
407{
408 u32 tmp[VLIB_FRAME_SIZE];
Damjan Marionefeea5b2022-02-10 15:01:03 +0100409 vlib_frame_bitmap_t mask = {};
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200410 vlib_frame_t *f;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000411 vnet_hw_if_tx_frame_t *tf;
412 u32 *to;
413 u32 n_copy = 0, n_free = 0;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200414
415 f = vlib_get_next_frame_internal (vm, node, next_index, 0);
416 tf = vlib_frame_scalar_args (f);
417
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000418 if (f->n_vectors > 0 &&
419 (!copy_frame || (tf->queue_id == copy_frame->queue_id)))
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200420 {
421 /* append current next frame */
422 n_free = VLIB_FRAME_SIZE - f->n_vectors;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000423 /*
424 * if frame contains enough space for worst case scenario,
425 * we can avoid use of tmp
426 */
427 if (n_free >= n_left)
428 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
429 else
430 to = tmp;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200431 }
432 else
433 {
434 if (f->n_vectors > 0)
435 {
436 /* current frame doesn't fit - grab empty one */
437 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
438 tf = vlib_frame_scalar_args (f);
439 }
440
441 /* empty frame - store scalar data */
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000442 store_tx_frame_scalar_data (copy_frame, tf);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200443 to = vlib_frame_vector_args (f);
444 n_free = VLIB_FRAME_SIZE;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200445 }
446
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000447 /*
448 * per packet queue id array
449 * compare with given queue_id, if match, copy respective buffer index from
450 * -> to
451 */
452 if (ppqi)
453 {
454 clib_mask_compare_u32 (copy_frame->queue_id, ppqi, mask, n_vectors);
455 n_copy = clib_compress_u32 (to, from, mask, n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200456
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000457 if (n_copy == 0)
458 return n_left;
459 }
460 else
461 {
462 /*
463 * no work required, just copy all buffer indices from -> to
464 */
465 n_copy = n_left;
466 vlib_buffer_copy_indices (to, from, n_copy);
467 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200468
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000469 if (to != tmp)
470 {
471 /* indices already written to frame, just close it */
472 vlib_put_next_frame (vm, node, next_index, n_free - n_copy);
473 }
474 else if (n_free >= n_copy)
475 {
476 /* enough space in the existing frame */
477 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
478 vlib_buffer_copy_indices (to, tmp, n_copy);
479 vlib_put_next_frame (vm, node, next_index, n_free - n_copy);
480 }
481 else
482 {
483 /* full frame */
484 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
485 vlib_buffer_copy_indices (to, tmp, n_free);
486 vlib_put_next_frame (vm, node, next_index, 0);
487
488 /* second frame */
489 u32 n_2nd_frame = n_copy - n_free;
490 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
491 tf = vlib_frame_scalar_args (f);
492 /* empty frame - store scalar data */
493 store_tx_frame_scalar_data (copy_frame, tf);
494 to = vlib_frame_vector_args (f);
495 vlib_buffer_copy_indices (to, tmp + n_free, n_2nd_frame);
496 vlib_put_next_frame (vm, node, next_index,
497 VLIB_FRAME_SIZE - n_2nd_frame);
498 }
499
500 return n_left - n_copy;
501}
502
503static_always_inline void
504enqueue_to_tx_node (vlib_main_t *vm, vlib_node_runtime_t *node,
505 vnet_hw_interface_t *hi, u32 next_index,
506 vnet_hw_if_output_node_runtime_t *r, u32 *from, void **p,
507 u32 n_vectors)
508{
509 u32 n_left = n_vectors;
510
511 ASSERT (n_vectors <= VLIB_FRAME_SIZE);
512
513 /*
514 * backward compatible for drivers not integrated with new tx infra.
515 */
516 if (r == 0)
517 {
518 n_left = enqueue_one_to_tx_node (vm, node, NULL, from, NULL, n_vectors,
519 n_left, next_index);
520 }
521 /*
522 * only 1 tx queue of given interface is available on given thread
523 */
524 else if (r->n_queues == 1)
525 {
526 n_left = enqueue_one_to_tx_node (vm, node, NULL, from, r->frame,
527 n_vectors, n_left, next_index);
528 }
529 /*
530 * multi tx-queues use case
531 */
532 else if (r->n_queues > 1)
533 {
534 u32 qids[VLIB_FRAME_SIZE];
535
536 hash_func_with_mask (p, qids, n_vectors, r->lookup_table,
537 vec_len (r->lookup_table) - 1, hi->hf);
538
539 for (u32 i = 0; i < r->n_queues; i++)
540 {
541 n_left = enqueue_one_to_tx_node (vm, node, qids, from, &r->frame[i],
542 n_vectors, n_left, next_index);
543 if (n_left == 0)
544 break;
545 }
546 }
547 else
548 ASSERT (0);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200549}
550
Damjan Marionf91098e2021-03-09 16:28:15 +0100551VLIB_NODE_FN (vnet_interface_output_node)
552(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400553{
554 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion4d2726e2021-03-23 21:05:18 +0100555 vnet_interface_main_t *im = &vnm->interface_main;
556 vlib_combined_counter_main_t *ccm;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400557 vnet_hw_interface_t *hi;
Damjan Marion3853b262021-03-23 18:47:34 +0100558 vnet_sw_interface_t *si;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400559 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000560 vnet_hw_if_output_node_runtime_t *r = 0;
Damjan Marion3853b262021-03-23 18:47:34 +0100561 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
Damjan Marion4d2726e2021-03-23 21:05:18 +0100562 u32 n_bytes, n_buffers = frame->n_vectors;
563 u32 config_index = ~0;
564 u32 sw_if_index = rt->sw_if_index;
565 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
566 u32 ti = vm->thread_index;
567 u8 arc = im->output_feature_arc_index;
Damjan Mariond4008cf2021-03-24 11:45:51 +0100568 int arc_or_subif = 0;
569 int do_tx_offloads = 0;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000570 void *ptr[VLIB_FRAME_SIZE], **p = ptr;
571 u8 is_parr = 0;
Damjan Marion3853b262021-03-23 18:47:34 +0100572 u32 *from;
573
574 if (node->flags & VLIB_NODE_FLAG_TRACE)
575 vnet_interface_output_trace (vm, node, frame, n_buffers);
576
577 from = vlib_frame_vector_args (frame);
578
579 if (rt->is_deleted)
580 return vlib_error_drop_buffers (
581 vm, node, from,
582 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
583 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400584
Benoît Ganne14331852021-07-15 19:21:31 +0200585 vnet_interface_pcap_tx_trace (vm, node, frame, 0 /* in_interface_ouput */);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500586
Damjan Marion3853b262021-03-23 18:47:34 +0100587 vlib_get_buffers (vm, from, bufs, n_buffers);
588
Damjan Marion4d2726e2021-03-23 21:05:18 +0100589 si = vnet_get_sw_interface (vnm, sw_if_index);
590 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Damjan Marion3853b262021-03-23 18:47:34 +0100591
592 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
593 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
594 {
595 vlib_simple_counter_main_t *cm;
596
597 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
598 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100599 vlib_increment_simple_counter (cm, ti, sw_if_index, n_buffers);
Damjan Marion3853b262021-03-23 18:47:34 +0100600
601 return vlib_error_drop_buffers (
602 vm, node, from,
603 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
604 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
605 }
606
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000607 if (hi->output_node_thread_runtimes)
608 r = vec_elt_at_index (hi->output_node_thread_runtimes, vm->thread_index);
609
610 if (r)
611 {
612 /*
613 * tx queue of given interface is not available on given thread
614 */
615 if (r->n_queues == 0)
616 return vlib_error_drop_buffers (
617 vm, node, from,
618 /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP,
619 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_NO_TX_QUEUE);
620 /*
621 * multiple tx queues available on given thread
622 */
623 else if (r->n_queues > 1)
624 /* construct array of pointer */
625 is_parr = 1;
626 }
627
Damjan Marion4d2726e2021-03-23 21:05:18 +0100628 /* interface-output feature arc handling */
629 if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
630 {
631 vnet_feature_config_main_t *fcm;
632 fcm = vnet_feature_get_config_main (arc);
633 config_index = vnet_get_feature_config_index (arc, sw_if_index);
634 vnet_get_config_data (&fcm->config_main, &config_index, &next_index, 0);
Damjan Mariond4008cf2021-03-24 11:45:51 +0100635 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100636 }
Damjan Mariond4008cf2021-03-24 11:45:51 +0100637 else if (hash_elts (hi->sub_interface_sw_if_index_by_id))
638 arc_or_subif = 1;
Damjan Marion4d2726e2021-03-23 21:05:18 +0100639
640 ccm = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX;
641
Nathan Skrzypczak2e558232021-05-27 19:34:59 +0200642 /* if not all three flags IP4_,TCP_,UDP_CKSUM set, do compute them
643 * here before sending to the interface */
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100644 if ((hi->caps & VNET_HW_IF_CAP_TX_CKSUM) != VNET_HW_IF_CAP_TX_CKSUM)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100645 do_tx_offloads = 1;
646
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000647 // basic processing
648 if (do_tx_offloads == 0 && arc_or_subif == 0 && is_parr == 0)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100649 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000650 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 0);
651 // basic processing + tx offloads
652 else if (do_tx_offloads == 1 && arc_or_subif == 0 && is_parr == 0)
Damjan Mariond4008cf2021-03-24 11:45:51 +0100653 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000654 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 1);
655 // basic processing + tx offloads + vlans + arcs
656 else if (do_tx_offloads == 1 && arc_or_subif == 1 && is_parr == 0)
657 n_bytes = vnet_interface_output_node_inline (
658 vm, sw_if_index, ccm, bufs, NULL, config_index, arc, n_buffers, 2);
659 // basic processing + tx offloads + vlans + arcs + multi-txqs
Dave Barach2c0a4f42017-06-29 09:30:15 -0400660 else
Damjan Mariond4008cf2021-03-24 11:45:51 +0100661 n_bytes = vnet_interface_output_node_inline (
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000662 vm, sw_if_index, ccm, bufs, p, config_index, arc, n_buffers, 3);
Damjan Marion4d2726e2021-03-23 21:05:18 +0100663
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200664 from = vlib_frame_vector_args (frame);
665 if (PREDICT_TRUE (next_index == VNET_INTERFACE_OUTPUT_NEXT_TX))
666 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +0000667 enqueue_to_tx_node (vm, node, hi, next_index, r, from, ptr,
668 frame->n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +0200669 }
670 else
671 {
672 vlib_buffer_enqueue_to_single_next (vm, node, from, next_index,
673 frame->n_vectors);
674 }
Damjan Marion4d2726e2021-03-23 21:05:18 +0100675
676 /* Update main interface stats. */
677 vlib_increment_combined_counter (ccm, ti, sw_if_index, n_buffers, n_bytes);
678 return n_buffers;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400679}
Benoît Ganne7d2094c2020-11-09 15:23:52 +0100680
Damjan Marionf91098e2021-03-09 16:28:15 +0100681VLIB_REGISTER_NODE (vnet_interface_output_node) = {
682 .name = "interface-output-template",
683 .vector_size = sizeof (u32),
684};
Dave Barach2c0a4f42017-06-29 09:30:15 -0400685
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
Filip Tehlar62668772019-03-04 03:33:32 -0800687VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
688 vlib_node_runtime_t *
689 node,
690 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691{
Dave Barachba868bb2016-08-08 09:51:21 -0400692 vnet_main_t *vnm = vnet_get_main ();
693 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 u32 n_left_from, next_index;
695
Benoît Ganne14331852021-07-15 19:21:31 +0200696 vnet_interface_pcap_tx_trace (vm, node, frame, 1 /* in_interface_ouput */);
Dave Barach5ecd5a52019-02-25 15:27:28 -0500697
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698 n_left_from = frame->n_vectors;
699
Damjan Mariona3d59862018-11-10 10:23:00 +0100700 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 next_index = node->cached_next_index;
702
703 while (n_left_from > 0)
704 {
705 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
706
707 while (n_left_from >= 4 && n_left_to_next >= 2)
708 {
709 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400710 vlib_buffer_t *b0, *b1;
711 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
713 /* Prefetch next iteration. */
714 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
715 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
716
717 bi0 = from[0];
718 bi1 = from[1];
719 to_next[0] = bi0;
720 to_next[1] = bi1;
721 from += 2;
722 to_next += 2;
723 n_left_to_next -= 2;
724 n_left_from -= 2;
725
726 b0 = vlib_get_buffer (vm, bi0);
727 b1 = vlib_get_buffer (vm, bi1);
728
Dave Barachba868bb2016-08-08 09:51:21 -0400729 hi0 =
730 vnet_get_sup_hw_interface (vnm,
731 vnet_buffer (b0)->sw_if_index
732 [VLIB_TX]);
733 hi1 =
734 vnet_get_sup_hw_interface (vnm,
735 vnet_buffer (b1)->sw_if_index
736 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
John Loe5453d02018-01-23 19:21:34 -0500738 next0 = hi0->output_node_next_index;
739 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
Dave Barachba868bb2016-08-08 09:51:21 -0400741 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
742 n_left_to_next, bi0, bi1, next0,
743 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 }
745
746 while (n_left_from > 0 && n_left_to_next > 0)
747 {
748 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400749 vlib_buffer_t *b0;
750 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
752 bi0 = from[0];
753 to_next[0] = bi0;
754 from += 1;
755 to_next += 1;
756 n_left_to_next -= 1;
757 n_left_from -= 1;
758
759 b0 = vlib_get_buffer (vm, bi0);
760
Dave Barachba868bb2016-08-08 09:51:21 -0400761 hi0 =
762 vnet_get_sup_hw_interface (vnm,
763 vnet_buffer (b0)->sw_if_index
764 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
John Loe5453d02018-01-23 19:21:34 -0500766 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
Dave Barachba868bb2016-08-08 09:51:21 -0400768 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
769 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 }
771
772 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
773 }
774
775 return frame->n_vectors;
776}
777
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000778typedef struct vnet_error_trace_t_
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000780 u32 sw_if_index;
Dave Barach4330c462020-06-10 17:07:32 -0400781 i8 details_valid;
782 u8 is_ip6;
783 u8 pad[2];
784 u16 mactype;
785 ip46_address_t src, dst;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000786} vnet_error_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Dave Barachba868bb2016-08-08 09:51:21 -0400788static u8 *
789format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000791 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000793 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794
Dave Barach4330c462020-06-10 17:07:32 -0400795 /* Normal, non-catchup trace */
796 if (t->details_valid == 0)
797 {
798 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
799 vnet_get_main (), t->sw_if_index);
800 }
801 else if (t->details_valid == 1)
802 {
803 /* The trace capture code didn't understant the mactype */
804 s = format (s, "mactype 0x%4x (not decoded)", t->mactype);
805 }
806 else if (t->details_valid == 2)
807 {
808 /* Dump the src/dst addresses */
809 if (t->is_ip6 == 0)
810 s = format (s, "IP4: %U -> %U",
811 format_ip4_address, &t->src.ip4,
812 format_ip4_address, &t->dst.ip4);
813 else
814 s = format (s, "IP6: %U -> %U",
815 format_ip6_address, &t->src.ip6,
816 format_ip6_address, &t->dst.ip6);
817 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 return s;
819}
820
821static void
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000822interface_trace_buffers (vlib_main_t * vm,
823 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824{
Dave Barachba868bb2016-08-08 09:51:21 -0400825 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826
827 buffers = vlib_frame_vector_args (frame);
828 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400829
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830 while (n_left >= 4)
831 {
832 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400833 vlib_buffer_t *b0, *b1;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000834 vnet_error_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835
836 /* Prefetch next iteration. */
837 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
838 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
839
840 bi0 = buffers[0];
841 bi1 = buffers[1];
842
843 b0 = vlib_get_buffer (vm, bi0);
844 b1 = vlib_get_buffer (vm, bi1);
845
846 if (b0->flags & VLIB_BUFFER_IS_TRACED)
847 {
Dave Barach4330c462020-06-10 17:07:32 -0400848 t0 = vlib_add_trace (vm, node, b0,
849 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000850 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400851 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852 }
853 if (b1->flags & VLIB_BUFFER_IS_TRACED)
854 {
Dave Barach4330c462020-06-10 17:07:32 -0400855 t1 = vlib_add_trace (vm, node, b1,
856 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000857 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400858 t1->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 }
860 buffers += 2;
861 n_left -= 2;
862 }
863
864 while (n_left >= 1)
865 {
866 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400867 vlib_buffer_t *b0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000868 vnet_error_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869
870 bi0 = buffers[0];
871
872 b0 = vlib_get_buffer (vm, bi0);
873
874 if (b0->flags & VLIB_BUFFER_IS_TRACED)
875 {
Dave Barach4330c462020-06-10 17:07:32 -0400876 t0 = vlib_add_trace (vm, node, b0,
877 STRUCT_OFFSET_OF (vnet_error_trace_t, pad));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000878 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Dave Barach4330c462020-06-10 17:07:32 -0400879 t0->details_valid = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 }
881 buffers += 1;
882 n_left -= 1;
883 }
884}
885
Dave Barachba868bb2016-08-08 09:51:21 -0400886typedef enum
887{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 VNET_ERROR_DISPOSITION_DROP,
889 VNET_ERROR_DISPOSITION_PUNT,
890 VNET_ERROR_N_DISPOSITION,
891} vnet_error_disposition_t;
892
Dave Barach4330c462020-06-10 17:07:32 -0400893static void
894drop_catchup_trace (vlib_main_t * vm,
895 vlib_node_runtime_t * node, vlib_buffer_t * b)
896{
897 /* Can we safely rewind the buffer? If not, fagedaboudit */
898 if (b->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
899 {
900 vnet_error_trace_t *t;
901 ip4_header_t *ip4;
902 ip6_header_t *ip6;
903 ethernet_header_t *eh;
904 i16 delta;
905
906 t = vlib_add_trace (vm, node, b, sizeof (*t));
907 delta = vnet_buffer (b)->l2_hdr_offset - b->current_data;
908 vlib_buffer_advance (b, delta);
909
910 eh = vlib_buffer_get_current (b);
911 /* Save mactype */
912 t->mactype = clib_net_to_host_u16 (eh->type);
913 t->details_valid = 1;
914 switch (t->mactype)
915 {
916 case ETHERNET_TYPE_IP4:
917 ip4 = (void *) (eh + 1);
918 t->details_valid = 2;
919 t->is_ip6 = 0;
920 t->src.ip4.as_u32 = ip4->src_address.as_u32;
921 t->dst.ip4.as_u32 = ip4->dst_address.as_u32;
922 break;
923
924 case ETHERNET_TYPE_IP6:
925 ip6 = (void *) (eh + 1);
926 t->details_valid = 2;
927 t->is_ip6 = 1;
928 clib_memcpy_fast (t->src.as_u8, ip6->src_address.as_u8,
929 sizeof (ip6_address_t));
930 clib_memcpy_fast (t->dst.as_u8, ip6->dst_address.as_u8,
931 sizeof (ip6_address_t));
932 break;
933
934 default:
935 /* Dunno, do nothing, leave details_valid alone */
936 break;
937 }
938 /* Restore current data (probably unnecessary) */
939 vlib_buffer_advance (b, -delta);
940 }
941}
942
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943static_always_inline uword
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000944interface_drop_punt (vlib_main_t * vm,
945 vlib_node_runtime_t * node,
946 vlib_frame_t * frame,
947 vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000949 u32 *from, n_left, thread_index, *sw_if_index;
950 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
951 u32 sw_if_indices[VLIB_FRAME_SIZE];
Dave Barachba868bb2016-08-08 09:51:21 -0400952 vlib_simple_counter_main_t *cm;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000953 u16 nexts[VLIB_FRAME_SIZE];
Dave Barach4330c462020-06-10 17:07:32 -0400954 u32 n_trace;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000955 vnet_main_t *vnm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000957 vnm = vnet_get_main ();
958 thread_index = vm->thread_index;
959 from = vlib_frame_vector_args (frame);
960 n_left = frame->n_vectors;
961 b = bufs;
962 sw_if_index = sw_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000964 vlib_get_buffers (vm, from, bufs, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
Dave Barach4330c462020-06-10 17:07:32 -0400966 /* "trace add error-drop NNN?" */
967 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
968 {
969 /* If pkts aren't otherwise traced... */
970 if ((node->flags & VLIB_NODE_FLAG_TRACE) == 0)
971 {
972 /* Trace them from here */
973 node->flags |= VLIB_NODE_FLAG_TRACE;
974 while (n_trace && n_left)
975 {
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200976 if (PREDICT_TRUE
977 (vlib_trace_buffer (vm, node, 0 /* next_index */ , b[0],
978 0 /* follow chain */ )))
979 {
980 /*
981 * Here we have a wireshark dissector problem.
982 * Packets may be well-formed, or not. We
983 * must not blow chunks in any case.
984 *
985 * Try to produce trace records which will help
986 * folks understand what's going on.
987 */
988 drop_catchup_trace (vm, node, b[0]);
989 n_trace--;
990 }
Dave Barach4330c462020-06-10 17:07:32 -0400991 n_left--;
992 b++;
993 }
994 }
995
996 vlib_set_trace_count (vm, node, n_trace);
997 b = bufs;
998 n_left = frame->n_vectors;
999 }
1000
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001002 interface_trace_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -04001003
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001004 /* All going to drop regardless, this is just a counting exercise */
1005 clib_memset (nexts, 0, sizeof (nexts));
1006
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1008 (disposition == VNET_ERROR_DISPOSITION_PUNT
1009 ? VNET_INTERFACE_COUNTER_PUNT
1010 : VNET_INTERFACE_COUNTER_DROP));
1011
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001012 /* collect the array of interfaces first ... */
1013 while (n_left >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001015 if (n_left >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001017 /* Prefetch 8 ahead - there's not much going on in each iteration */
1018 vlib_prefetch_buffer_header (b[4], LOAD);
1019 vlib_prefetch_buffer_header (b[5], LOAD);
1020 vlib_prefetch_buffer_header (b[6], LOAD);
1021 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001023 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1024 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
1025 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
1026 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
1027
1028 sw_if_index += 4;
1029 n_left -= 4;
1030 b += 4;
1031 }
1032 while (n_left)
1033 {
1034 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1035
1036 sw_if_index += 1;
1037 n_left -= 1;
1038 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039 }
1040
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001041 /* ... then count against them in blocks */
1042 n_left = frame->n_vectors;
1043
1044 while (n_left)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 {
Dave Barachba868bb2016-08-08 09:51:21 -04001046 vnet_sw_interface_t *sw_if0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001047 u16 off, count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001049 off = frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001051 sw_if_index = sw_if_indices + off;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001053 count = clib_count_equal_u32 (sw_if_index, n_left);
1054 n_left -= count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001056 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001058 /* Increment super-interface drop/punt counters for
1059 sub-interfaces. */
1060 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
1061 if (sw_if0->sup_sw_if_index != sw_if_index[0])
1062 vlib_increment_simple_counter
1063 (cm, thread_index, sw_if0->sup_sw_if_index, count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 }
1065
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001066 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067
1068 return frame->n_vectors;
1069}
1070
Dave Barachba868bb2016-08-08 09:51:21 -04001071static inline void
1072pcap_drop_trace (vlib_main_t * vm,
Dave Barach33909772019-09-23 10:27:27 -04001073 vnet_interface_main_t * im,
1074 vnet_pcap_t * pp, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075{
Dave Barachba868bb2016-08-08 09:51:21 -04001076 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001078 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 u32 bi0;
1080 i16 save_current_data;
1081 u16 save_current_length;
Dave Barach9382ad92019-09-23 16:03:49 -04001082 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083
1084 from = vlib_frame_vector_args (f);
1085
1086 while (n_left > 0)
1087 {
1088 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -04001089 {
1090 p1 = vlib_get_buffer (vm, from[1]);
1091 vlib_prefetch_buffer_header (p1, LOAD);
1092 }
1093
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 bi0 = from[0];
1095 b0 = vlib_get_buffer (vm, bi0);
1096 from++;
1097 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -04001098
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -04001100 if (im->pcap_drop_filter_hash
1101 && hash_get (im->pcap_drop_filter_hash, b0->error))
1102 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
Benoît Ganne30a81952021-02-26 13:47:41 +01001104 if (!vnet_is_packet_pcaped (pp, b0, ~0))
1105 continue; /* not matching, skip */
Dave Barachf5667c32019-09-25 11:27:46 -04001106
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 /* Trace all drops, or drops received on a specific interface */
Benoît Ganne30a81952021-02-26 13:47:41 +01001108 save_current_data = b0->current_data;
1109 save_current_length = b0->current_length;
1110
1111 /*
1112 * Typically, we'll need to rewind the buffer
1113 * if l2_hdr_offset is valid, make sure to rewind to the start of
1114 * the L2 header. This may not be the buffer start in case we pop-ed
1115 * vlan tags.
1116 * Otherwise, rewind to buffer start and hope for the best.
1117 */
1118 if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
Dave Barachba868bb2016-08-08 09:51:21 -04001119 {
Benoît Ganne30a81952021-02-26 13:47:41 +01001120 if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
1121 vlib_buffer_advance (b0, vnet_buffer (b0)->l2_hdr_offset -
1122 b0->current_data);
Dave Barachba868bb2016-08-08 09:51:21 -04001123 }
Benoît Ganne30a81952021-02-26 13:47:41 +01001124 else if (b0->current_data > 0)
1125 {
1126 vlib_buffer_advance (b0, (word) -b0->current_data);
1127 }
1128
1129 {
1130 vlib_buffer_t *last = b0;
1131 u32 error_node_index;
1132 int drop_string_len;
1133 vlib_node_t *n;
1134 /* Length of the error string */
1135 int error_string_len =
1136 clib_strnlen (em->counters_heap[b0->error].name, 128);
1137
1138 /* Dig up the drop node */
1139 error_node_index = vm->node_main.node_by_error[b0->error];
1140 n = vlib_get_node (vm, error_node_index);
1141
1142 /* Length of full drop string, w/ "nodename: " prepended */
1143 drop_string_len = error_string_len + vec_len (n->name) + 2;
1144
1145 /* Find the last buffer in the chain */
1146 while (last->flags & VLIB_BUFFER_NEXT_PRESENT)
1147 last = vlib_get_buffer (vm, last->next_buffer);
1148
1149 /*
1150 * Append <nodename>: <error-string> to the capture,
1151 * only if we can do that without allocating a new buffer.
1152 */
1153 if (PREDICT_TRUE ((last->current_data + last->current_length) <
1154 (VLIB_BUFFER_DEFAULT_DATA_SIZE - drop_string_len)))
1155 {
1156 clib_memcpy_fast (last->data + last->current_data +
1157 last->current_length,
1158 n->name, vec_len (n->name));
1159 clib_memcpy_fast (last->data + last->current_data +
1160 last->current_length + vec_len (n->name),
1161 ": ", 2);
1162 clib_memcpy_fast (last->data + last->current_data +
1163 last->current_length + vec_len (n->name) + 2,
1164 em->counters_heap[b0->error].name,
1165 error_string_len);
1166 last->current_length += drop_string_len;
1167 b0->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
1168 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
1169 last->current_length -= drop_string_len;
1170 b0->current_data = save_current_data;
1171 b0->current_length = save_current_length;
1172 continue;
1173 }
1174 }
1175
1176 /*
1177 * Didn't have space in the last buffer, here's the dropped
1178 * packet as-is
1179 */
1180 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
1181
1182 b0->current_data = save_current_data;
1183 b0->current_length = save_current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 }
1185}
1186
Filip Tehlar62668772019-03-04 03:33:32 -08001187#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -04001188void
1189vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001190{
Dave Barachba868bb2016-08-08 09:51:21 -04001191 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
1193 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001194 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
1196 if (is_add)
1197 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1198 else
1199 hash_unset (im->pcap_drop_filter_hash, error_index);
1200}
Filip Tehlar62668772019-03-04 03:33:32 -08001201#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001203VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
1204 vlib_node_runtime_t * node,
1205 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206{
Damjan Marion8fb5add2021-03-04 18:41:59 +01001207 vnet_main_t *vnm = vnet_get_main ();
Dave Barachba868bb2016-08-08 09:51:21 -04001208 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Damjan Marion8fb5add2021-03-04 18:41:59 +01001209 vnet_pcap_t *pp = &vnm->pcap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210
Dave Barach33909772019-09-23 10:27:27 -04001211 if (PREDICT_FALSE (pp->pcap_drop_enable))
1212 pcap_drop_trace (vm, im, pp, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001214 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215}
1216
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001217VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
1218 vlib_node_runtime_t * node,
1219 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220{
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001221 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222}
1223
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};
1234
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001235VLIB_REGISTER_NODE (interface_punt) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236 .name = "error-punt",
1237 .vector_size = sizeof (u32),
1238 .format_trace = format_vnet_error_trace,
Dave Barach4330c462020-06-10 17:07:32 -04001239 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001240 .n_next_nodes = 1,
1241 .next_nodes = {
1242 [0] = "punt",
1243 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244};
1245
Filip Tehlar62668772019-03-04 03:33:32 -08001246VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247 .name = "interface-output",
1248 .vector_size = sizeof (u32),
1249};
1250
Damjan Marion8932e452021-04-16 11:49:26 +02001251VLIB_NODE_FN (vnet_interface_output_arc_end_node)
1252(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Damjan Marion152e21d2016-11-29 14:55:43 +01001253{
1254 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8932e452021-04-16 11:49:26 +02001255 vnet_interface_main_t *im = &vnm->interface_main;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001256 vnet_hw_interface_t *hi;
Damjan Marion8932e452021-04-16 11:49:26 +02001257 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001258 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index = sw_if_indices;
Damjan Marionefeea5b2022-02-10 15:01:03 +01001259 vlib_frame_bitmap_t used_elts = {}, mask = {};
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001260 u32 *tmp, *from, n_left, n_comp, n_p_comp, swif, off;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001261 u16 next_index;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001262 void *ptr[VLIB_FRAME_SIZE], **p = ptr;
Damjan Marion152e21d2016-11-29 14:55:43 +01001263
Damjan Marion8932e452021-04-16 11:49:26 +02001264 from = vlib_frame_vector_args (frame);
1265 n_left = frame->n_vectors;
1266 vlib_get_buffers (vm, from, bufs, n_left);
1267
1268 while (n_left >= 8)
Damjan Marion152e21d2016-11-29 14:55:43 +01001269 {
Damjan Marion8932e452021-04-16 11:49:26 +02001270 vlib_prefetch_buffer_header (b[4], LOAD);
1271 vlib_prefetch_buffer_header (b[5], LOAD);
1272 vlib_prefetch_buffer_header (b[6], LOAD);
1273 vlib_prefetch_buffer_header (b[7], LOAD);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001274
1275 p[0] = vlib_buffer_get_current (b[0]);
1276 p[1] = vlib_buffer_get_current (b[1]);
1277 p[2] = vlib_buffer_get_current (b[2]);
1278 p[3] = vlib_buffer_get_current (b[3]);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001279 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
1280 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
1281 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
1282 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
Damjan Marion152e21d2016-11-29 14:55:43 +01001283
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001284 p += 4;
Damjan Marion8932e452021-04-16 11:49:26 +02001285 b += 4;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001286 sw_if_index += 4;
Damjan Marion8932e452021-04-16 11:49:26 +02001287 n_left -= 4;
Damjan Marion152e21d2016-11-29 14:55:43 +01001288 }
Damjan Marion8932e452021-04-16 11:49:26 +02001289
1290 while (n_left)
1291 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001292 p[0] = vlib_buffer_get_current (b[0]);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001293 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001294 p++;
Damjan Marion8932e452021-04-16 11:49:26 +02001295 b++;
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001296 sw_if_index++;
Damjan Marion8932e452021-04-16 11:49:26 +02001297 n_left--;
1298 }
1299
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001300 n_left = frame->n_vectors;
1301 swif = sw_if_indices[0];
1302 off = 0;
1303
1304 /* a bit ugly but it allows us to reuse stack space for temporary store
1305 * which may also improve memory latency */
1306 tmp = (u32 *) bufs;
1307
1308more:
1309 next_index = vec_elt (im->if_out_arc_end_next_index_by_sw_if_index, swif);
1310 hi = vnet_get_sup_hw_interface (vnm, swif);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001311 vnet_hw_if_output_node_runtime_t *r = 0;
1312 void *ptr_tmp[VLIB_FRAME_SIZE], **p_tmp = ptr_tmp;
1313
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001314 if (hi->output_node_thread_runtimes)
1315 r = vec_elt_at_index (hi->output_node_thread_runtimes, vm->thread_index);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001316
1317 /* compare and compress based on comparison mask */
1318 clib_mask_compare_u32 (swif, sw_if_indices, mask, frame->n_vectors);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001319 n_comp = clib_compress_u32 (tmp, from, mask, frame->n_vectors);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001320
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001321 /*
1322 * tx queue of given interface is not available on given thread
1323 */
1324 if (r)
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001325 {
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001326 if (r->n_queues == 0)
1327 {
1328 vlib_error_drop_buffers (
1329 vm, node, tmp,
1330 /* buffer stride */ 1, n_comp, VNET_INTERFACE_OUTPUT_NEXT_DROP,
1331 node->node_index, VNET_INTERFACE_OUTPUT_ERROR_NO_TX_QUEUE);
1332 goto drop;
1333 }
1334 else if (r->n_queues > 1)
1335 {
1336 n_p_comp = clib_compress_u64 ((u64 *) p_tmp, (u64 *) ptr, mask,
1337 frame->n_vectors);
1338 ASSERT (n_p_comp == n_comp);
1339 }
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001340 }
1341
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001342 enqueue_to_tx_node (vm, node, hi, next_index, r, tmp, ptr_tmp, n_comp);
1343
1344drop:
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001345 n_left -= n_comp;
1346 if (n_left)
1347 {
1348 /* store comparison mask so we can find next unused element */
Damjan Marionefeea5b2022-02-10 15:01:03 +01001349 vlib_frame_bitmap_or (used_elts, mask);
Damjan Marion1bd6cbb2021-04-15 13:12:51 +02001350
1351 /* fine first unused sw_if_index by scanning trough used_elts bitmap */
1352 while (PREDICT_FALSE (used_elts[off] == ~0))
1353 off++;
1354
1355 swif =
1356 sw_if_indices[(off << 6) + count_trailing_zeros (~used_elts[off])];
1357 goto more;
1358 }
1359
Damjan Marion8932e452021-04-16 11:49:26 +02001360 return frame->n_vectors;
Damjan Marion152e21d2016-11-29 14:55:43 +01001361}
1362
Damjan Marion8932e452021-04-16 11:49:26 +02001363VLIB_REGISTER_NODE (vnet_interface_output_arc_end_node) = {
1364 .name = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001365 .vector_size = sizeof (u32),
1366 .n_next_nodes = 1,
1367 .next_nodes = {
1368 [0] = "error-drop",
1369 },
1370};
1371
Damjan Marion8932e452021-04-16 11:49:26 +02001372VNET_FEATURE_ARC_INIT (interface_output, static) = {
1373 .arc_name = "interface-output",
Damjan Marion152e21d2016-11-29 14:55:43 +01001374 .start_nodes = VNET_FEATURES (0),
Damjan Marion8932e452021-04-16 11:49:26 +02001375 .last_in_arc = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001376 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1377};
1378
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001379VNET_FEATURE_INIT (span_tx, static) = {
1380 .arc_name = "interface-output",
1381 .node_name = "span-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001382 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001383};
1384
Matthew Smith537eeec2018-04-09 11:49:20 -05001385VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1386 .arc_name = "interface-output",
1387 .node_name = "ipsec-if-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001388 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
Matthew Smith537eeec2018-04-09 11:49:20 -05001389};
1390
Damjan Marion8932e452021-04-16 11:49:26 +02001391VNET_FEATURE_INIT (interface_output_arc_end, static) = {
Damjan Marion152e21d2016-11-29 14:55:43 +01001392 .arc_name = "interface-output",
Damjan Marion8932e452021-04-16 11:49:26 +02001393 .node_name = "interface-output-arc-end",
Damjan Marion152e21d2016-11-29 14:55:43 +01001394 .runs_before = 0,
1395};
Damjan Marion152e21d2016-11-29 14:55:43 +01001396
Filip Tehlar62668772019-03-04 03:33:32 -08001397#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398clib_error_t *
1399vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1400 u32 hw_if_index,
1401 u32 is_create)
1402{
Dave Barachba868bb2016-08-08 09:51:21 -04001403 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404 u32 next_index;
1405
John Loe5453d02018-01-23 19:21:34 -05001406 if (hi->output_node_index == 0)
1407 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001408
John Loe5453d02018-01-23 19:21:34 -05001409 next_index = vlib_node_add_next
1410 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1411 hi->output_node_index);
1412 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001413
1414 return 0;
1415}
1416
Dave Barachba868bb2016-08-08 09:51:21 -04001417VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1418 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419
John Loe5453d02018-01-23 19:21:34 -05001420void
1421vnet_set_interface_output_node (vnet_main_t * vnm,
1422 u32 hw_if_index, u32 node_index)
1423{
1424 ASSERT (node_index);
1425 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1426 u32 next_index = vlib_node_add_next
1427 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1428 hi->output_node_next_index = next_index;
1429 hi->output_node_index = node_index;
1430}
Filip Tehlar62668772019-03-04 03:33:32 -08001431#endif /* CLIB_MARCH_VARIANT */
John Loe5453d02018-01-23 19:21:34 -05001432
Dave Barachba868bb2016-08-08 09:51:21 -04001433/*
1434 * fd.io coding-style-patch-verification: ON
1435 *
1436 * Local Variables:
1437 * eval: (c-set-style "gnu")
1438 * End:
1439 */