span: add tx functionality and support for multiple mirror ports
[vpp.git] / vnet / vnet / l2 / l2_input.c
1 /*
2  * l2_input.c : layer 2 input packet processing
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 <vnet/ethernet/packet.h>
23 #include <vnet/ip/ip_packet.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/ip6_packet.h>
26 #include <vlib/cli.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/l2_output.h>
29 #include <vnet/l2/feat_bitmap.h>
30 #include <vnet/l2/l2_bvi.h>
31 #include <vnet/l2/l2_fib.h>
32
33 #include <vppinfra/error.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/cache.h>
36
37 /**
38  * @file
39  * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3).
40  *
41  * This file contains the CLI Commands that modify the input mode of an
42  * interface. For interfaces in a Layer 2 cross-connect, all packets
43  * received on one interface will be transmitted to the other. For
44  * interfaces in a bridge-domain, packets will be forwarded to other
45  * interfaces in the same bridge-domain based on destination mac address.
46  * For interfaces in Layer 3 mode, the packets will be routed.
47  */
48
49 /* Feature graph node names */
50 static char *l2input_feat_names[] = {
51 #define _(sym,name) name,
52   foreach_l2input_feat
53 #undef _
54 };
55
56 char **
57 l2input_get_feat_names (void)
58 {
59   return l2input_feat_names;
60 }
61
62
63 typedef struct
64 {
65   /* per-pkt trace data */
66   u8 src[6];
67   u8 dst[6];
68   u32 next_index;
69   u32 sw_if_index;
70 } l2input_trace_t;
71
72 /* packet trace format function */
73 static u8 *
74 format_l2input_trace (u8 * s, va_list * args)
75 {
76   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
77   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
78   l2input_trace_t *t = va_arg (*args, l2input_trace_t *);
79
80   s = format (s, "l2-input: sw_if_index %d dst %U src %U",
81               t->sw_if_index,
82               format_ethernet_address, t->dst,
83               format_ethernet_address, t->src);
84   return s;
85 }
86
87 l2input_main_t l2input_main;
88
89 #define foreach_l2input_error                   \
90 _(L2INPUT,     "L2 input packets")              \
91 _(DROP,        "L2 input drops")
92
93 typedef enum
94 {
95 #define _(sym,str) L2INPUT_ERROR_##sym,
96   foreach_l2input_error
97 #undef _
98     L2INPUT_N_ERROR,
99 } l2input_error_t;
100
101 static char *l2input_error_strings[] = {
102 #define _(sym,string) string,
103   foreach_l2input_error
104 #undef _
105 };
106
107 typedef enum
108 {                               /*  */
109   L2INPUT_NEXT_LEARN,
110   L2INPUT_NEXT_FWD,
111   L2INPUT_NEXT_DROP,
112   L2INPUT_N_NEXT,
113 } l2input_next_t;
114
115
116 static_always_inline void
117 classify_and_dispatch (vlib_main_t * vm,
118                        vlib_node_runtime_t * node,
119                        u32 cpu_index,
120                        l2input_main_t * msm, vlib_buffer_t * b0, u32 * next0)
121 {
122   /*
123    * Load L2 input feature struct
124    * Load bridge domain struct
125    * Parse ethernet header to determine unicast/mcast/broadcast
126    * take L2 input stat
127    * classify packet as IP/UDP/TCP, control, other
128    * mask feature bitmap
129    * go to first node in bitmap
130    * Later: optimize VTM
131    *
132    * For L2XC,
133    *   set tx sw-if-handle
134    */
135
136   u8 mcast_dmac;
137   __attribute__ ((unused)) u8 l2bcast;
138   __attribute__ ((unused)) u8 l2mcast;
139   __attribute__ ((unused)) u8 l2_stat_kind;
140   u16 ethertype;
141   u8 protocol;
142   l2_input_config_t *config;
143   l2_bridge_domain_t *bd_config;
144   u16 bd_index0;
145   u32 feature_bitmap;
146   u32 feat_mask;
147   ethernet_header_t *h0;
148   u8 *l3h0;
149   u32 sw_if_index0;
150
151 #define get_u32(addr) ( *((u32 *)(addr)) )
152 #define get_u16(addr) ( *((u16 *)(addr)) )
153 #define STATS_IF_LAYER2_UCAST_INPUT_CNT 0
154 #define STATS_IF_LAYER2_MCAST_INPUT_CNT 1
155 #define STATS_IF_LAYER2_BCAST_INPUT_CNT 2
156
157   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
158
159   h0 = vlib_buffer_get_current (b0);
160   l3h0 = (u8 *) h0 + vnet_buffer (b0)->l2.l2_len;
161
162   ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
163   feat_mask = ~0;
164
165   /* determine layer2 kind for stat and mask */
166   mcast_dmac = ethernet_address_cast (h0->dst_address);
167   l2bcast = 0;
168   l2mcast = 0;
169   l2_stat_kind = STATS_IF_LAYER2_UCAST_INPUT_CNT;
170   if (PREDICT_FALSE (mcast_dmac))
171     {
172       u32 *dsthi = (u32 *) & h0->dst_address[0];
173       u32 *dstlo = (u32 *) & h0->dst_address[2];
174       protocol = ((ip6_header_t *) l3h0)->protocol;
175
176       /* Disable bridge forwarding (flooding will execute instead if not xconnect) */
177       feat_mask &= ~(L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD);
178
179       /* Disable ARP-term for non-ARP and non-ICMP6 packet */
180       if (ethertype != ETHERNET_TYPE_ARP &&
181           (ethertype != ETHERNET_TYPE_IP6 || protocol != IP_PROTOCOL_ICMP6))
182         feat_mask &= ~(L2INPUT_FEAT_ARP_TERM);
183
184       /* dest mac is multicast or broadcast */
185       if ((*dstlo == 0xFFFFFFFF) && (*dsthi == 0xFFFFFFFF))
186         {
187           /* dest mac == FF:FF:FF:FF:FF:FF */
188           l2_stat_kind = STATS_IF_LAYER2_BCAST_INPUT_CNT;
189           l2bcast = 1;
190         }
191       else
192         {
193           l2_stat_kind = STATS_IF_LAYER2_MCAST_INPUT_CNT;
194           l2mcast = 1;
195         }
196     }
197   /* TODO: take l2 stat */
198
199   /* Get config for the input interface */
200   config = vec_elt_at_index (msm->configs, sw_if_index0);
201
202   /* Save split horizon group */
203   vnet_buffer (b0)->l2.shg = config->shg;
204
205   if (config->xconnect)
206     {
207       /* Set the output interface */
208       vnet_buffer (b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index;
209
210     }
211   else
212     {
213       /*
214        * Check for from-BVI processing - set SHG of unicast packets from BVI
215        * to 0 so it is not dropped for VXLAN tunnels or other ports with the
216        * same SHG as that of the BVI.
217        */
218       if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] == L2INPUT_BVI
219                          && !mcast_dmac))
220         vnet_buffer (b0)->l2.shg = 0;
221
222       /* Do bridge-domain processing */
223       bd_index0 = config->bd_index;
224       /* save BD ID for next feature graph nodes */
225       vnet_buffer (b0)->l2.bd_index = bd_index0;
226
227       /* Get config for the bridge domain interface */
228       bd_config = vec_elt_at_index (msm->bd_configs, bd_index0);
229
230       /*
231        * Process bridge domain feature enables.
232        * To perform learning/flooding/forwarding, the corresponding bit
233        * must be enabled in both the input interface config and in the
234        * bridge domain config. In the bd_bitmap, bits for features other
235        * than learning/flooding/forwarding should always be set.
236        */
237       feat_mask = feat_mask & bd_config->feature_bitmap;
238     }
239
240   /* mask out features from bitmap using packet type and bd config */
241   feature_bitmap = config->feature_bitmap & feat_mask;
242
243   /* save for next feature graph nodes */
244   vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
245
246   /* Determine the next node */
247   *next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
248                                             feature_bitmap);
249 }
250
251
252 static uword
253 l2input_node_fn (vlib_main_t * vm,
254                  vlib_node_runtime_t * node, vlib_frame_t * frame)
255 {
256   u32 n_left_from, *from, *to_next;
257   l2input_next_t next_index;
258   l2input_main_t *msm = &l2input_main;
259   u32 cpu_index = os_get_cpu_number ();
260
261   from = vlib_frame_vector_args (frame);
262   n_left_from = frame->n_vectors;       /* number of packets to process */
263   next_index = node->cached_next_index;
264
265   while (n_left_from > 0)
266     {
267       u32 n_left_to_next;
268
269       /* get space to enqueue frame to graph node "next_index" */
270       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
271
272       while (n_left_from >= 8 && n_left_to_next >= 4)
273         {
274           u32 bi0, bi1, bi2, bi3;
275           vlib_buffer_t *b0, *b1, *b2, *b3;
276           u32 next0, next1, next2, next3;
277           u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
278
279           /* Prefetch next iteration. */
280           {
281             vlib_buffer_t *p4, *p5, *p6, *p7;
282
283             p4 = vlib_get_buffer (vm, from[4]);
284             p5 = vlib_get_buffer (vm, from[5]);
285             p6 = vlib_get_buffer (vm, from[6]);
286             p7 = vlib_get_buffer (vm, from[7]);
287
288             /* Prefetch the buffer header and packet for the N+2 loop iteration */
289             vlib_prefetch_buffer_header (p4, LOAD);
290             vlib_prefetch_buffer_header (p5, LOAD);
291             vlib_prefetch_buffer_header (p6, LOAD);
292             vlib_prefetch_buffer_header (p7, LOAD);
293
294             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
295             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
296             CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
297             CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
298
299             /*
300              * Don't bother prefetching the bridge-domain config (which
301              * depends on the input config above). Only a small number of
302              * bridge domains are expected. Plus the structure is small
303              * and several fit in a cache line.
304              */
305           }
306
307           /* speculatively enqueue b0 and b1 to the current next frame */
308           /* bi is "buffer index", b is pointer to the buffer */
309           to_next[0] = bi0 = from[0];
310           to_next[1] = bi1 = from[1];
311           to_next[2] = bi2 = from[2];
312           to_next[3] = bi3 = from[3];
313           from += 4;
314           to_next += 4;
315           n_left_from -= 4;
316           n_left_to_next -= 4;
317
318           b0 = vlib_get_buffer (vm, bi0);
319           b1 = vlib_get_buffer (vm, bi1);
320           b2 = vlib_get_buffer (vm, bi2);
321           b3 = vlib_get_buffer (vm, bi3);
322
323           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
324             {
325               /* RX interface handles */
326               sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
327               sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
328               sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX];
329               sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX];
330
331               if (b0->flags & VLIB_BUFFER_IS_TRACED)
332                 {
333                   ethernet_header_t *h0 = vlib_buffer_get_current (b0);
334                   l2input_trace_t *t =
335                     vlib_add_trace (vm, node, b0, sizeof (*t));
336                   t->sw_if_index = sw_if_index0;
337                   clib_memcpy (t->src, h0->src_address, 6);
338                   clib_memcpy (t->dst, h0->dst_address, 6);
339                 }
340               if (b1->flags & VLIB_BUFFER_IS_TRACED)
341                 {
342                   ethernet_header_t *h1 = vlib_buffer_get_current (b1);
343                   l2input_trace_t *t =
344                     vlib_add_trace (vm, node, b1, sizeof (*t));
345                   t->sw_if_index = sw_if_index1;
346                   clib_memcpy (t->src, h1->src_address, 6);
347                   clib_memcpy (t->dst, h1->dst_address, 6);
348                 }
349               if (b2->flags & VLIB_BUFFER_IS_TRACED)
350                 {
351                   ethernet_header_t *h2 = vlib_buffer_get_current (b2);
352                   l2input_trace_t *t =
353                     vlib_add_trace (vm, node, b2, sizeof (*t));
354                   t->sw_if_index = sw_if_index2;
355                   clib_memcpy (t->src, h2->src_address, 6);
356                   clib_memcpy (t->dst, h2->dst_address, 6);
357                 }
358               if (b3->flags & VLIB_BUFFER_IS_TRACED)
359                 {
360                   ethernet_header_t *h3 = vlib_buffer_get_current (b3);
361                   l2input_trace_t *t =
362                     vlib_add_trace (vm, node, b3, sizeof (*t));
363                   t->sw_if_index = sw_if_index3;
364                   clib_memcpy (t->src, h3->src_address, 6);
365                   clib_memcpy (t->dst, h3->dst_address, 6);
366                 }
367             }
368
369           vlib_node_increment_counter (vm, l2input_node.index,
370                                        L2INPUT_ERROR_L2INPUT, 4);
371
372           classify_and_dispatch (vm, node, cpu_index, msm, b0, &next0);
373           classify_and_dispatch (vm, node, cpu_index, msm, b1, &next1);
374           classify_and_dispatch (vm, node, cpu_index, msm, b2, &next2);
375           classify_and_dispatch (vm, node, cpu_index, msm, b3, &next3);
376
377           /* verify speculative enqueues, maybe switch current next frame */
378           /* if next0==next1==next_index then nothing special needs to be done */
379           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
380                                            to_next, n_left_to_next,
381                                            bi0, bi1, bi2, bi3,
382                                            next0, next1, next2, next3);
383         }
384
385       while (n_left_from > 0 && n_left_to_next > 0)
386         {
387           u32 bi0;
388           vlib_buffer_t *b0;
389           u32 next0;
390           u32 sw_if_index0;
391
392           /* speculatively enqueue b0 to the current next frame */
393           bi0 = from[0];
394           to_next[0] = bi0;
395           from += 1;
396           to_next += 1;
397           n_left_from -= 1;
398           n_left_to_next -= 1;
399
400           b0 = vlib_get_buffer (vm, bi0);
401
402           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
403                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
404             {
405               ethernet_header_t *h0 = vlib_buffer_get_current (b0);
406               l2input_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
407               sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
408               t->sw_if_index = sw_if_index0;
409               clib_memcpy (t->src, h0->src_address, 6);
410               clib_memcpy (t->dst, h0->dst_address, 6);
411             }
412
413           vlib_node_increment_counter (vm, l2input_node.index,
414                                        L2INPUT_ERROR_L2INPUT, 1);
415
416           classify_and_dispatch (vm, node, cpu_index, msm, b0, &next0);
417
418           /* verify speculative enqueue, maybe switch current next frame */
419           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
420                                            to_next, n_left_to_next,
421                                            bi0, next0);
422         }
423
424       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
425     }
426
427   return frame->n_vectors;
428 }
429
430
431 /* *INDENT-OFF* */
432 VLIB_REGISTER_NODE (l2input_node) = {
433   .function = l2input_node_fn,
434   .name = "l2-input",
435   .vector_size = sizeof (u32),
436   .format_trace = format_l2input_trace,
437   .format_buffer = format_ethernet_header_with_length,
438   .type = VLIB_NODE_TYPE_INTERNAL,
439
440   .n_errors = ARRAY_LEN(l2input_error_strings),
441   .error_strings = l2input_error_strings,
442
443   .n_next_nodes = L2INPUT_N_NEXT,
444
445   /* edit / add dispositions here */
446   .next_nodes = {
447        [L2INPUT_NEXT_LEARN] = "l2-learn",
448        [L2INPUT_NEXT_FWD]   = "l2-fwd",
449        [L2INPUT_NEXT_DROP]  = "error-drop",
450   },
451 };
452 /* *INDENT-ON* */
453
454 VLIB_NODE_FUNCTION_MULTIARCH (l2input_node, l2input_node_fn)
455      clib_error_t *l2input_init (vlib_main_t * vm)
456 {
457   l2input_main_t *mp = &l2input_main;
458
459   mp->vlib_main = vm;
460   mp->vnet_main = vnet_get_main ();
461
462   /* Get packets RX'd from L2 interfaces */
463   ethernet_register_l2_input (vm, l2input_node.index);
464
465   /* Create the config vector */
466   vec_validate (mp->configs, 100);
467   /* create 100 sw interface entries and zero them */
468
469   /* Initialize the feature next-node indexes */
470   feat_bitmap_init_next_nodes (vm,
471                                l2input_node.index,
472                                L2INPUT_N_FEAT,
473                                l2input_get_feat_names (),
474                                mp->feat_next_node_index);
475
476   return 0;
477 }
478
479 VLIB_INIT_FUNCTION (l2input_init);
480
481
482 /** Get a pointer to the config for the given interface. */
483 l2_input_config_t *
484 l2input_intf_config (u32 sw_if_index)
485 {
486   l2input_main_t *mp = &l2input_main;
487
488   vec_validate (mp->configs, sw_if_index);
489   return vec_elt_at_index (mp->configs, sw_if_index);
490 }
491
492 /** Enable (or disable) the feature in the bitmap for the given interface. */
493 u32
494 l2input_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
495 {
496   l2input_main_t *mp = &l2input_main;
497   l2_input_config_t *config;
498
499   vec_validate (mp->configs, sw_if_index);
500   config = vec_elt_at_index (mp->configs, sw_if_index);
501
502   if (enable)
503     {
504       config->feature_bitmap |= feature_bitmap;
505     }
506   else
507     {
508       config->feature_bitmap &= ~feature_bitmap;
509     }
510
511   return config->feature_bitmap;
512 }
513
514 u32
515 l2input_set_bridge_features (u32 bd_index, u32 feat_mask, u32 feat_value)
516 {
517   l2_bridge_domain_t *bd_config;
518   vec_validate (l2input_main.bd_configs, bd_index);
519   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
520   bd_validate (bd_config);
521   bd_config->feature_bitmap =
522     (bd_config->feature_bitmap & ~feat_mask) | feat_value;
523   return bd_config->feature_bitmap;
524 }
525
526 /**
527  * Set the subinterface to run in l2 or l3 mode.
528  * For L3 mode, just the sw_if_index is specified.
529  * For bridged mode, the bd id and bvi flag are also specified.
530  * For xconnect mode, the peer sw_if_index is also specified.
531  * Return 0 if ok, or non-0 if there was an error.
532  */
533
534 u32
535 set_int_l2_mode (vlib_main_t * vm, vnet_main_t * vnet_main, u32 mode, u32 sw_if_index, u32 bd_index,    /* for bridged interface */
536                  u32 bvi,       /* the bridged interface is the BVI */
537                  u32 shg,       /* the bridged interface's split horizon group */
538                  u32 xc_sw_if_index)    /* peer interface for xconnect */
539 {
540   l2input_main_t *mp = &l2input_main;
541   l2output_main_t *l2om = &l2output_main;
542   vnet_main_t *vnm = vnet_get_main ();
543   vnet_hw_interface_t *hi;
544   l2_output_config_t *out_config;
545   l2_input_config_t *config;
546   l2_bridge_domain_t *bd_config;
547   l2_flood_member_t member;
548   u64 mac;
549   i32 l2_if_adjust = 0;
550   u32 slot;
551
552   hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
553   config = l2input_intf_config (sw_if_index);
554
555   if (config->bridge)
556     {
557       /* Interface is already in bridge mode. Undo the existing config. */
558       bd_config = vec_elt_at_index (mp->bd_configs, config->bd_index);
559
560       /* remove interface from flood vector */
561       bd_remove_member (bd_config, sw_if_index);
562
563       /* undo any BVI-related config */
564       if (bd_config->bvi_sw_if_index == sw_if_index)
565         {
566           bd_config->bvi_sw_if_index = ~0;
567           config->bvi = 0;
568
569           /* delete the l2fib entry for the bvi interface */
570           mac = *((u64 *) hi->hw_address);
571           l2fib_del_entry (mac, config->bd_index);
572
573           /* Make loop output node send packet back to ethernet-input node */
574           slot =
575             vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
576                                                 "ethernet-input",
577                                                 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
578           ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
579         }
580       l2_if_adjust--;
581     }
582   else if (config->xconnect)
583     {
584       l2_if_adjust--;
585     }
586
587   /*
588    * Directs the l2 output path to work out the interface
589    * output next-arc itself. Needed when recycling a sw_if_index.
590    */
591   vec_validate_init_empty (l2om->next_nodes.output_node_index_vec,
592                            sw_if_index, ~0);
593   l2om->next_nodes.output_node_index_vec[sw_if_index] = ~0;
594
595   /* Initialize the l2-input configuration for the interface */
596   if (mode == MODE_L3)
597     {
598       /* Set L2 config to BD index 0 so that if any packet accidentally
599        * came in on L2 path, it will be dropped in BD 0 */
600       config->xconnect = 0;
601       config->bridge = 0;
602       config->shg = 0;
603       config->bd_index = 0;
604       config->feature_bitmap = L2INPUT_FEAT_DROP;
605
606       /* Make sure any L2-output packet to this interface now in L3 mode is
607        * dropped. This may happen if L2 FIB MAC entry is stale */
608       l2om->next_nodes.output_node_index_vec[sw_if_index] =
609         L2OUTPUT_NEXT_BAD_INTF;
610     }
611   else if (mode == MODE_L2_CLASSIFY)
612     {
613       config->xconnect = 1;
614       config->bridge = 0;
615       config->output_sw_if_index = xc_sw_if_index;
616
617       /* Make sure last-chance drop is configured */
618       config->feature_bitmap |=
619         L2INPUT_FEAT_DROP | L2INPUT_FEAT_INPUT_CLASSIFY;
620
621       /* Make sure bridging features are disabled */
622       config->feature_bitmap &=
623         ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
624       shg = 0;                  /* not used in xconnect */
625
626       /* Insure all packets go to ethernet-input */
627       ethernet_set_rx_redirect (vnet_main, hi, 1);
628     }
629   else
630     {
631
632       if (mode == MODE_L2_BRIDGE)
633         {
634           /*
635            * Remove a check that the interface must be an Ethernet.
636            * Specifically so we can bridge to L3 tunnel interfaces.
637            * Here's the check:
638            * if (hi->hw_class_index != ethernet_hw_interface_class.index)
639            *
640            */
641           if (!hi)
642             return MODE_ERROR_ETH;      /* non-ethernet */
643
644           config->xconnect = 0;
645           config->bridge = 1;
646           config->bd_index = bd_index;
647
648           /*
649            * Enable forwarding, flooding, learning and ARP termination by default
650            * (note that ARP term is disabled on BD feature bitmap by default)
651            */
652           config->feature_bitmap |= L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD |
653             L2INPUT_FEAT_FLOOD | L2INPUT_FEAT_LEARN | L2INPUT_FEAT_ARP_TERM;
654
655           /* Make sure last-chance drop is configured */
656           config->feature_bitmap |= L2INPUT_FEAT_DROP;
657
658           /* Make sure xconnect is disabled */
659           config->feature_bitmap &= ~L2INPUT_FEAT_XCONNECT;
660
661           /* Set up bridge domain */
662           vec_validate (mp->bd_configs, bd_index);
663           bd_config = vec_elt_at_index (mp->bd_configs, bd_index);
664           bd_validate (bd_config);
665
666           /* TODO: think: add l2fib entry even for non-bvi interface? */
667
668           /* Do BVI interface initializations */
669           if (bvi)
670             {
671               /* ensure BD has no bvi interface (or replace that one with this??) */
672               if (bd_config->bvi_sw_if_index != ~0)
673                 {
674                   return MODE_ERROR_BVI_DEF;    /* bd already has a bvi interface */
675                 }
676               bd_config->bvi_sw_if_index = sw_if_index;
677               config->bvi = 1;
678
679               /* create the l2fib entry for the bvi interface */
680               mac = *((u64 *) hi->hw_address);
681               l2fib_add_entry (mac, bd_index, sw_if_index, 1, 0, 1);    /* static + bvi */
682
683               /* Disable learning by default. no use since l2fib entry is static. */
684               config->feature_bitmap &= ~L2INPUT_FEAT_LEARN;
685
686               /* Make loop output node send packet to l2-input node */
687               slot =
688                 vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
689                                                     "l2-input",
690                                                     VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
691               ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
692             }
693
694           /* Add interface to bridge-domain flood vector */
695           member.sw_if_index = sw_if_index;
696           member.flags = bvi ? L2_FLOOD_MEMBER_BVI : L2_FLOOD_MEMBER_NORMAL;
697           member.shg = shg;
698           bd_add_member (bd_config, &member);
699
700         }
701       else
702         {
703           config->xconnect = 1;
704           config->bridge = 0;
705           config->output_sw_if_index = xc_sw_if_index;
706
707           /* Make sure last-chance drop is configured */
708           config->feature_bitmap |= L2INPUT_FEAT_DROP;
709
710           /* Make sure bridging features are disabled */
711           config->feature_bitmap &=
712             ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
713
714           config->feature_bitmap |= L2INPUT_FEAT_XCONNECT;
715           shg = 0;              /* not used in xconnect */
716         }
717
718       /* set up split-horizon group */
719       config->shg = shg;
720       out_config = l2output_intf_config (sw_if_index);
721       out_config->shg = shg;
722
723       /*
724        * Test: remove this when non-IP features can be configured.
725        * Enable a non-IP feature to test IP feature masking
726        * config->feature_bitmap |= L2INPUT_FEAT_CTRL_PKT;
727        */
728
729       l2_if_adjust++;
730     }
731
732   /* Adjust count of L2 interfaces */
733   hi->l2_if_count += l2_if_adjust;
734
735   if (hi->hw_class_index == ethernet_hw_interface_class.index)
736     {
737       if ((hi->l2_if_count == 1) && (l2_if_adjust == 1))
738         {
739           /* Just added first L2 interface on this port */
740
741           /* Set promiscuous mode on the l2 interface */
742           ethernet_set_flags (vnet_main, hi->hw_if_index,
743                               ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
744
745           /* ensure all packets go to ethernet-input */
746           ethernet_set_rx_redirect (vnet_main, hi, 1);
747
748         }
749       else if ((hi->l2_if_count == 0) && (l2_if_adjust == -1))
750         {
751           /* Just removed only L2 subinterface on this port */
752
753           /* Disable promiscuous mode on the l2 interface */
754           ethernet_set_flags (vnet_main, hi->hw_if_index, 0);
755
756           /* Allow ip packets to go directly to ip4-input etc */
757           ethernet_set_rx_redirect (vnet_main, hi, 0);
758         }
759     }
760
761   /* Set up the L2/L3 flag in the interface parsing tables */
762   ethernet_sw_interface_set_l2_mode (vnm, sw_if_index, (mode != MODE_L3));
763
764   return 0;
765 }
766
767 /**
768  * Set subinterface in bridging mode with a bridge-domain ID.
769  * The CLI format is:
770  *   set interface l2 bridge <interface> <bd> [bvi] [split-horizon-group]
771  */
772 static clib_error_t *
773 int_l2_bridge (vlib_main_t * vm,
774                unformat_input_t * input, vlib_cli_command_t * cmd)
775 {
776   vnet_main_t *vnm = vnet_get_main ();
777   clib_error_t *error = 0;
778   u32 bd_index, bd_id;
779   u32 sw_if_index;
780   u32 bvi;
781   u32 rc;
782   u32 shg;
783
784   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
785     {
786       error = clib_error_return (0, "unknown interface `%U'",
787                                  format_unformat_error, input);
788       goto done;
789     }
790
791   if (!unformat (input, "%d", &bd_id))
792     {
793       error = clib_error_return (0, "expected bridge domain ID `%U'",
794                                  format_unformat_error, input);
795       goto done;
796     }
797
798   bd_index = bd_find_or_add_bd_index (&bd_main, bd_id);
799
800   /* optional bvi  */
801   bvi = unformat (input, "bvi");
802
803   /* optional split horizon group */
804   shg = 0;
805   (void) unformat (input, "%d", &shg);
806
807   /* set the interface mode */
808   if ((rc =
809        set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE, sw_if_index, bd_index, bvi,
810                         shg, 0)))
811     {
812       if (rc == MODE_ERROR_ETH)
813         {
814           error = clib_error_return (0, "bridged interface must be ethernet",
815                                      format_unformat_error, input);
816         }
817       else if (rc == MODE_ERROR_BVI_DEF)
818         {
819           error =
820             clib_error_return (0, "bridge-domain already has a bvi interface",
821                                format_unformat_error, input);
822         }
823       else
824         {
825           error = clib_error_return (0, "invalid configuration for interface",
826                                      format_unformat_error, input);
827         }
828       goto done;
829     }
830
831 done:
832   return error;
833 }
834
835 /*?
836  * Use this command put an interface into Layer 2 bridge domain. If a
837  * bridge-domain with the provided bridge-domain-id does not exist, it
838  * will be created. Interfaces in a bridge-domain forward packets to
839  * other interfaces in the same bridge-domain based on destination mac
840  * address. To remove an interface from a the Layer 2 bridge domain,
841  * put the interface in a different mode, for example Layer 3 mode.
842  *
843  * Optionally, an interface can be added to a Layer 2 bridge-domain as
844  * a Bridged Virtual Interface (bvi). Only one interface in a Layer 2
845  * bridge-domain can be a bvi.
846  *
847  * Optionally, a split-horizon group can also be specified. This defaults
848  * to 0 if not specified.
849  *
850  * @cliexpar
851  * Example of how to configure a Layer 2 bridge-domain with three
852  * interfaces (where 200 is the bridge-domain-id):
853  * @cliexcmd{set interface l2 bridge GigabitEthernet0/8/0.200 200}
854  * This interface is added a BVI interface:
855  * @cliexcmd{set interface l2 bridge GigabitEthernet0/9/0.200 200 bvi}
856  * This interface also has a split-horizon group of 1 specified:
857  * @cliexcmd{set interface l2 bridge GigabitEthernet0/a/0.200 200 1}
858  * Example of how to remove an interface from a Layer2 bridge-domain:
859  * @cliexcmd{set interface l3 GigabitEthernet0/a/0.200}
860 ?*/
861 /* *INDENT-OFF* */
862 VLIB_CLI_COMMAND (int_l2_bridge_cli, static) = {
863   .path = "set interface l2 bridge",
864   .short_help = "set interface l2 bridge <interface> <bridge-domain-id> [bvi] [shg]",
865   .function = int_l2_bridge,
866 };
867 /* *INDENT-ON* */
868
869 /**
870  * Set subinterface in xconnect mode with another interface.
871  * The CLI format is:
872  *   set interface l2 xconnect <interface> <peer interface>
873  */
874 static clib_error_t *
875 int_l2_xc (vlib_main_t * vm,
876            unformat_input_t * input, vlib_cli_command_t * cmd)
877 {
878   vnet_main_t *vnm = vnet_get_main ();
879   clib_error_t *error = 0;
880   u32 sw_if_index;
881   u32 xc_sw_if_index;
882
883   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
884     {
885       error = clib_error_return (0, "unknown interface `%U'",
886                                  format_unformat_error, input);
887       goto done;
888     }
889
890   if (!unformat_user
891       (input, unformat_vnet_sw_interface, vnm, &xc_sw_if_index))
892     {
893       error = clib_error_return (0, "unknown peer interface `%U'",
894                                  format_unformat_error, input);
895       goto done;
896     }
897
898   /* set the interface mode */
899   if (set_int_l2_mode
900       (vm, vnm, MODE_L2_XC, sw_if_index, 0, 0, 0, xc_sw_if_index))
901     {
902       error = clib_error_return (0, "invalid configuration for interface",
903                                  format_unformat_error, input);
904       goto done;
905     }
906
907 done:
908   return error;
909 }
910
911 /*?
912  * Use this command put an interface into Layer 2 cross-connect mode.
913  * Both interfaces must be in this mode for bi-directioal traffic. All
914  * packets received on one interface will be transmitted to the other.
915  * To remove the Layer 2 cross-connect, put the interface in a different
916  * mode, for example Layer 3 mode.
917  *
918  * @cliexpar
919  * Example of how to configure a Layer2 cross-connect between two interfaces:
920  * @cliexcmd{set interface l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300}
921  * @cliexcmd{set interface l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300}
922  * Example of how to remove a Layer2 cross-connect:
923  * @cliexcmd{set interface l3 GigabitEthernet0/8/0.300}
924  * @cliexcmd{set interface l3 GigabitEthernet0/9/0.300}
925 ?*/
926 /* *INDENT-OFF* */
927 VLIB_CLI_COMMAND (int_l2_xc_cli, static) = {
928   .path = "set interface l2 xconnect",
929   .short_help = "set interface l2 xconnect <interface> <peer interface>",
930   .function = int_l2_xc,
931 };
932 /* *INDENT-ON* */
933
934 /**
935  * Set subinterface in L3 mode.
936  * The CLI format is:
937  *   set interface l3 <interface>
938  */
939 static clib_error_t *
940 int_l3 (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
941 {
942   vnet_main_t *vnm = vnet_get_main ();
943   clib_error_t *error = 0;
944   u32 sw_if_index;
945
946   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
947     {
948       error = clib_error_return (0, "unknown interface `%U'",
949                                  format_unformat_error, input);
950       goto done;
951     }
952
953   /* set the interface mode */
954   if (set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0))
955     {
956       error = clib_error_return (0, "invalid configuration for interface",
957                                  format_unformat_error, input);
958       goto done;
959     }
960
961 done:
962   return error;
963 }
964
965 /*?
966  * Modify the packet processing mode of the interface to Layer 3, which
967  * implies packets will be routed. This is the default mode of an interface.
968  * Use this command to remove an interface from a Layer 2 cross-connect or a
969  * Layer 2 bridge.
970  *
971  * @cliexpar
972  * Example of how to set the mode of an interface to Layer 3:
973  * @cliexcmd{set interface l3 GigabitEthernet0/8/0.200}
974 ?*/
975 /* *INDENT-OFF* */
976      VLIB_CLI_COMMAND (int_l3_cli, static) = {
977   .path = "set interface l3",
978   .short_help = "set interface l3 <interface>",
979   .function = int_l3,
980 };
981 /* *INDENT-ON* */
982
983 /**
984  * Show interface mode.
985  * The CLI format is:
986  *    show mode [<if-name1> <if-name2> ...]
987  */
988 static clib_error_t *
989 show_int_mode (vlib_main_t * vm,
990                unformat_input_t * input, vlib_cli_command_t * cmd)
991 {
992   vnet_main_t *vnm = vnet_get_main ();
993   clib_error_t *error = 0;
994   char *mode;
995   u8 *args;
996   vnet_interface_main_t *im = &vnm->interface_main;
997   vnet_sw_interface_t *si, *sis = 0;
998   l2input_main_t *mp = &l2input_main;
999   l2_input_config_t *config;
1000
1001   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1002     {
1003       u32 sw_if_index;
1004
1005       /* See if user wants to show specific interface */
1006       if (unformat
1007           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1008         {
1009           si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1010           vec_add1 (sis, si[0]);
1011         }
1012       else
1013         {
1014           error = clib_error_return (0, "unknown input `%U'",
1015                                      format_unformat_error, input);
1016           goto done;
1017         }
1018
1019     }
1020
1021   if (vec_len (sis) == 0)       /* Get all interfaces */
1022     {
1023       /* Gather interfaces. */
1024       sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1025       _vec_len (sis) = 0;
1026       /* *INDENT-OFF* */
1027       pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sis, si[0]); }));
1028       /* *INDENT-ON* */
1029     }
1030
1031   vec_foreach (si, sis)
1032   {
1033     vec_validate (mp->configs, si->sw_if_index);
1034     config = vec_elt_at_index (mp->configs, si->sw_if_index);
1035     if (config->bridge)
1036       {
1037         u32 bd_id;
1038         mode = "l2 bridge";
1039         bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
1040
1041         args = format (0, "bd_id %d%s%d", bd_id,
1042                        config->bvi ? " bvi shg " : " shg ", config->shg);
1043       }
1044     else if (config->xconnect)
1045       {
1046         mode = "l2 xconnect";
1047         args = format (0, "%U",
1048                        format_vnet_sw_if_index_name,
1049                        vnm, config->output_sw_if_index);
1050       }
1051     else
1052       {
1053         mode = "l3";
1054         args = format (0, " ");
1055       }
1056     vlib_cli_output (vm, "%s %U %v\n",
1057                      mode,
1058                      format_vnet_sw_if_index_name,
1059                      vnm, si->sw_if_index, args);
1060     vec_free (args);
1061   }
1062
1063 done:
1064   vec_free (sis);
1065
1066   return error;
1067 }
1068
1069 /*?
1070  * Show the packet processing mode (Layer2 xcross-onnect, Layer 2 bridge,
1071  * Layer 3 routed) of all interfaces and sub-interfaces, or limit the
1072  * output to just the provided list of interfaces and sub-interfaces.
1073  * The output shows the mode, the interface, and if the interface is
1074  * a member of a bridge, the bridge-domain-id and the split horizen group (shg).
1075  *
1076  * @cliexpar
1077  * Example of displaying the mode of all interfaces:
1078  * @cliexstart{show mode}
1079  * l3 local0
1080  * l3 GigabitEthernet0/8/0
1081  * l3 GigabitEthernet0/9/0
1082  * l3 GigabitEthernet0/a/0
1083  * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1084  * l2 bridge GigabitEthernet0/9/0.200 bd_id 200 shg 0
1085  * l2 bridge GigabitEthernet0/a/0.200 bd_id 200 shg 0
1086  * l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300
1087  * l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300
1088  * @cliexend
1089  * Example of displaying the mode of a seleted list of interfaces:
1090  * @cliexstart{show mode GigabitEthernet0/8/0 GigabitEthernet0/8/0.200}
1091  * l3 GigabitEthernet0/8/0
1092  * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1093  * @cliexend
1094 ?*/
1095 /* *INDENT-OFF* */
1096 VLIB_CLI_COMMAND (show_l2_mode, static) = {
1097   .path = "show mode",
1098   .short_help = "show mode [<if-name1> <if-name2> ...]",
1099   .function = show_int_mode,
1100 };
1101 /* *INDENT-ON* */
1102
1103 #define foreach_l2_init_function                \
1104 _(feat_bitmap_drop_init)                        \
1105 _(l2fib_init)                                   \
1106 _(l2_input_classify_init)                             \
1107 _(l2bd_init)                                    \
1108 _(l2fwd_init)                                   \
1109 _(l2_inacl_init)                                \
1110 _(l2input_init)                                 \
1111 _(l2_vtr_init)                                  \
1112 _(l2_invtr_init)                                \
1113 _(l2_efp_filter_init)                           \
1114 _(l2learn_init)                                 \
1115 _(l2flood_init)                                 \
1116 _(l2_outacl_init)                               \
1117 _(l2output_init)                                \
1118 _(l2_patch_init)                                \
1119 _(l2_xcrw_init)
1120
1121 clib_error_t *
1122 l2_init (vlib_main_t * vm)
1123 {
1124   clib_error_t *error;
1125
1126 #define _(a) do {                                                       \
1127   if ((error = vlib_call_init_function (vm, a))) return error; }        \
1128 while (0);
1129   foreach_l2_init_function;
1130 #undef _
1131   return 0;
1132 }
1133
1134 VLIB_INIT_FUNCTION (l2_init);
1135
1136 /*
1137  * fd.io coding-style-patch-verification: ON
1138  *
1139  * Local Variables:
1140  * eval: (c-set-style "gnu")
1141  * End:
1142  */