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