2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
25 #include <linux/uaccess.h>
27 #include <libcfs/libcfs.h>
28 #include <lnet/lib-lnet.h>
30 /* This is really lnet_proc.c. You might need to update sanity test 215
31 * if any file format is changed. */
33 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
35 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
37 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
38 /* change version, 16 bits or 8 bits */
39 #define LNET_PROC_VER_BITS \
40 clamp_t(int, LNET_LOFFT_BITS / 4, 8, 16)
42 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
44 * bits for peer hash offset
45 * NB: we don't use the highest bit of *ppos because it's signed
47 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
48 LNET_PROC_CPT_BITS - \
49 LNET_PROC_VER_BITS - \
50 LNET_PROC_HASH_BITS - 1)
51 /* bits for hash index + position */
52 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
53 /* bits for peer hash table + hash version */
54 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
56 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
57 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
58 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
59 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
61 #define LNET_PROC_CPT_GET(pos) \
62 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
64 #define LNET_PROC_VER_GET(pos) \
65 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
67 #define LNET_PROC_HASH_GET(pos) \
68 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
70 #define LNET_PROC_HOFF_GET(pos) \
71 (int)((pos) & LNET_PROC_HOFF_MASK)
73 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
74 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
75 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
76 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
77 ((off) & LNET_PROC_HOFF_MASK))
79 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
81 static int __proc_lnet_stats(void *data, int write,
82 loff_t pos, void __user *buffer, int nob)
85 struct lnet_counters *ctrs;
86 struct lnet_counters_common common;
88 char tmpstr[256]; /* 7 %u and 4 u64 */
91 lnet_counters_reset();
97 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
101 rc = lnet_counters_get(ctrs);
105 common = ctrs->lct_common;
107 len = scnprintf(tmpstr, sizeof(tmpstr),
108 "%u %u %u %u %u %u %u %llu %llu "
110 common.lcc_msgs_alloc, common.lcc_msgs_max,
112 common.lcc_send_count, common.lcc_recv_count,
113 common.lcc_route_count, common.lcc_drop_count,
114 common.lcc_send_length, common.lcc_recv_length,
115 common.lcc_route_length, common.lcc_drop_length);
120 rc = cfs_trace_copyout_string(buffer, nob,
123 LIBCFS_FREE(ctrs, sizeof(*ctrs));
128 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
129 size_t *lenp, loff_t *ppos)
131 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
136 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
137 size_t *lenp, loff_t *ppos)
139 const int tmpsiz = 256;
147 BUILD_BUG_ON(sizeof(loff_t) < 4);
149 off = LNET_PROC_HOFF_GET(*ppos);
150 ver = LNET_PROC_VER_GET(*ppos);
157 LIBCFS_ALLOC(tmpstr, tmpsiz);
161 s = tmpstr; /* points to current position in tmpstr[] */
164 s += scnprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
165 the_lnet.ln_routing ? "enabled" : "disabled");
166 LASSERT(tmpstr + tmpsiz - s > 0);
168 s += scnprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
169 "net", "hops", "priority", "state", "router");
170 LASSERT(tmpstr + tmpsiz - s > 0);
173 ver = (unsigned int)the_lnet.ln_remote_nets_version;
175 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
179 struct lnet_route *route = NULL;
180 struct lnet_remotenet *rnet = NULL;
182 struct list_head *rn_list;
187 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
189 LIBCFS_FREE(tmpstr, tmpsiz);
193 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
195 rn_list = &the_lnet.ln_remote_nets_hash[i];
199 while (n != rn_list && route == NULL) {
200 rnet = list_entry(n, struct lnet_remotenet,
203 r = rnet->lrn_routes.next;
205 while (r != &rnet->lrn_routes) {
206 struct lnet_route *re =
207 list_entry(r, struct lnet_route,
223 __u32 net = rnet->lrn_net;
224 __u32 hops = route->lr_hops;
225 unsigned int priority = route->lr_priority;
226 int alive = lnet_is_route_alive(route);
228 s += scnprintf(s, tmpstr + tmpsiz - s,
229 "%-8s %4d %8u %7s %s\n",
230 libcfs_net2str(net), hops,
232 alive ? "up" : "down",
233 libcfs_nid2str(route->lr_nid));
234 LASSERT(tmpstr + tmpsiz - s > 0);
240 len = s - tmpstr; /* how many bytes was written */
242 if (len > *lenp) { /* linux-supplied buffer is too small */
244 } else if (len > 0) { /* wrote something */
245 if (copy_to_user(buffer, tmpstr, len))
249 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
253 LIBCFS_FREE(tmpstr, tmpsiz);
262 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
263 size_t *lenp, loff_t *ppos)
268 const int tmpsiz = 256;
273 off = LNET_PROC_HOFF_GET(*ppos);
274 ver = LNET_PROC_VER_GET(*ppos);
281 LIBCFS_ALLOC(tmpstr, tmpsiz);
285 s = tmpstr; /* points to current position in tmpstr[] */
288 s += scnprintf(s, tmpstr + tmpsiz - s,
290 "ref", "rtr_ref", "alive", "router");
291 LASSERT(tmpstr + tmpsiz - s > 0);
294 ver = (unsigned int)the_lnet.ln_routers_version;
296 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
299 struct lnet_peer *peer = NULL;
304 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
307 LIBCFS_FREE(tmpstr, tmpsiz);
311 r = the_lnet.ln_routers.next;
313 while (r != &the_lnet.ln_routers) {
314 struct lnet_peer *lp =
315 list_entry(r, struct lnet_peer,
328 lnet_nid_t nid = peer->lp_primary_nid;
329 int nrefs = atomic_read(&peer->lp_refcount);
330 int nrtrrefs = peer->lp_rtr_refcount;
331 int alive = lnet_is_gateway_alive(peer);
333 s += scnprintf(s, tmpstr + tmpsiz - s,
336 alive ? "up" : "down",
337 libcfs_nid2str(nid));
343 len = s - tmpstr; /* how many bytes was written */
345 if (len > *lenp) { /* linux-supplied buffer is too small */
347 } else if (len > 0) { /* wrote something */
348 if (copy_to_user(buffer, tmpstr, len))
352 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
356 LIBCFS_FREE(tmpstr, tmpsiz);
364 /* TODO: there should be no direct access to ptable. We should add a set
365 * of APIs that give access to the ptable and its members */
367 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
368 size_t *lenp, loff_t *ppos)
370 const int tmpsiz = 256;
371 struct lnet_peer_table *ptable;
374 int cpt = LNET_PROC_CPT_GET(*ppos);
375 int ver = LNET_PROC_VER_GET(*ppos);
376 int hash = LNET_PROC_HASH_GET(*ppos);
377 int hoff = LNET_PROC_HOFF_GET(*ppos);
383 struct lnet_peer_ni *peer;
385 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
387 for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
388 list_for_each_entry(peer,
389 &ptable->pt_hash[hash],
391 peer->lpni_mintxcredits =
392 peer->lpni_txcredits;
393 peer->lpni_minrtrcredits =
394 peer->lpni_rtrcredits;
406 BUILD_BUG_ON(LNET_PROC_HASH_BITS < LNET_PEER_HASH_BITS);
408 if (cpt >= LNET_CPT_NUMBER) {
413 LIBCFS_ALLOC(tmpstr, tmpsiz);
417 s = tmpstr; /* points to current position in tmpstr[] */
420 s += scnprintf(s, tmpstr + tmpsiz - s,
421 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
422 "nid", "refs", "state", "last", "max",
423 "rtr", "min", "tx", "min", "queue");
424 LASSERT(tmpstr + tmpsiz - s > 0);
428 struct lnet_peer_ni *peer;
438 ptable = the_lnet.ln_peer_tables[cpt];
440 ver = LNET_PROC_VERSION(ptable->pt_version);
442 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
443 lnet_net_unlock(cpt);
444 LIBCFS_FREE(tmpstr, tmpsiz);
448 while (hash < LNET_PEER_HASH_SIZE) {
450 p = ptable->pt_hash[hash].next;
452 while (p != &ptable->pt_hash[hash]) {
453 struct lnet_peer_ni *lp =
454 list_entry(p, struct lnet_peer_ni,
459 /* minor optimization: start from idx+1
460 * on next iteration if we've just
461 * drained lpni_hashlist */
462 if (lp->lpni_hashlist.next ==
463 &ptable->pt_hash[hash]) {
474 p = lp->lpni_hashlist.next;
486 lnet_nid_t nid = peer->lpni_nid;
487 int nrefs = atomic_read(&peer->lpni_refcount);
488 time64_t lastalive = -1;
489 char *aliveness = "NA";
490 int maxcr = (peer->lpni_net) ?
491 peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
492 int txcr = peer->lpni_txcredits;
493 int mintxcr = peer->lpni_mintxcredits;
494 int rtrcr = peer->lpni_rtrcredits;
495 int minrtrcr = peer->lpni_minrtrcredits;
496 int txqnob = peer->lpni_txqnob;
498 if (lnet_isrouter(peer) ||
499 lnet_peer_aliveness_enabled(peer))
500 aliveness = lnet_is_peer_ni_alive(peer) ?
503 lnet_net_unlock(cpt);
505 s += scnprintf(s, tmpstr + tmpsiz - s,
506 "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
507 libcfs_nid2str(nid), nrefs, aliveness,
508 lastalive, maxcr, rtrcr, minrtrcr, txcr,
510 LASSERT(tmpstr + tmpsiz - s > 0);
512 } else { /* peer is NULL */
513 lnet_net_unlock(cpt);
516 if (hash == LNET_PEER_HASH_SIZE) {
520 if (peer == NULL && cpt < LNET_CPT_NUMBER)
525 len = s - tmpstr; /* how many bytes was written */
527 if (len > *lenp) { /* linux-supplied buffer is too small */
529 } else if (len > 0) { /* wrote something */
530 if (copy_to_user(buffer, tmpstr, len))
533 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
536 LIBCFS_FREE(tmpstr, tmpsiz);
544 static int __proc_lnet_buffers(void *data, int write,
545 loff_t pos, void __user *buffer, int nob)
557 /* (4 %d) * 4 * LNET_CPT_NUMBER */
558 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
559 LIBCFS_ALLOC(tmpstr, tmpsiz);
563 s = tmpstr; /* points to current position in tmpstr[] */
565 s += scnprintf(s, tmpstr + tmpsiz - s,
567 "pages", "count", "credits", "min");
568 LASSERT(tmpstr + tmpsiz - s > 0);
570 if (the_lnet.ln_rtrpools == NULL)
571 goto out; /* I'm not a router */
573 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
574 struct lnet_rtrbufpool *rbp;
576 lnet_net_lock(LNET_LOCK_EX);
577 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
578 s += scnprintf(s, tmpstr + tmpsiz - s,
581 rbp[idx].rbp_nbuffers,
582 rbp[idx].rbp_credits,
583 rbp[idx].rbp_mincredits);
584 LASSERT(tmpstr + tmpsiz - s > 0);
586 lnet_net_unlock(LNET_LOCK_EX);
592 if (pos >= min_t(int, len, strlen(tmpstr)))
595 rc = cfs_trace_copyout_string(buffer, nob,
598 LIBCFS_FREE(tmpstr, tmpsiz);
603 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
604 size_t *lenp, loff_t *ppos)
606 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
607 __proc_lnet_buffers);
611 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
612 size_t *lenp, loff_t *ppos)
614 int tmpsiz = 128 * LNET_CPT_NUMBER;
624 /* Just reset the min stat. */
626 struct lnet_net *net;
630 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
631 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
632 struct lnet_tx_queue *tq;
636 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
637 for (j = 0; ni->ni_cpts != NULL &&
638 j < ni->ni_ncpts; j++) {
639 if (i == ni->ni_cpts[j])
643 if (j == ni->ni_ncpts)
648 tq->tq_credits_min = tq->tq_credits;
659 LIBCFS_ALLOC(tmpstr, tmpsiz);
663 s = tmpstr; /* points to current position in tmpstr[] */
666 s += scnprintf(s, tmpstr + tmpsiz - s,
667 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
668 "nid", "status", "alive", "refs", "peer",
669 "rtr", "max", "tx", "min");
670 LASSERT (tmpstr + tmpsiz - s > 0);
672 struct lnet_ni *ni = NULL;
673 int skip = *ppos - 1;
677 ni = lnet_get_ni_idx_locked(skip);
680 struct lnet_tx_queue *tq;
682 time64_t now = ktime_get_real_seconds();
683 time64_t last_alive = -1;
687 if (the_lnet.ln_routing)
688 last_alive = now - ni->ni_net->net_last_alive;
691 LASSERT(ni->ni_status != NULL);
692 stat = (ni->ni_status->ns_status ==
693 LNET_NI_STATUS_UP) ? "up" : "down";
696 /* @lo forever alive */
697 if (ni->ni_net->net_lnd->lnd_type == LOLND) {
702 /* we actually output credits information for
703 * TX queue of each partition */
704 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
705 for (j = 0; ni->ni_cpts != NULL &&
706 j < ni->ni_ncpts; j++) {
707 if (i == ni->ni_cpts[j])
711 if (j == ni->ni_ncpts)
717 s += scnprintf(s, tmpstr + tmpsiz - s,
718 "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
719 libcfs_nid2str(ni->ni_nid), stat,
720 last_alive, *ni->ni_refs[i],
721 ni->ni_net->net_tunables.lct_peer_tx_credits,
722 ni->ni_net->net_tunables.lct_peer_rtr_credits,
724 tq->tq_credits, tq->tq_credits_min);
728 LASSERT(tmpstr + tmpsiz - s > 0);
734 len = s - tmpstr; /* how many bytes was written */
736 if (len > *lenp) { /* linux-supplied buffer is too small */
738 } else if (len > 0) { /* wrote something */
739 if (copy_to_user(buffer, tmpstr, len))
745 LIBCFS_FREE(tmpstr, tmpsiz);
753 struct lnet_portal_rotors {
759 static struct lnet_portal_rotors portal_rotors[] = {
761 .pr_value = LNET_PTL_ROTOR_OFF,
763 .pr_desc = "Turn off message rotor for wildcard portals"
766 .pr_value = LNET_PTL_ROTOR_ON,
768 .pr_desc = "round-robin dispatch all PUT messages for "
772 .pr_value = LNET_PTL_ROTOR_RR_RT,
774 .pr_desc = "round-robin dispatch routed PUT message for "
778 .pr_value = LNET_PTL_ROTOR_HASH_RT,
779 .pr_name = "HASH_RT",
780 .pr_desc = "dispatch routed PUT message by hashing source "
781 "NID for wildcard portals"
790 static int __proc_lnet_portal_rotor(void *data, int write,
791 loff_t pos, void __user *buffer, int nob)
793 const int buf_len = 128;
799 LIBCFS_ALLOC(buf, buf_len);
806 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
807 if (portal_rotors[i].pr_value == portal_rotor)
811 LASSERT(portal_rotors[i].pr_value == portal_rotor);
814 rc = scnprintf(buf, buf_len,
815 "{\n\tportals: all\n"
816 "\trotor: %s\n\tdescription: %s\n}",
817 portal_rotors[i].pr_name,
818 portal_rotors[i].pr_desc);
820 if (pos >= min_t(int, rc, buf_len)) {
823 rc = cfs_trace_copyout_string(buffer, nob,
829 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
837 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
838 if (strncasecmp(portal_rotors[i].pr_name, tmp,
839 strlen(portal_rotors[i].pr_name)) == 0) {
840 portal_rotor = portal_rotors[i].pr_value;
847 LIBCFS_FREE(buf, buf_len);
852 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
853 size_t *lenp, loff_t *ppos)
855 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
856 __proc_lnet_portal_rotor);
860 static struct ctl_table lnet_table[] = {
862 * NB No .strategy entries have been provided since sysctl(8) prefers
863 * to go via /proc for portability.
868 .proc_handler = &proc_lnet_stats,
871 .procname = "routes",
873 .proc_handler = &proc_lnet_routes,
876 .procname = "routers",
878 .proc_handler = &proc_lnet_routers,
883 .proc_handler = &proc_lnet_peers,
886 .procname = "buffers",
888 .proc_handler = &proc_lnet_buffers,
893 .proc_handler = &proc_lnet_nis,
896 .procname = "portal_rotor",
898 .proc_handler = &proc_lnet_portal_rotor,
901 .procname = "lnet_lnd_timeout",
902 .data = &lnet_lnd_timeout,
903 .maxlen = sizeof(lnet_lnd_timeout),
905 .proc_handler = &proc_dointvec,
910 void lnet_router_debugfs_init(void)
912 lnet_insert_debugfs(lnet_table);
915 void lnet_router_debugfs_fini(void)
917 lnet_remove_debugfs(lnet_table);