SPAN:add l2 mirror
[vpp.git] / src / 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 #include <vnet/l2/l2_input.h>
20 #include <vnet/l2/l2_output.h>
21
22 #include <vnet/span/span.h>
23
24 typedef 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
32 static_always_inline u32
33 span_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
42 int
43 span_add_delete_entry (vlib_main_t * vm,
44                        u32 src_sw_if_index, u32 dst_sw_if_index, u8 state,
45                        span_feat_t sf)
46 {
47   span_main_t *sm = &span_main;
48
49   if (state > SPAN_BOTH)
50     return VNET_API_ERROR_UNIMPLEMENTED;
51
52   if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
53       || (src_sw_if_index == dst_sw_if_index))
54     return VNET_API_ERROR_INVALID_INTERFACE;
55
56   vec_validate_aligned (sm->interfaces, src_sw_if_index,
57                         CLIB_CACHE_LINE_BYTES);
58
59   span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
60
61   int rx = ! !(state & SPAN_RX);
62   int tx = ! !(state & SPAN_TX);
63
64   span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX];
65   span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX];
66
67   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);
69
70   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;
74
75   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     }
94
95   if (dst_sw_if_index > sm->max_sw_if_index)
96     sm->max_sw_if_index = dst_sw_if_index;
97
98   return 0;
99 }
100
101 static clib_error_t *
102 set_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;
109   u8 state = SPAN_BOTH;
110   span_feat_t sf = SPAN_FEAT_DEVICE;
111
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"))
121         state = SPAN_DISABLE;
122       else if (unformat (input, "rx"))
123         state = SPAN_RX;
124       else if (unformat (input, "tx"))
125         state = SPAN_TX;
126       else if (unformat (input, "both"))
127         state = SPAN_BOTH;
128       else if (unformat (input, "l2"))
129         sf = SPAN_FEAT_L2;
130       else
131         break;
132     }
133
134   int rv =
135     span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf);
136   if (rv == VNET_API_ERROR_INVALID_INTERFACE)
137     return clib_error_return (0, "Invalid interface");
138   return 0;
139 }
140
141 /* *INDENT-OFF* */
142 VLIB_CLI_COMMAND (set_interface_span_command, static) = {
143   .path = "set interface span",
144   .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}",
145   .function = set_interface_span_command_fn,
146 };
147 /* *INDENT-ON* */
148
149 static clib_error_t *
150 show_interfaces_span_command_fn (vlib_main_t * vm,
151                                  unformat_input_t * input,
152                                  vlib_cli_command_t * cmd)
153 {
154   span_main_t *sm = &span_main;
155   span_interface_t *si;
156   vnet_main_t *vnm = &vnet_main;
157   u8 header = 1;
158   char *states[] = { "none", "rx", "tx", "both" };
159   u8 *s = 0;
160
161   /* *INDENT-OFF* */
162   vec_foreach (si, sm->interfaces)
163   {
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)
172       {
173         u32 i;
174         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);
177         if (header)
178           {
179             vlib_cli_output (vm, "%-20s %-20s  %6s   %6s", "Source", "Destination",
180                              "Device", "L2");
181             header = 0;
182           }
183         s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
184                     si - sm->interfaces);
185         clib_bitmap_foreach (i, b, (
186           {
187             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);
191
192             vlib_cli_output (vm, "%-20v %-20U (%6s) (%6s)", s,
193                              format_vnet_sw_if_index_name, vnm, i,
194                              states[device], states[l2]);
195             vec_reset_length (s);
196           }));
197         clib_bitmap_free (b);
198         clib_bitmap_free (l);
199         clib_bitmap_free (d);
200       }
201       }
202   /* *INDENT-ON* */
203   vec_free (s);
204   return 0;
205 }
206
207 /* *INDENT-OFF* */
208 VLIB_CLI_COMMAND (show_interfaces_span_command, static) = {
209   .path = "show interface span",
210   .short_help = "Shows SPAN mirror table",
211   .function = show_interfaces_span_command_fn,
212 };
213 /* *INDENT-ON* */
214
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "gnu")
220  * End:
221  */