78d89f7e2598dd64959cb10c3c403f715ae6ab61
[vpp.git] / vnet / 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/l2/l2_input.h>
45 #include <vnet/srp/srp.h>
46 #include <vnet/lisp-gpe/lisp_gpe.h>
47 #include <vnet/devices/af_packet/af_packet.h>
48
49 int
50 vnet_sw_interface_is_p2p (vnet_main_t * vnm, u32 sw_if_index)
51 {
52   // FIXME - use flags on the HW itf
53   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
54   return (!(hw->hw_class_index == ethernet_hw_interface_class.index ||
55             hw->hw_class_index == af_packet_device_class.index ||
56             hw->hw_class_index == lisp_gpe_hw_class.index ||
57             hw->hw_class_index == srp_hw_interface_class.index));
58 }
59
60 /**
61  * @file
62  * @brief Loopback Interfaces.
63  *
64  * This file contains code to manage loopback interfaces.
65  */
66
67 static uword
68 ethernet_set_rewrite (vnet_main_t * vnm,
69                       u32 sw_if_index,
70                       u32 l3_type,
71                       void *dst_address,
72                       void *rewrite, uword max_rewrite_bytes)
73 {
74   vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
75   vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
76   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
77   ethernet_main_t *em = &ethernet_main;
78   ethernet_interface_t *ei;
79   ethernet_header_t *h = rewrite;
80   ethernet_type_t type;
81   uword n_bytes = sizeof (h[0]);
82
83   if (sub_sw != sup_sw)
84     {
85       if (sub_sw->sub.eth.flags.one_tag)
86         {
87           n_bytes += sizeof (ethernet_vlan_header_t);
88         }
89       else if (sub_sw->sub.eth.flags.two_tags)
90         {
91           n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
92         }
93       // Check for encaps that are not supported for L3 interfaces
94       if (!(sub_sw->sub.eth.flags.exact_match) ||
95           (sub_sw->sub.eth.flags.default_sub) ||
96           (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
97           (sub_sw->sub.eth.flags.inner_vlan_id_any))
98         {
99           return 0;
100         }
101     }
102
103   if (n_bytes > max_rewrite_bytes)
104     return 0;
105
106   switch (l3_type)
107     {
108 #define _(a,b) case VNET_L3_PACKET_TYPE_##a: type = ETHERNET_TYPE_##b; break
109       _(IP4, IP4);
110       _(IP6, IP6);
111       _(MPLS_UNICAST, MPLS_UNICAST);
112       _(MPLS_MULTICAST, MPLS_MULTICAST);
113       _(ARP, ARP);
114 #undef _
115     default:
116       return 0;
117     }
118
119   ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
120   clib_memcpy (h->src_address, ei->address, sizeof (h->src_address));
121   if (dst_address)
122     clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
123   else
124     memset (h->dst_address, ~0, sizeof (h->dst_address));       /* broadcast */
125
126   if (sub_sw->sub.eth.flags.one_tag)
127     {
128       ethernet_vlan_header_t *outer = (void *) (h + 1);
129
130       h->type = sub_sw->sub.eth.flags.dot1ad ?
131         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
132         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
133       outer->priority_cfi_and_id =
134         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
135       outer->type = clib_host_to_net_u16 (type);
136
137     }
138   else if (sub_sw->sub.eth.flags.two_tags)
139     {
140       ethernet_vlan_header_t *outer = (void *) (h + 1);
141       ethernet_vlan_header_t *inner = (void *) (outer + 1);
142
143       h->type = sub_sw->sub.eth.flags.dot1ad ?
144         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
145         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
146       outer->priority_cfi_and_id =
147         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
148       outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
149       inner->priority_cfi_and_id =
150         clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
151       inner->type = clib_host_to_net_u16 (type);
152
153     }
154   else
155     {
156       h->type = clib_host_to_net_u16 (type);
157     }
158
159   return n_bytes;
160 }
161
162 /* *INDENT-OFF* */
163 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
164   .name = "Ethernet",
165   .format_address = format_ethernet_address,
166   .format_header = format_ethernet_header_with_length,
167   .unformat_hw_address = unformat_ethernet_address,
168   .unformat_header = unformat_ethernet_header,
169   .set_rewrite = ethernet_set_rewrite,
170 };
171 /* *INDENT-ON* */
172
173 uword
174 unformat_ethernet_interface (unformat_input_t * input, va_list * args)
175 {
176   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
177   u32 *result = va_arg (*args, u32 *);
178   u32 hw_if_index;
179   ethernet_main_t *em = &ethernet_main;
180   ethernet_interface_t *eif;
181
182   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
183     return 0;
184
185   eif = ethernet_get_interface (em, hw_if_index);
186   if (eif)
187     {
188       *result = hw_if_index;
189       return 1;
190     }
191   return 0;
192 }
193
194 clib_error_t *
195 ethernet_register_interface (vnet_main_t * vnm,
196                              u32 dev_class_index,
197                              u32 dev_instance,
198                              u8 * address,
199                              u32 * hw_if_index_return,
200                              ethernet_flag_change_function_t flag_change)
201 {
202   ethernet_main_t *em = &ethernet_main;
203   ethernet_interface_t *ei;
204   vnet_hw_interface_t *hi;
205   clib_error_t *error = 0;
206   u32 hw_if_index;
207
208   pool_get (em->interfaces, ei);
209   ei->flag_change = flag_change;
210
211   hw_if_index = vnet_register_interface
212     (vnm,
213      dev_class_index, dev_instance,
214      ethernet_hw_interface_class.index, ei - em->interfaces);
215   *hw_if_index_return = hw_if_index;
216
217   hi = vnet_get_hw_interface (vnm, hw_if_index);
218
219   ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
220
221   hi->min_packet_bytes = hi->min_supported_packet_bytes =
222     ETHERNET_MIN_PACKET_BYTES;
223   hi->max_packet_bytes = hi->max_supported_packet_bytes =
224     ETHERNET_MAX_PACKET_BYTES;
225   hi->per_packet_overhead_bytes =
226     /* preamble */ 8 + /* inter frame gap */ 12;
227
228   /* Standard default ethernet MTU. */
229   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
230
231   clib_memcpy (ei->address, address, sizeof (ei->address));
232   vec_free (hi->hw_address);
233   vec_add (hi->hw_address, address, sizeof (ei->address));
234
235   if (error)
236     {
237       pool_put (em->interfaces, ei);
238       return error;
239     }
240   return error;
241 }
242
243 void
244 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
245 {
246   ethernet_main_t *em = &ethernet_main;
247   ethernet_interface_t *ei;
248   vnet_hw_interface_t *hi;
249   main_intf_t *main_intf;
250   vlan_table_t *vlan_table;
251   u32 idx;
252
253   hi = vnet_get_hw_interface (vnm, hw_if_index);
254   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
255
256   /* Delete vlan mapping table for dot1q and dot1ad. */
257   main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
258   if (main_intf->dot1q_vlans)
259     {
260       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
261       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
262         {
263           if (vlan_table->vlans[idx].qinqs)
264             {
265               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
266             }
267         }
268       pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
269     }
270   if (main_intf->dot1ad_vlans)
271     {
272       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
273       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
274         {
275           if (vlan_table->vlans[idx].qinqs)
276             {
277               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
278             }
279         }
280       pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
281     }
282
283   vnet_delete_hw_interface (vnm, hw_if_index);
284   pool_put (em->interfaces, ei);
285 }
286
287 u32
288 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
289 {
290   ethernet_main_t *em = &ethernet_main;
291   vnet_hw_interface_t *hi;
292   ethernet_interface_t *ei;
293
294   hi = vnet_get_hw_interface (vnm, hw_if_index);
295
296   ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
297
298   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
299   if (ei->flag_change)
300     return ei->flag_change (vnm, hi, flags);
301   return (u32) ~ 0;
302 }
303
304 /* Echo packets back to ethernet/l2-input. */
305 static uword
306 simulated_ethernet_interface_tx (vlib_main_t * vm,
307                                  vlib_node_runtime_t * node,
308                                  vlib_frame_t * frame)
309 {
310   u32 n_left_from, n_left_to_next, n_copy, *from, *to_next;
311   u32 next_index = VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
312   u32 i, next_node_index, bvi_flag, sw_if_index;
313   u32 n_pkts = 0, n_bytes = 0;
314   u32 cpu_index = vm->cpu_index;
315   vnet_main_t *vnm = vnet_get_main ();
316   vnet_interface_main_t *im = &vnm->interface_main;
317   vlib_node_main_t *nm = &vm->node_main;
318   vlib_node_t *loop_node;
319   vlib_buffer_t *b;
320
321   // check tx node index, it is ethernet-input on loopback create
322   // but can be changed to l2-input if loopback is configured as
323   // BVI of a BD (Bridge Domain).
324   loop_node = vec_elt (nm->nodes, node->node_index);
325   next_node_index = loop_node->next_nodes[next_index];
326   bvi_flag = (next_node_index == l2input_node.index) ? 1 : 0;
327
328   n_left_from = frame->n_vectors;
329   from = vlib_frame_args (frame);
330
331   while (n_left_from > 0)
332     {
333       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
334
335       n_copy = clib_min (n_left_from, n_left_to_next);
336
337       clib_memcpy (to_next, from, n_copy * sizeof (from[0]));
338       n_left_to_next -= n_copy;
339       n_left_from -= n_copy;
340       i = 0;
341       b = vlib_get_buffer (vm, from[i]);
342       sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
343       while (1)
344         {
345           // Set up RX and TX indices as if received from a real driver
346           // unless loopback is used as a BVI. For BVI case, leave TX index
347           // and update l2_len in packet as required for l2 forwarding path
348           vnet_buffer (b)->sw_if_index[VLIB_RX] = sw_if_index;
349           if (bvi_flag)
350             {
351               vnet_update_l2_len (b);
352               vnet_buffer (b)->sw_if_index[VLIB_TX] = L2INPUT_BVI;
353             }
354           else
355             vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
356
357           i++;
358           n_pkts++;
359           n_bytes += vlib_buffer_length_in_chain (vm, b);
360
361           if (i < n_copy)
362             b = vlib_get_buffer (vm, from[i]);
363           else
364             break;
365         }
366
367       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
368
369       /* increment TX interface stat */
370       vlib_increment_combined_counter (im->combined_sw_if_counters +
371                                        VNET_INTERFACE_COUNTER_TX, cpu_index,
372                                        sw_if_index, n_pkts, n_bytes);
373     }
374
375   return n_left_from;
376 }
377
378 static u8 *
379 format_simulated_ethernet_name (u8 * s, va_list * args)
380 {
381   u32 dev_instance = va_arg (*args, u32);
382   return format (s, "loop%d", dev_instance);
383 }
384
385 static clib_error_t *
386 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
387                                   u32 flags)
388 {
389   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
390     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
391   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
392   return 0;
393 }
394
395 /* *INDENT-OFF* */
396 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
397   .name = "Loopback",
398   .format_device_name = format_simulated_ethernet_name,
399   .tx_function = simulated_ethernet_interface_tx,
400   .admin_up_down_function = simulated_ethernet_admin_up_down,
401   .no_flatten_output_chains = 1,
402 };
403 /* *INDENT-ON* */
404
405 int
406 vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address)
407 {
408   vnet_main_t *vnm = vnet_get_main ();
409   vlib_main_t *vm = vlib_get_main ();
410   clib_error_t *error;
411   static u32 instance;
412   u8 address[6];
413   u32 hw_if_index;
414   vnet_hw_interface_t *hw_if;
415   u32 slot;
416   int rv = 0;
417
418   ASSERT (sw_if_indexp);
419
420   *sw_if_indexp = (u32) ~ 0;
421
422   memset (address, 0, sizeof (address));
423
424   /*
425    * Default MAC address (dead:0000:0000 + instance) is allocated
426    * if zero mac_address is configured. Otherwise, user-configurable MAC
427    * address is programmed on the loopback interface.
428    */
429   if (memcmp (address, mac_address, sizeof (address)))
430     clib_memcpy (address, mac_address, sizeof (address));
431   else
432     {
433       address[0] = 0xde;
434       address[1] = 0xad;
435       address[5] = instance;
436     }
437
438   error = ethernet_register_interface
439     (vnm,
440      ethernet_simulated_device_class.index, instance++, address, &hw_if_index,
441      /* flag change */ 0);
442
443   if (error)
444     {
445       rv = VNET_API_ERROR_INVALID_REGISTRATION;
446       clib_error_report (error);
447       return rv;
448     }
449
450   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
451   slot = vlib_node_add_named_next_with_slot
452     (vm, hw_if->tx_node_index,
453      "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
454   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
455
456   {
457     vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
458     *sw_if_indexp = si->sw_if_index;
459   }
460
461   return 0;
462 }
463
464 static clib_error_t *
465 create_simulated_ethernet_interfaces (vlib_main_t * vm,
466                                       unformat_input_t * input,
467                                       vlib_cli_command_t * cmd)
468 {
469   int rv;
470   u32 sw_if_index;
471   u8 mac_address[6];
472
473   memset (mac_address, 0, sizeof (mac_address));
474
475   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
476     {
477       if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
478         ;
479       else
480         break;
481     }
482
483   rv = vnet_create_loopback_interface (&sw_if_index, mac_address);
484
485   if (rv)
486     return clib_error_return (0, "vnet_create_loopback_interface failed");
487
488   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
489                    sw_if_index);
490   return 0;
491 }
492
493 /*?
494  * Create a loopback interface. Optionally, a MAC Address can be
495  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
496  *
497  * @cliexpar
498  * The following two command syntaxes are equivalent:
499  * @cliexcmd{loopback create-interface [mac <mac-addr>]}
500  * @cliexcmd{create loopback interface [mac <mac-addr>]}
501  * Example of how to create a loopback interface:
502  * @cliexcmd{loopback create-interface}
503 ?*/
504 /* *INDENT-OFF* */
505 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
506   .path = "loopback create-interface",
507   .short_help = "loopback create-interface [mac <mac-addr>]",
508   .function = create_simulated_ethernet_interfaces,
509 };
510 /* *INDENT-ON* */
511
512 /*?
513  * Create a loopback interface. Optionally, a MAC Address can be
514  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
515  *
516  * @cliexpar
517  * The following two command syntaxes are equivalent:
518  * @cliexcmd{loopback create-interface [mac <mac-addr>]}
519  * @cliexcmd{create loopback interface [mac <mac-addr>]}
520  * Example of how to create a loopback interface:
521  * @cliexcmd{create loopback interface}
522 ?*/
523 /* *INDENT-OFF* */
524 VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
525   .path = "create loopback interface",
526   .short_help = "create loopback interface [mac <mac-addr>]",
527   .function = create_simulated_ethernet_interfaces,
528 };
529 /* *INDENT-ON* */
530
531 ethernet_interface_t *
532 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
533 {
534   vnet_hw_interface_t *i =
535     vnet_get_hw_interface (vnet_get_main (), hw_if_index);
536   return (i->hw_class_index ==
537           ethernet_hw_interface_class.
538           index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
539 }
540
541 int
542 vnet_delete_loopback_interface (u32 sw_if_index)
543 {
544   vnet_main_t *vnm = vnet_get_main ();
545   vnet_sw_interface_t *si;
546
547   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
548     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
549
550   si = vnet_get_sw_interface (vnm, sw_if_index);
551   ethernet_delete_interface (vnm, si->hw_if_index);
552
553   return 0;
554 }
555
556 int
557 vnet_delete_sub_interface (u32 sw_if_index)
558 {
559   vnet_main_t *vnm = vnet_get_main ();
560   int rv = 0;
561
562   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
563     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
564
565
566   vnet_interface_main_t *im = &vnm->interface_main;
567   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
568
569   if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
570     {
571       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
572       u64 sup_and_sub_key =
573         ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
574
575       hash_unset_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
576       vnet_delete_sw_interface (vnm, sw_if_index);
577     }
578   else
579     {
580       rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
581     }
582   return rv;
583 }
584
585 static clib_error_t *
586 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
587                                       unformat_input_t * input,
588                                       vlib_cli_command_t * cmd)
589 {
590   int rv;
591   u32 sw_if_index = ~0;
592   vnet_main_t *vnm = vnet_get_main ();
593
594   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
595     {
596       if (unformat (input, "intfc %U",
597                     unformat_vnet_sw_interface, vnm, &sw_if_index))
598         ;
599       else
600         break;
601     }
602
603   if (sw_if_index == ~0)
604     return clib_error_return (0, "interface not specified");
605
606   rv = vnet_delete_loopback_interface (sw_if_index);
607
608   if (rv)
609     return clib_error_return (0, "vnet_delete_loopback_interface failed");
610
611   return 0;
612 }
613
614 static clib_error_t *
615 delete_sub_interface (vlib_main_t * vm,
616                       unformat_input_t * input, vlib_cli_command_t * cmd)
617 {
618   int rv = 0;
619   u32 sw_if_index = ~0;
620   vnet_main_t *vnm = vnet_get_main ();
621
622   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
623     {
624       if (unformat
625           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
626         ;
627       else
628         break;
629     }
630   if (sw_if_index == ~0)
631     return clib_error_return (0, "interface doesn't exist");
632
633   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
634     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
635   else
636     rv = vnet_delete_sub_interface (sw_if_index);
637   if (rv)
638     return clib_error_return (0, "delete_subinterface_interface failed");
639   return 0;
640 }
641
642 /*?
643  * Delete a loopback interface.
644  *
645  * @cliexpar
646  * The following two command syntaxes are equivalent:
647  * @cliexcmd{loopback delete-interface intfc <interface>}
648  * @cliexcmd{delete loopback interface intfc <interface>}
649  * Example of how to delete a loopback interface:
650  * @cliexcmd{loopback delete-interface intfc loop0}
651 ?*/
652 /* *INDENT-OFF* */
653 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
654   .path = "loopback delete-interface",
655   .short_help = "loopback delete-interface intfc <interface>",
656   .function = delete_simulated_ethernet_interfaces,
657 };
658 /* *INDENT-ON* */
659
660 /*?
661  * Delete a loopback interface.
662  *
663  * @cliexpar
664  * The following two command syntaxes are equivalent:
665  * @cliexcmd{loopback delete-interface intfc <interface>}
666  * @cliexcmd{delete loopback interface intfc <interface>}
667  * Example of how to delete a loopback interface:
668  * @cliexcmd{delete loopback interface intfc loop0}
669 ?*/
670 /* *INDENT-OFF* */
671 VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
672   .path = "delete loopback interface",
673   .short_help = "delete loopback interface intfc <interface>",
674   .function = delete_simulated_ethernet_interfaces,
675 };
676 /* *INDENT-ON* */
677
678 /*?
679  * Delete a sub-interface.
680  *
681  * @cliexpar
682  * Example of how to delete a sub-interface:
683  * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
684 ?*/
685 /* *INDENT-OFF* */
686 VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
687   .path = "delete sub-interface",
688   .short_help = "delete sub-interface <interface>",
689   .function = delete_sub_interface,
690 };
691 /* *INDENT-ON* */
692
693 /*
694  * fd.io coding-style-patch-verification: ON
695  *
696  * Local Variables:
697  * eval: (c-set-style "gnu")
698  * End:
699  */