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