ia64/xen-unstable

changeset 17127:c8b58f82d893

ioemu: VNC updates should be sent only when requested.

Reading qemu code I realized that the qemu vnc server sometimes sends
framebuffer updates even if the client didn't request any. This is not
consistent with the RFB protocol spec and can break some clients.
This patch strictly enforces compliance with the RFB protocol making
sure framebuffer updates are sent only if the client requested one.
Doing so is more difficult than it seems because some framebuffer
pseudo-encoding updates cannot be discarded but must be sent anyway:
for example desktop resize and pixel format change messages. To solve
the problem I wrote a queue that stores those messages and sends them
as soon as the client asks for an update. Since 90% of the times the
queue is used to store only few elements, the queue allocates 10
elements at the beginning and every time it runs out of elements
allocates other 10 elements. This is should drastically limit the
number of malloc and free needed to maintain the queue. I did some
stress tests in the last couple of days and seems to work well.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Feb 26 14:50:45 2008 +0000 (2008-02-26)
parents 580bcd4c9642
children 6ef22eefc360
files tools/ioemu/vnc.c
line diff
     1.1 --- a/tools/ioemu/vnc.c	Tue Feb 26 14:47:23 2008 +0000
     1.2 +++ b/tools/ioemu/vnc.c	Tue Feb 26 14:50:45 2008 +0000
     1.3 @@ -137,6 +137,23 @@ enum {
     1.4  
     1.5  #endif /* CONFIG_VNC_TLS */
     1.6  
     1.7 +#define QUEUE_ALLOC_UNIT 10
     1.8 +
     1.9 +typedef struct _QueueItem
    1.10 +{
    1.11 +    int x, y, w, h;
    1.12 +    int32_t enc;
    1.13 +    struct _QueueItem *next;
    1.14 +} QueueItem;
    1.15 +
    1.16 +typedef struct _Queue
    1.17 +{
    1.18 +    QueueItem *queue_start;
    1.19 +    int start_count;
    1.20 +    QueueItem *queue_end;
    1.21 +    int end_count;
    1.22 +} Queue;
    1.23 +
    1.24  struct VncState
    1.25  {
    1.26      QEMUTimer *timer;
    1.27 @@ -152,6 +169,9 @@ struct VncState
    1.28      uint64_t *update_row;	/* outstanding updates */
    1.29      int has_update;		/* there's outstanding updates in the
    1.30  				 * visible area */
    1.31 +
    1.32 +    int update_requested;       /* the client requested an update */
    1.33 +
    1.34      uint8_t *old_data;
    1.35      int depth; /* internal VNC frame buffer byte per pixel */
    1.36      int has_resize;
    1.37 @@ -186,6 +206,9 @@ struct VncState
    1.38  
    1.39      Buffer output;
    1.40      Buffer input;
    1.41 +    
    1.42 +    Queue upqueue;
    1.43 +
    1.44      kbd_layout_t *kbd_layout;
    1.45      /* current output mode information */
    1.46      VncWritePixels *write_pixels;
    1.47 @@ -248,6 +271,11 @@ static void _vnc_update_client(void *opa
    1.48  static void vnc_update_client(void *opaque);
    1.49  static void vnc_client_read(void *opaque);
    1.50  static void framebuffer_set_updated(VncState *vs, int x, int y, int w, int h);
    1.51 +static void pixel_format_message (VncState *vs);
    1.52 +static void enqueue_framebuffer_update(VncState *vs, int x, int y, int w, int h, int32_t encoding);
    1.53 +static void dequeue_framebuffer_update(VncState *vs);
    1.54 +static int is_empty_queue(VncState *vs);
    1.55 +static void free_queue(VncState *vs);
    1.56  
    1.57  #if 0
    1.58  static inline void vnc_set_bit(uint32_t *d, int k)
    1.59 @@ -370,13 +398,18 @@ static void vnc_dpy_resize(DisplayState 
    1.60      ds->height = h;
    1.61      ds->linesize = w * vs->depth;
    1.62      if (vs->csock != -1 && vs->has_resize && size_changed) {
    1.63 -	vnc_write_u8(vs, 0);  /* msg id */
    1.64 -	vnc_write_u8(vs, 0);
    1.65 -	vnc_write_u16(vs, 1); /* number of rects */
    1.66 -	vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
    1.67 -	vnc_flush(vs);
    1.68 -	vs->width = ds->width;
    1.69 -	vs->height = ds->height;
    1.70 +        vs->width = ds->width;
    1.71 +        vs->height = ds->height;
    1.72 +        if (vs->update_requested) {
    1.73 +	    vnc_write_u8(vs, 0);  /* msg id */
    1.74 +	    vnc_write_u8(vs, 0);
    1.75 +	    vnc_write_u16(vs, 1); /* number of rects */
    1.76 +	    vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
    1.77 +	    vnc_flush(vs);
    1.78 +            vs->update_requested--;
    1.79 +        } else {
    1.80 +            enqueue_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
    1.81 +        }
    1.82      }
    1.83      vs->dirty_pixel_shift = 0;
    1.84      for (o = DIRTY_PIXEL_BITS; o < ds->width; o *= 2)
    1.85 @@ -553,7 +586,8 @@ static void vnc_copy(DisplayState *ds, i
    1.86          return;
    1.87      }
    1.88  
    1.89 -    if (src_x < vs->visible_x || src_y < vs->visible_y ||
    1.90 +    if (!vs->update_requested ||
    1.91 +        src_x < vs->visible_x || src_y < vs->visible_y ||
    1.92  	dst_x < vs->visible_x || dst_y < vs->visible_y ||
    1.93  	(src_x + w) > (vs->visible_x + vs->visible_w) ||
    1.94  	(src_y + h) > (vs->visible_y + vs->visible_h) ||
    1.95 @@ -592,6 +626,7 @@ static void vnc_copy(DisplayState *ds, i
    1.96  	vnc_write_u16(vs, src_x);
    1.97  	vnc_write_u16(vs, src_y);
    1.98  	vnc_flush(vs);
    1.99 +        vs->update_requested--;
   1.100      } else
   1.101  	framebuffer_set_updated(vs, dst_x, dst_y, w, h);
   1.102  }
   1.103 @@ -624,8 +659,21 @@ static void _vnc_update_client(void *opa
   1.104      int maxx, maxy;
   1.105      int tile_bytes = vs->depth * DP2X(vs, 1);
   1.106  
   1.107 -    if (vs->csock == -1)
   1.108 +    if (!vs->update_requested || vs->csock == -1)
   1.109  	return;
   1.110 +    while (!is_empty_queue(vs) && vs->update_requested) {
   1.111 +        int enc = vs->upqueue.queue_end->enc; 
   1.112 +        dequeue_framebuffer_update(vs);
   1.113 +        switch (enc) {
   1.114 +            case 0x574D5669:
   1.115 +                pixel_format_message(vs);
   1.116 +                break;
   1.117 +            default:
   1.118 +                break;
   1.119 +        }
   1.120 +        vs->update_requested--;
   1.121 +    }
   1.122 +    if (!vs->update_requested) return;
   1.123  
   1.124      now = qemu_get_clock(rt_clock);
   1.125  
   1.126 @@ -717,8 +765,11 @@ static void _vnc_update_client(void *opa
   1.127      vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
   1.128      vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
   1.129  
   1.130 -    if (n_rectangles == 0)
   1.131 +    if (n_rectangles == 0) {
   1.132 +        vs->output.offset = saved_offset - 2;
   1.133  	goto backoff;
   1.134 +    } else
   1.135 +        vs->update_requested--;
   1.136  
   1.137      vs->has_update = 0;
   1.138      vnc_flush(vs);
   1.139 @@ -735,7 +786,8 @@ static void _vnc_update_client(void *opa
   1.140      vs->timer_interval += VNC_REFRESH_INTERVAL_INC;
   1.141      if (vs->timer_interval > VNC_REFRESH_INTERVAL_MAX) {
   1.142  	vs->timer_interval = VNC_REFRESH_INTERVAL_MAX;
   1.143 -	if (now - vs->last_update_time >= VNC_MAX_UPDATE_INTERVAL) {
   1.144 +	if (now - vs->last_update_time >= VNC_MAX_UPDATE_INTERVAL &&
   1.145 +            vs->update_requested) {
   1.146  	    /* Send a null update.  If the client is no longer
   1.147  	       interested (e.g. minimised) it'll ignore this, and we
   1.148  	       can stop scanning the buffer until it sends another
   1.149 @@ -752,6 +804,7 @@ static void _vnc_update_client(void *opa
   1.150  	    send_framebuffer_update(vs, 0, 0, 1, 1);
   1.151  	    vnc_flush(vs);
   1.152  	    vs->last_update_time = now;
   1.153 +            vs->update_requested--;
   1.154  	    return;
   1.155  	}
   1.156      }
   1.157 @@ -821,6 +874,88 @@ static void buffer_append(Buffer *buffer
   1.158      buffer->offset += len;
   1.159  }
   1.160  
   1.161 +static void enqueue_framebuffer_update(VncState *vs, int x, int y, int w, int h,
   1.162 +                                   int32_t encoding)
   1.163 +{
   1.164 +    Queue *q = &vs->upqueue; 
   1.165 +    if (q->queue_end != NULL) {
   1.166 +        if (q->queue_end != q->queue_start || q->start_count != q->end_count) {
   1.167 +            if (q->queue_end->next == NULL) {
   1.168 +                q->queue_end->next = (QueueItem *) qemu_mallocz (sizeof(QueueItem) * QUEUE_ALLOC_UNIT);
   1.169 +                q->end_count = QUEUE_ALLOC_UNIT;
   1.170 +            }
   1.171 +            q->queue_end = q->queue_end->next;
   1.172 +        }
   1.173 +    } else {
   1.174 +        q->queue_end = (QueueItem *) qemu_mallocz (sizeof(QueueItem) * QUEUE_ALLOC_UNIT);
   1.175 +        q->queue_start = q->queue_end;
   1.176 +        q->start_count = QUEUE_ALLOC_UNIT;
   1.177 +        q->end_count = QUEUE_ALLOC_UNIT;
   1.178 +    }
   1.179 +    q->end_count--;
   1.180 +
   1.181 +    q->queue_end->x = x;
   1.182 +    q->queue_end->y = y;
   1.183 +    q->queue_end->w = w;
   1.184 +    q->queue_end->h = h;
   1.185 +    q->queue_end->enc = encoding;
   1.186 +    q->queue_end->next = (q->end_count > 0) ? (q->queue_end + 1) : NULL;
   1.187 +}
   1.188 +
   1.189 +static void dequeue_framebuffer_update(VncState *vs)
   1.190 +{
   1.191 +    Queue *q = &vs->upqueue;
   1.192 +    if (q->queue_start == NULL || 
   1.193 +            (q->queue_end == q->queue_start && q->start_count == q->end_count))
   1.194 +        return;
   1.195 +
   1.196 +    vnc_write_u8(vs, 0);
   1.197 +    vnc_write_u8(vs, 0);
   1.198 +    vnc_write_u16(vs, 1);
   1.199 +    vnc_framebuffer_update(vs, q->queue_start->x, q->queue_start->y,
   1.200 +            q->queue_start->w, q->queue_start->h, q->queue_start->enc);
   1.201 +
   1.202 +    q->start_count--;
   1.203 +    if (q->queue_end != q->queue_start) {
   1.204 +        if (!q->start_count) {
   1.205 +            QueueItem *i = q->queue_start;
   1.206 +            q->queue_start = q->queue_start->next;
   1.207 +            q->start_count = QUEUE_ALLOC_UNIT;
   1.208 +            free (i - QUEUE_ALLOC_UNIT + 1);
   1.209 +        } else
   1.210 +            q->queue_start = q->queue_start->next;
   1.211 +    } else {
   1.212 +        q->queue_end = q->queue_end - QUEUE_ALLOC_UNIT + q->end_count + 1;
   1.213 +        q->queue_start = q->queue_end;
   1.214 +        q->end_count = QUEUE_ALLOC_UNIT;
   1.215 +        q->start_count = QUEUE_ALLOC_UNIT;
   1.216 +    }
   1.217 +}
   1.218 +
   1.219 +static int is_empty_queue(VncState *vs)
   1.220 +{
   1.221 +    Queue *q = &vs->upqueue;
   1.222 +    if (q->queue_end == NULL) return 1;
   1.223 +    if (q->queue_end == q->queue_start && q->start_count == q->end_count) return 1;
   1.224 +    return 0;
   1.225 +}
   1.226 +
   1.227 +static void free_queue(VncState *vs)
   1.228 +{
   1.229 +    Queue *q = &vs->upqueue;
   1.230 +    while (q->queue_start != NULL) {
   1.231 +        QueueItem *i;
   1.232 +        q->queue_start = q->queue_start + q->start_count - 1;
   1.233 +        i = q->queue_start;
   1.234 +        q->queue_start = q->queue_start->next;
   1.235 +        free(i - QUEUE_ALLOC_UNIT + 1);
   1.236 +        q->start_count = QUEUE_ALLOC_UNIT;
   1.237 +    }
   1.238 +    q->queue_end = NULL;
   1.239 +    q->start_count = 0;
   1.240 +    q->end_count = 0;
   1.241 +}
   1.242 +
   1.243  static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
   1.244  {
   1.245      if (ret == 0 || ret == -1) {
   1.246 @@ -833,6 +968,8 @@ static int vnc_client_io_error(VncState 
   1.247  	vs->csock = -1;
   1.248  	buffer_reset(&vs->input);
   1.249  	buffer_reset(&vs->output);
   1.250 +        free_queue(vs);
   1.251 +        vs->update_requested = 0;
   1.252  #if CONFIG_VNC_TLS
   1.253  	if (vs->tls_session) {
   1.254  	    gnutls_deinit(vs->tls_session);
   1.255 @@ -1044,12 +1181,18 @@ static void client_cut_text(VncState *vs
   1.256  static void check_pointer_type_change(VncState *vs, int absolute)
   1.257  {
   1.258      if (vs->has_pointer_type_change && vs->absolute != absolute) {
   1.259 -	vnc_write_u8(vs, 0);
   1.260 -	vnc_write_u8(vs, 0);
   1.261 -	vnc_write_u16(vs, 1);
   1.262 -	vnc_framebuffer_update(vs, absolute, 0,
   1.263 +        if (vs->update_requested) {
   1.264 +	    vnc_write_u8(vs, 0);
   1.265 +	    vnc_write_u8(vs, 0);
   1.266 +	    vnc_write_u16(vs, 1);
   1.267 +	    vnc_framebuffer_update(vs, absolute, 0,
   1.268  			       vs->ds->width, vs->ds->height, -257);
   1.269 -	vnc_flush(vs);
   1.270 +	    vnc_flush(vs);
   1.271 +            vs->update_requested--;
   1.272 +        } else {
   1.273 +            enqueue_framebuffer_update(vs, absolute, 0,
   1.274 +                               vs->ds->width, vs->ds->height, -257);
   1.275 +        }
   1.276      }
   1.277      vs->absolute = absolute;
   1.278  }
   1.279 @@ -1316,6 +1459,7 @@ static void framebuffer_update_request(V
   1.280      vs->visible_w = w;
   1.281      vs->visible_h = h;
   1.282  
   1.283 +    vs->update_requested++;
   1.284      qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
   1.285  }
   1.286  
   1.287 @@ -1536,12 +1680,17 @@ static void vnc_dpy_colourdepth(DisplayS
   1.288          vnc_client_error(vs);
   1.289      } else if (vs->csock != -1 && vs->has_WMVi) {
   1.290          /* Sending a WMVi message to notify the client*/
   1.291 -        vnc_write_u8(vs, 0);  /* msg id */
   1.292 -        vnc_write_u8(vs, 0);
   1.293 -        vnc_write_u16(vs, 1); /* number of rects */
   1.294 -        vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, 0x574D5669);
   1.295 -        pixel_format_message(vs);
   1.296 -        vnc_flush(vs);
   1.297 +        if (vs->update_requested) {
   1.298 +            vnc_write_u8(vs, 0);  /* msg id */
   1.299 +            vnc_write_u8(vs, 0);
   1.300 +            vnc_write_u16(vs, 1); /* number of rects */
   1.301 +            vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, 0x574D5669);
   1.302 +            pixel_format_message(vs);
   1.303 +            vnc_flush(vs);
   1.304 +            vs->update_requested--;
   1.305 +        } else {
   1.306 +            enqueue_framebuffer_update(vs, 0, 0, ds->width, ds->height, 0x574D5669);
   1.307 +        }
   1.308      } else {
   1.309          if (vs->pix_bpp == 4 && vs->depth == 4 &&
   1.310              host_big_endian_flag == vs->pix_big_endian &&
   1.311 @@ -2291,6 +2440,7 @@ static void vnc_listen_read(void *opaque
   1.312  	framebuffer_set_updated(vs, 0, 0, vs->ds->width, vs->ds->height);
   1.313  	vs->has_resize = 0;
   1.314  	vs->has_hextile = 0;
   1.315 +        vs->update_requested = 0;
   1.316  	vs->ds->dpy_copy = NULL;
   1.317  	vnc_timer_init(vs);
   1.318      }
   1.319 @@ -2413,6 +2563,8 @@ void vnc_display_close(DisplayState *ds)
   1.320  	vs->csock = -1;
   1.321  	buffer_reset(&vs->input);
   1.322  	buffer_reset(&vs->output);
   1.323 +        free_queue(vs);
   1.324 +        vs->update_requested = 0;
   1.325  #if CONFIG_VNC_TLS
   1.326  	if (vs->tls_session) {
   1.327  	    gnutls_deinit(vs->tls_session);