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, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, Lei Xia <lxia@cs.northwestern.edu>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * Lei Xia <lxia@cs.northwestern.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vm_guest_mem.h>
25 #include <devices/lnx_virtio_pci.h>
26 #include <palacios/vmm_vnet.h>
27 #include <palacios/vmm_sprintf.h>
28 #include <devices/pci.h>
31 #ifndef CONFIG_DEBUG_LINUX_VIRTIO_VNET
33 #define PrintDebug(fmt, args...)
37 #define QUEUE_SIZE 4096
38 #define CMD_QUEUE_SIZE 128
44 } __attribute__((packed));
51 struct virtio_vnet_state {
52 struct v3_vm_info * vm;
53 struct vnet_config vnet_cfg;
54 struct virtio_config virtio_cfg;
56 struct vm_device * pci_bus;
57 struct pci_device * pci_dev;
59 struct virtio_queue queue[NUM_QUEUES];
61 struct virtio_queue * cur_queue;
76 #define VNET_GET_ROUTES 10
77 #define VNET_ADD_ROUTE 11
78 #define VNET_DEL_ROUTE 12
80 #define VNET_GET_LINKS 20
81 #define VNET_ADD_LINK 21
82 #define VNET_DEL_LINK 22
84 // structure of the vnet command header
85 struct vnet_ctrl_hdr {
88 } __attribute__((packed));
91 struct vnet_bridge_pkt {
94 uint8_t pkt[ETHERNET_PACKET_LEN];
95 }__attribute__((packed));
98 static int virtio_reset(struct virtio_vnet_state * vnet_state) {
100 memset(vnet_state->queue, 0, sizeof(struct virtio_queue) * NUM_QUEUES);
102 vnet_state->cur_queue = &(vnet_state->queue[0]);
104 vnet_state->virtio_cfg.status = 0;
105 vnet_state->virtio_cfg.pci_isr = 0;
107 vnet_state->queue[0].queue_size = CMD_QUEUE_SIZE;
108 vnet_state->queue[1].queue_size = QUEUE_SIZE;
109 vnet_state->queue[2].queue_size = QUEUE_SIZE;
111 memset(&(vnet_state->vnet_cfg), 0, sizeof(struct vnet_config));
112 v3_lock_init(&(vnet_state->lock));
119 static int get_desc_count(struct virtio_queue * q, int index) {
120 struct vring_desc * tmp_desc = &(q->desc[index]);
123 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
124 tmp_desc = &(q->desc[tmp_desc->next]);
134 static int handle_cmd_kick(struct guest_info * core, struct virtio_vnet_state * vnet_state) {
135 struct virtio_queue * q = &(vnet_state->queue[0]);
137 PrintDebug("VNET Bridge: Handling command queue\n");
139 while (q->cur_avail_idx != q->avail->index) {
140 struct vring_desc * hdr_desc = NULL;
141 struct vring_desc * buf_desc = NULL;
142 struct vring_desc * status_desc = NULL;
143 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
144 uint16_t desc_cnt = get_desc_count(q, desc_idx);
145 struct vnet_ctrl_hdr * hdr = NULL;
148 uint8_t * status_ptr = NULL;
152 PrintDebug("VNET Bridge: CMD: Descriptor Count=%d, index=%d, desc_idx=%d\n",
153 desc_cnt, q->cur_avail_idx % QUEUE_SIZE, desc_idx);
156 PrintError("VNET Bridge cmd must include at least 3 descriptors (cnt=%d)\n", desc_cnt);
160 hdr_desc = &(q->desc[desc_idx]);
162 if (v3_gpa_to_hva(core, hdr_desc->addr_gpa, (addr_t *)&hdr) == -1) {
163 PrintError("Could not translate VirtioVNET header address\n");
167 desc_idx = hdr_desc->next;
169 if (hdr->cmd_type == VNET_ADD_ROUTE) {
171 for (i = 0; i < hdr->num_cmds; i++) {
172 uint8_t tmp_status = 0;
173 struct v3_vnet_route * route = NULL;
175 buf_desc = &(q->desc[desc_idx]);
177 if (v3_gpa_to_hva(core, buf_desc->addr_gpa, (addr_t *)&(route)) == -1) {
178 PrintError("Could not translate route address\n");
183 PrintDebug("VNET Bridge: Adding VNET Route\n");
185 tmp_status = v3_vnet_add_route(*route);
187 PrintDebug("VNET Route Added\n");
189 if (tmp_status != 0) {
190 PrintError("Error adding VNET ROUTE\n");
194 xfer_len += buf_desc->length;
195 desc_idx = buf_desc->next;
202 status_desc = &(q->desc[desc_idx]);
204 if (v3_gpa_to_hva(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
205 PrintError("VirtioVNET Error could not translate status address\n");
209 xfer_len += status_desc->length;
210 *status_ptr = status;
212 PrintDebug("Transferred %d bytes (xfer_len)\n", xfer_len);
213 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
214 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
221 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
222 PrintDebug("Raising IRQ %d\n", vnet_state->pci_dev->config_header.intr_line);
223 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
224 vnet_state->virtio_cfg.pci_isr = 1;
232 static int vnet_pkt_input_cb(struct v3_vm_info * vm, struct v3_vnet_pkt vnet_pkts[], uint16_t pkt_num, void * private_data){
233 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
234 struct virtio_queue * q = &(vnet_state->queue[RECV_QUEUE]);
238 struct v3_vnet_pkt * pkt = NULL;
244 flags = v3_lock_irqsave(vnet_state->lock);
246 if (q->ring_avail_addr == 0) {
247 PrintError("Queue is not set\n");
248 v3_unlock_irqrestore(vnet_state->lock, flags);
252 PrintDebug("VNET Bridge: RX: running on cpu: %d, num of pkts: %d\n", V3_Get_CPU(), pkt_num);
254 for (sent = 0; sent < pkt_num; sent++) {
255 pkt = &vnet_pkts[sent];
256 vnet_state->pkt_recv++;
258 if (q->cur_avail_idx != q->avail->index) {
259 uint16_t pkt_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
260 struct vring_desc * pkt_desc = NULL;
261 struct vnet_bridge_pkt * virtio_pkt = NULL;
263 pkt_desc = &(q->desc[pkt_idx]);
264 PrintDebug("VNET Bridge RX: buffer desc len: %d\n", pkt_desc->length);
266 if (v3_gpa_to_hva(&(vm->cores[0]), pkt_desc->addr_gpa, (addr_t *)&(virtio_pkt)) == -1) {
267 PrintError("Could not translate buffer address\n");
268 v3_unlock_irqrestore(vnet_state->lock, flags);
272 PrintDebug("VNET Bridge: RX: pkt sent to guest pkt size: %d, dst link: %d\n", pkt->size, pkt->dst_id);
274 // Fill in dst packet buffer
275 virtio_pkt->link_id = pkt->dst_id;
276 virtio_pkt->pkt_size = pkt->size;
277 memcpy(virtio_pkt->pkt, pkt->data, pkt->size);
279 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
280 q->used->ring[q->used->index % q->queue_size].length = sizeof(struct vnet_bridge_pkt);
285 vnet_state->pkt_drop++;
286 v3_vnet_disable_bridge();
291 v3_unlock_irqrestore(vnet_state->lock, flags);
295 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
296 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
297 vnet_state->virtio_cfg.pci_isr = 0x1;
298 PrintDebug("Raising IRQ %d\n", vnet_state->pci_dev->config_header.intr_line);
304 #ifdef CONFIG_VNET_PROFILE
305 if (vnet_state->pkt_recv % 200000 == 0)
306 PrintError("Vnet Bridge: sent: %ld, rxed: %ld, dropped: %ld, total exit: %ld, tx exit: %ld, rx exit: %ld\n",
307 vnet_state->pkt_sent,
308 vnet_state->pkt_recv,
309 vnet_state->pkt_drop,
310 vnet_state->total_exit,
312 vnet_state->rx_exit);
315 v3_unlock_irqrestore(vnet_state->lock, flags);
321 static void vnet_pkt_input_xcall(void * data) {
322 struct v3_vnet_bridge_input_args * args = (struct v3_vnet_bridge_input_args *)data;
324 vnet_pkt_input_cb(args->vm, args->vnet_pkts, args->pkt_num, args->private_data);
327 static int handle_pkt_kick(struct guest_info * core, struct virtio_vnet_state * vnet_state) {
328 struct virtio_queue * q = &(vnet_state->queue[XMIT_QUEUE]);
329 unsigned long flags = 0;
331 int cpu = V3_Get_CPU();
333 flags = v3_lock_irqsave(vnet_state->lock);
335 if (q->ring_avail_addr == 0) {
336 v3_unlock_irqrestore(vnet_state->lock,flags);
340 while (q->cur_avail_idx != q->avail->index) {
341 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
342 struct vring_desc * pkt_desc = NULL;
343 struct vnet_bridge_pkt * virtio_pkt = NULL;
345 pkt_desc = &(q->desc[desc_idx]);
347 PrintDebug("VNET Bridge: Handle TX desc buf_len: %d\n", pkt_desc->length);
349 if (v3_gpa_to_hva(core, pkt_desc->addr_gpa, (addr_t *)&(virtio_pkt)) == -1) {
350 PrintError("Could not translate buffer address\n");
354 PrintDebug("VNET Bridge: TX: on cpu %d pkt size: %d, dst link: %d\n", cpu, virtio_pkt->pkt_size, virtio_pkt->link_id);
356 v3_vnet_rx(virtio_pkt->pkt, virtio_pkt->pkt_size, virtio_pkt->link_id, LINK_EDGE);
358 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
359 q->used->ring[q->used->index % q->queue_size].length = pkt_desc->length; // What do we set this to????
362 vnet_state->pkt_sent++;
369 v3_unlock_irqrestore(vnet_state->lock,flags);
373 //PrintError("In polling get %d\n", recvd);
375 //if on the dom0 core, interrupt the domU core to poll pkts
376 //otherwise, call the polling directly
379 if (vnet_state->vm->cores[0].cpu_id == cpu) {
380 cpu = (cpu == 0) ? 1 : 0;
381 v3_interrupt_cpu(vnet_state->vm, cpu, V3_VNET_POLLING_VECTOR);
386 if ((vnet_state->pkt_sent % (QUEUE_SIZE/20)) == 0) {
387 //optimized for guest's, batch the interrupts
389 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
390 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
391 vnet_state->virtio_cfg.pci_isr = 0x1;
395 #ifdef CONFIG_VNET_PROFILE
396 if (vnet_state->pkt_sent % 200000 == 0)
397 PrintError("Vnet Bridge: sent: %ld, rxed: %ld, dropped: %ld, total exit: %ld, tx exit: %ld, rx exit: %ld\n",
398 vnet_state->pkt_sent,
399 vnet_state->pkt_recv,
400 vnet_state->pkt_drop,
401 vnet_state->total_exit,
403 vnet_state->rx_exit);
406 v3_unlock_irqrestore(vnet_state->lock,flags);
411 static int polling_pkt_from_guest(struct v3_vm_info * vm, void *private_data) {
412 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
414 return handle_pkt_kick(&(vm->cores[0]), vnet_state);
417 static int handle_rx_kick(struct guest_info *core, struct virtio_vnet_state * vnet_state) {
418 v3_vnet_enable_bridge();
423 static int vnet_virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
424 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
425 int port_idx = port % vnet_state->io_range_size;
427 PrintDebug("VNET Bridge: VIRTIO VNET Write for port %d len=%d, value=%x\n",
428 port, length, *(uint32_t *)src);
429 PrintDebug("VNET Bridge: port idx=%d\n", port_idx);
431 vnet_state->total_exit++;
434 case GUEST_FEATURES_PORT:
437 PrintError("Illegal write length for guest features\n");
441 vnet_state->virtio_cfg.guest_features = *(uint32_t *)src;
444 case VRING_PG_NUM_PORT: {
446 addr_t pfn = *(uint32_t *)src;
447 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
450 PrintError("Illegal write length for page frame number\n");
455 vnet_state->cur_queue->pfn = pfn;
457 vnet_state->cur_queue->ring_desc_addr = page_addr ;
458 vnet_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
459 vnet_state->cur_queue->ring_used_addr = ( vnet_state->cur_queue->ring_avail_addr + \
460 sizeof(struct vring_avail) + \
461 (QUEUE_SIZE * sizeof(uint16_t)));
463 // round up to next page boundary.
464 vnet_state->cur_queue->ring_used_addr = (vnet_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
466 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_desc_addr, (addr_t *)&(vnet_state->cur_queue->desc)) == -1) {
467 PrintError("Could not translate ring descriptor address\n");
471 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_avail_addr, (addr_t *)&(vnet_state->cur_queue->avail)) == -1) {
472 PrintError("Could not translate ring available address\n");
476 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_used_addr, (addr_t *)&(vnet_state->cur_queue->used)) == -1) {
477 PrintError("Could not translate ring used address\n");
481 PrintDebug("VNET Bridge: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
482 (void *)(vnet_state->cur_queue->ring_desc_addr),
483 (void *)(vnet_state->cur_queue->ring_avail_addr),
484 (void *)(vnet_state->cur_queue->ring_used_addr));
486 PrintDebug("VNET Bridge: RingDesc=%p, Avail=%p, Used=%p\n",
487 vnet_state->cur_queue->desc,
488 vnet_state->cur_queue->avail,
489 vnet_state->cur_queue->used);
491 if (vnet_state->queue[RECV_QUEUE].avail != NULL){
492 vnet_state->ready = 1;
495 //No notify when there is pkt tx from guest
496 if (vnet_state->queue[XMIT_QUEUE].used != NULL) {
497 vnet_state->queue[XMIT_QUEUE].used->flags |= VRING_NO_NOTIFY_FLAG;
502 case VRING_Q_SEL_PORT:
503 vnet_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
505 if (vnet_state->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
506 PrintError("VNET Bridge device has no qeueues. Selected %d\n",
507 vnet_state->virtio_cfg.vring_queue_selector);
511 vnet_state->cur_queue = &(vnet_state->queue[vnet_state->virtio_cfg.vring_queue_selector]);
514 case VRING_Q_NOTIFY_PORT: {
515 uint16_t queue_idx = *(uint16_t *)src;
517 PrintDebug("VNET Bridge: Handling Kick\n");
519 if (queue_idx == 0) {
520 if (handle_cmd_kick(core, vnet_state) == -1) {
521 PrintError("Could not handle Virtio VNET Control command\n");
524 } else if (queue_idx == 1) {
525 if (handle_pkt_kick(core, vnet_state) == -1){
526 PrintError("Could not handle Virtio VNET TX\n");
529 vnet_state->tx_exit ++;
530 //PrintError("Notify on TX\n");
531 } else if (queue_idx == 2) {
532 if (handle_rx_kick(core, vnet_state) == -1){
533 PrintError("Could not handle Virtio RX buffer refills Kick\n");
536 vnet_state->rx_exit ++;
538 PrintError("VNET Bridge: Kick on invalid queue (%d)\n", queue_idx);
544 case VIRTIO_STATUS_PORT:
545 vnet_state->virtio_cfg.status = *(uint8_t *)src;
547 if (vnet_state->virtio_cfg.status == 0) {
548 PrintDebug("VNET Bridge: Resetting device\n");
549 virtio_reset(vnet_state);
554 case VIRTIO_ISR_PORT:
555 vnet_state->virtio_cfg.pci_isr = *(uint8_t *)src;
566 static int vnet_virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
568 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
569 int port_idx = port % vnet_state->io_range_size;
572 case HOST_FEATURES_PORT:
574 PrintError("Illegal read length for host features\n");
578 *(uint32_t *)dst = vnet_state->virtio_cfg.host_features;
581 case VRING_PG_NUM_PORT:
583 PrintError("Illegal read length for page frame number\n");
587 *(uint32_t *)dst = vnet_state->cur_queue->pfn;
590 case VRING_SIZE_PORT:
592 PrintError("Illegal read length for vring size\n");
596 *(uint16_t *)dst = vnet_state->cur_queue->queue_size;
600 case VIRTIO_STATUS_PORT:
602 PrintError("Illegal read length for status\n");
606 *(uint8_t *)dst = vnet_state->virtio_cfg.status;
609 case VIRTIO_ISR_PORT:
610 *(uint8_t *)dst = vnet_state->virtio_cfg.pci_isr;
611 vnet_state->virtio_cfg.pci_isr = 0;
612 v3_pci_lower_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
616 if ( (port_idx >= sizeof(struct virtio_config)) &&
617 (port_idx < (sizeof(struct virtio_config) + sizeof(struct vnet_config))) ) {
618 int cfg_offset = port_idx - sizeof(struct virtio_config);
619 uint8_t * cfg_ptr = (uint8_t *)&(vnet_state->vnet_cfg);
621 memcpy(dst, cfg_ptr + cfg_offset, length);
624 PrintError("Read of Unhandled Virtio Read\n");
636 static struct v3_device_ops dev_ops = {
643 static int dev_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
644 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
645 struct virtio_vnet_state * vnet_state = NULL;
646 struct pci_device * pci_dev = NULL;
647 char * dev_id = v3_cfg_val(cfg, "ID");
649 PrintDebug("VNET Bridge: Initializing VNET Bridge Control device: %s\n", dev_id);
651 if (pci_bus == NULL) {
652 PrintError("VNET Bridge device require a PCI Bus");
656 vnet_state = (struct virtio_vnet_state *)V3_Malloc(sizeof(struct virtio_vnet_state));
657 memset(vnet_state, 0, sizeof(struct virtio_vnet_state));
661 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, vnet_state);
663 if (v3_attach_device(vm, dev) == -1) {
664 PrintError("Could not attach device %s\n", dev_id);
669 // PCI initialization
671 struct v3_pci_bar bars[6];
672 int num_ports = sizeof(struct virtio_config) + sizeof(struct vnet_config);
673 int tmp_ports = num_ports;
676 // This gets the number of ports, rounded up to a power of 2
677 vnet_state->io_range_size = 1; // must be a power of 2
679 while (tmp_ports > 0) {
681 vnet_state->io_range_size <<= 1;
684 // this is to account for any low order bits being set in num_ports
685 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
686 if ((num_ports & ((vnet_state->io_range_size >> 1) - 1)) == 0) {
687 vnet_state->io_range_size >>= 1;
690 for (i = 0; i < 6; i++) {
691 bars[i].type = PCI_BAR_NONE;
694 bars[0].type = PCI_BAR_IO;
695 bars[0].default_base_port = -1;
696 bars[0].num_ports = vnet_state->io_range_size;
697 bars[0].io_read = vnet_virtio_io_read;
698 bars[0].io_write = vnet_virtio_io_write;
699 bars[0].private_data = vnet_state;
701 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
702 0, PCI_AUTO_DEV_NUM, 0,
703 "LNX_VIRTIO_VNET", bars,
704 NULL, NULL, NULL, vnet_state);
707 PrintError("Could not register PCI Device\n");
711 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
712 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
713 pci_dev->config_header.device_id = VIRTIO_VNET_DEV_ID;
714 pci_dev->config_header.class = PCI_CLASS_MEMORY;
715 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
716 pci_dev->config_header.subsystem_id = VIRTIO_VNET_SUBDEVICE_ID;
717 pci_dev->config_header.intr_pin = 1;
718 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
721 vnet_state->pci_dev = pci_dev;
722 vnet_state->pci_bus = pci_bus;
725 virtio_reset(vnet_state);
727 V3_Print("Registering Virtio device as vnet bridge\n");
728 v3_vnet_add_bridge(vm, vnet_pkt_input_cb, vnet_pkt_input_xcall, polling_pkt_from_guest, 0, 500000, (void *)vnet_state);
735 device_register("LNX_VIRTIO_VNET", dev_init)