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