Whamcloud - gitweb
LU-11299 lnet: Cleanup rcd
[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 = snprintf(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 >= min_t(int, len, strlen(tmpstr)))
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         CLASSERT(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 += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
169                               the_lnet.ln_routing ? "enabled" : "disabled");
170                 LASSERT(tmpstr + tmpsiz - s > 0);
171
172                 s += snprintf(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 += snprintf(s, tmpstr + tmpsiz - s,
233                                       "%-8s %4d %8u %7s %s\n",
234                                       libcfs_net2str(net), hops,
235                                       priority,
236                                       alive ? "up" : "down",
237                                       /* TODO: replace with actual nid */
238                                       libcfs_nid2str(LNET_NID_ANY));
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 += snprintf(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 += snprintf(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         CLASSERT(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 += snprintf(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 += snprintf(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 += snprintf(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 += snprintf(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 += snprintf(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_last_alive;
694
695                         /* @lo forever alive */
696                         if (ni->ni_net->net_lnd->lnd_type == LOLND)
697                                 last_alive = 0;
698
699                         lnet_ni_lock(ni);
700                         LASSERT(ni->ni_status != NULL);
701                         stat = (ni->ni_status->ns_status ==
702                                 LNET_NI_STATUS_UP) ? "up" : "down";
703                         lnet_ni_unlock(ni);
704
705                         /* we actually output credits information for
706                          * TX queue of each partition */
707                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
708                                 for (j = 0; ni->ni_cpts != NULL &&
709                                      j < ni->ni_ncpts; j++) {
710                                         if (i == ni->ni_cpts[j])
711                                                 break;
712                                 }
713
714                                 if (j == ni->ni_ncpts)
715                                         continue;
716
717                                 if (i != 0)
718                                         lnet_net_lock(i);
719
720                                 s += snprintf(s, tmpstr + tmpsiz - s,
721                                       "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
722                                       libcfs_nid2str(ni->ni_nid), stat,
723                                       last_alive, *ni->ni_refs[i],
724                                       ni->ni_net->net_tunables.lct_peer_tx_credits,
725                                       ni->ni_net->net_tunables.lct_peer_rtr_credits,
726                                       tq->tq_credits_max,
727                                       tq->tq_credits, tq->tq_credits_min);
728                                 if (i != 0)
729                                         lnet_net_unlock(i);
730                         }
731                         LASSERT(tmpstr + tmpsiz - s > 0);
732                 }
733
734                 lnet_net_unlock(0);
735         }
736
737         len = s - tmpstr;     /* how many bytes was written */
738
739         if (len > *lenp) {    /* linux-supplied buffer is too small */
740                 rc = -EINVAL;
741         } else if (len > 0) { /* wrote something */
742                 if (copy_to_user(buffer, tmpstr, len))
743                         rc = -EFAULT;
744                 else
745                         *ppos += 1;
746         }
747
748         LIBCFS_FREE(tmpstr, tmpsiz);
749
750         if (rc == 0)
751                 *lenp = len;
752
753         return rc;
754 }
755
756 struct lnet_portal_rotors {
757         int             pr_value;
758         const char      *pr_name;
759         const char      *pr_desc;
760 };
761
762 static struct lnet_portal_rotors        portal_rotors[] = {
763         {
764                 .pr_value = LNET_PTL_ROTOR_OFF,
765                 .pr_name  = "OFF",
766                 .pr_desc  = "Turn off message rotor for wildcard portals"
767         },
768         {
769                 .pr_value = LNET_PTL_ROTOR_ON,
770                 .pr_name  = "ON",
771                 .pr_desc  = "round-robin dispatch all PUT messages for "
772                             "wildcard portals"
773         },
774         {
775                 .pr_value = LNET_PTL_ROTOR_RR_RT,
776                 .pr_name  = "RR_RT",
777                 .pr_desc  = "round-robin dispatch routed PUT message for "
778                             "wildcard portals"
779         },
780         {
781                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
782                 .pr_name  = "HASH_RT",
783                 .pr_desc  = "dispatch routed PUT message by hashing source "
784                             "NID for wildcard portals"
785         },
786         {
787                 .pr_value = -1,
788                 .pr_name  = NULL,
789                 .pr_desc  = NULL
790         },
791 };
792
793 static int __proc_lnet_portal_rotor(void *data, int write,
794                                     loff_t pos, void __user *buffer, int nob)
795 {
796         const int       buf_len = 128;
797         char            *buf;
798         char            *tmp;
799         int             rc;
800         int             i;
801
802         LIBCFS_ALLOC(buf, buf_len);
803         if (buf == NULL)
804                 return -ENOMEM;
805
806         if (!write) {
807                 lnet_res_lock(0);
808
809                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
810                         if (portal_rotors[i].pr_value == portal_rotor)
811                                 break;
812                 }
813
814                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
815                 lnet_res_unlock(0);
816
817                 rc = snprintf(buf, buf_len,
818                               "{\n\tportals: all\n"
819                               "\trotor: %s\n\tdescription: %s\n}",
820                               portal_rotors[i].pr_name,
821                               portal_rotors[i].pr_desc);
822
823                 if (pos >= min_t(int, rc, buf_len)) {
824                         rc = 0;
825                 } else {
826                         rc = cfs_trace_copyout_string(buffer, nob,
827                                         buf + pos, "\n");
828                 }
829                 goto out;
830         }
831
832         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
833         if (rc < 0)
834                 goto out;
835
836         tmp = strim(buf);
837
838         rc = -EINVAL;
839         lnet_res_lock(0);
840         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
841                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
842                                 strlen(portal_rotors[i].pr_name)) == 0) {
843                         portal_rotor = portal_rotors[i].pr_value;
844                         rc = 0;
845                         break;
846                 }
847         }
848         lnet_res_unlock(0);
849 out:
850         LIBCFS_FREE(buf, buf_len);
851         return rc;
852 }
853
854 static int
855 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
856                        size_t *lenp, loff_t *ppos)
857 {
858         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
859                                     __proc_lnet_portal_rotor);
860 }
861
862
863 static struct ctl_table lnet_table[] = {
864         /*
865          * NB No .strategy entries have been provided since sysctl(8) prefers
866          * to go via /proc for portability.
867          */
868         {
869                 INIT_CTL_NAME
870                 .procname       = "stats",
871                 .mode           = 0644,
872                 .proc_handler   = &proc_lnet_stats,
873         },
874         {
875                 INIT_CTL_NAME
876                 .procname       = "routes",
877                 .mode           = 0444,
878                 .proc_handler   = &proc_lnet_routes,
879         },
880         {
881                 INIT_CTL_NAME
882                 .procname       = "routers",
883                 .mode           = 0444,
884                 .proc_handler   = &proc_lnet_routers,
885         },
886         {
887                 INIT_CTL_NAME
888                 .procname       = "peers",
889                 .mode           = 0644,
890                 .proc_handler   = &proc_lnet_peers,
891         },
892         {
893                 INIT_CTL_NAME
894                 .procname       = "buffers",
895                 .mode           = 0444,
896                 .proc_handler   = &proc_lnet_buffers,
897         },
898         {
899                 INIT_CTL_NAME
900                 .procname       = "nis",
901                 .mode           = 0644,
902                 .proc_handler   = &proc_lnet_nis,
903         },
904         {
905                 INIT_CTL_NAME
906                 .procname       = "portal_rotor",
907                 .mode           = 0644,
908                 .proc_handler   = &proc_lnet_portal_rotor,
909         },
910         { .procname = NULL }
911 };
912
913 void lnet_router_debugfs_init(void)
914 {
915         lnet_insert_debugfs(lnet_table);
916 }
917
918 void lnet_router_debugfs_fini(void)
919 {
920         lnet_remove_debugfs(lnet_table);
921 }