spice-core.c 29 KB

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