1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com> 4 */ 5 #include <linux/sunrpc/clnt.h> 6 #include <linux/kobject.h> 7 #include <linux/sunrpc/addr.h> 8 #include <linux/sunrpc/xprtsock.h> 9 10 #include "sysfs.h" 11 12 struct xprt_addr { 13 const char *addr; 14 struct rcu_head rcu; 15 }; 16 17 static void free_xprt_addr(struct rcu_head *head) 18 { 19 struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu); 20 21 kfree(addr->addr); 22 kfree(addr); 23 } 24 25 static struct kset *rpc_sunrpc_kset; 26 static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj; 27 28 static void rpc_sysfs_object_release(struct kobject *kobj) 29 { 30 kfree(kobj); 31 } 32 33 static const struct kobj_ns_type_operations * 34 rpc_sysfs_object_child_ns_type(struct kobject *kobj) 35 { 36 return &net_ns_type_operations; 37 } 38 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, 43 }; 44 45 static struct kobject *rpc_sysfs_object_alloc(const char *name, 46 struct kset *kset, 47 struct kobject *parent) 48 { 49 struct kobject *kobj; 50 51 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 52 if (kobj) { 53 kobj->kset = kset; 54 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type, 55 parent, "%s", name) == 0) 56 return kobj; 57 kobject_put(kobj); 58 } 59 return NULL; 60 } 61 62 static inline struct rpc_xprt * 63 rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj) 64 { 65 struct rpc_sysfs_xprt *x = container_of(kobj, 66 struct rpc_sysfs_xprt, kobject); 67 68 return xprt_get(x->xprt); 69 } 70 71 static inline struct rpc_xprt_switch * 72 rpc_sysfs_xprt_kobj_get_xprt_switch(struct kobject *kobj) 73 { 74 struct rpc_sysfs_xprt *x = container_of(kobj, 75 struct rpc_sysfs_xprt, kobject); 76 77 return xprt_switch_get(x->xprt_switch); 78 } 79 80 static inline struct rpc_xprt_switch * 81 rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj) 82 { 83 struct rpc_sysfs_xprt_switch *x = container_of(kobj, 84 struct rpc_sysfs_xprt_switch, kobject); 85 86 return xprt_switch_get(x->xprt_switch); 87 } 88 89 static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj, 90 struct kobj_attribute *attr, 91 char *buf) 92 { 93 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 94 ssize_t ret; 95 96 if (!xprt) { 97 ret = sprintf(buf, "<closed>\n"); 98 goto out; 99 } 100 ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]); 101 xprt_put(xprt); 102 out: 103 return ret; 104 } 105 106 static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj, 107 struct kobj_attribute *attr, 108 char *buf) 109 { 110 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 111 size_t buflen = PAGE_SIZE; 112 ssize_t ret; 113 114 if (!xprt || !xprt_connected(xprt)) { 115 ret = sprintf(buf, "<closed>\n"); 116 } else if (xprt->ops->get_srcaddr) { 117 ret = xprt->ops->get_srcaddr(xprt, buf, buflen); 118 if (ret > 0) { 119 if (ret < buflen - 1) { 120 buf[ret] = '\n'; 121 ret++; 122 buf[ret] = '\0'; 123 } 124 } else 125 ret = sprintf(buf, "<closed>\n"); 126 } else 127 ret = sprintf(buf, "<not a socket>\n"); 128 xprt_put(xprt); 129 return ret; 130 } 131 132 static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj, 133 struct kobj_attribute *attr, char *buf) 134 { 135 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 136 unsigned short srcport = 0; 137 size_t buflen = PAGE_SIZE; 138 ssize_t ret; 139 140 if (!xprt || !xprt_connected(xprt)) { 141 ret = sprintf(buf, "<closed>\n"); 142 goto out; 143 } 144 145 if (xprt->ops->get_srcport) 146 srcport = xprt->ops->get_srcport(xprt); 147 148 ret = snprintf(buf, buflen, 149 "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n" 150 "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n" 151 "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n" 152 "backlog_q_len=%u\nmain_xprt=%d\nsrc_port=%u\n" 153 "tasks_queuelen=%ld\ndst_port=%s\n", 154 xprt->last_used, xprt->cong, xprt->cwnd, xprt->max_reqs, 155 xprt->min_reqs, xprt->num_reqs, xprt->binding.qlen, 156 xprt->sending.qlen, xprt->pending.qlen, 157 xprt->backlog.qlen, xprt->main, srcport, 158 atomic_long_read(&xprt->queuelen), 159 xprt->address_strings[RPC_DISPLAY_PORT]); 160 out: 161 xprt_put(xprt); 162 return ret; 163 } 164 165 static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj, 166 struct kobj_attribute *attr, 167 char *buf) 168 { 169 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 170 ssize_t ret; 171 int locked, connected, connecting, close_wait, bound, binding, 172 closing, congested, cwnd_wait, write_space, offline, remove; 173 174 if (!(xprt && xprt->state)) { 175 ret = sprintf(buf, "state=CLOSED\n"); 176 } else { 177 locked = test_bit(XPRT_LOCKED, &xprt->state); 178 connected = test_bit(XPRT_CONNECTED, &xprt->state); 179 connecting = test_bit(XPRT_CONNECTING, &xprt->state); 180 close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state); 181 bound = test_bit(XPRT_BOUND, &xprt->state); 182 binding = test_bit(XPRT_BINDING, &xprt->state); 183 closing = test_bit(XPRT_CLOSING, &xprt->state); 184 congested = test_bit(XPRT_CONGESTED, &xprt->state); 185 cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state); 186 write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state); 187 offline = test_bit(XPRT_OFFLINE, &xprt->state); 188 remove = test_bit(XPRT_REMOVE, &xprt->state); 189 190 ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s %s\n", 191 locked ? "LOCKED" : "", 192 connected ? "CONNECTED" : "", 193 connecting ? "CONNECTING" : "", 194 close_wait ? "CLOSE_WAIT" : "", 195 bound ? "BOUND" : "", 196 binding ? "BOUNDING" : "", 197 closing ? "CLOSING" : "", 198 congested ? "CONGESTED" : "", 199 cwnd_wait ? "CWND_WAIT" : "", 200 write_space ? "WRITE_SPACE" : "", 201 offline ? "OFFLINE" : "", 202 remove ? "REMOVE" : ""); 203 } 204 205 xprt_put(xprt); 206 return ret; 207 } 208 209 static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj, 210 struct kobj_attribute *attr, 211 char *buf) 212 { 213 struct rpc_xprt_switch *xprt_switch = 214 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj); 215 ssize_t ret; 216 217 if (!xprt_switch) 218 return 0; 219 ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\n" 220 "num_unique_destaddr=%u\nqueue_len=%ld\n", 221 xprt_switch->xps_nxprts, xprt_switch->xps_nactive, 222 xprt_switch->xps_nunique_destaddr_xprts, 223 atomic_long_read(&xprt_switch->xps_queuelen)); 224 xprt_switch_put(xprt_switch); 225 return ret; 226 } 227 228 static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj, 229 struct kobj_attribute *attr, 230 const char *buf, size_t count) 231 { 232 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 233 struct sockaddr *saddr; 234 char *dst_addr; 235 int port; 236 struct xprt_addr *saved_addr; 237 size_t buf_len; 238 239 if (!xprt) 240 return 0; 241 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP || 242 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) { 243 xprt_put(xprt); 244 return -EOPNOTSUPP; 245 } 246 247 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) { 248 count = -EINTR; 249 goto out_put; 250 } 251 saddr = (struct sockaddr *)&xprt->addr; 252 port = rpc_get_port(saddr); 253 254 /* buf_len is the len until the first occurence of either 255 * '\n' or '\0' 256 */ 257 buf_len = strcspn(buf, "\n"); 258 259 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL); 260 if (!dst_addr) 261 goto out_err; 262 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL); 263 if (!saved_addr) 264 goto out_err_free; 265 saved_addr->addr = 266 rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]); 267 rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr); 268 call_rcu(&saved_addr->rcu, free_xprt_addr); 269 xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr, 270 sizeof(*saddr)); 271 rpc_set_port(saddr, port); 272 273 xprt_force_disconnect(xprt); 274 out: 275 xprt_release_write(xprt, NULL); 276 out_put: 277 xprt_put(xprt); 278 return count; 279 out_err_free: 280 kfree(dst_addr); 281 out_err: 282 count = -ENOMEM; 283 goto out; 284 } 285 286 static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj, 287 struct kobj_attribute *attr, 288 const char *buf, size_t count) 289 { 290 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); 291 int offline = 0, online = 0, remove = 0; 292 struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj); 293 294 if (!xprt) 295 return 0; 296 297 if (!strncmp(buf, "offline", 7)) 298 offline = 1; 299 else if (!strncmp(buf, "online", 6)) 300 online = 1; 301 else if (!strncmp(buf, "remove", 6)) 302 remove = 1; 303 else { 304 count = -EINVAL; 305 goto out_put; 306 } 307 308 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) { 309 count = -EINTR; 310 goto out_put; 311 } 312 if (xprt->main) { 313 count = -EINVAL; 314 goto release_tasks; 315 } 316 if (offline) { 317 xprt_set_offline_locked(xprt, xps); 318 } else if (online) { 319 xprt_set_online_locked(xprt, xps); 320 } else if (remove) { 321 if (test_bit(XPRT_OFFLINE, &xprt->state)) 322 xprt_delete_locked(xprt, xps); 323 else 324 count = -EINVAL; 325 } 326 327 release_tasks: 328 xprt_release_write(xprt, NULL); 329 out_put: 330 xprt_put(xprt); 331 xprt_switch_put(xps); 332 return count; 333 } 334 335 int rpc_sysfs_init(void) 336 { 337 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj); 338 if (!rpc_sunrpc_kset) 339 return -ENOMEM; 340 rpc_sunrpc_client_kobj = 341 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL); 342 if (!rpc_sunrpc_client_kobj) 343 goto err_client; 344 rpc_sunrpc_xprt_switch_kobj = 345 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL); 346 if (!rpc_sunrpc_xprt_switch_kobj) 347 goto err_switch; 348 return 0; 349 err_switch: 350 kobject_put(rpc_sunrpc_client_kobj); 351 rpc_sunrpc_client_kobj = NULL; 352 err_client: 353 kset_unregister(rpc_sunrpc_kset); 354 rpc_sunrpc_kset = NULL; 355 return -ENOMEM; 356 } 357 358 static void rpc_sysfs_client_release(struct kobject *kobj) 359 { 360 struct rpc_sysfs_client *c; 361 362 c = container_of(kobj, struct rpc_sysfs_client, kobject); 363 kfree(c); 364 } 365 366 static void rpc_sysfs_xprt_switch_release(struct kobject *kobj) 367 { 368 struct rpc_sysfs_xprt_switch *xprt_switch; 369 370 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject); 371 kfree(xprt_switch); 372 } 373 374 static void rpc_sysfs_xprt_release(struct kobject *kobj) 375 { 376 struct rpc_sysfs_xprt *xprt; 377 378 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject); 379 kfree(xprt); 380 } 381 382 static const void *rpc_sysfs_client_namespace(struct kobject *kobj) 383 { 384 return container_of(kobj, struct rpc_sysfs_client, kobject)->net; 385 } 386 387 static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj) 388 { 389 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net; 390 } 391 392 static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj) 393 { 394 return container_of(kobj, struct rpc_sysfs_xprt, 395 kobject)->xprt->xprt_net; 396 } 397 398 static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr, 399 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store); 400 401 static struct kobj_attribute rpc_sysfs_xprt_srcaddr = __ATTR(srcaddr, 402 0644, rpc_sysfs_xprt_srcaddr_show, NULL); 403 404 static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info, 405 0444, rpc_sysfs_xprt_info_show, NULL); 406 407 static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state, 408 0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change); 409 410 static struct attribute *rpc_sysfs_xprt_attrs[] = { 411 &rpc_sysfs_xprt_dstaddr.attr, 412 &rpc_sysfs_xprt_srcaddr.attr, 413 &rpc_sysfs_xprt_info.attr, 414 &rpc_sysfs_xprt_change_state.attr, 415 NULL, 416 }; 417 ATTRIBUTE_GROUPS(rpc_sysfs_xprt); 418 419 static struct kobj_attribute rpc_sysfs_xprt_switch_info = 420 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL); 421 422 static struct attribute *rpc_sysfs_xprt_switch_attrs[] = { 423 &rpc_sysfs_xprt_switch_info.attr, 424 NULL, 425 }; 426 ATTRIBUTE_GROUPS(rpc_sysfs_xprt_switch); 427 428 static struct kobj_type rpc_sysfs_client_type = { 429 .release = rpc_sysfs_client_release, 430 .sysfs_ops = &kobj_sysfs_ops, 431 .namespace = rpc_sysfs_client_namespace, 432 }; 433 434 static struct kobj_type rpc_sysfs_xprt_switch_type = { 435 .release = rpc_sysfs_xprt_switch_release, 436 .default_groups = rpc_sysfs_xprt_switch_groups, 437 .sysfs_ops = &kobj_sysfs_ops, 438 .namespace = rpc_sysfs_xprt_switch_namespace, 439 }; 440 441 static struct kobj_type rpc_sysfs_xprt_type = { 442 .release = rpc_sysfs_xprt_release, 443 .default_groups = rpc_sysfs_xprt_groups, 444 .sysfs_ops = &kobj_sysfs_ops, 445 .namespace = rpc_sysfs_xprt_namespace, 446 }; 447 448 void rpc_sysfs_exit(void) 449 { 450 kobject_put(rpc_sunrpc_client_kobj); 451 kobject_put(rpc_sunrpc_xprt_switch_kobj); 452 kset_unregister(rpc_sunrpc_kset); 453 } 454 455 static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent, 456 struct net *net, 457 int clid) 458 { 459 struct rpc_sysfs_client *p; 460 461 p = kzalloc(sizeof(*p), GFP_KERNEL); 462 if (p) { 463 p->net = net; 464 p->kobject.kset = rpc_sunrpc_kset; 465 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type, 466 parent, "clnt-%d", clid) == 0) 467 return p; 468 kobject_put(&p->kobject); 469 } 470 return NULL; 471 } 472 473 static struct rpc_sysfs_xprt_switch * 474 rpc_sysfs_xprt_switch_alloc(struct kobject *parent, 475 struct rpc_xprt_switch *xprt_switch, 476 struct net *net, 477 gfp_t gfp_flags) 478 { 479 struct rpc_sysfs_xprt_switch *p; 480 481 p = kzalloc(sizeof(*p), gfp_flags); 482 if (p) { 483 p->net = net; 484 p->kobject.kset = rpc_sunrpc_kset; 485 if (kobject_init_and_add(&p->kobject, 486 &rpc_sysfs_xprt_switch_type, 487 parent, "switch-%d", 488 xprt_switch->xps_id) == 0) 489 return p; 490 kobject_put(&p->kobject); 491 } 492 return NULL; 493 } 494 495 static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent, 496 struct rpc_xprt *xprt, 497 gfp_t gfp_flags) 498 { 499 struct rpc_sysfs_xprt *p; 500 501 p = kzalloc(sizeof(*p), gfp_flags); 502 if (!p) 503 goto out; 504 p->kobject.kset = rpc_sunrpc_kset; 505 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type, 506 parent, "xprt-%d-%s", xprt->id, 507 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0) 508 return p; 509 kobject_put(&p->kobject); 510 out: 511 return NULL; 512 } 513 514 void rpc_sysfs_client_setup(struct rpc_clnt *clnt, 515 struct rpc_xprt_switch *xprt_switch, 516 struct net *net) 517 { 518 struct rpc_sysfs_client *rpc_client; 519 520 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj, 521 net, clnt->cl_clid); 522 if (rpc_client) { 523 char name[] = "switch"; 524 struct rpc_sysfs_xprt_switch *xswitch = 525 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs; 526 int ret; 527 528 clnt->cl_sysfs = rpc_client; 529 rpc_client->clnt = clnt; 530 rpc_client->xprt_switch = xprt_switch; 531 kobject_uevent(&rpc_client->kobject, KOBJ_ADD); 532 ret = sysfs_create_link_nowarn(&rpc_client->kobject, 533 &xswitch->kobject, name); 534 if (ret) 535 pr_warn("can't create link to %s in sysfs (%d)\n", 536 name, ret); 537 } 538 } 539 540 void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch, 541 struct rpc_xprt *xprt, 542 gfp_t gfp_flags) 543 { 544 struct rpc_sysfs_xprt_switch *rpc_xprt_switch; 545 struct net *net; 546 547 if (xprt_switch->xps_net) 548 net = xprt_switch->xps_net; 549 else 550 net = xprt->xprt_net; 551 rpc_xprt_switch = 552 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj, 553 xprt_switch, net, gfp_flags); 554 if (rpc_xprt_switch) { 555 xprt_switch->xps_sysfs = rpc_xprt_switch; 556 rpc_xprt_switch->xprt_switch = xprt_switch; 557 rpc_xprt_switch->xprt = xprt; 558 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD); 559 } 560 } 561 562 void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch, 563 struct rpc_xprt *xprt, 564 gfp_t gfp_flags) 565 { 566 struct rpc_sysfs_xprt *rpc_xprt; 567 struct rpc_sysfs_xprt_switch *switch_obj = 568 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs; 569 570 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags); 571 if (rpc_xprt) { 572 xprt->xprt_sysfs = rpc_xprt; 573 rpc_xprt->xprt = xprt; 574 rpc_xprt->xprt_switch = xprt_switch; 575 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD); 576 } 577 } 578 579 void rpc_sysfs_client_destroy(struct rpc_clnt *clnt) 580 { 581 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs; 582 583 if (rpc_client) { 584 char name[] = "switch"; 585 586 sysfs_remove_link(&rpc_client->kobject, name); 587 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE); 588 kobject_del(&rpc_client->kobject); 589 kobject_put(&rpc_client->kobject); 590 clnt->cl_sysfs = NULL; 591 } 592 } 593 594 void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch) 595 { 596 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs; 597 598 if (rpc_xprt_switch) { 599 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE); 600 kobject_del(&rpc_xprt_switch->kobject); 601 kobject_put(&rpc_xprt_switch->kobject); 602 xprt_switch->xps_sysfs = NULL; 603 } 604 } 605 606 void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt) 607 { 608 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs; 609 610 if (rpc_xprt) { 611 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE); 612 kobject_del(&rpc_xprt->kobject); 613 kobject_put(&rpc_xprt->kobject); 614 xprt->xprt_sysfs = NULL; 615 } 616 } 617