/* *------------------------------------------------------------------ * interface_api.c - vnet interface api * * Copyright (c) 2016 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. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define vl_typedefs /* define message structures */ #include #undef vl_typedefs #define vl_endianfun /* define message structures */ #include #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 #undef vl_printfun #include vpe_api_main_t vpe_api_main; #define foreach_vpe_api_msg \ _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \ _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \ _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \ _(WANT_INTERFACE_EVENTS, want_interface_events) \ _(SW_INTERFACE_DUMP, sw_interface_dump) \ _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \ _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \ _(SW_INTERFACE_RX_PLACEMENT_DUMP, sw_interface_rx_placement_dump) \ _(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \ _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \ _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \ _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \ _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \ _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \ _(SW_INTERFACE_ADD_DEL_MAC_ADDRESS, sw_interface_add_del_mac_address) \ _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \ _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \ _(CREATE_VLAN_SUBIF, create_vlan_subif) \ _(CREATE_SUBIF, create_subif) \ _(DELETE_SUBIF, delete_subif) \ _(CREATE_LOOPBACK, create_loopback) \ _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \ _(DELETE_LOOPBACK, delete_loopback) \ _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \ _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \ _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \ sw_interface_set_ip_directed_broadcast) static void vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp) { vl_api_sw_interface_set_flags_reply_t *rmp; vnet_main_t *vnm = vnet_get_main (); int rv = 0; clib_error_t *error; u16 flags; VALIDATE_SW_IF_INDEX (mp); flags = ((ntohl (mp->flags)) & IF_STATUS_API_FLAG_ADMIN_UP) ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0; error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags); if (error) { rv = -1; clib_error_report (error); } BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY); } static void vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp) { vl_api_hw_interface_set_mtu_reply_t *rmp; vnet_main_t *vnm = vnet_get_main (); u32 sw_if_index = ntohl (mp->sw_if_index); u16 mtu = ntohs (mp->mtu); ethernet_main_t *em = ðernet_main; int rv = 0; VALIDATE_SW_IF_INDEX (mp); vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE) { rv = VNET_API_ERROR_INVALID_VALUE; goto bad_sw_if_index; } vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index); ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index); if (!eif) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto bad_sw_if_index; } if (mtu < hi->min_supported_packet_bytes) { rv = VNET_API_ERROR_INVALID_VALUE; goto bad_sw_if_index; } if (mtu > hi->max_supported_packet_bytes) { rv = VNET_API_ERROR_INVALID_VALUE; goto bad_sw_if_index; } vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY); } static void vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp) { vl_api_sw_interface_set_mtu_reply_t *rmp; vnet_main_t *vnm = vnet_get_main (); u32 sw_if_index = ntohl (mp->sw_if_index); int rv = 0; int i; u32 per_protocol_mtu[VNET_N_MTU]; VALIDATE_SW_IF_INDEX (mp); for (i = 0; i < VNET_N_MTU; i++) { per_protocol_mtu[i] = ntohl (mp->mtu[i]); clib_warning ("MTU %u", per_protocol_mtu[i]); } vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY); } static void vl_api_sw_interface_set_ip_directed_broadcast_t_handler (vl_api_sw_interface_set_ip_directed_broadcast_t * mp) { vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp; u32 sw_if_index = ntohl (mp->sw_if_index); int rv = 0; VALIDATE_SW_IF_INDEX (mp); vnet_sw_interface_ip_directed_broadcast (vnet_get_main (), sw_if_index, mp->enable); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY); } static void send_sw_interface_details (vpe_api_main_t * am, vl_api_registration_t * rp, vnet_sw_interface_t * swif, u8 * interface_name, u32 context) { vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); vnet_device_class_t *dev_class = vnet_get_device_class (am->vnet_main, hi->dev_class_index); vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS); mp->sw_if_index = ntohl (swif->sw_if_index); mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index); mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? IF_STATUS_API_FLAG_ADMIN_UP : 0; mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? IF_STATUS_API_FLAG_LINK_UP : 0; mp->flags = ntohl (mp->flags); switch (swif->type) { case VNET_SW_INTERFACE_TYPE_SUB: mp->type = IF_API_TYPE_SUB; break; case VNET_SW_INTERFACE_TYPE_P2P: mp->type = IF_API_TYPE_P2P; break; case VNET_SW_INTERFACE_TYPE_PIPE: mp->type = IF_API_TYPE_PIPE; break; default: mp->type = IF_API_TYPE_HARDWARE; } mp->type = ntohl (mp->type); mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >> VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT); mp->link_speed = ntohl (hi->link_speed); mp->link_mtu = ntohs (hi->max_packet_bytes); mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]); mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]); mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]); mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]); mp->context = context; strncpy ((char *) mp->interface_name, (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1); if (dev_class && dev_class->name) strncpy ((char *) mp->interface_dev_type, (char *) dev_class->name, ARRAY_LEN (mp->interface_dev_type) - 1); /* Send the L2 address for ethernet physical intfcs */ if (swif->sup_sw_if_index == swif->sw_if_index && hi->hw_class_index == ethernet_hw_interface_class.index) { ethernet_main_t *em = ethernet_get_main (am->vlib_main); ethernet_interface_t *ei; ei = pool_elt_at_index (em->interfaces, hi->hw_instance); ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address)); mac_address_encode ((mac_address_t *) ei->address, mp->l2_address); } else if (swif->sup_sw_if_index != swif->sw_if_index) { vnet_sub_interface_t *sub = &swif->sub; mp->sub_id = ntohl (sub->id); mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2; mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id); mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id); mp->sub_if_flags = ntohl (sub->eth.raw_flags & SUB_IF_API_FLAG_MASK_VNET); } /* vlan tag rewrite data */ u32 vtr_op = L2_VTR_DISABLED; u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0; if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index, &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) { // error - default to disabled mp->vtr_op = ntohl (L2_VTR_DISABLED); clib_warning ("cannot get vlan tag rewrite for sw_if_index %d", swif->sw_if_index); } else { mp->vtr_op = ntohl (vtr_op); mp->vtr_push_dot1q = ntohl (vtr_push_dot1q); mp->vtr_tag1 = ntohl (vtr_tag1); mp->vtr_tag2 = ntohl (vtr_tag2); } /* pbb tag rewrite data */ ethernet_header_t eth_hdr; u32 pbb_vtr_op = L2_VTR_DISABLED; u16 outer_tag = 0; u16 b_vlanid = 0; u32 i_sid = 0; clib_memset (ð_hdr, 0, sizeof (eth_hdr)); if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index, &pbb_vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid)) { mp->sub_if_flags |= ntohl (SUB_IF_API_FLAG_DOT1AH); mac_address_encode ((mac_address_t *) eth_hdr.dst_address, mp->b_dmac); mac_address_encode ((mac_address_t *) eth_hdr.src_address, mp->b_smac); mp->b_vlanid = b_vlanid; mp->i_sid = i_sid; } u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index); if (tag) strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1); vl_api_send_msg (rp, (u8 *) mp); } static void vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp) { vpe_api_main_t *am = &vpe_api_main; vnet_sw_interface_t *swif; vnet_interface_main_t *im = &am->vnet_main->interface_main; vl_api_registration_t *rp; u32 sw_if_index; rp = vl_api_client_index_to_registration (mp->client_index); if (rp == 0) { clib_warning ("Client %d AWOL", mp->client_index); return; } u8 *filter = 0, *name = 0; sw_if_index = ntohl (mp->sw_if_index); if (!mp->name_filter_valid && sw_if_index != ~0 && sw_if_index != 0) { /* is it a valid sw_if_index? */ if (!vnet_sw_if_index_is_api_valid (sw_if_index)) return; swif = vec_elt_at_index (im->sw_interfaces, sw_if_index); vec_reset_length (name); name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main, swif, 0); send_sw_interface_details (am, rp, swif, name, mp->context); vec_free (name); return; } if (mp->name_filter_valid) { filter = vl_api_from_api_to_new_vec (&mp->name_filter); vec_add1 (filter, 0); /* Ensure it's a C string for strcasecmp() */ } char *strcasestr (char *, char *); /* lnx hdr file botch */ /* *INDENT-OFF* */ pool_foreach (swif, im->sw_interfaces, ({ if (!vnet_swif_is_api_visible (swif)) continue; vec_reset_length(name); name = format (name, "%U%c", format_vnet_sw_i
/*
 * 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.
 */
/*
  Copyright (c) 2004 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.
*/

#ifndef included_clib_byte_order_h
#define included_clib_byte_order_h

#include <vppinfra/clib.h>

#if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__)
#define CLIB_ARCH_IS_BIG_ENDIAN (0)
#define CLIB_ARCH_IS_LITTLE_ENDIAN (1)
#else
/* Default is big endian. */
#define CLIB_ARCH_IS_BIG_ENDIAN (1)
#define CLIB_ARCH_IS_LITTLE_ENDIAN (0)
#endif

/* Big/little endian. */
#define clib_arch_is_big_endian    CLIB_ARCH_IS_BIG_ENDIAN
#define clib_arch_is_little_endian CLIB_ARCH_IS_LITTLE_ENDIAN

always_inline u16
clib_byte_swap_u16 (u16 x)
{
#if defined (__aarch64__)
  if (!__builtin_constant_p (x))
    {
    __asm__ ("rev16 %w0, %w0":"+r" (x));
      return x;
    }
#endif
  return (x >> 8) | (x << 8);
}

always_inline i16
clib_byte_swap_i16 (i16 x)
{
  return clib_byte_swap_u16 (x);
}

always_inline u32
clib_byte_swap_u32 (u32 x)
{
#if defined (i386) || defined (__x86_64__)
  if (!__builtin_constant_p (x))
    {
      asm volatile ("bswap %0":"=r" (x):"0" (x));
      return x;
    }
#elif defined (__aarch64__)
  if (!__builtin_constant_p (x))
    {
    __asm__ ("rev %w0, %w0":"+r" (x));
      return x;
    }
#endif
  return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
}

always_inline i32
clib_byte_swap_i32 (i32 x)
{
  return clib_byte_swap_u32 (x);
}

always_inline u64
clib_byte_swap_u64 (u64 x)
{
#if defined (__x86_64__)
  if (!__builtin_constant_p (x))
    {
      asm volatile ("bswapq %0":"=r" (x):"0" (x));
      return x;
    }
#elif defined (__aarch64__)
  if (!__builtin_constant_p (x))
    {
    __asm__ ("rev %0, %0":"+r" (x));
      return x;
    }
#endif
#define _(x,n,i) \
  ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1)))
  return (_(x, 8, 0) | _(x, 8, 1)
	  | _(x, 8, 2) | _(x, 8, 3)
	  | _(x, 8, 4) | _(x, 8, 5) | _(x, 8, 6) | _(x, 8, 7));
#undef _
}

always_inline i64
clib_byte_swap_i64 (i64 x)
{
  return clib_byte_swap_u64 (x);
}

#define _(sex,type)						\
/* HOST -> SEX */						\
always_inline type						\
clib_host_to_##sex##_##type (type x)				\
{								\
  if (! clib_arch_is_##sex##_endian)				\
    x = clib_byte_swap_##type (x);				\
  return x;							\
}								\
								\
always_inline type						\
clib_host_to_##sex##_mem_##type (type * x)			\
{								\
  type v = x[0];						\
  return clib_host_to_##sex##_##type (v);			\
}								\
								\
always_inline type						\
clib_host_to_##sex##_unaligned_mem_##type (type * x)		\
{								\
  type v = clib_mem_unaligned (x, type);			\
  return clib_host_to_##sex##_##type (v);			\
}								\
								\
/* SEX -> HOST */						\
always_inline type						\
clib_##sex##_to_host_##type (type x)				\
{ return clib_host_to_##sex##_##type (x); }			\
								\
always_inline type						\
clib_##sex##_to_host_mem_##type (type * x)			\
{ return clib_host_to_##sex##_mem_##type (x); }			\
								\
always_inline type						\
clib_##sex##_to_host_unaligned_mem_##type (type * x)		\
{ return clib_host_to_##sex##_unaligned_mem_##type (x); }

#ifndef __cplusplus
_(little, u16)
_(little, u32)
_(little, u64)
_(little, i16)
_(little, i32)
_(little, i64)
_(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
#endif
#undef _
/* Network "net" alias for "big". */
#define _(type)						\
always_inline type					\
clib_net_to_host_##type (type x)			\
{ return clib_big_to_host_##type (x); }			\
							\
always_inline type					\
clib_net_to_host_mem_##type (type * x)			\
{ return clib_big_to_host_mem_##type (x); }		\
							\
always_inline type					\
clib_net_to_host_unaligned_mem_##type (type * x)	\
{ return clib_big_to_host_unaligned_mem_##type (x); }	\
							\
always_inline type					\
clib_host_to_net_##type (type x)			\
{ return clib_host_to_big_##type (x); }			\
							\
always_inline type					\
clib_host_to_net_mem_##type (type * x)			\
{ return clib_host_to_big_mem_##type (x); }		\
							\
always_inline type					\
clib_host_to_net_unaligned_mem_##type (type * x)	\
{ return clib_host_to_big_unaligned_mem_##type (x); }
#ifndef __cplusplus
  _(u16);
_(i16);
_(u32);
_(i32);
_(u64);
_(i64);
#endif

#undef _

/* Dummy endian swap functions for IEEE floating-point numbers */
/* *INDENT-OFF* */
always_inline f64 clib_net_to_host_f64 (f64 x) { return x; }
always_inline f64 clib_host_to_net_f64 (f64 x) { return x; }
always_inline f32 clib_net_to_host_f32 (f32 x) { return x; }
always_inline f32 clib_host_to_net_f32 (f32 x) { return x; }
/* *INDENT-ON* */

#endif /* included_clib_byte_order_h */

/*