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 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
30 vlib_cli_command_t * cmd)
32 unformat_input_t _line_input, *line_input = &_line_input;
34 u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
35 memif_create_if_args_t args = { 0 };
36 args.buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
38 /* Get a line of input. */
39 if (!unformat_user (input, unformat_line_input, line_input))
42 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
44 if (unformat (line_input, "key 0x%" PRIx64, &args.key))
46 else if (unformat (line_input, "socket %s", &args.socket_filename))
48 else if (unformat (line_input, "ring-size %u", &ring_size))
50 else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
52 else if (unformat (line_input, "master"))
54 else if (unformat (line_input, "slave"))
56 else if (unformat (line_input, "hw-addr %U",
57 unformat_ethernet_address, args.hw_addr))
60 return clib_error_return (0, "unknown input `%U'",
61 format_unformat_error, input);
63 unformat_free (line_input);
65 if (!is_pow2 (ring_size))
66 return clib_error_return (0, "ring size must be power of 2");
68 args.log2_ring_size = min_log2 (ring_size);
70 r = memif_create_if (vm, &args);
72 if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
73 && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
74 return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
76 if (r == VNET_API_ERROR_INVALID_INTERFACE)
77 return clib_error_return (0, "Invalid interface name");
79 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
80 return clib_error_return (0, "Interface already exists");
86 VLIB_CLI_COMMAND (memif_create_command, static) = {
87 .path = "create memif",
88 .short_help = "create memif [key <key>] [socket <path>] "
89 "[ring-size <size>] [buffer-size <size>] [hw-addr <mac-address>] "
91 .function = memif_create_command_fn,
96 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
97 vlib_cli_command_t * cmd)
99 unformat_input_t _line_input, *line_input = &_line_input;
103 /* Get a line of input. */
104 if (!unformat_user (input, unformat_line_input, line_input))
107 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
109 if (unformat (line_input, "key 0x%" PRIx64, &key))
112 return clib_error_return (0, "unknown input `%U'",
113 format_unformat_error, input);
115 unformat_free (line_input);
118 return clib_error_return (0, "missing key");
120 memif_delete_if (vm, key);
126 VLIB_CLI_COMMAND (memif_delete_command, static) = {
127 .path = "delete memif",
128 .short_help = "delete memif key <key-value>",
129 .function = memif_delete_command_fn,
133 static clib_error_t *
134 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
135 vlib_cli_command_t * cmd)
137 memif_main_t *mm = &memif_main;
139 vnet_main_t *vnm = vnet_get_main ();
143 pool_foreach (mif, mm->interfaces,
145 vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
146 vnm, mif->sw_if_index);
147 vlib_cli_output (vm, " key 0x%" PRIx64 " file %s", mif->key,
148 mif->socket_filename);
149 vlib_cli_output (vm, " listener %d conn-fd %d int-fd %d", mif->listener_index,
150 mif->connection.fd, mif->interrupt_line.fd);
151 vlib_cli_output (vm, " ring-size %u num-c2s-rings %u num-s2c-rings %u buffer_size %u",
152 (1 << mif->log2_ring_size),
156 for (i=0; i < mif->num_s2m_rings; i++)
158 memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
161 vlib_cli_output (vm, " slave-to-master ring %u:", i);
162 vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
165 for (i=0; i < mif->num_m2s_rings; i++)
167 memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
170 vlib_cli_output (vm, " master-to-slave ring %u:", i);
171 vlib_cli_output (vm, " head %u tail %u", ring->head, ring->tail);
181 VLIB_CLI_COMMAND (memif_show_command, static) = {
182 .path = "show memif",
183 .short_help = "show memif",
184 .function = memif_show_command_fn,
189 memif_cli_init (vlib_main_t * vm)
194 VLIB_INIT_FUNCTION (memif_cli_init);
197 * fd.io coding-style-patch-verification: ON
200 * eval: (c-set-style "gnu")