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