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>
8 #include <linux/sunrpc/xprtsock.h>
17 static void free_xprt_addr(struct rcu_head *head)
19 struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu);
25 static struct kset *rpc_sunrpc_kset;
26 static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
28 static void rpc_sysfs_object_release(struct kobject *kobj)
33 static const struct kobj_ns_type_operations *
34 rpc_sysfs_object_child_ns_type(struct kobject *kobj)
36 return &net_ns_type_operations;
39 static struct kobj_type rpc_sysfs_object_type = {
40 .release = rpc_sysfs_object_release,
41 .sysfs_ops = &kobj_sysfs_ops,
42 .child_ns_type = rpc_sysfs_object_child_ns_type,
45 static struct kobject *rpc_sysfs_object_alloc(const char *name,
47 struct kobject *parent)
51 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
54 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
55 parent, "%s", name) == 0)
62 static inline struct rpc_xprt *
63 rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj)
65 struct rpc_sysfs_xprt *x = container_of(kobj,
66 struct rpc_sysfs_xprt, kobject);
68 return xprt_get(x->xprt);
71 static inline struct rpc_xprt_switch *
72 rpc_sysfs_xprt_kobj_get_xprt_switch(struct kobject *kobj)
74 struct rpc_sysfs_xprt *x = container_of(kobj,
75 struct rpc_sysfs_xprt, kobject);
77 return xprt_switch_get(x->xprt_switch);
80 static inline struct rpc_xprt_switch *
81 rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj)
83 struct rpc_sysfs_xprt_switch *x = container_of(kobj,
84 struct rpc_sysfs_xprt_switch, kobject);
86 return xprt_switch_get(x->xprt_switch);
89 static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
90 struct kobj_attribute *attr,
93 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
98 ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
103 static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
104 struct kobj_attribute *attr,
107 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
108 struct sockaddr_storage saddr;
109 struct sock_xprt *sock;
115 sock = container_of(xprt, struct sock_xprt, xprt);
116 if (kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)
119 ret = sprintf(buf, "%pISc\n", &saddr);
125 static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
126 struct kobj_attribute *attr,
129 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
135 ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
136 "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n"
137 "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n"
138 "backlog_q_len=%u\nmain_xprt=%d\nsrc_port=%u\n"
139 "tasks_queuelen=%ld\ndst_port=%s\n",
140 xprt->last_used, xprt->cong, xprt->cwnd, xprt->max_reqs,
141 xprt->min_reqs, xprt->num_reqs, xprt->binding.qlen,
142 xprt->sending.qlen, xprt->pending.qlen,
143 xprt->backlog.qlen, xprt->main,
144 (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
145 get_srcport(xprt) : 0,
146 atomic_long_read(&xprt->queuelen),
147 (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
148 xprt->address_strings[RPC_DISPLAY_PORT] : "0");
153 static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
154 struct kobj_attribute *attr,
157 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
159 int locked, connected, connecting, close_wait, bound, binding,
160 closing, congested, cwnd_wait, write_space, offline, remove;
166 ret = sprintf(buf, "state=CLOSED\n");
168 locked = test_bit(XPRT_LOCKED, &xprt->state);
169 connected = test_bit(XPRT_CONNECTED, &xprt->state);
170 connecting = test_bit(XPRT_CONNECTING, &xprt->state);
171 close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state);
172 bound = test_bit(XPRT_BOUND, &xprt->state);
173 binding = test_bit(XPRT_BINDING, &xprt->state);
174 closing = test_bit(XPRT_CLOSING, &xprt->state);
175 congested = test_bit(XPRT_CONGESTED, &xprt->state);
176 cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state);
177 write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state);
178 offline = test_bit(XPRT_OFFLINE, &xprt->state);
179 remove = test_bit(XPRT_REMOVE, &xprt->state);
181 ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s %s\n",
182 locked ? "LOCKED" : "",
183 connected ? "CONNECTED" : "",
184 connecting ? "CONNECTING" : "",
185 close_wait ? "CLOSE_WAIT" : "",
186 bound ? "BOUND" : "",
187 binding ? "BOUNDING" : "",
188 closing ? "CLOSING" : "",
189 congested ? "CONGESTED" : "",
190 cwnd_wait ? "CWND_WAIT" : "",
191 write_space ? "WRITE_SPACE" : "",
192 offline ? "OFFLINE" : "",
193 remove ? "REMOVE" : "");
200 static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
201 struct kobj_attribute *attr,
204 struct rpc_xprt_switch *xprt_switch =
205 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
210 ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\n"
211 "num_unique_destaddr=%u\nqueue_len=%ld\n",
212 xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
213 xprt_switch->xps_nunique_destaddr_xprts,
214 atomic_long_read(&xprt_switch->xps_queuelen));
215 xprt_switch_put(xprt_switch);
219 static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
220 struct kobj_attribute *attr,
221 const char *buf, size_t count)
223 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
224 struct sockaddr *saddr;
227 struct xprt_addr *saved_addr;
232 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
233 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
238 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
242 saddr = (struct sockaddr *)&xprt->addr;
243 port = rpc_get_port(saddr);
245 /* buf_len is the len until the first occurence of either
248 buf_len = strcspn(buf, "\n");
250 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
253 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
257 rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
258 rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
259 call_rcu(&saved_addr->rcu, free_xprt_addr);
260 xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
262 rpc_set_port(saddr, port);
264 xprt_force_disconnect(xprt);
266 xprt_release_write(xprt, NULL);
277 static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
278 struct kobj_attribute *attr,
279 const char *buf, size_t count)
281 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
282 int offline = 0, online = 0, remove = 0;
283 struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj);
290 if (!strncmp(buf, "offline", 7))
292 else if (!strncmp(buf, "online", 6))
294 else if (!strncmp(buf, "remove", 6))
301 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
310 if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
311 spin_lock(&xps->xps_lock);
313 spin_unlock(&xps->xps_lock);
316 if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
317 spin_lock(&xps->xps_lock);
319 spin_unlock(&xps->xps_lock);
322 if (test_bit(XPRT_OFFLINE, &xprt->state)) {
323 if (!test_and_set_bit(XPRT_REMOVE, &xprt->state)) {
324 xprt_force_disconnect(xprt);
325 if (test_bit(XPRT_CONNECTED, &xprt->state)) {
326 if (!xprt->sending.qlen &&
327 !xprt->pending.qlen &&
328 !xprt->backlog.qlen &&
329 !atomic_long_read(&xprt->queuelen))
330 rpc_xprt_switch_remove_xprt(xps, xprt);
339 xprt_release_write(xprt, NULL);
342 xprt_switch_put(xps);
346 int rpc_sysfs_init(void)
348 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
349 if (!rpc_sunrpc_kset)
351 rpc_sunrpc_client_kobj =
352 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
353 if (!rpc_sunrpc_client_kobj)
355 rpc_sunrpc_xprt_switch_kobj =
356 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
357 if (!rpc_sunrpc_xprt_switch_kobj)
361 kobject_put(rpc_sunrpc_client_kobj);
362 rpc_sunrpc_client_kobj = NULL;
364 kset_unregister(rpc_sunrpc_kset);
365 rpc_sunrpc_kset = NULL;
369 static void rpc_sysfs_client_release(struct kobject *kobj)
371 struct rpc_sysfs_client *c;
373 c = container_of(kobj, struct rpc_sysfs_client, kobject);
377 static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
379 struct rpc_sysfs_xprt_switch *xprt_switch;
381 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
385 static void rpc_sysfs_xprt_release(struct kobject *kobj)
387 struct rpc_sysfs_xprt *xprt;
389 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
393 static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
395 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
398 static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
400 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
403 static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
405 return container_of(kobj, struct rpc_sysfs_xprt,
406 kobject)->xprt->xprt_net;
409 static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
410 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
412 static struct kobj_attribute rpc_sysfs_xprt_srcaddr = __ATTR(srcaddr,
413 0644, rpc_sysfs_xprt_srcaddr_show, NULL);
415 static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
416 0444, rpc_sysfs_xprt_info_show, NULL);
418 static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state,
419 0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change);
421 static struct attribute *rpc_sysfs_xprt_attrs[] = {
422 &rpc_sysfs_xprt_dstaddr.attr,
423 &rpc_sysfs_xprt_srcaddr.attr,
424 &rpc_sysfs_xprt_info.attr,
425 &rpc_sysfs_xprt_change_state.attr,
429 static struct kobj_attribute rpc_sysfs_xprt_switch_info =
430 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
432 static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
433 &rpc_sysfs_xprt_switch_info.attr,
437 static struct kobj_type rpc_sysfs_client_type = {
438 .release = rpc_sysfs_client_release,
439 .sysfs_ops = &kobj_sysfs_ops,
440 .namespace = rpc_sysfs_client_namespace,
443 static struct kobj_type rpc_sysfs_xprt_switch_type = {
444 .release = rpc_sysfs_xprt_switch_release,
445 .default_attrs = rpc_sysfs_xprt_switch_attrs,
446 .sysfs_ops = &kobj_sysfs_ops,
447 .namespace = rpc_sysfs_xprt_switch_namespace,
450 static struct kobj_type rpc_sysfs_xprt_type = {
451 .release = rpc_sysfs_xprt_release,
452 .default_attrs = rpc_sysfs_xprt_attrs,
453 .sysfs_ops = &kobj_sysfs_ops,
454 .namespace = rpc_sysfs_xprt_namespace,
457 void rpc_sysfs_exit(void)
459 kobject_put(rpc_sunrpc_client_kobj);
460 kobject_put(rpc_sunrpc_xprt_switch_kobj);
461 kset_unregister(rpc_sunrpc_kset);
464 static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
468 struct rpc_sysfs_client *p;
470 p = kzalloc(sizeof(*p), GFP_KERNEL);
473 p->kobject.kset = rpc_sunrpc_kset;
474 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
475 parent, "clnt-%d", clid) == 0)
477 kobject_put(&p->kobject);
482 static struct rpc_sysfs_xprt_switch *
483 rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
484 struct rpc_xprt_switch *xprt_switch,
488 struct rpc_sysfs_xprt_switch *p;
490 p = kzalloc(sizeof(*p), gfp_flags);
493 p->kobject.kset = rpc_sunrpc_kset;
494 if (kobject_init_and_add(&p->kobject,
495 &rpc_sysfs_xprt_switch_type,
497 xprt_switch->xps_id) == 0)
499 kobject_put(&p->kobject);
504 static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
505 struct rpc_xprt *xprt,
508 struct rpc_sysfs_xprt *p;
510 p = kzalloc(sizeof(*p), gfp_flags);
513 p->kobject.kset = rpc_sunrpc_kset;
514 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
515 parent, "xprt-%d-%s", xprt->id,
516 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
518 kobject_put(&p->kobject);
523 void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
524 struct rpc_xprt_switch *xprt_switch,
527 struct rpc_sysfs_client *rpc_client;
528 struct rpc_sysfs_xprt_switch *xswitch =
529 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
534 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
537 char name[] = "switch";
540 clnt->cl_sysfs = rpc_client;
541 rpc_client->clnt = clnt;
542 rpc_client->xprt_switch = xprt_switch;
543 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
544 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
545 &xswitch->kobject, name);
547 pr_warn("can't create link to %s in sysfs (%d)\n",
552 void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
553 struct rpc_xprt *xprt,
556 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
559 if (xprt_switch->xps_net)
560 net = xprt_switch->xps_net;
562 net = xprt->xprt_net;
564 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
565 xprt_switch, net, gfp_flags);
566 if (rpc_xprt_switch) {
567 xprt_switch->xps_sysfs = rpc_xprt_switch;
568 rpc_xprt_switch->xprt_switch = xprt_switch;
569 rpc_xprt_switch->xprt = xprt;
570 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
572 xprt_switch->xps_sysfs = NULL;
576 void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
577 struct rpc_xprt *xprt,
580 struct rpc_sysfs_xprt *rpc_xprt;
581 struct rpc_sysfs_xprt_switch *switch_obj =
582 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
587 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
589 xprt->xprt_sysfs = rpc_xprt;
590 rpc_xprt->xprt = xprt;
591 rpc_xprt->xprt_switch = xprt_switch;
592 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
596 void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
598 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
601 char name[] = "switch";
603 sysfs_remove_link(&rpc_client->kobject, name);
604 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
605 kobject_del(&rpc_client->kobject);
606 kobject_put(&rpc_client->kobject);
607 clnt->cl_sysfs = NULL;
611 void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
613 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
615 if (rpc_xprt_switch) {
616 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
617 kobject_del(&rpc_xprt_switch->kobject);
618 kobject_put(&rpc_xprt_switch->kobject);
619 xprt_switch->xps_sysfs = NULL;
623 void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
625 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
628 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
629 kobject_del(&rpc_xprt->kobject);
630 kobject_put(&rpc_xprt->kobject);
631 xprt->xprt_sysfs = NULL;