]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/net/net_rpc.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / lib / net / net_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 "net_internal.h"
35
36 #include "spdk/stdinc.h"
37
38 #include "spdk/rpc.h"
39 #include "spdk/net.h"
40 #include "spdk/util.h"
41
42 #include "spdk_internal/log.h"
43
44 struct rpc_ip_address {
45 int32_t ifc_index;
46 char *ip_address;
47 };
48
49 static void
50 free_rpc_ip_address(struct rpc_ip_address *req)
51 {
52 free(req->ip_address);
53 }
54
55 static const struct spdk_json_object_decoder rpc_ip_address_decoders[] = {
56 {"ifc_index", offsetof(struct rpc_ip_address, ifc_index), spdk_json_decode_int32},
57 {"ip_address", offsetof(struct rpc_ip_address, ip_address), spdk_json_decode_string},
58 };
59
60 static void
61 spdk_rpc_add_ip_address(struct spdk_jsonrpc_request *request,
62 const struct spdk_json_val *params)
63 {
64 struct rpc_ip_address req = {};
65 struct spdk_json_write_ctx *w;
66
67 if (spdk_json_decode_object(params, rpc_ip_address_decoders,
68 SPDK_COUNTOF(rpc_ip_address_decoders),
69 &req)) {
70 SPDK_DEBUGLOG(SPDK_LOG_NET, "spdk_json_decode_object failed\n");
71 goto invalid;
72 }
73
74 if (spdk_interface_add_ip_address(req.ifc_index, req.ip_address)) {
75 goto invalid;
76 }
77
78 free_rpc_ip_address(&req);
79
80 w = spdk_jsonrpc_begin_result(request);
81 if (w == NULL) {
82 return;
83 }
84
85 spdk_json_write_bool(w, true);
86 spdk_jsonrpc_end_result(request, w);
87 return;
88
89 invalid:
90 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
91 free_rpc_ip_address(&req);
92 }
93 SPDK_RPC_REGISTER("add_ip_address", spdk_rpc_add_ip_address, SPDK_RPC_RUNTIME)
94
95 static void
96 spdk_rpc_delete_ip_address(struct spdk_jsonrpc_request *request,
97 const struct spdk_json_val *params)
98 {
99 struct rpc_ip_address req = {};
100 struct spdk_json_write_ctx *w;
101
102 if (spdk_json_decode_object(params, rpc_ip_address_decoders,
103 SPDK_COUNTOF(rpc_ip_address_decoders),
104 &req)) {
105 SPDK_DEBUGLOG(SPDK_LOG_NET, "spdk_json_decode_object failed\n");
106 goto invalid;
107 }
108
109 if (spdk_interface_delete_ip_address(req.ifc_index, req.ip_address)) {
110 goto invalid;
111 }
112
113 free_rpc_ip_address(&req);
114
115 w = spdk_jsonrpc_begin_result(request);
116 if (w == NULL) {
117 return;
118 }
119
120 spdk_json_write_bool(w, true);
121 spdk_jsonrpc_end_result(request, w);
122 return;
123
124 invalid:
125 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
126 free_rpc_ip_address(&req);
127 }
128 SPDK_RPC_REGISTER("delete_ip_address", spdk_rpc_delete_ip_address, SPDK_RPC_RUNTIME)
129
130 static void
131 spdk_rpc_get_interfaces(struct spdk_jsonrpc_request *request,
132 const struct spdk_json_val *params)
133 {
134 struct spdk_json_write_ctx *w;
135 TAILQ_HEAD(, spdk_interface) *interface_head = spdk_interface_get_list();
136 struct spdk_interface *ifc;
137 char *ip_address;
138 struct in_addr inaddr;
139 uint32_t i;
140
141 if (params != NULL) {
142 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
143 "get_interfaces requires no parameters");
144 return;
145 }
146
147 w = spdk_jsonrpc_begin_result(request);
148 if (w == NULL) {
149 return;
150 }
151
152 spdk_json_write_array_begin(w);
153
154 TAILQ_FOREACH(ifc, interface_head, tailq) {
155 spdk_json_write_object_begin(w);
156
157 spdk_json_write_named_string(w, "name", ifc->name);
158
159 spdk_json_write_named_int32(w, "ifc_index", ifc->index);
160
161 spdk_json_write_named_array_begin(w, "ip_addr");
162 for (i = 0; i < ifc->num_ip_addresses; i++) {
163 memcpy(&inaddr, &ifc->ip_address[i], sizeof(uint32_t));
164 ip_address = inet_ntoa(inaddr);
165 spdk_json_write_string(w, ip_address);
166 }
167 spdk_json_write_array_end(w);
168
169 spdk_json_write_object_end(w);
170 }
171 spdk_json_write_array_end(w);
172
173 spdk_jsonrpc_end_result(request, w);
174 }
175 SPDK_RPC_REGISTER("get_interfaces", spdk_rpc_get_interfaces, SPDK_RPC_RUNTIME)
176
177 SPDK_LOG_REGISTER_COMPONENT("net", SPDK_LOG_NET)