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/ip/icmp46_packet.h>
42 #include <vnet/ip/ip4.h>
43 #include <vnet/ip/ip6.h>
44 #include <vnet/udp/udp_packet.h>
45 #include <vnet/feature/feature.h>
50 u8 data[128 - sizeof (u32)];
52 interface_output_trace_t;
55 format_vnet_interface_output_trace (u8 * s, va_list * va)
57 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
58 vlib_node_t *node = va_arg (*va, vlib_node_t *);
59 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
60 vnet_main_t *vnm = vnet_get_main ();
61 vnet_sw_interface_t *si;
64 if (t->sw_if_index != (u32) ~ 0)
66 indent = format_get_indent (s);
68 if (pool_is_free_index
69 (vnm->interface_main.sw_interfaces, t->sw_if_index))
71 /* the interface may have been deleted by the time the trace is printed */
72 s = format (s, "sw_if_index: %d\n%U%U",
74 format_white_space, indent,
75 node->format_buffer ? node->
76 format_buffer : format_hex_bytes, t->data,
81 si = vnet_get_sw_interface (vnm, t->sw_if_index);
83 s = format (s, "%U\n%U%U",
84 format_vnet_sw_interface_name, vnm, si,
85 format_white_space, indent,
86 node->format_buffer ? node->
87 format_buffer : format_hex_bytes, t->data,
95 vnet_interface_output_trace (vlib_main_t * vm,
96 vlib_node_runtime_t * node,
97 vlib_frame_t * frame, uword n_buffers)
102 from = vlib_frame_args (frame);
107 vlib_buffer_t *b0, *b1;
108 interface_output_trace_t *t0, *t1;
110 /* Prefetch next iteration. */
111 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
112 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
117 b0 = vlib_get_buffer (vm, bi0);
118 b1 = vlib_get_buffer (vm, bi1);
120 if (b0->flags & VLIB_BUFFER_IS_TRACED)
122 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
123 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
124 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
127 if (b1->flags & VLIB_BUFFER_IS_TRACED)
129 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
130 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
131 clib_memcpy (t1->data, vlib_buffer_get_current (b1),
142 interface_output_trace_t *t0;
146 b0 = vlib_get_buffer (vm, bi0);
148 if (b0->flags & VLIB_BUFFER_IS_TRACED)
150 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
151 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
152 clib_memcpy (t0->data, vlib_buffer_get_current (b0),
160 static_always_inline void
161 calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
168 int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
169 int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
171 ASSERT (!(is_ip4 && is_ip6));
173 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
174 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
175 th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
176 uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
180 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
181 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
182 ip4->checksum = ip4_header_checksum (ip4);
183 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
184 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
185 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
186 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
191 ASSERT (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM);
192 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
193 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
194 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
195 uh->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
198 b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
199 b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
200 b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
203 static_always_inline uword
204 vnet_interface_output_node_inline (vlib_main_t * vm,
205 vlib_node_runtime_t * node,
206 vlib_frame_t * frame, vnet_main_t * vnm,
207 vnet_hw_interface_t * hi,
210 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
211 vnet_sw_interface_t *si;
212 u32 n_left_to_tx, *from, *from_end, *to_tx;
213 u32 n_bytes, n_buffers, n_packets;
214 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
215 u32 thread_index = vm->thread_index;
216 vnet_interface_main_t *im = &vnm->interface_main;
217 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
218 u32 current_config_index = ~0;
219 u8 arc = im->output_feature_arc_index;
221 n_buffers = frame->n_vectors;
223 if (node->flags & VLIB_NODE_FLAG_TRACE)
224 vnet_interface_output_trace (vm, node, frame, n_buffers);
226 from = vlib_frame_args (frame);
229 return vlib_error_drop_buffers (vm, node, from,
230 /* buffer stride */ 1,
232 VNET_INTERFACE_OUTPUT_NEXT_DROP,
234 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
236 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
237 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
238 if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
239 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
240 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
242 vlib_simple_counter_main_t *cm;
244 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
245 VNET_INTERFACE_COUNTER_TX_ERROR);
246 vlib_increment_simple_counter (cm, thread_index,
247 rt->sw_if_index, n_buffers);
249 return vlib_error_drop_buffers (vm, node, from,
250 /* buffer stride */ 1,
252 VNET_INTERFACE_OUTPUT_NEXT_DROP,
254 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
257 from_end = from + n_buffers;
259 /* Total byte count of all buffers. */
263 /* interface-output feature arc handling */
264 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
266 vnet_feature_config_main_t *fcm;
267 fcm = vnet_feature_get_config_main (arc);
268 current_config_index = vnet_get_feature_config_index (arc,
270 vnet_get_config_data (&fcm->config_main, ¤t_config_index,
274 while (from < from_end)
276 /* Get new next frame since previous incomplete frame may have less
277 than VNET_FRAME_SIZE vectors in it. */
278 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
280 while (from + 8 <= from_end && n_left_to_tx >= 4)
282 u32 bi0, bi1, bi2, bi3;
283 vlib_buffer_t *b0, *b1, *b2, *b3;
284 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
287 /* Prefetch next iteration. */
288 vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
289 vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
290 vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
291 vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
305 b0 = vlib_get_buffer (vm, bi0);
306 b1 = vlib_get_buffer (vm, bi1);
307 b2 = vlib_get_buffer (vm, bi2);
308 b3 = vlib_get_buffer (vm, bi3);
310 /* Be grumpy about zero length buffers for benefit of
311 driver tx function. */
312 ASSERT (b0->current_length > 0);
313 ASSERT (b1->current_length > 0);
314 ASSERT (b2->current_length > 0);
315 ASSERT (b3->current_length > 0);
317 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
318 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
319 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
320 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
321 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
322 tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
323 tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
324 tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
326 n_bytes += n_bytes_b0 + n_bytes_b1;
327 n_bytes += n_bytes_b2 + n_bytes_b3;
330 if (PREDICT_FALSE (current_config_index != ~0))
332 b0->feature_arc_index = arc;
333 b1->feature_arc_index = arc;
334 b2->feature_arc_index = arc;
335 b3->feature_arc_index = arc;
336 b0->current_config_index = current_config_index;
337 b1->current_config_index = current_config_index;
338 b2->current_config_index = current_config_index;
339 b3->current_config_index = current_config_index;
342 /* update vlan subif tx counts, if required */
343 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
345 vlib_increment_combined_counter (im->combined_sw_if_counters +
346 VNET_INTERFACE_COUNTER_TX,
347 thread_index, tx_swif0, 1,
351 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
354 vlib_increment_combined_counter (im->combined_sw_if_counters +
355 VNET_INTERFACE_COUNTER_TX,
356 thread_index, tx_swif1, 1,
360 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
363 vlib_increment_combined_counter (im->combined_sw_if_counters +
364 VNET_INTERFACE_COUNTER_TX,
365 thread_index, tx_swif2, 1,
368 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
371 vlib_increment_combined_counter (im->combined_sw_if_counters +
372 VNET_INTERFACE_COUNTER_TX,
373 thread_index, tx_swif3, 1,
377 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
382 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
383 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
384 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
386 calc_checksums (vm, b0);
387 calc_checksums (vm, b1);
388 calc_checksums (vm, b2);
389 calc_checksums (vm, b3);
394 while (from + 1 <= from_end && n_left_to_tx >= 1)
406 b0 = vlib_get_buffer (vm, bi0);
408 /* Be grumpy about zero length buffers for benefit of
409 driver tx function. */
410 ASSERT (b0->current_length > 0);
412 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
413 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
414 n_bytes += n_bytes_b0;
417 if (PREDICT_FALSE (current_config_index != ~0))
419 b0->feature_arc_index = arc;
420 b0->current_config_index = current_config_index;
423 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
426 vlib_increment_combined_counter (im->combined_sw_if_counters +
427 VNET_INTERFACE_COUNTER_TX,
428 thread_index, tx_swif0, 1,
433 calc_checksums (vm, b0);
436 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
439 /* Update main interface stats. */
440 vlib_increment_combined_counter (im->combined_sw_if_counters
441 + VNET_INTERFACE_COUNTER_TX,
443 rt->sw_if_index, n_packets, n_bytes);
447 static_always_inline uword
448 vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
449 vlib_frame_t * frame)
451 vnet_main_t *vnm = vnet_get_main ();
452 vnet_hw_interface_t *hi;
453 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
454 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
456 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
457 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
458 /* do_tx_offloads */ 0);
460 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
461 /* do_tx_offloads */ 1);
464 VLIB_NODE_FUNCTION_MULTIARCH_CLONE (vnet_interface_output_node);
465 CLIB_MULTIARCH_SELECT_FN (vnet_interface_output_node);
467 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
469 vnet_per_buffer_interface_output (vlib_main_t * vm,
470 vlib_node_runtime_t * node,
471 vlib_frame_t * frame)
473 vnet_main_t *vnm = vnet_get_main ();
474 u32 n_left_to_next, *from, *to_next;
475 u32 n_left_from, next_index;
477 n_left_from = frame->n_vectors;
479 from = vlib_frame_args (frame);
480 next_index = node->cached_next_index;
482 while (n_left_from > 0)
484 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
486 while (n_left_from >= 4 && n_left_to_next >= 2)
488 u32 bi0, bi1, next0, next1;
489 vlib_buffer_t *b0, *b1;
490 vnet_hw_interface_t *hi0, *hi1;
492 /* Prefetch next iteration. */
493 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
494 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
505 b0 = vlib_get_buffer (vm, bi0);
506 b1 = vlib_get_buffer (vm, bi1);
509 vnet_get_sup_hw_interface (vnm,
510 vnet_buffer (b0)->sw_if_index
513 vnet_get_sup_hw_interface (vnm,
514 vnet_buffer (b1)->sw_if_index
517 next0 = hi0->hw_if_index;
518 next1 = hi1->hw_if_index;
520 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
521 n_left_to_next, bi0, bi1, next0,
525 while (n_left_from > 0 && n_left_to_next > 0)
529 vnet_hw_interface_t *hi0;
538 b0 = vlib_get_buffer (vm, bi0);
541 vnet_get_sup_hw_interface (vnm,
542 vnet_buffer (b0)->sw_if_index
545 next0 = hi0->hw_if_index;
547 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
548 n_left_to_next, bi0, next0);
551 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
554 return frame->n_vectors;
558 counter_index (vlib_main_t * vm, vlib_error_t e)
563 ni = vlib_error_get_node (e);
564 n = vlib_get_node (vm, ni);
566 ci = vlib_error_get_code (e);
567 ASSERT (ci < n->n_errors);
569 ci += n->error_heap_index;
575 format_vnet_error_trace (u8 * s, va_list * va)
577 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
578 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
579 vlib_error_t *e = va_arg (*va, vlib_error_t *);
580 vlib_node_t *error_node;
581 vlib_error_main_t *em = &vm->error_main;
584 error_node = vlib_get_node (vm, vlib_error_get_node (e[0]));
585 i = counter_index (vm, e[0]);
586 s = format (s, "%v: %s", error_node->name, em->error_strings_heap[i]);
592 trace_errors_with_buffers (vlib_main_t * vm,
593 vlib_node_runtime_t * node, vlib_frame_t * frame)
595 u32 n_left, *buffers;
597 buffers = vlib_frame_vector_args (frame);
598 n_left = frame->n_vectors;
603 vlib_buffer_t *b0, *b1;
604 vlib_error_t *t0, *t1;
606 /* Prefetch next iteration. */
607 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
608 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
613 b0 = vlib_get_buffer (vm, bi0);
614 b1 = vlib_get_buffer (vm, bi1);
616 if (b0->flags & VLIB_BUFFER_IS_TRACED)
618 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
621 if (b1->flags & VLIB_BUFFER_IS_TRACED)
623 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
638 b0 = vlib_get_buffer (vm, bi0);
640 if (b0->flags & VLIB_BUFFER_IS_TRACED)
642 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
651 validate_error (vlib_main_t * vm, vlib_error_t * e, u32 index)
653 uword node_index = vlib_error_get_node (e[0]);
654 uword code = vlib_error_get_code (e[0]);
657 if (node_index >= vec_len (vm->node_main.nodes))
658 return format (0, "[%d], node index out of range 0x%x, error 0x%x",
659 index, node_index, e[0]);
661 n = vlib_get_node (vm, node_index);
662 if (code >= n->n_errors)
663 return format (0, "[%d], code %d out of range for node %v",
664 index, code, n->name);
670 validate_error_frame (vlib_main_t * vm,
671 vlib_node_runtime_t * node, vlib_frame_t * f)
673 u32 *buffers = vlib_frame_args (f);
678 for (i = 0; i < f->n_vectors; i++)
680 b = vlib_get_buffer (vm, buffers[i]);
681 msg = validate_error (vm, &b->error, i);
691 VNET_ERROR_DISPOSITION_DROP,
692 VNET_ERROR_DISPOSITION_PUNT,
693 VNET_ERROR_N_DISPOSITION,
694 } vnet_error_disposition_t;
697 do_packet (vlib_main_t * vm, vlib_error_t a)
699 vlib_error_main_t *em = &vm->error_main;
700 u32 i = counter_index (vm, a);
701 em->counters[i] += 1;
702 vlib_error_elog_count (vm, i, 1);
705 static_always_inline uword
706 process_drop_punt (vlib_main_t * vm,
707 vlib_node_runtime_t * node,
708 vlib_frame_t * frame, vnet_error_disposition_t disposition)
710 vnet_main_t *vnm = vnet_get_main ();
711 vlib_error_main_t *em = &vm->error_main;
712 u32 *buffers, *first_buffer;
713 vlib_error_t current_error;
714 u32 current_counter_index, n_errors_left;
715 u32 current_sw_if_index, n_errors_current_sw_if_index;
717 vlib_simple_counter_main_t *cm;
718 u32 thread_index = vm->thread_index;
720 static vlib_error_t memory[VNET_ERROR_N_DISPOSITION];
721 static char memory_init[VNET_ERROR_N_DISPOSITION];
723 buffers = vlib_frame_args (frame);
724 first_buffer = buffers;
727 vlib_buffer_t *b = vlib_get_buffer (vm, first_buffer[0]);
729 if (!memory_init[disposition])
731 memory_init[disposition] = 1;
732 memory[disposition] = b->error;
735 current_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
736 n_errors_current_sw_if_index = 0;
739 current_error = memory[disposition];
740 current_counter_index = counter_index (vm, memory[disposition]);
741 current_counter = em->counters[current_counter_index];
743 if (node->flags & VLIB_NODE_FLAG_TRACE)
744 trace_errors_with_buffers (vm, node, frame);
746 n_errors_left = frame->n_vectors;
747 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
748 (disposition == VNET_ERROR_DISPOSITION_PUNT
749 ? VNET_INTERFACE_COUNTER_PUNT
750 : VNET_INTERFACE_COUNTER_DROP));
752 while (n_errors_left >= 2)
754 vlib_buffer_t *b0, *b1;
755 vnet_sw_interface_t *sw_if0, *sw_if1;
758 u32 sw_if_index0, sw_if_index1;
766 b0 = vlib_get_buffer (vm, bi0);
767 b1 = vlib_get_buffer (vm, bi1);
772 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
773 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
775 /* Speculate that sw_if_index == sw_if_index[01]. */
776 n_errors_current_sw_if_index += 2;
778 /* Speculatively assume all 2 (node, code) pairs are equal
779 to current (node, code). */
780 current_counter += 2;
782 if (PREDICT_FALSE (e0 != current_error
783 || e1 != current_error
784 || sw_if_index0 != current_sw_if_index
785 || sw_if_index1 != current_sw_if_index))
787 current_counter -= 2;
788 n_errors_current_sw_if_index -= 2;
790 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
791 vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
793 /* Increment super-interface drop/punt counters for
795 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
796 vlib_increment_simple_counter
797 (cm, thread_index, sw_if0->sup_sw_if_index,
798 sw_if0->sup_sw_if_index != sw_if_index0);
800 sw_if1 = vnet_get_sw_interface (vnm, sw_if_index1);
801 vlib_increment_simple_counter
802 (cm, thread_index, sw_if1->sup_sw_if_index,
803 sw_if1->sup_sw_if_index != sw_if_index1);
805 em->counters[current_counter_index] = current_counter;
809 /* For 2 repeated errors, change current error. */
810 if (e0 == e1 && e1 != current_error)
813 current_counter_index = counter_index (vm, e0);
815 current_counter = em->counters[current_counter_index];
819 while (n_errors_left >= 1)
822 vnet_sw_interface_t *sw_if0;
824 u32 bi0, sw_if_index0;
830 current_counter += 1;
832 b0 = vlib_get_buffer (vm, bi0);
835 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
837 /* Increment drop/punt counters. */
838 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
840 /* Increment super-interface drop/punt counters for sub-interfaces. */
841 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index0);
842 vlib_increment_simple_counter (cm, thread_index,
843 sw_if0->sup_sw_if_index,
844 sw_if0->sup_sw_if_index != sw_if_index0);
846 if (PREDICT_FALSE (e0 != current_error))
848 current_counter -= 1;
850 vlib_error_elog_count (vm, current_counter_index,
852 - em->counters[current_counter_index]));
854 em->counters[current_counter_index] = current_counter;
858 current_counter_index = counter_index (vm, e0);
859 current_counter = em->counters[current_counter_index];
863 if (n_errors_current_sw_if_index > 0)
865 vnet_sw_interface_t *si;
867 vlib_increment_simple_counter (cm, thread_index, current_sw_if_index,
868 n_errors_current_sw_if_index);
870 si = vnet_get_sw_interface (vnm, current_sw_if_index);
871 if (si->sup_sw_if_index != current_sw_if_index)
872 vlib_increment_simple_counter (cm, thread_index, si->sup_sw_if_index,
873 n_errors_current_sw_if_index);
876 vlib_error_elog_count (vm, current_counter_index,
878 - em->counters[current_counter_index]));
880 /* Return cached counter. */
881 em->counters[current_counter_index] = current_counter;
883 /* Save memory for next iteration. */
884 memory[disposition] = current_error;
886 if (disposition == VNET_ERROR_DISPOSITION_DROP || !vm->os_punt_frame)
888 vlib_buffer_free (vm, first_buffer, frame->n_vectors);
890 /* If there is no punt function, free the frame as well. */
891 if (disposition == VNET_ERROR_DISPOSITION_PUNT && !vm->os_punt_frame)
892 vlib_frame_free (vm, node, frame);
895 vm->os_punt_frame (vm, node, frame);
897 return frame->n_vectors;
901 pcap_drop_trace (vlib_main_t * vm,
902 vnet_interface_main_t * im, vlib_frame_t * f)
905 u32 n_left = f->n_vectors;
906 vlib_buffer_t *b0, *p1;
908 i16 save_current_data;
909 u16 save_current_length;
911 from = vlib_frame_vector_args (f);
915 if (PREDICT_TRUE (n_left > 1))
917 p1 = vlib_get_buffer (vm, from[1]);
918 vlib_prefetch_buffer_header (p1, LOAD);
922 b0 = vlib_get_buffer (vm, bi0);
926 /* See if we're pointedly ignoring this specific error */
927 if (im->pcap_drop_filter_hash
928 && hash_get (im->pcap_drop_filter_hash, b0->error))
931 /* Trace all drops, or drops received on a specific interface */
932 if (im->pcap_sw_if_index == 0 ||
933 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
935 save_current_data = b0->current_data;
936 save_current_length = b0->current_length;
939 * Typically, we'll need to rewind the buffer
941 if (b0->current_data > 0)
942 vlib_buffer_advance (b0, (word) - b0->current_data);
944 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
946 b0->current_data = save_current_data;
947 b0->current_length = save_current_length;
953 vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
955 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
957 if (im->pcap_drop_filter_hash == 0)
958 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
961 hash_set (im->pcap_drop_filter_hash, error_index, 1);
963 hash_unset (im->pcap_drop_filter_hash, error_index);
967 process_drop (vlib_main_t * vm,
968 vlib_node_runtime_t * node, vlib_frame_t * frame)
970 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
972 if (PREDICT_FALSE (im->drop_pcap_enable))
973 pcap_drop_trace (vm, im, frame);
975 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
979 process_punt (vlib_main_t * vm,
980 vlib_node_runtime_t * node, vlib_frame_t * frame)
982 return process_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
986 VLIB_REGISTER_NODE (drop_buffers,static) = {
987 .function = process_drop,
988 .name = "error-drop",
989 .flags = VLIB_NODE_FLAG_IS_DROP,
990 .vector_size = sizeof (u32),
991 .format_trace = format_vnet_error_trace,
992 .validate_frame = validate_error_frame,
996 VLIB_NODE_FUNCTION_MULTIARCH (drop_buffers, process_drop);
999 VLIB_REGISTER_NODE (punt_buffers,static) = {
1000 .function = process_punt,
1001 .flags = (VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH
1002 | VLIB_NODE_FLAG_IS_PUNT),
1003 .name = "error-punt",
1004 .vector_size = sizeof (u32),
1005 .format_trace = format_vnet_error_trace,
1006 .validate_frame = validate_error_frame,
1010 VLIB_NODE_FUNCTION_MULTIARCH (punt_buffers, process_punt);
1013 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node,static) = {
1014 .function = vnet_per_buffer_interface_output,
1015 .name = "interface-output",
1016 .vector_size = sizeof (u32),
1020 VLIB_NODE_FUNCTION_MULTIARCH (vnet_per_buffer_interface_output_node,
1021 vnet_per_buffer_interface_output);
1024 interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1025 vlib_frame_t * from_frame)
1027 vnet_main_t *vnm = vnet_get_main ();
1028 u32 last_sw_if_index = ~0;
1029 vlib_frame_t *to_frame = 0;
1030 vnet_hw_interface_t *hw = 0;
1031 u32 *from, *to_next = 0;
1034 from = vlib_frame_vector_args (from_frame);
1035 n_left_from = from_frame->n_vectors;
1036 while (n_left_from > 0)
1045 b0 = vlib_get_buffer (vm, bi0);
1046 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1048 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1052 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1053 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1055 last_sw_if_index = sw_if_index0;
1056 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1057 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1058 to_next = vlib_frame_vector_args (to_frame);
1063 to_frame->n_vectors++;
1065 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1066 return from_frame->n_vectors;
1070 VLIB_REGISTER_NODE (interface_tx, static) = {
1071 .function = interface_tx_node_fn,
1072 .name = "interface-tx",
1073 .vector_size = sizeof (u32),
1080 VNET_FEATURE_ARC_INIT (interface_output, static) =
1082 .arc_name = "interface-output",
1083 .start_nodes = VNET_FEATURES (0),
1084 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1087 VNET_FEATURE_INIT (span_tx, static) = {
1088 .arc_name = "interface-output",
1089 .node_name = "span-output",
1090 .runs_before = VNET_FEATURES ("interface-tx"),
1093 VNET_FEATURE_INIT (interface_tx, static) = {
1094 .arc_name = "interface-output",
1095 .node_name = "interface-tx",
1101 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1105 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1108 next_index = vlib_node_add_next_with_slot
1109 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1110 hi->output_node_index,
1111 /* next_index */ hw_if_index);
1113 ASSERT (next_index == hw_if_index);
1118 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1119 (vnet_per_buffer_interface_output_hw_interface_add_del);
1121 static clib_error_t *
1122 pcap_drop_trace_command_fn (vlib_main_t * vm,
1123 unformat_input_t * input,
1124 vlib_cli_command_t * cmd)
1126 vnet_main_t *vnm = vnet_get_main ();
1127 vnet_interface_main_t *im = &vnm->interface_main;
1131 clib_error_t *error = 0;
1133 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1135 if (unformat (input, "on"))
1137 if (im->drop_pcap_enable == 0)
1139 if (im->pcap_filename == 0)
1140 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
1142 memset (&im->pcap_main, 0, sizeof (im->pcap_main));
1143 im->pcap_main.file_name = (char *) im->pcap_filename;
1144 im->pcap_main.n_packets_to_capture = 100;
1145 if (im->pcap_pkts_to_capture)
1146 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1148 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1149 im->drop_pcap_enable = 1;
1151 vlib_cli_output (vm, "pcap drop capture on...");
1155 vlib_cli_output (vm, "pcap drop capture already on...");
1159 else if (unformat (input, "off"))
1163 if (im->drop_pcap_enable)
1165 vlib_cli_output (vm, "captured %d pkts...",
1166 im->pcap_main.n_packets_captured);
1167 if (im->pcap_main.n_packets_captured)
1169 im->pcap_main.n_packets_to_capture =
1170 im->pcap_main.n_packets_captured;
1171 error = pcap_write (&im->pcap_main);
1173 clib_error_report (error);
1175 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1180 vlib_cli_output (vm, "pcap drop capture already off...");
1183 im->drop_pcap_enable = 0;
1185 else if (unformat (input, "max %d", &max))
1187 im->pcap_pkts_to_capture = max;
1191 else if (unformat (input, "intfc %U",
1192 unformat_vnet_sw_interface, vnm,
1193 &im->pcap_sw_if_index))
1195 else if (unformat (input, "intfc any"))
1197 im->pcap_sw_if_index = 0;
1200 else if (unformat (input, "file %s", &filename))
1202 u8 *chroot_filename;
1203 /* Brain-police user path input */
1204 if (strstr ((char *) filename, "..")
1205 || index ((char *) filename, '/'))
1207 vlib_cli_output (vm, "illegal characters in filename '%s'",
1212 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1213 vec_free (filename);
1215 if (im->pcap_filename)
1216 vec_free (im->pcap_filename);
1217 im->pcap_filename = chroot_filename;
1218 im->pcap_main.file_name = (char *) im->pcap_filename;
1221 else if (unformat (input, "status"))
1223 if (im->drop_pcap_enable == 0)
1225 vlib_cli_output (vm, "pcap drop capture is off...");
1229 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1230 im->pcap_main.n_packets_captured,
1231 im->pcap_main.n_packets_to_capture);
1240 return clib_error_return (0, "unknown input `%U'",
1241 format_unformat_error, input);
1247 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
1248 .path = "pcap drop trace",
1250 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1251 .function = pcap_drop_trace_command_fn,
1256 * fd.io coding-style-patch-verification: ON
1259 * eval: (c-set-style "gnu")