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