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 12 #include "qemu/osdep.h" 13 #include "sysemu/blockdev.h" 14 #include "sysemu/block-backend.h" 15 #include "hw/block/block.h" 16 #include "qapi/error.h" 17 #include "qapi/clone-visitor.h" 18 #include "qapi/qapi-visit-block-export.h" 19 #include "qapi/qapi-commands-block-export.h" 20 #include "block/nbd.h" 21 #include "io/channel-socket.h" 22 #include "io/net-listener.h" 23 24 typedef struct NBDServerData { 25 QIONetListener *listener; 26 QCryptoTLSCreds *tlscreds; 27 char *tlsauthz; 28 uint32_t max_connections; 29 uint32_t connections; 30 } NBDServerData; 31 32 static NBDServerData *nbd_server; 33 static bool is_qemu_nbd; 34 35 static void nbd_update_server_watch(NBDServerData *s); 36 37 void nbd_server_is_qemu_nbd(bool value) 38 { 39 is_qemu_nbd = value; 40 } 41 42 bool nbd_server_is_running(void) 43 { 44 return nbd_server || is_qemu_nbd; 45 } 46 47 static void nbd_blockdev_client_closed(NBDClient *client, bool ignored) 48 { 49 nbd_client_put(client); 50 assert(nbd_server->connections > 0); 51 nbd_server->connections--; 52 nbd_update_server_watch(nbd_server); 53 } 54 55 static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc, 56 gpointer opaque) 57 { 58 nbd_server->connections++; 59 nbd_update_server_watch(nbd_server); 60 61 qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server"); 62 nbd_client_new(cioc, nbd_server->tlscreds, nbd_server->tlsauthz, 63 nbd_blockdev_client_closed); 64 } 65 66 static void nbd_update_server_watch(NBDServerData *s) 67 { 68 if (!s->max_connections || s->connections < s->max_connections) { 69 qio_net_listener_set_client_func(s->listener, nbd_accept, NULL, NULL); 70 } else { 71 qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL); 72 } 73 } 74 75 static void nbd_server_free(NBDServerData *server) 76 { 77 if (!server) { 78 return; 79 } 80 81 qio_net_listener_disconnect(server->listener); 82 object_unref(OBJECT(server->listener)); 83 if (server->tlscreds) { 84 object_unref(OBJECT(server->tlscreds)); 85 } 86 g_free(server->tlsauthz); 87 88 g_free(server); 89 } 90 91 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 92 { 93 Object *obj; 94 QCryptoTLSCreds *creds; 95 96 obj = object_resolve_path_component( 97 object_get_objects_root(), id); 98 if (!obj) { 99 error_setg(errp, "No TLS credentials with id '%s'", 100 id); 101 return NULL; 102 } 103 creds = (QCryptoTLSCreds *) 104 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 105 if (!creds) { 106 error_setg(errp, "Object with id '%s' is not TLS credentials", 107 id); 108 return NULL; 109 } 110 111 if (!qcrypto_tls_creds_check_endpoint(creds, 112 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, 113 errp)) { 114 return NULL; 115 } 116 object_ref(obj); 117 return creds; 118 } 119 120 121 void nbd_server_start(SocketAddress *addr, const char *tls_creds, 122 const char *tls_authz, uint32_t max_connections, 123 Error **errp) 124 { 125 if (nbd_server) { 126 error_setg(errp, "NBD server already running"); 127 return; 128 } 129 130 nbd_server = g_new0(NBDServerData, 1); 131 nbd_server->max_connections = max_connections; 132 nbd_server->listener = qio_net_listener_new(); 133 134 qio_net_listener_set_name(nbd_server->listener, 135 "nbd-listener"); 136 137 /* 138 * Because this server is persistent, a backlog of SOMAXCONN is 139 * better than trying to size it to max_connections. 140 */ 141 if (qio_net_listener_open_sync(nbd_server->listener, addr, SOMAXCONN, 142 errp) < 0) { 143 goto error; 144 } 145 146 if (tls_creds) { 147 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp); 148 if (!nbd_server->tlscreds) { 149 goto error; 150 } 151 } 152 153 nbd_server->tlsauthz = g_strdup(tls_authz); 154 155 nbd_update_server_watch(nbd_server); 156 157 return; 158 159 error: 160 nbd_server_free(nbd_server); 161 nbd_server = NULL; 162 } 163 164 void nbd_server_start_options(NbdServerOptions *arg, Error **errp) 165 { 166 nbd_server_start(arg->addr, arg->tls_creds, arg->tls_authz, 167 arg->max_connections, errp); 168 } 169 170 void qmp_nbd_server_start(SocketAddressLegacy *addr, 171 bool has_tls_creds, const char *tls_creds, 172 bool has_tls_authz, const char *tls_authz, 173 bool has_max_connections, uint32_t max_connections, 174 Error **errp) 175 { 176 SocketAddress *addr_flat = socket_address_flatten(addr); 177 178 nbd_server_start(addr_flat, tls_creds, tls_authz, max_connections, errp); 179 qapi_free_SocketAddress(addr_flat); 180 } 181 182 void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp) 183 { 184 BlockExport *export; 185 BlockDriverState *bs; 186 BlockBackend *on_eject_blk; 187 BlockExportOptions *export_opts; 188 189 bs = bdrv_lookup_bs(arg->device, arg->device, errp); 190 if (!bs) { 191 return; 192 } 193 194 /* 195 * block-export-add would default to the node-name, but we may have to use 196 * the device name as a default here for compatibility. 197 */ 198 if (!arg->has_name) { 199 arg->has_name = true; 200 arg->name = g_strdup(arg->device); 201 } 202 203 export_opts = g_new(BlockExportOptions, 1); 204 *export_opts = (BlockExportOptions) { 205 .type = BLOCK_EXPORT_TYPE_NBD, 206 .id = g_strdup(arg->name), 207 .node_name = g_strdup(bdrv_get_node_name(bs)), 208 .has_writable = arg->has_writable, 209 .writable = arg->writable, 210 }; 211 QAPI_CLONE_MEMBERS(BlockExportOptionsNbdBase, &export_opts->u.nbd, 212 qapi_NbdServerAddOptions_base(arg)); 213 if (arg->has_bitmap) { 214 export_opts->u.nbd.has_bitmaps = true; 215 QAPI_LIST_PREPEND(export_opts->u.nbd.bitmaps, g_strdup(arg->bitmap)); 216 } 217 218 /* 219 * nbd-server-add doesn't complain when a read-only device should be 220 * exported as writable, but simply downgrades it. This is an error with 221 * block-export-add. 222 */ 223 if (bdrv_is_read_only(bs)) { 224 export_opts->has_writable = true; 225 export_opts->writable = false; 226 } 227 228 export = blk_exp_add(export_opts, errp); 229 if (!export) { 230 goto fail; 231 } 232 233 /* 234 * nbd-server-add removes the export when the named BlockBackend used for 235 * @device goes away. 236 */ 237 on_eject_blk = blk_by_name(arg->device); 238 if (on_eject_blk) { 239 nbd_export_set_on_eject_blk(export, on_eject_blk); 240 } 241 242 fail: 243 qapi_free_BlockExportOptions(export_opts); 244 } 245 246 void qmp_nbd_server_remove(const char *name, 247 bool has_mode, BlockExportRemoveMode mode, 248 Error **errp) 249 { 250 BlockExport *exp; 251 252 exp = blk_exp_find(name); 253 if (exp && exp->drv->type != BLOCK_EXPORT_TYPE_NBD) { 254 error_setg(errp, "Block export '%s' is not an NBD export", name); 255 return; 256 } 257 258 qmp_block_export_del(name, has_mode, mode, errp); 259 } 260 261 void qmp_nbd_server_stop(Error **errp) 262 { 263 if (!nbd_server) { 264 error_setg(errp, "NBD server not running"); 265 return; 266 } 267 268 blk_exp_close_all_type(BLOCK_EXPORT_TYPE_NBD); 269 270 nbd_server_free(nbd_server); 271 nbd_server = NULL; 272 } 273