/* * 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. */ /** * @brief */ #include #include #include #include /** * A hash-table of load-balance maps by path index. * this provides the fast lookup of the LB map when a path goes down */ static uword *lb_maps_by_path_index; /** * A hash-table of load-balance maps by set of paths. * This provides the LB map sharing. * LB maps do not necessarily use all the paths in the list, since * the entry that is requesting the map, may not have an out-going * label for each of the paths. */ static uword *load_balance_map_db; typedef enum load_balance_map_path_flags_t_ { LOAD_BALANCE_MAP_PATH_UP = (1 << 0), LOAD_BALANCE_MAP_PATH_USABLE = (1 << 1), } __attribute__ ((packed)) load_balance_map_path_flags_t; typedef struct load_balance_map_path_t_ { /** * Index of the path */ fib_node_index_t lbmp_index; /** * Sibling Index in the list of all maps with this path index */ fib_node_index_t lbmp_sibling; /** * the normalised wegiht of the path */ u32 lbmp_weight; /** * The sate of the path */ load_balance_map_path_flags_t lbmp_flags; } load_balance_map_path_t; /** * The global pool of LB maps */ load_balance_map_t *load_balance_map_pool; /** * the logger */ vlib_log_class_t load_balance_map_logger; /* * Debug macro */ #define LOAD_BALANCE_MAP_DBG(_pl, _fmt, _args...) \ { \ vlib_log_debug(load_balance_map_logger, \ "lbm:" _fmt, \ ##_args); \ } static index_t load_balance_map_get_index (load_balance_map_t *lbm) { return (lbm - load_balance_map_pool); } u8* format_load_balance_map (u8 *s, va_list * ap) { index_t lbmi = va_arg(*ap, index_t); u32 indent = va_arg(*ap, u32); load_balance_map_t *lbm; u32 n_buckets, ii; lbm = load_balance_map_get(lbmi); n_buckets = vec_len(lbm->lbm_buckets); s = format(s, "load-balance-map: index:%d buckets:%d", lbmi, n_buckets); s = format(s, "\n%U index:", format_white_space, indent+2); for (ii = 0; ii < n_buckets; ii++) { s = format(s, "%5d", ii); } s = format(s, "\n%U map:", format_white_space, indent+2); for (ii = 0; ii < n_buckets; ii++) { s = format(s, "%5d", lbm->lbm_buckets[ii]); } return (s); } static uword load_balance_map_hash (load_balance_map_t *lbm) { u32 old_lbm_hash, new_lbm_hash, hash; load_balance_map_path_t *lb_path; new_lbm_hash = old_lbm_hash = vec_len(lbm->lbm_paths); vec_foreach (lb_path, lbm->lbm_paths) { hash = lb_path->lbmp_index; hash_mix32(hash, old_lbm_hash, new_lbm_hash); } return (new_lbm_hash); } always_inline uword load_balance_map_db_hash_key_from_index (uword index) { return 1 + 2*index; } always_inline uword load_balance_map_db_hash_key_is_index (uword key) { return key & 1; } always_inline uword load_balance_map_db_hash_key_2_index (uword key) { ASSERT (load_balance_map_db_hash_key_is_index (key)); return key / 2; } static load_balance_map_t* load_balance_map_db_get_from_hash_key (uword key) { load_balance_map_t *lbm; if (load_balance_map_db_hash_key_is_index (key)) { index_t lbm_index; lbm_index = load_balance_map_db_hash_key_2_index(key); lbm = load_balance_map_get(lbm_index); } else { lbm = uword_to_pointer (key, load_balance_map_t *); } return (lbm); } static uword load_balance_map_db_hash_key_sum (hash_t * h, uword key) { load_balance_map_t *lbm; lbm = load_balance_map_db_get_from_hash_key(key); return (load_balance_map_hash(lbm)); } static uword load_balance_map_db_hash_key_equal (hash_t * h, uword key1, uword key2) { load_balance_map_t *lbm1, *lbm2; lbm1 = load_balance_map_db_get_from_hash_key(key1); lbm2 = load_balance_map_db_get_from_hash_key(key2); return (load_balance_map_hash(lbm1) == load_balance_map_hash(lbm2)); } static index_t load_balance_map_db_find (load_balance_map_t *lbm) { uword *p; p = hash_get(load_balance_map_db, lbm); if (NULL != p) { return p[0]; } return (FIB_NODE_INDEX_INVALID); } static void load_balance_map_db_insert (load_balance_map_t *lbm) { load_balance_map_path_t *lbmp; fib_node_list_t list; uword *p; ASSERT(FIB_NODE_INDEX_INVALID == load_balance_map_db_find(lbm)); /* * insert into the DB based on the set of paths. */ hash_set (load_balance_map_db, load_balance_map_db_hash_key_from_index( load_balance_map_get_index(lbm)), load_balance_map_get_index(lbm)); /* * insert into each per-path list. */ vec_foreach(lbmp, lbm->lbm_paths) { p = hash_get(lb_maps_by_path_index, lbmp->lbmp_index); if (NULL == p) { list = fib_node_list_create(); hash_set(lb_maps_by_path_index, lbmp->lbmp_index, list); } else { list = p[0]; } lbmp->lbmp_sibling = fib_node_list_push_front(list, 0, FIB_NODE_TYPE_FIRST, load_balance_map_get_index(lbm)); } LOAD_BALANCE_MAP_DBG(lbm, "DB-inserted"); } static void load_balance_map_db_remove (load_balance_map_t *lbm) { load_balance_map_path_t *lbmp; uword *p; ASSERT(FIB_NODE_INDEX_INVALID != load_balance_map_db_find(lbm)); hash_unset(load_balance_map_db, load_balance_map_db_hash_key_from_index( load_balance_map_get_index(lbm))); /* * remove from each per-path list. */ vec_foreach(lbmp, lbm->lbm_paths) { p = hash_get(lb_maps_by_path_index, lbmp->lbmp_index); ALWAYS_ASSERT(NULL != p); fib_node_list_remove(p[0], lbmp->lbmp_sibling); } LOAD_BALANCE_MAP_DBG(lbm, "DB-removed"); } /** * @brief from the paths that are usable, fill the Map. */ static void load_balance_map_fill (load_balance_map_t *lbm) { load_balance_map_path_t *lbmp; u32 n_buckets, bucket, ii, jj; u16 *tmp_buckets; tmp_buckets = NULL; n_buckets = vec_len(lbm->lbm_buckets);