Whamcloud - gitweb
40d0289293112ff97bec1e21cbee14da791f8db8
[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_ni->ni_peertxcredits;
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                 struct list_head  *n;
662                 lnet_ni_t         *ni   = NULL;
663                 int                skip = *ppos - 1;
664
665                 lnet_net_lock(0);
666
667                 n = the_lnet.ln_nis.next;
668
669                 while (n != &the_lnet.ln_nis) {
670                         lnet_ni_t *a_ni = list_entry(n, lnet_ni_t, ni_list);
671
672                         if (skip == 0) {
673                                 ni = a_ni;
674                                 break;
675                         }
676
677                         skip--;
678                         n = n->next;
679                 }
680
681                 if (ni != NULL) {
682                         struct lnet_tx_queue    *tq;
683                         char    *stat;
684                         long    now = cfs_time_current_sec();
685                         int     last_alive = -1;
686                         int     i;
687                         int     j;
688
689                         if (the_lnet.ln_routing)
690                                 last_alive = now - ni->ni_last_alive;
691
692                         /* @lo forever alive */
693                         if (ni->ni_lnd->lnd_type == LOLND)
694                                 last_alive = 0;
695
696                         lnet_ni_lock(ni);
697                         LASSERT(ni->ni_status != NULL);
698                         stat = (ni->ni_status->ns_status ==
699                                 LNET_NI_STATUS_UP) ? "up" : "down";
700                         lnet_ni_unlock(ni);
701
702                         /* we actually output credits information for
703                          * TX queue of each partition */
704                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
705                                 for (j = 0; ni->ni_cpts != NULL &&
706                                      j < ni->ni_ncpts; j++) {
707                                         if (i == ni->ni_cpts[j])
708                                                 break;
709                                 }
710
711                                 if (j == ni->ni_ncpts)
712                                         continue;
713
714                                 if (i != 0)
715                                         lnet_net_lock(i);
716
717                                 s += snprintf(s, tmpstr + tmpsiz - s,
718                                       "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
719                                       libcfs_nid2str(ni->ni_nid), stat,
720                                       last_alive, *ni->ni_refs[i],
721                                       ni->ni_peertxcredits,
722                                       ni->ni_peerrtrcredits,
723                                       tq->tq_credits_max,
724                                       tq->tq_credits, tq->tq_credits_min);
725                                 if (i != 0)
726                                         lnet_net_unlock(i);
727                         }
728                         LASSERT(tmpstr + tmpsiz - s > 0);
729                 }
730
731                 lnet_net_unlock(0);
732         }
733
734         len = s - tmpstr;     /* how many bytes was written */
735
736         if (len > *lenp) {    /* linux-supplied buffer is too small */
737                 rc = -EINVAL;
738         } else if (len > 0) { /* wrote something */
739                 if (copy_to_user(buffer, tmpstr, len))
740                         rc = -EFAULT;
741                 else
742                         *ppos += 1;
743         }
744
745         LIBCFS_FREE(tmpstr, tmpsiz);
746
747         if (rc == 0)
748                 *lenp = len;
749
750         return rc;
751 }
752
753 struct lnet_portal_rotors {
754         int             pr_value;
755         const char      *pr_name;
756         const char      *pr_desc;
757 };
758
759 static struct lnet_portal_rotors        portal_rotors[] = {
760         {
761                 .pr_value = LNET_PTL_ROTOR_OFF,
762                 .pr_name  = "OFF",
763                 .pr_desc  = "Turn off message rotor for wildcard portals"
764         },
765         {
766                 .pr_value = LNET_PTL_ROTOR_ON,
767                 .pr_name  = "ON",
768                 .pr_desc  = "round-robin dispatch all PUT messages for "
769                             "wildcard portals"
770         },
771         {
772                 .pr_value = LNET_PTL_ROTOR_RR_RT,
773                 .pr_name  = "RR_RT",
774                 .pr_desc  = "round-robin dispatch routed PUT message for "
775                             "wildcard portals"
776         },
777         {
778                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
779                 .pr_name  = "HASH_RT",
780                 .pr_desc  = "dispatch routed PUT message by hashing source "
781                             "NID for wildcard portals"
782         },
783         {
784                 .pr_value = -1,
785                 .pr_name  = NULL,
786                 .pr_desc  = NULL
787         },
788 };
789
790 static int __proc_lnet_portal_rotor(void *data, int write,
791                                     loff_t pos, void __user *buffer, int nob)
792 {
793         const int       buf_len = 128;
794         char            *buf;
795         char            *tmp;
796         int             rc;
797         int             i;
798
799         LIBCFS_ALLOC(buf, buf_len);
800         if (buf == NULL)
801                 return -ENOMEM;
802
803         if (!write) {
804                 lnet_res_lock(0);
805
806                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
807                         if (portal_rotors[i].pr_value == portal_rotor)
808                                 break;
809                 }
810
811                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
812                 lnet_res_unlock(0);
813
814                 rc = snprintf(buf, buf_len,
815                               "{\n\tportals: all\n"
816                               "\trotor: %s\n\tdescription: %s\n}",
817                               portal_rotors[i].pr_name,
818                               portal_rotors[i].pr_desc);
819
820                 if (pos >= min_t(int, rc, buf_len)) {
821                         rc = 0;
822                 } else {
823                         rc = cfs_trace_copyout_string(buffer, nob,
824                                         buf + pos, "\n");
825                 }
826                 goto out;
827         }
828
829         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
830         if (rc < 0)
831                 goto out;
832
833         tmp = cfs_trimwhite(buf);
834
835         rc = -EINVAL;
836         lnet_res_lock(0);
837         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
838                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
839                                 strlen(portal_rotors[i].pr_name)) == 0) {
840                         portal_rotor = portal_rotors[i].pr_value;
841                         rc = 0;
842                         break;
843                 }
844         }
845         lnet_res_unlock(0);
846 out:
847         LIBCFS_FREE(buf, buf_len);
848         return rc;
849 }
850
851 static int
852 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
853                        size_t *lenp, loff_t *ppos)
854 {
855         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
856                                     __proc_lnet_portal_rotor);
857 }
858
859
860 static struct ctl_table lnet_table[] = {
861         /*
862          * NB No .strategy entries have been provided since sysctl(8) prefers
863          * to go via /proc for portability.
864          */
865         {
866                 INIT_CTL_NAME
867                 .procname       = "stats",
868                 .mode           = 0644,
869                 .proc_handler   = &proc_lnet_stats,
870         },
871         {
872                 INIT_CTL_NAME
873                 .procname       = "routes",
874                 .mode           = 0444,
875                 .proc_handler   = &proc_lnet_routes,
876         },
877         {
878                 INIT_CTL_NAME
879                 .procname       = "routers",
880                 .mode           = 0444,
881                 .proc_handler   = &proc_lnet_routers,
882         },
883         {
884                 INIT_CTL_NAME
885                 .procname       = "peers",
886                 .mode           = 0444,
887                 .proc_handler   = &proc_lnet_peers,
888         },
889         {
890                 INIT_CTL_NAME
891                 .procname       = "buffers",
892                 .mode           = 0444,
893                 .proc_handler   = &proc_lnet_buffers,
894         },
895         {
896                 INIT_CTL_NAME
897                 .procname       = "nis",
898                 .mode           = 0444,
899                 .proc_handler   = &proc_lnet_nis,
900         },
901         {
902                 INIT_CTL_NAME
903                 .procname       = "portal_rotor",
904                 .mode           = 0644,
905                 .proc_handler   = &proc_lnet_portal_rotor,
906         },
907         { 0 }
908 };
909
910 static struct ctl_table top_table[] = {
911         {
912                 INIT_CTL_NAME
913                 .procname       = "lnet",
914                 .mode           = 0555,
915                 .data           = NULL,
916                 .maxlen         = 0,
917                 .child          = lnet_table,
918         },
919         { 0 }
920 };
921
922 void
923 lnet_proc_init(void)
924 {
925 #ifdef CONFIG_SYSCTL
926         if (lnet_table_header == NULL)
927                 lnet_table_header = register_sysctl_table(top_table);
928 #endif
929 }
930
931 void
932 lnet_proc_fini(void)
933 {
934 #ifdef CONFIG_SYSCTL
935         if (lnet_table_header != NULL)
936                 unregister_sysctl_table(lnet_table_header);
937
938         lnet_table_header = NULL;
939 #endif
940 }