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/
33 * Message decoding, parsing and finalizing routines
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
41 lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev)
45 memset(ev, 0, sizeof(*ev));
49 ev->type = LNET_EVENT_UNLINK;
50 lnet_md_deconstruct(md, ev);
51 lnet_md2handle(&ev->md_handle, md);
56 * Don't need any lock, must be called after lnet_commit_md
59 lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type)
61 struct lnet_hdr *hdr = &msg->msg_hdr;
62 struct lnet_event *ev = &msg->msg_ev;
64 LASSERT(!msg->msg_routing);
67 ev->msg_type = msg->msg_type;
69 if (ev_type == LNET_EVENT_SEND) {
70 /* event for active message */
71 ev->target.nid = le64_to_cpu(hdr->dest_nid);
72 ev->target.pid = le32_to_cpu(hdr->dest_pid);
73 ev->initiator.nid = LNET_NID_ANY;
74 ev->initiator.pid = the_lnet.ln_pid;
75 ev->source.nid = LNET_NID_ANY;
76 ev->source.pid = the_lnet.ln_pid;
77 ev->sender = LNET_NID_ANY;
79 /* event for passive message */
80 ev->target.pid = hdr->dest_pid;
81 ev->target.nid = hdr->dest_nid;
82 ev->initiator.pid = hdr->src_pid;
83 /* Multi-Rail: resolve src_nid to "primary" peer NID */
84 ev->initiator.nid = msg->msg_initiator;
85 /* Multi-Rail: track source NID. */
86 ev->source.pid = hdr->src_pid;
87 ev->source.nid = hdr->src_nid;
88 ev->rlength = hdr->payload_length;
89 ev->sender = msg->msg_from;
90 ev->mlength = msg->msg_wanted;
91 ev->offset = msg->msg_offset;
98 case LNET_EVENT_PUT: /* passive PUT */
99 ev->pt_index = hdr->msg.put.ptl_index;
100 ev->match_bits = hdr->msg.put.match_bits;
101 ev->hdr_data = hdr->msg.put.hdr_data;
104 case LNET_EVENT_GET: /* passive GET */
105 ev->pt_index = hdr->msg.get.ptl_index;
106 ev->match_bits = hdr->msg.get.match_bits;
110 case LNET_EVENT_ACK: /* ACK */
111 ev->match_bits = hdr->msg.ack.match_bits;
112 ev->mlength = hdr->msg.ack.mlength;
115 case LNET_EVENT_REPLY: /* REPLY */
118 case LNET_EVENT_SEND: /* active message */
119 if (msg->msg_type == LNET_MSG_PUT) {
120 ev->pt_index = le32_to_cpu(hdr->msg.put.ptl_index);
121 ev->match_bits = le64_to_cpu(hdr->msg.put.match_bits);
122 ev->offset = le32_to_cpu(hdr->msg.put.offset);
124 ev->rlength = le32_to_cpu(hdr->payload_length);
125 ev->hdr_data = le64_to_cpu(hdr->msg.put.hdr_data);
128 LASSERT(msg->msg_type == LNET_MSG_GET);
129 ev->pt_index = le32_to_cpu(hdr->msg.get.ptl_index);
130 ev->match_bits = le64_to_cpu(hdr->msg.get.match_bits);
132 ev->rlength = le32_to_cpu(hdr->msg.get.sink_length);
133 ev->offset = le32_to_cpu(hdr->msg.get.src_offset);
141 lnet_msg_commit(struct lnet_msg *msg, int cpt)
143 struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
144 struct lnet_counters_common *common;
147 /* set the message deadline */
148 timeout_ns = lnet_transaction_timeout * NSEC_PER_SEC;
149 msg->msg_deadline = ktime_add_ns(ktime_get(), timeout_ns);
151 /* routed message can be committed for both receiving and sending */
152 LASSERT(!msg->msg_tx_committed);
154 if (msg->msg_sending) {
155 LASSERT(!msg->msg_receiving);
156 msg->msg_tx_cpt = cpt;
157 msg->msg_tx_committed = 1;
158 if (msg->msg_rx_committed) { /* routed message REPLY */
159 LASSERT(msg->msg_onactivelist);
163 LASSERT(!msg->msg_sending);
164 msg->msg_rx_cpt = cpt;
165 msg->msg_rx_committed = 1;
168 LASSERT(!msg->msg_onactivelist);
170 msg->msg_onactivelist = 1;
171 list_add_tail(&msg->msg_activelist, &container->msc_active);
173 common = &the_lnet.ln_counters[cpt]->lct_common;
174 common->lcc_msgs_alloc++;
175 if (common->lcc_msgs_alloc > common->lcc_msgs_max)
176 common->lcc_msgs_max = common->lcc_msgs_alloc;
180 lnet_msg_decommit_tx(struct lnet_msg *msg, int status)
182 struct lnet_counters_common *common;
183 struct lnet_event *ev = &msg->msg_ev;
185 LASSERT(msg->msg_tx_committed);
189 common = &(the_lnet.ln_counters[msg->msg_tx_cpt]->lct_common);
191 default: /* routed message */
192 LASSERT(msg->msg_routing);
193 LASSERT(msg->msg_rx_committed);
194 LASSERT(ev->type == 0);
196 common->lcc_route_length += msg->msg_len;
197 common->lcc_route_count++;
201 /* should have been decommitted */
202 LASSERT(!msg->msg_rx_committed);
203 /* overwritten while sending ACK */
204 LASSERT(msg->msg_type == LNET_MSG_ACK);
205 msg->msg_type = LNET_MSG_PUT; /* fix type */
208 case LNET_EVENT_SEND:
209 LASSERT(!msg->msg_rx_committed);
210 if (msg->msg_type == LNET_MSG_PUT)
211 common->lcc_send_length += msg->msg_len;
215 LASSERT(msg->msg_rx_committed);
216 /* overwritten while sending reply, we should never be
217 * here for optimized GET */
218 LASSERT(msg->msg_type == LNET_MSG_REPLY);
219 msg->msg_type = LNET_MSG_GET; /* fix type */
223 common->lcc_send_count++;
227 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
229 LNET_STATS_TYPE_SEND);
231 lnet_incr_stats(&msg->msg_txni->ni_stats,
233 LNET_STATS_TYPE_SEND);
235 lnet_return_tx_credits_locked(msg);
236 msg->msg_tx_committed = 0;
240 lnet_msg_decommit_rx(struct lnet_msg *msg, int status)
242 struct lnet_counters_common *common;
243 struct lnet_event *ev = &msg->msg_ev;
245 LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
246 LASSERT(msg->msg_rx_committed);
251 common = &(the_lnet.ln_counters[msg->msg_rx_cpt]->lct_common);
254 LASSERT(ev->type == 0);
255 LASSERT(msg->msg_routing);
259 LASSERT(msg->msg_type == LNET_MSG_ACK);
263 /* type is "REPLY" if it's an optimized GET on passive side,
264 * because optimized GET will never be committed for sending,
265 * so message type wouldn't be changed back to "GET" by
266 * lnet_msg_decommit_tx(), see details in lnet_parse_get() */
267 LASSERT(msg->msg_type == LNET_MSG_REPLY ||
268 msg->msg_type == LNET_MSG_GET);
269 common->lcc_send_length += msg->msg_wanted;
273 LASSERT(msg->msg_type == LNET_MSG_PUT);
276 case LNET_EVENT_REPLY:
277 /* type is "GET" if it's an optimized GET on active side,
278 * see details in lnet_create_reply_msg() */
279 LASSERT(msg->msg_type == LNET_MSG_GET ||
280 msg->msg_type == LNET_MSG_REPLY);
284 common->lcc_recv_count++;
288 lnet_incr_stats(&msg->msg_rxpeer->lpni_stats,
290 LNET_STATS_TYPE_RECV);
292 lnet_incr_stats(&msg->msg_rxni->ni_stats,
294 LNET_STATS_TYPE_RECV);
295 if (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_REPLY)
296 common->lcc_recv_length += msg->msg_wanted;
299 lnet_return_rx_credits_locked(msg);
300 msg->msg_rx_committed = 0;
304 lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status)
308 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
309 LASSERT(msg->msg_onactivelist);
311 if (msg->msg_tx_committed) { /* always decommit for sending first */
312 LASSERT(cpt == msg->msg_tx_cpt);
313 lnet_msg_decommit_tx(msg, status);
316 if (msg->msg_rx_committed) {
317 /* forwarding msg committed for both receiving and sending */
318 if (cpt != msg->msg_rx_cpt) {
319 lnet_net_unlock(cpt);
320 cpt2 = msg->msg_rx_cpt;
323 lnet_msg_decommit_rx(msg, status);
326 list_del(&msg->msg_activelist);
327 msg->msg_onactivelist = 0;
329 the_lnet.ln_counters[cpt2]->lct_common.lcc_msgs_alloc--;
332 lnet_net_unlock(cpt2);
338 lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
339 unsigned int offset, unsigned int mlen)
341 /* NB: @offset and @len are only useful for receiving */
342 /* Here, we attach the MD on lnet_msg and mark it busy and
343 * decrementing its threshold. Come what may, the lnet_msg "owns"
344 * the MD until a call to lnet_msg_detach_md or lnet_finalize()
345 * signals completion. */
346 LASSERT(!msg->msg_routing);
349 if (msg->msg_receiving) { /* committed for receiving */
350 msg->msg_offset = offset;
351 msg->msg_wanted = mlen;
355 if (md->md_threshold != LNET_MD_THRESH_INF) {
356 LASSERT(md->md_threshold > 0);
360 /* build umd in event */
361 lnet_md2handle(&msg->msg_ev.md_handle, md);
362 lnet_md_deconstruct(md, &msg->msg_ev);
366 lnet_complete_msg_locked(struct lnet_msg *msg, int cpt)
368 struct lnet_handle_wire ack_wmd;
370 int status = msg->msg_ev.status;
372 LASSERT(msg->msg_onactivelist);
374 if (status == 0 && msg->msg_ack) {
375 /* Only send an ACK if the PUT completed successfully */
377 lnet_msg_decommit(msg, cpt, 0);
380 lnet_net_unlock(cpt);
382 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
383 LASSERT(!msg->msg_routing);
385 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
387 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.source, 0, 0);
389 msg->msg_hdr.msg.ack.dst_wmd = ack_wmd;
390 msg->msg_hdr.msg.ack.match_bits = msg->msg_ev.match_bits;
391 msg->msg_hdr.msg.ack.mlength = cpu_to_le32(msg->msg_ev.mlength);
393 rc = lnet_send(msg->msg_ev.target.nid, msg, msg->msg_from);
397 * NB: message is committed for sending, we should return
398 * on success because LND will finalize this message later.
400 * Also, there is possibility that message is committed for
401 * sending and also failed before delivering to LND,
402 * i.e: ENOMEM, in that case we can't fall through either
403 * because CPT for sending can be different with CPT for
404 * receiving, so we should return back to lnet_finalize()
405 * to make sure we are locking the correct partition.
409 } else if (status == 0 && /* OK so far */
410 (msg->msg_routing && !msg->msg_sending)) {
412 LASSERT(!msg->msg_receiving); /* called back recv already */
413 lnet_net_unlock(cpt);
415 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
419 * NB: message is committed for sending, we should return
420 * on success because LND will finalize this message later.
422 * Also, there is possibility that message is committed for
423 * sending and also failed before delivering to LND,
424 * i.e: ENOMEM, in that case we can't fall through either:
425 * - The rule is message must decommit for sending first if
426 * the it's committed for both sending and receiving
427 * - CPT for sending can be different with CPT for receiving,
428 * so we should return back to lnet_finalize() to make
429 * sure we are locking the correct partition.
434 lnet_msg_decommit(msg, cpt, status);
440 lnet_dec_healthv_locked(atomic_t *healthv, int sensitivity)
442 int h = atomic_read(healthv);
444 if (h < sensitivity) {
445 atomic_set(healthv, 0);
448 atomic_set(healthv, h);
453 lnet_handle_local_failure(struct lnet_ni *local_ni)
456 * the lnet_net_lock(0) is used to protect the addref on the ni
457 * and the recovery queue.
460 /* the mt could've shutdown and cleaned up the queues */
461 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
466 lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
468 * add the NI to the recovery queue if it's not already there
469 * and it's health value is actually below the maximum. It's
470 * possible that the sensitivity might be set to 0, and the health
471 * value will not be reduced. In this case, there is no reason to
474 if (list_empty(&local_ni->ni_recovery) &&
475 atomic_read(&local_ni->ni_healthv) < LNET_MAX_HEALTH_VALUE) {
476 CDEBUG(D_NET, "ni %s added to recovery queue. Health = %d\n",
477 libcfs_nid2str(local_ni->ni_nid),
478 atomic_read(&local_ni->ni_healthv));
479 list_add_tail(&local_ni->ni_recovery,
480 &the_lnet.ln_mt_localNIRecovq);
481 lnet_ni_addref_locked(local_ni, 0);
486 /* must hold net_lock/0 */
488 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
490 __u32 sensitivity = lnet_health_sensitivity;
491 __u32 lp_sensitivity;
494 * If there is a health sensitivity in the peer then use that
495 * instead of the globally set one.
497 lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
499 sensitivity = lp_sensitivity;
501 lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
503 /* update the peer_net's health value */
504 lnet_update_peer_net_healthv(lpni);
507 * add the peer NI to the recovery queue if it's not already there
508 * and it's health value is actually below the maximum. It's
509 * possible that the sensitivity might be set to 0, and the health
510 * value will not be reduced. In this case, there is no reason to
513 lnet_peer_ni_add_to_recoveryq_locked(lpni,
514 &the_lnet.ln_mt_peerNIRecovq,
515 ktime_get_seconds());
519 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
521 /* lpni could be NULL if we're in the LOLND case */
526 /* the mt could've shutdown and cleaned up the queues */
527 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
531 lnet_handle_remote_failure_locked(lpni);
536 lnet_incr_hstats(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
537 enum lnet_msg_hstatus hstatus)
539 struct lnet_counters_health *health;
541 health = &the_lnet.ln_counters[0]->lct_health;
544 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
545 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
546 health->lch_local_interrupt_count++;
548 case LNET_MSG_STATUS_LOCAL_DROPPED:
549 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
550 health->lch_local_dropped_count++;
552 case LNET_MSG_STATUS_LOCAL_ABORTED:
553 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
554 health->lch_local_aborted_count++;
556 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
557 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
558 health->lch_local_no_route_count++;
560 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
561 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
562 health->lch_local_timeout_count++;
564 case LNET_MSG_STATUS_LOCAL_ERROR:
565 atomic_inc(&ni->ni_hstats.hlt_local_error);
566 health->lch_local_error_count++;
568 case LNET_MSG_STATUS_REMOTE_DROPPED:
570 atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
571 health->lch_remote_dropped_count++;
573 case LNET_MSG_STATUS_REMOTE_ERROR:
575 atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
576 health->lch_remote_error_count++;
578 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
580 atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
581 health->lch_remote_timeout_count++;
583 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
585 atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
586 health->lch_network_timeout_count++;
588 case LNET_MSG_STATUS_OK:
596 lnet_resend_msg_locked(struct lnet_msg *msg)
598 msg->msg_retry_count++;
601 * remove message from the active list and reset it to prepare
602 * for a resend. Two exceptions to this
604 * 1. the router case. When a message is being routed it is
605 * committed for rx when received and committed for tx when
606 * forwarded. We don't want to remove it from the active list, since
607 * code which handles receiving expects it to remain on the active
610 * 2. The REPLY case. Reply messages use the same message
611 * structure for the GET that was received.
613 if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
614 list_del_init(&msg->msg_activelist);
615 msg->msg_onactivelist = 0;
618 * The msg_target.nid which was originally set
619 * when calling LNetGet() or LNetPut() might've
620 * been overwritten if we're routing this message.
621 * Call lnet_msg_decommit_tx() to return the credit
622 * this message consumed. The message will
623 * consume another credit when it gets resent.
625 msg->msg_target.nid = msg->msg_hdr.dest_nid;
626 lnet_msg_decommit_tx(msg, -EAGAIN);
627 msg->msg_sending = 0;
628 msg->msg_receiving = 0;
629 msg->msg_target_is_router = 0;
631 CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
632 libcfs_nid2str(msg->msg_hdr.src_nid),
633 libcfs_nid2str(msg->msg_hdr.dest_nid),
634 lnet_msgtyp2str(msg->msg_type),
635 lnet_health_error2str(msg->msg_health_status), msg);
637 list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
639 complete(&the_lnet.ln_mt_wait_complete);
643 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
644 struct list_head *containerq,
645 int nworkers, void **workers)
650 list_add_tail(&msg->msg_list, containerq);
652 for (i = 0; i < nworkers; i++) {
653 if (workers[i] == current)
656 if (my_slot < 0 && workers[i] == NULL)
660 if (i < nworkers || my_slot < 0)
663 workers[my_slot] = current;
669 lnet_attempt_msg_resend(struct lnet_msg *msg)
671 struct lnet_msg_container *container;
675 /* we can only resend tx_committed messages */
676 LASSERT(msg->msg_tx_committed);
678 /* don't resend recovery messages */
679 if (msg->msg_recovery) {
680 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
681 libcfs_nid2str(msg->msg_from),
682 libcfs_nid2str(msg->msg_target.nid),
683 msg->msg_retry_count);
684 return -ENOTRECOVERABLE;
688 * if we explicitly indicated we don't want to resend then just
691 if (msg->msg_no_resend) {
692 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
693 libcfs_nid2str(msg->msg_from),
694 libcfs_nid2str(msg->msg_target.nid),
695 msg->msg_retry_count);
696 return -ENOTRECOVERABLE;
699 /* check if the message has exceeded the number of retries */
700 if (msg->msg_retry_count >= lnet_retry_count) {
701 CNETERR("msg %s->%s exceeded retry count %d\n",
702 libcfs_nid2str(msg->msg_from),
703 libcfs_nid2str(msg->msg_target.nid),
704 msg->msg_retry_count);
705 return -ENOTRECOVERABLE;
708 cpt = msg->msg_tx_cpt;
711 /* check again under lock */
712 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
713 lnet_net_unlock(cpt);
717 container = the_lnet.ln_msg_containers[cpt];
719 lnet_check_finalize_recursion_locked(msg,
720 &container->msc_resending,
721 container->msc_nfinalizers,
722 container->msc_resenders);
724 /* enough threads are resending */
726 lnet_net_unlock(cpt);
730 while (!list_empty(&container->msc_resending)) {
731 msg = list_entry(container->msc_resending.next,
732 struct lnet_msg, msg_list);
733 list_del(&msg->msg_list);
736 * resending the message will require us to call
737 * lnet_msg_decommit_tx() which will return the credit
738 * which this message holds. This could trigger another
739 * queued message to be sent. If that message fails and
740 * requires a resend we will recurse.
741 * But since at this point the slot is taken, the message
742 * will be queued in the container and dealt with
743 * later. This breaks the recursion.
745 lnet_resend_msg_locked(msg);
749 * msc_resenders is an array of process pointers. Each entry holds
750 * a pointer to the current process operating on the message. An
751 * array entry is created per CPT. If the array slot is already
752 * set, then it means that there is a thread on the CPT currently
753 * resending a message.
754 * Once the thread finishes clear the slot to enable the thread to
755 * take on more resend work.
757 container->msc_resenders[my_slot] = NULL;
758 lnet_net_unlock(cpt);
764 * Do a health check on the message:
765 * return -1 if we're not going to handle the error or
766 * if we've reached the maximum number of retries.
767 * success case will return -1 as well
768 * return 0 if it the message is requeued for send
771 lnet_health_check(struct lnet_msg *msg)
773 enum lnet_msg_hstatus hstatus = msg->msg_health_status;
774 struct lnet_peer_ni *lpni;
777 bool attempt_local_resend;
778 bool attempt_remote_resend;
779 bool handle_local_health;
780 bool handle_remote_health;
782 /* if we're shutting down no point in handling health. */
783 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
786 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
789 * if we're sending to the LOLND then the msg_txpeer will not be
790 * set. So no need to sanity check it.
792 if (msg->msg_tx_committed && msg->msg_txni->ni_nid != LNET_NID_LO_0)
793 LASSERT(msg->msg_txpeer);
794 else if (msg->msg_tx_committed &&
795 msg->msg_txni->ni_nid == LNET_NID_LO_0)
798 if (hstatus != LNET_MSG_STATUS_OK &&
799 ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
803 * always prefer txni/txpeer if they message is committed for both
806 if (msg->msg_tx_committed) {
808 lpni = msg->msg_txpeer;
809 attempt_local_resend = attempt_remote_resend = true;
812 lpni = msg->msg_rxpeer;
813 attempt_local_resend = attempt_remote_resend = false;
816 /* Don't further decrement the health value if a recovery message
819 if (msg->msg_recovery)
820 handle_local_health = handle_remote_health = false;
822 handle_local_health = handle_remote_health = true;
824 /* For local failures, health/recovery/resends are not needed if I only
825 * have a single (non-lolnd) interface. NB: pb_nnis includes the lolnd
826 * interface, so a single-rail node would have pb_nnis == 2.
828 if (the_lnet.ln_ping_target->pb_nnis <= 2) {
829 handle_local_health = false;
830 attempt_local_resend = false;
833 /* For remote failures, health/recovery/resends are not needed if the
834 * peer only has a single interface. Special case for routers where we
835 * rely on health feature to manage route aliveness. NB: unlike pb_nnis
836 * above, lp_nnis does _not_ include the lolnd, so a single-rail node
837 * would have lp_nnis == 1.
839 if (lpni && lpni->lpni_peer_net->lpn_peer->lp_nnis <= 1) {
840 attempt_remote_resend = false;
841 if (!lnet_isrouter(lpni))
842 handle_remote_health = false;
850 CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
851 libcfs_nid2str(ni->ni_nid),
852 (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
853 lnet_msgtyp2str(msg->msg_type),
854 lnet_health_error2str(hstatus));
857 * stats are only incremented for errors so avoid wasting time
858 * incrementing statistics if there is no error.
860 if (hstatus != LNET_MSG_STATUS_OK) {
862 lnet_incr_hstats(ni, lpni, hstatus);
867 case LNET_MSG_STATUS_OK:
869 * increment the local ni health whether we successfully
870 * received or sent a message on it.
872 * Ping counts are reset to 0 as appropriate to allow for
875 lnet_inc_healthv(&ni->ni_healthv, lnet_health_sensitivity);
877 * It's possible msg_txpeer is NULL in the LOLND
878 * case. Only increment the peer's health if we're
879 * receiving a message from it. It's the only sure way to
880 * know that a remote interface is up.
881 * If this interface is part of a router, then take that
882 * as indication that the router is fully healthy.
884 if (lpni && msg->msg_rx_committed) {
885 lpni->lpni_ping_count = 0;
887 * If we're receiving a message from the router or
888 * I'm a router, then set that lpni's health to
889 * maximum so we can commence communication
892 if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
893 lnet_set_lpni_healthv_locked(lpni,
894 LNET_MAX_HEALTH_VALUE);
896 __u32 sensitivity = lpni->lpni_peer_net->
897 lpn_peer->lp_health_sensitivity;
899 lnet_inc_lpni_healthv_locked(lpni,
900 (sensitivity) ? sensitivity :
901 lnet_health_sensitivity);
902 /* This peer NI may have previously aged out
903 * of recovery. Now that we've received a
904 * message from it, we can continue recovery
905 * if its health value is still below the
908 lnet_peer_ni_add_to_recoveryq_locked(lpni,
909 &the_lnet.ln_mt_peerNIRecovq,
910 ktime_get_seconds());
915 /* we can finalize this message */
917 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
918 case LNET_MSG_STATUS_LOCAL_DROPPED:
919 case LNET_MSG_STATUS_LOCAL_ABORTED:
920 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
921 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
922 if (handle_local_health)
923 lnet_handle_local_failure(ni);
924 if (attempt_local_resend)
925 return lnet_attempt_msg_resend(msg);
927 case LNET_MSG_STATUS_LOCAL_ERROR:
928 if (handle_local_health)
929 lnet_handle_local_failure(ni);
931 case LNET_MSG_STATUS_REMOTE_DROPPED:
932 if (handle_remote_health)
933 lnet_handle_remote_failure(lpni);
934 if (attempt_remote_resend)
935 return lnet_attempt_msg_resend(msg);
937 case LNET_MSG_STATUS_REMOTE_ERROR:
938 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
939 if (handle_remote_health)
940 lnet_handle_remote_failure(lpni);
942 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
943 if (handle_remote_health)
944 lnet_handle_remote_failure(lpni);
945 if (handle_local_health)
946 lnet_handle_local_failure(ni);
952 /* no resend is needed */
957 lnet_msg_detach_md(struct lnet_msg *msg, int status)
959 struct lnet_libmd *md = msg->msg_md;
960 lnet_handler_t handler = NULL;
961 int cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
965 while (md->md_flags & LNET_MD_FLAG_HANDLING)
966 /* An event handler is running - wait for it to
967 * complete to avoid races.
969 lnet_md_wait_handling(md, cpt);
971 /* Now it's safe to drop my caller's ref */
973 LASSERT(md->md_refcount >= 0);
975 unlink = lnet_md_unlinkable(md);
976 if (md->md_handler) {
977 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
978 msg->msg_ev.status = -ETIMEDOUT;
979 CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
981 msg->msg_ev.status = status;
983 msg->msg_ev.unlinked = unlink;
984 handler = md->md_handler;
986 md->md_flags |= LNET_MD_FLAG_HANDLING;
989 if (unlink || (md->md_refcount == 0 &&
990 md->md_threshold == LNET_MD_THRESH_INF))
991 lnet_detach_rsp_tracker(md, cpt);
997 lnet_res_unlock(cpt);
1000 handler(&msg->msg_ev);
1003 md->md_flags &= ~LNET_MD_FLAG_HANDLING;
1005 lnet_res_unlock(cpt);
1011 lnet_is_health_check(struct lnet_msg *msg)
1014 int status = msg->msg_ev.status;
1016 if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
1017 !msg->msg_onactivelist) {
1018 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
1023 if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
1024 (msg->msg_rx_committed && !msg->msg_rxpeer)) {
1025 /* The optimized GET case does not set msg_rxpeer, but status
1026 * could be zero. Only print the error message if we have a
1030 CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
1035 /* Check for status inconsistencies */
1036 if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
1037 (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
1038 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
1039 "checking (%d, %d)\n", msg, status,
1040 msg->msg_health_status);
1044 CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
1045 hc, status, msg->msg_health_status);
1051 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
1054 case LNET_MSG_STATUS_LOCAL_INTERRUPT:
1055 return "LOCAL_INTERRUPT";
1056 case LNET_MSG_STATUS_LOCAL_DROPPED:
1057 return "LOCAL_DROPPED";
1058 case LNET_MSG_STATUS_LOCAL_ABORTED:
1059 return "LOCAL_ABORTED";
1060 case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
1061 return "LOCAL_NO_ROUTE";
1062 case LNET_MSG_STATUS_LOCAL_TIMEOUT:
1063 return "LOCAL_TIMEOUT";
1064 case LNET_MSG_STATUS_LOCAL_ERROR:
1065 return "LOCAL_ERROR";
1066 case LNET_MSG_STATUS_REMOTE_DROPPED:
1067 return "REMOTE_DROPPED";
1068 case LNET_MSG_STATUS_REMOTE_ERROR:
1069 return "REMOTE_ERROR";
1070 case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1071 return "REMOTE_TIMEOUT";
1072 case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1073 return "NETWORK_TIMEOUT";
1074 case LNET_MSG_STATUS_OK:
1082 lnet_send_error_simulation(struct lnet_msg *msg,
1083 enum lnet_msg_hstatus *hstatus)
1088 if (list_empty(&the_lnet.ln_drop_rules))
1091 /* match only health rules */
1092 if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
1096 CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1097 libcfs_nid2str(msg->msg_hdr.src_nid),
1098 libcfs_nid2str(msg->msg_txni->ni_nid),
1099 libcfs_nid2str(msg->msg_hdr.dest_nid),
1100 lnet_msgtyp2str(msg->msg_type),
1101 lnet_health_error2str(*hstatus));
1105 EXPORT_SYMBOL(lnet_send_error_simulation);
1108 lnet_finalize(struct lnet_msg *msg, int status)
1110 struct lnet_msg_container *container;
1115 LASSERT(!in_interrupt());
1120 msg->msg_ev.status = status;
1122 if (lnet_is_health_check(msg)) {
1124 * Check the health status of the message. If it has one
1125 * of the errors that we're supposed to handle, and it has
1126 * not timed out, then
1127 * 1. Decrement the appropriate health_value
1128 * 2. queue the message on the resend queue
1130 * if the message send is success, timed out or failed in the
1131 * health check for any reason then we'll just finalize the
1132 * message. Otherwise just return since the message has been
1133 * put on the resend queue.
1135 if (!lnet_health_check(msg))
1140 * We're not going to resend this message so detach its MD and invoke
1141 * the appropriate callbacks
1143 if (msg->msg_md != NULL)
1144 lnet_msg_detach_md(msg, status);
1147 if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1148 /* not committed to network yet */
1149 LASSERT(!msg->msg_onactivelist);
1155 * NB: routed message can be committed for both receiving and sending,
1156 * we should finalize in LIFO order and keep counters correct.
1157 * (finalize sending first then finalize receiving)
1159 cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1162 container = the_lnet.ln_msg_containers[cpt];
1164 /* Recursion breaker. Don't complete the message here if I am (or
1165 * enough other threads are) already completing messages */
1166 my_slot = lnet_check_finalize_recursion_locked(msg,
1167 &container->msc_finalizing,
1168 container->msc_nfinalizers,
1169 container->msc_finalizers);
1171 /* enough threads are resending */
1172 if (my_slot == -1) {
1173 lnet_net_unlock(cpt);
1178 while (!list_empty(&container->msc_finalizing)) {
1179 msg = list_entry(container->msc_finalizing.next,
1180 struct lnet_msg, msg_list);
1182 list_del_init(&msg->msg_list);
1184 /* NB drops and regains the lnet lock if it actually does
1185 * anything, so my finalizing friends can chomp along too */
1186 rc = lnet_complete_msg_locked(msg, cpt);
1191 if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1192 lnet_net_unlock(cpt);
1193 lnet_delay_rule_check();
1197 container->msc_finalizers[my_slot] = NULL;
1198 lnet_net_unlock(cpt);
1203 EXPORT_SYMBOL(lnet_finalize);
1206 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1210 if (container->msc_init == 0)
1213 while (!list_empty(&container->msc_active)) {
1214 struct lnet_msg *msg;
1216 msg = list_entry(container->msc_active.next,
1217 struct lnet_msg, msg_activelist);
1218 LASSERT(msg->msg_onactivelist);
1219 msg->msg_onactivelist = 0;
1220 list_del_init(&msg->msg_activelist);
1226 CERROR("%d active msg on exit\n", count);
1228 if (container->msc_finalizers != NULL) {
1229 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1230 container->msc_nfinalizers);
1231 container->msc_finalizers = NULL;
1234 if (container->msc_resenders != NULL) {
1235 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1236 container->msc_nfinalizers);
1237 container->msc_resenders = NULL;
1239 container->msc_init = 0;
1243 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1247 container->msc_init = 1;
1249 INIT_LIST_HEAD(&container->msc_active);
1250 INIT_LIST_HEAD(&container->msc_finalizing);
1251 INIT_LIST_HEAD(&container->msc_resending);
1253 /* number of CPUs */
1254 container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1255 if (container->msc_nfinalizers == 0)
1256 container->msc_nfinalizers = 1;
1258 LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1259 container->msc_nfinalizers *
1260 sizeof(*container->msc_finalizers));
1262 if (container->msc_finalizers == NULL) {
1263 CERROR("Failed to allocate message finalizers\n");
1264 lnet_msg_container_cleanup(container);
1268 LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1269 container->msc_nfinalizers *
1270 sizeof(*container->msc_resenders));
1272 if (container->msc_resenders == NULL) {
1273 CERROR("Failed to allocate message resenders\n");
1274 lnet_msg_container_cleanup(container);
1282 lnet_msg_containers_destroy(void)
1284 struct lnet_msg_container *container;
1287 if (the_lnet.ln_msg_containers == NULL)
1290 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1291 lnet_msg_container_cleanup(container);
1293 cfs_percpt_free(the_lnet.ln_msg_containers);
1294 the_lnet.ln_msg_containers = NULL;
1298 lnet_msg_containers_create(void)
1300 struct lnet_msg_container *container;
1304 the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1305 sizeof(*container));
1307 if (the_lnet.ln_msg_containers == NULL) {
1308 CERROR("Failed to allocate cpu-partition data for network\n");
1312 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1313 rc = lnet_msg_container_setup(container, i);
1315 lnet_msg_containers_destroy();