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