]>
Commit | Line | Data |
---|---|---|
6dd844db PB |
1 | /* |
2 | * Serving QEMU block devices via NBD | |
3 | * | |
4 | * Copyright (c) 2012 Red Hat, Inc. | |
5 | * | |
6 | * Author: Paolo Bonzini <pbonzini@redhat.com> | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
9 | * later. See the COPYING file in the top-level directory. | |
10 | */ | |
11 | ||
d38ea87a | 12 | #include "qemu/osdep.h" |
9c17d615 | 13 | #include "sysemu/blockdev.h" |
e140177d | 14 | #include "sysemu/block-backend.h" |
0d09e41a | 15 | #include "hw/block/block.h" |
7b1b5d19 | 16 | #include "qapi/qmp/qerror.h" |
9c17d615 | 17 | #include "sysemu/sysemu.h" |
6dd844db | 18 | #include "qmp-commands.h" |
737e150e | 19 | #include "block/nbd.h" |
ae398278 | 20 | #include "io/channel-socket.h" |
6dd844db | 21 | |
ddffee39 DB |
22 | typedef struct NBDServerData { |
23 | QIOChannelSocket *listen_ioc; | |
24 | int watch; | |
25 | QCryptoTLSCreds *tlscreds; | |
26 | } NBDServerData; | |
27 | ||
28 | static NBDServerData *nbd_server; | |
29 | ||
6dd844db | 30 | |
ae398278 DB |
31 | static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition, |
32 | gpointer opaque) | |
6dd844db | 33 | { |
ae398278 | 34 | QIOChannelSocket *cioc; |
6dd844db | 35 | |
ddffee39 DB |
36 | if (!nbd_server) { |
37 | return FALSE; | |
38 | } | |
39 | ||
ae398278 DB |
40 | cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), |
41 | NULL); | |
42 | if (!cioc) { | |
43 | return TRUE; | |
44 | } | |
45 | ||
0d73f725 | 46 | qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server"); |
ddffee39 DB |
47 | nbd_client_new(NULL, cioc, |
48 | nbd_server->tlscreds, NULL, | |
49 | nbd_client_put); | |
ae398278 DB |
50 | object_unref(OBJECT(cioc)); |
51 | return TRUE; | |
6dd844db PB |
52 | } |
53 | ||
ddffee39 DB |
54 | |
55 | static void nbd_server_free(NBDServerData *server) | |
6dd844db | 56 | { |
ddffee39 | 57 | if (!server) { |
6dd844db PB |
58 | return; |
59 | } | |
60 | ||
ddffee39 DB |
61 | if (server->watch != -1) { |
62 | g_source_remove(server->watch); | |
63 | } | |
64 | object_unref(OBJECT(server->listen_ioc)); | |
65 | if (server->tlscreds) { | |
66 | object_unref(OBJECT(server->tlscreds)); | |
67 | } | |
68 | ||
69 | g_free(server); | |
70 | } | |
71 | ||
72 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) | |
73 | { | |
74 | Object *obj; | |
75 | QCryptoTLSCreds *creds; | |
76 | ||
77 | obj = object_resolve_path_component( | |
78 | object_get_objects_root(), id); | |
79 | if (!obj) { | |
80 | error_setg(errp, "No TLS credentials with id '%s'", | |
81 | id); | |
82 | return NULL; | |
83 | } | |
84 | creds = (QCryptoTLSCreds *) | |
85 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
86 | if (!creds) { | |
87 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
88 | id); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { | |
93 | error_setg(errp, | |
94 | "Expecting TLS credentials with a server endpoint"); | |
95 | return NULL; | |
96 | } | |
97 | object_ref(obj); | |
98 | return creds; | |
99 | } | |
100 | ||
101 | ||
102 | void qmp_nbd_server_start(SocketAddress *addr, | |
103 | bool has_tls_creds, const char *tls_creds, | |
104 | Error **errp) | |
105 | { | |
106 | if (nbd_server) { | |
107 | error_setg(errp, "NBD server already running"); | |
ae398278 | 108 | return; |
6dd844db | 109 | } |
ae398278 | 110 | |
ddffee39 DB |
111 | nbd_server = g_new0(NBDServerData, 1); |
112 | nbd_server->watch = -1; | |
113 | nbd_server->listen_ioc = qio_channel_socket_new(); | |
0d73f725 DB |
114 | qio_channel_set_name(QIO_CHANNEL(nbd_server->listen_ioc), |
115 | "nbd-listener"); | |
ddffee39 DB |
116 | if (qio_channel_socket_listen_sync( |
117 | nbd_server->listen_ioc, addr, errp) < 0) { | |
118 | goto error; | |
119 | } | |
120 | ||
121 | if (has_tls_creds) { | |
122 | nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp); | |
123 | if (!nbd_server->tlscreds) { | |
124 | goto error; | |
125 | } | |
126 | ||
127 | if (addr->type != SOCKET_ADDRESS_KIND_INET) { | |
128 | error_setg(errp, "TLS is only supported with IPv4/IPv6"); | |
129 | goto error; | |
130 | } | |
131 | } | |
132 | ||
133 | nbd_server->watch = qio_channel_add_watch( | |
134 | QIO_CHANNEL(nbd_server->listen_ioc), | |
135 | G_IO_IN, | |
136 | nbd_accept, | |
137 | NULL, | |
138 | NULL); | |
139 | ||
140 | return; | |
141 | ||
142 | error: | |
143 | nbd_server_free(nbd_server); | |
144 | nbd_server = NULL; | |
6dd844db PB |
145 | } |
146 | ||
6dd844db PB |
147 | void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, |
148 | Error **errp) | |
149 | { | |
094138d0 KW |
150 | BlockDriverState *bs = NULL; |
151 | BlockBackend *on_eject_blk; | |
6dd844db | 152 | NBDExport *exp; |
6dd844db | 153 | |
ddffee39 | 154 | if (!nbd_server) { |
17b6be4a PB |
155 | error_setg(errp, "NBD server not running"); |
156 | return; | |
157 | } | |
158 | ||
6dd844db PB |
159 | if (nbd_export_find(device)) { |
160 | error_setg(errp, "NBD server already exporting device '%s'", device); | |
161 | return; | |
162 | } | |
163 | ||
094138d0 KW |
164 | on_eject_blk = blk_by_name(device); |
165 | ||
166 | bs = bdrv_lookup_bs(device, device, errp); | |
167 | if (!bs) { | |
60fe4fac HB |
168 | return; |
169 | } | |
6dd844db | 170 | |
e6444734 | 171 | if (!has_writable) { |
f3313d23 | 172 | writable = false; |
e6444734 | 173 | } |
094138d0 | 174 | if (bdrv_is_read_only(bs)) { |
e6444734 PB |
175 | writable = false; |
176 | } | |
177 | ||
094138d0 KW |
178 | exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, |
179 | NULL, false, on_eject_blk, errp); | |
98f44bbe HR |
180 | if (!exp) { |
181 | return; | |
182 | } | |
6dd844db PB |
183 | |
184 | nbd_export_set_name(exp, device); | |
6dd844db | 185 | |
741cc431 HR |
186 | /* The list of named exports has a strong reference to this export now and |
187 | * our only way of accessing it is through nbd_export_find(), so we can drop | |
188 | * the strong reference that is @exp. */ | |
189 | nbd_export_put(exp); | |
6dd844db PB |
190 | } |
191 | ||
192 | void qmp_nbd_server_stop(Error **errp) | |
193 | { | |
741cc431 | 194 | nbd_export_close_all(); |
6dd844db | 195 | |
ddffee39 DB |
196 | nbd_server_free(nbd_server); |
197 | nbd_server = NULL; | |
6dd844db | 198 | } |