Whamcloud - gitweb
LU-7734 lnet: Multi-Rail local NI split
[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         lnet_counters_t *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                 lnet_route_t            *route = NULL;
181                 lnet_remotenet_t        *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, lnet_remotenet_t,
202                                                   lrn_list);
203
204                                 r = rnet->lrn_routes.next;
205
206                                 while (r != &rnet->lrn_routes) {
207                                         lnet_route_t *re =
208                                                 list_entry(r, lnet_route_t,
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->lp_nid;
228                         int          alive      = lnet_is_route_alive(route);
229
230                         s += snprintf(s, tmpstr + tmpsiz - s,
231                                       "%-8s %4u %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 *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                         lnet_peer_t *lp = list_entry(r, lnet_peer_t,
319                                                      lp_rtr_list);
320
321                         if (skip == 0) {
322                                 peer = lp;
323                                 break;
324                         }
325
326                         skip--;
327                         r = r->next;
328                 }
329
330                 if (peer != NULL) {
331                         lnet_nid_t nid = peer->lp_nid;
332                         cfs_time_t now = cfs_time_current();
333                         cfs_time_t deadline = peer->lp_ping_deadline;
334                         int nrefs     = peer->lp_refcount;
335                         int nrtrrefs  = peer->lp_rtr_refcount;
336                         int alive_cnt = peer->lp_alive_count;
337                         int alive     = peer->lp_alive;
338                         int pingsent  = !peer->lp_ping_notsent;
339                         int last_ping = cfs_duration_sec(cfs_time_sub(now,
340                                                      peer->lp_ping_timestamp));
341                         int down_ni   = 0;
342                         lnet_route_t *rtr;
343
344                         if ((peer->lp_ping_feats &
345                              LNET_PING_FEAT_NI_STATUS) != 0) {
346                                 list_for_each_entry(rtr, &peer->lp_routes,
347                                                     lr_gwlist) {
348                                         /* downis on any route should be the
349                                          * number of downis on the gateway */
350                                         if (rtr->lr_downis != 0) {
351                                                 down_ni = rtr->lr_downis;
352                                                 break;
353                                         }
354                                 }
355                         }
356
357                         if (deadline == 0)
358                                 s += snprintf(s, tmpstr + tmpsiz - s,
359                                               "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
360                                               nrefs, nrtrrefs, alive_cnt,
361                                               alive ? "up" : "down", last_ping,
362                                               pingsent, "NA", down_ni,
363                                               libcfs_nid2str(nid));
364                         else
365                                 s += snprintf(s, tmpstr + tmpsiz - s,
366                                               "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
367                                               nrefs, nrtrrefs, alive_cnt,
368                                               alive ? "up" : "down", last_ping,
369                                               pingsent,
370                                               cfs_duration_sec(cfs_time_sub(deadline, now)),
371                                               down_ni, libcfs_nid2str(nid));
372                         LASSERT(tmpstr + tmpsiz - s > 0);
373                 }
374
375                 lnet_net_unlock(0);
376         }
377
378         len = s - tmpstr;     /* how many bytes was written */
379
380         if (len > *lenp) {    /* linux-supplied buffer is too small */
381                 rc = -EINVAL;
382         } else if (len > 0) { /* wrote something */
383                 if (copy_to_user(buffer, tmpstr, len))
384                         rc = -EFAULT;
385                 else {
386                         off += 1;
387                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
388                 }
389         }
390
391         LIBCFS_FREE(tmpstr, tmpsiz);
392
393         if (rc == 0)
394                 *lenp = len;
395
396         return rc;
397 }
398
399 static int
400 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
401                 size_t *lenp, loff_t *ppos)
402 {
403         const int               tmpsiz  = 256;
404         struct lnet_peer_table  *ptable;
405         char                    *tmpstr;
406         char                    *s;
407         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
408         int                     ver  = LNET_PROC_VER_GET(*ppos);
409         int                     hash = LNET_PROC_HASH_GET(*ppos);
410         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
411         int                     rc = 0;
412         int                     len;
413
414         CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
415         LASSERT(!write);
416
417         if (*lenp == 0)
418                 return 0;
419
420         if (cpt >= LNET_CPT_NUMBER) {
421                 *lenp = 0;
422                 return 0;
423         }
424
425         LIBCFS_ALLOC(tmpstr, tmpsiz);
426         if (tmpstr == NULL)
427                 return -ENOMEM;
428
429         s = tmpstr; /* points to current position in tmpstr[] */
430
431         if (*ppos == 0) {
432                 s += snprintf(s, tmpstr + tmpsiz - s,
433                               "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
434                               "nid", "refs", "state", "last", "max",
435                               "rtr", "min", "tx", "min", "queue");
436                 LASSERT(tmpstr + tmpsiz - s > 0);
437
438                 hoff++;
439         } else {
440                 struct lnet_peer        *peer;
441                 struct list_head        *p;
442                 int                     skip;
443  again:
444                 p = NULL;
445                 peer = NULL;
446                 skip = hoff - 1;
447
448                 lnet_net_lock(cpt);
449                 ptable = the_lnet.ln_peer_tables[cpt];
450                 if (hoff == 1)
451                         ver = LNET_PROC_VERSION(ptable->pt_version);
452
453                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
454                         lnet_net_unlock(cpt);
455                         LIBCFS_FREE(tmpstr, tmpsiz);
456                         return -ESTALE;
457                 }
458
459                 while (hash < LNET_PEER_HASH_SIZE) {
460                         if (p == NULL)
461                                 p = ptable->pt_hash[hash].next;
462
463                         while (p != &ptable->pt_hash[hash]) {
464                                 lnet_peer_t *lp = list_entry(p, lnet_peer_t,
465                                                              lp_hashlist);
466                                 if (skip == 0) {
467                                         peer = lp;
468
469                                         /* minor optimization: start from idx+1
470                                          * on next iteration if we've just
471                                          * drained lp_hashlist */
472                                         if (lp->lp_hashlist.next ==
473                                             &ptable->pt_hash[hash]) {
474                                                 hoff = 1;
475                                                 hash++;
476                                         } else {
477                                                 hoff++;
478                                         }
479
480                                         break;
481                                 }
482
483                                 skip--;
484                                 p = lp->lp_hashlist.next;
485                         }
486
487                         if (peer != NULL)
488                                 break;
489
490                         p = NULL;
491                         hoff = 1;
492                         hash++;
493                 }
494
495                 if (peer != NULL) {
496                         lnet_nid_t nid       = peer->lp_nid;
497                         int nrefs     = peer->lp_refcount;
498                         int lastalive = -1;
499                         char *aliveness = "NA";
500                         int maxcr = peer->lp_net->net_tunables.lct_peer_tx_credits;
501                         int txcr = peer->lp_txcredits;
502                         int mintxcr = peer->lp_mintxcredits;
503                         int rtrcr = peer->lp_rtrcredits;
504                         int minrtrcr = peer->lp_minrtrcredits;
505                         int txqnob = peer->lp_txqnob;
506
507                         if (lnet_isrouter(peer) ||
508                             lnet_peer_aliveness_enabled(peer))
509                                 aliveness = peer->lp_alive ? "up" : "down";
510
511                         if (lnet_peer_aliveness_enabled(peer)) {
512                                 cfs_time_t     now = cfs_time_current();
513                                 cfs_duration_t delta;
514
515                                 delta = cfs_time_sub(now, peer->lp_last_alive);
516                                 lastalive = cfs_duration_sec(delta);
517
518                                 /* No need to mess up peers contents with
519                                  * arbitrarily long integers - it suffices to
520                                  * know that lastalive is more than 10000s old
521                                  */
522                                 if (lastalive >= 10000)
523                                         lastalive = 9999;
524                         }
525
526                         lnet_net_unlock(cpt);
527
528                         s += snprintf(s, tmpstr + tmpsiz - s,
529                                       "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
530                                       libcfs_nid2str(nid), nrefs, aliveness,
531                                       lastalive, maxcr, rtrcr, minrtrcr, txcr,
532                                       mintxcr, txqnob);
533                         LASSERT(tmpstr + tmpsiz - s > 0);
534
535                 } else { /* peer is NULL */
536                         lnet_net_unlock(cpt);
537                 }
538
539                 if (hash == LNET_PEER_HASH_SIZE) {
540                         cpt++;
541                         hash = 0;
542                         hoff = 1;
543                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
544                                 goto again;
545                 }
546         }
547
548         len = s - tmpstr;     /* how many bytes was written */
549
550         if (len > *lenp) {    /* linux-supplied buffer is too small */
551                 rc = -EINVAL;
552         } else if (len > 0) { /* wrote something */
553                 if (copy_to_user(buffer, tmpstr, len))
554                         rc = -EFAULT;
555                 else
556                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
557         }
558
559         LIBCFS_FREE(tmpstr, tmpsiz);
560
561         if (rc == 0)
562                 *lenp = len;
563
564         return rc;
565 }
566
567 static int __proc_lnet_buffers(void *data, int write,
568                                loff_t pos, void __user *buffer, int nob)
569 {
570         char            *s;
571         char            *tmpstr;
572         int             tmpsiz;
573         int             idx;
574         int             len;
575         int             rc;
576         int             i;
577
578         LASSERT(!write);
579
580         /* (4 %d) * 4 * LNET_CPT_NUMBER */
581         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
582         LIBCFS_ALLOC(tmpstr, tmpsiz);
583         if (tmpstr == NULL)
584                 return -ENOMEM;
585
586         s = tmpstr; /* points to current position in tmpstr[] */
587
588         s += snprintf(s, tmpstr + tmpsiz - s,
589                       "%5s %5s %7s %7s\n",
590                       "pages", "count", "credits", "min");
591         LASSERT(tmpstr + tmpsiz - s > 0);
592
593         if (the_lnet.ln_rtrpools == NULL)
594                 goto out; /* I'm not a router */
595
596         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
597                 lnet_rtrbufpool_t *rbp;
598
599                 lnet_net_lock(LNET_LOCK_EX);
600                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
601                         s += snprintf(s, tmpstr + tmpsiz - s,
602                                       "%5d %5d %7d %7d\n",
603                                       rbp[idx].rbp_npages,
604                                       rbp[idx].rbp_nbuffers,
605                                       rbp[idx].rbp_credits,
606                                       rbp[idx].rbp_mincredits);
607                         LASSERT(tmpstr + tmpsiz - s > 0);
608                 }
609                 lnet_net_unlock(LNET_LOCK_EX);
610         }
611
612  out:
613         len = s - tmpstr;
614
615         if (pos >= min_t(int, len, strlen(tmpstr)))
616                 rc = 0;
617         else
618                 rc = cfs_trace_copyout_string(buffer, nob,
619                                               tmpstr + pos, NULL);
620
621         LIBCFS_FREE(tmpstr, tmpsiz);
622         return rc;
623 }
624
625 static int
626 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
627                   size_t *lenp, loff_t *ppos)
628 {
629         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
630                                     __proc_lnet_buffers);
631 }
632
633 static int
634 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
635               size_t *lenp, loff_t *ppos)
636 {
637         int     tmpsiz = 128 * LNET_CPT_NUMBER;
638         int        rc = 0;
639         char      *tmpstr;
640         char      *s;
641         int        len;
642
643         LASSERT(!write);
644
645         if (*lenp == 0)
646                 return 0;
647
648         LIBCFS_ALLOC(tmpstr, tmpsiz);
649         if (tmpstr == NULL)
650                 return -ENOMEM;
651
652         s = tmpstr; /* points to current position in tmpstr[] */
653
654         if (*ppos == 0) {
655                 s += snprintf(s, tmpstr + tmpsiz - s,
656                               "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
657                               "nid", "status", "alive", "refs", "peer",
658                               "rtr", "max", "tx", "min");
659                 LASSERT (tmpstr + tmpsiz - s > 0);
660         } else {
661                 lnet_ni_t         *ni   = NULL;
662                 int                skip = *ppos - 1;
663
664                 lnet_net_lock(0);
665
666                 ni = lnet_get_ni_idx_locked(skip);
667
668                 if (ni != NULL) {
669                         struct lnet_tx_queue    *tq;
670                         char    *stat;
671                         time64_t now = ktime_get_real_seconds();
672                         int     last_alive = -1;
673                         int     i;
674                         int     j;
675
676                         if (the_lnet.ln_routing)
677                                 last_alive = now - ni->ni_last_alive;
678
679                         /* @lo forever alive */
680                         if (ni->ni_net->net_lnd->lnd_type == LOLND)
681                                 last_alive = 0;
682
683                         lnet_ni_lock(ni);
684                         LASSERT(ni->ni_status != NULL);
685                         stat = (ni->ni_status->ns_status ==
686                                 LNET_NI_STATUS_UP) ? "up" : "down";
687                         lnet_ni_unlock(ni);
688
689                         /* we actually output credits information for
690                          * TX queue of each partition */
691                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
692                                 for (j = 0; ni->ni_cpts != NULL &&
693                                      j < ni->ni_ncpts; j++) {
694                                         if (i == ni->ni_cpts[j])
695                                                 break;
696                                 }
697
698                                 if (j == ni->ni_ncpts)
699                                         continue;
700
701                                 if (i != 0)
702                                         lnet_net_lock(i);
703
704                                 s += snprintf(s, tmpstr + tmpsiz - s,
705                                       "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
706                                       libcfs_nid2str(ni->ni_nid), stat,
707                                       last_alive, *ni->ni_refs[i],
708                                       ni->ni_net->net_tunables.lct_peer_tx_credits,
709                                       ni->ni_net->net_tunables.lct_peer_rtr_credits,
710                                       tq->tq_credits_max,
711                                       tq->tq_credits, tq->tq_credits_min);
712                                 if (i != 0)
713                                         lnet_net_unlock(i);
714                         }
715                         LASSERT(tmpstr + tmpsiz - s > 0);
716                 }
717
718                 lnet_net_unlock(0);
719         }
720
721         len = s - tmpstr;     /* how many bytes was written */
722
723         if (len > *lenp) {    /* linux-supplied buffer is too small */
724                 rc = -EINVAL;
725         } else if (len > 0) { /* wrote something */
726                 if (copy_to_user(buffer, tmpstr, len))
727                         rc = -EFAULT;
728                 else
729                         *ppos += 1;
730         }
731
732         LIBCFS_FREE(tmpstr, tmpsiz);
733
734         if (rc == 0)
735                 *lenp = len;
736
737         return rc;
738 }
739
740 struct lnet_portal_rotors {
741         int             pr_value;
742         const char      *pr_name;
743         const char      *pr_desc;
744 };
745
746 static struct lnet_portal_rotors        portal_rotors[] = {
747         {
748                 .pr_value = LNET_PTL_ROTOR_OFF,
749                 .pr_name  = "OFF",
750                 .pr_desc  = "Turn off message rotor for wildcard portals"
751         },
752         {
753                 .pr_value = LNET_PTL_ROTOR_ON,
754                 .pr_name  = "ON",
755                 .pr_desc  = "round-robin dispatch all PUT messages for "
756                             "wildcard portals"
757         },
758         {
759                 .pr_value = LNET_PTL_ROTOR_RR_RT,
760                 .pr_name  = "RR_RT",
761                 .pr_desc  = "round-robin dispatch routed PUT message for "
762                             "wildcard portals"
763         },
764         {
765                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
766                 .pr_name  = "HASH_RT",
767                 .pr_desc  = "dispatch routed PUT message by hashing source "
768                             "NID for wildcard portals"
769         },
770         {
771                 .pr_value = -1,
772                 .pr_name  = NULL,
773                 .pr_desc  = NULL
774         },
775 };
776
777 static int __proc_lnet_portal_rotor(void *data, int write,
778                                     loff_t pos, void __user *buffer, int nob)
779 {
780         const int       buf_len = 128;
781         char            *buf;
782         char            *tmp;
783         int             rc;
784         int             i;
785
786         LIBCFS_ALLOC(buf, buf_len);
787         if (buf == NULL)
788                 return -ENOMEM;
789
790         if (!write) {
791                 lnet_res_lock(0);
792
793                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
794                         if (portal_rotors[i].pr_value == portal_rotor)
795                                 break;
796                 }
797
798                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
799                 lnet_res_unlock(0);
800
801                 rc = snprintf(buf, buf_len,
802                               "{\n\tportals: all\n"
803                               "\trotor: %s\n\tdescription: %s\n}",
804                               portal_rotors[i].pr_name,
805                               portal_rotors[i].pr_desc);
806
807                 if (pos >= min_t(int, rc, buf_len)) {
808                         rc = 0;
809                 } else {
810                         rc = cfs_trace_copyout_string(buffer, nob,
811                                         buf + pos, "\n");
812                 }
813                 goto out;
814         }
815
816         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
817         if (rc < 0)
818                 goto out;
819
820         tmp = cfs_trimwhite(buf);
821
822         rc = -EINVAL;
823         lnet_res_lock(0);
824         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
825                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
826                                 strlen(portal_rotors[i].pr_name)) == 0) {
827                         portal_rotor = portal_rotors[i].pr_value;
828                         rc = 0;
829                         break;
830                 }
831         }
832         lnet_res_unlock(0);
833 out:
834         LIBCFS_FREE(buf, buf_len);
835         return rc;
836 }
837
838 static int
839 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
840                        size_t *lenp, loff_t *ppos)
841 {
842         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
843                                     __proc_lnet_portal_rotor);
844 }
845
846
847 static struct ctl_table lnet_table[] = {
848         /*
849          * NB No .strategy entries have been provided since sysctl(8) prefers
850          * to go via /proc for portability.
851          */
852         {
853                 INIT_CTL_NAME
854                 .procname       = "stats",
855                 .mode           = 0644,
856                 .proc_handler   = &proc_lnet_stats,
857         },
858         {
859                 INIT_CTL_NAME
860                 .procname       = "routes",
861                 .mode           = 0444,
862                 .proc_handler   = &proc_lnet_routes,
863         },
864         {
865                 INIT_CTL_NAME
866                 .procname       = "routers",
867                 .mode           = 0444,
868                 .proc_handler   = &proc_lnet_routers,
869         },
870         {
871                 INIT_CTL_NAME
872                 .procname       = "peers",
873                 .mode           = 0444,
874                 .proc_handler   = &proc_lnet_peers,
875         },
876         {
877                 INIT_CTL_NAME
878                 .procname       = "buffers",
879                 .mode           = 0444,
880                 .proc_handler   = &proc_lnet_buffers,
881         },
882         {
883                 INIT_CTL_NAME
884                 .procname       = "nis",
885                 .mode           = 0444,
886                 .proc_handler   = &proc_lnet_nis,
887         },
888         {
889                 INIT_CTL_NAME
890                 .procname       = "portal_rotor",
891                 .mode           = 0644,
892                 .proc_handler   = &proc_lnet_portal_rotor,
893         },
894         { 0 }
895 };
896
897 static struct ctl_table top_table[] = {
898         {
899                 INIT_CTL_NAME
900                 .procname       = "lnet",
901                 .mode           = 0555,
902                 .data           = NULL,
903                 .maxlen         = 0,
904                 .child          = lnet_table,
905         },
906         { 0 }
907 };
908
909 void
910 lnet_proc_init(void)
911 {
912 #ifdef CONFIG_SYSCTL
913         if (lnet_table_header == NULL)
914                 lnet_table_header = register_sysctl_table(top_table);
915 #endif
916 }
917
918 void
919 lnet_proc_fini(void)
920 {
921 #ifdef CONFIG_SYSCTL
922         if (lnet_table_header != NULL)
923                 unregister_sysctl_table(lnet_table_header);
924
925         lnet_table_header = NULL;
926 #endif
927 }