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