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