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.hpdd.intel.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/linux/linux-misc.h>
28 #include <libcfs/libcfs.h>
29 #include <lnet/lib-lnet.h>
31 /* This is really lnet_proc.c. You might need to update sanity test 215
32 * if any file format is changed. */
34 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
36 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
38 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
39 /* change version, 16 bits or 8 bits */
40 #define LNET_PROC_VER_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
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;
88 const int tmpsiz = 256; /* 7 %u and 4 __u64 */
91 lnet_counters_reset();
97 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
101 LIBCFS_ALLOC(tmpstr, tmpsiz);
102 if (tmpstr == NULL) {
103 LIBCFS_FREE(ctrs, sizeof(*ctrs));
107 lnet_counters_get(ctrs);
109 len = snprintf(tmpstr, tmpsiz,
110 "%u %u %u %u %u %u %u %llu %llu "
112 ctrs->msgs_alloc, ctrs->msgs_max,
114 ctrs->send_count, ctrs->recv_count,
115 ctrs->route_count, ctrs->drop_count,
116 ctrs->send_length, ctrs->recv_length,
117 ctrs->route_length, ctrs->drop_length);
119 if (pos >= min_t(int, len, strlen(tmpstr)))
122 rc = cfs_trace_copyout_string(buffer, nob,
125 LIBCFS_FREE(tmpstr, tmpsiz);
126 LIBCFS_FREE(ctrs, sizeof(*ctrs));
131 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
132 size_t *lenp, loff_t *ppos)
134 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
139 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
140 size_t *lenp, loff_t *ppos)
142 const int tmpsiz = 256;
150 CLASSERT(sizeof(loff_t) >= 4);
152 off = LNET_PROC_HOFF_GET(*ppos);
153 ver = LNET_PROC_VER_GET(*ppos);
160 LIBCFS_ALLOC(tmpstr, tmpsiz);
164 s = tmpstr; /* points to current position in tmpstr[] */
167 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
168 the_lnet.ln_routing ? "enabled" : "disabled");
169 LASSERT(tmpstr + tmpsiz - s > 0);
171 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
172 "net", "hops", "priority", "state", "router");
173 LASSERT(tmpstr + tmpsiz - s > 0);
176 ver = (unsigned int)the_lnet.ln_remote_nets_version;
178 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
182 struct lnet_route *route = NULL;
183 struct lnet_remotenet *rnet = NULL;
185 struct list_head *rn_list;
190 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
192 LIBCFS_FREE(tmpstr, tmpsiz);
196 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
198 rn_list = &the_lnet.ln_remote_nets_hash[i];
202 while (n != rn_list && route == NULL) {
203 rnet = list_entry(n, struct lnet_remotenet,
206 r = rnet->lrn_routes.next;
208 while (r != &rnet->lrn_routes) {
209 struct lnet_route *re =
210 list_entry(r, struct lnet_route,
226 __u32 net = rnet->lrn_net;
227 __u32 hops = route->lr_hops;
228 unsigned int priority = route->lr_priority;
229 lnet_nid_t nid = route->lr_gateway->lpni_nid;
230 int alive = lnet_is_route_alive(route);
232 s += snprintf(s, tmpstr + tmpsiz - s,
233 "%-8s %4d %8u %7s %s\n",
234 libcfs_net2str(net), hops,
236 alive ? "up" : "down",
237 libcfs_nid2str(nid));
238 LASSERT(tmpstr + tmpsiz - s > 0);
244 len = s - tmpstr; /* how many bytes was written */
246 if (len > *lenp) { /* linux-supplied buffer is too small */
248 } else if (len > 0) { /* wrote something */
249 if (copy_to_user(buffer, tmpstr, len))
253 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
257 LIBCFS_FREE(tmpstr, tmpsiz);
266 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
267 size_t *lenp, loff_t *ppos)
272 const int tmpsiz = 256;
277 off = LNET_PROC_HOFF_GET(*ppos);
278 ver = LNET_PROC_VER_GET(*ppos);
285 LIBCFS_ALLOC(tmpstr, tmpsiz);
289 s = tmpstr; /* points to current position in tmpstr[] */
292 s += snprintf(s, tmpstr + tmpsiz - s,
293 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
294 "ref", "rtr_ref", "alive_cnt", "state",
295 "last_ping", "ping_sent", "deadline",
296 "down_ni", "router");
297 LASSERT(tmpstr + tmpsiz - s > 0);
300 ver = (unsigned int)the_lnet.ln_routers_version;
302 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
305 struct lnet_peer_ni *peer = NULL;
310 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
313 LIBCFS_FREE(tmpstr, tmpsiz);
317 r = the_lnet.ln_routers.next;
319 while (r != &the_lnet.ln_routers) {
320 struct lnet_peer_ni *lp =
321 list_entry(r, struct lnet_peer_ni,
334 lnet_nid_t nid = peer->lpni_nid;
335 time64_t now = ktime_get_seconds();
336 time64_t deadline = peer->lpni_ping_deadline;
337 int nrefs = atomic_read(&peer->lpni_refcount);
338 int nrtrrefs = peer->lpni_rtr_refcount;
339 int alive_cnt = peer->lpni_alive_count;
340 int alive = peer->lpni_alive;
341 int pingsent = !peer->lpni_ping_notsent;
342 time64_t last_ping = now - peer->lpni_ping_timestamp;
344 struct lnet_route *rtr;
346 if ((peer->lpni_ping_feats &
347 LNET_PING_FEAT_NI_STATUS) != 0) {
348 list_for_each_entry(rtr, &peer->lpni_routes,
350 /* downis on any route should be the
351 * number of downis on the gateway */
352 if (rtr->lr_downis != 0) {
353 down_ni = rtr->lr_downis;
360 s += snprintf(s, tmpstr + tmpsiz - s,
361 "%-4d %7d %9d %6s %12llu %9d %8s %7d %s\n",
362 nrefs, nrtrrefs, alive_cnt,
363 alive ? "up" : "down", last_ping,
364 pingsent, "NA", down_ni,
365 libcfs_nid2str(nid));
367 s += snprintf(s, tmpstr + tmpsiz - s,
368 "%-4d %7d %9d %6s %12llu %9d %8llu %7d %s\n",
369 nrefs, nrtrrefs, alive_cnt,
370 alive ? "up" : "down", last_ping,
373 down_ni, libcfs_nid2str(nid));
374 LASSERT(tmpstr + tmpsiz - s > 0);
380 len = s - tmpstr; /* how many bytes was written */
382 if (len > *lenp) { /* linux-supplied buffer is too small */
384 } else if (len > 0) { /* wrote something */
385 if (copy_to_user(buffer, tmpstr, len))
389 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
393 LIBCFS_FREE(tmpstr, tmpsiz);
401 /* TODO: there should be no direct access to ptable. We should add a set
402 * of APIs that give access to the ptable and its members */
404 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
405 size_t *lenp, loff_t *ppos)
407 const int tmpsiz = 256;
408 struct lnet_peer_table *ptable;
411 int cpt = LNET_PROC_CPT_GET(*ppos);
412 int ver = LNET_PROC_VER_GET(*ppos);
413 int hash = LNET_PROC_HASH_GET(*ppos);
414 int hoff = LNET_PROC_HOFF_GET(*ppos);
420 struct lnet_peer_ni *peer;
422 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
424 for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
425 list_for_each_entry(peer,
426 &ptable->pt_hash[hash],
428 peer->lpni_mintxcredits =
429 peer->lpni_txcredits;
430 peer->lpni_minrtrcredits =
431 peer->lpni_rtrcredits;
443 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
445 if (cpt >= LNET_CPT_NUMBER) {
450 LIBCFS_ALLOC(tmpstr, tmpsiz);
454 s = tmpstr; /* points to current position in tmpstr[] */
457 s += snprintf(s, tmpstr + tmpsiz - s,
458 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
459 "nid", "refs", "state", "last", "max",
460 "rtr", "min", "tx", "min", "queue");
461 LASSERT(tmpstr + tmpsiz - s > 0);
465 struct lnet_peer_ni *peer;
475 ptable = the_lnet.ln_peer_tables[cpt];
477 ver = LNET_PROC_VERSION(ptable->pt_version);
479 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
480 lnet_net_unlock(cpt);
481 LIBCFS_FREE(tmpstr, tmpsiz);
485 while (hash < LNET_PEER_HASH_SIZE) {
487 p = ptable->pt_hash[hash].next;
489 while (p != &ptable->pt_hash[hash]) {
490 struct lnet_peer_ni *lp =
491 list_entry(p, struct lnet_peer_ni,
496 /* minor optimization: start from idx+1
497 * on next iteration if we've just
498 * drained lpni_hashlist */
499 if (lp->lpni_hashlist.next ==
500 &ptable->pt_hash[hash]) {
511 p = lp->lpni_hashlist.next;
523 lnet_nid_t nid = peer->lpni_nid;
524 int nrefs = atomic_read(&peer->lpni_refcount);
525 time64_t lastalive = -1;
526 char *aliveness = "NA";
527 int maxcr = (peer->lpni_net) ?
528 peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
529 int txcr = peer->lpni_txcredits;
530 int mintxcr = peer->lpni_mintxcredits;
531 int rtrcr = peer->lpni_rtrcredits;
532 int minrtrcr = peer->lpni_minrtrcredits;
533 int txqnob = peer->lpni_txqnob;
535 if (lnet_isrouter(peer) ||
536 lnet_peer_aliveness_enabled(peer))
537 aliveness = peer->lpni_alive ? "up" : "down";
539 if (lnet_peer_aliveness_enabled(peer)) {
540 time64_t now = ktime_get_seconds();
542 lastalive = now - peer->lpni_last_alive;
544 /* No need to mess up peers contents with
545 * arbitrarily long integers - it suffices to
546 * know that lastalive is more than 10000s old
548 if (lastalive >= 10000)
552 lnet_net_unlock(cpt);
554 s += snprintf(s, tmpstr + tmpsiz - s,
555 "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
556 libcfs_nid2str(nid), nrefs, aliveness,
557 lastalive, maxcr, rtrcr, minrtrcr, txcr,
559 LASSERT(tmpstr + tmpsiz - s > 0);
561 } else { /* peer is NULL */
562 lnet_net_unlock(cpt);
565 if (hash == LNET_PEER_HASH_SIZE) {
569 if (peer == NULL && cpt < LNET_CPT_NUMBER)
574 len = s - tmpstr; /* how many bytes was written */
576 if (len > *lenp) { /* linux-supplied buffer is too small */
578 } else if (len > 0) { /* wrote something */
579 if (copy_to_user(buffer, tmpstr, len))
582 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
585 LIBCFS_FREE(tmpstr, tmpsiz);
593 static int __proc_lnet_buffers(void *data, int write,
594 loff_t pos, void __user *buffer, int nob)
606 /* (4 %d) * 4 * LNET_CPT_NUMBER */
607 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
608 LIBCFS_ALLOC(tmpstr, tmpsiz);
612 s = tmpstr; /* points to current position in tmpstr[] */
614 s += snprintf(s, tmpstr + tmpsiz - s,
616 "pages", "count", "credits", "min");
617 LASSERT(tmpstr + tmpsiz - s > 0);
619 if (the_lnet.ln_rtrpools == NULL)
620 goto out; /* I'm not a router */
622 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
623 struct lnet_rtrbufpool *rbp;
625 lnet_net_lock(LNET_LOCK_EX);
626 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
627 s += snprintf(s, tmpstr + tmpsiz - s,
630 rbp[idx].rbp_nbuffers,
631 rbp[idx].rbp_credits,
632 rbp[idx].rbp_mincredits);
633 LASSERT(tmpstr + tmpsiz - s > 0);
635 lnet_net_unlock(LNET_LOCK_EX);
641 if (pos >= min_t(int, len, strlen(tmpstr)))
644 rc = cfs_trace_copyout_string(buffer, nob,
647 LIBCFS_FREE(tmpstr, tmpsiz);
652 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
653 size_t *lenp, loff_t *ppos)
655 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
656 __proc_lnet_buffers);
660 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
661 size_t *lenp, loff_t *ppos)
663 int tmpsiz = 128 * LNET_CPT_NUMBER;
673 /* Just reset the min stat. */
675 struct lnet_net *net;
679 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
680 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
681 struct lnet_tx_queue *tq;
685 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
686 for (j = 0; ni->ni_cpts != NULL &&
687 j < ni->ni_ncpts; j++) {
688 if (i == ni->ni_cpts[j])
692 if (j == ni->ni_ncpts)
697 tq->tq_credits_min = tq->tq_credits;
708 LIBCFS_ALLOC(tmpstr, tmpsiz);
712 s = tmpstr; /* points to current position in tmpstr[] */
715 s += snprintf(s, tmpstr + tmpsiz - s,
716 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
717 "nid", "status", "alive", "refs", "peer",
718 "rtr", "max", "tx", "min");
719 LASSERT (tmpstr + tmpsiz - s > 0);
721 struct lnet_ni *ni = NULL;
722 int skip = *ppos - 1;
726 ni = lnet_get_ni_idx_locked(skip);
729 struct lnet_tx_queue *tq;
731 time64_t now = ktime_get_real_seconds();
732 time64_t last_alive = -1;
736 if (the_lnet.ln_routing)
737 last_alive = now - ni->ni_last_alive;
739 /* @lo forever alive */
740 if (ni->ni_net->net_lnd->lnd_type == LOLND)
744 LASSERT(ni->ni_status != NULL);
745 stat = (ni->ni_status->ns_status ==
746 LNET_NI_STATUS_UP) ? "up" : "down";
749 /* we actually output credits information for
750 * TX queue of each partition */
751 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
752 for (j = 0; ni->ni_cpts != NULL &&
753 j < ni->ni_ncpts; j++) {
754 if (i == ni->ni_cpts[j])
758 if (j == ni->ni_ncpts)
764 s += snprintf(s, tmpstr + tmpsiz - s,
765 "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
766 libcfs_nid2str(ni->ni_nid), stat,
767 last_alive, *ni->ni_refs[i],
768 ni->ni_net->net_tunables.lct_peer_tx_credits,
769 ni->ni_net->net_tunables.lct_peer_rtr_credits,
771 tq->tq_credits, tq->tq_credits_min);
775 LASSERT(tmpstr + tmpsiz - s > 0);
781 len = s - tmpstr; /* how many bytes was written */
783 if (len > *lenp) { /* linux-supplied buffer is too small */
785 } else if (len > 0) { /* wrote something */
786 if (copy_to_user(buffer, tmpstr, len))
792 LIBCFS_FREE(tmpstr, tmpsiz);
800 struct lnet_portal_rotors {
806 static struct lnet_portal_rotors portal_rotors[] = {
808 .pr_value = LNET_PTL_ROTOR_OFF,
810 .pr_desc = "Turn off message rotor for wildcard portals"
813 .pr_value = LNET_PTL_ROTOR_ON,
815 .pr_desc = "round-robin dispatch all PUT messages for "
819 .pr_value = LNET_PTL_ROTOR_RR_RT,
821 .pr_desc = "round-robin dispatch routed PUT message for "
825 .pr_value = LNET_PTL_ROTOR_HASH_RT,
826 .pr_name = "HASH_RT",
827 .pr_desc = "dispatch routed PUT message by hashing source "
828 "NID for wildcard portals"
837 static int __proc_lnet_portal_rotor(void *data, int write,
838 loff_t pos, void __user *buffer, int nob)
840 const int buf_len = 128;
846 LIBCFS_ALLOC(buf, buf_len);
853 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
854 if (portal_rotors[i].pr_value == portal_rotor)
858 LASSERT(portal_rotors[i].pr_value == portal_rotor);
861 rc = snprintf(buf, buf_len,
862 "{\n\tportals: all\n"
863 "\trotor: %s\n\tdescription: %s\n}",
864 portal_rotors[i].pr_name,
865 portal_rotors[i].pr_desc);
867 if (pos >= min_t(int, rc, buf_len)) {
870 rc = cfs_trace_copyout_string(buffer, nob,
876 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
880 tmp = cfs_trimwhite(buf);
884 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
885 if (strncasecmp(portal_rotors[i].pr_name, tmp,
886 strlen(portal_rotors[i].pr_name)) == 0) {
887 portal_rotor = portal_rotors[i].pr_value;
894 LIBCFS_FREE(buf, buf_len);
899 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
900 size_t *lenp, loff_t *ppos)
902 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
903 __proc_lnet_portal_rotor);
907 static struct ctl_table lnet_table[] = {
909 * NB No .strategy entries have been provided since sysctl(8) prefers
910 * to go via /proc for portability.
916 .proc_handler = &proc_lnet_stats,
920 .procname = "routes",
922 .proc_handler = &proc_lnet_routes,
926 .procname = "routers",
928 .proc_handler = &proc_lnet_routers,
934 .proc_handler = &proc_lnet_peers,
938 .procname = "buffers",
940 .proc_handler = &proc_lnet_buffers,
946 .proc_handler = &proc_lnet_nis,
950 .procname = "portal_rotor",
952 .proc_handler = &proc_lnet_portal_rotor,
957 void lnet_router_debugfs_init(void)
959 lnet_insert_debugfs(lnet_table, NULL);