Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU VNC display driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> |
| 5 | * Copyright (C) 2006 Fabrice Bellard |
| 6 | * Copyright (C) 2009 Red Hat, Inc |
| 7 | * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 29 | #include "qemu/osdep.h" |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 30 | #include "vnc.h" |
| 31 | #include "vnc-jobs.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/sockets.h" |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 33 | #include "qemu/main-loop.h" |
Markus Armbruster | a0b1a66 | 2015-03-17 18:16:21 +0100 | [diff] [blame] | 34 | #include "block/aio.h" |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Locking: |
| 38 | * |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 39 | * There are three levels of locking: |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 40 | * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?) |
| 41 | * - VncDisplay global lock: mainly used for framebuffer updates to avoid |
| 42 | * screen corruption if the framebuffer is updated |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 43 | * while the worker is doing something. |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 44 | * - VncState::output lock: used to make sure the output buffer is not corrupted |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 45 | * if two threads try to write on it at the same time |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 46 | * |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 47 | * While the VNC worker thread is working, the VncDisplay global lock is held |
| 48 | * to avoid screen corruption (this does not block vnc_refresh() because it |
| 49 | * uses trylock()) but the output lock is not held because the thread works on |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 50 | * its own output buffer. |
| 51 | * When the encoding job is done, the worker thread will hold the output lock |
| 52 | * and copy its output buffer in vs->output. |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 53 | */ |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 54 | |
| 55 | struct VncJobQueue { |
| 56 | QemuCond cond; |
| 57 | QemuMutex mutex; |
| 58 | QemuThread thread; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 59 | bool exit; |
| 60 | QTAILQ_HEAD(, VncJob) jobs; |
| 61 | }; |
| 62 | |
| 63 | typedef struct VncJobQueue VncJobQueue; |
| 64 | |
| 65 | /* |
| 66 | * We use a single global queue, but most of the functions are |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 67 | * already reentrant, so we can easily add more than one encoding thread |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 68 | */ |
| 69 | static VncJobQueue *queue; |
| 70 | |
| 71 | static void vnc_lock_queue(VncJobQueue *queue) |
| 72 | { |
| 73 | qemu_mutex_lock(&queue->mutex); |
| 74 | } |
| 75 | |
| 76 | static void vnc_unlock_queue(VncJobQueue *queue) |
| 77 | { |
| 78 | qemu_mutex_unlock(&queue->mutex); |
| 79 | } |
| 80 | |
| 81 | VncJob *vnc_job_new(VncState *vs) |
| 82 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 83 | VncJob *job = g_new0(VncJob, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 84 | |
| 85 | job->vs = vs; |
| 86 | vnc_lock_queue(queue); |
| 87 | QLIST_INIT(&job->rectangles); |
| 88 | vnc_unlock_queue(queue); |
| 89 | return job; |
| 90 | } |
| 91 | |
| 92 | int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h) |
| 93 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 94 | VncRectEntry *entry = g_new0(VncRectEntry, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 95 | |
| 96 | entry->rect.x = x; |
| 97 | entry->rect.y = y; |
| 98 | entry->rect.w = w; |
| 99 | entry->rect.h = h; |
| 100 | |
| 101 | vnc_lock_queue(queue); |
| 102 | QLIST_INSERT_HEAD(&job->rectangles, entry, next); |
| 103 | vnc_unlock_queue(queue); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | void vnc_job_push(VncJob *job) |
| 108 | { |
| 109 | vnc_lock_queue(queue); |
| 110 | if (queue->exit || QLIST_EMPTY(&job->rectangles)) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 111 | g_free(job); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 112 | } else { |
| 113 | QTAILQ_INSERT_TAIL(&queue->jobs, job, next); |
| 114 | qemu_cond_broadcast(&queue->cond); |
| 115 | } |
| 116 | vnc_unlock_queue(queue); |
| 117 | } |
| 118 | |
| 119 | static bool vnc_has_job_locked(VncState *vs) |
| 120 | { |
| 121 | VncJob *job; |
| 122 | |
| 123 | QTAILQ_FOREACH(job, &queue->jobs, next) { |
| 124 | if (job->vs == vs || !vs) { |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool vnc_has_job(VncState *vs) |
| 132 | { |
| 133 | bool ret; |
| 134 | |
| 135 | vnc_lock_queue(queue); |
| 136 | ret = vnc_has_job_locked(vs); |
| 137 | vnc_unlock_queue(queue); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | void vnc_jobs_clear(VncState *vs) |
| 142 | { |
| 143 | VncJob *job, *tmp; |
| 144 | |
| 145 | vnc_lock_queue(queue); |
| 146 | QTAILQ_FOREACH_SAFE(job, &queue->jobs, next, tmp) { |
| 147 | if (job->vs == vs || !vs) { |
| 148 | QTAILQ_REMOVE(&queue->jobs, job, next); |
| 149 | } |
| 150 | } |
| 151 | vnc_unlock_queue(queue); |
| 152 | } |
| 153 | |
| 154 | void vnc_jobs_join(VncState *vs) |
| 155 | { |
| 156 | vnc_lock_queue(queue); |
| 157 | while (vnc_has_job_locked(vs)) { |
| 158 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 159 | } |
| 160 | vnc_unlock_queue(queue); |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 161 | vnc_jobs_consume_buffer(vs); |
| 162 | } |
| 163 | |
| 164 | void vnc_jobs_consume_buffer(VncState *vs) |
| 165 | { |
| 166 | bool flush; |
| 167 | |
| 168 | vnc_lock_output(vs); |
| 169 | if (vs->jobs_buffer.offset) { |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 170 | if (vs->ioc != NULL && buffer_empty(&vs->output)) { |
| 171 | if (vs->ioc_tag) { |
| 172 | g_source_remove(vs->ioc_tag); |
| 173 | } |
| 174 | vs->ioc_tag = qio_channel_add_watch( |
| 175 | vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 176 | } |
| 177 | buffer_move(&vs->output, &vs->jobs_buffer); |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 178 | } |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 179 | flush = vs->ioc != NULL && vs->abort != true; |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 180 | vnc_unlock_output(vs); |
| 181 | |
| 182 | if (flush) { |
| 183 | vnc_flush(vs); |
| 184 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Copy data for local use |
| 189 | */ |
| 190 | static void vnc_async_encoding_start(VncState *orig, VncState *local) |
| 191 | { |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 192 | buffer_init(&local->output, "vnc-worker-output"); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 193 | local->sioc = NULL; /* Don't do any network work on this thread */ |
| 194 | local->ioc = NULL; /* Don't do any network work on this thread */ |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 195 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 196 | local->vnc_encoding = orig->vnc_encoding; |
| 197 | local->features = orig->features; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 198 | local->vd = orig->vd; |
Corentin Chary | 7d964c9 | 2011-02-04 09:05:56 +0100 | [diff] [blame] | 199 | local->lossy_rect = orig->lossy_rect; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 200 | local->write_pixels = orig->write_pixels; |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 201 | local->client_pf = orig->client_pf; |
| 202 | local->client_be = orig->client_be; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 203 | local->tight = orig->tight; |
| 204 | local->zlib = orig->zlib; |
| 205 | local->hextile = orig->hextile; |
Corentin Chary | 148954f | 2011-02-04 09:06:01 +0100 | [diff] [blame] | 206 | local->zrle = orig->zrle; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void vnc_async_encoding_end(VncState *orig, VncState *local) |
| 210 | { |
| 211 | orig->tight = local->tight; |
| 212 | orig->zlib = local->zlib; |
| 213 | orig->hextile = local->hextile; |
Corentin Chary | 148954f | 2011-02-04 09:06:01 +0100 | [diff] [blame] | 214 | orig->zrle = local->zrle; |
Corentin Chary | 7d964c9 | 2011-02-04 09:05:56 +0100 | [diff] [blame] | 215 | orig->lossy_rect = local->lossy_rect; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int vnc_worker_thread_loop(VncJobQueue *queue) |
| 219 | { |
| 220 | VncJob *job; |
| 221 | VncRectEntry *entry, *tmp; |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 222 | VncState vs = {}; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 223 | int n_rectangles; |
| 224 | int saved_offset; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 225 | |
| 226 | vnc_lock_queue(queue); |
| 227 | while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) { |
| 228 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 229 | } |
| 230 | /* Here job can only be NULL if queue->exit is true */ |
| 231 | job = QTAILQ_FIRST(&queue->jobs); |
| 232 | vnc_unlock_queue(queue); |
| 233 | |
| 234 | if (queue->exit) { |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | vnc_lock_output(job->vs); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 239 | if (job->vs->ioc == NULL || job->vs->abort == true) { |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 240 | vnc_unlock_output(job->vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 241 | goto disconnected; |
| 242 | } |
Peter Lieven | c3d6899 | 2015-10-30 12:10:11 +0100 | [diff] [blame] | 243 | if (buffer_empty(&job->vs->output)) { |
| 244 | /* |
| 245 | * Looks like a NOP as it obviously moves no data. But it |
| 246 | * moves the empty buffer, so we don't have to malloc a new |
| 247 | * one for vs.output |
| 248 | */ |
| 249 | buffer_move_empty(&vs.output, &job->vs->output); |
| 250 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 251 | vnc_unlock_output(job->vs); |
| 252 | |
| 253 | /* Make a local copy of vs and switch output buffers */ |
| 254 | vnc_async_encoding_start(job->vs, &vs); |
| 255 | |
| 256 | /* Start sending rectangles */ |
| 257 | n_rectangles = 0; |
| 258 | vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
| 259 | vnc_write_u8(&vs, 0); |
| 260 | saved_offset = vs.output.offset; |
| 261 | vnc_write_u16(&vs, 0); |
| 262 | |
| 263 | vnc_lock_display(job->vs->vd); |
| 264 | QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) { |
| 265 | int n; |
| 266 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 267 | if (job->vs->ioc == NULL) { |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 268 | vnc_unlock_display(job->vs->vd); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 269 | /* Copy persistent encoding data */ |
| 270 | vnc_async_encoding_end(job->vs, &vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 271 | goto disconnected; |
| 272 | } |
| 273 | |
| 274 | n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y, |
| 275 | entry->rect.w, entry->rect.h); |
| 276 | |
| 277 | if (n >= 0) { |
| 278 | n_rectangles += n; |
| 279 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 280 | g_free(entry); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 281 | } |
| 282 | vnc_unlock_display(job->vs->vd); |
| 283 | |
| 284 | /* Put n_rectangles at the beginning of the message */ |
| 285 | vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
| 286 | vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
| 287 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 288 | vnc_lock_output(job->vs); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 289 | if (job->vs->ioc != NULL) { |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 290 | buffer_move(&job->vs->jobs_buffer, &vs.output); |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 291 | /* Copy persistent encoding data */ |
| 292 | vnc_async_encoding_end(job->vs, &vs); |
| 293 | |
| 294 | qemu_bh_schedule(job->vs->bh); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 295 | } else { |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 296 | buffer_reset(&vs.output); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 297 | /* Copy persistent encoding data */ |
| 298 | vnc_async_encoding_end(job->vs, &vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 299 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 300 | vnc_unlock_output(job->vs); |
| 301 | |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 302 | disconnected: |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 303 | vnc_lock_queue(queue); |
| 304 | QTAILQ_REMOVE(&queue->jobs, job, next); |
| 305 | vnc_unlock_queue(queue); |
| 306 | qemu_cond_broadcast(&queue->cond); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 307 | g_free(job); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static VncJobQueue *vnc_queue_init(void) |
| 312 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 313 | VncJobQueue *queue = g_new0(VncJobQueue, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 314 | |
| 315 | qemu_cond_init(&queue->cond); |
| 316 | qemu_mutex_init(&queue->mutex); |
| 317 | QTAILQ_INIT(&queue->jobs); |
| 318 | return queue; |
| 319 | } |
| 320 | |
| 321 | static void vnc_queue_clear(VncJobQueue *q) |
| 322 | { |
| 323 | qemu_cond_destroy(&queue->cond); |
| 324 | qemu_mutex_destroy(&queue->mutex); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 325 | g_free(q); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 326 | queue = NULL; /* Unset global queue */ |
| 327 | } |
| 328 | |
| 329 | static void *vnc_worker_thread(void *arg) |
| 330 | { |
| 331 | VncJobQueue *queue = arg; |
| 332 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 333 | qemu_thread_get_self(&queue->thread); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 334 | |
| 335 | while (!vnc_worker_thread_loop(queue)) ; |
| 336 | vnc_queue_clear(queue); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
Blue Swirl | 71a8cde | 2012-10-28 11:04:48 +0000 | [diff] [blame] | 340 | static bool vnc_worker_thread_running(void) |
| 341 | { |
| 342 | return queue; /* Check global queue */ |
| 343 | } |
| 344 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 345 | void vnc_start_worker_thread(void) |
| 346 | { |
| 347 | VncJobQueue *q; |
| 348 | |
| 349 | if (vnc_worker_thread_running()) |
| 350 | return ; |
| 351 | |
| 352 | q = vnc_queue_init(); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 353 | qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q, |
| 354 | QEMU_THREAD_DETACHED); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 355 | queue = q; /* Set global queue */ |
| 356 | } |