spice-core.c 28 KB

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