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