| 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 | |
| 29 | #include "qemu/osdep.h" |
| 30 | #include "vnc.h" |
| 31 | #include "vnc-jobs.h" |
| 32 | #include "trace.h" |
| 33 | |
| 34 | /* |
| 35 | * Locking: |
| 36 | * |
| 37 | * There are three levels of locking: |
| 38 | * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?) |
| 39 | * - VncDisplay global lock: mainly used for framebuffer updates to avoid |
| 40 | * screen corruption if the framebuffer is updated |
| 41 | * while the worker is doing something. |
| 42 | * - VncState::output lock: used to make sure the output buffer is not corrupted |
| 43 | * if two threads try to write on it at the same time |
| 44 | * |
| 45 | * While the VNC worker thread is working, the VncDisplay global lock is held |
| 46 | * to avoid screen corruption (this does not block vnc_refresh() because it |
| 47 | * uses trylock()) but the output lock is not held because the thread works on |
| 48 | * its own output buffer. |
| 49 | * When the encoding job is done, the worker thread will hold the output lock |
| 50 | * and copy its output buffer in vs->output. |
| 51 | */ |
| 52 | |
| 53 | struct VncJobQueue { |
| 54 | QemuCond cond; |
| 55 | QemuMutex mutex; |
| 56 | QemuThread thread; |
| 57 | bool exit; |
| 58 | QTAILQ_HEAD(, VncJob) jobs; |
| 59 | }; |
| 60 | |
| 61 | static void vnc_lock_queue(VncJobQueue *queue) |
| 62 | { |
| 63 | qemu_mutex_lock(&queue->mutex); |
| 64 | } |
| 65 | |
| 66 | static void vnc_unlock_queue(VncJobQueue *queue) |
| 67 | { |
| 68 | qemu_mutex_unlock(&queue->mutex); |
| 69 | } |
| 70 | |
| 71 | VncJob *vnc_job_new(VncState *vs) |
| 72 | { |
| 73 | VncJob *job = g_new0(VncJob, 1); |
| 74 | |
| 75 | assert(vs->magic == VNC_MAGIC); |
| 76 | job->vs = vs; |
| 77 | QLIST_INIT(&job->rectangles); |
| 78 | return job; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Do not call this after pushing the job. |
| 83 | */ |
| 84 | int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h) |
| 85 | { |
| 86 | VncRectEntry *entry = g_new0(VncRectEntry, 1); |
| 87 | |
| 88 | trace_vnc_job_add_rect(job->vs, job, x, y, w, h); |
| 89 | assert(!QTAILQ_IN_USE(job, next)); |
| 90 | |
| 91 | entry->rect.x = x; |
| 92 | entry->rect.y = y; |
| 93 | entry->rect.w = w; |
| 94 | entry->rect.h = h; |
| 95 | |
| 96 | QLIST_INSERT_HEAD(&job->rectangles, entry, next); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | static void vnc_job_free(VncJob *job) |
| 101 | { |
| 102 | VncRectEntry *entry, *tmp; |
| 103 | |
| 104 | if (!job) { |
| 105 | return; |
| 106 | } |
| 107 | QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) { |
| 108 | /* no need for QLIST_REMOVE(entry, next) */ |
| 109 | g_free(entry); |
| 110 | } |
| 111 | g_free(job); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Push a job onto the queue. Ownership of the job is transferred. |
| 116 | */ |
| 117 | void vnc_job_push(VncJob *job) |
| 118 | { |
| 119 | VncJobQueue *queue = job->vs->vd->queue; |
| 120 | |
| 121 | assert(!QTAILQ_IN_USE(job, next)); |
| 122 | |
| 123 | if (QLIST_EMPTY(&job->rectangles)) { |
| 124 | vnc_job_free(job); |
| 125 | } else { |
| 126 | vnc_lock_queue(queue); |
| 127 | assert(!queue->exit); |
| 128 | QTAILQ_INSERT_TAIL(&queue->jobs, job, next); |
| 129 | qemu_cond_broadcast(&queue->cond); |
| 130 | vnc_unlock_queue(queue); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static bool vnc_has_job_locked(VncState *vs) |
| 135 | { |
| 136 | VncJobQueue *queue = vs->vd->queue; |
| 137 | VncJob *job; |
| 138 | |
| 139 | QTAILQ_FOREACH(job, &queue->jobs, next) { |
| 140 | if (job->vs == vs) { |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | void vnc_jobs_join(VncState *vs) |
| 148 | { |
| 149 | VncJobQueue *queue = vs->vd->queue; |
| 150 | |
| 151 | vnc_lock_queue(queue); |
| 152 | while (vnc_has_job_locked(vs)) { |
| 153 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 154 | } |
| 155 | vnc_unlock_queue(queue); |
| 156 | vnc_jobs_consume_buffer(vs); |
| 157 | } |
| 158 | |
| 159 | void vnc_jobs_consume_buffer(VncState *vs) |
| 160 | { |
| 161 | bool flush; |
| 162 | |
| 163 | vnc_lock_output(vs); |
| 164 | if (!buffer_empty(&vs->jobs_buffer)) { |
| 165 | if (vs->ioc != NULL && buffer_empty(&vs->output)) { |
| 166 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
| 167 | if (vs->disconnecting == FALSE) { |
| 168 | vs->ioc_tag = qio_channel_add_watch( |
| 169 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT, |
| 170 | vnc_client_io, vs, NULL); |
| 171 | } |
| 172 | } |
| 173 | buffer_move(&vs->output, &vs->jobs_buffer); |
| 174 | |
| 175 | if (vs->job_update == VNC_STATE_UPDATE_FORCE) { |
| 176 | vs->force_update_offset = vs->output.offset; |
| 177 | } |
| 178 | vs->job_update = VNC_STATE_UPDATE_NONE; |
| 179 | } |
| 180 | flush = vs->ioc != NULL && vs->abort != true; |
| 181 | vnc_unlock_output(vs); |
| 182 | |
| 183 | if (flush) { |
| 184 | vnc_flush(vs); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Copy data for local use |
| 190 | */ |
| 191 | static void vnc_async_encoding_start(VncState *orig, VncState *local) |
| 192 | { |
| 193 | buffer_init(&local->output, "vnc-worker-output"); |
| 194 | local->sioc = NULL; /* Don't do any network work on this thread */ |
| 195 | local->ioc = NULL; /* Don't do any network work on this thread */ |
| 196 | |
| 197 | local->vnc_encoding = orig->vnc_encoding; |
| 198 | local->features = orig->features; |
| 199 | local->vd = orig->vd; |
| 200 | local->write_pixels = orig->write_pixels; |
| 201 | local->client_pf = orig->client_pf; |
| 202 | local->client_endian = orig->client_endian; |
| 203 | local->hextile = orig->hextile; |
| 204 | local->client_width = orig->client_width; |
| 205 | local->client_height = orig->client_height; |
| 206 | } |
| 207 | |
| 208 | static void vnc_async_encoding_end(VncState *orig, VncState *local) |
| 209 | { |
| 210 | buffer_free(&local->output); |
| 211 | orig->hextile = local->hextile; |
| 212 | } |
| 213 | |
| 214 | static bool vnc_worker_clamp_rect(VncState *vs, VncJob *job, VncRect *rect) |
| 215 | { |
| 216 | trace_vnc_job_clamp_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 217 | |
| 218 | if (rect->x >= vs->client_width) { |
| 219 | goto discard; |
| 220 | } |
| 221 | rect->w = MIN(vs->client_width - rect->x, rect->w); |
| 222 | if (rect->w == 0) { |
| 223 | goto discard; |
| 224 | } |
| 225 | |
| 226 | if (rect->y >= vs->client_height) { |
| 227 | goto discard; |
| 228 | } |
| 229 | rect->h = MIN(vs->client_height - rect->y, rect->h); |
| 230 | if (rect->h == 0) { |
| 231 | goto discard; |
| 232 | } |
| 233 | |
| 234 | trace_vnc_job_clamped_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 235 | return true; |
| 236 | |
| 237 | discard: |
| 238 | trace_vnc_job_discard_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | static int vnc_worker_thread_loop(VncJobQueue *queue) |
| 243 | { |
| 244 | VncConnection *vc; |
| 245 | VncJob *job; |
| 246 | VncRectEntry *entry, *tmp; |
| 247 | VncState vs = {}; |
| 248 | int n_rectangles; |
| 249 | int saved_offset; |
| 250 | |
| 251 | vnc_lock_queue(queue); |
| 252 | while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) { |
| 253 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 254 | } |
| 255 | if (queue->exit) { |
| 256 | vnc_unlock_queue(queue); |
| 257 | return 1; |
| 258 | } |
| 259 | job = QTAILQ_FIRST(&queue->jobs); |
| 260 | vnc_unlock_queue(queue); |
| 261 | |
| 262 | assert(job->vs->magic == VNC_MAGIC); |
| 263 | vc = container_of(job->vs, VncConnection, vs); |
| 264 | |
| 265 | vnc_lock_output(job->vs); |
| 266 | if (job->vs->ioc == NULL || job->vs->abort == true) { |
| 267 | vnc_unlock_output(job->vs); |
| 268 | goto disconnected; |
| 269 | } |
| 270 | if (buffer_empty(&job->vs->output)) { |
| 271 | /* |
| 272 | * Looks like a NOP as it obviously moves no data. But it |
| 273 | * moves the empty buffer, so we don't have to malloc a new |
| 274 | * one for vs.output |
| 275 | */ |
| 276 | buffer_move_empty(&vs.output, &job->vs->output); |
| 277 | } |
| 278 | vnc_unlock_output(job->vs); |
| 279 | |
| 280 | /* Make a local copy of vs and switch output buffers */ |
| 281 | vnc_async_encoding_start(job->vs, &vs); |
| 282 | vs.magic = VNC_MAGIC; |
| 283 | |
| 284 | /* Start sending rectangles */ |
| 285 | n_rectangles = 0; |
| 286 | vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
| 287 | vnc_write_u8(&vs, 0); |
| 288 | saved_offset = vs.output.offset; |
| 289 | vnc_write_u16(&vs, 0); |
| 290 | |
| 291 | vnc_lock_display(job->vs->vd); |
| 292 | QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) { |
| 293 | int n; |
| 294 | |
| 295 | if (job->vs->ioc == NULL) { |
| 296 | vnc_unlock_display(job->vs->vd); |
| 297 | /* Copy persistent encoding data */ |
| 298 | vnc_async_encoding_end(job->vs, &vs); |
| 299 | goto disconnected; |
| 300 | } |
| 301 | |
| 302 | if (vnc_worker_clamp_rect(&vs, job, &entry->rect)) { |
| 303 | n = vnc_send_framebuffer_update(&vs, &vc->worker, |
| 304 | entry->rect.x, entry->rect.y, |
| 305 | entry->rect.w, entry->rect.h); |
| 306 | |
| 307 | if (n >= 0) { |
| 308 | n_rectangles += n; |
| 309 | } |
| 310 | } |
| 311 | QLIST_REMOVE(entry, next); |
| 312 | g_free(entry); |
| 313 | } |
| 314 | trace_vnc_job_nrects(&vs, job, n_rectangles); |
| 315 | vnc_unlock_display(job->vs->vd); |
| 316 | |
| 317 | /* Put n_rectangles at the beginning of the message */ |
| 318 | vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
| 319 | vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
| 320 | |
| 321 | vnc_lock_output(job->vs); |
| 322 | if (job->vs->ioc != NULL) { |
| 323 | buffer_move(&job->vs->jobs_buffer, &vs.output); |
| 324 | /* Copy persistent encoding data */ |
| 325 | vnc_async_encoding_end(job->vs, &vs); |
| 326 | |
| 327 | qemu_bh_schedule(job->vs->bh); |
| 328 | } else { |
| 329 | /* Copy persistent encoding data */ |
| 330 | vnc_async_encoding_end(job->vs, &vs); |
| 331 | } |
| 332 | vnc_unlock_output(job->vs); |
| 333 | |
| 334 | disconnected: |
| 335 | vnc_lock_queue(queue); |
| 336 | QTAILQ_REMOVE(&queue->jobs, job, next); |
| 337 | vnc_unlock_queue(queue); |
| 338 | qemu_cond_broadcast(&queue->cond); |
| 339 | vnc_job_free(job); |
| 340 | vs.magic = 0; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static VncJobQueue *vnc_queue_new(void) |
| 345 | { |
| 346 | VncJobQueue *queue = g_new0(VncJobQueue, 1); |
| 347 | |
| 348 | qemu_cond_init(&queue->cond); |
| 349 | qemu_mutex_init(&queue->mutex); |
| 350 | QTAILQ_INIT(&queue->jobs); |
| 351 | return queue; |
| 352 | } |
| 353 | |
| 354 | static void vnc_queue_free(VncJobQueue *queue) |
| 355 | { |
| 356 | qemu_cond_destroy(&queue->cond); |
| 357 | qemu_mutex_destroy(&queue->mutex); |
| 358 | g_free(queue); |
| 359 | } |
| 360 | |
| 361 | static void *vnc_worker_thread(void *arg) |
| 362 | { |
| 363 | VncJobQueue *queue = arg; |
| 364 | |
| 365 | while (!vnc_worker_thread_loop(queue)) ; |
| 366 | |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | void vnc_start_worker_thread(VncDisplay *vd) |
| 371 | { |
| 372 | assert(vd->queue == NULL); |
| 373 | |
| 374 | vd->queue = vnc_queue_new(); |
| 375 | qemu_thread_create(&vd->queue->thread, "vnc_worker", vnc_worker_thread, vd->queue, |
| 376 | QEMU_THREAD_JOINABLE); |
| 377 | } |
| 378 | |
| 379 | void vnc_stop_worker_thread(VncDisplay *vd) |
| 380 | { |
| 381 | VncJobQueue *queue = vd->queue; |
| 382 | |
| 383 | if (!queue) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | /* all VNC clients must have finished before we can stop the worker thread */ |
| 388 | vnc_lock_queue(queue); |
| 389 | assert(QTAILQ_EMPTY(&queue->jobs)); |
| 390 | queue->exit = true; |
| 391 | qemu_cond_broadcast(&queue->cond); |
| 392 | vnc_unlock_queue(queue); |
| 393 | |
| 394 | qemu_thread_join(&queue->thread); |
| 395 | g_clear_pointer(&vd->queue, vnc_queue_free); |
| 396 | } |