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") \ |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 92 | _(REFLECT_DROP, "Reflection Drop") \ |
| 93 | _(STALE_DROP, "Stale entry Drop") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 95 | typedef enum |
| 96 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | #define _(sym,str) L2FWD_ERROR_##sym, |
| 98 | foreach_l2fwd_error |
| 99 | #undef _ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 100 | L2FWD_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | } l2fwd_error_t; |
| 102 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 103 | static char *l2fwd_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | #define _(sym,string) string, |
| 105 | foreach_l2fwd_error |
| 106 | #undef _ |
| 107 | }; |
| 108 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 109 | typedef enum |
| 110 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | L2FWD_NEXT_L2_OUTPUT, |
| 112 | L2FWD_NEXT_FLOOD, |
| 113 | L2FWD_NEXT_DROP, |
| 114 | L2FWD_N_NEXT, |
| 115 | } l2fwd_next_t; |
| 116 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 117 | /** Forward one packet based on the mac table lookup result. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
| 119 | static_always_inline void |
| 120 | l2fwd_process (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 121 | vlib_node_runtime_t * node, |
| 122 | l2fwd_main_t * msm, |
| 123 | vlib_error_main_t * em, |
| 124 | vlib_buffer_t * b0, |
| 125 | u32 sw_if_index0, l2fib_entry_result_t * result0, u32 * next0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 127 | int try_flood = result0->raw == ~0; |
| 128 | int flood_error; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 129 | |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 130 | if (PREDICT_FALSE (try_flood)) |
| 131 | { |
| 132 | flood_error = L2FWD_ERROR_FLOOD; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 134 | else |
| 135 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 136 | /* lookup hit, forward packet */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | #ifdef COUNTERS |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 138 | em->counters[node_counter_base_index + L2FWD_ERROR_HIT] += 1; |
| 139 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 141 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = result0->fields.sw_if_index; |
| 142 | *next0 = L2FWD_NEXT_L2_OUTPUT; |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 143 | int l2fib_seq_num_valid = 1; |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 144 | |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 145 | /* check l2fib seq num for stale entries */ |
John Lo | 8d00fff | 2017-08-03 00:35:36 -0400 | [diff] [blame] | 146 | if (!result0->fields.age_not) |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 147 | { |
| 148 | l2fib_seq_num_t in_sn = {.as_u16 = vnet_buffer (b0)->l2.l2fib_sn }; |
| 149 | l2fib_seq_num_t expected_sn = { |
| 150 | .bd = in_sn.bd, |
| 151 | .swif = *l2fib_swif_seq_num (result0->fields.sw_if_index), |
| 152 | }; |
| 153 | l2fib_seq_num_valid = |
| 154 | expected_sn.as_u16 == result0->fields.sn.as_u16; |
| 155 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 157 | if (PREDICT_FALSE (!l2fib_seq_num_valid)) |
| 158 | { |
| 159 | flood_error = L2FWD_ERROR_STALE_DROP; |
| 160 | try_flood = 1; |
| 161 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 162 | /* perform reflection check */ |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 163 | else if (PREDICT_FALSE (sw_if_index0 == result0->fields.sw_if_index)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 164 | { |
| 165 | b0->error = node->errors[L2FWD_ERROR_REFLECT_DROP]; |
| 166 | *next0 = L2FWD_NEXT_DROP; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 167 | } |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 168 | /* perform filter check */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 169 | else if (PREDICT_FALSE (result0->fields.filter)) |
| 170 | { |
| 171 | b0->error = node->errors[L2FWD_ERROR_FILTER_DROP]; |
| 172 | *next0 = L2FWD_NEXT_DROP; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 173 | } |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 174 | /* perform BVI check */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 175 | else if (PREDICT_FALSE (result0->fields.bvi)) |
| 176 | { |
| 177 | u32 rc; |
| 178 | rc = l2_to_bvi (vm, |
| 179 | msm->vnet_main, |
| 180 | b0, |
| 181 | vnet_buffer (b0)->sw_if_index[VLIB_TX], |
| 182 | &msm->l3_next, next0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 184 | if (PREDICT_FALSE (rc)) |
| 185 | { |
| 186 | if (rc == TO_BVI_ERR_BAD_MAC) |
| 187 | { |
| 188 | b0->error = node->errors[L2FWD_ERROR_BVI_BAD_MAC]; |
| 189 | *next0 = L2FWD_NEXT_DROP; |
| 190 | } |
| 191 | else if (rc == TO_BVI_ERR_ETHERTYPE) |
| 192 | { |
| 193 | b0->error = node->errors[L2FWD_ERROR_BVI_ETHERTYPE]; |
| 194 | *next0 = L2FWD_NEXT_DROP; |
| 195 | } |
| 196 | } |
| 197 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | } |
Eyal Bari | 0f360dc | 2017-06-14 13:11:20 +0300 | [diff] [blame] | 199 | |
| 200 | /* flood */ |
| 201 | if (PREDICT_FALSE (try_flood)) |
| 202 | { |
| 203 | /* |
| 204 | * lookup miss, so flood |
| 205 | * TODO:replicate packet to each intf in bridge-domain |
| 206 | * For now just drop |
| 207 | */ |
| 208 | if (vnet_buffer (b0)->l2.feature_bitmap & L2INPUT_FEAT_UU_FLOOD) |
| 209 | { |
| 210 | *next0 = L2FWD_NEXT_FLOOD; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | /* Flooding is disabled */ |
| 215 | b0->error = node->errors[flood_error]; |
| 216 | *next0 = L2FWD_NEXT_DROP; |
| 217 | } |
| 218 | } |
| 219 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
Dave Barach | 681abe4 | 2017-02-15 09:01:01 -0500 | [diff] [blame] | 223 | static_always_inline uword |
| 224 | l2fwd_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 225 | vlib_frame_t * frame, int do_trace) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 227 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | l2fwd_next_t next_index; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 229 | l2fwd_main_t *msm = &l2fwd_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | vlib_node_t *n = vlib_get_node (vm, l2fwd_node.index); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 231 | CLIB_UNUSED (u32 node_counter_base_index) = n->error_heap_index; |
| 232 | vlib_error_main_t *em = &vm->error_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | l2fib_entry_key_t cached_key; |
| 234 | l2fib_entry_result_t cached_result; |
| 235 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 236 | /* Clear the one-entry cache in case mac table was updated */ |
| 237 | cached_key.raw = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | cached_result.raw = ~0; |
| 239 | |
| 240 | from = vlib_frame_vector_args (frame); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 241 | n_left_from = frame->n_vectors; /* number of packets to process */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | next_index = node->cached_next_index; |
| 243 | |
| 244 | while (n_left_from > 0) |
| 245 | { |
| 246 | u32 n_left_to_next; |
| 247 | |
| 248 | /* get space to enqueue frame to graph node "next_index" */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 249 | 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] | 250 | |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 251 | while (n_left_from >= 8 && n_left_to_next >= 4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | { |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 253 | u32 bi0, bi1, bi2, bi3; |
| 254 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 255 | u32 next0, next1, next2, next3; |
| 256 | u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3; |
| 257 | ethernet_header_t *h0, *h1, *h2, *h3; |
| 258 | l2fib_entry_key_t key0, key1, key2, key3; |
| 259 | l2fib_entry_result_t result0, result1, result2, result3; |
| 260 | u32 bucket0, bucket1, bucket2, bucket3; |
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 | /* Prefetch next iteration. */ |
| 263 | { |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 264 | vlib_buffer_t *p4, *p5, *p6, *p7; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 265 | |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 266 | p4 = vlib_get_buffer (vm, from[4]); |
| 267 | p5 = vlib_get_buffer (vm, from[5]); |
| 268 | p6 = vlib_get_buffer (vm, from[6]); |
| 269 | p7 = vlib_get_buffer (vm, from[7]); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 270 | |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 271 | vlib_prefetch_buffer_header (p4, LOAD); |
| 272 | vlib_prefetch_buffer_header (p5, LOAD); |
| 273 | vlib_prefetch_buffer_header (p6, LOAD); |
| 274 | vlib_prefetch_buffer_header (p7, LOAD); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 276 | CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 277 | CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 278 | CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 279 | CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 282 | /* speculatively enqueue b0 and b1 to the current next frame */ |
| 283 | /* bi is "buffer index", b is pointer to the buffer */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | to_next[0] = bi0 = from[0]; |
| 285 | to_next[1] = bi1 = from[1]; |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 286 | to_next[2] = bi2 = from[2]; |
| 287 | to_next[3] = bi3 = from[3]; |
| 288 | from += 4; |
| 289 | to_next += 4; |
| 290 | n_left_from -= 4; |
| 291 | n_left_to_next -= 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
| 293 | b0 = vlib_get_buffer (vm, bi0); |
| 294 | b1 = vlib_get_buffer (vm, bi1); |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 295 | b2 = vlib_get_buffer (vm, bi2); |
| 296 | b3 = vlib_get_buffer (vm, bi3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 298 | /* RX interface handles */ |
| 299 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 300 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 301 | sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX]; |
| 302 | sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 304 | h0 = vlib_buffer_get_current (b0); |
| 305 | h1 = vlib_buffer_get_current (b1); |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 306 | h2 = vlib_buffer_get_current (b2); |
| 307 | h3 = vlib_buffer_get_current (b3); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 308 | |
Dave Barach | 681abe4 | 2017-02-15 09:01:01 -0500 | [diff] [blame] | 309 | if (do_trace) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 310 | { |
| 311 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 312 | { |
| 313 | l2fwd_trace_t *t = |
| 314 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 315 | t->sw_if_index = sw_if_index0; |
| 316 | t->bd_index = vnet_buffer (b0)->l2.bd_index; |
| 317 | clib_memcpy (t->src, h0->src_address, 6); |
| 318 | clib_memcpy (t->dst, h0->dst_address, 6); |
| 319 | } |
| 320 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 321 | { |
| 322 | l2fwd_trace_t *t = |
| 323 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 324 | t->sw_if_index = sw_if_index1; |
| 325 | t->bd_index = vnet_buffer (b1)->l2.bd_index; |
| 326 | clib_memcpy (t->src, h1->src_address, 6); |
| 327 | clib_memcpy (t->dst, h1->dst_address, 6); |
| 328 | } |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 329 | if (b2->flags & VLIB_BUFFER_IS_TRACED) |
| 330 | { |
| 331 | l2fwd_trace_t *t = |
| 332 | vlib_add_trace (vm, node, b2, sizeof (*t)); |
| 333 | t->sw_if_index = sw_if_index2; |
| 334 | t->bd_index = vnet_buffer (b2)->l2.bd_index; |
| 335 | clib_memcpy (t->src, h2->src_address, 6); |
| 336 | clib_memcpy (t->dst, h2->dst_address, 6); |
| 337 | } |
| 338 | if (b3->flags & VLIB_BUFFER_IS_TRACED) |
| 339 | { |
| 340 | l2fwd_trace_t *t = |
| 341 | vlib_add_trace (vm, node, b3, sizeof (*t)); |
| 342 | t->sw_if_index = sw_if_index3; |
| 343 | t->bd_index = vnet_buffer (b3)->l2.bd_index; |
| 344 | clib_memcpy (t->src, h3->src_address, 6); |
| 345 | clib_memcpy (t->dst, h3->dst_address, 6); |
| 346 | } |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* process 2 pkts */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | #ifdef COUNTERS |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 351 | em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | #endif |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 353 | /* *INDENT-OFF* */ |
| 354 | l2fib_lookup_4 (msm->mac_table, &cached_key, &cached_result, |
| 355 | h0->dst_address, h1->dst_address, |
| 356 | h2->dst_address, h3->dst_address, |
| 357 | vnet_buffer (b0)->l2.bd_index, |
| 358 | vnet_buffer (b1)->l2.bd_index, |
| 359 | vnet_buffer (b2)->l2.bd_index, |
| 360 | vnet_buffer (b3)->l2.bd_index, |
| 361 | &key0, /* not used */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 362 | &key1, /* not used */ |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 363 | &key2, /* not used */ |
| 364 | &key3, /* not used */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 365 | &bucket0, /* not used */ |
| 366 | &bucket1, /* not used */ |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 367 | &bucket2, /* not used */ |
| 368 | &bucket3, /* not used */ |
| 369 | &result0, |
| 370 | &result1, |
| 371 | &result2, |
| 372 | &result3); |
| 373 | /* *INDENT-ON* */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 374 | l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, |
| 375 | &next0); |
| 376 | l2fwd_process (vm, node, msm, em, b1, sw_if_index1, &result1, |
| 377 | &next1); |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 378 | l2fwd_process (vm, node, msm, em, b2, sw_if_index2, &result2, |
| 379 | &next2); |
| 380 | l2fwd_process (vm, node, msm, em, b3, sw_if_index3, &result3, |
| 381 | &next3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 383 | /* verify speculative enqueues, maybe switch current next frame */ |
| 384 | /* if next0==next1==next_index then nothing special needs to be done */ |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 385 | vlib_validate_buffer_enqueue_x4 (vm, node, next_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 386 | to_next, n_left_to_next, |
Damjan Marion | daa2cd1 | 2016-11-22 21:10:25 -0800 | [diff] [blame] | 387 | bi0, bi1, bi2, bi3, |
| 388 | next0, next1, next2, next3); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | while (n_left_from > 0 && n_left_to_next > 0) |
| 392 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 393 | u32 bi0; |
| 394 | vlib_buffer_t *b0; |
| 395 | u32 next0; |
| 396 | u32 sw_if_index0; |
| 397 | ethernet_header_t *h0; |
| 398 | l2fib_entry_key_t key0; |
| 399 | l2fib_entry_result_t result0; |
| 400 | u32 bucket0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 402 | /* speculatively enqueue b0 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | bi0 = from[0]; |
| 404 | to_next[0] = bi0; |
| 405 | from += 1; |
| 406 | to_next += 1; |
| 407 | n_left_from -= 1; |
| 408 | n_left_to_next -= 1; |
| 409 | |
| 410 | b0 = vlib_get_buffer (vm, bi0); |
| 411 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 412 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 414 | h0 = vlib_buffer_get_current (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | |
Dave Barach | 681abe4 | 2017-02-15 09:01:01 -0500 | [diff] [blame] | 416 | if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 417 | { |
| 418 | l2fwd_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 419 | t->sw_if_index = sw_if_index0; |
| 420 | t->bd_index = vnet_buffer (b0)->l2.bd_index; |
| 421 | clib_memcpy (t->src, h0->src_address, 6); |
| 422 | clib_memcpy (t->dst, h0->dst_address, 6); |
| 423 | } |
| 424 | |
| 425 | /* process 1 pkt */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | #ifdef COUNTERS |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 427 | em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | #endif |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 429 | l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result, h0->dst_address, vnet_buffer (b0)->l2.bd_index, &key0, /* not used */ |
| 430 | &bucket0, /* not used */ |
| 431 | &result0); |
| 432 | l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, |
| 433 | &next0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 435 | /* verify speculative enqueue, maybe switch current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 437 | to_next, n_left_to_next, |
| 438 | bi0, next0); |
| 439 | } |
| 440 | |
| 441 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 442 | } |
| 443 | |
| 444 | return frame->n_vectors; |
| 445 | } |
| 446 | |
Dave Barach | 681abe4 | 2017-02-15 09:01:01 -0500 | [diff] [blame] | 447 | static uword |
| 448 | l2fwd_node_fn (vlib_main_t * vm, |
| 449 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 450 | { |
| 451 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 452 | return l2fwd_node_inline (vm, node, frame, 1 /* do_trace */ ); |
| 453 | return l2fwd_node_inline (vm, node, frame, 0 /* do_trace */ ); |
| 454 | } |
| 455 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 456 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | VLIB_REGISTER_NODE (l2fwd_node,static) = { |
| 458 | .function = l2fwd_node_fn, |
| 459 | .name = "l2-fwd", |
| 460 | .vector_size = sizeof (u32), |
| 461 | .format_trace = format_l2fwd_trace, |
| 462 | .type = VLIB_NODE_TYPE_INTERNAL, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 463 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | .n_errors = ARRAY_LEN(l2fwd_error_strings), |
| 465 | .error_strings = l2fwd_error_strings, |
| 466 | |
| 467 | .n_next_nodes = L2FWD_N_NEXT, |
| 468 | |
| 469 | /* edit / add dispositions here */ |
| 470 | .next_nodes = { |
| 471 | [L2FWD_NEXT_L2_OUTPUT] = "l2-output", |
| 472 | [L2FWD_NEXT_FLOOD] = "l2-flood", |
| 473 | [L2FWD_NEXT_DROP] = "error-drop", |
| 474 | }, |
| 475 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 476 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 478 | VLIB_NODE_FUNCTION_MULTIARCH (l2fwd_node, l2fwd_node_fn) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 479 | clib_error_t *l2fwd_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 481 | l2fwd_main_t *mp = &l2fwd_main; |
| 482 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | mp->vlib_main = vm; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 484 | mp->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | |
| 486 | /* init the hash table ptr */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 487 | mp->mac_table = get_mac_table (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 489 | /* Initialize the next nodes for each ethertype */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | next_by_ethertype_init (&mp->l3_next); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | VLIB_INIT_FUNCTION (l2fwd_init); |
| 496 | |
| 497 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 498 | /** 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] | 499 | void |
| 500 | l2fwd_register_input_type (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 501 | ethernet_type_t type, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 503 | l2fwd_main_t *mp = &l2fwd_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | u32 next_index; |
| 505 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 506 | next_index = vlib_node_add_next (vm, l2fwd_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
| 508 | next_by_ethertype_register (&mp->l3_next, type, next_index); |
| 509 | } |
| 510 | |
| 511 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 512 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 513 | * Set subinterface forward enable/disable. |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 514 | * The CLI format is: |
| 515 | * set interface l2 forward <interface> [disable] |
| 516 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | static clib_error_t * |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 518 | 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] | 519 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 520 | vnet_main_t *vnm = vnet_get_main (); |
| 521 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | u32 sw_if_index; |
| 523 | u32 enable; |
| 524 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 525 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | { |
| 527 | error = clib_error_return (0, "unknown interface `%U'", |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 528 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | goto done; |
| 530 | } |
| 531 | |
| 532 | enable = 1; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 533 | if (unformat (input, "disable")) |
| 534 | { |
| 535 | enable = 0; |
| 536 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 538 | /* set the interface flag */ |
| 539 | if (l2input_intf_config (sw_if_index)->xconnect) |
| 540 | { |
| 541 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_XCONNECT, enable); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FWD, enable); |
| 546 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 548 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | return error; |
| 550 | } |
| 551 | |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 552 | /*? |
| 553 | * Layer 2 unicast forwarding can be enabled and disabled on each |
| 554 | * interface and on each bridge-domain. Use this command to |
| 555 | * manage interfaces. It is enabled by default. |
| 556 | * |
| 557 | * @cliexpar |
| 558 | * Example of how to enable fowarding: |
| 559 | * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0} |
| 560 | * Example of how to disable fowarding: |
| 561 | * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0 disable} |
| 562 | ?*/ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 563 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | VLIB_CLI_COMMAND (int_fwd_cli, static) = { |
| 565 | .path = "set interface l2 forward", |
| 566 | .short_help = "set interface l2 forward <interface> [disable]", |
| 567 | .function = int_fwd, |
| 568 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 569 | /* *INDENT-ON* */ |
| 570 | |
| 571 | /* |
| 572 | * fd.io coding-style-patch-verification: ON |
| 573 | * |
| 574 | * Local Variables: |
| 575 | * eval: (c-set-style "gnu") |
| 576 | * End: |
| 577 | */ |