2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, Whamcloud, Inc.
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 cfs_sysctl_table_header_t *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 DECLARE_LL_PROC_PPOS_DECL;
167 CLASSERT(sizeof(loff_t) >= 4);
169 off = LNET_PROC_HOFF_GET(*ppos);
170 ver = LNET_PROC_VER_GET(*ppos);
177 LIBCFS_ALLOC(tmpstr, tmpsiz);
181 s = tmpstr; /* points to current position in tmpstr[] */
184 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
185 the_lnet.ln_routing ? "enabled" : "disabled");
186 LASSERT (tmpstr + tmpsiz - s > 0);
188 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %7s %s\n",
189 "net", "hops", "state", "router");
190 LASSERT (tmpstr + tmpsiz - s > 0);
193 ver = (unsigned int)the_lnet.ln_remote_nets_version;
195 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
199 lnet_route_t *route = NULL;
200 lnet_remotenet_t *rnet = NULL;
205 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
207 LIBCFS_FREE(tmpstr, tmpsiz);
211 n = the_lnet.ln_remote_nets.next;
213 while (n != &the_lnet.ln_remote_nets && route == NULL) {
214 rnet = cfs_list_entry(n, lnet_remotenet_t, lrn_list);
216 r = rnet->lrn_routes.next;
218 while (r != &rnet->lrn_routes) {
220 cfs_list_entry(r, lnet_route_t,
235 __u32 net = rnet->lrn_net;
236 unsigned int hops = route->lr_hops;
237 lnet_nid_t nid = route->lr_gateway->lp_nid;
238 int alive = route->lr_gateway->lp_alive;
240 s += snprintf(s, tmpstr + tmpsiz - s,
242 libcfs_net2str(net), hops,
243 alive ? "up" : "down",
244 libcfs_nid2str(nid));
245 LASSERT(tmpstr + tmpsiz - s > 0);
251 len = s - tmpstr; /* how many bytes was written */
253 if (len > *lenp) { /* linux-supplied buffer is too small */
255 } else if (len > 0) { /* wrote something */
256 if (cfs_copy_to_user(buffer, tmpstr, len))
260 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
264 LIBCFS_FREE(tmpstr, tmpsiz);
272 int LL_PROC_PROTO(proc_lnet_routers)
277 const int tmpsiz = 256;
282 DECLARE_LL_PROC_PPOS_DECL;
284 off = LNET_PROC_HOFF_GET(*ppos);
285 ver = LNET_PROC_VER_GET(*ppos);
292 LIBCFS_ALLOC(tmpstr, tmpsiz);
296 s = tmpstr; /* points to current position in tmpstr[] */
299 s += snprintf(s, tmpstr + tmpsiz - s,
300 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
301 "ref", "rtr_ref", "alive_cnt", "state",
302 "last_ping", "ping_sent", "deadline",
303 "down_ni", "router");
304 LASSERT(tmpstr + tmpsiz - s > 0);
307 ver = (unsigned int)the_lnet.ln_routers_version;
309 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
312 struct lnet_peer *peer = NULL;
317 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
320 LIBCFS_FREE(tmpstr, tmpsiz);
324 r = the_lnet.ln_routers.next;
326 while (r != &the_lnet.ln_routers) {
327 lnet_peer_t *lp = cfs_list_entry(r, lnet_peer_t,
340 lnet_nid_t nid = peer->lp_nid;
341 cfs_time_t now = cfs_time_current();
342 cfs_time_t deadline = peer->lp_ping_deadline;
343 int nrefs = peer->lp_refcount;
344 int nrtrrefs = peer->lp_rtr_refcount;
345 int alive_cnt = peer->lp_alive_count;
346 int alive = peer->lp_alive;
347 int pingsent = !peer->lp_ping_notsent;
348 int last_ping = cfs_duration_sec(cfs_time_sub(now,
349 peer->lp_ping_timestamp));
353 if ((peer->lp_ping_feats &
354 LNET_PING_FEAT_NI_STATUS) != 0) {
355 cfs_list_for_each_entry(rtr, &peer->lp_routes,
357 /* downis on any route should be the
358 * number of downis on the gateway */
359 if (rtr->lr_downis != 0) {
360 down_ni = rtr->lr_downis;
367 s += snprintf(s, tmpstr + tmpsiz - s,
368 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
369 nrefs, nrtrrefs, alive_cnt,
370 alive ? "up" : "down", last_ping,
371 pingsent, "NA", down_ni,
372 libcfs_nid2str(nid));
374 s += snprintf(s, tmpstr + tmpsiz - s,
375 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
376 nrefs, nrtrrefs, alive_cnt,
377 alive ? "up" : "down", last_ping,
379 cfs_duration_sec(cfs_time_sub(deadline, now)),
380 down_ni, libcfs_nid2str(nid));
381 LASSERT (tmpstr + tmpsiz - s > 0);
387 len = s - tmpstr; /* how many bytes was written */
389 if (len > *lenp) { /* linux-supplied buffer is too small */
391 } else if (len > 0) { /* wrote something */
392 if (cfs_copy_to_user(buffer, tmpstr, len))
396 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
400 LIBCFS_FREE(tmpstr, tmpsiz);
408 int LL_PROC_PROTO(proc_lnet_peers)
410 const int tmpsiz = 256;
411 struct lnet_peer_table *ptable;
414 int cpt = LNET_PROC_CPT_GET(*ppos);
415 int ver = LNET_PROC_VER_GET(*ppos);
416 int hash = LNET_PROC_HASH_GET(*ppos);
417 int hoff = LNET_PROC_HOFF_GET(*ppos);
421 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
427 if (cpt >= LNET_CPT_NUMBER) {
432 LIBCFS_ALLOC(tmpstr, tmpsiz);
436 s = tmpstr; /* points to current position in tmpstr[] */
439 s += snprintf(s, tmpstr + tmpsiz - s,
440 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
441 "nid", "refs", "state", "last", "max",
442 "rtr", "min", "tx", "min", "queue");
443 LASSERT (tmpstr + tmpsiz - s > 0);
447 struct lnet_peer *peer;
456 ptable = the_lnet.ln_peer_tables[cpt];
458 ver = LNET_PROC_VERSION(ptable->pt_version);
460 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
461 lnet_net_unlock(cpt);
462 LIBCFS_FREE(tmpstr, tmpsiz);
466 while (hash < LNET_PEER_HASH_SIZE) {
468 p = ptable->pt_hash[hash].next;
470 while (p != &ptable->pt_hash[hash]) {
471 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
476 /* minor optimization: start from idx+1
477 * on next iteration if we've just
478 * drained lp_hashlist */
479 if (lp->lp_hashlist.next ==
480 &ptable->pt_hash[hash]) {
491 p = lp->lp_hashlist.next;
503 lnet_nid_t nid = peer->lp_nid;
504 int nrefs = peer->lp_refcount;
506 char *aliveness = "NA";
507 int maxcr = peer->lp_ni->ni_peertxcredits;
508 int txcr = peer->lp_txcredits;
509 int mintxcr = peer->lp_mintxcredits;
510 int rtrcr = peer->lp_rtrcredits;
511 int minrtrcr = peer->lp_minrtrcredits;
512 int txqnob = peer->lp_txqnob;
514 if (lnet_isrouter(peer) ||
515 lnet_peer_aliveness_enabled(peer))
516 aliveness = peer->lp_alive ? "up" : "down";
518 if (lnet_peer_aliveness_enabled(peer)) {
519 cfs_time_t now = cfs_time_current();
520 cfs_duration_t delta;
522 delta = cfs_time_sub(now, peer->lp_last_alive);
523 lastalive = cfs_duration_sec(delta);
525 /* No need to mess up peers contents with
526 * arbitrarily long integers - it suffices to
527 * know that lastalive is more than 10000s old
529 if (lastalive >= 10000)
533 lnet_net_unlock(cpt);
535 s += snprintf(s, tmpstr + tmpsiz - s,
536 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
537 libcfs_nid2str(nid), nrefs, aliveness,
538 lastalive, maxcr, rtrcr, minrtrcr, txcr,
540 LASSERT (tmpstr + tmpsiz - s > 0);
542 } else { /* peer is NULL */
543 lnet_net_unlock(cpt);
546 if (hash == LNET_PEER_HASH_SIZE) {
550 if (peer == NULL && cpt < LNET_CPT_NUMBER)
555 len = s - tmpstr; /* how many bytes was written */
557 if (len > *lenp) { /* linux-supplied buffer is too small */
559 } else if (len > 0) { /* wrote something */
560 if (cfs_copy_to_user(buffer, tmpstr, len))
563 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
566 LIBCFS_FREE(tmpstr, tmpsiz);
574 static int __proc_lnet_buffers(void *data, int write,
575 loff_t pos, void *buffer, int nob)
587 /* (4 %d) * 4 * LNET_CPT_NUMBER */
588 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
589 LIBCFS_ALLOC(tmpstr, tmpsiz);
593 s = tmpstr; /* points to current position in tmpstr[] */
595 s += snprintf(s, tmpstr + tmpsiz - s,
597 "pages", "count", "credits", "min");
598 LASSERT (tmpstr + tmpsiz - s > 0);
600 if (the_lnet.ln_rtrpools == NULL)
601 goto out; /* I'm not a router */
603 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
604 lnet_rtrbufpool_t *rbp;
606 lnet_net_lock(LNET_LOCK_EX);
607 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
608 s += snprintf(s, tmpstr + tmpsiz - s,
611 rbp[idx].rbp_nbuffers,
612 rbp[idx].rbp_credits,
613 rbp[idx].rbp_mincredits);
614 LASSERT(tmpstr + tmpsiz - s > 0);
616 lnet_net_unlock(LNET_LOCK_EX);
622 if (pos >= min_t(int, len, strlen(tmpstr)))
625 rc = cfs_trace_copyout_string(buffer, nob,
628 LIBCFS_FREE(tmpstr, tmpsiz);
632 DECLARE_PROC_HANDLER(proc_lnet_buffers);
634 int LL_PROC_PROTO(proc_lnet_nis)
636 int tmpsiz = 128 * LNET_CPT_NUMBER;
642 DECLARE_LL_PROC_PPOS_DECL;
649 LIBCFS_ALLOC(tmpstr, tmpsiz);
653 s = tmpstr; /* points to current position in tmpstr[] */
656 s += snprintf(s, tmpstr + tmpsiz - s,
657 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
658 "nid", "status", "alive", "refs", "peer",
659 "rtr", "max", "tx", "min");
660 LASSERT (tmpstr + tmpsiz - s > 0);
663 lnet_ni_t *ni = NULL;
664 int skip = *ppos - 1;
668 n = the_lnet.ln_nis.next;
670 while (n != &the_lnet.ln_nis) {
671 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
683 struct lnet_tx_queue *tq;
685 long now = cfs_time_current_sec();
690 if (the_lnet.ln_routing)
691 last_alive = now - ni->ni_last_alive;
693 /* @lo forever alive */
694 if (ni->ni_lnd->lnd_type == LOLND)
698 LASSERT(ni->ni_status != NULL);
699 stat = (ni->ni_status->ns_status ==
700 LNET_NI_STATUS_UP) ? "up" : "down";
703 /* we actually output credits information for
704 * TX queue of each partition */
705 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
706 for (j = 0; ni->ni_cpts != NULL &&
707 j < ni->ni_ncpts; j++) {
708 if (i == ni->ni_cpts[j])
712 if (j == ni->ni_ncpts)
718 s += snprintf(s, tmpstr + tmpsiz - s,
719 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
720 libcfs_nid2str(ni->ni_nid), stat,
721 last_alive, *ni->ni_refs[i],
722 ni->ni_peertxcredits,
723 ni->ni_peerrtrcredits,
725 tq->tq_credits, tq->tq_credits_min);
729 LASSERT(tmpstr + tmpsiz - s > 0);
735 len = s - tmpstr; /* how many bytes was written */
737 if (len > *lenp) { /* linux-supplied buffer is too small */
739 } else if (len > 0) { /* wrote something */
740 if (cfs_copy_to_user(buffer, tmpstr, len))
746 LIBCFS_FREE(tmpstr, tmpsiz);
754 struct lnet_portal_rotors {
760 static struct lnet_portal_rotors portal_rotors[] = {
762 .pr_value = LNET_PTL_ROTOR_OFF,
764 .pr_desc = "Turn off message rotor for wildcard portals"
767 .pr_value = LNET_PTL_ROTOR_ON,
769 .pr_desc = "round-robin dispatch all PUT messages for "
773 .pr_value = LNET_PTL_ROTOR_RR_RT,
775 .pr_desc = "round-robin dispatch routed PUT message for "
779 .pr_value = LNET_PTL_ROTOR_HASH_RT,
780 .pr_name = "HASH_RT",
781 .pr_desc = "dispatch routed PUT message by hashing source "
782 "NID for wildcard portals"
791 extern int portal_rotor;
793 static int __proc_lnet_portal_rotor(void *data, int write,
794 loff_t pos, void *buffer, int nob)
796 const int buf_len = 128;
802 LIBCFS_ALLOC(buf, buf_len);
809 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
810 if (portal_rotors[i].pr_value == portal_rotor)
814 LASSERT(portal_rotors[i].pr_value == portal_rotor);
817 rc = snprintf(buf, buf_len,
818 "{\n\tportals: all\n"
819 "\trotor: %s\n\tdescription: %s\n}",
820 portal_rotors[i].pr_name,
821 portal_rotors[i].pr_desc);
823 if (pos >= min_t(int, rc, buf_len)) {
826 rc = cfs_trace_copyout_string(buffer, nob,
832 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
836 tmp = cfs_trimwhite(buf);
840 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
841 if (cfs_strncasecmp(portal_rotors[i].pr_name, tmp,
842 strlen(portal_rotors[i].pr_name)) == 0) {
843 portal_rotor = portal_rotors[i].pr_value;
850 LIBCFS_FREE(buf, buf_len);
853 DECLARE_PROC_HANDLER(proc_lnet_portal_rotor);
855 static cfs_sysctl_table_t lnet_table[] = {
857 * NB No .strategy entries have been provided since sysctl(8) prefers
858 * to go via /proc for portability.
861 INIT_CTL_NAME(PSDEV_LNET_STATS)
864 .proc_handler = &proc_lnet_stats,
867 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
868 .procname = "routes",
870 .proc_handler = &proc_lnet_routes,
873 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
874 .procname = "routers",
876 .proc_handler = &proc_lnet_routers,
879 INIT_CTL_NAME(PSDEV_LNET_PEERS)
882 .proc_handler = &proc_lnet_peers,
885 INIT_CTL_NAME(PSDEV_LNET_PEERS)
886 .procname = "buffers",
888 .proc_handler = &proc_lnet_buffers,
891 INIT_CTL_NAME(PSDEV_LNET_NIS)
894 .proc_handler = &proc_lnet_nis,
897 INIT_CTL_NAME(PSDEV_LNET_PTL_ROTOR)
898 .procname = "portal_rotor",
900 .proc_handler = &proc_lnet_portal_rotor,
907 static cfs_sysctl_table_t top_table[] = {
909 INIT_CTL_NAME(CTL_LNET)
925 if (lnet_table_header == NULL)
926 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
934 if (lnet_table_header != NULL)
935 cfs_unregister_sysctl_table(lnet_table_header);
937 lnet_table_header = NULL;