spice-core.c 27 KB

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