Whamcloud - gitweb
LU-12292 lnet: keep health even if recovery failed
[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                 rc = lnet_send(msg->msg_ev.target.nid, msg, msg->msg_from);
395
396                 lnet_net_lock(cpt);
397                 /*
398                  * NB: message is committed for sending, we should return
399                  * on success because LND will finalize this message later.
400                  *
401                  * Also, there is possibility that message is committed for
402                  * sending and also failed before delivering to LND,
403                  * i.e: ENOMEM, in that case we can't fall through either
404                  * because CPT for sending can be different with CPT for
405                  * receiving, so we should return back to lnet_finalize()
406                  * to make sure we are locking the correct partition.
407                  */
408                 return rc;
409
410         } else if (status == 0 &&       /* OK so far */
411                    (msg->msg_routing && !msg->msg_sending)) {
412                 /* not forwarded */
413                 LASSERT(!msg->msg_receiving);   /* called back recv already */
414                 lnet_net_unlock(cpt);
415
416                 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
417
418                 lnet_net_lock(cpt);
419                 /*
420                  * NB: message is committed for sending, we should return
421                  * on success because LND will finalize this message later.
422                  *
423                  * Also, there is possibility that message is committed for
424                  * sending and also failed before delivering to LND,
425                  * i.e: ENOMEM, in that case we can't fall through either:
426                  * - The rule is message must decommit for sending first if
427                  *   the it's committed for both sending and receiving
428                  * - CPT for sending can be different with CPT for receiving,
429                  *   so we should return back to lnet_finalize() to make
430                  *   sure we are locking the correct partition.
431                  */
432                 return rc;
433         }
434
435         lnet_msg_decommit(msg, cpt, status);
436         lnet_msg_free(msg);
437         return 0;
438 }
439
440 static void
441 lnet_dec_healthv_locked(atomic_t *healthv, int sensitivity)
442 {
443         int h = atomic_read(healthv);
444
445         if (h < sensitivity) {
446                 atomic_set(healthv, 0);
447         } else {
448                 h -= sensitivity;
449                 atomic_set(healthv, h);
450         }
451 }
452
453 static void
454 lnet_handle_local_failure(struct lnet_ni *local_ni)
455 {
456         /*
457          * the lnet_net_lock(0) is used to protect the addref on the ni
458          * and the recovery queue.
459          */
460         lnet_net_lock(0);
461         /* the mt could've shutdown and cleaned up the queues */
462         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
463                 lnet_net_unlock(0);
464                 return;
465         }
466
467         lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
468         /*
469          * add the NI to the recovery queue if it's not already there
470          * and it's health value is actually below the maximum. It's
471          * possible that the sensitivity might be set to 0, and the health
472          * value will not be reduced. In this case, there is no reason to
473          * invoke recovery
474          */
475         if (list_empty(&local_ni->ni_recovery) &&
476             atomic_read(&local_ni->ni_healthv) < LNET_MAX_HEALTH_VALUE) {
477                 CDEBUG(D_NET, "ni %s added to recovery queue. Health = %d\n",
478                         libcfs_nid2str(local_ni->ni_nid),
479                         atomic_read(&local_ni->ni_healthv));
480                 list_add_tail(&local_ni->ni_recovery,
481                               &the_lnet.ln_mt_localNIRecovq);
482                 lnet_ni_addref_locked(local_ni, 0);
483         }
484         lnet_net_unlock(0);
485 }
486
487 void
488 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
489 {
490         __u32 sensitivity = lnet_health_sensitivity;
491         __u32 lp_sensitivity;
492
493         /*
494          * NO-OP if:
495          * 1. lpni could be NULL if we're in the LOLND case
496          * 2. this is a recovery message
497          */
498         if (!lpni)
499                 return;
500
501         /*
502          * If there is a health sensitivity in the peer then use that
503          * instead of the globally set one.
504          */
505         lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
506         if (lp_sensitivity)
507                 sensitivity = lp_sensitivity;
508
509         lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
510
511         /* update the peer_net's health value */
512         lnet_update_peer_net_healthv(lpni);
513
514         /*
515          * add the peer NI to the recovery queue if it's not already there
516          * and it's health value is actually below the maximum. It's
517          * possible that the sensitivity might be set to 0, and the health
518          * value will not be reduced. In this case, there is no reason to
519          * invoke recovery
520          */
521         lnet_peer_ni_add_to_recoveryq_locked(lpni);
522 }
523
524 static void
525 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
526 {
527         /* lpni could be NULL if we're in the LOLND case */
528         if (!lpni)
529                 return;
530
531         lnet_net_lock(0);
532         /* the mt could've shutdown and cleaned up the queues */
533         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
534                 lnet_net_unlock(0);
535                 return;
536         }
537         lnet_handle_remote_failure_locked(lpni);
538         lnet_net_unlock(0);
539 }
540
541 static void
542 lnet_incr_hstats(struct lnet_msg *msg, enum lnet_msg_hstatus hstatus)
543 {
544         struct lnet_ni *ni = msg->msg_txni;
545         struct lnet_peer_ni *lpni = msg->msg_txpeer;
546         struct lnet_counters_health *health;
547
548         health = &the_lnet.ln_counters[0]->lct_health;
549
550         switch (hstatus) {
551         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
552                 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
553                 health->lch_local_interrupt_count++;
554                 break;
555         case LNET_MSG_STATUS_LOCAL_DROPPED:
556                 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
557                 health->lch_local_dropped_count++;
558                 break;
559         case LNET_MSG_STATUS_LOCAL_ABORTED:
560                 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
561                 health->lch_local_aborted_count++;
562                 break;
563         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
564                 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
565                 health->lch_local_no_route_count++;
566                 break;
567         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
568                 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
569                 health->lch_local_timeout_count++;
570                 break;
571         case LNET_MSG_STATUS_LOCAL_ERROR:
572                 atomic_inc(&ni->ni_hstats.hlt_local_error);
573                 health->lch_local_error_count++;
574                 break;
575         case LNET_MSG_STATUS_REMOTE_DROPPED:
576                 if (lpni)
577                         atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
578                 health->lch_remote_dropped_count++;
579                 break;
580         case LNET_MSG_STATUS_REMOTE_ERROR:
581                 if (lpni)
582                         atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
583                 health->lch_remote_error_count++;
584                 break;
585         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
586                 if (lpni)
587                         atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
588                 health->lch_remote_timeout_count++;
589                 break;
590         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
591                 if (lpni)
592                         atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
593                 health->lch_network_timeout_count++;
594                 break;
595         case LNET_MSG_STATUS_OK:
596                 break;
597         default:
598                 LBUG();
599         }
600 }
601
602 static void
603 lnet_resend_msg_locked(struct lnet_msg *msg)
604 {
605         msg->msg_retry_count++;
606
607         /*
608          * remove message from the active list and reset it to prepare
609          * for a resend. Two exceptions to this
610          *
611          * 1. the router case. When a message is being routed it is
612          * committed for rx when received and committed for tx when
613          * forwarded. We don't want to remove it from the active list, since
614          * code which handles receiving expects it to remain on the active
615          * list.
616          *
617          * 2. The REPLY case. Reply messages use the same message
618          * structure for the GET that was received.
619          */
620         if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
621                 list_del_init(&msg->msg_activelist);
622                 msg->msg_onactivelist = 0;
623         }
624         /*
625          * The msg_target.nid which was originally set
626          * when calling LNetGet() or LNetPut() might've
627          * been overwritten if we're routing this message.
628          * Call lnet_msg_decommit_tx() to return the credit
629          * this message consumed. The message will
630          * consume another credit when it gets resent.
631          */
632         msg->msg_target.nid = msg->msg_hdr.dest_nid;
633         lnet_msg_decommit_tx(msg, -EAGAIN);
634         msg->msg_sending = 0;
635         msg->msg_receiving = 0;
636         msg->msg_target_is_router = 0;
637
638         CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
639                libcfs_nid2str(msg->msg_hdr.src_nid),
640                libcfs_nid2str(msg->msg_hdr.dest_nid),
641                lnet_msgtyp2str(msg->msg_type),
642                lnet_health_error2str(msg->msg_health_status), msg);
643
644         list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
645
646         complete(&the_lnet.ln_mt_wait_complete);
647 }
648
649 int
650 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
651                                      struct list_head *containerq,
652                                      int nworkers, void **workers)
653 {
654         int my_slot = -1;
655         int i;
656
657         list_add_tail(&msg->msg_list, containerq);
658
659         for (i = 0; i < nworkers; i++) {
660                 if (workers[i] == current)
661                         break;
662
663                 if (my_slot < 0 && workers[i] == NULL)
664                         my_slot = i;
665         }
666
667         if (i < nworkers || my_slot < 0)
668                 return -1;
669
670         workers[my_slot] = current;
671
672         return my_slot;
673 }
674
675 int
676 lnet_attempt_msg_resend(struct lnet_msg *msg)
677 {
678         struct lnet_msg_container *container;
679         int my_slot;
680         int cpt;
681
682         /* we can only resend tx_committed messages */
683         LASSERT(msg->msg_tx_committed);
684
685         /* don't resend recovery messages */
686         if (msg->msg_recovery) {
687                 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
688                         libcfs_nid2str(msg->msg_from),
689                         libcfs_nid2str(msg->msg_target.nid),
690                         msg->msg_retry_count);
691                 return -ENOTRECOVERABLE;
692         }
693
694         /*
695          * if we explicitly indicated we don't want to resend then just
696          * return
697          */
698         if (msg->msg_no_resend) {
699                 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
700                         libcfs_nid2str(msg->msg_from),
701                         libcfs_nid2str(msg->msg_target.nid),
702                         msg->msg_retry_count);
703                 return -ENOTRECOVERABLE;
704         }
705
706         /* check if the message has exceeded the number of retries */
707         if (msg->msg_retry_count >= lnet_retry_count) {
708                 CNETERR("msg %s->%s exceeded retry count %d\n",
709                         libcfs_nid2str(msg->msg_from),
710                         libcfs_nid2str(msg->msg_target.nid),
711                         msg->msg_retry_count);
712                 return -ENOTRECOVERABLE;
713         }
714
715         cpt = msg->msg_tx_cpt;
716         lnet_net_lock(cpt);
717
718         /* check again under lock */
719         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
720                 lnet_net_unlock(cpt);
721                 return -ESHUTDOWN;
722         }
723
724         container = the_lnet.ln_msg_containers[cpt];
725         my_slot =
726                 lnet_check_finalize_recursion_locked(msg,
727                                         &container->msc_resending,
728                                         container->msc_nfinalizers,
729                                         container->msc_resenders);
730
731         /* enough threads are resending */
732         if (my_slot == -1) {
733                 lnet_net_unlock(cpt);
734                 return 0;
735         }
736
737         while (!list_empty(&container->msc_resending)) {
738                 msg = list_entry(container->msc_resending.next,
739                                         struct lnet_msg, msg_list);
740                 list_del(&msg->msg_list);
741
742                 /*
743                  * resending the message will require us to call
744                  * lnet_msg_decommit_tx() which will return the credit
745                  * which this message holds. This could trigger another
746                  * queued message to be sent. If that message fails and
747                  * requires a resend we will recurse.
748                  * But since at this point the slot is taken, the message
749                  * will be queued in the container and dealt with
750                  * later. This breaks the recursion.
751                  */
752                 lnet_resend_msg_locked(msg);
753         }
754
755         /*
756          * msc_resenders is an array of process pointers. Each entry holds
757          * a pointer to the current process operating on the message. An
758          * array entry is created per CPT. If the array slot is already
759          * set, then it means that there is a thread on the CPT currently
760          * resending a message.
761          * Once the thread finishes clear the slot to enable the thread to
762          * take on more resend work.
763          */
764         container->msc_resenders[my_slot] = NULL;
765         lnet_net_unlock(cpt);
766
767         return 0;
768 }
769
770 /*
771  * Do a health check on the message:
772  * return -1 if we're not going to handle the error or
773  *   if we've reached the maximum number of retries.
774  *   success case will return -1 as well
775  * return 0 if it the message is requeued for send
776  */
777 static int
778 lnet_health_check(struct lnet_msg *msg)
779 {
780         enum lnet_msg_hstatus hstatus = msg->msg_health_status;
781         struct lnet_peer_ni *lpni;
782         struct lnet_ni *ni;
783         bool lo = false;
784
785         /* if we're shutting down no point in handling health. */
786         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
787                 return -1;
788
789         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
790
791         /*
792          * if we're sending to the LOLND then the msg_txpeer will not be
793          * set. So no need to sanity check it.
794          */
795         if (msg->msg_tx_committed &&
796             LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) != LOLND)
797                 LASSERT(msg->msg_txpeer);
798         else if (msg->msg_tx_committed &&
799                  LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) == LOLND)
800                 lo = true;
801
802         if (hstatus != LNET_MSG_STATUS_OK &&
803             ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
804                 return -1;
805
806         /*
807          * stats are only incremented for errors so avoid wasting time
808          * incrementing statistics if there is no error.
809          */
810         if (hstatus != LNET_MSG_STATUS_OK) {
811                 lnet_net_lock(0);
812                 lnet_incr_hstats(msg, hstatus);
813                 lnet_net_unlock(0);
814         }
815
816         /*
817          * always prefer txni/txpeer if they message is committed for both
818          * directions.
819          */
820         if (msg->msg_tx_committed) {
821                 ni = msg->msg_txni;
822                 lpni = msg->msg_txpeer;
823         } else {
824                 ni = msg->msg_rxni;
825                 lpni = msg->msg_rxpeer;
826         }
827
828         if (!lo)
829                 LASSERT(ni && lpni);
830         else
831                 LASSERT(ni);
832
833         CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
834                libcfs_nid2str(ni->ni_nid),
835                (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
836                lnet_msgtyp2str(msg->msg_type),
837                lnet_health_error2str(hstatus));
838
839         switch (hstatus) {
840         case LNET_MSG_STATUS_OK:
841                 /*
842                  * increment the local ni health weather we successfully
843                  * received or sent a message on it.
844                  */
845                 lnet_inc_healthv(&ni->ni_healthv, lnet_health_sensitivity);
846                 /*
847                  * It's possible msg_txpeer is NULL in the LOLND
848                  * case. Only increment the peer's health if we're
849                  * receiving a message from it. It's the only sure way to
850                  * know that a remote interface is up.
851                  * If this interface is part of a router, then take that
852                  * as indication that the router is fully healthy.
853                  */
854                 if (lpni && msg->msg_rx_committed) {
855                         /*
856                          * If we're receiving a message from the router or
857                          * I'm a router, then set that lpni's health to
858                          * maximum so we can commence communication
859                          */
860                         lnet_net_lock(0);
861                         if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
862                                 lnet_set_lpni_healthv_locked(lpni,
863                                         LNET_MAX_HEALTH_VALUE);
864                         } else {
865                                 __u32 sensitivity = lpni->lpni_peer_net->
866                                         lpn_peer->lp_health_sensitivity;
867
868                                 lnet_inc_lpni_healthv_locked(lpni,
869                                         (sensitivity) ? sensitivity :
870                                         lnet_health_sensitivity);
871                         }
872                         lnet_net_unlock(0);
873                 }
874
875                 /* we can finalize this message */
876                 return -1;
877         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
878         case LNET_MSG_STATUS_LOCAL_DROPPED:
879         case LNET_MSG_STATUS_LOCAL_ABORTED:
880         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
881         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
882                 /*
883                  * don't further decrement the health value if the
884                  * recovery message failed.
885                  */
886                 if (!msg->msg_recovery)
887                         lnet_handle_local_failure(ni);
888                 if (msg->msg_tx_committed)
889                         /* add to the re-send queue */
890                         return lnet_attempt_msg_resend(msg);
891                 break;
892
893         /*
894          * These errors will not trigger a resend so simply
895          * finalize the message
896          */
897         case LNET_MSG_STATUS_LOCAL_ERROR:
898                 /*
899                  * don't further decrement the health value if the
900                  * recovery message failed.
901                  */
902                 if (!msg->msg_recovery)
903                         lnet_handle_local_failure(ni);
904                 return -1;
905
906         /*
907          * TODO: since the remote dropped the message we can
908          * attempt a resend safely.
909          */
910         case LNET_MSG_STATUS_REMOTE_DROPPED:
911                 if (!msg->msg_recovery)
912                         lnet_handle_remote_failure(lpni);
913                 if (msg->msg_tx_committed)
914                         return lnet_attempt_msg_resend(msg);
915                 break;
916
917         case LNET_MSG_STATUS_REMOTE_ERROR:
918         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
919         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
920                 if (!msg->msg_recovery)
921                         lnet_handle_remote_failure(lpni);
922                 return -1;
923         default:
924                 LBUG();
925         }
926
927         /* no resend is needed */
928         return -1;
929 }
930
931 static void
932 lnet_msg_detach_md(struct lnet_msg *msg, int cpt, int status)
933 {
934         struct lnet_libmd *md = msg->msg_md;
935         int unlink;
936
937         /* Now it's safe to drop my caller's ref */
938         md->md_refcount--;
939         LASSERT(md->md_refcount >= 0);
940
941         unlink = lnet_md_unlinkable(md);
942         if (md->md_eq != NULL) {
943                 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
944                         msg->msg_ev.status   = -ETIMEDOUT;
945                         CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
946                 } else {
947                         msg->msg_ev.status   = status;
948                 }
949                 msg->msg_ev.unlinked = unlink;
950                 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
951         }
952
953         if (unlink || (md->md_refcount == 0 &&
954                        md->md_threshold == LNET_MD_THRESH_INF))
955                 lnet_detach_rsp_tracker(md, cpt);
956
957         if (unlink)
958                 lnet_md_unlink(md);
959
960         msg->msg_md = NULL;
961 }
962
963 static bool
964 lnet_is_health_check(struct lnet_msg *msg)
965 {
966         bool hc = true;
967         int status = msg->msg_ev.status;
968
969         if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
970             !msg->msg_onactivelist) {
971                 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
972                        msg);
973                 return false;
974         }
975
976         if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
977             (msg->msg_rx_committed && !msg->msg_rxpeer)) {
978                 /* The optimized GET case does not set msg_rxpeer, but status
979                  * could be zero. Only print the error message if we have a
980                  * non-zero status.
981                  */
982                 if (status)
983                         CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
984                                status);
985                 return false;
986         }
987
988         /* Check for status inconsistencies */
989         if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
990              (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
991                 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
992                        "checking (%d, %d)\n", msg, status,
993                        msg->msg_health_status);
994                 hc = false;
995         }
996
997         CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
998                hc, status, msg->msg_health_status);
999
1000         return hc;
1001 }
1002
1003 char *
1004 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
1005 {
1006         switch (hstatus) {
1007         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
1008                 return "LOCAL_INTERRUPT";
1009         case LNET_MSG_STATUS_LOCAL_DROPPED:
1010                 return "LOCAL_DROPPED";
1011         case LNET_MSG_STATUS_LOCAL_ABORTED:
1012                 return "LOCAL_ABORTED";
1013         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
1014                 return "LOCAL_NO_ROUTE";
1015         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
1016                 return "LOCAL_TIMEOUT";
1017         case LNET_MSG_STATUS_LOCAL_ERROR:
1018                 return "LOCAL_ERROR";
1019         case LNET_MSG_STATUS_REMOTE_DROPPED:
1020                 return "REMOTE_DROPPED";
1021         case LNET_MSG_STATUS_REMOTE_ERROR:
1022                 return "REMOTE_ERROR";
1023         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1024                 return "REMOTE_TIMEOUT";
1025         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1026                 return "NETWORK_TIMEOUT";
1027         case LNET_MSG_STATUS_OK:
1028                 return "OK";
1029         default:
1030                 return "<UNKNOWN>";
1031         }
1032 }
1033
1034 bool
1035 lnet_send_error_simulation(struct lnet_msg *msg,
1036                            enum lnet_msg_hstatus *hstatus)
1037 {
1038         if (!msg)
1039                 return false;
1040
1041         if (list_empty(&the_lnet.ln_drop_rules))
1042             return false;
1043
1044         /* match only health rules */
1045         if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
1046                                   hstatus))
1047                 return false;
1048
1049         CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1050                 libcfs_nid2str(msg->msg_hdr.src_nid),
1051                 libcfs_nid2str(msg->msg_txni->ni_nid),
1052                 libcfs_nid2str(msg->msg_hdr.dest_nid),
1053                 lnet_msgtyp2str(msg->msg_type),
1054                 lnet_health_error2str(*hstatus));
1055
1056         return true;
1057 }
1058 EXPORT_SYMBOL(lnet_send_error_simulation);
1059
1060 void
1061 lnet_finalize(struct lnet_msg *msg, int status)
1062 {
1063         struct lnet_msg_container *container;
1064         int my_slot;
1065         int cpt;
1066         int rc;
1067
1068         LASSERT(!in_interrupt());
1069
1070         if (msg == NULL)
1071                 return;
1072
1073         msg->msg_ev.status = status;
1074
1075         if (lnet_is_health_check(msg)) {
1076                 /*
1077                  * Check the health status of the message. If it has one
1078                  * of the errors that we're supposed to handle, and it has
1079                  * not timed out, then
1080                  *      1. Decrement the appropriate health_value
1081                  *      2. queue the message on the resend queue
1082
1083                  * if the message send is success, timed out or failed in the
1084                  * health check for any reason then we'll just finalize the
1085                  * message. Otherwise just return since the message has been
1086                  * put on the resend queue.
1087                  */
1088                 if (!lnet_health_check(msg))
1089                         return;
1090         }
1091
1092         /*
1093          * We're not going to resend this message so detach its MD and invoke
1094          * the appropriate callbacks
1095          */
1096         if (msg->msg_md != NULL) {
1097                 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
1098                 lnet_res_lock(cpt);
1099                 lnet_msg_detach_md(msg, cpt, status);
1100                 lnet_res_unlock(cpt);
1101         }
1102
1103 again:
1104         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1105                 /* not committed to network yet */
1106                 LASSERT(!msg->msg_onactivelist);
1107                 lnet_msg_free(msg);
1108                 return;
1109         }
1110
1111         /*
1112          * NB: routed message can be committed for both receiving and sending,
1113          * we should finalize in LIFO order and keep counters correct.
1114          * (finalize sending first then finalize receiving)
1115          */
1116         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1117         lnet_net_lock(cpt);
1118
1119         container = the_lnet.ln_msg_containers[cpt];
1120
1121         /* Recursion breaker.  Don't complete the message here if I am (or
1122          * enough other threads are) already completing messages */
1123         my_slot = lnet_check_finalize_recursion_locked(msg,
1124                                                 &container->msc_finalizing,
1125                                                 container->msc_nfinalizers,
1126                                                 container->msc_finalizers);
1127
1128         /* enough threads are resending */
1129         if (my_slot == -1) {
1130                 lnet_net_unlock(cpt);
1131                 return;
1132         }
1133
1134         rc = 0;
1135         while (!list_empty(&container->msc_finalizing)) {
1136                 msg = list_entry(container->msc_finalizing.next,
1137                                  struct lnet_msg, msg_list);
1138
1139                 list_del_init(&msg->msg_list);
1140
1141                 /* NB drops and regains the lnet lock if it actually does
1142                  * anything, so my finalizing friends can chomp along too */
1143                 rc = lnet_complete_msg_locked(msg, cpt);
1144                 if (rc != 0)
1145                         break;
1146         }
1147
1148         if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1149                 lnet_net_unlock(cpt);
1150                 lnet_delay_rule_check();
1151                 lnet_net_lock(cpt);
1152         }
1153
1154         container->msc_finalizers[my_slot] = NULL;
1155         lnet_net_unlock(cpt);
1156
1157         if (rc != 0)
1158                 goto again;
1159 }
1160 EXPORT_SYMBOL(lnet_finalize);
1161
1162 void
1163 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1164 {
1165         int     count = 0;
1166
1167         if (container->msc_init == 0)
1168                 return;
1169
1170         while (!list_empty(&container->msc_active)) {
1171                 struct lnet_msg *msg;
1172
1173                 msg  = list_entry(container->msc_active.next,
1174                                   struct lnet_msg, msg_activelist);
1175                 LASSERT(msg->msg_onactivelist);
1176                 msg->msg_onactivelist = 0;
1177                 list_del_init(&msg->msg_activelist);
1178                 lnet_msg_free(msg);
1179                 count++;
1180         }
1181
1182         if (count > 0)
1183                 CERROR("%d active msg on exit\n", count);
1184
1185         if (container->msc_finalizers != NULL) {
1186                 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1187                                    container->msc_nfinalizers);
1188                 container->msc_finalizers = NULL;
1189         }
1190
1191         if (container->msc_resenders != NULL) {
1192                 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1193                                    container->msc_nfinalizers);
1194                 container->msc_resenders = NULL;
1195         }
1196         container->msc_init = 0;
1197 }
1198
1199 int
1200 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1201 {
1202         int rc = 0;
1203
1204         container->msc_init = 1;
1205
1206         INIT_LIST_HEAD(&container->msc_active);
1207         INIT_LIST_HEAD(&container->msc_finalizing);
1208         INIT_LIST_HEAD(&container->msc_resending);
1209
1210         /* number of CPUs */
1211         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1212         if (container->msc_nfinalizers == 0)
1213                 container->msc_nfinalizers = 1;
1214
1215         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1216                          container->msc_nfinalizers *
1217                          sizeof(*container->msc_finalizers));
1218
1219         if (container->msc_finalizers == NULL) {
1220                 CERROR("Failed to allocate message finalizers\n");
1221                 lnet_msg_container_cleanup(container);
1222                 return -ENOMEM;
1223         }
1224
1225         LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1226                          container->msc_nfinalizers *
1227                          sizeof(*container->msc_resenders));
1228
1229         if (container->msc_resenders == NULL) {
1230                 CERROR("Failed to allocate message resenders\n");
1231                 lnet_msg_container_cleanup(container);
1232                 return -ENOMEM;
1233         }
1234
1235         return rc;
1236 }
1237
1238 void
1239 lnet_msg_containers_destroy(void)
1240 {
1241         struct lnet_msg_container *container;
1242         int     i;
1243
1244         if (the_lnet.ln_msg_containers == NULL)
1245                 return;
1246
1247         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1248                 lnet_msg_container_cleanup(container);
1249
1250         cfs_percpt_free(the_lnet.ln_msg_containers);
1251         the_lnet.ln_msg_containers = NULL;
1252 }
1253
1254 int
1255 lnet_msg_containers_create(void)
1256 {
1257         struct lnet_msg_container *container;
1258         int     rc;
1259         int     i;
1260
1261         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1262                                                       sizeof(*container));
1263
1264         if (the_lnet.ln_msg_containers == NULL) {
1265                 CERROR("Failed to allocate cpu-partition data for network\n");
1266                 return -ENOMEM;
1267         }
1268
1269         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1270                 rc = lnet_msg_container_setup(container, i);
1271                 if (rc != 0) {
1272                         lnet_msg_containers_destroy();
1273                         return rc;
1274                 }
1275         }
1276
1277         return 0;
1278 }