]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/bdev/nvme/blockdev_nvme_rpc.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / lib / bdev / nvme / blockdev_nvme_rpc.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34 #include <string.h>
35
36 #include "blockdev_nvme.h"
37
38 #include "spdk/string.h"
39 #include "spdk/rpc.h"
40 #include "spdk/util.h"
41
42 #include "spdk_internal/log.h"
43
44 struct rpc_construct_nvme {
45 char *name;
46 char *trtype;
47 char *adrfam;
48 char *traddr;
49 char *trsvcid;
50 char *subnqn;
51 };
52
53 static void
54 free_rpc_construct_nvme(struct rpc_construct_nvme *req)
55 {
56 free(req->name);
57 free(req->trtype);
58 free(req->adrfam);
59 free(req->traddr);
60 free(req->trsvcid);
61 free(req->subnqn);
62 }
63
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},
68
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},
72 };
73
74 #define NVME_MAX_BLOCKDEVS_PER_RPC 32
75
76 static void
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)
80 {
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];
85 size_t count;
86 size_t i;
87 int rc;
88
89 if (spdk_json_decode_object(params, rpc_construct_nvme_decoders,
90 SPDK_COUNTOF(rpc_construct_nvme_decoders),
91 &req)) {
92 SPDK_ERRLOG("spdk_json_decode_object failed\n");
93 goto invalid;
94 }
95
96 /* Parse trtype */
97 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, req.trtype);
98 if (rc < 0) {
99 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
100 goto invalid;
101 }
102
103 /* Parse traddr */
104 snprintf(trid.traddr, sizeof(trid.traddr), "%s", req.traddr);
105
106 /* Parse adrfam */
107 if (req.adrfam) {
108 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, req.adrfam);
109 if (rc < 0) {
110 SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
111 goto invalid;
112 }
113 }
114
115 /* Parse trsvcid */
116 if (req.trsvcid) {
117 snprintf(trid.trsvcid, sizeof(trid.trsvcid), "%s", req.trsvcid);
118 }
119
120 /* Parse subnqn */
121 if (req.subnqn) {
122 snprintf(trid.subnqn, sizeof(trid.subnqn), "%s", req.subnqn);
123 }
124
125 count = NVME_MAX_BLOCKDEVS_PER_RPC;
126 if (spdk_bdev_nvme_create(&trid, req.name, names, &count)) {
127 goto invalid;
128 }
129
130 if (id == NULL) {
131 free_rpc_construct_nvme(&req);
132 return;
133 }
134
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]);
139 }
140 spdk_json_write_array_end(w);
141 spdk_jsonrpc_end_result(conn, w);
142
143 free_rpc_construct_nvme(&req);
144
145 return;
146
147 invalid:
148 spdk_jsonrpc_send_error_response(conn, id, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
149 free_rpc_construct_nvme(&req);
150 }
151 SPDK_RPC_REGISTER("construct_nvme_bdev", spdk_rpc_construct_nvme_bdev)