/* * Copyright (c) 2018 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 #include static format_function_t format_flow; uword unformat_ip_port_and_mask (unformat_input_t * input, va_list * args) { ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *); u32 port = 0, mask = 0; if (unformat (input, "any")) ; else if (unformat (input, "%u/%u", &port, &mask)) ; else if (unformat (input, "%u/0x%x", &port, &mask)) ; else if (unformat (input, "%u", &port)) mask = 0xffff; else return 0; if (port > 0xffff || mask > 0xffff) return 0; pm->port = port; pm->mask = mask; return 1; } u8 * format_ip_port_and_mask (u8 * s, va_list * args) { ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *); if (pm->port == 0 && pm->mask == 0) return format (s, "any"); if (pm->mask == 0xffff) return format (s, "%u", pm->port); return format (s, "%u/0x%x", pm->port, pm->mask); } uword unformat_ip_protocol_and_mask (unformat_input_t * input, va_list * args) { ip_prot_and_mask_t *pm = va_arg (*args, ip_prot_and_mask_t *); u32 prot = 0, mask = 0; if (unformat (input, "any")) ; else if (unformat (input, "%U", unformat_ip_protocol, &prot)) mask = 0xFF; else if (unformat (input, "%u", &prot)) mask = 0xFF; else return 0; if (prot > 0XFF || mask > 0xFF) return 0; pm->prot = prot; pm->mask = mask; return 1; } u8 * format_ip_protocol_and_mask (u8 * s, va_list * args) { ip_prot_and_mask_t *pm = va_arg (*args, ip_prot_and_mask_t *); if (pm->prot == 0 && pm->mask == 0) return format (s, "any"); return format (s, "%U", format_ip_protocol, pm->prot); } u8 * format_flow_error (u8 * s, va_list * args) { int error = va_arg (*args, int); if (error == 0) return format (s, "no error"); #define _(v,n,str) if (error == v) return format (s, #str); foreach_flow_error; #undef _ return format (s, "unknown error (%d)", error); } u8 * format_flow_actions (u8 * s, va_list * args) { u32 actions = va_arg (*args, u32); u8 *t = 0; #define _(a, b, c) if (actions & (1 << a)) \ t = format (t, "%s%s", t ? " ":"", c); foreach_flow_action #undef _ s = format (s, "%v", t); vec_free (t); return s; } u8 * format_flow_enabled_hw (u8 * s, va_list * args) { u32 flow_index = va_arg (*args, u32); vnet_flow_t *f = vnet_get_flow (flow_index); if (f == 0) return format (s, "not found"); u8 *t = 0; u32 hw_if_index; uword private_data; vnet_main_t *vnm = vnet_get_main (); /* *INDENT-OFF* */ hash_foreach (hw_if_index, private_data, f->private_data, ({ t = format (t, "%s%U", t ? ", " : "", format_vnet_hw_if_index_name, vnm, hw_if_index); })); /* *INDENT-ON* */ s = format (s, "%v", t); vec_free (t); return s; } u8 * format_rss_function (u8 * s, va_list * args) { vnet_rss_function_t func = va_arg (*args, vnet_rss_function_t); if (0) ; #undef _ #define _(f, n) \ else if (func == VNET_RSS_FUNC_##f) \ return format (s, n); foreach_rss_function #undef _ return format (s, "unknown"); } u8 * format_rss_types (u8 * s, va_list * args) { u64 type = va_arg (*args, u64); #undef _ #define _(a,b,c) \ if (type & (1UL<index); vlib_cli_output (vm, "%-10s: %s", "type", flow_type_strings[f->type]); vlib_cli_output (vm, "%-10s: %U", "match", format_flow, f); if (f->type == VNET_FLOW_TYPE_GENERIC) { vlib_cli_output (vm, "%s: %s", "spec", f->generic.pattern.spec); vlib_cli_output (vm, "%s: %s", "mask", f->generic.pattern.mask); } /* *INDENT-OFF* */ hash_foreach (hw_if_index, private_data, f->private_data, ({ hi = vnet_get_hw_interface (vnm, hw_if_index); dev_class = vnet_get_device_class (vnm, hi->dev_class_index); vlib_cli_output (vm, "interface %U\n", format_vnet_hw_if_index_name, vnm, hw_if_index); if (dev_class->format_flow) vlib_cli_output (vm, " %U\n", dev_class->format_flow, hi->dev_instance, f->index, private_data); })); /* *INDENT-ON* */ return 0; } no_args: /* *INDENT-OFF* */ pool_foreach (f, fm->global_flow_pool) { vlib_cli_output (vm, "%U\n", format_flow, f); if (f->type == VNET_FLOW_TYPE_GENERIC) { vlib_cli_output (vm, "%s: %s", "spec", f->generic.pattern.spec); vlib_cli_output (vm, "%s: %s", "mask", f->generic.pattern.mask); } } /* *INDENT-ON* */ return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_flow_entry_command, static) = { .path = "show flow entry", .short_help = "show flow entry [index ]", .function = show_flow_entry, }; /* *INDENT-ON* */ static clib_error_t * show_flow_ranges (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd_arg) { vnet_flow_main_t *fm = &flow_main; vnet_flow_range_t *r = 0; vlib_cli_output (vm, "%8s %8s %s", "Start", "Count", "Owner"); /* *INDENT-OFF* */ vec_foreach (r, fm->ranges) { vlib_cli_output (vm, "%8u %8u %s", r->start, r->count, r->owner); }; /* *INDENT-ON* */ return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_flow_ranges_command, static) = { .path = "show flow ranges", .short_help = "show flow ranges", .function = show_flow_ranges, }; /* *INDENT-ON* */ static clib_error_t * show_flow_interface (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd_arg) { vnet_main_t *vnm = vnet_get_main (); vnet_hw_interface_t *hi; vnet_device_class_t *dev_class; unformat_input_t _line_input, *line_input = &_line_input; u32 hw_if_index = ~0; if (unformat_user (input, unformat_line_input, line_input)) { while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) ; else return clib_error_return (0, "parse error: '%U'", format_unformat_error, line_input); } unformat_free (line_input); } if (hw_if_index == ~0) return clib_error_return (0, "please specify interface"); hi = vnet_get_hw_interface (vnm, hw_if_index); dev_class = vnet_get_device_class (vnm, hi->dev_class_index); if (dev_class->format_flow == 0) return clib_error_return (0, "not supported"); vlib_cli_output (vm, "%U", dev_class->format_flow, hi->dev_instance, ~0, 0); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_flow_interface_command, static) = { .path = "show flow interface", .short_help = "show flow interface ", .function = show_flow_interface, }; /* *INDENT-ON* */ static clib_error_t * test_flow (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd_arg) { vnet_flow_t flow; vnet_main_t *vnm = vnet_get_main (); unformat_input_t _line_input, *line_input = &_line_input; enum { FLOW_UNKNOWN_ACTION, FLOW_ADD, FLOW_DEL, FLOW_ENABLE, FLOW_DISABLE } action = FLOW_UNKNOWN_ACTION; enum { FLOW_UNKNOWN_CLASS, FLOW_ETHERNET_CLASS, FLOW_IPV4_CLASS, FLOW_IPV6_CLASS, } flow_class = FLOW_UNKNOWN_CLASS; u32 hw_if_index = ~0, flow_index = ~0; int rv; u32 teid = 0, session_id = 0, spi = 0; u32 vni = 0; u32 queue_start = 0, queue_end = 0; vnet_flow_type_t type = VNET_FLOW_TYPE_UNKNOWN; ip4_address_and_mask_t ip4s = { }; ip4_address_and_mask_t ip4d = { }; ip6_address_and_mask_t ip6s = { }; ip6_address_and_mask_t ip6d = { }; ip_port_and_mask_t sport = { }; ip_port_and_mask_t dport = { }; ip_prot_and_mask_t protocol = { }; u16 eth_type; bool tcp_udp_port_set = false; bool gtpc_set = false; bool gtpu_set = false; bool vni_set = false; bool l2tpv3oip_set = false; bool ipsec_esp_set = false, ipsec_ah_set = false; u8 *rss_type[3] = { }; u8 *type_str = NULL; u8 *spec = NULL; u8 *mask = NULL; clib_memset (&flow, 0, sizeof (vnet_flow_t)); flow.index = ~0; flow.actions = 0; 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, "add")) action = FLOW_ADD; else if (unformat (line_input, "del")) action = FLOW_DEL; else if (unformat (line_input, "enable")) action = FLOW_ENABLE; else if (unformat (line_input, "disable")) action = FLOW_DISABLE; else if (unformat (line_input, "spec %s", &spec)) ; else if (unformat (line_input, "mask %s", &mask)) ; else if (unformat (line_input, "eth-type %U", unformat_ethernet_type_host_byte_order, ð_type)) flow_class = FLOW_ETHERNET_CLASS; else if (unformat (line_input, "src-ip %U", unformat_ip4_address_and_mask, &ip4s)) flow_class = FLOW_IPV4_CLASS; else if (unformat (line_input, "dst-ip %U", unformat_ip4_address_and_mask, &ip4d)) flow_class = FLOW_IPV4_CLASS; else if (unformat (line_input, "ip6-src-ip %U", unformat_ip6_address_and_mask, &ip6s)) flow_class = FLOW_IPV6_CLASS; else if (unformat (line_input, "ip6-dst-ip %U", unformat_ip6_address_and_mask, &ip6d)) flow_class = FLOW_IPV6_CLASS; else if (unformat (line_input, "src-port %U", unformat_ip_port_and_mask, &sport)) tcp_udp_port_set = true; else if (unformat (line_input, "dst-port %U", unformat_ip_port_and_mask, &dport)) tcp_udp_port_set = true; else if (unformat (line_input, "proto %U", unformat_ip_protocol_and_mask, &protocol)) ; else if (unformat (line_input, "gtpc teid %u", &teid)) gtpc_set = true; else if (unformat (line_input, "gtpu teid %u", &teid)) gtpu_set = true; else if (unformat (line_input, "vxlan vni %u", &vni)) vni_set = true; else if (unformat (line_input, "session id %u", &session_id)) { if (protocol.prot == IP_PROTOCOL_L2TP) l2tpv3oip_set = true; } else if (unformat (line_input, "spi %u", &spi)) { if (protocol.prot == IP_PROTOCOL_IPSEC_ESP) ipsec_esp_set = true; else if (protocol.prot == IP_PROTOCOL_IPSEC_AH) ipsec_ah_set = true; } else if (unformat (line_input, "index %u", &flow_index)) ; else if (unformat (line_input, "next-node %U", unformat_vlib_node, vm, &flow.redirect_node_index)) flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_NODE; else if (unformat (line_input, "mark %d", &flow.mark_flow_id)) flow.actions |= VNET_FLOW_ACTION_MARK; else if (unformat (line_input, "buffer-advance %d", &flow.buffer_advance)) flow.actions |= VNET_FLOW_ACTION_BUFFER_ADVANCE; else if (unformat (line_input, "redirect-to-queue %d", &flow.redirect_queue)) flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_QUEUE; else if (unformat (line_input, "drop")) flow.actions |= VNET_FLOW_ACTION_DROP; else if (unformat (line_input, "rss function")) { if (0) ; #undef _ #define _(f, s) \ else if (unformat (line_input, s)) \ flow.rss_fun = VNET_RSS_FUNC_##f; foreach_rss_function #undef _ else { return clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); } flow.actions |= VNET_FLOW_ACTION_RSS; } else if (unformat (line_input, "rss types")) { rss_type[0] = NULL; rss_type[1] = NULL; rss_type[2] = NULL; type_str = NULL; if (unformat (line_input, "%s use %s and %s", &rss_type[0], &rss_type[1], &rss_type[2])) ; else if (unformat (line_input, "%s use %s", &rss_type[0], &rss_type[1])) ; else if (unformat (line_input, "%s", &rss_type[0])) ; #undef _ #define _(a,b,c) \ else if (!clib_strcmp(c, (const char *)type_str)) \ flow.rss_types |= (1ULL<.highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright (c) 2017-2019 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 <vppinfra/error.h>

u8 RT (rule_is_exact_match) (RTT (mma_rule) * key, RTT (mma_rule) * r)
{
  int i;

  for (i = 0; i < ARRAY_LEN (key->match.as_u64); i++)
    {
      if (key->match.as_u64[i] != r->match.as_u64[i])
	return 0;
    }
  for (i = 0; i < ARRAY_LEN (key->mask.as_u64); i++)
    {
      if (key->mask.as_u64[i] != r->mask.as_u64[i])
	return 0;
    }
  return 1;
}

u8
RT (rule_is_match_for_key) (RTT (mma_mask_or_match) * key, RTT (mma_rule) * r)
{
  RTT (mma_mask_or_match) _tmp_key, *tkp = &_tmp_key;
  int i;

  *tkp = *key;
  for (i = 0; i < ARRAY_LEN</