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