spice-core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 or
  7. * (at your option) version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <spice.h>
  18. #include <spice-experimental.h>
  19. #include <netdb.h>
  20. #include "sysemu.h"
  21. #include "qemu-common.h"
  22. #include "qemu-spice.h"
  23. #include "qemu-thread.h"
  24. #include "qemu-timer.h"
  25. #include "qemu-queue.h"
  26. #include "qemu-x509.h"
  27. #include "qemu_socket.h"
  28. #include "qmp-commands.h"
  29. #include "qint.h"
  30. #include "qbool.h"
  31. #include "qstring.h"
  32. #include "qjson.h"
  33. #include "notify.h"
  34. #include "migration.h"
  35. #include "monitor.h"
  36. #include "hw/hw.h"
  37. #include "spice-display.h"
  38. /* core bits */
  39. static SpiceServer *spice_server;
  40. static Notifier migration_state;
  41. static const char *auth = "spice";
  42. static char *auth_passwd;
  43. static time_t auth_expires = TIME_MAX;
  44. static int spice_migration_completed;
  45. int using_spice = 0;
  46. static QemuThread me;
  47. struct SpiceTimer {
  48. QEMUTimer *timer;
  49. QTAILQ_ENTRY(SpiceTimer) next;
  50. };
  51. static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
  52. static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
  53. {
  54. SpiceTimer *timer;
  55. timer = g_malloc0(sizeof(*timer));
  56. timer->timer = qemu_new_timer_ms(rt_clock, func, opaque);
  57. QTAILQ_INSERT_TAIL(&timers, timer, next);
  58. return timer;
  59. }
  60. static void timer_start(SpiceTimer *timer, uint32_t ms)
  61. {
  62. qemu_mod_timer(timer->timer, qemu_get_clock_ms(rt_clock) + ms);
  63. }
  64. static void timer_cancel(SpiceTimer *timer)
  65. {
  66. qemu_del_timer(timer->timer);
  67. }
  68. static void timer_remove(SpiceTimer *timer)
  69. {
  70. qemu_del_timer(timer->timer);
  71. qemu_free_timer(timer->timer);
  72. QTAILQ_REMOVE(&timers, timer, next);
  73. g_free(timer);
  74. }
  75. struct SpiceWatch {
  76. int fd;
  77. int event_mask;
  78. SpiceWatchFunc func;
  79. void *opaque;
  80. QTAILQ_ENTRY(SpiceWatch) next;
  81. };
  82. static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
  83. static void watch_read(void *opaque)
  84. {
  85. SpiceWatch *watch = opaque;
  86. watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
  87. }
  88. static void watch_write(void *opaque)
  89. {
  90. SpiceWatch *watch = opaque;
  91. watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
  92. }
  93. static void watch_update_mask(SpiceWatch *watch, int event_mask)
  94. {
  95. IOHandler *on_read = NULL;
  96. IOHandler *on_write = NULL;
  97. watch->event_mask = event_mask;
  98. if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
  99. on_read = watch_read;
  100. }
  101. if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
  102. on_write = watch_write;
  103. }
  104. qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
  105. }
  106. static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
  107. {
  108. SpiceWatch *watch;
  109. watch = g_malloc0(sizeof(*watch));
  110. watch->fd = fd;
  111. watch->func = func;
  112. watch->opaque = opaque;
  113. QTAILQ_INSERT_TAIL(&watches, watch, next);
  114. watch_update_mask(watch, event_mask);
  115. return watch;
  116. }
  117. static void watch_remove(SpiceWatch *watch)
  118. {
  119. qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
  120. QTAILQ_REMOVE(&watches, watch, next);
  121. g_free(watch);
  122. }
  123. typedef struct ChannelList ChannelList;
  124. struct ChannelList {
  125. SpiceChannelEventInfo *info;
  126. QTAILQ_ENTRY(ChannelList) link;
  127. };
  128. static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
  129. static void channel_list_add(SpiceChannelEventInfo *info)
  130. {
  131. ChannelList *item;
  132. item = g_malloc0(sizeof(*item));
  133. item->info = info;
  134. QTAILQ_INSERT_TAIL(&channel_list, item, link);
  135. }
  136. static void channel_list_del(SpiceChannelEventInfo *info)
  137. {
  138. ChannelList *item;
  139. QTAILQ_FOREACH(item, &channel_list, link) {
  140. if (item->info != info) {
  141. continue;
  142. }
  143. QTAILQ_REMOVE(&channel_list, item, link);
  144. g_free(item);
  145. return;
  146. }
  147. }
  148. static void add_addr_info(QDict *dict, struct sockaddr *addr, int len)
  149. {
  150. char host[NI_MAXHOST], port[NI_MAXSERV];
  151. const char *family;
  152. getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
  153. NI_NUMERICHOST | NI_NUMERICSERV);
  154. family = inet_strfamily(addr->sa_family);
  155. qdict_put(dict, "host", qstring_from_str(host));
  156. qdict_put(dict, "port", qstring_from_str(port));
  157. qdict_put(dict, "family", qstring_from_str(family));
  158. }
  159. static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info)
  160. {
  161. int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
  162. qdict_put(dict, "connection-id", qint_from_int(info->connection_id));
  163. qdict_put(dict, "channel-type", qint_from_int(info->type));
  164. qdict_put(dict, "channel-id", qint_from_int(info->id));
  165. qdict_put(dict, "tls", qbool_from_int(tls));
  166. }
  167. static void channel_event(int event, SpiceChannelEventInfo *info)
  168. {
  169. static const int qevent[] = {
  170. [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED,
  171. [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED,
  172. [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED,
  173. };
  174. QDict *server, *client;
  175. QObject *data;
  176. /*
  177. * Spice server might have called us from spice worker thread
  178. * context (happens on display channel disconnects). Spice should
  179. * not do that. It isn't that easy to fix it in spice and even
  180. * when it is fixed we still should cover the already released
  181. * spice versions. So detect that we've been called from another
  182. * thread and grab the iothread lock if so before calling qemu
  183. * functions.
  184. */
  185. bool need_lock = !qemu_thread_is_self(&me);
  186. if (need_lock) {
  187. qemu_mutex_lock_iothread();
  188. }
  189. client = qdict_new();
  190. server = qdict_new();
  191. #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT
  192. if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
  193. add_addr_info(client, (struct sockaddr *)&info->paddr_ext,
  194. info->plen_ext);
  195. add_addr_info(server, (struct sockaddr *)&info->laddr_ext,
  196. info->llen_ext);
  197. } else {
  198. error_report("spice: %s, extended address is expected",
  199. __func__);
  200. #endif
  201. add_addr_info(client, &info->paddr, info->plen);
  202. add_addr_info(server, &info->laddr, info->llen);
  203. #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT
  204. }
  205. #endif
  206. if (event == SPICE_CHANNEL_EVENT_INITIALIZED) {
  207. qdict_put(server, "auth", qstring_from_str(auth));
  208. add_channel_info(client, info);
  209. channel_list_add(info);
  210. }
  211. if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
  212. channel_list_del(info);
  213. }
  214. data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
  215. QOBJECT(client), QOBJECT(server));
  216. monitor_protocol_event(qevent[event], data);
  217. qobject_decref(data);
  218. if (need_lock) {
  219. qemu_mutex_unlock_iothread();
  220. }
  221. }
  222. static SpiceCoreInterface core_interface = {
  223. .base.type = SPICE_INTERFACE_CORE,
  224. .base.description = "qemu core services",
  225. .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
  226. .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
  227. .timer_add = timer_add,
  228. .timer_start = timer_start,
  229. .timer_cancel = timer_cancel,
  230. .timer_remove = timer_remove,
  231. .watch_add = watch_add,
  232. .watch_update_mask = watch_update_mask,
  233. .watch_remove = watch_remove,
  234. .channel_event = channel_event,
  235. };
  236. #ifdef SPICE_INTERFACE_MIGRATION
  237. typedef struct SpiceMigration {
  238. SpiceMigrateInstance sin;
  239. struct {
  240. MonitorCompletion *cb;
  241. void *opaque;
  242. } connect_complete;
  243. } SpiceMigration;
  244. static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
  245. static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
  246. static const SpiceMigrateInterface migrate_interface = {
  247. .base.type = SPICE_INTERFACE_MIGRATION,
  248. .base.description = "migration",
  249. .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
  250. .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
  251. .migrate_connect_complete = migrate_connect_complete_cb,
  252. .migrate_end_complete = migrate_end_complete_cb,
  253. };
  254. static SpiceMigration spice_migrate;
  255. static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
  256. {
  257. SpiceMigration *sm = container_of(sin, SpiceMigration, sin);
  258. if (sm->connect_complete.cb) {
  259. sm->connect_complete.cb(sm->connect_complete.opaque, NULL);
  260. }
  261. sm->connect_complete.cb = NULL;
  262. }
  263. static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
  264. {
  265. monitor_protocol_event(QEVENT_SPICE_MIGRATE_COMPLETED, NULL);
  266. spice_migration_completed = true;
  267. }
  268. #endif
  269. /* config string parsing */
  270. static int name2enum(const char *string, const char *table[], int entries)
  271. {
  272. int i;
  273. if (string) {
  274. for (i = 0; i < entries; i++) {
  275. if (!table[i]) {
  276. continue;
  277. }
  278. if (strcmp(string, table[i]) != 0) {
  279. continue;
  280. }
  281. return i;
  282. }
  283. }
  284. return -1;
  285. }
  286. static int parse_name(const char *string, const char *optname,
  287. const char *table[], int entries)
  288. {
  289. int value = name2enum(string, table, entries);
  290. if (value != -1) {
  291. return value;
  292. }
  293. error_report("spice: invalid %s: %s", optname, string);
  294. exit(1);
  295. }
  296. static const char *stream_video_names[] = {
  297. [ SPICE_STREAM_VIDEO_OFF ] = "off",
  298. [ SPICE_STREAM_VIDEO_ALL ] = "all",
  299. [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
  300. };
  301. #define parse_stream_video(_name) \
  302. parse_name(_name, "stream video control", \
  303. stream_video_names, ARRAY_SIZE(stream_video_names))
  304. static const char *compression_names[] = {
  305. [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
  306. [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
  307. [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
  308. [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
  309. [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
  310. [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
  311. };
  312. #define parse_compression(_name) \
  313. parse_name(_name, "image compression", \
  314. compression_names, ARRAY_SIZE(compression_names))
  315. static const char *wan_compression_names[] = {
  316. [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
  317. [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
  318. [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
  319. };
  320. #define parse_wan_compression(_name) \
  321. parse_name(_name, "wan compression", \
  322. wan_compression_names, ARRAY_SIZE(wan_compression_names))
  323. /* functions for the rest of qemu */
  324. static SpiceChannelList *qmp_query_spice_channels(void)
  325. {
  326. SpiceChannelList *cur_item = NULL, *head = NULL;
  327. ChannelList *item;
  328. QTAILQ_FOREACH(item, &channel_list, link) {
  329. SpiceChannelList *chan;
  330. char host[NI_MAXHOST], port[NI_MAXSERV];
  331. struct sockaddr *paddr;
  332. socklen_t plen;
  333. chan = g_malloc0(sizeof(*chan));
  334. chan->value = g_malloc0(sizeof(*chan->value));
  335. #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT
  336. if (item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
  337. paddr = (struct sockaddr *)&item->info->paddr_ext;
  338. plen = item->info->plen_ext;
  339. } else {
  340. #endif
  341. paddr = &item->info->paddr;
  342. plen = item->info->plen;
  343. #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT
  344. }
  345. #endif
  346. getnameinfo(paddr, plen,
  347. host, sizeof(host), port, sizeof(port),
  348. NI_NUMERICHOST | NI_NUMERICSERV);
  349. chan->value->host = g_strdup(host);
  350. chan->value->port = g_strdup(port);
  351. chan->value->family = g_strdup(inet_strfamily(paddr->sa_family));
  352. chan->value->connection_id = item->info->connection_id;
  353. chan->value->channel_type = item->info->type;
  354. chan->value->channel_id = item->info->id;
  355. chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
  356. /* XXX: waiting for the qapi to support GSList */
  357. if (!cur_item) {
  358. head = cur_item = chan;
  359. } else {
  360. cur_item->next = chan;
  361. cur_item = chan;
  362. }
  363. }
  364. return head;
  365. }
  366. SpiceInfo *qmp_query_spice(Error **errp)
  367. {
  368. QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
  369. int port, tls_port;
  370. const char *addr;
  371. SpiceInfo *info;
  372. char version_string[20]; /* 12 = |255.255.255\0| is the max */
  373. info = g_malloc0(sizeof(*info));
  374. if (!spice_server || !opts) {
  375. info->enabled = false;
  376. return info;
  377. }
  378. info->enabled = true;
  379. info->migrated = spice_migration_completed;
  380. addr = qemu_opt_get(opts, "addr");
  381. port = qemu_opt_get_number(opts, "port", 0);
  382. tls_port = qemu_opt_get_number(opts, "tls-port", 0);
  383. info->has_auth = true;
  384. info->auth = g_strdup(auth);
  385. info->has_host = true;
  386. info->host = g_strdup(addr ? addr : "0.0.0.0");
  387. info->has_compiled_version = true;
  388. snprintf(version_string, sizeof(version_string), "%d.%d.%d",
  389. (SPICE_SERVER_VERSION & 0xff0000) >> 16,
  390. (SPICE_SERVER_VERSION & 0xff00) >> 8,
  391. SPICE_SERVER_VERSION & 0xff);
  392. info->compiled_version = g_strdup(version_string);
  393. if (port) {
  394. info->has_port = true;
  395. info->port = port;
  396. }
  397. if (tls_port) {
  398. info->has_tls_port = true;
  399. info->tls_port = tls_port;
  400. }
  401. #if SPICE_SERVER_VERSION >= 0x000a03 /* 0.10.3 */
  402. info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
  403. SPICE_QUERY_MOUSE_MODE_SERVER :
  404. SPICE_QUERY_MOUSE_MODE_CLIENT;
  405. #else
  406. info->mouse_mode = SPICE_QUERY_MOUSE_MODE_UNKNOWN;
  407. #endif
  408. /* for compatibility with the original command */
  409. info->has_channels = true;
  410. info->channels = qmp_query_spice_channels();
  411. return info;
  412. }
  413. static void migration_state_notifier(Notifier *notifier, void *data)
  414. {
  415. MigrationState *s = data;
  416. if (migration_is_active(s)) {
  417. #ifdef SPICE_INTERFACE_MIGRATION
  418. spice_server_migrate_start(spice_server);
  419. #endif
  420. } else if (migration_has_finished(s)) {
  421. #ifndef SPICE_INTERFACE_MIGRATION
  422. spice_server_migrate_switch(spice_server);
  423. monitor_protocol_event(QEVENT_SPICE_MIGRATE_COMPLETED, NULL);
  424. spice_migration_completed = true;
  425. #else
  426. spice_server_migrate_end(spice_server, true);
  427. } else if (migration_has_failed(s)) {
  428. spice_server_migrate_end(spice_server, false);
  429. #endif
  430. }
  431. }
  432. int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
  433. const char *subject,
  434. MonitorCompletion *cb, void *opaque)
  435. {
  436. int ret;
  437. #ifdef SPICE_INTERFACE_MIGRATION
  438. spice_migrate.connect_complete.cb = cb;
  439. spice_migrate.connect_complete.opaque = opaque;
  440. ret = spice_server_migrate_connect(spice_server, hostname,
  441. port, tls_port, subject);
  442. #else
  443. ret = spice_server_migrate_info(spice_server, hostname,
  444. port, tls_port, subject);
  445. cb(opaque, NULL);
  446. #endif
  447. return ret;
  448. }
  449. static int add_channel(const char *name, const char *value, void *opaque)
  450. {
  451. int security = 0;
  452. int rc;
  453. if (strcmp(name, "tls-channel") == 0) {
  454. int *tls_port = opaque;
  455. if (!*tls_port) {
  456. error_report("spice: tried to setup tls-channel"
  457. " without specifying a TLS port");
  458. exit(1);
  459. }
  460. security = SPICE_CHANNEL_SECURITY_SSL;
  461. }
  462. if (strcmp(name, "plaintext-channel") == 0) {
  463. security = SPICE_CHANNEL_SECURITY_NONE;
  464. }
  465. if (security == 0) {
  466. return 0;
  467. }
  468. if (strcmp(value, "default") == 0) {
  469. rc = spice_server_set_channel_security(spice_server, NULL, security);
  470. } else {
  471. rc = spice_server_set_channel_security(spice_server, value, security);
  472. }
  473. if (rc != 0) {
  474. error_report("spice: failed to set channel security for %s", value);
  475. exit(1);
  476. }
  477. return 0;
  478. }
  479. static void vm_change_state_handler(void *opaque, int running,
  480. RunState state)
  481. {
  482. #if SPICE_SERVER_VERSION >= 0x000b02 /* 0.11.2 */
  483. if (running) {
  484. qemu_spice_display_start();
  485. spice_server_vm_start(spice_server);
  486. } else {
  487. spice_server_vm_stop(spice_server);
  488. qemu_spice_display_stop();
  489. }
  490. #endif
  491. }
  492. void qemu_spice_init(void)
  493. {
  494. QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
  495. const char *password, *str, *x509_dir, *addr,
  496. *x509_key_password = NULL,
  497. *x509_dh_file = NULL,
  498. *tls_ciphers = NULL;
  499. char *x509_key_file = NULL,
  500. *x509_cert_file = NULL,
  501. *x509_cacert_file = NULL;
  502. int port, tls_port, len, addr_flags;
  503. spice_image_compression_t compression;
  504. spice_wan_compression_t wan_compr;
  505. #if SPICE_SERVER_VERSION >= 0x000b02 /* 0.11.2 */
  506. bool seamless_migration;
  507. #endif
  508. qemu_thread_get_self(&me);
  509. if (!opts) {
  510. return;
  511. }
  512. port = qemu_opt_get_number(opts, "port", 0);
  513. tls_port = qemu_opt_get_number(opts, "tls-port", 0);
  514. if (!port && !tls_port) {
  515. error_report("neither port nor tls-port specified for spice");
  516. exit(1);
  517. }
  518. if (port < 0 || port > 65535) {
  519. error_report("spice port is out of range");
  520. exit(1);
  521. }
  522. if (tls_port < 0 || tls_port > 65535) {
  523. error_report("spice tls-port is out of range");
  524. exit(1);
  525. }
  526. password = qemu_opt_get(opts, "password");
  527. if (tls_port) {
  528. x509_dir = qemu_opt_get(opts, "x509-dir");
  529. if (NULL == x509_dir) {
  530. x509_dir = ".";
  531. }
  532. len = strlen(x509_dir) + 32;
  533. str = qemu_opt_get(opts, "x509-key-file");
  534. if (str) {
  535. x509_key_file = g_strdup(str);
  536. } else {
  537. x509_key_file = g_malloc(len);
  538. snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE);
  539. }
  540. str = qemu_opt_get(opts, "x509-cert-file");
  541. if (str) {
  542. x509_cert_file = g_strdup(str);
  543. } else {
  544. x509_cert_file = g_malloc(len);
  545. snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE);
  546. }
  547. str = qemu_opt_get(opts, "x509-cacert-file");
  548. if (str) {
  549. x509_cacert_file = g_strdup(str);
  550. } else {
  551. x509_cacert_file = g_malloc(len);
  552. snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE);
  553. }
  554. x509_key_password = qemu_opt_get(opts, "x509-key-password");
  555. x509_dh_file = qemu_opt_get(opts, "x509-dh-file");
  556. tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
  557. }
  558. addr = qemu_opt_get(opts, "addr");
  559. addr_flags = 0;
  560. if (qemu_opt_get_bool(opts, "ipv4", 0)) {
  561. addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
  562. } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
  563. addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
  564. }
  565. spice_server = spice_server_new();
  566. spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
  567. if (port) {
  568. spice_server_set_port(spice_server, port);
  569. }
  570. if (tls_port) {
  571. spice_server_set_tls(spice_server, tls_port,
  572. x509_cacert_file,
  573. x509_cert_file,
  574. x509_key_file,
  575. x509_key_password,
  576. x509_dh_file,
  577. tls_ciphers);
  578. }
  579. if (password) {
  580. spice_server_set_ticket(spice_server, password, 0, 0, 0);
  581. }
  582. if (qemu_opt_get_bool(opts, "sasl", 0)) {
  583. #if SPICE_SERVER_VERSION >= 0x000900 /* 0.9.0 */
  584. if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
  585. spice_server_set_sasl(spice_server, 1) == -1) {
  586. error_report("spice: failed to enable sasl");
  587. exit(1);
  588. }
  589. #else
  590. error_report("spice: sasl is not available (spice >= 0.9 required)");
  591. exit(1);
  592. #endif
  593. }
  594. if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
  595. auth = "none";
  596. spice_server_set_noauth(spice_server);
  597. }
  598. if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
  599. spice_server_set_agent_copypaste(spice_server, false);
  600. }
  601. compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
  602. str = qemu_opt_get(opts, "image-compression");
  603. if (str) {
  604. compression = parse_compression(str);
  605. }
  606. spice_server_set_image_compression(spice_server, compression);
  607. wan_compr = SPICE_WAN_COMPRESSION_AUTO;
  608. str = qemu_opt_get(opts, "jpeg-wan-compression");
  609. if (str) {
  610. wan_compr = parse_wan_compression(str);
  611. }
  612. spice_server_set_jpeg_compression(spice_server, wan_compr);
  613. wan_compr = SPICE_WAN_COMPRESSION_AUTO;
  614. str = qemu_opt_get(opts, "zlib-glz-wan-compression");
  615. if (str) {
  616. wan_compr = parse_wan_compression(str);
  617. }
  618. spice_server_set_zlib_glz_compression(spice_server, wan_compr);
  619. str = qemu_opt_get(opts, "streaming-video");
  620. if (str) {
  621. int streaming_video = parse_stream_video(str);
  622. spice_server_set_streaming_video(spice_server, streaming_video);
  623. }
  624. spice_server_set_agent_mouse
  625. (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
  626. spice_server_set_playback_compression
  627. (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
  628. qemu_opt_foreach(opts, add_channel, &tls_port, 0);
  629. #if SPICE_SERVER_VERSION >= 0x000a02 /* 0.10.2 */
  630. spice_server_set_name(spice_server, qemu_name);
  631. spice_server_set_uuid(spice_server, qemu_uuid);
  632. #endif
  633. #if SPICE_SERVER_VERSION >= 0x000b02 /* 0.11.2 */
  634. seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
  635. spice_server_set_seamless_migration(spice_server, seamless_migration);
  636. #endif
  637. if (0 != spice_server_init(spice_server, &core_interface)) {
  638. error_report("failed to initialize spice server");
  639. exit(1);
  640. };
  641. using_spice = 1;
  642. migration_state.notify = migration_state_notifier;
  643. add_migration_state_change_notifier(&migration_state);
  644. #ifdef SPICE_INTERFACE_MIGRATION
  645. spice_migrate.sin.base.sif = &migrate_interface.base;
  646. spice_migrate.connect_complete.cb = NULL;
  647. qemu_spice_add_interface(&spice_migrate.sin.base);
  648. #endif
  649. qemu_spice_input_init();
  650. qemu_spice_audio_init();
  651. qemu_add_vm_change_state_handler(vm_change_state_handler, &spice_server);
  652. g_free(x509_key_file);
  653. g_free(x509_cert_file);
  654. g_free(x509_cacert_file);
  655. }
  656. int qemu_spice_add_interface(SpiceBaseInstance *sin)
  657. {
  658. if (!spice_server) {
  659. if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
  660. error_report("Oops: spice configured but not active");
  661. exit(1);
  662. }
  663. /*
  664. * Create a spice server instance.
  665. * It does *not* listen on the network.
  666. * It handles QXL local rendering only.
  667. *
  668. * With a command line like '-vnc :0 -vga qxl' you'll end up here.
  669. */
  670. spice_server = spice_server_new();
  671. spice_server_init(spice_server, &core_interface);
  672. }
  673. return spice_server_add_interface(spice_server, sin);
  674. }
  675. static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
  676. {
  677. time_t lifetime, now = time(NULL);
  678. char *passwd;
  679. if (now < auth_expires) {
  680. passwd = auth_passwd;
  681. lifetime = (auth_expires - now);
  682. if (lifetime > INT_MAX) {
  683. lifetime = INT_MAX;
  684. }
  685. } else {
  686. passwd = NULL;
  687. lifetime = 1;
  688. }
  689. return spice_server_set_ticket(spice_server, passwd, lifetime,
  690. fail_if_conn, disconnect_if_conn);
  691. }
  692. int qemu_spice_set_passwd(const char *passwd,
  693. bool fail_if_conn, bool disconnect_if_conn)
  694. {
  695. free(auth_passwd);
  696. auth_passwd = strdup(passwd);
  697. return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
  698. }
  699. int qemu_spice_set_pw_expire(time_t expires)
  700. {
  701. auth_expires = expires;
  702. return qemu_spice_set_ticket(false, false);
  703. }
  704. int qemu_spice_display_add_client(int csock, int skipauth, int tls)
  705. {
  706. #if SPICE_SERVER_VERSION >= 0x000a01
  707. if (tls) {
  708. return spice_server_add_ssl_client(spice_server, csock, skipauth);
  709. } else {
  710. return spice_server_add_client(spice_server, csock, skipauth);
  711. }
  712. #else
  713. return -1;
  714. #endif
  715. }
  716. static void spice_register_config(void)
  717. {
  718. qemu_add_opts(&qemu_spice_opts);
  719. }
  720. machine_init(spice_register_config);