2
0

spice-core.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 "sysemu/sysemu.h"
  20. #include "sysemu/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 Notifier 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 iothread lock if so before calling qemu
  189. * functions.
  190. */
  191. bool need_lock = !qemu_thread_is_self(&me);
  192. if (need_lock) {
  193. qemu_mutex_lock_iothread();
  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. qemu_mutex_unlock_iothread();
  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 void migration_state_notifier(Notifier *notifier, void *data)
  497. {
  498. MigrationState *s = data;
  499. if (!spice_have_target_host) {
  500. return;
  501. }
  502. if (migration_in_setup(s)) {
  503. spice_server_migrate_start(spice_server);
  504. } else if (migration_has_finished(s) ||
  505. migration_in_postcopy_after_devices(s)) {
  506. spice_server_migrate_end(spice_server, true);
  507. spice_have_target_host = false;
  508. } else if (migration_has_failed(s)) {
  509. spice_server_migrate_end(spice_server, false);
  510. spice_have_target_host = false;
  511. }
  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_state.notify = migration_state_notifier;
  721. add_migration_state_change_notifier(&migration_state);
  722. spice_migrate.base.sif = &migrate_interface.base;
  723. qemu_spice.add_interface(&spice_migrate.base);
  724. qemu_spice_input_init();
  725. qemu_spice_display_stop();
  726. g_free(x509_key_file);
  727. g_free(x509_cert_file);
  728. g_free(x509_cacert_file);
  729. g_free(password);
  730. #ifdef HAVE_SPICE_GL
  731. if (qemu_opt_get_bool(opts, "gl", 0)) {
  732. if ((port != 0) || (tls_port != 0)) {
  733. error_report("SPICE GL support is local-only for now and "
  734. "incompatible with -spice port/tls-port");
  735. exit(1);
  736. }
  737. #if defined(CONFIG_GBM)
  738. egl_init(qemu_opt_get(opts, "rendernode"), DISPLAYGL_MODE_ON, &error_fatal);
  739. #elif defined(CONFIG_ANGLE)
  740. if (qemu_egl_init_dpy_angle(DISPLAYGL_MODE_ES)) {
  741. error_report("SPICE GL failed to initialize ANGLE display");
  742. exit(1);
  743. }
  744. spice_gl_ctx = qemu_egl_init_ctx();
  745. if (!spice_gl_ctx) {
  746. error_report("egl: egl_init_ctx failed");
  747. exit(1);
  748. }
  749. #else
  750. error_report("No backend to support SPICE GL");
  751. exit(1);
  752. #endif
  753. display_opengl = 1;
  754. spice_opengl = 1;
  755. }
  756. #endif
  757. }
  758. static int qemu_spice_add_interface(SpiceBaseInstance *sin)
  759. {
  760. if (!spice_server) {
  761. if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
  762. error_report("Oops: spice configured but not active");
  763. exit(1);
  764. }
  765. /*
  766. * Create a spice server instance.
  767. * It does *not* listen on the network.
  768. * It handles QXL local rendering only.
  769. *
  770. * With a command line like '-vnc :0 -vga qxl' you'll end up here.
  771. */
  772. spice_server = spice_server_new();
  773. spice_server_set_sasl_appname(spice_server, "qemu");
  774. spice_server_init(spice_server, &core_interface);
  775. qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
  776. }
  777. return spice_server_add_interface(spice_server, sin);
  778. }
  779. static GSList *spice_consoles;
  780. bool qemu_spice_have_display_interface(QemuConsole *con)
  781. {
  782. if (g_slist_find(spice_consoles, con)) {
  783. return true;
  784. }
  785. return false;
  786. }
  787. int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
  788. {
  789. if (g_slist_find(spice_consoles, con)) {
  790. return -1;
  791. }
  792. qxlin->id = qemu_console_get_index(con);
  793. spice_consoles = g_slist_append(spice_consoles, con);
  794. return qemu_spice_add_interface(&qxlin->base);
  795. }
  796. static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
  797. {
  798. time_t lifetime, now = time(NULL);
  799. char *passwd;
  800. if (now < auth_expires) {
  801. passwd = auth_passwd;
  802. lifetime = (auth_expires - now);
  803. if (lifetime > INT_MAX) {
  804. lifetime = INT_MAX;
  805. }
  806. } else {
  807. passwd = NULL;
  808. lifetime = 1;
  809. }
  810. return spice_server_set_ticket(spice_server, passwd, lifetime,
  811. fail_if_conn, disconnect_if_conn);
  812. }
  813. static int qemu_spice_set_passwd(const char *passwd,
  814. bool fail_if_conn, bool disconnect_if_conn)
  815. {
  816. if (strcmp(auth, "spice") != 0) {
  817. return -1;
  818. }
  819. g_free(auth_passwd);
  820. auth_passwd = g_strdup(passwd);
  821. return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
  822. }
  823. static int qemu_spice_set_pw_expire(time_t expires)
  824. {
  825. auth_expires = expires;
  826. return qemu_spice_set_ticket(false, false);
  827. }
  828. static int qemu_spice_display_add_client(int csock, int skipauth, int tls)
  829. {
  830. #ifdef WIN32
  831. csock = qemu_close_socket_osfhandle(csock);
  832. #endif
  833. if (tls) {
  834. return spice_server_add_ssl_client(spice_server, csock, skipauth);
  835. } else {
  836. return spice_server_add_client(spice_server, csock, skipauth);
  837. }
  838. }
  839. void qemu_spice_display_start(void)
  840. {
  841. if (spice_display_is_running) {
  842. return;
  843. }
  844. spice_display_is_running = true;
  845. spice_server_vm_start(spice_server);
  846. }
  847. void qemu_spice_display_stop(void)
  848. {
  849. if (!spice_display_is_running) {
  850. return;
  851. }
  852. spice_server_vm_stop(spice_server);
  853. spice_display_is_running = false;
  854. }
  855. int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
  856. {
  857. return spice_display_is_running;
  858. }
  859. static struct QemuSpiceOps real_spice_ops = {
  860. .init = qemu_spice_init,
  861. .display_init = qemu_spice_display_init,
  862. .migrate_info = qemu_spice_migrate_info,
  863. .set_passwd = qemu_spice_set_passwd,
  864. .set_pw_expire = qemu_spice_set_pw_expire,
  865. .display_add_client = qemu_spice_display_add_client,
  866. .add_interface = qemu_spice_add_interface,
  867. .qmp_query = qmp_query_spice_real,
  868. };
  869. static void spice_register_config(void)
  870. {
  871. qemu_spice = real_spice_ops;
  872. qemu_add_opts(&qemu_spice_opts);
  873. }
  874. opts_init(spice_register_config);
  875. module_opts("spice");
  876. #ifdef HAVE_SPICE_GL
  877. module_dep("ui-opengl");
  878. #endif