FD.io VPP  v21.10.1-2-g0a485f517
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>
27 #include <vlib/pci/pci_types_api.h>
28 
29 #include <vnet/format_fns.h>
30 #include <vnet/devices/virtio/virtio.api_enum.h>
31 #include <vnet/devices/virtio/virtio.api_types.h>
32 
33 #define REPLY_MSG_ID_BASE virtio_main.msg_id_base
35 
36 /* It will be deprecated in 21.01 */
37 static void
39 {
43  virtio_pci_create_if_args_t _a, *ap = &_a;
44 
45  clib_memset (ap, 0, sizeof (*ap));
46 
47  pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
48  if (!mp->use_random_mac)
49  {
50  clib_memcpy (ap->mac_addr, mp->mac_address, 6);
51  ap->mac_addr_set = 1;
52  }
53  ap->sw_if_index = (u32) ~ 0;
54  if (mp->gso_enabled)
55  ap->gso_enabled = 1;
56  else
57  ap->gso_enabled = 0;
60  else
62 
63  ap->features = clib_net_to_host_u64 (mp->features);
64 
66 
68  if (!reg)
69  return;;
70 
71  rmp = vl_msg_api_alloc (sizeof (*rmp));
72  rmp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_VIRTIO_PCI_CREATE_REPLY);
73  rmp->context = mp->context;
74  rmp->retval = htonl (ap->rv);
75  rmp->sw_if_index = htonl (ap->sw_if_index);
76 
77  vl_api_send_msg (reg, (u8 *) rmp);
78 }
79 
80 static void
82 {
86  virtio_pci_create_if_args_t _a, *ap = &_a;
87 
88  clib_memset (ap, 0, sizeof (*ap));
89 
90  pci_address_decode (&mp->pci_addr, (vlib_pci_addr_t *) & ap->addr);
91  if (!mp->use_random_mac)
92  {
93  clib_memcpy (ap->mac_addr, mp->mac_address, 6);
94  ap->mac_addr_set = 1;
95  }
96  ap->sw_if_index = (u32) ~ 0;
97 
98  STATIC_ASSERT (((int) VIRTIO_API_FLAG_GSO == (int) VIRTIO_FLAG_GSO),
99  "virtio gso api flag mismatch");
101  (int) VIRTIO_FLAG_CSUM_OFFLOAD),
102  "virtio checksum offload api flag mismatch");
104  (int) VIRTIO_FLAG_GRO_COALESCE),
105  "virtio gro coalesce api flag mismatch");
106  STATIC_ASSERT (((int) VIRTIO_API_FLAG_PACKED == (int) VIRTIO_FLAG_PACKED),
107  "virtio packed api flag mismatch");
109  (int) VIRTIO_FLAG_IN_ORDER),
110  "virtio in-order api flag mismatch");
112  (int) VIRTIO_FLAG_BUFFERING),
113  "virtio buffering api flag mismatch");
114 
115  ap->virtio_flags = clib_net_to_host_u32 (mp->virtio_flags);
116  ap->features = clib_net_to_host_u64 (mp->features);
117 
119  ap->gso_enabled = 1;
120  else
121  ap->gso_enabled = 0;
123  ap->checksum_offload_enabled = 1;
124  else
125  ap->checksum_offload_enabled = 0;
126 
127  virtio_pci_create_if (vm, ap);
128 
130  if (!reg)
131  return;;
132 
133  rmp = vl_msg_api_alloc (sizeof (*rmp));
134  rmp->_vl_msg_id =
135  htons (REPLY_MSG_ID_BASE + VL_API_VIRTIO_PCI_CREATE_V2_REPLY);
136  rmp->context = mp->context;
137  rmp->retval = htonl (ap->rv);
138  rmp->sw_if_index = htonl (ap->sw_if_index);
139 
140  vl_api_send_msg (reg, (u8 *) rmp);
141 }
142 
143 static void
145 {
146  vnet_main_t *vnm = vnet_get_main ();
148  virtio_main_t *vim = &virtio_main;
149  int rv = 0;
151  virtio_if_t *vif;
152  vl_api_virtio_pci_delete_reply_t *rmp;
154 
155  hw =
157  htonl (mp->sw_if_index));
158  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
159  {
160  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
161  goto reply;
162  }
163 
164  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);
165 
166  rv = virtio_pci_delete_if (vm, vif);
167 
168 reply:
170  if (!reg)
171  return;
172 
173  rmp = vl_msg_api_alloc (sizeof (*rmp));
174  rmp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_VIRTIO_PCI_DELETE_REPLY);
175  rmp->context = mp->context;
176  rmp->retval = htonl (rv);
177 
178  vl_api_send_msg (reg, (u8 *) rmp);
179 }
180 
181 static void
183  vl_api_registration_t * reg,
184  virtio_if_t * vif, u32 context)
185 {
187  mp = vl_msg_api_alloc (sizeof (*mp));
188 
189  clib_memset (mp, 0, sizeof (*mp));
190 
191  mp->_vl_msg_id =
192  htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_VIRTIO_PCI_DETAILS);
193  pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32,
194  &mp->pci_addr);
195  mp->sw_if_index = htonl (vif->sw_if_index);
196  virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0);
197  mp->rx_ring_sz = htons (vring->size);
198  vring = vec_elt_at_index (vif->txq_vrings, 0);
199  mp->tx_ring_sz = htons (vring->size);
200  clib_memcpy (mp->mac_addr, vif->mac_addr, 6);
201  mp->features = clib_host_to_net_u64 (vif->features);
202 
203  mp->context = context;
204  vl_api_send_msg (reg, (u8 *) mp);
205 }
206 
207 static void
210 {
213  virtio_main_t *vmx = &virtio_main;
214  virtio_if_t *vif;
215 
217  if (!reg)
218  return;
219 
220  pool_foreach (vif, vmx->interfaces)
221  {
222  if (vif->type == VIRTIO_IF_TYPE_PCI)
223  {
225  }
226  }
227 }
228 
229 #include <vnet/devices/virtio/virtio.api.c>
230 
231 static clib_error_t *
233 {
234  /*
235  * Set up the (msg_name, crc, message-id) table
236  */
238 
239  return 0;
240 }
241 
243 
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "gnu")
249  * End:
250  */
vl_api_sw_interface_virtio_pci_dump_t::context
u32 context
Definition: virtio.api:116
virtio_device_class
vnet_device_class_t virtio_device_class
pci.h
virtio_pci_create_if
void virtio_pci_create_if(vlib_main_t *vm, virtio_pci_create_if_args_t *args)
Definition: pci.c:1348
vl_api_virtio_pci_create_v2_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: virtio.api:97
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
virtio_pci_api_hookup
static clib_error_t * virtio_pci_api_hookup(vlib_main_t *vm)
Definition: virtio_api.c:232
vl_api_sw_interface_virtio_pci_details_t::tx_ring_sz
u16 tx_ring_sz
Definition: virtio.api:133
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
virtio_vring_t
Definition: virtio.h:63
virtio.h
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
vl_api_virtio_pci_create_v2_t::pci_addr
vl_api_pci_address_t pci_addr
Definition: virtio.api:81
virtio_if_t::mac_addr
u8 mac_addr[6]
Definition: virtio.h:171
vl_api_virtio_pci_create_reply_t
Reply for virtio pci create reply.
Definition: virtio.api:51
VIRTIO_API_FLAG_CSUM_OFFLOAD
@ VIRTIO_API_FLAG_CSUM_OFFLOAD
Definition: virtio.api:61
vpe_api_main_t
Definition: api_helper_macros.h:414
vl_api_virtio_pci_create_v2_t_handler
static void vl_api_virtio_pci_create_v2_t_handler(vl_api_virtio_pci_create_v2_t *mp)
Definition: virtio_api.c:81
virtio_pci_create_if_args_t::gso_enabled
u8 gso_enabled
Definition: pci.h:239
vl_api_virtio_pci_create_t::gso_enabled
bool gso_enabled
Definition: virtio.api:41
virtio_pci_create_if_args_t::mac_addr_set
u8 mac_addr_set
Definition: pci.h:236
vl_api_virtio_pci_create_v2_t::features
u64 features
Definition: virtio.api:85
am
app_main_t * am
Definition: application.c:489
vl_api_virtio_pci_create_v2_t::mac_address
vl_api_mac_address_t mac_address
Definition: virtio.api:83
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_virtio_pci_create_t::features
u64 features
Definition: virtio.api:43
vl_api_virtio_pci_create_v2_reply_t::context
u32 context
Definition: virtio.api:95
vnet_hw_interface_t::dev_instance
u32 dev_instance
Definition: interface.h:660
vl_api_virtio_pci_create_t::pci_addr
vl_api_pci_address_t pci_addr
Definition: virtio.api:38
vl_api_sw_interface_virtio_pci_dump_t_handler
static void vl_api_sw_interface_virtio_pci_dump_t_handler(vl_api_sw_interface_virtio_pci_dump_t *mp)
Definition: virtio_api.c:209
virtio_vring_t::size
u16 size
Definition: virtio.h:83
VIRTIO_API_FLAG_PACKED
@ VIRTIO_API_FLAG_PACKED
Definition: virtio.api:63
virtio_pci_create_if_args_t::features
u64 features
Definition: pci.h:238
virtio_main
virtio_main_t virtio_main
Definition: virtio.c:37
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
VIRTIO_API_FLAG_GRO_COALESCE
@ VIRTIO_API_FLAG_GRO_COALESCE
Definition: virtio.api:62
vl_api_virtio_pci_create_t::use_random_mac
bool use_random_mac
Definition: virtio.api:39
vl_api_virtio_pci_create_t
Initialize a new virtio pci interface with the given parameters.
Definition: virtio.api:33
vl_api_sw_interface_virtio_pci_dump_t
Dump virtio pci interfaces request.
Definition: virtio.api:113
virtio_if_t::txq_vrings
virtio_vring_t * txq_vrings
Definition: virtio.h:137
virtio_if_t::pci_addr
pci_addr_t pci_addr
Definition: virtio.h:196
virtio_pci_delete_if
int virtio_pci_delete_if(vlib_main_t *vm, virtio_if_t *vif)
Definition: pci.c:1548
vl_api_sw_interface_virtio_pci_dump_t::client_index
u32 client_index
Definition: virtio.api:115
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
vl_api_sw_interface_virtio_pci_details_t::pci_addr
vl_api_pci_address_t pci_addr
Definition: virtio.api:131
VIRTIO_API_FLAG_IN_ORDER
@ VIRTIO_API_FLAG_IN_ORDER
Definition: virtio.api:64
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
virtio_if_t::features
u64 features
Definition: virtio.h:131
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
vnet_get_sup_hw_interface_api_visible_or_null
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:101
vnet_hw_interface_t::dev_class_index
u32 dev_class_index
Definition: interface.h:659
virtio_pci_create_if_args_t::addr
u32 addr
Definition: pci.h:232
vl_api_sw_interface_virtio_pci_details_t::context
u32 context
Definition: virtio.api:129
virtio_pci_create_if_args_t::virtio_flags
u32 virtio_flags
Definition: pci.h:242
pci_address_encode
void pci_address_encode(const vlib_pci_addr_t *in, vl_api_pci_address_t *out)
Definition: pci_types_api.c:31
virtio_pci_create_if_args_t::checksum_offload_enabled
u8 checksum_offload_enabled
Definition: pci.h:240
interface.h
virtio_pci_create_if_args_t::rv
i32 rv
Definition: pci.h:234
VIRTIO_API_FLAG_GSO
@ VIRTIO_API_FLAG_GSO
Definition: virtio.api:60
vl_api_virtio_pci_delete_t
Delete virtio pci interface.
Definition: virtio.api:105
vl_api_sw_interface_virtio_pci_details_t
Reply for virtio pci interface dump request.
Definition: virtio.api:127
vl_api_virtio_pci_create_v2_reply_t::retval
i32 retval
Definition: virtio.api:96
virtio_pci_create_if_args_t::sw_if_index
u32 sw_if_index
Definition: pci.h:235
vl_api_sw_interface_virtio_pci_details_t::rx_ring_sz
u16 rx_ring_sz
Definition: virtio.api:134
STATIC_ASSERT
#define STATIC_ASSERT(truth,...)
Definition: error_bootstrap.h:111
vl_api_virtio_pci_create_t::context
u32 context
Definition: virtio.api:37
virtio_if_t::sw_if_index
u32 sw_if_index
Definition: virtio.h:153
vl_api_virtio_pci_create_reply_t::retval
i32 retval
Definition: virtio.api:55
virtio_main_t
Definition: virtio.h:214
vl_api_virtio_pci_delete_t_handler
static void vl_api_virtio_pci_delete_t_handler(vl_api_virtio_pci_delete_t *mp)
Definition: virtio_api.c:144
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
vl_api_virtio_pci_delete_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: virtio.api:109
vl_api_virtio_pci_create_v2_t::use_random_mac
bool use_random_mac
Definition: virtio.api:82
vl_api_virtio_pci_delete_t::client_index
u32 client_index
Definition: virtio.api:107
vl_api_sw_interface_virtio_pci_details_t::mac_addr
vl_api_mac_address_t mac_addr
Definition: virtio.api:132
format_fns.h
vl_api_virtio_pci_delete_t::context
u32 context
Definition: virtio.api:108
virtio_pci_create_if_args_t::mac_addr
u8 mac_addr[6]
Definition: pci.h:237
vl_api_virtio_pci_create_v2_reply_t
Reply for virtio pci create reply.
Definition: virtio.api:93
u32
unsigned int u32
Definition: types.h:88
vl_api_virtio_pci_create_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: virtio.api:56
api_helper_macros.h
virtio_pci_send_sw_interface_details
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:182
vl_api_virtio_pci_create_t::checksum_offload_enabled
bool checksum_offload_enabled
Definition: virtio.api:42
virtio_if_t::type
virtio_if_type_t type
Definition: virtio.h:150
virtio_if_t
Definition: virtio.h:128
vl_api_virtio_pci_create_t_handler
static void vl_api_virtio_pci_create_t_handler(vl_api_virtio_pci_create_t *mp)
Definition: virtio_api.c:38
vl_api_sw_interface_virtio_pci_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: virtio.api:130
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(virtio_pci_api_hookup)
virtio_if_t::rxq_vrings
virtio_vring_t * rxq_vrings
Definition: virtio.h:136
pci_address_decode
void pci_address_decode(const vl_api_pci_address_t *in, vlib_pci_addr_t *out)
Definition: pci_types_api.c:22
virtio_main_t::interfaces
virtio_if_t * interfaces
Definition: virtio.h:220
virtio_pci_create_if_args_t
Definition: pci.h:230
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vl_api_virtio_pci_create_v2_t::virtio_flags
vl_api_virtio_flags_t virtio_flags
Definition: virtio.api:84
vl_api_virtio_pci_create_reply_t::context
u32 context
Definition: virtio.api:54
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_api_virtio_pci_create_t::mac_address
vl_api_mac_address_t mac_address
Definition: virtio.api:40
vl_api_virtio_pci_create_v2_t::context
u32 context
Definition: virtio.api:80
vl_api_virtio_pci_create_t::client_index
u32 client_index
Definition: virtio.api:36
vl_api_virtio_pci_create_v2_t::client_index
u32 client_index
Definition: virtio.api:79
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
pci_types_api.h
pci_addr_t::as_u32
u32 as_u32
Definition: virtio.h:122
vnet.h
api_errno.h
vl_api_virtio_pci_create_v2_t
Initialize a new virtio pci interface with the given parameters.
Definition: virtio.api:77
VIRTIO_API_FLAG_BUFFERING
@ VIRTIO_API_FLAG_BUFFERING
Definition: virtio.api:65
REPLY_MSG_ID_BASE
#define REPLY_MSG_ID_BASE
Definition: virtio_api.c:33
vpe_api_main
vpe_api_main_t vpe_api_main
Definition: interface_api.c:47
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
vl_api_sw_interface_virtio_pci_details_t::features
u64 features
Definition: virtio.api:135