Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 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> |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 19 | #include <vnet/l2/l2_input.h> |
| 20 | #include <vnet/l2/l2_output.h> |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 21 | |
| 22 | #include <vnet/span/span.h> |
| 23 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 42 | int |
| 43 | span_add_delete_entry (vlib_main_t * vm, |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 44 | u32 src_sw_if_index, u32 dst_sw_if_index, u8 state, |
| 45 | span_feat_t sf) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 46 | { |
| 47 | span_main_t *sm = &span_main; |
| 48 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 49 | if (state > SPAN_BOTH) |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 50 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 51 | |
| 52 | if ((src_sw_if_index == ~0) || (dst_sw_if_index == ~0 && state > 0) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 53 | || (src_sw_if_index == dst_sw_if_index)) |
| 54 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 55 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 56 | vec_validate_aligned (sm->interfaces, src_sw_if_index, |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 57 | CLIB_CACHE_LINE_BYTES); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 58 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 59 | span_interface_t *si = vec_elt_at_index (sm->interfaces, src_sw_if_index); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 60 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 61 | int rx = ! !(state & SPAN_RX); |
| 62 | int tx = ! !(state & SPAN_TX); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 63 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 64 | span_mirror_t *rxm = &si->mirror_rxtx[sf][VLIB_RX]; |
| 65 | span_mirror_t *txm = &si->mirror_rxtx[sf][VLIB_TX]; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 66 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 69 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 74 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 94 | |
| 95 | if (dst_sw_if_index > sm->max_sw_if_index) |
| 96 | sm->max_sw_if_index = dst_sw_if_index; |
| 97 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 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; |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 109 | u8 state = SPAN_BOTH; |
| 110 | span_feat_t sf = SPAN_FEAT_DEVICE; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 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")) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 121 | state = SPAN_DISABLE; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 122 | else if (unformat (input, "rx")) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 123 | state = SPAN_RX; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 124 | else if (unformat (input, "tx")) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 125 | state = SPAN_TX; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 126 | else if (unformat (input, "both")) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 127 | state = SPAN_BOTH; |
| 128 | else if (unformat (input, "l2")) |
| 129 | sf = SPAN_FEAT_L2; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 130 | else |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | int rv = |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 135 | span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state, sf); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 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", |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 144 | .short_help = "set interface span <if-name> [l2] {disable | destination <if-name> [both|rx|tx]}", |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 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 | { |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 154 | span_main_t *sm = &span_main; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 155 | span_interface_t *si; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 156 | vnet_main_t *vnm = &vnet_main; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 157 | u8 header = 1; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 158 | char *states[] = { "none", "rx", "tx", "both" }; |
| 159 | u8 *s = 0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 160 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 161 | /* *INDENT-OFF* */ |
| 162 | vec_foreach (si, sm->interfaces) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 172 | { |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 173 | u32 i; |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 177 | if (header) |
| 178 | { |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 179 | vlib_cli_output (vm, "%-20s %-20s %6s %6s", "Source", "Destination", |
| 180 | "Device", "L2"); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 181 | header = 0; |
| 182 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 183 | s = format (s, "%U", format_vnet_sw_if_index_name, vnm, |
| 184 | si - sm->interfaces); |
| 185 | clib_bitmap_foreach (i, b, ( |
| 186 | { |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 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); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 191 | |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 192 | vlib_cli_output (vm, "%-20v %-20U (%6s) (%6s)", s, |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 193 | format_vnet_sw_if_index_name, vnm, i, |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 194 | states[device], states[l2]); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 195 | vec_reset_length (s); |
| 196 | })); |
| 197 | clib_bitmap_free (b); |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame^] | 198 | clib_bitmap_free (l); |
| 199 | clib_bitmap_free (d); |
| 200 | } |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 201 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 202 | /* *INDENT-ON* */ |
| 203 | vec_free (s); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* *INDENT-OFF* */ |
| 208 | VLIB_CLI_COMMAND (show_interfaces_span_command, static) = { |
Dave Barach | 13ad1f0 | 2017-03-26 19:36:18 -0400 | [diff] [blame] | 209 | .path = "show interface span", |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 210 | .short_help = "Shows SPAN mirror table", |
| 211 | .function = show_interfaces_span_command_fn, |
| 212 | }; |
| 213 | /* *INDENT-ON* */ |
| 214 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 215 | /* |
| 216 | * fd.io coding-style-patch-verification: ON |
| 217 | * |
| 218 | * Local Variables: |
| 219 | * eval: (c-set-style "gnu") |
| 220 | * End: |
| 221 | */ |