Whamcloud - gitweb
LU-56 lnet: match-table for Portals
[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 int
632 lnet_compare_routes(lnet_route_t *r1, lnet_route_t *r2)
633 {
634         lnet_peer_t *p1 = r1->lr_gateway;
635         lnet_peer_t *p2 = r2->lr_gateway;
636
637         if (r1->lr_hops < r2->lr_hops)
638                 return 1;
639
640         if (r1->lr_hops > r2->lr_hops)
641                 return -1;
642
643         if (p1->lp_txqnob < p2->lp_txqnob)
644                 return 1;
645
646         if (p1->lp_txqnob > p2->lp_txqnob)
647                 return -1;
648
649         if (p1->lp_txcredits > p2->lp_txcredits)
650                 return 1;
651
652         if (p1->lp_txcredits < p2->lp_txcredits)
653                 return -1;
654
655         return 0;
656 }
657
658
659 void
660 lnet_setpayloadbuffer(lnet_msg_t *msg)
661 {
662         lnet_libmd_t *md = msg->msg_md;
663
664         LASSERT (msg->msg_len > 0);
665         LASSERT (!msg->msg_routing);
666         LASSERT (md != NULL);
667         LASSERT (msg->msg_niov == 0);
668         LASSERT (msg->msg_iov == NULL);
669         LASSERT (msg->msg_kiov == NULL);
670
671         msg->msg_niov = md->md_niov;
672         if ((md->md_options & LNET_MD_KIOV) != 0)
673                 msg->msg_kiov = md->md_iov.kiov;
674         else
675                 msg->msg_iov = md->md_iov.iov;
676 }
677
678 void
679 lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
680                unsigned int offset, unsigned int len) 
681 {
682         msg->msg_type = type;
683         msg->msg_target = target;
684         msg->msg_len = len;
685         msg->msg_offset = offset;
686
687         if (len != 0)
688                 lnet_setpayloadbuffer(msg);
689
690         memset (&msg->msg_hdr, 0, sizeof (msg->msg_hdr));
691         msg->msg_hdr.type           = cpu_to_le32(type);
692         msg->msg_hdr.dest_nid       = cpu_to_le64(target.nid);
693         msg->msg_hdr.dest_pid       = cpu_to_le32(target.pid);
694         /* src_nid will be set later */
695         msg->msg_hdr.src_pid        = cpu_to_le32(the_lnet.ln_pid);
696         msg->msg_hdr.payload_length = cpu_to_le32(len);
697 }
698
699 void
700 lnet_ni_send(lnet_ni_t *ni, lnet_msg_t *msg)
701 {
702         void   *priv = msg->msg_private;
703         int     rc;
704
705         LASSERT (!cfs_in_interrupt ());
706         LASSERT (LNET_NETTYP(LNET_NIDNET(ni->ni_nid)) == LOLND ||
707                  (msg->msg_txcredit && msg->msg_peertxcredit));
708
709         rc = (ni->ni_lnd->lnd_send)(ni, priv, msg);
710         if (rc < 0)
711                 lnet_finalize(ni, msg, rc);
712 }
713
714 int
715 lnet_ni_eager_recv(lnet_ni_t *ni, lnet_msg_t *msg)
716 {
717         int     rc;
718
719         LASSERT(!msg->msg_sending);
720         LASSERT(msg->msg_receiving);
721         LASSERT(ni->ni_lnd->lnd_eager_recv != NULL);
722
723         msg->msg_rx_ready_delay = 1;
724         rc = (ni->ni_lnd->lnd_eager_recv)(ni, msg->msg_private, msg,
725                                           &msg->msg_private);
726         if (rc != 0) {
727                 CERROR("recv from %s / send to %s aborted: "
728                        "eager_recv failed %d\n",
729                        libcfs_nid2str(msg->msg_rxpeer->lp_nid),
730                        libcfs_id2str(msg->msg_target), rc);
731                 LASSERT(rc < 0); /* required by my callers */
732         }
733
734         return rc;
735 }
736
737 int
738 lnet_ni_eager_recv_locked(lnet_ni_t *ni, lnet_msg_t *msg)
739 {
740         int     rc;
741
742         if (ni->ni_lnd->lnd_eager_recv == NULL) {
743                 msg->msg_rx_ready_delay = 1;
744                 return 0;
745         }
746
747         LNET_UNLOCK();
748         rc = lnet_ni_eager_recv(ni, msg);
749         LNET_LOCK();
750
751         return rc;
752 }
753
754 /* NB: caller shall hold a ref on 'lp' as I'd drop LNET_LOCK */
755 void
756 lnet_ni_peer_alive(lnet_peer_t *lp)
757 {
758         cfs_time_t  last_alive = 0;
759         lnet_ni_t  *ni = lp->lp_ni;
760
761         LASSERT (lnet_peer_aliveness_enabled(lp));
762         LASSERT (ni->ni_lnd->lnd_query != NULL);
763         LASSERT (the_lnet.ln_routing == 1);
764
765         LNET_UNLOCK();
766         (ni->ni_lnd->lnd_query)(ni, lp->lp_nid, &last_alive);
767         LNET_LOCK();
768
769         lp->lp_last_query = cfs_time_current();
770
771         if (last_alive != 0) /* NI has updated timestamp */
772                 lp->lp_last_alive = last_alive;
773 }
774
775 /* NB: always called with LNET_LOCK held */
776 static inline int
777 lnet_peer_is_alive (lnet_peer_t *lp, cfs_time_t now)
778 {
779         int        alive;
780         cfs_time_t deadline;
781
782         LASSERT (lnet_peer_aliveness_enabled(lp));
783         LASSERT (the_lnet.ln_routing == 1);
784
785         /* Trust lnet_notify() if it has more recent aliveness news, but
786          * ignore the initial assumed death (see lnet_peers_start_down()).
787          */
788         if (!lp->lp_alive && lp->lp_alive_count > 0 &&
789             cfs_time_aftereq(lp->lp_timestamp, lp->lp_last_alive))
790                 return 0;
791
792         deadline = cfs_time_add(lp->lp_last_alive,
793                                 cfs_time_seconds(lp->lp_ni->ni_peertimeout));
794         alive = cfs_time_after(deadline, now);
795
796         /* Update obsolete lp_alive except for routers assumed to be dead
797          * initially, because router checker would update aliveness in this
798          * case, and moreover lp_last_alive at peer creation is assumed.
799          */
800         if (alive && !lp->lp_alive &&
801             !(lnet_isrouter(lp) && lp->lp_alive_count == 0))
802                 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
803
804         return alive;
805 }
806
807
808 /* NB: returns 1 when alive, 0 when dead, negative when error;
809  *     may drop the LNET_LOCK */
810 int
811 lnet_peer_alive_locked (lnet_peer_t *lp)
812 {
813         cfs_time_t now = cfs_time_current();
814
815         /* LU-630: only router checks peer health. */
816         if (the_lnet.ln_routing == 0)
817                 return 1;
818
819         if (!lnet_peer_aliveness_enabled(lp))
820                 return -ENODEV;
821
822         if (lnet_peer_is_alive(lp, now))
823                 return 1;
824
825         /* Peer appears dead, but we should avoid frequent NI queries (at
826          * most once per lnet_queryinterval seconds). */
827         if (lp->lp_last_query != 0) {
828                 static const int lnet_queryinterval = 1;
829
830                 cfs_time_t next_query =
831                            cfs_time_add(lp->lp_last_query,
832                                         cfs_time_seconds(lnet_queryinterval));
833
834                 if (cfs_time_before(now, next_query)) {
835                         if (lp->lp_alive)
836                                 CWARN("Unexpected aliveness of peer %s: "
837                                       "%d < %d (%d/%d)\n",
838                                       libcfs_nid2str(lp->lp_nid),
839                                       (int)now, (int)next_query,
840                                       lnet_queryinterval,
841                                       lp->lp_ni->ni_peertimeout);
842                         return 0;
843                 }
844         }
845
846         /* query NI for latest aliveness news */
847         lnet_ni_peer_alive(lp);
848
849         if (lnet_peer_is_alive(lp, now))
850                 return 1;
851
852         lnet_notify_locked(lp, 0, 0, lp->lp_last_alive);
853         return 0;
854 }
855
856 int
857 lnet_post_send_locked (lnet_msg_t *msg, int do_send)
858 {
859         /* lnet_send is going to LNET_UNLOCK immediately after this, so it sets
860          * do_send FALSE and I don't do the unlock/send/lock bit.  I return
861          * EAGAIN if msg blocked, EHOSTUNREACH if msg_txpeer appears dead, and
862          * 0 if sent or OK to send */
863         lnet_peer_t *lp = msg->msg_txpeer;
864         lnet_ni_t   *ni = lp->lp_ni;
865
866         /* non-lnet_send() callers have checked before */
867         LASSERT(!do_send || msg->msg_tx_delayed);
868         LASSERT(!msg->msg_receiving);
869
870         /* NB 'lp' is always the next hop */
871         if ((msg->msg_target.pid & LNET_PID_USERFLAG) == 0 &&
872             lnet_peer_alive_locked(lp) == 0) {
873                 the_lnet.ln_counters.drop_count++;
874                 the_lnet.ln_counters.drop_length += msg->msg_len;
875                 LNET_UNLOCK();
876
877                 CNETERR("Dropping message for %s: peer not alive\n",
878                         libcfs_id2str(msg->msg_target));
879                 if (do_send)
880                         lnet_finalize(ni, msg, -EHOSTUNREACH);
881
882                 LNET_LOCK();
883                 return EHOSTUNREACH;
884         }
885
886         if (!msg->msg_peertxcredit) {
887                 LASSERT ((lp->lp_txcredits < 0) ==
888                          !cfs_list_empty(&lp->lp_txq));
889
890                 msg->msg_peertxcredit = 1;
891                 lp->lp_txqnob += msg->msg_len + sizeof(lnet_hdr_t);
892                 lp->lp_txcredits--;
893
894                 if (lp->lp_txcredits < lp->lp_mintxcredits)
895                         lp->lp_mintxcredits = lp->lp_txcredits;
896
897                 if (lp->lp_txcredits < 0) {
898                         msg->msg_tx_delayed = 1;
899                         cfs_list_add_tail(&msg->msg_list, &lp->lp_txq);
900                         return EAGAIN;
901                 }
902         }
903
904         if (!msg->msg_txcredit) {
905                 LASSERT ((ni->ni_txcredits < 0) ==
906                          !cfs_list_empty(&ni->ni_txq));
907
908                 msg->msg_txcredit = 1;
909                 ni->ni_txcredits--;
910
911                 if (ni->ni_txcredits < ni->ni_mintxcredits)
912                         ni->ni_mintxcredits = ni->ni_txcredits;
913
914                 if (ni->ni_txcredits < 0) {
915                         msg->msg_tx_delayed = 1;
916                         cfs_list_add_tail(&msg->msg_list, &ni->ni_txq);
917                         return EAGAIN;
918                 }
919         }
920
921         if (do_send) {
922                 LNET_UNLOCK();
923                 lnet_ni_send(ni, msg);
924                 LNET_LOCK();
925         }
926         return 0;
927 }
928
929 #ifdef __KERNEL__
930
931 lnet_rtrbufpool_t *
932 lnet_msg2bufpool(lnet_msg_t *msg)
933 {
934         lnet_rtrbufpool_t *rbp = &the_lnet.ln_rtrpools[0];
935
936         LASSERT (msg->msg_len <= LNET_MTU);
937         while (msg->msg_len > (unsigned int)rbp->rbp_npages * CFS_PAGE_SIZE) {
938                 rbp++;
939                 LASSERT (rbp < &the_lnet.ln_rtrpools[LNET_NRBPOOLS]);
940         }
941
942         return rbp;
943 }
944
945 int
946 lnet_post_routed_recv_locked (lnet_msg_t *msg, int do_recv)
947 {
948         /* lnet_parse is going to LNET_UNLOCK immediately after this, so it
949          * sets do_recv FALSE and I don't do the unlock/send/lock bit.  I
950          * return EAGAIN if msg blocked and 0 if received or OK to receive */
951         lnet_peer_t         *lp = msg->msg_rxpeer;
952         lnet_rtrbufpool_t   *rbp;
953         lnet_rtrbuf_t       *rb;
954
955         LASSERT (msg->msg_iov == NULL);
956         LASSERT (msg->msg_kiov == NULL);
957         LASSERT (msg->msg_niov == 0);
958         LASSERT (msg->msg_routing);
959         LASSERT (msg->msg_receiving);
960         LASSERT (!msg->msg_sending);
961
962         /* non-lnet_parse callers only receive delayed messages */
963         LASSERT(!do_recv || msg->msg_rx_delayed);
964
965         if (!msg->msg_peerrtrcredit) {
966                 LASSERT ((lp->lp_rtrcredits < 0) ==
967                          !cfs_list_empty(&lp->lp_rtrq));
968
969                 msg->msg_peerrtrcredit = 1;
970                 lp->lp_rtrcredits--;
971                 if (lp->lp_rtrcredits < lp->lp_minrtrcredits)
972                         lp->lp_minrtrcredits = lp->lp_rtrcredits;
973
974                 if (lp->lp_rtrcredits < 0) {
975                         /* must have checked eager_recv before here */
976                         LASSERT(msg->msg_rx_ready_delay);
977                         msg->msg_rx_delayed = 1;
978                         cfs_list_add_tail(&msg->msg_list, &lp->lp_rtrq);
979                         return EAGAIN;
980                 }
981         }
982
983         rbp = lnet_msg2bufpool(msg);
984
985         if (!msg->msg_rtrcredit) {
986                 LASSERT ((rbp->rbp_credits < 0) ==
987                          !cfs_list_empty(&rbp->rbp_msgs));
988
989                 msg->msg_rtrcredit = 1;
990                 rbp->rbp_credits--;
991                 if (rbp->rbp_credits < rbp->rbp_mincredits)
992                         rbp->rbp_mincredits = rbp->rbp_credits;
993
994                 if (rbp->rbp_credits < 0) {
995                         /* must have checked eager_recv before here */
996                         LASSERT(msg->msg_rx_ready_delay);
997                         msg->msg_rx_delayed = 1;
998                         cfs_list_add_tail(&msg->msg_list, &rbp->rbp_msgs);
999                         return EAGAIN;
1000                 }
1001         }
1002
1003         LASSERT (!cfs_list_empty(&rbp->rbp_bufs));
1004         rb = cfs_list_entry(rbp->rbp_bufs.next, lnet_rtrbuf_t, rb_list);
1005         cfs_list_del(&rb->rb_list);
1006
1007         msg->msg_niov = rbp->rbp_npages;
1008         msg->msg_kiov = &rb->rb_kiov[0];
1009
1010         if (do_recv) {
1011                 LNET_UNLOCK();
1012                 lnet_ni_recv(lp->lp_ni, msg->msg_private, msg, 1,
1013                              0, msg->msg_len, msg->msg_len);
1014                 LNET_LOCK();
1015         }
1016         return 0;
1017 }
1018 #endif
1019
1020 void
1021 lnet_return_tx_credits_locked(lnet_msg_t *msg)
1022 {
1023         lnet_peer_t     *txpeer = msg->msg_txpeer;
1024         lnet_msg_t      *msg2;
1025         lnet_ni_t       *ni;
1026
1027         if (msg->msg_txcredit) {
1028                 /* give back NI txcredits */
1029                 msg->msg_txcredit = 0;
1030                 ni = txpeer->lp_ni;
1031
1032                 LASSERT((ni->ni_txcredits < 0) == !cfs_list_empty(&ni->ni_txq));
1033
1034                 ni->ni_txcredits++;
1035                 if (ni->ni_txcredits <= 0) {
1036                         msg2 = cfs_list_entry(ni->ni_txq.next, lnet_msg_t,
1037                                               msg_list);
1038                         cfs_list_del(&msg2->msg_list);
1039
1040                         LASSERT(msg2->msg_txpeer->lp_ni == ni);
1041                         LASSERT(msg2->msg_tx_delayed);
1042
1043                         (void) lnet_post_send_locked(msg2, 1);
1044                 }
1045         }
1046
1047         if (msg->msg_peertxcredit) {
1048                 /* give back peer txcredits */
1049                 msg->msg_peertxcredit = 0;
1050
1051                 LASSERT((txpeer->lp_txcredits < 0) ==
1052                         !cfs_list_empty(&txpeer->lp_txq));
1053
1054                 txpeer->lp_txqnob -= msg->msg_len + sizeof(lnet_hdr_t);
1055                 LASSERT (txpeer->lp_txqnob >= 0);
1056
1057                 txpeer->lp_txcredits++;
1058                 if (txpeer->lp_txcredits <= 0) {
1059                         msg2 = cfs_list_entry(txpeer->lp_txq.next,
1060                                               lnet_msg_t, msg_list);
1061                         cfs_list_del(&msg2->msg_list);
1062
1063                         LASSERT(msg2->msg_txpeer == txpeer);
1064                         LASSERT(msg2->msg_tx_delayed);
1065
1066                         (void) lnet_post_send_locked(msg2, 1);
1067                 }
1068         }
1069
1070         if (txpeer != NULL) {
1071                 msg->msg_txpeer = NULL;
1072                 lnet_peer_decref_locked(txpeer);
1073         }
1074 }
1075
1076 void
1077 lnet_return_rx_credits_locked(lnet_msg_t *msg)
1078 {
1079         lnet_peer_t     *rxpeer = msg->msg_rxpeer;
1080 #ifdef __KERNEL__
1081         lnet_msg_t      *msg2;
1082
1083         if (msg->msg_rtrcredit) {
1084                 /* give back global router credits */
1085                 lnet_rtrbuf_t     *rb;
1086                 lnet_rtrbufpool_t *rbp;
1087
1088                 /* NB If a msg ever blocks for a buffer in rbp_msgs, it stays
1089                  * there until it gets one allocated, or aborts the wait
1090                  * itself */
1091                 LASSERT (msg->msg_kiov != NULL);
1092
1093                 rb = cfs_list_entry(msg->msg_kiov, lnet_rtrbuf_t, rb_kiov[0]);
1094                 rbp = rb->rb_pool;
1095                 LASSERT (rbp == lnet_msg2bufpool(msg));
1096
1097                 msg->msg_kiov = NULL;
1098                 msg->msg_rtrcredit = 0;
1099
1100                 LASSERT((rbp->rbp_credits < 0) ==
1101                         !cfs_list_empty(&rbp->rbp_msgs));
1102                 LASSERT((rbp->rbp_credits > 0) ==
1103                         !cfs_list_empty(&rbp->rbp_bufs));
1104
1105                 cfs_list_add(&rb->rb_list, &rbp->rbp_bufs);
1106                 rbp->rbp_credits++;
1107                 if (rbp->rbp_credits <= 0) {
1108                         msg2 = cfs_list_entry(rbp->rbp_msgs.next,
1109                                               lnet_msg_t, msg_list);
1110                         cfs_list_del(&msg2->msg_list);
1111
1112                         (void) lnet_post_routed_recv_locked(msg2, 1);
1113                 }
1114         }
1115
1116         if (msg->msg_peerrtrcredit) {
1117                 /* give back peer router credits */
1118                 msg->msg_peerrtrcredit = 0;
1119
1120                 LASSERT((rxpeer->lp_rtrcredits < 0) ==
1121                         !cfs_list_empty(&rxpeer->lp_rtrq));
1122
1123                 rxpeer->lp_rtrcredits++;
1124                 if (rxpeer->lp_rtrcredits <= 0) {
1125                         msg2 = cfs_list_entry(rxpeer->lp_rtrq.next,
1126                                               lnet_msg_t, msg_list);
1127                         cfs_list_del(&msg2->msg_list);
1128
1129                         (void) lnet_post_routed_recv_locked(msg2, 1);
1130                 }
1131         }
1132 #else
1133         LASSERT (!msg->msg_rtrcredit);
1134         LASSERT (!msg->msg_peerrtrcredit);
1135 #endif
1136         if (rxpeer != NULL) {
1137                 msg->msg_rxpeer = NULL;
1138                 lnet_peer_decref_locked(rxpeer);
1139         }
1140 }
1141
1142 static lnet_peer_t *
1143 lnet_find_route_locked(lnet_ni_t *ni, lnet_nid_t target)
1144 {
1145         lnet_remotenet_t        *rnet;
1146         lnet_route_t            *rtr;
1147         lnet_route_t            *rtr_best;
1148         lnet_route_t            *rtr_last;
1149         struct lnet_peer        *lp_best;
1150         struct lnet_peer        *lp;
1151         int                     rc;
1152
1153         rnet = lnet_find_net_locked(LNET_NIDNET(target));
1154         if (rnet == NULL)
1155                 return NULL;
1156
1157         lp_best = NULL;
1158         rtr_best = rtr_last = NULL;
1159         cfs_list_for_each_entry(rtr, &rnet->lrn_routes, lr_list) {
1160                 lp = rtr->lr_gateway;
1161
1162                 if (!lp->lp_alive || /* gateway is down */
1163                     (lp->lp_ping_version == LNET_PROTO_PING_VERSION &&
1164                      rtr->lr_downis != 0)) /* NI to target is down */
1165                         continue;
1166
1167                 if (ni != NULL && lp->lp_ni != ni)
1168                         continue;
1169
1170                 if (lp_best == NULL) {
1171                         rtr_best = rtr_last = rtr;
1172                         lp_best = lp;
1173                         continue;
1174                 }
1175
1176                 rc = lnet_compare_routes(rtr, rtr_best);
1177                 if (rc < 0)
1178                         continue;
1179
1180                 rtr_best = rtr;
1181                 lp_best = lp;
1182         }
1183
1184         if (rtr_best != NULL) {
1185                 /* Place selected route at the end of the route list to ensure
1186                  * fairness; everything else being equal... */
1187                 cfs_list_del(&rtr_best->lr_list);
1188                 cfs_list_add_tail(&rtr_best->lr_list, &rnet->lrn_routes);
1189         }
1190
1191         return lp_best;
1192 }
1193
1194 int
1195 lnet_send(lnet_nid_t src_nid, lnet_msg_t *msg)
1196 {
1197         lnet_nid_t        dst_nid = msg->msg_target.nid;
1198         lnet_ni_t        *src_ni;
1199         lnet_ni_t        *local_ni;
1200         lnet_peer_t      *lp;
1201         int               rc;
1202
1203         LASSERT (msg->msg_txpeer == NULL);
1204         LASSERT (!msg->msg_sending);
1205         LASSERT (!msg->msg_target_is_router);
1206         LASSERT (!msg->msg_receiving);
1207
1208         msg->msg_sending = 1;
1209
1210         /* NB! ni != NULL == interface pre-determined (ACK/REPLY) */
1211
1212         LNET_LOCK();
1213
1214         if (the_lnet.ln_shutdown) {
1215                 LNET_UNLOCK();
1216                 return -ESHUTDOWN;
1217         }
1218
1219         if (src_nid == LNET_NID_ANY) {
1220                 src_ni = NULL;
1221         } else {
1222                 src_ni = lnet_nid2ni_locked(src_nid);
1223                 if (src_ni == NULL) {
1224                         LNET_UNLOCK();
1225                         LCONSOLE_WARN("Can't send to %s: src %s is not a "
1226                                       "local nid\n", libcfs_nid2str(dst_nid),
1227                                       libcfs_nid2str(src_nid));
1228                         return -EINVAL;
1229                 }
1230                 LASSERT (!msg->msg_routing);
1231         }
1232
1233         lnet_msg_commit(msg, 1);
1234         /* Is this for someone on a local network? */
1235         local_ni = lnet_net2ni_locked(LNET_NIDNET(dst_nid));
1236
1237         if (local_ni != NULL) {
1238                 if (src_ni == NULL) {
1239                         src_ni = local_ni;
1240                         src_nid = src_ni->ni_nid;
1241                 } else if (src_ni == local_ni) {
1242                         lnet_ni_decref_locked(local_ni);
1243                 } else {
1244                         lnet_ni_decref_locked(local_ni);
1245                         lnet_ni_decref_locked(src_ni);
1246                         LNET_UNLOCK();
1247                         LCONSOLE_WARN("No route to %s via from %s\n",
1248                                       libcfs_nid2str(dst_nid),
1249                                       libcfs_nid2str(src_nid));
1250                         return -EINVAL;
1251                 }
1252
1253                 LASSERT (src_nid != LNET_NID_ANY);
1254
1255                 if (!msg->msg_routing)
1256                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1257
1258                 if (src_ni == the_lnet.ln_loni) {
1259                         /* No send credit hassles with LOLND */
1260                         LNET_UNLOCK();
1261                         lnet_ni_send(src_ni, msg);
1262                         lnet_ni_decref(src_ni);
1263                         return 0;
1264                 }
1265
1266                 rc = lnet_nid2peer_locked(&lp, dst_nid);
1267                 lnet_ni_decref_locked(src_ni);  /* lp has ref on src_ni; lose mine */
1268                 if (rc != 0) {
1269                         LNET_UNLOCK();
1270                         LCONSOLE_WARN("Error %d finding peer %s\n", rc,
1271                                       libcfs_nid2str(dst_nid));
1272                         /* ENOMEM or shutting down */
1273                         return rc;
1274                 }
1275                 LASSERT (lp->lp_ni == src_ni);
1276         } else {
1277 #ifndef __KERNEL__
1278                 LNET_UNLOCK();
1279
1280                 /* NB
1281                  * - once application finishes computation, check here to update
1282                  *   router states before it waits for pending IO in LNetEQPoll
1283                  * - recursion breaker: router checker sends no message
1284                  *   to remote networks */
1285                 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING)
1286                         lnet_router_checker();
1287
1288                 LNET_LOCK();
1289 #endif
1290                 /* sending to a remote network */
1291                 lp = lnet_find_route_locked(src_ni, dst_nid);
1292                 if (lp == NULL) {
1293                         if (src_ni != NULL)
1294                                 lnet_ni_decref_locked(src_ni);
1295                         LNET_UNLOCK();
1296
1297                         LCONSOLE_WARN("No route to %s via %s "
1298                                       "(all routers down)\n",
1299                                       libcfs_id2str(msg->msg_target),
1300                                       libcfs_nid2str(src_nid));
1301                         return -EHOSTUNREACH;
1302                 }
1303
1304                 CDEBUG(D_NET, "Best route to %s via %s for %s %d\n",
1305                        libcfs_nid2str(dst_nid), libcfs_nid2str(lp->lp_nid),
1306                        lnet_msgtyp2str(msg->msg_type), msg->msg_len);
1307
1308                 if (src_ni == NULL) {
1309                         src_ni = lp->lp_ni;
1310                         src_nid = src_ni->ni_nid;
1311                 } else {
1312                         LASSERT (src_ni == lp->lp_ni);
1313                         lnet_ni_decref_locked(src_ni);
1314                 }
1315
1316                 lnet_peer_addref_locked(lp);
1317
1318                 LASSERT (src_nid != LNET_NID_ANY);
1319
1320                 if (!msg->msg_routing) {
1321                         /* I'm the source and now I know which NI to send on */
1322                         msg->msg_hdr.src_nid = cpu_to_le64(src_nid);
1323                 }
1324
1325                 msg->msg_target_is_router = 1;
1326                 msg->msg_target.nid = lp->lp_nid;
1327                 msg->msg_target.pid = LUSTRE_SRV_LNET_PID;
1328         }
1329
1330         /* 'lp' is our best choice of peer */
1331
1332         LASSERT (!msg->msg_peertxcredit);
1333         LASSERT (!msg->msg_txcredit);
1334         LASSERT (msg->msg_txpeer == NULL);
1335
1336         msg->msg_txpeer = lp;                   /* msg takes my ref on lp */
1337
1338         rc = lnet_post_send_locked(msg, 0);
1339         LNET_UNLOCK();
1340
1341         if (rc == EHOSTUNREACH)
1342                 return -EHOSTUNREACH;
1343
1344         if (rc == 0)
1345                 lnet_ni_send(src_ni, msg);
1346
1347         return 0;
1348 }
1349
1350 static void
1351 lnet_drop_message (lnet_ni_t *ni, void *private, unsigned int nob)
1352 {
1353         LNET_LOCK();
1354         the_lnet.ln_counters.drop_count++;
1355         the_lnet.ln_counters.drop_length += nob;
1356         LNET_UNLOCK();
1357
1358         lnet_ni_recv(ni, private, NULL, 0, 0, 0, nob);
1359 }
1360
1361 static void
1362 lnet_recv_put(lnet_ni_t *ni, lnet_msg_t *msg)
1363 {
1364         lnet_hdr_t      *hdr = &msg->msg_hdr;
1365
1366         if (msg->msg_wanted != 0)
1367                 lnet_setpayloadbuffer(msg);
1368
1369         lnet_build_msg_event(msg, LNET_EVENT_PUT);
1370
1371         /* Must I ACK?  If so I'll grab the ack_wmd out of the header and put
1372          * it back into the ACK during lnet_finalize() */
1373         msg->msg_ack = (!lnet_is_wire_handle_none(&hdr->msg.put.ack_wmd) &&
1374                         (msg->msg_md->md_options & LNET_MD_ACK_DISABLE) == 0);
1375
1376         lnet_ni_recv(ni, msg->msg_private, msg, msg->msg_rx_delayed,
1377                      msg->msg_offset, msg->msg_wanted, hdr->payload_length);
1378 }
1379
1380 static int
1381 lnet_parse_put(lnet_ni_t *ni, lnet_msg_t *msg)
1382 {
1383         int               rc;
1384         int               index;
1385         lnet_hdr_t       *hdr = &msg->msg_hdr;
1386         unsigned int      rlength = hdr->payload_length;
1387         lnet_process_id_t src= {0};
1388
1389         src.nid = hdr->src_nid;
1390         src.pid = hdr->src_pid;
1391
1392         /* Convert put fields to host byte order */
1393         hdr->msg.put.match_bits = le64_to_cpu(hdr->msg.put.match_bits);
1394         hdr->msg.put.ptl_index = le32_to_cpu(hdr->msg.put.ptl_index);
1395         hdr->msg.put.offset = le32_to_cpu(hdr->msg.put.offset);
1396
1397         index = hdr->msg.put.ptl_index;
1398
1399         msg->msg_rx_ready_delay = ni->ni_lnd->lnd_eager_recv == NULL;
1400
1401  again:
1402         rc = lnet_ptl_match_md(index, LNET_MD_OP_PUT, src,
1403                                rlength, hdr->msg.put.offset,
1404                                hdr->msg.put.match_bits, msg);
1405         switch (rc) {
1406         default:
1407                 LBUG();
1408
1409         case LNET_MATCHMD_OK:
1410                 lnet_recv_put(ni, msg);
1411                 return 0;
1412
1413         case LNET_MATCHMD_NONE:
1414                 if (msg->msg_rx_delayed) /* attached on delayed list */
1415                         return 0;
1416
1417                 rc = lnet_ni_eager_recv(ni, msg);
1418                 if (rc == 0)
1419                         goto again;
1420                 /* fall through */
1421
1422         case LNET_MATCHMD_DROP:
1423                 CNETERR("Dropping PUT from %s portal %d match "LPU64
1424                         " offset %d length %d: %d\n",
1425                         libcfs_id2str(src), index,
1426                         hdr->msg.put.match_bits,
1427                         hdr->msg.put.offset, rlength, rc);
1428
1429                 return ENOENT;          /* +ve: OK but no match */
1430         }
1431 }
1432
1433 static int
1434 lnet_parse_get(lnet_ni_t *ni, lnet_msg_t *msg, int rdma_get)
1435 {
1436         lnet_hdr_t        *hdr = &msg->msg_hdr;
1437         lnet_process_id_t  src = {0};
1438         lnet_handle_wire_t reply_wmd;
1439         int                rc;
1440
1441         src.nid = hdr->src_nid;
1442         src.pid = hdr->src_pid;
1443
1444         /* Convert get fields to host byte order */
1445         hdr->msg.get.match_bits = le64_to_cpu(hdr->msg.get.match_bits);
1446         hdr->msg.get.ptl_index = le32_to_cpu(hdr->msg.get.ptl_index);
1447         hdr->msg.get.sink_length = le32_to_cpu(hdr->msg.get.sink_length);
1448         hdr->msg.get.src_offset = le32_to_cpu(hdr->msg.get.src_offset);
1449
1450         rc = lnet_ptl_match_md(hdr->msg.get.ptl_index, LNET_MD_OP_GET, src,
1451                                hdr->msg.get.sink_length,
1452                                hdr->msg.get.src_offset,
1453                                hdr->msg.get.match_bits, msg);
1454         if (rc == LNET_MATCHMD_DROP) {
1455                 CNETERR("Dropping GET from %s portal %d match "LPU64
1456                         " offset %d length %d\n",
1457                         libcfs_id2str(src),
1458                         hdr->msg.get.ptl_index,
1459                         hdr->msg.get.match_bits,
1460                         hdr->msg.get.src_offset,
1461                         hdr->msg.get.sink_length);
1462                 return ENOENT;                  /* +ve: OK but no match */
1463         }
1464
1465         LASSERT (rc == LNET_MATCHMD_OK);
1466
1467         lnet_build_msg_event(msg, LNET_EVENT_GET);
1468
1469         reply_wmd = hdr->msg.get.return_wmd;
1470
1471         lnet_prep_send(msg, LNET_MSG_REPLY, src,
1472                        msg->msg_offset, msg->msg_wanted);
1473
1474         msg->msg_hdr.msg.reply.dst_wmd = reply_wmd;
1475
1476         if (rdma_get) {
1477                 /* The LND completes the REPLY from her recv procedure */
1478                 lnet_ni_recv(ni, msg->msg_private, msg, 0,
1479                              msg->msg_offset, msg->msg_len, msg->msg_len);
1480                 return 0;
1481         }
1482
1483         lnet_ni_recv(ni, msg->msg_private, NULL, 0, 0, 0, 0);
1484         msg->msg_receiving = 0;
1485
1486         rc = lnet_send(ni->ni_nid, msg);
1487         if (rc < 0) {
1488                 /* didn't get as far as lnet_ni_send() */
1489                 CERROR("%s: Unable to send REPLY for GET from %s: %d\n",
1490                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), rc);
1491
1492                 lnet_finalize(ni, msg, rc);
1493         }
1494
1495         return 0;
1496 }
1497
1498 static int
1499 lnet_parse_reply(lnet_ni_t *ni, lnet_msg_t *msg)
1500 {
1501         void             *private = msg->msg_private;
1502         lnet_hdr_t       *hdr = &msg->msg_hdr;
1503         lnet_process_id_t src = {0};
1504         lnet_libmd_t     *md;
1505         int               rlength;
1506         int               mlength;
1507
1508         LNET_LOCK();
1509
1510         src.nid = hdr->src_nid;
1511         src.pid = hdr->src_pid;
1512
1513         /* NB handles only looked up by creator (no flips) */
1514         md = lnet_wire_handle2md(&hdr->msg.reply.dst_wmd);
1515         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1516                 CNETERR("%s: Dropping REPLY from %s for %s "
1517                         "MD "LPX64"."LPX64"\n",
1518                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1519                         (md == NULL) ? "invalid" : "inactive",
1520                         hdr->msg.reply.dst_wmd.wh_interface_cookie,
1521                         hdr->msg.reply.dst_wmd.wh_object_cookie);
1522                 if (md != NULL && md->md_me != NULL)
1523                         CERROR("REPLY MD also attached to portal %d\n",
1524                                md->md_me->me_portal);
1525
1526                 LNET_UNLOCK();
1527                 return ENOENT;                  /* +ve: OK but no match */
1528         }
1529
1530         LASSERT (md->md_offset == 0);
1531
1532         rlength = hdr->payload_length;
1533         mlength = MIN(rlength, (int)md->md_length);
1534
1535         if (mlength < rlength &&
1536             (md->md_options & LNET_MD_TRUNCATE) == 0) {
1537                 CNETERR("%s: Dropping REPLY from %s length %d "
1538                         "for MD "LPX64" would overflow (%d)\n",
1539                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1540                         rlength, hdr->msg.reply.dst_wmd.wh_object_cookie,
1541                         mlength);
1542                 LNET_UNLOCK();
1543                 return ENOENT;          /* +ve: OK but no match */
1544         }
1545
1546         CDEBUG(D_NET, "%s: Reply from %s of length %d/%d into md "LPX64"\n",
1547                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1548                mlength, rlength, hdr->msg.reply.dst_wmd.wh_object_cookie);
1549
1550         lnet_msg_attach_md(msg, md, 0, mlength);
1551
1552         if (mlength != 0)
1553                 lnet_setpayloadbuffer(msg);
1554
1555         LNET_UNLOCK();
1556
1557         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
1558
1559         lnet_ni_recv(ni, private, msg, 0, 0, mlength, rlength);
1560         return 0;
1561 }
1562
1563 static int
1564 lnet_parse_ack(lnet_ni_t *ni, lnet_msg_t *msg)
1565 {
1566         lnet_hdr_t       *hdr = &msg->msg_hdr;
1567         lnet_process_id_t src = {0};
1568         lnet_libmd_t     *md;
1569
1570         src.nid = hdr->src_nid;
1571         src.pid = hdr->src_pid;
1572
1573         /* Convert ack fields to host byte order */
1574         hdr->msg.ack.match_bits = le64_to_cpu(hdr->msg.ack.match_bits);
1575         hdr->msg.ack.mlength = le32_to_cpu(hdr->msg.ack.mlength);
1576
1577         LNET_LOCK();
1578
1579         /* NB handles only looked up by creator (no flips) */
1580         md = lnet_wire_handle2md(&hdr->msg.ack.dst_wmd);
1581         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
1582                 /* Don't moan; this is expected */
1583                 CDEBUG(D_NET,
1584                        "%s: Dropping ACK from %s to %s MD "LPX64"."LPX64"\n",
1585                        libcfs_nid2str(ni->ni_nid), libcfs_id2str(src),
1586                        (md == NULL) ? "invalid" : "inactive",
1587                        hdr->msg.ack.dst_wmd.wh_interface_cookie,
1588                        hdr->msg.ack.dst_wmd.wh_object_cookie);
1589                 if (md != NULL && md->md_me != NULL)
1590                         CERROR("Source MD also attached to portal %d\n",
1591                                md->md_me->me_portal);
1592
1593                 LNET_UNLOCK();
1594                 return ENOENT;                  /* +ve! */
1595         }
1596
1597         CDEBUG(D_NET, "%s: ACK from %s into md "LPX64"\n",
1598                libcfs_nid2str(ni->ni_nid), libcfs_id2str(src), 
1599                hdr->msg.ack.dst_wmd.wh_object_cookie);
1600
1601         lnet_msg_attach_md(msg, md, 0, 0);
1602
1603         LNET_UNLOCK();
1604
1605         lnet_build_msg_event(msg, LNET_EVENT_ACK);
1606
1607         lnet_ni_recv(ni, msg->msg_private, msg, 0, 0, 0, msg->msg_len);
1608         return 0;
1609 }
1610
1611 char *
1612 lnet_msgtyp2str (int type)
1613 {
1614         switch (type) {
1615         case LNET_MSG_ACK:
1616                 return ("ACK");
1617         case LNET_MSG_PUT:
1618                 return ("PUT");
1619         case LNET_MSG_GET:
1620                 return ("GET");
1621         case LNET_MSG_REPLY:
1622                 return ("REPLY");
1623         case LNET_MSG_HELLO:
1624                 return ("HELLO");
1625         default:
1626                 return ("<UNKNOWN>");
1627         }
1628 }
1629
1630 void
1631 lnet_print_hdr(lnet_hdr_t * hdr)
1632 {
1633         lnet_process_id_t src = {0};
1634         lnet_process_id_t dst = {0};
1635         char *type_str = lnet_msgtyp2str (hdr->type);
1636
1637         src.nid = hdr->src_nid;
1638         src.pid = hdr->src_pid;
1639
1640         dst.nid = hdr->dest_nid;
1641         dst.pid = hdr->dest_pid;
1642
1643         CWARN("P3 Header at %p of type %s\n", hdr, type_str);
1644         CWARN("    From %s\n", libcfs_id2str(src));
1645         CWARN("    To   %s\n", libcfs_id2str(dst));
1646
1647         switch (hdr->type) {
1648         default:
1649                 break;
1650
1651         case LNET_MSG_PUT:
1652                 CWARN("    Ptl index %d, ack md "LPX64"."LPX64", "
1653                       "match bits "LPU64"\n",
1654                       hdr->msg.put.ptl_index,
1655                       hdr->msg.put.ack_wmd.wh_interface_cookie,
1656                       hdr->msg.put.ack_wmd.wh_object_cookie,
1657                       hdr->msg.put.match_bits);
1658                 CWARN("    Length %d, offset %d, hdr data "LPX64"\n",
1659                       hdr->payload_length, hdr->msg.put.offset,
1660                       hdr->msg.put.hdr_data);
1661                 break;
1662
1663         case LNET_MSG_GET:
1664                 CWARN("    Ptl index %d, return md "LPX64"."LPX64", "
1665                       "match bits "LPU64"\n", hdr->msg.get.ptl_index,
1666                       hdr->msg.get.return_wmd.wh_interface_cookie,
1667                       hdr->msg.get.return_wmd.wh_object_cookie,
1668                       hdr->msg.get.match_bits);
1669                 CWARN("    Length %d, src offset %d\n",
1670                       hdr->msg.get.sink_length,
1671                       hdr->msg.get.src_offset);
1672                 break;
1673
1674         case LNET_MSG_ACK:
1675                 CWARN("    dst md "LPX64"."LPX64", "
1676                       "manipulated length %d\n",
1677                       hdr->msg.ack.dst_wmd.wh_interface_cookie,
1678                       hdr->msg.ack.dst_wmd.wh_object_cookie,
1679                       hdr->msg.ack.mlength);
1680                 break;
1681
1682         case LNET_MSG_REPLY:
1683                 CWARN("    dst md "LPX64"."LPX64", "
1684                       "length %d\n",
1685                       hdr->msg.reply.dst_wmd.wh_interface_cookie,
1686                       hdr->msg.reply.dst_wmd.wh_object_cookie,
1687                       hdr->payload_length);
1688         }
1689
1690 }
1691
1692 int
1693 lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr, lnet_nid_t from_nid, 
1694            void *private, int rdma_req)
1695 {
1696         int            rc = 0;
1697         int            for_me;
1698         lnet_msg_t    *msg;
1699         lnet_pid_t     dest_pid;
1700         lnet_nid_t     dest_nid;
1701         lnet_nid_t     src_nid;
1702         __u32          payload_length;
1703         __u32          type;
1704
1705         LASSERT (!cfs_in_interrupt ());
1706
1707         type = le32_to_cpu(hdr->type);
1708         src_nid = le64_to_cpu(hdr->src_nid);
1709         dest_nid = le64_to_cpu(hdr->dest_nid);
1710         dest_pid = le32_to_cpu(hdr->dest_pid);
1711         payload_length = le32_to_cpu(hdr->payload_length);
1712
1713         for_me = (ni->ni_nid == dest_nid);
1714
1715         switch (type) {
1716         case LNET_MSG_ACK:
1717         case LNET_MSG_GET:
1718                 if (payload_length > 0) {
1719                         CERROR("%s, src %s: bad %s payload %d (0 expected)\n",
1720                                libcfs_nid2str(from_nid),
1721                                libcfs_nid2str(src_nid),
1722                                lnet_msgtyp2str(type), payload_length);
1723                         return -EPROTO;
1724                 }
1725                 break;
1726
1727         case LNET_MSG_PUT:
1728         case LNET_MSG_REPLY:
1729                 if (payload_length > (__u32)(for_me ? LNET_MAX_PAYLOAD : LNET_MTU)) {
1730                         CERROR("%s, src %s: bad %s payload %d "
1731                                "(%d max expected)\n",
1732                                libcfs_nid2str(from_nid),
1733                                libcfs_nid2str(src_nid),
1734                                lnet_msgtyp2str(type),
1735                                payload_length,
1736                                for_me ? LNET_MAX_PAYLOAD : LNET_MTU);
1737                         return -EPROTO;
1738                 }
1739                 break;
1740
1741         default:
1742                 CERROR("%s, src %s: Bad message type 0x%x\n",
1743                        libcfs_nid2str(from_nid),
1744                        libcfs_nid2str(src_nid), type);
1745                 return -EPROTO;
1746         }
1747
1748         if (the_lnet.ln_routing &&
1749             ni->ni_last_alive != cfs_time_current_sec()) {
1750                 LNET_LOCK();
1751
1752                 /* NB: so far here is the only place to set NI status to "up */
1753                 ni->ni_last_alive = cfs_time_current_sec();
1754                 if (ni->ni_status != NULL &&
1755                     ni->ni_status->ns_status == LNET_NI_STATUS_DOWN)
1756                         ni->ni_status->ns_status = LNET_NI_STATUS_UP;
1757                 LNET_UNLOCK();
1758         }
1759
1760         /* Regard a bad destination NID as a protocol error.  Senders should
1761          * know what they're doing; if they don't they're misconfigured, buggy
1762          * or malicious so we chop them off at the knees :) */
1763
1764         if (!for_me) {
1765                 if (LNET_NIDNET(dest_nid) == LNET_NIDNET(ni->ni_nid)) {
1766                         /* should have gone direct */
1767                         CERROR ("%s, src %s: Bad dest nid %s "
1768                                 "(should have been sent direct)\n",
1769                                 libcfs_nid2str(from_nid),
1770                                 libcfs_nid2str(src_nid),
1771                                 libcfs_nid2str(dest_nid));
1772                         return -EPROTO;
1773                 }
1774
1775                 if (lnet_islocalnid(dest_nid)) {
1776                         /* dest is another local NI; sender should have used
1777                          * this node's NID on its own network */
1778                         CERROR ("%s, src %s: Bad dest nid %s "
1779                                 "(it's my nid but on a different network)\n",
1780                                 libcfs_nid2str(from_nid),
1781                                 libcfs_nid2str(src_nid),
1782                                 libcfs_nid2str(dest_nid));
1783                         return -EPROTO;
1784                 }
1785
1786                 if (rdma_req && type == LNET_MSG_GET) {
1787                         CERROR ("%s, src %s: Bad optimized GET for %s "
1788                                 "(final destination must be me)\n",
1789                                 libcfs_nid2str(from_nid),
1790                                 libcfs_nid2str(src_nid),
1791                                 libcfs_nid2str(dest_nid));
1792                         return -EPROTO;
1793                 }
1794
1795                 if (!the_lnet.ln_routing) {
1796                         CERROR ("%s, src %s: Dropping message for %s "
1797                                 "(routing not enabled)\n",
1798                                 libcfs_nid2str(from_nid),
1799                                 libcfs_nid2str(src_nid),
1800                                 libcfs_nid2str(dest_nid));
1801                         goto drop;
1802                 }
1803         }
1804
1805         /* Message looks OK; we're not going to return an error, so we MUST
1806          * call back lnd_recv() come what may... */
1807
1808         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
1809             fail_peer (src_nid, 0))             /* shall we now? */
1810         {
1811                 CERROR("%s, src %s: Dropping %s to simulate failure\n",
1812                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1813                        lnet_msgtyp2str(type));
1814                 goto drop;
1815         }
1816
1817         msg = lnet_msg_alloc();
1818         if (msg == NULL) {
1819                 CERROR("%s, src %s: Dropping %s (out of memory)\n",
1820                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid), 
1821                        lnet_msgtyp2str(type));
1822                 goto drop;
1823         }
1824
1825         /* msg zeroed in lnet_msg_alloc; i.e. flags all clear, pointers NULL etc */
1826
1827         msg->msg_type = type;
1828         msg->msg_private = private;
1829         msg->msg_receiving = 1;
1830         msg->msg_len = msg->msg_wanted = payload_length;
1831         msg->msg_offset = 0;
1832         msg->msg_hdr = *hdr;
1833         /* for building message event */
1834         msg->msg_from = from_nid;
1835
1836         LNET_LOCK();
1837         rc = lnet_nid2peer_locked(&msg->msg_rxpeer, from_nid);
1838         if (rc != 0) {
1839                 LNET_UNLOCK();
1840                 CERROR("%s, src %s: Dropping %s "
1841                        "(error %d looking up sender)\n",
1842                        libcfs_nid2str(from_nid), libcfs_nid2str(src_nid),
1843                        lnet_msgtyp2str(type), rc);
1844                 lnet_msg_free(msg);
1845                 goto drop;
1846         }
1847
1848         lnet_msg_commit(msg, 0);
1849         LNET_UNLOCK();
1850
1851 #ifndef __KERNEL__
1852         LASSERT (for_me);
1853 #else
1854         if (!for_me) {
1855                 msg->msg_target.pid = dest_pid;
1856                 msg->msg_target.nid = dest_nid;
1857                 msg->msg_routing = 1;
1858                 msg->msg_offset = 0;
1859
1860                 LNET_LOCK();
1861                 if (msg->msg_rxpeer->lp_rtrcredits <= 0 ||
1862                     lnet_msg2bufpool(msg)->rbp_credits <= 0) {
1863                         rc = lnet_ni_eager_recv_locked(ni, msg);
1864                         if (rc != 0) {
1865                                 LNET_UNLOCK();
1866                                 goto free_drop;
1867                         }
1868                 }
1869                 rc = lnet_post_routed_recv_locked(msg, 0);
1870                 LNET_UNLOCK();
1871
1872                 if (rc == 0)
1873                         lnet_ni_recv(ni, msg->msg_private, msg, 0,
1874                                      0, payload_length, payload_length);
1875                 return 0;
1876         }
1877 #endif
1878         /* convert common msg->hdr fields to host byteorder */
1879         msg->msg_hdr.type = type;
1880         msg->msg_hdr.src_nid = src_nid;
1881         msg->msg_hdr.src_pid = le32_to_cpu(msg->msg_hdr.src_pid);
1882         msg->msg_hdr.dest_nid = dest_nid;
1883         msg->msg_hdr.dest_pid = dest_pid;
1884         msg->msg_hdr.payload_length = payload_length;
1885
1886         switch (type) {
1887         case LNET_MSG_ACK:
1888                 rc = lnet_parse_ack(ni, msg);
1889                 break;
1890         case LNET_MSG_PUT:
1891                 rc = lnet_parse_put(ni, msg);
1892                 break;
1893         case LNET_MSG_GET:
1894                 rc = lnet_parse_get(ni, msg, rdma_req);
1895                 break;
1896         case LNET_MSG_REPLY:
1897                 rc = lnet_parse_reply(ni, msg);
1898                 break;
1899         default:
1900                 LASSERT(0);
1901                 rc = -EPROTO;
1902                 goto free_drop;  /* prevent an unused label if !kernel */
1903         }
1904
1905         if (rc == 0)
1906                 return 0;
1907
1908         LASSERT (rc == ENOENT);
1909
1910  free_drop:
1911         LASSERT(msg->msg_md == NULL);
1912         lnet_finalize(ni, msg, rc);
1913
1914  drop:
1915         lnet_drop_message(ni, private, payload_length);
1916         return 0;
1917 }
1918
1919 void
1920 lnet_drop_delayed_msg_list(cfs_list_t *head, char *reason)
1921 {
1922         while (!cfs_list_empty(head)) {
1923                 lnet_process_id_t       id = {0};
1924                 lnet_msg_t              *msg;
1925
1926                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
1927                 cfs_list_del(&msg->msg_list);
1928
1929                 id.nid = msg->msg_hdr.src_nid;
1930                 id.pid = msg->msg_hdr.src_pid;
1931
1932                 LASSERT(msg->msg_md == NULL);
1933                 LASSERT(msg->msg_rx_delayed);
1934                 LASSERT(msg->msg_rxpeer != NULL);
1935                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1936
1937                 CWARN("Dropping delayed PUT from %s portal %d match "LPU64
1938                       " offset %d length %d: %s\n",
1939                       libcfs_id2str(id),
1940                       msg->msg_hdr.msg.put.ptl_index,
1941                       msg->msg_hdr.msg.put.match_bits,
1942                       msg->msg_hdr.msg.put.offset,
1943                       msg->msg_hdr.payload_length, reason);
1944
1945                 /* NB I can't drop msg's ref on msg_rxpeer until after I've
1946                  * called lnet_drop_message(), so I just hang onto msg as well
1947                  * until that's done */
1948
1949                 lnet_drop_message(msg->msg_rxpeer->lp_ni,
1950                                   msg->msg_private, msg->msg_len);
1951
1952                 LNET_LOCK();
1953                 lnet_peer_decref_locked(msg->msg_rxpeer);
1954                 LNET_UNLOCK();
1955
1956                 lnet_msg_free(msg);
1957         }
1958 }
1959
1960 void
1961 lnet_recv_delayed_msg_list(cfs_list_t *head)
1962 {
1963         while (!cfs_list_empty(head)) {
1964                 lnet_msg_t        *msg;
1965                 lnet_process_id_t  id;
1966
1967                 msg = cfs_list_entry(head->next, lnet_msg_t, msg_list);
1968                 cfs_list_del(&msg->msg_list);
1969
1970                 /* md won't disappear under me, since each msg
1971                  * holds a ref on it */
1972
1973                 id.nid = msg->msg_hdr.src_nid;
1974                 id.pid = msg->msg_hdr.src_pid;
1975
1976                 LASSERT(msg->msg_rx_delayed);
1977                 LASSERT(msg->msg_md != NULL);
1978                 LASSERT(msg->msg_rxpeer != NULL);
1979                 LASSERT(msg->msg_hdr.type == LNET_MSG_PUT);
1980
1981                 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
1982                        "match "LPU64" offset %d length %d.\n",
1983                         libcfs_id2str(id), msg->msg_hdr.msg.put.ptl_index,
1984                         msg->msg_hdr.msg.put.match_bits,
1985                         msg->msg_hdr.msg.put.offset,
1986                         msg->msg_hdr.payload_length);
1987
1988                 lnet_recv_put(msg->msg_rxpeer->lp_ni, msg);
1989         }
1990 }
1991
1992 /**
1993  * Initiate an asynchronous PUT operation.
1994  *
1995  * There are several events associated with a PUT: completion of the send on
1996  * the initiator node (LNET_EVENT_SEND), and when the send completes
1997  * successfully, the receipt of an acknowledgment (LNET_EVENT_ACK) indicating
1998  * that the operation was accepted by the target. The event LNET_EVENT_PUT is
1999  * used at the target node to indicate the completion of incoming data
2000  * delivery.
2001  *
2002  * The local events will be logged in the EQ associated with the MD pointed to
2003  * by \a mdh handle. Using a MD without an associated EQ results in these
2004  * events being discarded. In this case, the caller must have another
2005  * mechanism (e.g., a higher level protocol) for determining when it is safe
2006  * to modify the memory region associated with the MD.
2007  *
2008  * Note that LNet does not guarantee the order of LNET_EVENT_SEND and
2009  * LNET_EVENT_ACK, though intuitively ACK should happen after SEND.
2010  *
2011  * \param self Indicates the NID of a local interface through which to send
2012  * the PUT request. Use LNET_NID_ANY to let LNet choose one by itself.
2013  * \param mdh A handle for the MD that describes the memory to be sent. The MD
2014  * must be "free floating" (See LNetMDBind()).
2015  * \param ack Controls whether an acknowledgment is requested.
2016  * Acknowledgments are only sent when they are requested by the initiating
2017  * process and the target MD enables them.
2018  * \param target A process identifier for the target process.
2019  * \param portal The index in the \a target's portal table.
2020  * \param match_bits The match bits to use for MD selection at the target
2021  * process.
2022  * \param offset The offset into the target MD (only used when the target
2023  * MD has the LNET_MD_MANAGE_REMOTE option set).
2024  * \param hdr_data 64 bits of user data that can be included in the message
2025  * header. This data is written to an event queue entry at the target if an
2026  * EQ is present on the matching MD.
2027  *
2028  * \retval  0      Success, and only in this case events will be generated
2029  * and logged to EQ (if it exists).
2030  * \retval -EIO    Simulated failure.
2031  * \retval -ENOMEM Memory allocation failure.
2032  * \retval -ENOENT Invalid MD object.
2033  *
2034  * \see lnet_event_t::hdr_data and lnet_event_kind_t.
2035  */
2036 int
2037 LNetPut(lnet_nid_t self, lnet_handle_md_t mdh, lnet_ack_req_t ack,
2038         lnet_process_id_t target, unsigned int portal,
2039         __u64 match_bits, unsigned int offset,
2040         __u64 hdr_data)
2041 {
2042         lnet_msg_t       *msg;
2043         lnet_libmd_t     *md;
2044         int               rc;
2045
2046         LASSERT (the_lnet.ln_init);
2047         LASSERT (the_lnet.ln_refcount > 0);
2048
2049         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2050             fail_peer (target.nid, 1))          /* shall we now? */
2051         {
2052                 CERROR("Dropping PUT to %s: simulated failure\n",
2053                        libcfs_id2str(target));
2054                 return -EIO;
2055         }
2056
2057         msg = lnet_msg_alloc();
2058         if (msg == NULL) {
2059                 CERROR("Dropping PUT to %s: ENOMEM on lnet_msg_t\n",
2060                        libcfs_id2str(target));
2061                 return -ENOMEM;
2062         }
2063         msg->msg_vmflush = !!cfs_memory_pressure_get();
2064
2065         LNET_LOCK();
2066
2067         md = lnet_handle2md(&mdh);
2068         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2069                 lnet_msg_free_locked(msg);
2070
2071                 CERROR("Dropping PUT ("LPU64":%d:%s): MD (%d) invalid\n",
2072                        match_bits, portal, libcfs_id2str(target),
2073                        md == NULL ? -1 : md->md_threshold);
2074                 if (md != NULL && md->md_me != NULL)
2075                         CERROR("Source MD also attached to portal %d\n",
2076                                md->md_me->me_portal);
2077
2078                 LNET_UNLOCK();
2079                 return -ENOENT;
2080         }
2081
2082         CDEBUG(D_NET, "LNetPut -> %s\n", libcfs_id2str(target));
2083
2084         lnet_msg_attach_md(msg, md, 0, 0);
2085
2086         lnet_prep_send(msg, LNET_MSG_PUT, target, 0, md->md_length);
2087
2088         msg->msg_hdr.msg.put.match_bits = cpu_to_le64(match_bits);
2089         msg->msg_hdr.msg.put.ptl_index = cpu_to_le32(portal);
2090         msg->msg_hdr.msg.put.offset = cpu_to_le32(offset);
2091         msg->msg_hdr.msg.put.hdr_data = hdr_data;
2092
2093         /* NB handles only looked up by creator (no flips) */
2094         if (ack == LNET_ACK_REQ) {
2095                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2096                         the_lnet.ln_interface_cookie;
2097                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2098                         md->md_lh.lh_cookie;
2099         } else {
2100                 msg->msg_hdr.msg.put.ack_wmd.wh_interface_cookie = 
2101                         LNET_WIRE_HANDLE_COOKIE_NONE;
2102                 msg->msg_hdr.msg.put.ack_wmd.wh_object_cookie = 
2103                         LNET_WIRE_HANDLE_COOKIE_NONE;
2104         }
2105
2106         LNET_UNLOCK();
2107
2108         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2109
2110         rc = lnet_send(self, msg);
2111         if (rc != 0) {
2112                 CNETERR( "Error sending PUT to %s: %d\n",
2113                        libcfs_id2str(target), rc);
2114                 lnet_finalize (NULL, msg, rc);
2115         }
2116
2117         /* completion will be signalled by an event */
2118         return 0;
2119 }
2120
2121 lnet_msg_t *
2122 lnet_create_reply_msg (lnet_ni_t *ni, lnet_msg_t *getmsg)
2123 {
2124         /* The LND can DMA direct to the GET md (i.e. no REPLY msg).  This
2125          * returns a msg for the LND to pass to lnet_finalize() when the sink
2126          * data has been received.
2127          *
2128          * CAVEAT EMPTOR: 'getmsg' is the original GET, which is freed when
2129          * lnet_finalize() is called on it, so the LND must call this first */
2130
2131         lnet_msg_t        *msg = lnet_msg_alloc();
2132         lnet_libmd_t      *getmd = getmsg->msg_md;
2133         lnet_process_id_t  peer_id = getmsg->msg_target;
2134
2135         LASSERT (!getmsg->msg_target_is_router);
2136         LASSERT (!getmsg->msg_routing);
2137
2138         LNET_LOCK();
2139
2140         LASSERT (getmd->md_refcount > 0);
2141
2142         if (msg == NULL) {
2143                 CERROR ("%s: Dropping REPLY from %s: can't allocate msg\n",
2144                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id));
2145                 goto drop;
2146         }
2147
2148         if (getmd->md_threshold == 0) {
2149                 CERROR ("%s: Dropping REPLY from %s for inactive MD %p\n",
2150                         libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), 
2151                         getmd);
2152                 goto drop_msg;
2153         }
2154
2155         LASSERT (getmd->md_offset == 0);
2156
2157         CDEBUG(D_NET, "%s: Reply from %s md %p\n",
2158                libcfs_nid2str(ni->ni_nid), libcfs_id2str(peer_id), getmd);
2159
2160         /* setup information for lnet_build_msg_event */
2161         msg->msg_from = peer_id.nid;
2162         msg->msg_type = LNET_MSG_GET; /* flag this msg as an "optimized" GET */
2163         msg->msg_hdr.src_nid = peer_id.nid;
2164         msg->msg_hdr.payload_length = getmd->md_length;
2165         msg->msg_receiving = 1; /* required by lnet_msg_attach_md */
2166
2167         lnet_msg_attach_md(msg, getmd, getmd->md_offset, getmd->md_length);
2168         lnet_msg_commit(msg, 0);
2169
2170         LNET_UNLOCK();
2171
2172         lnet_build_msg_event(msg, LNET_EVENT_REPLY);
2173
2174         return msg;
2175
2176  drop_msg:
2177         lnet_msg_free_locked(msg);
2178  drop:
2179         the_lnet.ln_counters.drop_count++;
2180         the_lnet.ln_counters.drop_length += getmd->md_length;
2181
2182         LNET_UNLOCK ();
2183
2184         return NULL;
2185 }
2186
2187 void
2188 lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *reply, unsigned int len)
2189 {
2190         /* Set the REPLY length, now the RDMA that elides the REPLY message has
2191          * completed and I know it. */
2192         LASSERT (reply != NULL);
2193         LASSERT (reply->msg_type == LNET_MSG_GET);
2194         LASSERT (reply->msg_ev.type == LNET_EVENT_REPLY);
2195
2196         /* NB I trusted my peer to RDMA.  If she tells me she's written beyond
2197          * the end of my buffer, I might as well be dead. */
2198         LASSERT (len <= reply->msg_ev.mlength);
2199
2200         reply->msg_ev.mlength = len;
2201 }
2202
2203 /**
2204  * Initiate an asynchronous GET operation.
2205  *
2206  * On the initiator node, an LNET_EVENT_SEND is logged when the GET request
2207  * is sent, and an LNET_EVENT_REPLY is logged when the data returned from
2208  * the target node in the REPLY has been written to local MD.
2209  *
2210  * On the target node, an LNET_EVENT_GET is logged when the GET request
2211  * arrives and is accepted into a MD.
2212  *
2213  * \param self,target,portal,match_bits,offset See the discussion in LNetPut().
2214  * \param mdh A handle for the MD that describes the memory into which the
2215  * requested data will be received. The MD must be "free floating" (See LNetMDBind()).
2216  *
2217  * \retval  0      Success, and only in this case events will be generated
2218  * and logged to EQ (if it exists) of the MD.
2219  * \retval -EIO    Simulated failure.
2220  * \retval -ENOMEM Memory allocation failure.
2221  * \retval -ENOENT Invalid MD object.
2222  */
2223 int
2224 LNetGet(lnet_nid_t self, lnet_handle_md_t mdh, 
2225         lnet_process_id_t target, unsigned int portal, 
2226         __u64 match_bits, unsigned int offset)
2227 {
2228         lnet_msg_t       *msg;
2229         lnet_libmd_t     *md;
2230         int               rc;
2231
2232         LASSERT (the_lnet.ln_init);
2233         LASSERT (the_lnet.ln_refcount > 0);
2234
2235         if (!cfs_list_empty (&the_lnet.ln_test_peers) && /* normally we don't */
2236             fail_peer (target.nid, 1))          /* shall we now? */
2237         {
2238                 CERROR("Dropping GET to %s: simulated failure\n",
2239                        libcfs_id2str(target));
2240                 return -EIO;
2241         }
2242
2243         msg = lnet_msg_alloc();
2244         if (msg == NULL) {
2245                 CERROR("Dropping GET to %s: ENOMEM on lnet_msg_t\n",
2246                        libcfs_id2str(target));
2247                 return -ENOMEM;
2248         }
2249
2250         LNET_LOCK();
2251
2252         md = lnet_handle2md(&mdh);
2253         if (md == NULL || md->md_threshold == 0 || md->md_me != NULL) {
2254                 lnet_msg_free_locked(msg);
2255
2256                 CERROR("Dropping GET ("LPU64":%d:%s): MD (%d) invalid\n",
2257                        match_bits, portal, libcfs_id2str(target),
2258                        md == NULL ? -1 : md->md_threshold);
2259                 if (md != NULL && md->md_me != NULL)
2260                         CERROR("REPLY MD also attached to portal %d\n",
2261                                md->md_me->me_portal);
2262
2263                 LNET_UNLOCK();
2264                 return -ENOENT;
2265         }
2266
2267         CDEBUG(D_NET, "LNetGet -> %s\n", libcfs_id2str(target));
2268
2269         lnet_msg_attach_md(msg, md, 0, 0);
2270
2271         lnet_prep_send(msg, LNET_MSG_GET, target, 0, 0);
2272
2273         msg->msg_hdr.msg.get.match_bits = cpu_to_le64(match_bits);
2274         msg->msg_hdr.msg.get.ptl_index = cpu_to_le32(portal);
2275         msg->msg_hdr.msg.get.src_offset = cpu_to_le32(offset);
2276         msg->msg_hdr.msg.get.sink_length = cpu_to_le32(md->md_length);
2277
2278         /* NB handles only looked up by creator (no flips) */
2279         msg->msg_hdr.msg.get.return_wmd.wh_interface_cookie = 
2280                 the_lnet.ln_interface_cookie;
2281         msg->msg_hdr.msg.get.return_wmd.wh_object_cookie = 
2282                 md->md_lh.lh_cookie;
2283
2284         LNET_UNLOCK();
2285
2286         lnet_build_msg_event(msg, LNET_EVENT_SEND);
2287
2288         rc = lnet_send(self, msg);
2289         if (rc < 0) {
2290                 CNETERR( "Error sending GET to %s: %d\n",
2291                        libcfs_id2str(target), rc);
2292                 lnet_finalize (NULL, msg, rc);
2293         }
2294
2295         /* completion will be signalled by an event */
2296         return 0;
2297 }
2298
2299 /**
2300  * Calculate distance to node at \a dstnid.
2301  *
2302  * \param dstnid Target NID.
2303  * \param srcnidp If not NULL, NID of the local interface to reach \a dstnid
2304  * is saved here.
2305  * \param orderp If not NULL, order of the route to reach \a dstnid is saved
2306  * here.
2307  *
2308  * \retval 0 If \a dstnid belongs to a local interface, and reserved option
2309  * local_nid_dist_zero is set, which is the default.
2310  * \retval positives Distance to target NID, i.e. number of hops plus one.
2311  * \retval -EHOSTUNREACH If \a dstnid is not reachable.
2312  */
2313 int
2314 LNetDist (lnet_nid_t dstnid, lnet_nid_t *srcnidp, __u32 *orderp)
2315 {
2316         cfs_list_t       *e;
2317         lnet_ni_t        *ni;
2318         lnet_remotenet_t *rnet;
2319         __u32             dstnet = LNET_NIDNET(dstnid);
2320         int               hops;
2321         __u32             order = 2;
2322
2323         /* if !local_nid_dist_zero, I don't return a distance of 0 ever
2324          * (when lustre sees a distance of 0, it substitutes 0@lo), so I
2325          * keep order 0 free for 0@lo and order 1 free for a local NID
2326          * match */
2327
2328         LASSERT (the_lnet.ln_init);
2329         LASSERT (the_lnet.ln_refcount > 0);
2330
2331         LNET_LOCK();
2332
2333         cfs_list_for_each (e, &the_lnet.ln_nis) {
2334                 ni = cfs_list_entry(e, lnet_ni_t, ni_list);
2335
2336                 if (ni->ni_nid == dstnid) {
2337                         if (srcnidp != NULL)
2338                                 *srcnidp = dstnid;
2339                         if (orderp != NULL) {
2340                                 if (LNET_NETTYP(LNET_NIDNET(dstnid)) == LOLND)
2341                                         *orderp = 0;
2342                                 else
2343                                         *orderp = 1;
2344                         }
2345                         LNET_UNLOCK();
2346
2347                         return local_nid_dist_zero ? 0 : 1;
2348                 }
2349
2350                 if (LNET_NIDNET(ni->ni_nid) == dstnet) {
2351                         if (srcnidp != NULL)
2352                                 *srcnidp = ni->ni_nid;
2353                         if (orderp != NULL)
2354                                 *orderp = order;
2355                         LNET_UNLOCK();
2356                         return 1;
2357                 }
2358
2359                 order++;
2360         }
2361
2362         cfs_list_for_each (e, &the_lnet.ln_remote_nets) {
2363                 rnet = cfs_list_entry(e, lnet_remotenet_t, lrn_list);
2364
2365                 if (rnet->lrn_net == dstnet) {
2366                         lnet_route_t *route;
2367                         lnet_route_t *shortest = NULL;
2368
2369                         LASSERT (!cfs_list_empty(&rnet->lrn_routes));
2370
2371                         cfs_list_for_each_entry(route, &rnet->lrn_routes,
2372                                                 lr_list) {
2373                                 if (shortest == NULL ||
2374                                     route->lr_hops < shortest->lr_hops)
2375                                         shortest = route;
2376                         }
2377
2378                         LASSERT (shortest != NULL);
2379                         hops = shortest->lr_hops;
2380                         if (srcnidp != NULL)
2381                                 *srcnidp = shortest->lr_gateway->lp_ni->ni_nid;
2382                         if (orderp != NULL)
2383                                 *orderp = order;
2384                         LNET_UNLOCK();
2385                         return hops + 1;
2386                 }
2387                 order++;
2388         }
2389
2390         LNET_UNLOCK();
2391         return -EHOSTUNREACH;
2392 }
2393
2394 /**
2395  * Set the number of asynchronous messages expected from a target process.
2396  *
2397  * This function is only meaningful for userspace callers. It's a no-op when
2398  * called from kernel.
2399  *
2400  * Asynchronous messages are those that can come from a target when the
2401  * userspace process is not waiting for IO to complete; e.g., AST callbacks
2402  * from Lustre servers. Specifying the expected number of such messages
2403  * allows them to be eagerly received when user process is not running in
2404  * LNet; otherwise network errors may occur.
2405  *
2406  * \param id Process ID of the target process.
2407  * \param nasync Number of asynchronous messages expected from the target.
2408  *
2409  * \return 0 on success, and an error code otherwise.
2410  */
2411 int
2412 LNetSetAsync(lnet_process_id_t id, int nasync)
2413 {
2414 #ifdef __KERNEL__
2415         return 0;
2416 #else
2417         lnet_ni_t        *ni;
2418         lnet_remotenet_t *rnet;
2419         cfs_list_t       *tmp;
2420         lnet_route_t     *route;
2421         lnet_nid_t       *nids;
2422         int               nnids;
2423         int               maxnids = 256;
2424         int               rc = 0;
2425         int               rc2;
2426
2427         /* Target on a local network? */
2428         ni = lnet_net2ni(LNET_NIDNET(id.nid));
2429         if (ni != NULL) {
2430                 if (ni->ni_lnd->lnd_setasync != NULL) 
2431                         rc = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2432                 lnet_ni_decref(ni);
2433                 return rc;
2434         }
2435
2436         /* Target on a remote network: apply to routers */
2437  again:
2438         LIBCFS_ALLOC(nids, maxnids * sizeof(*nids));
2439         if (nids == NULL)
2440                 return -ENOMEM;
2441         nnids = 0;
2442
2443         /* Snapshot all the router NIDs */
2444         LNET_LOCK();
2445         rnet = lnet_find_net_locked(LNET_NIDNET(id.nid));
2446         if (rnet != NULL) {
2447                 cfs_list_for_each(tmp, &rnet->lrn_routes) {
2448                         if (nnids == maxnids) {
2449                                 LNET_UNLOCK();
2450                                 LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2451                                 maxnids *= 2;
2452                                 goto again;
2453                         }
2454
2455                         route = cfs_list_entry(tmp, lnet_route_t, lr_list);
2456                         nids[nnids++] = route->lr_gateway->lp_nid;
2457                 }
2458         }
2459         LNET_UNLOCK();
2460
2461         /* set async on all the routers */
2462         while (nnids-- > 0) {
2463                 id.pid = LUSTRE_SRV_LNET_PID;
2464                 id.nid = nids[nnids];
2465
2466                 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2467                 if (ni == NULL)
2468                         continue;
2469
2470                 if (ni->ni_lnd->lnd_setasync != NULL) {
2471                         rc2 = (ni->ni_lnd->lnd_setasync)(ni, id, nasync);
2472                         if (rc2 != 0)
2473                                 rc = rc2;
2474                 }
2475                 lnet_ni_decref(ni);
2476         }
2477
2478         LIBCFS_FREE(nids, maxnids * sizeof(*nids));
2479         return rc;
2480 #endif
2481 }