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