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