blob: d2302fa1dc404506cc62f49913098e3a9cae78d2 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <net/if.h>
Tom Jones61b4efc2024-01-30 09:06:47 +000022#ifdef __linux__
Damjan Marion8389fb92017-10-13 18:29:53 +020023#include <linux/if_tun.h>
Tom Jones61b4efc2024-01-30 09:06:47 +000024#elif __FreeBSD__
25#include <net/if_tun.h>
26#endif /* __linux__ */
Damjan Marion8389fb92017-10-13 18:29:53 +020027#include <sys/ioctl.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020028#include <sys/eventfd.h>
29
30#include <vlib/vlib.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020031#include <vlib/pci/pci.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020032#include <vlib/unix/unix.h>
33#include <vnet/ethernet/ethernet.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010034#include <vnet/ip/ip4_packet.h>
35#include <vnet/ip/ip6_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020036#include <vnet/devices/virtio/virtio.h>
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +020037#include <vnet/devices/virtio/virtio_inline.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020038#include <vnet/devices/virtio/pci.h>
Damjan Marion94100532020-11-06 23:25:57 +010039#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +000040#include <vnet/interface/tx_queue_funcs.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020041
42virtio_main_t virtio_main;
43
44#define _IOCTL(fd,a,...) \
45 if (ioctl (fd, a, __VA_ARGS__) < 0) \
46 { \
47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
48 goto error; \
49 }
50
51static clib_error_t *
52call_read_ready (clib_file_t * uf)
53{
Damjan Marion8389fb92017-10-13 18:29:53 +020054 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8389fb92017-10-13 18:29:53 +020055 u64 b;
56
57 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
Damjan Marion94100532020-11-06 23:25:57 +010058 vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
Damjan Marion8389fb92017-10-13 18:29:53 +020059
60 return 0;
61}
62
63
64clib_error_t *
65virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
66{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000067 vnet_virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +020068 int i;
69
70 if (!is_pow2 (sz))
71 return clib_error_return (0, "ring size must be power of 2");
72
73 if (sz > 32768)
74 return clib_error_return (0, "ring size must be 32768 or lower");
75
76 if (sz == 0)
77 sz = 256;
78
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000079 if (idx % 2)
80 {
81 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
82 CLIB_CACHE_LINE_BYTES);
83 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +000084 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000085 }
86 else
87 {
88 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
89 CLIB_CACHE_LINE_BYTES);
90 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
91 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000092 i = sizeof (vnet_virtio_vring_desc_t) * sz;
Damjan Marion8389fb92017-10-13 18:29:53 +020093 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
94 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040095 clib_memset (vring->desc, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +020096
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000097 i = sizeof (vnet_virtio_vring_avail_t) + sz * sizeof (vring->avail->ring[0]);
Damjan Marion8389fb92017-10-13 18:29:53 +020098 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
99 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400100 clib_memset (vring->avail, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200101 // tell kernel that we don't need interrupt
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200102 vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
Damjan Marion8389fb92017-10-13 18:29:53 +0200103
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000104 i = sizeof (vnet_virtio_vring_used_t) +
105 sz * sizeof (vnet_virtio_vring_used_elem_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200106 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
107 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400108 clib_memset (vring->used, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200109
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000110 vring->queue_id = idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200111 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100112 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200113
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100114 if (idx & 1)
115 {
116 clib_memset_u32 (vring->buffers, ~0, sz);
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000117 // tx path: suppress the interrupts from kernel
118 vring->call_fd = -1;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100119 }
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000120 else
121 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100122
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000123 vring->total_packets = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000124 vring->queue_size = sz;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100125 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
126 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000127 vring->queue_size, vring->call_fd, vring->kick_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200128
Damjan Marion7c6102b2019-11-08 17:59:56 +0100129 return 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200130}
131
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200132inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000133virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200134{
Stevena624dbe2018-01-09 11:13:29 -0800135 u16 used = vring->desc_in_use;
Steven074f6982018-03-30 22:18:11 -0700136 u16 last = vring->last_used_idx;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000137 u16 mask = vring->queue_size - 1;
Stevena624dbe2018-01-09 11:13:29 -0800138
139 while (used)
140 {
Steven074f6982018-03-30 22:18:11 -0700141 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
142 last++;
Stevena624dbe2018-01-09 11:13:29 -0800143 used--;
144 }
145}
146
147clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000148virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
Stevena624dbe2018-01-09 11:13:29 -0800149{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000150 vnet_virtio_vring_t *vring =
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000151 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Stevena624dbe2018-01-09 11:13:29 -0800152
153 clib_file_del_by_index (&file_main, vring->call_file_index);
154 close (vring->kick_fd);
155 close (vring->call_fd);
156 if (vring->used)
157 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100158 virtio_free_buffers (vm, vring);
Stevena624dbe2018-01-09 11:13:29 -0800159 clib_mem_free (vring->used);
160 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200161 if (vring->desc)
162 clib_mem_free (vring->desc);
163 if (vring->avail)
164 clib_mem_free (vring->avail);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000165 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000166 return 0;
167}
168
169clib_error_t *
170virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
171{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000172 vnet_virtio_vring_t *vring =
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000173 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
174
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000175 close (vring->kick_fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000176 if (vring->used)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200177 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100178 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000179 clib_mem_free (vring->used);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200180 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000181 if (vring->desc)
182 clib_mem_free (vring->desc);
183 if (vring->avail)
184 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200185 vec_free (vring->buffers);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200186 gro_flow_table_free (vring->flow_table);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000187 virtio_vring_buffering_free (vm, vring->buffering);
Mohsin Kazmi3f340172019-05-27 15:53:25 +0200188 clib_spinlock_free (&vring->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200189 return 0;
190}
191
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000192void
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200193virtio_set_packet_coalesce (virtio_if_t * vif)
194{
195 vnet_main_t *vnm = vnet_get_main ();
196 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000197 vnet_virtio_vring_t *vring;
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +0200198 vif->packet_coalesce = 1;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200199 vec_foreach (vring, vif->txq_vrings)
200 {
201 gro_flow_table_init (&vring->flow_table,
202 vif->type & (VIRTIO_IF_TYPE_TAP |
203 VIRTIO_IF_TYPE_PCI), hw->tx_node_index);
204 }
205}
206
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000207clib_error_t *
208virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size)
209{
210 vnet_main_t *vnm = vnet_get_main ();
211 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000212 vnet_virtio_vring_t *vring;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000213 clib_error_t *error = 0;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000214
215 vec_foreach (vring, vif->txq_vrings)
216 {
217 if ((error =
218 virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index,
219 buffering_size)))
220 {
221 break;
222 }
223 }
224
225 return error;
226}
227
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200228static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000229virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif,
230 vnet_virtio_vring_t *vring)
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200231{
232 if (vif->is_packed)
233 virtio_refill_vring_packed (vm, vif, vif->type, vring,
234 vif->virtio_net_hdr_sz,
235 virtio_input_node.index);
236 else
237 virtio_refill_vring_split (vm, vif, vif->type, vring,
238 vif->virtio_net_hdr_sz,
239 virtio_input_node.index);
240}
241
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200242void
Damjan Marion94100532020-11-06 23:25:57 +0100243virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif)
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000244{
245 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000246 vnet_virtio_vring_t *vring;
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100247 u32 i = 0;
Damjan Marion94100532020-11-06 23:25:57 +0100248
249 vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index);
250
251 vec_foreach (vring, vif->rxq_vrings)
252 {
253 vring->queue_index = vnet_hw_if_register_rx_queue (
254 vnm, vif->hw_if_index, RX_QUEUE_ACCESS (vring->queue_id),
255 VNET_HW_IF_RXQ_THREAD_ANY);
256 vring->buffer_pool_index = vlib_buffer_pool_get_default_for_numa (
257 vm, vnet_hw_if_get_rx_queue_numa_node (vnm, vring->queue_index));
258 if (vif->type == VIRTIO_IF_TYPE_TAP || vif->type == VIRTIO_IF_TYPE_TUN)
259 {
260
261 clib_file_t f = {
262 .read_function = call_read_ready,
263 .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
264 .file_descriptor = vring->call_fd,
265 .private_data = vring->queue_index,
266 .description = format (0, "%U vring %u", format_virtio_device_name,
267 vif->dev_instance, vring->queue_id),
268 };
269
270 vring->call_file_index = clib_file_add (&file_main, &f);
271 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
272 vring->call_file_index);
273 }
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100274 else if ((vif->type == VIRTIO_IF_TYPE_PCI) && (vif->support_int_mode) &&
275 (vif->msix_enabled == VIRTIO_MSIX_ENABLED))
276 {
277 u32 file_index;
278 file_index =
279 vlib_pci_get_msix_file_index (vm, vif->pci_dev_handle, i + 1);
280 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
281 file_index);
282 i++;
283 }
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200284 vnet_hw_if_set_rx_queue_mode (vnm, vring->queue_index,
285 VNET_HW_IF_RX_MODE_POLLING);
286 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
287 virtio_vring_fill (vm, vif, vring);
Damjan Marion94100532020-11-06 23:25:57 +0100288 }
289 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000290}
291
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000292void
293virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif)
294{
295 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000296 vnet_virtio_vring_t *vring;
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000297
298 vec_foreach (vring, vif->txq_vrings)
299 {
300 vring->queue_index = vnet_hw_if_register_tx_queue (
301 vnm, vif->hw_if_index, TX_QUEUE_ACCESS (vring->queue_id));
302 }
303
Steven Luongc4a48f22022-01-19 09:08:27 -0800304 if (vif->num_txqs == 0)
305 {
306 virtio_log_error (vif, "Interface %U has 0 txq",
307 format_vnet_hw_if_index_name, vnm, vif->hw_if_index);
308 return;
309 }
310
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000311 for (u32 j = 0; j < vlib_get_n_threads (); j++)
312 {
313 u32 qi = vif->txq_vrings[j % vif->num_txqs].queue_index;
314 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
315 }
316
317 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
318}
319
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200320inline void
321virtio_set_net_hdr_size (virtio_if_t * vif)
322{
323 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
324 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000325 vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_v1_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200326 else
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000327 vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200328}
329
330inline void
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000331virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
332 virtio_if_type_t type)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200333{
334 u32 i, j, hw_if_index;
335 virtio_if_t *vif;
336 vnet_main_t *vnm = &vnet_main;
337 virtio_main_t *mm = &virtio_main;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000338 vnet_virtio_vring_t *vring;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200339 struct feat_struct
340 {
341 u8 bit;
342 char *str;
343 };
344 struct feat_struct *feat_entry;
345
346 static struct feat_struct feat_array[] = {
347#define _(s,b) { .str = #s, .bit = b, },
348 foreach_virtio_net_features
349#undef _
350 {.str = NULL}
351 };
352
353 struct feat_struct *flag_entry;
354 static struct feat_struct flags_array[] = {
355#define _(b,e,s) { .bit = b, .str = s, },
356 foreach_virtio_if_flag
357#undef _
358 {.str = NULL}
359 };
360
361 if (!hw_if_indices)
362 return;
363
364 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
365 {
366 vnet_hw_interface_t *hi =
367 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
368 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
369 if (vif->type != type)
370 continue;
371 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
372 format_vnet_hw_if_index_name, vnm,
373 hw_if_indices[hw_if_index], vif->hw_if_index);
374 if (type == VIRTIO_IF_TYPE_PCI)
375 {
376 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
377 &vif->pci_addr);
378 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200379 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200380 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100381 u8 *str = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200382 if (vif->host_if_name)
383 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
384 if (vif->net_ns)
385 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200386 if (vif->host_mtu_size)
387 vlib_cli_output (vm, " host-mtu-size \"%d\"",
388 vif->host_mtu_size);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200389 if (type == VIRTIO_IF_TYPE_TAP)
390 vlib_cli_output (vm, " host-mac-addr: %U",
391 format_ethernet_address, vif->host_mac_addr);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500392 vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100393
394 vec_foreach_index (i, vif->vhost_fds)
395 str = format (str, " %d", vif->vhost_fds[i]);
396 vlib_cli_output (vm, " vhost-fds%v", str);
397 vec_free (str);
Aloys Augustin2857e782020-03-16 17:29:52 +0100398 vec_foreach_index (i, vif->tap_fds)
399 str = format (str, " %d", vif->tap_fds[i]);
400 vlib_cli_output (vm, " tap-fds%v", str);
401 vec_free (str);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200402 }
Chenmin Sun7c615ae2019-07-18 23:19:28 +0800403 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100404 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200405 vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000406 vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200407 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
408 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
409 vif->mac_addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200410 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
411 vlib_cli_output (vm, " flags 0x%x", vif->flags);
412 flag_entry = (struct feat_struct *) &flags_array;
413 while (flag_entry->str)
414 {
415 if (vif->flags & (1ULL << flag_entry->bit))
416 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
417 flag_entry->bit);
418 flag_entry++;
419 }
420 if (type == VIRTIO_IF_TYPE_PCI)
421 {
422 device_status (vm, vif);
423 }
424 vlib_cli_output (vm, " features 0x%lx", vif->features);
425 feat_entry = (struct feat_struct *) &feat_array;
426 while (feat_entry->str)
427 {
428 if (vif->features & (1ULL << feat_entry->bit))
429 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
430 feat_entry->bit);
431 feat_entry++;
432 }
433 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
434 feat_entry = (struct feat_struct *) &feat_array;
435 while (feat_entry->str)
436 {
437 if (vif->remote_features & (1ULL << feat_entry->bit))
438 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
439 feat_entry->bit);
440 feat_entry++;
441 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000442 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
443 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000444 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
445 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000446 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
447 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200448 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000449 vring = vec_elt_at_index (vif->rxq_vrings, i);
450 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000451 vlib_cli_output (
452 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
453 vring->queue_size, vring->last_used_idx, vring->desc_next,
454 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100455 if (vif->is_packed)
456 {
457 vlib_cli_output (vm,
458 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
459 vring->driver_event->flags,
460 vring->driver_event->off_wrap,
461 vring->device_event->flags,
462 vring->device_event->off_wrap);
463 vlib_cli_output (vm,
464 " avail wrap counter %d, used wrap counter %d",
465 vring->avail_wrap_counter,
466 vring->used_wrap_counter);
467 }
468 else
469 vlib_cli_output (vm,
470 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
471 vring->avail->flags, vring->avail->idx,
472 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200473 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200474 {
475 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
476 vring->call_fd);
477 }
478 if (show_descr)
479 {
480 vlib_cli_output (vm, "\n descriptor table:\n");
481 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100482 " id addr len flags next/id user_addr\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200483 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100484 " ===== ================== ===== ====== ======= ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000485 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200486 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100487 if (vif->is_packed)
488 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000489 vnet_virtio_vring_packed_desc_t *desc =
490 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100491 vlib_cli_output (vm,
492 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
493 j, desc->addr,
494 desc->len,
495 desc->flags, desc->id, desc->addr);
496 }
497 else
498 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000499 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100500 vlib_cli_output (vm,
501 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
502 j, desc->addr,
503 desc->len,
504 desc->flags, desc->next, desc->addr);
505 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200506 }
507 }
508 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000509 vec_foreach_index (i, vif->txq_vrings)
510 {
511 vring = vec_elt_at_index (vif->txq_vrings, i);
512 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000513 vlib_cli_output (
514 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
515 vring->queue_size, vring->last_used_idx, vring->desc_next,
516 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100517 if (vif->is_packed)
518 {
519 vlib_cli_output (vm,
520 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
521 vring->driver_event->flags,
522 vring->driver_event->off_wrap,
523 vring->device_event->flags,
524 vring->device_event->off_wrap);
525 vlib_cli_output (vm,
526 " avail wrap counter %d, used wrap counter %d",
527 vring->avail_wrap_counter,
528 vring->used_wrap_counter);
529 }
530 else
531 vlib_cli_output (vm,
532 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
533 vring->avail->flags, vring->avail->idx,
534 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200535 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000536 {
537 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
538 vring->call_fd);
539 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200540 if (vring->flow_table)
541 {
542 vlib_cli_output (vm, " %U", gro_flow_table_format,
543 vring->flow_table);
544 }
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000545 if (vif->packet_buffering)
546 {
547 vlib_cli_output (vm, " %U", virtio_vring_buffering_format,
548 vring->buffering);
549 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000550 if (show_descr)
551 {
552 vlib_cli_output (vm, "\n descriptor table:\n");
553 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100554 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000555 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100556 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000557 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000558 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100559 if (vif->is_packed)
560 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000561 vnet_virtio_vring_packed_desc_t *desc =
562 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100563 vlib_cli_output (vm,
564 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
565 j, desc->addr,
566 desc->len,
567 desc->flags, desc->id, desc->addr);
568 }
569 else
570 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000571 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100572 vlib_cli_output (vm,
573 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
574 j, desc->addr,
575 desc->len,
576 desc->flags, desc->next, desc->addr);
577 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000578 }
579 }
580 }
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000581 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
582 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000583 {
584 vring = vif->cxq_vring;
585 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000586 vlib_cli_output (
587 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
588 vring->queue_size, vring->last_used_idx, vring->desc_next,
589 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100590 if (vif->is_packed)
591 {
592 vlib_cli_output (vm,
593 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
594 vring->driver_event->flags,
595 vring->driver_event->off_wrap,
596 vring->device_event->flags,
597 vring->device_event->off_wrap);
598 vlib_cli_output (vm,
599 " avail wrap counter %d, used wrap counter %d",
600 vring->avail_wrap_counter,
601 vring->used_wrap_counter);
602 }
603 else
604 {
605 vlib_cli_output (vm,
606 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
607 vring->avail->flags, vring->avail->idx,
608 vring->used->flags, vring->used->idx);
609 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000610 if (show_descr)
611 {
612 vlib_cli_output (vm, "\n descriptor table:\n");
613 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100614 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000615 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100616 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000617 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000618 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100619 if (vif->is_packed)
620 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000621 vnet_virtio_vring_packed_desc_t *desc =
622 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100623 vlib_cli_output (vm,
624 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
625 j, desc->addr,
626 desc->len,
627 desc->flags, desc->id, desc->addr);
628 }
629 else
630 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000631 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100632 vlib_cli_output (vm,
633 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
634 j, desc->addr,
635 desc->len,
636 desc->flags, desc->next, desc->addr);
637 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000638 }
639 }
640 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200641 }
642
643}
644
Damjan Marionf41244f2019-11-08 17:41:06 +0100645static clib_error_t *
646virtio_init (vlib_main_t * vm)
647{
648 virtio_main_t *vim = &virtio_main;
649 clib_error_t *error = 0;
650
651 vim->log_default = vlib_log_register_class ("virtio", 0);
652 vlib_log_debug (vim->log_default, "initialized");
653
654 return error;
655}
656
657VLIB_INIT_FUNCTION (virtio_init);
658
Damjan Marion8389fb92017-10-13 18:29:53 +0200659/*
660 * fd.io coding-style-patch-verification: ON
661 *
662 * Local Variables:
663 * eval: (c-set-style "gnu")
664 * End:
665 */