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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * interface_output.c: interface output node
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vnet/feature/feature.h>
46 u8 data[128 - sizeof (u32)];
48 interface_output_trace_t;
51 format_vnet_interface_output_trace (u8 * s, va_list * va)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
54 vlib_node_t *node = va_arg (*va, vlib_node_t *);
55 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
56 vnet_main_t *vnm = vnet_get_main ();
57 vnet_sw_interface_t *si;
60 if (t->sw_if_index != (u32) ~ 0)
62 indent = format_get_indent (s);
64 if (pool_is_free_index
65 (vnm->interface_main.sw_interfaces, t->sw_if_index))
67 /* the interface may have been deleted by the time the trace is printed */
68 s = format (s, "sw_if_index: %d\n%U%U",
70 format_white_space, indent,
71 node->format_buffer ? node->
72 format_buffer : format_hex_bytes, t->data,
77 si = vnet_get_sw_interface (vnm, t->sw_if_index);
79 s = format (s, "%U\n%U%U",
80 format_vnet_sw_interface_name, vnm, si,
81 format_white_space, indent,
82 node->format_buffer ? node->
83 format_buffer : format_hex_bytes, t->data,
91 vnet_interface_output_trace (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * frame, uword n_buffers)
98 from = vlib_frame_args (frame);
103 vlib_buffer_t *b0, *b1;
104 interface_output_trace_t *t0, *t1;
106 /* Prefetch next iteration. */
107 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
108 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
113 b0 = vlib_get_buffer (vm, bi0);
114 b1 = vlib_get_buffer (vm, bi1);
116 if (b0->flags & VLIB_BUFFER_IS_TRACED)
118 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
119 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
120 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
123 if (b1->flags & VLIB_BUFFER_IS_TRACED)
125 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
126 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
127 clib_memcpy (t1->data, vlib_buffer_get_current (b1),
138 interface_output_trace_t *t0;
142 b0 = vlib_get_buffer (vm, bi0);
144 if (b0->flags & VLIB_BUFFER_IS_TRACED)
146 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
147 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
148 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
156 static never_inline u32
157 slow_path (vlib_main_t * vm,
160 u32 n_left_to_tx, u32 * to_tx, u32 * n_slow_bytes_result)
162 /* We've already enqueued a single buffer. */
164 u32 n_slow_bytes = 0;
166 while (n_left_to_tx > 0)
172 n_slow_bytes += vlib_buffer_length_in_chain (vm, b);
174 /* Be grumpy about zero length buffers for benefit of
175 driver tx function. */
176 ASSERT (b->current_length > 0);
178 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
182 b = vlib_get_buffer (vm, bi);
185 /* Ran out of space in next frame trying to enqueue buffers? */
186 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
189 *n_slow_bytes_result = n_slow_bytes;
194 * Increment TX stats. Roll up consecutive increments to the same sw_if_index
195 * into one increment.
197 static_always_inline void
198 incr_output_stats (vnet_main_t * vnm,
202 u32 * last_sw_if_index, u32 * n_packets, u32 * n_bytes)
204 vnet_interface_main_t *im;
206 if (PREDICT_TRUE (sw_if_index == *last_sw_if_index))
213 if (PREDICT_TRUE (*last_sw_if_index != ~0))
215 im = &vnm->interface_main;
217 vlib_increment_combined_counter (im->combined_sw_if_counters
218 + VNET_INTERFACE_COUNTER_TX,
221 *n_packets, *n_bytes);
223 *last_sw_if_index = sw_if_index;
230 /* Interface output functions. */
232 vnet_interface_output_node_flatten (vlib_main_t * vm,
233 vlib_node_runtime_t * node,
234 vlib_frame_t * frame)
236 vnet_main_t *vnm = vnet_get_main ();
237 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
238 vnet_sw_interface_t *si;
239 vnet_hw_interface_t *hi;
240 u32 n_left_to_tx, *from, *from_end, *to_tx;
241 u32 n_bytes, n_buffers, n_packets;
242 u32 last_sw_if_index;
243 u32 cpu_index = vm->cpu_index;
245 n_buffers = frame->n_vectors;
247 if (node->flags & VLIB_NODE_FLAG_TRACE)
248 vnet_interface_output_trace (vm, node, frame, n_buffers);
250 from = vlib_frame_args (frame);
253 return vlib_error_drop_buffers (vm, node, from,
254 /* buffer stride */ 1,
256 VNET_INTERFACE_OUTPUT_NEXT_DROP,
258 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
260 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
261 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
262 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
263 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
265 vlib_simple_counter_main_t *cm;
267 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
268 VNET_INTERFACE_COUNTER_TX_ERROR);
269 vlib_increment_simple_counter (cm, cpu_index,
270 rt->sw_if_index, n_buffers);
271 return vlib_error_drop_buffers (vm, node, from,
272 /* buffer stride */ 1,
274 VNET_INTERFACE_OUTPUT_NEXT_DROP,
276 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
279 from_end = from + n_buffers;
281 /* Total byte count of all buffers. */
284 last_sw_if_index = ~0;
286 while (from < from_end)
288 /* Get new next frame since previous incomplete frame may have less
289 than VNET_FRAME_SIZE vectors in it. */
290 vlib_get_new_next_frame (vm, node, VNET_INTERFACE_OUTPUT_NEXT_TX,
291 to_tx, n_left_to_tx);
293 while (from + 4 <= from_end && n_left_to_tx >= 2)
296 vlib_buffer_t *b0, *b1;
298 /* Prefetch next iteration. */
299 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
300 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
310 b0 = vlib_get_buffer (vm, bi0);
311 b1 = vlib_get_buffer (vm, bi1);
313 /* Be grumpy about zero length buffers for benefit of
314 driver tx function. */
315 ASSERT (b0->current_length > 0);
316 ASSERT (b1->current_length > 0);
319 ((b0->flags | b1->flags) & VLIB_BUFFER_NEXT_PRESENT))
321 u32 n_buffers, n_slow_bytes, i;
328 /* Do slow path two times. */
329 for (i = 0; i < 2; i++)
331 u32 bi = i ? bi1 : bi0;
332 vlib_buffer_t *b = i ? b1 : b0;
334 n_buffers = slow_path (vm, bi, b,
335 n_left_to_tx, to_tx, &n_slow_bytes);
337 /* Not enough room for single packet? */
343 n_left_to_tx -= n_buffers;
344 incr_output_stats (vnm, cpu_index, n_slow_bytes,
345 vnet_buffer (b)->sw_if_index[VLIB_TX],
346 &last_sw_if_index, &n_packets, &n_bytes);
351 incr_output_stats (vnm, cpu_index,
352 vlib_buffer_length_in_chain (vm, b0),
353 vnet_buffer (b0)->sw_if_index[VLIB_TX],
354 &last_sw_if_index, &n_packets, &n_bytes);
355 incr_output_stats (vnm, cpu_index,
356 vlib_buffer_length_in_chain (vm, b0),
357 vnet_buffer (b1)->sw_if_index[VLIB_TX],
358 &last_sw_if_index, &n_packets, &n_bytes);
362 while (from + 1 <= from_end && n_left_to_tx >= 1)
373 b0 = vlib_get_buffer (vm, bi0);
375 /* Be grumpy about zero length buffers for benefit of
376 driver tx function. */
377 ASSERT (b0->current_length > 0);
379 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_NEXT_PRESENT))
381 u32 n_buffers, n_slow_bytes;
388 n_buffers = slow_path (vm, bi0, b0,
389 n_left_to_tx, to_tx, &n_slow_bytes);
391 /* Not enough room for single packet? */
397 n_left_to_tx -= n_buffers;
399 incr_output_stats (vnm, cpu_index,
400 vlib_buffer_length_in_chain (vm, b0),
401 vnet_buffer (b0)->sw_if_index[VLIB_TX],
402 &last_sw_if_index, &n_packets, &n_bytes);
406 vlib_put_next_frame (vm, node, VNET_INTERFACE_OUTPUT_NEXT_TX,
410 /* Final update of interface stats. */
411 incr_output_stats (vnm, cpu_index, 0, ~0, /* ~0 will flush stats */
412 &last_sw_if_index, &n_packets, &n_bytes);
417 VLIB_NODE_FUNCTION_MULTIARCH_CLONE (vnet_interface_output_node_flatten);
418 CLIB_MULTIARCH_SELECT_FN (vnet_interface_output_node_flatten);
421 vnet_interface_output_node (vlib_main_t * vm,
422 vlib_node_runtime_t * node, vlib_frame_t * frame)
424 vnet_main_t *vnm = vnet_get_main ();
425 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
426 vnet_sw_interface_t *si;
427 vnet_hw_interface_t *hi;
428 u32 n_left_to_tx, *from, *from_end, *to_tx;
429 u32 n_bytes, n_buffers, n_packets;
430 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
431 u32 cpu_index = vm->cpu_index;
432 vnet_interface_main_t *im = &vnm->interface_main;
433 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
434 u32 current_config_index = ~0;
435 u8 arc = im->output_feature_arc_index;
437 n_buffers = frame->n_vectors;
439 if (node->flags & VLIB_NODE_FLAG_TRACE)
440 vnet_interface_output_trace (vm, node, frame, n_buffers);
442 from = vlib_frame_args (frame);
445 return vlib_error_drop_buffers (vm, node, from,
446 /* buffer stride */ 1,
448 VNET_INTERFACE_OUTPUT_NEXT_DROP,
450 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
452 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
453 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
454 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
455 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
457 vlib_simple_counter_main_t *cm;
459 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
460 VNET_INTERFACE_COUNTER_TX_ERROR);
461 vlib_increment_simple_counter (cm, cpu_index,
462 rt->sw_if_index, n_buffers);
464 return vlib_error_drop_buffers (vm, node, from,
465 /* buffer stride */ 1,
467 VNET_INTERFACE_OUTPUT_NEXT_DROP,
469 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
472 from_end = from + n_buffers;
474 /* Total byte count of all buffers. */
478 /* interface-output feature arc handling */
479 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
481 vnet_feature_config_main_t *fcm;
482 fcm = vnet_feature_get_config_main (arc);
483 current_config_index = vnet_get_feature_config_index (arc,
485 vnet_get_config_data (&fcm->config_main, ¤t_config_index,
489 while (from < from_end)
491 /* Get new next frame since previous incomplete frame may have less
492 than VNET_FRAME_SIZE vectors in it. */
493 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
495 while (from + 8 <= from_end && n_left_to_tx >= 4)
497 u32 bi0, bi1, bi2, bi3;
498 vlib_buffer_t *b0, *b1, *b2, *b3;
499 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
501 /* Prefetch next iteration. */
502 vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
503 vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
504 vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
505 vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
519 b0 = vlib_get_buffer (vm, bi0);
520 b1 = vlib_get_buffer (vm, bi1);
521 b2 = vlib_get_buffer (vm, bi2);
522 b3 = vlib_get_buffer (vm, bi3);
524 /* Be grumpy about zero length buffers for benefit of
525 driver tx function. */
526 ASSERT (b0->current_length > 0);
527 ASSERT (b1->current_length > 0);
528 ASSERT (b2->current_length > 0);
529 ASSERT (b3->current_length > 0);
531 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
532 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
533 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
534 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
535 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
536 tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
537 tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
538 tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
540 n_bytes += n_bytes_b0 + n_bytes_b1;
541 n_bytes += n_bytes_b2 + n_bytes_b3;
544 if (PREDICT_FALSE (current_config_index != ~0))
546 b0->feature_arc_index = arc;
547 b1->feature_arc_index = arc;
548 b2->feature_arc_index = arc;
549 b3->feature_arc_index = arc;
550 b0->current_config_index = current_config_index;
551 b1->current_config_index = current_config_index;
552 b2->current_config_index = current_config_index;
553 b3->current_config_index = current_config_index;
556 /* update vlan subif tx counts, if required */
557 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
559 vlib_increment_combined_counter (im->combined_sw_if_counters +
560 VNET_INTERFACE_COUNTER_TX,
561 cpu_index, tx_swif0, 1,
565 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
568 vlib_increment_combined_counter (im->combined_sw_if_counters +
569 VNET_INTERFACE_COUNTER_TX,
570 cpu_index, tx_swif1, 1,
574 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
577 vlib_increment_combined_counter (im->combined_sw_if_counters +
578 VNET_INTERFACE_COUNTER_TX,
579 cpu_index, tx_swif2, 1,
582 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
585 vlib_increment_combined_counter (im->combined_sw_if_counters +
586 VNET_INTERFACE_COUNTER_TX,
587 cpu_index, tx_swif3, 1,
592 while (from + 1 <= from_end && n_left_to_tx >= 1)
604 b0 = vlib_get_buffer (vm, bi0);
606 /* Be grumpy about zero length buffers for benefit of
607 driver tx function. */
608 ASSERT (b0->current_length > 0);
610 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
611 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
612 n_bytes += n_bytes_b0;
615 if (PREDICT_FALSE (current_config_index != ~0))
617 b0->feature_arc_index = arc;
618 b0->current_config_index = current_config_index;
621 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
624 vlib_increment_combined_counter (im->combined_sw_if_counters +
625 VNET_INTERFACE_COUNTER_TX,
626 cpu_index, tx_swif0, 1,
631 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
634 /* Update main interface stats. */
635 vlib_increment_combined_counter (im->combined_sw_if_counters
636 + VNET_INTERFACE_COUNTER_TX,
638 rt->sw_if_index, n_packets, n_bytes);
642 VLIB_NODE_FUNCTION_MULTIARCH_CLONE (vnet_interface_output_node);
643 CLIB_MULTIARCH_SELECT_FN (vnet_interface_output_node);
645 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
647 vnet_per_buffer_interface_output (vlib_main_t * vm,
648 vlib_node_runtime_t * node,
649 vlib_frame_t * frame)
651 vnet_main_t *vnm = vnet_get_main ();
652 u32 n_left_to_next, *from, *to_next;
653 u32 n_left_from, next_index;
655 n_left_from = frame->n_vectors;
657 from = vlib_frame_args (frame);
658 next_index = node->cached_next_index;
660 while (n_left_from > 0)
662 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
664 while (n_left_from >= 4 && n_left_to_next >= 2)
666 u32 bi0, bi1, next0, next1;
667 vlib_buffer_t *b0, *b1;
668 vnet_hw_interface_t *hi0, *hi1;
670 /* Prefetch next iteration. */
671 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
672 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
683 b0 = vlib_get_buffer (vm, bi0);
684 b1 = vlib_get_buffer (vm, bi1);
687 vnet_get_sup_hw_interface (vnm,
688 vnet_buffer (b0)->sw_if_index
691 vnet_get_sup_hw_interface (vnm,
692 vnet_buffer (b1)->sw_if_index
695 next0 = hi0->hw_if_index;
696 next1 = hi1->hw_if_index;
698 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
699 n_left_to_next, bi0, bi1, next0,
703 while (n_left_from > 0 && n_left_to_next > 0)
707 vnet_hw_interface_t *hi0;
716 b0 = vlib_get_buffer (vm, bi0);
719 vnet_get_sup_hw_interface (vnm,
720 vnet_buffer (b0)->sw_if_index
723 next0 = hi0->hw_if_index;
725 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
726 n_left_to_next, bi0, next0);
729 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
732 return frame->n_vectors;
736 counter_index (vlib_main_t * vm, vlib_error_t e)
741 ni = vlib_error_get_node (e);
742 n = vlib_get_node (vm, ni);
744 ci = vlib_error_get_code (e);
745 ASSERT (ci < n->n_errors);
747 ci += n->error_heap_index;
753 format_vnet_error_trace (u8 * s, va_list * va)
755 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
756 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
757 vlib_error_t *e = va_arg (*va, vlib_error_t *);
758 vlib_node_t *error_node;
759 vlib_error_main_t *em = &vm->error_main;
762 error_node = vlib_get_node (vm, vlib_error_get_node (e[0]));
763 i = counter_index (vm, e[0]);
764 s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]);
770 trace_errors_with_buffers (vlib_main_t * vm,
771 vlib_node_runtime_t * node, vlib_frame_t * frame)
773 u32 n_left, *buffers;
775 buffers = vlib_frame_vector_args (frame);
776 n_left = frame->n_vectors;
781 vlib_buffer_t *b0, *b1;
782 vlib_error_t *t0, *t1;
784 /* Prefetch next iteration. */
785 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
786 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
791 b0 = vlib_get_buffer (vm, bi0);
792 b1 = vlib_get_buffer (vm, bi1);
794 if (b0->flags & VLIB_BUFFER_IS_TRACED)
796 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
799 if (b1->flags & VLIB_BUFFER_IS_TRACED)
801 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
816 b0 = vlib_get_buffer (vm, bi0);
818 if (b0->flags & VLIB_BUFFER_IS_TRACED)
820 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
829 validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
831 uword node_index = vlib_error_get_node (e[0]);
832 uword code = vlib_error_get_code (e[0]);
835 if (node_index >= vec_len (vm->node_main.nodes))
836 return format (0, "[%d], node index out of range 0x%x, error 0x%x",
837 index, node_index, e[0]);
839 n = vlib_get_node (vm, node_index);
840 if (code >= n->n_errors)
841 return format (0, "[%d], code %d out of range for node %v",
842 index, code, n->name);
848 validate_error_frame (vlib_main_t * vm,
849 vlib_node_runtime_t * node, vlib_frame_t * f)
851 u32 *buffers = vlib_frame_args (f);
856 for (i = 0; i < f->n_vectors; i++)
858 b = vlib_get_buffer (vm, buffers[i]);
859 msg = validate_error (vm, &b->error, i);
869 VNET_ERROR_DISPOSITION_DROP,
870 VNET_ERROR_DISPOSITION_PUNT,
871 VNET_ERROR_N_DISPOSITION,
872 } vnet_error_disposition_t;
875 do_packet (vlib_main_t * vm, vlib_error_t a)
877 vlib_error_main_t *em = &vm->error_main;
878 u32 i = counter_index (vm, a);
879 em->counters[i] += 1;
880 vlib_error_elog_count (vm, i, 1);
883 static_always_inline uword
884 process_drop_punt (vlib_main_t * vm,
885 vlib_node_runtime_t * node,
886 vlib_frame_t * frame, vnet_error_disposition_t disposition)
888 vnet_main_t *vnm = vnet_get_main ();
889 vlib_error_main_t *em = &vm->error_main;
890 u32 *buffers, *first_buffer;
891 vlib_error_t current_error;
892 u32 current_counter_index, n_errors_left;
893 u32 current_sw_if_index, n_errors_current_sw_if_index;
895 vlib_simple_counter_main_t *cm;
896 u32 cpu_index = vm->cpu_index;
898 static vlib_error_t memory[VNET_ERROR_N_DISPOSITION];
899 static char memory_init[VNET_ERROR_N_DISPOSITION];
901 buffers = vlib_frame_args (frame);
902 first_buffer = buffers;
905 vlib_buffer_t *b = vlib_get_buffer (vm, first_buffer[0]);
907 if (!memory_init[disposition])
909 memory_init[disposition] = 1;
910 memory[disposition] = b->error;
913 current_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
914 n_errors_current_sw_if_index = 0;
917 current_error = memory[disposition];
918 current_counter_index = counter_index (vm, memory[disposition]);
919 current_counter = em->counters[current_counter_index];
921 if (node->flags & VLIB_NODE_FLAG_TRACE)
922 trace_errors_with_buffers (vm, node, frame);
924 n_errors_left = frame->n_vectors;
925 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
926 (disposition == VNET_ERROR_DISPOSITION_PUNT
927 ? VNET_INTERFACE_COUNTER_PUNT
928 : VNET_INTERFACE_COUNTER_DROP));
930 while (n_errors_left >= 2)
932 vlib_buffer_t *b0, *b1;
933 vnet_sw_interface_t *sw_if0, *sw_if1;
936 u32 sw_if_index0, sw_if_index1;
944 b0 = vlib_get_buffer (vm, bi0);
945 b1 = vlib_get_buffer (vm, bi1);
950 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
951 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
953 /* Speculate that sw_if_index == sw_if_index[01]. */
954 n_errors_current_sw_if_index += 2;
956 /* Speculatively assume all 2 (node, code) pairs are equal
957 to current (node, code). */
958 current_counter += 2;
960 if (PREDICT_FALSE (e0 != current_error
961 || e1 != current_error
962 || sw_if_index0 != current_sw_if_index
963 || sw_if_index1 != current_sw_if_index))
965 current_counter -= 2;
966 n_errors_current_sw_if_index -= 2;
968 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
969 vlib_increment_simple_counter (cm, cpu_index, sw_if_index1, 1);
971 /* Increment super-interface drop/punt counters for
973 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
974 vlib_increment_simple_counter
975 (cm, cpu_index, sw_if0->sup_sw_if_index,
976 sw_if0->sup_sw_if_index != sw_if_index0);
978 sw_if1 = vnet_get_sw_interface (vnm, sw_if_index1);
979 vlib_increment_simple_counter
980 (cm, cpu_index, sw_if1->sup_sw_if_index,
981 sw_if1->sup_sw_if_index != sw_if_index1);
983 em->counters[current_counter_index] = current_counter;
987 /* For 2 repeated errors, change current error. */
988 if (e0 == e1 && e1 != current_error)
991 current_counter_index = counter_index (vm, e0);
993 current_counter = em->counters[current_counter_index];
997 while (n_errors_left >= 1)
1000 vnet_sw_interface_t *sw_if0;
1002 u32 bi0, sw_if_index0;
1008 current_counter += 1;
1010 b0 = vlib_get_buffer (vm, bi0);
1013 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1015 /* Increment drop/punt counters. */
1016 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
1018 /* Increment super-interface drop/punt counters for sub-interfaces. */
1019 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
1020 vlib_increment_simple_counter (cm, cpu_index, sw_if0->sup_sw_if_index,
1021 sw_if0->sup_sw_if_index != sw_if_index0);
1023 if (PREDICT_FALSE (e0 != current_error))
1025 current_counter -= 1;
1027 vlib_error_elog_count (vm, current_counter_index,
1029 - em->counters[current_counter_index]));
1031 em->counters[current_counter_index] = current_counter;
1035 current_counter_index = counter_index (vm, e0);
1036 current_counter = em->counters[current_counter_index];
1040 if (n_errors_current_sw_if_index > 0)
1042 vnet_sw_interface_t *si;
1044 vlib_increment_simple_counter (cm, cpu_index, current_sw_if_index,
1045 n_errors_current_sw_if_index);
1047 si = vnet_get_sw_interface (vnm, current_sw_if_index);
1048 if (si->sup_sw_if_index != current_sw_if_index)
1049 vlib_increment_simple_counter (cm, cpu_index, si->sup_sw_if_index,
1050 n_errors_current_sw_if_index);
1053 vlib_error_elog_count (vm, current_counter_index,
1055 - em->counters[current_counter_index]));
1057 /* Return cached counter. */
1058 em->counters[current_counter_index] = current_counter;
1060 /* Save memory for next iteration. */
1061 memory[disposition] = current_error;
1063 if (disposition == VNET_ERROR_DISPOSITION_DROP || !vm->os_punt_frame)
1065 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
1067 /* If there is no punt function, free the frame as well. */
1068 if (disposition == VNET_ERROR_DISPOSITION_PUNT && !vm->os_punt_frame)
1069 vlib_frame_free (vm, node, frame);
1072 vm->os_punt_frame (vm, node, frame);
1074 return frame->n_vectors;
1078 pcap_drop_trace (vlib_main_t * vm,
1079 vnet_interface_main_t * im, vlib_frame_t * f)
1082 u32 n_left = f->n_vectors;
1083 vlib_buffer_t *b0, *p1;
1085 i16 save_current_data;
1086 u16 save_current_length;
1088 from = vlib_frame_vector_args (f);
1092 if (PREDICT_TRUE (n_left > 1))
1094 p1 = vlib_get_buffer (vm, from[1]);
1095 vlib_prefetch_buffer_header (p1, LOAD);
1099 b0 = vlib_get_buffer (vm, bi0);
1103 /* See if we're pointedly ignoring this specific error */
1104 if (im->pcap_drop_filter_hash
1105 && hash_get (im->pcap_drop_filter_hash, b0->error))
1108 /* Trace all drops, or drops received on a specific interface */
1109 if (im->pcap_sw_if_index == 0 ||
1110 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
1112 save_current_data = b0->current_data;
1113 save_current_length = b0->current_length;
1116 * Typically, we'll need to rewind the buffer
1118 if (b0->current_data > 0)
1119 vlib_buffer_advance (b0, (word) - b0->current_data);
1121 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1123 b0->current_data = save_current_data;
1124 b0->current_length = save_current_length;
1130 vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
1132 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1134 if (im->pcap_drop_filter_hash == 0)
1135 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
1138 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1140 hash_unset (im->pcap_drop_filter_hash, error_index);
1144 process_drop (vlib_main_t * vm,
1145 vlib_node_runtime_t * node, vlib_frame_t * frame)
1147 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1149 if (PREDICT_FALSE (im->drop_pcap_enable))
1150 pcap_drop_trace (vm, im, frame);
1152 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
1156 process_punt (vlib_main_t * vm,
1157 vlib_node_runtime_t * node, vlib_frame_t * frame)
1159 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
1163 VLIB_REGISTER_NODE (drop_buffers,static) = {
1164 .function = process_drop,
1165 .name = "error-drop",
1166 .flags = VLIB_NODE_FLAG_IS_DROP,
1167 .vector_size = sizeof (u32),
1168 .format_trace = format_vnet_error_trace,
1169 .validate_frame = validate_error_frame,
1173 VLIB_NODE_FUNCTION_MULTIARCH (drop_buffers, process_drop);
1176 VLIB_REGISTER_NODE (punt_buffers,static) = {
1177 .function = process_punt,
1178 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
1179 | VLIB_NODE_FLAG_IS_PUNT),
1180 .name = "error-punt",
1181 .vector_size = sizeof (u32),
1182 .format_trace = format_vnet_error_trace,
1183 .validate_frame = validate_error_frame,
1187 VLIB_NODE_FUNCTION_MULTIARCH (punt_buffers, process_punt);
1190 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node,static) = {
1191 .function = vnet_per_buffer_interface_output,
1192 .name = "interface-output",
1193 .vector_size = sizeof (u32),
1197 VLIB_NODE_FUNCTION_MULTIARCH (vnet_per_buffer_interface_output_node,
1198 vnet_per_buffer_interface_output);
1201 interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1202 vlib_frame_t * from_frame)
1204 vnet_main_t *vnm = vnet_get_main ();
1205 u32 last_sw_if_index = ~0;
1206 vlib_frame_t *to_frame = 0;
1207 vnet_hw_interface_t *hw = 0;
1208 u32 *from, *to_next = 0;
1211 from = vlib_frame_vector_args (from_frame);
1212 n_left_from = from_frame->n_vectors;
1213 while (n_left_from > 0)
1222 b0 = vlib_get_buffer (vm, bi0);
1223 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1225 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1229 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1230 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1232 last_sw_if_index = sw_if_index0;
1233 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1234 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1235 to_next = vlib_frame_vector_args (to_frame);
1240 to_frame->n_vectors++;
1242 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1243 return from_frame->n_vectors;
1247 VLIB_REGISTER_NODE (interface_tx, static) = {
1248 .function = interface_tx_node_fn,
1249 .name = "interface-tx",
1250 .vector_size = sizeof (u32),
1257 VNET_FEATURE_ARC_INIT (interface_output, static) =
1259 .arc_name = "interface-output",
1260 .start_nodes = VNET_FEATURES (0),
1261 .end_node = "interface-tx",
1262 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1265 VNET_FEATURE_INIT (span_tx, static) = {
1266 .arc_name = "interface-output",
1267 .node_name = "span-output",
1268 .runs_before = VNET_FEATURES ("interface-tx"),
1271 VNET_FEATURE_INIT (interface_tx, static) = {
1272 .arc_name = "interface-output",
1273 .node_name = "interface-tx",
1279 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1283 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1286 next_index = vlib_node_add_next_with_slot
1287 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1288 hi->output_node_index,
1289 /* next_index */ hw_if_index);
1291 ASSERT (next_index == hw_if_index);
1296 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1297 (vnet_per_buffer_interface_output_hw_interface_add_del);
1299 static clib_error_t *
1300 pcap_drop_trace_command_fn (vlib_main_t * vm,
1301 unformat_input_t * input,
1302 vlib_cli_command_t * cmd)
1304 vnet_main_t *vnm = vnet_get_main ();
1305 vnet_interface_main_t *im = &vnm->interface_main;
1309 clib_error_t *error = 0;
1311 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1313 if (unformat (input, "on"))
1315 if (im->drop_pcap_enable == 0)
1317 if (im->pcap_filename == 0)
1318 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
1320 memset (&im->pcap_main, 0, sizeof (im->pcap_main));
1321 im->pcap_main.file_name = (char *) im->pcap_filename;
1322 im->pcap_main.n_packets_to_capture = 100;
1323 if (im->pcap_pkts_to_capture)
1324 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1326 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1327 im->drop_pcap_enable = 1;
1329 vlib_cli_output (vm, "pcap drop capture on...");
1333 vlib_cli_output (vm, "pcap drop capture already on...");
1337 else if (unformat (input, "off"))
1341 if (im->drop_pcap_enable)
1343 vlib_cli_output (vm, "captured %d pkts...",
1344 im->pcap_main.n_packets_captured);
1345 if (im->pcap_main.n_packets_captured)
1347 im->pcap_main.n_packets_to_capture =
1348 im->pcap_main.n_packets_captured;
1349 error = pcap_write (&im->pcap_main);
1351 clib_error_report (error);
1353 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1358 vlib_cli_output (vm, "pcap drop capture already off...");
1361 im->drop_pcap_enable = 0;
1363 else if (unformat (input, "max %d", &max))
1365 im->pcap_pkts_to_capture = max;
1369 else if (unformat (input, "intfc %U",
1370 unformat_vnet_sw_interface, vnm,
1371 &im->pcap_sw_if_index))
1373 else if (unformat (input, "intfc any"))
1375 im->pcap_sw_if_index = 0;
1378 else if (unformat (input, "file %s", &filename))
1380 u8 *chroot_filename;
1381 /* Brain-police user path input */
1382 if (strstr ((char *) filename, "..")
1383 || index ((char *) filename, '/'))
1385 vlib_cli_output (vm, "illegal characters in filename '%s'",
1390 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1391 vec_free (filename);
1393 if (im->pcap_filename)
1394 vec_free (im->pcap_filename);
1395 vec_add1 (filename, 0);
1396 im->pcap_filename = chroot_filename;
1399 else if (unformat (input, "status"))
1401 if (im->drop_pcap_enable == 0)
1403 vlib_cli_output (vm, "pcap drop capture is off...");
1407 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1408 im->pcap_main.n_packets_captured,
1409 im->pcap_main.n_packets_to_capture);
1418 return clib_error_return (0, "unknown input `%U'",
1419 format_unformat_error, input);
1425 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
1426 .path = "pcap drop trace",
1428 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1429 .function = pcap_drop_trace_command_fn,
1434 * fd.io coding-style-patch-verification: ON
1437 * eval: (c-set-style "gnu")