xref: /spdk/test/app/fuzz/vhost_fuzz/vhost_fuzz_rpc.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2018 Mellanox Technologies LTD. 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 "spdk/stdinc.h"
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 
38 #include "vhost_fuzz.h"
39 
40 struct rpc_fuzz_vhost_dev_create {
41 	char	*socket;
42 	bool	is_blk;
43 	bool	use_bogus_buffer;
44 	bool	use_valid_buffer;
45 	bool	valid_lun;
46 	bool	test_scsi_tmf;
47 };
48 
49 static const struct spdk_json_object_decoder rpc_fuzz_vhost_dev_create_decoders[] = {
50 	{"socket", offsetof(struct rpc_fuzz_vhost_dev_create, socket), spdk_json_decode_string},
51 	{"is_blk", offsetof(struct rpc_fuzz_vhost_dev_create, is_blk), spdk_json_decode_bool, true},
52 	{"use_bogus_buffer", offsetof(struct rpc_fuzz_vhost_dev_create, use_bogus_buffer), spdk_json_decode_bool, true},
53 	{"use_valid_buffer", offsetof(struct rpc_fuzz_vhost_dev_create, use_valid_buffer), spdk_json_decode_bool, true},
54 	{"valid_lun", offsetof(struct rpc_fuzz_vhost_dev_create, valid_lun), spdk_json_decode_bool, true},
55 	{"test_scsi_tmf", offsetof(struct rpc_fuzz_vhost_dev_create, test_scsi_tmf), spdk_json_decode_bool, true},
56 };
57 
58 static void
59 spdk_rpc_fuzz_vhost_create_dev(struct spdk_jsonrpc_request *request,
60 			       const struct spdk_json_val *params)
61 {
62 	struct rpc_fuzz_vhost_dev_create req = {0};
63 	int rc;
64 
65 	if (spdk_json_decode_object(params, rpc_fuzz_vhost_dev_create_decoders,
66 				    SPDK_COUNTOF(rpc_fuzz_vhost_dev_create_decoders), &req)) {
67 		fprintf(stderr, "Unable to parse the request.\n");
68 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
69 						 "Unable to parse the object parameters.\n");
70 		return;
71 	}
72 
73 	if (strlen(req.socket) > PATH_MAX) {
74 		fprintf(stderr, "Socket address is too long.\n");
75 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
76 						 "Unable to parse the object parameters.\n");
77 		free(req.socket);
78 		return;
79 	}
80 
81 	rc = fuzz_vhost_dev_init(req.socket, req.is_blk, req.use_bogus_buffer, req.use_valid_buffer,
82 				 req.valid_lun, req.test_scsi_tmf);
83 
84 	if (rc != 0) {
85 		if (rc == -ENOMEM) {
86 			fprintf(stderr, "No valid memory for device initialization.\n");
87 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
88 							 "No memory returned from host.\n");
89 		} else if (rc == -EINVAL) {
90 			fprintf(stderr, "Invalid device parameters provided.\n");
91 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
92 							 "Parameters provided were invalid.\n");
93 		} else {
94 			fprintf(stderr, "unknown error from the guest.\n");
95 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
96 							 "Unexpected error code.\n");
97 		}
98 	} else {
99 		spdk_jsonrpc_send_bool_response(request, true);
100 	}
101 
102 	free(req.socket);
103 	return;
104 }
105 SPDK_RPC_REGISTER("fuzz_vhost_create_dev", spdk_rpc_fuzz_vhost_create_dev, SPDK_RPC_STARTUP);
106