blob: 99aa1f110fb7bc04cd92a295a4e7e50c022bedd7 [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;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020050 u32 flags;
51 u16 gso_size;
52 u8 gso_l4_hdr_sz;
53 u8 data[128 - 3 * sizeof (u32)];
Dave Barachba868bb2016-08-08 09:51:21 -040054}
55interface_output_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Dave Barachba868bb2016-08-08 09:51:21 -040057u8 *
58format_vnet_interface_output_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040061 vlib_node_t *node = va_arg (*va, vlib_node_t *);
62 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
63 vnet_main_t *vnm = vnet_get_main ();
64 vnet_sw_interface_t *si;
Christophe Fontained3c008d2017-10-02 18:10:54 +020065 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Dave Barachba868bb2016-08-08 09:51:21 -040067 if (t->sw_if_index != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 indent = format_get_indent (s);
Dave Barachba868bb2016-08-08 09:51:21 -040070
Neale Ranns177bbdc2016-11-15 09:46:51 +000071 if (pool_is_free_index
72 (vnm->interface_main.sw_interfaces, t->sw_if_index))
73 {
74 /* the interface may have been deleted by the time the trace is printed */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020075 s = format (s, "sw_if_index: %d ", t->sw_if_index);
Neale Ranns177bbdc2016-11-15 09:46:51 +000076 }
77 else
78 {
79 si = vnet_get_sw_interface (vnm, t->sw_if_index);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020080 s =
81 format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
82 t->flags);
Neale Ranns177bbdc2016-11-15 09:46:51 +000083 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020084#define _(bit, name, v, x) \
85 if (v && (t->flags & VNET_BUFFER_F_##name)) \
86 s = format (s, "%s ", v);
87 foreach_vnet_buffer_flag
88#undef _
89 if (t->flags & VNET_BUFFER_F_GSO)
90 {
91 s = format (s, "\n%Ugso_sz %d gso_l4_hdr_sz %d",
92 format_white_space, indent + 2, t->gso_size,
93 t->gso_l4_hdr_sz);
94 }
95 s =
96 format (s, "\n%U%U", format_white_space, indent,
97 node->format_buffer ? node->format_buffer : format_hex_bytes,
98 t->data, sizeof (t->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 }
100 return s;
101}
102
103static void
104vnet_interface_output_trace (vlib_main_t * vm,
105 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -0400106 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107{
Dave Barachba868bb2016-08-08 09:51:21 -0400108 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
110 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100111 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400112
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 while (n_left >= 4)
114 {
115 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400116 vlib_buffer_t *b0, *b1;
117 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119 /* Prefetch next iteration. */
120 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
121 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
122
123 bi0 = from[0];
124 bi1 = from[1];
125
126 b0 = vlib_get_buffer (vm, bi0);
127 b1 = vlib_get_buffer (vm, bi1);
128
129 if (b0->flags & VLIB_BUFFER_IS_TRACED)
130 {
131 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
132 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200133 t0->flags = b0->flags;
134 t0->gso_size = vnet_buffer2 (b0)->gso_size;
135 t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
Dave Barach178cf492018-11-13 16:34:13 -0500136 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
137 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 }
139 if (b1->flags & VLIB_BUFFER_IS_TRACED)
140 {
141 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
142 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200143 t1->flags = b1->flags;
144 t1->gso_size = vnet_buffer2 (b1)->gso_size;
145 t1->gso_l4_hdr_sz = vnet_buffer2 (b1)->gso_l4_hdr_sz;
Dave Barach178cf492018-11-13 16:34:13 -0500146 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
147 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 }
149 from += 2;
150 n_left -= 2;
151 }
152
153 while (n_left >= 1)
154 {
155 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400156 vlib_buffer_t *b0;
157 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
159 bi0 = from[0];
160
161 b0 = vlib_get_buffer (vm, bi0);
162
163 if (b0->flags & VLIB_BUFFER_IS_TRACED)
164 {
165 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
166 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200167 t0->flags = b0->flags;
168 t0->gso_size = vnet_buffer2 (b0)->gso_size;
169 t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
Dave Barach178cf492018-11-13 16:34:13 -0500170 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
171 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 }
173 from += 1;
174 n_left -= 1;
175 }
176}
177
Dave Barach2c0a4f42017-06-29 09:30:15 -0400178static_always_inline void
179calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180{
Dave Barach2c0a4f42017-06-29 09:30:15 -0400181 ip4_header_t *ip4;
182 ip6_header_t *ip6;
183 tcp_header_t *th;
184 udp_header_t *uh;
185
186 int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
187 int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
188
189 ASSERT (!(is_ip4 && is_ip6));
190
191 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
192 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
193 th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
194 uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
195
196 if (is_ip4)
197 {
198 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
199 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
200 ip4->checksum = ip4_header_checksum (ip4);
201 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrej Kozemcak9d7570c2019-01-07 08:39:22 +0100202 {
203 th->checksum = 0;
204 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
205 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400206 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
207 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
208 }
209 if (is_ip6)
210 {
211 int bogus;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400212 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200213 {
214 th->checksum = 0;
215 th->checksum =
216 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
217 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400218 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200219 {
220 uh->checksum = 0;
221 uh->checksum =
222 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
223 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400224 }
225
226 b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
227 b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
228 b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
229}
230
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200231static_always_inline u16
232tso_alloc_tx_bufs (vlib_main_t * vm,
233 vnet_interface_per_thread_data_t * ptd,
234 vlib_buffer_t * b0, u16 l4_hdr_sz)
235{
236 u32 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
237 u16 gso_size = vnet_buffer2 (b0)->gso_size;
238 u16 l234_sz = vnet_buffer (b0)->l4_hdr_offset + l4_hdr_sz;
239 /* rounded-up division */
240 u16 n_bufs = (n_bytes_b0 - l234_sz + (gso_size - 1)) / gso_size;
241 u16 n_alloc;
242
243 ASSERT (n_bufs > 0);
244 vec_validate (ptd->split_buffers, n_bufs - 1);
245
246 n_alloc = vlib_buffer_alloc (vm, ptd->split_buffers, n_bufs);
247 if (n_alloc < n_bufs)
248 {
249 vlib_buffer_free (vm, ptd->split_buffers, n_alloc);
250 return 0;
251 }
252 return 1;
253}
254
255static_always_inline void
256tso_init_buf_from_template_base (vlib_buffer_t * nb0, vlib_buffer_t * b0,
257 u32 flags, u16 length)
258{
259 nb0->current_data = 0;
260 nb0->total_length_not_including_first_buffer = 0;
261 nb0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | flags;
262 clib_memcpy_fast (&nb0->opaque, &b0->opaque, sizeof (nb0->opaque));
263 clib_memcpy_fast (nb0->data, b0->data, length);
264 nb0->current_length = length;
265}
266
267static_always_inline void
268tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
269 vlib_buffer_t * b0, u16 template_data_sz,
270 u16 gso_size, u8 ** p_dst_ptr, u16 * p_dst_left,
271 u32 next_tcp_seq, u32 flags)
272{
273 tso_init_buf_from_template_base (nb0, b0, flags, template_data_sz);
274
275 *p_dst_left =
276 clib_min (gso_size,
277 vlib_buffer_get_default_data_size (vm) - template_data_sz);
278 *p_dst_ptr = nb0->data + template_data_sz;
279
280 tcp_header_t *tcp =
281 (tcp_header_t *) (nb0->data + vnet_buffer (nb0)->l4_hdr_offset);
282 tcp->seq_number = clib_host_to_net_u32 (next_tcp_seq);
283}
284
285static_always_inline void
286tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6)
287{
288 u16 l3_hdr_offset = vnet_buffer (b0)->l3_hdr_offset;
289 u16 l4_hdr_offset = vnet_buffer (b0)->l4_hdr_offset;
290 ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l3_hdr_offset);
291 ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l3_hdr_offset);
292 tcp_header_t *tcp = (tcp_header_t *) (b0->data + l4_hdr_offset);
293
294 tcp->flags = tcp_flags;
295
296 if (is_ip6)
297 ip6->payload_length =
298 clib_host_to_net_u16 (b0->current_length -
299 vnet_buffer (b0)->l4_hdr_offset);
300 else
301 ip4->length =
302 clib_host_to_net_u16 (b0->current_length -
303 vnet_buffer (b0)->l3_hdr_offset);
304}
305
306/**
307 * Allocate the necessary number of ptd->split_buffers,
308 * and segment the possibly chained buffer(s) from b0 into
309 * there.
310 *
311 * Return the cumulative number of bytes sent or zero
312 * if allocation failed.
313 */
314
315static_always_inline u32
316tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
317 int do_tx_offloads, u32 sbi0, vlib_buffer_t * sb0,
318 u32 n_bytes_b0)
319{
320 u32 n_tx_bytes = 0;
321 int is_ip4 = sb0->flags & VNET_BUFFER_F_IS_IP4;
322 int is_ip6 = sb0->flags & VNET_BUFFER_F_IS_IP6;
323 ASSERT (is_ip4 || is_ip6);
324 ASSERT (sb0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID);
325 ASSERT (sb0->flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID);
326 ASSERT (sb0->flags & VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
327 u16 gso_size = vnet_buffer2 (sb0)->gso_size;
328
329 int l4_hdr_sz = vnet_buffer2 (sb0)->gso_l4_hdr_sz;
330 u8 save_tcp_flags = 0;
331 u8 tcp_flags_no_fin_psh = 0;
332 u32 next_tcp_seq = 0;
333
334 tcp_header_t *tcp =
335 (tcp_header_t *) (sb0->data + vnet_buffer (sb0)->l4_hdr_offset);
336 next_tcp_seq = clib_net_to_host_u32 (tcp->seq_number);
337 /* store original flags for last packet and reset FIN and PSH */
338 save_tcp_flags = tcp->flags;
339 tcp_flags_no_fin_psh = tcp->flags & ~(TCP_FLAG_FIN | TCP_FLAG_PSH);
340 tcp->checksum = 0;
341
342 u32 default_bflags =
343 sb0->flags & ~(VNET_BUFFER_F_GSO | VLIB_BUFFER_NEXT_PRESENT);
344 u16 l234_sz = vnet_buffer (sb0)->l4_hdr_offset + l4_hdr_sz;
345 int first_data_size = clib_min (gso_size, sb0->current_length - l234_sz);
346 next_tcp_seq += first_data_size;
347
348 if (PREDICT_FALSE (!tso_alloc_tx_bufs (vm, ptd, sb0, l4_hdr_sz)))
349 return 0;
350
351 vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[0]);
352 tso_init_buf_from_template_base (b0, sb0, default_bflags,
353 l4_hdr_sz + first_data_size);
354
355 u32 total_src_left = n_bytes_b0 - l234_sz - first_data_size;
356 if (total_src_left)
357 {
358 /* Need to copy more segments */
359 u8 *src_ptr, *dst_ptr;
360 u16 src_left, dst_left;
361 /* current source buffer */
362 vlib_buffer_t *csb0 = sb0;
363 u32 csbi0 = sbi0;
364 /* current dest buffer */
365 vlib_buffer_t *cdb0;
366 u16 dbi = 1; /* the buffer [0] is b0 */
367
368 src_ptr = sb0->data + l234_sz + first_data_size;
369 src_left = sb0->current_length - l234_sz - first_data_size;
370 b0->current_length = l234_sz + first_data_size;
371
372 tso_fixup_segmented_buf (b0, tcp_flags_no_fin_psh, is_ip6);
373 if (do_tx_offloads)
374 calc_checksums (vm, b0);
375
376 /* grab a second buffer and prepare the loop */
377 ASSERT (dbi < vec_len (ptd->split_buffers));
378 cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
379 tso_init_buf_from_template (vm, cdb0, b0, l234_sz, gso_size, &dst_ptr,
380 &dst_left, next_tcp_seq, default_bflags);
381
382 /* an arbitrary large number to catch the runaway loops */
383 int nloops = 2000;
384 while (total_src_left)
385 {
Dave Baracha2aefef2019-02-27 12:36:28 -0500386 if (nloops-- <= 0)
387 clib_panic ("infinite loop detected");
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200388 u16 bytes_to_copy = clib_min (src_left, dst_left);
389
390 clib_memcpy_fast (dst_ptr, src_ptr, bytes_to_copy);
391
392 src_left -= bytes_to_copy;
393 src_ptr += bytes_to_copy;
394 total_src_left -= bytes_to_copy;
395 dst_left -= bytes_to_copy;
396 dst_ptr += bytes_to_copy;
397 next_tcp_seq += bytes_to_copy;
398 cdb0->current_length += bytes_to_copy;
399
400 if (0 == src_left)
401 {
402 int has_next = (csb0->flags & VLIB_BUFFER_NEXT_PRESENT);
403 u32 next_bi = csb0->next_buffer;
404
405 /* init src to the next buffer in chain */
406 if (has_next)
407 {
408 csbi0 = next_bi;
409 csb0 = vlib_get_buffer (vm, csbi0);
410 src_left = csb0->current_length;
411 src_ptr = csb0->data;
412 }
413 else
414 {
415 ASSERT (total_src_left == 0);
416 break;
417 }
418 }
419 if (0 == dst_left && total_src_left)
420 {
421 if (do_tx_offloads)
422 calc_checksums (vm, cdb0);
423 n_tx_bytes += cdb0->current_length;
424 ASSERT (dbi < vec_len (ptd->split_buffers));
425 cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
426 tso_init_buf_from_template (vm, cdb0, b0, l234_sz,
427 gso_size, &dst_ptr, &dst_left,
428 next_tcp_seq, default_bflags);
429 }
430 }
431
432 tso_fixup_segmented_buf (cdb0, save_tcp_flags, is_ip6);
433 if (do_tx_offloads)
434 calc_checksums (vm, cdb0);
435
436 n_tx_bytes += cdb0->current_length;
437 }
438 n_tx_bytes += b0->current_length;
439 return n_tx_bytes;
440}
441
442static_always_inline void
443drop_one_buffer_and_count (vlib_main_t * vm, vnet_main_t * vnm,
444 vlib_node_runtime_t * node, u32 * pbi0,
445 u32 drop_error_code)
446{
447 u32 thread_index = vm->thread_index;
448 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
449
450 vlib_simple_counter_main_t *cm;
451 cm =
452 vec_elt_at_index (vnm->interface_main.sw_if_counters,
453 VNET_INTERFACE_COUNTER_TX_ERROR);
454 vlib_increment_simple_counter (cm, thread_index, rt->sw_if_index, 1);
455
456 vlib_error_drop_buffers (vm, node, pbi0,
457 /* buffer stride */ 1,
458 /* n_buffers */ 1,
459 VNET_INTERFACE_OUTPUT_NEXT_DROP,
460 node->node_index, drop_error_code);
461}
462
Dave Barach2c0a4f42017-06-29 09:30:15 -0400463static_always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200464vnet_interface_output_node_inline_gso (vlib_main_t * vm,
465 vlib_node_runtime_t * node,
466 vlib_frame_t * frame,
467 vnet_main_t * vnm,
468 vnet_hw_interface_t * hi,
469 int do_tx_offloads,
470 int do_segmentation)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400471{
Dave Barachba868bb2016-08-08 09:51:21 -0400472 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
473 vnet_sw_interface_t *si;
Dave Barachba868bb2016-08-08 09:51:21 -0400474 u32 n_left_to_tx, *from, *from_end, *to_tx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 u32 n_bytes, n_buffers, n_packets;
Damjan Marion363640d2017-03-06 11:53:10 +0100476 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
Damjan Marion586afd72017-04-05 19:18:20 +0200477 u32 thread_index = vm->thread_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400478 vnet_interface_main_t *im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
Damjan Marion152e21d2016-11-29 14:55:43 +0100480 u32 current_config_index = ~0;
481 u8 arc = im->output_feature_arc_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200482 vnet_interface_per_thread_data_t *ptd =
483 vec_elt_at_index (im->per_thread_data, thread_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
485 n_buffers = frame->n_vectors;
486
487 if (node->flags & VLIB_NODE_FLAG_TRACE)
488 vnet_interface_output_trace (vm, node, frame, n_buffers);
489
Damjan Mariona3d59862018-11-10 10:23:00 +0100490 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
492 if (rt->is_deleted)
Dave Barachba868bb2016-08-08 09:51:21 -0400493 return vlib_error_drop_buffers (vm, node, from,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 /* buffer stride */ 1,
495 n_buffers,
496 VNET_INTERFACE_OUTPUT_NEXT_DROP,
497 node->node_index,
498 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
499
500 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
501 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
Steve Shin042a6212017-07-07 14:57:46 -0700502 if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
503 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
Dave Barachba868bb2016-08-08 09:51:21 -0400504 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 {
Dave Barachba868bb2016-08-08 09:51:21 -0400506 vlib_simple_counter_main_t *cm;
507
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
Dave Barachba868bb2016-08-08 09:51:21 -0400509 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion586afd72017-04-05 19:18:20 +0200510 vlib_increment_simple_counter (cm, thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400511 rt->sw_if_index, n_buffers);
512
513 return vlib_error_drop_buffers (vm, node, from,
514 /* buffer stride */ 1,
515 n_buffers,
516 VNET_INTERFACE_OUTPUT_NEXT_DROP,
517 node->node_index,
518 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 }
520
521 from_end = from + n_buffers;
522
523 /* Total byte count of all buffers. */
524 n_bytes = 0;
525 n_packets = 0;
526
Damjan Marion152e21d2016-11-29 14:55:43 +0100527 /* interface-output feature arc handling */
528 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
529 {
530 vnet_feature_config_main_t *fcm;
531 fcm = vnet_feature_get_config_main (arc);
532 current_config_index = vnet_get_feature_config_index (arc,
533 rt->sw_if_index);
534 vnet_get_config_data (&fcm->config_main, &current_config_index,
535 &next_index, 0);
536 }
537
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 while (from < from_end)
539 {
540 /* Get new next frame since previous incomplete frame may have less
Dave Barachba868bb2016-08-08 09:51:21 -0400541 than VNET_FRAME_SIZE vectors in it. */
542 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Damjan Marion363640d2017-03-06 11:53:10 +0100544 while (from + 8 <= from_end && n_left_to_tx >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 {
Damjan Marion363640d2017-03-06 11:53:10 +0100546 u32 bi0, bi1, bi2, bi3;
547 vlib_buffer_t *b0, *b1, *b2, *b3;
548 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400549 u32 or_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551 /* Prefetch next iteration. */
Damjan Marion363640d2017-03-06 11:53:10 +0100552 vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
553 vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
554 vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
555 vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
557 bi0 = from[0];
558 bi1 = from[1];
Damjan Marion363640d2017-03-06 11:53:10 +0100559 bi2 = from[2];
560 bi3 = from[3];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 to_tx[0] = bi0;
562 to_tx[1] = bi1;
Damjan Marion363640d2017-03-06 11:53:10 +0100563 to_tx[2] = bi2;
564 to_tx[3] = bi3;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200565 if (!do_segmentation)
566 {
567 from += 4;
568 to_tx += 4;
569 n_left_to_tx -= 4;
570 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571
572 b0 = vlib_get_buffer (vm, bi0);
573 b1 = vlib_get_buffer (vm, bi1);
Damjan Marion363640d2017-03-06 11:53:10 +0100574 b2 = vlib_get_buffer (vm, bi2);
575 b3 = vlib_get_buffer (vm, bi3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200577 if (do_segmentation)
578 {
579 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
580
581 /* go to single loop if we need TSO segmentation */
582 if (PREDICT_FALSE (or_flags & VNET_BUFFER_F_GSO))
583 break;
584 from += 4;
585 to_tx += 4;
586 n_left_to_tx -= 4;
587 }
588
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 /* Be grumpy about zero length buffers for benefit of
590 driver tx function. */
591 ASSERT (b0->current_length > 0);
592 ASSERT (b1->current_length > 0);
Damjan Marion363640d2017-03-06 11:53:10 +0100593 ASSERT (b2->current_length > 0);
594 ASSERT (b3->current_length > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Dave Barachba868bb2016-08-08 09:51:21 -0400596 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
597 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
Damjan Marion363640d2017-03-06 11:53:10 +0100598 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
599 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
Dave Barachba868bb2016-08-08 09:51:21 -0400600 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
601 tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Damjan Marion363640d2017-03-06 11:53:10 +0100602 tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
603 tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
605 n_bytes += n_bytes_b0 + n_bytes_b1;
Damjan Marion363640d2017-03-06 11:53:10 +0100606 n_bytes += n_bytes_b2 + n_bytes_b3;
607 n_packets += 4;
Dave Barachba868bb2016-08-08 09:51:21 -0400608
Damjan Marion152e21d2016-11-29 14:55:43 +0100609 if (PREDICT_FALSE (current_config_index != ~0))
610 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200611 vnet_buffer (b0)->feature_arc_index = arc;
612 vnet_buffer (b1)->feature_arc_index = arc;
613 vnet_buffer (b2)->feature_arc_index = arc;
614 vnet_buffer (b3)->feature_arc_index = arc;
Damjan Marion152e21d2016-11-29 14:55:43 +0100615 b0->current_config_index = current_config_index;
616 b1->current_config_index = current_config_index;
Damjan Marion363640d2017-03-06 11:53:10 +0100617 b2->current_config_index = current_config_index;
618 b3->current_config_index = current_config_index;
Damjan Marion152e21d2016-11-29 14:55:43 +0100619 }
620
Damjan Marion363640d2017-03-06 11:53:10 +0100621 /* update vlan subif tx counts, if required */
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100622 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400623 {
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100624 vlib_increment_combined_counter (im->combined_sw_if_counters +
625 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200626 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100627 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400628 }
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100629
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100630 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
631 {
632
633 vlib_increment_combined_counter (im->combined_sw_if_counters +
634 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200635 thread_index, tx_swif1, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100636 n_bytes_b1);
637 }
Damjan Marion363640d2017-03-06 11:53:10 +0100638
639 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
640 {
641
642 vlib_increment_combined_counter (im->combined_sw_if_counters +
643 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200644 thread_index, tx_swif2, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100645 n_bytes_b2);
646 }
647 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
648 {
649
650 vlib_increment_combined_counter (im->combined_sw_if_counters +
651 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200652 thread_index, tx_swif3, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100653 n_bytes_b3);
654 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400655
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200656 if (!do_segmentation)
657 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400658
659 if (do_tx_offloads)
660 {
661 if (or_flags &
662 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
663 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
664 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
665 {
666 calc_checksums (vm, b0);
667 calc_checksums (vm, b1);
668 calc_checksums (vm, b2);
669 calc_checksums (vm, b3);
670 }
671 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 }
673
674 while (from + 1 <= from_end && n_left_to_tx >= 1)
675 {
676 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400677 vlib_buffer_t *b0;
678 u32 tx_swif0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
680 bi0 = from[0];
681 to_tx[0] = bi0;
682 from += 1;
683 to_tx += 1;
684 n_left_to_tx -= 1;
685
686 b0 = vlib_get_buffer (vm, bi0);
687
688 /* Be grumpy about zero length buffers for benefit of
689 driver tx function. */
690 ASSERT (b0->current_length > 0);
691
Dave Barachba868bb2016-08-08 09:51:21 -0400692 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
693 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 n_bytes += n_bytes_b0;
695 n_packets += 1;
696
Damjan Marion152e21d2016-11-29 14:55:43 +0100697 if (PREDICT_FALSE (current_config_index != ~0))
698 {
Damjan Marionaa682a32018-04-26 22:45:40 +0200699 vnet_buffer (b0)->feature_arc_index = arc;
Damjan Marion152e21d2016-11-29 14:55:43 +0100700 b0->current_config_index = current_config_index;
701 }
702
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200703 if (do_segmentation)
704 {
705 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_GSO))
706 {
707 /*
708 * Undo the enqueue of the b0 - it is not going anywhere,
709 * and will be freed either after it's segmented or
710 * when dropped, if there is no buffers to segment into.
711 */
712 to_tx -= 1;
713 n_left_to_tx += 1;
714 /* undo the counting. */
715 n_bytes -= n_bytes_b0;
716 n_packets -= 1;
717
718 u32 n_tx_bytes = 0;
719
720 n_tx_bytes =
721 tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b0,
722 n_bytes_b0);
723
724 if (PREDICT_FALSE (n_tx_bytes == 0))
725 {
726 drop_one_buffer_and_count (vm, vnm, node, from - 1,
727 VNET_INTERFACE_OUTPUT_ERROR_NO_BUFFERS_FOR_GSO);
728 continue;
729 }
730
731 u16 n_tx_bufs = vec_len (ptd->split_buffers);
732 u32 *from_tx_seg = ptd->split_buffers;
733
734 while (n_tx_bufs > 0)
735 {
736 if (n_tx_bufs >= n_left_to_tx)
737 {
738 while (n_left_to_tx > 0)
739 {
740 to_tx[0] = from_tx_seg[0];
741 to_tx += 1;
742 from_tx_seg += 1;
743 n_left_to_tx -= 1;
744 n_tx_bufs -= 1;
745 n_packets += 1;
746 }
747 vlib_put_next_frame (vm, node, next_index,
748 n_left_to_tx);
749 vlib_get_new_next_frame (vm, node, next_index,
750 to_tx, n_left_to_tx);
751 }
752 else
753 {
754 while (n_tx_bufs > 0)
755 {
756 to_tx[0] = from_tx_seg[0];
757 to_tx += 1;
758 from_tx_seg += 1;
759 n_left_to_tx -= 1;
760 n_tx_bufs -= 1;
761 n_packets += 1;
762 }
763 }
764 }
765 n_bytes += n_tx_bytes;
766 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
767 {
768
769 vlib_increment_combined_counter
770 (im->combined_sw_if_counters +
771 VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif0,
772 _vec_len (ptd->split_buffers), n_tx_bytes);
773 }
774 /* The buffers were enqueued. Reset the length */
775 _vec_len (ptd->split_buffers) = 0;
776 /* Free the now segmented buffer */
777 vlib_buffer_free_one (vm, bi0);
778 continue;
779 }
780 }
781
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100782 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400783 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100785 vlib_increment_combined_counter (im->combined_sw_if_counters +
786 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200787 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100788 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400789 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400790
791 if (do_tx_offloads)
792 calc_checksums (vm, b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 }
794
Dave Barachba868bb2016-08-08 09:51:21 -0400795 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 }
797
798 /* Update main interface stats. */
799 vlib_increment_combined_counter (im->combined_sw_if_counters
Dave Barachba868bb2016-08-08 09:51:21 -0400800 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200801 thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400802 rt->sw_if_index, n_packets, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803 return n_buffers;
804}
805
Dave Barach5ecd5a52019-02-25 15:27:28 -0500806static_always_inline void vnet_interface_pcap_tx_trace
807 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
808 int sw_if_index_from_buffer)
809{
810 u32 n_left_from, *from;
811 u32 sw_if_index;
812
813 if (PREDICT_TRUE (vm->pcap[VLIB_TX].pcap_enable == 0))
814 return;
815
816 if (sw_if_index_from_buffer == 0)
817 {
818 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
819 sw_if_index = rt->sw_if_index;
820 }
821 else
822 sw_if_index = ~0;
823
824 n_left_from = frame->n_vectors;
825 from = vlib_frame_vector_args (frame);
826
827 while (n_left_from > 0)
828 {
829 u32 bi0 = from[0];
830 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
831
832 if (sw_if_index_from_buffer)
833 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
834
835 if (vm->pcap[VLIB_TX].pcap_sw_if_index == 0 ||
836 vm->pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
837 pcap_add_buffer (&vm->pcap[VLIB_TX].pcap_main, vm, bi0, 512);
838 from++;
839 n_left_from--;
840 }
841}
842
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200843static_always_inline uword
844vnet_interface_output_node_inline (vlib_main_t * vm,
845 vlib_node_runtime_t * node,
846 vlib_frame_t * frame, vnet_main_t * vnm,
847 vnet_hw_interface_t * hi,
848 int do_tx_offloads)
849{
Dave Barach5ecd5a52019-02-25 15:27:28 -0500850 vnet_interface_pcap_tx_trace (vm, node, frame,
851 0 /* sw_if_index_from_buffer */ );
852
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200853 /*
854 * The 3-headed "if" is here because we want to err on the side
855 * of not impacting the non-GSO performance - so for the more
856 * common case of no GSO interfaces we want to prevent the
857 * segmentation codepath from being there altogether.
858 */
859 if (PREDICT_TRUE (vnm->interface_main.gso_interface_count == 0))
860 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
861 do_tx_offloads,
862 /* do_segmentation */ 0);
863 else if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
864 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
865 do_tx_offloads,
866 /* do_segmentation */ 0);
867 else
868 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
869 do_tx_offloads,
870 /* do_segmentation */ 1);
871}
872
Damjan Marion652d2e12019-02-02 00:15:27 +0100873uword
Dave Barach2c0a4f42017-06-29 09:30:15 -0400874vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
875 vlib_frame_t * frame)
876{
877 vnet_main_t *vnm = vnet_get_main ();
878 vnet_hw_interface_t *hi;
879 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
880 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
881
Dave Barach5ecd5a52019-02-25 15:27:28 -0500882 vnet_interface_pcap_tx_trace (vm, node, frame,
883 0 /* sw_if_index_from_buffer */ );
884
Dave Barach2c0a4f42017-06-29 09:30:15 -0400885 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
886 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
887 /* do_tx_offloads */ 0);
888 else
889 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
890 /* do_tx_offloads */ 1);
891}
892
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
894static uword
895vnet_per_buffer_interface_output (vlib_main_t * vm,
896 vlib_node_runtime_t * node,
897 vlib_frame_t * frame)
898{
Dave Barachba868bb2016-08-08 09:51:21 -0400899 vnet_main_t *vnm = vnet_get_main ();
900 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901 u32 n_left_from, next_index;
902
Dave Barach5ecd5a52019-02-25 15:27:28 -0500903 vnet_interface_pcap_tx_trace (vm, node, frame,
904 1 /* sw_if_index_from_buffer */ );
905
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 n_left_from = frame->n_vectors;
907
Damjan Mariona3d59862018-11-10 10:23:00 +0100908 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 next_index = node->cached_next_index;
910
911 while (n_left_from > 0)
912 {
913 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
914
915 while (n_left_from >= 4 && n_left_to_next >= 2)
916 {
917 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400918 vlib_buffer_t *b0, *b1;
919 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920
921 /* Prefetch next iteration. */
922 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
923 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
924
925 bi0 = from[0];
926 bi1 = from[1];
927 to_next[0] = bi0;
928 to_next[1] = bi1;
929 from += 2;
930 to_next += 2;
931 n_left_to_next -= 2;
932 n_left_from -= 2;
933
934 b0 = vlib_get_buffer (vm, bi0);
935 b1 = vlib_get_buffer (vm, bi1);
936
Dave Barachba868bb2016-08-08 09:51:21 -0400937 hi0 =
938 vnet_get_sup_hw_interface (vnm,
939 vnet_buffer (b0)->sw_if_index
940 [VLIB_TX]);
941 hi1 =
942 vnet_get_sup_hw_interface (vnm,
943 vnet_buffer (b1)->sw_if_index
944 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945
John Loe5453d02018-01-23 19:21:34 -0500946 next0 = hi0->output_node_next_index;
947 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948
Dave Barachba868bb2016-08-08 09:51:21 -0400949 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
950 n_left_to_next, bi0, bi1, next0,
951 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 }
953
954 while (n_left_from > 0 && n_left_to_next > 0)
955 {
956 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400957 vlib_buffer_t *b0;
958 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
960 bi0 = from[0];
961 to_next[0] = bi0;
962 from += 1;
963 to_next += 1;
964 n_left_to_next -= 1;
965 n_left_from -= 1;
966
967 b0 = vlib_get_buffer (vm, bi0);
968
Dave Barachba868bb2016-08-08 09:51:21 -0400969 hi0 =
970 vnet_get_sup_hw_interface (vnm,
971 vnet_buffer (b0)->sw_if_index
972 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973
John Loe5453d02018-01-23 19:21:34 -0500974 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Dave Barachba868bb2016-08-08 09:51:21 -0400976 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
977 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 }
979
980 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
981 }
982
983 return frame->n_vectors;
984}
985
986always_inline u32
987counter_index (vlib_main_t * vm, vlib_error_t e)
988{
Dave Barachba868bb2016-08-08 09:51:21 -0400989 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 u32 ci, ni;
991
992 ni = vlib_error_get_node (e);
993 n = vlib_get_node (vm, ni);
994
995 ci = vlib_error_get_code (e);
996 ASSERT (ci < n->n_errors);
997
998 ci += n->error_heap_index;
999
1000 return ci;
1001}
1002
Dave Barachba868bb2016-08-08 09:51:21 -04001003static u8 *
1004format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005{
Dave Barachba868bb2016-08-08 09:51:21 -04001006 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Dave Barachba868bb2016-08-08 09:51:21 -04001008 vlib_error_t *e = va_arg (*va, vlib_error_t *);
1009 vlib_node_t *error_node;
1010 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 u32 i;
1012
1013 error_node = vlib_get_node (vm, vlib_error_get_node (e[0]));
1014 i = counter_index (vm, e[0]);
1015 s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]);
1016
1017 return s;
1018}
1019
1020static void
1021trace_errors_with_buffers (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001022 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023{
Dave Barachba868bb2016-08-08 09:51:21 -04001024 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
1026 buffers = vlib_frame_vector_args (frame);
1027 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001028
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 while (n_left >= 4)
1030 {
1031 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -04001032 vlib_buffer_t *b0, *b1;
1033 vlib_error_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
1035 /* Prefetch next iteration. */
1036 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
1037 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
1038
1039 bi0 = buffers[0];
1040 bi1 = buffers[1];
1041
1042 b0 = vlib_get_buffer (vm, bi0);
1043 b1 = vlib_get_buffer (vm, bi1);
1044
1045 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1046 {
1047 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1048 t0[0] = b0->error;
1049 }
1050 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1051 {
1052 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1053 t1[0] = b1->error;
1054 }
1055 buffers += 2;
1056 n_left -= 2;
1057 }
1058
1059 while (n_left >= 1)
1060 {
1061 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -04001062 vlib_buffer_t *b0;
1063 vlib_error_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
1065 bi0 = buffers[0];
1066
1067 b0 = vlib_get_buffer (vm, bi0);
1068
1069 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1070 {
1071 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1072 t0[0] = b0->error;
1073 }
1074 buffers += 1;
1075 n_left -= 1;
1076 }
1077}
1078
1079static u8 *
1080validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
1081{
1082 uword node_index = vlib_error_get_node (e[0]);
1083 uword code = vlib_error_get_code (e[0]);
Dave Barachba868bb2016-08-08 09:51:21 -04001084 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085
1086 if (node_index >= vec_len (vm->node_main.nodes))
1087 return format (0, "[%d], node index out of range 0x%x, error 0x%x",
1088 index, node_index, e[0]);
1089
1090 n = vlib_get_node (vm, node_index);
1091 if (code >= n->n_errors)
1092 return format (0, "[%d], code %d out of range for node %v",
1093 index, code, n->name);
1094
1095 return 0;
1096}
1097
1098static u8 *
1099validate_error_frame (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001100 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101{
Damjan Mariona3d59862018-11-10 10:23:00 +01001102 u32 *buffers = vlib_frame_vector_args (f);
Dave Barachba868bb2016-08-08 09:51:21 -04001103 vlib_buffer_t *b;
1104 u8 *msg = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 uword i;
1106
1107 for (i = 0; i < f->n_vectors; i++)
1108 {
1109 b = vlib_get_buffer (vm, buffers[i]);
1110 msg = validate_error (vm, &b->error, i);
1111 if (msg)
1112 return msg;
1113 }
1114
1115 return msg;
1116}
1117
Dave Barachba868bb2016-08-08 09:51:21 -04001118typedef enum
1119{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 VNET_ERROR_DISPOSITION_DROP,
1121 VNET_ERROR_DISPOSITION_PUNT,
1122 VNET_ERROR_N_DISPOSITION,
1123} vnet_error_disposition_t;
1124
1125always_inline void
1126do_packet (vlib_main_t * vm, vlib_error_t a)
1127{
Dave Barachba868bb2016-08-08 09:51:21 -04001128 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 u32 i = counter_index (vm, a);
1130 em->counters[i] += 1;
1131 vlib_error_elog_count (vm, i, 1);
1132}
Dave Barachba868bb2016-08-08 09:51:21 -04001133
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134static_always_inline uword
1135process_drop_punt (vlib_main_t * vm,
1136 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -04001137 vlib_frame_t * frame, vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138{
Dave Barachba868bb2016-08-08 09:51:21 -04001139 vnet_main_t *vnm = vnet_get_main ();
1140 vlib_error_main_t *em = &vm->error_main;
1141 u32 *buffers, *first_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 vlib_error_t current_error;
1143 u32 current_counter_index, n_errors_left;
1144 u32 current_sw_if_index, n_errors_current_sw_if_index;
1145 u64 current_counter;
Dave Barachba868bb2016-08-08 09:51:21 -04001146 vlib_simple_counter_main_t *cm;
Damjan Marion586afd72017-04-05 19:18:20 +02001147 u32 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148
1149 static vlib_error_t memory[VNET_ERROR_N_DISPOSITION];
1150 static char memory_init[VNET_ERROR_N_DISPOSITION];
1151
Damjan Mariona3d59862018-11-10 10:23:00 +01001152 buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 first_buffer = buffers;
1154
1155 {
Dave Barachba868bb2016-08-08 09:51:21 -04001156 vlib_buffer_t *b = vlib_get_buffer (vm, first_buffer[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157
Dave Barachba868bb2016-08-08 09:51:21 -04001158 if (!memory_init[disposition])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 {
1160 memory_init[disposition] = 1;
1161 memory[disposition] = b->error;
1162 }
1163
1164 current_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1165 n_errors_current_sw_if_index = 0;
1166 }
1167
1168 current_error = memory[disposition];
1169 current_counter_index = counter_index (vm, memory[disposition]);
1170 current_counter = em->counters[current_counter_index];
1171
1172 if (node->flags & VLIB_NODE_FLAG_TRACE)
1173 trace_errors_with_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -04001174
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 n_errors_left = frame->n_vectors;
1176 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1177 (disposition == VNET_ERROR_DISPOSITION_PUNT
1178 ? VNET_INTERFACE_COUNTER_PUNT
1179 : VNET_INTERFACE_COUNTER_DROP));
1180
1181 while (n_errors_left >= 2)
1182 {
Dave Barachba868bb2016-08-08 09:51:21 -04001183 vlib_buffer_t *b0, *b1;
1184 vnet_sw_interface_t *sw_if0, *sw_if1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185 vlib_error_t e0, e1;
1186 u32 bi0, bi1;
1187 u32 sw_if_index0, sw_if_index1;
1188
1189 bi0 = buffers[0];
1190 bi1 = buffers[1];
1191
1192 buffers += 2;
1193 n_errors_left -= 2;
1194
1195 b0 = vlib_get_buffer (vm, bi0);
1196 b1 = vlib_get_buffer (vm, bi1);
1197
1198 e0 = b0->error;
1199 e1 = b1->error;
1200
1201 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1202 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1203
1204 /* Speculate that sw_if_index == sw_if_index[01]. */
1205 n_errors_current_sw_if_index += 2;
1206
1207 /* Speculatively assume all 2 (node, code) pairs are equal
Dave Barachba868bb2016-08-08 09:51:21 -04001208 to current (node, code). */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 current_counter += 2;
1210
1211 if (PREDICT_FALSE (e0 != current_error
1212 || e1 != current_error
1213 || sw_if_index0 != current_sw_if_index
1214 || sw_if_index1 != current_sw_if_index))
1215 {
1216 current_counter -= 2;
1217 n_errors_current_sw_if_index -= 2;
1218
Damjan Marion586afd72017-04-05 19:18:20 +02001219 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
1220 vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221
1222 /* Increment super-interface drop/punt counters for
1223 sub-interfaces. */
1224 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
1225 vlib_increment_simple_counter
Damjan Marion586afd72017-04-05 19:18:20 +02001226 (cm, thread_index, sw_if0->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -04001227 sw_if0->sup_sw_if_index != sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
1229 sw_if1 = vnet_get_sw_interface (vnm, sw_if_index1);
1230 vlib_increment_simple_counter
Damjan Marion586afd72017-04-05 19:18:20 +02001231 (cm, thread_index, sw_if1->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -04001232 sw_if1->sup_sw_if_index != sw_if_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
1234 em->counters[current_counter_index] = current_counter;
1235 do_packet (vm, e0);
1236 do_packet (vm, e1);
1237
1238 /* For 2 repeated errors, change current error. */
1239 if (e0 == e1 && e1 != current_error)
1240 {
1241 current_error = e0;
1242 current_counter_index = counter_index (vm, e0);
1243 }
1244 current_counter = em->counters[current_counter_index];
1245 }
1246 }
1247
1248 while (n_errors_left >= 1)
1249 {
Dave Barachba868bb2016-08-08 09:51:21 -04001250 vlib_buffer_t *b0;
1251 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252 vlib_error_t e0;
1253 u32 bi0, sw_if_index0;
1254
1255 bi0 = buffers[0];
1256
1257 buffers += 1;
1258 n_errors_left -= 1;
1259 current_counter += 1;
1260
1261 b0 = vlib_get_buffer (vm, bi0);
1262 e0 = b0->error;
1263
1264 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1265
1266 /* Increment drop/punt counters. */
Damjan Marion586afd72017-04-05 19:18:20 +02001267 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268
1269 /* Increment super-interface drop/punt counters for sub-interfaces. */
1270 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
Damjan Marion586afd72017-04-05 19:18:20 +02001271 vlib_increment_simple_counter (cm, thread_index,
1272 sw_if0->sup_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 sw_if0->sup_sw_if_index != sw_if_index0);
1274
1275 if (PREDICT_FALSE (e0 != current_error))
1276 {
1277 current_counter -= 1;
1278
1279 vlib_error_elog_count (vm, current_counter_index,
1280 (current_counter
1281 - em->counters[current_counter_index]));
Dave Barachba868bb2016-08-08 09:51:21 -04001282
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 em->counters[current_counter_index] = current_counter;
1284
1285 do_packet (vm, e0);
1286 current_error = e0;
1287 current_counter_index = counter_index (vm, e0);
1288 current_counter = em->counters[current_counter_index];
1289 }
1290 }
1291
1292 if (n_errors_current_sw_if_index > 0)
1293 {
Dave Barachba868bb2016-08-08 09:51:21 -04001294 vnet_sw_interface_t *si;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295
Damjan Marion586afd72017-04-05 19:18:20 +02001296 vlib_increment_simple_counter (cm, thread_index, current_sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297 n_errors_current_sw_if_index);
1298
1299 si = vnet_get_sw_interface (vnm, current_sw_if_index);
1300 if (si->sup_sw_if_index != current_sw_if_index)
Damjan Marion586afd72017-04-05 19:18:20 +02001301 vlib_increment_simple_counter (cm, thread_index, si->sup_sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -04001302 n_errors_current_sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001303 }
1304
1305 vlib_error_elog_count (vm, current_counter_index,
1306 (current_counter
1307 - em->counters[current_counter_index]));
1308
1309 /* Return cached counter. */
1310 em->counters[current_counter_index] = current_counter;
1311
1312 /* Save memory for next iteration. */
1313 memory[disposition] = current_error;
1314
Dave Barachba868bb2016-08-08 09:51:21 -04001315 if (disposition == VNET_ERROR_DISPOSITION_DROP || !vm->os_punt_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 {
Dave Barachba868bb2016-08-08 09:51:21 -04001317 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001318
1319 /* If there is no punt function, free the frame as well. */
Dave Barachba868bb2016-08-08 09:51:21 -04001320 if (disposition == VNET_ERROR_DISPOSITION_PUNT && !vm->os_punt_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001321 vlib_frame_free (vm, node, frame);
1322 }
1323 else
1324 vm->os_punt_frame (vm, node, frame);
1325
1326 return frame->n_vectors;
1327}
1328
Dave Barachba868bb2016-08-08 09:51:21 -04001329static inline void
1330pcap_drop_trace (vlib_main_t * vm,
1331 vnet_interface_main_t * im, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332{
Dave Barachba868bb2016-08-08 09:51:21 -04001333 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001334 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001335 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336 u32 bi0;
1337 i16 save_current_data;
1338 u16 save_current_length;
1339
1340 from = vlib_frame_vector_args (f);
1341
1342 while (n_left > 0)
1343 {
1344 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -04001345 {
1346 p1 = vlib_get_buffer (vm, from[1]);
1347 vlib_prefetch_buffer_header (p1, LOAD);
1348 }
1349
Ed Warnickecb9cada2015-12-08 15:45:58 -07001350 bi0 = from[0];
1351 b0 = vlib_get_buffer (vm, bi0);
1352 from++;
1353 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -04001354
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -04001356 if (im->pcap_drop_filter_hash
1357 && hash_get (im->pcap_drop_filter_hash, b0->error))
1358 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359
1360 /* Trace all drops, or drops received on a specific interface */
1361 if (im->pcap_sw_if_index == 0 ||
Dave Barachba868bb2016-08-08 09:51:21 -04001362 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
1363 {
1364 save_current_data = b0->current_data;
1365 save_current_length = b0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366
Dave Barachba868bb2016-08-08 09:51:21 -04001367 /*
1368 * Typically, we'll need to rewind the buffer
1369 */
1370 if (b0->current_data > 0)
1371 vlib_buffer_advance (b0, (word) - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372
Dave Barachba868bb2016-08-08 09:51:21 -04001373 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1374
1375 b0->current_data = save_current_data;
1376 b0->current_length = save_current_length;
1377 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001378 }
1379}
1380
Dave Barachba868bb2016-08-08 09:51:21 -04001381void
1382vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383{
Dave Barachba868bb2016-08-08 09:51:21 -04001384 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385
1386 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001387 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388
1389 if (is_add)
1390 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1391 else
1392 hash_unset (im->pcap_drop_filter_hash, error_index);
1393}
1394
1395static uword
1396process_drop (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001397 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398{
Dave Barachba868bb2016-08-08 09:51:21 -04001399 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400
1401 if (PREDICT_FALSE (im->drop_pcap_enable))
1402 pcap_drop_trace (vm, im, frame);
1403
1404 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
1405}
1406
1407static uword
1408process_punt (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001409 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410{
1411 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
1412}
1413
Dave Barachba868bb2016-08-08 09:51:21 -04001414/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001415VLIB_REGISTER_NODE (drop_buffers,static) = {
1416 .function = process_drop,
1417 .name = "error-drop",
1418 .flags = VLIB_NODE_FLAG_IS_DROP,
1419 .vector_size = sizeof (u32),
1420 .format_trace = format_vnet_error_trace,
1421 .validate_frame = validate_error_frame,
1422};
Dave Barachba868bb2016-08-08 09:51:21 -04001423/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424
Dave Barachba868bb2016-08-08 09:51:21 -04001425VLIB_NODE_FUNCTION_MULTIARCH (drop_buffers, process_drop);
Damjan Marion1c80e832016-05-11 23:07:18 +02001426
Dave Barachba868bb2016-08-08 09:51:21 -04001427/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428VLIB_REGISTER_NODE (punt_buffers,static) = {
1429 .function = process_punt,
1430 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
1431 | VLIB_NODE_FLAG_IS_PUNT),
1432 .name = "error-punt",
1433 .vector_size = sizeof (u32),
1434 .format_trace = format_vnet_error_trace,
1435 .validate_frame = validate_error_frame,
1436};
Dave Barachba868bb2016-08-08 09:51:21 -04001437/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438
Dave Barachba868bb2016-08-08 09:51:21 -04001439VLIB_NODE_FUNCTION_MULTIARCH (punt_buffers, process_punt);
Damjan Marion1c80e832016-05-11 23:07:18 +02001440
Dave Barachba868bb2016-08-08 09:51:21 -04001441/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node,static) = {
1443 .function = vnet_per_buffer_interface_output,
1444 .name = "interface-output",
1445 .vector_size = sizeof (u32),
1446};
Dave Barachba868bb2016-08-08 09:51:21 -04001447/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448
Dave Barach87040972019-01-24 15:12:53 -05001449/* Convenience node to drop a vector of buffers with a "misc error". */
1450static uword
1451misc_drop_buffers (vlib_main_t * vm,
1452 vlib_node_runtime_t * node, vlib_frame_t * frame)
1453{
1454 return vlib_error_drop_buffers (vm, node, vlib_frame_vector_args (frame),
1455 /* buffer stride */ 1,
1456 frame->n_vectors,
1457 /* next */ 0,
1458 node->node_index,
1459 /* error */ 0);
1460}
1461
1462static char *misc_drop_buffers_error_strings[] = {
1463 [0] = "misc. errors",
1464};
1465
1466/* *INDENT-OFF* */
1467VLIB_REGISTER_NODE (misc_drop_buffers_node,static) = {
1468 .function = misc_drop_buffers,
1469 .name = "misc-drop-buffers",
1470 .vector_size = sizeof (u32),
1471 .n_errors = 1,
1472 .n_next_nodes = 1,
1473 .next_nodes = {
1474 "error-drop",
1475 },
1476 .error_strings = misc_drop_buffers_error_strings,
1477};
1478/* *INDENT-ON* */
1479
Dave Barachba868bb2016-08-08 09:51:21 -04001480VLIB_NODE_FUNCTION_MULTIARCH (vnet_per_buffer_interface_output_node,
1481 vnet_per_buffer_interface_output);
Damjan Marion1c80e832016-05-11 23:07:18 +02001482
Damjan Marion152e21d2016-11-29 14:55:43 +01001483static uword
1484interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1485 vlib_frame_t * from_frame)
1486{
1487 vnet_main_t *vnm = vnet_get_main ();
1488 u32 last_sw_if_index = ~0;
1489 vlib_frame_t *to_frame = 0;
1490 vnet_hw_interface_t *hw = 0;
1491 u32 *from, *to_next = 0;
1492 u32 n_left_from;
1493
1494 from = vlib_frame_vector_args (from_frame);
1495 n_left_from = from_frame->n_vectors;
1496 while (n_left_from > 0)
1497 {
1498 u32 bi0;
1499 vlib_buffer_t *b0;
1500 u32 sw_if_index0;
1501
1502 bi0 = from[0];
1503 from++;
1504 n_left_from--;
1505 b0 = vlib_get_buffer (vm, bi0);
1506 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1507
1508 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1509 {
1510 if (to_frame)
1511 {
1512 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1513 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1514 }
1515 last_sw_if_index = sw_if_index0;
1516 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1517 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1518 to_next = vlib_frame_vector_args (to_frame);
1519 }
1520
1521 to_next[0] = bi0;
1522 to_next++;
1523 to_frame->n_vectors++;
1524 }
1525 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1526 return from_frame->n_vectors;
1527}
1528
1529/* *INDENT-OFF* */
1530VLIB_REGISTER_NODE (interface_tx, static) = {
1531 .function = interface_tx_node_fn,
1532 .name = "interface-tx",
1533 .vector_size = sizeof (u32),
1534 .n_next_nodes = 1,
1535 .next_nodes = {
1536 [0] = "error-drop",
1537 },
1538};
1539
1540VNET_FEATURE_ARC_INIT (interface_output, static) =
1541{
1542 .arc_name = "interface-output",
1543 .start_nodes = VNET_FEATURES (0),
Dave Baracha25def72018-11-26 11:04:45 -05001544 .last_in_arc = "interface-tx",
Damjan Marion152e21d2016-11-29 14:55:43 +01001545 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1546};
1547
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001548VNET_FEATURE_INIT (span_tx, static) = {
1549 .arc_name = "interface-output",
1550 .node_name = "span-output",
1551 .runs_before = VNET_FEATURES ("interface-tx"),
1552};
1553
Matthew Smith537eeec2018-04-09 11:49:20 -05001554VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1555 .arc_name = "interface-output",
1556 .node_name = "ipsec-if-output",
1557 .runs_before = VNET_FEATURES ("interface-tx"),
1558};
1559
Damjan Marion152e21d2016-11-29 14:55:43 +01001560VNET_FEATURE_INIT (interface_tx, static) = {
1561 .arc_name = "interface-output",
1562 .node_name = "interface-tx",
1563 .runs_before = 0,
1564};
1565/* *INDENT-ON* */
1566
Ed Warnickecb9cada2015-12-08 15:45:58 -07001567clib_error_t *
1568vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1569 u32 hw_if_index,
1570 u32 is_create)
1571{
Dave Barachba868bb2016-08-08 09:51:21 -04001572 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001573 u32 next_index;
1574
John Loe5453d02018-01-23 19:21:34 -05001575 if (hi->output_node_index == 0)
1576 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577
John Loe5453d02018-01-23 19:21:34 -05001578 next_index = vlib_node_add_next
1579 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1580 hi->output_node_index);
1581 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001582
1583 return 0;
1584}
1585
Dave Barachba868bb2016-08-08 09:51:21 -04001586VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1587 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001588
John Loe5453d02018-01-23 19:21:34 -05001589void
1590vnet_set_interface_output_node (vnet_main_t * vnm,
1591 u32 hw_if_index, u32 node_index)
1592{
1593 ASSERT (node_index);
1594 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1595 u32 next_index = vlib_node_add_next
1596 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1597 hi->output_node_next_index = next_index;
1598 hi->output_node_index = node_index;
1599}
1600
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601static clib_error_t *
1602pcap_drop_trace_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001603 unformat_input_t * input,
1604 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605{
Dave Barachba868bb2016-08-08 09:51:21 -04001606 vnet_main_t *vnm = vnet_get_main ();
1607 vnet_interface_main_t *im = &vnm->interface_main;
1608 u8 *filename;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001609 u32 max;
1610 int matched = 0;
Dave Barachba868bb2016-08-08 09:51:21 -04001611 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001612
Dave Barachba868bb2016-08-08 09:51:21 -04001613 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614 {
1615 if (unformat (input, "on"))
Dave Barachba868bb2016-08-08 09:51:21 -04001616 {
1617 if (im->drop_pcap_enable == 0)
1618 {
1619 if (im->pcap_filename == 0)
1620 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001621
Dave Barachb7b92992018-10-17 10:38:51 -04001622 clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
Dave Barachba868bb2016-08-08 09:51:21 -04001623 im->pcap_main.file_name = (char *) im->pcap_filename;
1624 im->pcap_main.n_packets_to_capture = 100;
1625 if (im->pcap_pkts_to_capture)
1626 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1627
1628 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1629 im->drop_pcap_enable = 1;
1630 matched = 1;
1631 vlib_cli_output (vm, "pcap drop capture on...");
1632 }
1633 else
1634 {
1635 vlib_cli_output (vm, "pcap drop capture already on...");
1636 }
1637 matched = 1;
1638 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001639 else if (unformat (input, "off"))
Dave Barachba868bb2016-08-08 09:51:21 -04001640 {
1641 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001642
Dave Barachba868bb2016-08-08 09:51:21 -04001643 if (im->drop_pcap_enable)
1644 {
1645 vlib_cli_output (vm, "captured %d pkts...",
1646 im->pcap_main.n_packets_captured);
1647 if (im->pcap_main.n_packets_captured)
1648 {
1649 im->pcap_main.n_packets_to_capture =
1650 im->pcap_main.n_packets_captured;
1651 error = pcap_write (&im->pcap_main);
1652 if (error)
1653 clib_error_report (error);
1654 else
1655 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1656 }
1657 }
1658 else
1659 {
1660 vlib_cli_output (vm, "pcap drop capture already off...");
1661 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001662
Dave Barachba868bb2016-08-08 09:51:21 -04001663 im->drop_pcap_enable = 0;
1664 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001665 else if (unformat (input, "max %d", &max))
Dave Barachba868bb2016-08-08 09:51:21 -04001666 {
1667 im->pcap_pkts_to_capture = max;
1668 matched = 1;
1669 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001670
Dave Barachba868bb2016-08-08 09:51:21 -04001671 else if (unformat (input, "intfc %U",
1672 unformat_vnet_sw_interface, vnm,
1673 &im->pcap_sw_if_index))
1674 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001675 else if (unformat (input, "intfc any"))
Dave Barachba868bb2016-08-08 09:51:21 -04001676 {
1677 im->pcap_sw_if_index = 0;
1678 matched = 1;
1679 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680 else if (unformat (input, "file %s", &filename))
Dave Barachba868bb2016-08-08 09:51:21 -04001681 {
1682 u8 *chroot_filename;
1683 /* Brain-police user path input */
1684 if (strstr ((char *) filename, "..")
1685 || index ((char *) filename, '/'))
1686 {
1687 vlib_cli_output (vm, "illegal characters in filename '%s'",
1688 filename);
1689 continue;
1690 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691
Dave Barachba868bb2016-08-08 09:51:21 -04001692 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1693 vec_free (filename);
1694
1695 if (im->pcap_filename)
1696 vec_free (im->pcap_filename);
Dave Barachba868bb2016-08-08 09:51:21 -04001697 im->pcap_filename = chroot_filename;
jerryian361abef2017-04-10 18:48:07 +08001698 im->pcap_main.file_name = (char *) im->pcap_filename;
Dave Barachba868bb2016-08-08 09:51:21 -04001699 matched = 1;
1700 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701 else if (unformat (input, "status"))
Dave Barachba868bb2016-08-08 09:51:21 -04001702 {
1703 if (im->drop_pcap_enable == 0)
1704 {
1705 vlib_cli_output (vm, "pcap drop capture is off...");
1706 continue;
1707 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708
Dave Barachba868bb2016-08-08 09:51:21 -04001709 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1710 im->pcap_main.n_packets_captured,
1711 im->pcap_main.n_packets_to_capture);
1712 matched = 1;
1713 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001714
1715 else
Dave Barachba868bb2016-08-08 09:51:21 -04001716 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717 }
1718
1719 if (matched == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001720 return clib_error_return (0, "unknown input `%U'",
1721 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722
1723 return 0;
1724}
1725
Dave Barachba868bb2016-08-08 09:51:21 -04001726/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001727VLIB_CLI_COMMAND (pcap_trace_command, static) = {
Dave Barachba868bb2016-08-08 09:51:21 -04001728 .path = "pcap drop trace",
1729 .short_help =
1730 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1731 .function = pcap_drop_trace_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732};
Dave Barachba868bb2016-08-08 09:51:21 -04001733/* *INDENT-ON* */
1734
1735/*
1736 * fd.io coding-style-patch-verification: ON
1737 *
1738 * Local Variables:
1739 * eval: (c-set-style "gnu")
1740 * End:
1741 */