Add support for multiple microarchitectures in single binary
[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-gre/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,
31      vlib_cli_command_t * cmd)
32 {
33   dpdk_main_t * dm = &dpdk_main;
34   u8 * filename;
35   u32 max;
36   int matched = 0;
37   clib_error_t * error = 0;
38
39   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
40     {
41       if (unformat (input, "on"))
42         {
43           if (dm->tx_pcap_enable == 0)
44             {
45               if (dm->pcap_filename == 0)
46                 dm->pcap_filename = format (0, "/tmp/vpe.pcap%c", 0);
47
48               memset (&dm->pcap_main, 0, sizeof (dm->pcap_main));
49               dm->pcap_main.file_name = (char *) dm->pcap_filename;
50               dm->pcap_main.n_packets_to_capture = 100;
51               if (dm->pcap_pkts_to_capture)
52                 dm->pcap_main.n_packets_to_capture = dm->pcap_pkts_to_capture;
53
54               dm->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
55               dm->tx_pcap_enable = 1;
56               matched = 1;
57               vlib_cli_output (vm, "pcap tx capture on...");
58             }
59           else
60             {
61               vlib_cli_output (vm, "pcap tx capture already on...");
62             }
63           matched = 1;
64         }
65       else if (unformat (input, "off"))
66         {
67           if (dm->tx_pcap_enable)
68             {
69               vlib_cli_output (vm, "captured %d pkts...",
70                                dm->pcap_main.n_packets_captured+1);
71               if (dm->pcap_main.n_packets_captured)
72                 {
73                   dm->pcap_main.n_packets_to_capture =
74                     dm->pcap_main.n_packets_captured;
75                   error = pcap_write (&dm->pcap_main);
76                   if (error)
77                     clib_error_report (error);
78                   else
79                     vlib_cli_output (vm, "saved to %s...", dm->pcap_filename);
80                 }
81             }
82           else
83             {
84               vlib_cli_output (vm, "pcap tx capture already off...");
85             }
86
87           dm->tx_pcap_enable = 0;
88           matched = 1;
89         }
90       else if (unformat (input, "max %d", &max))
91         {
92           dm->pcap_pkts_to_capture = max;
93           matched = 1;
94         }
95
96       else if (unformat (input, "intfc %U",
97                          unformat_vnet_sw_interface, dm->vnet_main,
98                          &dm->pcap_sw_if_index))
99         matched = 1;
100       else if (unformat (input, "intfc any"))
101         {
102           dm->pcap_sw_if_index = 0;
103           matched = 1;
104         }
105       else if (unformat (input, "file %s", &filename))
106         {
107           u8 * chroot_filename;
108           /* Brain-police user path input */
109           if (strstr((char *)filename, "..") || 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 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
151     .path = "pcap tx trace",
152     .short_help =
153     "pcap tx trace on off max <nn> intfc <intfc> file <name> status",
154     .function = pcap_trace_command_fn,
155 };
156
157
158 static clib_error_t *
159 show_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
160                  vlib_cli_command_t * cmd)
161 {
162   struct rte_mempool * rmp;
163   int i;
164
165   for(i = 0; i < vec_len(vm->buffer_main->pktmbuf_pools); i++)
166     {
167       rmp = vm->buffer_main->pktmbuf_pools[i];
168       if (rmp)
169         {
170           unsigned count = rte_mempool_count(rmp);
171           unsigned free_count = rte_mempool_free_count(rmp);
172
173           vlib_cli_output(vm, "name=\"%s\"  available = %7d allocated = %7d total = %7d\n",
174                           rmp->name, (u32)count, (u32)free_count,
175                           (u32)(count+free_count));
176         }
177       else
178         {
179            vlib_cli_output(vm, "rte_mempool is NULL (!)\n");
180         }
181     }
182   return 0;
183 }
184
185 VLIB_CLI_COMMAND (cmd_show_dpdk_bufferr,static) = {
186     .path = "show dpdk buffer",
187     .short_help = "show dpdk buffer state",
188     .function = show_dpdk_buffer,
189     .is_mp_safe = 1,
190 };
191
192 static clib_error_t *
193 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
194                  vlib_cli_command_t * cmd)
195 {
196   static u32 * allocated_buffers;
197   u32 n_alloc = 0;
198   u32 n_free = 0;
199   u32 first, actual_alloc;
200
201   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
202     {
203       if (unformat (input, "allocate %d", &n_alloc))
204         ;
205       else if (unformat (input, "free %d", &n_free))
206         ;
207       else
208         break;
209     }
210
211   if (n_free)
212     {
213       if (vec_len (allocated_buffers) < n_free)
214         return clib_error_return (0, "Can't free %d, only %d allocated",
215                                   n_free, vec_len (allocated_buffers));
216
217       first = vec_len(allocated_buffers) - n_free;
218       vlib_buffer_free (vm, allocated_buffers + first, n_free);
219       _vec_len (allocated_buffers) = first;
220     }
221   if (n_alloc)
222     {
223       first = vec_len (allocated_buffers);
224       vec_validate (allocated_buffers,
225                     vec_len (allocated_buffers) + n_alloc - 1);
226
227       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
228                                         n_alloc);
229       _vec_len (allocated_buffers) = first + actual_alloc;
230
231       if (actual_alloc < n_alloc)
232         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
233                          actual_alloc);
234     }
235
236   vlib_cli_output (vm, "Currently %d buffers allocated",
237                    vec_len (allocated_buffers));
238
239   if (allocated_buffers && vec_len(allocated_buffers) == 0)
240     vec_free(allocated_buffers);
241
242   return 0;
243 }
244
245 VLIB_CLI_COMMAND (cmd_test_dpdk_buffer,static) = {
246     .path = "test dpdk buffer",
247     .short_help = "test dpdk buffer [allocate <nn>][free <nn>]",
248     .function = test_dpdk_buffer,
249     .is_mp_safe = 1,
250 };
251
252 static void
253 show_dpdk_device_stats (vlib_main_t * vm, dpdk_device_t * xd)
254 {
255     vlib_cli_output(vm,
256                     "device_index %d\n"
257                     "  last_burst_sz           %d\n"
258                     "  max_burst_sz            %d\n"
259                     "  full_frames_cnt         %u\n"
260                     "  consec_full_frames_cnt  %u\n"
261                     "  congestion_cnt          %d\n"
262                     "  last_poll_time          %llu\n"
263                     "  max_poll_delay          %llu\n"
264                     "  discard_cnt             %u\n"
265                     "  total_packet_cnt        %u\n",
266                     xd->device_index,
267                     xd->efd_agent.last_burst_sz,
268                     xd->efd_agent.max_burst_sz,
269                     xd->efd_agent.full_frames_cnt,
270                     xd->efd_agent.consec_full_frames_cnt,
271                     xd->efd_agent.congestion_cnt,
272                     xd->efd_agent.last_poll_time,
273                     xd->efd_agent.max_poll_delay,
274                     xd->efd_agent.discard_cnt,
275                     xd->efd_agent.total_packet_cnt);
276
277     u32 device_queue_sz = rte_eth_rx_queue_count(xd->device_index,
278                                                  0 /* queue_id */);
279     vlib_cli_output(vm,
280                     "  device_queue_sz         %u\n",
281                     device_queue_sz);
282 }
283
284
285 /*
286  * Trigger threads to grab frame queue trace data
287  */
288 static clib_error_t *
289 trace_frame_queue (vlib_main_t *vm, unformat_input_t *input,
290                   vlib_cli_command_t *cmd)
291 {
292   clib_error_t * error = NULL;
293   frame_queue_trace_t *fqt;
294   frame_queue_nelt_counter_t *fqh;
295   u32 num_fq;
296   u32 fqix;
297   u32 enable = 0;
298
299   if (unformat(input, "on")) {
300     enable = 1;
301   } else if (unformat(input, "off")) {
302     enable = 0;
303   } else {
304       return clib_error_return(0, "expecting on or off");
305   }
306
307   num_fq = vec_len(vlib_frame_queues);
308   if (num_fq == 0)
309     {
310       vlib_cli_output(vm, "No frame queues exist\n");
311       return error;
312     }
313
314   // Allocate storage for trace if necessary
315   vec_validate_aligned(dpdk_main.frame_queue_traces, num_fq-1, CLIB_CACHE_LINE_BYTES);
316   vec_validate_aligned(dpdk_main.frame_queue_histogram, num_fq-1, CLIB_CACHE_LINE_BYTES);
317
318   for (fqix=0; fqix<num_fq; fqix++) {
319     fqt = &dpdk_main.frame_queue_traces[fqix];
320     fqh = &dpdk_main.frame_queue_histogram[fqix];
321
322     memset(fqt->n_vectors, 0xff, sizeof(fqt->n_vectors));
323     fqt->written = 0;
324     memset(fqh, 0, sizeof(*fqh));
325     vlib_frame_queues[fqix]->trace = enable;
326   }
327   return error;
328 }
329
330 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
331     .path = "trace frame-queue",
332     .short_help = "trace frame-queue (on|off)",
333     .function = trace_frame_queue,
334     .is_mp_safe = 1,
335 };
336
337
338 /*
339  * Adding two counters and compute percent of total
340  * Round up, e.g. 0.000001 => 1%
341  */
342 static u32
343 compute_percent (u64 *two_counters, u64 total)
344 {
345     if (total == 0)
346       {
347         return 0;
348       }
349     else
350       {
351         return (((two_counters[0] + two_counters[1]) * 100) + (total-1)) / total;
352       }
353 }
354
355 /*
356  * Display frame queue trace data gathered by threads.
357  */
358 static clib_error_t *
359 show_frame_queue_internal (vlib_main_t *vm,
360                            u32          histogram)
361 {
362   clib_error_t * error = NULL;
363   frame_queue_trace_t *fqt;
364   frame_queue_nelt_counter_t *fqh;
365   u32 num_fq;
366   u32 fqix;
367
368   num_fq = vec_len(dpdk_main.frame_queue_traces);
369   if (num_fq == 0)
370     {
371       vlib_cli_output(vm, "No trace data for frame queues\n");
372       return error;
373     }
374
375   if (histogram)
376     {
377       vlib_cli_output(vm, "0-1   2-3   4-5   6-7   8-9   10-11 12-13 14-15 "
378                           "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
379     }
380
381   for (fqix=0; fqix<num_fq; fqix++) {
382     fqt = &(dpdk_main.frame_queue_traces[fqix]);
383
384     vlib_cli_output(vm, "Thread %d %v\n", fqix, vlib_worker_threads[fqix].name);
385
386     if (fqt->written == 0)
387       {
388         vlib_cli_output(vm, "  no trace data\n");
389         continue;
390       }
391
392     if (histogram)
393       {
394         fqh = &(dpdk_main.frame_queue_histogram[fqix]);
395         u32 nelt;
396         u64 total = 0;
397
398         for (nelt=0; nelt<MAX_NELTS; nelt++) {
399             total += fqh->count[nelt];
400         }
401
402         /*
403          * Print in pairs to condense the output.
404          * Allow entries with 0 counts to be clearly identified, by rounding up.
405          * Any non-zero value will be displayed as at least one percent. This
406          * also means the sum of percentages can be > 100, but that is fine. The
407          * histogram is counted from the last time "trace frame on" was issued.
408          */
409         vlib_cli_output(vm,
410                         "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  "
411                         "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%\n",
412                         compute_percent(&fqh->count[ 0], total),
413                         compute_percent(&fqh->count[ 2], total),
414                         compute_percent(&fqh->count[ 4], total),
415                         compute_percent(&fqh->count[ 6], total),
416                         compute_percent(&fqh->count[ 8], total),
417                         compute_percent(&fqh->count[10], total),
418                         compute_percent(&fqh->count[12], total),
419                         compute_percent(&fqh->count[14], total),
420                         compute_percent(&fqh->count[16], total),
421                         compute_percent(&fqh->count[18], total),
422                         compute_percent(&fqh->count[20], total),
423                         compute_percent(&fqh->count[22], total),
424                         compute_percent(&fqh->count[24], total),
425                         compute_percent(&fqh->count[26], total),
426                         compute_percent(&fqh->count[28], total),
427                         compute_percent(&fqh->count[30], total));
428       }
429     else
430       {
431         vlib_cli_output(vm, "  vector-threshold %d  ring size %d  in use %d\n",
432                         fqt->threshold, fqt->nelts, fqt->n_in_use);
433         vlib_cli_output(vm, "  head %12d  head_hint %12d  tail %12d\n",
434                         fqt->head, fqt->head_hint, fqt->tail);
435         vlib_cli_output(vm, "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
436                         fqt->n_vectors[0], fqt->n_vectors[1], fqt->n_vectors[2], fqt->n_vectors[3],
437                         fqt->n_vectors[4], fqt->n_vectors[5], fqt->n_vectors[6], fqt->n_vectors[7],
438                         fqt->n_vectors[8], fqt->n_vectors[9], fqt->n_vectors[10], fqt->n_vectors[11],
439                         fqt->n_vectors[12], fqt->n_vectors[13], fqt->n_vectors[14], fqt->n_vectors[15]);
440
441         if (fqt->nelts > 16)
442           {
443             vlib_cli_output(vm, "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
444                             fqt->n_vectors[16], fqt->n_vectors[17], fqt->n_vectors[18], fqt->n_vectors[19],
445                             fqt->n_vectors[20], fqt->n_vectors[21], fqt->n_vectors[22], fqt->n_vectors[23],
446                             fqt->n_vectors[24], fqt->n_vectors[25], fqt->n_vectors[26], fqt->n_vectors[27],
447                             fqt->n_vectors[28], fqt->n_vectors[29], fqt->n_vectors[30], fqt->n_vectors[31]);
448           }
449       }
450
451    }
452   return error;
453 }
454
455 static clib_error_t *
456 show_frame_queue_trace (vlib_main_t *vm, unformat_input_t *input,
457                         vlib_cli_command_t *cmd)
458 {
459   return show_frame_queue_internal (vm, 0);
460 }
461
462 static clib_error_t *
463 show_frame_queue_histogram (vlib_main_t *vm, unformat_input_t *input,
464                             vlib_cli_command_t *cmd)
465 {
466   return show_frame_queue_internal (vm, 1);
467 }
468
469 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
470     .path = "show frame-queue",
471     .short_help = "show frame-queue trace",
472     .function = show_frame_queue_trace,
473 };
474
475 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
476     .path = "show frame-queue histogram",
477     .short_help = "show frame-queue histogram",
478     .function = show_frame_queue_histogram,
479 };
480
481
482 /*
483  * Modify the number of elements on the frame_queues
484  */
485 static clib_error_t *
486 test_frame_queue_nelts (vlib_main_t *vm, unformat_input_t *input,
487                         vlib_cli_command_t *cmd)
488 {
489   clib_error_t * error = NULL;
490   u32 num_fq;
491   u32 fqix;
492   u32 nelts = 0;
493
494   unformat(input, "%d", &nelts);
495   if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)) {
496       return clib_error_return(0, "expecting 4,8,16,32");
497   }
498
499   num_fq = vec_len(vlib_frame_queues);
500   if (num_fq == 0)
501     {
502       vlib_cli_output(vm, "No frame queues exist\n");
503       return error;
504     }
505
506   for (fqix=0; fqix<num_fq; fqix++) {
507     vlib_frame_queues[fqix]->nelts = nelts;
508   } 
509
510   return error;
511 }
512
513 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
514     .path = "test frame-queue nelts",
515     .short_help = "test frame-queue nelts (4,8,16,32)",
516     .function = test_frame_queue_nelts,
517 };
518
519
520 /*
521  * Modify the max number of packets pulled off the frame queues
522  */
523 static clib_error_t *
524 test_frame_queue_threshold (vlib_main_t *vm, unformat_input_t *input,
525                             vlib_cli_command_t *cmd)
526 {
527   clib_error_t * error = NULL;
528   u32 num_fq;
529   u32 fqix;
530   u32 threshold = 0;
531
532   if (unformat(input, "%d", &threshold)) {
533   } else {
534       vlib_cli_output(vm, "expecting threshold value\n");
535       return error;
536   }
537
538   if (threshold == 0)
539     threshold = ~0;
540
541   num_fq = vec_len(vlib_frame_queues);
542   if (num_fq == 0)
543     {
544       vlib_cli_output(vm, "No frame queues exist\n");
545       return error;
546     }
547
548   for (fqix=0; fqix<num_fq; fqix++) {
549     vlib_frame_queues[fqix]->vector_threshold = threshold;
550   } 
551
552   return error;
553 }
554
555 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
556     .path = "test frame-queue threshold",
557     .short_help = "test frame-queue threshold N (0=no limit)",
558     .function = test_frame_queue_threshold,
559 };
560
561 static void
562 show_efd_config (vlib_main_t * vm)
563 {
564     vlib_thread_main_t * tm = vlib_get_thread_main();
565     dpdk_main_t * dm = &dpdk_main;
566
567     vlib_cli_output(vm,
568                     "dpdk:   (0x%04x) enabled:%d monitor:%d drop_all:%d\n"
569                     "  dpdk_queue_hi_thresh          %d\n"
570                     "  consec_full_frames_hi_thresh  %d\n"
571                     "---------\n"
572                     "worker: (0x%04x) enabled:%d monitor:%d\n"
573                     "  worker_queue_hi_thresh        %d\n",
574                     dm->efd.enabled,
575                     ((dm->efd.enabled & DPDK_EFD_DISCARD_ENABLED) ? 1:0),
576                     ((dm->efd.enabled & DPDK_EFD_MONITOR_ENABLED) ? 1:0),
577                     ((dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED) ? 1:0),
578                     dm->efd.queue_hi_thresh,
579                     dm->efd.consec_full_frames_hi_thresh,
580                     tm->efd.enabled,
581                     ((tm->efd.enabled & VLIB_EFD_DISCARD_ENABLED) ? 1:0),
582                     ((dm->efd.enabled & VLIB_EFD_MONITOR_ENABLED) ? 1:0),
583                     tm->efd.queue_hi_thresh);
584     vlib_cli_output(vm,
585                     "---------\n"
586                     "ip_prec_bitmap   0x%02x\n"
587                     "mpls_exp_bitmap  0x%02x\n"
588                     "vlan_cos_bitmap  0x%02x\n",
589                     tm->efd.ip_prec_bitmap,
590                     tm->efd.mpls_exp_bitmap,
591                     tm->efd.vlan_cos_bitmap);
592 }
593
594 static clib_error_t *
595 show_efd (vlib_main_t * vm,
596           unformat_input_t * input,
597           vlib_cli_command_t * cmd)
598 {
599
600     if (unformat(input, "config")) {
601         show_efd_config(vm);
602     } else if (unformat(input, "dpdk")) {
603         dpdk_main_t * dm = &dpdk_main;
604         dpdk_device_t * xd;
605         u32 device_id = ~0;
606
607         unformat(input, "device %d", &device_id);
608         vec_foreach (xd, dm->devices) {
609             if ((xd->device_index == device_id) || (device_id == ~0)) {
610                 show_dpdk_device_stats(vm, xd);
611             }
612         }
613     } else if (unformat(input, "worker")) {
614         vlib_thread_main_t * tm = vlib_get_thread_main();
615         vlib_frame_queue_t *fq;
616         vlib_thread_registration_t * tr;
617         int thread_id;
618         u32 num_workers = 0;
619         u32 first_worker_index = 0;
620         uword * p;
621
622         p = hash_get_mem (tm->thread_registrations_by_name, "workers");
623         ASSERT (p);
624         tr = (vlib_thread_registration_t *) p[0];
625         if (tr)
626           {
627             num_workers = tr->count;
628             first_worker_index = tr->first_index;
629           }
630
631         vlib_cli_output(vm,
632                         "num_workers               %d\n"
633                         "first_worker_index        %d\n"
634                         "vlib_frame_queues[%d]:\n",
635                         num_workers,
636                         first_worker_index,
637                         tm->n_vlib_mains);
638
639         for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++) {
640             fq = vlib_frame_queues[thread_id];
641             if (fq) {
642                 vlib_cli_output(vm,
643                                 "%2d: frames_queued         %u\n"
644                                 "    frames_queued_hint    %u\n"
645                                 "    enqueue_full_events   %u\n"
646                                 "    enqueue_efd_discards  %u\n",
647                                 thread_id,
648                                 (fq->tail - fq->head),
649                                 (fq->tail - fq->head_hint),
650                                 fq->enqueue_full_events,
651                                 fq->enqueue_efd_discards);
652             }
653         }
654     } else if (unformat(input, "help")) {
655         vlib_cli_output(vm, "Usage: show efd config | "
656                         "dpdk [device <id>] | worker\n");
657     } else {
658         show_efd_config(vm);
659     }
660
661     return 0;
662 }
663
664 VLIB_CLI_COMMAND (show_efd_command, static) = {
665   .path = "show efd",
666   .short_help = "Show efd [device <id>] | [config]",
667   .function = show_efd,
668 };
669
670 static clib_error_t *
671 clear_efd (vlib_main_t * vm,
672            unformat_input_t * input,
673            vlib_cli_command_t * cmd)
674 {
675     dpdk_main_t * dm = &dpdk_main;
676     dpdk_device_t * xd;
677     vlib_thread_main_t * tm = vlib_get_thread_main();
678     vlib_frame_queue_t *fq;
679     int thread_id;
680
681     vec_foreach (xd, dm->devices) {
682         xd->efd_agent.last_burst_sz = 0;
683         xd->efd_agent.max_burst_sz = 0;
684         xd->efd_agent.full_frames_cnt = 0;
685         xd->efd_agent.consec_full_frames_cnt = 0;
686         xd->efd_agent.congestion_cnt = 0;
687         xd->efd_agent.last_poll_time = 0;
688         xd->efd_agent.max_poll_delay = 0;
689         xd->efd_agent.discard_cnt = 0;
690         xd->efd_agent.total_packet_cnt = 0;
691     }
692
693     for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++) {
694         fq = vlib_frame_queues[thread_id];
695         if (fq) {
696             fq->enqueue_full_events = 0;
697             fq->enqueue_efd_discards = 0;
698         }
699     }
700
701     return 0;
702 }
703
704 VLIB_CLI_COMMAND (clear_efd_command,static) = {
705   .path = "clear efd",
706   .short_help = "Clear early-fast-discard counters",
707   .function = clear_efd,
708 };
709
710 static clib_error_t *
711 parse_op_and_prec (vlib_main_t *vm, unformat_input_t *input,
712                    vlib_cli_command_t *cmd,
713                    char *prec_type, u8 *prec_bitmap)
714 {
715     clib_error_t * error = NULL;
716     u8 op = 0;
717     u8 prec = 0;
718
719     if (unformat(input, "ge")) {
720         op = EFD_OPERATION_GREATER_OR_EQUAL;
721     } else if (unformat(input, "lt")) {
722         op = EFD_OPERATION_LESS_THAN;
723     } else if (unformat(input, "help")) {
724         vlib_cli_output(vm,
725             "enter operation [ge | lt] and precedence <0-7>)");
726         return (error);
727     } else {
728         return clib_error_return(0, "unknown input `%U'",
729                                   format_unformat_error, input);
730     }
731
732     if (unformat (input, "%u", &prec)) {
733          if (prec > 7) {
734              return clib_error_return(0, "precedence %d is out of range <0-7>",
735                                       prec);
736          }
737     } else {
738         return clib_error_return(0, "unknown input `%U'",
739                                  format_unformat_error, input);
740     }
741
742     set_efd_bitmap(prec_bitmap, prec, op);
743
744     vlib_cli_output(vm,
745         "EFD will be set for %s precedence %s%u%s.",
746         prec_type,
747         (op == EFD_OPERATION_LESS_THAN) ? "less than " : "",
748         prec,
749         (op == EFD_OPERATION_GREATER_OR_EQUAL) ? " and greater" : "");
750
751     return (error);
752 }
753
754
755 static clib_error_t *
756 set_efd (vlib_main_t *vm, unformat_input_t *input,
757           vlib_cli_command_t *cmd)
758 {
759     dpdk_main_t * dm = &dpdk_main;
760     vlib_thread_main_t * tm = vlib_get_thread_main();
761     clib_error_t * error = NULL;
762     vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
763
764     if (unformat(input, "enable")) {
765         if (unformat(input, "dpdk")) {
766             dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
767         } else if (unformat(input, "worker")) {
768             tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
769         } else if (unformat(input, "monitor")) {
770             dm->efd.enabled |= DPDK_EFD_MONITOR_ENABLED;
771             tm->efd.enabled |= VLIB_EFD_MONITOR_ENABLED;
772         } else if (unformat(input, "drop_all")) {
773             dm->efd.enabled |= DPDK_EFD_DROPALL_ENABLED;
774         } else if (unformat(input, "default")) {
775             dm->efd.enabled = (DPDK_EFD_DISCARD_ENABLED |
776                                DPDK_EFD_MONITOR_ENABLED);
777             tm->efd.enabled = (VLIB_EFD_DISCARD_ENABLED |
778                                VLIB_EFD_MONITOR_ENABLED);
779         } else {
780             return clib_error_return(0, "Usage: set efd enable [dpdk | "
781                                      "worker | monitor | drop_all | default]");
782         }
783     } else if (unformat(input, "disable")) {
784         if (unformat(input, "dpdk")) {
785             dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
786         } else if (unformat(input, "worker")) {
787             tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
788         } else if (unformat(input, "monitor")) {
789             dm->efd.enabled &= ~DPDK_EFD_MONITOR_ENABLED;
790             tm->efd.enabled &= ~VLIB_EFD_MONITOR_ENABLED;
791         } else if (unformat(input, "drop_all")) {
792             dm->efd.enabled &= ~DPDK_EFD_DROPALL_ENABLED;
793         } else  if (unformat(input, "all")) {
794             dm->efd.enabled = 0;
795             tm->efd.enabled = 0;
796         } else {
797             return clib_error_return(0, "Usage: set efd disable [dpdk | "
798                                      "worker | monitor | drop_all | all]");
799         }
800     } else if (unformat(input, "worker_queue_hi_thresh")) {
801         u32 mark;
802         if (unformat (input, "%u", &mark)) {
803             tm->efd.queue_hi_thresh = mark;
804         } else {
805             return clib_error_return(0, "unknown input `%U'",
806                                      format_unformat_error, input);
807         }
808     } else if (unformat(input, "dpdk_device_hi_thresh")) {
809         u32 thresh;
810         if (unformat (input, "%u", &thresh)) {
811             dm->efd.queue_hi_thresh = thresh;
812         } else {
813             return clib_error_return(0, "unknown input `%U'",
814                                      format_unformat_error, input);
815         }
816     } else if (unformat(input, "consec_full_frames_hi_thresh")) {
817         u32 thresh;
818         if (unformat (input, "%u", &thresh)) {
819             dm->efd.consec_full_frames_hi_thresh = thresh;
820         } else {
821             return clib_error_return(0, "unknown input `%U'",
822                                      format_unformat_error, input);
823         }
824     } else if (unformat(input, "ip-prec")) {
825         return (parse_op_and_prec(vm, input, cmd,
826                                  "ip", &tm->efd.ip_prec_bitmap));
827     } else if (unformat(input, "mpls-exp")) {
828         return (parse_op_and_prec(vm, input, cmd,
829                                  "mpls", &tm->efd.mpls_exp_bitmap));
830     } else if (unformat(input, "vlan-cos")) {
831         return (parse_op_and_prec(vm, input, cmd,
832                                  "vlan", &tm->efd.vlan_cos_bitmap));
833     } else if (unformat(input, "help")) {
834         vlib_cli_output(vm,
835             "Usage:\n"
836             "  set efd enable <dpdk | worker | monitor | drop_all | default> |\n"
837             "  set efd disable <dpdk | worker | monitor | drop_all | all> |\n"
838             "  set efd <ip-prec | mpls-exp | vlan-cos> <ge | lt> <0-7>\n"
839             "  set efd worker_queue_hi_thresh <0-32> |\n"
840             "  set efd dpdk_device_hi_thresh <0-%d> |\n"
841             "  set efd consec_full_frames_hi_thresh <count> |\n",
842             DPDK_NB_RX_DESC_10GE);
843     } else {
844         return clib_error_return(0, "unknown input `%U'",
845                                   format_unformat_error, input);
846     }
847
848     if (dm->efd.enabled)
849         rt->function = dpdk_input_efd_multiarch_select();
850     else if (dm->use_rss)
851         rt->function = dpdk_input_rss_multiarch_select();
852     else
853         rt->function = dpdk_input_multiarch_select();
854
855     return error;
856 }
857
858 VLIB_CLI_COMMAND (cmd_set_efd,static) = {
859     .path = "set efd",
860     .short_help = "set early-fast-discard commands",
861     .function = set_efd,
862 };
863
864 static clib_error_t *
865 set_dpdk_if_desc (vlib_main_t *vm, unformat_input_t *input,
866           vlib_cli_command_t *cmd)
867 {
868   unformat_input_t _line_input, * line_input = &_line_input;
869   dpdk_main_t * dm = &dpdk_main;
870   vnet_hw_interface_t * hw;
871   dpdk_device_t * xd;
872   u32 hw_if_index = (u32) ~0;
873   u32 nb_rx_desc = (u32) ~0;
874   u32 nb_tx_desc = (u32) ~0;
875   clib_error_t * rv;
876
877   if (! unformat_user (input, unformat_line_input, line_input))
878     return 0;
879
880   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
881     if (unformat (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
882                   &hw_if_index))
883       ;
884     else if (unformat (line_input, "tx %d", &nb_tx_desc))
885       ;
886     else if (unformat (line_input, "rx %d", &nb_rx_desc))
887       ;
888     else
889       return clib_error_return (0, "parse error: '%U'",
890                                 format_unformat_error, line_input);
891   }
892
893   unformat_free (line_input);
894
895   if (hw_if_index == (u32) ~0)
896     return clib_error_return (0, "please specify valid interface name");
897
898   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
899   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
900
901   if (xd->dev_type != VNET_DPDK_DEV_ETH)
902     return clib_error_return (0, "number of descriptors can be set only for "
903                               "physical devices");
904
905   if ((nb_rx_desc == (u32) ~0 || nb_rx_desc == xd->nb_rx_desc) &&
906       (nb_tx_desc == (u32) ~0 || nb_tx_desc == xd->nb_tx_desc))
907     return clib_error_return (0, "nothing changed");
908
909   if (nb_rx_desc != (u32) ~0)
910         xd->nb_rx_desc = nb_rx_desc;
911
912   if (nb_tx_desc != (u32) ~0)
913         xd->nb_rx_desc = nb_rx_desc;
914
915   rv = dpdk_port_setup(dm, xd);
916
917   return rv < 0 ? rv : 0;
918 }
919
920 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
921     .path = "set dpdk interface descriptors",
922     .short_help = "set dpdk interface descriptors <if-name> [rx <n>] [tx <n>]",
923     .function = set_dpdk_if_desc,
924 };
925
926 static clib_error_t *
927 show_dpdk_if_placement (vlib_main_t *vm, unformat_input_t *input,
928           vlib_cli_command_t *cmd)
929 {
930   vlib_thread_main_t * tm = vlib_get_thread_main();
931   dpdk_main_t * dm = &dpdk_main;
932   dpdk_device_and_queue_t * dq;
933   int cpu;
934
935   if (tm->n_vlib_mains == 1)
936     vlib_cli_output(vm, "All interfaces are handled by main thread");
937
938   for(cpu = 0; cpu < vec_len(dm->devices_by_cpu); cpu++)
939     {
940       if (vec_len(dm->devices_by_cpu[cpu]))
941         vlib_cli_output(vm, "Thread %u (%s at lcore %u):", cpu,
942                         vlib_worker_threads[cpu].name,
943                         vlib_worker_threads[cpu].dpdk_lcore_id);
944
945       vec_foreach(dq, dm->devices_by_cpu[cpu])
946         {
947           u32 hw_if_index = dm->devices[dq->device].vlib_hw_if_index;
948           vnet_hw_interface_t * hi =  vnet_get_hw_interface(dm->vnet_main, hw_if_index);
949           vlib_cli_output(vm, "  %v queue %u", hi->name, dq->queue_id);
950         }
951     }
952   return 0;
953 }
954
955 VLIB_CLI_COMMAND (cmd_show_dpdk_if_placement,static) = {
956     .path = "show dpdk interface placement",
957     .short_help = "show dpdk interface placement",
958     .function = show_dpdk_if_placement,
959 };
960
961 static int
962 dpdk_device_queue_sort(void * a1, void * a2)
963 {
964   dpdk_device_and_queue_t * dq1 = a1;
965   dpdk_device_and_queue_t * dq2 = a2;
966
967   if (dq1->device > dq2->device)
968     return 1;
969   else if (dq1->device < dq2->device)
970     return -1;
971   else if (dq1->queue_id > dq2->queue_id)
972     return 1;
973   else if (dq1->queue_id < dq2->queue_id)
974     return -1;
975   else
976     return 0;
977 }
978
979 static clib_error_t *
980 set_dpdk_if_placement (vlib_main_t *vm, unformat_input_t *input,
981           vlib_cli_command_t *cmd)
982 {
983   unformat_input_t _line_input, * line_input = &_line_input;
984   dpdk_main_t * dm = &dpdk_main;
985   dpdk_device_and_queue_t * dq;
986   vnet_hw_interface_t * hw;
987   dpdk_device_t * xd;
988   u32 hw_if_index = (u32) ~0;
989   u32 queue = (u32) 0;
990   u32 cpu = (u32) ~0;
991   int i;
992
993   if (! unformat_user (input, unformat_line_input, line_input))
994     return 0;
995
996   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
997     if (unformat (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
998                   &hw_if_index))
999       ;
1000     else if (unformat (line_input, "queue %d", &queue))
1001       ;
1002     else if (unformat (line_input, "thread %d", &cpu))
1003       ;
1004     else
1005       return clib_error_return (0, "parse error: '%U'",
1006                                 format_unformat_error, line_input);
1007   }
1008
1009   unformat_free (line_input);
1010
1011   if (hw_if_index == (u32) ~0)
1012     return clib_error_return (0, "please specify valid interface name");
1013
1014   if (cpu < dm->input_cpu_first_index ||
1015       cpu >= (dm->input_cpu_first_index + dm->input_cpu_count))
1016     return clib_error_return (0, "please specify valid thread id");
1017
1018   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
1019   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1020
1021   for(i = 0; i < vec_len(dm->devices_by_cpu); i++)
1022     {
1023       vec_foreach(dq, dm->devices_by_cpu[i])
1024         {
1025           if (hw_if_index == dm->devices[dq->device].vlib_hw_if_index &&
1026               queue == dq->queue_id)
1027             {
1028               if (cpu == i) /* nothing to do */
1029                 return 0;
1030
1031               vec_del1(dm->devices_by_cpu[i], dq - dm->devices_by_cpu[i]);
1032               vec_add2(dm->devices_by_cpu[cpu], dq, 1);
1033               dq->queue_id = queue;
1034               dq->device = xd->device_index;
1035               xd->cpu_socket_id_by_queue[queue] =
1036                 rte_lcore_to_socket_id(vlib_worker_threads[cpu].dpdk_lcore_id);
1037
1038               vec_sort_with_function(dm->devices_by_cpu[i],
1039                                      dpdk_device_queue_sort);
1040
1041               vec_sort_with_function(dm->devices_by_cpu[cpu],
1042                                      dpdk_device_queue_sort);
1043
1044               if (vec_len(dm->devices_by_cpu[i]) == 0)
1045                 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1046                                      VLIB_NODE_STATE_DISABLED);
1047
1048               if (vec_len(dm->devices_by_cpu[cpu]) == 1)
1049                 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
1050                                      VLIB_NODE_STATE_POLLING);
1051
1052               return 0;
1053             }
1054         }
1055     }
1056
1057   return clib_error_return (0, "not found");
1058 }
1059
1060 VLIB_CLI_COMMAND (cmd_set_dpdk_if_placement,static) = {
1061     .path = "set dpdk interface placement",
1062     .short_help = "set dpdk interface placement <if-name> [queue <n>]  thread <n>",
1063     .function = set_dpdk_if_placement,
1064 };
1065
1066 clib_error_t *
1067 dpdk_cli_init (vlib_main_t * vm)
1068 {
1069   return 0;
1070 }
1071
1072 VLIB_INIT_FUNCTION (dpdk_cli_init);