hammer2 - userland API / span work
[dragonfly.git] / sbin / hammer2 / msg.c
CommitLineData
9ab15106
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
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>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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
17 * distribution.
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.
21 *
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
33 * SUCH DAMAGE.
34 */
35
36#include "hammer2.h"
37
78476205 38static int hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
78476205
MD
39static void hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
40
9ab15106
MD
41/*
42 * Initialize a low-level ioq
43 */
44void
45hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
46{
47 bzero(ioq, sizeof(*ioq));
48 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
49 TAILQ_INIT(&ioq->msgq);
50}
51
7dc0f844
MD
52/*
53 * Cleanup queue.
54 *
55 * caller holds iocom->mtx.
56 */
9ab15106 57void
4a2e0eae 58hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
9ab15106
MD
59{
60 hammer2_msg_t *msg;
61
62 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
7dc0f844 63 assert(0); /* shouldn't happen */
78476205
MD
64 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
65 hammer2_msg_free(iocom, msg);
9ab15106
MD
66 }
67 if ((msg = ioq->msg) != NULL) {
68 ioq->msg = NULL;
78476205 69 hammer2_msg_free(iocom, msg);
9ab15106
MD
70 }
71}
72
73/*
5903497c
MD
74 * Initialize a low-level communications channel.
75 *
76 * NOTE: The state_func() is called at least once from the loop and can be
77 * re-armed via hammer2_iocom_restate().
9ab15106
MD
78 */
79void
5903497c
MD
80hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
81 void (*state_func)(hammer2_iocom_t *),
82 void (*rcvmsg_func)(hammer2_iocom_t *, hammer2_msg_t *msg),
83 void (*altmsg_func)(hammer2_iocom_t *))
9ab15106
MD
84{
85 bzero(iocom, sizeof(*iocom));
86
5903497c
MD
87 iocom->state_callback = state_func;
88 iocom->rcvmsg_callback = rcvmsg_func;
89 iocom->altmsg_callback = altmsg_func;
90
7dc0f844 91 pthread_mutex_init(&iocom->mtx, NULL);
78476205
MD
92 RB_INIT(&iocom->staterd_tree);
93 RB_INIT(&iocom->statewr_tree);
9ab15106
MD
94 TAILQ_INIT(&iocom->freeq);
95 TAILQ_INIT(&iocom->freeq_aux);
8c280d5d 96 TAILQ_INIT(&iocom->addrq);
7dc0f844 97 TAILQ_INIT(&iocom->txmsgq);
9ab15106
MD
98 iocom->sock_fd = sock_fd;
99 iocom->alt_fd = alt_fd;
7dc0f844 100 iocom->flags = HAMMER2_IOCOMF_RREQ;
5903497c
MD
101 if (state_func)
102 iocom->flags |= HAMMER2_IOCOMF_SWORK;
9ab15106
MD
103 hammer2_ioq_init(iocom, &iocom->ioq_rx);
104 hammer2_ioq_init(iocom, &iocom->ioq_tx);
7dc0f844
MD
105 if (pipe(iocom->wakeupfds) < 0)
106 assert(0);
107 fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
108 fcntl(iocom->wakeupfds[1], F_SETFL, O_NONBLOCK);
9ab15106 109
62efe6ec
MD
110 /*
111 * Negotiate session crypto synchronously. This will mark the
112 * connection as error'd if it fails.
113 */
114 hammer2_crypto_negotiate(iocom);
115
116 /*
117 * Make sure our fds are set to non-blocking for the iocom core.
118 */
9ab15106
MD
119 if (sock_fd >= 0)
120 fcntl(sock_fd, F_SETFL, O_NONBLOCK);
121#if 0
122 /* if line buffered our single fgets() should be fine */
123 if (alt_fd >= 0)
124 fcntl(alt_fd, F_SETFL, O_NONBLOCK);
125#endif
126}
127
7dc0f844 128/*
5903497c
MD
129 * May only be called from a callback from iocom_core.
130 *
131 * Adjust state machine functions, set flags to guarantee that both
132 * the recevmsg_func and the sendmsg_func is called at least once.
133 */
134void
135hammer2_iocom_restate(hammer2_iocom_t *iocom,
136 void (*state_func)(hammer2_iocom_t *),
137 void (*rcvmsg_func)(hammer2_iocom_t *, hammer2_msg_t *msg),
138 void (*altmsg_func)(hammer2_iocom_t *))
139{
140 iocom->state_callback = state_func;
141 iocom->rcvmsg_callback = rcvmsg_func;
142 iocom->altmsg_callback = altmsg_func;
143 if (state_func)
144 iocom->flags |= HAMMER2_IOCOMF_SWORK;
145 else
146 iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
147}
148
149/*
7dc0f844
MD
150 * Cleanup a terminating iocom.
151 *
152 * Caller should not hold iocom->mtx. The iocom has already been disconnected
153 * from all possible references to it.
154 */
9ab15106
MD
155void
156hammer2_iocom_done(hammer2_iocom_t *iocom)
157{
158 hammer2_msg_t *msg;
159
7dc0f844
MD
160 if (iocom->sock_fd >= 0) {
161 close(iocom->sock_fd);
162 iocom->sock_fd = -1;
163 }
164 if (iocom->alt_fd >= 0) {
165 close(iocom->alt_fd);
166 iocom->alt_fd = -1;
167 }
9ab15106
MD
168 hammer2_ioq_done(iocom, &iocom->ioq_rx);
169 hammer2_ioq_done(iocom, &iocom->ioq_tx);
170 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
78476205 171 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
9ab15106
MD
172 free(msg);
173 }
174 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
78476205 175 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
9ab15106
MD
176 free(msg->aux_data);
177 msg->aux_data = NULL;
178 free(msg);
179 }
7dc0f844
MD
180 if (iocom->wakeupfds[0] >= 0) {
181 close(iocom->wakeupfds[0]);
182 iocom->wakeupfds[0] = -1;
183 }
184 if (iocom->wakeupfds[1] >= 0) {
185 close(iocom->wakeupfds[1]);
186 iocom->wakeupfds[1] = -1;
187 }
188 pthread_mutex_destroy(&iocom->mtx);
9ab15106
MD
189}
190
4a2e0eae
MD
191/*
192 * Allocate a new one-way message.
193 */
9ab15106 194hammer2_msg_t *
78476205 195hammer2_msg_alloc(hammer2_iocom_t *iocom, size_t aux_size, uint32_t cmd)
9ab15106
MD
196{
197 hammer2_msg_t *msg;
198 int hbytes;
199
7dc0f844 200 pthread_mutex_lock(&iocom->mtx);
9ab15106
MD
201 if (aux_size) {
202 aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
203 ~HAMMER2_MSG_ALIGNMASK;
204 if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
78476205 205 TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
9ab15106
MD
206 } else {
207 if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
78476205 208 TAILQ_REMOVE(&iocom->freeq, msg, qentry);
9ab15106 209 }
7dc0f844 210 pthread_mutex_unlock(&iocom->mtx);
9ab15106
MD
211 if (msg == NULL) {
212 msg = malloc(sizeof(*msg));
78476205 213 bzero(msg, sizeof(*msg));
9ab15106
MD
214 msg->aux_data = NULL;
215 msg->aux_size = 0;
216 }
217 if (msg->aux_size != aux_size) {
218 if (msg->aux_data) {
219 free(msg->aux_data);
220 msg->aux_data = NULL;
221 msg->aux_size = 0;
222 }
223 if (aux_size) {
224 msg->aux_data = malloc(aux_size);
225 msg->aux_size = aux_size;
226 }
227 }
9ab15106 228 hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
4a2e0eae
MD
229 if (hbytes)
230 bzero(&msg->any.head, hbytes);
78476205 231 msg->hdr_size = hbytes;
9ab15106 232 msg->any.head.cmd = cmd;
8c280d5d
MD
233 msg->any.head.aux_descr = 0;
234 msg->any.head.aux_crc = 0;
9ab15106
MD
235
236 return (msg);
237}
238
4a2e0eae 239/*
4a2e0eae
MD
240 * Free a message so it can be reused afresh.
241 *
242 * NOTE: aux_size can be 0 with a non-NULL aux_data.
243 */
7dc0f844 244static
9ab15106 245void
7dc0f844 246hammer2_msg_free_locked(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
9ab15106 247{
7dc0f844 248 msg->state = NULL;
9ab15106 249 if (msg->aux_data)
78476205 250 TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
9ab15106 251 else
78476205 252 TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
9ab15106
MD
253}
254
7dc0f844
MD
255void
256hammer2_msg_free(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
257{
258 pthread_mutex_lock(&iocom->mtx);
259 hammer2_msg_free_locked(iocom, msg);
260 pthread_mutex_unlock(&iocom->mtx);
261}
262
9ab15106
MD
263/*
264 * I/O core loop for an iocom.
7dc0f844
MD
265 *
266 * Thread localized, iocom->mtx not held.
9ab15106
MD
267 */
268void
5903497c 269hammer2_iocom_core(hammer2_iocom_t *iocom)
9ab15106 270{
7dc0f844
MD
271 struct pollfd fds[3];
272 char dummybuf[256];
5903497c 273 hammer2_msg_t *msg;
4a2e0eae 274 int timeout;
7dc0f844
MD
275 int count;
276 int wi; /* wakeup pipe */
277 int si; /* socket */
278 int ai; /* alt bulk path socket */
9ab15106 279
9ab15106 280 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
7dc0f844
MD
281 if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
282 HAMMER2_IOCOMF_WWORK |
283 HAMMER2_IOCOMF_PWORK |
5903497c 284 HAMMER2_IOCOMF_SWORK |
7dc0f844
MD
285 HAMMER2_IOCOMF_ARWORK |
286 HAMMER2_IOCOMF_AWWORK)) == 0) {
287 /*
288 * Only poll if no immediate work is pending.
289 * Otherwise we are just wasting our time calling
290 * poll.
291 */
292 timeout = 5000;
4a2e0eae 293
7dc0f844
MD
294 count = 0;
295 wi = -1;
296 si = -1;
297 ai = -1;
9ab15106 298
7dc0f844
MD
299 /*
300 * Always check the inter-thread pipe, e.g.
301 * for iocom->txmsgq work.
302 */
303 wi = count++;
304 fds[wi].fd = iocom->wakeupfds[0];
305 fds[wi].events = POLLIN;
306 fds[wi].revents = 0;
307
308 /*
309 * Check the socket input/output direction as
310 * requested
311 */
312 if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
313 HAMMER2_IOCOMF_WREQ)) {
314 si = count++;
315 fds[si].fd = iocom->sock_fd;
316 fds[si].events = 0;
317 fds[si].revents = 0;
318
319 if (iocom->flags & HAMMER2_IOCOMF_RREQ)
320 fds[si].events |= POLLIN;
321 if (iocom->flags & HAMMER2_IOCOMF_WREQ)
322 fds[si].events |= POLLOUT;
323 }
9ab15106 324
7dc0f844
MD
325 /*
326 * Check the alternative fd for work.
327 */
328 if (iocom->alt_fd >= 0) {
329 ai = count++;
330 fds[ai].fd = iocom->alt_fd;
331 fds[ai].events = POLLIN;
332 fds[ai].revents = 0;
333 }
334 poll(fds, count, timeout);
335
336 if (wi >= 0 && (fds[wi].revents & POLLIN))
337 iocom->flags |= HAMMER2_IOCOMF_PWORK;
338 if (si >= 0 && (fds[si].revents & POLLIN))
339 iocom->flags |= HAMMER2_IOCOMF_RWORK;
340 if (si >= 0 && (fds[si].revents & POLLOUT))
341 iocom->flags |= HAMMER2_IOCOMF_WWORK;
342 if (wi >= 0 && (fds[wi].revents & POLLOUT))
343 iocom->flags |= HAMMER2_IOCOMF_WWORK;
344 if (ai >= 0 && (fds[ai].revents & POLLIN))
345 iocom->flags |= HAMMER2_IOCOMF_ARWORK;
9ab15106 346 } else {
7dc0f844
MD
347 /*
348 * Always check the pipe
349 */
350 iocom->flags |= HAMMER2_IOCOMF_PWORK;
9ab15106 351 }
7dc0f844 352
5903497c
MD
353 if (iocom->flags & HAMMER2_IOCOMF_SWORK) {
354 iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
355 iocom->state_callback(iocom);
356 }
357
7dc0f844
MD
358 /*
359 * Pending message queues from other threads wake us up
360 * with a write to the wakeupfds[] pipe. We have to clear
361 * the pipe with a dummy read.
362 */
363 if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
364 iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
365 read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
366 iocom->flags |= HAMMER2_IOCOMF_RWORK;
367 iocom->flags |= HAMMER2_IOCOMF_WWORK;
368 if (TAILQ_FIRST(&iocom->txmsgq))
5903497c 369 hammer2_iocom_flush1(iocom);
9ab15106 370 }
7dc0f844
MD
371
372 /*
373 * Message write sequencing
374 */
375 if (iocom->flags & HAMMER2_IOCOMF_WWORK)
5903497c 376 hammer2_iocom_flush1(iocom);
7dc0f844
MD
377
378 /*
379 * Message read sequencing. Run this after the write
380 * sequencing in case the write sequencing allowed another
381 * auto-DELETE to occur on the read side.
382 */
5903497c
MD
383 if (iocom->flags & HAMMER2_IOCOMF_RWORK) {
384 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
385 (msg = hammer2_ioq_read(iocom)) != NULL) {
81666e1b
MD
386 if (DebugOpt) {
387 fprintf(stderr, "receive %s\n",
388 hammer2_msg_str(msg));
389 }
5903497c
MD
390 iocom->rcvmsg_callback(iocom, msg);
391 hammer2_state_cleanuprx(iocom, msg);
392 }
393 }
7dc0f844 394
02454b3e
MD
395 if (iocom->flags & HAMMER2_IOCOMF_ARWORK) {
396 iocom->flags &= ~HAMMER2_IOCOMF_ARWORK;
9ab15106 397 iocom->altmsg_callback(iocom);
02454b3e 398 }
9ab15106
MD
399 }
400}
401
402/*
403 * Read the next ready message from the ioq, issuing I/O if needed.
404 * Caller should retry on a read-event when NULL is returned.
405 *
406 * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
1b195a98
MD
407 * be returned for each open transaction, then the ioq and iocom
408 * will be errored out and a non-transactional HAMMER2_LNK_ERROR
409 * msg will be returned as the final message. The caller should not call
410 * us again after the final message is returned.
7dc0f844
MD
411 *
412 * Thread localized, iocom->mtx not held.
9ab15106
MD
413 */
414hammer2_msg_t *
415hammer2_ioq_read(hammer2_iocom_t *iocom)
416{
417 hammer2_ioq_t *ioq = &iocom->ioq_rx;
418 hammer2_msg_t *msg;
419 hammer2_msg_hdr_t *head;
1b195a98 420 hammer2_state_t *state;
9ab15106 421 ssize_t n;
78476205
MD
422 size_t bytes;
423 size_t nmax;
9ab15106 424 uint32_t xcrc32;
78476205 425 int error;
9ab15106 426
78476205 427again:
7dc0f844
MD
428 iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
429
9ab15106
MD
430 /*
431 * If a message is already pending we can just remove and
78476205 432 * return it. Message state has already been processed.
9ab15106
MD
433 */
434 if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
78476205
MD
435 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
436 return (msg);
9ab15106
MD
437 }
438
439 /*
440 * Message read in-progress (msg is NULL at the moment). We don't
441 * allocate a msg until we have its core header.
442 */
443 bytes = ioq->fifo_end - ioq->fifo_beg;
5cf97ec5 444 nmax = sizeof(ioq->buf) - ioq->fifo_end;
9ab15106
MD
445 msg = ioq->msg;
446
447 switch(ioq->state) {
448 case HAMMER2_MSGQ_STATE_HEADER1:
449 /*
450 * Load the primary header, fail on any non-trivial read
451 * error or on EOF. Since the primary header is the same
452 * size is the message alignment it will never straddle
453 * the end of the buffer.
454 */
455 if (bytes < (int)sizeof(msg->any.head)) {
456 n = read(iocom->sock_fd,
5cf97ec5 457 ioq->buf + ioq->fifo_end,
9ab15106
MD
458 nmax);
459 if (n <= 0) {
460 if (n == 0) {
461 ioq->error = HAMMER2_IOQ_ERROR_EOF;
462 break;
463 }
464 if (errno != EINTR &&
465 errno != EINPROGRESS &&
466 errno != EAGAIN) {
467 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
468 break;
469 }
470 n = 0;
471 /* fall through */
472 }
473 ioq->fifo_end += n;
474 bytes += n;
475 nmax -= n;
476 }
477
478 /*
479 * Insufficient data accumulated (msg is NULL, caller will
480 * retry on event).
481 */
482 assert(msg == NULL);
483 if (bytes < (int)sizeof(msg->any.head))
484 break;
485
9ab15106 486 /*
5cf97ec5
MD
487 * Calculate the header, decrypt data received so far.
488 * Data will be decrypted in-place. Partial blocks are
489 * not immediately decrypted.
8c280d5d
MD
490 *
491 * WARNING! The header might be in the wrong endian, we
492 * do not fix it up until we get the entire
493 * extended header.
9ab15106 494 */
5cf97ec5 495 hammer2_crypto_decrypt(iocom, ioq);
5cf97ec5 496 head = (void *)(ioq->buf + ioq->fifo_beg);
9ab15106
MD
497
498 /*
499 * Check and fixup the core header. Note that the icrc
500 * has to be calculated before any fixups, but the crc
501 * fields in the msg may have to be swapped like everything
502 * else.
503 */
504 if (head->magic != HAMMER2_MSGHDR_MAGIC &&
505 head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
506 ioq->error = HAMMER2_IOQ_ERROR_SYNC;
507 break;
508 }
509
9ab15106
MD
510 /*
511 * Calculate the full header size and aux data size
512 */
8c280d5d
MD
513 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
514 ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
515 HAMMER2_MSG_ALIGN;
516 ioq->abytes = bswap32(head->aux_bytes) *
517 HAMMER2_MSG_ALIGN;
518 } else {
519 ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
520 HAMMER2_MSG_ALIGN;
521 ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
522 }
78476205
MD
523 if (ioq->hbytes < sizeof(msg->any.head) ||
524 ioq->hbytes > sizeof(msg->any) ||
9ab15106
MD
525 ioq->abytes > HAMMER2_MSGAUX_MAX) {
526 ioq->error = HAMMER2_IOQ_ERROR_FIELD;
527 break;
528 }
529
530 /*
02454b3e 531 * Allocate the message, the next state will fill it in.
9ab15106 532 */
78476205 533 msg = hammer2_msg_alloc(iocom, ioq->abytes, 0);
9ab15106
MD
534 ioq->msg = msg;
535
536 /*
9ab15106
MD
537 * Fall through to the next state. Make sure that the
538 * extended header does not straddle the end of the buffer.
539 * We still want to issue larger reads into our buffer,
540 * book-keeping is easier if we don't bcopy() yet.
541 */
542 if (bytes + nmax < ioq->hbytes) {
5cf97ec5
MD
543 bcopy(ioq->buf + ioq->fifo_beg, ioq->buf, bytes);
544 ioq->fifo_cdx -= ioq->fifo_beg;
9ab15106
MD
545 ioq->fifo_beg = 0;
546 ioq->fifo_end = bytes;
5cf97ec5 547 nmax = sizeof(ioq->buf) - ioq->fifo_end;
9ab15106
MD
548 }
549 ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
550 /* fall through */
551 case HAMMER2_MSGQ_STATE_HEADER2:
552 /*
553 * Fill out the extended header.
554 */
555 assert(msg != NULL);
556 if (bytes < ioq->hbytes) {
557 n = read(iocom->sock_fd,
02454b3e 558 ioq->buf + ioq->fifo_end,
9ab15106
MD
559 nmax);
560 if (n <= 0) {
561 if (n == 0) {
562 ioq->error = HAMMER2_IOQ_ERROR_EOF;
563 break;
564 }
565 if (errno != EINTR &&
566 errno != EINPROGRESS &&
567 errno != EAGAIN) {
568 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
569 break;
570 }
571 n = 0;
572 /* fall through */
573 }
574 ioq->fifo_end += n;
575 bytes += n;
576 nmax -= n;
577 }
578
579 /*
580 * Insufficient data accumulated (set msg NULL so caller will
581 * retry on event).
582 */
583 if (bytes < ioq->hbytes) {
584 msg = NULL;
585 break;
586 }
587
588 /*
5cf97ec5 589 * Calculate the extended header, decrypt data received
8c280d5d
MD
590 * so far. Handle endian-conversion for the entire extended
591 * header.
9ab15106 592 */
5cf97ec5
MD
593 hammer2_crypto_decrypt(iocom, ioq);
594 head = (void *)(ioq->buf + ioq->fifo_beg);
9ab15106
MD
595
596 /*
8c280d5d 597 * Check the CRC.
9ab15106 598 */
8c280d5d
MD
599 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
600 xcrc32 = bswap32(head->hdr_crc);
601 else
602 xcrc32 = head->hdr_crc;
603 head->hdr_crc = 0;
604 if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
605 ioq->error = HAMMER2_IOQ_ERROR_XCRC;
81666e1b
MD
606 fprintf(stderr, "BAD-XCRC(%08x,%08x) %s\n",
607 xcrc32, hammer2_icrc32(head, ioq->hbytes),
608 hammer2_msg_str(msg));
02454b3e 609 assert(0);
8c280d5d
MD
610 break;
611 }
612 head->hdr_crc = xcrc32;
613
614 if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
615 hammer2_bswap_head(head);
9ab15106
MD
616 }
617
618 /*
619 * Copy the extended header into the msg and adjust the
620 * FIFO.
621 */
622 bcopy(head, &msg->any, ioq->hbytes);
623
624 /*
625 * We are either done or we fall-through.
626 */
627 if (ioq->abytes == 0) {
628 ioq->fifo_beg += ioq->hbytes;
629 break;
630 }
631
632 /*
633 * Must adjust nmax and bytes (and the state) when falling
634 * through.
635 */
636 ioq->fifo_beg += ioq->hbytes;
637 nmax -= ioq->hbytes;
638 bytes -= ioq->hbytes;
639 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
640 /* fall through */
641 case HAMMER2_MSGQ_STATE_AUXDATA1:
642 /*
643 * Copy the partial or complete payload from remaining
644 * bytes in the FIFO. We have to fall-through either
645 * way so we can check the crc.
78476205
MD
646 *
647 * Adjust msg->aux_size to the final actual value.
9ab15106 648 */
5cf97ec5
MD
649 ioq->already = ioq->fifo_cdx - ioq->fifo_beg;
650 if (ioq->already > ioq->abytes)
651 ioq->already = ioq->abytes;
9ab15106 652 if (bytes >= ioq->abytes) {
5cf97ec5 653 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
9ab15106
MD
654 ioq->abytes);
655 msg->aux_size = ioq->abytes;
656 ioq->fifo_beg += ioq->abytes;
5cf97ec5
MD
657 if (ioq->fifo_cdx < ioq->fifo_beg)
658 ioq->fifo_cdx = ioq->fifo_beg;
9ab15106
MD
659 bytes -= ioq->abytes;
660 } else if (bytes) {
5cf97ec5 661 bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
9ab15106
MD
662 bytes);
663 msg->aux_size = bytes;
664 ioq->fifo_beg += bytes;
5cf97ec5
MD
665 if (ioq->fifo_cdx < ioq->fifo_beg)
666 ioq->fifo_cdx = ioq->fifo_beg;
9ab15106 667 bytes = 0;
78476205
MD
668 } else {
669 msg->aux_size = 0;
9ab15106
MD
670 }
671 ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
672 /* fall through */
673 case HAMMER2_MSGQ_STATE_AUXDATA2:
674 /*
675 * Read the remainder of the payload directly into the
676 * msg->aux_data buffer.
677 */
678 assert(msg);
679 if (msg->aux_size < ioq->abytes) {
680 assert(bytes == 0);
681 n = read(iocom->sock_fd,
682 msg->aux_data + msg->aux_size,
683 ioq->abytes - msg->aux_size);
684 if (n <= 0) {
685 if (n == 0) {
686 ioq->error = HAMMER2_IOQ_ERROR_EOF;
687 break;
688 }
689 if (errno != EINTR &&
690 errno != EINPROGRESS &&
691 errno != EAGAIN) {
692 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
693 break;
694 }
695 n = 0;
696 /* fall through */
697 }
698 msg->aux_size += n;
699 }
700
701 /*
702 * Insufficient data accumulated (set msg NULL so caller will
703 * retry on event).
704 */
705 if (msg->aux_size < ioq->abytes) {
706 msg = NULL;
707 break;
708 }
709 assert(msg->aux_size == ioq->abytes);
5cf97ec5 710 hammer2_crypto_decrypt_aux(iocom, ioq, msg, ioq->already);
9ab15106
MD
711
712 /*
8c280d5d 713 * Check aux_crc, then we are done.
9ab15106
MD
714 */
715 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
8c280d5d 716 if (xcrc32 != msg->any.head.aux_crc) {
9ab15106
MD
717 ioq->error = HAMMER2_IOQ_ERROR_ACRC;
718 break;
719 }
720 break;
721 case HAMMER2_MSGQ_STATE_ERROR:
1b195a98
MD
722 /*
723 * Continued calls to drain recorded transactions (returning
724 * a LNK_ERROR for each one), before we return the final
725 * LNK_ERROR.
726 */
727 assert(msg == NULL);
728 break;
9ab15106
MD
729 default:
730 /*
731 * We don't double-return errors, the caller should not
732 * have called us again after getting an error msg.
733 */
734 assert(0);
735 break;
736 }
737
738 /*
5cf97ec5
MD
739 * Check the message sequence. The iv[] should prevent any
740 * possibility of a replay but we add this check anyway.
741 */
742 if (msg && ioq->error == 0) {
743 if ((msg->any.head.salt & 255) != (ioq->seq & 255)) {
744 ioq->error = HAMMER2_IOQ_ERROR_MSGSEQ;
745 } else {
746 ++ioq->seq;
747 }
748 }
749
750 /*
78476205
MD
751 * Process transactional state for the message.
752 */
753 if (msg && ioq->error == 0) {
754 error = hammer2_state_msgrx(iocom, msg);
755 if (error) {
756 if (error == HAMMER2_IOQ_ERROR_EALREADY) {
757 hammer2_msg_free(iocom, msg);
758 goto again;
759 }
760 ioq->error = error;
761 }
762 }
763
764 /*
9ab15106
MD
765 * Handle error, RREQ, or completion
766 *
767 * NOTE: nmax and bytes are invalid at this point, we don't bother
768 * to update them when breaking out.
769 */
770 if (ioq->error) {
771 /*
1b195a98
MD
772 * An unrecoverable error causes all active receive
773 * transactions to be terminated with a LNK_ERROR message.
9ab15106 774 *
1b195a98
MD
775 * Once all active transactions are exhausted we set the
776 * iocom ERROR flag and return a non-transactional LNK_ERROR
777 * message, which should cause master processing loops to
778 * terminate.
9ab15106 779 */
4a2e0eae 780 assert(ioq->msg == msg);
1b195a98
MD
781 if (msg) {
782 hammer2_msg_free(iocom, msg);
9ab15106 783 ioq->msg = NULL;
9ab15106 784 }
1b195a98
MD
785
786 /*
787 * No more I/O read processing
788 */
789 ioq->state = HAMMER2_MSGQ_STATE_ERROR;
790
791 /*
7dc0f844
MD
792 * Simulate a remote LNK_ERROR DELETE msg for any open
793 * transactions, ending with a final non-transactional
794 * LNK_ERROR (that the session can detect) when no
795 * transactions remain.
1b195a98
MD
796 */
797 msg = hammer2_msg_alloc(iocom, 0, 0);
9ab15106
MD
798 bzero(&msg->any.head, sizeof(msg->any.head));
799 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
800 msg->any.head.cmd = HAMMER2_LNK_ERROR;
801 msg->any.head.error = ioq->error;
1b195a98 802
7dc0f844 803 pthread_mutex_lock(&iocom->mtx);
1b195a98
MD
804 if ((state = RB_ROOT(&iocom->staterd_tree)) != NULL) {
805 /*
7dc0f844
MD
806 * Active remote transactions are still present.
807 * Simulate the other end sending us a DELETE.
1b195a98 808 */
7dc0f844 809 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
7dc0f844
MD
810 hammer2_msg_free(iocom, msg);
811 msg = NULL;
812 } else {
7dc0f844
MD
813 /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
814 msg->state = state;
815 msg->any.head.spanid = state->spanid;
816 msg->any.head.msgid = state->msgid;
817 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
818 HAMMER2_MSGF_DELETE;
819 }
820 } else if ((state = RB_ROOT(&iocom->statewr_tree)) != NULL) {
821 /*
822 * Active local transactions are still present.
823 * Simulate the other end sending us a DELETE.
824 */
825 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
7dc0f844
MD
826 hammer2_msg_free(iocom, msg);
827 msg = NULL;
828 } else {
7dc0f844
MD
829 msg->state = state;
830 msg->any.head.spanid = state->spanid;
831 msg->any.head.msgid = state->msgid;
832 msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
833 HAMMER2_MSGF_DELETE |
834 HAMMER2_MSGF_REPLY;
835 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
836 msg->any.head.cmd |=
837 HAMMER2_MSGF_CREATE;
838 }
839 }
1b195a98
MD
840 } else {
841 /*
7dc0f844
MD
842 * No active local or remote transactions remain.
843 * Generate a final LNK_ERROR and flag EOF.
1b195a98
MD
844 */
845 msg->state = NULL;
846 iocom->flags |= HAMMER2_IOCOMF_EOF;
81666e1b 847 fprintf(stderr, "EOF ON SOCKET %d\n", iocom->sock_fd);
1b195a98 848 }
7dc0f844
MD
849 pthread_mutex_unlock(&iocom->mtx);
850
851 /*
852 * For the iocom error case we want to set RWORK to indicate
853 * that more messages might be pending.
854 *
855 * It is possible to return NULL when there is more work to
856 * do because each message has to be DELETEd in both
857 * directions before we continue on with the next (though
858 * this could be optimized). The transmit direction will
859 * re-set RWORK.
860 */
861 if (msg)
862 iocom->flags |= HAMMER2_IOCOMF_RWORK;
9ab15106
MD
863 } else if (msg == NULL) {
864 /*
865 * Insufficient data received to finish building the message,
866 * set RREQ and return NULL.
867 *
868 * Leave ioq->msg intact.
869 * Leave the FIFO intact.
870 */
871 iocom->flags |= HAMMER2_IOCOMF_RREQ;
9ab15106
MD
872 } else {
873 /*
7dc0f844 874 * Return msg.
9ab15106
MD
875 *
876 * The fifo has already been advanced past the message.
877 * Trivially reset the FIFO indices if possible.
7dc0f844
MD
878 *
879 * clear the FIFO if it is now empty and set RREQ to wait
880 * for more from the socket. If the FIFO is not empty set
881 * TWORK to bypass the poll so we loop immediately.
9ab15106
MD
882 */
883 if (ioq->fifo_beg == ioq->fifo_end) {
884 iocom->flags |= HAMMER2_IOCOMF_RREQ;
5cf97ec5 885 ioq->fifo_cdx = 0;
9ab15106
MD
886 ioq->fifo_beg = 0;
887 ioq->fifo_end = 0;
888 } else {
7dc0f844 889 iocom->flags |= HAMMER2_IOCOMF_RWORK;
9ab15106
MD
890 }
891 ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
892 ioq->msg = NULL;
893 }
894 return (msg);
895}
896
897/*
898 * Calculate the header and data crc's and write a low-level message to
8c280d5d 899 * the connection. If aux_crc is non-zero the aux_data crc is already
9ab15106
MD
900 * assumed to have been set.
901 *
902 * A non-NULL msg is added to the queue but not necessarily flushed.
903 * Calling this function with msg == NULL will get a flush going.
7dc0f844
MD
904 *
905 * Caller must hold iocom->mtx.
9ab15106 906 */
7dc0f844
MD
907void
908hammer2_iocom_flush1(hammer2_iocom_t *iocom)
9ab15106
MD
909{
910 hammer2_ioq_t *ioq = &iocom->ioq_tx;
7dc0f844 911 hammer2_msg_t *msg;
9ab15106 912 uint32_t xcrc32;
4a2e0eae 913 int hbytes;
7dc0f844
MD
914 hammer2_msg_queue_t tmpq;
915
916 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
917 TAILQ_INIT(&tmpq);
918 pthread_mutex_lock(&iocom->mtx);
919 while ((msg = TAILQ_FIRST(&iocom->txmsgq)) != NULL) {
920 TAILQ_REMOVE(&iocom->txmsgq, msg, qentry);
921 TAILQ_INSERT_TAIL(&tmpq, msg, qentry);
9ab15106 922 }
7dc0f844 923 pthread_mutex_unlock(&iocom->mtx);
9ab15106 924
7dc0f844
MD
925 while ((msg = TAILQ_FIRST(&tmpq)) != NULL) {
926 /*
927 * Process terminal connection errors.
928 */
929 TAILQ_REMOVE(&tmpq, msg, qentry);
930 if (ioq->error) {
931 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
932 ++ioq->msgcount;
933 continue;
934 }
9ab15106 935
7dc0f844
MD
936 /*
937 * Finish populating the msg fields. The salt ensures that
938 * the iv[] array is ridiculously randomized and we also
939 * re-seed our PRNG every 32768 messages just to be sure.
940 */
941 msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
942 msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
943 ++ioq->seq;
944 if ((ioq->seq & 32767) == 0)
945 srandomdev();
9ab15106 946
7dc0f844
MD
947 /*
948 * Calculate aux_crc if 0, then calculate hdr_crc.
949 */
950 if (msg->aux_size && msg->any.head.aux_crc == 0) {
951 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
952 xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
953 msg->any.head.aux_crc = xcrc32;
954 }
955 msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
956 assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
9ab15106 957
7dc0f844
MD
958 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
959 HAMMER2_MSG_ALIGN;
960 msg->any.head.hdr_crc = 0;
961 msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
9ab15106 962
7dc0f844
MD
963 /*
964 * Enqueue the message (the flush codes handles stream
965 * encryption).
966 */
967 TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
968 ++ioq->msgcount;
969 }
970 hammer2_iocom_flush2(iocom);
4a2e0eae
MD
971}
972
7dc0f844
MD
973/*
974 * Thread localized, iocom->mtx not held by caller.
975 */
4a2e0eae 976void
7dc0f844 977hammer2_iocom_flush2(hammer2_iocom_t *iocom)
4a2e0eae
MD
978{
979 hammer2_ioq_t *ioq = &iocom->ioq_tx;
980 hammer2_msg_t *msg;
981 ssize_t nmax;
02454b3e 982 ssize_t omax;
4a2e0eae
MD
983 ssize_t nact;
984 struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
78476205
MD
985 size_t hbytes;
986 size_t abytes;
02454b3e
MD
987 size_t hoff;
988 size_t aoff;
4a2e0eae 989 int n;
9ab15106 990
7dc0f844
MD
991 if (ioq->error) {
992 hammer2_iocom_drain(iocom);
993 return;
994 }
995
9ab15106
MD
996 /*
997 * Pump messages out the connection by building an iovec.
02454b3e
MD
998 *
999 * ioq->hbytes/ioq->abytes tracks how much of the first message
1000 * in the queue has been successfully written out, so we can
1001 * resume writing.
9ab15106
MD
1002 */
1003 n = 0;
1004 nmax = 0;
02454b3e
MD
1005 hoff = ioq->hbytes;
1006 aoff = ioq->abytes;
9ab15106 1007
78476205 1008 TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
9ab15106
MD
1009 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1010 HAMMER2_MSG_ALIGN;
9ab15106 1011 abytes = msg->aux_size;
02454b3e
MD
1012 assert(hoff <= hbytes && aoff <= abytes);
1013
1014 if (hoff < hbytes) {
9ab15106
MD
1015 iov[n].iov_base = (char *)&msg->any.head + hoff;
1016 iov[n].iov_len = hbytes - hoff;
1017 nmax += hbytes - hoff;
1018 ++n;
1019 if (n == HAMMER2_IOQ_MAXIOVEC)
1020 break;
1021 }
02454b3e 1022 if (aoff < abytes) {
9ab15106 1023 assert(msg->aux_data != NULL);
02454b3e 1024 iov[n].iov_base = (char *)msg->aux_data + aoff;
9ab15106
MD
1025 iov[n].iov_len = abytes - aoff;
1026 nmax += abytes - aoff;
1027 ++n;
1028 if (n == HAMMER2_IOQ_MAXIOVEC)
1029 break;
1030 }
02454b3e
MD
1031 hoff = 0;
1032 aoff = 0;
9ab15106
MD
1033 }
1034 if (n == 0)
1035 return;
1036
1037 /*
5cf97ec5
MD
1038 * Encrypt and write the data. The crypto code will move the
1039 * data into the fifo and adjust the iov as necessary. If
1040 * encryption is disabled the iov is left alone.
1041 *
02454b3e
MD
1042 * May return a smaller iov (thus a smaller n), with aggregated
1043 * chunks. May reduce nmax to what fits in the FIFO.
5cf97ec5 1044 */
02454b3e
MD
1045 omax = nmax;
1046 n = hammer2_crypto_encrypt(iocom, ioq, iov, n, &nmax);
5cf97ec5
MD
1047
1048 /*
9ab15106
MD
1049 * Execute the writev() then figure out what happened.
1050 */
1051 nact = writev(iocom->sock_fd, iov, n);
1052 if (nact < 0) {
1053 if (errno != EINTR &&
1054 errno != EINPROGRESS &&
1055 errno != EAGAIN) {
7dc0f844
MD
1056 /*
1057 * Fatal write error
1058 */
9ab15106 1059 ioq->error = HAMMER2_IOQ_ERROR_SOCK;
4a2e0eae 1060 hammer2_iocom_drain(iocom);
9ab15106 1061 } else {
7dc0f844
MD
1062 /*
1063 * Wait for socket buffer space
1064 */
9ab15106
MD
1065 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1066 }
1067 return;
1068 }
7dc0f844
MD
1069
1070 /*
02454b3e
MD
1071 * Indicate bytes written successfully.
1072 *
1073 * If we were unable to write the entire iov array then set WREQ
1074 * to wait for more socket buffer space.
1075 *
1076 * If the FIFO space was insufficient to fully drain all messages
1077 * set WWORK to cause the core to call us again for the next batch.
7dc0f844 1078 */
5cf97ec5 1079 hammer2_crypto_encrypt_wrote(iocom, ioq, nact);
7dc0f844 1080 if (nact != nmax)
9ab15106
MD
1081 iocom->flags |= HAMMER2_IOCOMF_WREQ;
1082
7dc0f844
MD
1083 /*
1084 * Clean out the transmit queue based on what we successfully
02454b3e
MD
1085 * sent. ioq->hbytes/abytes represents the portion of the first
1086 * message previously sent.
7dc0f844 1087 */
9ab15106
MD
1088 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
1089 hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
1090 HAMMER2_MSG_ALIGN;
1091 abytes = msg->aux_size;
1092
78476205 1093 if ((size_t)nact < hbytes - ioq->hbytes) {
9ab15106 1094 ioq->hbytes += nact;
02454b3e 1095 /* nact = 0; */
9ab15106
MD
1096 break;
1097 }
1098 nact -= hbytes - ioq->hbytes;
1099 ioq->hbytes = hbytes;
78476205 1100 if ((size_t)nact < abytes - ioq->abytes) {
9ab15106 1101 ioq->abytes += nact;
02454b3e 1102 /* nact = 0; */
9ab15106
MD
1103 break;
1104 }
1105 nact -= abytes - ioq->abytes;
1106
78476205 1107 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
9ab15106
MD
1108 --ioq->msgcount;
1109 ioq->hbytes = 0;
1110 ioq->abytes = 0;
78476205
MD
1111
1112 hammer2_state_cleanuptx(iocom, msg);
9ab15106 1113 }
81666e1b
MD
1114
1115 /*
1116 * If more messages are pending on WREQ wasn't set we must
1117 * ensure that WWORK gets set.
1118 */
1119 if (msg && (iocom->flags & HAMMER2_IOCOMF_WREQ) == 0)
1120 iocom->flags |= HAMMER2_IOCOMF_WWORK;
02454b3e 1121 assert(nact == 0);
9ab15106 1122 if (ioq->error) {
7dc0f844 1123 hammer2_iocom_drain(iocom);
9ab15106
MD
1124 }
1125}
1126
1127/*
1128 * Kill pending msgs on ioq_tx and adjust the flags such that no more
1129 * write events will occur. We don't kill read msgs because we want
1130 * the caller to pull off our contrived terminal error msg to detect
1131 * the connection failure.
7dc0f844
MD
1132 *
1133 * Thread localized, iocom->mtx not held by caller.
9ab15106
MD
1134 */
1135void
4a2e0eae 1136hammer2_iocom_drain(hammer2_iocom_t *iocom)
9ab15106
MD
1137{
1138 hammer2_ioq_t *ioq = &iocom->ioq_tx;
1139 hammer2_msg_t *msg;
1140
7dc0f844 1141 iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
02454b3e
MD
1142 ioq->hbytes = 0;
1143 ioq->abytes = 0;
7dc0f844 1144
9ab15106 1145 while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
78476205 1146 TAILQ_REMOVE(&ioq->msgq, msg, qentry);
9ab15106 1147 --ioq->msgcount;
7dc0f844 1148 hammer2_state_cleanuptx(iocom, msg);
9ab15106 1149 }
9ab15106
MD
1150}
1151
1152/*
8c280d5d 1153 * Write a message to an iocom, with additional state processing.
78476205
MD
1154 */
1155void
8c280d5d
MD
1156hammer2_msg_write(hammer2_iocom_t *iocom, hammer2_msg_t *msg,
1157 void (*func)(hammer2_state_t *, hammer2_msg_t *),
7dc0f844
MD
1158 void *data,
1159 hammer2_state_t **statep)
78476205 1160{
8c280d5d 1161 hammer2_state_t *state;
7dc0f844 1162 char dummy;
78476205 1163
8c280d5d
MD
1164 /*
1165 * Handle state processing, create state if necessary.
1166 */
7dc0f844 1167 pthread_mutex_lock(&iocom->mtx);
8c280d5d 1168 if ((state = msg->state) != NULL) {
78476205 1169 /*
8c280d5d
MD
1170 * Existing transaction (could be reply). It is also
1171 * possible for this to be the first reply (CREATE is set),
1172 * in which case we populate state->txcmd.
78476205 1173 */
8c280d5d
MD
1174 msg->any.head.msgid = state->msgid;
1175 msg->any.head.spanid = state->spanid;
1176 if (func) {
1177 state->func = func;
1178 state->any.any = data;
78476205 1179 }
7dc0f844
MD
1180 assert(((state->txcmd ^ msg->any.head.cmd) &
1181 HAMMER2_MSGF_REPLY) == 0);
8c280d5d
MD
1182 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1183 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
8c280d5d 1184 } else if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
8c280d5d
MD
1185 /*
1186 * No existing state and CREATE is set, create new
1187 * state for outgoing command. This can't happen if
1188 * REPLY is set as the state would already exist for
1189 * a transaction reply.
1190 */
1191 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
1192
1193 state = malloc(sizeof(*state));
1194 bzero(state, sizeof(*state));
1195 state->iocom = iocom;
1196 state->flags = HAMMER2_STATE_DYNAMIC;
1197 state->msg = msg;
1198 state->msgid = (uint64_t)(uintptr_t)state;
1199 state->spanid = msg->any.head.spanid;
1200 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
7dc0f844 1201 state->rxcmd = HAMMER2_MSGF_REPLY;
8c280d5d
MD
1202 state->func = func;
1203 state->any.any = data;
1204 RB_INSERT(hammer2_state_tree, &iocom->statewr_tree, state);
1205 state->flags |= HAMMER2_STATE_INSERTED;
1206 msg->state = state;
1207 msg->any.head.msgid = state->msgid;
1208 /* spanid set by caller */
78476205 1209 } else {
8c280d5d
MD
1210 msg->any.head.msgid = 0;
1211 /* spanid set by caller */
1212 }
1213
7dc0f844
MD
1214 if (statep)
1215 *statep = state;
1216
8c280d5d 1217 /*
7dc0f844
MD
1218 * Queue it for output, wake up the I/O pthread. Note that the
1219 * I/O thread is responsible for generating the CRCs and encryption.
8c280d5d 1220 */
7dc0f844
MD
1221 TAILQ_INSERT_TAIL(&iocom->txmsgq, msg, qentry);
1222 dummy = 0;
1223 write(iocom->wakeupfds[1], &dummy, 1); /* XXX optimize me */
1224 pthread_mutex_unlock(&iocom->mtx);
8c280d5d
MD
1225}
1226
8c280d5d
MD
1227/*
1228 * This is a shortcut to formulate a reply to msg with a simple error code,
1229 * It can reply to and terminate a transaction, or it can reply to a one-way
1230 * messages. A HAMMER2_LNK_ERROR command code is utilized to encode
1231 * the error code (which can be 0). Not all transactions are terminated
1232 * with HAMMER2_LNK_ERROR status (the low level only cares about the
1233 * MSGF_DELETE flag), but most are.
1234 *
1235 * Replies to one-way messages are a bit of an oxymoron but the feature
1236 * is used by the debug (DBG) protocol.
1237 *
1238 * The reply contains no extended data.
1239 */
1240void
1241hammer2_msg_reply(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1242{
1243 hammer2_state_t *state = msg->state;
1244 hammer2_msg_t *nmsg;
1245 uint32_t cmd;
1246
1247
1248 /*
1249 * Reply with a simple error code and terminate the transaction.
1250 */
1251 cmd = HAMMER2_LNK_ERROR;
1252
1253 /*
1254 * Check if our direction has even been initiated yet, set CREATE.
1255 *
1256 * Check what direction this is (command or reply direction). Note
1257 * that txcmd might not have been initiated yet.
1258 *
1259 * If our direction has already been closed we just return without
1260 * doing anything.
1261 */
1262 if (state) {
1263 if (state->txcmd & HAMMER2_MSGF_DELETE)
1264 return;
1265 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1266 cmd |= HAMMER2_MSGF_CREATE;
7dc0f844 1267 if (state->txcmd & HAMMER2_MSGF_REPLY)
8c280d5d
MD
1268 cmd |= HAMMER2_MSGF_REPLY;
1269 cmd |= HAMMER2_MSGF_DELETE;
1270 } else {
1271 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1272 cmd |= HAMMER2_MSGF_REPLY;
78476205 1273 }
8c280d5d 1274
78476205
MD
1275 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1276 nmsg->any.head.error = error;
8c280d5d 1277 nmsg->state = msg->state;
7dc0f844 1278 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
8c280d5d
MD
1279}
1280
1281/*
1282 * Similar to hammer2_msg_reply() but leave the transaction open. That is,
1283 * we are generating a streaming reply or an intermediate acknowledgement
1284 * of some sort as part of the higher level protocol, with more to come
1285 * later.
1286 */
1287void
1288hammer2_msg_result(hammer2_iocom_t *iocom, hammer2_msg_t *msg, uint32_t error)
1289{
1290 hammer2_state_t *state = msg->state;
1291 hammer2_msg_t *nmsg;
1292 uint32_t cmd;
1293
1294
1295 /*
1296 * Reply with a simple error code and terminate the transaction.
1297 */
1298 cmd = HAMMER2_LNK_ERROR;
1299
1300 /*
1301 * Check if our direction has even been initiated yet, set CREATE.
1302 *
1303 * Check what direction this is (command or reply direction). Note
1304 * that txcmd might not have been initiated yet.
1305 *
1306 * If our direction has already been closed we just return without
1307 * doing anything.
1308 */
1309 if (state) {
1310 if (state->txcmd & HAMMER2_MSGF_DELETE)
1311 return;
1312 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1313 cmd |= HAMMER2_MSGF_CREATE;
7dc0f844 1314 if (state->txcmd & HAMMER2_MSGF_REPLY)
8c280d5d
MD
1315 cmd |= HAMMER2_MSGF_REPLY;
1316 /* continuing transaction, do not set MSGF_DELETE */
1317 } else {
1318 if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
1319 cmd |= HAMMER2_MSGF_REPLY;
1320 }
1321
1322 nmsg = hammer2_msg_alloc(iocom, 0, cmd);
1323 nmsg->any.head.error = error;
1324 nmsg->state = state;
7dc0f844 1325 hammer2_msg_write(iocom, nmsg, NULL, NULL, NULL);
8c280d5d
MD
1326}
1327
1328/*
1329 * Terminate a transaction given a state structure by issuing a DELETE.
1330 */
1331void
1332hammer2_state_reply(hammer2_state_t *state, uint32_t error)
1333{
1334 hammer2_msg_t *nmsg;
1335 uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
1336
1337 /*
1338 * Nothing to do if we already transmitted a delete
1339 */
1340 if (state->txcmd & HAMMER2_MSGF_DELETE)
1341 return;
1342
1343 /*
1344 * We must also set CREATE if this is our first response to a
1345 * remote command.
1346 */
1347 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
1348 cmd |= HAMMER2_MSGF_CREATE;
1349
1350 /*
1351 * Set REPLY if the other end initiated the command. Otherwise
1352 * we are the command direction.
1353 */
7dc0f844 1354 if (state->txcmd & HAMMER2_MSGF_REPLY)
8c280d5d
MD
1355 cmd |= HAMMER2_MSGF_REPLY;
1356
1357 nmsg = hammer2_msg_alloc(state->iocom, 0, cmd);
1358 nmsg->any.head.error = error;
1359 nmsg->state = state;
7dc0f844 1360 hammer2_msg_write(state->iocom, nmsg, NULL, NULL, NULL);
78476205
MD
1361}
1362
1363/************************************************************************
1364 * TRANSACTION STATE HANDLING *
1365 ************************************************************************
1366 *
1367 */
1368
1369RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
1370
1371/*
1372 * Process state tracking for a message after reception, prior to
1373 * execution.
1374 *
1375 * Called with msglk held and the msg dequeued.
1376 *
1377 * All messages are called with dummy state and return actual state.
1378 * (One-off messages often just return the same dummy state).
1379 *
1380 * May request that caller discard the message by setting *discardp to 1.
1381 * The returned state is not used in this case and is allowed to be NULL.
1382 *
1383 * --
1384 *
1385 * These routines handle persistent and command/reply message state via the
1386 * CREATE and DELETE flags. The first message in a command or reply sequence
1387 * sets CREATE, the last message in a command or reply sequence sets DELETE.
4a2e0eae 1388 *
78476205
MD
1389 * There can be any number of intermediate messages belonging to the same
1390 * sequence sent inbetween the CREATE message and the DELETE message,
1391 * which set neither flag. This represents a streaming command or reply.
1392 *
1393 * Any command message received with CREATE set expects a reply sequence to
1394 * be returned. Reply sequences work the same as command sequences except the
1395 * REPLY bit is also sent. Both the command side and reply side can
1396 * degenerate into a single message with both CREATE and DELETE set. Note
1397 * that one side can be streaming and the other side not, or neither, or both.
1398 *
1399 * The msgid is unique for the initiator. That is, two sides sending a new
1400 * message can use the same msgid without colliding.
1401 *
1402 * --
1403 *
1404 * ABORT sequences work by setting the ABORT flag along with normal message
1405 * state. However, ABORTs can also be sent on half-closed messages, that is
1406 * even if the command or reply side has already sent a DELETE, as long as
1407 * the message has not been fully closed it can still send an ABORT+DELETE
1408 * to terminate the half-closed message state.
1409 *
1410 * Since ABORT+DELETEs can race we silently discard ABORT's for message
1411 * state which has already been fully closed. REPLY+ABORT+DELETEs can
1412 * also race, and in this situation the other side might have already
1413 * initiated a new unrelated command with the same message id. Since
1414 * the abort has not set the CREATE flag the situation can be detected
1415 * and the message will also be discarded.
1416 *
1417 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
1418 * The ABORT request is essentially integrated into the command instead
1419 * of being sent later on. In this situation the command implementation
1420 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
1421 * special-case non-blocking operation for the command.
1422 *
1423 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
1424 * to be mid-stream aborts for command/reply sequences. ABORTs on
1425 * one-way messages are not supported.
1426 *
1427 * NOTE! If a command sequence does not support aborts the ABORT flag is
1428 * simply ignored.
1429 *
1430 * --
1431 *
1432 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
1433 * set. One-off messages cannot be aborted and typically aren't processed
1434 * by these routines. The REPLY bit can be used to distinguish whether a
1435 * one-off message is a command or reply. For example, one-off replies
1436 * will typically just contain status updates.
9ab15106 1437 */
78476205
MD
1438static int
1439hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1440{
1441 hammer2_state_t *state;
1442 hammer2_state_t dummy;
1443 int error;
1444
1445 /*
1446 * Lock RB tree and locate existing persistent state, if any.
1447 *
1448 * If received msg is a command state is on staterd_tree.
1449 * If received msg is a reply state is on statewr_tree.
1450 */
78476205
MD
1451
1452 dummy.msgid = msg->any.head.msgid;
8c280d5d 1453 dummy.spanid = msg->any.head.spanid;
7dc0f844 1454 pthread_mutex_lock(&iocom->mtx);
78476205
MD
1455 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
1456 state = RB_FIND(hammer2_state_tree,
1457 &iocom->statewr_tree, &dummy);
1458 } else {
1459 state = RB_FIND(hammer2_state_tree,
1460 &iocom->staterd_tree, &dummy);
1461 }
1462 msg->state = state;
7dc0f844 1463 pthread_mutex_unlock(&iocom->mtx);
78476205
MD
1464
1465 /*
1466 * Short-cut one-off or mid-stream messages (state may be NULL).
1467 */
1468 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1469 HAMMER2_MSGF_ABORT)) == 0) {
78476205
MD
1470 return(0);
1471 }
1472
1473 /*
1474 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1475 * inside the case statements.
1476 */
1477 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1478 HAMMER2_MSGF_REPLY)) {
1479 case HAMMER2_MSGF_CREATE:
1480 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1481 /*
1482 * New persistant command received.
1483 */
1484 if (state) {
81666e1b
MD
1485 fprintf(stderr, "duplicate-trans %s\n",
1486 hammer2_msg_str(msg));
78476205
MD
1487 error = HAMMER2_IOQ_ERROR_TRANS;
1488 break;
1489 }
1490 state = malloc(sizeof(*state));
1491 bzero(state, sizeof(*state));
1492 state->iocom = iocom;
1493 state->flags = HAMMER2_STATE_DYNAMIC;
1494 state->msg = msg;
7dc0f844 1495 state->txcmd = HAMMER2_MSGF_REPLY;
78476205 1496 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
7dc0f844 1497 pthread_mutex_lock(&iocom->mtx);
78476205 1498 RB_INSERT(hammer2_state_tree, &iocom->staterd_tree, state);
7dc0f844 1499 pthread_mutex_unlock(&iocom->mtx);
78476205 1500 state->flags |= HAMMER2_STATE_INSERTED;
8c280d5d
MD
1501 state->msgid = msg->any.head.msgid;
1502 state->spanid = msg->any.head.spanid;
78476205
MD
1503 msg->state = state;
1504 error = 0;
1505 break;
1506 case HAMMER2_MSGF_DELETE:
1507 /*
1508 * Persistent state is expected but might not exist if an
1509 * ABORT+DELETE races the close.
1510 */
1511 if (state == NULL) {
1512 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1513 error = HAMMER2_IOQ_ERROR_EALREADY;
1514 } else {
81666e1b
MD
1515 fprintf(stderr, "missing-state %s\n",
1516 hammer2_msg_str(msg));
78476205
MD
1517 error = HAMMER2_IOQ_ERROR_TRANS;
1518 }
1519 break;
1520 }
1521
1522 /*
1523 * Handle another ABORT+DELETE case if the msgid has already
1524 * been reused.
1525 */
1526 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1527 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1528 error = HAMMER2_IOQ_ERROR_EALREADY;
1529 } else {
81666e1b
MD
1530 fprintf(stderr, "reused-state %s\n",
1531 hammer2_msg_str(msg));
78476205
MD
1532 error = HAMMER2_IOQ_ERROR_TRANS;
1533 }
1534 break;
1535 }
1536 error = 0;
1537 break;
1538 default:
1539 /*
1540 * Check for mid-stream ABORT command received, otherwise
1541 * allow.
1542 */
1543 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1544 if (state == NULL ||
1545 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1546 error = HAMMER2_IOQ_ERROR_EALREADY;
1547 break;
1548 }
1549 }
1550 error = 0;
1551 break;
1552 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
1553 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
1554 /*
1555 * When receiving a reply with CREATE set the original
1556 * persistent state message should already exist.
1557 */
1558 if (state == NULL) {
81666e1b
MD
1559 fprintf(stderr, "no-state(r) %s\n",
1560 hammer2_msg_str(msg));
78476205
MD
1561 error = HAMMER2_IOQ_ERROR_TRANS;
1562 break;
1563 }
7dc0f844
MD
1564 assert(((state->rxcmd ^ msg->any.head.cmd) &
1565 HAMMER2_MSGF_REPLY) == 0);
78476205
MD
1566 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
1567 error = 0;
1568 break;
1569 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
1570 /*
1571 * Received REPLY+ABORT+DELETE in case where msgid has
1572 * already been fully closed, ignore the message.
1573 */
1574 if (state == NULL) {
1575 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1576 error = HAMMER2_IOQ_ERROR_EALREADY;
1577 } else {
81666e1b
MD
1578 fprintf(stderr, "no-state(r,d) %s\n",
1579 hammer2_msg_str(msg));
78476205
MD
1580 error = HAMMER2_IOQ_ERROR_TRANS;
1581 }
1582 break;
1583 }
1584
1585 /*
1586 * Received REPLY+ABORT+DELETE in case where msgid has
1587 * already been reused for an unrelated message,
1588 * ignore the message.
1589 */
1590 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1591 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1592 error = HAMMER2_IOQ_ERROR_EALREADY;
1593 } else {
81666e1b
MD
1594 fprintf(stderr, "reused-state(r,d) %s\n",
1595 hammer2_msg_str(msg));
78476205
MD
1596 error = HAMMER2_IOQ_ERROR_TRANS;
1597 }
1598 break;
1599 }
1600 error = 0;
1601 break;
1602 case HAMMER2_MSGF_REPLY:
1603 /*
1604 * Check for mid-stream ABORT reply received to sent command.
1605 */
1606 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
1607 if (state == NULL ||
1608 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
1609 error = HAMMER2_IOQ_ERROR_EALREADY;
1610 break;
1611 }
1612 }
1613 error = 0;
1614 break;
1615 }
78476205
MD
1616 return (error);
1617}
1618
9ab15106 1619void
78476205 1620hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
9ab15106 1621{
78476205
MD
1622 hammer2_state_t *state;
1623
1624 if ((state = msg->state) == NULL) {
1b195a98
MD
1625 /*
1626 * Free a non-transactional message, there is no state
1627 * to worry about.
1628 */
78476205
MD
1629 hammer2_msg_free(iocom, msg);
1630 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
1b195a98
MD
1631 /*
1632 * Message terminating transaction, destroy the related
1633 * state, the original message, and this message (if it
1634 * isn't the original message due to a CREATE|DELETE).
1635 */
7dc0f844 1636 pthread_mutex_lock(&iocom->mtx);
78476205
MD
1637 state->rxcmd |= HAMMER2_MSGF_DELETE;
1638 if (state->txcmd & HAMMER2_MSGF_DELETE) {
1639 if (state->msg == msg)
1640 state->msg = NULL;
1641 assert(state->flags & HAMMER2_STATE_INSERTED);
7dc0f844
MD
1642 if (state->rxcmd & HAMMER2_MSGF_REPLY) {
1643 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
78476205
MD
1644 RB_REMOVE(hammer2_state_tree,
1645 &iocom->statewr_tree, state);
1646 } else {
7dc0f844 1647 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
78476205
MD
1648 RB_REMOVE(hammer2_state_tree,
1649 &iocom->staterd_tree, state);
1650 }
1651 state->flags &= ~HAMMER2_STATE_INSERTED;
78476205
MD
1652 hammer2_state_free(state);
1653 } else {
7dc0f844 1654 ;
78476205 1655 }
7dc0f844 1656 pthread_mutex_unlock(&iocom->mtx);
78476205
MD
1657 hammer2_msg_free(iocom, msg);
1658 } else if (state->msg != msg) {
1b195a98
MD
1659 /*
1660 * Message not terminating transaction, leave state intact
1661 * and free message if it isn't the CREATE message.
1662 */
78476205 1663 hammer2_msg_free(iocom, msg);
9ab15106 1664 }
78476205
MD
1665}
1666
78476205
MD
1667static void
1668hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
1669{
1670 hammer2_state_t *state;
1671
1672 if ((state = msg->state) == NULL) {
1673 hammer2_msg_free(iocom, msg);
1674 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
7dc0f844 1675 pthread_mutex_lock(&iocom->mtx);
78476205
MD
1676 state->txcmd |= HAMMER2_MSGF_DELETE;
1677 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
1678 if (state->msg == msg)
1679 state->msg = NULL;
1680 assert(state->flags & HAMMER2_STATE_INSERTED);
7dc0f844
MD
1681 if (state->txcmd & HAMMER2_MSGF_REPLY) {
1682 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
78476205
MD
1683 RB_REMOVE(hammer2_state_tree,
1684 &iocom->staterd_tree, state);
1685 } else {
7dc0f844 1686 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
78476205
MD
1687 RB_REMOVE(hammer2_state_tree,
1688 &iocom->statewr_tree, state);
1689 }
1690 state->flags &= ~HAMMER2_STATE_INSERTED;
78476205
MD
1691 hammer2_state_free(state);
1692 } else {
7dc0f844 1693 ;
78476205 1694 }
7dc0f844 1695 pthread_mutex_unlock(&iocom->mtx);
78476205
MD
1696 hammer2_msg_free(iocom, msg);
1697 } else if (state->msg != msg) {
1698 hammer2_msg_free(iocom, msg);
1699 }
1700}
1701
7dc0f844
MD
1702/*
1703 * Called with iocom locked
1704 */
78476205
MD
1705void
1706hammer2_state_free(hammer2_state_t *state)
1707{
1708 hammer2_iocom_t *iocom = state->iocom;
1709 hammer2_msg_t *msg;
7dc0f844
MD
1710 char dummy;
1711
81666e1b
MD
1712 if (DebugOpt) {
1713 fprintf(stderr, "terminate state id=%08x\n",
1714 (uint32_t)state->msgid);
1715 }
7dc0f844 1716 assert(state->any.any == NULL);
78476205
MD
1717 msg = state->msg;
1718 state->msg = NULL;
78476205 1719 if (msg)
7dc0f844 1720 hammer2_msg_free_locked(iocom, msg);
78476205 1721 free(state);
7dc0f844
MD
1722
1723 /*
1724 * When an iocom error is present we are trying to close down the
1725 * iocom, but we have to wait for all states to terminate before
1726 * we can do so. The iocom rx code will terminate the receive side
1727 * for all transactions by simulating incoming DELETE messages,
1728 * but the state doesn't go away until both sides are terminated.
1729 *
1730 * We may have to wake up the rx code.
1731 */
1732 if (iocom->ioq_rx.error &&
1733 RB_EMPTY(&iocom->staterd_tree) &&
1734 RB_EMPTY(&iocom->statewr_tree)) {
1735 dummy = 0;
1736 write(iocom->wakeupfds[1], &dummy, 1);
1737 }
78476205
MD
1738}
1739
1740/*
1741 * Indexed messages are stored in a red-black tree indexed by their
1742 * msgid. Only persistent messages are indexed.
1743 */
1744int
1745hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
1746{
8c280d5d 1747 if (state1->spanid < state2->spanid)
78476205 1748 return(-1);
8c280d5d 1749 if (state1->spanid > state2->spanid)
78476205
MD
1750 return(1);
1751 if (state1->msgid < state2->msgid)
1752 return(-1);
1753 if (state1->msgid > state2->msgid)
1754 return(1);
1755 return(0);
9ab15106 1756}
81666e1b
MD
1757
1758const char *
1759hammer2_basecmd_str(uint32_t cmd)
1760{
1761 static char buf[64];
1762 char protobuf[32];
1763 char cmdbuf[32];
1764 const char *protostr;
1765 const char *cmdstr;
1766
1767 switch(cmd & HAMMER2_MSGF_PROTOS) {
1768 case HAMMER2_MSG_PROTO_LNK:
1769 protostr = "LNK_";
1770 break;
1771 case HAMMER2_MSG_PROTO_DBG:
1772 protostr = "DBG_";
1773 break;
1774 case HAMMER2_MSG_PROTO_DOM:
1775 protostr = "DOM_";
1776 break;
1777 case HAMMER2_MSG_PROTO_CAC:
1778 protostr = "CAC_";
1779 break;
1780 case HAMMER2_MSG_PROTO_QRM:
1781 protostr = "QRM_";
1782 break;
1783 case HAMMER2_MSG_PROTO_BLK:
1784 protostr = "BLK_";
1785 break;
1786 case HAMMER2_MSG_PROTO_VOP:
1787 protostr = "VOP_";
1788 break;
1789 default:
1790 snprintf(protobuf, sizeof(protobuf), "%x_",
1791 (cmd & HAMMER2_MSGF_PROTOS) >> 20);
1792 protostr = protobuf;
1793 break;
1794 }
1795
1796 switch(cmd & (HAMMER2_MSGF_PROTOS |
1797 HAMMER2_MSGF_CMDS |
1798 HAMMER2_MSGF_SIZE)) {
1799 case HAMMER2_LNK_PAD:
1800 cmdstr = "PAD";
1801 break;
1802 case HAMMER2_LNK_PING:
1803 cmdstr = "PING";
1804 break;
1805 case HAMMER2_LNK_AUTH:
1806 cmdstr = "AUTH";
1807 break;
1808 case HAMMER2_LNK_CONN:
1809 cmdstr = "CONN";
1810 break;
1811 case HAMMER2_LNK_SPAN:
1812 cmdstr = "SPAN";
1813 break;
1814 case HAMMER2_LNK_ERROR:
1815 if (cmd & HAMMER2_MSGF_DELETE)
1816 cmdstr = "RETURN";
1817 else
1818 cmdstr = "RESULT";
1819 break;
1820 case HAMMER2_DBG_SHELL:
1821 cmdstr = "SHELL";
1822 break;
1823 default:
1824 snprintf(cmdbuf, sizeof(cmdbuf),
1825 "%06x", (cmd & (HAMMER2_MSGF_PROTOS |
1826 HAMMER2_MSGF_CMDS |
1827 HAMMER2_MSGF_SIZE)));
1828 cmdstr = cmdbuf;
1829 break;
1830 }
1831 snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
1832 return (buf);
1833}
1834
1835const char *
1836hammer2_msg_str(hammer2_msg_t *msg)
1837{
1838 hammer2_state_t *state;
1839 static char buf[256];
1840 char errbuf[16];
1841 char statebuf[64];
1842 char flagbuf[64];
1843 const char *statestr;
1844 const char *errstr;
1845 uint32_t basecmd;
1846 int i;
1847
1848 /*
1849 * Parse the state
1850 */
1851 if ((state = msg->state) != NULL) {
1852 basecmd = (state->rxcmd & HAMMER2_MSGF_REPLY) ?
1853 state->txcmd : state->rxcmd;
1854 snprintf(statebuf, sizeof(statebuf),
1855 " %s=%s,L=%s%s,R=%s%s",
1856 ((state->txcmd & HAMMER2_MSGF_REPLY) ?
1857 "rcvcmd" : "sndcmd"),
1858 hammer2_basecmd_str(basecmd),
1859 ((state->txcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1860 ((state->txcmd & HAMMER2_MSGF_DELETE) ? "D" : ""),
1861 ((state->rxcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
1862 ((state->rxcmd & HAMMER2_MSGF_DELETE) ? "D" : "")
1863 );
1864 statestr = statebuf;
1865 } else {
1866 statestr = "";
1867 }
1868
1869 /*
1870 * Parse the error
1871 */
1872 switch(msg->any.head.error) {
1873 case 0:
1874 errstr = "";
1875 break;
1876 case HAMMER2_IOQ_ERROR_SYNC:
1877 errstr = "err=IOQ:NOSYNC";
1878 break;
1879 case HAMMER2_IOQ_ERROR_EOF:
1880 errstr = "err=IOQ:STREAMEOF";
1881 break;
1882 case HAMMER2_IOQ_ERROR_SOCK:
1883 errstr = "err=IOQ:SOCKERR";
1884 break;
1885 case HAMMER2_IOQ_ERROR_FIELD:
1886 errstr = "err=IOQ:BADFIELD";
1887 break;
1888 case HAMMER2_IOQ_ERROR_HCRC:
1889 errstr = "err=IOQ:BADHCRC";
1890 break;
1891 case HAMMER2_IOQ_ERROR_XCRC:
1892 errstr = "err=IOQ:BADXCRC";
1893 break;
1894 case HAMMER2_IOQ_ERROR_ACRC:
1895 errstr = "err=IOQ:BADACRC";
1896 break;
1897 case HAMMER2_IOQ_ERROR_STATE:
1898 errstr = "err=IOQ:BADSTATE";
1899 break;
1900 case HAMMER2_IOQ_ERROR_NOPEER:
1901 errstr = "err=IOQ:PEERCONFIG";
1902 break;
1903 case HAMMER2_IOQ_ERROR_NORKEY:
1904 errstr = "err=IOQ:BADRKEY";
1905 break;
1906 case HAMMER2_IOQ_ERROR_NOLKEY:
1907 errstr = "err=IOQ:BADLKEY";
1908 break;
1909 case HAMMER2_IOQ_ERROR_KEYXCHGFAIL:
1910 errstr = "err=IOQ:BADKEYXCHG";
1911 break;
1912 case HAMMER2_IOQ_ERROR_KEYFMT:
1913 errstr = "err=IOQ:BADFMT";
1914 break;
1915 case HAMMER2_IOQ_ERROR_BADURANDOM:
1916 errstr = "err=IOQ:BADRANDOM";
1917 break;
1918 case HAMMER2_IOQ_ERROR_MSGSEQ:
1919 errstr = "err=IOQ:BADSEQ";
1920 break;
1921 case HAMMER2_IOQ_ERROR_EALREADY:
1922 errstr = "err=IOQ:DUPMSG";
1923 break;
1924 case HAMMER2_IOQ_ERROR_TRANS:
1925 errstr = "err=IOQ:BADTRANS";
1926 break;
1927 case HAMMER2_MSG_ERR_NOSUPP:
1928 errstr = "err=NOSUPPORT";
1929 break;
1930 default:
1931 snprintf(errbuf, sizeof(errbuf),
1932 " err=%d", msg->any.head.error);
1933 errstr = errbuf;
1934 break;
1935 }
1936
1937 /*
1938 * Message flags
1939 */
1940 i = 0;
1941 if (msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
1942 HAMMER2_MSGF_ABORT | HAMMER2_MSGF_REPLY)) {
1943 flagbuf[i++] = '|';
1944 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
1945 flagbuf[i++] = 'C';
1946 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
1947 flagbuf[i++] = 'D';
1948 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY)
1949 flagbuf[i++] = 'R';
1950 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT)
1951 flagbuf[i++] = 'A';
1952 }
1953 flagbuf[i] = 0;
1954
1955 /*
1956 * Generate the buf
1957 */
1958 snprintf(buf, sizeof(buf),
1959 "msg=%s%s %s id=%08x span=%08x %s",
1960 hammer2_basecmd_str(msg->any.head.cmd),
1961 flagbuf,
1962 errstr,
1963 (uint32_t)(intmax_t)msg->any.head.msgid, /* for brevity */
1964 (uint32_t)(intmax_t)msg->any.head.spanid, /* for brevity */
1965 statestr);
1966
1967 return(buf);
1968}