memif: multi-queues support
[vpp.git] / src / plugins / memif / cli.c
1 /*
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25
26 #include <memif/memif.h>
27
28 static uword
29 unformat_memif_queues (unformat_input_t * input, va_list * args)
30 {
31   u32 *rx_queues = va_arg (*args, u32 *);
32   u32 *tx_queues = va_arg (*args, u32 *);
33
34   if (unformat (input, "rx-queues %u", rx_queues))
35     ;
36   if (unformat (input, "tx-queues %u", tx_queues))
37     ;
38
39   return 1;
40 }
41
42 static clib_error_t *
43 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44                          vlib_cli_command_t * cmd)
45 {
46   unformat_input_t _line_input, *line_input = &_line_input;
47   int r;
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;
53
54   /* Get a line of input. */
55   if (!unformat_user (input, unformat_line_input, line_input))
56     return 0;
57
58   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
59     {
60       if (unformat (line_input, "key 0x%" PRIx64, &args.key))
61         ;
62       else if (unformat (line_input, "socket %s", &args.socket_filename))
63         ;
64       else if (unformat (line_input, "ring-size %u", &ring_size))
65         ;
66       else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
67         ;
68       else if (unformat (line_input, "master"))
69         args.is_master = 1;
70       else if (unformat (line_input, "slave %U",
71                          unformat_memif_queues, &rx_queues, &tx_queues))
72         args.is_master = 0;
73       else if (unformat (line_input, "hw-addr %U",
74                          unformat_ethernet_address, args.hw_addr))
75         args.hw_addr_set = 1;
76       else
77         return clib_error_return (0, "unknown input `%U'",
78                                   format_unformat_error, input);
79     }
80   unformat_free (line_input);
81
82   if (!is_pow2 (ring_size))
83     return clib_error_return (0, "ring size must be power of 2");
84
85   args.log2_ring_size = min_log2 (ring_size);
86
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");
91
92   args.rx_queues = rx_queues;
93   args.tx_queues = tx_queues;
94
95   r = memif_create_if (vm, &args);
96
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);
100
101   if (r == VNET_API_ERROR_INVALID_INTERFACE)
102     return clib_error_return (0, "Invalid interface name");
103
104   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
105     return clib_error_return (0, "Interface already exists");
106
107   return 0;
108 }
109
110 /* *INDENT-OFF* */
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,
117 };
118 /* *INDENT-ON* */
119
120 static clib_error_t *
121 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
122                          vlib_cli_command_t * cmd)
123 {
124   unformat_input_t _line_input, *line_input = &_line_input;
125   u64 key = 0;
126   u8 key_defined = 0;
127
128   /* Get a line of input. */
129   if (!unformat_user (input, unformat_line_input, line_input))
130     return 0;
131
132   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
133     {
134       if (unformat (line_input, "key 0x%" PRIx64, &key))
135         key_defined = 1;
136       else
137         return clib_error_return (0, "unknown input `%U'",
138                                   format_unformat_error, input);
139     }
140   unformat_free (line_input);
141
142   if (!key_defined)
143     return clib_error_return (0, "missing key");
144
145   memif_delete_if (vm, key);
146
147   return 0;
148 }
149
150 /* *INDENT-OFF* */
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,
155 };
156 /* *INDENT-ON* */
157
158 static clib_error_t *
159 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
160                        vlib_cli_command_t * cmd)
161 {
162   memif_main_t *mm = &memif_main;
163   memif_if_t *mif;
164   vnet_main_t *vnm = vnet_get_main ();
165   int i;
166
167   /* *INDENT-OFF* */
168   pool_foreach (mif, mm->interfaces,
169     ({
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),
178                         mif->num_s2m_rings,
179                         mif->num_m2s_rings,
180                         mif->buffer_size);
181        for (i=0; i < mif->num_s2m_rings; i++)
182          {
183            memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_S2M, i);
184            if (ring)
185              {
186                vlib_cli_output (vm, "  slave-to-master ring %u:", i);
187                vlib_cli_output (vm, "    head %u tail %u", ring->head, ring->tail);
188              }
189          }
190        for (i=0; i < mif->num_m2s_rings; i++)
191          {
192            memif_ring_t * ring = memif_get_ring (mif, MEMIF_RING_M2S, i);
193            if (ring)
194              {
195                vlib_cli_output (vm, "  master-to-slave ring %u:", i);
196                vlib_cli_output (vm, "    head %u tail %u", ring->head, ring->tail);
197              }
198          }
199     }));
200   /* *INDENT-ON* */
201
202   return 0;
203 }
204
205 /* *INDENT-OFF* */
206 VLIB_CLI_COMMAND (memif_show_command, static) = {
207   .path = "show memif",
208   .short_help = "show memif",
209   .function = memif_show_command_fn,
210 };
211 /* *INDENT-ON* */
212
213 clib_error_t *
214 memif_cli_init (vlib_main_t * vm)
215 {
216   return 0;
217 }
218
219 VLIB_INIT_FUNCTION (memif_cli_init);
220
221 /*
222  * fd.io coding-style-patch-verification: ON
223  *
224  * Local Variables:
225  * eval: (c-set-style "gnu")
226  * End:
227  */