2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2013, Intel Corporation.
6 * This file is part of Portals
7 * http://sourceforge.net/projects/sandiaportals/
9 * Portals is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Portals is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Portals; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LNET
25 #include <libcfs/libcfs.h>
26 #include <lnet/lib-lnet.h>
28 #if defined(__KERNEL__) && defined(LNET_ROUTER)
30 /* This is really lnet_proc.c. You might need to update sanity test 215
31 * if any file format is changed. */
33 static struct ctl_table_header *lnet_table_header = NULL;
35 #ifndef HAVE_SYSCTL_UNNUMBERED
36 #define CTL_LNET (0x100)
38 PSDEV_LNET_STATS = 100,
47 #define CTL_LNET CTL_UNNUMBERED
48 #define PSDEV_LNET_STATS CTL_UNNUMBERED
49 #define PSDEV_LNET_ROUTES CTL_UNNUMBERED
50 #define PSDEV_LNET_ROUTERS CTL_UNNUMBERED
51 #define PSDEV_LNET_PEERS CTL_UNNUMBERED
52 #define PSDEV_LNET_BUFFERS CTL_UNNUMBERED
53 #define PSDEV_LNET_NIS CTL_UNNUMBERED
54 #define PSDEV_LNET_PTL_ROTOR CTL_UNNUMBERED
57 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
59 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
61 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
62 /* change version, 16 bits or 8 bits */
63 #define LNET_PROC_VER_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
65 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
67 * bits for peer hash offset
68 * NB: we don't use the highest bit of *ppos because it's signed
70 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
71 LNET_PROC_CPT_BITS - \
72 LNET_PROC_VER_BITS - \
73 LNET_PROC_HASH_BITS - 1)
74 /* bits for hash index + position */
75 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
76 /* bits for peer hash table + hash version */
77 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
79 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
80 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
81 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
82 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
84 #define LNET_PROC_CPT_GET(pos) \
85 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
87 #define LNET_PROC_VER_GET(pos) \
88 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
90 #define LNET_PROC_HASH_GET(pos) \
91 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
93 #define LNET_PROC_HOFF_GET(pos) \
94 (int)((pos) & LNET_PROC_HOFF_MASK)
96 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
97 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
98 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
99 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
100 ((off) & LNET_PROC_HOFF_MASK))
102 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
104 static int __proc_lnet_stats(void *data, int write,
105 loff_t pos, void *buffer, int nob)
108 lnet_counters_t *ctrs;
111 const int tmpsiz = 256; /* 7 %u and 4 LPU64 */
114 lnet_counters_reset();
120 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
124 LIBCFS_ALLOC(tmpstr, tmpsiz);
125 if (tmpstr == NULL) {
126 LIBCFS_FREE(ctrs, sizeof(*ctrs));
130 lnet_counters_get(ctrs);
132 len = snprintf(tmpstr, tmpsiz,
133 "%u %u %u %u %u %u %u "LPU64" "LPU64" "
135 ctrs->msgs_alloc, ctrs->msgs_max,
137 ctrs->send_count, ctrs->recv_count,
138 ctrs->route_count, ctrs->drop_count,
139 ctrs->send_length, ctrs->recv_length,
140 ctrs->route_length, ctrs->drop_length);
142 if (pos >= min_t(int, len, strlen(tmpstr)))
145 rc = cfs_trace_copyout_string(buffer, nob,
148 LIBCFS_FREE(tmpstr, tmpsiz);
149 LIBCFS_FREE(ctrs, sizeof(*ctrs));
153 DECLARE_PROC_HANDLER(proc_lnet_stats);
155 int LL_PROC_PROTO(proc_lnet_routes)
157 const int tmpsiz = 256;
165 CLASSERT(sizeof(loff_t) >= 4);
167 off = LNET_PROC_HOFF_GET(*ppos);
168 ver = LNET_PROC_VER_GET(*ppos);
175 LIBCFS_ALLOC(tmpstr, tmpsiz);
179 s = tmpstr; /* points to current position in tmpstr[] */
182 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
183 the_lnet.ln_routing ? "enabled" : "disabled");
184 LASSERT (tmpstr + tmpsiz - s > 0);
186 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
187 "net", "hops", "priority", "state", "router");
188 LASSERT (tmpstr + tmpsiz - s > 0);
191 ver = (unsigned int)the_lnet.ln_remote_nets_version;
193 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
197 lnet_route_t *route = NULL;
198 lnet_remotenet_t *rnet = NULL;
205 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
207 LIBCFS_FREE(tmpstr, tmpsiz);
211 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
213 rn_list = &the_lnet.ln_remote_nets_hash[i];
217 while (n != rn_list && route == NULL) {
218 rnet = cfs_list_entry(n, lnet_remotenet_t,
221 r = rnet->lrn_routes.next;
223 while (r != &rnet->lrn_routes) {
225 cfs_list_entry(r, lnet_route_t,
241 __u32 net = rnet->lrn_net;
242 unsigned int hops = route->lr_hops;
243 unsigned int priority = route->lr_priority;
244 lnet_nid_t nid = route->lr_gateway->lp_nid;
245 int alive = lnet_is_route_alive(route);
247 s += snprintf(s, tmpstr + tmpsiz - s,
248 "%-8s %4u %8u %7s %s\n",
249 libcfs_net2str(net), hops,
251 alive ? "up" : "down",
252 libcfs_nid2str(nid));
253 LASSERT(tmpstr + tmpsiz - s > 0);
259 len = s - tmpstr; /* how many bytes was written */
261 if (len > *lenp) { /* linux-supplied buffer is too small */
263 } else if (len > 0) { /* wrote something */
264 if (copy_to_user(buffer, tmpstr, len))
268 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
272 LIBCFS_FREE(tmpstr, tmpsiz);
280 int LL_PROC_PROTO(proc_lnet_routers)
285 const int tmpsiz = 256;
290 off = LNET_PROC_HOFF_GET(*ppos);
291 ver = LNET_PROC_VER_GET(*ppos);
298 LIBCFS_ALLOC(tmpstr, tmpsiz);
302 s = tmpstr; /* points to current position in tmpstr[] */
305 s += snprintf(s, tmpstr + tmpsiz - s,
306 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
307 "ref", "rtr_ref", "alive_cnt", "state",
308 "last_ping", "ping_sent", "deadline",
309 "down_ni", "router");
310 LASSERT(tmpstr + tmpsiz - s > 0);
313 ver = (unsigned int)the_lnet.ln_routers_version;
315 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
318 struct lnet_peer *peer = NULL;
323 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
326 LIBCFS_FREE(tmpstr, tmpsiz);
330 r = the_lnet.ln_routers.next;
332 while (r != &the_lnet.ln_routers) {
333 lnet_peer_t *lp = cfs_list_entry(r, lnet_peer_t,
346 lnet_nid_t nid = peer->lp_nid;
347 cfs_time_t now = cfs_time_current();
348 cfs_time_t deadline = peer->lp_ping_deadline;
349 int nrefs = peer->lp_refcount;
350 int nrtrrefs = peer->lp_rtr_refcount;
351 int alive_cnt = peer->lp_alive_count;
352 int alive = peer->lp_alive;
353 int pingsent = !peer->lp_ping_notsent;
354 int last_ping = cfs_duration_sec(cfs_time_sub(now,
355 peer->lp_ping_timestamp));
359 if ((peer->lp_ping_feats &
360 LNET_PING_FEAT_NI_STATUS) != 0) {
361 cfs_list_for_each_entry(rtr, &peer->lp_routes,
363 /* downis on any route should be the
364 * number of downis on the gateway */
365 if (rtr->lr_downis != 0) {
366 down_ni = rtr->lr_downis;
373 s += snprintf(s, tmpstr + tmpsiz - s,
374 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
375 nrefs, nrtrrefs, alive_cnt,
376 alive ? "up" : "down", last_ping,
377 pingsent, "NA", down_ni,
378 libcfs_nid2str(nid));
380 s += snprintf(s, tmpstr + tmpsiz - s,
381 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
382 nrefs, nrtrrefs, alive_cnt,
383 alive ? "up" : "down", last_ping,
385 cfs_duration_sec(cfs_time_sub(deadline, now)),
386 down_ni, libcfs_nid2str(nid));
387 LASSERT (tmpstr + tmpsiz - s > 0);
393 len = s - tmpstr; /* how many bytes was written */
395 if (len > *lenp) { /* linux-supplied buffer is too small */
397 } else if (len > 0) { /* wrote something */
398 if (copy_to_user(buffer, tmpstr, len))
402 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
406 LIBCFS_FREE(tmpstr, tmpsiz);
414 int LL_PROC_PROTO(proc_lnet_peers)
416 const int tmpsiz = 256;
417 struct lnet_peer_table *ptable;
420 int cpt = LNET_PROC_CPT_GET(*ppos);
421 int ver = LNET_PROC_VER_GET(*ppos);
422 int hash = LNET_PROC_HASH_GET(*ppos);
423 int hoff = LNET_PROC_HOFF_GET(*ppos);
427 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
433 if (cpt >= LNET_CPT_NUMBER) {
438 LIBCFS_ALLOC(tmpstr, tmpsiz);
442 s = tmpstr; /* points to current position in tmpstr[] */
445 s += snprintf(s, tmpstr + tmpsiz - s,
446 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
447 "nid", "refs", "state", "last", "max",
448 "rtr", "min", "tx", "min", "queue");
449 LASSERT (tmpstr + tmpsiz - s > 0);
453 struct lnet_peer *peer;
462 ptable = the_lnet.ln_peer_tables[cpt];
464 ver = LNET_PROC_VERSION(ptable->pt_version);
466 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
467 lnet_net_unlock(cpt);
468 LIBCFS_FREE(tmpstr, tmpsiz);
472 while (hash < LNET_PEER_HASH_SIZE) {
474 p = ptable->pt_hash[hash].next;
476 while (p != &ptable->pt_hash[hash]) {
477 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
482 /* minor optimization: start from idx+1
483 * on next iteration if we've just
484 * drained lp_hashlist */
485 if (lp->lp_hashlist.next ==
486 &ptable->pt_hash[hash]) {
497 p = lp->lp_hashlist.next;
509 lnet_nid_t nid = peer->lp_nid;
510 int nrefs = peer->lp_refcount;
512 char *aliveness = "NA";
513 int maxcr = peer->lp_ni->ni_peertxcredits;
514 int txcr = peer->lp_txcredits;
515 int mintxcr = peer->lp_mintxcredits;
516 int rtrcr = peer->lp_rtrcredits;
517 int minrtrcr = peer->lp_minrtrcredits;
518 int txqnob = peer->lp_txqnob;
520 if (lnet_isrouter(peer) ||
521 lnet_peer_aliveness_enabled(peer))
522 aliveness = peer->lp_alive ? "up" : "down";
524 if (lnet_peer_aliveness_enabled(peer)) {
525 cfs_time_t now = cfs_time_current();
526 cfs_duration_t delta;
528 delta = cfs_time_sub(now, peer->lp_last_alive);
529 lastalive = cfs_duration_sec(delta);
531 /* No need to mess up peers contents with
532 * arbitrarily long integers - it suffices to
533 * know that lastalive is more than 10000s old
535 if (lastalive >= 10000)
539 lnet_net_unlock(cpt);
541 s += snprintf(s, tmpstr + tmpsiz - s,
542 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
543 libcfs_nid2str(nid), nrefs, aliveness,
544 lastalive, maxcr, rtrcr, minrtrcr, txcr,
546 LASSERT (tmpstr + tmpsiz - s > 0);
548 } else { /* peer is NULL */
549 lnet_net_unlock(cpt);
552 if (hash == LNET_PEER_HASH_SIZE) {
556 if (peer == NULL && cpt < LNET_CPT_NUMBER)
561 len = s - tmpstr; /* how many bytes was written */
563 if (len > *lenp) { /* linux-supplied buffer is too small */
565 } else if (len > 0) { /* wrote something */
566 if (copy_to_user(buffer, tmpstr, len))
569 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
572 LIBCFS_FREE(tmpstr, tmpsiz);
580 static int __proc_lnet_buffers(void *data, int write,
581 loff_t pos, void *buffer, int nob)
593 /* (4 %d) * 4 * LNET_CPT_NUMBER */
594 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
595 LIBCFS_ALLOC(tmpstr, tmpsiz);
599 s = tmpstr; /* points to current position in tmpstr[] */
601 s += snprintf(s, tmpstr + tmpsiz - s,
603 "pages", "count", "credits", "min");
604 LASSERT (tmpstr + tmpsiz - s > 0);
606 if (the_lnet.ln_rtrpools == NULL)
607 goto out; /* I'm not a router */
609 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
610 lnet_rtrbufpool_t *rbp;
612 lnet_net_lock(LNET_LOCK_EX);
613 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
614 s += snprintf(s, tmpstr + tmpsiz - s,
617 rbp[idx].rbp_nbuffers,
618 rbp[idx].rbp_credits,
619 rbp[idx].rbp_mincredits);
620 LASSERT(tmpstr + tmpsiz - s > 0);
622 lnet_net_unlock(LNET_LOCK_EX);
628 if (pos >= min_t(int, len, strlen(tmpstr)))
631 rc = cfs_trace_copyout_string(buffer, nob,
634 LIBCFS_FREE(tmpstr, tmpsiz);
638 DECLARE_PROC_HANDLER(proc_lnet_buffers);
640 int LL_PROC_PROTO(proc_lnet_nis)
642 int tmpsiz = 128 * LNET_CPT_NUMBER;
653 LIBCFS_ALLOC(tmpstr, tmpsiz);
657 s = tmpstr; /* points to current position in tmpstr[] */
660 s += snprintf(s, tmpstr + tmpsiz - s,
661 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
662 "nid", "status", "alive", "refs", "peer",
663 "rtr", "max", "tx", "min");
664 LASSERT (tmpstr + tmpsiz - s > 0);
667 lnet_ni_t *ni = NULL;
668 int skip = *ppos - 1;
672 n = the_lnet.ln_nis.next;
674 while (n != &the_lnet.ln_nis) {
675 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
687 struct lnet_tx_queue *tq;
689 long now = cfs_time_current_sec();
694 if (the_lnet.ln_routing)
695 last_alive = now - ni->ni_last_alive;
697 /* @lo forever alive */
698 if (ni->ni_lnd->lnd_type == LOLND)
702 LASSERT(ni->ni_status != NULL);
703 stat = (ni->ni_status->ns_status ==
704 LNET_NI_STATUS_UP) ? "up" : "down";
707 /* we actually output credits information for
708 * TX queue of each partition */
709 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
710 for (j = 0; ni->ni_cpts != NULL &&
711 j < ni->ni_ncpts; j++) {
712 if (i == ni->ni_cpts[j])
716 if (j == ni->ni_ncpts)
722 s += snprintf(s, tmpstr + tmpsiz - s,
723 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
724 libcfs_nid2str(ni->ni_nid), stat,
725 last_alive, *ni->ni_refs[i],
726 ni->ni_peertxcredits,
727 ni->ni_peerrtrcredits,
729 tq->tq_credits, tq->tq_credits_min);
733 LASSERT(tmpstr + tmpsiz - s > 0);
739 len = s - tmpstr; /* how many bytes was written */
741 if (len > *lenp) { /* linux-supplied buffer is too small */
743 } else if (len > 0) { /* wrote something */
744 if (copy_to_user(buffer, tmpstr, len))
750 LIBCFS_FREE(tmpstr, tmpsiz);
758 struct lnet_portal_rotors {
764 static struct lnet_portal_rotors portal_rotors[] = {
766 .pr_value = LNET_PTL_ROTOR_OFF,
768 .pr_desc = "Turn off message rotor for wildcard portals"
771 .pr_value = LNET_PTL_ROTOR_ON,
773 .pr_desc = "round-robin dispatch all PUT messages for "
777 .pr_value = LNET_PTL_ROTOR_RR_RT,
779 .pr_desc = "round-robin dispatch routed PUT message for "
783 .pr_value = LNET_PTL_ROTOR_HASH_RT,
784 .pr_name = "HASH_RT",
785 .pr_desc = "dispatch routed PUT message by hashing source "
786 "NID for wildcard portals"
795 extern int portal_rotor;
797 static int __proc_lnet_portal_rotor(void *data, int write,
798 loff_t pos, void *buffer, int nob)
800 const int buf_len = 128;
806 LIBCFS_ALLOC(buf, buf_len);
813 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
814 if (portal_rotors[i].pr_value == portal_rotor)
818 LASSERT(portal_rotors[i].pr_value == portal_rotor);
821 rc = snprintf(buf, buf_len,
822 "{\n\tportals: all\n"
823 "\trotor: %s\n\tdescription: %s\n}",
824 portal_rotors[i].pr_name,
825 portal_rotors[i].pr_desc);
827 if (pos >= min_t(int, rc, buf_len)) {
830 rc = cfs_trace_copyout_string(buffer, nob,
836 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
840 tmp = cfs_trimwhite(buf);
844 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
845 if (cfs_strncasecmp(portal_rotors[i].pr_name, tmp,
846 strlen(portal_rotors[i].pr_name)) == 0) {
847 portal_rotor = portal_rotors[i].pr_value;
854 LIBCFS_FREE(buf, buf_len);
857 DECLARE_PROC_HANDLER(proc_lnet_portal_rotor);
859 static struct ctl_table lnet_table[] = {
861 * NB No .strategy entries have been provided since sysctl(8) prefers
862 * to go via /proc for portability.
865 INIT_CTL_NAME(PSDEV_LNET_STATS)
868 .proc_handler = &proc_lnet_stats,
871 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
872 .procname = "routes",
874 .proc_handler = &proc_lnet_routes,
877 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
878 .procname = "routers",
880 .proc_handler = &proc_lnet_routers,
883 INIT_CTL_NAME(PSDEV_LNET_PEERS)
886 .proc_handler = &proc_lnet_peers,
889 INIT_CTL_NAME(PSDEV_LNET_PEERS)
890 .procname = "buffers",
892 .proc_handler = &proc_lnet_buffers,
895 INIT_CTL_NAME(PSDEV_LNET_NIS)
898 .proc_handler = &proc_lnet_nis,
901 INIT_CTL_NAME(PSDEV_LNET_PTL_ROTOR)
902 .procname = "portal_rotor",
904 .proc_handler = &proc_lnet_portal_rotor,
911 static struct ctl_table top_table[] = {
913 INIT_CTL_NAME(CTL_LNET)
929 if (lnet_table_header == NULL)
930 lnet_table_header = register_sysctl_table(top_table);
938 if (lnet_table_header != NULL)
939 unregister_sysctl_table(lnet_table_header);
941 lnet_table_header = NULL;