devices: virtio API cleanup
[vpp.git] / src / vnet / devices / virtio / virtio_api.c
1 /*
2  *------------------------------------------------------------------
3  * virtio_api.c - vnet virtio pci device driver API support
4  *
5  * Copyright (c) 2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/devices/virtio/virtio.h>
27 #include <vnet/devices/virtio/pci.h>
28 #include <vnet/pci/pci_types_api.h>
29
30 #include <vnet/vnet_msg_enum.h>
31
32 #define vl_typedefs             /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_typedefs
35
36 #define vl_endianfun            /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_endianfun
39
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <vnet/vnet_all_api_h.h>
44 #undef vl_printfun
45
46 #include <vlibapi/api_helper_macros.h>
47
48 #define foreach_virtio_pci_api_msg                        \
49 _(VIRTIO_PCI_CREATE, virtio_pci_create)                   \
50 _(VIRTIO_PCI_DELETE, virtio_pci_delete)                   \
51 _(SW_INTERFACE_VIRTIO_PCI_DUMP, sw_interface_virtio_pci_dump)
52
53 static void
54 vl_api_virtio_pci_create_t_handler (vl_api_virtio_pci_create_t * mp)
55 {
56   vlib_main_t *vm = vlib_get_main ();
57   vl_api_virtio_pci_create_reply_t *rmp;
58   vl_api_registration_t *reg;
59   virtio_pci_create_if_args_t _a, *ap = &_a;
60
61   clib_memset (ap, 0, sizeof (*ap));
62
63   pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
64   if (!mp->use_random_mac)
65     {
66       clib_memcpy (ap->mac_addr, mp->mac_address, 6);
67       ap->mac_addr_set = 1;
68     }
69   ap->sw_if_index = (u32) ~ 0;
70   if (mp->gso_enabled)
71     ap->gso_enabled = 1;
72   else
73     ap->gso_enabled = 0;
74   ap->features = clib_net_to_host_u64 (mp->features);
75
76   virtio_pci_create_if (vm, ap);
77
78   reg = vl_api_client_index_to_registration (mp->client_index);
79   if (!reg)
80     return;;
81
82   rmp = vl_msg_api_alloc (sizeof (*rmp));
83   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY);
84   rmp->context = mp->context;
85   rmp->retval = htonl (ap->rv);
86   rmp->sw_if_index = htonl (ap->sw_if_index);
87
88   vl_api_send_msg (reg, (u8 *) rmp);
89 }
90
91 static void
92 vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp)
93 {
94   vnet_main_t *vnm = vnet_get_main ();
95   vlib_main_t *vm = vlib_get_main ();
96   virtio_main_t *vim = &virtio_main;
97   int rv = 0;
98   vnet_hw_interface_t *hw;
99   virtio_if_t *vif;
100   vl_api_virtio_pci_delete_reply_t *rmp;
101   vl_api_registration_t *reg;
102
103   hw =
104     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
105                                                    htonl (mp->sw_if_index));
106   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
107     {
108       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
109       goto reply;
110     }
111
112   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
113
114   rv = virtio_pci_delete_if (vm, vif);
115
116 reply:
117   reg = vl_api_client_index_to_registration (mp->client_index);
118   if (!reg)
119     return;
120
121   rmp = vl_msg_api_alloc (sizeof (*rmp));
122   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY);
123   rmp->context = mp->context;
124   rmp->retval = htonl (rv);
125
126   vl_api_send_msg (reg, (u8 *) rmp);
127 }
128
129 static void
130 virtio_pci_send_sw_interface_details (vpe_api_main_t * am,
131                                       vl_api_registration_t * reg,
132                                       virtio_if_t * vif, u32 context)
133 {
134   vl_api_sw_interface_virtio_pci_details_t *mp;
135   mp = vl_msg_api_alloc (sizeof (*mp));
136
137   clib_memset (mp, 0, sizeof (*mp));
138
139   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
140   pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
141                       &mp->pci_addr);
142   mp->sw_if_index = htonl (vif->sw_if_index);
143   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0);
144   mp->rx_ring_sz = htons (vring->size);
145   vring = vec_elt_at_index (vif->txq_vrings, 0);
146   mp->tx_ring_sz = htons (vring->size);
147   clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
148   mp->features = clib_host_to_net_u64 (vif->features);
149
150   mp->context = context;
151   vl_api_send_msg (reg, (u8 *) mp);
152 }
153
154 static void
155   vl_api_sw_interface_virtio_pci_dump_t_handler
156   (vl_api_sw_interface_virtio_pci_dump_t * mp)
157 {
158   vpe_api_main_t *am = &vpe_api_main;
159   vl_api_registration_t *reg;
160   virtio_main_t *vmx = &virtio_main;
161   virtio_if_t *vif;
162
163   reg = vl_api_client_index_to_registration (mp->client_index);
164   if (!reg)
165     return;
166
167   pool_foreach (vif, vmx->interfaces, (
168                                         {
169                                         if (vif->type == VIRTIO_IF_TYPE_PCI)
170                                         {
171                                         virtio_pci_send_sw_interface_details
172                                         (am, reg, vif, mp->context);}
173                                         }
174                 ));
175 }
176
177 #define vl_msg_name_crc_list
178 #include <vnet/vnet_all_api_h.h>
179 #undef vl_msg_name_crc_list
180
181 static void
182 setup_message_id_table (api_main_t * am)
183 {
184 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
185   foreach_vl_msg_name_crc_virtio;
186 #undef _
187 }
188
189 static clib_error_t *
190 virtio_pci_api_hookup (vlib_main_t * vm)
191 {
192   api_main_t *am = vlibapi_get_main ();
193
194 #define _(N,n)                                                  \
195     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
196                            vl_api_##n##_t_handler,              \
197                            vl_noop_handler,                     \
198                            vl_api_##n##_t_endian,               \
199                            vl_api_##n##_t_print,                \
200                            sizeof(vl_api_##n##_t), 1);
201   foreach_virtio_pci_api_msg;
202 #undef _
203
204   /*
205    * Set up the (msg_name, crc, message-id) table
206    */
207   setup_message_id_table (am);
208
209   return 0;
210 }
211
212 VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup);
213
214 /*
215  * fd.io coding-style-patch-verification: ON
216  *
217  * Local Variables:
218  * eval: (c-set-style "gnu")
219  * End:
220  */