Whamcloud - gitweb
LU-14361 statahead: add connect flag check for batch RPC
[fs/lustre-release.git] / lnet / lnet / lnet_debugfs.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2017, Intel Corporation.
5  *
6  *   This file is part of Lustre, https://wiki.whamcloud.com/
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <linux/uaccess.h>
26
27 #include <libcfs/libcfs.h>
28 #include <lnet/lib-lnet.h>
29
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
32  */
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)
37
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
41  */
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)
50
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)
55
56 #define LNET_PROC_CPT_GET(pos)                          \
57         (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
58
59 #define LNET_PROC_VER_GET(pos)                          \
60         (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
61
62 #define LNET_PROC_HASH_GET(pos)                         \
63         (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
64
65 #define LNET_PROC_HOFF_GET(pos)                         \
66         (int)((pos) & LNET_PROC_HOFF_MASK)
67
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))
73
74 #define LNET_PROC_VERSION(v)    ((unsigned int)((v) & LNET_PROC_VER_MASK))
75
76 static int proc_cpt_table(struct ctl_table *table, int write,
77                           void __user *buffer, size_t *lenp, loff_t *ppos)
78 {
79         size_t nob = *lenp;
80         loff_t pos = *ppos;
81         char *buf = NULL;
82         int len = 4096;
83         int rc  = 0;
84
85         if (write)
86                 return -EPERM;
87
88         while (1) {
89                 LIBCFS_ALLOC(buf, len);
90                 if (!buf)
91                         return -ENOMEM;
92
93                 rc = cfs_cpt_table_print(cfs_cpt_tab, buf, len);
94                 if (rc >= 0)
95                         break;
96
97                 if (rc == -EFBIG) {
98                         LIBCFS_FREE(buf, len);
99                         len <<= 1;
100                         continue;
101                 }
102                 goto out;
103         }
104
105         if (pos >= rc) {
106                 rc = 0;
107                 goto out;
108         }
109
110         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
111 out:
112         if (buf)
113                 LIBCFS_FREE(buf, len);
114         return rc;
115 }
116
117 static int proc_cpt_distance(struct ctl_table *table, int write,
118                              void __user *buffer, size_t *lenp, loff_t *ppos)
119 {
120         size_t nob = *lenp;
121         loff_t pos = *ppos;
122         char *buf = NULL;
123         int len = 4096;
124         int rc  = 0;
125
126         if (write)
127                 return -EPERM;
128
129         while (1) {
130                 LIBCFS_ALLOC(buf, len);
131                 if (!buf)
132                         return -ENOMEM;
133
134                 rc = cfs_cpt_distance_print(cfs_cpt_tab, buf, len);
135                 if (rc >= 0)
136                         break;
137
138                 if (rc == -EFBIG) {
139                         LIBCFS_FREE(buf, len);
140                         len <<= 1;
141                         continue;
142                 }
143                 goto out;
144         }
145
146         if (pos >= rc) {
147                 rc = 0;
148                 goto out;
149         }
150
151         rc = cfs_trace_copyout_string(buffer, nob, buf + pos, NULL);
152 out:
153         if (buf)
154                 LIBCFS_FREE(buf, len);
155         return rc;
156 }
157
158 static int proc_lnet_stats(struct ctl_table *table, int write,
159                            void __user *buffer, size_t *lenp, loff_t *ppos)
160 {
161         int rc;
162         struct lnet_counters *ctrs;
163         struct lnet_counters_common common;
164         size_t nob = *lenp;
165         loff_t pos = *ppos;
166         int len;
167         char tmpstr[256]; /* 7 %u and 4 u64 */
168
169         if (write) {
170                 lnet_counters_reset();
171                 return 0;
172         }
173
174         /* read */
175
176         LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
177         if (ctrs == NULL)
178                 return -ENOMEM;
179
180         rc = lnet_counters_get(ctrs);
181         if (rc)
182                 goto out_no_ctrs;
183
184         common = ctrs->lct_common;
185
186         len = scnprintf(tmpstr, sizeof(tmpstr),
187                         "%u %u %u %u %u %u %u %llu %llu "
188                         "%llu %llu",
189                         common.lcc_msgs_alloc, common.lcc_msgs_max,
190                         common.lcc_errors,
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);
195
196         if (pos >= len)
197                 rc = 0;
198         else
199                 rc = cfs_trace_copyout_string(buffer, nob,
200                                               tmpstr + pos, "\n");
201 out_no_ctrs:
202         LIBCFS_FREE(ctrs, sizeof(*ctrs));
203         return rc;
204 }
205
206 static int
207 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
208                  size_t *lenp, loff_t *ppos)
209 {
210         const int       tmpsiz = 256;
211         char            *tmpstr;
212         char            *s;
213         int             rc = 0;
214         int             len;
215         int             ver;
216         int             off;
217
218         BUILD_BUG_ON(sizeof(loff_t) < 4);
219
220         off = LNET_PROC_HOFF_GET(*ppos);
221         ver = LNET_PROC_VER_GET(*ppos);
222
223         LASSERT(!write);
224
225         if (*lenp == 0)
226                 return 0;
227
228         LIBCFS_ALLOC(tmpstr, tmpsiz);
229         if (tmpstr == NULL)
230                 return -ENOMEM;
231
232         s = tmpstr; /* points to current position in tmpstr[] */
233
234         if (*ppos == 0) {
235                 s += scnprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
236                                the_lnet.ln_routing ? "enabled" : "disabled");
237                 LASSERT(tmpstr + tmpsiz - s > 0);
238
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);
242
243                 lnet_net_lock(0);
244                 ver = (unsigned int)the_lnet.ln_remote_nets_version;
245                 lnet_net_unlock(0);
246                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
247         } else {
248                 struct list_head        *n;
249                 struct list_head        *r;
250                 struct lnet_route               *route = NULL;
251                 struct lnet_remotenet   *rnet  = NULL;
252                 int                     skip  = off - 1;
253                 struct list_head        *rn_list;
254                 int                     i;
255
256                 lnet_net_lock(0);
257
258                 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
259                         lnet_net_unlock(0);
260                         LIBCFS_FREE(tmpstr, tmpsiz);
261                         return -ESTALE;
262                 }
263
264                 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
265                      i++) {
266                         rn_list = &the_lnet.ln_remote_nets_hash[i];
267
268                         n = rn_list->next;
269
270                         while (n != rn_list && route == NULL) {
271                                 rnet = list_entry(n, struct lnet_remotenet,
272                                                   lrn_list);
273
274                                 r = rnet->lrn_routes.next;
275
276                                 while (r != &rnet->lrn_routes) {
277                                         struct lnet_route *re =
278                                                 list_entry(r, struct lnet_route,
279                                                            lr_list);
280                                         if (skip == 0) {
281                                                 route = re;
282                                                 break;
283                                         }
284
285                                         skip--;
286                                         r = r->next;
287                                 }
288
289                                 n = n->next;
290                         }
291                 }
292
293                 if (route != NULL) {
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);
298
299                         s += scnprintf(s, tmpstr + tmpsiz - s,
300                                        "%-8s %4d %8u %7s %s\n",
301                                        libcfs_net2str(net), hops,
302                                        priority,
303                                        alive ? "up" : "down",
304                                        libcfs_nidstr(&route->lr_nid));
305                         LASSERT(tmpstr + tmpsiz - s > 0);
306                 }
307
308                 lnet_net_unlock(0);
309         }
310
311         len = s - tmpstr;     /* how many bytes was written */
312
313         if (len > *lenp) {    /* linux-supplied buffer is too small */
314                 rc = -EINVAL;
315         } else if (len > 0) { /* wrote something */
316                 if (copy_to_user(buffer, tmpstr, len))
317                         rc = -EFAULT;
318                 else {
319                         off += 1;
320                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
321                 }
322         }
323
324         LIBCFS_FREE(tmpstr, tmpsiz);
325
326         if (rc == 0)
327                 *lenp = len;
328
329         return rc;
330 }
331
332 static int
333 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
334                   size_t *lenp, loff_t *ppos)
335 {
336         int        rc = 0;
337         char      *tmpstr;
338         char      *s;
339         const int  tmpsiz = 256;
340         int        len;
341         int        ver;
342         int        off;
343
344         off = LNET_PROC_HOFF_GET(*ppos);
345         ver = LNET_PROC_VER_GET(*ppos);
346
347         LASSERT(!write);
348
349         if (*lenp == 0)
350                 return 0;
351
352         LIBCFS_ALLOC(tmpstr, tmpsiz);
353         if (tmpstr == NULL)
354                 return -ENOMEM;
355
356         s = tmpstr; /* points to current position in tmpstr[] */
357
358         if (*ppos == 0) {
359                 s += scnprintf(s, tmpstr + tmpsiz - s,
360                                "%-4s %7s %5s %s\n",
361                                "ref", "rtr_ref", "alive", "router");
362                 LASSERT(tmpstr + tmpsiz - s > 0);
363
364                 lnet_net_lock(0);
365                 ver = (unsigned int)the_lnet.ln_routers_version;
366                 lnet_net_unlock(0);
367                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
368         } else {
369                 struct list_head *r;
370                 struct lnet_peer *peer = NULL;
371                 int               skip = off - 1;
372
373                 lnet_net_lock(0);
374
375                 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
376                         lnet_net_unlock(0);
377
378                         LIBCFS_FREE(tmpstr, tmpsiz);
379                         return -ESTALE;
380                 }
381
382                 r = the_lnet.ln_routers.next;
383
384                 while (r != &the_lnet.ln_routers) {
385                         struct lnet_peer *lp =
386                           list_entry(r, struct lnet_peer,
387                                      lp_rtr_list);
388
389                         if (skip == 0) {
390                                 peer = lp;
391                                 break;
392                         }
393
394                         skip--;
395                         r = r->next;
396                 }
397
398                 if (peer != NULL) {
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);
403
404                         s += scnprintf(s, tmpstr + tmpsiz - s,
405                                        "%-4d %7d %5s %s\n",
406                                        nrefs, nrtrrefs,
407                                        alive ? "up" : "down",
408                                        libcfs_nidstr(nid));
409                 }
410
411                 lnet_net_unlock(0);
412         }
413
414         len = s - tmpstr;     /* how many bytes was written */
415
416         if (len > *lenp) {    /* linux-supplied buffer is too small */
417                 rc = -EINVAL;
418         } else if (len > 0) { /* wrote something */
419                 if (copy_to_user(buffer, tmpstr, len))
420                         rc = -EFAULT;
421                 else {
422                         off += 1;
423                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
424                 }
425         }
426
427         LIBCFS_FREE(tmpstr, tmpsiz);
428
429         if (rc == 0)
430                 *lenp = len;
431
432         return rc;
433 }
434
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 */
437 static int
438 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
439                 size_t *lenp, loff_t *ppos)
440 {
441         const int               tmpsiz  = 256;
442         struct lnet_peer_table  *ptable;
443         char                    *tmpstr = NULL;
444         char                    *s;
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);
449         int                     rc = 0;
450         int                     len;
451
452         if (write) {
453                 int i;
454                 struct lnet_peer_ni *peer;
455
456                 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
457                         lnet_net_lock(i);
458                         for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
459                                 list_for_each_entry(peer,
460                                                     &ptable->pt_hash[hash],
461                                                     lpni_hashlist) {
462                                         peer->lpni_mintxcredits =
463                                                 peer->lpni_txcredits;
464                                         peer->lpni_minrtrcredits =
465                                                 peer->lpni_rtrcredits;
466                                 }
467                         }
468                         lnet_net_unlock(i);
469                 }
470                 *ppos += *lenp;
471                 return 0;
472         }
473
474         if (*lenp == 0)
475                 return 0;
476
477         BUILD_BUG_ON(LNET_PROC_HASH_BITS < LNET_PEER_HASH_BITS);
478
479         if (cpt >= LNET_CPT_NUMBER) {
480                 *lenp = 0;
481                 return 0;
482         }
483
484         LIBCFS_ALLOC(tmpstr, tmpsiz);
485         if (tmpstr == NULL)
486                 return -ENOMEM;
487
488         s = tmpstr; /* points to current position in tmpstr[] */
489
490         if (*ppos == 0) {
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);
496
497                 hoff++;
498         } else {
499                 struct lnet_peer_ni     *peer;
500                 struct list_head        *p;
501                 int                     skip;
502
503  again:
504                 p = NULL;
505                 peer = NULL;
506                 skip = hoff - 1;
507
508                 lnet_net_lock(cpt);
509                 ptable = the_lnet.ln_peer_tables[cpt];
510                 if (hoff == 1)
511                         ver = LNET_PROC_VERSION(ptable->pt_version);
512
513                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
514                         lnet_net_unlock(cpt);
515                         LIBCFS_FREE(tmpstr, tmpsiz);
516                         return -ESTALE;
517                 }
518
519                 while (hash < LNET_PEER_HASH_SIZE) {
520                         if (p == NULL)
521                                 p = ptable->pt_hash[hash].next;
522
523                         while (p != &ptable->pt_hash[hash]) {
524                                 struct lnet_peer_ni *lp =
525                                   list_entry(p, struct lnet_peer_ni,
526                                              lpni_hashlist);
527                                 if (skip == 0) {
528                                         peer = lp;
529
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]) {
535                                                 hoff = 1;
536                                                 hash++;
537                                         } else {
538                                                 hoff++;
539                                         }
540
541                                         break;
542                                 }
543
544                                 skip--;
545                                 p = lp->lpni_hashlist.next;
546                         }
547
548                         if (peer != NULL)
549                                 break;
550
551                         p = NULL;
552                         hoff = 1;
553                         hash++;
554                 }
555
556                 if (peer != NULL) {
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;
568
569                         if (lnet_isrouter(peer) ||
570                             lnet_peer_aliveness_enabled(peer))
571                                 aliveness = lnet_is_peer_ni_alive(peer) ?
572                                         "up" : "down";
573
574                         lnet_net_unlock(cpt);
575
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,
580                                        mintxcr, txqnob);
581                         LASSERT(tmpstr + tmpsiz - s > 0);
582
583                 } else { /* peer is NULL */
584                         lnet_net_unlock(cpt);
585                 }
586
587                 if (hash == LNET_PEER_HASH_SIZE) {
588                         cpt++;
589                         hash = 0;
590                         hoff = 1;
591                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
592                                 goto again;
593                 }
594         }
595
596         len = s - tmpstr;     /* how many bytes was written */
597
598         if (len > *lenp) {    /* linux-supplied buffer is too small */
599                 rc = -EINVAL;
600         } else if (len > 0) { /* wrote something */
601                 if (copy_to_user(buffer, tmpstr, len))
602                         rc = -EFAULT;
603                 else
604                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
605         }
606
607         LIBCFS_FREE(tmpstr, tmpsiz);
608
609         if (rc == 0)
610                 *lenp = len;
611
612         return rc;
613 }
614
615 static int proc_lnet_buffers(struct ctl_table *table, int write,
616                              void __user *buffer, size_t *lenp, loff_t *ppos)
617 {
618         size_t nob = *lenp;
619         loff_t pos = *ppos;
620         char            *s;
621         char            *tmpstr;
622         int             tmpsiz;
623         int             idx;
624         int             len;
625         int             rc;
626         int             i;
627
628         LASSERT(!write);
629
630         /* (4 %d) * 4 * LNET_CPT_NUMBER */
631         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
632         LIBCFS_ALLOC(tmpstr, tmpsiz);
633         if (tmpstr == NULL)
634                 return -ENOMEM;
635
636         s = tmpstr; /* points to current position in tmpstr[] */
637
638         s += scnprintf(s, tmpstr + tmpsiz - s,
639                        "%5s %5s %7s %7s\n",
640                        "pages", "count", "credits", "min");
641         LASSERT(tmpstr + tmpsiz - s > 0);
642
643         if (the_lnet.ln_rtrpools == NULL)
644                 goto out; /* I'm not a router */
645
646         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
647                 struct lnet_rtrbufpool *rbp;
648
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,
652                                        "%5d %5d %7d %7d\n",
653                                        rbp[idx].rbp_npages,
654                                        rbp[idx].rbp_nbuffers,
655                                        rbp[idx].rbp_credits,
656                                        rbp[idx].rbp_mincredits);
657                         LASSERT(tmpstr + tmpsiz - s > 0);
658                 }
659                 lnet_net_unlock(LNET_LOCK_EX);
660         }
661
662  out:
663         len = s - tmpstr;
664
665         if (pos >= min_t(int, len, strlen(tmpstr)))
666                 rc = 0;
667         else
668                 rc = cfs_trace_copyout_string(buffer, nob,
669                                               tmpstr + pos, NULL);
670
671         LIBCFS_FREE(tmpstr, tmpsiz);
672         return rc;
673 }
674
675 static int
676 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
677               size_t *lenp, loff_t *ppos)
678 {
679         int     tmpsiz = 128 * LNET_CPT_NUMBER;
680         int     rc = 0;
681         char    *tmpstr;
682         char    *s;
683         int     len;
684
685         if (*lenp == 0)
686                 return 0;
687
688         if (write) {
689                 /* Just reset the min stat. */
690                 struct lnet_ni  *ni;
691                 struct lnet_net *net;
692
693                 lnet_net_lock(0);
694
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;
698                                 int i;
699                                 int j;
700
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])
705                                                         break;
706                                         }
707
708                                         if (j == ni->ni_ncpts)
709                                                 continue;
710
711                                         if (i != 0)
712                                                 lnet_net_lock(i);
713                                         tq->tq_credits_min = tq->tq_credits;
714                                         if (i != 0)
715                                                 lnet_net_unlock(i);
716                                 }
717                         }
718                 }
719                 lnet_net_unlock(0);
720                 *ppos += *lenp;
721                 return 0;
722         }
723
724         LIBCFS_ALLOC(tmpstr, tmpsiz);
725         if (tmpstr == NULL)
726                 return -ENOMEM;
727
728         s = tmpstr; /* points to current position in tmpstr[] */
729
730         if (*ppos == 0) {
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);
736         } else {
737                 struct lnet_ni *ni   = NULL;
738                 int skip = *ppos - 1;
739
740                 lnet_net_lock(0);
741
742                 ni = lnet_get_ni_idx_locked(skip);
743
744                 if (ni != NULL) {
745                         struct lnet_tx_queue *tq;
746                         char *stat;
747                         time64_t now = ktime_get_seconds();
748                         time64_t last_alive = -1;
749                         int i;
750                         int j;
751
752                         if (the_lnet.ln_routing)
753                                 last_alive = now - ni->ni_net->net_last_alive;
754
755                         lnet_ni_lock(ni);
756                         LASSERT(ni->ni_status != NULL);
757                         stat = (lnet_ni_get_status_locked(ni) ==
758                                 LNET_NI_STATUS_UP) ? "up" : "down";
759                         lnet_ni_unlock(ni);
760
761                         /* @lo forever alive */
762                         if (ni->ni_net->net_lnd->lnd_type == LOLND) {
763                                 last_alive = 0;
764                                 stat = "up";
765                         }
766
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])
773                                                 break;
774                                 }
775
776                                 if (j == ni->ni_ncpts)
777                                         continue;
778
779                                 if (i != 0)
780                                         lnet_net_lock(i);
781
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,
788                                        tq->tq_credits_max,
789                                        tq->tq_credits, tq->tq_credits_min);
790                                 if (i != 0)
791                                         lnet_net_unlock(i);
792                         }
793                         LASSERT(tmpstr + tmpsiz - s > 0);
794                 }
795
796                 lnet_net_unlock(0);
797         }
798
799         len = s - tmpstr;     /* how many bytes was written */
800
801         if (len > *lenp) {    /* linux-supplied buffer is too small */
802                 rc = -EINVAL;
803         } else if (len > 0) { /* wrote something */
804                 if (copy_to_user(buffer, tmpstr, len))
805                         rc = -EFAULT;
806                 else
807                         *ppos += 1;
808         }
809
810         LIBCFS_FREE(tmpstr, tmpsiz);
811
812         if (rc == 0)
813                 *lenp = len;
814
815         return rc;
816 }
817
818 struct lnet_portal_rotors {
819         int             pr_value;
820         const char      *pr_name;
821         const char      *pr_desc;
822 };
823
824 static struct lnet_portal_rotors        portal_rotors[] = {
825         {
826                 .pr_value = LNET_PTL_ROTOR_OFF,
827                 .pr_name  = "OFF",
828                 .pr_desc  = "Turn off message rotor for wildcard portals"
829         },
830         {
831                 .pr_value = LNET_PTL_ROTOR_ON,
832                 .pr_name  = "ON",
833                 .pr_desc  = "round-robin dispatch all PUT messages for "
834                             "wildcard portals"
835         },
836         {
837                 .pr_value = LNET_PTL_ROTOR_RR_RT,
838                 .pr_name  = "RR_RT",
839                 .pr_desc  = "round-robin dispatch routed PUT message for "
840                             "wildcard portals"
841         },
842         {
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"
847         },
848         {
849                 .pr_value = -1,
850                 .pr_name  = NULL,
851                 .pr_desc  = NULL
852         },
853 };
854
855 static int proc_lnet_portal_rotor(struct ctl_table *table, int write,
856                                   void __user *buffer, size_t *lenp,
857                                   loff_t *ppos)
858 {
859         const int       buf_len = 128;
860         size_t nob = *lenp;
861         loff_t pos = *ppos;
862         char            *buf;
863         char            *tmp;
864         int             rc;
865         int             i;
866
867         if (!write) {
868                 LIBCFS_ALLOC(buf, buf_len);
869                 if (buf == NULL)
870                         return -ENOMEM;
871
872                 lnet_res_lock(0);
873
874                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
875                         if (portal_rotors[i].pr_value == portal_rotor)
876                                 break;
877                 }
878
879                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
880                 lnet_res_unlock(0);
881
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);
887
888                 if (pos >= min_t(int, rc, buf_len)) {
889                         rc = 0;
890                 } else {
891                         rc = cfs_trace_copyout_string(buffer, nob,
892                                                       buf + pos, "\n");
893                 }
894                 LIBCFS_FREE(buf, buf_len);
895
896                 return rc;
897         }
898
899         buf = memdup_user_nul(buffer, nob);
900         if (IS_ERR(buf))
901                 return PTR_ERR(buf);
902
903         tmp = strim(buf);
904
905         rc = -EINVAL;
906         lnet_res_lock(0);
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;
911                         rc = 0;
912                         break;
913                 }
914         }
915         lnet_res_unlock(0);
916         kfree(buf);
917
918         return rc;
919 }
920
921 static struct ctl_table lnet_table[] = {
922         /*
923          * NB No .strategy entries have been provided since sysctl(8) prefers
924          * to go via /proc for portability.
925          */
926         {
927                 .procname       = "cpu_partition_table",
928                 .maxlen         = 128,
929                 .mode           = 0444,
930                 .proc_handler   = &proc_cpt_table,
931         },
932         {
933                 .procname       = "cpu_partition_distance",
934                 .maxlen         = 128,
935                 .mode           = 0444,
936                 .proc_handler   = &proc_cpt_distance,
937         },
938         {
939                 .procname       = "stats",
940                 .mode           = 0644,
941                 .proc_handler   = &proc_lnet_stats,
942         },
943         {
944                 .procname       = "routes",
945                 .mode           = 0444,
946                 .proc_handler   = &proc_lnet_routes,
947         },
948         {
949                 .procname       = "routers",
950                 .mode           = 0444,
951                 .proc_handler   = &proc_lnet_routers,
952         },
953         {
954                 .procname       = "peers",
955                 .mode           = 0644,
956                 .proc_handler   = &proc_lnet_peers,
957         },
958         {
959                 .procname       = "buffers",
960                 .mode           = 0444,
961                 .proc_handler   = &proc_lnet_buffers,
962         },
963         {
964                 .procname       = "nis",
965                 .mode           = 0644,
966                 .proc_handler   = &proc_lnet_nis,
967         },
968         {
969                 .procname       = "portal_rotor",
970                 .mode           = 0644,
971                 .proc_handler   = &proc_lnet_portal_rotor,
972         },
973         {
974                 .procname       = "lnet_lnd_timeout",
975                 .data           = &lnet_lnd_timeout,
976                 .maxlen         = sizeof(lnet_lnd_timeout),
977                 .mode           = 0444,
978                 .proc_handler   = &debugfs_doint,
979         },
980         { .procname = NULL }
981 };
982
983 static void *debugfs_state;
984
985 void lnet_router_debugfs_init(void)
986 {
987         lnet_insert_debugfs(lnet_table, THIS_MODULE,
988                             &debugfs_state);
989 }
990
991 void lnet_router_debugfs_fini(void)
992 {
993         lnet_remove_debugfs(lnet_table);
994 }
995 void lnet_router_exit(void)
996 {
997         lnet_debugfs_fini(&debugfs_state);
998 }