31744f5fc31855a386253afde9bd0e32e44da687
[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 extern clib_error_t *
38 ethernet_arp_hw_interface_link_up_down (vnet_main_t * vnm,
39                                         u32 hw_if_index,
40                                         u32 flags);
41
42 // Feature graph node names
43 static char * l2input_feat_names[] = {
44 #define _(sym,name) name,
45   foreach_l2input_feat
46 #undef _
47 };
48
49 char **l2input_get_feat_names(void) {
50   return l2input_feat_names;
51 }
52
53
54 typedef struct {
55   /* per-pkt trace data */ 
56   u8 src[6];
57   u8 dst[6];
58   u32 next_index;
59   u32 sw_if_index;
60 } l2input_trace_t;
61
62 /* packet trace format function */
63 static u8 * format_l2input_trace (u8 * s, va_list * args)
64 {
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 *);
67   l2input_trace_t * t = va_arg (*args, l2input_trace_t *);
68   
69   s = format (s, "l2-input: sw_if_index %d dst %U src %U",
70               t->sw_if_index,
71               format_ethernet_address, t->dst,
72               format_ethernet_address, t->src);
73   return s;
74 }
75
76 l2input_main_t l2input_main;
77
78 static vlib_node_registration_t l2input_node;
79
80 #define foreach_l2input_error                   \
81 _(L2INPUT,     "L2 input packets")              \
82 _(DROP,        "L2 input drops")
83
84 typedef enum {
85 #define _(sym,str) L2INPUT_ERROR_##sym,
86   foreach_l2input_error
87 #undef _
88   L2INPUT_N_ERROR,
89 } l2input_error_t;
90
91 static char * l2input_error_strings[] = {
92 #define _(sym,string) string,
93   foreach_l2input_error
94 #undef _
95 };
96
97 typedef enum {                  /*  */
98   L2INPUT_NEXT_LEARN,
99   L2INPUT_NEXT_FWD,
100   L2INPUT_NEXT_DROP,
101   L2INPUT_N_NEXT,
102 } l2input_next_t;
103
104
105 static_always_inline void
106 classify_and_dispatch (vlib_main_t * vm,
107                        vlib_node_runtime_t * node,
108                        u32 cpu_index, 
109                        l2input_main_t * msm,
110                        vlib_buffer_t * b0,
111                        u32 *next0)
112 {
113   // Load L2 input feature struct
114   // Load bridge domain struct
115   // Parse ethernet header to determine unicast/mcast/broadcast
116   // take L2 input stat
117   // classify packet as IP/UDP/TCP, control, other
118   // mask feature bitmap
119   // go to first node in bitmap
120   // Later: optimize VTM
121   //
122   // For L2XC, 
123   //   set tx sw-if-handle
124  
125   u8 mcast_dmac; 
126   __attribute__((unused)) u8 l2bcast;
127   __attribute__((unused)) u8 l2mcast;
128   __attribute__((unused)) u8 l2_stat_kind;
129   u16 ethertype;
130   u8 protocol;
131   l2_input_config_t *config;
132   l2_bridge_domain_t *bd_config;
133   u16 bd_index0;
134   u32 feature_bitmap;
135   u32 feat_mask;
136   ethernet_header_t * h0;
137   u8 * l3h0;
138   u32 sw_if_index0;
139   u8 bvi_flg = 0;
140
141 #define get_u32(addr) ( *((u32 *)(addr)) )
142 #define get_u16(addr) ( *((u16 *)(addr)) )
143 #define STATS_IF_LAYER2_UCAST_INPUT_CNT 0
144 #define STATS_IF_LAYER2_MCAST_INPUT_CNT 1
145 #define STATS_IF_LAYER2_BCAST_INPUT_CNT 2
146
147   // Check for from-BVI processing
148   // When we come from ethernet-input, TX is ~0
149   if (PREDICT_FALSE (vnet_buffer(b0)->sw_if_index[VLIB_TX] != ~0)) {
150     // Set up for a from-bvi packet
151     bvi_to_l2 (vm, 
152                msm->vnet_main, 
153                cpu_index, 
154                b0, 
155                vnet_buffer(b0)->sw_if_index[VLIB_TX]);
156     bvi_flg = 1;
157   }
158
159   // The RX interface can be changed by bvi_to_l2()
160   sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
161
162   h0 = vlib_buffer_get_current (b0);
163   l3h0 = (u8 *)h0 + vnet_buffer(b0)->l2.l2_len;
164
165   // Determine L3 packet type. Only need to check the common types.
166   // Used to filter out features that don't apply to common packets.
167   ethertype = clib_net_to_host_u16(get_u16(l3h0 - 2));
168   if (ethertype == ETHERNET_TYPE_IP4) {
169     protocol = ((ip4_header_t *)l3h0)->protocol;
170     if ((protocol == IP_PROTOCOL_UDP) ||
171         (protocol == IP_PROTOCOL_TCP)) {
172       feat_mask = IP_UDP_TCP_FEAT_MASK;
173     } else {
174       feat_mask = IP4_FEAT_MASK;
175     }
176   } else if (ethertype == ETHERNET_TYPE_IP6) {
177     protocol = ((ip6_header_t *)l3h0)->protocol;
178     // Don't bother checking for extension headers for now
179     if ((protocol == IP_PROTOCOL_UDP) ||
180         (protocol == IP_PROTOCOL_TCP)) {
181       feat_mask = IP_UDP_TCP_FEAT_MASK;
182     } else {
183       feat_mask = IP6_FEAT_MASK;
184     }
185   } else if (ethertype == ETHERNET_TYPE_MPLS_UNICAST) {
186     feat_mask = IP6_FEAT_MASK;
187   } else {
188     // allow all features
189     feat_mask = ~0;
190   } 
191
192   // determine layer2 kind for stat and mask
193   mcast_dmac = ethernet_address_cast(h0->dst_address);
194   l2bcast = 0;
195   l2mcast = 0;
196   l2_stat_kind = STATS_IF_LAYER2_UCAST_INPUT_CNT;
197   if (PREDICT_FALSE (mcast_dmac)) {
198     u32 *dsthi = (u32 *) &h0->dst_address[0];
199     u32 *dstlo = (u32 *) &h0->dst_address[2];
200
201     // Disable bridge forwarding (flooding will execute instead if not xconnect)
202     feat_mask &= ~(L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD);
203     if (ethertype != ETHERNET_TYPE_ARP) // Disable ARP-term for non-ARP packet
204         feat_mask &= ~(L2INPUT_FEAT_ARP_TERM);
205
206     // dest mac is multicast or broadcast
207     if ((*dstlo == 0xFFFFFFFF) && (*dsthi == 0xFFFFFFFF)) { 
208       // dest mac == FF:FF:FF:FF:FF:FF
209       l2_stat_kind = STATS_IF_LAYER2_BCAST_INPUT_CNT;
210       l2bcast=1;
211     } else {
212       l2_stat_kind = STATS_IF_LAYER2_MCAST_INPUT_CNT;
213       l2mcast=1;
214     }
215   }
216   // TODO: take l2 stat
217
218   // Get config for the input interface
219   config = vec_elt_at_index(msm->configs, sw_if_index0);
220
221   // Save split horizon group, use 0 for BVI to make sure not dropped
222   vnet_buffer(b0)->l2.shg = bvi_flg ? 0 : config->shg;
223
224   if (config->xconnect) {
225     // Set the output interface
226     vnet_buffer(b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index;
227
228   } else {
229
230     // Do bridge-domain processing
231     bd_index0 = config->bd_index;
232     // save BD ID for next feature graph nodes
233     vnet_buffer(b0)->l2.bd_index = bd_index0;
234
235     // Get config for the bridge domain interface
236     bd_config = vec_elt_at_index(msm->bd_configs, bd_index0);
237
238     // Process bridge domain feature enables.
239     // To perform learning/flooding/forwarding, the corresponding bit
240     // must be enabled in both the input interface config and in the
241     // bridge domain config. In the bd_bitmap, bits for features other
242     // than learning/flooding/forwarding should always be set.
243     feat_mask = feat_mask & bd_config->feature_bitmap;
244   }
245
246   // mask out features from bitmap using packet type and bd config
247   feature_bitmap = config->feature_bitmap & feat_mask;
248
249   // save for next feature graph nodes
250   vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap;
251
252   // Determine the next node
253   *next0 = feat_bitmap_get_next_node_index(msm->feat_next_node_index,
254                                            feature_bitmap);
255 }
256
257
258 static uword
259 l2input_node_fn (vlib_main_t * vm,
260                  vlib_node_runtime_t * node,
261                  vlib_frame_t * frame)
262 {
263   u32 n_left_from, * from, * to_next;
264   l2input_next_t next_index;
265   l2input_main_t * msm = &l2input_main;
266   vlib_node_t *n = vlib_get_node (vm, l2input_node.index);
267   u32 node_counter_base_index = n->error_heap_index;
268   vlib_error_main_t * em = &vm->error_main;
269   u32 cpu_index = os_get_cpu_number();
270
271   from = vlib_frame_vector_args (frame);
272   n_left_from = frame->n_vectors; /* number of packets to process */
273   next_index = node->cached_next_index;
274
275   while (n_left_from > 0)
276     {
277       u32 n_left_to_next;
278
279       /* get space to enqueue frame to graph node "next_index" */
280       vlib_get_next_frame (vm, node, next_index,
281                            to_next, n_left_to_next);
282
283       while (n_left_from >= 6 && n_left_to_next >= 2)
284         {
285           u32 bi0, bi1;
286           vlib_buffer_t * b0, * b1;
287           u32 next0, next1;
288           u32 sw_if_index0, sw_if_index1;
289           
290           /* Prefetch next iteration. */
291           {
292             vlib_buffer_t * p2, * p3, * p4 , * p5;
293             u32 sw_if_index2, sw_if_index3;
294             
295             p2 = vlib_get_buffer (vm, from[2]);
296             p3 = vlib_get_buffer (vm, from[3]);
297             p4 = vlib_get_buffer (vm, from[4]);
298             p5 = vlib_get_buffer (vm, from[5]);
299             
300             // Prefetch the buffer header and packet for the N+2 loop iteration
301             vlib_prefetch_buffer_header (p4, LOAD);
302             vlib_prefetch_buffer_header (p5, LOAD);
303
304             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
305             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
306
307             // Prefetch the input config for the N+1 loop iteration
308             // This depends on the buffer header above
309             sw_if_index2 = vnet_buffer(p2)->sw_if_index[VLIB_RX];
310             sw_if_index3 = vnet_buffer(p3)->sw_if_index[VLIB_RX];
311             CLIB_PREFETCH (&msm->configs[sw_if_index2], CLIB_CACHE_LINE_BYTES, LOAD);
312             CLIB_PREFETCH (&msm->configs[sw_if_index3], CLIB_CACHE_LINE_BYTES, LOAD);
313
314             // Don't bother prefetching the bridge-domain config (which 
315             // depends on the input config above). Only a small number of
316             // bridge domains are expected. Plus the structure is small
317             // and several fit in a cache line.
318           }
319
320           /* speculatively enqueue b0 and b1 to the current next frame */
321           /* bi is "buffer index", b is pointer to the buffer */
322           to_next[0] = bi0 = from[0];
323           to_next[1] = bi1 = from[1];
324           from += 2;
325           to_next += 2;
326           n_left_from -= 2;
327           n_left_to_next -= 2;
328
329           b0 = vlib_get_buffer (vm, bi0);
330           b1 = vlib_get_buffer (vm, bi1);
331
332           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) {
333             /* RX interface handles */
334             sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
335             sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
336
337             if (b0->flags & VLIB_BUFFER_IS_TRACED) {
338               ethernet_header_t * h0 = vlib_buffer_get_current (b0);
339               l2input_trace_t *t = 
340                       vlib_add_trace (vm, node, b0, sizeof (*t));
341               t->sw_if_index = sw_if_index0;
342               memcpy(t->src, h0->src_address, 6);
343               memcpy(t->dst, h0->dst_address, 6);
344             }
345             if (b1->flags & VLIB_BUFFER_IS_TRACED) {
346               ethernet_header_t * h1 = vlib_buffer_get_current (b1);
347               l2input_trace_t *t = 
348                       vlib_add_trace (vm, node, b1, sizeof (*t));
349               t->sw_if_index = sw_if_index1;
350               memcpy(t->src, h1->src_address, 6);
351               memcpy(t->dst, h1->dst_address, 6);
352             }
353           }
354
355           em->counters[node_counter_base_index + L2INPUT_ERROR_L2INPUT] += 2;
356
357           classify_and_dispatch (vm,
358                                  node,
359                                  cpu_index, 
360                                  msm,
361                                  b0,
362                                  &next0);
363
364           classify_and_dispatch (vm,
365                                  node,
366                                  cpu_index, 
367                                  msm,
368                                  b1,
369                                  &next1);
370
371           /* verify speculative enqueues, maybe switch current next frame */
372           /* if next0==next1==next_index then nothing special needs to be done */
373           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
374                                            to_next, n_left_to_next,
375                                            bi0, bi1, next0, next1);
376       }
377       
378       while (n_left_from > 0 && n_left_to_next > 0)
379         {
380           u32 bi0;
381           vlib_buffer_t * b0;
382           u32 next0;
383           u32 sw_if_index0;
384  
385           /* speculatively enqueue b0 to the current next frame */
386           bi0 = from[0];
387           to_next[0] = bi0;
388           from += 1;
389           to_next += 1;
390           n_left_from -= 1;
391           n_left_to_next -= 1;
392
393           b0 = vlib_get_buffer (vm, bi0);
394
395           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
396                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
397             ethernet_header_t * h0 = vlib_buffer_get_current (b0);
398             l2input_trace_t *t = 
399                vlib_add_trace (vm, node, b0, sizeof (*t));
400             sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
401             t->sw_if_index = sw_if_index0;
402             memcpy(t->src, h0->src_address, 6);
403             memcpy(t->dst, h0->dst_address, 6);
404           }
405
406           em->counters[node_counter_base_index + L2INPUT_ERROR_L2INPUT] += 1;
407
408           classify_and_dispatch (vm,
409                                  node,
410                                  cpu_index, 
411                                  msm,
412                                  b0,
413                                  &next0);
414
415           /* verify speculative enqueue, maybe switch current next frame */
416           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
417                                            to_next, n_left_to_next,
418                                            bi0, next0);
419         }
420
421       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
422     }
423
424   return frame->n_vectors;
425 }
426
427
428 VLIB_REGISTER_NODE (l2input_node,static) = {
429   .function = l2input_node_fn,
430   .name = "l2-input",
431   .vector_size = sizeof (u32),
432   .format_trace = format_l2input_trace,
433   .format_buffer = format_ethernet_header_with_length,
434   .type = VLIB_NODE_TYPE_INTERNAL,
435   
436   .n_errors = ARRAY_LEN(l2input_error_strings),
437   .error_strings = l2input_error_strings,
438
439   .n_next_nodes = L2INPUT_N_NEXT,
440
441   /* edit / add dispositions here */
442   .next_nodes = {
443        [L2INPUT_NEXT_LEARN] = "l2-learn",
444        [L2INPUT_NEXT_FWD]   = "l2-fwd",
445        [L2INPUT_NEXT_DROP]  = "error-drop",
446   },
447 };
448
449 clib_error_t *l2input_init (vlib_main_t *vm)
450 {
451   l2input_main_t * mp = &l2input_main;
452  
453   mp->vlib_main = vm;
454   mp->vnet_main = vnet_get_main();
455
456   // Get packets RX'd from L2 interfaces
457   ethernet_register_l2_input (vm, l2input_node.index);
458
459   // Create the config vector
460   vec_validate(mp->configs, 100);  
461   // create 100 sw interface entries and zero them
462
463   // Initialize the feature next-node indexes
464   feat_bitmap_init_next_nodes(vm,
465                               l2input_node.index,
466                               L2INPUT_N_FEAT,
467                               l2input_get_feat_names(),
468                               mp->feat_next_node_index);
469
470   return 0;
471 }
472
473 VLIB_INIT_FUNCTION (l2input_init);
474
475
476 // Get a pointer to the config for the given interface
477 l2_input_config_t * l2input_intf_config (u32 sw_if_index)
478 {
479   l2input_main_t * mp = &l2input_main;
480  
481   vec_validate(mp->configs, sw_if_index);  
482   return vec_elt_at_index(mp->configs, sw_if_index);
483 }
484
485 // Enable (or disable) the feature in the bitmap for the given interface
486 u32 l2input_intf_bitmap_enable (u32 sw_if_index,
487                                  u32 feature_bitmap,
488                                  u32 enable)
489 {
490   l2input_main_t * mp = &l2input_main;
491   l2_input_config_t *config;
492  
493   vec_validate(mp->configs, sw_if_index);  
494   config = vec_elt_at_index(mp->configs, sw_if_index);
495
496   if (enable) {
497     config->feature_bitmap |= feature_bitmap;
498   } else {
499     config->feature_bitmap &= ~feature_bitmap;
500   }
501
502   return config->feature_bitmap;
503 }
504
505
506
507 // Set the subinterface to run in l2 or l3 mode.
508 // for L3 mode, just the sw_if_index is specified
509 // for bridged mode, the bd id and bvi flag are also specified
510 // for xconnect mode, the peer sw_if_index is also specified
511 // Return 0 if ok, or non-0 if there was an error
512
513 u32 set_int_l2_mode (vlib_main_t * vm,
514                      vnet_main_t * vnet_main,
515                      u32 mode,
516                      u32 sw_if_index,
517                      u32 bd_index, // for bridged interface
518                      u32 bvi,   // the bridged interface is the BVI
519                      u32 shg,   // the bridged interface's split horizon group
520                      u32 xc_sw_if_index) // peer interface for xconnect
521 {
522   l2input_main_t * mp = &l2input_main;
523   vnet_main_t * vnm = vnet_get_main();
524   vnet_hw_interface_t * hi;
525   l2_output_config_t * out_config;
526   l2_input_config_t * config;
527   l2_bridge_domain_t * bd_config;
528   l2_flood_member_t member;
529   u64 mac;
530   i32 l2_if_adjust = 0; 
531
532   hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
533
534   vec_validate(mp->configs, sw_if_index);  
535   config = vec_elt_at_index(mp->configs, sw_if_index);
536
537   if (config->bridge) {
538     // Interface is already in bridge mode. Undo the existing config.
539     bd_config = vec_elt_at_index(mp->bd_configs, config->bd_index);
540
541     // remove interface from flood vector
542     bd_remove_member (bd_config, sw_if_index);
543
544     // undo any BVI-related config
545     if (bd_config->bvi_sw_if_index == sw_if_index) {
546       bd_config->bvi_sw_if_index = ~0;
547       config->bvi = 0;
548
549       // restore output node
550       hi->output_node_index = bd_config->saved_bvi_output_node_index;
551
552       // delete the l2fib entry for the bvi interface
553       mac = *((u64 *)hi->hw_address);
554       l2fib_del_entry (mac, config->bd_index);
555     } 
556     l2_if_adjust--;
557   } else if (config->xconnect) {
558     l2_if_adjust--;
559   }
560
561   // Initialize the l2-input configuration for the interface
562   if (mode == MODE_L3) {
563     config->xconnect = 0;
564     config->bridge = 0;
565     config->shg = 0;
566     config->bd_index = 0;
567     config->feature_bitmap = L2INPUT_FEAT_DROP;
568   } else if (mode == MODE_L2_CLASSIFY) {
569       config->xconnect = 1;
570       config->bridge = 0;
571       config->output_sw_if_index = xc_sw_if_index;
572
573       // Make sure last-chance drop is configured
574       config->feature_bitmap |= L2INPUT_FEAT_DROP | L2INPUT_FEAT_CLASSIFY;
575
576       // Make sure bridging features are disabled
577       config->feature_bitmap &= 
578           ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
579       shg = 0; // not used in xconnect
580
581       // Insure all packets go to ethernet-input
582       ethernet_set_rx_redirect (vnet_main, hi, 1);
583   } else {
584
585     if (mode == MODE_L2_BRIDGE) {
586         /* 
587          * Remove a check that the interface must be an Ethernet.
588          * Specifically so we can bridge to L3 tunnel interfaces.
589          * Here's the check:
590          * if (hi->hw_class_index != ethernet_hw_interface_class.index)
591          * 
592          */
593         if (!hi)
594             return MODE_ERROR_ETH;  // non-ethernet
595
596       config->xconnect = 0;
597       config->bridge = 1;
598       config->bd_index = bd_index;
599
600       // Enable forwarding, flooding, learning and ARP termination by default
601       // (note that ARP term is disabled on BD feature bitmap by default)
602       config->feature_bitmap |= L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD | 
603           L2INPUT_FEAT_FLOOD | L2INPUT_FEAT_LEARN | L2INPUT_FEAT_ARP_TERM;
604
605       // Make sure last-chance drop is configured
606       config->feature_bitmap |= L2INPUT_FEAT_DROP;
607
608       // Make sure xconnect is disabled
609       config->feature_bitmap &= ~L2INPUT_FEAT_XCONNECT;
610
611       // Set up bridge domain
612       vec_validate(mp->bd_configs, bd_index);  
613       bd_config = vec_elt_at_index(mp->bd_configs, bd_index);
614       bd_validate (bd_config);
615
616       // TODO: think: add l2fib entry even for non-bvi interface?
617  
618       // Do BVI interface initializations
619       if (bvi) {
620         // insure BD has no bvi interface (or replace that one with this??)
621         if (bd_config->bvi_sw_if_index != ~0) {
622           return MODE_ERROR_BVI_DEF; // bd already has a bvi interface
623         } 
624         bd_config->bvi_sw_if_index = sw_if_index;
625         config->bvi = 1;
626
627         // make BVI outputs go to l2-input
628         bd_config->saved_bvi_output_node_index = hi->output_node_index;
629         hi->output_node_index = l2input_node.index;
630
631         // create the l2fib entry for the bvi interface
632         mac = *((u64 *)hi->hw_address);
633         l2fib_add_entry (mac, bd_index, sw_if_index, 1, 0, 1);  // static + bvi
634
635         // Disable learning by default. no use since l2fib entry is static.
636         config->feature_bitmap &= ~L2INPUT_FEAT_LEARN;
637
638         // Add BVI to arp_input_next_index_by_hw_if_index table so arp-input
639         // node can send out ARP response via BVI to BD
640         ethernet_arp_hw_interface_link_up_down(vnet_main, hi->hw_if_index, 0);
641  
642       }
643
644       // Add interface to bridge-domain flood vector
645       member.sw_if_index = sw_if_index;
646       member.flags = bvi ? L2_FLOOD_MEMBER_BVI : L2_FLOOD_MEMBER_NORMAL;
647       member.shg = shg;
648       bd_add_member (bd_config, &member);
649      
650     } else {
651       config->xconnect = 1;
652       config->bridge = 0;
653       config->output_sw_if_index = xc_sw_if_index;
654
655       // Make sure last-chance drop is configured
656       config->feature_bitmap |= L2INPUT_FEAT_DROP;
657
658       // Make sure bridging features are disabled
659       config->feature_bitmap &= ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
660
661       config->feature_bitmap |= L2INPUT_FEAT_XCONNECT;
662       shg = 0; // not used in xconnect
663     }
664
665     // set up split-horizon group
666     config->shg = shg;
667     out_config = l2output_intf_config (sw_if_index);
668     out_config->shg = shg;
669
670     // Test: remove this when non-IP features can be configured.
671     // Enable a non-IP feature to test IP feature masking
672     // config->feature_bitmap |= L2INPUT_FEAT_CTRL_PKT;
673
674     l2_if_adjust++;
675   }
676
677   // Adjust count of L2 interfaces
678   hi->l2_if_count += l2_if_adjust;
679
680   if (hi->hw_class_index == ethernet_hw_interface_class.index) {
681     if ((hi->l2_if_count == 1) && (l2_if_adjust == 1)) {
682       // Just added first L2 interface on this port
683
684       // Set promiscuous mode on the l2 interface
685       ethernet_set_flags (vnet_main, hi->hw_if_index,
686                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
687
688       // Insure all packets go to ethernet-input
689       ethernet_set_rx_redirect (vnet_main, hi, 1);
690
691     } else if ((hi->l2_if_count == 0) && (l2_if_adjust == -1)) {
692       // Just removed only L2 subinterface on this port
693
694       // Disable promiscuous mode on the l2 interface
695       ethernet_set_flags (vnet_main, hi->hw_if_index, 0);
696
697       // Allow ip packets to go directly to ip4-input etc
698       ethernet_set_rx_redirect (vnet_main, hi, 0);
699     }
700   }
701
702   // Set up the L2/L3 flag in the interface parsing tables
703   ethernet_sw_interface_set_l2_mode(vnm, sw_if_index, (mode!=MODE_L3));
704
705   return 0;
706 }
707
708 // set subinterface in bridging mode with a bridge-domain ID
709 // The CLI format is:
710 //    set interface l2 bridge <interface> <bd> [bvi] [split-horizon-group]
711 static clib_error_t *
712 int_l2_bridge  (vlib_main_t * vm,
713                 unformat_input_t * input,
714                 vlib_cli_command_t * cmd)
715 {
716   vnet_main_t * vnm = vnet_get_main();
717   clib_error_t * error = 0;
718   u32 bd_index, bd_id;
719   u32 sw_if_index;
720   u32 bvi;
721   u32 rc;
722   u32 shg;
723
724   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
725     {
726       error = clib_error_return (0, "unknown interface `%U'",
727                                  format_unformat_error, input);
728       goto done;
729     }
730
731   if (!unformat (input, "%d", &bd_id)) {
732       error = clib_error_return (0, "expected bridge domain ID `%U'",
733                                  format_unformat_error, input);
734       goto done;
735   }
736
737   bd_index = bd_find_or_add_bd_index (&bd_main, bd_id);
738
739   // optional bvi 
740   bvi = unformat (input, "bvi");
741
742   // optional split horizon group
743   shg = 0;
744   (void) unformat (input, "%d", &shg);
745
746   // set the interface mode
747   if ((rc = set_int_l2_mode(vm, vnm, MODE_L2_BRIDGE, sw_if_index, bd_index, bvi, shg, 0))) {
748     if (rc == MODE_ERROR_ETH) {
749       error = clib_error_return (0, "bridged interface must be ethernet",
750                                  format_unformat_error, input);
751     } else if (rc == MODE_ERROR_BVI_DEF) {
752       error = clib_error_return (0, "bridge-domain already has a bvi interface",
753                                  format_unformat_error, input);
754     } else {
755       error = clib_error_return (0, "invalid configuration for interface",
756                                  format_unformat_error, input);
757     }
758     goto done;
759   }
760
761  done:
762   return error;
763 }
764
765 VLIB_CLI_COMMAND (int_l2_bridge_cli, static) = {
766   .path = "set interface l2 bridge",
767   .short_help = "set interface to L2 bridging mode in <bridge-domain ID> [bvi] [shg]",
768   .function = int_l2_bridge,
769 };
770
771 // set subinterface in xconnect mode with another interface
772 // The CLI format is:
773 //    set interface l2 xconnect <interface> <peer interface>
774 static clib_error_t *
775 int_l2_xc (vlib_main_t * vm,
776            unformat_input_t * input,
777            vlib_cli_command_t * cmd)
778 {
779   vnet_main_t * vnm = vnet_get_main();
780   clib_error_t * error = 0;
781   u32 sw_if_index;
782   u32 xc_sw_if_index;
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_user (input, unformat_vnet_sw_interface, vnm, &xc_sw_if_index))
792     {
793       error = clib_error_return (0, "unknown peer interface `%U'",
794                                  format_unformat_error, input);
795       goto done;
796     }
797
798   // set the interface mode
799   if (set_int_l2_mode(vm, vnm, MODE_L2_XC, sw_if_index, 0, 0, 0, xc_sw_if_index)) {
800       error = clib_error_return (0, "invalid configuration for interface",
801                                  format_unformat_error, input);
802       goto done;
803   }
804
805  done:
806   return error;
807 }
808
809 VLIB_CLI_COMMAND (int_l2_xc_cli, static) = {
810   .path = "set interface l2 xconnect",
811   .short_help = "set interface to L2 cross-connect mode with <peer interface>",
812   .function = int_l2_xc,
813 };
814
815 // set subinterface in L3 mode
816 // The CLI format is:
817 //    set interface l3 <interface>
818 static clib_error_t *
819 int_l3  (vlib_main_t * vm,
820          unformat_input_t * input,
821          vlib_cli_command_t * cmd)
822 {
823   vnet_main_t * vnm = vnet_get_main();
824   clib_error_t * error = 0;
825   u32 sw_if_index;
826
827   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
828     {
829       error = clib_error_return (0, "unknown interface `%U'",
830                                  format_unformat_error, input);
831       goto done;
832     }
833
834   // set the interface mode
835   if (set_int_l2_mode(vm, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0)) {
836       error = clib_error_return (0, "invalid configuration for interface",
837                                  format_unformat_error, input);
838       goto done;
839   }
840
841  done:
842   return error;
843 }
844
845 VLIB_CLI_COMMAND (int_l3_cli, static) = {
846   .path = "set interface l3",
847   .short_help = "set interface to L3 mode",
848   .function = int_l3,
849 };
850
851 // The CLI format is:
852 //    show mode [<if-name1> <if-name2> ...]
853 static clib_error_t *
854 show_int_mode  (vlib_main_t * vm,
855          unformat_input_t * input,
856          vlib_cli_command_t * cmd)
857 {
858   vnet_main_t * vnm = vnet_get_main();
859   clib_error_t * error = 0;
860   char * mode;
861   u8 * args;
862   vnet_interface_main_t * im = &vnm->interface_main;
863   vnet_sw_interface_t * si, * sis = 0;
864   l2input_main_t * mp = &l2input_main;
865   l2_input_config_t * config;
866   
867   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
868     {
869        u32 sw_if_index;
870
871       /* See if user wants to show specific interface */
872       if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
873         {
874           si =  pool_elt_at_index (im->sw_interfaces, sw_if_index);
875           vec_add1 (sis, si[0]);
876         }
877       else
878         {
879           error = clib_error_return (0, "unknown input `%U'",
880                                      format_unformat_error, input);
881           goto done;
882         }
883
884     }
885
886   if (vec_len (sis) == 0) /* Get all interfaces */
887     {
888       /* Gather interfaces. */
889       sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
890       _vec_len (sis) = 0;
891       pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sis, si[0]); }));
892     }
893   
894   vec_foreach (si, sis)
895     {
896       vec_validate(mp->configs, si->sw_if_index);
897       config = vec_elt_at_index(mp->configs, si->sw_if_index);
898       if (config->bridge) {
899           u32 bd_id;
900           mode = "l2 bridge";
901           bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
902
903           args = format (0, "bd_id %d%s%d", bd_id, 
904                          config->bvi ? " bvi shg " : " shg ", config->shg);
905       } else if (config->xconnect) {
906           mode = "l2 xconnect";
907           args = format (0, "%U",
908           format_vnet_sw_if_index_name,
909           vnm, config->output_sw_if_index);
910       } else {
911         mode = "l3";
912         args = format (0, " ");
913       }
914       vlib_cli_output (vm, "%s %U %v\n",
915         mode,
916         format_vnet_sw_if_index_name,
917         vnm, si->sw_if_index,
918         args);
919       vec_free (args);
920   }
921
922 done:
923   vec_free (sis);
924   
925   return error;
926 }
927
928 VLIB_CLI_COMMAND (show_l2_mode, static) = {
929   .path = "show mode",
930   .short_help = "show mode [<if-name1> <if-name2> ...]",
931   .function = show_int_mode,
932 };
933
934 #define foreach_l2_init_function                \
935 _(feat_bitmap_drop_init)                        \
936 _(l2fib_init)                                   \
937 _(l2_classify_init)                             \
938 _(l2bd_init)                                    \
939 _(l2fwd_init)                                   \
940 _(l2_inacl_init)                                \
941 _(l2input_init)                                 \
942 _(l2_vtr_init)                                  \
943 _(l2_invtr_init)                                \
944 _(l2_efp_filter_init)                           \
945 _(l2learn_init)                                 \
946 _(l2flood_init)                                 \
947 _(l2_outacl_init)                               \
948 _(l2output_init)                                \
949 _(l2_patch_init)                                \
950 _(l2_xcrw_init)
951
952 clib_error_t *l2_init (vlib_main_t * vm)
953 {
954   clib_error_t * error;
955   
956 #define _(a) do {                                                       \
957   if ((error = vlib_call_init_function (vm, a))) return error; }        \
958 while (0);
959   foreach_l2_init_function;
960 #undef _
961   return 0;
962 }
963
964 VLIB_INIT_FUNCTION (l2_init);