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>
29 unformat_memif_queues (unformat_input_t * input, va_list * args)
31 u32 *rx_queues = va_arg (*args, u32 *);
32 u32 *tx_queues = va_arg (*args, u32 *);
34 if (unformat (input, "rx-queues %u", rx_queues))
36 if (unformat (input, "tx-queues %u", tx_queues))
43 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44 vlib_cli_command_t * cmd)
46 unformat_input_t _line_input, *line_input = &_line_input;
48 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
49 memif_create_if_args_t args = { 0 };
50 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
51 u32 rx_queues = MEMIF_DEFAULT_RX_QUEUES;
52 u32 tx_queues = MEMIF_DEFAULT_TX_QUEUES;
54 /* Get a line of input. */
55 if (!unformat_user (input, unformat_line_input, line_input))
58 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
60 if (unformat (line_input, "key 0x%" PRIx64, &args.key))
62 else if (unformat (line_input, "socket %s", &args.socket_filename))
64 else if (unformat (line_input, "ring-size %u", &ring_size))
66 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
68 else if (unformat (line_input, "master"))
70 else if (unformat (line_input, "slave %U",
71 unformat_memif_queues, &rx_queues, &tx_queues))
73 else if (unformat (line_input, "hw-addr %U",
74 unformat_ethernet_address, args.hw_addr))
77 return clib_error_return (0, "unknown input `%U'",
78 format_unformat_error, input);
80 unformat_free (line_input);
82 if (!is_pow2 (ring_size))
83 return clib_error_return (0, "ring size must be power of 2");
85 args.log2_ring_size = min_log2 (ring_size);
87 if (rx_queues > 255 || rx_queues < 1)
88 return clib_error_return (0, "rx queue must be between 1 - 255");
89 if (tx_queues > 255 || tx_queues < 1)
90 return clib_error_return (0, "tx queue must be between 1 - 255");
92 args.rx_queues = rx_queues;
93 args.tx_queues = tx_queues;
95 r = memif_create_if (vm, &args);
97 if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
98 && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
99 return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
101 if (r == VNET_API_ERROR_INVALID_INTERFACE)
102 return clib_error_return (0, "Invalid interface name");
104 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
105 return clib_error_return (0, "Interface already exists");
111 VLIB_CLI_COMMAND (memif_create_command, static) = {
112 .path = "create memif",
113 .short_help = "create memif [key <key>] [socket <path>] "
114 "[ring-size <size>] [buffer-size <size>] [hw-addr <mac-address>] "
115 "<master|slave [rx-queues <number>] [tx-queues <number>]>",
116 .function = memif_create_command_fn,
120 static clib_error_t *
121 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
122 vlib_cli_command_t * cmd)
124 unformat_input_t _line_input, *line_input = &_line_input;
128 /* Get a line of input. */
129 if (!unformat_user (input, unformat_line_input, line_input))
132 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
134 if (unformat (line_input, "key 0x%" PRIx64, &key))
137 return clib_error_return (0, "unknown input `%U'",
138 format_unformat_error, input);
140 unformat_free (line_input);
143 return clib_error_return (0, "missing key");
145 memif_delete_if (vm, key);
151 VLIB_CLI_COMMAND (memif_delete_command, static) = {
152 .path = "delete memif",
153 .short_help = "delete memif key <key-value>",
154 .function = memif_delete_command_fn,
158 static clib_error_t *
159 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
160 vlib_cli_command_t * cmd)
162 memif_main_t *mm = &memif_main;
164 vnet_main_t *vnm = vnet_get_main ();
168 pool_foreach (mif, mm->interfaces,
170 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
171 vnm, mif->sw_if_index);
172 vlib_cli_output (vm, " key 0x%" PRIx64 " file %s", mif->key,
173 mif->socket_filename);
174 vlib_cli_output (vm, " listener %d conn-fd %d int-fd %d", mif->listener_index,
175 mif->connection.fd, mif->interrupt_line.fd);
176 vlib_cli_output (vm, " ring-size %u num-s2m-rings %u num-m2s-rings %u buffer_size %u",
177 (1 << mif->log2_ring_size),
181 for (i=0; i < mif->num_s2m_rings; i++)
183 memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
186 vlib_cli_output (vm, " slave-to-master ring %u:", i);
187 vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
190 for (i=0; i < mif->num_m2s_rings; i++)
192 memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
195 vlib_cli_output (vm, " master-to-slave ring %u:", i);
196 vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
206 VLIB_CLI_COMMAND (memif_show_command, static) = {
207 .path = "show memif",
208 .short_help = "show memif",
209 .function = memif_show_command_fn,
214 memif_cli_init (vlib_main_t * vm)
219 VLIB_INIT_FUNCTION (memif_cli_init);
222 * fd.io coding-style-patch-verification: ON
225 * eval: (c-set-style "gnu")