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