blob: 33af8b8c4559aa01aa50cc1bd8296ecee21ff4f5 [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>
22#include <linux/if_tun.h>
23#include <sys/ioctl.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020024#include <sys/eventfd.h>
25
26#include <vlib/vlib.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020027#include <vlib/pci/pci.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020028#include <vlib/unix/unix.h>
29#include <vnet/ethernet/ethernet.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010030#include <vnet/ip/ip4_packet.h>
31#include <vnet/ip/ip6_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020032#include <vnet/devices/virtio/virtio.h>
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +020033#include <vnet/devices/virtio/virtio_inline.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020034#include <vnet/devices/virtio/pci.h>
Damjan Marion94100532020-11-06 23:25:57 +010035#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +000036#include <vnet/interface/tx_queue_funcs.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020037
38virtio_main_t virtio_main;
39
40#define _IOCTL(fd,a,...) \
41 if (ioctl (fd, a, __VA_ARGS__) < 0) \
42 { \
43 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
44 goto error; \
45 }
46
47static clib_error_t *
48call_read_ready (clib_file_t * uf)
49{
Damjan Marion8389fb92017-10-13 18:29:53 +020050 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8389fb92017-10-13 18:29:53 +020051 u64 b;
52
53 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
Damjan Marion94100532020-11-06 23:25:57 +010054 vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
Damjan Marion8389fb92017-10-13 18:29:53 +020055
56 return 0;
57}
58
59
60clib_error_t *
61virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
62{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000063 vnet_virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +020064 int i;
65
66 if (!is_pow2 (sz))
67 return clib_error_return (0, "ring size must be power of 2");
68
69 if (sz > 32768)
70 return clib_error_return (0, "ring size must be 32768 or lower");
71
72 if (sz == 0)
73 sz = 256;
74
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000075 if (idx % 2)
76 {
77 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
78 CLIB_CACHE_LINE_BYTES);
79 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +000080 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000081 }
82 else
83 {
84 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
85 CLIB_CACHE_LINE_BYTES);
86 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
87 }
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000088 i = sizeof (vnet_virtio_vring_desc_t) * sz;
Damjan Marion8389fb92017-10-13 18:29:53 +020089 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
90 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040091 clib_memset (vring->desc, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +020092
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000093 i = sizeof (vnet_virtio_vring_avail_t) + sz * sizeof (vring->avail->ring[0]);
Damjan Marion8389fb92017-10-13 18:29:53 +020094 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
95 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040096 clib_memset (vring->avail, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +020097 // tell kernel that we don't need interrupt
Mohsin Kazmia7a22812020-08-31 17:17:16 +020098 vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
Damjan Marion8389fb92017-10-13 18:29:53 +020099
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000100 i = sizeof (vnet_virtio_vring_used_t) +
101 sz * sizeof (vnet_virtio_vring_used_elem_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200102 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
103 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400104 clib_memset (vring->used, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200105
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000106 vring->queue_id = idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200107 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100108 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200109
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100110 if (idx & 1)
111 {
112 clib_memset_u32 (vring->buffers, ~0, sz);
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000113 // tx path: suppress the interrupts from kernel
114 vring->call_fd = -1;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100115 }
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000116 else
117 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100118
Mohsin Kazmidd0144a2022-09-14 11:25:54 +0000119 vring->total_packets = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000120 vring->queue_size = sz;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100121 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
122 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000123 vring->queue_size, vring->call_fd, vring->kick_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200124
Damjan Marion7c6102b2019-11-08 17:59:56 +0100125 return 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200126}
127
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200128inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000129virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200130{
Stevena624dbe2018-01-09 11:13:29 -0800131 u16 used = vring->desc_in_use;
Steven074f6982018-03-30 22:18:11 -0700132 u16 last = vring->last_used_idx;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000133 u16 mask = vring->queue_size - 1;
Stevena624dbe2018-01-09 11:13:29 -0800134
135 while (used)
136 {
Steven074f6982018-03-30 22:18:11 -0700137 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
138 last++;
Stevena624dbe2018-01-09 11:13:29 -0800139 used--;
140 }
141}
142
143clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000144virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
Stevena624dbe2018-01-09 11:13:29 -0800145{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000146 vnet_virtio_vring_t *vring =
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000147 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Stevena624dbe2018-01-09 11:13:29 -0800148
149 clib_file_del_by_index (&file_main, vring->call_file_index);
150 close (vring->kick_fd);
151 close (vring->call_fd);
152 if (vring->used)
153 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100154 virtio_free_buffers (vm, vring);
Stevena624dbe2018-01-09 11:13:29 -0800155 clib_mem_free (vring->used);
156 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200157 if (vring->desc)
158 clib_mem_free (vring->desc);
159 if (vring->avail)
160 clib_mem_free (vring->avail);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000161 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000162 return 0;
163}
164
165clib_error_t *
166virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
167{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000168 vnet_virtio_vring_t *vring =
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000169 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
170
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000171 close (vring->kick_fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000172 if (vring->used)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200173 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100174 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000175 clib_mem_free (vring->used);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200176 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000177 if (vring->desc)
178 clib_mem_free (vring->desc);
179 if (vring->avail)
180 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200181 vec_free (vring->buffers);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200182 gro_flow_table_free (vring->flow_table);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000183 virtio_vring_buffering_free (vm, vring->buffering);
Mohsin Kazmi3f340172019-05-27 15:53:25 +0200184 clib_spinlock_free (&vring->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200185 return 0;
186}
187
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000188void
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200189virtio_set_packet_coalesce (virtio_if_t * vif)
190{
191 vnet_main_t *vnm = vnet_get_main ();
192 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000193 vnet_virtio_vring_t *vring;
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +0200194 vif->packet_coalesce = 1;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200195 vec_foreach (vring, vif->txq_vrings)
196 {
197 gro_flow_table_init (&vring->flow_table,
198 vif->type & (VIRTIO_IF_TYPE_TAP |
199 VIRTIO_IF_TYPE_PCI), hw->tx_node_index);
200 }
201}
202
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000203clib_error_t *
204virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size)
205{
206 vnet_main_t *vnm = vnet_get_main ();
207 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000208 vnet_virtio_vring_t *vring;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000209 clib_error_t *error = 0;
210 vif->packet_buffering = 1;
211
212 vec_foreach (vring, vif->txq_vrings)
213 {
214 if ((error =
215 virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index,
216 buffering_size)))
217 {
218 break;
219 }
220 }
221
222 return error;
223}
224
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200225static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000226virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif,
227 vnet_virtio_vring_t *vring)
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200228{
229 if (vif->is_packed)
230 virtio_refill_vring_packed (vm, vif, vif->type, vring,
231 vif->virtio_net_hdr_sz,
232 virtio_input_node.index);
233 else
234 virtio_refill_vring_split (vm, vif, vif->type, vring,
235 vif->virtio_net_hdr_sz,
236 virtio_input_node.index);
237}
238
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200239void
Damjan Marion94100532020-11-06 23:25:57 +0100240virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif)
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000241{
242 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000243 vnet_virtio_vring_t *vring;
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100244 u32 i = 0;
Damjan Marion94100532020-11-06 23:25:57 +0100245
246 vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index);
247
248 vec_foreach (vring, vif->rxq_vrings)
249 {
250 vring->queue_index = vnet_hw_if_register_rx_queue (
251 vnm, vif->hw_if_index, RX_QUEUE_ACCESS (vring->queue_id),
252 VNET_HW_IF_RXQ_THREAD_ANY);
253 vring->buffer_pool_index = vlib_buffer_pool_get_default_for_numa (
254 vm, vnet_hw_if_get_rx_queue_numa_node (vnm, vring->queue_index));
255 if (vif->type == VIRTIO_IF_TYPE_TAP || vif->type == VIRTIO_IF_TYPE_TUN)
256 {
257
258 clib_file_t f = {
259 .read_function = call_read_ready,
260 .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
261 .file_descriptor = vring->call_fd,
262 .private_data = vring->queue_index,
263 .description = format (0, "%U vring %u", format_virtio_device_name,
264 vif->dev_instance, vring->queue_id),
265 };
266
267 vring->call_file_index = clib_file_add (&file_main, &f);
268 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
269 vring->call_file_index);
270 }
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100271 else if ((vif->type == VIRTIO_IF_TYPE_PCI) && (vif->support_int_mode) &&
272 (vif->msix_enabled == VIRTIO_MSIX_ENABLED))
273 {
274 u32 file_index;
275 file_index =
276 vlib_pci_get_msix_file_index (vm, vif->pci_dev_handle, i + 1);
277 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
278 file_index);
279 i++;
280 }
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200281 vnet_hw_if_set_rx_queue_mode (vnm, vring->queue_index,
282 VNET_HW_IF_RX_MODE_POLLING);
283 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
284 virtio_vring_fill (vm, vif, vring);
Damjan Marion94100532020-11-06 23:25:57 +0100285 }
286 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000287}
288
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000289void
290virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif)
291{
292 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000293 vnet_virtio_vring_t *vring;
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000294
295 vec_foreach (vring, vif->txq_vrings)
296 {
297 vring->queue_index = vnet_hw_if_register_tx_queue (
298 vnm, vif->hw_if_index, TX_QUEUE_ACCESS (vring->queue_id));
299 }
300
Steven Luongc4a48f22022-01-19 09:08:27 -0800301 if (vif->num_txqs == 0)
302 {
303 virtio_log_error (vif, "Interface %U has 0 txq",
304 format_vnet_hw_if_index_name, vnm, vif->hw_if_index);
305 return;
306 }
307
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000308 for (u32 j = 0; j < vlib_get_n_threads (); j++)
309 {
310 u32 qi = vif->txq_vrings[j % vif->num_txqs].queue_index;
311 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
312 }
313
314 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
315}
316
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200317inline void
318virtio_set_net_hdr_size (virtio_if_t * vif)
319{
320 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
321 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000322 vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_v1_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200323 else
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000324 vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200325}
326
327inline void
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000328virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
329 virtio_if_type_t type)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200330{
331 u32 i, j, hw_if_index;
332 virtio_if_t *vif;
333 vnet_main_t *vnm = &vnet_main;
334 virtio_main_t *mm = &virtio_main;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000335 vnet_virtio_vring_t *vring;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200336 struct feat_struct
337 {
338 u8 bit;
339 char *str;
340 };
341 struct feat_struct *feat_entry;
342
343 static struct feat_struct feat_array[] = {
344#define _(s,b) { .str = #s, .bit = b, },
345 foreach_virtio_net_features
346#undef _
347 {.str = NULL}
348 };
349
350 struct feat_struct *flag_entry;
351 static struct feat_struct flags_array[] = {
352#define _(b,e,s) { .bit = b, .str = s, },
353 foreach_virtio_if_flag
354#undef _
355 {.str = NULL}
356 };
357
358 if (!hw_if_indices)
359 return;
360
361 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
362 {
363 vnet_hw_interface_t *hi =
364 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
365 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
366 if (vif->type != type)
367 continue;
368 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
369 format_vnet_hw_if_index_name, vnm,
370 hw_if_indices[hw_if_index], vif->hw_if_index);
371 if (type == VIRTIO_IF_TYPE_PCI)
372 {
373 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
374 &vif->pci_addr);
375 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200376 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200377 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100378 u8 *str = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200379 if (vif->host_if_name)
380 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
381 if (vif->net_ns)
382 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200383 if (vif->host_mtu_size)
384 vlib_cli_output (vm, " host-mtu-size \"%d\"",
385 vif->host_mtu_size);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200386 if (type == VIRTIO_IF_TYPE_TAP)
387 vlib_cli_output (vm, " host-mac-addr: %U",
388 format_ethernet_address, vif->host_mac_addr);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500389 vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100390
391 vec_foreach_index (i, vif->vhost_fds)
392 str = format (str, " %d", vif->vhost_fds[i]);
393 vlib_cli_output (vm, " vhost-fds%v", str);
394 vec_free (str);
Aloys Augustin2857e782020-03-16 17:29:52 +0100395 vec_foreach_index (i, vif->tap_fds)
396 str = format (str, " %d", vif->tap_fds[i]);
397 vlib_cli_output (vm, " tap-fds%v", str);
398 vec_free (str);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200399 }
Chenmin Sun7c615ae2019-07-18 23:19:28 +0800400 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100401 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200402 vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000403 vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200404 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
405 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
406 vif->mac_addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200407 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
408 vlib_cli_output (vm, " flags 0x%x", vif->flags);
409 flag_entry = (struct feat_struct *) &flags_array;
410 while (flag_entry->str)
411 {
412 if (vif->flags & (1ULL << flag_entry->bit))
413 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
414 flag_entry->bit);
415 flag_entry++;
416 }
417 if (type == VIRTIO_IF_TYPE_PCI)
418 {
419 device_status (vm, vif);
420 }
421 vlib_cli_output (vm, " features 0x%lx", vif->features);
422 feat_entry = (struct feat_struct *) &feat_array;
423 while (feat_entry->str)
424 {
425 if (vif->features & (1ULL << feat_entry->bit))
426 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
427 feat_entry->bit);
428 feat_entry++;
429 }
430 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
431 feat_entry = (struct feat_struct *) &feat_array;
432 while (feat_entry->str)
433 {
434 if (vif->remote_features & (1ULL << feat_entry->bit))
435 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
436 feat_entry->bit);
437 feat_entry++;
438 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000439 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
440 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000441 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
442 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000443 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
444 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200445 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000446 vring = vec_elt_at_index (vif->rxq_vrings, i);
447 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000448 vlib_cli_output (
449 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
450 vring->queue_size, vring->last_used_idx, vring->desc_next,
451 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100452 if (vif->is_packed)
453 {
454 vlib_cli_output (vm,
455 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
456 vring->driver_event->flags,
457 vring->driver_event->off_wrap,
458 vring->device_event->flags,
459 vring->device_event->off_wrap);
460 vlib_cli_output (vm,
461 " avail wrap counter %d, used wrap counter %d",
462 vring->avail_wrap_counter,
463 vring->used_wrap_counter);
464 }
465 else
466 vlib_cli_output (vm,
467 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
468 vring->avail->flags, vring->avail->idx,
469 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200470 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200471 {
472 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
473 vring->call_fd);
474 }
475 if (show_descr)
476 {
477 vlib_cli_output (vm, "\n descriptor table:\n");
478 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100479 " id addr len flags next/id user_addr\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200480 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100481 " ===== ================== ===== ====== ======= ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000482 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200483 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100484 if (vif->is_packed)
485 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000486 vnet_virtio_vring_packed_desc_t *desc =
487 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100488 vlib_cli_output (vm,
489 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
490 j, desc->addr,
491 desc->len,
492 desc->flags, desc->id, desc->addr);
493 }
494 else
495 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000496 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100497 vlib_cli_output (vm,
498 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
499 j, desc->addr,
500 desc->len,
501 desc->flags, desc->next, desc->addr);
502 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200503 }
504 }
505 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000506 vec_foreach_index (i, vif->txq_vrings)
507 {
508 vring = vec_elt_at_index (vif->txq_vrings, i);
509 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000510 vlib_cli_output (
511 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
512 vring->queue_size, vring->last_used_idx, vring->desc_next,
513 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100514 if (vif->is_packed)
515 {
516 vlib_cli_output (vm,
517 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
518 vring->driver_event->flags,
519 vring->driver_event->off_wrap,
520 vring->device_event->flags,
521 vring->device_event->off_wrap);
522 vlib_cli_output (vm,
523 " avail wrap counter %d, used wrap counter %d",
524 vring->avail_wrap_counter,
525 vring->used_wrap_counter);
526 }
527 else
528 vlib_cli_output (vm,
529 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
530 vring->avail->flags, vring->avail->idx,
531 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200532 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000533 {
534 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
535 vring->call_fd);
536 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200537 if (vring->flow_table)
538 {
539 vlib_cli_output (vm, " %U", gro_flow_table_format,
540 vring->flow_table);
541 }
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000542 if (vif->packet_buffering)
543 {
544 vlib_cli_output (vm, " %U", virtio_vring_buffering_format,
545 vring->buffering);
546 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000547 if (show_descr)
548 {
549 vlib_cli_output (vm, "\n descriptor table:\n");
550 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100551 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000552 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100553 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000554 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000555 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100556 if (vif->is_packed)
557 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000558 vnet_virtio_vring_packed_desc_t *desc =
559 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100560 vlib_cli_output (vm,
561 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
562 j, desc->addr,
563 desc->len,
564 desc->flags, desc->id, desc->addr);
565 }
566 else
567 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000568 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100569 vlib_cli_output (vm,
570 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
571 j, desc->addr,
572 desc->len,
573 desc->flags, desc->next, desc->addr);
574 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000575 }
576 }
577 }
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000578 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
579 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000580 {
581 vring = vif->cxq_vring;
582 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000583 vlib_cli_output (
584 vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
585 vring->queue_size, vring->last_used_idx, vring->desc_next,
586 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100587 if (vif->is_packed)
588 {
589 vlib_cli_output (vm,
590 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
591 vring->driver_event->flags,
592 vring->driver_event->off_wrap,
593 vring->device_event->flags,
594 vring->device_event->off_wrap);
595 vlib_cli_output (vm,
596 " avail wrap counter %d, used wrap counter %d",
597 vring->avail_wrap_counter,
598 vring->used_wrap_counter);
599 }
600 else
601 {
602 vlib_cli_output (vm,
603 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
604 vring->avail->flags, vring->avail->idx,
605 vring->used->flags, vring->used->idx);
606 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000607 if (show_descr)
608 {
609 vlib_cli_output (vm, "\n descriptor table:\n");
610 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100611 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000612 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100613 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000614 for (j = 0; j < vring->queue_size; j++)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000615 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100616 if (vif->is_packed)
617 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000618 vnet_virtio_vring_packed_desc_t *desc =
619 &vring->packed_desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100620 vlib_cli_output (vm,
621 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
622 j, desc->addr,
623 desc->len,
624 desc->flags, desc->id, desc->addr);
625 }
626 else
627 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000628 vnet_virtio_vring_desc_t *desc = &vring->desc[j];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100629 vlib_cli_output (vm,
630 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
631 j, desc->addr,
632 desc->len,
633 desc->flags, desc->next, desc->addr);
634 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000635 }
636 }
637 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200638 }
639
640}
641
Damjan Marionf41244f2019-11-08 17:41:06 +0100642static clib_error_t *
643virtio_init (vlib_main_t * vm)
644{
645 virtio_main_t *vim = &virtio_main;
646 clib_error_t *error = 0;
647
648 vim->log_default = vlib_log_register_class ("virtio", 0);
649 vlib_log_debug (vim->log_default, "initialized");
650
651 return error;
652}
653
654VLIB_INIT_FUNCTION (virtio_init);
655
Damjan Marion8389fb92017-10-13 18:29:53 +0200656/*
657 * fd.io coding-style-patch-verification: ON
658 *
659 * Local Variables:
660 * eval: (c-set-style "gnu")
661 * End:
662 */