2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2008, Cui Zheng <cuizheng@cs.unm.edu>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Lei Xia <lxia@northwestern.edu>
16 * Cui Zheng <cuizheng@cs.unm.edu>
19 * This is free software. You are permitted to use,
20 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_dev_mgr.h>
25 #include <devices/lnx_virtio_pci.h>
26 #include <palacios/vm_guest_mem.h>
27 #include <palacios/vmm_sprintf.h>
28 #include <palacios/vmm_vnet.h>
29 #include <palacios/vmm_lock.h>
30 #include <palacios/vmm_util.h>
32 #include <devices/pci.h>
35 #ifndef CONFIG_DEBUG_VIRTIO_NET
37 #define PrintDebug(fmt, args...)
40 #define VIRTIO_NIC_PROFILE
42 #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */
43 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 << 10))
45 struct virtio_net_hdr {
48 #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */
50 uint16_t hdr_len; /* Ethernet + IP + tcp/udp hdrs */
51 uint16_t gso_size; /* Bytes to append to hdr_len per frame */
52 uint16_t csum_start; /* Position to start checksumming from */
53 uint16_t csum_offset; /* Offset after that to place checksum */
54 }__attribute__((packed));
57 #define QUEUE_SIZE 1024
58 #define CTRL_QUEUE_SIZE 64
61 struct virtio_net_config
63 uint8_t mac[ETH_ALEN]; //VIRTIO_NET_F_MAC
65 } __attribute__((packed));
67 struct virtio_dev_state {
68 struct vm_device * pci_bus;
69 struct list_head dev_list;
70 struct v3_vm_info *vm;
73 struct virtio_net_state {
74 struct virtio_net_config net_cfg;
75 struct virtio_config virtio_cfg;
77 struct vm_device * dev;
78 struct pci_device * pci_dev;
81 struct virtio_queue rx_vq; //index 0, rvq in Linux virtio driver, handle packet to guest
82 struct virtio_queue tx_vq; //index 1, svq in Linux virtio driver, handle packet from guest
83 struct virtio_queue ctrl_vq; //index 2, ctrol info from guest
85 ulong_t pkt_sent, pkt_recv, pkt_drop;
87 struct v3_dev_net_ops * net_ops;
92 struct virtio_dev_state * virtio_dev;
93 struct list_head dev_link;
96 static int virtio_free(struct vm_device * dev)
102 static int virtio_init_state(struct virtio_net_state * virtio)
104 virtio->rx_vq.ring_desc_addr = 0;
105 virtio->rx_vq.ring_avail_addr = 0;
106 virtio->rx_vq.ring_used_addr = 0;
107 virtio->rx_vq.pfn = 0;
108 virtio->rx_vq.cur_avail_idx = 0;
110 virtio->tx_vq.ring_desc_addr = 0;
111 virtio->tx_vq.ring_avail_addr = 0;
112 virtio->tx_vq.ring_used_addr = 0;
113 virtio->tx_vq.pfn = 0;
114 virtio->tx_vq.cur_avail_idx = 0;
116 virtio->ctrl_vq.ring_desc_addr = 0;
117 virtio->ctrl_vq.ring_avail_addr = 0;
118 virtio->ctrl_vq.ring_used_addr = 0;
119 virtio->ctrl_vq.pfn = 0;
120 virtio->ctrl_vq.cur_avail_idx = 0;
122 virtio->virtio_cfg.host_features = 0;
123 //virtio->virtio_cfg.status = VIRTIO_NET_S_LINK_UP;
124 virtio->virtio_cfg.pci_isr = 0;
126 if (v3_lock_init(&(virtio->lock)) == -1){
127 PrintError("Virtio NIC: Failure to init lock for net_state\n");
130 virtio->pkt_sent = virtio->pkt_recv = virtio->pkt_drop = 0;
135 static int pkt_tx(struct guest_info *core, struct virtio_net_state * virtio, struct vring_desc * buf_desc)
137 uint8_t * buf = NULL;
138 uint32_t len = buf_desc->length;
140 PrintDebug("Virtio NIC: Virtio Pkt Sending, net_state: %p, pkt size: %d\n", virtio, len);
142 if (guest_pa_to_host_va(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) {
143 PrintError("Could not translate buffer address\n");
147 if (virtio->net_ops->send(buf, len, (void *)core, NULL) == -1) {
154 static int build_receive_header(struct virtio_net_hdr * hdr, const void * buf, int raw) {
158 memcpy(hdr, buf, sizeof(struct virtio_net_hdr));
160 memset(hdr, 0, sizeof(struct virtio_net_hdr));
166 static int copy_data_to_desc(struct guest_info *core,
167 struct virtio_net_state * virtio_state,
168 struct vring_desc * desc,
173 uint8_t * desc_buf = NULL;
175 if (guest_pa_to_host_va(core, desc->addr_gpa, (addr_t *)&(desc_buf)) == -1) {
176 PrintError("Could not translate buffer address\n");
179 len = (desc->length < buf_len)?desc->length:buf_len;
180 memcpy(desc_buf, buf, len);
188 static int get_desc_count(struct virtio_queue * q, int index) {
189 struct vring_desc * tmp_desc = &(q->desc[index]);
192 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
193 tmp_desc = &(q->desc[tmp_desc->next]);
200 static int handle_ctrl(struct guest_info *core, struct virtio_net_state * dev) {
204 static int handle_pkt_tx(struct guest_info *core, struct virtio_net_state * virtio_state)
206 struct virtio_queue * q = &(virtio_state->tx_vq);
207 struct virtio_net_hdr * hdr = NULL;
209 if (q->avail->index < q->last_avail_idx) {
210 q->idx_overflow = true;
213 q->last_avail_idx = q->avail->index;
215 while (q->cur_avail_idx < q->avail->index ||
216 (q->idx_overflow && q->cur_avail_idx < (q->avail->index + 65536))) {
217 struct vring_desc * hdr_desc = NULL;
219 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
220 int desc_cnt = get_desc_count(q, desc_idx);
221 uint32_t req_len = 0;
224 hdr_desc = &(q->desc[desc_idx]);
225 if (guest_pa_to_host_va(core, hdr_desc->addr_gpa, &(hdr_addr)) == -1) {
226 PrintError("Could not translate block header address\n");
230 hdr = (struct virtio_net_hdr*)hdr_addr;
231 desc_idx = hdr_desc->next;
233 for (i = 0; i < desc_cnt - 1; i++) {
234 struct vring_desc * buf_desc = &(q->desc[desc_idx]);
235 if (pkt_tx(core, virtio_state, buf_desc) == -1) {
236 PrintError("Error handling nic operation\n");
240 req_len += buf_desc->length;
241 desc_idx = buf_desc->next;
243 virtio_state->pkt_sent ++;
245 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
246 q->used->ring[q->used->index % q->queue_size].length = req_len; // What do we set this to????
249 int last_idx = q->cur_avail_idx;
253 if (q->cur_avail_idx < last_idx) {
254 q->idx_overflow = false;
258 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
259 v3_pci_raise_irq(virtio_state->virtio_dev->pci_bus, 0, virtio_state->pci_dev);
260 virtio_state->virtio_cfg.pci_isr = 0x1;
263 #ifdef VIRTIO_NIC_PROFILE
264 if(virtio_state->pkt_sent % 10000 == 0)
265 PrintError("Virtio NIC: %p, pkt_sent: %ld\n", virtio_state, virtio_state->pkt_sent);
268 #ifdef CONFIG_VNET_PROFILE
271 core->vnet_times.total_handle_time = time - core->vnet_times.virtio_handle_start;
273 PrintError("Vnet_profiling: total_handle_time: %ld vnet_time: %ld copy_from_guest: %ld copy_to_guest: %ld malloc_free: %ld, route_lookup: %ld\n",
274 (long)core->vnet_times.total_handle_time,
275 (long)core->vnet_times.vnet_handle_time,
276 (long)core->vnet_times.time_copy_from_guest,
277 (long)core->vnet_times.time_copy_to_guest,
278 (long)core->vnet_times.time_mallocfree,
279 (long)core->vnet_times.time_route_lookup);
286 static int virtio_setup_queue(struct guest_info *core,
287 struct virtio_net_state * virtio_state,
288 struct virtio_queue * queue,
293 queue->ring_desc_addr = page_addr;
294 queue->ring_avail_addr = page_addr + (queue->queue_size * sizeof(struct vring_desc));
295 queue->ring_used_addr = ((queue->ring_avail_addr) +
296 (sizeof(struct vring_avail)) +
297 (queue->queue_size * sizeof(uint16_t)));
299 // round up to next page boundary.
300 queue->ring_used_addr = (queue->ring_used_addr + 0xfff) & ~0xfff;
301 if (guest_pa_to_host_va(core, queue->ring_desc_addr, (addr_t *)&(queue->desc)) == -1) {
302 PrintError("Could not translate ring descriptor address\n");
306 if (guest_pa_to_host_va(core, queue->ring_avail_addr, (addr_t *)&(queue->avail)) == -1) {
307 PrintError("Could not translate ring available address\n");
311 if (guest_pa_to_host_va(core, queue->ring_used_addr, (addr_t *)&(queue->used)) == -1) {
312 PrintError("Could not translate ring used address\n");
316 PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
317 (void *)(queue->ring_desc_addr),
318 (void *)(queue->ring_avail_addr),
319 (void *)(queue->ring_used_addr));
321 PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n",
322 queue->desc, queue->avail, queue->used);
327 static int virtio_io_write(struct guest_info *core, uint16_t port, void * src, uint_t length, void * private_data)
329 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
330 int port_idx = port % virtio->io_range_size;
332 #ifdef CONFIG_VNET_PROFILE
335 core->vnet_times.virtio_handle_start = time;
338 PrintDebug("VIRTIO NIC %p Write for port %d (index=%d) len=%d, value=%x\n", private_data,
339 port, port_idx, length, *(uint32_t *)src);
342 case GUEST_FEATURES_PORT:
344 PrintError("Illegal write length for guest features\n");
347 virtio->virtio_cfg.guest_features = *(uint32_t *)src;
348 PrintDebug("Setting Guest Features to %x\n", virtio->virtio_cfg.guest_features);
351 case VRING_PG_NUM_PORT:
353 PrintError("Illegal write length for page frame number\n");
356 addr_t pfn = *(uint32_t *)src;
357 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
358 uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector;
360 PrintDebug("Virtio Write: pfn: %p, page_addr %p, queue_idx %d\n", (void *)pfn, (void *)page_addr, queue_idx);
364 virtio_setup_queue(core, virtio, &virtio->rx_vq, pfn, page_addr);
367 virtio_setup_queue(core, virtio, &virtio->tx_vq, pfn, page_addr);
370 virtio_setup_queue(core, virtio, &virtio->ctrl_vq, pfn, page_addr);
377 case VRING_Q_SEL_PORT:
378 virtio->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
379 if (virtio->virtio_cfg.vring_queue_selector > 2) {
380 PrintError("Virtio NIC device only uses 3 queue, selected %d\n",
381 virtio->virtio_cfg.vring_queue_selector);
386 case VRING_Q_NOTIFY_PORT:
388 uint16_t queue_idx = *(uint16_t *)src;
390 PrintDebug("receive queue notification 0, packet get by Guest\n");
391 } else if (queue_idx == 1){
392 if (handle_pkt_tx(core, virtio) == -1) {
393 PrintError("Could not handle NIC Notification\n");
396 } else if (queue_idx == 2){
397 if (handle_ctrl(core, virtio) == -1) {
398 PrintError("Could not handle NIC Notification\n");
402 PrintError("Virtio NIC device only uses 3 queue, selected %d\n",
408 case VIRTIO_STATUS_PORT:
409 virtio->virtio_cfg.status = *(uint8_t *)src;
410 if (virtio->virtio_cfg.status == 0) {
411 PrintDebug("Resetting device\n");
412 virtio_init_state(virtio);
416 case VIRTIO_ISR_PORT:
417 virtio->virtio_cfg.pci_isr = *(uint8_t *)src;
427 static int virtio_io_read(struct guest_info *core, uint16_t port, void * dst, uint_t length, void * private_data)
429 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
430 int port_idx = port % virtio->io_range_size;
431 uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector;
433 PrintDebug("Virtio NIC %p: Read for port %d (index =%d), length=%d\n", private_data,
434 port, port_idx, length);
437 case HOST_FEATURES_PORT:
439 PrintError("Illegal read length for host features\n");
442 *(uint32_t *)dst = virtio->virtio_cfg.host_features;
445 case VRING_PG_NUM_PORT:
447 PrintError("Illegal read length for page frame number\n");
452 *(uint32_t *)dst = virtio->rx_vq.pfn;
455 *(uint32_t *)dst = virtio->tx_vq.pfn;
458 *(uint32_t *)dst = virtio->ctrl_vq.pfn;
465 case VRING_SIZE_PORT:
467 PrintError("Illegal read length for vring size\n");
472 *(uint16_t *)dst = virtio->rx_vq.queue_size;
475 *(uint16_t *)dst = virtio->tx_vq.queue_size;
478 *(uint16_t *)dst = virtio->ctrl_vq.queue_size;
483 PrintDebug("queue index: %d, value=0x%x\n", (int)queue_idx, *(uint16_t *)dst);
486 case VIRTIO_STATUS_PORT:
488 PrintError("Illegal read length for status\n");
491 *(uint8_t *)dst = virtio->virtio_cfg.status;
494 case VIRTIO_ISR_PORT:
495 *(uint8_t *)dst = virtio->virtio_cfg.pci_isr;
496 virtio->virtio_cfg.pci_isr = 0;
497 v3_pci_lower_irq(virtio->virtio_dev->pci_bus, 0, virtio->pci_dev);
501 PrintError("Virtio NIC: Read of Unhandled Virtio Read\n");
511 static int virtio_rx(uint8_t * buf, uint32_t size, void * private_data) {
512 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
513 struct virtio_queue * q = &(virtio->rx_vq);
514 struct virtio_net_hdr hdr;
515 uint32_t hdr_len = sizeof(struct virtio_net_hdr);
516 uint32_t data_len = size;
522 flags = v3_lock_irqsave(virtio->lock);
524 PrintDebug("VIRTIO NIC: receiving packet to virtio nic %p, size:%d\n", virtio, size);
532 build_receive_header(&hdr, buf, raw);
534 if (q->ring_avail_addr == 0) {
535 PrintError("Queue is not set\n");
540 if (q->last_avail_idx > q->avail->index)
541 q->idx_overflow = true;
542 q->last_avail_idx = q->avail->index;
544 if (q->cur_avail_idx < q->avail->index || (q->idx_overflow && q->cur_avail_idx < q->avail->index+65536)){
546 uint16_t hdr_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
547 uint16_t buf_idx = 0;
548 struct vring_desc * hdr_desc = NULL;
550 hdr_desc = &(q->desc[hdr_idx]);
551 if (guest_pa_to_host_va(&(virtio->virtio_dev->vm->cores[0]), hdr_desc->addr_gpa, &(hdr_addr)) == -1) {
552 PrintError("Could not translate receive buffer address\n");
557 memcpy((void *)hdr_addr, &hdr, sizeof(struct virtio_net_hdr));
558 if (offset >= data_len) {
559 hdr_desc->flags &= ~VIRTIO_NEXT_FLAG;
562 for (buf_idx = hdr_desc->next; offset < data_len; buf_idx = q->desc[hdr_idx].next) {
563 struct vring_desc * buf_desc = &(q->desc[buf_idx]);
566 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]), virtio, buf_desc, buf + offset, data_len - offset);
568 if (offset < data_len) {
569 buf_desc->flags = VIRTIO_NEXT_FLAG;
571 buf_desc->length = len;
574 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
575 q->used->ring[q->used->index % q->queue_size].length = data_len + hdr_len; // This should be the total length of data sent to guest (header+pkt_data)
578 int last_idx = q->cur_avail_idx;
580 if (q->cur_avail_idx < last_idx)
581 q->idx_overflow = false;
585 #ifdef VIRTIO_NIC_PROFILE
586 PrintError("Virtio NIC: %p, one pkt dropped receieved: %ld, dropped: %ld, sent: %ld curidx: %d, avaiIdx: %d\n",
587 virtio, virtio->pkt_recv, virtio->pkt_drop, virtio->pkt_sent, q->cur_avail_idx, q->avail->index);
591 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
592 PrintDebug("Raising IRQ %d\n", virtio->pci_dev->config_header.intr_line);
593 v3_pci_raise_irq(virtio->virtio_dev->pci_bus, 0, virtio->pci_dev);
594 virtio->virtio_cfg.pci_isr = 0x1;
597 #ifdef VIRTIO_NIC_PROFILE
598 if ((virtio->pkt_recv % 10000) == 0){
599 PrintError("Virtio NIC: %p, receieved: %ld, dropped: %ld, sent: %ld\n",
600 virtio, virtio->pkt_recv, virtio->pkt_drop, virtio->pkt_sent);
608 v3_unlock_irqrestore(virtio->lock, flags);
614 static struct v3_device_ops dev_ops = {
621 static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_state * net_state)
623 struct pci_device * pci_dev = NULL;
624 struct v3_pci_bar bars[6];
625 int num_ports = sizeof(struct virtio_config);
626 int tmp_ports = num_ports;
629 // This gets the number of ports, rounded up to a power of 2
630 net_state->io_range_size = 1; // must be a power of 2
631 while (tmp_ports > 0) {
633 net_state->io_range_size <<= 1;
636 // this is to account for any low order bits being set in num_ports
637 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
638 if ((num_ports & ((net_state->io_range_size >> 1) - 1)) == 0) {
639 net_state->io_range_size >>= 1;
642 for (i = 0; i < 6; i++) {
643 bars[i].type = PCI_BAR_NONE;
646 PrintDebug("Virtio-NIC io_range_size = %d\n", net_state->io_range_size);
648 bars[0].type = PCI_BAR_IO;
649 bars[0].default_base_port = -1;
650 bars[0].num_ports = net_state->io_range_size;
651 bars[0].io_read = virtio_io_read;
652 bars[0].io_write = virtio_io_write;
653 bars[0].private_data = net_state;
655 pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE,
656 0, PCI_AUTO_DEV_NUM, 0,
657 "LNX_VIRTIO_NIC", bars,
658 NULL, NULL, NULL, net_state);
661 PrintError("Virtio NIC: Could not register PCI Device\n");
665 PrintDebug("Virtio NIC: registered to PCI bus\n");
667 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
668 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
671 pci_dev->config_header.device_id = VIRTIO_NET_DEV_ID;
672 pci_dev->config_header.class = PCI_CLASS_NETWORK;
673 pci_dev->config_header.subclass = PCI_NET_SUBCLASS_OTHER;
674 pci_dev->config_header.subsystem_id = VIRTIO_NET_SUBDEVICE_ID;
675 pci_dev->config_header.intr_pin = 1;
676 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
678 net_state->pci_dev = pci_dev;
679 net_state->virtio_cfg.host_features = 0; //no features support now
680 net_state->rx_vq.queue_size = QUEUE_SIZE;
681 net_state->tx_vq.queue_size = QUEUE_SIZE;
682 net_state->ctrl_vq.queue_size = CTRL_QUEUE_SIZE;
683 net_state->virtio_dev = virtio;
685 virtio_init_state(net_state);
690 static int connect_fn(struct v3_vm_info * info,
691 void * frontend_data,
692 struct v3_dev_net_ops * ops,
694 void * private_data) {
695 struct virtio_dev_state * virtio = (struct virtio_dev_state *)frontend_data;
696 struct virtio_net_state * net_state = (struct virtio_net_state *)V3_Malloc(sizeof(struct virtio_net_state));
698 memset(net_state, 0, sizeof(struct virtio_net_state));
699 register_dev(virtio, net_state);
701 net_state->net_ops = ops;
702 net_state->backend_data = private_data;
704 ops->recv = virtio_rx;
705 ops->frontend_data = net_state;
710 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
711 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
712 struct virtio_dev_state * virtio_state = NULL;
713 char * name = v3_cfg_val(cfg, "name");
715 PrintDebug("Virtio NIC: Initializing VIRTIO Network device: %s\n", name);
717 if (pci_bus == NULL) {
718 PrintError("Virtio NIC: VirtIO devices require a PCI Bus");
722 virtio_state = (struct virtio_dev_state *)V3_Malloc(sizeof(struct virtio_dev_state));
723 memset(virtio_state, 0, sizeof(struct virtio_dev_state));
725 INIT_LIST_HEAD(&(virtio_state->dev_list));
726 virtio_state->pci_bus = pci_bus;
727 virtio_state->vm = vm;
729 struct vm_device * dev = v3_allocate_device(name, &dev_ops, virtio_state);
730 if (v3_attach_device(vm, dev) == -1) {
731 PrintError("Virtio NIC: Could not attach device %s\n", name);
735 if (v3_dev_add_net_frontend(vm, name, connect_fn, (void *)virtio_state) == -1) {
736 PrintError("Virtio NIC: Could not register %s as net frontend\n", name);
743 device_register("LNX_VIRTIO_NIC", virtio_init)