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