span: add feature (rx only) (VPP-185)
[vpp.git] / vnet / vnet / span / span.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlib/vlib.h>
17 #include <vppinfra/error.h>
18 #include <vnet/feature/feature.h>
19
20 #include <vnet/span/span.h>
21
22 int
23 span_add_delete_entry (vlib_main_t * vm,
24                        u32 src_sw_if_index, u32 dst_sw_if_index, u8 is_add)
25 {
26   span_main_t *sm = &span_main;
27
28   if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && is_add)
29       || (src_sw_if_index == dst_sw_if_index))
30     return VNET_API_ERROR_INVALID_INTERFACE;
31
32   vnet_sw_interface_t *sw =
33     vnet_get_sw_interface (sm->vnet_main, src_sw_if_index);
34
35   vec_validate_aligned (sm->dst_by_src_sw_if_index, sw->hw_if_index,
36                         CLIB_CACHE_LINE_BYTES);
37   sm->dst_by_src_sw_if_index[sw->hw_if_index] = is_add ? dst_sw_if_index : 0;
38   vnet_feature_enable_disable ("device-input", "span-input",
39                                sw->hw_if_index, is_add, 0, 0);
40   return 0;
41 }
42
43 static clib_error_t *
44 set_interface_span_command_fn (vlib_main_t * vm,
45                                unformat_input_t * input,
46                                vlib_cli_command_t * cmd)
47 {
48   span_main_t *sm = &span_main;
49   u32 src_sw_if_index = ~0;
50   u32 dst_sw_if_index = ~0;
51   u8 is_add = 1;
52
53   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
54     {
55       if (unformat (input, "%U", unformat_vnet_sw_interface,
56                     sm->vnet_main, &src_sw_if_index))
57         ;
58       else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
59                          sm->vnet_main, &dst_sw_if_index))
60         ;
61       else if (unformat (input, "disable"))
62         is_add = 0;
63       else
64         break;
65     }
66
67   int rv =
68     span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, is_add);
69   if (rv == VNET_API_ERROR_INVALID_INTERFACE)
70     return clib_error_return (0, "Invalid interface");
71   return 0;
72 }
73
74 /* *INDENT-OFF* */
75 VLIB_CLI_COMMAND (set_interface_span_command, static) = {
76   .path = "set interface span",
77   .short_help = "set interface span <if-name> [disable | destination <if-name>]",
78   .function = set_interface_span_command_fn,
79 };
80 /* *INDENT-ON* */
81
82 static clib_error_t *
83 show_interfaces_span_command_fn (vlib_main_t * vm,
84                                  unformat_input_t * input,
85                                  vlib_cli_command_t * cmd)
86 {
87
88   span_main_t *sm = &span_main;
89   vnet_main_t *vnm = &vnet_main;
90   u32 src_sw_if_index = 0, *dst_sw_if_index;
91   u8 header = 1;
92
93   vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
94   {
95     if (*dst_sw_if_index > 0)   // && *dst_sw_if_index != ~0)
96       {
97         if (header)
98           {
99             vlib_cli_output (vm,
100                              "SPAN source interface to destination interface table");
101             header = 0;
102           }
103         vlib_cli_output (vm, "%32U => %-32U",
104                          format_vnet_sw_if_index_name, vnm, src_sw_if_index,
105                          format_vnet_sw_if_index_name, vnm, *dst_sw_if_index);
106       }
107     src_sw_if_index++;
108   }
109   return 0;
110 }
111
112 /* *INDENT-OFF* */
113 VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
114   .path = "show interfaces span",
115   .short_help = "Shows SPAN mirror table",
116   .function = show_interfaces_span_command_fn,
117 };
118 /* *INDENT-ON* */
119
120 static clib_error_t *
121 span_init (vlib_main_t * vm)
122 {
123   span_main_t *sm = &span_main;
124
125   sm->vlib_main = vm;
126   sm->vnet_main = vnet_get_main ();
127
128   return 0;
129 }
130
131 VLIB_INIT_FUNCTION (span_init);
132
133 /*
134  * fd.io coding-style-patch-verification: ON
135  *
136  * Local Variables:
137  * eval: (c-set-style "gnu")
138  * End:
139  */