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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
32 #include <dpdk/device/dpdk_priv.h>
36 * @brief CLI for DPDK Abstraction Layer and pcap Tx Trace.
38 * This file contains the source code for CLI for DPDK
39 * Abstraction Layer and pcap Tx Trace.
44 show_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
45 vlib_cli_command_t * cmd)
47 vlib_buffer_main_t *bm = vm->buffer_main;
48 vlib_buffer_pool_t *bp;
50 vec_foreach (bp, bm->buffer_pools)
52 struct rte_mempool *rmp = dpdk_mempool_by_buffer_pool_index[bp->index];
55 unsigned count = rte_mempool_avail_count (rmp);
56 unsigned free_count = rte_mempool_in_use_count (rmp);
59 "name=\"%s\" available = %7d allocated = %7d total = %7d\n",
60 rmp->name, (u32) count, (u32) free_count,
61 (u32) (count + free_count));
65 vlib_cli_output (vm, "rte_mempool is NULL (!)\n");
72 * This command displays statistics of each DPDK mempool.
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
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,
90 show_dpdk_physmem (vlib_main_t * vm, unformat_input_t * input,
91 vlib_cli_command_t * cmd)
93 clib_error_t *err = 0;
100 err = clib_sysfs_read ("/proc/sys/fs/pipe-max-size", "%u", &pipe_max_size);
105 if (pipe (fds) == -1)
106 return clib_error_return_unix (0, "pipe");
109 #define F_SETPIPE_SZ (1024 + 7)
112 if (fcntl (fds[1], F_SETPIPE_SZ, pipe_max_size) == -1)
114 err = clib_error_return_unix (0, "fcntl(F_SETPIPE_SZ)");
118 if (fcntl (fds[0], F_SETFL, O_NONBLOCK) == -1)
120 err = clib_error_return_unix (0, "fcntl(F_SETFL)");
124 if ((f = fdopen (fds[1], "a")) == 0)
126 err = clib_error_return_unix (0, "fdopen");
130 rte_dump_physmem_layout (f);
136 uword len = vec_len (s);
137 vec_resize (s, len + n_try);
139 n = read (fds[0], s + len, n_try);
140 if (n < 0 && errno != EAGAIN)
142 err = clib_error_return_unix (0, "read");
145 _vec_len (s) = len + (n < 0 ? 0 : n);
148 vlib_cli_output (vm, "%v", s);
158 * This command displays DPDK physmem layout
161 * Example of how to display DPDK physmem layout:
162 * @cliexstart{show dpdk physmem}
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,
174 static clib_error_t *
175 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
176 vlib_cli_command_t * cmd)
178 static u32 *allocated_buffers;
181 u32 first, actual_alloc;
183 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
185 if (unformat (input, "allocate %d", &n_alloc))
187 else if (unformat (input, "free %d", &n_free))
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));
199 first = vec_len (allocated_buffers) - n_free;
200 vlib_buffer_free (vm, allocated_buffers + first, n_free);
201 _vec_len (allocated_buffers) = first;
205 first = vec_len (allocated_buffers);
206 vec_validate (allocated_buffers,
207 vec_len (allocated_buffers) + n_alloc - 1);
209 actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
211 _vec_len (allocated_buffers) = first + actual_alloc;
213 if (actual_alloc < n_alloc)
214 vlib_cli_output (vm, "WARNING: only allocated %d buffers",
218 vlib_cli_output (vm, "Currently %d buffers allocated",
219 vec_len (allocated_buffers));
221 if (allocated_buffers && vec_len (allocated_buffers) == 0)
222 vec_free (allocated_buffers);
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.
237 * Example of how to display how many DPDK buffer test command has allocated:
238 * @cliexstart{test dpdk buffer}
239 * Currently 0 buffers allocated
242 * Example of how to allocate DPDK buffers using the test command:
243 * @cliexstart{test dpdk buffer allocate 10}
244 * Currently 10 buffers allocated
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
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,
262 static clib_error_t *
263 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
264 vlib_cli_command_t * cmd)
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;
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;
276 if (!unformat_user (input, unformat_line_input, line_input))
279 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
281 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
284 else if (unformat (line_input, "tx %d", &nb_tx_desc))
286 else if (unformat (line_input, "rx %d", &nb_rx_desc))
290 error = clib_error_return (0, "parse error: '%U'",
291 format_unformat_error, line_input);
296 if (hw_if_index == (u32) ~ 0)
298 error = clib_error_return (0, "please specify valid interface name");
302 hw = vnet_get_hw_interface (vnm, hw_if_index);
303 xd = vec_elt_at_index (dm->devices, hw->dev_instance);
305 if ((nb_rx_desc == (u32) ~0 || nb_rx_desc == xd->conf.n_rx_desc) &&
306 (nb_tx_desc == (u32) ~0 || nb_tx_desc == xd->conf.n_tx_desc))
308 error = clib_error_return (0, "nothing changed");
312 if (nb_rx_desc != (u32) ~ 0)
313 xd->conf.n_rx_desc = nb_rx_desc;
315 if (nb_tx_desc != (u32) ~ 0)
316 xd->conf.n_tx_desc = nb_tx_desc;
318 dpdk_device_setup (xd);
320 if (vec_len (xd->errors))
321 return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
324 unformat_free (line_input);
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.
336 * Example of how to set the DPDK interface descriptors:
337 * @cliexcmd{set dpdk interface descriptors GigabitEthernet0/8/0 rx 512 tx 512}
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,
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)
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);
360 * This command is used to display the current DPDK version and
361 * the list of arguments passed to DPDK when started.
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
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,
379 /* Dummy function to get us linked in. */
381 dpdk_cli_reference (void)
386 dpdk_cli_init (vlib_main_t * vm)
391 VLIB_INIT_FUNCTION (dpdk_cli_init);
394 * fd.io coding-style-patch-verification: ON
397 * eval: (c-set-style "gnu")