/* spice-server char device flow control code Copyright (C) 2012-2015 Red Hat, Inc. Red Hat Authors: Yonit Halperin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see . */ #include #include "char-device.h" #include "red-client.h" #include "reds.h" #define CHAR_DEVICE_WRITE_TO_TIMEOUT 100 #define SPICE_CHAR_DEVICE_WAIT_TOKENS_TIMEOUT 30000 #define MAX_POOL_SIZE (10 * 64 * 1024) typedef struct SpiceCharDeviceClientState SpiceCharDeviceClientState; struct SpiceCharDeviceClientState { SpiceCharDeviceState *dev; RedClient *client; int do_flow_control; uint64_t num_client_tokens; uint64_t num_client_tokens_free; /* client messages that were consumed by the device */ uint64_t num_send_tokens; /* send to client */ SpiceTimer *wait_for_tokens_timer; int wait_for_tokens_started; GQueue *send_queue; uint32_t max_send_queue_size; }; struct RedCharDevicePrivate { int running; int active; /* has read/write been performed since the device was started */ int wait_for_migrate_data; uint32_t refs; GList *write_queue; GList *write_bufs_pool; uint64_t cur_pool_size; SpiceCharDeviceWriteBuffer *cur_write_buf; uint8_t *cur_write_buf_pos; SpiceTimer *write_to_dev_timer; uint64_t num_self_tokens; GList *clients; /* list of SpiceCharDeviceClientState */ uint64_t client_tokens_interval; /* frequency of returning tokens to the client */ SpiceCharDeviceInstance *sin; int during_read_from_device; int during_write_to_device; void *opaque; RedsState *reds; }; enum { WRITE_BUFFER_ORIGIN_NONE, WRITE_BUFFER_ORIGIN_CLIENT, WRITE_BUFFER_ORIGIN_SERVER, WRITE_BUFFER_ORIGIN_SERVER_NO_TOKEN, }; G_DEFINE_TYPE(RedCharDevice, red_char_device, G_TYPE_OBJECT) #define RED_CHAR_DEVICE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RED_TYPE_CHAR_DEVICE, RedCharDevicePrivate)) enum { PROP_0, PROP_CHAR_DEV_INSTANCE, PROP_SPICE_SERVER, PROP_CLIENT_TOKENS_INTERVAL, PROP_SELF_TOKENS, PROP_OPAQUE }; enum { RED_CHAR_DEVICE_SELF_TOKEN_CONSUMED, RED_CHAR_DEVICE_SELF_TOKEN_RELEASED, RED_CHAR_DEVICE_LAST_SIGNAL }; static guint signals[RED_CHAR_DEVICE_LAST_SIGNAL]; static void spice_char_device_write_buffer_unref(SpiceCharDeviceWriteBuffer *write_buf); static void spice_char_dev_write_retry(void *opaque); static RedPipeItem * spice_char_device_read_one_msg_from_device(SpiceCharDeviceState *dev) { RedCharDeviceClass *klass = RED_CHAR_DEVICE_GET_CLASS(dev); g_return_val_if_fail(RED_IS_CHAR_DEVICE(dev), NULL); g_return_val_if_fail(klass->read_one_msg_from_device != NULL, NULL); return klass->read_one_msg_from_device(dev->priv->sin, dev->priv->opaque); } static void spice_char_device_send_msg_to_client(SpiceCharDeviceState *dev, RedPipeItem *msg, RedClient *client) { RedCharDeviceClass *klass = RED_CHAR_DEVICE_GET_CLASS(dev); g_return_if_fail(RED_IS_CHAR_DEVICE(dev)); g_return_if_fail(klass->send_msg_to_client != NULL); klass->send_msg_to_client(msg, client, dev->priv->opaque); } static void spice_char_device_send_tokens_to_client(SpiceCharDeviceState *dev, RedClient *client, uint32_t tokens) { RedCharDeviceClass *klass = RED_CHAR_DEVICE_GET_CLASS(dev); g_return_if_fail(RED_IS_CHAR_DEVICE(dev)); g_return_if_fail(klass->send_tokens_to_client != NULL); klass->send_tokens_to_client(client, tokens, dev->priv->opaque); } static void spice_char_device_remove_client(SpiceCharDeviceState *dev, RedClient *client) { RedCharDeviceClass *klass = RED_CHAR_DEVICE_GET_CLASS(dev); g_return_if_fail(RED_IS_CHAR_DEVICE(dev)); g_return_if_fail(klass->remove_client != NULL); klass->remove_client(client, dev->priv->opaque); } static void spice_char_device_write_buffer_free(SpiceCharDeviceWriteBuffer *buf) { if (buf == NULL) return; free(buf->buf); free(buf); } static void spice_char_device_write_buffer_pool_add(SpiceCharDeviceState *dev, SpiceCharDeviceWriteBuffer *buf) { if (buf->refs == 1 && dev->priv->cur_pool_size < MAX_POOL_SIZE) { buf->buf_used = 0; buf->origin = WRITE_BUFFER_ORIGIN_NONE; buf->client = NULL; dev->priv->cur_pool_size += buf->buf_size; dev->priv->write_bufs_pool = g_list_prepend(dev->priv->write_bufs_pool, buf); return; } /* Buffer still being used - just unref for the caller */ spice_char_device_write_buffer_unref(buf); } static void spice_char_device_client_send_queue_free(SpiceCharDeviceState *dev, SpiceCharDeviceClientState *dev_client) { spice_debug("send_queue_empty %d", g_queue_is_empty(dev_client->send_queue)); g_queue_free_full(dev_client->send_queue, red_pipe_item_unref); g_queue_clear(dev_client->send_queue); dev_client->num_send_tokens += g_queue_get_length(dev_client->send_queue); } static void spice_char_device_client_free(SpiceCharDeviceState *dev, SpiceCharDeviceClientState *dev_client) { GList *l, *next; if (dev_client->wait_for_tokens_timer) { reds_core_timer_remove(dev->priv->reds, dev_client->wait_for_tokens_timer); dev_client->wait_for_tokens_timer = NULL; } spice_char_device_client_send_queue_free(dev, dev_client); /* remove write buffers that are associated with the client */ spice_debug("write_queue_is_empty %d", (dev->priv->write_queue == NULL) && !dev->priv->cur_write_buf); l = dev->priv->write_queue; while (l) { SpiceCharDeviceWriteBuffer *write_buf = l->data; next = l->next; if (write_buf->origin == WRITE_BUFFER_ORIGIN_CLIENT && write_buf->client == dev_client->client) { dev->priv->write_queue = g_list_delete_link(dev->priv->write_queue, l); spice_char_device_write_buffer_pool_add(dev, write_buf); } l = next; } if (dev->priv->cur_write_buf && dev->priv->cur_write_buf->origin == WRITE_BUFFER_ORIGIN_CLIENT && dev->priv->cur_write_buf->client == dev_client->client) { dev->priv->cur_write_buf->origin = WRITE_BUFFER_ORIGIN_NONE; dev->priv->cur_write_buf->client = NULL; } dev->priv->clients = g_list_remove(dev->priv->clients, dev_client); free(dev_client); } static void spice_char_device_handle_client_overflow(SpiceCharDeviceClientState *dev_client) { SpiceCharDeviceState *dev = dev_client->dev; spice_printerr("dev %p client %p ", dev, dev_client); spice_char_device_remove_client(dev, dev_client->client); } static SpiceCharDeviceClientState *spice_char_device_client_find(SpiceCharDeviceState *dev, RedClient *client) { GList *item; for (item = dev->priv->clients; item != NULL; item = item->next) { SpiceCharDeviceClientState *dev_client = item->data; if (dev_client->client == client) { return dev_client; } } return NULL; } /*************************** * Reading from the device * **************************/ static void device_client_wait_for_tokens_timeout(void *opaque) { SpiceCharDeviceClientState *dev_client = opaque; spice_char_device_handle_client_overflow(dev_client); } static int spice_char_device_can_send_to_client(SpiceCharDeviceClientState *dev_client) { return !dev_client->do_flow_control || dev_client->num_send_tokens; } static uint64_t spice_char_device_max_send_tokens(SpiceCharDeviceState *dev) { GList *item; uint64_t max = 0; for (item = dev->priv->clients; item != NULL; item = item->next) { SpiceCharDeviceClientState *dev_client = item->data; if (!dev_client->do_flow_control) { max = ~0; break; } if (dev_client->num_send_tokens > max) { max = dev_client->num_send_tokens; } } return max; } static void spice_char_device_add_msg_to_client_queue(SpiceCharDeviceClientState *dev_client, RedPipeItem *msg) { SpiceCharDeviceState *dev = dev_client->dev; if (g_queue_get_length(dev_client->send_queue) >= dev_client->max_send_queue_size) { spice_char_device_handle_client_overflow(dev_client); return; } red_pipe_item_ref(msg); g_queue_push_head(dev_client->send_queue, msg); if (!dev_client->wait_for_tokens_started) { reds_core_timer_start(dev->priv->reds, dev_client->wait_for_tokens_timer, SPICE_CHAR_DEVICE_WAIT_TOKENS_TIMEOUT); dev_client->wait_for_tokens_started = TRUE; } } static void spice_char_device_send_msg_to_clients(SpiceCharDeviceState *dev, RedPipeItem *msg) { GList *item, *next; item = dev->priv->clients; while (item) { next = item->next; SpiceCharDeviceClientState *dev_client = item->data; if (spice_char_device_can_send_to_client(dev_client)) { dev_client->num_send_tokens--; spice_assert(g_queue_is_empty(dev_client->send_queue)); spice_char_device_send_msg_to_client(dev, msg, dev_client->client); /* don't refer to dev_client anymore, it may have been released */ } else { spice_char_device_add_msg_to_client_queue(dev_client, msg); } item = next; } } static int spice_char_device_read_from_device(SpiceCharDeviceState *dev) { uint64_t max_send_tokens; int did_read = FALSE; if (!dev->priv->running || dev->priv->wait_for_migrate_data || !dev->priv->sin) { return FALSE; } /* There are 2 scenarios where we can get called recursively: * 1) spice-vmc vmc_read triggering flush of throttled data, recalling wakeup * (virtio) * 2) in case of sending messages to the client, and unreferencing the * msg, we trigger another read. */ if (dev->priv->during_read_from_device++ > 0) { return FALSE; } max_send_tokens = spice_char_device_max_send_tokens(dev); g_object_ref(RED_CHAR_DEVICE(dev)); /* * Reading from the device only in case at least one of the clients have a free token. * All messages will be discarded if no client is attached to the device */ while ((max_send_tokens || (dev->priv->clients == NULL)) && dev->priv->running) { RedPipeItem *msg; msg = spice_char_device_read_one_msg_from_device(dev); if (!msg) { if (dev->priv->during_read_from_device > 1) { dev->priv->during_read_from_device = 1; continue; /* a wakeup might have been called during the read - make sure it doesn't get lost */ } break; } did_read = TRUE; spice_char_device_send_msg_to_clients(dev, msg); red_pipe_item_unref(msg); max_send_tokens--; } dev->priv->during_read_from_device = 0; if (dev->priv->running) { dev->priv->active = dev->priv->active || did_read; } g_object_unref(RED_CHAR_DEVICE(dev)); return did_read; } static void spice_char_device_client_send_queue_push(SpiceCharDeviceClientState *dev_client) { while (!g_queue_is_empty(dev_client->send_queue)) { GList *msg; if (!spice_char_device_can_send_to_client(dev_client)) { break; } msg = g_queue_pop_tail(dev_client->send_queue); g_assert(msg != NULL); dev_client->num_send_tokens--; spice_char_device_send_msg_to_client(dev_client->dev, msg->data, dev_client->client); red_pipe_item_unref(msg); } } static void spice_char_device_send_to_client_tokens_absorb(SpiceCharDeviceClientState *dev_client, uint32_t tokens) { SpiceCharDeviceState *dev = dev_client->dev; dev_client->num_send_tokens += tokens; if (g_queue_get_length(dev_client->send_queue)) { spice_assert(dev_client->num_send_tokens == tokens); spice_char_device_client_send_queue_push(dev_client); } if (spice_char_device_can_send_to_client(dev_client)) { reds_core_timer_cancel(dev->priv->reds, dev_client->wait_for_tokens_timer); dev_client->wait_for_tokens_started = FALSE; spice_char_device_read_from_device(dev_client->dev); } else if (!g_queue_is_empty(dev_client->send_queue)) { reds_core_timer_start(dev->priv->reds, dev_client->wait_for_tokens_timer, SPICE_CHAR_DEVICE_WAIT_TOKENS_TIMEOUT); dev_client->wait_for_tokens_started = TRUE; } } void spice_char_device_send_to_client_tokens_add(SpiceCharDeviceState *dev, RedClient *client, uint32_t tokens) { SpiceCharDeviceClientState *dev_client; dev_client = spice_char_device_client_find(dev, client); if (!dev_client) { spice_error("client wasn't found dev %p client %p", dev, client); return; } spice_char_device_send_to_client_tokens_absorb(dev_client, tokens); } void spice_char_device_send_to_client_tokens_set(SpiceCharDeviceState *dev, RedClient *client, uint32_t tokens) { SpiceCharDeviceClientState *dev_client; dev_client = spice_char_device_client_find(dev, client); if (!dev_client) { spice_error("client wasn't found dev %p client %p", dev, client); return; } dev_client->num_send_tokens = 0; spice_char_device_send_to_client_tokens_absorb(dev_client, tokens); } /************************** * Writing to the device * ***************************/ static void spice_char_device_client_tokens_add(SpiceCharDeviceState *dev, SpiceCharDeviceClientState *dev_client, uint32_t num_tokens) { if (!dev_client->do_flow_control) { return; } if (num_tokens > 1) { spice_debug("#tokens > 1 (=%u)", num_tokens); } dev_client->num_client_tokens_free += num_tokens; if (dev_client->num_client_tokens_free >= dev->priv->client_tokens_interval) { uint32_t tokens = dev_client->num_client_tokens_free; dev_client->num_client_tokens += dev_client->num_client_tokens_free; dev_client->num_client_tokens_free = 0; spice_char_device_send_tokens_to_client(dev, dev_client->client, tokens); } } static int spice_char_device_write_to_device(SpiceCharDeviceState *dev) { SpiceCharDeviceInterface *sif; int total = 0; int n; if (!dev->priv->running || dev->priv->wait_for_migrate_data || !dev->priv->sin) { return 0; } /* protect against recursion with spice_char_device_wakeup */ if (dev->priv->during_write_to_device++ > 0) { return 0; } g_object_ref(RED_CHAR_DEVICE(dev)); if (dev->priv->write_to_dev_timer) { reds_core_timer_cancel(dev->priv->reds, dev->priv->write_to_dev_timer); } sif = spice_char_device_get_interface(dev->priv->sin); while (dev->priv->running) { uint32_t write_len; if (!dev->priv->cur_write_buf) { GList *item = g_list_last(dev->priv->write_queue); if (!item) { break; } dev->priv->cur_write_buf = item->data; dev->priv->cur_write_buf_pos = dev->priv->cur_write_buf->buf; dev->priv->write_queue = g_list_delete_link(dev->priv->write_queue, item); } write_len = dev->priv->cur_write_buf->buf + dev->priv->cur_write_buf->buf_used - dev->priv->cur_write_buf_pos; n = sif->write(dev->priv->sin, dev->priv->cur_write_buf_pos, write_len); if (n <= 0) { if (dev->priv->during_write_to_device > 1) { dev->priv->during_write_to_device = 1; continue; /* a wakeup might have been called during the write - make sure it doesn't get lost */ } break; } total += n; write_len -= n; if (!write_len) { SpiceCharDeviceWriteBuffer *release_buf = dev->priv->cur_write_buf; dev->priv->cur_write_buf = NULL; spice_char_device_write_buffer_release(dev, release_buf); continue; } dev->priv->cur_write_buf_pos += n; } /* retry writing as long as the write queue is not empty */ if (dev->priv->running) { if (dev->priv->cur_write_buf) { if (dev->priv->write_to_dev_timer) { reds_core_timer_start(dev->priv->reds, dev->priv->write_to_dev_timer, CHAR_DEVICE_WRITE_TO_TIMEOUT); } } else { spice_assert(dev->priv->write_queue == NULL); } dev->priv->active = dev->priv->active || total; } dev->priv->during_write_to_device = 0; g_object_unref(RED_CHAR_DEVICE(dev)); return total; } static void spice_char_dev_write_retry(void *opaque) { SpiceCharDeviceState *dev = opaque; if (dev->priv->write_to_dev_timer) { reds_core_timer_cancel(dev->priv->reds, dev->priv->write_to_dev_timer); } spice_char_device_write_to_device(dev); } static SpiceCharDeviceWriteBuffer *__spice_char_device_write_buffer_get( SpiceCharDeviceState *dev, RedClient *client, int size, int origin, int migrated_data_tokens) { GList *item; SpiceCharDeviceWriteBuffer *ret; if (origin == WRITE_BUFFER_ORIGIN_SERVER && !dev->priv->num_self_tokens) { return NULL; } if ((item = g_list_last(dev->priv->write_bufs_pool))) { ret = item->data; dev->priv->write_bufs_pool = g_list_delete_link(dev->priv->write_bufs_pool, item); dev->priv->cur_pool_size -= ret->buf_size; } else { ret = spice_new0(SpiceCharDeviceWriteBuffer, 1); } spice_assert(!ret->buf_used); if (ret->buf_size < size) { ret->buf = spice_realloc(ret->buf, size); ret->buf_size = size; } ret->origin = origin; if (origin == WRITE_BUFFER_ORIGIN_CLIENT) { spice_assert(client); SpiceCharDeviceClientState *dev_client = spice_char_device_client_find(dev, client); if (dev_client) { if (!migrated_data_tokens && dev_client->do_flow_control && !dev_client->num_client_tokens) { spice_printerr("token violation: dev %p client %p", dev, client); spice_char_device_handle_client_overflow(dev_client); goto error; } ret->client = client; if (!migrated_data_tokens && dev_client->do_flow_control) { dev_client->num_client_tokens--; } } else { /* it is possible that the client was removed due to send tokens underflow, but * the caller still receive messages from the client */ spice_printerr("client not found: dev %p client %p", dev, client); goto error; } } else if (origin == WRITE_BUFFER_ORIGIN_SERVER) { dev->priv->num_self_tokens--; g_signal_emit(G_OBJECT(dev), signals[RED_CHAR_DEVICE_SELF_TOKEN_CONSUMED], 0); } ret->token_price = migrated_data_tokens ? migrated_data_tokens : 1; ret->refs = 1; return ret; error: dev->priv->cur_pool_size += ret->buf_size; dev->priv->write_bufs_pool = g_list_prepend(dev->priv->write_bufs_pool, ret); return NULL; } SpiceCharDeviceWriteBuffer *spice_char_device_write_buffer_get(SpiceCharDeviceState *dev, RedClient *client, int size) { return __spice_char_device_write_buffer_get(dev, client, size, client ? WRITE_BUFFER_ORIGIN_CLIENT : WRITE_BUFFER_ORIGIN_SERVER, 0); } SpiceCharDeviceWriteBuffer *spice_char_device_write_buffer_get_server_no_token( SpiceCharDeviceState *dev, int size) { return __spice_char_device_write_buffer_get(dev, NULL, size, WRITE_BUFFER_ORIGIN_SERVER_NO_TOKEN, 0); } static SpiceCharDeviceWriteBuffer *spice_char_device_write_buffer_ref(SpiceCharDeviceWriteBuffer *write_buf) { spice_assert(write_buf); write_buf->refs++; return write_buf; } static void spice_char_device_write_buffer_unref(SpiceCharDeviceWriteBuffer *write_buf) { spice_assert(write_buf); write_buf->refs--; if (write_buf->refs == 0) spice_char_device_write_buffer_free(write_buf); } void spice_char_device_write_buffer_add(SpiceCharDeviceState *dev, SpiceCharDeviceWriteBuffer *write_buf) { spice_assert(dev); /* caller shouldn't add buffers for client that was removed */ if (write_buf->origin == WRITE_BUFFER_ORIGIN_CLIENT && !spice_char_device_client_find(dev, write_buf->client)) { spice_printerr("client not found: dev %p client %p", dev, write_buf->client); spice_char_device_write_buffer_pool_add(dev, write_buf); return; } dev->priv->write_queue = g_list_prepend(dev->priv->write_queue, write_buf); spice_char_device_write_to_device(dev); } void spice_char_device_write_buffer_release(SpiceCharDeviceState *dev, SpiceCharDeviceWriteBuffer *write_buf) { int buf_origin = write_buf->origin; uint32_t buf_token_price = write_buf->token_price; RedClient *client = write_buf->client; if (!dev) { spice_printerr("no device. write buffer is freed"); spice_char_device_write_buffer_free(write_buf); return; } spice_assert(dev->priv->cur_write_buf != write_buf); spice_char_device_write_buffer_pool_add(dev, write_buf); if (buf_origin == WRITE_BUFFER_ORIGIN_CLIENT) { SpiceCharDeviceClientState *dev_client; spice_assert(client); dev_client = spice_char_device_client_find(dev, client); /* when a client is removed, we remove all the buffers that are associated with it */ spice_assert(dev_client); spice_char_device_client_tokens_add(dev, dev_client, buf_token_price); } else if (buf_origin == WRITE_BUFFER_ORIGIN_SERVER) { dev->priv->num_self_tokens++; g_signal_emit(G_OBJECT(dev), signals[RED_CHAR_DEVICE_SELF_TOKEN_RELEASED], 0); } } /******************************** * char_device_state management * ********************************/ void spice_char_device_state_reset_dev_instance(SpiceCharDeviceState *state, SpiceCharDeviceInstance *sin) { spice_debug("sin %p dev_state %p", sin, state); state->priv->sin = sin; sin->st = state; } void *spice_char_device_state_opaque_get(SpiceCharDeviceState *dev) { return dev->priv->opaque; } void spice_char_device_state_destroy(SpiceCharDeviceState *char_dev) { g_return_if_fail(RED_IS_CHAR_DEVICE(char_dev)); g_object_unref(char_dev); } SpiceCharDeviceClientState *red_char_device_client_new(RedClient *client, int do_flow_control, uint32_t max_send_queue_size, uint32_t num_client_tokens, uint32_t num_send_tokens) { SpiceCharDeviceClientState *dev_client; /* Dev client creation */ dev_client = spice_new0(SpiceCharDeviceClientState, 1); dev_client->client = client; dev_client->send_queue = g_queue_new(); dev_client->max_send_queue_size = max_send_queue_size; dev_client->do_flow_control = do_flow_control; if (do_flow_control) { RedsState *reds; g_object_get(client, "spice-server", &reds, NULL); dev_client->wait_for_tokens_timer = reds_core_timer_add(reds, device_client_wait_for_tokens_timeout, dev_client); if (!dev_client->wait_for_tokens_timer) { spice_error("failed to create wait for tokens timer"); } dev_client->num_client_tokens = num_client_tokens; dev_client->num_send_tokens = num_send_tokens; } else { dev_client->num_client_tokens = ~0; dev_client->num_send_tokens = ~0; } /* end of dev client creation */ return dev_client; } int spice_char_device_client_add(SpiceCharDeviceState *dev, RedClient *client, int do_flow_control, uint32_t max_send_queue_size, uint32_t num_client_tokens, uint32_t num_send_tokens, int wait_for_migrate_data) { SpiceCharDeviceClientState *dev_client; spice_assert(dev); spice_assert(client); if (wait_for_migrate_data && (dev->priv->clients != NULL || dev->priv->active)) { spice_warning("can't restore device %p from migration data. The device " "has already been active", dev); return FALSE; } dev->priv->wait_for_migrate_data = wait_for_migrate_data; spice_debug("dev_state %p client %p", dev, client); dev_client = red_char_device_client_new(client, do_flow_control, max_send_queue_size, num_client_tokens, num_send_tokens); dev_client->dev = dev; dev->priv->clients = g_list_prepend(dev->priv->clients, dev_client); /* Now that we have a client, forward any pending device data */ spice_char_device_wakeup(dev); return TRUE; } void spice_char_device_client_remove(SpiceCharDeviceState *dev, RedClient *client) { SpiceCharDeviceClientState *dev_client; spice_debug("dev_state %p client %p", dev, client); dev_client = spice_char_device_client_find(dev, client); if (!dev_client) { spice_error("client wasn't found"); return; } spice_char_device_client_free(dev, dev_client); if (dev->priv->wait_for_migrate_data) { spice_assert(dev->priv->clients == NULL); dev->priv->wait_for_migrate_data = FALSE; spice_char_device_read_from_device(dev); } if (dev->priv->clients == NULL) { spice_debug("client removed, memory pool will be freed (%lu bytes)", dev->priv->cur_pool_size); g_list_free_full(dev->priv->write_bufs_pool, (GDestroyNotify) spice_char_device_write_buffer_free); dev->priv->write_bufs_pool = NULL; dev->priv->cur_pool_size = 0; } } int spice_char_device_client_exists(SpiceCharDeviceState *dev, RedClient *client) { return (spice_char_device_client_find(dev, client) != NULL); } void spice_char_device_start(SpiceCharDeviceState *dev) { spice_debug("dev_state %p", dev); dev->priv->running = TRUE; g_object_ref(RED_CHAR_DEVICE(dev)); while (spice_char_device_write_to_device(dev) || spice_char_device_read_from_device(dev)); g_object_unref(RED_CHAR_DEVICE(dev)); } void spice_char_device_stop(SpiceCharDeviceState *dev) { spice_debug("dev_state %p", dev); dev->priv->running = FALSE; dev->priv->active = FALSE; if (dev->priv->write_to_dev_timer) { reds_core_timer_cancel(dev->priv->reds, dev->priv->write_to_dev_timer); } } void spice_char_device_reset(SpiceCharDeviceState *dev) { GList *client_item; spice_char_device_stop(dev); dev->priv->wait_for_migrate_data = FALSE; spice_debug("dev_state %p", dev); g_list_free_full(dev->priv->write_queue, (GDestroyNotify) spice_char_device_write_buffer_release); dev->priv->write_queue = NULL; if (dev->priv->cur_write_buf) { SpiceCharDeviceWriteBuffer *release_buf = dev->priv->cur_write_buf; dev->priv->cur_write_buf = NULL; spice_char_device_write_buffer_release(dev, release_buf); } for (client_item = dev->priv->clients; client_item != NULL; client_item = client_item->next) { SpiceCharDeviceClientState *dev_client = client_item->data; spice_char_device_client_send_queue_free(dev, dev_client); } dev->priv->sin = NULL; } void spice_char_device_wakeup(SpiceCharDeviceState *dev) { spice_char_device_write_to_device(dev); spice_char_device_read_from_device(dev); } /************* * Migration * * **********/ void spice_char_device_state_migrate_data_marshall_empty(SpiceMarshaller *m) { SpiceMigrateDataCharDevice *mig_data; spice_debug(NULL); mig_data = (SpiceMigrateDataCharDevice *)spice_marshaller_reserve_space(m, sizeof(*mig_data)); memset(mig_data, 0, sizeof(*mig_data)); mig_data->version = SPICE_MIGRATE_DATA_CHAR_DEVICE_VERSION; mig_data->connected = FALSE; } static void migrate_data_marshaller_write_buffer_free(uint8_t *data, void *opaque) { SpiceCharDeviceWriteBuffer *write_buf = (SpiceCharDeviceWriteBuffer *)opaque; spice_char_device_write_buffer_unref(write_buf); } void spice_char_device_state_migrate_data_marshall(SpiceCharDeviceState *dev, SpiceMarshaller *m) { SpiceCharDeviceClientState *client_state; GList *item; uint32_t *write_to_dev_size_ptr; uint32_t *write_to_dev_tokens_ptr; SpiceMarshaller *m2; /* multi-clients are not supported */ spice_assert(g_list_length(dev->priv->clients) == 1); client_state = g_list_last(dev->priv->clients)->data; /* FIXME: if there were more than one client before the marshalling, * it is possible that the send_queue length > 0, and the send data * should be migrated as well */ spice_assert(g_queue_is_empty(client_state->send_queue)); spice_marshaller_add_uint32(m, SPICE_MIGRATE_DATA_CHAR_DEVICE_VERSION); spice_marshaller_add_uint8(m, 1); /* connected */ spice_marshaller_add_uint32(m, client_state->num_client_tokens); spice_marshaller_add_uint32(m, client_state->num_send_tokens); write_to_dev_size_ptr = (uint32_t *)spice_marshaller_reserve_space(m, sizeof(uint32_t)); write_to_dev_tokens_ptr = (uint32_t *)spice_marshaller_reserve_space(m, sizeof(uint32_t)); *write_to_dev_size_ptr = 0; *write_to_dev_tokens_ptr = 0; m2 = spice_marshaller_get_ptr_submarshaller(m, 0); if (dev->priv->cur_write_buf) { uint32_t buf_remaining = dev->priv->cur_write_buf->buf + dev->priv->cur_write_buf->buf_used - dev->priv->cur_write_buf_pos; spice_marshaller_add_ref_full(m2, dev->priv->cur_write_buf_pos, buf_remaining, migrate_data_marshaller_write_buffer_free, spice_char_device_write_buffer_ref(dev->priv->cur_write_buf) ); *write_to_dev_size_ptr += buf_remaining; if (dev->priv->cur_write_buf->origin == WRITE_BUFFER_ORIGIN_CLIENT) { spice_assert(dev->priv->cur_write_buf->client == client_state->client); (*write_to_dev_tokens_ptr) += dev->priv->cur_write_buf->token_price; } } for (item = g_list_last(dev->priv->write_queue); item != NULL; item = item->prev) { SpiceCharDeviceWriteBuffer *write_buf = item->data; spice_marshaller_add_ref_full(m2, write_buf->buf, write_buf->buf_used, migrate_data_marshaller_write_buffer_free, spice_char_device_write_buffer_ref(write_buf) ); *write_to_dev_size_ptr += write_buf->buf_used; if (write_buf->origin == WRITE_BUFFER_ORIGIN_CLIENT) { spice_assert(write_buf->client == client_state->client); (*write_to_dev_tokens_ptr) += write_buf->token_price; } } spice_debug("migration data dev %p: write_queue size %u tokens %u", dev, *write_to_dev_size_ptr, *write_to_dev_tokens_ptr); } int spice_char_device_state_restore(SpiceCharDeviceState *dev, SpiceMigrateDataCharDevice *mig_data) { SpiceCharDeviceClientState *client_state; uint32_t client_tokens_window; spice_assert(g_list_length(dev->priv->clients) == 1 && dev->priv->wait_for_migrate_data); client_state = g_list_last(dev->priv->clients)->data; if (mig_data->version > SPICE_MIGRATE_DATA_CHAR_DEVICE_VERSION) { spice_error("dev %p error: migration data version %u is bigger than self %u", dev, mig_data->version, SPICE_MIGRATE_DATA_CHAR_DEVICE_VERSION); return FALSE; } spice_assert(!dev->priv->cur_write_buf && (dev->priv->write_queue = NULL)); spice_assert(mig_data->connected); client_tokens_window = client_state->num_client_tokens; /* initial state of tokens */ client_state->num_client_tokens = mig_data->num_client_tokens; /* assumption: client_tokens_window stays the same across severs */ client_state->num_client_tokens_free = client_tokens_window - mig_data->num_client_tokens - mig_data->write_num_client_tokens; client_state->num_send_tokens = mig_data->num_send_tokens; if (mig_data->write_size > 0) { if (mig_data->write_num_client_tokens) { dev->priv->cur_write_buf = __spice_char_device_write_buffer_get(dev, client_state->client, mig_data->write_size, WRITE_BUFFER_ORIGIN_CLIENT, mig_data->write_num_client_tokens); } else { dev->priv->cur_write_buf = __spice_char_device_write_buffer_get(dev, NULL, mig_data->write_size, WRITE_BUFFER_ORIGIN_SERVER, 0); } /* the first write buffer contains all the data that was saved for migration */ memcpy(dev->priv->cur_write_buf->buf, ((uint8_t *)mig_data) + mig_data->write_data_ptr - sizeof(SpiceMigrateDataHeader), mig_data->write_size); dev->priv->cur_write_buf->buf_used = mig_data->write_size; dev->priv->cur_write_buf_pos = dev->priv->cur_write_buf->buf; } dev->priv->wait_for_migrate_data = FALSE; spice_char_device_write_to_device(dev); spice_char_device_read_from_device(dev); return TRUE; } /* FIXME: unused */ void spice_char_device_set_server(SpiceCharDeviceState *dev, void *server) { dev->priv->reds = server; } void* spice_char_device_get_server(SpiceCharDeviceState *dev) { return dev->priv->reds; } SpiceCharDeviceInterface *spice_char_device_get_interface(SpiceCharDeviceInstance *instance) { return SPICE_CONTAINEROF(instance->base.sif, SpiceCharDeviceInterface, base); } static void red_char_device_init_device_instance(RedCharDevice *self) { SpiceCharDeviceInterface *sif; g_return_if_fail(self->priv->reds); if (self->priv->write_to_dev_timer) { reds_core_timer_remove(self->priv->reds, self->priv->write_to_dev_timer); self->priv->write_to_dev_timer = NULL; } if (self->priv->sin == NULL) { return; } sif = spice_char_device_get_interface(self->priv->sin); if (sif->base.minor_version <= 2 || !(sif->flags & SPICE_CHAR_DEVICE_NOTIFY_WRITABLE)) { self->priv->write_to_dev_timer = reds_core_timer_add(self->priv->reds, spice_char_dev_write_retry, self); if (!self->priv->write_to_dev_timer) { spice_error("failed creating char dev write timer"); } } self->priv->sin->st = self; } static void red_char_device_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { RedCharDevice *self = RED_CHAR_DEVICE(object); switch (property_id) { case PROP_CHAR_DEV_INSTANCE: g_value_set_pointer(value, self->priv->sin); break; case PROP_SPICE_SERVER: g_value_set_pointer(value, self->priv->reds); break; case PROP_CLIENT_TOKENS_INTERVAL: g_value_set_uint64(value, self->priv->client_tokens_interval); break; case PROP_SELF_TOKENS: g_value_set_uint64(value, self->priv->num_self_tokens); break; case PROP_OPAQUE: g_value_set_pointer(value, self->priv->opaque); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); } } static void red_char_device_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { RedCharDevice *self = RED_CHAR_DEVICE(object); switch (property_id) { case PROP_CHAR_DEV_INSTANCE: self->priv->sin = g_value_get_pointer(value); break; case PROP_SPICE_SERVER: self->priv->reds = g_value_get_pointer(value); break; case PROP_CLIENT_TOKENS_INTERVAL: self->priv->client_tokens_interval = g_value_get_uint64(value); break; case PROP_SELF_TOKENS: self->priv->num_self_tokens = g_value_get_uint64(value); break; case PROP_OPAQUE: self->priv->opaque = g_value_get_pointer(value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); } } static void red_char_device_on_sin_changed(GObject *object, GParamSpec *pspec G_GNUC_UNUSED, gpointer user_data G_GNUC_UNUSED) { RedCharDevice *self = RED_CHAR_DEVICE(object); red_char_device_init_device_instance(self); } static void red_char_device_finalize(GObject *object) { RedCharDevice *self = RED_CHAR_DEVICE(object); self->priv->cur_pool_size = 0; reds_core_timer_remove(self->priv->reds, self->priv->write_to_dev_timer); g_list_free_full(self->priv->write_queue, (GDestroyNotify) spice_char_device_write_buffer_free); self->priv->write_queue = NULL; g_list_free_full(self->priv->write_bufs_pool, (GDestroyNotify) spice_char_device_write_buffer_free); self->priv->write_bufs_pool = NULL; if (self->priv->cur_write_buf) { spice_char_device_write_buffer_free(self->priv->cur_write_buf); } while (self->priv->clients != NULL) { GList *item = g_list_last(self->priv->clients); SpiceCharDeviceClientState *dev_client = item->data; spice_char_device_client_free(self, dev_client); } self->priv->running = FALSE; G_OBJECT_CLASS(red_char_device_parent_class)->finalize(object); } static void red_char_device_class_init(RedCharDeviceClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); g_type_class_add_private(klass, sizeof (RedCharDevicePrivate)); object_class->get_property = red_char_device_get_property; object_class->set_property = red_char_device_set_property; object_class->finalize = red_char_device_finalize; g_object_class_install_property(object_class, PROP_CHAR_DEV_INSTANCE, g_param_spec_pointer("sin", "Char device instance", "Char device instance", G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); g_object_class_install_property(object_class, PROP_SPICE_SERVER, g_param_spec_pointer("spice-server", "RedsState instance", "RedsState instance", G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); g_object_class_install_property(object_class, PROP_CLIENT_TOKENS_INTERVAL, g_param_spec_uint64("client-tokens-interval", "Client token interval", "Client token interval", 0, G_MAXUINT64, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); g_object_class_install_property(object_class, PROP_SELF_TOKENS, g_param_spec_uint64("self-tokens", "Self tokens", "Self tokens", 0, G_MAXUINT64, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); g_object_class_install_property(object_class, PROP_OPAQUE, g_param_spec_pointer("opaque", "opaque", "User data to pass to callbacks", G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); signals[RED_CHAR_DEVICE_SELF_TOKEN_CONSUMED] = g_signal_new("self-token-consumed", G_OBJECT_CLASS_TYPE(object_class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); /* FIXME: find a better name for the signal? It replaces a * on_free_self_token vfunc whose description was: * « The cb is called when a server (self) message that was addressed to the device, * has been completely written to it » */ signals[RED_CHAR_DEVICE_SELF_TOKEN_RELEASED] = g_signal_new("self-token-released", G_OBJECT_CLASS_TYPE(object_class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); } static void red_char_device_init(RedCharDevice *self) { self->priv = RED_CHAR_DEVICE_PRIVATE(self); g_signal_connect(self, "notify::sin", G_CALLBACK(red_char_device_on_sin_changed), NULL); }