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