Whamcloud - gitweb
LU-1711 mount: obd_mount to start osd
[fs/lustre-release.git] / lnet / lnet / lib-move.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/lnet/lib-move.c
35  *
36  * Data movement routines
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <lnet/lib-lnet.h>
42
43 static int local_nid_dist_zero = 1;
44 CFS_MODULE_PARM(local_nid_dist_zero, "i", int, 0444,
45                 "Reserved");
46
47 int
48 lnet_fail_nid (lnet_nid_t nid, unsigned int threshold)
49 {
50         lnet_test_peer_t  *tp;
51         cfs_list_t        *el;
52         cfs_list_t        *next;
53         cfs_list_t         cull;
54
55         LASSERT (the_lnet.ln_init);
56
57         /* NB: use lnet_net_lock(0) to serialize operations on test peers */
58         if (threshold != 0) {
59                 /* Adding a new entry */
60                 LIBCFS_ALLOC(tp, sizeof(*tp));
61                 if (tp == NULL)
62                         return -ENOMEM;
63
64                 tp->tp_nid = nid;
65                 tp->tp_threshold = threshold;
66
67                 lnet_net_lock(0);
68                 cfs_list_add_tail(&tp->tp_list, &the_lnet.ln_test_peers);
69                 lnet_net_unlock(0);
70                 return 0;
71         }
72
73         /* removing entries */
74         CFS_INIT_LIST_HEAD(&cull);
75
76         lnet_net_lock(0);
77
78         cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
79                 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
80
81                 if (tp->tp_threshold == 0 ||    /* needs culling anyway */
82                     nid == LNET_NID_ANY ||       /* removing all entries */
83                     tp->tp_nid == nid)          /* matched this one */
84                 {
85                         cfs_list_del (&tp->tp_list);
86                         cfs_list_add (&tp->tp_list, &cull);
87                 }
88         }
89
90         lnet_net_unlock(0);
91
92         while (!cfs_list_empty (&cull)) {
93                 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
94
95                 cfs_list_del (&tp->tp_list);
96                 LIBCFS_FREE(tp, sizeof (*tp));
97         }
98         return 0;
99 }
100
101 static int
102 fail_peer (lnet_nid_t nid, int outgoing)
103 {
104         lnet_test_peer_t *tp;
105         cfs_list_t       *el;
106         cfs_list_t       *next;
107         cfs_list_t        cull;
108         int               fail = 0;
109
110         CFS_INIT_LIST_HEAD (&cull);
111
112         /* NB: use lnet_net_lock(0) to serialize operations on test peers */
113         lnet_net_lock(0);
114
115         cfs_list_for_each_safe (el, next, &the_lnet.ln_test_peers) {
116                 tp = cfs_list_entry (el, lnet_test_peer_t, tp_list);
117
118                 if (tp->tp_threshold == 0) {
119                         /* zombie entry */
120                         if (outgoing) {
121                                 /* only cull zombies on outgoing tests,
122                                  * since we may be at interrupt priority on
123                                  * incoming messages. */
124                                 cfs_list_del (&tp->tp_list);
125                                 cfs_list_add (&tp->tp_list, &cull);
126                         }
127                         continue;
128                 }
129
130                 if (tp->tp_nid == LNET_NID_ANY || /* fail every peer */
131                     nid == tp->tp_nid) {        /* fail this peer */
132                         fail = 1;
133
134                         if (tp->tp_threshold != LNET_MD_THRESH_INF) {
135                                 tp->tp_threshold--;
136                                 if (outgoing &&
137                                     tp->tp_threshold == 0) {
138                                         /* see above */
139                                         cfs_list_del (&tp->tp_list);
140                                         cfs_list_add (&tp->tp_list, &cull);
141                                 }
142                         }
143                         break;
144                 }
145         }
146
147         lnet_net_unlock(0);
148
149         while (!cfs_list_empty (&cull)) {
150                 tp = cfs_list_entry (cull.next, lnet_test_peer_t, tp_list);
151                 cfs_list_del (&tp->tp_list);
152
153                 LIBCFS_FREE(tp, sizeof (*tp));
154         }
155
156         return (fail);
157 }
158
159 unsigned int
160 lnet_iov_nob (unsigned int niov, struct iovec *iov)
161 {
162         unsigned int nob = 0;
163
164         while (niov-- > 0)
165                 nob += (iov++)->iov_len;
166
167         return (nob);
168 }
169 EXPORT_SYMBOL(lnet_iov_nob);
170
171 void
172 lnet_copy_iov2iov (unsigned int ndiov, struct iovec *diov, unsigned int doffset,
173                    unsigned int nsiov, struct iovec *siov, unsigned int soffset,
174                    unsigned int nob)
175 {
176         /* NB diov, siov are READ-ONLY */
177         unsigned int  this_nob;
178
179         if (nob == 0)
180                 return;
181
182         /* skip complete frags before 'doffset' */
183         LASSERT (ndiov > 0);
184         while (doffset >= diov->iov_len) {
185                 doffset -= diov->iov_len;
186                 diov++;
187                 ndiov--;
188                 LASSERT (ndiov > 0);
189         }
190
191         /* skip complete frags before 'soffset' */
192         LASSERT (nsiov > 0);
193         while (soffset >= siov->iov_len) {
194                 soffset -= siov->iov_len;
195                 siov++;
196                 nsiov--;
197                 LASSERT (nsiov > 0);
198         }
199
200         do {
201                 LASSERT (ndiov > 0);
202                 LASSERT (nsiov > 0);
203                 this_nob = MIN(diov->iov_len - doffset,
204                                siov->iov_len - soffset);
205                 this_nob = MIN(this_nob, nob);
206
207                 memcpy ((char *)diov->iov_base + doffset,
208                         (char *)siov->iov_base + soffset, this_nob);
209                 nob -= this_nob;
210
211                 if (diov->iov_len > doffset + this_nob) {
212                         doffset += this_nob;
213                 } else {
214                         diov++;
215                         ndiov--;
216                         doffset = 0;
217                 }
218
219                 if (siov->iov_len > soffset + this_nob) {
220                         soffset += this_nob;
221                 } else {
222                         siov++;
223                         nsiov--;
224                         soffset = 0;
225                 }
226         } while (nob > 0);
227 }
228 EXPORT_SYMBOL(lnet_copy_iov2iov);
229
230 int
231 lnet_extract_iov (int dst_niov, struct iovec *dst,
232                   int src_niov, struct iovec *src,
233                   unsigned int offset, unsigned int len)
234 {
235         /* Initialise 'dst' to the subset of 'src' starting at 'offset',
236          * for exactly 'len' bytes, and return the number of entries.
237          * NB not destructive to 'src' */
238         unsigned int    frag_len;
239         unsigned int    niov;
240
241         if (len == 0)                           /* no data => */
242                 return (0);                     /* no frags */
243
244         LASSERT (src_niov > 0);
245         while (offset >= src->iov_len) {      /* skip initial frags */
246                 offset -= src->iov_len;
247                 src_niov--;
248                 src++;
249                 LASSERT (src_niov > 0);
250         }
251
252         niov = 1;
253         for (;;) {
254                 LASSERT (src_niov > 0);
255                 LASSERT ((int)niov <= dst_niov);
256
257                 frag_len = src->iov_len - offset;
258                 dst->iov_base = ((char *)src->iov_base) + offset;
259
260                 if (len <= frag_len) {
261                         dst->iov_len = len;
262                         return (niov);
263                 }
264
265                 dst->iov_len = frag_len;
266
267                 len -= frag_len;
268                 dst++;
269                 src++;
270                 niov++;
271                 src_niov--;
272                 offset = 0;
273         }
274 }
275 EXPORT_SYMBOL(lnet_extract_iov);
276
277 #ifndef __KERNEL__
278 unsigned int
279 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
280 {
281         LASSERT (0);
282         return (0);
283 }
284
285 void
286 lnet_copy_kiov2kiov (unsigned int ndkiov, lnet_kiov_t *dkiov, unsigned int doffset,
287                      unsigned int nskiov, lnet_kiov_t *skiov, unsigned int soffset,
288                      unsigned int nob)
289 {
290         LASSERT (0);
291 }
292
293 void
294 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
295                     unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
296                     unsigned int nob)
297 {
298         LASSERT (0);
299 }
300
301 void
302 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
303                     unsigned int niov, struct iovec *iov, unsigned int iovoffset,
304                     unsigned int nob)
305 {
306         LASSERT (0);
307 }
308
309 int
310 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
311                    int src_niov, lnet_kiov_t *src,
312                    unsigned int offset, unsigned int len)
313 {
314         LASSERT (0);
315 }
316
317 #else /* __KERNEL__ */
318
319 unsigned int
320 lnet_kiov_nob (unsigned int niov, lnet_kiov_t *kiov)
321 {
322         unsigned int  nob = 0;
323
324         while (niov-- > 0)
325                 nob += (kiov++)->kiov_len;
326
327         return (nob);
328 }
329 EXPORT_SYMBOL(lnet_kiov_nob);
330
331 void
332 lnet_copy_kiov2kiov (unsigned int ndiov, lnet_kiov_t *diov, unsigned int doffset,
333                      unsigned int nsiov, lnet_kiov_t *siov, unsigned int soffset,
334                      unsigned int nob)
335 {
336         /* NB diov, siov are READ-ONLY */
337         unsigned int    this_nob;
338         char           *daddr = NULL;
339         char           *saddr = NULL;
340
341         if (nob == 0)
342                 return;
343
344         LASSERT (!cfs_in_interrupt ());
345
346         LASSERT (ndiov > 0);
347         while (doffset >= diov->kiov_len) {
348                 doffset -= diov->kiov_len;
349                 diov++;
350                 ndiov--;
351                 LASSERT (ndiov > 0);
352         }
353
354         LASSERT (nsiov > 0);
355         while (soffset >= siov->kiov_len) {
356                 soffset -= siov->kiov_len;
357                 siov++;
358                 nsiov--;
359                 LASSERT (nsiov > 0);
360         }
361
362         do {
363                 LASSERT (ndiov > 0);
364                 LASSERT (nsiov > 0);
365                 this_nob = MIN(diov->kiov_len - doffset,
366                                siov->kiov_len - soffset);
367                 this_nob = MIN(this_nob, nob);
368
369                 if (daddr == NULL)
370                         daddr = ((char *)cfs_kmap(diov->kiov_page)) + 
371                                 diov->kiov_offset + doffset;
372                 if (saddr == NULL)
373                         saddr = ((char *)cfs_kmap(siov->kiov_page)) + 
374                                 siov->kiov_offset + soffset;
375
376                 /* Vanishing risk of kmap deadlock when mapping 2 pages.
377                  * However in practice at least one of the kiovs will be mapped
378                  * kernel pages and the map/unmap will be NOOPs */
379
380                 memcpy (daddr, saddr, this_nob);
381                 nob -= this_nob;
382
383                 if (diov->kiov_len > doffset + this_nob) {
384                         daddr += this_nob;
385                         doffset += this_nob;
386                 } else {
387                         cfs_kunmap(diov->kiov_page);
388                         daddr = NULL;
389                         diov++;
390                         ndiov--;
391                         doffset = 0;
392                 }
393
394                 if (siov->kiov_len > soffset + this_nob) {
395                         saddr += this_nob;
396                         soffset += this_nob;
397                 } else {
398                         cfs_kunmap(siov->kiov_page);
399                         saddr = NULL;
400                         siov++;
401                         nsiov--;
402                         soffset = 0;
403                 }
404         } while (nob > 0);
405
406         if (daddr != NULL)
407                 cfs_kunmap(diov->kiov_page);
408         if (saddr != NULL)
409                 cfs_kunmap(siov->kiov_page);
410 }
411 EXPORT_SYMBOL(lnet_copy_kiov2kiov);
412
413 void
414 lnet_copy_kiov2iov (unsigned int niov, struct iovec *iov, unsigned int iovoffset,
415                     unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
416                     unsigned int nob)
417 {
418         /* NB iov, kiov are READ-ONLY */
419         unsigned int    this_nob;
420         char           *addr = NULL;
421
422         if (nob == 0)
423                 return;
424
425         LASSERT (!cfs_in_interrupt ());
426
427         LASSERT (niov > 0);
428         while (iovoffset >= iov->iov_len) {
429                 iovoffset -= iov->iov_len;
430                 iov++;
431                 niov--;
432                 LASSERT (niov > 0);
433         }
434
435         LASSERT (nkiov > 0);
436         while (kiovoffset >= kiov->kiov_len) {
437                 kiovoffset -= kiov->kiov_len;
438                 kiov++;
439                 nkiov--;
440                 LASSERT (nkiov > 0);
441         }
442
443         do {
444                 LASSERT (niov > 0);
445                 LASSERT (nkiov > 0);
446                 this_nob = MIN(iov->iov_len - iovoffset,
447                                kiov->kiov_len - kiovoffset);
448                 this_nob = MIN(this_nob, nob);
449
450                 if (addr == NULL)
451                         addr = ((char *)cfs_kmap(kiov->kiov_page)) + 
452                                 kiov->kiov_offset + kiovoffset;
453
454                 memcpy ((char *)iov->iov_base + iovoffset, addr, this_nob);
455                 nob -= this_nob;
456
457                 if (iov->iov_len > iovoffset + this_nob) {
458                         iovoffset += this_nob;
459                 } else {
460                         iov++;
461                         niov--;
462                         iovoffset = 0;
463                 }
464
465                 if (kiov->kiov_len > kiovoffset + this_nob) {
466                         addr += this_nob;
467                         kiovoffset += this_nob;
468                 } else {
469                         cfs_kunmap(kiov->kiov_page);
470                         addr = NULL;
471                         kiov++;
472                         nkiov--;
473                         kiovoffset = 0;
474                 }
475
476         } while (nob > 0);
477
478         if (addr != NULL)
479                 cfs_kunmap(kiov->kiov_page);
480 }
481 EXPORT_SYMBOL(lnet_copy_kiov2iov);
482
483 void
484 lnet_copy_iov2kiov (unsigned int nkiov, lnet_kiov_t *kiov, unsigned int kiovoffset,
485                     unsigned int niov, struct iovec *iov, unsigned int iovoffset,
486                     unsigned int nob)
487 {
488         /* NB kiov, iov are READ-ONLY */
489         unsigned int    this_nob;
490         char           *addr = NULL;
491
492         if (nob == 0)
493                 return;
494
495         LASSERT (!cfs_in_interrupt ());
496
497         LASSERT (nkiov > 0);
498         while (kiovoffset >= kiov->kiov_len) {
499                 kiovoffset -= kiov->kiov_len;
500                 kiov++;
501                 nkiov--;
502                 LASSERT (nkiov > 0);
503         }
504
505         LASSERT (niov > 0);
506         while (iovoffset >= iov->iov_len) {
507                 iovoffset -= iov->iov_len;
508                 iov++;
509                 niov--;
510                 LASSERT (niov > 0);
511         }
512
513         do {
514                 LASSERT (nkiov > 0);
515                 LASSERT (niov > 0);
516                 this_nob = MIN(kiov->kiov_len - kiovoffset,
517                                iov->iov_len - iovoffset);
518                 this_nob = MIN(this_nob, nob);
519
520                 if (addr == NULL)
521                         addr = ((char *)cfs_kmap(kiov->kiov_page)) + 
522                                 kiov->kiov_offset + kiovoffset;
523
524                 memcpy (addr, (char *)iov->iov_base + iovoffset, this_nob);
525                 nob -= this_nob;
526
527                 if (kiov->kiov_len > kiovoffset + this_nob) {
528                         addr += this_nob;
529                         kiovoffset += this_nob;
530                 } else {
531                         cfs_kunmap(kiov->kiov_page);
532                         addr = NULL;
533                         kiov++;
534                         nkiov--;
535                         kiovoffset = 0;
536                 }
537
538                 if (iov->iov_len > iovoffset + this_nob) {
539                         iovoffset += this_nob;
540                 } else {
541                         iov++;
542                         niov--;
543                         iovoffset = 0;
544                 }
545         } while (nob > 0);
546
547         if (addr != NULL)
548                 cfs_kunmap(kiov->kiov_page);
549 }
550 EXPORT_SYMBOL(lnet_copy_iov2kiov);
551
552 int
553 lnet_extract_kiov (int dst_niov, lnet_kiov_t *dst,
554                    int src_niov, lnet_kiov_t *src,
555                    unsigned int offset, unsigned int len)
556 {
557         /* Initialise 'dst' to the subset of 'src' starting at 'offset',
558          * for exactly 'len' bytes, and return the number of entries.
559          * NB not destructive to 'src' */
560         unsigned int    frag_len;
561         unsigned int    niov;
562
563         if (len == 0)                           /* no data => */
564                 return (0);                     /* no frags */
565
566         LASSERT (src_niov > 0);
567         while (offset >= src->kiov_len) {      /* skip initial frags */
568                 offset -= src->kiov_len;
569                 src_niov--;
570                 src++;
571                 LASSERT (src_niov > 0);
572         }
573
574         niov = 1;
575         for (;;) {
576                 LASSERT (src_niov > 0);
577                 LASSERT ((int)niov <= dst_niov);
578
579                 frag_len = src->kiov_len - offset;
580                 dst->kiov_page = src->kiov_page;
581                 dst->kiov_offset = src->kiov_offset + offset;
582
583                 if (len <= frag_len) {
584                         dst->kiov_len = len;
585                         LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
586                         return (niov);
587                 }
588
589                 dst->kiov_len = frag_len;
590                 LASSERT (dst->kiov_offset + dst->kiov_len <= CFS_PAGE_SIZE);
591
592                 len -= frag_len;
593                 dst++;
594                 src++;
595                 niov++;
596                 src_niov--;
597                 offset = 0;
598         }
599 }
600 EXPORT_SYMBOL(lnet_extract_kiov);
601 #endif
602
603 void
604 lnet_ni_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
605              unsigned int offset, unsigned int mlen, unsigned int rlen)
606 {
607         unsigned int  niov = 0;
608         struct iovec *iov = NULL;
609         lnet_kiov_t  *kiov = NULL;
610         int           rc;
611
612         LASSERT (!cfs_in_interrupt ());
613         LASSERT (mlen == 0 || msg != NULL);
614
615         if (msg != NULL) {
616                 LASSERT(msg->msg_receiving);
617                 LASSERT(!msg->msg_sending);
618                 LASSERT(rlen == msg->msg_len);
619                 LASSERT(mlen <= msg->msg_len);
620                 LASSERT(msg->msg_offset == offset);
621                 LASSERT(msg->msg_wanted == mlen);
622
623                 msg->msg_receiving = 0;
624
625                 if (mlen != 0) {
626                         niov = msg->msg_niov;
627                         iov  = msg->msg_iov;
628                         kiov = msg->msg_kiov;
629
630                         LASSERT (niov > 0);
631                         LASSERT ((iov == NULL) != (kiov == NULL));
632                 }
633         }
634
635         rc = (ni->ni_lnd->lnd_recv)(ni, private, msg, delayed,
636                                     niov, iov, kiov, offset, mlen, rlen);
637         if (rc < 0)
638                 lnet_finalize(ni, msg, rc);
639 }
640
641 void
642 lnet_setpayloadbuffer(lnet_msg_t *msg)
643 {
644         lnet_libmd_t *md = msg->msg_md;
645
646         LASSERT (msg->msg_len > 0);
647         LASSERT (!msg->msg_routing);
648         LASSERT (md != NULL);
649         LASSERT (msg->msg_niov == 0);
650         LASSERT (msg->msg_iov == NULL);
651         LASSERT (msg->msg_kiov == NULL);
652
653         msg->msg_niov = md->md_niov;
654         if ((md->md_options & LNET_MD_KIOV) != 0)
655                 msg->msg_kiov = md->md_iov.kiov;
656         else
657                 msg->msg_iov = md->md_iov.iov;
658 }
659
660 void
661 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
662                unsigned int offset, unsigned int len) 
663 {
664         msg->msg_type = type;
665         msg->msg_target = target;
666         msg->msg_len = len;
667         msg->msg_offset = offset;
668
669         if (len != 0)
670                 lnet_setpayloadbuffer(msg);
671
672         memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
673         msg->msg_hdr.type           = cpu_to_le32(type);
674         msg->msg_hdr.dest_nid       = cpu_to_le64(target.nid);
675         msg->msg_hdr.dest_pid       = cpu_to_le32(target.pid);
676         /* src_nid will be set later */
677         msg->msg_hdr.src_pid        = cpu_to_le32(the_lnet.ln_pid);
678         msg->msg_hdr.payload_length = cpu_to_le32(len);
679 }
680
681 void
682 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
683 {
684         void   *priv = msg->msg_private;
685         int     rc;
686
687         LASSERT (!cfs_in_interrupt ());
688         LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
689                  (msg->msg_txcredit && msg->msg_peertxcredit));
690
691         rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
692         if (rc < 0)
693                 lnet_finalize(ni, msg, rc);
694 }
695
696 int
697 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
698 {
699         int     rc;
700
701         LASSERT(!msg->msg_sending);
702         LASSERT(msg->msg_receiving);
703         LASSERT(!msg->msg_rx_ready_delay);
704         LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
705
706         msg->msg_rx_ready_delay = 1;
707         rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
708                                           &msg->msg_private);
709         if (rc != 0) {
710                 CERROR("recv from %s / send to %s aborted: "
711                        "eager_recv failed %d\n",
712                        libcfs_nid2str(msg->msg_rxpeer->lp_nid),
713                        libcfs_id2str(msg->msg_target), rc);
714                 LASSERT(rc < 0); /* required by my callers */
715         }
716
717         return rc;
718 }
719
720 /* NB: caller shall hold a ref on 'lp' as I'd drop lnet_net_lock */
721 void
722 lnet_ni_query_locked(lnet_ni_t *ni, lnet_peer_t *lp)
723 {
724         cfs_time_t      last_alive = 0;
725
726         LASSERT(lnet_peer_aliveness_enabled(lp));
727         LASSERT(ni->ni_lnd->lnd_query != NULL);
728         LASSERT(the_lnet.ln_routing == 1);
729
730         lnet_net_unlock(lp->lp_cpt);
731         (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
732         lnet_net_lock(lp->lp_cpt);
733
734         lp->lp_last_query = cfs_time_current();
735
736         if (last_alive != 0) /* NI has updated timestamp */
737                 lp->lp_last_alive = last_alive;
738 }
739
740 /* NB: always called with lnet_net_lock held */
741 static inline int
742 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
743 {
744         int        alive;
745         cfs_time_t deadline;
746
747         LASSERT (lnet_peer_aliveness_enabled(lp));
748         LASSERT (the_lnet.ln_routing == 1);
749
750         /* Trust lnet_notify() if it has more recent aliveness news, but
751          * ignore the initial assumed death (see lnet_peers_start_down()).
752          */
753         if (!lp->lp_alive && lp->lp_alive_count > 0 &&
754             cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
755                 return 0;
756
757         deadline = cfs_time_add(lp->lp_last_alive,
758                                 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
759         alive = cfs_time_after(deadline, now);
760
761         /* Update obsolete lp_alive except for routers assumed to be dead
762          * initially, because router checker would update aliveness in this
763          * case, and moreover lp_last_alive at peer creation is assumed.
764          */
765         if (alive && !lp->lp_alive &&
766             !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
767                 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
768
769         return alive;
770 }
771
772
773 /* NB: returns 1 when alive, 0 when dead, negative when error;
774  *     may drop the lnet_net_lock */
775 int
776 lnet_peer_alive_locked (lnet_peer_t *lp)
777 {
778         cfs_time_t now = cfs_time_current();
779
780         /* LU-630: only router checks peer health. */
781         if (the_lnet.ln_routing == 0)
782                 return 1;
783
784         if (!lnet_peer_aliveness_enabled(lp))
785                 return -ENODEV;
786
787         if (lnet_peer_is_alive(lp, now))
788                 return 1;
789
790         /* Peer appears dead, but we should avoid frequent NI queries (at
791          * most once per lnet_queryinterval seconds). */
792         if (lp->lp_last_query != 0) {
793                 static const int lnet_queryinterval = 1;
794
795                 cfs_time_t next_query =
796                            cfs_time_add(lp->lp_last_query,
797                                         cfs_time_seconds(lnet_queryinterval));
798
799                 if (cfs_time_before(now, next_query)) {
800                         if (lp->lp_alive)
801                                 CWARN("Unexpected aliveness of peer %s: "
802                                       "%d < %d (%d/%d)\n",
803                                       libcfs_nid2str(lp->lp_nid),
804                                       (int)now, (int)next_query,
805                                       lnet_queryinterval,
806                                       lp->lp_ni->ni_peertimeout);
807                         return 0;
808                 }
809         }
810
811         /* query NI for latest aliveness news */
812         lnet_ni_query_locked(lp->lp_ni, lp);
813
814         if (lnet_peer_is_alive(lp, now))
815                 return 1;
816
817         lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
818         return 0;
819 }
820
821 int
822 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
823 {
824         /* lnet_send is going to lnet_net_unlock immediately after this,
825          * so it sets do_send FALSE and I don't do the unlock/send/lock bit.
826          * I return EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer
827          * appears dead, and 0 if sent or OK to send */
828         struct lnet_peer        *lp = msg->msg_txpeer;
829         struct lnet_ni          *ni = lp->lp_ni;
830         struct lnet_tx_queue    *tq;
831         int                     cpt;
832
833         /* non-lnet_send() callers have checked before */
834         LASSERT(!do_send || msg->msg_tx_delayed);
835         LASSERT(!msg->msg_receiving);
836         LASSERT(msg->msg_tx_committed);
837
838         cpt = msg->msg_tx_cpt;
839         tq = ni->ni_tx_queues[cpt];
840
841         /* NB 'lp' is always the next hop */
842         if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
843             lnet_peer_alive_locked(lp) == 0) {
844                 the_lnet.ln_counters[cpt]->drop_count++;
845                 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
846                 lnet_net_unlock(cpt);
847
848                 CNETERR("Dropping message for %s: peer not alive\n",
849                         libcfs_id2str(msg->msg_target));
850                 if (do_send)
851                         lnet_finalize(ni, msg, -EHOSTUNREACH);
852
853                 lnet_net_lock(cpt);
854                 return EHOSTUNREACH;
855         }
856
857         if (!msg->msg_peertxcredit) {
858                 LASSERT ((lp->lp_txcredits < 0) ==
859                          !cfs_list_empty(&lp->lp_txq));
860
861                 msg->msg_peertxcredit = 1;
862                 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
863                 lp->lp_txcredits--;
864
865                 if (lp->lp_txcredits < lp->lp_mintxcredits)
866                         lp->lp_mintxcredits = lp->lp_txcredits;
867
868                 if (lp->lp_txcredits < 0) {
869                         msg->msg_tx_delayed = 1;
870                         cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
871                         return EAGAIN;
872                 }
873         }
874
875         if (!msg->msg_txcredit) {
876                 LASSERT((tq->tq_credits < 0) ==
877                         !cfs_list_empty(&tq->tq_delayed));
878
879                 msg->msg_txcredit = 1;
880                 tq->tq_credits--;
881
882                 if (tq->tq_credits < tq->tq_credits_min)
883                         tq->tq_credits_min = tq->tq_credits;
884
885                 if (tq->tq_credits < 0) {
886                         msg->msg_tx_delayed = 1;
887                         cfs_list_add_tail(&msg->msg_list, &tq->tq_delayed);
888                         return EAGAIN;
889                 }
890         }
891
892         if (do_send) {
893                 lnet_net_unlock(cpt);
894                 lnet_ni_send(ni, msg);
895                 lnet_net_lock(cpt);
896         }
897         return 0;
898 }
899
900 #ifdef __KERNEL__
901
902 lnet_rtrbufpool_t *
903 lnet_msg2bufpool(lnet_msg_t *msg)
904 {
905         lnet_rtrbufpool_t       *rbp;
906         int                     cpt;
907
908         LASSERT(msg->msg_rx_committed);
909
910         cpt = msg->msg_rx_cpt;
911         rbp = &the_lnet.ln_rtrpools[cpt][0];
912
913         LASSERT(msg->msg_len <= LNET_MTU);
914         while (msg->msg_len > (unsigned int)rbp->rbp_npages * CFS_PAGE_SIZE) {
915                 rbp++;
916                 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
917         }
918
919         return rbp;
920 }
921
922 int
923 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
924 {
925         /* lnet_parse is going to lnet_net_unlock immediately after this, so it
926          * sets do_recv FALSE and I don't do the unlock/send/lock bit.  I
927          * return EAGAIN if msg blocked and 0 if received or OK to receive */
928         lnet_peer_t         *lp = msg->msg_rxpeer;
929         lnet_rtrbufpool_t   *rbp;
930         lnet_rtrbuf_t       *rb;
931
932         LASSERT (msg->msg_iov == NULL);
933         LASSERT (msg->msg_kiov == NULL);
934         LASSERT (msg->msg_niov == 0);
935         LASSERT (msg->msg_routing);
936         LASSERT (msg->msg_receiving);
937         LASSERT (!msg->msg_sending);
938
939         /* non-lnet_parse callers only receive delayed messages */
940         LASSERT(!do_recv || msg->msg_rx_delayed);
941
942         if (!msg->msg_peerrtrcredit) {
943                 LASSERT ((lp->lp_rtrcredits < 0) ==
944                          !cfs_list_empty(&lp->lp_rtrq));
945
946                 msg->msg_peerrtrcredit = 1;
947                 lp->lp_rtrcredits--;
948                 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
949                         lp->lp_minrtrcredits = lp->lp_rtrcredits;
950
951                 if (lp->lp_rtrcredits < 0) {
952                         /* must have checked eager_recv before here */
953                         LASSERT(msg->msg_rx_ready_delay);
954                         msg->msg_rx_delayed = 1;
955                         cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
956                         return EAGAIN;
957                 }
958         }
959
960         rbp = lnet_msg2bufpool(msg);
961
962         if (!msg->msg_rtrcredit) {
963                 LASSERT ((rbp->rbp_credits < 0) ==
964                          !cfs_list_empty(&rbp->rbp_msgs));
965
966                 msg->msg_rtrcredit = 1;
967                 rbp->rbp_credits--;
968                 if (rbp->rbp_credits < rbp->rbp_mincredits)
969                         rbp->rbp_mincredits = rbp->rbp_credits;
970
971                 if (rbp->rbp_credits < 0) {
972                         /* must have checked eager_recv before here */
973                         LASSERT(msg->msg_rx_ready_delay);
974                         msg->msg_rx_delayed = 1;
975                         cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
976                         return EAGAIN;
977                 }
978         }
979
980         LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
981         rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
982         cfs_list_del(&rb->rb_list);
983
984         msg->msg_niov = rbp->rbp_npages;
985         msg->msg_kiov = &rb->rb_kiov[0];
986
987         if (do_recv) {
988                 int cpt = msg->msg_rx_cpt;
989
990                 lnet_net_unlock(cpt);
991                 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
992                              0, msg->msg_len, msg->msg_len);
993                 lnet_net_lock(cpt);
994         }
995         return 0;
996 }
997 #endif
998
999 void
1000 lnet_return_tx_credits_locked(lnet_msg_t *msg)
1001 {
1002         lnet_peer_t     *txpeer = msg->msg_txpeer;
1003         lnet_msg_t      *msg2;
1004
1005         if (msg->msg_txcredit) {
1006                 struct lnet_ni       *ni = txpeer->lp_ni;
1007                 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1008
1009                 /* give back NI txcredits */
1010                 msg->msg_txcredit = 0;
1011
1012                 LASSERT((tq->tq_credits < 0) ==
1013                         !cfs_list_empty(&tq->tq_delayed));
1014
1015                 tq->tq_credits++;
1016                 if (tq->tq_credits <= 0) {
1017                         msg2 = cfs_list_entry(tq->tq_delayed.next,
1018                                               lnet_msg_t, msg_list);
1019                         cfs_list_del(&msg2->msg_list);
1020
1021                         LASSERT(msg2->msg_txpeer->lp_ni == ni);
1022                         LASSERT(msg2->msg_tx_delayed);
1023
1024                         (void) lnet_post_send_locked(msg2, 1);
1025                 }
1026         }
1027
1028         if (msg->msg_peertxcredit) {
1029                 /* give back peer txcredits */
1030                 msg->msg_peertxcredit = 0;
1031
1032                 LASSERT((txpeer->lp_txcredits < 0) ==
1033                         !cfs_list_empty(&txpeer->lp_txq));
1034
1035                 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1036                 LASSERT (txpeer->lp_txqnob >= 0);
1037
1038                 txpeer->lp_txcredits++;
1039                 if (txpeer->lp_txcredits <= 0) {
1040                         msg2 = cfs_list_entry(txpeer->lp_txq.next,
1041                                               lnet_msg_t, msg_list);
1042                         cfs_list_del(&msg2->msg_list);
1043
1044                         LASSERT(msg2->msg_txpeer == txpeer);
1045                         LASSERT(msg2->msg_tx_delayed);
1046
1047                         (void) lnet_post_send_locked(msg2, 1);
1048                 }
1049         }
1050
1051         if (txpeer != NULL) {
1052                 msg->msg_txpeer = NULL;
1053                 lnet_peer_decref_locked(txpeer);
1054         }
1055 }
1056
1057 void
1058 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1059 {
1060         lnet_peer_t     *rxpeer = msg->msg_rxpeer;
1061 #ifdef __KERNEL__
1062         lnet_msg_t      *msg2;
1063
1064         if (msg->msg_rtrcredit) {
1065                 /* give back global router credits */
1066                 lnet_rtrbuf_t     *rb;
1067                 lnet_rtrbufpool_t *rbp;
1068
1069                 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1070                  * there until it gets one allocated, or aborts the wait
1071                  * itself */
1072                 LASSERT (msg->msg_kiov != NULL);
1073
1074                 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1075                 rbp = rb->rb_pool;
1076                 LASSERT (rbp == lnet_msg2bufpool(msg));
1077
1078                 msg->msg_kiov = NULL;
1079                 msg->msg_rtrcredit = 0;
1080
1081                 LASSERT((rbp->rbp_credits < 0) ==
1082                         !cfs_list_empty(&rbp->rbp_msgs));
1083                 LASSERT((rbp->rbp_credits > 0) ==
1084                         !cfs_list_empty(&rbp->rbp_bufs));
1085
1086                 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1087                 rbp->rbp_credits++;
1088                 if (rbp->rbp_credits <= 0) {
1089                         msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1090                                               lnet_msg_t, msg_list);
1091                         cfs_list_del(&msg2->msg_list);
1092
1093                         (void) lnet_post_routed_recv_locked(msg2, 1);
1094                 }
1095         }
1096
1097         if (msg->msg_peerrtrcredit) {
1098                 /* give back peer router credits */
1099                 msg->msg_peerrtrcredit = 0;
1100
1101                 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1102                         !cfs_list_empty(&rxpeer->lp_rtrq));
1103
1104                 rxpeer->lp_rtrcredits++;
1105                 if (rxpeer->lp_rtrcredits <= 0) {
1106                         msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1107                                               lnet_msg_t, msg_list);
1108                         cfs_list_del(&msg2->msg_list);
1109
1110                         (void) lnet_post_routed_recv_locked(msg2, 1);
1111                 }
1112         }
1113 #else
1114         LASSERT (!msg->msg_rtrcredit);
1115         LASSERT (!msg->msg_peerrtrcredit);
1116 #endif
1117         if (rxpeer != NULL) {
1118                 msg->msg_rxpeer = NULL;
1119                 lnet_peer_decref_locked(rxpeer);
1120         }
1121 }
1122
1123 static int
1124 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1125 {
1126         lnet_peer_t *p1 = r1->lr_gateway;
1127         lnet_peer_t *p2 = r2->lr_gateway;
1128
1129         if (r1->lr_hops < r2->lr_hops)
1130                 return 1;
1131
1132         if (r1->lr_hops > r2->lr_hops)
1133                 return -1;
1134
1135         if (p1->lp_txqnob < p2->lp_txqnob)
1136                 return 1;
1137
1138         if (p1->lp_txqnob > p2->lp_txqnob)
1139                 return -1;
1140
1141         if (p1->lp_txcredits > p2->lp_txcredits)
1142                 return 1;
1143
1144         if (p1->lp_txcredits < p2->lp_txcredits)
1145                 return -1;
1146
1147         if (r1->lr_seq - r2->lr_seq <= 0)
1148                 return 1;
1149
1150         return -1;
1151 }
1152
1153 static lnet_peer_t *
1154 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1155 {
1156         lnet_remotenet_t        *rnet;
1157         lnet_route_t            *rtr;
1158         lnet_route_t            *rtr_best;
1159         lnet_route_t            *rtr_last;
1160         struct lnet_peer        *lp_best;
1161         struct lnet_peer        *lp;
1162         int                     rc;
1163
1164         /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1165          * rtr_nid nid, otherwise find the best gateway I can use */
1166
1167         rnet = lnet_find_net_locked(LNET_NIDNET(target));
1168         if (rnet == NULL)
1169                 return NULL;
1170
1171         lp_best = NULL;
1172         rtr_best = rtr_last = NULL;
1173         cfs_list_for_each_entry(rtr, &rnet->lrn_routes, lr_list) {
1174                 lp = rtr->lr_gateway;
1175
1176                 if (!lp->lp_alive || /* gateway is down */
1177                     ((lp->lp_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0 &&
1178                      rtr->lr_downis != 0)) /* NI to target is down */
1179                         continue;
1180
1181                 if (ni != NULL && lp->lp_ni != ni)
1182                         continue;
1183
1184                 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1185                         return lp;
1186
1187                 if (lp_best == NULL) {
1188                         rtr_best = rtr_last = rtr;
1189                         lp_best = lp;
1190                         continue;
1191                 }
1192
1193                 /* no protection on below fields, but it's harmless */
1194                 if (rtr_last->lr_seq - rtr->lr_seq < 0)
1195                         rtr_last = rtr;
1196
1197                 rc = lnet_compare_routes(rtr, rtr_best);
1198                 if (rc < 0)
1199                         continue;
1200
1201                 rtr_best = rtr;
1202                 lp_best = lp;
1203         }
1204
1205         /* set sequence number on the best router to the latest sequence + 1
1206          * so we can round-robin all routers, it's race and inaccurate but
1207          * harmless and functional  */
1208         if (rtr_best != NULL)
1209                 rtr_best->lr_seq = rtr_last->lr_seq + 1;
1210         return lp_best;
1211 }
1212
1213 int
1214 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1215 {
1216         lnet_nid_t              dst_nid = msg->msg_target.nid;
1217         struct lnet_ni          *src_ni;
1218         struct lnet_ni          *local_ni;
1219         struct lnet_peer        *lp;
1220         int                     cpt;
1221         int                     cpt2;
1222         int                     rc;
1223
1224         /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1225          * but we might want to use pre-determined router for ACK/REPLY
1226          * in the future */
1227         /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1228         LASSERT (msg->msg_txpeer == NULL);
1229         LASSERT (!msg->msg_sending);
1230         LASSERT (!msg->msg_target_is_router);
1231         LASSERT (!msg->msg_receiving);
1232
1233         msg->msg_sending = 1;
1234
1235         LASSERT(!msg->msg_tx_committed);
1236         cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1237  again:
1238         lnet_net_lock(cpt);
1239
1240         if (the_lnet.ln_shutdown) {
1241                 lnet_net_unlock(cpt);
1242                 return -ESHUTDOWN;
1243         }
1244
1245         if (src_nid == LNET_NID_ANY) {
1246                 src_ni = NULL;
1247         } else {
1248                 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1249                 if (src_ni == NULL) {
1250                         lnet_net_unlock(cpt);
1251                         LCONSOLE_WARN("Can't send to %s: src %s is not a "
1252                                       "local nid\n", libcfs_nid2str(dst_nid),
1253                                       libcfs_nid2str(src_nid));
1254                         return -EINVAL;
1255                 }
1256                 LASSERT (!msg->msg_routing);
1257         }
1258
1259         /* Is this for someone on a local network? */
1260         local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1261
1262         if (local_ni != NULL) {
1263                 if (src_ni == NULL) {
1264                         src_ni = local_ni;
1265                         src_nid = src_ni->ni_nid;
1266                 } else if (src_ni == local_ni) {
1267                         lnet_ni_decref_locked(local_ni, cpt);
1268                 } else {
1269                         lnet_ni_decref_locked(local_ni, cpt);
1270                         lnet_ni_decref_locked(src_ni, cpt);
1271                         lnet_net_unlock(cpt);
1272                         LCONSOLE_WARN("No route to %s via from %s\n",
1273                                       libcfs_nid2str(dst_nid),
1274                                       libcfs_nid2str(src_nid));
1275                         return -EINVAL;
1276                 }
1277
1278                 LASSERT(src_nid != LNET_NID_ANY);
1279                 lnet_msg_commit(msg, cpt);
1280
1281                 if (!msg->msg_routing)
1282                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1283
1284                 if (src_ni == the_lnet.ln_loni) {
1285                         /* No send credit hassles with LOLND */
1286                         lnet_net_unlock(cpt);
1287                         lnet_ni_send(src_ni, msg);
1288
1289                         lnet_net_lock(cpt);
1290                         lnet_ni_decref_locked(src_ni, cpt);
1291                         lnet_net_unlock(cpt);
1292                         return 0;
1293                 }
1294
1295                 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1296                 /* lp has ref on src_ni; lose mine */
1297                 lnet_ni_decref_locked(src_ni, cpt);
1298                 if (rc != 0) {
1299                         lnet_net_unlock(cpt);
1300                         LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1301                                       libcfs_nid2str(dst_nid));
1302                         /* ENOMEM or shutting down */
1303                         return rc;
1304                 }
1305                 LASSERT (lp->lp_ni == src_ni);
1306         } else {
1307 #ifndef __KERNEL__
1308                 lnet_net_unlock(cpt);
1309
1310                 /* NB
1311                  * - once application finishes computation, check here to update
1312                  *   router states before it waits for pending IO in LNetEQPoll
1313                  * - recursion breaker: router checker sends no message
1314                  *   to remote networks */
1315                 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1316                         lnet_router_checker();
1317
1318                 lnet_net_lock(cpt);
1319 #endif
1320                 /* sending to a remote network */
1321                 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1322                 if (lp == NULL) {
1323                         if (src_ni != NULL)
1324                                 lnet_ni_decref_locked(src_ni, cpt);
1325                         lnet_net_unlock(cpt);
1326
1327                         LCONSOLE_WARN("No route to %s via %s "
1328                                       "(all routers down)\n",
1329                                       libcfs_id2str(msg->msg_target),
1330                                       libcfs_nid2str(src_nid));
1331                         return -EHOSTUNREACH;
1332                 }
1333
1334                 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1335                  * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1336                  * pre-determined router, this can happen if router table
1337                  * was changed when we release the lock */
1338                 if (rtr_nid != lp->lp_nid) {
1339                         cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1340                         if (cpt2 != cpt) {
1341                                 if (src_ni != NULL)
1342                                         lnet_ni_decref_locked(src_ni, cpt);
1343                                 lnet_net_unlock(cpt);
1344
1345                                 rtr_nid = lp->lp_nid;
1346                                 cpt = cpt2;
1347                                 goto again;
1348                         }
1349                 }
1350
1351                 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1352                        libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1353                        lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1354
1355                 if (src_ni == NULL) {
1356                         src_ni = lp->lp_ni;
1357                         src_nid = src_ni->ni_nid;
1358                 } else {
1359                         LASSERT (src_ni == lp->lp_ni);
1360                         lnet_ni_decref_locked(src_ni, cpt);
1361                 }
1362
1363                 lnet_peer_addref_locked(lp);
1364
1365                 LASSERT(src_nid != LNET_NID_ANY);
1366                 lnet_msg_commit(msg, cpt);
1367
1368                 if (!msg->msg_routing) {
1369                         /* I'm the source and now I know which NI to send on */
1370                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1371                 }
1372
1373                 msg->msg_target_is_router = 1;
1374                 msg->msg_target.nid = lp->lp_nid;
1375                 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1376         }
1377
1378         /* 'lp' is our best choice of peer */
1379
1380         LASSERT (!msg->msg_peertxcredit);
1381         LASSERT (!msg->msg_txcredit);
1382         LASSERT (msg->msg_txpeer == NULL);
1383
1384         msg->msg_txpeer = lp;                   /* msg takes my ref on lp */
1385
1386         rc = lnet_post_send_locked(msg, 0);
1387         lnet_net_unlock(cpt);
1388
1389         if (rc == EHOSTUNREACH)
1390                 return -EHOSTUNREACH;
1391
1392         if (rc == 0)
1393                 lnet_ni_send(src_ni, msg);
1394
1395         return 0;
1396 }
1397
1398 static void
1399 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1400 {
1401         lnet_net_lock(cpt);
1402         the_lnet.ln_counters[cpt]->drop_count++;
1403         the_lnet.ln_counters[cpt]->drop_length += nob;
1404         lnet_net_unlock(cpt);
1405
1406         lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1407 }
1408
1409 static void
1410 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1411 {
1412         lnet_hdr_t      *hdr = &msg->msg_hdr;
1413
1414         if (msg->msg_wanted != 0)
1415                 lnet_setpayloadbuffer(msg);
1416
1417         lnet_build_msg_event(msg, LNET_EVENT_PUT);
1418
1419         /* Must I ACK?  If so I'll grab the ack_wmd out of the header and put
1420          * it back into the ACK during lnet_finalize() */
1421         msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1422                         (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1423
1424         lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1425                      msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1426 }
1427
1428 static int
1429 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1430 {
1431         lnet_hdr_t              *hdr = &msg->msg_hdr;
1432         struct lnet_match_info  info;
1433         int                     rc;
1434
1435         /* Convert put fields to host byte order */
1436         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1437         hdr->msg.put.ptl_index  = le32_to_cpu(hdr->msg.put.ptl_index);
1438         hdr->msg.put.offset     = le32_to_cpu(hdr->msg.put.offset);
1439
1440         info.mi_id.nid  = hdr->src_nid;
1441         info.mi_id.pid  = hdr->src_pid;
1442         info.mi_opc     = LNET_MD_OP_PUT;
1443         info.mi_portal  = hdr->msg.put.ptl_index;
1444         info.mi_rlength = hdr->payload_length;
1445         info.mi_roffset = hdr->msg.put.offset;
1446         info.mi_mbits   = hdr->msg.put.match_bits;
1447
1448         msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1449
1450  again:
1451         rc = lnet_ptl_match_md(&info, msg);
1452         switch (rc) {
1453         default:
1454                 LBUG();
1455
1456         case LNET_MATCHMD_OK:
1457                 lnet_recv_put(ni, msg);
1458                 return 0;
1459
1460         case LNET_MATCHMD_NONE:
1461                 if (msg->msg_rx_delayed) /* attached on delayed list */
1462                         return 0;
1463
1464                 rc = lnet_ni_eager_recv(ni, msg);
1465                 if (rc == 0)
1466                         goto again;
1467                 /* fall through */
1468
1469         case LNET_MATCHMD_DROP:
1470                 CNETERR("Dropping PUT from %s portal %d match "LPU64
1471                         " offset %d length %d: %d\n",
1472                         libcfs_id2str(info.mi_id), info.mi_portal,
1473                         info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1474
1475                 return ENOENT;  /* +ve: OK but no match */
1476         }
1477 }
1478
1479 static int
1480 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1481 {
1482         struct lnet_match_info  info;
1483         lnet_hdr_t              *hdr = &msg->msg_hdr;
1484         lnet_handle_wire_t      reply_wmd;
1485         int                     rc;
1486
1487         /* Convert get fields to host byte order */
1488         hdr->msg.get.match_bits   = le64_to_cpu(hdr->msg.get.match_bits);
1489         hdr->msg.get.ptl_index    = le32_to_cpu(hdr->msg.get.ptl_index);
1490         hdr->msg.get.sink_length  = le32_to_cpu(hdr->msg.get.sink_length);
1491         hdr->msg.get.src_offset   = le32_to_cpu(hdr->msg.get.src_offset);
1492
1493         info.mi_id.nid  = hdr->src_nid;
1494         info.mi_id.pid  = hdr->src_pid;
1495         info.mi_opc     = LNET_MD_OP_GET;
1496         info.mi_portal  = hdr->msg.get.ptl_index;
1497         info.mi_rlength = hdr->msg.get.sink_length;
1498         info.mi_roffset = hdr->msg.get.src_offset;
1499         info.mi_mbits   = hdr->msg.get.match_bits;
1500
1501         rc = lnet_ptl_match_md(&info, msg);
1502         if (rc == LNET_MATCHMD_DROP) {
1503                 CNETERR("Dropping GET from %s portal %d match "LPU64
1504                         " offset %d length %d\n",
1505                         libcfs_id2str(info.mi_id), info.mi_portal,
1506                         info.mi_mbits, info.mi_roffset, info.mi_rlength);
1507                 return ENOENT;  /* +ve: OK but no match */
1508         }
1509
1510         LASSERT(rc == LNET_MATCHMD_OK);
1511
1512         lnet_build_msg_event(msg, LNET_EVENT_GET);
1513
1514         reply_wmd = hdr->msg.get.return_wmd;
1515
1516         lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1517                        msg->msg_offset, msg->msg_wanted);
1518
1519         msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1520
1521         if (rdma_get) {
1522                 /* The LND completes the REPLY from her recv procedure */
1523                 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1524                              msg->msg_offset, msg->msg_len, msg->msg_len);
1525                 return 0;
1526         }
1527
1528         lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1529         msg->msg_receiving = 0;
1530
1531         rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1532         if (rc < 0) {
1533                 /* didn't get as far as lnet_ni_send() */
1534                 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1535                        libcfs_nid2str(ni->ni_nid),
1536                        libcfs_id2str(info.mi_id), rc);
1537
1538                 lnet_finalize(ni, msg, rc);
1539         }
1540
1541         return 0;
1542 }
1543
1544 static int
1545 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1546 {
1547         void             *private = msg->msg_private;
1548         lnet_hdr_t       *hdr = &msg->msg_hdr;
1549         lnet_process_id_t src = {0};
1550         lnet_libmd_t     *md;
1551         int               rlength;
1552         int               mlength;
1553         int                     cpt;
1554
1555         cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1556         lnet_res_lock(cpt);
1557
1558         src.nid = hdr->src_nid;
1559         src.pid = hdr->src_pid;
1560
1561         /* NB handles only looked up by creator (no flips) */
1562         md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1563         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1564                 CNETERR("%s: Dropping REPLY from %s for %s "
1565                         "MD "LPX64"."LPX64"\n",
1566                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1567                         (md == NULL) ? "invalid" : "inactive",
1568                         hdr->msg.reply.dst_wmd.wh_interface_cookie,
1569                         hdr->msg.reply.dst_wmd.wh_object_cookie);
1570                 if (md != NULL && md->md_me != NULL)
1571                         CERROR("REPLY MD also attached to portal %d\n",
1572                                md->md_me->me_portal);
1573
1574                 lnet_res_unlock(cpt);
1575                 return ENOENT;                  /* +ve: OK but no match */
1576         }
1577
1578         LASSERT (md->md_offset == 0);
1579
1580         rlength = hdr->payload_length;
1581         mlength = MIN(rlength, (int)md->md_length);
1582
1583         if (mlength < rlength &&
1584             (md->md_options & LNET_MD_TRUNCATE) == 0) {
1585                 CNETERR("%s: Dropping REPLY from %s length %d "
1586                         "for MD "LPX64" would overflow (%d)\n",
1587                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1588                         rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1589                         mlength);
1590                 lnet_res_unlock(cpt);
1591                 return ENOENT;          /* +ve: OK but no match */
1592         }
1593
1594         CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1595                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1596                mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1597
1598         lnet_msg_attach_md(msg, md, 0, mlength);
1599
1600         if (mlength != 0)
1601                 lnet_setpayloadbuffer(msg);
1602
1603         lnet_res_unlock(cpt);
1604
1605         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1606
1607         lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1608         return 0;
1609 }
1610
1611 static int
1612 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1613 {
1614         lnet_hdr_t       *hdr = &msg->msg_hdr;
1615         lnet_process_id_t src = {0};
1616         lnet_libmd_t     *md;
1617         int                     cpt;
1618
1619         src.nid = hdr->src_nid;
1620         src.pid = hdr->src_pid;
1621
1622         /* Convert ack fields to host byte order */
1623         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1624         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1625
1626         cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1627         lnet_res_lock(cpt);
1628
1629         /* NB handles only looked up by creator (no flips) */
1630         md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1631         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1632                 /* Don't moan; this is expected */
1633                 CDEBUG(D_NET,
1634                        "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1635                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1636                        (md == NULL) ? "invalid" : "inactive",
1637                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1638                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1639                 if (md != NULL && md->md_me != NULL)
1640                         CERROR("Source MD also attached to portal %d\n",
1641                                md->md_me->me_portal);
1642
1643                 lnet_res_unlock(cpt);
1644                 return ENOENT;                  /* +ve! */
1645         }
1646
1647         CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1648                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1649                hdr->msg.ack.dst_wmd.wh_object_cookie);
1650
1651         lnet_msg_attach_md(msg, md, 0, 0);
1652
1653         lnet_res_unlock(cpt);
1654
1655         lnet_build_msg_event(msg, LNET_EVENT_ACK);
1656
1657         lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1658         return 0;
1659 }
1660
1661 static int
1662 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1663 {
1664         int     rc = 0;
1665
1666 #ifdef __KERNEL__
1667         if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1668             lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1669                 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1670                         msg->msg_rx_ready_delay = 1;
1671                 } else {
1672                         lnet_net_unlock(msg->msg_rx_cpt);
1673                         rc = lnet_ni_eager_recv(ni, msg);
1674                         lnet_net_lock(msg->msg_rx_cpt);
1675                 }
1676         }
1677
1678         if (rc == 0)
1679                 rc = lnet_post_routed_recv_locked(msg, 0);
1680 #else
1681         LBUG();
1682 #endif
1683         return rc;
1684 }
1685
1686 char *
1687 lnet_msgtyp2str (int type)
1688 {
1689         switch (type) {
1690         case LNET_MSG_ACK:
1691                 return ("ACK");
1692         case LNET_MSG_PUT:
1693                 return ("PUT");
1694         case LNET_MSG_GET:
1695                 return ("GET");
1696         case LNET_MSG_REPLY:
1697                 return ("REPLY");
1698         case LNET_MSG_HELLO:
1699                 return ("HELLO");
1700         default:
1701                 return ("<UNKNOWN>");
1702         }
1703 }
1704 EXPORT_SYMBOL(lnet_msgtyp2str);
1705
1706 void
1707 lnet_print_hdr(lnet_hdr_t * hdr)
1708 {
1709         lnet_process_id_t src = {0};
1710         lnet_process_id_t dst = {0};
1711         char *type_str = lnet_msgtyp2str (hdr->type);
1712
1713         src.nid = hdr->src_nid;
1714         src.pid = hdr->src_pid;
1715
1716         dst.nid = hdr->dest_nid;
1717         dst.pid = hdr->dest_pid;
1718
1719         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1720         CWARN("    From %s\n", libcfs_id2str(src));
1721         CWARN("    To   %s\n", libcfs_id2str(dst));
1722
1723         switch (hdr->type) {
1724         default:
1725                 break;
1726
1727         case LNET_MSG_PUT:
1728                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
1729                       "match bits "LPU64"\n",
1730                       hdr->msg.put.ptl_index,
1731                       hdr->msg.put.ack_wmd.wh_interface_cookie,
1732                       hdr->msg.put.ack_wmd.wh_object_cookie,
1733                       hdr->msg.put.match_bits);
1734                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
1735                       hdr->payload_length, hdr->msg.put.offset,
1736                       hdr->msg.put.hdr_data);
1737                 break;
1738
1739         case LNET_MSG_GET:
1740                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
1741                       "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1742                       hdr->msg.get.return_wmd.wh_interface_cookie,
1743                       hdr->msg.get.return_wmd.wh_object_cookie,
1744                       hdr->msg.get.match_bits);
1745                 CWARN("    Length %d, src offset %d\n",
1746                       hdr->msg.get.sink_length,
1747                       hdr->msg.get.src_offset);
1748                 break;
1749
1750         case LNET_MSG_ACK:
1751                 CWARN("    dst md "LPX64"."LPX64", "
1752                       "manipulated length %d\n",
1753                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
1754                       hdr->msg.ack.dst_wmd.wh_object_cookie,
1755                       hdr->msg.ack.mlength);
1756                 break;
1757
1758         case LNET_MSG_REPLY:
1759                 CWARN("    dst md "LPX64"."LPX64", "
1760                       "length %d\n",
1761                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
1762                       hdr->msg.reply.dst_wmd.wh_object_cookie,
1763                       hdr->payload_length);
1764         }
1765
1766 }
1767
1768 int
1769 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1770            void *private, int rdma_req)
1771 {
1772         int             rc = 0;
1773         int             cpt;
1774         int             for_me;
1775         struct lnet_msg *msg;
1776         lnet_pid_t     dest_pid;
1777         lnet_nid_t     dest_nid;
1778         lnet_nid_t     src_nid;
1779         __u32          payload_length;
1780         __u32          type;
1781
1782         LASSERT (!cfs_in_interrupt ());
1783
1784         type = le32_to_cpu(hdr->type);
1785         src_nid = le64_to_cpu(hdr->src_nid);
1786         dest_nid = le64_to_cpu(hdr->dest_nid);
1787         dest_pid = le32_to_cpu(hdr->dest_pid);
1788         payload_length = le32_to_cpu(hdr->payload_length);
1789
1790         for_me = (ni->ni_nid == dest_nid);
1791         cpt = lnet_cpt_of_nid(from_nid);
1792
1793         switch (type) {
1794         case LNET_MSG_ACK:
1795         case LNET_MSG_GET:
1796                 if (payload_length > 0) {
1797                         CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1798                                libcfs_nid2str(from_nid),
1799                                libcfs_nid2str(src_nid),
1800                                lnet_msgtyp2str(type), payload_length);
1801                         return -EPROTO;
1802                 }
1803                 break;
1804
1805         case LNET_MSG_PUT:
1806         case LNET_MSG_REPLY:
1807                 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1808                         CERROR("%s, src %s: bad %s payload %d "
1809                                "(%d max expected)\n",
1810                                libcfs_nid2str(from_nid),
1811                                libcfs_nid2str(src_nid),
1812                                lnet_msgtyp2str(type),
1813                                payload_length,
1814                                for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1815                         return -EPROTO;
1816                 }
1817                 break;
1818
1819         default:
1820                 CERROR("%s, src %s: Bad message type 0x%x\n",
1821                        libcfs_nid2str(from_nid),
1822                        libcfs_nid2str(src_nid), type);
1823                 return -EPROTO;
1824         }
1825
1826         if (the_lnet.ln_routing &&
1827             ni->ni_last_alive != cfs_time_current_sec()) {
1828                 lnet_ni_lock(ni);
1829
1830                 /* NB: so far here is the only place to set NI status to "up */
1831                 ni->ni_last_alive = cfs_time_current_sec();
1832                 if (ni->ni_status != NULL &&
1833                     ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1834                         ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1835                 lnet_ni_unlock(ni);
1836         }
1837
1838         /* Regard a bad destination NID as a protocol error.  Senders should
1839          * know what they're doing; if they don't they're misconfigured, buggy
1840          * or malicious so we chop them off at the knees :) */
1841
1842         if (!for_me) {
1843                 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1844                         /* should have gone direct */
1845                         CERROR ("%s, src %s: Bad dest nid %s "
1846                                 "(should have been sent direct)\n",
1847                                 libcfs_nid2str(from_nid),
1848                                 libcfs_nid2str(src_nid),
1849                                 libcfs_nid2str(dest_nid));
1850                         return -EPROTO;
1851                 }
1852
1853                 if (lnet_islocalnid(dest_nid)) {
1854                         /* dest is another local NI; sender should have used
1855                          * this node's NID on its own network */
1856                         CERROR ("%s, src %s: Bad dest nid %s "
1857                                 "(it's my nid but on a different network)\n",
1858                                 libcfs_nid2str(from_nid),
1859                                 libcfs_nid2str(src_nid),
1860                                 libcfs_nid2str(dest_nid));
1861                         return -EPROTO;
1862                 }
1863
1864                 if (rdma_req && type == LNET_MSG_GET) {
1865                         CERROR ("%s, src %s: Bad optimized GET for %s "
1866                                 "(final destination must be me)\n",
1867                                 libcfs_nid2str(from_nid),
1868                                 libcfs_nid2str(src_nid),
1869                                 libcfs_nid2str(dest_nid));
1870                         return -EPROTO;
1871                 }
1872
1873                 if (!the_lnet.ln_routing) {
1874                         CERROR ("%s, src %s: Dropping message for %s "
1875                                 "(routing not enabled)\n",
1876                                 libcfs_nid2str(from_nid),
1877                                 libcfs_nid2str(src_nid),
1878                                 libcfs_nid2str(dest_nid));
1879                         goto drop;
1880                 }
1881         }
1882
1883         /* Message looks OK; we're not going to return an error, so we MUST
1884          * call back lnd_recv() come what may... */
1885
1886         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
1887             fail_peer (src_nid, 0))             /* shall we now? */
1888         {
1889                 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1890                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1891                        lnet_msgtyp2str(type));
1892                 goto drop;
1893         }
1894
1895         msg = lnet_msg_alloc();
1896         if (msg == NULL) {
1897                 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1898                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid), 
1899                        lnet_msgtyp2str(type));
1900                 goto drop;
1901         }
1902
1903         /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
1904
1905         msg->msg_type = type;
1906         msg->msg_private = private;
1907         msg->msg_receiving = 1;
1908         msg->msg_len = msg->msg_wanted = payload_length;
1909         msg->msg_offset = 0;
1910         msg->msg_hdr = *hdr;
1911         /* for building message event */
1912         msg->msg_from = from_nid;
1913         if (!for_me) {
1914                 msg->msg_target.pid     = dest_pid;
1915                 msg->msg_target.nid     = dest_nid;
1916                 msg->msg_routing        = 1;
1917
1918         } else {
1919                 /* convert common msg->hdr fields to host byteorder */
1920                 msg->msg_hdr.type       = type;
1921                 msg->msg_hdr.src_nid    = src_nid;
1922                 msg->msg_hdr.src_pid    = le32_to_cpu(msg->msg_hdr.src_pid);
1923                 msg->msg_hdr.dest_nid   = dest_nid;
1924                 msg->msg_hdr.dest_pid   = dest_pid;
1925                 msg->msg_hdr.payload_length = payload_length;
1926         }
1927
1928         lnet_net_lock(cpt);
1929         rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1930         if (rc != 0) {
1931                 lnet_net_unlock(cpt);
1932                 CERROR("%s, src %s: Dropping %s "
1933                        "(error %d looking up sender)\n",
1934                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1935                        lnet_msgtyp2str(type), rc);
1936                 lnet_msg_free(msg);
1937                 goto drop;
1938         }
1939
1940         lnet_msg_commit(msg, cpt);
1941
1942         if (!for_me) {
1943                 rc = lnet_parse_forward_locked(ni, msg);
1944                 lnet_net_unlock(cpt);
1945
1946                 if (rc < 0)
1947                         goto free_drop;
1948                 if (rc == 0) {
1949                         lnet_ni_recv(ni, msg->msg_private, msg, 0,
1950                                      0, payload_length, payload_length);
1951                 }
1952                 return 0;
1953         }
1954
1955         lnet_net_unlock(cpt);
1956
1957         switch (type) {
1958         case LNET_MSG_ACK:
1959                 rc = lnet_parse_ack(ni, msg);
1960                 break;
1961         case LNET_MSG_PUT:
1962                 rc = lnet_parse_put(ni, msg);
1963                 break;
1964         case LNET_MSG_GET:
1965                 rc = lnet_parse_get(ni, msg, rdma_req);
1966                 break;
1967         case LNET_MSG_REPLY:
1968                 rc = lnet_parse_reply(ni, msg);
1969                 break;
1970         default:
1971                 LASSERT(0);
1972                 rc = -EPROTO;
1973                 goto free_drop;  /* prevent an unused label if !kernel */
1974         }
1975
1976         if (rc == 0)
1977                 return 0;
1978
1979         LASSERT (rc == ENOENT);
1980
1981  free_drop:
1982         LASSERT(msg->msg_md == NULL);
1983         lnet_finalize(ni, msg, rc);
1984
1985  drop:
1986         lnet_drop_message(ni, cpt, private, payload_length);
1987         return 0;
1988 }
1989 EXPORT_SYMBOL(lnet_parse);
1990
1991 void
1992 lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason)
1993 {
1994         while (!cfs_list_empty(head)) {
1995                 lnet_process_id_t       id = {0};
1996                 lnet_msg_t              *msg;
1997
1998                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
1999                 cfs_list_del(&msg->msg_list);
2000
2001                 id.nid = msg->msg_hdr.src_nid;
2002                 id.pid = msg->msg_hdr.src_pid;
2003
2004                 LASSERT(msg->msg_md == NULL);
2005                 LASSERT(msg->msg_rx_delayed);
2006                 LASSERT(msg->msg_rxpeer != NULL);
2007                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2008
2009                 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
2010                       " offset %d length %d: %s\n",
2011                       libcfs_id2str(id),
2012                       msg->msg_hdr.msg.put.ptl_index,
2013                       msg->msg_hdr.msg.put.match_bits,
2014                       msg->msg_hdr.msg.put.offset,
2015                       msg->msg_hdr.payload_length, reason);
2016
2017                 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2018                  * called lnet_drop_message(), so I just hang onto msg as well
2019                  * until that's done */
2020
2021                 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2022                                   msg->msg_rxpeer->lp_cpt,
2023                                   msg->msg_private, msg->msg_len);
2024                 /*
2025                  * NB: message will not generate event because w/o attached MD,
2026                  * but we still should give error code so lnet_msg_decommit()
2027                  * can skip counters operations and other checks.
2028                  */
2029                 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2030         }
2031 }
2032
2033 void
2034 lnet_recv_delayed_msg_list(cfs_list_t *head)
2035 {
2036         while (!cfs_list_empty(head)) {
2037                 lnet_msg_t        *msg;
2038                 lnet_process_id_t  id;
2039
2040                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
2041                 cfs_list_del(&msg->msg_list);
2042
2043                 /* md won't disappear under me, since each msg
2044                  * holds a ref on it */
2045
2046                 id.nid = msg->msg_hdr.src_nid;
2047                 id.pid = msg->msg_hdr.src_pid;
2048
2049                 LASSERT(msg->msg_rx_delayed);
2050                 LASSERT(msg->msg_md != NULL);
2051                 LASSERT(msg->msg_rxpeer != NULL);
2052                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2053
2054                 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2055                        "match "LPU64" offset %d length %d.\n",
2056                         libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2057                         msg->msg_hdr.msg.put.match_bits,
2058                         msg->msg_hdr.msg.put.offset,
2059                         msg->msg_hdr.payload_length);
2060
2061                 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2062         }
2063 }
2064
2065 /**
2066  * Initiate an asynchronous PUT operation.
2067  *
2068  * There are several events associated with a PUT: completion of the send on
2069  * the initiator node (LNET_EVENT_SEND), and when the send completes
2070  * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2071  * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2072  * used at the target node to indicate the completion of incoming data
2073  * delivery.
2074  *
2075  * The local events will be logged in the EQ associated with the MD pointed to
2076  * by \a mdh handle. Using a MD without an associated EQ results in these
2077  * events being discarded. In this case, the caller must have another
2078  * mechanism (e.g., a higher level protocol) for determining when it is safe
2079  * to modify the memory region associated with the MD.
2080  *
2081  * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2082  * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2083  *
2084  * \param self Indicates the NID of a local interface through which to send
2085  * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2086  * \param mdh A handle for the MD that describes the memory to be sent. The MD
2087  * must be "free floating" (See LNetMDBind()).
2088  * \param ack Controls whether an acknowledgment is requested.
2089  * Acknowledgments are only sent when they are requested by the initiating
2090  * process and the target MD enables them.
2091  * \param target A process identifier for the target process.
2092  * \param portal The index in the \a target's portal table.
2093  * \param match_bits The match bits to use for MD selection at the target
2094  * process.
2095  * \param offset The offset into the target MD (only used when the target
2096  * MD has the LNET_MD_MANAGE_REMOTE option set).
2097  * \param hdr_data 64 bits of user data that can be included in the message
2098  * header. This data is written to an event queue entry at the target if an
2099  * EQ is present on the matching MD.
2100  *
2101  * \retval  0      Success, and only in this case events will be generated
2102  * and logged to EQ (if it exists).
2103  * \retval -EIO    Simulated failure.
2104  * \retval -ENOMEM Memory allocation failure.
2105  * \retval -ENOENT Invalid MD object.
2106  *
2107  * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2108  */
2109 int
2110 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2111         lnet_process_id_t target, unsigned int portal,
2112         __u64 match_bits, unsigned int offset,
2113         __u64 hdr_data)
2114 {
2115         struct lnet_msg         *msg;
2116         struct lnet_libmd       *md;
2117         int                     cpt;
2118         int                     rc;
2119
2120         LASSERT (the_lnet.ln_init);
2121         LASSERT (the_lnet.ln_refcount > 0);
2122
2123         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2124             fail_peer (target.nid, 1))          /* shall we now? */
2125         {
2126                 CERROR("Dropping PUT to %s: simulated failure\n",
2127                        libcfs_id2str(target));
2128                 return -EIO;
2129         }
2130
2131         msg = lnet_msg_alloc();
2132         if (msg == NULL) {
2133                 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2134                        libcfs_id2str(target));
2135                 return -ENOMEM;
2136         }
2137         msg->msg_vmflush = !!cfs_memory_pressure_get();
2138
2139         cpt = lnet_cpt_of_cookie(mdh.cookie);
2140         lnet_res_lock(cpt);
2141
2142         md = lnet_handle2md(&mdh);
2143         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2144                 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2145                        match_bits, portal, libcfs_id2str(target),
2146                        md == NULL ? -1 : md->md_threshold);
2147                 if (md != NULL && md->md_me != NULL)
2148                         CERROR("Source MD also attached to portal %d\n",
2149                                md->md_me->me_portal);
2150                 lnet_res_unlock(cpt);
2151
2152                 lnet_msg_free(msg);
2153                 return -ENOENT;
2154         }
2155
2156         CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2157
2158         lnet_msg_attach_md(msg, md, 0, 0);
2159
2160         lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2161
2162         msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2163         msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2164         msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2165         msg->msg_hdr.msg.put.hdr_data = hdr_data;
2166
2167         /* NB handles only looked up by creator (no flips) */
2168         if (ack == LNET_ACK_REQ) {
2169                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2170                         the_lnet.ln_interface_cookie;
2171                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2172                         md->md_lh.lh_cookie;
2173         } else {
2174                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2175                         LNET_WIRE_HANDLE_COOKIE_NONE;
2176                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2177                         LNET_WIRE_HANDLE_COOKIE_NONE;
2178         }
2179
2180         lnet_res_unlock(cpt);
2181
2182         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2183
2184         rc = lnet_send(self, msg, LNET_NID_ANY);
2185         if (rc != 0) {
2186                 CNETERR( "Error sending PUT to %s: %d\n",
2187                        libcfs_id2str(target), rc);
2188                 lnet_finalize (NULL, msg, rc);
2189         }
2190
2191         /* completion will be signalled by an event */
2192         return 0;
2193 }
2194 EXPORT_SYMBOL(LNetPut);
2195
2196 lnet_msg_t *
2197 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2198 {
2199         /* The LND can DMA direct to the GET md (i.e. no REPLY msg).  This
2200          * returns a msg for the LND to pass to lnet_finalize() when the sink
2201          * data has been received.
2202          *
2203          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2204          * lnet_finalize() is called on it, so the LND must call this first */
2205
2206         struct lnet_msg         *msg = lnet_msg_alloc();
2207         struct lnet_libmd       *getmd = getmsg->msg_md;
2208         lnet_process_id_t       peer_id = getmsg->msg_target;
2209         int                     cpt;
2210
2211         LASSERT(!getmsg->msg_target_is_router);
2212         LASSERT(!getmsg->msg_routing);
2213
2214         cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2215         lnet_res_lock(cpt);
2216
2217         LASSERT (getmd->md_refcount > 0);
2218
2219         if (msg == NULL) {
2220                 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2221                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2222                 goto drop;
2223         }
2224
2225         if (getmd->md_threshold == 0) {
2226                 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2227                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), 
2228                         getmd);
2229                 lnet_res_unlock(cpt);
2230                 goto drop;
2231         }
2232
2233         LASSERT(getmd->md_offset == 0);
2234
2235         CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2236                libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2237
2238         /* setup information for lnet_build_msg_event */
2239         msg->msg_from = peer_id.nid;
2240         msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2241         msg->msg_hdr.src_nid = peer_id.nid;
2242         msg->msg_hdr.payload_length = getmd->md_length;
2243         msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2244
2245         lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2246         lnet_res_unlock(cpt);
2247
2248         cpt = lnet_cpt_of_nid(peer_id.nid);
2249
2250         lnet_net_lock(cpt);
2251         lnet_msg_commit(msg, cpt);
2252         lnet_net_unlock(cpt);
2253
2254         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2255
2256         return msg;
2257
2258  drop:
2259         cpt = lnet_cpt_of_nid(peer_id.nid);
2260
2261         lnet_net_lock(cpt);
2262         the_lnet.ln_counters[cpt]->drop_count++;
2263         the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2264         lnet_net_unlock(cpt);
2265
2266         if (msg != NULL)
2267                 lnet_msg_free(msg);
2268
2269         return NULL;
2270 }
2271 EXPORT_SYMBOL(lnet_create_reply_msg);
2272
2273 void
2274 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2275 {
2276         /* Set the REPLY length, now the RDMA that elides the REPLY message has
2277          * completed and I know it. */
2278         LASSERT (reply != NULL);
2279         LASSERT (reply->msg_type == LNET_MSG_GET);
2280         LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2281
2282         /* NB I trusted my peer to RDMA.  If she tells me she's written beyond
2283          * the end of my buffer, I might as well be dead. */
2284         LASSERT (len <= reply->msg_ev.mlength);
2285
2286         reply->msg_ev.mlength = len;
2287 }
2288 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2289
2290 /**
2291  * Initiate an asynchronous GET operation.
2292  *
2293  * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2294  * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2295  * the target node in the REPLY has been written to local MD.
2296  *
2297  * On the target node, an LNET_EVENT_GET is logged when the GET request
2298  * arrives and is accepted into a MD.
2299  *
2300  * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2301  * \param mdh A handle for the MD that describes the memory into which the
2302  * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2303  *
2304  * \retval  0      Success, and only in this case events will be generated
2305  * and logged to EQ (if it exists) of the MD.
2306  * \retval -EIO    Simulated failure.
2307  * \retval -ENOMEM Memory allocation failure.
2308  * \retval -ENOENT Invalid MD object.
2309  */
2310 int
2311 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2312         lnet_process_id_t target, unsigned int portal,
2313         __u64 match_bits, unsigned int offset)
2314 {
2315         struct lnet_msg         *msg;
2316         struct lnet_libmd       *md;
2317         int                     cpt;
2318         int                     rc;
2319
2320         LASSERT (the_lnet.ln_init);
2321         LASSERT (the_lnet.ln_refcount > 0);
2322
2323         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2324             fail_peer (target.nid, 1))          /* shall we now? */
2325         {
2326                 CERROR("Dropping GET to %s: simulated failure\n",
2327                        libcfs_id2str(target));
2328                 return -EIO;
2329         }
2330
2331         msg = lnet_msg_alloc();
2332         if (msg == NULL) {
2333                 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2334                        libcfs_id2str(target));
2335                 return -ENOMEM;
2336         }
2337
2338         cpt = lnet_cpt_of_cookie(mdh.cookie);
2339         lnet_res_lock(cpt);
2340
2341         md = lnet_handle2md(&mdh);
2342         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2343                 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2344                        match_bits, portal, libcfs_id2str(target),
2345                        md == NULL ? -1 : md->md_threshold);
2346                 if (md != NULL && md->md_me != NULL)
2347                         CERROR("REPLY MD also attached to portal %d\n",
2348                                md->md_me->me_portal);
2349
2350                 lnet_res_unlock(cpt);
2351
2352                 lnet_msg_free(msg);
2353
2354                 return -ENOENT;
2355         }
2356
2357         CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2358
2359         lnet_msg_attach_md(msg, md, 0, 0);
2360
2361         lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2362
2363         msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2364         msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2365         msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2366         msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2367
2368         /* NB handles only looked up by creator (no flips) */
2369         msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2370                 the_lnet.ln_interface_cookie;
2371         msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2372                 md->md_lh.lh_cookie;
2373
2374         lnet_res_unlock(cpt);
2375
2376         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2377
2378         rc = lnet_send(self, msg, LNET_NID_ANY);
2379         if (rc < 0) {
2380                 CNETERR( "Error sending GET to %s: %d\n",
2381                        libcfs_id2str(target), rc);
2382                 lnet_finalize (NULL, msg, rc);
2383         }
2384
2385         /* completion will be signalled by an event */
2386         return 0;
2387 }
2388 EXPORT_SYMBOL(LNetGet);
2389
2390 /**
2391  * Calculate distance to node at \a dstnid.
2392  *
2393  * \param dstnid Target NID.
2394  * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2395  * is saved here.
2396  * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2397  * here.
2398  *
2399  * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2400  * local_nid_dist_zero is set, which is the default.
2401  * \retval positives Distance to target NID, i.e. number of hops plus one.
2402  * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2403  */
2404 int
2405 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2406 {
2407         cfs_list_t              *e;
2408         struct lnet_ni          *ni;
2409         lnet_remotenet_t        *rnet;
2410         __u32                   dstnet = LNET_NIDNET(dstnid);
2411         int                     hops;
2412         int                     cpt;
2413         __u32                   order = 2;
2414
2415         /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2416          * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2417          * keep order 0 free for 0@lo and order 1 free for a local NID
2418          * match */
2419
2420         LASSERT (the_lnet.ln_init);
2421         LASSERT (the_lnet.ln_refcount > 0);
2422
2423         cpt = lnet_net_lock_current();
2424
2425         cfs_list_for_each (e, &the_lnet.ln_nis) {
2426                 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2427
2428                 if (ni->ni_nid == dstnid) {
2429                         if (srcnidp != NULL)
2430                                 *srcnidp = dstnid;
2431                         if (orderp != NULL) {
2432                                 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2433                                         *orderp = 0;
2434                                 else
2435                                         *orderp = 1;
2436                         }
2437                         lnet_net_unlock(cpt);
2438
2439                         return local_nid_dist_zero ? 0 : 1;
2440                 }
2441
2442                 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2443                         if (srcnidp != NULL)
2444                                 *srcnidp = ni->ni_nid;
2445                         if (orderp != NULL)
2446                                 *orderp = order;
2447                         lnet_net_unlock(cpt);
2448                         return 1;
2449                 }
2450
2451                 order++;
2452         }
2453
2454         cfs_list_for_each (e, &the_lnet.ln_remote_nets) {
2455                 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2456
2457                 if (rnet->lrn_net == dstnet) {
2458                         lnet_route_t *route;
2459                         lnet_route_t *shortest = NULL;
2460
2461                         LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2462
2463                         cfs_list_for_each_entry(route, &rnet->lrn_routes,
2464                                                 lr_list) {
2465                                 if (shortest == NULL ||
2466                                     route->lr_hops < shortest->lr_hops)
2467                                         shortest = route;
2468                         }
2469
2470                         LASSERT (shortest != NULL);
2471                         hops = shortest->lr_hops;
2472                         if (srcnidp != NULL)
2473                                 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2474                         if (orderp != NULL)
2475                                 *orderp = order;
2476                         lnet_net_unlock(cpt);
2477                         return hops + 1;
2478                 }
2479                 order++;
2480         }
2481
2482         lnet_net_unlock(cpt);
2483         return -EHOSTUNREACH;
2484 }
2485 EXPORT_SYMBOL(LNetDist);
2486
2487 /**
2488  * Set the number of asynchronous messages expected from a target process.
2489  *
2490  * This function is only meaningful for userspace callers. It's a no-op when
2491  * called from kernel.
2492  *
2493  * Asynchronous messages are those that can come from a target when the
2494  * userspace process is not waiting for IO to complete; e.g., AST callbacks
2495  * from Lustre servers. Specifying the expected number of such messages
2496  * allows them to be eagerly received when user process is not running in
2497  * LNet; otherwise network errors may occur.
2498  *
2499  * \param id Process ID of the target process.
2500  * \param nasync Number of asynchronous messages expected from the target.
2501  *
2502  * \return 0 on success, and an error code otherwise.
2503  */
2504 int
2505 LNetSetAsync(lnet_process_id_t id, int nasync)
2506 {
2507 #ifdef __KERNEL__
2508         return 0;
2509 #else
2510         lnet_ni_t        *ni;
2511         lnet_remotenet_t *rnet;
2512         cfs_list_t       *tmp;
2513         lnet_route_t     *route;
2514         lnet_nid_t       *nids;
2515         int               nnids;
2516         int               maxnids = 256;
2517         int               rc = 0;
2518         int               rc2;
2519         int                     cpt;
2520
2521         /* Target on a local network? */
2522         ni = lnet_net2ni(LNET_NIDNET(id.nid));
2523         if (ni != NULL) {
2524                 if (ni->ni_lnd->lnd_setasync != NULL) 
2525                         rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2526                 lnet_ni_decref(ni);
2527                 return rc;
2528         }
2529
2530         /* Target on a remote network: apply to routers */
2531  again:
2532         LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2533         if (nids == NULL)
2534                 return -ENOMEM;
2535         nnids = 0;
2536
2537         /* Snapshot all the router NIDs */
2538         cpt = lnet_net_lock_current();
2539         rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2540         if (rnet != NULL) {
2541                 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2542                         if (nnids == maxnids) {
2543                                 lnet_net_unlock(cpt);
2544                                 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2545                                 maxnids *= 2;
2546                                 goto again;
2547                         }
2548
2549                         route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2550                         nids[nnids++] = route->lr_gateway->lp_nid;
2551                 }
2552         }
2553         lnet_net_unlock(cpt);
2554
2555         /* set async on all the routers */
2556         while (nnids-- > 0) {
2557                 id.pid = LUSTRE_SRV_LNET_PID;
2558                 id.nid = nids[nnids];
2559
2560                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2561                 if (ni == NULL)
2562                         continue;
2563
2564                 if (ni->ni_lnd->lnd_setasync != NULL) {
2565                         rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2566                         if (rc2 != 0)
2567                                 rc = rc2;
2568                 }
2569                 lnet_ni_decref(ni);
2570         }
2571
2572         LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2573         return rc;
2574 #endif
2575 }
2576 EXPORT_SYMBOL(LNetSetAsync);