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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vppinfra/error.h>
18 #include <vnet/feature/feature.h>
19 #include <vnet/l2/l2_input.h>
20 #include <vnet/l2/l2_output.h>
22 #include <vnet/span/span.h>
29 SPAN_BOTH = SPAN_RX | SPAN_TX
32 static_always_inline u32
33 span_dst_set (span_mirror_t * sm, u32 dst_sw_if_index, int enable)
35 if (dst_sw_if_index == ~0)
38 clib_bitmap_zero (sm->mirror_ports);
42 clib_bitmap_set (sm->mirror_ports, dst_sw_if_index, enable);
44 u32 last = sm->num_mirror_ports;
45 sm->num_mirror_ports = clib_bitmap_count_set_bits (sm->mirror_ports);
50 span_add_delete_entry (vlib_main_t * vm,
51 u32 src_sw_if_index, u32 dst_sw_if_index, u8 state,
54 span_main_t *sm = &span_main;
56 if (state > SPAN_BOTH)
57 return VNET_API_ERROR_UNIMPLEMENTED;
59 if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
60 || (src_sw_if_index == dst_sw_if_index))
61 return VNET_API_ERROR_INVALID_INTERFACE;
63 vec_validate_aligned (sm->interfaces, src_sw_if_index,
64 CLIB_CACHE_LINE_BYTES);
66 span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
68 int rx = ! !(state & SPAN_RX);
69 int tx = ! !(state & SPAN_TX);
71 span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX];
72 span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX];
74 u32 last_rx_ports_count = span_dst_set (rxm, dst_sw_if_index, rx);
75 u32 last_tx_ports_count = span_dst_set (txm, dst_sw_if_index, tx);
77 int enable_rx = last_rx_ports_count == 0 && rxm->num_mirror_ports == 1;
78 int disable_rx = last_rx_ports_count > 0 && rxm->num_mirror_ports == 0;
79 int enable_tx = last_tx_ports_count == 0 && txm->num_mirror_ports == 1;
80 int disable_tx = last_tx_ports_count > 0 && txm->num_mirror_ports == 0;
84 case SPAN_FEAT_DEVICE:
85 if (enable_rx || disable_rx)
86 vnet_feature_enable_disable ("device-input", "span-input",
87 src_sw_if_index, rx, 0, 0);
88 if (enable_tx || disable_tx)
89 vnet_feature_enable_disable ("interface-output", "span-output",
90 src_sw_if_index, tx, 0, 0);
93 if (enable_rx || disable_rx)
94 l2input_intf_bitmap_enable (src_sw_if_index, L2INPUT_FEAT_SPAN, rx);
95 if (enable_tx || disable_tx)
96 l2output_intf_bitmap_enable (src_sw_if_index, L2OUTPUT_FEAT_SPAN, tx);
99 return VNET_API_ERROR_UNIMPLEMENTED;
102 if (dst_sw_if_index != ~0 && dst_sw_if_index > sm->max_sw_if_index)
103 sm->max_sw_if_index = dst_sw_if_index;
109 unformat_span_state (unformat_input_t * input, va_list * args)
111 span_state_t *state = va_arg (*args, span_state_t *);
112 if (unformat (input, "disable"))
113 *state = SPAN_DISABLE;
114 else if (unformat (input, "rx"))
116 else if (unformat (input, "tx"))
118 else if (unformat (input, "both"))
125 static clib_error_t *
126 set_interface_span_command_fn (vlib_main_t * vm,
127 unformat_input_t * input,
128 vlib_cli_command_t * cmd)
130 span_main_t *sm = &span_main;
131 u32 src_sw_if_index = ~0;
132 u32 dst_sw_if_index = ~0;
133 span_feat_t sf = SPAN_FEAT_DEVICE;
134 span_state_t state = SPAN_BOTH;
137 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
139 if (unformat (input, "%U", unformat_vnet_sw_interface,
140 sm->vnet_main, &src_sw_if_index))
142 else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
143 sm->vnet_main, &dst_sw_if_index))
145 else if (unformat (input, "%U", unformat_span_state, &state))
148 return clib_error_return (0, "Multiple mirror states in input");
151 else if (unformat (input, "l2"))
154 return clib_error_return (0, "Invalid input");
158 span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf);
159 if (rv == VNET_API_ERROR_INVALID_INTERFACE)
160 return clib_error_return (0, "Invalid interface");
165 VLIB_CLI_COMMAND (set_interface_span_command, static) = {
166 .path = "set interface span",
167 .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}",
168 .function = set_interface_span_command_fn,
172 static clib_error_t *
173 show_interfaces_span_command_fn (vlib_main_t * vm,
174 unformat_input_t * input,
175 vlib_cli_command_t * cmd)
177 span_main_t *sm = &span_main;
178 span_interface_t *si;
179 vnet_main_t *vnm = &vnet_main;
181 static const char *states[] = {
182 [SPAN_DISABLE] = "none",
190 vec_foreach (si, sm->interfaces)
192 span_mirror_t * drxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_RX];
193 span_mirror_t * dtxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_TX];
195 span_mirror_t * lrxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_RX];
196 span_mirror_t * ltxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_TX];
198 if (drxm->num_mirror_ports || dtxm->num_mirror_ports ||
199 lrxm->num_mirror_ports || ltxm->num_mirror_ports)
202 clib_bitmap_t *d = clib_bitmap_dup_or (drxm->mirror_ports, dtxm->mirror_ports);
203 clib_bitmap_t *l = clib_bitmap_dup_or (lrxm->mirror_ports, ltxm->mirror_ports);
204 clib_bitmap_t *b = clib_bitmap_dup_or (d, l);
207 vlib_cli_output (vm, "%-20s %-20s %6s %6s", "Source", "Destination",
211 s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
212 si - sm->interfaces);
213 clib_bitmap_foreach (i, b, (
215 int device = (clib_bitmap_get (drxm->mirror_ports, i) +
216 clib_bitmap_get (dtxm->mirror_ports, i) * 2);
217 int l2 = (clib_bitmap_get (lrxm->mirror_ports, i) +
218 clib_bitmap_get (ltxm->mirror_ports, i) * 2);
220 vlib_cli_output (vm, "%-20v %-20U (%6s) (%6s)", s,
221 format_vnet_sw_if_index_name, vnm, i,
222 states[device], states[l2]);
223 vec_reset_length (s);
225 clib_bitmap_free (b);
226 clib_bitmap_free (l);
227 clib_bitmap_free (d);
236 VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
237 .path = "show interface span",
238 .short_help = "Shows SPAN mirror table",
239 .function = show_interfaces_span_command_fn,
244 * fd.io coding-style-patch-verification: ON
247 * eval: (c-set-style "gnu")