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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/ioctl.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
26 #include <memif/memif.h>
27 #include <memif/private.h>
31 memif_socket_filename_create_command_fn (vlib_main_t * vm,
32 unformat_input_t * input,
33 vlib_cli_command_t * cmd)
35 unformat_input_t _line_input, *line_input = &_line_input;
40 /* Get a line of input. */
41 if (!unformat_user (input, unformat_line_input, line_input))
47 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
49 if (unformat (line_input, "id %u", &socket_id))
51 else if (unformat (line_input, "filename %s", &socket_filename))
55 vec_free (socket_filename);
56 return clib_error_return (0, "unknown input `%U'",
57 format_unformat_error, input);
61 unformat_free (line_input);
63 if (socket_id == 0 || socket_id == ~0)
65 vec_free (socket_filename);
66 return clib_error_return (0, "Invalid socket id");
69 if (!socket_filename || *socket_filename == 0)
71 vec_free (socket_filename);
72 return clib_error_return (0, "Invalid socket filename");
75 r = memif_socket_filename_add_del (1, socket_id, socket_filename);
77 vec_free (socket_filename);
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");
92 return clib_error_return (0, "Unknown error");
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,
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)
112 unformat_input_t _line_input, *line_input = &_line_input;
116 /* Get a line of input. */
117 if (!unformat_user (input, unformat_line_input, line_input))
122 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
124 if (unformat (line_input, "id %u", &socket_id))
128 return clib_error_return (0, "unknown input `%U'",
129 format_unformat_error, input);
133 unformat_free (line_input);
135 if (socket_id == 0 || socket_id == ~0)
137 return clib_error_return (0, "Invalid socket id");
140 r = memif_socket_filename_add_del (0, socket_id, 0);
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");
155 return clib_error_return (0, "Unknown error");
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,
170 static clib_error_t *
171 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
172 vlib_cli_command_t * cmd)
174 unformat_input_t _line_input, *line_input = &_line_input;
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;
182 /* Get a line of input. */
183 if (!unformat_user (input, unformat_line_input, line_input))
186 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
188 if (unformat (line_input, "id %u", &args.id))
190 else if (unformat (line_input, "socket-id %u", &args.socket_id))
192 else if (unformat (line_input, "secret %s", &args.secret))
194 else if (unformat (line_input, "ring-size %u", &ring_size))
196 else if (unformat (line_input, "rx-queues %u", &rx_queues))
198 else if (unformat (line_input, "tx-queues %u", &tx_queues))
200 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
202 else if (unformat (line_input, "master"))
204 else if (unformat (line_input, "slave"))
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;
212 return clib_error_return (0, "unknown input `%U'",
213 format_unformat_error, input);
215 unformat_free (line_input);
217 if (!is_pow2 (ring_size))
218 return clib_error_return (0, "ring size must be power of 2");
220 if (ring_size > 32768)
221 return clib_error_return (0, "maximum ring size is 32768");
223 args.log2_ring_size = min_log2 (ring_size);
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");
230 args.rx_queues = rx_queues;
231 args.tx_queues = tx_queues;
233 r = memif_create_if (vm, &args);
235 vec_free (args.secret);
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);
241 if (r == VNET_API_ERROR_INVALID_ARGUMENT)
242 return clib_error_return (0, "Invalid argument");
244 if (r == VNET_API_ERROR_INVALID_INTERFACE)
245 return clib_error_return (0, "Invalid interface name");
247 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
248 return clib_error_return (0, "Interface with same id already exists");
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,
265 static clib_error_t *
266 create_memif_command_fn (vlib_main_t * vm, unformat_input_t * input,
267 vlib_cli_command_t * cmd)
269 vlib_cli_output (vm, "command deprecated. Please use "
270 "'create interface memif' instead.\n");
275 VLIB_CLI_COMMAND (create_memif_command, static) = {
276 .path = "create memif",
277 .function = create_memif_command_fn,
281 static clib_error_t *
282 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
283 vlib_cli_command_t * cmd)
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;
290 vnet_main_t *vnm = vnet_get_main ();
292 /* Get a line of input. */
293 if (!unformat_user (input, unformat_line_input, line_input))
296 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
298 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
300 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
304 return clib_error_return (0, "unknown input `%U'",
305 format_unformat_error, input);
307 unformat_free (line_input);
309 if (sw_if_index == ~0)
310 return clib_error_return (0,
311 "please specify interface name or sw_if_index");
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");
317 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
318 memif_delete_if (vm, mif);
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,
332 format_memif_if_flags (u8 * s, va_list * args)
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
342 format_memif_if_mode (u8 * s, va_list * args)
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);;
355 format_memif_queue (u8 * s, va_list * args)
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);
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);
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,
379 format_memif_descriptor (u8 * s, va_list * args)
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);
388 ring_size = 1 << mq->log2_ring_size;
392 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
395 "%Uid flags len address offset user address\n",
396 format_white_space, indent);
399 "%U===== ===== ======== ================== ====== ==================\n",
400 format_white_space, indent);
401 for (slot = 0; slot < ring_size; slot++)
403 s = format (s, "%U%-5d %-5d %-7d 0x%016lx %-6d 0x%016lx\n",
404 format_white_space, indent, slot,
405 ring->desc[slot].flags,
406 ring->desc[slot].length,
407 mif->regions[ring->desc[slot].region].shm,
408 ring->desc[slot].offset, memif_get_buffer (mif, ring,
411 s = format (s, "\n");
417 static clib_error_t *
418 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
419 vlib_cli_command_t * cmd)
421 memif_main_t *mm = &memif_main;
423 vnet_main_t *vnm = vnet_get_main ();
427 clib_error_t *error = 0;
428 u32 hw_if_index, *hw_if_indices = 0;
433 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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"))
442 error = clib_error_return (0, "unknown input `%U'",
443 format_unformat_error, input);
448 vlib_cli_output (vm, "sockets\n");
449 vlib_cli_output (vm, " %-3s %-11s %s\n", "id", "listener", "filename");
452 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
454 memif_socket_file_t *msf;
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);
462 s = format (s, "no");
464 vlib_cli_output(vm, " %-3u %-11v %s\n", sock_id, s, filename);
465 vec_reset_length (s);
470 vlib_cli_output (vm, "\n");
472 if (vec_len (hw_if_indices) == 0)
475 pool_foreach (mif, mm->interfaces,
476 vec_add1 (hw_if_indices, mif->hw_if_index);
481 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
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);
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);
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);
513 vec_foreach_index (i, mif->tx_queues)
515 mq = vec_elt_at_index (mif->tx_queues, i);
516 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
518 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
520 vec_foreach_index (i, mif->rx_queues)
522 mq = vec_elt_at_index (mif->rx_queues, i);
523 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
525 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
529 vec_free (hw_if_indices);
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,
542 memif_cli_init (vlib_main_t * vm)
547 VLIB_INIT_FUNCTION (memif_cli_init);
550 * fd.io coding-style-patch-verification: ON
553 * eval: (c-set-style "gnu")