blob: 96ed927dc38e0a777e3bfae1cb8e9957bb3ac23f [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 Grajciar53f06a02020-03-30 08:12:57 +020028#include <vlib/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 Kazmi6d4af892020-01-03 15:11:53 +000074 if (mp->checksum_offload_enabled)
75 ap->checksum_offload_enabled = 1;
76 else
77 ap->checksum_offload_enabled = 0;
78
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020079 ap->features = clib_net_to_host_u64 (mp->features);
80
81 virtio_pci_create_if (vm, ap);
82
83 reg = vl_api_client_index_to_registration (mp->client_index);
84 if (!reg)
85 return;;
86
87 rmp = vl_msg_api_alloc (sizeof (*rmp));
88 rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY);
89 rmp->context = mp->context;
90 rmp->retval = htonl (ap->rv);
91 rmp->sw_if_index = htonl (ap->sw_if_index);
92
93 vl_api_send_msg (reg, (u8 *) rmp);
94}
95
96static void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020097vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp)
98{
99 vnet_main_t *vnm = vnet_get_main ();
100 vlib_main_t *vm = vlib_get_main ();
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000101 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200102 int rv = 0;
103 vnet_hw_interface_t *hw;
104 virtio_if_t *vif;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200105 vl_api_virtio_pci_delete_reply_t *rmp;
106 vl_api_registration_t *reg;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200107
Dave Barach3940de32019-07-23 16:28:36 -0400108 hw =
109 vnet_get_sup_hw_interface_api_visible_or_null (vnm,
110 htonl (mp->sw_if_index));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200111 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
112 {
Dave Barach3940de32019-07-23 16:28:36 -0400113 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200114 goto reply;
115 }
116
Mohsin Kazmiddd21832019-01-22 13:05:00 +0000117 vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200118
119 rv = virtio_pci_delete_if (vm, vif);
120
121reply:
122 reg = vl_api_client_index_to_registration (mp->client_index);
123 if (!reg)
124 return;
125
126 rmp = vl_msg_api_alloc (sizeof (*rmp));
127 rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY);
128 rmp->context = mp->context;
129 rmp->retval = htonl (rv);
130
131 vl_api_send_msg (reg, (u8 *) rmp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200132}
133
134static void
135virtio_pci_send_sw_interface_details (vpe_api_main_t * am,
136 vl_api_registration_t * reg,
137 virtio_if_t * vif, u32 context)
138{
139 vl_api_sw_interface_virtio_pci_details_t *mp;
140 mp = vl_msg_api_alloc (sizeof (*mp));
141
142 clib_memset (mp, 0, sizeof (*mp));
143
144 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
Jakub Grajciar2c504f82019-09-26 10:34:41 +0200145 pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
146 &mp->pci_addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200147 mp->sw_if_index = htonl (vif->sw_if_index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000148 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0);
149 mp->rx_ring_sz = htons (vring->size);
150 vring = vec_elt_at_index (vif->txq_vrings, 0);
151 mp->tx_ring_sz = htons (vring->size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200152 clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
153 mp->features = clib_host_to_net_u64 (vif->features);
154
155 mp->context = context;
156 vl_api_send_msg (reg, (u8 *) mp);
157}
158
159static void
160 vl_api_sw_interface_virtio_pci_dump_t_handler
161 (vl_api_sw_interface_virtio_pci_dump_t * mp)
162{
163 vpe_api_main_t *am = &vpe_api_main;
164 vl_api_registration_t *reg;
165 virtio_main_t *vmx = &virtio_main;
166 virtio_if_t *vif;
167
168 reg = vl_api_client_index_to_registration (mp->client_index);
169 if (!reg)
170 return;
171
172 pool_foreach (vif, vmx->interfaces, (
173 {
174 if (vif->type == VIRTIO_IF_TYPE_PCI)
175 {
176 virtio_pci_send_sw_interface_details
177 (am, reg, vif, mp->context);}
178 }
179 ));
180}
181
182#define vl_msg_name_crc_list
183#include <vnet/vnet_all_api_h.h>
184#undef vl_msg_name_crc_list
185
186static void
187setup_message_id_table (api_main_t * am)
188{
189#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
190 foreach_vl_msg_name_crc_virtio;
191#undef _
192}
193
194static clib_error_t *
195virtio_pci_api_hookup (vlib_main_t * vm)
196{
Dave Barach39d69112019-11-27 11:42:13 -0500197 api_main_t *am = vlibapi_get_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200198
199#define _(N,n) \
200 vl_msg_api_set_handlers(VL_API_##N, #n, \
201 vl_api_##n##_t_handler, \
202 vl_noop_handler, \
203 vl_api_##n##_t_endian, \
204 vl_api_##n##_t_print, \
205 sizeof(vl_api_##n##_t), 1);
206 foreach_virtio_pci_api_msg;
207#undef _
208
209 /*
210 * Set up the (msg_name, crc, message-id) table
211 */
212 setup_message_id_table (am);
213
214 return 0;
215}
216
217VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup);
218
219/*
220 * fd.io coding-style-patch-verification: ON
221 *
222 * Local Variables:
223 * eval: (c-set-style "gnu")
224 * End:
225 */