blob: 3b9b1dbcaa90d744a427113ac462a841fc6667e8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_learn.c : layer 2 learning 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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vlib/cli.h>
22
23#include <vnet/l2/l2_input.h>
24#include <vnet/l2/feat_bitmap.h>
25#include <vnet/l2/l2_fib.h>
26#include <vnet/l2/l2_learn.h>
27
28#include <vppinfra/error.h>
29#include <vppinfra/hash.h>
30
Neale Rannseb1525f2018-09-09 04:41:02 -040031#ifndef CLIB_MARCH_VARIANT
Dave Wallace71612d62017-10-24 01:32:41 -040032l2learn_main_t l2learn_main;
Neale Rannseb1525f2018-09-09 04:41:02 -040033#endif
Dave Wallace71612d62017-10-24 01:32:41 -040034
Chris Luke16bcf7d2016-09-01 14:31:46 -040035/**
36 * @file
Billy McFall22aa3e92016-09-09 08:46:40 -040037 * @brief Ethernet Bridge Learning.
Ed Warnickecb9cada2015-12-08 15:45:58 -070038 *
39 * Populate the mac table with entries mapping the packet's source mac + bridge
Dave Barach97d8dc22016-08-15 15:31:15 -040040 * domain ID to the input sw_if_index.
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 *
42 * Note that learning and forwarding are separate graph nodes. This means that
43 * for a set of packets, all learning is performed first, then all nodes are
44 * forwarded. The forwarding is done based on the end-state of the mac table,
45 * instead of the state after each packet. Thus the forwarding results could
46 * differ in certain cases (mac move tests), but this not expected to cause
47 * problems in real-world networks. It is much simpler to separate learning
48 * and forwarding into separate nodes.
49 */
50
51
Dave Barach97d8dc22016-08-15 15:31:15 -040052typedef struct
53{
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 u8 src[6];
55 u8 dst[6];
56 u32 sw_if_index;
57 u16 bd_index;
58} l2learn_trace_t;
59
60
61/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040062static u8 *
63format_l2learn_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040067 l2learn_trace_t *t = va_arg (*args, l2learn_trace_t *);
68
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 s = format (s, "l2-learn: sw_if_index %d dst %U src %U bd_index %d",
Dave Barach97d8dc22016-08-15 15:31:15 -040070 t->sw_if_index,
71 format_ethernet_address, t->dst,
72 format_ethernet_address, t->src, t->bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 return s;
74}
75
Damjan Mariond770cfc2019-09-02 19:00:33 +020076extern vlib_node_registration_t l2learn_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78#define foreach_l2learn_error \
79_(L2LEARN, "L2 learn packets") \
80_(MISS, "L2 learn misses") \
81_(MAC_MOVE, "L2 mac moves") \
82_(MAC_MOVE_VIOLATE, "L2 mac move violations") \
83_(LIMIT, "L2 not learned due to limit") \
John Lodbfa5742017-09-02 08:27:36 -040084_(HIT_UPDATE, "L2 learn hit updates") \
Dave Barach97d8dc22016-08-15 15:31:15 -040085_(FILTER_DROP, "L2 filter mac drops")
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barach97d8dc22016-08-15 15:31:15 -040087typedef enum
88{
Ed Warnickecb9cada2015-12-08 15:45:58 -070089#define _(sym,str) L2LEARN_ERROR_##sym,
90 foreach_l2learn_error
91#undef _
Dave Barach97d8dc22016-08-15 15:31:15 -040092 L2LEARN_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070093} l2learn_error_t;
94
Dave Barach97d8dc22016-08-15 15:31:15 -040095static char *l2learn_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070096#define _(sym,string) string,
97 foreach_l2learn_error
98#undef _
99};
100
Dave Barach97d8dc22016-08-15 15:31:15 -0400101typedef enum
102{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 L2LEARN_NEXT_L2FWD,
104 L2LEARN_NEXT_DROP,
105 L2LEARN_N_NEXT,
106} l2learn_next_t;
107
108
Chris Luke16bcf7d2016-09-01 14:31:46 -0400109/** Perform learning on one packet based on the mac table lookup result. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
111static_always_inline void
112l2learn_process (vlib_node_runtime_t * node,
Dave Barach97d8dc22016-08-15 15:31:15 -0400113 l2learn_main_t * msm,
114 u64 * counter_base,
115 vlib_buffer_t * b0,
116 u32 sw_if_index0,
117 l2fib_entry_key_t * key0,
118 l2fib_entry_key_t * cached_key,
John Lodbfa5742017-09-02 08:27:36 -0400119 u32 * count,
Neale Rannsf0fa3172018-09-12 07:37:43 -0400120 l2fib_entry_result_t * result0, u16 * next0, u8 timestamp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121{
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100122 l2_bridge_domain_t *bd_config =
123 vec_elt_at_index (l2input_main.bd_configs, vnet_buffer (b0)->l2.bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400124 /* Set up the default next node (typically L2FWD) */
John Lobeb0b2e2017-07-22 00:21:36 -0400125 *next0 = vnet_l2_feature_next (b0, msm->feat_next_node_index,
126 L2INPUT_FEAT_LEARN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Dave Barach97d8dc22016-08-15 15:31:15 -0400128 /* Check mac table lookup result */
Dave Barach97d8dc22016-08-15 15:31:15 -0400129 if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
130 {
John Lo8d00fff2017-08-03 00:35:36 -0400131 /* Entry in L2FIB with matching sw_if_index matched - normal fast path */
John Lodbfa5742017-09-02 08:27:36 -0400132 u32 dtime = timestamp - result0->fields.timestamp;
Neale Ranns47a3d992020-09-29 15:38:51 +0000133 u32 dsn = (result0->fields.sn - vnet_buffer (b0)->l2.l2fib_sn);
John Lo5a6508d2017-10-03 13:13:47 -0400134 u32 check = (dtime && vnet_buffer (b0)->l2.bd_age) || dsn;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300135
John Lodbfa5742017-09-02 08:27:36 -0400136 if (PREDICT_TRUE (check == 0))
137 return; /* MAC entry up to date */
Neale Rannsb54d0812018-09-06 06:22:56 -0700138 if (l2fib_entry_result_is_set_AGE_NOT (result0))
John Lodbfa5742017-09-02 08:27:36 -0400139 return; /* Static MAC always age_not */
140 if (msm->global_learn_count > msm->global_learn_limit)
John Loe531f4c2017-08-22 09:16:50 -0400141 return; /* Above learn limit - do not update */
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100142 if (bd_config->learn_count > bd_config->learn_limit)
143 return; /* Above bridge domain learn limit - do not update */
John Lodbfa5742017-09-02 08:27:36 -0400144
145 /* Limit updates per l2-learn node call to avoid prolonged update burst
John Lo5a6508d2017-10-03 13:13:47 -0400146 * as dtime advance over 1 minute mark, unless more than 1 min behind
147 * or SN obsolete */
148 if ((*count > 2) && (dtime == 1) && (dsn == 0))
John Lodbfa5742017-09-02 08:27:36 -0400149 return;
150
151 counter_base[L2LEARN_ERROR_HIT_UPDATE] += 1;
152 *count += 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400153 }
154 else if (result0->raw == ~0)
155 {
John Lo8d00fff2017-08-03 00:35:36 -0400156 /* Entry not in L2FIB - add it */
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 counter_base[L2LEARN_ERROR_MISS] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100159 if ((msm->global_learn_count >= msm->global_learn_limit) ||
160 (bd_config->learn_count >= bd_config->learn_limit))
Dave Barach97d8dc22016-08-15 15:31:15 -0400161 {
162 /*
163 * Global limit reached. Do not learn the mac but forward the packet.
164 * In the future, limits could also be per-interface or bridge-domain.
165 */
166 counter_base[L2LEARN_ERROR_LIMIT] += 1;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300167 return;
Dave Barach97d8dc22016-08-15 15:31:15 -0400168 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
John Lo8d00fff2017-08-03 00:35:36 -0400170 /* Do not learn if mac is 0 */
171 l2fib_entry_key_t key = *key0;
172 key.fields.bd_index = 0;
173 if (key.raw == 0)
174 return;
175
Eyal Bari31a71ab2017-06-25 14:42:33 +0300176 /* It is ok to learn */
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100177 /* learn_count variable may have little inaccuracy because they are not
178 * incremented/decremented with atomic operations */
179 /* l2fib_scan is call every 2sec fixing potential inaccuracy */
Eyal Bari31a71ab2017-06-25 14:42:33 +0300180 msm->global_learn_count++;
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100181 bd_config->learn_count++;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300182 result0->raw = 0; /* clear all fields */
183 result0->fields.sw_if_index = sw_if_index0;
Neale Rannsb54d0812018-09-06 06:22:56 -0700184 if (msm->client_pid != 0)
185 l2fib_entry_result_set_LRN_EVT (result0);
186 else
187 l2fib_entry_result_clear_LRN_EVT (result0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400188 }
189 else
190 {
John Lo8d00fff2017-08-03 00:35:36 -0400191 /* Entry in L2FIB with different sw_if_index - mac move or filter */
Neale Rannsb54d0812018-09-06 06:22:56 -0700192 if (l2fib_entry_result_is_set_FILTER (result0))
Eyal Bari31a71ab2017-06-25 14:42:33 +0300193 {
194 ASSERT (result0->fields.sw_if_index == ~0);
195 /* drop packet because lookup matched a filter mac entry */
196 b0->error = node->errors[L2LEARN_ERROR_FILTER_DROP];
197 *next0 = L2LEARN_NEXT_DROP;
198 return;
199 }
200
Neale Rannsb54d0812018-09-06 06:22:56 -0700201 if (l2fib_entry_result_is_set_STATIC (result0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400202 {
203 /*
204 * Don't overwrite a static mac
205 * TODO: Check violation policy. For now drop the packet
206 */
207 b0->error = node->errors[L2LEARN_ERROR_MAC_MOVE_VIOLATE];
208 *next0 = L2LEARN_NEXT_DROP;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300209 return;
Dave Barach97d8dc22016-08-15 15:31:15 -0400210 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400211
Eyal Bari31a71ab2017-06-25 14:42:33 +0300212 /*
213 * TODO: may want to rate limit mac moves
214 * TODO: check global/bridge domain/interface learn limits
215 */
216 result0->fields.sw_if_index = sw_if_index0;
Neale Rannsb54d0812018-09-06 06:22:56 -0700217 if (l2fib_entry_result_is_set_AGE_NOT (result0))
John Lo8d00fff2017-08-03 00:35:36 -0400218 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700219 /* The mac was provisioned */
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100220 /* learn_count variable may have little inaccuracy because they are
221 * not incremented/decremented with atomic operations */
222 /* l2fib_scan is call every 2sec fixing potential inaccuracy */
John Lo8d00fff2017-08-03 00:35:36 -0400223 msm->global_learn_count++;
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100224 bd_config->learn_count++;
225
Neale Rannsb54d0812018-09-06 06:22:56 -0700226 l2fib_entry_result_clear_AGE_NOT (result0);
John Lo8d00fff2017-08-03 00:35:36 -0400227 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700228 if (msm->client_pid != 0)
229 l2fib_entry_result_set_bits (result0,
230 (L2FIB_ENTRY_RESULT_FLAG_LRN_EVT |
231 L2FIB_ENTRY_RESULT_FLAG_LRN_MOV));
232 else
233 l2fib_entry_result_clear_bits (result0,
234 (L2FIB_ENTRY_RESULT_FLAG_LRN_EVT |
235 L2FIB_ENTRY_RESULT_FLAG_LRN_MOV));
John Lo8d00fff2017-08-03 00:35:36 -0400236 counter_base[L2LEARN_ERROR_MAC_MOVE] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 }
238
Eyal Bari31a71ab2017-06-25 14:42:33 +0300239 /* Update the entry */
240 result0->fields.timestamp = timestamp;
Neale Ranns47a3d992020-09-29 15:38:51 +0000241 result0->fields.sn = vnet_buffer (b0)->l2.l2fib_sn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Eyal Bari31a71ab2017-06-25 14:42:33 +0300243 BVT (clib_bihash_kv) kv;
244 kv.key = key0->raw;
245 kv.value = result0->raw;
246 BV (clib_bihash_add_del) (msm->mac_table, &kv, 1 /* is_add */ );
John Lodbfa5742017-09-02 08:27:36 -0400247
248 /* Invalidate the cache */
249 cached_key->raw = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250}
251
252
Dave Barach681abe42017-02-15 09:01:01 -0500253static_always_inline uword
254l2learn_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
255 vlib_frame_t * frame, int do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256{
Neale Rannsf0fa3172018-09-12 07:37:43 -0400257 u32 n_left, *from;
Dave Barach97d8dc22016-08-15 15:31:15 -0400258 l2learn_main_t *msm = &l2learn_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 vlib_node_t *n = vlib_get_node (vm, l2learn_node.index);
260 u32 node_counter_base_index = n->error_heap_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400261 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 l2fib_entry_key_t cached_key;
263 l2fib_entry_result_t cached_result;
Damjan Mariond171d482016-12-05 14:16:38 +0100264 u8 timestamp = (u8) (vlib_time_now (vm) / 60);
John Lodbfa5742017-09-02 08:27:36 -0400265 u32 count = 0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400266 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
267 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
269 from = vlib_frame_vector_args (frame);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400270 n_left = frame->n_vectors; /* number of packets to process */
271 vlib_get_buffers (vm, from, bufs, n_left);
272 next = nexts;
273 b = bufs;
Dave Barach97d8dc22016-08-15 15:31:15 -0400274
275 /* Clear the one-entry cache in case mac table was updated */
276 cached_key.raw = ~0;
277 cached_result.raw = ~0; /* warning be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Neale Rannsf0fa3172018-09-12 07:37:43 -0400279 while (n_left > 8)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400281 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
282 const ethernet_header_t *h0, *h1, *h2, *h3;
283 l2fib_entry_key_t key0, key1, key2, key3;
284 l2fib_entry_result_t result0, result1, result2, result3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Neale Rannsf0fa3172018-09-12 07:37:43 -0400286 /* Prefetch next iteration. */
287 {
288 /* buffer header is read and written, so use LOAD
289 * prefetch */
290 vlib_prefetch_buffer_header (b[4], LOAD);
291 vlib_prefetch_buffer_header (b[5], LOAD);
292 vlib_prefetch_buffer_header (b[6], LOAD);
293 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Neale Rannsf0fa3172018-09-12 07:37:43 -0400295 CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, LOAD);
296 CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, LOAD);
297 CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, LOAD);
298 CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, LOAD);
299 }
300
301 /* RX interface handles */
302 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
303 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
304 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
305 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
306
307 /* Process 4 x pkts */
308
309 h0 = vlib_buffer_get_current (b[0]);
310 h1 = vlib_buffer_get_current (b[1]);
311 h2 = vlib_buffer_get_current (b[2]);
312 h3 = vlib_buffer_get_current (b[3]);
313
314 if (do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400316 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
Dave Barach97d8dc22016-08-15 15:31:15 -0400317 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400318 l2learn_trace_t *t =
319 vlib_add_trace (vm, node, b[0], sizeof (*t));
Dave Barach97d8dc22016-08-15 15:31:15 -0400320 t->sw_if_index = sw_if_index0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400321 t->bd_index = vnet_buffer (b[0])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500322 clib_memcpy_fast (t->src, h0->src_address, 6);
323 clib_memcpy_fast (t->dst, h0->dst_address, 6);
Dave Barach97d8dc22016-08-15 15:31:15 -0400324 }
Neale Rannsf0fa3172018-09-12 07:37:43 -0400325 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
326 {
327 l2learn_trace_t *t =
328 vlib_add_trace (vm, node, b[1], sizeof (*t));
329 t->sw_if_index = sw_if_index1;
330 t->bd_index = vnet_buffer (b[1])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500331 clib_memcpy_fast (t->src, h1->src_address, 6);
332 clib_memcpy_fast (t->dst, h1->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400333 }
334 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
335 {
336 l2learn_trace_t *t =
337 vlib_add_trace (vm, node, b[2], sizeof (*t));
338 t->sw_if_index = sw_if_index2;
339 t->bd_index = vnet_buffer (b[2])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500340 clib_memcpy_fast (t->src, h2->src_address, 6);
341 clib_memcpy_fast (t->dst, h2->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400342 }
343 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
344 {
345 l2learn_trace_t *t =
346 vlib_add_trace (vm, node, b[3], sizeof (*t));
347 t->sw_if_index = sw_if_index3;
348 t->bd_index = vnet_buffer (b[3])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500349 clib_memcpy_fast (t->src, h3->src_address, 6);
350 clib_memcpy_fast (t->dst, h3->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400351 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352 }
353
Neale Rannsf0fa3172018-09-12 07:37:43 -0400354 /* process 4 pkts */
355 vlib_node_increment_counter (vm, l2learn_node.index,
356 L2LEARN_ERROR_L2LEARN, 4);
357
358 l2fib_lookup_4 (msm->mac_table, &cached_key, &cached_result,
359 h0->src_address,
360 h1->src_address,
361 h2->src_address,
362 h3->src_address,
363 vnet_buffer (b[0])->l2.bd_index,
364 vnet_buffer (b[1])->l2.bd_index,
365 vnet_buffer (b[2])->l2.bd_index,
366 vnet_buffer (b[3])->l2.bd_index,
367 &key0, &key1, &key2, &key3,
Neale Rannsf0fa3172018-09-12 07:37:43 -0400368 &result0, &result1, &result2, &result3);
369
370 l2learn_process (node, msm, &em->counters[node_counter_base_index],
371 b[0], sw_if_index0, &key0, &cached_key,
372 &count, &result0, next, timestamp);
373
374 l2learn_process (node, msm, &em->counters[node_counter_base_index],
375 b[1], sw_if_index1, &key1, &cached_key,
376 &count, &result1, next + 1, timestamp);
377
378 l2learn_process (node, msm, &em->counters[node_counter_base_index],
379 b[2], sw_if_index2, &key2, &cached_key,
380 &count, &result2, next + 2, timestamp);
381
382 l2learn_process (node, msm, &em->counters[node_counter_base_index],
383 b[3], sw_if_index3, &key3, &cached_key,
384 &count, &result3, next + 3, timestamp);
385
386 next += 4;
387 b += 4;
388 n_left -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 }
390
Neale Rannsf0fa3172018-09-12 07:37:43 -0400391 while (n_left > 0)
392 {
393 u32 sw_if_index0;
394 ethernet_header_t *h0;
395 l2fib_entry_key_t key0;
396 l2fib_entry_result_t result0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400397
398 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
399
400 h0 = vlib_buffer_get_current (b[0]);
401
402 if (do_trace && PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
403 {
404 l2learn_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
405 t->sw_if_index = sw_if_index0;
406 t->bd_index = vnet_buffer (b[0])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500407 clib_memcpy_fast (t->src, h0->src_address, 6);
408 clib_memcpy_fast (t->dst, h0->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400409 }
410
411 /* process 1 pkt */
412 vlib_node_increment_counter (vm, l2learn_node.index,
413 L2LEARN_ERROR_L2LEARN, 1);
414
415
416 l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result,
417 h0->src_address, vnet_buffer (b[0])->l2.bd_index,
Eyal Bari11d47af2018-10-31 10:55:33 +0200418 &key0, &result0);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400419
420 l2learn_process (node, msm, &em->counters[node_counter_base_index],
421 b[0], sw_if_index0, &key0, &cached_key,
422 &count, &result0, next, timestamp);
423
424 next += 1;
425 b += 1;
426 n_left -= 1;
427 }
428
429 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 return frame->n_vectors;
432}
433
Neale Rannseb1525f2018-09-09 04:41:02 -0400434VLIB_NODE_FN (l2learn_node) (vlib_main_t * vm,
435 vlib_node_runtime_t * node, vlib_frame_t * frame)
Dave Barach681abe42017-02-15 09:01:01 -0500436{
437 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
438 return l2learn_node_inline (vm, node, frame, 1 /* do_trace */ );
439 return l2learn_node_inline (vm, node, frame, 0 /* do_trace */ );
440}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barach97d8dc22016-08-15 15:31:15 -0400442/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200443VLIB_REGISTER_NODE (l2learn_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 .name = "l2-learn",
445 .vector_size = sizeof (u32),
446 .format_trace = format_l2learn_trace,
447 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400448
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 .n_errors = ARRAY_LEN(l2learn_error_strings),
450 .error_strings = l2learn_error_strings,
451
452 .n_next_nodes = L2LEARN_N_NEXT,
453
454 /* edit / add dispositions here */
455 .next_nodes = {
456 [L2LEARN_NEXT_DROP] = "error-drop",
457 [L2LEARN_NEXT_L2FWD] = "l2-fwd",
458 },
459};
Dave Barach97d8dc22016-08-15 15:31:15 -0400460/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
Neale Rannseb1525f2018-09-09 04:41:02 -0400462#ifndef CLIB_MARCH_VARIANT
463clib_error_t *
464l2learn_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465{
Dave Barach97d8dc22016-08-15 15:31:15 -0400466 l2learn_main_t *mp = &l2learn_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Dave Barach97d8dc22016-08-15 15:31:15 -0400468 mp->vlib_main = vm;
469 mp->vnet_main = vnet_get_main ();
470
471 /* Initialize the feature next-node indexes */
472 feat_bitmap_init_next_nodes (vm,
473 l2learn_node.index,
474 L2INPUT_N_FEAT,
475 l2input_get_feat_names (),
476 mp->feat_next_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
478 /* init the hash table ptr */
Dave Barach97d8dc22016-08-15 15:31:15 -0400479 mp->mac_table = get_mac_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Dave Barach97d8dc22016-08-15 15:31:15 -0400481 /*
482 * Set the default number of dynamically learned macs to the number
483 * of buckets.
484 */
John Lo8d00fff2017-08-03 00:35:36 -0400485 mp->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100487 /*
488 * Set the default number of dynamically learned macs to the number
489 * of buckets.
490 */
491 mp->bd_default_learn_limit = L2LEARN_DEFAULT_LIMIT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 return 0;
493}
494
495VLIB_INIT_FUNCTION (l2learn_init);
496
497
Dave Barach97d8dc22016-08-15 15:31:15 -0400498/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400499 * Set subinterface learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400500 * The CLI format is:
501 * set interface l2 learn <interface> [disable]
502 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503static clib_error_t *
504int_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400505 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506{
Dave Barach97d8dc22016-08-15 15:31:15 -0400507 vnet_main_t *vnm = vnet_get_main ();
508 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 u32 sw_if_index;
510 u32 enable;
511
Dave Barach97d8dc22016-08-15 15:31:15 -0400512 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 {
514 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400515 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 goto done;
517 }
518
519 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400520 if (unformat (input, "disable"))
521 {
522 enable = 0;
523 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barach97d8dc22016-08-15 15:31:15 -0400525 /* set the interface flag */
526 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_LEARN, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Dave Barach97d8dc22016-08-15 15:31:15 -0400528done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 return error;
530}
531
Billy McFall22aa3e92016-09-09 08:46:40 -0400532/*?
533 * Layer 2 learning can be enabled and disabled on each
534 * interface and on each bridge-domain. Use this command to
535 * manage interfaces. It is enabled by default.
536 *
537 * @cliexpar
538 * Example of how to enable learning:
539 * @cliexcmd{set interface l2 learn GigabitEthernet0/8/0}
540 * Example of how to disable learning:
541 * @cliexcmd{set interface l2 learn GigabitEthernet0/8/0 disable}
542?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400543/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544VLIB_CLI_COMMAND (int_learn_cli, static) = {
545 .path = "set interface l2 learn",
546 .short_help = "set interface l2 learn <interface> [disable]",
547 .function = int_learn,
548};
Dave Barach97d8dc22016-08-15 15:31:15 -0400549/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551
552static clib_error_t *
553l2learn_config (vlib_main_t * vm, unformat_input_t * input)
554{
555 l2learn_main_t *mp = &l2learn_main;
556
557 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
558 {
559 if (unformat (input, "limit %d", &mp->global_learn_limit))
Dave Barach97d8dc22016-08-15 15:31:15 -0400560 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
562 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400563 return clib_error_return (0, "unknown input `%U'",
564 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 }
566
567 return 0;
568}
569
570VLIB_CONFIG_FUNCTION (l2learn_config, "l2learn");
571
Neale Rannseb1525f2018-09-09 04:41:02 -0400572#endif
573
Dave Barach97d8dc22016-08-15 15:31:15 -0400574
575/*
576 * fd.io coding-style-patch-verification: ON
577 *
578 * Local Variables:
579 * eval: (c-set-style "gnu")
580 * End:
581 */