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);
83 VLIB_CLI_COMMAND (memif_socket_filename_create_command, static) = {
84 .path = "create memif socket",
85 .short_help = "create memif socket [id <id>] [filename <path>]",
86 .function = memif_socket_filename_create_command_fn,
90 memif_socket_filename_delete_command_fn (vlib_main_t * vm,
91 unformat_input_t * input,
92 vlib_cli_command_t * cmd)
94 unformat_input_t _line_input, *line_input = &_line_input;
97 /* Get a line of input. */
98 if (!unformat_user (input, unformat_line_input, line_input))
103 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
105 if (unformat (line_input, "id %u", &socket_id))
109 unformat_free (line_input);
110 return clib_error_return (0, "unknown input `%U'",
111 format_unformat_error, input);
115 unformat_free (line_input);
117 if (socket_id == 0 || socket_id == ~0)
119 return clib_error_return (0, "Invalid socket id");
122 return memif_socket_filename_add_del (0, socket_id, 0);
125 VLIB_CLI_COMMAND (memif_socket_filename_delete_command, static) = {
126 .path = "delete memif socket",
127 .short_help = "delete memif socket [id <id>]",
128 .function = memif_socket_filename_delete_command_fn,
131 static clib_error_t *
132 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
133 vlib_cli_command_t * cmd)
135 unformat_input_t _line_input, *line_input = &_line_input;
137 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
138 memif_create_if_args_t args = { 0 };
139 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
140 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
141 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
143 /* Get a line of input. */
144 if (!unformat_user (input, unformat_line_input, line_input))
147 args.is_zero_copy = 1;
149 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
151 if (unformat (line_input, "id %u", &args.id))
153 else if (unformat (line_input, "socket-id %u", &args.socket_id))
155 else if (unformat (line_input, "secret %s", &args.secret))
157 else if (unformat (line_input, "ring-size %u", &ring_size))
159 else if (unformat (line_input, "rx-queues %u", &rx_queues))
161 else if (unformat (line_input, "tx-queues %u", &tx_queues))
163 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
165 else if (unformat (line_input, "master"))
167 else if (unformat (line_input, "slave"))
169 else if (unformat (line_input, "no-zero-copy"))
170 args.is_zero_copy = 0;
171 else if (unformat (line_input, "use-dma"))
173 else if (unformat (line_input, "mode ip"))
174 args.mode = MEMIF_INTERFACE_MODE_IP;
175 else if (unformat (line_input, "hw-addr %U",
176 unformat_ethernet_address, args.hw_addr))
177 args.hw_addr_set = 1;
180 unformat_free (line_input);
181 return clib_error_return (0, "unknown input `%U'",
182 format_unformat_error, input);
185 unformat_free (line_input);
187 if (!is_pow2 (ring_size))
188 return clib_error_return (0, "ring size must be power of 2");
190 if (ring_size > 32768)
191 return clib_error_return (0, "maximum ring size is 32768");
193 args.log2_ring_size = min_log2 (ring_size);
195 if (rx_queues > 255 || rx_queues < 1)
196 return clib_error_return (0, "rx queue must be between 1 - 255");
197 if (tx_queues > 255 || tx_queues < 1)
198 return clib_error_return (0, "tx queue must be between 1 - 255");
200 args.rx_queues = rx_queues;
201 args.tx_queues = tx_queues;
203 err = memif_create_if (vm, &args);
205 vec_free (args.secret);
210 VLIB_CLI_COMMAND (memif_create_command, static) = {
211 .path = "create interface memif",
212 .short_help = "create interface memif [id <id>] [socket-id <socket-id>] "
213 "[ring-size <size>] [buffer-size <size>] "
214 "[hw-addr <mac-address>] "
215 "<master|slave> [rx-queues <number>] [tx-queues <number>] "
216 "[mode ip] [secret <string>]",
217 .function = memif_create_command_fn,
220 static clib_error_t *
221 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
222 vlib_cli_command_t * cmd)
224 unformat_input_t _line_input, *line_input = &_line_input;
225 u32 sw_if_index = ~0;
226 vnet_hw_interface_t *hw;
227 memif_main_t *mm = &memif_main;
229 vnet_main_t *vnm = vnet_get_main ();
231 /* Get a line of input. */
232 if (!unformat_user (input, unformat_line_input, line_input))
235 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
237 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
239 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
244 unformat_free (line_input);
245 return clib_error_return (0, "unknown input `%U'",
246 format_unformat_error, input);
249 unformat_free (line_input);
251 if (sw_if_index == ~0)
252 return clib_error_return (0,
253 "please specify interface name or sw_if_index");
255 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
256 if (hw == NULL || memif_device_class.index != hw->dev_class_index)
257 return clib_error_return (0, "not a memif interface");
259 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
260 memif_delete_if (vm, mif);
265 VLIB_CLI_COMMAND (memif_delete_command, static) = {
266 .path = "delete interface memif",
267 .short_help = "delete interface memif {<interface> | sw_if_index <sw_idx>}",
268 .function = memif_delete_command_fn,
272 format_memif_if_flags (u8 * s, va_list * args)
274 u32 flags = va_arg (*args, u32);
275 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
276 foreach_memif_if_flag
282 format_memif_if_mode (u8 * s, va_list * args)
284 memif_if_t *mif = va_arg (*args, memif_if_t *);
285 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
286 return format (s, "ethernet");
287 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
288 return format (s, "ip");
289 if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
290 return format (s, "punt-inject");
291 return format (s, "unknown mode (%u)", mif->mode);;
295 format_memif_queue (u8 * s, va_list * args)
297 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
298 uword i = va_arg (*args, uword);
299 u32 indent = format_get_indent (s);
301 s = format (s, "%U%s ring %u:\n",
302 format_white_space, indent,
303 (mq->type == MEMIF_RING_S2M) ?
304 "slave-to-master" : "master-to-slave", i);
305 s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
306 format_white_space, indent + 4,
307 mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
310 s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
311 format_white_space, indent + 4,
312 mq->ring->head, mq->ring->tail, mq->ring->flags,
319 format_memif_descriptor (u8 * s, va_list * args)
321 memif_if_t *mif = va_arg (*args, memif_if_t *);
322 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
323 u32 indent = format_get_indent (s);
328 ring_size = 1 << mq->log2_ring_size;
332 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
334 "%Uid flags region len address offset "
336 format_white_space, indent);
338 "%U===== ===== ====== ======== ================== "
339 "========== ==================\n",
340 format_white_space, indent);
341 for (slot = 0; slot < ring_size; slot++)
343 s = format (s, "%U%-5d %-5d %-6d %-7d 0x%016lx %-10d 0x%016lx\n",
344 format_white_space, indent, slot, ring->desc[slot].flags,
345 ring->desc[slot].region, ring->desc[slot].length,
346 mif->regions[ring->desc[slot].region].shm,
347 ring->desc[slot].offset,
348 memif_get_buffer (mif, ring, slot));
350 s = format (s, "\n");
356 static clib_error_t *
357 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
358 vlib_cli_command_t * cmd)
360 memif_main_t *mm = &memif_main;
362 vnet_main_t *vnm = vnet_get_main ();
367 clib_error_t *error = 0;
368 u32 hw_if_index, *hw_if_indices = 0;
373 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
376 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
377 vec_add1 (hw_if_indices, hw_if_index);
378 else if (unformat (input, "descriptors"))
382 error = clib_error_return (0, "unknown input `%U'",
383 format_unformat_error, input);
388 vlib_cli_output (vm, "sockets\n");
389 vlib_cli_output (vm, " %-3s %-11s %s\n", "id", "listener", "filename");
391 hash_foreach (sock_id, msf_idx, mm->socket_file_index_by_sock_id,
393 memif_socket_file_t *msf;
396 msf = pool_elt_at_index(mm->socket_files, msf_idx);
397 filename = msf->filename;
398 if (msf->is_listener)
399 s = format (s, "yes (%u)", msf->ref_cnt);
401 s = format (s, "no");
403 vlib_cli_output(vm, " %-3u %-11v %s\n", sock_id, s, filename);
404 vec_reset_length (s);
408 vlib_cli_output (vm, "\n");
410 if (vec_len (hw_if_indices) == 0)
412 pool_foreach (mif, mm->interfaces)
413 vec_add1 (hw_if_indices, mif->hw_if_index);
416 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
418 vnet_hw_interface_t *hi =
419 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
420 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
421 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
422 mif->socket_file_index);
423 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
424 vnm, mif->sw_if_index);
425 if (mif->remote_name)
426 vlib_cli_output (vm, " remote-name \"%s\"", mif->remote_name);
427 if (mif->remote_if_name)
428 vlib_cli_output (vm, " remote-interface \"%s\"",
429 mif->remote_if_name);
430 vlib_cli_output (vm, " socket-id %u id %u mode %U", msf->socket_id,
431 mif->id, format_memif_if_mode, mif);
432 vlib_cli_output (vm, " flags%U", format_memif_if_flags, mif->flags);
433 vlib_cli_output (vm, " listener-fd %d conn-fd %d",
434 msf->sock ? msf->sock->fd : 0,
435 mif->sock ? mif->sock->fd : 0);
436 vlib_cli_output (vm, " num-s2m-rings %u num-m2s-rings %u "
437 "buffer-size %u num-regions %u",
438 mif->run.num_s2m_rings, mif->run.num_m2s_rings,
439 mif->run.buffer_size, vec_len (mif->regions));
441 if (mif->local_disc_string)
442 vlib_cli_output (vm, " local-disc-reason \"%s\"",
443 mif->local_disc_string);
444 if (mif->remote_disc_string)
445 vlib_cli_output (vm, " remote-disc-reason \"%s\"",
446 mif->remote_disc_string);
448 vec_foreach_index (i, mif->regions)
450 mr = vec_elt_at_index (mif->regions, i);
451 vlib_cli_output (vm, " region %u size %u fd %d", i,
452 mr->region_size, mr->fd);
454 vec_foreach_index (i, mif->tx_queues)
456 mq = vec_elt_at_index (mif->tx_queues, i);
457 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
459 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
461 vec_foreach_index (i, mif->rx_queues)
463 mq = vec_elt_at_index (mif->rx_queues, i);
464 vlib_cli_output (vm, " %U", format_memif_queue, mq, i);
466 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
470 vec_free (hw_if_indices);
474 VLIB_CLI_COMMAND (memif_show_command, static) = {
475 .path = "show memif",
476 .short_help = "show memif [<interface>] [descriptors]",
477 .function = memif_show_command_fn,
481 memif_cli_init (vlib_main_t * vm)
486 VLIB_INIT_FUNCTION (memif_cli_init);
489 * fd.io coding-style-patch-verification: ON
492 * eval: (c-set-style "gnu")