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>
53 u8 data[128 - 3 * sizeof (u32)];
55 interface_output_trace_t;
57 #ifndef CLIB_MARCH_VARIANT
59 format_vnet_interface_output_trace (u8 * s, va_list * va)
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
62 vlib_node_t *node = va_arg (*va, vlib_node_t *);
63 interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
64 vnet_main_t *vnm = vnet_get_main ();
65 vnet_sw_interface_t *si;
68 if (t->sw_if_index != (u32) ~ 0)
70 indent = format_get_indent (s);
72 if (pool_is_free_index
73 (vnm->interface_main.sw_interfaces, t->sw_if_index))
75 /* the interface may have been deleted by the time the trace is printed */
76 s = format (s, "sw_if_index: %d ", t->sw_if_index);
80 si = vnet_get_sw_interface (vnm, t->sw_if_index);
82 format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
85 #define _(bit, name, v, x) \
86 if (v && (t->flags & VNET_BUFFER_F_##name)) \
87 s = format (s, "%s ", v);
88 foreach_vnet_buffer_flag
90 if (t->flags & VNET_BUFFER_F_GSO)
92 s = format (s, "\n%Ugso_sz %d gso_l4_hdr_sz %d",
93 format_white_space, indent + 2, t->gso_size,
97 format (s, "\n%U%U", format_white_space, indent,
98 node->format_buffer ? node->format_buffer : format_hex_bytes,
99 t->data, sizeof (t->data));
105 vnet_interface_output_trace (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
107 vlib_frame_t * frame, uword n_buffers)
112 from = vlib_frame_vector_args (frame);
117 vlib_buffer_t *b0, *b1;
118 interface_output_trace_t *t0, *t1;
120 /* Prefetch next iteration. */
121 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
122 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
127 b0 = vlib_get_buffer (vm, bi0);
128 b1 = vlib_get_buffer (vm, bi1);
130 if (b0->flags & VLIB_BUFFER_IS_TRACED)
132 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
133 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
134 t0->flags = b0->flags;
135 t0->gso_size = vnet_buffer2 (b0)->gso_size;
136 t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
137 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
140 if (b1->flags & VLIB_BUFFER_IS_TRACED)
142 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
143 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
144 t1->flags = b1->flags;
145 t1->gso_size = vnet_buffer2 (b1)->gso_size;
146 t1->gso_l4_hdr_sz = vnet_buffer2 (b1)->gso_l4_hdr_sz;
147 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
158 interface_output_trace_t *t0;
162 b0 = vlib_get_buffer (vm, bi0);
164 if (b0->flags & VLIB_BUFFER_IS_TRACED)
166 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
167 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
168 t0->flags = b0->flags;
169 t0->gso_size = vnet_buffer2 (b0)->gso_size;
170 t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
171 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
179 static_always_inline void
180 calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
187 int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
188 int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
190 ASSERT (!(is_ip4 && is_ip6));
192 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
193 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
194 th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
195 uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
199 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
200 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
201 ip4->checksum = ip4_header_checksum (ip4);
202 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
205 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
207 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
208 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
213 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
217 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
219 if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
223 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
227 b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
228 b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
229 b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
232 static_always_inline u16
233 tso_alloc_tx_bufs (vlib_main_t * vm,
234 vnet_interface_per_thread_data_t * ptd,
235 vlib_buffer_t * b0, u16 l4_hdr_sz)
237 u32 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
238 u16 gso_size = vnet_buffer2 (b0)->gso_size;
239 u16 l234_sz = vnet_buffer (b0)->l4_hdr_offset + l4_hdr_sz;
240 /* rounded-up division */
241 u16 n_bufs = (n_bytes_b0 - l234_sz + (gso_size - 1)) / gso_size;
245 vec_validate (ptd->split_buffers, n_bufs - 1);
247 n_alloc = vlib_buffer_alloc (vm, ptd->split_buffers, n_bufs);
248 if (n_alloc < n_bufs)
250 vlib_buffer_free (vm, ptd->split_buffers, n_alloc);
256 static_always_inline void
257 tso_init_buf_from_template_base (vlib_buffer_t * nb0, vlib_buffer_t * b0,
258 u32 flags, u16 length)
260 nb0->current_data = 0;
261 nb0->total_length_not_including_first_buffer = 0;
262 nb0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | flags;
263 clib_memcpy_fast (&nb0->opaque, &b0->opaque, sizeof (nb0->opaque));
264 clib_memcpy_fast (nb0->data, b0->data, length);
265 nb0->current_length = length;
268 static_always_inline void
269 tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
270 vlib_buffer_t * b0, u16 template_data_sz,
271 u16 gso_size, u8 ** p_dst_ptr, u16 * p_dst_left,
272 u32 next_tcp_seq, u32 flags)
274 tso_init_buf_from_template_base (nb0, b0, flags, template_data_sz);
278 vlib_buffer_get_default_data_size (vm) - template_data_sz);
279 *p_dst_ptr = nb0->data + template_data_sz;
282 (tcp_header_t *) (nb0->data + vnet_buffer (nb0)->l4_hdr_offset);
283 tcp->seq_number = clib_host_to_net_u32 (next_tcp_seq);
286 static_always_inline void
287 tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6)
289 u16 l3_hdr_offset = vnet_buffer (b0)->l3_hdr_offset;
290 u16 l4_hdr_offset = vnet_buffer (b0)->l4_hdr_offset;
291 ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l3_hdr_offset);
292 ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l3_hdr_offset);
293 tcp_header_t *tcp = (tcp_header_t *) (b0->data + l4_hdr_offset);
295 tcp->flags = tcp_flags;
298 ip6->payload_length =
299 clib_host_to_net_u16 (b0->current_length -
300 vnet_buffer (b0)->l4_hdr_offset);
303 clib_host_to_net_u16 (b0->current_length -
304 vnet_buffer (b0)->l3_hdr_offset);
308 * Allocate the necessary number of ptd->split_buffers,
309 * and segment the possibly chained buffer(s) from b0 into
312 * Return the cumulative number of bytes sent or zero
313 * if allocation failed.
316 static_always_inline u32
317 tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
318 int do_tx_offloads, u32 sbi0, vlib_buffer_t * sb0,
322 int is_ip4 = sb0->flags & VNET_BUFFER_F_IS_IP4;
323 int is_ip6 = sb0->flags & VNET_BUFFER_F_IS_IP6;
324 ASSERT (is_ip4 || is_ip6);
325 ASSERT (sb0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID);
326 ASSERT (sb0->flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID);
327 ASSERT (sb0->flags & VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
328 u16 gso_size = vnet_buffer2 (sb0)->gso_size;
330 int l4_hdr_sz = vnet_buffer2 (sb0)->gso_l4_hdr_sz;
331 u8 save_tcp_flags = 0;
332 u8 tcp_flags_no_fin_psh = 0;
333 u32 next_tcp_seq = 0;
336 (tcp_header_t *) (sb0->data + vnet_buffer (sb0)->l4_hdr_offset);
337 next_tcp_seq = clib_net_to_host_u32 (tcp->seq_number);
338 /* store original flags for last packet and reset FIN and PSH */
339 save_tcp_flags = tcp->flags;
340 tcp_flags_no_fin_psh = tcp->flags & ~(TCP_FLAG_FIN | TCP_FLAG_PSH);
344 sb0->flags & ~(VNET_BUFFER_F_GSO | VLIB_BUFFER_NEXT_PRESENT);
345 u16 l234_sz = vnet_buffer (sb0)->l4_hdr_offset + l4_hdr_sz;
346 int first_data_size = clib_min (gso_size, sb0->current_length - l234_sz);
347 next_tcp_seq += first_data_size;
349 if (PREDICT_FALSE (!tso_alloc_tx_bufs (vm, ptd, sb0, l4_hdr_sz)))
352 vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[0]);
353 tso_init_buf_from_template_base (b0, sb0, default_bflags,
354 l4_hdr_sz + first_data_size);
356 u32 total_src_left = n_bytes_b0 - l234_sz - first_data_size;
359 /* Need to copy more segments */
360 u8 *src_ptr, *dst_ptr;
361 u16 src_left, dst_left;
362 /* current source buffer */
363 vlib_buffer_t *csb0 = sb0;
365 /* current dest buffer */
367 u16 dbi = 1; /* the buffer [0] is b0 */
369 src_ptr = sb0->data + l234_sz + first_data_size;
370 src_left = sb0->current_length - l234_sz - first_data_size;
371 b0->current_length = l234_sz + first_data_size;
373 tso_fixup_segmented_buf (b0, tcp_flags_no_fin_psh, is_ip6);
375 calc_checksums (vm, b0);
377 /* grab a second buffer and prepare the loop */
378 ASSERT (dbi < vec_len (ptd->split_buffers));
379 cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
380 tso_init_buf_from_template (vm, cdb0, b0, l234_sz, gso_size, &dst_ptr,
381 &dst_left, next_tcp_seq, default_bflags);
383 /* an arbitrary large number to catch the runaway loops */
385 while (total_src_left)
388 clib_panic ("infinite loop detected");
389 u16 bytes_to_copy = clib_min (src_left, dst_left);
391 clib_memcpy_fast (dst_ptr, src_ptr, bytes_to_copy);
393 src_left -= bytes_to_copy;
394 src_ptr += bytes_to_copy;
395 total_src_left -= bytes_to_copy;
396 dst_left -= bytes_to_copy;
397 dst_ptr += bytes_to_copy;
398 next_tcp_seq += bytes_to_copy;
399 cdb0->current_length += bytes_to_copy;
403 int has_next = (csb0->flags & VLIB_BUFFER_NEXT_PRESENT);
404 u32 next_bi = csb0->next_buffer;
406 /* init src to the next buffer in chain */
410 csb0 = vlib_get_buffer (vm, csbi0);
411 src_left = csb0->current_length;
412 src_ptr = csb0->data;
416 ASSERT (total_src_left == 0);
420 if (0 == dst_left && total_src_left)
423 calc_checksums (vm, cdb0);
424 n_tx_bytes += cdb0->current_length;
425 ASSERT (dbi < vec_len (ptd->split_buffers));
426 cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
427 tso_init_buf_from_template (vm, cdb0, b0, l234_sz,
428 gso_size, &dst_ptr, &dst_left,
429 next_tcp_seq, default_bflags);
433 tso_fixup_segmented_buf (cdb0, save_tcp_flags, is_ip6);
435 calc_checksums (vm, cdb0);
437 n_tx_bytes += cdb0->current_length;
439 n_tx_bytes += b0->current_length;
443 static_always_inline void
444 drop_one_buffer_and_count (vlib_main_t * vm, vnet_main_t * vnm,
445 vlib_node_runtime_t * node, u32 * pbi0,
448 u32 thread_index = vm->thread_index;
449 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
451 vlib_simple_counter_main_t *cm;
453 vec_elt_at_index (vnm->interface_main.sw_if_counters,
454 VNET_INTERFACE_COUNTER_TX_ERROR);
455 vlib_increment_simple_counter (cm, thread_index, rt->sw_if_index, 1);
457 vlib_error_drop_buffers (vm, node, pbi0,
458 /* buffer stride */ 1,
460 VNET_INTERFACE_OUTPUT_NEXT_DROP,
461 node->node_index, drop_error_code);
464 static_always_inline uword
465 vnet_interface_output_node_inline_gso (vlib_main_t * vm,
466 vlib_node_runtime_t * node,
467 vlib_frame_t * frame,
469 vnet_hw_interface_t * hi,
473 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
474 vnet_sw_interface_t *si;
475 u32 n_left_to_tx, *from, *from_end, *to_tx;
476 u32 n_bytes, n_buffers, n_packets;
477 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
478 u32 thread_index = vm->thread_index;
479 vnet_interface_main_t *im = &vnm->interface_main;
480 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
481 u32 current_config_index = ~0;
482 u8 arc = im->output_feature_arc_index;
483 vnet_interface_per_thread_data_t *ptd =
484 vec_elt_at_index (im->per_thread_data, thread_index);
486 n_buffers = frame->n_vectors;
488 if (node->flags & VLIB_NODE_FLAG_TRACE)
489 vnet_interface_output_trace (vm, node, frame, n_buffers);
491 from = vlib_frame_vector_args (frame);
494 return vlib_error_drop_buffers (vm, node, from,
495 /* buffer stride */ 1,
497 VNET_INTERFACE_OUTPUT_NEXT_DROP,
499 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
501 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
502 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
503 if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
504 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
505 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
507 vlib_simple_counter_main_t *cm;
509 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
510 VNET_INTERFACE_COUNTER_TX_ERROR);
511 vlib_increment_simple_counter (cm, thread_index,
512 rt->sw_if_index, n_buffers);
514 return vlib_error_drop_buffers (vm, node, from,
515 /* buffer stride */ 1,
517 VNET_INTERFACE_OUTPUT_NEXT_DROP,
519 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
522 from_end = from + n_buffers;
524 /* Total byte count of all buffers. */
528 /* interface-output feature arc handling */
529 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
531 vnet_feature_config_main_t *fcm;
532 fcm = vnet_feature_get_config_main (arc);
533 current_config_index = vnet_get_feature_config_index (arc,
535 vnet_get_config_data (&fcm->config_main, ¤t_config_index,
539 while (from < from_end)
541 /* Get new next frame since previous incomplete frame may have less
542 than VNET_FRAME_SIZE vectors in it. */
543 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
545 while (from + 8 <= from_end && n_left_to_tx >= 4)
547 u32 bi0, bi1, bi2, bi3;
548 vlib_buffer_t *b0, *b1, *b2, *b3;
549 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
552 /* Prefetch next iteration. */
553 vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
554 vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
555 vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
556 vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
566 if (!do_segmentation)
573 b0 = vlib_get_buffer (vm, bi0);
574 b1 = vlib_get_buffer (vm, bi1);
575 b2 = vlib_get_buffer (vm, bi2);
576 b3 = vlib_get_buffer (vm, bi3);
580 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
582 /* go to single loop if we need TSO segmentation */
583 if (PREDICT_FALSE (or_flags & VNET_BUFFER_F_GSO))
590 /* Be grumpy about zero length buffers for benefit of
591 driver tx function. */
592 ASSERT (b0->current_length > 0);
593 ASSERT (b1->current_length > 0);
594 ASSERT (b2->current_length > 0);
595 ASSERT (b3->current_length > 0);
597 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
598 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
599 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
600 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
601 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
602 tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
603 tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
604 tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
606 n_bytes += n_bytes_b0 + n_bytes_b1;
607 n_bytes += n_bytes_b2 + n_bytes_b3;
610 if (PREDICT_FALSE (current_config_index != ~0))
612 vnet_buffer (b0)->feature_arc_index = arc;
613 vnet_buffer (b1)->feature_arc_index = arc;
614 vnet_buffer (b2)->feature_arc_index = arc;
615 vnet_buffer (b3)->feature_arc_index = arc;
616 b0->current_config_index = current_config_index;
617 b1->current_config_index = current_config_index;
618 b2->current_config_index = current_config_index;
619 b3->current_config_index = current_config_index;
622 /* update vlan subif tx counts, if required */
623 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
625 vlib_increment_combined_counter (im->combined_sw_if_counters +
626 VNET_INTERFACE_COUNTER_TX,
627 thread_index, tx_swif0, 1,
631 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
634 vlib_increment_combined_counter (im->combined_sw_if_counters +
635 VNET_INTERFACE_COUNTER_TX,
636 thread_index, tx_swif1, 1,
640 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
643 vlib_increment_combined_counter (im->combined_sw_if_counters +
644 VNET_INTERFACE_COUNTER_TX,
645 thread_index, tx_swif2, 1,
648 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
651 vlib_increment_combined_counter (im->combined_sw_if_counters +
652 VNET_INTERFACE_COUNTER_TX,
653 thread_index, tx_swif3, 1,
657 if (!do_segmentation)
658 or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
663 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
664 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
665 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
667 calc_checksums (vm, b0);
668 calc_checksums (vm, b1);
669 calc_checksums (vm, b2);
670 calc_checksums (vm, b3);
675 while (from + 1 <= from_end && n_left_to_tx >= 1)
687 b0 = vlib_get_buffer (vm, bi0);
689 /* Be grumpy about zero length buffers for benefit of
690 driver tx function. */
691 ASSERT (b0->current_length > 0);
693 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
694 tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
695 n_bytes += n_bytes_b0;
698 if (PREDICT_FALSE (current_config_index != ~0))
700 vnet_buffer (b0)->feature_arc_index = arc;
701 b0->current_config_index = current_config_index;
706 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_GSO))
709 * Undo the enqueue of the b0 - it is not going anywhere,
710 * and will be freed either after it's segmented or
711 * when dropped, if there is no buffers to segment into.
715 /* undo the counting. */
716 n_bytes -= n_bytes_b0;
722 tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b0,
725 if (PREDICT_FALSE (n_tx_bytes == 0))
727 drop_one_buffer_and_count (vm, vnm, node, from - 1,
728 VNET_INTERFACE_OUTPUT_ERROR_NO_BUFFERS_FOR_GSO);
732 u16 n_tx_bufs = vec_len (ptd->split_buffers);
733 u32 *from_tx_seg = ptd->split_buffers;
735 while (n_tx_bufs > 0)
737 if (n_tx_bufs >= n_left_to_tx)
739 while (n_left_to_tx > 0)
741 to_tx[0] = from_tx_seg[0];
748 vlib_put_next_frame (vm, node, next_index,
750 vlib_get_new_next_frame (vm, node, next_index,
751 to_tx, n_left_to_tx);
755 while (n_tx_bufs > 0)
757 to_tx[0] = from_tx_seg[0];
766 n_bytes += n_tx_bytes;
767 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
770 vlib_increment_combined_counter
771 (im->combined_sw_if_counters +
772 VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif0,
773 _vec_len (ptd->split_buffers), n_tx_bytes);
775 /* The buffers were enqueued. Reset the length */
776 _vec_len (ptd->split_buffers) = 0;
777 /* Free the now segmented buffer */
778 vlib_buffer_free_one (vm, bi0);
783 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
786 vlib_increment_combined_counter (im->combined_sw_if_counters +
787 VNET_INTERFACE_COUNTER_TX,
788 thread_index, tx_swif0, 1,
793 calc_checksums (vm, b0);
796 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
799 /* Update main interface stats. */
800 vlib_increment_combined_counter (im->combined_sw_if_counters
801 + VNET_INTERFACE_COUNTER_TX,
803 rt->sw_if_index, n_packets, n_bytes);
806 #endif /* CLIB_MARCH_VARIANT */
808 static_always_inline void vnet_interface_pcap_tx_trace
809 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
810 int sw_if_index_from_buffer)
812 u32 n_left_from, *from;
815 if (PREDICT_TRUE (vm->pcap[VLIB_TX].pcap_enable == 0))
818 if (sw_if_index_from_buffer == 0)
820 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
821 sw_if_index = rt->sw_if_index;
826 n_left_from = frame->n_vectors;
827 from = vlib_frame_vector_args (frame);
829 while (n_left_from > 0)
832 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
834 if (sw_if_index_from_buffer)
835 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
837 if (vm->pcap[VLIB_TX].pcap_sw_if_index == 0 ||
838 vm->pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
839 pcap_add_buffer (&vm->pcap[VLIB_TX].pcap_main, vm, bi0, 512);
845 #ifndef CLIB_MARCH_VARIANT
846 static_always_inline uword
847 vnet_interface_output_node_inline (vlib_main_t * vm,
848 vlib_node_runtime_t * node,
849 vlib_frame_t * frame, vnet_main_t * vnm,
850 vnet_hw_interface_t * hi,
853 vnet_interface_pcap_tx_trace (vm, node, frame,
854 0 /* sw_if_index_from_buffer */ );
857 * The 3-headed "if" is here because we want to err on the side
858 * of not impacting the non-GSO performance - so for the more
859 * common case of no GSO interfaces we want to prevent the
860 * segmentation codepath from being there altogether.
862 if (PREDICT_TRUE (vnm->interface_main.gso_interface_count == 0))
863 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
865 /* do_segmentation */ 0);
866 else if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
867 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
869 /* do_segmentation */ 0);
871 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
873 /* do_segmentation */ 1);
877 vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
878 vlib_frame_t * frame)
880 vnet_main_t *vnm = vnet_get_main ();
881 vnet_hw_interface_t *hi;
882 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
883 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
885 vnet_interface_pcap_tx_trace (vm, node, frame,
886 0 /* sw_if_index_from_buffer */ );
888 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
889 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
890 /* do_tx_offloads */ 0);
892 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
893 /* do_tx_offloads */ 1);
895 #endif /* CLIB_MARCH_VARIANT */
897 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
898 VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
899 vlib_node_runtime_t *
901 vlib_frame_t * frame)
903 vnet_main_t *vnm = vnet_get_main ();
904 u32 n_left_to_next, *from, *to_next;
905 u32 n_left_from, next_index;
907 vnet_interface_pcap_tx_trace (vm, node, frame,
908 1 /* sw_if_index_from_buffer */ );
910 n_left_from = frame->n_vectors;
912 from = vlib_frame_vector_args (frame);
913 next_index = node->cached_next_index;
915 while (n_left_from > 0)
917 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
919 while (n_left_from >= 4 && n_left_to_next >= 2)
921 u32 bi0, bi1, next0, next1;
922 vlib_buffer_t *b0, *b1;
923 vnet_hw_interface_t *hi0, *hi1;
925 /* Prefetch next iteration. */
926 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
927 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
938 b0 = vlib_get_buffer (vm, bi0);
939 b1 = vlib_get_buffer (vm, bi1);
942 vnet_get_sup_hw_interface (vnm,
943 vnet_buffer (b0)->sw_if_index
946 vnet_get_sup_hw_interface (vnm,
947 vnet_buffer (b1)->sw_if_index
950 next0 = hi0->output_node_next_index;
951 next1 = hi1->output_node_next_index;
953 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
954 n_left_to_next, bi0, bi1, next0,
958 while (n_left_from > 0 && n_left_to_next > 0)
962 vnet_hw_interface_t *hi0;
971 b0 = vlib_get_buffer (vm, bi0);
974 vnet_get_sup_hw_interface (vnm,
975 vnet_buffer (b0)->sw_if_index
978 next0 = hi0->output_node_next_index;
980 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
981 n_left_to_next, bi0, next0);
984 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
987 return frame->n_vectors;
990 typedef struct vnet_error_trace_t_
993 } vnet_error_trace_t;
997 format_vnet_error_trace (u8 * s, va_list * va)
999 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1000 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1001 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
1003 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
1004 vnet_get_main (), t->sw_if_index);
1010 interface_trace_buffers (vlib_main_t * vm,
1011 vlib_node_runtime_t * node, vlib_frame_t * frame)
1013 u32 n_left, *buffers;
1015 buffers = vlib_frame_vector_args (frame);
1016 n_left = frame->n_vectors;
1021 vlib_buffer_t *b0, *b1;
1022 vnet_error_trace_t *t0, *t1;
1024 /* Prefetch next iteration. */
1025 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
1026 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
1031 b0 = vlib_get_buffer (vm, bi0);
1032 b1 = vlib_get_buffer (vm, bi1);
1034 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1036 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1037 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1039 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1041 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1042 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1052 vnet_error_trace_t *t0;
1056 b0 = vlib_get_buffer (vm, bi0);
1058 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1060 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1061 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1070 VNET_ERROR_DISPOSITION_DROP,
1071 VNET_ERROR_DISPOSITION_PUNT,
1072 VNET_ERROR_N_DISPOSITION,
1073 } vnet_error_disposition_t;
1075 static_always_inline uword
1076 interface_drop_punt (vlib_main_t * vm,
1077 vlib_node_runtime_t * node,
1078 vlib_frame_t * frame,
1079 vnet_error_disposition_t disposition)
1081 u32 *from, n_left, thread_index, *sw_if_index;
1082 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1083 u32 sw_if_indices[VLIB_FRAME_SIZE];
1084 vlib_simple_counter_main_t *cm;
1085 u16 nexts[VLIB_FRAME_SIZE];
1088 vnm = vnet_get_main ();
1089 thread_index = vm->thread_index;
1090 from = vlib_frame_vector_args (frame);
1091 n_left = frame->n_vectors;
1093 sw_if_index = sw_if_indices;
1095 vlib_get_buffers (vm, from, bufs, n_left);
1097 if (node->flags & VLIB_NODE_FLAG_TRACE)
1098 interface_trace_buffers (vm, node, frame);
1100 /* All going to drop regardless, this is just a counting exercise */
1101 clib_memset (nexts, 0, sizeof (nexts));
1103 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1104 (disposition == VNET_ERROR_DISPOSITION_PUNT
1105 ? VNET_INTERFACE_COUNTER_PUNT
1106 : VNET_INTERFACE_COUNTER_DROP));
1108 /* collect the array of interfaces first ... */
1113 /* Prefetch 8 ahead - there's not much going on in each iteration */
1114 vlib_prefetch_buffer_header (b[4], LOAD);
1115 vlib_prefetch_buffer_header (b[5], LOAD);
1116 vlib_prefetch_buffer_header (b[6], LOAD);
1117 vlib_prefetch_buffer_header (b[7], LOAD);
1119 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1120 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
1121 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
1122 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
1130 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1137 /* ... then count against them in blocks */
1138 n_left = frame->n_vectors;
1142 vnet_sw_interface_t *sw_if0;
1145 off = frame->n_vectors - n_left;
1147 sw_if_index = sw_if_indices + off;
1149 count = clib_count_equal_u32 (sw_if_index, n_left);
1152 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
1154 /* Increment super-interface drop/punt counters for
1156 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
1157 if (sw_if0->sup_sw_if_index != sw_if_index[0])
1158 vlib_increment_simple_counter
1159 (cm, thread_index, sw_if0->sup_sw_if_index, count);
1162 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1164 return frame->n_vectors;
1168 pcap_drop_trace (vlib_main_t * vm,
1169 vnet_interface_main_t * im, vlib_frame_t * f)
1172 u32 n_left = f->n_vectors;
1173 vlib_buffer_t *b0, *p1;
1175 i16 save_current_data;
1176 u16 save_current_length;
1178 from = vlib_frame_vector_args (f);
1182 if (PREDICT_TRUE (n_left > 1))
1184 p1 = vlib_get_buffer (vm, from[1]);
1185 vlib_prefetch_buffer_header (p1, LOAD);
1189 b0 = vlib_get_buffer (vm, bi0);
1193 /* See if we're pointedly ignoring this specific error */
1194 if (im->pcap_drop_filter_hash
1195 && hash_get (im->pcap_drop_filter_hash, b0->error))
1198 /* Trace all drops, or drops received on a specific interface */
1199 if (im->pcap_sw_if_index == 0 ||
1200 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
1202 save_current_data = b0->current_data;
1203 save_current_length = b0->current_length;
1206 * Typically, we'll need to rewind the buffer
1208 if (b0->current_data > 0)
1209 vlib_buffer_advance (b0, (word) - b0->current_data);
1211 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1213 b0->current_data = save_current_data;
1214 b0->current_length = save_current_length;
1219 #ifndef CLIB_MARCH_VARIANT
1221 vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
1223 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1225 if (im->pcap_drop_filter_hash == 0)
1226 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
1229 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1231 hash_unset (im->pcap_drop_filter_hash, error_index);
1233 #endif /* CLIB_MARCH_VARIANT */
1235 VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
1236 vlib_node_runtime_t * node,
1237 vlib_frame_t * frame)
1239 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1241 if (PREDICT_FALSE (im->drop_pcap_enable))
1242 pcap_drop_trace (vm, im, frame);
1244 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
1247 VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
1248 vlib_node_runtime_t * node,
1249 vlib_frame_t * frame)
1251 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
1255 VLIB_REGISTER_NODE (interface_drop) = {
1256 .name = "error-drop",
1257 .vector_size = sizeof (u32),
1258 .format_trace = format_vnet_error_trace,
1267 VLIB_REGISTER_NODE (interface_punt) = {
1268 .name = "error-punt",
1269 .vector_size = sizeof (u32),
1270 .format_trace = format_vnet_error_trace,
1279 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
1280 .name = "interface-output",
1281 .vector_size = sizeof (u32),
1286 interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1287 vlib_frame_t * from_frame)
1289 vnet_main_t *vnm = vnet_get_main ();
1290 u32 last_sw_if_index = ~0;
1291 vlib_frame_t *to_frame = 0;
1292 vnet_hw_interface_t *hw = 0;
1293 u32 *from, *to_next = 0;
1296 from = vlib_frame_vector_args (from_frame);
1297 n_left_from = from_frame->n_vectors;
1298 while (n_left_from > 0)
1307 b0 = vlib_get_buffer (vm, bi0);
1308 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1310 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1314 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1315 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1317 last_sw_if_index = sw_if_index0;
1318 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1319 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1320 to_next = vlib_frame_vector_args (to_frame);
1325 to_frame->n_vectors++;
1327 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1328 return from_frame->n_vectors;
1332 VLIB_REGISTER_NODE (interface_tx, static) = {
1333 .function = interface_tx_node_fn,
1334 .name = "interface-tx",
1335 .vector_size = sizeof (u32),
1342 VNET_FEATURE_ARC_INIT (interface_output, static) =
1344 .arc_name = "interface-output",
1345 .start_nodes = VNET_FEATURES (0),
1346 .last_in_arc = "interface-tx",
1347 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1350 VNET_FEATURE_INIT (span_tx, static) = {
1351 .arc_name = "interface-output",
1352 .node_name = "span-output",
1353 .runs_before = VNET_FEATURES ("interface-tx"),
1356 VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1357 .arc_name = "interface-output",
1358 .node_name = "ipsec-if-output",
1359 .runs_before = VNET_FEATURES ("interface-tx"),
1362 VNET_FEATURE_INIT (interface_tx, static) = {
1363 .arc_name = "interface-output",
1364 .node_name = "interface-tx",
1369 #ifndef CLIB_MARCH_VARIANT
1371 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1375 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1378 if (hi->output_node_index == 0)
1381 next_index = vlib_node_add_next
1382 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1383 hi->output_node_index);
1384 hi->output_node_next_index = next_index;
1389 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1390 (vnet_per_buffer_interface_output_hw_interface_add_del);
1393 vnet_set_interface_output_node (vnet_main_t * vnm,
1394 u32 hw_if_index, u32 node_index)
1396 ASSERT (node_index);
1397 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1398 u32 next_index = vlib_node_add_next
1399 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1400 hi->output_node_next_index = next_index;
1401 hi->output_node_index = node_index;
1403 #endif /* CLIB_MARCH_VARIANT */
1405 static clib_error_t *
1406 pcap_drop_trace_command_fn (vlib_main_t * vm,
1407 unformat_input_t * input,
1408 vlib_cli_command_t * cmd)
1410 vnet_main_t *vnm = vnet_get_main ();
1411 vnet_interface_main_t *im = &vnm->interface_main;
1415 clib_error_t *error = 0;
1417 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1419 if (unformat (input, "on"))
1421 if (im->drop_pcap_enable == 0)
1423 if (im->pcap_filename == 0)
1424 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
1426 clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
1427 im->pcap_main.file_name = (char *) im->pcap_filename;
1428 im->pcap_main.n_packets_to_capture = 100;
1429 if (im->pcap_pkts_to_capture)
1430 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1432 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1433 im->drop_pcap_enable = 1;
1435 vlib_cli_output (vm, "pcap drop capture on...");
1439 vlib_cli_output (vm, "pcap drop capture already on...");
1443 else if (unformat (input, "off"))
1447 if (im->drop_pcap_enable)
1449 vlib_cli_output (vm, "captured %d pkts...",
1450 im->pcap_main.n_packets_captured);
1451 if (im->pcap_main.n_packets_captured)
1453 im->pcap_main.n_packets_to_capture =
1454 im->pcap_main.n_packets_captured;
1455 error = pcap_write (&im->pcap_main);
1457 clib_error_report (error);
1459 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1464 vlib_cli_output (vm, "pcap drop capture already off...");
1467 im->drop_pcap_enable = 0;
1469 else if (unformat (input, "max %d", &max))
1471 im->pcap_pkts_to_capture = max;
1475 else if (unformat (input, "intfc %U",
1476 unformat_vnet_sw_interface, vnm,
1477 &im->pcap_sw_if_index))
1479 else if (unformat (input, "intfc any"))
1481 im->pcap_sw_if_index = 0;
1484 else if (unformat (input, "file %s", &filename))
1486 u8 *chroot_filename;
1487 /* Brain-police user path input */
1488 if (strstr ((char *) filename, "..")
1489 || index ((char *) filename, '/'))
1491 vlib_cli_output (vm, "illegal characters in filename '%s'",
1496 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1497 vec_free (filename);
1499 if (im->pcap_filename)
1500 vec_free (im->pcap_filename);
1501 im->pcap_filename = chroot_filename;
1502 im->pcap_main.file_name = (char *) im->pcap_filename;
1505 else if (unformat (input, "status"))
1507 if (im->drop_pcap_enable == 0)
1509 vlib_cli_output (vm, "pcap drop capture is off...");
1513 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1514 im->pcap_main.n_packets_captured,
1515 im->pcap_main.n_packets_to_capture);
1524 return clib_error_return (0, "unknown input `%U'",
1525 format_unformat_error, input);
1531 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
1532 .path = "pcap drop trace",
1534 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1535 .function = pcap_drop_trace_command_fn,
1540 * fd.io coding-style-patch-verification: ON
1543 * eval: (c-set-style "gnu")