4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Message decoding, parsing and finalizing routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <lnet/lib-lnet.h>
42 lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev)
46 memset(ev, 0, sizeof(*ev));
50 ev->type = LNET_EVENT_UNLINK;
51 lnet_md_deconstruct(md, ev);
52 lnet_md2handle(&ev->md_handle, md);
57 * Don't need any lock, must be called after lnet_commit_md
60 lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type)
62 struct lnet_hdr *hdr = &msg->msg_hdr;
63 struct lnet_event *ev = &msg->msg_ev;
65 LASSERT(!msg->msg_routing);
68 ev->msg_type = msg->msg_type;
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;
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;
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;
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;
111 case LNET_EVENT_ACK: /* ACK */
112 ev->match_bits = hdr->msg.ack.match_bits;
113 ev->mlength = hdr->msg.ack.mlength;
116 case LNET_EVENT_REPLY: /* REPLY */
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);
125 ev->rlength = le32_to_cpu(hdr->payload_length);
126 ev->hdr_data = le64_to_cpu(hdr->msg.put.hdr_data);
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);
133 ev->rlength = le32_to_cpu(hdr->msg.get.sink_length);
134 ev->offset = le32_to_cpu(hdr->msg.get.src_offset);
142 lnet_msg_commit(struct lnet_msg *msg, int cpt)
144 struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
145 struct lnet_counters_common *common;
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);
152 /* routed message can be committed for both receiving and sending */
153 LASSERT(!msg->msg_tx_committed);
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);
164 LASSERT(!msg->msg_sending);
165 msg->msg_rx_cpt = cpt;
166 msg->msg_rx_committed = 1;
169 LASSERT(!msg->msg_onactivelist);
171 msg->msg_onactivelist = 1;
172 list_add_tail(&msg->msg_activelist, &container->msc_active);
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;
181 lnet_msg_decommit_tx(struct lnet_msg *msg, int status)
183 struct lnet_counters_common *common;
184 struct lnet_event *ev = &msg->msg_ev;
186 LASSERT(msg->msg_tx_committed);
190 common = &(the_lnet.ln_counters[msg->msg_tx_cpt]->lct_common);
192 default: /* routed message */
193 LASSERT(msg->msg_routing);
194 LASSERT(msg->msg_rx_committed);
195 LASSERT(ev->type == 0);
197 common->lcc_route_length += msg->msg_len;
198 common->lcc_route_count++;
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 */
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;
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 */
224 common->lcc_send_count++;
228 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
230 LNET_STATS_TYPE_SEND);
232 lnet_incr_stats(&msg->msg_txni->ni_stats,
234 LNET_STATS_TYPE_SEND);
236 lnet_return_tx_credits_locked(msg);
237 msg->msg_tx_committed = 0;
241 lnet_msg_decommit_rx(struct lnet_msg *msg, int status)
243 struct lnet_counters_common *common;
244 struct lnet_event *ev = &msg->msg_ev;
246 LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
247 LASSERT(msg->msg_rx_committed);
252 common = &(the_lnet.ln_counters[msg->msg_rx_cpt]->lct_common);
255 LASSERT(ev->type == 0);
256 LASSERT(msg->msg_routing);
260 LASSERT(msg->msg_type == LNET_MSG_ACK);
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;
274 LASSERT(msg->msg_type == LNET_MSG_PUT);
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);
285 common->lcc_recv_count++;
289 lnet_incr_stats(&msg->msg_rxpeer->lpni_stats,
291 LNET_STATS_TYPE_RECV);
293 lnet_incr_stats(&msg->msg_rxni->ni_stats,
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;
300 lnet_return_rx_credits_locked(msg);
301 msg->msg_rx_committed = 0;
305 lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status)
309 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
310 LASSERT(msg->msg_onactivelist);
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);
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;
324 lnet_msg_decommit_rx(msg, status);
327 list_del(&msg->msg_activelist);
328 msg->msg_onactivelist = 0;
330 the_lnet.ln_counters[cpt2]->lct_common.lcc_msgs_alloc--;
333 lnet_net_unlock(cpt2);
339 lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
340 unsigned int offset, unsigned int mlen)
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);
350 if (msg->msg_receiving) { /* committed for receiving */
351 msg->msg_offset = offset;
352 msg->msg_wanted = mlen;
356 if (md->md_threshold != LNET_MD_THRESH_INF) {
357 LASSERT(md->md_threshold > 0);
361 /* build umd in event */
362 lnet_md2handle(&msg->msg_ev.md_handle, md);
363 lnet_md_deconstruct(md, &msg->msg_ev);
367 lnet_complete_msg_locked(struct lnet_msg *msg, int cpt)
369 struct lnet_handle_wire ack_wmd;
371 int status = msg->msg_ev.status;
373 LASSERT(msg->msg_onactivelist);
375 if (status == 0 && msg->msg_ack) {
376 /* Only send an ACK if the PUT completed successfully */
378 lnet_msg_decommit(msg, cpt, 0);
381 lnet_net_unlock(cpt);
383 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
384 LASSERT(!msg->msg_routing);
386 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
388 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.source, 0, 0);
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);
394 rc = lnet_send(msg->msg_ev.target.nid, msg, msg->msg_from);
398 * NB: message is committed for sending, we should return
399 * on success because LND will finalize this message later.
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.
410 } else if (status == 0 && /* OK so far */
411 (msg->msg_routing && !msg->msg_sending)) {
413 LASSERT(!msg->msg_receiving); /* called back recv already */
414 lnet_net_unlock(cpt);
416 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
420 * NB: message is committed for sending, we should return
421 * on success because LND will finalize this message later.
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.
435 lnet_msg_decommit(msg, cpt, status);
441 lnet_dec_healthv_locked(atomic_t *healthv, int sensitivity)
443 int h = atomic_read(healthv);
445 if (h < sensitivity) {
446 atomic_set(healthv, 0);
449 atomic_set(healthv, h);
454 lnet_handle_local_failure(struct lnet_ni *local_ni)
457 * the lnet_net_lock(0) is used to protect the addref on the ni
458 * and the recovery queue.
461 /* the mt could've shutdown and cleaned up the queues */
462 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
467 lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
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
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);
488 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
490 __u32 sensitivity = lnet_health_sensitivity;
491 __u32 lp_sensitivity;
495 * 1. lpni could be NULL if we're in the LOLND case
496 * 2. this is a recovery message
502 * If there is a health sensitivity in the peer then use that
503 * instead of the globally set one.
505 lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
507 sensitivity = lp_sensitivity;
509 lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
511 /* update the peer_net's health value */
512 lnet_update_peer_net_healthv(lpni);
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
521 lnet_peer_ni_add_to_recoveryq_locked(lpni);
525 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
527 /* lpni could be NULL if we're in the LOLND case */
532 /* the mt could've shutdown and cleaned up the queues */
533 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
537 lnet_handle_remote_failure_locked(lpni);
542 lnet_incr_hstats(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
543 enum lnet_msg_hstatus hstatus)
545 struct lnet_counters_health *health;
547 health = &the_lnet.ln_counters[0]->lct_health;
550 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
551 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
552 health->lch_local_interrupt_count++;
554 case LNET_MSG_STATUS_LOCAL_DROPPED:
555 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
556 health->lch_local_dropped_count++;
558 case LNET_MSG_STATUS_LOCAL_ABORTED:
559 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
560 health->lch_local_aborted_count++;
562 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
563 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
564 health->lch_local_no_route_count++;
566 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
567 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
568 health->lch_local_timeout_count++;
570 case LNET_MSG_STATUS_LOCAL_ERROR:
571 atomic_inc(&ni->ni_hstats.hlt_local_error);
572 health->lch_local_error_count++;
574 case LNET_MSG_STATUS_REMOTE_DROPPED:
576 atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
577 health->lch_remote_dropped_count++;
579 case LNET_MSG_STATUS_REMOTE_ERROR:
581 atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
582 health->lch_remote_error_count++;
584 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
586 atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
587 health->lch_remote_timeout_count++;
589 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
591 atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
592 health->lch_network_timeout_count++;
594 case LNET_MSG_STATUS_OK:
602 lnet_resend_msg_locked(struct lnet_msg *msg)
604 msg->msg_retry_count++;
607 * remove message from the active list and reset it to prepare
608 * for a resend. Two exceptions to this
610 * 1. the router case. When a message is being routed it is
611 * committed for rx when received and committed for tx when
612 * forwarded. We don't want to remove it from the active list, since
613 * code which handles receiving expects it to remain on the active
616 * 2. The REPLY case. Reply messages use the same message
617 * structure for the GET that was received.
619 if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
620 list_del_init(&msg->msg_activelist);
621 msg->msg_onactivelist = 0;
624 * The msg_target.nid which was originally set
625 * when calling LNetGet() or LNetPut() might've
626 * been overwritten if we're routing this message.
627 * Call lnet_msg_decommit_tx() to return the credit
628 * this message consumed. The message will
629 * consume another credit when it gets resent.
631 msg->msg_target.nid = msg->msg_hdr.dest_nid;
632 lnet_msg_decommit_tx(msg, -EAGAIN);
633 msg->msg_sending = 0;
634 msg->msg_receiving = 0;
635 msg->msg_target_is_router = 0;
637 CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
638 libcfs_nid2str(msg->msg_hdr.src_nid),
639 libcfs_nid2str(msg->msg_hdr.dest_nid),
640 lnet_msgtyp2str(msg->msg_type),
641 lnet_health_error2str(msg->msg_health_status), msg);
643 list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
645 complete(&the_lnet.ln_mt_wait_complete);
649 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
650 struct list_head *containerq,
651 int nworkers, void **workers)
656 list_add_tail(&msg->msg_list, containerq);
658 for (i = 0; i < nworkers; i++) {
659 if (workers[i] == current)
662 if (my_slot < 0 && workers[i] == NULL)
666 if (i < nworkers || my_slot < 0)
669 workers[my_slot] = current;
675 lnet_attempt_msg_resend(struct lnet_msg *msg)
677 struct lnet_msg_container *container;
681 /* we can only resend tx_committed messages */
682 LASSERT(msg->msg_tx_committed);
684 /* don't resend recovery messages */
685 if (msg->msg_recovery) {
686 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
687 libcfs_nid2str(msg->msg_from),
688 libcfs_nid2str(msg->msg_target.nid),
689 msg->msg_retry_count);
690 return -ENOTRECOVERABLE;
694 * if we explicitly indicated we don't want to resend then just
697 if (msg->msg_no_resend) {
698 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
699 libcfs_nid2str(msg->msg_from),
700 libcfs_nid2str(msg->msg_target.nid),
701 msg->msg_retry_count);
702 return -ENOTRECOVERABLE;
705 /* check if the message has exceeded the number of retries */
706 if (msg->msg_retry_count >= lnet_retry_count) {
707 CNETERR("msg %s->%s exceeded retry count %d\n",
708 libcfs_nid2str(msg->msg_from),
709 libcfs_nid2str(msg->msg_target.nid),
710 msg->msg_retry_count);
711 return -ENOTRECOVERABLE;
714 cpt = msg->msg_tx_cpt;
717 /* check again under lock */
718 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
719 lnet_net_unlock(cpt);
723 container = the_lnet.ln_msg_containers[cpt];
725 lnet_check_finalize_recursion_locked(msg,
726 &container->msc_resending,
727 container->msc_nfinalizers,
728 container->msc_resenders);
730 /* enough threads are resending */
732 lnet_net_unlock(cpt);
736 while (!list_empty(&container->msc_resending)) {
737 msg = list_entry(container->msc_resending.next,
738 struct lnet_msg, msg_list);
739 list_del(&msg->msg_list);
742 * resending the message will require us to call
743 * lnet_msg_decommit_tx() which will return the credit
744 * which this message holds. This could trigger another
745 * queued message to be sent. If that message fails and
746 * requires a resend we will recurse.
747 * But since at this point the slot is taken, the message
748 * will be queued in the container and dealt with
749 * later. This breaks the recursion.
751 lnet_resend_msg_locked(msg);
755 * msc_resenders is an array of process pointers. Each entry holds
756 * a pointer to the current process operating on the message. An
757 * array entry is created per CPT. If the array slot is already
758 * set, then it means that there is a thread on the CPT currently
759 * resending a message.
760 * Once the thread finishes clear the slot to enable the thread to
761 * take on more resend work.
763 container->msc_resenders[my_slot] = NULL;
764 lnet_net_unlock(cpt);
770 * Do a health check on the message:
771 * return -1 if we're not going to handle the error or
772 * if we've reached the maximum number of retries.
773 * success case will return -1 as well
774 * return 0 if it the message is requeued for send
777 lnet_health_check(struct lnet_msg *msg)
779 enum lnet_msg_hstatus hstatus = msg->msg_health_status;
780 struct lnet_peer_ni *lpni;
783 bool attempt_local_resend;
784 bool attempt_remote_resend;
785 bool handle_local_health;
786 bool handle_remote_health;
788 /* if we're shutting down no point in handling health. */
789 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
792 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
795 * if we're sending to the LOLND then the msg_txpeer will not be
796 * set. So no need to sanity check it.
798 if (msg->msg_tx_committed && msg->msg_txni->ni_nid != LNET_NID_LO_0)
799 LASSERT(msg->msg_txpeer);
800 else if (msg->msg_tx_committed &&
801 msg->msg_txni->ni_nid == LNET_NID_LO_0)
804 if (hstatus != LNET_MSG_STATUS_OK &&
805 ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
809 * always prefer txni/txpeer if they message is committed for both
812 if (msg->msg_tx_committed) {
814 lpni = msg->msg_txpeer;
815 attempt_local_resend = attempt_remote_resend = true;
818 lpni = msg->msg_rxpeer;
819 attempt_local_resend = attempt_remote_resend = false;
822 /* Don't further decrement the health value if a recovery message
825 if (msg->msg_recovery)
826 handle_local_health = handle_remote_health = false;
828 handle_local_health = handle_remote_health = true;
830 /* For local failures, health/recovery/resends are not needed if I only
831 * have a single (non-lolnd) interface. NB: pb_nnis includes the lolnd
832 * interface, so a single-rail node would have pb_nnis == 2.
834 if (the_lnet.ln_ping_target->pb_nnis <= 2) {
835 handle_local_health = false;
836 attempt_local_resend = false;
839 /* For remote failures, health/recovery/resends are not needed if the
840 * peer only has a single interface. Special case for routers where we
841 * rely on health feature to manage route aliveness. NB: unlike pb_nnis
842 * above, lp_nnis does _not_ include the lolnd, so a single-rail node
843 * would have lp_nnis == 1.
845 if (lpni && lpni->lpni_peer_net->lpn_peer->lp_nnis <= 1) {
846 attempt_remote_resend = false;
847 if (!lnet_isrouter(lpni))
848 handle_remote_health = false;
856 CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
857 libcfs_nid2str(ni->ni_nid),
858 (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
859 lnet_msgtyp2str(msg->msg_type),
860 lnet_health_error2str(hstatus));
863 * stats are only incremented for errors so avoid wasting time
864 * incrementing statistics if there is no error.
866 if (hstatus != LNET_MSG_STATUS_OK) {
868 lnet_incr_hstats(ni, lpni, hstatus);
873 case LNET_MSG_STATUS_OK:
875 * increment the local ni health weather we successfully
876 * received or sent a message on it.
878 lnet_inc_healthv(&ni->ni_healthv, lnet_health_sensitivity);
880 * It's possible msg_txpeer is NULL in the LOLND
881 * case. Only increment the peer's health if we're
882 * receiving a message from it. It's the only sure way to
883 * know that a remote interface is up.
884 * If this interface is part of a router, then take that
885 * as indication that the router is fully healthy.
887 if (lpni && msg->msg_rx_committed) {
889 * If we're receiving a message from the router or
890 * I'm a router, then set that lpni's health to
891 * maximum so we can commence communication
894 if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
895 lnet_set_lpni_healthv_locked(lpni,
896 LNET_MAX_HEALTH_VALUE);
898 __u32 sensitivity = lpni->lpni_peer_net->
899 lpn_peer->lp_health_sensitivity;
901 lnet_inc_lpni_healthv_locked(lpni,
902 (sensitivity) ? sensitivity :
903 lnet_health_sensitivity);
908 /* we can finalize this message */
910 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
911 case LNET_MSG_STATUS_LOCAL_DROPPED:
912 case LNET_MSG_STATUS_LOCAL_ABORTED:
913 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
914 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
915 if (handle_local_health)
916 lnet_handle_local_failure(ni);
917 if (attempt_local_resend)
918 return lnet_attempt_msg_resend(msg);
920 case LNET_MSG_STATUS_LOCAL_ERROR:
921 if (handle_local_health)
922 lnet_handle_local_failure(ni);
924 case LNET_MSG_STATUS_REMOTE_DROPPED:
925 if (handle_remote_health)
926 lnet_handle_remote_failure(lpni);
927 if (attempt_remote_resend)
928 return lnet_attempt_msg_resend(msg);
930 case LNET_MSG_STATUS_REMOTE_ERROR:
931 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
932 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
933 if (handle_remote_health)
934 lnet_handle_remote_failure(lpni);
940 /* no resend is needed */
945 lnet_msg_detach_md(struct lnet_msg *msg, int status)
947 struct lnet_libmd *md = msg->msg_md;
948 lnet_handler_t handler = NULL;
949 int cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
953 while (md->md_flags & LNET_MD_FLAG_HANDLING)
954 /* An event handler is running - wait for it to
955 * complete to avoid races.
957 lnet_md_wait_handling(md, cpt);
959 /* Now it's safe to drop my caller's ref */
961 LASSERT(md->md_refcount >= 0);
963 unlink = lnet_md_unlinkable(md);
964 if (md->md_handler) {
965 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
966 msg->msg_ev.status = -ETIMEDOUT;
967 CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
969 msg->msg_ev.status = status;
971 msg->msg_ev.unlinked = unlink;
972 handler = md->md_handler;
974 md->md_flags |= LNET_MD_FLAG_HANDLING;
977 if (unlink || (md->md_refcount == 0 &&
978 md->md_threshold == LNET_MD_THRESH_INF))
979 lnet_detach_rsp_tracker(md, cpt);
985 lnet_res_unlock(cpt);
988 handler(&msg->msg_ev);
991 md->md_flags &= ~LNET_MD_FLAG_HANDLING;
993 lnet_res_unlock(cpt);
999 lnet_is_health_check(struct lnet_msg *msg)
1002 int status = msg->msg_ev.status;
1004 if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
1005 !msg->msg_onactivelist) {
1006 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
1011 if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
1012 (msg->msg_rx_committed && !msg->msg_rxpeer)) {
1013 /* The optimized GET case does not set msg_rxpeer, but status
1014 * could be zero. Only print the error message if we have a
1018 CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
1023 /* Check for status inconsistencies */
1024 if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
1025 (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
1026 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
1027 "checking (%d, %d)\n", msg, status,
1028 msg->msg_health_status);
1032 CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
1033 hc, status, msg->msg_health_status);
1039 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
1042 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
1043 return "LOCAL_INTERRUPT";
1044 case LNET_MSG_STATUS_LOCAL_DROPPED:
1045 return "LOCAL_DROPPED";
1046 case LNET_MSG_STATUS_LOCAL_ABORTED:
1047 return "LOCAL_ABORTED";
1048 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
1049 return "LOCAL_NO_ROUTE";
1050 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
1051 return "LOCAL_TIMEOUT";
1052 case LNET_MSG_STATUS_LOCAL_ERROR:
1053 return "LOCAL_ERROR";
1054 case LNET_MSG_STATUS_REMOTE_DROPPED:
1055 return "REMOTE_DROPPED";
1056 case LNET_MSG_STATUS_REMOTE_ERROR:
1057 return "REMOTE_ERROR";
1058 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1059 return "REMOTE_TIMEOUT";
1060 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1061 return "NETWORK_TIMEOUT";
1062 case LNET_MSG_STATUS_OK:
1070 lnet_send_error_simulation(struct lnet_msg *msg,
1071 enum lnet_msg_hstatus *hstatus)
1076 if (list_empty(&the_lnet.ln_drop_rules))
1079 /* match only health rules */
1080 if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
1084 CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1085 libcfs_nid2str(msg->msg_hdr.src_nid),
1086 libcfs_nid2str(msg->msg_txni->ni_nid),
1087 libcfs_nid2str(msg->msg_hdr.dest_nid),
1088 lnet_msgtyp2str(msg->msg_type),
1089 lnet_health_error2str(*hstatus));
1093 EXPORT_SYMBOL(lnet_send_error_simulation);
1096 lnet_finalize(struct lnet_msg *msg, int status)
1098 struct lnet_msg_container *container;
1103 LASSERT(!in_interrupt());
1108 msg->msg_ev.status = status;
1110 if (lnet_is_health_check(msg)) {
1112 * Check the health status of the message. If it has one
1113 * of the errors that we're supposed to handle, and it has
1114 * not timed out, then
1115 * 1. Decrement the appropriate health_value
1116 * 2. queue the message on the resend queue
1118 * if the message send is success, timed out or failed in the
1119 * health check for any reason then we'll just finalize the
1120 * message. Otherwise just return since the message has been
1121 * put on the resend queue.
1123 if (!lnet_health_check(msg))
1128 * We're not going to resend this message so detach its MD and invoke
1129 * the appropriate callbacks
1131 if (msg->msg_md != NULL)
1132 lnet_msg_detach_md(msg, status);
1135 if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1136 /* not committed to network yet */
1137 LASSERT(!msg->msg_onactivelist);
1143 * NB: routed message can be committed for both receiving and sending,
1144 * we should finalize in LIFO order and keep counters correct.
1145 * (finalize sending first then finalize receiving)
1147 cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1150 container = the_lnet.ln_msg_containers[cpt];
1152 /* Recursion breaker. Don't complete the message here if I am (or
1153 * enough other threads are) already completing messages */
1154 my_slot = lnet_check_finalize_recursion_locked(msg,
1155 &container->msc_finalizing,
1156 container->msc_nfinalizers,
1157 container->msc_finalizers);
1159 /* enough threads are resending */
1160 if (my_slot == -1) {
1161 lnet_net_unlock(cpt);
1166 while (!list_empty(&container->msc_finalizing)) {
1167 msg = list_entry(container->msc_finalizing.next,
1168 struct lnet_msg, msg_list);
1170 list_del_init(&msg->msg_list);
1172 /* NB drops and regains the lnet lock if it actually does
1173 * anything, so my finalizing friends can chomp along too */
1174 rc = lnet_complete_msg_locked(msg, cpt);
1179 if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1180 lnet_net_unlock(cpt);
1181 lnet_delay_rule_check();
1185 container->msc_finalizers[my_slot] = NULL;
1186 lnet_net_unlock(cpt);
1191 EXPORT_SYMBOL(lnet_finalize);
1194 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1198 if (container->msc_init == 0)
1201 while (!list_empty(&container->msc_active)) {
1202 struct lnet_msg *msg;
1204 msg = list_entry(container->msc_active.next,
1205 struct lnet_msg, msg_activelist);
1206 LASSERT(msg->msg_onactivelist);
1207 msg->msg_onactivelist = 0;
1208 list_del_init(&msg->msg_activelist);
1214 CERROR("%d active msg on exit\n", count);
1216 if (container->msc_finalizers != NULL) {
1217 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1218 container->msc_nfinalizers);
1219 container->msc_finalizers = NULL;
1222 if (container->msc_resenders != NULL) {
1223 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1224 container->msc_nfinalizers);
1225 container->msc_resenders = NULL;
1227 container->msc_init = 0;
1231 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1235 container->msc_init = 1;
1237 INIT_LIST_HEAD(&container->msc_active);
1238 INIT_LIST_HEAD(&container->msc_finalizing);
1239 INIT_LIST_HEAD(&container->msc_resending);
1241 /* number of CPUs */
1242 container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1243 if (container->msc_nfinalizers == 0)
1244 container->msc_nfinalizers = 1;
1246 LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1247 container->msc_nfinalizers *
1248 sizeof(*container->msc_finalizers));
1250 if (container->msc_finalizers == NULL) {
1251 CERROR("Failed to allocate message finalizers\n");
1252 lnet_msg_container_cleanup(container);
1256 LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1257 container->msc_nfinalizers *
1258 sizeof(*container->msc_resenders));
1260 if (container->msc_resenders == NULL) {
1261 CERROR("Failed to allocate message resenders\n");
1262 lnet_msg_container_cleanup(container);
1270 lnet_msg_containers_destroy(void)
1272 struct lnet_msg_container *container;
1275 if (the_lnet.ln_msg_containers == NULL)
1278 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1279 lnet_msg_container_cleanup(container);
1281 cfs_percpt_free(the_lnet.ln_msg_containers);
1282 the_lnet.ln_msg_containers = NULL;
1286 lnet_msg_containers_create(void)
1288 struct lnet_msg_container *container;
1292 the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1293 sizeof(*container));
1295 if (the_lnet.ln_msg_containers == NULL) {
1296 CERROR("Failed to allocate cpu-partition data for network\n");
1300 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1301 rc = lnet_msg_container_setup(container, i);
1303 lnet_msg_containers_destroy();