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