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