FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
span.c
Go to the documentation of this file.
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 
25 
26 typedef enum
27 {
29  SPAN_RX = 1,
30  SPAN_TX = 2,
32 } span_state_t;
33 
35 span_dst_set (span_mirror_t * sm, u32 dst_sw_if_index, int enable)
36 {
37  if (dst_sw_if_index == ~0)
38  {
39  ASSERT (enable == 0);
41  }
42  else
43  sm->mirror_ports =
44  clib_bitmap_set (sm->mirror_ports, dst_sw_if_index, enable);
45 
48  return last;
49 }
50 
51 int
53  u32 src_sw_if_index, u32 dst_sw_if_index, u8 state,
54  span_feat_t sf)
55 {
56  span_main_t *sm = &span_main;
57 
58  if (state > SPAN_BOTH)
59  return VNET_API_ERROR_UNIMPLEMENTED;
60 
61  if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0)
62  || (src_sw_if_index == dst_sw_if_index))
63  return VNET_API_ERROR_INVALID_INTERFACE;
64 
65  vec_validate_aligned (sm->interfaces, src_sw_if_index,
67 
68  span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index);
69 
70  int rx = ! !(state & SPAN_RX);
71  int tx = ! !(state & SPAN_TX);
72 
73  span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX];
74  span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX];
75 
76  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);
78 
79  int enable_rx = last_rx_ports_count == 0 && rxm->num_mirror_ports == 1;
80  int disable_rx = last_rx_ports_count > 0 && rxm->num_mirror_ports == 0;
81  int enable_tx = last_tx_ports_count == 0 && txm->num_mirror_ports == 1;
82  int disable_tx = last_tx_ports_count > 0 && txm->num_mirror_ports == 0;
83 
84  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);
90  if (enable_tx || disable_tx)
91  vnet_feature_enable_disable ("interface-output", "span-output",
92  src_sw_if_index, tx, 0, 0);
93  break;
94  case SPAN_FEAT_L2:
95  if (enable_rx || disable_rx)
96  l2input_intf_bitmap_enable (src_sw_if_index, L2INPUT_FEAT_SPAN, rx);
97  if (enable_tx || disable_tx)
98  l2output_intf_bitmap_enable (src_sw_if_index, L2OUTPUT_FEAT_SPAN, tx);
99  break;
100  default:
101  return VNET_API_ERROR_UNIMPLEMENTED;
102  }
103 
104  if (dst_sw_if_index != ~0 && dst_sw_if_index > sm->max_sw_if_index)
105  sm->max_sw_if_index = dst_sw_if_index;
106 
107  return 0;
108 }
109 
110 static uword
111 unformat_span_state (unformat_input_t * input, va_list * args)
112 {
113  span_state_t *state = va_arg (*args, span_state_t *);
114  if (unformat (input, "disable"))
115  *state = SPAN_DISABLE;
116  else if (unformat (input, "rx"))
117  *state = SPAN_RX;
118  else if (unformat (input, "tx"))
119  *state = SPAN_TX;
120  else if (unformat (input, "both"))
121  *state = SPAN_BOTH;
122  else
123  return 0;
124  return 1;
125 }
126 
127 static clib_error_t *
129  unformat_input_t * input,
130  vlib_cli_command_t * cmd)
131 {
132  span_main_t *sm = &span_main;
133  u32 src_sw_if_index = ~0;
134  u32 dst_sw_if_index = ~0;
137  int state_set = 0;
138 
140  {
141  if (unformat (input, "%U", unformat_vnet_sw_interface,
142  sm->vnet_main, &src_sw_if_index))
143  ;
144  else if (unformat (input, "destination %U", unformat_vnet_sw_interface,
145  sm->vnet_main, &dst_sw_if_index))
146  ;
147  else if (unformat (input, "%U", unformat_span_state, &state))
148  {
149  if (state_set)
150  return clib_error_return (0, "Multiple mirror states in input");
151  state_set = 1;
152  }
153  else if (unformat (input, "l2"))
154  sf = SPAN_FEAT_L2;
155  else
156  return clib_error_return (0, "Invalid input");
157  }
158 
159  int rv =
160  span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf);
161  if (rv == VNET_API_ERROR_INVALID_INTERFACE)
162  return clib_error_return (0, "Invalid interface");
163  return 0;
164 }
165 
166 /* *INDENT-OFF* */
168  .path = "set interface span",
169  .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}",
170  .function = set_interface_span_command_fn,
171 };
172 /* *INDENT-ON* */
173 
174 static clib_error_t *
176  unformat_input_t * input,
177  vlib_cli_command_t * cmd)
178 {
179  span_main_t *sm = &span_main;
181  vnet_main_t *vnm = &vnet_main;
182  u8 header = 1;
183  static const char *states[] = {
184  [SPAN_DISABLE] = "none",
185  [SPAN_RX] = "rx",
186  [SPAN_TX] = "tx",
187  [SPAN_BOTH] = "both"
188  };
189  u8 *s = 0;
190 
191  /* *INDENT-OFF* */
192  vec_foreach (si, sm->interfaces)
193  {
194  span_mirror_t * drxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_RX];
195  span_mirror_t * dtxm = &si->mirror_rxtx[SPAN_FEAT_DEVICE][VLIB_TX];
196 
197  span_mirror_t * lrxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_RX];
198  span_mirror_t * ltxm = &si->mirror_rxtx[SPAN_FEAT_L2][VLIB_TX];
199 
200  if (drxm->num_mirror_ports || dtxm->num_mirror_ports ||
201  lrxm->num_mirror_ports || ltxm->num_mirror_ports)
202  {
203  u32 i;
207  if (header)
208  {
209  vlib_cli_output (vm, "%-32s %-32s %6s %6s", "Source", "Destination",
210  "Device", "L2");
211  header = 0;
212  }
213  s = format (s, "%U", format_vnet_sw_if_index_name, vnm,
214  si - sm->interfaces);
216  {
217  int device = (clib_bitmap_get (drxm->mirror_ports, i) +
218  clib_bitmap_get (dtxm->mirror_ports, i) * 2);
219  int l2 = (clib_bitmap_get (lrxm->mirror_ports, i) +
220  clib_bitmap_get (ltxm->mirror_ports, i) * 2);
221 
222  vlib_cli_output (vm, "%-32v %-32U (%6s) (%6s)", s,
224  states[device], states[l2]);
225  vec_reset_length (s);
226  }
228  clib_bitmap_free (l);
229  clib_bitmap_free (d);
230  }
231  }
232  /* *INDENT-ON* */
233  vec_free (s);
234  return 0;
235 }
236 
237 /* *INDENT-OFF* */
239  .path = "show interface span",
240  .short_help = "Shows SPAN mirror table",
242 };
243 /* *INDENT-ON* */
244 
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "gnu")
250  * End:
251  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
vlib.h
show_interfaces_span_command_fn
static clib_error_t * show_interfaces_span_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: span.c:175
SPAN_RX
@ SPAN_RX
Definition: span.c:29
SPAN_TX
@ SPAN_TX
Definition: span.c:30
clib_bitmap_t
uword clib_bitmap_t
Definition: bitmap.h:50
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
span_main_t
Definition: span.h:41
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
span_mirror_t::num_mirror_ports
u32 num_mirror_ports
Definition: span.h:33
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
SPAN_FEAT_DEVICE
@ SPAN_FEAT_DEVICE
Definition: span.h:25
state
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
span_main
span_main_t span_main
Definition: span.c:24
unformat_input_t
struct _unformat_input_t unformat_input_t
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
clib_bitmap_get
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
error.h
feature.h
span_dst_set
static_always_inline u32 span_dst_set(span_mirror_t *sm, u32 dst_sw_if_index, int enable)
Definition: span.c:35
l2_output.h
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
clib_bitmap_count_set_bits
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:468
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
set_interface_span_command
static vlib_cli_command_t set_interface_span_command
(constructor) VLIB_CLI_COMMAND (set_interface_span_command)
Definition: span.c:167
vec_validate_aligned
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:534
span_mirror_t::mirror_ports
clib_bitmap_t * mirror_ports
Definition: span.h:32
static_always_inline
#define static_always_inline
Definition: clib.h:112
clib_bitmap_free
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
uword
u64 uword
Definition: types.h:112
last
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
span.h
l2input_intf_bitmap_enable
u32 l2input_intf_bitmap_enable(u32 sw_if_index, l2input_feat_masks_t feature_bitmap, u32 enable)
Enable (or disable) the feature in the bitmap for the given interface.
Definition: l2_input.c:177
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
clib_bitmap_set
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap.
Definition: bitmap.h:167
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
l2_input.h
show_interfaces_span_command
static vlib_cli_command_t show_interfaces_span_command
(constructor) VLIB_CLI_COMMAND (show_interfaces_span_command)
Definition: span.c:238
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
l2output_intf_bitmap_enable
void l2output_intf_bitmap_enable(u32 sw_if_index, l2output_feat_masks_t feature_bitmap, u32 enable)
Enable (or disable) the feature in the bitmap for the given interface.
Definition: l2_output.c:625
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
SPAN_FEAT_L2
@ SPAN_FEAT_L2
Definition: span.h:26
u32
unsigned int u32
Definition: types.h:88
SPAN_DISABLE
@ SPAN_DISABLE
Definition: span.c:28
unformat_span_state
static uword unformat_span_state(unformat_input_t *input, va_list *args)
Definition: span.c:111
span_add_delete_entry
int span_add_delete_entry(vlib_main_t *vm, u32 src_sw_if_index, u32 dst_sw_if_index, u8 state, span_feat_t sf)
Definition: span.c:52
si
vnet_sw_interface_t * si
Definition: interface_output.c:398
span_mirror_t
Definition: span.h:30
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
span_main_t::vnet_main
vnet_main_t * vnet_main
Definition: span.h:55
vnet_feature_enable_disable
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: pnat_test_stubs.h:50
SPAN_BOTH
@ SPAN_BOTH
Definition: span.c:31
vnet_main
vnet_main_t vnet_main
Definition: misc.c:43
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
set_interface_span_command_fn
static clib_error_t * set_interface_span_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: span.c:128
rv
int __clib_unused rv
Definition: application.c:491
clib_bitmap_zero
#define clib_bitmap_zero(v)
Clear a bitmap.
Definition: bitmap.h:102
span_interface_t
Definition: span.h:36
span_main_t::max_sw_if_index
u32 max_sw_if_index
Definition: span.h:51
clib_bitmap_foreach
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
vlib_cli_command_t
Definition: cli.h:92
span_main_t::interfaces
span_interface_t * interfaces
Definition: span.h:48
span_state_t
span_state_t
Definition: span.c:26
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
clib_bitmap_dup_or
static uword * clib_bitmap_dup_or(uword *ai, uword *bi)
Logical operator across two bitmaps which duplicates the first bitmap.
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
span_feat_t
span_feat_t
Definition: span.h:23