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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Message decoding, parsing and finalizing routines
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <lnet/lib-lnet.h>
46 lnet_build_unlink_event(lnet_libmd_t *md, lnet_event_t *ev)
50 memset(ev, 0, sizeof(*ev));
54 ev->type = LNET_EVENT_UNLINK;
55 lnet_md_deconstruct(md, &ev->md);
56 lnet_md2handle(&ev->md_handle, md);
61 * Don't need any lock, must be called after lnet_commit_md
64 lnet_build_msg_event(lnet_msg_t *msg, lnet_event_kind_t ev_type)
66 lnet_hdr_t *hdr = &msg->msg_hdr;
67 lnet_event_t *ev = &msg->msg_ev;
69 LASSERT(!msg->msg_routing);
73 if (ev_type == LNET_EVENT_SEND) {
74 /* event for active message */
75 ev->target.nid = le64_to_cpu(hdr->dest_nid);
76 ev->target.pid = le32_to_cpu(hdr->dest_pid);
77 ev->initiator.nid = LNET_NID_ANY;
78 ev->initiator.pid = the_lnet.ln_pid;
79 ev->sender = LNET_NID_ANY;
82 /* event for passive message */
83 ev->target.pid = hdr->dest_pid;
84 ev->target.nid = hdr->dest_nid;
85 ev->initiator.pid = hdr->src_pid;
86 ev->initiator.nid = hdr->src_nid;
87 ev->rlength = hdr->payload_length;
88 ev->sender = msg->msg_from;
89 ev->mlength = msg->msg_wanted;
90 ev->offset = msg->msg_offset;
97 case LNET_EVENT_PUT: /* passive PUT */
98 ev->pt_index = hdr->msg.put.ptl_index;
99 ev->match_bits = hdr->msg.put.match_bits;
100 ev->hdr_data = hdr->msg.put.hdr_data;
103 case LNET_EVENT_GET: /* passive GET */
104 ev->pt_index = hdr->msg.get.ptl_index;
105 ev->match_bits = hdr->msg.get.match_bits;
109 case LNET_EVENT_ACK: /* ACK */
110 ev->match_bits = hdr->msg.ack.match_bits;
111 ev->mlength = hdr->msg.ack.mlength;
114 case LNET_EVENT_REPLY: /* REPLY */
117 case LNET_EVENT_SEND: /* active message */
118 if (msg->msg_type == LNET_MSG_PUT) {
119 ev->pt_index = le32_to_cpu(hdr->msg.put.ptl_index);
120 ev->match_bits = le64_to_cpu(hdr->msg.put.match_bits);
121 ev->offset = le32_to_cpu(hdr->msg.put.offset);
123 ev->rlength = le32_to_cpu(hdr->payload_length);
124 ev->hdr_data = le64_to_cpu(hdr->msg.put.hdr_data);
127 LASSERT(msg->msg_type == LNET_MSG_GET);
128 ev->pt_index = le32_to_cpu(hdr->msg.get.ptl_index);
129 ev->match_bits = le64_to_cpu(hdr->msg.get.match_bits);
131 ev->rlength = le32_to_cpu(hdr->msg.get.sink_length);
132 ev->offset = le32_to_cpu(hdr->msg.get.src_offset);
140 lnet_msg_commit(lnet_msg_t *msg, int cpt)
142 struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
143 lnet_counters_t *counters = the_lnet.ln_counters[cpt];
145 /* routed message can be committed for both receiving and sending */
146 LASSERT(!msg->msg_tx_committed);
148 if (msg->msg_sending) {
149 LASSERT(!msg->msg_receiving);
151 msg->msg_tx_cpt = cpt;
152 msg->msg_tx_committed = 1;
153 if (msg->msg_rx_committed) { /* routed message REPLY */
154 LASSERT(msg->msg_onactivelist);
158 LASSERT(!msg->msg_sending);
159 msg->msg_rx_cpt = cpt;
160 msg->msg_rx_committed = 1;
163 LASSERT(!msg->msg_onactivelist);
164 msg->msg_onactivelist = 1;
165 list_add(&msg->msg_activelist, &container->msc_active);
167 counters->msgs_alloc++;
168 if (counters->msgs_alloc > counters->msgs_max)
169 counters->msgs_max = counters->msgs_alloc;
173 lnet_msg_decommit_tx(lnet_msg_t *msg, int status)
175 lnet_counters_t *counters;
176 lnet_event_t *ev = &msg->msg_ev;
178 LASSERT(msg->msg_tx_committed);
182 counters = the_lnet.ln_counters[msg->msg_tx_cpt];
184 default: /* routed message */
185 LASSERT(msg->msg_routing);
186 LASSERT(msg->msg_rx_committed);
187 LASSERT(ev->type == 0);
189 counters->route_length += msg->msg_len;
190 counters->route_count++;
194 /* should have been decommitted */
195 LASSERT(!msg->msg_rx_committed);
196 /* overwritten while sending ACK */
197 LASSERT(msg->msg_type == LNET_MSG_ACK);
198 msg->msg_type = LNET_MSG_PUT; /* fix type */
201 case LNET_EVENT_SEND:
202 LASSERT(!msg->msg_rx_committed);
203 if (msg->msg_type == LNET_MSG_PUT)
204 counters->send_length += msg->msg_len;
208 LASSERT(msg->msg_rx_committed);
209 /* overwritten while sending reply, we should never be
210 * here for optimized GET */
211 LASSERT(msg->msg_type == LNET_MSG_REPLY);
212 msg->msg_type = LNET_MSG_GET; /* fix type */
216 counters->send_count++;
218 lnet_return_tx_credits_locked(msg);
219 msg->msg_tx_committed = 0;
223 lnet_msg_decommit_rx(lnet_msg_t *msg, int status)
225 lnet_counters_t *counters;
226 lnet_event_t *ev = &msg->msg_ev;
228 LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
229 LASSERT(msg->msg_rx_committed);
234 counters = the_lnet.ln_counters[msg->msg_rx_cpt];
237 LASSERT(ev->type == 0);
238 LASSERT(msg->msg_routing);
242 LASSERT(msg->msg_type == LNET_MSG_ACK);
246 /* type is "REPLY" if it's an optimized GET on passive side,
247 * because optimized GET will never be committed for sending,
248 * so message type wouldn't be changed back to "GET" by
249 * lnet_msg_decommit_tx(), see details in lnet_parse_get() */
250 LASSERT(msg->msg_type == LNET_MSG_REPLY ||
251 msg->msg_type == LNET_MSG_GET);
252 counters->send_length += msg->msg_wanted;
256 LASSERT(msg->msg_type == LNET_MSG_PUT);
259 case LNET_EVENT_REPLY:
260 /* type is "GET" if it's an optimized GET on active side,
261 * see details in lnet_create_reply_msg() */
262 LASSERT(msg->msg_type == LNET_MSG_GET ||
263 msg->msg_type == LNET_MSG_REPLY);
267 counters->recv_count++;
268 if (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_REPLY)
269 counters->recv_length += msg->msg_wanted;
272 lnet_return_rx_credits_locked(msg);
273 msg->msg_rx_committed = 0;
277 lnet_msg_decommit(lnet_msg_t *msg, int cpt, int status)
281 LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
282 LASSERT(msg->msg_onactivelist);
284 if (msg->msg_tx_committed) { /* always decommit for sending first */
285 LASSERT(cpt == msg->msg_tx_cpt);
286 lnet_msg_decommit_tx(msg, status);
289 if (msg->msg_rx_committed) {
290 /* forwarding msg committed for both receiving and sending */
291 if (cpt != msg->msg_rx_cpt) {
292 lnet_net_unlock(cpt);
293 cpt2 = msg->msg_rx_cpt;
296 lnet_msg_decommit_rx(msg, status);
299 list_del(&msg->msg_activelist);
300 msg->msg_onactivelist = 0;
302 the_lnet.ln_counters[cpt2]->msgs_alloc--;
305 lnet_net_unlock(cpt2);
311 lnet_msg_attach_md(lnet_msg_t *msg, lnet_libmd_t *md,
312 unsigned int offset, unsigned int mlen)
314 /* NB: @offset and @len are only useful for receiving */
315 /* Here, we attach the MD on lnet_msg and mark it busy and
316 * decrementing its threshold. Come what may, the lnet_msg "owns"
317 * the MD until a call to lnet_msg_detach_md or lnet_finalize()
318 * signals completion. */
319 LASSERT(!msg->msg_routing);
322 if (msg->msg_receiving) { /* committed for receiving */
323 msg->msg_offset = offset;
324 msg->msg_wanted = mlen;
328 if (md->md_threshold != LNET_MD_THRESH_INF) {
329 LASSERT(md->md_threshold > 0);
333 /* build umd in event */
334 lnet_md2handle(&msg->msg_ev.md_handle, md);
335 lnet_md_deconstruct(md, &msg->msg_ev.md);
339 lnet_msg_detach_md(lnet_msg_t *msg, int status)
341 lnet_libmd_t *md = msg->msg_md;
344 /* Now it's safe to drop my caller's ref */
346 LASSERT(md->md_refcount >= 0);
348 unlink = lnet_md_unlinkable(md);
349 if (md->md_eq != NULL) {
350 msg->msg_ev.status = status;
351 msg->msg_ev.unlinked = unlink;
352 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
362 lnet_complete_msg_locked(lnet_msg_t *msg, int cpt)
364 lnet_handle_wire_t ack_wmd;
366 int status = msg->msg_ev.status;
368 LASSERT(msg->msg_onactivelist);
370 if (status == 0 && msg->msg_ack) {
371 /* Only send an ACK if the PUT completed successfully */
373 lnet_msg_decommit(msg, cpt, 0);
376 lnet_net_unlock(cpt);
378 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
379 LASSERT(!msg->msg_routing);
381 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
383 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.initiator, 0, 0);
385 msg->msg_hdr.msg.ack.dst_wmd = ack_wmd;
386 msg->msg_hdr.msg.ack.match_bits = msg->msg_ev.match_bits;
387 msg->msg_hdr.msg.ack.mlength = cpu_to_le32(msg->msg_ev.mlength);
389 /* NB: we probably want to use NID of msg::msg_from as 3rd
390 * parameter (router NID) if it's routed message */
391 rc = lnet_send(msg->msg_ev.target.nid, msg, LNET_NID_ANY);
395 * NB: message is committed for sending, we should return
396 * on success because LND will finalize this message later.
398 * Also, there is possibility that message is committed for
399 * sending and also failed before delivering to LND,
400 * i.e: ENOMEM, in that case we can't fall through either
401 * because CPT for sending can be different with CPT for
402 * receiving, so we should return back to lnet_finalize()
403 * to make sure we are locking the correct partition.
407 } else if (status == 0 && /* OK so far */
408 (msg->msg_routing && !msg->msg_sending)) {
410 LASSERT(!msg->msg_receiving); /* called back recv already */
411 lnet_net_unlock(cpt);
413 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
417 * NB: message is committed for sending, we should return
418 * on success because LND will finalize this message later.
420 * Also, there is possibility that message is committed for
421 * sending and also failed before delivering to LND,
422 * i.e: ENOMEM, in that case we can't fall through either:
423 * - The rule is message must decommit for sending first if
424 * the it's committed for both sending and receiving
425 * - CPT for sending can be different with CPT for receiving,
426 * so we should return back to lnet_finalize() to make
427 * sure we are locking the correct partition.
432 lnet_msg_decommit(msg, cpt, status);
438 lnet_finalize(lnet_ni_t *ni, lnet_msg_t *msg, int status)
440 struct lnet_msg_container *container;
446 LASSERT(!in_interrupt());
451 msg->msg_ev.status = status;
453 if (msg->msg_md != NULL) {
454 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
457 lnet_msg_detach_md(msg, status);
458 lnet_res_unlock(cpt);
463 if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
464 /* not committed to network yet */
465 LASSERT(!msg->msg_onactivelist);
471 * NB: routed message can be committed for both receiving and sending,
472 * we should finalize in LIFO order and keep counters correct.
473 * (finalize sending first then finalize receiving)
475 cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
478 container = the_lnet.ln_msg_containers[cpt];
479 list_add_tail(&msg->msg_list, &container->msc_finalizing);
481 /* Recursion breaker. Don't complete the message here if I am (or
482 * enough other threads are) already completing messages */
485 for (i = 0; i < container->msc_nfinalizers; i++) {
486 if (container->msc_finalizers[i] == current)
489 if (my_slot < 0 && container->msc_finalizers[i] == NULL)
493 if (i < container->msc_nfinalizers || my_slot < 0) {
494 lnet_net_unlock(cpt);
498 container->msc_finalizers[my_slot] = current;
500 while (!list_empty(&container->msc_finalizing)) {
501 msg = list_entry(container->msc_finalizing.next,
502 lnet_msg_t, msg_list);
504 list_del(&msg->msg_list);
506 /* NB drops and regains the lnet lock if it actually does
507 * anything, so my finalizing friends can chomp along too */
508 rc = lnet_complete_msg_locked(msg, cpt);
513 if (unlikely(!list_empty(&the_lnet.ln_delay_rules))) {
514 lnet_net_unlock(cpt);
515 lnet_delay_rule_check();
519 container->msc_finalizers[my_slot] = NULL;
520 lnet_net_unlock(cpt);
525 EXPORT_SYMBOL(lnet_finalize);
528 lnet_msg_container_cleanup(struct lnet_msg_container *container)
532 if (container->msc_init == 0)
535 while (!list_empty(&container->msc_active)) {
536 lnet_msg_t *msg = list_entry(container->msc_active.next,
537 lnet_msg_t, msg_activelist);
539 LASSERT(msg->msg_onactivelist);
540 msg->msg_onactivelist = 0;
541 list_del(&msg->msg_activelist);
547 CERROR("%d active msg on exit\n", count);
549 if (container->msc_finalizers != NULL) {
550 LIBCFS_FREE(container->msc_finalizers,
551 container->msc_nfinalizers *
552 sizeof(*container->msc_finalizers));
553 container->msc_finalizers = NULL;
555 container->msc_init = 0;
559 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
563 container->msc_init = 1;
565 INIT_LIST_HEAD(&container->msc_active);
566 INIT_LIST_HEAD(&container->msc_finalizing);
570 container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
572 LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
573 container->msc_nfinalizers *
574 sizeof(*container->msc_finalizers));
576 if (container->msc_finalizers == NULL) {
577 CERROR("Failed to allocate message finalizers\n");
578 lnet_msg_container_cleanup(container);
586 lnet_msg_containers_destroy(void)
588 struct lnet_msg_container *container;
591 if (the_lnet.ln_msg_containers == NULL)
594 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
595 lnet_msg_container_cleanup(container);
597 cfs_percpt_free(the_lnet.ln_msg_containers);
598 the_lnet.ln_msg_containers = NULL;
602 lnet_msg_containers_create(void)
604 struct lnet_msg_container *container;
608 the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
611 if (the_lnet.ln_msg_containers == NULL) {
612 CERROR("Failed to allocate cpu-partition data for network\n");
616 cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
617 rc = lnet_msg_container_setup(container, i);
619 lnet_msg_containers_destroy();