Skip to content

Commit

Permalink
virtio: Small refactor for virtio vsock
Browse files Browse the repository at this point in the history
refactor the interfaces between different virtio vsock
layers for a better OO design.

Signed-off-by: Jingyao Zhou <[email protected]>
  • Loading branch information
abrandnewusername committed Jan 16, 2023
1 parent 813be71 commit f376e0b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
*/
#pragma once

typedef void (*vsock_inject_irq_fn_t)(void *cookie);
typedef struct virtio_emul virtio_emul_t;

typedef void (*vsock_tx_forward_fn_t)(int cid, void *buffer, unsigned int len);

struct vsock_passthrough {
typedef struct virtio_vsock_callbacks {
void (*rx_complete)(virtio_emul_t *emul, char *buf, unsigned int len);
} virtio_vsock_callbacks_t;

typedef struct virtio_vsock_passthrough {
int guest_cid;
vsock_inject_irq_fn_t injectIRQ;
void (*injectIRQ)(void *cookie);
vsock_tx_forward_fn_t forward;
void *vsock_data;
} virtio_vsock_passthrough_t;

struct virtio_vsock_driver {
virtio_vsock_passthrough_t backend_fn;
virtio_vsock_callbacks_t emul_cb;
};

typedef int (*vsock_driver_init)(struct vsock_passthrough *driver, ps_io_ops_t io_ops, void *config);
typedef int (*vsock_driver_init)(struct virtio_vsock_driver *driver, ps_io_ops_t io_ops, void *config);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
typedef struct virtio_vsock {
unsigned int iobase;
virtio_emul_t *emul;
struct vsock_passthrough emul_driver_funcs;
struct virtio_vsock_driver *emul_driver;
struct virtio_vsock_passthrough emul_driver_funcs;
ps_io_ops_t ioops;
} virtio_vsock_t;

Expand All @@ -25,4 +26,4 @@ virtio_vsock_t *common_make_virtio_vsock(vm_t *vm,
ioport_type_t port_type,
unsigned int interrupt_pin,
unsigned int interrupt_line,
struct vsock_passthrough backend);
struct virtio_vsock_passthrough backend);
7 changes: 4 additions & 3 deletions libsel4vmmplatsupport/src/drivers/virtio_vsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ static int virtio_vsock_io_out(void *cookie, unsigned int port_no, unsigned int
}


static int emul_vsock_driver_init(struct vsock_passthrough *driver, ps_io_ops_t io_ops, void *config)
static int emul_vsock_driver_init(struct virtio_vsock_driver *driver, ps_io_ops_t io_ops, void *config)
{
virtio_vsock_t *vsock = (virtio_vsock_t *) config;
*driver = vsock->emul_driver_funcs;
driver->backend_fn = vsock->emul_driver_funcs;
vsock->emul_driver = driver;
return 0;
}

Expand Down Expand Up @@ -91,7 +92,7 @@ static vmm_pci_entry_t vmm_virtio_vsock_pci_bar(unsigned int iobase, size_t ioba

virtio_vsock_t *common_make_virtio_vsock(vm_t *vm, vmm_pci_space_t *pci, vmm_io_port_list_t *ioport,
ioport_range_t ioport_range, ioport_type_t port_type, unsigned int interrupt_pin, unsigned int interrupt_line,
struct vsock_passthrough backend)
struct virtio_vsock_passthrough backend)
{
int err = ps_new_stdlib_malloc_ops(&ops.malloc_ops);
ZF_LOGF_IF(err, "Failed to get malloc ops");
Expand Down
15 changes: 10 additions & 5 deletions libsel4vmmplatsupport/src/drivers/virtio_vsock_emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
static char buf[VIRTIO_VSOCK_CAMKES_MTU];

typedef struct vsock_internal {
struct vsock_passthrough driver;
struct virtio_vsock_driver driver;
/* what queue did data come through? */
int queue_num;
} vsock_internal_t;
Expand Down Expand Up @@ -66,7 +66,7 @@ static void emul_vsock_rx_complete(virtio_emul_t *emul, char *buf, unsigned int
/* record that we've used this descriptor chain now */
virtq->last_idx[RX_QUEUE]++;
/* notify the guest that there is something in its used ring */
vsock->driver.injectIRQ(vsock->driver.vsock_data);
vsock->driver.backend_fn.injectIRQ(vsock->driver.backend_fn.vsock_data);
}
}

Expand All @@ -75,6 +75,10 @@ void vsock_rx_complete(virtio_emul_t *emul, char *buf, unsigned int len)
emul_vsock_rx_complete(emul, buf, len);
}

static virtio_vsock_callbacks_t emul_callbacks = {
.rx_complete = vsock_rx_complete
};

static void vsock_handle_packet(virtio_emul_t *emul, void *buffer, unsigned int len)
{
vsock_internal_t *vsock = emul->internal;
Expand All @@ -86,7 +90,7 @@ static void vsock_handle_packet(virtio_emul_t *emul, void *buffer, unsigned int
packet->hdr.len = VIRTIO_VSOCK_CAMKES_MTU - sizeof(struct virtio_vsock_hdr);
}

vsock->driver.forward(cid, buffer, len);
vsock->driver.backend_fn.forward(cid, buffer, len);
}

static void emul_vsock_notify_tx(virtio_emul_t *emul)
Expand Down Expand Up @@ -126,7 +130,7 @@ static void emul_vsock_notify_tx(virtio_emul_t *emul)
idx++;
struct vring_used_elem used_elem = {desc_head, 0};
ring_used_add(emul, &virtq->vring[vsock->queue_num], used_elem);
vsock->driver.injectIRQ(vsock->driver.vsock_data);
vsock->driver.backend_fn.injectIRQ(vsock->driver.backend_fn.vsock_data);
}
/* update which parts of the ring we have processed */
virtq->last_idx[vsock->queue_num] = idx;
Expand All @@ -151,7 +155,7 @@ static bool vsock_device_emul_io_in(struct virtio_emul *emul, unsigned int offse

/* Set VIRTIO_VSOCK_CFG_GUEST_CID in little-endian */
if (offset == VIRTIO_VSOCK_CFG_GUEST_CID) {
*result = vsock->driver.guest_cid;
*result = vsock->driver.backend_fn.guest_cid;
} else {
*result = 0;
}
Expand Down Expand Up @@ -204,6 +208,7 @@ void *vsock_virtio_emul_init(virtio_emul_t *emul, ps_io_ops_t io_ops, vsock_driv
emul->notify = emul_vsock_notify_tx;
emul->device_io_in = vsock_device_emul_io_in;
emul->device_io_out = vsock_device_emul_io_out;
internal->driver.emul_cb = emul_callbacks;

int err = driver(&internal->driver, io_ops, config);
if (err) {
Expand Down

0 comments on commit f376e0b

Please sign in to comment.