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