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