blob: bbca81c2d02d80802886cc2bdf8f51fd0a9f09d5 [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{
Damjan Marion8389fb92017-10-13 18:29:53 +020063 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 Kazmia7a22812020-08-31 17:17:16 +020088 i = sizeof (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 Kazmia7a22812020-08-31 17:17:16 +020093 i = sizeof (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 Kazmia7a22812020-08-31 17:17:16 +0200100 i = sizeof (vring_used_t) + sz * sizeof (vring_used_elem_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200101 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
102 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400103 clib_memset (vring->used, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200104
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000105 vring->queue_id = idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200106 ASSERT (vring->buffers == 0);
Damjan Marionc58408c2018-01-18 14:54:04 +0100107 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
Damjan Marion8389fb92017-10-13 18:29:53 +0200108
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100109 if (idx & 1)
110 {
111 clib_memset_u32 (vring->buffers, ~0, sz);
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000112 // tx path: suppress the interrupts from kernel
113 vring->call_fd = -1;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100114 }
Mohsin Kazmi4b563402021-01-27 14:16:56 +0000115 else
116 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100117
Damjan Marion8389fb92017-10-13 18:29:53 +0200118 vring->size = sz;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100119 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
120 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
121 vring->size, vring->call_fd, vring->kick_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200122
Damjan Marion7c6102b2019-11-08 17:59:56 +0100123 return 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200124}
125
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200126inline void
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100127virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring)
Damjan Marion8389fb92017-10-13 18:29:53 +0200128{
Stevena624dbe2018-01-09 11:13:29 -0800129 u16 used = vring->desc_in_use;
Steven074f6982018-03-30 22:18:11 -0700130 u16 last = vring->last_used_idx;
Stevena624dbe2018-01-09 11:13:29 -0800131 u16 mask = vring->size - 1;
132
133 while (used)
134 {
Steven074f6982018-03-30 22:18:11 -0700135 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
136 last++;
Stevena624dbe2018-01-09 11:13:29 -0800137 used--;
138 }
139}
140
141clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000142virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
Stevena624dbe2018-01-09 11:13:29 -0800143{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000144 virtio_vring_t *vring =
145 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
Stevena624dbe2018-01-09 11:13:29 -0800146
147 clib_file_del_by_index (&file_main, vring->call_file_index);
148 close (vring->kick_fd);
149 close (vring->call_fd);
150 if (vring->used)
151 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100152 virtio_free_buffers (vm, vring);
Stevena624dbe2018-01-09 11:13:29 -0800153 clib_mem_free (vring->used);
154 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200155 if (vring->desc)
156 clib_mem_free (vring->desc);
157 if (vring->avail)
158 clib_mem_free (vring->avail);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000159 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000160 return 0;
161}
162
163clib_error_t *
164virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
165{
166 virtio_vring_t *vring =
167 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
168
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000169 close (vring->kick_fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000170 if (vring->used)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200171 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100172 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000173 clib_mem_free (vring->used);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200174 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000175 if (vring->desc)
176 clib_mem_free (vring->desc);
177 if (vring->avail)
178 clib_mem_free (vring->avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200179 vec_free (vring->buffers);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200180 gro_flow_table_free (vring->flow_table);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000181 virtio_vring_buffering_free (vm, vring->buffering);
Mohsin Kazmi3f340172019-05-27 15:53:25 +0200182 clib_spinlock_free (&vring->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200183 return 0;
184}
185
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000186void
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200187virtio_set_packet_coalesce (virtio_if_t * vif)
188{
189 vnet_main_t *vnm = vnet_get_main ();
190 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
191 virtio_vring_t *vring;
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +0200192 vif->packet_coalesce = 1;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200193 vec_foreach (vring, vif->txq_vrings)
194 {
195 gro_flow_table_init (&vring->flow_table,
196 vif->type & (VIRTIO_IF_TYPE_TAP |
197 VIRTIO_IF_TYPE_PCI), hw->tx_node_index);
198 }
199}
200
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000201clib_error_t *
202virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size)
203{
204 vnet_main_t *vnm = vnet_get_main ();
205 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
206 virtio_vring_t *vring;
207 clib_error_t *error = 0;
208 vif->packet_buffering = 1;
209
210 vec_foreach (vring, vif->txq_vrings)
211 {
212 if ((error =
213 virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index,
214 buffering_size)))
215 {
216 break;
217 }
218 }
219
220 return error;
221}
222
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200223static void
224virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif, virtio_vring_t *vring)
225{
226 if (vif->is_packed)
227 virtio_refill_vring_packed (vm, vif, vif->type, vring,
228 vif->virtio_net_hdr_sz,
229 virtio_input_node.index);
230 else
231 virtio_refill_vring_split (vm, vif, vif->type, vring,
232 vif->virtio_net_hdr_sz,
233 virtio_input_node.index);
234}
235
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200236void
Damjan Marion94100532020-11-06 23:25:57 +0100237virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif)
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000238{
239 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion94100532020-11-06 23:25:57 +0100240 virtio_vring_t *vring;
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100241 u32 i = 0;
Damjan Marion94100532020-11-06 23:25:57 +0100242
243 vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index);
244
245 vec_foreach (vring, vif->rxq_vrings)
246 {
247 vring->queue_index = vnet_hw_if_register_rx_queue (
248 vnm, vif->hw_if_index, RX_QUEUE_ACCESS (vring->queue_id),
249 VNET_HW_IF_RXQ_THREAD_ANY);
250 vring->buffer_pool_index = vlib_buffer_pool_get_default_for_numa (
251 vm, vnet_hw_if_get_rx_queue_numa_node (vnm, vring->queue_index));
252 if (vif->type == VIRTIO_IF_TYPE_TAP || vif->type == VIRTIO_IF_TYPE_TUN)
253 {
254
255 clib_file_t f = {
256 .read_function = call_read_ready,
257 .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
258 .file_descriptor = vring->call_fd,
259 .private_data = vring->queue_index,
260 .description = format (0, "%U vring %u", format_virtio_device_name,
261 vif->dev_instance, vring->queue_id),
262 };
263
264 vring->call_file_index = clib_file_add (&file_main, &f);
265 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
266 vring->call_file_index);
267 }
Mohsin Kazmi90ffe062021-02-23 12:46:14 +0100268 else if ((vif->type == VIRTIO_IF_TYPE_PCI) && (vif->support_int_mode) &&
269 (vif->msix_enabled == VIRTIO_MSIX_ENABLED))
270 {
271 u32 file_index;
272 file_index =
273 vlib_pci_get_msix_file_index (vm, vif->pci_dev_handle, i + 1);
274 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
275 file_index);
276 i++;
277 }
Mohsin Kazmi6799f9b2021-04-28 18:55:45 +0200278 vnet_hw_if_set_rx_queue_mode (vnm, vring->queue_index,
279 VNET_HW_IF_RX_MODE_POLLING);
280 vring->mode = VNET_HW_IF_RX_MODE_POLLING;
281 virtio_vring_fill (vm, vif, vring);
Damjan Marion94100532020-11-06 23:25:57 +0100282 }
283 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000284}
285
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000286void
287virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif)
288{
289 vnet_main_t *vnm = vnet_get_main ();
290 virtio_vring_t *vring;
291
292 vec_foreach (vring, vif->txq_vrings)
293 {
294 vring->queue_index = vnet_hw_if_register_tx_queue (
295 vnm, vif->hw_if_index, TX_QUEUE_ACCESS (vring->queue_id));
296 }
297
Steven Luongc4a48f22022-01-19 09:08:27 -0800298 if (vif->num_txqs == 0)
299 {
300 virtio_log_error (vif, "Interface %U has 0 txq",
301 format_vnet_hw_if_index_name, vnm, vif->hw_if_index);
302 return;
303 }
304
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000305 for (u32 j = 0; j < vlib_get_n_threads (); j++)
306 {
307 u32 qi = vif->txq_vrings[j % vif->num_txqs].queue_index;
308 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
309 }
310
311 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
312}
313
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200314inline void
315virtio_set_net_hdr_size (virtio_if_t * vif)
316{
317 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
318 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200319 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_v1_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200320 else
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200321 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_t);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200322}
323
324inline void
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000325virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
326 virtio_if_type_t type)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200327{
328 u32 i, j, hw_if_index;
329 virtio_if_t *vif;
330 vnet_main_t *vnm = &vnet_main;
331 virtio_main_t *mm = &virtio_main;
332 virtio_vring_t *vring;
333 struct feat_struct
334 {
335 u8 bit;
336 char *str;
337 };
338 struct feat_struct *feat_entry;
339
340 static struct feat_struct feat_array[] = {
341#define _(s,b) { .str = #s, .bit = b, },
342 foreach_virtio_net_features
343#undef _
344 {.str = NULL}
345 };
346
347 struct feat_struct *flag_entry;
348 static struct feat_struct flags_array[] = {
349#define _(b,e,s) { .bit = b, .str = s, },
350 foreach_virtio_if_flag
351#undef _
352 {.str = NULL}
353 };
354
355 if (!hw_if_indices)
356 return;
357
358 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
359 {
360 vnet_hw_interface_t *hi =
361 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
362 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
363 if (vif->type != type)
364 continue;
365 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
366 format_vnet_hw_if_index_name, vnm,
367 hw_if_indices[hw_if_index], vif->hw_if_index);
368 if (type == VIRTIO_IF_TYPE_PCI)
369 {
370 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
371 &vif->pci_addr);
372 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200373 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200374 {
Damjan Marion7c6102b2019-11-08 17:59:56 +0100375 u8 *str = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200376 if (vif->host_if_name)
377 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
378 if (vif->net_ns)
379 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200380 if (vif->host_mtu_size)
381 vlib_cli_output (vm, " host-mtu-size \"%d\"",
382 vif->host_mtu_size);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200383 if (type == VIRTIO_IF_TYPE_TAP)
384 vlib_cli_output (vm, " host-mac-addr: %U",
385 format_ethernet_address, vif->host_mac_addr);
Matthew Smithbd50ed12020-07-24 13:38:03 -0500386 vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up);
Damjan Marion7c6102b2019-11-08 17:59:56 +0100387
388 vec_foreach_index (i, vif->vhost_fds)
389 str = format (str, " %d", vif->vhost_fds[i]);
390 vlib_cli_output (vm, " vhost-fds%v", str);
391 vec_free (str);
Aloys Augustin2857e782020-03-16 17:29:52 +0100392 vec_foreach_index (i, vif->tap_fds)
393 str = format (str, " %d", vif->tap_fds[i]);
394 vlib_cli_output (vm, " tap-fds%v", str);
395 vec_free (str);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200396 }
Chenmin Sun7c615ae2019-07-18 23:19:28 +0800397 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
Mohsin Kazmiba0061f2019-12-18 17:08:54 +0100398 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200399 vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000400 vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200401 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
402 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
403 vif->mac_addr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200404 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
405 vlib_cli_output (vm, " flags 0x%x", vif->flags);
406 flag_entry = (struct feat_struct *) &flags_array;
407 while (flag_entry->str)
408 {
409 if (vif->flags & (1ULL << flag_entry->bit))
410 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
411 flag_entry->bit);
412 flag_entry++;
413 }
414 if (type == VIRTIO_IF_TYPE_PCI)
415 {
416 device_status (vm, vif);
417 }
418 vlib_cli_output (vm, " features 0x%lx", vif->features);
419 feat_entry = (struct feat_struct *) &feat_array;
420 while (feat_entry->str)
421 {
422 if (vif->features & (1ULL << feat_entry->bit))
423 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
424 feat_entry->bit);
425 feat_entry++;
426 }
427 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
428 feat_entry = (struct feat_struct *) &feat_array;
429 while (feat_entry->str)
430 {
431 if (vif->remote_features & (1ULL << feat_entry->bit))
432 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
433 feat_entry->bit);
434 feat_entry++;
435 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000436 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
437 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000438 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
439 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000440 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
441 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200442 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000443 vring = vec_elt_at_index (vif->rxq_vrings, i);
444 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200445 vlib_cli_output (vm,
446 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
447 vring->size, vring->last_used_idx, vring->desc_next,
448 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100449 if (vif->is_packed)
450 {
451 vlib_cli_output (vm,
452 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
453 vring->driver_event->flags,
454 vring->driver_event->off_wrap,
455 vring->device_event->flags,
456 vring->device_event->off_wrap);
457 vlib_cli_output (vm,
458 " avail wrap counter %d, used wrap counter %d",
459 vring->avail_wrap_counter,
460 vring->used_wrap_counter);
461 }
462 else
463 vlib_cli_output (vm,
464 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
465 vring->avail->flags, vring->avail->idx,
466 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200467 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200468 {
469 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
470 vring->call_fd);
471 }
472 if (show_descr)
473 {
474 vlib_cli_output (vm, "\n descriptor table:\n");
475 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100476 " id addr len flags next/id user_addr\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200477 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100478 " ===== ================== ===== ====== ======= ==================\n");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200479 for (j = 0; j < vring->size; j++)
480 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100481 if (vif->is_packed)
482 {
483 vring_packed_desc_t *desc = &vring->packed_desc[j];
484 vlib_cli_output (vm,
485 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
486 j, desc->addr,
487 desc->len,
488 desc->flags, desc->id, desc->addr);
489 }
490 else
491 {
492 vring_desc_t *desc = &vring->desc[j];
493 vlib_cli_output (vm,
494 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
495 j, desc->addr,
496 desc->len,
497 desc->flags, desc->next, desc->addr);
498 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200499 }
500 }
501 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000502 vec_foreach_index (i, vif->txq_vrings)
503 {
504 vring = vec_elt_at_index (vif->txq_vrings, i);
505 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
506 vlib_cli_output (vm,
507 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
508 vring->size, vring->last_used_idx, vring->desc_next,
509 vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100510 if (vif->is_packed)
511 {
512 vlib_cli_output (vm,
513 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
514 vring->driver_event->flags,
515 vring->driver_event->off_wrap,
516 vring->device_event->flags,
517 vring->device_event->off_wrap);
518 vlib_cli_output (vm,
519 " avail wrap counter %d, used wrap counter %d",
520 vring->avail_wrap_counter,
521 vring->used_wrap_counter);
522 }
523 else
524 vlib_cli_output (vm,
525 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
526 vring->avail->flags, vring->avail->idx,
527 vring->used->flags, vring->used->idx);
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200528 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000529 {
530 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
531 vring->call_fd);
532 }
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200533 if (vring->flow_table)
534 {
535 vlib_cli_output (vm, " %U", gro_flow_table_format,
536 vring->flow_table);
537 }
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000538 if (vif->packet_buffering)
539 {
540 vlib_cli_output (vm, " %U", virtio_vring_buffering_format,
541 vring->buffering);
542 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000543 if (show_descr)
544 {
545 vlib_cli_output (vm, "\n descriptor table:\n");
546 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100547 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000548 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100549 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000550 for (j = 0; j < vring->size; j++)
551 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100552 if (vif->is_packed)
553 {
554 vring_packed_desc_t *desc = &vring->packed_desc[j];
555 vlib_cli_output (vm,
556 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
557 j, desc->addr,
558 desc->len,
559 desc->flags, desc->id, desc->addr);
560 }
561 else
562 {
563 vring_desc_t *desc = &vring->desc[j];
564 vlib_cli_output (vm,
565 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
566 j, desc->addr,
567 desc->len,
568 desc->flags, desc->next, desc->addr);
569 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000570 }
571 }
572 }
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000573 if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL &&
574 vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000575 {
576 vring = vif->cxq_vring;
577 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
578 vlib_cli_output (vm,
579 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
580 vring->size, vring->last_used_idx,
581 vring->desc_next, vring->desc_in_use);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100582 if (vif->is_packed)
583 {
584 vlib_cli_output (vm,
585 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
586 vring->driver_event->flags,
587 vring->driver_event->off_wrap,
588 vring->device_event->flags,
589 vring->device_event->off_wrap);
590 vlib_cli_output (vm,
591 " avail wrap counter %d, used wrap counter %d",
592 vring->avail_wrap_counter,
593 vring->used_wrap_counter);
594 }
595 else
596 {
597 vlib_cli_output (vm,
598 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
599 vring->avail->flags, vring->avail->idx,
600 vring->used->flags, vring->used->idx);
601 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000602 if (show_descr)
603 {
604 vlib_cli_output (vm, "\n descriptor table:\n");
605 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100606 " id addr len flags next/id user_addr\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000607 vlib_cli_output (vm,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100608 " ===== ================== ===== ====== ======== ==================\n");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000609 for (j = 0; j < vring->size; j++)
610 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100611 if (vif->is_packed)
612 {
613 vring_packed_desc_t *desc = &vring->packed_desc[j];
614 vlib_cli_output (vm,
615 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
616 j, desc->addr,
617 desc->len,
618 desc->flags, desc->id, desc->addr);
619 }
620 else
621 {
622 vring_desc_t *desc = &vring->desc[j];
623 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->next, desc->addr);
628 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000629 }
630 }
631 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200632 }
633
634}
635
Damjan Marionf41244f2019-11-08 17:41:06 +0100636static clib_error_t *
637virtio_init (vlib_main_t * vm)
638{
639 virtio_main_t *vim = &virtio_main;
640 clib_error_t *error = 0;
641
642 vim->log_default = vlib_log_register_class ("virtio", 0);
643 vlib_log_debug (vim->log_default, "initialized");
644
645 return error;
646}
647
648VLIB_INIT_FUNCTION (virtio_init);
649
Damjan Marion8389fb92017-10-13 18:29:53 +0200650/*
651 * fd.io coding-style-patch-verification: ON
652 *
653 * Local Variables:
654 * eval: (c-set-style "gnu")
655 * End:
656 */