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