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