2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
25 #include <linux/uaccess.h>
27 #include <libcfs/libcfs.h>
28 #include <lnet/lib-lnet.h>
30 /* This is really lnet_proc.c. You might need to update sanity test 215
31 * if any file format is changed. */
33 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
35 * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
37 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
38 /* change version, 16 bits or 8 bits */
39 #define LNET_PROC_VER_BITS MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
41 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
43 * bits for peer hash offset
44 * NB: we don't use the highest bit of *ppos because it's signed
46 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
47 LNET_PROC_CPT_BITS - \
48 LNET_PROC_VER_BITS - \
49 LNET_PROC_HASH_BITS - 1)
50 /* bits for hash index + position */
51 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
52 /* bits for peer hash table + hash version */
53 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
55 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
56 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
57 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
58 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
60 #define LNET_PROC_CPT_GET(pos) \
61 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
63 #define LNET_PROC_VER_GET(pos) \
64 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
66 #define LNET_PROC_HASH_GET(pos) \
67 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
69 #define LNET_PROC_HOFF_GET(pos) \
70 (int)((pos) & LNET_PROC_HOFF_MASK)
72 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
73 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
74 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
75 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
76 ((off) & LNET_PROC_HOFF_MASK))
78 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
80 static int __proc_lnet_stats(void *data, int write,
81 loff_t pos, void __user *buffer, int nob)
84 struct lnet_counters *ctrs;
85 struct lnet_counters_common common;
88 const int tmpsiz = 256; /* 7 %u and 4 __u64 */
91 lnet_counters_reset();
97 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
101 LIBCFS_ALLOC(tmpstr, tmpsiz);
102 if (tmpstr == NULL) {
103 LIBCFS_FREE(ctrs, sizeof(*ctrs));
107 lnet_counters_get(ctrs);
108 common = ctrs->lct_common;
110 len = snprintf(tmpstr, tmpsiz,
111 "%u %u %u %u %u %u %u %llu %llu "
113 common.lcc_msgs_alloc, common.lcc_msgs_max,
115 common.lcc_send_count, common.lcc_recv_count,
116 common.lcc_route_count, common.lcc_drop_count,
117 common.lcc_send_length, common.lcc_recv_length,
118 common.lcc_route_length, common.lcc_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 struct lnet_route *route = NULL;
184 struct lnet_remotenet *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, struct lnet_remotenet,
207 r = rnet->lrn_routes.next;
209 while (r != &rnet->lrn_routes) {
210 struct lnet_route *re =
211 list_entry(r, struct lnet_route,
227 __u32 net = rnet->lrn_net;
228 __u32 hops = route->lr_hops;
229 unsigned int priority = route->lr_priority;
230 lnet_nid_t nid = route->lr_gateway->lp_primary_nid;
231 int alive = lnet_is_route_alive(route);
233 s += snprintf(s, tmpstr + tmpsiz - s,
234 "%-8s %4d %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 struct lnet_peer *lp =
322 list_entry(r, struct lnet_peer,
335 lnet_nid_t nid = peer->lp_primary_nid;
336 time64_t now = ktime_get_seconds();
337 /* TODO: readjust what's being printed */
338 time64_t deadline = 0;
339 int nrefs = atomic_read(&peer->lp_refcount);
340 int nrtrrefs = peer->lp_rtr_refcount;
343 int pingsent = ((peer->lp_state & LNET_PEER_PING_SENT)
345 time64_t last_ping = now - peer->lp_rtrcheck_timestamp;
347 struct lnet_route *rtr;
350 list_for_each_entry(rtr, &peer->lp_routes,
352 /* downis on any route should be the
353 * number of downis on the gateway */
354 if (rtr->lr_downis != 0) {
355 down_ni = rtr->lr_downis;
362 s += snprintf(s, tmpstr + tmpsiz - s,
363 "%-4d %7d %9d %6s %12llu %9d %8s %7d %s\n",
364 nrefs, nrtrrefs, alive_cnt,
365 alive ? "up" : "down", last_ping,
366 pingsent, "NA", down_ni,
367 libcfs_nid2str(nid));
369 s += snprintf(s, tmpstr + tmpsiz - s,
370 "%-4d %7d %9d %6s %12llu %9d %8llu %7d %s\n",
371 nrefs, nrtrrefs, alive_cnt,
372 alive ? "up" : "down", last_ping,
375 down_ni, libcfs_nid2str(nid));
376 LASSERT(tmpstr + tmpsiz - s > 0);
382 len = s - tmpstr; /* how many bytes was written */
384 if (len > *lenp) { /* linux-supplied buffer is too small */
386 } else if (len > 0) { /* wrote something */
387 if (copy_to_user(buffer, tmpstr, len))
391 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
395 LIBCFS_FREE(tmpstr, tmpsiz);
403 /* TODO: there should be no direct access to ptable. We should add a set
404 * of APIs that give access to the ptable and its members */
406 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
407 size_t *lenp, loff_t *ppos)
409 const int tmpsiz = 256;
410 struct lnet_peer_table *ptable;
413 int cpt = LNET_PROC_CPT_GET(*ppos);
414 int ver = LNET_PROC_VER_GET(*ppos);
415 int hash = LNET_PROC_HASH_GET(*ppos);
416 int hoff = LNET_PROC_HOFF_GET(*ppos);
422 struct lnet_peer_ni *peer;
424 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
426 for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
427 list_for_each_entry(peer,
428 &ptable->pt_hash[hash],
430 peer->lpni_mintxcredits =
431 peer->lpni_txcredits;
432 peer->lpni_minrtrcredits =
433 peer->lpni_rtrcredits;
445 CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
447 if (cpt >= LNET_CPT_NUMBER) {
452 LIBCFS_ALLOC(tmpstr, tmpsiz);
456 s = tmpstr; /* points to current position in tmpstr[] */
459 s += snprintf(s, tmpstr + tmpsiz - s,
460 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
461 "nid", "refs", "state", "last", "max",
462 "rtr", "min", "tx", "min", "queue");
463 LASSERT(tmpstr + tmpsiz - s > 0);
467 struct lnet_peer_ni *peer;
477 ptable = the_lnet.ln_peer_tables[cpt];
479 ver = LNET_PROC_VERSION(ptable->pt_version);
481 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
482 lnet_net_unlock(cpt);
483 LIBCFS_FREE(tmpstr, tmpsiz);
487 while (hash < LNET_PEER_HASH_SIZE) {
489 p = ptable->pt_hash[hash].next;
491 while (p != &ptable->pt_hash[hash]) {
492 struct lnet_peer_ni *lp =
493 list_entry(p, struct lnet_peer_ni,
498 /* minor optimization: start from idx+1
499 * on next iteration if we've just
500 * drained lpni_hashlist */
501 if (lp->lpni_hashlist.next ==
502 &ptable->pt_hash[hash]) {
513 p = lp->lpni_hashlist.next;
525 lnet_nid_t nid = peer->lpni_nid;
526 int nrefs = atomic_read(&peer->lpni_refcount);
527 time64_t lastalive = -1;
528 char *aliveness = "NA";
529 int maxcr = (peer->lpni_net) ?
530 peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
531 int txcr = peer->lpni_txcredits;
532 int mintxcr = peer->lpni_mintxcredits;
533 int rtrcr = peer->lpni_rtrcredits;
534 int minrtrcr = peer->lpni_minrtrcredits;
535 int txqnob = peer->lpni_txqnob;
537 if (lnet_isrouter(peer) ||
538 lnet_peer_aliveness_enabled(peer))
539 aliveness = lnet_is_peer_ni_alive(peer) ?
542 if (lnet_peer_aliveness_enabled(peer)) {
543 time64_t now = ktime_get_seconds();
545 lastalive = now - peer->lpni_last_alive;
547 /* No need to mess up peers contents with
548 * arbitrarily long integers - it suffices to
549 * know that lastalive is more than 10000s old
551 if (lastalive >= 10000)
555 lnet_net_unlock(cpt);
557 s += snprintf(s, tmpstr + tmpsiz - s,
558 "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
559 libcfs_nid2str(nid), nrefs, aliveness,
560 lastalive, maxcr, rtrcr, minrtrcr, txcr,
562 LASSERT(tmpstr + tmpsiz - s > 0);
564 } else { /* peer is NULL */
565 lnet_net_unlock(cpt);
568 if (hash == LNET_PEER_HASH_SIZE) {
572 if (peer == NULL && cpt < LNET_CPT_NUMBER)
577 len = s - tmpstr; /* how many bytes was written */
579 if (len > *lenp) { /* linux-supplied buffer is too small */
581 } else if (len > 0) { /* wrote something */
582 if (copy_to_user(buffer, tmpstr, len))
585 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
588 LIBCFS_FREE(tmpstr, tmpsiz);
596 static int __proc_lnet_buffers(void *data, int write,
597 loff_t pos, void __user *buffer, int nob)
609 /* (4 %d) * 4 * LNET_CPT_NUMBER */
610 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
611 LIBCFS_ALLOC(tmpstr, tmpsiz);
615 s = tmpstr; /* points to current position in tmpstr[] */
617 s += snprintf(s, tmpstr + tmpsiz - s,
619 "pages", "count", "credits", "min");
620 LASSERT(tmpstr + tmpsiz - s > 0);
622 if (the_lnet.ln_rtrpools == NULL)
623 goto out; /* I'm not a router */
625 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
626 struct lnet_rtrbufpool *rbp;
628 lnet_net_lock(LNET_LOCK_EX);
629 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
630 s += snprintf(s, tmpstr + tmpsiz - s,
633 rbp[idx].rbp_nbuffers,
634 rbp[idx].rbp_credits,
635 rbp[idx].rbp_mincredits);
636 LASSERT(tmpstr + tmpsiz - s > 0);
638 lnet_net_unlock(LNET_LOCK_EX);
644 if (pos >= min_t(int, len, strlen(tmpstr)))
647 rc = cfs_trace_copyout_string(buffer, nob,
650 LIBCFS_FREE(tmpstr, tmpsiz);
655 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
656 size_t *lenp, loff_t *ppos)
658 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
659 __proc_lnet_buffers);
663 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
664 size_t *lenp, loff_t *ppos)
666 int tmpsiz = 128 * LNET_CPT_NUMBER;
676 /* Just reset the min stat. */
678 struct lnet_net *net;
682 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
683 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
684 struct lnet_tx_queue *tq;
688 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
689 for (j = 0; ni->ni_cpts != NULL &&
690 j < ni->ni_ncpts; j++) {
691 if (i == ni->ni_cpts[j])
695 if (j == ni->ni_ncpts)
700 tq->tq_credits_min = tq->tq_credits;
711 LIBCFS_ALLOC(tmpstr, tmpsiz);
715 s = tmpstr; /* points to current position in tmpstr[] */
718 s += snprintf(s, tmpstr + tmpsiz - s,
719 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
720 "nid", "status", "alive", "refs", "peer",
721 "rtr", "max", "tx", "min");
722 LASSERT (tmpstr + tmpsiz - s > 0);
724 struct lnet_ni *ni = NULL;
725 int skip = *ppos - 1;
729 ni = lnet_get_ni_idx_locked(skip);
732 struct lnet_tx_queue *tq;
734 time64_t now = ktime_get_real_seconds();
735 time64_t last_alive = -1;
739 if (the_lnet.ln_routing)
740 last_alive = now - ni->ni_last_alive;
742 /* @lo forever alive */
743 if (ni->ni_net->net_lnd->lnd_type == LOLND)
747 LASSERT(ni->ni_status != NULL);
748 stat = (ni->ni_status->ns_status ==
749 LNET_NI_STATUS_UP) ? "up" : "down";
752 /* we actually output credits information for
753 * TX queue of each partition */
754 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
755 for (j = 0; ni->ni_cpts != NULL &&
756 j < ni->ni_ncpts; j++) {
757 if (i == ni->ni_cpts[j])
761 if (j == ni->ni_ncpts)
767 s += snprintf(s, tmpstr + tmpsiz - s,
768 "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
769 libcfs_nid2str(ni->ni_nid), stat,
770 last_alive, *ni->ni_refs[i],
771 ni->ni_net->net_tunables.lct_peer_tx_credits,
772 ni->ni_net->net_tunables.lct_peer_rtr_credits,
774 tq->tq_credits, tq->tq_credits_min);
778 LASSERT(tmpstr + tmpsiz - s > 0);
784 len = s - tmpstr; /* how many bytes was written */
786 if (len > *lenp) { /* linux-supplied buffer is too small */
788 } else if (len > 0) { /* wrote something */
789 if (copy_to_user(buffer, tmpstr, len))
795 LIBCFS_FREE(tmpstr, tmpsiz);
803 struct lnet_portal_rotors {
809 static struct lnet_portal_rotors portal_rotors[] = {
811 .pr_value = LNET_PTL_ROTOR_OFF,
813 .pr_desc = "Turn off message rotor for wildcard portals"
816 .pr_value = LNET_PTL_ROTOR_ON,
818 .pr_desc = "round-robin dispatch all PUT messages for "
822 .pr_value = LNET_PTL_ROTOR_RR_RT,
824 .pr_desc = "round-robin dispatch routed PUT message for "
828 .pr_value = LNET_PTL_ROTOR_HASH_RT,
829 .pr_name = "HASH_RT",
830 .pr_desc = "dispatch routed PUT message by hashing source "
831 "NID for wildcard portals"
840 static int __proc_lnet_portal_rotor(void *data, int write,
841 loff_t pos, void __user *buffer, int nob)
843 const int buf_len = 128;
849 LIBCFS_ALLOC(buf, buf_len);
856 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
857 if (portal_rotors[i].pr_value == portal_rotor)
861 LASSERT(portal_rotors[i].pr_value == portal_rotor);
864 rc = snprintf(buf, buf_len,
865 "{\n\tportals: all\n"
866 "\trotor: %s\n\tdescription: %s\n}",
867 portal_rotors[i].pr_name,
868 portal_rotors[i].pr_desc);
870 if (pos >= min_t(int, rc, buf_len)) {
873 rc = cfs_trace_copyout_string(buffer, nob,
879 rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
887 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
888 if (strncasecmp(portal_rotors[i].pr_name, tmp,
889 strlen(portal_rotors[i].pr_name)) == 0) {
890 portal_rotor = portal_rotors[i].pr_value;
897 LIBCFS_FREE(buf, buf_len);
902 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
903 size_t *lenp, loff_t *ppos)
905 return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
906 __proc_lnet_portal_rotor);
910 static struct ctl_table lnet_table[] = {
912 * NB No .strategy entries have been provided since sysctl(8) prefers
913 * to go via /proc for portability.
919 .proc_handler = &proc_lnet_stats,
923 .procname = "routes",
925 .proc_handler = &proc_lnet_routes,
929 .procname = "routers",
931 .proc_handler = &proc_lnet_routers,
937 .proc_handler = &proc_lnet_peers,
941 .procname = "buffers",
943 .proc_handler = &proc_lnet_buffers,
949 .proc_handler = &proc_lnet_nis,
953 .procname = "portal_rotor",
955 .proc_handler = &proc_lnet_portal_rotor,
960 void lnet_router_debugfs_init(void)
962 lnet_insert_debugfs(lnet_table);
965 void lnet_router_debugfs_fini(void)
967 lnet_remove_debugfs(lnet_table);