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