/* * 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 /** * The magic number of child entries that make a path-list popular. * There's a trade-off here between convergnece and forwarding speed. * Popular path-lists generate load-balance maps for the entires that * use them. If the map is present there is a switch path cost to indirect * through the map - this indirection provides the fast convergence - so * without the map convergence is slower. */ #define FIB_PATH_LIST_POPULAR 64 /** * FIB path-list * A representation of the list/set of path trough which a prefix is reachable */ typedef struct fib_path_list_t_ { /** * A path-list is a node in the FIB graph. */ fib_node_t fpl_node; /** * Flags on the path-list */ fib_path_list_flags_t fpl_flags; /** * Vector of paths indicies for all configured paths. * For shareable path-lists this list MUST not change. */ fib_node_index_t *fpl_paths; /** * the RPF list calculated for this path list */ fib_node_index_t fpl_urpf; /** * Hash table of paths. valid only with INDEXED flag */ uword *fpl_db; } fib_path_list_t; /* * Array of strings/names for the FIB sources */ static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES; /* * The memory pool from which we allocate all the path-lists */ static fib_path_list_t * fib_path_list_pool; /* * The data-base of shared path-lists */ static uword *fib_path_list_db; /* * Debug macro */ #ifdef FIB_DEBUG #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) \ { \ u8 *_tmp = 0; \ _tmp = fib_path_list_format( \ fib_path_list_get_index(_pl), _tmp); \ clib_warning("pl:[%d:%p:%p:%s]:" _fmt, \ fib_path_list_get_index(_pl), \ _pl, _pl->fpl_paths, _tmp, \ ##_args); \ vec_free(_tmp); \ } #else #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...) #endif static fib_path_list_t * fib_path_list_get (fib_node_index_t index) { return (pool_elt_at_index(fib_path_list_pool, index)); } static fib_node_t * fib_path_list_get_node (fib_node_index_t index) { return ((fib_node_t*)fib_path_list_get(index)); } static fib_path_list_t* fib_path_list_from_fib_node (fib_node_t *node) { #if CLIB_DEBUG > 0 ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type); #endif return ((fib_path_list_t*)node); } static fib_node_index_t fib_path_list_get_index (fib_path_list_t *path_list) { return (path_list - fib_path_list_pool); } static u8 * format_fib_path_list (u8 * s, va_list * args) { fib_path_list_attribute_t attr; fib_node_index_t *path_index; fib_path_list_t *path_list; path_list = va_arg (*args, fib_path_list_t *); s = format (s, " index:%u", fib_path_list_get_index(path_list)); s = format (s, " locks:%u", path_list->fpl_node.fn_locks); if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags) { s = format (s, " flags:"); FOR_EACH_PATH_LIST_ATTRIBUTE(attr) { if ((1<fpl_flags) { s = format (s, "%s,", fib_path_list_attr_names[attr]); } } } s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf); vec_foreach (path_index, path_list->fpl_paths) { s = fib_path_format(*path_index, s); s = format(s, "\n"); } return (s); } u8 * fib_path_list_format (fib_node_index_t path_list_index, u8 * s) { fib_path_list_t *path_list; path_list = fib_path_list_get(path_list_index); return (format(s, "%U", format_fib_path_list, path_list)); } static uword fib_path_list_hash (fib_path_list_t *path_list) { uword old_path_list_hash, new_path_list_hash, path_hash; fib_node_index_t *path_index; ASSERT(path_list); new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths); vec_foreach (path_index, path_list->fpl_paths) { path_hash = fib_path_hash(*path_index); #if uword_bits == 64 hash_mix64(path_hash, old_path_list_hash, new_path_list_hash); #else hash_mix32(path_hash, old_path_list_hash, new_path_list_hash); #endif } return (new_path_list_hash); } always_inline uword fib_path_list_db_hash_key_from_index (uword index) { return 1 + 2*index; } always_inline uword fib_path_list_db_hash_key_is_index (uword key) { return key & 1; } always_inline uword fib_path_list_db_hash_key_2_index (uword key) { ASSERT (fib_path_list_db_hash_key_is_index (key)); return key / 2; } static fib_path_list_t* fib_path_list_db_get_from_hash_key (uword key) { fib_path_list_t *path_list; if (fib_path_list_db_hash_key_is_index (key)) { fib_node_index_t path_list_index; path_list_index = fib_path_list_db_hash_key_2_index(key); path_list = fib_path_list_get(path_list_index); } else { path_list = uword_to_pointer (key, fib_path_list_t *); } return (path_list); } static uword fib_path_list_db_hash_key_sum (hash_t * h, uword key) { fib_path_list_t *path_list; path_list = fib_path_list_db_get_from_hash_key(key); return (fib_path_list_hash(path_list)); } static uword fib_path_list_db_hash_key_equal (hash_t * h, uword key1, uword key2) { fib_path_list_t *path_list1, *path_list2; path_list1 = fib_path_list_db_get_from_hash_key(key1); path_list2 = fib_path_list_db_get_from_hash_key(key2); return (fib_path_list_hash(path_list1) == fib_path_list_hash(path_list2)); } static fib_node_index_t fib_path_list_db_find (fib_path_list_t *path_list) { uword *p; p = hash_get(fib_path_list_db, path_list); if (NULL != p) { return p[0]; } return (FIB_NODE_INDEX_INVALID); } static void fib_path_list_db_insert (fib_node_index_t path_list_index) { fib_path_list_t *path_list; path_list = fib_path_list_get(path_list_index); ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list)); hash_set (fib_path_list_db, fib_path_list_db_hash_key_from_index(path_list_index), path_list_index); FIB_PATH_LIST_DBG(path_list, "DB-inserted"); } static void fib_path_list_db_remove (fib_node_index_t path_list_index) { fib_path_list_t *path_list; path_list = fib_path_list_get(path_list_index); ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list)); hash_unset(fib_path_list_db, fib_path_list_db_hash_key_from_index(path_list_index)); FIB_PATH_LIST_DBG(path_list, "DB-removed"); } static void fib_path_list_destroy (fib_path_list_t *path_list) { fib_node_index_t *path_index; FIB_PATH_LIST_DBG(path_list, "destroy"); vec_foreach (path_index, path_list->fpl_paths) { fib_path_destroy(*path_index); } vec_free(path_list->fpl_paths); fib_urpf_list_unlock(path_list->fpl_urpf); fib_node_deinit(&path_list->fpl_node); pool_put(fib_path_list_pool, path_list); } static void fib_path_list_last_lock_gone (fib_node_t *node) { fib_path_list_t *path_list; path_list = fib_path_list_from_fib_node(node); FIB_PATH_LIST_DBG(path_list, "last-lock"); if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED) { fib_path_list_db_remove(fib_path_list_get_index(path_list)); } fib_path_list_destroy(path_list); } /* * fib_path_mk_lb * * update the multipath adj this path-list will contribute to its * children's forwarding. */ static void fib_path_list_mk_lb (fib_path_list_t *path_list, fib_forward_chain_type_t fct, dpo_id_t *dpo) { load_balance_path_t *nhs; fib_node_index_t *path_index; nhs = NULL; if (!dpo_id_is_valid(dpo)) { /* * first time create */ dpo_set(dpo, DPO_LOAD_BALANCE, fib_forw_chain_type_to_dpo_proto(fct), load_balance_create(0, fib_forw_chain_type_to_dpo_proto(fct), 0 /* FIXME FLOW HASH */)); } /* * We gather the
/*
 * 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) 2009 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_vector_altivec_h
#define included_vector_altivec_h

/* 128 bit shifts. */
#define _(t,ti,lr,f)						\
  always_inline t t##_##lr (t x, t y)				\
  { return (t) __builtin_altivec_##f ((ti) x, (ti) y); }	\
								\
  always_inline t t##_i##lr (t x, int i)			\
  {								\
    t j = {i,i,i,i}; \
    return t##_##lr (x, j);					\
  }

_(u16x8, i16x8, shift_left, vslh);
_(u32x4, i32x4, shift_left, vslw);
_(u16x8, i16x8, shift_right, vsrh);
_(u32x4, i32x4, shift_right, vsrw);
_(i16x8, i16x8, shift_right, vsrah);
_(i32x4, i32x4, shift_right, vsraw);
_(u16x8, i16x8, rotate_left, vrlh);
_(i16x8, i16x8, rotate_left, vrlh);
_(u32x4, i32x4, rotate_left, vrlw);
_(i32x4, i32x4, rotate_left, vrlw);

#undef _

#define _(t,it,lr,f)						\
  always_inline t t##_word_shift_##lr (t x, int n_words)	\
  {								\
    i32x4 n_bits = {0,0,0,n_words * BITS (it)};			\
    return (t) __builtin_altivec_##f ((i32x4) x, n_bits);	\
  }

_(u32x4, u32, left, vslo)
_(i32x4, i32, left, vslo)
_(u32x4, u32, right, vsro)
_(i32x4, i32, right, vsro)
_(u16x8, u16, left, vslo)
_(i16x8, i16, left, vslo)
_(u16x8, u16, right, vsro) _(i16x8, i16, right, vsro)
#undef _

/* Interleave. */
#define _(t,it,lh,f)						\
  always_inline t t##_interleave_##lh (t x, t y)		\
  { return (t) __builtin_altivec_##f ((it) x, (it) y); }

_(u32x4, i32x4, lo, vmrglw)
_(i32x4, i32x4, lo, vmrglw)
_(u16x8, i16x8, lo, vmrglh)
_(i16x8, i16x8, lo, vmrglh)
_(u32x4, i32x4, hi, vmrghw)
_(i32x4, i32x4, hi, vmrghw)
_(u16x8, i16x8, hi, vmrghh) _(i16x8, i16x8, hi, vmrghh)
#undef _
/* Unaligned loads/stores. */
#ifndef __cplusplus
#define _(t)						\
  always_inline void t##_store_unaligned (t x, t * a)	\
  { clib_mem_unaligned (a, t) = x; }			\
  always_inline t t##_load_unaligned (t * a)		\
  { return clib_mem_unaligned (a, t); }
  _(u8x16) _(u16x8) _(u32x4) _(u64x2) _(i8x16) _(i16x8) _(i32x4) _(i64x2)
#undef _
#endif
#define _signed_binop(n,m,f,g)						\
  /* Unsigned */							\
  always_inline u##n##x##m						\
  u##n##x##m##_##f (u##n##x##m x, u##n##x##m y)				\
  { return (u##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); } \
									\
  /* Signed */								\
  always_inline i##n##x##m						\
  i##n##x##m##_##f (i##n##x##m x, i##n##x##m y)				\
  { return (i##n##x##m) __builtin_altivec_##g ((i##n##x##m) x, (i##n##x##m) y); }
/* Compare operations. */
  _signed_binop (16, 8, is_equal, vcmpequh)
_signed_binop (32, 4, is_equal, vcmpequw)
#undef _signed_binop
     always_inline u16x8 u16x8_is_zero (u16x8 x)
{
  u16x8 zero = { 0 };
  return u16x8_is_equal (x, zero);
}

#endif /* included_vector_altivec_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
u32 fib_path_list_db_size (void) { return (hash_elts(fib_path_list_db)); } void fib_path_list_walk (fib_node_index_t path_list_index, fib_path_list_walk_fn_t func, void *ctx) { fib_node_index_t *path_index; fib_path_list_t *path_list; path_list = fib_path_list_get(path_list_index); vec_foreach(path_index, path_list->fpl_paths) { if (FIB_PATH_LIST_WALK_STOP == func(path_list_index, *path_index, ctx)) break; } } void fib_path_list_module_init (void) { fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft); fib_path_list_db = hash_create2 (/* elts */ 0, /* user */ 0, /* value_bytes */ sizeof (fib_node_index_t), fib_path_list_db_hash_key_sum, fib_path_list_db_hash_key_equal, /* format pair/arg */ 0, 0); } static clib_error_t * show_fib_path_list_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { fib_path_list_t *path_list; fib_node_index_t pli; if (unformat (input, "%d", &pli)) { /* * show one in detail */ if (!pool_is_free_index(fib_path_list_pool, pli)) { path_list = fib_path_list_get(pli); u8 *s = fib_path_list_format(pli, NULL); s = format(s, "children:"); s = fib_node_children_format(path_list->fpl_node.fn_children, s); vlib_cli_output (vm, "%s", s); vec_free(s); } else { vlib_cli_output (vm, "path list %d invalid", pli); } } else { /* * show all */ vlib_cli_output (vm, "FIB Path Lists"); pool_foreach(path_list, fib_path_list_pool, ({ vlib_cli_output (vm, "%U", format_fib_path_list, path_list); })); } return (NULL); } VLIB_CLI_COMMAND (show_fib_path_list, static) = { .path = "show fib path-lists", .function = show_fib_path_list_command, .short_help = "show fib path-lists", };