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