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, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vm_guest_mem.h>
23 #include <devices/lnx_virtio_pci.h>
24 #include <palacios/vmm_vnet.h>
25 #include <palacios/vmm_sprintf.h>
26 #include <devices/pci.h>
29 #ifndef CONFIG_DEBUG_VNET_BRG
31 #define PrintDebug(fmt, args...)
35 #define QUEUE_SIZE 128
41 } __attribute__((packed));
48 struct virtio_vnet_state {
49 struct v3_vm_info *vm;
50 struct vnet_config vnet_cfg;
51 struct virtio_config virtio_cfg;
53 struct vm_device * pci_bus;
54 struct pci_device * pci_dev;
56 struct virtio_queue queue[NUM_QUEUES];
58 struct virtio_queue * cur_queue;
64 #define VNET_GET_ROUTES 10
65 #define VNET_ADD_ROUTE 11
66 #define VNET_DEL_ROUTE 12
68 #define VNET_GET_LINKS 20
69 #define VNET_ADD_LINK 21
70 #define VNET_DEL_LINK 22
72 // structure of the vnet command header
73 struct vnet_ctrl_hdr {
76 } __attribute__((packed));
78 static int virtio_reset(struct virtio_vnet_state * vnet_brg) {
80 memset(vnet_brg->queue, 0, sizeof(struct virtio_queue) * 2);
82 vnet_brg->cur_queue = &(vnet_brg->queue[0]);
84 vnet_brg->virtio_cfg.status = 0;
85 vnet_brg->virtio_cfg.pci_isr = 0;
87 vnet_brg->queue[0].queue_size = QUEUE_SIZE;
88 vnet_brg->queue[1].queue_size = QUEUE_SIZE;
89 vnet_brg->queue[2].queue_size = QUEUE_SIZE;
91 memset(&(vnet_brg->vnet_cfg), 0, sizeof(struct vnet_config));
92 v3_lock_init(&(vnet_brg->lock));
99 static int get_desc_count(struct virtio_queue * q, int index) {
100 struct vring_desc * tmp_desc = &(q->desc[index]);
103 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
104 tmp_desc = &(q->desc[tmp_desc->next]);
114 static int handle_cmd_kick(struct guest_info * core, struct virtio_vnet_state * vnet_state) {
115 struct virtio_queue * q = &(vnet_state->queue[0]);
117 PrintDebug("VNET Bridge: Handling command queue\n");
119 while (q->cur_avail_idx < q->avail->index) {
120 struct vring_desc * hdr_desc = NULL;
121 struct vring_desc * buf_desc = NULL;
122 struct vring_desc * status_desc = NULL;
123 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
124 uint16_t desc_cnt = get_desc_count(q, desc_idx);
125 struct vnet_ctrl_hdr * hdr = NULL;
128 uint8_t * status_ptr = NULL;
132 PrintDebug("Descriptor Count=%d, index=%d, desc_idx=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE, desc_idx);
135 PrintError("VNET Bridge cmd must include at least 3 descriptors (cnt=%d)\n", desc_cnt);
139 hdr_desc = &(q->desc[desc_idx]);
141 if (guest_pa_to_host_va(core, hdr_desc->addr_gpa, (addr_t *)&hdr) == -1) {
142 PrintError("Could not translate VirtioVNET header address\n");
146 desc_idx = hdr_desc->next;
148 if (hdr->cmd_type == VNET_ADD_ROUTE) {
150 for (i = 0; i < hdr->num_cmds; i++) {
151 uint8_t tmp_status = 0;
152 struct v3_vnet_route * route = NULL;
154 buf_desc = &(q->desc[desc_idx]);
156 if (guest_pa_to_host_va(core, buf_desc->addr_gpa, (addr_t *)&(route)) == -1) {
157 PrintError("Could not translate route address\n");
162 PrintDebug("Adding VNET Route\n");
164 tmp_status = v3_vnet_add_route(*route);
166 PrintDebug("VNET Route Added\n");
168 if (tmp_status != 0) {
169 PrintError("Error adding VNET ROUTE\n");
173 xfer_len += buf_desc->length;
174 desc_idx = buf_desc->next;
181 status_desc = &(q->desc[desc_idx]);
183 if (guest_pa_to_host_va(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
184 PrintError("VirtioVNET Error could not translate status address\n");
188 xfer_len += status_desc->length;
189 *status_ptr = status;
191 PrintDebug("Transferred %d bytes (xfer_len)\n", xfer_len);
192 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
193 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
200 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
201 PrintDebug("Raising IRQ %d\n", vnet_state->pci_dev->config_header.intr_line);
202 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
203 vnet_state->virtio_cfg.pci_isr = 1;
210 static int copy_data_to_desc(struct guest_info *core,
211 struct vring_desc * desc,
216 uint8_t * desc_buf = NULL;
218 if (guest_pa_to_host_va(core, desc->addr_gpa, (addr_t *)&(desc_buf)) == -1) {
219 PrintError("Could not translate buffer address\n");
222 len = (desc->length < buf_len)?desc->length:buf_len;
223 memcpy(desc_buf, buf, len);
229 static int vnet_brg_rx(struct v3_vnet_pkt *pkt, void * private_data) {
230 struct virtio_vnet_state * vnet_brg = (struct virtio_vnet_state *)private_data;
231 struct virtio_queue * q = &(vnet_brg->queue[RECV_QUEUE]);
232 char *buf = (char *)pkt;
234 int pkt_head_len = ((addr_t)pkt->data) - ((addr_t)pkt);
235 uint32_t data_len = sizeof(struct v3_vnet_pkt); //TODO: should not transfer the whole struct, only the data size
238 flags = v3_lock_irqsave(vnet_brg->lock);
240 PrintDebug("VNET Bridge: RX: pkt sent to guest size: %d\n, pkt_header_len: %d\n", data_len, pkt_head_len);
241 v3_hexdump(buf, 100, NULL, 0);
243 if (q->ring_avail_addr == 0) {
244 PrintError("Queue is not set\n");
249 if (q->last_avail_idx > q->avail->index)
250 q->idx_overflow = true;
251 q->last_avail_idx = q->avail->index;
253 if (q->cur_avail_idx < q->avail->index || (q->idx_overflow && q->cur_avail_idx < q->avail->index+65536)){
254 uint16_t buf_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
257 struct vring_desc * buf_desc = NULL;
259 buf_desc = &(q->desc[buf_idx]);
260 PrintDebug("VNET Bridge RX: buffer desc len: %d\n", buf_desc->length);
262 len = copy_data_to_desc(&(vnet_brg->vm->cores[0]), buf_desc, buf + offset, data_len - offset);
268 buf_desc->length = len;
270 if (offset >= data_len) {
271 buf_desc->flags &= ~VIRTIO_NEXT_FLAG;
273 PrintDebug("VNET Bridge: RX: Pkt not fit into one desc buf\n");
276 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
277 q->used->ring[q->used->index % q->queue_size].length = data_len; // This should be the total length of data sent to guest (header+pkt_data)
280 int last_idx = q->cur_avail_idx;
282 if (q->cur_avail_idx < last_idx)
283 q->idx_overflow = false;
285 PrintDebug("VNET Bridge: Handle RX: Fails to send, no available buffer: current_idx:%d, availIdx: %d\n", q->cur_avail_idx, q->avail->index);
288 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
289 v3_pci_raise_irq(vnet_brg->pci_bus, 0, vnet_brg->pci_dev);
290 vnet_brg->virtio_cfg.pci_isr = 0x1;
291 PrintDebug("Raising IRQ %d\n", vnet_brg->pci_dev->config_header.intr_line);
298 v3_unlock_irqrestore(vnet_brg->lock, flags);
303 static int vnet_send(struct v3_vnet_pkt *pkt, int pkt_len, void * private_data){
304 struct guest_info *core = (struct guest_info *)private_data;
306 #ifdef CONFIG_DEBUG_VNET_BRG
308 PrintDebug("VNET Bridge: send pkt size: %d\n", pkt->size);
309 v3_hexdump(pkt->data,pkt->size, NULL, 0);
313 #ifdef CONFIG_VNET_PROFILE
316 core->vnet_times.time_copy_from_guest = start - core->vnet_times.virtio_handle_start;
319 pkt->src_type = LINK_EDGE;
321 return v3_vnet_send_pkt(pkt, (void *)core);
324 static int pkt_tx(struct guest_info *core, struct virtio_vnet_state * vnet_brg, struct vring_desc * buf_desc)
326 uint8_t * buf = NULL;
327 uint32_t len = buf_desc->length;
328 struct v3_vnet_pkt *pkt;
330 if (guest_pa_to_host_va(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) {
331 PrintError("Could not translate buffer address\n");
335 pkt = (struct v3_vnet_pkt *)buf;
336 if (vnet_send(pkt, len, (void *)core) == -1) {
343 static int handle_pkt_tx(struct guest_info *core, struct virtio_vnet_state * vnet_brg)
345 struct virtio_queue * q = &(vnet_brg->queue[XMIT_QUEUE]);
347 if (q->avail->index < q->last_avail_idx) {
348 q->idx_overflow = true;
351 q->last_avail_idx = q->avail->index;
353 PrintDebug("VNET Bridge Device: Handle TX\n");
355 while (q->cur_avail_idx < q->avail->index ||
356 (q->idx_overflow && q->cur_avail_idx < (q->avail->index + 65536))) {
357 struct vring_desc * buf_desc = NULL;
358 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
360 buf_desc = &(q->desc[desc_idx]);
362 PrintDebug("VNET Bridge: Handle TX buf_len: %d\n", buf_desc->length);
364 if (pkt_tx(core, vnet_brg, buf_desc) == -1) {
365 PrintError("Error handling nic operation\n");
369 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
370 q->used->ring[q->used->index % q->queue_size].length = buf_desc->length; // What do we set this to????
373 int last_idx = q->cur_avail_idx;
375 if (q->cur_avail_idx < last_idx) {
376 q->idx_overflow = false;
380 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
381 v3_pci_raise_irq(vnet_brg->pci_bus, 0, vnet_brg->pci_dev);
382 vnet_brg->virtio_cfg.pci_isr = 0x1;
385 #ifdef CONFIG_VNET_PROFILE
388 core->vnet_times.total_handle_time = time - core->vnet_times.virtio_handle_start;
389 core->vnet_times.print = true;
395 static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
396 struct virtio_vnet_state * vnet_brg = (struct virtio_vnet_state *)private_data;
397 int port_idx = port % vnet_brg->io_range_size;
400 PrintDebug("VNET Bridge: VIRTIO VNET Write for port %d len=%d, value=%x\n",
401 port, length, *(uint32_t *)src);
402 PrintDebug("VNET Bridge: port idx=%d\n", port_idx);
406 case GUEST_FEATURES_PORT:
408 PrintError("Illegal write length for guest features\n");
411 vnet_brg->virtio_cfg.guest_features = *(uint32_t *)src;
414 case VRING_PG_NUM_PORT:
416 addr_t pfn = *(uint32_t *)src;
417 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
419 vnet_brg->cur_queue->pfn = pfn;
421 vnet_brg->cur_queue->ring_desc_addr = page_addr ;
422 vnet_brg->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
423 vnet_brg->cur_queue->ring_used_addr = ( vnet_brg->cur_queue->ring_avail_addr + \
424 sizeof(struct vring_avail) + \
425 (QUEUE_SIZE * sizeof(uint16_t)));
427 // round up to next page boundary.
428 vnet_brg->cur_queue->ring_used_addr = (vnet_brg->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
430 if (guest_pa_to_host_va(core, vnet_brg->cur_queue->ring_desc_addr, (addr_t *)&(vnet_brg->cur_queue->desc)) == -1) {
431 PrintError("Could not translate ring descriptor address\n");
435 if (guest_pa_to_host_va(core, vnet_brg->cur_queue->ring_avail_addr, (addr_t *)&(vnet_brg->cur_queue->avail)) == -1) {
436 PrintError("Could not translate ring available address\n");
440 if (guest_pa_to_host_va(core, vnet_brg->cur_queue->ring_used_addr, (addr_t *)&(vnet_brg->cur_queue->used)) == -1) {
441 PrintError("Could not translate ring used address\n");
445 PrintDebug("VNET Bridge: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
446 (void *)(vnet_brg->cur_queue->ring_desc_addr),
447 (void *)(vnet_brg->cur_queue->ring_avail_addr),
448 (void *)(vnet_brg->cur_queue->ring_used_addr));
450 PrintDebug("VNET Bridge: RingDesc=%p, Avail=%p, Used=%p\n",
451 vnet_brg->cur_queue->desc, vnet_brg->cur_queue->avail, vnet_brg->cur_queue->used);
454 PrintError("Illegal write length for page frame number\n");
458 case VRING_Q_SEL_PORT:
459 vnet_brg->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
461 if (vnet_brg->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
462 PrintError("VNET Bridge device has no qeueues. Selected %d\n",
463 vnet_brg->virtio_cfg.vring_queue_selector);
467 vnet_brg->cur_queue = &(vnet_brg->queue[vnet_brg->virtio_cfg.vring_queue_selector]);
470 case VRING_Q_NOTIFY_PORT: {
471 uint16_t queue_idx = *(uint16_t *)src;
473 PrintDebug("VNET Bridge: Handling Kick\n");
475 if (queue_idx == 0) {
476 if (handle_cmd_kick(core, vnet_brg) == -1) {
477 PrintError("Could not handle Virtio VNET Control command\n");
480 } else if (queue_idx == 1) {
481 if (handle_pkt_tx(core, vnet_brg) == -1){
482 PrintError("Could not handle Virtio VNET TX\n");
485 } else if (queue_idx == 2) {
486 PrintDebug("VNET Bridge: receive kick on RX Queue\n");
488 PrintError("VNET Bridge: Kick on invalid queue (%d)\n", queue_idx);
494 case VIRTIO_STATUS_PORT:
495 vnet_brg->virtio_cfg.status = *(uint8_t *)src;
497 if (vnet_brg->virtio_cfg.status == 0) {
498 PrintDebug("VNET Bridge: Resetting device\n");
499 virtio_reset(vnet_brg);
504 case VIRTIO_ISR_PORT:
505 vnet_brg->virtio_cfg.pci_isr = *(uint8_t *)src;
516 static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
518 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
519 int port_idx = port % vnet_state->io_range_size;
522 PrintDebug("VirtioVNET: VIRTIO SYMBIOTIC Read for port %d (index =%d), length=%d\n",
523 port, port_idx, length);
526 case HOST_FEATURES_PORT:
528 PrintError("Illegal read length for host features\n");
532 *(uint32_t *)dst = vnet_state->virtio_cfg.host_features;
535 case VRING_PG_NUM_PORT:
537 PrintError("Illegal read length for page frame number\n");
541 *(uint32_t *)dst = vnet_state->cur_queue->pfn;
544 case VRING_SIZE_PORT:
546 PrintError("Illegal read length for vring size\n");
550 *(uint16_t *)dst = vnet_state->cur_queue->queue_size;
554 case VIRTIO_STATUS_PORT:
556 PrintError("Illegal read length for status\n");
560 *(uint8_t *)dst = vnet_state->virtio_cfg.status;
563 case VIRTIO_ISR_PORT:
564 *(uint8_t *)dst = vnet_state->virtio_cfg.pci_isr;
565 vnet_state->virtio_cfg.pci_isr = 0;
566 v3_pci_lower_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
570 if ( (port_idx >= sizeof(struct virtio_config)) &&
571 (port_idx < (sizeof(struct virtio_config) + sizeof(struct vnet_config))) ) {
572 int cfg_offset = port_idx - sizeof(struct virtio_config);
573 uint8_t * cfg_ptr = (uint8_t *)&(vnet_state->vnet_cfg);
575 memcpy(dst, cfg_ptr + cfg_offset, length);
578 PrintError("Read of Unhandled Virtio Read\n");
588 static int vnet_brg_input(struct v3_vm_info * vm,
589 struct v3_vnet_pkt * pkt,
590 void * private_data){
592 return vnet_brg_rx(pkt, private_data);
595 static struct v3_device_ops dev_ops = {
603 static int vnet_brg_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
604 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
605 struct virtio_vnet_state * vbrg_state = NULL;
606 struct pci_device * pci_dev = NULL;
607 char * name = v3_cfg_val(cfg, "name");
609 PrintDebug("VNET Bridge: Initializing VNET Bridge Control device: %s\n", name);
611 if (pci_bus == NULL) {
612 PrintError("VNET Bridge device require a PCI Bus");
616 vbrg_state = (struct virtio_vnet_state *)V3_Malloc(sizeof(struct virtio_vnet_state));
617 memset(vbrg_state, 0, sizeof(struct virtio_vnet_state));
621 struct vm_device * dev = v3_allocate_device(name, &dev_ops, vbrg_state);
623 if (v3_attach_device(vm, dev) == -1) {
624 PrintError("Could not attach device %s\n", name);
629 // PCI initialization
631 struct v3_pci_bar bars[6];
632 int num_ports = sizeof(struct virtio_config) + sizeof(struct vnet_config);
633 int tmp_ports = num_ports;
636 // This gets the number of ports, rounded up to a power of 2
637 vbrg_state->io_range_size = 1; // must be a power of 2
639 while (tmp_ports > 0) {
641 vbrg_state->io_range_size <<= 1;
644 // this is to account for any low order bits being set in num_ports
645 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
646 if ((num_ports & ((vbrg_state->io_range_size >> 1) - 1)) == 0) {
647 vbrg_state->io_range_size >>= 1;
650 for (i = 0; i < 6; i++) {
651 bars[i].type = PCI_BAR_NONE;
654 bars[0].type = PCI_BAR_IO;
655 bars[0].default_base_port = -1;
656 bars[0].num_ports = vbrg_state->io_range_size;
657 bars[0].io_read = virtio_io_read;
658 bars[0].io_write = virtio_io_write;
659 bars[0].private_data = vbrg_state;
661 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
662 0, PCI_AUTO_DEV_NUM, 0,
663 "LNX_VIRTIO_VNET", bars,
664 NULL, NULL, NULL, vbrg_state);
667 PrintError("Could not register PCI Device\n");
671 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
672 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
673 pci_dev->config_header.device_id = VIRTIO_VNET_DEV_ID;
674 pci_dev->config_header.class = PCI_CLASS_MEMORY;
675 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
676 pci_dev->config_header.subsystem_id = VIRTIO_VNET_SUBDEVICE_ID;
677 pci_dev->config_header.intr_pin = 1;
678 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
681 vbrg_state->pci_dev = pci_dev;
682 vbrg_state->pci_bus = pci_bus;
685 virtio_reset(vbrg_state);
687 v3_vnet_add_bridge(vm, vnet_brg_input, (void *)vbrg_state);
692 uchar_t dstmac[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
693 uchar_t zeromac[6] = {0,0,0,0,0,0};
695 struct v3_vnet_route route;
697 route.dst_type = LINK_EDGE;
699 route.src_type = LINK_ANY;
701 memcpy(route.dst_mac, dstmac, 6);
702 route.dst_mac_qual = MAC_NONE;
703 memcpy(route.src_mac, zeromac, 6);
704 route.src_mac_qual = MAC_ANY;
706 v3_vnet_add_route(route);
714 device_register("LNX_VNET_BRG", vnet_brg_init)