interface: fix pcap drop trace in case of vlan
[vpp.git] / src / vnet / interface_output.c
index 698c6e1..f51e9a1 100644 (file)
@@ -179,8 +179,6 @@ vnet_interface_output_trace (vlib_main_t * vm,
 static_always_inline void
 calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
 {
-  ip4_header_t *ip4;
-  ip6_header_t *ip6;
   tcp_header_t *th;
   udp_header_t *uh;
 
@@ -189,13 +187,13 @@ calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
 
   ASSERT (!(is_ip4 && is_ip6));
 
-  ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
-  ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
   th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
   uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
 
   if (is_ip4)
     {
+      ip4_header_t *ip4;
+
       ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
        ip4->checksum = ip4_header_checksum (ip4);
@@ -204,26 +202,28 @@ calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
          th->checksum = 0;
          th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
        }
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
+      else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
        uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
     }
-  if (is_ip6)
+  else if (is_ip6)
     {
       int bogus;
+      ip6_header_t *ip6;
+
+      ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
        {
          th->checksum = 0;
          th->checksum =
            ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
        }
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
+      else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
        {
          uh->checksum = 0;
          uh->checksum =
            ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
        }
     }
-
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
@@ -232,13 +232,14 @@ calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
 static_always_inline u16
 tso_alloc_tx_bufs (vlib_main_t * vm,
                   vnet_interface_per_thread_data_t * ptd,
-                  vlib_buffer_t * b0, u16 l4_hdr_sz)
+                  vlib_buffer_t * b0, u32 n_bytes_b0, u16 l234_sz,
+                  u16 gso_size)
 {
-  u32 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
-  u16 gso_size = vnet_buffer2 (b0)->gso_size;
-  u16 l234_sz = vnet_buffer (b0)->l4_hdr_offset + l4_hdr_sz;
+  u16 size =
+    clib_min (gso_size, vlib_buffer_get_default_data_size (vm) - l234_sz);
+
   /* rounded-up division */
-  u16 n_bufs = (n_bytes_b0 - l234_sz + (gso_size - 1)) / gso_size;
+  u16 n_bufs = (n_bytes_b0 - l234_sz + (size - 1)) / size;
   u16 n_alloc;
 
   ASSERT (n_bufs > 0);
@@ -250,18 +251,19 @@ tso_alloc_tx_bufs (vlib_main_t * vm,
       vlib_buffer_free (vm, ptd->split_buffers, n_alloc);
       return 0;
     }
-  return 1;
+  return n_alloc;
 }
 
 static_always_inline void
 tso_init_buf_from_template_base (vlib_buffer_t * nb0, vlib_buffer_t * b0,
                                 u32 flags, u16 length)
 {
-  nb0->current_data = 0;
+  nb0->current_data = b0->current_data;
   nb0->total_length_not_including_first_buffer = 0;
   nb0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | flags;
   clib_memcpy_fast (&nb0->opaque, &b0->opaque, sizeof (nb0->opaque));
-  clib_memcpy_fast (nb0->data, b0->data, length);
+  clib_memcpy_fast (vlib_buffer_get_current (nb0),
+                   vlib_buffer_get_current (b0), length);
   nb0->current_length = length;
 }
 
@@ -275,8 +277,9 @@ tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
 
   *p_dst_left =
     clib_min (gso_size,
-             vlib_buffer_get_default_data_size (vm) - template_data_sz);
-  *p_dst_ptr = nb0->data + template_data_sz;
+             vlib_buffer_get_default_data_size (vm) - (template_data_sz +
+                                                       nb0->current_data));
+  *p_dst_ptr = vlib_buffer_get_current (nb0) + template_data_sz;
 
   tcp_header_t *tcp =
     (tcp_header_t *) (nb0->data + vnet_buffer (nb0)->l4_hdr_offset);
@@ -297,11 +300,11 @@ tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6)
   if (is_ip6)
     ip6->payload_length =
       clib_host_to_net_u16 (b0->current_length -
-                           vnet_buffer (b0)->l4_hdr_offset);
+                           (l4_hdr_offset - b0->current_data));
   else
     ip4->length =
       clib_host_to_net_u16 (b0->current_length -
-                           vnet_buffer (b0)->l3_hdr_offset);
+                           (l3_hdr_offset - b0->current_data));
 }
 
 /**
@@ -342,16 +345,18 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
 
   u32 default_bflags =
     sb0->flags & ~(VNET_BUFFER_F_GSO | VLIB_BUFFER_NEXT_PRESENT);
-  u16 l234_sz = vnet_buffer (sb0)->l4_hdr_offset + l4_hdr_sz;
+  u16 l234_sz = vnet_buffer (sb0)->l4_hdr_offset + l4_hdr_sz
+    - sb0->current_data;
   int first_data_size = clib_min (gso_size, sb0->current_length - l234_sz);
   next_tcp_seq += first_data_size;
 
-  if (PREDICT_FALSE (!tso_alloc_tx_bufs (vm, ptd, sb0, l4_hdr_sz)))
+  if (PREDICT_FALSE
+      (!tso_alloc_tx_bufs (vm, ptd, sb0, n_bytes_b0, l234_sz, gso_size)))
     return 0;
 
   vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[0]);
   tso_init_buf_from_template_base (b0, sb0, default_bflags,
-                                  l4_hdr_sz + first_data_size);
+                                  l234_sz + first_data_size);
 
   u32 total_src_left = n_bytes_b0 - l234_sz - first_data_size;
   if (total_src_left)
@@ -366,9 +371,8 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
       vlib_buffer_t *cdb0;
       u16 dbi = 1;             /* the buffer [0] is b0 */
 
-      src_ptr = sb0->data + l234_sz + first_data_size;
+      src_ptr = vlib_buffer_get_current (sb0) + l234_sz + first_data_size;
       src_left = sb0->current_length - l234_sz - first_data_size;
-      b0->current_length = l234_sz + first_data_size;
 
       tso_fixup_segmented_buf (b0, tcp_flags_no_fin_psh, is_ip6);
       if (do_tx_offloads)
@@ -409,7 +413,7 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
                  csbi0 = next_bi;
                  csb0 = vlib_get_buffer (vm, csbi0);
                  src_left = csb0->current_length;
-                 src_ptr = csb0->data;
+                 src_ptr = vlib_buffer_get_current (csb0);
                }
              else
                {
@@ -482,6 +486,7 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
   u8 arc = im->output_feature_arc_index;
   vnet_interface_per_thread_data_t *ptd =
     vec_elt_at_index (im->per_thread_data, thread_index);
+  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
 
   n_buffers = frame->n_vectors;
 
@@ -489,6 +494,7 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
     vnet_interface_output_trace (vm, node, frame, n_buffers);
 
   from = vlib_frame_vector_args (frame);
+  vlib_get_buffers (vm, from, b, n_buffers);
 
   if (rt->is_deleted)
     return vlib_error_drop_buffers (vm, node, from,
@@ -500,8 +506,7 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
 
   si = vnet_get_sw_interface (vnm, rt->sw_if_index);
   hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
-  if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
-                    VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
+  if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
       !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
     {
       vlib_simple_counter_main_t *cm;
@@ -545,15 +550,14 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
       while (from + 8 <= from_end && n_left_to_tx >= 4)
        {
          u32 bi0, bi1, bi2, bi3;
-         vlib_buffer_t *b0, *b1, *b2, *b3;
          u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
          u32 or_flags;
 
          /* Prefetch next iteration. */
-         vlib_prefetch_buffer_with_index (vm, from[4], LOAD);
-         vlib_prefetch_buffer_with_index (vm, from[5], LOAD);
-         vlib_prefetch_buffer_with_index (vm, from[6], LOAD);
-         vlib_prefetch_buffer_with_index (vm, from[7], LOAD);
+         vlib_prefetch_buffer_header (b[4], LOAD);
+         vlib_prefetch_buffer_header (b[5], LOAD);
+         vlib_prefetch_buffer_header (b[6], LOAD);
+         vlib_prefetch_buffer_header (b[7], LOAD);
 
          bi0 = from[0];
          bi1 = from[1];
@@ -563,45 +567,34 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
          to_tx[1] = bi1;
          to_tx[2] = bi2;
          to_tx[3] = bi3;
-         if (!do_segmentation)
-           {
-             from += 4;
-             to_tx += 4;
-             n_left_to_tx -= 4;
-           }
 
-         b0 = vlib_get_buffer (vm, bi0);
-         b1 = vlib_get_buffer (vm, bi1);
-         b2 = vlib_get_buffer (vm, bi2);
-         b3 = vlib_get_buffer (vm, bi3);
+         or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
 
          if (do_segmentation)
            {
-             or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
-
              /* go to single loop if we need TSO segmentation */
              if (PREDICT_FALSE (or_flags & VNET_BUFFER_F_GSO))
                break;
-             from += 4;
-             to_tx += 4;
-             n_left_to_tx -= 4;
            }
+         from += 4;
+         to_tx += 4;
+         n_left_to_tx -= 4;
 
          /* Be grumpy about zero length buffers for benefit of
             driver tx function. */
-         ASSERT (b0->current_length > 0);
-         ASSERT (b1->current_length > 0);
-         ASSERT (b2->current_length > 0);
-         ASSERT (b3->current_length > 0);
-
-         n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
-         n_bytes_b1 = vlib_buffer_length_in_chain (vm, b1);
-         n_bytes_b2 = vlib_buffer_length_in_chain (vm, b2);
-         n_bytes_b3 = vlib_buffer_length_in_chain (vm, b3);
-         tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
-         tx_swif1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
-         tx_swif2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
-         tx_swif3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
+         ASSERT (b[0]->current_length > 0);
+         ASSERT (b[1]->current_length > 0);
+         ASSERT (b[2]->current_length > 0);
+         ASSERT (b[3]->current_length > 0);
+
+         n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
+         n_bytes_b1 = vlib_buffer_length_in_chain (vm, b[1]);
+         n_bytes_b2 = vlib_buffer_length_in_chain (vm, b[2]);
+         n_bytes_b3 = vlib_buffer_length_in_chain (vm, b[3]);
+         tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
+         tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
+         tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
+         tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
 
          n_bytes += n_bytes_b0 + n_bytes_b1;
          n_bytes += n_bytes_b2 + n_bytes_b3;
@@ -609,14 +602,14 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
 
          if (PREDICT_FALSE (current_config_index != ~0))
            {
-             vnet_buffer (b0)->feature_arc_index = arc;
-             vnet_buffer (b1)->feature_arc_index = arc;
-             vnet_buffer (b2)->feature_arc_index = arc;
-             vnet_buffer (b3)->feature_arc_index = arc;
-             b0->current_config_index = current_config_index;
-             b1->current_config_index = current_config_index;
-             b2->current_config_index = current_config_index;
-             b3->current_config_index = current_config_index;
+             vnet_buffer (b[0])->feature_arc_index = arc;
+             vnet_buffer (b[1])->feature_arc_index = arc;
+             vnet_buffer (b[2])->feature_arc_index = arc;
+             vnet_buffer (b[3])->feature_arc_index = arc;
+             b[0]->current_config_index = current_config_index;
+             b[1]->current_config_index = current_config_index;
+             b[2]->current_config_index = current_config_index;
+             b[3]->current_config_index = current_config_index;
            }
 
          /* update vlan subif tx counts, if required */
@@ -654,9 +647,6 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
                                               n_bytes_b3);
            }
 
-         if (!do_segmentation)
-           or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
-
          if (do_tx_offloads)
            {
              if (or_flags &
@@ -664,18 +654,19 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
                   VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
                   VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
                {
-                 calc_checksums (vm, b0);
-                 calc_checksums (vm, b1);
-                 calc_checksums (vm, b2);
-                 calc_checksums (vm, b3);
+                 calc_checksums (vm, b[0]);
+                 calc_checksums (vm, b[1]);
+                 calc_checksums (vm, b[2]);
+                 calc_checksums (vm, b[3]);
                }
            }
+         b += 4;
+
        }
 
       while (from + 1 <= from_end && n_left_to_tx >= 1)
        {
          u32 bi0;
-         vlib_buffer_t *b0;
          u32 tx_swif0;
 
          bi0 = from[0];
@@ -684,26 +675,24 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
          to_tx += 1;
          n_left_to_tx -= 1;
 
-         b0 = vlib_get_buffer (vm, bi0);
-
          /* Be grumpy about zero length buffers for benefit of
             driver tx function. */
-         ASSERT (b0->current_length > 0);
+         ASSERT (b[0]->current_length > 0);
 
-         n_bytes_b0 = vlib_buffer_length_in_chain (vm, b0);
-         tx_swif0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
+         n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
+         tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
          n_bytes += n_bytes_b0;
          n_packets += 1;
 
          if (PREDICT_FALSE (current_config_index != ~0))
            {
-             vnet_buffer (b0)->feature_arc_index = arc;
-             b0->current_config_index = current_config_index;
+             vnet_buffer (b[0])->feature_arc_index = arc;
+             b[0]->current_config_index = current_config_index;
            }
 
          if (do_segmentation)
            {
-             if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_GSO))
+             if (PREDICT_FALSE (b[0]->flags & VNET_BUFFER_F_GSO))
                {
                  /*
                   * Undo the enqueue of the b0 - it is not going anywhere,
@@ -719,13 +708,14 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
                  u32 n_tx_bytes = 0;
 
                  n_tx_bytes =
-                   tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b0,
+                   tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b[0],
                                        n_bytes_b0);
 
                  if (PREDICT_FALSE (n_tx_bytes == 0))
                    {
                      drop_one_buffer_and_count (vm, vnm, node, from - 1,
                                                 VNET_INTERFACE_OUTPUT_ERROR_NO_BUFFERS_FOR_GSO);
+                     b += 1;
                      continue;
                    }
 
@@ -750,17 +740,14 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
                          vlib_get_new_next_frame (vm, node, next_index,
                                                   to_tx, n_left_to_tx);
                        }
-                     else
+                     while (n_tx_bufs > 0)
                        {
-                         while (n_tx_bufs > 0)
-                           {
-                             to_tx[0] = from_tx_seg[0];
-                             to_tx += 1;
-                             from_tx_seg += 1;
-                             n_left_to_tx -= 1;
-                             n_tx_bufs -= 1;
-                             n_packets += 1;
-                           }
+                         to_tx[0] = from_tx_seg[0];
+                         to_tx += 1;
+                         from_tx_seg += 1;
+                         n_left_to_tx -= 1;
+                         n_tx_bufs -= 1;
+                         n_packets += 1;
                        }
                    }
                  n_bytes += n_tx_bytes;
@@ -776,6 +763,7 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
                  _vec_len (ptd->split_buffers) = 0;
                  /* Free the now segmented buffer */
                  vlib_buffer_free_one (vm, bi0);
+                 b += 1;
                  continue;
                }
            }
@@ -790,7 +778,9 @@ vnet_interface_output_node_inline_gso (vlib_main_t * vm,
            }
 
          if (do_tx_offloads)
-           calc_checksums (vm, b0);
+           calc_checksums (vm, b[0]);
+
+         b += 1;
        }
 
       vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
@@ -812,7 +802,7 @@ static_always_inline void vnet_interface_pcap_tx_trace
   u32 n_left_from, *from;
   u32 sw_if_index;
 
-  if (PREDICT_TRUE (vm->pcap[VLIB_TX].pcap_enable == 0))
+  if (PREDICT_TRUE (vlib_global_main.pcap[VLIB_TX].pcap_enable == 0))
     return;
 
   if (sw_if_index_from_buffer == 0)
@@ -834,9 +824,10 @@ static_always_inline void vnet_interface_pcap_tx_trace
       if (sw_if_index_from_buffer)
        sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
 
-      if (vm->pcap[VLIB_TX].pcap_sw_if_index == 0 ||
-         vm->pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
-       pcap_add_buffer (&vm->pcap[VLIB_TX].pcap_main, vm, bi0, 512);
+      if (vlib_global_main.pcap[VLIB_TX].pcap_sw_if_index == 0 ||
+         vlib_global_main.pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
+       pcap_add_buffer (&vlib_global_main.pcap[VLIB_TX].pcap_main, vm, bi0,
+                        512);
       from++;
       n_left_from--;
     }
@@ -1201,8 +1192,19 @@ pcap_drop_trace (vlib_main_t * vm,
 
          /*
           * Typically, we'll need to rewind the buffer
+          * if l2_hdr_offset is valid, make sure to rewind to the start of
+          * the L2 header. This may not be the buffer start in case we pop-ed
+          * vlan tags.
+          * Otherwise, rewind to buffer start and hope for the best.
           */
-         if (b0->current_data > 0)
+         if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
+           {
+             if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
+               vlib_buffer_advance (b0,
+                                    vnet_buffer (b0)->l2_hdr_offset -
+                                    b0->current_data);
+           }
+         else if (b0->current_data > 0)
            vlib_buffer_advance (b0, (word) - b0->current_data);
 
          pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
@@ -1326,7 +1328,7 @@ interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
 }
 
 /* *INDENT-OFF* */
-VLIB_REGISTER_NODE (interface_tx, static) = {
+VLIB_REGISTER_NODE (interface_tx) = {
   .function = interface_tx_node_fn,
   .name = "interface-tx",
   .vector_size = sizeof (u32),
@@ -1422,7 +1424,7 @@ pcap_drop_trace_command_fn (vlib_main_t * vm,
 
              clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
              im->pcap_main.file_name = (char *) im->pcap_filename;
-             im->pcap_main.n_packets_to_capture = 100;
+             im->pcap_main.n_packets_to_capture = PCAP_DEF_PKT_TO_CAPTURE;
              if (im->pcap_pkts_to_capture)
                im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
 
@@ -1450,6 +1452,8 @@ pcap_drop_trace_command_fn (vlib_main_t * vm,
                  im->pcap_main.n_packets_to_capture =
                    im->pcap_main.n_packets_captured;
                  error = pcap_write (&im->pcap_main);
+                 if (im->pcap_main.file_descriptor >= 0)
+                   pcap_close (&im->pcap_main);
                  if (error)
                    clib_error_report (error);
                  else