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>
44 u8 data[128 - sizeof (u32)];
45 } interface_output_trace_t;
47 u8 * format_vnet_interface_output_trace (u8 * s, va_list * va)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
50 vlib_node_t * node = va_arg (*va, vlib_node_t *);
51 interface_output_trace_t * t = va_arg (*va, interface_output_trace_t *);
52 vnet_main_t * vnm = vnet_get_main();
53 vnet_sw_interface_t * si;
56 if (t->sw_if_index != (u32)~0)
58 si = vnet_get_sw_interface (vnm, t->sw_if_index);
59 indent = format_get_indent (s);
61 s = format (s, "%U\n%U%U",
62 format_vnet_sw_interface_name, vnm, si,
63 format_white_space, indent,
64 node->format_buffer ? node->format_buffer : format_hex_bytes,
65 t->data, sizeof (t->data));
71 vnet_interface_output_trace (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
79 from = vlib_frame_args (frame);
84 vlib_buffer_t * b0, * b1;
85 interface_output_trace_t * t0, * t1;
87 /* Prefetch next iteration. */
88 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
89 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
94 b0 = vlib_get_buffer (vm, bi0);
95 b1 = vlib_get_buffer (vm, bi1);
97 if (b0->flags & VLIB_BUFFER_IS_TRACED)
99 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
100 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
101 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
104 if (b1->flags & VLIB_BUFFER_IS_TRACED)
106 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
107 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
108 clib_memcpy (t1->data, vlib_buffer_get_current (b1),
119 interface_output_trace_t * t0;
123 b0 = vlib_get_buffer (vm, bi0);
125 if (b0->flags & VLIB_BUFFER_IS_TRACED)
127 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
128 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
129 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
137 static never_inline u32
138 slow_path (vlib_main_t * vm,
143 u32 * n_slow_bytes_result)
145 /* We've already enqueued a single buffer. */
147 u32 n_slow_bytes = 0;
149 while (n_left_to_tx > 0)
155 n_slow_bytes += vlib_buffer_length_in_chain (vm, b);
157 /* Be grumpy about zero length buffers for benefit of
158 driver tx function. */
159 ASSERT (b->current_length > 0);
161 if (! (b->flags & VLIB_BUFFER_NEXT_PRESENT))
165 b = vlib_get_buffer (vm, bi);
168 /* Ran out of space in next frame trying to enqueue buffers? */
169 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
172 *n_slow_bytes_result = n_slow_bytes;
177 * Increment TX stats. Roll up consecutive increments to the same sw_if_index
178 * into one increment.
181 void incr_output_stats (vnet_main_t * vnm,
185 u32 * last_sw_if_index,
188 vnet_interface_main_t * im;
190 if (PREDICT_TRUE (sw_if_index == *last_sw_if_index)) {
194 if (PREDICT_TRUE (*last_sw_if_index != ~0)) {
195 im = &vnm->interface_main;
197 vlib_increment_combined_counter (im->combined_sw_if_counters
198 + VNET_INTERFACE_COUNTER_TX,
204 *last_sw_if_index = sw_if_index;
211 /* Interface output functions. */
213 vnet_interface_output_node (vlib_main_t * vm,
214 vlib_node_runtime_t * node,
215 vlib_frame_t * frame)
217 vnet_main_t * vnm = vnet_get_main();
218 vnet_interface_output_runtime_t * rt = (void *) node->runtime_data;
219 vnet_sw_interface_t * si;
220 vnet_hw_interface_t * hi;
221 u32 n_left_to_tx, * from, * from_end, * to_tx;
222 u32 n_bytes, n_buffers, n_packets;
223 u32 last_sw_if_index;
224 u32 cpu_index = vm->cpu_index;
226 n_buffers = frame->n_vectors;
228 if (node->flags & VLIB_NODE_FLAG_TRACE)
229 vnet_interface_output_trace (vm, node, frame, n_buffers);
231 from = vlib_frame_args (frame);
234 return vlib_error_drop_buffers (vm, node,
236 /* buffer stride */ 1,
238 VNET_INTERFACE_OUTPUT_NEXT_DROP,
240 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
242 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
243 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
244 if (! (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
245 ! (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
247 vlib_simple_counter_main_t * cm;
249 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
250 VNET_INTERFACE_COUNTER_TX_ERROR);
251 vlib_increment_simple_counter (cm, cpu_index,
252 rt->sw_if_index, n_buffers);
253 return vlib_error_drop_buffers (vm, node,
255 /* buffer stride */ 1,
257 VNET_INTERFACE_OUTPUT_NEXT_DROP,
259 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
262 from_end = from + n_buffers;
264 /* Total byte count of all buffers. */
267 last_sw_if_index = ~0;
269 while (from < from_end)
271 /* Get new next frame since previous incomplete frame may have less
272 than VNET_FRAME_SIZE vectors in it. */
273 vlib_get_new_next_frame (vm, node, VNET_INTERFACE_OUTPUT_NEXT_TX,
274 to_tx, n_left_to_tx);
276 while (from + 4 <= from_end && n_left_to_tx >= 2)
279 vlib_buffer_t * b0, * b1;
281 /* Prefetch next iteration. */
282 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
283 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
293 b0 = vlib_get_buffer (vm, bi0);
294 b1 = vlib_get_buffer (vm, bi1);
296 /* Be grumpy about zero length buffers for benefit of
297 driver tx function. */
298 ASSERT (b0->current_length > 0);
299 ASSERT (b1->current_length > 0);
301 if (PREDICT_FALSE ((b0->flags | b1->flags) & VLIB_BUFFER_NEXT_PRESENT))
303 u32 n_buffers, n_slow_bytes, i;
310 /* Do slow path two times. */
311 for (i = 0; i < 2; i++)
313 u32 bi = i ? bi1 : bi0;
314 vlib_buffer_t * b = i ? b1 : b0;
316 n_buffers = slow_path (vm, bi, b,
317 n_left_to_tx, to_tx, &n_slow_bytes);
319 /* Not enough room for single packet? */
325 n_left_to_tx -= n_buffers;
326 incr_output_stats (vnm, cpu_index, n_slow_bytes,
327 vnet_buffer(b)->sw_if_index[VLIB_TX],
328 &last_sw_if_index, &n_packets, &n_bytes);
331 incr_output_stats (vnm, cpu_index,
332 vlib_buffer_length_in_chain (vm, b0),
333 vnet_buffer(b0)->sw_if_index[VLIB_TX],
334 &last_sw_if_index, &n_packets, &n_bytes);
335 incr_output_stats (vnm, cpu_index,
336 vlib_buffer_length_in_chain (vm, b0),
337 vnet_buffer(b1)->sw_if_index[VLIB_TX],
338 &last_sw_if_index, &n_packets, &n_bytes);
342 while (from + 1 <= from_end && n_left_to_tx >= 1)
353 b0 = vlib_get_buffer (vm, bi0);
355 /* Be grumpy about zero length buffers for benefit of
356 driver tx function. */
357 ASSERT (b0->current_length > 0);
359 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_NEXT_PRESENT))
361 u32 n_buffers, n_slow_bytes;
368 n_buffers = slow_path (vm, bi0, b0,
369 n_left_to_tx, to_tx, &n_slow_bytes);
371 /* Not enough room for single packet? */
377 n_left_to_tx -= n_buffers;
379 incr_output_stats (vnm, cpu_index,
380 vlib_buffer_length_in_chain (vm, b0),
381 vnet_buffer(b0)->sw_if_index[VLIB_TX],
382 &last_sw_if_index, &n_packets, &n_bytes);
386 vlib_put_next_frame (vm, node, VNET_INTERFACE_OUTPUT_NEXT_TX, n_left_to_tx);
389 /* Final update of interface stats. */
390 incr_output_stats (vnm, cpu_index, 0, ~0, /* ~0 will flush stats */
391 &last_sw_if_index, &n_packets, &n_bytes);
398 vnet_interface_output_node_no_flatten (vlib_main_t * vm,
399 vlib_node_runtime_t * node,
400 vlib_frame_t * frame)
402 vnet_main_t * vnm = vnet_get_main();
403 vnet_interface_output_runtime_t * rt = (void *) node->runtime_data;
404 vnet_sw_interface_t * si;
405 vnet_hw_interface_t * hi;
406 u32 n_left_to_tx, * from, * from_end, * to_tx;
407 u32 n_bytes, n_buffers, n_packets;
408 u32 n_bytes_b0, n_bytes_b1;
409 u32 cpu_index = vm->cpu_index;
410 vnet_interface_main_t * im = &vnm->interface_main;
411 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
413 n_buffers = frame->n_vectors;
415 if (node->flags & VLIB_NODE_FLAG_TRACE)
416 vnet_interface_output_trace (vm, node, frame, n_buffers);
418 from = vlib_frame_args (frame);
421 return vlib_error_drop_buffers (vm, node,
423 /* buffer stride */ 1,
425 VNET_INTERFACE_OUTPUT_NEXT_DROP,
427 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
429 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
430 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
431 if (! (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
432 ! (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
434 vlib_simple_counter_main_t * cm;
436 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
437 VNET_INTERFACE_COUNTER_TX_ERROR);
438 vlib_increment_simple_counter (cm, cpu_index,
439 rt->sw_if_index, n_buffers);
441 return vlib_error_drop_buffers (vm, node,
443 /* buffer stride */ 1,
445 VNET_INTERFACE_OUTPUT_NEXT_DROP,
447 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
450 from_end = from + n_buffers;
452 /* Total byte count of all buffers. */
456 while (from < from_end)
458 /* Get new next frame since previous incomplete frame may have less
459 than VNET_FRAME_SIZE vectors in it. */
460 vlib_get_new_next_frame (vm, node, next_index,
461 to_tx, n_left_to_tx);
463 while (from + 4 <= from_end && n_left_to_tx >= 2)
466 vlib_buffer_t * b0, * b1;
467 u32 tx_swif0, tx_swif1;
469 /* Prefetch next iteration. */
470 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
471 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
481 b0 = vlib_get_buffer (vm, bi0);
482 b1 = vlib_get_buffer (vm, bi1);
484 /* Be grumpy about zero length buffers for benefit of
485 driver tx function. */
486 ASSERT (b0->current_length > 0);
487 ASSERT (b1->current_length > 0);
489 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
490 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
491 tx_swif0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
492 tx_swif1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
494 n_bytes += n_bytes_b0 + n_bytes_b1;
497 if (PREDICT_FALSE(si->output_feature_bitmap &&
498 ((b0->flags & BUFFER_OUTPUT_FEAT_DONE) == 0)))
501 b0->flags |= BUFFER_OUTPUT_FEAT_DONE;
502 vnet_buffer(b0)->output_features.bitmap = si->output_feature_bitmap;
503 count_trailing_zeros(next0, vnet_buffer(b0)->output_features.bitmap);
504 vnet_buffer(b0)->output_features.bitmap &= ~(1 << next0);
505 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_tx,
506 n_left_to_tx, bi0, next0);
510 vnet_buffer(b0)->output_features.bitmap = 0;
512 if (PREDICT_FALSE(tx_swif0 != rt->sw_if_index))
514 /* update vlan subif tx counts, if required */
515 vlib_increment_combined_counter (im->combined_sw_if_counters
516 + VNET_INTERFACE_COUNTER_TX,
524 if (PREDICT_FALSE(si->output_feature_bitmap &&
525 ((b1->flags & BUFFER_OUTPUT_FEAT_DONE) == 0)))
528 b1->flags |= BUFFER_OUTPUT_FEAT_DONE;
529 vnet_buffer(b1)->output_features.bitmap = si->output_feature_bitmap;
530 count_trailing_zeros(next1, vnet_buffer(b1)->output_features.bitmap);
531 vnet_buffer(b1)->output_features.bitmap &= ~(1 << next1);
532 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_tx,
533 n_left_to_tx, bi1, next1);
537 vnet_buffer(b1)->output_features.bitmap = 0;
539 /* update vlan subif tx counts, if required */
540 if (PREDICT_FALSE(tx_swif1 != rt->sw_if_index))
543 vlib_increment_combined_counter (im->combined_sw_if_counters
544 + VNET_INTERFACE_COUNTER_TX,
554 while (from + 1 <= from_end && n_left_to_tx >= 1)
566 b0 = vlib_get_buffer (vm, bi0);
568 /* Be grumpy about zero length buffers for benefit of
569 driver tx function. */
570 ASSERT (b0->current_length > 0);
572 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
573 tx_swif0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
574 n_bytes += n_bytes_b0;
577 if (PREDICT_FALSE(si->output_feature_bitmap &&
578 ((b0->flags & BUFFER_OUTPUT_FEAT_DONE) == 0)))
581 b0->flags |= BUFFER_OUTPUT_FEAT_DONE;
582 vnet_buffer(b0)->output_features.bitmap = si->output_feature_bitmap;
583 count_trailing_zeros(next0, vnet_buffer(b0)->output_features.bitmap);
584 vnet_buffer(b0)->output_features.bitmap &= ~(1 << next0);
585 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_tx,
586 n_left_to_tx, bi0, next0);
590 vnet_buffer(b0)->output_features.bitmap = 0;
592 if (PREDICT_FALSE(tx_swif0 != rt->sw_if_index))
595 vlib_increment_combined_counter (im->combined_sw_if_counters
596 + VNET_INTERFACE_COUNTER_TX,
605 vlib_put_next_frame (vm, node, next_index,
609 /* Update main interface stats. */
610 vlib_increment_combined_counter (im->combined_sw_if_counters
611 + VNET_INTERFACE_COUNTER_TX,
620 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
622 vnet_per_buffer_interface_output (vlib_main_t * vm,
623 vlib_node_runtime_t * node,
624 vlib_frame_t * frame)
626 vnet_main_t * vnm = vnet_get_main();
627 u32 n_left_to_next, * from, * to_next;
628 u32 n_left_from, next_index;
630 n_left_from = frame->n_vectors;
632 from = vlib_frame_args (frame);
633 next_index = node->cached_next_index;
635 while (n_left_from > 0)
637 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
639 while (n_left_from >= 4 && n_left_to_next >= 2)
641 u32 bi0, bi1, next0, next1;
642 vlib_buffer_t * b0, * b1;
643 vnet_hw_interface_t * hi0, * hi1;
645 /* Prefetch next iteration. */
646 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
647 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
658 b0 = vlib_get_buffer (vm, bi0);
659 b1 = vlib_get_buffer (vm, bi1);
661 hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer (b0)->sw_if_index[VLIB_TX]);
662 hi1 = vnet_get_sup_hw_interface (vnm, vnet_buffer (b1)->sw_if_index[VLIB_TX]);
664 next0 = hi0->hw_if_index;
665 next1 = hi1->hw_if_index;
667 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, n_left_to_next,
668 bi0, bi1, next0, next1);
671 while (n_left_from > 0 && n_left_to_next > 0)
675 vnet_hw_interface_t * hi0;
684 b0 = vlib_get_buffer (vm, bi0);
686 hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer (b0)->sw_if_index[VLIB_TX]);
688 next0 = hi0->hw_if_index;
690 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next,
694 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
697 return frame->n_vectors;
701 counter_index (vlib_main_t * vm, vlib_error_t e)
706 ni = vlib_error_get_node (e);
707 n = vlib_get_node (vm, ni);
709 ci = vlib_error_get_code (e);
710 ASSERT (ci < n->n_errors);
712 ci += n->error_heap_index;
717 static u8 * format_vnet_error_trace (u8 * s, va_list * va)
719 vlib_main_t * vm = va_arg (*va, vlib_main_t *);
720 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
721 vlib_error_t * e = va_arg (*va, vlib_error_t *);
722 vlib_node_t * error_node;
723 vlib_error_main_t * em = &vm->error_main;
726 error_node = vlib_get_node (vm, vlib_error_get_node (e[0]));
727 i = counter_index (vm, e[0]);
728 s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]);
734 trace_errors_with_buffers (vlib_main_t * vm,
735 vlib_node_runtime_t * node,
736 vlib_frame_t * frame)
738 u32 n_left, * buffers;
740 buffers = vlib_frame_vector_args (frame);
741 n_left = frame->n_vectors;
746 vlib_buffer_t * b0, * b1;
747 vlib_error_t * t0, * t1;
749 /* Prefetch next iteration. */
750 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
751 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
756 b0 = vlib_get_buffer (vm, bi0);
757 b1 = vlib_get_buffer (vm, bi1);
759 if (b0->flags & VLIB_BUFFER_IS_TRACED)
761 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
764 if (b1->flags & VLIB_BUFFER_IS_TRACED)
766 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
781 b0 = vlib_get_buffer (vm, bi0);
783 if (b0->flags & VLIB_BUFFER_IS_TRACED)
785 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
794 validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
796 uword node_index = vlib_error_get_node (e[0]);
797 uword code = vlib_error_get_code (e[0]);
800 if (node_index >= vec_len (vm->node_main.nodes))
801 return format (0, "[%d], node index out of range 0x%x, error 0x%x",
802 index, node_index, e[0]);
804 n = vlib_get_node (vm, node_index);
805 if (code >= n->n_errors)
806 return format (0, "[%d], code %d out of range for node %v",
807 index, code, n->name);
813 validate_error_frame (vlib_main_t * vm,
814 vlib_node_runtime_t * node,
817 u32 * buffers = vlib_frame_args (f);
822 for (i = 0; i < f->n_vectors; i++)
824 b = vlib_get_buffer (vm, buffers[i]);
825 msg = validate_error (vm, &b->error, i);
834 VNET_ERROR_DISPOSITION_DROP,
835 VNET_ERROR_DISPOSITION_PUNT,
836 VNET_ERROR_N_DISPOSITION,
837 } vnet_error_disposition_t;
840 do_packet (vlib_main_t * vm, vlib_error_t a)
842 vlib_error_main_t * em = &vm->error_main;
843 u32 i = counter_index (vm, a);
844 em->counters[i] += 1;
845 vlib_error_elog_count (vm, i, 1);
848 static_always_inline uword
849 process_drop_punt (vlib_main_t * vm,
850 vlib_node_runtime_t * node,
851 vlib_frame_t * frame,
852 vnet_error_disposition_t disposition)
854 vnet_main_t * vnm = vnet_get_main();
855 vlib_error_main_t * em = &vm->error_main;
856 u32 * buffers, * first_buffer;
857 vlib_error_t current_error;
858 u32 current_counter_index, n_errors_left;
859 u32 current_sw_if_index, n_errors_current_sw_if_index;
861 vlib_simple_counter_main_t * cm;
862 u32 cpu_index = vm->cpu_index;
864 static vlib_error_t memory[VNET_ERROR_N_DISPOSITION];
865 static char memory_init[VNET_ERROR_N_DISPOSITION];
867 buffers = vlib_frame_args (frame);
868 first_buffer = buffers;
871 vlib_buffer_t * b = vlib_get_buffer (vm, first_buffer[0]);
873 if (! memory_init[disposition])
875 memory_init[disposition] = 1;
876 memory[disposition] = b->error;
879 current_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
880 n_errors_current_sw_if_index = 0;
883 current_error = memory[disposition];
884 current_counter_index = counter_index (vm, memory[disposition]);
885 current_counter = em->counters[current_counter_index];
887 if (node->flags & VLIB_NODE_FLAG_TRACE)
888 trace_errors_with_buffers (vm, node, frame);
890 n_errors_left = frame->n_vectors;
891 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
892 (disposition == VNET_ERROR_DISPOSITION_PUNT
893 ? VNET_INTERFACE_COUNTER_PUNT
894 : VNET_INTERFACE_COUNTER_DROP));
896 while (n_errors_left >= 2)
898 vlib_buffer_t * b0, * b1;
899 vnet_sw_interface_t * sw_if0, * sw_if1;
902 u32 sw_if_index0, sw_if_index1;
910 b0 = vlib_get_buffer (vm, bi0);
911 b1 = vlib_get_buffer (vm, bi1);
916 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
917 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
919 /* Speculate that sw_if_index == sw_if_index[01]. */
920 n_errors_current_sw_if_index += 2;
922 /* Speculatively assume all 2 (node, code) pairs are equal
923 to current (node, code). */
924 current_counter += 2;
926 if (PREDICT_FALSE (e0 != current_error
927 || e1 != current_error
928 || sw_if_index0 != current_sw_if_index
929 || sw_if_index1 != current_sw_if_index))
931 current_counter -= 2;
932 n_errors_current_sw_if_index -= 2;
934 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
935 vlib_increment_simple_counter (cm, cpu_index, sw_if_index1, 1);
937 /* Increment super-interface drop/punt counters for
939 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
940 vlib_increment_simple_counter
941 (cm, cpu_index, sw_if0->sup_sw_if_index,
942 sw_if0->sup_sw_if_index != sw_if_index0);
944 sw_if1 = vnet_get_sw_interface (vnm, sw_if_index1);
945 vlib_increment_simple_counter
946 (cm, cpu_index, sw_if1->sup_sw_if_index,
947 sw_if1->sup_sw_if_index != sw_if_index1);
949 em->counters[current_counter_index] = current_counter;
953 /* For 2 repeated errors, change current error. */
954 if (e0 == e1 && e1 != current_error)
957 current_counter_index = counter_index (vm, e0);
959 current_counter = em->counters[current_counter_index];
963 while (n_errors_left >= 1)
966 vnet_sw_interface_t * sw_if0;
968 u32 bi0, sw_if_index0;
974 current_counter += 1;
976 b0 = vlib_get_buffer (vm, bi0);
979 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
981 /* Increment drop/punt counters. */
982 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
984 /* Increment super-interface drop/punt counters for sub-interfaces. */
985 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
986 vlib_increment_simple_counter (cm, cpu_index, sw_if0->sup_sw_if_index,
987 sw_if0->sup_sw_if_index != sw_if_index0);
989 if (PREDICT_FALSE (e0 != current_error))
991 current_counter -= 1;
993 vlib_error_elog_count (vm, current_counter_index,
995 - em->counters[current_counter_index]));
997 em->counters[current_counter_index] = current_counter;
1001 current_counter_index = counter_index (vm, e0);
1002 current_counter = em->counters[current_counter_index];
1006 if (n_errors_current_sw_if_index > 0)
1008 vnet_sw_interface_t * si;
1010 vlib_increment_simple_counter (cm, cpu_index, current_sw_if_index,
1011 n_errors_current_sw_if_index);
1013 si = vnet_get_sw_interface (vnm, current_sw_if_index);
1014 if (si->sup_sw_if_index != current_sw_if_index)
1015 vlib_increment_simple_counter (cm, cpu_index, si->sup_sw_if_index,
1016 n_errors_current_sw_if_index);
1019 vlib_error_elog_count (vm, current_counter_index,
1021 - em->counters[current_counter_index]));
1023 /* Return cached counter. */
1024 em->counters[current_counter_index] = current_counter;
1026 /* Save memory for next iteration. */
1027 memory[disposition] = current_error;
1029 if (disposition == VNET_ERROR_DISPOSITION_DROP
1030 || ! vm->os_punt_frame)
1037 /* If there is no punt function, free the frame as well. */
1038 if (disposition == VNET_ERROR_DISPOSITION_PUNT && ! vm->os_punt_frame)
1039 vlib_frame_free (vm, node, frame);
1042 vm->os_punt_frame (vm, node, frame);
1044 return frame->n_vectors;
1048 pcap_drop_trace (vlib_main_t * vm,
1049 vnet_interface_main_t * im,
1053 u32 n_left = f->n_vectors;
1054 vlib_buffer_t * b0, * p1;
1056 i16 save_current_data;
1057 u16 save_current_length;
1059 from = vlib_frame_vector_args (f);
1063 if (PREDICT_TRUE (n_left > 1))
1065 p1 = vlib_get_buffer (vm, from[1]);
1066 vlib_prefetch_buffer_header (p1, LOAD);
1070 b0 = vlib_get_buffer (vm, bi0);
1074 /* See if we're pointedly ignoring this specific error */
1075 if (im->pcap_drop_filter_hash
1076 && hash_get (im->pcap_drop_filter_hash, b0->error))
1079 /* Trace all drops, or drops received on a specific interface */
1080 if (im->pcap_sw_if_index == 0 ||
1081 im->pcap_sw_if_index == vnet_buffer(b0)->sw_if_index [VLIB_RX])
1083 save_current_data = b0->current_data;
1084 save_current_length = b0->current_length;
1087 * Typically, we'll need to rewind the buffer
1089 if (b0->current_data > 0)
1090 vlib_buffer_advance (b0, (word) -b0->current_data);
1092 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1094 b0->current_data = save_current_data;
1095 b0->current_length = save_current_length;
1100 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
1102 vnet_interface_main_t * im = &vnet_get_main()->interface_main;
1104 if (im->pcap_drop_filter_hash == 0)
1105 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
1108 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1110 hash_unset (im->pcap_drop_filter_hash, error_index);
1114 process_drop (vlib_main_t * vm,
1115 vlib_node_runtime_t * node,
1116 vlib_frame_t * frame)
1118 vnet_interface_main_t * im = &vnet_get_main()->interface_main;
1120 if (PREDICT_FALSE (im->drop_pcap_enable))
1121 pcap_drop_trace (vm, im, frame);
1123 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
1127 process_punt (vlib_main_t * vm,
1128 vlib_node_runtime_t * node,
1129 vlib_frame_t * frame)
1131 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
1134 VLIB_REGISTER_NODE (drop_buffers,static) = {
1135 .function = process_drop,
1136 .name = "error-drop",
1137 .flags = VLIB_NODE_FLAG_IS_DROP,
1138 .vector_size = sizeof (u32),
1139 .format_trace = format_vnet_error_trace,
1140 .validate_frame = validate_error_frame,
1143 VLIB_REGISTER_NODE (punt_buffers,static) = {
1144 .function = process_punt,
1145 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
1146 | VLIB_NODE_FLAG_IS_PUNT),
1147 .name = "error-punt",
1148 .vector_size = sizeof (u32),
1149 .format_trace = format_vnet_error_trace,
1150 .validate_frame = validate_error_frame,
1153 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node,static) = {
1154 .function = vnet_per_buffer_interface_output,
1155 .name = "interface-output",
1156 .vector_size = sizeof (u32),
1160 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1164 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
1167 next_index = vlib_node_add_next_with_slot
1168 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1169 hi->output_node_index,
1170 /* next_index */ hw_if_index);
1172 ASSERT (next_index == hw_if_index);
1177 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1178 (vnet_per_buffer_interface_output_hw_interface_add_del);
1180 static clib_error_t *
1181 pcap_drop_trace_command_fn (vlib_main_t * vm,
1182 unformat_input_t * input,
1183 vlib_cli_command_t * cmd)
1185 vnet_main_t * vnm = vnet_get_main();
1186 vnet_interface_main_t * im = &vnm->interface_main;
1190 clib_error_t * error = 0;
1192 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1194 if (unformat (input, "on"))
1196 if (im->drop_pcap_enable == 0)
1198 if (im->pcap_filename == 0)
1199 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
1201 memset (&im->pcap_main, 0, sizeof (im->pcap_main));
1202 im->pcap_main.file_name = (char *) im->pcap_filename;
1203 im->pcap_main.n_packets_to_capture = 100;
1204 if (im->pcap_pkts_to_capture)
1205 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1207 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1208 im->drop_pcap_enable = 1;
1210 vlib_cli_output (vm, "pcap drop capture on...");
1214 vlib_cli_output (vm, "pcap drop capture already on...");
1218 else if (unformat (input, "off"))
1222 if (im->drop_pcap_enable)
1224 vlib_cli_output (vm, "captured %d pkts...",
1225 im->pcap_main.n_packets_captured);
1226 if (im->pcap_main.n_packets_captured)
1228 im->pcap_main.n_packets_to_capture =
1229 im->pcap_main.n_packets_captured;
1230 error = pcap_write (&im->pcap_main);
1232 clib_error_report (error);
1234 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1239 vlib_cli_output (vm, "pcap drop capture already off...");
1242 im->drop_pcap_enable = 0;
1244 else if (unformat (input, "max %d", &max))
1246 im->pcap_pkts_to_capture = max;
1250 else if (unformat (input, "intfc %U",
1251 unformat_vnet_sw_interface, vnm,
1252 &im->pcap_sw_if_index))
1254 else if (unformat (input, "intfc any"))
1256 im->pcap_sw_if_index = 0;
1259 else if (unformat (input, "file %s", &filename))
1261 u8 * chroot_filename;
1262 /* Brain-police user path input */
1263 if (strstr((char *)filename, "..") || index((char *)filename, '/'))
1265 vlib_cli_output (vm, "illegal characters in filename '%s'",
1270 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1271 vec_free (filename);
1273 if (im->pcap_filename)
1274 vec_free (im->pcap_filename);
1275 vec_add1 (filename, 0);
1276 im->pcap_filename = chroot_filename;
1279 else if (unformat (input, "status"))
1281 if (im->drop_pcap_enable == 0)
1283 vlib_cli_output (vm, "pcap drop capture is off...");
1287 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1288 im->pcap_main.n_packets_captured,
1289 im->pcap_main.n_packets_to_capture);
1298 return clib_error_return (0, "unknown input `%U'",
1299 format_unformat_error, input);
1304 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
1305 .path = "pcap drop trace",
1307 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1308 .function = pcap_drop_trace_command_fn,