blob: ac167e827c6702dfcc6e5e632d51ba23556089c3 [file] [log] [blame]
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001/*
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>
Jakub Grajciar2c504f82019-09-26 10:34:41 +020028#include <vnet/pci/pci_types_api.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020029
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
53static void
54vl_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
Jakub Grajciar2c504f82019-09-26 10:34:41 +020063 pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020064 if (!mp->use_random_mac)
65 {
66 clib_memcpy (ap->mac_addr, mp->mac_address, 6);
67 ap->mac_addr_set = 1;
68 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020069 ap->sw_if_index = (u32) ~ 0;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +020070 if (mp->gso_enabled)
71 ap->gso_enabled = 1;
72 else
73 ap->gso_enabled = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020074 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
91static void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020092vl_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 ();
Mohsin Kazmiddd21832019-01-22 13:05:00 +000096 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020097 int rv = 0;
98 vnet_hw_interface_t *hw;
99 virtio_if_t *vif;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200100 vl_api_virtio_pci_delete_reply_t *rmp;
101 vl_api_registration_t *reg;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200102
Dave Barach3940de32019-07-23 16:28:36 -0400103 hw =
104 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
105 htonl (mp->sw_if_index));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200106 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
107 {
Dave Barach3940de32019-07-23 16:28:36 -0400108 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200109 goto reply;
110 }
111
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000112 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200113
114 rv = virtio_pci_delete_if (vm, vif);
115
116reply:
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);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200127}
128
129static void
130virtio_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);
Jakub Grajciar2c504f82019-09-26 10:34:41 +0200140 pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
141 &mp->pci_addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200142 mp->sw_if_index = htonl (vif->sw_if_index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000143 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);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200147 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
154static 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
181static void
182setup_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
189static clib_error_t *
190virtio_pci_api_hookup (vlib_main_t * vm)
191{
Dave Barach39d69112019-11-27 11:42:13 -0500192 api_main_t *am = vlibapi_get_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200193
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
212VLIB_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 */