FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
cli.c
Go to the documentation of this file.
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>
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 *
45  vlib_cli_command_t * cmd)
46 {
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 *
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 *
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 
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 *
264  vlib_cli_command_t * cmd)
265 {
266  unformat_input_t _line_input, *line_input = &_line_input;
267  dpdk_main_t *dm = &dpdk_main;
269  dpdk_device_t *xd;
270  u32 hw_if_index = (u32) ~ 0;
271  u32 nb_rx_desc = (u32) ~ 0;
272  u32 nb_tx_desc = (u32) ~ 0;
273  clib_error_t *error = NULL;
274 
275  if (!unformat_user (input, unformat_line_input, line_input))
276  return 0;
277 
278  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
279  {
280  if (unformat
281  (line_input, "%U", unformat_vnet_hw_interface, dm->vnet_main,
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 (dm->vnet_main, hw_if_index);
303  xd = vec_elt_at_index (dm->devices, hw->dev_instance);
304 
305  if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
306  {
307  error =
309  "number of descriptors can be set only for "
310  "physical devices");
311  goto done;
312  }
313 
314  if ((nb_rx_desc == (u32) ~ 0 || nb_rx_desc == xd->nb_rx_desc) &&
315  (nb_tx_desc == (u32) ~ 0 || nb_tx_desc == xd->nb_tx_desc))
316  {
317  error = clib_error_return (0, "nothing changed");
318  goto done;
319  }
320 
321  if (nb_rx_desc != (u32) ~ 0)
322  xd->nb_rx_desc = nb_rx_desc;
323 
324  if (nb_tx_desc != (u32) ~ 0)
325  xd->nb_tx_desc = nb_tx_desc;
326 
327  dpdk_device_setup (xd);
328 
329  if (vec_len (xd->errors))
330  return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
331 
332 done:
333  unformat_free (line_input);
334 
335  return error;
336 }
337 
338 /*?
339  * This command sets the number of DPDK '<em>rx</em>' and
340  * '<em>tx</em>' descriptors for the given physical interface. Use
341  * the command '<em>show hardware-interface</em>' to display the
342  * current descriptor allocation.
343  *
344  * @cliexpar
345  * Example of how to set the DPDK interface descriptors:
346  * @cliexcmd{set dpdk interface descriptors GigabitEthernet0/8/0 rx 512 tx 512}
347 ?*/
348 /* *INDENT-OFF* */
349 VLIB_CLI_COMMAND (cmd_set_dpdk_if_desc,static) = {
350  .path = "set dpdk interface descriptors",
351  .short_help = "set dpdk interface descriptors <interface> [rx <nn>] [tx <nn>]",
352  .function = set_dpdk_if_desc,
353 };
354 /* *INDENT-ON* */
355 
356 static clib_error_t *
358  unformat_input_t * input,
359  vlib_cli_command_t * cmd)
360 {
361 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
362  _("DPDK Version", "%s", rte_version ());
363  _("DPDK EAL init args", "%s", dpdk_config_main.eal_init_args_str);
364 #undef _
365  return 0;
366 }
367 
368 /*?
369  * This command is used to display the current DPDK version and
370  * the list of arguments passed to DPDK when started.
371  *
372  * @cliexpar
373  * Example of how to display how many DPDK buffer test command has allocated:
374  * @cliexstart{show dpdk version}
375  * DPDK Version: DPDK 16.11.0
376  * DPDK EAL init args: -c 1 -n 4 --huge-dir /run/vpp/hugepages --file-prefix vpp -w 0000:00:08.0 -w 0000:00:09.0 --master-lcore 0 --socket-mem 256
377  * @cliexend
378 ?*/
379 /* *INDENT-OFF* */
380 VLIB_CLI_COMMAND (show_vpe_version_command, static) = {
381  .path = "show dpdk version",
382  .short_help = "show dpdk version",
383  .function = show_dpdk_version_command_fn,
384 };
385 /* *INDENT-ON* */
386 
387 /* Dummy function to get us linked in. */
388 void
390 {
391 }
392 
393 clib_error_t *
395 {
396  return 0;
397 }
398 
400 
401 /*
402  * fd.io coding-style-patch-verification: ON
403  *
404  * Local Variables:
405  * eval: (c-set-style "gnu")
406  * End:
407  */
unformat_function_t unformat_vnet_hw_interface
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:507
u8 count
Definition: dhcp.api:208
u8 * eal_init_args_str
Definition: dpdk.h:278
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:937
dpdk_main_t dpdk_main
Definition: init.c:46
u16 flags
Definition: dpdk.h:176
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
clib_error_t * errors
Definition: dpdk.h:220
vlib_buffer_main_t * buffer_main
Definition: main.h:152
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
void dpdk_cli_reference(void)
Definition: cli.c:389
unsigned char u8
Definition: types.h:56
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
dpdk_config_main_t dpdk_config_main
Definition: init.c:47
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:281
unsigned int u32
Definition: types.h:88
void dpdk_device_setup(dpdk_device_t *xd)
Definition: common.c:39
unformat_function_t unformat_line_input
Definition: format.h:283
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
char * name
Definition: main.h:140
static clib_error_t * show_dpdk_buffer(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:44
static clib_error_t * set_dpdk_if_desc(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:263
struct _unformat_input_t unformat_input_t
#define clib_error_return_unix(e, args...)
Definition: error.h:102
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:454
u16 nb_rx_desc
Definition: dpdk.h:190
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
Definition: sysfs.c:50
vlib_main_t * vm
Definition: in2out_ed.c:1599
static clib_error_t * show_dpdk_version_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:357
u8 len
Definition: ip_types.api:92
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
dpdk_device_t * devices
Definition: dpdk.h:326
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
format_function_t format_dpdk_device_errors
Definition: dpdk.h:442
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
static clib_error_t * test_dpdk_buffer(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:175
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define F_SETPIPE_SZ
#define vec_foreach(var, vec)
Vector iterator.
clib_error_t * dpdk_cli_init(vlib_main_t *vm)
Definition: cli.c:394
struct rte_mempool ** dpdk_mempool_by_buffer_pool_index
Definition: buffer.c:33
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
vnet_main_t * vnet_main
Definition: dpdk.h:344
u16 nb_tx_desc
Definition: dpdk.h:178
static clib_error_t * show_dpdk_physmem(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:90
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171