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 #define LNET_LOFFT_BITS (sizeof(loff_t) * 8)
31 /* NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
33 #define LNET_PROC_CPT_BITS (LNET_CPT_BITS + 1)
34 /* change version, 16 bits or 8 bits */
35 #define LNET_PROC_VER_BITS \
36 clamp_t(int, LNET_LOFFT_BITS / 4, 8, 16)
38 #define LNET_PROC_HASH_BITS LNET_PEER_HASH_BITS
39 /* bits for peer hash offset
40 * NB: we don't use the highest bit of *ppos because it's signed
42 #define LNET_PROC_HOFF_BITS (LNET_LOFFT_BITS - \
43 LNET_PROC_CPT_BITS - \
44 LNET_PROC_VER_BITS - \
45 LNET_PROC_HASH_BITS - 1)
46 /* bits for hash index + position */
47 #define LNET_PROC_HPOS_BITS (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
48 /* bits for peer hash table + hash version */
49 #define LNET_PROC_VPOS_BITS (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
51 #define LNET_PROC_CPT_MASK ((1ULL << LNET_PROC_CPT_BITS) - 1)
52 #define LNET_PROC_VER_MASK ((1ULL << LNET_PROC_VER_BITS) - 1)
53 #define LNET_PROC_HASH_MASK ((1ULL << LNET_PROC_HASH_BITS) - 1)
54 #define LNET_PROC_HOFF_MASK ((1ULL << LNET_PROC_HOFF_BITS) - 1)
56 #define LNET_PROC_CPT_GET(pos) \
57 (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
59 #define LNET_PROC_VER_GET(pos) \
60 (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
62 #define LNET_PROC_HASH_GET(pos) \
63 (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
65 #define LNET_PROC_HOFF_GET(pos) \
66 (int)((pos) & LNET_PROC_HOFF_MASK)
68 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off) \
69 (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) | \
70 ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) | \
71 ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
72 ((off) & LNET_PROC_HOFF_MASK))
74 #define LNET_PROC_VERSION(v) ((unsigned int)((v) & LNET_PROC_VER_MASK))
76 static int proc_cpt_table(struct ctl_table *table, int write,
77 void __user *buffer, size_t *lenp, loff_t *ppos)
89 LIBCFS_ALLOC(buf, len);
93 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
98 LIBCFS_FREE(buf, len);
110 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
113 LIBCFS_FREE(buf, len);
117 static int proc_cpt_distance(struct ctl_table *table, int write,
118 void __user *buffer, size_t *lenp, loff_t *ppos)
130 LIBCFS_ALLOC(buf, len);
134 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
139 LIBCFS_FREE(buf, len);
151 rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
154 LIBCFS_FREE(buf, len);
158 static int proc_lnet_stats(struct ctl_table *table, int write,
159 void __user *buffer, size_t *lenp, loff_t *ppos)
162 struct lnet_counters *ctrs;
163 struct lnet_counters_common common;
167 char tmpstr[256]; /* 7 %u and 4 u64 */
170 lnet_counters_reset();
176 LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
180 rc = lnet_counters_get(ctrs);
184 common = ctrs->lct_common;
186 len = scnprintf(tmpstr, sizeof(tmpstr),
187 "%u %u %u %u %u %u %u %llu %llu "
189 common.lcc_msgs_alloc, common.lcc_msgs_max,
191 common.lcc_send_count, common.lcc_recv_count,
192 common.lcc_route_count, common.lcc_drop_count,
193 common.lcc_send_length, common.lcc_recv_length,
194 common.lcc_route_length, common.lcc_drop_length);
199 rc = cfs_trace_copyout_string(buffer, nob,
202 LIBCFS_FREE(ctrs, sizeof(*ctrs));
207 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
208 size_t *lenp, loff_t *ppos)
210 const int tmpsiz = 256;
218 BUILD_BUG_ON(sizeof(loff_t) < 4);
220 off = LNET_PROC_HOFF_GET(*ppos);
221 ver = LNET_PROC_VER_GET(*ppos);
228 LIBCFS_ALLOC(tmpstr, tmpsiz);
232 s = tmpstr; /* points to current position in tmpstr[] */
235 s += scnprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
236 the_lnet.ln_routing ? "enabled" : "disabled");
237 LASSERT(tmpstr + tmpsiz - s > 0);
239 s += scnprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
240 "net", "hops", "priority", "state", "router");
241 LASSERT(tmpstr + tmpsiz - s > 0);
244 ver = (unsigned int)the_lnet.ln_remote_nets_version;
246 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
250 struct lnet_route *route = NULL;
251 struct lnet_remotenet *rnet = NULL;
253 struct list_head *rn_list;
258 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
260 LIBCFS_FREE(tmpstr, tmpsiz);
264 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
266 rn_list = &the_lnet.ln_remote_nets_hash[i];
270 while (n != rn_list && route == NULL) {
271 rnet = list_entry(n, struct lnet_remotenet,
274 r = rnet->lrn_routes.next;
276 while (r != &rnet->lrn_routes) {
277 struct lnet_route *re =
278 list_entry(r, struct lnet_route,
294 __u32 net = rnet->lrn_net;
295 __u32 hops = route->lr_hops;
296 unsigned int priority = route->lr_priority;
297 int alive = lnet_is_route_alive(route);
299 s += scnprintf(s, tmpstr + tmpsiz - s,
300 "%-8s %4d %8u %7s %s\n",
301 libcfs_net2str(net), hops,
303 alive ? "up" : "down",
304 libcfs_nidstr(&route->lr_nid));
305 LASSERT(tmpstr + tmpsiz - s > 0);
311 len = s - tmpstr; /* how many bytes was written */
313 if (len > *lenp) { /* linux-supplied buffer is too small */
315 } else if (len > 0) { /* wrote something */
316 if (copy_to_user(buffer, tmpstr, len))
320 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
324 LIBCFS_FREE(tmpstr, tmpsiz);
333 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
334 size_t *lenp, loff_t *ppos)
339 const int tmpsiz = 256;
344 off = LNET_PROC_HOFF_GET(*ppos);
345 ver = LNET_PROC_VER_GET(*ppos);
352 LIBCFS_ALLOC(tmpstr, tmpsiz);
356 s = tmpstr; /* points to current position in tmpstr[] */
359 s += scnprintf(s, tmpstr + tmpsiz - s,
361 "ref", "rtr_ref", "alive", "router");
362 LASSERT(tmpstr + tmpsiz - s > 0);
365 ver = (unsigned int)the_lnet.ln_routers_version;
367 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
370 struct lnet_peer *peer = NULL;
375 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
378 LIBCFS_FREE(tmpstr, tmpsiz);
382 r = the_lnet.ln_routers.next;
384 while (r != &the_lnet.ln_routers) {
385 struct lnet_peer *lp =
386 list_entry(r, struct lnet_peer,
399 struct lnet_nid *nid = &peer->lp_primary_nid;
400 int nrefs = atomic_read(&peer->lp_refcount);
401 int nrtrrefs = peer->lp_rtr_refcount;
402 int alive = lnet_is_gateway_alive(peer);
404 s += scnprintf(s, tmpstr + tmpsiz - s,
407 alive ? "up" : "down",
414 len = s - tmpstr; /* how many bytes was written */
416 if (len > *lenp) { /* linux-supplied buffer is too small */
418 } else if (len > 0) { /* wrote something */
419 if (copy_to_user(buffer, tmpstr, len))
423 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
427 LIBCFS_FREE(tmpstr, tmpsiz);
435 /* TODO: there should be no direct access to ptable. We should add a set
436 * of APIs that give access to the ptable and its members */
438 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
439 size_t *lenp, loff_t *ppos)
441 const int tmpsiz = 256;
442 struct lnet_peer_table *ptable;
445 int cpt = LNET_PROC_CPT_GET(*ppos);
446 int ver = LNET_PROC_VER_GET(*ppos);
447 int hash = LNET_PROC_HASH_GET(*ppos);
448 int hoff = LNET_PROC_HOFF_GET(*ppos);
454 struct lnet_peer_ni *peer;
456 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
458 for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
459 list_for_each_entry(peer,
460 &ptable->pt_hash[hash],
462 peer->lpni_mintxcredits =
463 peer->lpni_txcredits;
464 peer->lpni_minrtrcredits =
465 peer->lpni_rtrcredits;
477 BUILD_BUG_ON(LNET_PROC_HASH_BITS < LNET_PEER_HASH_BITS);
479 if (cpt >= LNET_CPT_NUMBER) {
484 LIBCFS_ALLOC(tmpstr, tmpsiz);
488 s = tmpstr; /* points to current position in tmpstr[] */
491 s += scnprintf(s, tmpstr + tmpsiz - s,
492 "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
493 "nid", "refs", "state", "last", "max",
494 "rtr", "min", "tx", "min", "queue");
495 LASSERT(tmpstr + tmpsiz - s > 0);
499 struct lnet_peer_ni *peer;
509 ptable = the_lnet.ln_peer_tables[cpt];
511 ver = LNET_PROC_VERSION(ptable->pt_version);
513 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
514 lnet_net_unlock(cpt);
515 LIBCFS_FREE(tmpstr, tmpsiz);
519 while (hash < LNET_PEER_HASH_SIZE) {
521 p = ptable->pt_hash[hash].next;
523 while (p != &ptable->pt_hash[hash]) {
524 struct lnet_peer_ni *lp =
525 list_entry(p, struct lnet_peer_ni,
530 /* minor optimization: start from idx+1
531 * on next iteration if we've just
532 * drained lpni_hashlist */
533 if (lp->lpni_hashlist.next ==
534 &ptable->pt_hash[hash]) {
545 p = lp->lpni_hashlist.next;
557 struct lnet_nid nid = peer->lpni_nid;
558 int nrefs = kref_read(&peer->lpni_kref);
559 time64_t lastalive = -1;
560 char *aliveness = "NA";
561 int maxcr = (peer->lpni_net) ?
562 peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
563 int txcr = peer->lpni_txcredits;
564 int mintxcr = peer->lpni_mintxcredits;
565 int rtrcr = peer->lpni_rtrcredits;
566 int minrtrcr = peer->lpni_minrtrcredits;
567 int txqnob = peer->lpni_txqnob;
569 if (lnet_isrouter(peer) ||
570 lnet_peer_aliveness_enabled(peer))
571 aliveness = lnet_is_peer_ni_alive(peer) ?
574 lnet_net_unlock(cpt);
576 s += scnprintf(s, tmpstr + tmpsiz - s,
577 "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
578 libcfs_nidstr(&nid), nrefs, aliveness,
579 lastalive, maxcr, rtrcr, minrtrcr, txcr,
581 LASSERT(tmpstr + tmpsiz - s > 0);
583 } else { /* peer is NULL */
584 lnet_net_unlock(cpt);
587 if (hash == LNET_PEER_HASH_SIZE) {
591 if (peer == NULL && cpt < LNET_CPT_NUMBER)
596 len = s - tmpstr; /* how many bytes was written */
598 if (len > *lenp) { /* linux-supplied buffer is too small */
600 } else if (len > 0) { /* wrote something */
601 if (copy_to_user(buffer, tmpstr, len))
604 *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
607 LIBCFS_FREE(tmpstr, tmpsiz);
615 static int proc_lnet_buffers(struct ctl_table *table, int write,
616 void __user *buffer, size_t *lenp, loff_t *ppos)
630 /* (4 %d) * 4 * LNET_CPT_NUMBER */
631 tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
632 LIBCFS_ALLOC(tmpstr, tmpsiz);
636 s = tmpstr; /* points to current position in tmpstr[] */
638 s += scnprintf(s, tmpstr + tmpsiz - s,
640 "pages", "count", "credits", "min");
641 LASSERT(tmpstr + tmpsiz - s > 0);
643 if (the_lnet.ln_rtrpools == NULL)
644 goto out; /* I'm not a router */
646 for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
647 struct lnet_rtrbufpool *rbp;
649 lnet_net_lock(LNET_LOCK_EX);
650 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
651 s += scnprintf(s, tmpstr + tmpsiz - s,
654 rbp[idx].rbp_nbuffers,
655 rbp[idx].rbp_credits,
656 rbp[idx].rbp_mincredits);
657 LASSERT(tmpstr + tmpsiz - s > 0);
659 lnet_net_unlock(LNET_LOCK_EX);
665 if (pos >= min_t(int, len, strlen(tmpstr)))
668 rc = cfs_trace_copyout_string(buffer, nob,
671 LIBCFS_FREE(tmpstr, tmpsiz);
676 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
677 size_t *lenp, loff_t *ppos)
679 int tmpsiz = 128 * LNET_CPT_NUMBER;
689 /* Just reset the min stat. */
691 struct lnet_net *net;
695 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
696 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
697 struct lnet_tx_queue *tq;
701 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
702 for (j = 0; ni->ni_cpts != NULL &&
703 j < ni->ni_ncpts; j++) {
704 if (i == ni->ni_cpts[j])
708 if (j == ni->ni_ncpts)
713 tq->tq_credits_min = tq->tq_credits;
724 LIBCFS_ALLOC(tmpstr, tmpsiz);
728 s = tmpstr; /* points to current position in tmpstr[] */
731 s += scnprintf(s, tmpstr + tmpsiz - s,
732 "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
733 "nid", "status", "alive", "refs", "peer",
734 "rtr", "max", "tx", "min");
735 LASSERT (tmpstr + tmpsiz - s > 0);
737 struct lnet_ni *ni = NULL;
738 int skip = *ppos - 1;
742 ni = lnet_get_ni_idx_locked(skip);
745 struct lnet_tx_queue *tq;
747 time64_t now = ktime_get_seconds();
748 time64_t last_alive = -1;
752 if (the_lnet.ln_routing)
753 last_alive = now - ni->ni_net->net_last_alive;
756 LASSERT(ni->ni_status != NULL);
757 stat = (lnet_ni_get_status_locked(ni) ==
758 LNET_NI_STATUS_UP) ? "up" : "down";
761 /* @lo forever alive */
762 if (ni->ni_net->net_lnd->lnd_type == LOLND) {
767 /* we actually output credits information for
768 * TX queue of each partition */
769 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
770 for (j = 0; ni->ni_cpts != NULL &&
771 j < ni->ni_ncpts; j++) {
772 if (i == ni->ni_cpts[j])
776 if (j == ni->ni_ncpts)
782 s += scnprintf(s, tmpstr + tmpsiz - s,
783 "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
784 libcfs_nidstr(&ni->ni_nid), stat,
785 last_alive, *ni->ni_refs[i],
786 ni->ni_net->net_tunables.lct_peer_tx_credits,
787 ni->ni_net->net_tunables.lct_peer_rtr_credits,
789 tq->tq_credits, tq->tq_credits_min);
793 LASSERT(tmpstr + tmpsiz - s > 0);
799 len = s - tmpstr; /* how many bytes was written */
801 if (len > *lenp) { /* linux-supplied buffer is too small */
803 } else if (len > 0) { /* wrote something */
804 if (copy_to_user(buffer, tmpstr, len))
810 LIBCFS_FREE(tmpstr, tmpsiz);
818 struct lnet_portal_rotors {
824 static struct lnet_portal_rotors portal_rotors[] = {
826 .pr_value = LNET_PTL_ROTOR_OFF,
828 .pr_desc = "Turn off message rotor for wildcard portals"
831 .pr_value = LNET_PTL_ROTOR_ON,
833 .pr_desc = "round-robin dispatch all PUT messages for "
837 .pr_value = LNET_PTL_ROTOR_RR_RT,
839 .pr_desc = "round-robin dispatch routed PUT message for "
843 .pr_value = LNET_PTL_ROTOR_HASH_RT,
844 .pr_name = "HASH_RT",
845 .pr_desc = "dispatch routed PUT message by hashing source "
846 "NID for wildcard portals"
855 static int proc_lnet_portal_rotor(struct ctl_table *table, int write,
856 void __user *buffer, size_t *lenp,
859 const int buf_len = 128;
868 LIBCFS_ALLOC(buf, buf_len);
874 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
875 if (portal_rotors[i].pr_value == portal_rotor)
879 LASSERT(portal_rotors[i].pr_value == portal_rotor);
882 rc = scnprintf(buf, buf_len,
883 "{\n\tportals: all\n"
884 "\trotor: %s\n\tdescription: %s\n}",
885 portal_rotors[i].pr_name,
886 portal_rotors[i].pr_desc);
888 if (pos >= min_t(int, rc, buf_len)) {
891 rc = cfs_trace_copyout_string(buffer, nob,
894 LIBCFS_FREE(buf, buf_len);
899 buf = memdup_user_nul(buffer, nob);
907 for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
908 if (strncasecmp(portal_rotors[i].pr_name, tmp,
909 strlen(portal_rotors[i].pr_name)) == 0) {
910 portal_rotor = portal_rotors[i].pr_value;
921 static struct ctl_table lnet_table[] = {
923 * NB No .strategy entries have been provided since sysctl(8) prefers
924 * to go via /proc for portability.
927 .procname = "cpu_partition_table",
930 .proc_handler = &proc_cpt_table,
933 .procname = "cpu_partition_distance",
936 .proc_handler = &proc_cpt_distance,
941 .proc_handler = &proc_lnet_stats,
944 .procname = "routes",
946 .proc_handler = &proc_lnet_routes,
949 .procname = "routers",
951 .proc_handler = &proc_lnet_routers,
956 .proc_handler = &proc_lnet_peers,
959 .procname = "buffers",
961 .proc_handler = &proc_lnet_buffers,
966 .proc_handler = &proc_lnet_nis,
969 .procname = "portal_rotor",
971 .proc_handler = &proc_lnet_portal_rotor,
974 .procname = "lnet_lnd_timeout",
975 .data = &lnet_lnd_timeout,
976 .maxlen = sizeof(lnet_lnd_timeout),
978 .proc_handler = &debugfs_doint,
983 static void *debugfs_state;
985 void lnet_router_debugfs_init(void)
987 lnet_insert_debugfs(lnet_table, THIS_MODULE,
991 void lnet_router_debugfs_fini(void)
993 lnet_remove_debugfs(lnet_table);
995 void lnet_router_exit(void)
997 lnet_debugfs_fini(&debugfs_state);