interface: improve MTU handling
[vpp.git] / src / vnet / ethernet / interface.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ethernet_interface.c: ethernet interfaces
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
43 #include <vnet/ethernet/ethernet.h>
44 //#include <vnet/ethernet/arp.h>
45 #include <vnet/l2/l2_input.h>
46 #include <vnet/l2/l2_bd.h>
47 #include <vnet/adj/adj.h>
48 #include <vnet/adj/adj_mcast.h>
49 #include <vnet/ip-neighbor/ip_neighbor.h>
50
51 /**
52  * @file
53  * @brief Loopback Interfaces.
54  *
55  * This file contains code to manage loopback interfaces.
56  */
57
58 static const u8 *
59 ethernet_ip4_mcast_dst_addr (void)
60 {
61   const static u8 ethernet_mcast_dst_mac[] = {
62     0x1, 0x0, 0x5e, 0x0, 0x0, 0x0,
63   };
64
65   return (ethernet_mcast_dst_mac);
66 }
67
68 static const u8 *
69 ethernet_ip6_mcast_dst_addr (void)
70 {
71   const static u8 ethernet_mcast_dst_mac[] = {
72     0x33, 0x33, 0x00, 0x0, 0x0, 0x0,
73   };
74
75   return (ethernet_mcast_dst_mac);
76 }
77
78 /**
79  * @brief build a rewrite string to use for sending packets of type 'link_type'
80  * to 'dst_address'
81  */
82 u8 *
83 ethernet_build_rewrite (vnet_main_t * vnm,
84                         u32 sw_if_index,
85                         vnet_link_t link_type, const void *dst_address)
86 {
87   vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
88   vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
89   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
90   ethernet_main_t *em = &ethernet_main;
91   ethernet_interface_t *ei;
92   ethernet_header_t *h;
93   ethernet_type_t type;
94   uword n_bytes = sizeof (h[0]);
95   u8 *rewrite = NULL;
96   u8 is_p2p = 0;
97
98   if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) ||
99       (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE))
100     is_p2p = 1;
101   if (sub_sw != sup_sw)
102     {
103       if (sub_sw->sub.eth.flags.one_tag)
104         {
105           n_bytes += sizeof (ethernet_vlan_header_t);
106         }
107       else if (sub_sw->sub.eth.flags.two_tags)
108         {
109           n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
110         }
111       else if (PREDICT_FALSE (is_p2p))
112         {
113           n_bytes = sizeof (ethernet_header_t);
114         }
115       if (PREDICT_FALSE (!is_p2p))
116         {
117           // Check for encaps that are not supported for L3 interfaces
118           if (!(sub_sw->sub.eth.flags.exact_match) ||
119               (sub_sw->sub.eth.flags.default_sub) ||
120               (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
121               (sub_sw->sub.eth.flags.inner_vlan_id_any))
122             {
123               return 0;
124             }
125         }
126       else
127         {
128           n_bytes = sizeof (ethernet_header_t);
129         }
130     }
131
132   switch (link_type)
133     {
134 #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
135       _(IP4, IP4);
136       _(IP6, IP6);
137       _(MPLS, MPLS);
138       _(ARP, ARP);
139 #undef _
140     default:
141       return NULL;
142     }
143
144   vec_validate (rewrite, n_bytes - 1);
145   h = (ethernet_header_t *) rewrite;
146   ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
147   clib_memcpy (h->src_address, &ei->address, sizeof (h->src_address));
148   if (is_p2p)
149     {
150       clib_memcpy (h->dst_address, sub_sw->p2p.client_mac,
151                    sizeof (h->dst_address));
152     }
153   else
154     {
155       if (dst_address)
156         clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
157       else
158         clib_memset (h->dst_address, ~0, sizeof (h->dst_address));      /* broadcast */
159     }
160
161   if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag)
162     {
163       ethernet_vlan_header_t *outer = (void *) (h + 1);
164
165       h->type = sub_sw->sub.eth.flags.dot1ad ?
166         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
167         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
168       outer->priority_cfi_and_id =
169         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
170       outer->type = clib_host_to_net_u16 (type);
171
172     }
173   else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags)
174     {
175       ethernet_vlan_header_t *outer = (void *) (h + 1);
176       ethernet_vlan_header_t *inner = (void *) (outer + 1);
177
178       h->type = sub_sw->sub.eth.flags.dot1ad ?
179         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
180         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
181       outer->priority_cfi_and_id =
182         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
183       outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
184       inner->priority_cfi_and_id =
185         clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
186       inner->type = clib_host_to_net_u16 (type);
187
188     }
189   else
190     {
191       h->type = clib_host_to_net_u16 (type);
192     }
193
194   return (rewrite);
195 }
196
197 void
198 ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
199 {
200   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
201
202   if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) ||
203       (si->type == VNET_SW_INTERFACE_TYPE_PIPE))
204     {
205       default_update_adjacency (vnm, sw_if_index, ai);
206     }
207   else
208     {
209       ip_adjacency_t *adj;
210
211       adj = adj_get (ai);
212
213       switch (adj->lookup_next_index)
214         {
215         case IP_LOOKUP_NEXT_GLEAN:
216           adj_glean_update_rewrite (ai);
217           break;
218         case IP_LOOKUP_NEXT_ARP:
219         case IP_LOOKUP_NEXT_REWRITE:
220           ip_neighbor_update (vnm, ai);
221           break;
222         case IP_LOOKUP_NEXT_BCAST:
223           adj_nbr_update_rewrite (ai,
224                                   ADJ_NBR_REWRITE_FLAG_COMPLETE,
225                                   ethernet_build_rewrite
226                                   (vnm,
227                                    adj->rewrite_header.sw_if_index,
228                                    adj->ia_link,
229                                    VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
230           break;
231         case IP_LOOKUP_NEXT_MCAST:
232           {
233             /*
234              * Construct a partial rewrite from the known ethernet mcast dest MAC
235              */
236             u8 *rewrite;
237             u8 offset;
238
239             rewrite = ethernet_build_rewrite
240               (vnm,
241                sw_if_index,
242                adj->ia_link,
243                (adj->ia_nh_proto == FIB_PROTOCOL_IP6 ?
244                 ethernet_ip6_mcast_dst_addr () :
245                 ethernet_ip4_mcast_dst_addr ()));
246
247             /*
248              * Complete the remaining fields of the adj's rewrite to direct the
249              * complete of the rewrite at switch time by copying in the IP
250              * dst address's bytes.
251              * Ofset is 2 bytes into the destintation address.
252              */
253             offset = vec_len (rewrite) - 2;
254             adj_mcast_update_rewrite (ai, rewrite, offset);
255
256             break;
257           }
258         case IP_LOOKUP_NEXT_DROP:
259         case IP_LOOKUP_NEXT_PUNT:
260         case IP_LOOKUP_NEXT_LOCAL:
261         case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
262         case IP_LOOKUP_NEXT_MIDCHAIN:
263         case IP_LOOKUP_NEXT_ICMP_ERROR:
264         case IP_LOOKUP_N_NEXT:
265           ASSERT (0);
266           break;
267         }
268     }
269 }
270
271 static void
272 ethernet_interface_address_copy (ethernet_interface_address_t * dst,
273                                  const u8 * mac)
274 {
275   clib_memcpy (&dst->mac, (u8 *) mac, sizeof (dst->mac));
276   /*
277    * ethernet dataplane loads mac as u64, makes sure the last 2 bytes are 0
278    * for comparison purpose
279    */
280   dst->zero = 0;
281 }
282
283 static void
284 ethernet_set_mac (vnet_hw_interface_t * hi, ethernet_interface_t * ei,
285                   const u8 * mac_address)
286 {
287   vec_validate (hi->hw_address, sizeof (mac_address_t) - 1);
288   clib_memcpy (hi->hw_address, mac_address, sizeof (mac_address_t));
289   ethernet_interface_address_copy (&ei->address, mac_address);
290 }
291
292 static clib_error_t *
293 ethernet_mac_change (vnet_hw_interface_t * hi,
294                      const u8 * old_address, const u8 * mac_address)
295 {
296   ethernet_interface_t *ei;
297   ethernet_main_t *em;
298
299   em = &ethernet_main;
300   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
301
302   ethernet_set_mac (hi, ei, mac_address);
303
304   {
305     ethernet_address_change_ctx_t *cb;
306     vec_foreach (cb, em->address_change_callbacks)
307       cb->function (em, hi->sw_if_index, cb->function_opaque);
308   }
309
310   return (NULL);
311 }
312
313 static clib_error_t *
314 ethernet_set_mtu (vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 mtu)
315 {
316   ethernet_interface_t *ei =
317     pool_elt_at_index (ethernet_main.interfaces, hi->hw_instance);
318
319   if (ei->cb.set_mtu)
320     return ei->cb.set_mtu (vnm, hi, mtu);
321
322   return 0;
323 }
324
325 /* *INDENT-OFF* */
326 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
327   .name = "Ethernet",
328   .tx_hash_fn_type = VNET_HASH_FN_TYPE_ETHERNET,
329   .format_address = format_ethernet_address,
330   .format_header = format_ethernet_header_with_length,
331   .unformat_hw_address = unformat_ethernet_address,
332   .unformat_header = unformat_ethernet_header,
333   .build_rewrite = ethernet_build_rewrite,
334   .update_adjacency = ethernet_update_adjacency,
335   .mac_addr_change_function = ethernet_mac_change,
336   .set_mtu = ethernet_set_mtu,
337 };
338 /* *INDENT-ON* */
339
340 uword
341 unformat_ethernet_interface (unformat_input_t * input, va_list * args)
342 {
343   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
344   u32 *result = va_arg (*args, u32 *);
345   u32 hw_if_index;
346   ethernet_main_t *em = &ethernet_main;
347   ethernet_interface_t *eif;
348
349   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
350     return 0;
351
352   eif = ethernet_get_interface (em, hw_if_index);
353   if (eif)
354     {
355       *result = hw_if_index;
356       return 1;
357     }
358   return 0;
359 }
360
361 u32
362 vnet_eth_register_interface (vnet_main_t *vnm,
363                              vnet_eth_interface_registration_t *r)
364 {
365   ethernet_main_t *em = &ethernet_main;
366   ethernet_interface_t *ei;
367   vnet_hw_interface_t *hi;
368   u32 hw_if_index;
369
370   pool_get (em->interfaces, ei);
371   clib_memcpy (&ei->cb, &r->cb, sizeof (vnet_eth_if_callbacks_t));
372
373   hw_if_index = vnet_register_interface (
374     vnm, r->dev_class_index, r->dev_instance,
375     ethernet_hw_interface_class.index, ei - em->interfaces);
376
377   hi = vnet_get_hw_interface (vnm, hw_if_index);
378
379   ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
380
381   hi->min_packet_bytes = hi->min_supported_packet_bytes =
382     ETHERNET_MIN_PACKET_BYTES;
383   hi->max_supported_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
384   hi->max_packet_bytes = em->default_mtu;
385
386   /* Default ethernet MTU, 9000 unless set by ethernet_config see below */
387   vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, em->default_mtu);
388
389   ethernet_set_mac (hi, ei, r->address);
390   return hw_if_index;
391 }
392
393 void
394 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
395 {
396   ethernet_main_t *em = &ethernet_main;
397   ethernet_interface_t *ei;
398   vnet_hw_interface_t *hi;
399   main_intf_t *main_intf;
400   vlan_table_t *vlan_table;
401   u32 idx;
402
403   hi = vnet_get_hw_interface (vnm, hw_if_index);
404   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
405
406   /* Delete vlan mapping table for dot1q and dot1ad. */
407   main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
408   if (main_intf->dot1q_vlans)
409     {
410       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
411       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
412         {
413           if (vlan_table->vlans[idx].qinqs)
414             {
415               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
416               vlan_table->vlans[idx].qinqs = 0;
417             }
418         }
419       pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
420       main_intf->dot1q_vlans = 0;
421     }
422   if (main_intf->dot1ad_vlans)
423     {
424       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
425       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
426         {
427           if (vlan_table->vlans[idx].qinqs)
428             {
429               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
430               vlan_table->vlans[idx].qinqs = 0;
431             }
432         }
433       pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
434       main_intf->dot1ad_vlans = 0;
435     }
436
437   vnet_delete_hw_interface (vnm, hw_if_index);
438   pool_put (em->interfaces, ei);
439 }
440
441 u32
442 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
443 {
444   ethernet_main_t *em = &ethernet_main;
445   vnet_hw_interface_t *hi;
446   ethernet_interface_t *ei;
447   u32 opn_flags = flags & ETHERNET_INTERFACE_FLAGS_SET_OPN_MASK;
448
449   hi = vnet_get_hw_interface (vnm, hw_if_index);
450
451   ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
452
453   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
454
455   /* preserve status bits and update last set operation bits */
456   ei->flags = (ei->flags & ETHERNET_INTERFACE_FLAGS_STATUS_MASK) | opn_flags;
457
458   if (ei->cb.flag_change)
459     {
460       switch (opn_flags)
461         {
462         case ETHERNET_INTERFACE_FLAG_DEFAULT_L3:
463           if (hi->caps & VNET_HW_IF_CAP_MAC_FILTER)
464             {
465               if (ei->cb.flag_change (vnm, hi, opn_flags) != ~0)
466                 {
467                   ei->flags |= ETHERNET_INTERFACE_FLAG_STATUS_L3;
468                   return 0;
469                 }
470               ei->flags &= ~ETHERNET_INTERFACE_FLAG_STATUS_L3;
471               return ~0;
472             }
473           /* fall through */
474         case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
475           ei->flags &= ~ETHERNET_INTERFACE_FLAG_STATUS_L3;
476           return ei->cb.flag_change (vnm, hi, opn_flags);
477         default:
478           return ~0;
479         }
480     }
481   return ~0;
482 }
483
484 /**
485  * Echo packets back to ethernet/l2-input.
486  */
487 static uword
488 simulated_ethernet_interface_tx (vlib_main_t * vm,
489                                  vlib_node_runtime_t *
490                                  node, vlib_frame_t * frame)
491 {
492   u32 n_left_from, *from;
493   u32 next_index = 0;
494   u32 n_bytes;
495   u32 thread_index = vm->thread_index;
496   vnet_main_t *vnm = vnet_get_main ();
497   vnet_interface_main_t *im = &vnm->interface_main;
498   l2_input_config_t *config;
499   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
500   u16 nexts[VLIB_FRAME_SIZE], *next;
501   u32 new_rx_sw_if_index = ~0;
502   u32 new_tx_sw_if_index = ~0;
503
504   n_left_from = frame->n_vectors;
505   from = vlib_frame_vector_args (frame);
506
507   vlib_get_buffers (vm, from, bufs, n_left_from);
508   b = bufs;
509   next = nexts;
510
511   /* Ordinarily, this is the only config lookup. */
512   config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
513   next_index = (l2_input_is_bridge (config) ?
514                 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
515                 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
516   new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
517   new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
518
519   while (n_left_from >= 4)
520     {
521       u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
522       u32 not_all_match_config;
523
524       /* Prefetch next iteration. */
525       if (PREDICT_TRUE (n_left_from >= 8))
526         {
527           vlib_prefetch_buffer_header (b[4], STORE);
528           vlib_prefetch_buffer_header (b[5], STORE);
529           vlib_prefetch_buffer_header (b[6], STORE);
530           vlib_prefetch_buffer_header (b[7], STORE);
531         }
532
533       /* Make sure all pkts were transmitted on the same (loop) intfc */
534       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
535       sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
536       sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
537       sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
538
539       not_all_match_config = (sw_if_index0 ^ sw_if_index1)
540         ^ (sw_if_index2 ^ sw_if_index3);
541       not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index;
542
543       /* Speed path / expected case: all pkts on the same intfc */
544       if (PREDICT_TRUE (not_all_match_config == 0))
545         {
546           next[0] = next_index;
547           next[1] = next_index;
548           next[2] = next_index;
549           next[3] = next_index;
550           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
551           vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
552           vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
553           vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
554           vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
555           vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
556           vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
557           vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
558           n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
559           n_bytes += vlib_buffer_length_in_chain (vm, b[1]);
560           n_bytes += vlib_buffer_length_in_chain (vm, b[2]);
561           n_bytes += vlib_buffer_length_in_chain (vm, b[3]);
562
563           if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
564             {
565               vnet_update_l2_len (b[0]);
566               vnet_update_l2_len (b[1]);
567               vnet_update_l2_len (b[2]);
568               vnet_update_l2_len (b[3]);
569             }
570
571           /* increment TX interface stat */
572           vlib_increment_combined_counter (im->combined_sw_if_counters +
573                                            VNET_INTERFACE_COUNTER_TX,
574                                            thread_index, new_rx_sw_if_index,
575                                            4 /* pkts */ , n_bytes);
576           b += 4;
577           next += 4;
578           n_left_from -= 4;
579           continue;
580         }
581
582       /*
583        * Slow path: we know that at least one of the pkts
584        * was transmitted on a different sw_if_index, so
585        * check each sw_if_index against the cached data and proceed
586        * accordingly.
587        *
588        * This shouldn't happen, but code can (and does) bypass the
589        * per-interface output node, so deal with it.
590        */
591       if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
592                          != new_rx_sw_if_index))
593         {
594           config = l2input_intf_config
595             (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
596           next_index = (l2_input_is_bridge (config) ?
597                         VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
598                         VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
599           new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
600           new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
601         }
602       next[0] = next_index;
603       vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
604       vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
605       n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
606       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
607         vnet_update_l2_len (b[0]);
608
609       vlib_increment_combined_counter (im->combined_sw_if_counters +
610                                        VNET_INTERFACE_COUNTER_TX,
611                                        thread_index, new_rx_sw_if_index,
612                                        1 /* pkts */ , n_bytes);
613
614       if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX]
615                          != new_rx_sw_if_index))
616         {
617           config = l2input_intf_config
618             (vnet_buffer (b[1])->sw_if_index[VLIB_TX]);
619           next_index = (l2_input_is_bridge (config) ?
620                         VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
621                         VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
622           new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
623           new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
624         }
625       next[1] = next_index;
626       vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
627       vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
628       n_bytes = vlib_buffer_length_in_chain (vm, b[1]);
629       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
630         vnet_update_l2_len (b[1]);
631
632       vlib_increment_combined_counter (im->combined_sw_if_counters +
633                                        VNET_INTERFACE_COUNTER_TX,
634                                        thread_index, new_rx_sw_if_index,
635                                        1 /* pkts */ , n_bytes);
636
637       if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX]
638                          != new_rx_sw_if_index))
639         {
640           config = l2input_intf_config
641             (vnet_buffer (b[2])->sw_if_index[VLIB_TX]);
642           next_index = (l2_input_is_bridge (config) ?
643                         VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
644                         VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
645           new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
646           new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
647         }
648       next[2] = next_index;
649       vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
650       vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
651       n_bytes = vlib_buffer_length_in_chain (vm, b[2]);
652       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
653         vnet_update_l2_len (b[2]);
654
655       vlib_increment_combined_counter (im->combined_sw_if_counters +
656                                        VNET_INTERFACE_COUNTER_TX,
657                                        thread_index, new_rx_sw_if_index,
658                                        1 /* pkts */ , n_bytes);
659
660       if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX]
661                          != new_rx_sw_if_index))
662         {
663           config = l2input_intf_config
664             (vnet_buffer (b[3])->sw_if_index[VLIB_TX]);
665           next_index = (l2_input_is_bridge (config) ?
666                         VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
667                         VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
668           new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
669           new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
670         }
671       next[3] = next_index;
672       vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
673       vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
674       n_bytes = vlib_buffer_length_in_chain (vm, b[3]);
675       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
676         vnet_update_l2_len (b[3]);
677
678       vlib_increment_combined_counter (im->combined_sw_if_counters +
679                                        VNET_INTERFACE_COUNTER_TX,
680                                        thread_index, new_rx_sw_if_index,
681                                        1 /* pkts */ , n_bytes);
682       b += 4;
683       next += 4;
684       n_left_from -= 4;
685     }
686   while (n_left_from > 0)
687     {
688       if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
689                          != new_rx_sw_if_index))
690         {
691           config = l2input_intf_config
692             (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
693           next_index = (l2_input_is_bridge (config) ?
694                         VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
695                         VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
696           new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
697           new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
698         }
699       next[0] = next_index;
700       vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
701       vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
702       n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
703       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
704         vnet_update_l2_len (b[0]);
705
706       vlib_increment_combined_counter (im->combined_sw_if_counters +
707                                        VNET_INTERFACE_COUNTER_TX,
708                                        thread_index, new_rx_sw_if_index,
709                                        1 /* pkts */ , n_bytes);
710       b += 1;
711       next += 1;
712       n_left_from -= 1;
713     }
714
715   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
716
717   return frame->n_vectors;
718 }
719
720 static u8 *
721 format_simulated_ethernet_name (u8 * s, va_list * args)
722 {
723   u32 dev_instance = va_arg (*args, u32);
724   return format (s, "loop%d", dev_instance);
725 }
726
727 static clib_error_t *
728 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
729                                   u32 flags)
730 {
731   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
732     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
733   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
734   return 0;
735 }
736
737 static clib_error_t *
738 simulated_ethernet_mac_change (vnet_hw_interface_t * hi,
739                                const u8 * old_address, const u8 * mac_address)
740 {
741   l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address);
742
743   return (NULL);
744 }
745
746
747 /* *INDENT-OFF* */
748 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
749   .name = "Loopback",
750   .format_device_name = format_simulated_ethernet_name,
751   .tx_function = simulated_ethernet_interface_tx,
752   .admin_up_down_function = simulated_ethernet_admin_up_down,
753   .mac_addr_change_function = simulated_ethernet_mac_change,
754 };
755 /* *INDENT-ON* */
756
757 /*
758  * Maintain a bitmap of allocated loopback instance numbers.
759  */
760 #define LOOPBACK_MAX_INSTANCE           (16 * 1024)
761
762 static u32
763 loopback_instance_alloc (u8 is_specified, u32 want)
764 {
765   ethernet_main_t *em = &ethernet_main;
766
767   /*
768    * Check for dynamically allocaetd instance number.
769    */
770   if (!is_specified)
771     {
772       u32 bit;
773
774       bit = clib_bitmap_first_clear (em->bm_loopback_instances);
775       if (bit >= LOOPBACK_MAX_INSTANCE)
776         {
777           return ~0;
778         }
779       em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
780                                                    bit, 1);
781       return bit;
782     }
783
784   /*
785    * In range?
786    */
787   if (want >= LOOPBACK_MAX_INSTANCE)
788     {
789       return ~0;
790     }
791
792   /*
793    * Already in use?
794    */
795   if (clib_bitmap_get (em->bm_loopback_instances, want))
796     {
797       return ~0;
798     }
799
800   /*
801    * Grant allocation request.
802    */
803   em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
804                                                want, 1);
805
806   return want;
807 }
808
809 static int
810 loopback_instance_free (u32 instance)
811 {
812   ethernet_main_t *em = &ethernet_main;
813
814   if (instance >= LOOPBACK_MAX_INSTANCE)
815     {
816       return -1;
817     }
818
819   if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0)
820     {
821       return -1;
822     }
823
824   em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
825                                                instance, 0);
826   return 0;
827 }
828
829 int
830 vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
831                                 u8 is_specified, u32 user_instance)
832 {
833   vnet_main_t *vnm = vnet_get_main ();
834   vlib_main_t *vm = vlib_get_main ();
835   u32 instance;
836   u8 address[6];
837   u32 hw_if_index;
838   vnet_hw_interface_t *hw_if;
839   u32 slot;
840
841   ASSERT (sw_if_indexp);
842
843   *sw_if_indexp = (u32) ~ 0;
844
845   clib_memset (address, 0, sizeof (address));
846
847   /*
848    * Allocate a loopback instance.  Either select on dynamically
849    * or try to use the desired user_instance number.
850    */
851   instance = loopback_instance_alloc (is_specified, user_instance);
852   if (instance == ~0)
853     {
854       return VNET_API_ERROR_INVALID_REGISTRATION;
855     }
856
857   /*
858    * Default MAC address (dead:0000:0000 + instance) is allocated
859    * if zero mac_address is configured. Otherwise, user-configurable MAC
860    * address is programmed on the loopback interface.
861    */
862   if (memcmp (address, mac_address, sizeof (address)))
863     clib_memcpy (address, mac_address, sizeof (address));
864   else
865     {
866       address[0] = 0xde;
867       address[1] = 0xad;
868       address[5] = instance;
869     }
870
871   vnet_eth_interface_registration_t eir = {};
872   eir.dev_class_index = ethernet_simulated_device_class.index;
873   eir.dev_instance = instance;
874   eir.address = address;
875   hw_if_index = vnet_eth_register_interface (vnm, &eir);
876   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
877   slot = vlib_node_add_named_next_with_slot
878     (vm, hw_if->tx_node_index,
879      "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
880   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
881
882   slot = vlib_node_add_named_next_with_slot
883     (vm, hw_if->tx_node_index,
884      "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
885   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
886
887   {
888     vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
889     *sw_if_indexp = si->sw_if_index;
890
891     /* By default don't flood to loopbacks, as packets just keep
892      * coming back ... If this loopback becomes a BVI, we'll change it */
893     si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
894   }
895
896   return 0;
897 }
898
899 static clib_error_t *
900 create_simulated_ethernet_interfaces (vlib_main_t * vm,
901                                       unformat_input_t * input,
902                                       vlib_cli_command_t * cmd)
903 {
904   int rv;
905   u32 sw_if_index;
906   u8 mac_address[6];
907   u8 is_specified = 0;
908   u32 user_instance = 0;
909
910   clib_memset (mac_address, 0, sizeof (mac_address));
911
912   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
913     {
914       if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
915         ;
916       if (unformat (input, "instance %d", &user_instance))
917         is_specified = 1;
918       else
919         break;
920     }
921
922   rv = vnet_create_loopback_interface (&sw_if_index, mac_address,
923                                        is_specified, user_instance);
924
925   if (rv)
926     return clib_error_return (0, "vnet_create_loopback_interface failed");
927
928   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
929                    sw_if_index);
930   return 0;
931 }
932
933 /*?
934  * Create a loopback interface. Optionally, a MAC Address can be
935  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
936  *
937  * @cliexpar
938  * The following two command syntaxes are equivalent:
939  * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
940  * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
941  * Example of how to create a loopback interface:
942  * @cliexcmd{loopback create-interface}
943 ?*/
944 /* *INDENT-OFF* */
945 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
946   .path = "loopback create-interface",
947   .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]",
948   .function = create_simulated_ethernet_interfaces,
949 };
950 /* *INDENT-ON* */
951
952 /*?
953  * Create a loopback interface. Optionally, a MAC Address can be
954  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
955  *
956  * @cliexpar
957  * The following two command syntaxes are equivalent:
958  * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
959  * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
960  * Example of how to create a loopback interface:
961  * @cliexcmd{create loopback interface}
962 ?*/
963 /* *INDENT-OFF* */
964 VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
965   .path = "create loopback interface",
966   .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]",
967   .function = create_simulated_ethernet_interfaces,
968 };
969 /* *INDENT-ON* */
970
971 ethernet_interface_t *
972 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
973 {
974   vnet_hw_interface_t *i =
975     vnet_get_hw_interface (vnet_get_main (), hw_if_index);
976   return (i->hw_class_index ==
977           ethernet_hw_interface_class.
978           index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
979 }
980
981 mac_address_t *
982 ethernet_interface_add_del_address (ethernet_main_t * em,
983                                     u32 hw_if_index, const u8 * address,
984                                     u8 is_add)
985 {
986   ethernet_interface_t *ei = ethernet_get_interface (em, hw_if_index);
987   ethernet_interface_address_t *if_addr = 0;
988   int found = 0;
989
990   /* return if there is not an ethernet interface for this hw interface */
991   if (!ei)
992     return 0;
993
994   /* determine whether the address is configured on the interface */
995   vec_foreach (if_addr, ei->secondary_addrs)
996   {
997     if (ethernet_mac_address_equal (if_addr->mac.bytes, address))
998       {
999         found = 1;
1000         break;
1001       }
1002   }
1003
1004   if (is_add)
1005     {
1006       if (!found)
1007         {
1008           /* address not found yet: add it */
1009           vec_add2 (ei->secondary_addrs, if_addr, 1);
1010           ethernet_interface_address_copy (if_addr, address);
1011         }
1012       return &if_addr->mac;
1013     }
1014
1015   /* delete case */
1016   if (found)
1017     vec_delete (ei->secondary_addrs, 1, if_addr - ei->secondary_addrs);
1018
1019   return 0;
1020 }
1021
1022 int
1023 vnet_delete_loopback_interface (u32 sw_if_index)
1024 {
1025   vnet_main_t *vnm = vnet_get_main ();
1026
1027   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1028     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1029
1030   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1031   if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index)
1032     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1033
1034   if (loopback_instance_free (hw->dev_instance) < 0)
1035     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1036
1037   ethernet_delete_interface (vnm, hw->hw_if_index);
1038
1039   return 0;
1040 }
1041
1042 int
1043 vnet_create_sub_interface (u32 sw_if_index, u32 id,
1044                            u32 flags, u16 inner_vlan_id, u16 outer_vlan_id,
1045                            u32 * sub_sw_if_index)
1046 {
1047   vnet_main_t *vnm = vnet_get_main ();
1048   vnet_interface_main_t *im = &vnm->interface_main;
1049   vnet_hw_interface_t *hi;
1050   u64 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) id;
1051   vnet_sw_interface_t template;
1052   uword *p;
1053   u64 *kp;
1054
1055   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1056
1057   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1058   if (p)
1059     {
1060       return (VNET_API_ERROR_VLAN_ALREADY_EXISTS);
1061     }
1062
1063   clib_memset (&template, 0, sizeof (template));
1064   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1065   template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1066   template.sup_sw_if_index = sw_if_index;
1067   template.sub.id = id;
1068   template.sub.eth.raw_flags = flags;
1069   template.sub.eth.outer_vlan_id = outer_vlan_id;
1070   template.sub.eth.inner_vlan_id = inner_vlan_id;
1071
1072   if (vnet_create_sw_interface (vnm, &template, sub_sw_if_index))
1073     return (VNET_API_ERROR_UNSPECIFIED);
1074
1075   kp = clib_mem_alloc (sizeof (*kp));
1076   *kp = sup_and_sub_key;
1077
1078   hash_set (hi->sub_interface_sw_if_index_by_id, id, *sub_sw_if_index);
1079   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, *sub_sw_if_index);
1080
1081   return (0);
1082 }
1083
1084 int
1085 vnet_delete_sub_interface (u32 sw_if_index)
1086 {
1087   vnet_main_t *vnm = vnet_get_main ();
1088   vnet_sw_interface_t *si;
1089   int rv = 0;
1090
1091   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1092     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1093
1094   si = vnet_get_sw_interface (vnm, sw_if_index);
1095   if (si->type == VNET_SW_INTERFACE_TYPE_SUB ||
1096       si->type == VNET_SW_INTERFACE_TYPE_PIPE ||
1097       si->type == VNET_SW_INTERFACE_TYPE_P2P)
1098     {
1099       vnet_interface_main_t *im = &vnm->interface_main;
1100       vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1101       u64 sup_and_sub_key =
1102         ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
1103       hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1104       hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id);
1105       vnet_delete_sw_interface (vnm, sw_if_index);
1106     }
1107   else
1108     rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
1109
1110   return rv;
1111 }
1112
1113 static clib_error_t *
1114 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
1115                                       unformat_input_t * input,
1116                                       vlib_cli_command_t * cmd)
1117 {
1118   int rv;
1119   u32 sw_if_index = ~0;
1120   vnet_main_t *vnm = vnet_get_main ();
1121
1122   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1123     {
1124       if (unformat (input, "intfc %U",
1125                     unformat_vnet_sw_interface, vnm, &sw_if_index))
1126         ;
1127       else
1128         break;
1129     }
1130
1131   if (sw_if_index == ~0)
1132     return clib_error_return (0, "interface not specified");
1133
1134   rv = vnet_delete_loopback_interface (sw_if_index);
1135
1136   if (rv)
1137     return clib_error_return (0, "vnet_delete_loopback_interface failed");
1138
1139   return 0;
1140 }
1141
1142 static clib_error_t *
1143 delete_sub_interface (vlib_main_t * vm,
1144                       unformat_input_t * input, vlib_cli_command_t * cmd)
1145 {
1146   int rv = 0;
1147   u32 sw_if_index = ~0;
1148   vnet_main_t *vnm = vnet_get_main ();
1149
1150   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1151     {
1152       if (unformat
1153           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1154         ;
1155       else
1156         break;
1157     }
1158   if (sw_if_index == ~0)
1159     return clib_error_return (0, "interface doesn't exist");
1160
1161   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1162     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1163   else
1164     rv = vnet_delete_sub_interface (sw_if_index);
1165   if (rv)
1166     return clib_error_return (0, "delete_subinterface_interface failed");
1167   return 0;
1168 }
1169
1170 /*?
1171  * Delete a loopback interface.
1172  *
1173  * @cliexpar
1174  * The following two command syntaxes are equivalent:
1175  * @cliexcmd{loopback delete-interface intfc <interface>}
1176  * @cliexcmd{delete loopback interface intfc <interface>}
1177  * Example of how to delete a loopback interface:
1178  * @cliexcmd{loopback delete-interface intfc loop0}
1179 ?*/
1180 /* *INDENT-OFF* */
1181 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
1182   .path = "loopback delete-interface",
1183   .short_help = "loopback delete-interface intfc <interface>",
1184   .function = delete_simulated_ethernet_interfaces,
1185 };
1186 /* *INDENT-ON* */
1187
1188 /*?
1189  * Delete a loopback interface.
1190  *
1191  * @cliexpar
1192  * The following two command syntaxes are equivalent:
1193  * @cliexcmd{loopback delete-interface intfc <interface>}
1194  * @cliexcmd{delete loopback interface intfc <interface>}
1195  * Example of how to delete a loopback interface:
1196  * @cliexcmd{delete loopback interface intfc loop0}
1197 ?*/
1198 /* *INDENT-OFF* */
1199 VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
1200   .path = "delete loopback interface",
1201   .short_help = "delete loopback interface intfc <interface>",
1202   .function = delete_simulated_ethernet_interfaces,
1203 };
1204 /* *INDENT-ON* */
1205
1206 /*?
1207  * Delete a sub-interface.
1208  *
1209  * @cliexpar
1210  * Example of how to delete a sub-interface:
1211  * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
1212 ?*/
1213 /* *INDENT-OFF* */
1214 VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
1215   .path = "delete sub-interface",
1216   .short_help = "delete sub-interface <interface>",
1217   .function = delete_sub_interface,
1218 };
1219 /* *INDENT-ON* */
1220
1221 /* ethernet { ... } configuration. */
1222 /*?
1223  *
1224  * @cfgcmd{default-mtu &lt;n&gt;}
1225  * Specify the default mtu in the range of 64-9000. The default is 9000 bytes.
1226  *
1227  */
1228 static clib_error_t *
1229 ethernet_config (vlib_main_t * vm, unformat_input_t * input)
1230 {
1231   ethernet_main_t *em = &ethernet_main;
1232
1233   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1234     {
1235       if (unformat (input, "default-mtu %u", &em->default_mtu))
1236         {
1237           if (em->default_mtu < 64 || em->default_mtu > 9000)
1238             return clib_error_return (0, "default MTU must be >=64, <=9000");
1239         }
1240       else
1241         {
1242           return clib_error_return (0, "unknown input '%U'",
1243                                     format_unformat_error, input);
1244         }
1245     }
1246   return 0;
1247 }
1248
1249 VLIB_CONFIG_FUNCTION (ethernet_config, "ethernet");
1250
1251 /*
1252  * fd.io coding-style-patch-verification: ON
1253  *
1254  * Local Variables:
1255  * eval: (c-set-style "gnu")
1256  * End:
1257  */