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(msg);
87 if ((msg = ioq->msg) != NULL) {
89 hammer2_msg_free(msg);
94 * Initialize a low-level communications channel.
96 * NOTE: The signal_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 (*signal_func)(hammer2_router_t *),
102 void (*rcvmsg_func)(hammer2_msg_t *),
103 void (*altmsg_func)(hammer2_iocom_t *))
105 bzero(iocom, sizeof(*iocom));
107 iocom->router.signal_callback = signal_func;
108 iocom->router.rcvmsg_callback = rcvmsg_func;
109 iocom->router.altmsg_callback = altmsg_func;
111 pthread_mutex_init(&iocom->mtx, NULL);
112 RB_INIT(&iocom->router.staterd_tree);
113 RB_INIT(&iocom->router.statewr_tree);
114 TAILQ_INIT(&iocom->freeq);
115 TAILQ_INIT(&iocom->freeq_aux);
116 TAILQ_INIT(&iocom->addrq);
117 TAILQ_INIT(&iocom->router.txmsgq);
118 iocom->router.iocom = iocom;
119 iocom->sock_fd = sock_fd;
120 iocom->alt_fd = alt_fd;
121 iocom->flags = HAMMER2_IOCOMF_RREQ;
123 iocom->flags |= HAMMER2_IOCOMF_SWORK;
124 hammer2_ioq_init(iocom, &iocom->ioq_rx);
125 hammer2_ioq_init(iocom, &iocom->ioq_tx);
126 if (pipe(iocom->wakeupfds) < 0)
128 fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
129 fcntl(iocom->wakeupfds[1], F_SETFL, O_NONBLOCK);
132 * Negotiate session crypto synchronously. This will mark the
133 * connection as error'd if it fails.
135 hammer2_crypto_negotiate(iocom);
138 * Make sure our fds are set to non-blocking for the iocom core.
141 fcntl(sock_fd, F_SETFL, O_NONBLOCK);
143 /* if line buffered our single fgets() should be fine */
145 fcntl(alt_fd, F_SETFL, O_NONBLOCK);
150 * May only be called from a callback from iocom_core.
152 * Adjust state machine functions, set flags to guarantee that both
153 * the recevmsg_func and the sendmsg_func is called at least once.
156 hammer2_router_restate(hammer2_router_t *router,
157 void (*signal_func)(hammer2_router_t *),
158 void (*rcvmsg_func)(hammer2_msg_t *msg),
159 void (*altmsg_func)(hammer2_iocom_t *))
161 router->signal_callback = signal_func;
162 router->rcvmsg_callback = rcvmsg_func;
163 router->altmsg_callback = altmsg_func;
165 router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
167 router->iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
171 hammer2_router_signal(hammer2_router_t *router)
173 if (router->signal_callback)
174 router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
178 * Cleanup a terminating iocom.
180 * Caller should not hold iocom->mtx. The iocom has already been disconnected
181 * from all possible references to it.
184 hammer2_iocom_done(hammer2_iocom_t *iocom)
188 if (iocom->sock_fd >= 0) {
189 close(iocom->sock_fd);
192 if (iocom->alt_fd >= 0) {
193 close(iocom->alt_fd);
196 hammer2_ioq_done(iocom, &iocom->ioq_rx);
197 hammer2_ioq_done(iocom, &iocom->ioq_tx);
198 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
199 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
202 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
203 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
205 msg->aux_data = NULL;
208 if (iocom->wakeupfds[0] >= 0) {
209 close(iocom->wakeupfds[0]);
210 iocom->wakeupfds[0] = -1;
212 if (iocom->wakeupfds[1] >= 0) {
213 close(iocom->wakeupfds[1]);
214 iocom->wakeupfds[1] = -1;
216 pthread_mutex_destroy(&iocom->mtx);
220 * Allocate a new one-way message.
223 hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
224 void (*func)(hammer2_msg_t *), void *data)
226 hammer2_state_t *state = NULL;
227 hammer2_iocom_t *iocom = router->iocom;
231 pthread_mutex_lock(&iocom->mtx);
233 aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
234 ~HAMMER2_MSG_ALIGNMASK;
235 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
236 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
238 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
239 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
241 if ((cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_REPLY)) ==
242 HAMMER2_MSGF_CREATE) {
244 * Create state when CREATE is set without REPLY.
246 * NOTE: CREATE in txcmd handled by hammer2_msg_write()
247 * NOTE: DELETE in txcmd handled by hammer2_state_cleanuptx()
249 state = malloc(sizeof(*state));
250 bzero(state, sizeof(*state));
251 state->iocom = iocom;
252 state->flags = HAMMER2_STATE_DYNAMIC;
253 state->msgid = (uint64_t)(uintptr_t)state;
254 /* XXX set state->spanid from router */
255 state->txcmd = cmd & ~(HAMMER2_MSGF_CREATE |
256 HAMMER2_MSGF_DELETE);
257 state->rxcmd = HAMMER2_MSGF_REPLY;
259 state->any.any = data;
260 pthread_mutex_lock(&iocom->mtx);
261 RB_INSERT(hammer2_state_tree, &iocom->router.statewr_tree, state);
262 pthread_mutex_unlock(&iocom->mtx);
263 state->flags |= HAMMER2_STATE_INSERTED;
265 pthread_mutex_unlock(&iocom->mtx);
267 msg = malloc(sizeof(*msg));
268 bzero(msg, sizeof(*msg));
269 msg->aux_data = NULL;
272 if (msg->aux_size != aux_size) {
275 msg->aux_data = NULL;
279 msg->aux_data = malloc(aux_size);
280 msg->aux_size = aux_size;
283 hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
285 bzero(&msg->any.head, hbytes);
286 msg->hdr_size = hbytes;
287 msg->any.head.cmd = cmd;
288 msg->any.head.aux_descr = 0;
289 msg->any.head.aux_crc = 0;
290 msg->router = router;
294 msg->any.head.msgid = state->msgid;
300 * Free a message so it can be reused afresh.
302 * NOTE: aux_size can be 0 with a non-NULL aux_data.
306 hammer2_msg_free_locked(hammer2_msg_t *msg)
308 hammer2_iocom_t *iocom = msg->router->iocom;
312 TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
314 TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
318 hammer2_msg_free(hammer2_msg_t *msg)
320 hammer2_iocom_t *iocom = msg->router->iocom;
322 pthread_mutex_lock(&iocom->mtx);
323 hammer2_msg_free_locked(msg);
324 pthread_mutex_unlock(&iocom->mtx);
328 * I/O core loop for an iocom.
330 * Thread localized, iocom->mtx not held.
333 hammer2_iocom_core(hammer2_iocom_t *iocom)
335 struct pollfd fds[3];
340 int wi; /* wakeup pipe */
342 int ai; /* alt bulk path socket */
344 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
345 if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
346 HAMMER2_IOCOMF_WWORK |
347 HAMMER2_IOCOMF_PWORK |
348 HAMMER2_IOCOMF_SWORK |
349 HAMMER2_IOCOMF_ARWORK |
350 HAMMER2_IOCOMF_AWWORK)) == 0) {
352 * Only poll if no immediate work is pending.
353 * Otherwise we are just wasting our time calling
364 * Always check the inter-thread pipe, e.g.
365 * for iocom->txmsgq work.
368 fds[wi].fd = iocom->wakeupfds[0];
369 fds[wi].events = POLLIN;
373 * Check the socket input/output direction as
376 if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
377 HAMMER2_IOCOMF_WREQ)) {
379 fds[si].fd = iocom->sock_fd;
383 if (iocom->flags & HAMMER2_IOCOMF_RREQ)
384 fds[si].events |= POLLIN;
385 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
386 fds[si].events |= POLLOUT;
390 * Check the alternative fd for work.
392 if (iocom->alt_fd >= 0) {
394 fds[ai].fd = iocom->alt_fd;
395 fds[ai].events = POLLIN;
398 poll(fds, count, timeout);
400 if (wi >= 0 && (fds[wi].revents & POLLIN))
401 iocom->flags |= HAMMER2_IOCOMF_PWORK;
402 if (si >= 0 && (fds[si].revents & POLLIN))
403 iocom->flags |= HAMMER2_IOCOMF_RWORK;
404 if (si >= 0 && (fds[si].revents & POLLOUT))
405 iocom->flags |= HAMMER2_IOCOMF_WWORK;
406 if (wi >= 0 && (fds[wi].revents & POLLOUT))
407 iocom->flags |= HAMMER2_IOCOMF_WWORK;
408 if (ai >= 0 && (fds[ai].revents & POLLIN))
409 iocom->flags |= HAMMER2_IOCOMF_ARWORK;
412 * Always check the pipe
414 iocom->flags |= HAMMER2_IOCOMF_PWORK;
417 if (iocom->flags & HAMMER2_IOCOMF_SWORK) {
418 iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
419 iocom->router.signal_callback(&iocom->router);
423 * Pending message queues from other threads wake us up
424 * with a write to the wakeupfds[] pipe. We have to clear
425 * the pipe with a dummy read.
427 if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
428 iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
429 read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
430 iocom->flags |= HAMMER2_IOCOMF_RWORK;
431 iocom->flags |= HAMMER2_IOCOMF_WWORK;
432 if (TAILQ_FIRST(&iocom->router.txmsgq))
433 hammer2_iocom_flush1(iocom);
437 * Message write sequencing
439 if (iocom->flags & HAMMER2_IOCOMF_WWORK)
440 hammer2_iocom_flush1(iocom);
443 * Message read sequencing. Run this after the write
444 * sequencing in case the write sequencing allowed another
445 * auto-DELETE to occur on the read side.
447 if (iocom->flags & HAMMER2_IOCOMF_RWORK) {
448 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
449 (msg = hammer2_ioq_read(iocom)) != NULL) {
451 fprintf(stderr, "receive %s\n",
452 hammer2_msg_str(msg));
454 iocom->router.rcvmsg_callback(msg);
455 hammer2_state_cleanuprx(iocom, msg);
459 if (iocom->flags & HAMMER2_IOCOMF_ARWORK) {
460 iocom->flags &= ~HAMMER2_IOCOMF_ARWORK;
461 iocom->router.altmsg_callback(iocom);
467 * Make sure there's enough room in the FIFO to hold the
470 * Assume worst case encrypted form is 2x the size of the
471 * plaintext equivalent.
475 hammer2_ioq_makeroom(hammer2_ioq_t *ioq, size_t needed)
480 bytes = ioq->fifo_cdx - ioq->fifo_beg;
481 nmax = sizeof(ioq->buf) - ioq->fifo_end;
482 if (bytes + nmax / 2 < needed) {
484 bcopy(ioq->buf + ioq->fifo_beg,
488 ioq->fifo_cdx -= ioq->fifo_beg;
490 if (ioq->fifo_cdn < ioq->fifo_end) {
491 bcopy(ioq->buf + ioq->fifo_cdn,
492 ioq->buf + ioq->fifo_cdx,
493 ioq->fifo_end - ioq->fifo_cdn);
495 ioq->fifo_end -= ioq->fifo_cdn - ioq->fifo_cdx;
496 ioq->fifo_cdn = ioq->fifo_cdx;
497 nmax = sizeof(ioq->buf) - ioq->fifo_end;
503 * Read the next ready message from the ioq, issuing I/O if needed.
504 * Caller should retry on a read-event when NULL is returned.
506 * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
507 * be returned for each open transaction, then the ioq and iocom
508 * will be errored out and a non-transactional HAMMER2_LNK_ERROR
509 * msg will be returned as the final message. The caller should not call
510 * us again after the final message is returned.
512 * Thread localized, iocom->mtx not held.
515 hammer2_ioq_read(hammer2_iocom_t *iocom)
517 hammer2_ioq_t *ioq = &iocom->ioq_rx;
519 hammer2_msg_hdr_t *head;
520 hammer2_state_t *state;
528 iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
531 * If a message is already pending we can just remove and
532 * return it. Message state has already been processed.
534 if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
535 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
543 * Message read in-progress (msg is NULL at the moment). We don't
544 * allocate a msg until we have its core header.
546 nmax = sizeof(ioq->buf) - ioq->fifo_end;
547 bytes = ioq->fifo_cdx - ioq->fifo_beg; /* already decrypted */
551 case HAMMER2_MSGQ_STATE_HEADER1:
553 * Load the primary header, fail on any non-trivial read
554 * error or on EOF. Since the primary header is the same
555 * size is the message alignment it will never straddle
556 * the end of the buffer.
558 nmax = hammer2_ioq_makeroom(ioq, sizeof(msg->any.head));
559 if (bytes < sizeof(msg->any.head)) {
560 n = read(iocom->sock_fd,
561 ioq->buf + ioq->fifo_end,
565 ioq->error = HAMMER2_IOQ_ERROR_EOF;
568 if (errno != EINTR &&
569 errno != EINPROGRESS &&
571 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
577 ioq->fifo_end += (size_t)n;
582 * Decrypt data received so far. Data will be decrypted
583 * in-place but might create gaps in the FIFO. Partial
584 * blocks are not immediately decrypted.
586 * WARNING! The header might be in the wrong endian, we
587 * do not fix it up until we get the entire
590 if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
591 hammer2_crypto_decrypt(iocom, ioq);
593 ioq->fifo_cdx = ioq->fifo_end;
594 ioq->fifo_cdn = ioq->fifo_end;
596 bytes = ioq->fifo_cdx - ioq->fifo_beg;
599 * Insufficient data accumulated (msg is NULL, caller will
603 if (bytes < sizeof(msg->any.head))
607 * Check and fixup the core header. Note that the icrc
608 * has to be calculated before any fixups, but the crc
609 * fields in the msg may have to be swapped like everything
612 head = (void *)(ioq->buf + ioq->fifo_beg);
613 if (head->magic != HAMMER2_MSGHDR_MAGIC &&
614 head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
615 ioq->error = HAMMER2_IOQ_ERROR_SYNC;
620 * Calculate the full header size and aux data size
622 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
623 ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
625 ioq->abytes = bswap32(head->aux_bytes) *
628 ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
630 ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
632 if (ioq->hbytes < sizeof(msg->any.head) ||
633 ioq->hbytes > sizeof(msg->any) ||
634 ioq->abytes > HAMMER2_MSGAUX_MAX) {
635 ioq->error = HAMMER2_IOQ_ERROR_FIELD;
640 * Allocate the message, the next state will fill it in.
642 msg = hammer2_msg_alloc(&iocom->router, ioq->abytes, 0,
647 * Fall through to the next state. Make sure that the
648 * extended header does not straddle the end of the buffer.
649 * We still want to issue larger reads into our buffer,
650 * book-keeping is easier if we don't bcopy() yet.
652 * Make sure there is enough room for bloated encrypt data.
654 nmax = hammer2_ioq_makeroom(ioq, ioq->hbytes);
655 ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
657 case HAMMER2_MSGQ_STATE_HEADER2:
659 * Fill out the extended header.
662 if (bytes < ioq->hbytes) {
663 n = read(iocom->sock_fd,
664 ioq->buf + ioq->fifo_end,
668 ioq->error = HAMMER2_IOQ_ERROR_EOF;
671 if (errno != EINTR &&
672 errno != EINPROGRESS &&
674 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
680 ioq->fifo_end += (size_t)n;
684 if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
685 hammer2_crypto_decrypt(iocom, ioq);
687 ioq->fifo_cdx = ioq->fifo_end;
688 ioq->fifo_cdn = ioq->fifo_end;
690 bytes = ioq->fifo_cdx - ioq->fifo_beg;
693 * Insufficient data accumulated (set msg NULL so caller will
696 if (bytes < ioq->hbytes) {
702 * Calculate the extended header, decrypt data received
703 * so far. Handle endian-conversion for the entire extended
706 head = (void *)(ioq->buf + ioq->fifo_beg);
711 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
712 xcrc32 = bswap32(head->hdr_crc);
714 xcrc32 = head->hdr_crc;
716 if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
717 ioq->error = HAMMER2_IOQ_ERROR_XCRC;
718 fprintf(stderr, "BAD-XCRC(%08x,%08x) %s\n",
719 xcrc32, hammer2_icrc32(head, ioq->hbytes),
720 hammer2_msg_str(msg));
724 head->hdr_crc = xcrc32;
726 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
727 hammer2_bswap_head(head);
731 * Copy the extended header into the msg and adjust the
734 bcopy(head, &msg->any, ioq->hbytes);
737 * We are either done or we fall-through.
739 if (ioq->abytes == 0) {
740 ioq->fifo_beg += ioq->hbytes;
745 * Must adjust bytes (and the state) when falling through.
746 * nmax doesn't change.
748 ioq->fifo_beg += ioq->hbytes;
749 bytes -= ioq->hbytes;
750 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
752 case HAMMER2_MSGQ_STATE_AUXDATA1:
754 * Copy the partial or complete payload from remaining
755 * bytes in the FIFO in order to optimize the makeroom call
756 * in the AUXDATA2 state. We have to fall-through either
757 * way so we can check the crc.
759 * msg->aux_size tracks our aux data.
761 if (bytes >= ioq->abytes) {
762 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
764 msg->aux_size = ioq->abytes;
765 ioq->fifo_beg += ioq->abytes;
766 assert(ioq->fifo_beg <= ioq->fifo_cdx);
767 assert(ioq->fifo_cdx <= ioq->fifo_cdn);
768 bytes -= ioq->abytes;
770 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
772 msg->aux_size = bytes;
773 ioq->fifo_beg += bytes;
774 if (ioq->fifo_cdx < ioq->fifo_beg)
775 ioq->fifo_cdx = ioq->fifo_beg;
776 assert(ioq->fifo_beg <= ioq->fifo_cdx);
777 assert(ioq->fifo_cdx <= ioq->fifo_cdn);
782 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
784 case HAMMER2_MSGQ_STATE_AUXDATA2:
786 * Make sure there is enough room for more data.
789 nmax = hammer2_ioq_makeroom(ioq, ioq->abytes - msg->aux_size);
792 * Read and decrypt more of the payload.
794 if (msg->aux_size < ioq->abytes) {
796 n = read(iocom->sock_fd,
797 ioq->buf + ioq->fifo_end,
801 ioq->error = HAMMER2_IOQ_ERROR_EOF;
804 if (errno != EINTR &&
805 errno != EINPROGRESS &&
807 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
813 ioq->fifo_end += (size_t)n;
817 if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
818 hammer2_crypto_decrypt(iocom, ioq);
820 ioq->fifo_cdx = ioq->fifo_end;
821 ioq->fifo_cdn = ioq->fifo_end;
823 bytes = ioq->fifo_cdx - ioq->fifo_beg;
825 if (bytes > ioq->abytes - msg->aux_size)
826 bytes = ioq->abytes - msg->aux_size;
829 bcopy(ioq->buf + ioq->fifo_beg,
830 msg->aux_data + msg->aux_size,
832 msg->aux_size += bytes;
833 ioq->fifo_beg += bytes;
837 * Insufficient data accumulated (set msg NULL so caller will
840 if (msg->aux_size < ioq->abytes) {
844 assert(msg->aux_size == ioq->abytes);
847 * Check aux_crc, then we are done.
849 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
850 if (xcrc32 != msg->any.head.aux_crc) {
851 ioq->error = HAMMER2_IOQ_ERROR_ACRC;
855 case HAMMER2_MSGQ_STATE_ERROR:
857 * Continued calls to drain recorded transactions (returning
858 * a LNK_ERROR for each one), before we return the final
865 * We don't double-return errors, the caller should not
866 * have called us again after getting an error msg.
873 * Check the message sequence. The iv[] should prevent any
874 * possibility of a replay but we add this check anyway.
876 if (msg && ioq->error == 0) {
877 if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
878 ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
885 * Process transactional state for the message.
887 if (msg && ioq->error == 0) {
888 error = hammer2_state_msgrx(iocom, msg);
890 if (error == HAMMER2_IOQ_ERROR_EALREADY) {
891 hammer2_msg_free(msg);
899 * Handle error, RREQ, or completion
901 * NOTE: nmax and bytes are invalid at this point, we don't bother
902 * to update them when breaking out.
907 * An unrecoverable error causes all active receive
908 * transactions to be terminated with a LNK_ERROR message.
910 * Once all active transactions are exhausted we set the
911 * iocom ERROR flag and return a non-transactional LNK_ERROR
912 * message, which should cause master processing loops to
915 assert(ioq->msg == msg);
917 hammer2_msg_free(msg);
922 * No more I/O read processing
924 ioq->state = HAMMER2_MSGQ_STATE_ERROR;
927 * Simulate a remote LNK_ERROR DELETE msg for any open
928 * transactions, ending with a final non-transactional
929 * LNK_ERROR (that the session can detect) when no
930 * transactions remain.
932 msg = hammer2_msg_alloc(&iocom->router, 0, 0, NULL, NULL);
933 bzero(&msg->any.head, sizeof(msg->any.head));
934 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
935 msg->any.head.cmd = HAMMER2_LNK_ERROR;
936 msg->any.head.error = ioq->error;
938 pthread_mutex_lock(&iocom->mtx);
939 hammer2_iocom_drain(iocom);
940 if ((state = RB_ROOT(&iocom->router.staterd_tree)) != NULL) {
942 * Active remote transactions are still present.
943 * Simulate the other end sending us a DELETE.
945 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
946 hammer2_msg_free(msg);
949 /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
951 msg->any.head.spanid = state->spanid;
952 msg->any.head.msgid = state->msgid;
953 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
956 } else if ((state = RB_ROOT(&iocom->router.statewr_tree)) != NULL) {
958 * Active local transactions are still present.
959 * Simulate the other end sending us a DELETE.
961 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
962 hammer2_msg_free(msg);
966 msg->any.head.spanid = state->spanid;
967 msg->any.head.msgid = state->msgid;
968 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
969 HAMMER2_MSGF_DELETE |
971 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
978 * No active local or remote transactions remain.
979 * Generate a final LNK_ERROR and flag EOF.
982 iocom->flags |= HAMMER2_IOCOMF_EOF;
983 fprintf(stderr, "EOF ON SOCKET %d\n", iocom->sock_fd);
985 pthread_mutex_unlock(&iocom->mtx);
988 * For the iocom error case we want to set RWORK to indicate
989 * that more messages might be pending.
991 * It is possible to return NULL when there is more work to
992 * do because each message has to be DELETEd in both
993 * directions before we continue on with the next (though
994 * this could be optimized). The transmit direction will
998 iocom->flags |= HAMMER2_IOCOMF_RWORK;
999 } else if (msg == NULL) {
1001 * Insufficient data received to finish building the message,
1002 * set RREQ and return NULL.
1004 * Leave ioq->msg intact.
1005 * Leave the FIFO intact.
1007 iocom->flags |= HAMMER2_IOCOMF_RREQ;
1012 * The fifo has already been advanced past the message.
1013 * Trivially reset the FIFO indices if possible.
1015 * clear the FIFO if it is now empty and set RREQ to wait
1016 * for more from the socket. If the FIFO is not empty set
1017 * TWORK to bypass the poll so we loop immediately.
1019 if (ioq->fifo_beg == ioq->fifo_cdx &&
1020 ioq->fifo_cdn == ioq->fifo_end) {
1021 iocom->flags |= HAMMER2_IOCOMF_RREQ;
1027 iocom->flags |= HAMMER2_IOCOMF_RWORK;
1029 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
1036 * Calculate the header and data crc's and write a low-level message to
1037 * the connection. If aux_crc is non-zero the aux_data crc is already
1038 * assumed to have been set.
1040 * A non-NULL msg is added to the queue but not necessarily flushed.
1041 * Calling this function with msg == NULL will get a flush going.
1043 * Caller must hold iocom->mtx.
1046 hammer2_iocom_flush1(hammer2_iocom_t *iocom)
1048 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1052 hammer2_msg_queue_t tmpq;
1054 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
1056 pthread_mutex_lock(&iocom->mtx);
1057 while ((msg = TAILQ_FIRST(&iocom->router.txmsgq)) != NULL) {
1058 TAILQ_REMOVE(&iocom->router.txmsgq, msg, qentry);
1059 TAILQ_INSERT_TAIL(&tmpq, msg, qentry);
1061 pthread_mutex_unlock(&iocom->mtx);
1063 while ((msg = TAILQ_FIRST(&tmpq)) != NULL) {
1065 * Process terminal connection errors.
1067 TAILQ_REMOVE(&tmpq, msg, qentry);
1069 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
1075 * Finish populating the msg fields. The salt ensures that
1076 * the iv[] array is ridiculously randomized and we also
1077 * re-seed our PRNG every 32768 messages just to be sure.
1079 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
1080 msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
1082 if ((ioq->seq & 32767) == 0)
1086 * Calculate aux_crc if 0, then calculate hdr_crc.
1088 if (msg->aux_size && msg->any.head.aux_crc == 0) {
1089 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
1090 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
1091 msg->any.head.aux_crc = xcrc32;
1093 msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
1094 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
1096 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1098 msg->any.head.hdr_crc = 0;
1099 msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
1102 * Enqueue the message (the flush codes handles stream
1105 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
1108 hammer2_iocom_flush2(iocom);
1112 * Thread localized, iocom->mtx not held by caller.
1115 hammer2_iocom_flush2(hammer2_iocom_t *iocom)
1117 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1120 struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
1129 hammer2_iocom_drain(iocom);
1134 * Pump messages out the connection by building an iovec.
1136 * ioq->hbytes/ioq->abytes tracks how much of the first message
1137 * in the queue has been successfully written out, so we can
1145 TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
1146 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1148 abytes = msg->aux_size;
1149 assert(hoff <= hbytes && aoff <= abytes);
1151 if (hoff < hbytes) {
1152 iov[iovcnt].iov_base = (char *)&msg->any.head + hoff;
1153 iov[iovcnt].iov_len = hbytes - hoff;
1154 nact += hbytes - hoff;
1156 if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
1159 if (aoff < abytes) {
1160 assert(msg->aux_data != NULL);
1161 iov[iovcnt].iov_base = (char *)msg->aux_data + aoff;
1162 iov[iovcnt].iov_len = abytes - aoff;
1163 nact += abytes - aoff;
1165 if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
1175 * Encrypt and write the data. The crypto code will move the
1176 * data into the fifo and adjust the iov as necessary. If
1177 * encryption is disabled the iov is left alone.
1179 * May return a smaller iov (thus a smaller n), with aggregated
1180 * chunks. May reduce nmax to what fits in the FIFO.
1182 * This function sets nact to the number of original bytes now
1183 * encrypted, adding to the FIFO some number of bytes that might
1184 * be greater depending on the crypto mechanic. iov[] is adjusted
1185 * to point at the FIFO if necessary.
1187 * NOTE: The return value from the writev() is the post-encrypted
1188 * byte count, not the plaintext count.
1190 if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
1192 * Make sure the FIFO has a reasonable amount of space
1193 * left (if not completely full).
1195 if (ioq->fifo_beg > sizeof(ioq->buf) / 2 &&
1196 sizeof(ioq->buf) - ioq->fifo_end >= HAMMER2_MSG_ALIGN * 2) {
1197 bcopy(ioq->buf + ioq->fifo_beg, ioq->buf,
1198 ioq->fifo_end - ioq->fifo_beg);
1199 ioq->fifo_cdx -= ioq->fifo_beg;
1200 ioq->fifo_cdn -= ioq->fifo_beg;
1201 ioq->fifo_end -= ioq->fifo_beg;
1205 iovcnt = hammer2_crypto_encrypt(iocom, ioq, iov, iovcnt, &nact);
1206 n = writev(iocom->sock_fd, iov, iovcnt);
1211 if (ioq->fifo_beg == ioq->fifo_end) {
1219 n = writev(iocom->sock_fd, iov, iovcnt);
1227 * Clean out the transmit queue based on what we successfully
1228 * sent (nact is the plaintext count). ioq->hbytes/abytes
1229 * represents the portion of the first message previously sent.
1231 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1232 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1234 abytes = msg->aux_size;
1236 if ((size_t)nact < hbytes - ioq->hbytes) {
1237 ioq->hbytes += nact;
1241 nact -= hbytes - ioq->hbytes;
1242 ioq->hbytes = hbytes;
1243 if ((size_t)nact < abytes - ioq->abytes) {
1244 ioq->abytes += nact;
1248 nact -= abytes - ioq->abytes;
1250 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1255 hammer2_state_cleanuptx(iocom, msg);
1260 * Process the return value from the write w/regards to blocking.
1263 if (errno != EINTR &&
1264 errno != EINPROGRESS &&
1269 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
1270 hammer2_iocom_drain(iocom);
1273 * Wait for socket buffer space
1275 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1278 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1281 hammer2_iocom_drain(iocom);
1286 * Kill pending msgs on ioq_tx and adjust the flags such that no more
1287 * write events will occur. We don't kill read msgs because we want
1288 * the caller to pull off our contrived terminal error msg to detect
1289 * the connection failure.
1291 * Thread localized, iocom->mtx not held by caller.
1294 hammer2_iocom_drain(hammer2_iocom_t *iocom)
1296 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1299 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
1303 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1304 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
1306 hammer2_state_cleanuptx(iocom, msg);
1311 * Write a message to an iocom, with additional state processing.
1314 hammer2_msg_write(hammer2_msg_t *msg)
1316 hammer2_iocom_t *iocom = msg->router->iocom;
1317 hammer2_state_t *state;
1321 * Handle state processing, create state if necessary.
1323 pthread_mutex_lock(&iocom->mtx);
1324 if ((state = msg->state) != NULL) {
1326 * Existing transaction (could be reply). It is also
1327 * possible for this to be the first reply (CREATE is set),
1328 * in which case we populate state->txcmd.
1330 * state->txcmd is adjusted to hold the final message cmd,
1331 * and we also be sure to set the CREATE bit here. We did
1332 * not set it in hammer2_msg_alloc() because that would have
1333 * not been serialized (state could have gotten ripped out
1334 * from under the message prior to it being transmitted).
1336 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE |
1337 HAMMER2_MSGF_REPLY)) ==
1338 HAMMER2_MSGF_CREATE) {
1339 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1341 msg->any.head.msgid = state->msgid;
1342 msg->any.head.spanid = state->spanid;
1343 assert(((state->txcmd ^ msg->any.head.cmd) &
1344 HAMMER2_MSGF_REPLY) == 0);
1345 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1346 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1348 msg->any.head.msgid = 0;
1349 /* XXX set spanid by router */
1353 * Queue it for output, wake up the I/O pthread. Note that the
1354 * I/O thread is responsible for generating the CRCs and encryption.
1356 TAILQ_INSERT_TAIL(&iocom->router.txmsgq, msg, qentry);
1358 write(iocom->wakeupfds[1], &dummy, 1); /* XXX optimize me */
1359 pthread_mutex_unlock(&iocom->mtx);
1363 * This is a shortcut to formulate a reply to msg with a simple error code,
1364 * It can reply to and terminate a transaction, or it can reply to a one-way
1365 * messages. A HAMMER2_LNK_ERROR command code is utilized to encode
1366 * the error code (which can be 0). Not all transactions are terminated
1367 * with HAMMER2_LNK_ERROR status (the low level only cares about the
1368 * MSGF_DELETE flag), but most are.
1370 * Replies to one-way messages are a bit of an oxymoron but the feature
1371 * is used by the debug (DBG) protocol.
1373 * The reply contains no extended data.
1376 hammer2_msg_reply(hammer2_msg_t *msg, uint32_t error)
1378 hammer2_iocom_t *iocom = msg->router->iocom;
1379 hammer2_state_t *state = msg->state;
1380 hammer2_msg_t *nmsg;
1385 * Reply with a simple error code and terminate the transaction.
1387 cmd = HAMMER2_LNK_ERROR;
1390 * Check if our direction has even been initiated yet, set CREATE.
1392 * Check what direction this is (command or reply direction). Note
1393 * that txcmd might not have been initiated yet.
1395 * If our direction has already been closed we just return without
1399 if (state->txcmd & HAMMER2_MSGF_DELETE)
1401 if (state->txcmd & HAMMER2_MSGF_REPLY)
1402 cmd |= HAMMER2_MSGF_REPLY;
1403 cmd |= HAMMER2_MSGF_DELETE;
1405 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1406 cmd |= HAMMER2_MSGF_REPLY;
1410 * Allocate the message and associate it with the existing state.
1411 * We cannot pass MSGF_CREATE to msg_alloc() because that may
1412 * allocate new state. We have our state already.
1414 nmsg = hammer2_msg_alloc(&iocom->router, 0, cmd, NULL, NULL);
1416 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1417 nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
1419 nmsg->any.head.error = error;
1420 nmsg->state = state;
1421 hammer2_msg_write(nmsg);
1425 * Similar to hammer2_msg_reply() but leave the transaction open. That is,
1426 * we are generating a streaming reply or an intermediate acknowledgement
1427 * of some sort as part of the higher level protocol, with more to come
1431 hammer2_msg_result(hammer2_msg_t *msg, uint32_t error)
1433 hammer2_iocom_t *iocom = msg->router->iocom;
1434 hammer2_state_t *state = msg->state;
1435 hammer2_msg_t *nmsg;
1440 * Reply with a simple error code and terminate the transaction.
1442 cmd = HAMMER2_LNK_ERROR;
1445 * Check if our direction has even been initiated yet, set CREATE.
1447 * Check what direction this is (command or reply direction). Note
1448 * that txcmd might not have been initiated yet.
1450 * If our direction has already been closed we just return without
1454 if (state->txcmd & HAMMER2_MSGF_DELETE)
1456 if (state->txcmd & HAMMER2_MSGF_REPLY)
1457 cmd |= HAMMER2_MSGF_REPLY;
1458 /* continuing transaction, do not set MSGF_DELETE */
1460 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1461 cmd |= HAMMER2_MSGF_REPLY;
1464 nmsg = hammer2_msg_alloc(&iocom->router, 0, cmd, NULL, NULL);
1466 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1467 nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
1469 nmsg->any.head.error = error;
1470 nmsg->state = state;
1471 hammer2_msg_write(nmsg);
1475 * Terminate a transaction given a state structure by issuing a DELETE.
1478 hammer2_state_reply(hammer2_state_t *state, uint32_t error)
1480 hammer2_msg_t *nmsg;
1481 uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
1484 * Nothing to do if we already transmitted a delete
1486 if (state->txcmd & HAMMER2_MSGF_DELETE)
1490 * Set REPLY if the other end initiated the command. Otherwise
1491 * we are the command direction.
1493 if (state->txcmd & HAMMER2_MSGF_REPLY)
1494 cmd |= HAMMER2_MSGF_REPLY;
1496 nmsg = hammer2_msg_alloc(&state->iocom->router, 0, cmd, NULL, NULL);
1498 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1499 nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
1501 nmsg->any.head.error = error;
1502 nmsg->state = state;
1503 hammer2_msg_write(nmsg);
1506 /************************************************************************
1507 * TRANSACTION STATE HANDLING *
1508 ************************************************************************
1513 * Process state tracking for a message after reception, prior to
1516 * Called with msglk held and the msg dequeued.
1518 * All messages are called with dummy state and return actual state.
1519 * (One-off messages often just return the same dummy state).
1521 * May request that caller discard the message by setting *discardp to 1.
1522 * The returned state is not used in this case and is allowed to be NULL.
1526 * These routines handle persistent and command/reply message state via the
1527 * CREATE and DELETE flags. The first message in a command or reply sequence
1528 * sets CREATE, the last message in a command or reply sequence sets DELETE.
1530 * There can be any number of intermediate messages belonging to the same
1531 * sequence sent inbetween the CREATE message and the DELETE message,
1532 * which set neither flag. This represents a streaming command or reply.
1534 * Any command message received with CREATE set expects a reply sequence to
1535 * be returned. Reply sequences work the same as command sequences except the
1536 * REPLY bit is also sent. Both the command side and reply side can
1537 * degenerate into a single message with both CREATE and DELETE set. Note
1538 * that one side can be streaming and the other side not, or neither, or both.
1540 * The msgid is unique for the initiator. That is, two sides sending a new
1541 * message can use the same msgid without colliding.
1545 * ABORT sequences work by setting the ABORT flag along with normal message
1546 * state. However, ABORTs can also be sent on half-closed messages, that is
1547 * even if the command or reply side has already sent a DELETE, as long as
1548 * the message has not been fully closed it can still send an ABORT+DELETE
1549 * to terminate the half-closed message state.
1551 * Since ABORT+DELETEs can race we silently discard ABORT's for message
1552 * state which has already been fully closed. REPLY+ABORT+DELETEs can
1553 * also race, and in this situation the other side might have already
1554 * initiated a new unrelated command with the same message id. Since
1555 * the abort has not set the CREATE flag the situation can be detected
1556 * and the message will also be discarded.
1558 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
1559 * The ABORT request is essentially integrated into the command instead
1560 * of being sent later on. In this situation the command implementation
1561 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
1562 * special-case non-blocking operation for the command.
1564 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
1565 * to be mid-stream aborts for command/reply sequences. ABORTs on
1566 * one-way messages are not supported.
1568 * NOTE! If a command sequence does not support aborts the ABORT flag is
1573 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
1574 * set. One-off messages cannot be aborted and typically aren't processed
1575 * by these routines. The REPLY bit can be used to distinguish whether a
1576 * one-off message is a command or reply. For example, one-off replies
1577 * will typically just contain status updates.
1580 hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1582 hammer2_state_t *state;
1583 hammer2_state_t dummy;
1587 * Lock RB tree and locate existing persistent state, if any.
1589 * If received msg is a command state is on staterd_tree.
1590 * If received msg is a reply state is on statewr_tree.
1593 dummy.msgid = msg->any.head.msgid;
1594 dummy.spanid = msg->any.head.spanid;
1595 pthread_mutex_lock(&iocom->mtx);
1596 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1597 state = RB_FIND(hammer2_state_tree,
1598 &iocom->router.statewr_tree, &dummy);
1600 state = RB_FIND(hammer2_state_tree,
1601 &iocom->router.staterd_tree, &dummy);
1604 pthread_mutex_unlock(&iocom->mtx);
1607 * Short-cut one-off or mid-stream messages (state may be NULL).
1609 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1610 HAMMER2_MSGF_ABORT)) == 0) {
1615 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1616 * inside the case statements.
1618 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1619 HAMMER2_MSGF_REPLY)) {
1620 case HAMMER2_MSGF_CREATE:
1621 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1623 * New persistant command received.
1626 fprintf(stderr, "duplicate-trans %s\n",
1627 hammer2_msg_str(msg));
1628 error = HAMMER2_IOQ_ERROR_TRANS;
1632 state = malloc(sizeof(*state));
1633 bzero(state, sizeof(*state));
1634 state->iocom = iocom;
1635 state->flags = HAMMER2_STATE_DYNAMIC;
1637 state->txcmd = HAMMER2_MSGF_REPLY;
1638 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1639 state->flags |= HAMMER2_STATE_INSERTED;
1640 state->msgid = msg->any.head.msgid;
1641 state->spanid = msg->any.head.spanid;
1643 pthread_mutex_lock(&iocom->mtx);
1644 RB_INSERT(hammer2_state_tree,
1645 &iocom->router.staterd_tree, state);
1646 pthread_mutex_unlock(&iocom->mtx);
1649 fprintf(stderr, "create state %p id=%08x on iocom staterd %p\n",
1650 state, (uint32_t)state->msgid, iocom);
1653 case HAMMER2_MSGF_DELETE:
1655 * Persistent state is expected but might not exist if an
1656 * ABORT+DELETE races the close.
1658 if (state == NULL) {
1659 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1660 error = HAMMER2_IOQ_ERROR_EALREADY;
1662 fprintf(stderr, "missing-state %s\n",
1663 hammer2_msg_str(msg));
1664 error = HAMMER2_IOQ_ERROR_TRANS;
1671 * Handle another ABORT+DELETE case if the msgid has already
1674 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1675 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1676 error = HAMMER2_IOQ_ERROR_EALREADY;
1678 fprintf(stderr, "reused-state %s\n",
1679 hammer2_msg_str(msg));
1680 error = HAMMER2_IOQ_ERROR_TRANS;
1689 * Check for mid-stream ABORT command received, otherwise
1692 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1693 if (state == NULL ||
1694 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1695 error = HAMMER2_IOQ_ERROR_EALREADY;
1701 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
1702 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1704 * When receiving a reply with CREATE set the original
1705 * persistent state message should already exist.
1707 if (state == NULL) {
1708 fprintf(stderr, "no-state(r) %s\n",
1709 hammer2_msg_str(msg));
1710 error = HAMMER2_IOQ_ERROR_TRANS;
1714 assert(((state->rxcmd ^ msg->any.head.cmd) &
1715 HAMMER2_MSGF_REPLY) == 0);
1716 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1719 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1721 * Received REPLY+ABORT+DELETE in case where msgid has
1722 * already been fully closed, ignore the message.
1724 if (state == NULL) {
1725 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1726 error = HAMMER2_IOQ_ERROR_EALREADY;
1728 fprintf(stderr, "no-state(r,d) %s\n",
1729 hammer2_msg_str(msg));
1730 error = HAMMER2_IOQ_ERROR_TRANS;
1737 * Received REPLY+ABORT+DELETE in case where msgid has
1738 * already been reused for an unrelated message,
1739 * ignore the message.
1741 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1742 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1743 error = HAMMER2_IOQ_ERROR_EALREADY;
1745 fprintf(stderr, "reused-state(r,d) %s\n",
1746 hammer2_msg_str(msg));
1747 error = HAMMER2_IOQ_ERROR_TRANS;
1754 case HAMMER2_MSGF_REPLY:
1756 * Check for mid-stream ABORT reply received to sent command.
1758 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1759 if (state == NULL ||
1760 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1761 error = HAMMER2_IOQ_ERROR_EALREADY;
1772 hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1774 hammer2_state_t *state;
1776 if ((state = msg->state) == NULL) {
1778 * Free a non-transactional message, there is no state
1781 hammer2_msg_free(msg);
1782 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1784 * Message terminating transaction, destroy the related
1785 * state, the original message, and this message (if it
1786 * isn't the original message due to a CREATE|DELETE).
1788 pthread_mutex_lock(&iocom->mtx);
1789 state->rxcmd |= HAMMER2_MSGF_DELETE;
1790 if (state->txcmd & HAMMER2_MSGF_DELETE) {
1791 if (state->msg == msg)
1793 assert(state->flags & HAMMER2_STATE_INSERTED);
1794 if (state->rxcmd & HAMMER2_MSGF_REPLY) {
1795 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1796 RB_REMOVE(hammer2_state_tree,
1797 &iocom->router.statewr_tree, state);
1799 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1800 RB_REMOVE(hammer2_state_tree,
1801 &iocom->router.staterd_tree, state);
1803 state->flags &= ~HAMMER2_STATE_INSERTED;
1804 hammer2_state_free(state);
1808 pthread_mutex_unlock(&iocom->mtx);
1809 hammer2_msg_free(msg);
1810 } else if (state->msg != msg) {
1812 * Message not terminating transaction, leave state intact
1813 * and free message if it isn't the CREATE message.
1815 hammer2_msg_free(msg);
1820 hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1822 hammer2_state_t *state;
1824 if ((state = msg->state) == NULL) {
1825 hammer2_msg_free(msg);
1826 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1827 pthread_mutex_lock(&iocom->mtx);
1828 state->txcmd |= HAMMER2_MSGF_DELETE;
1829 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
1830 if (state->msg == msg)
1832 assert(state->flags & HAMMER2_STATE_INSERTED);
1833 if (state->txcmd & HAMMER2_MSGF_REPLY) {
1834 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
1835 RB_REMOVE(hammer2_state_tree,
1836 &iocom->router.staterd_tree, state);
1838 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1839 RB_REMOVE(hammer2_state_tree,
1840 &iocom->router.statewr_tree, state);
1842 state->flags &= ~HAMMER2_STATE_INSERTED;
1843 hammer2_state_free(state);
1847 pthread_mutex_unlock(&iocom->mtx);
1848 hammer2_msg_free(msg);
1849 } else if (state->msg != msg) {
1850 hammer2_msg_free(msg);
1855 * Called with iocom locked
1858 hammer2_state_free(hammer2_state_t *state)
1860 hammer2_iocom_t *iocom = state->iocom;
1865 fprintf(stderr, "terminate state %p id=%08x\n",
1866 state, (uint32_t)state->msgid);
1868 assert(state->any.any == NULL);
1872 hammer2_msg_free_locked(msg);
1876 * When an iocom error is present we are trying to close down the
1877 * iocom, but we have to wait for all states to terminate before
1878 * we can do so. The iocom rx code will terminate the receive side
1879 * for all transactions by simulating incoming DELETE messages,
1880 * but the state doesn't go away until both sides are terminated.
1882 * We may have to wake up the rx code.
1884 if (iocom->ioq_rx.error &&
1885 RB_EMPTY(&iocom->router.staterd_tree) &&
1886 RB_EMPTY(&iocom->router.statewr_tree)) {
1888 write(iocom->wakeupfds[1], &dummy, 1);
1893 hammer2_basecmd_str(uint32_t cmd)
1895 static char buf[64];
1898 const char *protostr;
1901 switch(cmd & HAMMER2_MSGF_PROTOS) {
1902 case HAMMER2_MSG_PROTO_LNK:
1905 case HAMMER2_MSG_PROTO_DBG:
1908 case HAMMER2_MSG_PROTO_DOM:
1911 case HAMMER2_MSG_PROTO_CAC:
1914 case HAMMER2_MSG_PROTO_QRM:
1917 case HAMMER2_MSG_PROTO_BLK:
1920 case HAMMER2_MSG_PROTO_VOP:
1924 snprintf(protobuf, sizeof(protobuf), "%x_",
1925 (cmd & HAMMER2_MSGF_PROTOS) >> 20);
1926 protostr = protobuf;
1930 switch(cmd & (HAMMER2_MSGF_PROTOS |
1932 HAMMER2_MSGF_SIZE)) {
1933 case HAMMER2_LNK_PAD:
1936 case HAMMER2_LNK_PING:
1939 case HAMMER2_LNK_AUTH:
1942 case HAMMER2_LNK_CONN:
1945 case HAMMER2_LNK_SPAN:
1948 case HAMMER2_LNK_ERROR:
1949 if (cmd & HAMMER2_MSGF_DELETE)
1954 case HAMMER2_DBG_SHELL:
1958 snprintf(cmdbuf, sizeof(cmdbuf),
1959 "%06x", (cmd & (HAMMER2_MSGF_PROTOS |
1961 HAMMER2_MSGF_SIZE)));
1965 snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
1970 hammer2_msg_str(hammer2_msg_t *msg)
1972 hammer2_state_t *state;
1973 static char buf[256];
1977 const char *statestr;
1985 if ((state = msg->state) != NULL) {
1986 basecmd = (state->rxcmd & HAMMER2_MSGF_REPLY) ?
1987 state->txcmd : state->rxcmd;
1988 snprintf(statebuf, sizeof(statebuf),
1989 " %s=%s,L=%s%s,R=%s%s",
1990 ((state->txcmd & HAMMER2_MSGF_REPLY) ?
1991 "rcvcmd" : "sndcmd"),
1992 hammer2_basecmd_str(basecmd),
1993 ((state->txcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1994 ((state->txcmd & HAMMER2_MSGF_DELETE) ? "D" : ""),
1995 ((state->rxcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1996 ((state->rxcmd & HAMMER2_MSGF_DELETE) ? "D" : "")
1998 statestr = statebuf;
2006 switch(msg->any.head.error) {
2010 case HAMMER2_IOQ_ERROR_SYNC:
2011 errstr = "err=IOQ:NOSYNC";
2013 case HAMMER2_IOQ_ERROR_EOF:
2014 errstr = "err=IOQ:STREAMEOF";
2016 case HAMMER2_IOQ_ERROR_SOCK:
2017 errstr = "err=IOQ:SOCKERR";
2019 case HAMMER2_IOQ_ERROR_FIELD:
2020 errstr = "err=IOQ:BADFIELD";
2022 case HAMMER2_IOQ_ERROR_HCRC:
2023 errstr = "err=IOQ:BADHCRC";
2025 case HAMMER2_IOQ_ERROR_XCRC:
2026 errstr = "err=IOQ:BADXCRC";
2028 case HAMMER2_IOQ_ERROR_ACRC:
2029 errstr = "err=IOQ:BADACRC";
2031 case HAMMER2_IOQ_ERROR_STATE:
2032 errstr = "err=IOQ:BADSTATE";
2034 case HAMMER2_IOQ_ERROR_NOPEER:
2035 errstr = "err=IOQ:PEERCONFIG";
2037 case HAMMER2_IOQ_ERROR_NORKEY:
2038 errstr = "err=IOQ:BADRKEY";
2040 case HAMMER2_IOQ_ERROR_NOLKEY:
2041 errstr = "err=IOQ:BADLKEY";
2043 case HAMMER2_IOQ_ERROR_KEYXCHGFAIL:
2044 errstr = "err=IOQ:BADKEYXCHG";
2046 case HAMMER2_IOQ_ERROR_KEYFMT:
2047 errstr = "err=IOQ:BADFMT";
2049 case HAMMER2_IOQ_ERROR_BADURANDOM:
2050 errstr = "err=IOQ:BADRANDOM";
2052 case HAMMER2_IOQ_ERROR_MSGSEQ:
2053 errstr = "err=IOQ:BADSEQ";
2055 case HAMMER2_IOQ_ERROR_EALREADY:
2056 errstr = "err=IOQ:DUPMSG";
2058 case HAMMER2_IOQ_ERROR_TRANS:
2059 errstr = "err=IOQ:BADTRANS";
2061 case HAMMER2_MSG_ERR_NOSUPP:
2062 errstr = "err=NOSUPPORT";
2065 snprintf(errbuf, sizeof(errbuf),
2066 " err=%d", msg->any.head.error);
2075 if (msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
2076 HAMMER2_MSGF_ABORT | HAMMER2_MSGF_REPLY)) {
2078 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
2080 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
2082 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY)
2084 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT)
2092 snprintf(buf, sizeof(buf),
2093 "msg=%s%s %s id=%08x span=%08x %s",
2094 hammer2_basecmd_str(msg->any.head.cmd),
2097 (uint32_t)(intmax_t)msg->any.head.msgid, /* for brevity */
2098 (uint32_t)(intmax_t)msg->any.head.spanid, /* for brevity */