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