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) 2010, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2010, Cui Zheng <cuizheng@cs.unm.edu>
12 * Copyright (c) 2010, 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 <vnet/vnet.h>
29 #include <palacios/vmm_lock.h>
30 #include <palacios/vmm_util.h>
31 #include <devices/pci.h>
32 #include <palacios/vmm_ethernet.h>
33 #include <palacios/vmm_time.h>
36 #ifndef V3_CONFIG_DEBUG_VIRTIO_NET
38 #define PrintDebug(fmt, args...)
41 #ifndef V3_CONFIG_VNET
42 static int net_debug = 0;
45 #define TX_QUEUE_SIZE 4096
46 #define RX_QUEUE_SIZE 4096
47 #define CTRL_QUEUE_SIZE 64
49 /* The feature bitmap for virtio nic
51 #define VIRTIO_NET_F_CSUM 0 /* Host handles pkts w/ partial csum */
52 #define VIRTIO_NET_F_GUEST_CSUM 1 /* Guest handles pkts w/ partial csum */
53 #define VIRTIO_NET_F_MAC 5 /* Host has given MAC address. */
54 #define VIRTIO_NET_F_GSO 6 /* Host handles pkts w/ any GSO type */
55 #define VIRTIO_NET_F_GUEST_TSO4 7 /* Guest can handle TSOv4 in. */
56 #define VIRTIO_NET_F_GUEST_TSO6 8 /* Guest can handle TSOv6 in. */
57 #define VIRTIO_NET_F_GUEST_ECN 9 /* Guest can handle TSO[6] w/ ECN in. */
58 #define VIRTIO_NET_F_GUEST_UFO 10 /* Guest can handle UFO in. */
59 #define VIRTIO_NET_F_HOST_TSO4 11 /* Host can handle TSOv4 in. */
60 #define VIRTIO_NET_F_HOST_TSO6 12 /* Host can handle TSOv6 in. */
61 #define VIRTIO_NET_F_HOST_ECN 13 /* Host can handle TSO[6] w/ ECN in. */
62 #define VIRTIO_NET_F_HOST_UFO 14 /* Host can handle UFO in. */
63 #define VIRTIO_NET_F_MRG_RXBUF 15 /* Host can merge receive buffers. */
64 #define VIRTIO_NET_F_STATUS 16 /* virtio_net_config.status available */
66 /* Port to get virtio config */
67 #define VIRTIO_NET_CONFIG 20
69 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 << 10))
71 /* for gso_type in virtio_net_hdr */
72 #define VIRTIO_NET_HDR_GSO_NONE 0
73 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* GSO frame, IPv4 TCP (TSO) */
74 #define VIRTIO_NET_HDR_GSO_UDP 3 /* GSO frame, IPv4 UDP (UFO) */
75 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */
76 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */
79 /* for flags in virtio_net_hdr */
80 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */
83 /* First element of the scatter-gather list, used with GSO or CSUM features */
88 uint16_t hdr_len; /* Ethernet + IP + tcp/udp hdrs */
89 uint16_t gso_size; /* Bytes to append to hdr_len per frame */
90 uint16_t csum_start; /* Position to start checksumming from */
91 uint16_t csum_offset; /* Offset after that to place checksum */
92 }__attribute__((packed));
95 /* The header to use when the MRG_RXBUF
96 * feature has been negotiated. */
97 struct virtio_net_hdr_mrg_rxbuf {
98 struct virtio_net_hdr hdr;
99 uint16_t num_buffers; /* Number of merged rx buffers */
102 struct virtio_net_config
104 uint8_t mac[ETH_ALEN]; /* VIRTIO_NET_F_MAC */
106 } __attribute__((packed));
108 struct virtio_dev_state {
109 struct vm_device * pci_bus;
110 struct list_head dev_list;
111 struct v3_vm_info *vm;
113 uint8_t mac[ETH_ALEN];
116 struct virtio_net_state {
117 struct virtio_net_config net_cfg;
118 struct virtio_config virtio_cfg;
120 struct v3_vm_info * vm;
121 struct vm_device * dev;
122 struct pci_device * pci_dev;
127 struct virtio_queue rx_vq; /* idx 0*/
128 struct virtio_queue tx_vq; /* idx 1*/
129 struct virtio_queue ctrl_vq; /* idx 2*/
131 uint8_t mergeable_rx_bufs;
133 struct v3_timer * timer;
134 struct nic_statistics stats;
136 struct v3_dev_net_ops * net_ops;
137 v3_lock_t rx_lock, tx_lock;
139 uint8_t tx_notify, rx_notify;
140 uint32_t tx_pkts, rx_pkts;
144 struct virtio_dev_state * virtio_dev;
145 struct list_head dev_link;
149 static int virtio_init_state(struct virtio_net_state * virtio)
151 virtio->rx_vq.queue_size = RX_QUEUE_SIZE;
152 virtio->tx_vq.queue_size = TX_QUEUE_SIZE;
153 virtio->ctrl_vq.queue_size = CTRL_QUEUE_SIZE;
155 virtio->rx_vq.ring_desc_addr = 0;
156 virtio->rx_vq.ring_avail_addr = 0;
157 virtio->rx_vq.ring_used_addr = 0;
158 virtio->rx_vq.pfn = 0;
159 virtio->rx_vq.cur_avail_idx = 0;
161 virtio->tx_vq.ring_desc_addr = 0;
162 virtio->tx_vq.ring_avail_addr = 0;
163 virtio->tx_vq.ring_used_addr = 0;
164 virtio->tx_vq.pfn = 0;
165 virtio->tx_vq.cur_avail_idx = 0;
167 virtio->ctrl_vq.ring_desc_addr = 0;
168 virtio->ctrl_vq.ring_avail_addr = 0;
169 virtio->ctrl_vq.ring_used_addr = 0;
170 virtio->ctrl_vq.pfn = 0;
171 virtio->ctrl_vq.cur_avail_idx = 0;
173 virtio->virtio_cfg.pci_isr = 0;
175 virtio->mergeable_rx_bufs = 1;
177 virtio->virtio_cfg.host_features = 0 | (1 << VIRTIO_NET_F_MAC);
178 if(virtio->mergeable_rx_bufs) {
179 virtio->virtio_cfg.host_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
182 if ((v3_lock_init(&(virtio->rx_lock)) == -1) ||
183 (v3_lock_init(&(virtio->tx_lock)) == -1)){
184 PrintError("Virtio NIC: Failure to init locks for net_state\n");
190 static int tx_one_pkt(struct guest_info * core,
191 struct virtio_net_state * virtio,
192 struct vring_desc * buf_desc)
194 uint8_t * buf = NULL;
195 uint32_t len = buf_desc->length;
197 if (v3_gpa_to_hva(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) {
198 PrintDebug("Could not translate buffer address\n");
202 V3_Net_Print(2, "Virtio-NIC: virtio_tx: size: %d\n", len);
204 v3_hexdump(buf, len, NULL, 0);
207 if(virtio->net_ops->send(buf, len, virtio->backend_data) < 0){
208 virtio->stats.tx_dropped ++;
212 virtio->stats.tx_pkts ++;
213 virtio->stats.tx_bytes += len;
219 /*copy data into ring buffer */
220 static inline int copy_data_to_desc(struct guest_info * core,
221 struct virtio_net_state * virtio_state,
222 struct vring_desc * desc,
227 uint8_t * desc_buf = NULL;
229 if (v3_gpa_to_hva(core, desc->addr_gpa, (addr_t *)&(desc_buf)) == -1) {
230 PrintDebug("Could not translate buffer address\n");
233 len = (desc->length < (buf_len+dst_offset))?(desc->length - dst_offset):buf_len;
234 memcpy(desc_buf + dst_offset, buf, len);
240 static inline int get_desc_count(struct virtio_queue * q, int index) {
241 struct vring_desc * tmp_desc = &(q->desc[index]);
244 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
245 tmp_desc = &(q->desc[tmp_desc->next]);
252 static inline void enable_cb(struct virtio_queue *queue){
254 queue->used->flags &= ~ VRING_NO_NOTIFY_FLAG;
258 static inline void disable_cb(struct virtio_queue *queue) {
260 queue->used->flags |= VRING_NO_NOTIFY_FLAG;
264 static int handle_pkt_tx(struct guest_info * core,
265 struct virtio_net_state * virtio_state,
268 struct virtio_queue *q = &(virtio_state->tx_vq);
269 int txed = 0, left = 0;
272 if (!q->ring_avail_addr) {
276 flags = v3_lock_irqsave(virtio_state->tx_lock);
277 while (q->cur_avail_idx != q->avail->index) {
278 struct virtio_net_hdr_mrg_rxbuf * hdr = NULL;
279 struct vring_desc * hdr_desc = NULL;
281 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
282 int desc_cnt = get_desc_count(q, desc_idx);
285 PrintError("VNIC: merged rx buffer not supported, desc_cnt %d\n", desc_cnt);
289 hdr_desc = &(q->desc[desc_idx]);
290 if (v3_gpa_to_hva(core, hdr_desc->addr_gpa, &(hdr_addr)) == -1) {
291 PrintError("Could not translate block header address\n");
295 hdr = (struct virtio_net_hdr_mrg_rxbuf *)hdr_addr;
296 desc_idx = hdr_desc->next;
298 V3_Net_Print(2, "Virtio NIC: TX hdr count : %d\n", hdr->num_buffers);
300 /* here we assumed that one ethernet pkt is not splitted into multiple buffer */
301 struct vring_desc * buf_desc = &(q->desc[desc_idx]);
302 if (tx_one_pkt(core, virtio_state, buf_desc) == -1) {
303 PrintError("Virtio NIC: Error handling nic operation\n");
306 if(buf_desc->next & VIRTIO_NEXT_FLAG){
307 PrintError("Virtio NIC: TX more buffer need to read\n");
310 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
311 q->used->ring[q->used->index % q->queue_size].length = buf_desc->length; /* What do we set this to???? */
316 if(++txed >= quote && quote > 0){
317 left = (q->cur_avail_idx != q->avail->index);
322 v3_unlock_irqrestore(virtio_state->tx_lock, flags);
324 if (txed && !(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
325 v3_pci_raise_irq(virtio_state->virtio_dev->pci_bus,
326 0, virtio_state->pci_dev);
327 virtio_state->virtio_cfg.pci_isr = 0x1;
328 virtio_state->stats.rx_interrupts ++;
332 V3_Net_Print(2, "Virtio Handle TX: txed pkts: %d, left %d\n", txed, left);
339 v3_unlock_irqrestore(virtio_state->tx_lock, flags);
344 static int virtio_setup_queue(struct guest_info *core,
345 struct virtio_net_state * virtio_state,
346 struct virtio_queue * queue,
347 addr_t pfn, addr_t page_addr) {
350 queue->ring_desc_addr = page_addr;
351 queue->ring_avail_addr = page_addr + (queue->queue_size * sizeof(struct vring_desc));
352 queue->ring_used_addr = ((queue->ring_avail_addr) +
353 (sizeof(struct vring_avail)) +
354 (queue->queue_size * sizeof(uint16_t)));
356 // round up to next page boundary.
357 queue->ring_used_addr = (queue->ring_used_addr + 0xfff) & ~0xfff;
358 if (v3_gpa_to_hva(core, queue->ring_desc_addr, (addr_t *)&(queue->desc)) == -1) {
359 PrintError("Could not translate ring descriptor address\n");
363 if (v3_gpa_to_hva(core, queue->ring_avail_addr, (addr_t *)&(queue->avail)) == -1) {
364 PrintError("Could not translate ring available address\n");
368 if (v3_gpa_to_hva(core, queue->ring_used_addr, (addr_t *)&(queue->used)) == -1) {
369 PrintError("Could not translate ring used address\n");
373 PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
374 (void *)(queue->ring_desc_addr),
375 (void *)(queue->ring_avail_addr),
376 (void *)(queue->ring_used_addr));
378 PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n",
379 queue->desc, queue->avail, queue->used);
384 static int virtio_io_write(struct guest_info *core,
385 uint16_t port, void * src,
386 uint_t length, void * private_data)
388 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
389 int port_idx = port % virtio->io_range_size;
391 PrintDebug("VIRTIO NIC %p Write for port %d (index=%d) len=%d, value=%x\n",
392 private_data, port, port_idx,
393 length, *(uint32_t *)src);
396 case GUEST_FEATURES_PORT:
398 PrintError("Illegal write length for guest features\n");
401 virtio->virtio_cfg.guest_features = *(uint32_t *)src;
404 case VRING_PG_NUM_PORT:
406 PrintError("Illegal write length for page frame number\n");
409 addr_t pfn = *(uint32_t *)src;
410 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
411 uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector;
414 virtio_setup_queue(core, virtio,
419 virtio_setup_queue(core, virtio,
422 if(virtio->tx_notify == 0){
423 disable_cb(&virtio->tx_vq);
428 virtio_setup_queue(core, virtio,
437 case VRING_Q_SEL_PORT:
438 virtio->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
439 if (virtio->virtio_cfg.vring_queue_selector > 2) {
440 PrintError("Virtio NIC: wrong queue idx: %d\n",
441 virtio->virtio_cfg.vring_queue_selector);
446 case VRING_Q_NOTIFY_PORT:
448 uint16_t queue_idx = *(uint16_t *)src;
450 /* receive queue refill */
451 virtio->stats.tx_interrupts ++;
452 } else if (queue_idx == 1){
453 if (handle_pkt_tx(core, virtio, 0) < 0) {
454 PrintError("Virtio NIC: Error to handle packet TX\n");
457 virtio->stats.tx_interrupts ++;
458 } else if (queue_idx == 2){
461 PrintError("Virtio NIC: Wrong queue index %d\n", queue_idx);
466 case VIRTIO_STATUS_PORT:
467 virtio->virtio_cfg.status = *(uint8_t *)src;
468 if (virtio->virtio_cfg.status == 0) {
469 virtio_init_state(virtio);
473 case VIRTIO_ISR_PORT:
474 virtio->virtio_cfg.pci_isr = *(uint8_t *)src;
485 static int virtio_io_read(struct guest_info *core,
486 uint16_t port, void * dst,
487 uint_t length, void * private_data)
489 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
490 int port_idx = port % virtio->io_range_size;
491 uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector;
493 PrintDebug("Virtio NIC %p: Read for port 0x%x (index =%d), length=%d\n",
494 private_data, port, port_idx, length);
497 case HOST_FEATURES_PORT:
499 PrintError("Virtio NIC: Illegal read length for host features\n");
502 *(uint32_t *)dst = virtio->virtio_cfg.host_features;
505 case VRING_PG_NUM_PORT:
507 PrintError("Virtio NIC: Illegal read length for page frame number\n");
512 *(uint32_t *)dst = virtio->rx_vq.pfn;
515 *(uint32_t *)dst = virtio->tx_vq.pfn;
518 *(uint32_t *)dst = virtio->ctrl_vq.pfn;
525 case VRING_SIZE_PORT:
527 PrintError("Virtio NIC: Illegal read length for vring size\n");
532 *(uint16_t *)dst = virtio->rx_vq.queue_size;
535 *(uint16_t *)dst = virtio->tx_vq.queue_size;
538 *(uint16_t *)dst = virtio->ctrl_vq.queue_size;
545 case VIRTIO_STATUS_PORT:
547 PrintError("Virtio NIC: Illegal read length for status\n");
550 *(uint8_t *)dst = virtio->virtio_cfg.status;
553 case VIRTIO_ISR_PORT:
554 *(uint8_t *)dst = virtio->virtio_cfg.pci_isr;
555 virtio->virtio_cfg.pci_isr = 0;
556 v3_pci_lower_irq(virtio->virtio_dev->pci_bus,
560 case VIRTIO_NET_CONFIG ... VIRTIO_NET_CONFIG + ETH_ALEN:
561 *(uint8_t *)dst = virtio->net_cfg.mac[port_idx-VIRTIO_NET_CONFIG];
565 PrintError("Virtio NIC: Read of Unhandled Virtio Read:%d\n",
574 /* receiving raw ethernet pkt from backend */
575 static int virtio_rx(uint8_t * buf, uint32_t size, void * private_data) {
576 struct virtio_net_state * virtio = (struct virtio_net_state *)private_data;
577 struct virtio_queue * q = &(virtio->rx_vq);
578 struct virtio_net_hdr_mrg_rxbuf hdr;
580 uint8_t kick_guest = 0;
582 V3_Net_Print(2, "Virtio NIC: virtio_rx: size: %d\n", size);
584 if (!q->ring_avail_addr) {
585 V3_Net_Print(2, "Virtio NIC: RX Queue not set\n");
586 virtio->stats.rx_dropped ++;
591 memset(&hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
593 flags = v3_lock_irqsave(virtio->rx_lock);
595 if (q->cur_avail_idx != q->avail->index){
597 struct vring_desc * buf_desc;
598 uint32_t hdr_len, len;
601 hdr_len = (virtio->mergeable_rx_bufs)?
602 sizeof(struct virtio_net_hdr_mrg_rxbuf):
603 sizeof(struct virtio_net_hdr);
605 if(virtio->mergeable_rx_bufs){/* merged buffer */
606 struct vring_desc * hdr_desc;
607 uint16_t old_idx = q->cur_avail_idx;
609 buf_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
610 hdr_desc = &(q->desc[buf_idx]);
612 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
613 virtio, hdr_desc, buf, size, hdr_len);
619 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
620 q->used->ring[q->used->index % q->queue_size].length = hdr_len + offset;
624 while(offset < size) {
625 buf_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
626 buf_desc = &(q->desc[buf_idx]);
628 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
629 virtio, buf_desc, buf+offset, size-offset, 0);
631 V3_Net_Print(2, "Virtio NIC: merged buffer, %d buffer size %d\n",
632 hdr.num_buffers, len);
633 q->cur_avail_idx = old_idx;
637 buf_desc->flags &= ~VIRTIO_NEXT_FLAG;
639 q->used->ring[(q->used->index + hdr.num_buffers) % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
640 q->used->ring[(q->used->index + hdr.num_buffers) % q->queue_size].length = len;
646 copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
647 virtio, hdr_desc, (uchar_t *)&hdr, hdr_len, 0);
648 q->used->index += hdr.num_buffers;
650 buf_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
651 buf_desc = &(q->desc[buf_idx]);
654 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
655 virtio, buf_desc, (uchar_t *)&(hdr.hdr), hdr_len, 0);
657 V3_Net_Print(2, "Virtio NIC: rx copy header error %d, hdr_len %d\n",
662 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
663 virtio, buf_desc, buf, size, hdr_len);
665 V3_Net_Print(2, "Virtio NIC: rx copy data error %d\n", len);
670 /* copy rest of data */
671 while(offset < size &&
672 (buf_desc->flags & VIRTIO_NEXT_FLAG)){
673 buf_desc = &(q->desc[buf_desc->next]);
674 len = copy_data_to_desc(&(virtio->virtio_dev->vm->cores[0]),
675 virtio, buf_desc, buf+offset, size-offset, 0);
681 buf_desc->flags &= ~VIRTIO_NEXT_FLAG;
684 V3_Net_Print(2, "Virtio NIC: rx not enough ring buffer, buffer size %d\n",
689 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
690 q->used->ring[q->used->index % q->queue_size].length = size + hdr_len; /* This should be the total length of data sent to guest (header+pkt_data) */
695 virtio->stats.rx_pkts ++;
696 virtio->stats.rx_bytes += size;
698 V3_Net_Print(2, "Virtio NIC: Guest RX queue is full\n");
699 virtio->stats.rx_dropped ++;
701 /* kick guest to refill RX queue */
705 v3_unlock_irqrestore(virtio->rx_lock, flags);
707 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG) || kick_guest) {
708 V3_Net_Print(2, "Virtio NIC: RX Raising IRQ %d\n",
709 virtio->pci_dev->config_header.intr_line);
711 virtio->virtio_cfg.pci_isr = 0x1;
712 v3_pci_raise_irq(virtio->virtio_dev->pci_bus, 0, virtio->pci_dev);
713 virtio->stats.rx_interrupts ++;
716 /* notify guest if it is in guest mode */
717 if((kick_guest || virtio->rx_notify == 1) &&
718 V3_Get_CPU() != virtio->virtio_dev->vm->cores[0].pcpu_id){
719 v3_interrupt_cpu(virtio->virtio_dev->vm,
720 virtio->virtio_dev->vm->cores[0].pcpu_id,
727 virtio->stats.rx_dropped ++;
728 v3_unlock_irqrestore(virtio->rx_lock, flags);
733 static int virtio_free(struct virtio_dev_state * virtio) {
734 struct virtio_net_state * backend = NULL;
735 struct virtio_net_state * tmp = NULL;
738 list_for_each_entry_safe(backend, tmp, &(virtio->dev_list), dev_link) {
740 // unregister from PCI
742 list_del(&(backend->dev_link));
751 static struct v3_device_ops dev_ops = {
752 .free = (int (*)(void *))virtio_free,
756 static int virtio_poll(int quote, void * data){
757 struct virtio_net_state * virtio = (struct virtio_net_state *)data;
760 return handle_pkt_tx(&(virtio->vm->cores[0]), virtio, quote);
766 static int register_dev(struct virtio_dev_state * virtio,
767 struct virtio_net_state * net_state)
769 struct pci_device * pci_dev = NULL;
770 struct v3_pci_bar bars[6];
771 int num_ports = sizeof(struct virtio_config);
772 int tmp_ports = num_ports;
775 /* This gets the number of ports, rounded up to a power of 2 */
776 net_state->io_range_size = 1;
777 while (tmp_ports > 0) {
779 net_state->io_range_size <<= 1;
782 /* this is to account for any low order bits being set in num_ports
783 * if there are none, then num_ports was already a power of 2 so we shift right to reset it
785 if ((num_ports & ((net_state->io_range_size >> 1) - 1)) == 0) {
786 net_state->io_range_size >>= 1;
789 for (i = 0; i < 6; i++) {
790 bars[i].type = PCI_BAR_NONE;
793 PrintDebug("Virtio NIC: io_range_size = %d\n",
794 net_state->io_range_size);
796 bars[0].type = PCI_BAR_IO;
797 bars[0].default_base_port = -1;
798 bars[0].num_ports = net_state->io_range_size;
799 bars[0].io_read = virtio_io_read;
800 bars[0].io_write = virtio_io_write;
801 bars[0].private_data = net_state;
803 pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE,
804 0, PCI_AUTO_DEV_NUM, 0,
805 "LNX_VIRTIO_NIC", bars,
806 NULL, NULL, NULL, net_state);
809 PrintError("Virtio NIC: Could not register PCI Device\n");
813 PrintDebug("Virtio NIC: registered to PCI bus\n");
815 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
816 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
819 pci_dev->config_header.device_id = VIRTIO_NET_DEV_ID;
820 pci_dev->config_header.class = PCI_CLASS_NETWORK;
821 pci_dev->config_header.subclass = PCI_NET_SUBCLASS_OTHER;
822 pci_dev->config_header.subsystem_id = VIRTIO_NET_SUBDEVICE_ID;
823 pci_dev->config_header.intr_pin = 1;
824 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
826 net_state->pci_dev = pci_dev;
827 net_state->virtio_dev = virtio;
829 memcpy(net_state->net_cfg.mac, virtio->mac, 6);
831 virtio_init_state(net_state);
833 V3_Print("Virtio NIC: Registered Intr Line %d\n", pci_dev->config_header.intr_line);
835 /* Add backend to list of devices */
836 list_add(&(net_state->dev_link), &(virtio->dev_list));
842 #define RATE_UPPER_THRESHOLD 10 /* 10000 pkts per second, around 100Mbits */
843 #define RATE_LOWER_THRESHOLD 1
844 #define PROFILE_PERIOD 10000 /*us*/
846 static void virtio_nic_timer(struct guest_info * core,
847 uint64_t cpu_cycles, uint64_t cpu_freq,
849 struct virtio_net_state * net_state = (struct virtio_net_state *)priv_data;
851 static int profile_ms = 0;
853 if(!net_state->status){ /* VNIC is not in working status */
857 period_us = (1000*cpu_cycles)/cpu_freq;
858 net_state->past_us += period_us;
860 if(net_state->past_us > PROFILE_PERIOD){
861 uint32_t tx_rate, rx_rate;
863 tx_rate = (net_state->stats.tx_pkts - net_state->tx_pkts)/(net_state->past_us/1000); /* pkts/per ms */
864 rx_rate = (net_state->stats.rx_pkts - net_state->rx_pkts)/(net_state->past_us/1000);
866 net_state->tx_pkts = net_state->stats.tx_pkts;
867 net_state->rx_pkts = net_state->stats.rx_pkts;
869 if(tx_rate > RATE_UPPER_THRESHOLD && net_state->tx_notify == 1){
870 V3_Print("Virtio NIC: Switch TX to VMM driven mode\n");
871 disable_cb(&(net_state->tx_vq));
872 net_state->tx_notify = 0;
875 if(tx_rate < RATE_LOWER_THRESHOLD && net_state->tx_notify == 0){
876 V3_Print("Virtio NIC: Switch TX to Guest driven mode\n");
877 enable_cb(&(net_state->tx_vq));
878 net_state->tx_notify = 1;
881 if(rx_rate > RATE_UPPER_THRESHOLD && net_state->rx_notify == 1){
882 V3_Print("Virtio NIC: Switch RX to VMM None notify mode\n");
883 net_state->rx_notify = 0;
886 if(rx_rate < RATE_LOWER_THRESHOLD && net_state->rx_notify == 0){
887 V3_Print("Virtio NIC: Switch RX to VMM notify mode\n");
888 net_state->rx_notify = 1;
891 net_state->past_us = 0;
894 profile_ms += period_us/1000;
895 if(profile_ms > 20000){
896 V3_Net_Print(1, "Virtio NIC: TX: Pkt: %lld, Bytes: %lld\n\t\tRX Pkt: %lld. Bytes: %lld\n\t\tDropped: tx %lld, rx %lld\nInterrupts: tx %d, rx %d\nTotal Exit: %lld\n",
897 net_state->stats.tx_pkts, net_state->stats.tx_bytes,
898 net_state->stats.rx_pkts, net_state->stats.rx_bytes,
899 net_state->stats.tx_dropped, net_state->stats.rx_dropped,
900 net_state->stats.tx_interrupts, net_state->stats.rx_interrupts,
901 net_state->vm->cores[0].num_exits);
906 static struct v3_timer_ops timer_ops = {
907 .update_timer = virtio_nic_timer,
911 static int connect_fn(struct v3_vm_info * info,
912 void * frontend_data,
913 struct v3_dev_net_ops * ops,
915 void * private_data) {
916 struct virtio_dev_state * virtio = (struct virtio_dev_state *)frontend_data;
917 struct virtio_net_state * net_state = (struct virtio_net_state *)V3_Malloc(sizeof(struct virtio_net_state));
919 memset(net_state, 0, sizeof(struct virtio_net_state));
920 register_dev(virtio, net_state);
922 net_state->vm = info;
923 net_state->net_ops = ops;
924 net_state->backend_data = private_data;
925 net_state->virtio_dev = virtio;
926 net_state->tx_notify = 1;
927 net_state->rx_notify = 1;
929 net_state->timer = v3_add_timer(&(info->cores[0]),
930 &timer_ops,net_state);
932 ops->recv = virtio_rx;
933 ops->poll = virtio_poll;
934 ops->config.frontend_data = net_state;
935 ops->config.poll = 1;
936 ops->config.quote = 64;
937 ops->config.fnt_mac = V3_Malloc(ETH_ALEN);
938 memcpy(ops->config.fnt_mac, virtio->mac, ETH_ALEN);
943 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
944 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
945 struct virtio_dev_state * virtio_state = NULL;
946 char * dev_id = v3_cfg_val(cfg, "ID");
948 char * str = v3_cfg_val(cfg, "mac");
949 memcpy(macstr, str, strlen(str));
951 if (pci_bus == NULL) {
952 PrintError("Virtio NIC: Virtio device require a PCI Bus");
956 virtio_state = (struct virtio_dev_state *)V3_Malloc(sizeof(struct virtio_dev_state));
957 memset(virtio_state, 0, sizeof(struct virtio_dev_state));
959 INIT_LIST_HEAD(&(virtio_state->dev_list));
960 virtio_state->pci_bus = pci_bus;
961 virtio_state->vm = vm;
963 if (macstr != NULL && !str2mac(macstr, virtio_state->mac)) {
964 PrintDebug("Virtio NIC: Mac specified %s\n", macstr);
966 random_ethaddr(virtio_state->mac);
969 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, virtio_state);
972 PrintError("Virtio NIC: Could not attach device %s\n", dev_id);
973 V3_Free(virtio_state);
977 if (v3_dev_add_net_frontend(vm, dev_id, connect_fn, (void *)virtio_state) == -1) {
978 PrintError("Virtio NIC: Could not register %s as net frontend\n", dev_id);
979 v3_remove_device(dev);
986 device_register("LNX_VIRTIO_NIC", virtio_init)