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