7d828f54951fee1e0653c477f82c6475166cf263
[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 /**
41  * @file
42  * Interface CLI.
43  */
44
45 #include <vnet/vnet.h>
46 #include <vnet/ip/ip.h>
47 #include <vppinfra/bitmap.h>
48
49 static int
50 compare_interface_names (void *a1, void *a2)
51 {
52   u32 *hi1 = a1;
53   u32 *hi2 = a2;
54
55   return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
56 }
57
58 static clib_error_t *
59 show_or_clear_hw_interfaces (vlib_main_t * vm,
60                              unformat_input_t * input,
61                              vlib_cli_command_t * cmd)
62 {
63   clib_error_t *error = 0;
64   vnet_main_t *vnm = vnet_get_main ();
65   vnet_interface_main_t *im = &vnm->interface_main;
66   vnet_hw_interface_t *hi;
67   u32 hw_if_index, *hw_if_indices = 0;
68   int i, verbose = -1, is_show, show_bond = 0;
69
70   is_show = strstr (cmd->path, "show") != 0;
71   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
72     {
73       /* See if user wants to show a specific interface. */
74       if (unformat
75           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
76         vec_add1 (hw_if_indices, hw_if_index);
77
78       /* See if user wants to show an interface with a specific hw_if_index. */
79       else if (unformat (input, "%u", &hw_if_index))
80         vec_add1 (hw_if_indices, hw_if_index);
81
82       else if (unformat (input, "verbose"))
83         verbose = 1;            /* this is also the default */
84
85       else if (unformat (input, "detail"))
86         verbose = 2;
87
88       else if (unformat (input, "brief"))
89         verbose = 0;
90
91       else if (unformat (input, "bond"))
92         {
93           show_bond = 1;
94           if (verbose < 0)
95             verbose = 0;        /* default to brief for link bonding */
96         }
97
98       else
99         {
100           error = clib_error_return (0, "unknown input `%U'",
101                                      format_unformat_error, input);
102           goto done;
103         }
104     }
105
106   /* Gather interfaces. */
107   if (vec_len (hw_if_indices) == 0)
108     pool_foreach (hi, im->hw_interfaces,
109                   vec_add1 (hw_if_indices, hi - im->hw_interfaces));
110
111   if (verbose < 0)
112     verbose = 1;                /* default to verbose (except bond) */
113
114   if (is_show)
115     {
116       /* Sort by name. */
117       vec_sort_with_function (hw_if_indices, compare_interface_names);
118
119       vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
120       for (i = 0; i < vec_len (hw_if_indices); i++)
121         {
122           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
123           if (show_bond == 0)   /* show all interfaces */
124             vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
125                              hi, verbose);
126           else if ((hi->bond_info) &&
127                    (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
128             {                   /* show only bonded interface and all its slave interfaces */
129               int hw_idx;
130               vnet_hw_interface_t *shi;
131               vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
132                                hi, verbose);
133
134               /* *INDENT-OFF* */
135               clib_bitmap_foreach (hw_idx, hi->bond_info,
136               ({
137                 shi = vnet_get_hw_interface(vnm, hw_idx);
138                 vlib_cli_output (vm, "%U\n",
139                                  format_vnet_hw_interface, vnm, shi, verbose);
140               }));
141               /* *INDENT-ON* */
142             }
143         }
144     }
145   else
146     {
147       for (i = 0; i < vec_len (hw_if_indices); i++)
148         {
149           vnet_device_class_t *dc;
150
151           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
152           dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
153
154           if (dc->clear_counters)
155             dc->clear_counters (hi->dev_instance);
156         }
157     }
158
159 done:
160   vec_free (hw_if_indices);
161   return error;
162 }
163
164 /* *INDENT-OFF* */
165 /*?
166  * Displays various information about the state of the current terminal
167  * session.
168  *
169  * @cliexpar
170  * @cliexstart{show hardware}
171  * Name                Link  Hardware
172  * GigabitEthernet2/0/0               up   GigabitEthernet2/0/0
173  * Ethernet address 00:50:56:b7:7c:83
174  * Intel 82545em_copper
175  *   link up, media 1000T full-duplex, master,
176  *   0 unprocessed, 384 total buffers on rx queue 0 ring
177  *   237 buffers in driver rx cache
178  *   rx total packets                                    1816
179  *   rx total bytes                                    181084
180  *   rx good packets                                     1816
181  *   rx good bytes                                     181084
182  *   rx 65 127 byte packets                              1586
183  *   rx 256 511 byte packets                              230
184  *   tx total packets                                     346
185  *   tx total bytes                                     90224
186  *   tx good packets                                      346
187  *   tx good bytes                                      88840
188  *   tx 64 byte packets                                     1
189  *   tx 65 127 byte packets                               115
190  *   tx 256 511 byte packets                              230
191  * @cliexend
192  ?*/
193 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
194   .path = "show hardware-interfaces",
195   .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] [<if-name1> <if-name2> ...]",
196   .function = show_or_clear_hw_interfaces,
197 };
198 /* *INDENT-ON* */
199
200 /* *INDENT-OFF* */
201 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
202   .path = "clear hardware-interfaces",
203   .short_help = "Clear hardware interfaces statistics",
204   .function = show_or_clear_hw_interfaces,
205 };
206 /* *INDENT-ON* */
207
208 static int
209 sw_interface_name_compare (void *a1, void *a2)
210 {
211   vnet_sw_interface_t *si1 = a1;
212   vnet_sw_interface_t *si2 = a2;
213
214   return vnet_sw_interface_compare (vnet_get_main (),
215                                     si1->sw_if_index, si2->sw_if_index);
216 }
217
218 static clib_error_t *
219 show_sw_interfaces (vlib_main_t * vm,
220                     unformat_input_t * input, vlib_cli_command_t * cmd)
221 {
222   clib_error_t *error = 0;
223   vnet_main_t *vnm = vnet_get_main ();
224   vnet_interface_main_t *im = &vnm->interface_main;
225   vnet_sw_interface_t *si, *sorted_sis = 0;
226   u8 show_addresses = 0;
227
228   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
229     {
230       u32 sw_if_index;
231
232       /* See if user wants to show specific interface */
233       if (unformat
234           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
235         {
236           si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
237           vec_add1 (sorted_sis, si[0]);
238         }
239       else if (unformat (input, "address") || unformat (input, "addr"))
240         show_addresses = 1;
241       else
242         {
243           error = clib_error_return (0, "unknown input `%U'",
244                                      format_unformat_error, input);
245           goto done;
246         }
247     }
248
249   if (!show_addresses)
250     vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
251
252   if (vec_len (sorted_sis) == 0)        /* Get all interfaces */
253     {
254       /* Gather interfaces. */
255       sorted_sis =
256         vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
257       _vec_len (sorted_sis) = 0;
258       pool_foreach (si, im->sw_interfaces, (
259                                              {
260                                              vec_add1 (sorted_sis, si[0]);
261                                              }
262                     ));
263
264       /* Sort by name. */
265       vec_sort_with_function (sorted_sis, sw_interface_name_compare);
266     }
267
268   if (show_addresses)
269     {
270       vec_foreach (si, sorted_sis)
271       {
272         l2input_main_t *l2m = &l2input_main;
273         ip4_main_t *im4 = &ip4_main;
274         ip6_main_t *im6 = &ip6_main;
275         ip_lookup_main_t *lm4 = &im4->lookup_main;
276         ip_lookup_main_t *lm6 = &im6->lookup_main;
277         ip_interface_address_t *ia = 0;
278         ip4_address_t *r4;
279         ip6_address_t *r6;
280         u32 fib_index4 = 0, fib_index6 = 0;
281         ip4_fib_t *fib4;
282         ip6_fib_t *fib6;
283         l2_input_config_t *config;
284
285         if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
286           fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
287                                 si->sw_if_index);
288
289         if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
290           fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
291                                 si->sw_if_index);
292
293         fib4 = vec_elt_at_index (im4->fibs, fib_index4);
294         fib6 = vec_elt_at_index (im6->fibs, fib_index6);
295
296         if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
297           vlib_cli_output
298             (vm, "%U (%s): \n  unnumbered, use %U",
299              format_vnet_sw_if_index_name,
300              vnm, si->sw_if_index,
301              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
302              format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
303
304         else
305           {
306             vlib_cli_output (vm, "%U (%s):",
307                              format_vnet_sw_if_index_name,
308                              vnm, si->sw_if_index,
309                              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
310                              ? "up" : "dn");
311           }
312
313         /* Display any L2 addressing info */
314         vec_validate (l2m->configs, si->sw_if_index);
315         config = vec_elt_at_index (l2m->configs, si->sw_if_index);
316         if (config->bridge)
317           {
318             u32 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
319             vlib_cli_output (vm, "  l2 bridge bd_id %d%s%d", bd_id,
320                              config->bvi ? " bvi shg " : " shg ",
321                              config->shg);
322           }
323         else if (config->xconnect)
324           {
325             vlib_cli_output (vm, "  l2 xconnect %U",
326                              format_vnet_sw_if_index_name,
327                              vnm, config->output_sw_if_index);
328           }
329
330         /* Display any IP4 addressing info */
331           /* *INDENT-OFF* */
332           foreach_ip_interface_address (lm4, ia, si->sw_if_index,
333                                         1 /* honor unnumbered */,
334           ({
335             r4 = ip_interface_address_get_address (lm4, ia);
336             if (fib4->table_id)
337               {
338                 vlib_cli_output (vm, "  %U/%d table %d",
339                                  format_ip4_address, r4,
340                                  ia->address_length,
341                                  fib4->table_id);
342               }
343             else
344               {
345                 vlib_cli_output (vm, "  %U/%d",
346                                  format_ip4_address, r4,
347                                  ia->address_length);
348               }
349           }));
350           /* *INDENT-ON* */
351
352         /* Display any IP6 addressing info */
353           /* *INDENT-OFF* */
354           foreach_ip_interface_address (lm6, ia, si->sw_if_index,
355                                         1 /* honor unnumbered */,
356           ({
357             r6 = ip_interface_address_get_address (lm6, ia);
358             if (fib6->table_id)
359               {
360                 vlib_cli_output (vm, "  %U/%d table %d",
361                                  format_ip6_address, r6,
362                                  ia->address_length,
363                                  fib6->table_id);
364               }
365             else
366               {
367                 vlib_cli_output (vm, "  %U/%d",
368                                  format_ip6_address, r6,
369                                  ia->address_length);
370               }
371           }));
372           /* *INDENT-ON* */
373       }
374     }
375   else
376     {
377       vec_foreach (si, sorted_sis)
378       {
379         vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
380       }
381     }
382
383 done:
384   vec_free (sorted_sis);
385   return error;
386 }
387
388 /* *INDENT-OFF* */
389 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
390   .path = "show interfaces",
391   .short_help = "show interfaces [address|addr] [<if-name1> <if-name2> ...]",
392   .function = show_sw_interfaces,
393 };
394 /* *INDENT-ON* */
395
396 /* Root of all interface commands. */
397 /* *INDENT-OFF* */
398 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
399   .path = "interface",
400   .short_help = "Interface commands",
401 };
402 /* *INDENT-ON* */
403
404 /* *INDENT-OFF* */
405 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
406   .path = "set interface",
407   .short_help = "Interface commands",
408 };
409 /* *INDENT-ON* */
410
411 static clib_error_t *
412 clear_interface_counters (vlib_main_t * vm,
413                           unformat_input_t * input, vlib_cli_command_t * cmd)
414 {
415   vnet_main_t *vnm = vnet_get_main ();
416   vnet_interface_main_t *im = &vnm->interface_main;
417   vlib_simple_counter_main_t *sm;
418   vlib_combined_counter_main_t *cm;
419   static vnet_main_t **my_vnet_mains;
420   int i, j, n_counters;
421
422   vec_reset_length (my_vnet_mains);
423
424   for (i = 0; i < vec_len (vnet_mains); i++)
425     {
426       if (vnet_mains[i])
427         vec_add1 (my_vnet_mains, vnet_mains[i]);
428     }
429
430   if (vec_len (vnet_mains) == 0)
431     vec_add1 (my_vnet_mains, vnm);
432
433   n_counters = vec_len (im->combined_sw_if_counters);
434
435   for (j = 0; j < n_counters; j++)
436     {
437       for (i = 0; i < vec_len (my_vnet_mains); i++)
438         {
439           im = &my_vnet_mains[i]->interface_main;
440           cm = im->combined_sw_if_counters + j;
441           vlib_clear_combined_counters (cm);
442         }
443     }
444
445   n_counters = vec_len (im->sw_if_counters);
446
447   for (j = 0; j < n_counters; j++)
448     {
449       for (i = 0; i < vec_len (my_vnet_mains); i++)
450         {
451           im = &my_vnet_mains[i]->interface_main;
452           sm = im->sw_if_counters + j;
453           vlib_clear_simple_counters (sm);
454         }
455     }
456
457   return 0;
458 }
459
460 /* *INDENT-OFF* */
461 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
462   .path = "clear interfaces",
463   .short_help = "Clear interfaces statistics",
464   .function = clear_interface_counters,
465 };
466 /* *INDENT-ON* */
467
468 /**
469  * Parse subinterface names.
470  *
471  * The following subinterface syntax is supported. The first two are for
472  * backwards compatability:
473  *
474  * <intf-name> <id>
475  *     - a subinterface with the name <intf-name>.<id>. The subinterface
476  *       is a single dot1q vlan with vlan id <id> and exact-match semantics.
477  *
478  * <intf-name> <min_id>-<max_id>
479  *     - a set of the above subinterfaces, repeating for each id
480  *       in the range <min_id> to <max_id>
481  *
482  * In the following, exact-match semantics (i.e. the number of vlan tags on the
483  * packet must match the number of tags in the configuration) are used only if
484  * the keyword exact-match is present. Non-exact match is the default.
485  *
486  * <intf-name> <id> dot1q <outer_id> [exact-match]
487  *     - a subinterface with the name <intf-name>.<id>. The subinterface
488  *       is a single dot1q vlan with vlan id <outer_id>.
489  *
490  * <intf-name> <id> dot1q any [exact-match]
491  *     - a subinterface with the name <intf-name>.<id>. The subinterface
492  *       is a single dot1q vlan with any vlan id.
493  *
494  * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
495  *     - a subinterface with the name <intf-name>.<id>. The subinterface
496  *       is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
497  *       <inner_id>.
498  *
499  * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
500  *     - a subinterface with the name <intf-name>.<id>. The subinterface
501  *       is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
502  *
503  * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
504  *
505  *     - a subinterface with the name <intf-name>.<id>. The subinterface
506  *       is a double dot1q vlan with any outer vlan id and any inner vlan id.
507  *
508  * For each of the above CLI, there is a duplicate that uses the keyword
509  * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
510  * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
511  * tagged packets the inner ethertype is always 0x8100. Also note that
512  * the dot1q and dot1ad naming spaces are independent, so it is legal to
513  * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
514  *
515  * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
516  *     - a subinterface with the name <intf-name>.<id>. The subinterface
517  *       is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
518  *       id <inner_id>.
519  *
520  * <intf-name> <id> untagged
521  *     - a subinterface with the name <intf-name>.<id>. The subinterface
522  *       has no vlan tags. Only one can be specified per interface.
523  *
524  * <intf-name> <id> default
525  *     - a subinterface with the name <intf-name>.<id>. This is associated
526  *       with a packet that did not match any other configured subinterface
527  *       on this interface. Only one can be specified per interface.
528  */
529
530 static clib_error_t *
531 parse_vlan_sub_interfaces (unformat_input_t * input,
532                            vnet_sw_interface_t * template)
533 {
534   clib_error_t *error = 0;
535   u32 inner_vlan, outer_vlan;
536
537   if (unformat (input, "any inner-dot1q any"))
538     {
539       template->sub.eth.flags.two_tags = 1;
540       template->sub.eth.flags.outer_vlan_id_any = 1;
541       template->sub.eth.flags.inner_vlan_id_any = 1;
542     }
543   else if (unformat (input, "any"))
544     {
545       template->sub.eth.flags.one_tag = 1;
546       template->sub.eth.flags.outer_vlan_id_any = 1;
547     }
548   else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
549     {
550       template->sub.eth.flags.two_tags = 1;
551       template->sub.eth.flags.inner_vlan_id_any = 1;
552       template->sub.eth.outer_vlan_id = outer_vlan;
553     }
554   else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
555     {
556       template->sub.eth.flags.two_tags = 1;
557       template->sub.eth.outer_vlan_id = outer_vlan;
558       template->sub.eth.inner_vlan_id = inner_vlan;
559     }
560   else if (unformat (input, "%d", &outer_vlan))
561     {
562       template->sub.eth.flags.one_tag = 1;
563       template->sub.eth.outer_vlan_id = outer_vlan;
564     }
565   else
566     {
567       error = clib_error_return (0, "expected dot1q config, got `%U'",
568                                  format_unformat_error, input);
569       goto done;
570     }
571
572   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
573     {
574       if (unformat (input, "exact-match"))
575         {
576           template->sub.eth.flags.exact_match = 1;
577         }
578     }
579
580 done:
581   return error;
582 }
583
584 static clib_error_t *
585 create_sub_interfaces (vlib_main_t * vm,
586                        unformat_input_t * input, vlib_cli_command_t * cmd)
587 {
588   vnet_main_t *vnm = vnet_get_main ();
589   clib_error_t *error = 0;
590   u32 hw_if_index, sw_if_index;
591   vnet_hw_interface_t *hi;
592   u32 id, id_min, id_max;
593   vnet_sw_interface_t template;
594
595   hw_if_index = ~0;
596   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
597     {
598       error = clib_error_return (0, "unknown interface `%U'",
599                                  format_unformat_error, input);
600       goto done;
601     }
602
603   memset (&template, 0, sizeof (template));
604   template.sub.eth.raw_flags = 0;
605
606   if (unformat (input, "%d default", &id_min))
607     {
608       id_max = id_min;
609       template.sub.eth.flags.default_sub = 1;
610     }
611   else if (unformat (input, "%d untagged", &id_min))
612     {
613       id_max = id_min;
614       template.sub.eth.flags.no_tags = 1;
615       template.sub.eth.flags.exact_match = 1;
616     }
617   else if (unformat (input, "%d dot1q", &id_min))
618     {
619       /* parse dot1q config */
620       id_max = id_min;
621       error = parse_vlan_sub_interfaces (input, &template);
622       if (error)
623         goto done;
624     }
625   else if (unformat (input, "%d dot1ad", &id_min))
626     {
627       /* parse dot1ad config */
628       id_max = id_min;
629       template.sub.eth.flags.dot1ad = 1;
630       error = parse_vlan_sub_interfaces (input, &template);
631       if (error)
632         goto done;
633     }
634   else if (unformat (input, "%d-%d", &id_min, &id_max))
635     {
636       template.sub.eth.flags.one_tag = 1;
637       template.sub.eth.outer_vlan_id = id_min;
638       template.sub.eth.flags.exact_match = 1;
639       if (id_min > id_max)
640         goto id_error;
641     }
642   else if (unformat (input, "%d", &id_min))
643     {
644       id_max = id_min;
645       template.sub.eth.flags.one_tag = 1;
646       template.sub.eth.outer_vlan_id = id_min;
647       template.sub.eth.flags.exact_match = 1;
648     }
649   else
650     {
651     id_error:
652       error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
653                                  format_unformat_error, input);
654       goto done;
655     }
656
657   hi = vnet_get_hw_interface (vnm, hw_if_index);
658
659   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
660     {
661       error =
662         clib_error_return (0,
663                            "not allowed as %v belong to a BondEthernet interface",
664                            hi->name);
665       goto done;
666     }
667
668   for (id = id_min; id <= id_max; id++)
669     {
670       uword *p;
671       vnet_interface_main_t *im = &vnm->interface_main;
672       u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
673       u64 *kp;
674
675       p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
676       if (p)
677         {
678           if (CLIB_DEBUG > 0)
679             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
680                           hi->sw_if_index, id);
681           continue;
682         }
683
684       kp = clib_mem_alloc (sizeof (*kp));
685       *kp = sup_and_sub_key;
686
687       template.type = VNET_SW_INTERFACE_TYPE_SUB;
688       template.sup_sw_if_index = hi->sw_if_index;
689       template.sub.id = id;
690       error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
691       if (error)
692         goto done;
693
694       hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
695       hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
696       vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
697                        vnet_get_main (), sw_if_index);
698     }
699
700 done:
701   return error;
702 }
703
704 /* *INDENT-OFF* */
705 /*?
706  * Create vlan subinterfaces
707  *
708  * @cliexpar
709  * @cliexstart{create sub-interfaces}
710  *
711  * To create a vlan subinterface 11 to process packets on 802.1q VLAN id 11, use:
712  *
713  *  vpp# create sub GigabitEthernet2/0/0 11
714  *
715  * This shorthand is equivalent to:
716  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 11 exact-match
717  *
718  * You can specify a subinterface number that is different from the vlan id:
719  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 100
720  *
721  * You can create qinq and q-in-any interfaces:
722  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200
723  *  vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any
724  *
725  * You can also create dot1ad interfaces:
726  *  vpp# create sub GigabitEthernet2/0/0 11 dot1ad 11
727  *  vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q 200
728  *
729  * Subinterfaces can be configured as either exact-match or non-exact match.
730  * Non-exact match is the CLI default. If exact-match is specified,
731  * packets must have the same number of vlan tags as the configuration.
732  * For non-exact-match, packets must at least that number of tags.
733  * L3 (routed) interfaces must be configured as exact-match.
734  * L2 interfaces are typically configured as non-exact-match.
735  *
736  * For example, a packet with outer vlan 100 and inner 200 would match this interface:
737  *  vpp# create sub GigabitEthernet2/0/0 5 dot1q 100
738  *
739  * but would not match this interface:
740  *  vpp# create sub GigabitEthernet2/0/0 5 dot1q 100 exact-match
741  *
742  * There are two special subinterfaces that can be configured. Subinterface untagged has no vlan tags:
743  *  vpp# create sub GigabitEthernet2/0/0 5 untagged
744  *
745  * The subinterface default matches any packet that does not match any other subinterface:
746  *  vpp# create sub GigabitEthernet2/0/0 7 default
747  * @cliexend
748  ?*/
749 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
750   .path = "create sub-interfaces",
751   .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
752   .function = create_sub_interfaces,
753 };
754 /* *INDENT-ON* */
755
756 static clib_error_t *
757 set_state (vlib_main_t * vm,
758            unformat_input_t * input, vlib_cli_command_t * cmd)
759 {
760   vnet_main_t *vnm = vnet_get_main ();
761   clib_error_t *error;
762   u32 sw_if_index, flags;
763
764   sw_if_index = ~0;
765   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
766     {
767       error = clib_error_return (0, "unknown interface `%U'",
768                                  format_unformat_error, input);
769       goto done;
770     }
771
772   if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
773     {
774       error = clib_error_return (0, "unknown flags `%U'",
775                                  format_unformat_error, input);
776       goto done;
777     }
778
779   error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
780   if (error)
781     goto done;
782
783 done:
784   return error;
785 }
786
787
788 /* *INDENT-OFF* */
789 /*?
790  * Interface admin up/down
791  *
792  * @cliexpar
793  * @cliexstart{set interface state}
794  *  vpp# set interface state GigabitEthernet2/0/0 up
795  *  vpp# set interface state GigabitEthernet2/0/0 down
796  * @cliexend
797  ?*/
798 VLIB_CLI_COMMAND (set_state_command, static) = {
799   .path = "set interface state",
800   .short_help = "Set interface state",
801   .function = set_state,
802 };
803 /* *INDENT-ON* */
804
805 static clib_error_t *
806 set_unnumbered (vlib_main_t * vm,
807                 unformat_input_t * input, vlib_cli_command_t * cmd)
808 {
809   vnet_main_t *vnm = vnet_get_main ();
810   u32 unnumbered_sw_if_index;
811   u32 inherit_from_sw_if_index;
812   vnet_sw_interface_t *si;
813   int is_set = 0;
814   int is_del = 0;
815
816   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
817     {
818
819       if (unformat (input, "%U use %U",
820                     unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
821                     unformat_vnet_sw_interface, vnm,
822                     &inherit_from_sw_if_index))
823         is_set = 1;
824       else if (unformat (input, "del %U",
825                          unformat_vnet_sw_interface,
826                          vnm, &unnumbered_sw_if_index))
827         is_del = 1;
828       else
829         {
830           if (is_set || is_del)
831             break;
832           else
833             return clib_error_return
834               (0, "parse error '%U'", format_unformat_error, input);
835         }
836     }
837
838   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
839   if (is_del)
840     {
841       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
842       si->unnumbered_sw_if_index = (u32) ~ 0;
843     }
844   else
845     {
846       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
847       si->unnumbered_sw_if_index = inherit_from_sw_if_index;
848     }
849
850   return 0;
851 }
852
853 /* *INDENT-OFF* */
854 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
855   .path = "set interface unnumbered",
856   .short_help = "set interface unnumbered [<intfc> use <intfc>][del <intfc>]",
857   .function = set_unnumbered,
858 };
859 /* *INDENT-ON* */
860
861
862
863 static clib_error_t *
864 set_hw_class (vlib_main_t * vm,
865               unformat_input_t * input, vlib_cli_command_t * cmd)
866 {
867   vnet_main_t *vnm = vnet_get_main ();
868   vnet_interface_main_t *im = &vnm->interface_main;
869   clib_error_t *error;
870   u32 hw_if_index, hw_class_index;
871
872   hw_if_index = ~0;
873   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
874     {
875       error = clib_error_return (0, "unknown hardware interface `%U'",
876                                  format_unformat_error, input);
877       goto done;
878     }
879
880   if (!unformat_user (input, unformat_hash_string,
881                       im->hw_interface_class_by_name, &hw_class_index))
882     {
883       error = clib_error_return (0, "unknown hardware class `%U'",
884                                  format_unformat_error, input);
885       goto done;
886     }
887
888   error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
889   if (error)
890     goto done;
891
892 done:
893   return error;
894 }
895
896 /* *INDENT-OFF* */
897 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
898   .path = "set interface hw-class",
899   .short_help = "Set interface hardware class",
900   .function = set_hw_class,
901 };
902 /* *INDENT-ON* */
903
904 static clib_error_t *
905 vnet_interface_cli_init (vlib_main_t * vm)
906 {
907   return 0;
908 }
909
910 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
911
912 static clib_error_t *
913 renumber_interface_command_fn (vlib_main_t * vm,
914                                unformat_input_t * input,
915                                vlib_cli_command_t * cmd)
916 {
917   u32 hw_if_index;
918   u32 new_dev_instance;
919   vnet_main_t *vnm = vnet_get_main ();
920   int rv;
921
922   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
923     return clib_error_return (0, "unknown hardware interface `%U'",
924                               format_unformat_error, input);
925
926   if (!unformat (input, "%d", &new_dev_instance))
927     return clib_error_return (0, "new dev instance missing");
928
929   rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
930
931   switch (rv)
932     {
933     case 0:
934       break;
935
936     default:
937       return clib_error_return (0, "vnet_interface_name_renumber returned %d",
938                                 rv);
939
940     }
941
942   return 0;
943 }
944
945
946 /* *INDENT-OFF* */
947 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
948   .path = "renumber interface",
949   .short_help = "renumber interface <if-name> <new-dev-instance>",
950   .function = renumber_interface_command_fn,
951 };
952 /* *INDENT-ON* */
953
954 static clib_error_t *
955 promiscuous_cmd (vlib_main_t * vm,
956                  unformat_input_t * input, vlib_cli_command_t * cmd)
957 {
958   vnet_main_t *vnm = vnet_get_main ();
959   u32 hw_if_index;
960   u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
961   ethernet_main_t *em = &ethernet_main;
962   ethernet_interface_t *eif;
963
964   if (unformat (input, "on %U",
965                 unformat_vnet_hw_interface, vnm, &hw_if_index))
966     ;
967   else if (unformat (input, "off %U",
968                      unformat_ethernet_interface, vnm, &hw_if_index))
969     flags = 0;
970   else
971     return clib_error_return (0, "unknown input `%U'",
972                               format_unformat_error, input);
973
974   eif = ethernet_get_interface (em, hw_if_index);
975   if (!eif)
976     return clib_error_return (0, "not supported");
977
978   ethernet_set_flags (vnm, hw_if_index, flags);
979   return 0;
980 }
981
982 /* *INDENT-OFF* */
983 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
984   .path = "set interface promiscuous",
985   .short_help = "set interface promiscuous [on | off] <intfc>",
986   .function = promiscuous_cmd,
987 };
988 /* *INDENT-ON* */
989
990 static clib_error_t *
991 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
992 {
993   vnet_main_t *vnm = vnet_get_main ();
994   u32 hw_if_index, mtu;
995   u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
996   ethernet_main_t *em = &ethernet_main;
997
998   if (unformat (input, "%d %U", &mtu,
999                 unformat_vnet_hw_interface, vnm, &hw_if_index))
1000     {
1001       vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1002       ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1003
1004       if (!eif)
1005         return clib_error_return (0, "not supported");
1006
1007       if (mtu < hi->min_supported_packet_bytes)
1008         return clib_error_return (0, "Invalid mtu (%d): "
1009                                   "must be >= min pkt bytes (%d)", mtu,
1010                                   hi->min_supported_packet_bytes);
1011
1012       if (mtu > hi->max_supported_packet_bytes)
1013         return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1014                                   hi->max_supported_packet_bytes);
1015
1016       if (hi->max_packet_bytes != mtu)
1017         {
1018           hi->max_packet_bytes = mtu;
1019           ethernet_set_flags (vnm, hw_if_index, flags);
1020         }
1021     }
1022   else
1023     return clib_error_return (0, "unknown input `%U'",
1024                               format_unformat_error, input);
1025   return 0;
1026 }
1027
1028 /* *INDENT-OFF* */
1029 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1030   .path = "set interface mtu",
1031   .short_help = "set interface mtu <value> <intfc>",
1032   .function = mtu_cmd,
1033 };
1034 /* *INDENT-ON* */
1035
1036 /*
1037  * fd.io coding-style-patch-verification: ON
1038  *
1039  * Local Variables:
1040  * eval: (c-set-style "gnu")
1041  * End:
1042  */