blob: 85ee0c2e5662499930455744a9a00dc1c889ccdb [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>
Eyal Bari001fd402017-07-16 09:34:53 +030019#include <vnet/l2/l2_input.h>
20#include <vnet/l2/l2_output.h>
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010021
22#include <vnet/span/span.h>
23
Dave Wallace71612d62017-10-24 01:32:41 -040024span_main_t span_main;
25
Eyal Bari001fd402017-07-16 09:34:53 +030026typedef enum
27{
28 SPAN_DISABLE = 0,
29 SPAN_RX = 1,
30 SPAN_TX = 2,
31 SPAN_BOTH = SPAN_RX | SPAN_TX
32} span_state_t;
33
34static_always_inline u32
35span_dst_set (span_mirror_t * sm, u32 dst_sw_if_index, int enable)
36{
Eyal Bari4a7d50e2017-07-30 13:27:46 +030037 if (dst_sw_if_index == ~0)
38 {
39 ASSERT (enable == 0);
40 clib_bitmap_zero (sm->mirror_ports);
41 }
42 else
43 sm->mirror_ports =
44 clib_bitmap_set (sm->mirror_ports, dst_sw_if_index, enable);
45
Eyal Bari001fd402017-07-16 09:34:53 +030046 u32 last = sm->num_mirror_ports;
47 sm->num_mirror_ports = clib_bitmap_count_set_bits (sm->mirror_ports);
48 return last;
49}
50
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010051int
52span_add_delete_entry (vlib_main_t * vm,
Eyal Bari001fd402017-07-16 09:34:53 +030053 u32 src_sw_if_index, u32 dst_sw_if_index, u8 state,
54 span_feat_t sf)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010055{
56 span_main_t *sm = &span_main;
57
Eyal Bari001fd402017-07-16 09:34:53 +030058 if (state > SPAN_BOTH)
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010059 return VNET_API_ERROR_UNIMPLEMENTED;
60
61 if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010062 || (src_sw_if_index == dst_sw_if_index))
63 return VNET_API_ERROR_INVALID_INTERFACE;
64
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010065 vec_validate_aligned (sm->interfaces, src_sw_if_index,
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010066 CLIB_CACHE_LINE_BYTES);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010067
Eyal Bari001fd402017-07-16 09:34:53 +030068 span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010069
Eyal Bari001fd402017-07-16 09:34:53 +030070 int rx = ! !(state & SPAN_RX);
71 int tx = ! !(state & SPAN_TX);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010072
Eyal Bari001fd402017-07-16 09:34:53 +030073 span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX];
74 span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX];
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010075
Eyal Bari001fd402017-07-16 09:34:53 +030076 u32 last_rx_ports_count = span_dst_set (rxm, dst_sw_if_index, rx);
77 u32 last_tx_ports_count = span_dst_set (txm, dst_sw_if_index, tx);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010078
Eyal Bari001fd402017-07-16 09:34:53 +030079 int enable_rx = last_rx_ports_count == 0 && rxm->num_mirror_ports == 1;
Eyal Bari4a7d50e2017-07-30 13:27:46 +030080 int disable_rx = last_rx_ports_count > 0 && rxm->num_mirror_ports == 0;
Eyal Bari001fd402017-07-16 09:34:53 +030081 int enable_tx = last_tx_ports_count == 0 && txm->num_mirror_ports == 1;
Eyal Bari4a7d50e2017-07-30 13:27:46 +030082 int disable_tx = last_tx_ports_count > 0 && txm->num_mirror_ports == 0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010083
Eyal Bari001fd402017-07-16 09:34:53 +030084 switch (sf)
85 {
86 case SPAN_FEAT_DEVICE:
87 if (enable_rx || disable_rx)
88 vnet_feature_enable_disable ("device-input", "span-input",
89 src_sw_if_index, rx, 0, 0);
Damjan Marion38c61912023-10-17 16:06:26 +000090 if (enable_rx || disable_rx)
91 vnet_feature_enable_disable ("port-rx-eth", "span-input",
92 src_sw_if_index, rx, 0, 0);
Eyal Bari001fd402017-07-16 09:34:53 +030093 if (enable_tx || disable_tx)
94 vnet_feature_enable_disable ("interface-output", "span-output",
95 src_sw_if_index, tx, 0, 0);
96 break;
97 case SPAN_FEAT_L2:
98 if (enable_rx || disable_rx)
99 l2input_intf_bitmap_enable (src_sw_if_index, L2INPUT_FEAT_SPAN, rx);
100 if (enable_tx || disable_tx)
101 l2output_intf_bitmap_enable (src_sw_if_index, L2OUTPUT_FEAT_SPAN, tx);
102 break;
103 default:
104 return VNET_API_ERROR_UNIMPLEMENTED;
105 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100106
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300107 if (dst_sw_if_index != ~0 && dst_sw_if_index > sm->max_sw_if_index)
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100108 sm->max_sw_if_index = dst_sw_if_index;
109
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100110 return 0;
111}
112
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300113static uword
114unformat_span_state (unformat_input_t * input, va_list * args)
115{
116 span_state_t *state = va_arg (*args, span_state_t *);
117 if (unformat (input, "disable"))
118 *state = SPAN_DISABLE;
119 else if (unformat (input, "rx"))
120 *state = SPAN_RX;
121 else if (unformat (input, "tx"))
122 *state = SPAN_TX;
123 else if (unformat (input, "both"))
124 *state = SPAN_BOTH;
125 else
126 return 0;
127 return 1;
128}
129
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100130static clib_error_t *
131set_interface_span_command_fn (vlib_main_t * vm,
132 unformat_input_t * input,
133 vlib_cli_command_t * cmd)
134{
135 span_main_t *sm = &span_main;
136 u32 src_sw_if_index = ~0;
137 u32 dst_sw_if_index = ~0;
Eyal Bari001fd402017-07-16 09:34:53 +0300138 span_feat_t sf = SPAN_FEAT_DEVICE;
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300139 span_state_t state = SPAN_BOTH;
140 int state_set = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100141
142 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
143 {
144 if (unformat (input, "%U", unformat_vnet_sw_interface,
145 sm->vnet_main, &src_sw_if_index))
146 ;
147 else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
148 sm->vnet_main, &dst_sw_if_index))
149 ;
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300150 else if (unformat (input, "%U", unformat_span_state, &state))
151 {
152 if (state_set)
153 return clib_error_return (0, "Multiple mirror states in input");
154 state_set = 1;
155 }
Eyal Bari001fd402017-07-16 09:34:53 +0300156 else if (unformat (input, "l2"))
157 sf = SPAN_FEAT_L2;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100158 else
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300159 return clib_error_return (0, "Invalid input");
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100160 }
161
162 int rv =
Eyal Bari001fd402017-07-16 09:34:53 +0300163 span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100164 if (rv == VNET_API_ERROR_INVALID_INTERFACE)
165 return clib_error_return (0, "Invalid interface");
166 return 0;
167}
168
169/* *INDENT-OFF* */
170VLIB_CLI_COMMAND (set_interface_span_command, static) = {
171 .path = "set interface span",
Eyal Bari001fd402017-07-16 09:34:53 +0300172 .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}",
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100173 .function = set_interface_span_command_fn,
174};
175/* *INDENT-ON* */
176
177static clib_error_t *
178show_interfaces_span_command_fn (vlib_main_t * vm,
179 unformat_input_t * input,
180 vlib_cli_command_t * cmd)
181{
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100182 span_main_t *sm = &span_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100183 span_interface_t *si;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100184 vnet_main_t *vnm = &vnet_main;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100185 u8 header = 1;
Eyal Bari4a7d50e2017-07-30 13:27:46 +0300186 static const char *states[] = {
187 [SPAN_DISABLE] = "none",
188 [SPAN_RX] = "rx",
189 [SPAN_TX] = "tx",
190 [SPAN_BOTH] = "both"
191 };
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100192 u8 *s = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100193
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100194 /* *INDENT-OFF* */
195 vec_foreach (si, sm->interfaces)
Eyal Bari001fd402017-07-16 09:34:53 +0300196 {
197 span_mirror_t * drxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_RX];
198 span_mirror_t * dtxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_TX];
199
200 span_mirror_t * lrxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_RX];
201 span_mirror_t * ltxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_TX];
202
203 if (drxm->num_mirror_ports || dtxm->num_mirror_ports ||
204 lrxm->num_mirror_ports || ltxm->num_mirror_ports)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100205 {
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100206 u32 i;
Eyal Bari001fd402017-07-16 09:34:53 +0300207 clib_bitmap_t *d = clib_bitmap_dup_or (drxm->mirror_ports, dtxm->mirror_ports);
208 clib_bitmap_t *l = clib_bitmap_dup_or (lrxm->mirror_ports, ltxm->mirror_ports);
209 clib_bitmap_t *b = clib_bitmap_dup_or (d, l);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100210 if (header)
211 {
Igor Mikhailov (imichail)0ac827e2019-01-11 14:03:53 -0800212 vlib_cli_output (vm, "%-32s %-32s %6s %6s", "Source", "Destination",
Eyal Bari001fd402017-07-16 09:34:53 +0300213 "Device", "L2");
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100214 header = 0;
215 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100216 s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
217 si - sm->interfaces);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100218 clib_bitmap_foreach (i, b)
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100219 {
Eyal Bari001fd402017-07-16 09:34:53 +0300220 int device = (clib_bitmap_get (drxm->mirror_ports, i) +
221 clib_bitmap_get (dtxm->mirror_ports, i) * 2);
222 int l2 = (clib_bitmap_get (lrxm->mirror_ports, i) +
223 clib_bitmap_get (ltxm->mirror_ports, i) * 2);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100224
Igor Mikhailov (imichail)0ac827e2019-01-11 14:03:53 -0800225 vlib_cli_output (vm, "%-32v %-32U (%6s) (%6s)", s,
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100226 format_vnet_sw_if_index_name, vnm, i,
Eyal Bari001fd402017-07-16 09:34:53 +0300227 states[device], states[l2]);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100228 vec_reset_length (s);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100229 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100230 clib_bitmap_free (b);
Eyal Bari001fd402017-07-16 09:34:53 +0300231 clib_bitmap_free (l);
232 clib_bitmap_free (d);
233 }
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100234 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100235 /* *INDENT-ON* */
236 vec_free (s);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100237 return 0;
238}
239
240/* *INDENT-OFF* */
241VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400242 .path = "show interface span",
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100243 .short_help = "Shows SPAN mirror table",
244 .function = show_interfaces_span_command_fn,
245};
246/* *INDENT-ON* */
247
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100248/*
249 * fd.io coding-style-patch-verification: ON
250 *
251 * Local Variables:
252 * eval: (c-set-style "gnu")
253 * End:
254 */