Whamcloud - gitweb
f23d7d3a44c5c6bab2b418f6dcb2f6e7bd5abc4e
[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    = 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;
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, msg->msg_from);
394
395                 lnet_net_lock(cpt);
396                 /*
397                  * NB: message is committed for sending, we should return
398                  * on success because LND will finalize this message later.
399                  *
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.
406                  */
407                 return rc;
408
409         } else if (status == 0 &&       /* OK so far */
410                    (msg->msg_routing && !msg->msg_sending)) {
411                 /* not forwarded */
412                 LASSERT(!msg->msg_receiving);   /* called back recv already */
413                 lnet_net_unlock(cpt);
414
415                 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
416
417                 lnet_net_lock(cpt);
418                 /*
419                  * NB: message is committed for sending, we should return
420                  * on success because LND will finalize this message later.
421                  *
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.
430                  */
431                 return rc;
432         }
433
434         lnet_msg_decommit(msg, cpt, status);
435         lnet_msg_free(msg);
436         return 0;
437 }
438
439 static void
440 lnet_dec_healthv_locked(atomic_t *healthv, int sensitivity)
441 {
442         int h = atomic_read(healthv);
443
444         if (h < sensitivity) {
445                 atomic_set(healthv, 0);
446         } else {
447                 h -= sensitivity;
448                 atomic_set(healthv, h);
449         }
450 }
451
452 /* must hold net_lock/0 */
453 void
454 lnet_ni_add_to_recoveryq_locked(struct lnet_ni *ni,
455                                 struct list_head *recovery_queue, time64_t now)
456 {
457         if (!list_empty(&ni->ni_recovery))
458                 return;
459
460         if (atomic_read(&ni->ni_healthv) == LNET_MAX_HEALTH_VALUE)
461                 return;
462
463         /* This NI is going on the recovery queue, so take a ref on it */
464         lnet_ni_addref_locked(ni, 0);
465
466         lnet_ni_set_next_ping(ni, now);
467
468         CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld health :%d\n",
469                libcfs_nid2str(ni->ni_nid),
470                ni->ni_ping_count,
471                ni->ni_next_ping,
472                atomic_read(&ni->ni_healthv));
473
474         list_add_tail(&ni->ni_recovery, recovery_queue);
475 }
476
477 static void
478 lnet_handle_local_failure(struct lnet_ni *local_ni)
479 {
480         /*
481          * the lnet_net_lock(0) is used to protect the addref on the ni
482          * and the recovery queue.
483          */
484         lnet_net_lock(0);
485         /* the mt could've shutdown and cleaned up the queues */
486         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
487                 lnet_net_unlock(0);
488                 return;
489         }
490
491         lnet_dec_healthv_locked(&local_ni->ni_healthv, lnet_health_sensitivity);
492         lnet_ni_add_to_recoveryq_locked(local_ni, &the_lnet.ln_mt_localNIRecovq,
493                                         ktime_get_seconds());
494         lnet_net_unlock(0);
495 }
496
497 /* must hold net_lock/0 */
498 void
499 lnet_handle_remote_failure_locked(struct lnet_peer_ni *lpni)
500 {
501         __u32 sensitivity = lnet_health_sensitivity;
502         __u32 lp_sensitivity;
503
504         /*
505          * If there is a health sensitivity in the peer then use that
506          * instead of the globally set one.
507          */
508         lp_sensitivity = lpni->lpni_peer_net->lpn_peer->lp_health_sensitivity;
509         if (lp_sensitivity)
510                 sensitivity = lp_sensitivity;
511
512         lnet_dec_healthv_locked(&lpni->lpni_healthv, sensitivity);
513
514         /* update the peer_net's health value */
515         lnet_update_peer_net_healthv(lpni);
516
517         /*
518          * add the peer NI to the recovery queue if it's not already there
519          * and it's health value is actually below the maximum. It's
520          * possible that the sensitivity might be set to 0, and the health
521          * value will not be reduced. In this case, there is no reason to
522          * invoke recovery
523          */
524         lnet_peer_ni_add_to_recoveryq_locked(lpni,
525                                              &the_lnet.ln_mt_peerNIRecovq,
526                                              ktime_get_seconds());
527 }
528
529 static void
530 lnet_handle_remote_failure(struct lnet_peer_ni *lpni)
531 {
532         /* lpni could be NULL if we're in the LOLND case */
533         if (!lpni)
534                 return;
535
536         lnet_net_lock(0);
537         /* the mt could've shutdown and cleaned up the queues */
538         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
539                 lnet_net_unlock(0);
540                 return;
541         }
542         lnet_handle_remote_failure_locked(lpni);
543         lnet_net_unlock(0);
544 }
545
546 static void
547 lnet_incr_hstats(struct lnet_ni *ni, struct lnet_peer_ni *lpni,
548                  enum lnet_msg_hstatus hstatus)
549 {
550         struct lnet_counters_health *health;
551
552         health = &the_lnet.ln_counters[0]->lct_health;
553
554         switch (hstatus) {
555         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
556                 atomic_inc(&ni->ni_hstats.hlt_local_interrupt);
557                 health->lch_local_interrupt_count++;
558                 break;
559         case LNET_MSG_STATUS_LOCAL_DROPPED:
560                 atomic_inc(&ni->ni_hstats.hlt_local_dropped);
561                 health->lch_local_dropped_count++;
562                 break;
563         case LNET_MSG_STATUS_LOCAL_ABORTED:
564                 atomic_inc(&ni->ni_hstats.hlt_local_aborted);
565                 health->lch_local_aborted_count++;
566                 break;
567         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
568                 atomic_inc(&ni->ni_hstats.hlt_local_no_route);
569                 health->lch_local_no_route_count++;
570                 break;
571         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
572                 atomic_inc(&ni->ni_hstats.hlt_local_timeout);
573                 health->lch_local_timeout_count++;
574                 break;
575         case LNET_MSG_STATUS_LOCAL_ERROR:
576                 atomic_inc(&ni->ni_hstats.hlt_local_error);
577                 health->lch_local_error_count++;
578                 break;
579         case LNET_MSG_STATUS_REMOTE_DROPPED:
580                 if (lpni)
581                         atomic_inc(&lpni->lpni_hstats.hlt_remote_dropped);
582                 health->lch_remote_dropped_count++;
583                 break;
584         case LNET_MSG_STATUS_REMOTE_ERROR:
585                 if (lpni)
586                         atomic_inc(&lpni->lpni_hstats.hlt_remote_error);
587                 health->lch_remote_error_count++;
588                 break;
589         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
590                 if (lpni)
591                         atomic_inc(&lpni->lpni_hstats.hlt_remote_timeout);
592                 health->lch_remote_timeout_count++;
593                 break;
594         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
595                 if (lpni)
596                         atomic_inc(&lpni->lpni_hstats.hlt_network_timeout);
597                 health->lch_network_timeout_count++;
598                 break;
599         case LNET_MSG_STATUS_OK:
600                 break;
601         default:
602                 LBUG();
603         }
604 }
605
606 static void
607 lnet_resend_msg_locked(struct lnet_msg *msg)
608 {
609         msg->msg_retry_count++;
610
611         /*
612          * remove message from the active list and reset it to prepare
613          * for a resend. Two exceptions to this
614          *
615          * 1. the router case. When a message is being routed it is
616          * committed for rx when received and committed for tx when
617          * forwarded. We don't want to remove it from the active list, since
618          * code which handles receiving expects it to remain on the active
619          * list.
620          *
621          * 2. The REPLY case. Reply messages use the same message
622          * structure for the GET that was received.
623          */
624         if (!msg->msg_routing && msg->msg_type != LNET_MSG_REPLY) {
625                 list_del_init(&msg->msg_activelist);
626                 msg->msg_onactivelist = 0;
627         }
628         /*
629          * The msg_target.nid which was originally set
630          * when calling LNetGet() or LNetPut() might've
631          * been overwritten if we're routing this message.
632          * Call lnet_msg_decommit_tx() to return the credit
633          * this message consumed. The message will
634          * consume another credit when it gets resent.
635          */
636         msg->msg_target.nid = msg->msg_hdr.dest_nid;
637         lnet_msg_decommit_tx(msg, -EAGAIN);
638         msg->msg_sending = 0;
639         msg->msg_receiving = 0;
640         msg->msg_target_is_router = 0;
641
642         CDEBUG(D_NET, "%s->%s:%s:%s - queuing msg (%p) for resend\n",
643                libcfs_nid2str(msg->msg_hdr.src_nid),
644                libcfs_nid2str(msg->msg_hdr.dest_nid),
645                lnet_msgtyp2str(msg->msg_type),
646                lnet_health_error2str(msg->msg_health_status), msg);
647
648         list_add_tail(&msg->msg_list, the_lnet.ln_mt_resendqs[msg->msg_tx_cpt]);
649
650         complete(&the_lnet.ln_mt_wait_complete);
651 }
652
653 int
654 lnet_check_finalize_recursion_locked(struct lnet_msg *msg,
655                                      struct list_head *containerq,
656                                      int nworkers, void **workers)
657 {
658         int my_slot = -1;
659         int i;
660
661         list_add_tail(&msg->msg_list, containerq);
662
663         for (i = 0; i < nworkers; i++) {
664                 if (workers[i] == current)
665                         break;
666
667                 if (my_slot < 0 && workers[i] == NULL)
668                         my_slot = i;
669         }
670
671         if (i < nworkers || my_slot < 0)
672                 return -1;
673
674         workers[my_slot] = current;
675
676         return my_slot;
677 }
678
679 int
680 lnet_attempt_msg_resend(struct lnet_msg *msg)
681 {
682         struct lnet_msg_container *container;
683         int my_slot;
684         int cpt;
685
686         /* we can only resend tx_committed messages */
687         LASSERT(msg->msg_tx_committed);
688
689         /* don't resend recovery messages */
690         if (msg->msg_recovery) {
691                 CDEBUG(D_NET, "msg %s->%s is a recovery ping. retry# %d\n",
692                         libcfs_nid2str(msg->msg_from),
693                         libcfs_nid2str(msg->msg_target.nid),
694                         msg->msg_retry_count);
695                 return -ENOTRECOVERABLE;
696         }
697
698         /*
699          * if we explicitly indicated we don't want to resend then just
700          * return
701          */
702         if (msg->msg_no_resend) {
703                 CDEBUG(D_NET, "msg %s->%s requested no resend. retry# %d\n",
704                         libcfs_nid2str(msg->msg_from),
705                         libcfs_nid2str(msg->msg_target.nid),
706                         msg->msg_retry_count);
707                 return -ENOTRECOVERABLE;
708         }
709
710         /* check if the message has exceeded the number of retries */
711         if (msg->msg_retry_count >= lnet_retry_count) {
712                 CNETERR("msg %s->%s exceeded retry count %d\n",
713                         libcfs_nid2str(msg->msg_from),
714                         libcfs_nid2str(msg->msg_target.nid),
715                         msg->msg_retry_count);
716                 return -ENOTRECOVERABLE;
717         }
718
719         cpt = msg->msg_tx_cpt;
720         lnet_net_lock(cpt);
721
722         /* check again under lock */
723         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING) {
724                 lnet_net_unlock(cpt);
725                 return -ESHUTDOWN;
726         }
727
728         container = the_lnet.ln_msg_containers[cpt];
729         my_slot =
730                 lnet_check_finalize_recursion_locked(msg,
731                                         &container->msc_resending,
732                                         container->msc_nfinalizers,
733                                         container->msc_resenders);
734
735         /* enough threads are resending */
736         if (my_slot == -1) {
737                 lnet_net_unlock(cpt);
738                 return 0;
739         }
740
741         while (!list_empty(&container->msc_resending)) {
742                 msg = list_entry(container->msc_resending.next,
743                                         struct lnet_msg, msg_list);
744                 list_del(&msg->msg_list);
745
746                 /*
747                  * resending the message will require us to call
748                  * lnet_msg_decommit_tx() which will return the credit
749                  * which this message holds. This could trigger another
750                  * queued message to be sent. If that message fails and
751                  * requires a resend we will recurse.
752                  * But since at this point the slot is taken, the message
753                  * will be queued in the container and dealt with
754                  * later. This breaks the recursion.
755                  */
756                 lnet_resend_msg_locked(msg);
757         }
758
759         /*
760          * msc_resenders is an array of process pointers. Each entry holds
761          * a pointer to the current process operating on the message. An
762          * array entry is created per CPT. If the array slot is already
763          * set, then it means that there is a thread on the CPT currently
764          * resending a message.
765          * Once the thread finishes clear the slot to enable the thread to
766          * take on more resend work.
767          */
768         container->msc_resenders[my_slot] = NULL;
769         lnet_net_unlock(cpt);
770
771         return 0;
772 }
773
774 /*
775  * Do a health check on the message:
776  * return -1 if we're not going to handle the error or
777  *   if we've reached the maximum number of retries.
778  *   success case will return -1 as well
779  * return 0 if it the message is requeued for send
780  */
781 static int
782 lnet_health_check(struct lnet_msg *msg)
783 {
784         enum lnet_msg_hstatus hstatus = msg->msg_health_status;
785         struct lnet_peer_ni *lpni;
786         struct lnet_ni *ni;
787         bool lo = false;
788         bool attempt_local_resend;
789         bool attempt_remote_resend;
790         bool handle_local_health;
791         bool handle_remote_health;
792
793         /* if we're shutting down no point in handling health. */
794         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
795                 return -1;
796
797         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
798
799         /*
800          * if we're sending to the LOLND then the msg_txpeer will not be
801          * set. So no need to sanity check it.
802          */
803         if (msg->msg_tx_committed && msg->msg_txni->ni_nid != LNET_NID_LO_0)
804                 LASSERT(msg->msg_txpeer);
805         else if (msg->msg_tx_committed &&
806                  msg->msg_txni->ni_nid == LNET_NID_LO_0)
807                 lo = true;
808
809         if (hstatus != LNET_MSG_STATUS_OK &&
810             ktime_compare(ktime_get(), msg->msg_deadline) >= 0)
811                 return -1;
812
813         /*
814          * always prefer txni/txpeer if they message is committed for both
815          * directions.
816          */
817         if (msg->msg_tx_committed) {
818                 ni = msg->msg_txni;
819                 lpni = msg->msg_txpeer;
820                 attempt_local_resend = attempt_remote_resend = true;
821         } else {
822                 ni = msg->msg_rxni;
823                 lpni = msg->msg_rxpeer;
824                 attempt_local_resend = attempt_remote_resend = false;
825         }
826
827         if (!lo)
828                 LASSERT(ni && lpni);
829         else
830                 LASSERT(ni);
831
832         CDEBUG(D_NET, "health check: %s->%s: %s: %s\n",
833                libcfs_nid2str(ni->ni_nid),
834                (lo) ? "self" : libcfs_nid2str(lpni->lpni_nid),
835                lnet_msgtyp2str(msg->msg_type),
836                lnet_health_error2str(hstatus));
837
838         /*
839          * stats are only incremented for errors so avoid wasting time
840          * incrementing statistics if there is no error. Similarly, whether to
841          * update health values or perform resends is only applicable for
842          * messages with a health status != OK.
843          */
844         if (hstatus != LNET_MSG_STATUS_OK) {
845                 /* Don't further decrement the health value if a recovery
846                  * message failed.
847                  */
848                 if (msg->msg_recovery)
849                         handle_local_health = handle_remote_health = false;
850                 else
851                         handle_local_health = handle_remote_health = true;
852
853                 /* For local failures, health/recovery/resends are not needed if
854                  * I only have a single (non-lolnd) interface. NB: pb_nnis
855                  * includes the lolnd interface, so a single-rail node would
856                  * have pb_nnis == 2.
857                  */
858                 if (the_lnet.ln_ping_target->pb_nnis <= 2) {
859                         handle_local_health = false;
860                         attempt_local_resend = false;
861                 }
862
863                 lnet_net_lock(0);
864                 lnet_incr_hstats(ni, lpni, hstatus);
865                 /* For remote failures, health/recovery/resends are not needed
866                  * if the peer only has a single interface. Special case for
867                  * routers where we rely on health feature to manage route
868                  * aliveness. NB: unlike pb_nnis above, lp_nnis does _not_
869                  * include the lolnd, so a single-rail node would have
870                  * lp_nnis == 1.
871                  */
872                 if (lpni && lpni->lpni_peer_net &&
873                     lpni->lpni_peer_net->lpn_peer &&
874                     lpni->lpni_peer_net->lpn_peer->lp_nnis <= 1) {
875                         attempt_remote_resend = false;
876                         if (!lnet_isrouter(lpni))
877                                 handle_remote_health = false;
878                 }
879                 lnet_net_unlock(0);
880         }
881
882         switch (hstatus) {
883         case LNET_MSG_STATUS_OK:
884                 /*
885                  * increment the local ni health whether we successfully
886                  * received or sent a message on it.
887                  *
888                  * Ping counts are reset to 0 as appropriate to allow for
889                  * faster recovery.
890                  */
891                 lnet_inc_healthv(&ni->ni_healthv, lnet_health_sensitivity);
892                 lnet_net_lock(0);
893                 ni->ni_ping_count = 0;
894                 /*
895                  * It's possible msg_txpeer is NULL in the LOLND
896                  * case. Only increment the peer's health if we're
897                  * receiving a message from it. It's the only sure way to
898                  * know that a remote interface is up.
899                  * If this interface is part of a router, then take that
900                  * as indication that the router is fully healthy.
901                  */
902                 if (lpni && msg->msg_rx_committed) {
903                         lpni->lpni_ping_count = 0;
904                         /*
905                          * If we're receiving a message from the router or
906                          * I'm a router, then set that lpni's health to
907                          * maximum so we can commence communication
908                          */
909                         if (lnet_isrouter(lpni) || the_lnet.ln_routing) {
910                                 lnet_set_lpni_healthv_locked(lpni,
911                                         LNET_MAX_HEALTH_VALUE);
912                         } else {
913                                 __u32 sensitivity = lpni->lpni_peer_net->
914                                         lpn_peer->lp_health_sensitivity;
915
916                                 lnet_inc_lpni_healthv_locked(lpni,
917                                         (sensitivity) ? sensitivity :
918                                         lnet_health_sensitivity);
919                                 /* This peer NI may have previously aged out
920                                  * of recovery. Now that we've received a
921                                  * message from it, we can continue recovery
922                                  * if its health value is still below the
923                                  * maximum.
924                                  */
925                                 lnet_peer_ni_add_to_recoveryq_locked(lpni,
926                                                 &the_lnet.ln_mt_peerNIRecovq,
927                                                 ktime_get_seconds());
928                         }
929                 }
930                 lnet_net_unlock(0);
931
932                 /* we can finalize this message */
933                 return -1;
934         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
935         case LNET_MSG_STATUS_LOCAL_DROPPED:
936         case LNET_MSG_STATUS_LOCAL_ABORTED:
937         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
938         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
939                 if (handle_local_health)
940                         lnet_handle_local_failure(ni);
941                 if (attempt_local_resend)
942                         return lnet_attempt_msg_resend(msg);
943                 break;
944         case LNET_MSG_STATUS_LOCAL_ERROR:
945                 if (handle_local_health)
946                         lnet_handle_local_failure(ni);
947                 return -1;
948         case LNET_MSG_STATUS_REMOTE_DROPPED:
949                 if (handle_remote_health)
950                         lnet_handle_remote_failure(lpni);
951                 if (attempt_remote_resend)
952                         return lnet_attempt_msg_resend(msg);
953                 break;
954         case LNET_MSG_STATUS_REMOTE_ERROR:
955         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
956                 if (handle_remote_health)
957                         lnet_handle_remote_failure(lpni);
958                 return -1;
959         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
960                 if (handle_remote_health)
961                         lnet_handle_remote_failure(lpni);
962                 if (handle_local_health)
963                         lnet_handle_local_failure(ni);
964                 return -1;
965         default:
966                 LBUG();
967         }
968
969         /* no resend is needed */
970         return -1;
971 }
972
973 static void
974 lnet_msg_detach_md(struct lnet_msg *msg, int status)
975 {
976         struct lnet_libmd *md = msg->msg_md;
977         lnet_handler_t handler = NULL;
978         int cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
979         int unlink;
980
981         lnet_res_lock(cpt);
982         while (md->md_flags & LNET_MD_FLAG_HANDLING)
983                 /* An event handler is running - wait for it to
984                  * complete to avoid races.
985                  */
986                 lnet_md_wait_handling(md, cpt);
987
988         /* Now it's safe to drop my caller's ref */
989         md->md_refcount--;
990         LASSERT(md->md_refcount >= 0);
991
992         unlink = lnet_md_unlinkable(md);
993         if (md->md_handler) {
994                 if ((md->md_flags & LNET_MD_FLAG_ABORTED) && !status) {
995                         msg->msg_ev.status   = -ETIMEDOUT;
996                         CDEBUG(D_NET, "md 0x%p already unlinked\n", md);
997                 } else {
998                         msg->msg_ev.status   = status;
999                 }
1000                 msg->msg_ev.unlinked = unlink;
1001                 handler = md->md_handler;
1002                 if (!unlink)
1003                         md->md_flags |= LNET_MD_FLAG_HANDLING;
1004         }
1005
1006         if (unlink || (md->md_refcount == 0 &&
1007                        md->md_threshold == LNET_MD_THRESH_INF))
1008                 lnet_detach_rsp_tracker(md, cpt);
1009
1010         msg->msg_md = NULL;
1011         if (unlink)
1012                 lnet_md_unlink(md);
1013
1014         lnet_res_unlock(cpt);
1015
1016         if (handler) {
1017                 handler(&msg->msg_ev);
1018                 if (!unlink) {
1019                         lnet_res_lock(cpt);
1020                         md->md_flags &= ~LNET_MD_FLAG_HANDLING;
1021                         wake_up_var(md);
1022                         lnet_res_unlock(cpt);
1023                 }
1024         }
1025 }
1026
1027 static bool
1028 lnet_is_health_check(struct lnet_msg *msg)
1029 {
1030         bool hc = true;
1031         int status = msg->msg_ev.status;
1032
1033         if ((!msg->msg_tx_committed && !msg->msg_rx_committed) ||
1034             !msg->msg_onactivelist) {
1035                 CDEBUG(D_NET, "msg %p not committed for send or receive\n",
1036                        msg);
1037                 return false;
1038         }
1039
1040         if ((msg->msg_tx_committed && !msg->msg_txpeer) ||
1041             (msg->msg_rx_committed && !msg->msg_rxpeer)) {
1042                 /* The optimized GET case does not set msg_rxpeer, but status
1043                  * could be zero. Only print the error message if we have a
1044                  * non-zero status.
1045                  */
1046                 if (status)
1047                         CDEBUG(D_NET, "msg %p status %d cannot retry\n", msg,
1048                                status);
1049                 return false;
1050         }
1051
1052         /* Check for status inconsistencies */
1053         if ((!status && msg->msg_health_status != LNET_MSG_STATUS_OK) ||
1054              (status && msg->msg_health_status == LNET_MSG_STATUS_OK)) {
1055                 CDEBUG(D_NET, "Msg %p is in inconsistent state, don't perform health "
1056                        "checking (%d, %d)\n", msg, status,
1057                        msg->msg_health_status);
1058                 hc = false;
1059         }
1060
1061         CDEBUG(D_NET, "health check = %d, status = %d, hstatus = %d\n",
1062                hc, status, msg->msg_health_status);
1063
1064         return hc;
1065 }
1066
1067 char *
1068 lnet_health_error2str(enum lnet_msg_hstatus hstatus)
1069 {
1070         switch (hstatus) {
1071         case LNET_MSG_STATUS_LOCAL_INTERRUPT:
1072                 return "LOCAL_INTERRUPT";
1073         case LNET_MSG_STATUS_LOCAL_DROPPED:
1074                 return "LOCAL_DROPPED";
1075         case LNET_MSG_STATUS_LOCAL_ABORTED:
1076                 return "LOCAL_ABORTED";
1077         case LNET_MSG_STATUS_LOCAL_NO_ROUTE:
1078                 return "LOCAL_NO_ROUTE";
1079         case LNET_MSG_STATUS_LOCAL_TIMEOUT:
1080                 return "LOCAL_TIMEOUT";
1081         case LNET_MSG_STATUS_LOCAL_ERROR:
1082                 return "LOCAL_ERROR";
1083         case LNET_MSG_STATUS_REMOTE_DROPPED:
1084                 return "REMOTE_DROPPED";
1085         case LNET_MSG_STATUS_REMOTE_ERROR:
1086                 return "REMOTE_ERROR";
1087         case LNET_MSG_STATUS_REMOTE_TIMEOUT:
1088                 return "REMOTE_TIMEOUT";
1089         case LNET_MSG_STATUS_NETWORK_TIMEOUT:
1090                 return "NETWORK_TIMEOUT";
1091         case LNET_MSG_STATUS_OK:
1092                 return "OK";
1093         default:
1094                 return "<UNKNOWN>";
1095         }
1096 }
1097
1098 bool
1099 lnet_send_error_simulation(struct lnet_msg *msg,
1100                            enum lnet_msg_hstatus *hstatus)
1101 {
1102         if (!msg)
1103                 return false;
1104
1105         if (list_empty(&the_lnet.ln_drop_rules))
1106             return false;
1107
1108         /* match only health rules */
1109         if (!lnet_drop_rule_match(&msg->msg_hdr, LNET_NID_ANY,
1110                                   hstatus))
1111                 return false;
1112
1113         CDEBUG(D_NET, "src %s(%s)->dst %s: %s simulate health error: %s\n",
1114                 libcfs_nid2str(msg->msg_hdr.src_nid),
1115                 libcfs_nid2str(msg->msg_txni->ni_nid),
1116                 libcfs_nid2str(msg->msg_hdr.dest_nid),
1117                 lnet_msgtyp2str(msg->msg_type),
1118                 lnet_health_error2str(*hstatus));
1119
1120         return true;
1121 }
1122 EXPORT_SYMBOL(lnet_send_error_simulation);
1123
1124 void
1125 lnet_finalize(struct lnet_msg *msg, int status)
1126 {
1127         struct lnet_msg_container *container;
1128         int my_slot;
1129         int cpt;
1130         int rc;
1131
1132         LASSERT(!in_interrupt());
1133
1134         if (msg == NULL)
1135                 return;
1136
1137         msg->msg_ev.status = status;
1138
1139         if (lnet_is_health_check(msg)) {
1140                 /*
1141                  * Check the health status of the message. If it has one
1142                  * of the errors that we're supposed to handle, and it has
1143                  * not timed out, then
1144                  *      1. Decrement the appropriate health_value
1145                  *      2. queue the message on the resend queue
1146
1147                  * if the message send is success, timed out or failed in the
1148                  * health check for any reason then we'll just finalize the
1149                  * message. Otherwise just return since the message has been
1150                  * put on the resend queue.
1151                  */
1152                 if (!lnet_health_check(msg))
1153                         return;
1154         }
1155
1156         /*
1157          * We're not going to resend this message so detach its MD and invoke
1158          * the appropriate callbacks
1159          */
1160         if (msg->msg_md != NULL)
1161                 lnet_msg_detach_md(msg, status);
1162
1163 again:
1164         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
1165                 /* not committed to network yet */
1166                 LASSERT(!msg->msg_onactivelist);
1167                 lnet_msg_free(msg);
1168                 return;
1169         }
1170
1171         /*
1172          * NB: routed message can be committed for both receiving and sending,
1173          * we should finalize in LIFO order and keep counters correct.
1174          * (finalize sending first then finalize receiving)
1175          */
1176         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
1177         lnet_net_lock(cpt);
1178
1179         container = the_lnet.ln_msg_containers[cpt];
1180
1181         /* Recursion breaker.  Don't complete the message here if I am (or
1182          * enough other threads are) already completing messages */
1183         my_slot = lnet_check_finalize_recursion_locked(msg,
1184                                                 &container->msc_finalizing,
1185                                                 container->msc_nfinalizers,
1186                                                 container->msc_finalizers);
1187
1188         /* enough threads are resending */
1189         if (my_slot == -1) {
1190                 lnet_net_unlock(cpt);
1191                 return;
1192         }
1193
1194         rc = 0;
1195         while (!list_empty(&container->msc_finalizing)) {
1196                 msg = list_entry(container->msc_finalizing.next,
1197                                  struct lnet_msg, msg_list);
1198
1199                 list_del_init(&msg->msg_list);
1200
1201                 /* NB drops and regains the lnet lock if it actually does
1202                  * anything, so my finalizing friends can chomp along too */
1203                 rc = lnet_complete_msg_locked(msg, cpt);
1204                 if (rc != 0)
1205                         break;
1206         }
1207
1208         if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
1209                 lnet_net_unlock(cpt);
1210                 lnet_delay_rule_check();
1211                 lnet_net_lock(cpt);
1212         }
1213
1214         container->msc_finalizers[my_slot] = NULL;
1215         lnet_net_unlock(cpt);
1216
1217         if (rc != 0)
1218                 goto again;
1219 }
1220 EXPORT_SYMBOL(lnet_finalize);
1221
1222 void
1223 lnet_msg_container_cleanup(struct lnet_msg_container *container)
1224 {
1225         int     count = 0;
1226
1227         if (container->msc_init == 0)
1228                 return;
1229
1230         while (!list_empty(&container->msc_active)) {
1231                 struct lnet_msg *msg;
1232
1233                 msg  = list_entry(container->msc_active.next,
1234                                   struct lnet_msg, msg_activelist);
1235                 LASSERT(msg->msg_onactivelist);
1236                 msg->msg_onactivelist = 0;
1237                 list_del_init(&msg->msg_activelist);
1238                 lnet_msg_free(msg);
1239                 count++;
1240         }
1241
1242         if (count > 0)
1243                 CERROR("%d active msg on exit\n", count);
1244
1245         if (container->msc_finalizers != NULL) {
1246                 CFS_FREE_PTR_ARRAY(container->msc_finalizers,
1247                                    container->msc_nfinalizers);
1248                 container->msc_finalizers = NULL;
1249         }
1250
1251         if (container->msc_resenders != NULL) {
1252                 CFS_FREE_PTR_ARRAY(container->msc_resenders,
1253                                    container->msc_nfinalizers);
1254                 container->msc_resenders = NULL;
1255         }
1256         container->msc_init = 0;
1257 }
1258
1259 int
1260 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
1261 {
1262         int rc = 0;
1263
1264         container->msc_init = 1;
1265
1266         INIT_LIST_HEAD(&container->msc_active);
1267         INIT_LIST_HEAD(&container->msc_finalizing);
1268         INIT_LIST_HEAD(&container->msc_resending);
1269
1270         /* number of CPUs */
1271         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
1272         if (container->msc_nfinalizers == 0)
1273                 container->msc_nfinalizers = 1;
1274
1275         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
1276                          container->msc_nfinalizers *
1277                          sizeof(*container->msc_finalizers));
1278
1279         if (container->msc_finalizers == NULL) {
1280                 CERROR("Failed to allocate message finalizers\n");
1281                 lnet_msg_container_cleanup(container);
1282                 return -ENOMEM;
1283         }
1284
1285         LIBCFS_CPT_ALLOC(container->msc_resenders, lnet_cpt_table(), cpt,
1286                          container->msc_nfinalizers *
1287                          sizeof(*container->msc_resenders));
1288
1289         if (container->msc_resenders == NULL) {
1290                 CERROR("Failed to allocate message resenders\n");
1291                 lnet_msg_container_cleanup(container);
1292                 return -ENOMEM;
1293         }
1294
1295         return rc;
1296 }
1297
1298 void
1299 lnet_msg_containers_destroy(void)
1300 {
1301         struct lnet_msg_container *container;
1302         int     i;
1303
1304         if (the_lnet.ln_msg_containers == NULL)
1305                 return;
1306
1307         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
1308                 lnet_msg_container_cleanup(container);
1309
1310         cfs_percpt_free(the_lnet.ln_msg_containers);
1311         the_lnet.ln_msg_containers = NULL;
1312 }
1313
1314 int
1315 lnet_msg_containers_create(void)
1316 {
1317         struct lnet_msg_container *container;
1318         int     rc;
1319         int     i;
1320
1321         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
1322                                                       sizeof(*container));
1323
1324         if (the_lnet.ln_msg_containers == NULL) {
1325                 CERROR("Failed to allocate cpu-partition data for network\n");
1326                 return -ENOMEM;
1327         }
1328
1329         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
1330                 rc = lnet_msg_container_setup(container, i);
1331                 if (rc != 0) {
1332                         lnet_msg_containers_destroy();
1333                         return rc;
1334                 }
1335         }
1336
1337         return 0;
1338 }