From 3f757b846c829d1f5d75eeedb2c34d3ab34d5a50 Mon Sep 17 00:00:00 2001 From: Yonit Halperin Date: Wed, 17 Mar 2010 18:43:41 +0200 Subject: new migration process #576029 #576031 #576033 - the server acquires the target info from a dedicated Qemu command - when migration ends, the client receieves a new message RED_MIGRATE_SWITCH_HOST - client then disconnects from the source and connects to the target. The connection to the target is entirely new. --- client/application.cpp | 64 +++++++++++++++++++- client/application.h | 22 +++++++ client/red_client.cpp | 20 ++++++- client/red_client.h | 1 + common/red.h | 13 ++++- server/reds.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++- server/spice.h | 11 ++++ 7 files changed, 281 insertions(+), 6 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index 8a010e4f..094fa933 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -51,6 +51,8 @@ #define CA_FILE_NAME "spice_truststore.pem" +#define SWITCH_HOST_TIMEOUT 150 + #ifdef CAIRO_CANVAS_CACH_IS_SHARED mutex_t cairo_surface_user_data_mutex; #endif @@ -65,16 +67,26 @@ void ConnectedEvent::response(AbstractProcessLoop& events_loop) void DisconnectedEvent::response(AbstractProcessLoop& events_loop) { Application* app = static_cast(events_loop.get_owner()); + + if (app->_during_host_switch) { + app->_client.connect(); + app->activate_interval_timer(*app->_switch_host_timer, SWITCH_HOST_TIMEOUT); + // todo: add indication for migration + app->show_splash(0); + } else { #ifdef RED_DEBUG - app->show_splash(0); + app->show_splash(0); #else - app->do_quit(SPICEC_ERROR_CODE_SUCCESS); + app->do_quit(SPICEC_ERROR_CODE_SUCCESS); #endif + } } void ConnectionErrorEvent::response(AbstractProcessLoop& events_loop) { Application* app = static_cast(events_loop.get_owner()); + + app->_during_host_switch = false; #ifdef RED_DEBUG app->show_splash(0); #else @@ -96,6 +108,32 @@ void MonitorsQuery::do_response(AbstractProcessLoop& events_loop) } } +SwitchHostEvent::SwitchHostEvent(const char* host, int port, int sport, const char* cert_subject) +{ + _host = host; + _port = port; + _sport = sport; + if (cert_subject) { + _cert_subject = cert_subject; + } +} + +void SwitchHostTimer::response(AbstractProcessLoop& events_loop) { + Application* app = (Application*)events_loop.get_owner(); + + if (app->_during_host_switch) { + app->do_connect(); + } else { + app->deactivate_interval_timer(this); + } +} + +void SwitchHostEvent::response(AbstractProcessLoop& events_loop) +{ + Application* app = static_cast(events_loop.get_owner()); + app->switch_host(_host, _port, _sport, _cert_subject); +} + class GUILayer: public ScreenLayer { public: GUILayer(); @@ -279,6 +317,7 @@ Application::Application() , _title (L"SPICEc:%d") , _splash_mode (true) , _sys_key_intercept_mode (false) + , _during_host_switch(false) { DBG(0, ""); Platform::set_process_loop(*this); @@ -327,6 +366,8 @@ Application::Application() _sticky_info.key_down = false; _sticky_info.key = REDKEY_INVALID; _sticky_info.timer.reset(new StickyKeyTimer()); + + _switch_host_timer.reset(new SwitchHostTimer()); } Application::~Application() @@ -392,6 +433,21 @@ void Application::connect() _client.connect(); } +void Application::switch_host(const std::string& host, int port, int sport, + const std::string& cert_subject) +{ + LOG_INFO("host=%s port=%d sport=%d", host.c_str(), port, sport); + _during_host_switch = true; + do_disconnect(); + _client.set_target(host.c_str(), port, sport); + + if (!cert_subject.empty()) { + set_host_cert_subject(cert_subject.c_str(), "spicec"); + } + + _client.connect(); + } + int Application::run() { _client.connect(); @@ -701,6 +757,10 @@ void Application::unpress_all() void Application::on_connected() { + if (_during_host_switch) { + _during_host_switch = false; + deactivate_interval_timer(*_switch_host_timer); + } } void Application::on_disconnecting() diff --git a/client/application.h b/client/application.h index e924ce14..213308f3 100644 --- a/client/application.h +++ b/client/application.h @@ -72,6 +72,18 @@ private: std::vector _monitors; }; +class SwitchHostEvent: public Event { +public: + SwitchHostEvent(const char* host, int port, int sport, const char* cert_subject); + virtual void response(AbstractProcessLoop& events_loop); + +private: + std::string _host; + int _port; + int _sport; + std::string _cert_subject; +}; + struct KeyInfo { uint32_t _make; uint32_t _break; @@ -109,6 +121,11 @@ typedef struct StickyInfo { AutoRef timer; } StickyInfo; +class SwitchHostTimer: public Timer { +public: + virtual void response(AbstractProcessLoop& events_loop); +}; + class Application : public ProcessLoop, public Platform::EventListener, public Platform::DisplayModeListner, @@ -154,6 +171,8 @@ public: void show(); void external_show(); void connect(); + void switch_host(const std::string& host, int port, int sport, const std::string& cert_subject); + const PeerConnectionOptMap& get_con_opt_map() {return _peer_con_opt;} const RedPeer::HostAuthOptions& get_host_auth_opt() { return _host_auth_opt;} const std::string& get_connection_ciphers() { return _con_ciphers;} @@ -225,6 +244,7 @@ private: friend class MonitorsQuery; friend class AutoAbort; friend class StickyKeyTimer; + friend class SwitchHostTimer; private: RedClient _client; @@ -252,6 +272,8 @@ private: StickyInfo _sticky_info; std::vector _canvas_types; AutoRef _app_menu; + bool _during_host_switch; + AutoRef _switch_host_timer; }; #endif diff --git a/client/red_client.cpp b/client/red_client.cpp index 90793266..9fa2b1a3 100644 --- a/client/red_client.cpp +++ b/client/red_client.cpp @@ -288,6 +288,8 @@ RedClient::RedClient(Application& application) message_loop->set_handler(RED_MIGRATE_BEGIN, &RedClient::handle_migrate_begin, sizeof(RedMigrationBegin)); message_loop->set_handler(RED_MIGRATE_CANCEL, &RedClient::handle_migrate_cancel, 0); + message_loop->set_handler(RED_MIGRATE_SWITCH_HOST, &RedClient::handle_migrate_switch_host, + sizeof(RedMigrationSwitchHost)); message_loop->set_handler(RED_INIT, &RedClient::handle_init, sizeof(RedInit)); message_loop->set_handler(RED_CHANNELS_LIST, &RedClient::handle_channels, sizeof(RedChannels)); @@ -392,7 +394,8 @@ RedPeer::ConnectionOptions::Type RedClient::get_connection_options(uint32_t chan void RedClient::connect() { - //todo wait for disconnect state + //todo wait for disconnect state (but notifce that the main process loop + // must run when waiting for aborts) if (_connection_id || !abort_channels()) { return; } @@ -791,6 +794,21 @@ void RedClient::handle_agent_tokens(RedPeer::InMessage* message) _agent_tokens += token->num_tokens; } +void RedClient::handle_migrate_switch_host(RedPeer::InMessage* message) +{ + RedMigrationSwitchHost* migrate = (RedMigrationSwitchHost*)message->data(); + char* host = ((char*)migrate) + migrate->host_offset; + char* subject = NULL; + if (migrate->cert_subject_size) { + subject = ((char*)migrate)+ migrate->cert_subject_offset; + } + AutoRef switch_event(new SwitchHostEvent(host, + migrate->port, + migrate->sport, + subject)); + push_event(*switch_event); +} + void RedClient::migrate_channel(RedChannel& channel) { DBG(0, "channel type %u id %u", channel.get_type(), channel.get_id()); diff --git a/client/red_client.h b/client/red_client.h index b7edadbd..2937996b 100644 --- a/client/red_client.h +++ b/client/red_client.h @@ -187,6 +187,7 @@ private: void handle_agent_disconnected(RedPeer::InMessage* message); void handle_agent_data(RedPeer::InMessage* message); void handle_agent_tokens(RedPeer::InMessage* message); + void handle_migrate_switch_host(RedPeer::InMessage* message); void on_agent_reply(VDAgentReply* reply); diff --git a/common/red.h b/common/red.h index cead0667..44cef460 100644 --- a/common/red.h +++ b/common/red.h @@ -46,7 +46,7 @@ #define RED_MAGIC (*(uint32_t*)"REDQ") #define RED_VERSION_MAJOR 1 -#define RED_VERSION_MINOR 1 +#define RED_VERSION_MINOR 2 // Encryption & Ticketing Parameters #define RED_MAX_PASSWORD_LENGTH 60 @@ -166,6 +166,8 @@ enum { RED_AGENT_DATA, RED_AGENT_TOKEN, + RED_MIGRATE_SWITCH_HOST, + RED_MESSAGES_END, }; @@ -231,6 +233,15 @@ typedef struct ATTR_PACKED RedMigrationBegin { uint32_t pub_key_size; } RedMigrationBegin; +typedef struct ATTR_PACKED RedMigrationSwitchHost { + uint16_t port; + uint16_t sport; + uint32_t host_offset; + uint32_t host_size; + uint32_t cert_subject_offset; + uint32_t cert_subject_size; +} RedMigrationSwitchHost; + enum { RED_MIGRATE_NEED_FLUSH = (1 << 0), RED_MIGRATE_NEED_DATA_TRANSFER = (1 << 1), diff --git a/server/reds.c b/server/reds.c index a75aa555..45d899d8 100644 --- a/server/reds.c +++ b/server/reds.c @@ -238,6 +238,8 @@ typedef struct RedsStatValue { #endif +typedef struct SimpleOutItem SimpleOutItem; + typedef struct RedsState { int listen_socket; int secure_listen_socket; @@ -280,6 +282,8 @@ typedef struct RedsState { uint32_t net_test_id; int net_test_stage; int peer_minor_version; + + SimpleOutItem* mig_switch_host_item; } RedsState; uint64_t bitrate_per_sec = ~0; @@ -1032,11 +1036,11 @@ static int outgoing_write(RedsStreamContext *peer, OutgoingHandler *handler, voi return OUTGOING_OK; } -typedef struct SimpleOutItem { +struct SimpleOutItem { RedsOutItem base; RedDataHeader header; uint8_t data[0]; -} SimpleOutItem; +}; static void reds_prepare_basic_out_item(RedsOutItem *in_item, struct iovec* vec, int *len) { @@ -5692,3 +5696,151 @@ int spice_server_kbd_leds(SpiceServer *s, KeyboardInterface *kbd, int leds) reds_on_keyborad_leads_change(NULL, leds); return 0; } + +static void reds_free_mig_switch_host_item(RedsOutItem *item) +{ + if (!item) { + return; + } + + ASSERT((SimpleOutItem*)item == reds->mig_switch_host_item); + free(item); + reds->mig_switch_host_item = NULL; +} + +int spice_server_migrate_info(SpiceServer *s, const char* dest, int port, int secure_port, + const char* cert_subject) +{ + int host_len; + int subject_len; + RedMigrationSwitchHost *mig_msg; + + ASSERT(reds == s); + + if (reds->mig_switch_host_item) { + reds_free_mig_switch_host_item(&reds->mig_switch_host_item->base); + } + + if ((port == -1 && secure_port == -1) || !dest) { + red_printf("invalid args port %d secure-port %d host %s", + port, + secure_port, + dest ? dest : "NULL"); + return -1; + } + + host_len = strlen(dest) + 1; + subject_len = cert_subject ? strlen(cert_subject) + 1 : 0; + reds->mig_switch_host_item = new_simple_out_item(RED_MIGRATE_SWITCH_HOST, + sizeof(RedMigrationSwitchHost) + + host_len + subject_len); + if (!(reds->mig_switch_host_item)) { + red_printf("alloc item failed"); + return -1; + } + reds->mig_switch_host_item->base.release = reds_free_mig_switch_host_item; + mig_msg = (RedMigrationSwitchHost*)reds->mig_switch_host_item->data; + mig_msg->port = port; + mig_msg->sport = secure_port; + mig_msg->host_offset = sizeof(RedMigrationSwitchHost); + mig_msg->host_size = host_len; + mig_msg->cert_subject_offset = sizeof(RedMigrationSwitchHost) + host_len; + mig_msg->cert_subject_size = subject_len; + memcpy((uint8_t*)(mig_msg) + mig_msg->host_offset, dest, host_len); + memcpy((uint8_t*)(mig_msg) + mig_msg->cert_subject_offset, cert_subject, subject_len); + + return 0; +} + +int spice_server_migrate_start(SpiceServer *s) +{ + ASSERT(reds == s); + red_printf(""); + + if (reds->listen_socket != -1) { + core->set_file_handlers(core, reds->listen_socket, NULL, NULL, NULL); + } + + if (reds->secure_listen_socket != -1) { + core->set_file_handlers(core, reds->secure_listen_socket, NULL, NULL, NULL); + } + + if (reds->peer == NULL) { + red_printf("not connected to peer"); + return 0; + } + + return 0; +} + +static inline uint64_t get_now() +{ + struct timespec time; + + clock_gettime(CLOCK_MONOTONIC, &time); + + return time.tv_sec * 1000000 + (time.tv_nsec / 1000); +} + +int spice_server_migrate_end(SpiceServer *s, int completed) +{ + ASSERT(reds == s); + red_printf("status %s", completed ? "success" : "failure"); + + if (reds->listen_socket != -1) { + core->set_file_handlers(core, reds->listen_socket, reds_accept, NULL, NULL); + } + + if (reds->secure_listen_socket != -1) { + core->set_file_handlers(core, reds->secure_listen_socket, reds_accept_ssl_connection, + NULL, NULL); + } + + if (reds->peer == NULL) { + red_printf("no peer connected"); + if (reds->mig_switch_host_item) { + reds_free_mig_switch_host_item(&reds->mig_switch_host_item->base); + } + return 0; + } + + if (completed) { + uint64_t end_time; + + if ((RED_VERSION_MAJOR == 1) && (reds->peer_minor_version < 2)) { + red_printf("minor version mismatch client %u server %u", + reds->peer_minor_version, RED_VERSION_MINOR); + reds_disconnect(); + return 0; + } + + if (!reds->mig_switch_host_item) { + red_printf("missing pre-migrate information"); + reds_disconnect(); + return -1; + } + reds_push_pipe_item(&reds->mig_switch_host_item->base); + end_time = get_now() + MIGRATE_TIMEOUT * 1000; + + // waiting for the client to receive the message and diconnect + while (reds->peer) { + usleep(10000); + if (get_now() > end_time) { + red_printf("timeout"); + break; + } + reds_main_read(NULL); + reds_push(); + } + if (!reds->peer) { + red_printf("client disconnected"); + } + reds_disconnect(); + return 0; + } else { + if (reds->mig_switch_host_item) { + reds_free_mig_switch_host_item(&reds->mig_switch_host_item->base); + } + return 0; + } +} diff --git a/server/spice.h b/server/spice.h index 88cea70b..05c63611 100644 --- a/server/spice.h +++ b/server/spice.h @@ -91,4 +91,15 @@ int spice_server_add_renderer(SpiceServer *s, const char *name); int spice_server_get_sock_info(SpiceServer *s, struct sockaddr *sa, socklen_t *salen); int spice_server_get_peer_info(SpiceServer *s, struct sockaddr *sa, socklen_t *salen); +/* + * setting information about the migration destination. + * For null port use -1. + * cert_subject format: pairs of = separated with commas. + * Commas and backslashes within must be preceded by a backslash. + */ +int spice_server_migrate_info(SpiceServer *s, const char* dest, int port, int secure_port, + const char* cert_subject); +int spice_server_migrate_start(SpiceServer *s); +int spice_server_migrate_end(SpiceServer *s, int completed); + #endif -- cgit