2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static int hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
39 static void hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
42 * Initialize a low-level ioq
45 hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
47 bzero(ioq, sizeof(*ioq));
48 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
49 TAILQ_INIT(&ioq->msgq);
53 hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
57 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
58 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
59 hammer2_msg_free(iocom, msg);
61 if ((msg = ioq->msg) != NULL) {
63 hammer2_msg_free(iocom, msg);
68 * Initialize a low-level communications channel
71 hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd)
73 bzero(iocom, sizeof(*iocom));
75 RB_INIT(&iocom->staterd_tree);
76 RB_INIT(&iocom->statewr_tree);
77 TAILQ_INIT(&iocom->freeq);
78 TAILQ_INIT(&iocom->freeq_aux);
79 TAILQ_INIT(&iocom->addrq);
80 iocom->sock_fd = sock_fd;
81 iocom->alt_fd = alt_fd;
82 iocom->flags = HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_WIDLE;
83 hammer2_ioq_init(iocom, &iocom->ioq_rx);
84 hammer2_ioq_init(iocom, &iocom->ioq_tx);
87 * Negotiate session crypto synchronously. This will mark the
88 * connection as error'd if it fails.
90 hammer2_crypto_negotiate(iocom);
93 * Make sure our fds are set to non-blocking for the iocom core.
96 fcntl(sock_fd, F_SETFL, O_NONBLOCK);
98 /* if line buffered our single fgets() should be fine */
100 fcntl(alt_fd, F_SETFL, O_NONBLOCK);
105 hammer2_iocom_done(hammer2_iocom_t *iocom)
110 hammer2_ioq_done(iocom, &iocom->ioq_rx);
111 hammer2_ioq_done(iocom, &iocom->ioq_tx);
112 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
113 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
116 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
117 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
119 msg->aux_data = NULL;
125 * Allocate a new one-way message.
128 hammer2_msg_alloc(hammer2_iocom_t *iocom, size_t aux_size, uint32_t cmd)
134 aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
135 ~HAMMER2_MSG_ALIGNMASK;
136 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
137 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
139 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
140 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
143 msg = malloc(sizeof(*msg));
144 bzero(msg, sizeof(*msg));
145 msg->aux_data = NULL;
148 if (msg->aux_size != aux_size) {
151 msg->aux_data = NULL;
155 msg->aux_data = malloc(aux_size);
156 msg->aux_size = aux_size;
159 hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
161 bzero(&msg->any.head, hbytes);
162 msg->hdr_size = hbytes;
163 msg->any.head.cmd = cmd;
164 msg->any.head.aux_descr = 0;
165 msg->any.head.aux_crc = 0;
171 * Free a message so it can be reused afresh.
173 * NOTE: aux_size can be 0 with a non-NULL aux_data.
176 hammer2_msg_free(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
179 TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
181 TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
185 * I/O core loop for an iocom.
188 hammer2_iocom_core(hammer2_iocom_t *iocom,
189 void (*recvmsg_func)(hammer2_iocom_t *),
190 void (*sendmsg_func)(hammer2_iocom_t *),
191 void (*altmsg_func)(hammer2_iocom_t *))
193 struct pollfd fds[2];
196 iocom->recvmsg_callback = recvmsg_func;
197 iocom->sendmsg_callback = sendmsg_func;
198 iocom->altmsg_callback = altmsg_func;
200 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
203 fds[0].fd = iocom->sock_fd;
207 if (iocom->flags & HAMMER2_IOCOMF_RREQ)
208 fds[0].events |= POLLIN;
211 if ((iocom->flags & HAMMER2_IOCOMF_WIDLE) == 0) {
212 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
213 fds[0].events |= POLLOUT;
218 if (iocom->alt_fd >= 0) {
219 fds[1].fd = iocom->alt_fd;
220 fds[1].events |= POLLIN;
222 poll(fds, 2, timeout);
224 poll(fds, 1, timeout);
226 if ((fds[0].revents & POLLIN) ||
227 (iocom->flags & HAMMER2_IOCOMF_RREQ) == 0) {
228 iocom->recvmsg_callback(iocom);
230 if ((iocom->flags & HAMMER2_IOCOMF_WIDLE) == 0) {
231 if ((fds[0].revents & POLLOUT) ||
232 (iocom->flags & HAMMER2_IOCOMF_WREQ) == 0) {
233 iocom->sendmsg_callback(iocom);
236 if (iocom->alt_fd >= 0 && (fds[1].revents & POLLIN))
237 iocom->altmsg_callback(iocom);
242 * Read the next ready message from the ioq, issuing I/O if needed.
243 * Caller should retry on a read-event when NULL is returned.
245 * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
246 * be returned for each open transaction, then the ioq and iocom
247 * will be errored out and a non-transactional HAMMER2_LNK_ERROR
248 * msg will be returned as the final message. The caller should not call
249 * us again after the final message is returned.
252 hammer2_ioq_read(hammer2_iocom_t *iocom)
254 hammer2_ioq_t *ioq = &iocom->ioq_rx;
256 hammer2_msg_hdr_t *head;
257 hammer2_state_t *state;
266 * If a message is already pending we can just remove and
267 * return it. Message state has already been processed.
269 if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
270 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
275 * Message read in-progress (msg is NULL at the moment). We don't
276 * allocate a msg until we have its core header.
278 bytes = ioq->fifo_end - ioq->fifo_beg;
279 nmax = sizeof(ioq->buf) - ioq->fifo_end;
283 case HAMMER2_MSGQ_STATE_HEADER1:
285 * Load the primary header, fail on any non-trivial read
286 * error or on EOF. Since the primary header is the same
287 * size is the message alignment it will never straddle
288 * the end of the buffer.
290 if (bytes < (int)sizeof(msg->any.head)) {
291 n = read(iocom->sock_fd,
292 ioq->buf + ioq->fifo_end,
296 ioq->error = HAMMER2_IOQ_ERROR_EOF;
299 if (errno != EINTR &&
300 errno != EINPROGRESS &&
302 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
314 * Insufficient data accumulated (msg is NULL, caller will
318 if (bytes < (int)sizeof(msg->any.head))
322 * Calculate the header, decrypt data received so far.
323 * Data will be decrypted in-place. Partial blocks are
324 * not immediately decrypted.
326 * WARNING! The header might be in the wrong endian, we
327 * do not fix it up until we get the entire
330 hammer2_crypto_decrypt(iocom, ioq);
331 head = (void *)(ioq->buf + ioq->fifo_beg);
334 * Check and fixup the core header. Note that the icrc
335 * has to be calculated before any fixups, but the crc
336 * fields in the msg may have to be swapped like everything
339 if (head->magic != HAMMER2_MSGHDR_MAGIC &&
340 head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
341 ioq->error = HAMMER2_IOQ_ERROR_SYNC;
346 * Calculate the full header size and aux data size
348 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
349 ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
351 ioq->abytes = bswap32(head->aux_bytes) *
354 ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
356 ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
358 if (ioq->hbytes < sizeof(msg->any.head) ||
359 ioq->hbytes > sizeof(msg->any) ||
360 ioq->abytes > HAMMER2_MSGAUX_MAX) {
361 ioq->error = HAMMER2_IOQ_ERROR_FIELD;
366 * Finally allocate the message and copy the core header
367 * to the embedded extended header.
369 * Initialize msg->aux_size to 0 and use it to track
370 * the amount of data copied from the stream.
372 msg = hammer2_msg_alloc(iocom, ioq->abytes, 0);
376 * We are either done or we fall-through
378 if (ioq->hbytes == sizeof(msg->any.head) && ioq->abytes == 0) {
379 bcopy(head, &msg->any.head, sizeof(msg->any.head));
380 ioq->fifo_beg += ioq->hbytes;
385 * Fall through to the next state. Make sure that the
386 * extended header does not straddle the end of the buffer.
387 * We still want to issue larger reads into our buffer,
388 * book-keeping is easier if we don't bcopy() yet.
390 if (bytes + nmax < ioq->hbytes) {
391 bcopy(ioq->buf + ioq->fifo_beg, ioq->buf, bytes);
392 ioq->fifo_cdx -= ioq->fifo_beg;
394 ioq->fifo_end = bytes;
395 nmax = sizeof(ioq->buf) - ioq->fifo_end;
397 ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
399 case HAMMER2_MSGQ_STATE_HEADER2:
401 * Fill out the extended header.
404 if (bytes < ioq->hbytes) {
405 n = read(iocom->sock_fd,
406 msg->any.buf + ioq->fifo_end,
410 ioq->error = HAMMER2_IOQ_ERROR_EOF;
413 if (errno != EINTR &&
414 errno != EINPROGRESS &&
416 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
428 * Insufficient data accumulated (set msg NULL so caller will
431 if (bytes < ioq->hbytes) {
437 * Calculate the extended header, decrypt data received
438 * so far. Handle endian-conversion for the entire extended
441 hammer2_crypto_decrypt(iocom, ioq);
442 head = (void *)(ioq->buf + ioq->fifo_beg);
447 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
448 xcrc32 = bswap32(head->hdr_crc);
450 xcrc32 = head->hdr_crc;
452 if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
453 ioq->error = HAMMER2_IOQ_ERROR_XCRC;
456 head->hdr_crc = xcrc32;
458 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
459 hammer2_bswap_head(head);
463 * Copy the extended header into the msg and adjust the
466 bcopy(head, &msg->any, ioq->hbytes);
469 * We are either done or we fall-through.
471 if (ioq->abytes == 0) {
472 ioq->fifo_beg += ioq->hbytes;
477 * Must adjust nmax and bytes (and the state) when falling
480 ioq->fifo_beg += ioq->hbytes;
482 bytes -= ioq->hbytes;
483 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
485 case HAMMER2_MSGQ_STATE_AUXDATA1:
487 * Copy the partial or complete payload from remaining
488 * bytes in the FIFO. We have to fall-through either
489 * way so we can check the crc.
491 * Adjust msg->aux_size to the final actual value.
493 ioq->already = ioq->fifo_cdx - ioq->fifo_beg;
494 if (ioq->already > ioq->abytes)
495 ioq->already = ioq->abytes;
496 if (bytes >= ioq->abytes) {
497 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
499 msg->aux_size = ioq->abytes;
500 ioq->fifo_beg += ioq->abytes;
501 if (ioq->fifo_cdx < ioq->fifo_beg)
502 ioq->fifo_cdx = ioq->fifo_beg;
503 bytes -= ioq->abytes;
505 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
507 msg->aux_size = bytes;
508 ioq->fifo_beg += bytes;
509 if (ioq->fifo_cdx < ioq->fifo_beg)
510 ioq->fifo_cdx = ioq->fifo_beg;
515 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
517 case HAMMER2_MSGQ_STATE_AUXDATA2:
519 * Read the remainder of the payload directly into the
520 * msg->aux_data buffer.
523 if (msg->aux_size < ioq->abytes) {
525 n = read(iocom->sock_fd,
526 msg->aux_data + msg->aux_size,
527 ioq->abytes - msg->aux_size);
530 ioq->error = HAMMER2_IOQ_ERROR_EOF;
533 if (errno != EINTR &&
534 errno != EINPROGRESS &&
536 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
546 * Insufficient data accumulated (set msg NULL so caller will
549 if (msg->aux_size < ioq->abytes) {
553 assert(msg->aux_size == ioq->abytes);
554 hammer2_crypto_decrypt_aux(iocom, ioq, msg, ioq->already);
557 * Check aux_crc, then we are done.
559 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
560 if (xcrc32 != msg->any.head.aux_crc) {
561 ioq->error = HAMMER2_IOQ_ERROR_ACRC;
565 case HAMMER2_MSGQ_STATE_ERROR:
567 * Continued calls to drain recorded transactions (returning
568 * a LNK_ERROR for each one), before we return the final
575 * We don't double-return errors, the caller should not
576 * have called us again after getting an error msg.
583 * Check the message sequence. The iv[] should prevent any
584 * possibility of a replay but we add this check anyway.
586 if (msg && ioq->error == 0) {
587 if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
588 ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
595 * Process transactional state for the message.
597 if (msg && ioq->error == 0) {
598 error = hammer2_state_msgrx(iocom, msg);
600 if (error == HAMMER2_IOQ_ERROR_EALREADY) {
601 hammer2_msg_free(iocom, msg);
609 * Handle error, RREQ, or completion
611 * NOTE: nmax and bytes are invalid at this point, we don't bother
612 * to update them when breaking out.
616 * An unrecoverable error causes all active receive
617 * transactions to be terminated with a LNK_ERROR message.
619 * Once all active transactions are exhausted we set the
620 * iocom ERROR flag and return a non-transactional LNK_ERROR
621 * message, which should cause master processing loops to
624 assert(ioq->msg == msg);
626 hammer2_msg_free(iocom, msg);
631 * No more I/O read processing
633 ioq->state = HAMMER2_MSGQ_STATE_ERROR;
636 * Return LNK_ERROR for any open transaction, and finally
637 * as a non-transactional message when no transactions are
640 msg = hammer2_msg_alloc(iocom, 0, 0);
641 bzero(&msg->any.head, sizeof(msg->any.head));
642 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
643 msg->any.head.cmd = HAMMER2_LNK_ERROR;
644 msg->any.head.error = ioq->error;
646 if ((state = RB_ROOT(&iocom->staterd_tree)) != NULL) {
648 * Active transactions are still present. Simulate
649 * the other end sending us a DELETE.
651 state->txcmd |= HAMMER2_MSGF_DELETE;
653 msg->any.head.spanid = state->spanid;
654 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
658 * No active transactions remain
661 iocom->flags |= HAMMER2_IOCOMF_EOF;
663 } else if (msg == NULL) {
665 * Insufficient data received to finish building the message,
666 * set RREQ and return NULL.
668 * Leave ioq->msg intact.
669 * Leave the FIFO intact.
671 iocom->flags |= HAMMER2_IOCOMF_RREQ;
679 * Return msg, clear the FIFO if it is now empty.
680 * Flag RREQ if the caller needs to wait for a read-event
683 * The fifo has already been advanced past the message.
684 * Trivially reset the FIFO indices if possible.
686 if (ioq->fifo_beg == ioq->fifo_end) {
687 iocom->flags |= HAMMER2_IOCOMF_RREQ;
692 iocom->flags &= ~HAMMER2_IOCOMF_RREQ;
694 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
701 * Calculate the header and data crc's and write a low-level message to
702 * the connection. If aux_crc is non-zero the aux_data crc is already
703 * assumed to have been set.
705 * A non-NULL msg is added to the queue but not necessarily flushed.
706 * Calling this function with msg == NULL will get a flush going.
709 hammer2_ioq_write(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
711 hammer2_ioq_t *ioq = &iocom->ioq_tx;
718 * Process terminal connection errors.
721 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
723 hammer2_iocom_drain(iocom);
728 * Finish populating the msg fields. The salt ensures that the iv[]
729 * array is ridiculously randomized and we also re-seed our PRNG
730 * every 32768 messages just to be sure.
732 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
733 msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
735 if ((ioq->seq & 32767) == 0)
739 * Calculate aux_crc if 0, then calculate hdr_crc.
741 if (msg->aux_size && msg->any.head.aux_crc == 0) {
742 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
743 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
744 msg->any.head.aux_crc = xcrc32;
746 msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
747 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
749 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
750 msg->any.head.hdr_crc = 0;
751 msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
754 * Enqueue the message (the flush codes handles stream encryption).
756 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
758 iocom->flags &= ~HAMMER2_IOCOMF_WIDLE;
761 * Flush if we know we can write (WREQ not set) and if
762 * sufficient messages have accumulated. Otherwise hold
763 * off to avoid piecemeal system calls.
765 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
767 if (ioq->msgcount < HAMMER2_IOQ_MAXIOVEC / 2)
769 hammer2_iocom_flush(iocom);
773 hammer2_iocom_flush(hammer2_iocom_t *iocom)
775 hammer2_ioq_t *ioq = &iocom->ioq_tx;
779 struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
787 * Pump messages out the connection by building an iovec.
792 TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
794 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
797 abytes = msg->aux_size;
802 if (hbytes - hoff > 0) {
803 iov[n].iov_base = (char *)&msg->any.head + hoff;
804 iov[n].iov_len = hbytes - hoff;
805 nmax += hbytes - hoff;
807 if (n == HAMMER2_IOQ_MAXIOVEC)
810 if (abytes - aoff > 0) {
811 assert(msg->aux_data != NULL);
812 iov[n].iov_base = msg->aux_data + aoff;
813 iov[n].iov_len = abytes - aoff;
814 nmax += abytes - aoff;
816 if (n == HAMMER2_IOQ_MAXIOVEC)
824 * Encrypt and write the data. The crypto code will move the
825 * data into the fifo and adjust the iov as necessary. If
826 * encryption is disabled the iov is left alone.
828 * hammer2_crypto_encrypt_wrote()
830 n = hammer2_crypto_encrypt(iocom, ioq, iov, n);
833 * Execute the writev() then figure out what happened.
835 nact = writev(iocom->sock_fd, iov, n);
837 if (errno != EINTR &&
838 errno != EINPROGRESS &&
840 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
841 hammer2_iocom_drain(iocom);
843 iocom->flags |= HAMMER2_IOCOMF_WREQ;
847 hammer2_crypto_encrypt_wrote(iocom, ioq, nact);
849 iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
851 iocom->flags |= HAMMER2_IOCOMF_WREQ;
853 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
854 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
856 abytes = msg->aux_size;
858 if ((size_t)nact < hbytes - ioq->hbytes) {
862 nact -= hbytes - ioq->hbytes;
863 ioq->hbytes = hbytes;
864 if ((size_t)nact < abytes - ioq->abytes) {
868 nact -= abytes - ioq->abytes;
870 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
875 hammer2_state_cleanuptx(iocom, msg);
878 iocom->flags |= HAMMER2_IOCOMF_WIDLE;
879 iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
882 iocom->flags |= HAMMER2_IOCOMF_EOF |
883 HAMMER2_IOCOMF_WIDLE;
884 iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
889 * Kill pending msgs on ioq_tx and adjust the flags such that no more
890 * write events will occur. We don't kill read msgs because we want
891 * the caller to pull off our contrived terminal error msg to detect
892 * the connection failure.
895 hammer2_iocom_drain(hammer2_iocom_t *iocom)
897 hammer2_ioq_t *ioq = &iocom->ioq_tx;
900 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
901 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
903 hammer2_msg_free(iocom, msg);
905 iocom->flags |= HAMMER2_IOCOMF_WIDLE;
906 iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
910 * Write a message to an iocom, with additional state processing.
912 * The iocom lock must be held by the caller. XXX
915 hammer2_msg_write(hammer2_iocom_t *iocom, hammer2_msg_t *msg,
916 void (*func)(hammer2_state_t *, hammer2_msg_t *),
919 hammer2_state_t *state;
922 * Handle state processing, create state if necessary.
924 if ((state = msg->state) != NULL) {
926 * Existing transaction (could be reply). It is also
927 * possible for this to be the first reply (CREATE is set),
928 * in which case we populate state->txcmd.
930 msg->any.head.msgid = state->msgid;
931 msg->any.head.spanid = state->spanid;
934 state->any.any = data;
936 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
937 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
938 fprintf(stderr, "MSGWRITE IN REPLY msgid %016jx\n",
939 (intmax_t)msg->any.head.msgid);
940 } else if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
941 fprintf(stderr, "MSGWRITE NEW MSG\n");
943 * No existing state and CREATE is set, create new
944 * state for outgoing command. This can't happen if
945 * REPLY is set as the state would already exist for
946 * a transaction reply.
948 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
950 state = malloc(sizeof(*state));
951 bzero(state, sizeof(*state));
952 state->iocom = iocom;
953 state->flags = HAMMER2_STATE_DYNAMIC;
955 state->msgid = (uint64_t)(uintptr_t)state;
956 state->spanid = msg->any.head.spanid;
957 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
959 state->any.any = data;
960 RB_INSERT(hammer2_state_tree, &iocom->statewr_tree, state);
961 state->flags |= HAMMER2_STATE_INSERTED;
963 msg->any.head.msgid = state->msgid;
964 /* spanid set by caller */
966 fprintf(stderr, "MSGWRITE ONE-OFF\n");
967 msg->any.head.msgid = 0;
968 /* spanid set by caller */
972 * Queue it for output
974 hammer2_ioq_write(iocom, msg);
979 case HAMMER2_MSGF_DELETE:
981 * Sent ABORT+DELETE in case where msgid has already
982 * been fully closed, ignore the message.
985 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
986 error = HAMMER2_IOQ_ERROR_EALREADY;
988 iocom_printf(iocom, msg->any.head.cmd,
989 "hammer2_state_msgtx: "
990 "no state match for DELETE\n");
991 error = HAMMER2_IOQ_ERROR_TRANS;
997 * Sent ABORT+DELETE in case where msgid has
998 * already been reused for an unrelated message,
999 * ignore the message.
1001 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
1002 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1003 error = HAMMER2_IOQ_ERROR_EALREADY;
1005 iocom_printf(iocom, msg->any.head.cmd,
1006 "hammer2_state_msgtx: "
1007 "state reused for DELETE\n");
1008 error = HAMMER2_IOQ_ERROR_TRANS;
1015 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1017 * When transmitting a reply with DELETE set the original
1018 * persistent state message should already exist.
1020 * This is very similar to the REPLY|CREATE|* case except
1021 * txcmd is already stored, so we just add the DELETE flag.
1023 * Sent REPLY+ABORT+DELETE in case where msgid has
1024 * already been fully closed, ignore the message.
1026 if (state == NULL) {
1027 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1028 error = HAMMER2_IOQ_ERROR_EALREADY;
1030 iocom_printf(iocom, msg->any.head.cmd,
1031 "hammer2_state_msgtx: "
1032 "no state match for "
1033 "REPLY | DELETE\n");
1034 error = HAMMER2_IOQ_ERROR_TRANS;
1040 * Sent REPLY+ABORT+DELETE in case where msgid has already
1041 * been reused for an unrelated message, ignore the message.
1043 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
1044 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1045 error = HAMMER2_IOQ_ERROR_EALREADY;
1047 iocom_printf(iocom, msg->any.head.cmd,
1048 "hammer2_state_msgtx: "
1050 "REPLY | DELETE\n");
1051 error = HAMMER2_IOQ_ERROR_TRANS;
1057 case HAMMER2_MSGF_REPLY:
1059 * Check for mid-stream ABORT reply sent.
1061 * One-off REPLY messages are allowed for e.g. status updates.
1063 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1064 if (state == NULL ||
1065 (state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
1066 error = HAMMER2_IOQ_ERROR_EALREADY;
1073 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1079 * This is a shortcut to formulate a reply to msg with a simple error code,
1080 * It can reply to and terminate a transaction, or it can reply to a one-way
1081 * messages. A HAMMER2_LNK_ERROR command code is utilized to encode
1082 * the error code (which can be 0). Not all transactions are terminated
1083 * with HAMMER2_LNK_ERROR status (the low level only cares about the
1084 * MSGF_DELETE flag), but most are.
1086 * Replies to one-way messages are a bit of an oxymoron but the feature
1087 * is used by the debug (DBG) protocol.
1089 * The reply contains no extended data.
1092 hammer2_msg_reply(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1094 hammer2_state_t *state = msg->state;
1095 hammer2_msg_t *nmsg;
1100 * Reply with a simple error code and terminate the transaction.
1102 cmd = HAMMER2_LNK_ERROR;
1105 * Check if our direction has even been initiated yet, set CREATE.
1107 * Check what direction this is (command or reply direction). Note
1108 * that txcmd might not have been initiated yet.
1110 * If our direction has already been closed we just return without
1114 if (state->txcmd & HAMMER2_MSGF_DELETE)
1116 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1117 cmd |= HAMMER2_MSGF_CREATE;
1118 if ((state->rxcmd & HAMMER2_MSGF_REPLY) == 0)
1119 cmd |= HAMMER2_MSGF_REPLY;
1120 cmd |= HAMMER2_MSGF_DELETE;
1122 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1123 cmd |= HAMMER2_MSGF_REPLY;
1126 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1127 nmsg->any.head.error = error;
1128 nmsg->state = msg->state;
1129 hammer2_msg_write(iocom, nmsg, NULL, 0);
1133 * Similar to hammer2_msg_reply() but leave the transaction open. That is,
1134 * we are generating a streaming reply or an intermediate acknowledgement
1135 * of some sort as part of the higher level protocol, with more to come
1139 hammer2_msg_result(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1141 hammer2_state_t *state = msg->state;
1142 hammer2_msg_t *nmsg;
1147 * Reply with a simple error code and terminate the transaction.
1149 cmd = HAMMER2_LNK_ERROR;
1152 * Check if our direction has even been initiated yet, set CREATE.
1154 * Check what direction this is (command or reply direction). Note
1155 * that txcmd might not have been initiated yet.
1157 * If our direction has already been closed we just return without
1161 if (state->txcmd & HAMMER2_MSGF_DELETE)
1163 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1164 cmd |= HAMMER2_MSGF_CREATE;
1165 if ((state->rxcmd & HAMMER2_MSGF_REPLY) == 0)
1166 cmd |= HAMMER2_MSGF_REPLY;
1167 /* continuing transaction, do not set MSGF_DELETE */
1169 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1170 cmd |= HAMMER2_MSGF_REPLY;
1173 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1174 nmsg->any.head.error = error;
1175 nmsg->state = state;
1176 hammer2_msg_write(iocom, nmsg, NULL, 0);
1180 * Terminate a transaction given a state structure by issuing a DELETE.
1183 hammer2_state_reply(hammer2_state_t *state, uint32_t error)
1185 hammer2_msg_t *nmsg;
1186 uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
1189 * Nothing to do if we already transmitted a delete
1191 if (state->txcmd & HAMMER2_MSGF_DELETE)
1195 * We must also set CREATE if this is our first response to a
1198 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1199 cmd |= HAMMER2_MSGF_CREATE;
1202 * Set REPLY if the other end initiated the command. Otherwise
1203 * we are the command direction.
1205 if ((state->rxcmd & HAMMER2_MSGF_REPLY) == 0)
1206 cmd |= HAMMER2_MSGF_REPLY;
1208 nmsg = hammer2_msg_alloc(state->iocom, 0, cmd);
1209 nmsg->any.head.error = error;
1210 nmsg->state = state;
1211 hammer2_msg_write(state->iocom, nmsg, NULL, 0);
1214 /************************************************************************
1215 * TRANSACTION STATE HANDLING *
1216 ************************************************************************
1220 RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
1223 * Process state tracking for a message after reception, prior to
1226 * Called with msglk held and the msg dequeued.
1228 * All messages are called with dummy state and return actual state.
1229 * (One-off messages often just return the same dummy state).
1231 * May request that caller discard the message by setting *discardp to 1.
1232 * The returned state is not used in this case and is allowed to be NULL.
1236 * These routines handle persistent and command/reply message state via the
1237 * CREATE and DELETE flags. The first message in a command or reply sequence
1238 * sets CREATE, the last message in a command or reply sequence sets DELETE.
1240 * There can be any number of intermediate messages belonging to the same
1241 * sequence sent inbetween the CREATE message and the DELETE message,
1242 * which set neither flag. This represents a streaming command or reply.
1244 * Any command message received with CREATE set expects a reply sequence to
1245 * be returned. Reply sequences work the same as command sequences except the
1246 * REPLY bit is also sent. Both the command side and reply side can
1247 * degenerate into a single message with both CREATE and DELETE set. Note
1248 * that one side can be streaming and the other side not, or neither, or both.
1250 * The msgid is unique for the initiator. That is, two sides sending a new
1251 * message can use the same msgid without colliding.
1255 * ABORT sequences work by setting the ABORT flag along with normal message
1256 * state. However, ABORTs can also be sent on half-closed messages, that is
1257 * even if the command or reply side has already sent a DELETE, as long as
1258 * the message has not been fully closed it can still send an ABORT+DELETE
1259 * to terminate the half-closed message state.
1261 * Since ABORT+DELETEs can race we silently discard ABORT's for message
1262 * state which has already been fully closed. REPLY+ABORT+DELETEs can
1263 * also race, and in this situation the other side might have already
1264 * initiated a new unrelated command with the same message id. Since
1265 * the abort has not set the CREATE flag the situation can be detected
1266 * and the message will also be discarded.
1268 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
1269 * The ABORT request is essentially integrated into the command instead
1270 * of being sent later on. In this situation the command implementation
1271 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
1272 * special-case non-blocking operation for the command.
1274 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
1275 * to be mid-stream aborts for command/reply sequences. ABORTs on
1276 * one-way messages are not supported.
1278 * NOTE! If a command sequence does not support aborts the ABORT flag is
1283 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
1284 * set. One-off messages cannot be aborted and typically aren't processed
1285 * by these routines. The REPLY bit can be used to distinguish whether a
1286 * one-off message is a command or reply. For example, one-off replies
1287 * will typically just contain status updates.
1290 hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1292 hammer2_state_t *state;
1293 hammer2_state_t dummy;
1297 * Lock RB tree and locate existing persistent state, if any.
1299 * If received msg is a command state is on staterd_tree.
1300 * If received msg is a reply state is on statewr_tree.
1302 /*lockmgr(&pmp->msglk, LK_EXCLUSIVE);*/
1304 dummy.msgid = msg->any.head.msgid;
1305 dummy.spanid = msg->any.head.spanid;
1307 iocom_printf(iocom, msg->any.head.cmd,
1308 "received msg %08x msgid %jx spanid=%jx\n",
1310 (intmax_t)msg->any.head.msgid,
1311 (intmax_t)msg->any.head.spanid);
1313 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1314 state = RB_FIND(hammer2_state_tree,
1315 &iocom->statewr_tree, &dummy);
1317 state = RB_FIND(hammer2_state_tree,
1318 &iocom->staterd_tree, &dummy);
1323 * Short-cut one-off or mid-stream messages (state may be NULL).
1325 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1326 HAMMER2_MSGF_ABORT)) == 0) {
1327 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1332 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1333 * inside the case statements.
1335 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1336 HAMMER2_MSGF_REPLY)) {
1337 case HAMMER2_MSGF_CREATE:
1338 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1340 * New persistant command received.
1343 iocom_printf(iocom, msg->any.head.cmd,
1344 "hammer2_state_msgrx: "
1345 "duplicate transaction\n");
1346 error = HAMMER2_IOQ_ERROR_TRANS;
1349 state = malloc(sizeof(*state));
1350 bzero(state, sizeof(*state));
1351 state->iocom = iocom;
1352 state->flags = HAMMER2_STATE_DYNAMIC;
1354 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1355 RB_INSERT(hammer2_state_tree, &iocom->staterd_tree, state);
1356 state->flags |= HAMMER2_STATE_INSERTED;
1357 state->msgid = msg->any.head.msgid;
1358 state->spanid = msg->any.head.spanid;
1362 case HAMMER2_MSGF_DELETE:
1364 * Persistent state is expected but might not exist if an
1365 * ABORT+DELETE races the close.
1367 if (state == NULL) {
1368 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1369 error = HAMMER2_IOQ_ERROR_EALREADY;
1371 iocom_printf(iocom, msg->any.head.cmd,
1372 "hammer2_state_msgrx: "
1373 "no state for DELETE\n");
1374 error = HAMMER2_IOQ_ERROR_TRANS;
1380 * Handle another ABORT+DELETE case if the msgid has already
1383 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1384 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1385 error = HAMMER2_IOQ_ERROR_EALREADY;
1387 iocom_printf(iocom, msg->any.head.cmd,
1388 "hammer2_state_msgrx: "
1389 "state reused for DELETE\n");
1390 error = HAMMER2_IOQ_ERROR_TRANS;
1398 * Check for mid-stream ABORT command received, otherwise
1401 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1402 if (state == NULL ||
1403 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1404 error = HAMMER2_IOQ_ERROR_EALREADY;
1410 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
1411 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1413 * When receiving a reply with CREATE set the original
1414 * persistent state message should already exist.
1416 if (state == NULL) {
1417 iocom_printf(iocom, msg->any.head.cmd,
1418 "hammer2_state_msgrx: "
1419 "no state match for REPLY cmd=%08x\n",
1421 error = HAMMER2_IOQ_ERROR_TRANS;
1424 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1427 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1429 * Received REPLY+ABORT+DELETE in case where msgid has
1430 * already been fully closed, ignore the message.
1432 if (state == NULL) {
1433 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1434 error = HAMMER2_IOQ_ERROR_EALREADY;
1436 iocom_printf(iocom, msg->any.head.cmd,
1437 "hammer2_state_msgrx: "
1438 "no state match for "
1440 error = HAMMER2_IOQ_ERROR_TRANS;
1446 * Received REPLY+ABORT+DELETE in case where msgid has
1447 * already been reused for an unrelated message,
1448 * ignore the message.
1450 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1451 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1452 error = HAMMER2_IOQ_ERROR_EALREADY;
1454 iocom_printf(iocom, msg->any.head.cmd,
1455 "hammer2_state_msgrx: "
1456 "state reused for REPLY|DELETE\n");
1457 error = HAMMER2_IOQ_ERROR_TRANS;
1463 case HAMMER2_MSGF_REPLY:
1465 * Check for mid-stream ABORT reply received to sent command.
1467 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1468 if (state == NULL ||
1469 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1470 error = HAMMER2_IOQ_ERROR_EALREADY;
1477 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1482 hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1484 hammer2_state_t *state;
1486 if ((state = msg->state) == NULL) {
1488 * Free a non-transactional message, there is no state
1491 hammer2_msg_free(iocom, msg);
1492 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1494 * Message terminating transaction, destroy the related
1495 * state, the original message, and this message (if it
1496 * isn't the original message due to a CREATE|DELETE).
1498 /*lockmgr(&pmp->msglk, LK_EXCLUSIVE);*/
1499 state->rxcmd |= HAMMER2_MSGF_DELETE;
1500 if (state->txcmd & HAMMER2_MSGF_DELETE) {
1501 if (state->msg == msg)
1503 assert(state->flags & HAMMER2_STATE_INSERTED);
1504 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1505 RB_REMOVE(hammer2_state_tree,
1506 &iocom->statewr_tree, state);
1508 RB_REMOVE(hammer2_state_tree,
1509 &iocom->staterd_tree, state);
1511 state->flags &= ~HAMMER2_STATE_INSERTED;
1512 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1513 hammer2_state_free(state);
1515 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1517 hammer2_msg_free(iocom, msg);
1518 } else if (state->msg != msg) {
1520 * Message not terminating transaction, leave state intact
1521 * and free message if it isn't the CREATE message.
1523 hammer2_msg_free(iocom, msg);
1528 hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1530 hammer2_state_t *state;
1532 if ((state = msg->state) == NULL) {
1533 hammer2_msg_free(iocom, msg);
1534 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1535 /*lockmgr(&pmp->msglk, LK_EXCLUSIVE);*/
1536 state->txcmd |= HAMMER2_MSGF_DELETE;
1537 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
1538 if (state->msg == msg)
1540 assert(state->flags & HAMMER2_STATE_INSERTED);
1541 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1542 RB_REMOVE(hammer2_state_tree,
1543 &iocom->staterd_tree, state);
1545 RB_REMOVE(hammer2_state_tree,
1546 &iocom->statewr_tree, state);
1548 state->flags &= ~HAMMER2_STATE_INSERTED;
1549 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1550 hammer2_state_free(state);
1552 /*lockmgr(&pmp->msglk, LK_RELEASE);*/
1554 hammer2_msg_free(iocom, msg);
1555 } else if (state->msg != msg) {
1556 hammer2_msg_free(iocom, msg);
1561 hammer2_state_free(hammer2_state_t *state)
1563 hammer2_iocom_t *iocom = state->iocom;
1569 hammer2_msg_free(iocom, msg);
1574 * Indexed messages are stored in a red-black tree indexed by their
1575 * msgid. Only persistent messages are indexed.
1578 hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
1580 if (state1->spanid < state2->spanid)
1582 if (state1->spanid > state2->spanid)
1584 if (state1->msgid < state2->msgid)
1586 if (state1->msgid > state2->msgid)