2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2013, Intel Corporation.
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 struct ctl_table_header *lnet_table_header = NULL;
35 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
37 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
39 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
40 /* change version, 16 bits or 8 bits */
41 #define LNET_PROC_VER_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
43 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
45 * bits for peer hash offset
46 * NB: we don't use the highest bit of *ppos because it's signed
48 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
49 LNET_PROC_CPT_BITS - \
50 LNET_PROC_VER_BITS - \
51 LNET_PROC_HASH_BITS - 1)
52 /* bits for hash index + position */
53 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
54 /* bits for peer hash table + hash version */
55 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
57 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
58 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
59 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
60 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
62 #define LNET_PROC_CPT_GET(pos) \
63 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
65 #define LNET_PROC_VER_GET(pos) \
66 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
68 #define LNET_PROC_HASH_GET(pos) \
69 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
71 #define LNET_PROC_HOFF_GET(pos) \
72 (int)((pos) & LNET_PROC_HOFF_MASK)
74 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
75 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
76 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
77 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
78 ((off) & LNET_PROC_HOFF_MASK))
80 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
82 static int __proc_lnet_stats(void *data, int write,
83 loff_t pos, void *buffer, int nob)
86 lnet_counters_t *ctrs;
89 const int tmpsiz = 256; /* 7 %u and 4 LPU64 */
92 lnet_counters_reset();
98 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
102 LIBCFS_ALLOC(tmpstr, tmpsiz);
103 if (tmpstr == NULL) {
104 LIBCFS_FREE(ctrs, sizeof(*ctrs));
108 lnet_counters_get(ctrs);
110 len = snprintf(tmpstr, tmpsiz,
111 "%u %u %u %u %u %u %u "LPU64" "LPU64" "
113 ctrs->msgs_alloc, ctrs->msgs_max,
115 ctrs->send_count, ctrs->recv_count,
116 ctrs->route_count, ctrs->drop_count,
117 ctrs->send_length, ctrs->recv_length,
118 ctrs->route_length, ctrs->drop_length);
120 if (pos >= min_t(int, len, strlen(tmpstr)))
123 rc = cfs_trace_copyout_string(buffer, nob,
126 LIBCFS_FREE(tmpstr, tmpsiz);
127 LIBCFS_FREE(ctrs, sizeof(*ctrs));
132 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
133 size_t *lenp, loff_t *ppos)
135 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
140 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
141 size_t *lenp, loff_t *ppos)
143 const int tmpsiz = 256;
151 CLASSERT(sizeof(loff_t) >= 4);
153 off = LNET_PROC_HOFF_GET(*ppos);
154 ver = LNET_PROC_VER_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 %8s %7s %s\n",
173 "net", "hops", "priority", "state", "router");
174 LASSERT (tmpstr + tmpsiz - s > 0);
177 ver = (unsigned int)the_lnet.ln_remote_nets_version;
179 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
183 lnet_route_t *route = NULL;
184 lnet_remotenet_t *rnet = NULL;
186 struct list_head *rn_list;
191 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
193 LIBCFS_FREE(tmpstr, tmpsiz);
197 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
199 rn_list = &the_lnet.ln_remote_nets_hash[i];
203 while (n != rn_list && route == NULL) {
204 rnet = list_entry(n, lnet_remotenet_t,
207 r = rnet->lrn_routes.next;
209 while (r != &rnet->lrn_routes) {
211 list_entry(r, lnet_route_t,
227 __u32 net = rnet->lrn_net;
228 unsigned int hops = route->lr_hops;
229 unsigned int priority = route->lr_priority;
230 lnet_nid_t nid = route->lr_gateway->lp_nid;
231 int alive = lnet_is_route_alive(route);
233 s += snprintf(s, tmpstr + tmpsiz - s,
234 "%-8s %4u %8u %7s %s\n",
235 libcfs_net2str(net), hops,
237 alive ? "up" : "down",
238 libcfs_nid2str(nid));
239 LASSERT(tmpstr + tmpsiz - s > 0);
245 len = s - tmpstr; /* how many bytes was written */
247 if (len > *lenp) { /* linux-supplied buffer is too small */
249 } else if (len > 0) { /* wrote something */
250 if (copy_to_user(buffer, tmpstr, len))
254 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
258 LIBCFS_FREE(tmpstr, tmpsiz);
267 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
268 size_t *lenp, loff_t *ppos)
273 const int tmpsiz = 256;
278 off = LNET_PROC_HOFF_GET(*ppos);
279 ver = LNET_PROC_VER_GET(*ppos);
286 LIBCFS_ALLOC(tmpstr, tmpsiz);
290 s = tmpstr; /* points to current position in tmpstr[] */
293 s += snprintf(s, tmpstr + tmpsiz - s,
294 "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
295 "ref", "rtr_ref", "alive_cnt", "state",
296 "last_ping", "ping_sent", "deadline",
297 "down_ni", "router");
298 LASSERT(tmpstr + tmpsiz - s > 0);
301 ver = (unsigned int)the_lnet.ln_routers_version;
303 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
306 struct lnet_peer *peer = NULL;
311 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
314 LIBCFS_FREE(tmpstr, tmpsiz);
318 r = the_lnet.ln_routers.next;
320 while (r != &the_lnet.ln_routers) {
321 lnet_peer_t *lp = list_entry(r, lnet_peer_t,
334 lnet_nid_t nid = peer->lp_nid;
335 cfs_time_t now = cfs_time_current();
336 cfs_time_t deadline = peer->lp_ping_deadline;
337 int nrefs = peer->lp_refcount;
338 int nrtrrefs = peer->lp_rtr_refcount;
339 int alive_cnt = peer->lp_alive_count;
340 int alive = peer->lp_alive;
341 int pingsent = !peer->lp_ping_notsent;
342 int last_ping = cfs_duration_sec(cfs_time_sub(now,
343 peer->lp_ping_timestamp));
347 if ((peer->lp_ping_feats &
348 LNET_PING_FEAT_NI_STATUS) != 0) {
349 list_for_each_entry(rtr, &peer->lp_routes,
351 /* downis on any route should be the
352 * number of downis on the gateway */
353 if (rtr->lr_downis != 0) {
354 down_ni = rtr->lr_downis;
361 s += snprintf(s, tmpstr + tmpsiz - s,
362 "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
363 nrefs, nrtrrefs, alive_cnt,
364 alive ? "up" : "down", last_ping,
365 pingsent, "NA", down_ni,
366 libcfs_nid2str(nid));
368 s += snprintf(s, tmpstr + tmpsiz - s,
369 "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
370 nrefs, nrtrrefs, alive_cnt,
371 alive ? "up" : "down", last_ping,
373 cfs_duration_sec(cfs_time_sub(deadline, now)),
374 down_ni, libcfs_nid2str(nid));
375 LASSERT (tmpstr + tmpsiz - s > 0);
381 len = s - tmpstr; /* how many bytes was written */
383 if (len > *lenp) { /* linux-supplied buffer is too small */
385 } else if (len > 0) { /* wrote something */
386 if (copy_to_user(buffer, tmpstr, len))
390 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
394 LIBCFS_FREE(tmpstr, tmpsiz);
403 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
404 size_t *lenp, loff_t *ppos)
406 const int tmpsiz = 256;
407 struct lnet_peer_table *ptable;
410 int cpt = LNET_PROC_CPT_GET(*ppos);
411 int ver = LNET_PROC_VER_GET(*ppos);
412 int hash = LNET_PROC_HASH_GET(*ppos);
413 int hoff = LNET_PROC_HOFF_GET(*ppos);
417 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
423 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;
452 ptable = the_lnet.ln_peer_tables[cpt];
454 ver = LNET_PROC_VERSION(ptable->pt_version);
456 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
457 lnet_net_unlock(cpt);
458 LIBCFS_FREE(tmpstr, tmpsiz);
462 while (hash < LNET_PEER_HASH_SIZE) {
464 p = ptable->pt_hash[hash].next;
466 while (p != &ptable->pt_hash[hash]) {
467 lnet_peer_t *lp = list_entry(p, lnet_peer_t,
472 /* minor optimization: start from idx+1
473 * on next iteration if we've just
474 * drained lp_hashlist */
475 if (lp->lp_hashlist.next ==
476 &ptable->pt_hash[hash]) {
487 p = lp->lp_hashlist.next;
499 lnet_nid_t nid = peer->lp_nid;
500 int nrefs = peer->lp_refcount;
502 char *aliveness = "NA";
503 int maxcr = peer->lp_ni->ni_peertxcredits;
504 int txcr = peer->lp_txcredits;
505 int mintxcr = peer->lp_mintxcredits;
506 int rtrcr = peer->lp_rtrcredits;
507 int minrtrcr = peer->lp_minrtrcredits;
508 int txqnob = peer->lp_txqnob;
510 if (lnet_isrouter(peer) ||
511 lnet_peer_aliveness_enabled(peer))
512 aliveness = peer->lp_alive ? "up" : "down";
514 if (lnet_peer_aliveness_enabled(peer)) {
515 cfs_time_t now = cfs_time_current();
516 cfs_duration_t delta;
518 delta = cfs_time_sub(now, peer->lp_last_alive);
519 lastalive = cfs_duration_sec(delta);
521 /* No need to mess up peers contents with
522 * arbitrarily long integers - it suffices to
523 * know that lastalive is more than 10000s old
525 if (lastalive >= 10000)
529 lnet_net_unlock(cpt);
531 s += snprintf(s, tmpstr + tmpsiz - s,
532 "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
533 libcfs_nid2str(nid), nrefs, aliveness,
534 lastalive, maxcr, rtrcr, minrtrcr, txcr,
536 LASSERT (tmpstr + tmpsiz - s > 0);
538 } else { /* peer is NULL */
539 lnet_net_unlock(cpt);
542 if (hash == LNET_PEER_HASH_SIZE) {
546 if (peer == NULL && cpt < LNET_CPT_NUMBER)
551 len = s - tmpstr; /* how many bytes was written */
553 if (len > *lenp) { /* linux-supplied buffer is too small */
555 } else if (len > 0) { /* wrote something */
556 if (copy_to_user(buffer, tmpstr, len))
559 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
562 LIBCFS_FREE(tmpstr, tmpsiz);
570 static int __proc_lnet_buffers(void *data, int write,
571 loff_t pos, void *buffer, int nob)
583 /* (4 %d) * 4 * LNET_CPT_NUMBER */
584 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
585 LIBCFS_ALLOC(tmpstr, tmpsiz);
589 s = tmpstr; /* points to current position in tmpstr[] */
591 s += snprintf(s, tmpstr + tmpsiz - s,
593 "pages", "count", "credits", "min");
594 LASSERT (tmpstr + tmpsiz - s > 0);
596 if (the_lnet.ln_rtrpools == NULL)
597 goto out; /* I'm not a router */
599 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
600 lnet_rtrbufpool_t *rbp;
602 lnet_net_lock(LNET_LOCK_EX);
603 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
604 s += snprintf(s, tmpstr + tmpsiz - s,
607 rbp[idx].rbp_nbuffers,
608 rbp[idx].rbp_credits,
609 rbp[idx].rbp_mincredits);
610 LASSERT(tmpstr + tmpsiz - s > 0);
612 lnet_net_unlock(LNET_LOCK_EX);
618 if (pos >= min_t(int, len, strlen(tmpstr)))
621 rc = cfs_trace_copyout_string(buffer, nob,
624 LIBCFS_FREE(tmpstr, tmpsiz);
629 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
630 size_t *lenp, loff_t *ppos)
632 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
633 __proc_lnet_buffers);
637 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
638 size_t *lenp, loff_t *ppos)
640 int tmpsiz = 128 * LNET_CPT_NUMBER;
651 LIBCFS_ALLOC(tmpstr, tmpsiz);
655 s = tmpstr; /* points to current position in tmpstr[] */
658 s += snprintf(s, tmpstr + tmpsiz - s,
659 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
660 "nid", "status", "alive", "refs", "peer",
661 "rtr", "max", "tx", "min");
662 LASSERT (tmpstr + tmpsiz - s > 0);
665 lnet_ni_t *ni = NULL;
666 int skip = *ppos - 1;
670 n = the_lnet.ln_nis.next;
672 while (n != &the_lnet.ln_nis) {
673 lnet_ni_t *a_ni = list_entry(n, lnet_ni_t, ni_list);
685 struct lnet_tx_queue *tq;
687 long now = cfs_time_current_sec();
692 if (the_lnet.ln_routing)
693 last_alive = now - ni->ni_last_alive;
695 /* @lo forever alive */
696 if (ni->ni_lnd->lnd_type == LOLND)
700 LASSERT(ni->ni_status != NULL);
701 stat = (ni->ni_status->ns_status ==
702 LNET_NI_STATUS_UP) ? "up" : "down";
705 /* we actually output credits information for
706 * TX queue of each partition */
707 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
708 for (j = 0; ni->ni_cpts != NULL &&
709 j < ni->ni_ncpts; j++) {
710 if (i == ni->ni_cpts[j])
714 if (j == ni->ni_ncpts)
720 s += snprintf(s, tmpstr + tmpsiz - s,
721 "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
722 libcfs_nid2str(ni->ni_nid), stat,
723 last_alive, *ni->ni_refs[i],
724 ni->ni_peertxcredits,
725 ni->ni_peerrtrcredits,
727 tq->tq_credits, tq->tq_credits_min);
731 LASSERT(tmpstr + tmpsiz - s > 0);
737 len = s - tmpstr; /* how many bytes was written */
739 if (len > *lenp) { /* linux-supplied buffer is too small */
741 } else if (len > 0) { /* wrote something */
742 if (copy_to_user(buffer, tmpstr, len))
748 LIBCFS_FREE(tmpstr, tmpsiz);
756 struct lnet_portal_rotors {
762 static struct lnet_portal_rotors portal_rotors[] = {
764 .pr_value = LNET_PTL_ROTOR_OFF,
766 .pr_desc = "Turn off message rotor for wildcard portals"
769 .pr_value = LNET_PTL_ROTOR_ON,
771 .pr_desc = "round-robin dispatch all PUT messages for "
775 .pr_value = LNET_PTL_ROTOR_RR_RT,
777 .pr_desc = "round-robin dispatch routed PUT message for "
781 .pr_value = LNET_PTL_ROTOR_HASH_RT,
782 .pr_name = "HASH_RT",
783 .pr_desc = "dispatch routed PUT message by hashing source "
784 "NID for wildcard portals"
793 extern int portal_rotor;
795 static int __proc_lnet_portal_rotor(void *data, int write,
796 loff_t pos, void *buffer, int nob)
798 const int buf_len = 128;
804 LIBCFS_ALLOC(buf, buf_len);
811 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
812 if (portal_rotors[i].pr_value == portal_rotor)
816 LASSERT(portal_rotors[i].pr_value == portal_rotor);
819 rc = snprintf(buf, buf_len,
820 "{\n\tportals: all\n"
821 "\trotor: %s\n\tdescription: %s\n}",
822 portal_rotors[i].pr_name,
823 portal_rotors[i].pr_desc);
825 if (pos >= min_t(int, rc, buf_len)) {
828 rc = cfs_trace_copyout_string(buffer, nob,
834 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
838 tmp = cfs_trimwhite(buf);
842 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
843 if (cfs_strncasecmp(portal_rotors[i].pr_name, tmp,
844 strlen(portal_rotors[i].pr_name)) == 0) {
845 portal_rotor = portal_rotors[i].pr_value;
852 LIBCFS_FREE(buf, buf_len);
857 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
858 size_t *lenp, loff_t *ppos)
860 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
861 __proc_lnet_portal_rotor);
865 static struct ctl_table lnet_table[] = {
867 * NB No .strategy entries have been provided since sysctl(8) prefers
868 * to go via /proc for portability.
874 .proc_handler = &proc_lnet_stats,
878 .procname = "routes",
880 .proc_handler = &proc_lnet_routes,
884 .procname = "routers",
886 .proc_handler = &proc_lnet_routers,
892 .proc_handler = &proc_lnet_peers,
896 .procname = "buffers",
898 .proc_handler = &proc_lnet_buffers,
904 .proc_handler = &proc_lnet_nis,
908 .procname = "portal_rotor",
910 .proc_handler = &proc_lnet_portal_rotor,
915 static struct ctl_table top_table[] = {
931 if (lnet_table_header == NULL)
932 lnet_table_header = register_sysctl_table(top_table);
940 if (lnet_table_header != NULL)
941 unregister_sysctl_table(lnet_table_header);
943 lnet_table_header = NULL;