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;
66 ulong_t pkt_sent, pkt_recv, pkt_drop, tx_exit, rx_exit, total_exit;
70 #define VNET_GET_ROUTES 10
71 #define VNET_ADD_ROUTE 11
72 #define VNET_DEL_ROUTE 12
74 #define VNET_GET_LINKS 20
75 #define VNET_ADD_LINK 21
76 #define VNET_DEL_LINK 22
78 // structure of the vnet command header
79 struct vnet_ctrl_hdr {
82 } __attribute__((packed));
85 struct vnet_bridge_pkt {
88 uint8_t pkt[ETHERNET_PACKET_LEN];
89 }__attribute__((packed));
92 static int virtio_reset(struct virtio_vnet_state * vnet_state) {
94 memset(vnet_state->queue, 0, sizeof(struct virtio_queue) * NUM_QUEUES);
96 vnet_state->cur_queue = &(vnet_state->queue[0]);
98 vnet_state->virtio_cfg.status = 0;
99 vnet_state->virtio_cfg.pci_isr = 0;
101 vnet_state->queue[0].queue_size = CMD_QUEUE_SIZE;
102 vnet_state->queue[1].queue_size = QUEUE_SIZE;
103 vnet_state->queue[2].queue_size = QUEUE_SIZE;
105 memset(&(vnet_state->vnet_cfg), 0, sizeof(struct vnet_config));
106 v3_lock_init(&(vnet_state->lock));
113 static int get_desc_count(struct virtio_queue * q, int index) {
114 struct vring_desc * tmp_desc = &(q->desc[index]);
117 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
118 tmp_desc = &(q->desc[tmp_desc->next]);
128 static int handle_cmd_kick(struct guest_info * core, struct virtio_vnet_state * vnet_state) {
129 struct virtio_queue * q = &(vnet_state->queue[0]);
131 PrintDebug("VNET Bridge: Handling command queue\n");
133 while (q->cur_avail_idx != q->avail->index) {
134 struct vring_desc * hdr_desc = NULL;
135 struct vring_desc * buf_desc = NULL;
136 struct vring_desc * status_desc = NULL;
137 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
138 uint16_t desc_cnt = get_desc_count(q, desc_idx);
139 struct vnet_ctrl_hdr * hdr = NULL;
142 uint8_t * status_ptr = NULL;
146 PrintDebug("VNET Bridge: CMD: Descriptor Count=%d, index=%d, desc_idx=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE, desc_idx);
149 PrintError("VNET Bridge cmd must include at least 3 descriptors (cnt=%d)\n", desc_cnt);
153 hdr_desc = &(q->desc[desc_idx]);
155 if (v3_gpa_to_hva(core, hdr_desc->addr_gpa, (addr_t *)&hdr) == -1) {
156 PrintError("Could not translate VirtioVNET header address\n");
160 desc_idx = hdr_desc->next;
162 if (hdr->cmd_type == VNET_ADD_ROUTE) {
164 for (i = 0; i < hdr->num_cmds; i++) {
165 uint8_t tmp_status = 0;
166 struct v3_vnet_route * route = NULL;
168 buf_desc = &(q->desc[desc_idx]);
170 if (v3_gpa_to_hva(core, buf_desc->addr_gpa, (addr_t *)&(route)) == -1) {
171 PrintError("Could not translate route address\n");
176 PrintDebug("VNET Bridge: Adding VNET Route\n");
178 tmp_status = v3_vnet_add_route(*route);
180 PrintDebug("VNET Route Added\n");
182 if (tmp_status != 0) {
183 PrintError("Error adding VNET ROUTE\n");
187 xfer_len += buf_desc->length;
188 desc_idx = buf_desc->next;
195 status_desc = &(q->desc[desc_idx]);
197 if (v3_gpa_to_hva(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
198 PrintError("VirtioVNET Error could not translate status address\n");
202 xfer_len += status_desc->length;
203 *status_ptr = status;
205 PrintDebug("Transferred %d bytes (xfer_len)\n", xfer_len);
206 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
207 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
214 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
215 PrintDebug("Raising IRQ %d\n", vnet_state->pci_dev->config_header.intr_line);
216 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
217 vnet_state->virtio_cfg.pci_isr = 1;
225 static int vnet_pkt_input_cb(struct v3_vm_info * vm, struct v3_vnet_pkt vnet_pkts[], uint16_t pkt_num, void * private_data){
226 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
227 struct virtio_queue * q = &(vnet_state->queue[RECV_QUEUE]);
231 struct v3_vnet_pkt *pkt;
236 flags = v3_lock_irqsave(vnet_state->lock);
238 if (q->ring_avail_addr == 0) {
239 PrintError("Queue is not set\n");
243 PrintDebug("VNET Bridge: RX: running on cpu: %d, num of pkts: %d\n", V3_Get_CPU(), pkt_num);
245 for(sent = 0; sent < pkt_num; sent ++) {
246 pkt = &vnet_pkts[sent];
247 vnet_state->pkt_recv ++;
249 if (q->cur_avail_idx != q->avail->index) {
250 uint16_t pkt_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
251 struct vring_desc * pkt_desc = NULL;
252 struct vnet_bridge_pkt * virtio_pkt = NULL;
254 pkt_desc = &(q->desc[pkt_idx]);
255 PrintDebug("VNET Bridge RX: buffer desc len: %d\n", pkt_desc->length);
257 if (v3_gpa_to_hva(&(vm->cores[0]), pkt_desc->addr_gpa, (addr_t *)&(virtio_pkt)) == -1) {
258 PrintError("Could not translate buffer address\n");
262 PrintDebug("VNET Bridge: RX: pkt sent to guest pkt size: %d, dst link: %d\n", pkt->size, pkt->dst_id);
264 // Fill in dst packet buffer
265 virtio_pkt->link_id = pkt->dst_id;
266 virtio_pkt->pkt_size = pkt->size;
267 memcpy(virtio_pkt->pkt, pkt->data, pkt->size);
269 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
270 q->used->ring[q->used->index % q->queue_size].length = sizeof(struct vnet_bridge_pkt);
275 vnet_state->pkt_drop ++;
276 //v3_vnet_disable_bridge();
284 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
285 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
286 vnet_state->virtio_cfg.pci_isr = 0x1;
287 PrintDebug("Raising IRQ %d\n", vnet_state->pci_dev->config_header.intr_line);
293 #ifdef CONFIG_VNET_PROFILE
294 if (vnet_state->pkt_recv % 20000 == 0)
295 PrintError("Vnet Bridge: sent: %ld, rxed: %ld, dropped: %ld, total exit: %ld, tx exit: %ld, rx exit: %ld\n",
296 vnet_state->pkt_sent,
297 vnet_state->pkt_recv,
298 vnet_state->pkt_drop,
299 vnet_state->total_exit,
301 vnet_state->rx_exit);
306 v3_unlock_irqrestore(vnet_state->lock, flags);
311 static int vnet_pkt_input_xcall(void *data){
312 struct v3_vnet_bridge_xcall_args *args = (struct v3_vnet_bridge_xcall_args *)data;
314 return vnet_pkt_input_cb(args->vm, args->vnet_pkts, args->pkt_num, args->private_data);
317 static int handle_pkt_kick(struct guest_info *core, struct virtio_vnet_state * vnet_state)
319 struct virtio_queue * q = &(vnet_state->queue[XMIT_QUEUE]);
322 if (q->ring_avail_addr == 0) {
326 while (q->cur_avail_idx != q->avail->index) {
327 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
328 struct vring_desc * pkt_desc = NULL;
329 struct vnet_bridge_pkt * virtio_pkt = NULL;
331 pkt_desc = &(q->desc[desc_idx]);
333 PrintDebug("VNET Bridge: Handle TX desc buf_len: %d\n", pkt_desc->length);
335 if (v3_gpa_to_hva(core, pkt_desc->addr_gpa, (addr_t *)&(virtio_pkt)) == -1) {
336 PrintError("Could not translate buffer address\n");
340 //PrintDebug("VNET Bridge: TX: on cpu %d pkt size: %d, dst link: %d\n", cpu, virtio_pkt->pkt_size, virtio_pkt->link_id);
342 struct v3_vnet_pkt pkt;
343 pkt.size = virtio_pkt->pkt_size;
344 pkt.src_type = LINK_EDGE;
346 memcpy(pkt.header, virtio_pkt->pkt, ETHERNET_HEADER_LEN);
347 pkt.data = virtio_pkt->pkt;
349 v3_vnet_send_pkt(&pkt, NULL);
351 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
352 q->used->ring[q->used->index % q->queue_size].length = pkt_desc->length; // What do we set this to????
355 vnet_state->pkt_sent ++;
365 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
366 v3_pci_raise_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
367 vnet_state->virtio_cfg.pci_isr = 0x1;
370 //PrintError("Virtio VNET: polling %d pkts\n", recvd);
372 #ifdef CONFIG_VNET_PROFILE
373 if (vnet_state->pkt_sent % 20000 == 0)
374 PrintError("Vnet Bridge: sent: %ld, rxed: %ld, dropped: %ld, total exit: %ld, tx exit: %ld, rx exit: %ld\n",
375 vnet_state->pkt_sent,
376 vnet_state->pkt_recv,
377 vnet_state->pkt_drop,
378 vnet_state->total_exit,
380 vnet_state->rx_exit);
386 static void vnet_virtio_poll(struct v3_vm_info * vm, void *private_data){
387 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
389 if(vm == vnet_state->vm){
390 handle_pkt_kick(&(vm->cores[0]), vnet_state);
394 static int handle_rx_kick(struct guest_info *core, struct virtio_vnet_state * vnet_state)
396 //v3_vnet_enable_bridge();
401 static int vnet_virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
402 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
403 int port_idx = port % vnet_state->io_range_size;
405 PrintDebug("VNET Bridge: VIRTIO VNET Write for port %d len=%d, value=%x\n",
406 port, length, *(uint32_t *)src);
408 vnet_state->total_exit ++;
410 case GUEST_FEATURES_PORT:
412 PrintError("Illegal write length for guest features\n");
415 vnet_state->virtio_cfg.guest_features = *(uint32_t *)src;
418 case VRING_PG_NUM_PORT:
420 addr_t pfn = *(uint32_t *)src;
421 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
423 vnet_state->cur_queue->pfn = pfn;
425 vnet_state->cur_queue->ring_desc_addr = page_addr ;
426 vnet_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
427 vnet_state->cur_queue->ring_used_addr = ( vnet_state->cur_queue->ring_avail_addr + \
428 sizeof(struct vring_avail) + \
429 (QUEUE_SIZE * sizeof(uint16_t)));
431 // round up to next page boundary.
432 vnet_state->cur_queue->ring_used_addr = (vnet_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
434 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_desc_addr, (addr_t *)&(vnet_state->cur_queue->desc)) == -1) {
435 PrintError("Could not translate ring descriptor address\n");
439 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_avail_addr, (addr_t *)&(vnet_state->cur_queue->avail)) == -1) {
440 PrintError("Could not translate ring available address\n");
444 if (v3_gpa_to_hva(core, vnet_state->cur_queue->ring_used_addr, (addr_t *)&(vnet_state->cur_queue->used)) == -1) {
445 PrintError("Could not translate ring used address\n");
449 PrintDebug("VNET Bridge: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
450 (void *)(vnet_state->cur_queue->ring_desc_addr),
451 (void *)(vnet_state->cur_queue->ring_avail_addr),
452 (void *)(vnet_state->cur_queue->ring_used_addr));
454 PrintDebug("VNET Bridge: RingDesc=%p, Avail=%p, Used=%p\n",
455 vnet_state->cur_queue->desc, vnet_state->cur_queue->avail, vnet_state->cur_queue->used);
457 if(vnet_state->queue[RECV_QUEUE].avail != NULL){
458 vnet_state->ready = 1;
459 vnet_state->queue[RECV_QUEUE].used->flags |= VRING_NO_NOTIFY_FLAG;
462 //No notify when there is pkt tx from guest
463 //palacios will do the polling
464 if(vnet_state->queue[XMIT_QUEUE].used != NULL){
465 vnet_state->queue[XMIT_QUEUE].used->flags |= VRING_NO_NOTIFY_FLAG;
468 PrintError("Illegal write length for page frame number\n");
472 case VRING_Q_SEL_PORT:
473 vnet_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
475 if (vnet_state->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
476 PrintError("VNET Bridge device has no qeueues. Selected %d\n",
477 vnet_state->virtio_cfg.vring_queue_selector);
481 vnet_state->cur_queue = &(vnet_state->queue[vnet_state->virtio_cfg.vring_queue_selector]);
484 case VRING_Q_NOTIFY_PORT: {
485 uint16_t queue_idx = *(uint16_t *)src;
487 PrintDebug("VNET Bridge: Handling Kick\n");
489 if (queue_idx == 0) {
490 if (handle_cmd_kick(core, vnet_state) == -1) {
491 PrintError("Could not handle Virtio VNET Control command\n");
494 } else if (queue_idx == 1) {
495 if (handle_pkt_kick(core, vnet_state) == -1){
496 PrintError("Could not handle Virtio VNET TX\n");
499 PrintError("Notify on TX\n");
500 } else if (queue_idx == 2) {
501 if (handle_rx_kick(core, vnet_state) == -1){
502 PrintError("Could not handle Virtio RX buffer refills Kick\n");
505 vnet_state->rx_exit ++;
507 PrintError("VNET Bridge: Kick on invalid queue (%d)\n", queue_idx);
513 case VIRTIO_STATUS_PORT:
514 vnet_state->virtio_cfg.status = *(uint8_t *)src;
516 if (vnet_state->virtio_cfg.status == 0) {
517 PrintDebug("VNET Bridge: Resetting device\n");
518 virtio_reset(vnet_state);
523 case VIRTIO_ISR_PORT:
524 vnet_state->virtio_cfg.pci_isr = *(uint8_t *)src;
535 static int vnet_virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
537 struct virtio_vnet_state * vnet_state = (struct virtio_vnet_state *)private_data;
538 int port_idx = port % vnet_state->io_range_size;
541 case HOST_FEATURES_PORT:
543 PrintError("Illegal read length for host features\n");
547 *(uint32_t *)dst = vnet_state->virtio_cfg.host_features;
550 case VRING_PG_NUM_PORT:
552 PrintError("Illegal read length for page frame number\n");
556 *(uint32_t *)dst = vnet_state->cur_queue->pfn;
559 case VRING_SIZE_PORT:
561 PrintError("Illegal read length for vring size\n");
565 *(uint16_t *)dst = vnet_state->cur_queue->queue_size;
569 case VIRTIO_STATUS_PORT:
571 PrintError("Illegal read length for status\n");
575 *(uint8_t *)dst = vnet_state->virtio_cfg.status;
578 case VIRTIO_ISR_PORT:
579 *(uint8_t *)dst = vnet_state->virtio_cfg.pci_isr;
580 vnet_state->virtio_cfg.pci_isr = 0;
581 v3_pci_lower_irq(vnet_state->pci_bus, 0, vnet_state->pci_dev);
585 if ( (port_idx >= sizeof(struct virtio_config)) &&
586 (port_idx < (sizeof(struct virtio_config) + sizeof(struct vnet_config))) ) {
587 int cfg_offset = port_idx - sizeof(struct virtio_config);
588 uint8_t * cfg_ptr = (uint8_t *)&(vnet_state->vnet_cfg);
590 memcpy(dst, cfg_ptr + cfg_offset, length);
593 PrintError("Read of Unhandled Virtio Read\n");
605 static struct v3_device_ops dev_ops = {
612 static int dev_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
613 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
614 struct virtio_vnet_state * vnet_state = NULL;
615 struct pci_device * pci_dev = NULL;
616 char * name = v3_cfg_val(cfg, "name");
618 PrintDebug("VNET Bridge: Initializing VNET Bridge Control device: %s\n", name);
620 if (pci_bus == NULL) {
621 PrintError("VNET Bridge device require a PCI Bus");
625 vnet_state = (struct virtio_vnet_state *)V3_Malloc(sizeof(struct virtio_vnet_state));
626 memset(vnet_state, 0, sizeof(struct virtio_vnet_state));
630 struct vm_device * dev = v3_allocate_device(name, &dev_ops, vnet_state);
632 if (v3_attach_device(vm, dev) == -1) {
633 PrintError("Could not attach device %s\n", name);
638 // PCI initialization
640 struct v3_pci_bar bars[6];
641 int num_ports = sizeof(struct virtio_config) + sizeof(struct vnet_config);
642 int tmp_ports = num_ports;
645 // This gets the number of ports, rounded up to a power of 2
646 vnet_state->io_range_size = 1; // must be a power of 2
648 while (tmp_ports > 0) {
650 vnet_state->io_range_size <<= 1;
653 // this is to account for any low order bits being set in num_ports
654 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
655 if ((num_ports & ((vnet_state->io_range_size >> 1) - 1)) == 0) {
656 vnet_state->io_range_size >>= 1;
659 for (i = 0; i < 6; i++) {
660 bars[i].type = PCI_BAR_NONE;
663 bars[0].type = PCI_BAR_IO;
664 bars[0].default_base_port = -1;
665 bars[0].num_ports = vnet_state->io_range_size;
666 bars[0].io_read = vnet_virtio_io_read;
667 bars[0].io_write = vnet_virtio_io_write;
668 bars[0].private_data = vnet_state;
670 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
671 0, 5 /*PCI_AUTO_DEV_NUM*/, 0,
672 "LNX_VIRTIO_VNET", bars,
673 NULL, NULL, NULL, vnet_state);
676 PrintError("Could not register PCI Device\n");
680 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
681 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
682 pci_dev->config_header.device_id = VIRTIO_VNET_DEV_ID;
683 pci_dev->config_header.class = PCI_CLASS_MEMORY;
684 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
685 pci_dev->config_header.subsystem_id = VIRTIO_VNET_SUBDEVICE_ID;
686 pci_dev->config_header.intr_pin = 1;
687 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
690 vnet_state->pci_dev = pci_dev;
691 vnet_state->pci_bus = pci_bus;
694 virtio_reset(vnet_state);
696 struct v3_vnet_bridge_ops brg_ops;
697 brg_ops.input = vnet_pkt_input_cb;
698 brg_ops.polling_pkt = vnet_virtio_poll;
699 brg_ops.xcall_input = vnet_pkt_input_xcall;
701 V3_Print("Registering Virtio device as vnet bridge\n");
702 v3_vnet_add_bridge(vm, &brg_ops, (void *)vnet_state);
708 device_register("LNX_VIRTIO_VNET", dev_init)