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