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> |
| 19 | |
| 20 | #include <vnet/span/span.h> |
| 21 | |
| 22 | int |
| 23 | span_add_delete_entry (vlib_main_t * vm, |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 24 | u32 src_sw_if_index, u32 dst_sw_if_index, u8 state) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 25 | { |
| 26 | span_main_t *sm = &span_main; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 27 | span_interface_t *si; |
| 28 | u32 new_num_rx_mirror_ports, new_num_tx_mirror_ports; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 29 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 30 | if (state > 3) |
| 31 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 32 | |
| 33 | 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] | 34 | || (src_sw_if_index == dst_sw_if_index)) |
| 35 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 36 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 37 | vnet_sw_interface_t *sw_if; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 38 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 39 | sw_if = vnet_get_sw_interface (vnet_get_main (), src_sw_if_index); |
| 40 | if (sw_if->type == VNET_SW_INTERFACE_TYPE_SUB) |
| 41 | return VNET_API_ERROR_UNIMPLEMENTED; |
| 42 | |
| 43 | vec_validate_aligned (sm->interfaces, src_sw_if_index, |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 44 | CLIB_CACHE_LINE_BYTES); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 45 | si = vec_elt_at_index (sm->interfaces, src_sw_if_index); |
| 46 | |
| 47 | si->rx_mirror_ports = clib_bitmap_set (si->rx_mirror_ports, dst_sw_if_index, |
| 48 | (state & 1) != 0); |
| 49 | si->tx_mirror_ports = clib_bitmap_set (si->tx_mirror_ports, dst_sw_if_index, |
| 50 | (state & 2) != 0); |
| 51 | |
| 52 | new_num_rx_mirror_ports = clib_bitmap_count_set_bits (si->rx_mirror_ports); |
| 53 | new_num_tx_mirror_ports = clib_bitmap_count_set_bits (si->tx_mirror_ports); |
| 54 | |
| 55 | if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0) |
| 56 | vnet_feature_enable_disable ("device-input", "span-input", |
| 57 | src_sw_if_index, 1, 0, 0); |
| 58 | |
| 59 | if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1) |
| 60 | vnet_feature_enable_disable ("device-input", "span-input", |
| 61 | src_sw_if_index, 0, 0, 0); |
| 62 | |
| 63 | if (new_num_rx_mirror_ports == 1 && si->num_rx_mirror_ports == 0) |
| 64 | vnet_feature_enable_disable ("device-input", "span-output", |
| 65 | src_sw_if_index, 1, 0, 0); |
| 66 | |
| 67 | if (new_num_rx_mirror_ports == 0 && si->num_rx_mirror_ports == 1) |
| 68 | vnet_feature_enable_disable ("device-input", "span-output", |
| 69 | src_sw_if_index, 0, 0, 0); |
| 70 | |
| 71 | si->num_rx_mirror_ports = new_num_rx_mirror_ports; |
| 72 | si->num_tx_mirror_ports = new_num_tx_mirror_ports; |
| 73 | |
| 74 | if (dst_sw_if_index > sm->max_sw_if_index) |
| 75 | sm->max_sw_if_index = dst_sw_if_index; |
| 76 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static clib_error_t * |
| 81 | set_interface_span_command_fn (vlib_main_t * vm, |
| 82 | unformat_input_t * input, |
| 83 | vlib_cli_command_t * cmd) |
| 84 | { |
| 85 | span_main_t *sm = &span_main; |
| 86 | u32 src_sw_if_index = ~0; |
| 87 | u32 dst_sw_if_index = ~0; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 88 | u8 state = 3; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 89 | |
| 90 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 91 | { |
| 92 | if (unformat (input, "%U", unformat_vnet_sw_interface, |
| 93 | sm->vnet_main, &src_sw_if_index)) |
| 94 | ; |
| 95 | else if (unformat (input, "destination %U", unformat_vnet_sw_interface, |
| 96 | sm->vnet_main, &dst_sw_if_index)) |
| 97 | ; |
| 98 | else if (unformat (input, "disable")) |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 99 | state = 0; |
| 100 | else if (unformat (input, "rx")) |
| 101 | state = 1; |
| 102 | else if (unformat (input, "tx")) |
| 103 | state = 2; |
| 104 | else if (unformat (input, "both")) |
| 105 | state = 3; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 106 | else |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | int rv = |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 111 | span_add_delete_entry (vm, src_sw_if_index, dst_sw_if_index, state); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 112 | if (rv == VNET_API_ERROR_INVALID_INTERFACE) |
| 113 | return clib_error_return (0, "Invalid interface"); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* *INDENT-OFF* */ |
| 118 | VLIB_CLI_COMMAND (set_interface_span_command, static) = { |
| 119 | .path = "set interface span", |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 120 | .short_help = "set interface span <if-name> [disable | destination <if-name> [both|rx|tx]]", |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 121 | .function = set_interface_span_command_fn, |
| 122 | }; |
| 123 | /* *INDENT-ON* */ |
| 124 | |
| 125 | static clib_error_t * |
| 126 | show_interfaces_span_command_fn (vlib_main_t * vm, |
| 127 | unformat_input_t * input, |
| 128 | vlib_cli_command_t * cmd) |
| 129 | { |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 130 | span_main_t *sm = &span_main; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 131 | span_interface_t *si; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 132 | vnet_main_t *vnm = &vnet_main; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 133 | u8 header = 1; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 134 | char *states[] = { "none", "rx", "tx", "both" }; |
| 135 | u8 *s = 0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 136 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 137 | /* *INDENT-OFF* */ |
| 138 | vec_foreach (si, sm->interfaces) |
| 139 | if (si->num_rx_mirror_ports || si->num_tx_mirror_ports) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 140 | { |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 141 | clib_bitmap_t *b; |
| 142 | u32 i; |
| 143 | b = clib_bitmap_dup_or (si->rx_mirror_ports, si->tx_mirror_ports); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 144 | if (header) |
| 145 | { |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 146 | vlib_cli_output (vm, "%-40s %s", "Source interface", |
| 147 | "Mirror interface (direction)"); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 148 | header = 0; |
| 149 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 150 | s = format (s, "%U", format_vnet_sw_if_index_name, vnm, |
| 151 | si - sm->interfaces); |
| 152 | clib_bitmap_foreach (i, b, ( |
| 153 | { |
| 154 | int state; |
| 155 | state = (clib_bitmap_get (si->rx_mirror_ports, i) + |
| 156 | clib_bitmap_get (si->tx_mirror_ports, i) * 2); |
| 157 | |
| 158 | vlib_cli_output (vm, "%-40v %U (%s)", s, |
| 159 | format_vnet_sw_if_index_name, vnm, i, |
| 160 | states[state]); |
| 161 | vec_reset_length (s); |
| 162 | })); |
| 163 | clib_bitmap_free (b); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 164 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 165 | /* *INDENT-ON* */ |
| 166 | vec_free (s); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* *INDENT-OFF* */ |
| 171 | VLIB_CLI_COMMAND (show_interfaces_span_command, static) = { |
| 172 | .path = "show interfaces span", |
| 173 | .short_help = "Shows SPAN mirror table", |
| 174 | .function = show_interfaces_span_command_fn, |
| 175 | }; |
| 176 | /* *INDENT-ON* */ |
| 177 | |
| 178 | static clib_error_t * |
| 179 | span_init (vlib_main_t * vm) |
| 180 | { |
| 181 | span_main_t *sm = &span_main; |
| 182 | |
| 183 | sm->vlib_main = vm; |
| 184 | sm->vnet_main = vnet_get_main (); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | VLIB_INIT_FUNCTION (span_init); |
| 190 | |
| 191 | /* |
| 192 | * fd.io coding-style-patch-verification: ON |
| 193 | * |
| 194 | * Local Variables: |
| 195 | * eval: (c-set-style "gnu") |
| 196 | * End: |
| 197 | */ |