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_feats &
352 LNET_PING_FEAT_NI_STATUS) != 0) {
353 cfs_list_for_each_entry(rtr, &peer->lp_routes,
355 /* downis on any route should be the
356 * number of downis on the gateway */
357 if (rtr->lr_downis != 0) {
358 down_ni = rtr->lr_downis;
365 s += snprintf(s, tmpstr + tmpsiz - s,
366 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
367 nrefs, nrtrrefs, alive_cnt,
368 alive ? "up" : "down", last_ping,
369 pingsent, "NA", down_ni,
370 libcfs_nid2str(nid));
372 s += snprintf(s, tmpstr + tmpsiz - s,
373 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
374 nrefs, nrtrrefs, alive_cnt,
375 alive ? "up" : "down", last_ping,
377 cfs_duration_sec(cfs_time_sub(deadline, now)),
378 down_ni, libcfs_nid2str(nid));
379 LASSERT (tmpstr + tmpsiz - s > 0);
385 len = s - tmpstr; /* how many bytes was written */
387 if (len > *lenp) { /* linux-supplied buffer is too small */
389 } else if (len > 0) { /* wrote something */
390 if (cfs_copy_to_user(buffer, tmpstr, len))
394 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
398 LIBCFS_FREE(tmpstr, tmpsiz);
406 int LL_PROC_PROTO(proc_lnet_peers)
408 const int tmpsiz = 256;
409 struct lnet_peer_table *ptable;
412 int cpt = LNET_PROC_CPT_GET(*ppos);
413 int ver = LNET_PROC_VER_GET(*ppos);
414 int hoff = LNET_PROC_HOFF_GET(*ppos);
415 int hash = LNET_PROC_HASH_GET(*ppos);
419 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
425 if (cpt >= LNET_CPT_NUMBER)
428 LIBCFS_ALLOC(tmpstr, tmpsiz);
432 s = tmpstr; /* points to current position in tmpstr[] */
435 s += snprintf(s, tmpstr + tmpsiz - s,
436 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
437 "nid", "refs", "state", "last", "max",
438 "rtr", "min", "tx", "min", "queue");
439 LASSERT (tmpstr + tmpsiz - s > 0);
443 struct lnet_peer *peer = NULL;
444 cfs_list_t *p = NULL;
449 ptable = the_lnet.ln_peer_tables[cpt];
451 ver = LNET_PROC_VERSION(ptable->pt_version);
453 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
454 lnet_net_unlock(cpt);
455 LIBCFS_FREE(tmpstr, tmpsiz);
459 while (hash < LNET_PEER_HASH_SIZE) {
461 p = ptable->pt_hash[hash].next;
463 while (p != &ptable->pt_hash[hash]) {
464 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
469 /* minor optimization: start from idx+1
470 * on next iteration if we've just
471 * drained lp_hashlist */
472 if (lp->lp_hashlist.next ==
473 &ptable->pt_hash[hash]) {
484 p = lp->lp_hashlist.next;
496 lnet_nid_t nid = peer->lp_nid;
497 int nrefs = peer->lp_refcount;
499 char *aliveness = "NA";
500 int maxcr = peer->lp_ni->ni_peertxcredits;
501 int txcr = peer->lp_txcredits;
502 int mintxcr = peer->lp_mintxcredits;
503 int rtrcr = peer->lp_rtrcredits;
504 int minrtrcr = peer->lp_minrtrcredits;
505 int txqnob = peer->lp_txqnob;
507 if (lnet_isrouter(peer) ||
508 lnet_peer_aliveness_enabled(peer))
509 aliveness = peer->lp_alive ? "up" : "down";
511 if (lnet_peer_aliveness_enabled(peer)) {
512 cfs_time_t now = cfs_time_current();
513 cfs_duration_t delta;
515 delta = cfs_time_sub(now, peer->lp_last_alive);
516 lastalive = cfs_duration_sec(delta);
518 /* No need to mess up peers contents with
519 * arbitrarily long integers - it suffices to
520 * know that lastalive is more than 10000s old
522 if (lastalive >= 10000)
526 lnet_net_unlock(cpt);
528 s += snprintf(s, tmpstr + tmpsiz - s,
529 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
530 libcfs_nid2str(nid), nrefs, aliveness,
531 lastalive, maxcr, rtrcr, minrtrcr, txcr,
533 LASSERT (tmpstr + tmpsiz - s > 0);
535 } else { /* peer is NULL */
536 lnet_net_unlock(cpt);
538 if (hash == LNET_PEER_HASH_SIZE &&
539 cpt < LNET_CPT_NUMBER - 1) {
548 len = s - tmpstr; /* how many bytes was written */
550 if (len > *lenp) { /* linux-supplied buffer is too small */
552 } else if (len > 0) { /* wrote something */
553 if (cfs_copy_to_user(buffer, tmpstr, len))
556 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
559 LIBCFS_FREE(tmpstr, tmpsiz);
567 static int __proc_lnet_buffers(void *data, int write,
568 loff_t pos, void *buffer, int nob)
580 /* (4 %d) * 4 * LNET_CPT_NUMBER */
581 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
582 LIBCFS_ALLOC(tmpstr, tmpsiz);
586 s = tmpstr; /* points to current position in tmpstr[] */
588 s += snprintf(s, tmpstr + tmpsiz - s,
590 "pages", "count", "credits", "min");
591 LASSERT (tmpstr + tmpsiz - s > 0);
593 if (the_lnet.ln_rtrpools == NULL)
594 goto out; /* I'm not a router */
596 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
597 lnet_rtrbufpool_t *rbp;
599 lnet_net_lock(LNET_LOCK_EX);
600 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
601 s += snprintf(s, tmpstr + tmpsiz - s,
604 rbp[idx].rbp_nbuffers,
605 rbp[idx].rbp_credits,
606 rbp[idx].rbp_mincredits);
607 LASSERT(tmpstr + tmpsiz - s > 0);
609 lnet_net_unlock(LNET_LOCK_EX);
615 if (pos >= min_t(int, len, strlen(tmpstr)))
618 rc = cfs_trace_copyout_string(buffer, nob,
621 LIBCFS_FREE(tmpstr, tmpsiz);
625 DECLARE_PROC_HANDLER(proc_lnet_buffers);
627 int LL_PROC_PROTO(proc_lnet_nis)
629 int tmpsiz = 128 * LNET_CPT_NUMBER;
635 DECLARE_LL_PROC_PPOS_DECL;
642 LIBCFS_ALLOC(tmpstr, tmpsiz);
646 s = tmpstr; /* points to current position in tmpstr[] */
649 s += snprintf(s, tmpstr + tmpsiz - s,
650 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
651 "nid", "status", "alive", "refs", "peer",
652 "rtr", "max", "tx", "min");
653 LASSERT (tmpstr + tmpsiz - s > 0);
656 lnet_ni_t *ni = NULL;
657 int skip = *ppos - 1;
661 n = the_lnet.ln_nis.next;
663 while (n != &the_lnet.ln_nis) {
664 lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
676 struct lnet_tx_queue *tq;
678 long now = cfs_time_current_sec();
683 if (the_lnet.ln_routing)
684 last_alive = now - ni->ni_last_alive;
686 /* @lo forever alive */
687 if (ni->ni_lnd->lnd_type == LOLND)
691 LASSERT(ni->ni_status != NULL);
692 stat = (ni->ni_status->ns_status ==
693 LNET_NI_STATUS_UP) ? "up" : "down";
696 /* we actually output credits information for
697 * TX queue of each partition */
698 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
699 for (j = 0; ni->ni_cpts != NULL &&
700 j < ni->ni_ncpts; j++) {
701 if (i == ni->ni_cpts[j])
705 if (j == ni->ni_ncpts)
711 s += snprintf(s, tmpstr + tmpsiz - s,
712 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
713 libcfs_nid2str(ni->ni_nid), stat,
714 last_alive, *ni->ni_refs[i],
715 ni->ni_peertxcredits,
716 ni->ni_peerrtrcredits,
718 tq->tq_credits, tq->tq_credits_min);
722 LASSERT(tmpstr + tmpsiz - s > 0);
728 len = s - tmpstr; /* how many bytes was written */
730 if (len > *lenp) { /* linux-supplied buffer is too small */
732 } else if (len > 0) { /* wrote something */
733 if (cfs_copy_to_user(buffer, tmpstr, len))
739 LIBCFS_FREE(tmpstr, tmpsiz);
747 static cfs_sysctl_table_t lnet_table[] = {
749 * NB No .strategy entries have been provided since sysctl(8) prefers
750 * to go via /proc for portability.
753 INIT_CTL_NAME(PSDEV_LNET_STATS)
756 .proc_handler = &proc_lnet_stats,
759 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
760 .procname = "routes",
762 .proc_handler = &proc_lnet_routes,
765 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
766 .procname = "routers",
768 .proc_handler = &proc_lnet_routers,
771 INIT_CTL_NAME(PSDEV_LNET_PEERS)
774 .proc_handler = &proc_lnet_peers,
777 INIT_CTL_NAME(PSDEV_LNET_PEERS)
778 .procname = "buffers",
780 .proc_handler = &proc_lnet_buffers,
783 INIT_CTL_NAME(PSDEV_LNET_NIS)
786 .proc_handler = &proc_lnet_nis,
793 static cfs_sysctl_table_t top_table[] = {
795 INIT_CTL_NAME(CTL_LNET)
811 if (lnet_table_header == NULL)
812 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
820 if (lnet_table_header != NULL)
821 cfs_unregister_sysctl_table(lnet_table_header);
823 lnet_table_header = NULL;