]> Git Repo - qemu.git/blame - block/nbd-client.c
qtest/ahci: Adding simple dma read-write test
[qemu.git] / block / nbd-client.c
CommitLineData
2302c1ca
MAL
1/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <[email protected]>
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <[email protected]>
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
29#include "nbd-client.h"
30#include "qemu/sockets.h"
31
32#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
33#define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
34
69152c09
MAL
35static void nbd_recv_coroutines_enter_all(NbdClientSession *s)
36{
37 int i;
38
39 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
40 if (s->recv_coroutine[i]) {
41 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
42 }
43 }
44}
45
f53a829b 46static void nbd_teardown_connection(BlockDriverState *bs)
4a41a2d6 47{
f53a829b
HR
48 NbdClientSession *client = nbd_get_client_session(bs);
49
4a41a2d6
SH
50 /* finish any pending coroutines */
51 shutdown(client->sock, 2);
52 nbd_recv_coroutines_enter_all(client);
53
f53a829b 54 nbd_client_detach_aio_context(bs);
4a41a2d6
SH
55 closesocket(client->sock);
56 client->sock = -1;
57}
58
2302c1ca
MAL
59static void nbd_reply_ready(void *opaque)
60{
f53a829b
HR
61 BlockDriverState *bs = opaque;
62 NbdClientSession *s = nbd_get_client_session(bs);
2302c1ca
MAL
63 uint64_t i;
64 int ret;
65
66 if (s->reply.handle == 0) {
67 /* No reply already in flight. Fetch a header. It is possible
68 * that another thread has done the same thing in parallel, so
69 * the socket is not readable anymore.
70 */
71 ret = nbd_receive_reply(s->sock, &s->reply);
72 if (ret == -EAGAIN) {
73 return;
74 }
75 if (ret < 0) {
76 s->reply.handle = 0;
77 goto fail;
78 }
79 }
80
81 /* There's no need for a mutex on the receive side, because the
82 * handler acts as a synchronization point and ensures that only
83 * one coroutine is called until the reply finishes. */
84 i = HANDLE_TO_INDEX(s, s->reply.handle);
85 if (i >= MAX_NBD_REQUESTS) {
86 goto fail;
87 }
88
89 if (s->recv_coroutine[i]) {
90 qemu_coroutine_enter(s->recv_coroutine[i], NULL);
91 return;
92 }
93
94fail:
f53a829b 95 nbd_teardown_connection(bs);
2302c1ca
MAL
96}
97
98static void nbd_restart_write(void *opaque)
99{
f53a829b 100 BlockDriverState *bs = opaque;
2302c1ca 101
f53a829b 102 qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine, NULL);
2302c1ca
MAL
103}
104
f53a829b
HR
105static int nbd_co_send_request(BlockDriverState *bs,
106 struct nbd_request *request,
107 QEMUIOVector *qiov, int offset)
2302c1ca 108{
f53a829b 109 NbdClientSession *s = nbd_get_client_session(bs);
69447cd8 110 AioContext *aio_context;
2302c1ca
MAL
111 int rc, ret;
112
113 qemu_co_mutex_lock(&s->send_mutex);
114 s->send_coroutine = qemu_coroutine_self();
f53a829b 115 aio_context = bdrv_get_aio_context(bs);
69447cd8 116 aio_set_fd_handler(aio_context, s->sock,
f53a829b 117 nbd_reply_ready, nbd_restart_write, bs);
2302c1ca
MAL
118 if (qiov) {
119 if (!s->is_unix) {
120 socket_set_cork(s->sock, 1);
121 }
122 rc = nbd_send_request(s->sock, request);
123 if (rc >= 0) {
124 ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
125 offset, request->len);
126 if (ret != request->len) {
127 rc = -EIO;
128 }
129 }
130 if (!s->is_unix) {
131 socket_set_cork(s->sock, 0);
132 }
133 } else {
134 rc = nbd_send_request(s->sock, request);
135 }
f53a829b 136 aio_set_fd_handler(aio_context, s->sock, nbd_reply_ready, NULL, bs);
2302c1ca
MAL
137 s->send_coroutine = NULL;
138 qemu_co_mutex_unlock(&s->send_mutex);
139 return rc;
140}
141
142static void nbd_co_receive_reply(NbdClientSession *s,
143 struct nbd_request *request, struct nbd_reply *reply,
144 QEMUIOVector *qiov, int offset)
145{
146 int ret;
147
148 /* Wait until we're woken up by the read handler. TODO: perhaps
149 * peek at the next reply and avoid yielding if it's ours? */
150 qemu_coroutine_yield();
151 *reply = s->reply;
152 if (reply->handle != request->handle) {
153 reply->error = EIO;
154 } else {
155 if (qiov && reply->error == 0) {
156 ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
157 offset, request->len);
158 if (ret != request->len) {
159 reply->error = EIO;
160 }
161 }
162
163 /* Tell the read handler to read another header. */
164 s->reply.handle = 0;
165 }
166}
167
168static void nbd_coroutine_start(NbdClientSession *s,
169 struct nbd_request *request)
170{
171 int i;
172
173 /* Poor man semaphore. The free_sema is locked when no other request
174 * can be accepted, and unlocked after receiving one reply. */
175 if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
176 qemu_co_mutex_lock(&s->free_sema);
177 assert(s->in_flight < MAX_NBD_REQUESTS);
178 }
179 s->in_flight++;
180
181 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
182 if (s->recv_coroutine[i] == NULL) {
183 s->recv_coroutine[i] = qemu_coroutine_self();
184 break;
185 }
186 }
187
188 assert(i < MAX_NBD_REQUESTS);
189 request->handle = INDEX_TO_HANDLE(s, i);
190}
191
192static void nbd_coroutine_end(NbdClientSession *s,
193 struct nbd_request *request)
194{
195 int i = HANDLE_TO_INDEX(s, request->handle);
196 s->recv_coroutine[i] = NULL;
197 if (s->in_flight-- == MAX_NBD_REQUESTS) {
198 qemu_co_mutex_unlock(&s->free_sema);
199 }
200}
201
f53a829b 202static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
2302c1ca
MAL
203 int nb_sectors, QEMUIOVector *qiov,
204 int offset)
205{
f53a829b 206 NbdClientSession *client = nbd_get_client_session(bs);
b1b27b64 207 struct nbd_request request = { .type = NBD_CMD_READ };
2302c1ca
MAL
208 struct nbd_reply reply;
209 ssize_t ret;
210
2302c1ca
MAL
211 request.from = sector_num * 512;
212 request.len = nb_sectors * 512;
213
214 nbd_coroutine_start(client, &request);
f53a829b 215 ret = nbd_co_send_request(bs, &request, NULL, 0);
2302c1ca
MAL
216 if (ret < 0) {
217 reply.error = -ret;
218 } else {
219 nbd_co_receive_reply(client, &request, &reply, qiov, offset);
220 }
221 nbd_coroutine_end(client, &request);
222 return -reply.error;
223
224}
225
f53a829b 226static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
2302c1ca
MAL
227 int nb_sectors, QEMUIOVector *qiov,
228 int offset)
229{
f53a829b 230 NbdClientSession *client = nbd_get_client_session(bs);
b1b27b64 231 struct nbd_request request = { .type = NBD_CMD_WRITE };
2302c1ca
MAL
232 struct nbd_reply reply;
233 ssize_t ret;
234
f53a829b 235 if (!bdrv_enable_write_cache(bs) &&
2302c1ca
MAL
236 (client->nbdflags & NBD_FLAG_SEND_FUA)) {
237 request.type |= NBD_CMD_FLAG_FUA;
238 }
239
240 request.from = sector_num * 512;
241 request.len = nb_sectors * 512;
242
243 nbd_coroutine_start(client, &request);
f53a829b 244 ret = nbd_co_send_request(bs, &request, qiov, offset);
2302c1ca
MAL
245 if (ret < 0) {
246 reply.error = -ret;
247 } else {
248 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
249 }
250 nbd_coroutine_end(client, &request);
251 return -reply.error;
252}
253
254/* qemu-nbd has a limit of slightly less than 1M per request. Try to
255 * remain aligned to 4K. */
256#define NBD_MAX_SECTORS 2040
257
f53a829b
HR
258int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
259 int nb_sectors, QEMUIOVector *qiov)
2302c1ca
MAL
260{
261 int offset = 0;
262 int ret;
263 while (nb_sectors > NBD_MAX_SECTORS) {
f53a829b 264 ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
2302c1ca
MAL
265 if (ret < 0) {
266 return ret;
267 }
268 offset += NBD_MAX_SECTORS * 512;
269 sector_num += NBD_MAX_SECTORS;
270 nb_sectors -= NBD_MAX_SECTORS;
271 }
f53a829b 272 return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
2302c1ca
MAL
273}
274
f53a829b
HR
275int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
276 int nb_sectors, QEMUIOVector *qiov)
2302c1ca
MAL
277{
278 int offset = 0;
279 int ret;
280 while (nb_sectors > NBD_MAX_SECTORS) {
f53a829b 281 ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
2302c1ca
MAL
282 if (ret < 0) {
283 return ret;
284 }
285 offset += NBD_MAX_SECTORS * 512;
286 sector_num += NBD_MAX_SECTORS;
287 nb_sectors -= NBD_MAX_SECTORS;
288 }
f53a829b 289 return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
2302c1ca
MAL
290}
291
f53a829b 292int nbd_client_co_flush(BlockDriverState *bs)
2302c1ca 293{
f53a829b 294 NbdClientSession *client = nbd_get_client_session(bs);
b1b27b64 295 struct nbd_request request = { .type = NBD_CMD_FLUSH };
2302c1ca
MAL
296 struct nbd_reply reply;
297 ssize_t ret;
298
299 if (!(client->nbdflags & NBD_FLAG_SEND_FLUSH)) {
300 return 0;
301 }
302
2302c1ca
MAL
303 if (client->nbdflags & NBD_FLAG_SEND_FUA) {
304 request.type |= NBD_CMD_FLAG_FUA;
305 }
306
307 request.from = 0;
308 request.len = 0;
309
310 nbd_coroutine_start(client, &request);
f53a829b 311 ret = nbd_co_send_request(bs, &request, NULL, 0);
2302c1ca
MAL
312 if (ret < 0) {
313 reply.error = -ret;
314 } else {
315 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
316 }
317 nbd_coroutine_end(client, &request);
318 return -reply.error;
319}
320
f53a829b
HR
321int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
322 int nb_sectors)
2302c1ca 323{
f53a829b 324 NbdClientSession *client = nbd_get_client_session(bs);
b1b27b64 325 struct nbd_request request = { .type = NBD_CMD_TRIM };
2302c1ca
MAL
326 struct nbd_reply reply;
327 ssize_t ret;
328
329 if (!(client->nbdflags & NBD_FLAG_SEND_TRIM)) {
330 return 0;
331 }
2302c1ca
MAL
332 request.from = sector_num * 512;
333 request.len = nb_sectors * 512;
334
335 nbd_coroutine_start(client, &request);
f53a829b 336 ret = nbd_co_send_request(bs, &request, NULL, 0);
2302c1ca
MAL
337 if (ret < 0) {
338 reply.error = -ret;
339 } else {
340 nbd_co_receive_reply(client, &request, &reply, NULL, 0);
341 }
342 nbd_coroutine_end(client, &request);
343 return -reply.error;
344
345}
346
f53a829b 347void nbd_client_detach_aio_context(BlockDriverState *bs)
69447cd8 348{
f53a829b
HR
349 aio_set_fd_handler(bdrv_get_aio_context(bs),
350 nbd_get_client_session(bs)->sock, NULL, NULL, NULL);
69447cd8
SH
351}
352
f53a829b
HR
353void nbd_client_attach_aio_context(BlockDriverState *bs,
354 AioContext *new_context)
69447cd8 355{
f53a829b
HR
356 aio_set_fd_handler(new_context, nbd_get_client_session(bs)->sock,
357 nbd_reply_ready, NULL, bs);
69447cd8
SH
358}
359
f53a829b 360void nbd_client_close(BlockDriverState *bs)
2302c1ca 361{
f53a829b 362 NbdClientSession *client = nbd_get_client_session(bs);
b1b27b64
MAL
363 struct nbd_request request = {
364 .type = NBD_CMD_DISC,
365 .from = 0,
366 .len = 0
367 };
2302c1ca 368
4a41a2d6
SH
369 if (client->sock == -1) {
370 return;
371 }
372
373 nbd_send_request(client->sock, &request);
5ad283eb 374
f53a829b 375 nbd_teardown_connection(bs);
2302c1ca
MAL
376}
377
f53a829b
HR
378int nbd_client_init(BlockDriverState *bs, int sock, const char *export,
379 Error **errp)
2302c1ca 380{
f53a829b 381 NbdClientSession *client = nbd_get_client_session(bs);
2302c1ca
MAL
382 int ret;
383
384 /* NBD handshake */
e2bc625f 385 logout("session init %s\n", export);
e53a18e4 386 qemu_set_block(sock);
e2bc625f 387 ret = nbd_receive_negotiate(sock, export,
2302c1ca 388 &client->nbdflags, &client->size,
1ce52846 389 &client->blocksize, errp);
2302c1ca
MAL
390 if (ret < 0) {
391 logout("Failed to negotiate with the NBD server\n");
392 closesocket(sock);
393 return ret;
394 }
395
396 qemu_co_mutex_init(&client->send_mutex);
397 qemu_co_mutex_init(&client->free_sema);
2302c1ca
MAL
398 client->sock = sock;
399
400 /* Now that we're connected, set the socket to be non-blocking and
401 * kick the reply mechanism. */
402 qemu_set_nonblock(sock);
f53a829b 403 nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs));
2302c1ca
MAL
404
405 logout("Established connection with NBD server\n");
406 return 0;
407}
This page took 0.144635 seconds and 4 git commands to generate.