memif: fix crash caused by zero pkt len in memif and clear dirty cache while interfac...
[vpp.git] / src / plugins / memif / cli.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25
26 #include <memif/memif.h>
27 #include <memif/private.h>
28
29
30 static clib_error_t *
31 memif_socket_filename_create_command_fn (vlib_main_t * vm,
32                                          unformat_input_t * input,
33                                          vlib_cli_command_t * cmd)
34 {
35   unformat_input_t _line_input, *line_input = &_line_input;
36   int r;
37   u32 socket_id;
38   u8 *socket_filename;
39
40   /* Get a line of input. */
41   if (!unformat_user (input, unformat_line_input, line_input))
42     return 0;
43
44   socket_id = ~0;
45   socket_filename = 0;
46
47   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48     {
49       if (unformat (line_input, "id %u", &socket_id))
50         ;
51       else if (unformat (line_input, "filename %s", &socket_filename))
52         ;
53       else
54         {
55           vec_free (socket_filename);
56           return clib_error_return (0, "unknown input `%U'",
57                                     format_unformat_error, input);
58         }
59     }
60
61   unformat_free (line_input);
62
63   if (socket_id == 0 || socket_id == ~0)
64     {
65       vec_free (socket_filename);
66       return clib_error_return (0, "Invalid socket id");
67     }
68
69   if (!socket_filename || *socket_filename == 0)
70     {
71       vec_free (socket_filename);
72       return clib_error_return (0, "Invalid socket filename");
73     }
74
75   r = memif_socket_filename_add_del (1, socket_id, socket_filename);
76
77   vec_free (socket_filename);
78
79   if (r < 0)
80     {
81       switch (r)
82         {
83         case VNET_API_ERROR_INVALID_ARGUMENT:
84           return clib_error_return (0, "Invalid argument");
85         case VNET_API_ERROR_SYSCALL_ERROR_1:
86           return clib_error_return (0, "Syscall error 1");
87         case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
88           return clib_error_return (0, "Already exists");
89         case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
90           return clib_error_return (0, "Interface still in use");
91         default:
92           return clib_error_return (0, "Unknown error");
93         }
94     }
95
96   return 0;
97 }
98
99 /* *INDENT-OFF* */
100 VLIB_CLI_COMMAND (memif_socket_filename_create_command, static) = {
101   .path = "create memif socket",
102   .short_help = "create memif socket [id <id>] [filename <path>]",
103   .function = memif_socket_filename_create_command_fn,
104 };
105 /* *INDENT-ON* */
106
107 static clib_error_t *
108 memif_socket_filename_delete_command_fn (vlib_main_t * vm,
109                                          unformat_input_t * input,
110                                          vlib_cli_command_t * cmd)
111 {
112   unformat_input_t _line_input, *line_input = &_line_input;
113   int r;
114   u32 socket_id;
115
116   /* Get a line of input. */
117   if (!unformat_user (input, unformat_line_input, line_input))
118     return 0;
119
120   socket_id = ~0;
121
122   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
123     {
124       if (unformat (line_input, "id %u", &socket_id))
125         ;
126       else
127         {
128           return clib_error_return (0, "unknown input `%U'",
129                                     format_unformat_error, input);
130         }
131     }
132
133   unformat_free (line_input);
134
135   if (socket_id == 0 || socket_id == ~0)
136     {
137       return clib_error_return (0, "Invalid socket id");
138     }
139
140   r = memif_socket_filename_add_del (0, socket_id, 0);
141
142   if (r < 0)
143     {
144       switch (r)
145         {
146         case VNET_API_ERROR_INVALID_ARGUMENT:
147           return clib_error_return (0, "Invalid argument");
148         case VNET_API_ERROR_SYSCALL_ERROR_1:
149           return clib_error_return (0, "Syscall error 1");
150         case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
151           return clib_error_return (0, "Already exists");
152         case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
153           return clib_error_return (0, "Interface still in use");
154         default:
155           return clib_error_return (0, "Unknown error");
156         }
157     }
158
159   return 0;
160 }
161
162 /* *INDENT-OFF* */
163 VLIB_CLI_COMMAND (memif_socket_filename_delete_command, static) = {
164   .path = "delete memif socket",
165   .short_help = "delete memif socket [id <id>]",
166   .function = memif_socket_filename_delete_command_fn,
167 };
168 /* *INDENT-ON* */
169
170 static clib_error_t *
171 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
172                          vlib_cli_command_t * cmd)
173 {
174   unformat_input_t _line_input, *line_input = &_line_input;
175   int r;
176   u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
177   memif_create_if_args_t args = { 0 };
178   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
179   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
180   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
181
182   /* Get a line of input. */
183   if (!unformat_user (input, unformat_line_input, line_input))
184     return 0;
185
186   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187     {
188       if (unformat (line_input, "id %u", &args.id))
189         ;
190       else if (unformat (line_input, "socket-id %u", &args.socket_id))
191         ;
192       else if (unformat (line_input, "secret %s", &args.secret))
193         ;
194       else if (unformat (line_input, "ring-size %u", &ring_size))
195         ;
196       else if (unformat (line_input, "rx-queues %u", &rx_queues))
197         ;
198       else if (unformat (line_input, "tx-queues %u", &tx_queues))
199         ;
200       else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
201         ;
202       else if (unformat (line_input, "master"))
203         args.is_master = 1;
204       else if (unformat (line_input, "slave"))
205         args.is_master = 0;
206       else if (unformat (line_input, "mode ip"))
207         args.mode = MEMIF_INTERFACE_MODE_IP;
208       else if (unformat (line_input, "hw-addr %U",
209                          unformat_ethernet_address, args.hw_addr))
210         args.hw_addr_set = 1;
211       else
212         return clib_error_return (0, "unknown input `%U'",
213                                   format_unformat_error, input);
214     }
215   unformat_free (line_input);
216
217   if (!is_pow2 (ring_size))
218     return clib_error_return (0, "ring size must be power of 2");
219
220   if (ring_size > 32768)
221     return clib_error_return (0, "maximum ring size is 32768");
222
223   args.log2_ring_size = min_log2 (ring_size);
224
225   if (rx_queues > 255 || rx_queues < 1)
226     return clib_error_return (0, "rx queue must be between 1 - 255");
227   if (tx_queues > 255 || tx_queues < 1)
228     return clib_error_return (0, "tx queue must be between 1 - 255");
229
230   args.rx_queues = rx_queues;
231   args.tx_queues = tx_queues;
232
233   r = memif_create_if (vm, &args);
234
235   vec_free (args.secret);
236
237   if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
238       && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
239     return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
240
241   if (r == VNET_API_ERROR_INVALID_ARGUMENT)
242     return clib_error_return (0, "Invalid argument");
243
244   if (r == VNET_API_ERROR_INVALID_INTERFACE)
245     return clib_error_return (0, "Invalid interface name");
246
247   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
248     return clib_error_return (0, "Interface with same id already exists");
249
250   return 0;
251 }
252
253 /* *INDENT-OFF* */
254 VLIB_CLI_COMMAND (memif_create_command, static) = {
255   .path = "create interface memif",
256   .short_help = "create interface memif [id <id>] [socket-id <socket-id>] "
257                 "[ring-size <size>] [buffer-size <size>] "
258                 "[hw-addr <mac-address>] "
259                 "<master|slave> [rx-queues <number>] [tx-queues <number>] "
260                 "[mode ip] [secret <string>]",
261   .function = memif_create_command_fn,
262 };
263 /* *INDENT-ON* */
264
265 static clib_error_t *
266 create_memif_command_fn (vlib_main_t * vm, unformat_input_t * input,
267                          vlib_cli_command_t * cmd)
268 {
269   vlib_cli_output (vm, "command deprecated. Please use "
270                    "'create interface memif' instead.\n");
271   return 0;
272 }
273
274 /* *INDENT-OFF* */
275 VLIB_CLI_COMMAND (create_memif_command, static) = {
276   .path = "create memif",
277   .function = create_memif_command_fn,
278 };
279 /* *INDENT-ON* */
280
281 static clib_error_t *
282 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
283                          vlib_cli_command_t * cmd)
284 {
285   unformat_input_t _line_input, *line_input = &_line_input;
286   u32 sw_if_index = ~0;
287   vnet_hw_interface_t *hw;
288   memif_main_t *mm = &memif_main;
289   memif_if_t *mif;
290   vnet_main_t *vnm = vnet_get_main ();
291
292   /* Get a line of input. */
293   if (!unformat_user (input, unformat_line_input, line_input))
294     return 0;
295
296   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
297     {
298       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
299         ;
300       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
301                          vnm, &sw_if_index))
302         ;
303       else
304         return clib_error_return (0, "unknown input `%U'",
305                                   format_unformat_error, input);
306     }
307   unformat_free (line_input);
308
309   if (sw_if_index == ~0)
310     return clib_error_return (0,
311                               "please specify interface name or sw_if_index");
312
313   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
314   if (hw == NULL || memif_device_class.index != hw->dev_class_index)
315     return clib_error_return (0, "not a memif interface");
316
317   mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
318   memif_delete_if (vm, mif);
319
320   return 0;
321 }
322
323 /* *INDENT-OFF* */
324 VLIB_CLI_COMMAND (memif_delete_command, static) = {
325   .path = "delete interface memif",
326   .short_help = "delete interface memif {<interface> | sw_if_index <sw_idx>}",
327   .function = memif_delete_command_fn,
328 };
329 /* *INDENT-ON* */
330
331 static u8 *
332 format_memif_if_flags (u8 * s, va_list * args)
333 {
334   u32 flags = va_arg (*args, u32);
335 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
336   foreach_memif_if_flag
337 #undef _
338     return s;
339 }
340
341 static u8 *
342 format_memif_if_mode (u8 * s, va_list * args)
343 {
344   memif_if_t *mif = va_arg (*args, memif_if_t *);
345   if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
346     return format (s, "ethernet");
347   if (mif->mode == MEMIF_INTERFACE_MODE_IP)
348     return format (s, "ip");
349   if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
350     return format (s, "punt-inject");
351   return format (s, "unknown mode (%u)", mif->mode);;
352 }
353
354 static u8 *
355 format_memif_queue (u8 * s, va_list * args)
356 {
357   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
358   uword i = va_arg (*args, uword);
359   u32 indent = format_get_indent (s);
360
361   s = format (s, "%U%s ring %u:\n",
362               format_white_space, indent,
363               (mq->type == MEMIF_RING_S2M) ?
364               "slave-to-master" : "master-to-slave", i);
365   s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
366               format_white_space, indent + 4,
367               mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
368
369   if (mq->ring)
370     s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
371                 format_white_space, indent + 4,
372                 mq->ring->head, mq->ring->tail, mq->ring->flags,
373                 mq->int_count);
374
375   return s;
376 }
377
378 static u8 *
379 format_memif_descriptor (u8 * s, va_list * args)
380 {
381   memif_if_t *mif = va_arg (*args, memif_if_t *);
382   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
383   u32 indent = format_get_indent (s);
384   memif_ring_t *ring;
385   u16 ring_size;
386   u16 slot;
387
388   ring_size = 1 << mq->log2_ring_size;
389   ring = mq->ring;
390   if (ring)
391     {
392       s = format (s, "%Udescriptor table:\n", format_white_space, indent);
393       s =
394         format (s,
395                 "%Uid    flags buf len desc len      address       offset    user address\n",
396                 format_white_space, indent);
397       s =
398         format (s,
399                 "%U===== ===== ======= ======== ================== ====== ==================\n",
400                 format_white_space, indent);
401       for (slot = 0; slot < ring_size; slot++)
402         {
403           s = format (s, "%U%-5d %-5d %-7d %-7d  0x%016lx %-6d 0x%016lx\n",
404                       format_white_space, indent, slot,
405                       ring->desc[slot].flags, ring->desc[slot].buffer_length,
406                       ring->desc[slot].length,
407                       mif->regions[ring->desc[slot].region].shm,
408                       ring->desc[slot].offset, memif_get_buffer (mif, ring,
409                                                                  slot));
410         }
411       s = format (s, "\n");
412     }
413
414   return s;
415 }
416
417 static clib_error_t *
418 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
419                        vlib_cli_command_t * cmd)
420 {
421   memif_main_t *mm = &memif_main;
422   memif_if_t *mif;
423   vnet_main_t *vnm = vnet_get_main ();
424   memif_queue_t *mq;
425   uword i;
426   int show_descr = 0;
427   clib_error_t *error = 0;
428   u32 hw_if_index, *hw_if_indices = 0;
429   u32 sock_id;
430   u32 msf_idx;
431   u8 *s = 0;
432
433   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
434     {
435       if (unformat
436           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
437         vec_add1 (hw_if_indices, hw_if_index);
438       else if (unformat (input, "descriptors"))
439         show_descr = 1;
440       else
441         {
442           error = clib_error_return (0, "unknown input `%U'",
443                                      format_unformat_error, input);
444           goto done;
445         }
446     }
447
448   vlib_cli_output (vm, "sockets\n");
449   vlib_cli_output (vm, "  %-3s %-11s %s\n", "id", "listener", "filename");
450
451   /* *INDENT-OFF* */
452   hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
453     ({
454       memif_socket_file_t *msf;
455       u8 *filename;
456
457       msf = pool_elt_at_index(mm->socket_files, msf_idx);
458       filename = msf->filename;
459       if (msf->is_listener)
460         s = format (s, "yes (%u)", msf->ref_cnt);
461       else
462         s = format (s, "no");
463
464       vlib_cli_output(vm, "  %-3u %-11v %s\n", sock_id, s, filename);
465       vec_reset_length (s);
466     }));
467   /* *INDENT-ON* */
468   vec_free (s);
469
470   vlib_cli_output (vm, "\n");
471
472   if (vec_len (hw_if_indices) == 0)
473     {
474       /* *INDENT-OFF* */
475       pool_foreach (mif, mm->interfaces,
476           vec_add1 (hw_if_indices, mif->hw_if_index);
477       );
478       /* *INDENT-ON* */
479     }
480
481   for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
482     {
483       vnet_hw_interface_t *hi =
484         vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
485       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
486       memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
487                                                    mif->socket_file_index);
488       vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
489                        vnm, mif->sw_if_index);
490       if (mif->remote_name)
491         vlib_cli_output (vm, "  remote-name \"%s\"", mif->remote_name);
492       if (mif->remote_if_name)
493         vlib_cli_output (vm, "  remote-interface \"%s\"",
494                          mif->remote_if_name);
495       vlib_cli_output (vm, "  socket-id %u id %u mode %U", msf->socket_id,
496                        mif->id, format_memif_if_mode, mif);
497       vlib_cli_output (vm, "  flags%U", format_memif_if_flags, mif->flags);
498       vlib_cli_output (vm, "  listener-fd %d conn-fd %d",
499                        msf->sock ? msf->sock->fd : 0,
500                        mif->sock ? mif->sock->fd : 0);
501       vlib_cli_output (vm,
502                        "  num-s2m-rings %u num-m2s-rings %u buffer-size %u",
503                        mif->run.num_s2m_rings, mif->run.num_m2s_rings,
504                        mif->run.buffer_size);
505
506       if (mif->local_disc_string)
507         vlib_cli_output (vm, "  local-disc-reason \"%s\"",
508                          mif->local_disc_string);
509       if (mif->remote_disc_string)
510         vlib_cli_output (vm, "  remote-disc-reason \"%s\"",
511                          mif->remote_disc_string);
512
513       vec_foreach_index (i, mif->tx_queues)
514       {
515         mq = vec_elt_at_index (mif->tx_queues, i);
516         vlib_cli_output (vm, "  %U", format_memif_queue, mq, i);
517         if (show_descr)
518           vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
519       }
520       vec_foreach_index (i, mif->rx_queues)
521       {
522         mq = vec_elt_at_index (mif->rx_queues, i);
523         vlib_cli_output (vm, "  %U", format_memif_queue, mq, i);
524         if (show_descr)
525           vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
526       }
527     }
528 done:
529   vec_free (hw_if_indices);
530   return error;
531 }
532
533 /* *INDENT-OFF* */
534 VLIB_CLI_COMMAND (memif_show_command, static) = {
535   .path = "show memif",
536   .short_help = "show memif [<interface>] [descriptors]",
537   .function = memif_show_command_fn,
538 };
539 /* *INDENT-ON* */
540
541 clib_error_t *
542 memif_cli_init (vlib_main_t * vm)
543 {
544   return 0;
545 }
546
547 VLIB_INIT_FUNCTION (memif_cli_init);
548
549 /*
550  * fd.io coding-style-patch-verification: ON
551  *
552  * Local Variables:
553  * eval: (c-set-style "gnu")
554  * End:
555  */