blob: 5035799befec0e1b2be0418f6839f0eb2d5d68b7 [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>
28
29#include <vnet/vnet_msg_enum.h>
30
31#define vl_typedefs /* define message structures */
32#include <vnet/vnet_all_api_h.h>
33#undef vl_typedefs
34
35#define vl_endianfun /* define message structures */
36#include <vnet/vnet_all_api_h.h>
37#undef vl_endianfun
38
39/* instantiate all the print functions we know about */
40#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41#define vl_printfun
42#include <vnet/vnet_all_api_h.h>
43#undef vl_printfun
44
45#include <vlibapi/api_helper_macros.h>
46
47#define foreach_virtio_pci_api_msg \
48_(VIRTIO_PCI_CREATE, virtio_pci_create) \
49_(VIRTIO_PCI_DELETE, virtio_pci_delete) \
50_(SW_INTERFACE_VIRTIO_PCI_DUMP, sw_interface_virtio_pci_dump)
51
52static void
53vl_api_virtio_pci_create_t_handler (vl_api_virtio_pci_create_t * mp)
54{
55 vlib_main_t *vm = vlib_get_main ();
56 vl_api_virtio_pci_create_reply_t *rmp;
57 vl_api_registration_t *reg;
58 virtio_pci_create_if_args_t _a, *ap = &_a;
59
60 clib_memset (ap, 0, sizeof (*ap));
61
62 ap->addr = ntohl (mp->pci_addr);
63 if (!mp->use_random_mac)
64 {
65 clib_memcpy (ap->mac_addr, mp->mac_address, 6);
66 ap->mac_addr_set = 1;
67 }
68 ap->rxq_size = ntohs (mp->rx_ring_sz);
69 ap->txq_size = ntohs (mp->tx_ring_sz);
70 ap->sw_if_index = (u32) ~ 0;
71 ap->features = clib_net_to_host_u64 (mp->features);
72
73 virtio_pci_create_if (vm, ap);
74
75 reg = vl_api_client_index_to_registration (mp->client_index);
76 if (!reg)
77 return;;
78
79 rmp = vl_msg_api_alloc (sizeof (*rmp));
80 rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY);
81 rmp->context = mp->context;
82 rmp->retval = htonl (ap->rv);
83 rmp->sw_if_index = htonl (ap->sw_if_index);
84
85 vl_api_send_msg (reg, (u8 *) rmp);
86}
87
88static void
89virtio_pci_send_sw_interface_event_deleted (vpe_api_main_t * am,
90 vl_api_registration_t * reg,
91 u32 sw_if_index)
92{
93 vl_api_sw_interface_event_t *mp;
94
95 mp = vl_msg_api_alloc (sizeof (*mp));
96 clib_memset (mp, 0, sizeof (*mp));
97 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_EVENT);
98 mp->sw_if_index = htonl (sw_if_index);
99
100 mp->admin_up_down = 0;
101 mp->link_up_down = 0;
102 mp->deleted = 1;
103 vl_api_send_msg (reg, (u8 *) mp);
104}
105
106static void
107vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp)
108{
109 vnet_main_t *vnm = vnet_get_main ();
110 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000111 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200112 int rv = 0;
113 vnet_hw_interface_t *hw;
114 virtio_if_t *vif;
115 vpe_api_main_t *vam = &vpe_api_main;
116 vl_api_virtio_pci_delete_reply_t *rmp;
117 vl_api_registration_t *reg;
118 u32 sw_if_index = ntohl (mp->sw_if_index);
119
120 hw = vnet_get_sup_hw_interface (vnm, htonl (mp->sw_if_index));
121 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
122 {
123 rv = VNET_API_ERROR_INVALID_INTERFACE;
124 goto reply;
125 }
126
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000127 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200128
129 rv = virtio_pci_delete_if (vm, vif);
130
131reply:
132 reg = vl_api_client_index_to_registration (mp->client_index);
133 if (!reg)
134 return;
135
136 rmp = vl_msg_api_alloc (sizeof (*rmp));
137 rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY);
138 rmp->context = mp->context;
139 rmp->retval = htonl (rv);
140
141 vl_api_send_msg (reg, (u8 *) rmp);
142
143 if (!rv)
144 {
145 virtio_pci_send_sw_interface_event_deleted (vam, reg, sw_if_index);
146 }
147}
148
149static void
150virtio_pci_send_sw_interface_details (vpe_api_main_t * am,
151 vl_api_registration_t * reg,
152 virtio_if_t * vif, u32 context)
153{
154 vl_api_sw_interface_virtio_pci_details_t *mp;
155 mp = vl_msg_api_alloc (sizeof (*mp));
156
157 clib_memset (mp, 0, sizeof (*mp));
158
159 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
160 mp->pci_addr = htonl (vif->pci_addr.as_u32);
161 mp->sw_if_index = htonl (vif->sw_if_index);
162 mp->rx_ring_sz = htons (vif->rx_ring_sz);
163 mp->tx_ring_sz = htons (vif->tx_ring_sz);
164 clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
165 mp->features = clib_host_to_net_u64 (vif->features);
166
167 mp->context = context;
168 vl_api_send_msg (reg, (u8 *) mp);
169}
170
171static void
172 vl_api_sw_interface_virtio_pci_dump_t_handler
173 (vl_api_sw_interface_virtio_pci_dump_t * mp)
174{
175 vpe_api_main_t *am = &vpe_api_main;
176 vl_api_registration_t *reg;
177 virtio_main_t *vmx = &virtio_main;
178 virtio_if_t *vif;
179
180 reg = vl_api_client_index_to_registration (mp->client_index);
181 if (!reg)
182 return;
183
184 pool_foreach (vif, vmx->interfaces, (
185 {
186 if (vif->type == VIRTIO_IF_TYPE_PCI)
187 {
188 virtio_pci_send_sw_interface_details
189 (am, reg, vif, mp->context);}
190 }
191 ));
192}
193
194#define vl_msg_name_crc_list
195#include <vnet/vnet_all_api_h.h>
196#undef vl_msg_name_crc_list
197
198static void
199setup_message_id_table (api_main_t * am)
200{
201#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
202 foreach_vl_msg_name_crc_virtio;
203#undef _
204}
205
206static clib_error_t *
207virtio_pci_api_hookup (vlib_main_t * vm)
208{
209 api_main_t *am = &api_main;
210
211#define _(N,n) \
212 vl_msg_api_set_handlers(VL_API_##N, #n, \
213 vl_api_##n##_t_handler, \
214 vl_noop_handler, \
215 vl_api_##n##_t_endian, \
216 vl_api_##n##_t_print, \
217 sizeof(vl_api_##n##_t), 1);
218 foreach_virtio_pci_api_msg;
219#undef _
220
221 /*
222 * Set up the (msg_name, crc, message-id) table
223 */
224 setup_message_id_table (am);
225
226 return 0;
227}
228
229VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup);
230
231/*
232 * fd.io coding-style-patch-verification: ON
233 *
234 * Local Variables:
235 * eval: (c-set-style "gnu")
236 * End:
237 */