2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device Server Side
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "nbd-internal.h"
23 static int system_errno_to_nbd_errno(int err
)
47 /* Definitions for opaque data types */
49 typedef struct NBDRequest NBDRequest
;
52 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
60 void (*close
)(NBDExport
*exp
);
67 QTAILQ_HEAD(, NBDClient
) clients
;
68 QTAILQ_ENTRY(NBDExport
) next
;
72 BlockBackend
*eject_notifier_blk
;
73 Notifier eject_notifier
;
76 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
80 void (*close
)(NBDClient
*client
);
83 QCryptoTLSCreds
*tlscreds
;
85 QIOChannelSocket
*sioc
; /* The underlying data channel */
86 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
88 Coroutine
*recv_coroutine
;
91 Coroutine
*send_coroutine
;
95 QTAILQ_ENTRY(NBDClient
) next
;
100 /* That's all folks */
102 static void nbd_set_handlers(NBDClient
*client
);
103 static void nbd_unset_handlers(NBDClient
*client
);
104 static void nbd_update_can_read(NBDClient
*client
);
106 static gboolean
nbd_negotiate_continue(QIOChannel
*ioc
,
107 GIOCondition condition
,
110 qemu_coroutine_enter(opaque
);
114 static ssize_t
nbd_negotiate_read(QIOChannel
*ioc
, void *buffer
, size_t size
)
119 assert(qemu_in_coroutine());
120 /* Negotiation are always in main loop. */
121 watch
= qio_channel_add_watch(ioc
,
123 nbd_negotiate_continue
,
124 qemu_coroutine_self(),
126 ret
= read_sync(ioc
, buffer
, size
);
127 g_source_remove(watch
);
132 static ssize_t
nbd_negotiate_write(QIOChannel
*ioc
, void *buffer
, size_t size
)
137 assert(qemu_in_coroutine());
138 /* Negotiation are always in main loop. */
139 watch
= qio_channel_add_watch(ioc
,
141 nbd_negotiate_continue
,
142 qemu_coroutine_self(),
144 ret
= write_sync(ioc
, buffer
, size
);
145 g_source_remove(watch
);
149 static ssize_t
nbd_negotiate_drop_sync(QIOChannel
*ioc
, size_t size
)
151 ssize_t ret
, dropped
= size
;
152 uint8_t *buffer
= g_malloc(MIN(65536, size
));
155 ret
= nbd_negotiate_read(ioc
, buffer
, MIN(65536, size
));
169 /* Basic flow for negotiation
196 static int nbd_negotiate_send_rep(QIOChannel
*ioc
, uint32_t type
, uint32_t opt
)
201 TRACE("Reply opt=%" PRIx32
" type=%" PRIx32
, type
, opt
);
203 magic
= cpu_to_be64(NBD_REP_MAGIC
);
204 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
205 LOG("write failed (rep magic)");
208 opt
= cpu_to_be32(opt
);
209 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
210 LOG("write failed (rep opt)");
213 type
= cpu_to_be32(type
);
214 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
215 LOG("write failed (rep type)");
218 len
= cpu_to_be32(0);
219 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
220 LOG("write failed (rep data length)");
226 static int nbd_negotiate_send_rep_list(QIOChannel
*ioc
, NBDExport
*exp
)
228 uint64_t magic
, name_len
;
229 uint32_t opt
, type
, len
;
231 TRACE("Advertising export name '%s'", exp
->name
? exp
->name
: "");
232 name_len
= strlen(exp
->name
);
233 magic
= cpu_to_be64(NBD_REP_MAGIC
);
234 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
235 LOG("write failed (magic)");
238 opt
= cpu_to_be32(NBD_OPT_LIST
);
239 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
240 LOG("write failed (opt)");
243 type
= cpu_to_be32(NBD_REP_SERVER
);
244 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
245 LOG("write failed (reply type)");
248 len
= cpu_to_be32(name_len
+ sizeof(len
));
249 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
250 LOG("write failed (length)");
253 len
= cpu_to_be32(name_len
);
254 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
255 LOG("write failed (length)");
258 if (nbd_negotiate_write(ioc
, exp
->name
, name_len
) != name_len
) {
259 LOG("write failed (buffer)");
265 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
270 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
273 return nbd_negotiate_send_rep(client
->ioc
,
274 NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
277 /* For each export, send a NBD_REP_SERVER reply. */
278 QTAILQ_FOREACH(exp
, &exports
, next
) {
279 if (nbd_negotiate_send_rep_list(client
->ioc
, exp
)) {
283 /* Finish with a NBD_REP_ACK. */
284 return nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_LIST
);
287 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
290 char name
[NBD_MAX_NAME_SIZE
+ 1];
293 [20 .. xx] export name (length bytes)
295 TRACE("Checking length");
296 if (length
>= sizeof(name
)) {
297 LOG("Bad length received");
300 if (nbd_negotiate_read(client
->ioc
, name
, length
) != length
) {
306 TRACE("Client requested export '%s'", name
);
308 client
->exp
= nbd_export_find(name
);
310 LOG("export not found");
314 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
315 nbd_export_get(client
->exp
);
322 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
327 struct NBDTLSHandshakeData data
= { 0 };
329 TRACE("Setting up TLS");
332 if (nbd_negotiate_drop_sync(ioc
, length
) != length
) {
335 nbd_negotiate_send_rep(ioc
, NBD_REP_ERR_INVALID
, NBD_OPT_STARTTLS
);
339 if (nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
,
340 NBD_OPT_STARTTLS
) < 0) {
344 tioc
= qio_channel_tls_new_server(ioc
,
352 TRACE("Starting TLS handshake");
353 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
354 qio_channel_tls_handshake(tioc
,
359 if (!data
.complete
) {
360 g_main_loop_run(data
.loop
);
362 g_main_loop_unref(data
.loop
);
364 object_unref(OBJECT(tioc
));
365 error_free(data
.error
);
369 return QIO_CHANNEL(tioc
);
373 static int nbd_negotiate_options(NBDClient
*client
)
376 bool fixedNewstyle
= false;
379 [ 0 .. 3] client flags
381 [ 0 .. 7] NBD_OPTS_MAGIC
382 [ 8 .. 11] NBD option
383 [12 .. 15] Data length
386 [ 0 .. 7] NBD_OPTS_MAGIC
387 [ 8 .. 11] Second NBD option
388 [12 .. 15] Data length
392 if (nbd_negotiate_read(client
->ioc
, &flags
, sizeof(flags
)) !=
397 TRACE("Checking client flags");
398 be32_to_cpus(&flags
);
399 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
400 TRACE("Client supports fixed newstyle handshake");
401 fixedNewstyle
= true;
402 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
405 TRACE("Unknown client flags 0x%" PRIx32
" received", flags
);
411 uint32_t clientflags
, length
;
414 if (nbd_negotiate_read(client
->ioc
, &magic
, sizeof(magic
)) !=
419 TRACE("Checking opts magic");
420 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
421 LOG("Bad magic received");
425 if (nbd_negotiate_read(client
->ioc
, &clientflags
,
426 sizeof(clientflags
)) != sizeof(clientflags
)) {
430 clientflags
= be32_to_cpu(clientflags
);
432 if (nbd_negotiate_read(client
->ioc
, &length
, sizeof(length
)) !=
437 length
= be32_to_cpu(length
);
439 TRACE("Checking option 0x%" PRIx32
, clientflags
);
440 if (client
->tlscreds
&&
441 client
->ioc
== (QIOChannel
*)client
->sioc
) {
443 if (!fixedNewstyle
) {
444 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
447 switch (clientflags
) {
448 case NBD_OPT_STARTTLS
:
449 tioc
= nbd_negotiate_handle_starttls(client
, length
);
453 object_unref(OBJECT(client
->ioc
));
454 client
->ioc
= QIO_CHANNEL(tioc
);
457 case NBD_OPT_EXPORT_NAME
:
458 /* No way to return an error to client, so drop connection */
459 TRACE("Option 0x%x not permitted before TLS", clientflags
);
463 TRACE("Option 0x%" PRIx32
" not permitted before TLS",
465 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
468 ret
= nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_TLS_REQD
,
475 } else if (fixedNewstyle
) {
476 switch (clientflags
) {
478 ret
= nbd_negotiate_handle_list(client
, length
);
487 case NBD_OPT_EXPORT_NAME
:
488 return nbd_negotiate_handle_export_name(client
, length
);
490 case NBD_OPT_STARTTLS
:
491 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
494 if (client
->tlscreds
) {
495 TRACE("TLS already enabled");
496 ret
= nbd_negotiate_send_rep(client
->ioc
,
500 TRACE("TLS not configured");
501 ret
= nbd_negotiate_send_rep(client
->ioc
,
510 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
511 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
514 ret
= nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_UNSUP
,
523 * If broken new-style we should drop the connection
524 * for anything except NBD_OPT_EXPORT_NAME
526 switch (clientflags
) {
527 case NBD_OPT_EXPORT_NAME
:
528 return nbd_negotiate_handle_export_name(client
, length
);
531 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
543 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
545 NBDClient
*client
= data
->client
;
546 char buf
[8 + 8 + 8 + 128];
548 const uint16_t myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
549 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
552 /* Old style negotiation header without options
553 [ 0 .. 7] passwd ("NBDMAGIC")
554 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
556 [24 .. 25] server flags (0)
557 [26 .. 27] export flags
558 [28 .. 151] reserved (0)
560 New style negotiation header with options
561 [ 0 .. 7] passwd ("NBDMAGIC")
562 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
563 [16 .. 17] server flags (0)
566 [26 .. 27] export flags
567 [28 .. 151] reserved (0)
570 qio_channel_set_blocking(client
->ioc
, false, NULL
);
573 TRACE("Beginning negotiation.");
574 memset(buf
, 0, sizeof(buf
));
575 memcpy(buf
, "NBDMAGIC", 8);
577 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
579 TRACE("advertising size %" PRIu64
" and flags %x",
580 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
581 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
582 stq_be_p(buf
+ 16, client
->exp
->size
);
583 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
585 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
586 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
);
590 if (client
->tlscreds
) {
591 TRACE("TLS cannot be enabled with oldstyle protocol");
594 if (nbd_negotiate_write(client
->ioc
, buf
, sizeof(buf
)) != sizeof(buf
)) {
599 if (nbd_negotiate_write(client
->ioc
, buf
, 18) != 18) {
603 rc
= nbd_negotiate_options(client
);
605 LOG("option negotiation failed");
609 TRACE("advertising size %" PRIu64
" and flags %x",
610 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
611 stq_be_p(buf
+ 18, client
->exp
->size
);
612 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
613 if (nbd_negotiate_write(client
->ioc
, buf
+ 18, sizeof(buf
) - 18) !=
620 TRACE("Negotiation succeeded.");
626 static ssize_t
nbd_receive_request(QIOChannel
*ioc
, struct nbd_request
*request
)
628 uint8_t buf
[NBD_REQUEST_SIZE
];
632 ret
= read_sync(ioc
, buf
, sizeof(buf
));
637 if (ret
!= sizeof(buf
)) {
643 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
644 [ 4 .. 7] type (0 == READ, 1 == WRITE)
650 magic
= ldl_be_p(buf
);
651 request
->type
= ldl_be_p(buf
+ 4);
652 request
->handle
= ldq_be_p(buf
+ 8);
653 request
->from
= ldq_be_p(buf
+ 16);
654 request
->len
= ldl_be_p(buf
+ 24);
656 TRACE("Got request: { magic = 0x%" PRIx32
", .type = %" PRIx32
657 ", from = %" PRIu64
" , len = %" PRIu32
" }",
658 magic
, request
->type
, request
->from
, request
->len
);
660 if (magic
!= NBD_REQUEST_MAGIC
) {
661 LOG("invalid magic (got 0x%" PRIx32
")", magic
);
667 static ssize_t
nbd_send_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
669 uint8_t buf
[NBD_REPLY_SIZE
];
672 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
674 TRACE("Sending response to client: { .error = %" PRId32
675 ", handle = %" PRIu64
" }",
676 reply
->error
, reply
->handle
);
679 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
680 [ 4 .. 7] error (0 == no error)
683 stl_be_p(buf
, NBD_REPLY_MAGIC
);
684 stl_be_p(buf
+ 4, reply
->error
);
685 stq_be_p(buf
+ 8, reply
->handle
);
687 ret
= write_sync(ioc
, buf
, sizeof(buf
));
692 if (ret
!= sizeof(buf
)) {
693 LOG("writing to socket failed");
699 #define MAX_NBD_REQUESTS 16
701 void nbd_client_get(NBDClient
*client
)
706 void nbd_client_put(NBDClient
*client
)
708 if (--client
->refcount
== 0) {
709 /* The last reference should be dropped by client->close,
710 * which is called by client_close.
712 assert(client
->closing
);
714 nbd_unset_handlers(client
);
715 object_unref(OBJECT(client
->sioc
));
716 object_unref(OBJECT(client
->ioc
));
717 if (client
->tlscreds
) {
718 object_unref(OBJECT(client
->tlscreds
));
720 g_free(client
->tlsaclname
);
722 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
723 nbd_export_put(client
->exp
);
729 static void client_close(NBDClient
*client
)
731 if (client
->closing
) {
735 client
->closing
= true;
737 /* Force requests to finish. They will drop their own references,
738 * then we'll close the socket and free the NBDClient.
740 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
743 /* Also tell the client, so that they release their reference. */
745 client
->close(client
);
749 static NBDRequest
*nbd_request_get(NBDClient
*client
)
753 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
754 client
->nb_requests
++;
755 nbd_update_can_read(client
);
757 req
= g_new0(NBDRequest
, 1);
758 nbd_client_get(client
);
759 req
->client
= client
;
763 static void nbd_request_put(NBDRequest
*req
)
765 NBDClient
*client
= req
->client
;
768 qemu_vfree(req
->data
);
772 client
->nb_requests
--;
773 nbd_update_can_read(client
);
774 nbd_client_put(client
);
777 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
779 NBDExport
*exp
= opaque
;
782 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
786 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
787 nbd_set_handlers(client
);
791 static void blk_aio_detach(void *opaque
)
793 NBDExport
*exp
= opaque
;
796 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
798 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
799 nbd_unset_handlers(client
);
805 static void nbd_eject_notifier(Notifier
*n
, void *data
)
807 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
808 nbd_export_close(exp
);
811 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
, off_t size
,
812 uint16_t nbdflags
, void (*close
)(NBDExport
*),
813 bool writethrough
, BlockBackend
*on_eject_blk
,
817 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
820 blk_insert_bs(blk
, bs
);
821 blk_set_enable_write_cache(blk
, !writethrough
);
824 QTAILQ_INIT(&exp
->clients
);
826 exp
->dev_offset
= dev_offset
;
827 exp
->nbdflags
= nbdflags
;
828 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
830 error_setg_errno(errp
, -exp
->size
,
831 "Failed to determine the NBD export's length");
834 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
837 exp
->ctx
= blk_get_aio_context(blk
);
838 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
841 blk_ref(on_eject_blk
);
842 exp
->eject_notifier_blk
= on_eject_blk
;
843 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
844 blk_add_remove_bs_notifier(on_eject_blk
, &exp
->eject_notifier
);
848 * NBD exports are used for non-shared storage migration. Make sure
849 * that BDRV_O_INACTIVE is cleared and the image is ready for write
850 * access since the export could be available before migration handover.
852 aio_context_acquire(exp
->ctx
);
853 blk_invalidate_cache(blk
, NULL
);
854 aio_context_release(exp
->ctx
);
863 NBDExport
*nbd_export_find(const char *name
)
866 QTAILQ_FOREACH(exp
, &exports
, next
) {
867 if (strcmp(name
, exp
->name
) == 0) {
875 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
877 if (exp
->name
== name
) {
882 if (exp
->name
!= NULL
) {
885 QTAILQ_REMOVE(&exports
, exp
, next
);
890 exp
->name
= g_strdup(name
);
891 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
896 void nbd_export_close(NBDExport
*exp
)
898 NBDClient
*client
, *next
;
901 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
902 client_close(client
);
904 nbd_export_set_name(exp
, NULL
);
908 void nbd_export_get(NBDExport
*exp
)
910 assert(exp
->refcount
> 0);
914 void nbd_export_put(NBDExport
*exp
)
916 assert(exp
->refcount
> 0);
917 if (exp
->refcount
== 1) {
918 nbd_export_close(exp
);
921 if (--exp
->refcount
== 0) {
922 assert(exp
->name
== NULL
);
929 if (exp
->eject_notifier_blk
) {
930 notifier_remove(&exp
->eject_notifier
);
931 blk_unref(exp
->eject_notifier_blk
);
933 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
934 blk_aio_detach
, exp
);
943 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
948 void nbd_export_close_all(void)
950 NBDExport
*exp
, *next
;
952 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
953 nbd_export_close(exp
);
957 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
960 NBDClient
*client
= req
->client
;
963 g_assert(qemu_in_coroutine());
964 qemu_co_mutex_lock(&client
->send_lock
);
965 client
->send_coroutine
= qemu_coroutine_self();
966 nbd_set_handlers(client
);
969 rc
= nbd_send_reply(client
->ioc
, reply
);
971 qio_channel_set_cork(client
->ioc
, true);
972 rc
= nbd_send_reply(client
->ioc
, reply
);
974 ret
= write_sync(client
->ioc
, req
->data
, len
);
979 qio_channel_set_cork(client
->ioc
, false);
982 client
->send_coroutine
= NULL
;
983 nbd_set_handlers(client
);
984 qemu_co_mutex_unlock(&client
->send_lock
);
988 /* Collect a client request. Return 0 if request looks valid, -EAGAIN
989 * to keep trying the collection, -EIO to drop connection right away,
990 * and any other negative value to report an error to the client
991 * (although the caller may still need to disconnect after reporting
993 static ssize_t
nbd_co_receive_request(NBDRequest
*req
,
994 struct nbd_request
*request
)
996 NBDClient
*client
= req
->client
;
1000 g_assert(qemu_in_coroutine());
1001 client
->recv_coroutine
= qemu_coroutine_self();
1002 nbd_update_can_read(client
);
1004 rc
= nbd_receive_request(client
->ioc
, request
);
1006 if (rc
!= -EAGAIN
) {
1012 TRACE("Decoding type");
1014 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1015 if (command
!= NBD_CMD_WRITE
) {
1016 /* No payload, we are ready to read the next request. */
1017 req
->complete
= true;
1020 if (command
== NBD_CMD_DISC
) {
1021 /* Special case: we're going to disconnect without a reply,
1022 * whether or not flags, from, or len are bogus */
1023 TRACE("Request type is DISCONNECT");
1028 /* Check for sanity in the parameters, part 1. Defer as many
1029 * checks as possible until after reading any NBD_CMD_WRITE
1030 * payload, so we can try and keep the connection alive. */
1031 if ((request
->from
+ request
->len
) < request
->from
) {
1032 LOG("integer overflow detected, you're probably being attacked");
1037 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1038 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1039 LOG("len (%" PRIu32
" ) is larger than max len (%u)",
1040 request
->len
, NBD_MAX_BUFFER_SIZE
);
1045 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
1046 if (req
->data
== NULL
) {
1051 if (command
== NBD_CMD_WRITE
) {
1052 TRACE("Reading %" PRIu32
" byte(s)", request
->len
);
1054 if (read_sync(client
->ioc
, req
->data
, request
->len
) != request
->len
) {
1055 LOG("reading from socket failed");
1059 req
->complete
= true;
1062 /* Sanity checks, part 2. */
1063 if (request
->from
+ request
->len
> client
->exp
->size
) {
1064 LOG("operation past EOF; From: %" PRIu64
", Len: %" PRIu32
1065 ", Size: %" PRIu64
, request
->from
, request
->len
,
1066 (uint64_t)client
->exp
->size
);
1067 rc
= command
== NBD_CMD_WRITE
? -ENOSPC
: -EINVAL
;
1070 if (request
->type
& ~NBD_CMD_MASK_COMMAND
& ~NBD_CMD_FLAG_FUA
) {
1071 LOG("unsupported flags (got 0x%x)",
1072 request
->type
& ~NBD_CMD_MASK_COMMAND
);
1080 client
->recv_coroutine
= NULL
;
1081 nbd_update_can_read(client
);
1086 static void nbd_trip(void *opaque
)
1088 NBDClient
*client
= opaque
;
1089 NBDExport
*exp
= client
->exp
;
1091 struct nbd_request request
;
1092 struct nbd_reply reply
;
1097 TRACE("Reading request.");
1098 if (client
->closing
) {
1102 req
= nbd_request_get(client
);
1103 ret
= nbd_co_receive_request(req
, &request
);
1104 if (ret
== -EAGAIN
) {
1111 reply
.handle
= request
.handle
;
1118 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1120 if (client
->closing
) {
1122 * The client may be closed when we are blocked in
1123 * nbd_co_receive_request()
1130 TRACE("Request type is READ");
1132 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1133 ret
= blk_co_flush(exp
->blk
);
1135 LOG("flush failed");
1141 ret
= blk_pread(exp
->blk
, request
.from
+ exp
->dev_offset
,
1142 req
->data
, request
.len
);
1144 LOG("reading from file failed");
1149 TRACE("Read %" PRIu32
" byte(s)", request
.len
);
1150 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1154 TRACE("Request type is WRITE");
1156 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1157 TRACE("Server is read-only, return error");
1158 reply
.error
= EROFS
;
1162 TRACE("Writing to device");
1165 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1166 flags
|= BDRV_REQ_FUA
;
1168 ret
= blk_pwrite(exp
->blk
, request
.from
+ exp
->dev_offset
,
1169 req
->data
, request
.len
, flags
);
1171 LOG("writing to file failed");
1176 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1182 /* unreachable, thanks to special case in nbd_co_receive_request() */
1186 TRACE("Request type is FLUSH");
1188 ret
= blk_co_flush(exp
->blk
);
1190 LOG("flush failed");
1193 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1198 TRACE("Request type is TRIM");
1199 ret
= blk_co_pdiscard(exp
->blk
, request
.from
+ exp
->dev_offset
,
1202 LOG("discard failed");
1205 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1210 LOG("invalid request type (%" PRIu32
") received", request
.type
);
1211 reply
.error
= EINVAL
;
1213 /* We must disconnect after NBD_CMD_WRITE if we did not
1216 if (nbd_co_send_reply(req
, &reply
, 0) < 0 || !req
->complete
) {
1222 TRACE("Request/Reply complete");
1225 nbd_request_put(req
);
1229 nbd_request_put(req
);
1230 client_close(client
);
1233 static void nbd_read(void *opaque
)
1235 NBDClient
*client
= opaque
;
1237 if (client
->recv_coroutine
) {
1238 qemu_coroutine_enter(client
->recv_coroutine
);
1240 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
, client
));
1244 static void nbd_restart_write(void *opaque
)
1246 NBDClient
*client
= opaque
;
1248 qemu_coroutine_enter(client
->send_coroutine
);
1251 static void nbd_set_handlers(NBDClient
*client
)
1253 if (client
->exp
&& client
->exp
->ctx
) {
1254 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1256 client
->can_read
? nbd_read
: NULL
,
1257 client
->send_coroutine
? nbd_restart_write
: NULL
,
1262 static void nbd_unset_handlers(NBDClient
*client
)
1264 if (client
->exp
&& client
->exp
->ctx
) {
1265 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1266 true, NULL
, NULL
, NULL
);
1270 static void nbd_update_can_read(NBDClient
*client
)
1272 bool can_read
= client
->recv_coroutine
||
1273 client
->nb_requests
< MAX_NBD_REQUESTS
;
1275 if (can_read
!= client
->can_read
) {
1276 client
->can_read
= can_read
;
1277 nbd_set_handlers(client
);
1279 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1280 * in nbd_set_handlers() will have taken care of that */
1284 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1286 NBDClientNewData
*data
= opaque
;
1287 NBDClient
*client
= data
->client
;
1288 NBDExport
*exp
= client
->exp
;
1291 nbd_export_get(exp
);
1293 if (nbd_negotiate(data
)) {
1294 client_close(client
);
1297 qemu_co_mutex_init(&client
->send_lock
);
1298 nbd_set_handlers(client
);
1301 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1307 void nbd_client_new(NBDExport
*exp
,
1308 QIOChannelSocket
*sioc
,
1309 QCryptoTLSCreds
*tlscreds
,
1310 const char *tlsaclname
,
1311 void (*close_fn
)(NBDClient
*))
1314 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1316 client
= g_malloc0(sizeof(NBDClient
));
1317 client
->refcount
= 1;
1319 client
->tlscreds
= tlscreds
;
1321 object_ref(OBJECT(client
->tlscreds
));
1323 client
->tlsaclname
= g_strdup(tlsaclname
);
1324 client
->sioc
= sioc
;
1325 object_ref(OBJECT(client
->sioc
));
1326 client
->ioc
= QIO_CHANNEL(sioc
);
1327 object_ref(OBJECT(client
->ioc
));
1328 client
->can_read
= true;
1329 client
->close
= close_fn
;
1331 data
->client
= client
;
1332 data
->co
= qemu_coroutine_create(nbd_co_client_start
, data
);
1333 qemu_coroutine_enter(data
->co
);