xref: /linux/drivers/net/netdevsim/health.c (revision 8faabc041a001140564f718dabe37753e88b37fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/debugfs.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 
9 #include "netdevsim.h"
10 
11 static int
nsim_dev_empty_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)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)
15 {
16 	return 0;
17 }
18 
19 static int
nsim_dev_empty_reporter_diagnose(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,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)
23 {
24 	return 0;
25 }
26 
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,
32 };
33 
34 struct nsim_dev_dummy_reporter_ctx {
35 	char *break_msg;
36 };
37 
38 static int
nsim_dev_dummy_reporter_recover(struct devlink_health_reporter * reporter,void * priv_ctx,struct netlink_ext_ack * extack)39 nsim_dev_dummy_reporter_recover(struct devlink_health_reporter *reporter,
40 				void *priv_ctx,
41 				struct netlink_ext_ack *extack)
42 {
43 	struct nsim_dev_health *health = devlink_health_reporter_priv(reporter);
44 	struct nsim_dev_dummy_reporter_ctx *ctx = priv_ctx;
45 
46 	if (health->fail_recover) {
47 		/* For testing purposes, user set debugfs fail_recover
48 		 * value to true. Fail right away.
49 		 */
50 		NL_SET_ERR_MSG_MOD(extack, "User setup the recover to fail for testing purposes");
51 		return -EINVAL;
52 	}
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;
59 	}
60 	return 0;
61 }
62 
nsim_dev_dummy_fmsg_put(struct devlink_fmsg * fmsg,u32 binary_len)63 static int nsim_dev_dummy_fmsg_put(struct devlink_fmsg *fmsg, u32 binary_len)
64 {
65 	char *binary;
66 	int i;
67 
68 	devlink_fmsg_bool_pair_put(fmsg, "test_bool", true);
69 	devlink_fmsg_u8_pair_put(fmsg, "test_u8", 1);
70 	devlink_fmsg_u32_pair_put(fmsg, "test_u32", 3);
71 	devlink_fmsg_u64_pair_put(fmsg, "test_u64", 4);
72 	devlink_fmsg_string_pair_put(fmsg, "test_string", "somestring");
73 
74 	binary = kmalloc(binary_len, GFP_KERNEL | __GFP_NOWARN);
75 	if (!binary)
76 		return -ENOMEM;
77 	get_random_bytes(binary, binary_len);
78 	devlink_fmsg_binary_pair_put(fmsg, "test_binary", binary, binary_len);
79 	kfree(binary);
80 
81 	devlink_fmsg_pair_nest_start(fmsg, "test_nest");
82 	devlink_fmsg_obj_nest_start(fmsg);
83 	devlink_fmsg_bool_pair_put(fmsg, "nested_test_bool", false);
84 	devlink_fmsg_u8_pair_put(fmsg, "nested_test_u8", false);
85 	devlink_fmsg_obj_nest_end(fmsg);
86 	devlink_fmsg_pair_nest_end(fmsg);
87 	devlink_fmsg_arr_pair_nest_end(fmsg);
88 	devlink_fmsg_arr_pair_nest_start(fmsg, "test_u32_array");
89 
90 	for (i = 0; i < 10; i++)
91 		devlink_fmsg_u32_put(fmsg, i);
92 	devlink_fmsg_arr_pair_nest_end(fmsg);
93 	devlink_fmsg_arr_pair_nest_start(fmsg, "test_array_of_objects");
94 
95 	for (i = 0; i < 10; i++) {
96 		devlink_fmsg_obj_nest_start(fmsg);
97 		devlink_fmsg_bool_pair_put(fmsg, "in_array_nested_test_bool",
98 					   false);
99 		devlink_fmsg_u8_pair_put(fmsg, "in_array_nested_test_u8", i);
100 		devlink_fmsg_obj_nest_end(fmsg);
101 	}
102 	devlink_fmsg_arr_pair_nest_end(fmsg);
103 
104 	return 0;
105 }
106 
107 static int
nsim_dev_dummy_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)108 nsim_dev_dummy_reporter_dump(struct devlink_health_reporter *reporter,
109 			     struct devlink_fmsg *fmsg, void *priv_ctx,
110 			     struct netlink_ext_ack *extack)
111 {
112 	struct nsim_dev_health *health = devlink_health_reporter_priv(reporter);
113 	struct nsim_dev_dummy_reporter_ctx *ctx = priv_ctx;
114 
115 	if (ctx)
116 		devlink_fmsg_string_pair_put(fmsg, "break_message", ctx->break_msg);
117 
118 	return nsim_dev_dummy_fmsg_put(fmsg, health->binary_len);
119 }
120 
121 static int
nsim_dev_dummy_reporter_diagnose(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,struct netlink_ext_ack * extack)122 nsim_dev_dummy_reporter_diagnose(struct devlink_health_reporter *reporter,
123 				 struct devlink_fmsg *fmsg,
124 				 struct netlink_ext_ack *extack)
125 {
126 	struct nsim_dev_health *health = devlink_health_reporter_priv(reporter);
127 
128 	if (health->recovered_break_msg)
129 		devlink_fmsg_string_pair_put(fmsg, "recovered_break_message",
130 					     health->recovered_break_msg);
131 
132 	return nsim_dev_dummy_fmsg_put(fmsg, health->binary_len);
133 }
134 
135 static const
136 struct devlink_health_reporter_ops nsim_dev_dummy_reporter_ops = {
137 	.name = "dummy",
138 	.recover = nsim_dev_dummy_reporter_recover,
139 	.dump = nsim_dev_dummy_reporter_dump,
140 	.diagnose = nsim_dev_dummy_reporter_diagnose,
141 };
142 
nsim_dev_health_break_write(struct file * file,const char __user * data,size_t count,loff_t * ppos)143 static ssize_t nsim_dev_health_break_write(struct file *file,
144 					   const char __user *data,
145 					   size_t count, loff_t *ppos)
146 {
147 	struct nsim_dev_health *health = file->private_data;
148 	struct nsim_dev_dummy_reporter_ctx ctx;
149 	char *break_msg;
150 	int err;
151 
152 	if (count == 0 || count > PAGE_SIZE)
153 		return -EINVAL;
154 	break_msg = memdup_user_nul(data, count);
155 	if (IS_ERR(break_msg))
156 		return PTR_ERR(break_msg);
157 
158 	if (break_msg[count - 1] == '\n')
159 		break_msg[count - 1] = '\0';
160 
161 	ctx.break_msg = break_msg;
162 	err = devlink_health_report(health->dummy_reporter, break_msg, &ctx);
163 	if (err)
164 		goto out;
165 
166 out:
167 	kfree(break_msg);
168 	return err ?: count;
169 }
170 
171 static const struct file_operations nsim_dev_health_break_fops = {
172 	.open = simple_open,
173 	.write = nsim_dev_health_break_write,
174 	.llseek = generic_file_llseek,
175 	.owner = THIS_MODULE,
176 };
177 
nsim_dev_health_init(struct nsim_dev * nsim_dev,struct devlink * devlink)178 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink)
179 {
180 	struct nsim_dev_health *health = &nsim_dev->health;
181 	int err;
182 
183 	health->empty_reporter =
184 		devl_health_reporter_create(devlink,
185 					    &nsim_dev_empty_reporter_ops,
186 					    0, health);
187 	if (IS_ERR(health->empty_reporter))
188 		return PTR_ERR(health->empty_reporter);
189 
190 	health->dummy_reporter =
191 		devl_health_reporter_create(devlink,
192 					    &nsim_dev_dummy_reporter_ops,
193 					    0, health);
194 	if (IS_ERR(health->dummy_reporter)) {
195 		err = PTR_ERR(health->dummy_reporter);
196 		goto err_empty_reporter_destroy;
197 	}
198 
199 	health->ddir = debugfs_create_dir("health", nsim_dev->ddir);
200 	if (IS_ERR(health->ddir)) {
201 		err = PTR_ERR(health->ddir);
202 		goto err_dummy_reporter_destroy;
203 	}
204 
205 	health->recovered_break_msg = NULL;
206 	debugfs_create_file("break_health", 0200, health->ddir, health,
207 			    &nsim_dev_health_break_fops);
208 	health->binary_len = 16;
209 	debugfs_create_u32("binary_len", 0600, health->ddir,
210 			   &health->binary_len);
211 	health->fail_recover = false;
212 	debugfs_create_bool("fail_recover", 0600, health->ddir,
213 			    &health->fail_recover);
214 	return 0;
215 
216 err_dummy_reporter_destroy:
217 	devl_health_reporter_destroy(health->dummy_reporter);
218 err_empty_reporter_destroy:
219 	devl_health_reporter_destroy(health->empty_reporter);
220 	return err;
221 }
222 
nsim_dev_health_exit(struct nsim_dev * nsim_dev)223 void nsim_dev_health_exit(struct nsim_dev *nsim_dev)
224 {
225 	struct nsim_dev_health *health = &nsim_dev->health;
226 
227 	debugfs_remove_recursive(health->ddir);
228 	kfree(health->recovered_break_msg);
229 	devl_health_reporter_destroy(health->dummy_reporter);
230 	devl_health_reporter_destroy(health->empty_reporter);
231 }
232