api: multiple connections per process
[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
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
52 static void
53 vl_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->sw_if_index = (u32) ~ 0;
69   if (mp->gso_enabled)
70     ap->gso_enabled = 1;
71   else
72     ap->gso_enabled = 0;
73   ap->features = clib_net_to_host_u64 (mp->features);
74
75   virtio_pci_create_if (vm, ap);
76
77   reg = vl_api_client_index_to_registration (mp->client_index);
78   if (!reg)
79     return;;
80
81   rmp = vl_msg_api_alloc (sizeof (*rmp));
82   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_CREATE_REPLY);
83   rmp->context = mp->context;
84   rmp->retval = htonl (ap->rv);
85   rmp->sw_if_index = htonl (ap->sw_if_index);
86
87   vl_api_send_msg (reg, (u8 *) rmp);
88 }
89
90 static void
91 vl_api_virtio_pci_delete_t_handler (vl_api_virtio_pci_delete_t * mp)
92 {
93   vnet_main_t *vnm = vnet_get_main ();
94   vlib_main_t *vm = vlib_get_main ();
95   virtio_main_t *vim = &virtio_main;
96   int rv = 0;
97   vnet_hw_interface_t *hw;
98   virtio_if_t *vif;
99   vl_api_virtio_pci_delete_reply_t *rmp;
100   vl_api_registration_t *reg;
101
102   hw =
103     vnet_get_sup_hw_interface_api_visible_or_null (vnm,
104                                                    htonl (mp->sw_if_index));
105   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
106     {
107       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
108       goto reply;
109     }
110
111   vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
112
113   rv = virtio_pci_delete_if (vm, vif);
114
115 reply:
116   reg = vl_api_client_index_to_registration (mp->client_index);
117   if (!reg)
118     return;
119
120   rmp = vl_msg_api_alloc (sizeof (*rmp));
121   rmp->_vl_msg_id = htons (VL_API_VIRTIO_PCI_DELETE_REPLY);
122   rmp->context = mp->context;
123   rmp->retval = htonl (rv);
124
125   vl_api_send_msg (reg, (u8 *) rmp);
126 }
127
128 static void
129 virtio_pci_send_sw_interface_details (vpe_api_main_t * am,
130                                       vl_api_registration_t * reg,
131                                       virtio_if_t * vif, u32 context)
132 {
133   vl_api_sw_interface_virtio_pci_details_t *mp;
134   mp = vl_msg_api_alloc (sizeof (*mp));
135
136   clib_memset (mp, 0, sizeof (*mp));
137
138   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
139   mp->pci_addr = htonl (vif->pci_addr.as_u32);
140   mp->sw_if_index = htonl (vif->sw_if_index);
141   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0);
142   mp->rx_ring_sz = htons (vring->size);
143   vring = vec_elt_at_index (vif->txq_vrings, 0);
144   mp->tx_ring_sz = htons (vring->size);
145   clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
146   mp->features = clib_host_to_net_u64 (vif->features);
147
148   mp->context = context;
149   vl_api_send_msg (reg, (u8 *) mp);
150 }
151
152 static void
153   vl_api_sw_interface_virtio_pci_dump_t_handler
154   (vl_api_sw_interface_virtio_pci_dump_t * mp)
155 {
156   vpe_api_main_t *am = &vpe_api_main;
157   vl_api_registration_t *reg;
158   virtio_main_t *vmx = &virtio_main;
159   virtio_if_t *vif;
160
161   reg = vl_api_client_index_to_registration (mp->client_index);
162   if (!reg)
163     return;
164
165   pool_foreach (vif, vmx->interfaces, (
166                                         {
167                                         if (vif->type == VIRTIO_IF_TYPE_PCI)
168                                         {
169                                         virtio_pci_send_sw_interface_details
170                                         (am, reg, vif, mp->context);}
171                                         }
172                 ));
173 }
174
175 #define vl_msg_name_crc_list
176 #include <vnet/vnet_all_api_h.h>
177 #undef vl_msg_name_crc_list
178
179 static void
180 setup_message_id_table (api_main_t * am)
181 {
182 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
183   foreach_vl_msg_name_crc_virtio;
184 #undef _
185 }
186
187 static clib_error_t *
188 virtio_pci_api_hookup (vlib_main_t * vm)
189 {
190   api_main_t *am = vlibapi_get_main ();
191
192 #define _(N,n)                                                  \
193     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
194                            vl_api_##n##_t_handler,              \
195                            vl_noop_handler,                     \
196                            vl_api_##n##_t_endian,               \
197                            vl_api_##n##_t_print,                \
198                            sizeof(vl_api_##n##_t), 1);
199   foreach_virtio_pci_api_msg;
200 #undef _
201
202   /*
203    * Set up the (msg_name, crc, message-id) table
204    */
205   setup_message_id_table (am);
206
207   return 0;
208 }
209
210 VLIB_API_INIT_FUNCTION (virtio_pci_api_hookup);
211
212 /*
213  * fd.io coding-style-patch-verification: ON
214  *
215  * Local Variables:
216  * eval: (c-set-style "gnu")
217  * End:
218  */