dpdk: Decoupling the meaning of xd->device_index in dpdk_plugin
[vpp.git] / src / plugins / dpdk / device / 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 #include <unistd.h>
17 #include <fcntl.h>
18
19 #include <vnet/vnet.h>
20 #include <vppinfra/vec.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/format.h>
23 #include <vppinfra/xxhash.h>
24 #include <vppinfra/linux/sysfs.c>
25
26 #include <vnet/ethernet/ethernet.h>
27 #include <dpdk/device/dpdk.h>
28 #include <vnet/classify/vnet_classify.h>
29 #include <vnet/mpls/packet.h>
30
31 #include <dpdk/device/dpdk_priv.h>
32
33 /**
34  * @file
35  * @brief CLI for DPDK Abstraction Layer and pcap Tx Trace.
36  *
37  * This file contains the source code for CLI for DPDK
38  * Abstraction Layer and pcap Tx Trace.
39  */
40
41
42 static clib_error_t *
43 get_hqos (u32 hw_if_index, u32 subport_id, dpdk_device_t ** xd,
44           dpdk_device_config_t ** devconf)
45 {
46   dpdk_main_t *dm = &dpdk_main;
47   vnet_hw_interface_t *hw;
48   struct rte_eth_dev_info dev_info;
49   uword *p = 0;
50   clib_error_t *error = NULL;
51
52
53   if (hw_if_index == (u32) ~ 0)
54     {
55       error = clib_error_return (0, "please specify valid interface name");
56       goto done;
57     }
58
59   if (subport_id != 0)
60     {
61       error = clib_error_return (0, "Invalid subport");
62       goto done;
63     }
64
65   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
66   *xd = vec_elt_at_index (dm->devices, hw->dev_instance);
67
68   rte_eth_dev_info_get ((*xd)->port_id, &dev_info);
69   if (dev_info.pci_dev)
70     {                           /* bonded interface has no pci info */
71       vlib_pci_addr_t pci_addr;
72
73       pci_addr.domain = dev_info.pci_dev->addr.domain;
74       pci_addr.bus = dev_info.pci_dev->addr.bus;
75       pci_addr.slot = dev_info.pci_dev->addr.devid;
76       pci_addr.function = dev_info.pci_dev->addr.function;
77
78       p =
79         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
80     }
81
82   if (p)
83     (*devconf) = pool_elt_at_index (dm->conf->dev_confs, p[0]);
84   else
85     (*devconf) = &dm->conf->default_devconf;
86
87 done:
88   return error;
89 }
90
91 static inline clib_error_t *
92 pcap_trace_command_internal (vlib_main_t * vm,
93                              unformat_input_t * input,
94                              vlib_cli_command_t * cmd, int rx_tx)
95 {
96 #define PCAP_DEF_PKT_TO_CAPTURE (100)
97
98   unformat_input_t _line_input, *line_input = &_line_input;
99   dpdk_main_t *dm = &dpdk_main;
100   u8 *filename;
101   u8 *chroot_filename = 0;
102   u32 max = 0;
103   int enabled = 0;
104   int errorFlag = 0;
105   clib_error_t *error = 0;
106
107   /* Get a line of input. */
108   if (!unformat_user (input, unformat_line_input, line_input))
109     return 0;
110
111   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
112     {
113       if (unformat (line_input, "on"))
114         {
115           if (dm->pcap[rx_tx].pcap_enable == 0)
116             {
117               enabled = 1;
118             }
119           else
120             {
121               vlib_cli_output (vm, "pcap tx capture already on...");
122               errorFlag = 1;
123               break;
124             }
125         }
126       else if (unformat (line_input, "off"))
127         {
128           if (dm->pcap[rx_tx].pcap_enable)
129             {
130               vlib_cli_output
131                 (vm, "captured %d pkts...",
132                  dm->pcap[rx_tx].pcap_main.n_packets_captured);
133               if (dm->pcap[rx_tx].pcap_main.n_packets_captured)
134                 {
135                   dm->pcap[rx_tx].pcap_main.n_packets_to_capture =
136                     dm->pcap[rx_tx].pcap_main.n_packets_captured;
137                   error = pcap_write (&dm->pcap[rx_tx].pcap_main);
138                   if (error)
139                     clib_error_report (error);
140                   else
141                     vlib_cli_output (vm, "saved to %s...",
142                                      dm->pcap[rx_tx].pcap_filename);
143                 }
144
145               dm->pcap[rx_tx].pcap_enable = 0;
146             }
147           else
148             {
149               vlib_cli_output (vm, "pcap tx capture already off...");
150               errorFlag = 1;
151               break;
152             }
153         }
154       else if (unformat (line_input, "max %d", &max))
155         {
156           if (dm->pcap[rx_tx].pcap_enable)
157             {
158               vlib_cli_output
159                 (vm,
160                  "can't change max value while pcap tx capture active...");
161               errorFlag = 1;
162               break;
163             }
164           dm->pcap[rx_tx].pcap_main.n_packets_to_capture = max;
165         }
166       else if (unformat (line_input, "intfc %U",
167                          unformat_vnet_sw_interface, dm->vnet_main,
168                          &dm->pcap[rx_tx].pcap_sw_if_index))
169         ;
170
171       else if (unformat (line_input, "intfc any"))
172         {
173           dm->pcap[rx_tx].pcap_sw_if_index = 0;
174         }
175       else if (unformat (line_input, "file %s", &filename))
176         {
177           if (dm->pcap[rx_tx].pcap_enable)
178             {
179               vlib_cli_output
180                 (vm, "can't change file while pcap tx capture active...");
181               errorFlag = 1;
182               break;
183             }
184
185           /* Brain-police user path input */
186           if (strstr ((char *) filename, "..")
187               || index ((char *) filename, '/'))
188             {
189               vlib_cli_output (vm, "illegal characters in filename '%s'",
190                                filename);
191               vlib_cli_output (vm, "Hint: .. and / are not allowed.");
192               vec_free (filename);
193               errorFlag = 1;
194               break;
195             }
196
197           chroot_filename = format (0, "/tmp/%s%c", filename, 0);
198           vec_free (filename);
199         }
200       else if (unformat (line_input, "status"))
201         {
202           if (dm->pcap[rx_tx].pcap_sw_if_index == 0)
203             {
204               vlib_cli_output
205                 (vm, "max is %d for any interface to file %s",
206                  dm->pcap_pkts_to_capture ?
207                  dm->pcap[rx_tx].pcap_pkts_to_capture
208                  : PCAP_DEF_PKT_TO_CAPTURE,
209                  dm->pcap_filename ?
210                  dm->pcap[rx_tx].pcap_filename : (u8 *) "/tmp/vpe.pcap");
211             }
212           else
213             {
214               vlib_cli_output (vm, "max is %d for interface %U to file %s",
215                                dm->pcap[rx_tx].pcap_pkts_to_capture
216                                ? dm->pcap_pkts_to_capture
217                                : PCAP_DEF_PKT_TO_CAPTURE,
218                                format_vnet_sw_if_index_name, dm->vnet_main,
219                                dm->pcap_sw_if_index,
220                                dm->pcap[rx_tx].pcap_filename
221                                ? dm->pcap[rx_tx].pcap_filename : (u8 *)
222                                "/tmp/vpe.pcap");
223             }
224
225           if (dm->pcap[rx_tx].pcap_enable == 0)
226             {
227               vlib_cli_output (vm, "pcap %s capture is off...",
228                                (rx_tx == VLIB_RX) ? "rx" : "tx");
229             }
230           else
231             {
232               vlib_cli_output (vm, "pcap %s capture is on: %d of %d pkts...",
233                                (rx_tx == VLIB_RX) ? "rx" : "tx",
234                                dm->pcap[rx_tx].pcap_main.n_packets_captured,
235                                dm->pcap[rx_tx].
236                                pcap_main.n_packets_to_capture);
237             }
238           break;
239         }
240
241       else
242         {
243           error = clib_error_return (0, "unknown input `%U'",
244                                      format_unformat_error, line_input);
245           errorFlag = 1;
246           break;
247         }
248     }
249   unformat_free (line_input);
250
251
252   if (errorFlag == 0)
253     {
254       /* Since no error, save configured values. */
255       if (chroot_filename)
256         {
257           if (dm->pcap[rx_tx].pcap_filename)
258             vec_free (dm->pcap[rx_tx].pcap_filename);
259           vec_add1 (chroot_filename, 0);
260           dm->pcap[rx_tx].pcap_filename = chroot_filename;
261         }
262
263       if (max)
264         dm->pcap[rx_tx].pcap_pkts_to_capture = max;
265
266
267       if (enabled)
268         {
269           if (dm->pcap[rx_tx].pcap_filename == 0)
270             dm->pcap[rx_tx].pcap_filename = format (0, "/tmp/vpe.pcap%c", 0);
271
272           memset (&dm->pcap[rx_tx].pcap_main, 0,
273                   sizeof (dm->pcap[rx_tx].pcap_main));
274           dm->pcap[rx_tx].pcap_main.file_name =
275             (char *) dm->pcap[rx_tx].pcap_filename;
276           dm->pcap[rx_tx].pcap_main.n_packets_to_capture
277             = PCAP_DEF_PKT_TO_CAPTURE;
278           if (dm->pcap[rx_tx].pcap_pkts_to_capture)
279             dm->pcap[rx_tx].pcap_main.n_packets_to_capture
280               = dm->pcap[rx_tx].pcap_pkts_to_capture;
281
282           dm->pcap[rx_tx].pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
283           dm->pcap[rx_tx].pcap_enable = 1;
284           vlib_cli_output (vm, "pcap tx capture on...");
285         }
286     }
287   else if (chroot_filename)
288     vec_free (chroot_filename);
289
290   return error;
291 }
292
293 static clib_error_t *
294 pcap_rx_trace_command_fn (vlib_main_t * vm,
295                           unformat_input_t * input, vlib_cli_command_t * cmd)
296 {
297   return pcap_trace_command_internal (vm, input, cmd, VLIB_RX);
298 }
299
300 static clib_error_t *
301 pcap_tx_trace_command_fn (vlib_main_t * vm,
302                           unformat_input_t * input, vlib_cli_command_t * cmd)
303 {
304   return pcap_trace_command_internal (vm, input, cmd, VLIB_TX);
305 }
306
307
308 /*?
309  * This command is used to start or stop a packet capture, or show
310  * the status of packet capture. Note that both "pcap rx trace" and
311  * "pcap tx trace" are implemented. The command syntax is identical,
312  * simply substitute rx for tx as needed.
313  *
314  * This command has the following optional parameters:
315  *
316  * - <b>on|off</b> - Used to start or stop a packet capture.
317  *
318  * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
319  *   of packets have been received, buffer is flushed to file. Once another
320  *   '<em>nn</em>' number of packets have been received, buffer is flushed
321  *   to file, overwriting previous write. If not entered, value defaults
322  *   to 100. Can only be updated if packet capture is off.
323  *
324  * - <b>intfc <interface>|any</b> - Used to specify a given interface,
325  *   or use '<em>any</em>' to run packet capture on all interfaces.
326  *   '<em>any</em>' is the default if not provided. Settings from a previous
327  *   packet capture are preserved, so '<em>any</em>' can be used to reset
328  *   the interface setting.
329  *
330  * - <b>file <name></b> - Used to specify the output filename. The file will
331  *   be placed in the '<em>/tmp</em>' directory, so only the filename is
332  *   supported. Directory should not be entered. If file already exists, file
333  *   will be overwritten. If no filename is provided, '<em>/tmp/vpe.pcap</em>'
334  *   will be used. Can only be updated if packet capture is off.
335  *
336  * - <b>status</b> - Displays the current status and configured attributes
337  *   associated with a packet capture. If packet capture is in progress,
338  *   '<em>status</em>' also will return the number of packets currently in
339  *   the local buffer. All additional attributes entered on command line
340  *   with '<em>status</em>' will be ingnored and not applied.
341  *
342  * @cliexpar
343  * Example of how to display the status of a tx packet capture when off:
344  * @cliexstart{pcap tx trace status}
345  * max is 100, for any interface to file /tmp/vpe.pcap
346  * pcap tx capture is off...
347  * @cliexend
348  * Example of how to start a tx packet capture:
349  * @cliexstart{pcap tx trace on max 35 intfc GigabitEthernet0/8/0 file vppTest.pcap}
350  * pcap tx capture on...
351  * @cliexend
352  * Example of how to display the status of a tx packet capture in progress:
353  * @cliexstart{pcap tx trace status}
354  * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
355  * pcap tx capture is on: 20 of 35 pkts...
356  * @cliexend
357  * Example of how to stop a tx packet capture:
358  * @cliexstart{vppctl pcap tx trace off}
359  * captured 21 pkts...
360  * saved to /tmp/vppTest.pcap...
361  * @cliexend
362 ?*/
363 /* *INDENT-OFF* */
364
365 VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
366     .path = "pcap tx trace",
367     .short_help =
368     "pcap tx trace [on|off] [max <nn>] [intfc <interface>|any] [file <name>] [status]",
369     .function = pcap_tx_trace_command_fn,
370 };
371 VLIB_CLI_COMMAND (pcap_rx_trace_command, static) = {
372     .path = "pcap rx trace",
373     .short_help =
374     "pcap rx trace [on|off] [max <nn>] [intfc <interface>|any] [file <name>] [status]",
375     .function = pcap_rx_trace_command_fn,
376 };
377 /* *INDENT-ON* */
378
379
380 static clib_error_t *
381 show_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
382                   vlib_cli_command_t * cmd)
383 {
384   struct rte_mempool *rmp;
385   int i;
386
387   for (i = 0; i < vec_len (dpdk_main.pktmbuf_pools); i++)
388     {
389       rmp = dpdk_main.pktmbuf_pools[i];
390       if (rmp)
391         {
392           unsigned count = rte_mempool_avail_count (rmp);
393           unsigned free_count = rte_mempool_in_use_count (rmp);
394
395           vlib_cli_output (vm,
396                            "name=\"%s\"  available = %7d allocated = %7d total = %7d\n",
397                            rmp->name, (u32) count, (u32) free_count,
398                            (u32) (count + free_count));
399         }
400       else
401         {
402           vlib_cli_output (vm, "rte_mempool is NULL (!)\n");
403         }
404     }
405   return 0;
406 }
407
408 /*?
409  * This command displays statistics of each DPDK mempool.
410  *
411  * @cliexpar
412  * Example of how to display DPDK buffer data:
413  * @cliexstart{show dpdk buffer}
414  * name="mbuf_pool_socket0"  available =   15104 allocated =    1280 total =   16384
415  * @cliexend
416 ?*/
417 /* *INDENT-OFF* */
418 VLIB_CLI_COMMAND (cmd_show_dpdk_buffer,static) = {
419     .path = "show dpdk buffer",
420     .short_help = "show dpdk buffer",
421     .function = show_dpdk_buffer,
422     .is_mp_safe = 1,
423 };
424 /* *INDENT-ON* */
425
426 static clib_error_t *
427 show_dpdk_physmem (vlib_main_t * vm, unformat_input_t * input,
428                    vlib_cli_command_t * cmd)
429 {
430   clib_error_t *err = 0;
431   u32 pipe_max_size;
432   int fds[2];
433   u8 *s = 0;
434   int n, n_try;
435   FILE *f;
436
437   err = clib_sysfs_read ("/proc/sys/fs/pipe-max-size", "%u", &pipe_max_size);
438
439   if (err)
440     return err;
441
442   if (pipe (fds) == -1)
443     return clib_error_return_unix (0, "pipe");
444
445 #ifndef F_SETPIPE_SZ
446 #define F_SETPIPE_SZ    (1024 + 7)
447 #endif
448
449   if (fcntl (fds[1], F_SETPIPE_SZ, pipe_max_size) == -1)
450     {
451       err = clib_error_return_unix (0, "fcntl(F_SETPIPE_SZ)");
452       goto error;
453     }
454
455   if (fcntl (fds[0], F_SETFL, O_NONBLOCK) == -1)
456     {
457       err = clib_error_return_unix (0, "fcntl(F_SETFL)");
458       goto error;
459     }
460
461   if ((f = fdopen (fds[1], "a")) == 0)
462     {
463       err = clib_error_return_unix (0, "fdopen");
464       goto error;
465     }
466
467   rte_dump_physmem_layout (f);
468   fflush (f);
469
470   n = n_try = 4096;
471   while (n == n_try)
472     {
473       uword len = vec_len (s);
474       vec_resize (s, len + n_try);
475
476       n = read (fds[0], s + len, n_try);
477       if (n < 0 && errno != EAGAIN)
478         {
479           err = clib_error_return_unix (0, "read");
480           goto error;
481         }
482       _vec_len (s) = len + (n < 0 ? 0 : n);
483     }
484
485   vlib_cli_output (vm, "%v", s);
486
487 error:
488   close (fds[0]);
489   close (fds[1]);
490   vec_free (s);
491   return err;
492 }
493
494 /*?
495  * This command displays DPDK physmem layout
496  *
497  * @cliexpar
498  * Example of how to display DPDK physmem layout:
499  * @cliexstart{show dpdk physmem}
500  * @cliexend
501 ?*/
502 /* *INDENT-OFF* */
503 VLIB_CLI_COMMAND (cmd_show_dpdk_physmem,static) = {
504     .path = "show dpdk physmem",
505     .short_help = "show dpdk physmem",
506     .function = show_dpdk_physmem,
507     .is_mp_safe = 1,
508 };
509 /* *INDENT-ON* */
510
511 static clib_error_t *
512 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
513                   vlib_cli_command_t * cmd)
514 {
515   static u32 *allocated_buffers;
516   u32 n_alloc = 0;
517   u32 n_free = 0;
518   u32 first, actual_alloc;
519
520   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
521     {
522       if (unformat (input, "allocate %d", &n_alloc))
523         ;
524       else if (unformat (input, "free %d", &n_free))
525         ;
526       else
527         break;
528     }
529
530   if (n_free)
531     {
532       if (vec_len (allocated_buffers) < n_free)
533         return clib_error_return (0, "Can't free %d, only %d allocated",
534                                   n_free, vec_len (allocated_buffers));
535
536       first = vec_len (allocated_buffers) - n_free;
537       vlib_buffer_free (vm, allocated_buffers + first, n_free);
538       _vec_len (allocated_buffers) = first;
539     }
540   if (n_alloc)
541     {
542       first = vec_len (allocated_buffers);
543       vec_validate (allocated_buffers,
544                     vec_len (allocated_buffers) + n_alloc - 1);
545
546       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
547                                         n_alloc);
548       _vec_len (allocated_buffers) = first + actual_alloc;
549
550       if (actual_alloc < n_alloc)
551         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
552                          actual_alloc);
553     }
554
555   vlib_cli_output (vm, "Currently %d buffers allocated",
556                    vec_len (allocated_buffers));
557
558   if (allocated_buffers && vec_len (allocated_buffers) == 0)
559     vec_free (allocated_buffers);
560
561   return 0;
562 }
563
564 /*?
565  * This command tests the allocation and freeing of DPDK buffers.
566  * If both '<em>allocate</em>' and '<em>free</em>' are entered on the
567  * same command, the '<em>free</em>' is executed first. If no
568  * parameters are provided, this command display how many DPDK buffers
569  * the test command has allocated.
570  *
571  * @cliexpar
572  * @parblock
573  *
574  * Example of how to display how many DPDK buffer test command has allcoated:
575  * @cliexstart{test dpdk buffer}
576  * Currently 0 buffers allocated
577  * @cliexend
578  *
579  * Example of how to allocate DPDK buffers using the test command:
580  * @cliexstart{test dpdk buffer allocate 10}
581  * Currently 10 buffers allocated
582  * @cliexend
583  *
584  * Example of how to free DPDK buffers allocated by the test command:
585  * @cliexstart{test dpdk buffer free 10}
586  * Currently 0 buffers allocated
587  * @cliexend
588  * @endparblock
589 ?*/
590 /* *INDENT-OFF* */
591 VLIB_CLI_COMMAND (cmd_test_dpdk_buffer,static) = {
592     .path = "test dpdk buffer",
593     .short_help = "test dpdk buffer [allocate <nn>] [free <nn>]",
594     .function = test_dpdk_buffer,
595     .is_mp_safe = 1,
596 };
597 /* *INDENT-ON* */
598
599 static clib_error_t *
600 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
601                   vlib_cli_command_t * cmd)
602 {
603   unformat_input_t _line_input, *line_input = &_line_input;
604   dpdk_main_t *dm = &dpdk_main;
605   vnet_hw_interface_t *hw;
606   dpdk_device_t *xd;
607   u32 hw_if_index = (u32) ~ 0;
608   u32 nb_rx_desc = (u32) ~ 0;
609   u32 nb_tx_desc = (u32) ~ 0;
610   clib_error_t *error = NULL;
611
612   if (!unformat_user (input, unformat_line_input, line_input))
613     return 0;
614
615   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
616     {
617       if (unformat
618           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
619            &hw_if_index))
620         ;
621       else if (unformat (line_input, "tx %d", &nb_tx_desc))
622         ;
623       else if (unformat (line_input, "rx %d", &nb_rx_desc))
624         ;
625       else
626         {
627           error = clib_error_return (0, "parse error: '%U'",
628                                      format_unformat_error, line_input);
629           goto done;
630         }
631     }
632
633   if (hw_if_index == (u32) ~ 0)
634     {
635       error = clib_error_return (0, "please specify valid interface name");
636       goto done;
637     }
638
639   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
640   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
641
642   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
643     {
644       error =
645         clib_error_return (0,
646                            "number of descriptors can be set only for "
647                            "physical devices");
648       goto done;
649     }
650
651   if ((nb_rx_desc == (u32) ~ 0 || nb_rx_desc == xd->nb_rx_desc) &&
652       (nb_tx_desc == (u32) ~ 0 || nb_tx_desc == xd->nb_tx_desc))
653     {
654       error = clib_error_return (0, "nothing changed");
655       goto done;
656     }
657
658   if (nb_rx_desc != (u32) ~ 0)
659     xd->nb_rx_desc = nb_rx_desc;
660
661   if (nb_tx_desc != (u32) ~ 0)
662     xd->nb_tx_desc = nb_tx_desc;
663
664   dpdk_device_setup (xd);
665
666   if (vec_len (xd->errors))
667     return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
668
669 done:
670   unformat_free (line_input);
671
672   return error;
673 }
674
675 /*?
676  * This command sets the number of DPDK '<em>rx</em>' and
677  * '<em>tx</em>' descriptors for the given physical interface. Use
678  * the command '<em>show hardware-interface</em>' to display the
679  * current descriptor allocation.
680  *
681  * @cliexpar
682  * Example of how to set the DPDK interface descriptors:
683  * @cliexcmd{set dpdk interface descriptors GigabitEthernet0/8/0 rx 512 tx 512}
684 ?*/
685 /* *INDENT-OFF* */
686 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
687     .path = "set dpdk interface descriptors",
688     .short_help = "set dpdk interface descriptors <interface> [rx <nn>] [tx <nn>]",
689     .function = set_dpdk_if_desc,
690 };
691 /* *INDENT-ON* */
692
693 static int
694 dpdk_device_queue_sort (void *a1, void *a2)
695 {
696   dpdk_device_and_queue_t *dq1 = a1;
697   dpdk_device_and_queue_t *dq2 = a2;
698
699   if (dq1->device > dq2->device)
700     return 1;
701   else if (dq1->device < dq2->device)
702     return -1;
703   else if (dq1->queue_id > dq2->queue_id)
704     return 1;
705   else if (dq1->queue_id < dq2->queue_id)
706     return -1;
707   else
708     return 0;
709 }
710
711
712 static clib_error_t *
713 show_dpdk_if_hqos_placement (vlib_main_t * vm, unformat_input_t * input,
714                              vlib_cli_command_t * cmd)
715 {
716   vlib_thread_main_t *tm = vlib_get_thread_main ();
717   dpdk_main_t *dm = &dpdk_main;
718   dpdk_device_and_queue_t *dq;
719   int cpu;
720
721   if (tm->n_vlib_mains == 1)
722     vlib_cli_output (vm, "All interfaces are handled by main thread");
723
724   for (cpu = 0; cpu < vec_len (dm->devices_by_hqos_cpu); cpu++)
725     {
726       if (cpu >= dm->hqos_cpu_first_index &&
727           cpu < (dm->hqos_cpu_first_index + dm->hqos_cpu_count))
728         vlib_cli_output (vm, "Thread %u (%s at lcore %u):", cpu,
729                          vlib_worker_threads[cpu].name,
730                          vlib_worker_threads[cpu].lcore_id);
731
732       vec_foreach (dq, dm->devices_by_hqos_cpu[cpu])
733       {
734         u32 hw_if_index = dm->devices[dq->device].hw_if_index;
735         vnet_hw_interface_t *hi =
736           vnet_get_hw_interface (dm->vnet_main, hw_if_index);
737         vlib_cli_output (vm, "  %v queue %u", hi->name, dq->queue_id);
738       }
739     }
740   return 0;
741 }
742
743 /*?
744  * This command is used to display the thread and core each
745  * DPDK output interface and HQoS queue is assigned too.
746  *
747  * @cliexpar
748  * Example of how to display the DPDK output interface and HQoS queue placement:
749  * @cliexstart{show dpdk interface hqos placement}
750  * Thread 1 (vpp_hqos-threads_0 at lcore 3):
751  *   GigabitEthernet0/8/0 queue 0
752  * Thread 2 (vpp_hqos-threads_1 at lcore 4):
753  *   GigabitEthernet0/9/0 queue 0
754  * @cliexend
755 ?*/
756 /* *INDENT-OFF* */
757 VLIB_CLI_COMMAND (cmd_show_dpdk_if_hqos_placement, static) = {
758   .path = "show dpdk interface hqos placement",
759   .short_help = "show dpdk interface hqos placement",
760   .function = show_dpdk_if_hqos_placement,
761 };
762 /* *INDENT-ON* */
763
764 static clib_error_t *
765 set_dpdk_if_hqos_placement (vlib_main_t * vm, unformat_input_t * input,
766                             vlib_cli_command_t * cmd)
767 {
768   unformat_input_t _line_input, *line_input = &_line_input;
769   dpdk_main_t *dm = &dpdk_main;
770   dpdk_device_and_queue_t *dq;
771   vnet_hw_interface_t *hw;
772   dpdk_device_t *xd;
773   u32 hw_if_index = (u32) ~ 0;
774   u32 cpu = (u32) ~ 0;
775   int i;
776   clib_error_t *error = NULL;
777
778   if (!unformat_user (input, unformat_line_input, line_input))
779     return 0;
780
781   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
782     {
783       if (unformat
784           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
785            &hw_if_index))
786         ;
787       else if (unformat (line_input, "thread %d", &cpu))
788         ;
789       else
790         {
791           error = clib_error_return (0, "parse error: '%U'",
792                                      format_unformat_error, line_input);
793           goto done;
794         }
795     }
796
797   if (hw_if_index == (u32) ~ 0)
798     return clib_error_return (0, "please specify valid interface name");
799
800   if (cpu < dm->hqos_cpu_first_index ||
801       cpu >= (dm->hqos_cpu_first_index + dm->hqos_cpu_count))
802     {
803       error = clib_error_return (0, "please specify valid thread id");
804       goto done;
805     }
806
807   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
808   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
809
810   for (i = 0; i < vec_len (dm->devices_by_hqos_cpu); i++)
811     {
812       vec_foreach (dq, dm->devices_by_hqos_cpu[i])
813       {
814         if (hw_if_index == dm->devices[dq->device].hw_if_index)
815           {
816             if (cpu == i)       /* nothing to do */
817               goto done;
818
819             vec_del1 (dm->devices_by_hqos_cpu[i],
820                       dq - dm->devices_by_hqos_cpu[i]);
821             vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
822             dq->queue_id = 0;
823             dq->device = xd->device_index;
824
825             vec_sort_with_function (dm->devices_by_hqos_cpu[i],
826                                     dpdk_device_queue_sort);
827
828             vec_sort_with_function (dm->devices_by_hqos_cpu[cpu],
829                                     dpdk_device_queue_sort);
830
831             goto done;
832           }
833       }
834     }
835
836   error = clib_error_return (0, "not found");
837
838 done:
839   unformat_free (line_input);
840
841   return error;
842 }
843
844 /*?
845  * This command is used to assign a given DPDK output interface and
846  * HQoS queue to a different thread. This will not create a thread,
847  * so the thread must already exist. Use '<em>/etc/vpp/startup.conf</em>'
848  * for the initial thread creation. See @ref qos_doc for more details.
849  *
850  * @cliexpar
851  * Example of how to display the DPDK output interface and HQoS queue placement:
852  * @cliexstart{show dpdk interface hqos placement}
853  * Thread 1 (vpp_hqos-threads_0 at lcore 3):
854  *   GigabitEthernet0/8/0 queue 0
855  * Thread 2 (vpp_hqos-threads_1 at lcore 4):
856  *   GigabitEthernet0/9/0 queue 0
857  * @cliexend
858  * Example of how to assign a DPDK output interface and HQoS queue to a thread:
859  * @cliexcmd{set dpdk interface hqos placement GigabitEthernet0/8/0 thread 2}
860 ?*/
861 /* *INDENT-OFF* */
862 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_placement, static) = {
863   .path = "set dpdk interface hqos placement",
864   .short_help = "set dpdk interface hqos placement <interface> thread <n>",
865   .function = set_dpdk_if_hqos_placement,
866 };
867 /* *INDENT-ON* */
868
869 static clib_error_t *
870 set_dpdk_if_hqos_pipe (vlib_main_t * vm, unformat_input_t * input,
871                        vlib_cli_command_t * cmd)
872 {
873   unformat_input_t _line_input, *line_input = &_line_input;
874   dpdk_main_t *dm = &dpdk_main;
875   vnet_hw_interface_t *hw;
876   dpdk_device_t *xd;
877   u32 hw_if_index = (u32) ~ 0;
878   u32 subport_id = (u32) ~ 0;
879   u32 pipe_id = (u32) ~ 0;
880   u32 profile_id = (u32) ~ 0;
881   int rv;
882   clib_error_t *error = NULL;
883
884   if (!unformat_user (input, unformat_line_input, line_input))
885     return 0;
886
887   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
888     {
889       if (unformat
890           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
891            &hw_if_index))
892         ;
893       else if (unformat (line_input, "subport %d", &subport_id))
894         ;
895       else if (unformat (line_input, "pipe %d", &pipe_id))
896         ;
897       else if (unformat (line_input, "profile %d", &profile_id))
898         ;
899       else
900         {
901           error = clib_error_return (0, "parse error: '%U'",
902                                      format_unformat_error, line_input);
903           goto done;
904         }
905     }
906
907   if (hw_if_index == (u32) ~ 0)
908     {
909       error = clib_error_return (0, "please specify valid interface name");
910       goto done;
911     }
912
913   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
914   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
915
916   rv =
917     rte_sched_pipe_config (xd->hqos_ht->hqos, subport_id, pipe_id,
918                            profile_id);
919   if (rv)
920     {
921       error = clib_error_return (0, "pipe configuration failed");
922       goto done;
923     }
924
925 done:
926   unformat_free (line_input);
927
928   return error;
929 }
930
931 /*?
932  * This command is used to change the profile associate with a HQoS pipe. The
933  * '<em><profile_id></em>' is zero based. Use the command
934  * '<em>show dpdk interface hqos</em>' to display the content of each profile.
935  * See @ref qos_doc for more details.
936  *
937  * @note
938  * Currently there is not an API to create a new HQoS pipe profile. One is
939  * created by default in the code (search for '<em>hqos_pipe_params_default</em>'').
940  * Additional profiles can be created in code and code recompiled. Then use this
941  * command to assign it.
942  *
943  * @cliexpar
944  * Example of how to assign a new profile to a HQoS pipe:
945  * @cliexcmd{set dpdk interface hqos pipe GigabitEthernet0/8/0 subport 0 pipe 2 profile 1}
946 ?*/
947 /* *INDENT-OFF* */
948 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_pipe, static) =
949 {
950   .path = "set dpdk interface hqos pipe",
951   .short_help = "set dpdk interface hqos pipe <interface> subport <subport_id> pipe <pipe_id> "
952                   "profile <profile_id>",
953   .function = set_dpdk_if_hqos_pipe,
954 };
955 /* *INDENT-ON* */
956
957 static clib_error_t *
958 set_dpdk_if_hqos_subport (vlib_main_t * vm, unformat_input_t * input,
959                           vlib_cli_command_t * cmd)
960 {
961   unformat_input_t _line_input, *line_input = &_line_input;
962   dpdk_main_t *dm = &dpdk_main;
963   dpdk_device_t *xd = NULL;
964   u32 hw_if_index = (u32) ~ 0;
965   u32 subport_id = (u32) ~ 0;
966   struct rte_sched_subport_params p;
967   int rv;
968   clib_error_t *error = NULL;
969   u32 tb_rate = (u32) ~ 0;
970   u32 tb_size = (u32) ~ 0;
971   u32 tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] =
972     { (u32) ~ 0, (u32) ~ 0, (u32) ~ 0, (u32) ~ 0 };
973   u32 tc_period = (u32) ~ 0;
974   dpdk_device_config_t *devconf = NULL;
975
976   if (!unformat_user (input, unformat_line_input, line_input))
977     return 0;
978
979   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
980     {
981       if (unformat
982           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
983            &hw_if_index))
984         ;
985       else if (unformat (line_input, "subport %d", &subport_id))
986         ;
987       else if (unformat (line_input, "rate %d", &tb_rate))
988         ;
989       else if (unformat (line_input, "bktsize %d", &tb_size))
990         ;
991       else if (unformat (line_input, "tc0 %d", &tc_rate[0]))
992         ;
993       else if (unformat (line_input, "tc1 %d", &tc_rate[1]))
994         ;
995       else if (unformat (line_input, "tc2 %d", &tc_rate[2]))
996         ;
997       else if (unformat (line_input, "tc3 %d", &tc_rate[3]))
998         ;
999       else if (unformat (line_input, "period %d", &tc_period))
1000         ;
1001       else
1002         {
1003           error = clib_error_return (0, "parse error: '%U'",
1004                                      format_unformat_error, line_input);
1005           goto done;
1006         }
1007     }
1008
1009   error = get_hqos (hw_if_index, subport_id, &xd, &devconf);
1010
1011   if (error == NULL)
1012     {
1013       /* Copy the current values over to local structure. */
1014       memcpy (&p, &devconf->hqos.subport[subport_id], sizeof (p));
1015
1016       /* Update local structure with input values. */
1017       if (tb_rate != (u32) ~ 0)
1018         {
1019           p.tb_rate = tb_rate;
1020           p.tc_rate[0] = tb_rate;
1021           p.tc_rate[1] = tb_rate;
1022           p.tc_rate[2] = tb_rate;
1023           p.tc_rate[3] = tb_rate;
1024         }
1025       if (tb_size != (u32) ~ 0)
1026         {
1027           p.tb_size = tb_size;
1028         }
1029       if (tc_rate[0] != (u32) ~ 0)
1030         {
1031           p.tc_rate[0] = tc_rate[0];
1032         }
1033       if (tc_rate[1] != (u32) ~ 0)
1034         {
1035           p.tc_rate[1] = tc_rate[1];
1036         }
1037       if (tc_rate[2] != (u32) ~ 0)
1038         {
1039           p.tc_rate[2] = tc_rate[2];
1040         }
1041       if (tc_rate[3] != (u32) ~ 0)
1042         {
1043           p.tc_rate[3] = tc_rate[3];
1044         }
1045       if (tc_period != (u32) ~ 0)
1046         {
1047           p.tc_period = tc_period;
1048         }
1049
1050       /* Apply changes. */
1051       rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport_id, &p);
1052       if (rv)
1053         {
1054           error = clib_error_return (0, "subport configuration failed");
1055           goto done;
1056         }
1057       else
1058         {
1059           /* Successfully applied, so save of the input values. */
1060           memcpy (&devconf->hqos.subport[subport_id], &p, sizeof (p));
1061         }
1062     }
1063
1064 done:
1065   unformat_free (line_input);
1066
1067   return error;
1068 }
1069
1070 /*?
1071  * This command is used to set the subport level parameters such as token
1072  * bucket rate (bytes per seconds), token bucket size (bytes), traffic class
1073  * rates (bytes per seconds) and token update period (Milliseconds).
1074  *
1075  * By default, the '<em>rate</em>' is set to 1250000000 bytes/second (10GbE
1076  * rate) and each of the four traffic classes is set to 100% of the port rate.
1077  * If the '<em>rate</em>' is updated by this command, all four traffic classes
1078  * are assigned the same value. Each of the four traffic classes can be updated
1079  * individually.
1080  *
1081  * @cliexpar
1082  * Example of how modify the subport attributes for a 1GbE link:
1083  * @cliexcmd{set dpdk interface hqos subport GigabitEthernet0/8/0 subport 0 rate 125000000}
1084 ?*/
1085 /* *INDENT-OFF* */
1086 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_subport, static) = {
1087   .path = "set dpdk interface hqos subport",
1088   .short_help = "set dpdk interface hqos subport <interface> subport <subport_id> "
1089                  "[rate <n>] [bktsize <n>] [tc0 <n>] [tc1 <n>] [tc2 <n>] [tc3 <n>] "
1090                  "[period <n>]",
1091   .function = set_dpdk_if_hqos_subport,
1092 };
1093 /* *INDENT-ON* */
1094
1095 static clib_error_t *
1096 set_dpdk_if_hqos_tctbl (vlib_main_t * vm, unformat_input_t * input,
1097                         vlib_cli_command_t * cmd)
1098 {
1099   unformat_input_t _line_input, *line_input = &_line_input;
1100   vlib_thread_main_t *tm = vlib_get_thread_main ();
1101   dpdk_main_t *dm = &dpdk_main;
1102   vnet_hw_interface_t *hw;
1103   dpdk_device_t *xd;
1104   u32 hw_if_index = (u32) ~ 0;
1105   u32 tc = (u32) ~ 0;
1106   u32 queue = (u32) ~ 0;
1107   u32 entry = (u32) ~ 0;
1108   u32 val, i;
1109   clib_error_t *error = NULL;
1110
1111   if (!unformat_user (input, unformat_line_input, line_input))
1112     return 0;
1113
1114   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1115     {
1116       if (unformat
1117           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1118            &hw_if_index))
1119         ;
1120       else if (unformat (line_input, "entry %d", &entry))
1121         ;
1122       else if (unformat (line_input, "tc %d", &tc))
1123         ;
1124       else if (unformat (line_input, "queue %d", &queue))
1125         ;
1126       else
1127         {
1128           error = clib_error_return (0, "parse error: '%U'",
1129                                      format_unformat_error, line_input);
1130           goto done;
1131         }
1132     }
1133
1134   if (hw_if_index == (u32) ~ 0)
1135     {
1136       error = clib_error_return (0, "please specify valid interface name");
1137       goto done;
1138     }
1139   if (entry >= 64)
1140     {
1141       error = clib_error_return (0, "invalid entry");
1142       goto done;
1143     }
1144   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
1145     {
1146       error = clib_error_return (0, "invalid traffic class");
1147       goto done;
1148     }
1149   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
1150     {
1151       error = clib_error_return (0, "invalid traffic class queue");
1152       goto done;
1153     }
1154
1155   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1156   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1157
1158   /* Detect the set of worker threads */
1159   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1160   /* Should never happen, shut up Coverity warning */
1161   if (p == 0)
1162     {
1163       error = clib_error_return (0, "no worker registrations?");
1164       goto done;
1165     }
1166
1167   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1168   int worker_thread_first = tr->first_index;
1169   int worker_thread_count = tr->count;
1170
1171   val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1172   for (i = 0; i < worker_thread_count; i++)
1173     xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
1174
1175 done:
1176   unformat_free (line_input);
1177
1178   return error;
1179 }
1180
1181 /*?
1182  * This command is used to set the traffic class translation table. The
1183  * traffic class translation table is used to map 64 values (0-63) to one of
1184  * four traffic class and one of four HQoS input queue. Use the '<em>show
1185  * dpdk interface hqos</em>' command to display the traffic class translation
1186  * table. See @ref qos_doc for more details.
1187  *
1188  * This command has the following parameters:
1189  *
1190  * - <b><interface></b> - Used to specify the output interface.
1191  *
1192  * - <b>entry <map_val></b> - Mapped value (0-63) to assign traffic class and queue to.
1193  *
1194  * - <b>tc <tc_id></b> - Traffic class (0-3) to be used by the provided mapped value.
1195  *
1196  * - <b>queue <queue_id></b> - HQoS input queue (0-3) to be used by the provided mapped value.
1197  *
1198  * @cliexpar
1199  * Example of how modify the traffic class translation table:
1200  * @cliexcmd{set dpdk interface hqos tctbl GigabitEthernet0/8/0 entry 16 tc 2 queue 2}
1201 ?*/
1202 /* *INDENT-OFF* */
1203 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_tctbl, static) = {
1204   .path = "set dpdk interface hqos tctbl",
1205   .short_help = "set dpdk interface hqos tctbl <interface> entry <map_val> tc <tc_id> queue <queue_id>",
1206   .function = set_dpdk_if_hqos_tctbl,
1207 };
1208 /* *INDENT-ON* */
1209
1210 static clib_error_t *
1211 set_dpdk_if_hqos_pktfield (vlib_main_t * vm, unformat_input_t * input,
1212                            vlib_cli_command_t * cmd)
1213 {
1214   unformat_input_t _line_input, *line_input = &_line_input;
1215   vlib_thread_main_t *tm = vlib_get_thread_main ();
1216   dpdk_main_t *dm = &dpdk_main;
1217   clib_error_t *error = NULL;
1218
1219   /* Device specific data */
1220   struct rte_eth_dev_info dev_info;
1221   dpdk_device_config_t *devconf = 0;
1222   vnet_hw_interface_t *hw;
1223   dpdk_device_t *xd;
1224   u32 hw_if_index = (u32) ~ 0;
1225
1226   /* Detect the set of worker threads */
1227   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1228   /* Should never happen, shut up Coverity warning */
1229   if (p == 0)
1230     return clib_error_return (0, "no worker registrations?");
1231
1232   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1233   int worker_thread_first = tr->first_index;
1234   int worker_thread_count = tr->count;
1235
1236   /* Packet field configuration */
1237   u64 mask = (u64) ~ 0;
1238   u32 id = (u32) ~ 0;
1239   u32 offset = (u32) ~ 0;
1240
1241   /* HQoS params */
1242   u32 n_subports_per_port, n_pipes_per_subport, tctbl_size;
1243
1244   u32 i;
1245
1246   /* Parse input arguments */
1247   if (!unformat_user (input, unformat_line_input, line_input))
1248     return 0;
1249
1250   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1251     {
1252       if (unformat
1253           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1254            &hw_if_index))
1255         ;
1256       else if (unformat (line_input, "id subport"))
1257         id = 0;
1258       else if (unformat (line_input, "id pipe"))
1259         id = 1;
1260       else if (unformat (line_input, "id tc"))
1261         id = 2;
1262       else if (unformat (line_input, "id %d", &id))
1263         ;
1264       else if (unformat (line_input, "offset %d", &offset))
1265         ;
1266       else if (unformat (line_input, "mask %llx", &mask))
1267         ;
1268       else
1269         {
1270           error = clib_error_return (0, "parse error: '%U'",
1271                                      format_unformat_error, line_input);
1272           goto done;
1273         }
1274     }
1275
1276   /* Get interface */
1277   if (hw_if_index == (u32) ~ 0)
1278     {
1279       error = clib_error_return (0, "please specify valid interface name");
1280       goto done;
1281     }
1282
1283   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1284   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1285
1286   rte_eth_dev_info_get (xd->port_id, &dev_info);
1287   if (dev_info.pci_dev)
1288     {                           /* bonded interface has no pci info */
1289       vlib_pci_addr_t pci_addr;
1290
1291       pci_addr.domain = dev_info.pci_dev->addr.domain;
1292       pci_addr.bus = dev_info.pci_dev->addr.bus;
1293       pci_addr.slot = dev_info.pci_dev->addr.devid;
1294       pci_addr.function = dev_info.pci_dev->addr.function;
1295
1296       p =
1297         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1298     }
1299
1300   if (p)
1301     devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
1302   else
1303     devconf = &dm->conf->default_devconf;
1304
1305   if (devconf->hqos_enabled == 0)
1306     {
1307       vlib_cli_output (vm, "HQoS disabled for this interface");
1308       goto done;
1309     }
1310
1311   n_subports_per_port = devconf->hqos.port.n_subports_per_port;
1312   n_pipes_per_subport = devconf->hqos.port.n_pipes_per_subport;
1313   tctbl_size = RTE_DIM (devconf->hqos.tc_table);
1314
1315   /* Validate packet field configuration: id, offset and mask */
1316   if (id >= 3)
1317     {
1318       error = clib_error_return (0, "invalid packet field id");
1319       goto done;
1320     }
1321
1322   switch (id)
1323     {
1324     case 0:
1325       if (dpdk_hqos_validate_mask (mask, n_subports_per_port) != 0)
1326         {
1327           error = clib_error_return (0, "invalid subport ID mask "
1328                                      "(n_subports_per_port = %u)",
1329                                      n_subports_per_port);
1330           goto done;
1331         }
1332       break;
1333     case 1:
1334       if (dpdk_hqos_validate_mask (mask, n_pipes_per_subport) != 0)
1335         {
1336           error = clib_error_return (0, "invalid pipe ID mask "
1337                                      "(n_pipes_per_subport = %u)",
1338                                      n_pipes_per_subport);
1339           goto done;
1340         }
1341       break;
1342     case 2:
1343     default:
1344       if (dpdk_hqos_validate_mask (mask, tctbl_size) != 0)
1345         {
1346           error = clib_error_return (0, "invalid TC table index mask "
1347                                      "(TC table size = %u)", tctbl_size);
1348           goto done;
1349         }
1350     }
1351
1352   /* Propagate packet field configuration to all workers */
1353   for (i = 0; i < worker_thread_count; i++)
1354     switch (id)
1355       {
1356       case 0:
1357         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabpos = offset;
1358         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabmask = mask;
1359         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabshr =
1360           count_trailing_zeros (mask);
1361         break;
1362       case 1:
1363         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabpos = offset;
1364         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabmask = mask;
1365         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabshr =
1366           count_trailing_zeros (mask);
1367         break;
1368       case 2:
1369       default:
1370         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabpos = offset;
1371         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabmask = mask;
1372         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabshr =
1373           count_trailing_zeros (mask);
1374       }
1375
1376 done:
1377   unformat_free (line_input);
1378
1379   return error;
1380 }
1381
1382 /*?
1383  * This command is used to set the packet fields required for classifiying the
1384  * incoming packet. As a result of classification process, packet field
1385  * information will be mapped to 5 tuples (subport, pipe, traffic class, pipe,
1386  * color) and stored in packet mbuf.
1387  *
1388  * This command has the following parameters:
1389  *
1390  * - <b><interface></b> - Used to specify the output interface.
1391  *
1392  * - <b>id subport|pipe|tc</b> - Classification occurs across three fields.
1393  * This parameter indicates which of the three masks are being configured. Legacy
1394  * code used 0-2 to represent these three fields, so 0-2 is still accepted.
1395  *   - <b>subport|0</b> - Currently only one subport is supported, so only
1396  * an empty mask is supported for the subport classification.
1397  *   - <b>pipe|1</b> - Currently, 4096 pipes per subport are supported, so a
1398  * 12-bit mask should be configure to map to the 0-4095 pipes.
1399  *   - <b>tc|2</b> - The translation table (see '<em>set dpdk interface hqos
1400  * tctbl</em>' command) maps each value (0-63) into one of the 4 traffic classes
1401  * per pipe. A 6-bit mask should be configure to map this field to a traffic class.
1402  *
1403  * - <b>offset <n></b> - Offset in the packet to apply the 64-bit mask for classification.
1404  * The offset should be on an 8-byte boundary (0,8,16,24..).
1405  *
1406  * - <b>mask <hex-mask></b> - 64-bit mask to apply to packet at the given '<em>offset</em>'.
1407  * Bits must be contiguous and should not include '<em>0x</em>'.
1408  *
1409  * The default values for the '<em>pktfield</em>' assumes Ethernet/IPv4/UDP packets with
1410  * no VLAN. Adjust based on expected packet format and desired classification field.
1411  * - '<em>subport</em>' is always empty (offset 0 mask 0000000000000000)
1412  * - By default, '<em>pipe</em>' maps to the UDP payload bits 12 .. 23 (offset 40
1413  * mask 0000000fff000000)
1414  * - By default, '<em>tc</em>' maps to the DSCP field in IP header (offset 48 mask
1415  * 00000000000000fc)
1416  *
1417  * @cliexpar
1418  * Example of how modify the '<em>pipe</em>' classification filter to match VLAN:
1419  * @cliexcmd{set dpdk interface hqos pktfield GigabitEthernet0/8/0 id pipe offset 8 mask 0000000000000FFF}
1420 ?*/
1421 /* *INDENT-OFF* */
1422 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_pktfield, static) = {
1423   .path = "set dpdk interface hqos pktfield",
1424   .short_help = "set dpdk interface hqos pktfield <interface> id subport|pipe|tc offset <n> "
1425                  "mask <hex-mask>",
1426   .function = set_dpdk_if_hqos_pktfield,
1427 };
1428 /* *INDENT-ON* */
1429
1430 static clib_error_t *
1431 show_dpdk_if_hqos (vlib_main_t * vm, unformat_input_t * input,
1432                    vlib_cli_command_t * cmd)
1433 {
1434   unformat_input_t _line_input, *line_input = &_line_input;
1435   vlib_thread_main_t *tm = vlib_get_thread_main ();
1436   dpdk_main_t *dm = &dpdk_main;
1437   vnet_hw_interface_t *hw;
1438   dpdk_device_t *xd;
1439   dpdk_device_config_hqos_t *cfg;
1440   dpdk_device_hqos_per_hqos_thread_t *ht;
1441   dpdk_device_hqos_per_worker_thread_t *wk;
1442   u32 *tctbl;
1443   u32 hw_if_index = (u32) ~ 0;
1444   u32 profile_id, subport_id, i;
1445   struct rte_eth_dev_info dev_info;
1446   dpdk_device_config_t *devconf = 0;
1447   vlib_thread_registration_t *tr;
1448   uword *p = 0;
1449   clib_error_t *error = NULL;
1450
1451   if (!unformat_user (input, unformat_line_input, line_input))
1452     return 0;
1453
1454   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1455     {
1456       if (unformat
1457           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1458            &hw_if_index))
1459         ;
1460       else
1461         {
1462           error = clib_error_return (0, "parse error: '%U'",
1463                                      format_unformat_error, line_input);
1464           goto done;
1465         }
1466     }
1467
1468   if (hw_if_index == (u32) ~ 0)
1469     {
1470       error = clib_error_return (0, "please specify interface name!!");
1471       goto done;
1472     }
1473
1474   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1475   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1476
1477   rte_eth_dev_info_get (xd->port_id, &dev_info);
1478   if (dev_info.pci_dev)
1479     {                           /* bonded interface has no pci info */
1480       vlib_pci_addr_t pci_addr;
1481
1482       pci_addr.domain = dev_info.pci_dev->addr.domain;
1483       pci_addr.bus = dev_info.pci_dev->addr.bus;
1484       pci_addr.slot = dev_info.pci_dev->addr.devid;
1485       pci_addr.function = dev_info.pci_dev->addr.function;
1486
1487       p =
1488         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1489     }
1490
1491   if (p)
1492     devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
1493   else
1494     devconf = &dm->conf->default_devconf;
1495
1496   if (devconf->hqos_enabled == 0)
1497     {
1498       vlib_cli_output (vm, "HQoS disabled for this interface");
1499       goto done;
1500     }
1501
1502   /* Detect the set of worker threads */
1503   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1504
1505   /* Should never happen, shut up Coverity warning */
1506   if (p == 0)
1507     {
1508       error = clib_error_return (0, "no worker registrations?");
1509       goto done;
1510     }
1511
1512   tr = (vlib_thread_registration_t *) p[0];
1513
1514   cfg = &devconf->hqos;
1515   ht = xd->hqos_ht;
1516   wk = &xd->hqos_wt[tr->first_index];
1517   tctbl = wk->hqos_tc_table;
1518
1519   vlib_cli_output (vm, " Thread:");
1520   vlib_cli_output (vm, "   Input SWQ size = %u packets", cfg->swq_size);
1521   vlib_cli_output (vm, "   Enqueue burst size = %u packets",
1522                    ht->hqos_burst_enq);
1523   vlib_cli_output (vm, "   Dequeue burst size = %u packets",
1524                    ht->hqos_burst_deq);
1525
1526   vlib_cli_output (vm,
1527                    "   Packet field 0: slab position = %4u, slab bitmask = 0x%016llx   (subport)",
1528                    wk->hqos_field0_slabpos, wk->hqos_field0_slabmask);
1529   vlib_cli_output (vm,
1530                    "   Packet field 1: slab position = %4u, slab bitmask = 0x%016llx   (pipe)",
1531                    wk->hqos_field1_slabpos, wk->hqos_field1_slabmask);
1532   vlib_cli_output (vm,
1533                    "   Packet field 2: slab position = %4u, slab bitmask = 0x%016llx   (tc)",
1534                    wk->hqos_field2_slabpos, wk->hqos_field2_slabmask);
1535   vlib_cli_output (vm,
1536                    "   Packet field 2  tc translation table: ([Mapped Value Range]: tc/queue tc/queue ...)");
1537   vlib_cli_output (vm,
1538                    "     [ 0 .. 15]: "
1539                    "%u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u",
1540                    tctbl[0] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1541                    tctbl[0] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1542                    tctbl[1] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1543                    tctbl[1] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1544                    tctbl[2] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1545                    tctbl[2] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1546                    tctbl[3] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1547                    tctbl[3] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1548                    tctbl[4] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1549                    tctbl[4] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1550                    tctbl[5] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1551                    tctbl[5] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1552                    tctbl[6] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1553                    tctbl[6] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1554                    tctbl[7] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1555                    tctbl[7] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1556                    tctbl[8] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1557                    tctbl[8] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1558                    tctbl[9] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1559                    tctbl[9] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1560                    tctbl[10] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1561                    tctbl[10] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1562                    tctbl[11] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1563                    tctbl[11] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1564                    tctbl[12] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1565                    tctbl[12] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1566                    tctbl[13] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1567                    tctbl[13] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1568                    tctbl[14] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1569                    tctbl[14] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1570                    tctbl[15] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1571                    tctbl[15] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
1572   vlib_cli_output (vm,
1573                    "     [16 .. 31]: "
1574                    "%u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u",
1575                    tctbl[16] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1576                    tctbl[16] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1577                    tctbl[17] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1578                    tctbl[17] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1579                    tctbl[18] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1580                    tctbl[18] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1581                    tctbl[19] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1582                    tctbl[19] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1583                    tctbl[20] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1584                    tctbl[20] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1585                    tctbl[21] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1586                    tctbl[21] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1587                    tctbl[22] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1588                    tctbl[22] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1589                    tctbl[23] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1590                    tctbl[23] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1591                    tctbl[24] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1592                    tctbl[24] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1593                    tctbl[25] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1594                    tctbl[25] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1595                    tctbl[26] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1596                    tctbl[26] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1597                    tctbl[27] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1598                    tctbl[27] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1599                    tctbl[28] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1600                    tctbl[28] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1601                    tctbl[29] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1602                    tctbl[29] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1603                    tctbl[30] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1604                    tctbl[30] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1605                    tctbl[31] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1606                    tctbl[31] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
1607   vlib_cli_output (vm,
1608                    "     [32 .. 47]: "
1609                    "%u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u",
1610                    tctbl[32] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1611                    tctbl[32] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1612                    tctbl[33] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1613                    tctbl[33] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1614                    tctbl[34] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1615                    tctbl[34] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1616                    tctbl[35] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1617                    tctbl[35] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1618                    tctbl[36] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1619                    tctbl[36] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1620                    tctbl[37] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1621                    tctbl[37] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1622                    tctbl[38] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1623                    tctbl[38] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1624                    tctbl[39] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1625                    tctbl[39] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1626                    tctbl[40] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1627                    tctbl[40] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1628                    tctbl[41] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1629                    tctbl[41] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1630                    tctbl[42] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1631                    tctbl[42] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1632                    tctbl[43] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1633                    tctbl[43] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1634                    tctbl[44] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1635                    tctbl[44] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1636                    tctbl[45] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1637                    tctbl[45] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1638                    tctbl[46] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1639                    tctbl[46] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1640                    tctbl[47] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1641                    tctbl[47] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
1642   vlib_cli_output (vm,
1643                    "     [48 .. 63]: "
1644                    "%u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u %u/%u",
1645                    tctbl[48] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1646                    tctbl[48] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1647                    tctbl[49] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1648                    tctbl[49] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1649                    tctbl[50] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1650                    tctbl[50] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1651                    tctbl[51] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1652                    tctbl[51] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1653                    tctbl[52] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1654                    tctbl[52] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1655                    tctbl[53] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1656                    tctbl[53] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1657                    tctbl[54] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1658                    tctbl[54] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1659                    tctbl[55] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1660                    tctbl[55] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1661                    tctbl[56] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1662                    tctbl[56] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1663                    tctbl[57] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1664                    tctbl[57] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1665                    tctbl[58] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1666                    tctbl[58] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1667                    tctbl[59] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1668                    tctbl[59] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1669                    tctbl[60] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1670                    tctbl[60] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1671                    tctbl[61] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1672                    tctbl[61] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1673                    tctbl[62] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1674                    tctbl[62] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1675                    tctbl[63] / RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS,
1676                    tctbl[63] % RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS);
1677   vlib_cli_output (vm, " Port:");
1678   vlib_cli_output (vm, "   Rate = %u bytes/second", cfg->port.rate);
1679   vlib_cli_output (vm, "   MTU = %u bytes", cfg->port.mtu);
1680   vlib_cli_output (vm, "   Frame overhead = %u bytes",
1681                    cfg->port.frame_overhead);
1682   vlib_cli_output (vm, "   Number of subports = %u",
1683                    cfg->port.n_subports_per_port);
1684   vlib_cli_output (vm, "   Number of pipes per subport = %u",
1685                    cfg->port.n_pipes_per_subport);
1686   vlib_cli_output (vm,
1687                    "   Packet queue size: TC0 = %u, TC1 = %u, TC2 = %u, TC3 = %u packets",
1688                    cfg->port.qsize[0], cfg->port.qsize[1], cfg->port.qsize[2],
1689                    cfg->port.qsize[3]);
1690   vlib_cli_output (vm, "   Number of pipe profiles = %u",
1691                    cfg->port.n_pipe_profiles);
1692
1693   for (subport_id = 0; subport_id < vec_len (cfg->subport); subport_id++)
1694     {
1695       vlib_cli_output (vm, " Subport %u:", subport_id);
1696       vlib_cli_output (vm, "   Rate = %u bytes/second",
1697                        cfg->subport[subport_id].tb_rate);
1698       vlib_cli_output (vm, "   Token bucket size = %u bytes",
1699                        cfg->subport[subport_id].tb_size);
1700       vlib_cli_output (vm,
1701                        "   Traffic class rate: TC0 = %u, TC1 = %u, TC2 = %u, TC3 = %u bytes/second",
1702                        cfg->subport[subport_id].tc_rate[0],
1703                        cfg->subport[subport_id].tc_rate[1],
1704                        cfg->subport[subport_id].tc_rate[2],
1705                        cfg->subport[subport_id].tc_rate[3]);
1706       vlib_cli_output (vm, "   TC period = %u milliseconds",
1707                        cfg->subport[subport_id].tc_period);
1708     }
1709
1710   for (profile_id = 0; profile_id < vec_len (cfg->pipe); profile_id++)
1711     {
1712       vlib_cli_output (vm, " Pipe profile %u:", profile_id);
1713       vlib_cli_output (vm, "   Rate = %u bytes/second",
1714                        cfg->pipe[profile_id].tb_rate);
1715       vlib_cli_output (vm, "   Token bucket size = %u bytes",
1716                        cfg->pipe[profile_id].tb_size);
1717       vlib_cli_output (vm,
1718                        "   Traffic class rate: TC0 = %u, TC1 = %u, TC2 = %u, TC3 = %u bytes/second",
1719                        cfg->pipe[profile_id].tc_rate[0],
1720                        cfg->pipe[profile_id].tc_rate[1],
1721                        cfg->pipe[profile_id].tc_rate[2],
1722                        cfg->pipe[profile_id].tc_rate[3]);
1723       vlib_cli_output (vm, "   TC period = %u milliseconds",
1724                        cfg->pipe[profile_id].tc_period);
1725 #ifdef RTE_SCHED_SUBPORT_TC_OV
1726       vlib_cli_output (vm, "   TC3 oversubscription_weight = %u",
1727                        cfg->pipe[profile_id].tc_ov_weight);
1728 #endif
1729
1730       for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1731         {
1732           vlib_cli_output (vm,
1733                            "   TC%u WRR weights: Q0 = %u, Q1 = %u, Q2 = %u, Q3 = %u",
1734                            i, cfg->pipe[profile_id].wrr_weights[i * 4],
1735                            cfg->pipe[profile_id].wrr_weights[i * 4 + 1],
1736                            cfg->pipe[profile_id].wrr_weights[i * 4 + 2],
1737                            cfg->pipe[profile_id].wrr_weights[i * 4 + 3]);
1738         }
1739     }
1740
1741 #ifdef RTE_SCHED_RED
1742   vlib_cli_output (vm, " Weighted Random Early Detection (WRED):");
1743   for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1744     {
1745       vlib_cli_output (vm, "   TC%u min: G = %u, Y = %u, R = %u", i,
1746                        cfg->port.red_params[i][e_RTE_METER_GREEN].min_th,
1747                        cfg->port.red_params[i][e_RTE_METER_YELLOW].min_th,
1748                        cfg->port.red_params[i][e_RTE_METER_RED].min_th);
1749
1750       vlib_cli_output (vm, "   TC%u max: G = %u, Y = %u, R = %u", i,
1751                        cfg->port.red_params[i][e_RTE_METER_GREEN].max_th,
1752                        cfg->port.red_params[i][e_RTE_METER_YELLOW].max_th,
1753                        cfg->port.red_params[i][e_RTE_METER_RED].max_th);
1754
1755       vlib_cli_output (vm,
1756                        "   TC%u inverted probability: G = %u, Y = %u, R = %u",
1757                        i, cfg->port.red_params[i][e_RTE_METER_GREEN].maxp_inv,
1758                        cfg->port.red_params[i][e_RTE_METER_YELLOW].maxp_inv,
1759                        cfg->port.red_params[i][e_RTE_METER_RED].maxp_inv);
1760
1761       vlib_cli_output (vm, "   TC%u weight: R = %u, Y = %u, R = %u", i,
1762                        cfg->port.red_params[i][e_RTE_METER_GREEN].wq_log2,
1763                        cfg->port.red_params[i][e_RTE_METER_YELLOW].wq_log2,
1764                        cfg->port.red_params[i][e_RTE_METER_RED].wq_log2);
1765     }
1766 #endif
1767
1768 done:
1769   unformat_free (line_input);
1770
1771   return error;
1772 }
1773
1774 /*?
1775  * This command is used to display details of an output interface's HQoS
1776  * settings.
1777  *
1778  * @cliexpar
1779  * Example of how to display HQoS settings for an interfaces:
1780  * @cliexstart{show dpdk interface hqos GigabitEthernet0/8/0}
1781  *  Thread:
1782  *    Input SWQ size = 4096 packets
1783  *    Enqueue burst size = 256 packets
1784  *    Dequeue burst size = 220 packets
1785  *    Packet field 0: slab position =    0, slab bitmask = 0x0000000000000000   (subport)
1786  *    Packet field 1: slab position =   40, slab bitmask = 0x0000000fff000000   (pipe)
1787  *    Packet field 2: slab position =    8, slab bitmask = 0x00000000000000fc   (tc)
1788  *    Packet field 2  tc translation table: ([Mapped Value Range]: tc/queue tc/queue ...)
1789  *      [ 0 .. 15]: 0/0 0/1 0/2 0/3 1/0 1/1 1/2 1/3 2/0 2/1 2/2 2/3 3/0 3/1 3/2 3/3
1790  *      [16 .. 31]: 0/0 0/1 0/2 0/3 1/0 1/1 1/2 1/3 2/0 2/1 2/2 2/3 3/0 3/1 3/2 3/3
1791  *      [32 .. 47]: 0/0 0/1 0/2 0/3 1/0 1/1 1/2 1/3 2/0 2/1 2/2 2/3 3/0 3/1 3/2 3/3
1792  *      [48 .. 63]: 0/0 0/1 0/2 0/3 1/0 1/1 1/2 1/3 2/0 2/1 2/2 2/3 3/0 3/1 3/2 3/3
1793  *  Port:
1794  *    Rate = 1250000000 bytes/second
1795  *    MTU = 1514 bytes
1796  *    Frame overhead = 24 bytes
1797  *    Number of subports = 1
1798  *    Number of pipes per subport = 4096
1799  *    Packet queue size: TC0 = 64, TC1 = 64, TC2 = 64, TC3 = 64 packets
1800  *    Number of pipe profiles = 2
1801  *  Subport 0:
1802  *    Rate = 1250000000 bytes/second
1803  *    Token bucket size = 1000000 bytes
1804  *    Traffic class rate: TC0 = 1250000000, TC1 = 1250000000, TC2 = 1250000000, TC3 = 1250000000 bytes/second
1805  *    TC period = 10 milliseconds
1806  *  Pipe profile 0:
1807  *    Rate = 305175 bytes/second
1808  *    Token bucket size = 1000000 bytes
1809  *    Traffic class rate: TC0 = 305175, TC1 = 305175, TC2 = 305175, TC3 = 305175 bytes/second
1810  *    TC period = 40 milliseconds
1811  *    TC0 WRR weights: Q0 = 1, Q1 = 1, Q2 = 1, Q3 = 1
1812  *    TC1 WRR weights: Q0 = 1, Q1 = 1, Q2 = 1, Q3 = 1
1813  *    TC2 WRR weights: Q0 = 1, Q1 = 1, Q2 = 1, Q3 = 1
1814  *    TC3 WRR weights: Q0 = 1, Q1 = 1, Q2 = 1, Q3 = 1
1815  * @cliexend
1816 ?*/
1817 /* *INDENT-OFF* */
1818 VLIB_CLI_COMMAND (cmd_show_dpdk_if_hqos, static) = {
1819   .path = "show dpdk interface hqos",
1820   .short_help = "show dpdk interface hqos <interface>",
1821   .function = show_dpdk_if_hqos,
1822 };
1823
1824 /* *INDENT-ON* */
1825
1826 static clib_error_t *
1827 show_dpdk_hqos_queue_stats (vlib_main_t * vm, unformat_input_t * input,
1828                             vlib_cli_command_t * cmd)
1829 {
1830   unformat_input_t _line_input, *line_input = &_line_input;
1831   clib_error_t *error = NULL;
1832 #ifdef RTE_SCHED_COLLECT_STATS
1833   dpdk_main_t *dm = &dpdk_main;
1834   u32 hw_if_index = (u32) ~ 0;
1835   u32 subport = (u32) ~ 0;
1836   u32 pipe = (u32) ~ 0;
1837   u32 tc = (u32) ~ 0;
1838   u32 tc_q = (u32) ~ 0;
1839   vnet_hw_interface_t *hw;
1840   dpdk_device_t *xd;
1841   uword *p = 0;
1842   struct rte_eth_dev_info dev_info;
1843   dpdk_device_config_t *devconf = 0;
1844   u32 qindex;
1845   struct rte_sched_queue_stats stats;
1846   u16 qlen;
1847
1848   if (!unformat_user (input, unformat_line_input, line_input))
1849     return 0;
1850
1851   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1852     {
1853       if (unformat
1854           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1855            &hw_if_index))
1856         ;
1857
1858       else if (unformat (line_input, "subport %d", &subport))
1859         ;
1860
1861       else if (unformat (line_input, "pipe %d", &pipe))
1862         ;
1863
1864       else if (unformat (line_input, "tc %d", &tc))
1865         ;
1866
1867       else if (unformat (line_input, "tc_q %d", &tc_q))
1868         ;
1869
1870       else
1871         {
1872           error = clib_error_return (0, "parse error: '%U'",
1873                                      format_unformat_error, line_input);
1874           goto done;
1875         }
1876     }
1877
1878   if (hw_if_index == (u32) ~ 0)
1879     {
1880       error = clib_error_return (0, "please specify interface name!!");
1881       goto done;
1882     }
1883
1884   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1885   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1886
1887   rte_eth_dev_info_get (xd->port_id, &dev_info);
1888   if (dev_info.pci_dev)
1889     {                           /* bonded interface has no pci info */
1890       vlib_pci_addr_t pci_addr;
1891
1892       pci_addr.domain = dev_info.pci_dev->addr.domain;
1893       pci_addr.bus = dev_info.pci_dev->addr.bus;
1894       pci_addr.slot = dev_info.pci_dev->addr.devid;
1895       pci_addr.function = dev_info.pci_dev->addr.function;
1896
1897       p =
1898         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1899     }
1900
1901   if (p)
1902     devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
1903   else
1904     devconf = &dm->conf->default_devconf;
1905
1906   if (devconf->hqos_enabled == 0)
1907     {
1908       vlib_cli_output (vm, "HQoS disabled for this interface");
1909       goto done;
1910     }
1911
1912   /*
1913    * Figure out which queue to query.  cf rte_sched_port_qindex.  (Not sure why
1914    * that method isn't made public by DPDK - how _should_ we get the queue ID?)
1915    */
1916   qindex = subport * devconf->hqos.port.n_pipes_per_subport + pipe;
1917   qindex = qindex * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + tc;
1918   qindex = qindex * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + tc_q;
1919
1920   if (rte_sched_queue_read_stats (xd->hqos_ht->hqos, qindex, &stats, &qlen) !=
1921       0)
1922     {
1923       error = clib_error_return (0, "failed to read stats");
1924       goto done;
1925     }
1926
1927   vlib_cli_output (vm, "%=24s%=16s", "Stats Parameter", "Value");
1928   vlib_cli_output (vm, "%=24s%=16d", "Packets", stats.n_pkts);
1929   vlib_cli_output (vm, "%=24s%=16d", "Packets dropped", stats.n_pkts_dropped);
1930 #ifdef RTE_SCHED_RED
1931   vlib_cli_output (vm, "%=24s%=16d", "Packets dropped (RED)",
1932                    stats.n_pkts_red_dropped);
1933 #endif
1934   vlib_cli_output (vm, "%=24s%=16d", "Bytes", stats.n_bytes);
1935   vlib_cli_output (vm, "%=24s%=16d", "Bytes dropped", stats.n_bytes_dropped);
1936
1937 #else
1938
1939   /* Get a line of input */
1940   if (!unformat_user (input, unformat_line_input, line_input))
1941     return 0;
1942
1943   vlib_cli_output (vm, "RTE_SCHED_COLLECT_STATS disabled in DPDK");
1944   goto done;
1945
1946 #endif
1947
1948 done:
1949   unformat_free (line_input);
1950
1951   return error;
1952 }
1953
1954 /*?
1955  * This command is used to display statistics associated with a HQoS traffic class
1956  * queue.
1957  *
1958  * @note
1959  * Statistic collection by the scheduler is disabled by default in DPDK. In order to
1960  * turn it on, add the following line to '<em>../vpp/dpdk/Makefile</em>':
1961  * - <b>$(call set,RTE_SCHED_COLLECT_STATS,y)</b>
1962  *
1963  * @cliexpar
1964  * Example of how to display statistics of HQoS a HQoS traffic class queue:
1965  * @cliexstart{show dpdk hqos queue GigabitEthernet0/9/0 subport 0 pipe 3181 tc 0 tc_q 0}
1966  *      Stats Parameter          Value
1967  *          Packets               140
1968  *      Packets dropped            0
1969  *           Bytes               8400
1970  *       Bytes dropped             0
1971  * @cliexend
1972 ?*/
1973 /* *INDENT-OFF* */
1974 VLIB_CLI_COMMAND (cmd_show_dpdk_hqos_queue_stats, static) = {
1975   .path = "show dpdk hqos queue",
1976   .short_help = "show dpdk hqos queue <interface> subport <subport_id> pipe <pipe_id> tc <tc_id> tc_q <queue_id>",
1977   .function = show_dpdk_hqos_queue_stats,
1978 };
1979 /* *INDENT-ON* */
1980
1981 static clib_error_t *
1982 show_dpdk_version_command_fn (vlib_main_t * vm,
1983                               unformat_input_t * input,
1984                               vlib_cli_command_t * cmd)
1985 {
1986 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
1987   _("DPDK Version", "%s", rte_version ());
1988   _("DPDK EAL init args", "%s", dpdk_config_main.eal_init_args_str);
1989 #undef _
1990   return 0;
1991 }
1992
1993 /*?
1994  * This command is used to display the current DPDK version and
1995  * the list of arguments passed to DPDK when started.
1996  *
1997  * @cliexpar
1998  * Example of how to display how many DPDK buffer test command has allcoated:
1999  * @cliexstart{show dpdk version}
2000  * DPDK Version:        DPDK 16.11.0
2001  * DPDK EAL init args:  -c 1 -n 4 --huge-dir /run/vpp/hugepages --file-prefix vpp -w 0000:00:08.0 -w 0000:00:09.0 --master-lcore 0 --socket-mem 256
2002  * @cliexend
2003 ?*/
2004 /* *INDENT-OFF* */
2005 VLIB_CLI_COMMAND (show_vpe_version_command, static) = {
2006   .path = "show dpdk version",
2007   .short_help = "show dpdk version",
2008   .function = show_dpdk_version_command_fn,
2009 };
2010 /* *INDENT-ON* */
2011
2012 #if CLI_DEBUG
2013
2014 static clib_error_t *
2015 dpdk_validate_buffers_fn (vlib_main_t * vm, unformat_input_t * input,
2016                           vlib_cli_command_t * cmd_arg)
2017 {
2018   u32 n_invalid_bufs = 0, uninitialized = 0;
2019   u32 is_poison = 0, is_test = 0;
2020   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2021     {
2022       if (unformat (input, "poison"))
2023         is_poison = 1;
2024       else if (unformat (input, "trajectory"))
2025         is_test = 1;
2026       else
2027         return clib_error_return (0, "unknown input `%U'",
2028                                   format_unformat_error, input);
2029     }
2030
2031   if (VLIB_BUFFER_TRACE_TRAJECTORY == 0)
2032     {
2033       vlib_cli_output (vm, "Trajectory not enabled. Recompile with "
2034                        "VLIB_BUFFER_TRACE_TRAJECTORY 1");
2035       return 0;
2036     }
2037   if (is_poison)
2038     {
2039       dpdk_buffer_poison_trajectory_all ();
2040     }
2041   if (is_test)
2042     {
2043       n_invalid_bufs = dpdk_buffer_validate_trajectory_all (&uninitialized);
2044       if (!n_invalid_bufs)
2045         vlib_cli_output (vm, "All buffers are valid %d uninitialized",
2046                          uninitialized);
2047       else
2048         vlib_cli_output (vm, "Found %d invalid buffers and %d uninitialized",
2049                          n_invalid_bufs, uninitialized);
2050     }
2051   return 0;
2052 }
2053
2054 /* *INDENT-OFF* */
2055 VLIB_CLI_COMMAND (test_dpdk_buffers_command, static) =
2056 {
2057   .path = "test dpdk buffers",
2058   .short_help = "test dpdk buffers [poison] [trajectory]",
2059   .function = dpdk_validate_buffers_fn,
2060 };
2061 /* *INDENT-ON* */
2062
2063 #endif
2064
2065 clib_error_t *
2066 dpdk_cli_init (vlib_main_t * vm)
2067 {
2068   return 0;
2069 }
2070
2071 VLIB_INIT_FUNCTION (dpdk_cli_init);
2072
2073 /*
2074  * fd.io coding-style-patch-verification: ON
2075  *
2076  * Local Variables:
2077  * eval: (c-set-style "gnu")
2078  * End:
2079  */