/* * 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. */ /* * cli.c: command line interface * * Copyright (c) 2008 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 #include #include #include /* Root of all show commands. */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vlib_cli_show_command, static) = { .path = "show", .short_help = "Show commands", }; /* *INDENT-ON* */ /* Root of all clear commands. */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = { .path = "clear", .short_help = "Clear commands", }; /* *INDENT-ON* */ /* Root of all set commands. */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vlib_cli_set_command, static) = { .path = "set", .short_help = "Set commands", }; /* *INDENT-ON* */ /* Root of all test commands. */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vlib_cli_test_command, static) = { .path = "test", .short_help = "Test commands", }; /* *INDENT-ON* */ /* Returns bitmap of commands which match key. */ static uword * vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input) { int i, n; uword *match = 0; vlib_cli_parse_position_t *p; unformat_skip_white_space (input); for (i = 0;; i++) { uword k; k = unformat_get_input (input); switch (k) { case 'a' ... 'z': case 'A' ... 'Z': case '0' ... '9': case '-': case '_': break; case ' ': case '\t': case '\r': case '\n': case UNFORMAT_END_OF_INPUT: /* White space or end of input removes any non-white matches that were before possible. */ if (i < vec_len (c->sub_command_positions) && clib_bitmap_count_set_bits (match) > 1) { p = vec_elt_at_index (c->sub_command_positions, i); for (n = 0; n < vec_len (p->bitmaps); n++) match = clib_bitmap_andnot (match, p->bitmaps[n]); } goto done; default: unformat_put_input (input); goto done; } if (i >= vec_len (c->sub_command_positions)) { no_match: clib_bitmap_free (match); return 0; } p = vec_elt_at_index (c->sub_command_positions, i); if (vec_len (p->bitmaps) == 0) goto no_match; n = k - p->min_char; if (n < 0 || n >= vec_len (p->bitmaps)) goto no_match; if (i == 0) match = clib_bitmap_dup (p->bitmaps[n]); else match = clib_bitmap_and (match, p->bitmaps[n]); if (clib_bitmap_is_zero (match)) goto no_match; } done: return match; } /* Looks for string based sub-input formatted { SUB-INPUT }. */ uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args) { unformat_input_t *sub_input = va_arg (*args, unformat_input_t *); u8 *s; uword c; while (1) { c = unformat_get_input (i); switch (c) { case ' ': case '\t': case '\n': case '\r': case '\f': break; case '{': default: /* Put back paren. */ if (c != UNFORMAT_END_OF_INPUT) unformat_put_input (i); if (c == '{' && unformat (i, "%v", &s)) { unformat_init_vector (sub_input, s); return 1; } return 0; } } return 0; } static vlib_cli_command_t * get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si) { vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si); return vec_elt_at_index (cm->commands, s->index); } static uword unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args) { vlib_main_t *vm = va_arg (*args, vlib_main_t *); vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *); vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **); vlib_cli_main_t *cm = &vm->cli_main; uword *match_bitmap, is_unique, index; { vlib_cli_sub_rule_t *sr; vlib_cli_parse_rule_t *r; vec_foreach (sr, c->sub_rules) { void **d; r = vec_elt_at_index (cm->parse_rules, sr->rule_index); vec_add2 (cm->parse_rule_data, d, 1); vec_reset_length (d[0]); if (r->data_size) d[0] = _vec_resize (d[0], /* length increment */ 1, r->data_size, /* header_bytes */ 0, /* data align */ sizeof (uword)); if (unformat_user (i, r->unformat_function, vm, d[0])) { *result = vec_elt_at_index (cm->commands, sr->command_index); return 1; } } } match_bitmap = vlib_cli_sub_command_match (c, i); is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1; index = ~0; if (is_unique) { index = clib_bitmap_first_set (match_bitmap); *result = get_sub_command (cm, c, index); } clib_bitmap_free (match_bitmap); return is_unique; } static int vlib_cli_cmp_strings (void *a1, void *a2) { u8 *c1 = *(u8 **) a1; u8 *c2 = *(u8 **) a2; return vec_cmp (c1, c2); } u8 ** vlib_cli_get_possible_completions (u8 * str) { vlib_cli_command_t *c; vlib_cli_sub_command_t *sc; vlib_main_t *vm = vlib_get_main (); vlib_cli_main_t *vcm = &vm->cli_main; uword *match_bitmap = 0; uword index, is_unique, help_next_level; u8 **result = 0; unformat_input_t input; unformat_init_vector (&input, vec_dup (str)); c = vec_elt_at_index (vcm->commands, 0); /* remove trailing whitespace, except for one of them */ while (vec_len (input.buffer) >= 2 && isspace (input.buffer[vec_len (input.buffer) - 1]) && isspace (input.buffer[vec_len (input.buffer) - 2])) { vec_del1 (input.buffer, vec_len (input.buffer) - 1); } /* if input is empty, directly return list of root commands */ if (vec_len (input.buffer) == 0 || (vec_len (input.buffer) == 1 && isspace (input.buffer[0]))) { vec_foreach (sc, c->sub_commands) { vec_add1 (result, (u8 *) sc->name); } goto done; } /* add a trailing '?' so that vlib_cli_sub_command_match can find * all commands starting with the input string */ vec_add1 (input.buffer, '?'); while (1) { match_bitmap = vlib_cli_sub_command_match (c, &input); /* no match: return no result */ if (match_bitmap == 0) { goto done; } is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1; /* unique match: try to step one subcommand level further */ if (is_unique) { /* stop if no more input */ if (input.index >= vec_len (input.buffer) - 1) { break; } index = clib_bitmap_first_set (match_bitmap); c = get_sub_command (vcm, c, index); clib_bitmap_free (match_bitmap); continue; } /* multiple matches: stop here, return all matches */ break; } /* remove trailing '?' */ vec_del1 (input.buffer, vec_len (input.buffer) - 1); /* if we have a space at the end of input, and a unique match, * autocomplete the next level of subcommands */ help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]); /* *INDENT-OFF* */ clib_bitmap_foreach(index, match_bitmap, { if (help_next_level && is_unique) { c = get_sub_command (vcm, c, index); vec_foreach (sc, c->sub_commands) { vec_add1 (result, (u8*) sc->name); } goto done; /* break doesn't work in this macro-loop */ } sc = &c->sub_commands[index]; vec_add1(result, (u8*) sc->name); }); /* *INDENT-ON* */ done: clib_bitmap_free (match_bitmap); unformat_free (&input); if (result) vec_sort_with_function (result, vlib_cli_cmp_strings); return result; } static u8 * format_vlib_cli_command_help (u8 * s, va_list * args) { vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *); int is_long = va_arg (*args, int); if (is_long && c->long_help) s = format (s, "%s", c->long_help); else if (c->short_help) s = format (s, "%s", c->short_help); else s = format (s, "%v commands", c->path); return s; } static u8 * format_vlib_cli_parse_rule_name (u8 * s, va_list * args) { vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *); return format (s, "<%U>", format_c_identifier, r->name); } static u8 * format_vlib_cli_path (u8 * s, va_list * args) { u8 *path = va_arg (*args, u8 *); int i, in_rule; in_rule = 0; for (i = 0; i < vec_len (path); i++) { switch (path[i]) { case '%': in_rule = 1; vec_add1 (s, '<'); /* start of */ break; case '_': /* _ -> space in rules. */ vec_add1 (s, in_rule ? ' ' : '_'); break; case ' ': if (in_rule) { vec_add1 (s, '>'); /* end of */ in_rule = 0; } vec_add1 (s, ' '); break; default: vec_add1 (s, path[i]); break; } } if (in_rule) vec_add1 (s, '>'); /* terminate */ return s; } static vlib_cli_command_t * all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index) { vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index); vlib_cli_sub_command_t *sc; vlib_cli_sub_rule_t *sr; if (c->function) vec_add1 (subs, c[0]); vec_foreach (sr, c->sub_rules) subs = all_subs (cm, subs, sr->command_index); vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index); return subs; } static int vlib_cli_cmp_rule (void *a1, void *a2) { vlib_cli_sub_rule_t *r1 = a1; vlib_cli_sub_rule_t *r2 = a2; return vec_cmp (r1->name, r2->name); } static int vlib_cli_cmp_command (void *a1, void *a2) { vlib_cli_command_t *c1 = a1; vlib_cli_command_t *c2 = a2; return vec_cmp (c1->path, c2->path); } static clib_error_t * vlib_cli_dispatch_sub_commands (vlib_main_t * vm, vlib_cli_main_t * cm, unformat_input_t * input, uword parent_command_index) { vlib_cli_command_t *parent, *c; clib_error_t *error = 0; unformat_input_t sub_input; u8 *string; uword is_main_dispatch = cm == &vm->cli_main; parent = vec_elt_at_index (cm->commands, parent_command_index); if (is_main_dispatch && unformat (input, "help")) { uword help_at_end_of_line, i; help_at_end_of_line = unformat_check_input (input) == UNFORMAT_END_OF_INPUT; while (1) { c = parent; if (unformat_user (input, unformat_vlib_cli_sub_command, vm, c, &parent)) ; else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT)) goto unknown; else break; } /* help SUB-COMMAND => long format help. "help" at end of line: show all commands. */ if (!help_at_end_of_line) vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c, /* is_long */ 1); else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0) vlib_cli_output (vm, "%v: no sub-commands", c->path); else { vlib_cli_sub_command_t *sc; vlib_cli_sub_rule_t *sr, *subs; subs = vec_dup (c->sub_rules); /* Add in rules if any. */ vec_foreach (sc, c->sub_commands) { vec_add2 (subs, sr, 1); sr->name = sc->name; sr->command_index = sc->index; sr->rule_index = ~0; } vec_sort_with_function (subs, vlib_cli_cmp_rule); for (i = 0; i < vec_len (subs); i++) { vlib_cli_command_t *d; vlib_cli_parse_rule_t *r; d = vec_elt_at_index (cm->commands, subs[i].command_index); r = subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules, subs [i].rule_index) : 0; if (r) vlib_cli_output (vm, " %-30U %U", format_vlib_cli_parse_rule_name, r, format_vlib_cli_command_help, d, /* is_long */ 0); else vlib_cli_output (vm, " %-30v %U", subs[i].name, format_vlib_cli_command_help, d, /* is_long */ 0); } vec_free (subs); } } else if (is_main_dispatch && (unformat (input, "choices") || unformat (input, "?"))) { vlib_cli_command_t *sub, *subs; subs = all_subs (cm, 0, parent_command_index); vec_sort_with_function (subs, vlib_cli_cmp_command); vec_foreach (sub, subs) vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, sub->path, format_vlib_cli_command_help, sub, /* is_long */ 0); vec_free (subs); } else if (unformat (input, "comment %v", &string)) { vec_free (string); } else if (unformat (input, "uncomment %U", unformat_vlib_cli_sub_input, &sub_input)) { error = vlib_cli_dispatch_sub_commands (vm, cm, &sub_input, parent_command_index); unformat_free (&sub_input); } else if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c)) { unformat_input_t *si; uword has_sub_commands = vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0; si = input; if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input)) si = &sub_input; if (has_sub_commands) error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands); if (has_sub_commands && !error) /* Found valid sub-command. */ ; else if (c->function) { clib_error_t *c_error; /* Skip white space for benefit of called function. */ unformat_skip_white_space (si); if (unformat (si, "?")) { vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */ 0); } else { if (PREDICT_FALSE (vm->elog_trace_cli_commands)) { /* *INDENT-OFF* */ ELOG_TYPE_DECLARE (e) = { .format = "cli-cmd: %s", .format_args = "T4", }; /* *INDENT-ON* */ struct { u32 c; } *ed; ed = ELOG_DATA (&vm->elog_main, e); ed->c = elog_global_id_for_msg_name (c->path); } if (!c->is_mp_safe) vlib_worker_thread_barrier_sync (vm); c_error = c->function (vm, si, c); if (!c->is_mp_safe) vlib_worker_thread_barrier_release (vm); if (PREDICT_FALSE (vm->elog_trace_cli_commands)) { /* *INDENT-OFF* */ ELOG_TYPE_DECLARE (e) = { .format = "cli-cmd: %s %s", .format_args = "T4T4", }; /* *INDENT-ON* */ struct { u32 c, err; } *ed; ed = ELOG_DATA (&vm->elog_main, e); ed->c = elog_global_id_for_msg_name (c->path); if (c_error) { vec_add1 (c_error->what, 0); ed->err = elog_global_id_for_msg_name ((const char *) c_error->what); _vec_len (c_error->what) -= 1; } else ed->err = elog_global_id_for_msg_name ("OK"); } if (c_error) { error = clib_error_return (0, "%v: %v", c->path, c_error->what); clib_error_free (c_error); /* Free sub i
/*
 *------------------------------------------------------------------
 * flow_api.c - flow api
 *
 * Copyright (c) 2020 Intel 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 <stddef.h>

#include <vnet/vnet.h>
#include <vlibmemory/api.h>
#include <vnet/interface.h>
#include <vnet/api_errno.h>
#include <vnet/flow/flow.h>
#include <vnet/fib/fib_table.h>
#include <vnet/tunnel/tunnel_types_api.h>
#include <vnet/ip/ip_types_api.h>
#include <vnet/vnet_msg_enum.h>

#define vl_typedefs		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_typedefs

#define vl_endianfun		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <vnet/vnet_all_api_h.h>
#undef vl_printfun

#include <vlibapi/api_helper_macros.h>

#define foreach_vpe_api_msg         \
_(FLOW_ADD, flow_add)               \
_(FLOW_DEL, flow_del)               \
_(FLOW_ENABLE, flow_enable)         \
_(FLOW_DISABLE, flow_disable)

static inline void
ipv4_addr_and_mask_convert (vl_api_ip4_address_and_mask_t * vl_api_addr,
			    ip4_address_and_mask_t * vnet_addr)
{
  clib_memcpy (vnet_addr, vl_api_addr, sizeof (*vnet_addr));
}

static inline void
ipv6_addr_and_mask_convert (vl_api_ip6_address_and_mask_t * vl_api_addr,
			    ip6_address_and_mask_t * vnet_addr)
{
  clib_memcpy (vnet_addr, vl_api_addr, sizeof (*vnet_addr));
}

static inline void
protocol_and_mask_convert (vl_api_ip_prot_and_mask_t * vl_api_protocol,
			   ip_prot_and_mask_t * vnet_protocol)
{
  vnet_protocol->prot = (ip_protocol_t) vl_api_protocol->prot;
  vnet_protocol->mask = vl_api_protocol->mask;
}

static inline void
port_and_mask_convert (vl_api_ip_port_and_mask_t * vl_api_port,
		       ip_port_and_mask_t * vnet_port)
{
  vnet_port->port = ntohs (vl_api_port->port);
  vnet_port->mask = ntohs (vl_api_port->mask);
}

static inline void
ipv4_n_tuple_flow_convert (vl_api_flow_ip4_n_tuple_t * vl_api_flow,
			   vnet_flow_ip4_n_tuple_t * f)
{
  ipv4_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
  ipv4_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
  protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);

  port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
  port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
}

static void
ipv6_n_tuple_flow_convert (vl_api_flow_ip6_n_tuple_t * vl_api_flow,
			   vnet_flow_ip6_n_tuple_t * f)
{
  ipv6_addr_and_mask_convert (&vl_api_flow->src_addr, &f->src_addr);
  ipv6_addr_and_mask_convert (&vl_api_flow->dst_addr, &f->dst_addr);
  protocol_and_mask_convert (&vl_api_flow->protocol, &f->protocol);

  port_and_mask_convert (&vl_api_flow->src_port, &f->src_port);
  port_and_mask_convert (&vl_api_flow->dst_port, &f->dst_port);
}

static inline void
ipv4_n_tuple_tagged_flow_convert (vl_api_flow_ip4_n_tuple_tagged_t *
				  vl_api_flow,
				  vnet_flow_ip4_n_tuple_tagged_t * f)
{
  return ipv4_n_tuple_flow_convert ((vl_api_flow_ip4_n_tuple_t *) vl_api_flow,
				    (vnet_flow_ip4_n_tuple_t *) f);
}

static inline void
ipv6_n_tuple_tagged_flow_convert (vl_api_flow_ip6_n_tuple_tagged_t *
				  vl_api_flow,
				  vnet_flow_ip6_n_tuple_tagged_t * f)
{
  return