udp: fix csum computation when offload disabled
[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 VLIB_CLI_COMMAND (cmd_show_dpdk_buffer,static) = {
81     .path = "show dpdk buffer",
82     .short_help = "show dpdk buffer",
83     .function = show_dpdk_buffer,
84     .is_mp_safe = 1,
85 };
86
87 static clib_error_t *
88 show_dpdk_physmem (vlib_main_t * vm, unformat_input_t * input,
89                    vlib_cli_command_t * cmd)
90 {
91   clib_error_t *err = 0;
92   int fds[2];
93   u8 *s = 0;
94   int n, n_try;
95   FILE *f;
96
97   /*
98    * XXX: Pipes on FreeBSD grow dynamically up to 64KB (FreeBSD 15), don't
99    * manually tweak this value on FreeBSD at the moment.
100    */
101 #ifdef __linux__
102   u32 pipe_max_size;
103
104   err = clib_sysfs_read ("/proc/sys/fs/pipe-max-size", "%u", &pipe_max_size);
105
106   if (err)
107     return err;
108
109   if (pipe (fds) == -1)
110     return clib_error_return_unix (0, "pipe");
111
112 #ifndef F_SETPIPE_SZ
113 #define F_SETPIPE_SZ    (1024 + 7)
114 #endif
115
116   if (fcntl (fds[1], F_SETPIPE_SZ, pipe_max_size) == -1)
117     {
118       err = clib_error_return_unix (0, "fcntl(F_SETPIPE_SZ)");
119       goto error;
120     }
121 #endif /* __linux__ */
122
123   if (fcntl (fds[0], F_SETFL, O_NONBLOCK) == -1)
124     {
125       err = clib_error_return_unix (0, "fcntl(F_SETFL)");
126       goto error;
127     }
128
129   if ((f = fdopen (fds[1], "a")) == 0)
130     {
131       err = clib_error_return_unix (0, "fdopen");
132       goto error;
133     }
134
135   rte_dump_physmem_layout (f);
136   fflush (f);
137
138   n = n_try = 4096;
139   while (n == n_try)
140     {
141       uword len = vec_len (s);
142       vec_resize (s, len + n_try);
143
144       n = read (fds[0], s + len, n_try);
145       if (n < 0 && errno != EAGAIN)
146         {
147           err = clib_error_return_unix (0, "read");
148           goto error;
149         }
150       vec_set_len (s, len + (n < 0 ? 0 : n));
151     }
152
153   vlib_cli_output (vm, "%v", s);
154
155 error:
156   close (fds[0]);
157   close (fds[1]);
158   vec_free (s);
159   return err;
160 }
161
162 /*?
163  * This command displays DPDK physmem layout
164  *
165  * @cliexpar
166  * Example of how to display DPDK physmem layout:
167  * @cliexstart{show dpdk physmem}
168  * @cliexend
169 ?*/
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,
174     .is_mp_safe = 1,
175 };
176
177 static clib_error_t *
178 test_dpdk_buffer (vlib_main_t * vm, unformat_input_t * input,
179                   vlib_cli_command_t * cmd)
180 {
181   static u32 *allocated_buffers;
182   u32 n_alloc = 0;
183   u32 n_free = 0;
184   u32 first, actual_alloc;
185
186   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
187     {
188       if (unformat (input, "allocate %d", &n_alloc))
189         ;
190       else if (unformat (input, "free %d", &n_free))
191         ;
192       else
193         break;
194     }
195
196   if (n_free)
197     {
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));
201
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);
205     }
206   if (n_alloc)
207     {
208       first = vec_len (allocated_buffers);
209       vec_validate (allocated_buffers,
210                     vec_len (allocated_buffers) + n_alloc - 1);
211
212       actual_alloc = vlib_buffer_alloc (vm, allocated_buffers + first,
213                                         n_alloc);
214       vec_set_len (allocated_buffers, first + actual_alloc);
215
216       if (actual_alloc < n_alloc)
217         vlib_cli_output (vm, "WARNING: only allocated %d buffers",
218                          actual_alloc);
219     }
220
221   vlib_cli_output (vm, "Currently %d buffers allocated",
222                    vec_len (allocated_buffers));
223
224   if (allocated_buffers && vec_len (allocated_buffers) == 0)
225     vec_free (allocated_buffers);
226
227   return 0;
228 }
229
230 /*?
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.
236  *
237  * @cliexpar
238  * @parblock
239  *
240  * Example of how to display how many DPDK buffer test command has allocated:
241  * @cliexstart{test dpdk buffer}
242  * Currently 0 buffers allocated
243  * @cliexend
244  *
245  * Example of how to allocate DPDK buffers using the test command:
246  * @cliexstart{test dpdk buffer allocate 10}
247  * Currently 10 buffers allocated
248  * @cliexend
249  *
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
253  * @cliexend
254  * @endparblock
255 ?*/
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,
260     .is_mp_safe = 1,
261 };
262
263 static clib_error_t *
264 set_dpdk_if_desc (vlib_main_t * vm, unformat_input_t * input,
265                   vlib_cli_command_t * cmd)
266 {
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;
271   dpdk_device_t *xd;
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;
276
277   if (!unformat_user (input, unformat_line_input, line_input))
278     return 0;
279
280   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
281     {
282       if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
283                     &hw_if_index))
284         ;
285       else if (unformat (line_input, "tx %d", &nb_tx_desc))
286         ;
287       else if (unformat (line_input, "rx %d", &nb_rx_desc))
288         ;
289       else
290         {
291           error = clib_error_return (0, "parse error: '%U'",
292                                      format_unformat_error, line_input);
293           goto done;
294         }
295     }
296
297   if (hw_if_index == (u32) ~ 0)
298     {
299       error = clib_error_return (0, "please specify valid interface name");
300       goto done;
301     }
302
303   hw = vnet_get_hw_interface (vnm, hw_if_index);
304   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
305
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))
308     {
309       error = clib_error_return (0, "nothing changed");
310       goto done;
311     }
312
313   if (nb_rx_desc != (u32) ~ 0)
314     xd->conf.n_rx_desc = nb_rx_desc;
315
316   if (nb_tx_desc != (u32) ~ 0)
317     xd->conf.n_tx_desc = nb_tx_desc;
318
319   dpdk_device_setup (xd);
320
321   if (vec_len (xd->errors))
322     return clib_error_return (0, "%U", format_dpdk_device_errors, xd);
323
324 done:
325   unformat_free (line_input);
326
327   return error;
328 }
329
330 /*?
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.
335  *
336  * @cliexpar
337  * Example of how to set the DPDK interface descriptors:
338  * @cliexcmd{set dpdk interface descriptors GigabitEthernet0/8/0 rx 512 tx 512}
339 ?*/
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
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)
350 {
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);
354 #undef _
355   return 0;
356 }
357
358 /*?
359  * This command is used to display the current DPDK version and
360  * the list of arguments passed to DPDK when started.
361  *
362  * @cliexpar
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
368  * @cliexend
369 ?*/
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,
374 };
375
376 /* Dummy function to get us linked in. */
377 void
378 dpdk_cli_reference (void)
379 {
380 }
381
382 clib_error_t *
383 dpdk_cli_init (vlib_main_t * vm)
384 {
385   return 0;
386 }
387
388 VLIB_INIT_FUNCTION (dpdk_cli_init);
389
390 /*
391  * fd.io coding-style-patch-verification: ON
392  *
393  * Local Variables:
394  * eval: (c-set-style "gnu")
395  * End:
396  */