CLI:add l2 input/outut to "sh int features"
[vpp.git] / src / vnet / interface_cli.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * interface_cli.c: interface CLI
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 /**
41  * @file
42  * Interface CLI.
43  */
44
45 #include <vnet/vnet.h>
46 #include <vnet/ip/ip.h>
47 #include <vppinfra/bitmap.h>
48 #include <vnet/fib/ip4_fib.h>
49 #include <vnet/fib/ip6_fib.h>
50 #include <vnet/l2/l2_output.h>
51 #include <vnet/l2/l2_input.h>
52
53 static int
54 compare_interface_names (void *a1, void *a2)
55 {
56   u32 *hi1 = a1;
57   u32 *hi2 = a2;
58
59   return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
60 }
61
62 static clib_error_t *
63 show_or_clear_hw_interfaces (vlib_main_t * vm,
64                              unformat_input_t * input,
65                              vlib_cli_command_t * cmd)
66 {
67   clib_error_t *error = 0;
68   vnet_main_t *vnm = vnet_get_main ();
69   vnet_interface_main_t *im = &vnm->interface_main;
70   vnet_hw_interface_t *hi;
71   u32 hw_if_index, *hw_if_indices = 0;
72   int i, verbose = -1, is_show, show_bond = 0;
73
74   is_show = strstr (cmd->path, "show") != 0;
75   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
76     {
77       /* See if user wants to show a specific interface. */
78       if (unformat
79           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
80         vec_add1 (hw_if_indices, hw_if_index);
81
82       /* See if user wants to show an interface with a specific hw_if_index. */
83       else if (unformat (input, "%u", &hw_if_index))
84         vec_add1 (hw_if_indices, hw_if_index);
85
86       else if (unformat (input, "verbose"))
87         verbose = 1;            /* this is also the default */
88
89       else if (unformat (input, "detail"))
90         verbose = 2;
91
92       else if (unformat (input, "brief"))
93         verbose = 0;
94
95       else if (unformat (input, "bond"))
96         {
97           show_bond = 1;
98           if (verbose < 0)
99             verbose = 0;        /* default to brief for link bonding */
100         }
101
102       else
103         {
104           error = clib_error_return (0, "unknown input `%U'",
105                                      format_unformat_error, input);
106           goto done;
107         }
108     }
109
110   /* Gather interfaces. */
111   if (vec_len (hw_if_indices) == 0)
112     pool_foreach (hi, im->hw_interfaces,
113                   vec_add1 (hw_if_indices, hi - im->hw_interfaces));
114
115   if (verbose < 0)
116     verbose = 1;                /* default to verbose (except bond) */
117
118   if (is_show)
119     {
120       /* Sort by name. */
121       vec_sort_with_function (hw_if_indices, compare_interface_names);
122
123       vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
124       for (i = 0; i < vec_len (hw_if_indices); i++)
125         {
126           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
127           if (show_bond == 0)   /* show all interfaces */
128             vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
129                              hi, verbose);
130           else if ((hi->bond_info) &&
131                    (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
132             {                   /* show only bonded interface and all its slave interfaces */
133               int hw_idx;
134               vnet_hw_interface_t *shi;
135               vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
136                                hi, verbose);
137
138               /* *INDENT-OFF* */
139               clib_bitmap_foreach (hw_idx, hi->bond_info,
140               ({
141                 shi = vnet_get_hw_interface(vnm, hw_idx);
142                 vlib_cli_output (vm, "%U\n",
143                                  format_vnet_hw_interface, vnm, shi, verbose);
144               }));
145               /* *INDENT-ON* */
146             }
147         }
148     }
149   else
150     {
151       for (i = 0; i < vec_len (hw_if_indices); i++)
152         {
153           vnet_device_class_t *dc;
154
155           hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
156           dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
157
158           if (dc->clear_counters)
159             dc->clear_counters (hi->dev_instance);
160         }
161     }
162
163 done:
164   vec_free (hw_if_indices);
165   return error;
166 }
167
168 /* *INDENT-OFF* */
169 /*?
170  * Displays various information about the state of the current terminal
171  * session.
172  *
173  * @cliexpar
174  * @cliexstart{show hardware}
175  * Name                Link  Hardware
176  * GigabitEthernet2/0/0               up   GigabitEthernet2/0/0
177  * Ethernet address 00:50:56:b7:7c:83
178  * Intel 82545em_copper
179  *   link up, media 1000T full-duplex, master,
180  *   0 unprocessed, 384 total buffers on rx queue 0 ring
181  *   237 buffers in driver rx cache
182  *   rx total packets                                    1816
183  *   rx total bytes                                    181084
184  *   rx good packets                                     1816
185  *   rx good bytes                                     181084
186  *   rx 65 127 byte packets                              1586
187  *   rx 256 511 byte packets                              230
188  *   tx total packets                                     346
189  *   tx total bytes                                     90224
190  *   tx good packets                                      346
191  *   tx good bytes                                      88840
192  *   tx 64 byte packets                                     1
193  *   tx 65 127 byte packets                               115
194  *   tx 256 511 byte packets                              230
195  * @cliexend
196  ?*/
197 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
198   .path = "show hardware-interfaces",
199   .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] [<if-name1> <if-name2> ...]",
200   .function = show_or_clear_hw_interfaces,
201 };
202 /* *INDENT-ON* */
203
204 /* *INDENT-OFF* */
205 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
206   .path = "clear hardware-interfaces",
207   .short_help = "Clear hardware interfaces statistics",
208   .function = show_or_clear_hw_interfaces,
209 };
210 /* *INDENT-ON* */
211
212 static int
213 sw_interface_name_compare (void *a1, void *a2)
214 {
215   vnet_sw_interface_t *si1 = a1;
216   vnet_sw_interface_t *si2 = a2;
217
218   return vnet_sw_interface_compare (vnet_get_main (),
219                                     si1->sw_if_index, si2->sw_if_index);
220 }
221
222 static clib_error_t *
223 show_sw_interfaces (vlib_main_t * vm,
224                     unformat_input_t * input, vlib_cli_command_t * cmd)
225 {
226   clib_error_t *error = 0;
227   vnet_main_t *vnm = vnet_get_main ();
228   vnet_interface_main_t *im = &vnm->interface_main;
229   vnet_sw_interface_t *si, *sorted_sis = 0;
230   u32 sw_if_index = ~(u32) 0;
231   u8 show_addresses = 0;
232   u8 show_features = 0;
233   u8 show_tag = 0;
234
235   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
236     {
237       /* See if user wants to show specific interface */
238       if (unformat
239           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
240         {
241           si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
242           vec_add1 (sorted_sis, si[0]);
243         }
244       else if (unformat (input, "address") || unformat (input, "addr"))
245         show_addresses = 1;
246       else if (unformat (input, "features") || unformat (input, "feat"))
247         show_features = 1;
248       else if (unformat (input, "tag"))
249         show_tag = 1;
250       else
251         {
252           error = clib_error_return (0, "unknown input `%U'",
253                                      format_unformat_error, input);
254           goto done;
255         }
256     }
257
258   if (show_features || show_tag)
259     {
260       if (sw_if_index == ~(u32) 0)
261         return clib_error_return (0, "Interface not specified...");
262     }
263
264   if (show_features)
265     {
266       vnet_interface_features_show (vm, sw_if_index);
267
268       l2_input_config_t *l2_input = l2input_intf_config (sw_if_index);
269       u32 fb = l2_input->feature_bitmap;
270       /* intf input features are masked by bridge domain */
271       if (l2_input->bridge)
272         fb &= l2input_bd_config (l2_input->bd_index)->feature_bitmap;
273       vlib_cli_output (vm, "\nl2-input:\n%U", format_l2_input_features, fb);
274
275       l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
276       vlib_cli_output (vm, "\nl2-output:");
277       if (l2_output->out_vtr_flag)
278         vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
279       vlib_cli_output (vm, "%U", format_l2_output_features,
280                        l2_output->feature_bitmap);
281       return 0;
282     }
283   if (show_tag)
284     {
285       u8 *tag;
286       tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
287       vlib_cli_output (vm, "%U: %s",
288                        format_vnet_sw_if_index_name, vnm, sw_if_index,
289                        tag ? (char *) tag : "(none)");
290       return 0;
291     }
292
293   if (!show_addresses)
294     vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
295
296   if (vec_len (sorted_sis) == 0)        /* Get all interfaces */
297     {
298       /* Gather interfaces. */
299       sorted_sis =
300         vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
301       _vec_len (sorted_sis) = 0;
302       pool_foreach (si, im->sw_interfaces, (
303                                              {
304                                              int visible =
305                                              vnet_swif_is_api_visible (si);
306                                              if (visible)
307                                              vec_add1 (sorted_sis, si[0]);}
308                     ));
309
310       /* Sort by name. */
311       vec_sort_with_function (sorted_sis, sw_interface_name_compare);
312     }
313
314   if (show_addresses)
315     {
316       vec_foreach (si, sorted_sis)
317       {
318         ip4_main_t *im4 = &ip4_main;
319         ip6_main_t *im6 = &ip6_main;
320         ip_lookup_main_t *lm4 = &im4->lookup_main;
321         ip_lookup_main_t *lm6 = &im6->lookup_main;
322         ip_interface_address_t *ia = 0;
323         ip4_address_t *r4;
324         ip6_address_t *r6;
325         u32 fib_index4 = 0, fib_index6 = 0;
326         ip4_fib_t *fib4;
327         ip6_fib_t *fib6;
328
329         if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
330           fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
331                                 si->sw_if_index);
332
333         if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
334           fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
335                                 si->sw_if_index);
336
337         fib4 = ip4_fib_get (fib_index4);
338         fib6 = ip6_fib_get (fib_index6);
339
340         if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
341           vlib_cli_output
342             (vm, "%U (%s): \n  unnumbered, use %U",
343              format_vnet_sw_if_index_name,
344              vnm, si->sw_if_index,
345              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
346              format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
347
348         else
349           {
350             vlib_cli_output (vm, "%U (%s):",
351                              format_vnet_sw_if_index_name,
352                              vnm, si->sw_if_index,
353                              (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
354                              ? "up" : "dn");
355           }
356
357         /* Display any L2 info */
358         l2_input_config_t *l2_input = l2input_intf_config (si->sw_if_index);
359         if (l2_input->bridge)
360           {
361             u32 bd_id = l2input_main.bd_configs[l2_input->bd_index].bd_id;
362             vlib_cli_output (vm, "  l2 bridge bd_id %d%s%d", bd_id,
363                              l2_input->bvi ? " bvi shg " : " shg ",
364                              l2_input->shg);
365           }
366         else if (l2_input->xconnect)
367           {
368             vlib_cli_output (vm, "  l2 xconnect %U",
369                              format_vnet_sw_if_index_name,
370                              vnm, l2_input->output_sw_if_index);
371           }
372
373         /* Display any IP4 addressing info */
374           /* *INDENT-OFF* */
375           foreach_ip_interface_address (lm4, ia, si->sw_if_index,
376                                         1 /* honor unnumbered */,
377           ({
378             r4 = ip_interface_address_get_address (lm4, ia);
379             if (fib4->table_id)
380               {
381                 vlib_cli_output (vm, "  %U/%d table %d",
382                                  format_ip4_address, r4,
383                                  ia->address_length,
384                                  fib4->table_id);
385               }
386             else
387               {
388                 vlib_cli_output (vm, "  %U/%d",
389                                  format_ip4_address, r4,
390                                  ia->address_length);
391               }
392           }));
393           /* *INDENT-ON* */
394
395         /* Display any IP6 addressing info */
396           /* *INDENT-OFF* */
397           foreach_ip_interface_address (lm6, ia, si->sw_if_index,
398                                         1 /* honor unnumbered */,
399           ({
400             r6 = ip_interface_address_get_address (lm6, ia);
401             if (fib6->table_id)
402               {
403                 vlib_cli_output (vm, "  %U/%d table %d",
404                                  format_ip6_address, r6,
405                                  ia->address_length,
406                                  fib6->table_id);
407               }
408             else
409               {
410                 vlib_cli_output (vm, "  %U/%d",
411                                  format_ip6_address, r6,
412                                  ia->address_length);
413               }
414           }));
415           /* *INDENT-ON* */
416       }
417     }
418   else
419     {
420       vec_foreach (si, sorted_sis)
421       {
422         vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
423       }
424     }
425
426 done:
427   vec_free (sorted_sis);
428   return error;
429 }
430
431 /* *INDENT-OFF* */
432 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
433   .path = "show interface",
434   .short_help = "show interface [address|addr|features|feat] [<if-name1> <if-name2> ...]",
435   .function = show_sw_interfaces,
436 };
437 /* *INDENT-ON* */
438
439 /* Root of all interface commands. */
440 /* *INDENT-OFF* */
441 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
442   .path = "interface",
443   .short_help = "Interface commands",
444 };
445 /* *INDENT-ON* */
446
447 /* *INDENT-OFF* */
448 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
449   .path = "set interface",
450   .short_help = "Interface commands",
451 };
452 /* *INDENT-ON* */
453
454 static clib_error_t *
455 clear_interface_counters (vlib_main_t * vm,
456                           unformat_input_t * input, vlib_cli_command_t * cmd)
457 {
458   vnet_main_t *vnm = vnet_get_main ();
459   vnet_interface_main_t *im = &vnm->interface_main;
460   vlib_simple_counter_main_t *sm;
461   vlib_combined_counter_main_t *cm;
462   static vnet_main_t **my_vnet_mains;
463   int i, j, n_counters;
464
465   vec_reset_length (my_vnet_mains);
466
467   for (i = 0; i < vec_len (vnet_mains); i++)
468     {
469       if (vnet_mains[i])
470         vec_add1 (my_vnet_mains, vnet_mains[i]);
471     }
472
473   if (vec_len (vnet_mains) == 0)
474     vec_add1 (my_vnet_mains, vnm);
475
476   n_counters = vec_len (im->combined_sw_if_counters);
477
478   for (j = 0; j < n_counters; j++)
479     {
480       for (i = 0; i < vec_len (my_vnet_mains); i++)
481         {
482           im = &my_vnet_mains[i]->interface_main;
483           cm = im->combined_sw_if_counters + j;
484           vlib_clear_combined_counters (cm);
485         }
486     }
487
488   n_counters = vec_len (im->sw_if_counters);
489
490   for (j = 0; j < n_counters; j++)
491     {
492       for (i = 0; i < vec_len (my_vnet_mains); i++)
493         {
494           im = &my_vnet_mains[i]->interface_main;
495           sm = im->sw_if_counters + j;
496           vlib_clear_simple_counters (sm);
497         }
498     }
499
500   return 0;
501 }
502
503 /* *INDENT-OFF* */
504 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
505   .path = "clear interfaces",
506   .short_help = "Clear interfaces statistics",
507   .function = clear_interface_counters,
508 };
509 /* *INDENT-ON* */
510
511 /**
512  * Parse subinterface names.
513  *
514  * The following subinterface syntax is supported. The first two are for
515  * backwards compatability:
516  *
517  * <intf-name> <id>
518  *     - a subinterface with the name <intf-name>.<id>. The subinterface
519  *       is a single dot1q vlan with vlan id <id> and exact-match semantics.
520  *
521  * <intf-name> <min_id>-<max_id>
522  *     - a set of the above subinterfaces, repeating for each id
523  *       in the range <min_id> to <max_id>
524  *
525  * In the following, exact-match semantics (i.e. the number of vlan tags on the
526  * packet must match the number of tags in the configuration) are used only if
527  * the keyword exact-match is present. Non-exact match is the default.
528  *
529  * <intf-name> <id> dot1q <outer_id> [exact-match]
530  *     - a subinterface with the name <intf-name>.<id>. The subinterface
531  *       is a single dot1q vlan with vlan id <outer_id>.
532  *
533  * <intf-name> <id> dot1q any [exact-match]
534  *     - a subinterface with the name <intf-name>.<id>. The subinterface
535  *       is a single dot1q vlan with any vlan id.
536  *
537  * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
538  *     - a subinterface with the name <intf-name>.<id>. The subinterface
539  *       is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
540  *       <inner_id>.
541  *
542  * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
543  *     - a subinterface with the name <intf-name>.<id>. The subinterface
544  *       is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
545  *
546  * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
547  *
548  *     - a subinterface with the name <intf-name>.<id>. The subinterface
549  *       is a double dot1q vlan with any outer vlan id and any inner vlan id.
550  *
551  * For each of the above CLI, there is a duplicate that uses the keyword
552  * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
553  * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
554  * tagged packets the inner ethertype is always 0x8100. Also note that
555  * the dot1q and dot1ad naming spaces are independent, so it is legal to
556  * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
557  *
558  * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
559  *     - a subinterface with the name <intf-name>.<id>. The subinterface
560  *       is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
561  *       id <inner_id>.
562  *
563  * <intf-name> <id> untagged
564  *     - a subinterface with the name <intf-name>.<id>. The subinterface
565  *       has no vlan tags. Only one can be specified per interface.
566  *
567  * <intf-name> <id> default
568  *     - a subinterface with the name <intf-name>.<id>. This is associated
569  *       with a packet that did not match any other configured subinterface
570  *       on this interface. Only one can be specified per interface.
571  */
572
573 static clib_error_t *
574 parse_vlan_sub_interfaces (unformat_input_t * input,
575                            vnet_sw_interface_t * template)
576 {
577   clib_error_t *error = 0;
578   u32 inner_vlan, outer_vlan;
579
580   if (unformat (input, "any inner-dot1q any"))
581     {
582       template->sub.eth.flags.two_tags = 1;
583       template->sub.eth.flags.outer_vlan_id_any = 1;
584       template->sub.eth.flags.inner_vlan_id_any = 1;
585     }
586   else if (unformat (input, "any"))
587     {
588       template->sub.eth.flags.one_tag = 1;
589       template->sub.eth.flags.outer_vlan_id_any = 1;
590     }
591   else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
592     {
593       template->sub.eth.flags.two_tags = 1;
594       template->sub.eth.flags.inner_vlan_id_any = 1;
595       template->sub.eth.outer_vlan_id = outer_vlan;
596     }
597   else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
598     {
599       template->sub.eth.flags.two_tags = 1;
600       template->sub.eth.outer_vlan_id = outer_vlan;
601       template->sub.eth.inner_vlan_id = inner_vlan;
602     }
603   else if (unformat (input, "%d", &outer_vlan))
604     {
605       template->sub.eth.flags.one_tag = 1;
606       template->sub.eth.outer_vlan_id = outer_vlan;
607     }
608   else
609     {
610       error = clib_error_return (0, "expected dot1q config, got `%U'",
611                                  format_unformat_error, input);
612       goto done;
613     }
614
615   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
616     {
617       if (unformat (input, "exact-match"))
618         {
619           template->sub.eth.flags.exact_match = 1;
620         }
621     }
622
623 done:
624   return error;
625 }
626
627 static clib_error_t *
628 create_sub_interfaces (vlib_main_t * vm,
629                        unformat_input_t * input, vlib_cli_command_t * cmd)
630 {
631   vnet_main_t *vnm = vnet_get_main ();
632   clib_error_t *error = 0;
633   u32 hw_if_index, sw_if_index;
634   vnet_hw_interface_t *hi;
635   u32 id, id_min, id_max;
636   vnet_sw_interface_t template;
637
638   hw_if_index = ~0;
639   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
640     {
641       error = clib_error_return (0, "unknown interface `%U'",
642                                  format_unformat_error, input);
643       goto done;
644     }
645
646   memset (&template, 0, sizeof (template));
647   template.sub.eth.raw_flags = 0;
648
649   if (unformat (input, "%d default", &id_min))
650     {
651       id_max = id_min;
652       template.sub.eth.flags.default_sub = 1;
653     }
654   else if (unformat (input, "%d untagged", &id_min))
655     {
656       id_max = id_min;
657       template.sub.eth.flags.no_tags = 1;
658       template.sub.eth.flags.exact_match = 1;
659     }
660   else if (unformat (input, "%d dot1q", &id_min))
661     {
662       /* parse dot1q config */
663       id_max = id_min;
664       error = parse_vlan_sub_interfaces (input, &template);
665       if (error)
666         goto done;
667     }
668   else if (unformat (input, "%d dot1ad", &id_min))
669     {
670       /* parse dot1ad config */
671       id_max = id_min;
672       template.sub.eth.flags.dot1ad = 1;
673       error = parse_vlan_sub_interfaces (input, &template);
674       if (error)
675         goto done;
676     }
677   else if (unformat (input, "%d-%d", &id_min, &id_max))
678     {
679       template.sub.eth.flags.one_tag = 1;
680       template.sub.eth.flags.exact_match = 1;
681       if (id_min > id_max)
682         goto id_error;
683     }
684   else if (unformat (input, "%d", &id_min))
685     {
686       id_max = id_min;
687       template.sub.eth.flags.one_tag = 1;
688       template.sub.eth.outer_vlan_id = id_min;
689       template.sub.eth.flags.exact_match = 1;
690     }
691   else
692     {
693     id_error:
694       error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
695                                  format_unformat_error, input);
696       goto done;
697     }
698
699   hi = vnet_get_hw_interface (vnm, hw_if_index);
700
701   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
702     {
703       error =
704         clib_error_return (0,
705                            "not allowed as %v belong to a BondEthernet interface",
706                            hi->name);
707       goto done;
708     }
709
710   for (id = id_min; id <= id_max; id++)
711     {
712       uword *p;
713       vnet_interface_main_t *im = &vnm->interface_main;
714       u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
715       u64 *kp;
716
717       p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
718       if (p)
719         {
720           if (CLIB_DEBUG > 0)
721             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
722                           hi->sw_if_index, id);
723           continue;
724         }
725
726       kp = clib_mem_alloc (sizeof (*kp));
727       *kp = sup_and_sub_key;
728
729       template.type = VNET_SW_INTERFACE_TYPE_SUB;
730       template.flood_class = VNET_FLOOD_CLASS_NORMAL;
731       template.sup_sw_if_index = hi->sw_if_index;
732       template.sub.id = id;
733       if (id_min < id_max)
734         template.sub.eth.outer_vlan_id = id;
735
736       error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
737       if (error)
738         goto done;
739
740       hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
741       hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
742       vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
743                        vnet_get_main (), sw_if_index);
744     }
745
746 done:
747   return error;
748 }
749
750 /* *INDENT-OFF* */
751 /*?
752  * Create vlan subinterfaces
753  *
754  * @cliexpar
755  * @cliexstart{create sub-interfaces}
756  *
757  * To create a vlan subinterface 11 to process packets on 802.1q VLAN id 11, use:
758  *
759  *  vpp# create sub GigabitEthernet2/0/0 11
760  *
761  * This shorthand is equivalent to:
762  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 11 exact-match
763  *
764  * You can specify a subinterface number that is different from the vlan id:
765  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 100
766  *
767  * You can create qinq and q-in-any interfaces:
768  *  vpp# create sub GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200
769  *  vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any
770  *
771  * You can also create dot1ad interfaces:
772  *  vpp# create sub GigabitEthernet2/0/0 11 dot1ad 11
773  *  vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q 200
774  *
775  * Subinterfaces can be configured as either exact-match or non-exact match.
776  * Non-exact match is the CLI default. If exact-match is specified,
777  * packets must have the same number of vlan tags as the configuration.
778  * For non-exact-match, packets must at least that number of tags.
779  * L3 (routed) interfaces must be configured as exact-match.
780  * L2 interfaces are typically configured as non-exact-match.
781  *
782  * For example, a packet with outer vlan 100 and inner 200 would match this interface:
783  *  vpp# create sub GigabitEthernet2/0/0 5 dot1q 100
784  *
785  * but would not match this interface:
786  *  vpp# create sub GigabitEthernet2/0/0 5 dot1q 100 exact-match
787  *
788  * There are two special subinterfaces that can be configured. Subinterface untagged has no vlan tags:
789  *  vpp# create sub GigabitEthernet2/0/0 5 untagged
790  *
791  * The subinterface default matches any packet that does not match any other subinterface:
792  *  vpp# create sub GigabitEthernet2/0/0 7 default
793  * @cliexend
794  ?*/
795 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
796   .path = "create sub-interfaces",
797   .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
798   .function = create_sub_interfaces,
799 };
800 /* *INDENT-ON* */
801
802 static clib_error_t *
803 set_state (vlib_main_t * vm,
804            unformat_input_t * input, vlib_cli_command_t * cmd)
805 {
806   vnet_main_t *vnm = vnet_get_main ();
807   clib_error_t *error;
808   u32 sw_if_index, flags;
809
810   sw_if_index = ~0;
811   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
812     {
813       error = clib_error_return (0, "unknown interface `%U'",
814                                  format_unformat_error, input);
815       goto done;
816     }
817
818   if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
819     {
820       error = clib_error_return (0, "unknown flags `%U'",
821                                  format_unformat_error, input);
822       goto done;
823     }
824
825   error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
826   if (error)
827     goto done;
828
829 done:
830   return error;
831 }
832
833
834 /* *INDENT-OFF* */
835 /*?
836  * Interface admin up/down
837  *
838  * @cliexpar
839  * @cliexstart{set interface state}
840  *  vpp# set interface state GigabitEthernet2/0/0 up
841  *  vpp# set interface state GigabitEthernet2/0/0 down
842  * @cliexend
843  ?*/
844 VLIB_CLI_COMMAND (set_state_command, static) = {
845   .path = "set interface state",
846   .short_help = "set interface state <if-name> [up|down|punt|enable]",
847   .function = set_state,
848 };
849 /* *INDENT-ON* */
850
851 static clib_error_t *
852 set_unnumbered (vlib_main_t * vm,
853                 unformat_input_t * input, vlib_cli_command_t * cmd)
854 {
855   vnet_main_t *vnm = vnet_get_main ();
856   u32 unnumbered_sw_if_index;
857   u32 inherit_from_sw_if_index;
858   vnet_sw_interface_t *si;
859   int is_set = 0;
860   int is_del = 0;
861   u32 was_unnum;
862
863   if (unformat (input, "%U use %U",
864                 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
865                 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
866     is_set = 1;
867   else if (unformat (input, "del %U",
868                      unformat_vnet_sw_interface, vnm,
869                      &unnumbered_sw_if_index))
870     is_del = 1;
871   else
872     return clib_error_return (0, "parse error '%U'",
873                               format_unformat_error, input);
874
875   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
876   was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
877
878   if (is_del)
879     {
880       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
881       si->unnumbered_sw_if_index = (u32) ~ 0;
882
883       ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
884         [unnumbered_sw_if_index] = ~0;
885       ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
886         [unnumbered_sw_if_index] = ~0;
887     }
888   else if (is_set)
889     {
890       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
891       si->unnumbered_sw_if_index = inherit_from_sw_if_index;
892
893       ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
894         [unnumbered_sw_if_index] =
895         ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
896         [inherit_from_sw_if_index];
897       ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
898         [unnumbered_sw_if_index] =
899         ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
900         [inherit_from_sw_if_index];
901     }
902
903   if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
904     {
905       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
906       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
907     }
908
909   return 0;
910 }
911
912 /* *INDENT-OFF* */
913 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
914   .path = "set interface unnumbered",
915   .short_help = "set interface unnumbered [<intfc> use <intfc> | del <intfc>]",
916   .function = set_unnumbered,
917 };
918 /* *INDENT-ON* */
919
920
921
922 static clib_error_t *
923 set_hw_class (vlib_main_t * vm,
924               unformat_input_t * input, vlib_cli_command_t * cmd)
925 {
926   vnet_main_t *vnm = vnet_get_main ();
927   vnet_interface_main_t *im = &vnm->interface_main;
928   clib_error_t *error;
929   u32 hw_if_index, hw_class_index;
930
931   hw_if_index = ~0;
932   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
933     {
934       error = clib_error_return (0, "unknown hardware interface `%U'",
935                                  format_unformat_error, input);
936       goto done;
937     }
938
939   if (!unformat_user (input, unformat_hash_string,
940                       im->hw_interface_class_by_name, &hw_class_index))
941     {
942       error = clib_error_return (0, "unknown hardware class `%U'",
943                                  format_unformat_error, input);
944       goto done;
945     }
946
947   error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
948   if (error)
949     goto done;
950
951 done:
952   return error;
953 }
954
955 /* *INDENT-OFF* */
956 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
957   .path = "set interface hw-class",
958   .short_help = "Set interface hardware class",
959   .function = set_hw_class,
960 };
961 /* *INDENT-ON* */
962
963 static clib_error_t *
964 vnet_interface_cli_init (vlib_main_t * vm)
965 {
966   return 0;
967 }
968
969 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
970
971 static clib_error_t *
972 renumber_interface_command_fn (vlib_main_t * vm,
973                                unformat_input_t * input,
974                                vlib_cli_command_t * cmd)
975 {
976   u32 hw_if_index;
977   u32 new_dev_instance;
978   vnet_main_t *vnm = vnet_get_main ();
979   int rv;
980
981   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
982     return clib_error_return (0, "unknown hardware interface `%U'",
983                               format_unformat_error, input);
984
985   if (!unformat (input, "%d", &new_dev_instance))
986     return clib_error_return (0, "new dev instance missing");
987
988   rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
989
990   switch (rv)
991     {
992     case 0:
993       break;
994
995     default:
996       return clib_error_return (0, "vnet_interface_name_renumber returned %d",
997                                 rv);
998
999     }
1000
1001   return 0;
1002 }
1003
1004
1005 /* *INDENT-OFF* */
1006 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1007   .path = "renumber interface",
1008   .short_help = "renumber interface <if-name> <new-dev-instance>",
1009   .function = renumber_interface_command_fn,
1010 };
1011 /* *INDENT-ON* */
1012
1013 static clib_error_t *
1014 promiscuous_cmd (vlib_main_t * vm,
1015                  unformat_input_t * input, vlib_cli_command_t * cmd)
1016 {
1017   vnet_main_t *vnm = vnet_get_main ();
1018   u32 hw_if_index;
1019   u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
1020   ethernet_main_t *em = &ethernet_main;
1021   ethernet_interface_t *eif;
1022
1023   if (unformat (input, "on %U",
1024                 unformat_vnet_hw_interface, vnm, &hw_if_index))
1025     ;
1026   else if (unformat (input, "off %U",
1027                      unformat_ethernet_interface, vnm, &hw_if_index))
1028     flags = 0;
1029   else
1030     return clib_error_return (0, "unknown input `%U'",
1031                               format_unformat_error, input);
1032
1033   eif = ethernet_get_interface (em, hw_if_index);
1034   if (!eif)
1035     return clib_error_return (0, "not supported");
1036
1037   ethernet_set_flags (vnm, hw_if_index, flags);
1038   return 0;
1039 }
1040
1041 /* *INDENT-OFF* */
1042 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1043   .path = "set interface promiscuous",
1044   .short_help = "set interface promiscuous [on | off] <intfc>",
1045   .function = promiscuous_cmd,
1046 };
1047 /* *INDENT-ON* */
1048
1049 static clib_error_t *
1050 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1051 {
1052   vnet_main_t *vnm = vnet_get_main ();
1053   u32 hw_if_index, mtu;
1054   u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
1055   ethernet_main_t *em = &ethernet_main;
1056
1057   if (unformat (input, "%d %U", &mtu,
1058                 unformat_vnet_hw_interface, vnm, &hw_if_index))
1059     {
1060       vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1061       ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1062
1063       if (!eif)
1064         return clib_error_return (0, "not supported");
1065
1066       if (mtu < hi->min_supported_packet_bytes)
1067         return clib_error_return (0, "Invalid mtu (%d): "
1068                                   "must be >= min pkt bytes (%d)", mtu,
1069                                   hi->min_supported_packet_bytes);
1070
1071       if (mtu > hi->max_supported_packet_bytes)
1072         return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1073                                   hi->max_supported_packet_bytes);
1074
1075       if (hi->max_packet_bytes != mtu)
1076         {
1077           hi->max_packet_bytes = mtu;
1078           ethernet_set_flags (vnm, hw_if_index, flags);
1079         }
1080     }
1081   else
1082     return clib_error_return (0, "unknown input `%U'",
1083                               format_unformat_error, input);
1084   return 0;
1085 }
1086
1087 /* *INDENT-OFF* */
1088 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1089   .path = "set interface mtu",
1090   .short_help = "set interface mtu <value> <intfc>",
1091   .function = mtu_cmd,
1092 };
1093 /* *INDENT-ON* */
1094
1095 static clib_error_t *
1096 set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1097                            vlib_cli_command_t * cmd)
1098 {
1099   vnet_main_t *vnm = vnet_get_main ();
1100   clib_error_t *error = 0;
1101   u32 sw_if_index = ~0;
1102   u64 mac = 0;
1103
1104   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1105     {
1106       error = clib_error_return (0, "unknown interface `%U'",
1107                                  format_unformat_error, input);
1108       goto done;
1109     }
1110   if (!unformat_user (input, unformat_ethernet_address, &mac))
1111     {
1112       error = clib_error_return (0, "expected mac address `%U'",
1113                                  format_unformat_error, input);
1114       goto done;
1115     }
1116   error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
1117 done:
1118   return error;
1119 }
1120
1121 /*?
1122  * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1123  * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1124  * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1125  *
1126  * @cliexpar
1127  * @parblock
1128  * Example of how to change MAC Address of interface:
1129  * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1130  * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1131  * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1132  * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1133  * @endparblock
1134 ?*/
1135 /* *INDENT-OFF* */
1136 VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1137   .path = "set interface mac address",
1138   .short_help = "set interface mac address <intfc> <mac-address>",
1139   .function = set_interface_mac_address,
1140 };
1141 /* *INDENT-ON* */
1142
1143 static clib_error_t *
1144 set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1145 {
1146   vnet_main_t *vnm = vnet_get_main ();
1147   u32 sw_if_index = ~0;
1148   u8 *tag = 0;
1149
1150   if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1151                  vnm, &sw_if_index, &tag))
1152     return clib_error_return (0, "unknown input `%U'",
1153                               format_unformat_error, input);
1154
1155   vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1156
1157   return 0;
1158 }
1159
1160 /* *INDENT-OFF* */
1161 VLIB_CLI_COMMAND (set_tag_command, static) = {
1162   .path = "set interface tag",
1163   .short_help = "set interface tag <intfc> <tag>",
1164   .function = set_tag,
1165 };
1166 /* *INDENT-ON* */
1167
1168 static clib_error_t *
1169 clear_tag (vlib_main_t * vm, unformat_input_t * input,
1170            vlib_cli_command_t * cmd)
1171 {
1172   vnet_main_t *vnm = vnet_get_main ();
1173   u32 sw_if_index = ~0;
1174
1175   if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1176     return clib_error_return (0, "unknown input `%U'",
1177                               format_unformat_error, input);
1178
1179   vnet_clear_sw_interface_tag (vnm, sw_if_index);
1180
1181   return 0;
1182 }
1183
1184 /* *INDENT-OFF* */
1185 VLIB_CLI_COMMAND (clear_tag_command, static) = {
1186   .path = "clear interface tag",
1187   .short_help = "clear interface tag <intfc>",
1188   .function = clear_tag,
1189 };
1190 /* *INDENT-ON* */
1191
1192 static clib_error_t *
1193 set_hw_interface_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1194                           u32 queue_id, vnet_hw_interface_rx_mode mode)
1195 {
1196   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1197   vnet_device_class_t *dev_class =
1198     vnet_get_device_class (vnm, hw->dev_class_index);
1199   clib_error_t *error;
1200   vnet_hw_interface_rx_mode old_mode;
1201   int rv;
1202
1203   if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
1204     mode = hw->default_rx_mode;
1205
1206   rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &old_mode);
1207   switch (rv)
1208     {
1209     case 0:
1210       if (old_mode == mode)
1211         return 0;               /* same rx-mode, no change */
1212       break;
1213     case VNET_API_ERROR_INVALID_INTERFACE:
1214       return clib_error_return (0, "invalid interface");
1215     default:
1216       return clib_error_return (0, "unknown error");
1217     }
1218
1219   if (dev_class->rx_mode_change_function)
1220     {
1221       error = dev_class->rx_mode_change_function (vnm, hw_if_index, queue_id,
1222                                                   mode);
1223       if (error)
1224         return (error);
1225     }
1226
1227   rv = vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1228   switch (rv)
1229     {
1230     case 0:
1231       break;
1232     case VNET_API_ERROR_UNSUPPORTED:
1233       return clib_error_return (0, "unsupported");
1234     case VNET_API_ERROR_INVALID_INTERFACE:
1235       return clib_error_return (0, "invalid interface");
1236     default:
1237       return clib_error_return (0, "unknown error");
1238     }
1239
1240   return 0;
1241 }
1242
1243 static clib_error_t *
1244 set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1245                        vlib_cli_command_t * cmd)
1246 {
1247   clib_error_t *error = 0;
1248   unformat_input_t _line_input, *line_input = &_line_input;
1249   vnet_main_t *vnm = vnet_get_main ();
1250   vnet_hw_interface_t *hw;
1251   u32 hw_if_index = (u32) ~ 0;
1252   u32 queue_id = (u32) ~ 0;
1253   vnet_hw_interface_rx_mode mode = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
1254   int i;
1255
1256   if (!unformat_user (input, unformat_line_input, line_input))
1257     return 0;
1258
1259   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1260     {
1261       if (unformat
1262           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1263         ;
1264       else if (unformat (line_input, "queue %d", &queue_id))
1265         ;
1266       else if (unformat (line_input, "polling"))
1267         mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
1268       else if (unformat (line_input, "interrupt"))
1269         mode = VNET_HW_INTERFACE_RX_MODE_INTERRUPT;
1270       else if (unformat (line_input, "adaptive"))
1271         mode = VNET_HW_INTERFACE_RX_MODE_ADAPTIVE;
1272       else
1273         {
1274           error = clib_error_return (0, "parse error: '%U'",
1275                                      format_unformat_error, line_input);
1276           unformat_free (line_input);
1277           return error;
1278         }
1279     }
1280
1281   unformat_free (line_input);
1282
1283   if (hw_if_index == (u32) ~ 0)
1284     return clib_error_return (0, "please specify valid interface name");
1285
1286   if (mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
1287     return clib_error_return (0, "please specify valid rx-mode");
1288
1289   hw = vnet_get_hw_interface (vnm, hw_if_index);
1290
1291   if (queue_id == ~0)
1292     {
1293       for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1294         {
1295           error = set_hw_interface_rx_mode (vnm, hw_if_index, i, mode);
1296           if (error)
1297             break;
1298         }
1299       hw->default_rx_mode = mode;
1300     }
1301   else
1302     error = set_hw_interface_rx_mode (vnm, hw_if_index, queue_id, mode);
1303
1304   return (error);
1305 }
1306
1307 /*?
1308  * This command is used to assign a given interface, and optionally a
1309  * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1310  * it defaults to 0.
1311  *
1312  * @cliexpar
1313  * Example of how to display the interface placement:
1314  * @cliexstart{show interface rx-placement}
1315  * Thread 1 (vpp_wk_0):
1316  *   GigabitEthernet0/8/0 queue 0
1317  *   GigabitEthernet0/9/0 queue 0
1318  * Thread 2 (vpp_wk_1):
1319  *   GigabitEthernet0/8/0 queue 1
1320  *   GigabitEthernet0/9/0 queue 1
1321  * @cliexend
1322  * Example of how to assign a interface and queue to a thread:
1323  * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
1324 ?*/
1325 /* *INDENT-OFF* */
1326 VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1327     .path = "set interface rx-mode",
1328     .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1329     .function = set_interface_rx_mode,
1330 };
1331 /* *INDENT-ON* */
1332
1333 static clib_error_t *
1334 show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1335                                 vlib_cli_command_t * cmd)
1336 {
1337   u8 *s = 0;
1338   vnet_main_t *vnm = vnet_get_main ();
1339   vnet_device_input_runtime_t *rt;
1340   vnet_device_and_queue_t *dq;
1341   vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
1342   uword si;
1343   int index = 0;
1344
1345   /* *INDENT-OFF* */
1346   foreach_vlib_main (({
1347     clib_bitmap_foreach (si, pn->sibling_bitmap,
1348       ({
1349         rt = vlib_node_get_runtime_data (this_vlib_main, si);
1350
1351         if (vec_len (rt->devices_and_queues))
1352           s = format (s, "  node %U:\n", format_vlib_node_name, vm, si);
1353
1354         vec_foreach (dq, rt->devices_and_queues)
1355           {
1356             vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm,
1357                                                              dq->hw_if_index);
1358             s = format (s, "    %U queue %u (%U)\n",
1359                         format_vnet_sw_if_index_name, vnm, hi->sw_if_index,
1360                         dq->queue_id,
1361                         format_vnet_hw_interface_rx_mode, dq->mode);
1362           }
1363       }));
1364     if (vec_len (s) > 0)
1365       {
1366         vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
1367                         vlib_worker_threads[index].name, s);
1368         vec_reset_length (s);
1369       }
1370     index++;
1371   }));
1372   /* *INDENT-ON* */
1373
1374   vec_free (s);
1375   return 0;
1376 }
1377
1378 /* *INDENT-OFF* */
1379 VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1380   .path = "show interface rx-placement",
1381   .short_help = "show interface rx-placement",
1382   .function = show_interface_rx_placement_fn,
1383 };
1384 /* *INDENT-ON* */
1385
1386 static clib_error_t *
1387 set_interface_rx_placement (vlib_main_t * vm, unformat_input_t * input,
1388                             vlib_cli_command_t * cmd)
1389 {
1390   clib_error_t *error = 0;
1391   unformat_input_t _line_input, *line_input = &_line_input;
1392   vnet_main_t *vnm = vnet_get_main ();
1393   vnet_device_main_t *vdm = &vnet_device_main;
1394   vnet_hw_interface_rx_mode mode;
1395   u32 hw_if_index = (u32) ~ 0;
1396   u32 queue_id = (u32) 0;
1397   u32 thread_index = (u32) ~ 0;
1398   int rv;
1399
1400   if (!unformat_user (input, unformat_line_input, line_input))
1401     return 0;
1402
1403   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1404     {
1405       if (unformat
1406           (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1407         ;
1408       else if (unformat (line_input, "queue %d", &queue_id))
1409         ;
1410       else if (unformat (line_input, "main", &thread_index))
1411         thread_index = 0;
1412       else if (unformat (line_input, "worker %d", &thread_index))
1413         thread_index += vdm->first_worker_thread_index;
1414       else
1415         {
1416           error = clib_error_return (0, "parse error: '%U'",
1417                                      format_unformat_error, line_input);
1418           unformat_free (line_input);
1419           return error;
1420         }
1421     }
1422
1423   unformat_free (line_input);
1424
1425   if (hw_if_index == (u32) ~ 0)
1426     return clib_error_return (0, "please specify valid interface name");
1427
1428   if (thread_index > vdm->last_worker_thread_index)
1429     return clib_error_return (0,
1430                               "please specify valid worker thread or main");
1431
1432   rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &mode);
1433
1434   if (rv)
1435     return clib_error_return (0, "not found");
1436
1437   rv = vnet_hw_interface_unassign_rx_thread (vnm, hw_if_index, queue_id);
1438
1439   if (rv)
1440     return clib_error_return (0, "not found");
1441
1442   vnet_hw_interface_assign_rx_thread (vnm, hw_if_index, queue_id,
1443                                       thread_index);
1444   vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1445
1446   return 0;
1447 }
1448
1449 /*?
1450  * This command is used to assign a given interface, and optionally a
1451  * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1452  * it defaults to 0.
1453  *
1454  * @cliexpar
1455  * Example of how to display the interface placement:
1456  * @cliexstart{show interface placement}
1457  * Thread 1 (vpp_wk_0):
1458  *   GigabitEthernet0/8/0 queue 0
1459  *   GigabitEthernet0/9/0 queue 0
1460  * Thread 2 (vpp_wk_1):
1461  *   GigabitEthernet0/8/0 queue 1
1462  *   GigabitEthernet0/9/0 queue 1
1463  * @cliexend
1464  * Example of how to assign a interface and queue to a thread:
1465  * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
1466 ?*/
1467 /* *INDENT-OFF* */
1468 VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1469     .path = "set interface rx-placement",
1470     .short_help = "set interface rx-placement <hw-interface> [queue <n>] "
1471       "[worker <n> | main]",
1472     .function = set_interface_rx_placement,
1473     .is_mp_safe = 1,
1474 };
1475
1476 /* *INDENT-ON* */
1477 /*
1478  * fd.io coding-style-patch-verification: ON
1479  *
1480  * Local Variables:
1481  * eval: (c-set-style "gnu")
1482  * End:
1483  */