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