4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Message decoding, parsing and finalizing routines
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <lnet/lib-lnet.h>
42 lnet_build_unlink_event(lnet_libmd_t *md, lnet_event_t *ev)
46 memset(ev, 0, sizeof(*ev));
50 ev->type = LNET_EVENT_UNLINK;
51 lnet_md_deconstruct(md, &ev->md);
52 lnet_md2handle(&ev->md_handle, md);
57 * Don't need any lock, must be called after lnet_commit_md
60 lnet_build_msg_event(lnet_msg_t *msg, lnet_event_kind_t ev_type)
62 lnet_hdr_t *hdr = &msg->msg_hdr;
63 lnet_event_t *ev = &msg->msg_ev;
65 LASSERT(!msg->msg_routing);
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;
80 /* event for passive message */
81 ev->target.pid = hdr->dest_pid;
82 ev->target.nid = hdr->dest_nid;
83 ev->initiator.pid = hdr->src_pid;
84 /* Multi-Rail: resolve src_nid to "primary" peer NID */
85 ev->initiator.nid = msg->msg_initiator;
86 /* Multi-Rail: track source NID. */
87 ev->source.pid = hdr->src_pid;
88 ev->source.nid = hdr->src_nid;
89 ev->rlength = hdr->payload_length;
90 ev->sender = msg->msg_from;
91 ev->mlength = msg->msg_wanted;
92 ev->offset = msg->msg_offset;
99 case LNET_EVENT_PUT: /* passive PUT */
100 ev->pt_index = hdr->msg.put.ptl_index;
101 ev->match_bits = hdr->msg.put.match_bits;
102 ev->hdr_data = hdr->msg.put.hdr_data;
105 case LNET_EVENT_GET: /* passive GET */
106 ev->pt_index = hdr->msg.get.ptl_index;
107 ev->match_bits = hdr->msg.get.match_bits;
111 case LNET_EVENT_ACK: /* ACK */
112 ev->match_bits = hdr->msg.ack.match_bits;
113 ev->mlength = hdr->msg.ack.mlength;
116 case LNET_EVENT_REPLY: /* REPLY */
119 case LNET_EVENT_SEND: /* active message */
120 if (msg->msg_type == LNET_MSG_PUT) {
121 ev->pt_index = le32_to_cpu(hdr->msg.put.ptl_index);
122 ev->match_bits = le64_to_cpu(hdr->msg.put.match_bits);
123 ev->offset = le32_to_cpu(hdr->msg.put.offset);
125 ev->rlength = le32_to_cpu(hdr->payload_length);
126 ev->hdr_data = le64_to_cpu(hdr->msg.put.hdr_data);
129 LASSERT(msg->msg_type == LNET_MSG_GET);
130 ev->pt_index = le32_to_cpu(hdr->msg.get.ptl_index);
131 ev->match_bits = le64_to_cpu(hdr->msg.get.match_bits);
133 ev->rlength = le32_to_cpu(hdr->msg.get.sink_length);
134 ev->offset = le32_to_cpu(hdr->msg.get.src_offset);
142 lnet_msg_commit(lnet_msg_t *msg, int cpt)
144 struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
145 lnet_counters_t *counters = the_lnet.ln_counters[cpt];
147 /* routed message can be committed for both receiving and sending */
148 LASSERT(!msg->msg_tx_committed);
150 if (msg->msg_sending) {
151 LASSERT(!msg->msg_receiving);
153 msg->msg_tx_cpt = cpt;
154 msg->msg_tx_committed = 1;
155 if (msg->msg_rx_committed) { /* routed message REPLY */
156 LASSERT(msg->msg_onactivelist);
160 LASSERT(!msg->msg_sending);
161 msg->msg_rx_cpt = cpt;
162 msg->msg_rx_committed = 1;
165 LASSERT(!msg->msg_onactivelist);
166 msg->msg_onactivelist = 1;
167 list_add(&msg->msg_activelist, &container->msc_active);
169 counters->msgs_alloc++;
170 if (counters->msgs_alloc > counters->msgs_max)
171 counters->msgs_max = counters->msgs_alloc;
175 lnet_msg_decommit_tx(lnet_msg_t *msg, int status)
177 lnet_counters_t *counters;
178 lnet_event_t *ev = &msg->msg_ev;
180 LASSERT(msg->msg_tx_committed);
184 counters = the_lnet.ln_counters[msg->msg_tx_cpt];
186 default: /* routed message */
187 LASSERT(msg->msg_routing);
188 LASSERT(msg->msg_rx_committed);
189 LASSERT(ev->type == 0);
191 counters->route_length += msg->msg_len;
192 counters->route_count++;
196 /* should have been decommitted */
197 LASSERT(!msg->msg_rx_committed);
198 /* overwritten while sending ACK */
199 LASSERT(msg->msg_type == LNET_MSG_ACK);
200 msg->msg_type = LNET_MSG_PUT; /* fix type */
203 case LNET_EVENT_SEND:
204 LASSERT(!msg->msg_rx_committed);
205 if (msg->msg_type == LNET_MSG_PUT)
206 counters->send_length += msg->msg_len;
210 LASSERT(msg->msg_rx_committed);
211 /* overwritten while sending reply, we should never be
212 * here for optimized GET */
213 LASSERT(msg->msg_type == LNET_MSG_REPLY);
214 msg->msg_type = LNET_MSG_GET; /* fix type */
218 counters->send_count++;
220 lnet_return_tx_credits_locked(msg);
221 msg->msg_tx_committed = 0;
225 lnet_msg_decommit_rx(lnet_msg_t *msg, int status)
227 lnet_counters_t *counters;
228 lnet_event_t *ev = &msg->msg_ev;
230 LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
231 LASSERT(msg->msg_rx_committed);
236 counters = the_lnet.ln_counters[msg->msg_rx_cpt];
239 LASSERT(ev->type == 0);
240 LASSERT(msg->msg_routing);
244 LASSERT(msg->msg_type == LNET_MSG_ACK);
248 /* type is "REPLY" if it's an optimized GET on passive side,
249 * because optimized GET will never be committed for sending,
250 * so message type wouldn't be changed back to "GET" by
251 * lnet_msg_decommit_tx(), see details in lnet_parse_get() */
252 LASSERT(msg->msg_type == LNET_MSG_REPLY ||
253 msg->msg_type == LNET_MSG_GET);
254 counters->send_length += msg->msg_wanted;
258 LASSERT(msg->msg_type == LNET_MSG_PUT);
261 case LNET_EVENT_REPLY:
262 /* type is "GET" if it's an optimized GET on active side,
263 * see details in lnet_create_reply_msg() */
264 LASSERT(msg->msg_type == LNET_MSG_GET ||
265 msg->msg_type == LNET_MSG_REPLY);
269 counters->recv_count++;
270 if (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_REPLY)
271 counters->recv_length += msg->msg_wanted;
274 lnet_return_rx_credits_locked(msg);
275 msg->msg_rx_committed = 0;
279 lnet_msg_decommit(lnet_msg_t *msg, int cpt, int status)
283 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
284 LASSERT(msg->msg_onactivelist);
286 if (msg->msg_tx_committed) { /* always decommit for sending first */
287 LASSERT(cpt == msg->msg_tx_cpt);
288 lnet_msg_decommit_tx(msg, status);
291 if (msg->msg_rx_committed) {
292 /* forwarding msg committed for both receiving and sending */
293 if (cpt != msg->msg_rx_cpt) {
294 lnet_net_unlock(cpt);
295 cpt2 = msg->msg_rx_cpt;
298 lnet_msg_decommit_rx(msg, status);
301 list_del(&msg->msg_activelist);
302 msg->msg_onactivelist = 0;
304 the_lnet.ln_counters[cpt2]->msgs_alloc--;
307 lnet_net_unlock(cpt2);
313 lnet_msg_attach_md(lnet_msg_t *msg, lnet_libmd_t *md,
314 unsigned int offset, unsigned int mlen)
316 /* NB: @offset and @len are only useful for receiving */
317 /* Here, we attach the MD on lnet_msg and mark it busy and
318 * decrementing its threshold. Come what may, the lnet_msg "owns"
319 * the MD until a call to lnet_msg_detach_md or lnet_finalize()
320 * signals completion. */
321 LASSERT(!msg->msg_routing);
324 if (msg->msg_receiving) { /* committed for receiving */
325 msg->msg_offset = offset;
326 msg->msg_wanted = mlen;
330 if (md->md_threshold != LNET_MD_THRESH_INF) {
331 LASSERT(md->md_threshold > 0);
335 /* build umd in event */
336 lnet_md2handle(&msg->msg_ev.md_handle, md);
337 lnet_md_deconstruct(md, &msg->msg_ev.md);
341 lnet_msg_detach_md(lnet_msg_t *msg, int status)
343 lnet_libmd_t *md = msg->msg_md;
346 /* Now it's safe to drop my caller's ref */
348 LASSERT(md->md_refcount >= 0);
350 unlink = lnet_md_unlinkable(md);
351 if (md->md_eq != NULL) {
352 msg->msg_ev.status = status;
353 msg->msg_ev.unlinked = unlink;
354 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
364 lnet_complete_msg_locked(lnet_msg_t *msg, int cpt)
366 struct lnet_handle_wire ack_wmd;
368 int status = msg->msg_ev.status;
370 LASSERT(msg->msg_onactivelist);
372 if (status == 0 && msg->msg_ack) {
373 /* Only send an ACK if the PUT completed successfully */
375 lnet_msg_decommit(msg, cpt, 0);
378 lnet_net_unlock(cpt);
380 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
381 LASSERT(!msg->msg_routing);
383 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
385 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.source, 0, 0);
387 msg->msg_hdr.msg.ack.dst_wmd = ack_wmd;
388 msg->msg_hdr.msg.ack.match_bits = msg->msg_ev.match_bits;
389 msg->msg_hdr.msg.ack.mlength = cpu_to_le32(msg->msg_ev.mlength);
391 /* NB: we probably want to use NID of msg::msg_from as 3rd
392 * parameter (router NID) if it's routed message */
393 rc = lnet_send(msg->msg_ev.target.nid, msg, LNET_NID_ANY);
397 * NB: message is committed for sending, we should return
398 * on success because LND will finalize this message later.
400 * Also, there is possibility that message is committed for
401 * sending and also failed before delivering to LND,
402 * i.e: ENOMEM, in that case we can't fall through either
403 * because CPT for sending can be different with CPT for
404 * receiving, so we should return back to lnet_finalize()
405 * to make sure we are locking the correct partition.
409 } else if (status == 0 && /* OK so far */
410 (msg->msg_routing && !msg->msg_sending)) {
412 LASSERT(!msg->msg_receiving); /* called back recv already */
413 lnet_net_unlock(cpt);
415 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
419 * NB: message is committed for sending, we should return
420 * on success because LND will finalize this message later.
422 * Also, there is possibility that message is committed for
423 * sending and also failed before delivering to LND,
424 * i.e: ENOMEM, in that case we can't fall through either:
425 * - The rule is message must decommit for sending first if
426 * the it's committed for both sending and receiving
427 * - CPT for sending can be different with CPT for receiving,
428 * so we should return back to lnet_finalize() to make
429 * sure we are locking the correct partition.
434 lnet_msg_decommit(msg, cpt, status);
440 lnet_finalize(lnet_ni_t *ni, lnet_msg_t *msg, int status)
442 struct lnet_msg_container *container;
448 LASSERT(!in_interrupt());
453 msg->msg_ev.status = status;
455 if (msg->msg_md != NULL) {
456 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
459 lnet_msg_detach_md(msg, status);
460 lnet_res_unlock(cpt);
465 if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
466 /* not committed to network yet */
467 LASSERT(!msg->msg_onactivelist);
473 * NB: routed message can be committed for both receiving and sending,
474 * we should finalize in LIFO order and keep counters correct.
475 * (finalize sending first then finalize receiving)
477 cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
480 container = the_lnet.ln_msg_containers[cpt];
481 list_add_tail(&msg->msg_list, &container->msc_finalizing);
483 /* Recursion breaker. Don't complete the message here if I am (or
484 * enough other threads are) already completing messages */
487 for (i = 0; i < container->msc_nfinalizers; i++) {
488 if (container->msc_finalizers[i] == current)
491 if (my_slot < 0 && container->msc_finalizers[i] == NULL)
495 if (i < container->msc_nfinalizers || my_slot < 0) {
496 lnet_net_unlock(cpt);
500 container->msc_finalizers[my_slot] = current;
502 while (!list_empty(&container->msc_finalizing)) {
503 msg = list_entry(container->msc_finalizing.next,
504 lnet_msg_t, msg_list);
506 list_del(&msg->msg_list);
508 /* NB drops and regains the lnet lock if it actually does
509 * anything, so my finalizing friends can chomp along too */
510 rc = lnet_complete_msg_locked(msg, cpt);
515 if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
516 lnet_net_unlock(cpt);
517 lnet_delay_rule_check();
521 container->msc_finalizers[my_slot] = NULL;
522 lnet_net_unlock(cpt);
527 EXPORT_SYMBOL(lnet_finalize);
530 lnet_msg_container_cleanup(struct lnet_msg_container *container)
534 if (container->msc_init == 0)
537 while (!list_empty(&container->msc_active)) {
538 lnet_msg_t *msg = list_entry(container->msc_active.next,
539 lnet_msg_t, msg_activelist);
541 LASSERT(msg->msg_onactivelist);
542 msg->msg_onactivelist = 0;
543 list_del(&msg->msg_activelist);
549 CERROR("%d active msg on exit\n", count);
551 if (container->msc_finalizers != NULL) {
552 LIBCFS_FREE(container->msc_finalizers,
553 container->msc_nfinalizers *
554 sizeof(*container->msc_finalizers));
555 container->msc_finalizers = NULL;
557 container->msc_init = 0;
561 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
565 container->msc_init = 1;
567 INIT_LIST_HEAD(&container->msc_active);
568 INIT_LIST_HEAD(&container->msc_finalizing);
572 container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
574 LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
575 container->msc_nfinalizers *
576 sizeof(*container->msc_finalizers));
578 if (container->msc_finalizers == NULL) {
579 CERROR("Failed to allocate message finalizers\n");
580 lnet_msg_container_cleanup(container);
588 lnet_msg_containers_destroy(void)
590 struct lnet_msg_container *container;
593 if (the_lnet.ln_msg_containers == NULL)
596 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
597 lnet_msg_container_cleanup(container);
599 cfs_percpt_free(the_lnet.ln_msg_containers);
600 the_lnet.ln_msg_containers = NULL;
604 lnet_msg_containers_create(void)
606 struct lnet_msg_container *container;
610 the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
613 if (the_lnet.ln_msg_containers == NULL) {
614 CERROR("Failed to allocate cpu-partition data for network\n");
618 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
619 rc = lnet_msg_container_setup(container, i);
621 lnet_msg_containers_destroy();