treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / netdevsim / health.c
blob9aa637d162eb767a8a371d6d2fa41592583a07b5
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Mellanox Technologies. All rights reserved */
4 #include <linux/debugfs.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
9 #include "netdevsim.h"
11 static int
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)
16 return 0;
19 static int
20 nsim_dev_empty_reporter_diagnose(struct devlink_health_reporter *reporter,
21 struct devlink_fmsg *fmsg,
22 struct netlink_ext_ack *extack)
24 return 0;
27 static const
28 struct devlink_health_reporter_ops nsim_dev_empty_reporter_ops = {
29 .name = "empty",
30 .dump = nsim_dev_empty_reporter_dump,
31 .diagnose = nsim_dev_empty_reporter_diagnose,
34 struct nsim_dev_dummy_reporter_ctx {
35 char *break_msg;
38 static int
39 nsim_dev_dummy_reporter_recover(struct devlink_health_reporter *reporter,
40 void *priv_ctx,
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");
51 return -EINVAL;
53 if (ctx) {
54 kfree(health->recovered_break_msg);
55 health->recovered_break_msg = kstrdup(ctx->break_msg,
56 GFP_KERNEL);
57 if (!health->recovered_break_msg)
58 return -ENOMEM;
60 return 0;
63 static int nsim_dev_dummy_fmsg_put(struct devlink_fmsg *fmsg, u32 binary_len)
65 char *binary;
66 int err;
67 int i;
69 err = devlink_fmsg_bool_pair_put(fmsg, "test_bool", true);
70 if (err)
71 return err;
72 err = devlink_fmsg_u8_pair_put(fmsg, "test_u8", 1);
73 if (err)
74 return err;
75 err = devlink_fmsg_u32_pair_put(fmsg, "test_u32", 3);
76 if (err)
77 return err;
78 err = devlink_fmsg_u64_pair_put(fmsg, "test_u64", 4);
79 if (err)
80 return err;
81 err = devlink_fmsg_string_pair_put(fmsg, "test_string", "somestring");
82 if (err)
83 return err;
85 binary = kmalloc(binary_len, GFP_KERNEL);
86 if (!binary)
87 return -ENOMEM;
88 get_random_bytes(binary, binary_len);
89 err = devlink_fmsg_binary_pair_put(fmsg, "test_binary", binary, binary_len);
90 kfree(binary);
91 if (err)
92 return err;
94 err = devlink_fmsg_pair_nest_start(fmsg, "test_nest");
95 if (err)
96 return err;
97 err = devlink_fmsg_obj_nest_start(fmsg);
98 if (err)
99 return err;
100 err = devlink_fmsg_bool_pair_put(fmsg, "nested_test_bool", false);
101 if (err)
102 return err;
103 err = devlink_fmsg_u8_pair_put(fmsg, "nested_test_u8", false);
104 if (err)
105 return err;
106 err = devlink_fmsg_obj_nest_end(fmsg);
107 if (err)
108 return err;
109 err = devlink_fmsg_pair_nest_end(fmsg);
110 if (err)
111 return err;
113 err = devlink_fmsg_arr_pair_nest_start(fmsg, "test_bool_array");
114 if (err)
115 return err;
116 for (i = 0; i < 10; i++) {
117 err = devlink_fmsg_bool_put(fmsg, true);
118 if (err)
119 return err;
121 err = devlink_fmsg_arr_pair_nest_end(fmsg);
122 if (err)
123 return err;
125 err = devlink_fmsg_arr_pair_nest_start(fmsg, "test_u8_array");
126 if (err)
127 return err;
128 for (i = 0; i < 10; i++) {
129 err = devlink_fmsg_u8_put(fmsg, i);
130 if (err)
131 return err;
133 err = devlink_fmsg_arr_pair_nest_end(fmsg);
134 if (err)
135 return err;
137 err = devlink_fmsg_arr_pair_nest_start(fmsg, "test_u32_array");
138 if (err)
139 return err;
140 for (i = 0; i < 10; i++) {
141 err = devlink_fmsg_u32_put(fmsg, i);
142 if (err)
143 return err;
145 err = devlink_fmsg_arr_pair_nest_end(fmsg);
146 if (err)
147 return err;
149 err = devlink_fmsg_arr_pair_nest_start(fmsg, "test_u64_array");
150 if (err)
151 return err;
152 for (i = 0; i < 10; i++) {
153 err = devlink_fmsg_u64_put(fmsg, i);
154 if (err)
155 return err;
157 err = devlink_fmsg_arr_pair_nest_end(fmsg);
158 if (err)
159 return err;
161 err = devlink_fmsg_arr_pair_nest_start(fmsg, "test_array_of_objects");
162 if (err)
163 return err;
164 for (i = 0; i < 10; i++) {
165 err = devlink_fmsg_obj_nest_start(fmsg);
166 if (err)
167 return err;
168 err = devlink_fmsg_bool_pair_put(fmsg,
169 "in_array_nested_test_bool",
170 false);
171 if (err)
172 return err;
173 err = devlink_fmsg_u8_pair_put(fmsg,
174 "in_array_nested_test_u8",
176 if (err)
177 return err;
178 err = devlink_fmsg_obj_nest_end(fmsg);
179 if (err)
180 return err;
182 return devlink_fmsg_arr_pair_nest_end(fmsg);
185 static int
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;
192 int err;
194 if (ctx) {
195 err = devlink_fmsg_string_pair_put(fmsg, "break_message",
196 ctx->break_msg);
197 if (err)
198 return err;
200 return nsim_dev_dummy_fmsg_put(fmsg, health->binary_len);
203 static int
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);
209 int err;
211 if (health->recovered_break_msg) {
212 err = devlink_fmsg_string_pair_put(fmsg,
213 "recovered_break_message",
214 health->recovered_break_msg);
215 if (err)
216 return err;
218 return nsim_dev_dummy_fmsg_put(fmsg, health->binary_len);
221 static const
222 struct devlink_health_reporter_ops nsim_dev_dummy_reporter_ops = {
223 .name = "dummy",
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;
235 char *break_msg;
236 int err;
238 break_msg = kmalloc(count + 1, GFP_KERNEL);
239 if (!break_msg)
240 return -ENOMEM;
242 if (copy_from_user(break_msg, data, count)) {
243 err = -EFAULT;
244 goto out;
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);
252 if (err)
253 goto out;
255 out:
256 kfree(break_msg);
257 return err ?: count;
260 static const struct file_operations nsim_dev_health_break_fops = {
261 .open = simple_open,
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;
269 int err;
271 health->empty_reporter =
272 devlink_health_reporter_create(devlink,
273 &nsim_dev_empty_reporter_ops,
274 0, false, health);
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,
281 0, false, health);
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);
302 return 0;
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);
308 return err;
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);