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