4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "blockdev_nvme.h"
38 #include "spdk/string.h"
40 #include "spdk/util.h"
42 #include "spdk_internal/log.h"
44 struct rpc_construct_nvme
{
54 free_rpc_construct_nvme(struct rpc_construct_nvme
*req
)
64 static const struct spdk_json_object_decoder rpc_construct_nvme_decoders
[] = {
65 {"name", offsetof(struct rpc_construct_nvme
, name
), spdk_json_decode_string
},
66 {"trtype", offsetof(struct rpc_construct_nvme
, trtype
), spdk_json_decode_string
},
67 {"traddr", offsetof(struct rpc_construct_nvme
, traddr
), spdk_json_decode_string
},
69 {"adrfam", offsetof(struct rpc_construct_nvme
, adrfam
), spdk_json_decode_string
, true},
70 {"trsvcid", offsetof(struct rpc_construct_nvme
, trsvcid
), spdk_json_decode_string
, true},
71 {"subnqn", offsetof(struct rpc_construct_nvme
, subnqn
), spdk_json_decode_string
, true},
74 #define NVME_MAX_BLOCKDEVS_PER_RPC 32
77 spdk_rpc_construct_nvme_bdev(struct spdk_jsonrpc_server_conn
*conn
,
78 const struct spdk_json_val
*params
,
79 const struct spdk_json_val
*id
)
81 struct rpc_construct_nvme req
= {};
82 struct spdk_json_write_ctx
*w
;
83 struct spdk_nvme_transport_id trid
= {};
84 const char *names
[NVME_MAX_BLOCKDEVS_PER_RPC
];
89 if (spdk_json_decode_object(params
, rpc_construct_nvme_decoders
,
90 SPDK_COUNTOF(rpc_construct_nvme_decoders
),
92 SPDK_ERRLOG("spdk_json_decode_object failed\n");
97 rc
= spdk_nvme_transport_id_parse_trtype(&trid
.trtype
, req
.trtype
);
99 SPDK_ERRLOG("Failed to parse trtype: %s\n", req
.trtype
);
104 snprintf(trid
.traddr
, sizeof(trid
.traddr
), "%s", req
.traddr
);
108 rc
= spdk_nvme_transport_id_parse_adrfam(&trid
.adrfam
, req
.adrfam
);
110 SPDK_ERRLOG("Failed to parse adrfam: %s\n", req
.adrfam
);
117 snprintf(trid
.trsvcid
, sizeof(trid
.trsvcid
), "%s", req
.trsvcid
);
122 snprintf(trid
.subnqn
, sizeof(trid
.subnqn
), "%s", req
.subnqn
);
125 count
= NVME_MAX_BLOCKDEVS_PER_RPC
;
126 if (spdk_bdev_nvme_create(&trid
, req
.name
, names
, &count
)) {
131 free_rpc_construct_nvme(&req
);
135 w
= spdk_jsonrpc_begin_result(conn
, id
);
136 spdk_json_write_array_begin(w
);
137 for (i
= 0; i
< count
; i
++) {
138 spdk_json_write_string(w
, names
[i
]);
140 spdk_json_write_array_end(w
);
141 spdk_jsonrpc_end_result(conn
, w
);
143 free_rpc_construct_nvme(&req
);
148 spdk_jsonrpc_send_error_response(conn
, id
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
, "Invalid parameters");
149 free_rpc_construct_nvme(&req
);
151 SPDK_RPC_REGISTER("construct_nvme_bdev", spdk_rpc_construct_nvme_bdev
)