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