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
35 * TODO: txcmd CREATE state is deferred by tx msgq, need to calculate
36 * a streaming response. See subr_diskiocom()'s diskiodone().
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/queue.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/socket.h>
48 #include <sys/vnode.h>
52 #include <sys/thread.h>
53 #include <sys/globaldata.h>
54 #include <sys/limits.h>
58 RB_GENERATE(kdmsg_state_tree, kdmsg_state, rbnode, kdmsg_state_cmp);
60 static int kdmsg_msg_receive_handling(kdmsg_msg_t *msg);
61 static int kdmsg_state_msgrx(kdmsg_msg_t *msg);
62 static int kdmsg_state_msgtx(kdmsg_msg_t *msg);
63 static void kdmsg_state_cleanuprx(kdmsg_msg_t *msg);
64 static void kdmsg_state_cleanuptx(kdmsg_msg_t *msg);
65 static void kdmsg_state_abort(kdmsg_state_t *state);
66 static void kdmsg_state_free(kdmsg_state_t *state);
68 static void kdmsg_iocom_thread_rd(void *arg);
69 static void kdmsg_iocom_thread_wr(void *arg);
70 static int kdmsg_autorxmsg(kdmsg_msg_t *msg);
72 /*static struct lwkt_token kdmsg_token = LWKT_TOKEN_INITIALIZER(kdmsg_token);*/
75 * Initialize the roll-up communications structure for a network
76 * messaging session. This function does not install the socket.
79 kdmsg_iocom_init(kdmsg_iocom_t *iocom, void *handle, uint32_t flags,
80 struct malloc_type *mmsg,
81 int (*rcvmsg)(kdmsg_msg_t *msg))
83 bzero(iocom, sizeof(*iocom));
84 iocom->handle = handle;
86 iocom->rcvmsg = rcvmsg;
88 lockinit(&iocom->msglk, "h2msg", 0, 0);
89 TAILQ_INIT(&iocom->msgq);
90 RB_INIT(&iocom->staterd_tree);
91 RB_INIT(&iocom->statewr_tree);
93 iocom->state0.iocom = iocom;
94 iocom->state0.parent = &iocom->state0;
95 TAILQ_INIT(&iocom->state0.subq);
99 * [Re]connect using the passed file pointer. The caller must ref the
100 * fp for us. We own that ref now.
103 kdmsg_iocom_reconnect(kdmsg_iocom_t *iocom, struct file *fp,
104 const char *subsysname)
107 * Destroy the current connection
109 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
110 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX);
111 while (iocom->msgrd_td || iocom->msgwr_td) {
112 wakeup(&iocom->msg_ctl);
113 lksleep(iocom, &iocom->msglk, 0, "clstrkl", hz);
117 * Drop communications descriptor
120 fdrop(iocom->msg_fp);
121 iocom->msg_fp = NULL;
125 * Setup new communications descriptor
130 iocom->flags &= ~KDMSG_IOCOMF_EXITNOACC;
132 lwkt_create(kdmsg_iocom_thread_rd, iocom, &iocom->msgrd_td,
133 NULL, 0, -1, "%s-msgrd", subsysname);
134 lwkt_create(kdmsg_iocom_thread_wr, iocom, &iocom->msgwr_td,
135 NULL, 0, -1, "%s-msgwr", subsysname);
136 lockmgr(&iocom->msglk, LK_RELEASE);
140 * Caller sets up iocom->auto_lnk_conn and iocom->auto_lnk_span, then calls
141 * this function to handle the state machine for LNK_CONN and LNK_SPAN.
143 static int kdmsg_lnk_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
144 static int kdmsg_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
147 kdmsg_iocom_autoinitiate(kdmsg_iocom_t *iocom,
148 void (*auto_callback)(kdmsg_msg_t *msg))
152 iocom->auto_callback = auto_callback;
154 msg = kdmsg_msg_alloc(&iocom->state0,
155 DMSG_LNK_CONN | DMSGF_CREATE,
156 kdmsg_lnk_conn_reply, NULL);
157 iocom->auto_lnk_conn.head = msg->any.head;
158 msg->any.lnk_conn = iocom->auto_lnk_conn;
159 iocom->conn_state = msg->state;
160 kdmsg_msg_write(msg);
165 kdmsg_lnk_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
167 kdmsg_iocom_t *iocom = state->iocom;
171 * Upon receipt of the LNK_CONN acknowledgement initiate an
172 * automatic SPAN if we were asked to. Used by e.g. xdisk, but
173 * not used by HAMMER2 which must manage more than one transmitted
176 if ((msg->any.head.cmd & DMSGF_CREATE) &&
177 (iocom->flags & KDMSG_IOCOMF_AUTOTXSPAN)) {
178 rmsg = kdmsg_msg_alloc(&iocom->state0,
179 DMSG_LNK_SPAN | DMSGF_CREATE,
180 kdmsg_lnk_span_reply, NULL);
181 iocom->auto_lnk_span.head = rmsg->any.head;
182 rmsg->any.lnk_span = iocom->auto_lnk_span;
183 kdmsg_msg_write(rmsg);
187 * Process shim after the CONN is acknowledged and before the CONN
188 * transaction is deleted. For deletions this gives device drivers
189 * the ability to interlock new operations on the circuit before
190 * it becomes illegal and panics.
192 if (iocom->auto_callback)
193 iocom->auto_callback(msg);
195 if ((state->txcmd & DMSGF_DELETE) == 0 &&
196 (msg->any.head.cmd & DMSGF_DELETE)) {
197 iocom->conn_state = NULL;
198 kdmsg_msg_reply(msg, 0);
206 kdmsg_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
209 * Be sure to process shim before terminating the SPAN
210 * transaction. Gives device drivers the ability to
211 * interlock new operations on the circuit before it
212 * becomes illegal and panics.
214 if (state->iocom->auto_callback)
215 state->iocom->auto_callback(msg);
217 if ((state->txcmd & DMSGF_DELETE) == 0 &&
218 (msg->any.head.cmd & DMSGF_DELETE)) {
219 kdmsg_msg_reply(msg, 0);
225 * Disconnect and clean up
228 kdmsg_iocom_uninit(kdmsg_iocom_t *iocom)
230 kdmsg_state_t *state;
233 * Ask the cluster controller to go away
235 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
236 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX);
238 while (iocom->msgrd_td || iocom->msgwr_td) {
239 wakeup(&iocom->msg_ctl);
240 lksleep(iocom, &iocom->msglk, 0, "clstrkl", hz);
246 if ((state = iocom->freerd_state) != NULL) {
247 iocom->freerd_state = NULL;
248 kdmsg_state_free(state);
251 if ((state = iocom->freewr_state) != NULL) {
252 iocom->freewr_state = NULL;
253 kdmsg_state_free(state);
257 * Drop communications descriptor
260 fdrop(iocom->msg_fp);
261 iocom->msg_fp = NULL;
263 lockmgr(&iocom->msglk, LK_RELEASE);
267 * Cluster controller thread. Perform messaging functions. We have one
268 * thread for the reader and one for the writer. The writer handles
269 * shutdown requests (which should break the reader thread).
273 kdmsg_iocom_thread_rd(void *arg)
275 kdmsg_iocom_t *iocom = arg;
277 kdmsg_msg_t *msg = NULL;
282 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILLRX) == 0) {
284 * Retrieve the message from the pipe or socket.
286 error = fp_read(iocom->msg_fp, &hdr, sizeof(hdr),
287 NULL, 1, UIO_SYSSPACE);
290 if (hdr.magic != DMSG_HDR_MAGIC) {
291 kprintf("kdmsg: bad magic: %04x\n", hdr.magic);
295 hbytes = (hdr.cmd & DMSGF_SIZE) * DMSG_ALIGN;
296 if (hbytes < sizeof(hdr) || hbytes > DMSG_HDR_MAX) {
297 kprintf("kdmsg: bad header size %zd\n", hbytes);
302 /* XXX messy: mask cmd to avoid allocating state */
303 msg = kdmsg_msg_alloc(&iocom->state0,
304 hdr.cmd & DMSGF_BASECMDMASK,
307 msg->hdr_size = hbytes;
308 if (hbytes > sizeof(hdr)) {
309 error = fp_read(iocom->msg_fp, &msg->any.head + 1,
310 hbytes - sizeof(hdr),
311 NULL, 1, UIO_SYSSPACE);
313 kprintf("kdmsg: short msg received\n");
318 msg->aux_size = hdr.aux_bytes;
319 if (msg->aux_size > DMSG_AUX_MAX) {
320 kprintf("kdmsg: illegal msg payload size %zd\n",
326 abytes = DMSG_DOALIGN(msg->aux_size);
327 msg->aux_data = kmalloc(abytes, iocom->mmsg, M_WAITOK);
328 msg->flags |= KDMSG_FLAG_AUXALLOC;
329 error = fp_read(iocom->msg_fp, msg->aux_data,
330 abytes, NULL, 1, UIO_SYSSPACE);
332 kprintf("kdmsg: short msg payload received\n");
337 error = kdmsg_msg_receive_handling(msg);
341 kprintf("kdmsg: read thread terminating error=%d\n", error);
343 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
348 * Shutdown the socket and set KILLRX for consistency in case the
349 * shutdown was not commanded. Signal the transmit side to shutdown
350 * by setting KILLTX and waking it up.
352 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
353 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX |
354 KDMSG_CLUSTERCTL_KILLTX);
355 iocom->msgrd_td = NULL;
356 lockmgr(&iocom->msglk, LK_RELEASE);
357 wakeup(&iocom->msg_ctl);
360 * iocom can be ripped out at any time once the lock is
361 * released with msgrd_td set to NULL. The wakeup()s are safe but
370 kdmsg_iocom_thread_wr(void *arg)
372 kdmsg_iocom_t *iocom = arg;
374 kdmsg_state_t *state;
386 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
388 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILLTX) == 0 && error == 0) {
390 * Sleep if no messages pending. Interlock with flag while
393 if (TAILQ_EMPTY(&iocom->msgq)) {
394 atomic_set_int(&iocom->msg_ctl,
395 KDMSG_CLUSTERCTL_SLEEPING);
396 lksleep(&iocom->msg_ctl, &iocom->msglk, 0, "msgwr", hz);
397 atomic_clear_int(&iocom->msg_ctl,
398 KDMSG_CLUSTERCTL_SLEEPING);
401 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
403 * Remove msg from the transmit queue and do
404 * persist and half-closed state handling.
406 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
407 lockmgr(&iocom->msglk, LK_RELEASE);
409 error = kdmsg_state_msgtx(msg);
410 if (error == EALREADY) {
413 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
418 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
423 * Dump the message to the pipe or socket.
425 * We have to clean up the message as if the transmit
426 * succeeded even if it failed.
428 error = fp_write(iocom->msg_fp, &msg->any,
429 msg->hdr_size, &res, UIO_SYSSPACE);
430 if (error || res != msg->hdr_size) {
433 kdmsg_state_cleanuptx(msg);
434 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
438 abytes = DMSG_DOALIGN(msg->aux_size);
439 error = fp_write(iocom->msg_fp,
440 msg->aux_data, abytes,
442 if (error || res != abytes) {
445 kdmsg_state_cleanuptx(msg);
446 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
450 kdmsg_state_cleanuptx(msg);
451 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
455 kprintf("kdmsg: write thread terminating error=%d\n", error);
458 * Shutdown the socket and set KILLTX for consistency in case the
459 * shutdown was not commanded. Signal the receive side to shutdown
460 * by setting KILLRX and waking it up.
462 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
463 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX |
464 KDMSG_CLUSTERCTL_KILLTX);
465 wakeup(&iocom->msg_ctl);
468 * The transmit thread is responsible for final cleanups, wait
469 * for the receive side to terminate to prevent new received
470 * states from interfering with our cleanup.
472 * Do not set msgwr_td to NULL until we actually exit.
474 while (iocom->msgrd_td) {
475 wakeup(&iocom->msg_ctl);
476 lksleep(iocom, &iocom->msglk, 0, "clstrkt", hz);
480 * Loop until all the states are gone and there are no messages
486 while (TAILQ_FIRST(&iocom->msgq) ||
487 RB_ROOT(&iocom->staterd_tree) ||
488 RB_ROOT(&iocom->statewr_tree)) {
490 * Drain the transmit msgq.
492 kdmsg_drain_msgq(iocom);
495 * Flag any pending states that we are in the middle of
498 RB_FOREACH(state, kdmsg_state_tree, &iocom->staterd_tree)
499 atomic_set_int(&state->flags, KDMSG_STATE_DYING);
500 RB_FOREACH(state, kdmsg_state_tree, &iocom->statewr_tree)
501 atomic_set_int(&state->flags, KDMSG_STATE_DYING);
504 * Simulate received message completions (with error) for
505 * all remaining states. The transmit side is the
506 * responsibility of the service code so we may end up
507 * looping until things like e.g. I/O requests are complete.
510 RB_FOREACH(state, kdmsg_state_tree, &iocom->staterd_tree) {
511 if ((state->rxcmd & DMSGF_DELETE) == 0) {
512 lockmgr(&iocom->msglk, LK_RELEASE);
513 kdmsg_state_abort(state);
514 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
520 RB_FOREACH(state, kdmsg_state_tree, &iocom->statewr_tree) {
521 if ((state->rxcmd & DMSGF_DELETE) == 0) {
522 lockmgr(&iocom->msglk, LK_RELEASE);
523 kdmsg_state_abort(state);
524 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
531 * If we've gone through the whole thing sleep for ~1 second.
532 * States may not go away completely until service side actions
533 * finish handling any in-progress operations.
536 lksleep(iocom, &iocom->msglk, 0, "clstrtk", hz);
538 if ((int)(ticks - save_ticks) > hz*2 && didwarn == 0) {
540 kprintf("kdmsg: warning, write thread on %p still "
541 "terminating\n", iocom);
543 if ((int)(ticks - save_ticks) > hz*60) {
544 panic("kdmsg: write thread on %p could not terminate\n",
550 * Exit handling is done by the write thread.
552 iocom->flags |= KDMSG_IOCOMF_EXITNOACC;
553 lockmgr(&iocom->msglk, LK_RELEASE);
556 * The state trees had better be empty now
558 KKASSERT(RB_EMPTY(&iocom->staterd_tree));
559 KKASSERT(RB_EMPTY(&iocom->statewr_tree));
560 KKASSERT(iocom->conn_state == NULL);
562 if (iocom->exit_func) {
564 * iocom is invalid after we call the exit function.
566 iocom->msgwr_td = NULL;
567 iocom->exit_func(iocom);
570 * iocom can be ripped out from under us once msgwr_td is
571 * set to NULL. The wakeup is safe.
573 iocom->msgwr_td = NULL;
580 * This cleans out the pending transmit message queue, adjusting any
581 * persistent states properly in the process.
583 * Caller must hold pmp->iocom.msglk
586 kdmsg_drain_msgq(kdmsg_iocom_t *iocom)
591 * Clean out our pending transmit queue, executing the
592 * appropriate state adjustments. If this tries to open
593 * any new outgoing transactions we have to loop up and
596 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
597 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
598 lockmgr(&iocom->msglk, LK_RELEASE);
599 if (kdmsg_state_msgtx(msg))
602 kdmsg_state_cleanuptx(msg);
603 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
608 * Do all processing required to handle a freshly received message
609 * after its low level header has been validated.
613 kdmsg_msg_receive_handling(kdmsg_msg_t *msg)
615 kdmsg_iocom_t *iocom = msg->state->iocom;
619 * State machine tracking, state assignment for msg,
620 * returns error and discard status. Errors are fatal
621 * to the connection except for EALREADY which forces
622 * a discard without execution.
624 error = kdmsg_state_msgrx(msg);
627 * Raw protocol or connection error
630 if (error == EALREADY)
632 } else if (msg->state && msg->state->func) {
634 * Message related to state which already has a
635 * handling function installed for it.
637 error = msg->state->func(msg->state, msg);
638 kdmsg_state_cleanuprx(msg);
639 } else if (iocom->flags & KDMSG_IOCOMF_AUTOANY) {
640 error = kdmsg_autorxmsg(msg);
641 kdmsg_state_cleanuprx(msg);
643 error = iocom->rcvmsg(msg);
644 kdmsg_state_cleanuprx(msg);
650 * Process state tracking for a message after reception, prior to
653 * Called with msglk held and the msg dequeued.
655 * All messages are called with dummy state and return actual state.
656 * (One-off messages often just return the same dummy state).
658 * May request that caller discard the message by setting *discardp to 1.
659 * The returned state is not used in this case and is allowed to be NULL.
663 * These routines handle persistent and command/reply message state via the
664 * CREATE and DELETE flags. The first message in a command or reply sequence
665 * sets CREATE, the last message in a command or reply sequence sets DELETE.
667 * There can be any number of intermediate messages belonging to the same
668 * sequence sent inbetween the CREATE message and the DELETE message,
669 * which set neither flag. This represents a streaming command or reply.
671 * Any command message received with CREATE set expects a reply sequence to
672 * be returned. Reply sequences work the same as command sequences except the
673 * REPLY bit is also sent. Both the command side and reply side can
674 * degenerate into a single message with both CREATE and DELETE set. Note
675 * that one side can be streaming and the other side not, or neither, or both.
677 * The msgid is unique for the initiator. That is, two sides sending a new
678 * message can use the same msgid without colliding.
682 * ABORT sequences work by setting the ABORT flag along with normal message
683 * state. However, ABORTs can also be sent on half-closed messages, that is
684 * even if the command or reply side has already sent a DELETE, as long as
685 * the message has not been fully closed it can still send an ABORT+DELETE
686 * to terminate the half-closed message state.
688 * Since ABORT+DELETEs can race we silently discard ABORT's for message
689 * state which has already been fully closed. REPLY+ABORT+DELETEs can
690 * also race, and in this situation the other side might have already
691 * initiated a new unrelated command with the same message id. Since
692 * the abort has not set the CREATE flag the situation can be detected
693 * and the message will also be discarded.
695 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
696 * The ABORT request is essentially integrated into the command instead
697 * of being sent later on. In this situation the command implementation
698 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
699 * special-case non-blocking operation for the command.
701 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
702 * to be mid-stream aborts for command/reply sequences. ABORTs on
703 * one-way messages are not supported.
705 * NOTE! If a command sequence does not support aborts the ABORT flag is
710 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
711 * set. One-off messages cannot be aborted and typically aren't processed
712 * by these routines. The REPLY bit can be used to distinguish whether a
713 * one-off message is a command or reply. For example, one-off replies
714 * will typically just contain status updates.
718 kdmsg_state_msgrx(kdmsg_msg_t *msg)
720 kdmsg_iocom_t *iocom = msg->state->iocom;
721 kdmsg_state_t *state;
722 kdmsg_state_t *pstate;
723 kdmsg_state_t sdummy;
727 * Make sure a state structure is ready to go in case we need a new
728 * one. This is the only routine which uses freerd_state so no
729 * races are possible.
731 if ((state = iocom->freerd_state) == NULL) {
732 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
733 state->flags = KDMSG_STATE_DYNAMIC;
734 state->iocom = iocom;
735 TAILQ_INIT(&state->subq);
736 iocom->freerd_state = state;
740 * Lock RB tree and locate existing persistent state, if any.
742 * If received msg is a command state is on staterd_tree.
743 * If received msg is a reply state is on statewr_tree.
745 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
748 sdummy.msgid = msg->any.head.msgid;
749 sdummy.iocom = iocom;
750 if (msg->any.head.cmd & DMSGF_REVTRANS) {
751 state = RB_FIND(kdmsg_state_tree, &iocom->statewr_tree,
754 state = RB_FIND(kdmsg_state_tree, &iocom->staterd_tree,
758 state = &iocom->state0;
759 if (state->flags & KDMSG_STATE_INTERLOCK) {
760 state->flags |= KDMSG_STATE_SIGNAL;
761 lksleep(state, &iocom->msglk, 0, "dmrace", hz);
768 * Short-cut one-off or mid-stream messages.
770 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
771 DMSGF_ABORT)) == 0) {
777 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
778 * inside the case statements.
780 switch(msg->any.head.cmd & (DMSGF_CREATE|DMSGF_DELETE|DMSGF_REPLY)) {
782 case DMSGF_CREATE | DMSGF_DELETE:
784 * New persistant command received.
786 if (state != &iocom->state0) {
787 kprintf("kdmsg_state_msgrx: duplicate transaction\n");
793 * Lookup the circuit. The circuit is an open transaction.
794 * the REVCIRC bit in the message tells us which side
795 * initiated the transaction representing the circuit.
797 if (msg->any.head.circuit) {
798 sdummy.msgid = msg->any.head.circuit;
800 if (msg->any.head.cmd & DMSGF_REVCIRC) {
801 pstate = RB_FIND(kdmsg_state_tree,
802 &iocom->statewr_tree,
805 pstate = RB_FIND(kdmsg_state_tree,
806 &iocom->staterd_tree,
809 if (pstate == NULL) {
810 kprintf("kdmsg_state_msgrx: "
811 "missing parent in stacked trans\n");
816 pstate = &iocom->state0;
822 state = iocom->freerd_state;
823 iocom->freerd_state = NULL;
826 state->parent = pstate;
827 KKASSERT(state->iocom == iocom);
828 state->flags |= KDMSG_STATE_INSERTED |
829 KDMSG_STATE_OPPOSITE;
830 state->icmd = msg->any.head.cmd & DMSGF_BASECMDMASK;
831 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
832 state->txcmd = DMSGF_REPLY;
833 state->msgid = msg->any.head.msgid;
834 RB_INSERT(kdmsg_state_tree, &iocom->staterd_tree, state);
835 TAILQ_INSERT_TAIL(&pstate->subq, state, entry);
840 * Persistent state is expected but might not exist if an
841 * ABORT+DELETE races the close.
843 if (state == &iocom->state0) {
844 if (msg->any.head.cmd & DMSGF_ABORT) {
847 kprintf("kdmsg_state_msgrx: "
848 "no state for DELETE\n");
855 * Handle another ABORT+DELETE case if the msgid has already
858 if ((state->rxcmd & DMSGF_CREATE) == 0) {
859 if (msg->any.head.cmd & DMSGF_ABORT) {
862 kprintf("kdmsg_state_msgrx: "
863 "state reused for DELETE\n");
872 * Check for mid-stream ABORT command received, otherwise
875 if (msg->any.head.cmd & DMSGF_ABORT) {
876 if (state == &iocom->state0 ||
877 (state->rxcmd & DMSGF_CREATE) == 0) {
884 case DMSGF_REPLY | DMSGF_CREATE:
885 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
887 * When receiving a reply with CREATE set the original
888 * persistent state message should already exist.
890 if (state == &iocom->state0) {
891 kprintf("kdmsg_state_msgrx: no state match for "
892 "REPLY cmd=%08x msgid=%016jx\n",
894 (intmax_t)msg->any.head.msgid);
898 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
901 case DMSGF_REPLY | DMSGF_DELETE:
903 * Received REPLY+ABORT+DELETE in case where msgid has
904 * already been fully closed, ignore the message.
906 if (state == &iocom->state0) {
907 if (msg->any.head.cmd & DMSGF_ABORT) {
910 kprintf("kdmsg_state_msgrx: no state match "
911 "for REPLY|DELETE\n");
918 * Received REPLY+ABORT+DELETE in case where msgid has
919 * already been reused for an unrelated message,
920 * ignore the message.
922 if ((state->rxcmd & DMSGF_CREATE) == 0) {
923 if (msg->any.head.cmd & DMSGF_ABORT) {
926 kprintf("kdmsg_state_msgrx: state reused "
927 "for REPLY|DELETE\n");
936 * Check for mid-stream ABORT reply received to sent command.
938 if (msg->any.head.cmd & DMSGF_ABORT) {
939 if (state == &iocom->state0 ||
940 (state->rxcmd & DMSGF_CREATE) == 0) {
950 * Calculate the easy-switch() transactional command. Represents
951 * the outer-transaction command for any transaction-create or
952 * transaction-delete, and the inner message command for any
953 * non-transaction or inside-transaction command. tcmd will be
954 * set to 0 if the message state is illegal.
956 * The two can be told apart because outer-transaction commands
957 * always have a DMSGF_CREATE and/or DMSGF_DELETE flag.
960 lockmgr(&iocom->msglk, LK_RELEASE);
962 if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE)) {
963 if (state != &iocom->state0) {
964 msg->tcmd = (msg->state->icmd & DMSGF_BASECMDMASK) |
965 (msg->any.head.cmd & (DMSGF_CREATE |
972 msg->tcmd = msg->any.head.cmd & DMSGF_CMDSWMASK;
978 * Called instead of iocom->rcvmsg() if any of the AUTO flags are set.
979 * This routine must call iocom->rcvmsg() for anything not automatically
983 kdmsg_autorxmsg(kdmsg_msg_t *msg)
985 kdmsg_iocom_t *iocom = msg->state->iocom;
990 * Main switch processes transaction create/delete sequences only.
991 * Use icmd (DELETEs use DMSG_LNK_ERROR
993 * NOTE: If processing in-transaction messages you generally want
994 * an inner switch on msg->any.head.cmd.
997 cmd = (msg->state->icmd & DMSGF_BASECMDMASK) |
998 (msg->any.head.cmd & (DMSGF_CREATE |
1006 case DMSG_LNK_CONN | DMSGF_CREATE:
1007 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE:
1009 * Received LNK_CONN transaction. Transmit response and
1010 * leave transaction open, which allows the other end to
1011 * start to the SPAN protocol.
1013 * Handle shim after acknowledging the CONN.
1015 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
1016 if (iocom->flags & KDMSG_IOCOMF_AUTOCONN) {
1017 kdmsg_msg_result(msg, 0);
1018 if (iocom->auto_callback)
1019 iocom->auto_callback(msg);
1021 error = iocom->rcvmsg(msg);
1026 case DMSG_LNK_CONN | DMSGF_DELETE:
1028 * This message is usually simulated after a link is lost
1029 * to clean up the transaction.
1031 if (iocom->flags & KDMSG_IOCOMF_AUTOCONN) {
1032 if (iocom->auto_callback)
1033 iocom->auto_callback(msg);
1034 kdmsg_msg_reply(msg, 0);
1036 error = iocom->rcvmsg(msg);
1039 case DMSG_LNK_SPAN | DMSGF_CREATE:
1040 case DMSG_LNK_SPAN | DMSGF_CREATE | DMSGF_DELETE:
1042 * Received LNK_SPAN transaction. We do not have to respond
1043 * (except on termination), but we must leave the transaction
1046 * Handle shim after acknowledging the SPAN.
1048 if (iocom->flags & KDMSG_IOCOMF_AUTORXSPAN) {
1049 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
1050 if (iocom->auto_callback)
1051 iocom->auto_callback(msg);
1056 error = iocom->rcvmsg(msg);
1060 case DMSG_LNK_SPAN | DMSGF_DELETE:
1062 * Process shims (auto_callback) before cleaning up the
1063 * circuit structure and closing the transactions. Device
1064 * driver should ensure that the circuit is not used after
1065 * the auto_callback() returns.
1067 * Handle shim before closing the SPAN transaction.
1069 if (iocom->flags & KDMSG_IOCOMF_AUTORXSPAN) {
1070 if (iocom->auto_callback)
1071 iocom->auto_callback(msg);
1072 kdmsg_msg_reply(msg, 0);
1074 error = iocom->rcvmsg(msg);
1079 * Anything unhandled goes into rcvmsg.
1081 * NOTE: Replies to link-level messages initiated by our side
1082 * are handled by the state callback, they are NOT
1085 error = iocom->rcvmsg(msg);
1092 * Post-receive-handling message and state cleanup. This routine is called
1093 * after the state function handling/callback to properly dispose of the
1094 * message and update or dispose of the state.
1098 kdmsg_state_cleanuprx(kdmsg_msg_t *msg)
1100 kdmsg_iocom_t *iocom = msg->state->iocom;
1101 kdmsg_state_t *state;
1102 kdmsg_state_t *pstate;
1104 if ((state = msg->state) == NULL) {
1105 kdmsg_msg_free(msg);
1106 } else if (msg->any.head.cmd & DMSGF_DELETE) {
1107 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1108 KKASSERT((state->rxcmd & DMSGF_DELETE) == 0);
1109 state->rxcmd |= DMSGF_DELETE;
1110 if (state->txcmd & DMSGF_DELETE) {
1111 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
1112 if (state->rxcmd & DMSGF_REPLY) {
1113 KKASSERT(msg->any.head.cmd &
1115 RB_REMOVE(kdmsg_state_tree,
1116 &iocom->statewr_tree, state);
1118 KKASSERT((msg->any.head.cmd &
1120 RB_REMOVE(kdmsg_state_tree,
1121 &iocom->staterd_tree, state);
1123 pstate = state->parent;
1124 TAILQ_REMOVE(&pstate->subq, state, entry);
1125 if (pstate != &pstate->iocom->state0 &&
1126 TAILQ_EMPTY(&pstate->subq) &&
1127 (pstate->flags & KDMSG_STATE_INSERTED) == 0) {
1128 kdmsg_state_free(pstate);
1130 state->flags &= ~KDMSG_STATE_INSERTED;
1131 state->parent = NULL;
1132 kdmsg_msg_free(msg);
1133 if (TAILQ_EMPTY(&state->subq))
1134 kdmsg_state_free(state);
1135 lockmgr(&iocom->msglk, LK_RELEASE);
1137 kdmsg_msg_free(msg);
1138 lockmgr(&iocom->msglk, LK_RELEASE);
1141 kdmsg_msg_free(msg);
1146 * Simulate receiving a message which terminates an active transaction
1147 * state. Our simulated received message must set DELETE and may also
1148 * have to set CREATE. It must also ensure that all fields are set such
1149 * that the receive handling code can find the state (kdmsg_state_msgrx())
1150 * or an endless loop will ensue.
1152 * This is used when the other end of the link is dead so the device driver
1153 * gets a completed transaction for all pending states.
1157 kdmsg_state_abort(kdmsg_state_t *state)
1162 * Prevent recursive aborts which could otherwise occur if the
1163 * simulated message reception runs state->func which then turns
1164 * around and tries to reply to a broken circuit when then calls
1165 * the state abort code again.
1167 if (state->flags & KDMSG_STATE_ABORTING)
1169 state->flags |= KDMSG_STATE_ABORTING;
1172 * NOTE: Args to kdmsg_msg_alloc() to avoid dynamic state allocation.
1174 * NOTE: We are simulating a received message using our state
1175 * (vs a message generated by the other side using its state),
1176 * so we must invert DMSGF_REVTRANS and DMSGF_REVCIRC.
1178 msg = kdmsg_msg_alloc(state, DMSG_LNK_ERROR, NULL, NULL);
1179 if ((state->rxcmd & DMSGF_CREATE) == 0)
1180 msg->any.head.cmd |= DMSGF_CREATE;
1181 msg->any.head.cmd |= DMSGF_DELETE | (state->rxcmd & DMSGF_REPLY);
1182 msg->any.head.cmd ^= (DMSGF_REVTRANS | DMSGF_REVCIRC);
1183 msg->any.head.error = DMSG_ERR_LOSTLINK;
1184 kdmsg_msg_receive_handling(msg);
1188 * Process state tracking for a message prior to transmission.
1190 * Called with msglk held and the msg dequeued. Returns non-zero if
1191 * the message is bad and should be deleted by the caller.
1193 * One-off messages are usually with dummy state and msg->state may be NULL
1194 * in this situation.
1196 * New transactions (when CREATE is set) will insert the state.
1198 * May request that caller discard the message by setting *discardp to 1.
1199 * A NULL state may be returned in this case.
1203 kdmsg_state_msgtx(kdmsg_msg_t *msg)
1205 kdmsg_iocom_t *iocom = msg->state->iocom;
1206 kdmsg_state_t *state;
1210 * Make sure a state structure is ready to go in case we need a new
1211 * one. This is the only routine which uses freewr_state so no
1212 * races are possible.
1214 if ((state = iocom->freewr_state) == NULL) {
1215 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
1216 state->flags = KDMSG_STATE_DYNAMIC;
1217 state->iocom = iocom;
1218 iocom->freewr_state = state;
1222 * Lock RB tree. If persistent state is present it will have already
1223 * been assigned to msg.
1225 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1229 * Short-cut one-off or mid-stream messages (state may be NULL).
1231 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
1232 DMSGF_ABORT)) == 0) {
1233 lockmgr(&iocom->msglk, LK_RELEASE);
1239 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1240 * inside the case statements.
1242 switch(msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
1245 case DMSGF_CREATE | DMSGF_DELETE:
1247 * Insert the new persistent message state and mark
1248 * half-closed if DELETE is set. Since this is a new
1249 * message it isn't possible to transition into the fully
1250 * closed state here.
1252 * XXX state must be assigned and inserted by
1253 * kdmsg_msg_write(). txcmd is assigned by us
1256 KKASSERT(state != NULL);
1257 state->icmd = msg->any.head.cmd & DMSGF_BASECMDMASK;
1258 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
1259 state->rxcmd = DMSGF_REPLY;
1264 * Sent ABORT+DELETE in case where msgid has already
1265 * been fully closed, ignore the message.
1267 if (state == &iocom->state0) {
1268 if (msg->any.head.cmd & DMSGF_ABORT) {
1271 kprintf("kdmsg_state_msgtx: no state match "
1272 "for DELETE cmd=%08x msgid=%016jx\n",
1274 (intmax_t)msg->any.head.msgid);
1281 * Sent ABORT+DELETE in case where msgid has
1282 * already been reused for an unrelated message,
1283 * ignore the message.
1285 if ((state->txcmd & DMSGF_CREATE) == 0) {
1286 if (msg->any.head.cmd & DMSGF_ABORT) {
1289 kprintf("kdmsg_state_msgtx: state reused "
1299 * Check for mid-stream ABORT command sent
1301 if (msg->any.head.cmd & DMSGF_ABORT) {
1302 if (state == &state->iocom->state0 ||
1303 (state->txcmd & DMSGF_CREATE) == 0) {
1310 case DMSGF_REPLY | DMSGF_CREATE:
1311 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
1313 * When transmitting a reply with CREATE set the original
1314 * persistent state message should already exist.
1316 if (state == &state->iocom->state0) {
1317 kprintf("kdmsg_state_msgtx: no state match "
1318 "for REPLY | CREATE\n");
1322 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
1325 case DMSGF_REPLY | DMSGF_DELETE:
1327 * When transmitting a reply with DELETE set the original
1328 * persistent state message should already exist.
1330 * This is very similar to the REPLY|CREATE|* case except
1331 * txcmd is already stored, so we just add the DELETE flag.
1333 * Sent REPLY+ABORT+DELETE in case where msgid has
1334 * already been fully closed, ignore the message.
1336 if (state == &state->iocom->state0) {
1337 if (msg->any.head.cmd & DMSGF_ABORT) {
1340 kprintf("kdmsg_state_msgtx: no state match "
1341 "for REPLY | DELETE\n");
1348 * Sent REPLY+ABORT+DELETE in case where msgid has already
1349 * been reused for an unrelated message, ignore the message.
1351 if ((state->txcmd & DMSGF_CREATE) == 0) {
1352 if (msg->any.head.cmd & DMSGF_ABORT) {
1355 kprintf("kdmsg_state_msgtx: state reused "
1356 "for REPLY | DELETE\n");
1365 * Check for mid-stream ABORT reply sent.
1367 * One-off REPLY messages are allowed for e.g. status updates.
1369 if (msg->any.head.cmd & DMSGF_ABORT) {
1370 if (state == &state->iocom->state0 ||
1371 (state->txcmd & DMSGF_CREATE) == 0) {
1381 * Set interlock (XXX hack) in case the send side blocks and a
1382 * response is returned before kdmsg_state_cleanuptx() can be
1385 if (state && error == 0)
1386 state->flags |= KDMSG_STATE_INTERLOCK;
1388 lockmgr(&iocom->msglk, LK_RELEASE);
1395 kdmsg_state_cleanuptx(kdmsg_msg_t *msg)
1397 kdmsg_iocom_t *iocom = msg->state->iocom;
1398 kdmsg_state_t *state;
1399 kdmsg_state_t *pstate;
1401 if ((state = msg->state) == NULL) {
1402 kdmsg_msg_free(msg);
1406 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1409 * Clear interlock (XXX hack) in case the send side blocks and a
1410 * response is returned in the other thread before
1411 * kdmsg_state_cleanuptx() can be run. We maintain our hold on
1412 * iocom->msglk so we can do this before completing our task.
1414 if (state->flags & KDMSG_STATE_SIGNAL) {
1415 kprintf("kdmsg: state %p interlock!\n", state);
1418 state->flags &= ~(KDMSG_STATE_INTERLOCK | KDMSG_STATE_SIGNAL);
1420 if (msg->any.head.cmd & DMSGF_DELETE) {
1421 KKASSERT((state->txcmd & DMSGF_DELETE) == 0);
1422 state->txcmd |= DMSGF_DELETE;
1423 if (state->rxcmd & DMSGF_DELETE) {
1424 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
1425 if (state->txcmd & DMSGF_REPLY) {
1426 KKASSERT(msg->any.head.cmd &
1428 RB_REMOVE(kdmsg_state_tree,
1429 &iocom->staterd_tree, state);
1431 KKASSERT((msg->any.head.cmd &
1433 RB_REMOVE(kdmsg_state_tree,
1434 &iocom->statewr_tree, state);
1436 pstate = state->parent;
1437 TAILQ_REMOVE(&pstate->subq, state, entry);
1438 if (pstate != &pstate->iocom->state0 &&
1439 TAILQ_EMPTY(&pstate->subq) &&
1440 (pstate->flags & KDMSG_STATE_INSERTED) == 0) {
1441 kdmsg_state_free(pstate);
1443 state->flags &= ~KDMSG_STATE_INSERTED;
1444 state->parent = NULL;
1445 kdmsg_msg_free(msg);
1446 if (TAILQ_EMPTY(&state->subq))
1447 kdmsg_state_free(state);
1449 kdmsg_msg_free(msg);
1452 kdmsg_msg_free(msg);
1454 lockmgr(&iocom->msglk, LK_RELEASE);
1459 kdmsg_state_free(kdmsg_state_t *state)
1461 kdmsg_iocom_t *iocom = state->iocom;
1463 KKASSERT((state->flags & KDMSG_STATE_INSERTED) == 0);
1464 kfree(state, iocom->mmsg);
1468 kdmsg_msg_alloc(kdmsg_state_t *state, uint32_t cmd,
1469 int (*func)(kdmsg_state_t *, kdmsg_msg_t *), void *data)
1471 kdmsg_iocom_t *iocom = state->iocom;
1472 kdmsg_state_t *pstate;
1476 KKASSERT(iocom != NULL);
1477 hbytes = (cmd & DMSGF_SIZE) * DMSG_ALIGN;
1478 msg = kmalloc(offsetof(struct kdmsg_msg, any) + hbytes,
1479 iocom->mmsg, M_WAITOK | M_ZERO);
1480 msg->hdr_size = hbytes;
1482 if ((cmd & (DMSGF_CREATE | DMSGF_REPLY)) == DMSGF_CREATE) {
1484 * New transaction, requires tracking state and a unique
1485 * msgid to be allocated.
1488 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
1489 TAILQ_INIT(&state->subq);
1490 state->iocom = iocom;
1491 state->parent = pstate;
1492 state->flags = KDMSG_STATE_DYNAMIC;
1494 state->any.any = data;
1495 state->msgid = (uint64_t)(uintptr_t)state;
1496 /*msg->any.head.msgid = state->msgid;XXX*/
1498 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1499 if (RB_INSERT(kdmsg_state_tree, &iocom->statewr_tree, state))
1500 panic("duplicate msgid allocated");
1501 TAILQ_INSERT_TAIL(&pstate->subq, state, entry);
1502 state->flags |= KDMSG_STATE_INSERTED;
1503 lockmgr(&iocom->msglk, LK_RELEASE);
1505 pstate = state->parent;
1508 if (state->flags & KDMSG_STATE_OPPOSITE)
1509 cmd |= DMSGF_REVTRANS;
1510 if (pstate->flags & KDMSG_STATE_OPPOSITE)
1511 cmd |= DMSGF_REVCIRC;
1513 msg->any.head.magic = DMSG_HDR_MAGIC;
1514 msg->any.head.cmd = cmd;
1515 msg->any.head.msgid = state->msgid;
1516 msg->any.head.circuit = pstate->msgid;
1523 kdmsg_msg_free(kdmsg_msg_t *msg)
1525 kdmsg_iocom_t *iocom = msg->state->iocom;
1527 if ((msg->flags & KDMSG_FLAG_AUXALLOC) &&
1528 msg->aux_data && msg->aux_size) {
1529 kfree(msg->aux_data, iocom->mmsg);
1530 msg->flags &= ~KDMSG_FLAG_AUXALLOC;
1533 msg->aux_data = NULL;
1536 kfree(msg, iocom->mmsg);
1540 kdmsg_detach_aux_data(kdmsg_msg_t *msg, kdmsg_data_t *data)
1542 if (msg->flags & KDMSG_FLAG_AUXALLOC) {
1543 data->aux_data = msg->aux_data;
1544 data->aux_size = msg->aux_size;
1545 data->iocom = msg->state->iocom;
1546 msg->flags &= ~KDMSG_FLAG_AUXALLOC;
1548 data->aux_data = NULL;
1550 data->iocom = msg->state->iocom;
1555 kdmsg_free_aux_data(kdmsg_data_t *data)
1558 kfree(data->aux_data, data->iocom->mmsg);
1562 * Indexed messages are stored in a red-black tree indexed by their
1563 * msgid. Only persistent messages are indexed.
1566 kdmsg_state_cmp(kdmsg_state_t *state1, kdmsg_state_t *state2)
1568 if (state1->iocom < state2->iocom)
1570 if (state1->iocom > state2->iocom)
1572 if (state1->msgid < state2->msgid)
1574 if (state1->msgid > state2->msgid)
1580 * Write a message. All requisit command flags have been set.
1582 * If msg->state is non-NULL the message is written to the existing
1583 * transaction. msgid will be set accordingly.
1585 * If msg->state is NULL and CREATE is set new state is allocated and
1586 * (func, data) is installed. A msgid is assigned.
1588 * If msg->state is NULL and CREATE is not set the message is assumed
1589 * to be a one-way message. The originator must assign the msgid
1590 * (or leave it 0, which is typical.
1592 * This function merely queues the message to the management thread, it
1593 * does not write to the message socket/pipe.
1596 kdmsg_msg_write(kdmsg_msg_t *msg)
1598 kdmsg_iocom_t *iocom = msg->state->iocom;
1599 kdmsg_state_t *state;
1603 * Continuance or termination of existing transaction.
1604 * The transaction could have been initiated by either end.
1606 * (Function callback and aux data for the receive side can
1607 * be replaced or left alone).
1610 msg->any.head.msgid = state->msgid;
1613 * One-off message (always uses msgid 0 to distinguish
1614 * between a possibly lost in-transaction message due to
1615 * competing aborts and a real one-off message?)
1618 msg->any.head.msgid = 0;
1621 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1624 * This flag is not set until after the tx thread has drained
1625 * the tx msgq and simulated responses. After that point the
1626 * txthread is dead and can no longer simulate responses.
1628 * Device drivers should never try to send a message once this
1629 * flag is set. They should have detected (through the state
1630 * closures) that the link is in trouble.
1632 if (iocom->flags & KDMSG_IOCOMF_EXITNOACC) {
1633 lockmgr(&iocom->msglk, LK_RELEASE);
1634 panic("kdmsg_msg_write: Attempt to write message to "
1635 "terminated iocom\n");
1639 * Finish up the msg fields. Note that msg->aux_size and the
1640 * aux_bytes stored in the message header represent the unaligned
1641 * (actual) bytes of data, but the buffer is sized to an aligned
1642 * size and the CRC is generated over the aligned length.
1644 msg->any.head.salt = /* (random << 8) | */ (iocom->msg_seq & 255);
1647 if (msg->aux_data && msg->aux_size) {
1648 uint32_t abytes = DMSG_DOALIGN(msg->aux_size);
1650 msg->any.head.aux_bytes = msg->aux_size;
1651 msg->any.head.aux_crc = iscsi_crc32(msg->aux_data, abytes);
1653 msg->any.head.hdr_crc = 0;
1654 msg->any.head.hdr_crc = iscsi_crc32(msg->any.buf, msg->hdr_size);
1656 TAILQ_INSERT_TAIL(&iocom->msgq, msg, qentry);
1658 if (iocom->msg_ctl & KDMSG_CLUSTERCTL_SLEEPING) {
1659 atomic_clear_int(&iocom->msg_ctl,
1660 KDMSG_CLUSTERCTL_SLEEPING);
1661 wakeup(&iocom->msg_ctl);
1664 lockmgr(&iocom->msglk, LK_RELEASE);
1668 * Reply to a message and terminate our side of the transaction.
1670 * If msg->state is non-NULL we are replying to a one-way message.
1673 kdmsg_msg_reply(kdmsg_msg_t *msg, uint32_t error)
1675 kdmsg_state_t *state = msg->state;
1680 * Reply with a simple error code and terminate the transaction.
1682 cmd = DMSG_LNK_ERROR;
1685 * Check if our direction has even been initiated yet, set CREATE.
1687 * Check what direction this is (command or reply direction). Note
1688 * that txcmd might not have been initiated yet.
1690 * If our direction has already been closed we just return without
1693 if (state != &state->iocom->state0) {
1694 if (state->txcmd & DMSGF_DELETE)
1696 if ((state->txcmd & DMSGF_CREATE) == 0)
1697 cmd |= DMSGF_CREATE;
1698 if (state->txcmd & DMSGF_REPLY)
1700 cmd |= DMSGF_DELETE;
1702 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1706 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1707 nmsg->any.head.error = error;
1708 kdmsg_msg_write(nmsg);
1712 * Reply to a message and continue our side of the transaction.
1714 * If msg->state is non-NULL we are replying to a one-way message and this
1715 * function degenerates into the same as kdmsg_msg_reply().
1718 kdmsg_msg_result(kdmsg_msg_t *msg, uint32_t error)
1720 kdmsg_state_t *state = msg->state;
1725 * Return a simple result code, do NOT terminate the transaction.
1727 cmd = DMSG_LNK_ERROR;
1730 * Check if our direction has even been initiated yet, set CREATE.
1732 * Check what direction this is (command or reply direction). Note
1733 * that txcmd might not have been initiated yet.
1735 * If our direction has already been closed we just return without
1738 if (state != &state->iocom->state0) {
1739 if (state->txcmd & DMSGF_DELETE)
1741 if ((state->txcmd & DMSGF_CREATE) == 0)
1742 cmd |= DMSGF_CREATE;
1743 if (state->txcmd & DMSGF_REPLY)
1745 /* continuing transaction, do not set MSGF_DELETE */
1747 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1751 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1752 nmsg->any.head.error = error;
1753 kdmsg_msg_write(nmsg);
1757 * Reply to a message and terminate our side of the transaction.
1759 * If msg->state is non-NULL we are replying to a one-way message.
1762 kdmsg_state_reply(kdmsg_state_t *state, uint32_t error)
1768 * Reply with a simple error code and terminate the transaction.
1770 cmd = DMSG_LNK_ERROR;
1773 * Check if our direction has even been initiated yet, set CREATE.
1775 * Check what direction this is (command or reply direction). Note
1776 * that txcmd might not have been initiated yet.
1778 * If our direction has already been closed we just return without
1782 if (state->txcmd & DMSGF_DELETE)
1784 if ((state->txcmd & DMSGF_CREATE) == 0)
1785 cmd |= DMSGF_CREATE;
1786 if (state->txcmd & DMSGF_REPLY)
1788 cmd |= DMSGF_DELETE;
1790 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1791 nmsg->any.head.error = error;
1792 kdmsg_msg_write(nmsg);
1796 * Reply to a message and continue our side of the transaction.
1798 * If msg->state is non-NULL we are replying to a one-way message and this
1799 * function degenerates into the same as kdmsg_msg_reply().
1802 kdmsg_state_result(kdmsg_state_t *state, uint32_t error)
1808 * Return a simple result code, do NOT terminate the transaction.
1810 cmd = DMSG_LNK_ERROR;
1813 * Check if our direction has even been initiated yet, set CREATE.
1815 * Check what direction this is (command or reply direction). Note
1816 * that txcmd might not have been initiated yet.
1818 * If our direction has already been closed we just return without
1822 if (state->txcmd & DMSGF_DELETE)
1824 if ((state->txcmd & DMSGF_CREATE) == 0)
1825 cmd |= DMSGF_CREATE;
1826 if (state->txcmd & DMSGF_REPLY)
1828 /* continuing transaction, do not set MSGF_DELETE */
1830 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1831 nmsg->any.head.error = error;
1832 kdmsg_msg_write(nmsg);