1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Mellanox Technologies. All rights reserved */
4 #include <linux/debugfs.h>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
12 nsim_dev_empty_reporter_dump(struct devlink_health_reporter
*reporter
,
13 struct devlink_fmsg
*fmsg
, void *priv_ctx
,
14 struct netlink_ext_ack
*extack
)
20 nsim_dev_empty_reporter_diagnose(struct devlink_health_reporter
*reporter
,
21 struct devlink_fmsg
*fmsg
,
22 struct netlink_ext_ack
*extack
)
28 struct devlink_health_reporter_ops nsim_dev_empty_reporter_ops
= {
30 .dump
= nsim_dev_empty_reporter_dump
,
31 .diagnose
= nsim_dev_empty_reporter_diagnose
,
34 struct nsim_dev_dummy_reporter_ctx
{
39 nsim_dev_dummy_reporter_recover(struct devlink_health_reporter
*reporter
,
41 struct netlink_ext_ack
*extack
)
43 struct nsim_dev_health
*health
= devlink_health_reporter_priv(reporter
);
44 struct nsim_dev_dummy_reporter_ctx
*ctx
= priv_ctx
;
46 if (health
->fail_recover
) {
47 /* For testing purposes, user set debugfs fail_recover
48 * value to true. Fail right away.
50 NL_SET_ERR_MSG_MOD(extack
, "User setup the recover to fail for testing purposes");
54 kfree(health
->recovered_break_msg
);
55 health
->recovered_break_msg
= kstrdup(ctx
->break_msg
,
57 if (!health
->recovered_break_msg
)
63 static int nsim_dev_dummy_fmsg_put(struct devlink_fmsg
*fmsg
, u32 binary_len
)
69 err
= devlink_fmsg_bool_pair_put(fmsg
, "test_bool", true);
72 err
= devlink_fmsg_u8_pair_put(fmsg
, "test_u8", 1);
75 err
= devlink_fmsg_u32_pair_put(fmsg
, "test_u32", 3);
78 err
= devlink_fmsg_u64_pair_put(fmsg
, "test_u64", 4);
81 err
= devlink_fmsg_string_pair_put(fmsg
, "test_string", "somestring");
85 binary
= kmalloc(binary_len
, GFP_KERNEL
);
88 get_random_bytes(binary
, binary_len
);
89 err
= devlink_fmsg_binary_pair_put(fmsg
, "test_binary", binary
, binary_len
);
94 err
= devlink_fmsg_pair_nest_start(fmsg
, "test_nest");
97 err
= devlink_fmsg_obj_nest_start(fmsg
);
100 err
= devlink_fmsg_bool_pair_put(fmsg
, "nested_test_bool", false);
103 err
= devlink_fmsg_u8_pair_put(fmsg
, "nested_test_u8", false);
106 err
= devlink_fmsg_obj_nest_end(fmsg
);
109 err
= devlink_fmsg_pair_nest_end(fmsg
);
113 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, "test_bool_array");
116 for (i
= 0; i
< 10; i
++) {
117 err
= devlink_fmsg_bool_put(fmsg
, true);
121 err
= devlink_fmsg_arr_pair_nest_end(fmsg
);
125 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, "test_u8_array");
128 for (i
= 0; i
< 10; i
++) {
129 err
= devlink_fmsg_u8_put(fmsg
, i
);
133 err
= devlink_fmsg_arr_pair_nest_end(fmsg
);
137 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, "test_u32_array");
140 for (i
= 0; i
< 10; i
++) {
141 err
= devlink_fmsg_u32_put(fmsg
, i
);
145 err
= devlink_fmsg_arr_pair_nest_end(fmsg
);
149 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, "test_u64_array");
152 for (i
= 0; i
< 10; i
++) {
153 err
= devlink_fmsg_u64_put(fmsg
, i
);
157 err
= devlink_fmsg_arr_pair_nest_end(fmsg
);
161 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, "test_array_of_objects");
164 for (i
= 0; i
< 10; i
++) {
165 err
= devlink_fmsg_obj_nest_start(fmsg
);
168 err
= devlink_fmsg_bool_pair_put(fmsg
,
169 "in_array_nested_test_bool",
173 err
= devlink_fmsg_u8_pair_put(fmsg
,
174 "in_array_nested_test_u8",
178 err
= devlink_fmsg_obj_nest_end(fmsg
);
182 return devlink_fmsg_arr_pair_nest_end(fmsg
);
186 nsim_dev_dummy_reporter_dump(struct devlink_health_reporter
*reporter
,
187 struct devlink_fmsg
*fmsg
, void *priv_ctx
,
188 struct netlink_ext_ack
*extack
)
190 struct nsim_dev_health
*health
= devlink_health_reporter_priv(reporter
);
191 struct nsim_dev_dummy_reporter_ctx
*ctx
= priv_ctx
;
195 err
= devlink_fmsg_string_pair_put(fmsg
, "break_message",
200 return nsim_dev_dummy_fmsg_put(fmsg
, health
->binary_len
);
204 nsim_dev_dummy_reporter_diagnose(struct devlink_health_reporter
*reporter
,
205 struct devlink_fmsg
*fmsg
,
206 struct netlink_ext_ack
*extack
)
208 struct nsim_dev_health
*health
= devlink_health_reporter_priv(reporter
);
211 if (health
->recovered_break_msg
) {
212 err
= devlink_fmsg_string_pair_put(fmsg
,
213 "recovered_break_message",
214 health
->recovered_break_msg
);
218 return nsim_dev_dummy_fmsg_put(fmsg
, health
->binary_len
);
222 struct devlink_health_reporter_ops nsim_dev_dummy_reporter_ops
= {
224 .recover
= nsim_dev_dummy_reporter_recover
,
225 .dump
= nsim_dev_dummy_reporter_dump
,
226 .diagnose
= nsim_dev_dummy_reporter_diagnose
,
229 static ssize_t
nsim_dev_health_break_write(struct file
*file
,
230 const char __user
*data
,
231 size_t count
, loff_t
*ppos
)
233 struct nsim_dev_health
*health
= file
->private_data
;
234 struct nsim_dev_dummy_reporter_ctx ctx
;
238 break_msg
= kmalloc(count
+ 1, GFP_KERNEL
);
242 if (copy_from_user(break_msg
, data
, count
)) {
246 break_msg
[count
] = '\0';
247 if (break_msg
[count
- 1] == '\n')
248 break_msg
[count
- 1] = '\0';
250 ctx
.break_msg
= break_msg
;
251 err
= devlink_health_report(health
->dummy_reporter
, break_msg
, &ctx
);
260 static const struct file_operations nsim_dev_health_break_fops
= {
262 .write
= nsim_dev_health_break_write
,
263 .llseek
= generic_file_llseek
,
266 int nsim_dev_health_init(struct nsim_dev
*nsim_dev
, struct devlink
*devlink
)
268 struct nsim_dev_health
*health
= &nsim_dev
->health
;
271 health
->empty_reporter
=
272 devlink_health_reporter_create(devlink
,
273 &nsim_dev_empty_reporter_ops
,
275 if (IS_ERR(health
->empty_reporter
))
276 return PTR_ERR(health
->empty_reporter
);
278 health
->dummy_reporter
=
279 devlink_health_reporter_create(devlink
,
280 &nsim_dev_dummy_reporter_ops
,
282 if (IS_ERR(health
->dummy_reporter
)) {
283 err
= PTR_ERR(health
->dummy_reporter
);
284 goto err_empty_reporter_destroy
;
287 health
->ddir
= debugfs_create_dir("health", nsim_dev
->ddir
);
288 if (IS_ERR_OR_NULL(health
->ddir
)) {
289 err
= PTR_ERR_OR_ZERO(health
->ddir
) ?: -EINVAL
;
290 goto err_dummy_reporter_destroy
;
293 health
->recovered_break_msg
= NULL
;
294 debugfs_create_file("break_health", 0200, health
->ddir
, health
,
295 &nsim_dev_health_break_fops
);
296 health
->binary_len
= 16;
297 debugfs_create_u32("binary_len", 0600, health
->ddir
,
298 &health
->binary_len
);
299 health
->fail_recover
= false;
300 debugfs_create_bool("fail_recover", 0600, health
->ddir
,
301 &health
->fail_recover
);
304 err_dummy_reporter_destroy
:
305 devlink_health_reporter_destroy(health
->dummy_reporter
);
306 err_empty_reporter_destroy
:
307 devlink_health_reporter_destroy(health
->empty_reporter
);
311 void nsim_dev_health_exit(struct nsim_dev
*nsim_dev
)
313 struct nsim_dev_health
*health
= &nsim_dev
->health
;
315 debugfs_remove_recursive(health
->ddir
);
316 kfree(health
->recovered_break_msg
);
317 devlink_health_reporter_destroy(health
->dummy_reporter
);
318 devlink_health_reporter_destroy(health
->empty_reporter
);