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 args.is_zero_copy = 1;
188 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
190 if (unformat (line_input, "id %u", &args.id))
192 else if (unformat (line_input, "socket-id %u", &args.socket_id))
194 else if (unformat (line_input, "secret %s", &args.secret))
196 else if (unformat (line_input, "ring-size %u", &ring_size))
198 else if (unformat (line_input, "rx-queues %u", &rx_queues))
200 else if (unformat (line_input, "tx-queues %u", &tx_queues))
202 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
204 else if (unformat (line_input, "master"))
206 else if (unformat (line_input, "slave"))
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;
216 return clib_error_return (0, "unknown input `%U'",
217 format_unformat_error, input);
219 unformat_free (line_input);
221 if (!is_pow2 (ring_size))
222 return clib_error_return (0, "ring size must be power of 2");
224 if (ring_size > 32768)
225 return clib_error_return (0, "maximum ring size is 32768");
227 args.log2_ring_size = min_log2 (ring_size);
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");
234 args.rx_queues = rx_queues;
235 args.tx_queues = tx_queues;
237 r = memif_create_if (vm, &args);
239 vec_free (args.secret);
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);
245 if (r == VNET_API_ERROR_INVALID_ARGUMENT)
246 return clib_error_return (0, "Invalid argument");
248 if (r == VNET_API_ERROR_INVALID_INTERFACE)
249 return clib_error_return (0, "Invalid interface name");
251 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
252 return clib_error_return (0, "Interface with same id already exists");
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,
269 static clib_error_t *
270 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
271 vlib_cli_command_t * cmd)
273 unformat_input_t _line_input, *line_input = &_line_input;
274 u32 sw_if_index = ~0;
275 vnet_hw_interface_t *hw;
276 memif_main_t *mm = &memif_main;
278 vnet_main_t *vnm = vnet_get_main ();
280 /* Get a line of input. */
281 if (!unformat_user (input, unformat_line_input, line_input))
284 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
286 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
288 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
292 return clib_error_return (0, "unknown input `%U'",
293 format_unformat_error, input);
295 unformat_free (line_input);
297 if (sw_if_index == ~0)
298 return clib_error_return (0,
299 "please specify interface name or sw_if_index");
301 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
302 if (hw == NULL || memif_device_class.index != hw->dev_class_index)
303 return clib_error_return (0, "not a memif interface");
305 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
306 memif_delete_if (vm, mif);
312 VLIB_CLI_COMMAND (memif_delete_command, static) = {
313 .path = "delete interface memif",
314 .short_help = "delete interface memif {<interface> | sw_if_index <sw_idx>}",
315 .function = memif_delete_command_fn,
320 format_memif_if_flags (u8 * s, va_list * args)
322 u32 flags = va_arg (*args, u32);
323 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
324 foreach_memif_if_flag
330 format_memif_if_mode (u8 * s, va_list * args)
332 memif_if_t *mif = va_arg (*args, memif_if_t *);
333 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
334 return format (s, "ethernet");
335 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
336 return format (s, "ip");
337 if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
338 return format (s, "punt-inject");
339 return format (s, "unknown mode (%u)", mif->mode);;
343 format_memif_queue (u8 * s, va_list * args)
345 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
346 uword i = va_arg (*args, uword);
347 u32 indent = format_get_indent (s);
349 s = format (s, "%U%s ring %u:\n",
350 format_white_space, indent,
351 (mq->type == MEMIF_RING_S2M) ?
352 "slave-to-master" : "master-to-slave", i);
353 s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
354 format_white_space, indent + 4,
355 mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
358 s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
359 format_white_space, indent + 4,
360 mq->ring->head, mq->ring->tail, mq->ring->flags,
367 format_memif_descriptor (u8 * s, va_list * args)
369 memif_if_t *mif = va_arg (*args, memif_if_t *);
370 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
371 u32 indent = format_get_indent (s);
376 ring_size = 1 << mq->log2_ring_size;
380 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
383 "%Uid flags len address offset user address\n",
384 format_white_space, indent);
387 "%U===== ===== ======== ================== ====== ==================\n",
388 format_white_space, indent);
389 for (slot = 0; slot < ring_size; slot++)
391 s = format (s, "%U%-5d %-5d %-7d 0x%016lx %-6d 0x%016lx\n",
392 format_white_space, indent, slot,
393 ring->desc[slot].flags,
394 ring->desc[slot].length,
395 mif->regions[ring->desc[slot].region].shm,
396 ring->desc[slot].offset, memif_get_buffer (mif, ring,
399 s = format (s, "\n");
405 static clib_error_t *
406 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
407 vlib_cli_command_t * cmd)
409 memif_main_t *mm = &memif_main;
411 vnet_main_t *vnm = vnet_get_main ();
416 clib_error_t *error = 0;
417 u32 hw_if_index, *hw_if_indices = 0;
422 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
425 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
426 vec_add1 (hw_if_indices, hw_if_index);
427 else if (unformat (input, "descriptors"))
431 error = clib_error_return (0, "unknown input `%U'",
432 format_unformat_error, input);
437 vlib_cli_output (vm, "sockets\n");
438 vlib_cli_output (vm, " %-3s %-11s %s\n", "id", "listener", "filename");
441 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
443 memif_socket_file_t *msf;
446 msf = pool_elt_at_index(mm->socket_files, msf_idx);
447 filename = msf->filename;
448 if (msf->is_listener)
449 s = format (s, "yes (%u)", msf->ref_cnt);
451 s = format (s, "no");
453 vlib_cli_output(vm, " %-3u %-11v %s\n", sock_id, s, filename);
454 vec_reset_length (s);
459 vlib_cli_output (vm, "\n");
461 if (vec_len (hw_if_indices) == 0)
464 pool_foreach (mif, mm->interfaces,
465 vec_add1 (hw_if_indices, mif->hw_if_index);
470 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
472 vnet_hw_interface_t *hi =
473 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
474 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
475 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
476 mif->socket_file_index);
477 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
478 vnm, mif->sw_if_index);
479 if (mif->remote_name)
480 vlib_cli_output (vm, " remote-name \"%s\"", mif->remote_name);
481 if (mif->remote_if_name)
482 vlib_cli_output (vm, " remote-interface \"%s\"",
483 mif->remote_if_name);
484 vlib_cli_output (vm, " socket-id %u id %u mode %U", msf->socket_id,
485 mif->id, format_memif_if_mode, mif);
486 vlib_cli_output (vm, " flags%U", format_memif_if_flags, mif->flags);
487 vlib_cli_output (vm, " listener-fd %d conn-fd %d",
488 msf->sock ? msf->sock->fd : 0,
489 mif->sock ? mif->sock->fd : 0);
490 vlib_cli_output (vm, " num-s2m-rings %u num-m2s-rings %u "
491 "buffer-size %u num-regions %u",
492 mif->run.num_s2m_rings, mif->run.num_m2s_rings,
493 mif->run.buffer_size, vec_len (mif->regions));
495 if (mif->local_disc_string)
496 vlib_cli_output (vm, " local-disc-reason \"%s\"",
497 mif->local_disc_string);
498 if (mif->remote_disc_string)
499 vlib_cli_output (vm, " remote-disc-reason \"%s\"",
500 mif->remote_disc_string);
503 vec_foreach_index (i, mif->regions)
505 mr = vec_elt_at_index (mif->regions, i);
506 vlib_cli_output (vm, " region %u size %u fd %d", i,
507 mr->region_size, mr->fd);
509 vec_foreach_index (i, mif->tx_queues)
511 mq = vec_elt_at_index (mif->tx_queues, i);
512 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
514 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
516 vec_foreach_index (i, mif->rx_queues)
518 mq = vec_elt_at_index (mif->rx_queues, i);
519 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
521 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
526 vec_free (hw_if_indices);
531 VLIB_CLI_COMMAND (memif_show_command, static) = {
532 .path = "show memif",
533 .short_help = "show memif [<interface>] [descriptors]",
534 .function = memif_show_command_fn,
539 memif_cli_init (vlib_main_t * vm)
544 VLIB_INIT_FUNCTION (memif_cli_init);
547 * fd.io coding-style-patch-verification: ON
550 * eval: (c-set-style "gnu")