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