Whamcloud - gitweb
LU-9859 libcfs: delete libcfs/linux/libcfs.h
[fs/lustre-release.git] / lnet / lnet / router_proc.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2017, Intel Corporation.
5  *
6  *   This file is part of Lustre, https://wiki.hpdd.intel.com/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <linux/uaccess.h>
26
27 #include <libcfs/linux/linux-misc.h>
28 #include <libcfs/libcfs.h>
29 #include <lnet/lib-lnet.h>
30
31 /* This is really lnet_proc.c. You might need to update sanity test 215
32  * if any file format is changed. */
33
34 #define LNET_LOFFT_BITS         (sizeof(loff_t) * 8)
35 /*
36  * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
37  */
38 #define LNET_PROC_CPT_BITS      (LNET_CPT_BITS + 1)
39 /* change version, 16 bits or 8 bits */
40 #define LNET_PROC_VER_BITS      MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
41
42 #define LNET_PROC_HASH_BITS     LNET_PEER_HASH_BITS
43 /*
44  * bits for peer hash offset
45  * NB: we don't use the highest bit of *ppos because it's signed
46  */
47 #define LNET_PROC_HOFF_BITS     (LNET_LOFFT_BITS -       \
48                                  LNET_PROC_CPT_BITS -    \
49                                  LNET_PROC_VER_BITS -    \
50                                  LNET_PROC_HASH_BITS - 1)
51 /* bits for hash index + position */
52 #define LNET_PROC_HPOS_BITS     (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
53 /* bits for peer hash table + hash version */
54 #define LNET_PROC_VPOS_BITS     (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
55
56 #define LNET_PROC_CPT_MASK      ((1ULL << LNET_PROC_CPT_BITS) - 1)
57 #define LNET_PROC_VER_MASK      ((1ULL << LNET_PROC_VER_BITS) - 1)
58 #define LNET_PROC_HASH_MASK     ((1ULL << LNET_PROC_HASH_BITS) - 1)
59 #define LNET_PROC_HOFF_MASK     ((1ULL << LNET_PROC_HOFF_BITS) - 1)
60
61 #define LNET_PROC_CPT_GET(pos)                          \
62         (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
63
64 #define LNET_PROC_VER_GET(pos)                          \
65         (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
66
67 #define LNET_PROC_HASH_GET(pos)                         \
68         (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
69
70 #define LNET_PROC_HOFF_GET(pos)                         \
71         (int)((pos) & LNET_PROC_HOFF_MASK)
72
73 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off)         \
74         (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) |   \
75         ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) |   \
76         ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
77         ((off) & LNET_PROC_HOFF_MASK))
78
79 #define LNET_PROC_VERSION(v)    ((unsigned int)((v) & LNET_PROC_VER_MASK))
80
81 static int __proc_lnet_stats(void *data, int write,
82                              loff_t pos, void __user *buffer, int nob)
83 {
84         int              rc;
85         struct lnet_counters *ctrs;
86         int              len;
87         char            *tmpstr;
88         const int        tmpsiz = 256; /* 7 %u and 4 __u64 */
89
90         if (write) {
91                 lnet_counters_reset();
92                 return 0;
93         }
94
95         /* read */
96
97         LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
98         if (ctrs == NULL)
99                 return -ENOMEM;
100
101         LIBCFS_ALLOC(tmpstr, tmpsiz);
102         if (tmpstr == NULL) {
103                 LIBCFS_FREE(ctrs, sizeof(*ctrs));
104                 return -ENOMEM;
105         }
106
107         lnet_counters_get(ctrs);
108
109         len = snprintf(tmpstr, tmpsiz,
110                        "%u %u %u %u %u %u %u %llu %llu "
111                        "%llu %llu",
112                        ctrs->msgs_alloc, ctrs->msgs_max,
113                        ctrs->errors,
114                        ctrs->send_count, ctrs->recv_count,
115                        ctrs->route_count, ctrs->drop_count,
116                        ctrs->send_length, ctrs->recv_length,
117                        ctrs->route_length, ctrs->drop_length);
118
119         if (pos >= min_t(int, len, strlen(tmpstr)))
120                 rc = 0;
121         else
122                 rc = cfs_trace_copyout_string(buffer, nob,
123                                               tmpstr + pos, "\n");
124
125         LIBCFS_FREE(tmpstr, tmpsiz);
126         LIBCFS_FREE(ctrs, sizeof(*ctrs));
127         return rc;
128 }
129
130 static int
131 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
132                 size_t *lenp, loff_t *ppos)
133 {
134         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
135                                     __proc_lnet_stats);
136 }
137
138 static int
139 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
140                  size_t *lenp, loff_t *ppos)
141 {
142         const int       tmpsiz = 256;
143         char            *tmpstr;
144         char            *s;
145         int             rc = 0;
146         int             len;
147         int             ver;
148         int             off;
149
150         CLASSERT(sizeof(loff_t) >= 4);
151
152         off = LNET_PROC_HOFF_GET(*ppos);
153         ver = LNET_PROC_VER_GET(*ppos);
154
155         LASSERT(!write);
156
157         if (*lenp == 0)
158                 return 0;
159
160         LIBCFS_ALLOC(tmpstr, tmpsiz);
161         if (tmpstr == NULL)
162                 return -ENOMEM;
163
164         s = tmpstr; /* points to current position in tmpstr[] */
165
166         if (*ppos == 0) {
167                 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
168                               the_lnet.ln_routing ? "enabled" : "disabled");
169                 LASSERT(tmpstr + tmpsiz - s > 0);
170
171                 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
172                               "net", "hops", "priority", "state", "router");
173                 LASSERT(tmpstr + tmpsiz - s > 0);
174
175                 lnet_net_lock(0);
176                 ver = (unsigned int)the_lnet.ln_remote_nets_version;
177                 lnet_net_unlock(0);
178                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
179         } else {
180                 struct list_head        *n;
181                 struct list_head        *r;
182                 struct lnet_route               *route = NULL;
183                 struct lnet_remotenet   *rnet  = NULL;
184                 int                     skip  = off - 1;
185                 struct list_head        *rn_list;
186                 int                     i;
187
188                 lnet_net_lock(0);
189
190                 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
191                         lnet_net_unlock(0);
192                         LIBCFS_FREE(tmpstr, tmpsiz);
193                         return -ESTALE;
194                 }
195
196                 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
197                      i++) {
198                         rn_list = &the_lnet.ln_remote_nets_hash[i];
199
200                         n = rn_list->next;
201
202                         while (n != rn_list && route == NULL) {
203                                 rnet = list_entry(n, struct lnet_remotenet,
204                                                   lrn_list);
205
206                                 r = rnet->lrn_routes.next;
207
208                                 while (r != &rnet->lrn_routes) {
209                                         struct lnet_route *re =
210                                                 list_entry(r, struct lnet_route,
211                                                            lr_list);
212                                         if (skip == 0) {
213                                                 route = re;
214                                                 break;
215                                         }
216
217                                         skip--;
218                                         r = r->next;
219                                 }
220
221                                 n = n->next;
222                         }
223                 }
224
225                 if (route != NULL) {
226                         __u32        net        = rnet->lrn_net;
227                         __u32 hops              = route->lr_hops;
228                         unsigned int priority   = route->lr_priority;
229                         lnet_nid_t   nid        = route->lr_gateway->lpni_nid;
230                         int          alive      = lnet_is_route_alive(route);
231
232                         s += snprintf(s, tmpstr + tmpsiz - s,
233                                       "%-8s %4d %8u %7s %s\n",
234                                       libcfs_net2str(net), hops,
235                                       priority,
236                                       alive ? "up" : "down",
237                                       libcfs_nid2str(nid));
238                         LASSERT(tmpstr + tmpsiz - s > 0);
239                 }
240
241                 lnet_net_unlock(0);
242         }
243
244         len = s - tmpstr;     /* how many bytes was written */
245
246         if (len > *lenp) {    /* linux-supplied buffer is too small */
247                 rc = -EINVAL;
248         } else if (len > 0) { /* wrote something */
249                 if (copy_to_user(buffer, tmpstr, len))
250                         rc = -EFAULT;
251                 else {
252                         off += 1;
253                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
254                 }
255         }
256
257         LIBCFS_FREE(tmpstr, tmpsiz);
258
259         if (rc == 0)
260                 *lenp = len;
261
262         return rc;
263 }
264
265 static int
266 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
267                   size_t *lenp, loff_t *ppos)
268 {
269         int        rc = 0;
270         char      *tmpstr;
271         char      *s;
272         const int  tmpsiz = 256;
273         int        len;
274         int        ver;
275         int        off;
276
277         off = LNET_PROC_HOFF_GET(*ppos);
278         ver = LNET_PROC_VER_GET(*ppos);
279
280         LASSERT(!write);
281
282         if (*lenp == 0)
283                 return 0;
284
285         LIBCFS_ALLOC(tmpstr, tmpsiz);
286         if (tmpstr == NULL)
287                 return -ENOMEM;
288
289         s = tmpstr; /* points to current position in tmpstr[] */
290
291         if (*ppos == 0) {
292                 s += snprintf(s, tmpstr + tmpsiz - s,
293                               "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
294                               "ref", "rtr_ref", "alive_cnt", "state",
295                               "last_ping", "ping_sent", "deadline",
296                               "down_ni", "router");
297                 LASSERT(tmpstr + tmpsiz - s > 0);
298
299                 lnet_net_lock(0);
300                 ver = (unsigned int)the_lnet.ln_routers_version;
301                 lnet_net_unlock(0);
302                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
303         } else {
304                 struct list_head *r;
305                 struct lnet_peer_ni *peer = NULL;
306                 int               skip = off - 1;
307
308                 lnet_net_lock(0);
309
310                 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
311                         lnet_net_unlock(0);
312
313                         LIBCFS_FREE(tmpstr, tmpsiz);
314                         return -ESTALE;
315                 }
316
317                 r = the_lnet.ln_routers.next;
318
319                 while (r != &the_lnet.ln_routers) {
320                         struct lnet_peer_ni *lp =
321                           list_entry(r, struct lnet_peer_ni,
322                                      lpni_rtr_list);
323
324                         if (skip == 0) {
325                                 peer = lp;
326                                 break;
327                         }
328
329                         skip--;
330                         r = r->next;
331                 }
332
333                 if (peer != NULL) {
334                         lnet_nid_t nid = peer->lpni_nid;
335                         cfs_time_t now = cfs_time_current();
336                         cfs_time_t deadline = peer->lpni_ping_deadline;
337                         int nrefs     = atomic_read(&peer->lpni_refcount);
338                         int nrtrrefs  = peer->lpni_rtr_refcount;
339                         int alive_cnt = peer->lpni_alive_count;
340                         int alive     = peer->lpni_alive;
341                         int pingsent  = !peer->lpni_ping_notsent;
342                         int last_ping = cfs_duration_sec(cfs_time_sub(now,
343                                                      peer->lpni_ping_timestamp));
344                         int down_ni   = 0;
345                         struct lnet_route *rtr;
346
347                         if ((peer->lpni_ping_feats &
348                              LNET_PING_FEAT_NI_STATUS) != 0) {
349                                 list_for_each_entry(rtr, &peer->lpni_routes,
350                                                     lr_gwlist) {
351                                         /* downis on any route should be the
352                                          * number of downis on the gateway */
353                                         if (rtr->lr_downis != 0) {
354                                                 down_ni = rtr->lr_downis;
355                                                 break;
356                                         }
357                                 }
358                         }
359
360                         if (deadline == 0)
361                                 s += snprintf(s, tmpstr + tmpsiz - s,
362                                               "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
363                                               nrefs, nrtrrefs, alive_cnt,
364                                               alive ? "up" : "down", last_ping,
365                                               pingsent, "NA", down_ni,
366                                               libcfs_nid2str(nid));
367                         else
368                                 s += snprintf(s, tmpstr + tmpsiz - s,
369                                               "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
370                                               nrefs, nrtrrefs, alive_cnt,
371                                               alive ? "up" : "down", last_ping,
372                                               pingsent,
373                                               cfs_duration_sec(cfs_time_sub(deadline, now)),
374                                               down_ni, libcfs_nid2str(nid));
375                         LASSERT(tmpstr + tmpsiz - s > 0);
376                 }
377
378                 lnet_net_unlock(0);
379         }
380
381         len = s - tmpstr;     /* how many bytes was written */
382
383         if (len > *lenp) {    /* linux-supplied buffer is too small */
384                 rc = -EINVAL;
385         } else if (len > 0) { /* wrote something */
386                 if (copy_to_user(buffer, tmpstr, len))
387                         rc = -EFAULT;
388                 else {
389                         off += 1;
390                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
391                 }
392         }
393
394         LIBCFS_FREE(tmpstr, tmpsiz);
395
396         if (rc == 0)
397                 *lenp = len;
398
399         return rc;
400 }
401
402 /* TODO: there should be no direct access to ptable. We should add a set
403  * of APIs that give access to the ptable and its members */
404 static int
405 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
406                 size_t *lenp, loff_t *ppos)
407 {
408         const int               tmpsiz  = 256;
409         struct lnet_peer_table  *ptable;
410         char                    *tmpstr = NULL;
411         char                    *s;
412         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
413         int                     ver  = LNET_PROC_VER_GET(*ppos);
414         int                     hash = LNET_PROC_HASH_GET(*ppos);
415         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
416         int                     rc = 0;
417         int                     len;
418
419         if (write) {
420                 int i;
421                 struct lnet_peer_ni *peer;
422
423                 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
424                         lnet_net_lock(i);
425                         for (hash = 0; hash < LNET_PEER_HASH_SIZE; hash++) {
426                                 list_for_each_entry(peer,
427                                                     &ptable->pt_hash[hash],
428                                                     lpni_hashlist) {
429                                         peer->lpni_mintxcredits =
430                                                 peer->lpni_txcredits;
431                                         peer->lpni_minrtrcredits =
432                                                 peer->lpni_rtrcredits;
433                                 }
434                         }
435                         lnet_net_unlock(i);
436                 }
437                 *ppos += *lenp;
438                 return 0;
439         }
440
441         if (*lenp == 0)
442                 return 0;
443
444         CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
445
446         if (cpt >= LNET_CPT_NUMBER) {
447                 *lenp = 0;
448                 return 0;
449         }
450
451         LIBCFS_ALLOC(tmpstr, tmpsiz);
452         if (tmpstr == NULL)
453                 return -ENOMEM;
454
455         s = tmpstr; /* points to current position in tmpstr[] */
456
457         if (*ppos == 0) {
458                 s += snprintf(s, tmpstr + tmpsiz - s,
459                               "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
460                               "nid", "refs", "state", "last", "max",
461                               "rtr", "min", "tx", "min", "queue");
462                 LASSERT(tmpstr + tmpsiz - s > 0);
463
464                 hoff++;
465         } else {
466                 struct lnet_peer_ni     *peer;
467                 struct list_head        *p;
468                 int                     skip;
469
470  again:
471                 p = NULL;
472                 peer = NULL;
473                 skip = hoff - 1;
474
475                 lnet_net_lock(cpt);
476                 ptable = the_lnet.ln_peer_tables[cpt];
477                 if (hoff == 1)
478                         ver = LNET_PROC_VERSION(ptable->pt_version);
479
480                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
481                         lnet_net_unlock(cpt);
482                         LIBCFS_FREE(tmpstr, tmpsiz);
483                         return -ESTALE;
484                 }
485
486                 while (hash < LNET_PEER_HASH_SIZE) {
487                         if (p == NULL)
488                                 p = ptable->pt_hash[hash].next;
489
490                         while (p != &ptable->pt_hash[hash]) {
491                                 struct lnet_peer_ni *lp =
492                                   list_entry(p, struct lnet_peer_ni,
493                                              lpni_hashlist);
494                                 if (skip == 0) {
495                                         peer = lp;
496
497                                         /* minor optimization: start from idx+1
498                                          * on next iteration if we've just
499                                          * drained lpni_hashlist */
500                                         if (lp->lpni_hashlist.next ==
501                                             &ptable->pt_hash[hash]) {
502                                                 hoff = 1;
503                                                 hash++;
504                                         } else {
505                                                 hoff++;
506                                         }
507
508                                         break;
509                                 }
510
511                                 skip--;
512                                 p = lp->lpni_hashlist.next;
513                         }
514
515                         if (peer != NULL)
516                                 break;
517
518                         p = NULL;
519                         hoff = 1;
520                         hash++;
521                 }
522
523                 if (peer != NULL) {
524                         lnet_nid_t nid = peer->lpni_nid;
525                         int nrefs = atomic_read(&peer->lpni_refcount);
526                         int lastalive = -1;
527                         char *aliveness = "NA";
528                         int maxcr = (peer->lpni_net) ?
529                           peer->lpni_net->net_tunables.lct_peer_tx_credits : 0;
530                         int txcr = peer->lpni_txcredits;
531                         int mintxcr = peer->lpni_mintxcredits;
532                         int rtrcr = peer->lpni_rtrcredits;
533                         int minrtrcr = peer->lpni_minrtrcredits;
534                         int txqnob = peer->lpni_txqnob;
535
536                         if (lnet_isrouter(peer) ||
537                             lnet_peer_aliveness_enabled(peer))
538                                 aliveness = peer->lpni_alive ? "up" : "down";
539
540                         if (lnet_peer_aliveness_enabled(peer)) {
541                                 cfs_time_t now = cfs_time_current();
542                                 cfs_duration_t delta;
543
544                                 delta = cfs_time_sub(now, peer->lpni_last_alive);
545                                 lastalive = cfs_duration_sec(delta);
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 %5d %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                         int     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 %5d %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 = cfs_trimwhite(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         { 0 }
958 };
959
960 void lnet_router_debugfs_init(void)
961 {
962         lnet_insert_debugfs(lnet_table, NULL);
963 }