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