2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/queue.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/socket.h>
44 #include <sys/vnode.h>
48 #include <sys/thread.h>
49 #include <sys/globaldata.h>
50 #include <sys/limits.h>
54 RB_GENERATE(kdmsg_state_tree, kdmsg_state, rbnode, kdmsg_state_cmp);
56 static void kdmsg_iocom_thread_rd(void *arg);
57 static void kdmsg_iocom_thread_wr(void *arg);
60 * Initialize the roll-up communications structure for a network
61 * messaging session. This function does not install the socket.
64 kdmsg_iocom_init(kdmsg_iocom_t *iocom, void *handle,
65 struct malloc_type *mmsg,
66 int (*lnk_rcvmsg)(kdmsg_msg_t *msg),
67 int (*dbg_rcvmsg)(kdmsg_msg_t *msg),
68 int (*misc_rcvmsg)(kdmsg_msg_t *msg))
70 bzero(iocom, sizeof(*iocom));
71 iocom->handle = handle;
73 iocom->lnk_rcvmsg = lnk_rcvmsg;
74 iocom->dbg_rcvmsg = dbg_rcvmsg;
75 iocom->misc_rcvmsg = misc_rcvmsg;
76 iocom->router.iocom = iocom;
77 lockinit(&iocom->msglk, "h2msg", 0, 0);
78 TAILQ_INIT(&iocom->msgq);
79 RB_INIT(&iocom->staterd_tree);
80 RB_INIT(&iocom->statewr_tree);
84 * [Re]connect using the passed file pointer. The caller must ref the
85 * fp for us. We own that ref now.
88 kdmsg_iocom_reconnect(kdmsg_iocom_t *iocom, struct file *fp,
89 const char *subsysname)
92 * Destroy the current connection
94 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
95 while (iocom->msgrd_td || iocom->msgwr_td) {
96 wakeup(&iocom->msg_ctl);
97 tsleep(iocom, 0, "clstrkl", hz);
101 * Drop communications descriptor
104 fdrop(iocom->msg_fp);
105 iocom->msg_fp = NULL;
107 kprintf("RESTART CONNECTION\n");
110 * Setup new communications descriptor
116 lwkt_create(kdmsg_iocom_thread_rd, iocom, &iocom->msgrd_td,
117 NULL, 0, -1, "%s-msgrd", subsysname);
118 lwkt_create(kdmsg_iocom_thread_wr, iocom, &iocom->msgwr_td,
119 NULL, 0, -1, "%s-msgwr", subsysname);
123 * Disconnect and clean up
126 kdmsg_iocom_uninit(kdmsg_iocom_t *iocom)
129 * Ask the cluster controller to go away
131 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
133 while (iocom->msgrd_td || iocom->msgwr_td) {
134 wakeup(&iocom->msg_ctl);
135 tsleep(iocom, 0, "clstrkl", hz);
139 * Drop communications descriptor
142 fdrop(iocom->msg_fp);
143 iocom->msg_fp = NULL;
148 * Cluster controller thread. Perform messaging functions. We have one
149 * thread for the reader and one for the writer. The writer handles
150 * shutdown requests (which should break the reader thread).
154 kdmsg_iocom_thread_rd(void *arg)
156 kdmsg_iocom_t *iocom = arg;
159 kdmsg_state_t *state;
163 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILL) == 0) {
165 * Retrieve the message from the pipe or socket.
167 error = fp_read(iocom->msg_fp, &hdr, sizeof(hdr),
168 NULL, 1, UIO_SYSSPACE);
171 if (hdr.magic != DMSG_HDR_MAGIC) {
172 kprintf("kdmsg: bad magic: %04x\n", hdr.magic);
176 hbytes = (hdr.cmd & DMSGF_SIZE) * DMSG_ALIGN;
177 if (hbytes < sizeof(hdr) || hbytes > DMSG_AUX_MAX) {
178 kprintf("kdmsg: bad header size %zd\n", hbytes);
182 /* XXX messy: mask cmd to avoid allocating state */
183 msg = kdmsg_msg_alloc(&iocom->router,
184 hdr.cmd & DMSGF_BASECMDMASK,
187 msg->hdr_size = hbytes;
188 if (hbytes > sizeof(hdr)) {
189 error = fp_read(iocom->msg_fp, &msg->any.head + 1,
190 hbytes - sizeof(hdr),
191 NULL, 1, UIO_SYSSPACE);
193 kprintf("kdmsg: short msg received\n");
198 msg->aux_size = hdr.aux_bytes * DMSG_ALIGN;
199 if (msg->aux_size > DMSG_AUX_MAX) {
200 kprintf("kdmsg: illegal msg payload size %zd\n",
206 msg->aux_data = kmalloc(msg->aux_size, iocom->mmsg,
208 error = fp_read(iocom->msg_fp, msg->aux_data,
210 NULL, 1, UIO_SYSSPACE);
212 kprintf("kdmsg: short msg payload received\n");
218 * State machine tracking, state assignment for msg,
219 * returns error and discard status. Errors are fatal
220 * to the connection except for EALREADY which forces
221 * a discard without execution.
223 error = kdmsg_state_msgrx(msg);
226 * Raw protocol or connection error
229 if (error == EALREADY)
231 } else if (msg->state && msg->state->func) {
233 * Message related to state which already has a
234 * handling function installed for it.
236 error = msg->state->func(msg->state, msg);
237 kdmsg_state_cleanuprx(msg);
238 } else if ((msg->any.head.cmd & DMSGF_PROTOS) ==
241 * Message related to the LNK protocol set
243 error = iocom->lnk_rcvmsg(msg);
244 kdmsg_state_cleanuprx(msg);
245 } else if ((msg->any.head.cmd & DMSGF_PROTOS) ==
248 * Message related to the DBG protocol set
250 error = iocom->dbg_rcvmsg(msg);
251 kdmsg_state_cleanuprx(msg);
254 * Other higher-level messages (e.g. vnops)
256 error = iocom->misc_rcvmsg(msg);
257 kdmsg_state_cleanuprx(msg);
263 kprintf("kdmsg: read failed error %d\n", error);
265 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
267 if (msg->state && msg->state->msg == msg)
268 msg->state->msg = NULL;
272 if ((state = iocom->freerd_state) != NULL) {
273 iocom->freerd_state = NULL;
274 kdmsg_state_free(state);
278 * Shutdown the socket before waiting for the transmit side.
280 * If we are dying due to e.g. a socket disconnect verses being
281 * killed explicity we have to set KILL in order to kick the tx
282 * side when it might not have any other work to do. KILL might
283 * already be set if we are in an unmount or reconnect.
285 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
287 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
288 wakeup(&iocom->msg_ctl);
291 * Wait for the transmit side to drain remaining messages
292 * before cleaning up the rx state. The transmit side will
293 * set KILLTX and wait for the rx side to completely finish
294 * (set msgrd_td to NULL) before cleaning up any remaining
297 lockmgr(&iocom->msglk, LK_RELEASE);
298 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX);
299 wakeup(&iocom->msg_ctl);
300 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILLTX) == 0) {
301 wakeup(&iocom->msg_ctl);
302 tsleep(iocom, 0, "clstrkw", hz);
305 iocom->msgrd_td = NULL;
308 * iocom can be ripped out from under us at this point but
317 kdmsg_iocom_thread_wr(void *arg)
319 kdmsg_iocom_t *iocom = arg;
321 kdmsg_state_t *state;
330 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
332 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILL) == 0 && error == 0) {
334 * Sleep if no messages pending. Interlock with flag while
337 if (TAILQ_EMPTY(&iocom->msgq)) {
338 atomic_set_int(&iocom->msg_ctl,
339 KDMSG_CLUSTERCTL_SLEEPING);
340 lksleep(&iocom->msg_ctl, &iocom->msglk, 0, "msgwr", hz);
341 atomic_clear_int(&iocom->msg_ctl,
342 KDMSG_CLUSTERCTL_SLEEPING);
345 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
347 * Remove msg from the transmit queue and do
348 * persist and half-closed state handling.
350 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
351 lockmgr(&iocom->msglk, LK_RELEASE);
353 error = kdmsg_state_msgtx(msg);
354 if (error == EALREADY) {
357 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
362 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
367 * Dump the message to the pipe or socket.
369 error = fp_write(iocom->msg_fp, &msg->any,
370 msg->hdr_size, &res, UIO_SYSSPACE);
371 if (error || res != msg->hdr_size) {
374 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
378 error = fp_write(iocom->msg_fp,
379 msg->aux_data, msg->aux_size,
381 if (error || res != msg->aux_size) {
384 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
388 kdmsg_state_cleanuptx(msg);
389 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
394 * Cleanup messages pending transmission and release msgq lock.
397 kprintf("kdmsg: write failed error %d\n", error);
400 if (msg->state && msg->state->msg == msg)
401 msg->state->msg = NULL;
406 * Shutdown the socket. This will cause the rx thread to get an
407 * EOF and ensure that both threads get to a termination state.
409 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
412 * Set KILLTX (which the rx side waits for), then wait for the RX
413 * side to completely finish before we clean out any remaining
416 lockmgr(&iocom->msglk, LK_RELEASE);
417 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLTX);
418 wakeup(&iocom->msg_ctl);
419 while (iocom->msgrd_td) {
420 wakeup(&iocom->msg_ctl);
421 tsleep(iocom, 0, "clstrkw", hz);
423 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
426 * Simulate received MSGF_DELETE's for any remaining states.
429 RB_FOREACH(state, kdmsg_state_tree, &iocom->staterd_tree) {
431 (state->rxcmd & DMSGF_DELETE) == 0) {
432 lockmgr(&iocom->msglk, LK_RELEASE);
433 msg = kdmsg_msg_alloc(&iocom->router, DMSG_LNK_ERROR,
435 if ((state->rxcmd & DMSGF_CREATE) == 0)
436 msg->any.head.cmd |= DMSGF_CREATE;
437 msg->any.head.cmd |= DMSGF_DELETE;
439 state->rxcmd = msg->any.head.cmd &
441 msg->state->func(state, msg);
442 kdmsg_state_cleanuprx(msg);
443 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
446 if (state->func == NULL) {
447 state->flags &= ~KDMSG_STATE_INSERTED;
448 RB_REMOVE(kdmsg_state_tree,
449 &iocom->staterd_tree, state);
450 kdmsg_state_free(state);
456 * NOTE: We have to drain the msgq to handle situations
457 * where received states have built up output
458 * messages, to avoid creating messages with
459 * duplicate CREATE/DELETE flags.
462 kdmsg_drain_msgq(iocom);
463 RB_FOREACH(state, kdmsg_state_tree, &iocom->statewr_tree) {
465 (state->rxcmd & DMSGF_DELETE) == 0) {
466 lockmgr(&iocom->msglk, LK_RELEASE);
467 msg = kdmsg_msg_alloc(&iocom->router, DMSG_LNK_ERROR,
469 if ((state->rxcmd & DMSGF_CREATE) == 0)
470 msg->any.head.cmd |= DMSGF_CREATE;
471 msg->any.head.cmd |= DMSGF_DELETE |
474 state->rxcmd = msg->any.head.cmd &
476 msg->state->func(state, msg);
477 kdmsg_state_cleanuprx(msg);
478 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
481 if (state->func == NULL) {
482 state->flags &= ~KDMSG_STATE_INSERTED;
483 RB_REMOVE(kdmsg_state_tree,
484 &iocom->statewr_tree, state);
485 kdmsg_state_free(state);
490 kdmsg_drain_msgq(iocom);
492 panic("kdmsg: comm thread shutdown couldn't drain");
493 if (RB_ROOT(&iocom->statewr_tree))
496 if ((state = iocom->freewr_state) != NULL) {
497 iocom->freewr_state = NULL;
498 kdmsg_state_free(state);
501 lockmgr(&iocom->msglk, LK_RELEASE);
504 * The state trees had better be empty now
506 KKASSERT(RB_EMPTY(&iocom->staterd_tree));
507 KKASSERT(RB_EMPTY(&iocom->statewr_tree));
508 KKASSERT(iocom->conn_state == NULL);
510 if (iocom->exit_func) {
512 * iocom is invalid after we call the exit function.
514 iocom->msgwr_td = NULL;
515 iocom->exit_func(iocom);
518 * iocom can be ripped out from under us once msgwr_td is
519 * set to NULL. The wakeup is safe.
521 iocom->msgwr_td = NULL;
528 * This cleans out the pending transmit message queue, adjusting any
529 * persistent states properly in the process.
531 * Caller must hold pmp->iocom.msglk
534 kdmsg_drain_msgq(kdmsg_iocom_t *iocom)
539 * Clean out our pending transmit queue, executing the
540 * appropriate state adjustments. If this tries to open
541 * any new outgoing transactions we have to loop up and
544 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
545 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
546 lockmgr(&iocom->msglk, LK_RELEASE);
547 if (msg->state && msg->state->msg == msg)
548 msg->state->msg = NULL;
549 if (kdmsg_state_msgtx(msg))
552 kdmsg_state_cleanuptx(msg);
553 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
558 * Process state tracking for a message after reception, prior to
561 * Called with msglk held and the msg dequeued.
563 * All messages are called with dummy state and return actual state.
564 * (One-off messages often just return the same dummy state).
566 * May request that caller discard the message by setting *discardp to 1.
567 * The returned state is not used in this case and is allowed to be NULL.
571 * These routines handle persistent and command/reply message state via the
572 * CREATE and DELETE flags. The first message in a command or reply sequence
573 * sets CREATE, the last message in a command or reply sequence sets DELETE.
575 * There can be any number of intermediate messages belonging to the same
576 * sequence sent inbetween the CREATE message and the DELETE message,
577 * which set neither flag. This represents a streaming command or reply.
579 * Any command message received with CREATE set expects a reply sequence to
580 * be returned. Reply sequences work the same as command sequences except the
581 * REPLY bit is also sent. Both the command side and reply side can
582 * degenerate into a single message with both CREATE and DELETE set. Note
583 * that one side can be streaming and the other side not, or neither, or both.
585 * The msgid is unique for the initiator. That is, two sides sending a new
586 * message can use the same msgid without colliding.
590 * ABORT sequences work by setting the ABORT flag along with normal message
591 * state. However, ABORTs can also be sent on half-closed messages, that is
592 * even if the command or reply side has already sent a DELETE, as long as
593 * the message has not been fully closed it can still send an ABORT+DELETE
594 * to terminate the half-closed message state.
596 * Since ABORT+DELETEs can race we silently discard ABORT's for message
597 * state which has already been fully closed. REPLY+ABORT+DELETEs can
598 * also race, and in this situation the other side might have already
599 * initiated a new unrelated command with the same message id. Since
600 * the abort has not set the CREATE flag the situation can be detected
601 * and the message will also be discarded.
603 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
604 * The ABORT request is essentially integrated into the command instead
605 * of being sent later on. In this situation the command implementation
606 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
607 * special-case non-blocking operation for the command.
609 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
610 * to be mid-stream aborts for command/reply sequences. ABORTs on
611 * one-way messages are not supported.
613 * NOTE! If a command sequence does not support aborts the ABORT flag is
618 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
619 * set. One-off messages cannot be aborted and typically aren't processed
620 * by these routines. The REPLY bit can be used to distinguish whether a
621 * one-off message is a command or reply. For example, one-off replies
622 * will typically just contain status updates.
625 kdmsg_state_msgrx(kdmsg_msg_t *msg)
627 kdmsg_iocom_t *iocom;
628 kdmsg_state_t *state;
631 iocom = msg->router->iocom;
634 * XXX resolve msg->any.head.source and msg->any.head.target
635 * into LNK_SPAN references.
637 * XXX replace msg->router
641 * Make sure a state structure is ready to go in case we need a new
642 * one. This is the only routine which uses freerd_state so no
643 * races are possible.
645 if ((state = iocom->freerd_state) == NULL) {
646 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
647 state->flags = KDMSG_STATE_DYNAMIC;
648 iocom->freerd_state = state;
652 * Lock RB tree and locate existing persistent state, if any.
654 * If received msg is a command state is on staterd_tree.
655 * If received msg is a reply state is on statewr_tree.
657 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
659 state->msgid = msg->any.head.msgid;
660 state->router = &iocom->router;
661 kprintf("received msg %08x msgid %jx source=%jx target=%jx\n",
663 (intmax_t)msg->any.head.msgid,
664 (intmax_t)msg->any.head.source,
665 (intmax_t)msg->any.head.target);
666 if (msg->any.head.cmd & DMSGF_REPLY)
667 state = RB_FIND(kdmsg_state_tree, &iocom->statewr_tree, state);
669 state = RB_FIND(kdmsg_state_tree, &iocom->staterd_tree, state);
673 * Short-cut one-off or mid-stream messages (state may be NULL).
675 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
676 DMSGF_ABORT)) == 0) {
677 lockmgr(&iocom->msglk, LK_RELEASE);
682 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
683 * inside the case statements.
685 switch(msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE | DMSGF_REPLY)) {
687 case DMSGF_CREATE | DMSGF_DELETE:
689 * New persistant command received.
692 kprintf("kdmsg_state_msgrx: duplicate transaction\n");
696 state = iocom->freerd_state;
697 iocom->freerd_state = NULL;
699 state->router = msg->router;
701 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
702 state->txcmd = DMSGF_REPLY;
703 RB_INSERT(kdmsg_state_tree, &iocom->staterd_tree, state);
704 state->flags |= KDMSG_STATE_INSERTED;
709 * Persistent state is expected but might not exist if an
710 * ABORT+DELETE races the close.
713 if (msg->any.head.cmd & DMSGF_ABORT) {
716 kprintf("kdmsg_state_msgrx: no state "
724 * Handle another ABORT+DELETE case if the msgid has already
727 if ((state->rxcmd & DMSGF_CREATE) == 0) {
728 if (msg->any.head.cmd & DMSGF_ABORT) {
731 kprintf("kdmsg_state_msgrx: state reused "
741 * Check for mid-stream ABORT command received, otherwise
744 if (msg->any.head.cmd & DMSGF_ABORT) {
746 (state->rxcmd & DMSGF_CREATE) == 0) {
753 case DMSGF_REPLY | DMSGF_CREATE:
754 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
756 * When receiving a reply with CREATE set the original
757 * persistent state message should already exist.
760 kprintf("kdmsg_state_msgrx: no state match for "
761 "REPLY cmd=%08x msgid=%016jx\n",
763 (intmax_t)msg->any.head.msgid);
767 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
770 case DMSGF_REPLY | DMSGF_DELETE:
772 * Received REPLY+ABORT+DELETE in case where msgid has
773 * already been fully closed, ignore the message.
776 if (msg->any.head.cmd & DMSGF_ABORT) {
779 kprintf("kdmsg_state_msgrx: no state match "
780 "for REPLY|DELETE\n");
787 * Received REPLY+ABORT+DELETE in case where msgid has
788 * already been reused for an unrelated message,
789 * ignore the message.
791 if ((state->rxcmd & DMSGF_CREATE) == 0) {
792 if (msg->any.head.cmd & DMSGF_ABORT) {
795 kprintf("kdmsg_state_msgrx: state reused "
796 "for REPLY|DELETE\n");
805 * Check for mid-stream ABORT reply received to sent command.
807 if (msg->any.head.cmd & DMSGF_ABORT) {
809 (state->rxcmd & DMSGF_CREATE) == 0) {
817 lockmgr(&iocom->msglk, LK_RELEASE);
822 kdmsg_state_cleanuprx(kdmsg_msg_t *msg)
824 kdmsg_iocom_t *iocom;
825 kdmsg_state_t *state;
827 iocom = msg->router->iocom;
829 if ((state = msg->state) == NULL) {
831 } else if (msg->any.head.cmd & DMSGF_DELETE) {
832 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
833 state->rxcmd |= DMSGF_DELETE;
834 if (state->txcmd & DMSGF_DELETE) {
835 if (state->msg == msg)
837 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
838 if (state->rxcmd & DMSGF_REPLY) {
839 KKASSERT(msg->any.head.cmd &
841 RB_REMOVE(kdmsg_state_tree,
842 &iocom->statewr_tree, state);
844 KKASSERT((msg->any.head.cmd &
846 RB_REMOVE(kdmsg_state_tree,
847 &iocom->staterd_tree, state);
849 state->flags &= ~KDMSG_STATE_INSERTED;
850 lockmgr(&iocom->msglk, LK_RELEASE);
851 kdmsg_state_free(state);
853 lockmgr(&iocom->msglk, LK_RELEASE);
856 } else if (state->msg != msg) {
862 * Process state tracking for a message prior to transmission.
864 * Called with msglk held and the msg dequeued.
866 * One-off messages are usually with dummy state and msg->state may be NULL
869 * New transactions (when CREATE is set) will insert the state.
871 * May request that caller discard the message by setting *discardp to 1.
872 * A NULL state may be returned in this case.
875 kdmsg_state_msgtx(kdmsg_msg_t *msg)
877 kdmsg_iocom_t *iocom;
878 kdmsg_state_t *state;
881 iocom = msg->router->iocom;
884 * Make sure a state structure is ready to go in case we need a new
885 * one. This is the only routine which uses freewr_state so no
886 * races are possible.
888 if ((state = iocom->freewr_state) == NULL) {
889 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
890 state->flags = KDMSG_STATE_DYNAMIC;
891 state->router = &iocom->router;
892 iocom->freewr_state = state;
896 * Lock RB tree. If persistent state is present it will have already
897 * been assigned to msg.
899 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
903 * Short-cut one-off or mid-stream messages (state may be NULL).
905 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
906 DMSGF_ABORT)) == 0) {
907 lockmgr(&iocom->msglk, LK_RELEASE);
913 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
914 * inside the case statements.
916 switch(msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
919 case DMSGF_CREATE | DMSGF_DELETE:
921 * Insert the new persistent message state and mark
922 * half-closed if DELETE is set. Since this is a new
923 * message it isn't possible to transition into the fully
926 * XXX state must be assigned and inserted by
927 * kdmsg_msg_write(). txcmd is assigned by us
930 KKASSERT(state != NULL);
931 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
932 state->rxcmd = DMSGF_REPLY;
937 * Sent ABORT+DELETE in case where msgid has already
938 * been fully closed, ignore the message.
941 if (msg->any.head.cmd & DMSGF_ABORT) {
944 kprintf("kdmsg_state_msgtx: no state match "
945 "for DELETE cmd=%08x msgid=%016jx\n",
947 (intmax_t)msg->any.head.msgid);
954 * Sent ABORT+DELETE in case where msgid has
955 * already been reused for an unrelated message,
956 * ignore the message.
958 if ((state->txcmd & DMSGF_CREATE) == 0) {
959 if (msg->any.head.cmd & DMSGF_ABORT) {
962 kprintf("kdmsg_state_msgtx: state reused "
972 * Check for mid-stream ABORT command sent
974 if (msg->any.head.cmd & DMSGF_ABORT) {
976 (state->txcmd & DMSGF_CREATE) == 0) {
983 case DMSGF_REPLY | DMSGF_CREATE:
984 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
986 * When transmitting a reply with CREATE set the original
987 * persistent state message should already exist.
990 kprintf("kdmsg_state_msgtx: no state match "
991 "for REPLY | CREATE\n");
995 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
998 case DMSGF_REPLY | DMSGF_DELETE:
1000 * When transmitting a reply with DELETE set the original
1001 * persistent state message should already exist.
1003 * This is very similar to the REPLY|CREATE|* case except
1004 * txcmd is already stored, so we just add the DELETE flag.
1006 * Sent REPLY+ABORT+DELETE in case where msgid has
1007 * already been fully closed, ignore the message.
1009 if (state == NULL) {
1010 if (msg->any.head.cmd & DMSGF_ABORT) {
1013 kprintf("kdmsg_state_msgtx: no state match "
1014 "for REPLY | DELETE\n");
1021 * Sent REPLY+ABORT+DELETE in case where msgid has already
1022 * been reused for an unrelated message, ignore the message.
1024 if ((state->txcmd & DMSGF_CREATE) == 0) {
1025 if (msg->any.head.cmd & DMSGF_ABORT) {
1028 kprintf("kdmsg_state_msgtx: state reused "
1029 "for REPLY | DELETE\n");
1038 * Check for mid-stream ABORT reply sent.
1040 * One-off REPLY messages are allowed for e.g. status updates.
1042 if (msg->any.head.cmd & DMSGF_ABORT) {
1043 if (state == NULL ||
1044 (state->txcmd & DMSGF_CREATE) == 0) {
1052 lockmgr(&iocom->msglk, LK_RELEASE);
1057 kdmsg_state_cleanuptx(kdmsg_msg_t *msg)
1059 kdmsg_iocom_t *iocom;
1060 kdmsg_state_t *state;
1062 iocom = msg->router->iocom;
1064 if ((state = msg->state) == NULL) {
1065 kdmsg_msg_free(msg);
1066 } else if (msg->any.head.cmd & DMSGF_DELETE) {
1067 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1068 state->txcmd |= DMSGF_DELETE;
1069 if (state->rxcmd & DMSGF_DELETE) {
1070 if (state->msg == msg)
1072 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
1073 if (state->txcmd & DMSGF_REPLY) {
1074 KKASSERT(msg->any.head.cmd &
1076 RB_REMOVE(kdmsg_state_tree,
1077 &iocom->staterd_tree, state);
1079 KKASSERT((msg->any.head.cmd &
1081 RB_REMOVE(kdmsg_state_tree,
1082 &iocom->statewr_tree, state);
1084 state->flags &= ~KDMSG_STATE_INSERTED;
1085 lockmgr(&iocom->msglk, LK_RELEASE);
1086 kdmsg_state_free(state);
1088 lockmgr(&iocom->msglk, LK_RELEASE);
1090 kdmsg_msg_free(msg);
1091 } else if (state->msg != msg) {
1092 kdmsg_msg_free(msg);
1097 kdmsg_state_free(kdmsg_state_t *state)
1099 kdmsg_iocom_t *iocom;
1102 iocom = state->router->iocom;
1104 KKASSERT((state->flags & KDMSG_STATE_INSERTED) == 0);
1107 kfree(state, iocom->mmsg);
1109 kdmsg_msg_free(msg);
1113 kdmsg_msg_alloc(kdmsg_router_t *router, uint32_t cmd,
1114 int (*func)(kdmsg_state_t *, kdmsg_msg_t *), void *data)
1116 kdmsg_iocom_t *iocom;
1118 kdmsg_state_t *state;
1121 iocom = router->iocom;
1122 hbytes = (cmd & DMSGF_SIZE) * DMSG_ALIGN;
1123 msg = kmalloc(offsetof(struct kdmsg_msg, any) + hbytes,
1124 iocom->mmsg, M_WAITOK | M_ZERO);
1125 msg->hdr_size = hbytes;
1126 msg->router = router;
1127 KKASSERT(router != NULL);
1128 msg->any.head.magic = DMSG_HDR_MAGIC;
1129 msg->any.head.source = 0;
1130 msg->any.head.target = router->target;
1131 msg->any.head.cmd = cmd;
1133 if (cmd & DMSGF_CREATE) {
1135 * New transaction, requires tracking state and a unique
1136 * msgid to be allocated.
1138 KKASSERT(msg->state == NULL);
1139 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
1140 state->flags = KDMSG_STATE_DYNAMIC;
1142 state->any.any = data;
1144 state->msgid = (uint64_t)(uintptr_t)state;
1145 state->router = msg->router;
1147 msg->any.head.source = 0;
1148 msg->any.head.target = state->router->target;
1149 msg->any.head.msgid = state->msgid;
1151 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1152 if (RB_INSERT(kdmsg_state_tree, &iocom->statewr_tree, state))
1153 panic("duplicate msgid allocated");
1154 state->flags |= KDMSG_STATE_INSERTED;
1155 msg->any.head.msgid = state->msgid;
1156 lockmgr(&iocom->msglk, LK_RELEASE);
1163 kdmsg_msg_free(kdmsg_msg_t *msg)
1165 kdmsg_iocom_t *iocom;
1167 iocom = msg->router->iocom;
1169 if (msg->aux_data && msg->aux_size) {
1170 kfree(msg->aux_data, iocom->mmsg);
1171 msg->aux_data = NULL;
1175 kfree(msg, iocom->mmsg);
1179 * Indexed messages are stored in a red-black tree indexed by their
1180 * msgid. Only persistent messages are indexed.
1183 kdmsg_state_cmp(kdmsg_state_t *state1, kdmsg_state_t *state2)
1185 if (state1->router < state2->router)
1187 if (state1->router > state2->router)
1189 if (state1->msgid < state2->msgid)
1191 if (state1->msgid > state2->msgid)
1197 * Write a message. All requisit command flags have been set.
1199 * If msg->state is non-NULL the message is written to the existing
1200 * transaction. msgid will be set accordingly.
1202 * If msg->state is NULL and CREATE is set new state is allocated and
1203 * (func, data) is installed. A msgid is assigned.
1205 * If msg->state is NULL and CREATE is not set the message is assumed
1206 * to be a one-way message. The originator must assign the msgid
1207 * (or leave it 0, which is typical.
1209 * This function merely queues the message to the management thread, it
1210 * does not write to the message socket/pipe.
1213 kdmsg_msg_write(kdmsg_msg_t *msg)
1215 kdmsg_iocom_t *iocom;
1216 kdmsg_state_t *state;
1218 iocom = msg->router->iocom;
1222 * Continuance or termination of existing transaction.
1223 * The transaction could have been initiated by either end.
1225 * (Function callback and aux data for the receive side can
1226 * be replaced or left alone).
1229 msg->any.head.msgid = state->msgid;
1230 msg->any.head.source = 0;
1231 msg->any.head.target = state->router->target;
1232 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1235 * One-off message (always uses msgid 0 to distinguish
1236 * between a possibly lost in-transaction message due to
1237 * competing aborts and a real one-off message?)
1239 msg->any.head.msgid = 0;
1240 msg->any.head.source = 0;
1241 msg->any.head.target = msg->router->target;
1242 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1246 * Finish up the msg fields
1248 msg->any.head.salt = /* (random << 8) | */ (iocom->msg_seq & 255);
1251 msg->any.head.hdr_crc = 0;
1252 msg->any.head.hdr_crc = iscsi_crc32(msg->any.buf, msg->hdr_size);
1254 TAILQ_INSERT_TAIL(&iocom->msgq, msg, qentry);
1256 if (iocom->msg_ctl & KDMSG_CLUSTERCTL_SLEEPING) {
1257 atomic_clear_int(&iocom->msg_ctl,
1258 KDMSG_CLUSTERCTL_SLEEPING);
1259 wakeup(&iocom->msg_ctl);
1262 lockmgr(&iocom->msglk, LK_RELEASE);
1266 * Reply to a message and terminate our side of the transaction.
1268 * If msg->state is non-NULL we are replying to a one-way message.
1271 kdmsg_msg_reply(kdmsg_msg_t *msg, uint32_t error)
1273 kdmsg_state_t *state = msg->state;
1278 * Reply with a simple error code and terminate the transaction.
1280 cmd = DMSG_LNK_ERROR;
1283 * Check if our direction has even been initiated yet, set CREATE.
1285 * Check what direction this is (command or reply direction). Note
1286 * that txcmd might not have been initiated yet.
1288 * If our direction has already been closed we just return without
1292 if (state->txcmd & DMSGF_DELETE)
1294 if ((state->txcmd & DMSGF_CREATE) == 0)
1295 cmd |= DMSGF_CREATE;
1296 if (state->txcmd & DMSGF_REPLY)
1298 cmd |= DMSGF_DELETE;
1300 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1303 kprintf("MSG_REPLY state=%p msg %08x\n", state, cmd);
1305 /* XXX messy mask cmd to avoid allocating state */
1306 nmsg = kdmsg_msg_alloc(msg->router, cmd & DMSGF_BASECMDMASK,
1308 nmsg->any.head.cmd = cmd;
1309 nmsg->any.head.error = error;
1310 nmsg->state = state;
1311 kdmsg_msg_write(nmsg);
1315 * Reply to a message and continue our side of the transaction.
1317 * If msg->state is non-NULL we are replying to a one-way message and this
1318 * function degenerates into the same as kdmsg_msg_reply().
1321 kdmsg_msg_result(kdmsg_msg_t *msg, uint32_t error)
1323 kdmsg_state_t *state = msg->state;
1328 * Return a simple result code, do NOT terminate the transaction.
1330 cmd = DMSG_LNK_ERROR;
1333 * Check if our direction has even been initiated yet, set CREATE.
1335 * Check what direction this is (command or reply direction). Note
1336 * that txcmd might not have been initiated yet.
1338 * If our direction has already been closed we just return without
1342 if (state->txcmd & DMSGF_DELETE)
1344 if ((state->txcmd & DMSGF_CREATE) == 0)
1345 cmd |= DMSGF_CREATE;
1346 if (state->txcmd & DMSGF_REPLY)
1348 /* continuing transaction, do not set MSGF_DELETE */
1350 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1354 /* XXX messy mask cmd to avoid allocating state */
1355 nmsg = kdmsg_msg_alloc(msg->router, cmd & DMSGF_BASECMDMASK,
1357 nmsg->any.head.cmd = cmd;
1358 nmsg->any.head.error = error;
1359 nmsg->state = state;
1360 kdmsg_msg_write(nmsg);