2
0

spice-core.c 29 KB

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