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
80 VLIB_CLI_COMMAND (cmd_show_dpdk_buffer,static) = {
81 .path = "show dpdk buffer",
82 .short_help = "show dpdk buffer",
83 .function = show_dpdk_buffer,
88 show_dpdk_physmem (vlib_main_t * vm, unformat_input_t * input,
89 vlib_cli_command_t * cmd)
91 clib_error_t *err = 0;
98 * XXX: Pipes on FreeBSD grow dynamically up to 64KB (FreeBSD 15), don't
99 * manually tweak this value on FreeBSD at the moment.
104 err = clib_sysfs_read ("/proc/sys/fs/pipe-max-size", "%u", &pipe_max_size);
109 if (pipe (fds) == -1)
110 return clib_error_return_unix (0, "pipe");
113 #define F_SETPIPE_SZ (1024 + 7)
116 if (fcntl (fds[1], F_SETPIPE_SZ, pipe_max_size) == -1)
118 err = clib_error_return_unix (0, "fcntl(F_SETPIPE_SZ)");
121 #endif /* __linux__ */
123 if (fcntl (fds[0], F_SETFL, O_NONBLOCK) == -1)
125 err = clib_error_return_unix (0, "fcntl(F_SETFL)");
129 if ((f = fdopen (fds[1], "a")) == 0)
131 err = clib_error_return_unix (0, "fdopen");
135 rte_dump_physmem_layout (f);
141 uword len = vec_len (s);
142 vec_resize (s, len + n_try);
144 n = read (fds[0], s + len, n_try);
145 if (n < 0 && errno != EAGAIN)
147 err = clib_error_return_unix (0, "read");
150 vec_set_len (s, len + (n < 0 ? 0 : n));
153 vlib_cli_output (vm, "%v", s);
163 * This command displays DPDK physmem layout
166 * Example of how to display DPDK physmem layout:
167 * @cliexstart{show dpdk physmem}
170 VLIB_CLI_COMMAND (cmd_show_dpdk_physmem,static) = {
171 .path = "show dpdk physmem",
172 .short_help = "show dpdk physmem",
173 .function = show_dpdk_physmem,
177 static clib_error_t *
178 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
179 vlib_cli_command_t * cmd)
181 static u32 *allocated_buffers;
184 u32 first, actual_alloc;
186 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
188 if (unformat (input, "allocate %d", &n_alloc))
190 else if (unformat (input, "free %d", &n_free))
198 if (vec_len (allocated_buffers) < n_free)
199 return clib_error_return (0, "Can't free %d, only %d allocated",
200 n_free, vec_len (allocated_buffers));
202 first = vec_len (allocated_buffers) - n_free;
203 vlib_buffer_free (vm, allocated_buffers + first, n_free);
204 vec_set_len (allocated_buffers, first);
208 first = vec_len (allocated_buffers);
209 vec_validate (allocated_buffers,
210 vec_len (allocated_buffers) + n_alloc - 1);
212 actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
214 vec_set_len (allocated_buffers, first + actual_alloc);
216 if (actual_alloc < n_alloc)
217 vlib_cli_output (vm, "WARNING: only allocated %d buffers",
221 vlib_cli_output (vm, "Currently %d buffers allocated",
222 vec_len (allocated_buffers));
224 if (allocated_buffers && vec_len (allocated_buffers) == 0)
225 vec_free (allocated_buffers);
231 * This command tests the allocation and freeing of DPDK buffers.
232 * If both '<em>allocate</em>' and '<em>free</em>' are entered on the
233 * same command, the '<em>free</em>' is executed first. If no
234 * parameters are provided, this command display how many DPDK buffers
235 * the test command has allocated.
240 * Example of how to display how many DPDK buffer test command has allocated:
241 * @cliexstart{test dpdk buffer}
242 * Currently 0 buffers allocated
245 * Example of how to allocate DPDK buffers using the test command:
246 * @cliexstart{test dpdk buffer allocate 10}
247 * Currently 10 buffers allocated
250 * Example of how to free DPDK buffers allocated by the test command:
251 * @cliexstart{test dpdk buffer free 10}
252 * Currently 0 buffers allocated
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,
263 static clib_error_t *
264 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
265 vlib_cli_command_t * cmd)
267 unformat_input_t _line_input, *line_input = &_line_input;
268 dpdk_main_t *dm = &dpdk_main;
269 vnet_main_t *vnm = vnet_get_main ();
270 vnet_hw_interface_t *hw;
272 u32 hw_if_index = (u32) ~ 0;
273 u32 nb_rx_desc = (u32) ~ 0;
274 u32 nb_tx_desc = (u32) ~ 0;
275 clib_error_t *error = NULL;
277 if (!unformat_user (input, unformat_line_input, line_input))
280 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
282 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
285 else if (unformat (line_input, "tx %d", &nb_tx_desc))
287 else if (unformat (line_input, "rx %d", &nb_rx_desc))
291 error = clib_error_return (0, "parse error: '%U'",
292 format_unformat_error, line_input);
297 if (hw_if_index == (u32) ~ 0)
299 error = clib_error_return (0, "please specify valid interface name");
303 hw = vnet_get_hw_interface (vnm, hw_if_index);
304 xd = vec_elt_at_index (dm->devices, hw->dev_instance);
306 if ((nb_rx_desc == (u32) ~0 || nb_rx_desc == xd->conf.n_rx_desc) &&
307 (nb_tx_desc == (u32) ~0 || nb_tx_desc == xd->conf.n_tx_desc))
309 error = clib_error_return (0, "nothing changed");
313 if (nb_rx_desc != (u32) ~ 0)
314 xd->conf.n_rx_desc = nb_rx_desc;
316 if (nb_tx_desc != (u32) ~ 0)
317 xd->conf.n_tx_desc = nb_tx_desc;
319 dpdk_device_setup (xd);
321 if (vec_len (xd->errors))
322 return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
325 unformat_free (line_input);
331 * This command sets the number of DPDK '<em>rx</em>' and
332 * '<em>tx</em>' descriptors for the given physical interface. Use
333 * the command '<em>show hardware-interface</em>' to display the
334 * current descriptor allocation.
337 * Example of how to set the DPDK interface descriptors:
338 * @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,
346 static clib_error_t *
347 show_dpdk_version_command_fn (vlib_main_t * vm,
348 unformat_input_t * input,
349 vlib_cli_command_t * cmd)
351 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
352 _("DPDK Version", "%s", rte_version ());
353 _("DPDK EAL init args", "%s", dpdk_config_main.eal_init_args_str);
359 * This command is used to display the current DPDK version and
360 * the list of arguments passed to DPDK when started.
363 * Example of how to display how many DPDK buffer test command has allocated:
364 * @cliexstart{show dpdk version}
365 * DPDK Version: DPDK 16.11.0
366 * DPDK EAL init args: --in-memory --no-telemetry --file-prefix vpp
367 * -w 0000:00:08.0 -w 0000:00:09.0
370 VLIB_CLI_COMMAND (show_vpe_version_command, static) = {
371 .path = "show dpdk version",
372 .short_help = "show dpdk version",
373 .function = show_dpdk_version_command_fn,
376 /* Dummy function to get us linked in. */
378 dpdk_cli_reference (void)
383 dpdk_cli_init (vlib_main_t * vm)
388 VLIB_INIT_FUNCTION (dpdk_cli_init);
391 * fd.io coding-style-patch-verification: ON
394 * eval: (c-set-style "gnu")