Whamcloud - gitweb
LU-11986 lnet: don't read debugfs lnet stats when shutting down
[fs/lustre-release.git] / lnet / lnet / router_proc.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 /* This is really lnet_proc.c. You might need to update sanity test 215
31  * if any file format is changed. */
32
33 #define LNET_LOFFT_BITS         (sizeof(loff_t) * 8)
34 /*
35  * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
36  */
37 #define LNET_PROC_CPT_BITS      (LNET_CPT_BITS + 1)
38 /* change version, 16 bits or 8 bits */
39 #define LNET_PROC_VER_BITS              \
40         clamp_t(int, LNET_LOFFT_BITS / 4, 8, 16)
41
42 #define LNET_PROC_HASH_BITS     LNET_PEER_HASH_BITS
43 /*
44  * bits for peer hash offset
45  * NB: we don't use the highest bit of *ppos because it's signed
46  */
47 #define LNET_PROC_HOFF_BITS     (LNET_LOFFT_BITS -       \
48                                  LNET_PROC_CPT_BITS -    \
49                                  LNET_PROC_VER_BITS -    \
50                                  LNET_PROC_HASH_BITS - 1)
51 /* bits for hash index + position */
52 #define LNET_PROC_HPOS_BITS     (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
53 /* bits for peer hash table + hash version */
54 #define LNET_PROC_VPOS_BITS     (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
55
56 #define LNET_PROC_CPT_MASK      ((1ULL << LNET_PROC_CPT_BITS) - 1)
57 #define LNET_PROC_VER_MASK      ((1ULL << LNET_PROC_VER_BITS) - 1)
58 #define LNET_PROC_HASH_MASK     ((1ULL << LNET_PROC_HASH_BITS) - 1)
59 #define LNET_PROC_HOFF_MASK     ((1ULL << LNET_PROC_HOFF_BITS) - 1)
60
61 #define LNET_PROC_CPT_GET(pos)                          \
62         (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
63
64 #define LNET_PROC_VER_GET(pos)                          \
65         (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
66
67 #define LNET_PROC_HASH_GET(pos)                         \
68         (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
69
70 #define LNET_PROC_HOFF_GET(pos)                         \
71         (int)((pos) & LNET_PROC_HOFF_MASK)
72
73 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off)         \
74         (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) |   \
75         ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) |   \
76         ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
77         ((off) & LNET_PROC_HOFF_MASK))
78
79 #define LNET_PROC_VERSION(v)    ((unsigned int)((v) & LNET_PROC_VER_MASK))
80
81 static int __proc_lnet_stats(void *data, int write,
82                              loff_t pos, void __user *buffer, int nob)
83 {
84         int              rc;
85         struct lnet_counters *ctrs;
86         struct lnet_counters_common common;
87         int              len;
88         char tmpstr[256]; /* 7 %u and 4 u64 */
89
90         if (write) {
91                 lnet_counters_reset();
92                 return 0;
93         }
94
95         /* read */
96
97         LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
98         if (ctrs == NULL)
99                 return -ENOMEM;
100
101         rc = lnet_counters_get(ctrs);
102         if (rc)
103                 goto out_no_ctrs;
104
105         common = ctrs->lct_common;
106
107         len = scnprintf(tmpstr, sizeof(tmpstr),
108                         "%u %u %u %u %u %u %u %llu %llu "
109                         "%llu %llu",
110                         common.lcc_msgs_alloc, common.lcc_msgs_max,
111                         common.lcc_errors,
112                         common.lcc_send_count, common.lcc_recv_count,
113                         common.lcc_route_count, common.lcc_drop_count,
114                         common.lcc_send_length, common.lcc_recv_length,
115                         common.lcc_route_length, common.lcc_drop_length);
116
117         if (pos >= len)
118                 rc = 0;
119         else
120                 rc = cfs_trace_copyout_string(buffer, nob,
121                                               tmpstr + pos, "\n");
122 out_no_ctrs:
123         LIBCFS_FREE(ctrs, sizeof(*ctrs));
124         return rc;
125 }
126
127 static int
128 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
129                 size_t *lenp, loff_t *ppos)
130 {
131         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
132                                     __proc_lnet_stats);
133 }
134
135 static int
136 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
137                  size_t *lenp, loff_t *ppos)
138 {
139         const int       tmpsiz = 256;
140         char            *tmpstr;
141         char            *s;
142         int             rc = 0;
143         int             len;
144         int             ver;
145         int             off;
146
147         BUILD_BUG_ON(sizeof(loff_t) < 4);
148
149         off = LNET_PROC_HOFF_GET(*ppos);
150         ver = LNET_PROC_VER_GET(*ppos);
151
152         LASSERT(!write);
153
154         if (*lenp == 0)
155                 return 0;
156
157         LIBCFS_ALLOC(tmpstr, tmpsiz);
158         if (tmpstr == NULL)
159                 return -ENOMEM;
160
161         s = tmpstr; /* points to current position in tmpstr[] */
162
163         if (*ppos == 0) {
164                 s += scnprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
165                                the_lnet.ln_routing ? "enabled" : "disabled");
166                 LASSERT(tmpstr + tmpsiz - s > 0);
167
168                 s += scnprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
169                                "net", "hops", "priority", "state", "router");
170                 LASSERT(tmpstr + tmpsiz - s > 0);
171
172                 lnet_net_lock(0);
173                 ver = (unsigned int)the_lnet.ln_remote_nets_version;
174                 lnet_net_unlock(0);
175                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
176         } else {
177                 struct list_head        *n;
178                 struct list_head        *r;
179                 struct lnet_route               *route = NULL;
180                 struct lnet_remotenet   *rnet  = NULL;
181                 int                     skip  = off - 1;
182                 struct list_head        *rn_list;
183                 int                     i;
184
185                 lnet_net_lock(0);
186
187                 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
188                         lnet_net_unlock(0);
189                         LIBCFS_FREE(tmpstr, tmpsiz);
190                         return -ESTALE;
191                 }
192
193                 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
194                      i++) {
195                         rn_list = &the_lnet.ln_remote_nets_hash[i];
196
197                         n = rn_list->next;
198
199                         while (n != rn_list && route == NULL) {
200                                 rnet = list_entry(n, struct lnet_remotenet,
201                                                   lrn_list);
202
203                                 r = rnet->lrn_routes.next;
204
205                                 while (r != &rnet->lrn_routes) {
206                                         struct lnet_route *re =
207                                                 list_entry(r, struct lnet_route,
208                                                            lr_list);
209                                         if (skip == 0) {
210                                                 route = re;
211                                                 break;
212                                         }
213
214                                         skip--;
215                                         r = r->next;
216                                 }
217
218                                 n = n->next;
219                         }
220                 }
221
222                 if (route != NULL) {
223                         __u32 net = rnet->lrn_net;
224                         __u32 hops = route->lr_hops;
225                         unsigned int priority = route->lr_priority;
226                         int alive = lnet_is_route_alive(route);
227
228                         s += scnprintf(s, tmpstr + tmpsiz - s,
229                                        "%-8s %4d %8u %7s %s\n",
230                                        libcfs_net2str(net), hops,
231                                        priority,
232                                        alive ? "up" : "down",
233                                        libcfs_nid2str(route->lr_nid));
234                         LASSERT(tmpstr + tmpsiz - s > 0);
235                 }
236
237                 lnet_net_unlock(0);
238         }
239
240         len = s - tmpstr;     /* how many bytes was written */
241
242         if (len > *lenp) {    /* linux-supplied buffer is too small */
243                 rc = -EINVAL;
244         } else if (len > 0) { /* wrote something */
245                 if (copy_to_user(buffer, tmpstr, len))
246                         rc = -EFAULT;
247                 else {
248                         off += 1;
249                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
250                 }
251         }
252
253         LIBCFS_FREE(tmpstr, tmpsiz);
254
255         if (rc == 0)
256                 *lenp = len;
257
258         return rc;
259 }
260
261 static int
262 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
263                   size_t *lenp, loff_t *ppos)
264 {
265         int        rc = 0;
266         char      *tmpstr;
267         char      *s;
268         const int  tmpsiz = 256;
269         int        len;
270         int        ver;
271         int        off;
272
273         off = LNET_PROC_HOFF_GET(*ppos);
274         ver = LNET_PROC_VER_GET(*ppos);
275
276         LASSERT(!write);
277
278         if (*lenp == 0)
279                 return 0;
280
281         LIBCFS_ALLOC(tmpstr, tmpsiz);
282         if (tmpstr == NULL)
283                 return -ENOMEM;
284
285         s = tmpstr; /* points to current position in tmpstr[] */
286
287         if (*ppos == 0) {
288                 s += scnprintf(s, tmpstr + tmpsiz - s,
289                                "%-4s %7s %5s %s\n",
290                                "ref", "rtr_ref", "alive", "router");
291                 LASSERT(tmpstr + tmpsiz - s > 0);
292
293                 lnet_net_lock(0);
294                 ver = (unsigned int)the_lnet.ln_routers_version;
295                 lnet_net_unlock(0);
296                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
297         } else {
298                 struct list_head *r;
299                 struct lnet_peer *peer = NULL;
300                 int               skip = off - 1;
301
302                 lnet_net_lock(0);
303
304                 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
305                         lnet_net_unlock(0);
306
307                         LIBCFS_FREE(tmpstr, tmpsiz);
308                         return -ESTALE;
309                 }
310
311                 r = the_lnet.ln_routers.next;
312
313                 while (r != &the_lnet.ln_routers) {
314                         struct lnet_peer *lp =
315                           list_entry(r, struct lnet_peer,
316                                      lp_rtr_list);
317
318                         if (skip == 0) {
319                                 peer = lp;
320                                 break;
321                         }
322
323                         skip--;
324                         r = r->next;
325                 }
326
327                 if (peer != NULL) {
328                         lnet_nid_t nid = peer->lp_primary_nid;
329                         int nrefs     = atomic_read(&peer->lp_refcount);
330                         int nrtrrefs  = peer->lp_rtr_refcount;
331                         int alive     = lnet_is_gateway_alive(peer);
332
333                         s += scnprintf(s, tmpstr + tmpsiz - s,
334                                        "%-4d %7d %5s %s\n",
335                                        nrefs, nrtrrefs,
336                                        alive ? "up" : "down",
337                                        libcfs_nid2str(nid));
338                 }
339
340                 lnet_net_unlock(0);
341         }
342
343         len = s - tmpstr;     /* how many bytes was written */
344
345         if (len > *lenp) {    /* linux-supplied buffer is too small */
346                 rc = -EINVAL;
347         } else if (len > 0) { /* wrote something */
348                 if (copy_to_user(buffer, tmpstr, len))
349                         rc = -EFAULT;
350                 else {
351                         off += 1;
352                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
353                 }
354         }
355
356         LIBCFS_FREE(tmpstr, tmpsiz);
357
358         if (rc == 0)
359                 *lenp = len;
360
361         return rc;
362 }
363
364 /* TODO: there should be no direct access to ptable. We should add a set
365  * of APIs that give access to the ptable and its members */
366 static int
367 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
368                 size_t *lenp, loff_t *ppos)
369 {
370         const int               tmpsiz  = 256;
371         struct lnet_peer_table  *ptable;
372         char                    *tmpstr = NULL;
373         char                    *s;
374         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
375         int                     ver  = LNET_PROC_VER_GET(*ppos);
376         int                     hash = LNET_PROC_HASH_GET(*ppos);
377         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
378         int                     rc = 0;
379         int                     len;
380
381         if (write) {
382                 int i;
383                 struct lnet_peer_ni *peer;
384
385                 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
386                         lnet_net_lock(i);
387                         for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
388                                 list_for_each_entry(peer,
389                                                     &ptable->pt_hash[hash],
390                                                     lpni_hashlist) {
391                                         peer->lpni_mintxcredits =
392                                                 peer->lpni_txcredits;
393                                         peer->lpni_minrtrcredits =
394                                                 peer->lpni_rtrcredits;
395                                 }
396                         }
397                         lnet_net_unlock(i);
398                 }
399                 *ppos += *lenp;
400                 return 0;
401         }
402
403         if (*lenp == 0)
404                 return 0;
405
406         BUILD_BUG_ON(LNET_PROC_HASH_BITS < LNET_PEER_HASH_BITS);
407
408         if (cpt >= LNET_CPT_NUMBER) {
409                 *lenp = 0;
410                 return 0;
411         }
412
413         LIBCFS_ALLOC(tmpstr, tmpsiz);
414         if (tmpstr == NULL)
415                 return -ENOMEM;
416
417         s = tmpstr; /* points to current position in tmpstr[] */
418
419         if (*ppos == 0) {
420                 s += scnprintf(s, tmpstr + tmpsiz - s,
421                                "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
422                                "nid", "refs", "state", "last", "max",
423                                "rtr", "min", "tx", "min", "queue");
424                 LASSERT(tmpstr + tmpsiz - s > 0);
425
426                 hoff++;
427         } else {
428                 struct lnet_peer_ni     *peer;
429                 struct list_head        *p;
430                 int                     skip;
431
432  again:
433                 p = NULL;
434                 peer = NULL;
435                 skip = hoff - 1;
436
437                 lnet_net_lock(cpt);
438                 ptable = the_lnet.ln_peer_tables[cpt];
439                 if (hoff == 1)
440                         ver = LNET_PROC_VERSION(ptable->pt_version);
441
442                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
443                         lnet_net_unlock(cpt);
444                         LIBCFS_FREE(tmpstr, tmpsiz);
445                         return -ESTALE;
446                 }
447
448                 while (hash < LNET_PEER_HASH_SIZE) {
449                         if (p == NULL)
450                                 p = ptable->pt_hash[hash].next;
451
452                         while (p != &ptable->pt_hash[hash]) {
453                                 struct lnet_peer_ni *lp =
454                                   list_entry(p, struct lnet_peer_ni,
455                                              lpni_hashlist);
456                                 if (skip == 0) {
457                                         peer = lp;
458
459                                         /* minor optimization: start from idx+1
460                                          * on next iteration if we've just
461                                          * drained lpni_hashlist */
462                                         if (lp->lpni_hashlist.next ==
463                                             &ptable->pt_hash[hash]) {
464                                                 hoff = 1;
465                                                 hash++;
466                                         } else {
467                                                 hoff++;
468                                         }
469
470                                         break;
471                                 }
472
473                                 skip--;
474                                 p = lp->lpni_hashlist.next;
475                         }
476
477                         if (peer != NULL)
478                                 break;
479
480                         p = NULL;
481                         hoff = 1;
482                         hash++;
483                 }
484
485                 if (peer != NULL) {
486                         lnet_nid_t nid = peer->lpni_nid;
487                         int nrefs = atomic_read(&peer->lpni_refcount);
488                         time64_t lastalive = -1;
489                         char *aliveness = "NA";
490                         int maxcr = (peer->lpni_net) ?
491                           peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
492                         int txcr = peer->lpni_txcredits;
493                         int mintxcr = peer->lpni_mintxcredits;
494                         int rtrcr = peer->lpni_rtrcredits;
495                         int minrtrcr = peer->lpni_minrtrcredits;
496                         int txqnob = peer->lpni_txqnob;
497
498                         if (lnet_isrouter(peer) ||
499                             lnet_peer_aliveness_enabled(peer))
500                                 aliveness = lnet_is_peer_ni_alive(peer) ?
501                                         "up" : "down";
502
503                         lnet_net_unlock(cpt);
504
505                         s += scnprintf(s, tmpstr + tmpsiz - s,
506                                        "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
507                                        libcfs_nid2str(nid), nrefs, aliveness,
508                                        lastalive, maxcr, rtrcr, minrtrcr, txcr,
509                                        mintxcr, txqnob);
510                         LASSERT(tmpstr + tmpsiz - s > 0);
511
512                 } else { /* peer is NULL */
513                         lnet_net_unlock(cpt);
514                 }
515
516                 if (hash == LNET_PEER_HASH_SIZE) {
517                         cpt++;
518                         hash = 0;
519                         hoff = 1;
520                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
521                                 goto again;
522                 }
523         }
524
525         len = s - tmpstr;     /* how many bytes was written */
526
527         if (len > *lenp) {    /* linux-supplied buffer is too small */
528                 rc = -EINVAL;
529         } else if (len > 0) { /* wrote something */
530                 if (copy_to_user(buffer, tmpstr, len))
531                         rc = -EFAULT;
532                 else
533                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
534         }
535
536         LIBCFS_FREE(tmpstr, tmpsiz);
537
538         if (rc == 0)
539                 *lenp = len;
540
541         return rc;
542 }
543
544 static int __proc_lnet_buffers(void *data, int write,
545                                loff_t pos, void __user *buffer, int nob)
546 {
547         char            *s;
548         char            *tmpstr;
549         int             tmpsiz;
550         int             idx;
551         int             len;
552         int             rc;
553         int             i;
554
555         LASSERT(!write);
556
557         /* (4 %d) * 4 * LNET_CPT_NUMBER */
558         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
559         LIBCFS_ALLOC(tmpstr, tmpsiz);
560         if (tmpstr == NULL)
561                 return -ENOMEM;
562
563         s = tmpstr; /* points to current position in tmpstr[] */
564
565         s += scnprintf(s, tmpstr + tmpsiz - s,
566                        "%5s %5s %7s %7s\n",
567                        "pages", "count", "credits", "min");
568         LASSERT(tmpstr + tmpsiz - s > 0);
569
570         if (the_lnet.ln_rtrpools == NULL)
571                 goto out; /* I'm not a router */
572
573         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
574                 struct lnet_rtrbufpool *rbp;
575
576                 lnet_net_lock(LNET_LOCK_EX);
577                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
578                         s += scnprintf(s, tmpstr + tmpsiz - s,
579                                        "%5d %5d %7d %7d\n",
580                                        rbp[idx].rbp_npages,
581                                        rbp[idx].rbp_nbuffers,
582                                        rbp[idx].rbp_credits,
583                                        rbp[idx].rbp_mincredits);
584                         LASSERT(tmpstr + tmpsiz - s > 0);
585                 }
586                 lnet_net_unlock(LNET_LOCK_EX);
587         }
588
589  out:
590         len = s - tmpstr;
591
592         if (pos >= min_t(int, len, strlen(tmpstr)))
593                 rc = 0;
594         else
595                 rc = cfs_trace_copyout_string(buffer, nob,
596                                               tmpstr + pos, NULL);
597
598         LIBCFS_FREE(tmpstr, tmpsiz);
599         return rc;
600 }
601
602 static int
603 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
604                   size_t *lenp, loff_t *ppos)
605 {
606         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
607                                     __proc_lnet_buffers);
608 }
609
610 static int
611 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
612               size_t *lenp, loff_t *ppos)
613 {
614         int     tmpsiz = 128 * LNET_CPT_NUMBER;
615         int     rc = 0;
616         char    *tmpstr;
617         char    *s;
618         int     len;
619
620         if (*lenp == 0)
621                 return 0;
622
623         if (write) {
624                 /* Just reset the min stat. */
625                 struct lnet_ni  *ni;
626                 struct lnet_net *net;
627
628                 lnet_net_lock(0);
629
630                 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
631                         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
632                                 struct lnet_tx_queue *tq;
633                                 int i;
634                                 int j;
635
636                                 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
637                                         for (j = 0; ni->ni_cpts != NULL &&
638                                              j < ni->ni_ncpts; j++) {
639                                                 if (i == ni->ni_cpts[j])
640                                                         break;
641                                         }
642
643                                         if (j == ni->ni_ncpts)
644                                                 continue;
645
646                                         if (i != 0)
647                                                 lnet_net_lock(i);
648                                         tq->tq_credits_min = tq->tq_credits;
649                                         if (i != 0)
650                                                 lnet_net_unlock(i);
651                                 }
652                         }
653                 }
654                 lnet_net_unlock(0);
655                 *ppos += *lenp;
656                 return 0;
657         }
658
659         LIBCFS_ALLOC(tmpstr, tmpsiz);
660         if (tmpstr == NULL)
661                 return -ENOMEM;
662
663         s = tmpstr; /* points to current position in tmpstr[] */
664
665         if (*ppos == 0) {
666                 s += scnprintf(s, tmpstr + tmpsiz - s,
667                                "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
668                                "nid", "status", "alive", "refs", "peer",
669                                "rtr", "max", "tx", "min");
670                 LASSERT (tmpstr + tmpsiz - s > 0);
671         } else {
672                 struct lnet_ni *ni   = NULL;
673                 int skip = *ppos - 1;
674
675                 lnet_net_lock(0);
676
677                 ni = lnet_get_ni_idx_locked(skip);
678
679                 if (ni != NULL) {
680                         struct lnet_tx_queue *tq;
681                         char *stat;
682                         time64_t now = ktime_get_real_seconds();
683                         time64_t last_alive = -1;
684                         int i;
685                         int j;
686
687                         if (the_lnet.ln_routing)
688                                 last_alive = now - ni->ni_net->net_last_alive;
689
690                         lnet_ni_lock(ni);
691                         LASSERT(ni->ni_status != NULL);
692                         stat = (ni->ni_status->ns_status ==
693                                 LNET_NI_STATUS_UP) ? "up" : "down";
694                         lnet_ni_unlock(ni);
695
696                         /* @lo forever alive */
697                         if (ni->ni_net->net_lnd->lnd_type == LOLND) {
698                                 last_alive = 0;
699                                 stat = "up";
700                         }
701
702                         /* we actually output credits information for
703                          * TX queue of each partition */
704                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
705                                 for (j = 0; ni->ni_cpts != NULL &&
706                                      j < ni->ni_ncpts; j++) {
707                                         if (i == ni->ni_cpts[j])
708                                                 break;
709                                 }
710
711                                 if (j == ni->ni_ncpts)
712                                         continue;
713
714                                 if (i != 0)
715                                         lnet_net_lock(i);
716
717                                 s += scnprintf(s, tmpstr + tmpsiz - s,
718                                        "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
719                                        libcfs_nid2str(ni->ni_nid), stat,
720                                        last_alive, *ni->ni_refs[i],
721                                        ni->ni_net->net_tunables.lct_peer_tx_credits,
722                                        ni->ni_net->net_tunables.lct_peer_rtr_credits,
723                                        tq->tq_credits_max,
724                                        tq->tq_credits, tq->tq_credits_min);
725                                 if (i != 0)
726                                         lnet_net_unlock(i);
727                         }
728                         LASSERT(tmpstr + tmpsiz - s > 0);
729                 }
730
731                 lnet_net_unlock(0);
732         }
733
734         len = s - tmpstr;     /* how many bytes was written */
735
736         if (len > *lenp) {    /* linux-supplied buffer is too small */
737                 rc = -EINVAL;
738         } else if (len > 0) { /* wrote something */
739                 if (copy_to_user(buffer, tmpstr, len))
740                         rc = -EFAULT;
741                 else
742                         *ppos += 1;
743         }
744
745         LIBCFS_FREE(tmpstr, tmpsiz);
746
747         if (rc == 0)
748                 *lenp = len;
749
750         return rc;
751 }
752
753 struct lnet_portal_rotors {
754         int             pr_value;
755         const char      *pr_name;
756         const char      *pr_desc;
757 };
758
759 static struct lnet_portal_rotors        portal_rotors[] = {
760         {
761                 .pr_value = LNET_PTL_ROTOR_OFF,
762                 .pr_name  = "OFF",
763                 .pr_desc  = "Turn off message rotor for wildcard portals"
764         },
765         {
766                 .pr_value = LNET_PTL_ROTOR_ON,
767                 .pr_name  = "ON",
768                 .pr_desc  = "round-robin dispatch all PUT messages for "
769                             "wildcard portals"
770         },
771         {
772                 .pr_value = LNET_PTL_ROTOR_RR_RT,
773                 .pr_name  = "RR_RT",
774                 .pr_desc  = "round-robin dispatch routed PUT message for "
775                             "wildcard portals"
776         },
777         {
778                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
779                 .pr_name  = "HASH_RT",
780                 .pr_desc  = "dispatch routed PUT message by hashing source "
781                             "NID for wildcard portals"
782         },
783         {
784                 .pr_value = -1,
785                 .pr_name  = NULL,
786                 .pr_desc  = NULL
787         },
788 };
789
790 static int __proc_lnet_portal_rotor(void *data, int write,
791                                     loff_t pos, void __user *buffer, int nob)
792 {
793         const int       buf_len = 128;
794         char            *buf;
795         char            *tmp;
796         int             rc;
797         int             i;
798
799         LIBCFS_ALLOC(buf, buf_len);
800         if (buf == NULL)
801                 return -ENOMEM;
802
803         if (!write) {
804                 lnet_res_lock(0);
805
806                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
807                         if (portal_rotors[i].pr_value == portal_rotor)
808                                 break;
809                 }
810
811                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
812                 lnet_res_unlock(0);
813
814                 rc = scnprintf(buf, buf_len,
815                                "{\n\tportals: all\n"
816                                "\trotor: %s\n\tdescription: %s\n}",
817                                portal_rotors[i].pr_name,
818                                portal_rotors[i].pr_desc);
819
820                 if (pos >= min_t(int, rc, buf_len)) {
821                         rc = 0;
822                 } else {
823                         rc = cfs_trace_copyout_string(buffer, nob,
824                                         buf + pos, "\n");
825                 }
826                 goto out;
827         }
828
829         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
830         if (rc < 0)
831                 goto out;
832
833         tmp = strim(buf);
834
835         rc = -EINVAL;
836         lnet_res_lock(0);
837         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
838                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
839                                 strlen(portal_rotors[i].pr_name)) == 0) {
840                         portal_rotor = portal_rotors[i].pr_value;
841                         rc = 0;
842                         break;
843                 }
844         }
845         lnet_res_unlock(0);
846 out:
847         LIBCFS_FREE(buf, buf_len);
848         return rc;
849 }
850
851 static int
852 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
853                        size_t *lenp, loff_t *ppos)
854 {
855         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
856                                     __proc_lnet_portal_rotor);
857 }
858
859
860 static struct ctl_table lnet_table[] = {
861         /*
862          * NB No .strategy entries have been provided since sysctl(8) prefers
863          * to go via /proc for portability.
864          */
865         {
866                 .procname       = "stats",
867                 .mode           = 0644,
868                 .proc_handler   = &proc_lnet_stats,
869         },
870         {
871                 .procname       = "routes",
872                 .mode           = 0444,
873                 .proc_handler   = &proc_lnet_routes,
874         },
875         {
876                 .procname       = "routers",
877                 .mode           = 0444,
878                 .proc_handler   = &proc_lnet_routers,
879         },
880         {
881                 .procname       = "peers",
882                 .mode           = 0644,
883                 .proc_handler   = &proc_lnet_peers,
884         },
885         {
886                 .procname       = "buffers",
887                 .mode           = 0444,
888                 .proc_handler   = &proc_lnet_buffers,
889         },
890         {
891                 .procname       = "nis",
892                 .mode           = 0644,
893                 .proc_handler   = &proc_lnet_nis,
894         },
895         {
896                 .procname       = "portal_rotor",
897                 .mode           = 0644,
898                 .proc_handler   = &proc_lnet_portal_rotor,
899         },
900         {
901                 .procname       = "lnet_lnd_timeout",
902                 .data           = &lnet_lnd_timeout,
903                 .maxlen         = sizeof(lnet_lnd_timeout),
904                 .mode           = 0444,
905                 .proc_handler   = &proc_dointvec,
906         },
907         { .procname = NULL }
908 };
909
910 void lnet_router_debugfs_init(void)
911 {
912         lnet_insert_debugfs(lnet_table);
913 }
914
915 void lnet_router_debugfs_fini(void)
916 {
917         lnet_remove_debugfs(lnet_table);
918 }