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