bb6c6a9e396d8e1d0b1ab68eef761b08a4c650c9
[vpp.git] / src / plugins / dpdk / device / cli.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <unistd.h>
17 #include <fcntl.h>
18
19 #include <vnet/vnet.h>
20 #include <vppinfra/vec.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/format.h>
23 #include <vppinfra/xxhash.h>
24 #include <vppinfra/linux/sysfs.c>
25
26 #include <vnet/ethernet/ethernet.h>
27 #include <dpdk/buffer.h>
28 #include <dpdk/device/dpdk.h>
29 #include <vnet/classify/vnet_classify.h>
30 #include <vnet/mpls/packet.h>
31
32 #include <dpdk/device/dpdk_priv.h>
33
34 /**
35  * @file
36  * @brief CLI for DPDK Abstraction Layer and pcap Tx Trace.
37  *
38  * This file contains the source code for CLI for DPDK
39  * Abstraction Layer and pcap Tx Trace.
40  */
41
42
43 static clib_error_t *
44 show_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
45                   vlib_cli_command_t * cmd)
46 {
47   vlib_buffer_main_t *bm = vm->buffer_main;
48   vlib_buffer_pool_t *bp;
49
50   vec_foreach (bp, bm->buffer_pools)
51   {
52     struct rte_mempool *rmp = dpdk_mempool_by_buffer_pool_index[bp->index];
53     if (rmp)
54       {
55         unsigned count = rte_mempool_avail_count (rmp);
56         unsigned free_count = rte_mempool_in_use_count (rmp);
57
58         vlib_cli_output (vm,
59                          "name=\"%s\"  available = %7d allocated = %7d total = %7d\n",
60                          rmp->name, (u32) count, (u32) free_count,
61                          (u32) (count + free_count));
62       }
63     else
64       {
65         vlib_cli_output (vm, "rte_mempool is NULL (!)\n");
66       }
67   }
68   return 0;
69 }
70
71 /*?
72  * This command displays statistics of each DPDK mempool.
73  *
74  * @cliexpar
75  * Example of how to display DPDK buffer data:
76  * @cliexstart{show dpdk buffer}
77  * name="mbuf_pool_socket0"  available =   15104 allocated =    1280 total =   16384
78  * @cliexend
79 ?*/
80 /* *INDENT-OFF* */
81 VLIB_CLI_COMMAND (cmd_show_dpdk_buffer,static) = {
82     .path = "show dpdk buffer",
83     .short_help = "show dpdk buffer",
84     .function = show_dpdk_buffer,
85     .is_mp_safe = 1,
86 };
87 /* *INDENT-ON* */
88
89 static clib_error_t *
90 show_dpdk_physmem (vlib_main_t * vm, unformat_input_t * input,
91                    vlib_cli_command_t * cmd)
92 {
93   clib_error_t *err = 0;
94   u32 pipe_max_size;
95   int fds[2];
96   u8 *s = 0;
97   int n, n_try;
98   FILE *f;
99
100   err = clib_sysfs_read ("/proc/sys/fs/pipe-max-size", "%u", &pipe_max_size);
101
102   if (err)
103     return err;
104
105   if (pipe (fds) == -1)
106     return clib_error_return_unix (0, "pipe");
107
108 #ifndef F_SETPIPE_SZ
109 #define F_SETPIPE_SZ    (1024 + 7)
110 #endif
111
112   if (fcntl (fds[1], F_SETPIPE_SZ, pipe_max_size) == -1)
113     {
114       err = clib_error_return_unix (0, "fcntl(F_SETPIPE_SZ)");
115       goto error;
116     }
117
118   if (fcntl (fds[0], F_SETFL, O_NONBLOCK) == -1)
119     {
120       err = clib_error_return_unix (0, "fcntl(F_SETFL)");
121       goto error;
122     }
123
124   if ((f = fdopen (fds[1], "a")) == 0)
125     {
126       err = clib_error_return_unix (0, "fdopen");
127       goto error;
128     }
129
130   rte_dump_physmem_layout (f);
131   fflush (f);
132
133   n = n_try = 4096;
134   while (n == n_try)
135     {
136       uword len = vec_len (s);
137       vec_resize (s, len + n_try);
138
139       n = read (fds[0], s + len, n_try);
140       if (n < 0 && errno != EAGAIN)
141         {
142           err = clib_error_return_unix (0, "read");
143           goto error;
144         }
145       _vec_len (s) = len + (n < 0 ? 0 : n);
146     }
147
148   vlib_cli_output (vm, "%v", s);
149
150 error:
151   close (fds[0]);
152   close (fds[1]);
153   vec_free (s);
154   return err;
155 }
156
157 /*?
158  * This command displays DPDK physmem layout
159  *
160  * @cliexpar
161  * Example of how to display DPDK physmem layout:
162  * @cliexstart{show dpdk physmem}
163  * @cliexend
164 ?*/
165 /* *INDENT-OFF* */
166 VLIB_CLI_COMMAND (cmd_show_dpdk_physmem,static) = {
167     .path = "show dpdk physmem",
168     .short_help = "show dpdk physmem",
169     .function = show_dpdk_physmem,
170     .is_mp_safe = 1,
171 };
172 /* *INDENT-ON* */
173
174 static clib_error_t *
175 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
176                   vlib_cli_command_t * cmd)
177 {
178   static u32 *allocated_buffers;
179   u32 n_alloc = 0;
180   u32 n_free = 0;
181   u32 first, actual_alloc;
182
183   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
184     {
185       if (unformat (input, "allocate %d", &n_alloc))
186         ;
187       else if (unformat (input, "free %d", &n_free))
188         ;
189       else
190         break;
191     }
192
193   if (n_free)
194     {
195       if (vec_len (allocated_buffers) < n_free)
196         return clib_error_return (0, "Can't free %d, only %d allocated",
197                                   n_free, vec_len (allocated_buffers));
198
199       first = vec_len (allocated_buffers) - n_free;
200       vlib_buffer_free (vm, allocated_buffers + first, n_free);
201       _vec_len (allocated_buffers) = first;
202     }
203   if (n_alloc)
204     {
205       first = vec_len (allocated_buffers);
206       vec_validate (allocated_buffers,
207                     vec_len (allocated_buffers) + n_alloc - 1);
208
209       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
210                                         n_alloc);
211       _vec_len (allocated_buffers) = first + actual_alloc;
212
213       if (actual_alloc < n_alloc)
214         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
215                          actual_alloc);
216     }
217
218   vlib_cli_output (vm, "Currently %d buffers allocated",
219                    vec_len (allocated_buffers));
220
221   if (allocated_buffers && vec_len (allocated_buffers) == 0)
222     vec_free (allocated_buffers);
223
224   return 0;
225 }
226
227 /*?
228  * This command tests the allocation and freeing of DPDK buffers.
229  * If both '<em>allocate</em>' and '<em>free</em>' are entered on the
230  * same command, the '<em>free</em>' is executed first. If no
231  * parameters are provided, this command display how many DPDK buffers
232  * the test command has allocated.
233  *
234  * @cliexpar
235  * @parblock
236  *
237  * Example of how to display how many DPDK buffer test command has allocated:
238  * @cliexstart{test dpdk buffer}
239  * Currently 0 buffers allocated
240  * @cliexend
241  *
242  * Example of how to allocate DPDK buffers using the test command:
243  * @cliexstart{test dpdk buffer allocate 10}
244  * Currently 10 buffers allocated
245  * @cliexend
246  *
247  * Example of how to free DPDK buffers allocated by the test command:
248  * @cliexstart{test dpdk buffer free 10}
249  * Currently 0 buffers allocated
250  * @cliexend
251  * @endparblock
252 ?*/
253 /* *INDENT-OFF* */
254 VLIB_CLI_COMMAND (cmd_test_dpdk_buffer,static) = {
255     .path = "test dpdk buffer",
256     .short_help = "test dpdk buffer [allocate <nn>] [free <nn>]",
257     .function = test_dpdk_buffer,
258     .is_mp_safe = 1,
259 };
260 /* *INDENT-ON* */
261
262 static clib_error_t *
263 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
264                   vlib_cli_command_t * cmd)
265 {
266   unformat_input_t _line_input, *line_input = &_line_input;
267   dpdk_main_t *dm = &dpdk_main;
268   vnet_main_t *vnm = vnet_get_main ();
269   vnet_hw_interface_t *hw;
270   dpdk_device_t *xd;
271   u32 hw_if_index = (u32) ~ 0;
272   u32 nb_rx_desc = (u32) ~ 0;
273   u32 nb_tx_desc = (u32) ~ 0;
274   clib_error_t *error = NULL;
275
276   if (!unformat_user (input, unformat_line_input, line_input))
277     return 0;
278
279   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
280     {
281       if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
282                     &hw_if_index))
283         ;
284       else if (unformat (line_input, "tx %d", &nb_tx_desc))
285         ;
286       else if (unformat (line_input, "rx %d", &nb_rx_desc))
287         ;
288       else
289         {
290           error = clib_error_return (0, "parse error: '%U'",
291                                      format_unformat_error, line_input);
292           goto done;
293         }
294     }
295
296   if (hw_if_index == (u32) ~ 0)
297     {
298       error = clib_error_return (0, "please specify valid interface name");
299       goto done;
300     }
301
302   hw = vnet_get_hw_interface (vnm, hw_if_index);
303   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
304
305   if ((nb_rx_desc == (u32) ~ 0 || nb_rx_desc == xd->nb_rx_desc) &&
306       (nb_tx_desc == (u32) ~ 0 || nb_tx_desc == xd->nb_tx_desc))
307     {
308       error = clib_error_return (0, "nothing changed");
309       goto done;
310     }
311
312   if (nb_rx_desc != (u32) ~ 0)
313     xd->nb_rx_desc = nb_rx_desc;
314
315   if (nb_tx_desc != (u32) ~ 0)
316     xd->nb_tx_desc = nb_tx_desc;
317
318   dpdk_device_setup (xd);
319
320   if (vec_len (xd->errors))
321     return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
322
323 done:
324   unformat_free (line_input);
325
326   return error;
327 }
328
329 /*?
330  * This command sets the number of DPDK '<em>rx</em>' and
331  * '<em>tx</em>' descriptors for the given physical interface. Use
332  * the command '<em>show hardware-interface</em>' to display the
333  * current descriptor allocation.
334  *
335  * @cliexpar
336  * Example of how to set the DPDK interface descriptors:
337  * @cliexcmd{set dpdk interface descriptors GigabitEthernet0/8/0 rx 512 tx 512}
338 ?*/
339 /* *INDENT-OFF* */
340 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
341     .path = "set dpdk interface descriptors",
342     .short_help = "set dpdk interface descriptors <interface> [rx <nn>] [tx <nn>]",
343     .function = set_dpdk_if_desc,
344 };
345 /* *INDENT-ON* */
346
347 static clib_error_t *
348 show_dpdk_version_command_fn (vlib_main_t * vm,
349                               unformat_input_t * input,
350                               vlib_cli_command_t * cmd)
351 {
352 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
353   _("DPDK Version", "%s", rte_version ());
354   _("DPDK EAL init args", "%s", dpdk_config_main.eal_init_args_str);
355 #undef _
356   return 0;
357 }
358
359 /*?
360  * This command is used to display the current DPDK version and
361  * the list of arguments passed to DPDK when started.
362  *
363  * @cliexpar
364  * Example of how to display how many DPDK buffer test command has allocated:
365  * @cliexstart{show dpdk version}
366  * DPDK Version:        DPDK 16.11.0
367  * DPDK EAL init args:  --in-memory --no-telemetry --file-prefix vpp
368  *  -w 0000:00:08.0 -w 0000:00:09.0
369  * @cliexend
370 ?*/
371 /* *INDENT-OFF* */
372 VLIB_CLI_COMMAND (show_vpe_version_command, static) = {
373   .path = "show dpdk version",
374   .short_help = "show dpdk version",
375   .function = show_dpdk_version_command_fn,
376 };
377 /* *INDENT-ON* */
378
379 /* Dummy function to get us linked in. */
380 void
381 dpdk_cli_reference (void)
382 {
383 }
384
385 clib_error_t *
386 dpdk_cli_init (vlib_main_t * vm)
387 {
388   return 0;
389 }
390
391 VLIB_INIT_FUNCTION (dpdk_cli_init);
392
393 /*
394  * fd.io coding-style-patch-verification: ON
395  *
396  * Local Variables:
397  * eval: (c-set-style "gnu")
398  * End:
399  */