xref: /spdk/module/accel/mlx5/accel_mlx5_rpc.c (revision 318515b44ec8b67f83bcc9ca83f0c7d5ea919e62)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  */
4 
5 #include "spdk/rpc.h"
6 #include "spdk/util.h"
7 #include "spdk/log.h"
8 
9 #include "accel_mlx5.h"
10 
11 static const struct spdk_json_object_decoder rpc_mlx5_module_decoder[] = {
12 	{"qp_size", offsetof(struct accel_mlx5_attr, qp_size), spdk_json_decode_uint16, true},
13 	{"num_requests", offsetof(struct accel_mlx5_attr, num_requests), spdk_json_decode_uint32, true},
14 	{"allowed_devs", offsetof(struct accel_mlx5_attr, allowed_devs), spdk_json_decode_string, true},
15 	{"crypto_split_blocks", offsetof(struct accel_mlx5_attr, crypto_split_blocks), spdk_json_decode_uint16, true},
16 	{"enable_driver", offsetof(struct accel_mlx5_attr, enable_driver), spdk_json_decode_bool, true},
17 };
18 
19 static void
20 rpc_mlx5_scan_accel_module(struct spdk_jsonrpc_request *request,
21 			   const struct spdk_json_val *params)
22 {
23 	struct accel_mlx5_attr attr = {};
24 	int rc;
25 
26 	accel_mlx5_get_default_attr(&attr);
27 
28 	if (params != NULL) {
29 		if (spdk_json_decode_object(params, rpc_mlx5_module_decoder,
30 					    SPDK_COUNTOF(rpc_mlx5_module_decoder),
31 					    &attr)) {
32 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
33 			free(attr.allowed_devs);
34 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
35 							 "spdk_json_decode_object failed");
36 			return;
37 		}
38 	}
39 
40 	rc = accel_mlx5_enable(&attr);
41 	if (rc) {
42 		spdk_jsonrpc_send_error_response_fmt(request, rc, "mlx5 scan failed with %d", rc);
43 	} else {
44 		spdk_jsonrpc_send_bool_response(request, true);
45 	}
46 	free(attr.allowed_devs);
47 }
48 SPDK_RPC_REGISTER("mlx5_scan_accel_module", rpc_mlx5_scan_accel_module, SPDK_RPC_STARTUP)
49 
50 static int
51 rpc_decode_dump_stat_level(const struct spdk_json_val *val, void *out)
52 {
53 	enum accel_mlx5_dump_state_level *level = out;
54 
55 	if (spdk_json_strequal(val, "total") == true) {
56 		*level = ACCEL_MLX5_DUMP_STAT_LEVEL_TOTAL;
57 	} else if (spdk_json_strequal(val, "channel") == true) {
58 		*level = ACCEL_MLX5_DUMP_STAT_LEVEL_CHANNEL;
59 	} else if (spdk_json_strequal(val, "device") == true) {
60 		*level = ACCEL_MLX5_DUMP_STAT_LEVEL_DEV;
61 	} else {
62 		SPDK_NOTICELOG("Invalid parameter value: level\n");
63 		return -EINVAL;
64 	}
65 
66 	return 0;
67 }
68 
69 struct accel_mlx5_rpc_dump_stats_ctx {
70 	struct spdk_jsonrpc_request *request;
71 	struct spdk_json_write_ctx *w;
72 };
73 
74 static void
75 accel_mlx5_dump_stats_done(void *_ctx, int rc)
76 {
77 	struct accel_mlx5_rpc_dump_stats_ctx *ctx = _ctx;
78 	if (rc) {
79 		spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
80 						 "Failed to dump stats");
81 	} else {
82 		spdk_jsonrpc_end_result(ctx->request, ctx->w);
83 	}
84 	free(ctx);
85 }
86 
87 static const struct spdk_json_object_decoder rpc_accel_mlx5_dump_stats_decoder[] = {
88 	{"level", 0, rpc_decode_dump_stat_level, true},
89 };
90 
91 static void
92 rpc_accel_mlx5_dump_stats(struct spdk_jsonrpc_request *request,
93 			  const struct spdk_json_val *params)
94 {
95 	struct accel_mlx5_rpc_dump_stats_ctx *ctx;
96 	enum accel_mlx5_dump_state_level level = ACCEL_MLX5_DUMP_STAT_LEVEL_CHANNEL;
97 	int rc;
98 
99 	if (params != NULL) {
100 		if (spdk_json_decode_object(params, rpc_accel_mlx5_dump_stats_decoder,
101 					    SPDK_COUNTOF(rpc_accel_mlx5_dump_stats_decoder),
102 					    &level)) {
103 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
104 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
105 							 "spdk_json_decode_object failed");
106 			return;
107 		}
108 	}
109 
110 	ctx = calloc(1, sizeof(*ctx));
111 	if (!ctx) {
112 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
113 						 "memory allocation failed");
114 		return;
115 	}
116 	ctx->request = request;
117 	ctx->w = spdk_jsonrpc_begin_result(ctx->request);
118 	rc = accel_mlx5_dump_stats(ctx->w, level, accel_mlx5_dump_stats_done, ctx);
119 	if (rc) {
120 		spdk_json_write_null(ctx->w);
121 		spdk_jsonrpc_end_result(ctx->request, ctx->w);
122 		free(ctx);
123 	}
124 }
125 SPDK_RPC_REGISTER("accel_mlx5_dump_stats", rpc_accel_mlx5_dump_stats, SPDK_RPC_RUNTIME)
126