Add ability to override the interface name.
[vpp.git] / vnet / vnet / interface_cli.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  * interface_cli.c: interface CLI
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
43 static int compare_interface_names (void *a1, void *a2)
44 {
45   u32 * hi1 = a1;
46   u32 * hi2 = a2;
47
48   return vnet_hw_interface_compare (vnet_get_main(), *hi1, *hi2);
49 }
50
51 static clib_error_t *
52 show_or_clear_hw_interfaces (vlib_main_t * vm,
53                              unformat_input_t * input,
54                              vlib_cli_command_t * cmd)
55 {
56   clib_error_t * error = 0;
57   vnet_main_t * vnm = vnet_get_main();
58   vnet_interface_main_t * im = &vnm->interface_main;
59   vnet_hw_interface_t * hi;
60   u32 hw_if_index, * hw_if_indices = 0;
61   int i, verbose = 1, is_show;
62
63   is_show = strstr (cmd->path, "show") != 0;
64   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
65     {
66       /* See if user wants to show a specific interface. */
67       if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
68         {
69           vec_add1 (hw_if_indices, hw_if_index);
70           /* Implies verbose. */
71           verbose = 1;
72         }
73       /* See if user wants to show an interface with a specific hw_if_index. */
74       else if (unformat (input, "%u", &hw_if_index))
75        {
76          vec_add1 (hw_if_indices, hw_if_index);
77          /* Implies verbose. */
78          verbose = 1;
79        }
80
81       else if (unformat (input, "verbose"))
82         verbose = 1;
83
84       else if (unformat (input, "detail"))
85         verbose = 2;
86
87       else if (unformat (input, "brief"))
88         verbose = 0;
89
90       else
91         {
92           error = clib_error_return (0, "unknown input `%U'",
93                                      format_unformat_error, input);
94           goto done;
95         }
96     }
97         
98   /* Gather interfaces. */
99   if (vec_len (hw_if_indices) == 0)
100     pool_foreach (hi, im->hw_interfaces,
101                   vec_add1 (hw_if_indices, hi - im->hw_interfaces));
102
103   if (is_show)
104     {
105       /* Sort by name. */
106       vec_sort_with_function (hw_if_indices, compare_interface_names);
107
108       vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
109       for (i = 0; i < vec_len (hw_if_indices); i++)
110         {
111           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
112           vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, hi, verbose);
113         }
114     }
115   else
116     {
117       for (i = 0; i < vec_len (hw_if_indices); i++)
118         {
119           vnet_device_class_t * dc;
120
121           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
122           dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
123           
124           if (dc->clear_counters)
125             dc->clear_counters (hi->dev_instance);
126         }
127     }
128
129  done:
130   vec_free (hw_if_indices);
131   return error;
132 }
133
134 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
135   .path = "show hardware-interfaces",
136   .short_help = "show hardware-interfaces [verbose|brief]  [<if-name1> <if-name2> ...]",
137   .function = show_or_clear_hw_interfaces,
138 };
139
140 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
141   .path = "clear hardware-interfaces",
142   .short_help = "Clear hardware interfaces statistics",
143   .function = show_or_clear_hw_interfaces,
144 };
145
146 static int sw_interface_name_compare (void *a1, void *a2)
147 {
148   vnet_sw_interface_t *si1 = a1;
149   vnet_sw_interface_t *si2 = a2;
150
151   return vnet_sw_interface_compare (vnet_get_main(), 
152                                     si1->sw_if_index, si2->sw_if_index);
153 }
154
155 static clib_error_t *
156 show_sw_interfaces (vlib_main_t * vm,
157                     unformat_input_t * input,
158                     vlib_cli_command_t * cmd)
159 {
160   clib_error_t * error = 0;
161   vnet_main_t * vnm = vnet_get_main();
162   vnet_interface_main_t * im = &vnm->interface_main;
163   vnet_sw_interface_t * si, * sorted_sis = 0;
164   u8 show_addresses = 0;
165
166   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
167     {
168        u32 sw_if_index;
169
170       /* See if user wants to show specific interface */
171       if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
172         {
173           si =  pool_elt_at_index (im->sw_interfaces, sw_if_index);
174           vec_add1 (sorted_sis, si[0]);
175         }
176
177       else if (unformat (input, "address") || unformat (input, "addr"))
178           show_addresses = 1;
179
180       else
181         {
182           error = clib_error_return (0, "unknown input `%U'",
183                                      format_unformat_error, input);
184           goto done;
185         }
186     }
187
188   if (!show_addresses)
189       vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
190
191   if (vec_len (sorted_sis) == 0) /* Get all interfaces */
192     {
193       /* Gather interfaces. */
194       sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
195       _vec_len (sorted_sis) = 0;
196       pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
197
198       /* Sort by name. */
199       vec_sort_with_function (sorted_sis, sw_interface_name_compare);
200     }
201
202   if (show_addresses)
203     {
204       vec_foreach (si, sorted_sis)
205         {
206           l2input_main_t * l2m = &l2input_main;
207           ip4_main_t * im4 = &ip4_main;
208           ip6_main_t * im6 = &ip6_main;
209           ip_lookup_main_t * lm4 = &im4->lookup_main;
210           ip_lookup_main_t * lm6 = &im6->lookup_main;
211           ip_interface_address_t * ia = 0;
212           ip4_address_t * r4;
213           ip6_address_t * r6;
214           u32 fib_index4 = 0, fib_index6 = 0;
215           ip4_fib_t * fib4;
216           ip6_fib_t * fib6;
217           l2_input_config_t * config;
218
219           if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
220             fib_index4 = vec_elt (im4->fib_index_by_sw_if_index, 
221                                   si->sw_if_index);
222
223           if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
224             fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
225                                   si->sw_if_index);
226
227           fib4 = vec_elt_at_index (im4->fibs, fib_index4);
228           fib6 = vec_elt_at_index (im6->fibs, fib_index6);
229
230           if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
231             vlib_cli_output 
232                 (vm, "%U (%s): \n  unnumbered, use %U", 
233                  format_vnet_sw_if_index_name,
234                  vnm, si->sw_if_index,
235                  (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
236                  format_vnet_sw_if_index_name,
237                  vnm, si->unnumbered_sw_if_index);
238                              
239           else
240             {
241             vlib_cli_output (vm, "%U (%s):", 
242                              format_vnet_sw_if_index_name,
243                              vnm, si->sw_if_index,
244                              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) 
245                              ? "up" : "dn");
246             }
247
248           /* Display any L2 addressing info */
249           vec_validate(l2m->configs, si->sw_if_index);
250           config = vec_elt_at_index(l2m->configs, si->sw_if_index);
251           if (config->bridge) 
252             {
253               u32 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
254               vlib_cli_output (vm, "  l2 bridge bd_id %d%s%d", bd_id, 
255                              config->bvi ? " bvi shg " : " shg ", config->shg);
256             } 
257           else if (config->xconnect) 
258             {
259               vlib_cli_output (vm, "  l2 xconnect %U", 
260                                format_vnet_sw_if_index_name,
261                                vnm, config->output_sw_if_index);
262             }
263
264           /* Display any IP4 addressing info */
265           foreach_ip_interface_address (lm4, ia, si->sw_if_index, 
266                                         1 /* honor unnumbered */,
267           ({
268             r4 = ip_interface_address_get_address (lm4, ia);
269             if (fib4->table_id)
270               {
271                 vlib_cli_output (vm, "  %U/%d table %d", 
272                                  format_ip4_address, r4, 
273                                  ia->address_length,
274                                  fib4->table_id);
275               }
276             else
277               {
278                 vlib_cli_output (vm, "  %U/%d", 
279                                  format_ip4_address, r4, 
280                                  ia->address_length);
281               }
282           }));
283
284           /* Display any IP6 addressing info */
285           foreach_ip_interface_address (lm6, ia, si->sw_if_index, 
286                                         1 /* honor unnumbered */,
287           ({
288             r6 = ip_interface_address_get_address (lm6, ia);
289             if (fib6->table_id)
290               {
291                 vlib_cli_output (vm, "  %U/%d table %d", 
292                                  format_ip6_address, r6, 
293                                  ia->address_length,
294                                  fib6->table_id);
295               }
296             else
297               {
298                 vlib_cli_output (vm, "  %U/%d", 
299                                  format_ip6_address, r6, 
300                                  ia->address_length);
301               }
302           }));
303         }
304     }
305   else
306     {
307       vec_foreach (si, sorted_sis)
308         {
309           vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
310         }
311     }
312
313  done:
314   vec_free (sorted_sis);
315   return error;
316 }
317
318 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
319   .path = "show interfaces",
320   .short_help = "show interfaces [address|addr] [<if-name1> <if-name2> ...]",
321   .function = show_sw_interfaces,
322 };
323
324 /* Root of all interface commands. */
325 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
326   .path = "interface",
327   .short_help = "Interface commands",
328 };
329
330 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
331   .path = "set interface",
332   .short_help = "Interface commands",
333 };
334
335 static clib_error_t *
336 clear_interface_counters (vlib_main_t * vm,
337                           unformat_input_t * input,
338                           vlib_cli_command_t * cmd)
339 {
340   vnet_main_t * vnm = vnet_get_main();
341   vnet_interface_main_t * im = &vnm->interface_main;
342   vlib_simple_counter_main_t * sm;
343   vlib_combined_counter_main_t * cm;
344   static vnet_main_t ** my_vnet_mains;
345   int i, j, n_counters;
346
347   vec_reset_length (my_vnet_mains);
348       
349   for (i = 0; i < vec_len (vnet_mains); i++)
350     {
351       if (vnet_mains[i])
352         vec_add1 (my_vnet_mains, vnet_mains[i]);
353     }
354
355   if (vec_len (vnet_mains) == 0)
356     vec_add1 (my_vnet_mains, vnm);
357
358   n_counters = vec_len (im->combined_sw_if_counters);
359
360   for (j = 0; j < n_counters; j++)
361     {
362       for (i = 0; i < vec_len(my_vnet_mains); i++)
363         {
364           im = &my_vnet_mains[i]->interface_main;
365           cm = im->combined_sw_if_counters + j;
366           vlib_clear_combined_counters (cm);
367         }
368     }
369
370   n_counters = vec_len (im->sw_if_counters);
371
372   for (j = 0; j < n_counters; j++)
373     {
374       for (i = 0; i < vec_len(my_vnet_mains); i++)
375         {
376           im = &my_vnet_mains[i]->interface_main;
377           sm = im->sw_if_counters + j;
378           vlib_clear_simple_counters (sm);
379         }
380     }
381
382   return 0;
383 }
384
385 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
386   .path = "clear interfaces",
387   .short_help = "Clear interfaces statistics",
388   .function = clear_interface_counters,
389 };
390
391 // The following subinterface syntax is supported. The first two are for 
392 // backwards compatability:
393 //
394 // <intf-name> <id>
395 //     - a subinterface with the name <intf-name>.<id>. The subinterface
396 //       is a single dot1q vlan with vlan id <id> and exact-match semantics.
397 //
398 // <intf-name> <min_id>-<max_id> 
399 //     - a set of the above subinterfaces, repeating for each id
400 //       in the range <min_id> to <max_id>
401 //
402 // In the following, exact-match semantics (i.e. the number of vlan tags on the
403 // packet must match the number of tags in the configuration) are used only if 
404 // the keyword exact-match is present. Non-exact match is the default.
405 //
406 // <intf-name> <id> dot1q <outer_id> [exact-match]
407 //     - a subinterface with the name <intf-name>.<id>. The subinterface
408 //       is a single dot1q vlan with vlan id <outer_id>. 
409 //
410 // <intf-name> <id> dot1q any [exact-match]
411 //     - a subinterface with the name <intf-name>.<id>. The subinterface
412 //       is a single dot1q vlan with any vlan id.
413 //
414 // <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
415 //     - a subinterface with the name <intf-name>.<id>. The subinterface
416 //       is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id 
417 //       <inner_id>. 
418 //
419 // <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
420 //     - a subinterface with the name <intf-name>.<id>. The subinterface
421 //       is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
422 //
423 // <intf-name> <id> dot1q any inner-dot1q any [exact-match]
424 //
425 //     - a subinterface with the name <intf-name>.<id>. The subinterface
426 //       is a double dot1q vlan with any outer vlan id and any inner vlan id.
427 //
428 // For each of the above CLI, there is a duplicate that uses the keyword
429 // "dot1ad" in place of the first "dot1q". These interfaces use ethertype
430 // 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
431 // tagged packets the inner ethertype is always 0x8100. Also note that
432 // the dot1q and dot1ad naming spaces are independent, so it is legal to
433 // have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
434 //
435 // <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
436 //     - a subinterface with the name <intf-name>.<id>. The subinterface
437 //       is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan 
438 //       id <inner_id>. 
439 //
440 // <intf-name> <id> untagged
441 //     - a subinterface with the name <intf-name>.<id>. The subinterface
442 //       has no vlan tags. Only one can be specified per interface.
443 //      
444 // <intf-name> <id> default
445 //     - a subinterface with the name <intf-name>.<id>. This is associated
446 //       with a packet that did not match any other configured subinterface
447 //       on this interface. Only one can be specified per interface.
448
449
450 static clib_error_t *
451 parse_vlan_sub_interfaces (unformat_input_t    * input,
452                            vnet_sw_interface_t * template)
453 {
454   clib_error_t * error = 0;
455   u32 inner_vlan, outer_vlan;
456
457   if (unformat (input, "any inner-dot1q any")) {
458     template->sub.eth.flags.two_tags = 1;
459     template->sub.eth.flags.outer_vlan_id_any = 1;
460     template->sub.eth.flags.inner_vlan_id_any = 1;
461   } else if (unformat (input, "any")) {
462     template->sub.eth.flags.one_tag = 1;
463     template->sub.eth.flags.outer_vlan_id_any = 1;
464   } else if (unformat (input, "%d inner-dot1q any", &outer_vlan)) {
465     template->sub.eth.flags.two_tags = 1;
466     template->sub.eth.flags.inner_vlan_id_any = 1;
467     template->sub.eth.outer_vlan_id = outer_vlan;     
468   } else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan)) {
469     template->sub.eth.flags.two_tags = 1;
470     template->sub.eth.outer_vlan_id = outer_vlan;
471     template->sub.eth.inner_vlan_id = inner_vlan;     
472   } else if (unformat (input, "%d", &outer_vlan)) {
473     template->sub.eth.flags.one_tag = 1;
474     template->sub.eth.outer_vlan_id = outer_vlan;
475   } else {
476     error = clib_error_return (0, "expected dot1q config, got `%U'",
477                               format_unformat_error, input);
478     goto done;
479   }
480
481   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
482     if (unformat (input, "exact-match")) {
483       template->sub.eth.flags.exact_match = 1;
484     }
485   }
486
487  done:
488   return error;
489 }
490
491 static clib_error_t *
492 create_sub_interfaces (vlib_main_t * vm,
493                        unformat_input_t * input,
494                        vlib_cli_command_t * cmd)
495 {
496   vnet_main_t * vnm = vnet_get_main();
497   clib_error_t * error = 0;
498   u32 hw_if_index, sw_if_index;
499   vnet_hw_interface_t * hi;
500   u32 id, id_min, id_max;
501   vnet_sw_interface_t template;
502
503   hw_if_index = ~0;
504   if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
505     {
506       error = clib_error_return (0, "unknown interface `%U'",
507                                  format_unformat_error, input);
508       goto done;
509     }
510
511   memset (&template, 0, sizeof (template));
512   template.sub.eth.raw_flags = 0;
513
514   if (unformat (input, "%d default", &id_min)) {
515     id_max = id_min;
516     template.sub.eth.flags.default_sub = 1;
517   } else if (unformat (input, "%d untagged", &id_min)) {
518     id_max = id_min;
519     template.sub.eth.flags.no_tags = 1;
520     template.sub.eth.flags.exact_match = 1;
521   } else if (unformat (input, "%d dot1q", &id_min)) {
522     // parse dot1q config
523     id_max = id_min;
524     error = parse_vlan_sub_interfaces(input, &template);
525     if (error) goto done;
526   } else if (unformat (input, "%d dot1ad", &id_min)) {
527     // parse dot1ad config
528     id_max = id_min;
529     template.sub.eth.flags.dot1ad = 1;
530     error = parse_vlan_sub_interfaces(input, &template);
531     if (error) goto done;
532   } else if (unformat (input, "%d-%d", &id_min, &id_max)) {
533     template.sub.eth.flags.one_tag = 1;
534     template.sub.eth.outer_vlan_id = id_min;
535     template.sub.eth.flags.exact_match = 1;
536     if (id_min > id_max)
537       goto id_error;
538   } else if (unformat (input, "%d", &id_min)) {
539     id_max = id_min;
540     template.sub.eth.flags.one_tag = 1;
541     template.sub.eth.outer_vlan_id = id_min;
542     template.sub.eth.flags.exact_match = 1;
543   } else {
544     id_error:
545       error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
546                                  format_unformat_error, input);
547       goto done;
548   }
549
550   /*
551   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
552     error = clib_error_return (0, "unexpected text `%U'",
553                                format_unformat_error, input);
554     goto done;
555   }
556   */
557
558   hi = vnet_get_hw_interface (vnm, hw_if_index);
559   for (id = id_min; id <= id_max; id++)
560     {
561       uword * p;
562       vnet_interface_main_t * im = &vnm->interface_main;
563       u64 sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) |
564           (u64) id;
565       u64 * kp;
566
567       p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
568       if (p)
569         {
570           if (CLIB_DEBUG > 0)
571             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
572                           hi->sw_if_index, id);
573           continue;
574         }
575
576       kp = clib_mem_alloc (sizeof (*kp));
577       *kp = sup_and_sub_key;
578
579       template.type = VNET_SW_INTERFACE_TYPE_SUB;
580       template.sup_sw_if_index = hi->sw_if_index;
581       template.sub.id = id;
582       error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
583       if (error) goto done;
584       hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
585       hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
586     }
587
588   if (error)
589     goto done;
590
591  done:
592   return error;
593 }
594
595 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
596   .path = "create sub-interface",
597   .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
598   .function = create_sub_interfaces,
599 };
600
601 static clib_error_t *
602 set_state (vlib_main_t * vm,
603            unformat_input_t * input,
604            vlib_cli_command_t * cmd)
605 {
606   vnet_main_t * vnm = vnet_get_main();
607   clib_error_t * error;
608   u32 sw_if_index, flags;
609
610   sw_if_index = ~0;
611   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
612     {
613       error = clib_error_return (0, "unknown interface `%U'",
614                                  format_unformat_error, input);
615       goto done;
616     }
617
618   if (! unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
619     {
620       error = clib_error_return (0, "unknown flags `%U'",
621                                  format_unformat_error, input);
622       goto done;
623     }
624
625   error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
626   if (error)
627     goto done;
628
629  done:
630   return error;
631 }
632
633 VLIB_CLI_COMMAND (set_state_command, static) = {
634   .path = "set interface state",
635   .short_help = "Set interface state",
636   .function = set_state,
637 };
638
639 static clib_error_t *
640 set_unnumbered (vlib_main_t * vm,
641                 unformat_input_t * input,
642                 vlib_cli_command_t * cmd)
643 {
644   vnet_main_t * vnm = vnet_get_main();
645   u32 unnumbered_sw_if_index;
646   u32 inherit_from_sw_if_index;
647   vnet_sw_interface_t * si;
648   int is_set = 0;
649   int is_del = 0;
650
651   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
652   {
653
654       if (unformat (input, "%U use %U", 
655                     unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
656                     unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
657           is_set = 1;
658       else if (unformat (input, "del %U",
659                          unformat_vnet_sw_interface, 
660                          vnm, &unnumbered_sw_if_index))
661           is_del = 1;
662       else
663         {
664           if (is_set || is_del)
665             break;
666           else
667             return clib_error_return 
668               (0, "parse error '%U'", format_unformat_error, input);
669         }
670   }
671
672   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
673   if (is_del) {
674       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
675       si->unnumbered_sw_if_index = (u32)~0;
676   } else {
677       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
678       si->unnumbered_sw_if_index = inherit_from_sw_if_index;
679   }
680       
681   return 0;
682 }
683
684 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
685   .path = "set interface unnumbered",
686   .short_help = "set interface unnumbered [<intfc> use <intfc>][del <intfc>]",
687   .function = set_unnumbered,
688 };
689
690
691
692 static clib_error_t *
693 set_hw_class (vlib_main_t * vm,
694               unformat_input_t * input,
695               vlib_cli_command_t * cmd)
696 {
697   vnet_main_t * vnm = vnet_get_main();
698   vnet_interface_main_t * im = &vnm->interface_main;
699   clib_error_t * error;
700   u32 hw_if_index, hw_class_index;
701
702   hw_if_index = ~0;
703   if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
704     {
705       error = clib_error_return (0, "unknown hardware interface `%U'",
706                                  format_unformat_error, input);
707       goto done;
708     }
709
710   if (! unformat_user (input, unformat_hash_string,
711                        im->hw_interface_class_by_name, &hw_class_index))
712     {
713       error = clib_error_return (0, "unknown hardware class `%U'",
714                                  format_unformat_error, input);
715       goto done;
716     }
717
718   error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
719   if (error)
720     goto done;
721
722  done:
723   return error;
724 }
725
726 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
727   .path = "set interface hw-class",
728   .short_help = "Set interface hardware class",
729   .function = set_hw_class,
730 };
731
732 static clib_error_t * vnet_interface_cli_init (vlib_main_t * vm)
733 { return 0; }
734
735 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
736
737 static clib_error_t * 
738 renumber_interface_command_fn (vlib_main_t * vm,
739                                unformat_input_t * input,
740                                vlib_cli_command_t * cmd)
741 {
742   u32 hw_if_index;
743   u32 new_dev_instance;
744   vnet_main_t * vnm = vnet_get_main();
745   int rv;
746
747   if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
748     return clib_error_return (0, "unknown hardware interface `%U'",
749                               format_unformat_error, input);
750
751   if (! unformat (input, "%d", &new_dev_instance))
752     return clib_error_return (0, "new dev instance missing");
753
754   rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
755
756   switch (rv)
757     {
758     case 0:
759       break;
760
761     default:
762       return clib_error_return (0, "vnet_interface_name_renumber returned %d",
763                                 rv);
764
765     }
766
767   return 0;
768 }
769
770
771 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
772   .path = "renumber interface",
773   .short_help = "renumber interface <if-name> <new-dev-instance>",
774   .function = renumber_interface_command_fn,
775 };
776