VPP-189 More coverity bug fixes
[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 #if RTE_VERSION >= RTE_VERSION_NUM(16, 7, 0, 0)
171           unsigned count = rte_mempool_avail_count(rmp);
172           unsigned free_count = rte_mempool_in_use_count(rmp);
173 #else
174           unsigned count = rte_mempool_count(rmp);
175           unsigned free_count = rte_mempool_free_count(rmp);
176 #endif
177
178           vlib_cli_output(vm, "name=\"%s\"  available = %7d allocated = %7d total = %7d\n",
179                           rmp->name, (u32)count, (u32)free_count,
180                           (u32)(count+free_count));
181         }
182       else
183         {
184            vlib_cli_output(vm, "rte_mempool is NULL (!)\n");
185         }
186     }
187   return 0;
188 }
189
190 VLIB_CLI_COMMAND (cmd_show_dpdk_bufferr,static) = {
191     .path = "show dpdk buffer",
192     .short_help = "show dpdk buffer state",
193     .function = show_dpdk_buffer,
194     .is_mp_safe = 1,
195 };
196
197 static clib_error_t *
198 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
199                  vlib_cli_command_t * cmd)
200 {
201   static u32 * allocated_buffers;
202   u32 n_alloc = 0;
203   u32 n_free = 0;
204   u32 first, actual_alloc;
205
206   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
207     {
208       if (unformat (input, "allocate %d", &n_alloc))
209         ;
210       else if (unformat (input, "free %d", &n_free))
211         ;
212       else
213         break;
214     }
215
216   if (n_free)
217     {
218       if (vec_len (allocated_buffers) < n_free)
219         return clib_error_return (0, "Can't free %d, only %d allocated",
220                                   n_free, vec_len (allocated_buffers));
221
222       first = vec_len(allocated_buffers) - n_free;
223       vlib_buffer_free (vm, allocated_buffers + first, n_free);
224       _vec_len (allocated_buffers) = first;
225     }
226   if (n_alloc)
227     {
228       first = vec_len (allocated_buffers);
229       vec_validate (allocated_buffers,
230                     vec_len (allocated_buffers) + n_alloc - 1);
231
232       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
233                                         n_alloc);
234       _vec_len (allocated_buffers) = first + actual_alloc;
235
236       if (actual_alloc < n_alloc)
237         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
238                          actual_alloc);
239     }
240
241   vlib_cli_output (vm, "Currently %d buffers allocated",
242                    vec_len (allocated_buffers));
243
244   if (allocated_buffers && vec_len(allocated_buffers) == 0)
245     vec_free(allocated_buffers);
246
247   return 0;
248 }
249
250 VLIB_CLI_COMMAND (cmd_test_dpdk_buffer,static) = {
251     .path = "test dpdk buffer",
252     .short_help = "test dpdk buffer [allocate <nn>][free <nn>]",
253     .function = test_dpdk_buffer,
254     .is_mp_safe = 1,
255 };
256
257 static void
258 show_dpdk_device_stats (vlib_main_t * vm, dpdk_device_t * xd)
259 {
260     vlib_cli_output(vm,
261                     "device_index %d\n"
262                     "  last_burst_sz           %d\n"
263                     "  max_burst_sz            %d\n"
264                     "  full_frames_cnt         %u\n"
265                     "  consec_full_frames_cnt  %u\n"
266                     "  congestion_cnt          %d\n"
267                     "  last_poll_time          %llu\n"
268                     "  max_poll_delay          %llu\n"
269                     "  discard_cnt             %u\n"
270                     "  total_packet_cnt        %u\n",
271                     xd->device_index,
272                     xd->efd_agent.last_burst_sz,
273                     xd->efd_agent.max_burst_sz,
274                     xd->efd_agent.full_frames_cnt,
275                     xd->efd_agent.consec_full_frames_cnt,
276                     xd->efd_agent.congestion_cnt,
277                     xd->efd_agent.last_poll_time,
278                     xd->efd_agent.max_poll_delay,
279                     xd->efd_agent.discard_cnt,
280                     xd->efd_agent.total_packet_cnt);
281
282     u32 device_queue_sz = rte_eth_rx_queue_count(xd->device_index,
283                                                  0 /* queue_id */);
284     vlib_cli_output(vm,
285                     "  device_queue_sz         %u\n",
286                     device_queue_sz);
287 }
288
289 static void
290 show_efd_config (vlib_main_t * vm)
291 {
292     vlib_thread_main_t * tm = vlib_get_thread_main();
293     dpdk_main_t * dm = &dpdk_main;
294
295     vlib_cli_output(vm,
296                     "dpdk:   (0x%04x) enabled:%d monitor:%d drop_all:%d\n"
297                     "  dpdk_queue_hi_thresh          %d\n"
298                     "  consec_full_frames_hi_thresh  %d\n"
299                     "---------\n"
300                     "worker: (0x%04x) enabled:%d monitor:%d\n"
301                     "  worker_queue_hi_thresh        %d\n",
302                     dm->efd.enabled,
303                     ((dm->efd.enabled & DPDK_EFD_DISCARD_ENABLED) ? 1:0),
304                     ((dm->efd.enabled & DPDK_EFD_MONITOR_ENABLED) ? 1:0),
305                     ((dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED) ? 1:0),
306                     dm->efd.queue_hi_thresh,
307                     dm->efd.consec_full_frames_hi_thresh,
308                     tm->efd.enabled,
309                     ((tm->efd.enabled & VLIB_EFD_DISCARD_ENABLED) ? 1:0),
310                     ((dm->efd.enabled & VLIB_EFD_MONITOR_ENABLED) ? 1:0),
311                     tm->efd.queue_hi_thresh);
312     vlib_cli_output(vm,
313                     "---------\n"
314                     "ip_prec_bitmap   0x%02x\n"
315                     "mpls_exp_bitmap  0x%02x\n"
316                     "vlan_cos_bitmap  0x%02x\n",
317                     tm->efd.ip_prec_bitmap,
318                     tm->efd.mpls_exp_bitmap,
319                     tm->efd.vlan_cos_bitmap);
320 }
321
322 static clib_error_t *
323 show_efd (vlib_main_t * vm,
324           unformat_input_t * input,
325           vlib_cli_command_t * cmd)
326 {
327
328     if (unformat(input, "config")) {
329         show_efd_config(vm);
330     } else if (unformat(input, "dpdk")) {
331         dpdk_main_t * dm = &dpdk_main;
332         dpdk_device_t * xd;
333         u32 device_id = ~0;
334
335         unformat(input, "device %d", &device_id);
336         vec_foreach (xd, dm->devices) {
337             if ((xd->device_index == device_id) || (device_id == ~0)) {
338                 show_dpdk_device_stats(vm, xd);
339             }
340         }
341     } else if (unformat(input, "worker")) {
342         vlib_thread_main_t * tm = vlib_get_thread_main();
343         vlib_frame_queue_t *fq;
344         vlib_thread_registration_t * tr;
345         int thread_id;
346         u32 num_workers = 0;
347         u32 first_worker_index = 0;
348         uword * p;
349
350         p = hash_get_mem (tm->thread_registrations_by_name, "workers");
351         ASSERT (p);
352         tr = (vlib_thread_registration_t *) p[0];
353         if (tr)
354           {
355             num_workers = tr->count;
356             first_worker_index = tr->first_index;
357           }
358
359         vlib_cli_output(vm,
360                         "num_workers               %d\n"
361                         "first_worker_index        %d\n"
362                         "vlib_frame_queues[%d]:\n",
363                         num_workers,
364                         first_worker_index,
365                         tm->n_vlib_mains);
366
367         for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++) {
368             fq = vlib_frame_queues[thread_id];
369             if (fq) {
370                 vlib_cli_output(vm,
371                                 "%2d: frames_queued         %u\n"
372                                 "    frames_queued_hint    %u\n"
373                                 "    enqueue_full_events   %u\n"
374                                 "    enqueue_efd_discards  %u\n",
375                                 thread_id,
376                                 (fq->tail - fq->head),
377                                 (fq->tail - fq->head_hint),
378                                 fq->enqueue_full_events,
379                                 fq->enqueue_efd_discards);
380             }
381         }
382     } else if (unformat(input, "help")) {
383         vlib_cli_output(vm, "Usage: show efd config | "
384                         "dpdk [device <id>] | worker\n");
385     } else {
386         show_efd_config(vm);
387     }
388
389     return 0;
390 }
391
392 VLIB_CLI_COMMAND (show_efd_command, static) = {
393   .path = "show efd",
394   .short_help = "Show efd [device <id>] | [config]",
395   .function = show_efd,
396 };
397
398 static clib_error_t *
399 clear_efd (vlib_main_t * vm,
400            unformat_input_t * input,
401            vlib_cli_command_t * cmd)
402 {
403     dpdk_main_t * dm = &dpdk_main;
404     dpdk_device_t * xd;
405     vlib_thread_main_t * tm = vlib_get_thread_main();
406     vlib_frame_queue_t *fq;
407     int thread_id;
408
409     vec_foreach (xd, dm->devices) {
410         xd->efd_agent.last_burst_sz = 0;
411         xd->efd_agent.max_burst_sz = 0;
412         xd->efd_agent.full_frames_cnt = 0;
413         xd->efd_agent.consec_full_frames_cnt = 0;
414         xd->efd_agent.congestion_cnt = 0;
415         xd->efd_agent.last_poll_time = 0;
416         xd->efd_agent.max_poll_delay = 0;
417         xd->efd_agent.discard_cnt = 0;
418         xd->efd_agent.total_packet_cnt = 0;
419     }
420
421     for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++) {
422         fq = vlib_frame_queues[thread_id];
423         if (fq) {
424             fq->enqueue_full_events = 0;
425             fq->enqueue_efd_discards = 0;
426         }
427     }
428
429     return 0;
430 }
431
432 VLIB_CLI_COMMAND (clear_efd_command,static) = {
433   .path = "clear efd",
434   .short_help = "Clear early-fast-discard counters",
435   .function = clear_efd,
436 };
437
438 static clib_error_t *
439 parse_op_and_prec (vlib_main_t *vm, unformat_input_t *input,
440                    vlib_cli_command_t *cmd,
441                    char *prec_type, u8 *prec_bitmap)
442 {
443     clib_error_t * error = NULL;
444     u8 op = 0;
445     u8 prec = 0;
446
447     if (unformat(input, "ge")) {
448         op = EFD_OPERATION_GREATER_OR_EQUAL;
449     } else if (unformat(input, "lt")) {
450         op = EFD_OPERATION_LESS_THAN;
451     } else if (unformat(input, "help")) {
452         vlib_cli_output(vm,
453             "enter operation [ge | lt] and precedence <0-7>)");
454         return (error);
455     } else {
456         return clib_error_return(0, "unknown input `%U'",
457                                   format_unformat_error, input);
458     }
459
460     if (unformat (input, "%u", &prec)) {
461          if (prec > 7) {
462              return clib_error_return(0, "precedence %d is out of range <0-7>",
463                                       prec);
464          }
465     } else {
466         return clib_error_return(0, "unknown input `%U'",
467                                  format_unformat_error, input);
468     }
469
470     set_efd_bitmap(prec_bitmap, prec, op);
471
472     vlib_cli_output(vm,
473         "EFD will be set for %s precedence %s%u%s.",
474         prec_type,
475         (op == EFD_OPERATION_LESS_THAN) ? "less than " : "",
476         prec,
477         (op == EFD_OPERATION_GREATER_OR_EQUAL) ? " and greater" : "");
478
479     return (error);
480 }
481
482
483 static clib_error_t *
484 set_efd (vlib_main_t *vm, unformat_input_t *input,
485           vlib_cli_command_t *cmd)
486 {
487     dpdk_main_t * dm = &dpdk_main;
488     vlib_thread_main_t * tm = vlib_get_thread_main();
489     clib_error_t * error = NULL;
490     vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
491
492     if (unformat(input, "enable")) {
493         if (unformat(input, "dpdk")) {
494             dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
495         } else if (unformat(input, "worker")) {
496             tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
497         } else if (unformat(input, "monitor")) {
498             dm->efd.enabled |= DPDK_EFD_MONITOR_ENABLED;
499             tm->efd.enabled |= VLIB_EFD_MONITOR_ENABLED;
500         } else if (unformat(input, "drop_all")) {
501             dm->efd.enabled |= DPDK_EFD_DROPALL_ENABLED;
502         } else if (unformat(input, "default")) {
503             dm->efd.enabled = (DPDK_EFD_DISCARD_ENABLED |
504                                DPDK_EFD_MONITOR_ENABLED);
505             tm->efd.enabled = (VLIB_EFD_DISCARD_ENABLED |
506                                VLIB_EFD_MONITOR_ENABLED);
507         } else {
508             return clib_error_return(0, "Usage: set efd enable [dpdk | "
509                                      "worker | monitor | drop_all | default]");
510         }
511     } else if (unformat(input, "disable")) {
512         if (unformat(input, "dpdk")) {
513             dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
514         } else if (unformat(input, "worker")) {
515             tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
516         } else if (unformat(input, "monitor")) {
517             dm->efd.enabled &= ~DPDK_EFD_MONITOR_ENABLED;
518             tm->efd.enabled &= ~VLIB_EFD_MONITOR_ENABLED;
519         } else if (unformat(input, "drop_all")) {
520             dm->efd.enabled &= ~DPDK_EFD_DROPALL_ENABLED;
521         } else  if (unformat(input, "all")) {
522             dm->efd.enabled = 0;
523             tm->efd.enabled = 0;
524         } else {
525             return clib_error_return(0, "Usage: set efd disable [dpdk | "
526                                      "worker | monitor | drop_all | all]");
527         }
528     } else if (unformat(input, "worker_queue_hi_thresh")) {
529         u32 mark;
530         if (unformat (input, "%u", &mark)) {
531             tm->efd.queue_hi_thresh = mark;
532         } else {
533             return clib_error_return(0, "unknown input `%U'",
534                                      format_unformat_error, input);
535         }
536     } else if (unformat(input, "dpdk_device_hi_thresh")) {
537         u32 thresh;
538         if (unformat (input, "%u", &thresh)) {
539             dm->efd.queue_hi_thresh = thresh;
540         } else {
541             return clib_error_return(0, "unknown input `%U'",
542                                      format_unformat_error, input);
543         }
544     } else if (unformat(input, "consec_full_frames_hi_thresh")) {
545         u32 thresh;
546         if (unformat (input, "%u", &thresh)) {
547             dm->efd.consec_full_frames_hi_thresh = thresh;
548         } else {
549             return clib_error_return(0, "unknown input `%U'",
550                                      format_unformat_error, input);
551         }
552     } else if (unformat(input, "ip-prec")) {
553         return (parse_op_and_prec(vm, input, cmd,
554                                  "ip", &tm->efd.ip_prec_bitmap));
555     } else if (unformat(input, "mpls-exp")) {
556         return (parse_op_and_prec(vm, input, cmd,
557                                  "mpls", &tm->efd.mpls_exp_bitmap));
558     } else if (unformat(input, "vlan-cos")) {
559         return (parse_op_and_prec(vm, input, cmd,
560                                  "vlan", &tm->efd.vlan_cos_bitmap));
561     } else if (unformat(input, "help")) {
562         vlib_cli_output(vm,
563             "Usage:\n"
564             "  set efd enable <dpdk | worker | monitor | drop_all | default> |\n"
565             "  set efd disable <dpdk | worker | monitor | drop_all | all> |\n"
566             "  set efd <ip-prec | mpls-exp | vlan-cos> <ge | lt> <0-7>\n"
567             "  set efd worker_queue_hi_thresh <0-32> |\n"
568             "  set efd dpdk_device_hi_thresh <0-%d> |\n"
569             "  set efd consec_full_frames_hi_thresh <count> |\n",
570             DPDK_NB_RX_DESC_10GE);
571     } else {
572         return clib_error_return(0, "unknown input `%U'",
573                                   format_unformat_error, input);
574     }
575
576     if (dm->efd.enabled)
577         rt->function = dpdk_input_efd_multiarch_select();
578     else if (dm->use_rss)
579         rt->function = dpdk_input_rss_multiarch_select();
580     else
581         rt->function = dpdk_input_multiarch_select();
582
583     return error;
584 }
585
586 VLIB_CLI_COMMAND (cmd_set_efd,static) = {
587     .path = "set efd",
588     .short_help = "set early-fast-discard commands",
589     .function = set_efd,
590 };
591
592 static clib_error_t *
593 set_dpdk_if_desc (vlib_main_t *vm, unformat_input_t *input,
594           vlib_cli_command_t *cmd)
595 {
596   unformat_input_t _line_input, * line_input = &_line_input;
597   dpdk_main_t * dm = &dpdk_main;
598   vnet_hw_interface_t * hw;
599   dpdk_device_t * xd;
600   u32 hw_if_index = (u32) ~0;
601   u32 nb_rx_desc = (u32) ~0;
602   u32 nb_tx_desc = (u32) ~0;
603   clib_error_t * rv;
604
605   if (! unformat_user (input, unformat_line_input, line_input))
606     return 0;
607
608   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
609     if (unformat (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
610                   &hw_if_index))
611       ;
612     else if (unformat (line_input, "tx %d", &nb_tx_desc))
613       ;
614     else if (unformat (line_input, "rx %d", &nb_rx_desc))
615       ;
616     else
617       return clib_error_return (0, "parse error: '%U'",
618                                 format_unformat_error, line_input);
619   }
620
621   unformat_free (line_input);
622
623   if (hw_if_index == (u32) ~0)
624     return clib_error_return (0, "please specify valid interface name");
625
626   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
627   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
628
629   if (xd->dev_type != VNET_DPDK_DEV_ETH)
630     return clib_error_return (0, "number of descriptors can be set only for "
631                               "physical devices");
632
633   if ((nb_rx_desc == (u32) ~0 || nb_rx_desc == xd->nb_rx_desc) &&
634       (nb_tx_desc == (u32) ~0 || nb_tx_desc == xd->nb_tx_desc))
635     return clib_error_return (0, "nothing changed");
636
637   if (nb_rx_desc != (u32) ~0)
638         xd->nb_rx_desc = nb_rx_desc;
639
640   if (nb_tx_desc != (u32) ~0)
641         xd->nb_rx_desc = nb_rx_desc;
642
643   rv = dpdk_port_setup(dm, xd);
644
645   return rv;
646 }
647
648 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
649     .path = "set dpdk interface descriptors",
650     .short_help = "set dpdk interface descriptors <if-name> [rx <n>] [tx <n>]",
651     .function = set_dpdk_if_desc,
652 };
653
654 static clib_error_t *
655 show_dpdk_if_placement (vlib_main_t *vm, unformat_input_t *input,
656           vlib_cli_command_t *cmd)
657 {
658   vlib_thread_main_t * tm = vlib_get_thread_main();
659   dpdk_main_t * dm = &dpdk_main;
660   dpdk_device_and_queue_t * dq;
661   int cpu;
662
663   if (tm->n_vlib_mains == 1)
664     vlib_cli_output(vm, "All interfaces are handled by main thread");
665
666   for(cpu = 0; cpu < vec_len(dm->devices_by_cpu); cpu++)
667     {
668       if (vec_len(dm->devices_by_cpu[cpu]))
669         vlib_cli_output(vm, "Thread %u (%s at lcore %u):", cpu,
670                         vlib_worker_threads[cpu].name,
671                         vlib_worker_threads[cpu].dpdk_lcore_id);
672
673       vec_foreach(dq, dm->devices_by_cpu[cpu])
674         {
675           u32 hw_if_index = dm->devices[dq->device].vlib_hw_if_index;
676           vnet_hw_interface_t * hi =  vnet_get_hw_interface(dm->vnet_main, hw_if_index);
677           vlib_cli_output(vm, "  %v queue %u", hi->name, dq->queue_id);
678         }
679     }
680   return 0;
681 }
682
683 VLIB_CLI_COMMAND (cmd_show_dpdk_if_placement,static) = {
684     .path = "show dpdk interface placement",
685     .short_help = "show dpdk interface placement",
686     .function = show_dpdk_if_placement,
687 };
688
689 static int
690 dpdk_device_queue_sort(void * a1, void * a2)
691 {
692   dpdk_device_and_queue_t * dq1 = a1;
693   dpdk_device_and_queue_t * dq2 = a2;
694
695   if (dq1->device > dq2->device)
696     return 1;
697   else if (dq1->device < dq2->device)
698     return -1;
699   else if (dq1->queue_id > dq2->queue_id)
700     return 1;
701   else if (dq1->queue_id < dq2->queue_id)
702     return -1;
703   else
704     return 0;
705 }
706
707 static clib_error_t *
708 set_dpdk_if_placement (vlib_main_t *vm, unformat_input_t *input,
709           vlib_cli_command_t *cmd)
710 {
711   unformat_input_t _line_input, * line_input = &_line_input;
712   dpdk_main_t * dm = &dpdk_main;
713   dpdk_device_and_queue_t * dq;
714   vnet_hw_interface_t * hw;
715   dpdk_device_t * xd;
716   u32 hw_if_index = (u32) ~0;
717   u32 queue = (u32) 0;
718   u32 cpu = (u32) ~0;
719   int i;
720
721   if (! unformat_user (input, unformat_line_input, line_input))
722     return 0;
723
724   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
725     if (unformat (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
726                   &hw_if_index))
727       ;
728     else if (unformat (line_input, "queue %d", &queue))
729       ;
730     else if (unformat (line_input, "thread %d", &cpu))
731       ;
732     else
733       return clib_error_return (0, "parse error: '%U'",
734                                 format_unformat_error, line_input);
735   }
736
737   unformat_free (line_input);
738
739   if (hw_if_index == (u32) ~0)
740     return clib_error_return (0, "please specify valid interface name");
741
742   if (cpu < dm->input_cpu_first_index ||
743       cpu >= (dm->input_cpu_first_index + dm->input_cpu_count))
744     return clib_error_return (0, "please specify valid thread id");
745
746   hw = vnet_get_hw_interface (dm->vnet_main, hw_if_index);
747   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
748
749   for(i = 0; i < vec_len(dm->devices_by_cpu); i++)
750     {
751       vec_foreach(dq, dm->devices_by_cpu[i])
752         {
753           if (hw_if_index == dm->devices[dq->device].vlib_hw_if_index &&
754               queue == dq->queue_id)
755             {
756               if (cpu == i) /* nothing to do */
757                 return 0;
758
759               vec_del1(dm->devices_by_cpu[i], dq - dm->devices_by_cpu[i]);
760               vec_add2(dm->devices_by_cpu[cpu], dq, 1);
761               dq->queue_id = queue;
762               dq->device = xd->device_index;
763               xd->cpu_socket_id_by_queue[queue] =
764                 rte_lcore_to_socket_id(vlib_worker_threads[cpu].dpdk_lcore_id);
765
766               vec_sort_with_function(dm->devices_by_cpu[i],
767                                      dpdk_device_queue_sort);
768
769               vec_sort_with_function(dm->devices_by_cpu[cpu],
770                                      dpdk_device_queue_sort);
771
772               if (vec_len(dm->devices_by_cpu[i]) == 0)
773                 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
774                                      VLIB_NODE_STATE_DISABLED);
775
776               if (vec_len(dm->devices_by_cpu[cpu]) == 1)
777                 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
778                                      VLIB_NODE_STATE_POLLING);
779
780               return 0;
781             }
782         }
783     }
784
785   return clib_error_return (0, "not found");
786 }
787
788 VLIB_CLI_COMMAND (cmd_set_dpdk_if_placement,static) = {
789     .path = "set dpdk interface placement",
790     .short_help = "set dpdk interface placement <if-name> [queue <n>]  thread <n>",
791     .function = set_dpdk_if_placement,
792 };
793
794 clib_error_t *
795 dpdk_cli_init (vlib_main_t * vm)
796 {
797   return 0;
798 }
799
800 VLIB_INIT_FUNCTION (dpdk_cli_init);