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 MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
41 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
43 * bits for peer hash offset
44 * NB: we don't use the highest bit of *ppos because it's signed
46 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
47 LNET_PROC_CPT_BITS - \
48 LNET_PROC_VER_BITS - \
49 LNET_PROC_HASH_BITS - 1)
50 /* bits for hash index + position */
51 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
52 /* bits for peer hash table + hash version */
53 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
55 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
56 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
57 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
58 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
60 #define LNET_PROC_CPT_GET(pos) \
61 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
63 #define LNET_PROC_VER_GET(pos) \
64 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
66 #define LNET_PROC_HASH_GET(pos) \
67 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
69 #define LNET_PROC_HOFF_GET(pos) \
70 (int)((pos) & LNET_PROC_HOFF_MASK)
72 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
73 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
74 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
75 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
76 ((off) & LNET_PROC_HOFF_MASK))
78 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
80 static int __proc_lnet_stats(void *data, int write,
81 loff_t pos, void __user *buffer, int nob)
84 struct lnet_counters *ctrs;
87 const int tmpsiz = 256; /* 7 %u and 4 __u64 */
90 lnet_counters_reset();
96 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
100 LIBCFS_ALLOC(tmpstr, tmpsiz);
101 if (tmpstr == NULL) {
102 LIBCFS_FREE(ctrs, sizeof(*ctrs));
106 lnet_counters_get(ctrs);
108 len = snprintf(tmpstr, tmpsiz,
109 "%u %u %u %u %u %u %u %llu %llu "
111 ctrs->msgs_alloc, ctrs->msgs_max,
113 ctrs->send_count, ctrs->recv_count,
114 ctrs->route_count, ctrs->drop_count,
115 ctrs->send_length, ctrs->recv_length,
116 ctrs->route_length, ctrs->drop_length);
118 if (pos >= min_t(int, len, strlen(tmpstr)))
121 rc = cfs_trace_copyout_string(buffer, nob,
124 LIBCFS_FREE(tmpstr, tmpsiz);
125 LIBCFS_FREE(ctrs, sizeof(*ctrs));
130 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
131 size_t *lenp, loff_t *ppos)
133 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
138 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
139 size_t *lenp, loff_t *ppos)
141 const int tmpsiz = 256;
149 CLASSERT(sizeof(loff_t) >= 4);
151 off = LNET_PROC_HOFF_GET(*ppos);
152 ver = LNET_PROC_VER_GET(*ppos);
159 LIBCFS_ALLOC(tmpstr, tmpsiz);
163 s = tmpstr; /* points to current position in tmpstr[] */
166 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
167 the_lnet.ln_routing ? "enabled" : "disabled");
168 LASSERT(tmpstr + tmpsiz - s > 0);
170 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
171 "net", "hops", "priority", "state", "router");
172 LASSERT(tmpstr + tmpsiz - s > 0);
175 ver = (unsigned int)the_lnet.ln_remote_nets_version;
177 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
181 struct lnet_route *route = NULL;
182 struct lnet_remotenet *rnet = NULL;
184 struct list_head *rn_list;
189 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
191 LIBCFS_FREE(tmpstr, tmpsiz);
195 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
197 rn_list = &the_lnet.ln_remote_nets_hash[i];
201 while (n != rn_list && route == NULL) {
202 rnet = list_entry(n, struct lnet_remotenet,
205 r = rnet->lrn_routes.next;
207 while (r != &rnet->lrn_routes) {
208 struct lnet_route *re =
209 list_entry(r, struct lnet_route,
225 __u32 net = rnet->lrn_net;
226 __u32 hops = route->lr_hops;
227 unsigned int priority = route->lr_priority;
228 lnet_nid_t nid = route->lr_gateway->lpni_nid;
229 int alive = lnet_is_route_alive(route);
231 s += snprintf(s, tmpstr + tmpsiz - s,
232 "%-8s %4d %8u %7s %s\n",
233 libcfs_net2str(net), hops,
235 alive ? "up" : "down",
236 libcfs_nid2str(nid));
237 LASSERT(tmpstr + tmpsiz - s > 0);
243 len = s - tmpstr; /* how many bytes was written */
245 if (len > *lenp) { /* linux-supplied buffer is too small */
247 } else if (len > 0) { /* wrote something */
248 if (copy_to_user(buffer, tmpstr, len))
252 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
256 LIBCFS_FREE(tmpstr, tmpsiz);
265 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
266 size_t *lenp, loff_t *ppos)
271 const int tmpsiz = 256;
276 off = LNET_PROC_HOFF_GET(*ppos);
277 ver = LNET_PROC_VER_GET(*ppos);
284 LIBCFS_ALLOC(tmpstr, tmpsiz);
288 s = tmpstr; /* points to current position in tmpstr[] */
291 s += snprintf(s, tmpstr + tmpsiz - s,
292 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
293 "ref", "rtr_ref", "alive_cnt", "state",
294 "last_ping", "ping_sent", "deadline",
295 "down_ni", "router");
296 LASSERT(tmpstr + tmpsiz - s > 0);
299 ver = (unsigned int)the_lnet.ln_routers_version;
301 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
304 struct lnet_peer_ni *peer = NULL;
309 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
312 LIBCFS_FREE(tmpstr, tmpsiz);
316 r = the_lnet.ln_routers.next;
318 while (r != &the_lnet.ln_routers) {
319 struct lnet_peer_ni *lp =
320 list_entry(r, struct lnet_peer_ni,
333 lnet_nid_t nid = peer->lpni_nid;
334 time64_t now = ktime_get_seconds();
335 time64_t deadline = peer->lpni_ping_deadline;
336 int nrefs = atomic_read(&peer->lpni_refcount);
337 int nrtrrefs = peer->lpni_rtr_refcount;
338 int alive_cnt = peer->lpni_alive_count;
339 int alive = peer->lpni_alive;
340 int pingsent = !peer->lpni_ping_notsent;
341 time64_t last_ping = now - peer->lpni_ping_timestamp;
343 struct lnet_route *rtr;
345 if ((peer->lpni_ping_feats &
346 LNET_PING_FEAT_NI_STATUS) != 0) {
347 list_for_each_entry(rtr, &peer->lpni_routes,
349 /* downis on any route should be the
350 * number of downis on the gateway */
351 if (rtr->lr_downis != 0) {
352 down_ni = rtr->lr_downis;
359 s += snprintf(s, tmpstr + tmpsiz - s,
360 "%-4d %7d %9d %6s %12llu %9d %8s %7d %s\n",
361 nrefs, nrtrrefs, alive_cnt,
362 alive ? "up" : "down", last_ping,
363 pingsent, "NA", down_ni,
364 libcfs_nid2str(nid));
366 s += snprintf(s, tmpstr + tmpsiz - s,
367 "%-4d %7d %9d %6s %12llu %9d %8llu %7d %s\n",
368 nrefs, nrtrrefs, alive_cnt,
369 alive ? "up" : "down", last_ping,
372 down_ni, libcfs_nid2str(nid));
373 LASSERT(tmpstr + tmpsiz - s > 0);
379 len = s - tmpstr; /* how many bytes was written */
381 if (len > *lenp) { /* linux-supplied buffer is too small */
383 } else if (len > 0) { /* wrote something */
384 if (copy_to_user(buffer, tmpstr, len))
388 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
392 LIBCFS_FREE(tmpstr, tmpsiz);
400 /* TODO: there should be no direct access to ptable. We should add a set
401 * of APIs that give access to the ptable and its members */
403 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
404 size_t *lenp, loff_t *ppos)
406 const int tmpsiz = 256;
407 struct lnet_peer_table *ptable;
410 int cpt = LNET_PROC_CPT_GET(*ppos);
411 int ver = LNET_PROC_VER_GET(*ppos);
412 int hash = LNET_PROC_HASH_GET(*ppos);
413 int hoff = LNET_PROC_HOFF_GET(*ppos);
419 struct lnet_peer_ni *peer;
421 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
423 for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
424 list_for_each_entry(peer,
425 &ptable->pt_hash[hash],
427 peer->lpni_mintxcredits =
428 peer->lpni_txcredits;
429 peer->lpni_minrtrcredits =
430 peer->lpni_rtrcredits;
442 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
444 if (cpt >= LNET_CPT_NUMBER) {
449 LIBCFS_ALLOC(tmpstr, tmpsiz);
453 s = tmpstr; /* points to current position in tmpstr[] */
456 s += snprintf(s, tmpstr + tmpsiz - s,
457 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
458 "nid", "refs", "state", "last", "max",
459 "rtr", "min", "tx", "min", "queue");
460 LASSERT(tmpstr + tmpsiz - s > 0);
464 struct lnet_peer_ni *peer;
474 ptable = the_lnet.ln_peer_tables[cpt];
476 ver = LNET_PROC_VERSION(ptable->pt_version);
478 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
479 lnet_net_unlock(cpt);
480 LIBCFS_FREE(tmpstr, tmpsiz);
484 while (hash < LNET_PEER_HASH_SIZE) {
486 p = ptable->pt_hash[hash].next;
488 while (p != &ptable->pt_hash[hash]) {
489 struct lnet_peer_ni *lp =
490 list_entry(p, struct lnet_peer_ni,
495 /* minor optimization: start from idx+1
496 * on next iteration if we've just
497 * drained lpni_hashlist */
498 if (lp->lpni_hashlist.next ==
499 &ptable->pt_hash[hash]) {
510 p = lp->lpni_hashlist.next;
522 lnet_nid_t nid = peer->lpni_nid;
523 int nrefs = atomic_read(&peer->lpni_refcount);
524 time64_t lastalive = -1;
525 char *aliveness = "NA";
526 int maxcr = (peer->lpni_net) ?
527 peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
528 int txcr = peer->lpni_txcredits;
529 int mintxcr = peer->lpni_mintxcredits;
530 int rtrcr = peer->lpni_rtrcredits;
531 int minrtrcr = peer->lpni_minrtrcredits;
532 int txqnob = peer->lpni_txqnob;
534 if (lnet_isrouter(peer) ||
535 lnet_peer_aliveness_enabled(peer))
536 aliveness = peer->lpni_alive ? "up" : "down";
538 if (lnet_peer_aliveness_enabled(peer)) {
539 time64_t now = ktime_get_seconds();
541 lastalive = now - peer->lpni_last_alive;
543 /* No need to mess up peers contents with
544 * arbitrarily long integers - it suffices to
545 * know that lastalive is more than 10000s old
547 if (lastalive >= 10000)
551 lnet_net_unlock(cpt);
553 s += snprintf(s, tmpstr + tmpsiz - s,
554 "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
555 libcfs_nid2str(nid), nrefs, aliveness,
556 lastalive, maxcr, rtrcr, minrtrcr, txcr,
558 LASSERT(tmpstr + tmpsiz - s > 0);
560 } else { /* peer is NULL */
561 lnet_net_unlock(cpt);
564 if (hash == LNET_PEER_HASH_SIZE) {
568 if (peer == NULL && cpt < LNET_CPT_NUMBER)
573 len = s - tmpstr; /* how many bytes was written */
575 if (len > *lenp) { /* linux-supplied buffer is too small */
577 } else if (len > 0) { /* wrote something */
578 if (copy_to_user(buffer, tmpstr, len))
581 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
584 LIBCFS_FREE(tmpstr, tmpsiz);
592 static int __proc_lnet_buffers(void *data, int write,
593 loff_t pos, void __user *buffer, int nob)
605 /* (4 %d) * 4 * LNET_CPT_NUMBER */
606 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
607 LIBCFS_ALLOC(tmpstr, tmpsiz);
611 s = tmpstr; /* points to current position in tmpstr[] */
613 s += snprintf(s, tmpstr + tmpsiz - s,
615 "pages", "count", "credits", "min");
616 LASSERT(tmpstr + tmpsiz - s > 0);
618 if (the_lnet.ln_rtrpools == NULL)
619 goto out; /* I'm not a router */
621 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
622 struct lnet_rtrbufpool *rbp;
624 lnet_net_lock(LNET_LOCK_EX);
625 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
626 s += snprintf(s, tmpstr + tmpsiz - s,
629 rbp[idx].rbp_nbuffers,
630 rbp[idx].rbp_credits,
631 rbp[idx].rbp_mincredits);
632 LASSERT(tmpstr + tmpsiz - s > 0);
634 lnet_net_unlock(LNET_LOCK_EX);
640 if (pos >= min_t(int, len, strlen(tmpstr)))
643 rc = cfs_trace_copyout_string(buffer, nob,
646 LIBCFS_FREE(tmpstr, tmpsiz);
651 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
652 size_t *lenp, loff_t *ppos)
654 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
655 __proc_lnet_buffers);
659 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
660 size_t *lenp, loff_t *ppos)
662 int tmpsiz = 128 * LNET_CPT_NUMBER;
672 /* Just reset the min stat. */
674 struct lnet_net *net;
678 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
679 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
680 struct lnet_tx_queue *tq;
684 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
685 for (j = 0; ni->ni_cpts != NULL &&
686 j < ni->ni_ncpts; j++) {
687 if (i == ni->ni_cpts[j])
691 if (j == ni->ni_ncpts)
696 tq->tq_credits_min = tq->tq_credits;
707 LIBCFS_ALLOC(tmpstr, tmpsiz);
711 s = tmpstr; /* points to current position in tmpstr[] */
714 s += snprintf(s, tmpstr + tmpsiz - s,
715 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
716 "nid", "status", "alive", "refs", "peer",
717 "rtr", "max", "tx", "min");
718 LASSERT (tmpstr + tmpsiz - s > 0);
720 struct lnet_ni *ni = NULL;
721 int skip = *ppos - 1;
725 ni = lnet_get_ni_idx_locked(skip);
728 struct lnet_tx_queue *tq;
730 time64_t now = ktime_get_real_seconds();
731 time64_t last_alive = -1;
735 if (the_lnet.ln_routing)
736 last_alive = now - ni->ni_last_alive;
738 /* @lo forever alive */
739 if (ni->ni_net->net_lnd->lnd_type == LOLND)
743 LASSERT(ni->ni_status != NULL);
744 stat = (ni->ni_status->ns_status ==
745 LNET_NI_STATUS_UP) ? "up" : "down";
748 /* we actually output credits information for
749 * TX queue of each partition */
750 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
751 for (j = 0; ni->ni_cpts != NULL &&
752 j < ni->ni_ncpts; j++) {
753 if (i == ni->ni_cpts[j])
757 if (j == ni->ni_ncpts)
763 s += snprintf(s, tmpstr + tmpsiz - s,
764 "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
765 libcfs_nid2str(ni->ni_nid), stat,
766 last_alive, *ni->ni_refs[i],
767 ni->ni_net->net_tunables.lct_peer_tx_credits,
768 ni->ni_net->net_tunables.lct_peer_rtr_credits,
770 tq->tq_credits, tq->tq_credits_min);
774 LASSERT(tmpstr + tmpsiz - s > 0);
780 len = s - tmpstr; /* how many bytes was written */
782 if (len > *lenp) { /* linux-supplied buffer is too small */
784 } else if (len > 0) { /* wrote something */
785 if (copy_to_user(buffer, tmpstr, len))
791 LIBCFS_FREE(tmpstr, tmpsiz);
799 struct lnet_portal_rotors {
805 static struct lnet_portal_rotors portal_rotors[] = {
807 .pr_value = LNET_PTL_ROTOR_OFF,
809 .pr_desc = "Turn off message rotor for wildcard portals"
812 .pr_value = LNET_PTL_ROTOR_ON,
814 .pr_desc = "round-robin dispatch all PUT messages for "
818 .pr_value = LNET_PTL_ROTOR_RR_RT,
820 .pr_desc = "round-robin dispatch routed PUT message for "
824 .pr_value = LNET_PTL_ROTOR_HASH_RT,
825 .pr_name = "HASH_RT",
826 .pr_desc = "dispatch routed PUT message by hashing source "
827 "NID for wildcard portals"
836 static int __proc_lnet_portal_rotor(void *data, int write,
837 loff_t pos, void __user *buffer, int nob)
839 const int buf_len = 128;
845 LIBCFS_ALLOC(buf, buf_len);
852 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
853 if (portal_rotors[i].pr_value == portal_rotor)
857 LASSERT(portal_rotors[i].pr_value == portal_rotor);
860 rc = snprintf(buf, buf_len,
861 "{\n\tportals: all\n"
862 "\trotor: %s\n\tdescription: %s\n}",
863 portal_rotors[i].pr_name,
864 portal_rotors[i].pr_desc);
866 if (pos >= min_t(int, rc, buf_len)) {
869 rc = cfs_trace_copyout_string(buffer, nob,
875 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
879 tmp = cfs_trimwhite(buf);
883 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
884 if (strncasecmp(portal_rotors[i].pr_name, tmp,
885 strlen(portal_rotors[i].pr_name)) == 0) {
886 portal_rotor = portal_rotors[i].pr_value;
893 LIBCFS_FREE(buf, buf_len);
898 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
899 size_t *lenp, loff_t *ppos)
901 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
902 __proc_lnet_portal_rotor);
906 static struct ctl_table lnet_table[] = {
908 * NB No .strategy entries have been provided since sysctl(8) prefers
909 * to go via /proc for portability.
915 .proc_handler = &proc_lnet_stats,
919 .procname = "routes",
921 .proc_handler = &proc_lnet_routes,
925 .procname = "routers",
927 .proc_handler = &proc_lnet_routers,
933 .proc_handler = &proc_lnet_peers,
937 .procname = "buffers",
939 .proc_handler = &proc_lnet_buffers,
945 .proc_handler = &proc_lnet_nis,
949 .procname = "portal_rotor",
951 .proc_handler = &proc_lnet_portal_rotor,
956 void lnet_router_debugfs_init(void)
958 lnet_insert_debugfs(lnet_table, NULL);