FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
virtio_api.c
Go to the documentation of this file.
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>
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 
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
55 {
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;
76  else
78 
79  ap->features = clib_net_to_host_u64 (mp->features);
80 
81  virtio_pci_create_if (vm, ap);
82 
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 
96 static void
98 {
99  vnet_main_t *vnm = vnet_get_main ();
101  virtio_main_t *vim = &virtio_main;
102  int rv = 0;
104  virtio_if_t *vif;
105  vl_api_virtio_pci_delete_reply_t *rmp;
107 
108  hw =
110  htonl (mp->sw_if_index));
111  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
112  {
113  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
114  goto reply;
115  }
116 
117  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
118 
119  rv = virtio_pci_delete_if (vm, vif);
120 
121 reply:
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);
132 }
133 
134 static void
136  vl_api_registration_t * reg,
137  virtio_if_t * vif, u32 context)
138 {
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);
145  pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
146  &mp->pci_addr);
147  mp->sw_if_index = htonl (vif->sw_if_index);
148  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);
152  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 
159 static void
162 {
165  virtio_main_t *vmx = &virtio_main;
166  virtio_if_t *vif;
167 
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 
186 static void
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 
194 static clib_error_t *
196 {
197  api_main_t *am = vlibapi_get_main ();
198 
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);
207 #undef _
208 
209  /*
210  * Set up the (msg_name, crc, message-id) table
211  */
213 
214  return 0;
215 }
216 
218 
219 /*
220  * fd.io coding-style-patch-verification: ON
221  *
222  * Local Variables:
223  * eval: (c-set-style "gnu")
224  * End:
225  */
virtio_if_t * interfaces
Definition: virtio.h:188
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
Reply for virtio pci create reply.
Definition: virtio.api:50
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
#define foreach_virtio_pci_api_msg
Definition: virtio_api.c:48
vnet_device_class_t virtio_device_class
vl_api_interface_index_t sw_if_index
Definition: virtio.api:66
Initialize a new virtio pci interface with the given parameters.
Definition: virtio.api:33
Dump virtio pci interfaces request.
Definition: virtio.api:70
void * vl_msg_api_alloc(int nbytes)
Delete virtio pci interface.
Definition: virtio.api:62
unsigned char u8
Definition: types.h:56
void virtio_pci_create_if(vlib_main_t *vm, virtio_pci_create_if_args_t *args)
Definition: pci.c:1166
VLIB_API_INIT_FUNCTION(virtio_pci_api_hookup)
static clib_error_t * virtio_pci_api_hookup(vlib_main_t *vm)
Definition: virtio_api.c:195
#define clib_memcpy(d, s, n)
Definition: string.h:180
u32 as_u32
Definition: virtio.h:132
u64 features
Definition: virtio.h:158
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
static void virtio_pci_send_sw_interface_details(vpe_api_main_t *am, vl_api_registration_t *reg, virtio_if_t *vif, u32 context)
Definition: virtio_api.c:135
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
unsigned int u32
Definition: types.h:88
static void vl_api_virtio_pci_delete_t_handler(vl_api_virtio_pci_delete_t *mp)
Definition: virtio_api.c:97
static void setup_message_id_table(api_main_t *am)
Definition: virtio_api.c:187
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
vl_api_mac_address_t mac_address
Definition: virtio.api:39
static void vl_api_virtio_pci_create_t_handler(vl_api_virtio_pci_create_t *mp)
Definition: virtio_api.c:54
pci_addr_t pci_addr
Definition: virtio.h:149
vlib_main_t * vm
Definition: in2out_ed.c:1810
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
virtio_vring_t * rxq_vrings
Definition: virtio.h:156
Reply for virtio pci interface dump request.
Definition: virtio.api:84
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
u8 mac_addr[6]
Definition: virtio.h:167
static void vl_api_sw_interface_virtio_pci_dump_t_handler(vl_api_sw_interface_virtio_pci_dump_t *mp)
Definition: virtio_api.c:161
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
int virtio_pci_delete_if(vlib_main_t *vm, virtio_if_t *vif)
Definition: pci.c:1344
virtio_main_t virtio_main
Definition: virtio.c:37
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_pci_address_t pci_addr
Definition: virtio.api:37
vl_api_interface_index_t sw_if_index
Definition: virtio.api:87
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:378
u32 sw_if_index
Definition: virtio.h:142
void pci_address_encode(const vlib_pci_addr_t *in, vl_api_pci_address_t *out)
Definition: pci_types_api.c:43
void pci_address_decode(const vl_api_pci_address_t *in, vlib_pci_addr_t *out)
Definition: pci_types_api.c:34
vpe_api_main_t vpe_api_main
Definition: interface_api.c:54
vl_api_interface_index_t sw_if_index
Definition: virtio.api:54
virtio_vring_t * txq_vrings
Definition: virtio.h:157