blob: b3bc76bbc2a083649e57b6ab30c0bb337e8a09a6 [file] [log] [blame]
Olga Kornievskaia74678742021-06-08 15:59:10 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
4 */
Olga Kornievskaiac441f122021-06-08 15:59:11 -04005#include <linux/sunrpc/clnt.h>
Olga Kornievskaia74678742021-06-08 15:59:10 -04006#include <linux/kobject.h>
Olga Kornievskaia587bc722021-06-08 15:59:19 -04007#include <linux/sunrpc/addr.h>
8
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -04009#include "sysfs.h"
Olga Kornievskaia74678742021-06-08 15:59:10 -040010
Olga Kornievskaia587bc722021-06-08 15:59:19 -040011struct xprt_addr {
12 const char *addr;
13 struct rcu_head rcu;
14};
15
16static void free_xprt_addr(struct rcu_head *head)
17{
18 struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu);
19
20 kfree(addr->addr);
21 kfree(addr);
22}
23
Olga Kornievskaia74678742021-06-08 15:59:10 -040024static struct kset *rpc_sunrpc_kset;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040025static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
Olga Kornievskaiac441f122021-06-08 15:59:11 -040026
27static void rpc_sysfs_object_release(struct kobject *kobj)
28{
29 kfree(kobj);
30}
31
32static const struct kobj_ns_type_operations *
33rpc_sysfs_object_child_ns_type(struct kobject *kobj)
34{
35 return &net_ns_type_operations;
36}
37
38static struct kobj_type rpc_sysfs_object_type = {
39 .release = rpc_sysfs_object_release,
40 .sysfs_ops = &kobj_sysfs_ops,
41 .child_ns_type = rpc_sysfs_object_child_ns_type,
42};
43
44static struct kobject *rpc_sysfs_object_alloc(const char *name,
45 struct kset *kset,
46 struct kobject *parent)
47{
48 struct kobject *kobj;
49
50 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
51 if (kobj) {
52 kobj->kset = kset;
53 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
54 parent, "%s", name) == 0)
55 return kobj;
56 kobject_put(kobj);
57 }
58 return NULL;
59}
Olga Kornievskaia74678742021-06-08 15:59:10 -040060
Olga Kornievskaia587bc722021-06-08 15:59:19 -040061static inline struct rpc_xprt *
62rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj)
63{
64 struct rpc_sysfs_xprt *x = container_of(kobj,
65 struct rpc_sysfs_xprt, kobject);
66
67 return xprt_get(x->xprt);
68}
69
Olga Kornievskaia0e559032021-06-08 15:59:21 -040070static inline struct rpc_xprt_switch *
71rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj)
72{
73 struct rpc_sysfs_xprt_switch *x = container_of(kobj,
74 struct rpc_sysfs_xprt_switch, kobject);
75
76 return xprt_switch_get(x->xprt_switch);
77}
78
Olga Kornievskaia587bc722021-06-08 15:59:19 -040079static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
80 struct kobj_attribute *attr,
81 char *buf)
82{
83 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
84 ssize_t ret;
85
86 if (!xprt)
87 return 0;
88 ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
89 xprt_put(xprt);
90 return ret + 1;
91}
92
Olga Kornievskaia4a096512021-06-08 15:59:20 -040093static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
94 struct kobj_attribute *attr,
95 char *buf)
96{
97 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
98 ssize_t ret;
99
100 if (!xprt)
101 return 0;
102
103 ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
104 "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n"
105 "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n"
106 "backlog_q_len=%u\n", xprt->last_used, xprt->cong,
107 xprt->cwnd, xprt->max_reqs, xprt->min_reqs,
108 xprt->num_reqs, xprt->binding.qlen, xprt->sending.qlen,
109 xprt->pending.qlen, xprt->backlog.qlen);
110 xprt_put(xprt);
111 return ret + 1;
112}
113
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400114static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
115 struct kobj_attribute *attr,
116 char *buf)
117{
118 struct rpc_xprt_switch *xprt_switch =
119 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
120 ssize_t ret;
121
122 if (!xprt_switch)
123 return 0;
124 ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\nqueue_len=%ld\n",
125 xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
126 atomic_long_read(&xprt_switch->xps_queuelen));
127 xprt_switch_put(xprt_switch);
128 return ret + 1;
129}
130
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400131static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
132 struct kobj_attribute *attr,
133 const char *buf, size_t count)
134{
135 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
136 struct sockaddr *saddr;
137 char *dst_addr;
138 int port;
139 struct xprt_addr *saved_addr;
140 size_t buf_len;
141
142 if (!xprt)
143 return 0;
144 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
145 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
146 xprt_put(xprt);
147 return -EOPNOTSUPP;
148 }
149
150 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
151 count = -EINTR;
152 goto out_put;
153 }
154 saddr = (struct sockaddr *)&xprt->addr;
155 port = rpc_get_port(saddr);
156
157 /* buf_len is the len until the first occurence of either
158 * '\n' or '\0'
159 */
160 buf_len = strcspn(buf, "\n");
161
162 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
163 if (!dst_addr)
164 goto out_err;
165 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
166 if (!saved_addr)
167 goto out_err_free;
168 saved_addr->addr =
169 rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
170 rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
171 call_rcu(&saved_addr->rcu, free_xprt_addr);
172 xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
173 sizeof(*saddr));
174 rpc_set_port(saddr, port);
175
176 xprt_force_disconnect(xprt);
177out:
178 xprt_release_write(xprt, NULL);
179out_put:
180 xprt_put(xprt);
181 return count;
182out_err_free:
183 kfree(dst_addr);
184out_err:
185 count = -ENOMEM;
186 goto out;
187}
188
Olga Kornievskaia74678742021-06-08 15:59:10 -0400189int rpc_sysfs_init(void)
190{
191 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
192 if (!rpc_sunrpc_kset)
193 return -ENOMEM;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400194 rpc_sunrpc_client_kobj =
195 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
196 if (!rpc_sunrpc_client_kobj)
197 goto err_client;
198 rpc_sunrpc_xprt_switch_kobj =
199 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
200 if (!rpc_sunrpc_xprt_switch_kobj)
201 goto err_switch;
Olga Kornievskaia74678742021-06-08 15:59:10 -0400202 return 0;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400203err_switch:
204 kobject_put(rpc_sunrpc_client_kobj);
205 rpc_sunrpc_client_kobj = NULL;
206err_client:
207 kset_unregister(rpc_sunrpc_kset);
208 rpc_sunrpc_kset = NULL;
209 return -ENOMEM;
Olga Kornievskaia74678742021-06-08 15:59:10 -0400210}
211
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400212static void rpc_sysfs_client_release(struct kobject *kobj)
213{
214 struct rpc_sysfs_client *c;
215
216 c = container_of(kobj, struct rpc_sysfs_client, kobject);
217 kfree(c);
218}
219
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400220static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
221{
222 struct rpc_sysfs_xprt_switch *xprt_switch;
223
224 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
225 kfree(xprt_switch);
226}
227
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400228static void rpc_sysfs_xprt_release(struct kobject *kobj)
229{
230 struct rpc_sysfs_xprt *xprt;
231
232 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
233 kfree(xprt);
234}
235
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400236static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
237{
238 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
239}
240
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400241static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
242{
243 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
244}
245
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400246static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
247{
248 return container_of(kobj, struct rpc_sysfs_xprt,
249 kobject)->xprt->xprt_net;
250}
251
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400252static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
253 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
254
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400255static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
256 0444, rpc_sysfs_xprt_info_show, NULL);
257
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400258static struct attribute *rpc_sysfs_xprt_attrs[] = {
259 &rpc_sysfs_xprt_dstaddr.attr,
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400260 &rpc_sysfs_xprt_info.attr,
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400261 NULL,
262};
263
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400264static struct kobj_attribute rpc_sysfs_xprt_switch_info =
265 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
266
267static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
268 &rpc_sysfs_xprt_switch_info.attr,
269 NULL,
270};
271
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400272static struct kobj_type rpc_sysfs_client_type = {
273 .release = rpc_sysfs_client_release,
274 .sysfs_ops = &kobj_sysfs_ops,
275 .namespace = rpc_sysfs_client_namespace,
276};
277
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400278static struct kobj_type rpc_sysfs_xprt_switch_type = {
279 .release = rpc_sysfs_xprt_switch_release,
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400280 .default_attrs = rpc_sysfs_xprt_switch_attrs,
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400281 .sysfs_ops = &kobj_sysfs_ops,
282 .namespace = rpc_sysfs_xprt_switch_namespace,
283};
284
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400285static struct kobj_type rpc_sysfs_xprt_type = {
286 .release = rpc_sysfs_xprt_release,
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400287 .default_attrs = rpc_sysfs_xprt_attrs,
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400288 .sysfs_ops = &kobj_sysfs_ops,
289 .namespace = rpc_sysfs_xprt_namespace,
290};
291
Olga Kornievskaia74678742021-06-08 15:59:10 -0400292void rpc_sysfs_exit(void)
293{
Olga Kornievskaiac441f122021-06-08 15:59:11 -0400294 kobject_put(rpc_sunrpc_client_kobj);
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400295 kobject_put(rpc_sunrpc_xprt_switch_kobj);
Olga Kornievskaia74678742021-06-08 15:59:10 -0400296 kset_unregister(rpc_sunrpc_kset);
297}
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400298
299static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
300 struct net *net,
301 int clid)
302{
303 struct rpc_sysfs_client *p;
304
305 p = kzalloc(sizeof(*p), GFP_KERNEL);
306 if (p) {
307 p->net = net;
308 p->kobject.kset = rpc_sunrpc_kset;
309 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
310 parent, "clnt-%d", clid) == 0)
311 return p;
312 kobject_put(&p->kobject);
313 }
314 return NULL;
315}
316
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400317static struct rpc_sysfs_xprt_switch *
318rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
319 struct rpc_xprt_switch *xprt_switch,
320 struct net *net,
321 gfp_t gfp_flags)
322{
323 struct rpc_sysfs_xprt_switch *p;
324
325 p = kzalloc(sizeof(*p), gfp_flags);
326 if (p) {
327 p->net = net;
328 p->kobject.kset = rpc_sunrpc_kset;
329 if (kobject_init_and_add(&p->kobject,
330 &rpc_sysfs_xprt_switch_type,
331 parent, "switch-%d",
332 xprt_switch->xps_id) == 0)
333 return p;
334 kobject_put(&p->kobject);
335 }
336 return NULL;
337}
338
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400339static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
340 struct rpc_xprt *xprt,
341 gfp_t gfp_flags)
342{
343 struct rpc_sysfs_xprt *p;
344
345 p = kzalloc(sizeof(*p), gfp_flags);
346 if (!p)
347 goto out;
348 p->kobject.kset = rpc_sunrpc_kset;
349 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
350 parent, "xprt-%d-%s", xprt->id,
351 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
352 return p;
353 kobject_put(&p->kobject);
354out:
355 return NULL;
356}
357
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400358void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
359 struct rpc_xprt_switch *xprt_switch,
360 struct net *net)
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400361{
362 struct rpc_sysfs_client *rpc_client;
363
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400364 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
365 net, clnt->cl_clid);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400366 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400367 char name[] = "switch";
368 struct rpc_sysfs_xprt_switch *xswitch =
369 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
370 int ret;
371
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400372 clnt->cl_sysfs = rpc_client;
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400373 rpc_client->clnt = clnt;
374 rpc_client->xprt_switch = xprt_switch;
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400375 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400376 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
377 &xswitch->kobject, name);
378 if (ret)
379 pr_warn("can't create link to %s in sysfs (%d)\n",
380 name, ret);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400381 }
382}
383
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400384void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
385 struct rpc_xprt *xprt,
386 gfp_t gfp_flags)
387{
388 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
389 struct net *net;
390
391 if (xprt_switch->xps_net)
392 net = xprt_switch->xps_net;
393 else
394 net = xprt->xprt_net;
395 rpc_xprt_switch =
396 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
397 xprt_switch, net, gfp_flags);
398 if (rpc_xprt_switch) {
399 xprt_switch->xps_sysfs = rpc_xprt_switch;
400 rpc_xprt_switch->xprt_switch = xprt_switch;
401 rpc_xprt_switch->xprt = xprt;
402 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
403 }
404}
405
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400406void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
407 struct rpc_xprt *xprt,
408 gfp_t gfp_flags)
409{
410 struct rpc_sysfs_xprt *rpc_xprt;
411 struct rpc_sysfs_xprt_switch *switch_obj =
412 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
413
414 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
415 if (rpc_xprt) {
416 xprt->xprt_sysfs = rpc_xprt;
417 rpc_xprt->xprt = xprt;
418 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
419 }
420}
421
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400422void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
423{
424 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
425
426 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400427 char name[] = "switch";
428
429 sysfs_remove_link(&rpc_client->kobject, name);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400430 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
431 kobject_del(&rpc_client->kobject);
432 kobject_put(&rpc_client->kobject);
433 clnt->cl_sysfs = NULL;
434 }
435}
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400436
437void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
438{
439 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
440
441 if (rpc_xprt_switch) {
442 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
443 kobject_del(&rpc_xprt_switch->kobject);
444 kobject_put(&rpc_xprt_switch->kobject);
445 xprt_switch->xps_sysfs = NULL;
446 }
447}
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400448
449void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
450{
451 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
452
453 if (rpc_xprt) {
454 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
455 kobject_del(&rpc_xprt->kobject);
456 kobject_put(&rpc_xprt->kobject);
457 xprt->xprt_sysfs = NULL;
458 }
459}