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