34c77def79a974ee23717592acc0bb2134be2ad0
[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 #include <memif/private.h>
28
29 static clib_error_t *
30 memif_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
31                          vlib_cli_command_t * cmd)
32 {
33   unformat_input_t _line_input, *line_input = &_line_input;
34   int r;
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;
40
41   /* Get a line of input. */
42   if (!unformat_user (input, unformat_line_input, line_input))
43     return 0;
44
45   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46     {
47       if (unformat (line_input, "id %u", &args.id))
48         ;
49       else if (unformat (line_input, "socket %s", &args.socket_filename))
50         ;
51       else if (unformat (line_input, "secret %s", &args.secret))
52         ;
53       else if (unformat (line_input, "ring-size %u", &ring_size))
54         ;
55       else if (unformat (line_input, "rx-queues %u", &rx_queues))
56         ;
57       else if (unformat (line_input, "tx-queues %u", &tx_queues))
58         ;
59       else if (unformat (line_input, "buffer-size %u", &args.buffer_size))
60         ;
61       else if (unformat (line_input, "master"))
62         args.is_master = 1;
63       else if (unformat (line_input, "slave"))
64         args.is_master = 0;
65       else if (unformat (line_input, "hw-addr %U",
66                          unformat_ethernet_address, args.hw_addr))
67         args.hw_addr_set = 1;
68       else
69         return clib_error_return (0, "unknown input `%U'",
70                                   format_unformat_error, input);
71     }
72   unformat_free (line_input);
73
74   if (!is_pow2 (ring_size))
75     return clib_error_return (0, "ring size must be power of 2");
76
77   args.log2_ring_size = min_log2 (ring_size);
78
79   if (rx_queues > 255 || rx_queues < 1)
80     return clib_error_return (0, "rx queue must be between 1 - 255");
81   if (tx_queues > 255 || tx_queues < 1)
82     return clib_error_return (0, "tx queue must be between 1 - 255");
83
84   args.rx_queues = rx_queues;
85   args.tx_queues = tx_queues;
86
87   r = memif_create_if (vm, &args);
88
89   vec_free (args.socket_filename);
90   vec_free (args.secret);
91
92   if (r <= VNET_API_ERROR_SYSCALL_ERROR_1
93       && r >= VNET_API_ERROR_SYSCALL_ERROR_10)
94     return clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
95
96   if (r == VNET_API_ERROR_INVALID_INTERFACE)
97     return clib_error_return (0, "Invalid interface name");
98
99   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
100     return clib_error_return (0, "Interface with same id already exists");
101
102   return 0;
103 }
104
105 /* *INDENT-OFF* */
106 VLIB_CLI_COMMAND (memif_create_command, static) = {
107   .path = "create memif",
108   .short_help = "create memif [id <id>] [socket <path>] "
109                 "[ring-size <size>] [buffer-size <size>] [hw-addr <mac-address>] "
110                 "<master|slave> [rx-queues <number>] [tx-queues <number>]",
111   .function = memif_create_command_fn,
112 };
113 /* *INDENT-ON* */
114
115 static clib_error_t *
116 memif_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
117                          vlib_cli_command_t * cmd)
118 {
119   unformat_input_t _line_input, *line_input = &_line_input;
120   u32 sw_if_index = ~0;
121   vnet_hw_interface_t *hw;
122   memif_main_t *mm = &memif_main;
123   memif_if_t *mif;
124   vnet_main_t *vnm = vnet_get_main ();
125
126   /* Get a line of input. */
127   if (!unformat_user (input, unformat_line_input, line_input))
128     return 0;
129
130   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
131     {
132       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
133         ;
134       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
135                          vnm, &sw_if_index))
136         ;
137       else
138         return clib_error_return (0, "unknown input `%U'",
139                                   format_unformat_error, input);
140     }
141   unformat_free (line_input);
142
143   if (sw_if_index == ~0)
144     return clib_error_return (0,
145                               "please specify interface name or sw_if_index");
146
147   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
148   if (hw == NULL || memif_device_class.index != hw->dev_class_index)
149     return clib_error_return (0, "not a memif interface");
150
151   mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
152   memif_delete_if (vm, mif);
153
154   return 0;
155 }
156
157 /* *INDENT-OFF* */
158 VLIB_CLI_COMMAND (memif_delete_command, static) = {
159   .path = "delete memif",
160   .short_help = "delete memif {<interface> | sw_if_index <sw_idx>}",
161   .function = memif_delete_command_fn,
162 };
163 /* *INDENT-ON* */
164
165 static u8 *
166 format_memif_if_flags (u8 * s, va_list * args)
167 {
168   u32 flags = va_arg (*args, u32);
169 #define _(a,b,c) if ( flags & (1 << a)) s = format (s, " %s", c);
170   foreach_memif_if_flag
171 #undef _
172     return s;
173 }
174
175 static u8 *
176 format_memif_if_mode (u8 * s, va_list * args)
177 {
178   memif_if_t *mif = va_arg (*args, memif_if_t *);
179   if (mif->mode == MEMIF_INTERFACE_MODE_ETHERNET)
180     return format (s, "ethernet");
181   if (mif->mode == MEMIF_INTERFACE_MODE_IP)
182     return format (s, "ip");
183   if (mif->mode == MEMIF_INTERFACE_MODE_PUNT_INJECT)
184     return format (s, "punt-inject");
185   return format (s, "unknown mode (%u)", mif->mode);;
186 }
187
188 static u8 *
189 format_memif_queue (u8 * s, va_list * args)
190 {
191   memif_if_t *mif = va_arg (*args, memif_if_t *);
192   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
193   uword i = va_arg (*args, uword);
194   uword indent = format_get_indent (s);
195
196   s = format (s, "%U%s ring %u:\n",
197               format_white_space, indent,
198               (mif->flags & MEMIF_IF_FLAG_IS_SLAVE) ?
199               "slave-to-master" : "master-to-slave", i);
200   s = format (s, "%Uregion %u offset %u ring-size %u int-fd %d\n",
201               format_white_space, indent + 4,
202               mq->region, mq->offset, (1 << mq->log2_ring_size), mq->int_fd);
203
204   if (mq->ring)
205     s = format (s, "%Uhead %u tail %u flags 0x%04x interrupts %u\n",
206                 format_white_space, indent + 4,
207                 mq->ring->head, mq->ring->tail, mq->ring->flags,
208                 mq->int_count);
209
210   return s;
211 }
212
213 static u8 *
214 format_memif_descriptor (u8 * s, va_list * args)
215 {
216   memif_if_t *mif = va_arg (*args, memif_if_t *);
217   memif_queue_t *mq = va_arg (*args, memif_queue_t *);
218   uword indent = format_get_indent (s);
219   memif_ring_t *ring;
220   u16 ring_size;
221   u16 slot;
222
223   ring_size = 1 << mq->log2_ring_size;
224   ring = mq->ring;
225   if (ring)
226     {
227       s = format (s, "%Udescriptor table:\n", format_white_space, indent);
228       s =
229         format (s,
230                 "%Uid    flags buf len desc len      address       offset    user address\n",
231                 format_white_space, indent);
232       s =
233         format (s,
234                 "%U===== ===== ======= ======== ================== ====== ==================\n",
235                 format_white_space, indent);
236       for (slot = 0; slot < ring_size; slot++)
237         {
238           s = format (s, "%U%-5d %-5d %-7d %-7d  0x%016lx %-6d 0x%016lx\n",
239                       format_white_space, indent, slot,
240                       ring->desc[slot].flags, ring->desc[slot].buffer_length,
241                       ring->desc[slot].length,
242                       mif->regions[ring->desc[slot].region].shm,
243                       ring->desc[slot].offset, memif_get_buffer (mif, ring,
244                                                                  slot));
245         }
246       s = format (s, "\n");
247     }
248
249   return s;
250 }
251
252 static clib_error_t *
253 memif_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
254                        vlib_cli_command_t * cmd)
255 {
256   memif_main_t *mm = &memif_main;
257   memif_if_t *mif;
258   vnet_main_t *vnm = vnet_get_main ();
259   memif_queue_t *mq;
260   uword i;
261   int show_descr = 0;
262   clib_error_t *error = 0;
263   u32 hw_if_index, *hw_if_indices = 0;
264
265   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
266     {
267       if (unformat
268           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
269         vec_add1 (hw_if_indices, hw_if_index);
270       else if (unformat (input, "descriptors"))
271         show_descr = 1;
272       else
273         {
274           error = clib_error_return (0, "unknown input `%U'",
275                                      format_unformat_error, input);
276           goto done;
277         }
278     }
279
280   if (vec_len (hw_if_indices) == 0)
281     {
282       /* *INDENT-OFF* */
283       pool_foreach (mif, mm->interfaces,
284           vec_add1 (hw_if_indices, mif->hw_if_index);
285       );
286       /* *INDENT-ON* */
287     }
288
289   for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
290     {
291       vnet_hw_interface_t *hi =
292         vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
293       mif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
294       memif_socket_file_t *msf = vec_elt_at_index (mm->socket_files,
295                                                    mif->socket_file_index);
296       vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
297                        vnm, mif->sw_if_index);
298       if (mif->remote_name)
299         vlib_cli_output (vm, "  remote-name \"%s\"", mif->remote_name);
300       if (mif->remote_if_name)
301         vlib_cli_output (vm, "  remote-interface \"%s\"",
302                          mif->remote_if_name);
303       vlib_cli_output (vm, "  id %d mode %U file %s", mif->id,
304                        format_memif_if_mode, mif, msf->filename);
305       vlib_cli_output (vm, "  flags%U", format_memif_if_flags, mif->flags);
306       vlib_cli_output (vm, "  listener-fd %d conn-fd %d", msf->fd,
307                        mif->conn_fd);
308       vlib_cli_output (vm,
309                        "  num-s2m-rings %u num-m2s-rings %u buffer-size %u",
310                        mif->run.num_s2m_rings, mif->run.num_m2s_rings,
311                        mif->run.buffer_size);
312
313       if (mif->local_disc_string)
314         vlib_cli_output (vm, "  local-disc-reason \"%s\"",
315                          mif->local_disc_string);
316       if (mif->remote_disc_string)
317         vlib_cli_output (vm, "  remote-disc-reason \"%s\"",
318                          mif->remote_disc_string);
319
320       vec_foreach_index (i, mif->tx_queues)
321       {
322         mq = vec_elt_at_index (mif->tx_queues, i);
323         vlib_cli_output (vm, "  %U", format_memif_queue, mif, mq, i);
324         if (show_descr)
325           vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
326       }
327       vec_foreach_index (i, mif->rx_queues)
328       {
329         mq = vec_elt_at_index (mif->rx_queues, i);
330         vlib_cli_output (vm, "  %U", format_memif_queue, mif, mq, i);
331         if (show_descr)
332           vlib_cli_output (vm, "  %U", format_memif_descriptor, mif, mq);
333       }
334     }
335 done:
336   vec_free (hw_if_indices);
337   return error;
338 }
339
340 /* *INDENT-OFF* */
341 VLIB_CLI_COMMAND (memif_show_command, static) = {
342   .path = "show memif",
343   .short_help = "show memif {<interface>] [descriptors]",
344   .function = memif_show_command_fn,
345 };
346 /* *INDENT-ON* */
347
348 clib_error_t *
349 memif_cli_init (vlib_main_t * vm)
350 {
351   return 0;
352 }
353
354 VLIB_INIT_FUNCTION (memif_cli_init);
355
356 /*
357  * fd.io coding-style-patch-verification: ON
358  *
359  * Local Variables:
360  * eval: (c-set-style "gnu")
361  * End:
362  */