Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #include <vlib/vlib.h> |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vnet/pg/pg.h> |
| 18 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 19 | #include <vnet/feature/feature.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #include <vppinfra/error.h> |
| 21 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 22 | typedef struct |
| 23 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | /* vector of dispositions, indexed by rx_sw_if_index */ |
| 25 | u32 *tx_next_by_rx_sw_if_index; |
| 26 | u32 *tx_sw_if_index_by_rx_sw_if_index; |
| 27 | |
| 28 | /* convenience variables */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 29 | vlib_main_t *vlib_main; |
| 30 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | } l2_patch_main_t; |
| 32 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 33 | typedef struct |
| 34 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | u32 rx_sw_if_index; |
| 36 | u32 tx_sw_if_index; |
| 37 | } l2_patch_trace_t; |
| 38 | |
| 39 | /* packet trace format function */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 40 | static u8 * |
| 41 | format_l2_patch_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | { |
| 43 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 44 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 45 | l2_patch_trace_t *t = va_arg (*args, l2_patch_trace_t *); |
| 46 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 48 | t->tx_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | return s; |
| 50 | } |
| 51 | |
| 52 | l2_patch_main_t l2_patch_main; |
| 53 | |
| 54 | static vlib_node_registration_t l2_patch_node; |
| 55 | |
| 56 | #define foreach_l2_patch_error \ |
| 57 | _(PATCHED, "L2 patch packets") \ |
| 58 | _(DROPPED, "L2 patch misconfigured drops") |
| 59 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 60 | typedef enum |
| 61 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | #define _(sym,str) L2_PATCH_ERROR_##sym, |
| 63 | foreach_l2_patch_error |
| 64 | #undef _ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 65 | L2_PATCH_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | } l2_patch_error_t; |
| 67 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 68 | static char *l2_patch_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | #define _(sym,string) string, |
| 70 | foreach_l2_patch_error |
| 71 | #undef _ |
| 72 | }; |
| 73 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 74 | typedef enum |
| 75 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | L2_PATCH_NEXT_DROP, |
| 77 | L2_PATCH_N_NEXT, |
| 78 | } l2_patch_next_t; |
| 79 | |
| 80 | static uword |
| 81 | l2_patch_node_fn (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 82 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 84 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | l2_patch_next_t next_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 86 | l2_patch_main_t *l2pm = &l2_patch_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | vlib_node_t *n = vlib_get_node (vm, l2_patch_node.index); |
| 88 | u32 node_counter_base_index = n->error_heap_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 89 | vlib_error_main_t *em = &vm->error_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
| 91 | from = vlib_frame_vector_args (frame); |
| 92 | n_left_from = frame->n_vectors; |
| 93 | next_index = node->cached_next_index; |
| 94 | |
| 95 | while (n_left_from > 0) |
| 96 | { |
| 97 | u32 n_left_to_next; |
| 98 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 99 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 102 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 103 | u32 bi0, bi1; |
| 104 | vlib_buffer_t *b0, *b1; |
| 105 | u32 next0, next1; |
| 106 | u32 sw_if_index0, sw_if_index1; |
| 107 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | /* Prefetch next iteration. */ |
| 109 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 110 | vlib_buffer_t *p2, *p3; |
| 111 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | p2 = vlib_get_buffer (vm, from[2]); |
| 113 | p3 = vlib_get_buffer (vm, from[3]); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 114 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | vlib_prefetch_buffer_header (p2, LOAD); |
| 116 | vlib_prefetch_buffer_header (p3, LOAD); |
| 117 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 118 | /* So stupid / simple, we don't need to prefetch data */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 121 | /* speculatively enqueue b0 and b1 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | to_next[0] = bi0 = from[0]; |
| 123 | to_next[1] = bi1 = from[1]; |
| 124 | from += 2; |
| 125 | to_next += 2; |
| 126 | n_left_from -= 2; |
| 127 | n_left_to_next -= 2; |
| 128 | |
| 129 | b0 = vlib_get_buffer (vm, bi0); |
| 130 | b1 = vlib_get_buffer (vm, bi1); |
| 131 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 132 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 133 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 135 | ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0); |
| 136 | ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0); |
| 137 | ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index1] != ~0); |
| 138 | ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1] != ~0); |
| 139 | |
Damjan Marion | d5c9d6f | 2016-10-31 23:21:46 +0100 | [diff] [blame] | 140 | next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0]; |
| 141 | next1 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index1]; |
| 142 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = |
| 143 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0]; |
| 144 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = |
| 145 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1]; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 146 | |
| 147 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 148 | { |
| 149 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 150 | { |
| 151 | l2_patch_trace_t *t = |
| 152 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 153 | t->rx_sw_if_index = sw_if_index0; |
| 154 | t->tx_sw_if_index = |
| 155 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0]; |
| 156 | } |
| 157 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 158 | { |
| 159 | l2_patch_trace_t *t = |
| 160 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 161 | t->rx_sw_if_index = sw_if_index1; |
| 162 | t->tx_sw_if_index = |
| 163 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1]; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* verify speculative enqueues, maybe switch current next frame */ |
| 168 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 169 | to_next, n_left_to_next, |
| 170 | bi0, bi1, next0, next1); |
| 171 | } |
| 172 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | while (n_left_from > 0 && n_left_to_next > 0) |
| 174 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 175 | u32 bi0; |
| 176 | vlib_buffer_t *b0; |
| 177 | u32 next0; |
| 178 | u32 sw_if_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 180 | /* speculatively enqueue b0 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | bi0 = from[0]; |
| 182 | to_next[0] = bi0; |
| 183 | from += 1; |
| 184 | to_next += 1; |
| 185 | n_left_from -= 1; |
| 186 | n_left_to_next -= 1; |
| 187 | |
| 188 | b0 = vlib_get_buffer (vm, bi0); |
| 189 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 190 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 192 | ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0); |
| 193 | ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
Damjan Marion | d5c9d6f | 2016-10-31 23:21:46 +0100 | [diff] [blame] | 195 | next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0]; |
| 196 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = |
| 197 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 199 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 200 | { |
| 201 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 202 | { |
| 203 | l2_patch_trace_t *t = |
| 204 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 205 | t->rx_sw_if_index = sw_if_index0; |
| 206 | t->tx_sw_if_index = |
| 207 | l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0]; |
| 208 | } |
| 209 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 211 | /* verify speculative enqueue, maybe switch current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 213 | to_next, n_left_to_next, |
| 214 | bi0, next0); |
| 215 | } |
| 216 | |
| 217 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 218 | } |
| 219 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 220 | em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] += |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | frame->n_vectors; |
| 222 | |
| 223 | return frame->n_vectors; |
| 224 | } |
| 225 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 226 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | VLIB_REGISTER_NODE (l2_patch_node, static) = { |
| 228 | .function = l2_patch_node_fn, |
Damjan Marion | d5c9d6f | 2016-10-31 23:21:46 +0100 | [diff] [blame] | 229 | .name = "l2-patch", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | .vector_size = sizeof (u32), |
| 231 | .format_trace = format_l2_patch_trace, |
| 232 | .type = VLIB_NODE_TYPE_INTERNAL, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 233 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | .n_errors = ARRAY_LEN(l2_patch_error_strings), |
| 235 | .error_strings = l2_patch_error_strings, |
| 236 | |
| 237 | .n_next_nodes = L2_PATCH_N_NEXT, |
| 238 | |
| 239 | /* edit / add dispositions here */ |
| 240 | .next_nodes = { |
| 241 | [L2_PATCH_NEXT_DROP] = "error-drop", |
| 242 | }, |
| 243 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 244 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 246 | VLIB_NODE_FUNCTION_MULTIARCH (l2_patch_node, l2_patch_node_fn) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 247 | int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, |
| 248 | int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 250 | l2_patch_main_t *l2pm = &l2_patch_main; |
| 251 | vnet_hw_interface_t *rxhi, *txhi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | u32 tx_next_index; |
| 253 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 254 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX |
| 256 | * macros... |
| 257 | */ |
| 258 | |
| 259 | rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index); |
| 260 | |
| 261 | /* Make sure caller didn't pass a vlan subif, etc. */ |
| 262 | if (rxhi->sw_if_index != rx_sw_if_index) |
| 263 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 264 | |
| 265 | txhi = vnet_get_sup_hw_interface (l2pm->vnet_main, tx_sw_if_index); |
| 266 | if (txhi->sw_if_index != tx_sw_if_index) |
| 267 | return VNET_API_ERROR_INVALID_SW_IF_INDEX_2; |
| 268 | |
| 269 | if (is_add) |
| 270 | { |
| 271 | tx_next_index = vlib_node_add_next (l2pm->vlib_main, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 272 | l2_patch_node.index, |
| 273 | txhi->output_node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
| 275 | vec_validate_init_empty (l2pm->tx_next_by_rx_sw_if_index, |
| 276 | rx_sw_if_index, ~0); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 277 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index; |
| 279 | vec_validate_init_empty (l2pm->tx_sw_if_index_by_rx_sw_if_index, |
| 280 | rx_sw_if_index, ~0); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 281 | l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] |
| 282 | = txhi->sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 284 | ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index, |
| 285 | ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); |
| 286 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 287 | vnet_feature_enable_disable ("device-input", "l2-patch", |
| 288 | rxhi->hw_if_index, 1, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | } |
| 290 | else |
| 291 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 292 | ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index, |
| 293 | 0 /* disable promiscuous mode */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 295 | vnet_feature_enable_disable ("device-input", "l2-patch", |
| 296 | rxhi->hw_if_index, 0, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 298 | { |
| 299 | l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0; |
| 300 | l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0; |
| 301 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 303 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static clib_error_t * |
| 308 | test_patch_command_fn (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 309 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 311 | l2_patch_main_t *l2pm = &l2_patch_main; |
| 312 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | u32 rx_sw_if_index, tx_sw_if_index; |
| 314 | int rv; |
| 315 | int rx_set = 0; |
| 316 | int tx_set = 0; |
| 317 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 318 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
| 320 | /* Get a line of input. */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 321 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | return 0; |
| 323 | |
| 324 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 325 | { |
| 326 | if (unformat (line_input, "rx %U", unformat_vnet_sw_interface, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 327 | l2pm->vnet_main, &rx_sw_if_index)) |
| 328 | rx_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 330 | l2pm->vnet_main, &tx_sw_if_index)) |
| 331 | tx_set = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | else if (unformat (line_input, "del")) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 333 | is_add = 0; |
| 334 | else |
| 335 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (rx_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 339 | { |
| 340 | error = clib_error_return (0, "rx interface not set"); |
| 341 | goto done; |
| 342 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
| 344 | if (tx_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 345 | { |
| 346 | error = clib_error_return (0, "tx interface not set"); |
| 347 | goto done; |
| 348 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | |
| 350 | rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add); |
| 351 | |
| 352 | switch (rv) |
| 353 | { |
| 354 | case 0: |
| 355 | break; |
| 356 | |
| 357 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 358 | error = clib_error_return (0, "rx interface not a physical port"); |
| 359 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
| 361 | case VNET_API_ERROR_INVALID_SW_IF_INDEX_2: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 362 | error = clib_error_return (0, "tx interface not a physical port"); |
| 363 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
| 365 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 366 | error = clib_error_return |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 367 | (0, "WARNING: vnet_l2_patch_add_del returned %d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 368 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 370 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 371 | |
| 372 | done: |
| 373 | unformat_free (line_input); |
| 374 | |
| 375 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 378 | /*? |
| 379 | * Create or delete a Layer 2 patch. |
| 380 | * |
| 381 | * @cliexpar |
| 382 | * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]} |
| 383 | * @cliexend |
| 384 | * @todo This is incomplete. This needs a detailed description and a |
| 385 | * practical example. |
| 386 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 387 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | VLIB_CLI_COMMAND (test_patch_command, static) = { |
| 389 | .path = "test l2patch", |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 390 | .short_help = "test l2patch rx <intfc> tx <intfc> [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | .function = test_patch_command_fn, |
| 392 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 393 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 395 | /** Display the contents of the l2patch table. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 397 | show_l2patch (vlib_main_t * vm, |
| 398 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 400 | l2_patch_main_t *l2pm = &l2_patch_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | u32 rx_sw_if_index; |
| 402 | u32 no_entries = 1; |
| 403 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 404 | ASSERT (vec_len (l2pm->tx_next_by_rx_sw_if_index) == |
| 405 | vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index)); |
| 406 | |
| 407 | for (rx_sw_if_index = 0; |
| 408 | rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | rx_sw_if_index++) |
| 410 | { |
| 411 | u32 tx_sw_if_index = |
| 412 | l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]; |
| 413 | if (tx_sw_if_index != ~0) |
| 414 | { |
| 415 | no_entries = 0; |
| 416 | vlib_cli_output (vm, "%26U -> %U", |
| 417 | format_vnet_sw_if_index_name, |
| 418 | l2pm->vnet_main, rx_sw_if_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 419 | format_vnet_sw_if_index_name, |
| 420 | l2pm->vnet_main, tx_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | if (no_entries) |
| 425 | vlib_cli_output (vm, "no l2patch entries"); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 430 | /*? |
| 431 | * Show Layer 2 patch entries. |
| 432 | * |
| 433 | * @cliexpar |
| 434 | * @cliexstart{show l2patch} |
| 435 | * @cliexend |
| 436 | * @todo This is incomplete. This needs a detailed description and a |
| 437 | * practical example. |
| 438 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 439 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | VLIB_CLI_COMMAND (show_l2patch_cli, static) = { |
| 441 | .path = "show l2patch", |
| 442 | .short_help = "Show l2 interface cross-connect entries", |
| 443 | .function = show_l2patch, |
| 444 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 445 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 447 | clib_error_t * |
| 448 | l2_patch_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 450 | l2_patch_main_t *mp = &l2_patch_main; |
| 451 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 453 | mp->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | VLIB_INIT_FUNCTION (l2_patch_init); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 459 | |
| 460 | /* |
| 461 | * fd.io coding-style-patch-verification: ON |
| 462 | * |
| 463 | * Local Variables: |
| 464 | * eval: (c-set-style "gnu") |
| 465 | * End: |
| 466 | */ |