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