/* * Copyright (c) 2011-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. */ /** * @file * @brief BFD CLI implementation */ #include #include #include #include #include #include #include #include static u8 * format_bfd_session_cli (u8 * s, va_list * args) { vlib_main_t *vm = va_arg (*args, vlib_main_t *); bfd_session_t *bs = va_arg (*args, bfd_session_t *); switch (bs->transport) { case BFD_TRANSPORT_UDP4: s = format (s, "%=10u %-32s %20U %20U\n", bs->bs_idx, "IPv4 address", format_ip4_address, bs->udp.key.local_addr.ip4.as_u8, format_ip4_address, bs->udp.key.peer_addr.ip4.as_u8); break; case BFD_TRANSPORT_UDP6: s = format (s, "%=10u %-32s %20U %20U\n", bs->bs_idx, "IPv6 address", format_ip6_address, &bs->udp.key.local_addr.ip6, format_ip6_address, &bs->udp.key.peer_addr.ip6); break; } s = format (s, "%10s %-32s %20s %20s\n", "", "Session state", bfd_state_string (bs->local_state), bfd_state_string (bs->remote_state)); s = format (s, "%10s %-32s %20s %20s\n", "", "Diagnostic code", bfd_diag_code_string (bs->local_diag), bfd_diag_code_string (bs->remote_diag)); s = format (s, "%10s %-32s %20u %20u\n", "", "Detect multiplier", bs->local_detect_mult, bs->remote_detect_mult); s = format (s, "%10s %-32s %20u %20llu\n", "", "Required Min Rx Interval (usec)", bs->config_required_min_rx_usec, bs->remote_min_rx_usec); s = format (s, "%10s %-32s %20u %20u\n", "", "Desired Min Tx Interval (usec)", bs->config_desired_min_tx_usec, bfd_nsec_to_usec (bs->remote_desired_min_tx_nsec)); s = format (s, "%10s %-32s %20u\n", "", "Transmit interval", bfd_nsec_to_usec (bs->transmit_interval_nsec)); u64 now = clib_cpu_time_now (); u8 *tmp = NULL; if (bs->last_tx_nsec > 0) { tmp = format (tmp, "%.2fs ago", (now - bs->last_tx_nsec) * vm->clib_time.seconds_per_clock); s = format (s, "%10s %-32s %20v\n", "", "Last control frame tx", tmp); vec_reset_length (tmp); } if (bs->last_rx_nsec) { tmp = format (tmp, "%.2fs ago", (now - bs->last_rx_nsec) * vm->clib_time.seconds_per_clock); s = format (s, "%10s %-32s %20v\n", "", "Last control frame rx", tmp); vec_reset_length (tmp); } s = format (s, "%10s %-32s %20u %20llu\n", "", "Min Echo Rx Interval (usec)", 1, bs->remote_min_echo_rx_usec); if (bs->echo) { s = format (s, "%10s %-32s %20u\n", "", "Echo transmit interval", bfd_nsec_to_usec (bs->echo_transmit_interval_nsec)); tmp = format (tmp, "%.2fs ago", (now - bs->echo_last_tx_nsec) * vm->clib_time.seconds_per_clock); s = format (s, "%10s %-32s %20v\n", "", "Last echo frame tx", tmp); vec_reset_length (tmp); tmp = format (tmp, "%.6fs", (bs->echo_last_rx_nsec - bs->echo_last_tx_nsec) * vm->clib_time.seconds_per_clock); s = format (s, "%10s %-32s %20v\n", "", "Last echo frame roundtrip time", tmp); } vec_free (tmp); tmp = NULL; s = format (s, "%10s %-32s %20s %20s\n", "", "Demand mode", "no", bs->remote_demand ? "yes" : "no"); s = format (s, "%10s %-32s %20s\n", "", "Poll state", bfd_poll_state_string (bs->poll_state)); if (bs->auth.curr_key) { s = format (s, "%10s %-32s %20u\n", "", "Authentication config key ID", bs->auth.curr_key->conf_key_id); s = format (s, "%10s %-32s %20u\n", "", "Authentication BFD key ID", bs->auth.curr_bfd_key_id); s = format (s, "%10s %-32s %20u %20u\n", "", "Sequence number", bs->auth.local_seq_number, bs->auth.remote_seq_number); } return s; } static clib_error_t * show_bfd (vlib_main_t * vm, unformat_input_t * input, CLIB_UNUSED (vlib_cli_command_t * lmd)) { bfd_main_t *bm = &bfd_main; bfd_session_t *bs = NULL; unformat_input_t _line_input, *line_input = &_line_input; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; if (unformat (line_input, "keys")) { bfd_auth_key_t *key = NULL; u8 *s = format (NULL, "%=10s %=25s %=10s\n", "Configuration Key ID", "Type", "Use Count"); /* *INDENT-OFF* */ pool_foreach (key, bm->auth_keys) { s = format (s, "%10u %-25s %10u\n", key->conf_key_id, bfd_auth_type_str (key->auth_type), key->use_count); } /* *INDENT-ON* */ vlib_cli_output (vm, "%v\n", s); vec_free (s); vlib_cli_output (vm, "Number of configured BFD keys: %lu\n", (u64) pool_elts (bm->auth_keys)); } else if (unformat (line_input, "sessions")) { u8 *s = format (NULL, "%=10s %=32s %=20s %=20s\n", "Index", "Property", "Local value", "Remote value"); /* *INDENT-OFF* */ pool_foreach (bs, bm->sessions) { s = format (s, "%U", format_bfd_session_cli, vm, bs); } /* *INDENT-ON* */ vlib_cli_output (vm, "%v", s); vec_free (s); vlib_cli_output (vm, "Number of configured BFD sessions: %lu\n", (u64) pool_elts (bm->sessions)); } else if (unformat (line_input, "echo-source")) { int is_set; u32 sw_if_index; int have_usable_ip4; ip4_address_t ip4; int have_usable_ip6; ip6_address_t ip6; bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4, &have_usable_ip6, &ip6); if (is_set) { vnet_sw_interface_t *sw_if = vnet_get_sw_interface_or_null (&vnet_main, sw_if_index); vnet_hw_interface_t *hw_if = vnet_get_hw_interface (&vnet_main, sw_if->hw_if_index); u8 *s = format (NULL, "UDP echo source is: %v\n", hw_if->name); s = format (s, "IPv4 address usable as echo source: "); if (have_usable_ip4) { s = format (s, "%U\n", format_ip4_address, &ip4); } else { s = format (s, "none\n"); } s = format (s, "IPv6 address usable as echo source: "); if (have_usable_ip6) { s = format (s, "%U\n", format_ip6_address, &ip6); } else { s = format (s, "none\n"); } vlib_cli_output (vm, "%v", s); vec_free (s); } else { vlib_cli_output (vm, "UDP echo source is not set.\n"); } } else { vlib_cli_output (vm, "Number of configured BFD sessions: %lu\n", (u64) pool_elts (bm->sessions)); vlib_cli_output (vm, "Number of configured BFD keys: %lu\n", (u64) pool_elts (bm->auth_keys)); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_bfd_command, static) = { .path = "show bfd", .short_help = "show bfd [keys|sessions|echo-source]", .function = show_bfd, }; /* *INDENT-ON* */ static clib_error_t * bfd_cli_key_add (vlib_main_t * vm, unformat_input_t * input, CLIB_UNUSED (vlib_cli_command_t * lmd)) { clib_error_t *ret = NULL; int have_key_id = 0; u32 key_id = 0; u8 *vec_auth_type = NULL; bfd_auth_type_e auth_type = BFD_AUTH_TYPE_reserved; u8 *secret = NULL; unformat_input_t _line_input, *line_input = &_line_input; static const u8 keyed_sha1[] = "keyed-sha1"; static const u8 meticulous_keyed_sha1[] = "meticulous-keyed-sha1"; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "conf-key-id %u", &key_id)) { have_key_id = 1; } else if (unformat (line_input, "type %U", unformat_token, "a-zA-Z0-9-", &vec_auth_type)) { if (vec_len (vec_auth_type) == sizeof (keyed_sha1) - 1 && 0 == memcmp (vec_auth_type, keyed_sha1, sizeof (keyed_sha1) - 1)) { auth_type = BFD_AUTH_TYPE_keyed_sha1; } else if (vec_len (vec_auth_type) == sizeof (meticulous_keyed_sha1) - 1 && 0 == memcmp (vec_auth_type, meticulous_keyed_sha1, sizeof (meticulous_keyed_sha1) - 1)) { auth_type = BFD_AUTH_TYPE_meticulous_keyed_sha1; } else { ret = clib_error_return (0, "invalid type `%v'", vec_auth_type); goto out; } } else if (unformat (line_input, "secret %U", unformat_hex_string, &secret)) { /* nothing to do here */ } else { ret = clib_error_return (0, "Unknown input `%U'", format_unformat_error, line_input); goto out; } } if (!have_key_id) { ret = clib_error_return (0, "required parameter missing: `conf-key-id'"); goto out; } if (!vec_auth_type) { ret = clib_error_return (0, "required parameter missing: `type'"); goto out; } if (!secret) { ret = clib_error_return (0, "required parameter missing: `secret'"); goto out; } vnet_api_error_t rv = bfd_auth_set_key (key_id, auth_type, vec_len (secret), secret); if (rv) { ret = clib_error_return (0, "`bfd_auth_set_key' API call failed, rv=%d:%U", (int) rv, format_vnet_api_errno, rv); } out: vec_free (vec_auth_type); return ret; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (bfd_cli_key_add_command, static) = { .path = "bfd key set", .short_help = "bfd key set" " conf-key-id " " type " " secret ", .function = bfd_cli_key_add, }; /* *INDENT-ON* */ static clib_error_t * bfd_cli_key_del (vlib_main_t * vm, unformat_input_t * input, CLIB_UNUSED (vlib_cli_command_t * lmd)) { clib_error_t *ret = NULL; u32 key_id = 0; unformat_input_t _line_input, *line_input = &_line_input; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (!unformat (line_input, "conf-key-id %u", &key_id)) { ret = clib_error_return (0, "Unknown input `%U'", format_unformat_error, line_input); goto out; } } vnet_api_error_t rv = bfd_auth_del_key (key_id); if (rv) { ret = clib_error_return (0, "`bfd_auth_del_key' API call failed, rv=%d:%U", (int) rv, format_vnet_api_errno, rv); } out: return ret; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (bfd_cli_key_del_command, static) = { .path = "bfd key del", .short_help = "bfd key del conf-key-id ", .function = bfd_cli_key_del, }; /* *INDENT-ON* */ #define INTERFACE_STR "interface" #define LOCAL_ADDR_STR "local-addr" #define PEER_ADDR_STR "peer-addr" #define CONF_KEY_ID_STR "conf-key-id" #define BFD_KEY_ID_STR "bfd-key-id" #define DESIRED_MIN_TX_STR "desired-min-tx" #define REQUIRED_MIN_RX_STR "required-min-rx" #define DETECT_MULT_STR "detect-mult" #define ADMIN_STR "admin" #define DELAYED_STR "delayed" static const unsigned mandatory = 1; static const unsigned optional = 0; #define DECLARE(t, n, s, r, ...) \ int have_##n = 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/buffer.h>

#include <vnet/bier/bier_fmask.h>
#include <vnet/bier/bier_hdr_inlines.h>
#include <vlib/vlib.h>

static char * bier_output_error_strings[] = {
#define bier_error(n,s) s,
#include <vnet/bier/bier_output_error.def>
#undef bier_error
};

/*
 * Keep these values sematically the same as BIER output
 */
#define foreach_bier_output_next                \
    _(DROP, "bier-drop")

typedef enum {
#define _(s,n) BIER_OUTPUT_NEXT_##s,
    foreach_bier_output_next
#undef _
    BIER_OUTPUT_N_NEXT,
} bier_output_next_t;

typedef enum {
#define bier_error(n,s) BIER_OUTPUT_ERROR_##n,
#include <vnet/bier/bier_output_error.def>
#undef bier_error
    BIER_OUTPUT_N_ERROR,
} bier_output_error_t;

/**
 * Forward declaration
 */
vlib_node_registration_t bier_output_node;
extern vlib_combined_counter_main_t bier_fmask_counters;

/**
 * @brief Packet trace recoed for a BIER output
 */
typedef struct bier_output_trace_t_
{
    u32 next_index;
    index_t bfm_index;
} bier_output_trace_t;

static uword
bier_output (vlib_main_t * vm,
             vlib_node_runtime_t * node,
             vlib_frame_t * from_frame)
{
  vlib_combined_counter_main_t *cm = &bier_fmask_counters;
    u32 n_left_from, next_index, * from, * to_next;
    u32 thread_index;

    thread_index = vm->thread_index;
    from = vlib_frame_vector_args (from_frame);
    n_left_from = from_frame->n_vectors;

    /*
     * objection your honour! speculation!
     */
    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 > 0 && n_left_to_next > 0)
        {
            bier_output_next_t next0;
            bier_bit_string_t bbs;
            vlib_buffer_t * b0;
            bier_fmask_t *bfm0;
            mpls_label_t *h0;
            bier_hdr_t *bh0;
            u32 bfmi0;
            u32 bi0;

            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);
            bh0 = vlib_buffer_get_current (b0);
            bier_bit_string_init_from_hdr(bh0, &bbs);

            /*
             * In the BIER Lookup node we squirelled away the
             * BIER fmask index as the adj index
             */
            bfmi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
            bfm0 = bier_fmask_get(bfmi0);

            vlib_increment_combined_counter(
                cm, thread_index, bfmi0, 1,
                vlib_buffer_length_in_chain (vm, b0));

            /*
             * perform the logical AND of the packet's mask with
             * that of the fmask objects, to reset the bits that
             * are only on the shortest path the the fmask NH.
             */
            bier_bit_string_logical_and_string(
                &bfm0->bfm_bits.bfmb_input_reset_string,
                &bbs);

            /*
             * this is the last time we touch the BIER header
             * so flip to network order
             */
            bier_hdr_hton(bh0);

            /*
             * paint the BIER peer's label
             */
            if (!(bfm0->bfm_flags & BIER_FMASK_FLAG_DISP))
            {
                /*
                 * since a BIFT value and a MPLS label are formated the
                 * same, this painting works OK.
                 */
                vlib_buffer_advance(b0, -(word)sizeof(mpls_label_t));
                h0 = vlib_buffer_get_current(b0);
                
                h0[0] = bfm0->bfm_label;

                ((char*)h0)[3]= vnet_buffer(b0)->mpls.ttl - 1;
            }

            /*
             * setup next graph node
             */