Whamcloud - gitweb
LU-11297 lnet: MR Routing Feature
[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, int sensitivity)
444 {
445         int h = atomic_read(healthv);
446
447         if (h < sensitivity) {
448                 atomic_set(healthv, 0);
449         } else {
450                 h -= sensitivity;
451                 atomic_set(healthv, h);
452         }
453 }
454
455 static void
456 lnet_handle_local_failure(struct lnet_ni *local_ni)
457 {
458         /*
459          * the lnet_net_lock(0) is used to protect the addref on the ni
460          * and the recovery queue.
461          */
462         lnet_net_lock(0);
463         /* the mt could've shutdown and cleaned up the queues */
464         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
465                 lnet_net_unlock(0);
466                 return;
467         }
468
469         lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
470         /*
471          * add the NI to the recovery queue if it's not already there
472          * and it's health value is actually below the maximum. It's
473          * possible that the sensitivity might be set to 0, and the health
474          * value will not be reduced. In this case, there is no reason to
475          * invoke recovery
476          */
477         if (list_empty(&local_ni->ni_recovery) &&
478             atomic_read(&local_ni->ni_healthv) < LNET_MAX_HEALTH_VALUE) {
479                 CERROR("ni %s added to recovery queue. Health = %d\n",
480                         libcfs_nid2str(local_ni->ni_nid),
481                         atomic_read(&local_ni->ni_healthv));
482                 list_add_tail(&local_ni->ni_recovery,
483                               &the_lnet.ln_mt_localNIRecovq);
484                 lnet_ni_addref_locked(local_ni, 0);
485         }
486         lnet_net_unlock(0);
487 }
488
489 void
490 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
491 {
492         __u32 sensitivity = lnet_health_sensitivity;
493         __u32 lp_sensitivity;
494
495         /* lpni could be NULL if we're in the LOLND case */
496         if (!lpni)
497                 return;
498
499         /*
500          * If there is a health sensitivity in the peer then use that
501          * instead of the globally set one.
502          */
503         lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
504         if (lp_sensitivity)
505                 sensitivity = lp_sensitivity;
506
507         lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
508         /*
509          * add the peer NI to the recovery queue if it's not already there
510          * and it's health value is actually below the maximum. It's
511          * possible that the sensitivity might be set to 0, and the health
512          * value will not be reduced. In this case, there is no reason to
513          * invoke recovery
514          */
515         lnet_peer_ni_add_to_recoveryq_locked(lpni);
516 }
517
518 static void
519 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
520 {
521         /* lpni could be NULL if we're in the LOLND case */
522         if (!lpni)
523                 return;
524
525         lnet_net_lock(0);
526         /* the mt could've shutdown and cleaned up the queues */
527         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
528                 lnet_net_unlock(0);
529                 return;
530         }
531         lnet_handle_remote_failure_locked(lpni);
532         lnet_net_unlock(0);
533 }
534
535 static void
536 lnet_incr_hstats(struct lnet_msg *msg, enum lnet_msg_hstatus hstatus)
537 {
538         struct lnet_ni *ni = msg->msg_txni;
539         struct lnet_peer_ni *lpni = msg->msg_txpeer;
540         struct lnet_counters_health *health;
541
542         health = &the_lnet.ln_counters[0]->lct_health;
543
544         switch (hstatus) {
545         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
546                 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
547                 health->lch_local_interrupt_count++;
548                 break;
549         case LNET_MSG_STATUS_LOCAL_DROPPED:
550                 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
551                 health->lch_local_dropped_count++;
552                 break;
553         case LNET_MSG_STATUS_LOCAL_ABORTED:
554                 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
555                 health->lch_local_aborted_count++;
556                 break;
557         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
558                 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
559                 health->lch_local_no_route_count++;
560                 break;
561         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
562                 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
563                 health->lch_local_timeout_count++;
564                 break;
565         case LNET_MSG_STATUS_LOCAL_ERROR:
566                 atomic_inc(&ni->ni_hstats.hlt_local_error);
567                 health->lch_local_error_count++;
568                 break;
569         case LNET_MSG_STATUS_REMOTE_DROPPED:
570                 if (lpni)
571                         atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
572                 health->lch_remote_dropped_count++;
573                 break;
574         case LNET_MSG_STATUS_REMOTE_ERROR:
575                 if (lpni)
576                         atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
577                 health->lch_remote_error_count++;
578                 break;
579         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
580                 if (lpni)
581                         atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
582                 health->lch_remote_timeout_count++;
583                 break;
584         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
585                 if (lpni)
586                         atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
587                 health->lch_network_timeout_count++;
588                 break;
589         case LNET_MSG_STATUS_OK:
590                 break;
591         default:
592                 LBUG();
593         }
594 }
595
596 /*
597  * Do a health check on the message:
598  * return -1 if we're not going to handle the error or
599  *   if we've reached the maximum number of retries.
600  *   success case will return -1 as well
601  * return 0 if it the message is requeued for send
602  */
603 static int
604 lnet_health_check(struct lnet_msg *msg)
605 {
606         enum lnet_msg_hstatus hstatus = msg->msg_health_status;
607         bool lo = false;
608         struct lnet_ni *ni;
609         struct lnet_peer_ni *lpni;
610
611         /* if we're shutting down no point in handling health. */
612         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
613                 return -1;
614
615         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
616
617         /*
618          * if we're sending to the LOLND then the msg_txpeer will not be
619          * set. So no need to sanity check it.
620          */
621         if (msg->msg_tx_committed &&
622             LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) != LOLND)
623                 LASSERT(msg->msg_txpeer);
624         else if (msg->msg_tx_committed &&
625                  LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) == LOLND)
626                 lo = true;
627
628         if (hstatus != LNET_MSG_STATUS_OK &&
629             ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
630                 return -1;
631
632         /*
633          * stats are only incremented for errors so avoid wasting time
634          * incrementing statistics if there is no error.
635          */
636         if (hstatus != LNET_MSG_STATUS_OK) {
637                 lnet_net_lock(0);
638                 lnet_incr_hstats(msg, hstatus);
639                 lnet_net_unlock(0);
640         }
641
642         /*
643          * always prefer txni/txpeer if they message is committed for both
644          * directions.
645          */
646         if (msg->msg_tx_committed) {
647                 ni = msg->msg_txni;
648                 lpni = msg->msg_txpeer;
649         } else {
650                 ni = msg->msg_rxni;
651                 lpni = msg->msg_rxpeer;
652         }
653
654         if (!lo)
655                 LASSERT(ni && lpni);
656         else
657                 LASSERT(ni);
658
659         CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
660                libcfs_nid2str(ni->ni_nid),
661                (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
662                lnet_msgtyp2str(msg->msg_type),
663                lnet_health_error2str(hstatus));
664
665         switch (hstatus) {
666         case LNET_MSG_STATUS_OK:
667                 /*
668                  * increment the local ni health weather we successfully
669                  * received or sent a message on it.
670                  */
671                 lnet_inc_healthv(&ni->ni_healthv);
672                 /*
673                  * It's possible msg_txpeer is NULL in the LOLND
674                  * case. Only increment the peer's health if we're
675                  * receiving a message from it. It's the only sure way to
676                  * know that a remote interface is up.
677                  * If this interface is part of a router, then take that
678                  * as indication that the router is fully healthy.
679                  */
680                 if (lpni && msg->msg_rx_committed) {
681                         /*
682                          * If we're receiving a message from the router or
683                          * I'm a router, then set that lpni's health to
684                          * maximum so we can commence communication
685                          */
686                         if (lnet_isrouter(lpni) || the_lnet.ln_routing)
687                                 lnet_set_healthv(&lpni->lpni_healthv,
688                                                  LNET_MAX_HEALTH_VALUE);
689                         else
690                                 lnet_inc_healthv(&lpni->lpni_healthv);
691                 }
692
693                 /* we can finalize this message */
694                 return -1;
695         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
696         case LNET_MSG_STATUS_LOCAL_DROPPED:
697         case LNET_MSG_STATUS_LOCAL_ABORTED:
698         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
699         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
700                 lnet_handle_local_failure(ni);
701                 if (msg->msg_tx_committed)
702                         /* add to the re-send queue */
703                         goto resend;
704                 break;
705
706         /*
707          * These errors will not trigger a resend so simply
708          * finalize the message
709          */
710         case LNET_MSG_STATUS_LOCAL_ERROR:
711                 lnet_handle_local_failure(ni);
712                 return -1;
713
714         /*
715          * TODO: since the remote dropped the message we can
716          * attempt a resend safely.
717          */
718         case LNET_MSG_STATUS_REMOTE_DROPPED:
719                 lnet_handle_remote_failure(lpni);
720                 if (msg->msg_tx_committed)
721                         goto resend;
722                 break;
723
724         case LNET_MSG_STATUS_REMOTE_ERROR:
725         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
726         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
727                 lnet_handle_remote_failure(lpni);
728                 return -1;
729         default:
730                 LBUG();
731         }
732
733 resend:
734         /* we can only resend tx_committed messages */
735         LASSERT(msg->msg_tx_committed);
736
737         /* don't resend recovery messages */
738         if (msg->msg_recovery) {
739                 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
740                         libcfs_nid2str(msg->msg_from),
741                         libcfs_nid2str(msg->msg_target.nid),
742                         msg->msg_retry_count);
743                 return -1;
744         }
745
746         /*
747          * if we explicitly indicated we don't want to resend then just
748          * return
749          */
750         if (msg->msg_no_resend) {
751                 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
752                         libcfs_nid2str(msg->msg_from),
753                         libcfs_nid2str(msg->msg_target.nid),
754                         msg->msg_retry_count);
755                 return -1;
756         }
757
758         /* check if the message has exceeded the number of retries */
759         if (msg->msg_retry_count >= lnet_retry_count) {
760                 CNETERR("msg %s->%s exceeded retry count %d\n",
761                         libcfs_nid2str(msg->msg_from),
762                         libcfs_nid2str(msg->msg_target.nid),
763                         msg->msg_retry_count);
764                 return -1;
765         }
766         msg->msg_retry_count++;
767
768         lnet_net_lock(msg->msg_tx_cpt);
769
770         /* check again under lock */
771         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
772                 lnet_net_unlock(msg->msg_tx_cpt);
773                 return -1;
774         }
775
776         /*
777          * remove message from the active list and reset it in preparation
778          * for a resend. Two exception to this
779          *
780          * 1. the router case, whe a message is committed for rx when
781          * received, then tx when it is sent. When committed to both tx and
782          * rx we don't want to remove it from the active list.
783          *
784          * 2. The REPLY case since it uses the same msg block for the GET
785          * that was received.
786          */
787         if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
788                 list_del_init(&msg->msg_activelist);
789                 msg->msg_onactivelist = 0;
790         }
791         /*
792          * The msg_target.nid which was originally set
793          * when calling LNetGet() or LNetPut() might've
794          * been overwritten if we're routing this message.
795          * Call lnet_return_tx_credits_locked() to return
796          * the credit this message consumed. The message will
797          * consume another credit when it gets resent.
798          */
799         msg->msg_target.nid = msg->msg_hdr.dest_nid;
800         lnet_msg_decommit_tx(msg, -EAGAIN);
801         msg->msg_sending = 0;
802         msg->msg_receiving = 0;
803         msg->msg_target_is_router = 0;
804
805         CDEBUG(D_NET, "%s->%s:%s:%s - queuing for resend\n",
806                libcfs_nid2str(msg->msg_hdr.src_nid),
807                libcfs_nid2str(msg->msg_hdr.dest_nid),
808                lnet_msgtyp2str(msg->msg_type),
809                lnet_health_error2str(hstatus));
810
811         list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
812         lnet_net_unlock(msg->msg_tx_cpt);
813
814         wake_up(&the_lnet.ln_mt_waitq);
815         return 0;
816 }
817
818 static void
819 lnet_msg_detach_md(struct lnet_msg *msg, int cpt, int status)
820 {
821         struct lnet_libmd *md = msg->msg_md;
822         int unlink;
823
824         /* Now it's safe to drop my caller's ref */
825         md->md_refcount--;
826         LASSERT(md->md_refcount >= 0);
827
828         unlink = lnet_md_unlinkable(md);
829         if (md->md_eq != NULL) {
830                 msg->msg_ev.status   = status;
831                 msg->msg_ev.unlinked = unlink;
832                 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
833         }
834
835         if (unlink) {
836                 lnet_detach_rsp_tracker(md, cpt);
837                 lnet_md_unlink(md);
838         }
839
840         msg->msg_md = NULL;
841 }
842
843 static bool
844 lnet_is_health_check(struct lnet_msg *msg)
845 {
846         bool hc = true;
847         int status = msg->msg_ev.status;
848
849         if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
850             !msg->msg_onactivelist) {
851                 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
852                        msg);
853                 return false;
854         }
855
856         if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
857             (msg->msg_rx_committed && !msg->msg_rxpeer)) {
858                 CDEBUG(D_NET, "msg %p failed too early to retry and send\n",
859                        msg);
860                 return false;
861         }
862
863         /* Check for status inconsistencies */
864         if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
865              (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
866                 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
867                        "checking (%d, %d)\n", msg, status,
868                        msg->msg_health_status);
869                 hc = false;
870         }
871
872         CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
873                hc, status, msg->msg_health_status);
874
875         return hc;
876 }
877
878 char *
879 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
880 {
881         switch (hstatus) {
882         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
883                 return "LOCAL_INTERRUPT";
884         case LNET_MSG_STATUS_LOCAL_DROPPED:
885                 return "LOCAL_DROPPED";
886         case LNET_MSG_STATUS_LOCAL_ABORTED:
887                 return "LOCAL_ABORTED";
888         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
889                 return "LOCAL_NO_ROUTE";
890         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
891                 return "LOCAL_TIMEOUT";
892         case LNET_MSG_STATUS_LOCAL_ERROR:
893                 return "LOCAL_ERROR";
894         case LNET_MSG_STATUS_REMOTE_DROPPED:
895                 return "REMOTE_DROPPED";
896         case LNET_MSG_STATUS_REMOTE_ERROR:
897                 return "REMOTE_ERROR";
898         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
899                 return "REMOTE_TIMEOUT";
900         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
901                 return "NETWORK_TIMEOUT";
902         case LNET_MSG_STATUS_OK:
903                 return "OK";
904         default:
905                 return "<UNKNOWN>";
906         }
907 }
908
909 bool
910 lnet_send_error_simulation(struct lnet_msg *msg,
911                            enum lnet_msg_hstatus *hstatus)
912 {
913         if (!msg)
914                 return false;
915
916         if (list_empty(&the_lnet.ln_drop_rules))
917             return false;
918
919         /* match only health rules */
920         if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
921                                   hstatus))
922                 return false;
923
924         CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
925                 libcfs_nid2str(msg->msg_hdr.src_nid),
926                 libcfs_nid2str(msg->msg_txni->ni_nid),
927                 libcfs_nid2str(msg->msg_hdr.dest_nid),
928                 lnet_msgtyp2str(msg->msg_type),
929                 lnet_health_error2str(*hstatus));
930
931         return true;
932 }
933 EXPORT_SYMBOL(lnet_send_error_simulation);
934
935 void
936 lnet_finalize(struct lnet_msg *msg, int status)
937 {
938         struct lnet_msg_container *container;
939         int my_slot;
940         int cpt;
941         int rc;
942         int i;
943
944         LASSERT(!in_interrupt());
945
946         if (msg == NULL)
947                 return;
948
949         msg->msg_ev.status = status;
950
951         if (lnet_is_health_check(msg)) {
952                 /*
953                  * Check the health status of the message. If it has one
954                  * of the errors that we're supposed to handle, and it has
955                  * not timed out, then
956                  *      1. Decrement the appropriate health_value
957                  *      2. queue the message on the resend queue
958
959                  * if the message send is success, timed out or failed in the
960                  * health check for any reason then we'll just finalize the
961                  * message. Otherwise just return since the message has been
962                  * put on the resend queue.
963                  */
964                 if (!lnet_health_check(msg))
965                         /* Message is queued for resend */
966                         return;
967         }
968
969         /*
970          * We're not going to resend this message so detach its MD and invoke
971          * the appropriate callbacks
972          */
973         if (msg->msg_md != NULL) {
974                 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
975                 lnet_res_lock(cpt);
976                 lnet_msg_detach_md(msg, cpt, status);
977                 lnet_res_unlock(cpt);
978         }
979
980 again:
981         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
982                 /* not committed to network yet */
983                 LASSERT(!msg->msg_onactivelist);
984                 lnet_msg_free(msg);
985                 return;
986         }
987
988         /*
989          * NB: routed message can be committed for both receiving and sending,
990          * we should finalize in LIFO order and keep counters correct.
991          * (finalize sending first then finalize receiving)
992          */
993         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
994         lnet_net_lock(cpt);
995
996         container = the_lnet.ln_msg_containers[cpt];
997         list_add_tail(&msg->msg_list, &container->msc_finalizing);
998
999         /* Recursion breaker.  Don't complete the message here if I am (or
1000          * enough other threads are) already completing messages */
1001
1002         my_slot = -1;
1003         for (i = 0; i < container->msc_nfinalizers; i++) {
1004                 if (container->msc_finalizers[i] == current)
1005                         break;
1006
1007                 if (my_slot < 0 && container->msc_finalizers[i] == NULL)
1008                         my_slot = i;
1009         }
1010
1011         if (i < container->msc_nfinalizers || my_slot < 0) {
1012                 lnet_net_unlock(cpt);
1013                 return;
1014         }
1015
1016         container->msc_finalizers[my_slot] = current;
1017
1018         rc = 0;
1019         while (!list_empty(&container->msc_finalizing)) {
1020                 msg = list_entry(container->msc_finalizing.next,
1021                                  struct lnet_msg, msg_list);
1022
1023                 list_del_init(&msg->msg_list);
1024
1025                 /* NB drops and regains the lnet lock if it actually does
1026                  * anything, so my finalizing friends can chomp along too */
1027                 rc = lnet_complete_msg_locked(msg, cpt);
1028                 if (rc != 0)
1029                         break;
1030         }
1031
1032         if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1033                 lnet_net_unlock(cpt);
1034                 lnet_delay_rule_check();
1035                 lnet_net_lock(cpt);
1036         }
1037
1038         container->msc_finalizers[my_slot] = NULL;
1039         lnet_net_unlock(cpt);
1040
1041         if (rc != 0)
1042                 goto again;
1043 }
1044 EXPORT_SYMBOL(lnet_finalize);
1045
1046 void
1047 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1048 {
1049         int     count = 0;
1050
1051         if (container->msc_init == 0)
1052                 return;
1053
1054         while (!list_empty(&container->msc_active)) {
1055                 struct lnet_msg *msg;
1056
1057                 msg  = list_entry(container->msc_active.next,
1058                                   struct lnet_msg, msg_activelist);
1059                 LASSERT(msg->msg_onactivelist);
1060                 msg->msg_onactivelist = 0;
1061                 list_del_init(&msg->msg_activelist);
1062                 lnet_msg_free(msg);
1063                 count++;
1064         }
1065
1066         if (count > 0)
1067                 CERROR("%d active msg on exit\n", count);
1068
1069         if (container->msc_finalizers != NULL) {
1070                 LIBCFS_FREE(container->msc_finalizers,
1071                             container->msc_nfinalizers *
1072                             sizeof(*container->msc_finalizers));
1073                 container->msc_finalizers = NULL;
1074         }
1075         container->msc_init = 0;
1076 }
1077
1078 int
1079 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1080 {
1081         int rc = 0;
1082
1083         container->msc_init = 1;
1084
1085         INIT_LIST_HEAD(&container->msc_active);
1086         INIT_LIST_HEAD(&container->msc_finalizing);
1087
1088         /* number of CPUs */
1089         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1090         if (container->msc_nfinalizers == 0)
1091                 container->msc_nfinalizers = 1;
1092
1093         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1094                          container->msc_nfinalizers *
1095                          sizeof(*container->msc_finalizers));
1096
1097         if (container->msc_finalizers == NULL) {
1098                 CERROR("Failed to allocate message finalizers\n");
1099                 lnet_msg_container_cleanup(container);
1100                 return -ENOMEM;
1101         }
1102
1103         return rc;
1104 }
1105
1106 void
1107 lnet_msg_containers_destroy(void)
1108 {
1109         struct lnet_msg_container *container;
1110         int     i;
1111
1112         if (the_lnet.ln_msg_containers == NULL)
1113                 return;
1114
1115         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1116                 lnet_msg_container_cleanup(container);
1117
1118         cfs_percpt_free(the_lnet.ln_msg_containers);
1119         the_lnet.ln_msg_containers = NULL;
1120 }
1121
1122 int
1123 lnet_msg_containers_create(void)
1124 {
1125         struct lnet_msg_container *container;
1126         int     rc;
1127         int     i;
1128
1129         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1130                                                       sizeof(*container));
1131
1132         if (the_lnet.ln_msg_containers == NULL) {
1133                 CERROR("Failed to allocate cpu-partition data for network\n");
1134                 return -ENOMEM;
1135         }
1136
1137         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1138                 rc = lnet_msg_container_setup(container, i);
1139                 if (rc != 0) {
1140                         lnet_msg_containers_destroy();
1141                         return rc;
1142                 }
1143         }
1144
1145         return 0;
1146 }