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