threads: add support for multiple worker handoff queues
[vpp.git] / vnet / vnet / devices / dpdk / 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 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/xxhash.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23 #include <vnet/classify/vnet_classify.h>
24 #include <vnet/mpls/packet.h>
25
26 #include "dpdk_priv.h"
27
28 static clib_error_t *
29 pcap_trace_command_fn (vlib_main_t * vm,
30                        unformat_input_t * input, vlib_cli_command_t * cmd)
31 {
32   dpdk_main_t *dm = &dpdk_main;
33   u8 *filename;
34   u32 max;
35   int matched = 0;
36   clib_error_t *error = 0;
37
38   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
39     {
40       if (unformat (input, "on"))
41         {
42           if (dm->tx_pcap_enable == 0)
43             {
44               if (dm->pcap_filename == 0)
45                 dm->pcap_filename = format (0, "/tmp/vpe.pcap%c", 0);
46
47               memset (&dm->pcap_main, 0, sizeof (dm->pcap_main));
48               dm->pcap_main.file_name = (char *) dm->pcap_filename;
49               dm->pcap_main.n_packets_to_capture = 100;
50               if (dm->pcap_pkts_to_capture)
51                 dm->pcap_main.n_packets_to_capture = dm->pcap_pkts_to_capture;
52
53               dm->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
54               dm->tx_pcap_enable = 1;
55               matched = 1;
56               vlib_cli_output (vm, "pcap tx capture on...");
57             }
58           else
59             {
60               vlib_cli_output (vm, "pcap tx capture already on...");
61             }
62           matched = 1;
63         }
64       else if (unformat (input, "off"))
65         {
66           if (dm->tx_pcap_enable)
67             {
68               vlib_cli_output (vm, "captured %d pkts...",
69                                dm->pcap_main.n_packets_captured + 1);
70               if (dm->pcap_main.n_packets_captured)
71                 {
72                   dm->pcap_main.n_packets_to_capture =
73                     dm->pcap_main.n_packets_captured;
74                   error = pcap_write (&dm->pcap_main);
75                   if (error)
76                     clib_error_report (error);
77                   else
78                     vlib_cli_output (vm, "saved to %s...", dm->pcap_filename);
79                 }
80             }
81           else
82             {
83               vlib_cli_output (vm, "pcap tx capture already off...");
84             }
85
86           dm->tx_pcap_enable = 0;
87           matched = 1;
88         }
89       else if (unformat (input, "max %d", &max))
90         {
91           dm->pcap_pkts_to_capture = max;
92           matched = 1;
93         }
94
95       else if (unformat (input, "intfc %U",
96                          unformat_vnet_sw_interface, dm->vnet_main,
97                          &dm->pcap_sw_if_index))
98         matched = 1;
99       else if (unformat (input, "intfc any"))
100         {
101           dm->pcap_sw_if_index = 0;
102           matched = 1;
103         }
104       else if (unformat (input, "file %s", &filename))
105         {
106           u8 *chroot_filename;
107           /* Brain-police user path input */
108           if (strstr ((char *) filename, "..")
109               || index ((char *) filename, '/'))
110             {
111               vlib_cli_output (vm, "illegal characters in filename '%s'",
112                                filename);
113               continue;
114             }
115
116           chroot_filename = format (0, "/tmp/%s%c", filename, 0);
117           vec_free (filename);
118
119           if (dm->pcap_filename)
120             vec_free (dm->pcap_filename);
121           vec_add1 (filename, 0);
122           dm->pcap_filename = chroot_filename;
123           matched = 1;
124         }
125       else if (unformat (input, "status"))
126         {
127           if (dm->tx_pcap_enable == 0)
128             {
129               vlib_cli_output (vm, "pcap tx capture is off...");
130               continue;
131             }
132
133           vlib_cli_output (vm, "pcap tx capture: %d of %d pkts...",
134                            dm->pcap_main.n_packets_captured,
135                            dm->pcap_main.n_packets_to_capture);
136           matched = 1;
137         }
138
139       else
140         break;
141     }
142
143   if (matched == 0)
144     return clib_error_return (0, "unknown input `%U'",
145                               format_unformat_error, input);
146
147   return 0;
148 }
149
150 /* *INDENT-OFF* */
151 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
152     .path = "pcap tx trace",
153     .short_help =
154     "pcap tx trace on off max <nn> intfc <intfc> file <name> status",
155     .function = pcap_trace_command_fn,
156 };
157 /* *INDENT-ON* */
158
159
160 static clib_error_t *
161 show_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
162                   vlib_cli_command_t * cmd)
163 {
164   struct rte_mempool *rmp;
165   int i;
166
167   for (i = 0; i < vec_len (vm->buffer_main->pktmbuf_pools); i++)
168     {
169       rmp = vm->buffer_main->pktmbuf_pools[i];
170       if (rmp)
171         {
172           unsigned count = rte_mempool_avail_count (rmp);
173           unsigned free_count = rte_mempool_in_use_count (rmp);
174
175           vlib_cli_output (vm,
176                            "name=\"%s\"  available = %7d allocated = %7d total = %7d\n",
177                            rmp->name, (u32) count, (u32) free_count,
178                            (u32) (count + free_count));
179         }
180       else
181         {
182           vlib_cli_output (vm, "rte_mempool is NULL (!)\n");
183         }
184     }
185   return 0;
186 }
187
188 /* *INDENT-OFF* */
189 VLIB_CLI_COMMAND (cmd_show_dpdk_bufferr,static) = {
190     .path = "show dpdk buffer",
191     .short_help = "show dpdk buffer state",
192     .function = show_dpdk_buffer,
193     .is_mp_safe = 1,
194 };
195 /* *INDENT-ON* */
196
197 static clib_error_t *
198 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
199                   vlib_cli_command_t * cmd)
200 {
201   static u32 *allocated_buffers;
202   u32 n_alloc = 0;
203   u32 n_free = 0;
204   u32 first, actual_alloc;
205
206   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
207     {
208       if (unformat (input, "allocate %d", &n_alloc))
209         ;
210       else if (unformat (input, "free %d", &n_free))
211         ;
212       else
213         break;
214     }
215
216   if (n_free)
217     {
218       if (vec_len (allocated_buffers) < n_free)
219         return clib_error_return (0, "Can't free %d, only %d allocated",
220                                   n_free, vec_len (allocated_buffers));
221
222       first = vec_len (allocated_buffers) - n_free;
223       vlib_buffer_free (vm, allocated_buffers + first, n_free);
224       _vec_len (allocated_buffers) = first;
225     }
226   if (n_alloc)
227     {
228       first = vec_len (allocated_buffers);
229       vec_validate (allocated_buffers,
230                     vec_len (allocated_buffers) + n_alloc - 1);
231
232       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
233                                         n_alloc);
234       _vec_len (allocated_buffers) = first + actual_alloc;
235
236       if (actual_alloc < n_alloc)
237         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
238                          actual_alloc);
239     }
240
241   vlib_cli_output (vm, "Currently %d buffers allocated",
242                    vec_len (allocated_buffers));
243
244   if (allocated_buffers && vec_len (allocated_buffers) == 0)
245     vec_free (allocated_buffers);
246
247   return 0;
248 }
249
250 /* *INDENT-OFF* */
251 VLIB_CLI_COMMAND (cmd_test_dpdk_buffer,static) = {
252     .path = "test dpdk buffer",
253     .short_help = "test dpdk buffer [allocate <nn>][free <nn>]",
254     .function = test_dpdk_buffer,
255     .is_mp_safe = 1,
256 };
257 /* *INDENT-ON* */
258
259 static void
260 show_dpdk_device_stats (vlib_main_t * vm, dpdk_device_t * xd)
261 {
262   vlib_cli_output (vm,
263                    "device_index %d\n"
264                    "  last_burst_sz           %d\n"
265                    "  max_burst_sz            %d\n"
266                    "  full_frames_cnt         %u\n"
267                    "  consec_full_frames_cnt  %u\n"
268                    "  congestion_cnt          %d\n"
269                    "  last_poll_time          %llu\n"
270                    "  max_poll_delay          %llu\n"
271                    "  discard_cnt             %u\n"
272                    "  total_packet_cnt        %u\n",
273                    xd->device_index,
274                    xd->efd_agent.last_burst_sz,
275                    xd->efd_agent.max_burst_sz,
276                    xd->efd_agent.full_frames_cnt,
277                    xd->efd_agent.consec_full_frames_cnt,
278                    xd->efd_agent.congestion_cnt,
279                    xd->efd_agent.last_poll_time,
280                    xd->efd_agent.max_poll_delay,
281                    xd->efd_agent.discard_cnt, xd->efd_agent.total_packet_cnt);
282
283   u32 device_queue_sz = rte_eth_rx_queue_count (xd->device_index,
284                                                 0 /* queue_id */ );
285   vlib_cli_output (vm, "  device_queue_sz         %u\n", device_queue_sz);
286 }
287
288 static void
289 show_efd_config (vlib_main_t * vm)
290 {
291   vlib_thread_main_t *tm = vlib_get_thread_main ();
292   dpdk_main_t *dm = &dpdk_main;
293
294   vlib_cli_output (vm,
295                    "dpdk:   (0x%04x) enabled:%d monitor:%d drop_all:%d\n"
296                    "  dpdk_queue_hi_thresh          %d\n"
297                    "  consec_full_frames_hi_thresh  %d\n"
298                    "---------\n"
299                    "worker: (0x%04x) enabled:%d monitor:%d\n"
300                    "  worker_queue_hi_thresh        %d\n",
301                    dm->efd.enabled,
302                    ((dm->efd.enabled & DPDK_EFD_DISCARD_ENABLED) ? 1 : 0),
303                    ((dm->efd.enabled & DPDK_EFD_MONITOR_ENABLED) ? 1 : 0),
304                    ((dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED) ? 1 : 0),
305                    dm->efd.queue_hi_thresh,
306                    dm->efd.consec_full_frames_hi_thresh,
307                    tm->efd.enabled,
308                    ((tm->efd.enabled & VLIB_EFD_DISCARD_ENABLED) ? 1 : 0),
309                    ((dm->efd.enabled & VLIB_EFD_MONITOR_ENABLED) ? 1 : 0),
310                    tm->efd.queue_hi_thresh);
311   vlib_cli_output (vm,
312                    "---------\n"
313                    "ip_prec_bitmap   0x%02x\n"
314                    "mpls_exp_bitmap  0x%02x\n"
315                    "vlan_cos_bitmap  0x%02x\n",
316                    tm->efd.ip_prec_bitmap,
317                    tm->efd.mpls_exp_bitmap, tm->efd.vlan_cos_bitmap);
318 }
319
320 static clib_error_t *
321 show_efd (vlib_main_t * vm,
322           unformat_input_t * input, vlib_cli_command_t * cmd)
323 {
324
325   if (unformat (input, "config"))
326     {
327       show_efd_config (vm);
328     }
329   else if (unformat (input, "dpdk"))
330     {
331       dpdk_main_t *dm = &dpdk_main;
332       dpdk_device_t *xd;
333       u32 device_id = ~0;
334
335       (void) unformat (input, "device %d", &device_id);
336         /* *INDENT-OFF* */
337         vec_foreach (xd, dm->devices)
338           {
339             if ((xd->device_index == device_id) || (device_id == ~0))
340               {
341                 show_dpdk_device_stats(vm, xd);
342               }
343           }
344         /* *INDENT-ON* */
345     }
346   else if (unformat (input, "worker"))
347     {
348       vlib_thread_main_t *tm = vlib_get_thread_main ();
349       vlib_thread_registration_t *tr;
350       u32 num_workers = 0;
351       u32 first_worker_index = 0;
352       uword *p;
353
354       p = hash_get_mem (tm->thread_registrations_by_name, "workers");
355       ASSERT (p);
356       tr = (vlib_thread_registration_t *) p[0];
357       if (tr)
358         {
359           num_workers = tr->count;
360           first_worker_index = tr->first_index;
361         }
362
363       vlib_cli_output (vm,
364                        "num_workers               %d\n"
365                        "first_worker_index        %d\n",
366                        num_workers, first_worker_index);
367
368     }
369   else if (unformat (input, "help"))
370     {
371       vlib_cli_output (vm, "Usage: show efd config | "
372                        "dpdk [device <id>] | worker\n");
373     }
374   else
375     {
376       show_efd_config (vm);
377     }
378
379   return 0;
380 }
381
382 /* *INDENT-OFF* */
383 VLIB_CLI_COMMAND (show_efd_command, static) = {
384   .path = "show efd",
385   .short_help = "Show efd [device <id>] | [config]",
386   .function = show_efd,
387 };
388 /* *INDENT-ON* */
389
390 static clib_error_t *
391 clear_efd (vlib_main_t * vm,
392            unformat_input_t * input, vlib_cli_command_t * cmd)
393 {
394   dpdk_main_t *dm = &dpdk_main;
395   dpdk_device_t *xd;
396
397     /* *INDENT-OFF* */
398     vec_foreach (xd, dm->devices)
399       {
400         xd->efd_agent.last_burst_sz = 0;
401         xd->efd_agent.max_burst_sz = 0;
402         xd->efd_agent.full_frames_cnt = 0;
403         xd->efd_agent.consec_full_frames_cnt = 0;
404         xd->efd_agent.congestion_cnt = 0;
405         xd->efd_agent.last_poll_time = 0;
406         xd->efd_agent.max_poll_delay = 0;
407         xd->efd_agent.discard_cnt = 0;
408         xd->efd_agent.total_packet_cnt = 0;
409       }
410     /* *INDENT-ON* */
411
412   return 0;
413 }
414
415 /* *INDENT-OFF* */
416 VLIB_CLI_COMMAND (clear_efd_command,static) = {
417   .path = "clear efd",
418   .short_help = "Clear early-fast-discard counters",
419   .function = clear_efd,
420 };
421 /* *INDENT-ON* */
422
423 static clib_error_t *
424 parse_op_and_prec (vlib_main_t * vm, unformat_input_t * input,
425                    vlib_cli_command_t * cmd,
426                    char *prec_type, u8 * prec_bitmap)
427 {
428   clib_error_t *error = NULL;
429   u8 op = 0;
430   u8 prec = 0;
431
432   if (unformat (input, "ge"))
433     {
434       op = EFD_OPERATION_GREATER_OR_EQUAL;
435     }
436   else if (unformat (input, "lt"))
437     {
438       op = EFD_OPERATION_LESS_THAN;
439     }
440   else if (unformat (input, "help"))
441     {
442       vlib_cli_output (vm, "enter operation [ge | lt] and precedence <0-7>)");
443       return (error);
444     }
445   else
446     {
447       return clib_error_return (0, "unknown input `%U'",
448                                 format_unformat_error, input);
449     }
450
451   if (unformat (input, "%u", &prec))
452     {
453       if (prec > 7)
454         {
455           return clib_error_return (0, "precedence %d is out of range <0-7>",
456                                     prec);
457         }
458     }
459   else
460     {
461       return clib_error_return (0, "unknown input `%U'",
462                                 format_unformat_error, input);
463     }
464
465   set_efd_bitmap (prec_bitmap, prec, op);
466
467   vlib_cli_output (vm,
468                    "EFD will be set for %s precedence %s%u%s.",
469                    prec_type,
470                    (op == EFD_OPERATION_LESS_THAN) ? "less than " : "",
471                    prec,
472                    (op ==
473                     EFD_OPERATION_GREATER_OR_EQUAL) ? " and greater" : "");
474
475   return (error);
476 }
477
478
479 static clib_error_t *
480 set_efd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
481 {
482   dpdk_main_t *dm = &dpdk_main;
483   vlib_thread_main_t *tm = vlib_get_thread_main ();
484   clib_error_t *error = NULL;
485   vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
486
487   if (unformat (input, "enable"))
488     {
489       if (unformat (input, "dpdk"))
490         {
491           dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
492         }
493       else if (unformat (input, "worker"))
494         {
495           tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
496         }
497       else if (unformat (input, "monitor"))
498         {
499           dm->efd.enabled |= DPDK_EFD_MONITOR_ENABLED;
500           tm->efd.enabled |= VLIB_EFD_MONITOR_ENABLED;
501         }
502       else if (unformat (input, "drop_all"))
503         {
504           dm->efd.enabled |= DPDK_EFD_DROPALL_ENABLED;
505         }
506       else if (unformat (input, "default"))
507         {
508           dm->efd.enabled = (DPDK_EFD_DISCARD_ENABLED |
509                              DPDK_EFD_MONITOR_ENABLED);
510           tm->efd.enabled = (VLIB_EFD_DISCARD_ENABLED |
511                              VLIB_EFD_MONITOR_ENABLED);
512         }
513       else
514         {
515           return clib_error_return (0, "Usage: set efd enable [dpdk | "
516                                     "worker | monitor | drop_all | default]");
517         }
518     }
519   else if (unformat (input, "disable"))
520     {
521       if (unformat (input, "dpdk"))
522         {
523           dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
524         }
525       else if (unformat (input, "worker"))
526         {
527           tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
528         }
529       else if (unformat (input, "monitor"))
530         {
531           dm->efd.enabled &= ~DPDK_EFD_MONITOR_ENABLED;
532           tm->efd.enabled &= ~VLIB_EFD_MONITOR_ENABLED;
533         }
534       else if (unformat (input, "drop_all"))
535         {
536           dm->efd.enabled &= ~DPDK_EFD_DROPALL_ENABLED;
537         }
538       else if (unformat (input, "all"))
539         {
540           dm->efd.enabled = 0;
541           tm->efd.enabled = 0;
542         }
543       else
544         {
545           return clib_error_return (0, "Usage: set efd disable [dpdk | "
546                                     "worker | monitor | drop_all | all]");
547         }
548     }
549   else if (unformat (input, "worker_queue_hi_thresh"))
550     {
551       u32 mark;
552       if (unformat (input, "%u", &mark))
553         {
554           tm->efd.queue_hi_thresh = mark;
555         }
556       else
557         {
558           return clib_error_return (0, "unknown input `%U'",
559                                     format_unformat_error, input);
560         }
561     }
562   else if (unformat (input, "dpdk_device_hi_thresh"))
563     {
564       u32 thresh;
565       if (unformat (input, "%u", &thresh))
566         {
567           dm->efd.queue_hi_thresh = thresh;
568         }
569       else
570         {
571           return clib_error_return (0, "unknown input `%U'",
572                                     format_unformat_error, input);
573         }
574     }
575   else if (unformat (input, "consec_full_frames_hi_thresh"))
576     {
577       u32 thresh;
578       if (unformat (input, "%u", &thresh))
579         {
580           dm->efd.consec_full_frames_hi_thresh = thresh;
581         }
582       else
583         {
584           return clib_error_return (0, "unknown input `%U'",
585                                     format_unformat_error, input);
586         }
587     }
588   else if (unformat (input, "ip-prec"))
589     {
590       return (parse_op_and_prec (vm, input, cmd,
591                                  "ip", &tm->efd.ip_prec_bitmap));
592     }
593   else if (unformat (input, "mpls-exp"))
594     {
595       return (parse_op_and_prec (vm, input, cmd,
596                                  "mpls", &tm->efd.mpls_exp_bitmap));
597     }
598   else if (unformat (input, "vlan-cos"))
599     {
600       return (parse_op_and_prec (vm, input, cmd,
601                                  "vlan", &tm->efd.vlan_cos_bitmap));
602     }
603   else if (unformat (input, "help"))
604     {
605       vlib_cli_output (vm,
606                        "Usage:\n"
607                        "  set efd enable <dpdk | worker | monitor | drop_all | default> |\n"
608                        "  set efd disable <dpdk | worker | monitor | drop_all | all> |\n"
609                        "  set efd <ip-prec | mpls-exp | vlan-cos> <ge | lt> <0-7>\n"
610                        "  set efd worker_queue_hi_thresh <0-32> |\n"
611                        "  set efd dpdk_device_hi_thresh <0-%d> |\n"
612                        "  set efd consec_full_frames_hi_thresh <count> |\n",
613                        DPDK_NB_RX_DESC_10GE);
614     }
615   else
616     {
617       return clib_error_return (0, "unknown input `%U'",
618                                 format_unformat_error, input);
619     }
620
621   if (dm->efd.enabled)
622     rt->function = dpdk_input_efd_multiarch_select ();
623   else if (dm->use_rss)
624     rt->function = dpdk_input_rss_multiarch_select ();
625   else
626     rt->function = dpdk_input_multiarch_select ();
627
628   return error;
629 }
630
631 /* *INDENT-OFF* */
632 VLIB_CLI_COMMAND (cmd_set_efd,static) = {
633     .path = "set efd",
634     .short_help = "set early-fast-discard commands",
635     .function = set_efd,
636 };
637 /* *INDENT-ON* */
638
639 static clib_error_t *
640 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
641                   vlib_cli_command_t * cmd)
642 {
643   unformat_input_t _line_input, *line_input = &_line_input;
644   dpdk_main_t *dm = &dpdk_main;
645   vnet_hw_interface_t *hw;
646   dpdk_device_t *xd;
647   u32 hw_if_index = (u32) ~ 0;
648   u32 nb_rx_desc = (u32) ~ 0;
649   u32 nb_tx_desc = (u32) ~ 0;
650   clib_error_t *rv;
651
652   if (!unformat_user (input, unformat_line_input, line_input))
653     return 0;
654
655   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
656     {
657       if (unformat
658           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
659            &hw_if_index))
660         ;
661       else if (unformat (line_input, "tx %d", &nb_tx_desc))
662         ;
663       else if (unformat (line_input, "rx %d", &nb_rx_desc))
664         ;
665       else
666         return clib_error_return (0, "parse error: '%U'",
667                                   format_unformat_error, line_input);
668     }
669
670   unformat_free (line_input);
671
672   if (hw_if_index == (u32) ~ 0)
673     return clib_error_return (0, "please specify valid interface name");
674
675   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
676   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
677
678   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
679     return clib_error_return (0, "number of descriptors can be set only for "
680                               "physical devices");
681
682   if ((nb_rx_desc == (u32) ~ 0 || nb_rx_desc == xd->nb_rx_desc) &&
683       (nb_tx_desc == (u32) ~ 0 || nb_tx_desc == xd->nb_tx_desc))
684     return clib_error_return (0, "nothing changed");
685
686   if (nb_rx_desc != (u32) ~ 0)
687     xd->nb_rx_desc = nb_rx_desc;
688
689   if (nb_tx_desc != (u32) ~ 0)
690     xd->nb_rx_desc = nb_rx_desc;
691
692   rv = dpdk_port_setup (dm, xd);
693
694   return rv;
695 }
696
697 /* *INDENT-OFF* */
698 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
699     .path = "set dpdk interface descriptors",
700     .short_help = "set dpdk interface descriptors <if-name> [rx <n>] [tx <n>]",
701     .function = set_dpdk_if_desc,
702 };
703 /* *INDENT-ON* */
704
705 static clib_error_t *
706 show_dpdk_if_placement (vlib_main_t * vm, unformat_input_t * input,
707                         vlib_cli_command_t * cmd)
708 {
709   vlib_thread_main_t *tm = vlib_get_thread_main ();
710   dpdk_main_t *dm = &dpdk_main;
711   dpdk_device_and_queue_t *dq;
712   int cpu;
713
714   if (tm->n_vlib_mains == 1)
715     vlib_cli_output (vm, "All interfaces are handled by main thread");
716
717   for (cpu = 0; cpu < vec_len (dm->devices_by_cpu); cpu++)
718     {
719       if (vec_len (dm->devices_by_cpu[cpu]))
720         vlib_cli_output (vm, "Thread %u (%s at lcore %u):", cpu,
721                          vlib_worker_threads[cpu].name,
722                          vlib_worker_threads[cpu].lcore_id);
723
724       /* *INDENT-OFF* */
725       vec_foreach(dq, dm->devices_by_cpu[cpu])
726         {
727           u32 hw_if_index = dm->devices[dq->device].vlib_hw_if_index;
728           vnet_hw_interface_t * hi =  vnet_get_hw_interface(dm->vnet_main, hw_if_index);
729           vlib_cli_output(vm, "  %v queue %u", hi->name, dq->queue_id);
730         }
731       /* *INDENT-ON* */
732     }
733   return 0;
734 }
735
736 /* *INDENT-OFF* */
737 VLIB_CLI_COMMAND (cmd_show_dpdk_if_placement,static) = {
738     .path = "show dpdk interface placement",
739     .short_help = "show dpdk interface placement",
740     .function = show_dpdk_if_placement,
741 };
742 /* *INDENT-ON* */
743
744 static int
745 dpdk_device_queue_sort (void *a1, void *a2)
746 {
747   dpdk_device_and_queue_t *dq1 = a1;
748   dpdk_device_and_queue_t *dq2 = a2;
749
750   if (dq1->device > dq2->device)
751     return 1;
752   else if (dq1->device < dq2->device)
753     return -1;
754   else if (dq1->queue_id > dq2->queue_id)
755     return 1;
756   else if (dq1->queue_id < dq2->queue_id)
757     return -1;
758   else
759     return 0;
760 }
761
762 static clib_error_t *
763 set_dpdk_if_placement (vlib_main_t * vm, unformat_input_t * input,
764                        vlib_cli_command_t * cmd)
765 {
766   unformat_input_t _line_input, *line_input = &_line_input;
767   dpdk_main_t *dm = &dpdk_main;
768   dpdk_device_and_queue_t *dq;
769   vnet_hw_interface_t *hw;
770   dpdk_device_t *xd;
771   u32 hw_if_index = (u32) ~ 0;
772   u32 queue = (u32) 0;
773   u32 cpu = (u32) ~ 0;
774   int i;
775
776   if (!unformat_user (input, unformat_line_input, line_input))
777     return 0;
778
779   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
780     {
781       if (unformat
782           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
783            &hw_if_index))
784         ;
785       else if (unformat (line_input, "queue %d", &queue))
786         ;
787       else if (unformat (line_input, "thread %d", &cpu))
788         ;
789       else
790         return clib_error_return (0, "parse error: '%U'",
791                                   format_unformat_error, line_input);
792     }
793
794   unformat_free (line_input);
795
796   if (hw_if_index == (u32) ~ 0)
797     return clib_error_return (0, "please specify valid interface name");
798
799   if (cpu < dm->input_cpu_first_index ||
800       cpu >= (dm->input_cpu_first_index + dm->input_cpu_count))
801     return clib_error_return (0, "please specify valid thread id");
802
803   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
804   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
805
806   for (i = 0; i < vec_len (dm->devices_by_cpu); i++)
807     {
808       /* *INDENT-OFF* */
809       vec_foreach(dq, dm->devices_by_cpu[i])
810         {
811           if (hw_if_index == dm->devices[dq->device].vlib_hw_if_index &&
812               queue == dq->queue_id)
813             {
814               if (cpu == i) /* nothing to do */
815                 return 0;
816
817               vec_del1(dm->devices_by_cpu[i], dq - dm->devices_by_cpu[i]);
818               vec_add2(dm->devices_by_cpu[cpu], dq, 1);
819               dq->queue_id = queue;
820               dq->device = xd->device_index;
821               xd->cpu_socket_id_by_queue[queue] =
822                 rte_lcore_to_socket_id(vlib_worker_threads[cpu].lcore_id);
823
824               vec_sort_with_function(dm->devices_by_cpu[i],
825                                      dpdk_device_queue_sort);
826
827               vec_sort_with_function(dm->devices_by_cpu[cpu],
828                                      dpdk_device_queue_sort);
829
830               if (vec_len(dm->devices_by_cpu[i]) == 0)
831                 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
832                                      VLIB_NODE_STATE_DISABLED);
833
834               if (vec_len(dm->devices_by_cpu[cpu]) == 1)
835                 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
836                                      VLIB_NODE_STATE_POLLING);
837
838               return 0;
839             }
840         }
841       /* *INDENT-ON* */
842     }
843
844   return clib_error_return (0, "not found");
845 }
846
847 /* *INDENT-OFF* */
848 VLIB_CLI_COMMAND (cmd_set_dpdk_if_placement,static) = {
849     .path = "set dpdk interface placement",
850     .short_help = "set dpdk interface placement <if-name> [queue <n>]  thread <n>",
851     .function = set_dpdk_if_placement,
852 };
853 /* *INDENT-ON* */
854
855 static clib_error_t *
856 show_dpdk_if_hqos_placement (vlib_main_t * vm, unformat_input_t * input,
857                              vlib_cli_command_t * cmd)
858 {
859   vlib_thread_main_t *tm = vlib_get_thread_main ();
860   dpdk_main_t *dm = &dpdk_main;
861   dpdk_device_and_queue_t *dq;
862   int cpu;
863
864   if (tm->n_vlib_mains == 1)
865     vlib_cli_output (vm, "All interfaces are handled by main thread");
866
867   for (cpu = 0; cpu < vec_len (dm->devices_by_hqos_cpu); cpu++)
868     {
869       if (vec_len (dm->devices_by_hqos_cpu[cpu]))
870         vlib_cli_output (vm, "Thread %u (%s at lcore %u):", cpu,
871                          vlib_worker_threads[cpu].name,
872                          vlib_worker_threads[cpu].lcore_id);
873
874       vec_foreach (dq, dm->devices_by_hqos_cpu[cpu])
875       {
876         u32 hw_if_index = dm->devices[dq->device].vlib_hw_if_index;
877         vnet_hw_interface_t *hi =
878           vnet_get_hw_interface (dm->vnet_main, hw_if_index);
879         vlib_cli_output (vm, "  %v queue %u", hi->name, dq->queue_id);
880       }
881     }
882   return 0;
883 }
884
885 /* *INDENT-OFF* */
886 VLIB_CLI_COMMAND (cmd_show_dpdk_if_hqos_placement, static) = {
887   .path = "show dpdk interface hqos placement",
888   .short_help = "show dpdk interface hqos placement",
889   .function = show_dpdk_if_hqos_placement,
890 };
891 /* *INDENT-ON* */
892
893 static clib_error_t *
894 set_dpdk_if_hqos_placement (vlib_main_t * vm, unformat_input_t * input,
895                             vlib_cli_command_t * cmd)
896 {
897   unformat_input_t _line_input, *line_input = &_line_input;
898   dpdk_main_t *dm = &dpdk_main;
899   dpdk_device_and_queue_t *dq;
900   vnet_hw_interface_t *hw;
901   dpdk_device_t *xd;
902   u32 hw_if_index = (u32) ~ 0;
903   u32 cpu = (u32) ~ 0;
904   int i;
905
906   if (!unformat_user (input, unformat_line_input, line_input))
907     return 0;
908
909   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
910     {
911       if (unformat
912           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
913            &hw_if_index))
914         ;
915       else if (unformat (line_input, "thread %d", &cpu))
916         ;
917       else
918         return clib_error_return (0, "parse error: '%U'",
919                                   format_unformat_error, line_input);
920     }
921
922   unformat_free (line_input);
923
924   if (hw_if_index == (u32) ~ 0)
925     return clib_error_return (0, "please specify valid interface name");
926
927   if (cpu < dm->hqos_cpu_first_index ||
928       cpu >= (dm->hqos_cpu_first_index + dm->hqos_cpu_count))
929     return clib_error_return (0, "please specify valid thread id");
930
931   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
932   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
933
934   for (i = 0; i < vec_len (dm->devices_by_hqos_cpu); i++)
935     {
936       vec_foreach (dq, dm->devices_by_hqos_cpu[i])
937       {
938         if (hw_if_index == dm->devices[dq->device].vlib_hw_if_index)
939           {
940             if (cpu == i)       /* nothing to do */
941               return 0;
942
943             vec_del1 (dm->devices_by_hqos_cpu[i],
944                       dq - dm->devices_by_hqos_cpu[i]);
945             vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
946             dq->queue_id = 0;
947             dq->device = xd->device_index;
948
949             vec_sort_with_function (dm->devices_by_hqos_cpu[i],
950                                     dpdk_device_queue_sort);
951
952             vec_sort_with_function (dm->devices_by_hqos_cpu[cpu],
953                                     dpdk_device_queue_sort);
954
955             return 0;
956           }
957       }
958     }
959
960   return clib_error_return (0, "not found");
961 }
962
963 /* *INDENT-OFF* */
964 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_placement, static) = {
965   .path = "set dpdk interface hqos placement",
966   .short_help = "set dpdk interface hqos placement <if-name> thread <n>",
967   .function = set_dpdk_if_hqos_placement,
968 };
969 /* *INDENT-ON* */
970
971 static clib_error_t *
972 set_dpdk_if_hqos_pipe (vlib_main_t * vm, unformat_input_t * input,
973                        vlib_cli_command_t * cmd)
974 {
975   unformat_input_t _line_input, *line_input = &_line_input;
976   dpdk_main_t *dm = &dpdk_main;
977   vnet_hw_interface_t *hw;
978   dpdk_device_t *xd;
979   u32 hw_if_index = (u32) ~ 0;
980   u32 subport_id = (u32) ~ 0;
981   u32 pipe_id = (u32) ~ 0;
982   u32 profile_id = (u32) ~ 0;
983   int rv;
984
985   if (!unformat_user (input, unformat_line_input, line_input))
986     return 0;
987
988   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
989     {
990       if (unformat
991           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
992            &hw_if_index))
993         ;
994       else if (unformat (line_input, "subport %d", &subport_id))
995         ;
996       else if (unformat (line_input, "pipe %d", &pipe_id))
997         ;
998       else if (unformat (line_input, "profile %d", &profile_id))
999         ;
1000       else
1001         return clib_error_return (0, "parse error: '%U'",
1002                                   format_unformat_error, line_input);
1003     }
1004
1005   unformat_free (line_input);
1006
1007   if (hw_if_index == (u32) ~ 0)
1008     return clib_error_return (0, "please specify valid interface name");
1009
1010   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1011   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1012
1013   rv =
1014     rte_sched_pipe_config (xd->hqos_ht->hqos, subport_id, pipe_id,
1015                            profile_id);
1016   if (rv)
1017     return clib_error_return (0, "pipe configuration failed");
1018
1019   return 0;
1020 }
1021
1022 /* *INDENT-OFF* */
1023 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_pipe, static) =
1024 {
1025   .path = "set dpdk interface hqos pipe",
1026   .short_help = "set dpdk interface hqos pipe <if-name> subport <n> pipe <n> "
1027                   "profile <n>",
1028   .function = set_dpdk_if_hqos_pipe,
1029 };
1030 /* *INDENT-ON* */
1031
1032 static clib_error_t *
1033 set_dpdk_if_hqos_subport (vlib_main_t * vm, unformat_input_t * input,
1034                           vlib_cli_command_t * cmd)
1035 {
1036   unformat_input_t _line_input, *line_input = &_line_input;
1037   dpdk_main_t *dm = &dpdk_main;
1038   vnet_hw_interface_t *hw;
1039   dpdk_device_t *xd;
1040   u32 hw_if_index = (u32) ~ 0;
1041   u32 subport_id = (u32) ~ 0;
1042   struct rte_sched_subport_params p = {
1043     .tb_rate = 1250000000,      /* 10GbE */
1044     .tb_size = 1000000,
1045     .tc_rate = {1250000000, 1250000000, 1250000000, 1250000000},
1046     .tc_period = 10,
1047   };
1048   int rv;
1049
1050   if (!unformat_user (input, unformat_line_input, line_input))
1051     return 0;
1052
1053   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1054     {
1055       if (unformat
1056           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1057            &hw_if_index))
1058         ;
1059       else if (unformat (line_input, "subport %d", &subport_id))
1060         ;
1061       else if (unformat (line_input, "rate %d", &p.tb_rate))
1062         {
1063           p.tc_rate[0] = p.tb_rate;
1064           p.tc_rate[1] = p.tb_rate;
1065           p.tc_rate[2] = p.tb_rate;
1066           p.tc_rate[3] = p.tb_rate;
1067         }
1068       else if (unformat (line_input, "bktsize %d", &p.tb_size))
1069         ;
1070       else if (unformat (line_input, "tc0 %d", &p.tc_rate[0]))
1071         ;
1072       else if (unformat (line_input, "tc1 %d", &p.tc_rate[1]))
1073         ;
1074       else if (unformat (line_input, "tc2 %d", &p.tc_rate[2]))
1075         ;
1076       else if (unformat (line_input, "tc3 %d", &p.tc_rate[3]))
1077         ;
1078       else if (unformat (line_input, "period %d", &p.tc_period))
1079         ;
1080       else
1081         return clib_error_return (0, "parse error: '%U'",
1082                                   format_unformat_error, line_input);
1083     }
1084
1085   unformat_free (line_input);
1086
1087   if (hw_if_index == (u32) ~ 0)
1088     return clib_error_return (0, "please specify valid interface name");
1089
1090   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1091   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1092
1093   rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport_id, &p);
1094   if (rv)
1095     return clib_error_return (0, "subport configuration failed");
1096
1097   return 0;
1098 }
1099
1100 /* *INDENT-OFF* */
1101 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_subport, static) = {
1102   .path = "set dpdk interface hqos subport",
1103   .short_help = "set dpdk interface hqos subport <if-name> subport <n> "
1104                  "[rate <n>] [bktsize <n>] [tc0 <n>] [tc1 <n>] [tc2 <n>] [tc3 <n>] "
1105                  "[period <n>]",
1106   .function = set_dpdk_if_hqos_subport,
1107 };
1108 /* *INDENT-ON* */
1109
1110 static clib_error_t *
1111 set_dpdk_if_hqos_tctbl (vlib_main_t * vm, unformat_input_t * input,
1112                         vlib_cli_command_t * cmd)
1113 {
1114   unformat_input_t _line_input, *line_input = &_line_input;
1115   vlib_thread_main_t *tm = vlib_get_thread_main ();
1116   dpdk_main_t *dm = &dpdk_main;
1117   vnet_hw_interface_t *hw;
1118   dpdk_device_t *xd;
1119   u32 hw_if_index = (u32) ~ 0;
1120   u32 tc = (u32) ~ 0;
1121   u32 queue = (u32) ~ 0;
1122   u32 entry = (u32) ~ 0;
1123   u32 val, i;
1124
1125   if (!unformat_user (input, unformat_line_input, line_input))
1126     return 0;
1127
1128   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1129     {
1130       if (unformat
1131           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1132            &hw_if_index))
1133         ;
1134       else if (unformat (line_input, "entry %d", &entry))
1135         ;
1136       else if (unformat (line_input, "tc %d", &tc))
1137         ;
1138       else if (unformat (line_input, "queue %d", &queue))
1139         ;
1140       else
1141         return clib_error_return (0, "parse error: '%U'",
1142                                   format_unformat_error, line_input);
1143     }
1144
1145   unformat_free (line_input);
1146
1147   if (hw_if_index == (u32) ~ 0)
1148     return clib_error_return (0, "please specify valid interface name");
1149   if (entry >= 64)
1150     return clib_error_return (0, "invalid entry");
1151   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
1152     return clib_error_return (0, "invalid traffic class");
1153   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
1154     return clib_error_return (0, "invalid traffic class");
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     return clib_error_return (0, "no worker registrations?");
1164
1165   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1166   int worker_thread_first = tr->first_index;
1167   int worker_thread_count = tr->count;
1168
1169   val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1170   for (i = 0; i < worker_thread_count; i++)
1171     xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
1172
1173   return 0;
1174 }
1175
1176 /* *INDENT-OFF* */
1177 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_tctbl, static) = {
1178   .path = "set dpdk interface hqos tctbl",
1179   .short_help = "set dpdk interface hqos tctbl <if-name> entry <n> tc <n> queue <n>",
1180   .function = set_dpdk_if_hqos_tctbl,
1181 };
1182 /* *INDENT-ON* */
1183
1184 static clib_error_t *
1185 set_dpdk_if_hqos_pktfield (vlib_main_t * vm, unformat_input_t * input,
1186                            vlib_cli_command_t * cmd)
1187 {
1188   unformat_input_t _line_input, *line_input = &_line_input;
1189   vlib_thread_main_t *tm = vlib_get_thread_main ();
1190   dpdk_main_t *dm = &dpdk_main;
1191
1192   /* Device specific data */
1193   struct rte_eth_dev_info dev_info;
1194   dpdk_device_config_t *devconf = 0;
1195   vnet_hw_interface_t *hw;
1196   dpdk_device_t *xd;
1197   u32 hw_if_index = (u32) ~ 0;
1198
1199   /* Detect the set of worker threads */
1200   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1201   /* Should never happen, shut up Coverity warning */
1202   if (p == 0)
1203     return clib_error_return (0, "no worker registrations?");
1204
1205   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1206   int worker_thread_first = tr->first_index;
1207   int worker_thread_count = tr->count;
1208
1209   /* Packet field configuration */
1210   u64 mask = (u64) ~ 0;
1211   u32 id = (u32) ~ 0;
1212   u32 offset = (u32) ~ 0;
1213
1214   /* HQoS params */
1215   u32 n_subports_per_port, n_pipes_per_subport, tctbl_size;
1216
1217   u32 i;
1218
1219   /* Parse input arguments */
1220   if (!unformat_user (input, unformat_line_input, line_input))
1221     return 0;
1222
1223   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1224     {
1225       if (unformat
1226           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1227            &hw_if_index))
1228         ;
1229       else if (unformat (line_input, "id %d", &id))
1230         ;
1231       else if (unformat (line_input, "offset %d", &offset))
1232         ;
1233       else if (unformat (line_input, "mask %llx", &mask))
1234         ;
1235       else
1236         return clib_error_return (0, "parse error: '%U'",
1237                                   format_unformat_error, line_input);
1238     }
1239
1240   unformat_free (line_input);
1241
1242   /* Get interface */
1243   if (hw_if_index == (u32) ~ 0)
1244     return clib_error_return (0, "please specify valid interface name");
1245
1246   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1247   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1248
1249   rte_eth_dev_info_get (xd->device_index, &dev_info);
1250   if (dev_info.pci_dev)
1251     {                           /* bonded interface has no pci info */
1252       vlib_pci_addr_t pci_addr;
1253
1254       pci_addr.domain = dev_info.pci_dev->addr.domain;
1255       pci_addr.bus = dev_info.pci_dev->addr.bus;
1256       pci_addr.slot = dev_info.pci_dev->addr.devid;
1257       pci_addr.function = dev_info.pci_dev->addr.function;
1258
1259       p =
1260         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1261     }
1262
1263   if (p)
1264     devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
1265   else
1266     devconf = &dm->conf->default_devconf;
1267
1268   if (devconf->hqos_enabled == 0)
1269     {
1270       vlib_cli_output (vm, "HQoS disabled for this interface");
1271       return 0;
1272     }
1273
1274   n_subports_per_port = devconf->hqos.port.n_subports_per_port;
1275   n_pipes_per_subport = devconf->hqos.port.n_pipes_per_subport;
1276   tctbl_size = RTE_DIM (devconf->hqos.tc_table);
1277
1278   /* Validate packet field configuration: id, offset and mask */
1279   if (id >= 3)
1280     return clib_error_return (0, "invalid packet field id");
1281
1282   switch (id)
1283     {
1284     case 0:
1285       if (dpdk_hqos_validate_mask (mask, n_subports_per_port) != 0)
1286         return clib_error_return (0, "invalid subport ID mask "
1287                                   "(n_subports_per_port = %u)",
1288                                   n_subports_per_port);
1289       break;
1290     case 1:
1291       if (dpdk_hqos_validate_mask (mask, n_pipes_per_subport) != 0)
1292         return clib_error_return (0, "invalid pipe ID mask "
1293                                   "(n_pipes_per_subport = %u)",
1294                                   n_pipes_per_subport);
1295       break;
1296     case 2:
1297     default:
1298       if (dpdk_hqos_validate_mask (mask, tctbl_size) != 0)
1299         return clib_error_return (0, "invalid TC table index mask "
1300                                   "(TC table size = %u)", tctbl_size);
1301     }
1302
1303   /* Propagate packet field configuration to all workers */
1304   for (i = 0; i < worker_thread_count; i++)
1305     switch (id)
1306       {
1307       case 0:
1308         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabpos = offset;
1309         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabmask = mask;
1310         xd->hqos_wt[worker_thread_first + i].hqos_field0_slabshr =
1311           __builtin_ctzll (mask);
1312         break;
1313       case 1:
1314         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabpos = offset;
1315         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabmask = mask;
1316         xd->hqos_wt[worker_thread_first + i].hqos_field1_slabshr =
1317           __builtin_ctzll (mask);
1318         break;
1319       case 2:
1320       default:
1321         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabpos = offset;
1322         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabmask = mask;
1323         xd->hqos_wt[worker_thread_first + i].hqos_field2_slabshr =
1324           __builtin_ctzll (mask);
1325       }
1326
1327   return 0;
1328 }
1329
1330 /* *INDENT-OFF* */
1331 VLIB_CLI_COMMAND (cmd_set_dpdk_if_hqos_pktfield, static) = {
1332   .path = "set dpdk interface hqos pktfield",
1333   .short_help = "set dpdk interface hqos pktfield <if-name> id <n> offset <n> "
1334                  "mask <n>",
1335   .function = set_dpdk_if_hqos_pktfield,
1336 };
1337 /* *INDENT-ON* */
1338
1339 static clib_error_t *
1340 show_dpdk_if_hqos (vlib_main_t * vm, unformat_input_t * input,
1341                    vlib_cli_command_t * cmd)
1342 {
1343   unformat_input_t _line_input, *line_input = &_line_input;
1344   vlib_thread_main_t *tm = vlib_get_thread_main ();
1345   dpdk_main_t *dm = &dpdk_main;
1346   vnet_hw_interface_t *hw;
1347   dpdk_device_t *xd;
1348   dpdk_device_config_hqos_t *cfg;
1349   dpdk_device_hqos_per_hqos_thread_t *ht;
1350   dpdk_device_hqos_per_worker_thread_t *wk;
1351   u32 *tctbl;
1352   u32 hw_if_index = (u32) ~ 0;
1353   u32 profile_id, i;
1354   struct rte_eth_dev_info dev_info;
1355   dpdk_device_config_t *devconf = 0;
1356   vlib_thread_registration_t *tr;
1357   uword *p = 0;
1358
1359   if (!unformat_user (input, unformat_line_input, line_input))
1360     return 0;
1361
1362   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1363     {
1364       if (unformat
1365           (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
1366            &hw_if_index))
1367         ;
1368       else
1369         return clib_error_return (0, "parse error: '%U'",
1370                                   format_unformat_error, line_input);
1371     }
1372
1373   unformat_free (line_input);
1374
1375   if (hw_if_index == (u32) ~ 0)
1376     return clib_error_return (0, "please specify interface name!!");
1377
1378   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1379   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1380
1381   rte_eth_dev_info_get (xd->device_index, &dev_info);
1382   if (dev_info.pci_dev)
1383     {                           /* bonded interface has no pci info */
1384       vlib_pci_addr_t pci_addr;
1385
1386       pci_addr.domain = dev_info.pci_dev->addr.domain;
1387       pci_addr.bus = dev_info.pci_dev->addr.bus;
1388       pci_addr.slot = dev_info.pci_dev->addr.devid;
1389       pci_addr.function = dev_info.pci_dev->addr.function;
1390
1391       p =
1392         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1393     }
1394
1395   if (p)
1396     devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
1397   else
1398     devconf = &dm->conf->default_devconf;
1399
1400   if (devconf->hqos_enabled == 0)
1401     {
1402       vlib_cli_output (vm, "HQoS disabled for this interface");
1403       return 0;
1404     }
1405
1406   /* Detect the set of worker threads */
1407   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1408
1409   /* Should never happen, shut up Coverity warning */
1410   if (p == 0)
1411     return clib_error_return (0, "no worker registrations?");
1412
1413   tr = (vlib_thread_registration_t *) p[0];
1414
1415   cfg = &devconf->hqos;
1416   ht = xd->hqos_ht;
1417   wk = &xd->hqos_wt[tr->first_index];
1418   tctbl = wk->hqos_tc_table;
1419
1420   vlib_cli_output (vm, " Thread:");
1421   vlib_cli_output (vm, "   Input SWQ size = %u packets", cfg->swq_size);
1422   vlib_cli_output (vm, "   Enqueue burst size = %u packets",
1423                    ht->hqos_burst_enq);
1424   vlib_cli_output (vm, "   Dequeue burst size = %u packets",
1425                    ht->hqos_burst_deq);
1426
1427   vlib_cli_output (vm,
1428                    "   Packet field 0: slab position = %4u, slab bitmask = 0x%016llx",
1429                    wk->hqos_field0_slabpos, wk->hqos_field0_slabmask);
1430   vlib_cli_output (vm,
1431                    "   Packet field 1: slab position = %4u, slab bitmask = 0x%016llx",
1432                    wk->hqos_field1_slabpos, wk->hqos_field1_slabmask);
1433   vlib_cli_output (vm,
1434                    "   Packet field 2: slab position = %4u, slab bitmask = 0x%016llx",
1435                    wk->hqos_field2_slabpos, wk->hqos_field2_slabmask);
1436   vlib_cli_output (vm, "   Packet field 2 translation table:");
1437   vlib_cli_output (vm, "     [ 0 .. 15]: "
1438                    "%2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u",
1439                    tctbl[0], tctbl[1], tctbl[2], tctbl[3],
1440                    tctbl[4], tctbl[5], tctbl[6], tctbl[7],
1441                    tctbl[8], tctbl[9], tctbl[10], tctbl[11],
1442                    tctbl[12], tctbl[13], tctbl[14], tctbl[15]);
1443   vlib_cli_output (vm, "     [16 .. 31]: "
1444                    "%2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u",
1445                    tctbl[16], tctbl[17], tctbl[18], tctbl[19],
1446                    tctbl[20], tctbl[21], tctbl[22], tctbl[23],
1447                    tctbl[24], tctbl[25], tctbl[26], tctbl[27],
1448                    tctbl[28], tctbl[29], tctbl[30], tctbl[31]);
1449   vlib_cli_output (vm, "     [32 .. 47]: "
1450                    "%2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u",
1451                    tctbl[32], tctbl[33], tctbl[34], tctbl[35],
1452                    tctbl[36], tctbl[37], tctbl[38], tctbl[39],
1453                    tctbl[40], tctbl[41], tctbl[42], tctbl[43],
1454                    tctbl[44], tctbl[45], tctbl[46], tctbl[47]);
1455   vlib_cli_output (vm, "     [48 .. 63]: "
1456                    "%2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u %2u",
1457                    tctbl[48], tctbl[49], tctbl[50], tctbl[51],
1458                    tctbl[52], tctbl[53], tctbl[54], tctbl[55],
1459                    tctbl[56], tctbl[57], tctbl[58], tctbl[59],
1460                    tctbl[60], tctbl[61], tctbl[62], tctbl[63]);
1461
1462   vlib_cli_output (vm, " Port:");
1463   vlib_cli_output (vm, "   Rate = %u bytes/second", cfg->port.rate);
1464   vlib_cli_output (vm, "   MTU = %u bytes", cfg->port.mtu);
1465   vlib_cli_output (vm, "   Frame overhead = %u bytes",
1466                    cfg->port.frame_overhead);
1467   vlib_cli_output (vm, "   Number of subports = %u",
1468                    cfg->port.n_subports_per_port);
1469   vlib_cli_output (vm, "   Number of pipes per subport = %u",
1470                    cfg->port.n_pipes_per_subport);
1471   vlib_cli_output (vm,
1472                    "   Packet queue size: TC0 = %u, TC1 = %u, TC2 = %u, TC3 = %u packets",
1473                    cfg->port.qsize[0], cfg->port.qsize[1], cfg->port.qsize[2],
1474                    cfg->port.qsize[3]);
1475   vlib_cli_output (vm, "   Number of pipe profiles = %u",
1476                    cfg->port.n_pipe_profiles);
1477
1478   for (profile_id = 0; profile_id < vec_len (cfg->pipe); profile_id++)
1479     {
1480       vlib_cli_output (vm, " Pipe profile %u:", profile_id);
1481       vlib_cli_output (vm, "   Rate = %u bytes/second",
1482                        cfg->pipe[profile_id].tb_rate);
1483       vlib_cli_output (vm, "   Token bucket size = %u bytes",
1484                        cfg->pipe[profile_id].tb_size);
1485       vlib_cli_output (vm,
1486                        "   Traffic class rate: TC0 = %u, TC1 = %u, TC2 = %u, TC3 = %u bytes/second",
1487                        cfg->pipe[profile_id].tc_rate[0],
1488                        cfg->pipe[profile_id].tc_rate[1],
1489                        cfg->pipe[profile_id].tc_rate[2],
1490                        cfg->pipe[profile_id].tc_rate[3]);
1491       vlib_cli_output (vm, "   TC period = %u milliseconds",
1492                        cfg->pipe[profile_id].tc_period);
1493 #ifdef RTE_SCHED_SUBPORT_TC_OV
1494       vlib_cli_output (vm, "   TC3 oversubscription_weight = %u",
1495                        cfg->pipe[profile_id].tc_ov_weight);
1496 #endif
1497
1498       for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1499         {
1500           vlib_cli_output (vm,
1501                            "   TC%u WRR weights: Q0 = %u, Q1 = %u, Q2 = %u, Q3 = %u",
1502                            i, cfg->pipe[profile_id].wrr_weights[i * 4],
1503                            cfg->pipe[profile_id].wrr_weights[i * 4 + 1],
1504                            cfg->pipe[profile_id].wrr_weights[i * 4 + 2],
1505                            cfg->pipe[profile_id].wrr_weights[i * 4 + 3]);
1506         }
1507     }
1508
1509 #ifdef RTE_SCHED_RED
1510   vlib_cli_output (vm, " Weighted Random Early Detection (WRED):");
1511   for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1512     {
1513       vlib_cli_output (vm, "   TC%u min: G = %u, Y = %u, R = %u", i,
1514                        cfg->port.red_params[i][e_RTE_METER_GREEN].min_th,
1515                        cfg->port.red_params[i][e_RTE_METER_YELLOW].min_th,
1516                        cfg->port.red_params[i][e_RTE_METER_RED].min_th);
1517
1518       vlib_cli_output (vm, "   TC%u max: G = %u, Y = %u, R = %u", i,
1519                        cfg->port.red_params[i][e_RTE_METER_GREEN].max_th,
1520                        cfg->port.red_params[i][e_RTE_METER_YELLOW].max_th,
1521                        cfg->port.red_params[i][e_RTE_METER_RED].max_th);
1522
1523       vlib_cli_output (vm,
1524                        "   TC%u inverted probability: G = %u, Y = %u, R = %u",
1525                        i, cfg->port.red_params[i][e_RTE_METER_GREEN].maxp_inv,
1526                        cfg->port.red_params[i][e_RTE_METER_YELLOW].maxp_inv,
1527                        cfg->port.red_params[i][e_RTE_METER_RED].maxp_inv);
1528
1529       vlib_cli_output (vm, "   TC%u weight: R = %u, Y = %u, R = %u", i,
1530                        cfg->port.red_params[i][e_RTE_METER_GREEN].wq_log2,
1531                        cfg->port.red_params[i][e_RTE_METER_YELLOW].wq_log2,
1532                        cfg->port.red_params[i][e_RTE_METER_RED].wq_log2);
1533     }
1534 #endif
1535
1536   return 0;
1537 }
1538
1539 /* *INDENT-OFF* */
1540 VLIB_CLI_COMMAND (cmd_show_dpdk_if_hqos, static) = {
1541   .path = "show dpdk interface hqos",
1542   .short_help = "show dpdk interface hqos <if-name>",
1543   .function = show_dpdk_if_hqos,
1544 };
1545 /* *INDENT-ON* */
1546
1547 clib_error_t *
1548 dpdk_cli_init (vlib_main_t * vm)
1549 {
1550   return 0;
1551 }
1552
1553 VLIB_INIT_FUNCTION (dpdk_cli_init);
1554
1555 /*
1556  * fd.io coding-style-patch-verification: ON
1557  *
1558  * Local Variables:
1559  * eval: (c-set-style "gnu")
1560  * End:
1561  */