/* * 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 static const char *const lookup_input_names[] = LOOKUP_INPUTS; static const char *const lookup_cast_names[] = LOOKUP_CASTS; /** * If a packet encounters a lookup DPO more than the many times * then we assume there is a loop in the forward graph and drop the packet */ #define MAX_LUKPS_PER_PACKET 4 /** * @brief Enumeration of the lookup subtypes */ typedef enum lookup_sub_type_t_ { LOOKUP_SUB_TYPE_SRC, LOOKUP_SUB_TYPE_DST, LOOKUP_SUB_TYPE_DST_MCAST, LOOKUP_SUB_TYPE_DST_TABLE_FROM_INTERFACE, } lookup_sub_type_t; #define LOOKUP_SUB_TYPE_NUM (LOOKUP_SUB_TYPE_DST_TABLE_FROM_INTERFACE+1) #define FOR_EACH_LOOKUP_SUB_TYPE(_st) \ for (_st = LOOKUP_SUB_TYPE_IP4_SRC; _st < LOOKUP_SUB_TYPE_NUM; _st++) /** * @brief pool of all MPLS Label DPOs */ lookup_dpo_t *lookup_dpo_pool; /** * @brief An array of registered DPO type values for the sub-types */ static dpo_type_t lookup_dpo_sub_types[LOOKUP_SUB_TYPE_NUM]; static lookup_dpo_t * lookup_dpo_alloc (void) { lookup_dpo_t *lkd; pool_get_aligned(lookup_dpo_pool, lkd, CLIB_CACHE_LINE_BYTES); return (lkd); } static index_t lookup_dpo_get_index (lookup_dpo_t *lkd) { return (lkd - lookup_dpo_pool); } static void lookup_dpo_add_or_lock_i (fib_node_index_t fib_index, dpo_proto_t proto, lookup_cast_t cast, lookup_input_t input, lookup_table_t table_config, dpo_id_t *dpo) { lookup_dpo_t *lkd; dpo_type_t type; lkd = lookup_dpo_alloc(); lkd->lkd_fib_index = fib_index; lkd->lkd_proto = proto; lkd->lkd_input = input; lkd->lkd_table = table_config; lkd->lkd_cast = cast; /* * use the input type to select the lookup sub-type */ type = 0; switch (input) { case LOOKUP_INPUT_SRC_ADDR: type = lookup_dpo_sub_types[LOOKUP_SUB_TYPE_SRC]; break; case LOOKUP_INPUT_DST_ADDR: switch (table_config) { case LOOKUP_TABLE_FROM_INPUT_INTERFACE: type = lookup_dpo_sub_types[LOOKUP_SUB_TYPE_DST_TABLE_FROM_INTERFACE]; break; case LOOKUP_TABLE_FROM_CONFIG: type = lookup_dpo_sub_types[LOOKUP_SUB_TYPE_DST]; break; } if (LOOKUP_MULTICAST == cast) { type = lookup_dpo_sub_types[LOOKUP_SUB_TYPE_DST_MCAST]; } } if (0 == type) { dpo_reset(dpo); } else { dpo_set(dpo, type, proto, lookup_dpo_get_index(lkd)); } } void lookup_dpo_add_or_lock_w_fib_index (fib_node_index_t fib_index, dpo_proto_t proto, lookup_cast_t cast, lookup_input_t input, lookup_table_t table_config, dpo_id_t *dpo) { if (LOOKUP_TABLE_FROM_CONFIG == table_config) { if (LOOKUP_UNICAST == cast) { fib_table_lock(fib_index, dpo_proto_to_fib(proto), FIB_SOURCE_RR); } else { mfib_table_lock(fib_index, dpo_proto_to_fib(proto), MFIB_SOURCE_RR); } } lookup_dpo_add_or_lock_i(fib_index, proto, cast, input, table_config, dpo); } void lookup_dpo_add_or_lock_w_table_id (u32 table_id, dpo_proto_t proto, lookup_cast_t cast, lookup_input_t input, lookup_table_t table_config, dpo_id_t *dpo) { fib_node_index_t fib_index = FIB_NODE_INDEX_INVALID; if (LOOKUP_TABLE_FROM_CONFIG == table_config) { if (LOOKUP_UNICAST == cast) { fib_index = fib_table_find_or_create_and_lock(dpo_proto_to_fib(proto), table_id, FIB_SOURCE_RR); } else { fib_index = mfib_table_find_or_create_and_lock(dpo_proto_to_fib(proto), table_id, MFIB_SOURCE_RR); } } ASSERT(FIB_NODE_INDEX_INVALID != fib_index); lookup_dpo_add_or_lock_i(fib_index, proto, cast, input, table_config, dpo); } u8* format_lookup_dpo (u8 *s, va_list *args) { index_t index = va_arg (*args, index_t); lookup_dpo_t *lkd; lkd = lookup_dpo_get(index); if (LOOKUP_TABLE_FROM_INPUT_INTERFACE == lkd->lkd_table) { s = format(s, "%s,%s lookup in interface's %U table", lookup_input_names[lkd->lkd_input], lookup_cast_names[lkd->lkd_cast], format_dpo_proto, lkd->lkd_proto); } else { if (LOOKUP_UNICAST == lkd->lkd_cast) { s = format(s, "%s,%s lookup in %U", lookup_input_names[lkd->lkd_input], lookup_cast_names[lkd->lkd_cast], format_fib_table_name, lkd->lkd_fib_index, dpo_proto_to_fib(lkd->lkd_proto)); } else { s = format(s, "%s,%s lookup in %U", lookup_input_names[lkd->lkd_input], lookup_cast_names[lkd->lkd_cast], format_mfib_table_name, lkd->lkd_fib_index, dpo_proto_to_fib(lkd->lkd_proto)); } } return (s); } static void lookup_dpo_lock (dpo_id_t *dpo) { lookup_dpo_t *lkd; lkd = lookup_dpo_get(dpo->dpoi_index); lkd->lkd_locks++; } static void lookup_dpo_unlock (dpo_id_t *dpo) { lookup_dpo_t *lkd; lkd = lookup_dpo_get(dpo->dpoi_index); lkd->lkd_locks--; if (0 == lkd->lkd_locks) { if (LOOKUP_TABLE_FROM_CONFIG == lkd->lkd_table) { if (LOOKUP_UNICAST == lkd->lkd_cast) { fib_table_unlock(lkd->lkd_fib_index, dpo_proto_to_fib(lkd->lkd_proto), FIB_SOURCE_RR); } else { mfib_table_unlock(lkd->lkd_fib_index, dpo_proto_to_fib(lkd->lkd_proto), MFIB_SOURCE_RR); } } pool_put(lookup_dpo_pool, lkd); } } always_inline void ip4_src_fib_lookup_one (u32 src_fib_index0, const ip4_address_t * addr0, u32 * src_adj_index0) { ip4_fib_mtrie_leaf_t leaf0; ip4_fib_mtrie_t * mtrie0; mtrie0 = &ip4_fib_get (src_fib_index0)->mtrie; leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, addr0); leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2); leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3); src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0); } always_inline void ip4_src_fib_lookup_two (u32 src_fib_index0, u32 src_fib_index1, const ip4_address_t * addr0, const ip4_address_t * addr1, u32 * src_adj_index0, u32 * src_adj_index1) { ip4_fib_mtrie_leaf_t leaf0, leaf1; ip4_fib_mtrie_t * mtrie0, * mtrie1; mtrie0 = &ip4_fib_get (src_fib_index0)->mtrie; mtrie1 = &ip4_fib_get (src_fib_index1)->mtrie; leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, addr0); leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, addr1); leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2); leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 2); leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3); leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 3); src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0); src_adj_index1[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf1); } /** * @brief Lookup trace data */ typedef struct lookup_trace_t_ { union { ip46_address_t addr; mpls_unicast_header_t hdr; }; fib_node_index_t fib_index; index_t lbi; } lookup_trace_t; always_inline uword lookup_dpo_ip4_inline (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * from_frame, int input_src_addr, int table_from_interface) { u32 n_left_from, next_index, * from, * to_next; u32 thread_index = vlib_get_thread_index(); vlib_combined_counter_main_t * cm = &load_balance_main.lbm_to_counters; from = vlib_frame_vector_args (from_frame); n_left_from = from_frame->n_vectors; next_index = node->cached_next_index; while (n_left_from > 0) { u32 n_left_to_next; vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next); while (n_left_from >= 4 && n_left_to_next > 2) { u32 bi0, lkdi0, lbi0, fib_index0, next0, hash_c0; flow_hash_config_t flow_hash_config0; const ip4_address_t *input_addr0; const load_balance_t *lb0; const lookup_dpo_t * lkd0; const ip4_header_t * ip0; const dpo_id_t *dpo0; vlib_buffer_t * b0; u32 bi1, lkdi1, lbi1, fib_index1, next1, hash_c1; flow_hash_config_t flow_hash_config1; const ip4_address_t *input_addr1; const load_balance_t *lb1; const lookup_dpo_t * lkd1; const ip4_header_t * ip1; const dpo_id_t *dpo1; vlib_buffer_t * b1; /* Prefetch next iteration. */ { vlib_buffer_t * p2, * p3; p2 = vlib_get_buffer (vm, from[2]); p3 = vlib_get_buffer (vm, from[3]); vlib_prefetch_buffer_header (p2, LOAD); vlib_prefetch_buffer_header (p3, LOAD); CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); } bi0 = from[0]; to_next[0] = bi0; bi1 = from[1]; to_next[1] = bi1; from += 2; to_next += 2; n_left_from -= 2; n_left_to_next -= 2; b0 = vlib_get_buffer (vm, bi0); ip0 = vlib_buffer_get_current (b0); b1 = vlib_get_buffer (vm, bi1); ip1 = vlib_buffer_get_current (b1); /* dst lookup was done by ip4 lookup */ lkdi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX]; lkdi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX]; lkd0 = lookup_dpo_get(lkdi0); lkd1 = lookup_dpo_get(lkdi1); /* * choose between a lookup using the fib index in the DPO * or getting the FIB index from the interface. */ if (table_from_interface) { fib_index0 = ip4_fib_table_get_index_for_sw_if_index( vnet_buffer(b0)->sw_if_index[VLIB_RX]); fib_index1 = ip4_fib_table_get_index_for_sw_if_index( vnet_buffer(b1)->sw_if_index[VLIB_RX]); } else { fib_index0 = lkd0->lkd_fib_index; fib_index1 = lkd1->lkd_fib_index; } /* * choose between a source or destination address lookup in the table */ if (input_src_addr) { input_addr0 = &ip0->src_address; input_addr1 = &ip1->src_address; } else { input_addr0 = &ip0->dst_address; input_addr1 = &ip1->dst_address; } /* do lookup */ ip4_src_fib_lookup_two (fib_index0, fib_index1, input_addr0, input_addr1, &lbi0, &lbi1); lb0 = load_balance_get(lbi0); lb1 = load_balance_get(lbi1); vnet_buffer(b0)->sw_if_index[VLIB_TX] = fib_index0; vnet_buffer(b1)->sw_if_index[VLIB_TX] = fib_index1; /* Use flow hash to compute multipath adjacency. */ hash_c0 = vnet_buffer (b0)->ip.flow_hash = 0; hash_c1 = vnet_buffer (b1)->ip.flow_hash = 0; if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) { flow_hash_config0 = lb0->lb_hash_config; hash_c0 = vnet_buffer (b0)->ip.flow_hash = ip4_compute_flow_hash (ip0, flow_hash_config0); } if (PREDICT_FALSE (lb1->lb_n_buckets > 1)) { flow_hash_config1 = lb1->lb_hash_config; hash_c1 = vnet_buffer (b1)->ip.flow_hash = ip4_compute_flow_hash (ip1, flow_hash_config1); } dpo0 = load_balance_get_bucket_i(lb0, (hash_c0 & (lb0->lb_n_buckets_minus_1))); dpo1 = load_balance_get_bucket_i(lb1, (hash_c1 & (lb1->lb_n_buckets_minus_1))); next0 = dpo0->dpoi_next_node; next1 = dpo1->dpoi_next_node; vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; vnet_buffer(b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index; vlib_increment_combined_counter (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b0)); vlib_increment_combined_counter (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, b1)); if (!(b0->flags & VNET_BUFFER_F_LOOP_COUNTER_VALID)) { vnet_buffer2(b0)->loop_counter = 0; b0->flags |= VNET_BUFFER_F_LOOP_COUNTER_VALID; } if (!(b1->flags & VNET_BUFFER_F_LOOP_COUNTER_VALID)) { vnet_buffer2(b1)->loop_counter = 0; b1->flags |= VNET_BUFFER_F_LOOP_COUNTER_VALID; } vnet_buffer2(b0)->loop_counter++; vnet_buffer2(b1)->loop_counter++; if (PREDICT_FALSE(vnet_buffer2(b0)->loop_counter > MAX_LUKPS_PER_PACKET)) next0 = IP_LOOKUP_NEXT_DROP; if (PREDICT_FALSE(vnet_buffer2(b1)->loop_counter > MAX_LUKPS_PER_PACKET)) next1 = IP_LOOKUP_NEXT_DROP; if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) { lookup_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr)); tr->fib_index = fib_index0; tr->lbi = lbi0; tr->addr.ip4 = *input_addr0; } if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) { lookup_trace_t *tr = vlib_add_trace (vm, node, b1, sizeof (*tr)); tr->fib_index = fib_index1; tr->lbi = lbi1; tr->addr.ip4 = *input_addr1; } vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1); } while (n_left_from > 0 && n_left_to_next > 0) { u32 bi0, lkdi0, lbi0, fib_index0, next0, hash_c0; flow_hash_config_t flow_hash_config0; const ip4_address_t *input_addr; const load_balance_t *lb0; const lookup_dpo_t * lkd0; const ip4_header_t * ip0; const dpo_id_t *dpo0; vlib_buffer_t * b0; bi0 = from[0]; to_next[0] = bi0; from += 1; to_next += 1; n_left_from -= 1; n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); ip0 = vlib_buffer_get_current (b0); /* dst lookup was done by ip4 lookup */ lkdi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX]; lkd0 = lookup_dpo_get(lkdi0); /* * choose between a lookup using the fib index in the DPO * or getting the FIB index from the interface. */ if (table_from_interface) { fib_index0 = ip4_fib_table_get_index_for_sw_if_index( vnet_buffer(b0)->sw_if_index[VLIB_RX]); } else { fib_index0 = lkd0->lkd_fib_index; } /* * choose between a source or destination address lookup in the table */ if (input_src_addr) { input_addr = &ip0->src_address; } else { input_addr = &ip0->dst_address; } /* do lookup */ ip4_src_fib_lookup_one (fib_index0, input_addr, &lbi0); lb0 = load_balance_get(lbi0); vnet_buffer(b0)->sw_if_index[VLIB_TX] = fib_index0; /* Use flow hash to compute multipath adjacency. */ hash_c0 = vnet_buffer (b0)->ip.flow_hash = 0; if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) { flow_hash_config0 = lb0->lb_hash_config; hash_c0 = vnet_buffer (b0)->ip.flow_hash = ip4_compute_flow_hash (ip0, flow_hash_config0); } dpo0 = load_balance_get_bucket_i(lb0, (hash_c0 & (lb0->lb_n_buckets_minus_1))); next0 = dpo0->dpoi_next_node; vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; vlib_increment_combined_counter (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b0)); if (!(b0->flags & VNET_BUFFER_F_LOOP_COUNTER_VALID))
/*
 *------------------------------------------------------------------
 * ip_api.c - vnet ip 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 <stddef.h>

#include <vnet/ip6-nd/ip6_nd.h>
#include <vnet/ip6-nd/ip6_ra.h>

#include <vnet/fib/fib_table.h>
#include <vnet/ip/ip_types_api.h>

#include <vpp/app/version.h>

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

/* define message IDs */
#include <vnet/format_fns.h>
#include <vnet/ip6-nd/ip6_nd.api_enum.h>
#include <vnet/ip6-nd/ip6_nd.api_types.h>

/**
 * Base message ID fot the plugin
 */
static u32 ip6_nd_base_msg_id;
#define REPLY_MSG_ID_BASE ip6_nd_base_msg_id

#include <vlibapi/api_helper_macros.h>

static void
send_ip6nd_proxy_details (vl_api_registration_t * reg,
			  u32 context,
			  const ip46_address_t * addr, u32 sw_if_index)
{
  vl_api_ip6nd_proxy_details_t *mp;

  mp = vl_msg_api_alloc (sizeof (*mp));
  clib_memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
  mp->context = context;
  mp->sw_if_index = htonl (sw_if_index);

  ip6_address_encode (&addr->ip6, mp->ip);

  vl_api_send_msg (reg, (u8 *) mp);
}

typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
{
  u32 *indices;
} api_ip6nd_proxy_fib_table_walk_ctx_t;

static fib_table_walk_rc_t
api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
{
  api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;

  if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
    {
      vec_add1 (ctx->indices, fei);
    }

  return (FIB_TABLE_WALK_CONTINUE);
}

static void
vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
{
  ip6_main_t *im6 = &ip6_main;
  fib_table_t *fib_table;
  api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
    .indices = NULL,
  };
  fib_node_index_t *feip;
  const fib_prefix_t *pfx;
  vl_api_registration_t *reg;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  /* *INDENT-OFF* */
  pool_foreach (fib_table, im6->fibs,
  ({
    fib_table_walk(fib_table->ft_index,
                   FIB_PROTOCOL_IP6,
                   api_ip6nd_proxy_fib_table_walk,
                   &ctx);
  }));
  /* *INDENT-ON* */

  vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);

  vec_foreach (feip, ctx.indices)
  {
    pfx = fib_entry_get_prefix (*feip);

    send_ip6nd_proxy_details (reg,
			      mp->context,
			      &pfx->fp_addr,
			      fib_entry_get_resolving_interface (*feip));
  }

  vec_free (ctx.indices);
}

static void
vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
{
  vl_api_ip6nd_proxy_add_del_reply_t *rmp;
  ip6_address_t ip6;
  int rv = 0;

  VALIDATE_SW_IF_INDEX (mp);

  ip6_address_decode (mp->ip, &ip6);
  if (mp->is_add)
    rv = ip6_nd_proxy_add (ntohl (mp->sw_if_index), &ip6);
  else
    rv = ip6_nd_proxy_del (ntohl (mp->sw_if_index), &ip6);

  BAD_SW_IF_INDEX_LABEL;
  REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
}

static void
  vl_api_sw_interface_ip6nd_ra_config_t_handler
  (vl_api_sw_interface_ip6nd_ra_config_t * mp)
{
  vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
  vlib_main_t *vm = vlib_get_main ();
  int rv = 0;
  u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
    default_router;

  is_no = mp->is_no == 1;
  suppress = mp->suppress == 1;
  managed = mp->managed == 1;
  other = mp->other == 1;
  ll_option = mp->ll_option == 1;
  send_unicast = mp->send_unicast == 1;
  cease = mp->cease == 1;
  default_router = mp->default_router == 1;

  VALIDATE_SW_IF_INDEX (mp);

  rv = ip6_ra_config (vm, ntohl (mp->sw_if_index),
		      suppress, managed, other,
		      ll_option, send_unicast, cease,
		      default_router, ntohl (mp->lifetime),
		      ntohl (mp->initial_count),
		      ntohl (mp->initial_interval),
		      ntohl (mp->max_interval),
		      ntohl (mp->min_interval), is_no);

  BAD_SW_IF_INDEX_LABEL;

  REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
}

static void
  vl_api_sw_interface_ip6nd_ra_prefix_t_handler
  (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
{
  vlib_main_t *vm = vlib_get_main ();
  vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
  fib_prefix_t pfx;
  int rv = 0