/* * 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.c: VNET interfaces/sub-interfaces * * 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 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0) #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1) static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index, u32 flags, u32 helper_flags); static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index, u32 flags, u32 helper_flags); static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index, u32 redistribute); typedef struct { /* Either sw or hw interface index. */ u32 sw_hw_if_index; /* Flags. */ u32 flags; } vnet_sw_hw_interface_state_t; static void serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va) { vnet_sw_hw_interface_state_t *s = va_arg (*va, vnet_sw_hw_interface_state_t *); u32 n = va_arg (*va, u32); u32 i; for (i = 0; i < n; i++) { serialize_integer (m, s[i].sw_hw_if_index, sizeof (s[i].sw_hw_if_index)); serialize_integer (m, s[i].flags, sizeof (s[i].flags)); } } static void unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va) { vnet_sw_hw_interface_state_t *s = va_arg (*va, vnet_sw_hw_interface_state_t *); u32 n = va_arg (*va, u32); u32 i; for (i = 0; i < n; i++) { unserialize_integer (m, &s[i].sw_hw_if_index, sizeof (s[i].sw_hw_if_index)); unserialize_integer (m, &s[i].flags, sizeof (s[i].flags)); } } void serialize_vnet_interface_state (serialize_main_t * m, va_list * va) { vnet_main_t *vnm = va_arg (*va, vnet_main_t *); vnet_sw_hw_interface_state_t *sts = 0, *st; vnet_sw_interface_t *sif; vnet_hw_interface_t *hif; vnet_interface_main_t *im = &vnm->interface_main; /* Serialize hardware interface classes since they may have changed. Must do this before sending up/down flags. */ /* *INDENT-OFF* */ pool_foreach (hif, im->hw_interfaces, ({ vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index); serialize_cstring (m, hw_class->name); })); /* *INDENT-ON* */ /* Send sw/hw interface state when non-zero. */ /* *INDENT-OFF* */ pool_foreach (sif, im->sw_interfaces, ({ if (sif->flags != 0) { vec_add2 (sts, st, 1); st->sw_hw_if_index = sif->sw_if_index; st->flags = sif->flags; } })); /* *INDENT-ON* */ vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state); if (sts) _vec_len (sts) = 0; /* *INDENT-OFF* */ pool_foreach (hif, im->hw_interfaces, ({ if (hif->flags != 0) { vec_add2 (sts, st, 1); st->sw_hw_if_index = hif->hw_if_index; st->flags = hif->flags; } })); /* *INDENT-ON* */ vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state); vec_free (sts); } void unserialize_vnet_interface_state (serialize_main_t * m, va_list * va) { vnet_main_t *vnm = va_arg (*va, vnet_main_t *); vnet_sw_hw_interface_state_t *sts = 0, *st; /* First set interface hardware class. */ { vnet_interface_main_t *im = &vnm->interface_main; vnet_hw_interface_t *hif; char *class_name; uword *p; clib_error_t *error; /* *INDENT-OFF* */ pool_foreach (hif, im->hw_interfaces, ({ unserialize_cstring (m, &class_name); p = hash_get_mem (im->hw_interface_class_by_name, class_name); ASSERT (p != 0); error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0); if (error) clib_error_report (error); vec_free (class_name); })); /* *INDENT-ON* */ } vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state); vec_foreach (st, sts) vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags, /* no distribute */ 0); vec_free (sts); vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state); vec_foreach (st, sts) vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags, /* no distribute */ 0); vec_free (sts); } static clib_error_t * call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index, u32 flags, _vnet_interface_function_list_elt_t ** elts) { _vnet_interface_function_list_elt_t *elt; vnet_interface_function_priority_t prio; clib_error_t *error = 0; for (prio = VNET_ITF_FUNC_PRIORITY_LOW; prio <= VNET_ITF_FUNC_PRIORITY_HIGH; prio++) { elt = elts[prio]; while (elt) { error = elt->fp (vnm, if_index, flags); if (error) return error; elt = elt->next_interface_function; } } return error; } static clib_error_t * call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index, u32 is_create) { vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); vnet_hw_interface_class_t *hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index); vnet_device_class_t *dev_class = vnet_get_device_class (vnm, hi->dev_class_index); clib_error_t *error = 0; if (hw_class->interface_add_del_function && (error = hw_class->interface_add_del_function (vnm, hw_if_index, is_create))) return error; if (dev_class->interface_add_del_function && (error = dev_class->interface_add_del_function (vnm, hw_if_index, is_create))) return error; error = call_elf_section_interface_callbacks (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions); return error; } static clib_error_t * call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index, u32 is_create) { return call_elf_section_interface_callbacks (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions); } #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0) #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1) static clib_error_t * vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index, u32 flags, u32 helper_flags) { vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); vnet_hw_interface_class_t *hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index); u32 mask; clib_error_t *error = 0; u32 is_create = (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0; mask = (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK | VNET_HW_INTERFACE_FLAG_SPEED_MASK); flags &= mask; /* Call hardware interface add/del callbacks. */ if (is_create) call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create); /* Already in the desired state? */ if (!is_create && (hi->flags & mask) == flags) goto done; if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) != (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)) { /* Do hardware class (e.g. ethernet). */ if (hw_class->link_up_down_function && (error = hw_class->link_up_down_function (vnm, hw_if_index, flags))) goto done; error = call_elf_section_interface_callbacks (vnm, hw_if_index, flags, vnm->hw_interface_link_up_down_functions); if (error) goto done; } hi->flags &= ~mask; hi->flags |= flags; done: return error; } static clib_error_t * vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index, u32 flags, u32 helper_flags) { vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); u32 mask; clib_error_t *error = 0; u32 is_create = (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0; u32 old_flags; mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT; flags &= mask; if (is_create) { error = call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create); if (error) goto done; if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) { /* Notify everyone when the interface is created as admin up */ error = call_elf_section_interface_callbacks (vnm, sw_if_index, flags, vnm-> sw_interface_admin_up_down_functions); if (error) goto done; } } else { vnet_sw_interface_t *si_sup = si; /* Check that super interface is in correct state. */ if (si->type == VNET_SW_INTERFACE_TYPE_SUB) { si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index); /* Check to see if we're bringing down the soft interface and if it's parent is up */ if ((flags != (si_sup->flags & mask)) && (!((flags == 0) && ((si_sup->flags & mask) == VNET_SW_INTERFACE_FLAG_ADMIN_UP)))) { error = clib_error_return (0, "super-interface %U must be %U", format_vnet_sw_interface_name, vnm, si_sup, format_vnet_sw_interface_flags, flags); goto done; } } /* Do not change state for slave link of bonded interfaces */ if (si->flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE) { error = clib_error_return (0, "not allowed as %U belong to a BondEthernet interface", format_vnet_sw_interface_name, vnm, si); goto done; } /* Already in the desired state? */ if ((si->flags & mask) == flags) goto done; /* Sub-interfaces of hardware interfaces that do no redistribute, do not redistribute themselves. */ if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE) { vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si_sup->hw_if_index); vnet_device_class_t *dev_class = vnet_get_device_class (vnm, hi->dev_class_index); if (!dev_class->redistribute) helper_flags &= ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE; } /* set the flags now before invoking the registered clients * so that the state they query is consistent with the state here notified */ old_flags = si->flags; si->flags &= ~mask; si->flags |= flags; if ((flags | old_flags) & VNET_SW_INTERFACE_FLAG_ADMIN_UP) error = call_elf_section_interface_callbacks (vnm, sw_if_index, flags, vnm->sw_interface_admin_up_down_functions); if (error) { /* restore flags on error */ si->flags = old_flags; goto done; } if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE) { vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index); vnet_hw_interface_class_t *hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index); vnet_device_class_t *dev_class = vnet_get_device_class (vnm, hi->dev_class_index); if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) && (si->flags & VNET_SW_INTERFACE_FLAG_ERROR)) { error = clib_error_return (0, "Interface in the error state"); goto done; } /* save the si admin up flag */ old_flags = si->flags; /* update si admin up flag in advance if we are going admin down */ if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP; if (dev_class->admin_up_down_function && (error = dev_class->admin_up_down_function (vnm, si->hw_if_index, flags))) { /* restore si admin up flag to it's original state on errors */ si->flags = old_flags; goto done; } if (hw_class->admin_up_down_function && (error = hw_class->admin_up_down_function (vnm, si->hw_if_index, flags))) { /* restore si admin up flag to it's original state on errors */ si->flags = old_flags; goto done; } /* Admin down implies link down. */ if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)) vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index, hi->flags & ~VNET_HW_INTERFACE_FLAG_LINK_UP, helper_flags); } } si->flags &= ~mask; si->flags |= flags; done: return error; } clib_error_t * vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags) { return vnet_hw_interface_set_flags_helper (vnm, hw_if_index, flags, VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE); } clib_error_t * vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags) { return vnet_sw_interface_set_flags_helper (vnm, sw_if_index, flags, VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE); } static u32 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm, vnet_sw_interface_t * template) { vnet_interface_main_t *im = &vnm->interface_main; vnet_sw_interface_t *sw; u32 sw_if_index; pool_get (im->sw_interfaces, sw); sw_if_index = sw - im->sw_interfaces; sw[0] = template[0]; sw->flags = 0; sw->sw_if_index = sw_if_index; if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE) sw->sup_sw_if_index = sw->sw_if_index; /* Allocate counters for this interface. */ { u32 i; vnet_interface_counter_lock (im); for (i = 0; i < vec_len (im->sw_if_counters); i++) { vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index); vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index); } for (i = 0; i < vec_len (im->combined_sw_if_counters); i++) { vlib_validate_combined_counter (&im->combined_sw_if_counters[i], sw_if_index); vlib_zero_combined_counter (&im->combined_sw_if_counters[i], sw_if_index); } vnet_interface_counter_unlock (im); } return sw_if_index; } clib_error_t * vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template, u32 * sw_if_index) { clib_error_t *error; vnet_hw_interface_t *hi; vnet_device_class_t *dev_class; hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index); dev_class = vnet_get_device_class (vnm, hi->dev_class_index); if (template->type == VNET_SW_INTERFACE_TYPE_SUB && dev_class->subif_add_del_function) { error = dev_class->subif_add_del_function (vnm, hi->hw_if_index, (struct vnet_sw_interface_t *) template, 1); if (error) return error; } *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template); error = vnet_sw
/*
 * 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) 2001, 2002, 2003 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 <vppinfra/format.h>
#include <ctype.h>

/* Format vectors. */
u8 *
format_vec32 (u8 * s, va_list * va)
{
  u32 *v = va_arg (*va, u32 *);
  char *fmt = va_arg (*va, char *);
  uword i;
  for (i = 0; i < vec_len (v); i++)
    {
      if (i > 0)
	s = format (s, ", ");
      s = format (s, fmt, v[i]);
    }
  return s;
}

u8 *
format_vec_uword (u8 * s, va_list * va)
{
  uword *v = va_arg (*va, uword *);
  char *fmt = va_arg (*va, char *);
  uword i;
  for (i = 0; i < vec_len (v); i++)
    {
      if (i > 0)
	s = format (s, ", ");
      s = format (s, fmt, v[i]);
    }
  return s;
}

/* Ascii buffer and length. */
u8 *
format_ascii_bytes (u8 * s, va_list * va)
{
  u8 *v = va_arg (*va, u8 *);
  uword n_bytes = va_arg (*va, uword);
  vec_add (s, v, n_bytes);
  return s;
}

/* Format hex dump. */
u8 *
format_hex_bytes (u8 * s, va_list * va)
{
  u8 *bytes = va_arg (*va, u8 *);
  int n_bytes = va_arg (*va, int);
  uword i;

  /* Print short or long form depending on byte count. */
  uword short_form = n_bytes <= 32;
  u32 indent = format_get_indent (s);

  if (n_bytes == 0)
    return s;

  for (i = 0; i < n_bytes; i++)
    {
      if (!short_form && (i % 32) == 0)
	s = format (s, "%08x: ", i);

      s = format (s, "%02x", bytes[i]);

      if (!short_form && ((i + 1) % 32) == 0 && (i + 1) < n_bytes)
	s = format (s, "\n%U", format_white_space, indent);
    }

  return s;
}

u8 *
format_hex_bytes_no_wrap (u8 * s, va_list * va)
{
  u8 *bytes = va_arg (*va, u8 *);
  int n_bytes = va_arg (*va, int);
  uword i;

  if (n_bytes == 0)
    return s;

  for (i = 0; i < n_bytes; i++)
    s = format (s, "%02x", bytes[i]);

  return s;
}

/* Add variable number of spaces. */
u8 *
format_white_space (u8 * s, va_list * va)
{
  u32 n = va_arg (*va, u32);
  while (n-- > 0)
    vec_add1 (s, ' ');
  return s;
}

u8 *
format_time_interval (u8 * s, va_list * args)
{
  u8 *fmt = va_arg (*args, u8 *);
  f64 t = va_arg (*args, f64);
  u8 *f;

  const f64 seconds_per_minute = 60;
  const f64 seconds_per_hour = 60 * seconds_per_minute;
  const f64 seconds_per_day = 24 * seconds_per_hour;
  uword days, hours, minutes, secs, msecs, usecs;

  days = t / seconds_per_day;
  t -= days * seconds_per_day;

  hours = t / seconds_per_hour;
  t -= hours * seconds_per_hour;

  minutes = t / seconds_per_minute;
  t -= minutes * seconds_per_minute;

  secs = t;
  t -= secs;

  msecs = 1e3 * t;
  usecs = 1e6 * t;

  for (f = fmt; *f; f++)
    {
      uword what, c;
      char *what_fmt = "%d";

      switch (c = *f)
	{
	default:
	  vec_add1 (s, c);
	  continue;

	case 'd':
	  what = days;
	  what_fmt = "%d";
	  break;
	case 'h':
	  what = hours;
	  what_fmt = "%02d";
	  break;
	case 'm':
	  what = minutes;
	  what_fmt = "%02d";
	  break;
	case 's':
	  what = secs</