blob: 6f2201b3ade4f9785f3f32bde402371ecc44f5f3 [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{
Dave Barach97d8dc22016-08-15 15:31:15 -0400122 /* Set up the default next node (typically L2FWD) */
John Lobeb0b2e2017-07-22 00:21:36 -0400123 *next0 = vnet_l2_feature_next (b0, msm->feat_next_node_index,
124 L2INPUT_FEAT_LEARN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barach97d8dc22016-08-15 15:31:15 -0400126 /* Check mac table lookup result */
Dave Barach97d8dc22016-08-15 15:31:15 -0400127 if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
128 {
John Lo8d00fff2017-08-03 00:35:36 -0400129 /* Entry in L2FIB with matching sw_if_index matched - normal fast path */
John Lodbfa5742017-09-02 08:27:36 -0400130 u32 dtime = timestamp - result0->fields.timestamp;
Neale Ranns47a3d992020-09-29 15:38:51 +0000131 u32 dsn = (result0->fields.sn - vnet_buffer (b0)->l2.l2fib_sn);
John Lo5a6508d2017-10-03 13:13:47 -0400132 u32 check = (dtime && vnet_buffer (b0)->l2.bd_age) || dsn;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300133
John Lodbfa5742017-09-02 08:27:36 -0400134 if (PREDICT_TRUE (check == 0))
135 return; /* MAC entry up to date */
Neale Rannsb54d0812018-09-06 06:22:56 -0700136 if (l2fib_entry_result_is_set_AGE_NOT (result0))
John Lodbfa5742017-09-02 08:27:36 -0400137 return; /* Static MAC always age_not */
138 if (msm->global_learn_count > msm->global_learn_limit)
John Loe531f4c2017-08-22 09:16:50 -0400139 return; /* Above learn limit - do not update */
John Lodbfa5742017-09-02 08:27:36 -0400140
141 /* Limit updates per l2-learn node call to avoid prolonged update burst
John Lo5a6508d2017-10-03 13:13:47 -0400142 * as dtime advance over 1 minute mark, unless more than 1 min behind
143 * or SN obsolete */
144 if ((*count > 2) && (dtime == 1) && (dsn == 0))
John Lodbfa5742017-09-02 08:27:36 -0400145 return;
146
147 counter_base[L2LEARN_ERROR_HIT_UPDATE] += 1;
148 *count += 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400149 }
150 else if (result0->raw == ~0)
151 {
John Lo8d00fff2017-08-03 00:35:36 -0400152 /* Entry not in L2FIB - add it */
Dave Barach97d8dc22016-08-15 15:31:15 -0400153 counter_base[L2LEARN_ERROR_MISS] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
John Lo8d00fff2017-08-03 00:35:36 -0400155 if (msm->global_learn_count >= msm->global_learn_limit)
Dave Barach97d8dc22016-08-15 15:31:15 -0400156 {
157 /*
158 * Global limit reached. Do not learn the mac but forward the packet.
159 * In the future, limits could also be per-interface or bridge-domain.
160 */
161 counter_base[L2LEARN_ERROR_LIMIT] += 1;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300162 return;
Dave Barach97d8dc22016-08-15 15:31:15 -0400163 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
John Lo8d00fff2017-08-03 00:35:36 -0400165 /* Do not learn if mac is 0 */
166 l2fib_entry_key_t key = *key0;
167 key.fields.bd_index = 0;
168 if (key.raw == 0)
169 return;
170
Eyal Bari31a71ab2017-06-25 14:42:33 +0300171 /* It is ok to learn */
172 msm->global_learn_count++;
173 result0->raw = 0; /* clear all fields */
174 result0->fields.sw_if_index = sw_if_index0;
Neale Rannsb54d0812018-09-06 06:22:56 -0700175 if (msm->client_pid != 0)
176 l2fib_entry_result_set_LRN_EVT (result0);
177 else
178 l2fib_entry_result_clear_LRN_EVT (result0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400179 }
180 else
181 {
John Lo8d00fff2017-08-03 00:35:36 -0400182 /* Entry in L2FIB with different sw_if_index - mac move or filter */
Neale Rannsb54d0812018-09-06 06:22:56 -0700183 if (l2fib_entry_result_is_set_FILTER (result0))
Eyal Bari31a71ab2017-06-25 14:42:33 +0300184 {
185 ASSERT (result0->fields.sw_if_index == ~0);
186 /* drop packet because lookup matched a filter mac entry */
187 b0->error = node->errors[L2LEARN_ERROR_FILTER_DROP];
188 *next0 = L2LEARN_NEXT_DROP;
189 return;
190 }
191
Neale Rannsb54d0812018-09-06 06:22:56 -0700192 if (l2fib_entry_result_is_set_STATIC (result0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400193 {
194 /*
195 * Don't overwrite a static mac
196 * TODO: Check violation policy. For now drop the packet
197 */
198 b0->error = node->errors[L2LEARN_ERROR_MAC_MOVE_VIOLATE];
199 *next0 = L2LEARN_NEXT_DROP;
Eyal Bari31a71ab2017-06-25 14:42:33 +0300200 return;
Dave Barach97d8dc22016-08-15 15:31:15 -0400201 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400202
Eyal Bari31a71ab2017-06-25 14:42:33 +0300203 /*
204 * TODO: may want to rate limit mac moves
205 * TODO: check global/bridge domain/interface learn limits
206 */
207 result0->fields.sw_if_index = sw_if_index0;
Neale Rannsb54d0812018-09-06 06:22:56 -0700208 if (l2fib_entry_result_is_set_AGE_NOT (result0))
John Lo8d00fff2017-08-03 00:35:36 -0400209 {
Neale Rannsb54d0812018-09-06 06:22:56 -0700210 /* The mac was provisioned */
John Lo8d00fff2017-08-03 00:35:36 -0400211 msm->global_learn_count++;
Neale Rannsb54d0812018-09-06 06:22:56 -0700212 l2fib_entry_result_clear_AGE_NOT (result0);
John Lo8d00fff2017-08-03 00:35:36 -0400213 }
Neale Rannsb54d0812018-09-06 06:22:56 -0700214 if (msm->client_pid != 0)
215 l2fib_entry_result_set_bits (result0,
216 (L2FIB_ENTRY_RESULT_FLAG_LRN_EVT |
217 L2FIB_ENTRY_RESULT_FLAG_LRN_MOV));
218 else
219 l2fib_entry_result_clear_bits (result0,
220 (L2FIB_ENTRY_RESULT_FLAG_LRN_EVT |
221 L2FIB_ENTRY_RESULT_FLAG_LRN_MOV));
John Lo8d00fff2017-08-03 00:35:36 -0400222 counter_base[L2LEARN_ERROR_MAC_MOVE] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 }
224
Eyal Bari31a71ab2017-06-25 14:42:33 +0300225 /* Update the entry */
226 result0->fields.timestamp = timestamp;
Neale Ranns47a3d992020-09-29 15:38:51 +0000227 result0->fields.sn = vnet_buffer (b0)->l2.l2fib_sn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Eyal Bari31a71ab2017-06-25 14:42:33 +0300229 BVT (clib_bihash_kv) kv;
230 kv.key = key0->raw;
231 kv.value = result0->raw;
232 BV (clib_bihash_add_del) (msm->mac_table, &kv, 1 /* is_add */ );
John Lodbfa5742017-09-02 08:27:36 -0400233
234 /* Invalidate the cache */
235 cached_key->raw = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236}
237
238
Dave Barach681abe42017-02-15 09:01:01 -0500239static_always_inline uword
240l2learn_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
241 vlib_frame_t * frame, int do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
Neale Rannsf0fa3172018-09-12 07:37:43 -0400243 u32 n_left, *from;
Dave Barach97d8dc22016-08-15 15:31:15 -0400244 l2learn_main_t *msm = &l2learn_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 vlib_node_t *n = vlib_get_node (vm, l2learn_node.index);
246 u32 node_counter_base_index = n->error_heap_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400247 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 l2fib_entry_key_t cached_key;
249 l2fib_entry_result_t cached_result;
Damjan Mariond171d482016-12-05 14:16:38 +0100250 u8 timestamp = (u8) (vlib_time_now (vm) / 60);
John Lodbfa5742017-09-02 08:27:36 -0400251 u32 count = 0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400252 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
253 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
255 from = vlib_frame_vector_args (frame);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400256 n_left = frame->n_vectors; /* number of packets to process */
257 vlib_get_buffers (vm, from, bufs, n_left);
258 next = nexts;
259 b = bufs;
Dave Barach97d8dc22016-08-15 15:31:15 -0400260
261 /* Clear the one-entry cache in case mac table was updated */
262 cached_key.raw = ~0;
263 cached_result.raw = ~0; /* warning be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Neale Rannsf0fa3172018-09-12 07:37:43 -0400265 while (n_left > 8)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400267 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
268 const ethernet_header_t *h0, *h1, *h2, *h3;
269 l2fib_entry_key_t key0, key1, key2, key3;
270 l2fib_entry_result_t result0, result1, result2, result3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Neale Rannsf0fa3172018-09-12 07:37:43 -0400272 /* Prefetch next iteration. */
273 {
274 /* buffer header is read and written, so use LOAD
275 * prefetch */
276 vlib_prefetch_buffer_header (b[4], LOAD);
277 vlib_prefetch_buffer_header (b[5], LOAD);
278 vlib_prefetch_buffer_header (b[6], LOAD);
279 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Neale Rannsf0fa3172018-09-12 07:37:43 -0400281 CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, LOAD);
282 CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, LOAD);
283 CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, LOAD);
284 CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, LOAD);
285 }
286
287 /* RX interface handles */
288 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
289 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
290 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
291 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
292
293 /* Process 4 x pkts */
294
295 h0 = vlib_buffer_get_current (b[0]);
296 h1 = vlib_buffer_get_current (b[1]);
297 h2 = vlib_buffer_get_current (b[2]);
298 h3 = vlib_buffer_get_current (b[3]);
299
300 if (do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400302 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
Dave Barach97d8dc22016-08-15 15:31:15 -0400303 {
Neale Rannsf0fa3172018-09-12 07:37:43 -0400304 l2learn_trace_t *t =
305 vlib_add_trace (vm, node, b[0], sizeof (*t));
Dave Barach97d8dc22016-08-15 15:31:15 -0400306 t->sw_if_index = sw_if_index0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400307 t->bd_index = vnet_buffer (b[0])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500308 clib_memcpy_fast (t->src, h0->src_address, 6);
309 clib_memcpy_fast (t->dst, h0->dst_address, 6);
Dave Barach97d8dc22016-08-15 15:31:15 -0400310 }
Neale Rannsf0fa3172018-09-12 07:37:43 -0400311 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
312 {
313 l2learn_trace_t *t =
314 vlib_add_trace (vm, node, b[1], sizeof (*t));
315 t->sw_if_index = sw_if_index1;
316 t->bd_index = vnet_buffer (b[1])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500317 clib_memcpy_fast (t->src, h1->src_address, 6);
318 clib_memcpy_fast (t->dst, h1->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400319 }
320 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
321 {
322 l2learn_trace_t *t =
323 vlib_add_trace (vm, node, b[2], sizeof (*t));
324 t->sw_if_index = sw_if_index2;
325 t->bd_index = vnet_buffer (b[2])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500326 clib_memcpy_fast (t->src, h2->src_address, 6);
327 clib_memcpy_fast (t->dst, h2->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400328 }
329 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
330 {
331 l2learn_trace_t *t =
332 vlib_add_trace (vm, node, b[3], sizeof (*t));
333 t->sw_if_index = sw_if_index3;
334 t->bd_index = vnet_buffer (b[3])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500335 clib_memcpy_fast (t->src, h3->src_address, 6);
336 clib_memcpy_fast (t->dst, h3->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400337 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 }
339
Neale Rannsf0fa3172018-09-12 07:37:43 -0400340 /* process 4 pkts */
341 vlib_node_increment_counter (vm, l2learn_node.index,
342 L2LEARN_ERROR_L2LEARN, 4);
343
344 l2fib_lookup_4 (msm->mac_table, &cached_key, &cached_result,
345 h0->src_address,
346 h1->src_address,
347 h2->src_address,
348 h3->src_address,
349 vnet_buffer (b[0])->l2.bd_index,
350 vnet_buffer (b[1])->l2.bd_index,
351 vnet_buffer (b[2])->l2.bd_index,
352 vnet_buffer (b[3])->l2.bd_index,
353 &key0, &key1, &key2, &key3,
Neale Rannsf0fa3172018-09-12 07:37:43 -0400354 &result0, &result1, &result2, &result3);
355
356 l2learn_process (node, msm, &em->counters[node_counter_base_index],
357 b[0], sw_if_index0, &key0, &cached_key,
358 &count, &result0, next, timestamp);
359
360 l2learn_process (node, msm, &em->counters[node_counter_base_index],
361 b[1], sw_if_index1, &key1, &cached_key,
362 &count, &result1, next + 1, timestamp);
363
364 l2learn_process (node, msm, &em->counters[node_counter_base_index],
365 b[2], sw_if_index2, &key2, &cached_key,
366 &count, &result2, next + 2, timestamp);
367
368 l2learn_process (node, msm, &em->counters[node_counter_base_index],
369 b[3], sw_if_index3, &key3, &cached_key,
370 &count, &result3, next + 3, timestamp);
371
372 next += 4;
373 b += 4;
374 n_left -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 }
376
Neale Rannsf0fa3172018-09-12 07:37:43 -0400377 while (n_left > 0)
378 {
379 u32 sw_if_index0;
380 ethernet_header_t *h0;
381 l2fib_entry_key_t key0;
382 l2fib_entry_result_t result0;
Neale Rannsf0fa3172018-09-12 07:37:43 -0400383
384 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
385
386 h0 = vlib_buffer_get_current (b[0]);
387
388 if (do_trace && PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
389 {
390 l2learn_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
391 t->sw_if_index = sw_if_index0;
392 t->bd_index = vnet_buffer (b[0])->l2.bd_index;
Dave Barach178cf492018-11-13 16:34:13 -0500393 clib_memcpy_fast (t->src, h0->src_address, 6);
394 clib_memcpy_fast (t->dst, h0->dst_address, 6);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400395 }
396
397 /* process 1 pkt */
398 vlib_node_increment_counter (vm, l2learn_node.index,
399 L2LEARN_ERROR_L2LEARN, 1);
400
401
402 l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result,
403 h0->src_address, vnet_buffer (b[0])->l2.bd_index,
Eyal Bari11d47af2018-10-31 10:55:33 +0200404 &key0, &result0);
Neale Rannsf0fa3172018-09-12 07:37:43 -0400405
406 l2learn_process (node, msm, &em->counters[node_counter_base_index],
407 b[0], sw_if_index0, &key0, &cached_key,
408 &count, &result0, next, timestamp);
409
410 next += 1;
411 b += 1;
412 n_left -= 1;
413 }
414
415 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 return frame->n_vectors;
418}
419
Neale Rannseb1525f2018-09-09 04:41:02 -0400420VLIB_NODE_FN (l2learn_node) (vlib_main_t * vm,
421 vlib_node_runtime_t * node, vlib_frame_t * frame)
Dave Barach681abe42017-02-15 09:01:01 -0500422{
423 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
424 return l2learn_node_inline (vm, node, frame, 1 /* do_trace */ );
425 return l2learn_node_inline (vm, node, frame, 0 /* do_trace */ );
426}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Dave Barach97d8dc22016-08-15 15:31:15 -0400428/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +0200429VLIB_REGISTER_NODE (l2learn_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 .name = "l2-learn",
431 .vector_size = sizeof (u32),
432 .format_trace = format_l2learn_trace,
433 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400434
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 .n_errors = ARRAY_LEN(l2learn_error_strings),
436 .error_strings = l2learn_error_strings,
437
438 .n_next_nodes = L2LEARN_N_NEXT,
439
440 /* edit / add dispositions here */
441 .next_nodes = {
442 [L2LEARN_NEXT_DROP] = "error-drop",
443 [L2LEARN_NEXT_L2FWD] = "l2-fwd",
444 },
445};
Dave Barach97d8dc22016-08-15 15:31:15 -0400446/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Neale Rannseb1525f2018-09-09 04:41:02 -0400448#ifndef CLIB_MARCH_VARIANT
449clib_error_t *
450l2learn_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451{
Dave Barach97d8dc22016-08-15 15:31:15 -0400452 l2learn_main_t *mp = &l2learn_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barach97d8dc22016-08-15 15:31:15 -0400454 mp->vlib_main = vm;
455 mp->vnet_main = vnet_get_main ();
456
457 /* Initialize the feature next-node indexes */
458 feat_bitmap_init_next_nodes (vm,
459 l2learn_node.index,
460 L2INPUT_N_FEAT,
461 l2input_get_feat_names (),
462 mp->feat_next_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
464 /* init the hash table ptr */
Dave Barach97d8dc22016-08-15 15:31:15 -0400465 mp->mac_table = get_mac_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
Dave Barach97d8dc22016-08-15 15:31:15 -0400467 /*
468 * Set the default number of dynamically learned macs to the number
469 * of buckets.
470 */
John Lo8d00fff2017-08-03 00:35:36 -0400471 mp->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
473 return 0;
474}
475
476VLIB_INIT_FUNCTION (l2learn_init);
477
478
Dave Barach97d8dc22016-08-15 15:31:15 -0400479/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400480 * Set subinterface learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400481 * The CLI format is:
482 * set interface l2 learn <interface> [disable]
483 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484static clib_error_t *
485int_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400486 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487{
Dave Barach97d8dc22016-08-15 15:31:15 -0400488 vnet_main_t *vnm = vnet_get_main ();
489 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 u32 sw_if_index;
491 u32 enable;
492
Dave Barach97d8dc22016-08-15 15:31:15 -0400493 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 {
495 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400496 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 goto done;
498 }
499
500 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400501 if (unformat (input, "disable"))
502 {
503 enable = 0;
504 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Dave Barach97d8dc22016-08-15 15:31:15 -0400506 /* set the interface flag */
507 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_LEARN, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barach97d8dc22016-08-15 15:31:15 -0400509done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 return error;
511}
512
Billy McFall22aa3e92016-09-09 08:46:40 -0400513/*?
514 * Layer 2 learning can be enabled and disabled on each
515 * interface and on each bridge-domain. Use this command to
516 * manage interfaces. It is enabled by default.
517 *
518 * @cliexpar
519 * Example of how to enable learning:
520 * @cliexcmd{set interface l2 learn GigabitEthernet0/8/0}
521 * Example of how to disable learning:
522 * @cliexcmd{set interface l2 learn GigabitEthernet0/8/0 disable}
523?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400524/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525VLIB_CLI_COMMAND (int_learn_cli, static) = {
526 .path = "set interface l2 learn",
527 .short_help = "set interface l2 learn <interface> [disable]",
528 .function = int_learn,
529};
Dave Barach97d8dc22016-08-15 15:31:15 -0400530/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
532
533static clib_error_t *
534l2learn_config (vlib_main_t * vm, unformat_input_t * input)
535{
536 l2learn_main_t *mp = &l2learn_main;
537
538 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
539 {
540 if (unformat (input, "limit %d", &mp->global_learn_limit))
Dave Barach97d8dc22016-08-15 15:31:15 -0400541 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
543 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400544 return clib_error_return (0, "unknown input `%U'",
545 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 }
547
548 return 0;
549}
550
551VLIB_CONFIG_FUNCTION (l2learn_config, "l2learn");
552
Neale Rannseb1525f2018-09-09 04:41:02 -0400553#endif
554
Dave Barach97d8dc22016-08-15 15:31:15 -0400555
556/*
557 * fd.io coding-style-patch-verification: ON
558 *
559 * Local Variables:
560 * eval: (c-set-style "gnu")
561 * End:
562 */