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 |
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 |
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 |
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(objp: health->recovered_break_msg); |
55 | health->recovered_break_msg = kstrdup(s: ctx->break_msg, |
56 | GFP_KERNEL); |
57 | if (!health->recovered_break_msg) |
58 | return -ENOMEM; |
59 | } |
60 | return 0; |
61 | } |
62 | |
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, name: "test_bool" , value: true); |
69 | devlink_fmsg_u8_pair_put(fmsg, name: "test_u8" , value: 1); |
70 | devlink_fmsg_u32_pair_put(fmsg, name: "test_u32" , value: 3); |
71 | devlink_fmsg_u64_pair_put(fmsg, name: "test_u64" , value: 4); |
72 | devlink_fmsg_string_pair_put(fmsg, name: "test_string" , value: "somestring" ); |
73 | |
74 | binary = kmalloc(size: binary_len, GFP_KERNEL | __GFP_NOWARN); |
75 | if (!binary) |
76 | return -ENOMEM; |
77 | get_random_bytes(buf: binary, len: binary_len); |
78 | devlink_fmsg_binary_pair_put(fmsg, name: "test_binary" , value: binary, value_len: binary_len); |
79 | kfree(objp: binary); |
80 | |
81 | devlink_fmsg_pair_nest_start(fmsg, name: "test_nest" ); |
82 | devlink_fmsg_obj_nest_start(fmsg); |
83 | devlink_fmsg_bool_pair_put(fmsg, name: "nested_test_bool" , value: false); |
84 | devlink_fmsg_u8_pair_put(fmsg, name: "nested_test_u8" , value: 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, name: "test_u32_array" ); |
89 | |
90 | for (i = 0; i < 10; i++) |
91 | devlink_fmsg_u32_put(fmsg, value: i); |
92 | devlink_fmsg_arr_pair_nest_end(fmsg); |
93 | devlink_fmsg_arr_pair_nest_start(fmsg, name: "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, name: "in_array_nested_test_bool" , |
98 | value: false); |
99 | devlink_fmsg_u8_pair_put(fmsg, name: "in_array_nested_test_u8" , value: 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 |
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, name: "break_message" , value: ctx->break_msg); |
117 | |
118 | return nsim_dev_dummy_fmsg_put(fmsg, binary_len: health->binary_len); |
119 | } |
120 | |
121 | static int |
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, name: "recovered_break_message" , |
130 | value: health->recovered_break_msg); |
131 | |
132 | return nsim_dev_dummy_fmsg_put(fmsg, binary_len: 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 | |
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 | break_msg = memdup_user_nul(data, count); |
153 | if (IS_ERR(ptr: break_msg)) |
154 | return PTR_ERR(ptr: break_msg); |
155 | |
156 | if (break_msg[count - 1] == '\n') |
157 | break_msg[count - 1] = '\0'; |
158 | |
159 | ctx.break_msg = break_msg; |
160 | err = devlink_health_report(reporter: health->dummy_reporter, msg: break_msg, priv_ctx: &ctx); |
161 | if (err) |
162 | goto out; |
163 | |
164 | out: |
165 | kfree(objp: break_msg); |
166 | return err ?: count; |
167 | } |
168 | |
169 | static const struct file_operations nsim_dev_health_break_fops = { |
170 | .open = simple_open, |
171 | .write = nsim_dev_health_break_write, |
172 | .llseek = generic_file_llseek, |
173 | .owner = THIS_MODULE, |
174 | }; |
175 | |
176 | int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink) |
177 | { |
178 | struct nsim_dev_health *health = &nsim_dev->health; |
179 | int err; |
180 | |
181 | health->empty_reporter = |
182 | devl_health_reporter_create(devlink, |
183 | ops: &nsim_dev_empty_reporter_ops, |
184 | graceful_period: 0, priv: health); |
185 | if (IS_ERR(ptr: health->empty_reporter)) |
186 | return PTR_ERR(ptr: health->empty_reporter); |
187 | |
188 | health->dummy_reporter = |
189 | devl_health_reporter_create(devlink, |
190 | ops: &nsim_dev_dummy_reporter_ops, |
191 | graceful_period: 0, priv: health); |
192 | if (IS_ERR(ptr: health->dummy_reporter)) { |
193 | err = PTR_ERR(ptr: health->dummy_reporter); |
194 | goto err_empty_reporter_destroy; |
195 | } |
196 | |
197 | health->ddir = debugfs_create_dir(name: "health" , parent: nsim_dev->ddir); |
198 | if (IS_ERR(ptr: health->ddir)) { |
199 | err = PTR_ERR(ptr: health->ddir); |
200 | goto err_dummy_reporter_destroy; |
201 | } |
202 | |
203 | health->recovered_break_msg = NULL; |
204 | debugfs_create_file(name: "break_health" , mode: 0200, parent: health->ddir, data: health, |
205 | fops: &nsim_dev_health_break_fops); |
206 | health->binary_len = 16; |
207 | debugfs_create_u32(name: "binary_len" , mode: 0600, parent: health->ddir, |
208 | value: &health->binary_len); |
209 | health->fail_recover = false; |
210 | debugfs_create_bool(name: "fail_recover" , mode: 0600, parent: health->ddir, |
211 | value: &health->fail_recover); |
212 | return 0; |
213 | |
214 | err_dummy_reporter_destroy: |
215 | devl_health_reporter_destroy(reporter: health->dummy_reporter); |
216 | err_empty_reporter_destroy: |
217 | devl_health_reporter_destroy(reporter: health->empty_reporter); |
218 | return err; |
219 | } |
220 | |
221 | void nsim_dev_health_exit(struct nsim_dev *nsim_dev) |
222 | { |
223 | struct nsim_dev_health *health = &nsim_dev->health; |
224 | |
225 | debugfs_remove_recursive(dentry: health->ddir); |
226 | kfree(objp: health->recovered_break_msg); |
227 | devl_health_reporter_destroy(reporter: health->dummy_reporter); |
228 | devl_health_reporter_destroy(reporter: health->empty_reporter); |
229 | } |
230 | |