]> Git Repo - qemu.git/blob - migration/qemu-file.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20141216-1' into staging
[qemu.git] / migration / qemu-file.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu-common.h"
25 #include "qemu/iov.h"
26 #include "qemu/sockets.h"
27 #include "block/coroutine.h"
28 #include "migration/migration.h"
29 #include "migration/qemu-file.h"
30 #include "migration/qemu-file-internal.h"
31 #include "trace.h"
32
33 bool qemu_file_mode_is_not_valid(const char *mode)
34 {
35     if (mode == NULL ||
36         (mode[0] != 'r' && mode[0] != 'w') ||
37         mode[1] != 'b' || mode[2] != 0) {
38         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
39         return true;
40     }
41
42     return false;
43 }
44
45 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
46 {
47     QEMUFile *f;
48
49     f = g_malloc0(sizeof(QEMUFile));
50
51     f->opaque = opaque;
52     f->ops = ops;
53     return f;
54 }
55
56 /*
57  * Get last error for stream f
58  *
59  * Return negative error value if there has been an error on previous
60  * operations, return 0 if no error happened.
61  *
62  */
63 int qemu_file_get_error(QEMUFile *f)
64 {
65     return f->last_error;
66 }
67
68 void qemu_file_set_error(QEMUFile *f, int ret)
69 {
70     if (f->last_error == 0) {
71         f->last_error = ret;
72     }
73 }
74
75 bool qemu_file_is_writable(QEMUFile *f)
76 {
77     return f->ops->writev_buffer || f->ops->put_buffer;
78 }
79
80 /**
81  * Flushes QEMUFile buffer
82  *
83  * If there is writev_buffer QEMUFileOps it uses it otherwise uses
84  * put_buffer ops.
85  */
86 void qemu_fflush(QEMUFile *f)
87 {
88     ssize_t ret = 0;
89
90     if (!qemu_file_is_writable(f)) {
91         return;
92     }
93
94     if (f->ops->writev_buffer) {
95         if (f->iovcnt > 0) {
96             ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
97         }
98     } else {
99         if (f->buf_index > 0) {
100             ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
101         }
102     }
103     if (ret >= 0) {
104         f->pos += ret;
105     }
106     f->buf_index = 0;
107     f->iovcnt = 0;
108     if (ret < 0) {
109         qemu_file_set_error(f, ret);
110     }
111 }
112
113 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
114 {
115     int ret = 0;
116
117     if (f->ops->before_ram_iterate) {
118         ret = f->ops->before_ram_iterate(f, f->opaque, flags);
119         if (ret < 0) {
120             qemu_file_set_error(f, ret);
121         }
122     }
123 }
124
125 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
126 {
127     int ret = 0;
128
129     if (f->ops->after_ram_iterate) {
130         ret = f->ops->after_ram_iterate(f, f->opaque, flags);
131         if (ret < 0) {
132             qemu_file_set_error(f, ret);
133         }
134     }
135 }
136
137 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
138 {
139     int ret = -EINVAL;
140
141     if (f->ops->hook_ram_load) {
142         ret = f->ops->hook_ram_load(f, f->opaque, flags);
143         if (ret < 0) {
144             qemu_file_set_error(f, ret);
145         }
146     } else {
147         qemu_file_set_error(f, ret);
148     }
149 }
150
151 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
152                          ram_addr_t offset, size_t size, int *bytes_sent)
153 {
154     if (f->ops->save_page) {
155         int ret = f->ops->save_page(f, f->opaque, block_offset,
156                                     offset, size, bytes_sent);
157
158         if (ret != RAM_SAVE_CONTROL_DELAYED) {
159             if (bytes_sent && *bytes_sent > 0) {
160                 qemu_update_position(f, *bytes_sent);
161             } else if (ret < 0) {
162                 qemu_file_set_error(f, ret);
163             }
164         }
165
166         return ret;
167     }
168
169     return RAM_SAVE_CONTROL_NOT_SUPP;
170 }
171
172 /*
173  * Attempt to fill the buffer from the underlying file
174  * Returns the number of bytes read, or negative value for an error.
175  *
176  * Note that it can return a partially full buffer even in a not error/not EOF
177  * case if the underlying file descriptor gives a short read, and that can
178  * happen even on a blocking fd.
179  */
180 static ssize_t qemu_fill_buffer(QEMUFile *f)
181 {
182     int len;
183     int pending;
184
185     assert(!qemu_file_is_writable(f));
186
187     pending = f->buf_size - f->buf_index;
188     if (pending > 0) {
189         memmove(f->buf, f->buf + f->buf_index, pending);
190     }
191     f->buf_index = 0;
192     f->buf_size = pending;
193
194     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
195                         IO_BUF_SIZE - pending);
196     if (len > 0) {
197         f->buf_size += len;
198         f->pos += len;
199     } else if (len == 0) {
200         qemu_file_set_error(f, -EIO);
201     } else if (len != -EAGAIN) {
202         qemu_file_set_error(f, len);
203     }
204
205     return len;
206 }
207
208 int qemu_get_fd(QEMUFile *f)
209 {
210     if (f->ops->get_fd) {
211         return f->ops->get_fd(f->opaque);
212     }
213     return -1;
214 }
215
216 void qemu_update_position(QEMUFile *f, size_t size)
217 {
218     f->pos += size;
219 }
220
221 /** Closes the file
222  *
223  * Returns negative error value if any error happened on previous operations or
224  * while closing the file. Returns 0 or positive number on success.
225  *
226  * The meaning of return value on success depends on the specific backend
227  * being used.
228  */
229 int qemu_fclose(QEMUFile *f)
230 {
231     int ret;
232     qemu_fflush(f);
233     ret = qemu_file_get_error(f);
234
235     if (f->ops->close) {
236         int ret2 = f->ops->close(f->opaque);
237         if (ret >= 0) {
238             ret = ret2;
239         }
240     }
241     /* If any error was spotted before closing, we should report it
242      * instead of the close() return value.
243      */
244     if (f->last_error) {
245         ret = f->last_error;
246     }
247     g_free(f);
248     trace_qemu_file_fclose();
249     return ret;
250 }
251
252 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
253 {
254     /* check for adjacent buffer and coalesce them */
255     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
256         f->iov[f->iovcnt - 1].iov_len) {
257         f->iov[f->iovcnt - 1].iov_len += size;
258     } else {
259         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
260         f->iov[f->iovcnt++].iov_len = size;
261     }
262
263     if (f->iovcnt >= MAX_IOV_SIZE) {
264         qemu_fflush(f);
265     }
266 }
267
268 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
269 {
270     if (!f->ops->writev_buffer) {
271         qemu_put_buffer(f, buf, size);
272         return;
273     }
274
275     if (f->last_error) {
276         return;
277     }
278
279     f->bytes_xfer += size;
280     add_to_iovec(f, buf, size);
281 }
282
283 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
284 {
285     int l;
286
287     if (f->last_error) {
288         return;
289     }
290
291     while (size > 0) {
292         l = IO_BUF_SIZE - f->buf_index;
293         if (l > size) {
294             l = size;
295         }
296         memcpy(f->buf + f->buf_index, buf, l);
297         f->bytes_xfer += l;
298         if (f->ops->writev_buffer) {
299             add_to_iovec(f, f->buf + f->buf_index, l);
300         }
301         f->buf_index += l;
302         if (f->buf_index == IO_BUF_SIZE) {
303             qemu_fflush(f);
304         }
305         if (qemu_file_get_error(f)) {
306             break;
307         }
308         buf += l;
309         size -= l;
310     }
311 }
312
313 void qemu_put_byte(QEMUFile *f, int v)
314 {
315     if (f->last_error) {
316         return;
317     }
318
319     f->buf[f->buf_index] = v;
320     f->bytes_xfer++;
321     if (f->ops->writev_buffer) {
322         add_to_iovec(f, f->buf + f->buf_index, 1);
323     }
324     f->buf_index++;
325     if (f->buf_index == IO_BUF_SIZE) {
326         qemu_fflush(f);
327     }
328 }
329
330 void qemu_file_skip(QEMUFile *f, int size)
331 {
332     if (f->buf_index + size <= f->buf_size) {
333         f->buf_index += size;
334     }
335 }
336
337 /*
338  * Read 'size' bytes from file (at 'offset') into buf without moving the
339  * pointer.
340  *
341  * It will return size bytes unless there was an error, in which case it will
342  * return as many as it managed to read (assuming blocking fd's which
343  * all current QEMUFile are)
344  */
345 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
346 {
347     int pending;
348     int index;
349
350     assert(!qemu_file_is_writable(f));
351     assert(offset < IO_BUF_SIZE);
352     assert(size <= IO_BUF_SIZE - offset);
353
354     /* The 1st byte to read from */
355     index = f->buf_index + offset;
356     /* The number of available bytes starting at index */
357     pending = f->buf_size - index;
358
359     /*
360      * qemu_fill_buffer might return just a few bytes, even when there isn't
361      * an error, so loop collecting them until we get enough.
362      */
363     while (pending < size) {
364         int received = qemu_fill_buffer(f);
365
366         if (received <= 0) {
367             break;
368         }
369
370         index = f->buf_index + offset;
371         pending = f->buf_size - index;
372     }
373
374     if (pending <= 0) {
375         return 0;
376     }
377     if (size > pending) {
378         size = pending;
379     }
380
381     memcpy(buf, f->buf + index, size);
382     return size;
383 }
384
385 /*
386  * Read 'size' bytes of data from the file into buf.
387  * 'size' can be larger than the internal buffer.
388  *
389  * It will return size bytes unless there was an error, in which case it will
390  * return as many as it managed to read (assuming blocking fd's which
391  * all current QEMUFile are)
392  */
393 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
394 {
395     int pending = size;
396     int done = 0;
397
398     while (pending > 0) {
399         int res;
400
401         res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
402         if (res == 0) {
403             return done;
404         }
405         qemu_file_skip(f, res);
406         buf += res;
407         pending -= res;
408         done += res;
409     }
410     return done;
411 }
412
413 /*
414  * Peeks a single byte from the buffer; this isn't guaranteed to work if
415  * offset leaves a gap after the previous read/peeked data.
416  */
417 int qemu_peek_byte(QEMUFile *f, int offset)
418 {
419     int index = f->buf_index + offset;
420
421     assert(!qemu_file_is_writable(f));
422     assert(offset < IO_BUF_SIZE);
423
424     if (index >= f->buf_size) {
425         qemu_fill_buffer(f);
426         index = f->buf_index + offset;
427         if (index >= f->buf_size) {
428             return 0;
429         }
430     }
431     return f->buf[index];
432 }
433
434 int qemu_get_byte(QEMUFile *f)
435 {
436     int result;
437
438     result = qemu_peek_byte(f, 0);
439     qemu_file_skip(f, 1);
440     return result;
441 }
442
443 int64_t qemu_ftell(QEMUFile *f)
444 {
445     qemu_fflush(f);
446     return f->pos;
447 }
448
449 int qemu_file_rate_limit(QEMUFile *f)
450 {
451     if (qemu_file_get_error(f)) {
452         return 1;
453     }
454     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
455         return 1;
456     }
457     return 0;
458 }
459
460 int64_t qemu_file_get_rate_limit(QEMUFile *f)
461 {
462     return f->xfer_limit;
463 }
464
465 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
466 {
467     f->xfer_limit = limit;
468 }
469
470 void qemu_file_reset_rate_limit(QEMUFile *f)
471 {
472     f->bytes_xfer = 0;
473 }
474
475 void qemu_put_be16(QEMUFile *f, unsigned int v)
476 {
477     qemu_put_byte(f, v >> 8);
478     qemu_put_byte(f, v);
479 }
480
481 void qemu_put_be32(QEMUFile *f, unsigned int v)
482 {
483     qemu_put_byte(f, v >> 24);
484     qemu_put_byte(f, v >> 16);
485     qemu_put_byte(f, v >> 8);
486     qemu_put_byte(f, v);
487 }
488
489 void qemu_put_be64(QEMUFile *f, uint64_t v)
490 {
491     qemu_put_be32(f, v >> 32);
492     qemu_put_be32(f, v);
493 }
494
495 unsigned int qemu_get_be16(QEMUFile *f)
496 {
497     unsigned int v;
498     v = qemu_get_byte(f) << 8;
499     v |= qemu_get_byte(f);
500     return v;
501 }
502
503 unsigned int qemu_get_be32(QEMUFile *f)
504 {
505     unsigned int v;
506     v = qemu_get_byte(f) << 24;
507     v |= qemu_get_byte(f) << 16;
508     v |= qemu_get_byte(f) << 8;
509     v |= qemu_get_byte(f);
510     return v;
511 }
512
513 uint64_t qemu_get_be64(QEMUFile *f)
514 {
515     uint64_t v;
516     v = (uint64_t)qemu_get_be32(f) << 32;
517     v |= qemu_get_be32(f);
518     return v;
519 }
This page took 0.051144 seconds and 4 git commands to generate.