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
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 hoff = LNET_PROC_HOFF_GET(*ppos);
417 int hash = LNET_PROC_HASH_GET(*ppos);
421 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
427 if (cpt >= LNET_CPT_NUMBER)
430 LIBCFS_ALLOC(tmpstr, tmpsiz);
434 s = tmpstr; /* points to current position in tmpstr[] */
437 s += snprintf(s, tmpstr + tmpsiz - s,
438 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
439 "nid", "refs", "state", "last", "max",
440 "rtr", "min", "tx", "min", "queue");
441 LASSERT (tmpstr + tmpsiz - s > 0);
445 struct lnet_peer *peer = NULL;
446 cfs_list_t *p = NULL;
451 ptable = the_lnet.ln_peer_tables[cpt];
453 ver = LNET_PROC_VERSION(ptable->pt_version);
455 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
456 lnet_net_unlock(cpt);
457 LIBCFS_FREE(tmpstr, tmpsiz);
461 while (hash < LNET_PEER_HASH_SIZE) {
463 p = ptable->pt_hash[hash].next;
465 while (p != &ptable->pt_hash[hash]) {
466 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
471 /* minor optimization: start from idx+1
472 * on next iteration if we've just
473 * drained lp_hashlist */
474 if (lp->lp_hashlist.next ==
475 &ptable->pt_hash[hash]) {
486 p = lp->lp_hashlist.next;
498 lnet_nid_t nid = peer->lp_nid;
499 int nrefs = peer->lp_refcount;
501 char *aliveness = "NA";
502 int maxcr = peer->lp_ni->ni_peertxcredits;
503 int txcr = peer->lp_txcredits;
504 int mintxcr = peer->lp_mintxcredits;
505 int rtrcr = peer->lp_rtrcredits;
506 int minrtrcr = peer->lp_minrtrcredits;
507 int txqnob = peer->lp_txqnob;
509 if (lnet_isrouter(peer) ||
510 lnet_peer_aliveness_enabled(peer))
511 aliveness = peer->lp_alive ? "up" : "down";
513 if (lnet_peer_aliveness_enabled(peer)) {
514 cfs_time_t now = cfs_time_current();
515 cfs_duration_t delta;
517 delta = cfs_time_sub(now, peer->lp_last_alive);
518 lastalive = cfs_duration_sec(delta);
520 /* No need to mess up peers contents with
521 * arbitrarily long integers - it suffices to
522 * know that lastalive is more than 10000s old
524 if (lastalive >= 10000)
528 lnet_net_unlock(cpt);
530 s += snprintf(s, tmpstr + tmpsiz - s,
531 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
532 libcfs_nid2str(nid), nrefs, aliveness,
533 lastalive, maxcr, rtrcr, minrtrcr, txcr,
535 LASSERT (tmpstr + tmpsiz - s > 0);
537 } else { /* peer is NULL */
538 lnet_net_unlock(cpt);
540 if (hash == LNET_PEER_HASH_SIZE &&
541 cpt < LNET_CPT_NUMBER - 1) {
550 len = s - tmpstr; /* how many bytes was written */
552 if (len > *lenp) { /* linux-supplied buffer is too small */
554 } else if (len > 0) { /* wrote something */
555 if (cfs_copy_to_user(buffer, tmpstr, len))
558 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
561 LIBCFS_FREE(tmpstr, tmpsiz);
569 static int __proc_lnet_buffers(void *data, int write,
570 loff_t pos, void *buffer, int nob)
582 /* (4 %d) * 4 * LNET_CPT_NUMBER */
583 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
584 LIBCFS_ALLOC(tmpstr, tmpsiz);
588 s = tmpstr; /* points to current position in tmpstr[] */
590 s += snprintf(s, tmpstr + tmpsiz - s,
592 "pages", "count", "credits", "min");
593 LASSERT (tmpstr + tmpsiz - s > 0);
595 if (the_lnet.ln_rtrpools == NULL)
596 goto out; /* I'm not a router */
598 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
599 lnet_rtrbufpool_t *rbp;
601 lnet_net_lock(LNET_LOCK_EX);
602 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
603 s += snprintf(s, tmpstr + tmpsiz - s,
606 rbp[idx].rbp_nbuffers,
607 rbp[idx].rbp_credits,
608 rbp[idx].rbp_mincredits);
609 LASSERT(tmpstr + tmpsiz - s > 0);
611 lnet_net_unlock(LNET_LOCK_EX);
617 if (pos >= min_t(int, len, strlen(tmpstr)))
620 rc = cfs_trace_copyout_string(buffer, nob,
623 LIBCFS_FREE(tmpstr, tmpsiz);
627 DECLARE_PROC_HANDLER(proc_lnet_buffers);
629 int LL_PROC_PROTO(proc_lnet_nis)
631 int tmpsiz = 128 * LNET_CPT_NUMBER;
637 DECLARE_LL_PROC_PPOS_DECL;
644 LIBCFS_ALLOC(tmpstr, tmpsiz);
648 s = tmpstr; /* points to current position in tmpstr[] */
651 s += snprintf(s, tmpstr + tmpsiz - s,
652 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
653 "nid", "status", "alive", "refs", "peer",
654 "rtr", "max", "tx", "min");
655 LASSERT (tmpstr + tmpsiz - s > 0);
658 lnet_ni_t *ni = NULL;
659 int skip = *ppos - 1;
663 n = the_lnet.ln_nis.next;
665 while (n != &the_lnet.ln_nis) {
666 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
678 struct lnet_tx_queue *tq;
680 long now = cfs_time_current_sec();
685 if (the_lnet.ln_routing)
686 last_alive = now - ni->ni_last_alive;
688 /* @lo forever alive */
689 if (ni->ni_lnd->lnd_type == LOLND)
693 LASSERT(ni->ni_status != NULL);
694 stat = (ni->ni_status->ns_status ==
695 LNET_NI_STATUS_UP) ? "up" : "down";
698 /* we actually output credits information for
699 * TX queue of each partition */
700 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
701 for (j = 0; ni->ni_cpts != NULL &&
702 j < ni->ni_ncpts; j++) {
703 if (i == ni->ni_cpts[j])
707 if (j == ni->ni_ncpts)
713 s += snprintf(s, tmpstr + tmpsiz - s,
714 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
715 libcfs_nid2str(ni->ni_nid), stat,
716 last_alive, *ni->ni_refs[i],
717 ni->ni_peertxcredits,
718 ni->ni_peerrtrcredits,
720 tq->tq_credits, tq->tq_credits_min);
724 LASSERT(tmpstr + tmpsiz - s > 0);
730 len = s - tmpstr; /* how many bytes was written */
732 if (len > *lenp) { /* linux-supplied buffer is too small */
734 } else if (len > 0) { /* wrote something */
735 if (cfs_copy_to_user(buffer, tmpstr, len))
741 LIBCFS_FREE(tmpstr, tmpsiz);
749 struct lnet_portal_rotors {
755 static struct lnet_portal_rotors portal_rotors[] = {
757 .pr_value = LNET_PTL_ROTOR_OFF,
759 .pr_desc = "Turn off message rotor for wildcard portals"
762 .pr_value = LNET_PTL_ROTOR_ON,
764 .pr_desc = "round-robin dispatch all PUT messages for "
768 .pr_value = LNET_PTL_ROTOR_RR_RT,
770 .pr_desc = "round-robin dispatch routed PUT message for "
774 .pr_value = LNET_PTL_ROTOR_HASH_RT,
775 .pr_name = "HASH_RT",
776 .pr_desc = "dispatch routed PUT message by hashing source "
777 "NID for wildcard portals"
786 extern int portal_rotor;
788 static int __proc_lnet_portal_rotor(void *data, int write,
789 loff_t pos, void *buffer, int nob)
791 const int buf_len = 128;
797 LIBCFS_ALLOC(buf, buf_len);
804 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
805 if (portal_rotors[i].pr_value == portal_rotor)
809 LASSERT(portal_rotors[i].pr_value == portal_rotor);
812 rc = snprintf(buf, buf_len,
813 "{\n\tportals: all\n"
814 "\trotor: %s\n\tdescription: %s\n}",
815 portal_rotors[i].pr_name,
816 portal_rotors[i].pr_desc);
818 if (pos >= min_t(int, rc, buf_len)) {
821 rc = cfs_trace_copyout_string(buffer, nob,
827 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
831 tmp = cfs_trimwhite(buf);
835 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
836 if (cfs_strncasecmp(portal_rotors[i].pr_name, tmp,
837 strlen(portal_rotors[i].pr_name)) == 0) {
838 portal_rotor = portal_rotors[i].pr_value;
845 LIBCFS_FREE(buf, buf_len);
848 DECLARE_PROC_HANDLER(proc_lnet_portal_rotor);
850 static cfs_sysctl_table_t lnet_table[] = {
852 * NB No .strategy entries have been provided since sysctl(8) prefers
853 * to go via /proc for portability.
856 INIT_CTL_NAME(PSDEV_LNET_STATS)
859 .proc_handler = &proc_lnet_stats,
862 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
863 .procname = "routes",
865 .proc_handler = &proc_lnet_routes,
868 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
869 .procname = "routers",
871 .proc_handler = &proc_lnet_routers,
874 INIT_CTL_NAME(PSDEV_LNET_PEERS)
877 .proc_handler = &proc_lnet_peers,
880 INIT_CTL_NAME(PSDEV_LNET_PEERS)
881 .procname = "buffers",
883 .proc_handler = &proc_lnet_buffers,
886 INIT_CTL_NAME(PSDEV_LNET_NIS)
889 .proc_handler = &proc_lnet_nis,
892 INIT_CTL_NAME(PSDEV_LNET_PTL_ROTOR)
893 .procname = "portal_rotor",
895 .proc_handler = &proc_lnet_portal_rotor,
902 static cfs_sysctl_table_t top_table[] = {
904 INIT_CTL_NAME(CTL_LNET)
920 if (lnet_table_header == NULL)
921 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
929 if (lnet_table_header != NULL)
930 cfs_unregister_sysctl_table(lnet_table_header);
932 lnet_table_header = NULL;