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