/* * 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. */ /* * ip/ip4_fib.h: ip4 mtrie fib * * Copyright (c) 2012 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 /** * Global pool of IPv4 8bit PLYs */ ip4_fib_mtrie_8_ply_t *ip4_ply_pool; always_inline u32 ip4_fib_mtrie_leaf_is_non_empty (ip4_fib_mtrie_8_ply_t * p, u8 dst_byte) { /* * It's 'non-empty' if the length of the leaf stored is greater than the * length of a leaf in the covering ply. i.e. the leaf is more specific * than it's would be cover in the covering ply */ if (p->dst_address_bits_of_leaves[dst_byte] > p->dst_address_bits_base) return (1); return (0); } always_inline ip4_fib_mtrie_leaf_t ip4_fib_mtrie_leaf_set_adj_index (u32 adj_index) { ip4_fib_mtrie_leaf_t l; l = 1 + 2 * adj_index; ASSERT (ip4_fib_mtrie_leaf_get_adj_index (l) == adj_index); return l; } always_inline u32 ip4_fib_mtrie_leaf_is_next_ply (ip4_fib_mtrie_leaf_t n) { return (n & 1) == 0; } always_inline u32 ip4_fib_mtrie_leaf_get_next_ply_index (ip4_fib_mtrie_leaf_t n) { ASSERT (ip4_fib_mtrie_leaf_is_next_ply (n)); return n >> 1; } always_inline ip4_fib_mtrie_leaf_t ip4_fib_mtrie_leaf_set_next_ply_index (u32 i) { ip4_fib_mtrie_leaf_t l; l = 0 + 2 * i; ASSERT (ip4_fib_mtrie_leaf_get_next_ply_index (l) == i); return l; } #ifndef __ALTIVEC__ #define PLY_X4_SPLAT_INIT(init_x4, init) \ init_x4 = u32x4_splat (init); #else #define PLY_X4_SPLAT_INIT(init_x4, init) \ { \ u32x4_union_t y; \ y.as_u32[0] = init; \ y.as_u32[1] = init; \ y.as_u32[2] = init; \ y.as_u32[3] = init; \ init_x4 = y.as_u32x4; \ } #endif #ifdef CLIB_HAVE_VEC128 #define PLY_INIT_LEAVES(p) \ { \ u32x4 *l, init_x4; \ \ PLY_X4_SPLAT_INIT(init_x4, init); \ for (l = p->leaves_as_u32x4; \ l < p->leaves_as_u32x4 + ARRAY_LEN (p->leaves_as_u32x4); \ l += 4) \ { \ l[0] = init_x4; \ l[1] = init_x4; \ l[2] = init_x4; \ l[3] = init_x4; \ } \ } #else #define PLY_INIT_LEAVES(p) \ { \ u32 *l; \ \ for (l = p->leaves; l < p->leaves + ARRAY_LEN (p->leaves); l += 4) \ { \ l[0] = init; \ l[1] = init; \ l[2] = init; \ l[3] = init; \ } \ } #endif #define PLY_INIT(p, init, prefix_len, ply_base
#!/bin/bash

# Get Command Line arguements if present
VPP_DIR=$1
if [ "x$1" != "x" ]; then
    VPP_DIR=$1
else
    VPP_DIR=`dirname $0`/../../
fi

# Figure out what system we are running on
if [ -f /etc/lsb-release ];then
    . /etc/lsb-release
elif [ -f /etc/redhat-release ];then
    sudo -E yum install -y redhat-lsb
    DISTRIB_ID=`lsb_release -si`
    DISTRIB_RELEASE=`lsb_release -sr`
    DISTRIB_CODENAME=`lsb_release -sc`
    DISTRIB_DESCRIPTION=`lsb_release -sd`
fi
echo DISTRIB_ID: $DISTRIB_ID
echo DISTRIB_RELEASE: $DISTRIB_RELEASE
echo DISTRIB_CODENAME: $DISTRIB_CODENAME
echo DISTRIB_DESCRIPTION: $DISTRIB_DESCRIPTION

if [ $DISTRIB_ID == "Ubuntu" ]; then
    (cd ${VPP_DIR}/build-root/;sudo -E dpkg -i *.deb)
elif [ $DISTRIB_ID == "CentOS" ]; then
    (cd ${VPP_DIR}/build-root/;sudo -E rpm -Uvh *.rpm)
fi
eaf_is_terminal (old_leaf); if (old_leaf == del_leaf || (!old_leaf_is_terminal && unset_leaf (m, a, get_next_ply_for_leaf (m, old_leaf), dst_address_byte_index + 1))) { old_ply->n_non_empty_leafs -= ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); old_ply->leaves[i] = ip4_fib_mtrie_leaf_set_adj_index (a->cover_adj_index); old_ply->dst_address_bits_of_leaves[i] = a->cover_address_length; old_ply->n_non_empty_leafs += ip4_fib_mtrie_leaf_is_non_empty (old_ply, i); ASSERT (old_ply->n_non_empty_leafs >= 0); if (old_ply->n_non_empty_leafs == 0 && dst_address_byte_index > 0) { pool_put (ip4_ply_pool, old_ply); /* Old ply was deleted. */ return 1; } #if CLIB_DEBUG > 0 else if (dst_address_byte_index) { int ii, count = 0; for (ii = 0; ii < ARRAY_LEN (old_ply->leaves); ii++) { count += ip4_fib_mtrie_leaf_is_non_empty (old_ply, ii); } ASSERT (count); } #endif } } /* Old ply was not deleted. */ return 0; } static void unset_root_leaf (ip4_fib_mtrie_t * m, const ip4_fib_mtrie_set_unset_leaf_args_t * a) { ip4_fib_mtrie_leaf_t old_leaf, del_leaf; i32 n_dst_bits_next_plies; i32 i, n_dst_bits_this_ply, old_leaf_is_terminal; u16 dst_byte; ip4_fib_mtrie_16_ply_t *old_ply; ASSERT (a->dst_address_length <= 32); old_ply = &m->root_ply; n_dst_bits_next_plies = a->dst_address_length - BITS (u16); dst_byte = a->dst_address.as_u16[0]; n_dst_bits_this_ply = (n_dst_bits_next_plies <= 0 ? (16 - a->dst_address_length) : 0); del_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index); /* Starting at the value of the byte at this section of the v4 address * fill the buckets/slots of the ply */ for (i = 0; i < (1 << n_dst_bits_this_ply); i++) { u16 slot; slot = clib_net_to_host_u16 (dst_byte); slot += i; slot = clib_host_to_net_u16 (slot); old_leaf = old_ply->leaves[slot]; old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf); if (old_leaf == del_leaf || (!old_leaf_is_terminal && unset_leaf (m, a, get_next_ply_for_leaf (m, old_leaf), 2))) { old_ply->leaves[slot] = ip4_fib_mtrie_leaf_set_adj_index (a->cover_adj_index); old_ply->dst_address_bits_of_leaves[slot] = a->cover_address_length; } } } void ip4_fib_mtrie_route_add (ip4_fib_mtrie_t * m, const ip4_address_t * dst_address, u32 dst_address_length, u32 adj_index) { ip4_fib_mtrie_set_unset_leaf_args_t a; ip4_main_t *im = &ip4_main; /* Honor dst_address_length. Fib masks are in network byte order */ a.dst_address.as_u32 = (dst_address->as_u32 & im->fib_masks[dst_address_length]); a.dst_address_length = dst_address_length; a.adj_index = adj_index; set_root_leaf (m, &a); } void ip4_fib_mtrie_route_del (ip4_fib_mtrie_t * m, const ip4_address_t * dst_address, u32 dst_address_length, u32 adj_index, u32 cover_address_length, u32 cover_adj_index) { ip4_fib_mtrie_set_unset_leaf_args_t a; ip4_main_t *im = &ip4_main; /* Honor dst_address_length. Fib masks are in network byte order */ a.dst_address.as_u32 = (dst_address->as_u32 & im->fib_masks[dst_address_length]); a.dst_address_length = dst_address_length; a.adj_index = adj_index; a.cover_adj_index = cover_adj_index; a.cover_address_length = cover_address_length; /* the top level ply is never removed */ unset_root_leaf (m, &a); } /* Returns number of bytes of memory used by mtrie. */ static uword mtrie_ply_memory_usage (ip4_fib_mtrie_t * m, ip4_fib_mtrie_8_ply_t * p) { uword bytes, i; bytes = sizeof (p[0]); for (i = 0; i < ARRAY_LEN (p->leaves); i++) { ip4_fib_mtrie_leaf_t l = p->leaves[i]; if (ip4_fib_mtrie_leaf_is_next_ply (l)) bytes += mtrie_ply_memory_usage (m, get_next_ply_for_leaf (m, l)); } return bytes; } /* Returns number of bytes of memory used by mtrie. */ uword ip4_fib_mtrie_memory_usage (ip4_fib_mtrie_t * m) { uword bytes, i; bytes = sizeof (*m); for (i = 0; i < ARRAY_LEN (m->root_ply.leaves); i++) { ip4_fib_mtrie_leaf_t l = m->root_ply.leaves[i]; if (ip4_fib_mtrie_leaf_is_next_ply (l)) bytes += mtrie_ply_memory_usage (m, get_next_ply_for_leaf (m, l)); } return bytes; } static u8 * format_ip4_fib_mtrie_leaf (u8 * s, va_list * va) { ip4_fib_mtrie_leaf_t l = va_arg (*va, ip4_fib_mtrie_leaf_t); if (ip4_fib_mtrie_leaf_is_terminal (l)) s = format (s, "lb-index %d", ip4_fib_mtrie_leaf_get_adj_index (l)); else s = format (s, "next ply %d", ip4_fib_mtrie_leaf_get_next_ply_index (l)); return s; } #define FORMAT_PLY(s, _p, _a, _i, _base_address, _ply_max_len, _indent) \ ({ \ u32 a, ia_length; \ ip4_address_t ia; \ ip4_fib_mtrie_leaf_t _l = p->leaves[(_i)]; \ \ a = (_base_address) + ((_a) << (32 - (_ply_max_len))); \ ia.as_u32 = clib_host_to_net_u32 (a); \ ia_length = (_p)->dst_address_bits_of_leaves[(_i)]; \ s = format (s, "\n%U%U %U", \ format_white_space, (_indent) + 4, \ format_ip4_address_and_length, &ia, ia_length, \ format_ip4_fib_mtrie_leaf, _l); \ \ if (ip4_fib_mtrie_leaf_is_next_ply (_l)) \ s = format (s, "\n%U", \ format_ip4_fib_mtrie_ply, m, a, (_indent) + 8, \ ip4_fib_mtrie_leaf_get_next_ply_index (_l)); \ s; \ }) static u8 * format_ip4_fib_mtrie_ply (u8 * s, va_list * va) { ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *); u32 base_address = va_arg (*va, u32); u32 indent = va_arg (*va, u32); u32 ply_index = va_arg (*va, u32); ip4_fib_mtrie_8_ply_t *p; int i; p = pool_elt_at_index (ip4_ply_pool, ply_index); s = format (s, "%Uply index %d, %d non-empty leaves", format_white_space, indent, ply_index, p->n_non_empty_leafs); for (i = 0; i < ARRAY_LEN (p->leaves); i++) { if (ip4_fib_mtrie_leaf_is_non_empty (p, i)) { s = FORMAT_PLY (s, p, i, i, base_address, p->dst_address_bits_base + 8, indent); } } return s; } u8 * format_ip4_fib_mtrie (u8 * s, va_list * va) { ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *); int verbose = va_arg (*va, int); ip4_fib_mtrie_16_ply_t *p; u32 base_address = 0; int i; s = format (s, "%d plies, memory usage %U\n", pool_elts (ip4_ply_pool), format_memory_size, ip4_fib_mtrie_memory_usage (m)); s = format (s, "root-ply"); p = &m->root_ply; if (verbose) { s = format (s, "root-ply"); p = &m->root_ply; for (i = 0; i < ARRAY_LEN (p->leaves); i++) { u16 slot; slot = clib_host_to_net_u16 (i); if (p->dst_address_bits_of_leaves[slot] > 0) { s = FORMAT_PLY (s, p, i, slot, base_address, 16, 0); } } } return s; } /** Default heap size for the IPv4 mtries */ #define IP4_FIB_DEFAULT_MTRIE_HEAP_SIZE (32<<20) static clib_error_t * ip4_mtrie_module_init (vlib_main_t * vm) { CLIB_UNUSED (ip4_fib_mtrie_8_ply_t * p); ip4_main_t *im = &ip4_main; clib_error_t *error = NULL; uword *old_heap; if (0 == im->mtrie_heap_size) im->mtrie_heap_size = IP4_FIB_DEFAULT_MTRIE_HEAP_SIZE; #if USE_DLMALLOC == 0 im->mtrie_mheap = mheap_alloc (0, im->mtrie_heap_size); #else im->mtrie_mheap = create_mspace (im->mtrie_heap_size, 1 /* locked */ ); #endif /* Burn one ply so index 0 is taken */ old_heap = clib_mem_set_heap (ip4_main.mtrie_mheap); pool_get (ip4_ply_pool, p); clib_mem_set_heap (old_heap); return (error); } VLIB_INIT_FUNCTION (ip4_mtrie_module_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */