memif: improve error reporting
[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   clib_error_t *err;
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           unformat_free (line_input);
57           return clib_error_return (0, "unknown input `%U'",
58                                     format_unformat_error, input);
59         }
60     }
61
62   unformat_free (line_input);
63
64   if (socket_id == 0 || socket_id == ~0)
65     {
66       vec_free (socket_filename);
67       return clib_error_return (0, "Invalid socket id");
68     }
69
70   if (!socket_filename || *socket_filename == 0)
71     {
72       vec_free (socket_filename);
73       return clib_error_return (0, "Invalid socket filename");
74     }
75
76   err = memif_socket_filename_add_del (1, socket_id, socket_filename);
77
78   vec_free (socket_filename);
79
80   return err;
81 }
82
83 /* *INDENT-OFF* */
84 VLIB_CLI_COMMAND (memif_socket_filename_create_command, static) = {
85   .path = "create memif socket",
86   .short_help = "create memif socket [id <id>] [filename <path>]",
87   .function = memif_socket_filename_create_command_fn,
88 };
89 /* *INDENT-ON* */
90
91 static clib_error_t *
92 memif_socket_filename_delete_command_fn (vlib_main_t * vm,
93                                          unformat_input_t * input,
94                                          vlib_cli_command_t * cmd)
95 {
96   unformat_input_t _line_input, *line_input = &_line_input;
97   u32 socket_id;
98
99   /* Get a line of input. */
100   if (!unformat_user (input, unformat_line_input, line_input))
101     return 0;
102
103   socket_id = ~0;
104
105   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
106     {
107       if (unformat (line_input, "id %u", &socket_id))
108         ;
109       else
110         {
111           unformat_free (line_input);
112           return clib_error_return (0, "unknown input `%U'",
113                                     format_unformat_error, input);
114         }
115     }
116
117   unformat_free (line_input);
118
119   if (socket_id == 0 || socket_id == ~0)
120     {
121       return clib_error_return (0, "Invalid socket id");
122     }
123
124   return memif_socket_filename_add_del (0, socket_id, 0);
125 }
126
127 /* *INDENT-OFF* */
128 VLIB_CLI_COMMAND (memif_socket_filename_delete_command, static) = {
129   .path = "delete memif socket",
130   .short_help = "delete memif socket [id <id>]",
131   .function = memif_socket_filename_delete_command_fn,
132 };
133 /* *INDENT-ON* */
134
135 static clib_error_t *
136 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
137                          vlib_cli_command_t * cmd)
138 {
139   unformat_input_t _line_input, *line_input = &_line_input;
140   clib_error_t *err;
141   u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
142   memif_create_if_args_t args = { 0 };
143   args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
144   u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
145   u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
146
147   /* Get a line of input. */
148   if (!unformat_user (input, unformat_line_input, line_input))
149     return 0;
150
151   args.is_zero_copy = 1;
152
153   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
154     {
155       if (unformat (line_input, "id %u", &args.id))
156         ;
157       else if (unformat (line_input, "socket-id %u", &args.socket_id))
158         ;
159       else if (unformat (line_input, "secret %s", &args.secret))
160         ;
161       else if (unformat (line_input, "ring-size %u", &ring_size))
162         ;
163       else if (unformat (line_input, "rx-queues %u", &rx_queues))
164         ;
165       else if (unformat (line_input, "tx-queues %u", &tx_queues))
166         ;
167       else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
168         ;
169       else if (unformat (line_input, "master"))
170         args.is_master = 1;
171       else if (unformat (line_input, "slave"))
172         args.is_master = 0;
173       else if (unformat (line_input, "no-zero-copy"))
174         args.is_zero_copy = 0;
175       else if (unformat (line_input, "mode ip"))
176         args.mode = MEMIF_INTERFACE_MODE_IP;
177       else if (unformat (line_input, "hw-addr %U",
178                          unformat_ethernet_address, args.hw_addr))
179         args.hw_addr_set = 1;
180       else
181         {
182           unformat_free (line_input);
183           return clib_error_return (0, "unknown input `%U'",
184                                     format_unformat_error, input);
185         }
186     }
187   unformat_free (line_input);
188
189   if (!is_pow2 (ring_size))
190     return clib_error_return (0, "ring size must be power of 2");
191
192   if (ring_size > 32768)
193     return clib_error_return (0, "maximum ring size is 32768");
194
195   args.log2_ring_size = min_log2 (ring_size);
196
197   if (rx_queues > 255 || rx_queues < 1)
198     return clib_error_return (0, "rx queue must be between 1 - 255");
199   if (tx_queues > 255 || tx_queues < 1)
200     return clib_error_return (0, "tx queue must be between 1 - 255");
201
202   args.rx_queues = rx_queues;
203   args.tx_queues = tx_queues;
204
205   err = memif_create_if (vm, &args);
206
207   vec_free (args.secret);
208
209   return err;
210 }
211
212 /* *INDENT-OFF* */
213 VLIB_CLI_COMMAND (memif_create_command, static) = {
214   .path = "create interface memif",
215   .short_help = "create interface memif [id <id>] [socket-id <socket-id>] "
216                 "[ring-size <size>] [buffer-size <size>] "
217                 "[hw-addr <mac-address>] "
218                 "<master|slave> [rx-queues <number>] [tx-queues <number>] "
219                 "[mode ip] [secret <string>]",
220   .function = memif_create_command_fn,
221 };
222 /* *INDENT-ON* */
223
224 static clib_error_t *
225 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
226                          vlib_cli_command_t * cmd)
227 {
228   unformat_input_t _line_input, *line_input = &_line_input;
229   u32 sw_if_index = ~0;
230   vnet_hw_interface_t *hw;
231   memif_main_t *mm = &memif_main;
232   memif_if_t *mif;
233   vnet_main_t *vnm = vnet_get_main ();
234
235   /* Get a line of input. */
236   if (!unformat_user (input, unformat_line_input, line_input))
237     return 0;
238
239   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
240     {
241       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
242         ;
243       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
244                          vnm, &sw_if_index))
245         ;
246       else
247         {
248           unformat_free (line_input);
249           return clib_error_return (0, "unknown input `%U'",
250                                     format_unformat_error, input);
251         }
252     }
253   unformat_free (line_input);
254
255   if (sw_if_index == ~0)
256     return clib_error_return (0,
257                               "please specify interface name or sw_if_index");
258
259   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
260   if (hw == NULL || memif_device_class.index != hw->dev_class_index)
261     return clib_error_return (0, "not a memif interface");
262
263   mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
264   memif_delete_if (vm, mif);
265
266   return 0;
267 }
268
269 /* *INDENT-OFF* */
270 VLIB_CLI_COMMAND (memif_delete_command, static) = {
271   .path = "delete interface memif",
272   .short_help = "delete interface memif {<interface> | sw_if_index <sw_idx>}",
273   .function = memif_delete_command_fn,
274 };
275 /* *INDENT-ON* */
276
277 static u8 *
278 format_memif_if_flags (u8 * s, va_list * args)
279 {
280   u32 flags = va_arg (*args, u32);
281 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
282   foreach_memif_if_flag
283 #undef _
284     return s;
285 }
286
287 static u8 *
288 format_memif_if_mode (u8 * s, va_list * args)
289 {
290   memif_if_t *mif = va_arg (*args, memif_if_t *);
291   if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
292     return format (s, "ethernet");
293   if (mif->mode == MEMIF_INTERFACE_MODE_IP)
294     return format (s, "ip");
295   if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
296     return format (s, "punt-inject");
297   return format (s, "unknown mode (%u)", mif->mode);;
298 }
299
300 static u8 *
301 format_memif_queue (u8 * s, va_list * args)
302 {
303   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
304   uword i = va_arg (*args, uword);
305   u32 indent = format_get_indent (s);
306
307   s = format (s, "%U%s ring %u:\n",
308               format_white_space, indent,
309               (mq->type == MEMIF_RING_S2M) ?
310               "slave-to-master" : "master-to-slave", i);
311   s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
312               format_white_space, indent + 4,
313               mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
314
315   if (mq->ring)
316     s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
317                 format_white_space, indent + 4,
318                 mq->ring->head, mq->ring->tail, mq->ring->flags,
319                 mq->int_count);
320
321   return s;
322 }
323
324 static u8 *
325 format_memif_descriptor (u8 * s, va_list * args)
326 {
327   memif_if_t *mif = va_arg (*args, memif_if_t *);
328   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
329   u32 indent = format_get_indent (s);
330   memif_ring_t *ring;
331   u16 ring_size;
332   u16 slot;
333
334   ring_size = 1 << mq->log2_ring_size;
335   ring = mq->ring;
336   if (ring)
337     {
338       s = format (s, "%Udescriptor table:\n", format_white_space, indent);
339       s = format (s,
340                   "%Uid    flags region len         address         offset    "
341                   "    user address\n",
342                   format_white_space, indent);
343       s = format (s,
344                   "%U===== ===== ====== ======== ================== "
345                   "========== ==================\n",
346                   format_white_space, indent);
347       for (slot = 0; slot < ring_size; slot++)
348         {
349           s = format (s, "%U%-5d %-5d %-6d %-7d  0x%016lx %-10d 0x%016lx\n",
350                       format_white_space, indent, slot, ring->desc[slot].flags,
351                       ring->desc[slot].region, ring->desc[slot].length,
352                       mif->regions[ring->desc[slot].region].shm,
353                       ring->desc[slot].offset,
354                       memif_get_buffer (mif, ring, slot));
355         }
356       s = format (s, "\n");
357     }
358
359   return s;
360 }
361
362 static clib_error_t *
363 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
364                        vlib_cli_command_t * cmd)
365 {
366   memif_main_t *mm = &memif_main;
367   memif_if_t *mif;
368   vnet_main_t *vnm = vnet_get_main ();
369   memif_region_t *mr;
370   memif_queue_t *mq;
371   uword i;
372   int show_descr = 0;
373   clib_error_t *error = 0;
374   u32 hw_if_index, *hw_if_indices = 0;
375   u32 sock_id;
376   u32 msf_idx;
377   u8 *s = 0;
378
379   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
380     {
381       if (unformat
382           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
383         vec_add1 (hw_if_indices, hw_if_index);
384       else if (unformat (input, "descriptors"))
385         show_descr = 1;
386       else
387         {
388           error = clib_error_return (0, "unknown input `%U'",
389                                      format_unformat_error, input);
390           goto done;
391         }
392     }
393
394   vlib_cli_output (vm, "sockets\n");
395   vlib_cli_output (vm, "  %-3s %-11s %s\n", "id", "listener", "filename");
396
397   /* *INDENT-OFF* */
398   hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
399     ({
400       memif_socket_file_t *msf;
401       u8 *filename;
402
403       msf = pool_elt_at_index(mm->socket_files, msf_idx);
404       filename = msf->filename;
405       if (msf->is_listener)
406         s = format (s, "yes (%u)", msf->ref_cnt);
407       else
408         s = format (s, "no");
409
410       vlib_cli_output(vm, "  %-3u %-11v %s\n", sock_id, s, filename);
411       vec_reset_length (s);
412     }));
413   /* *INDENT-ON* */
414   vec_free (s);
415
416   vlib_cli_output (vm, "\n");
417
418   if (vec_len (hw_if_indices) == 0)
419     {
420       /* *INDENT-OFF* */
421       pool_foreach (mif, mm->interfaces)
422           vec_add1 (hw_if_indices, mif->hw_if_index);
423       /* *INDENT-ON* */
424     }
425
426   for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
427     {
428       vnet_hw_interface_t *hi =
429         vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
430       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
431       memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
432                                                    mif->socket_file_index);
433       vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
434                        vnm, mif->sw_if_index);
435       if (mif->remote_name)
436         vlib_cli_output (vm, "  remote-name \"%s\"", mif->remote_name);
437       if (mif->remote_if_name)
438         vlib_cli_output (vm, "  remote-interface \"%s\"",
439                          mif->remote_if_name);
440       vlib_cli_output (vm, "  socket-id %u id %u mode %U", msf->socket_id,
441                        mif->id, format_memif_if_mode, mif);
442       vlib_cli_output (vm, "  flags%U", format_memif_if_flags, mif->flags);
443       vlib_cli_output (vm, "  listener-fd %d conn-fd %d",
444                        msf->sock ? msf->sock->fd : 0,
445                        mif->sock ? mif->sock->fd : 0);
446       vlib_cli_output (vm, "  num-s2m-rings %u num-m2s-rings %u "
447                        "buffer-size %u num-regions %u",
448                        mif->run.num_s2m_rings, mif->run.num_m2s_rings,
449                        mif->run.buffer_size, vec_len (mif->regions));
450
451       if (mif->local_disc_string)
452         vlib_cli_output (vm, "  local-disc-reason \"%s\"",
453                          mif->local_disc_string);
454       if (mif->remote_disc_string)
455         vlib_cli_output (vm, "  remote-disc-reason \"%s\"",
456                          mif->remote_disc_string);
457
458       /* *INDENT-OFF* */
459       vec_foreach_index (i, mif->regions)
460         {
461           mr = vec_elt_at_index (mif->regions, i);
462           vlib_cli_output (vm, "  region %u size %u fd %d", i,
463                            mr->region_size, mr->fd);
464         }
465       vec_foreach_index (i, mif->tx_queues)
466         {
467           mq = vec_elt_at_index (mif->tx_queues, i);
468           vlib_cli_output (vm, "  %U", format_memif_queue, mq, i);
469           if (show_descr)
470             vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
471         }
472       vec_foreach_index (i, mif->rx_queues)
473         {
474           mq = vec_elt_at_index (mif->rx_queues, i);
475           vlib_cli_output (vm, "  %U", format_memif_queue, mq, i);
476           if (show_descr)
477             vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
478         }
479       /* *INDENT-ON* */
480     }
481 done:
482   vec_free (hw_if_indices);
483   return error;
484 }
485
486 /* *INDENT-OFF* */
487 VLIB_CLI_COMMAND (memif_show_command, static) = {
488   .path = "show memif",
489   .short_help = "show memif [<interface>] [descriptors]",
490   .function = memif_show_command_fn,
491 };
492 /* *INDENT-ON* */
493
494 clib_error_t *
495 memif_cli_init (vlib_main_t * vm)
496 {
497   return 0;
498 }
499
500 VLIB_INIT_FUNCTION (memif_cli_init);
501
502 /*
503  * fd.io coding-style-patch-verification: ON
504  *
505  * Local Variables:
506  * eval: (c-set-style "gnu")
507  * End:
508  */