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