Whamcloud - gitweb
LU-12199 lnet: Ensure md is detached when msg is not committed
[fs/lustre-release.git] / lnet / lnet / lib-msg.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, 2017, 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-msg.c
33  *
34  * Message decoding, parsing and finalizing routines
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <lnet/lib-lnet.h>
40
41 void
42 lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev)
43 {
44         ENTRY;
45
46         memset(ev, 0, sizeof(*ev));
47
48         ev->status   = 0;
49         ev->unlinked = 1;
50         ev->type     = LNET_EVENT_UNLINK;
51         lnet_md_deconstruct(md, &ev->md);
52         lnet_md2handle(&ev->md_handle, md);
53         EXIT;
54 }
55
56 /*
57  * Don't need any lock, must be called after lnet_commit_md
58  */
59 void
60 lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type)
61 {
62         struct lnet_hdr *hdr = &msg->msg_hdr;
63         struct lnet_event *ev = &msg->msg_ev;
64
65         LASSERT(!msg->msg_routing);
66
67         ev->type = ev_type;
68         ev->msg_type = msg->msg_type;
69
70         if (ev_type == LNET_EVENT_SEND) {
71                 /* event for active message */
72                 ev->target.nid    = le64_to_cpu(hdr->dest_nid);
73                 ev->target.pid    = le32_to_cpu(hdr->dest_pid);
74                 ev->initiator.nid = LNET_NID_ANY;
75                 ev->initiator.pid = the_lnet.ln_pid;
76                 ev->source.nid    = LNET_NID_ANY;
77                 ev->source.pid    = the_lnet.ln_pid;
78                 ev->sender        = LNET_NID_ANY;
79         } else {
80                 /* event for passive message */
81                 ev->target.pid    = hdr->dest_pid;
82                 ev->target.nid    = hdr->dest_nid;
83                 ev->initiator.pid = hdr->src_pid;
84                 /* Multi-Rail: resolve src_nid to "primary" peer NID */
85                 ev->initiator.nid = msg->msg_initiator;
86                 /* Multi-Rail: track source NID. */
87                 ev->source.pid    = hdr->src_pid;
88                 ev->source.nid    = hdr->src_nid;
89                 ev->rlength       = hdr->payload_length;
90                 ev->sender        = msg->msg_from;
91                 ev->mlength       = msg->msg_wanted;
92                 ev->offset        = msg->msg_offset;
93         }
94
95         switch (ev_type) {
96         default:
97                 LBUG();
98
99         case LNET_EVENT_PUT: /* passive PUT */
100                 ev->pt_index   = hdr->msg.put.ptl_index;
101                 ev->match_bits = hdr->msg.put.match_bits;
102                 ev->hdr_data   = hdr->msg.put.hdr_data;
103                 return;
104
105         case LNET_EVENT_GET: /* passive GET */
106                 ev->pt_index   = hdr->msg.get.ptl_index;
107                 ev->match_bits = hdr->msg.get.match_bits;
108                 ev->hdr_data   = 0;
109                 return;
110
111         case LNET_EVENT_ACK: /* ACK */
112                 ev->match_bits = hdr->msg.ack.match_bits;
113                 ev->mlength    = hdr->msg.ack.mlength;
114                 return;
115
116         case LNET_EVENT_REPLY: /* REPLY */
117                 return;
118
119         case LNET_EVENT_SEND: /* active message */
120                 if (msg->msg_type == LNET_MSG_PUT) {
121                         ev->pt_index   = le32_to_cpu(hdr->msg.put.ptl_index);
122                         ev->match_bits = le64_to_cpu(hdr->msg.put.match_bits);
123                         ev->offset     = le32_to_cpu(hdr->msg.put.offset);
124                         ev->mlength    =
125                         ev->rlength    = le32_to_cpu(hdr->payload_length);
126                         ev->hdr_data   = le64_to_cpu(hdr->msg.put.hdr_data);
127
128                 } else {
129                         LASSERT(msg->msg_type == LNET_MSG_GET);
130                         ev->pt_index   = le32_to_cpu(hdr->msg.get.ptl_index);
131                         ev->match_bits = le64_to_cpu(hdr->msg.get.match_bits);
132                         ev->mlength    =
133                         ev->rlength    = le32_to_cpu(hdr->msg.get.sink_length);
134                         ev->offset     = le32_to_cpu(hdr->msg.get.src_offset);
135                         ev->hdr_data   = 0;
136                 }
137                 return;
138         }
139 }
140
141 void
142 lnet_msg_commit(struct lnet_msg *msg, int cpt)
143 {
144         struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
145         struct lnet_counters_common *common;
146         s64 timeout_ns;
147
148         /* set the message deadline */
149         timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
150         msg->msg_deadline = ktime_add_ns(ktime_get(), timeout_ns);
151
152         /* routed message can be committed for both receiving and sending */
153         LASSERT(!msg->msg_tx_committed);
154
155         if (msg->msg_sending) {
156                 LASSERT(!msg->msg_receiving);
157                 msg->msg_tx_cpt = cpt;
158                 msg->msg_tx_committed = 1;
159                 if (msg->msg_rx_committed) { /* routed message REPLY */
160                         LASSERT(msg->msg_onactivelist);
161                         return;
162                 }
163         } else {
164                 LASSERT(!msg->msg_sending);
165                 msg->msg_rx_cpt = cpt;
166                 msg->msg_rx_committed = 1;
167         }
168
169         LASSERT(!msg->msg_onactivelist);
170
171         msg->msg_onactivelist = 1;
172         list_add_tail(&msg->msg_activelist, &container->msc_active);
173
174         common = &the_lnet.ln_counters[cpt]->lct_common;
175         common->lcc_msgs_alloc++;
176         if (common->lcc_msgs_alloc > common->lcc_msgs_max)
177                 common->lcc_msgs_max = common->lcc_msgs_alloc;
178 }
179
180 static void
181 lnet_msg_decommit_tx(struct lnet_msg *msg, int status)
182 {
183         struct lnet_counters_common *common;
184         struct lnet_event *ev = &msg->msg_ev;
185
186         LASSERT(msg->msg_tx_committed);
187         if (status != 0)
188                 goto out;
189
190         common = &(the_lnet.ln_counters[msg->msg_tx_cpt]->lct_common);
191         switch (ev->type) {
192         default: /* routed message */
193                 LASSERT(msg->msg_routing);
194                 LASSERT(msg->msg_rx_committed);
195                 LASSERT(ev->type == 0);
196
197                 common->lcc_route_length += msg->msg_len;
198                 common->lcc_route_count++;
199                 goto incr_stats;
200
201         case LNET_EVENT_PUT:
202                 /* should have been decommitted */
203                 LASSERT(!msg->msg_rx_committed);
204                 /* overwritten while sending ACK */
205                 LASSERT(msg->msg_type == LNET_MSG_ACK);
206                 msg->msg_type = LNET_MSG_PUT; /* fix type */
207                 break;
208
209         case LNET_EVENT_SEND:
210                 LASSERT(!msg->msg_rx_committed);
211                 if (msg->msg_type == LNET_MSG_PUT)
212                         common->lcc_send_length += msg->msg_len;
213                 break;
214
215         case LNET_EVENT_GET:
216                 LASSERT(msg->msg_rx_committed);
217                 /* overwritten while sending reply, we should never be
218                  * here for optimized GET */
219                 LASSERT(msg->msg_type == LNET_MSG_REPLY);
220                 msg->msg_type = LNET_MSG_GET; /* fix type */
221                 break;
222         }
223
224         common->lcc_send_count++;
225
226 incr_stats:
227         if (msg->msg_txpeer)
228                 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
229                                 msg->msg_type,
230                                 LNET_STATS_TYPE_SEND);
231         if (msg->msg_txni)
232                 lnet_incr_stats(&msg->msg_txni->ni_stats,
233                                 msg->msg_type,
234                                 LNET_STATS_TYPE_SEND);
235  out:
236         lnet_return_tx_credits_locked(msg);
237         msg->msg_tx_committed = 0;
238 }
239
240 static void
241 lnet_msg_decommit_rx(struct lnet_msg *msg, int status)
242 {
243         struct lnet_counters_common *common;
244         struct lnet_event *ev = &msg->msg_ev;
245
246         LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
247         LASSERT(msg->msg_rx_committed);
248
249         if (status != 0)
250                 goto out;
251
252         common = &(the_lnet.ln_counters[msg->msg_rx_cpt]->lct_common);
253         switch (ev->type) {
254         default:
255                 LASSERT(ev->type == 0);
256                 LASSERT(msg->msg_routing);
257                 goto incr_stats;
258
259         case LNET_EVENT_ACK:
260                 LASSERT(msg->msg_type == LNET_MSG_ACK);
261                 break;
262
263         case LNET_EVENT_GET:
264                 /* type is "REPLY" if it's an optimized GET on passive side,
265                  * because optimized GET will never be committed for sending,
266                  * so message type wouldn't be changed back to "GET" by
267                  * lnet_msg_decommit_tx(), see details in lnet_parse_get() */
268                 LASSERT(msg->msg_type == LNET_MSG_REPLY ||
269                         msg->msg_type == LNET_MSG_GET);
270                 common->lcc_send_length += msg->msg_wanted;
271                 break;
272
273         case LNET_EVENT_PUT:
274                 LASSERT(msg->msg_type == LNET_MSG_PUT);
275                 break;
276
277         case LNET_EVENT_REPLY:
278                 /* type is "GET" if it's an optimized GET on active side,
279                  * see details in lnet_create_reply_msg() */
280                 LASSERT(msg->msg_type == LNET_MSG_GET ||
281                         msg->msg_type == LNET_MSG_REPLY);
282                 break;
283         }
284
285         common->lcc_recv_count++;
286
287 incr_stats:
288         if (msg->msg_rxpeer)
289                 lnet_incr_stats(&msg->msg_rxpeer->lpni_stats,
290                                 msg->msg_type,
291                                 LNET_STATS_TYPE_RECV);
292         if (msg->msg_rxni)
293                 lnet_incr_stats(&msg->msg_rxni->ni_stats,
294                                 msg->msg_type,
295                                 LNET_STATS_TYPE_RECV);
296         if (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_REPLY)
297                 common->lcc_recv_length += msg->msg_wanted;
298
299  out:
300         lnet_return_rx_credits_locked(msg);
301         msg->msg_rx_committed = 0;
302 }
303
304 void
305 lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status)
306 {
307         int     cpt2 = cpt;
308
309         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
310         LASSERT(msg->msg_onactivelist);
311
312         if (msg->msg_tx_committed) { /* always decommit for sending first */
313                 LASSERT(cpt == msg->msg_tx_cpt);
314                 lnet_msg_decommit_tx(msg, status);
315         }
316
317         if (msg->msg_rx_committed) {
318                 /* forwarding msg committed for both receiving and sending */
319                 if (cpt != msg->msg_rx_cpt) {
320                         lnet_net_unlock(cpt);
321                         cpt2 = msg->msg_rx_cpt;
322                         lnet_net_lock(cpt2);
323                 }
324                 lnet_msg_decommit_rx(msg, status);
325         }
326
327         list_del(&msg->msg_activelist);
328         msg->msg_onactivelist = 0;
329
330         the_lnet.ln_counters[cpt2]->lct_common.lcc_msgs_alloc--;
331
332         if (cpt2 != cpt) {
333                 lnet_net_unlock(cpt2);
334                 lnet_net_lock(cpt);
335         }
336 }
337
338 void
339 lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
340                    unsigned int offset, unsigned int mlen)
341 {
342         /* NB: @offset and @len are only useful for receiving */
343         /* Here, we attach the MD on lnet_msg and mark it busy and
344          * decrementing its threshold. Come what may, the lnet_msg "owns"
345          * the MD until a call to lnet_msg_detach_md or lnet_finalize()
346          * signals completion. */
347         LASSERT(!msg->msg_routing);
348
349         msg->msg_md = md;
350         if (msg->msg_receiving) { /* committed for receiving */
351                 msg->msg_offset = offset;
352                 msg->msg_wanted = mlen;
353         }
354
355         md->md_refcount++;
356         if (md->md_threshold != LNET_MD_THRESH_INF) {
357                 LASSERT(md->md_threshold > 0);
358                 md->md_threshold--;
359         }
360
361         /* build umd in event */
362         lnet_md2handle(&msg->msg_ev.md_handle, md);
363         lnet_md_deconstruct(md, &msg->msg_ev.md);
364 }
365
366 static int
367 lnet_complete_msg_locked(struct lnet_msg *msg, int cpt)
368 {
369         struct lnet_handle_wire ack_wmd;
370         int                rc;
371         int                status = msg->msg_ev.status;
372
373         LASSERT(msg->msg_onactivelist);
374
375         if (status == 0 && msg->msg_ack) {
376                 /* Only send an ACK if the PUT completed successfully */
377
378                 lnet_msg_decommit(msg, cpt, 0);
379
380                 msg->msg_ack = 0;
381                 lnet_net_unlock(cpt);
382
383                 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
384                 LASSERT(!msg->msg_routing);
385
386                 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
387
388                 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.source, 0, 0);
389
390                 msg->msg_hdr.msg.ack.dst_wmd = ack_wmd;
391                 msg->msg_hdr.msg.ack.match_bits = msg->msg_ev.match_bits;
392                 msg->msg_hdr.msg.ack.mlength = cpu_to_le32(msg->msg_ev.mlength);
393
394                 /* NB: we probably want to use NID of msg::msg_from as 3rd
395                  * parameter (router NID) if it's routed message */
396                 rc = lnet_send(msg->msg_ev.target.nid, msg, msg->msg_from);
397
398                 lnet_net_lock(cpt);
399                 /*
400                  * NB: message is committed for sending, we should return
401                  * on success because LND will finalize this message later.
402                  *
403                  * Also, there is possibility that message is committed for
404                  * sending and also failed before delivering to LND,
405                  * i.e: ENOMEM, in that case we can't fall through either
406                  * because CPT for sending can be different with CPT for
407                  * receiving, so we should return back to lnet_finalize()
408                  * to make sure we are locking the correct partition.
409                  */
410                 return rc;
411
412         } else if (status == 0 &&       /* OK so far */
413                    (msg->msg_routing && !msg->msg_sending)) {
414                 /* not forwarded */
415                 LASSERT(!msg->msg_receiving);   /* called back recv already */
416                 lnet_net_unlock(cpt);
417
418                 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
419
420                 lnet_net_lock(cpt);
421                 /*
422                  * NB: message is committed for sending, we should return
423                  * on success because LND will finalize this message later.
424                  *
425                  * Also, there is possibility that message is committed for
426                  * sending and also failed before delivering to LND,
427                  * i.e: ENOMEM, in that case we can't fall through either:
428                  * - The rule is message must decommit for sending first if
429                  *   the it's committed for both sending and receiving
430                  * - CPT for sending can be different with CPT for receiving,
431                  *   so we should return back to lnet_finalize() to make
432                  *   sure we are locking the correct partition.
433                  */
434                 return rc;
435         }
436
437         lnet_msg_decommit(msg, cpt, status);
438         lnet_msg_free(msg);
439         return 0;
440 }
441
442 static void
443 lnet_dec_healthv_locked(atomic_t *healthv)
444 {
445         int h = atomic_read(healthv);
446
447         if (h < lnet_health_sensitivity) {
448                 atomic_set(healthv, 0);
449         } else {
450                 h -= lnet_health_sensitivity;
451                 atomic_set(healthv, h);
452         }
453 }
454
455 static void
456 lnet_handle_local_failure(struct lnet_msg *msg)
457 {
458         struct lnet_ni *local_ni;
459
460         local_ni = msg->msg_txni;
461
462         /*
463          * the lnet_net_lock(0) is used to protect the addref on the ni
464          * and the recovery queue.
465          */
466         lnet_net_lock(0);
467         /* the mt could've shutdown and cleaned up the queues */
468         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
469                 lnet_net_unlock(0);
470                 return;
471         }
472
473         lnet_dec_healthv_locked(&local_ni->ni_healthv);
474         /*
475          * add the NI to the recovery queue if it's not already there
476          * and it's health value is actually below the maximum. It's
477          * possible that the sensitivity might be set to 0, and the health
478          * value will not be reduced. In this case, there is no reason to
479          * invoke recovery
480          */
481         if (list_empty(&local_ni->ni_recovery) &&
482             atomic_read(&local_ni->ni_healthv) < LNET_MAX_HEALTH_VALUE) {
483                 CERROR("ni %s added to recovery queue. Health = %d\n",
484                         libcfs_nid2str(local_ni->ni_nid),
485                         atomic_read(&local_ni->ni_healthv));
486                 list_add_tail(&local_ni->ni_recovery,
487                               &the_lnet.ln_mt_localNIRecovq);
488                 lnet_ni_addref_locked(local_ni, 0);
489         }
490         lnet_net_unlock(0);
491 }
492
493 void
494 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
495 {
496         /* lpni could be NULL if we're in the LOLND case */
497         if (!lpni)
498                 return;
499
500         lnet_dec_healthv_locked(&lpni->lpni_healthv);
501         /*
502          * add the peer NI to the recovery queue if it's not already there
503          * and it's health value is actually below the maximum. It's
504          * possible that the sensitivity might be set to 0, and the health
505          * value will not be reduced. In this case, there is no reason to
506          * invoke recovery
507          */
508         lnet_peer_ni_add_to_recoveryq_locked(lpni);
509 }
510
511 static void
512 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
513 {
514         /* lpni could be NULL if we're in the LOLND case */
515         if (!lpni)
516                 return;
517
518         lnet_net_lock(0);
519         /* the mt could've shutdown and cleaned up the queues */
520         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
521                 lnet_net_unlock(0);
522                 return;
523         }
524         lnet_handle_remote_failure_locked(lpni);
525         lnet_net_unlock(0);
526 }
527
528 static void
529 lnet_incr_hstats(struct lnet_msg *msg, enum lnet_msg_hstatus hstatus)
530 {
531         struct lnet_ni *ni = msg->msg_txni;
532         struct lnet_peer_ni *lpni = msg->msg_txpeer;
533         struct lnet_counters_health *health;
534
535         health = &the_lnet.ln_counters[0]->lct_health;
536
537         switch (hstatus) {
538         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
539                 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
540                 health->lch_local_interrupt_count++;
541                 break;
542         case LNET_MSG_STATUS_LOCAL_DROPPED:
543                 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
544                 health->lch_local_dropped_count++;
545                 break;
546         case LNET_MSG_STATUS_LOCAL_ABORTED:
547                 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
548                 health->lch_local_aborted_count++;
549                 break;
550         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
551                 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
552                 health->lch_local_no_route_count++;
553                 break;
554         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
555                 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
556                 health->lch_local_timeout_count++;
557                 break;
558         case LNET_MSG_STATUS_LOCAL_ERROR:
559                 atomic_inc(&ni->ni_hstats.hlt_local_error);
560                 health->lch_local_error_count++;
561                 break;
562         case LNET_MSG_STATUS_REMOTE_DROPPED:
563                 if (lpni)
564                         atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
565                 health->lch_remote_dropped_count++;
566                 break;
567         case LNET_MSG_STATUS_REMOTE_ERROR:
568                 if (lpni)
569                         atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
570                 health->lch_remote_error_count++;
571                 break;
572         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
573                 if (lpni)
574                         atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
575                 health->lch_remote_timeout_count++;
576                 break;
577         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
578                 if (lpni)
579                         atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
580                 health->lch_network_timeout_count++;
581                 break;
582         case LNET_MSG_STATUS_OK:
583                 break;
584         default:
585                 LBUG();
586         }
587 }
588
589 /*
590  * Do a health check on the message:
591  * return -1 if we're not going to handle the error or
592  *   if we've reached the maximum number of retries.
593  *   success case will return -1 as well
594  * return 0 if it the message is requeued for send
595  */
596 static int
597 lnet_health_check(struct lnet_msg *msg)
598 {
599         enum lnet_msg_hstatus hstatus = msg->msg_health_status;
600         bool lo = false;
601
602         /* if we're shutting down no point in handling health. */
603         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
604                 return -1;
605
606         LASSERT(msg->msg_txni);
607
608         /*
609          * if we're sending to the LOLND then the msg_txpeer will not be
610          * set. So no need to sanity check it.
611          */
612         if (LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) != LOLND)
613                 LASSERT(msg->msg_txpeer);
614         else
615                 lo = true;
616
617         if (hstatus != LNET_MSG_STATUS_OK &&
618             ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
619                 return -1;
620
621         /*
622          * stats are only incremented for errors so avoid wasting time
623          * incrementing statistics if there is no error.
624          */
625         if (hstatus != LNET_MSG_STATUS_OK) {
626                 lnet_net_lock(0);
627                 lnet_incr_hstats(msg, hstatus);
628                 lnet_net_unlock(0);
629         }
630
631         CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
632                libcfs_nid2str(msg->msg_txni->ni_nid),
633                (lo) ? "self" : libcfs_nid2str(msg->msg_txpeer->lpni_nid),
634                lnet_msgtyp2str(msg->msg_type),
635                lnet_health_error2str(hstatus));
636
637         switch (hstatus) {
638         case LNET_MSG_STATUS_OK:
639                 lnet_inc_healthv(&msg->msg_txni->ni_healthv);
640                 /*
641                  * It's possible msg_txpeer is NULL in the LOLND
642                  * case.
643                  */
644                 if (msg->msg_txpeer)
645                         lnet_inc_healthv(&msg->msg_txpeer->lpni_healthv);
646
647                 /* we can finalize this message */
648                 return -1;
649         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
650         case LNET_MSG_STATUS_LOCAL_DROPPED:
651         case LNET_MSG_STATUS_LOCAL_ABORTED:
652         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
653         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
654                 lnet_handle_local_failure(msg);
655                 /* add to the re-send queue */
656                 goto resend;
657
658         /*
659          * These errors will not trigger a resend so simply
660          * finalize the message
661          */
662         case LNET_MSG_STATUS_LOCAL_ERROR:
663                 lnet_handle_local_failure(msg);
664                 return -1;
665
666         /*
667          * TODO: since the remote dropped the message we can
668          * attempt a resend safely.
669          */
670         case LNET_MSG_STATUS_REMOTE_DROPPED:
671                 lnet_handle_remote_failure(msg->msg_txpeer);
672                 goto resend;
673
674         case LNET_MSG_STATUS_REMOTE_ERROR:
675         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
676         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
677                 lnet_handle_remote_failure(msg->msg_txpeer);
678                 return -1;
679         default:
680                 LBUG();
681         }
682
683 resend:
684         /* don't resend recovery messages */
685         if (msg->msg_recovery) {
686                 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
687                         libcfs_nid2str(msg->msg_from),
688                         libcfs_nid2str(msg->msg_target.nid),
689                         msg->msg_retry_count);
690                 return -1;
691         }
692
693         /*
694          * if we explicitly indicated we don't want to resend then just
695          * return
696          */
697         if (msg->msg_no_resend) {
698                 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
699                         libcfs_nid2str(msg->msg_from),
700                         libcfs_nid2str(msg->msg_target.nid),
701                         msg->msg_retry_count);
702                 return -1;
703         }
704
705         /* check if the message has exceeded the number of retries */
706         if (msg->msg_retry_count >= lnet_retry_count) {
707                 CNETERR("msg %s->%s exceeded retry count %d\n",
708                         libcfs_nid2str(msg->msg_from),
709                         libcfs_nid2str(msg->msg_target.nid),
710                         msg->msg_retry_count);
711                 return -1;
712         }
713         msg->msg_retry_count++;
714
715         lnet_net_lock(msg->msg_tx_cpt);
716
717         /* check again under lock */
718         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
719                 lnet_net_unlock(msg->msg_tx_cpt);
720                 return -1;
721         }
722
723         /*
724          * remove message from the active list and reset it in preparation
725          * for a resend. Two exception to this
726          *
727          * 1. the router case, whe a message is committed for rx when
728          * received, then tx when it is sent. When committed to both tx and
729          * rx we don't want to remove it from the active list.
730          *
731          * 2. The REPLY case since it uses the same msg block for the GET
732          * that was received.
733          */
734         if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
735                 list_del_init(&msg->msg_activelist);
736                 msg->msg_onactivelist = 0;
737         }
738         /*
739          * The msg_target.nid which was originally set
740          * when calling LNetGet() or LNetPut() might've
741          * been overwritten if we're routing this message.
742          * Call lnet_return_tx_credits_locked() to return
743          * the credit this message consumed. The message will
744          * consume another credit when it gets resent.
745          */
746         msg->msg_target.nid = msg->msg_hdr.dest_nid;
747         lnet_msg_decommit_tx(msg, -EAGAIN);
748         msg->msg_sending = 0;
749         msg->msg_receiving = 0;
750         msg->msg_target_is_router = 0;
751
752         CDEBUG(D_NET, "%s->%s:%s:%s - queuing for resend\n",
753                libcfs_nid2str(msg->msg_hdr.src_nid),
754                libcfs_nid2str(msg->msg_hdr.dest_nid),
755                lnet_msgtyp2str(msg->msg_type),
756                lnet_health_error2str(hstatus));
757
758         list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
759         lnet_net_unlock(msg->msg_tx_cpt);
760
761         wake_up(&the_lnet.ln_mt_waitq);
762         return 0;
763 }
764
765 static void
766 lnet_msg_detach_md(struct lnet_msg *msg, int cpt, int status)
767 {
768         struct lnet_libmd *md = msg->msg_md;
769         int unlink;
770
771         /* Now it's safe to drop my caller's ref */
772         md->md_refcount--;
773         LASSERT(md->md_refcount >= 0);
774
775         unlink = lnet_md_unlinkable(md);
776         if (md->md_eq != NULL) {
777                 msg->msg_ev.status   = status;
778                 msg->msg_ev.unlinked = unlink;
779                 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
780         }
781
782         if (unlink) {
783                 lnet_detach_rsp_tracker(md, cpt);
784                 lnet_md_unlink(md);
785         }
786
787         msg->msg_md = NULL;
788 }
789
790 static bool
791 lnet_is_health_check(struct lnet_msg *msg)
792 {
793         bool hc;
794         int status = msg->msg_ev.status;
795
796         /*
797          * perform a health check for any message committed for transmit
798          */
799         hc = msg->msg_tx_committed;
800
801         /* Check for status inconsistencies */
802         if (hc &&
803             ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
804              (status && msg->msg_health_status == LNET_MSG_STATUS_OK))) {
805                 CERROR("Msg is in inconsistent state, don't perform health "
806                        "checking (%d, %d)\n", status, msg->msg_health_status);
807                 hc = false;
808         }
809
810         CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
811                hc, status, msg->msg_health_status);
812
813         return hc;
814 }
815
816 char *
817 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
818 {
819         switch (hstatus) {
820         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
821                 return "LOCAL_INTERRUPT";
822         case LNET_MSG_STATUS_LOCAL_DROPPED:
823                 return "LOCAL_DROPPED";
824         case LNET_MSG_STATUS_LOCAL_ABORTED:
825                 return "LOCAL_ABORTED";
826         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
827                 return "LOCAL_NO_ROUTE";
828         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
829                 return "LOCAL_TIMEOUT";
830         case LNET_MSG_STATUS_LOCAL_ERROR:
831                 return "LOCAL_ERROR";
832         case LNET_MSG_STATUS_REMOTE_DROPPED:
833                 return "REMOTE_DROPPED";
834         case LNET_MSG_STATUS_REMOTE_ERROR:
835                 return "REMOTE_ERROR";
836         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
837                 return "REMOTE_TIMEOUT";
838         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
839                 return "NETWORK_TIMEOUT";
840         case LNET_MSG_STATUS_OK:
841                 return "OK";
842         default:
843                 return "<UNKNOWN>";
844         }
845 }
846
847 bool
848 lnet_send_error_simulation(struct lnet_msg *msg,
849                            enum lnet_msg_hstatus *hstatus)
850 {
851         if (!msg)
852                 return false;
853
854         if (list_empty(&the_lnet.ln_drop_rules))
855             return false;
856
857         /* match only health rules */
858         if (!lnet_drop_rule_match(&msg->msg_hdr, hstatus))
859                 return false;
860
861         CDEBUG(D_NET, "src %s, dst %s: %s simulate health error: %s\n",
862                 libcfs_nid2str(msg->msg_hdr.src_nid),
863                 libcfs_nid2str(msg->msg_hdr.dest_nid),
864                 lnet_msgtyp2str(msg->msg_type),
865                 lnet_health_error2str(*hstatus));
866
867         return true;
868 }
869 EXPORT_SYMBOL(lnet_send_error_simulation);
870
871 void
872 lnet_finalize(struct lnet_msg *msg, int status)
873 {
874         struct lnet_msg_container *container;
875         int my_slot;
876         int cpt;
877         int rc;
878         int i;
879
880         LASSERT(!in_interrupt());
881
882         if (msg == NULL)
883                 return;
884
885         msg->msg_ev.status = status;
886
887         if (lnet_is_health_check(msg)) {
888                 /*
889                  * Check the health status of the message. If it has one
890                  * of the errors that we're supposed to handle, and it has
891                  * not timed out, then
892                  *      1. Decrement the appropriate health_value
893                  *      2. queue the message on the resend queue
894
895                  * if the message send is success, timed out or failed in the
896                  * health check for any reason then we'll just finalize the
897                  * message. Otherwise just return since the message has been
898                  * put on the resend queue.
899                  */
900                 if (!lnet_health_check(msg))
901                         /* Message is queued for resend */
902                         return;
903         }
904
905         /*
906          * We're not going to resend this message so detach its MD and invoke
907          * the appropriate callbacks
908          */
909         if (msg->msg_md != NULL) {
910                 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
911                 lnet_res_lock(cpt);
912                 lnet_msg_detach_md(msg, cpt, status);
913                 lnet_res_unlock(cpt);
914         }
915
916 again:
917         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
918                 /* not committed to network yet */
919                 LASSERT(!msg->msg_onactivelist);
920                 lnet_msg_free(msg);
921                 return;
922         }
923
924         /*
925          * NB: routed message can be committed for both receiving and sending,
926          * we should finalize in LIFO order and keep counters correct.
927          * (finalize sending first then finalize receiving)
928          */
929         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
930         lnet_net_lock(cpt);
931
932         container = the_lnet.ln_msg_containers[cpt];
933         list_add_tail(&msg->msg_list, &container->msc_finalizing);
934
935         /* Recursion breaker.  Don't complete the message here if I am (or
936          * enough other threads are) already completing messages */
937
938         my_slot = -1;
939         for (i = 0; i < container->msc_nfinalizers; i++) {
940                 if (container->msc_finalizers[i] == current)
941                         break;
942
943                 if (my_slot < 0 && container->msc_finalizers[i] == NULL)
944                         my_slot = i;
945         }
946
947         if (i < container->msc_nfinalizers || my_slot < 0) {
948                 lnet_net_unlock(cpt);
949                 return;
950         }
951
952         container->msc_finalizers[my_slot] = current;
953
954         rc = 0;
955         while (!list_empty(&container->msc_finalizing)) {
956                 msg = list_entry(container->msc_finalizing.next,
957                                  struct lnet_msg, msg_list);
958
959                 list_del_init(&msg->msg_list);
960
961                 /* NB drops and regains the lnet lock if it actually does
962                  * anything, so my finalizing friends can chomp along too */
963                 rc = lnet_complete_msg_locked(msg, cpt);
964                 if (rc != 0)
965                         break;
966         }
967
968         if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
969                 lnet_net_unlock(cpt);
970                 lnet_delay_rule_check();
971                 lnet_net_lock(cpt);
972         }
973
974         container->msc_finalizers[my_slot] = NULL;
975         lnet_net_unlock(cpt);
976
977         if (rc != 0)
978                 goto again;
979 }
980 EXPORT_SYMBOL(lnet_finalize);
981
982 void
983 lnet_msg_container_cleanup(struct lnet_msg_container *container)
984 {
985         int     count = 0;
986
987         if (container->msc_init == 0)
988                 return;
989
990         while (!list_empty(&container->msc_active)) {
991                 struct lnet_msg *msg;
992
993                 msg  = list_entry(container->msc_active.next,
994                                   struct lnet_msg, msg_activelist);
995                 LASSERT(msg->msg_onactivelist);
996                 msg->msg_onactivelist = 0;
997                 list_del_init(&msg->msg_activelist);
998                 lnet_msg_free(msg);
999                 count++;
1000         }
1001
1002         if (count > 0)
1003                 CERROR("%d active msg on exit\n", count);
1004
1005         if (container->msc_finalizers != NULL) {
1006                 LIBCFS_FREE(container->msc_finalizers,
1007                             container->msc_nfinalizers *
1008                             sizeof(*container->msc_finalizers));
1009                 container->msc_finalizers = NULL;
1010         }
1011         container->msc_init = 0;
1012 }
1013
1014 int
1015 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1016 {
1017         int rc = 0;
1018
1019         container->msc_init = 1;
1020
1021         INIT_LIST_HEAD(&container->msc_active);
1022         INIT_LIST_HEAD(&container->msc_finalizing);
1023
1024         /* number of CPUs */
1025         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1026         if (container->msc_nfinalizers == 0)
1027                 container->msc_nfinalizers = 1;
1028
1029         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1030                          container->msc_nfinalizers *
1031                          sizeof(*container->msc_finalizers));
1032
1033         if (container->msc_finalizers == NULL) {
1034                 CERROR("Failed to allocate message finalizers\n");
1035                 lnet_msg_container_cleanup(container);
1036                 return -ENOMEM;
1037         }
1038
1039         return rc;
1040 }
1041
1042 void
1043 lnet_msg_containers_destroy(void)
1044 {
1045         struct lnet_msg_container *container;
1046         int     i;
1047
1048         if (the_lnet.ln_msg_containers == NULL)
1049                 return;
1050
1051         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1052                 lnet_msg_container_cleanup(container);
1053
1054         cfs_percpt_free(the_lnet.ln_msg_containers);
1055         the_lnet.ln_msg_containers = NULL;
1056 }
1057
1058 int
1059 lnet_msg_containers_create(void)
1060 {
1061         struct lnet_msg_container *container;
1062         int     rc;
1063         int     i;
1064
1065         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1066                                                       sizeof(*container));
1067
1068         if (the_lnet.ln_msg_containers == NULL) {
1069                 CERROR("Failed to allocate cpu-partition data for network\n");
1070                 return -ENOMEM;
1071         }
1072
1073         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1074                 rc = lnet_msg_container_setup(container, i);
1075                 if (rc != 0) {
1076                         lnet_msg_containers_destroy();
1077                         return rc;
1078                 }
1079         }
1080
1081         return 0;
1082 }