X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fchar_stream.c;h=445a0ac5eb06f9130e1093efc7d02f36ccecc719;hb=2377d33e71ba625a547b414916949181db2a49da;hp=4590cae2c78a09c13e7a59bd3e2f3c9e6d2ea7fd;hpb=4cc7f00cbe637224fb12baaf0e95f8b0040eb7bd;p=palacios.git diff --git a/palacios/src/devices/char_stream.c b/palacios/src/devices/char_stream.c index 4590cae..445a0ac 100644 --- a/palacios/src/devices/char_stream.c +++ b/palacios/src/devices/char_stream.c @@ -18,7 +18,7 @@ */ #include -#include +#include #include #include #include @@ -27,84 +27,92 @@ struct stream_state { - v3_stream_t stream; + struct v3_stream * stream; struct v3_dev_char_ops char_ops; + struct v3_vm_info * vm; + void * push_fn_arg; }; +static uint64_t stream_input(struct v3_stream * stream, uint8_t * buf, uint64_t len) { + struct stream_state * state = stream->guest_stream_data; + + return state->char_ops.input(state->vm, buf, len, state->push_fn_arg); + +} -static int stream_write(uint8_t * buf, uint64_t length, void * private_data) -{ - struct stream_state *state = (struct stream_state *)private_data; - - return v3_stream_write(state->stream, buf, length); +static uint64_t stream_output(uint8_t * buf, uint64_t length, void * private_data) { + struct stream_state * state = (struct stream_state *)private_data; + + return v3_stream_output(state->stream, buf, length); } +static int stream_free(struct stream_state * state) { + v3_stream_close(state->stream); + + // detach host event + + V3_Free(state); + + return 0; +} static struct v3_device_ops dev_ops = { - .free = NULL, - .reset = NULL, - .start = NULL, - .stop = NULL, + .free = (int (*)(void *))stream_free, }; -static int stream_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) -{ +static int stream_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { char * dev_id = v3_cfg_val(cfg, "ID"); char * stream_name = v3_cfg_val(cfg, "name"); struct stream_state * state = NULL; v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend"); - state = (struct stream_state *)V3_Malloc(sizeof(struct stream_state)); if (state == NULL) { - PrintError("Could not allocate stream backend device\n"); + PrintError(vm, VCORE_NONE, "Could not allocate stream backend device\n"); return -1; } memset(state, 0, sizeof(struct stream_state)); - state->stream = v3_stream_open(vm, stream_name); + struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state); - if (state->stream == NULL) { - PrintError("Could not open stream %s\n", stream_name); + if (dev == NULL) { + PrintError(vm, VCORE_NONE, "Could not allocate device %s\n", dev_id); V3_Free(state); return -1; } - - state->char_ops.write = stream_write; - - struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state); - if (dev == NULL) { - PrintError("Could not allocate device %s\n", dev_id); - return -1; - } - if (v3_attach_device(vm, dev) == -1) { - PrintError("Could not attach device %s\n", dev_id); - V3_Free(state); + state->stream = v3_stream_open(vm, stream_name, stream_input, state); + + if (state->stream == NULL) { + PrintError(vm, VCORE_NONE, "Could not open stream %s\n", stream_name); + v3_remove_device(dev); return -1; } + state->vm = vm; + state->char_ops.output = stream_output; + if (v3_dev_connect_char(vm, v3_cfg_val(frontend_cfg, "tag"), &(state->char_ops), frontend_cfg, state, &(state->push_fn_arg)) == -1) { - PrintError("Could not connect %s to frontend %s\n", + PrintError(vm, VCORE_NONE, "Could not connect %s to frontend %s\n", dev_id, v3_cfg_val(frontend_cfg, "tag")); + v3_remove_device(dev); return -1; } - + return 0; } -device_register("CHAR_STREAM", stream_init) - +device_register("CHAR_STREAM", stream_init);