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.
34 #include "net_internal.h"
36 #include "spdk/stdinc.h"
40 #include "spdk/util.h"
42 #include "spdk_internal/log.h"
44 struct rpc_ip_address
{
50 free_rpc_ip_address(struct rpc_ip_address
*req
)
52 free(req
->ip_address
);
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
},
61 spdk_rpc_add_ip_address(struct spdk_jsonrpc_request
*request
,
62 const struct spdk_json_val
*params
)
64 struct rpc_ip_address req
= {};
65 struct spdk_json_write_ctx
*w
;
67 if (spdk_json_decode_object(params
, rpc_ip_address_decoders
,
68 SPDK_COUNTOF(rpc_ip_address_decoders
),
70 SPDK_DEBUGLOG(SPDK_LOG_NET
, "spdk_json_decode_object failed\n");
74 if (spdk_interface_add_ip_address(req
.ifc_index
, req
.ip_address
)) {
78 free_rpc_ip_address(&req
);
80 w
= spdk_jsonrpc_begin_result(request
);
85 spdk_json_write_bool(w
, true);
86 spdk_jsonrpc_end_result(request
, w
);
90 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
, "Invalid parameters");
91 free_rpc_ip_address(&req
);
93 SPDK_RPC_REGISTER("add_ip_address", spdk_rpc_add_ip_address
, SPDK_RPC_RUNTIME
)
96 spdk_rpc_delete_ip_address(struct spdk_jsonrpc_request
*request
,
97 const struct spdk_json_val
*params
)
99 struct rpc_ip_address req
= {};
100 struct spdk_json_write_ctx
*w
;
102 if (spdk_json_decode_object(params
, rpc_ip_address_decoders
,
103 SPDK_COUNTOF(rpc_ip_address_decoders
),
105 SPDK_DEBUGLOG(SPDK_LOG_NET
, "spdk_json_decode_object failed\n");
109 if (spdk_interface_delete_ip_address(req
.ifc_index
, req
.ip_address
)) {
113 free_rpc_ip_address(&req
);
115 w
= spdk_jsonrpc_begin_result(request
);
120 spdk_json_write_bool(w
, true);
121 spdk_jsonrpc_end_result(request
, w
);
125 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
, "Invalid parameters");
126 free_rpc_ip_address(&req
);
128 SPDK_RPC_REGISTER("delete_ip_address", spdk_rpc_delete_ip_address
, SPDK_RPC_RUNTIME
)
131 spdk_rpc_get_interfaces(struct spdk_jsonrpc_request
*request
,
132 const struct spdk_json_val
*params
)
134 struct spdk_json_write_ctx
*w
;
135 TAILQ_HEAD(, spdk_interface
) *interface_head
= spdk_interface_get_list();
136 struct spdk_interface
*ifc
;
138 struct in_addr inaddr
;
141 if (params
!= NULL
) {
142 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
143 "get_interfaces requires no parameters");
147 w
= spdk_jsonrpc_begin_result(request
);
152 spdk_json_write_array_begin(w
);
154 TAILQ_FOREACH(ifc
, interface_head
, tailq
) {
155 spdk_json_write_object_begin(w
);
157 spdk_json_write_named_string(w
, "name", ifc
->name
);
159 spdk_json_write_named_int32(w
, "ifc_index", ifc
->index
);
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
);
167 spdk_json_write_array_end(w
);
169 spdk_json_write_object_end(w
);
171 spdk_json_write_array_end(w
);
173 spdk_jsonrpc_end_result(request
, w
);
175 SPDK_RPC_REGISTER("get_interfaces", spdk_rpc_get_interfaces
, SPDK_RPC_RUNTIME
)
177 SPDK_LOG_REGISTER_COMPONENT("net", SPDK_LOG_NET
)