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>
30 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
31 vlib_cli_command_t * cmd)
33 unformat_input_t _line_input, *line_input = &_line_input;
35 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
36 memif_create_if_args_t args = { 0 };
37 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
38 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
39 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
41 /* Get a line of input. */
42 if (!unformat_user (input, unformat_line_input, line_input))
45 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
47 if (unformat (line_input, "id %u", &args.id))
49 else if (unformat (line_input, "socket %s", &args.socket_filename))
51 else if (unformat (line_input, "secret %s", &args.secret))
53 else if (unformat (line_input, "ring-size %u", &ring_size))
55 else if (unformat (line_input, "rx-queues %u", &rx_queues))
57 else if (unformat (line_input, "tx-queues %u", &tx_queues))
59 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
61 else if (unformat (line_input, "master"))
63 else if (unformat (line_input, "slave"))
65 else if (unformat (line_input, "mode ip"))
66 args.mode = MEMIF_INTERFACE_MODE_IP;
67 else if (unformat (line_input, "hw-addr %U",
68 unformat_ethernet_address, args.hw_addr))
71 return clib_error_return (0, "unknown input `%U'",
72 format_unformat_error, input);
74 unformat_free (line_input);
76 if (!is_pow2 (ring_size))
77 return clib_error_return (0, "ring size must be power of 2");
79 args.log2_ring_size = min_log2 (ring_size);
81 if (rx_queues > 255 || rx_queues < 1)
82 return clib_error_return (0, "rx queue must be between 1 - 255");
83 if (tx_queues > 255 || tx_queues < 1)
84 return clib_error_return (0, "tx queue must be between 1 - 255");
86 args.rx_queues = rx_queues;
87 args.tx_queues = tx_queues;
89 r = memif_create_if (vm, &args);
91 vec_free (args.socket_filename);
92 vec_free (args.secret);
94 if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
95 && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
96 return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
98 if (r == VNET_API_ERROR_INVALID_INTERFACE)
99 return clib_error_return (0, "Invalid interface name");
101 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
102 return clib_error_return (0, "Interface with same id already exists");
108 VLIB_CLI_COMMAND (memif_create_command, static) = {
109 .path = "create memif",
110 .short_help = "create memif [id <id>] [socket <path>] "
111 "[ring-size <size>] [buffer-size <size>] [hw-addr <mac-address>] "
112 "<master|slave> [rx-queues <number>] [tx-queues <number>]"
114 .function = memif_create_command_fn,
118 static clib_error_t *
119 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
120 vlib_cli_command_t * cmd)
122 unformat_input_t _line_input, *line_input = &_line_input;
123 u32 sw_if_index = ~0;
124 vnet_hw_interface_t *hw;
125 memif_main_t *mm = &memif_main;
127 vnet_main_t *vnm = vnet_get_main ();
129 /* Get a line of input. */
130 if (!unformat_user (input, unformat_line_input, line_input))
133 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
135 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
137 else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
141 return clib_error_return (0, "unknown input `%U'",
142 format_unformat_error, input);
144 unformat_free (line_input);
146 if (sw_if_index == ~0)
147 return clib_error_return (0,
148 "please specify interface name or sw_if_index");
150 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
151 if (hw == NULL || memif_device_class.index != hw->dev_class_index)
152 return clib_error_return (0, "not a memif interface");
154 mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
155 memif_delete_if (vm, mif);
161 VLIB_CLI_COMMAND (memif_delete_command, static) = {
162 .path = "delete memif",
163 .short_help = "delete memif {<interface> | sw_if_index <sw_idx>}",
164 .function = memif_delete_command_fn,
169 format_memif_if_flags (u8 * s, va_list * args)
171 u32 flags = va_arg (*args, u32);
172 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
173 foreach_memif_if_flag
179 format_memif_if_mode (u8 * s, va_list * args)
181 memif_if_t *mif = va_arg (*args, memif_if_t *);
182 if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
183 return format (s, "ethernet");
184 if (mif->mode == MEMIF_INTERFACE_MODE_IP)
185 return format (s, "ip");
186 if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
187 return format (s, "punt-inject");
188 return format (s, "unknown mode (%u)", mif->mode);;
192 format_memif_queue (u8 * s, va_list * args)
194 memif_if_t *mif = va_arg (*args, memif_if_t *);
195 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
196 uword i = va_arg (*args, uword);
197 uword indent = format_get_indent (s);
199 s = format (s, "%U%s ring %u:\n",
200 format_white_space, indent,
201 (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ?
202 "slave-to-master" : "master-to-slave", i);
203 s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
204 format_white_space, indent + 4,
205 mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
208 s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
209 format_white_space, indent + 4,
210 mq->ring->head, mq->ring->tail, mq->ring->flags,
217 format_memif_descriptor (u8 * s, va_list * args)
219 memif_if_t *mif = va_arg (*args, memif_if_t *);
220 memif_queue_t *mq = va_arg (*args, memif_queue_t *);
221 uword indent = format_get_indent (s);
226 ring_size = 1 << mq->log2_ring_size;
230 s = format (s, "%Udescriptor table:\n", format_white_space, indent);
233 "%Uid flags buf len desc len address offset user address\n",
234 format_white_space, indent);
237 "%U===== ===== ======= ======== ================== ====== ==================\n",
238 format_white_space, indent);
239 for (slot = 0; slot < ring_size; slot++)
241 s = format (s, "%U%-5d %-5d %-7d %-7d 0x%016lx %-6d 0x%016lx\n",
242 format_white_space, indent, slot,
243 ring->desc[slot].flags, ring->desc[slot].buffer_length,
244 ring->desc[slot].length,
245 mif->regions[ring->desc[slot].region].shm,
246 ring->desc[slot].offset, memif_get_buffer (mif, ring,
249 s = format (s, "\n");
255 static clib_error_t *
256 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
257 vlib_cli_command_t * cmd)
259 memif_main_t *mm = &memif_main;
261 vnet_main_t *vnm = vnet_get_main ();
265 clib_error_t *error = 0;
266 u32 hw_if_index, *hw_if_indices = 0;
268 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
271 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
272 vec_add1 (hw_if_indices, hw_if_index);
273 else if (unformat (input, "descriptors"))
277 error = clib_error_return (0, "unknown input `%U'",
278 format_unformat_error, input);
283 if (vec_len (hw_if_indices) == 0)
286 pool_foreach (mif, mm->interfaces,
287 vec_add1 (hw_if_indices, mif->hw_if_index);
292 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
294 vnet_hw_interface_t *hi =
295 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
296 mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
297 memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
298 mif->socket_file_index);
299 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
300 vnm, mif->sw_if_index);
301 if (mif->remote_name)
302 vlib_cli_output (vm, " remote-name \"%s\"", mif->remote_name);
303 if (mif->remote_if_name)
304 vlib_cli_output (vm, " remote-interface \"%s\"",
305 mif->remote_if_name);
306 vlib_cli_output (vm, " id %d mode %U file %s", mif->id,
307 format_memif_if_mode, mif, msf->filename);
308 vlib_cli_output (vm, " flags%U", format_memif_if_flags, mif->flags);
309 vlib_cli_output (vm, " listener-fd %d conn-fd %d", msf->fd,
312 " num-s2m-rings %u num-m2s-rings %u buffer-size %u",
313 mif->run.num_s2m_rings, mif->run.num_m2s_rings,
314 mif->run.buffer_size);
316 if (mif->local_disc_string)
317 vlib_cli_output (vm, " local-disc-reason \"%s\"",
318 mif->local_disc_string);
319 if (mif->remote_disc_string)
320 vlib_cli_output (vm, " remote-disc-reason \"%s\"",
321 mif->remote_disc_string);
323 vec_foreach_index (i, mif->tx_queues)
325 mq = vec_elt_at_index (mif->tx_queues, i);
326 vlib_cli_output (vm, " %U", format_memif_queue, mif, mq, i);
328 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
330 vec_foreach_index (i, mif->rx_queues)
332 mq = vec_elt_at_index (mif->rx_queues, i);
333 vlib_cli_output (vm, " %U", format_memif_queue, mif, mq, i);
335 vlib_cli_output (vm, " %U", format_memif_descriptor, mif, mq);
339 vec_free (hw_if_indices);
344 VLIB_CLI_COMMAND (memif_show_command, static) = {
345 .path = "show memif",
346 .short_help = "show memif {<interface>] [descriptors]",
347 .function = memif_show_command_fn,
352 memif_cli_init (vlib_main_t * vm)
357 VLIB_INIT_FUNCTION (memif_cli_init);
360 * fd.io coding-style-patch-verification: ON
363 * eval: (c-set-style "gnu")