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