Whamcloud - gitweb
LU-6245 libcfs: remove libcfsutil.h
[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, 2013, Intel Corporation.
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 /* This is really lnet_proc.c. You might need to update sanity test 215
29  * if any file format is changed. */
30
31 static struct ctl_table_header *lnet_table_header = NULL;
32
33 #define LNET_LOFFT_BITS         (sizeof(loff_t) * 8)
34 /*
35  * NB: max allowed LNET_CPT_BITS is 8 on 64-bit system and 2 on 32-bit system
36  */
37 #define LNET_PROC_CPT_BITS      (LNET_CPT_BITS + 1)
38 /* change version, 16 bits or 8 bits */
39 #define LNET_PROC_VER_BITS      MAX(((MIN(LNET_LOFFT_BITS, 64)) / 4), 8)
40
41 #define LNET_PROC_HASH_BITS     LNET_PEER_HASH_BITS
42 /*
43  * bits for peer hash offset
44  * NB: we don't use the highest bit of *ppos because it's signed
45  */
46 #define LNET_PROC_HOFF_BITS     (LNET_LOFFT_BITS -       \
47                                  LNET_PROC_CPT_BITS -    \
48                                  LNET_PROC_VER_BITS -    \
49                                  LNET_PROC_HASH_BITS - 1)
50 /* bits for hash index + position */
51 #define LNET_PROC_HPOS_BITS     (LNET_PROC_HASH_BITS + LNET_PROC_HOFF_BITS)
52 /* bits for peer hash table + hash version */
53 #define LNET_PROC_VPOS_BITS     (LNET_PROC_HPOS_BITS + LNET_PROC_VER_BITS)
54
55 #define LNET_PROC_CPT_MASK      ((1ULL << LNET_PROC_CPT_BITS) - 1)
56 #define LNET_PROC_VER_MASK      ((1ULL << LNET_PROC_VER_BITS) - 1)
57 #define LNET_PROC_HASH_MASK     ((1ULL << LNET_PROC_HASH_BITS) - 1)
58 #define LNET_PROC_HOFF_MASK     ((1ULL << LNET_PROC_HOFF_BITS) - 1)
59
60 #define LNET_PROC_CPT_GET(pos)                          \
61         (int)(((pos) >> LNET_PROC_VPOS_BITS) & LNET_PROC_CPT_MASK)
62
63 #define LNET_PROC_VER_GET(pos)                          \
64         (int)(((pos) >> LNET_PROC_HPOS_BITS) & LNET_PROC_VER_MASK)
65
66 #define LNET_PROC_HASH_GET(pos)                         \
67         (int)(((pos) >> LNET_PROC_HOFF_BITS) & LNET_PROC_HASH_MASK)
68
69 #define LNET_PROC_HOFF_GET(pos)                         \
70         (int)((pos) & LNET_PROC_HOFF_MASK)
71
72 #define LNET_PROC_POS_MAKE(cpt, ver, hash, off)         \
73         (((((loff_t)(cpt)) & LNET_PROC_CPT_MASK) << LNET_PROC_VPOS_BITS) |   \
74         ((((loff_t)(ver)) & LNET_PROC_VER_MASK) << LNET_PROC_HPOS_BITS) |   \
75         ((((loff_t)(hash)) & LNET_PROC_HASH_MASK) << LNET_PROC_HOFF_BITS) | \
76         ((off) & LNET_PROC_HOFF_MASK))
77
78 #define LNET_PROC_VERSION(v)    ((unsigned int)((v) & LNET_PROC_VER_MASK))
79
80 static int __proc_lnet_stats(void *data, int write,
81                              loff_t pos, void __user *buffer, int nob)
82 {
83         int              rc;
84         lnet_counters_t *ctrs;
85         int              len;
86         char            *tmpstr;
87         const int        tmpsiz = 256; /* 7 %u and 4 LPU64 */
88
89         if (write) {
90                 lnet_counters_reset();
91                 return 0;
92         }
93
94         /* read */
95
96         LIBCFS_ALLOC(ctrs, sizeof(*ctrs));
97         if (ctrs == NULL)
98                 return -ENOMEM;
99
100         LIBCFS_ALLOC(tmpstr, tmpsiz);
101         if (tmpstr == NULL) {
102                 LIBCFS_FREE(ctrs, sizeof(*ctrs));
103                 return -ENOMEM;
104         }
105
106         lnet_counters_get(ctrs);
107
108         len = snprintf(tmpstr, tmpsiz,
109                        "%u %u %u %u %u %u %u "LPU64" "LPU64" "
110                        LPU64" "LPU64,
111                        ctrs->msgs_alloc, ctrs->msgs_max,
112                        ctrs->errors,
113                        ctrs->send_count, ctrs->recv_count,
114                        ctrs->route_count, ctrs->drop_count,
115                        ctrs->send_length, ctrs->recv_length,
116                        ctrs->route_length, ctrs->drop_length);
117
118         if (pos >= min_t(int, len, strlen(tmpstr)))
119                 rc = 0;
120         else
121                 rc = cfs_trace_copyout_string(buffer, nob,
122                                               tmpstr + pos, "\n");
123
124         LIBCFS_FREE(tmpstr, tmpsiz);
125         LIBCFS_FREE(ctrs, sizeof(*ctrs));
126         return rc;
127 }
128
129 static int
130 proc_lnet_stats(struct ctl_table *table, int write, void __user *buffer,
131                 size_t *lenp, loff_t *ppos)
132 {
133         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
134                                     __proc_lnet_stats);
135 }
136
137 static int
138 proc_lnet_routes(struct ctl_table *table, int write, void __user *buffer,
139                  size_t *lenp, loff_t *ppos)
140 {
141         const int       tmpsiz = 256;
142         char            *tmpstr;
143         char            *s;
144         int             rc = 0;
145         int             len;
146         int             ver;
147         int             off;
148
149         CLASSERT(sizeof(loff_t) >= 4);
150
151         off = LNET_PROC_HOFF_GET(*ppos);
152         ver = LNET_PROC_VER_GET(*ppos);
153
154         LASSERT (!write);
155
156         if (*lenp == 0)
157                 return 0;
158
159         LIBCFS_ALLOC(tmpstr, tmpsiz);
160         if (tmpstr == NULL)
161                 return -ENOMEM;
162
163         s = tmpstr; /* points to current position in tmpstr[] */
164
165         if (*ppos == 0) {
166                 s += snprintf(s, tmpstr + tmpsiz - s, "Routing %s\n",
167                               the_lnet.ln_routing ? "enabled" : "disabled");
168                 LASSERT (tmpstr + tmpsiz - s > 0);
169
170                 s += snprintf(s, tmpstr + tmpsiz - s, "%-8s %4s %8s %7s %s\n",
171                               "net", "hops", "priority", "state", "router");
172                 LASSERT (tmpstr + tmpsiz - s > 0);
173
174                 lnet_net_lock(0);
175                 ver = (unsigned int)the_lnet.ln_remote_nets_version;
176                 lnet_net_unlock(0);
177                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
178         } else {
179                 struct list_head        *n;
180                 struct list_head        *r;
181                 lnet_route_t            *route = NULL;
182                 lnet_remotenet_t        *rnet  = NULL;
183                 int                     skip  = off - 1;
184                 struct list_head        *rn_list;
185                 int                     i;
186
187                 lnet_net_lock(0);
188
189                 if (ver != LNET_PROC_VERSION(the_lnet.ln_remote_nets_version)) {
190                         lnet_net_unlock(0);
191                         LIBCFS_FREE(tmpstr, tmpsiz);
192                         return -ESTALE;
193                 }
194
195                 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE && route == NULL;
196                      i++) {
197                         rn_list = &the_lnet.ln_remote_nets_hash[i];
198
199                         n = rn_list->next;
200
201                         while (n != rn_list && route == NULL) {
202                                 rnet = list_entry(n, lnet_remotenet_t,
203                                                   lrn_list);
204
205                                 r = rnet->lrn_routes.next;
206
207                                 while (r != &rnet->lrn_routes) {
208                                         lnet_route_t *re =
209                                                 list_entry(r, lnet_route_t,
210                                                            lr_list);
211                                         if (skip == 0) {
212                                                 route = re;
213                                                 break;
214                                         }
215
216                                         skip--;
217                                         r = r->next;
218                                 }
219
220                                 n = n->next;
221                         }
222                 }
223
224                 if (route != NULL) {
225                         __u32        net        = rnet->lrn_net;
226                         unsigned int hops       = route->lr_hops;
227                         unsigned int priority   = route->lr_priority;
228                         lnet_nid_t   nid        = route->lr_gateway->lp_nid;
229                         int          alive      = lnet_is_route_alive(route);
230
231                         s += snprintf(s, tmpstr + tmpsiz - s,
232                                       "%-8s %4u %8u %7s %s\n",
233                                       libcfs_net2str(net), hops,
234                                       priority,
235                                       alive ? "up" : "down",
236                                       libcfs_nid2str(nid));
237                         LASSERT(tmpstr + tmpsiz - s > 0);
238                 }
239
240                 lnet_net_unlock(0);
241         }
242
243         len = s - tmpstr;     /* how many bytes was written */
244
245         if (len > *lenp) {    /* linux-supplied buffer is too small */
246                 rc = -EINVAL;
247         } else if (len > 0) { /* wrote something */
248                 if (copy_to_user(buffer, tmpstr, len))
249                         rc = -EFAULT;
250                 else {
251                         off += 1;
252                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
253                 }
254         }
255
256         LIBCFS_FREE(tmpstr, tmpsiz);
257
258         if (rc == 0)
259                 *lenp = len;
260
261         return rc;
262 }
263
264 static int
265 proc_lnet_routers(struct ctl_table *table, int write, void __user *buffer,
266                   size_t *lenp, loff_t *ppos)
267 {
268         int        rc = 0;
269         char      *tmpstr;
270         char      *s;
271         const int  tmpsiz = 256;
272         int        len;
273         int        ver;
274         int        off;
275
276         off = LNET_PROC_HOFF_GET(*ppos);
277         ver = LNET_PROC_VER_GET(*ppos);
278
279         LASSERT (!write);
280
281         if (*lenp == 0)
282                 return 0;
283
284         LIBCFS_ALLOC(tmpstr, tmpsiz);
285         if (tmpstr == NULL)
286                 return -ENOMEM;
287
288         s = tmpstr; /* points to current position in tmpstr[] */
289
290         if (*ppos == 0) {
291                 s += snprintf(s, tmpstr + tmpsiz - s,
292                               "%-4s %7s %9s %6s %12s %9s %8s %7s %s\n",
293                               "ref", "rtr_ref", "alive_cnt", "state",
294                               "last_ping", "ping_sent", "deadline",
295                               "down_ni", "router");
296                 LASSERT(tmpstr + tmpsiz - s > 0);
297
298                 lnet_net_lock(0);
299                 ver = (unsigned int)the_lnet.ln_routers_version;
300                 lnet_net_unlock(0);
301                 *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
302         } else {
303                 struct list_head *r;
304                 struct lnet_peer *peer = NULL;
305                 int               skip = off - 1;
306
307                 lnet_net_lock(0);
308
309                 if (ver != LNET_PROC_VERSION(the_lnet.ln_routers_version)) {
310                         lnet_net_unlock(0);
311
312                         LIBCFS_FREE(tmpstr, tmpsiz);
313                         return -ESTALE;
314                 }
315
316                 r = the_lnet.ln_routers.next;
317
318                 while (r != &the_lnet.ln_routers) {
319                         lnet_peer_t *lp = list_entry(r, lnet_peer_t,
320                                                      lp_rtr_list);
321
322                         if (skip == 0) {
323                                 peer = lp;
324                                 break;
325                         }
326
327                         skip--;
328                         r = r->next;
329                 }
330
331                 if (peer != NULL) {
332                         lnet_nid_t nid = peer->lp_nid;
333                         cfs_time_t now = cfs_time_current();
334                         cfs_time_t deadline = peer->lp_ping_deadline;
335                         int nrefs     = peer->lp_refcount;
336                         int nrtrrefs  = peer->lp_rtr_refcount;
337                         int alive_cnt = peer->lp_alive_count;
338                         int alive     = peer->lp_alive;
339                         int pingsent  = !peer->lp_ping_notsent;
340                         int last_ping = cfs_duration_sec(cfs_time_sub(now,
341                                                      peer->lp_ping_timestamp));
342                         int down_ni   = 0;
343                         lnet_route_t *rtr;
344
345                         if ((peer->lp_ping_feats &
346                              LNET_PING_FEAT_NI_STATUS) != 0) {
347                                 list_for_each_entry(rtr, &peer->lp_routes,
348                                                     lr_gwlist) {
349                                         /* downis on any route should be the
350                                          * number of downis on the gateway */
351                                         if (rtr->lr_downis != 0) {
352                                                 down_ni = rtr->lr_downis;
353                                                 break;
354                                         }
355                                 }
356                         }
357
358                         if (deadline == 0)
359                                 s += snprintf(s, tmpstr + tmpsiz - s,
360                                               "%-4d %7d %9d %6s %12d %9d %8s %7d %s\n",
361                                               nrefs, nrtrrefs, alive_cnt,
362                                               alive ? "up" : "down", last_ping,
363                                               pingsent, "NA", down_ni,
364                                               libcfs_nid2str(nid));
365                         else
366                                 s += snprintf(s, tmpstr + tmpsiz - s,
367                                               "%-4d %7d %9d %6s %12d %9d %8lu %7d %s\n",
368                                               nrefs, nrtrrefs, alive_cnt,
369                                               alive ? "up" : "down", last_ping,
370                                               pingsent,
371                                               cfs_duration_sec(cfs_time_sub(deadline, now)),
372                                               down_ni, libcfs_nid2str(nid));
373                         LASSERT (tmpstr + tmpsiz - s > 0);
374                 }
375
376                 lnet_net_unlock(0);
377         }
378
379         len = s - tmpstr;     /* how many bytes was written */
380
381         if (len > *lenp) {    /* linux-supplied buffer is too small */
382                 rc = -EINVAL;
383         } else if (len > 0) { /* wrote something */
384                 if (copy_to_user(buffer, tmpstr, len))
385                         rc = -EFAULT;
386                 else {
387                         off += 1;
388                         *ppos = LNET_PROC_POS_MAKE(0, ver, 0, off);
389                 }
390         }
391
392         LIBCFS_FREE(tmpstr, tmpsiz);
393
394         if (rc == 0)
395                 *lenp = len;
396
397         return rc;
398 }
399
400 static int
401 proc_lnet_peers(struct ctl_table *table, int write, void __user *buffer,
402                 size_t *lenp, loff_t *ppos)
403 {
404         const int               tmpsiz  = 256;
405         struct lnet_peer_table  *ptable;
406         char                    *tmpstr;
407         char                    *s;
408         int                     cpt  = LNET_PROC_CPT_GET(*ppos);
409         int                     ver  = LNET_PROC_VER_GET(*ppos);
410         int                     hash = LNET_PROC_HASH_GET(*ppos);
411         int                     hoff = LNET_PROC_HOFF_GET(*ppos);
412         int                     rc = 0;
413         int                     len;
414
415         CLASSERT(LNET_PROC_HASH_BITS >= LNET_PEER_HASH_BITS);
416         LASSERT(!write);
417
418         if (*lenp == 0)
419                 return 0;
420
421         if (cpt >= LNET_CPT_NUMBER) {
422                 *lenp = 0;
423                 return 0;
424         }
425
426         LIBCFS_ALLOC(tmpstr, tmpsiz);
427         if (tmpstr == NULL)
428                 return -ENOMEM;
429
430         s = tmpstr; /* points to current position in tmpstr[] */
431
432         if (*ppos == 0) {
433                 s += snprintf(s, tmpstr + tmpsiz - s,
434                               "%-24s %4s %5s %5s %5s %5s %5s %5s %5s %s\n",
435                               "nid", "refs", "state", "last", "max",
436                               "rtr", "min", "tx", "min", "queue");
437                 LASSERT (tmpstr + tmpsiz - s > 0);
438
439                 hoff++;
440         } else {
441                 struct lnet_peer        *peer;
442                 struct list_head        *p;
443                 int                     skip;
444  again:
445                 p = NULL;
446                 peer = NULL;
447                 skip = hoff - 1;
448
449                 lnet_net_lock(cpt);
450                 ptable = the_lnet.ln_peer_tables[cpt];
451                 if (hoff == 1)
452                         ver = LNET_PROC_VERSION(ptable->pt_version);
453
454                 if (ver != LNET_PROC_VERSION(ptable->pt_version)) {
455                         lnet_net_unlock(cpt);
456                         LIBCFS_FREE(tmpstr, tmpsiz);
457                         return -ESTALE;
458                 }
459
460                 while (hash < LNET_PEER_HASH_SIZE) {
461                         if (p == NULL)
462                                 p = ptable->pt_hash[hash].next;
463
464                         while (p != &ptable->pt_hash[hash]) {
465                                 lnet_peer_t *lp = list_entry(p, lnet_peer_t,
466                                                              lp_hashlist);
467                                 if (skip == 0) {
468                                         peer = lp;
469
470                                         /* minor optimization: start from idx+1
471                                          * on next iteration if we've just
472                                          * drained lp_hashlist */
473                                         if (lp->lp_hashlist.next ==
474                                             &ptable->pt_hash[hash]) {
475                                                 hoff = 1;
476                                                 hash++;
477                                         } else {
478                                                 hoff++;
479                                         }
480
481                                         break;
482                                 }
483
484                                 skip--;
485                                 p = lp->lp_hashlist.next;
486                         }
487
488                         if (peer != NULL)
489                                 break;
490
491                         p = NULL;
492                         hoff = 1;
493                         hash++;
494                 }
495
496                 if (peer != NULL) {
497                         lnet_nid_t nid       = peer->lp_nid;
498                         int        nrefs     = peer->lp_refcount;
499                         int        lastalive = -1;
500                         char      *aliveness = "NA";
501                         int        maxcr     = peer->lp_ni->ni_peertxcredits;
502                         int        txcr      = peer->lp_txcredits;
503                         int        mintxcr   = peer->lp_mintxcredits;
504                         int        rtrcr     = peer->lp_rtrcredits;
505                         int        minrtrcr  = peer->lp_minrtrcredits;
506                         int        txqnob    = peer->lp_txqnob;
507
508                         if (lnet_isrouter(peer) ||
509                             lnet_peer_aliveness_enabled(peer))
510                                 aliveness = peer->lp_alive ? "up" : "down";
511
512                         if (lnet_peer_aliveness_enabled(peer)) {
513                                 cfs_time_t     now = cfs_time_current();
514                                 cfs_duration_t delta;
515
516                                 delta = cfs_time_sub(now, peer->lp_last_alive);
517                                 lastalive = cfs_duration_sec(delta);
518
519                                 /* No need to mess up peers contents with
520                                  * arbitrarily long integers - it suffices to
521                                  * know that lastalive is more than 10000s old
522                                  */
523                                 if (lastalive >= 10000)
524                                         lastalive = 9999;
525                         }
526
527                         lnet_net_unlock(cpt);
528
529                         s += snprintf(s, tmpstr + tmpsiz - s,
530                                       "%-24s %4d %5s %5d %5d %5d %5d %5d %5d %d\n",
531                                       libcfs_nid2str(nid), nrefs, aliveness,
532                                       lastalive, maxcr, rtrcr, minrtrcr, txcr,
533                                       mintxcr, txqnob);
534                         LASSERT (tmpstr + tmpsiz - s > 0);
535
536                 } else { /* peer is NULL */
537                         lnet_net_unlock(cpt);
538                 }
539
540                 if (hash == LNET_PEER_HASH_SIZE) {
541                         cpt++;
542                         hash = 0;
543                         hoff = 1;
544                         if (peer == NULL && cpt < LNET_CPT_NUMBER)
545                                 goto again;
546                 }
547         }
548
549         len = s - tmpstr;     /* how many bytes was written */
550
551         if (len > *lenp) {    /* linux-supplied buffer is too small */
552                 rc = -EINVAL;
553         } else if (len > 0) { /* wrote something */
554                 if (copy_to_user(buffer, tmpstr, len))
555                         rc = -EFAULT;
556                 else
557                         *ppos = LNET_PROC_POS_MAKE(cpt, ver, hash, hoff);
558         }
559
560         LIBCFS_FREE(tmpstr, tmpsiz);
561
562         if (rc == 0)
563                 *lenp = len;
564
565         return rc;
566 }
567
568 static int __proc_lnet_buffers(void *data, int write,
569                                loff_t pos, void __user *buffer, int nob)
570 {
571         char            *s;
572         char            *tmpstr;
573         int             tmpsiz;
574         int             idx;
575         int             len;
576         int             rc;
577         int             i;
578
579         LASSERT(!write);
580
581         /* (4 %d) * 4 * LNET_CPT_NUMBER */
582         tmpsiz = 64 * (LNET_NRBPOOLS + 1) * LNET_CPT_NUMBER;
583         LIBCFS_ALLOC(tmpstr, tmpsiz);
584         if (tmpstr == NULL)
585                 return -ENOMEM;
586
587         s = tmpstr; /* points to current position in tmpstr[] */
588
589         s += snprintf(s, tmpstr + tmpsiz - s,
590                       "%5s %5s %7s %7s\n",
591                       "pages", "count", "credits", "min");
592         LASSERT (tmpstr + tmpsiz - s > 0);
593
594         if (the_lnet.ln_rtrpools == NULL)
595                 goto out; /* I'm not a router */
596
597         for (idx = 0; idx < LNET_NRBPOOLS; idx++) {
598                 lnet_rtrbufpool_t *rbp;
599
600                 lnet_net_lock(LNET_LOCK_EX);
601                 cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
602                         s += snprintf(s, tmpstr + tmpsiz - s,
603                                       "%5d %5d %7d %7d\n",
604                                       rbp[idx].rbp_npages,
605                                       rbp[idx].rbp_nbuffers,
606                                       rbp[idx].rbp_credits,
607                                       rbp[idx].rbp_mincredits);
608                         LASSERT(tmpstr + tmpsiz - s > 0);
609                 }
610                 lnet_net_unlock(LNET_LOCK_EX);
611         }
612
613  out:
614         len = s - tmpstr;
615
616         if (pos >= min_t(int, len, strlen(tmpstr)))
617                 rc = 0;
618         else
619                 rc = cfs_trace_copyout_string(buffer, nob,
620                                               tmpstr + pos, NULL);
621
622         LIBCFS_FREE(tmpstr, tmpsiz);
623         return rc;
624 }
625
626 static int
627 proc_lnet_buffers(struct ctl_table *table, int write, void __user *buffer,
628                   size_t *lenp, loff_t *ppos)
629 {
630         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
631                                     __proc_lnet_buffers);
632 }
633
634 static int
635 proc_lnet_nis(struct ctl_table *table, int write, void __user *buffer,
636               size_t *lenp, loff_t *ppos)
637 {
638         int     tmpsiz = 128 * LNET_CPT_NUMBER;
639         int        rc = 0;
640         char      *tmpstr;
641         char      *s;
642         int        len;
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                 struct list_head  *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 = 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 (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 static int __proc_lnet_portal_rotor(void *data, int write,
792                                     loff_t pos, void __user *buffer, int nob)
793 {
794         const int       buf_len = 128;
795         char            *buf;
796         char            *tmp;
797         int             rc;
798         int             i;
799
800         LIBCFS_ALLOC(buf, buf_len);
801         if (buf == NULL)
802                 return -ENOMEM;
803
804         if (!write) {
805                 lnet_res_lock(0);
806
807                 for (i = 0; portal_rotors[i].pr_value >= 0; i++) {
808                         if (portal_rotors[i].pr_value == portal_rotor)
809                                 break;
810                 }
811
812                 LASSERT(portal_rotors[i].pr_value == portal_rotor);
813                 lnet_res_unlock(0);
814
815                 rc = snprintf(buf, buf_len,
816                               "{\n\tportals: all\n"
817                               "\trotor: %s\n\tdescription: %s\n}",
818                               portal_rotors[i].pr_name,
819                               portal_rotors[i].pr_desc);
820
821                 if (pos >= min_t(int, rc, buf_len)) {
822                         rc = 0;
823                 } else {
824                         rc = cfs_trace_copyout_string(buffer, nob,
825                                         buf + pos, "\n");
826                 }
827                 goto out;
828         }
829
830         rc = cfs_trace_copyin_string(buf, buf_len, buffer, nob);
831         if (rc < 0)
832                 goto out;
833
834         tmp = cfs_trimwhite(buf);
835
836         rc = -EINVAL;
837         lnet_res_lock(0);
838         for (i = 0; portal_rotors[i].pr_name != NULL; i++) {
839                 if (strncasecmp(portal_rotors[i].pr_name, tmp,
840                                 strlen(portal_rotors[i].pr_name)) == 0) {
841                         portal_rotor = portal_rotors[i].pr_value;
842                         rc = 0;
843                         break;
844                 }
845         }
846         lnet_res_unlock(0);
847 out:
848         LIBCFS_FREE(buf, buf_len);
849         return rc;
850 }
851
852 static int
853 proc_lnet_portal_rotor(struct ctl_table *table, int write, void __user *buffer,
854                        size_t *lenp, loff_t *ppos)
855 {
856         return lprocfs_call_handler(table->data, write, ppos, buffer, lenp,
857                                     __proc_lnet_portal_rotor);
858 }
859
860
861 static struct ctl_table lnet_table[] = {
862         /*
863          * NB No .strategy entries have been provided since sysctl(8) prefers
864          * to go via /proc for portability.
865          */
866         {
867                 INIT_CTL_NAME
868                 .procname       = "stats",
869                 .mode           = 0644,
870                 .proc_handler   = &proc_lnet_stats,
871         },
872         {
873                 INIT_CTL_NAME
874                 .procname       = "routes",
875                 .mode           = 0444,
876                 .proc_handler   = &proc_lnet_routes,
877         },
878         {
879                 INIT_CTL_NAME
880                 .procname       = "routers",
881                 .mode           = 0444,
882                 .proc_handler   = &proc_lnet_routers,
883         },
884         {
885                 INIT_CTL_NAME
886                 .procname       = "peers",
887                 .mode           = 0444,
888                 .proc_handler   = &proc_lnet_peers,
889         },
890         {
891                 INIT_CTL_NAME
892                 .procname       = "buffers",
893                 .mode           = 0444,
894                 .proc_handler   = &proc_lnet_buffers,
895         },
896         {
897                 INIT_CTL_NAME
898                 .procname       = "nis",
899                 .mode           = 0444,
900                 .proc_handler   = &proc_lnet_nis,
901         },
902         {
903                 INIT_CTL_NAME
904                 .procname       = "portal_rotor",
905                 .mode           = 0644,
906                 .proc_handler   = &proc_lnet_portal_rotor,
907         },
908         { 0 }
909 };
910
911 static struct ctl_table top_table[] = {
912         {
913                 INIT_CTL_NAME
914                 .procname       = "lnet",
915                 .mode           = 0555,
916                 .data           = NULL,
917                 .maxlen         = 0,
918                 .child          = lnet_table,
919         },
920         { 0 }
921 };
922
923 void
924 lnet_proc_init(void)
925 {
926 #ifdef CONFIG_SYSCTL
927         if (lnet_table_header == NULL)
928                 lnet_table_header = register_sysctl_table(top_table);
929 #endif
930 }
931
932 void
933 lnet_proc_fini(void)
934 {
935 #ifdef CONFIG_SYSCTL
936         if (lnet_table_header != NULL)
937                 unregister_sysctl_table(lnet_table_header);
938
939         lnet_table_header = NULL;
940 #endif
941 }