interface: Allow VLAN tag-rewrite on non-sub-interfaces too.
[vpp.git] / src / 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  * @brief Interface CLI.
43  *
44  * Source code for several CLI interface commands.
45  *
46  */
47
48 #include <vnet/vnet.h>
49 #include <vnet/ip/ip.h>
50 #include <vppinfra/bitmap.h>
51 #include <vnet/fib/ip4_fib.h>
52 #include <vnet/fib/ip6_fib.h>
53 #include <vnet/l2/l2_output.h>
54 #include <vnet/l2/l2_input.h>
55
56 static int
57 compare_interface_names (void *a1, void *a2)
58 {
59   u32 *hi1 = a1;
60   u32 *hi2 = a2;
61
62   return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
63 }
64
65 static clib_error_t *
66 show_or_clear_hw_interfaces (vlib_main_t * vm,
67                              unformat_input_t * input,
68                              vlib_cli_command_t * cmd)
69 {
70   clib_error_t *error = 0;
71   vnet_main_t *vnm = vnet_get_main ();
72   vnet_interface_main_t *im = &vnm->interface_main;
73   vnet_hw_interface_t *hi;
74   u32 hw_if_index, *hw_if_indices = 0;
75   int i, verbose = -1, is_show, show_bond = 0;
76
77   is_show = strstr (cmd->path, "show") != 0;
78   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
79     {
80       /* See if user wants to show a specific interface. */
81       if (unformat
82           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
83         vec_add1 (hw_if_indices, hw_if_index);
84
85       /* See if user wants to show an interface with a specific hw_if_index. */
86       else if (unformat (input, "%u", &hw_if_index))
87         vec_add1 (hw_if_indices, hw_if_index);
88
89       else if (unformat (input, "verbose"))
90         verbose = 1;            /* this is also the default */
91
92       else if (unformat (input, "detail"))
93         verbose = 2;
94
95       else if (unformat (input, "brief"))
96         verbose = 0;
97
98       else if (unformat (input, "bond"))
99         {
100           show_bond = 1;
101           if (verbose < 0)
102             verbose = 0;        /* default to brief for link bonding */
103         }
104
105       else
106         {
107           error = clib_error_return (0, "unknown input `%U'",
108                                      format_unformat_error, input);
109           goto done;
110         }
111     }
112
113   /* Gather interfaces. */
114   if (vec_len (hw_if_indices) == 0)
115     pool_foreach (hi, im->hw_interfaces,
116                   vec_add1 (hw_if_indices, hi - im->hw_interfaces));
117
118   if (verbose < 0)
119     verbose = 1;                /* default to verbose (except bond) */
120
121   if (is_show)
122     {
123       /* Sort by name. */
124       vec_sort_with_function (hw_if_indices, compare_interface_names);
125
126       vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
127       for (i = 0; i < vec_len (hw_if_indices); i++)
128         {
129           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
130           if (show_bond == 0)   /* show all interfaces */
131             vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
132                              hi, verbose);
133           else if ((hi->bond_info) &&
134                    (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
135             {                   /* show only bonded interface and all its slave interfaces */
136               int hw_idx;
137               vnet_hw_interface_t *shi;
138               vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
139                                hi, verbose);
140
141               /* *INDENT-OFF* */
142               clib_bitmap_foreach (hw_idx, hi->bond_info,
143               ({
144                 shi = vnet_get_hw_interface(vnm, hw_idx);
145                 vlib_cli_output (vm, "%U\n",
146                                  format_vnet_hw_interface, vnm, shi, verbose);
147               }));
148               /* *INDENT-ON* */
149             }
150         }
151     }
152   else
153     {
154       for (i = 0; i < vec_len (hw_if_indices); i++)
155         {
156           vnet_device_class_t *dc;
157
158           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
159           dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
160
161           if (dc->clear_counters)
162             dc->clear_counters (hi->dev_instance);
163         }
164     }
165
166 done:
167   vec_free (hw_if_indices);
168   return error;
169 }
170
171 /*?
172  * Display more detailed information about all or a list of given interfaces.
173  * The verboseness of the output can be controlled by the following optional
174  * parameters:
175  * - brief: Only show name, index and state (default for bonded interfaces).
176  * - verbose: Also display additional attributes (default for all other interfaces).
177  * - detail: Also display all remaining attributes and extended statistics.
178  *
179  * To limit the output of the command to bonded interfaces and their slave
180  * interfaces, use the '<em>bond</em>' optional parameter.
181  *
182  * @cliexpar
183  * Example of how to display default data for all interfaces:
184  * @cliexstart{show hardware-interfaces}
185  *               Name                Idx   Link  Hardware
186  * GigabitEthernet7/0/0               1     up   GigabitEthernet7/0/0
187  *   Ethernet address ec:f4:bb:c0:bc:fc
188  *   Intel e1000
189  *     carrier up full duplex speed 1000 mtu 9216
190  *     rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
191  *     cpu socket 0
192  * GigabitEthernet7/0/1               2     up   GigabitEthernet7/0/1
193  *   Ethernet address ec:f4:bb:c0:bc:fd
194  *   Intel e1000
195  *     carrier up full duplex speed 1000 mtu 9216
196  *     rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
197  *     cpu socket 0
198  * VirtualEthernet0/0/0               3     up   VirtualEthernet0/0/0
199  *   Ethernet address 02:fe:a5:a9:8b:8e
200  * VirtualEthernet0/0/1               4     up   VirtualEthernet0/0/1
201  *   Ethernet address 02:fe:c0:4e:3b:b0
202  * VirtualEthernet0/0/2               5     up   VirtualEthernet0/0/2
203  *   Ethernet address 02:fe:1f:73:92:81
204  * VirtualEthernet0/0/3               6     up   VirtualEthernet0/0/3
205  *   Ethernet address 02:fe:f2:25:c4:68
206  * local0                             0    down  local0
207  *   local
208  * @cliexend
209  * Example of how to display '<em>verbose</em>' data for an interface by name and
210  * software index (where 2 is the software index):
211  * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
212  *               Name                Idx   Link  Hardware
213  * GigabitEthernet7/0/0               1     up   GigabitEthernet7/0/0
214  *   Ethernet address ec:f4:bb:c0:bc:fc
215  *   Intel e1000
216  *     carrier up full duplex speed 1000 mtu 9216
217  *     rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
218  *     cpu socket 0
219  * GigabitEthernet7/0/1               2    down  GigabitEthernet7/0/1
220  *   Ethernet address ec:f4:bb:c0:bc:fd
221  *   Intel e1000
222  *     carrier up full duplex speed 1000 mtu 9216
223  *     rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
224  *     cpu socket 0
225  * @cliexend
226  ?*/
227 /* *INDENT-OFF* */
228 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
229   .path = "show hardware-interfaces",
230   .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
231     "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
232   .function = show_or_clear_hw_interfaces,
233 };
234 /* *INDENT-ON* */
235
236
237 /*?
238  * Clear the extended statistics for all or a list of given interfaces
239  * (statistics associated with the '<em>show hardware-interfaces</em>' command).
240  *
241  * @cliexpar
242  * Example of how to clear the extended statistics for all interfaces:
243  * @cliexcmd{clear hardware-interfaces}
244  * Example of how to clear the extended statistics for an interface by
245  * name and software index (where 2 is the software index):
246  * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
247  ?*/
248 /* *INDENT-OFF* */
249 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
250   .path = "clear hardware-interfaces",
251   .short_help = "clear hardware-interfaces "
252     "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
253   .function = show_or_clear_hw_interfaces,
254 };
255 /* *INDENT-ON* */
256
257 static int
258 sw_interface_name_compare (void *a1, void *a2)
259 {
260   vnet_sw_interface_t *si1 = a1;
261   vnet_sw_interface_t *si2 = a2;
262
263   return vnet_sw_interface_compare (vnet_get_main (),
264                                     si1->sw_if_index, si2->sw_if_index);
265 }
266
267 static clib_error_t *
268 show_sw_interfaces (vlib_main_t * vm,
269                     unformat_input_t * input, vlib_cli_command_t * cmd)
270 {
271   clib_error_t *error = 0;
272   vnet_main_t *vnm = vnet_get_main ();
273   unformat_input_t _linput, *linput = &_linput;
274   vnet_interface_main_t *im = &vnm->interface_main;
275   vnet_sw_interface_t *si, *sorted_sis = 0;
276   u32 sw_if_index = ~(u32) 0;
277   u8 show_addresses = 0;
278   u8 show_features = 0;
279   u8 show_tag = 0;
280   u8 show_vtr = 0;
281   int verbose = 0;
282
283   /*
284    * Get a line of input. Won't work if the user typed
285    * "show interface" and nothing more.
286    */
287   if (unformat_user (input, unformat_line_input, linput))
288     {
289       while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
290         {
291           /* See if user wants to show specific interface */
292           if (unformat
293               (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
294             {
295               si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
296               vec_add1 (sorted_sis, si[0]);
297             }
298           else if (unformat (linput, "address") || unformat (linput, "addr"))
299             show_addresses = 1;
300           else if (unformat (linput, "features") || unformat (linput, "feat"))
301             show_features = 1;
302           else if (unformat (linput, "tag"))
303             show_tag = 1;
304           else if (unformat (linput, "vtr"))
305             show_vtr = 1;
306           else if (unformat (linput, "verbose"))
307             verbose = 1;
308           else
309             {
310               vec_free (sorted_sis);
311               error = clib_error_return (0, "unknown input `%U'",
312                                          format_unformat_error, linput);
313               goto done;
314             }
315         }
316       unformat_free (linput);
317     }
318   if (show_features || show_tag || show_vtr)
319     {
320       if (sw_if_index == ~(u32) 0)
321         {
322           vec_free (sorted_sis);
323           return clib_error_return (0, "Interface not specified...");
324         }
325     }
326
327   if (show_features)
328     {
329       vnet_interface_features_show (vm, sw_if_index, verbose);
330
331       l2_input_config_t *l2_input = l2input_intf_config (sw_if_index);
332       u32 fb = l2_input->feature_bitmap;
333       /* intf input features are masked by bridge domain */
334       if (l2_input->bridge)
335         fb &= l2input_bd_config (l2_input->bd_index)->feature_bitmap;
336       vlib_cli_output (vm, "\nl2-input:\n%U", format_l2_input_features, fb,
337                        1);
338
339       l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
340       vlib_cli_output (vm, "\nl2-output:");
341       if (l2_output->out_vtr_flag)
342         vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
343       vlib_cli_output (vm, "%U", format_l2_output_features,
344                        l2_output->feature_bitmap, 1);
345       vec_free (sorted_sis);
346       return 0;
347     }
348   if (show_tag)
349     {
350       u8 *tag;
351       tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
352       vlib_cli_output (vm, "%U: %s",
353                        format_vnet_sw_if_index_name, vnm, sw_if_index,
354                        tag ? (char *) tag : "(none)");
355       vec_free (sorted_sis);
356       return 0;
357     }
358
359   /*
360    * Show vlan tag rewrite data for one interface.
361    */
362   if (show_vtr)
363     {
364       u32 vtr_op = L2_VTR_DISABLED;
365       u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
366
367       if (l2vtr_get (vm, vnm, sw_if_index,
368                      &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
369         {
370           vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
371                            format_vnet_sw_if_index_name, vnm, sw_if_index);
372           return 0;
373         }
374       vlib_cli_output (vm, "%U:  VTR %0U",
375                        format_vnet_sw_if_index_name, vnm, sw_if_index,
376                        format_vtr, vtr_op, push_dot1q, tag1, tag2);
377       return 0;
378     }
379
380   if (!show_addresses)
381     vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
382
383   if (vec_len (sorted_sis) == 0)        /* Get all interfaces */
384     {
385       /* Gather interfaces. */
386       sorted_sis =
387         vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
388       _vec_len (sorted_sis) = 0;
389       /* *INDENT-OFF* */
390       pool_foreach (si, im->sw_interfaces,
391       ({
392         int visible = vnet_swif_is_api_visible (si);
393         if (visible)
394           vec_add1 (sorted_sis, si[0]);}
395         ));
396       /* *INDENT-ON* */
397       /* Sort by name. */
398       vec_sort_with_function (sorted_sis, sw_interface_name_compare);
399     }
400
401   if (show_addresses)
402     {
403       vec_foreach (si, sorted_sis)
404       {
405         ip4_main_t *im4 = &ip4_main;
406         ip6_main_t *im6 = &ip6_main;
407         ip_lookup_main_t *lm4 = &im4->lookup_main;
408         ip_lookup_main_t *lm6 = &im6->lookup_main;
409         ip_interface_address_t *ia = 0;
410         u32 fib_index4 = 0, fib_index6 = 0;
411
412         if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
413           fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
414                                 si->sw_if_index);
415
416         if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
417           fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
418                                 si->sw_if_index);
419
420         ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
421         ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
422
423         if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
424           vlib_cli_output
425             (vm, "%U (%s): \n  unnumbered, use %U",
426              format_vnet_sw_if_index_name, vnm, si->sw_if_index,
427              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
428              format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
429         else
430           vlib_cli_output
431             (vm, "%U (%s):",
432              format_vnet_sw_if_index_name, vnm, si->sw_if_index,
433              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
434
435         /* Display any L2 info */
436         l2_input_config_t *l2_input = l2input_intf_config (si->sw_if_index);
437         if (l2_input->bridge)
438           {
439             bd_main_t *bdm = &bd_main;
440             u32 bd_id = l2input_main.bd_configs[l2_input->bd_index].bd_id;
441             vlib_cli_output (vm, "  L2 bridge bd-id %d idx %d shg %d %s",
442                              bd_id, bd_find_index (bdm, bd_id), l2_input->shg,
443                              l2_input->bvi ? "bvi" : " ");
444           }
445         else if (l2_input->xconnect)
446           vlib_cli_output (vm, "  L2 xconnect %U",
447                            format_vnet_sw_if_index_name, vnm,
448                            l2_input->output_sw_if_index);
449
450         /* *INDENT-OFF* */
451         /* Display any IP4 addressing info */
452         foreach_ip_interface_address (lm4, ia, si->sw_if_index,
453                                       1 /* honor unnumbered */,
454         ({
455           ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
456           if (fib4->table_id)
457             vlib_cli_output (vm, "  L3 %U/%d ip4 table-id %d fib-idx %d",
458                              format_ip4_address, r4, ia->address_length,
459                              fib4->table_id,
460                              ip4_fib_index_from_table_id (fib4->table_id));
461           else
462             vlib_cli_output (vm, "  L3 %U/%d",
463                              format_ip4_address, r4, ia->address_length);
464         }));
465         /* *INDENT-ON* */
466
467         /* *INDENT-OFF* */
468         /* Display any IP6 addressing info */
469         foreach_ip_interface_address (lm6, ia, si->sw_if_index,
470                                       1 /* honor unnumbered */,
471         ({
472           ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
473           if (fib6->table_id)
474             vlib_cli_output (vm, "  L3 %U/%d ip6 table-id %d fib-idx %d",
475                              format_ip6_address, r6, ia->address_length,
476                              fib6->table_id,
477                              ip6_fib_index_from_table_id (fib6->table_id));
478           else
479             vlib_cli_output (vm, "  L3 %U/%d",
480                              format_ip6_address, r6, ia->address_length);
481         }));
482         /* *INDENT-ON* */
483       }
484     }
485   else
486     {
487       vec_foreach (si, sorted_sis)
488       {
489         vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
490       }
491     }
492
493 done:
494   vec_free (sorted_sis);
495   return error;
496 }
497
498 /* *INDENT-OFF* */
499 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
500   .path = "show interface",
501   .short_help = "show interface [address|addr|features|feat|vtr] [<interface> [<interface> [..]]] [verbose]",
502   .function = show_sw_interfaces,
503   .is_mp_safe = 1,
504 };
505 /* *INDENT-ON* */
506
507 /* Root of all interface commands. */
508 /* *INDENT-OFF* */
509 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
510   .path = "interface",
511   .short_help = "Interface commands",
512 };
513 /* *INDENT-ON* */
514
515 /* *INDENT-OFF* */
516 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
517   .path = "set interface",
518   .short_help = "Interface commands",
519 };
520 /* *INDENT-ON* */
521
522 static clib_error_t *
523 clear_interface_counters (vlib_main_t * vm,
524                           unformat_input_t * input, vlib_cli_command_t * cmd)
525 {
526   vnet_main_t *vnm = vnet_get_main ();
527   vnet_interface_main_t *im = &vnm->interface_main;
528   vlib_simple_counter_main_t *sm;
529   vlib_combined_counter_main_t *cm;
530   int j, n_counters;
531
532   n_counters = vec_len (im->combined_sw_if_counters);
533
534   for (j = 0; j < n_counters; j++)
535     {
536       im = &vnm->interface_main;
537       cm = im->combined_sw_if_counters + j;
538       vlib_clear_combined_counters (cm);
539     }
540
541   n_counters = vec_len (im->sw_if_counters);
542
543   for (j = 0; j < n_counters; j++)
544     {
545       im = &vnm->interface_main;
546       sm = im->sw_if_counters + j;
547       vlib_clear_simple_counters (sm);
548     }
549
550   return 0;
551 }
552
553 /*?
554  * Clear the statistics for all interfaces (statistics associated with the
555  * '<em>show interface</em>' command).
556  *
557  * @cliexpar
558  * Example of how to clear the statistics for all interfaces:
559  * @cliexcmd{clear interfaces}
560  ?*/
561 /* *INDENT-OFF* */
562 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
563   .path = "clear interfaces",
564   .short_help = "clear interfaces",
565   .function = clear_interface_counters,
566 };
567 /* *INDENT-ON* */
568
569 /**
570  * Parse subinterface names.
571  *
572  * The following subinterface syntax is supported. The first two are for
573  * backwards compatability:
574  *
575  * <intf-name> <id>
576  *     - a subinterface with the name <intf-name>.<id>. The subinterface
577  *       is a single dot1q vlan with vlan id <id> and exact-match semantics.
578  *
579  * <intf-name> <min_id>-<max_id>
580  *     - a set of the above subinterfaces, repeating for each id
581  *       in the range <min_id> to <max_id>
582  *
583  * In the following, exact-match semantics (i.e. the number of vlan tags on the
584  * packet must match the number of tags in the configuration) are used only if
585  * the keyword exact-match is present. Non-exact match is the default.
586  *
587  * <intf-name> <id> dot1q <outer_id> [exact-match]
588  *     - a subinterface with the name <intf-name>.<id>. The subinterface
589  *       is a single dot1q vlan with vlan id <outer_id>.
590  *
591  * <intf-name> <id> dot1q any [exact-match]
592  *     - a subinterface with the name <intf-name>.<id>. The subinterface
593  *       is a single dot1q vlan with any vlan id.
594  *
595  * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
596  *     - a subinterface with the name <intf-name>.<id>. The subinterface
597  *       is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
598  *       <inner_id>.
599  *
600  * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
601  *     - a subinterface with the name <intf-name>.<id>. The subinterface
602  *       is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
603  *
604  * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
605  *
606  *     - a subinterface with the name <intf-name>.<id>. The subinterface
607  *       is a double dot1q vlan with any outer vlan id and any inner vlan id.
608  *
609  * For each of the above CLI, there is a duplicate that uses the keyword
610  * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
611  * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
612  * tagged packets the inner ethertype is always 0x8100. Also note that
613  * the dot1q and dot1ad naming spaces are independent, so it is legal to
614  * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
615  *
616  * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
617  *     - a subinterface with the name <intf-name>.<id>. The subinterface
618  *       is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
619  *       id <inner_id>.
620  *
621  * <intf-name> <id> untagged
622  *     - a subinterface with the name <intf-name>.<id>. The subinterface
623  *       has no vlan tags. Only one can be specified per interface.
624  *
625  * <intf-name> <id> default
626  *     - a subinterface with the name <intf-name>.<id>. This is associated
627  *       with a packet that did not match any other configured subinterface
628  *       on this interface. Only one can be specified per interface.
629  */
630
631 static clib_error_t *
632 parse_vlan_sub_interfaces (unformat_input_t * input,
633                            vnet_sw_interface_t * template)
634 {
635   clib_error_t *error = 0;
636   u32 inner_vlan, outer_vlan;
637
638   if (unformat (input, "any inner-dot1q any"))
639     {
640       template->sub.eth.flags.two_tags = 1;
641       template->sub.eth.flags.outer_vlan_id_any = 1;
642       template->sub.eth.flags.inner_vlan_id_any = 1;
643     }
644   else if (unformat (input, "any"))
645     {
646       template->sub.eth.flags.one_tag = 1;
647       template->sub.eth.flags.outer_vlan_id_any = 1;
648     }
649   else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
650     {
651       template->sub.eth.flags.two_tags = 1;
652       template->sub.eth.flags.inner_vlan_id_any = 1;
653       template->sub.eth.outer_vlan_id = outer_vlan;
654     }
655   else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
656     {
657       template->sub.eth.flags.two_tags = 1;
658       template->sub.eth.outer_vlan_id = outer_vlan;
659       template->sub.eth.inner_vlan_id = inner_vlan;
660     }
661   else if (unformat (input, "%d", &outer_vlan))
662     {
663       template->sub.eth.flags.one_tag = 1;
664       template->sub.eth.outer_vlan_id = outer_vlan;
665     }
666   else
667     {
668       error = clib_error_return (0, "expected dot1q config, got `%U'",
669                                  format_unformat_error, input);
670       goto done;
671     }
672
673   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
674     {
675       if (unformat (input, "exact-match"))
676         {
677           template->sub.eth.flags.exact_match = 1;
678         }
679     }
680
681 done:
682   return error;
683 }
684
685 static clib_error_t *
686 create_sub_interfaces (vlib_main_t * vm,
687                        unformat_input_t * input, vlib_cli_command_t * cmd)
688 {
689   vnet_main_t *vnm = vnet_get_main ();
690   clib_error_t *error = 0;
691   u32 hw_if_index, sw_if_index;
692   vnet_hw_interface_t *hi;
693   u32 id, id_min, id_max;
694   vnet_sw_interface_t template;
695
696   hw_if_index = ~0;
697   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
698     {
699       error = clib_error_return (0, "unknown interface `%U'",
700                                  format_unformat_error, input);
701       goto done;
702     }
703
704   clib_memset (&template, 0, sizeof (template));
705   template.sub.eth.raw_flags = 0;
706
707   if (unformat (input, "%d default", &id_min))
708     {
709       id_max = id_min;
710       template.sub.eth.flags.default_sub = 1;
711     }
712   else if (unformat (input, "%d untagged", &id_min))
713     {
714       id_max = id_min;
715       template.sub.eth.flags.no_tags = 1;
716       template.sub.eth.flags.exact_match = 1;
717     }
718   else if (unformat (input, "%d dot1q", &id_min))
719     {
720       /* parse dot1q config */
721       id_max = id_min;
722       error = parse_vlan_sub_interfaces (input, &template);
723       if (error)
724         goto done;
725     }
726   else if (unformat (input, "%d dot1ad", &id_min))
727     {
728       /* parse dot1ad config */
729       id_max = id_min;
730       template.sub.eth.flags.dot1ad = 1;
731       error = parse_vlan_sub_interfaces (input, &template);
732       if (error)
733         goto done;
734     }
735   else if (unformat (input, "%d-%d", &id_min, &id_max))
736     {
737       template.sub.eth.flags.one_tag = 1;
738       template.sub.eth.flags.exact_match = 1;
739       if (id_min > id_max)
740         goto id_error;
741     }
742   else if (unformat (input, "%d", &id_min))
743     {
744       id_max = id_min;
745       template.sub.eth.flags.one_tag = 1;
746       template.sub.eth.outer_vlan_id = id_min;
747       template.sub.eth.flags.exact_match = 1;
748     }
749   else
750     {
751     id_error:
752       error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
753                                  format_unformat_error, input);
754       goto done;
755     }
756
757   hi = vnet_get_hw_interface (vnm, hw_if_index);
758
759   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
760     {
761       error =
762         clib_error_return (0,
763                            "not allowed as %v belong to a BondEthernet interface",
764                            hi->name);
765       goto done;
766     }
767
768   for (id = id_min; id <= id_max; id++)
769     {
770       uword *p;
771       vnet_interface_main_t *im = &vnm->interface_main;
772       u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
773       u64 *kp;
774
775       p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
776       if (p)
777         {
778           if (CLIB_DEBUG > 0)
779             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
780                           hi->sw_if_index, id);
781           continue;
782         }
783
784       kp = clib_mem_alloc (sizeof (*kp));
785       *kp = sup_and_sub_key;
786
787       template.type = VNET_SW_INTERFACE_TYPE_SUB;
788       template.flood_class = VNET_FLOOD_CLASS_NORMAL;
789       template.sup_sw_if_index = hi->sw_if_index;
790       template.sub.id = id;
791       if (id_min < id_max)
792         template.sub.eth.outer_vlan_id = id;
793
794       error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
795       if (error)
796         goto done;
797
798       hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
799       hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
800       vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
801                        vnet_get_main (), sw_if_index);
802     }
803
804 done:
805   return error;
806 }
807
808 /*?
809  * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
810  * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
811  * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
812  * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
813  * but this is not recommended.
814  *
815  * This command has several variations:
816  * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
817  * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
818  *
819  * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
820  * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
821  * match any other subinterfaces should be sent to this subinterface.
822  *
823  * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
824  * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
825  * to this subinterface.
826  *
827  * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
828  * subinterfaces to handle a range of VLAN IDs.
829  *
830  * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
831  * Use this command to specify the outer VLAN ID, to either be explicit or to make the
832  * VLAN ID different from the '<em>subId</em>'.
833  *
834  * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
835  * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
836  * the inner VLAN ID.
837  *
838  * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
839  * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
840  * default. If '<em>exact-match</em>' is specified, packets must have the same number of
841  * VLAN tags as the configuration. For non-exact-match, packets must at least that number
842  * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
843  * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
844  * entered, then the default behavior is exact-match.
845  *
846  * Use the '<em>show interface</em>' command to display all subinterfaces.
847  *
848  * @cliexpar
849  * @parblock
850  * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
851  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
852  *
853  * The previous example is shorthand and is equivalent to:
854  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
855  *
856  *
857  * Example of how to create a subinterface number that is different from the VLAN ID:
858  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
859  *
860  *
861  * Examples of how to create q-in-q and q-in-any subinterfaces:
862  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
863  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
864  *
865  * Examples of how to create dot1ad interfaces:
866  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
867  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
868  *
869  *
870  * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
871  * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
872  * is non-exact match:
873  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
874  *
875  * However, the same packet would NOT match this interface because '<em>exact-match</em>'
876  * is specified and only one VLAN is configured, but packet contains two VLANs:
877  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
878  *
879  *
880  * Example of how to created a subinterface to process untagged packets:
881  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
882  *
883  * Example of how to created a subinterface to process any packet with a VLAN ID that
884  * does not match any other subinterface:
885  * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
886  *
887  * When subinterfaces are created, they are in the down state. Example of how to
888  * enable a newly created subinterface:
889  * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
890  * @endparblock
891  ?*/
892 /* *INDENT-OFF* */
893 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
894   .path = "create sub-interfaces",
895   .short_help = "create sub-interfaces <interface> "
896     "{<subId> [default|untagged]} | "
897     "{<subId>-<subId>} | "
898     "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
899   .function = create_sub_interfaces,
900 };
901 /* *INDENT-ON* */
902
903 static clib_error_t *
904 set_state (vlib_main_t * vm,
905            unformat_input_t * input, vlib_cli_command_t * cmd)
906 {
907   vnet_main_t *vnm = vnet_get_main ();
908   clib_error_t *error;
909   u32 sw_if_index, flags;
910
911   sw_if_index = ~0;
912   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
913     {
914       error = clib_error_return (0, "unknown interface `%U'",
915                                  format_unformat_error, input);
916       goto done;
917     }
918
919   if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
920     {
921       error = clib_error_return (0, "unknown flags `%U'",
922                                  format_unformat_error, input);
923       goto done;
924     }
925
926   error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
927   if (error)
928     goto done;
929
930 done:
931   return error;
932 }
933
934
935 /*?
936  * This command is used to change the admin state (up/down) of an interface.
937  *
938  * If an interface is down, the optional '<em>punt</em>' flag can also be set.
939  * The '<em>punt</em>' flag implies the interface is disabled for forwarding
940  * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
941  * '<em>punt</em>' flag (interface is still down).
942  *
943  * @cliexpar
944  * Example of how to configure the admin state of an interface to '<em>up</em?':
945  * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
946  * Example of how to configure the admin state of an interface to '<em>down</em?':
947  * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
948  ?*/
949 /* *INDENT-OFF* */
950 VLIB_CLI_COMMAND (set_state_command, static) = {
951   .path = "set interface state",
952   .short_help = "set interface state <interface> [up|down|punt|enable]",
953   .function = set_state,
954 };
955 /* *INDENT-ON* */
956
957 static clib_error_t *
958 set_unnumbered (vlib_main_t * vm,
959                 unformat_input_t * input, vlib_cli_command_t * cmd)
960 {
961   vnet_main_t *vnm = vnet_get_main ();
962   u32 unnumbered_sw_if_index = ~0;
963   u32 inherit_from_sw_if_index = ~0;
964   int enable = 1;
965
966   if (unformat (input, "%U use %U",
967                 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
968                 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
969     enable = 1;
970   else if (unformat (input, "del %U",
971                      unformat_vnet_sw_interface, vnm,
972                      &unnumbered_sw_if_index))
973     enable = 0;
974   else
975     return clib_error_return (0, "parse error '%U'",
976                               format_unformat_error, input);
977
978   if (~0 == unnumbered_sw_if_index)
979     return clib_error_return (0, "Specify the unnumbered interface");
980   if (enable && ~0 == inherit_from_sw_if_index)
981     return clib_error_return (0, "When enabling unnumbered specify the"
982                               " IP enabled interface that it uses");
983
984   vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
985                                        inherit_from_sw_if_index, enable);
986
987   return (NULL);
988 }
989
990 /* *INDENT-OFF* */
991 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
992   .path = "set interface unnumbered",
993   .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
994   .function = set_unnumbered,
995 };
996 /* *INDENT-ON* */
997
998
999
1000 static clib_error_t *
1001 set_hw_class (vlib_main_t * vm,
1002               unformat_input_t * input, vlib_cli_command_t * cmd)
1003 {
1004   vnet_main_t *vnm = vnet_get_main ();
1005   vnet_interface_main_t *im = &vnm->interface_main;
1006   clib_error_t *error;
1007   u32 hw_if_index, hw_class_index;
1008
1009   hw_if_index = ~0;
1010   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1011     {
1012       error = clib_error_return (0, "unknown hardware interface `%U'",
1013                                  format_unformat_error, input);
1014       goto done;
1015     }
1016
1017   if (!unformat_user (input, unformat_hash_string,
1018                       im->hw_interface_class_by_name, &hw_class_index))
1019     {
1020       error = clib_error_return (0, "unknown hardware class `%U'",
1021                                  format_unformat_error, input);
1022       goto done;
1023     }
1024
1025   error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1026   if (error)
1027     goto done;
1028
1029 done:
1030   return error;
1031 }
1032
1033 /* *INDENT-OFF* */
1034 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1035   .path = "set interface hw-class",
1036   .short_help = "Set interface hardware class",
1037   .function = set_hw_class,
1038 };
1039 /* *INDENT-ON* */
1040
1041 static clib_error_t *
1042 vnet_interface_cli_init (vlib_main_t * vm)
1043 {
1044   return 0;
1045 }
1046
1047 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1048
1049 static clib_error_t *
1050 renumber_interface_command_fn (vlib_main_t * vm,
1051                                unformat_input_t * input,
1052                                vlib_cli_command_t * cmd)
1053 {
1054   u32 hw_if_index;
1055   u32 new_dev_instance;
1056   vnet_main_t *vnm = vnet_get_main ();
1057   int rv;
1058
1059   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1060     return clib_error_return (0, "unknown hardware interface `%U'",
1061                               format_unformat_error, input);
1062
1063   if (!unformat (input, "%d", &new_dev_instance))
1064     return clib_error_return (0, "new dev instance missing");
1065
1066   rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1067
1068   switch (rv)
1069     {
1070     case 0:
1071       break;
1072
1073     default:
1074       return clib_error_return (0, "vnet_interface_name_renumber returned %d",
1075                                 rv);
1076
1077     }
1078
1079   return 0;
1080 }
1081
1082
1083 /* *INDENT-OFF* */
1084 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1085   .path = "renumber interface",
1086   .short_help = "renumber interface <interface> <new-dev-instance>",
1087   .function = renumber_interface_command_fn,
1088 };
1089 /* *INDENT-ON* */
1090
1091 static clib_error_t *
1092 promiscuous_cmd (vlib_main_t * vm,
1093                  unformat_input_t * input, vlib_cli_command_t * cmd)
1094 {
1095   vnet_main_t *vnm = vnet_get_main ();
1096   u32 hw_if_index;
1097   u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
1098   ethernet_main_t *em = &ethernet_main;
1099   ethernet_interface_t *eif;
1100
1101   if (unformat (input, "on %U",
1102                 unformat_vnet_hw_interface, vnm, &hw_if_index))
1103     ;
1104   else if (unformat (input, "off %U",
1105                      unformat_ethernet_interface, vnm, &hw_if_index))
1106     flags = 0;
1107   else
1108     return clib_error_return (0, "unknown input `%U'",
1109                               format_unformat_error, input);
1110
1111   eif = ethernet_get_interface (em, hw_if_index);
1112   if (!eif)
1113     return clib_error_return (0, "not supported");
1114
1115   ethernet_set_flags (vnm, hw_if_index, flags);
1116   return 0;
1117 }
1118
1119 /* *INDENT-OFF* */
1120 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1121   .path = "set interface promiscuous",
1122   .short_help = "set interface promiscuous [on|off] <interface>",
1123   .function = promiscuous_cmd,
1124 };
1125 /* *INDENT-ON* */
1126
1127 static clib_error_t *
1128 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1129 {
1130   vnet_main_t *vnm = vnet_get_main ();
1131   u32 hw_if_index, sw_if_index, mtu;
1132   ethernet_main_t *em = &ethernet_main;
1133   u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
1134
1135   if (unformat (input, "%d %U", &mtu,
1136                 unformat_vnet_hw_interface, vnm, &hw_if_index))
1137     {
1138       /*
1139        * Change physical MTU on interface. Only supported for Ethernet
1140        * interfaces
1141        */
1142       vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1143       ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1144
1145       if (!eif)
1146         return clib_error_return (0, "not supported");
1147
1148       if (mtu < hi->min_supported_packet_bytes)
1149         return clib_error_return (0, "Invalid mtu (%d): "
1150                                   "must be >= min pkt bytes (%d)", mtu,
1151                                   hi->min_supported_packet_bytes);
1152
1153       if (mtu > hi->max_supported_packet_bytes)
1154         return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1155                                   hi->max_supported_packet_bytes);
1156
1157       vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
1158       goto done;
1159     }
1160   else if (unformat (input, "packet %d %U", &mtu,
1161                      unformat_vnet_sw_interface, vnm, &sw_if_index))
1162     /* Set default packet MTU (including L3 header */
1163     mtus[VNET_MTU_L3] = mtu;
1164   else if (unformat (input, "ip4 %d %U", &mtu,
1165                      unformat_vnet_sw_interface, vnm, &sw_if_index))
1166     mtus[VNET_MTU_IP4] = mtu;
1167   else if (unformat (input, "ip6 %d %U", &mtu,
1168                      unformat_vnet_sw_interface, vnm, &sw_if_index))
1169     mtus[VNET_MTU_IP6] = mtu;
1170   else if (unformat (input, "mpls %d %U", &mtu,
1171                      unformat_vnet_sw_interface, vnm, &sw_if_index))
1172     mtus[VNET_MTU_MPLS] = mtu;
1173   else
1174     return clib_error_return (0, "unknown input `%U'",
1175                               format_unformat_error, input);
1176
1177   vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1178
1179 done:
1180   return 0;
1181 }
1182
1183 /* *INDENT-OFF* */
1184 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1185   .path = "set interface mtu",
1186   .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
1187   .function = mtu_cmd,
1188 };
1189 /* *INDENT-ON* */
1190
1191 static clib_error_t *
1192 show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1193                                 vlib_cli_command_t * cmd)
1194 {
1195   vnet_main_t *vnm = vnet_get_main ();
1196   vnet_interface_main_t *im = &vnm->interface_main;
1197   ethernet_main_t *em = &ethernet_main;
1198   u32 sw_if_index = ~0;
1199   vnet_sw_interface_t *si, *sorted_sis = 0;
1200
1201   if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1202     {
1203       si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1204       vec_add1 (sorted_sis, si[0]);
1205     }
1206
1207   /* if an interface name was not passed, get all interfaces */
1208   if (vec_len (sorted_sis) == 0)
1209     {
1210       sorted_sis =
1211         vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1212       _vec_len (sorted_sis) = 0;
1213       /* *INDENT-OFF* */
1214       pool_foreach (si, im->sw_interfaces,
1215       ({
1216         int visible = vnet_swif_is_api_visible (si);
1217         if (visible)
1218           vec_add1 (sorted_sis, si[0]);}
1219         ));
1220       /* *INDENT-ON* */
1221       /* Sort by name. */
1222       vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1223     }
1224
1225   vec_foreach (si, sorted_sis)
1226   {
1227     vnet_sw_interface_t *sup_si;
1228     ethernet_interface_t *ei;
1229
1230     sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1231     ei = ethernet_get_interface (em, sup_si->hw_if_index);
1232
1233     vlib_cli_output (vm, "%U (%s):",
1234                      format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1235                      (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1236                      "up" : "dn");
1237
1238     if (ei && ei->secondary_addrs)
1239       {
1240         mac_address_t *sec_addr;
1241
1242         vec_foreach (sec_addr, ei->secondary_addrs)
1243         {
1244           vlib_cli_output (vm, "  %U", format_mac_address_t, sec_addr);
1245         }
1246       }
1247   }
1248
1249   vec_free (sorted_sis);
1250   return 0;
1251 }
1252
1253 /*?
1254  * This command is used to display interface secondary mac addresses.
1255  *
1256  * @cliexpar
1257  * Example of how to display interface secondary mac addresses:
1258  * @cliexstart{show interface secondary-mac-address}
1259  * @cliexend
1260 ?*/
1261 /* *INDENT-OFF* */
1262 VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1263   .path = "show interface secondary-mac-address",
1264   .short_help = "show interface secondary-mac-address [<interface>]",
1265   .function = show_interface_sec_mac_addr_fn,
1266 };
1267 /* *INDENT-ON* */
1268
1269 static clib_error_t *
1270 interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1271                                vlib_cli_command_t * cmd)
1272 {
1273   vnet_main_t *vnm = vnet_get_main ();
1274   vnet_sw_interface_t *si = NULL;
1275   clib_error_t *error = 0;
1276   u32 sw_if_index = ~0;
1277   u8 mac[6] = { 0 };
1278   u8 is_add, is_del;
1279
1280   is_add = is_del = 0;
1281
1282   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1283     {
1284       error = clib_error_return (0, "unknown interface `%U'",
1285                                  format_unformat_error, input);
1286       goto done;
1287     }
1288   if (!unformat_user (input, unformat_ethernet_address, mac))
1289     {
1290       error = clib_error_return (0, "expected mac address `%U'",
1291                                  format_unformat_error, input);
1292       goto done;
1293     }
1294
1295   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1296     {
1297       if (unformat (input, "add"))
1298         is_add = 1;
1299       else if (unformat (input, "del"))
1300         is_del = 1;
1301       else
1302         break;
1303     }
1304
1305   if (is_add == is_del)
1306     {
1307       error = clib_error_return (0, "must choose one of add or del");
1308       goto done;
1309     }
1310
1311   si = vnet_get_sw_interface (vnm, sw_if_index);
1312   error =
1313     vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1314
1315 done:
1316   return error;
1317 }
1318
1319 /*?
1320  * The '<em>set interface secondary-mac-address </em>' command allows adding
1321  * or deleting extra MAC addresses on a given interface without changing the
1322  * default MAC address. This could allow packets sent to these MAC addresses
1323  * to be received without setting the interface to promiscuous mode.
1324  * Not all interfaces support this operation. The ones that do are mostly
1325  * hardware NICs, though virtio does also.
1326  *
1327  * @cliexpar
1328  * @parblock
1329  * Example of how to add a secondary MAC Address on an interface:
1330  * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1331  * Example of how to delete a secondary MAC address from an interface:
1332  * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1333  * @endparblock
1334 ?*/
1335 /* *INDENT-OFF* */
1336 VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1337   .path = "set interface secondary-mac-address",
1338   .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1339   .function = interface_add_del_mac_address,
1340 };
1341 /* *INDENT-ON* */
1342
1343 static clib_error_t *
1344 set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1345                            vlib_cli_command_t * cmd)
1346 {
1347   vnet_main_t *vnm = vnet_get_main ();
1348   vnet_sw_interface_t *si = NULL;
1349   clib_error_t *error = 0;
1350   u32 sw_if_index = ~0;
1351   u8 mac[6] = { 0 };
1352
1353   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1354     {
1355       error = clib_error_return (0, "unknown interface `%U'",
1356                                  format_unformat_error, input);
1357       goto done;
1358     }
1359   if (!unformat_user (input, unformat_ethernet_address, mac))
1360     {
1361       error = clib_error_return (0, "expected mac address `%U'",
1362                                  format_unformat_error, input);
1363       goto done;
1364     }
1365   si = vnet_get_sw_interface (vnm, sw_if_index);
1366   error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
1367 done:
1368   return error;
1369 }
1370
1371 /*?
1372  * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1373  * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1374  * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1375  *
1376  * @cliexpar
1377  * @parblock
1378  * Example of how to change MAC Address of interface:
1379  * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1380  * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1381  * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1382  * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1383  * @endparblock
1384 ?*/
1385 /* *INDENT-OFF* */
1386 VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1387   .path = "set interface mac address",
1388   .short_help = "set interface mac address <interface> <mac-address>",
1389   .function = set_interface_mac_address,
1390 };
1391 /* *INDENT-ON* */
1392
1393 static clib_error_t *
1394 set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1395 {
1396   vnet_main_t *vnm = vnet_get_main ();
1397   u32 sw_if_index = ~0;
1398   u8 *tag = 0;
1399
1400   if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1401                  vnm, &sw_if_index, &tag))
1402     return clib_error_return (0, "unknown input `%U'",
1403                               format_unformat_error, input);
1404
1405   vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1406
1407   return 0;
1408 }
1409
1410 /* *INDENT-OFF* */
1411 VLIB_CLI_COMMAND (set_tag_command, static) = {
1412   .path = "set interface tag",
1413   .short_help = "set interface tag <interface> <tag>",
1414   .function = set_tag,
1415 };
1416 /* *INDENT-ON* */
1417
1418 static clib_error_t *
1419 clear_tag (vlib_main_t * vm, unformat_input_t * input,
1420            vlib_cli_command_t * cmd)
1421 {
1422   vnet_main_t *vnm = vnet_get_main ();
1423   u32 sw_if_index = ~0;
1424
1425   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1426     return clib_error_return (0, "unknown input `%U'",
1427                               format_unformat_error, input);
1428
1429   vnet_clear_sw_interface_tag (vnm, sw_if_index);
1430
1431   return 0;
1432 }
1433
1434 /* *INDENT-OFF* */
1435 VLIB_CLI_COMMAND (clear_tag_command, static) = {
1436   .path = "clear interface tag",
1437   .short_help = "clear interface tag <interface>",
1438   .function = clear_tag,
1439 };
1440 /* *INDENT-ON* */
1441
1442 static clib_error_t *
1443 set_ip_directed_broadcast (vlib_main_t * vm,
1444                            unformat_input_t * input, vlib_cli_command_t * cmd)
1445 {
1446   vnet_main_t *vnm = vnet_get_main ();
1447   u32 sw_if_index = ~0;
1448   u8 enable = 0;
1449
1450   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1451   else if (unformat (input, "enable"))
1452     enable = 1;
1453   else if (unformat (input, "disable"))
1454     enable = 0;
1455   else
1456     return clib_error_return (0, "unknown input: `%U'",
1457                               format_unformat_error, input);
1458
1459   if (~0 == sw_if_index)
1460     return clib_error_return (0, "specify an interface: `%U'",
1461                               format_unformat_error, input);
1462
1463   vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1464
1465   return 0;
1466 }
1467
1468 /*?
1469  * This command is used to enable/disable IP directed broadcast
1470  * If directed broadcast is enabled a packet sent to the interface's
1471  * subnet broadcast address will be sent L2 broadcast on the interface,
1472  * otherwise it is dropped.
1473  ?*/
1474 /* *INDENT-OFF* */
1475 VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1476   .path = "set interface ip directed-broadcast",
1477   .short_help = "set interface enable <interface> <enable|disable>",
1478   .function = set_ip_directed_broadcast,
1479 };
1480 /* *INDENT-ON* */
1481
1482 static clib_error_t *
1483 set_hw_interface_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1484                           u32 queue_id, vnet_hw_interface_rx_mode mode)
1485 {
1486   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1487   vnet_device_class_t *dev_class =
1488     vnet_get_device_class (vnm, hw->dev_class_index);
1489   clib_error_t *error;
1490   vnet_hw_interface_rx_mode old_mode;
1491   int rv;
1492
1493   if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
1494     mode = hw->default_rx_mode;
1495
1496   rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &old_mode);
1497   switch (rv)
1498     {
1499     case 0:
1500       if (old_mode == mode)
1501         return 0;               /* same rx-mode, no change */
1502       break;
1503     case VNET_API_ERROR_INVALID_INTERFACE:
1504       return clib_error_return (0, "invalid interface");
1505     case VNET_API_ERROR_INVALID_QUEUE:
1506       return clib_error_return (0, "invalid queue");
1507     default:
1508       return clib_error_return (0, "unknown error");
1509     }
1510
1511   if (dev_class->rx_mode_change_function)
1512     {
1513       error = dev_class->rx_mode_change_function (vnm, hw_if_index, queue_id,
1514                                                   mode);
1515       if (error)
1516         return (error);
1517     }
1518
1519   rv = vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1520   switch (rv)
1521     {
1522     case 0:
1523       break;
1524     case VNET_API_ERROR_UNSUPPORTED:
1525       return clib_error_return (0, "unsupported");
1526     case VNET_API_ERROR_INVALID_INTERFACE:
1527       return clib_error_return (0, "invalid interface");
1528     case VNET_API_ERROR_INVALID_QUEUE:
1529       return clib_error_return (0, "invalid queue");
1530     default:
1531       return clib_error_return (0, "unknown error");
1532     }
1533
1534   return 0;
1535 }
1536
1537 clib_error_t *
1538 set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1539                                  u8 queue_id_valid, u32 queue_id,
1540                                  vnet_hw_interface_rx_mode mode)
1541 {
1542   clib_error_t *error = 0;
1543   vnet_hw_interface_t *hw;
1544   int i;
1545
1546   hw = vnet_get_hw_interface (vnm, hw_if_index);
1547
1548   if (queue_id_valid == 0)
1549     {
1550       for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1551         {
1552           error = set_hw_interface_rx_mode (vnm, hw_if_index, i, mode);
1553           if (error)
1554             break;
1555         }
1556       hw->default_rx_mode = mode;
1557     }
1558   else
1559     error = set_hw_interface_rx_mode (vnm, hw_if_index, queue_id, mode);
1560
1561   return (error);
1562 }
1563
1564 static clib_error_t *
1565 set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1566                        vlib_cli_command_t * cmd)
1567 {
1568   clib_error_t *error = 0;
1569   unformat_input_t _line_input, *line_input = &_line_input;
1570   vnet_main_t *vnm = vnet_get_main ();
1571   u32 hw_if_index = (u32) ~ 0;
1572   u32 queue_id = (u32) ~ 0;
1573   vnet_hw_interface_rx_mode mode = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
1574   u8 queue_id_valid = 0;
1575
1576   if (!unformat_user (input, unformat_line_input, line_input))
1577     return 0;
1578
1579   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1580     {
1581       if (unformat
1582           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1583         ;
1584       else if (unformat (line_input, "queue %d", &queue_id))
1585         queue_id_valid = 1;
1586       else if (unformat (line_input, "polling"))
1587         mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
1588       else if (unformat (line_input, "interrupt"))
1589         mode = VNET_HW_INTERFACE_RX_MODE_INTERRUPT;
1590       else if (unformat (line_input, "adaptive"))
1591         mode = VNET_HW_INTERFACE_RX_MODE_ADAPTIVE;
1592       else
1593         {
1594           error = clib_error_return (0, "parse error: '%U'",
1595                                      format_unformat_error, line_input);
1596           unformat_free (line_input);
1597           return error;
1598         }
1599     }
1600
1601   unformat_free (line_input);
1602
1603   if (hw_if_index == (u32) ~ 0)
1604     return clib_error_return (0, "please specify valid interface name");
1605
1606   if (mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
1607     return clib_error_return (0, "please specify valid rx-mode");
1608
1609   error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1610                                            queue_id, mode);
1611
1612   return (error);
1613 }
1614
1615 /*?
1616  * This command is used to assign the RX packet processing mode (polling,
1617  * interrupt, adaptive) of the a given interface, and optionally a
1618  * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1619  * is applied to all queues of the interface. Not all interfaces support
1620  * all modes. To display the current rx-mode use the command
1621  * '<em>show interface rx-placement</em>'.
1622  *
1623  * @cliexpar
1624  * Example of how to assign rx-mode to all queues on an interface:
1625  * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1626  * Example of how to assign rx-mode to one queue of an interface:
1627  * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1628  * Example of how to display the rx-mode of all interfaces:
1629  * @cliexstart{show interface rx-placement}
1630  * Thread 1 (vpp_wk_0):
1631  *   node dpdk-input:
1632  *     GigabitEthernet7/0/0 queue 0 (polling)
1633  *   node vhost-user-input:
1634  *     VirtualEthernet0/0/12 queue 0 (interrupt)
1635  *     VirtualEthernet0/0/12 queue 2 (polling)
1636  *     VirtualEthernet0/0/13 queue 0 (polling)
1637  *     VirtualEthernet0/0/13 queue 2 (polling)
1638  * Thread 2 (vpp_wk_1):
1639  *   node dpdk-input:
1640  *     GigabitEthernet7/0/1 queue 0 (polling)
1641  *   node vhost-user-input:
1642  *     VirtualEthernet0/0/12 queue 1 (polling)
1643  *     VirtualEthernet0/0/12 queue 3 (polling)
1644  *     VirtualEthernet0/0/13 queue 1 (polling)
1645  *     VirtualEthernet0/0/13 queue 3 (polling)
1646  * @cliexend
1647 ?*/
1648 /* *INDENT-OFF* */
1649 VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1650     .path = "set interface rx-mode",
1651     .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1652     .function = set_interface_rx_mode,
1653 };
1654 /* *INDENT-ON* */
1655
1656 static clib_error_t *
1657 show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1658                                 vlib_cli_command_t * cmd)
1659 {
1660   u8 *s = 0;
1661   vnet_main_t *vnm = vnet_get_main ();
1662   vnet_device_input_runtime_t *rt;
1663   vnet_device_and_queue_t *dq;
1664   vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
1665   uword si;
1666   int index = 0;
1667
1668   /* *INDENT-OFF* */
1669   foreach_vlib_main (({
1670     clib_bitmap_foreach (si, pn->sibling_bitmap,
1671       ({
1672         rt = vlib_node_get_runtime_data (this_vlib_main, si);
1673
1674         if (vec_len (rt->devices_and_queues))
1675           s = format (s, "  node %U:\n", format_vlib_node_name, vm, si);
1676
1677         vec_foreach (dq, rt->devices_and_queues)
1678           {
1679             vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm,
1680                                                              dq->hw_if_index);
1681             s = format (s, "    %U queue %u (%U)\n",
1682                         format_vnet_sw_if_index_name, vnm, hi->sw_if_index,
1683                         dq->queue_id,
1684                         format_vnet_hw_interface_rx_mode, dq->mode);
1685           }
1686       }));
1687     if (vec_len (s) > 0)
1688       {
1689         vlib_cli_output(vm, "Thread %u (%s):\n%v", index,
1690                         vlib_worker_threads[index].name, s);
1691         vec_reset_length (s);
1692       }
1693     index++;
1694   }));
1695   /* *INDENT-ON* */
1696
1697   vec_free (s);
1698   return 0;
1699 }
1700
1701 /*?
1702  * This command is used to display the interface and queue worker
1703  * thread placement.
1704  *
1705  * @cliexpar
1706  * Example of how to display the interface placement:
1707  * @cliexstart{show interface rx-placement}
1708  * Thread 1 (vpp_wk_0):
1709  *   node dpdk-input:
1710  *     GigabitEthernet7/0/0 queue 0 (polling)
1711  *   node vhost-user-input:
1712  *     VirtualEthernet0/0/12 queue 0 (polling)
1713  *     VirtualEthernet0/0/12 queue 2 (polling)
1714  *     VirtualEthernet0/0/13 queue 0 (polling)
1715  *     VirtualEthernet0/0/13 queue 2 (polling)
1716  * Thread 2 (vpp_wk_1):
1717  *   node dpdk-input:
1718  *     GigabitEthernet7/0/1 queue 0 (polling)
1719  *   node vhost-user-input:
1720  *     VirtualEthernet0/0/12 queue 1 (polling)
1721  *     VirtualEthernet0/0/12 queue 3 (polling)
1722  *     VirtualEthernet0/0/13 queue 1 (polling)
1723  *     VirtualEthernet0/0/13 queue 3 (polling)
1724  * @cliexend
1725 ?*/
1726 /* *INDENT-OFF* */
1727 VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1728   .path = "show interface rx-placement",
1729   .short_help = "show interface rx-placement",
1730   .function = show_interface_rx_placement_fn,
1731 };
1732 /* *INDENT-ON* */
1733
1734 clib_error_t *
1735 set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1736                                u32 thread_index, u8 is_main)
1737 {
1738   vnet_main_t *vnm = vnet_get_main ();
1739   vnet_device_main_t *vdm = &vnet_device_main;
1740   clib_error_t *error = 0;
1741   vnet_hw_interface_rx_mode mode = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
1742   int rv;
1743
1744   if (is_main)
1745     thread_index = 0;
1746   else
1747     thread_index += vdm->first_worker_thread_index;
1748
1749   if (thread_index > vdm->last_worker_thread_index)
1750     return clib_error_return (0,
1751                               "please specify valid worker thread or main");
1752
1753   rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &mode);
1754
1755   if (rv)
1756     return clib_error_return (0, "not found");
1757
1758   rv = vnet_hw_interface_unassign_rx_thread (vnm, hw_if_index, queue_id);
1759
1760   if (rv)
1761     return clib_error_return (0, "not found");
1762
1763   vnet_hw_interface_assign_rx_thread (vnm, hw_if_index, queue_id,
1764                                       thread_index);
1765   vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1766
1767   return (error);
1768 }
1769
1770 static clib_error_t *
1771 set_interface_rx_placement (vlib_main_t * vm, unformat_input_t * input,
1772                             vlib_cli_command_t * cmd)
1773 {
1774   clib_error_t *error = 0;
1775   unformat_input_t _line_input, *line_input = &_line_input;
1776   vnet_main_t *vnm = vnet_get_main ();
1777   u32 hw_if_index = (u32) ~ 0;
1778   u32 queue_id = (u32) 0;
1779   u32 thread_index = (u32) ~ 0;
1780   u8 is_main = 0;
1781
1782   if (!unformat_user (input, unformat_line_input, line_input))
1783     return 0;
1784
1785   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1786     {
1787       if (unformat
1788           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1789         ;
1790       else if (unformat (line_input, "queue %d", &queue_id))
1791         ;
1792       else if (unformat (line_input, "main", &thread_index))
1793         is_main = 1;
1794       else if (unformat (line_input, "worker %d", &thread_index))
1795         ;
1796       else
1797         {
1798           error = clib_error_return (0, "parse error: '%U'",
1799                                      format_unformat_error, line_input);
1800           unformat_free (line_input);
1801           return error;
1802         }
1803     }
1804
1805   unformat_free (line_input);
1806
1807   if (hw_if_index == (u32) ~ 0)
1808     return clib_error_return (0, "please specify valid interface name");
1809
1810   error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1811                                          is_main);
1812
1813   return (error);
1814 }
1815
1816 /*?
1817  * This command is used to assign a given interface, and optionally a
1818  * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1819  * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1820  * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
1821  *
1822  * @cliexpar
1823  * Example of how to display the interface placement:
1824  * @cliexstart{show interface rx-placement}
1825  * Thread 1 (vpp_wk_0):
1826  *   node dpdk-input:
1827  *     GigabitEthernet7/0/0 queue 0 (polling)
1828  *   node vhost-user-input:
1829  *     VirtualEthernet0/0/12 queue 0 (polling)
1830  *     VirtualEthernet0/0/12 queue 2 (polling)
1831  *     VirtualEthernet0/0/13 queue 0 (polling)
1832  *     VirtualEthernet0/0/13 queue 2 (polling)
1833  * Thread 2 (vpp_wk_1):
1834  *   node dpdk-input:
1835  *     GigabitEthernet7/0/1 queue 0 (polling)
1836  *   node vhost-user-input:
1837  *     VirtualEthernet0/0/12 queue 1 (polling)
1838  *     VirtualEthernet0/0/12 queue 3 (polling)
1839  *     VirtualEthernet0/0/13 queue 1 (polling)
1840  *     VirtualEthernet0/0/13 queue 3 (polling)
1841  * @cliexend
1842  * Example of how to assign a interface and queue to a worker thread:
1843  * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1844  * Example of how to display the interface placement:
1845  * @cliexstart{show interface rx-placement}
1846  * Thread 1 (vpp_wk_0):
1847  *   node dpdk-input:
1848  *     GigabitEthernet7/0/0 queue 0 (polling)
1849  *   node vhost-user-input:
1850  *     VirtualEthernet0/0/12 queue 0 (polling)
1851  *     VirtualEthernet0/0/12 queue 1 (polling)
1852  *     VirtualEthernet0/0/12 queue 2 (polling)
1853  *     VirtualEthernet0/0/13 queue 0 (polling)
1854  *     VirtualEthernet0/0/13 queue 2 (polling)
1855  * Thread 2 (vpp_wk_1):
1856  *   node dpdk-input:
1857  *     GigabitEthernet7/0/1 queue 0 (polling)
1858  *   node vhost-user-input:
1859  *     VirtualEthernet0/0/12 queue 3 (polling)
1860  *     VirtualEthernet0/0/13 queue 1 (polling)
1861  *     VirtualEthernet0/0/13 queue 3 (polling)
1862  * @cliexend
1863 ?*/
1864 /* *INDENT-OFF* */
1865 VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1866     .path = "set interface rx-placement",
1867     .short_help = "set interface rx-placement <interface> [queue <n>] "
1868       "[worker <n> | main]",
1869     .function = set_interface_rx_placement,
1870     .is_mp_safe = 1,
1871 };
1872 /* *INDENT-ON* */
1873
1874 static u8 *
1875 format_vnet_pcap (u8 * s, va_list * args)
1876 {
1877   vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
1878   int type = va_arg (*args, int);
1879   int printed = 0;
1880
1881   if (type == 0)
1882     {
1883       if (pp->pcap_rx_enable)
1884         {
1885           s = format (s, "rx");
1886           printed = 1;
1887         }
1888       if (pp->pcap_tx_enable)
1889         {
1890           if (printed)
1891             s = format (s, " and ");
1892           s = format (s, "tx");
1893           printed = 1;
1894         }
1895       if (pp->pcap_drop_enable)
1896         {
1897           if (printed)
1898             s = format (s, " and ");
1899           s = format (s, "drop");
1900           printed = 1;
1901         }
1902       return s;
1903     }
1904   s = format (s, "unknown type %d!", type);
1905   return s;
1906 }
1907
1908
1909 int
1910 vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
1911 {
1912   vlib_main_t *vm = vlib_get_main ();
1913   vnet_pcap_t *pp = &vm->pcap;
1914   pcap_main_t *pm = &pp->pcap_main;
1915   vnet_classify_main_t *cm = &vnet_classify_main;
1916   vnet_classify_filter_set_t *set = 0;
1917
1918   if (a->status)
1919     {
1920       if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
1921         {
1922           vlib_cli_output
1923             (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
1924              format_vnet_pcap, pp, 0 /* print type */ ,
1925              pm->n_packets_captured, pm->n_packets_to_capture);
1926           vlib_cli_output (vm, "capture to file %s", pm->file_name);
1927         }
1928       else
1929         vlib_cli_output (vm, "pcap dispatch capture disabled");
1930
1931       return 0;
1932     }
1933
1934   /* Consistency checks */
1935
1936   /* Enable w/ capture already enabled not allowed */
1937   if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
1938       && (a->rx_enable + a->tx_enable + a->drop_enable))
1939     return VNET_API_ERROR_INVALID_VALUE;
1940
1941   /* Disable capture with capture already disabled, not interesting */
1942   if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) == 0)
1943       && ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
1944     return VNET_API_ERROR_VALUE_EXIST;
1945
1946   /* Change number of packets to capture while capturing */
1947   if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
1948       && (a->rx_enable + a->tx_enable + a->drop_enable)
1949       && (pm->n_packets_to_capture != a->packets_to_capture))
1950     return VNET_API_ERROR_INVALID_VALUE_2;
1951
1952   set = pool_elt_at_index (cm->filter_sets, cm->filter_set_by_sw_if_index[0]);
1953
1954   /* Classify filter specified, but no classify filter configured */
1955   if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
1956       (set->table_indices[0] == ~0))
1957     return VNET_API_ERROR_NO_SUCH_LABEL;
1958
1959   if (a->rx_enable + a->tx_enable + a->drop_enable)
1960     {
1961       /* Sanity check max bytes per pkt */
1962       if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
1963         return VNET_API_ERROR_INVALID_MEMORY_SIZE;
1964
1965       /* Clean up from previous run, if any */
1966       vec_free (pm->file_name);
1967       vec_free (pm->pcap_data);
1968       memset (pm, 0, sizeof (*pm));
1969
1970       vec_validate_aligned (vnet_trace_dummy, 2048, CLIB_CACHE_LINE_BYTES);
1971       if (pm->lock == 0)
1972         clib_spinlock_init (&(pm->lock));
1973
1974       if (a->filename == 0)
1975         {
1976           u8 *stem = 0;
1977
1978           if (a->rx_enable)
1979             stem = format (stem, "rx");
1980           if (a->tx_enable)
1981             stem = format (stem, "tx");
1982           if (a->drop_enable)
1983             stem = format (stem, "drop");
1984           a->filename = format (0, "/tmp/%s.pcap%c", stem, 0);
1985           vec_free (stem);
1986         }
1987
1988       pm->file_name = (char *) a->filename;
1989       pm->n_packets_captured = 0;
1990       pm->packet_type = PCAP_PACKET_TYPE_ethernet;
1991       pm->n_packets_to_capture = a->packets_to_capture;
1992       pp->pcap_sw_if_index = a->sw_if_index;
1993       if (a->filter)
1994         pp->filter_classify_table_index = set->table_indices[0];
1995       else
1996         pp->filter_classify_table_index = ~0;
1997       pp->pcap_rx_enable = a->rx_enable;
1998       pp->pcap_tx_enable = a->tx_enable;
1999       pp->pcap_drop_enable = a->drop_enable;
2000       pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
2001     }
2002   else
2003     {
2004       pp->pcap_rx_enable = 0;
2005       pp->pcap_tx_enable = 0;
2006       pp->pcap_drop_enable = 0;
2007       pp->filter_classify_table_index = ~0;
2008       if (pm->n_packets_captured)
2009         {
2010           clib_error_t *error;
2011           pm->n_packets_to_capture = pm->n_packets_captured;
2012           vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2013                            pm->n_packets_captured, pm->file_name);
2014           error = pcap_write (pm);
2015           if (pm->flags & PCAP_MAIN_INIT_DONE)
2016             pcap_close (pm);
2017           /* Report I/O errors... */
2018           if (error)
2019             {
2020               clib_error_report (error);
2021               return VNET_API_ERROR_SYSCALL_ERROR_1;
2022             }
2023           return 0;
2024         }
2025       else
2026         return VNET_API_ERROR_NO_SUCH_ENTRY;
2027     }
2028
2029   return 0;
2030 }
2031
2032 static clib_error_t *
2033 pcap_trace_command_fn (vlib_main_t * vm,
2034                        unformat_input_t * input, vlib_cli_command_t * cmd)
2035 {
2036   unformat_input_t _line_input, *line_input = &_line_input;
2037   vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
2038   vnet_main_t *vnm = vnet_get_main ();
2039   u8 *filename = 0;
2040   u32 max = 1000;
2041   u32 max_bytes_per_pkt = 512;
2042   int rv;
2043   int rx_enable = 0;
2044   int tx_enable = 0;
2045   int drop_enable = 0;
2046   int status = 0;
2047   int filter = 0;
2048   u32 sw_if_index = ~0;
2049
2050   /* Get a line of input. */
2051   if (!unformat_user (input, unformat_line_input, line_input))
2052     return 0;
2053
2054   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2055     {
2056       if (unformat (line_input, "rx"))
2057         rx_enable = 1;
2058       else if (unformat (line_input, "tx"))
2059         tx_enable = 1;
2060       else if (unformat (line_input, "drop"))
2061         drop_enable = 1;
2062       else if (unformat (line_input, "off"))
2063         rx_enable = tx_enable = drop_enable = 0;
2064       else if (unformat (line_input, "max-bytes-per-pkt %u",
2065                          &max_bytes_per_pkt))
2066         ;
2067       else if (unformat (line_input, "max %d", &max))
2068         ;
2069       else if (unformat (line_input, "packets-to-capture %d", &max))
2070         ;
2071       else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2072                          &filename))
2073         ;
2074       else if (unformat (line_input, "status %=", &status, 1))
2075         ;
2076       else if (unformat (line_input, "intfc %U",
2077                          unformat_vnet_sw_interface, vnm, &sw_if_index))
2078         ;
2079       else if (unformat (line_input, "intfc any"))
2080         sw_if_index = 0;
2081       else if (unformat (line_input, "filter"))
2082         filter = 1;
2083       else
2084         {
2085           return clib_error_return (0, "unknown input `%U'",
2086                                     format_unformat_error, line_input);
2087         }
2088     }
2089
2090   unformat_free (line_input);
2091
2092   /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2093   a->filename = filename;
2094   a->rx_enable = rx_enable;
2095   a->tx_enable = tx_enable;
2096   a->drop_enable = drop_enable;
2097   a->status = status;
2098   a->packets_to_capture = max;
2099   a->sw_if_index = sw_if_index;
2100   a->filter = filter;
2101   a->max_bytes_per_pkt = max_bytes_per_pkt;
2102
2103   rv = vnet_pcap_dispatch_trace_configure (a);
2104
2105   switch (rv)
2106     {
2107     case 0:
2108       break;
2109
2110     case VNET_API_ERROR_INVALID_VALUE:
2111       return clib_error_return (0, "dispatch trace already enabled...");
2112
2113     case VNET_API_ERROR_VALUE_EXIST:
2114       return clib_error_return (0, "dispatch trace already disabled...");
2115
2116     case VNET_API_ERROR_INVALID_VALUE_2:
2117       return clib_error_return
2118         (0, "can't change number of records to capture while tracing...");
2119
2120     case VNET_API_ERROR_SYSCALL_ERROR_1:
2121       return clib_error_return (0, "I/O writing trace capture...");
2122
2123     case VNET_API_ERROR_NO_SUCH_ENTRY:
2124       return clib_error_return (0, "No packets captured...");
2125
2126     case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2127       return clib_error_return (0,
2128                                 "Max bytes per pkt must be > 32, < 9000...");
2129
2130     case VNET_API_ERROR_NO_SUCH_LABEL:
2131       return clib_error_return
2132         (0, "No classify filter configured, see 'classify filter...'");
2133
2134     default:
2135       vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2136       break;
2137     }
2138   return 0;
2139 }
2140
2141 /*?
2142  * This command is used to start or stop a packet capture, or show
2143  * the status of packet capture.
2144  *
2145  * This command has the following optional parameters:
2146  *
2147  *
2148  * - <b>rx</b> - Capture received packets
2149  *
2150  * - <b>tx</b> - Capture transmitted packets
2151  *
2152  * - <b>drop</b> - Capture dropped packets
2153  *
2154  * - <b>off</b> - Stop capturing packets, write results to the specified file
2155  *
2156  * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2157  *   of packets have been received, buffer is flushed to file. Once another
2158  *   '<em>nn</em>' number of packets have been received, buffer is flushed
2159  *   to file, overwriting previous write. If not entered, value defaults
2160  *   to 100. Can only be updated if packet capture is off.
2161  *
2162  * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2163  *   for each packet. Must be >= 32, <= 9000.
2164  *
2165  * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
2166  *   or use '<em>any</em>' to run packet capture on all interfaces.
2167  *   '<em>any</em>' is the default if not provided. Settings from a previous
2168  *   packet capture are preserved, so '<em>any</em>' can be used to reset
2169  *   the interface setting.
2170  *
2171  * - <b>filter</b> - Use the pcap rx / tx / drop trace filter, which
2172  *   must be configured. Use <b>classify filter pcap...</b> to configure the
2173  *   filter. The filter will only be executed if the per-interface or
2174  *   any-interface tests fail.
2175  *
2176  * - <b>file <name></b> - Used to specify the output filename. The file will
2177  *   be placed in the '<em>/tmp</em>' directory, so only the filename is
2178  *   supported. Directory should not be entered. If file already exists, file
2179  *   will be overwritten. If no filename is provided, the file will be
2180  *   named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2181  *   Can only be updated if packet capture is off.
2182  *
2183  * - <b>status</b> - Displays the current status and configured attributes
2184  *   associated with a packet capture. If packet capture is in progress,
2185  *   '<em>status</em>' also will return the number of packets currently in
2186  *   the local buffer. All additional attributes entered on command line
2187  *   with '<em>status</em>' will be ignored and not applied.
2188  *
2189  * @cliexpar
2190  * Example of how to display the status of a tx packet capture when off:
2191  * @cliexstart{pcap trace status}
2192  * max is 100, for any interface to file /tmp/vpe.pcap
2193  * pcap tx capture is off...
2194  * @cliexend
2195  * Example of how to start a tx packet capture:
2196  * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file vppTest.pcap}
2197  * @cliexend
2198  * Example of how to display the status of a tx packet capture in progress:
2199  * @cliexstart{pcap trace status}
2200  * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2201  * pcap tx capture is on: 20 of 35 pkts...
2202  * @cliexend
2203  * Example of how to stop a tx packet capture:
2204  * @cliexstart{pcap trace off}
2205  * captured 21 pkts...
2206  * saved to /tmp/vppTest.pcap...
2207  * @cliexend
2208 ?*/
2209 /* *INDENT-OFF* */
2210
2211 VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
2212     .path = "pcap trace",
2213     .short_help =
2214     "pcap trace rx tx drop off [max <nn>] [intfc <interface>|any] [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]",
2215     .function = pcap_trace_command_fn,
2216 };
2217 /* *INDENT-ON* */
2218
2219 /*
2220  * fd.io coding-style-patch-verification: ON
2221  *
2222  * Local Variables:
2223  * eval: (c-set-style "gnu")
2224  * End:
2225  */