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