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