blob: bc244eff36dd630a3c8cfc77fabd9b856472a225 [file] [log] [blame]
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +01001/*
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
22int
23span_add_delete_entry (vlib_main_t * vm,
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010024 u32 src_sw_if_index, u32 dst_sw_if_index, u8 state)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010025{
26 span_main_t *sm = &span_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010027 span_interface_t *si;
28 u32 new_num_rx_mirror_ports, new_num_tx_mirror_ports;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010029
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010030 if (state > 3)
31 return VNET_API_ERROR_UNIMPLEMENTED;
32
33 if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010034 || (src_sw_if_index == dst_sw_if_index))
35 return VNET_API_ERROR_INVALID_INTERFACE;
36
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010037 vnet_sw_interface_t *sw_if;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010038
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010039 sw_if = vnet_get_sw_interface (vnet_get_main (), src_sw_if_index);
40 if (sw_if->type == VNET_SW_INTERFACE_TYPE_SUB)
41 return VNET_API_ERROR_UNIMPLEMENTED;
42
43 vec_validate_aligned (sm->interfaces, src_sw_if_index,
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010044 CLIB_CACHE_LINE_BYTES);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010045 si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
46
47 si->rx_mirror_ports = clib_bitmap_set (si->rx_mirror_ports, dst_sw_if_index,
48 (state & 1) != 0);
49 si->tx_mirror_ports = clib_bitmap_set (si->tx_mirror_ports, dst_sw_if_index,
50 (state & 2) != 0);
51
52 new_num_rx_mirror_ports = clib_bitmap_count_set_bits (si->rx_mirror_ports);
53 new_num_tx_mirror_ports = clib_bitmap_count_set_bits (si->tx_mirror_ports);
54
55 if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0)
56 vnet_feature_enable_disable ("device-input", "span-input",
57 src_sw_if_index, 1, 0, 0);
58
59 if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1)
60 vnet_feature_enable_disable ("device-input", "span-input",
61 src_sw_if_index, 0, 0, 0);
62
63 if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0)
Pavel Kotucek56634092017-01-24 08:26:31 +010064 vnet_feature_enable_disable ("interface-output", "span-output",
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010065 src_sw_if_index, 1, 0, 0);
66
67 if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1)
Pavel Kotucek56634092017-01-24 08:26:31 +010068 vnet_feature_enable_disable ("interface-output", "span-output",
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010069 src_sw_if_index, 0, 0, 0);
70
71 si->num_rx_mirror_ports = new_num_rx_mirror_ports;
72 si->num_tx_mirror_ports = new_num_tx_mirror_ports;
73
74 if (dst_sw_if_index > sm->max_sw_if_index)
75 sm->max_sw_if_index = dst_sw_if_index;
76
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010077 return 0;
78}
79
80static clib_error_t *
81set_interface_span_command_fn (vlib_main_t * vm,
82 unformat_input_t * input,
83 vlib_cli_command_t * cmd)
84{
85 span_main_t *sm = &span_main;
86 u32 src_sw_if_index = ~0;
87 u32 dst_sw_if_index = ~0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010088 u8 state = 3;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010089
90 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
91 {
92 if (unformat (input, "%U", unformat_vnet_sw_interface,
93 sm->vnet_main, &src_sw_if_index))
94 ;
95 else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
96 sm->vnet_main, &dst_sw_if_index))
97 ;
98 else if (unformat (input, "disable"))
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010099 state = 0;
100 else if (unformat (input, "rx"))
101 state = 1;
102 else if (unformat (input, "tx"))
103 state = 2;
104 else if (unformat (input, "both"))
105 state = 3;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100106 else
107 break;
108 }
109
110 int rv =
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100111 span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100112 if (rv == VNET_API_ERROR_INVALID_INTERFACE)
113 return clib_error_return (0, "Invalid interface");
114 return 0;
115}
116
117/* *INDENT-OFF* */
118VLIB_CLI_COMMAND (set_interface_span_command, static) = {
119 .path = "set interface span",
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100120 .short_help = "set interface span <if-name> [disable | destination <if-name> [both|rx|tx]]",
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100121 .function = set_interface_span_command_fn,
122};
123/* *INDENT-ON* */
124
125static clib_error_t *
126show_interfaces_span_command_fn (vlib_main_t * vm,
127 unformat_input_t * input,
128 vlib_cli_command_t * cmd)
129{
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100130 span_main_t *sm = &span_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100131 span_interface_t *si;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100132 vnet_main_t *vnm = &vnet_main;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100133 u8 header = 1;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100134 char *states[] = { "none", "rx", "tx", "both" };
135 u8 *s = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100136
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100137 /* *INDENT-OFF* */
138 vec_foreach (si, sm->interfaces)
139 if (si->num_rx_mirror_ports || si->num_tx_mirror_ports)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100140 {
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100141 clib_bitmap_t *b;
142 u32 i;
143 b = clib_bitmap_dup_or (si->rx_mirror_ports, si->tx_mirror_ports);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100144 if (header)
145 {
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100146 vlib_cli_output (vm, "%-40s %s", "Source interface",
147 "Mirror interface (direction)");
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100148 header = 0;
149 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100150 s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
151 si - sm->interfaces);
152 clib_bitmap_foreach (i, b, (
153 {
154 int state;
155 state = (clib_bitmap_get (si->rx_mirror_ports, i) +
156 clib_bitmap_get (si->tx_mirror_ports, i) * 2);
157
158 vlib_cli_output (vm, "%-40v %U (%s)", s,
159 format_vnet_sw_if_index_name, vnm, i,
160 states[state]);
161 vec_reset_length (s);
162 }));
163 clib_bitmap_free (b);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100164 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100165 /* *INDENT-ON* */
166 vec_free (s);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100167 return 0;
168}
169
170/* *INDENT-OFF* */
171VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
172 .path = "show interfaces span",
173 .short_help = "Shows SPAN mirror table",
174 .function = show_interfaces_span_command_fn,
175};
176/* *INDENT-ON* */
177
178static clib_error_t *
179span_init (vlib_main_t * vm)
180{
181 span_main_t *sm = &span_main;
182
183 sm->vlib_main = vm;
184 sm->vnet_main = vnet_get_main ();
185
186 return 0;
187}
188
189VLIB_INIT_FUNCTION (span_init);
190
191/*
192 * fd.io coding-style-patch-verification: ON
193 *
194 * Local Variables:
195 * eval: (c-set-style "gnu")
196 * End:
197 */