l2: performance enhancement in l2input
[vpp.git] / src / 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 <vnet/fib/fib_node.h>
27 #include <vnet/ethernet/arp_packet.h>
28 #include <vlib/cli.h>
29 #include <vnet/l2/l2_input.h>
30 #include <vnet/l2/l2_output.h>
31 #include <vnet/l2/feat_bitmap.h>
32 #include <vnet/l2/l2_bvi.h>
33 #include <vnet/l2/l2_fib.h>
34 #include <vnet/l2/l2_bd.h>
35
36 #include <vppinfra/error.h>
37 #include <vppinfra/hash.h>
38 #include <vppinfra/cache.h>
39
40 /**
41  * @file
42  * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3).
43  *
44  * This file contains the CLI Commands that modify the input mode of an
45  * interface. For interfaces in a Layer 2 cross-connect, all packets
46  * received on one interface will be transmitted to the other. For
47  * interfaces in a bridge-domain, packets will be forwarded to other
48  * interfaces in the same bridge-domain based on destination mac address.
49  * For interfaces in Layer 3 mode, the packets will be routed.
50  */
51
52 #ifndef CLIB_MARCH_VARIANT
53
54 /* Feature graph node names */
55 static char *l2input_feat_names[] = {
56 #define _(sym,name) name,
57   foreach_l2input_feat
58 #undef _
59 };
60
61 char **
62 l2input_get_feat_names (void)
63 {
64   return l2input_feat_names;
65 }
66
67 u8 *
68 format_l2_input_features (u8 * s, va_list * args)
69 {
70   static char *display_names[] = {
71 #define _(sym,name) #sym,
72     foreach_l2input_feat
73 #undef _
74   };
75   u32 feature_bitmap = va_arg (*args, u32);
76   u32 verbose = va_arg (*args, u32);
77
78   if (feature_bitmap == 0)
79     {
80       s = format (s, "  none configured");
81       return s;
82     }
83
84   feature_bitmap &= ~L2INPUT_FEAT_DROP; /* Not a feature */
85   int i;
86   for (i = L2INPUT_N_FEAT - 1; i >= 0; i--)
87     {
88       if (feature_bitmap & (1 << i))
89         {
90           if (verbose)
91             s = format (s, "%17s (%s)\n",
92                         display_names[i], l2input_feat_names[i]);
93           else
94             s = format (s, "%s ", l2input_feat_names[i]);
95         }
96     }
97   return s;
98 }
99 #endif /* CLIB_MARCH_VARIANT */
100
101 typedef struct
102 {
103   /* per-pkt trace data */
104   u8 dst_and_src[12];
105   u32 next_index;
106   u32 sw_if_index;
107 } l2input_trace_t;
108
109 /* packet trace format function */
110 static u8 *
111 format_l2input_trace (u8 * s, va_list * args)
112 {
113   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
114   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
115   l2input_trace_t *t = va_arg (*args, l2input_trace_t *);
116
117   s = format (s, "l2-input: sw_if_index %d dst %U src %U",
118               t->sw_if_index,
119               format_ethernet_address, t->dst_and_src,
120               format_ethernet_address, t->dst_and_src + 6);
121   return s;
122 }
123
124 extern l2input_main_t l2input_main;
125
126 #ifndef CLIB_MARCH_VARIANT
127 l2input_main_t l2input_main;
128 #endif /* CLIB_MARCH_VARIANT */
129
130 #define foreach_l2input_error                   \
131 _(L2INPUT,     "L2 input packets")              \
132 _(DROP,        "L2 input drops")
133
134 typedef enum
135 {
136 #define _(sym,str) L2INPUT_ERROR_##sym,
137   foreach_l2input_error
138 #undef _
139     L2INPUT_N_ERROR,
140 } l2input_error_t;
141
142 static char *l2input_error_strings[] = {
143 #define _(sym,string) string,
144   foreach_l2input_error
145 #undef _
146 };
147
148 typedef enum
149 {                               /*  */
150   L2INPUT_NEXT_LEARN,
151   L2INPUT_NEXT_FWD,
152   L2INPUT_NEXT_DROP,
153   L2INPUT_N_NEXT,
154 } l2input_next_t;
155
156
157 static_always_inline void
158 classify_and_dispatch (l2input_main_t * msm, vlib_buffer_t * b0, u32 * next0)
159 {
160   /*
161    * Load L2 input feature struct
162    * Load bridge domain struct
163    * Parse ethernet header to determine unicast/mcast/broadcast
164    * take L2 input stat
165    * classify packet as IP/UDP/TCP, control, other
166    * mask feature bitmap
167    * go to first node in bitmap
168    * Later: optimize VTM
169    *
170    * For L2XC,
171    *   set tx sw-if-handle
172    */
173
174   u32 feat_mask = ~0;
175   u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
176   ethernet_header_t *h0 = vlib_buffer_get_current (b0);
177
178   /* Get config for the input interface */
179   l2_input_config_t *config = vec_elt_at_index (msm->configs, sw_if_index0);
180
181   /* Save split horizon group */
182   vnet_buffer (b0)->l2.shg = config->shg;
183
184   /* determine layer2 kind for stat and mask */
185   if (PREDICT_FALSE (ethernet_address_cast (h0->dst_address)))
186     {
187       u8 *l3h0 = (u8 *) h0 + vnet_buffer (b0)->l2.l2_len;
188
189 #define get_u16(addr) ( *((u16 *)(addr)) )
190       u16 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
191       u8 protocol = ((ip6_header_t *) l3h0)->protocol;
192
193       /* Disable bridge forwarding (flooding will execute instead if not xconnect) */
194       feat_mask &= ~(L2INPUT_FEAT_FWD |
195                      L2INPUT_FEAT_UU_FLOOD |
196                      L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_GBP_FWD);
197
198       if (ethertype != ETHERNET_TYPE_ARP)
199         feat_mask &= ~(L2INPUT_FEAT_ARP_UFWD);
200
201       /* Disable ARP-term for non-ARP and non-ICMP6 packet */
202       if (ethertype != ETHERNET_TYPE_ARP &&
203           (ethertype != ETHERNET_TYPE_IP6 || protocol != IP_PROTOCOL_ICMP6))
204         feat_mask &= ~(L2INPUT_FEAT_ARP_TERM);
205       /*
206        * For packet from BVI - set SHG of ARP request or ICMPv6 neighbor
207        * solicitation packet from BVI to 0 so it can also flood to VXLAN
208        * tunnels or other ports with the same SHG as that of the BVI.
209        */
210       else if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] ==
211                               L2INPUT_BVI))
212         {
213           if (ethertype == ETHERNET_TYPE_ARP)
214             {
215               ethernet_arp_header_t *arp0 = (ethernet_arp_header_t *) l3h0;
216               if (arp0->opcode ==
217                   clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request))
218                 vnet_buffer (b0)->l2.shg = 0;
219             }
220           else                  /* must be ICMPv6 */
221             {
222               ip6_header_t *iph0 = (ip6_header_t *) l3h0;
223               icmp6_neighbor_solicitation_or_advertisement_header_t *ndh0;
224               ndh0 = ip6_next_header (iph0);
225               if (ndh0->icmp.type == ICMP6_neighbor_solicitation)
226                 vnet_buffer (b0)->l2.shg = 0;
227             }
228         }
229     }
230   else
231     {
232       /*
233        * For packet from BVI - set SHG of unicast packet from BVI to 0 so it
234        * is not dropped on output to VXLAN tunnels or other ports with the
235        * same SHG as that of the BVI.
236        */
237       if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] ==
238                          L2INPUT_BVI))
239         vnet_buffer (b0)->l2.shg = 0;
240     }
241
242
243   if (config->bridge)
244     {
245       /* Do bridge-domain processing */
246       u16 bd_index0 = config->bd_index;
247       /* save BD ID for next feature graph nodes */
248       vnet_buffer (b0)->l2.bd_index = bd_index0;
249
250       /* Get config for the bridge domain interface */
251       l2_bridge_domain_t *bd_config =
252         vec_elt_at_index (msm->bd_configs, bd_index0);
253
254       /* Save bridge domain and interface seq_num */
255       /* *INDENT-OFF* */
256       l2fib_seq_num_t sn = {
257         .swif = *l2fib_swif_seq_num(sw_if_index0),
258         .bd = bd_config->seq_num,
259       };
260       /* *INDENT-ON* */
261       vnet_buffer (b0)->l2.l2fib_sn = sn.as_u16;;
262       vnet_buffer (b0)->l2.bd_age = bd_config->mac_age;
263
264       /*
265        * Process bridge domain feature enables.
266        * To perform learning/flooding/forwarding, the corresponding bit
267        * must be enabled in both the input interface config and in the
268        * bridge domain config. In the bd_bitmap, bits for features other
269        * than learning/flooding/forwarding should always be set.
270        */
271       feat_mask = feat_mask & bd_config->feature_bitmap;
272     }
273   else if (config->xconnect)
274     {
275       /* Set the output interface */
276       vnet_buffer (b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index;
277     }
278   else
279     feat_mask = L2INPUT_FEAT_DROP;
280
281   /* mask out features from bitmap using packet type and bd config */
282   u32 feature_bitmap = config->feature_bitmap & feat_mask;
283
284   /* save for next feature graph nodes */
285   vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
286
287   /* Determine the next node */
288   *next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
289                                             feature_bitmap);
290 }
291
292 static_always_inline uword
293 l2input_node_inline (vlib_main_t * vm,
294                      vlib_node_runtime_t * node, vlib_frame_t * frame,
295                      int do_trace)
296 {
297   u32 n_left_from, *from, *to_next;
298   l2input_next_t next_index;
299   l2input_main_t *msm = &l2input_main;
300   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
301
302   from = vlib_frame_vector_args (frame);
303   n_left_from = frame->n_vectors;       /* number of packets to process */
304   next_index = node->cached_next_index;
305   vlib_get_buffers (vm, from, bufs, n_left_from);
306
307   while (n_left_from > 0)
308     {
309       u32 n_left_to_next;
310
311       /* get space to enqueue frame to graph node "next_index" */
312       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
313
314       while (n_left_from >= 8 && n_left_to_next >= 4)
315         {
316           u32 next0, next1, next2, next3;
317           u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
318
319           /* Prefetch next iteration. */
320           {
321
322             /* Prefetch the buffer header and packet for the N+2 loop iteration */
323             vlib_prefetch_buffer_header (b[4], LOAD);
324             vlib_prefetch_buffer_header (b[5], LOAD);
325             vlib_prefetch_buffer_header (b[6], LOAD);
326             vlib_prefetch_buffer_header (b[7], LOAD);
327
328             CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
329             CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
330             CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
331             CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
332
333             /*
334              * Don't bother prefetching the bridge-domain config (which
335              * depends on the input config above). Only a small number of
336              * bridge domains are expected. Plus the structure is small
337              * and several fit in a cache line.
338              */
339           }
340
341           /* speculatively enqueue b0 and b1 to the current next frame */
342           /* bi is "buffer index", b is pointer to the buffer */
343
344           if (do_trace)
345             {
346               /* RX interface handles */
347               sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
348               sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
349               sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
350               sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
351
352               if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
353                 {
354                   ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
355                   l2input_trace_t *t =
356                     vlib_add_trace (vm, node, b[0], sizeof (*t));
357                   t->sw_if_index = sw_if_index0;
358                   clib_memcpy_fast (t->dst_and_src, h0->dst_address,
359                                     sizeof (h0->dst_address) +
360                                     sizeof (h0->src_address));
361                 }
362               if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
363                 {
364                   ethernet_header_t *h1 = vlib_buffer_get_current (b[1]);
365                   l2input_trace_t *t =
366                     vlib_add_trace (vm, node, b[1], sizeof (*t));
367                   t->sw_if_index = sw_if_index1;
368                   clib_memcpy_fast (t->dst_and_src, h1->dst_address,
369                                     sizeof (h1->dst_address) +
370                                     sizeof (h1->src_address));
371                 }
372               if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
373                 {
374                   ethernet_header_t *h2 = vlib_buffer_get_current (b[2]);
375                   l2input_trace_t *t =
376                     vlib_add_trace (vm, node, b[2], sizeof (*t));
377                   t->sw_if_index = sw_if_index2;
378                   clib_memcpy_fast (t->dst_and_src, h2->dst_address,
379                                     sizeof (h2->dst_address) +
380                                     sizeof (h2->src_address));
381                 }
382               if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
383                 {
384                   ethernet_header_t *h3 = vlib_buffer_get_current (b[3]);
385                   l2input_trace_t *t =
386                     vlib_add_trace (vm, node, b[3], sizeof (*t));
387                   t->sw_if_index = sw_if_index3;
388                   clib_memcpy_fast (t->dst_and_src, h3->dst_address,
389                                     sizeof (h3->dst_address) +
390                                     sizeof (h3->src_address));
391                 }
392             }
393
394           classify_and_dispatch (msm, b[0], &next0);
395           classify_and_dispatch (msm, b[1], &next1);
396           //show the better performance when clib_memcpy_fast is put here.
397           clib_memcpy_fast (to_next, from, sizeof (from[0]) * 4);
398           to_next += 4;
399           classify_and_dispatch (msm, b[2], &next2);
400           classify_and_dispatch (msm, b[3], &next3);
401           b += 4;
402           n_left_from -= 4;
403           n_left_to_next -= 4;
404
405           /* verify speculative enqueues, maybe switch current next frame */
406           /* if next0==next1==next_index then nothing special needs to be done */
407           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
408                                            to_next, n_left_to_next,
409                                            from[0], from[1], from[2], from[3],
410                                            next0, next1, next2, next3);
411           from += 4;
412         }
413
414       while (n_left_from > 0 && n_left_to_next > 0)
415         {
416           u32 next0;
417           u32 sw_if_index0;
418
419           /* speculatively enqueue b0 to the current next frame */
420
421           if (do_trace && PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
422             {
423               ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
424               l2input_trace_t *t =
425                 vlib_add_trace (vm, node, b[0], sizeof (*t));
426               sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
427               t->sw_if_index = sw_if_index0;
428               clib_memcpy_fast (t->dst_and_src, h0->dst_address,
429                                 sizeof (h0->dst_address) +
430                                 sizeof (h0->src_address));
431             }
432
433           classify_and_dispatch (msm, b[0], &next0);
434           b += 1;
435           to_next[0] = from[0];
436           to_next += 1;
437           n_left_from -= 1;
438           n_left_to_next -= 1;
439
440           /* verify speculative enqueue, maybe switch current next frame */
441           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
442                                            to_next, n_left_to_next,
443                                            from[0], next0);
444           from += 1;
445         }
446
447       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
448     }
449
450   vlib_node_increment_counter (vm, l2input_node.index,
451                                L2INPUT_ERROR_L2INPUT, frame->n_vectors);
452
453   return frame->n_vectors;
454 }
455
456 VLIB_NODE_FN (l2input_node) (vlib_main_t * vm,
457                              vlib_node_runtime_t * node, vlib_frame_t * frame)
458 {
459   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
460     return l2input_node_inline (vm, node, frame, 1 /* do_trace */ );
461   return l2input_node_inline (vm, node, frame, 0 /* do_trace */ );
462 }
463
464 /* *INDENT-OFF* */
465 VLIB_REGISTER_NODE (l2input_node) = {
466   .name = "l2-input",
467   .vector_size = sizeof (u32),
468   .format_trace = format_l2input_trace,
469   .format_buffer = format_ethernet_header_with_length,
470   .type = VLIB_NODE_TYPE_INTERNAL,
471
472   .n_errors = ARRAY_LEN(l2input_error_strings),
473   .error_strings = l2input_error_strings,
474
475   .n_next_nodes = L2INPUT_N_NEXT,
476
477   /* edit / add dispositions here */
478   .next_nodes = {
479        [L2INPUT_NEXT_LEARN] = "l2-learn",
480        [L2INPUT_NEXT_FWD]   = "l2-fwd",
481        [L2INPUT_NEXT_DROP]  = "error-drop",
482   },
483 };
484 /* *INDENT-ON* */
485
486 #ifndef CLIB_MARCH_VARIANT
487 clib_error_t *
488 l2input_init (vlib_main_t * vm)
489 {
490   l2input_main_t *mp = &l2input_main;
491
492   mp->vlib_main = vm;
493   mp->vnet_main = vnet_get_main ();
494
495   /* Get packets RX'd from L2 interfaces */
496   ethernet_register_l2_input (vm, l2input_node.index);
497
498   /* Create the config vector */
499   vec_validate (mp->configs, 100);
500   /* create 100 sw interface entries and zero them */
501
502   /* Initialize the feature next-node indexes */
503   feat_bitmap_init_next_nodes (vm,
504                                l2input_node.index,
505                                L2INPUT_N_FEAT,
506                                l2input_get_feat_names (),
507                                mp->feat_next_node_index);
508
509   return 0;
510 }
511
512 VLIB_INIT_FUNCTION (l2input_init);
513
514
515 /** Get a pointer to the config for the given interface. */
516 l2_input_config_t *
517 l2input_intf_config (u32 sw_if_index)
518 {
519   l2input_main_t *mp = &l2input_main;
520
521   vec_validate (mp->configs, sw_if_index);
522   return vec_elt_at_index (mp->configs, sw_if_index);
523 }
524
525 /** Enable (or disable) the feature in the bitmap for the given interface. */
526 u32
527 l2input_intf_bitmap_enable (u32 sw_if_index,
528                             l2input_feat_masks_t feature_bitmap, u32 enable)
529 {
530   l2_input_config_t *config = l2input_intf_config (sw_if_index);
531
532   if (enable)
533     config->feature_bitmap |= feature_bitmap;
534   else
535     config->feature_bitmap &= ~feature_bitmap;
536
537   return config->feature_bitmap;
538 }
539
540 u32
541 l2input_set_bridge_features (u32 bd_index, u32 feat_mask, u32 feat_value)
542 {
543   l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);;
544   bd_validate (bd_config);
545   bd_config->feature_bitmap =
546     (bd_config->feature_bitmap & ~feat_mask) | feat_value;
547   return bd_config->feature_bitmap;
548 }
549
550 void
551 l2input_interface_mac_change (u32 sw_if_index,
552                               const u8 * old_address, const u8 * new_address)
553 {
554   /* check if the sw_if_index passed is a BVI in a BD */
555   l2_input_config_t *intf_config;
556
557   intf_config = l2input_intf_config (sw_if_index);
558
559   if (intf_config->bridge && intf_config->bvi)
560     {
561       /* delete and re-add l2fib entry for the bvi interface */
562       l2fib_del_entry (old_address, intf_config->bd_index, sw_if_index);
563       l2fib_add_entry (new_address,
564                        intf_config->bd_index,
565                        sw_if_index,
566                        L2FIB_ENTRY_RESULT_FLAG_BVI |
567                        L2FIB_ENTRY_RESULT_FLAG_STATIC);
568     }
569 }
570
571 /**
572  * Set the subinterface to run in l2 or l3 mode.
573  * For L3 mode, just the sw_if_index is specified.
574  * For bridged mode, the bd id and bvi flag are also specified.
575  * For xconnect mode, the peer sw_if_index is also specified.
576  * Return 0 if ok, or non-0 if there was an error.
577  */
578
579 u32
580 set_int_l2_mode (vlib_main_t * vm, vnet_main_t * vnet_main,     /*           */
581                  u32 mode,      /* One of L2 modes or back to L3 mode        */
582                  u32 sw_if_index,       /* sw interface index                */
583                  u32 bd_index,  /* for bridged interface                     */
584                  l2_bd_port_type_t port_type,   /* port_type */
585                  u32 shg,       /* the bridged interface split horizon group */
586                  u32 xc_sw_if_index)    /* peer interface for xconnect       */
587 {
588   l2input_main_t *mp = &l2input_main;
589   l2output_main_t *l2om = &l2output_main;
590   vnet_main_t *vnm = vnet_get_main ();
591   vnet_hw_interface_t *hi;
592   l2_output_config_t *out_config;
593   l2_input_config_t *config;
594   l2_bridge_domain_t *bd_config;
595   i32 l2_if_adjust = 0;
596   vnet_device_class_t *dev_class;
597
598   hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
599   config = l2input_intf_config (sw_if_index);
600
601   if (config->bridge)
602     {
603       /* Interface is already in bridge mode. Undo the existing config. */
604       bd_config = vec_elt_at_index (mp->bd_configs, config->bd_index);
605
606       /* remove interface from flood vector */
607       bd_remove_member (bd_config, sw_if_index);
608
609       /* undo any BVI-related config */
610       if (bd_config->bvi_sw_if_index == sw_if_index)
611         {
612           vnet_sw_interface_t *si;
613
614           bd_config->bvi_sw_if_index = ~0;
615           config->bvi = 0;
616
617           /* delete the l2fib entry for the bvi interface */
618           l2fib_del_entry (hi->hw_address, config->bd_index, sw_if_index);
619
620           /* since this is a no longer BVI interface do not to flood to it */
621           si = vnet_get_sw_interface (vnm, sw_if_index);
622           si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
623         }
624       if (bd_config->uu_fwd_sw_if_index == sw_if_index)
625         {
626           bd_config->uu_fwd_sw_if_index = ~0;
627           bd_config->feature_bitmap &= ~L2INPUT_FEAT_UU_FWD;
628         }
629
630       /* Clear MACs learned on the interface */
631       if ((config->feature_bitmap & L2INPUT_FEAT_LEARN) ||
632           (bd_config->feature_bitmap & L2INPUT_FEAT_LEARN))
633         l2fib_flush_int_mac (vm, sw_if_index);
634
635       l2_if_adjust--;
636     }
637   else if (config->xconnect)
638     {
639       l2_if_adjust--;
640     }
641
642   /* Make sure vector is big enough */
643   vec_validate_init_empty (l2om->output_node_index_vec, sw_if_index,
644                            L2OUTPUT_NEXT_DROP);
645
646   /* Initialize the l2-input configuration for the interface */
647   if (mode == MODE_L3)
648     {
649       /* Set L2 config to BD index 0 so that if any packet accidentally
650        * came in on L2 path, it will be dropped in BD 0 */
651       config->xconnect = 0;
652       config->bridge = 0;
653       config->shg = 0;
654       config->bd_index = 0;
655       config->feature_bitmap = L2INPUT_FEAT_DROP;
656
657       /* Clear L2 output config */
658       out_config = l2output_intf_config (sw_if_index);
659       clib_memset (out_config, 0, sizeof (l2_output_config_t));
660
661       /* Make sure any L2-output packet to this interface now in L3 mode is
662        * dropped. This may happen if L2 FIB MAC entry is stale */
663       l2om->output_node_index_vec[sw_if_index] = L2OUTPUT_NEXT_BAD_INTF;
664     }
665   else
666     {
667       /* Add or update l2-output node next-arc and output_node_index_vec table
668        * for the interface */
669       l2output_create_output_node_mapping (vm, vnet_main, sw_if_index);
670
671       if (mode == MODE_L2_BRIDGE)
672         {
673           u8 member_flags;
674
675           /*
676            * Remove a check that the interface must be an Ethernet.
677            * Specifically so we can bridge to L3 tunnel interfaces.
678            * Here's the check:
679            * if (hi->hw_class_index != ethernet_hw_interface_class.index)
680            *
681            */
682           if (!hi)
683             return MODE_ERROR_ETH;      /* non-ethernet */
684
685           config->xconnect = 0;
686           config->bridge = 1;
687           config->bd_index = bd_index;
688           *l2fib_valid_swif_seq_num (sw_if_index) += 1;
689
690           /*
691            * Enable forwarding, flooding, learning and ARP termination by default
692            * (note that ARP term is disabled on BD feature bitmap by default)
693            */
694           config->feature_bitmap |= (L2INPUT_FEAT_FWD |
695                                      L2INPUT_FEAT_UU_FLOOD |
696                                      L2INPUT_FEAT_UU_FWD |
697                                      L2INPUT_FEAT_FLOOD |
698                                      L2INPUT_FEAT_LEARN |
699                                      L2INPUT_FEAT_ARP_UFWD |
700                                      L2INPUT_FEAT_ARP_TERM);
701
702           /* Make sure last-chance drop is configured */
703           config->feature_bitmap |= L2INPUT_FEAT_DROP;
704
705           /* Make sure xconnect is disabled */
706           config->feature_bitmap &= ~L2INPUT_FEAT_XCONNECT;
707
708           /* Set up bridge domain */
709           bd_config = l2input_bd_config (bd_index);
710           bd_validate (bd_config);
711
712           /* TODO: think: add l2fib entry even for non-bvi interface? */
713
714           /* Do BVI interface initializations */
715           if (L2_BD_PORT_TYPE_BVI == port_type)
716             {
717               vnet_sw_interface_t *si;
718
719               /* ensure BD has no bvi interface (or replace that one with this??) */
720               if (bd_config->bvi_sw_if_index != ~0)
721                 {
722                   return MODE_ERROR_BVI_DEF;    /* bd already has a bvi interface */
723                 }
724               bd_config->bvi_sw_if_index = sw_if_index;
725               config->bvi = 1;
726
727               /* create the l2fib entry for the bvi interface */
728               l2fib_add_entry (hi->hw_address, bd_index, sw_if_index,
729                                L2FIB_ENTRY_RESULT_FLAG_BVI |
730                                L2FIB_ENTRY_RESULT_FLAG_STATIC);
731
732               /* Disable learning by default. no use since l2fib entry is static. */
733               config->feature_bitmap &= ~L2INPUT_FEAT_LEARN;
734
735               /* since this is a BVI interface we want to flood to it */
736               si = vnet_get_sw_interface (vnm, sw_if_index);
737               si->flood_class = VNET_FLOOD_CLASS_BVI;
738               member_flags = L2_FLOOD_MEMBER_BVI;
739             }
740           else if (L2_BD_PORT_TYPE_UU_FWD == port_type)
741             {
742               bd_config->uu_fwd_sw_if_index = sw_if_index;
743               bd_config->feature_bitmap |= L2INPUT_FEAT_UU_FWD;
744             }
745           else
746             {
747               member_flags = L2_FLOOD_MEMBER_NORMAL;
748             }
749
750           if (L2_BD_PORT_TYPE_NORMAL == port_type ||
751               L2_BD_PORT_TYPE_BVI == port_type)
752             {
753               /* Add interface to bridge-domain flood vector */
754               l2_flood_member_t member = {
755                 .sw_if_index = sw_if_index,
756                 .flags = member_flags,
757                 .shg = shg,
758               };
759               bd_add_member (bd_config, &member);
760             }
761         }
762       else if (mode == MODE_L2_XC)
763         {
764           config->xconnect = 1;
765           config->bridge = 0;
766           config->output_sw_if_index = xc_sw_if_index;
767
768           /* Make sure last-chance drop is configured */
769           config->feature_bitmap |= L2INPUT_FEAT_DROP;
770
771           /* Make sure bridging features are disabled */
772           config->feature_bitmap &=
773             ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
774
775           config->feature_bitmap |= L2INPUT_FEAT_XCONNECT;
776           shg = 0;              /* not used in xconnect */
777         }
778       else if (mode == MODE_L2_CLASSIFY)
779         {
780           config->xconnect = 1;
781           config->bridge = 0;
782           config->output_sw_if_index = xc_sw_if_index;
783
784           /* Make sure last-chance drop is configured */
785           config->feature_bitmap |=
786             L2INPUT_FEAT_DROP | L2INPUT_FEAT_INPUT_CLASSIFY;
787
788           /* Make sure bridging features are disabled */
789           config->feature_bitmap &=
790             ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
791           shg = 0;              /* not used in xconnect */
792         }
793
794       /* set up split-horizon group and set output feature bit */
795       config->shg = shg;
796       out_config = l2output_intf_config (sw_if_index);
797       out_config->shg = shg;
798       out_config->feature_bitmap |= L2OUTPUT_FEAT_OUTPUT;
799
800       /*
801        * Test: remove this when non-IP features can be configured.
802        * Enable a non-IP feature to test IP feature masking
803        * config->feature_bitmap |= L2INPUT_FEAT_CTRL_PKT;
804        */
805
806       l2_if_adjust++;
807     }
808
809   /* Adjust count of L2 interfaces */
810   hi->l2_if_count += l2_if_adjust;
811
812   if (hi->hw_class_index == ethernet_hw_interface_class.index)
813     {
814       if ((hi->l2_if_count == 1) && (l2_if_adjust == 1))
815         {
816           /* Just added first L2 interface on this port
817            * Set promiscuous mode on the l2 interface */
818           ethernet_set_flags (vnet_main, hi->hw_if_index,
819                               ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
820         }
821       else if ((hi->l2_if_count == 0) && (l2_if_adjust == -1))
822         {
823           /* Just removed only L2 subinterface on this port
824            * Disable promiscuous mode on the l2 interface */
825           ethernet_set_flags (vnet_main, hi->hw_if_index,
826                               /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
827
828         }
829     }
830
831   /* Set up the L2/L3 flag in the interface parsing tables */
832   ethernet_sw_interface_set_l2_mode (vnm, sw_if_index, (mode != MODE_L3));
833
834   dev_class = vnet_get_device_class (vnet_main, hi->dev_class_index);
835   if (dev_class->set_l2_mode_function)
836     {
837       dev_class->set_l2_mode_function (vnet_main, hi, l2_if_adjust);
838     }
839
840   return 0;
841 }
842 #endif /* CLIB_MARCH_VARIANT */
843
844 /**
845  * Set subinterface in bridging mode with a bridge-domain ID.
846  * The CLI format is:
847  *   set interface l2 bridge <interface> <bd> [bvi] [split-horizon-group]
848  */
849 static clib_error_t *
850 int_l2_bridge (vlib_main_t * vm,
851                unformat_input_t * input, vlib_cli_command_t * cmd)
852 {
853   vnet_main_t *vnm = vnet_get_main ();
854   l2_bd_port_type_t port_type;
855   clib_error_t *error = 0;
856   u32 bd_index, bd_id;
857   u32 sw_if_index;
858   u32 rc;
859   u32 shg;
860
861   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
862     {
863       error = clib_error_return (0, "unknown interface `%U'",
864                                  format_unformat_error, input);
865       goto done;
866     }
867
868   if (!unformat (input, "%d", &bd_id))
869     {
870       error = clib_error_return (0, "expected bridge domain ID `%U'",
871                                  format_unformat_error, input);
872       goto done;
873     }
874
875   if (bd_id > L2_BD_ID_MAX)
876     {
877       error = clib_error_return (0, "bridge domain ID exceed 16M limit",
878                                  format_unformat_error, input);
879       goto done;
880     }
881   bd_index = bd_find_or_add_bd_index (&bd_main, bd_id);
882
883   /* optional bvi  */
884   port_type = L2_BD_PORT_TYPE_NORMAL;
885   if (unformat (input, "bvi"))
886     port_type = L2_BD_PORT_TYPE_BVI;
887   if (unformat (input, "uu-fwd"))
888     port_type = L2_BD_PORT_TYPE_UU_FWD;
889
890   /* optional split horizon group */
891   shg = 0;
892   (void) unformat (input, "%d", &shg);
893
894   /* set the interface mode */
895   if ((rc =
896        set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE, sw_if_index, bd_index,
897                         port_type, shg, 0)))
898     {
899       if (rc == MODE_ERROR_ETH)
900         {
901           error = clib_error_return (0, "bridged interface must be ethernet",
902                                      format_unformat_error, input);
903         }
904       else if (rc == MODE_ERROR_BVI_DEF)
905         {
906           error =
907             clib_error_return (0, "bridge-domain already has a bvi interface",
908                                format_unformat_error, input);
909         }
910       else
911         {
912           error = clib_error_return (0, "invalid configuration for interface",
913                                      format_unformat_error, input);
914         }
915       goto done;
916     }
917
918 done:
919   return error;
920 }
921
922 /*?
923  * Use this command put an interface into Layer 2 bridge domain. If a
924  * bridge-domain with the provided bridge-domain-id does not exist, it
925  * will be created. Interfaces in a bridge-domain forward packets to
926  * other interfaces in the same bridge-domain based on destination mac
927  * address. To remove an interface from a the Layer 2 bridge domain,
928  * put the interface in a different mode, for example Layer 3 mode.
929  *
930  * Optionally, an interface can be added to a Layer 2 bridge-domain as
931  * a Bridged Virtual Interface (bvi). Only one interface in a Layer 2
932  * bridge-domain can be a bvi.
933  *
934  * Optionally, a split-horizon group can also be specified. This defaults
935  * to 0 if not specified.
936  *
937  * @cliexpar
938  * Example of how to configure a Layer 2 bridge-domain with three
939  * interfaces (where 200 is the bridge-domain-id):
940  * @cliexcmd{set interface l2 bridge GigabitEthernet0/8/0.200 200}
941  * This interface is added a BVI interface:
942  * @cliexcmd{set interface l2 bridge GigabitEthernet0/9/0.200 200 bvi}
943  * This interface also has a split-horizon group of 1 specified:
944  * @cliexcmd{set interface l2 bridge GigabitEthernet0/a/0.200 200 1}
945  * Example of how to remove an interface from a Layer2 bridge-domain:
946  * @cliexcmd{set interface l3 GigabitEthernet0/a/0.200}
947 ?*/
948 /* *INDENT-OFF* */
949 VLIB_CLI_COMMAND (int_l2_bridge_cli, static) = {
950   .path = "set interface l2 bridge",
951   .short_help = "set interface l2 bridge <interface> <bridge-domain-id> [bvi|uu-fwd] [shg]",
952   .function = int_l2_bridge,
953 };
954 /* *INDENT-ON* */
955
956 /**
957  * Set subinterface in xconnect mode with another interface.
958  * The CLI format is:
959  *   set interface l2 xconnect <interface> <peer interface>
960  */
961 static clib_error_t *
962 int_l2_xc (vlib_main_t * vm,
963            unformat_input_t * input, vlib_cli_command_t * cmd)
964 {
965   vnet_main_t *vnm = vnet_get_main ();
966   clib_error_t *error = 0;
967   u32 sw_if_index;
968   u32 xc_sw_if_index;
969
970   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
971     {
972       error = clib_error_return (0, "unknown interface `%U'",
973                                  format_unformat_error, input);
974       goto done;
975     }
976
977   if (!unformat_user
978       (input, unformat_vnet_sw_interface, vnm, &xc_sw_if_index))
979     {
980       error = clib_error_return (0, "unknown peer interface `%U'",
981                                  format_unformat_error, input);
982       goto done;
983     }
984
985   /* set the interface mode */
986   if (set_int_l2_mode
987       (vm, vnm, MODE_L2_XC, sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL,
988        0, xc_sw_if_index))
989     {
990       error = clib_error_return (0, "invalid configuration for interface",
991                                  format_unformat_error, input);
992       goto done;
993     }
994
995 done:
996   return error;
997 }
998
999 /*?
1000  * Use this command put an interface into Layer 2 cross-connect mode.
1001  * Both interfaces must be in this mode for bi-directional traffic. All
1002  * packets received on one interface will be transmitted to the other.
1003  * To remove the Layer 2 cross-connect, put the interface in a different
1004  * mode, for example Layer 3 mode.
1005  *
1006  * @cliexpar
1007  * Example of how to configure a Layer2 cross-connect between two interfaces:
1008  * @cliexcmd{set interface l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300}
1009  * @cliexcmd{set interface l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300}
1010  * Example of how to remove a Layer2 cross-connect:
1011  * @cliexcmd{set interface l3 GigabitEthernet0/8/0.300}
1012  * @cliexcmd{set interface l3 GigabitEthernet0/9/0.300}
1013 ?*/
1014 /* *INDENT-OFF* */
1015 VLIB_CLI_COMMAND (int_l2_xc_cli, static) = {
1016   .path = "set interface l2 xconnect",
1017   .short_help = "set interface l2 xconnect <interface> <peer interface>",
1018   .function = int_l2_xc,
1019 };
1020 /* *INDENT-ON* */
1021
1022 /**
1023  * Set subinterface in L3 mode.
1024  * The CLI format is:
1025  *   set interface l3 <interface>
1026  */
1027 static clib_error_t *
1028 int_l3 (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1029 {
1030   vnet_main_t *vnm = vnet_get_main ();
1031   clib_error_t *error = 0;
1032   u32 sw_if_index;
1033
1034   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1035     {
1036       error = clib_error_return (0, "unknown interface `%U'",
1037                                  format_unformat_error, input);
1038       goto done;
1039     }
1040
1041   /* set the interface mode */
1042   if (set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0,
1043                        L2_BD_PORT_TYPE_NORMAL, 0, 0))
1044     {
1045       error = clib_error_return (0, "invalid configuration for interface",
1046                                  format_unformat_error, input);
1047       goto done;
1048     }
1049
1050 done:
1051   return error;
1052 }
1053
1054 /*?
1055  * Modify the packet processing mode of the interface to Layer 3, which
1056  * implies packets will be routed. This is the default mode of an interface.
1057  * Use this command to remove an interface from a Layer 2 cross-connect or a
1058  * Layer 2 bridge.
1059  *
1060  * @cliexpar
1061  * Example of how to set the mode of an interface to Layer 3:
1062  * @cliexcmd{set interface l3 GigabitEthernet0/8/0.200}
1063 ?*/
1064 /* *INDENT-OFF* */
1065      VLIB_CLI_COMMAND (int_l3_cli, static) = {
1066   .path = "set interface l3",
1067   .short_help = "set interface l3 <interface>",
1068   .function = int_l3,
1069 };
1070 /* *INDENT-ON* */
1071
1072 /**
1073  * Show interface mode.
1074  * The CLI format is:
1075  *    show mode [<if-name1> <if-name2> ...]
1076  */
1077 static clib_error_t *
1078 show_int_mode (vlib_main_t * vm,
1079                unformat_input_t * input, vlib_cli_command_t * cmd)
1080 {
1081   vnet_main_t *vnm = vnet_get_main ();
1082   clib_error_t *error = 0;
1083   char *mode;
1084   u8 *args;
1085   vnet_interface_main_t *im = &vnm->interface_main;
1086
1087   vnet_sw_interface_t *si, *sis = 0;
1088   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1089     {
1090       u32 sw_if_index;
1091
1092       /* See if user wants to show specific interface */
1093       if (unformat
1094           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1095         {
1096           si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1097           vec_add1 (sis, si[0]);
1098         }
1099       else
1100         {
1101           error = clib_error_return (0, "unknown input `%U'",
1102                                      format_unformat_error, input);
1103           goto done;
1104         }
1105     }
1106
1107   if (vec_len (sis) == 0)       /* Get all interfaces */
1108     {
1109       /* Gather interfaces. */
1110       sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1111       _vec_len (sis) = 0;
1112       /* *INDENT-OFF* */
1113       pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sis, si[0]); }));
1114       /* *INDENT-ON* */
1115     }
1116
1117   vec_foreach (si, sis)
1118   {
1119     l2_input_config_t *config = l2input_intf_config (si->sw_if_index);
1120     if (config->bridge)
1121       {
1122         u32 bd_id;
1123         mode = "l2 bridge";
1124         bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
1125
1126         args = format (0, "bd_id %d%s%d", bd_id,
1127                        config->bvi ? " bvi shg " : " shg ", config->shg);
1128       }
1129     else if (config->xconnect)
1130       {
1131         mode = "l2 xconnect";
1132         args = format (0, "%U",
1133                        format_vnet_sw_if_index_name,
1134                        vnm, config->output_sw_if_index);
1135       }
1136     else
1137       {
1138         mode = "l3";
1139         args = format (0, " ");
1140       }
1141     vlib_cli_output (vm, "%s %U %v\n",
1142                      mode,
1143                      format_vnet_sw_if_index_name,
1144                      vnm, si->sw_if_index, args);
1145     vec_free (args);
1146   }
1147
1148 done:
1149   vec_free (sis);
1150
1151   return error;
1152 }
1153
1154 /*?
1155  * Show the packet processing mode (Layer2 cross-connect, Layer 2 bridge,
1156  * Layer 3 routed) of all interfaces and sub-interfaces, or limit the
1157  * output to just the provided list of interfaces and sub-interfaces.
1158  * The output shows the mode, the interface, and if the interface is
1159  * a member of a bridge, the bridge-domain-id and the split horizon group (shg).
1160  *
1161  * @cliexpar
1162  * Example of displaying the mode of all interfaces:
1163  * @cliexstart{show mode}
1164  * l3 local0
1165  * l3 GigabitEthernet0/8/0
1166  * l3 GigabitEthernet0/9/0
1167  * l3 GigabitEthernet0/a/0
1168  * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1169  * l2 bridge GigabitEthernet0/9/0.200 bd_id 200 shg 0
1170  * l2 bridge GigabitEthernet0/a/0.200 bd_id 200 shg 0
1171  * l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300
1172  * l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300
1173  * @cliexend
1174  * Example of displaying the mode of a selected list of interfaces:
1175  * @cliexstart{show mode GigabitEthernet0/8/0 GigabitEthernet0/8/0.200}
1176  * l3 GigabitEthernet0/8/0
1177  * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1178  * @cliexend
1179 ?*/
1180 /* *INDENT-OFF* */
1181 VLIB_CLI_COMMAND (show_l2_mode, static) = {
1182   .path = "show mode",
1183   .short_help = "show mode [<if-name1> <if-name2> ...]",
1184   .function = show_int_mode,
1185 };
1186 /* *INDENT-ON* */
1187
1188 #define foreach_l2_init_function                \
1189 _(feat_bitmap_drop_init)                        \
1190 _(l2fib_init)                                   \
1191 _(l2_input_classify_init)                             \
1192 _(l2bd_init)                                    \
1193 _(l2fwd_init)                                   \
1194 _(l2_in_out_acl_init)                           \
1195 _(l2input_init)                                 \
1196 _(l2_vtr_init)                                  \
1197 _(l2_invtr_init)                                \
1198 _(l2_efp_filter_init)                           \
1199 _(l2learn_init)                                 \
1200 _(l2flood_init)                                 \
1201 _(l2output_init)                                \
1202 _(l2_patch_init)                                \
1203 _(l2_xcrw_init)
1204
1205 #ifndef CLIB_MARCH_VARIANT
1206 clib_error_t *
1207 l2_init (vlib_main_t * vm)
1208 {
1209   clib_error_t *error;
1210
1211 #define _(a) do {                                                       \
1212   if ((error = vlib_call_init_function (vm, a))) return error; }        \
1213 while (0);
1214   foreach_l2_init_function;
1215 #undef _
1216   return 0;
1217 }
1218
1219 VLIB_INIT_FUNCTION (l2_init);
1220 #endif /* CLIB_MARCH_VARIANT */
1221
1222 /*
1223  * fd.io coding-style-patch-verification: ON
1224  *
1225  * Local Variables:
1226  * eval: (c-set-style "gnu")
1227  * End:
1228  */