blob: 6ecd17895efb85f8ceefcbff6106d7a3dee691d5 [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
Eyal Bari001fd402017-07-16 09:34:53 +030024typedef enum
25{
26 SPAN_DISABLE = 0,
27 SPAN_RX = 1,
28 SPAN_TX = 2,
29 SPAN_BOTH = SPAN_RX | SPAN_TX
30} span_state_t;
31
32static_always_inline u32
33span_dst_set (span_mirror_t * sm, u32 dst_sw_if_index, int enable)
34{
35 sm->mirror_ports =
36 clib_bitmap_set (sm->mirror_ports, dst_sw_if_index, enable);
37 u32 last = sm->num_mirror_ports;
38 sm->num_mirror_ports = clib_bitmap_count_set_bits (sm->mirror_ports);
39 return last;
40}
41
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010042int
43span_add_delete_entry (vlib_main_t * vm,
Eyal Bari001fd402017-07-16 09:34:53 +030044 u32 src_sw_if_index, u32 dst_sw_if_index, u8 state,
45 span_feat_t sf)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010046{
47 span_main_t *sm = &span_main;
48
Eyal Bari001fd402017-07-16 09:34:53 +030049 if (state > SPAN_BOTH)
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010050 return VNET_API_ERROR_UNIMPLEMENTED;
51
52 if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010053 || (src_sw_if_index == dst_sw_if_index))
54 return VNET_API_ERROR_INVALID_INTERFACE;
55
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010056 vec_validate_aligned (sm->interfaces, src_sw_if_index,
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010057 CLIB_CACHE_LINE_BYTES);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010058
Eyal Bari001fd402017-07-16 09:34:53 +030059 span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010060
Eyal Bari001fd402017-07-16 09:34:53 +030061 int rx = ! !(state & SPAN_RX);
62 int tx = ! !(state & SPAN_TX);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010063
Eyal Bari001fd402017-07-16 09:34:53 +030064 span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX];
65 span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX];
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010066
Eyal Bari001fd402017-07-16 09:34:53 +030067 u32 last_rx_ports_count = span_dst_set (rxm, dst_sw_if_index, rx);
68 u32 last_tx_ports_count = span_dst_set (txm, dst_sw_if_index, tx);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010069
Eyal Bari001fd402017-07-16 09:34:53 +030070 int enable_rx = last_rx_ports_count == 0 && rxm->num_mirror_ports == 1;
71 int disable_rx = last_rx_ports_count == 1 && rxm->num_mirror_ports == 0;
72 int enable_tx = last_tx_ports_count == 0 && txm->num_mirror_ports == 1;
73 int disable_tx = last_tx_ports_count == 1 && txm->num_mirror_ports == 0;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010074
Eyal Bari001fd402017-07-16 09:34:53 +030075 switch (sf)
76 {
77 case SPAN_FEAT_DEVICE:
78 if (enable_rx || disable_rx)
79 vnet_feature_enable_disable ("device-input", "span-input",
80 src_sw_if_index, rx, 0, 0);
81 if (enable_tx || disable_tx)
82 vnet_feature_enable_disable ("interface-output", "span-output",
83 src_sw_if_index, tx, 0, 0);
84 break;
85 case SPAN_FEAT_L2:
86 if (enable_rx || disable_rx)
87 l2input_intf_bitmap_enable (src_sw_if_index, L2INPUT_FEAT_SPAN, rx);
88 if (enable_tx || disable_tx)
89 l2output_intf_bitmap_enable (src_sw_if_index, L2OUTPUT_FEAT_SPAN, tx);
90 break;
91 default:
92 return VNET_API_ERROR_UNIMPLEMENTED;
93 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +010094
95 if (dst_sw_if_index > sm->max_sw_if_index)
96 sm->max_sw_if_index = dst_sw_if_index;
97
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010098 return 0;
99}
100
101static clib_error_t *
102set_interface_span_command_fn (vlib_main_t * vm,
103 unformat_input_t * input,
104 vlib_cli_command_t * cmd)
105{
106 span_main_t *sm = &span_main;
107 u32 src_sw_if_index = ~0;
108 u32 dst_sw_if_index = ~0;
Eyal Bari001fd402017-07-16 09:34:53 +0300109 u8 state = SPAN_BOTH;
110 span_feat_t sf = SPAN_FEAT_DEVICE;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100111
112 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
113 {
114 if (unformat (input, "%U", unformat_vnet_sw_interface,
115 sm->vnet_main, &src_sw_if_index))
116 ;
117 else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
118 sm->vnet_main, &dst_sw_if_index))
119 ;
120 else if (unformat (input, "disable"))
Eyal Bari001fd402017-07-16 09:34:53 +0300121 state = SPAN_DISABLE;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100122 else if (unformat (input, "rx"))
Eyal Bari001fd402017-07-16 09:34:53 +0300123 state = SPAN_RX;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100124 else if (unformat (input, "tx"))
Eyal Bari001fd402017-07-16 09:34:53 +0300125 state = SPAN_TX;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100126 else if (unformat (input, "both"))
Eyal Bari001fd402017-07-16 09:34:53 +0300127 state = SPAN_BOTH;
128 else if (unformat (input, "l2"))
129 sf = SPAN_FEAT_L2;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100130 else
131 break;
132 }
133
134 int rv =
Eyal Bari001fd402017-07-16 09:34:53 +0300135 span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100136 if (rv == VNET_API_ERROR_INVALID_INTERFACE)
137 return clib_error_return (0, "Invalid interface");
138 return 0;
139}
140
141/* *INDENT-OFF* */
142VLIB_CLI_COMMAND (set_interface_span_command, static) = {
143 .path = "set interface span",
Eyal Bari001fd402017-07-16 09:34:53 +0300144 .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}",
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100145 .function = set_interface_span_command_fn,
146};
147/* *INDENT-ON* */
148
149static clib_error_t *
150show_interfaces_span_command_fn (vlib_main_t * vm,
151 unformat_input_t * input,
152 vlib_cli_command_t * cmd)
153{
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100154 span_main_t *sm = &span_main;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100155 span_interface_t *si;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100156 vnet_main_t *vnm = &vnet_main;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100157 u8 header = 1;
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100158 char *states[] = { "none", "rx", "tx", "both" };
159 u8 *s = 0;
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100160
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100161 /* *INDENT-OFF* */
162 vec_foreach (si, sm->interfaces)
Eyal Bari001fd402017-07-16 09:34:53 +0300163 {
164 span_mirror_t * drxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_RX];
165 span_mirror_t * dtxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_TX];
166
167 span_mirror_t * lrxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_RX];
168 span_mirror_t * ltxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_TX];
169
170 if (drxm->num_mirror_ports || dtxm->num_mirror_ports ||
171 lrxm->num_mirror_ports || ltxm->num_mirror_ports)
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100172 {
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100173 u32 i;
Eyal Bari001fd402017-07-16 09:34:53 +0300174 clib_bitmap_t *d = clib_bitmap_dup_or (drxm->mirror_ports, dtxm->mirror_ports);
175 clib_bitmap_t *l = clib_bitmap_dup_or (lrxm->mirror_ports, ltxm->mirror_ports);
176 clib_bitmap_t *b = clib_bitmap_dup_or (d, l);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100177 if (header)
178 {
Eyal Bari001fd402017-07-16 09:34:53 +0300179 vlib_cli_output (vm, "%-20s %-20s %6s %6s", "Source", "Destination",
180 "Device", "L2");
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100181 header = 0;
182 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100183 s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
184 si - sm->interfaces);
185 clib_bitmap_foreach (i, b, (
186 {
Eyal Bari001fd402017-07-16 09:34:53 +0300187 int device = (clib_bitmap_get (drxm->mirror_ports, i) +
188 clib_bitmap_get (dtxm->mirror_ports, i) * 2);
189 int l2 = (clib_bitmap_get (lrxm->mirror_ports, i) +
190 clib_bitmap_get (ltxm->mirror_ports, i) * 2);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100191
Eyal Bari001fd402017-07-16 09:34:53 +0300192 vlib_cli_output (vm, "%-20v %-20U (%6s) (%6s)", s,
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100193 format_vnet_sw_if_index_name, vnm, i,
Eyal Bari001fd402017-07-16 09:34:53 +0300194 states[device], states[l2]);
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100195 vec_reset_length (s);
196 }));
197 clib_bitmap_free (b);
Eyal Bari001fd402017-07-16 09:34:53 +0300198 clib_bitmap_free (l);
199 clib_bitmap_free (d);
200 }
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100201 }
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +0100202 /* *INDENT-ON* */
203 vec_free (s);
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100204 return 0;
205}
206
207/* *INDENT-OFF* */
208VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400209 .path = "show interface span",
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100210 .short_help = "Shows SPAN mirror table",
211 .function = show_interfaces_span_command_fn,
212};
213/* *INDENT-ON* */
214
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */