Whamcloud - gitweb
LU-4196 build: Build support for OFED-3.5 and SLES 11
[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 (!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 (!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 (!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 (!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 (!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 /**
820  * \param msg The message to be sent.
821  * \param do_send True if lnet_ni_send() should be called in this function.
822  *        lnet_send() is going to lnet_net_unlock immediately after this, so
823  *        it sets do_send FALSE and I don't do the unlock/send/lock bit.
824  *
825  * \retval 0 If \a msg sent or OK to send.
826  * \retval EAGAIN If \a msg blocked for credit.
827  * \retval EHOSTUNREACH If the next hop of the message appears dead.
828  * \retval ECANCELED If the MD of the message has been unlinked.
829  */
830 static int
831 lnet_post_send_locked(lnet_msg_t *msg, int do_send)
832 {
833         lnet_peer_t             *lp = msg->msg_txpeer;
834         lnet_ni_t               *ni = lp->lp_ni;
835         int                     cpt = msg->msg_tx_cpt;
836         struct lnet_tx_queue    *tq = ni->ni_tx_queues[cpt];
837
838         /* non-lnet_send() callers have checked before */
839         LASSERT(!do_send || msg->msg_tx_delayed);
840         LASSERT(!msg->msg_receiving);
841         LASSERT(msg->msg_tx_committed);
842
843         /* NB 'lp' is always the next hop */
844         if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
845             lnet_peer_alive_locked(lp) == 0) {
846                 the_lnet.ln_counters[cpt]->drop_count++;
847                 the_lnet.ln_counters[cpt]->drop_length += msg->msg_len;
848                 lnet_net_unlock(cpt);
849
850                 CNETERR("Dropping message for %s: peer not alive\n",
851                         libcfs_id2str(msg->msg_target));
852                 if (do_send)
853                         lnet_finalize(ni, msg, -EHOSTUNREACH);
854
855                 lnet_net_lock(cpt);
856                 return EHOSTUNREACH;
857         }
858
859         if (msg->msg_md != NULL &&
860             (msg->msg_md->md_flags & LNET_MD_FLAG_ABORTED) != 0) {
861                 lnet_net_unlock(cpt);
862
863                 CNETERR("Aborting message for %s: LNetM[DE]Unlink() already "
864                         "called on the MD/ME.\n",
865                         libcfs_id2str(msg->msg_target));
866                 if (do_send)
867                         lnet_finalize(ni, msg, -ECANCELED);
868
869                 lnet_net_lock(cpt);
870                 return ECANCELED;
871         }
872
873         if (!msg->msg_peertxcredit) {
874                 LASSERT ((lp->lp_txcredits < 0) ==
875                          !cfs_list_empty(&lp->lp_txq));
876
877                 msg->msg_peertxcredit = 1;
878                 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
879                 lp->lp_txcredits--;
880
881                 if (lp->lp_txcredits < lp->lp_mintxcredits)
882                         lp->lp_mintxcredits = lp->lp_txcredits;
883
884                 if (lp->lp_txcredits < 0) {
885                         msg->msg_tx_delayed = 1;
886                         cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
887                         return EAGAIN;
888                 }
889         }
890
891         if (!msg->msg_txcredit) {
892                 LASSERT((tq->tq_credits < 0) ==
893                         !cfs_list_empty(&tq->tq_delayed));
894
895                 msg->msg_txcredit = 1;
896                 tq->tq_credits--;
897
898                 if (tq->tq_credits < tq->tq_credits_min)
899                         tq->tq_credits_min = tq->tq_credits;
900
901                 if (tq->tq_credits < 0) {
902                         msg->msg_tx_delayed = 1;
903                         cfs_list_add_tail(&msg->msg_list, &tq->tq_delayed);
904                         return EAGAIN;
905                 }
906         }
907
908         if (do_send) {
909                 lnet_net_unlock(cpt);
910                 lnet_ni_send(ni, msg);
911                 lnet_net_lock(cpt);
912         }
913         return 0;
914 }
915
916 #ifdef __KERNEL__
917
918 lnet_rtrbufpool_t *
919 lnet_msg2bufpool(lnet_msg_t *msg)
920 {
921         lnet_rtrbufpool_t       *rbp;
922         int                     cpt;
923
924         LASSERT(msg->msg_rx_committed);
925
926         cpt = msg->msg_rx_cpt;
927         rbp = &the_lnet.ln_rtrpools[cpt][0];
928
929         LASSERT(msg->msg_len <= LNET_MTU);
930         while (msg->msg_len > (unsigned int)rbp->rbp_npages * PAGE_CACHE_SIZE) {
931                 rbp++;
932                 LASSERT(rbp < &the_lnet.ln_rtrpools[cpt][LNET_NRBPOOLS]);
933         }
934
935         return rbp;
936 }
937
938 int
939 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
940 {
941         /* lnet_parse is going to lnet_net_unlock immediately after this, so it
942          * sets do_recv FALSE and I don't do the unlock/send/lock bit.  I
943          * return EAGAIN if msg blocked and 0 if received or OK to receive */
944         lnet_peer_t         *lp = msg->msg_rxpeer;
945         lnet_rtrbufpool_t   *rbp;
946         lnet_rtrbuf_t       *rb;
947
948         LASSERT (msg->msg_iov == NULL);
949         LASSERT (msg->msg_kiov == NULL);
950         LASSERT (msg->msg_niov == 0);
951         LASSERT (msg->msg_routing);
952         LASSERT (msg->msg_receiving);
953         LASSERT (!msg->msg_sending);
954
955         /* non-lnet_parse callers only receive delayed messages */
956         LASSERT(!do_recv || msg->msg_rx_delayed);
957
958         if (!msg->msg_peerrtrcredit) {
959                 LASSERT ((lp->lp_rtrcredits < 0) ==
960                          !cfs_list_empty(&lp->lp_rtrq));
961
962                 msg->msg_peerrtrcredit = 1;
963                 lp->lp_rtrcredits--;
964                 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
965                         lp->lp_minrtrcredits = lp->lp_rtrcredits;
966
967                 if (lp->lp_rtrcredits < 0) {
968                         /* must have checked eager_recv before here */
969                         LASSERT(msg->msg_rx_ready_delay);
970                         msg->msg_rx_delayed = 1;
971                         cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
972                         return EAGAIN;
973                 }
974         }
975
976         rbp = lnet_msg2bufpool(msg);
977
978         if (!msg->msg_rtrcredit) {
979                 LASSERT ((rbp->rbp_credits < 0) ==
980                          !cfs_list_empty(&rbp->rbp_msgs));
981
982                 msg->msg_rtrcredit = 1;
983                 rbp->rbp_credits--;
984                 if (rbp->rbp_credits < rbp->rbp_mincredits)
985                         rbp->rbp_mincredits = rbp->rbp_credits;
986
987                 if (rbp->rbp_credits < 0) {
988                         /* must have checked eager_recv before here */
989                         LASSERT(msg->msg_rx_ready_delay);
990                         msg->msg_rx_delayed = 1;
991                         cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
992                         return EAGAIN;
993                 }
994         }
995
996         LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
997         rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
998         cfs_list_del(&rb->rb_list);
999
1000         msg->msg_niov = rbp->rbp_npages;
1001         msg->msg_kiov = &rb->rb_kiov[0];
1002
1003         if (do_recv) {
1004                 int cpt = msg->msg_rx_cpt;
1005
1006                 lnet_net_unlock(cpt);
1007                 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1008                              0, msg->msg_len, msg->msg_len);
1009                 lnet_net_lock(cpt);
1010         }
1011         return 0;
1012 }
1013 #endif
1014
1015 void
1016 lnet_return_tx_credits_locked(lnet_msg_t *msg)
1017 {
1018         lnet_peer_t     *txpeer = msg->msg_txpeer;
1019         lnet_msg_t      *msg2;
1020
1021         if (msg->msg_txcredit) {
1022                 struct lnet_ni       *ni = txpeer->lp_ni;
1023                 struct lnet_tx_queue *tq = ni->ni_tx_queues[msg->msg_tx_cpt];
1024
1025                 /* give back NI txcredits */
1026                 msg->msg_txcredit = 0;
1027
1028                 LASSERT((tq->tq_credits < 0) ==
1029                         !cfs_list_empty(&tq->tq_delayed));
1030
1031                 tq->tq_credits++;
1032                 if (tq->tq_credits <= 0) {
1033                         msg2 = cfs_list_entry(tq->tq_delayed.next,
1034                                               lnet_msg_t, msg_list);
1035                         cfs_list_del(&msg2->msg_list);
1036
1037                         LASSERT(msg2->msg_txpeer->lp_ni == ni);
1038                         LASSERT(msg2->msg_tx_delayed);
1039
1040                         (void) lnet_post_send_locked(msg2, 1);
1041                 }
1042         }
1043
1044         if (msg->msg_peertxcredit) {
1045                 /* give back peer txcredits */
1046                 msg->msg_peertxcredit = 0;
1047
1048                 LASSERT((txpeer->lp_txcredits < 0) ==
1049                         !cfs_list_empty(&txpeer->lp_txq));
1050
1051                 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1052                 LASSERT (txpeer->lp_txqnob >= 0);
1053
1054                 txpeer->lp_txcredits++;
1055                 if (txpeer->lp_txcredits <= 0) {
1056                         msg2 = cfs_list_entry(txpeer->lp_txq.next,
1057                                               lnet_msg_t, msg_list);
1058                         cfs_list_del(&msg2->msg_list);
1059
1060                         LASSERT(msg2->msg_txpeer == txpeer);
1061                         LASSERT(msg2->msg_tx_delayed);
1062
1063                         (void) lnet_post_send_locked(msg2, 1);
1064                 }
1065         }
1066
1067         if (txpeer != NULL) {
1068                 msg->msg_txpeer = NULL;
1069                 lnet_peer_decref_locked(txpeer);
1070         }
1071 }
1072
1073 void
1074 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1075 {
1076         lnet_peer_t     *rxpeer = msg->msg_rxpeer;
1077 #ifdef __KERNEL__
1078         lnet_msg_t      *msg2;
1079
1080         if (msg->msg_rtrcredit) {
1081                 /* give back global router credits */
1082                 lnet_rtrbuf_t     *rb;
1083                 lnet_rtrbufpool_t *rbp;
1084
1085                 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1086                  * there until it gets one allocated, or aborts the wait
1087                  * itself */
1088                 LASSERT (msg->msg_kiov != NULL);
1089
1090                 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1091                 rbp = rb->rb_pool;
1092                 LASSERT (rbp == lnet_msg2bufpool(msg));
1093
1094                 msg->msg_kiov = NULL;
1095                 msg->msg_rtrcredit = 0;
1096
1097                 LASSERT((rbp->rbp_credits < 0) ==
1098                         !cfs_list_empty(&rbp->rbp_msgs));
1099                 LASSERT((rbp->rbp_credits > 0) ==
1100                         !cfs_list_empty(&rbp->rbp_bufs));
1101
1102                 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1103                 rbp->rbp_credits++;
1104                 if (rbp->rbp_credits <= 0) {
1105                         msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1106                                               lnet_msg_t, msg_list);
1107                         cfs_list_del(&msg2->msg_list);
1108
1109                         (void) lnet_post_routed_recv_locked(msg2, 1);
1110                 }
1111         }
1112
1113         if (msg->msg_peerrtrcredit) {
1114                 /* give back peer router credits */
1115                 msg->msg_peerrtrcredit = 0;
1116
1117                 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1118                         !cfs_list_empty(&rxpeer->lp_rtrq));
1119
1120                 rxpeer->lp_rtrcredits++;
1121                 if (rxpeer->lp_rtrcredits <= 0) {
1122                         msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1123                                               lnet_msg_t, msg_list);
1124                         cfs_list_del(&msg2->msg_list);
1125
1126                         (void) lnet_post_routed_recv_locked(msg2, 1);
1127                 }
1128         }
1129 #else
1130         LASSERT (!msg->msg_rtrcredit);
1131         LASSERT (!msg->msg_peerrtrcredit);
1132 #endif
1133         if (rxpeer != NULL) {
1134                 msg->msg_rxpeer = NULL;
1135                 lnet_peer_decref_locked(rxpeer);
1136         }
1137 }
1138
1139 static int
1140 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
1141 {
1142         lnet_peer_t *p1 = r1->lr_gateway;
1143         lnet_peer_t *p2 = r2->lr_gateway;
1144
1145         if (r1->lr_priority < r2->lr_priority)
1146                 return 1;
1147
1148         if (r1->lr_priority > r2->lr_priority)
1149                 return -1;
1150
1151         if (r1->lr_hops < r2->lr_hops)
1152                 return 1;
1153
1154         if (r1->lr_hops > r2->lr_hops)
1155                 return -1;
1156
1157         if (p1->lp_txqnob < p2->lp_txqnob)
1158                 return 1;
1159
1160         if (p1->lp_txqnob > p2->lp_txqnob)
1161                 return -1;
1162
1163         if (p1->lp_txcredits > p2->lp_txcredits)
1164                 return 1;
1165
1166         if (p1->lp_txcredits < p2->lp_txcredits)
1167                 return -1;
1168
1169         if (r1->lr_seq - r2->lr_seq <= 0)
1170                 return 1;
1171
1172         return -1;
1173 }
1174
1175 static lnet_peer_t *
1176 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target, lnet_nid_t rtr_nid)
1177 {
1178         lnet_remotenet_t        *rnet;
1179         lnet_route_t            *route;
1180         lnet_route_t            *best_route;
1181         lnet_route_t            *last_route;
1182         struct lnet_peer        *lp_best;
1183         struct lnet_peer        *lp;
1184         int                     rc;
1185
1186         /* If @rtr_nid is not LNET_NID_ANY, return the gateway with
1187          * rtr_nid nid, otherwise find the best gateway I can use */
1188
1189         rnet = lnet_find_net_locked(LNET_NIDNET(target));
1190         if (rnet == NULL)
1191                 return NULL;
1192
1193         lp_best = NULL;
1194         best_route = last_route = NULL;
1195         cfs_list_for_each_entry(route, &rnet->lrn_routes, lr_list) {
1196                 lp = route->lr_gateway;
1197
1198                 if (!lnet_is_route_alive(route))
1199                         continue;
1200
1201                 if (ni != NULL && lp->lp_ni != ni)
1202                         continue;
1203
1204                 if (lp->lp_nid == rtr_nid) /* it's pre-determined router */
1205                         return lp;
1206
1207                 if (lp_best == NULL) {
1208                         best_route = last_route = route;
1209                         lp_best = lp;
1210                         continue;
1211                 }
1212
1213                 /* no protection on below fields, but it's harmless */
1214                 if (last_route->lr_seq - route->lr_seq < 0)
1215                         last_route = route;
1216
1217                 rc = lnet_compare_routes(route, best_route);
1218                 if (rc < 0)
1219                         continue;
1220
1221                 best_route = route;
1222                 lp_best = lp;
1223         }
1224
1225         /* set sequence number on the best router to the latest sequence + 1
1226          * so we can round-robin all routers, it's race and inaccurate but
1227          * harmless and functional  */
1228         if (best_route != NULL)
1229                 best_route->lr_seq = last_route->lr_seq + 1;
1230         return lp_best;
1231 }
1232
1233 int
1234 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg, lnet_nid_t rtr_nid)
1235 {
1236         lnet_nid_t              dst_nid = msg->msg_target.nid;
1237         struct lnet_ni          *src_ni;
1238         struct lnet_ni          *local_ni;
1239         struct lnet_peer        *lp;
1240         int                     cpt;
1241         int                     cpt2;
1242         int                     rc;
1243
1244         /* NB: rtr_nid is set to LNET_NID_ANY for all current use-cases,
1245          * but we might want to use pre-determined router for ACK/REPLY
1246          * in the future */
1247         /* NB: ni != NULL == interface pre-determined (ACK/REPLY) */
1248         LASSERT (msg->msg_txpeer == NULL);
1249         LASSERT (!msg->msg_sending);
1250         LASSERT (!msg->msg_target_is_router);
1251         LASSERT (!msg->msg_receiving);
1252
1253         msg->msg_sending = 1;
1254
1255         LASSERT(!msg->msg_tx_committed);
1256         cpt = lnet_cpt_of_nid(rtr_nid == LNET_NID_ANY ? dst_nid : rtr_nid);
1257  again:
1258         lnet_net_lock(cpt);
1259
1260         if (the_lnet.ln_shutdown) {
1261                 lnet_net_unlock(cpt);
1262                 return -ESHUTDOWN;
1263         }
1264
1265         if (src_nid == LNET_NID_ANY) {
1266                 src_ni = NULL;
1267         } else {
1268                 src_ni = lnet_nid2ni_locked(src_nid, cpt);
1269                 if (src_ni == NULL) {
1270                         lnet_net_unlock(cpt);
1271                         LCONSOLE_WARN("Can't send to %s: src %s is not a "
1272                                       "local nid\n", libcfs_nid2str(dst_nid),
1273                                       libcfs_nid2str(src_nid));
1274                         return -EINVAL;
1275                 }
1276                 LASSERT (!msg->msg_routing);
1277         }
1278
1279         /* Is this for someone on a local network? */
1280         local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid), cpt);
1281
1282         if (local_ni != NULL) {
1283                 if (src_ni == NULL) {
1284                         src_ni = local_ni;
1285                         src_nid = src_ni->ni_nid;
1286                 } else if (src_ni == local_ni) {
1287                         lnet_ni_decref_locked(local_ni, cpt);
1288                 } else {
1289                         lnet_ni_decref_locked(local_ni, cpt);
1290                         lnet_ni_decref_locked(src_ni, cpt);
1291                         lnet_net_unlock(cpt);
1292                         LCONSOLE_WARN("No route to %s via from %s\n",
1293                                       libcfs_nid2str(dst_nid),
1294                                       libcfs_nid2str(src_nid));
1295                         return -EINVAL;
1296                 }
1297
1298                 LASSERT(src_nid != LNET_NID_ANY);
1299                 lnet_msg_commit(msg, cpt);
1300
1301                 if (!msg->msg_routing)
1302                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1303
1304                 if (src_ni == the_lnet.ln_loni) {
1305                         /* No send credit hassles with LOLND */
1306                         lnet_net_unlock(cpt);
1307                         lnet_ni_send(src_ni, msg);
1308
1309                         lnet_net_lock(cpt);
1310                         lnet_ni_decref_locked(src_ni, cpt);
1311                         lnet_net_unlock(cpt);
1312                         return 0;
1313                 }
1314
1315                 rc = lnet_nid2peer_locked(&lp, dst_nid, cpt);
1316                 /* lp has ref on src_ni; lose mine */
1317                 lnet_ni_decref_locked(src_ni, cpt);
1318                 if (rc != 0) {
1319                         lnet_net_unlock(cpt);
1320                         LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1321                                       libcfs_nid2str(dst_nid));
1322                         /* ENOMEM or shutting down */
1323                         return rc;
1324                 }
1325                 LASSERT (lp->lp_ni == src_ni);
1326         } else {
1327 #ifndef __KERNEL__
1328                 lnet_net_unlock(cpt);
1329
1330                 /* NB
1331                  * - once application finishes computation, check here to update
1332                  *   router states before it waits for pending IO in LNetEQPoll
1333                  * - recursion breaker: router checker sends no message
1334                  *   to remote networks */
1335                 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1336                         lnet_router_checker();
1337
1338                 lnet_net_lock(cpt);
1339 #endif
1340                 /* sending to a remote network */
1341                 lp = lnet_find_route_locked(src_ni, dst_nid, rtr_nid);
1342                 if (lp == NULL) {
1343                         if (src_ni != NULL)
1344                                 lnet_ni_decref_locked(src_ni, cpt);
1345                         lnet_net_unlock(cpt);
1346
1347                         LCONSOLE_WARN("No route to %s via %s "
1348                                       "(all routers down)\n",
1349                                       libcfs_id2str(msg->msg_target),
1350                                       libcfs_nid2str(src_nid));
1351                         return -EHOSTUNREACH;
1352                 }
1353
1354                 /* rtr_nid is LNET_NID_ANY or NID of pre-determined router,
1355                  * it's possible that rtr_nid isn't LNET_NID_ANY and lp isn't
1356                  * pre-determined router, this can happen if router table
1357                  * was changed when we release the lock */
1358                 if (rtr_nid != lp->lp_nid) {
1359                         cpt2 = lnet_cpt_of_nid_locked(lp->lp_nid);
1360                         if (cpt2 != cpt) {
1361                                 if (src_ni != NULL)
1362                                         lnet_ni_decref_locked(src_ni, cpt);
1363                                 lnet_net_unlock(cpt);
1364
1365                                 rtr_nid = lp->lp_nid;
1366                                 cpt = cpt2;
1367                                 goto again;
1368                         }
1369                 }
1370
1371                 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1372                        libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1373                        lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1374
1375                 if (src_ni == NULL) {
1376                         src_ni = lp->lp_ni;
1377                         src_nid = src_ni->ni_nid;
1378                 } else {
1379                         LASSERT (src_ni == lp->lp_ni);
1380                         lnet_ni_decref_locked(src_ni, cpt);
1381                 }
1382
1383                 lnet_peer_addref_locked(lp);
1384
1385                 LASSERT(src_nid != LNET_NID_ANY);
1386                 lnet_msg_commit(msg, cpt);
1387
1388                 if (!msg->msg_routing) {
1389                         /* I'm the source and now I know which NI to send on */
1390                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1391                 }
1392
1393                 msg->msg_target_is_router = 1;
1394                 msg->msg_target.nid = lp->lp_nid;
1395                 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1396         }
1397
1398         /* 'lp' is our best choice of peer */
1399
1400         LASSERT (!msg->msg_peertxcredit);
1401         LASSERT (!msg->msg_txcredit);
1402         LASSERT (msg->msg_txpeer == NULL);
1403
1404         msg->msg_txpeer = lp;                   /* msg takes my ref on lp */
1405
1406         rc = lnet_post_send_locked(msg, 0);
1407         lnet_net_unlock(cpt);
1408
1409         if (rc == EHOSTUNREACH || rc == ECANCELED)
1410                 return -rc;
1411
1412         if (rc == 0)
1413                 lnet_ni_send(src_ni, msg);
1414
1415         return 0; /* rc == 0 or EAGAIN */
1416 }
1417
1418 static void
1419 lnet_drop_message(lnet_ni_t *ni, int cpt, void *private, unsigned int nob)
1420 {
1421         lnet_net_lock(cpt);
1422         the_lnet.ln_counters[cpt]->drop_count++;
1423         the_lnet.ln_counters[cpt]->drop_length += nob;
1424         lnet_net_unlock(cpt);
1425
1426         lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1427 }
1428
1429 static void
1430 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1431 {
1432         lnet_hdr_t      *hdr = &msg->msg_hdr;
1433
1434         if (msg->msg_wanted != 0)
1435                 lnet_setpayloadbuffer(msg);
1436
1437         lnet_build_msg_event(msg, LNET_EVENT_PUT);
1438
1439         /* Must I ACK?  If so I'll grab the ack_wmd out of the header and put
1440          * it back into the ACK during lnet_finalize() */
1441         msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1442                         (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1443
1444         lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1445                      msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1446 }
1447
1448 static int
1449 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1450 {
1451         lnet_hdr_t              *hdr = &msg->msg_hdr;
1452         struct lnet_match_info  info;
1453         int                     rc;
1454
1455         /* Convert put fields to host byte order */
1456         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1457         hdr->msg.put.ptl_index  = le32_to_cpu(hdr->msg.put.ptl_index);
1458         hdr->msg.put.offset     = le32_to_cpu(hdr->msg.put.offset);
1459
1460         info.mi_id.nid  = hdr->src_nid;
1461         info.mi_id.pid  = hdr->src_pid;
1462         info.mi_opc     = LNET_MD_OP_PUT;
1463         info.mi_portal  = hdr->msg.put.ptl_index;
1464         info.mi_rlength = hdr->payload_length;
1465         info.mi_roffset = hdr->msg.put.offset;
1466         info.mi_mbits   = hdr->msg.put.match_bits;
1467
1468         msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1469
1470  again:
1471         rc = lnet_ptl_match_md(&info, msg);
1472         switch (rc) {
1473         default:
1474                 LBUG();
1475
1476         case LNET_MATCHMD_OK:
1477                 lnet_recv_put(ni, msg);
1478                 return 0;
1479
1480         case LNET_MATCHMD_NONE:
1481                 if (msg->msg_rx_delayed) /* attached on delayed list */
1482                         return 0;
1483
1484                 rc = lnet_ni_eager_recv(ni, msg);
1485                 if (rc == 0)
1486                         goto again;
1487                 /* fall through */
1488
1489         case LNET_MATCHMD_DROP:
1490                 CNETERR("Dropping PUT from %s portal %d match "LPU64
1491                         " offset %d length %d: %d\n",
1492                         libcfs_id2str(info.mi_id), info.mi_portal,
1493                         info.mi_mbits, info.mi_roffset, info.mi_rlength, rc);
1494
1495                 return ENOENT;  /* +ve: OK but no match */
1496         }
1497 }
1498
1499 static int
1500 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1501 {
1502         struct lnet_match_info  info;
1503         lnet_hdr_t              *hdr = &msg->msg_hdr;
1504         lnet_handle_wire_t      reply_wmd;
1505         int                     rc;
1506
1507         /* Convert get fields to host byte order */
1508         hdr->msg.get.match_bits   = le64_to_cpu(hdr->msg.get.match_bits);
1509         hdr->msg.get.ptl_index    = le32_to_cpu(hdr->msg.get.ptl_index);
1510         hdr->msg.get.sink_length  = le32_to_cpu(hdr->msg.get.sink_length);
1511         hdr->msg.get.src_offset   = le32_to_cpu(hdr->msg.get.src_offset);
1512
1513         info.mi_id.nid  = hdr->src_nid;
1514         info.mi_id.pid  = hdr->src_pid;
1515         info.mi_opc     = LNET_MD_OP_GET;
1516         info.mi_portal  = hdr->msg.get.ptl_index;
1517         info.mi_rlength = hdr->msg.get.sink_length;
1518         info.mi_roffset = hdr->msg.get.src_offset;
1519         info.mi_mbits   = hdr->msg.get.match_bits;
1520
1521         rc = lnet_ptl_match_md(&info, msg);
1522         if (rc == LNET_MATCHMD_DROP) {
1523                 CNETERR("Dropping GET from %s portal %d match "LPU64
1524                         " offset %d length %d\n",
1525                         libcfs_id2str(info.mi_id), info.mi_portal,
1526                         info.mi_mbits, info.mi_roffset, info.mi_rlength);
1527                 return ENOENT;  /* +ve: OK but no match */
1528         }
1529
1530         LASSERT(rc == LNET_MATCHMD_OK);
1531
1532         lnet_build_msg_event(msg, LNET_EVENT_GET);
1533
1534         reply_wmd = hdr->msg.get.return_wmd;
1535
1536         lnet_prep_send(msg, LNET_MSG_REPLY, info.mi_id,
1537                        msg->msg_offset, msg->msg_wanted);
1538
1539         msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1540
1541         if (rdma_get) {
1542                 /* The LND completes the REPLY from her recv procedure */
1543                 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1544                              msg->msg_offset, msg->msg_len, msg->msg_len);
1545                 return 0;
1546         }
1547
1548         lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1549         msg->msg_receiving = 0;
1550
1551         rc = lnet_send(ni->ni_nid, msg, LNET_NID_ANY);
1552         if (rc < 0) {
1553                 /* didn't get as far as lnet_ni_send() */
1554                 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1555                        libcfs_nid2str(ni->ni_nid),
1556                        libcfs_id2str(info.mi_id), rc);
1557
1558                 lnet_finalize(ni, msg, rc);
1559         }
1560
1561         return 0;
1562 }
1563
1564 static int
1565 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1566 {
1567         void             *private = msg->msg_private;
1568         lnet_hdr_t       *hdr = &msg->msg_hdr;
1569         lnet_process_id_t src = {0};
1570         lnet_libmd_t     *md;
1571         int               rlength;
1572         int               mlength;
1573         int                     cpt;
1574
1575         cpt = lnet_cpt_of_cookie(hdr->msg.reply.dst_wmd.wh_object_cookie);
1576         lnet_res_lock(cpt);
1577
1578         src.nid = hdr->src_nid;
1579         src.pid = hdr->src_pid;
1580
1581         /* NB handles only looked up by creator (no flips) */
1582         md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1583         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1584                 CNETERR("%s: Dropping REPLY from %s for %s "
1585                         "MD "LPX64"."LPX64"\n",
1586                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1587                         (md == NULL) ? "invalid" : "inactive",
1588                         hdr->msg.reply.dst_wmd.wh_interface_cookie,
1589                         hdr->msg.reply.dst_wmd.wh_object_cookie);
1590                 if (md != NULL && md->md_me != NULL)
1591                         CERROR("REPLY MD also attached to portal %d\n",
1592                                md->md_me->me_portal);
1593
1594                 lnet_res_unlock(cpt);
1595                 return ENOENT;                  /* +ve: OK but no match */
1596         }
1597
1598         LASSERT (md->md_offset == 0);
1599
1600         rlength = hdr->payload_length;
1601         mlength = MIN(rlength, (int)md->md_length);
1602
1603         if (mlength < rlength &&
1604             (md->md_options & LNET_MD_TRUNCATE) == 0) {
1605                 CNETERR("%s: Dropping REPLY from %s length %d "
1606                         "for MD "LPX64" would overflow (%d)\n",
1607                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1608                         rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1609                         mlength);
1610                 lnet_res_unlock(cpt);
1611                 return ENOENT;          /* +ve: OK but no match */
1612         }
1613
1614         CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1615                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1616                mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1617
1618         lnet_msg_attach_md(msg, md, 0, mlength);
1619
1620         if (mlength != 0)
1621                 lnet_setpayloadbuffer(msg);
1622
1623         lnet_res_unlock(cpt);
1624
1625         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1626
1627         lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1628         return 0;
1629 }
1630
1631 static int
1632 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1633 {
1634         lnet_hdr_t       *hdr = &msg->msg_hdr;
1635         lnet_process_id_t src = {0};
1636         lnet_libmd_t     *md;
1637         int                     cpt;
1638
1639         src.nid = hdr->src_nid;
1640         src.pid = hdr->src_pid;
1641
1642         /* Convert ack fields to host byte order */
1643         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1644         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1645
1646         cpt = lnet_cpt_of_cookie(hdr->msg.ack.dst_wmd.wh_object_cookie);
1647         lnet_res_lock(cpt);
1648
1649         /* NB handles only looked up by creator (no flips) */
1650         md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1651         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1652                 /* Don't moan; this is expected */
1653                 CDEBUG(D_NET,
1654                        "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1655                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1656                        (md == NULL) ? "invalid" : "inactive",
1657                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1658                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1659                 if (md != NULL && md->md_me != NULL)
1660                         CERROR("Source MD also attached to portal %d\n",
1661                                md->md_me->me_portal);
1662
1663                 lnet_res_unlock(cpt);
1664                 return ENOENT;                  /* +ve! */
1665         }
1666
1667         CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1668                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1669                hdr->msg.ack.dst_wmd.wh_object_cookie);
1670
1671         lnet_msg_attach_md(msg, md, 0, 0);
1672
1673         lnet_res_unlock(cpt);
1674
1675         lnet_build_msg_event(msg, LNET_EVENT_ACK);
1676
1677         lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1678         return 0;
1679 }
1680
1681 static int
1682 lnet_parse_forward_locked(lnet_ni_t *ni, lnet_msg_t *msg)
1683 {
1684         int     rc = 0;
1685
1686 #ifdef __KERNEL__
1687         if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1688             lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1689                 if (ni->ni_lnd->lnd_eager_recv == NULL) {
1690                         msg->msg_rx_ready_delay = 1;
1691                 } else {
1692                         lnet_net_unlock(msg->msg_rx_cpt);
1693                         rc = lnet_ni_eager_recv(ni, msg);
1694                         lnet_net_lock(msg->msg_rx_cpt);
1695                 }
1696         }
1697
1698         if (rc == 0)
1699                 rc = lnet_post_routed_recv_locked(msg, 0);
1700 #else
1701         LBUG();
1702 #endif
1703         return rc;
1704 }
1705
1706 char *
1707 lnet_msgtyp2str (int type)
1708 {
1709         switch (type) {
1710         case LNET_MSG_ACK:
1711                 return ("ACK");
1712         case LNET_MSG_PUT:
1713                 return ("PUT");
1714         case LNET_MSG_GET:
1715                 return ("GET");
1716         case LNET_MSG_REPLY:
1717                 return ("REPLY");
1718         case LNET_MSG_HELLO:
1719                 return ("HELLO");
1720         default:
1721                 return ("<UNKNOWN>");
1722         }
1723 }
1724 EXPORT_SYMBOL(lnet_msgtyp2str);
1725
1726 void
1727 lnet_print_hdr(lnet_hdr_t * hdr)
1728 {
1729         lnet_process_id_t src = {0};
1730         lnet_process_id_t dst = {0};
1731         char *type_str = lnet_msgtyp2str (hdr->type);
1732
1733         src.nid = hdr->src_nid;
1734         src.pid = hdr->src_pid;
1735
1736         dst.nid = hdr->dest_nid;
1737         dst.pid = hdr->dest_pid;
1738
1739         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1740         CWARN("    From %s\n", libcfs_id2str(src));
1741         CWARN("    To   %s\n", libcfs_id2str(dst));
1742
1743         switch (hdr->type) {
1744         default:
1745                 break;
1746
1747         case LNET_MSG_PUT:
1748                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
1749                       "match bits "LPU64"\n",
1750                       hdr->msg.put.ptl_index,
1751                       hdr->msg.put.ack_wmd.wh_interface_cookie,
1752                       hdr->msg.put.ack_wmd.wh_object_cookie,
1753                       hdr->msg.put.match_bits);
1754                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
1755                       hdr->payload_length, hdr->msg.put.offset,
1756                       hdr->msg.put.hdr_data);
1757                 break;
1758
1759         case LNET_MSG_GET:
1760                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
1761                       "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1762                       hdr->msg.get.return_wmd.wh_interface_cookie,
1763                       hdr->msg.get.return_wmd.wh_object_cookie,
1764                       hdr->msg.get.match_bits);
1765                 CWARN("    Length %d, src offset %d\n",
1766                       hdr->msg.get.sink_length,
1767                       hdr->msg.get.src_offset);
1768                 break;
1769
1770         case LNET_MSG_ACK:
1771                 CWARN("    dst md "LPX64"."LPX64", "
1772                       "manipulated length %d\n",
1773                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
1774                       hdr->msg.ack.dst_wmd.wh_object_cookie,
1775                       hdr->msg.ack.mlength);
1776                 break;
1777
1778         case LNET_MSG_REPLY:
1779                 CWARN("    dst md "LPX64"."LPX64", "
1780                       "length %d\n",
1781                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
1782                       hdr->msg.reply.dst_wmd.wh_object_cookie,
1783                       hdr->payload_length);
1784         }
1785
1786 }
1787
1788 int
1789 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid,
1790            void *private, int rdma_req)
1791 {
1792         int             rc = 0;
1793         int             cpt;
1794         int             for_me;
1795         struct lnet_msg *msg;
1796         lnet_pid_t     dest_pid;
1797         lnet_nid_t     dest_nid;
1798         lnet_nid_t     src_nid;
1799         __u32          payload_length;
1800         __u32          type;
1801
1802         LASSERT (!in_interrupt ());
1803
1804         type = le32_to_cpu(hdr->type);
1805         src_nid = le64_to_cpu(hdr->src_nid);
1806         dest_nid = le64_to_cpu(hdr->dest_nid);
1807         dest_pid = le32_to_cpu(hdr->dest_pid);
1808         payload_length = le32_to_cpu(hdr->payload_length);
1809
1810         for_me = (ni->ni_nid == dest_nid);
1811         cpt = lnet_cpt_of_nid(from_nid);
1812
1813         switch (type) {
1814         case LNET_MSG_ACK:
1815         case LNET_MSG_GET:
1816                 if (payload_length > 0) {
1817                         CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1818                                libcfs_nid2str(from_nid),
1819                                libcfs_nid2str(src_nid),
1820                                lnet_msgtyp2str(type), payload_length);
1821                         return -EPROTO;
1822                 }
1823                 break;
1824
1825         case LNET_MSG_PUT:
1826         case LNET_MSG_REPLY:
1827                 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1828                         CERROR("%s, src %s: bad %s payload %d "
1829                                "(%d max expected)\n",
1830                                libcfs_nid2str(from_nid),
1831                                libcfs_nid2str(src_nid),
1832                                lnet_msgtyp2str(type),
1833                                payload_length,
1834                                for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1835                         return -EPROTO;
1836                 }
1837                 break;
1838
1839         default:
1840                 CERROR("%s, src %s: Bad message type 0x%x\n",
1841                        libcfs_nid2str(from_nid),
1842                        libcfs_nid2str(src_nid), type);
1843                 return -EPROTO;
1844         }
1845
1846         if (the_lnet.ln_routing &&
1847             ni->ni_last_alive != cfs_time_current_sec()) {
1848                 lnet_ni_lock(ni);
1849
1850                 /* NB: so far here is the only place to set NI status to "up */
1851                 ni->ni_last_alive = cfs_time_current_sec();
1852                 if (ni->ni_status != NULL &&
1853                     ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1854                         ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1855                 lnet_ni_unlock(ni);
1856         }
1857
1858         /* Regard a bad destination NID as a protocol error.  Senders should
1859          * know what they're doing; if they don't they're misconfigured, buggy
1860          * or malicious so we chop them off at the knees :) */
1861
1862         if (!for_me) {
1863                 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1864                         /* should have gone direct */
1865                         CERROR ("%s, src %s: Bad dest nid %s "
1866                                 "(should have been sent direct)\n",
1867                                 libcfs_nid2str(from_nid),
1868                                 libcfs_nid2str(src_nid),
1869                                 libcfs_nid2str(dest_nid));
1870                         return -EPROTO;
1871                 }
1872
1873                 if (lnet_islocalnid(dest_nid)) {
1874                         /* dest is another local NI; sender should have used
1875                          * this node's NID on its own network */
1876                         CERROR ("%s, src %s: Bad dest nid %s "
1877                                 "(it's my nid but on a different network)\n",
1878                                 libcfs_nid2str(from_nid),
1879                                 libcfs_nid2str(src_nid),
1880                                 libcfs_nid2str(dest_nid));
1881                         return -EPROTO;
1882                 }
1883
1884                 if (rdma_req && type == LNET_MSG_GET) {
1885                         CERROR ("%s, src %s: Bad optimized GET for %s "
1886                                 "(final destination must be me)\n",
1887                                 libcfs_nid2str(from_nid),
1888                                 libcfs_nid2str(src_nid),
1889                                 libcfs_nid2str(dest_nid));
1890                         return -EPROTO;
1891                 }
1892
1893                 if (!the_lnet.ln_routing) {
1894                         CERROR ("%s, src %s: Dropping message for %s "
1895                                 "(routing not enabled)\n",
1896                                 libcfs_nid2str(from_nid),
1897                                 libcfs_nid2str(src_nid),
1898                                 libcfs_nid2str(dest_nid));
1899                         goto drop;
1900                 }
1901         }
1902
1903         /* Message looks OK; we're not going to return an error, so we MUST
1904          * call back lnd_recv() come what may... */
1905
1906         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
1907             fail_peer (src_nid, 0))             /* shall we now? */
1908         {
1909                 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1910                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1911                        lnet_msgtyp2str(type));
1912                 goto drop;
1913         }
1914
1915         msg = lnet_msg_alloc();
1916         if (msg == NULL) {
1917                 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1918                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid), 
1919                        lnet_msgtyp2str(type));
1920                 goto drop;
1921         }
1922
1923         /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
1924
1925         msg->msg_type = type;
1926         msg->msg_private = private;
1927         msg->msg_receiving = 1;
1928         msg->msg_len = msg->msg_wanted = payload_length;
1929         msg->msg_offset = 0;
1930         msg->msg_hdr = *hdr;
1931         /* for building message event */
1932         msg->msg_from = from_nid;
1933         if (!for_me) {
1934                 msg->msg_target.pid     = dest_pid;
1935                 msg->msg_target.nid     = dest_nid;
1936                 msg->msg_routing        = 1;
1937
1938         } else {
1939                 /* convert common msg->hdr fields to host byteorder */
1940                 msg->msg_hdr.type       = type;
1941                 msg->msg_hdr.src_nid    = src_nid;
1942                 msg->msg_hdr.src_pid    = le32_to_cpu(msg->msg_hdr.src_pid);
1943                 msg->msg_hdr.dest_nid   = dest_nid;
1944                 msg->msg_hdr.dest_pid   = dest_pid;
1945                 msg->msg_hdr.payload_length = payload_length;
1946         }
1947
1948         lnet_net_lock(cpt);
1949         rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid, cpt);
1950         if (rc != 0) {
1951                 lnet_net_unlock(cpt);
1952                 CERROR("%s, src %s: Dropping %s "
1953                        "(error %d looking up sender)\n",
1954                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1955                        lnet_msgtyp2str(type), rc);
1956                 lnet_msg_free(msg);
1957                 goto drop;
1958         }
1959
1960         lnet_msg_commit(msg, cpt);
1961
1962         if (!for_me) {
1963                 rc = lnet_parse_forward_locked(ni, msg);
1964                 lnet_net_unlock(cpt);
1965
1966                 if (rc < 0)
1967                         goto free_drop;
1968                 if (rc == 0) {
1969                         lnet_ni_recv(ni, msg->msg_private, msg, 0,
1970                                      0, payload_length, payload_length);
1971                 }
1972                 return 0;
1973         }
1974
1975         lnet_net_unlock(cpt);
1976
1977         switch (type) {
1978         case LNET_MSG_ACK:
1979                 rc = lnet_parse_ack(ni, msg);
1980                 break;
1981         case LNET_MSG_PUT:
1982                 rc = lnet_parse_put(ni, msg);
1983                 break;
1984         case LNET_MSG_GET:
1985                 rc = lnet_parse_get(ni, msg, rdma_req);
1986                 break;
1987         case LNET_MSG_REPLY:
1988                 rc = lnet_parse_reply(ni, msg);
1989                 break;
1990         default:
1991                 LASSERT(0);
1992                 rc = -EPROTO;
1993                 goto free_drop;  /* prevent an unused label if !kernel */
1994         }
1995
1996         if (rc == 0)
1997                 return 0;
1998
1999         LASSERT (rc == ENOENT);
2000
2001  free_drop:
2002         LASSERT(msg->msg_md == NULL);
2003         lnet_finalize(ni, msg, rc);
2004
2005  drop:
2006         lnet_drop_message(ni, cpt, private, payload_length);
2007         return 0;
2008 }
2009 EXPORT_SYMBOL(lnet_parse);
2010
2011 void
2012 lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason)
2013 {
2014         while (!cfs_list_empty(head)) {
2015                 lnet_process_id_t       id = {0};
2016                 lnet_msg_t              *msg;
2017
2018                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
2019                 cfs_list_del(&msg->msg_list);
2020
2021                 id.nid = msg->msg_hdr.src_nid;
2022                 id.pid = msg->msg_hdr.src_pid;
2023
2024                 LASSERT(msg->msg_md == NULL);
2025                 LASSERT(msg->msg_rx_delayed);
2026                 LASSERT(msg->msg_rxpeer != NULL);
2027                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2028
2029                 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
2030                       " offset %d length %d: %s\n",
2031                       libcfs_id2str(id),
2032                       msg->msg_hdr.msg.put.ptl_index,
2033                       msg->msg_hdr.msg.put.match_bits,
2034                       msg->msg_hdr.msg.put.offset,
2035                       msg->msg_hdr.payload_length, reason);
2036
2037                 /* NB I can't drop msg's ref on msg_rxpeer until after I've
2038                  * called lnet_drop_message(), so I just hang onto msg as well
2039                  * until that's done */
2040
2041                 lnet_drop_message(msg->msg_rxpeer->lp_ni,
2042                                   msg->msg_rxpeer->lp_cpt,
2043                                   msg->msg_private, msg->msg_len);
2044                 /*
2045                  * NB: message will not generate event because w/o attached MD,
2046                  * but we still should give error code so lnet_msg_decommit()
2047                  * can skip counters operations and other checks.
2048                  */
2049                 lnet_finalize(msg->msg_rxpeer->lp_ni, msg, -ENOENT);
2050         }
2051 }
2052
2053 void
2054 lnet_recv_delayed_msg_list(cfs_list_t *head)
2055 {
2056         while (!cfs_list_empty(head)) {
2057                 lnet_msg_t        *msg;
2058                 lnet_process_id_t  id;
2059
2060                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
2061                 cfs_list_del(&msg->msg_list);
2062
2063                 /* md won't disappear under me, since each msg
2064                  * holds a ref on it */
2065
2066                 id.nid = msg->msg_hdr.src_nid;
2067                 id.pid = msg->msg_hdr.src_pid;
2068
2069                 LASSERT(msg->msg_rx_delayed);
2070                 LASSERT(msg->msg_md != NULL);
2071                 LASSERT(msg->msg_rxpeer != NULL);
2072                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
2073
2074                 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
2075                        "match "LPU64" offset %d length %d.\n",
2076                         libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
2077                         msg->msg_hdr.msg.put.match_bits,
2078                         msg->msg_hdr.msg.put.offset,
2079                         msg->msg_hdr.payload_length);
2080
2081                 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
2082         }
2083 }
2084
2085 /**
2086  * Initiate an asynchronous PUT operation.
2087  *
2088  * There are several events associated with a PUT: completion of the send on
2089  * the initiator node (LNET_EVENT_SEND), and when the send completes
2090  * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
2091  * that the operation was accepted by the target. The event LNET_EVENT_PUT is
2092  * used at the target node to indicate the completion of incoming data
2093  * delivery.
2094  *
2095  * The local events will be logged in the EQ associated with the MD pointed to
2096  * by \a mdh handle. Using a MD without an associated EQ results in these
2097  * events being discarded. In this case, the caller must have another
2098  * mechanism (e.g., a higher level protocol) for determining when it is safe
2099  * to modify the memory region associated with the MD.
2100  *
2101  * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2102  * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2103  *
2104  * \param self Indicates the NID of a local interface through which to send
2105  * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2106  * \param mdh A handle for the MD that describes the memory to be sent. The MD
2107  * must be "free floating" (See LNetMDBind()).
2108  * \param ack Controls whether an acknowledgment is requested.
2109  * Acknowledgments are only sent when they are requested by the initiating
2110  * process and the target MD enables them.
2111  * \param target A process identifier for the target process.
2112  * \param portal The index in the \a target's portal table.
2113  * \param match_bits The match bits to use for MD selection at the target
2114  * process.
2115  * \param offset The offset into the target MD (only used when the target
2116  * MD has the LNET_MD_MANAGE_REMOTE option set).
2117  * \param hdr_data 64 bits of user data that can be included in the message
2118  * header. This data is written to an event queue entry at the target if an
2119  * EQ is present on the matching MD.
2120  *
2121  * \retval  0      Success, and only in this case events will be generated
2122  * and logged to EQ (if it exists).
2123  * \retval -EIO    Simulated failure.
2124  * \retval -ENOMEM Memory allocation failure.
2125  * \retval -ENOENT Invalid MD object.
2126  *
2127  * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2128  */
2129 int
2130 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2131         lnet_process_id_t target, unsigned int portal,
2132         __u64 match_bits, unsigned int offset,
2133         __u64 hdr_data)
2134 {
2135         struct lnet_msg         *msg;
2136         struct lnet_libmd       *md;
2137         int                     cpt;
2138         int                     rc;
2139
2140         LASSERT (the_lnet.ln_init);
2141         LASSERT (the_lnet.ln_refcount > 0);
2142
2143         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2144             fail_peer (target.nid, 1))          /* shall we now? */
2145         {
2146                 CERROR("Dropping PUT to %s: simulated failure\n",
2147                        libcfs_id2str(target));
2148                 return -EIO;
2149         }
2150
2151         msg = lnet_msg_alloc();
2152         if (msg == NULL) {
2153                 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2154                        libcfs_id2str(target));
2155                 return -ENOMEM;
2156         }
2157         msg->msg_vmflush = !!memory_pressure_get();
2158
2159         cpt = lnet_cpt_of_cookie(mdh.cookie);
2160         lnet_res_lock(cpt);
2161
2162         md = lnet_handle2md(&mdh);
2163         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2164                 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2165                        match_bits, portal, libcfs_id2str(target),
2166                        md == NULL ? -1 : md->md_threshold);
2167                 if (md != NULL && md->md_me != NULL)
2168                         CERROR("Source MD also attached to portal %d\n",
2169                                md->md_me->me_portal);
2170                 lnet_res_unlock(cpt);
2171
2172                 lnet_msg_free(msg);
2173                 return -ENOENT;
2174         }
2175
2176         CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2177
2178         lnet_msg_attach_md(msg, md, 0, 0);
2179
2180         lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2181
2182         msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2183         msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2184         msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2185         msg->msg_hdr.msg.put.hdr_data = hdr_data;
2186
2187         /* NB handles only looked up by creator (no flips) */
2188         if (ack == LNET_ACK_REQ) {
2189                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2190                         the_lnet.ln_interface_cookie;
2191                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2192                         md->md_lh.lh_cookie;
2193         } else {
2194                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2195                         LNET_WIRE_HANDLE_COOKIE_NONE;
2196                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2197                         LNET_WIRE_HANDLE_COOKIE_NONE;
2198         }
2199
2200         lnet_res_unlock(cpt);
2201
2202         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2203
2204         rc = lnet_send(self, msg, LNET_NID_ANY);
2205         if (rc != 0) {
2206                 CNETERR( "Error sending PUT to %s: %d\n",
2207                        libcfs_id2str(target), rc);
2208                 lnet_finalize (NULL, msg, rc);
2209         }
2210
2211         /* completion will be signalled by an event */
2212         return 0;
2213 }
2214 EXPORT_SYMBOL(LNetPut);
2215
2216 lnet_msg_t *
2217 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2218 {
2219         /* The LND can DMA direct to the GET md (i.e. no REPLY msg).  This
2220          * returns a msg for the LND to pass to lnet_finalize() when the sink
2221          * data has been received.
2222          *
2223          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2224          * lnet_finalize() is called on it, so the LND must call this first */
2225
2226         struct lnet_msg         *msg = lnet_msg_alloc();
2227         struct lnet_libmd       *getmd = getmsg->msg_md;
2228         lnet_process_id_t       peer_id = getmsg->msg_target;
2229         int                     cpt;
2230
2231         LASSERT(!getmsg->msg_target_is_router);
2232         LASSERT(!getmsg->msg_routing);
2233
2234         if (msg == NULL) {
2235                 CERROR("%s: Dropping REPLY from %s: can't allocate msg\n",
2236                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2237                 goto drop;
2238         }
2239
2240         cpt = lnet_cpt_of_cookie(getmd->md_lh.lh_cookie);
2241         lnet_res_lock(cpt);
2242
2243         LASSERT(getmd->md_refcount > 0);
2244
2245         if (getmd->md_threshold == 0) {
2246                 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2247                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), 
2248                         getmd);
2249                 lnet_res_unlock(cpt);
2250                 goto drop;
2251         }
2252
2253         LASSERT(getmd->md_offset == 0);
2254
2255         CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2256                libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2257
2258         /* setup information for lnet_build_msg_event */
2259         msg->msg_from = peer_id.nid;
2260         msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2261         msg->msg_hdr.src_nid = peer_id.nid;
2262         msg->msg_hdr.payload_length = getmd->md_length;
2263         msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2264
2265         lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2266         lnet_res_unlock(cpt);
2267
2268         cpt = lnet_cpt_of_nid(peer_id.nid);
2269
2270         lnet_net_lock(cpt);
2271         lnet_msg_commit(msg, cpt);
2272         lnet_net_unlock(cpt);
2273
2274         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2275
2276         return msg;
2277
2278  drop:
2279         cpt = lnet_cpt_of_nid(peer_id.nid);
2280
2281         lnet_net_lock(cpt);
2282         the_lnet.ln_counters[cpt]->drop_count++;
2283         the_lnet.ln_counters[cpt]->drop_length += getmd->md_length;
2284         lnet_net_unlock(cpt);
2285
2286         if (msg != NULL)
2287                 lnet_msg_free(msg);
2288
2289         return NULL;
2290 }
2291 EXPORT_SYMBOL(lnet_create_reply_msg);
2292
2293 void
2294 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2295 {
2296         /* Set the REPLY length, now the RDMA that elides the REPLY message has
2297          * completed and I know it. */
2298         LASSERT (reply != NULL);
2299         LASSERT (reply->msg_type == LNET_MSG_GET);
2300         LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2301
2302         /* NB I trusted my peer to RDMA.  If she tells me she's written beyond
2303          * the end of my buffer, I might as well be dead. */
2304         LASSERT (len <= reply->msg_ev.mlength);
2305
2306         reply->msg_ev.mlength = len;
2307 }
2308 EXPORT_SYMBOL(lnet_set_reply_msg_len);
2309
2310 /**
2311  * Initiate an asynchronous GET operation.
2312  *
2313  * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2314  * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2315  * the target node in the REPLY has been written to local MD.
2316  *
2317  * On the target node, an LNET_EVENT_GET is logged when the GET request
2318  * arrives and is accepted into a MD.
2319  *
2320  * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2321  * \param mdh A handle for the MD that describes the memory into which the
2322  * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2323  *
2324  * \retval  0      Success, and only in this case events will be generated
2325  * and logged to EQ (if it exists) of the MD.
2326  * \retval -EIO    Simulated failure.
2327  * \retval -ENOMEM Memory allocation failure.
2328  * \retval -ENOENT Invalid MD object.
2329  */
2330 int
2331 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh,
2332         lnet_process_id_t target, unsigned int portal,
2333         __u64 match_bits, unsigned int offset)
2334 {
2335         struct lnet_msg         *msg;
2336         struct lnet_libmd       *md;
2337         int                     cpt;
2338         int                     rc;
2339
2340         LASSERT (the_lnet.ln_init);
2341         LASSERT (the_lnet.ln_refcount > 0);
2342
2343         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2344             fail_peer (target.nid, 1))          /* shall we now? */
2345         {
2346                 CERROR("Dropping GET to %s: simulated failure\n",
2347                        libcfs_id2str(target));
2348                 return -EIO;
2349         }
2350
2351         msg = lnet_msg_alloc();
2352         if (msg == NULL) {
2353                 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2354                        libcfs_id2str(target));
2355                 return -ENOMEM;
2356         }
2357
2358         cpt = lnet_cpt_of_cookie(mdh.cookie);
2359         lnet_res_lock(cpt);
2360
2361         md = lnet_handle2md(&mdh);
2362         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2363                 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2364                        match_bits, portal, libcfs_id2str(target),
2365                        md == NULL ? -1 : md->md_threshold);
2366                 if (md != NULL && md->md_me != NULL)
2367                         CERROR("REPLY MD also attached to portal %d\n",
2368                                md->md_me->me_portal);
2369
2370                 lnet_res_unlock(cpt);
2371
2372                 lnet_msg_free(msg);
2373                 return -ENOENT;
2374         }
2375
2376         CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2377
2378         lnet_msg_attach_md(msg, md, 0, 0);
2379
2380         lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2381
2382         msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2383         msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2384         msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2385         msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2386
2387         /* NB handles only looked up by creator (no flips) */
2388         msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie =
2389                 the_lnet.ln_interface_cookie;
2390         msg->msg_hdr.msg.get.return_wmd.wh_object_cookie =
2391                 md->md_lh.lh_cookie;
2392
2393         lnet_res_unlock(cpt);
2394
2395         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2396
2397         rc = lnet_send(self, msg, LNET_NID_ANY);
2398         if (rc < 0) {
2399                 CNETERR("Error sending GET to %s: %d\n",
2400                         libcfs_id2str(target), rc);
2401                 lnet_finalize(NULL, msg, rc);
2402         }
2403
2404         /* completion will be signalled by an event */
2405         return 0;
2406 }
2407 EXPORT_SYMBOL(LNetGet);
2408
2409 /**
2410  * Calculate distance to node at \a dstnid.
2411  *
2412  * \param dstnid Target NID.
2413  * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2414  * is saved here.
2415  * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2416  * here.
2417  *
2418  * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2419  * local_nid_dist_zero is set, which is the default.
2420  * \retval positives Distance to target NID, i.e. number of hops plus one.
2421  * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2422  */
2423 int
2424 LNetDist(lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2425 {
2426         cfs_list_t              *e;
2427         struct lnet_ni          *ni;
2428         lnet_remotenet_t        *rnet;
2429         __u32                   dstnet = LNET_NIDNET(dstnid);
2430         int                     hops;
2431         int                     cpt;
2432         __u32                   order = 2;
2433         cfs_list_t              *rn_list;
2434
2435         /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2436          * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2437          * keep order 0 free for 0@lo and order 1 free for a local NID
2438          * match */
2439
2440         LASSERT (the_lnet.ln_init);
2441         LASSERT (the_lnet.ln_refcount > 0);
2442
2443         cpt = lnet_net_lock_current();
2444
2445         cfs_list_for_each (e, &the_lnet.ln_nis) {
2446                 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2447
2448                 if (ni->ni_nid == dstnid) {
2449                         if (srcnidp != NULL)
2450                                 *srcnidp = dstnid;
2451                         if (orderp != NULL) {
2452                                 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2453                                         *orderp = 0;
2454                                 else
2455                                         *orderp = 1;
2456                         }
2457                         lnet_net_unlock(cpt);
2458
2459                         return local_nid_dist_zero ? 0 : 1;
2460                 }
2461
2462                 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2463                         if (srcnidp != NULL)
2464                                 *srcnidp = ni->ni_nid;
2465                         if (orderp != NULL)
2466                                 *orderp = order;
2467                         lnet_net_unlock(cpt);
2468                         return 1;
2469                 }
2470
2471                 order++;
2472         }
2473
2474         rn_list = lnet_net2rnethash(dstnet);
2475         cfs_list_for_each(e, rn_list) {
2476                 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2477
2478                 if (rnet->lrn_net == dstnet) {
2479                         lnet_route_t *route;
2480                         lnet_route_t *shortest = NULL;
2481
2482                         LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2483
2484                         cfs_list_for_each_entry(route, &rnet->lrn_routes,
2485                                                 lr_list) {
2486                                 if (shortest == NULL ||
2487                                     route->lr_hops < shortest->lr_hops)
2488                                         shortest = route;
2489                         }
2490
2491                         LASSERT (shortest != NULL);
2492                         hops = shortest->lr_hops;
2493                         if (srcnidp != NULL)
2494                                 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2495                         if (orderp != NULL)
2496                                 *orderp = order;
2497                         lnet_net_unlock(cpt);
2498                         return hops + 1;
2499                 }
2500                 order++;
2501         }
2502
2503         lnet_net_unlock(cpt);
2504         return -EHOSTUNREACH;
2505 }
2506 EXPORT_SYMBOL(LNetDist);
2507
2508 /**
2509  * Set the number of asynchronous messages expected from a target process.
2510  *
2511  * This function is only meaningful for userspace callers. It's a no-op when
2512  * called from kernel.
2513  *
2514  * Asynchronous messages are those that can come from a target when the
2515  * userspace process is not waiting for IO to complete; e.g., AST callbacks
2516  * from Lustre servers. Specifying the expected number of such messages
2517  * allows them to be eagerly received when user process is not running in
2518  * LNet; otherwise network errors may occur.
2519  *
2520  * \param id Process ID of the target process.
2521  * \param nasync Number of asynchronous messages expected from the target.
2522  *
2523  * \return 0 on success, and an error code otherwise.
2524  */
2525 int
2526 LNetSetAsync(lnet_process_id_t id, int nasync)
2527 {
2528 #ifdef __KERNEL__
2529         return 0;
2530 #else
2531         lnet_ni_t        *ni;
2532         lnet_remotenet_t *rnet;
2533         cfs_list_t       *tmp;
2534         lnet_route_t     *route;
2535         lnet_nid_t       *nids;
2536         int               nnids;
2537         int               maxnids = 256;
2538         int               rc = 0;
2539         int               rc2;
2540         int                     cpt;
2541
2542         /* Target on a local network? */
2543         ni = lnet_net2ni(LNET_NIDNET(id.nid));
2544         if (ni != NULL) {
2545                 if (ni->ni_lnd->lnd_setasync != NULL) 
2546                         rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2547                 lnet_ni_decref(ni);
2548                 return rc;
2549         }
2550
2551         /* Target on a remote network: apply to routers */
2552  again:
2553         LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2554         if (nids == NULL)
2555                 return -ENOMEM;
2556         nnids = 0;
2557
2558         /* Snapshot all the router NIDs */
2559         cpt = lnet_net_lock_current();
2560         rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2561         if (rnet != NULL) {
2562                 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2563                         if (nnids == maxnids) {
2564                                 lnet_net_unlock(cpt);
2565                                 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2566                                 maxnids *= 2;
2567                                 goto again;
2568                         }
2569
2570                         route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2571                         nids[nnids++] = route->lr_gateway->lp_nid;
2572                 }
2573         }
2574         lnet_net_unlock(cpt);
2575
2576         /* set async on all the routers */
2577         while (nnids-- > 0) {
2578                 id.pid = LUSTRE_SRV_LNET_PID;
2579                 id.nid = nids[nnids];
2580
2581                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2582                 if (ni == NULL)
2583                         continue;
2584
2585                 if (ni->ni_lnd->lnd_setasync != NULL) {
2586                         rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2587                         if (rc2 != 0)
2588                                 rc = rc2;
2589                 }
2590                 lnet_ni_decref(ni);
2591         }
2592
2593         LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2594         return rc;
2595 #endif
2596 }
2597 EXPORT_SYMBOL(LNetSetAsync);