blob: beeb62a2db4dbd392920147b9b10928b63dceea8 [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>
42#include <vnet/ip/ip4.h>
43#include <vnet/ip/ip6.h>
44#include <vnet/udp/udp_packet.h>
Damjan Marion152e21d2016-11-29 14:55:43 +010045#include <vnet/feature/feature.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
Dave Barachba868bb2016-08-08 09:51:21 -040047typedef struct
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 u32 sw_if_index;
Chris Luke1c9f6ce2016-04-26 10:55:52 -040050 u8 data[128 - sizeof (u32)];
Dave Barachba868bb2016-08-08 09:51:21 -040051}
52interface_output_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Dave Barachba868bb2016-08-08 09:51:21 -040054u8 *
55format_vnet_interface_output_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070056{
57 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040058 vlib_node_t *node = va_arg (*va, vlib_node_t *);
59 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
60 vnet_main_t *vnm = vnet_get_main ();
61 vnet_sw_interface_t *si;
Christophe Fontained3c008d2017-10-02 18:10:54 +020062 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barachba868bb2016-08-08 09:51:21 -040064 if (t->sw_if_index != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 indent = format_get_indent (s);
Dave Barachba868bb2016-08-08 09:51:21 -040067
Neale Ranns177bbdc2016-11-15 09:46:51 +000068 if (pool_is_free_index
69 (vnm->interface_main.sw_interfaces, t->sw_if_index))
70 {
71 /* the interface may have been deleted by the time the trace is printed */
72 s = format (s, "sw_if_index: %d\n%U%U",
73 t->sw_if_index,
74 format_white_space, indent,
75 node->format_buffer ? node->
76 format_buffer : format_hex_bytes, t->data,
77 sizeof (t->data));
78 }
79 else
80 {
81 si = vnet_get_sw_interface (vnm, t->sw_if_index);
82
83 s = format (s, "%U\n%U%U",
84 format_vnet_sw_interface_name, vnm, si,
85 format_white_space, indent,
86 node->format_buffer ? node->
87 format_buffer : format_hex_bytes, t->data,
88 sizeof (t->data));
89 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 }
91 return s;
92}
93
94static void
95vnet_interface_output_trace (vlib_main_t * vm,
96 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -040097 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098{
Dave Barachba868bb2016-08-08 09:51:21 -040099 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100102 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400103
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 while (n_left >= 4)
105 {
106 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400107 vlib_buffer_t *b0, *b1;
108 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
110 /* Prefetch next iteration. */
111 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
112 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
113
114 bi0 = from[0];
115 bi1 = from[1];
116
117 b0 = vlib_get_buffer (vm, bi0);
118 b1 = vlib_get_buffer (vm, bi1);
119
120 if (b0->flags & VLIB_BUFFER_IS_TRACED)
121 {
122 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
123 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Dave Barach178cf492018-11-13 16:34:13 -0500124 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
125 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 }
127 if (b1->flags & VLIB_BUFFER_IS_TRACED)
128 {
129 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
130 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Dave Barach178cf492018-11-13 16:34:13 -0500131 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
132 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 }
134 from += 2;
135 n_left -= 2;
136 }
137
138 while (n_left >= 1)
139 {
140 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400141 vlib_buffer_t *b0;
142 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
144 bi0 = from[0];
145
146 b0 = vlib_get_buffer (vm, bi0);
147
148 if (b0->flags & VLIB_BUFFER_IS_TRACED)
149 {
150 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
151 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Dave Barach178cf492018-11-13 16:34:13 -0500152 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
153 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 }
155 from += 1;
156 n_left -= 1;
157 }
158}
159
Dave Barach2c0a4f42017-06-29 09:30:15 -0400160static_always_inline void
161calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Dave Barach2c0a4f42017-06-29 09:30:15 -0400163 ip4_header_t *ip4;
164 ip6_header_t *ip6;
165 tcp_header_t *th;
166 udp_header_t *uh;
167
168 int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
169 int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
170
171 ASSERT (!(is_ip4 && is_ip6));
172
173 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
174 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
175 th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
176 uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
177
178 if (is_ip4)
179 {
180 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
181 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
182 ip4->checksum = ip4_header_checksum (ip4);
183 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrej Kozemcak9d7570c2019-01-07 08:39:22 +0100184 {
185 th->checksum = 0;
186 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
187 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400188 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
189 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
190 }
191 if (is_ip6)
192 {
193 int bogus;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400194 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
195 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
196 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
197 uh->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
198 }
199
200 b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
201 b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
202 b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
203}
204
205static_always_inline uword
206vnet_interface_output_node_inline (vlib_main_t * vm,
207 vlib_node_runtime_t * node,
208 vlib_frame_t * frame, vnet_main_t * vnm,
209 vnet_hw_interface_t * hi,
210 int do_tx_offloads)
211{
Dave Barachba868bb2016-08-08 09:51:21 -0400212 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
213 vnet_sw_interface_t *si;
Dave Barachba868bb2016-08-08 09:51:21 -0400214 u32 n_left_to_tx, *from, *from_end, *to_tx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 u32 n_bytes, n_buffers, n_packets;
Damjan Marion363640d2017-03-06 11:53:10 +0100216 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
Damjan Marion586afd72017-04-05 19:18:20 +0200217 u32 thread_index = vm->thread_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400218 vnet_interface_main_t *im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
Damjan Marion152e21d2016-11-29 14:55:43 +0100220 u32 current_config_index = ~0;
221 u8 arc = im->output_feature_arc_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
223 n_buffers = frame->n_vectors;
224
225 if (node->flags & VLIB_NODE_FLAG_TRACE)
226 vnet_interface_output_trace (vm, node, frame, n_buffers);
227
Damjan Mariona3d59862018-11-10 10:23:00 +0100228 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
230 if (rt->is_deleted)
Dave Barachba868bb2016-08-08 09:51:21 -0400231 return vlib_error_drop_buffers (vm, node, from,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 /* buffer stride */ 1,
233 n_buffers,
234 VNET_INTERFACE_OUTPUT_NEXT_DROP,
235 node->node_index,
236 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
237
238 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
239 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
Steve Shin042a6212017-07-07 14:57:46 -0700240 if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
241 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
Dave Barachba868bb2016-08-08 09:51:21 -0400242 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 {
Dave Barachba868bb2016-08-08 09:51:21 -0400244 vlib_simple_counter_main_t *cm;
245
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
Dave Barachba868bb2016-08-08 09:51:21 -0400247 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion586afd72017-04-05 19:18:20 +0200248 vlib_increment_simple_counter (cm, thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400249 rt->sw_if_index, n_buffers);
250
251 return vlib_error_drop_buffers (vm, node, from,
252 /* buffer stride */ 1,
253 n_buffers,
254 VNET_INTERFACE_OUTPUT_NEXT_DROP,
255 node->node_index,
256 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 }
258
259 from_end = from + n_buffers;
260
261 /* Total byte count of all buffers. */
262 n_bytes = 0;
263 n_packets = 0;
264
Damjan Marion152e21d2016-11-29 14:55:43 +0100265 /* interface-output feature arc handling */
266 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
267 {
268 vnet_feature_config_main_t *fcm;
269 fcm = vnet_feature_get_config_main (arc);
270 current_config_index = vnet_get_feature_config_index (arc,
271 rt->sw_if_index);
272 vnet_get_config_data (&fcm->config_main, &current_config_index,
273 &next_index, 0);
274 }
275
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 while (from < from_end)
277 {
278 /* Get new next frame since previous incomplete frame may have less
Dave Barachba868bb2016-08-08 09:51:21 -0400279 than VNET_FRAME_SIZE vectors in it. */
280 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Damjan Marion363640d2017-03-06 11:53:10 +0100282 while (from + 8 <= from_end && n_left_to_tx >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 {
Damjan Marion363640d2017-03-06 11:53:10 +0100284 u32 bi0, bi1, bi2, bi3;
285 vlib_buffer_t *b0, *b1, *b2, *b3;
286 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400287 u32 or_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
289 /* Prefetch next iteration. */
Damjan Marion363640d2017-03-06 11:53:10 +0100290 vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
291 vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
292 vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
293 vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
295 bi0 = from[0];
296 bi1 = from[1];
Damjan Marion363640d2017-03-06 11:53:10 +0100297 bi2 = from[2];
298 bi3 = from[3];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 to_tx[0] = bi0;
300 to_tx[1] = bi1;
Damjan Marion363640d2017-03-06 11:53:10 +0100301 to_tx[2] = bi2;
302 to_tx[3] = bi3;
303 from += 4;
304 to_tx += 4;
305 n_left_to_tx -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
307 b0 = vlib_get_buffer (vm, bi0);
308 b1 = vlib_get_buffer (vm, bi1);
Damjan Marion363640d2017-03-06 11:53:10 +0100309 b2 = vlib_get_buffer (vm, bi2);
310 b3 = vlib_get_buffer (vm, bi3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312 /* Be grumpy about zero length buffers for benefit of
313 driver tx function. */
314 ASSERT (b0->current_length > 0);
315 ASSERT (b1->current_length > 0);
Damjan Marion363640d2017-03-06 11:53:10 +0100316 ASSERT (b2->current_length > 0);
317 ASSERT (b3->current_length > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Dave Barachba868bb2016-08-08 09:51:21 -0400319 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
320 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
Damjan Marion363640d2017-03-06 11:53:10 +0100321 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
322 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
Dave Barachba868bb2016-08-08 09:51:21 -0400323 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
324 tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Damjan Marion363640d2017-03-06 11:53:10 +0100325 tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
326 tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
328 n_bytes += n_bytes_b0 + n_bytes_b1;
Damjan Marion363640d2017-03-06 11:53:10 +0100329 n_bytes += n_bytes_b2 + n_bytes_b3;
330 n_packets += 4;
Dave Barachba868bb2016-08-08 09:51:21 -0400331
Damjan Marion152e21d2016-11-29 14:55:43 +0100332 if (PREDICT_FALSE (current_config_index != ~0))
333 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200334 vnet_buffer (b0)->feature_arc_index = arc;
335 vnet_buffer (b1)->feature_arc_index = arc;
336 vnet_buffer (b2)->feature_arc_index = arc;
337 vnet_buffer (b3)->feature_arc_index = arc;
Damjan Marion152e21d2016-11-29 14:55:43 +0100338 b0->current_config_index = current_config_index;
339 b1->current_config_index = current_config_index;
Damjan Marion363640d2017-03-06 11:53:10 +0100340 b2->current_config_index = current_config_index;
341 b3->current_config_index = current_config_index;
Damjan Marion152e21d2016-11-29 14:55:43 +0100342 }
343
Damjan Marion363640d2017-03-06 11:53:10 +0100344 /* update vlan subif tx counts, if required */
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100345 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400346 {
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100347 vlib_increment_combined_counter (im->combined_sw_if_counters +
348 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200349 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100350 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400351 }
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100352
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100353 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
354 {
355
356 vlib_increment_combined_counter (im->combined_sw_if_counters +
357 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200358 thread_index, tx_swif1, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100359 n_bytes_b1);
360 }
Damjan Marion363640d2017-03-06 11:53:10 +0100361
362 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
363 {
364
365 vlib_increment_combined_counter (im->combined_sw_if_counters +
366 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200367 thread_index, tx_swif2, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100368 n_bytes_b2);
369 }
370 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
371 {
372
373 vlib_increment_combined_counter (im->combined_sw_if_counters +
374 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200375 thread_index, tx_swif3, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100376 n_bytes_b3);
377 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400378
379 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
380
381 if (do_tx_offloads)
382 {
383 if (or_flags &
384 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
385 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
386 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
387 {
388 calc_checksums (vm, b0);
389 calc_checksums (vm, b1);
390 calc_checksums (vm, b2);
391 calc_checksums (vm, b3);
392 }
393 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 }
395
396 while (from + 1 <= from_end && n_left_to_tx >= 1)
397 {
398 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400399 vlib_buffer_t *b0;
400 u32 tx_swif0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
402 bi0 = from[0];
403 to_tx[0] = bi0;
404 from += 1;
405 to_tx += 1;
406 n_left_to_tx -= 1;
407
408 b0 = vlib_get_buffer (vm, bi0);
409
410 /* Be grumpy about zero length buffers for benefit of
411 driver tx function. */
412 ASSERT (b0->current_length > 0);
413
Dave Barachba868bb2016-08-08 09:51:21 -0400414 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
415 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 n_bytes += n_bytes_b0;
417 n_packets += 1;
418
Damjan Marion152e21d2016-11-29 14:55:43 +0100419 if (PREDICT_FALSE (current_config_index != ~0))
420 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200421 vnet_buffer (b0)->feature_arc_index = arc;
Damjan Marion152e21d2016-11-29 14:55:43 +0100422 b0->current_config_index = current_config_index;
423 }
424
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100425 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400426 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100428 vlib_increment_combined_counter (im->combined_sw_if_counters +
429 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200430 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100431 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400432 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400433
434 if (do_tx_offloads)
435 calc_checksums (vm, b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 }
437
Dave Barachba868bb2016-08-08 09:51:21 -0400438 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 }
440
441 /* Update main interface stats. */
442 vlib_increment_combined_counter (im->combined_sw_if_counters
Dave Barachba868bb2016-08-08 09:51:21 -0400443 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200444 thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400445 rt->sw_if_index, n_packets, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 return n_buffers;
447}
448
Damjan Marion652d2e12019-02-02 00:15:27 +0100449uword
Dave Barach2c0a4f42017-06-29 09:30:15 -0400450vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
451 vlib_frame_t * frame)
452{
453 vnet_main_t *vnm = vnet_get_main ();
454 vnet_hw_interface_t *hi;
455 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
456 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
457
458 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
459 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
460 /* do_tx_offloads */ 0);
461 else
462 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
463 /* do_tx_offloads */ 1);
464}
465
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
467static uword
468vnet_per_buffer_interface_output (vlib_main_t * vm,
469 vlib_node_runtime_t * node,
470 vlib_frame_t * frame)
471{
Dave Barachba868bb2016-08-08 09:51:21 -0400472 vnet_main_t *vnm = vnet_get_main ();
473 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 u32 n_left_from, next_index;
475
476 n_left_from = frame->n_vectors;
477
Damjan Mariona3d59862018-11-10 10:23:00 +0100478 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 next_index = node->cached_next_index;
480
481 while (n_left_from > 0)
482 {
483 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
484
485 while (n_left_from >= 4 && n_left_to_next >= 2)
486 {
487 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400488 vlib_buffer_t *b0, *b1;
489 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
491 /* Prefetch next iteration. */
492 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
493 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
494
495 bi0 = from[0];
496 bi1 = from[1];
497 to_next[0] = bi0;
498 to_next[1] = bi1;
499 from += 2;
500 to_next += 2;
501 n_left_to_next -= 2;
502 n_left_from -= 2;
503
504 b0 = vlib_get_buffer (vm, bi0);
505 b1 = vlib_get_buffer (vm, bi1);
506
Dave Barachba868bb2016-08-08 09:51:21 -0400507 hi0 =
508 vnet_get_sup_hw_interface (vnm,
509 vnet_buffer (b0)->sw_if_index
510 [VLIB_TX]);
511 hi1 =
512 vnet_get_sup_hw_interface (vnm,
513 vnet_buffer (b1)->sw_if_index
514 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
John Loe5453d02018-01-23 19:21:34 -0500516 next0 = hi0->output_node_next_index;
517 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
Dave Barachba868bb2016-08-08 09:51:21 -0400519 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
520 n_left_to_next, bi0, bi1, next0,
521 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 }
523
524 while (n_left_from > 0 && n_left_to_next > 0)
525 {
526 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400527 vlib_buffer_t *b0;
528 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530 bi0 = from[0];
531 to_next[0] = bi0;
532 from += 1;
533 to_next += 1;
534 n_left_to_next -= 1;
535 n_left_from -= 1;
536
537 b0 = vlib_get_buffer (vm, bi0);
538
Dave Barachba868bb2016-08-08 09:51:21 -0400539 hi0 =
540 vnet_get_sup_hw_interface (vnm,
541 vnet_buffer (b0)->sw_if_index
542 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
John Loe5453d02018-01-23 19:21:34 -0500544 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545
Dave Barachba868bb2016-08-08 09:51:21 -0400546 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
547 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 }
549
550 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
551 }
552
553 return frame->n_vectors;
554}
555
556always_inline u32
557counter_index (vlib_main_t * vm, vlib_error_t e)
558{
Dave Barachba868bb2016-08-08 09:51:21 -0400559 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 u32 ci, ni;
561
562 ni = vlib_error_get_node (e);
563 n = vlib_get_node (vm, ni);
564
565 ci = vlib_error_get_code (e);
566 ASSERT (ci < n->n_errors);
567
568 ci += n->error_heap_index;
569
570 return ci;
571}
572
Dave Barachba868bb2016-08-08 09:51:21 -0400573static u8 *
574format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575{
Dave Barachba868bb2016-08-08 09:51:21 -0400576 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Dave Barachba868bb2016-08-08 09:51:21 -0400578 vlib_error_t *e = va_arg (*va, vlib_error_t *);
579 vlib_node_t *error_node;
580 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 u32 i;
582
583 error_node = vlib_get_node (vm, vlib_error_get_node (e[0]));
584 i = counter_index (vm, e[0]);
585 s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]);
586
587 return s;
588}
589
590static void
591trace_errors_with_buffers (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400592 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593{
Dave Barachba868bb2016-08-08 09:51:21 -0400594 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
596 buffers = vlib_frame_vector_args (frame);
597 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400598
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 while (n_left >= 4)
600 {
601 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400602 vlib_buffer_t *b0, *b1;
603 vlib_error_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
605 /* Prefetch next iteration. */
606 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
607 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
608
609 bi0 = buffers[0];
610 bi1 = buffers[1];
611
612 b0 = vlib_get_buffer (vm, bi0);
613 b1 = vlib_get_buffer (vm, bi1);
614
615 if (b0->flags & VLIB_BUFFER_IS_TRACED)
616 {
617 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
618 t0[0] = b0->error;
619 }
620 if (b1->flags & VLIB_BUFFER_IS_TRACED)
621 {
622 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
623 t1[0] = b1->error;
624 }
625 buffers += 2;
626 n_left -= 2;
627 }
628
629 while (n_left >= 1)
630 {
631 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400632 vlib_buffer_t *b0;
633 vlib_error_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
635 bi0 = buffers[0];
636
637 b0 = vlib_get_buffer (vm, bi0);
638
639 if (b0->flags & VLIB_BUFFER_IS_TRACED)
640 {
641 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
642 t0[0] = b0->error;
643 }
644 buffers += 1;
645 n_left -= 1;
646 }
647}
648
649static u8 *
650validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
651{
652 uword node_index = vlib_error_get_node (e[0]);
653 uword code = vlib_error_get_code (e[0]);
Dave Barachba868bb2016-08-08 09:51:21 -0400654 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655
656 if (node_index >= vec_len (vm->node_main.nodes))
657 return format (0, "[%d], node index out of range 0x%x, error 0x%x",
658 index, node_index, e[0]);
659
660 n = vlib_get_node (vm, node_index);
661 if (code >= n->n_errors)
662 return format (0, "[%d], code %d out of range for node %v",
663 index, code, n->name);
664
665 return 0;
666}
667
668static u8 *
669validate_error_frame (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400670 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671{
Damjan Mariona3d59862018-11-10 10:23:00 +0100672 u32 *buffers = vlib_frame_vector_args (f);
Dave Barachba868bb2016-08-08 09:51:21 -0400673 vlib_buffer_t *b;
674 u8 *msg = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 uword i;
676
677 for (i = 0; i < f->n_vectors; i++)
678 {
679 b = vlib_get_buffer (vm, buffers[i]);
680 msg = validate_error (vm, &b->error, i);
681 if (msg)
682 return msg;
683 }
684
685 return msg;
686}
687
Dave Barachba868bb2016-08-08 09:51:21 -0400688typedef enum
689{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 VNET_ERROR_DISPOSITION_DROP,
691 VNET_ERROR_DISPOSITION_PUNT,
692 VNET_ERROR_N_DISPOSITION,
693} vnet_error_disposition_t;
694
695always_inline void
696do_packet (vlib_main_t * vm, vlib_error_t a)
697{
Dave Barachba868bb2016-08-08 09:51:21 -0400698 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 u32 i = counter_index (vm, a);
700 em->counters[i] += 1;
701 vlib_error_elog_count (vm, i, 1);
702}
Dave Barachba868bb2016-08-08 09:51:21 -0400703
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704static_always_inline uword
705process_drop_punt (vlib_main_t * vm,
706 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -0400707 vlib_frame_t * frame, vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708{
Dave Barachba868bb2016-08-08 09:51:21 -0400709 vnet_main_t *vnm = vnet_get_main ();
710 vlib_error_main_t *em = &vm->error_main;
711 u32 *buffers, *first_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 vlib_error_t current_error;
713 u32 current_counter_index, n_errors_left;
714 u32 current_sw_if_index, n_errors_current_sw_if_index;
715 u64 current_counter;
Dave Barachba868bb2016-08-08 09:51:21 -0400716 vlib_simple_counter_main_t *cm;
Damjan Marion586afd72017-04-05 19:18:20 +0200717 u32 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
719 static vlib_error_t memory[VNET_ERROR_N_DISPOSITION];
720 static char memory_init[VNET_ERROR_N_DISPOSITION];
721
Damjan Mariona3d59862018-11-10 10:23:00 +0100722 buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 first_buffer = buffers;
724
725 {
Dave Barachba868bb2016-08-08 09:51:21 -0400726 vlib_buffer_t *b = vlib_get_buffer (vm, first_buffer[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
Dave Barachba868bb2016-08-08 09:51:21 -0400728 if (!memory_init[disposition])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 {
730 memory_init[disposition] = 1;
731 memory[disposition] = b->error;
732 }
733
734 current_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
735 n_errors_current_sw_if_index = 0;
736 }
737
738 current_error = memory[disposition];
739 current_counter_index = counter_index (vm, memory[disposition]);
740 current_counter = em->counters[current_counter_index];
741
742 if (node->flags & VLIB_NODE_FLAG_TRACE)
743 trace_errors_with_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400744
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 n_errors_left = frame->n_vectors;
746 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
747 (disposition == VNET_ERROR_DISPOSITION_PUNT
748 ? VNET_INTERFACE_COUNTER_PUNT
749 : VNET_INTERFACE_COUNTER_DROP));
750
751 while (n_errors_left >= 2)
752 {
Dave Barachba868bb2016-08-08 09:51:21 -0400753 vlib_buffer_t *b0, *b1;
754 vnet_sw_interface_t *sw_if0, *sw_if1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 vlib_error_t e0, e1;
756 u32 bi0, bi1;
757 u32 sw_if_index0, sw_if_index1;
758
759 bi0 = buffers[0];
760 bi1 = buffers[1];
761
762 buffers += 2;
763 n_errors_left -= 2;
764
765 b0 = vlib_get_buffer (vm, bi0);
766 b1 = vlib_get_buffer (vm, bi1);
767
768 e0 = b0->error;
769 e1 = b1->error;
770
771 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
772 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
773
774 /* Speculate that sw_if_index == sw_if_index[01]. */
775 n_errors_current_sw_if_index += 2;
776
777 /* Speculatively assume all 2 (node, code) pairs are equal
Dave Barachba868bb2016-08-08 09:51:21 -0400778 to current (node, code). */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 current_counter += 2;
780
781 if (PREDICT_FALSE (e0 != current_error
782 || e1 != current_error
783 || sw_if_index0 != current_sw_if_index
784 || sw_if_index1 != current_sw_if_index))
785 {
786 current_counter -= 2;
787 n_errors_current_sw_if_index -= 2;
788
Damjan Marion586afd72017-04-05 19:18:20 +0200789 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
790 vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
792 /* Increment super-interface drop/punt counters for
793 sub-interfaces. */
794 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
795 vlib_increment_simple_counter
Damjan Marion586afd72017-04-05 19:18:20 +0200796 (cm, thread_index, sw_if0->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400797 sw_if0->sup_sw_if_index != sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
799 sw_if1 = vnet_get_sw_interface (vnm, sw_if_index1);
800 vlib_increment_simple_counter
Damjan Marion586afd72017-04-05 19:18:20 +0200801 (cm, thread_index, sw_if1->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400802 sw_if1->sup_sw_if_index != sw_if_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
804 em->counters[current_counter_index] = current_counter;
805 do_packet (vm, e0);
806 do_packet (vm, e1);
807
808 /* For 2 repeated errors, change current error. */
809 if (e0 == e1 && e1 != current_error)
810 {
811 current_error = e0;
812 current_counter_index = counter_index (vm, e0);
813 }
814 current_counter = em->counters[current_counter_index];
815 }
816 }
817
818 while (n_errors_left >= 1)
819 {
Dave Barachba868bb2016-08-08 09:51:21 -0400820 vlib_buffer_t *b0;
821 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822 vlib_error_t e0;
823 u32 bi0, sw_if_index0;
824
825 bi0 = buffers[0];
826
827 buffers += 1;
828 n_errors_left -= 1;
829 current_counter += 1;
830
831 b0 = vlib_get_buffer (vm, bi0);
832 e0 = b0->error;
833
834 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
835
836 /* Increment drop/punt counters. */
Damjan Marion586afd72017-04-05 19:18:20 +0200837 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
839 /* Increment super-interface drop/punt counters for sub-interfaces. */
840 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
Damjan Marion586afd72017-04-05 19:18:20 +0200841 vlib_increment_simple_counter (cm, thread_index,
842 sw_if0->sup_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 sw_if0->sup_sw_if_index != sw_if_index0);
844
845 if (PREDICT_FALSE (e0 != current_error))
846 {
847 current_counter -= 1;
848
849 vlib_error_elog_count (vm, current_counter_index,
850 (current_counter
851 - em->counters[current_counter_index]));
Dave Barachba868bb2016-08-08 09:51:21 -0400852
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 em->counters[current_counter_index] = current_counter;
854
855 do_packet (vm, e0);
856 current_error = e0;
857 current_counter_index = counter_index (vm, e0);
858 current_counter = em->counters[current_counter_index];
859 }
860 }
861
862 if (n_errors_current_sw_if_index > 0)
863 {
Dave Barachba868bb2016-08-08 09:51:21 -0400864 vnet_sw_interface_t *si;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Damjan Marion586afd72017-04-05 19:18:20 +0200866 vlib_increment_simple_counter (cm, thread_index, current_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 n_errors_current_sw_if_index);
868
869 si = vnet_get_sw_interface (vnm, current_sw_if_index);
870 if (si->sup_sw_if_index != current_sw_if_index)
Damjan Marion586afd72017-04-05 19:18:20 +0200871 vlib_increment_simple_counter (cm, thread_index, si->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400872 n_errors_current_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 }
874
875 vlib_error_elog_count (vm, current_counter_index,
876 (current_counter
877 - em->counters[current_counter_index]));
878
879 /* Return cached counter. */
880 em->counters[current_counter_index] = current_counter;
881
882 /* Save memory for next iteration. */
883 memory[disposition] = current_error;
884
Dave Barachba868bb2016-08-08 09:51:21 -0400885 if (disposition == VNET_ERROR_DISPOSITION_DROP || !vm->os_punt_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 {
Dave Barachba868bb2016-08-08 09:51:21 -0400887 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
889 /* If there is no punt function, free the frame as well. */
Dave Barachba868bb2016-08-08 09:51:21 -0400890 if (disposition == VNET_ERROR_DISPOSITION_PUNT && !vm->os_punt_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 vlib_frame_free (vm, node, frame);
892 }
893 else
894 vm->os_punt_frame (vm, node, frame);
895
896 return frame->n_vectors;
897}
898
Dave Barachba868bb2016-08-08 09:51:21 -0400899static inline void
900pcap_drop_trace (vlib_main_t * vm,
901 vnet_interface_main_t * im, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902{
Dave Barachba868bb2016-08-08 09:51:21 -0400903 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400905 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 u32 bi0;
907 i16 save_current_data;
908 u16 save_current_length;
909
910 from = vlib_frame_vector_args (f);
911
912 while (n_left > 0)
913 {
914 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -0400915 {
916 p1 = vlib_get_buffer (vm, from[1]);
917 vlib_prefetch_buffer_header (p1, LOAD);
918 }
919
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 bi0 = from[0];
921 b0 = vlib_get_buffer (vm, bi0);
922 from++;
923 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -0400924
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -0400926 if (im->pcap_drop_filter_hash
927 && hash_get (im->pcap_drop_filter_hash, b0->error))
928 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929
930 /* Trace all drops, or drops received on a specific interface */
931 if (im->pcap_sw_if_index == 0 ||
Dave Barachba868bb2016-08-08 09:51:21 -0400932 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
933 {
934 save_current_data = b0->current_data;
935 save_current_length = b0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
Dave Barachba868bb2016-08-08 09:51:21 -0400937 /*
938 * Typically, we'll need to rewind the buffer
939 */
940 if (b0->current_data > 0)
941 vlib_buffer_advance (b0, (word) - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942
Dave Barachba868bb2016-08-08 09:51:21 -0400943 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
944
945 b0->current_data = save_current_data;
946 b0->current_length = save_current_length;
947 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 }
949}
950
Dave Barachba868bb2016-08-08 09:51:21 -0400951void
952vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953{
Dave Barachba868bb2016-08-08 09:51:21 -0400954 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
956 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400957 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958
959 if (is_add)
960 hash_set (im->pcap_drop_filter_hash, error_index, 1);
961 else
962 hash_unset (im->pcap_drop_filter_hash, error_index);
963}
964
965static uword
966process_drop (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400967 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968{
Dave Barachba868bb2016-08-08 09:51:21 -0400969 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970
971 if (PREDICT_FALSE (im->drop_pcap_enable))
972 pcap_drop_trace (vm, im, frame);
973
974 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
975}
976
977static uword
978process_punt (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400979 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980{
981 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
982}
983
Dave Barachba868bb2016-08-08 09:51:21 -0400984/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985VLIB_REGISTER_NODE (drop_buffers,static) = {
986 .function = process_drop,
987 .name = "error-drop",
988 .flags = VLIB_NODE_FLAG_IS_DROP,
989 .vector_size = sizeof (u32),
990 .format_trace = format_vnet_error_trace,
991 .validate_frame = validate_error_frame,
992};
Dave Barachba868bb2016-08-08 09:51:21 -0400993/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
Dave Barachba868bb2016-08-08 09:51:21 -0400995VLIB_NODE_FUNCTION_MULTIARCH (drop_buffers, process_drop);
Damjan Marion1c80e832016-05-11 23:07:18 +0200996
Dave Barachba868bb2016-08-08 09:51:21 -0400997/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998VLIB_REGISTER_NODE (punt_buffers,static) = {
999 .function = process_punt,
1000 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
1001 | VLIB_NODE_FLAG_IS_PUNT),
1002 .name = "error-punt",
1003 .vector_size = sizeof (u32),
1004 .format_trace = format_vnet_error_trace,
1005 .validate_frame = validate_error_frame,
1006};
Dave Barachba868bb2016-08-08 09:51:21 -04001007/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
Dave Barachba868bb2016-08-08 09:51:21 -04001009VLIB_NODE_FUNCTION_MULTIARCH (punt_buffers, process_punt);
Damjan Marion1c80e832016-05-11 23:07:18 +02001010
Dave Barachba868bb2016-08-08 09:51:21 -04001011/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node,static) = {
1013 .function = vnet_per_buffer_interface_output,
1014 .name = "interface-output",
1015 .vector_size = sizeof (u32),
1016};
Dave Barachba868bb2016-08-08 09:51:21 -04001017/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018
Dave Barach87040972019-01-24 15:12:53 -05001019/* Convenience node to drop a vector of buffers with a "misc error". */
1020static uword
1021misc_drop_buffers (vlib_main_t * vm,
1022 vlib_node_runtime_t * node, vlib_frame_t * frame)
1023{
1024 return vlib_error_drop_buffers (vm, node, vlib_frame_vector_args (frame),
1025 /* buffer stride */ 1,
1026 frame->n_vectors,
1027 /* next */ 0,
1028 node->node_index,
1029 /* error */ 0);
1030}
1031
1032static char *misc_drop_buffers_error_strings[] = {
1033 [0] = "misc. errors",
1034};
1035
1036/* *INDENT-OFF* */
1037VLIB_REGISTER_NODE (misc_drop_buffers_node,static) = {
1038 .function = misc_drop_buffers,
1039 .name = "misc-drop-buffers",
1040 .vector_size = sizeof (u32),
1041 .n_errors = 1,
1042 .n_next_nodes = 1,
1043 .next_nodes = {
1044 "error-drop",
1045 },
1046 .error_strings = misc_drop_buffers_error_strings,
1047};
1048/* *INDENT-ON* */
1049
Dave Barachba868bb2016-08-08 09:51:21 -04001050VLIB_NODE_FUNCTION_MULTIARCH (vnet_per_buffer_interface_output_node,
1051 vnet_per_buffer_interface_output);
Damjan Marion1c80e832016-05-11 23:07:18 +02001052
Damjan Marion152e21d2016-11-29 14:55:43 +01001053static uword
1054interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1055 vlib_frame_t * from_frame)
1056{
1057 vnet_main_t *vnm = vnet_get_main ();
1058 u32 last_sw_if_index = ~0;
1059 vlib_frame_t *to_frame = 0;
1060 vnet_hw_interface_t *hw = 0;
1061 u32 *from, *to_next = 0;
1062 u32 n_left_from;
1063
1064 from = vlib_frame_vector_args (from_frame);
1065 n_left_from = from_frame->n_vectors;
1066 while (n_left_from > 0)
1067 {
1068 u32 bi0;
1069 vlib_buffer_t *b0;
1070 u32 sw_if_index0;
1071
1072 bi0 = from[0];
1073 from++;
1074 n_left_from--;
1075 b0 = vlib_get_buffer (vm, bi0);
1076 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1077
1078 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1079 {
1080 if (to_frame)
1081 {
1082 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1083 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1084 }
1085 last_sw_if_index = sw_if_index0;
1086 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1087 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1088 to_next = vlib_frame_vector_args (to_frame);
1089 }
1090
1091 to_next[0] = bi0;
1092 to_next++;
1093 to_frame->n_vectors++;
1094 }
1095 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1096 return from_frame->n_vectors;
1097}
1098
1099/* *INDENT-OFF* */
1100VLIB_REGISTER_NODE (interface_tx, static) = {
1101 .function = interface_tx_node_fn,
1102 .name = "interface-tx",
1103 .vector_size = sizeof (u32),
1104 .n_next_nodes = 1,
1105 .next_nodes = {
1106 [0] = "error-drop",
1107 },
1108};
1109
1110VNET_FEATURE_ARC_INIT (interface_output, static) =
1111{
1112 .arc_name = "interface-output",
1113 .start_nodes = VNET_FEATURES (0),
Dave Baracha25def72018-11-26 11:04:45 -05001114 .last_in_arc = "interface-tx",
Damjan Marion152e21d2016-11-29 14:55:43 +01001115 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1116};
1117
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001118VNET_FEATURE_INIT (span_tx, static) = {
1119 .arc_name = "interface-output",
1120 .node_name = "span-output",
1121 .runs_before = VNET_FEATURES ("interface-tx"),
1122};
1123
Matthew Smith537eeec2018-04-09 11:49:20 -05001124VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1125 .arc_name = "interface-output",
1126 .node_name = "ipsec-if-output",
1127 .runs_before = VNET_FEATURES ("interface-tx"),
1128};
1129
Damjan Marion152e21d2016-11-29 14:55:43 +01001130VNET_FEATURE_INIT (interface_tx, static) = {
1131 .arc_name = "interface-output",
1132 .node_name = "interface-tx",
1133 .runs_before = 0,
1134};
1135/* *INDENT-ON* */
1136
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137clib_error_t *
1138vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1139 u32 hw_if_index,
1140 u32 is_create)
1141{
Dave Barachba868bb2016-08-08 09:51:21 -04001142 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 u32 next_index;
1144
John Loe5453d02018-01-23 19:21:34 -05001145 if (hi->output_node_index == 0)
1146 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
John Loe5453d02018-01-23 19:21:34 -05001148 next_index = vlib_node_add_next
1149 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1150 hi->output_node_index);
1151 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152
1153 return 0;
1154}
1155
Dave Barachba868bb2016-08-08 09:51:21 -04001156VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1157 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158
John Loe5453d02018-01-23 19:21:34 -05001159void
1160vnet_set_interface_output_node (vnet_main_t * vnm,
1161 u32 hw_if_index, u32 node_index)
1162{
1163 ASSERT (node_index);
1164 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1165 u32 next_index = vlib_node_add_next
1166 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1167 hi->output_node_next_index = next_index;
1168 hi->output_node_index = node_index;
1169}
1170
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171static clib_error_t *
1172pcap_drop_trace_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001173 unformat_input_t * input,
1174 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175{
Dave Barachba868bb2016-08-08 09:51:21 -04001176 vnet_main_t *vnm = vnet_get_main ();
1177 vnet_interface_main_t *im = &vnm->interface_main;
1178 u8 *filename;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 u32 max;
1180 int matched = 0;
Dave Barachba868bb2016-08-08 09:51:21 -04001181 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182
Dave Barachba868bb2016-08-08 09:51:21 -04001183 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 {
1185 if (unformat (input, "on"))
Dave Barachba868bb2016-08-08 09:51:21 -04001186 {
1187 if (im->drop_pcap_enable == 0)
1188 {
1189 if (im->pcap_filename == 0)
1190 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Dave Barachb7b92992018-10-17 10:38:51 -04001192 clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
Dave Barachba868bb2016-08-08 09:51:21 -04001193 im->pcap_main.file_name = (char *) im->pcap_filename;
1194 im->pcap_main.n_packets_to_capture = 100;
1195 if (im->pcap_pkts_to_capture)
1196 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1197
1198 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1199 im->drop_pcap_enable = 1;
1200 matched = 1;
1201 vlib_cli_output (vm, "pcap drop capture on...");
1202 }
1203 else
1204 {
1205 vlib_cli_output (vm, "pcap drop capture already on...");
1206 }
1207 matched = 1;
1208 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 else if (unformat (input, "off"))
Dave Barachba868bb2016-08-08 09:51:21 -04001210 {
1211 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
Dave Barachba868bb2016-08-08 09:51:21 -04001213 if (im->drop_pcap_enable)
1214 {
1215 vlib_cli_output (vm, "captured %d pkts...",
1216 im->pcap_main.n_packets_captured);
1217 if (im->pcap_main.n_packets_captured)
1218 {
1219 im->pcap_main.n_packets_to_capture =
1220 im->pcap_main.n_packets_captured;
1221 error = pcap_write (&im->pcap_main);
1222 if (error)
1223 clib_error_report (error);
1224 else
1225 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1226 }
1227 }
1228 else
1229 {
1230 vlib_cli_output (vm, "pcap drop capture already off...");
1231 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232
Dave Barachba868bb2016-08-08 09:51:21 -04001233 im->drop_pcap_enable = 0;
1234 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235 else if (unformat (input, "max %d", &max))
Dave Barachba868bb2016-08-08 09:51:21 -04001236 {
1237 im->pcap_pkts_to_capture = max;
1238 matched = 1;
1239 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240
Dave Barachba868bb2016-08-08 09:51:21 -04001241 else if (unformat (input, "intfc %U",
1242 unformat_vnet_sw_interface, vnm,
1243 &im->pcap_sw_if_index))
1244 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245 else if (unformat (input, "intfc any"))
Dave Barachba868bb2016-08-08 09:51:21 -04001246 {
1247 im->pcap_sw_if_index = 0;
1248 matched = 1;
1249 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 else if (unformat (input, "file %s", &filename))
Dave Barachba868bb2016-08-08 09:51:21 -04001251 {
1252 u8 *chroot_filename;
1253 /* Brain-police user path input */
1254 if (strstr ((char *) filename, "..")
1255 || index ((char *) filename, '/'))
1256 {
1257 vlib_cli_output (vm, "illegal characters in filename '%s'",
1258 filename);
1259 continue;
1260 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261
Dave Barachba868bb2016-08-08 09:51:21 -04001262 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1263 vec_free (filename);
1264
1265 if (im->pcap_filename)
1266 vec_free (im->pcap_filename);
Dave Barachba868bb2016-08-08 09:51:21 -04001267 im->pcap_filename = chroot_filename;
jerryian361abef2017-04-10 18:48:07 +08001268 im->pcap_main.file_name = (char *) im->pcap_filename;
Dave Barachba868bb2016-08-08 09:51:21 -04001269 matched = 1;
1270 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271 else if (unformat (input, "status"))
Dave Barachba868bb2016-08-08 09:51:21 -04001272 {
1273 if (im->drop_pcap_enable == 0)
1274 {
1275 vlib_cli_output (vm, "pcap drop capture is off...");
1276 continue;
1277 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278
Dave Barachba868bb2016-08-08 09:51:21 -04001279 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1280 im->pcap_main.n_packets_captured,
1281 im->pcap_main.n_packets_to_capture);
1282 matched = 1;
1283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001284
1285 else
Dave Barachba868bb2016-08-08 09:51:21 -04001286 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 }
1288
1289 if (matched == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001290 return clib_error_return (0, "unknown input `%U'",
1291 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292
1293 return 0;
1294}
1295
Dave Barachba868bb2016-08-08 09:51:21 -04001296/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297VLIB_CLI_COMMAND (pcap_trace_command, static) = {
Dave Barachba868bb2016-08-08 09:51:21 -04001298 .path = "pcap drop trace",
1299 .short_help =
1300 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1301 .function = pcap_drop_trace_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302};
Dave Barachba868bb2016-08-08 09:51:21 -04001303/* *INDENT-ON* */
1304
1305/*
1306 * fd.io coding-style-patch-verification: ON
1307 *
1308 * Local Variables:
1309 * eval: (c-set-style "gnu")
1310 * End:
1311 */