aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ioam/ip6
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2018-11-13 16:34:13 -0500
committerFlorin Coras <florin.coras@gmail.com>2018-11-14 15:54:01 +0000
commit178cf493d009995b28fdf220f04c98860ff79a9b (patch)
tree097c1be82b8f6fa9bc04b9b1e193158e2e4997eb /src/plugins/ioam/ip6
parent6917b94f2146aa51195a6a2a1ccd8416a1d74bf3 (diff)
Remove c-11 memcpy checks from perf-critical code
Change-Id: Id4f37f5d4a03160572954a416efa1ef9b3d79ad1 Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'src/plugins/ioam/ip6')
-rw-r--r--src/plugins/ioam/ip6/ioam_cache.h18
-rw-r--r--src/plugins/ioam/ip6/ioam_cache_node.c18
-rw-r--r--src/plugins/ioam/ip6/ioam_cache_tunnel_select_node.c6
3 files changed, 21 insertions, 21 deletions
diff --git a/src/plugins/ioam/ip6/ioam_cache.h b/src/plugins/ioam/ip6/ioam_cache.h
index 717aaf11b8f..e75deba73f7 100644
--- a/src/plugins/ioam/ip6/ioam_cache.h
+++ b/src/plugins/ioam/ip6/ioam_cache.h
@@ -374,10 +374,10 @@ ioam_cache_add (vlib_buffer_t * b0,
clib_memset (entry, 0, sizeof (*entry));
pool_index = entry - cm->ioam_rewrite_pool;
- clib_memcpy (entry->dst_address.as_u64, ip0->dst_address.as_u64,
- sizeof (ip6_address_t));
- clib_memcpy (entry->src_address.as_u64, ip0->src_address.as_u64,
- sizeof (ip6_address_t));
+ clib_memcpy_fast (entry->dst_address.as_u64, ip0->dst_address.as_u64,
+ sizeof (ip6_address_t));
+ clib_memcpy_fast (entry->src_address.as_u64, ip0->src_address.as_u64,
+ sizeof (ip6_address_t));
entry->src_port = src_port;
entry->dst_port = dst_port;
entry->seq_no = seq_no;
@@ -395,7 +395,7 @@ ioam_cache_add (vlib_buffer_t * b0,
}
e2e_id_offset = (u8 *) e2e - (u8 *) hbh0;
/* setup e2e id option to insert v6 address of the node caching it */
- clib_memcpy (entry->ioam_rewrite_string, hbh0, rewrite_len);
+ clib_memcpy_fast (entry->ioam_rewrite_string, hbh0, rewrite_len);
hbh0 = (ip6_hop_by_hop_header_t *) entry->ioam_rewrite_string;
/* suffix rewrite string with e2e ID option */
@@ -657,10 +657,10 @@ ioam_cache_ts_add (ip6_header_t * ip0,
clib_memset (entry, 0, sizeof (*entry));
*pool_index = entry - cm->ioam_ts_pool[thread_id];
- clib_memcpy (entry->dst_address.as_u64, ip0->dst_address.as_u64,
- sizeof (ip6_address_t));
- clib_memcpy (entry->src_address.as_u64, ip0->src_address.as_u64,
- sizeof (ip6_address_t));
+ clib_memcpy_fast (entry->dst_address.as_u64, ip0->dst_address.as_u64,
+ sizeof (ip6_address_t));
+ clib_memcpy_fast (entry->src_address.as_u64, ip0->src_address.as_u64,
+ sizeof (ip6_address_t));
entry->src_port = src_port;
entry->dst_port = dst_port;
entry->seq_no = seq_no;
diff --git a/src/plugins/ioam/ip6/ioam_cache_node.c b/src/plugins/ioam/ip6/ioam_cache_node.c
index dd27e127a09..59bc1a64811 100644
--- a/src/plugins/ioam/ip6/ioam_cache_node.c
+++ b/src/plugins/ioam/ip6/ioam_cache_node.c
@@ -344,17 +344,17 @@ ip6_add_from_cache_hbh_node_fn (vlib_main_t * vm,
hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
srh0 = (ip6_sr_header_t *) ((u8 *) hbh0 + rewrite_len);
/* $$$ tune, rewrite_len is a multiple of 8 */
- clib_memcpy (hbh0, rewrite, rewrite_len);
- clib_memcpy (srh0, cm->sr_rewrite_template, sr_rewrite_len);
+ clib_memcpy_fast (hbh0, rewrite, rewrite_len);
+ clib_memcpy_fast (srh0, cm->sr_rewrite_template, sr_rewrite_len);
/* Copy dst address into the DA slot in the segment list */
- clib_memcpy (srh0->segments, ip0->dst_address.as_u64,
- sizeof (ip6_address_t));
+ clib_memcpy_fast (srh0->segments, ip0->dst_address.as_u64,
+ sizeof (ip6_address_t));
/* Rewrite the ip6 dst address with the first hop */
- clib_memcpy (ip0->dst_address.as_u64, entry->next_hop.as_u64,
- sizeof (ip6_address_t));
- clib_memcpy (&srh0->segments[1],
- (u8 *) hbh0 + entry->my_address_offset,
- sizeof (ip6_address_t));
+ clib_memcpy_fast (ip0->dst_address.as_u64, entry->next_hop.as_u64,
+ sizeof (ip6_address_t));
+ clib_memcpy_fast (&srh0->segments[1],
+ (u8 *) hbh0 + entry->my_address_offset,
+ sizeof (ip6_address_t));
ioam_cache_entry_free (entry);
/* Patch the protocol chain, insert the h-b-h (type 0) header */
diff --git a/src/plugins/ioam/ip6/ioam_cache_tunnel_select_node.c b/src/plugins/ioam/ip6/ioam_cache_tunnel_select_node.c
index 79ee58eccda..63bcaff14b2 100644
--- a/src/plugins/ioam/ip6/ioam_cache_tunnel_select_node.c
+++ b/src/plugins/ioam/ip6/ioam_cache_tunnel_select_node.c
@@ -415,7 +415,7 @@ ip6_reset_ts_hbh_node_fn (vlib_main_t * vm,
hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
/* $$$ tune, rewrite_length is a multiple of 8 */
- clib_memcpy (hbh0, rewrite, rewrite_length);
+ clib_memcpy_fast (hbh0, rewrite, rewrite_length);
e2e =
(ioam_e2e_cache_option_t *) ((u8 *) hbh0 +
cm->rewrite_pool_index_offset);
@@ -475,7 +475,7 @@ ip6_reset_ts_hbh_node_fn (vlib_main_t * vm,
hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
/* $$$ tune, rewrite_length is a multiple of 8 */
- clib_memcpy (hbh1, rewrite, rewrite_length);
+ clib_memcpy_fast (hbh1, rewrite, rewrite_length);
e2e =
(ioam_e2e_cache_option_t *) ((u8 *) hbh1 +
cm->rewrite_pool_index_offset);
@@ -581,7 +581,7 @@ ip6_reset_ts_hbh_node_fn (vlib_main_t * vm,
hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
/* $$$ tune, rewrite_length is a multiple of 8 */
- clib_memcpy (hbh0, rewrite, rewrite_length);
+ clib_memcpy_fast (hbh0, rewrite, rewrite_length);
e2e =
(ioam_e2e_cache_option_t *) ((u8 *) hbh0 +
cm->rewrite_pool_index_offset);
f='#n412'>412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/* Hey Emacs use -*- mode: C -*- */
/*
 * Copyright 2021 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 <vnet/vnet.h>
#include <vnet/plugin/plugin.h>
#include <vnet/devices/netlink.h>
#include <vnet/ip/ip.h>
#include <vppinfra/linux/netns.h>
#include <plugins/linux-cp/lcp_interface.h>

/* helper function to copy forward all sw interface link state flags
 * MTU, and IP addresses into their counterpart LIP interface.
 *
 * This is called upon MTU changes and state changes.
 */
void
lcp_itf_pair_sync_state (lcp_itf_pair_t *lip)
{
  vnet_sw_interface_t *sw;
  vnet_sw_interface_t *sup_sw;
  int curr_ns_fd = -1;
  int vif_ns_fd = -1;
  u32 mtu;
  u32 netlink_mtu;

  if (!lcp_sync ())
    return;

  sw =
    vnet_get_sw_interface_or_null (vnet_get_main (), lip->lip_phy_sw_if_index);
  if (!sw)
    return;
  sup_sw =
    vnet_get_sw_interface_or_null (vnet_get_main (), sw->sup_sw_if_index);
  if (!sup_sw)
    return;

  if (lip->lip_namespace)
    {
      curr_ns_fd = clib_netns_open (NULL /* self */);
      vif_ns_fd = clib_netns_open (lip->lip_namespace);
      if (vif_ns_fd != -1)
	clib_setns (vif_ns_fd);
    }

  LCP_ITF_PAIR_INFO ("sync_state: %U flags %u sup-flags %u mtu %u sup-mtu %u",
		     format_lcp_itf_pair, lip, sw->flags, sup_sw->flags,
		     sw->mtu[VNET_MTU_L3], sup_sw->mtu[VNET_MTU_L3]);

  /* Linux will not allow children to be admin-up if their parent is
   * admin-down. If child is up but parent is not, force it down.
   */
  int state = sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;

  if (state && !(sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
    {
      LCP_ITF_PAIR_WARN (
	"sync_state: %U flags %u sup-flags %u mtu %u sup-mtu %u: "
	"forcing state to sup-flags to satisfy netlink",
	format_lcp_itf_pair, lip, sw->flags, sup_sw->flags,
	sw->mtu[VNET_MTU_L3], sup_sw->mtu[VNET_MTU_L3]);
      state = 0;
    }
  lcp_itf_set_link_state (lip, state);

  /* Linux will clamp MTU of children when the parent is lower. VPP is fine
   * with differing MTUs. VPP assumes that if a subint has MTU of 0, that it
   * inherits from its parent. Linux likes to be more explicit, so we
   * reconcile any differences.
   */
  mtu = sw->mtu[VNET_MTU_L3];
  if (mtu == 0)
    mtu = sup_sw->mtu[VNET_MTU_L3];

  if (sup_sw->mtu[VNET_MTU_L3] < sw->mtu[VNET_MTU_L3])
    {
      LCP_ITF_PAIR_WARN ("sync_state: %U flags %u mtu %u sup-mtu %u: "
			 "clamping to sup-mtu to satisfy netlink",
			 format_lcp_itf_pair, lip, sw->flags,
			 sw->mtu[VNET_MTU_L3], sup_sw->mtu[VNET_MTU_L3]);
      mtu = sup_sw->mtu[VNET_MTU_L3];
    }

  /* Set MTU on all of {sw, tap, netlink}. Only send a netlink message if we
   * really do want to change the MTU.
   */
  vnet_sw_interface_set_mtu (vnet_get_main (), lip->lip_phy_sw_if_index, mtu);
  vnet_sw_interface_set_mtu (vnet_get_main (), lip->lip_host_sw_if_index, mtu);
  if (NULL == vnet_netlink_get_link_mtu (lip->lip_vif_index, &netlink_mtu))
    {
      if (netlink_mtu != mtu)
	vnet_netlink_set_link_mtu (lip->lip_vif_index, mtu);
    }

  /* Linux will remove IPv6 addresses on children when the parent state
   * goes down, so we ensure all IPv4/IPv6 addresses are synced.
   */
  lcp_itf_set_interface_addr (lip);

  if (vif_ns_fd != -1)
    close (vif_ns_fd);

  if (curr_ns_fd != -1)
    {
      clib_setns (curr_ns_fd);
      close (curr_ns_fd);
    }

  return;
}

static walk_rc_t
lcp_itf_pair_walk_sync_state_all_cb (index_t lipi, void *ctx)
{
  lcp_itf_pair_t *lip;
  lip = lcp_itf_pair_get (lipi);
  if (!lip)
    return WALK_CONTINUE;

  lcp_itf_pair_sync_state (lip);
  return WALK_CONTINUE;
}

static walk_rc_t
lcp_itf_pair_walk_sync_state_hw_cb (vnet_main_t *vnm, u32 sw_if_index,
				    void *arg)
{
  lcp_itf_pair_t *lip;

  lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw_if_index));
  if (!lip)
    {
      return WALK_CONTINUE;
    }

  lcp_itf_pair_sync_state (lip);
  return WALK_CONTINUE;
}

void
lcp_itf_pair_sync_state_all ()
{
  lcp_itf_pair_walk (lcp_itf_pair_walk_sync_state_all_cb, 0);
}

void
lcp_itf_pair_sync_state_hw (vnet_hw_interface_t *hi)
{
  if (!hi)
    return;
  LCP_ITF_PAIR_DBG ("sync_state_hw: hi %U", format_vnet_sw_if_index_name,
		    vnet_get_main (), hi->hw_if_index);

  vnet_hw_interface_walk_sw (vnet_get_main (), hi->hw_if_index,
			     lcp_itf_pair_walk_sync_state_hw_cb, NULL);
}

static clib_error_t *
lcp_itf_admin_state_change (vnet_main_t *vnm, u32 sw_if_index, u32 flags)
{
  lcp_itf_pair_t *lip;
  vnet_hw_interface_t *hi;
  vnet_sw_interface_t *si;

  if (!lcp_sync ())
    return 0;

  LCP_ITF_PAIR_DBG ("admin_state_change: sw %U %u",
		    format_vnet_sw_if_index_name, vnm, sw_if_index, flags);

  // Sync interface state changes into host
  lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw_if_index));
  if (!lip)
    return NULL;
  LCP_ITF_PAIR_INFO ("admin_state_change: %U flags %u", format_lcp_itf_pair,
		     lip, flags);

  if (vnet_sw_interface_is_sub (vnm, sw_if_index))
    {
      lcp_itf_pair_sync_state (lip);
      return NULL;
    }

  // When Linux changes link on a parent interface, all of its children also
  // change. If a parent interface changes MTU, all of its children are clamped
  // at that MTU by Linux. Neither holds true in VPP, so we are forced to undo
  // change by walking the sub-interfaces of a phy and syncing their state back
  // into Linux.
  si = vnet_get_sw_interface_or_null (vnm, sw_if_index);
  if (!si)
    return NULL;

  hi = vnet_get_hw_interface_or_null (vnm, si->hw_if_index);
  if (!hi)
    return NULL;
  LCP_ITF_PAIR_DBG ("admin_state_change: si %U hi %U, syncing children",
		    format_vnet_sw_if_index_name, vnm, si->sw_if_index,
		    format_vnet_sw_if_index_name, vnm, hi->sw_if_index);

  lcp_itf_pair_sync_state_hw (hi);

  return NULL;
}

VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lcp_itf_admin_state_change);

static clib_error_t *
lcp_itf_mtu_change (vnet_main_t *vnm, u32 sw_if_index, u32 flags)
{
  vnet_sw_interface_t *si;
  vnet_hw_interface_t *hi;
  if (!lcp_sync ())
    return NULL;

  LCP_ITF_PAIR_DBG ("mtu_change: sw %U %u", format_vnet_sw_if_index_name, vnm,
		    sw_if_index, flags);

  if (vnet_sw_interface_is_sub (vnm, sw_if_index))
    {
      lcp_itf_pair_t *lip;
      lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw_if_index));
      if (lip)
	lcp_itf_pair_sync_state (lip);
      return NULL;
    }

  // When Linux changes link on a parent interface, all of its children also
  // change. If a parent interface changes MTU, all of its children are clamped
  // at that MTU by Linux. Neither holds true in VPP, so we are forced to undo
  // change by walking the sub-interfaces of a phy and syncing their state back
  // into Linux.
  si = vnet_get_sw_interface_or_null (vnm, sw_if_index);
  if (!si)
    return NULL;

  hi = vnet_get_hw_interface_or_null (vnm, si->hw_if_index);
  if (!hi)
    return NULL;
  LCP_ITF_PAIR_DBG ("mtu_change: si %U hi %U, syncing children",
		    format_vnet_sw_if_index_name, vnm, si->sw_if_index,
		    format_vnet_sw_if_index_name, vnm, hi->sw_if_index);

  lcp_itf_pair_sync_state_hw (hi);

  return NULL;
}

VNET_SW_INTERFACE_MTU_CHANGE_FUNCTION (lcp_itf_mtu_change);

static void
lcp_itf_ip4_add_del_interface_addr (ip4_main_t *im, uword opaque,
				    u32 sw_if_index, ip4_address_t *address,
				    u32 address_length, u32 if_address_index,
				    u32 is_del)
{
  const lcp_itf_pair_t *lip;
  int curr_ns_fd = -1;
  int vif_ns_fd = -1;

  if (!lcp_sync ())
    return;

  LCP_ITF_PAIR_DBG ("ip4_addr_%s: si:%U %U/%u", is_del ? "del" : "add",
		    format_vnet_sw_if_index_name, vnet_get_main (),
		    sw_if_index, format_ip4_address, address, address_length);

  lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw_if_index));
  if (!lip)
    return;

  if (lip->lip_namespace)
    {
      curr_ns_fd = clib_netns_open (NULL /* self */);
      vif_ns_fd = clib_netns_open (lip->lip_namespace);
      if (vif_ns_fd != -1)
	clib_setns (vif_ns_fd);
    }

  LCP_ITF_PAIR_DBG ("ip4_addr_%s: %U ip4 %U/%u", is_del ? "del" : "add",
		    format_lcp_itf_pair, lip, format_ip4_address, address,
		    address_length);

  if (is_del)
    vnet_netlink_del_ip4_addr (lip->lip_vif_index, address, address_length);
  else
    vnet_netlink_add_ip4_addr (lip->lip_vif_index, address, address_length);

  if (vif_ns_fd != -1)
    close (vif_ns_fd);

  if (curr_ns_fd != -1)
    {
      clib_setns (curr_ns_fd);
      close (curr_ns_fd);
    }
  return;
}

static void
lcp_itf_ip6_add_del_interface_addr (ip6_main_t *im, uword opaque,
				    u32 sw_if_index, ip6_address_t *address,
				    u32 address_length, u32 if_address_index,
				    u32 is_del)
{
  const lcp_itf_pair_t *lip;
  int curr_ns_fd = -1;
  int vif_ns_fd = -1;

  if (!lcp_sync ())
    return;

  LCP_ITF_PAIR_DBG ("ip6_addr_%s: si:%U %U/%u", is_del ? "del" : "add",
		    format_vnet_sw_if_index_name, vnet_get_main (),
		    sw_if_index, format_ip6_address, address, address_length);

  lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw_if_index));
  if (!lip)
    return;

  if (lip->lip_namespace)
    {
      curr_ns_fd = clib_netns_open (NULL /* self */);
      vif_ns_fd = clib_netns_open (lip->lip_namespace);
      if (vif_ns_fd != -1)
	clib_setns (vif_ns_fd);
    }
  LCP_ITF_PAIR_DBG ("ip6_addr_%s: %U ip4 %U/%u", is_del ? "del" : "add",
		    format_lcp_itf_pair, lip, format_ip6_address, address,
		    address_length);
  if (is_del)
    vnet_netlink_del_ip6_addr (lip->lip_vif_index, address, address_length);
  else
    vnet_netlink_add_ip6_addr (lip->lip_vif_index, address, address_length);

  if (vif_ns_fd != -1)
    close (vif_ns_fd);

  if (curr_ns_fd != -1)
    {
      clib_setns (curr_ns_fd);
      close (curr_ns_fd);
    }
}

static clib_error_t *
lcp_itf_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_create)
{
  const vnet_sw_interface_t *sw;
  uword is_sub;

  if (!lcp_auto_subint ())
    return NULL;

  sw = vnet_get_sw_interface_or_null (vnm, sw_if_index);
  if (!sw)
    return NULL;

  is_sub = vnet_sw_interface_is_sub (vnm, sw_if_index);
  if (!is_sub)
    return NULL;

  LCP_ITF_PAIR_DBG ("interface_%s: sw %U parent %U", is_create ? "add" : "del",
		    format_vnet_sw_if_index_name, vnet_get_main (),
		    sw->sw_if_index, format_vnet_sw_if_index_name,
		    vnet_get_main (), sw->sup_sw_if_index);

  if (is_create)
    {
      const lcp_itf_pair_t *sup_lip;
      u8 *name = 0;

      // If the parent has a LIP auto-create a LIP for this interface
      sup_lip =
	lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw->sup_sw_if_index));
      if (!sup_lip)
	return NULL;

      name = format (name, "%s.%d", sup_lip->lip_host_name, sw->sub.id);

      LCP_ITF_PAIR_INFO (
	"interface_%s: %U has parent %U, auto-creating LCP with host-if %s",
	is_create ? "add" : "del", format_vnet_sw_if_index_name,
	vnet_get_main (), sw->sw_if_index, format_lcp_itf_pair, sup_lip, name);

      lcp_itf_pair_create (sw->sw_if_index, name, LCP_ITF_HOST_TAP,
			   sup_lip->lip_namespace, NULL);

      vec_free (name);
    }
  else
    {
      lcp_itf_pair_delete (sw_if_index);
    }

  return NULL;
}

VNET_SW_INTERFACE_ADD_DEL_FUNCTION (lcp_itf_interface_add_del);

static clib_error_t *
lcp_itf_sync_init (vlib_main_t *vm)
{
  ip4_main_t *im4 = &ip4_main;
  ip6_main_t *im6 = &ip6_main;

  ip4_add_del_interface_address_callback_t cb4;
  ip6_add_del_interface_address_callback_t cb6;

  cb4.function = lcp_itf_ip4_add_del_interface_addr;
  cb4.function_opaque = 0;
  vec_add1 (im4->add_del_interface_address_callbacks, cb4);

  cb6.function = lcp_itf_ip6_add_del_interface_addr;
  cb6.function_opaque = 0;
  vec_add1 (im6->add_del_interface_address_callbacks, cb6);

  return NULL;
}

VLIB_INIT_FUNCTION (lcp_itf_sync_init) = {
  .runs_after = VLIB_INITS ("vnet_interface_init", "tcp_init", "udp_init"),
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */