4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 IBM Corp.
8 * Stefan Berger <stefanb@linux.vnet.ibm.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
28 #include "qemu-common.h"
29 #include "qemu/error-report.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"
38 #define QSB_CHUNK_SIZE (1 << 10)
39 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
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.
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
51 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
53 QEMUSizedBuffer
*qsb_create(const uint8_t *buffer
, size_t len
)
56 size_t alloc_len
, num_chunks
, i
, to_copy
;
57 size_t chunk_size
= (len
> QSB_MAX_CHUNK_SIZE
)
61 num_chunks
= DIV_ROUND_UP(len
? len
: QSB_CHUNK_SIZE
, chunk_size
);
62 alloc_len
= num_chunks
* chunk_size
;
64 qsb
= g_try_new0(QEMUSizedBuffer
, 1);
69 qsb
->iov
= g_try_new0(struct iovec
, num_chunks
);
75 qsb
->n_iov
= num_chunks
;
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 */
85 qsb
->iov
[i
].iov_len
= chunk_size
;
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
);
94 qsb
->size
= alloc_len
;
100 * Free the QEMUSizedBuffer
102 * @qsb: The QEMUSizedBuffer to free
104 void qsb_free(QEMUSizedBuffer
*qsb
)
112 for (i
= 0; i
< qsb
->n_iov
; i
++) {
113 g_free(qsb
->iov
[i
].iov_base
);
120 * Get the number of used bytes in the QEMUSizedBuffer
122 * @qsb: A QEMUSizedBuffer
124 * Returns the number of bytes currently used in this buffer
126 size_t qsb_get_length(const QEMUSizedBuffer
*qsb
)
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.
137 * @qsb: A QEMUSizedBuffer
138 * @new_len: The new length of bytes in the buffer
140 * Returns the number of bytes the buffer was truncated or extended
143 size_t qsb_set_length(QEMUSizedBuffer
*qsb
, size_t new_len
)
145 if (new_len
<= qsb
->size
) {
148 qsb
->used
= qsb
->size
;
154 * Get the iovec that holds the data for a given position @pos.
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
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.
166 static ssize_t
qsb_get_iovec(const QEMUSizedBuffer
*qsb
,
167 off_t pos
, off_t
*d_off
)
172 if (pos
> qsb
->used
) {
176 for (i
= 0; i
< qsb
->n_iov
; i
++) {
177 if (curr
+ qsb
->iov
[i
].iov_len
> pos
) {
181 curr
+= qsb
->iov
[i
].iov_len
;
187 * Convert the QEMUSizedBuffer into a flat buffer.
189 * Note: If at all possible, try to avoid this function since it
190 * may unnecessarily copy memory around.
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)
197 * Returns the number of bytes copied into the output buffer
199 ssize_t
qsb_get_buffer(const QEMUSizedBuffer
*qsb
, off_t start
,
200 size_t count
, uint8_t *buffer
)
202 const struct iovec
*iov
;
203 size_t to_copy
, all_copy
;
209 if (start
> qsb
->used
) {
213 all_copy
= qsb
->used
- start
;
214 if (all_copy
> count
) {
220 index
= qsb_get_iovec(qsb
, start
, &s_off
);
225 while (all_copy
> 0) {
226 iov
= &qsb
->iov
[index
];
230 to_copy
= iov
->iov_len
- s_off
;
231 if (to_copy
> all_copy
) {
234 memcpy(&buffer
[d_off
], &s
[s_off
], to_copy
);
247 * Grow the QEMUSizedBuffer to the given size and allocate
250 * @qsb: A QEMUSizedBuffer
251 * @new_size: The new size of the buffer
254 * a negative error code in case of memory allocation failure
256 * the new size of the buffer. The returned size may be greater or equal
259 static ssize_t
qsb_grow(QEMUSizedBuffer
*qsb
, size_t new_size
)
261 size_t needed_chunks
, i
;
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
;
269 needed_chunks
= DIV_ROUND_UP(size_diff
, chunk_size
);
271 new_iov
= g_try_new(struct iovec
, qsb
->n_iov
+ needed_chunks
);
272 if (new_iov
== NULL
) {
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
) {
283 /* Free previously allocated new chunks */
284 for (j
= qsb
->n_iov
; j
< i
; j
++) {
285 g_free(new_iov
[j
].iov_base
);
294 * Now we can't get any allocation errors, copy over to new iov
297 for (i
= 0; i
< qsb
->n_iov
; i
++) {
298 new_iov
[i
] = qsb
->iov
[i
];
301 qsb
->n_iov
+= needed_chunks
;
304 qsb
->size
+= (needed_chunks
* chunk_size
);
311 * Write into the QEMUSizedBuffer at a given position and a given
312 * number of bytes. This function will automatically grow the
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
320 * Returns @size or a negative error code in case of memory allocation failure,
321 * or with an invalid 'pos'
323 ssize_t
qsb_write_at(QEMUSizedBuffer
*qsb
, const uint8_t *source
,
324 off_t pos
, size_t count
)
326 ssize_t rc
= qsb_grow(qsb
, pos
+ count
);
328 size_t all_copy
= count
;
329 const struct iovec
*iov
;
332 off_t d_off
, s_off
= 0;
338 if (pos
+ count
> qsb
->used
) {
339 qsb
->used
= pos
+ count
;
342 index
= qsb_get_iovec(qsb
, pos
, &d_off
);
347 while (all_copy
> 0) {
348 iov
= &qsb
->iov
[index
];
350 dest
= iov
->iov_base
;
352 to_copy
= iov
->iov_len
- d_off
;
353 if (to_copy
> all_copy
) {
357 memcpy(&dest
[d_off
], &source
[s_off
], to_copy
);
369 typedef struct QEMUBuffer
{
370 QEMUSizedBuffer
*qsb
;
375 static ssize_t
buf_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
,
378 QEMUBuffer
*s
= opaque
;
379 ssize_t len
= qsb_get_length(s
->qsb
) - pos
;
388 return qsb_get_buffer(s
->qsb
, pos
, len
, buf
);
391 static ssize_t
buf_put_buffer(void *opaque
, const uint8_t *buf
,
392 int64_t pos
, size_t size
)
394 QEMUBuffer
*s
= opaque
;
396 return qsb_write_at(s
->qsb
, buf
, pos
, size
);
399 static int buf_close(void *opaque
)
401 QEMUBuffer
*s
= opaque
;
403 if (s
->qsb_allocated
) {
412 const QEMUSizedBuffer
*qemu_buf_get(QEMUFile
*f
)
423 static const QEMUFileOps buf_read_ops
= {
424 .get_buffer
= buf_get_buffer
,
428 static const QEMUFileOps buf_write_ops
= {
429 .put_buffer
= buf_put_buffer
,
433 QEMUFile
*qemu_bufopen(const char *mode
, QEMUSizedBuffer
*input
)
437 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') ||
439 error_report("qemu_bufopen: Argument validity check failed");
443 s
= g_malloc0(sizeof(QEMUBuffer
));
446 if (s
->qsb
== NULL
) {
447 s
->qsb
= qsb_create(NULL
, 0);
448 s
->qsb_allocated
= true;
452 error_report("qemu_bufopen: qsb_create failed");
457 if (mode
[0] == 'r') {
458 s
->file
= qemu_fopen_ops(s
, &buf_read_ops
);
460 s
->file
= qemu_fopen_ops(s
, &buf_write_ops
);