/* * 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 static ila_main_t ila_main; #define ILA_TABLE_DEFAULT_HASH_NUM_BUCKETS (64 * 1024) #define ILA_TABLE_DEFAULT_HASH_MEMORY_SIZE (32<<20) #define foreach_ila_error \ _(NONE, "valid ILA packets") typedef enum { #define _(sym,str) ILA_ERROR_##sym, foreach_ila_error #undef _ ILA_N_ERROR, } ila_error_t; static char *ila_error_strings[] = { #define _(sym,string) string, foreach_ila_error #undef _ }; typedef enum { ILA_ILA2SIR_NEXT_DROP, ILA_ILA2SIR_N_NEXT, } ila_ila2sir_next_t; typedef struct { u32 ila_index; ip6_address_t initial_dst; u32 adj_index; } ila_ila2sir_trace_t; static ila_entry_t ila_sir2ila_default_entry = { .csum_mode = ILA_CSUM_MODE_NO_ACTION, .type = ILA_TYPE_IID, .dir = ILA_DIR_ILA2SIR, //Will pass the packet with no }; /** * @brief Dynamically registered DPO Type for ILA */ static dpo_type_t ila_dpo_type; /** * @brief Dynamically registered FIB node type for ILA */ static fib_node_type_t ila_fib_node_type; /** * FIB source for adding entries */ static fib_source_t ila_fib_src; u8 * format_half_ip6_address (u8 * s, va_list * va) { u64 v = clib_net_to_host_u64 (va_arg (*va, u64)); return format (s, "%04x:%04x:%04x:%04x", v >> 48, (v >> 32) & 0xffff, (v >> 16) & 0xffff, v & 0xffff); } u8 * format_ila_direction (u8 * s, va_list * args) { ila_direction_t t = va_arg (*args, ila_direction_t); #define _(i,n,st) \ if (t == ILA_DIR_##i) \ return format(s, st); ila_foreach_direction #undef _ return format (s, "invalid_ila_direction"); } static u8 * format_csum_mode (u8 * s, va_list * va) { ila_csum_mode_t csum_mode = va_arg (*va, ila_csum_mode_t); char *txt; switch (csum_mode) { #define _(i,n,st) \ case ILA_CSUM_MODE_##i: \ txt = st; \ break; ila_csum_foreach_type #undef _ default: txt = "invalid_ila_csum_mode"; break; } return format (s, txt); } u8 * format_ila_type (u8 * s, va_list * args) { ila_type_t t = va_arg (*args, ila_type_t); #define _(i,n,st) \ if (t == ILA_TYPE_##i) \ return format(s, st); ila_foreach_type #undef _ return format (s, "invalid_ila_type"); } static u8 * format_ila_entry (u8 * s, va_list * va) { vnet_main_t *vnm = va_arg (*va, vnet_main_t *); ila_entry_t *e = va_arg (*va, ila_entry_t *); if (!e) { return format (s, "%-15s%=40s%=40s%+16s%+18s%+11s", "Type", "SIR Address", "ILA Address", "Checksum Mode", "Direction", "Next DPO"); } else if (vnm) { if (ip6_address_is_zero(&e->next_hop)) { return format (s, "%-15U%=40U%=40U%18U%11U%s", format_ila_type, e->type, format_ip6_address, &e->sir_address, format_ip6_address, &e->ila_address, format_csum_mode, e->csum_mode, format_ila_direction, e->dir, "n/a"); } else { return format (s, "%-15U%=40U%=40U%18U%11U%U", format_ila_type, e->type, format_ip6_address, &e->sir_address, format_ip6_address, &e->ila_address, format_csum_mode, e->csum_mode, format_ila_direction, e->dir, format_dpo_id, &e->ila_dpo, 0); } } return NULL; } u8 * format_ila_ila2sir_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); ila_ila2sir_trace_t *t = va_arg (*args, ila_ila2sir_trace_t *); return format (s, "ILA -> SIR adj index: %d entry index: %d initial_dst: %U", t->adj_index, t->ila_index, format_ip6_address, &t->initial_dst); } static uword unformat_ila_direction (unformat_input_t * input, va_list * args) { ila_direction_t *result = va_arg (*args, ila_direction_t *); #define _(i,n,s) \ if (unformat(input, s)) \ { \ *result = ILA_DIR_##i; \ return 1;\ } ila_foreach_direction #undef _ return 0; } static uword unformat_ila_type (unformat_input_t * input, va_list * args) { ila_type_t *result = va_arg (*args, ila_type_t *); #define _(i,n,s) \ if (unformat(input, s)) \ { \ *result = ILA_TYPE_##i; \ return 1;\ } ila_foreach_type #undef _ return 0; } static uword unformat_ila_csum_mode (unformat_input_t * input, va_list * args) { ila_csum_mode_t *result = va_arg (*args, ila_csum_mode_t *); if (unformat (input, "none") || unformat (input, "no-action")) { *result = ILA_CSUM_MODE_NO_ACTION; return 1; } if (unformat (input, "neutral-map")) { *result = ILA_CSUM_MODE_NEUTRAL_MAP; return 1; } if (unformat (input, "adjust-transport")) { *result = ILA_CSUM_MODE_ADJUST_TRANSPORT; return 1; } return 0; } static uword unformat_half_ip6_address (unformat_input_t * input, va_list * args) { u64 *result = va_arg (*args, u64 *); u32 a[4]; if (!unformat (input, "%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3])) return 0; if (a[0] > 0xFFFF || a[1] > 0xFFFF || a[2] > 0xFFFF || a[3] > 0xFFFF) return 0; *result = clib_host_to_net_u64 ((((u64) a[0]) << 48) | (((u64) a[1]) << 32) | (((u64) a[2]) << 16) | (((u64) a[3]))); return 1; } static vlib_node_registration_t ila_ila2sir_node; static uword ila_ila2sir (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { u32 n_left_from, *from, next_index, *to_next, n_left_to_next; ila_main_t *ilm = &ila_main; from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; next_index = node->cached_next_index; while (n_left_from > 0) { 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 pi0, pi1; vlib_buffer_t *p0, *p1; ila_entry_t *ie0, *ie1; ip6_header_t *ip60, *ip61; ip6_address_t *sir_address0, *sir_address1; { 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, sizeof (ip6_header_t), LOAD); CLIB_PREFETCH (p3->data, sizeof (ip6_header_t), LOAD); } pi0 = to_next[0] = from[0]; pi1 = to_next[1] = from[1]; from += 2; n_left_from -= 2; to_next += 2; n_left_to_next -= 2; p0 = vlib_get_buffer (vm, pi0); p1 = vlib_get_buffer (vm, pi1); ip60 = vlib_buffer_get_current (p0); ip61 = vlib_buffer_get_current (p1); sir_address0 = &ip60->dst_address; sir_address1 = &ip61->dst_address; ie0 = pool_elt_at_index (ilm->entries, vnet_buffer (p0)->ip.adj_index[VLIB_TX]); ie1 = pool_elt_at_index (ilm->entries, vnet_buffer (p1)->ip.adj_index[VLIB_TX]); if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED)) { ila_ila2sir_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr)); tr->ila_index = ie0 - ilm->entries; tr->initial_dst = ip60->dst_address; tr->adj_index = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; } if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED)) { ila_ila2sir_trace_t *tr = vlib_add_trace (vm, node, p1, sizeof (*tr)); tr->ila_index = ie1 - ilm->entries; tr->initial_dst = ip61->dst_address; tr->adj_index = vnet_buffer (p1)->ip.adj_index[VLIB_TX]; } sir_address0 = (ie0->dir != ILA_DIR_SIR2ILA) ? &ie0->sir_address : sir_address0; sir_address1 = (ie1->dir != ILA_DIR_SIR2ILA) ? &ie1->sir_address : sir_address1; ip60->dst_address.as_u64[0] =
/*
 * 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 <vnet/vnet.h>

#include <vnet/bier/bier_types.h>
#include <vnet/bier/bier_bit_string.h>

/*
 * the first bit in the first byte is bit position 1.
 * bit position 0 is not valid
 */
#define BIER_GET_STRING_POS(_bp, _byte, _bit, _str)             \
{                                                               \
    _bp--;                                                      \
    _byte = ((BIER_BBS_LEN_TO_BUCKETS((_str)->bbs_len) - 1 ) -  \
             (_bp / BIER_BIT_MASK_BITS_PER_BUCKET));            \
    _bit = _bp % BIER_BIT_MASK_BITS_PER_BUCKET;                 \
}

static inline int
bier_bit_pos_is_valid (bier_bp_t bp,  const bier_bit_string_t *bbs)
{
    if (!((bp <= BIER_BBS_LEN_TO_BITS((bbs)->bbs_len)) &&
          (bp >= 1))) {
        return (0);
    }
    return (1);
}

/*
 * Validate a bit poistion
 */
#define BIER_BIT_POS_IS_VALID(_bp, _str)                                \
{                                                                       \
    if (!bier_bit_pos_is_valid(_bp, _str)) return;                      \
}

void
bier_bit_string_set_bit (bier_bit_string_t *bit_string,
                         bier_bp_t bp)
{
    bier_bit_mask_bucket_t bmask;
    u16 byte_pos, bit_pos;

    BIER_BIT_POS_IS_VALID(bp, bit_string);
    BIER_GET_STRING_POS(bp, byte_pos, bit_pos, bit_string);

    bmask = ((bier_bit_mask_bucket_t)1 << bit_pos);
    bit_string->bbs_buckets[byte_pos] |= bmask;
}

void
bier_bit_string_clear_bit (bier_bit_string_t *bit_string,
                           bier_bp_t bp)
{
    u16 byte_pos, bit_pos;

    BIER_BIT_POS_IS_VALID(bp, bit_string);
    BIER_GET_STRING_POS(bp, byte_pos, bit_pos, bit_string);

    bit_string->bbs_buckets[byte_pos] &= ~(1 << bit_pos);
}

u8 *
format_bier_bit_string (u8 * string,
                        va_list * args)
{
    bier_bit_string_t *bs = va_arg(*args, bier_bit_string_t *);
    int leading_marker = 0;
    int suppress_zero = 0;
    u16 index;
    u32 *ptr;

    ptr = (u32 *)bs->bbs_buckets;

    string = format(string, "%d#", (8 * bs->bbs_len));

    for (index = 0; index < (bs->bbs_len/4); index++) {
        if (!ptr[index]) {
            if (!leading_marker) {
                leading_marker = 1;
                suppress_zero = 1;
                string = format(string, ":");
                continue;
            }
            if (suppress_zero) continue;
        } else {
            suppress_zero = 0;
        }

        string = format(string, "%s%X", index ? ":" : "",
                        clib_net_to_host_u32(ptr[index]));
    }

    return (string);
}