Whamcloud - gitweb
LU-11300 lnet: peer aliveness
[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 = lnet_is_peer_ni_alive(peer) ?
540                                         "up" : "down";
541
542                         if (lnet_peer_aliveness_enabled(peer)) {
543                                 time64_t now = ktime_get_seconds();
544
545                                 lastalive = now - peer->lpni_last_alive;
546
547                                 /* No need to mess up peers contents with
548                                  * arbitrarily long integers - it suffices to
549                                  * know that lastalive is more than 10000s old
550                                  */
551                                 if (lastalive >= 10000)
552                                         lastalive = 9999;
553                         }
554
555                         lnet_net_unlock(cpt);
556
557                         s += snprintf(s, tmpstr + tmpsiz - s,
558                                       "%-24s %4d %5s %5lld %5d %5d %5d %5d %5d %d\n",
559                                       libcfs_nid2str(nid), nrefs, aliveness,
560                                       lastalive, maxcr, rtrcr, minrtrcr, txcr,
561                                       mintxcr, txqnob);
562                         LASSERT(tmpstr + tmpsiz - s > 0);
563
564                 } else { /* peer is NULL */
565                         lnet_net_unlock(cpt);
566                 }
567
568                 if (hash == LNET_PEER_HASH_SIZE) {
569                         cpt++;
570                         hash = 0;
571                         hoff = 1;
572                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
573                                 goto again;
574                 }
575         }
576
577         len = s - tmpstr;     /* how many bytes was written */
578
579         if (len > *lenp) {    /* linux-supplied buffer is too small */
580                 rc = -EINVAL;
581         } else if (len > 0) { /* wrote something */
582                 if (copy_to_user(buffer, tmpstr, len))
583                         rc = -EFAULT;
584                 else
585                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
586         }
587
588         LIBCFS_FREE(tmpstr, tmpsiz);
589
590         if (rc == 0)
591                 *lenp = len;
592
593         return rc;
594 }
595
596 static int __proc_lnet_buffers(void *data, int write,
597                                loff_t pos, void __user *buffer, int nob)
598 {
599         char            *s;
600         char            *tmpstr;
601         int             tmpsiz;
602         int             idx;
603         int             len;
604         int             rc;
605         int             i;
606
607         LASSERT(!write);
608
609         /* (4 %d) * 4 * LNET_CPT_NUMBER */
610         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
611         LIBCFS_ALLOC(tmpstr, tmpsiz);
612         if (tmpstr == NULL)
613                 return -ENOMEM;
614
615         s = tmpstr; /* points to current position in tmpstr[] */
616
617         s += snprintf(s, tmpstr + tmpsiz - s,
618                       "%5s %5s %7s %7s\n",
619                       "pages", "count", "credits", "min");
620         LASSERT(tmpstr + tmpsiz - s > 0);
621
622         if (the_lnet.ln_rtrpools == NULL)
623                 goto out; /* I'm not a router */
624
625         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
626                 struct lnet_rtrbufpool *rbp;
627
628                 lnet_net_lock(LNET_LOCK_EX);
629                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
630                         s += snprintf(s, tmpstr + tmpsiz - s,
631                                       "%5d %5d %7d %7d\n",
632                                       rbp[idx].rbp_npages,
633                                       rbp[idx].rbp_nbuffers,
634                                       rbp[idx].rbp_credits,
635                                       rbp[idx].rbp_mincredits);
636                         LASSERT(tmpstr + tmpsiz - s > 0);
637                 }
638                 lnet_net_unlock(LNET_LOCK_EX);
639         }
640
641  out:
642         len = s - tmpstr;
643
644         if (pos >= min_t(int, len, strlen(tmpstr)))
645                 rc = 0;
646         else
647                 rc = cfs_trace_copyout_string(buffer, nob,
648                                               tmpstr + pos, NULL);
649
650         LIBCFS_FREE(tmpstr, tmpsiz);
651         return rc;
652 }
653
654 static int
655 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
656                   size_t *lenp, loff_t *ppos)
657 {
658         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
659                                     __proc_lnet_buffers);
660 }
661
662 static int
663 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
664               size_t *lenp, loff_t *ppos)
665 {
666         int     tmpsiz = 128 * LNET_CPT_NUMBER;
667         int     rc = 0;
668         char    *tmpstr;
669         char    *s;
670         int     len;
671
672         if (*lenp == 0)
673                 return 0;
674
675         if (write) {
676                 /* Just reset the min stat. */
677                 struct lnet_ni  *ni;
678                 struct lnet_net *net;
679
680                 lnet_net_lock(0);
681
682                 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
683                         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
684                                 struct lnet_tx_queue *tq;
685                                 int i;
686                                 int j;
687
688                                 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
689                                         for (j = 0; ni->ni_cpts != NULL &&
690                                              j < ni->ni_ncpts; j++) {
691                                                 if (i == ni->ni_cpts[j])
692                                                         break;
693                                         }
694
695                                         if (j == ni->ni_ncpts)
696                                                 continue;
697
698                                         if (i != 0)
699                                                 lnet_net_lock(i);
700                                         tq->tq_credits_min = tq->tq_credits;
701                                         if (i != 0)
702                                                 lnet_net_unlock(i);
703                                 }
704                         }
705                 }
706                 lnet_net_unlock(0);
707                 *ppos += *lenp;
708                 return 0;
709         }
710
711         LIBCFS_ALLOC(tmpstr, tmpsiz);
712         if (tmpstr == NULL)
713                 return -ENOMEM;
714
715         s = tmpstr; /* points to current position in tmpstr[] */
716
717         if (*ppos == 0) {
718                 s += snprintf(s, tmpstr + tmpsiz - s,
719                               "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
720                               "nid", "status", "alive", "refs", "peer",
721                               "rtr", "max", "tx", "min");
722                 LASSERT (tmpstr + tmpsiz - s > 0);
723         } else {
724                 struct lnet_ni *ni   = NULL;
725                 int skip = *ppos - 1;
726
727                 lnet_net_lock(0);
728
729                 ni = lnet_get_ni_idx_locked(skip);
730
731                 if (ni != NULL) {
732                         struct lnet_tx_queue *tq;
733                         char *stat;
734                         time64_t now = ktime_get_real_seconds();
735                         time64_t last_alive = -1;
736                         int i;
737                         int j;
738
739                         if (the_lnet.ln_routing)
740                                 last_alive = now - ni->ni_last_alive;
741
742                         /* @lo forever alive */
743                         if (ni->ni_net->net_lnd->lnd_type == LOLND)
744                                 last_alive = 0;
745
746                         lnet_ni_lock(ni);
747                         LASSERT(ni->ni_status != NULL);
748                         stat = (ni->ni_status->ns_status ==
749                                 LNET_NI_STATUS_UP) ? "up" : "down";
750                         lnet_ni_unlock(ni);
751
752                         /* we actually output credits information for
753                          * TX queue of each partition */
754                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
755                                 for (j = 0; ni->ni_cpts != NULL &&
756                                      j < ni->ni_ncpts; j++) {
757                                         if (i == ni->ni_cpts[j])
758                                                 break;
759                                 }
760
761                                 if (j == ni->ni_ncpts)
762                                         continue;
763
764                                 if (i != 0)
765                                         lnet_net_lock(i);
766
767                                 s += snprintf(s, tmpstr + tmpsiz - s,
768                                       "%-24s %6s %5lld %4d %4d %4d %5d %5d %5d\n",
769                                       libcfs_nid2str(ni->ni_nid), stat,
770                                       last_alive, *ni->ni_refs[i],
771                                       ni->ni_net->net_tunables.lct_peer_tx_credits,
772                                       ni->ni_net->net_tunables.lct_peer_rtr_credits,
773                                       tq->tq_credits_max,
774                                       tq->tq_credits, tq->tq_credits_min);
775                                 if (i != 0)
776                                         lnet_net_unlock(i);
777                         }
778                         LASSERT(tmpstr + tmpsiz - s > 0);
779                 }
780
781                 lnet_net_unlock(0);
782         }
783
784         len = s - tmpstr;     /* how many bytes was written */
785
786         if (len > *lenp) {    /* linux-supplied buffer is too small */
787                 rc = -EINVAL;
788         } else if (len > 0) { /* wrote something */
789                 if (copy_to_user(buffer, tmpstr, len))
790                         rc = -EFAULT;
791                 else
792                         *ppos += 1;
793         }
794
795         LIBCFS_FREE(tmpstr, tmpsiz);
796
797         if (rc == 0)
798                 *lenp = len;
799
800         return rc;
801 }
802
803 struct lnet_portal_rotors {
804         int             pr_value;
805         const char      *pr_name;
806         const char      *pr_desc;
807 };
808
809 static struct lnet_portal_rotors        portal_rotors[] = {
810         {
811                 .pr_value = LNET_PTL_ROTOR_OFF,
812                 .pr_name  = "OFF",
813                 .pr_desc  = "Turn off message rotor for wildcard portals"
814         },
815         {
816                 .pr_value = LNET_PTL_ROTOR_ON,
817                 .pr_name  = "ON",
818                 .pr_desc  = "round-robin dispatch all PUT messages for "
819                             "wildcard portals"
820         },
821         {
822                 .pr_value = LNET_PTL_ROTOR_RR_RT,
823                 .pr_name  = "RR_RT",
824                 .pr_desc  = "round-robin dispatch routed PUT message for "
825                             "wildcard portals"
826         },
827         {
828                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
829                 .pr_name  = "HASH_RT",
830                 .pr_desc  = "dispatch routed PUT message by hashing source "
831                             "NID for wildcard portals"
832         },
833         {
834                 .pr_value = -1,
835                 .pr_name  = NULL,
836                 .pr_desc  = NULL
837         },
838 };
839
840 static int __proc_lnet_portal_rotor(void *data, int write,
841                                     loff_t pos, void __user *buffer, int nob)
842 {
843         const int       buf_len = 128;
844         char            *buf;
845         char            *tmp;
846         int             rc;
847         int             i;
848
849         LIBCFS_ALLOC(buf, buf_len);
850         if (buf == NULL)
851                 return -ENOMEM;
852
853         if (!write) {
854                 lnet_res_lock(0);
855
856                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
857                         if (portal_rotors[i].pr_value == portal_rotor)
858                                 break;
859                 }
860
861                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
862                 lnet_res_unlock(0);
863
864                 rc = snprintf(buf, buf_len,
865                               "{\n\tportals: all\n"
866                               "\trotor: %s\n\tdescription: %s\n}",
867                               portal_rotors[i].pr_name,
868                               portal_rotors[i].pr_desc);
869
870                 if (pos >= min_t(int, rc, buf_len)) {
871                         rc = 0;
872                 } else {
873                         rc = cfs_trace_copyout_string(buffer, nob,
874                                         buf + pos, "\n");
875                 }
876                 goto out;
877         }
878
879         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
880         if (rc < 0)
881                 goto out;
882
883         tmp = strim(buf);
884
885         rc = -EINVAL;
886         lnet_res_lock(0);
887         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
888                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
889                                 strlen(portal_rotors[i].pr_name)) == 0) {
890                         portal_rotor = portal_rotors[i].pr_value;
891                         rc = 0;
892                         break;
893                 }
894         }
895         lnet_res_unlock(0);
896 out:
897         LIBCFS_FREE(buf, buf_len);
898         return rc;
899 }
900
901 static int
902 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
903                        size_t *lenp, loff_t *ppos)
904 {
905         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
906                                     __proc_lnet_portal_rotor);
907 }
908
909
910 static struct ctl_table lnet_table[] = {
911         /*
912          * NB No .strategy entries have been provided since sysctl(8) prefers
913          * to go via /proc for portability.
914          */
915         {
916                 INIT_CTL_NAME
917                 .procname       = "stats",
918                 .mode           = 0644,
919                 .proc_handler   = &proc_lnet_stats,
920         },
921         {
922                 INIT_CTL_NAME
923                 .procname       = "routes",
924                 .mode           = 0444,
925                 .proc_handler   = &proc_lnet_routes,
926         },
927         {
928                 INIT_CTL_NAME
929                 .procname       = "routers",
930                 .mode           = 0444,
931                 .proc_handler   = &proc_lnet_routers,
932         },
933         {
934                 INIT_CTL_NAME
935                 .procname       = "peers",
936                 .mode           = 0644,
937                 .proc_handler   = &proc_lnet_peers,
938         },
939         {
940                 INIT_CTL_NAME
941                 .procname       = "buffers",
942                 .mode           = 0444,
943                 .proc_handler   = &proc_lnet_buffers,
944         },
945         {
946                 INIT_CTL_NAME
947                 .procname       = "nis",
948                 .mode           = 0644,
949                 .proc_handler   = &proc_lnet_nis,
950         },
951         {
952                 INIT_CTL_NAME
953                 .procname       = "portal_rotor",
954                 .mode           = 0644,
955                 .proc_handler   = &proc_lnet_portal_rotor,
956         },
957         { .procname = NULL }
958 };
959
960 void lnet_router_debugfs_init(void)
961 {
962         lnet_insert_debugfs(lnet_table);
963 }
964
965 void lnet_router_debugfs_fini(void)
966 {
967         lnet_remove_debugfs(lnet_table);
968 }