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