Add qemu 2.4.0
[kvmfornfv.git] / qemu / migration / qemu-file-buf.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2014 IBM Corp.
6  *
7  * Authors:
8  *  Stefan Berger <stefanb@linux.vnet.ibm.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 #include "qemu-common.h"
29 #include "qemu/error-report.h"
30 #include "qemu/iov.h"
31 #include "qemu/sockets.h"
32 #include "block/coroutine.h"
33 #include "migration/migration.h"
34 #include "migration/qemu-file.h"
35 #include "migration/qemu-file-internal.h"
36 #include "trace.h"
37
38 #define QSB_CHUNK_SIZE      (1 << 10)
39 #define QSB_MAX_CHUNK_SIZE  (16 * QSB_CHUNK_SIZE)
40
41 /**
42  * Create a QEMUSizedBuffer
43  * This type of buffer uses scatter-gather lists internally and
44  * can grow to any size. Any data array in the scatter-gather list
45  * can hold different amount of bytes.
46  *
47  * @buffer: Optional buffer to copy into the QSB
48  * @len: size of initial buffer; if @buffer is given, buffer must
49  *       hold at least len bytes
50  *
51  * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
52  */
53 QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
54 {
55     QEMUSizedBuffer *qsb;
56     size_t alloc_len, num_chunks, i, to_copy;
57     size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
58                         ? QSB_MAX_CHUNK_SIZE
59                         : QSB_CHUNK_SIZE;
60
61     num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
62     alloc_len = num_chunks * chunk_size;
63
64     qsb = g_try_new0(QEMUSizedBuffer, 1);
65     if (!qsb) {
66         return NULL;
67     }
68
69     qsb->iov = g_try_new0(struct iovec, num_chunks);
70     if (!qsb->iov) {
71         g_free(qsb);
72         return NULL;
73     }
74
75     qsb->n_iov = num_chunks;
76
77     for (i = 0; i < num_chunks; i++) {
78         qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
79         if (!qsb->iov[i].iov_base) {
80             /* qsb_free is safe since g_free can cope with NULL */
81             qsb_free(qsb);
82             return NULL;
83         }
84
85         qsb->iov[i].iov_len = chunk_size;
86         if (buffer) {
87             to_copy = (len - qsb->used) > chunk_size
88                       ? chunk_size : (len - qsb->used);
89             memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
90             qsb->used += to_copy;
91         }
92     }
93
94     qsb->size = alloc_len;
95
96     return qsb;
97 }
98
99 /**
100  * Free the QEMUSizedBuffer
101  *
102  * @qsb: The QEMUSizedBuffer to free
103  */
104 void qsb_free(QEMUSizedBuffer *qsb)
105 {
106     size_t i;
107
108     if (!qsb) {
109         return;
110     }
111
112     for (i = 0; i < qsb->n_iov; i++) {
113         g_free(qsb->iov[i].iov_base);
114     }
115     g_free(qsb->iov);
116     g_free(qsb);
117 }
118
119 /**
120  * Get the number of used bytes in the QEMUSizedBuffer
121  *
122  * @qsb: A QEMUSizedBuffer
123  *
124  * Returns the number of bytes currently used in this buffer
125  */
126 size_t qsb_get_length(const QEMUSizedBuffer *qsb)
127 {
128     return qsb->used;
129 }
130
131 /**
132  * Set the length of the buffer; the primary usage of this
133  * function is to truncate the number of used bytes in the buffer.
134  * The size will not be extended beyond the current number of
135  * allocated bytes in the QEMUSizedBuffer.
136  *
137  * @qsb: A QEMUSizedBuffer
138  * @new_len: The new length of bytes in the buffer
139  *
140  * Returns the number of bytes the buffer was truncated or extended
141  * to.
142  */
143 size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
144 {
145     if (new_len <= qsb->size) {
146         qsb->used = new_len;
147     } else {
148         qsb->used = qsb->size;
149     }
150     return qsb->used;
151 }
152
153 /**
154  * Get the iovec that holds the data for a given position @pos.
155  *
156  * @qsb: A QEMUSizedBuffer
157  * @pos: The index of a byte in the buffer
158  * @d_off: Pointer to an offset that this function will indicate
159  *         at what position within the returned iovec the byte
160  *         is to be found
161  *
162  * Returns the index of the iovec that holds the byte at the given
163  * index @pos in the byte stream; a negative number if the iovec
164  * for the given position @pos does not exist.
165  */
166 static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
167                              off_t pos, off_t *d_off)
168 {
169     ssize_t i;
170     off_t curr = 0;
171
172     if (pos > qsb->used) {
173         return -1;
174     }
175
176     for (i = 0; i < qsb->n_iov; i++) {
177         if (curr + qsb->iov[i].iov_len > pos) {
178             *d_off = pos - curr;
179             return i;
180         }
181         curr += qsb->iov[i].iov_len;
182     }
183     return -1;
184 }
185
186 /*
187  * Convert the QEMUSizedBuffer into a flat buffer.
188  *
189  * Note: If at all possible, try to avoid this function since it
190  *       may unnecessarily copy memory around.
191  *
192  * @qsb: pointer to QEMUSizedBuffer
193  * @start: offset to start at
194  * @count: number of bytes to copy
195  * @buf: a pointer to a buffer to write into (at least @count bytes)
196  *
197  * Returns the number of bytes copied into the output buffer
198  */
199 ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
200                        size_t count, uint8_t *buffer)
201 {
202     const struct iovec *iov;
203     size_t to_copy, all_copy;
204     ssize_t index;
205     off_t s_off;
206     off_t d_off = 0;
207     char *s;
208
209     if (start > qsb->used) {
210         return 0;
211     }
212
213     all_copy = qsb->used - start;
214     if (all_copy > count) {
215         all_copy = count;
216     } else {
217         count = all_copy;
218     }
219
220     index = qsb_get_iovec(qsb, start, &s_off);
221     if (index < 0) {
222         return 0;
223     }
224
225     while (all_copy > 0) {
226         iov = &qsb->iov[index];
227
228         s = iov->iov_base;
229
230         to_copy = iov->iov_len - s_off;
231         if (to_copy > all_copy) {
232             to_copy = all_copy;
233         }
234         memcpy(&buffer[d_off], &s[s_off], to_copy);
235
236         d_off += to_copy;
237         all_copy -= to_copy;
238
239         s_off = 0;
240         index++;
241     }
242
243     return count;
244 }
245
246 /**
247  * Grow the QEMUSizedBuffer to the given size and allocate
248  * memory for it.
249  *
250  * @qsb: A QEMUSizedBuffer
251  * @new_size: The new size of the buffer
252  *
253  * Return:
254  *    a negative error code in case of memory allocation failure
255  * or
256  *    the new size of the buffer. The returned size may be greater or equal
257  *    to @new_size.
258  */
259 static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
260 {
261     size_t needed_chunks, i;
262
263     if (qsb->size < new_size) {
264         struct iovec *new_iov;
265         size_t size_diff = new_size - qsb->size;
266         size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
267                              ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
268
269         needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
270
271         new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
272         if (new_iov == NULL) {
273             return -ENOMEM;
274         }
275
276         /* Allocate new chunks as needed into new_iov */
277         for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
278             new_iov[i].iov_base = g_try_malloc0(chunk_size);
279             new_iov[i].iov_len = chunk_size;
280             if (!new_iov[i].iov_base) {
281                 size_t j;
282
283                 /* Free previously allocated new chunks */
284                 for (j = qsb->n_iov; j < i; j++) {
285                     g_free(new_iov[j].iov_base);
286                 }
287                 g_free(new_iov);
288
289                 return -ENOMEM;
290             }
291         }
292
293         /*
294          * Now we can't get any allocation errors, copy over to new iov
295          * and switch.
296          */
297         for (i = 0; i < qsb->n_iov; i++) {
298             new_iov[i] = qsb->iov[i];
299         }
300
301         qsb->n_iov += needed_chunks;
302         g_free(qsb->iov);
303         qsb->iov = new_iov;
304         qsb->size += (needed_chunks * chunk_size);
305     }
306
307     return qsb->size;
308 }
309
310 /**
311  * Write into the QEMUSizedBuffer at a given position and a given
312  * number of bytes. This function will automatically grow the
313  * QEMUSizedBuffer.
314  *
315  * @qsb: A QEMUSizedBuffer
316  * @source: A byte array to copy data from
317  * @pos: The position within the @qsb to write data to
318  * @size: The number of bytes to copy into the @qsb
319  *
320  * Returns @size or a negative error code in case of memory allocation failure,
321  *           or with an invalid 'pos'
322  */
323 ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
324                      off_t pos, size_t count)
325 {
326     ssize_t rc = qsb_grow(qsb, pos + count);
327     size_t to_copy;
328     size_t all_copy = count;
329     const struct iovec *iov;
330     ssize_t index;
331     char *dest;
332     off_t d_off, s_off = 0;
333
334     if (rc < 0) {
335         return rc;
336     }
337
338     if (pos + count > qsb->used) {
339         qsb->used = pos + count;
340     }
341
342     index = qsb_get_iovec(qsb, pos, &d_off);
343     if (index < 0) {
344         return -EINVAL;
345     }
346
347     while (all_copy > 0) {
348         iov = &qsb->iov[index];
349
350         dest = iov->iov_base;
351
352         to_copy = iov->iov_len - d_off;
353         if (to_copy > all_copy) {
354             to_copy = all_copy;
355         }
356
357         memcpy(&dest[d_off], &source[s_off], to_copy);
358
359         s_off += to_copy;
360         all_copy -= to_copy;
361
362         d_off = 0;
363         index++;
364     }
365
366     return count;
367 }
368
369 typedef struct QEMUBuffer {
370     QEMUSizedBuffer *qsb;
371     QEMUFile *file;
372     bool qsb_allocated;
373 } QEMUBuffer;
374
375 static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
376 {
377     QEMUBuffer *s = opaque;
378     ssize_t len = qsb_get_length(s->qsb) - pos;
379
380     if (len <= 0) {
381         return 0;
382     }
383
384     if (len > size) {
385         len = size;
386     }
387     return qsb_get_buffer(s->qsb, pos, len, buf);
388 }
389
390 static int buf_put_buffer(void *opaque, const uint8_t *buf,
391                           int64_t pos, int size)
392 {
393     QEMUBuffer *s = opaque;
394
395     return qsb_write_at(s->qsb, buf, pos, size);
396 }
397
398 static int buf_close(void *opaque)
399 {
400     QEMUBuffer *s = opaque;
401
402     if (s->qsb_allocated) {
403         qsb_free(s->qsb);
404     }
405
406     g_free(s);
407
408     return 0;
409 }
410
411 const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
412 {
413     QEMUBuffer *p;
414
415     qemu_fflush(f);
416
417     p = f->opaque;
418
419     return p->qsb;
420 }
421
422 static const QEMUFileOps buf_read_ops = {
423     .get_buffer = buf_get_buffer,
424     .close =      buf_close,
425 };
426
427 static const QEMUFileOps buf_write_ops = {
428     .put_buffer = buf_put_buffer,
429     .close =      buf_close,
430 };
431
432 QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
433 {
434     QEMUBuffer *s;
435
436     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
437         mode[1] != '\0') {
438         error_report("qemu_bufopen: Argument validity check failed");
439         return NULL;
440     }
441
442     s = g_malloc0(sizeof(QEMUBuffer));
443     s->qsb = input;
444
445     if (s->qsb == NULL) {
446         s->qsb = qsb_create(NULL, 0);
447         s->qsb_allocated = true;
448     }
449     if (!s->qsb) {
450         g_free(s);
451         error_report("qemu_bufopen: qsb_create failed");
452         return NULL;
453     }
454
455
456     if (mode[0] == 'r') {
457         s->file = qemu_fopen_ops(s, &buf_read_ops);
458     } else {
459         s->file = qemu_fopen_ops(s, &buf_write_ops);
460     }
461     return s->file;
462 }