2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static int hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
39 static void hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
42 * Initialize a low-level ioq
45 hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
47 bzero(ioq, sizeof(*ioq));
48 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
49 TAILQ_INIT(&ioq->msgq);
55 * caller holds iocom->mtx.
58 hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
62 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
63 assert(0); /* shouldn't happen */
64 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
65 hammer2_msg_free(iocom, msg);
67 if ((msg = ioq->msg) != NULL) {
69 hammer2_msg_free(iocom, msg);
74 * Initialize a low-level communications channel
77 hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd)
79 bzero(iocom, sizeof(*iocom));
81 pthread_mutex_init(&iocom->mtx, NULL);
82 RB_INIT(&iocom->staterd_tree);
83 RB_INIT(&iocom->statewr_tree);
84 TAILQ_INIT(&iocom->freeq);
85 TAILQ_INIT(&iocom->freeq_aux);
86 TAILQ_INIT(&iocom->addrq);
87 TAILQ_INIT(&iocom->txmsgq);
88 iocom->sock_fd = sock_fd;
89 iocom->alt_fd = alt_fd;
90 iocom->flags = HAMMER2_IOCOMF_RREQ;
91 hammer2_ioq_init(iocom, &iocom->ioq_rx);
92 hammer2_ioq_init(iocom, &iocom->ioq_tx);
93 if (pipe(iocom->wakeupfds) < 0)
95 fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
96 fcntl(iocom->wakeupfds[1], F_SETFL, O_NONBLOCK);
99 * Negotiate session crypto synchronously. This will mark the
100 * connection as error'd if it fails.
102 hammer2_crypto_negotiate(iocom);
105 * Make sure our fds are set to non-blocking for the iocom core.
108 fcntl(sock_fd, F_SETFL, O_NONBLOCK);
110 /* if line buffered our single fgets() should be fine */
112 fcntl(alt_fd, F_SETFL, O_NONBLOCK);
117 * Cleanup a terminating iocom.
119 * Caller should not hold iocom->mtx. The iocom has already been disconnected
120 * from all possible references to it.
123 hammer2_iocom_done(hammer2_iocom_t *iocom)
127 if (iocom->sock_fd >= 0) {
128 close(iocom->sock_fd);
131 if (iocom->alt_fd >= 0) {
132 close(iocom->alt_fd);
135 hammer2_ioq_done(iocom, &iocom->ioq_rx);
136 hammer2_ioq_done(iocom, &iocom->ioq_tx);
137 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
138 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
141 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
142 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
144 msg->aux_data = NULL;
147 if (iocom->wakeupfds[0] >= 0) {
148 close(iocom->wakeupfds[0]);
149 iocom->wakeupfds[0] = -1;
151 if (iocom->wakeupfds[1] >= 0) {
152 close(iocom->wakeupfds[1]);
153 iocom->wakeupfds[1] = -1;
155 pthread_mutex_destroy(&iocom->mtx);
159 * Allocate a new one-way message.
162 hammer2_msg_alloc(hammer2_iocom_t *iocom, size_t aux_size, uint32_t cmd)
167 pthread_mutex_lock(&iocom->mtx);
169 aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
170 ~HAMMER2_MSG_ALIGNMASK;
171 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
172 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
174 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
175 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
177 pthread_mutex_unlock(&iocom->mtx);
179 msg = malloc(sizeof(*msg));
180 bzero(msg, sizeof(*msg));
181 msg->aux_data = NULL;
184 if (msg->aux_size != aux_size) {
187 msg->aux_data = NULL;
191 msg->aux_data = malloc(aux_size);
192 msg->aux_size = aux_size;
195 hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
197 bzero(&msg->any.head, hbytes);
198 msg->hdr_size = hbytes;
199 msg->any.head.cmd = cmd;
200 msg->any.head.aux_descr = 0;
201 msg->any.head.aux_crc = 0;
207 * Free a message so it can be reused afresh.
209 * NOTE: aux_size can be 0 with a non-NULL aux_data.
213 hammer2_msg_free_locked(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
217 TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
219 TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
223 hammer2_msg_free(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
225 pthread_mutex_lock(&iocom->mtx);
226 hammer2_msg_free_locked(iocom, msg);
227 pthread_mutex_unlock(&iocom->mtx);
231 * I/O core loop for an iocom.
233 * Thread localized, iocom->mtx not held.
236 hammer2_iocom_core(hammer2_iocom_t *iocom,
237 void (*recvmsg_func)(hammer2_iocom_t *),
238 void (*sendmsg_func)(hammer2_iocom_t *),
239 void (*altmsg_func)(hammer2_iocom_t *))
241 struct pollfd fds[3];
245 int wi; /* wakeup pipe */
247 int ai; /* alt bulk path socket */
249 iocom->recvmsg_callback = recvmsg_func;
250 iocom->sendmsg_callback = sendmsg_func;
251 iocom->altmsg_callback = altmsg_func;
253 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
254 if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
255 HAMMER2_IOCOMF_WWORK |
256 HAMMER2_IOCOMF_PWORK |
257 HAMMER2_IOCOMF_ARWORK |
258 HAMMER2_IOCOMF_AWWORK)) == 0) {
260 * Only poll if no immediate work is pending.
261 * Otherwise we are just wasting our time calling
272 * Always check the inter-thread pipe, e.g.
273 * for iocom->txmsgq work.
276 fds[wi].fd = iocom->wakeupfds[0];
277 fds[wi].events = POLLIN;
281 * Check the socket input/output direction as
284 if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
285 HAMMER2_IOCOMF_WREQ)) {
287 fds[si].fd = iocom->sock_fd;
291 if (iocom->flags & HAMMER2_IOCOMF_RREQ)
292 fds[si].events |= POLLIN;
293 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
294 fds[si].events |= POLLOUT;
298 * Check the alternative fd for work.
300 if (iocom->alt_fd >= 0) {
302 fds[ai].fd = iocom->alt_fd;
303 fds[ai].events = POLLIN;
306 poll(fds, count, timeout);
308 if (wi >= 0 && (fds[wi].revents & POLLIN))
309 iocom->flags |= HAMMER2_IOCOMF_PWORK;
310 if (si >= 0 && (fds[si].revents & POLLIN))
311 iocom->flags |= HAMMER2_IOCOMF_RWORK;
312 if (si >= 0 && (fds[si].revents & POLLOUT))
313 iocom->flags |= HAMMER2_IOCOMF_WWORK;
314 if (wi >= 0 && (fds[wi].revents & POLLOUT))
315 iocom->flags |= HAMMER2_IOCOMF_WWORK;
316 if (ai >= 0 && (fds[ai].revents & POLLIN))
317 iocom->flags |= HAMMER2_IOCOMF_ARWORK;
320 * Always check the pipe
322 iocom->flags |= HAMMER2_IOCOMF_PWORK;
326 * Pending message queues from other threads wake us up
327 * with a write to the wakeupfds[] pipe. We have to clear
328 * the pipe with a dummy read.
330 if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
331 iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
332 read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
333 iocom->flags |= HAMMER2_IOCOMF_RWORK;
334 iocom->flags |= HAMMER2_IOCOMF_WWORK;
335 if (TAILQ_FIRST(&iocom->txmsgq))
336 iocom->sendmsg_callback(iocom);
340 * Message write sequencing
342 if (iocom->flags & HAMMER2_IOCOMF_WWORK)
343 iocom->sendmsg_callback(iocom);
346 * Message read sequencing. Run this after the write
347 * sequencing in case the write sequencing allowed another
348 * auto-DELETE to occur on the read side.
350 if (iocom->flags & HAMMER2_IOCOMF_RWORK)
351 iocom->recvmsg_callback(iocom);
353 if (iocom->flags & HAMMER2_IOCOMF_ARWORK)
354 iocom->altmsg_callback(iocom);
359 * Read the next ready message from the ioq, issuing I/O if needed.
360 * Caller should retry on a read-event when NULL is returned.
362 * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
363 * be returned for each open transaction, then the ioq and iocom
364 * will be errored out and a non-transactional HAMMER2_LNK_ERROR
365 * msg will be returned as the final message. The caller should not call
366 * us again after the final message is returned.
368 * Thread localized, iocom->mtx not held.
371 hammer2_ioq_read(hammer2_iocom_t *iocom)
373 hammer2_ioq_t *ioq = &iocom->ioq_rx;
375 hammer2_msg_hdr_t *head;
376 hammer2_state_t *state;
384 iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
387 * If a message is already pending we can just remove and
388 * return it. Message state has already been processed.
390 if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
391 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
396 * Message read in-progress (msg is NULL at the moment). We don't
397 * allocate a msg until we have its core header.
399 bytes = ioq->fifo_end - ioq->fifo_beg;
400 nmax = sizeof(ioq->buf) - ioq->fifo_end;
404 case HAMMER2_MSGQ_STATE_HEADER1:
406 * Load the primary header, fail on any non-trivial read
407 * error or on EOF. Since the primary header is the same
408 * size is the message alignment it will never straddle
409 * the end of the buffer.
411 if (bytes < (int)sizeof(msg->any.head)) {
412 n = read(iocom->sock_fd,
413 ioq->buf + ioq->fifo_end,
417 ioq->error = HAMMER2_IOQ_ERROR_EOF;
420 if (errno != EINTR &&
421 errno != EINPROGRESS &&
423 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
435 * Insufficient data accumulated (msg is NULL, caller will
439 if (bytes < (int)sizeof(msg->any.head))
443 * Calculate the header, decrypt data received so far.
444 * Data will be decrypted in-place. Partial blocks are
445 * not immediately decrypted.
447 * WARNING! The header might be in the wrong endian, we
448 * do not fix it up until we get the entire
451 hammer2_crypto_decrypt(iocom, ioq);
452 head = (void *)(ioq->buf + ioq->fifo_beg);
455 * Check and fixup the core header. Note that the icrc
456 * has to be calculated before any fixups, but the crc
457 * fields in the msg may have to be swapped like everything
460 if (head->magic != HAMMER2_MSGHDR_MAGIC &&
461 head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
462 ioq->error = HAMMER2_IOQ_ERROR_SYNC;
467 * Calculate the full header size and aux data size
469 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
470 ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
472 ioq->abytes = bswap32(head->aux_bytes) *
475 ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
477 ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
479 if (ioq->hbytes < sizeof(msg->any.head) ||
480 ioq->hbytes > sizeof(msg->any) ||
481 ioq->abytes > HAMMER2_MSGAUX_MAX) {
482 ioq->error = HAMMER2_IOQ_ERROR_FIELD;
487 * Finally allocate the message and copy the core header
488 * to the embedded extended header.
490 * Initialize msg->aux_size to 0 and use it to track
491 * the amount of data copied from the stream.
493 msg = hammer2_msg_alloc(iocom, ioq->abytes, 0);
497 * We are either done or we fall-through
499 if (ioq->hbytes == sizeof(msg->any.head) && ioq->abytes == 0) {
500 bcopy(head, &msg->any.head, sizeof(msg->any.head));
501 ioq->fifo_beg += ioq->hbytes;
506 * Fall through to the next state. Make sure that the
507 * extended header does not straddle the end of the buffer.
508 * We still want to issue larger reads into our buffer,
509 * book-keeping is easier if we don't bcopy() yet.
511 if (bytes + nmax < ioq->hbytes) {
512 bcopy(ioq->buf + ioq->fifo_beg, ioq->buf, bytes);
513 ioq->fifo_cdx -= ioq->fifo_beg;
515 ioq->fifo_end = bytes;
516 nmax = sizeof(ioq->buf) - ioq->fifo_end;
518 ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
520 case HAMMER2_MSGQ_STATE_HEADER2:
522 * Fill out the extended header.
525 if (bytes < ioq->hbytes) {
526 n = read(iocom->sock_fd,
527 msg->any.buf + ioq->fifo_end,
531 ioq->error = HAMMER2_IOQ_ERROR_EOF;
534 if (errno != EINTR &&
535 errno != EINPROGRESS &&
537 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
549 * Insufficient data accumulated (set msg NULL so caller will
552 if (bytes < ioq->hbytes) {
558 * Calculate the extended header, decrypt data received
559 * so far. Handle endian-conversion for the entire extended
562 hammer2_crypto_decrypt(iocom, ioq);
563 head = (void *)(ioq->buf + ioq->fifo_beg);
568 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
569 xcrc32 = bswap32(head->hdr_crc);
571 xcrc32 = head->hdr_crc;
573 if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
574 ioq->error = HAMMER2_IOQ_ERROR_XCRC;
577 head->hdr_crc = xcrc32;
579 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
580 hammer2_bswap_head(head);
584 * Copy the extended header into the msg and adjust the
587 bcopy(head, &msg->any, ioq->hbytes);
590 * We are either done or we fall-through.
592 if (ioq->abytes == 0) {
593 ioq->fifo_beg += ioq->hbytes;
598 * Must adjust nmax and bytes (and the state) when falling
601 ioq->fifo_beg += ioq->hbytes;
603 bytes -= ioq->hbytes;
604 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
606 case HAMMER2_MSGQ_STATE_AUXDATA1:
608 * Copy the partial or complete payload from remaining
609 * bytes in the FIFO. We have to fall-through either
610 * way so we can check the crc.
612 * Adjust msg->aux_size to the final actual value.
614 ioq->already = ioq->fifo_cdx - ioq->fifo_beg;
615 if (ioq->already > ioq->abytes)
616 ioq->already = ioq->abytes;
617 if (bytes >= ioq->abytes) {
618 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
620 msg->aux_size = ioq->abytes;
621 ioq->fifo_beg += ioq->abytes;
622 if (ioq->fifo_cdx < ioq->fifo_beg)
623 ioq->fifo_cdx = ioq->fifo_beg;
624 bytes -= ioq->abytes;
626 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
628 msg->aux_size = bytes;
629 ioq->fifo_beg += bytes;
630 if (ioq->fifo_cdx < ioq->fifo_beg)
631 ioq->fifo_cdx = ioq->fifo_beg;
636 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
638 case HAMMER2_MSGQ_STATE_AUXDATA2:
640 * Read the remainder of the payload directly into the
641 * msg->aux_data buffer.
644 if (msg->aux_size < ioq->abytes) {
646 n = read(iocom->sock_fd,
647 msg->aux_data + msg->aux_size,
648 ioq->abytes - msg->aux_size);
651 ioq->error = HAMMER2_IOQ_ERROR_EOF;
654 if (errno != EINTR &&
655 errno != EINPROGRESS &&
657 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
667 * Insufficient data accumulated (set msg NULL so caller will
670 if (msg->aux_size < ioq->abytes) {
674 assert(msg->aux_size == ioq->abytes);
675 hammer2_crypto_decrypt_aux(iocom, ioq, msg, ioq->already);
678 * Check aux_crc, then we are done.
680 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
681 if (xcrc32 != msg->any.head.aux_crc) {
682 ioq->error = HAMMER2_IOQ_ERROR_ACRC;
686 case HAMMER2_MSGQ_STATE_ERROR:
688 * Continued calls to drain recorded transactions (returning
689 * a LNK_ERROR for each one), before we return the final
696 * We don't double-return errors, the caller should not
697 * have called us again after getting an error msg.
704 * Check the message sequence. The iv[] should prevent any
705 * possibility of a replay but we add this check anyway.
707 if (msg && ioq->error == 0) {
708 if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
709 ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
716 * Process transactional state for the message.
718 if (msg && ioq->error == 0) {
719 error = hammer2_state_msgrx(iocom, msg);
721 if (error == HAMMER2_IOQ_ERROR_EALREADY) {
722 hammer2_msg_free(iocom, msg);
730 * Handle error, RREQ, or completion
732 * NOTE: nmax and bytes are invalid at this point, we don't bother
733 * to update them when breaking out.
737 * An unrecoverable error causes all active receive
738 * transactions to be terminated with a LNK_ERROR message.
740 * Once all active transactions are exhausted we set the
741 * iocom ERROR flag and return a non-transactional LNK_ERROR
742 * message, which should cause master processing loops to
745 assert(ioq->msg == msg);
747 hammer2_msg_free(iocom, msg);
752 * No more I/O read processing
754 ioq->state = HAMMER2_MSGQ_STATE_ERROR;
757 * Simulate a remote LNK_ERROR DELETE msg for any open
758 * transactions, ending with a final non-transactional
759 * LNK_ERROR (that the session can detect) when no
760 * transactions remain.
762 msg = hammer2_msg_alloc(iocom, 0, 0);
763 bzero(&msg->any.head, sizeof(msg->any.head));
764 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
765 msg->any.head.cmd = HAMMER2_LNK_ERROR;
766 msg->any.head.error = ioq->error;
768 pthread_mutex_lock(&iocom->mtx);
769 if ((state = RB_ROOT(&iocom->staterd_tree)) != NULL) {
771 * Active remote transactions are still present.
772 * Simulate the other end sending us a DELETE.
774 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
775 fprintf(stderr, "SIMULATE DELETION RCONT %p\n", state);
776 hammer2_msg_free(iocom, msg);
779 fprintf(stderr, "SIMULATE DELETION %p RD RXCMD %08x\n", state, state->rxcmd);
780 /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
782 msg->any.head.spanid = state->spanid;
783 msg->any.head.msgid = state->msgid;
784 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
787 } else if ((state = RB_ROOT(&iocom->statewr_tree)) != NULL) {
789 * Active local transactions are still present.
790 * Simulate the other end sending us a DELETE.
792 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
793 fprintf(stderr, "SIMULATE DELETION WCONT\n");
794 hammer2_msg_free(iocom, msg);
797 fprintf(stderr, "SIMULATE DELETION WD RXCMD %08x\n", state->txcmd);
798 /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
800 msg->any.head.spanid = state->spanid;
801 msg->any.head.msgid = state->msgid;
802 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
803 HAMMER2_MSGF_DELETE |
805 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
812 * No active local or remote transactions remain.
813 * Generate a final LNK_ERROR and flag EOF.
816 iocom->flags |= HAMMER2_IOCOMF_EOF;
817 fprintf(stderr, "EOF ON SOCKET\n");
819 pthread_mutex_unlock(&iocom->mtx);
822 * For the iocom error case we want to set RWORK to indicate
823 * that more messages might be pending.
825 * It is possible to return NULL when there is more work to
826 * do because each message has to be DELETEd in both
827 * directions before we continue on with the next (though
828 * this could be optimized). The transmit direction will
832 iocom->flags |= HAMMER2_IOCOMF_RWORK;
833 } else if (msg == NULL) {
835 * Insufficient data received to finish building the message,
836 * set RREQ and return NULL.
838 * Leave ioq->msg intact.
839 * Leave the FIFO intact.
841 iocom->flags |= HAMMER2_IOCOMF_RREQ;
846 * The fifo has already been advanced past the message.
847 * Trivially reset the FIFO indices if possible.
849 * clear the FIFO if it is now empty and set RREQ to wait
850 * for more from the socket. If the FIFO is not empty set
851 * TWORK to bypass the poll so we loop immediately.
853 if (ioq->fifo_beg == ioq->fifo_end) {
854 iocom->flags |= HAMMER2_IOCOMF_RREQ;
859 iocom->flags |= HAMMER2_IOCOMF_RWORK;
861 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
868 * Calculate the header and data crc's and write a low-level message to
869 * the connection. If aux_crc is non-zero the aux_data crc is already
870 * assumed to have been set.
872 * A non-NULL msg is added to the queue but not necessarily flushed.
873 * Calling this function with msg == NULL will get a flush going.
875 * Caller must hold iocom->mtx.
878 hammer2_iocom_flush1(hammer2_iocom_t *iocom)
880 hammer2_ioq_t *ioq = &iocom->ioq_tx;
884 hammer2_msg_queue_t tmpq;
886 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
888 pthread_mutex_lock(&iocom->mtx);
889 while ((msg = TAILQ_FIRST(&iocom->txmsgq)) != NULL) {
890 TAILQ_REMOVE(&iocom->txmsgq, msg, qentry);
891 TAILQ_INSERT_TAIL(&tmpq, msg, qentry);
893 pthread_mutex_unlock(&iocom->mtx);
895 while ((msg = TAILQ_FIRST(&tmpq)) != NULL) {
897 * Process terminal connection errors.
899 TAILQ_REMOVE(&tmpq, msg, qentry);
901 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
907 * Finish populating the msg fields. The salt ensures that
908 * the iv[] array is ridiculously randomized and we also
909 * re-seed our PRNG every 32768 messages just to be sure.
911 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
912 msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
914 if ((ioq->seq & 32767) == 0)
918 * Calculate aux_crc if 0, then calculate hdr_crc.
920 if (msg->aux_size && msg->any.head.aux_crc == 0) {
921 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
922 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
923 msg->any.head.aux_crc = xcrc32;
925 msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
926 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
928 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
930 msg->any.head.hdr_crc = 0;
931 msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
934 * Enqueue the message (the flush codes handles stream
937 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
940 hammer2_iocom_flush2(iocom);
944 * Thread localized, iocom->mtx not held by caller.
947 hammer2_iocom_flush2(hammer2_iocom_t *iocom)
949 hammer2_ioq_t *ioq = &iocom->ioq_tx;
953 struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
961 hammer2_iocom_drain(iocom);
966 * Pump messages out the connection by building an iovec.
971 TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
973 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
976 abytes = msg->aux_size;
981 if (hbytes - hoff > 0) {
982 iov[n].iov_base = (char *)&msg->any.head + hoff;
983 iov[n].iov_len = hbytes - hoff;
984 nmax += hbytes - hoff;
986 if (n == HAMMER2_IOQ_MAXIOVEC)
989 if (abytes - aoff > 0) {
990 assert(msg->aux_data != NULL);
991 iov[n].iov_base = msg->aux_data + aoff;
992 iov[n].iov_len = abytes - aoff;
993 nmax += abytes - aoff;
995 if (n == HAMMER2_IOQ_MAXIOVEC)
1003 * Encrypt and write the data. The crypto code will move the
1004 * data into the fifo and adjust the iov as necessary. If
1005 * encryption is disabled the iov is left alone.
1007 * hammer2_crypto_encrypt_wrote()
1009 n = hammer2_crypto_encrypt(iocom, ioq, iov, n);
1012 * Execute the writev() then figure out what happened.
1014 nact = writev(iocom->sock_fd, iov, n);
1016 if (errno != EINTR &&
1017 errno != EINPROGRESS &&
1022 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
1023 hammer2_iocom_drain(iocom);
1026 * Wait for socket buffer space
1028 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1034 * Indicate bytes written successfully. If we were unable to
1035 * write the entire iov array then set WREQ to wait for more
1036 * socket buffer space.
1038 hammer2_crypto_encrypt_wrote(iocom, ioq, nact);
1040 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1043 * Clean out the transmit queue based on what we successfully
1046 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1047 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1049 abytes = msg->aux_size;
1051 if ((size_t)nact < hbytes - ioq->hbytes) {
1052 ioq->hbytes += nact;
1055 nact -= hbytes - ioq->hbytes;
1056 ioq->hbytes = hbytes;
1057 if ((size_t)nact < abytes - ioq->abytes) {
1058 ioq->abytes += nact;
1061 nact -= abytes - ioq->abytes;
1063 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1068 hammer2_state_cleanuptx(iocom, msg);
1071 hammer2_iocom_drain(iocom);
1076 * Kill pending msgs on ioq_tx and adjust the flags such that no more
1077 * write events will occur. We don't kill read msgs because we want
1078 * the caller to pull off our contrived terminal error msg to detect
1079 * the connection failure.
1081 * Thread localized, iocom->mtx not held by caller.
1084 hammer2_iocom_drain(hammer2_iocom_t *iocom)
1086 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1089 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
1091 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1092 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1094 hammer2_state_cleanuptx(iocom, msg);
1099 * Write a message to an iocom, with additional state processing.
1102 hammer2_msg_write(hammer2_iocom_t *iocom, hammer2_msg_t *msg,
1103 void (*func)(hammer2_state_t *, hammer2_msg_t *),
1105 hammer2_state_t **statep)
1107 hammer2_state_t *state;
1111 * Handle state processing, create state if necessary.
1113 pthread_mutex_lock(&iocom->mtx);
1114 if ((state = msg->state) != NULL) {
1116 * Existing transaction (could be reply). It is also
1117 * possible for this to be the first reply (CREATE is set),
1118 * in which case we populate state->txcmd.
1120 msg->any.head.msgid = state->msgid;
1121 msg->any.head.spanid = state->spanid;
1124 state->any.any = data;
1126 assert(((state->txcmd ^ msg->any.head.cmd) &
1127 HAMMER2_MSGF_REPLY) == 0);
1128 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1129 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1130 } else if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
1132 * No existing state and CREATE is set, create new
1133 * state for outgoing command. This can't happen if
1134 * REPLY is set as the state would already exist for
1135 * a transaction reply.
1137 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1139 state = malloc(sizeof(*state));
1140 bzero(state, sizeof(*state));
1141 state->iocom = iocom;
1142 state->flags = HAMMER2_STATE_DYNAMIC;
1144 state->msgid = (uint64_t)(uintptr_t)state;
1145 state->spanid = msg->any.head.spanid;
1146 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1147 state->rxcmd = HAMMER2_MSGF_REPLY;
1149 state->any.any = data;
1150 RB_INSERT(hammer2_state_tree, &iocom->statewr_tree, state);
1151 state->flags |= HAMMER2_STATE_INSERTED;
1153 msg->any.head.msgid = state->msgid;
1154 /* spanid set by caller */
1156 msg->any.head.msgid = 0;
1157 /* spanid set by caller */
1164 * Queue it for output, wake up the I/O pthread. Note that the
1165 * I/O thread is responsible for generating the CRCs and encryption.
1167 TAILQ_INSERT_TAIL(&iocom->txmsgq, msg, qentry);
1169 write(iocom->wakeupfds[1], &dummy, 1); /* XXX optimize me */
1170 pthread_mutex_unlock(&iocom->mtx);
1174 * This is a shortcut to formulate a reply to msg with a simple error code,
1175 * It can reply to and terminate a transaction, or it can reply to a one-way
1176 * messages. A HAMMER2_LNK_ERROR command code is utilized to encode
1177 * the error code (which can be 0). Not all transactions are terminated
1178 * with HAMMER2_LNK_ERROR status (the low level only cares about the
1179 * MSGF_DELETE flag), but most are.
1181 * Replies to one-way messages are a bit of an oxymoron but the feature
1182 * is used by the debug (DBG) protocol.
1184 * The reply contains no extended data.
1187 hammer2_msg_reply(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1189 hammer2_state_t *state = msg->state;
1190 hammer2_msg_t *nmsg;
1195 * Reply with a simple error code and terminate the transaction.
1197 cmd = HAMMER2_LNK_ERROR;
1200 * Check if our direction has even been initiated yet, set CREATE.
1202 * Check what direction this is (command or reply direction). Note
1203 * that txcmd might not have been initiated yet.
1205 * If our direction has already been closed we just return without
1209 if (state->txcmd & HAMMER2_MSGF_DELETE)
1211 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1212 cmd |= HAMMER2_MSGF_CREATE;
1213 if (state->txcmd & HAMMER2_MSGF_REPLY)
1214 cmd |= HAMMER2_MSGF_REPLY;
1215 cmd |= HAMMER2_MSGF_DELETE;
1217 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1218 cmd |= HAMMER2_MSGF_REPLY;
1221 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1222 nmsg->any.head.error = error;
1223 nmsg->state = msg->state;
1224 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
1228 * Similar to hammer2_msg_reply() but leave the transaction open. That is,
1229 * we are generating a streaming reply or an intermediate acknowledgement
1230 * of some sort as part of the higher level protocol, with more to come
1234 hammer2_msg_result(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1236 hammer2_state_t *state = msg->state;
1237 hammer2_msg_t *nmsg;
1242 * Reply with a simple error code and terminate the transaction.
1244 cmd = HAMMER2_LNK_ERROR;
1247 * Check if our direction has even been initiated yet, set CREATE.
1249 * Check what direction this is (command or reply direction). Note
1250 * that txcmd might not have been initiated yet.
1252 * If our direction has already been closed we just return without
1256 if (state->txcmd & HAMMER2_MSGF_DELETE)
1258 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1259 cmd |= HAMMER2_MSGF_CREATE;
1260 if (state->txcmd & HAMMER2_MSGF_REPLY)
1261 cmd |= HAMMER2_MSGF_REPLY;
1262 /* continuing transaction, do not set MSGF_DELETE */
1264 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1265 cmd |= HAMMER2_MSGF_REPLY;
1268 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1269 nmsg->any.head.error = error;
1270 nmsg->state = state;
1271 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
1275 * Terminate a transaction given a state structure by issuing a DELETE.
1278 hammer2_state_reply(hammer2_state_t *state, uint32_t error)
1280 hammer2_msg_t *nmsg;
1281 uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
1284 * Nothing to do if we already transmitted a delete
1286 if (state->txcmd & HAMMER2_MSGF_DELETE)
1290 * We must also set CREATE if this is our first response to a
1293 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1294 cmd |= HAMMER2_MSGF_CREATE;
1297 * Set REPLY if the other end initiated the command. Otherwise
1298 * we are the command direction.
1300 if (state->txcmd & HAMMER2_MSGF_REPLY)
1301 cmd |= HAMMER2_MSGF_REPLY;
1303 nmsg = hammer2_msg_alloc(state->iocom, 0, cmd);
1304 nmsg->any.head.error = error;
1305 nmsg->state = state;
1306 hammer2_msg_write(state->iocom, nmsg, NULL, NULL, NULL);
1309 /************************************************************************
1310 * TRANSACTION STATE HANDLING *
1311 ************************************************************************
1315 RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
1318 * Process state tracking for a message after reception, prior to
1321 * Called with msglk held and the msg dequeued.
1323 * All messages are called with dummy state and return actual state.
1324 * (One-off messages often just return the same dummy state).
1326 * May request that caller discard the message by setting *discardp to 1.
1327 * The returned state is not used in this case and is allowed to be NULL.
1331 * These routines handle persistent and command/reply message state via the
1332 * CREATE and DELETE flags. The first message in a command or reply sequence
1333 * sets CREATE, the last message in a command or reply sequence sets DELETE.
1335 * There can be any number of intermediate messages belonging to the same
1336 * sequence sent inbetween the CREATE message and the DELETE message,
1337 * which set neither flag. This represents a streaming command or reply.
1339 * Any command message received with CREATE set expects a reply sequence to
1340 * be returned. Reply sequences work the same as command sequences except the
1341 * REPLY bit is also sent. Both the command side and reply side can
1342 * degenerate into a single message with both CREATE and DELETE set. Note
1343 * that one side can be streaming and the other side not, or neither, or both.
1345 * The msgid is unique for the initiator. That is, two sides sending a new
1346 * message can use the same msgid without colliding.
1350 * ABORT sequences work by setting the ABORT flag along with normal message
1351 * state. However, ABORTs can also be sent on half-closed messages, that is
1352 * even if the command or reply side has already sent a DELETE, as long as
1353 * the message has not been fully closed it can still send an ABORT+DELETE
1354 * to terminate the half-closed message state.
1356 * Since ABORT+DELETEs can race we silently discard ABORT's for message
1357 * state which has already been fully closed. REPLY+ABORT+DELETEs can
1358 * also race, and in this situation the other side might have already
1359 * initiated a new unrelated command with the same message id. Since
1360 * the abort has not set the CREATE flag the situation can be detected
1361 * and the message will also be discarded.
1363 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
1364 * The ABORT request is essentially integrated into the command instead
1365 * of being sent later on. In this situation the command implementation
1366 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
1367 * special-case non-blocking operation for the command.
1369 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
1370 * to be mid-stream aborts for command/reply sequences. ABORTs on
1371 * one-way messages are not supported.
1373 * NOTE! If a command sequence does not support aborts the ABORT flag is
1378 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
1379 * set. One-off messages cannot be aborted and typically aren't processed
1380 * by these routines. The REPLY bit can be used to distinguish whether a
1381 * one-off message is a command or reply. For example, one-off replies
1382 * will typically just contain status updates.
1385 hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1387 hammer2_state_t *state;
1388 hammer2_state_t dummy;
1392 * Lock RB tree and locate existing persistent state, if any.
1394 * If received msg is a command state is on staterd_tree.
1395 * If received msg is a reply state is on statewr_tree.
1398 dummy.msgid = msg->any.head.msgid;
1399 dummy.spanid = msg->any.head.spanid;
1401 iocom_printf(iocom, msg->any.head.cmd,
1402 "received msg %08x msgid %jx spanid=%jx\n",
1404 (intmax_t)msg->any.head.msgid,
1405 (intmax_t)msg->any.head.spanid);
1407 pthread_mutex_lock(&iocom->mtx);
1408 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1409 state = RB_FIND(hammer2_state_tree,
1410 &iocom->statewr_tree, &dummy);
1412 state = RB_FIND(hammer2_state_tree,
1413 &iocom->staterd_tree, &dummy);
1416 pthread_mutex_unlock(&iocom->mtx);
1419 * Short-cut one-off or mid-stream messages (state may be NULL).
1421 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1422 HAMMER2_MSGF_ABORT)) == 0) {
1427 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1428 * inside the case statements.
1430 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1431 HAMMER2_MSGF_REPLY)) {
1432 case HAMMER2_MSGF_CREATE:
1433 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1435 * New persistant command received.
1438 iocom_printf(iocom, msg->any.head.cmd,
1439 "hammer2_state_msgrx: "
1440 "duplicate transaction\n");
1441 error = HAMMER2_IOQ_ERROR_TRANS;
1444 state = malloc(sizeof(*state));
1445 bzero(state, sizeof(*state));
1446 state->iocom = iocom;
1447 state->flags = HAMMER2_STATE_DYNAMIC;
1449 state->txcmd = HAMMER2_MSGF_REPLY;
1450 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1451 pthread_mutex_lock(&iocom->mtx);
1452 RB_INSERT(hammer2_state_tree, &iocom->staterd_tree, state);
1453 pthread_mutex_unlock(&iocom->mtx);
1454 state->flags |= HAMMER2_STATE_INSERTED;
1455 state->msgid = msg->any.head.msgid;
1456 state->spanid = msg->any.head.spanid;
1460 case HAMMER2_MSGF_DELETE:
1462 * Persistent state is expected but might not exist if an
1463 * ABORT+DELETE races the close.
1465 if (state == NULL) {
1466 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1467 error = HAMMER2_IOQ_ERROR_EALREADY;
1469 iocom_printf(iocom, msg->any.head.cmd,
1470 "hammer2_state_msgrx: "
1471 "no state for DELETE\n");
1472 error = HAMMER2_IOQ_ERROR_TRANS;
1478 * Handle another ABORT+DELETE case if the msgid has already
1481 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1482 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1483 error = HAMMER2_IOQ_ERROR_EALREADY;
1485 iocom_printf(iocom, msg->any.head.cmd,
1486 "hammer2_state_msgrx: "
1487 "state reused for DELETE\n");
1488 error = HAMMER2_IOQ_ERROR_TRANS;
1496 * Check for mid-stream ABORT command received, otherwise
1499 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1500 if (state == NULL ||
1501 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1502 error = HAMMER2_IOQ_ERROR_EALREADY;
1508 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
1509 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1511 * When receiving a reply with CREATE set the original
1512 * persistent state message should already exist.
1514 if (state == NULL) {
1515 iocom_printf(iocom, msg->any.head.cmd,
1516 "hammer2_state_msgrx: "
1517 "no state match for REPLY cmd=%08x\n",
1519 error = HAMMER2_IOQ_ERROR_TRANS;
1522 assert(((state->rxcmd ^ msg->any.head.cmd) &
1523 HAMMER2_MSGF_REPLY) == 0);
1524 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1527 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1529 * Received REPLY+ABORT+DELETE in case where msgid has
1530 * already been fully closed, ignore the message.
1532 if (state == NULL) {
1533 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1534 error = HAMMER2_IOQ_ERROR_EALREADY;
1536 iocom_printf(iocom, msg->any.head.cmd,
1537 "hammer2_state_msgrx: "
1538 "no state match for "
1540 error = HAMMER2_IOQ_ERROR_TRANS;
1546 * Received REPLY+ABORT+DELETE in case where msgid has
1547 * already been reused for an unrelated message,
1548 * ignore the message.
1550 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1551 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1552 error = HAMMER2_IOQ_ERROR_EALREADY;
1554 iocom_printf(iocom, msg->any.head.cmd,
1555 "hammer2_state_msgrx: "
1556 "state reused for REPLY|DELETE\n");
1557 error = HAMMER2_IOQ_ERROR_TRANS;
1563 case HAMMER2_MSGF_REPLY:
1565 * Check for mid-stream ABORT reply received to sent command.
1567 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1568 if (state == NULL ||
1569 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1570 error = HAMMER2_IOQ_ERROR_EALREADY;
1581 hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1583 hammer2_state_t *state;
1585 if ((state = msg->state) == NULL) {
1587 * Free a non-transactional message, there is no state
1590 hammer2_msg_free(iocom, msg);
1591 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1593 * Message terminating transaction, destroy the related
1594 * state, the original message, and this message (if it
1595 * isn't the original message due to a CREATE|DELETE).
1597 pthread_mutex_lock(&iocom->mtx);
1598 state->rxcmd |= HAMMER2_MSGF_DELETE;
1599 if (state->txcmd & HAMMER2_MSGF_DELETE) {
1600 if (state->msg == msg)
1602 assert(state->flags & HAMMER2_STATE_INSERTED);
1603 if (state->rxcmd & HAMMER2_MSGF_REPLY) {
1604 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1605 RB_REMOVE(hammer2_state_tree,
1606 &iocom->statewr_tree, state);
1608 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1609 RB_REMOVE(hammer2_state_tree,
1610 &iocom->staterd_tree, state);
1612 state->flags &= ~HAMMER2_STATE_INSERTED;
1613 hammer2_state_free(state);
1617 pthread_mutex_unlock(&iocom->mtx);
1618 hammer2_msg_free(iocom, msg);
1619 } else if (state->msg != msg) {
1621 * Message not terminating transaction, leave state intact
1622 * and free message if it isn't the CREATE message.
1624 hammer2_msg_free(iocom, msg);
1629 hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1631 hammer2_state_t *state;
1633 if ((state = msg->state) == NULL) {
1634 hammer2_msg_free(iocom, msg);
1635 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1636 pthread_mutex_lock(&iocom->mtx);
1637 state->txcmd |= HAMMER2_MSGF_DELETE;
1638 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
1639 if (state->msg == msg)
1641 assert(state->flags & HAMMER2_STATE_INSERTED);
1642 if (state->txcmd & HAMMER2_MSGF_REPLY) {
1643 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1644 RB_REMOVE(hammer2_state_tree,
1645 &iocom->staterd_tree, state);
1647 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1648 RB_REMOVE(hammer2_state_tree,
1649 &iocom->statewr_tree, state);
1651 state->flags &= ~HAMMER2_STATE_INSERTED;
1652 hammer2_state_free(state);
1656 pthread_mutex_unlock(&iocom->mtx);
1657 hammer2_msg_free(iocom, msg);
1658 } else if (state->msg != msg) {
1659 hammer2_msg_free(iocom, msg);
1664 * Called with iocom locked
1667 hammer2_state_free(hammer2_state_t *state)
1669 hammer2_iocom_t *iocom = state->iocom;
1673 fprintf(stderr, "STATE FREE %p\n", state);
1675 assert(state->any.any == NULL);
1679 hammer2_msg_free_locked(iocom, msg);
1683 * When an iocom error is present we are trying to close down the
1684 * iocom, but we have to wait for all states to terminate before
1685 * we can do so. The iocom rx code will terminate the receive side
1686 * for all transactions by simulating incoming DELETE messages,
1687 * but the state doesn't go away until both sides are terminated.
1689 * We may have to wake up the rx code.
1691 if (iocom->ioq_rx.error &&
1692 RB_EMPTY(&iocom->staterd_tree) &&
1693 RB_EMPTY(&iocom->statewr_tree)) {
1695 write(iocom->wakeupfds[1], &dummy, 1);
1700 * Indexed messages are stored in a red-black tree indexed by their
1701 * msgid. Only persistent messages are indexed.
1704 hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
1706 if (state1->spanid < state2->spanid)
1708 if (state1->spanid > state2->spanid)
1710 if (state1->msgid < state2->msgid)
1712 if (state1->msgid > state2->msgid)