kvm tools: Cleanup virtio code some more

This patch cleans up some more style problems in virtio code.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Pekka Enberg
2011-05-05 22:45:49 +03:00
committed by Will Deacon
parent 80ffe4d196
commit 407475bf94
5 changed files with 86 additions and 87 deletions
+38 -48
View File
@@ -51,11 +51,7 @@ struct blk_dev {
static struct blk_dev *bdevs[VIRTIO_BLK_MAX_DEV];
static bool virtio_blk_pci_io_device_specific_in(struct blk_dev *bdev,
void *data,
unsigned long offset,
int size,
u32 count)
static bool virtio_blk_dev_in(struct blk_dev *bdev, void *data, unsigned long offset, int size, u32 count)
{
u8 *config_space = (u8 *) &bdev->blk_config;
@@ -68,23 +64,19 @@ static bool virtio_blk_pci_io_device_specific_in(struct blk_dev *bdev,
}
/* Translate port into device id + offset in that device addr space */
static void virtio_blk_port2dev(u16 port,
u16 base,
u16 size,
u16 *dev_idx,
u16 *offset)
static void virtio_blk_port2dev(u16 port, u16 base, u16 size, u16 *dev_idx, u16 *offset)
{
*dev_idx = (port - base) / size;
*offset = port - (base + *dev_idx * size);
}
static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int size, u32 count)
{
u16 offset, dev_idx;
bool ret = true;
struct blk_dev *bdev;
struct blk_dev *bdev;
u16 offset, dev_idx;
bool ret = true;
virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE,
&dev_idx, &offset);
virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, &dev_idx, &offset);
bdev = bdevs[dev_idx];
@@ -118,7 +110,8 @@ static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int siz
ioport__write16(data, bdev->config_vector);
break;
default:
ret = virtio_blk_pci_io_device_specific_in(bdev, data, offset, size, count);
ret = virtio_blk_dev_in(bdev, data, offset, size, count);
break;
};
mutex_unlock(&bdev->mutex);
@@ -143,23 +136,15 @@ static bool virtio_blk_do_io_request(struct kvm *self,
switch (req->type) {
case VIRTIO_BLK_T_IN:
block_cnt = disk_image__read_sector_iov(bdev->disk,
req->sector,
iov + 1,
in + out - 2);
block_cnt = disk_image__read_sector_iov(bdev->disk, req->sector, iov + 1, in + out - 2);
break;
case VIRTIO_BLK_T_OUT:
block_cnt = disk_image__write_sector_iov(bdev->disk,
req->sector,
iov + 1,
in + out - 2);
block_cnt = disk_image__write_sector_iov(bdev->disk, req->sector, iov + 1, in + out - 2);
break;
default:
warning("request type %d", req->type);
block_cnt = -1;
break;
}
/* status */
@@ -173,9 +158,12 @@ static bool virtio_blk_do_io_request(struct kvm *self,
static void virtio_blk_do_io(struct kvm *kvm, void *param)
{
struct blk_dev_job *job = param;
struct virt_queue *vq = job->vq;
struct blk_dev *bdev = job->bdev;
struct blk_dev_job *job = param;
struct virt_queue *vq;
struct blk_dev *bdev;
vq = job->vq;
bdev = job->bdev;
while (virt_queue__available(vq))
virtio_blk_do_io_request(kvm, bdev, vq);
@@ -185,12 +173,11 @@ static void virtio_blk_do_io(struct kvm *kvm, void *param)
static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int size, u32 count)
{
struct blk_dev *bdev;
u16 offset, dev_idx;
bool ret = true;
struct blk_dev *bdev;
virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE,
&dev_idx, &offset);
virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, &dev_idx, &offset);
bdev = bdevs[dev_idx];
@@ -207,13 +194,13 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si
job = &bdev->jobs[bdev->queue_selector];
queue = &bdev->vqs[bdev->queue_selector];
queue->pfn = ioport__read32(data);
p = guest_flat_to_host(self, queue->pfn << 12);
queue = &bdev->vqs[bdev->queue_selector];
queue->pfn = ioport__read32(data);
p = guest_flat_to_host(self, queue->pfn << 12);
vring_init(&queue->vring, VIRTIO_BLK_QUEUE_SIZE, p, 4096);
*job = (struct blk_dev_job) {
*job = (struct blk_dev_job) {
.vq = queue,
.bdev = bdev,
};
@@ -223,24 +210,27 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si
break;
}
case VIRTIO_PCI_QUEUE_SEL:
bdev->queue_selector = ioport__read16(data);
bdev->queue_selector = ioport__read16(data);
break;
case VIRTIO_PCI_QUEUE_NOTIFY: {
u16 queue_index;
queue_index = ioport__read16(data);
queue_index = ioport__read16(data);
thread_pool__do_job(bdev->jobs[queue_index].job_id);
break;
}
case VIRTIO_PCI_STATUS:
bdev->status = ioport__read8(data);
bdev->status = ioport__read8(data);
break;
case VIRTIO_MSI_CONFIG_VECTOR:
bdev->config_vector = VIRTIO_MSI_NO_VECTOR;
bdev->config_vector = VIRTIO_MSI_NO_VECTOR;
break;
case VIRTIO_MSI_QUEUE_VECTOR:
break;
default:
ret = false;
ret = false;
break;
};
mutex_unlock(&bdev->mutex);
@@ -273,26 +263,26 @@ static int virtio_blk_find_empty_dev(void)
void virtio_blk__init(struct kvm *self, struct disk_image *disk)
{
int new_dev_idx;
u16 blk_dev_base_addr;
struct blk_dev *bdev;
int new_dev_idx;
if (!disk)
return;
new_dev_idx = virtio_blk_find_empty_dev();
new_dev_idx = virtio_blk_find_empty_dev();
if (new_dev_idx < 0)
die("Could not find an empty block device slot");
bdevs[new_dev_idx] = calloc(1, sizeof(struct blk_dev));
bdevs[new_dev_idx] = calloc(1, sizeof(struct blk_dev));
if (bdevs[new_dev_idx] == NULL)
die("Failed allocating bdev");
bdev = bdevs[new_dev_idx];
bdev = bdevs[new_dev_idx];
blk_dev_base_addr = IOPORT_VIRTIO_BLK + new_dev_idx * IOPORT_VIRTIO_BLK_SIZE;
blk_dev_base_addr = IOPORT_VIRTIO_BLK + new_dev_idx * IOPORT_VIRTIO_BLK_SIZE;
*bdev = (struct blk_dev) {
*bdev = (struct blk_dev) {
.mutex = PTHREAD_MUTEX_INITIALIZER,
.disk = disk,
.idx = new_dev_idx,
+16 -16
View File
@@ -147,11 +147,11 @@ static bool virtio_console_pci_io_in(struct kvm *self, u16 port, void *data, int
static void virtio_console_handle_callback(struct kvm *self, void *param)
{
struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE];
struct virt_queue *vq;
u16 out, in;
u16 head;
u32 len;
struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE];
struct virt_queue *vq;
u16 out, in;
u16 head;
u32 len;
vq = param;
@@ -166,8 +166,8 @@ static void virtio_console_handle_callback(struct kvm *self, void *param)
static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, int size, u32 count)
{
unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
bool ret = true;
unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
bool ret = true;
mutex_lock(&cdev.mutex);
@@ -181,18 +181,16 @@ static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, in
assert(cdev.queue_selector < VIRTIO_CONSOLE_NUM_QUEUES);
queue = &cdev.vqs[cdev.queue_selector];
queue->pfn = ioport__read32(data);
p = guest_flat_to_host(self, queue->pfn << 12);
queue = &cdev.vqs[cdev.queue_selector];
queue->pfn = ioport__read32(data);
p = guest_flat_to_host(self, queue->pfn << 12);
vring_init(&queue->vring, VIRTIO_CONSOLE_QUEUE_SIZE, p, 4096);
if (cdev.queue_selector == VIRTIO_CONSOLE_TX_QUEUE)
cdev.jobs[cdev.queue_selector] =
thread_pool__add_job(self, virtio_console_handle_callback, queue);
cdev.jobs[cdev.queue_selector] = thread_pool__add_job(self, virtio_console_handle_callback, queue);
else if (cdev.queue_selector == VIRTIO_CONSOLE_RX_QUEUE)
cdev.jobs[cdev.queue_selector] =
thread_pool__add_job(self, virtio_console__inject_interrupt_callback, queue);
cdev.jobs[cdev.queue_selector] = thread_pool__add_job(self, virtio_console__inject_interrupt_callback, queue);
break;
}
@@ -214,15 +212,17 @@ static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, in
break;
default:
ret = false;
break;
};
mutex_unlock(&cdev.mutex);
return ret;
}
static struct ioport_operations virtio_console_io_ops = {
.io_in = virtio_console_pci_io_in,
.io_out = virtio_console_pci_io_out,
.io_in = virtio_console_pci_io_in,
.io_out = virtio_console_pci_io_out,
};
#define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4
+1
View File
@@ -10,6 +10,7 @@
struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, u32 head, u32 len)
{
struct vring_used_elem *used_elem;
used_elem = &queue->vring.used->ring[queue->vring.used->idx % queue->vring.num];
used_elem->id = head;
used_elem->len = len;
+21 -15
View File
@@ -56,17 +56,17 @@ static struct net_device net_device = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
.net_config = {
.mac = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
.mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
.status = VIRTIO_NET_S_LINK_UP,
},
.host_features = 1UL << VIRTIO_NET_F_MAC |
1UL << VIRTIO_NET_F_CSUM |
1UL << VIRTIO_NET_F_HOST_UFO |
1UL << VIRTIO_NET_F_HOST_TSO4 |
1UL << VIRTIO_NET_F_HOST_TSO6 |
1UL << VIRTIO_NET_F_GUEST_UFO |
1UL << VIRTIO_NET_F_GUEST_TSO4 |
1UL << VIRTIO_NET_F_GUEST_TSO6,
.host_features = 1UL << VIRTIO_NET_F_MAC
| 1UL << VIRTIO_NET_F_CSUM
| 1UL << VIRTIO_NET_F_HOST_UFO
| 1UL << VIRTIO_NET_F_HOST_TSO4
| 1UL << VIRTIO_NET_F_HOST_TSO6
| 1UL << VIRTIO_NET_F_GUEST_UFO
| 1UL << VIRTIO_NET_F_GUEST_TSO4
| 1UL << VIRTIO_NET_F_GUEST_TSO6,
};
static void *virtio_net_rx_thread(void *p)
@@ -130,9 +130,11 @@ static void *virtio_net_tx_thread(void *p)
}
pthread_exit(NULL);
return NULL;
}
static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offset, int size, u32 count)
{
u8 *config_space = (u8 *) &net_device.net_config;
@@ -193,18 +195,21 @@ static bool virtio_net_pci_io_in(struct kvm *self, u16 port, void *data, int siz
static void virtio_net_handle_callback(struct kvm *self, u16 queue_index)
{
if (queue_index == VIRTIO_NET_TX_QUEUE) {
switch (queue_index) {
case VIRTIO_NET_TX_QUEUE: {
mutex_lock(&net_device.io_tx_mutex);
pthread_cond_signal(&net_device.io_tx_cond);
mutex_unlock(&net_device.io_tx_mutex);
} else if (queue_index == VIRTIO_NET_RX_QUEUE) {
break;
}
case VIRTIO_NET_RX_QUEUE: {
mutex_lock(&net_device.io_rx_mutex);
pthread_cond_signal(&net_device.io_rx_cond);
mutex_unlock(&net_device.io_rx_mutex);
break;
}
default:
warning("Unknown queue index %u", queue_index);
}
}
@@ -255,6 +260,7 @@ static bool virtio_net_pci_io_out(struct kvm *self, u16 port, void *data, int si
};
mutex_unlock(&net_device.mutex);
return ret;
}
+10 -8
View File
@@ -46,8 +46,8 @@ static struct rng_dev rdev;
static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size, u32 count)
{
unsigned long offset;
bool ret = true;
unsigned long offset;
bool ret = true;
offset = port - IOPORT_VIRTIO_RNG;
@@ -76,6 +76,7 @@ static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size
break;
default:
ret = false;
break;
};
return ret;
@@ -84,11 +85,12 @@ static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size
static bool virtio_rng_do_io_request(struct kvm *kvm, struct virt_queue *queue)
{
struct iovec iov[VIRTIO_RNG_QUEUE_SIZE];
u16 out, in, head;
unsigned int len = 0;
u16 out, in, head;
head = virt_queue__get_iov(queue, iov, &out, &in, kvm);
len = readv(rdev.fd, iov, in);
head = virt_queue__get_iov(queue, iov, &out, &in, kvm);
len = readv(rdev.fd, iov, in);
virt_queue__set_used_elem(queue, head, len);
return true;
@@ -109,7 +111,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz
unsigned long offset;
bool ret = true;
offset = port - IOPORT_VIRTIO_RNG;
offset = port - IOPORT_VIRTIO_RNG;
switch (offset) {
case VIRTIO_MSI_QUEUE_VECTOR:
@@ -125,8 +127,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz
vring_init(&queue->vring, VIRTIO_RNG_QUEUE_SIZE, p, 4096);
rdev.jobs[rdev.queue_selector] =
thread_pool__add_job(kvm, virtio_rng_do_io, queue);
rdev.jobs[rdev.queue_selector] = thread_pool__add_job(kvm, virtio_rng_do_io, queue);
break;
}
@@ -147,6 +148,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz
break;
default:
ret = false;
break;
};
return ret;