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 unformat_free (line_input);
57 return clib_error_return (0, "unknown input `%U'",
58 format_unformat_error, input);
62 unformat_free (line_input);
64 if (socket_id == 0 || socket_id == ~0)
66 vec_free (socket_filename);
67 return clib_error_return (0, "Invalid socket id");
70 if (!socket_filename || *socket_filename == 0)
72 vec_free (socket_filename);
73 return clib_error_return (0, "Invalid socket filename");
76 r = memif_socket_filename_add_del (1, socket_id, socket_filename);
78 vec_free (socket_filename);
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");
93 return clib_error_return (0, "Unknown error");
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,
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)
113 unformat_input_t _line_input, *line_input = &_line_input;
117 /* Get a line of input. */
118 if (!unformat_user (input, unformat_line_input, line_input))
123 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
125 if (unformat (line_input, "id %u", &socket_id))
129 unformat_free (line_input);
130 return clib_error_return (0, "unknown input `%U'",
131 format_unformat_error, input);
135 unformat_free (line_input);
137 if (socket_id == 0 || socket_id == ~0)
139 return clib_error_return (0, "Invalid socket id");
142 r = memif_socket_filename_add_del (0, socket_id, 0);
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");
157 return clib_error_return (0, "Unknown error");
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,
172 static clib_error_t *
173 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
174 vlib_cli_command_t * cmd)
176 unformat_input_t _line_input, *line_input = &_line_input;
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;
184 /* Get a line of input. */
185 if (!unformat_user (input, unformat_line_input, line_input))
188 args.is_zero_copy = 1;
190 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
192 if (unformat (line_input, "id %u", &args.id))
194 else if (unformat (line_input, "socket-id %u", &args.socket_id))
196 else if (unformat (line_input, "secret %s", &args.secret))
198 else if (unformat (line_input, "ring-size %u", &ring_size))
200 else if (unformat (line_input, "rx-queues %u", &rx_queues))
202 else if (unformat (line_input, "tx-queues %u", &tx_queues))
204 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
206 else if (unformat (line_input, "master"))
208 else if (unformat (line_input, "slave"))
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;
219 unformat_free (line_input);
220 return clib_error_return (0, "unknown input `%U'",
221 format_unformat_error, input);
224 unformat_free (line_input);
226 if (!is_pow2 (ring_size))
227 return clib_error_return (0, "ring size must be power of 2");
229 if (ring_size > 32768)
230 return clib_error_return (0, "maximum ring size is 32768");
232 args.log2_ring_size = min_log2 (ring_size);
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");
239 args.rx_queues = rx_queues;
240 args.tx_queues = tx_queues;
242 r = memif_create_if (vm, &args);
244 vec_free (args.secret);
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);
250 if (r == VNET_API_ERROR_INVALID_ARGUMENT)
251 return clib_error_return (0, "Invalid argument");
253 if (r == VNET_API_ERROR_INVALID_INTERFACE)
254 return clib_error_return (0, "Invalid interface name");
256 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
257 return clib_error_return (0, "Interface with same id already exists");
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,
274 static clib_error_t *
275 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
276 vlib_cli_command_t * cmd)
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;
283 vnet_main_t *vnm = vnet_get_main ();
285 /* Get a line of input. */
286 if (!unformat_user (input, unformat_line_input, line_input))
289 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
291 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
293 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
298 unformat_free (line_input);
299 return clib_error_return (0, "unknown input `%U'",
300 format_unformat_error, input);
303 unformat_free (line_input);
305 if (sw_if_index == ~0)
306 return clib_error_return (0,
307 "please specify interface name or sw_if_index");
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");
313 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
314 memif_delete_if (vm, mif);
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,
328 format_memif_if_flags (u8 * s, va_list * args)
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
338 format_memif_if_mode (u8 * s, va_list * args)
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);;
351 format_memif_queue (u8 * s, va_list * args)
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);
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);
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,
375 format_memif_descriptor (u8 * s, va_list * args)
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);
384 ring_size = 1 << mq->log2_ring_size;
388 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
390 "%Uid flags region len address offset "
392 format_white_space, indent);
394 "%U===== ===== ====== ======== ================== "
395 "========== ==================\n",
396 format_white_space, indent);
397 for (slot = 0; slot < ring_size; slot++)
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));
406 s = format (s, "\n");
412 static clib_error_t *
413 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
414 vlib_cli_command_t * cmd)
416 memif_main_t *mm = &memif_main;
418 vnet_main_t *vnm = vnet_get_main ();
423 clib_error_t *error = 0;
424 u32 hw_if_index, *hw_if_indices = 0;
429 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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"))
438 error = clib_error_return (0, "unknown input `%U'",
439 format_unformat_error, input);
444 vlib_cli_output (vm, "sockets\n");
445 vlib_cli_output (vm, " %-3s %-11s %s\n", "id", "listener", "filename");
448 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
450 memif_socket_file_t *msf;
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);
458 s = format (s, "no");
460 vlib_cli_output(vm, " %-3u %-11v %s\n", sock_id, s, filename);
461 vec_reset_length (s);
466 vlib_cli_output (vm, "\n");
468 if (vec_len (hw_if_indices) == 0)
471 pool_foreach (mif, mm->interfaces)
472 vec_add1 (hw_if_indices, mif->hw_if_index);
476 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
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));
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);
509 vec_foreach_index (i, mif->regions)
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);
515 vec_foreach_index (i, mif->tx_queues)
517 mq = vec_elt_at_index (mif->tx_queues, i);
518 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
520 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
522 vec_foreach_index (i, mif->rx_queues)
524 mq = vec_elt_at_index (mif->rx_queues, i);
525 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
527 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
532 vec_free (hw_if_indices);
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,
545 memif_cli_init (vlib_main_t * vm)
550 VLIB_INIT_FUNCTION (memif_cli_init);
553 * fd.io coding-style-patch-verification: ON
556 * eval: (c-set-style "gnu")