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