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 err = memif_socket_filename_add_del (1, socket_id, (char *) socket_filename);
78 vec_free (socket_filename);
84 VLIB_CLI_COMMAND (memif_socket_filename_create_command, static) = {
85 .path = "create memif socket",
86 .short_help = "create memif socket [id <id>] [filename <path>]",
87 .function = memif_socket_filename_create_command_fn,
92 memif_socket_filename_delete_command_fn (vlib_main_t * vm,
93 unformat_input_t * input,
94 vlib_cli_command_t * cmd)
96 unformat_input_t _line_input, *line_input = &_line_input;
99 /* Get a line of input. */
100 if (!unformat_user (input, unformat_line_input, line_input))
105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
107 if (unformat (line_input, "id %u", &socket_id))
111 unformat_free (line_input);
112 return clib_error_return (0, "unknown input `%U'",
113 format_unformat_error, input);
117 unformat_free (line_input);
119 if (socket_id == 0 || socket_id == ~0)
121 return clib_error_return (0, "Invalid socket id");
124 return memif_socket_filename_add_del (0, socket_id, 0);
128 VLIB_CLI_COMMAND (memif_socket_filename_delete_command, static) = {
129 .path = "delete memif socket",
130 .short_help = "delete memif socket [id <id>]",
131 .function = memif_socket_filename_delete_command_fn,
135 static clib_error_t *
136 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
137 vlib_cli_command_t * cmd)
139 unformat_input_t _line_input, *line_input = &_line_input;
141 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
142 memif_create_if_args_t args = { 0 };
143 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
144 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
145 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
147 /* Get a line of input. */
148 if (!unformat_user (input, unformat_line_input, line_input))
151 args.is_zero_copy = 1;
153 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
155 if (unformat (line_input, "id %u", &args.id))
157 else if (unformat (line_input, "socket-id %u", &args.socket_id))
159 else if (unformat (line_input, "secret %s", &args.secret))
161 else if (unformat (line_input, "ring-size %u", &ring_size))
163 else if (unformat (line_input, "rx-queues %u", &rx_queues))
165 else if (unformat (line_input, "tx-queues %u", &tx_queues))
167 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
169 else if (unformat (line_input, "master"))
171 else if (unformat (line_input, "slave"))
173 else if (unformat (line_input, "no-zero-copy"))
174 args.is_zero_copy = 0;
175 else if (unformat (line_input, "mode ip"))
176 args.mode = MEMIF_INTERFACE_MODE_IP;
177 else if (unformat (line_input, "hw-addr %U",
178 unformat_ethernet_address, args.hw_addr))
179 args.hw_addr_set = 1;
182 unformat_free (line_input);
183 return clib_error_return (0, "unknown input `%U'",
184 format_unformat_error, input);
187 unformat_free (line_input);
189 if (!is_pow2 (ring_size))
190 return clib_error_return (0, "ring size must be power of 2");
192 if (ring_size > 32768)
193 return clib_error_return (0, "maximum ring size is 32768");
195 args.log2_ring_size = min_log2 (ring_size);
197 if (rx_queues > 255 || rx_queues < 1)
198 return clib_error_return (0, "rx queue must be between 1 - 255");
199 if (tx_queues > 255 || tx_queues < 1)
200 return clib_error_return (0, "tx queue must be between 1 - 255");
202 args.rx_queues = rx_queues;
203 args.tx_queues = tx_queues;
205 err = memif_create_if (vm, &args);
207 vec_free (args.secret);
213 VLIB_CLI_COMMAND (memif_create_command, static) = {
214 .path = "create interface memif",
215 .short_help = "create interface memif [id <id>] [socket-id <socket-id>] "
216 "[ring-size <size>] [buffer-size <size>] "
217 "[hw-addr <mac-address>] "
218 "<master|slave> [rx-queues <number>] [tx-queues <number>] "
219 "[mode ip] [secret <string>]",
220 .function = memif_create_command_fn,
224 static clib_error_t *
225 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
226 vlib_cli_command_t * cmd)
228 unformat_input_t _line_input, *line_input = &_line_input;
229 u32 sw_if_index = ~0;
230 vnet_hw_interface_t *hw;
231 memif_main_t *mm = &memif_main;
233 vnet_main_t *vnm = vnet_get_main ();
235 /* Get a line of input. */
236 if (!unformat_user (input, unformat_line_input, line_input))
239 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
243 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
248 unformat_free (line_input);
249 return clib_error_return (0, "unknown input `%U'",
250 format_unformat_error, input);
253 unformat_free (line_input);
255 if (sw_if_index == ~0)
256 return clib_error_return (0,
257 "please specify interface name or sw_if_index");
259 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
260 if (hw == NULL || memif_device_class.index != hw->dev_class_index)
261 return clib_error_return (0, "not a memif interface");
263 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
264 memif_delete_if (vm, mif);
270 VLIB_CLI_COMMAND (memif_delete_command, static) = {
271 .path = "delete interface memif",
272 .short_help = "delete interface memif {<interface> | sw_if_index <sw_idx>}",
273 .function = memif_delete_command_fn,
278 format_memif_if_flags (u8 * s, va_list * args)
280 u32 flags = va_arg (*args, u32);
281 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
282 foreach_memif_if_flag
288 format_memif_if_mode (u8 * s, va_list * args)
290 memif_if_t *mif = va_arg (*args, memif_if_t *);
291 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
292 return format (s, "ethernet");
293 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
294 return format (s, "ip");
295 if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
296 return format (s, "punt-inject");
297 return format (s, "unknown mode (%u)", mif->mode);;
301 format_memif_queue (u8 * s, va_list * args)
303 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
304 uword i = va_arg (*args, uword);
305 u32 indent = format_get_indent (s);
307 s = format (s, "%U%s ring %u:\n",
308 format_white_space, indent,
309 (mq->type == MEMIF_RING_S2M) ?
310 "slave-to-master" : "master-to-slave", i);
311 s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
312 format_white_space, indent + 4,
313 mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
316 s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
317 format_white_space, indent + 4,
318 mq->ring->head, mq->ring->tail, mq->ring->flags,
325 format_memif_descriptor (u8 * s, va_list * args)
327 memif_if_t *mif = va_arg (*args, memif_if_t *);
328 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
329 u32 indent = format_get_indent (s);
334 ring_size = 1 << mq->log2_ring_size;
338 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
340 "%Uid flags region len address offset "
342 format_white_space, indent);
344 "%U===== ===== ====== ======== ================== "
345 "========== ==================\n",
346 format_white_space, indent);
347 for (slot = 0; slot < ring_size; slot++)
349 s = format (s, "%U%-5d %-5d %-6d %-7d 0x%016lx %-10d 0x%016lx\n",
350 format_white_space, indent, slot, ring->desc[slot].flags,
351 ring->desc[slot].region, ring->desc[slot].length,
352 mif->regions[ring->desc[slot].region].shm,
353 ring->desc[slot].offset,
354 memif_get_buffer (mif, ring, slot));
356 s = format (s, "\n");
362 static clib_error_t *
363 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
364 vlib_cli_command_t * cmd)
366 memif_main_t *mm = &memif_main;
368 vnet_main_t *vnm = vnet_get_main ();
373 clib_error_t *error = 0;
374 u32 hw_if_index, *hw_if_indices = 0;
379 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
382 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
383 vec_add1 (hw_if_indices, hw_if_index);
384 else if (unformat (input, "descriptors"))
388 error = clib_error_return (0, "unknown input `%U'",
389 format_unformat_error, input);
394 vlib_cli_output (vm, "sockets\n");
395 vlib_cli_output (vm, " %-3s %-11s %s\n", "id", "listener", "filename");
398 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
400 memif_socket_file_t *msf;
403 msf = pool_elt_at_index(mm->socket_files, msf_idx);
404 filename = msf->filename;
405 if (msf->is_listener)
406 s = format (s, "yes (%u)", msf->ref_cnt);
408 s = format (s, "no");
410 vlib_cli_output(vm, " %-3u %-11v %s\n", sock_id, s, filename);
411 vec_reset_length (s);
416 vlib_cli_output (vm, "\n");
418 if (vec_len (hw_if_indices) == 0)
421 pool_foreach (mif, mm->interfaces)
422 vec_add1 (hw_if_indices, mif->hw_if_index);
426 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
428 vnet_hw_interface_t *hi =
429 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
430 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
431 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
432 mif->socket_file_index);
433 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
434 vnm, mif->sw_if_index);
435 if (mif->remote_name)
436 vlib_cli_output (vm, " remote-name \"%s\"", mif->remote_name);
437 if (mif->remote_if_name)
438 vlib_cli_output (vm, " remote-interface \"%s\"",
439 mif->remote_if_name);
440 vlib_cli_output (vm, " socket-id %u id %u mode %U", msf->socket_id,
441 mif->id, format_memif_if_mode, mif);
442 vlib_cli_output (vm, " flags%U", format_memif_if_flags, mif->flags);
443 vlib_cli_output (vm, " listener-fd %d conn-fd %d",
444 msf->sock ? msf->sock->fd : 0,
445 mif->sock ? mif->sock->fd : 0);
446 vlib_cli_output (vm, " num-s2m-rings %u num-m2s-rings %u "
447 "buffer-size %u num-regions %u",
448 mif->run.num_s2m_rings, mif->run.num_m2s_rings,
449 mif->run.buffer_size, vec_len (mif->regions));
451 if (mif->local_disc_string)
452 vlib_cli_output (vm, " local-disc-reason \"%s\"",
453 mif->local_disc_string);
454 if (mif->remote_disc_string)
455 vlib_cli_output (vm, " remote-disc-reason \"%s\"",
456 mif->remote_disc_string);
459 vec_foreach_index (i, mif->regions)
461 mr = vec_elt_at_index (mif->regions, i);
462 vlib_cli_output (vm, " region %u size %u fd %d", i,
463 mr->region_size, mr->fd);
465 vec_foreach_index (i, mif->tx_queues)
467 mq = vec_elt_at_index (mif->tx_queues, i);
468 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
470 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
472 vec_foreach_index (i, mif->rx_queues)
474 mq = vec_elt_at_index (mif->rx_queues, i);
475 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
477 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
482 vec_free (hw_if_indices);
487 VLIB_CLI_COMMAND (memif_show_command, static) = {
488 .path = "show memif",
489 .short_help = "show memif [<interface>] [descriptors]",
490 .function = memif_show_command_fn,
495 memif_cli_init (vlib_main_t * vm)
500 VLIB_INIT_FUNCTION (memif_cli_init);
503 * fd.io coding-style-patch-verification: ON
506 * eval: (c-set-style "gnu")