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 * Indexed messages are stored in a red-black tree indexed by their
43 * msgid. Only persistent messages are indexed.
46 hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
48 if (state1->spanid < state2->spanid)
50 if (state1->spanid > state2->spanid)
52 if (state1->msgid < state2->msgid)
54 if (state1->msgid > state2->msgid)
59 RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
62 * Initialize a low-level ioq
65 hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
67 bzero(ioq, sizeof(*ioq));
68 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
69 TAILQ_INIT(&ioq->msgq);
75 * caller holds iocom->mtx.
78 hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
82 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
83 assert(0); /* shouldn't happen */
84 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
85 hammer2_msg_free(iocom, msg);
87 if ((msg = ioq->msg) != NULL) {
89 hammer2_msg_free(iocom, msg);
94 * Initialize a low-level communications channel.
96 * NOTE: The state_func() is called at least once from the loop and can be
97 * re-armed via hammer2_iocom_restate().
100 hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
101 void (*state_func)(hammer2_iocom_t *),
102 void (*rcvmsg_func)(hammer2_iocom_t *, hammer2_msg_t *msg),
103 void (*altmsg_func)(hammer2_iocom_t *))
105 bzero(iocom, sizeof(*iocom));
107 iocom->state_callback = state_func;
108 iocom->rcvmsg_callback = rcvmsg_func;
109 iocom->altmsg_callback = altmsg_func;
111 pthread_mutex_init(&iocom->mtx, NULL);
112 RB_INIT(&iocom->staterd_tree);
113 RB_INIT(&iocom->statewr_tree);
114 TAILQ_INIT(&iocom->freeq);
115 TAILQ_INIT(&iocom->freeq_aux);
116 TAILQ_INIT(&iocom->addrq);
117 TAILQ_INIT(&iocom->txmsgq);
118 iocom->sock_fd = sock_fd;
119 iocom->alt_fd = alt_fd;
120 iocom->flags = HAMMER2_IOCOMF_RREQ;
122 iocom->flags |= HAMMER2_IOCOMF_SWORK;
123 hammer2_ioq_init(iocom, &iocom->ioq_rx);
124 hammer2_ioq_init(iocom, &iocom->ioq_tx);
125 if (pipe(iocom->wakeupfds) < 0)
127 fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
128 fcntl(iocom->wakeupfds[1], F_SETFL, O_NONBLOCK);
131 * Negotiate session crypto synchronously. This will mark the
132 * connection as error'd if it fails.
134 hammer2_crypto_negotiate(iocom);
137 * Make sure our fds are set to non-blocking for the iocom core.
140 fcntl(sock_fd, F_SETFL, O_NONBLOCK);
142 /* if line buffered our single fgets() should be fine */
144 fcntl(alt_fd, F_SETFL, O_NONBLOCK);
149 * May only be called from a callback from iocom_core.
151 * Adjust state machine functions, set flags to guarantee that both
152 * the recevmsg_func and the sendmsg_func is called at least once.
155 hammer2_iocom_restate(hammer2_iocom_t *iocom,
156 void (*state_func)(hammer2_iocom_t *),
157 void (*rcvmsg_func)(hammer2_iocom_t *, hammer2_msg_t *msg),
158 void (*altmsg_func)(hammer2_iocom_t *))
160 iocom->state_callback = state_func;
161 iocom->rcvmsg_callback = rcvmsg_func;
162 iocom->altmsg_callback = altmsg_func;
164 iocom->flags |= HAMMER2_IOCOMF_SWORK;
166 iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
170 * Cleanup a terminating iocom.
172 * Caller should not hold iocom->mtx. The iocom has already been disconnected
173 * from all possible references to it.
176 hammer2_iocom_done(hammer2_iocom_t *iocom)
180 if (iocom->sock_fd >= 0) {
181 close(iocom->sock_fd);
184 if (iocom->alt_fd >= 0) {
185 close(iocom->alt_fd);
188 hammer2_ioq_done(iocom, &iocom->ioq_rx);
189 hammer2_ioq_done(iocom, &iocom->ioq_tx);
190 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
191 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
194 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
195 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
197 msg->aux_data = NULL;
200 if (iocom->wakeupfds[0] >= 0) {
201 close(iocom->wakeupfds[0]);
202 iocom->wakeupfds[0] = -1;
204 if (iocom->wakeupfds[1] >= 0) {
205 close(iocom->wakeupfds[1]);
206 iocom->wakeupfds[1] = -1;
208 pthread_mutex_destroy(&iocom->mtx);
212 * Allocate a new one-way message.
215 hammer2_msg_alloc(hammer2_iocom_t *iocom, size_t aux_size, uint32_t cmd)
220 pthread_mutex_lock(&iocom->mtx);
222 aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
223 ~HAMMER2_MSG_ALIGNMASK;
224 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
225 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
227 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
228 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
230 pthread_mutex_unlock(&iocom->mtx);
232 msg = malloc(sizeof(*msg));
233 bzero(msg, sizeof(*msg));
234 msg->aux_data = NULL;
237 if (msg->aux_size != aux_size) {
240 msg->aux_data = NULL;
244 msg->aux_data = malloc(aux_size);
245 msg->aux_size = aux_size;
248 hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
250 bzero(&msg->any.head, hbytes);
251 msg->hdr_size = hbytes;
252 msg->any.head.cmd = cmd;
253 msg->any.head.aux_descr = 0;
254 msg->any.head.aux_crc = 0;
260 * Free a message so it can be reused afresh.
262 * NOTE: aux_size can be 0 with a non-NULL aux_data.
266 hammer2_msg_free_locked(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
270 TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
272 TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
276 hammer2_msg_free(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
278 pthread_mutex_lock(&iocom->mtx);
279 hammer2_msg_free_locked(iocom, msg);
280 pthread_mutex_unlock(&iocom->mtx);
284 * I/O core loop for an iocom.
286 * Thread localized, iocom->mtx not held.
289 hammer2_iocom_core(hammer2_iocom_t *iocom)
291 struct pollfd fds[3];
296 int wi; /* wakeup pipe */
298 int ai; /* alt bulk path socket */
300 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
301 if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
302 HAMMER2_IOCOMF_WWORK |
303 HAMMER2_IOCOMF_PWORK |
304 HAMMER2_IOCOMF_SWORK |
305 HAMMER2_IOCOMF_ARWORK |
306 HAMMER2_IOCOMF_AWWORK)) == 0) {
308 * Only poll if no immediate work is pending.
309 * Otherwise we are just wasting our time calling
320 * Always check the inter-thread pipe, e.g.
321 * for iocom->txmsgq work.
324 fds[wi].fd = iocom->wakeupfds[0];
325 fds[wi].events = POLLIN;
329 * Check the socket input/output direction as
332 if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
333 HAMMER2_IOCOMF_WREQ)) {
335 fds[si].fd = iocom->sock_fd;
339 if (iocom->flags & HAMMER2_IOCOMF_RREQ)
340 fds[si].events |= POLLIN;
341 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
342 fds[si].events |= POLLOUT;
346 * Check the alternative fd for work.
348 if (iocom->alt_fd >= 0) {
350 fds[ai].fd = iocom->alt_fd;
351 fds[ai].events = POLLIN;
354 poll(fds, count, timeout);
356 if (wi >= 0 && (fds[wi].revents & POLLIN))
357 iocom->flags |= HAMMER2_IOCOMF_PWORK;
358 if (si >= 0 && (fds[si].revents & POLLIN))
359 iocom->flags |= HAMMER2_IOCOMF_RWORK;
360 if (si >= 0 && (fds[si].revents & POLLOUT))
361 iocom->flags |= HAMMER2_IOCOMF_WWORK;
362 if (wi >= 0 && (fds[wi].revents & POLLOUT))
363 iocom->flags |= HAMMER2_IOCOMF_WWORK;
364 if (ai >= 0 && (fds[ai].revents & POLLIN))
365 iocom->flags |= HAMMER2_IOCOMF_ARWORK;
368 * Always check the pipe
370 iocom->flags |= HAMMER2_IOCOMF_PWORK;
373 if (iocom->flags & HAMMER2_IOCOMF_SWORK) {
374 iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
375 iocom->state_callback(iocom);
379 * Pending message queues from other threads wake us up
380 * with a write to the wakeupfds[] pipe. We have to clear
381 * the pipe with a dummy read.
383 if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
384 iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
385 read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
386 iocom->flags |= HAMMER2_IOCOMF_RWORK;
387 iocom->flags |= HAMMER2_IOCOMF_WWORK;
388 if (TAILQ_FIRST(&iocom->txmsgq))
389 hammer2_iocom_flush1(iocom);
393 * Message write sequencing
395 if (iocom->flags & HAMMER2_IOCOMF_WWORK)
396 hammer2_iocom_flush1(iocom);
399 * Message read sequencing. Run this after the write
400 * sequencing in case the write sequencing allowed another
401 * auto-DELETE to occur on the read side.
403 if (iocom->flags & HAMMER2_IOCOMF_RWORK) {
404 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
405 (msg = hammer2_ioq_read(iocom)) != NULL) {
407 fprintf(stderr, "receive %s\n",
408 hammer2_msg_str(msg));
410 iocom->rcvmsg_callback(iocom, msg);
411 hammer2_state_cleanuprx(iocom, msg);
415 if (iocom->flags & HAMMER2_IOCOMF_ARWORK) {
416 iocom->flags &= ~HAMMER2_IOCOMF_ARWORK;
417 iocom->altmsg_callback(iocom);
423 * Read the next ready message from the ioq, issuing I/O if needed.
424 * Caller should retry on a read-event when NULL is returned.
426 * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
427 * be returned for each open transaction, then the ioq and iocom
428 * will be errored out and a non-transactional HAMMER2_LNK_ERROR
429 * msg will be returned as the final message. The caller should not call
430 * us again after the final message is returned.
432 * Thread localized, iocom->mtx not held.
435 hammer2_ioq_read(hammer2_iocom_t *iocom)
437 hammer2_ioq_t *ioq = &iocom->ioq_rx;
439 hammer2_msg_hdr_t *head;
440 hammer2_state_t *state;
448 iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
451 * If a message is already pending we can just remove and
452 * return it. Message state has already been processed.
454 if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
455 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
463 * Message read in-progress (msg is NULL at the moment). We don't
464 * allocate a msg until we have its core header.
466 bytes = ioq->fifo_end - ioq->fifo_beg;
467 nmax = sizeof(ioq->buf) - ioq->fifo_end;
471 case HAMMER2_MSGQ_STATE_HEADER1:
473 * Load the primary header, fail on any non-trivial read
474 * error or on EOF. Since the primary header is the same
475 * size is the message alignment it will never straddle
476 * the end of the buffer.
478 if (bytes < (int)sizeof(msg->any.head)) {
479 n = read(iocom->sock_fd,
480 ioq->buf + ioq->fifo_end,
484 ioq->error = HAMMER2_IOQ_ERROR_EOF;
487 if (errno != EINTR &&
488 errno != EINPROGRESS &&
490 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
502 * Insufficient data accumulated (msg is NULL, caller will
506 if (bytes < (int)sizeof(msg->any.head))
510 * Calculate the header, decrypt data received so far.
511 * Data will be decrypted in-place. Partial blocks are
512 * not immediately decrypted.
514 * WARNING! The header might be in the wrong endian, we
515 * do not fix it up until we get the entire
518 hammer2_crypto_decrypt(iocom, ioq);
519 head = (void *)(ioq->buf + ioq->fifo_beg);
522 * Check and fixup the core header. Note that the icrc
523 * has to be calculated before any fixups, but the crc
524 * fields in the msg may have to be swapped like everything
527 if (head->magic != HAMMER2_MSGHDR_MAGIC &&
528 head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
529 ioq->error = HAMMER2_IOQ_ERROR_SYNC;
534 * Calculate the full header size and aux data size
536 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
537 ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
539 ioq->abytes = bswap32(head->aux_bytes) *
542 ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
544 ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
546 if (ioq->hbytes < sizeof(msg->any.head) ||
547 ioq->hbytes > sizeof(msg->any) ||
548 ioq->abytes > HAMMER2_MSGAUX_MAX) {
549 ioq->error = HAMMER2_IOQ_ERROR_FIELD;
554 * Allocate the message, the next state will fill it in.
556 msg = hammer2_msg_alloc(iocom, ioq->abytes, 0);
560 * Fall through to the next state. Make sure that the
561 * extended header does not straddle the end of the buffer.
562 * We still want to issue larger reads into our buffer,
563 * book-keeping is easier if we don't bcopy() yet.
565 if (bytes + nmax < ioq->hbytes) {
566 bcopy(ioq->buf + ioq->fifo_beg, ioq->buf, bytes);
567 ioq->fifo_cdx -= ioq->fifo_beg;
569 ioq->fifo_end = bytes;
570 nmax = sizeof(ioq->buf) - ioq->fifo_end;
572 ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
574 case HAMMER2_MSGQ_STATE_HEADER2:
576 * Fill out the extended header.
579 if (bytes < ioq->hbytes) {
580 n = read(iocom->sock_fd,
581 ioq->buf + ioq->fifo_end,
585 ioq->error = HAMMER2_IOQ_ERROR_EOF;
588 if (errno != EINTR &&
589 errno != EINPROGRESS &&
591 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
603 * Insufficient data accumulated (set msg NULL so caller will
606 if (bytes < ioq->hbytes) {
612 * Calculate the extended header, decrypt data received
613 * so far. Handle endian-conversion for the entire extended
616 hammer2_crypto_decrypt(iocom, ioq);
617 head = (void *)(ioq->buf + ioq->fifo_beg);
622 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
623 xcrc32 = bswap32(head->hdr_crc);
625 xcrc32 = head->hdr_crc;
627 if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
628 ioq->error = HAMMER2_IOQ_ERROR_XCRC;
629 fprintf(stderr, "BAD-XCRC(%08x,%08x) %s\n",
630 xcrc32, hammer2_icrc32(head, ioq->hbytes),
631 hammer2_msg_str(msg));
635 head->hdr_crc = xcrc32;
637 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
638 hammer2_bswap_head(head);
642 * Copy the extended header into the msg and adjust the
645 bcopy(head, &msg->any, ioq->hbytes);
648 * We are either done or we fall-through.
650 if (ioq->abytes == 0) {
651 ioq->fifo_beg += ioq->hbytes;
656 * Must adjust nmax and bytes (and the state) when falling
659 ioq->fifo_beg += ioq->hbytes;
661 bytes -= ioq->hbytes;
662 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
664 case HAMMER2_MSGQ_STATE_AUXDATA1:
666 * Copy the partial or complete payload from remaining
667 * bytes in the FIFO. We have to fall-through either
668 * way so we can check the crc.
670 * Adjust msg->aux_size to the final actual value.
672 ioq->already = ioq->fifo_cdx - ioq->fifo_beg;
673 if (ioq->already > ioq->abytes)
674 ioq->already = ioq->abytes;
675 if (bytes >= ioq->abytes) {
676 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
678 msg->aux_size = ioq->abytes;
679 ioq->fifo_beg += ioq->abytes;
680 if (ioq->fifo_cdx < ioq->fifo_beg)
681 ioq->fifo_cdx = ioq->fifo_beg;
682 bytes -= ioq->abytes;
684 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
686 msg->aux_size = bytes;
687 ioq->fifo_beg += bytes;
688 if (ioq->fifo_cdx < ioq->fifo_beg)
689 ioq->fifo_cdx = ioq->fifo_beg;
694 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
696 case HAMMER2_MSGQ_STATE_AUXDATA2:
698 * Read the remainder of the payload directly into the
699 * msg->aux_data buffer.
702 if (msg->aux_size < ioq->abytes) {
704 n = read(iocom->sock_fd,
705 msg->aux_data + msg->aux_size,
706 ioq->abytes - msg->aux_size);
709 ioq->error = HAMMER2_IOQ_ERROR_EOF;
712 if (errno != EINTR &&
713 errno != EINPROGRESS &&
715 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
725 * Insufficient data accumulated (set msg NULL so caller will
728 if (msg->aux_size < ioq->abytes) {
732 assert(msg->aux_size == ioq->abytes);
733 hammer2_crypto_decrypt_aux(iocom, ioq, msg, ioq->already);
736 * Check aux_crc, then we are done.
738 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
739 if (xcrc32 != msg->any.head.aux_crc) {
740 ioq->error = HAMMER2_IOQ_ERROR_ACRC;
744 case HAMMER2_MSGQ_STATE_ERROR:
746 * Continued calls to drain recorded transactions (returning
747 * a LNK_ERROR for each one), before we return the final
754 * We don't double-return errors, the caller should not
755 * have called us again after getting an error msg.
762 * Check the message sequence. The iv[] should prevent any
763 * possibility of a replay but we add this check anyway.
765 if (msg && ioq->error == 0) {
766 if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
767 ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
774 * Process transactional state for the message.
776 if (msg && ioq->error == 0) {
777 error = hammer2_state_msgrx(iocom, msg);
779 if (error == HAMMER2_IOQ_ERROR_EALREADY) {
780 hammer2_msg_free(iocom, msg);
788 * Handle error, RREQ, or completion
790 * NOTE: nmax and bytes are invalid at this point, we don't bother
791 * to update them when breaking out.
796 * An unrecoverable error causes all active receive
797 * transactions to be terminated with a LNK_ERROR message.
799 * Once all active transactions are exhausted we set the
800 * iocom ERROR flag and return a non-transactional LNK_ERROR
801 * message, which should cause master processing loops to
804 assert(ioq->msg == msg);
806 hammer2_msg_free(iocom, msg);
811 * No more I/O read processing
813 ioq->state = HAMMER2_MSGQ_STATE_ERROR;
816 * Simulate a remote LNK_ERROR DELETE msg for any open
817 * transactions, ending with a final non-transactional
818 * LNK_ERROR (that the session can detect) when no
819 * transactions remain.
821 msg = hammer2_msg_alloc(iocom, 0, 0);
822 bzero(&msg->any.head, sizeof(msg->any.head));
823 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
824 msg->any.head.cmd = HAMMER2_LNK_ERROR;
825 msg->any.head.error = ioq->error;
827 pthread_mutex_lock(&iocom->mtx);
828 hammer2_iocom_drain(iocom);
829 if ((state = RB_ROOT(&iocom->staterd_tree)) != NULL) {
831 * Active remote transactions are still present.
832 * Simulate the other end sending us a DELETE.
834 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
835 hammer2_msg_free(iocom, msg);
838 /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
840 msg->any.head.spanid = state->spanid;
841 msg->any.head.msgid = state->msgid;
842 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
845 } else if ((state = RB_ROOT(&iocom->statewr_tree)) != NULL) {
847 * Active local transactions are still present.
848 * Simulate the other end sending us a DELETE.
850 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
851 hammer2_msg_free(iocom, msg);
855 msg->any.head.spanid = state->spanid;
856 msg->any.head.msgid = state->msgid;
857 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
858 HAMMER2_MSGF_DELETE |
860 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
867 * No active local or remote transactions remain.
868 * Generate a final LNK_ERROR and flag EOF.
871 iocom->flags |= HAMMER2_IOCOMF_EOF;
872 fprintf(stderr, "EOF ON SOCKET %d\n", iocom->sock_fd);
874 pthread_mutex_unlock(&iocom->mtx);
877 * For the iocom error case we want to set RWORK to indicate
878 * that more messages might be pending.
880 * It is possible to return NULL when there is more work to
881 * do because each message has to be DELETEd in both
882 * directions before we continue on with the next (though
883 * this could be optimized). The transmit direction will
887 iocom->flags |= HAMMER2_IOCOMF_RWORK;
888 } else if (msg == NULL) {
890 * Insufficient data received to finish building the message,
891 * set RREQ and return NULL.
893 * Leave ioq->msg intact.
894 * Leave the FIFO intact.
896 iocom->flags |= HAMMER2_IOCOMF_RREQ;
901 * The fifo has already been advanced past the message.
902 * Trivially reset the FIFO indices if possible.
904 * clear the FIFO if it is now empty and set RREQ to wait
905 * for more from the socket. If the FIFO is not empty set
906 * TWORK to bypass the poll so we loop immediately.
908 if (ioq->fifo_beg == ioq->fifo_end) {
909 iocom->flags |= HAMMER2_IOCOMF_RREQ;
914 iocom->flags |= HAMMER2_IOCOMF_RWORK;
916 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
923 * Calculate the header and data crc's and write a low-level message to
924 * the connection. If aux_crc is non-zero the aux_data crc is already
925 * assumed to have been set.
927 * A non-NULL msg is added to the queue but not necessarily flushed.
928 * Calling this function with msg == NULL will get a flush going.
930 * Caller must hold iocom->mtx.
933 hammer2_iocom_flush1(hammer2_iocom_t *iocom)
935 hammer2_ioq_t *ioq = &iocom->ioq_tx;
939 hammer2_msg_queue_t tmpq;
941 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
943 pthread_mutex_lock(&iocom->mtx);
944 while ((msg = TAILQ_FIRST(&iocom->txmsgq)) != NULL) {
945 TAILQ_REMOVE(&iocom->txmsgq, msg, qentry);
946 TAILQ_INSERT_TAIL(&tmpq, msg, qentry);
948 pthread_mutex_unlock(&iocom->mtx);
950 while ((msg = TAILQ_FIRST(&tmpq)) != NULL) {
952 * Process terminal connection errors.
954 TAILQ_REMOVE(&tmpq, msg, qentry);
956 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
962 * Finish populating the msg fields. The salt ensures that
963 * the iv[] array is ridiculously randomized and we also
964 * re-seed our PRNG every 32768 messages just to be sure.
966 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
967 msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
969 if ((ioq->seq & 32767) == 0)
973 * Calculate aux_crc if 0, then calculate hdr_crc.
975 if (msg->aux_size && msg->any.head.aux_crc == 0) {
976 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
977 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
978 msg->any.head.aux_crc = xcrc32;
980 msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
981 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
983 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
985 msg->any.head.hdr_crc = 0;
986 msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
989 * Enqueue the message (the flush codes handles stream
992 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
995 hammer2_iocom_flush2(iocom);
999 * Thread localized, iocom->mtx not held by caller.
1002 hammer2_iocom_flush2(hammer2_iocom_t *iocom)
1004 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1009 struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
1017 hammer2_iocom_drain(iocom);
1022 * Pump messages out the connection by building an iovec.
1024 * ioq->hbytes/ioq->abytes tracks how much of the first message
1025 * in the queue has been successfully written out, so we can
1033 TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
1034 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1036 abytes = msg->aux_size;
1037 assert(hoff <= hbytes && aoff <= abytes);
1039 if (hoff < hbytes) {
1040 iov[n].iov_base = (char *)&msg->any.head + hoff;
1041 iov[n].iov_len = hbytes - hoff;
1042 nmax += hbytes - hoff;
1044 if (n == HAMMER2_IOQ_MAXIOVEC)
1047 if (aoff < abytes) {
1048 assert(msg->aux_data != NULL);
1049 iov[n].iov_base = (char *)msg->aux_data + aoff;
1050 iov[n].iov_len = abytes - aoff;
1051 nmax += abytes - aoff;
1053 if (n == HAMMER2_IOQ_MAXIOVEC)
1063 * Encrypt and write the data. The crypto code will move the
1064 * data into the fifo and adjust the iov as necessary. If
1065 * encryption is disabled the iov is left alone.
1067 * May return a smaller iov (thus a smaller n), with aggregated
1068 * chunks. May reduce nmax to what fits in the FIFO.
1071 n = hammer2_crypto_encrypt(iocom, ioq, iov, n, &nmax);
1074 * Execute the writev() then figure out what happened.
1076 nact = writev(iocom->sock_fd, iov, n);
1078 if (errno != EINTR &&
1079 errno != EINPROGRESS &&
1084 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
1085 hammer2_iocom_drain(iocom);
1088 * Wait for socket buffer space
1090 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1096 * Indicate bytes written successfully.
1098 * If we were unable to write the entire iov array then set WREQ
1099 * to wait for more socket buffer space.
1101 * If the FIFO space was insufficient to fully drain all messages
1102 * set WWORK to cause the core to call us again for the next batch.
1104 hammer2_crypto_encrypt_wrote(iocom, ioq, nact);
1106 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1109 * Clean out the transmit queue based on what we successfully
1110 * sent. ioq->hbytes/abytes represents the portion of the first
1111 * message previously sent.
1113 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1114 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1116 abytes = msg->aux_size;
1118 if ((size_t)nact < hbytes - ioq->hbytes) {
1119 ioq->hbytes += nact;
1123 nact -= hbytes - ioq->hbytes;
1124 ioq->hbytes = hbytes;
1125 if ((size_t)nact < abytes - ioq->abytes) {
1126 ioq->abytes += nact;
1130 nact -= abytes - ioq->abytes;
1132 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1137 hammer2_state_cleanuptx(iocom, msg);
1141 * If more messages are pending on WREQ wasn't set we must
1142 * ensure that WWORK gets set.
1144 if (msg && (iocom->flags & HAMMER2_IOCOMF_WREQ) == 0)
1145 iocom->flags |= HAMMER2_IOCOMF_WWORK;
1148 hammer2_iocom_drain(iocom);
1153 * Kill pending msgs on ioq_tx and adjust the flags such that no more
1154 * write events will occur. We don't kill read msgs because we want
1155 * the caller to pull off our contrived terminal error msg to detect
1156 * the connection failure.
1158 * Thread localized, iocom->mtx not held by caller.
1161 hammer2_iocom_drain(hammer2_iocom_t *iocom)
1163 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1166 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
1170 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1171 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1173 hammer2_state_cleanuptx(iocom, msg);
1178 * Write a message to an iocom, with additional state processing.
1181 hammer2_msg_write(hammer2_iocom_t *iocom, hammer2_msg_t *msg,
1182 void (*func)(hammer2_state_t *, hammer2_msg_t *),
1184 hammer2_state_t **statep)
1186 hammer2_state_t *state;
1190 * Handle state processing, create state if necessary.
1192 pthread_mutex_lock(&iocom->mtx);
1193 if ((state = msg->state) != NULL) {
1195 * Existing transaction (could be reply). It is also
1196 * possible for this to be the first reply (CREATE is set),
1197 * in which case we populate state->txcmd.
1199 msg->any.head.msgid = state->msgid;
1200 msg->any.head.spanid = state->spanid;
1203 state->any.any = data;
1205 assert(((state->txcmd ^ msg->any.head.cmd) &
1206 HAMMER2_MSGF_REPLY) == 0);
1207 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1208 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1209 } else if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
1211 * No existing state and CREATE is set, create new
1212 * state for outgoing command. This can't happen if
1213 * REPLY is set as the state would already exist for
1214 * a transaction reply.
1216 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1218 state = malloc(sizeof(*state));
1219 bzero(state, sizeof(*state));
1220 state->iocom = iocom;
1221 state->flags = HAMMER2_STATE_DYNAMIC;
1223 state->msgid = (uint64_t)(uintptr_t)state;
1224 state->spanid = msg->any.head.spanid;
1225 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1226 state->rxcmd = HAMMER2_MSGF_REPLY;
1228 state->any.any = data;
1229 pthread_mutex_lock(&iocom->mtx);
1230 RB_INSERT(hammer2_state_tree, &iocom->statewr_tree, state);
1231 pthread_mutex_unlock(&iocom->mtx);
1232 state->flags |= HAMMER2_STATE_INSERTED;
1234 msg->any.head.msgid = state->msgid;
1235 /* spanid set by caller */
1237 msg->any.head.msgid = 0;
1238 /* spanid set by caller */
1245 * Queue it for output, wake up the I/O pthread. Note that the
1246 * I/O thread is responsible for generating the CRCs and encryption.
1248 TAILQ_INSERT_TAIL(&iocom->txmsgq, msg, qentry);
1250 write(iocom->wakeupfds[1], &dummy, 1); /* XXX optimize me */
1251 pthread_mutex_unlock(&iocom->mtx);
1255 * This is a shortcut to formulate a reply to msg with a simple error code,
1256 * It can reply to and terminate a transaction, or it can reply to a one-way
1257 * messages. A HAMMER2_LNK_ERROR command code is utilized to encode
1258 * the error code (which can be 0). Not all transactions are terminated
1259 * with HAMMER2_LNK_ERROR status (the low level only cares about the
1260 * MSGF_DELETE flag), but most are.
1262 * Replies to one-way messages are a bit of an oxymoron but the feature
1263 * is used by the debug (DBG) protocol.
1265 * The reply contains no extended data.
1268 hammer2_msg_reply(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1270 hammer2_state_t *state = msg->state;
1271 hammer2_msg_t *nmsg;
1276 * Reply with a simple error code and terminate the transaction.
1278 cmd = HAMMER2_LNK_ERROR;
1281 * Check if our direction has even been initiated yet, set CREATE.
1283 * Check what direction this is (command or reply direction). Note
1284 * that txcmd might not have been initiated yet.
1286 * If our direction has already been closed we just return without
1290 if (state->txcmd & HAMMER2_MSGF_DELETE)
1292 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1293 cmd |= HAMMER2_MSGF_CREATE;
1294 if (state->txcmd & HAMMER2_MSGF_REPLY)
1295 cmd |= HAMMER2_MSGF_REPLY;
1296 cmd |= HAMMER2_MSGF_DELETE;
1298 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1299 cmd |= HAMMER2_MSGF_REPLY;
1302 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1303 nmsg->any.head.error = error;
1304 nmsg->state = msg->state;
1305 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
1309 * Similar to hammer2_msg_reply() but leave the transaction open. That is,
1310 * we are generating a streaming reply or an intermediate acknowledgement
1311 * of some sort as part of the higher level protocol, with more to come
1315 hammer2_msg_result(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1317 hammer2_state_t *state = msg->state;
1318 hammer2_msg_t *nmsg;
1323 * Reply with a simple error code and terminate the transaction.
1325 cmd = HAMMER2_LNK_ERROR;
1328 * Check if our direction has even been initiated yet, set CREATE.
1330 * Check what direction this is (command or reply direction). Note
1331 * that txcmd might not have been initiated yet.
1333 * If our direction has already been closed we just return without
1337 if (state->txcmd & HAMMER2_MSGF_DELETE)
1339 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1340 cmd |= HAMMER2_MSGF_CREATE;
1341 if (state->txcmd & HAMMER2_MSGF_REPLY)
1342 cmd |= HAMMER2_MSGF_REPLY;
1343 /* continuing transaction, do not set MSGF_DELETE */
1345 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1346 cmd |= HAMMER2_MSGF_REPLY;
1349 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1350 nmsg->any.head.error = error;
1351 nmsg->state = state;
1352 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
1356 * Terminate a transaction given a state structure by issuing a DELETE.
1359 hammer2_state_reply(hammer2_state_t *state, uint32_t error)
1361 hammer2_msg_t *nmsg;
1362 uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
1365 * Nothing to do if we already transmitted a delete
1367 if (state->txcmd & HAMMER2_MSGF_DELETE)
1371 * We must also set CREATE if this is our first response to a
1374 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1375 cmd |= HAMMER2_MSGF_CREATE;
1378 * Set REPLY if the other end initiated the command. Otherwise
1379 * we are the command direction.
1381 if (state->txcmd & HAMMER2_MSGF_REPLY)
1382 cmd |= HAMMER2_MSGF_REPLY;
1384 nmsg = hammer2_msg_alloc(state->iocom, 0, cmd);
1385 nmsg->any.head.error = error;
1386 nmsg->state = state;
1387 hammer2_msg_write(state->iocom, nmsg, NULL, NULL, NULL);
1390 /************************************************************************
1391 * TRANSACTION STATE HANDLING *
1392 ************************************************************************
1397 * Process state tracking for a message after reception, prior to
1400 * Called with msglk held and the msg dequeued.
1402 * All messages are called with dummy state and return actual state.
1403 * (One-off messages often just return the same dummy state).
1405 * May request that caller discard the message by setting *discardp to 1.
1406 * The returned state is not used in this case and is allowed to be NULL.
1410 * These routines handle persistent and command/reply message state via the
1411 * CREATE and DELETE flags. The first message in a command or reply sequence
1412 * sets CREATE, the last message in a command or reply sequence sets DELETE.
1414 * There can be any number of intermediate messages belonging to the same
1415 * sequence sent inbetween the CREATE message and the DELETE message,
1416 * which set neither flag. This represents a streaming command or reply.
1418 * Any command message received with CREATE set expects a reply sequence to
1419 * be returned. Reply sequences work the same as command sequences except the
1420 * REPLY bit is also sent. Both the command side and reply side can
1421 * degenerate into a single message with both CREATE and DELETE set. Note
1422 * that one side can be streaming and the other side not, or neither, or both.
1424 * The msgid is unique for the initiator. That is, two sides sending a new
1425 * message can use the same msgid without colliding.
1429 * ABORT sequences work by setting the ABORT flag along with normal message
1430 * state. However, ABORTs can also be sent on half-closed messages, that is
1431 * even if the command or reply side has already sent a DELETE, as long as
1432 * the message has not been fully closed it can still send an ABORT+DELETE
1433 * to terminate the half-closed message state.
1435 * Since ABORT+DELETEs can race we silently discard ABORT's for message
1436 * state which has already been fully closed. REPLY+ABORT+DELETEs can
1437 * also race, and in this situation the other side might have already
1438 * initiated a new unrelated command with the same message id. Since
1439 * the abort has not set the CREATE flag the situation can be detected
1440 * and the message will also be discarded.
1442 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
1443 * The ABORT request is essentially integrated into the command instead
1444 * of being sent later on. In this situation the command implementation
1445 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
1446 * special-case non-blocking operation for the command.
1448 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
1449 * to be mid-stream aborts for command/reply sequences. ABORTs on
1450 * one-way messages are not supported.
1452 * NOTE! If a command sequence does not support aborts the ABORT flag is
1457 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
1458 * set. One-off messages cannot be aborted and typically aren't processed
1459 * by these routines. The REPLY bit can be used to distinguish whether a
1460 * one-off message is a command or reply. For example, one-off replies
1461 * will typically just contain status updates.
1464 hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1466 hammer2_state_t *state;
1467 hammer2_state_t dummy;
1471 * Lock RB tree and locate existing persistent state, if any.
1473 * If received msg is a command state is on staterd_tree.
1474 * If received msg is a reply state is on statewr_tree.
1477 dummy.msgid = msg->any.head.msgid;
1478 dummy.spanid = msg->any.head.spanid;
1479 pthread_mutex_lock(&iocom->mtx);
1480 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1481 state = RB_FIND(hammer2_state_tree,
1482 &iocom->statewr_tree, &dummy);
1484 state = RB_FIND(hammer2_state_tree,
1485 &iocom->staterd_tree, &dummy);
1488 pthread_mutex_unlock(&iocom->mtx);
1491 * Short-cut one-off or mid-stream messages (state may be NULL).
1493 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1494 HAMMER2_MSGF_ABORT)) == 0) {
1499 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1500 * inside the case statements.
1502 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1503 HAMMER2_MSGF_REPLY)) {
1504 case HAMMER2_MSGF_CREATE:
1505 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1507 * New persistant command received.
1510 fprintf(stderr, "duplicate-trans %s\n",
1511 hammer2_msg_str(msg));
1512 error = HAMMER2_IOQ_ERROR_TRANS;
1516 state = malloc(sizeof(*state));
1517 bzero(state, sizeof(*state));
1518 state->iocom = iocom;
1519 state->flags = HAMMER2_STATE_DYNAMIC;
1521 state->txcmd = HAMMER2_MSGF_REPLY;
1522 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1523 state->flags |= HAMMER2_STATE_INSERTED;
1524 state->msgid = msg->any.head.msgid;
1525 state->spanid = msg->any.head.spanid;
1527 pthread_mutex_lock(&iocom->mtx);
1528 RB_INSERT(hammer2_state_tree, &iocom->staterd_tree, state);
1529 pthread_mutex_unlock(&iocom->mtx);
1532 fprintf(stderr, "create state %p id=%08x on iocom staterd %p\n",
1533 state, (uint32_t)state->msgid, iocom);
1536 case HAMMER2_MSGF_DELETE:
1538 * Persistent state is expected but might not exist if an
1539 * ABORT+DELETE races the close.
1541 if (state == NULL) {
1542 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1543 error = HAMMER2_IOQ_ERROR_EALREADY;
1545 fprintf(stderr, "missing-state %s\n",
1546 hammer2_msg_str(msg));
1547 error = HAMMER2_IOQ_ERROR_TRANS;
1554 * Handle another ABORT+DELETE case if the msgid has already
1557 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1558 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1559 error = HAMMER2_IOQ_ERROR_EALREADY;
1561 fprintf(stderr, "reused-state %s\n",
1562 hammer2_msg_str(msg));
1563 error = HAMMER2_IOQ_ERROR_TRANS;
1572 * Check for mid-stream ABORT command received, otherwise
1575 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1576 if (state == NULL ||
1577 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1578 error = HAMMER2_IOQ_ERROR_EALREADY;
1584 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
1585 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1587 * When receiving a reply with CREATE set the original
1588 * persistent state message should already exist.
1590 if (state == NULL) {
1591 fprintf(stderr, "no-state(r) %s\n",
1592 hammer2_msg_str(msg));
1593 error = HAMMER2_IOQ_ERROR_TRANS;
1597 assert(((state->rxcmd ^ msg->any.head.cmd) &
1598 HAMMER2_MSGF_REPLY) == 0);
1599 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1602 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1604 * Received REPLY+ABORT+DELETE in case where msgid has
1605 * already been fully closed, ignore the message.
1607 if (state == NULL) {
1608 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1609 error = HAMMER2_IOQ_ERROR_EALREADY;
1611 fprintf(stderr, "no-state(r,d) %s\n",
1612 hammer2_msg_str(msg));
1613 error = HAMMER2_IOQ_ERROR_TRANS;
1620 * Received REPLY+ABORT+DELETE in case where msgid has
1621 * already been reused for an unrelated message,
1622 * ignore the message.
1624 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1625 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1626 error = HAMMER2_IOQ_ERROR_EALREADY;
1628 fprintf(stderr, "reused-state(r,d) %s\n",
1629 hammer2_msg_str(msg));
1630 error = HAMMER2_IOQ_ERROR_TRANS;
1637 case HAMMER2_MSGF_REPLY:
1639 * Check for mid-stream ABORT reply received to sent command.
1641 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1642 if (state == NULL ||
1643 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1644 error = HAMMER2_IOQ_ERROR_EALREADY;
1655 hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1657 hammer2_state_t *state;
1659 if ((state = msg->state) == NULL) {
1661 * Free a non-transactional message, there is no state
1664 hammer2_msg_free(iocom, msg);
1665 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1667 * Message terminating transaction, destroy the related
1668 * state, the original message, and this message (if it
1669 * isn't the original message due to a CREATE|DELETE).
1671 pthread_mutex_lock(&iocom->mtx);
1672 state->rxcmd |= HAMMER2_MSGF_DELETE;
1673 if (state->txcmd & HAMMER2_MSGF_DELETE) {
1674 if (state->msg == msg)
1676 assert(state->flags & HAMMER2_STATE_INSERTED);
1677 if (state->rxcmd & HAMMER2_MSGF_REPLY) {
1678 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1679 RB_REMOVE(hammer2_state_tree,
1680 &iocom->statewr_tree, state);
1682 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1683 RB_REMOVE(hammer2_state_tree,
1684 &iocom->staterd_tree, state);
1686 state->flags &= ~HAMMER2_STATE_INSERTED;
1687 hammer2_state_free(state);
1691 pthread_mutex_unlock(&iocom->mtx);
1692 hammer2_msg_free(iocom, msg);
1693 } else if (state->msg != msg) {
1695 * Message not terminating transaction, leave state intact
1696 * and free message if it isn't the CREATE message.
1698 hammer2_msg_free(iocom, msg);
1703 hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1705 hammer2_state_t *state;
1707 if ((state = msg->state) == NULL) {
1708 hammer2_msg_free(iocom, msg);
1709 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1710 pthread_mutex_lock(&iocom->mtx);
1711 state->txcmd |= HAMMER2_MSGF_DELETE;
1712 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
1713 if (state->msg == msg)
1715 assert(state->flags & HAMMER2_STATE_INSERTED);
1716 if (state->txcmd & HAMMER2_MSGF_REPLY) {
1717 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1718 RB_REMOVE(hammer2_state_tree,
1719 &iocom->staterd_tree, state);
1721 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1722 RB_REMOVE(hammer2_state_tree,
1723 &iocom->statewr_tree, state);
1725 state->flags &= ~HAMMER2_STATE_INSERTED;
1726 hammer2_state_free(state);
1730 pthread_mutex_unlock(&iocom->mtx);
1731 hammer2_msg_free(iocom, msg);
1732 } else if (state->msg != msg) {
1733 hammer2_msg_free(iocom, msg);
1738 * Called with iocom locked
1741 hammer2_state_free(hammer2_state_t *state)
1743 hammer2_iocom_t *iocom = state->iocom;
1748 fprintf(stderr, "terminate state %p id=%08x\n",
1749 state, (uint32_t)state->msgid);
1751 assert(state->any.any == NULL);
1755 hammer2_msg_free_locked(iocom, msg);
1759 * When an iocom error is present we are trying to close down the
1760 * iocom, but we have to wait for all states to terminate before
1761 * we can do so. The iocom rx code will terminate the receive side
1762 * for all transactions by simulating incoming DELETE messages,
1763 * but the state doesn't go away until both sides are terminated.
1765 * We may have to wake up the rx code.
1767 if (iocom->ioq_rx.error &&
1768 RB_EMPTY(&iocom->staterd_tree) &&
1769 RB_EMPTY(&iocom->statewr_tree)) {
1771 write(iocom->wakeupfds[1], &dummy, 1);
1776 hammer2_basecmd_str(uint32_t cmd)
1778 static char buf[64];
1781 const char *protostr;
1784 switch(cmd & HAMMER2_MSGF_PROTOS) {
1785 case HAMMER2_MSG_PROTO_LNK:
1788 case HAMMER2_MSG_PROTO_DBG:
1791 case HAMMER2_MSG_PROTO_DOM:
1794 case HAMMER2_MSG_PROTO_CAC:
1797 case HAMMER2_MSG_PROTO_QRM:
1800 case HAMMER2_MSG_PROTO_BLK:
1803 case HAMMER2_MSG_PROTO_VOP:
1807 snprintf(protobuf, sizeof(protobuf), "%x_",
1808 (cmd & HAMMER2_MSGF_PROTOS) >> 20);
1809 protostr = protobuf;
1813 switch(cmd & (HAMMER2_MSGF_PROTOS |
1815 HAMMER2_MSGF_SIZE)) {
1816 case HAMMER2_LNK_PAD:
1819 case HAMMER2_LNK_PING:
1822 case HAMMER2_LNK_AUTH:
1825 case HAMMER2_LNK_CONN:
1828 case HAMMER2_LNK_SPAN:
1831 case HAMMER2_LNK_ERROR:
1832 if (cmd & HAMMER2_MSGF_DELETE)
1837 case HAMMER2_DBG_SHELL:
1841 snprintf(cmdbuf, sizeof(cmdbuf),
1842 "%06x", (cmd & (HAMMER2_MSGF_PROTOS |
1844 HAMMER2_MSGF_SIZE)));
1848 snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
1853 hammer2_msg_str(hammer2_msg_t *msg)
1855 hammer2_state_t *state;
1856 static char buf[256];
1860 const char *statestr;
1868 if ((state = msg->state) != NULL) {
1869 basecmd = (state->rxcmd & HAMMER2_MSGF_REPLY) ?
1870 state->txcmd : state->rxcmd;
1871 snprintf(statebuf, sizeof(statebuf),
1872 " %s=%s,L=%s%s,R=%s%s",
1873 ((state->txcmd & HAMMER2_MSGF_REPLY) ?
1874 "rcvcmd" : "sndcmd"),
1875 hammer2_basecmd_str(basecmd),
1876 ((state->txcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1877 ((state->txcmd & HAMMER2_MSGF_DELETE) ? "D" : ""),
1878 ((state->rxcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1879 ((state->rxcmd & HAMMER2_MSGF_DELETE) ? "D" : "")
1881 statestr = statebuf;
1889 switch(msg->any.head.error) {
1893 case HAMMER2_IOQ_ERROR_SYNC:
1894 errstr = "err=IOQ:NOSYNC";
1896 case HAMMER2_IOQ_ERROR_EOF:
1897 errstr = "err=IOQ:STREAMEOF";
1899 case HAMMER2_IOQ_ERROR_SOCK:
1900 errstr = "err=IOQ:SOCKERR";
1902 case HAMMER2_IOQ_ERROR_FIELD:
1903 errstr = "err=IOQ:BADFIELD";
1905 case HAMMER2_IOQ_ERROR_HCRC:
1906 errstr = "err=IOQ:BADHCRC";
1908 case HAMMER2_IOQ_ERROR_XCRC:
1909 errstr = "err=IOQ:BADXCRC";
1911 case HAMMER2_IOQ_ERROR_ACRC:
1912 errstr = "err=IOQ:BADACRC";
1914 case HAMMER2_IOQ_ERROR_STATE:
1915 errstr = "err=IOQ:BADSTATE";
1917 case HAMMER2_IOQ_ERROR_NOPEER:
1918 errstr = "err=IOQ:PEERCONFIG";
1920 case HAMMER2_IOQ_ERROR_NORKEY:
1921 errstr = "err=IOQ:BADRKEY";
1923 case HAMMER2_IOQ_ERROR_NOLKEY:
1924 errstr = "err=IOQ:BADLKEY";
1926 case HAMMER2_IOQ_ERROR_KEYXCHGFAIL:
1927 errstr = "err=IOQ:BADKEYXCHG";
1929 case HAMMER2_IOQ_ERROR_KEYFMT:
1930 errstr = "err=IOQ:BADFMT";
1932 case HAMMER2_IOQ_ERROR_BADURANDOM:
1933 errstr = "err=IOQ:BADRANDOM";
1935 case HAMMER2_IOQ_ERROR_MSGSEQ:
1936 errstr = "err=IOQ:BADSEQ";
1938 case HAMMER2_IOQ_ERROR_EALREADY:
1939 errstr = "err=IOQ:DUPMSG";
1941 case HAMMER2_IOQ_ERROR_TRANS:
1942 errstr = "err=IOQ:BADTRANS";
1944 case HAMMER2_MSG_ERR_NOSUPP:
1945 errstr = "err=NOSUPPORT";
1948 snprintf(errbuf, sizeof(errbuf),
1949 " err=%d", msg->any.head.error);
1958 if (msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1959 HAMMER2_MSGF_ABORT | HAMMER2_MSGF_REPLY)) {
1961 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1963 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
1965 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY)
1967 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT)
1975 snprintf(buf, sizeof(buf),
1976 "msg=%s%s %s id=%08x span=%08x %s",
1977 hammer2_basecmd_str(msg->any.head.cmd),
1980 (uint32_t)(intmax_t)msg->any.head.msgid, /* for brevity */
1981 (uint32_t)(intmax_t)msg->any.head.spanid, /* for brevity */