spice-core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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/sysemu.h"
  21. #include "qemu-common.h"
  22. #include "ui/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/sockets.h"
  28. #include "qmp-commands.h"
  29. #include "qapi/qmp/qint.h"
  30. #include "qapi/qmp/qbool.h"
  31. #include "qapi/qmp/qstring.h"
  32. #include "qapi/qmp/qjson.h"
  33. #include "qemu/notify.h"
  34. #include "migration/migration.h"
  35. #include "hw/hw.h"
  36. #include "ui/spice-display.h"
  37. #include "qapi-event.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. static int spice_display_is_running;
  46. static int spice_have_target_host;
  47. int using_spice = 0;
  48. static QemuThread me;
  49. struct SpiceTimer {
  50. QEMUTimer *timer;
  51. QTAILQ_ENTRY(SpiceTimer) next;
  52. };
  53. static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
  54. static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
  55. {
  56. SpiceTimer *timer;
  57. timer = g_malloc0(sizeof(*timer));
  58. timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque);
  59. QTAILQ_INSERT_TAIL(&timers, timer, next);
  60. return timer;
  61. }
  62. static void timer_start(SpiceTimer *timer, uint32_t ms)
  63. {
  64. timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms);
  65. }
  66. static void timer_cancel(SpiceTimer *timer)
  67. {
  68. timer_del(timer->timer);
  69. }
  70. static void timer_remove(SpiceTimer *timer)
  71. {
  72. timer_del(timer->timer);
  73. timer_free(timer->timer);
  74. QTAILQ_REMOVE(&timers, timer, next);
  75. g_free(timer);
  76. }
  77. struct SpiceWatch {
  78. int fd;
  79. int event_mask;
  80. SpiceWatchFunc func;
  81. void *opaque;
  82. QTAILQ_ENTRY(SpiceWatch) next;
  83. };
  84. static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
  85. static void watch_read(void *opaque)
  86. {
  87. SpiceWatch *watch = opaque;
  88. watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
  89. }
  90. static void watch_write(void *opaque)
  91. {
  92. SpiceWatch *watch = opaque;
  93. watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
  94. }
  95. static void watch_update_mask(SpiceWatch *watch, int event_mask)
  96. {
  97. IOHandler *on_read = NULL;
  98. IOHandler *on_write = NULL;
  99. watch->event_mask = event_mask;
  100. if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
  101. on_read = watch_read;
  102. }
  103. if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
  104. on_write = watch_write;
  105. }
  106. qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
  107. }
  108. static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
  109. {
  110. SpiceWatch *watch;
  111. watch = g_malloc0(sizeof(*watch));
  112. watch->fd = fd;
  113. watch->func = func;
  114. watch->opaque = opaque;
  115. QTAILQ_INSERT_TAIL(&watches, watch, next);
  116. watch_update_mask(watch, event_mask);
  117. return watch;
  118. }
  119. static void watch_remove(SpiceWatch *watch)
  120. {
  121. qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
  122. QTAILQ_REMOVE(&watches, watch, next);
  123. g_free(watch);
  124. }
  125. typedef struct ChannelList ChannelList;
  126. struct ChannelList {
  127. SpiceChannelEventInfo *info;
  128. QTAILQ_ENTRY(ChannelList) link;
  129. };
  130. static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
  131. static void channel_list_add(SpiceChannelEventInfo *info)
  132. {
  133. ChannelList *item;
  134. item = g_malloc0(sizeof(*item));
  135. item->info = info;
  136. QTAILQ_INSERT_TAIL(&channel_list, item, link);
  137. }
  138. static void channel_list_del(SpiceChannelEventInfo *info)
  139. {
  140. ChannelList *item;
  141. QTAILQ_FOREACH(item, &channel_list, link) {
  142. if (item->info != info) {
  143. continue;
  144. }
  145. QTAILQ_REMOVE(&channel_list, item, link);
  146. g_free(item);
  147. return;
  148. }
  149. }
  150. static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
  151. {
  152. char host[NI_MAXHOST], port[NI_MAXSERV];
  153. getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
  154. NI_NUMERICHOST | NI_NUMERICSERV);
  155. info->host = g_strdup(host);
  156. info->port = g_strdup(port);
  157. info->family = inet_netfamily(addr->sa_family);
  158. }
  159. static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
  160. {
  161. int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
  162. sc->connection_id = info->connection_id;
  163. sc->channel_type = info->type;
  164. sc->channel_id = info->id;
  165. sc->tls = !!tls;
  166. }
  167. static void channel_event(int event, SpiceChannelEventInfo *info)
  168. {
  169. SpiceServerInfo *server = g_malloc0(sizeof(*server));
  170. SpiceChannel *client = g_malloc0(sizeof(*client));
  171. server->base = g_malloc0(sizeof(*server->base));
  172. client->base = g_malloc0(sizeof(*client->base));
  173. /*
  174. * Spice server might have called us from spice worker thread
  175. * context (happens on display channel disconnects). Spice should
  176. * not do that. It isn't that easy to fix it in spice and even
  177. * when it is fixed we still should cover the already released
  178. * spice versions. So detect that we've been called from another
  179. * thread and grab the iothread lock if so before calling qemu
  180. * functions.
  181. */
  182. bool need_lock = !qemu_thread_is_self(&me);
  183. if (need_lock) {
  184. qemu_mutex_lock_iothread();
  185. }
  186. if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
  187. add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext,
  188. info->plen_ext);
  189. add_addr_info(server->base, (struct sockaddr *)&info->laddr_ext,
  190. info->llen_ext);
  191. } else {
  192. error_report("spice: %s, extended address is expected",
  193. __func__);
  194. }
  195. switch (event) {
  196. case SPICE_CHANNEL_EVENT_CONNECTED:
  197. qapi_event_send_spice_connected(server->base, client->base, &error_abort);
  198. break;
  199. case SPICE_CHANNEL_EVENT_INITIALIZED:
  200. if (auth) {
  201. server->has_auth = true;
  202. server->auth = g_strdup(auth);
  203. }
  204. add_channel_info(client, info);
  205. channel_list_add(info);
  206. qapi_event_send_spice_initialized(server, client, &error_abort);
  207. break;
  208. case SPICE_CHANNEL_EVENT_DISCONNECTED:
  209. channel_list_del(info);
  210. qapi_event_send_spice_disconnected(server->base, client->base, &error_abort);
  211. break;
  212. default:
  213. break;
  214. }
  215. if (need_lock) {
  216. qemu_mutex_unlock_iothread();
  217. }
  218. qapi_free_SpiceServerInfo(server);
  219. qapi_free_SpiceChannel(client);
  220. }
  221. static SpiceCoreInterface core_interface = {
  222. .base.type = SPICE_INTERFACE_CORE,
  223. .base.description = "qemu core services",
  224. .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
  225. .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
  226. .timer_add = timer_add,
  227. .timer_start = timer_start,
  228. .timer_cancel = timer_cancel,
  229. .timer_remove = timer_remove,
  230. .watch_add = watch_add,
  231. .watch_update_mask = watch_update_mask,
  232. .watch_remove = watch_remove,
  233. .channel_event = channel_event,
  234. };
  235. typedef struct SpiceMigration {
  236. SpiceMigrateInstance sin;
  237. struct {
  238. MonitorCompletion *cb;
  239. void *opaque;
  240. } connect_complete;
  241. } SpiceMigration;
  242. static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
  243. static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
  244. static const SpiceMigrateInterface migrate_interface = {
  245. .base.type = SPICE_INTERFACE_MIGRATION,
  246. .base.description = "migration",
  247. .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
  248. .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
  249. .migrate_connect_complete = migrate_connect_complete_cb,
  250. .migrate_end_complete = migrate_end_complete_cb,
  251. };
  252. static SpiceMigration spice_migrate;
  253. static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
  254. {
  255. SpiceMigration *sm = container_of(sin, SpiceMigration, sin);
  256. if (sm->connect_complete.cb) {
  257. sm->connect_complete.cb(sm->connect_complete.opaque, NULL);
  258. }
  259. sm->connect_complete.cb = NULL;
  260. }
  261. static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
  262. {
  263. qapi_event_send_spice_migrate_completed(&error_abort);
  264. spice_migration_completed = true;
  265. }
  266. /* config string parsing */
  267. static int name2enum(const char *string, const char *table[], int entries)
  268. {
  269. int i;
  270. if (string) {
  271. for (i = 0; i < entries; i++) {
  272. if (!table[i]) {
  273. continue;
  274. }
  275. if (strcmp(string, table[i]) != 0) {
  276. continue;
  277. }
  278. return i;
  279. }
  280. }
  281. return -1;
  282. }
  283. static int parse_name(const char *string, const char *optname,
  284. const char *table[], int entries)
  285. {
  286. int value = name2enum(string, table, entries);
  287. if (value != -1) {
  288. return value;
  289. }
  290. error_report("spice: invalid %s: %s", optname, string);
  291. exit(1);
  292. }
  293. static const char *stream_video_names[] = {
  294. [ SPICE_STREAM_VIDEO_OFF ] = "off",
  295. [ SPICE_STREAM_VIDEO_ALL ] = "all",
  296. [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
  297. };
  298. #define parse_stream_video(_name) \
  299. parse_name(_name, "stream video control", \
  300. stream_video_names, ARRAY_SIZE(stream_video_names))
  301. static const char *compression_names[] = {
  302. [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
  303. [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
  304. [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
  305. [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
  306. [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
  307. [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
  308. };
  309. #define parse_compression(_name) \
  310. parse_name(_name, "image compression", \
  311. compression_names, ARRAY_SIZE(compression_names))
  312. static const char *wan_compression_names[] = {
  313. [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
  314. [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
  315. [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
  316. };
  317. #define parse_wan_compression(_name) \
  318. parse_name(_name, "wan compression", \
  319. wan_compression_names, ARRAY_SIZE(wan_compression_names))
  320. /* functions for the rest of qemu */
  321. static SpiceChannelList *qmp_query_spice_channels(void)
  322. {
  323. SpiceChannelList *cur_item = NULL, *head = NULL;
  324. ChannelList *item;
  325. QTAILQ_FOREACH(item, &channel_list, link) {
  326. SpiceChannelList *chan;
  327. char host[NI_MAXHOST], port[NI_MAXSERV];
  328. struct sockaddr *paddr;
  329. socklen_t plen;
  330. if (!(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT)) {
  331. error_report("invalid channel event");
  332. return NULL;
  333. }
  334. chan = g_malloc0(sizeof(*chan));
  335. chan->value = g_malloc0(sizeof(*chan->value));
  336. chan->value->base = g_malloc0(sizeof(*chan->value->base));
  337. paddr = (struct sockaddr *)&item->info->paddr_ext;
  338. plen = item->info->plen_ext;
  339. getnameinfo(paddr, plen,
  340. host, sizeof(host), port, sizeof(port),
  341. NI_NUMERICHOST | NI_NUMERICSERV);
  342. chan->value->base->host = g_strdup(host);
  343. chan->value->base->port = g_strdup(port);
  344. chan->value->base->family = inet_netfamily(paddr->sa_family);
  345. chan->value->connection_id = item->info->connection_id;
  346. chan->value->channel_type = item->info->type;
  347. chan->value->channel_id = item->info->id;
  348. chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
  349. /* XXX: waiting for the qapi to support GSList */
  350. if (!cur_item) {
  351. head = cur_item = chan;
  352. } else {
  353. cur_item->next = chan;
  354. cur_item = chan;
  355. }
  356. }
  357. return head;
  358. }
  359. static QemuOptsList qemu_spice_opts = {
  360. .name = "spice",
  361. .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
  362. .desc = {
  363. {
  364. .name = "port",
  365. .type = QEMU_OPT_NUMBER,
  366. },{
  367. .name = "tls-port",
  368. .type = QEMU_OPT_NUMBER,
  369. },{
  370. .name = "addr",
  371. .type = QEMU_OPT_STRING,
  372. },{
  373. .name = "ipv4",
  374. .type = QEMU_OPT_BOOL,
  375. },{
  376. .name = "ipv6",
  377. .type = QEMU_OPT_BOOL,
  378. },{
  379. .name = "password",
  380. .type = QEMU_OPT_STRING,
  381. },{
  382. .name = "disable-ticketing",
  383. .type = QEMU_OPT_BOOL,
  384. },{
  385. .name = "disable-copy-paste",
  386. .type = QEMU_OPT_BOOL,
  387. },{
  388. .name = "disable-agent-file-xfer",
  389. .type = QEMU_OPT_BOOL,
  390. },{
  391. .name = "sasl",
  392. .type = QEMU_OPT_BOOL,
  393. },{
  394. .name = "x509-dir",
  395. .type = QEMU_OPT_STRING,
  396. },{
  397. .name = "x509-key-file",
  398. .type = QEMU_OPT_STRING,
  399. },{
  400. .name = "x509-key-password",
  401. .type = QEMU_OPT_STRING,
  402. },{
  403. .name = "x509-cert-file",
  404. .type = QEMU_OPT_STRING,
  405. },{
  406. .name = "x509-cacert-file",
  407. .type = QEMU_OPT_STRING,
  408. },{
  409. .name = "x509-dh-key-file",
  410. .type = QEMU_OPT_STRING,
  411. },{
  412. .name = "tls-ciphers",
  413. .type = QEMU_OPT_STRING,
  414. },{
  415. .name = "tls-channel",
  416. .type = QEMU_OPT_STRING,
  417. },{
  418. .name = "plaintext-channel",
  419. .type = QEMU_OPT_STRING,
  420. },{
  421. .name = "image-compression",
  422. .type = QEMU_OPT_STRING,
  423. },{
  424. .name = "jpeg-wan-compression",
  425. .type = QEMU_OPT_STRING,
  426. },{
  427. .name = "zlib-glz-wan-compression",
  428. .type = QEMU_OPT_STRING,
  429. },{
  430. .name = "streaming-video",
  431. .type = QEMU_OPT_STRING,
  432. },{
  433. .name = "agent-mouse",
  434. .type = QEMU_OPT_BOOL,
  435. },{
  436. .name = "playback-compression",
  437. .type = QEMU_OPT_BOOL,
  438. }, {
  439. .name = "seamless-migration",
  440. .type = QEMU_OPT_BOOL,
  441. },
  442. { /* end of list */ }
  443. },
  444. };
  445. SpiceInfo *qmp_query_spice(Error **errp)
  446. {
  447. QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
  448. int port, tls_port;
  449. const char *addr;
  450. SpiceInfo *info;
  451. unsigned int major;
  452. unsigned int minor;
  453. unsigned int micro;
  454. info = g_malloc0(sizeof(*info));
  455. if (!spice_server || !opts) {
  456. info->enabled = false;
  457. return info;
  458. }
  459. info->enabled = true;
  460. info->migrated = spice_migration_completed;
  461. addr = qemu_opt_get(opts, "addr");
  462. port = qemu_opt_get_number(opts, "port", 0);
  463. tls_port = qemu_opt_get_number(opts, "tls-port", 0);
  464. info->has_auth = true;
  465. info->auth = g_strdup(auth);
  466. info->has_host = true;
  467. info->host = g_strdup(addr ? addr : "*");
  468. info->has_compiled_version = true;
  469. major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
  470. minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
  471. micro = SPICE_SERVER_VERSION & 0xff;
  472. info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
  473. if (port) {
  474. info->has_port = true;
  475. info->port = port;
  476. }
  477. if (tls_port) {
  478. info->has_tls_port = true;
  479. info->tls_port = tls_port;
  480. }
  481. info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
  482. SPICE_QUERY_MOUSE_MODE_SERVER :
  483. SPICE_QUERY_MOUSE_MODE_CLIENT;
  484. /* for compatibility with the original command */
  485. info->has_channels = true;
  486. info->channels = qmp_query_spice_channels();
  487. return info;
  488. }
  489. static void migration_state_notifier(Notifier *notifier, void *data)
  490. {
  491. MigrationState *s = data;
  492. if (!spice_have_target_host) {
  493. return;
  494. }
  495. if (migration_in_setup(s)) {
  496. spice_server_migrate_start(spice_server);
  497. } else if (migration_has_finished(s)) {
  498. spice_server_migrate_end(spice_server, true);
  499. spice_have_target_host = false;
  500. } else if (migration_has_failed(s)) {
  501. spice_server_migrate_end(spice_server, false);
  502. spice_have_target_host = false;
  503. }
  504. }
  505. int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
  506. const char *subject,
  507. MonitorCompletion *cb, void *opaque)
  508. {
  509. int ret;
  510. spice_migrate.connect_complete.cb = cb;
  511. spice_migrate.connect_complete.opaque = opaque;
  512. ret = spice_server_migrate_connect(spice_server, hostname,
  513. port, tls_port, subject);
  514. spice_have_target_host = true;
  515. return ret;
  516. }
  517. static int add_channel(const char *name, const char *value, void *opaque)
  518. {
  519. int security = 0;
  520. int rc;
  521. if (strcmp(name, "tls-channel") == 0) {
  522. int *tls_port = opaque;
  523. if (!*tls_port) {
  524. error_report("spice: tried to setup tls-channel"
  525. " without specifying a TLS port");
  526. exit(1);
  527. }
  528. security = SPICE_CHANNEL_SECURITY_SSL;
  529. }
  530. if (strcmp(name, "plaintext-channel") == 0) {
  531. security = SPICE_CHANNEL_SECURITY_NONE;
  532. }
  533. if (security == 0) {
  534. return 0;
  535. }
  536. if (strcmp(value, "default") == 0) {
  537. rc = spice_server_set_channel_security(spice_server, NULL, security);
  538. } else {
  539. rc = spice_server_set_channel_security(spice_server, value, security);
  540. }
  541. if (rc != 0) {
  542. error_report("spice: failed to set channel security for %s", value);
  543. exit(1);
  544. }
  545. return 0;
  546. }
  547. static void vm_change_state_handler(void *opaque, int running,
  548. RunState state)
  549. {
  550. if (running) {
  551. qemu_spice_display_start();
  552. } else {
  553. qemu_spice_display_stop();
  554. }
  555. }
  556. void qemu_spice_init(void)
  557. {
  558. QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
  559. const char *password, *str, *x509_dir, *addr,
  560. *x509_key_password = NULL,
  561. *x509_dh_file = NULL,
  562. *tls_ciphers = NULL;
  563. char *x509_key_file = NULL,
  564. *x509_cert_file = NULL,
  565. *x509_cacert_file = NULL;
  566. int port, tls_port, addr_flags;
  567. spice_image_compression_t compression;
  568. spice_wan_compression_t wan_compr;
  569. bool seamless_migration;
  570. qemu_thread_get_self(&me);
  571. if (!opts) {
  572. return;
  573. }
  574. port = qemu_opt_get_number(opts, "port", 0);
  575. tls_port = qemu_opt_get_number(opts, "tls-port", 0);
  576. if (!port && !tls_port) {
  577. error_report("neither port nor tls-port specified for spice");
  578. exit(1);
  579. }
  580. if (port < 0 || port > 65535) {
  581. error_report("spice port is out of range");
  582. exit(1);
  583. }
  584. if (tls_port < 0 || tls_port > 65535) {
  585. error_report("spice tls-port is out of range");
  586. exit(1);
  587. }
  588. password = qemu_opt_get(opts, "password");
  589. if (tls_port) {
  590. x509_dir = qemu_opt_get(opts, "x509-dir");
  591. if (!x509_dir) {
  592. x509_dir = ".";
  593. }
  594. str = qemu_opt_get(opts, "x509-key-file");
  595. if (str) {
  596. x509_key_file = g_strdup(str);
  597. } else {
  598. x509_key_file = g_strdup_printf("%s/%s", x509_dir,
  599. X509_SERVER_KEY_FILE);
  600. }
  601. str = qemu_opt_get(opts, "x509-cert-file");
  602. if (str) {
  603. x509_cert_file = g_strdup(str);
  604. } else {
  605. x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
  606. X509_SERVER_CERT_FILE);
  607. }
  608. str = qemu_opt_get(opts, "x509-cacert-file");
  609. if (str) {
  610. x509_cacert_file = g_strdup(str);
  611. } else {
  612. x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
  613. X509_CA_CERT_FILE);
  614. }
  615. x509_key_password = qemu_opt_get(opts, "x509-key-password");
  616. x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
  617. tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
  618. }
  619. addr = qemu_opt_get(opts, "addr");
  620. addr_flags = 0;
  621. if (qemu_opt_get_bool(opts, "ipv4", 0)) {
  622. addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
  623. } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
  624. addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
  625. }
  626. spice_server = spice_server_new();
  627. spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
  628. if (port) {
  629. spice_server_set_port(spice_server, port);
  630. }
  631. if (tls_port) {
  632. spice_server_set_tls(spice_server, tls_port,
  633. x509_cacert_file,
  634. x509_cert_file,
  635. x509_key_file,
  636. x509_key_password,
  637. x509_dh_file,
  638. tls_ciphers);
  639. }
  640. if (password) {
  641. qemu_spice_set_passwd(password, false, false);
  642. }
  643. if (qemu_opt_get_bool(opts, "sasl", 0)) {
  644. if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
  645. spice_server_set_sasl(spice_server, 1) == -1) {
  646. error_report("spice: failed to enable sasl");
  647. exit(1);
  648. }
  649. auth = "sasl";
  650. }
  651. if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
  652. auth = "none";
  653. spice_server_set_noauth(spice_server);
  654. }
  655. if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
  656. spice_server_set_agent_copypaste(spice_server, false);
  657. }
  658. if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
  659. #if SPICE_SERVER_VERSION >= 0x000c04
  660. spice_server_set_agent_file_xfer(spice_server, false);
  661. #else
  662. error_report("this qemu build does not support the "
  663. "\"disable-agent-file-xfer\" option");
  664. exit(1);
  665. #endif
  666. }
  667. compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
  668. str = qemu_opt_get(opts, "image-compression");
  669. if (str) {
  670. compression = parse_compression(str);
  671. }
  672. spice_server_set_image_compression(spice_server, compression);
  673. wan_compr = SPICE_WAN_COMPRESSION_AUTO;
  674. str = qemu_opt_get(opts, "jpeg-wan-compression");
  675. if (str) {
  676. wan_compr = parse_wan_compression(str);
  677. }
  678. spice_server_set_jpeg_compression(spice_server, wan_compr);
  679. wan_compr = SPICE_WAN_COMPRESSION_AUTO;
  680. str = qemu_opt_get(opts, "zlib-glz-wan-compression");
  681. if (str) {
  682. wan_compr = parse_wan_compression(str);
  683. }
  684. spice_server_set_zlib_glz_compression(spice_server, wan_compr);
  685. str = qemu_opt_get(opts, "streaming-video");
  686. if (str) {
  687. int streaming_video = parse_stream_video(str);
  688. spice_server_set_streaming_video(spice_server, streaming_video);
  689. } else {
  690. spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
  691. }
  692. spice_server_set_agent_mouse
  693. (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
  694. spice_server_set_playback_compression
  695. (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
  696. qemu_opt_foreach(opts, add_channel, &tls_port, 0);
  697. spice_server_set_name(spice_server, qemu_name);
  698. spice_server_set_uuid(spice_server, qemu_uuid);
  699. seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
  700. spice_server_set_seamless_migration(spice_server, seamless_migration);
  701. if (spice_server_init(spice_server, &core_interface) != 0) {
  702. error_report("failed to initialize spice server");
  703. exit(1);
  704. };
  705. using_spice = 1;
  706. migration_state.notify = migration_state_notifier;
  707. add_migration_state_change_notifier(&migration_state);
  708. spice_migrate.sin.base.sif = &migrate_interface.base;
  709. spice_migrate.connect_complete.cb = NULL;
  710. qemu_spice_add_interface(&spice_migrate.sin.base);
  711. qemu_spice_input_init();
  712. qemu_spice_audio_init();
  713. qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
  714. g_free(x509_key_file);
  715. g_free(x509_cert_file);
  716. g_free(x509_cacert_file);
  717. #if SPICE_SERVER_VERSION >= 0x000c02
  718. qemu_spice_register_ports();
  719. #endif
  720. }
  721. int qemu_spice_add_interface(SpiceBaseInstance *sin)
  722. {
  723. if (!spice_server) {
  724. if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
  725. error_report("Oops: spice configured but not active");
  726. exit(1);
  727. }
  728. /*
  729. * Create a spice server instance.
  730. * It does *not* listen on the network.
  731. * It handles QXL local rendering only.
  732. *
  733. * With a command line like '-vnc :0 -vga qxl' you'll end up here.
  734. */
  735. spice_server = spice_server_new();
  736. spice_server_set_sasl_appname(spice_server, "qemu");
  737. spice_server_init(spice_server, &core_interface);
  738. qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
  739. }
  740. return spice_server_add_interface(spice_server, sin);
  741. }
  742. static GSList *spice_consoles;
  743. bool qemu_spice_have_display_interface(QemuConsole *con)
  744. {
  745. if (g_slist_find(spice_consoles, con)) {
  746. return true;
  747. }
  748. return false;
  749. }
  750. int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
  751. {
  752. if (g_slist_find(spice_consoles, con)) {
  753. return -1;
  754. }
  755. qxlin->id = qemu_console_get_index(con);
  756. spice_consoles = g_slist_append(spice_consoles, con);
  757. return qemu_spice_add_interface(&qxlin->base);
  758. }
  759. static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
  760. {
  761. time_t lifetime, now = time(NULL);
  762. char *passwd;
  763. if (now < auth_expires) {
  764. passwd = auth_passwd;
  765. lifetime = (auth_expires - now);
  766. if (lifetime > INT_MAX) {
  767. lifetime = INT_MAX;
  768. }
  769. } else {
  770. passwd = NULL;
  771. lifetime = 1;
  772. }
  773. return spice_server_set_ticket(spice_server, passwd, lifetime,
  774. fail_if_conn, disconnect_if_conn);
  775. }
  776. int qemu_spice_set_passwd(const char *passwd,
  777. bool fail_if_conn, bool disconnect_if_conn)
  778. {
  779. if (strcmp(auth, "spice") != 0) {
  780. return -1;
  781. }
  782. g_free(auth_passwd);
  783. auth_passwd = g_strdup(passwd);
  784. return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
  785. }
  786. int qemu_spice_set_pw_expire(time_t expires)
  787. {
  788. auth_expires = expires;
  789. return qemu_spice_set_ticket(false, false);
  790. }
  791. int qemu_spice_display_add_client(int csock, int skipauth, int tls)
  792. {
  793. if (tls) {
  794. return spice_server_add_ssl_client(spice_server, csock, skipauth);
  795. } else {
  796. return spice_server_add_client(spice_server, csock, skipauth);
  797. }
  798. }
  799. void qemu_spice_display_start(void)
  800. {
  801. spice_display_is_running = true;
  802. spice_server_vm_start(spice_server);
  803. }
  804. void qemu_spice_display_stop(void)
  805. {
  806. spice_server_vm_stop(spice_server);
  807. spice_display_is_running = false;
  808. }
  809. int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
  810. {
  811. return spice_display_is_running;
  812. }
  813. static void spice_register_config(void)
  814. {
  815. qemu_add_opts(&qemu_spice_opts);
  816. }
  817. machine_init(spice_register_config);