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