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,
46 #define CTL_LNET CTL_UNNUMBERED
47 #define PSDEV_LNET_STATS CTL_UNNUMBERED
48 #define PSDEV_LNET_ROUTES CTL_UNNUMBERED
49 #define PSDEV_LNET_ROUTERS CTL_UNNUMBERED
50 #define PSDEV_LNET_PEERS CTL_UNNUMBERED
51 #define PSDEV_LNET_BUFFERS CTL_UNNUMBERED
52 #define PSDEV_LNET_NIS CTL_UNNUMBERED
56 * NB: we don't use the highest bit of *ppos because it's signed;
57 * next 9 bits is used to stash idx (assuming that
58 * LNET_PEER_HASHSIZE < 512)
60 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
61 #define LNET_VERSION_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
62 #define LNET_PHASH_IDX_BITS 9
63 #define LNET_PHASH_NUM_BITS (LNET_LOFFT_BITS - 1 -\
64 LNET_VERSION_BITS - LNET_PHASH_IDX_BITS)
65 #define LNET_PHASH_BITS (LNET_PHASH_IDX_BITS + LNET_PHASH_NUM_BITS)
67 #define LNET_VERSION_BITMASK ((1ULL << LNET_VERSION_BITS) - 1)
68 #define LNET_PHASH_IDX_BITMASK ((1ULL << LNET_PHASH_IDX_BITS) - 1)
69 #define LNET_PHASH_NUM_BITMASK ((1ULL << LNET_PHASH_NUM_BITS) - 1)
71 #define LNET_VERSION_MASK (LNET_VERSION_BITMASK << LNET_PHASH_BITS)
72 #define LNET_PHASH_IDX_MASK (LNET_PHASH_IDX_BITMASK << LNET_PHASH_NUM_BITS)
73 #define LNET_PHASH_NUM_MASK (LNET_PHASH_NUM_BITMASK)
75 #define LNET_VERSION_GET(pos) (int)(((pos) & LNET_VERSION_MASK) >> \
77 #define LNET_PHASH_IDX_GET(pos) (int)(((pos) & LNET_PHASH_IDX_MASK) >> \
79 #define LNET_PHASH_NUM_GET(pos) (int)((pos) & LNET_PHASH_NUM_MASK)
80 #define LNET_VERSION_VALID_MASK(ver) \
81 (unsigned int)((ver) & \
83 #define LNET_PHASH_POS_MAKE(ver, idx, num) \
84 (((((loff_t)(ver)) & LNET_VERSION_BITMASK) << \
86 ((((loff_t)(idx)) & LNET_PHASH_IDX_BITMASK) <<\
87 LNET_PHASH_NUM_BITS) | \
88 ((num) & LNET_PHASH_NUM_BITMASK))
90 static int __proc_lnet_stats(void *data, int write,
91 loff_t pos, void *buffer, int nob)
94 lnet_counters_t *ctrs;
97 const int tmpsiz = 256; /* 7 %u and 4 LPU64 */
100 lnet_counters_reset();
106 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
110 LIBCFS_ALLOC(tmpstr, tmpsiz);
111 if (tmpstr == NULL) {
112 LIBCFS_FREE(ctrs, sizeof(*ctrs));
116 lnet_counters_get(ctrs);
118 len = snprintf(tmpstr, tmpsiz,
119 "%u %u %u %u %u %u %u "LPU64" "LPU64" "
121 ctrs->msgs_alloc, ctrs->msgs_max,
123 ctrs->send_count, ctrs->recv_count,
124 ctrs->route_count, ctrs->drop_count,
125 ctrs->send_length, ctrs->recv_length,
126 ctrs->route_length, ctrs->drop_length);
128 if (pos >= min_t(int, len, strlen(tmpstr)))
131 rc = cfs_trace_copyout_string(buffer, nob,
134 LIBCFS_FREE(tmpstr, tmpsiz);
135 LIBCFS_FREE(ctrs, sizeof(*ctrs));
139 DECLARE_PROC_HANDLER(proc_lnet_stats);
141 int LL_PROC_PROTO(proc_lnet_routes)
146 const int tmpsiz = 256;
151 DECLARE_LL_PROC_PPOS_DECL;
153 num = LNET_PHASH_NUM_GET(*ppos);
154 ver = LNET_VERSION_GET(*ppos);
161 LIBCFS_ALLOC(tmpstr, tmpsiz);
165 s = tmpstr; /* points to current position in tmpstr[] */
168 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
169 the_lnet.ln_routing ? "enabled" : "disabled");
170 LASSERT (tmpstr + tmpsiz - s > 0);
172 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %7s %s\n",
173 "net", "hops", "state", "router");
174 LASSERT (tmpstr + tmpsiz - s > 0);
177 ver = (unsigned int)the_lnet.ln_remote_nets_version;
179 *ppos = LNET_PHASH_POS_MAKE(ver, 0, num);
183 lnet_route_t *route = NULL;
184 lnet_remotenet_t *rnet = NULL;
189 if (ver != LNET_VERSION_VALID_MASK(the_lnet.ln_remote_nets_version)) {
191 LIBCFS_FREE(tmpstr, tmpsiz);
195 n = the_lnet.ln_remote_nets.next;
197 while (n != &the_lnet.ln_remote_nets && route == NULL) {
198 rnet = cfs_list_entry(n, lnet_remotenet_t, lrn_list);
200 r = rnet->lrn_routes.next;
202 while (r != &rnet->lrn_routes) {
204 cfs_list_entry(r, lnet_route_t,
219 __u32 net = rnet->lrn_net;
220 unsigned int hops = route->lr_hops;
221 lnet_nid_t nid = route->lr_gateway->lp_nid;
222 int alive = route->lr_gateway->lp_alive;
224 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4u %7s %s\n",
225 libcfs_net2str(net), hops,
226 alive ? "up" : "down", libcfs_nid2str(nid));
227 LASSERT (tmpstr + tmpsiz - s > 0);
233 len = s - tmpstr; /* how many bytes was written */
235 if (len > *lenp) { /* linux-supplied buffer is too small */
237 } else if (len > 0) { /* wrote something */
238 if (cfs_copy_to_user(buffer, tmpstr, len))
242 *ppos = LNET_PHASH_POS_MAKE(ver, 0, num);
246 LIBCFS_FREE(tmpstr, tmpsiz);
254 int LL_PROC_PROTO(proc_lnet_routers)
259 const int tmpsiz = 256;
264 DECLARE_LL_PROC_PPOS_DECL;
266 num = LNET_PHASH_NUM_GET(*ppos);
267 ver = LNET_VERSION_GET(*ppos);
274 LIBCFS_ALLOC(tmpstr, tmpsiz);
278 s = tmpstr; /* points to current position in tmpstr[] */
281 s += snprintf(s, tmpstr + tmpsiz - s,
282 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
283 "ref", "rtr_ref", "alive_cnt", "state", "last_ping",
284 "ping_sent", "deadline", "down_ni", "router");
285 LASSERT (tmpstr + tmpsiz - s > 0);
288 ver = (unsigned int)the_lnet.ln_routers_version;
290 *ppos = LNET_PHASH_POS_MAKE(ver, 0, num);
293 lnet_peer_t *peer = NULL;
298 if (ver != LNET_VERSION_VALID_MASK(the_lnet.ln_routers_version)) {
300 LIBCFS_FREE(tmpstr, tmpsiz);
304 r = the_lnet.ln_routers.next;
306 while (r != &the_lnet.ln_routers) {
307 lnet_peer_t *lp = cfs_list_entry(r, lnet_peer_t,
320 lnet_nid_t nid = peer->lp_nid;
321 cfs_time_t now = cfs_time_current();
322 cfs_time_t deadline = peer->lp_ping_deadline;
323 int nrefs = peer->lp_refcount;
324 int nrtrrefs = peer->lp_rtr_refcount;
325 int alive_cnt = peer->lp_alive_count;
326 int alive = peer->lp_alive;
327 int pingsent = !peer->lp_ping_notsent;
328 int last_ping = cfs_duration_sec(cfs_time_sub(now,
329 peer->lp_ping_timestamp));
333 if (peer->lp_ping_version == LNET_PROTO_PING_VERSION) {
334 cfs_list_for_each_entry(rtr, &peer->lp_routes,
336 /* downis on any route should be the
337 * number of downis on the gateway */
338 if (rtr->lr_downis != 0) {
339 down_ni = rtr->lr_downis;
346 s += snprintf(s, tmpstr + tmpsiz - s,
347 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
348 nrefs, nrtrrefs, alive_cnt,
349 alive ? "up" : "down", last_ping,
350 pingsent, "NA", down_ni,
351 libcfs_nid2str(nid));
353 s += snprintf(s, tmpstr + tmpsiz - s,
354 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
355 nrefs, nrtrrefs, alive_cnt,
356 alive ? "up" : "down", last_ping,
358 cfs_duration_sec(cfs_time_sub(deadline, now)),
359 down_ni, libcfs_nid2str(nid));
360 LASSERT (tmpstr + tmpsiz - s > 0);
366 len = s - tmpstr; /* how many bytes was written */
368 if (len > *lenp) { /* linux-supplied buffer is too small */
370 } else if (len > 0) { /* wrote something */
371 if (cfs_copy_to_user(buffer, tmpstr, len))
375 *ppos = LNET_PHASH_POS_MAKE(ver, 0, num);
379 LIBCFS_FREE(tmpstr, tmpsiz);
387 int LL_PROC_PROTO(proc_lnet_peers)
389 struct lnet_peer_table *ptable = the_lnet.ln_peer_table;
393 const int tmpsiz = 256;
399 DECLARE_LL_PROC_PPOS_DECL;
401 idx = LNET_PHASH_IDX_GET(*ppos);
402 num = LNET_PHASH_NUM_GET(*ppos);
403 ver = LNET_VERSION_GET(*ppos);
405 CLASSERT ((1ULL << LNET_PHASH_BITS) > LNET_PEER_HASHSIZE);
412 LIBCFS_ALLOC(tmpstr, tmpsiz);
416 s = tmpstr; /* points to current position in tmpstr[] */
419 s += snprintf(s, tmpstr + tmpsiz - s,
420 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
421 "nid", "refs", "state", "last", "max",
422 "rtr", "min", "tx", "min", "queue");
423 LASSERT (tmpstr + tmpsiz - s > 0);
426 ver = (unsigned int)ptable->pt_version;
428 *ppos = LNET_PHASH_POS_MAKE(ver, idx, num);
432 cfs_list_t *p = NULL;
433 lnet_peer_t *peer = NULL;
438 if (ver != LNET_VERSION_VALID_MASK(ptable->pt_version)) {
440 LIBCFS_FREE(tmpstr, tmpsiz);
444 while (idx < LNET_PEER_HASHSIZE) {
446 p = ptable->pt_hash[idx].next;
448 while (p != &ptable->pt_hash[idx]) {
449 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
454 /* minor optimization: start from idx+1
455 * on next iteration if we've just
456 * drained lp_hashlist */
457 if (lp->lp_hashlist.next ==
458 &ptable->pt_hash[idx]) {
469 p = lp->lp_hashlist.next;
481 lnet_nid_t nid = peer->lp_nid;
482 int nrefs = peer->lp_refcount;
484 char *aliveness = "NA";
485 int maxcr = peer->lp_ni->ni_peertxcredits;
486 int txcr = peer->lp_txcredits;
487 int mintxcr = peer->lp_mintxcredits;
488 int rtrcr = peer->lp_rtrcredits;
489 int minrtrcr = peer->lp_minrtrcredits;
490 int txqnob = peer->lp_txqnob;
492 if (lnet_isrouter(peer) ||
493 lnet_peer_aliveness_enabled(peer))
494 aliveness = peer->lp_alive ? "up" : "down";
496 if (lnet_peer_aliveness_enabled(peer)) {
497 cfs_time_t now = cfs_time_current();
498 cfs_duration_t delta;
500 delta = cfs_time_sub(now, peer->lp_last_alive);
501 lastalive = cfs_duration_sec(delta);
503 /* No need to mess up peers contents with
504 * arbitrarily long integers - it suffices to
505 * know that lastalive is more than 10000s old
507 if (lastalive >= 10000)
511 s += snprintf(s, tmpstr + tmpsiz - s,
512 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
513 libcfs_nid2str(nid), nrefs, aliveness,
514 lastalive, maxcr, rtrcr, minrtrcr, txcr,
516 LASSERT (tmpstr + tmpsiz - s > 0);
522 len = s - tmpstr; /* how many bytes was written */
524 if (len > *lenp) { /* linux-supplied buffer is too small */
526 } else if (len > 0) { /* wrote something */
527 if (cfs_copy_to_user(buffer, tmpstr, len))
530 *ppos = LNET_PHASH_POS_MAKE(ver, idx, num);
533 LIBCFS_FREE(tmpstr, tmpsiz);
541 static int __proc_lnet_buffers(void *data, int write,
542 loff_t pos, void *buffer, int nob)
549 const int tmpsiz = 64 * (LNET_NRBPOOLS + 1); /* (4 %d) * 4 */
554 LIBCFS_ALLOC(tmpstr, tmpsiz);
558 s = tmpstr; /* points to current position in tmpstr[] */
560 s += snprintf(s, tmpstr + tmpsiz - s,
562 "pages", "count", "credits", "min");
563 LASSERT (tmpstr + tmpsiz - s > 0);
565 if (the_lnet.ln_rtrpools == NULL)
566 goto out; /* I'm not a router */
570 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
571 lnet_rtrbufpool_t *rbp = &the_lnet.ln_rtrpools[idx];
573 int npages = rbp->rbp_npages;
574 int nbuf = rbp->rbp_nbuffers;
575 int cr = rbp->rbp_credits;
576 int mincr = rbp->rbp_mincredits;
578 s += snprintf(s, tmpstr + tmpsiz - s,
580 npages, nbuf, cr, mincr);
581 LASSERT (tmpstr + tmpsiz - s > 0);
589 if (pos >= min_t(int, len, strlen(tmpstr)))
592 rc = cfs_trace_copyout_string(buffer, nob,
595 LIBCFS_FREE(tmpstr, tmpsiz);
599 DECLARE_PROC_HANDLER(proc_lnet_buffers);
601 int LL_PROC_PROTO(proc_lnet_nis)
606 const int tmpsiz = 256;
609 DECLARE_LL_PROC_PPOS_DECL;
616 LIBCFS_ALLOC(tmpstr, tmpsiz);
620 s = tmpstr; /* points to current position in tmpstr[] */
623 s += snprintf(s, tmpstr + tmpsiz - s,
624 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
625 "nid", "status", "alive", "refs", "peer",
626 "rtr", "max", "tx", "min");
627 LASSERT (tmpstr + tmpsiz - s > 0);
630 lnet_ni_t *ni = NULL;
631 int skip = *ppos - 1;
635 n = the_lnet.ln_nis.next;
637 while (n != &the_lnet.ln_nis) {
638 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
650 cfs_time_t now = cfs_time_current();
652 int maxtxcr = ni->ni_maxtxcredits;
653 int txcr = ni->ni_txcredits;
654 int mintxcr = ni->ni_mintxcredits;
655 int npeertxcr = ni->ni_peertxcredits;
656 int npeerrtrcr = ni->ni_peerrtrcredits;
657 lnet_nid_t nid = ni->ni_nid;
658 int nref = ni->ni_refcount;
661 if (the_lnet.ln_routing)
662 last_alive = cfs_duration_sec(cfs_time_sub(now,
664 if (ni->ni_lnd->lnd_type == LOLND) /* @lo forever alive */
667 LASSERT (ni->ni_status != NULL);
668 stat = (ni->ni_status->ns_status == LNET_NI_STATUS_UP) ?
671 s += snprintf(s, tmpstr + tmpsiz - s,
672 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
673 libcfs_nid2str(nid), stat, last_alive, nref,
674 npeertxcr, npeerrtrcr, maxtxcr,
676 LASSERT (tmpstr + tmpsiz - s > 0);
682 len = s - tmpstr; /* how many bytes was written */
684 if (len > *lenp) { /* linux-supplied buffer is too small */
686 } else if (len > 0) { /* wrote something */
687 if (cfs_copy_to_user(buffer, tmpstr, len))
693 LIBCFS_FREE(tmpstr, tmpsiz);
701 static cfs_sysctl_table_t lnet_table[] = {
703 * NB No .strategy entries have been provided since sysctl(8) prefers
704 * to go via /proc for portability.
707 INIT_CTL_NAME(PSDEV_LNET_STATS)
710 .proc_handler = &proc_lnet_stats,
713 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
714 .procname = "routes",
716 .proc_handler = &proc_lnet_routes,
719 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
720 .procname = "routers",
722 .proc_handler = &proc_lnet_routers,
725 INIT_CTL_NAME(PSDEV_LNET_PEERS)
728 .proc_handler = &proc_lnet_peers,
731 INIT_CTL_NAME(PSDEV_LNET_PEERS)
732 .procname = "buffers",
734 .proc_handler = &proc_lnet_buffers,
737 INIT_CTL_NAME(PSDEV_LNET_NIS)
740 .proc_handler = &proc_lnet_nis,
747 static cfs_sysctl_table_t top_table[] = {
749 INIT_CTL_NAME(CTL_LNET)
765 if (lnet_table_header == NULL)
766 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
774 if (lnet_table_header != NULL)
775 cfs_unregister_sysctl_table(lnet_table_header);
777 lnet_table_header = NULL;