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