virtio: Minor fixes and header cleanup
[vpp.git] / src / vnet / devices / virtio / cli.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 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 <vlib/vlib.h>
18 #include <vlib/unix/unix.h>
19 #include <vlib/pci/pci.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/devices/virtio/virtio.h>
24 #include <vnet/devices/virtio/pci.h>
25
26 static clib_error_t *
27 virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
28                               vlib_cli_command_t * cmd)
29 {
30   unformat_input_t _line_input, *line_input = &_line_input;
31   virtio_pci_create_if_args_t args;
32   u32 tmp;
33   u64 feature_mask = (u64) ~ (0ULL);
34
35   /* Get a line of input. */
36   if (!unformat_user (input, unformat_line_input, line_input))
37     return 0;
38
39   memset (&args, 0, sizeof (args));
40   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
41     {
42       if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
43         ;
44       else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
45         args.features = feature_mask;
46       else if (unformat (line_input, "rx-queue-size %u", &tmp))
47         args.rxq_size = tmp;
48       else if (unformat (line_input, "tx-queue-size %u", &tmp))
49         args.txq_size = tmp;
50       else
51         return clib_error_return (0, "unknown input `%U'",
52                                   format_unformat_error, input);
53     }
54   unformat_free (line_input);
55
56   virtio_pci_create_if (vm, &args);
57
58   return args.error;
59 }
60
61 /* *INDENT-OFF* */
62 VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
63   .path = "create interface virtio",
64   .short_help = "create interface virtio <pci-address> "
65                 "[feature-mask <hex-mask>] [rx-queue-size <size>] [tx-queue-size <size>]",
66   .function = virtio_pci_create_command_fn,
67 };
68 /* *INDENT-ON* */
69
70 static clib_error_t *
71 virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
72                               vlib_cli_command_t * cmd)
73 {
74   unformat_input_t _line_input, *line_input = &_line_input;
75   u32 sw_if_index = ~0;
76   vnet_hw_interface_t *hw;
77   virtio_main_t *vim = &virtio_main;
78   virtio_if_t *vif;
79   vnet_main_t *vnm = vnet_get_main ();
80
81   /* Get a line of input. */
82   if (!unformat_user (input, unformat_line_input, line_input))
83     return 0;
84
85   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
86     {
87       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
88         ;
89       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
90                          vnm, &sw_if_index))
91         ;
92       else
93         return clib_error_return (0, "unknown input `%U'",
94                                   format_unformat_error, input);
95     }
96   unformat_free (line_input);
97
98   if (sw_if_index == ~0)
99     return clib_error_return (0,
100                               "please specify interface name or sw_if_index");
101
102   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
103   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
104     return clib_error_return (0, "not a virtio interface");
105
106   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
107
108   if (virtio_pci_delete_if (vm, vif) < 0)
109     return clib_error_return (0, "not a virtio pci interface");
110
111   return 0;
112 }
113
114 /* *INDENT-OFF* */
115 VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
116   .path = "delete interface virtio",
117   .short_help = "delete interface virtio "
118     "{<interface> | sw_if_index <sw_idx>}",
119   .function = virtio_pci_delete_command_fn,
120 };
121 /* *INDENT-ON* */
122
123 static clib_error_t *
124 show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
125                     vlib_cli_command_t * cmd)
126 {
127   virtio_main_t *vim = &virtio_main;
128   vnet_main_t *vnm = &vnet_main;
129   virtio_if_t *vif;
130   clib_error_t *error = 0;
131   u32 hw_if_index, *hw_if_indices = 0;
132   vnet_hw_interface_t *hi;
133   u8 show_descr = 0, show_device_config = 0;
134
135   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
136     {
137       if (unformat
138           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
139         {
140           hi = vnet_get_hw_interface (vnm, hw_if_index);
141           if (virtio_device_class.index != hi->dev_class_index)
142             {
143               error = clib_error_return (0, "unknown input `%U'",
144                                          format_unformat_error, input);
145               goto done;
146             }
147           vec_add1 (hw_if_indices, hw_if_index);
148         }
149       else if (unformat (input, "descriptors") || unformat (input, "desc"))
150         show_descr = 1;
151       else if (unformat (input, "debug-device"))
152         show_device_config = 1;
153       else
154         {
155           error = clib_error_return (0, "unknown input `%U'",
156                                      format_unformat_error, input);
157           goto done;
158         }
159     }
160
161   if (vec_len (hw_if_indices) == 0)
162     {
163       pool_foreach (vif, vim->interfaces,
164                     vec_add1 (hw_if_indices, vif->hw_if_index);
165         );
166     }
167   else if (show_device_config)
168     {
169       vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
170       if (vif->type == VIRTIO_IF_TYPE_PCI)
171         debug_device_config_space (vm, vif);
172     }
173
174   virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);
175
176 done:
177   vec_free (hw_if_indices);
178   return error;
179 }
180
181 /* *INDENT-OFF* */
182 VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
183   .path = "show virtio pci",
184   .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
185   .function = show_virtio_pci_fn,
186 };
187 /* *INDENT-ON* */
188
189 clib_error_t *
190 virtio_pci_cli_init (vlib_main_t * vm)
191 {
192   virtio_main_t *vim = &virtio_main;
193   vim->log_default = vlib_log_register_class ("virtio-pci", 0);
194   return 0;
195 }
196
197 VLIB_INIT_FUNCTION (virtio_pci_cli_init);
198
199 /*
200  * fd.io coding-style-patch-verification: ON
201  *
202  * Local Variables:
203  * eval: (c-set-style "gnu")
204  * End:
205  */