/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * interface_output.c: interface output node * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include typedef struct { u32 sw_if_index; u8 data[128 - sizeof (u32)]; } interface_output_trace_t; u8 * format_vnet_interface_output_trace (u8 * s, va_list * va) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); vlib_node_t *node = va_arg (*va, vlib_node_t *); interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *); vnet_main_t *vnm = vnet_get_main (); vnet_sw_interface_t *si; u32 indent; if (t->sw_if_index != (u32) ~ 0) { indent = format_get_indent (s); if (pool_is_free_index (vnm->interface_main.sw_interfaces, t->sw_if_index)) { /* the interface may have been deleted by the time the trace is printed */ s = format (s, "sw_if_index: %d\n%U%U", t->sw_if_index, format_white_space, indent, node->format_buffer ? node-> format_buffer : format_hex_bytes, t->data, sizeof (t->data)); } else { si = vnet_get_sw_interface (vnm, t->sw_if_index); s = format (s, "%U\n%U%U", format_vnet_sw_interface_name, vnm, si, format_white_space, indent, node->format_buffer ? node-> format_buffer : format_hex_bytes, t->data, sizeof (t->data)); } } return s; } static void vnet_interface_output_trace (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, uword n_buffers) { u32 n_left, *from; n_left = n_buffers; from = vlib_frame_args (frame); while (n_left >= 4) { u32 bi0, bi1; vlib_buffer_t *b0, *b1; interface_output_trace_t *t0, *t1; /* Prefetch next iteration. */ vlib_prefetch_buffer_with_index (vm, from[2], LOAD); vlib_prefetch_buffer_with_index (vm, from[3], LOAD); bi0 = from[0]; bi1 = from[1]; b0 = vlib_get_buffer (vm, bi0); b1 = vlib_get_buffer (vm, bi1); if (b0->flags & VLIB_BUFFER_IS_TRACED) { t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; clib_memcpy (t0->data, vlib_buffer_get_current (b0), sizeof (t0->data)); } if (b1->flags & VLIB_BUFFER_IS_TRACED) { t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX]; clib_memcpy (t1->data, vlib_buffer_get_current (b1), sizeof (t1->data)); } from += 2; n_left -= 2; } while (n_left >= 1) { u32 bi0; vlib_buffer_t *b0; interface_output_trace_t *t0; bi0 = from[0]; b0 = vlib_get_buffer (vm, bi0); if (b0->flags & VLIB_BUFFER_IS_TRACED) { t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; clib_memcpy (t0->data, vlib_buffer_get_current (b0), sizeof (t0->data)); } from += 1; n_left -= 1; } } 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; int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0; int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0; 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 = (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); if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM) th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4); if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4); } if (is_ip6) { int bogus; if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM) th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus); if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) 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; } static_always_inline uword vnet_interface_output_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, vnet_main_t * vnm, vnet_hw_interface_t * hi, int do_tx_offloads) { vnet_interface_output_runtime_t *rt = (void *) node->runtime_data; vnet_sw_interface_t *si; u32 n_left_to_tx, *from, *from_end, *to_tx; u32 n_bytes, n_buffers, n_packets; u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3; u32 thread_index = vm->thread_index; vnet_interface_main_t *im = &vnm->interface_main; u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX; u32 current_config_index = ~0; u8 arc = im->output_feature_arc_index; n_buffers = frame->n_vectors; if (node->flags & VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace (vm, node, frame, n_buffers); from = vlib_frame_args (frame); if (rt->is_deleted) return vlib_error_drop_buffers (vm, node, from, /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP, node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED); 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)) || !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)) { vlib_simple_counter_main_t *cm; cm = vec_elt_at_index (vnm->interface_main.sw_if_counters, VNET_INTERFACE_COUNTER_TX_ERROR); vlib_increment_simple_counter (cm, thread_index, rt->sw_if_index, n_buffers); return vlib_error_drop_buffers (vm, node, from, /* buffer stride */ 1, n_buffers, VNET_INTERFACE_OUTPUT_NEXT_DROP, node->node_index, VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN); } from_end = from + n_buffers; /* Total byte count of all buffers. */ n_bytes = 0; n_packets = 0; /* interface-output feature arc handling */ if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index))) { vnet_feature_config_main_t *fcm; fcm = vnet_feature_get_config_main (arc); current_config_index = vnet_get_feature_config_index (arc, rt->sw_if_index); vnet_get_config_data (&fcm->config_main, ¤t_config_index, &next_index, 0); } while (from < from_end) { /* Get new next frame since previous incomplete frame may have less than VNET_FRAME_SIZE vectors in it. */ vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx); 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); bi0 = from[0]; bi1 = from[1]; bi2 = from[2]; bi3 = from[3]; to_tx[0] = bi0; to_tx[1] = bi1; to_tx[2] = bi2; to_tx[3] = bi3; 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); /* 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]; n_bytes += n_bytes_b0 + n_bytes_b1; n_bytes += n_bytes_b2 + n_bytes_b3; n_packets += 4; if (PREDICT_FALSE (current_config_index != ~0)) { b0->feature_arc_index = arc; b1->feature_arc_index = arc; b2->feature_arc_index = arc; 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; } /* update vlan subif tx counts, if required */ if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index)) { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif0, 1, n_bytes_b0); } if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index)) { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif1, 1, n_bytes_b1); } if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index)) { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif2, 1, n_bytes_b2); } if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index)) { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif3, 1, n_bytes_b3); } or_flags = b0->flags | b1->flags | b2->flags | b3->flags; if (do_tx_offloads) { if (or_flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM | 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); } } } while (from + 1 <= from_end && n_left_to_tx >= 1) { u32 bi0; vlib_buffer_t *b0; u32 tx_swif0; bi0 = from[0]; to_tx[0] = bi0; from
/*
 * Copyright (c) 2017 SUSE LLC.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <vnet/vnet.h>
#include <vlibmemory/api.h>

#include <vnet/interface.h>
#include <vnet/api_errno.h>
#include <vnet/feature/feature.h>
#include <vnet/geneve/geneve.h>
#include <vnet/fib/fib_table.h>
#include <vnet/ip/ip_types_api.h>

#include <vnet/vnet_msg_enum.h>

#define vl_typedefs		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_typedefs

#define vl_endianfun		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <vnet/vnet_all_api_h.h>
#undef vl_printfun

#include <vlibapi/api_helper_macros.h>

#define foreach_vpe_api_msg                             \
_(SW_INTERFACE_SET_GENEVE_BYPASS, sw_interface_set_geneve_bypass)         \
_(GENEVE_ADD_DEL_TUNNEL, geneve_add_del_tunnel)                           \
_(GENEVE_TUNNEL_DUMP, geneve_tunnel_dump)

static void
  vl_api_sw_interface_set_geneve_bypass_t_handler
  (vl_api_sw_interface_set_geneve_bypass_t * mp)
{
  vl_api_sw_interface_set_geneve_bypass_reply_t *rmp;
  int rv = 0;
  u32 sw_if_index = ntohl (mp->sw_if_index);

  VALIDATE_SW_IF_INDEX (mp);

  vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
  BAD_SW_IF_INDEX_LABEL;

  REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY);
}

static void vl_api_geneve_add_del_tunnel_t_handler
  (vl_api_geneve_add_del_tunnel_t * mp)
{
  vl_api_geneve_add_del_tunnel_reply_t *rmp;
  int rv = 0;
  ip4_main_t *im = &ip4_main;

  uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
  if (!p)
    {
      rv = VNET_API_ERROR_NO_SUCH_FIB;
      goto out;
    }

  vnet_geneve_add_del_tunnel_args_t a = {
    .is_add = mp->is_add,
    .is_ip6 = mp->remote_address.af,
    .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
    .encap_fib_index = p[0],
    .decap_next_index = ntohl (mp->decap_next_index),
    .vni = ntohl (mp->vni),
  };

  ip_address_decode (&mp->remote_address, &a.remote);
  ip_address_decode (&mp->local_address, &a.local);

  /* Check src & dst are different */
  if (ip46_address_cmp (&a.remote, &a.local) == 0)
    {
      rv = VNET_API_ERROR_SAME_SRC_DST;
      goto out;
    }
  if (ip46_address_is_multicast (&a.remote) &&
      !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
    {
      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
      goto out;
    }

  u32 sw_if_index = ~0;
  rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);

out:
  /* *INDENT-OFF* */
  REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY,
  ({
    rmp->sw_if_index = ntohl (sw_if_index);
  }));
  /* *INDENT-ON* */
}

static void send_geneve_tunnel_details
  (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context)
{
  vl_api_geneve_tunnel_details_t *rmp;
  ip4_main_t *im4 = &ip4_main;
  ip6_main_t *im6 = &ip6_main;
  u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote);

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  clib_memset (rmp, 0, sizeof (*rmp));
  rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS);
  ip_address_encode (&t</