spice-core.c 26 KB

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