Whamcloud - gitweb
LU-1425 build: make Lustre build ready for gcov
[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, Whamcloud, Inc.
5  *
6  *   This file is part of Portals
7  *   http://sourceforge.net/projects/sandiaportals/
8  *
9  *   Portals is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Portals is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Portals; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #define DEBUG_SUBSYSTEM S_LNET
25 #include <libcfs/libcfs.h>
26 #include <lnet/lib-lnet.h>
27
28 #if defined(__KERNEL__) && defined(LNET_ROUTER)
29
30 /* This is really lnet_proc.c. You might need to update sanity test 215
31  * if any file format is changed. */
32
33 static cfs_sysctl_table_header_t *lnet_table_header = NULL;
34
35 #ifndef HAVE_SYSCTL_UNNUMBERED
36 #define CTL_LNET         (0x100)
37 enum {
38         PSDEV_LNET_STATS = 100,
39         PSDEV_LNET_ROUTES,
40         PSDEV_LNET_ROUTERS,
41         PSDEV_LNET_PEERS,
42         PSDEV_LNET_BUFFERS,
43         PSDEV_LNET_NIS,
44         PSDEV_LNET_PTL_ROTOR,
45 };
46 #else
47 #define CTL_LNET           CTL_UNNUMBERED
48 #define PSDEV_LNET_STATS   CTL_UNNUMBERED
49 #define PSDEV_LNET_ROUTES  CTL_UNNUMBERED
50 #define PSDEV_LNET_ROUTERS CTL_UNNUMBERED
51 #define PSDEV_LNET_PEERS   CTL_UNNUMBERED
52 #define PSDEV_LNET_BUFFERS CTL_UNNUMBERED
53 #define PSDEV_LNET_NIS     CTL_UNNUMBERED
54 #define PSDEV_LNET_PTL_ROTOR    CTL_UNNUMBERED
55 #endif
56
57 #define LNET_LOFFT_BITS         (sizeof(loff_t) * 8)
58 /*
59  * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
60  */
61 #define LNET_PROC_CPT_BITS      (LNET_CPT_BITS + 1)
62 /* change version, 16 bits or 8 bits */
63 #define LNET_PROC_VER_BITS      MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
64
65 #define LNET_PROC_HASH_BITS     LNET_PEER_HASH_BITS
66 /*
67  * bits for peer hash offset
68  * NB: we don't use the highest bit of *ppos because it's signed
69  */
70 #define LNET_PROC_HOFF_BITS     (LNET_LOFFT_BITS -       \
71                                  LNET_PROC_CPT_BITS -    \
72                                  LNET_PROC_VER_BITS -    \
73                                  LNET_PROC_HASH_BITS - 1)
74 /* bits for hash index + position */
75 #define LNET_PROC_HPOS_BITS     (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
76 /* bits for peer hash table + hash version */
77 #define LNET_PROC_VPOS_BITS     (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
78
79 #define LNET_PROC_CPT_MASK      ((1ULL << LNET_PROC_CPT_BITS) - 1)
80 #define LNET_PROC_VER_MASK      ((1ULL << LNET_PROC_VER_BITS) - 1)
81 #define LNET_PROC_HASH_MASK     ((1ULL << LNET_PROC_HASH_BITS) - 1)
82 #define LNET_PROC_HOFF_MASK     ((1ULL << LNET_PROC_HOFF_BITS) - 1)
83
84 #define LNET_PROC_CPT_GET(pos)                          \
85         (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
86
87 #define LNET_PROC_VER_GET(pos)                          \
88         (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
89
90 #define LNET_PROC_HASH_GET(pos)                         \
91         (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
92
93 #define LNET_PROC_HOFF_GET(pos)                         \
94         (int)((pos) & LNET_PROC_HOFF_MASK)
95
96 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off)         \
97         (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) |   \
98         ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) |   \
99         ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
100         ((off) & LNET_PROC_HOFF_MASK))
101
102 #define LNET_PROC_VERSION(v)    ((unsigned int)((v) & LNET_PROC_VER_MASK))
103
104 static int __proc_lnet_stats(void *data, int write,
105                              loff_t pos, void *buffer, int nob)
106 {
107         int              rc;
108         lnet_counters_t *ctrs;
109         int              len;
110         char            *tmpstr;
111         const int        tmpsiz = 256; /* 7 %u and 4 LPU64 */
112
113         if (write) {
114                 lnet_counters_reset();
115                 return 0;
116         }
117
118         /* read */
119
120         LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
121         if (ctrs == NULL)
122                 return -ENOMEM;
123
124         LIBCFS_ALLOC(tmpstr, tmpsiz);
125         if (tmpstr == NULL) {
126                 LIBCFS_FREE(ctrs, sizeof(*ctrs));
127                 return -ENOMEM;
128         }
129
130         lnet_counters_get(ctrs);
131
132         len = snprintf(tmpstr, tmpsiz,
133                        "%u %u %u %u %u %u %u "LPU64" "LPU64" "
134                        LPU64" "LPU64,
135                        ctrs->msgs_alloc, ctrs->msgs_max,
136                        ctrs->errors,
137                        ctrs->send_count, ctrs->recv_count,
138                        ctrs->route_count, ctrs->drop_count,
139                        ctrs->send_length, ctrs->recv_length,
140                        ctrs->route_length, ctrs->drop_length);
141
142         if (pos >= min_t(int, len, strlen(tmpstr)))
143                 rc = 0;
144         else
145                 rc = cfs_trace_copyout_string(buffer, nob,
146                                               tmpstr + pos, "\n");
147
148         LIBCFS_FREE(tmpstr, tmpsiz);
149         LIBCFS_FREE(ctrs, sizeof(*ctrs));
150         return rc;
151 }
152
153 DECLARE_PROC_HANDLER(proc_lnet_stats);
154
155 int LL_PROC_PROTO(proc_lnet_routes)
156 {
157         const int       tmpsiz = 256;
158         char            *tmpstr;
159         char            *s;
160         int             rc = 0;
161         int             len;
162         int             ver;
163         int             off;
164
165         DECLARE_LL_PROC_PPOS_DECL;
166
167         CLASSERT(sizeof(loff_t) >= 4);
168
169         off = LNET_PROC_HOFF_GET(*ppos);
170         ver = LNET_PROC_VER_GET(*ppos);
171
172         LASSERT (!write);
173
174         if (*lenp == 0)
175                 return 0;
176
177         LIBCFS_ALLOC(tmpstr, tmpsiz);
178         if (tmpstr == NULL)
179                 return -ENOMEM;
180
181         s = tmpstr; /* points to current position in tmpstr[] */
182
183         if (*ppos == 0) {
184                 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
185                               the_lnet.ln_routing ? "enabled" : "disabled");
186                 LASSERT (tmpstr + tmpsiz - s > 0);
187
188                 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %7s %s\n",
189                               "net", "hops", "state", "router");
190                 LASSERT (tmpstr + tmpsiz - s > 0);
191
192                 lnet_net_lock(0);
193                 ver = (unsigned int)the_lnet.ln_remote_nets_version;
194                 lnet_net_unlock(0);
195                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
196         } else {
197                 cfs_list_t              *n;
198                 cfs_list_t              *r;
199                 lnet_route_t            *route = NULL;
200                 lnet_remotenet_t        *rnet  = NULL;
201                 int                     skip  = off - 1;
202
203                 lnet_net_lock(0);
204
205                 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
206                         lnet_net_unlock(0);
207                         LIBCFS_FREE(tmpstr, tmpsiz);
208                         return -ESTALE;
209                 }
210
211                 n = the_lnet.ln_remote_nets.next;
212
213                 while (n != &the_lnet.ln_remote_nets && route == NULL) {
214                         rnet = cfs_list_entry(n, lnet_remotenet_t, lrn_list);
215
216                         r = rnet->lrn_routes.next;
217
218                         while (r != &rnet->lrn_routes) {
219                                 lnet_route_t *re =
220                                         cfs_list_entry(r, lnet_route_t,
221                                                        lr_list);
222                                 if (skip == 0) {
223                                         route = re;
224                                         break;
225                                 }
226
227                                 skip--;
228                                 r = r->next;
229                         }
230
231                         n = n->next;
232                 }
233
234                 if (route != NULL) {
235                         __u32        net   = rnet->lrn_net;
236                         unsigned int hops  = route->lr_hops;
237                         lnet_nid_t   nid   = route->lr_gateway->lp_nid;
238                         int          alive = route->lr_gateway->lp_alive;
239
240                         s += snprintf(s, tmpstr + tmpsiz - s,
241                                       "%-8s %4u %7s %s\n",
242                                       libcfs_net2str(net), hops,
243                                       alive ? "up" : "down",
244                                       libcfs_nid2str(nid));
245                         LASSERT(tmpstr + tmpsiz - s > 0);
246                 }
247
248                 lnet_net_unlock(0);
249         }
250
251         len = s - tmpstr;     /* how many bytes was written */
252
253         if (len > *lenp) {    /* linux-supplied buffer is too small */
254                 rc = -EINVAL;
255         } else if (len > 0) { /* wrote something */
256                 if (cfs_copy_to_user(buffer, tmpstr, len))
257                         rc = -EFAULT;
258                 else {
259                         off += 1;
260                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
261                 }
262         }
263
264         LIBCFS_FREE(tmpstr, tmpsiz);
265
266         if (rc == 0)
267                 *lenp = len;
268
269         return rc;
270 }
271
272 int LL_PROC_PROTO(proc_lnet_routers)
273 {
274         int        rc = 0;
275         char      *tmpstr;
276         char      *s;
277         const int  tmpsiz = 256;
278         int        len;
279         int        ver;
280         int        off;
281
282         DECLARE_LL_PROC_PPOS_DECL;
283
284         off = LNET_PROC_HOFF_GET(*ppos);
285         ver = LNET_PROC_VER_GET(*ppos);
286
287         LASSERT (!write);
288
289         if (*lenp == 0)
290                 return 0;
291
292         LIBCFS_ALLOC(tmpstr, tmpsiz);
293         if (tmpstr == NULL)
294                 return -ENOMEM;
295
296         s = tmpstr; /* points to current position in tmpstr[] */
297
298         if (*ppos == 0) {
299                 s += snprintf(s, tmpstr + tmpsiz - s,
300                               "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
301                               "ref", "rtr_ref", "alive_cnt", "state",
302                               "last_ping", "ping_sent", "deadline",
303                               "down_ni", "router");
304                 LASSERT(tmpstr + tmpsiz - s > 0);
305
306                 lnet_net_lock(0);
307                 ver = (unsigned int)the_lnet.ln_routers_version;
308                 lnet_net_unlock(0);
309                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
310         } else {
311                 cfs_list_t              *r;
312                 struct lnet_peer        *peer = NULL;
313                 int                     skip = off - 1;
314
315                 lnet_net_lock(0);
316
317                 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
318                         lnet_net_unlock(0);
319
320                         LIBCFS_FREE(tmpstr, tmpsiz);
321                         return -ESTALE;
322                 }
323
324                 r = the_lnet.ln_routers.next;
325
326                 while (r != &the_lnet.ln_routers) {
327                         lnet_peer_t *lp = cfs_list_entry(r, lnet_peer_t,
328                                                          lp_rtr_list);
329
330                         if (skip == 0) {
331                                 peer = lp;
332                                 break;
333                         }
334
335                         skip--;
336                         r = r->next;
337                 }
338
339                 if (peer != NULL) {
340                         lnet_nid_t nid = peer->lp_nid;
341                         cfs_time_t now = cfs_time_current();
342                         cfs_time_t deadline = peer->lp_ping_deadline;
343                         int nrefs     = peer->lp_refcount;
344                         int nrtrrefs  = peer->lp_rtr_refcount;
345                         int alive_cnt = peer->lp_alive_count;
346                         int alive     = peer->lp_alive;
347                         int pingsent  = !peer->lp_ping_notsent;
348                         int last_ping = cfs_duration_sec(cfs_time_sub(now,
349                                                      peer->lp_ping_timestamp));
350                         int down_ni   = 0;
351                         lnet_route_t *rtr;
352
353                         if ((peer->lp_ping_feats &
354                              LNET_PING_FEAT_NI_STATUS) != 0) {
355                                 cfs_list_for_each_entry(rtr, &peer->lp_routes,
356                                                         lr_gwlist) {
357                                         /* downis on any route should be the
358                                          * number of downis on the gateway */
359                                         if (rtr->lr_downis != 0) {
360                                                 down_ni = rtr->lr_downis;
361                                                 break;
362                                         }
363                                 }
364                         }
365
366                         if (deadline == 0)
367                                 s += snprintf(s, tmpstr + tmpsiz - s,
368                                               "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
369                                               nrefs, nrtrrefs, alive_cnt,
370                                               alive ? "up" : "down", last_ping,
371                                               pingsent, "NA", down_ni,
372                                               libcfs_nid2str(nid));
373                         else
374                                 s += snprintf(s, tmpstr + tmpsiz - s,
375                                               "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
376                                               nrefs, nrtrrefs, alive_cnt,
377                                               alive ? "up" : "down", last_ping,
378                                               pingsent,
379                                               cfs_duration_sec(cfs_time_sub(deadline, now)),
380                                               down_ni, libcfs_nid2str(nid));
381                         LASSERT (tmpstr + tmpsiz - s > 0);
382                 }
383
384                 lnet_net_unlock(0);
385         }
386
387         len = s - tmpstr;     /* how many bytes was written */
388
389         if (len > *lenp) {    /* linux-supplied buffer is too small */
390                 rc = -EINVAL;
391         } else if (len > 0) { /* wrote something */
392                 if (cfs_copy_to_user(buffer, tmpstr, len))
393                         rc = -EFAULT;
394                 else {
395                         off += 1;
396                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
397                 }
398         }
399
400         LIBCFS_FREE(tmpstr, tmpsiz);
401
402         if (rc == 0)
403                 *lenp = len;
404
405         return rc;
406 }
407
408 int LL_PROC_PROTO(proc_lnet_peers)
409 {
410         const int               tmpsiz  = 256;
411         struct lnet_peer_table  *ptable;
412         char                    *tmpstr;
413         char                    *s;
414         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
415         int                     ver  = LNET_PROC_VER_GET(*ppos);
416         int                     hash = LNET_PROC_HASH_GET(*ppos);
417         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
418         int                     rc = 0;
419         int                     len;
420
421         CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
422         LASSERT(!write);
423
424         if (*lenp == 0)
425                 return 0;
426
427         if (cpt >= LNET_CPT_NUMBER) {
428                 *lenp = 0;
429                 return 0;
430         }
431
432         LIBCFS_ALLOC(tmpstr, tmpsiz);
433         if (tmpstr == NULL)
434                 return -ENOMEM;
435
436         s = tmpstr; /* points to current position in tmpstr[] */
437
438         if (*ppos == 0) {
439                 s += snprintf(s, tmpstr + tmpsiz - s,
440                               "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
441                               "nid", "refs", "state", "last", "max",
442                               "rtr", "min", "tx", "min", "queue");
443                 LASSERT (tmpstr + tmpsiz - s > 0);
444
445                 hoff++;
446         } else {
447                 struct lnet_peer        *peer;
448                 cfs_list_t              *p;
449                 int                     skip;
450  again:
451                 p = NULL;
452                 peer = NULL;
453                 skip = hoff - 1;
454
455                 lnet_net_lock(cpt);
456                 ptable = the_lnet.ln_peer_tables[cpt];
457                 if (hoff == 1)
458                         ver = LNET_PROC_VERSION(ptable->pt_version);
459
460                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
461                         lnet_net_unlock(cpt);
462                         LIBCFS_FREE(tmpstr, tmpsiz);
463                         return -ESTALE;
464                 }
465
466                 while (hash < LNET_PEER_HASH_SIZE) {
467                         if (p == NULL)
468                                 p = ptable->pt_hash[hash].next;
469
470                         while (p != &ptable->pt_hash[hash]) {
471                                 lnet_peer_t *lp = cfs_list_entry(p, lnet_peer_t,
472                                                                  lp_hashlist);
473                                 if (skip == 0) {
474                                         peer = lp;
475
476                                         /* minor optimization: start from idx+1
477                                          * on next iteration if we've just
478                                          * drained lp_hashlist */
479                                         if (lp->lp_hashlist.next ==
480                                             &ptable->pt_hash[hash]) {
481                                                 hoff = 1;
482                                                 hash++;
483                                         } else {
484                                                 hoff++;
485                                         }
486
487                                         break;
488                                 }
489
490                                 skip--;
491                                 p = lp->lp_hashlist.next;
492                         }
493
494                         if (peer != NULL)
495                                 break;
496
497                         p = NULL;
498                         hoff = 1;
499                         hash++;
500                 }
501
502                 if (peer != NULL) {
503                         lnet_nid_t nid       = peer->lp_nid;
504                         int        nrefs     = peer->lp_refcount;
505                         int        lastalive = -1;
506                         char      *aliveness = "NA";
507                         int        maxcr     = peer->lp_ni->ni_peertxcredits;
508                         int        txcr      = peer->lp_txcredits;
509                         int        mintxcr   = peer->lp_mintxcredits;
510                         int        rtrcr     = peer->lp_rtrcredits;
511                         int        minrtrcr  = peer->lp_minrtrcredits;
512                         int        txqnob    = peer->lp_txqnob;
513
514                         if (lnet_isrouter(peer) ||
515                             lnet_peer_aliveness_enabled(peer))
516                                 aliveness = peer->lp_alive ? "up" : "down";
517
518                         if (lnet_peer_aliveness_enabled(peer)) {
519                                 cfs_time_t     now = cfs_time_current();
520                                 cfs_duration_t delta;
521
522                                 delta = cfs_time_sub(now, peer->lp_last_alive);
523                                 lastalive = cfs_duration_sec(delta);
524
525                                 /* No need to mess up peers contents with
526                                  * arbitrarily long integers - it suffices to
527                                  * know that lastalive is more than 10000s old
528                                  */
529                                 if (lastalive >= 10000)
530                                         lastalive = 9999;
531                         }
532
533                         lnet_net_unlock(cpt);
534
535                         s += snprintf(s, tmpstr + tmpsiz - s,
536                                       "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
537                                       libcfs_nid2str(nid), nrefs, aliveness,
538                                       lastalive, maxcr, rtrcr, minrtrcr, txcr,
539                                       mintxcr, txqnob);
540                         LASSERT (tmpstr + tmpsiz - s > 0);
541
542                 } else { /* peer is NULL */
543                         lnet_net_unlock(cpt);
544                 }
545
546                 if (hash == LNET_PEER_HASH_SIZE) {
547                         cpt++;
548                         hash = 0;
549                         hoff = 1;
550                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
551                                 goto again;
552                 }
553         }
554
555         len = s - tmpstr;     /* how many bytes was written */
556
557         if (len > *lenp) {    /* linux-supplied buffer is too small */
558                 rc = -EINVAL;
559         } else if (len > 0) { /* wrote something */
560                 if (cfs_copy_to_user(buffer, tmpstr, len))
561                         rc = -EFAULT;
562                 else
563                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
564         }
565
566         LIBCFS_FREE(tmpstr, tmpsiz);
567
568         if (rc == 0)
569                 *lenp = len;
570
571         return rc;
572 }
573
574 static int __proc_lnet_buffers(void *data, int write,
575                                loff_t pos, void *buffer, int nob)
576 {
577         char            *s;
578         char            *tmpstr;
579         int             tmpsiz;
580         int             idx;
581         int             len;
582         int             rc;
583         int             i;
584
585         LASSERT(!write);
586
587         /* (4 %d) * 4 * LNET_CPT_NUMBER */
588         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
589         LIBCFS_ALLOC(tmpstr, tmpsiz);
590         if (tmpstr == NULL)
591                 return -ENOMEM;
592
593         s = tmpstr; /* points to current position in tmpstr[] */
594
595         s += snprintf(s, tmpstr + tmpsiz - s,
596                       "%5s %5s %7s %7s\n",
597                       "pages", "count", "credits", "min");
598         LASSERT (tmpstr + tmpsiz - s > 0);
599
600         if (the_lnet.ln_rtrpools == NULL)
601                 goto out; /* I'm not a router */
602
603         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
604                 lnet_rtrbufpool_t *rbp;
605
606                 lnet_net_lock(LNET_LOCK_EX);
607                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
608                         s += snprintf(s, tmpstr + tmpsiz - s,
609                                       "%5d %5d %7d %7d\n",
610                                       rbp[idx].rbp_npages,
611                                       rbp[idx].rbp_nbuffers,
612                                       rbp[idx].rbp_credits,
613                                       rbp[idx].rbp_mincredits);
614                         LASSERT(tmpstr + tmpsiz - s > 0);
615                 }
616                 lnet_net_unlock(LNET_LOCK_EX);
617         }
618
619  out:
620         len = s - tmpstr;
621
622         if (pos >= min_t(int, len, strlen(tmpstr)))
623                 rc = 0;
624         else
625                 rc = cfs_trace_copyout_string(buffer, nob,
626                                               tmpstr + pos, NULL);
627
628         LIBCFS_FREE(tmpstr, tmpsiz);
629         return rc;
630 }
631
632 DECLARE_PROC_HANDLER(proc_lnet_buffers);
633
634 int LL_PROC_PROTO(proc_lnet_nis)
635 {
636         int     tmpsiz = 128 * LNET_CPT_NUMBER;
637         int        rc = 0;
638         char      *tmpstr;
639         char      *s;
640         int        len;
641
642         DECLARE_LL_PROC_PPOS_DECL;
643
644         LASSERT (!write);
645
646         if (*lenp == 0)
647                 return 0;
648
649         LIBCFS_ALLOC(tmpstr, tmpsiz);
650         if (tmpstr == NULL)
651                 return -ENOMEM;
652
653         s = tmpstr; /* points to current position in tmpstr[] */
654
655         if (*ppos == 0) {
656                 s += snprintf(s, tmpstr + tmpsiz - s,
657                               "%-24s %6s %5s %4s %4s %4s %5s %5s %5s\n",
658                               "nid", "status", "alive", "refs", "peer",
659                               "rtr", "max", "tx", "min");
660                 LASSERT (tmpstr + tmpsiz - s > 0);
661         } else {
662                 cfs_list_t        *n;
663                 lnet_ni_t         *ni   = NULL;
664                 int                skip = *ppos - 1;
665
666                 lnet_net_lock(0);
667
668                 n = the_lnet.ln_nis.next;
669
670                 while (n != &the_lnet.ln_nis) {
671                         lnet_ni_t *a_ni = cfs_list_entry(n, lnet_ni_t, ni_list);
672
673                         if (skip == 0) {
674                                 ni = a_ni;
675                                 break;
676                         }
677
678                         skip--;
679                         n = n->next;
680                 }
681
682                 if (ni != NULL) {
683                         struct lnet_tx_queue    *tq;
684                         char    *stat;
685                         long    now = cfs_time_current_sec();
686                         int     last_alive = -1;
687                         int     i;
688                         int     j;
689
690                         if (the_lnet.ln_routing)
691                                 last_alive = now - ni->ni_last_alive;
692
693                         /* @lo forever alive */
694                         if (ni->ni_lnd->lnd_type == LOLND)
695                                 last_alive = 0;
696
697                         lnet_ni_lock(ni);
698                         LASSERT(ni->ni_status != NULL);
699                         stat = (ni->ni_status->ns_status ==
700                                 LNET_NI_STATUS_UP) ? "up" : "down";
701                         lnet_ni_unlock(ni);
702
703                         /* we actually output credits information for
704                          * TX queue of each partition */
705                         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
706                                 for (j = 0; ni->ni_cpts != NULL &&
707                                      j < ni->ni_ncpts; j++) {
708                                         if (i == ni->ni_cpts[j])
709                                                 break;
710                                 }
711
712                                 if (j == ni->ni_ncpts)
713                                         continue;
714
715                                 if (i != 0)
716                                         lnet_net_lock(i);
717
718                                 s += snprintf(s, tmpstr + tmpsiz - s,
719                                       "%-24s %6s %5d %4d %4d %4d %5d %5d %5d\n",
720                                       libcfs_nid2str(ni->ni_nid), stat,
721                                       last_alive, *ni->ni_refs[i],
722                                       ni->ni_peertxcredits,
723                                       ni->ni_peerrtrcredits,
724                                       tq->tq_credits_max,
725                                       tq->tq_credits, tq->tq_credits_min);
726                                 if (i != 0)
727                                         lnet_net_unlock(i);
728                         }
729                         LASSERT(tmpstr + tmpsiz - s > 0);
730                 }
731
732                 lnet_net_unlock(0);
733         }
734
735         len = s - tmpstr;     /* how many bytes was written */
736
737         if (len > *lenp) {    /* linux-supplied buffer is too small */
738                 rc = -EINVAL;
739         } else if (len > 0) { /* wrote something */
740                 if (cfs_copy_to_user(buffer, tmpstr, len))
741                         rc = -EFAULT;
742                 else
743                         *ppos += 1;
744         }
745
746         LIBCFS_FREE(tmpstr, tmpsiz);
747
748         if (rc == 0)
749                 *lenp = len;
750
751         return rc;
752 }
753
754 struct lnet_portal_rotors {
755         int             pr_value;
756         const char      *pr_name;
757         const char      *pr_desc;
758 };
759
760 static struct lnet_portal_rotors        portal_rotors[] = {
761         {
762                 .pr_value = LNET_PTL_ROTOR_OFF,
763                 .pr_name  = "OFF",
764                 .pr_desc  = "Turn off message rotor for wildcard portals"
765         },
766         {
767                 .pr_value = LNET_PTL_ROTOR_ON,
768                 .pr_name  = "ON",
769                 .pr_desc  = "round-robin dispatch all PUT messages for "
770                             "wildcard portals"
771         },
772         {
773                 .pr_value = LNET_PTL_ROTOR_RR_RT,
774                 .pr_name  = "RR_RT",
775                 .pr_desc  = "round-robin dispatch routed PUT message for "
776                             "wildcard portals"
777         },
778         {
779                 .pr_value = LNET_PTL_ROTOR_HASH_RT,
780                 .pr_name  = "HASH_RT",
781                 .pr_desc  = "dispatch routed PUT message by hashing source "
782                             "NID for wildcard portals"
783         },
784         {
785                 .pr_value = -1,
786                 .pr_name  = NULL,
787                 .pr_desc  = NULL
788         },
789 };
790
791 extern int portal_rotor;
792
793 static int __proc_lnet_portal_rotor(void *data, int write,
794                                     loff_t pos, void *buffer, int nob)
795 {
796         const int       buf_len = 128;
797         char            *buf;
798         char            *tmp;
799         int             rc;
800         int             i;
801
802         LIBCFS_ALLOC(buf, buf_len);
803         if (buf == NULL)
804                 return -ENOMEM;
805
806         if (!write) {
807                 lnet_res_lock(0);
808
809                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
810                         if (portal_rotors[i].pr_value == portal_rotor)
811                                 break;
812                 }
813
814                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
815                 lnet_res_unlock(0);
816
817                 rc = snprintf(buf, buf_len,
818                               "{\n\tportals: all\n"
819                               "\trotor: %s\n\tdescription: %s\n}",
820                               portal_rotors[i].pr_name,
821                               portal_rotors[i].pr_desc);
822
823                 if (pos >= min_t(int, rc, buf_len)) {
824                         rc = 0;
825                 } else {
826                         rc = cfs_trace_copyout_string(buffer, nob,
827                                         buf + pos, "\n");
828                 }
829                 goto out;
830         }
831
832         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
833         if (rc < 0)
834                 goto out;
835
836         tmp = cfs_trimwhite(buf);
837
838         rc = -EINVAL;
839         lnet_res_lock(0);
840         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
841                 if (cfs_strncasecmp(portal_rotors[i].pr_name, tmp,
842                                     strlen(portal_rotors[i].pr_name)) == 0) {
843                         portal_rotor = portal_rotors[i].pr_value;
844                         rc = 0;
845                         break;
846                 }
847         }
848         lnet_res_unlock(0);
849 out:
850         LIBCFS_FREE(buf, buf_len);
851         return rc;
852 }
853 DECLARE_PROC_HANDLER(proc_lnet_portal_rotor);
854
855 static cfs_sysctl_table_t lnet_table[] = {
856         /*
857          * NB No .strategy entries have been provided since sysctl(8) prefers
858          * to go via /proc for portability.
859          */
860         {
861                 INIT_CTL_NAME(PSDEV_LNET_STATS)
862                 .procname = "stats",
863                 .mode     = 0644,
864                 .proc_handler = &proc_lnet_stats,
865         },
866         {
867                 INIT_CTL_NAME(PSDEV_LNET_ROUTES)
868                 .procname = "routes",
869                 .mode     = 0444,
870                 .proc_handler = &proc_lnet_routes,
871         },
872         {
873                 INIT_CTL_NAME(PSDEV_LNET_ROUTERS)
874                 .procname = "routers",
875                 .mode     = 0444,
876                 .proc_handler = &proc_lnet_routers,
877         },
878         {
879                 INIT_CTL_NAME(PSDEV_LNET_PEERS)
880                 .procname = "peers",
881                 .mode     = 0444,
882                 .proc_handler = &proc_lnet_peers,
883         },
884         {
885                 INIT_CTL_NAME(PSDEV_LNET_PEERS)
886                 .procname = "buffers",
887                 .mode     = 0444,
888                 .proc_handler = &proc_lnet_buffers,
889         },
890         {
891                 INIT_CTL_NAME(PSDEV_LNET_NIS)
892                 .procname = "nis",
893                 .mode     = 0444,
894                 .proc_handler = &proc_lnet_nis,
895         },
896         {
897                 INIT_CTL_NAME(PSDEV_LNET_PTL_ROTOR)
898                 .procname = "portal_rotor",
899                 .mode     = 0644,
900                 .proc_handler = &proc_lnet_portal_rotor,
901         },
902         {
903                 INIT_CTL_NAME(0)
904         }
905 };
906
907 static cfs_sysctl_table_t top_table[] = {
908         {
909                 INIT_CTL_NAME(CTL_LNET)
910                 .procname = "lnet",
911                 .mode     = 0555,
912                 .data     = NULL,
913                 .maxlen   = 0,
914                 .child    = lnet_table,
915         },
916         {
917                 INIT_CTL_NAME(0)
918         }
919 };
920
921 void
922 lnet_proc_init(void)
923 {
924 #ifdef CONFIG_SYSCTL
925         if (lnet_table_header == NULL)
926                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
927 #endif
928 }
929
930 void
931 lnet_proc_fini(void)
932 {
933 #ifdef CONFIG_SYSCTL
934         if (lnet_table_header != NULL)
935                 cfs_unregister_sysctl_table(lnet_table_header);
936
937         lnet_table_header = NULL;
938 #endif
939 }
940
941 #else
942
943 void
944 lnet_proc_init(void)
945 {
946 }
947
948 void
949 lnet_proc_fini(void)
950 {
951 }
952
953 #endif