Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * l2_fwd.c : layer 2 forwarding using l2fib |
| 3 | * |
| 4 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vnet/vnet.h> |
| 20 | #include <vnet/pg/pg.h> |
| 21 | #include <vnet/ethernet/ethernet.h> |
| 22 | #include <vlib/cli.h> |
| 23 | |
| 24 | #include <vnet/l2/l2_input.h> |
| 25 | #include <vnet/l2/l2_bvi.h> |
| 26 | #include <vnet/l2/l2_fwd.h> |
| 27 | #include <vnet/l2/l2_fib.h> |
| 28 | |
| 29 | #include <vppinfra/error.h> |
| 30 | #include <vppinfra/hash.h> |
| 31 | #include <vppinfra/sparse_vec.h> |
| 32 | |
| 33 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame^] | 34 | /** |
| 35 | * @file |
| 36 | * @brief Ethernet Forwarding. |
| 37 | * |
| 38 | * Code in this file handles forwarding Layer 2 packets. This file calls |
| 39 | * the FIB lookup, packet learning and the packet flooding as necessary. |
| 40 | * Packet is then sent to the next graph node. |
| 41 | */ |
| 42 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 43 | typedef struct |
| 44 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 46 | /* Hash table */ |
| 47 | BVT (clib_bihash) * mac_table; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 49 | /* next node index for the L3 input node of each ethertype */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | next_by_ethertype_t l3_next; |
| 51 | |
| 52 | /* convenience variables */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 53 | vlib_main_t *vlib_main; |
| 54 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } l2fwd_main_t; |
| 56 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
| 59 | /* per-pkt trace data */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | u8 src[6]; |
| 61 | u8 dst[6]; |
| 62 | u32 sw_if_index; |
| 63 | u16 bd_index; |
| 64 | } l2fwd_trace_t; |
| 65 | |
| 66 | /* packet trace format function */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 67 | static u8 * |
| 68 | format_l2fwd_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | { |
| 70 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 71 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 72 | l2fwd_trace_t *t = va_arg (*args, l2fwd_trace_t *); |
| 73 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | s = format (s, "l2-fwd: sw_if_index %d dst %U src %U bd_index %d", |
| 75 | t->sw_if_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 76 | format_ethernet_address, t->dst, |
| 77 | format_ethernet_address, t->src, t->bd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | return s; |
| 79 | } |
| 80 | |
| 81 | l2fwd_main_t l2fwd_main; |
| 82 | |
| 83 | static vlib_node_registration_t l2fwd_node; |
| 84 | |
| 85 | #define foreach_l2fwd_error \ |
| 86 | _(L2FWD, "L2 forward packets") \ |
| 87 | _(FLOOD, "L2 forward misses") \ |
| 88 | _(HIT, "L2 forward hits") \ |
John Lo | 7185c3b | 2016-06-04 00:02:37 -0400 | [diff] [blame] | 89 | _(BVI_BAD_MAC, "BVI L3 MAC mismatch") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | _(BVI_ETHERTYPE, "BVI packet with unhandled ethertype") \ |
| 91 | _(FILTER_DROP, "Filter Mac Drop") \ |
| 92 | _(REFLECT_DROP, "Reflection Drop") |
| 93 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 94 | typedef enum |
| 95 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | #define _(sym,str) L2FWD_ERROR_##sym, |
| 97 | foreach_l2fwd_error |
| 98 | #undef _ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 99 | L2FWD_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | } l2fwd_error_t; |
| 101 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 102 | static char *l2fwd_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | #define _(sym,string) string, |
| 104 | foreach_l2fwd_error |
| 105 | #undef _ |
| 106 | }; |
| 107 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 108 | typedef enum |
| 109 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | L2FWD_NEXT_L2_OUTPUT, |
| 111 | L2FWD_NEXT_FLOOD, |
| 112 | L2FWD_NEXT_DROP, |
| 113 | L2FWD_N_NEXT, |
| 114 | } l2fwd_next_t; |
| 115 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 116 | /** Forward one packet based on the mac table lookup result. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | static_always_inline void |
| 119 | l2fwd_process (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 120 | vlib_node_runtime_t * node, |
| 121 | l2fwd_main_t * msm, |
| 122 | vlib_error_main_t * em, |
| 123 | vlib_buffer_t * b0, |
| 124 | u32 sw_if_index0, l2fib_entry_result_t * result0, u32 * next0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 126 | if (PREDICT_FALSE (result0->raw == ~0)) |
| 127 | { |
| 128 | /* |
| 129 | * lookup miss, so flood |
| 130 | * TODO:replicate packet to each intf in bridge-domain |
| 131 | * For now just drop |
| 132 | */ |
| 133 | if (vnet_buffer (b0)->l2.feature_bitmap & L2INPUT_FEAT_UU_FLOOD) |
| 134 | { |
| 135 | *next0 = L2FWD_NEXT_FLOOD; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | /* Flooding is disabled */ |
| 140 | b0->error = node->errors[L2FWD_ERROR_FLOOD]; |
| 141 | *next0 = L2FWD_NEXT_DROP; |
| 142 | } |
| 143 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 145 | else |
| 146 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 148 | /* lookup hit, forward packet */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | #ifdef COUNTERS |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 150 | em->counters[node_counter_base_index + L2FWD_ERROR_HIT] += 1; |
| 151 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 153 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = result0->fields.sw_if_index; |
| 154 | *next0 = L2FWD_NEXT_L2_OUTPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 156 | /* perform reflection check */ |
| 157 | if (PREDICT_FALSE (sw_if_index0 == result0->fields.sw_if_index)) |
| 158 | { |
| 159 | b0->error = node->errors[L2FWD_ERROR_REFLECT_DROP]; |
| 160 | *next0 = L2FWD_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 162 | /* perform filter check */ |
| 163 | } |
| 164 | else if (PREDICT_FALSE (result0->fields.filter)) |
| 165 | { |
| 166 | b0->error = node->errors[L2FWD_ERROR_FILTER_DROP]; |
| 167 | *next0 = L2FWD_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 169 | /* perform BVI check */ |
| 170 | } |
| 171 | else if (PREDICT_FALSE (result0->fields.bvi)) |
| 172 | { |
| 173 | u32 rc; |
| 174 | rc = l2_to_bvi (vm, |
| 175 | msm->vnet_main, |
| 176 | b0, |
| 177 | vnet_buffer (b0)->sw_if_index[VLIB_TX], |
| 178 | &msm->l3_next, next0); |
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 | if (PREDICT_FALSE (rc)) |
| 181 | { |
| 182 | if (rc == TO_BVI_ERR_BAD_MAC) |
| 183 | { |
| 184 | b0->error = node->errors[L2FWD_ERROR_BVI_BAD_MAC]; |
| 185 | *next0 = L2FWD_NEXT_DROP; |
| 186 | } |
| 187 | else if (rc == TO_BVI_ERR_ETHERTYPE) |
| 188 | { |
| 189 | b0->error = node->errors[L2FWD_ERROR_BVI_ETHERTYPE]; |
| 190 | *next0 = L2FWD_NEXT_DROP; |
| 191 | } |
| 192 | } |
| 193 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | static uword |
| 199 | l2fwd_node_fn (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 200 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 202 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | l2fwd_next_t next_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 204 | l2fwd_main_t *msm = &l2fwd_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | vlib_node_t *n = vlib_get_node (vm, l2fwd_node.index); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 206 | CLIB_UNUSED (u32 node_counter_base_index) = n->error_heap_index; |
| 207 | vlib_error_main_t *em = &vm->error_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | l2fib_entry_key_t cached_key; |
| 209 | l2fib_entry_result_t cached_result; |
| 210 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 211 | /* Clear the one-entry cache in case mac table was updated */ |
| 212 | cached_key.raw = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | cached_result.raw = ~0; |
| 214 | |
| 215 | from = vlib_frame_vector_args (frame); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 216 | n_left_from = frame->n_vectors; /* number of packets to process */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | next_index = node->cached_next_index; |
| 218 | |
| 219 | while (n_left_from > 0) |
| 220 | { |
| 221 | u32 n_left_to_next; |
| 222 | |
| 223 | /* get space to enqueue frame to graph node "next_index" */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 224 | 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] | 225 | |
| 226 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 227 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 228 | u32 bi0, bi1; |
| 229 | vlib_buffer_t *b0, *b1; |
| 230 | u32 next0, next1; |
| 231 | u32 sw_if_index0, sw_if_index1; |
| 232 | ethernet_header_t *h0, *h1; |
| 233 | l2fib_entry_key_t key0, key1; |
| 234 | l2fib_entry_result_t result0, result1; |
| 235 | u32 bucket0, bucket1; |
| 236 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | /* Prefetch next iteration. */ |
| 238 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 239 | vlib_buffer_t *p2, *p3; |
| 240 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | p2 = vlib_get_buffer (vm, from[2]); |
| 242 | p3 = vlib_get_buffer (vm, from[3]); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 243 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | vlib_prefetch_buffer_header (p2, LOAD); |
| 245 | vlib_prefetch_buffer_header (p3, LOAD); |
| 246 | |
| 247 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 248 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 249 | } |
| 250 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 251 | /* speculatively enqueue b0 and b1 to the current next frame */ |
| 252 | /* bi is "buffer index", b is pointer to the buffer */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | to_next[0] = bi0 = from[0]; |
| 254 | to_next[1] = bi1 = from[1]; |
| 255 | from += 2; |
| 256 | to_next += 2; |
| 257 | n_left_from -= 2; |
| 258 | n_left_to_next -= 2; |
| 259 | |
| 260 | b0 = vlib_get_buffer (vm, bi0); |
| 261 | b1 = vlib_get_buffer (vm, bi1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 263 | /* RX interface handles */ |
| 264 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 265 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 267 | h0 = vlib_buffer_get_current (b0); |
| 268 | h1 = vlib_buffer_get_current (b1); |
| 269 | |
| 270 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 271 | { |
| 272 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 273 | { |
| 274 | l2fwd_trace_t *t = |
| 275 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 276 | t->sw_if_index = sw_if_index0; |
| 277 | t->bd_index = vnet_buffer (b0)->l2.bd_index; |
| 278 | clib_memcpy (t->src, h0->src_address, 6); |
| 279 | clib_memcpy (t->dst, h0->dst_address, 6); |
| 280 | } |
| 281 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 282 | { |
| 283 | l2fwd_trace_t *t = |
| 284 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 285 | t->sw_if_index = sw_if_index1; |
| 286 | t->bd_index = vnet_buffer (b1)->l2.bd_index; |
| 287 | clib_memcpy (t->src, h1->src_address, 6); |
| 288 | clib_memcpy (t->dst, h1->dst_address, 6); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* process 2 pkts */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | #ifdef COUNTERS |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 294 | em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | #endif |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 296 | l2fib_lookup_2 (msm->mac_table, &cached_key, &cached_result, h0->dst_address, h1->dst_address, vnet_buffer (b0)->l2.bd_index, vnet_buffer (b1)->l2.bd_index, &key0, /* not used */ |
| 297 | &key1, /* not used */ |
| 298 | &bucket0, /* not used */ |
| 299 | &bucket1, /* not used */ |
| 300 | &result0, &result1); |
| 301 | l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, |
| 302 | &next0); |
| 303 | l2fwd_process (vm, node, msm, em, b1, sw_if_index1, &result1, |
| 304 | &next1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 306 | /* verify speculative enqueues, maybe switch current next frame */ |
| 307 | /* if next0==next1==next_index then nothing special needs to be done */ |
| 308 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 309 | to_next, n_left_to_next, |
| 310 | bi0, bi1, next0, next1); |
| 311 | } |
| 312 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | while (n_left_from > 0 && n_left_to_next > 0) |
| 314 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 315 | u32 bi0; |
| 316 | vlib_buffer_t *b0; |
| 317 | u32 next0; |
| 318 | u32 sw_if_index0; |
| 319 | ethernet_header_t *h0; |
| 320 | l2fib_entry_key_t key0; |
| 321 | l2fib_entry_result_t result0; |
| 322 | u32 bucket0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 324 | /* speculatively enqueue b0 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | bi0 = from[0]; |
| 326 | to_next[0] = bi0; |
| 327 | from += 1; |
| 328 | to_next += 1; |
| 329 | n_left_from -= 1; |
| 330 | n_left_to_next -= 1; |
| 331 | |
| 332 | b0 = vlib_get_buffer (vm, bi0); |
| 333 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 334 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 336 | h0 = vlib_buffer_get_current (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 338 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 339 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 340 | { |
| 341 | l2fwd_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 342 | t->sw_if_index = sw_if_index0; |
| 343 | t->bd_index = vnet_buffer (b0)->l2.bd_index; |
| 344 | clib_memcpy (t->src, h0->src_address, 6); |
| 345 | clib_memcpy (t->dst, h0->dst_address, 6); |
| 346 | } |
| 347 | |
| 348 | /* process 1 pkt */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | #ifdef COUNTERS |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 350 | em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | #endif |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 352 | l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result, h0->dst_address, vnet_buffer (b0)->l2.bd_index, &key0, /* not used */ |
| 353 | &bucket0, /* not used */ |
| 354 | &result0); |
| 355 | l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, |
| 356 | &next0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 358 | /* verify speculative enqueue, maybe switch current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 360 | to_next, n_left_to_next, |
| 361 | bi0, next0); |
| 362 | } |
| 363 | |
| 364 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 365 | } |
| 366 | |
| 367 | return frame->n_vectors; |
| 368 | } |
| 369 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 370 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | VLIB_REGISTER_NODE (l2fwd_node,static) = { |
| 372 | .function = l2fwd_node_fn, |
| 373 | .name = "l2-fwd", |
| 374 | .vector_size = sizeof (u32), |
| 375 | .format_trace = format_l2fwd_trace, |
| 376 | .type = VLIB_NODE_TYPE_INTERNAL, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 377 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | .n_errors = ARRAY_LEN(l2fwd_error_strings), |
| 379 | .error_strings = l2fwd_error_strings, |
| 380 | |
| 381 | .n_next_nodes = L2FWD_N_NEXT, |
| 382 | |
| 383 | /* edit / add dispositions here */ |
| 384 | .next_nodes = { |
| 385 | [L2FWD_NEXT_L2_OUTPUT] = "l2-output", |
| 386 | [L2FWD_NEXT_FLOOD] = "l2-flood", |
| 387 | [L2FWD_NEXT_DROP] = "error-drop", |
| 388 | }, |
| 389 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 390 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 392 | VLIB_NODE_FUNCTION_MULTIARCH (l2fwd_node, l2fwd_node_fn) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 393 | clib_error_t *l2fwd_init (vlib_main_t * vm) |
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 | l2fwd_main_t *mp = &l2fwd_main; |
| 396 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 398 | mp->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
| 400 | /* init the hash table ptr */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 401 | mp->mac_table = get_mac_table (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 403 | /* Initialize the next nodes for each ethertype */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | next_by_ethertype_init (&mp->l3_next); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | VLIB_INIT_FUNCTION (l2fwd_init); |
| 410 | |
| 411 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 412 | /** Add the L3 input node for this ethertype to the next nodes structure. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | void |
| 414 | l2fwd_register_input_type (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 415 | ethernet_type_t type, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 417 | l2fwd_main_t *mp = &l2fwd_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | u32 next_index; |
| 419 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 420 | next_index = vlib_node_add_next (vm, l2fwd_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | |
| 422 | next_by_ethertype_register (&mp->l3_next, type, next_index); |
| 423 | } |
| 424 | |
| 425 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 426 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 427 | * Set subinterface forward enable/disable. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 428 | * The CLI format is: |
| 429 | * set interface l2 forward <interface> [disable] |
| 430 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 432 | int_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 434 | vnet_main_t *vnm = vnet_get_main (); |
| 435 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | u32 sw_if_index; |
| 437 | u32 enable; |
| 438 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 439 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | { |
| 441 | error = clib_error_return (0, "unknown interface `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 442 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | goto done; |
| 444 | } |
| 445 | |
| 446 | enable = 1; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 447 | if (unformat (input, "disable")) |
| 448 | { |
| 449 | enable = 0; |
| 450 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 452 | /* set the interface flag */ |
| 453 | if (l2input_intf_config (sw_if_index)->xconnect) |
| 454 | { |
| 455 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_XCONNECT, enable); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FWD, enable); |
| 460 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 462 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | return error; |
| 464 | } |
| 465 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame^] | 466 | /*? |
| 467 | * Layer 2 unicast forwarding can be enabled and disabled on each |
| 468 | * interface and on each bridge-domain. Use this command to |
| 469 | * manage interfaces. It is enabled by default. |
| 470 | * |
| 471 | * @cliexpar |
| 472 | * Example of how to enable fowarding: |
| 473 | * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0} |
| 474 | * Example of how to disable fowarding: |
| 475 | * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0 disable} |
| 476 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 477 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | VLIB_CLI_COMMAND (int_fwd_cli, static) = { |
| 479 | .path = "set interface l2 forward", |
| 480 | .short_help = "set interface l2 forward <interface> [disable]", |
| 481 | .function = int_fwd, |
| 482 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 483 | /* *INDENT-ON* */ |
| 484 | |
| 485 | /* |
| 486 | * fd.io coding-style-patch-verification: ON |
| 487 | * |
| 488 | * Local Variables: |
| 489 | * eval: (c-set-style "gnu") |
| 490 | * End: |
| 491 | */ |