]> git.proxmox.com Git - mirror_qemu.git/blame - ui/vnc-jobs.c
rbd: Fix bugs around -drive parameter "server"
[mirror_qemu.git] / ui / vnc-jobs.c
CommitLineData
bd023f95
CC
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
e16f4c87 29#include "qemu/osdep.h"
bd023f95
CC
30#include "vnc.h"
31#include "vnc-jobs.h"
1de7afc9 32#include "qemu/sockets.h"
d9034011 33#include "qemu/main-loop.h"
a0b1a66e 34#include "block/aio.h"
bd023f95
CC
35
36/*
37 * Locking:
38 *
11f66978 39 * There are three levels of locking:
bd023f95
CC
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
11f66978 43 * while the worker is doing something.
bd023f95 44 * - VncState::output lock: used to make sure the output buffer is not corrupted
11f66978 45 * if two threads try to write on it at the same time
bd023f95 46 *
11f66978
PM
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
bd023f95
CC
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.
11f66978 53 */
bd023f95
CC
54
55struct VncJobQueue {
56 QemuCond cond;
57 QemuMutex mutex;
58 QemuThread thread;
bd023f95
CC
59 bool exit;
60 QTAILQ_HEAD(, VncJob) jobs;
61};
62
63typedef struct VncJobQueue VncJobQueue;
64
65/*
66 * We use a single global queue, but most of the functions are
11f66978 67 * already reentrant, so we can easily add more than one encoding thread
bd023f95
CC
68 */
69static VncJobQueue *queue;
70
71static void vnc_lock_queue(VncJobQueue *queue)
72{
73 qemu_mutex_lock(&queue->mutex);
74}
75
76static void vnc_unlock_queue(VncJobQueue *queue)
77{
78 qemu_mutex_unlock(&queue->mutex);
79}
80
81VncJob *vnc_job_new(VncState *vs)
82{
fedf0d35 83 VncJob *job = g_new0(VncJob, 1);
bd023f95
CC
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
92int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
93{
fedf0d35 94 VncRectEntry *entry = g_new0(VncRectEntry, 1);
bd023f95
CC
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
107void vnc_job_push(VncJob *job)
108{
109 vnc_lock_queue(queue);
110 if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
7267c094 111 g_free(job);
bd023f95
CC
112 } else {
113 QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
114 qemu_cond_broadcast(&queue->cond);
115 }
116 vnc_unlock_queue(queue);
117}
118
119static 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
bd023f95
CC
131void vnc_jobs_join(VncState *vs)
132{
133 vnc_lock_queue(queue);
134 while (vnc_has_job_locked(vs)) {
135 qemu_cond_wait(&queue->cond, &queue->mutex);
136 }
137 vnc_unlock_queue(queue);
175b2a6e
CC
138 vnc_jobs_consume_buffer(vs);
139}
140
141void vnc_jobs_consume_buffer(VncState *vs)
142{
143 bool flush;
144
145 vnc_lock_output(vs);
146 if (vs->jobs_buffer.offset) {
04d2529d
DB
147 if (vs->ioc != NULL && buffer_empty(&vs->output)) {
148 if (vs->ioc_tag) {
149 g_source_remove(vs->ioc_tag);
150 }
151 vs->ioc_tag = qio_channel_add_watch(
152 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
d9034011
GH
153 }
154 buffer_move(&vs->output, &vs->jobs_buffer);
175b2a6e 155 }
04d2529d 156 flush = vs->ioc != NULL && vs->abort != true;
175b2a6e
CC
157 vnc_unlock_output(vs);
158
159 if (flush) {
160 vnc_flush(vs);
161 }
bd023f95
CC
162}
163
164/*
165 * Copy data for local use
166 */
167static void vnc_async_encoding_start(VncState *orig, VncState *local)
168{
2e0c90af 169 buffer_init(&local->output, "vnc-worker-output");
04d2529d
DB
170 local->sioc = NULL; /* Don't do any network work on this thread */
171 local->ioc = NULL; /* Don't do any network work on this thread */
2e0c90af 172
bd023f95
CC
173 local->vnc_encoding = orig->vnc_encoding;
174 local->features = orig->features;
bd023f95 175 local->vd = orig->vd;
7d964c9d 176 local->lossy_rect = orig->lossy_rect;
bd023f95 177 local->write_pixels = orig->write_pixels;
9f64916d
GH
178 local->client_pf = orig->client_pf;
179 local->client_be = orig->client_be;
bd023f95
CC
180 local->tight = orig->tight;
181 local->zlib = orig->zlib;
182 local->hextile = orig->hextile;
148954fa 183 local->zrle = orig->zrle;
bd023f95
CC
184}
185
186static void vnc_async_encoding_end(VncState *orig, VncState *local)
187{
188 orig->tight = local->tight;
189 orig->zlib = local->zlib;
190 orig->hextile = local->hextile;
148954fa 191 orig->zrle = local->zrle;
7d964c9d 192 orig->lossy_rect = local->lossy_rect;
bd023f95
CC
193}
194
195static int vnc_worker_thread_loop(VncJobQueue *queue)
196{
197 VncJob *job;
198 VncRectEntry *entry, *tmp;
2e0c90af 199 VncState vs = {};
bd023f95
CC
200 int n_rectangles;
201 int saved_offset;
bd023f95
CC
202
203 vnc_lock_queue(queue);
204 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
205 qemu_cond_wait(&queue->cond, &queue->mutex);
206 }
207 /* Here job can only be NULL if queue->exit is true */
208 job = QTAILQ_FIRST(&queue->jobs);
209 vnc_unlock_queue(queue);
210
211 if (queue->exit) {
212 return -1;
213 }
214
215 vnc_lock_output(job->vs);
04d2529d 216 if (job->vs->ioc == NULL || job->vs->abort == true) {
175b2a6e 217 vnc_unlock_output(job->vs);
bd023f95
CC
218 goto disconnected;
219 }
c3d6899c
PL
220 if (buffer_empty(&job->vs->output)) {
221 /*
222 * Looks like a NOP as it obviously moves no data. But it
223 * moves the empty buffer, so we don't have to malloc a new
224 * one for vs.output
225 */
226 buffer_move_empty(&vs.output, &job->vs->output);
227 }
bd023f95
CC
228 vnc_unlock_output(job->vs);
229
230 /* Make a local copy of vs and switch output buffers */
231 vnc_async_encoding_start(job->vs, &vs);
232
233 /* Start sending rectangles */
234 n_rectangles = 0;
235 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
236 vnc_write_u8(&vs, 0);
237 saved_offset = vs.output.offset;
238 vnc_write_u16(&vs, 0);
239
240 vnc_lock_display(job->vs->vd);
241 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
242 int n;
243
04d2529d 244 if (job->vs->ioc == NULL) {
bd023f95 245 vnc_unlock_display(job->vs->vd);
e3c1adf1
GA
246 /* Copy persistent encoding data */
247 vnc_async_encoding_end(job->vs, &vs);
bd023f95
CC
248 goto disconnected;
249 }
250
251 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
252 entry->rect.w, entry->rect.h);
253
254 if (n >= 0) {
255 n_rectangles += n;
256 }
7267c094 257 g_free(entry);
bd023f95
CC
258 }
259 vnc_unlock_display(job->vs->vd);
260
261 /* Put n_rectangles at the beginning of the message */
262 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
263 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
264
bd023f95 265 vnc_lock_output(job->vs);
04d2529d 266 if (job->vs->ioc != NULL) {
d9034011 267 buffer_move(&job->vs->jobs_buffer, &vs.output);
175b2a6e
CC
268 /* Copy persistent encoding data */
269 vnc_async_encoding_end(job->vs, &vs);
270
271 qemu_bh_schedule(job->vs->bh);
e3c1adf1 272 } else {
d9034011 273 buffer_reset(&vs.output);
e3c1adf1
GA
274 /* Copy persistent encoding data */
275 vnc_async_encoding_end(job->vs, &vs);
bd023f95 276 }
bd023f95
CC
277 vnc_unlock_output(job->vs);
278
175b2a6e 279disconnected:
bd023f95
CC
280 vnc_lock_queue(queue);
281 QTAILQ_REMOVE(&queue->jobs, job, next);
282 vnc_unlock_queue(queue);
283 qemu_cond_broadcast(&queue->cond);
7267c094 284 g_free(job);
bd023f95
CC
285 return 0;
286}
287
288static VncJobQueue *vnc_queue_init(void)
289{
fedf0d35 290 VncJobQueue *queue = g_new0(VncJobQueue, 1);
bd023f95
CC
291
292 qemu_cond_init(&queue->cond);
293 qemu_mutex_init(&queue->mutex);
294 QTAILQ_INIT(&queue->jobs);
295 return queue;
296}
297
298static void vnc_queue_clear(VncJobQueue *q)
299{
300 qemu_cond_destroy(&queue->cond);
301 qemu_mutex_destroy(&queue->mutex);
7267c094 302 g_free(q);
bd023f95
CC
303 queue = NULL; /* Unset global queue */
304}
305
306static void *vnc_worker_thread(void *arg)
307{
308 VncJobQueue *queue = arg;
309
b7680cb6 310 qemu_thread_get_self(&queue->thread);
bd023f95
CC
311
312 while (!vnc_worker_thread_loop(queue)) ;
313 vnc_queue_clear(queue);
314 return NULL;
315}
316
71a8cdec
BS
317static bool vnc_worker_thread_running(void)
318{
319 return queue; /* Check global queue */
320}
321
bd023f95
CC
322void vnc_start_worker_thread(void)
323{
324 VncJobQueue *q;
325
326 if (vnc_worker_thread_running())
327 return ;
328
329 q = vnc_queue_init();
4900116e
DDAG
330 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
331 QEMU_THREAD_DETACHED);
bd023f95
CC
332 queue = q; /* Set global queue */
333}