aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/span/span_api.c
blob: 9566941ab9e04e639ad8189f8790393a7973e8c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *------------------------------------------------------------------
 * span_api.c - span mirroring api
 *
 * 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 <vlibmemory/api.h>

#include <vnet/interface.h>
#include <vnet/api_errno.h>
#include <vnet/span/span.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                             \
_(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable)   \
_(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump)                       \

static void
  vl_api_sw_interface_span_enable_disable_t_handler
  (vl_api_sw_interface_span_enable_disable_t * mp)
{
  vl_api_sw_interface_span_enable_disable_reply_t *rmp;
  int rv;

  vlib_main_t *vm = vlib_get_main ();

  rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
			      ntohl (mp->sw_if_index_to), mp->state,
			      mp->is_l2 ? SPAN_FEAT_L2 : SPAN_FEAT_DEVICE);

  REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
}

static void
vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
{

  vl_api_registration_t *reg;
  span_interface_t *si;
  vl_api_sw_interface_span_details_t *rmp;
  span_main_t *sm = &span_main;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  span_feat_t sf = mp->is_l2 ? SPAN_FEAT_L2 : SPAN_FEAT_DEVICE;
  /* *INDENT-OFF* */
  vec_foreach (si, sm->interfaces)
  {
    span_mirror_t * rxm = &si->mirror_rxtx[sf][VLIB_RX];
    span_mirror_t * txm = &si->mirror_rxtx[sf][VLIB_TX];
    if (rxm->num_mirror_ports || txm->num_mirror_ports)
    {
      clib_bitmap_t *b;
      u32 i;
      b = clib_bitmap_dup_or (rxm->mirror_ports, txm->mirror_ports);
      clib_bitmap_foreach (i, b, (
        {
          rmp = vl_msg_api_alloc (sizeof (*rmp));
          clib_memset (rmp, 0, sizeof (*rmp));
          rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
          rmp->context = mp->context;

          rmp->sw_if_index_from = htonl (si - sm->interfaces);
          rmp->sw_if_index_to = htonl (i);
          rmp->state = (u8) (clib_bitmap_get (rxm->mirror_ports, i) +
                             clib_bitmap_get (txm->mirror_ports, i) * 2);
	  rmp->is_l2 = mp->is_l2;

          vl_api_send_msg (reg, (u8 *) rmp);
        }));
      clib_bitmap_free (b);
    }
    }
  /* *INDENT-ON* */
}

/*
 * vpe_api_hookup
 * Add vpe's API message handlers to the table.
 * vlib has already mapped shared memory and
 * added the client registration handlers.
 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
 */
#define vl_msg_name_crc_list
#include <vnet/vnet_all_api_h.h>
#undef vl_msg_name_crc_list

static void
setup_message_id_table (api_main_t * am)
{
#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
  foreach_vl_msg_name_crc_span;
#undef _
}

static clib_error_t *
span_api_hookup (vlib_main_t * vm)
{
  api_main_t *am = &api_main;

#define _(N,n)                                                  \
    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
                           vl_api_##n##_t_handler,              \
                           vl_noop_handler,                     \
                           vl_api_##n##_t_endian,               \
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 1);
  foreach_vpe_api_msg;
#undef _

  /*
   * Set up the (msg_name, crc, message-id) table
   */
  setup_message_id_table (am);

  return 0;
}

VLIB_API_INIT_FUNCTION (span_api_hookup);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an> v4_address_match (vlib_parse_main_t * pm, vlib_parse_type_t * type, vlib_lex_token_t * t, vlib_parse_value_t * valuep) { u32 digit; u32 value = 0; int i; if (vec_len (pm->tokens) - (t - pm->tokens) < 7) return VLIB_PARSE_MATCH_FAIL; /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */ for (i = 0; i < 7; i++) { if ((i & 1) == 0) { if (t[i].token != VLIB_LEX_number) return VLIB_PARSE_MATCH_FAIL; if (t[i].value.as_uword > 0xff) return VLIB_PARSE_MATCH_FAIL; digit = t[i].value.as_uword; value = (value << 8) | digit; } else { if (t[i].token != VLIB_LEX_dot) return VLIB_PARSE_MATCH_FAIL; } } /* note: caller advances by 1 */ pm->current_token_index += 6; valuep->value.as_uword = value; return VLIB_PARSE_MATCH_VALUE; } PARSE_TYPE_INIT (v4_address, v4_address_match, 0, format_value_v4_address) static u8 *format_value_v4_address_and_mask (u8 * s, va_list * args) { vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *); u32 *a = v->value.as_pointer; s = format (s, "%d.%d.%d.%d", (a[0] >> 24) & 0xFF, (a[0] >> 16) & 0xFF, (a[0] >> 8) & 0xFF, (a[0] >> 0) & 0xFF); s = format (s, "/%d", a[1]); return s; } static vlib_parse_match_t v4_address_and_mask_match (vlib_parse_main_t * pm, vlib_parse_type_t * type, vlib_lex_token_t * t, vlib_parse_value_t * valuep) { u32 digit; u32 address = 0; u32 *rv = 0; int i; if (vec_len (pm->tokens) - (t - pm->tokens) < 9) return VLIB_PARSE_MATCH_FAIL; /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */ for (i = 0; i < 7; i++) { if ((i & 1) == 0) { if (t[i].token != VLIB_LEX_number) return VLIB_PARSE_MATCH_FAIL; if (t[i].value.as_uword > 0xff) return VLIB_PARSE_MATCH_FAIL; digit = t[i].value.as_uword; address = (address << 8) | digit; } else { if (t[i].token != VLIB_LEX_dot) return VLIB_PARSE_MATCH_FAIL; } } if (t[7].token != VLIB_LEX_slash || t[8].token != VLIB_LEX_number) return VLIB_PARSE_MATCH_FAIL; vec_add1 (rv, address); vec_add1 (rv, t[8].value.as_uword); /* note: caller advances by 1 */ pm->current_token_index += 8; valuep->value.as_pointer = rv; return VLIB_PARSE_MATCH_VALUE; } void v4_address_and_mask_cleanup (vlib_parse_value_t * valuep) { u32 *trash = valuep->value.as_pointer; vec_free (trash); } PARSE_TYPE_INIT (v4_address_and_mask, v4_address_and_mask_match, v4_address_and_mask_cleanup, format_value_v4_address_and_mask) vlib_lex_main_t vlib_lex_main; vlib_parse_match_t eval_factor0 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_factor1 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_factor2 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { word a; int index = vec_len (pm->parse_value) - 1; a = pm->parse_value[index].value.as_word; pm->parse_value[index].value.as_word = -a; return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_term0 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_term1 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; int index = vec_len (pm->parse_value) - 2; a = pm->parse_value[index].value.as_uword; b = pm->parse_value[index + 1].value.as_uword; pm->parse_value[index].value.as_uword = a * b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_term2 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; int index = vec_len (pm->parse_value) - 2; a = pm->parse_value[index].value.as_uword; b = pm->parse_value[index + 1].value.as_uword; pm->parse_value[index].value.as_uword = a / b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_exp0 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_exp1 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; int index = vec_len (pm->parse_value) - 2; a = pm->parse_value[index].value.as_uword; b = pm->parse_value[index + 1].value.as_uword; pm->parse_value[index].value.as_uword = a + b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_exp2 (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { uword a, b; int index = vec_len (pm->parse_value) - 2; a = pm->parse_value[index].value.as_uword; b = pm->parse_value[index + 1].value.as_uword; pm->parse_value[index].value.as_uword = a - b; _vec_len (pm->parse_value) -= 1; clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } vlib_parse_match_t eval_result (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_DONE; } vlib_parse_match_t noop_match_rule (vlib_parse_main_t * pm, vlib_parse_item_t * item, vlib_parse_value_t * value) { clib_warning ("%U", format_vlib_parse_value, pm); return VLIB_PARSE_MATCH_RULE; } #if 0 PARSE_INIT (t1, "moo", eval0); PARSE_INIT (t2, "moo cow mumble", eval1); PARSE_INIT (t3, "moo cow", eval2); PARSE_INIT (t4, "moo cow mumble grunch", eval3); #endif #if 0 PARSE_INIT (r1, "eval <exp>", eval_result); PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0); PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1); PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2); PARSE_INIT (r5, "<exp2> = ", noop_match_rule); PARSE_TYPE_INIT (exp, rule_match, 0, 0); PARSE_TYPE_INIT (exp2, rule_match, 0, 0); PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0); PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1); PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2); PARSE_INIT (r9, "<term2> = ", noop_match_rule); PARSE_TYPE_INIT (term, rule_match, 0, 0); PARSE_TYPE_INIT (term2, rule_match, 0, 0); PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1); PARSE_INIT (r10, "<factor> = <number>", eval_factor0); PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2); PARSE_TYPE_INIT (factor, rule_match, 0, 0); #endif PARSE_INIT (r1, "eval <exp>", eval_result); #if 1 PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0); PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1); PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2); PARSE_INIT (r5, "<exp2> = ", noop_match_rule); PARSE_TYPE_INIT (exp, rule_match, 0, 0); PARSE_TYPE_INIT (exp2, rule_match, 0, 0); PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0); PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1); PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2); PARSE_INIT (r9, "<term2> = ", noop_match_rule); PARSE_TYPE_INIT (term, rule_match, 0, 0); PARSE_TYPE_INIT (term2, rule_match, 0, 0); PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1); PARSE_INIT (r10, "<factor> = <number>", eval_factor0); PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2); PARSE_TYPE_INIT (factor, rule_match, 0, 0); #endif #if 0 PARSE_TYPE_INIT (exp, rule_match, 0, 0); PARSE_INIT (r6, "<exp> = a b", eval_term0); PARSE_INIT (r7, "<exp> = c d", eval_term1); PARSE_INIT (r9, "<exp> = ", noop_match_rule); #endif #if 0 #define foreach_rule_evaluator \ _(0) \ _(1) \ _(2) \ _(3) #define _(n) \ vlib_parse_match_t eval##n (vlib_parse_main_t *pm, \ vlib_parse_item_t *item, \ vlib_parse_value_t *value) \ { \ clib_warning ("%U", format_vlib_parse_value, pm); \ return VLIB_PARSE_MATCH_DONE; \ } foreach_rule_evaluator #undef _ PARSE_INIT (r1, "eval <moo>", eval_result); PARSE_INIT (r2, "<moo> = cow", eval0); PARSE_INIT (r4, "<moo> = ", eval1); PARSE_TYPE_INIT (moo, rule_match, 0, 0); #endif clib_error_t * test_init (vlib_main_t * vm) { clib_error_t *error; if ((error = vlib_call_init_function (vm, parse_init))) return error; return 0; } VLIB_INIT_FUNCTION (test_init); clib_error_t * vlib_stdlex_init (vlib_main_t * vm) { vlib_lex_main_t *lm = &vlib_lex_main; u16 top_index; u16 slash_index, slash_star_index, slash_slash_index, slash_star_star_index; u16 slash_token; u16 word_index; u16 zero_index, octal_index, decimal_index, hex_index, binary_index; top_index = vlib_lex_add_table ("top"); #define foreach_top_level_single_character_token \ _('(', lpar) \ _(')', rpar) \ _(';', semi) \ _('[', lbrack) \ _(']', rbrack) \ _('{', lcurly) \ _('}', rcurly) \ _('+', plus) \ _('-', minus) \ _('*', star) \ _('%', percent) \ _('@', atsign) \ _(',', comma) \ _('.', dot) \ _('?', qmark) #define _(c,t) \ vlib_lex_set_action_range(top_index,c,c,VLIB_LEX_RETURN,vlib_lex_add_token(lm, #t), top_index); foreach_top_level_single_character_token; #undef _ /* Numbers */ zero_index = vlib_lex_add_table ("zero"); octal_index = vlib_lex_add_table ("octal"); decimal_index = vlib_lex_add_table ("decimal"); hex_index = vlib_lex_add_table ("hex"); binary_index = vlib_lex_add_table ("binary"); /* Support 0x 0b 0t and 0123 [octal] */ vlib_lex_set_action_range (top_index, '0', '0', VLIB_LEX_START_NUMBER, 10, zero_index); vlib_lex_set_action_range (top_index, '1', '9', VLIB_LEX_START_NUMBER, 10, decimal_index); vlib_lex_set_action_range (zero_index, 0, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); vlib_lex_set_action_range (zero_index, 'x', 'x', VLIB_LEX_IGNORE, ~0, hex_index); vlib_lex_set_action_range (zero_index, 'b', 'b', VLIB_LEX_IGNORE, ~0, binary_index); vlib_lex_set_action_range (zero_index, 't', 't', VLIB_LEX_IGNORE, ~0, decimal_index); vlib_lex_set_action_range (zero_index, '0', '7', VLIB_LEX_START_NUMBER, 8, octal_index); /* Octal */ vlib_lex_set_action_range (octal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); vlib_lex_set_action_range (octal_index, '0', '7', VLIB_LEX_ADD_TO_NUMBER, 8, octal_index); /* Decimal */ vlib_lex_set_action_range (decimal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); vlib_lex_set_action_range (decimal_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 10, decimal_index); /* Hex */ vlib_lex_set_action_range (hex_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); vlib_lex_set_action_range (hex_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 16, hex_index); vlib_lex_set_action_range (hex_index, 'a', 'f', VLIB_LEX_ADD_TO_NUMBER, 16, hex_index); vlib_lex_set_action_range (hex_index, 'A', 'F', VLIB_LEX_ADD_TO_NUMBER, 16, hex_index); /* Binary */ vlib_lex_set_action_range (binary_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index); vlib_lex_set_action_range (binary_index, '0', '1', VLIB_LEX_ADD_TO_NUMBER, 2, binary_index); /* c/c++ comment syntax is the worst... */ slash_index = vlib_lex_add_table ("slash"); slash_star_index = vlib_lex_add_table ("slash_star"); slash_star_star_index = vlib_lex_add_table ("slash_star_star"); slash_slash_index = vlib_lex_add_table ("slash_slash"); slash_token = vlib_lex_add_token (lm, "slash"); /* Top level: see a slash, ignore, go to slash table */ vlib_lex_set_action_range (top_index, '/', '/', VLIB_LEX_IGNORE, ~0, slash_index); /* default for slash table: return SLASH, go to top table */ vlib_lex_set_action_range (slash_index, 1, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, slash_token, top_index); /* see slash-slash, go to s-s table */ vlib_lex_set_action_range (slash_index, '/', '/', VLIB_LEX_IGNORE, ~0, slash_slash_index); /* see slash-star, go to s-* table */ vlib_lex_set_action_range (slash_index, '*', '*', VLIB_LEX_IGNORE, ~0, slash_star_index); /* EOL in s-s table, ignore, go to top table */ vlib_lex_set_action_range (slash_slash_index, '\n', '\n', VLIB_LEX_IGNORE, ~0, top_index); /* slash-star blah blah star */ vlib_lex_set_action_range (slash_star_index, '*', '*', VLIB_LEX_IGNORE, ~0, slash_star_star_index); /* slash star blah blah star slash */ vlib_lex_set_action_range (slash_star_star_index, '/', '/', VLIB_LEX_IGNORE, ~0, top_index); /* LT, =, GT */ vlib_lex_set_action_range (top_index, '<', '<', VLIB_LEX_RETURN, VLIB_LEX_lt, top_index); vlib_lex_set_action_range (top_index, '=', '=', VLIB_LEX_RETURN, VLIB_LEX_equals, top_index); vlib_lex_set_action_range (top_index, '>', '>', VLIB_LEX_RETURN, VLIB_LEX_gt, top_index); /* words, key and otherwise */ word_index = vlib_lex_add_table ("word"); vlib_lex_set_action_range (top_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); vlib_lex_set_action_range (top_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); vlib_lex_set_action_range (word_index, 0, 0x7f, VLIB_LEX_KEYWORD_CHECK, ~0, top_index); vlib_lex_set_action_range (word_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); vlib_lex_set_action_range (word_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); vlib_lex_set_action_range (word_index, '_', '_', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); vlib_lex_set_action_range (word_index, '0', '9', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */