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->md);
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.md);
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;
493 /* lpni could be NULL if we're in the LOLND case */
498 * If there is a health sensitivity in the peer then use that
499 * instead of the globally set one.
501 lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
503 sensitivity = lp_sensitivity;
505 lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
507 /* update the peer_net's health value */
508 lnet_update_peer_net_healthv(lpni);
511 * add the peer NI to the recovery queue if it's not already there
512 * and it's health value is actually below the maximum. It's
513 * possible that the sensitivity might be set to 0, and the health
514 * value will not be reduced. In this case, there is no reason to
517 lnet_peer_ni_add_to_recoveryq_locked(lpni);
521 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
523 /* lpni could be NULL if we're in the LOLND case */
528 /* the mt could've shutdown and cleaned up the queues */
529 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
533 lnet_handle_remote_failure_locked(lpni);
538 lnet_incr_hstats(struct lnet_msg *msg, enum lnet_msg_hstatus hstatus)
540 struct lnet_ni *ni = msg->msg_txni;
541 struct lnet_peer_ni *lpni = msg->msg_txpeer;
542 struct lnet_counters_health *health;
544 health = &the_lnet.ln_counters[0]->lct_health;
547 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
548 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
549 health->lch_local_interrupt_count++;
551 case LNET_MSG_STATUS_LOCAL_DROPPED:
552 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
553 health->lch_local_dropped_count++;
555 case LNET_MSG_STATUS_LOCAL_ABORTED:
556 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
557 health->lch_local_aborted_count++;
559 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
560 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
561 health->lch_local_no_route_count++;
563 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
564 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
565 health->lch_local_timeout_count++;
567 case LNET_MSG_STATUS_LOCAL_ERROR:
568 atomic_inc(&ni->ni_hstats.hlt_local_error);
569 health->lch_local_error_count++;
571 case LNET_MSG_STATUS_REMOTE_DROPPED:
573 atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
574 health->lch_remote_dropped_count++;
576 case LNET_MSG_STATUS_REMOTE_ERROR:
578 atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
579 health->lch_remote_error_count++;
581 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
583 atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
584 health->lch_remote_timeout_count++;
586 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
588 atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
589 health->lch_network_timeout_count++;
591 case LNET_MSG_STATUS_OK:
599 lnet_resend_msg_locked(struct lnet_msg *msg)
601 msg->msg_retry_count++;
604 * remove message from the active list and reset it to prepare
605 * for a resend. Two exceptions to this
607 * 1. the router case. When a message is being routed it is
608 * committed for rx when received and committed for tx when
609 * forwarded. We don't want to remove it from the active list, since
610 * code which handles receiving expects it to remain on the active
613 * 2. The REPLY case. Reply messages use the same message
614 * structure for the GET that was received.
616 if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
617 list_del_init(&msg->msg_activelist);
618 msg->msg_onactivelist = 0;
621 * The msg_target.nid which was originally set
622 * when calling LNetGet() or LNetPut() might've
623 * been overwritten if we're routing this message.
624 * Call lnet_msg_decommit_tx() to return the credit
625 * this message consumed. The message will
626 * consume another credit when it gets resent.
628 msg->msg_target.nid = msg->msg_hdr.dest_nid;
629 lnet_msg_decommit_tx(msg, -EAGAIN);
630 msg->msg_sending = 0;
631 msg->msg_receiving = 0;
632 msg->msg_target_is_router = 0;
634 CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
635 libcfs_nid2str(msg->msg_hdr.src_nid),
636 libcfs_nid2str(msg->msg_hdr.dest_nid),
637 lnet_msgtyp2str(msg->msg_type),
638 lnet_health_error2str(msg->msg_health_status), msg);
640 list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
642 complete(&the_lnet.ln_mt_wait_complete);
646 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
647 struct list_head *containerq,
648 int nworkers, void **workers)
653 list_add_tail(&msg->msg_list, containerq);
655 for (i = 0; i < nworkers; i++) {
656 if (workers[i] == current)
659 if (my_slot < 0 && workers[i] == NULL)
663 if (i < nworkers || my_slot < 0)
666 workers[my_slot] = current;
672 lnet_attempt_msg_resend(struct lnet_msg *msg)
674 struct lnet_msg_container *container;
678 /* we can only resend tx_committed messages */
679 LASSERT(msg->msg_tx_committed);
681 /* don't resend recovery messages */
682 if (msg->msg_recovery) {
683 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
684 libcfs_nid2str(msg->msg_from),
685 libcfs_nid2str(msg->msg_target.nid),
686 msg->msg_retry_count);
687 return -ENOTRECOVERABLE;
691 * if we explicitly indicated we don't want to resend then just
694 if (msg->msg_no_resend) {
695 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
696 libcfs_nid2str(msg->msg_from),
697 libcfs_nid2str(msg->msg_target.nid),
698 msg->msg_retry_count);
699 return -ENOTRECOVERABLE;
702 /* check if the message has exceeded the number of retries */
703 if (msg->msg_retry_count >= lnet_retry_count) {
704 CNETERR("msg %s->%s exceeded retry count %d\n",
705 libcfs_nid2str(msg->msg_from),
706 libcfs_nid2str(msg->msg_target.nid),
707 msg->msg_retry_count);
708 return -ENOTRECOVERABLE;
711 cpt = msg->msg_tx_cpt;
714 /* check again under lock */
715 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
716 lnet_net_unlock(cpt);
720 container = the_lnet.ln_msg_containers[cpt];
722 lnet_check_finalize_recursion_locked(msg,
723 &container->msc_resending,
724 container->msc_nfinalizers,
725 container->msc_resenders);
727 /* enough threads are resending */
729 lnet_net_unlock(cpt);
733 while (!list_empty(&container->msc_resending)) {
734 msg = list_entry(container->msc_resending.next,
735 struct lnet_msg, msg_list);
736 list_del(&msg->msg_list);
739 * resending the message will require us to call
740 * lnet_msg_decommit_tx() which will return the credit
741 * which this message holds. This could trigger another
742 * queued message to be sent. If that message fails and
743 * requires a resend we will recurse.
744 * But since at this point the slot is taken, the message
745 * will be queued in the container and dealt with
746 * later. This breaks the recursion.
748 lnet_resend_msg_locked(msg);
752 * msc_resenders is an array of process pointers. Each entry holds
753 * a pointer to the current process operating on the message. An
754 * array entry is created per CPT. If the array slot is already
755 * set, then it means that there is a thread on the CPT currently
756 * resending a message.
757 * Once the thread finishes clear the slot to enable the thread to
758 * take on more resend work.
760 container->msc_resenders[my_slot] = NULL;
761 lnet_net_unlock(cpt);
767 * Do a health check on the message:
768 * return -1 if we're not going to handle the error or
769 * if we've reached the maximum number of retries.
770 * success case will return -1 as well
771 * return 0 if it the message is requeued for send
774 lnet_health_check(struct lnet_msg *msg)
776 enum lnet_msg_hstatus hstatus = msg->msg_health_status;
777 struct lnet_peer_ni *lpni;
781 /* if we're shutting down no point in handling health. */
782 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
785 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
788 * if we're sending to the LOLND then the msg_txpeer will not be
789 * set. So no need to sanity check it.
791 if (msg->msg_tx_committed &&
792 LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) != LOLND)
793 LASSERT(msg->msg_txpeer);
794 else if (msg->msg_tx_committed &&
795 LNET_NETTYP(LNET_NIDNET(msg->msg_txni->ni_nid)) == LOLND)
798 if (hstatus != LNET_MSG_STATUS_OK &&
799 ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
803 * stats are only incremented for errors so avoid wasting time
804 * incrementing statistics if there is no error.
806 if (hstatus != LNET_MSG_STATUS_OK) {
808 lnet_incr_hstats(msg, hstatus);
813 * always prefer txni/txpeer if they message is committed for both
816 if (msg->msg_tx_committed) {
818 lpni = msg->msg_txpeer;
821 lpni = msg->msg_rxpeer;
829 CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
830 libcfs_nid2str(ni->ni_nid),
831 (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
832 lnet_msgtyp2str(msg->msg_type),
833 lnet_health_error2str(hstatus));
836 case LNET_MSG_STATUS_OK:
838 * increment the local ni health weather we successfully
839 * received or sent a message on it.
841 lnet_inc_healthv(&ni->ni_healthv);
843 * It's possible msg_txpeer is NULL in the LOLND
844 * case. Only increment the peer's health if we're
845 * receiving a message from it. It's the only sure way to
846 * know that a remote interface is up.
847 * If this interface is part of a router, then take that
848 * as indication that the router is fully healthy.
850 if (lpni && msg->msg_rx_committed) {
852 * If we're receiving a message from the router or
853 * I'm a router, then set that lpni's health to
854 * maximum so we can commence communication
857 if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
858 lnet_set_lpni_healthv_locked(lpni,
859 LNET_MAX_HEALTH_VALUE);
861 lnet_inc_lpni_healthv_locked(lpni);
866 /* we can finalize this message */
868 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
869 case LNET_MSG_STATUS_LOCAL_DROPPED:
870 case LNET_MSG_STATUS_LOCAL_ABORTED:
871 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
872 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
873 lnet_handle_local_failure(ni);
874 if (msg->msg_tx_committed)
875 /* add to the re-send queue */
876 return lnet_attempt_msg_resend(msg);
880 * These errors will not trigger a resend so simply
881 * finalize the message
883 case LNET_MSG_STATUS_LOCAL_ERROR:
884 lnet_handle_local_failure(ni);
888 * TODO: since the remote dropped the message we can
889 * attempt a resend safely.
891 case LNET_MSG_STATUS_REMOTE_DROPPED:
892 lnet_handle_remote_failure(lpni);
893 if (msg->msg_tx_committed)
894 return lnet_attempt_msg_resend(msg);
897 case LNET_MSG_STATUS_REMOTE_ERROR:
898 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
899 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
900 lnet_handle_remote_failure(lpni);
906 /* no resend is needed */
911 lnet_msg_detach_md(struct lnet_msg *msg, int cpt, int status)
913 struct lnet_libmd *md = msg->msg_md;
916 /* Now it's safe to drop my caller's ref */
918 LASSERT(md->md_refcount >= 0);
920 unlink = lnet_md_unlinkable(md);
921 if (md->md_eq != NULL) {
922 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
923 msg->msg_ev.status = -ETIMEDOUT;
924 CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
926 msg->msg_ev.status = status;
928 msg->msg_ev.unlinked = unlink;
929 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
932 if (unlink || (md->md_refcount == 0 &&
933 md->md_threshold == LNET_MD_THRESH_INF))
934 lnet_detach_rsp_tracker(md, cpt);
943 lnet_is_health_check(struct lnet_msg *msg)
946 int status = msg->msg_ev.status;
948 if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
949 !msg->msg_onactivelist) {
950 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
955 if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
956 (msg->msg_rx_committed && !msg->msg_rxpeer)) {
957 /* The optimized GET case does not set msg_rxpeer, but status
958 * could be zero. Only print the error message if we have a
962 CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
967 /* Check for status inconsistencies */
968 if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
969 (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
970 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
971 "checking (%d, %d)\n", msg, status,
972 msg->msg_health_status);
976 CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
977 hc, status, msg->msg_health_status);
983 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
986 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
987 return "LOCAL_INTERRUPT";
988 case LNET_MSG_STATUS_LOCAL_DROPPED:
989 return "LOCAL_DROPPED";
990 case LNET_MSG_STATUS_LOCAL_ABORTED:
991 return "LOCAL_ABORTED";
992 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
993 return "LOCAL_NO_ROUTE";
994 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
995 return "LOCAL_TIMEOUT";
996 case LNET_MSG_STATUS_LOCAL_ERROR:
997 return "LOCAL_ERROR";
998 case LNET_MSG_STATUS_REMOTE_DROPPED:
999 return "REMOTE_DROPPED";
1000 case LNET_MSG_STATUS_REMOTE_ERROR:
1001 return "REMOTE_ERROR";
1002 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1003 return "REMOTE_TIMEOUT";
1004 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1005 return "NETWORK_TIMEOUT";
1006 case LNET_MSG_STATUS_OK:
1014 lnet_send_error_simulation(struct lnet_msg *msg,
1015 enum lnet_msg_hstatus *hstatus)
1020 if (list_empty(&the_lnet.ln_drop_rules))
1023 /* match only health rules */
1024 if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
1028 CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1029 libcfs_nid2str(msg->msg_hdr.src_nid),
1030 libcfs_nid2str(msg->msg_txni->ni_nid),
1031 libcfs_nid2str(msg->msg_hdr.dest_nid),
1032 lnet_msgtyp2str(msg->msg_type),
1033 lnet_health_error2str(*hstatus));
1037 EXPORT_SYMBOL(lnet_send_error_simulation);
1040 lnet_finalize(struct lnet_msg *msg, int status)
1042 struct lnet_msg_container *container;
1047 LASSERT(!in_interrupt());
1052 msg->msg_ev.status = status;
1054 if (lnet_is_health_check(msg)) {
1056 * Check the health status of the message. If it has one
1057 * of the errors that we're supposed to handle, and it has
1058 * not timed out, then
1059 * 1. Decrement the appropriate health_value
1060 * 2. queue the message on the resend queue
1062 * if the message send is success, timed out or failed in the
1063 * health check for any reason then we'll just finalize the
1064 * message. Otherwise just return since the message has been
1065 * put on the resend queue.
1067 if (!lnet_health_check(msg))
1072 * We're not going to resend this message so detach its MD and invoke
1073 * the appropriate callbacks
1075 if (msg->msg_md != NULL) {
1076 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
1078 lnet_msg_detach_md(msg, cpt, status);
1079 lnet_res_unlock(cpt);
1083 if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1084 /* not committed to network yet */
1085 LASSERT(!msg->msg_onactivelist);
1091 * NB: routed message can be committed for both receiving and sending,
1092 * we should finalize in LIFO order and keep counters correct.
1093 * (finalize sending first then finalize receiving)
1095 cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1098 container = the_lnet.ln_msg_containers[cpt];
1100 /* Recursion breaker. Don't complete the message here if I am (or
1101 * enough other threads are) already completing messages */
1102 my_slot = lnet_check_finalize_recursion_locked(msg,
1103 &container->msc_finalizing,
1104 container->msc_nfinalizers,
1105 container->msc_finalizers);
1107 /* enough threads are resending */
1108 if (my_slot == -1) {
1109 lnet_net_unlock(cpt);
1114 while (!list_empty(&container->msc_finalizing)) {
1115 msg = list_entry(container->msc_finalizing.next,
1116 struct lnet_msg, msg_list);
1118 list_del_init(&msg->msg_list);
1120 /* NB drops and regains the lnet lock if it actually does
1121 * anything, so my finalizing friends can chomp along too */
1122 rc = lnet_complete_msg_locked(msg, cpt);
1127 if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1128 lnet_net_unlock(cpt);
1129 lnet_delay_rule_check();
1133 container->msc_finalizers[my_slot] = NULL;
1134 lnet_net_unlock(cpt);
1139 EXPORT_SYMBOL(lnet_finalize);
1142 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1146 if (container->msc_init == 0)
1149 while (!list_empty(&container->msc_active)) {
1150 struct lnet_msg *msg;
1152 msg = list_entry(container->msc_active.next,
1153 struct lnet_msg, msg_activelist);
1154 LASSERT(msg->msg_onactivelist);
1155 msg->msg_onactivelist = 0;
1156 list_del_init(&msg->msg_activelist);
1162 CERROR("%d active msg on exit\n", count);
1164 if (container->msc_finalizers != NULL) {
1165 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1166 container->msc_nfinalizers);
1167 container->msc_finalizers = NULL;
1170 if (container->msc_resenders != NULL) {
1171 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1172 container->msc_nfinalizers);
1173 container->msc_resenders = NULL;
1175 container->msc_init = 0;
1179 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1183 container->msc_init = 1;
1185 INIT_LIST_HEAD(&container->msc_active);
1186 INIT_LIST_HEAD(&container->msc_finalizing);
1187 INIT_LIST_HEAD(&container->msc_resending);
1189 /* number of CPUs */
1190 container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1191 if (container->msc_nfinalizers == 0)
1192 container->msc_nfinalizers = 1;
1194 LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1195 container->msc_nfinalizers *
1196 sizeof(*container->msc_finalizers));
1198 if (container->msc_finalizers == NULL) {
1199 CERROR("Failed to allocate message finalizers\n");
1200 lnet_msg_container_cleanup(container);
1204 LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1205 container->msc_nfinalizers *
1206 sizeof(*container->msc_resenders));
1208 if (container->msc_resenders == NULL) {
1209 CERROR("Failed to allocate message resenders\n");
1210 lnet_msg_container_cleanup(container);
1218 lnet_msg_containers_destroy(void)
1220 struct lnet_msg_container *container;
1223 if (the_lnet.ln_msg_containers == NULL)
1226 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1227 lnet_msg_container_cleanup(container);
1229 cfs_percpt_free(the_lnet.ln_msg_containers);
1230 the_lnet.ln_msg_containers = NULL;
1234 lnet_msg_containers_create(void)
1236 struct lnet_msg_container *container;
1240 the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1241 sizeof(*container));
1243 if (the_lnet.ln_msg_containers == NULL) {
1244 CERROR("Failed to allocate cpu-partition data for network\n");
1248 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1249 rc = lnet_msg_container_setup(container, i);
1251 lnet_msg_containers_destroy();