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
55 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
57 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
59 #define LNET_PROC_CPT_BITS LNET_CPT_BITS
60 /* change version, 16 bits or 8 bits */
61 #define LNET_PROC_VER_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
63 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
65 * bits for peer hash offset
66 * NB: we don't use the highest bit of *ppos because it's signed
68 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
69 LNET_PROC_CPT_BITS - \
70 LNET_PROC_VER_BITS - \
71 LNET_PROC_HASH_BITS - 1)
72 /* bits for hash index + position */
73 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
74 /* bits for peer hash table + hash version */
75 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
77 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
78 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
79 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
80 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
82 #define LNET_PROC_CPT_GET(pos) \
83 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
85 #define LNET_PROC_VER_GET(pos) \
86 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
88 #define LNET_PROC_HASH_GET(pos) \
89 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
91 #define LNET_PROC_HOFF_GET(pos) \
92 (int)((pos) & LNET_PROC_HOFF_MASK)
94 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
95 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
96 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
97 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
98 ((off) & LNET_PROC_HOFF_MASK))
100 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
102 static int __proc_lnet_stats(void *data, int write,
103 loff_t pos, void *buffer, int nob)
106 lnet_counters_t *ctrs;
109 const int tmpsiz = 256; /* 7 %u and 4 LPU64 */
112 lnet_counters_reset();
118 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
122 LIBCFS_ALLOC(tmpstr, tmpsiz);
123 if (tmpstr == NULL) {
124 LIBCFS_FREE(ctrs, sizeof(*ctrs));
128 lnet_counters_get(ctrs);
130 len = snprintf(tmpstr, tmpsiz,
131 "%u %u %u %u %u %u %u "LPU64" "LPU64" "
133 ctrs->msgs_alloc, ctrs->msgs_max,
135 ctrs->send_count, ctrs->recv_count,
136 ctrs->route_count, ctrs->drop_count,
137 ctrs->send_length, ctrs->recv_length,
138 ctrs->route_length, ctrs->drop_length);
140 if (pos >= min_t(int, len, strlen(tmpstr)))
143 rc = cfs_trace_copyout_string(buffer, nob,
146 LIBCFS_FREE(tmpstr, tmpsiz);
147 LIBCFS_FREE(ctrs, sizeof(*ctrs));
151 DECLARE_PROC_HANDLER(proc_lnet_stats);
153 int LL_PROC_PROTO(proc_lnet_routes)
155 const int tmpsiz = 256;
163 DECLARE_LL_PROC_PPOS_DECL;
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 %7s %s\n",
187 "net", "hops", "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;
203 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
205 LIBCFS_FREE(tmpstr, tmpsiz);
209 n = the_lnet.ln_remote_nets.next;
211 while (n != &the_lnet.ln_remote_nets && route == NULL) {
212 rnet = cfs_list_entry(n, lnet_remotenet_t, lrn_list);
214 r = rnet->lrn_routes.next;
216 while (r != &rnet->lrn_routes) {
218 cfs_list_entry(r, lnet_route_t,
233 __u32 net = rnet->lrn_net;
234 unsigned int hops = route->lr_hops;
235 lnet_nid_t nid = route->lr_gateway->lp_nid;
236 int alive = route->lr_gateway->lp_alive;
238 s += snprintf(s, tmpstr + tmpsiz - s,
240 libcfs_net2str(net), hops,
241 alive ? "up" : "down",
242 libcfs_nid2str(nid));
243 LASSERT(tmpstr + tmpsiz - s > 0);
249 len = s - tmpstr; /* how many bytes was written */
251 if (len > *lenp) { /* linux-supplied buffer is too small */
253 } else if (len > 0) { /* wrote something */
254 if (cfs_copy_to_user(buffer, tmpstr, len))
258 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
262 LIBCFS_FREE(tmpstr, tmpsiz);
270 int LL_PROC_PROTO(proc_lnet_routers)
275 const int tmpsiz = 256;
280 DECLARE_LL_PROC_PPOS_DECL;
282 off = LNET_PROC_HOFF_GET(*ppos);
283 ver = LNET_PROC_VER_GET(*ppos);
290 LIBCFS_ALLOC(tmpstr, tmpsiz);
294 s = tmpstr; /* points to current position in tmpstr[] */
297 s += snprintf(s, tmpstr + tmpsiz - s,
298 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
299 "ref", "rtr_ref", "alive_cnt", "state",
300 "last_ping", "ping_sent", "deadline",
301 "down_ni", "router");
302 LASSERT(tmpstr + tmpsiz - s > 0);
305 ver = (unsigned int)the_lnet.ln_routers_version;
307 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
310 struct lnet_peer *peer = NULL;
315 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
318 LIBCFS_FREE(tmpstr, tmpsiz);
322 r = the_lnet.ln_routers.next;
324 while (r != &the_lnet.ln_routers) {
325 lnet_peer_t *lp = cfs_list_entry(r, lnet_peer_t,
338 lnet_nid_t nid = peer->lp_nid;
339 cfs_time_t now = cfs_time_current();
340 cfs_time_t deadline = peer->lp_ping_deadline;
341 int nrefs = peer->lp_refcount;
342 int nrtrrefs = peer->lp_rtr_refcount;
343 int alive_cnt = peer->lp_alive_count;
344 int alive = peer->lp_alive;
345 int pingsent = !peer->lp_ping_notsent;
346 int last_ping = cfs_duration_sec(cfs_time_sub(now,
347 peer->lp_ping_timestamp));
351 if (peer->lp_ping_version == LNET_PROTO_PING_VERSION) {
352 cfs_list_for_each_entry(rtr, &peer->lp_routes,
354 /* downis on any route should be the
355 * number of downis on the gateway */
356 if (rtr->lr_downis != 0) {
357 down_ni = rtr->lr_downis;
364 s += snprintf(s, tmpstr + tmpsiz - s,
365 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
366 nrefs, nrtrrefs, alive_cnt,
367 alive ? "up" : "down", last_ping,
368 pingsent, "NA", down_ni,
369 libcfs_nid2str(nid));
371 s += snprintf(s, tmpstr + tmpsiz - s,
372 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
373 nrefs, nrtrrefs, alive_cnt,
374 alive ? "up" : "down", last_ping,
376 cfs_duration_sec(cfs_time_sub(deadline, now)),
377 down_ni, libcfs_nid2str(nid));
378 LASSERT (tmpstr + tmpsiz - s > 0);
384 len = s - tmpstr; /* how many bytes was written */
386 if (len > *lenp) { /* linux-supplied buffer is too small */
388 } else if (len > 0) { /* wrote something */
389 if (cfs_copy_to_user(buffer, tmpstr, len))
393 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
397 LIBCFS_FREE(tmpstr, tmpsiz);
405 int LL_PROC_PROTO(proc_lnet_peers)
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 hoff = LNET_PROC_HOFF_GET(*ppos);
414 int hash = LNET_PROC_HASH_GET(*ppos);
418 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
424 if (cpt >= LNET_CPT_NUMBER)
427 LIBCFS_ALLOC(tmpstr, tmpsiz);
431 s = tmpstr; /* points to current position in tmpstr[] */
434 s += snprintf(s, tmpstr + tmpsiz - s,
435 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
436 "nid", "refs", "state", "last", "max",
437 "rtr", "min", "tx", "min", "queue");
438 LASSERT (tmpstr + tmpsiz - s > 0);
442 struct lnet_peer *peer = NULL;
443 cfs_list_t *p = NULL;
448 ptable = the_lnet.ln_peer_tables[cpt];
450 ver = LNET_PROC_VERSION(ptable->pt_version);
452 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
453 lnet_net_unlock(cpt);
454 LIBCFS_FREE(tmpstr, tmpsiz);
458 while (hash < LNET_PEER_HASH_SIZE) {
460 p = ptable->pt_hash[hash].next;
462 while (p != &ptable->pt_hash[hash]) {
463 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
468 /* minor optimization: start from idx+1
469 * on next iteration if we've just
470 * drained lp_hashlist */
471 if (lp->lp_hashlist.next ==
472 &ptable->pt_hash[hash]) {
483 p = lp->lp_hashlist.next;
495 lnet_nid_t nid = peer->lp_nid;
496 int nrefs = peer->lp_refcount;
498 char *aliveness = "NA";
499 int maxcr = peer->lp_ni->ni_peertxcredits;
500 int txcr = peer->lp_txcredits;
501 int mintxcr = peer->lp_mintxcredits;
502 int rtrcr = peer->lp_rtrcredits;
503 int minrtrcr = peer->lp_minrtrcredits;
504 int txqnob = peer->lp_txqnob;
506 if (lnet_isrouter(peer) ||
507 lnet_peer_aliveness_enabled(peer))
508 aliveness = peer->lp_alive ? "up" : "down";
510 if (lnet_peer_aliveness_enabled(peer)) {
511 cfs_time_t now = cfs_time_current();
512 cfs_duration_t delta;
514 delta = cfs_time_sub(now, peer->lp_last_alive);
515 lastalive = cfs_duration_sec(delta);
517 /* No need to mess up peers contents with
518 * arbitrarily long integers - it suffices to
519 * know that lastalive is more than 10000s old
521 if (lastalive >= 10000)
525 lnet_net_unlock(cpt);
527 s += snprintf(s, tmpstr + tmpsiz - s,
528 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
529 libcfs_nid2str(nid), nrefs, aliveness,
530 lastalive, maxcr, rtrcr, minrtrcr, txcr,
532 LASSERT (tmpstr + tmpsiz - s > 0);
534 } else { /* peer is NULL */
535 lnet_net_unlock(cpt);
537 if (hash == LNET_PEER_HASH_SIZE &&
538 cpt < LNET_CPT_NUMBER - 1) {
547 len = s - tmpstr; /* how many bytes was written */
549 if (len > *lenp) { /* linux-supplied buffer is too small */
551 } else if (len > 0) { /* wrote something */
552 if (cfs_copy_to_user(buffer, tmpstr, len))
555 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
558 LIBCFS_FREE(tmpstr, tmpsiz);
566 static int __proc_lnet_buffers(void *data, int write,
567 loff_t pos, void *buffer, int nob)
579 /* (4 %d) * 4 * LNET_CPT_NUMBER */
580 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
581 LIBCFS_ALLOC(tmpstr, tmpsiz);
585 s = tmpstr; /* points to current position in tmpstr[] */
587 s += snprintf(s, tmpstr + tmpsiz - s,
589 "pages", "count", "credits", "min");
590 LASSERT (tmpstr + tmpsiz - s > 0);
592 if (the_lnet.ln_rtrpools == NULL)
593 goto out; /* I'm not a router */
595 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
596 lnet_rtrbufpool_t *rbp;
598 lnet_net_lock(LNET_LOCK_EX);
599 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
600 s += snprintf(s, tmpstr + tmpsiz - s,
603 rbp[idx].rbp_nbuffers,
604 rbp[idx].rbp_credits,
605 rbp[idx].rbp_mincredits);
606 LASSERT(tmpstr + tmpsiz - s > 0);
608 lnet_net_unlock(LNET_LOCK_EX);
614 if (pos >= min_t(int, len, strlen(tmpstr)))
617 rc = cfs_trace_copyout_string(buffer, nob,
620 LIBCFS_FREE(tmpstr, tmpsiz);
624 DECLARE_PROC_HANDLER(proc_lnet_buffers);
626 int LL_PROC_PROTO(proc_lnet_nis)
628 int tmpsiz = 128 * LNET_CPT_NUMBER;
634 DECLARE_LL_PROC_PPOS_DECL;
641 LIBCFS_ALLOC(tmpstr, tmpsiz);
645 s = tmpstr; /* points to current position in tmpstr[] */
648 s += snprintf(s, tmpstr + tmpsiz - s,
649 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
650 "nid", "status", "alive", "refs", "peer",
651 "rtr", "max", "tx", "min");
652 LASSERT (tmpstr + tmpsiz - s > 0);
655 lnet_ni_t *ni = NULL;
656 int skip = *ppos - 1;
660 n = the_lnet.ln_nis.next;
662 while (n != &the_lnet.ln_nis) {
663 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
676 struct lnet_tx_queue *tq;
677 long now = cfs_time_current_sec();
681 if (the_lnet.ln_routing)
682 last_alive = now - ni->ni_last_alive;
684 /* @lo forever alive */
685 if (ni->ni_lnd->lnd_type == LOLND)
689 LASSERT(ni->ni_status != NULL);
690 stat = (ni->ni_status->ns_status ==
691 LNET_NI_STATUS_UP) ? "up" : "down";
694 /* we actually output credits information for
695 * TX queue of each partition */
696 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
700 s += snprintf(s, tmpstr + tmpsiz - s,
701 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
702 libcfs_nid2str(ni->ni_nid), stat,
703 last_alive, *ni->ni_refs[i],
704 ni->ni_peertxcredits,
705 ni->ni_peerrtrcredits,
707 tq->tq_credits, tq->tq_credits_min);
711 LASSERT(tmpstr + tmpsiz - s > 0);
717 len = s - tmpstr; /* how many bytes was written */
719 if (len > *lenp) { /* linux-supplied buffer is too small */
721 } else if (len > 0) { /* wrote something */
722 if (cfs_copy_to_user(buffer, tmpstr, len))
728 LIBCFS_FREE(tmpstr, tmpsiz);
736 static cfs_sysctl_table_t lnet_table[] = {
738 * NB No .strategy entries have been provided since sysctl(8) prefers
739 * to go via /proc for portability.
742 INIT_CTL_NAME(PSDEV_LNET_STATS)
745 .proc_handler = &proc_lnet_stats,
748 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
749 .procname = "routes",
751 .proc_handler = &proc_lnet_routes,
754 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
755 .procname = "routers",
757 .proc_handler = &proc_lnet_routers,
760 INIT_CTL_NAME(PSDEV_LNET_PEERS)
763 .proc_handler = &proc_lnet_peers,
766 INIT_CTL_NAME(PSDEV_LNET_PEERS)
767 .procname = "buffers",
769 .proc_handler = &proc_lnet_buffers,
772 INIT_CTL_NAME(PSDEV_LNET_NIS)
775 .proc_handler = &proc_lnet_nis,
782 static cfs_sysctl_table_t top_table[] = {
784 INIT_CTL_NAME(CTL_LNET)
800 if (lnet_table_header == NULL)
801 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
809 if (lnet_table_header != NULL)
810 cfs_unregister_sysctl_table(lnet_table_header);
812 lnet_table_header = NULL;