Whamcloud - gitweb
LU-15930 lnet: Remove duplicate checks for peer sensitivity
[fs/lustre-release.git] / lnet / lnet / lib-msg.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lnet/lnet/lib-msg.c
32  *
33  * Message decoding, parsing and finalizing routines
34  */
35
36 #define DEBUG_SUBSYSTEM S_LNET
37
38 #include <lnet/lib-lnet.h>
39
40 void
41 lnet_build_unlink_event(struct lnet_libmd *md, struct lnet_event *ev)
42 {
43         ENTRY;
44
45         memset(ev, 0, sizeof(*ev));
46
47         ev->status   = 0;
48         ev->unlinked = 1;
49         ev->type     = LNET_EVENT_UNLINK;
50         lnet_md_deconstruct(md, ev);
51         lnet_md2handle(&ev->md_handle, md);
52         EXIT;
53 }
54
55 /*
56  * Don't need any lock, must be called after lnet_commit_md
57  */
58 void
59 lnet_build_msg_event(struct lnet_msg *msg, enum lnet_event_kind ev_type)
60 {
61         struct lnet_hdr *hdr = &msg->msg_hdr;
62         struct lnet_event *ev = &msg->msg_ev;
63
64         LASSERT(!msg->msg_routing);
65
66         ev->type = ev_type;
67         ev->msg_type = msg->msg_type;
68
69         if (ev_type == LNET_EVENT_SEND) {
70                 /* event for active message */
71                 ev->target.nid    = hdr->dest_nid;
72                 ev->target.pid    = hdr->dest_pid;
73                 ev->initiator.nid = LNET_ANY_NID;
74                 ev->initiator.pid = the_lnet.ln_pid;
75                 ev->source.nid    = LNET_ANY_NID;
76                 ev->source.pid    = the_lnet.ln_pid;
77                 ev->sender        = LNET_ANY_NID;
78         } else {
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;
92         }
93
94         switch (ev_type) {
95         default:
96                 LBUG();
97
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;
102                 return;
103
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;
107                 ev->hdr_data   = 0;
108                 return;
109
110         case LNET_EVENT_ACK: /* ACK */
111                 ev->match_bits = hdr->msg.ack.match_bits;
112                 ev->mlength    = hdr->msg.ack.mlength;
113                 return;
114
115         case LNET_EVENT_REPLY: /* REPLY */
116                 return;
117
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);
123                         ev->mlength    =
124                         ev->rlength    = le32_to_cpu(hdr->payload_length);
125                         ev->hdr_data   = le64_to_cpu(hdr->msg.put.hdr_data);
126
127                 } else {
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);
131                         ev->mlength    =
132                         ev->rlength    = le32_to_cpu(hdr->msg.get.sink_length);
133                         ev->offset     = le32_to_cpu(hdr->msg.get.src_offset);
134                         ev->hdr_data   = 0;
135                 }
136                 return;
137         }
138 }
139
140 void
141 lnet_msg_commit(struct lnet_msg *msg, int cpt)
142 {
143         struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
144         struct lnet_counters_common *common;
145         s64 timeout_ns;
146
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);
150
151         /* routed message can be committed for both receiving and sending */
152         LASSERT(!msg->msg_tx_committed);
153
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);
160                         return;
161                 }
162         } else {
163                 LASSERT(!msg->msg_sending);
164                 msg->msg_rx_cpt = cpt;
165                 msg->msg_rx_committed = 1;
166         }
167
168         LASSERT(!msg->msg_onactivelist);
169
170         msg->msg_onactivelist = 1;
171         list_add_tail(&msg->msg_activelist, &container->msc_active);
172
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;
177 }
178
179 static void
180 lnet_msg_decommit_tx(struct lnet_msg *msg, int status)
181 {
182         struct lnet_counters_common *common;
183         struct lnet_event *ev = &msg->msg_ev;
184
185         LASSERT(msg->msg_tx_committed);
186         if (status != 0)
187                 goto out;
188
189         common = &(the_lnet.ln_counters[msg->msg_tx_cpt]->lct_common);
190         switch (ev->type) {
191         default: /* routed message */
192                 LASSERT(msg->msg_routing);
193                 LASSERT(msg->msg_rx_committed);
194                 LASSERT(ev->type == 0);
195
196                 common->lcc_route_length += msg->msg_len;
197                 common->lcc_route_count++;
198                 goto incr_stats;
199
200         case LNET_EVENT_PUT:
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 */
206                 break;
207
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;
212                 break;
213
214         case LNET_EVENT_GET:
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 */
220                 break;
221         }
222
223         common->lcc_send_count++;
224
225 incr_stats:
226         if (msg->msg_txpeer)
227                 lnet_incr_stats(&msg->msg_txpeer->lpni_stats,
228                                 msg->msg_type,
229                                 LNET_STATS_TYPE_SEND);
230         if (msg->msg_txni)
231                 lnet_incr_stats(&msg->msg_txni->ni_stats,
232                                 msg->msg_type,
233                                 LNET_STATS_TYPE_SEND);
234  out:
235         lnet_return_tx_credits_locked(msg);
236         msg->msg_tx_committed = 0;
237 }
238
239 static void
240 lnet_msg_decommit_rx(struct lnet_msg *msg, int status)
241 {
242         struct lnet_counters_common *common;
243         struct lnet_event *ev = &msg->msg_ev;
244
245         LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
246         LASSERT(msg->msg_rx_committed);
247
248         if (status != 0)
249                 goto out;
250
251         common = &(the_lnet.ln_counters[msg->msg_rx_cpt]->lct_common);
252         switch (ev->type) {
253         default:
254                 LASSERT(ev->type == 0);
255                 LASSERT(msg->msg_routing);
256                 goto incr_stats;
257
258         case LNET_EVENT_ACK:
259                 LASSERT(msg->msg_type == LNET_MSG_ACK);
260                 break;
261
262         case LNET_EVENT_GET:
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;
270                 break;
271
272         case LNET_EVENT_PUT:
273                 LASSERT(msg->msg_type == LNET_MSG_PUT);
274                 break;
275
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);
281                 break;
282         }
283
284         common->lcc_recv_count++;
285
286 incr_stats:
287         if (msg->msg_rxpeer)
288                 lnet_incr_stats(&msg->msg_rxpeer->lpni_stats,
289                                 msg->msg_type,
290                                 LNET_STATS_TYPE_RECV);
291         if (msg->msg_rxni)
292                 lnet_incr_stats(&msg->msg_rxni->ni_stats,
293                                 msg->msg_type,
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;
297
298  out:
299         lnet_return_rx_credits_locked(msg);
300         msg->msg_rx_committed = 0;
301 }
302
303 void
304 lnet_msg_decommit(struct lnet_msg *msg, int cpt, int status)
305 {
306         int     cpt2 = cpt;
307
308         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
309         LASSERT(msg->msg_onactivelist);
310
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);
314         }
315
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;
321                         lnet_net_lock(cpt2);
322                 }
323                 lnet_msg_decommit_rx(msg, status);
324         }
325
326         list_del(&msg->msg_activelist);
327         msg->msg_onactivelist = 0;
328
329         the_lnet.ln_counters[cpt2]->lct_common.lcc_msgs_alloc--;
330
331         if (cpt2 != cpt) {
332                 lnet_net_unlock(cpt2);
333                 lnet_net_lock(cpt);
334         }
335 }
336
337 void
338 lnet_msg_attach_md(struct lnet_msg *msg, struct lnet_libmd *md,
339                    unsigned int offset, unsigned int mlen)
340 {
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);
347
348         msg->msg_md = md;
349         if (msg->msg_receiving) { /* committed for receiving */
350                 msg->msg_offset = offset;
351                 msg->msg_wanted = mlen;
352         }
353
354         md->md_refcount++;
355         if (md->md_threshold != LNET_MD_THRESH_INF) {
356                 LASSERT(md->md_threshold > 0);
357                 md->md_threshold--;
358         }
359
360         /* build umd in event */
361         lnet_md2handle(&msg->msg_ev.md_handle, md);
362         lnet_md_deconstruct(md, &msg->msg_ev);
363 }
364
365 static int
366 lnet_complete_msg_locked(struct lnet_msg *msg, int cpt)
367 {
368         struct lnet_handle_wire ack_wmd;
369         int                rc;
370         int                status = msg->msg_ev.status;
371
372         LASSERT(msg->msg_onactivelist);
373
374         if (status == 0 && msg->msg_ack) {
375                 /* Only send an ACK if the PUT completed successfully */
376
377                 lnet_msg_decommit(msg, cpt, 0);
378
379                 msg->msg_ack = 0;
380                 lnet_net_unlock(cpt);
381
382                 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
383                 LASSERT(!msg->msg_routing);
384
385                 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
386
387                 lnet_prep_send(msg, LNET_MSG_ACK, &msg->msg_ev.source, 0, 0);
388
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);
392
393                 rc = lnet_send(&msg->msg_ev.target.nid, msg,
394                                &msg->msg_from);
395
396                 lnet_net_lock(cpt);
397                 /*
398                  * NB: message is committed for sending, we should return
399                  * on success because LND will finalize this message later.
400                  *
401                  * Also, there is possibility that message is committed for
402                  * sending and also failed before delivering to LND,
403                  * i.e: ENOMEM, in that case we can't fall through either
404                  * because CPT for sending can be different with CPT for
405                  * receiving, so we should return back to lnet_finalize()
406                  * to make sure we are locking the correct partition.
407                  */
408                 return rc;
409
410         } else if (status == 0 &&       /* OK so far */
411                    (msg->msg_routing && !msg->msg_sending)) {
412                 /* not forwarded */
413                 LASSERT(!msg->msg_receiving);   /* called back recv already */
414                 lnet_net_unlock(cpt);
415
416                 rc = lnet_send(NULL, msg, NULL);
417
418                 lnet_net_lock(cpt);
419                 /*
420                  * NB: message is committed for sending, we should return
421                  * on success because LND will finalize this message later.
422                  *
423                  * Also, there is possibility that message is committed for
424                  * sending and also failed before delivering to LND,
425                  * i.e: ENOMEM, in that case we can't fall through either:
426                  * - The rule is message must decommit for sending first if
427                  *   the it's committed for both sending and receiving
428                  * - CPT for sending can be different with CPT for receiving,
429                  *   so we should return back to lnet_finalize() to make
430                  *   sure we are locking the correct partition.
431                  */
432                 return rc;
433         }
434
435         lnet_msg_decommit(msg, cpt, status);
436         lnet_msg_free(msg);
437         return 0;
438 }
439
440 /* must hold net_lock/0 */
441 void
442 lnet_ni_add_to_recoveryq_locked(struct lnet_ni *ni,
443                                 struct list_head *recovery_queue, time64_t now)
444 {
445         if (!list_empty(&ni->ni_recovery))
446                 return;
447
448         if (atomic_read(&ni->ni_healthv) == LNET_MAX_HEALTH_VALUE)
449                 return;
450
451         /* This NI is going on the recovery queue, so take a ref on it */
452         lnet_ni_addref_locked(ni, 0);
453
454         lnet_ni_set_next_ping(ni, now);
455
456         CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld health :%d\n",
457                libcfs_nidstr(&ni->ni_nid),
458                ni->ni_ping_count,
459                ni->ni_next_ping,
460                atomic_read(&ni->ni_healthv));
461
462         list_add_tail(&ni->ni_recovery, recovery_queue);
463 }
464
465 static void
466 lnet_handle_local_failure(struct lnet_ni *local_ni)
467 {
468         /*
469          * the lnet_net_lock(0) is used to protect the addref on the ni
470          * and the recovery queue.
471          */
472         lnet_net_lock(0);
473         /* the mt could've shutdown and cleaned up the queues */
474         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
475                 lnet_net_unlock(0);
476                 return;
477         }
478
479         lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
480         lnet_ni_add_to_recoveryq_locked(local_ni, &the_lnet.ln_mt_localNIRecovq,
481                                         ktime_get_seconds());
482         lnet_net_unlock(0);
483 }
484
485 /* must hold net_lock/0 */
486 void
487 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
488 {
489         lnet_dec_lpni_healthv_locked(lpni);
490
491         /*
492          * add the peer NI to the recovery queue if it's not already there
493          * and it's health value is actually below the maximum. It's
494          * possible that the sensitivity might be set to 0, and the health
495          * value will not be reduced. In this case, there is no reason to
496          * invoke recovery
497          */
498         lnet_peer_ni_add_to_recoveryq_locked(lpni,
499                                              &the_lnet.ln_mt_peerNIRecovq,
500                                              ktime_get_seconds());
501 }
502
503 static void
504 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
505 {
506         /* lpni could be NULL if we're in the LOLND case */
507         if (!lpni)
508                 return;
509
510         lnet_net_lock(0);
511         /* the mt could've shutdown and cleaned up the queues */
512         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
513                 lnet_net_unlock(0);
514                 return;
515         }
516         lnet_handle_remote_failure_locked(lpni);
517         lnet_net_unlock(0);
518 }
519
520 static void
521 lnet_incr_hstats(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
522                  enum lnet_msg_hstatus hstatus)
523 {
524         struct lnet_counters_health *health;
525
526         health = &the_lnet.ln_counters[0]->lct_health;
527
528         switch (hstatus) {
529         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
530                 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
531                 health->lch_local_interrupt_count++;
532                 break;
533         case LNET_MSG_STATUS_LOCAL_DROPPED:
534                 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
535                 health->lch_local_dropped_count++;
536                 break;
537         case LNET_MSG_STATUS_LOCAL_ABORTED:
538                 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
539                 health->lch_local_aborted_count++;
540                 break;
541         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
542                 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
543                 health->lch_local_no_route_count++;
544                 break;
545         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
546                 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
547                 health->lch_local_timeout_count++;
548                 break;
549         case LNET_MSG_STATUS_LOCAL_ERROR:
550                 atomic_inc(&ni->ni_hstats.hlt_local_error);
551                 health->lch_local_error_count++;
552                 break;
553         case LNET_MSG_STATUS_REMOTE_DROPPED:
554                 if (lpni)
555                         atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
556                 health->lch_remote_dropped_count++;
557                 break;
558         case LNET_MSG_STATUS_REMOTE_ERROR:
559                 if (lpni)
560                         atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
561                 health->lch_remote_error_count++;
562                 break;
563         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
564                 if (lpni)
565                         atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
566                 health->lch_remote_timeout_count++;
567                 break;
568         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
569                 if (lpni)
570                         atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
571                 health->lch_network_timeout_count++;
572                 break;
573         case LNET_MSG_STATUS_OK:
574                 break;
575         default:
576                 LBUG();
577         }
578 }
579
580 static void
581 lnet_resend_msg_locked(struct lnet_msg *msg)
582 {
583         msg->msg_retry_count++;
584
585         /*
586          * remove message from the active list and reset it to prepare
587          * for a resend. Two exceptions to this
588          *
589          * 1. the router case. When a message is being routed it is
590          * committed for rx when received and committed for tx when
591          * forwarded. We don't want to remove it from the active list, since
592          * code which handles receiving expects it to remain on the active
593          * list.
594          *
595          * 2. The REPLY case. Reply messages use the same message
596          * structure for the GET that was received.
597          */
598         if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
599                 list_del_init(&msg->msg_activelist);
600                 msg->msg_onactivelist = 0;
601         }
602         /*
603          * The msg_target.nid which was originally set
604          * when calling LNetGet() or LNetPut() might've
605          * been overwritten if we're routing this message.
606          * Call lnet_msg_decommit_tx() to return the credit
607          * this message consumed. The message will
608          * consume another credit when it gets resent.
609          */
610         msg->msg_target.nid = msg->msg_hdr.dest_nid;
611         lnet_msg_decommit_tx(msg, -EAGAIN);
612         msg->msg_sending = 0;
613         msg->msg_receiving = 0;
614         msg->msg_target_is_router = 0;
615
616         CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
617                libcfs_nidstr(&msg->msg_hdr.src_nid),
618                libcfs_nidstr(&msg->msg_hdr.dest_nid),
619                lnet_msgtyp2str(msg->msg_type),
620                lnet_health_error2str(msg->msg_health_status), msg);
621
622         list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
623
624         complete(&the_lnet.ln_mt_wait_complete);
625 }
626
627 int
628 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
629                                      struct list_head *containerq,
630                                      int nworkers, void **workers)
631 {
632         int my_slot = -1;
633         int i;
634
635         list_add_tail(&msg->msg_list, containerq);
636
637         for (i = 0; i < nworkers; i++) {
638                 if (workers[i] == current)
639                         break;
640
641                 if (my_slot < 0 && workers[i] == NULL)
642                         my_slot = i;
643         }
644
645         if (i < nworkers || my_slot < 0)
646                 return -1;
647
648         workers[my_slot] = current;
649
650         return my_slot;
651 }
652
653 int
654 lnet_attempt_msg_resend(struct lnet_msg *msg)
655 {
656         struct lnet_msg_container *container;
657         int my_slot;
658         int cpt;
659
660         /* we can only resend tx_committed messages */
661         LASSERT(msg->msg_tx_committed);
662
663         /* don't resend recovery messages */
664         if (msg->msg_recovery) {
665                 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
666                         libcfs_nidstr(&msg->msg_from),
667                         libcfs_nidstr(&msg->msg_target.nid),
668                         msg->msg_retry_count);
669                 return -ENOTRECOVERABLE;
670         }
671
672         /*
673          * if we explicitly indicated we don't want to resend then just
674          * return
675          */
676         if (msg->msg_no_resend) {
677                 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
678                         libcfs_nidstr(&msg->msg_from),
679                         libcfs_nidstr(&msg->msg_target.nid),
680                         msg->msg_retry_count);
681                 return -ENOTRECOVERABLE;
682         }
683
684         /* check if the message has exceeded the number of retries */
685         if (msg->msg_retry_count >= lnet_retry_count) {
686                 CNETERR("msg %s->%s exceeded retry count %d\n",
687                         libcfs_nidstr(&msg->msg_from),
688                         libcfs_nidstr(&msg->msg_target.nid),
689                         msg->msg_retry_count);
690                 return -ENOTRECOVERABLE;
691         }
692
693         cpt = msg->msg_tx_cpt;
694         lnet_net_lock(cpt);
695
696         /* check again under lock */
697         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
698                 lnet_net_unlock(cpt);
699                 return -ESHUTDOWN;
700         }
701
702         container = the_lnet.ln_msg_containers[cpt];
703         my_slot =
704                 lnet_check_finalize_recursion_locked(msg,
705                                         &container->msc_resending,
706                                         container->msc_nfinalizers,
707                                         container->msc_resenders);
708
709         /* enough threads are resending */
710         if (my_slot == -1) {
711                 lnet_net_unlock(cpt);
712                 return 0;
713         }
714
715         while ((msg = list_first_entry_or_null(&container->msc_resending,
716                                                struct lnet_msg,
717                                                msg_list)) != NULL) {
718                 list_del(&msg->msg_list);
719
720                 /*
721                  * resending the message will require us to call
722                  * lnet_msg_decommit_tx() which will return the credit
723                  * which this message holds. This could trigger another
724                  * queued message to be sent. If that message fails and
725                  * requires a resend we will recurse.
726                  * But since at this point the slot is taken, the message
727                  * will be queued in the container and dealt with
728                  * later. This breaks the recursion.
729                  */
730                 lnet_resend_msg_locked(msg);
731         }
732
733         /*
734          * msc_resenders is an array of process pointers. Each entry holds
735          * a pointer to the current process operating on the message. An
736          * array entry is created per CPT. If the array slot is already
737          * set, then it means that there is a thread on the CPT currently
738          * resending a message.
739          * Once the thread finishes clear the slot to enable the thread to
740          * take on more resend work.
741          */
742         container->msc_resenders[my_slot] = NULL;
743         lnet_net_unlock(cpt);
744
745         return 0;
746 }
747
748 /*
749  * Do a health check on the message:
750  * return -1 if we're not going to handle the error or
751  *   if we've reached the maximum number of retries.
752  *   success case will return -1 as well
753  * return 0 if it the message is requeued for send
754  */
755 static int
756 lnet_health_check(struct lnet_msg *msg)
757 {
758         enum lnet_msg_hstatus hstatus = msg->msg_health_status;
759         struct lnet_peer_ni *lpni;
760         struct lnet_ni *ni;
761         bool lo = false;
762         bool attempt_local_resend;
763         bool attempt_remote_resend;
764         bool handle_local_health;
765         bool handle_remote_health;
766
767         /* if we're shutting down no point in handling health. */
768         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
769                 return -1;
770
771         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
772
773         /*
774          * if we're sending to the LOLND then the msg_txpeer will not be
775          * set. So no need to sanity check it.
776          */
777         if (msg->msg_tx_committed &&
778             !nid_is_lo0(&msg->msg_txni->ni_nid))
779                 LASSERT(msg->msg_txpeer);
780         else if (msg->msg_tx_committed &&
781                  nid_is_lo0(&msg->msg_txni->ni_nid))
782                 lo = true;
783
784         if (hstatus != LNET_MSG_STATUS_OK &&
785             ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
786                 return -1;
787
788         /*
789          * always prefer txni/txpeer if they message is committed for both
790          * directions.
791          */
792         if (msg->msg_tx_committed) {
793                 ni = msg->msg_txni;
794                 lpni = msg->msg_txpeer;
795                 attempt_local_resend = attempt_remote_resend = true;
796         } else {
797                 ni = msg->msg_rxni;
798                 lpni = msg->msg_rxpeer;
799                 attempt_local_resend = attempt_remote_resend = false;
800         }
801
802         if (!lo)
803                 LASSERT(ni && lpni);
804         else
805                 LASSERT(ni);
806
807         CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
808                libcfs_nidstr(&ni->ni_nid),
809                (lo) ? "self" : libcfs_nidstr(&lpni->lpni_nid),
810                lnet_msgtyp2str(msg->msg_type),
811                lnet_health_error2str(hstatus));
812
813         /*
814          * stats are only incremented for errors so avoid wasting time
815          * incrementing statistics if there is no error. Similarly, whether to
816          * update health values or perform resends is only applicable for
817          * messages with a health status != OK.
818          */
819         if (hstatus != LNET_MSG_STATUS_OK) {
820                 /* Don't further decrement the health value if a recovery
821                  * message failed.
822                  */
823                 if (msg->msg_recovery)
824                         handle_local_health = handle_remote_health = false;
825                 else
826                         handle_local_health = handle_remote_health = true;
827
828                 /* For local failures, health/recovery/resends are not needed if
829                  * I only have a single (non-lolnd) interface. NB: pb_nnis
830                  * includes the lolnd interface, so a single-rail node would
831                  * have pb_nnis == 2.
832                  */
833                 if (the_lnet.ln_ping_target->pb_nnis <= 2) {
834                         handle_local_health = false;
835                         attempt_local_resend = false;
836                 }
837
838                 lnet_net_lock(0);
839                 lnet_incr_hstats(ni, lpni, hstatus);
840                 /* For remote failures, health/recovery/resends are not needed
841                  * if the peer only has a single interface. Special case for
842                  * routers where we rely on health feature to manage route
843                  * aliveness. NB: unlike pb_nnis above, lp_nnis does _not_
844                  * include the lolnd, so a single-rail node would have
845                  * lp_nnis == 1.
846                  */
847                 if (lpni && lpni->lpni_peer_net &&
848                     lpni->lpni_peer_net->lpn_peer &&
849                     lpni->lpni_peer_net->lpn_peer->lp_nnis <= 1) {
850                         attempt_remote_resend = false;
851                         if (!lnet_isrouter(lpni))
852                                 handle_remote_health = false;
853                 }
854                 /* Do not put my interfaces into peer NI recovery. They should
855                  * be handled with local NI recovery.
856                  */
857                 if (handle_remote_health && lpni &&
858                     lnet_nid_to_ni_locked(&lpni->lpni_nid, 0))
859                         handle_remote_health = false;
860                 lnet_net_unlock(0);
861         }
862
863         switch (hstatus) {
864         case LNET_MSG_STATUS_OK:
865                 /*
866                  * increment the local ni health whether we successfully
867                  * received or sent a message on it.
868                  *
869                  * Ping counts are reset to 0 as appropriate to allow for
870                  * faster recovery.
871                  */
872                 lnet_inc_healthv(&ni->ni_healthv, lnet_health_sensitivity);
873                 /*
874                  * It's possible msg_txpeer is NULL in the LOLND
875                  * case. Only increment the peer's health if we're
876                  * receiving a message from it. It's the only sure way to
877                  * know that a remote interface is up.
878                  * If this interface is part of a router, then take that
879                  * as indication that the router is fully healthy.
880                  */
881                 if (lpni && msg->msg_rx_committed) {
882                         lnet_net_lock(0);
883                         lpni->lpni_ping_count = 0;
884                         ni->ni_ping_count = 0;
885                         /*
886                          * If we're receiving a message from the router or
887                          * I'm a router, then set that lpni's health to
888                          * maximum so we can commence communication
889                          */
890                         if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
891                                 lnet_set_lpni_healthv_locked(lpni,
892                                         LNET_MAX_HEALTH_VALUE);
893                         } else {
894                                 lnet_inc_lpni_healthv_locked(lpni);
895                                 /* This peer NI may have previously aged out
896                                  * of recovery. Now that we've received a
897                                  * message from it, we can continue recovery
898                                  * if its health value is still below the
899                                  * maximum.
900                                  */
901                                 lnet_peer_ni_add_to_recoveryq_locked(lpni,
902                                                 &the_lnet.ln_mt_peerNIRecovq,
903                                                 ktime_get_seconds());
904                         }
905                         lnet_net_unlock(0);
906                 }
907
908                 /* we can finalize this message */
909                 return -1;
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);
919                 break;
920         case LNET_MSG_STATUS_LOCAL_ERROR:
921                 if (handle_local_health)
922                         lnet_handle_local_failure(ni);
923                 return -1;
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);
929                 break;
930         case LNET_MSG_STATUS_REMOTE_ERROR:
931         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
932                 if (handle_remote_health)
933                         lnet_handle_remote_failure(lpni);
934                 return -1;
935         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
936                 if (handle_remote_health)
937                         lnet_handle_remote_failure(lpni);
938                 if (handle_local_health)
939                         lnet_handle_local_failure(ni);
940                 return -1;
941         default:
942                 LBUG();
943         }
944
945         /* no resend is needed */
946         return -1;
947 }
948
949 static void
950 lnet_msg_detach_md(struct lnet_msg *msg, int status)
951 {
952         struct lnet_libmd *md = msg->msg_md;
953         lnet_handler_t handler = NULL;
954         int cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
955         int unlink;
956
957         lnet_res_lock(cpt);
958         while (md->md_flags & LNET_MD_FLAG_HANDLING)
959                 /* An event handler is running - wait for it to
960                  * complete to avoid races.
961                  */
962                 lnet_md_wait_handling(md, cpt);
963
964         /* Now it's safe to drop my caller's ref */
965         md->md_refcount--;
966         LASSERT(md->md_refcount >= 0);
967
968         unlink = lnet_md_unlinkable(md);
969         if (md->md_handler) {
970                 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
971                         msg->msg_ev.status   = -ETIMEDOUT;
972                         CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
973                 } else {
974                         msg->msg_ev.status   = status;
975                 }
976                 msg->msg_ev.unlinked = unlink;
977                 handler = md->md_handler;
978                 if (!unlink)
979                         md->md_flags |= LNET_MD_FLAG_HANDLING;
980         }
981
982         if (unlink || (md->md_refcount == 0 &&
983                        md->md_threshold == LNET_MD_THRESH_INF))
984                 lnet_detach_rsp_tracker(md, cpt);
985
986         msg->msg_md = NULL;
987         if (unlink)
988                 lnet_md_unlink(md);
989
990         lnet_res_unlock(cpt);
991
992         if (handler) {
993                 handler(&msg->msg_ev);
994                 if (!unlink) {
995                         lnet_res_lock(cpt);
996                         md->md_flags &= ~LNET_MD_FLAG_HANDLING;
997                         wake_up_var(md);
998                         lnet_res_unlock(cpt);
999                 }
1000         }
1001 }
1002
1003 static bool
1004 lnet_is_health_check(struct lnet_msg *msg)
1005 {
1006         bool hc = true;
1007         int status = msg->msg_ev.status;
1008
1009         if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
1010             !msg->msg_onactivelist) {
1011                 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
1012                        msg);
1013                 return false;
1014         }
1015
1016         if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
1017             (msg->msg_rx_committed && !msg->msg_rxpeer)) {
1018                 /* The optimized GET case does not set msg_rxpeer, but status
1019                  * could be zero. Only print the error message if we have a
1020                  * non-zero status.
1021                  */
1022                 if (status)
1023                         CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
1024                                status);
1025                 return false;
1026         }
1027
1028         /* Check for status inconsistencies */
1029         if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
1030              (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
1031                 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
1032                        "checking (%d, %d)\n", msg, status,
1033                        msg->msg_health_status);
1034                 hc = false;
1035         }
1036
1037         CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
1038                hc, status, msg->msg_health_status);
1039
1040         return hc;
1041 }
1042
1043 char *
1044 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
1045 {
1046         switch (hstatus) {
1047         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
1048                 return "LOCAL_INTERRUPT";
1049         case LNET_MSG_STATUS_LOCAL_DROPPED:
1050                 return "LOCAL_DROPPED";
1051         case LNET_MSG_STATUS_LOCAL_ABORTED:
1052                 return "LOCAL_ABORTED";
1053         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
1054                 return "LOCAL_NO_ROUTE";
1055         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
1056                 return "LOCAL_TIMEOUT";
1057         case LNET_MSG_STATUS_LOCAL_ERROR:
1058                 return "LOCAL_ERROR";
1059         case LNET_MSG_STATUS_REMOTE_DROPPED:
1060                 return "REMOTE_DROPPED";
1061         case LNET_MSG_STATUS_REMOTE_ERROR:
1062                 return "REMOTE_ERROR";
1063         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1064                 return "REMOTE_TIMEOUT";
1065         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1066                 return "NETWORK_TIMEOUT";
1067         case LNET_MSG_STATUS_OK:
1068                 return "OK";
1069         default:
1070                 return "<UNKNOWN>";
1071         }
1072 }
1073
1074 bool
1075 lnet_send_error_simulation(struct lnet_msg *msg,
1076                            enum lnet_msg_hstatus *hstatus)
1077 {
1078         if (!msg)
1079                 return false;
1080
1081         if (list_empty(&the_lnet.ln_drop_rules))
1082             return false;
1083
1084         /* match only health rules */
1085         if (!lnet_drop_rule_match(&msg->msg_hdr, NULL, hstatus))
1086                 return false;
1087
1088         CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1089                 libcfs_nidstr(&msg->msg_hdr.src_nid),
1090                 libcfs_nidstr(&msg->msg_txni->ni_nid),
1091                 libcfs_nidstr(&msg->msg_hdr.dest_nid),
1092                 lnet_msgtyp2str(msg->msg_type),
1093                 lnet_health_error2str(*hstatus));
1094
1095         return true;
1096 }
1097 EXPORT_SYMBOL(lnet_send_error_simulation);
1098
1099 void
1100 lnet_finalize(struct lnet_msg *msg, int status)
1101 {
1102         struct lnet_msg_container *container;
1103         int my_slot;
1104         int cpt;
1105         int rc;
1106
1107         LASSERT(!in_interrupt());
1108
1109         if (msg == NULL)
1110                 return;
1111
1112         msg->msg_ev.status = status;
1113
1114         if (lnet_is_health_check(msg)) {
1115                 /*
1116                  * Check the health status of the message. If it has one
1117                  * of the errors that we're supposed to handle, and it has
1118                  * not timed out, then
1119                  *      1. Decrement the appropriate health_value
1120                  *      2. queue the message on the resend queue
1121
1122                  * if the message send is success, timed out or failed in the
1123                  * health check for any reason then we'll just finalize the
1124                  * message. Otherwise just return since the message has been
1125                  * put on the resend queue.
1126                  */
1127                 if (!lnet_health_check(msg))
1128                         return;
1129         }
1130
1131         /*
1132          * We're not going to resend this message so detach its MD and invoke
1133          * the appropriate callbacks
1134          */
1135         if (msg->msg_md != NULL)
1136                 lnet_msg_detach_md(msg, status);
1137
1138 again:
1139         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1140                 /* not committed to network yet */
1141                 LASSERT(!msg->msg_onactivelist);
1142                 lnet_msg_free(msg);
1143                 return;
1144         }
1145
1146         /*
1147          * NB: routed message can be committed for both receiving and sending,
1148          * we should finalize in LIFO order and keep counters correct.
1149          * (finalize sending first then finalize receiving)
1150          */
1151         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1152         lnet_net_lock(cpt);
1153
1154         container = the_lnet.ln_msg_containers[cpt];
1155
1156         /* Recursion breaker.  Don't complete the message here if I am (or
1157          * enough other threads are) already completing messages */
1158         my_slot = lnet_check_finalize_recursion_locked(msg,
1159                                                 &container->msc_finalizing,
1160                                                 container->msc_nfinalizers,
1161                                                 container->msc_finalizers);
1162
1163         /* enough threads are resending */
1164         if (my_slot == -1) {
1165                 lnet_net_unlock(cpt);
1166                 return;
1167         }
1168
1169         rc = 0;
1170         while ((msg = list_first_entry_or_null(&container->msc_finalizing,
1171                                                struct lnet_msg,
1172                                                msg_list)) != NULL) {
1173                 list_del_init(&msg->msg_list);
1174
1175                 /* NB drops and regains the lnet lock if it actually does
1176                  * anything, so my finalizing friends can chomp along too */
1177                 rc = lnet_complete_msg_locked(msg, cpt);
1178                 if (rc != 0)
1179                         break;
1180         }
1181
1182         if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1183                 lnet_net_unlock(cpt);
1184                 lnet_delay_rule_check();
1185                 lnet_net_lock(cpt);
1186         }
1187
1188         container->msc_finalizers[my_slot] = NULL;
1189         lnet_net_unlock(cpt);
1190
1191         if (rc != 0)
1192                 goto again;
1193 }
1194 EXPORT_SYMBOL(lnet_finalize);
1195
1196 void
1197 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1198 {
1199         struct lnet_msg *msg;
1200         int count = 0;
1201
1202         if (container->msc_init == 0)
1203                 return;
1204
1205         while ((msg = list_first_entry_or_null(&container->msc_active,
1206                                                struct lnet_msg,
1207                                                msg_activelist)) != NULL) {
1208                 LASSERT(msg->msg_onactivelist);
1209                 msg->msg_onactivelist = 0;
1210                 list_del_init(&msg->msg_activelist);
1211                 lnet_msg_free(msg);
1212                 count++;
1213         }
1214
1215         if (count > 0)
1216                 CERROR("%d active msg on exit\n", count);
1217
1218         if (container->msc_finalizers != NULL) {
1219                 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1220                                    container->msc_nfinalizers);
1221                 container->msc_finalizers = NULL;
1222         }
1223
1224         if (container->msc_resenders != NULL) {
1225                 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1226                                    container->msc_nfinalizers);
1227                 container->msc_resenders = NULL;
1228         }
1229         container->msc_init = 0;
1230 }
1231
1232 int
1233 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1234 {
1235         int rc = 0;
1236
1237         container->msc_init = 1;
1238
1239         INIT_LIST_HEAD(&container->msc_active);
1240         INIT_LIST_HEAD(&container->msc_finalizing);
1241         INIT_LIST_HEAD(&container->msc_resending);
1242
1243         /* number of CPUs */
1244         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1245         if (container->msc_nfinalizers == 0)
1246                 container->msc_nfinalizers = 1;
1247
1248         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1249                          container->msc_nfinalizers *
1250                          sizeof(*container->msc_finalizers));
1251
1252         if (container->msc_finalizers == NULL) {
1253                 CERROR("Failed to allocate message finalizers\n");
1254                 lnet_msg_container_cleanup(container);
1255                 return -ENOMEM;
1256         }
1257
1258         LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1259                          container->msc_nfinalizers *
1260                          sizeof(*container->msc_resenders));
1261
1262         if (container->msc_resenders == NULL) {
1263                 CERROR("Failed to allocate message resenders\n");
1264                 lnet_msg_container_cleanup(container);
1265                 return -ENOMEM;
1266         }
1267
1268         return rc;
1269 }
1270
1271 void
1272 lnet_msg_containers_destroy(void)
1273 {
1274         struct lnet_msg_container *container;
1275         int     i;
1276
1277         if (the_lnet.ln_msg_containers == NULL)
1278                 return;
1279
1280         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1281                 lnet_msg_container_cleanup(container);
1282
1283         cfs_percpt_free(the_lnet.ln_msg_containers);
1284         the_lnet.ln_msg_containers = NULL;
1285 }
1286
1287 int
1288 lnet_msg_containers_create(void)
1289 {
1290         struct lnet_msg_container *container;
1291         int     rc;
1292         int     i;
1293
1294         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1295                                                       sizeof(*container));
1296
1297         if (the_lnet.ln_msg_containers == NULL) {
1298                 CERROR("Failed to allocate cpu-partition data for network\n");
1299                 return -ENOMEM;
1300         }
1301
1302         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1303                 rc = lnet_msg_container_setup(container, i);
1304                 if (rc != 0) {
1305                         lnet_msg_containers_destroy();
1306                         return rc;
1307                 }
1308         }
1309
1310         return 0;
1311 }