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