1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
5 #include <linux/sunrpc/clnt.h>
6 #include <linux/kobject.h>
7 #include <linux/sunrpc/addr.h>
16 static void free_xprt_addr(struct rcu_head *head)
18 struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu);
24 static struct kset *rpc_sunrpc_kset;
25 static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
27 static void rpc_sysfs_object_release(struct kobject *kobj)
32 static const struct kobj_ns_type_operations *
33 rpc_sysfs_object_child_ns_type(struct kobject *kobj)
35 return &net_ns_type_operations;
38 static 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,
44 static struct kobject *rpc_sysfs_object_alloc(const char *name,
46 struct kobject *parent)
50 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
53 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
54 parent, "%s", name) == 0)
61 static inline struct rpc_xprt *
62 rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj)
64 struct rpc_sysfs_xprt *x = container_of(kobj,
65 struct rpc_sysfs_xprt, kobject);
67 return xprt_get(x->xprt);
70 static inline struct rpc_xprt_switch *
71 rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj)
73 struct rpc_sysfs_xprt_switch *x = container_of(kobj,
74 struct rpc_sysfs_xprt_switch, kobject);
76 return xprt_switch_get(x->xprt_switch);
79 static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
80 struct kobj_attribute *attr,
83 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
88 ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
93 static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
94 struct kobj_attribute *attr,
97 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
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);
114 static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
115 struct kobj_attribute *attr,
118 struct rpc_xprt_switch *xprt_switch =
119 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
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);
131 static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
132 struct kobj_attribute *attr,
133 const char *buf, size_t count)
135 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
136 struct sockaddr *saddr;
139 struct xprt_addr *saved_addr;
144 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
145 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
150 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
154 saddr = (struct sockaddr *)&xprt->addr;
155 port = rpc_get_port(saddr);
157 /* buf_len is the len until the first occurence of either
160 buf_len = strcspn(buf, "\n");
162 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
165 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
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,
174 rpc_set_port(saddr, port);
176 xprt_force_disconnect(xprt);
178 xprt_release_write(xprt, NULL);
189 int rpc_sysfs_init(void)
191 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
192 if (!rpc_sunrpc_kset)
194 rpc_sunrpc_client_kobj =
195 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
196 if (!rpc_sunrpc_client_kobj)
198 rpc_sunrpc_xprt_switch_kobj =
199 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
200 if (!rpc_sunrpc_xprt_switch_kobj)
204 kobject_put(rpc_sunrpc_client_kobj);
205 rpc_sunrpc_client_kobj = NULL;
207 kset_unregister(rpc_sunrpc_kset);
208 rpc_sunrpc_kset = NULL;
212 static void rpc_sysfs_client_release(struct kobject *kobj)
214 struct rpc_sysfs_client *c;
216 c = container_of(kobj, struct rpc_sysfs_client, kobject);
220 static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
222 struct rpc_sysfs_xprt_switch *xprt_switch;
224 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
228 static void rpc_sysfs_xprt_release(struct kobject *kobj)
230 struct rpc_sysfs_xprt *xprt;
232 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
236 static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
238 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
241 static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
243 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
246 static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
248 return container_of(kobj, struct rpc_sysfs_xprt,
249 kobject)->xprt->xprt_net;
252 static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
253 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
255 static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
256 0444, rpc_sysfs_xprt_info_show, NULL);
258 static struct attribute *rpc_sysfs_xprt_attrs[] = {
259 &rpc_sysfs_xprt_dstaddr.attr,
260 &rpc_sysfs_xprt_info.attr,
264 static struct kobj_attribute rpc_sysfs_xprt_switch_info =
265 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
267 static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
268 &rpc_sysfs_xprt_switch_info.attr,
272 static 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,
278 static struct kobj_type rpc_sysfs_xprt_switch_type = {
279 .release = rpc_sysfs_xprt_switch_release,
280 .default_attrs = rpc_sysfs_xprt_switch_attrs,
281 .sysfs_ops = &kobj_sysfs_ops,
282 .namespace = rpc_sysfs_xprt_switch_namespace,
285 static struct kobj_type rpc_sysfs_xprt_type = {
286 .release = rpc_sysfs_xprt_release,
287 .default_attrs = rpc_sysfs_xprt_attrs,
288 .sysfs_ops = &kobj_sysfs_ops,
289 .namespace = rpc_sysfs_xprt_namespace,
292 void rpc_sysfs_exit(void)
294 kobject_put(rpc_sunrpc_client_kobj);
295 kobject_put(rpc_sunrpc_xprt_switch_kobj);
296 kset_unregister(rpc_sunrpc_kset);
299 static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
303 struct rpc_sysfs_client *p;
305 p = kzalloc(sizeof(*p), GFP_KERNEL);
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)
312 kobject_put(&p->kobject);
317 static struct rpc_sysfs_xprt_switch *
318 rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
319 struct rpc_xprt_switch *xprt_switch,
323 struct rpc_sysfs_xprt_switch *p;
325 p = kzalloc(sizeof(*p), gfp_flags);
328 p->kobject.kset = rpc_sunrpc_kset;
329 if (kobject_init_and_add(&p->kobject,
330 &rpc_sysfs_xprt_switch_type,
332 xprt_switch->xps_id) == 0)
334 kobject_put(&p->kobject);
339 static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
340 struct rpc_xprt *xprt,
343 struct rpc_sysfs_xprt *p;
345 p = kzalloc(sizeof(*p), gfp_flags);
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)
353 kobject_put(&p->kobject);
358 void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
359 struct rpc_xprt_switch *xprt_switch,
362 struct rpc_sysfs_client *rpc_client;
364 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
367 char name[] = "switch";
368 struct rpc_sysfs_xprt_switch *xswitch =
369 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
372 clnt->cl_sysfs = rpc_client;
373 rpc_client->clnt = clnt;
374 rpc_client->xprt_switch = xprt_switch;
375 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
376 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
377 &xswitch->kobject, name);
379 pr_warn("can't create link to %s in sysfs (%d)\n",
384 void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
385 struct rpc_xprt *xprt,
388 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
391 if (xprt_switch->xps_net)
392 net = xprt_switch->xps_net;
394 net = xprt->xprt_net;
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);
406 void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
407 struct rpc_xprt *xprt,
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;
414 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
416 xprt->xprt_sysfs = rpc_xprt;
417 rpc_xprt->xprt = xprt;
418 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
422 void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
424 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
427 char name[] = "switch";
429 sysfs_remove_link(&rpc_client->kobject, name);
430 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
431 kobject_del(&rpc_client->kobject);
432 kobject_put(&rpc_client->kobject);
433 clnt->cl_sysfs = NULL;
437 void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
439 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
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;
449 void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
451 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
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;