2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * LNK_SPAN PROTOCOL SUPPORT FUNCTIONS - Please see sys/dmsg.h for an
36 * involved explanation of the protocol.
39 #include "dmsg_local.h"
41 void (*dmsg_node_handler)(void **opaquep, struct dmsg_msg *msg, int op);
44 * Maximum spanning tree distance. This has the practical effect of
45 * stopping tail-chasing closed loops when a feeder span is lost.
47 #define DMSG_SPAN_MAXDIST 16
50 * RED-BLACK TREE DEFINITIONS
54 * (1) shared fsid's (a cluster).
55 * (2) unique fsid's (a node in a cluster) <--- LNK_SPAN transactions.
57 * We need to aggegate all active LNK_SPANs, aggregate, and create our own
58 * outgoing LNK_SPAN transactions on each of our connections representing
59 * the aggregated state.
61 * h2span_conn - list of iocom connections who wish to receive SPAN
62 * propagation from other connections. Might contain
63 * a filter string. Only iocom's with an open
64 * LNK_CONN transactions are applicable for SPAN
67 * h2span_relay - List of links relayed (via SPAN). Essentially
68 * each relay structure represents a LNK_SPAN
69 * transaction that we initiated, verses h2span_link
70 * which is a LNK_SPAN transaction that we received.
74 * h2span_cluster - Organizes the shared fsid's. One structure for
77 * h2span_node - Organizes the nodes in a cluster. One structure
78 * for each unique {cluster,node}, aka {fsid, pfs_fsid}.
80 * h2span_link - Organizes all incoming and outgoing LNK_SPAN message
81 * transactions related to a node.
83 * One h2span_link structure for each incoming LNK_SPAN
84 * transaction. Links selected for propagation back
85 * out are also where the outgoing LNK_SPAN messages
86 * are indexed into (so we can propagate changes).
88 * The h2span_link's use a red-black tree to sort the
89 * distance hop metric for the incoming LNK_SPAN. We
90 * then select the top N for outgoing. When the
91 * topology changes the top N may also change and cause
92 * new outgoing LNK_SPAN transactions to be opened
93 * and less desireable ones to be closed, causing
94 * transactional aborts within the message flow in
97 * Also note - All outgoing LNK_SPAN message transactions are also
98 * entered into a red-black tree for use by the routing
99 * function. This is handled by msg.c in the state
105 TAILQ_HEAD(h2span_media_queue, h2span_media);
106 TAILQ_HEAD(h2span_conn_queue, h2span_conn);
107 TAILQ_HEAD(h2span_relay_queue, h2span_relay);
109 RB_HEAD(h2span_cluster_tree, h2span_cluster);
110 RB_HEAD(h2span_node_tree, h2span_node);
111 RB_HEAD(h2span_link_tree, h2span_link);
112 RB_HEAD(h2span_relay_tree, h2span_relay);
116 * This represents a media
118 struct h2span_media {
119 TAILQ_ENTRY(h2span_media) entry;
122 struct h2span_media_config {
123 dmsg_vol_data_t copy_run;
124 dmsg_vol_data_t copy_pend;
129 int pipefd[2]; /* signal stop */
131 pthread_t iocom_thread;
132 enum { H2MC_STOPPED, H2MC_CONNECT, H2MC_RUNNING } state;
133 } config[DMSG_COPYID_COUNT];
136 typedef struct h2span_media_config h2span_media_config_t;
138 #define H2CONFCTL_STOP 0x00000001
139 #define H2CONFCTL_UPDATE 0x00000002
142 * Received LNK_CONN transaction enables SPAN protocol over connection.
143 * (may contain filter). Typically one for each mount and several may
144 * share the same media.
147 TAILQ_ENTRY(h2span_conn) entry;
148 struct h2span_relay_tree tree;
149 struct h2span_media *media;
154 * All received LNK_SPANs are organized by cluster (pfs_clid),
155 * node (pfs_fsid), and link (received LNK_SPAN transaction).
157 struct h2span_cluster {
158 RB_ENTRY(h2span_cluster) rbnode;
159 struct h2span_node_tree tree;
160 uuid_t pfs_clid; /* shared fsid */
162 char cl_label[128]; /* cluster label (typ PEER_BLOCK) */
163 int refs; /* prevents destruction */
167 RB_ENTRY(h2span_node) rbnode;
168 struct h2span_link_tree tree;
169 struct h2span_cluster *cls;
171 uuid_t pfs_fsid; /* unique fsid */
172 char fs_label[128]; /* fs label (typ PEER_HAMMER2) */
177 RB_ENTRY(h2span_link) rbnode;
178 dmsg_state_t *state; /* state<->link */
179 struct h2span_node *node; /* related node */
182 struct h2span_relay_queue relayq; /* relay out */
186 * Any LNK_SPAN transactions we receive which are relayed out other
187 * connections utilize this structure to track the LNK_SPAN transactions
188 * we initiate (relay out) on other connections. We only relay out
189 * LNK_SPANs on connections we have an open CONN transaction for.
191 * The relay structure points to the outgoing LNK_SPAN trans (out_state)
192 * and to the incoming LNK_SPAN transaction (in_state). The relay
193 * structure holds refs on the related states.
195 * In many respects this is the core of the protocol... actually figuring
196 * out what LNK_SPANs to relay. The spanid used for relaying is the
197 * address of the 'state' structure, which is why h2span_relay has to
198 * be entered into a RB-TREE based at h2span_conn (so we can look
199 * up the spanid to validate it).
201 struct h2span_relay {
202 TAILQ_ENTRY(h2span_relay) entry; /* from link */
203 RB_ENTRY(h2span_relay) rbnode; /* from h2span_conn */
204 struct h2span_conn *conn; /* related CONN transaction */
205 dmsg_state_t *source_rt; /* h2span_link state */
206 dmsg_state_t *target_rt; /* h2span_relay state */
209 typedef struct h2span_media h2span_media_t;
210 typedef struct h2span_conn h2span_conn_t;
211 typedef struct h2span_cluster h2span_cluster_t;
212 typedef struct h2span_node h2span_node_t;
213 typedef struct h2span_link h2span_link_t;
214 typedef struct h2span_relay h2span_relay_t;
216 #define dmsg_termstr(array) _dmsg_termstr((array), sizeof(array))
218 static h2span_relay_t *dmsg_generate_relay(h2span_conn_t *conn,
219 h2span_link_t *slink);
220 static uint32_t dmsg_rnss(void);
224 _dmsg_termstr(char *base, size_t size)
230 * Cluster peer_type, uuid, AND label must match for a match
234 h2span_cluster_cmp(h2span_cluster_t *cls1, h2span_cluster_t *cls2)
238 if (cls1->peer_type < cls2->peer_type)
240 if (cls1->peer_type > cls2->peer_type)
242 r = uuid_compare(&cls1->pfs_clid, &cls2->pfs_clid, NULL);
244 r = strcmp(cls1->cl_label, cls2->cl_label);
250 * Match against fs_label/pfs_fsid. Together these two items represent a
251 * unique node. In most cases the primary differentiator is pfs_fsid but
252 * we also string-match fs_label.
256 h2span_node_cmp(h2span_node_t *node1, h2span_node_t *node2)
260 r = strcmp(node1->fs_label, node2->fs_label);
262 r = uuid_compare(&node1->pfs_fsid, &node2->pfs_fsid, NULL);
267 * Sort/subsort must match h2span_relay_cmp() under any given node
268 * to make the aggregation algorithm easier, so the best links are
269 * in the same sorted order as the best relays.
271 * NOTE: We cannot use link*->state->msgid because this msgid is created
272 * by each remote host and thus might wind up being the same.
276 h2span_link_cmp(h2span_link_t *link1, h2span_link_t *link2)
278 if (link1->dist < link2->dist)
280 if (link1->dist > link2->dist)
282 if (link1->rnss < link2->rnss)
284 if (link1->rnss > link2->rnss)
287 if ((uintptr_t)link1->state < (uintptr_t)link2->state)
289 if ((uintptr_t)link1->state > (uintptr_t)link2->state)
292 if (link1->state->msgid < link2->state->msgid)
294 if (link1->state->msgid > link2->state->msgid)
301 * Relay entries are sorted by node, subsorted by distance and link
302 * address (so we can match up the conn->tree relay topology with
303 * a node's link topology).
307 h2span_relay_cmp(h2span_relay_t *relay1, h2span_relay_t *relay2)
309 h2span_link_t *link1 = relay1->source_rt->any.link;
310 h2span_link_t *link2 = relay2->source_rt->any.link;
312 if ((intptr_t)link1->node < (intptr_t)link2->node)
314 if ((intptr_t)link1->node > (intptr_t)link2->node)
316 if (link1->dist < link2->dist)
318 if (link1->dist > link2->dist)
320 if (link1->rnss < link2->rnss)
322 if (link1->rnss > link2->rnss)
325 if ((uintptr_t)link1->state < (uintptr_t)link2->state)
327 if ((uintptr_t)link1->state > (uintptr_t)link2->state)
330 if (link1->state->msgid < link2->state->msgid)
332 if (link1->state->msgid > link2->state->msgid)
338 RB_PROTOTYPE_STATIC(h2span_cluster_tree, h2span_cluster,
339 rbnode, h2span_cluster_cmp);
340 RB_PROTOTYPE_STATIC(h2span_node_tree, h2span_node,
341 rbnode, h2span_node_cmp);
342 RB_PROTOTYPE_STATIC(h2span_link_tree, h2span_link,
343 rbnode, h2span_link_cmp);
344 RB_PROTOTYPE_STATIC(h2span_relay_tree, h2span_relay,
345 rbnode, h2span_relay_cmp);
347 RB_GENERATE_STATIC(h2span_cluster_tree, h2span_cluster,
348 rbnode, h2span_cluster_cmp);
349 RB_GENERATE_STATIC(h2span_node_tree, h2span_node,
350 rbnode, h2span_node_cmp);
351 RB_GENERATE_STATIC(h2span_link_tree, h2span_link,
352 rbnode, h2span_link_cmp);
353 RB_GENERATE_STATIC(h2span_relay_tree, h2span_relay,
354 rbnode, h2span_relay_cmp);
357 * Global mutex protects cluster_tree lookups, connq, mediaq.
359 static pthread_mutex_t cluster_mtx;
360 static struct h2span_cluster_tree cluster_tree = RB_INITIALIZER(cluster_tree);
361 static struct h2span_conn_queue connq = TAILQ_HEAD_INITIALIZER(connq);
362 static struct h2span_media_queue mediaq = TAILQ_HEAD_INITIALIZER(mediaq);
364 static void dmsg_lnk_span(dmsg_msg_t *msg);
365 static void dmsg_lnk_conn(dmsg_msg_t *msg);
366 static void dmsg_lnk_circ(dmsg_msg_t *msg);
367 static void dmsg_lnk_relay(dmsg_msg_t *msg);
368 static void dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node);
369 static void dmsg_relay_delete(h2span_relay_t *relay);
371 static void *dmsg_volconf_thread(void *info);
372 static void dmsg_volconf_stop(h2span_media_config_t *conf);
373 static void dmsg_volconf_start(h2span_media_config_t *conf,
374 const char *hostname);
377 dmsg_msg_lnk_signal(dmsg_iocom_t *iocom __unused)
379 pthread_mutex_lock(&cluster_mtx);
380 dmsg_relay_scan(NULL, NULL);
381 pthread_mutex_unlock(&cluster_mtx);
385 * DMSG_PROTO_LNK - Generic DMSG_PROTO_LNK.
386 * (incoming iocom lock not held)
388 * This function is typically called for one-way and opening-transactions
389 * since state->func is assigned after that, but it will also be called
390 * if no state->func is assigned on transaction-open.
393 dmsg_msg_lnk(dmsg_msg_t *msg)
395 uint32_t icmd = msg->state ? msg->state->icmd : msg->any.head.cmd;
397 switch(icmd & DMSGF_BASECMDMASK) {
409 "MSG_PROTO_LNK: Unknown msg %08x\n", msg->any.head.cmd);
410 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
411 /* state invalid after reply */
417 * LNK_CONN - iocom identify message reception.
418 * (incoming iocom lock not held)
420 * Remote node identifies itself to us, sets up a SPAN filter, and gives us
421 * the ok to start transmitting SPANs.
424 dmsg_lnk_conn(dmsg_msg_t *msg)
426 dmsg_state_t *state = msg->state;
427 h2span_media_t *media;
428 h2span_media_config_t *conf;
430 h2span_relay_t *relay;
434 pthread_mutex_lock(&cluster_mtx);
436 fprintf(stderr, "dmsg_lnk_conn: msg %p cmd %08x state %p txcmd %08x rxcmd %08x\n",
437 msg, msg->any.head.cmd, state, state->txcmd, state->rxcmd);
439 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
440 case DMSG_LNK_CONN | DMSGF_CREATE:
441 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE:
443 * On transaction start we allocate a new h2span_conn and
444 * acknowledge the request, leaving the transaction open.
445 * We then relay priority-selected SPANs.
447 fprintf(stderr, "LNK_CONN(%08x): %s/%s/%s\n",
448 (uint32_t)msg->any.head.msgid,
449 dmsg_uuid_to_str(&msg->any.lnk_conn.pfs_clid,
451 msg->any.lnk_conn.cl_label,
452 msg->any.lnk_conn.fs_label);
455 conn = dmsg_alloc(sizeof(*conn));
457 RB_INIT(&conn->tree);
458 state->iocom->conn = conn; /* XXX only one */
460 state->func = dmsg_lnk_conn;
461 state->any.conn = conn;
462 TAILQ_INSERT_TAIL(&connq, conn, entry);
467 TAILQ_FOREACH(media, &mediaq, entry) {
468 if (uuid_compare(&msg->any.lnk_conn.mediaid,
469 &media->mediaid, NULL) == 0) {
474 media = dmsg_alloc(sizeof(*media));
475 media->mediaid = msg->any.lnk_conn.mediaid;
476 TAILQ_INSERT_TAIL(&mediaq, media, entry);
481 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
482 dmsg_msg_result(msg, 0);
483 dmsg_iocom_signal(msg->iocom);
487 case DMSG_LNK_CONN | DMSGF_DELETE:
488 case DMSG_LNK_ERROR | DMSGF_DELETE:
491 * On transaction terminate we clean out our h2span_conn
492 * and acknowledge the request, closing the transaction.
494 fprintf(stderr, "LNK_CONN: Terminated\n");
495 conn = state->any.conn;
499 * Clean out the media structure. If refs drops to zero we
500 * also clean out the media config threads. These threads
501 * maintain span connections to other hammer2 service daemons.
504 if (--media->refs == 0) {
505 fprintf(stderr, "Shutting down media spans\n");
506 for (i = 0; i < DMSG_COPYID_COUNT; ++i) {
507 conf = &media->config[i];
509 if (conf->thread == NULL)
511 conf->ctl = H2CONFCTL_STOP;
512 pthread_cond_signal(&conf->cond);
514 for (i = 0; i < DMSG_COPYID_COUNT; ++i) {
515 conf = &media->config[i];
517 if (conf->thread == NULL)
519 pthread_mutex_unlock(&cluster_mtx);
520 pthread_join(conf->thread, NULL);
521 pthread_mutex_lock(&cluster_mtx);
523 pthread_cond_destroy(&conf->cond);
525 fprintf(stderr, "Media shutdown complete\n");
526 TAILQ_REMOVE(&mediaq, media, entry);
531 * Clean out all relays. This requires terminating each
534 while ((relay = RB_ROOT(&conn->tree)) != NULL) {
535 dmsg_relay_delete(relay);
543 msg->state->any.conn = NULL;
544 msg->state->iocom->conn = NULL;
545 TAILQ_REMOVE(&connq, conn, entry);
548 dmsg_msg_reply(msg, 0);
549 /* state invalid after reply */
551 case DMSG_LNK_VOLCONF:
553 * One-way volume-configuration message is transmitted
554 * over the open LNK_CONN transaction.
556 fprintf(stderr, "RECEIVED VOLCONF\n");
557 if (msg->any.lnk_volconf.index < 0 ||
558 msg->any.lnk_volconf.index >= DMSG_COPYID_COUNT) {
559 fprintf(stderr, "VOLCONF: ILLEGAL INDEX %d\n",
560 msg->any.lnk_volconf.index);
563 if (msg->any.lnk_volconf.copy.path[sizeof(msg->any.lnk_volconf.copy.path) - 1] != 0 ||
564 msg->any.lnk_volconf.copy.path[0] == 0) {
565 fprintf(stderr, "VOLCONF: ILLEGAL PATH %d\n",
566 msg->any.lnk_volconf.index);
569 conn = msg->state->any.conn;
571 fprintf(stderr, "VOLCONF: LNK_CONN is missing\n");
574 conf = &conn->media->config[msg->any.lnk_volconf.index];
575 conf->copy_pend = msg->any.lnk_volconf.copy;
576 conf->ctl |= H2CONFCTL_UPDATE;
577 if (conf->thread == NULL) {
578 fprintf(stderr, "VOLCONF THREAD STARTED\n");
579 pthread_cond_init(&conf->cond, NULL);
580 pthread_create(&conf->thread, NULL,
581 dmsg_volconf_thread, (void *)conf);
583 pthread_cond_signal(&conf->cond);
589 if (msg->any.head.cmd & DMSGF_DELETE)
591 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
594 pthread_mutex_unlock(&cluster_mtx);
598 * LNK_SPAN - Spanning tree protocol message reception
599 * (incoming iocom lock not held)
601 * Receive a spanning tree transactional message, creating or destroying
602 * a SPAN and propagating it to other iocoms.
605 dmsg_lnk_span(dmsg_msg_t *msg)
607 dmsg_state_t *state = msg->state;
608 h2span_cluster_t dummy_cls;
609 h2span_node_t dummy_node;
610 h2span_cluster_t *cls;
612 h2span_link_t *slink;
613 h2span_relay_t *relay;
616 assert((msg->any.head.cmd & DMSGF_REPLY) == 0);
618 pthread_mutex_lock(&cluster_mtx);
621 * On transaction start we initialize the tracking infrastructure
623 if (msg->any.head.cmd & DMSGF_CREATE) {
624 assert(state->func == NULL);
625 state->func = dmsg_lnk_span;
627 dmsg_termstr(msg->any.lnk_span.cl_label);
628 dmsg_termstr(msg->any.lnk_span.fs_label);
633 dummy_cls.pfs_clid = msg->any.lnk_span.pfs_clid;
634 dummy_cls.peer_type = msg->any.lnk_span.peer_type;
635 bcopy(msg->any.lnk_span.cl_label,
637 sizeof(dummy_cls.cl_label));
638 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls);
640 cls = dmsg_alloc(sizeof(*cls));
641 cls->pfs_clid = msg->any.lnk_span.pfs_clid;
642 cls->peer_type = msg->any.lnk_span.peer_type;
643 bcopy(msg->any.lnk_span.cl_label,
645 sizeof(cls->cl_label));
647 RB_INSERT(h2span_cluster_tree, &cluster_tree, cls);
653 dummy_node.pfs_fsid = msg->any.lnk_span.pfs_fsid;
654 bcopy(msg->any.lnk_span.fs_label, dummy_node.fs_label,
655 sizeof(dummy_node.fs_label));
656 node = RB_FIND(h2span_node_tree, &cls->tree, &dummy_node);
658 node = dmsg_alloc(sizeof(*node));
659 node->pfs_fsid = msg->any.lnk_span.pfs_fsid;
660 node->pfs_type = msg->any.lnk_span.pfs_type;
661 bcopy(msg->any.lnk_span.fs_label,
663 sizeof(node->fs_label));
665 RB_INIT(&node->tree);
666 RB_INSERT(h2span_node_tree, &cls->tree, node);
667 if (dmsg_node_handler) {
668 dmsg_node_handler(&node->opaque, msg,
676 assert(state->any.link == NULL);
677 slink = dmsg_alloc(sizeof(*slink));
678 TAILQ_INIT(&slink->relayq);
680 slink->dist = msg->any.lnk_span.dist;
681 slink->rnss = msg->any.lnk_span.rnss;
682 slink->state = state;
683 state->any.link = slink;
685 RB_INSERT(h2span_link_tree, &node->tree, slink);
688 "LNK_SPAN(thr %p): %p %s cl=%s fs=%s dist=%d\n",
691 dmsg_uuid_to_str(&msg->any.lnk_span.pfs_clid, &alloc),
692 msg->any.lnk_span.cl_label,
693 msg->any.lnk_span.fs_label,
694 msg->any.lnk_span.dist);
697 dmsg_relay_scan(NULL, node);
699 dmsg_iocom_signal(msg->iocom);
703 * On transaction terminate we remove the tracking infrastructure.
705 if (msg->any.head.cmd & DMSGF_DELETE) {
706 slink = state->any.link;
707 assert(slink != NULL);
711 fprintf(stderr, "LNK_DELE(thr %p): %p %s cl=%s fs=%s dist=%d\n",
714 dmsg_uuid_to_str(&cls->pfs_clid, &alloc),
715 state->msg->any.lnk_span.cl_label,
716 state->msg->any.lnk_span.fs_label,
717 state->msg->any.lnk_span.dist);
721 * Clean out all relays. This requires terminating each
724 while ((relay = TAILQ_FIRST(&slink->relayq)) != NULL) {
725 dmsg_relay_delete(relay);
729 * Clean out the topology
731 RB_REMOVE(h2span_link_tree, &node->tree, slink);
732 if (RB_EMPTY(&node->tree)) {
733 RB_REMOVE(h2span_node_tree, &cls->tree, node);
734 if (dmsg_node_handler) {
735 dmsg_node_handler(&node->opaque, msg,
738 if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
739 RB_REMOVE(h2span_cluster_tree,
747 state->any.link = NULL;
753 * We have to terminate the transaction
755 dmsg_state_reply(state, 0);
756 /* state invalid after reply */
759 * If the node still exists issue any required updates. If
760 * it doesn't then all related relays have already been
761 * removed and there's nothing left to do.
765 dmsg_relay_scan(NULL, node);
768 dmsg_iocom_signal(msg->iocom);
771 pthread_mutex_unlock(&cluster_mtx);
775 * LNK_CIRC - Virtual circuit protocol message reception
776 * (incoming iocom lock not held)
781 dmsg_lnk_circ(dmsg_msg_t *msg)
783 dmsg_circuit_t *circA;
784 dmsg_circuit_t *circB;
785 dmsg_state_t *rx_state;
786 dmsg_state_t *tx_state;
790 dmsg_iocom_t *iocomA;
791 dmsg_iocom_t *iocomB;
794 /*pthread_mutex_lock(&cluster_mtx);*/
796 if (DMsgDebugOpt >= 4)
797 fprintf(stderr, "CIRC receive cmd=%08x\n", msg->any.head.cmd);
799 switch (msg->any.head.cmd & (DMSGF_CREATE |
803 case DMSGF_CREATE | DMSGF_DELETE:
805 * (A) wishes to establish a virtual circuit through us to (B).
806 * (B) is specified by lnk_circ.target (the message id for
807 * a LNK_SPAN that (A) received from us which represents (B)).
809 * Designate the originator of the circuit (the current
810 * remote end) as (A) and the other side as (B).
812 * Accept the VC but do not reply. We will wait for the end-
813 * to-end reply to propagate back.
818 * Locate the open transaction state that the other end
819 * specified in <target>. This will be an open SPAN
820 * transaction that we transmitted (h2span_relay) over
821 * the interface the LNK_CIRC is being received on.
823 * (all LNK_CIRC's that we transmit are on circuit0)
825 pthread_mutex_lock(&iocomA->mtx);
826 dummy.msgid = msg->any.lnk_circ.target;
827 tx_state = RB_FIND(dmsg_state_tree,
828 &iocomA->circuit0.statewr_tree,
830 pthread_mutex_unlock(&iocomA->mtx);
831 if (tx_state == NULL) {
833 fprintf(stderr, "dmsg_lnk_circ: no circuit\n");
834 dmsg_msg_reply(msg, DMSG_ERR_CANTCIRC);
837 if (tx_state->icmd != DMSG_LNK_SPAN) {
839 fprintf(stderr, "dmsg_lnk_circ: not LNK_SPAN\n");
840 dmsg_msg_reply(msg, DMSG_ERR_CANTCIRC);
844 /* locate h2span_link */
845 rx_state = tx_state->any.relay->source_rt;
848 * A wishes to establish a VC through us to the
851 * A sends us the msgid of an open SPAN transaction
852 * it received from us as <target>.
854 circA = dmsg_alloc(sizeof(*circA));
855 dmsg_circuit_init(iocomA, circA);
856 circA->state = msg->state; /* LNK_CIRC state */
857 circA->msgid = msg->state->msgid;
858 circA->span_state = tx_state; /* H2SPAN_RELAY state */
860 circA->refs = 2; /* state and peer */
863 * Upgrade received state so we act on both it and its
864 * peer (created below) symmetrically.
866 msg->state->any.circ = circA;
867 msg->state->func = dmsg_lnk_circ;
869 iocomB = rx_state->iocom;
871 circB = dmsg_alloc(sizeof(*circB));
872 dmsg_circuit_init(iocomB, circB);
875 * Create a LNK_CIRC transaction on B
877 fwd_msg = dmsg_msg_alloc(&iocomB->circuit0,
878 0, DMSG_LNK_CIRC | DMSGF_CREATE,
879 dmsg_lnk_circ, circB);
880 fwd_msg->state->any.circ = circB;
881 fwd_msg->any.lnk_circ.target = rx_state->msgid;
882 circB->state = fwd_msg->state; /* LNK_CIRC state */
883 circB->msgid = fwd_msg->any.head.msgid;
884 circB->span_state = rx_state; /* H2SPAN_LINK state */
886 circB->refs = 2; /* state and peer */
888 if (DMsgDebugOpt >= 4)
889 fprintf(stderr, "CIRC forward %p->%p\n", circA, circB);
892 * Link the two circuits together.
897 if (iocomA < iocomB) {
898 pthread_mutex_lock(&iocomA->mtx);
899 pthread_mutex_lock(&iocomB->mtx);
901 pthread_mutex_lock(&iocomB->mtx);
902 pthread_mutex_lock(&iocomA->mtx);
904 if (RB_INSERT(dmsg_circuit_tree, &iocomA->circuit_tree, circA))
906 if (RB_INSERT(dmsg_circuit_tree, &iocomB->circuit_tree, circB))
908 if (iocomA < iocomB) {
909 pthread_mutex_unlock(&iocomB->mtx);
910 pthread_mutex_unlock(&iocomA->mtx);
912 pthread_mutex_unlock(&iocomA->mtx);
913 pthread_mutex_unlock(&iocomB->mtx);
916 dmsg_msg_write(fwd_msg);
918 if ((msg->any.head.cmd & DMSGF_DELETE) == 0)
920 /* FALL THROUGH TO DELETE */
923 * (A) Is deleting the virtual circuit, propogate closure
927 if (msg->state->any.circ == NULL) {
928 /* already returned an error/deleted */
931 circA = msg->state->any.circ;
933 assert(msg->state == circA->state);
936 * We are closing B's send side. If B's receive side is
937 * already closed we disconnect the circuit from B's state.
940 if (circB && (state = circB->state) != NULL) {
941 if (state->rxcmd & DMSGF_DELETE) {
944 state->any.circ = NULL;
945 dmsg_circuit_drop(circB);
947 dmsg_state_reply(state, msg->any.head.error);
951 * We received a close on A. If A's send side is already
952 * closed we disconnect the circuit from A's state.
954 if (circA && (state = circA->state) != NULL) {
955 if (state->txcmd & DMSGF_DELETE) {
958 state->any.circ = NULL;
959 dmsg_circuit_drop(circA);
964 * Disconnect the peer<->peer association
970 dmsg_circuit_drop(circA);
971 dmsg_circuit_drop(circB); /* XXX SMP */
975 case DMSGF_REPLY | DMSGF_CREATE:
976 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
978 * (B) is acknowledging the creation of the virtual
979 * circuit. This propagates all the way back to (A), though
980 * it should be noted that (A) can start issuing commands
981 * via the virtual circuit before seeing this reply.
983 circB = msg->state->any.circ;
986 assert(msg->state == circB->state);
988 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
989 dmsg_state_result(circA->state, msg->any.head.error);
992 /* FALL THROUGH TO DELETE */
993 case DMSGF_REPLY | DMSGF_DELETE:
995 * (B) Is deleting the virtual circuit or acknowledging
996 * our deletion of the virtual circuit, propogate closure
1000 circB = msg->state->any.circ;
1001 circA = circB->peer;
1002 assert(msg->state == circB->state);
1005 * We received a close on (B), propagate to (A). If we have
1006 * already received the close from (A) we disconnect the state.
1009 if (circA && (state = circA->state) != NULL) {
1010 if (state->rxcmd & DMSGF_DELETE) {
1012 circA->state = NULL;
1013 state->any.circ = NULL;
1014 dmsg_circuit_drop(circA);
1016 dmsg_state_reply(state, msg->any.head.error);
1020 * We received a close on (B). If (B)'s send side is already
1021 * closed we disconnect the state.
1023 if (circB && (state = circB->state) != NULL) {
1024 if (state->txcmd & DMSGF_DELETE) {
1026 circB->state = NULL;
1027 state->any.circ = NULL;
1028 dmsg_circuit_drop(circB);
1033 * Disconnect the peer<->peer association
1039 dmsg_circuit_drop(circB);
1040 dmsg_circuit_drop(circA); /* XXX SMP */
1046 /*pthread_mutex_lock(&cluster_mtx);*/
1050 * Update relay transactions for SPANs.
1052 * Called with cluster_mtx held.
1054 static void dmsg_relay_scan_specific(h2span_node_t *node,
1055 h2span_conn_t *conn);
1058 dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node)
1060 h2span_cluster_t *cls;
1064 * Iterate specific node
1066 TAILQ_FOREACH(conn, &connq, entry)
1067 dmsg_relay_scan_specific(node, conn);
1072 * Iterate cluster ids, nodes, and either a specific connection
1073 * or all connections.
1075 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
1079 RB_FOREACH(node, h2span_node_tree, &cls->tree) {
1081 * Synchronize the node's link (received SPANs)
1082 * with each connection's relays.
1085 dmsg_relay_scan_specific(node, conn);
1087 TAILQ_FOREACH(conn, &connq, entry) {
1088 dmsg_relay_scan_specific(node,
1091 assert(conn == NULL);
1099 * Update the relay'd SPANs for this (node, conn).
1101 * Iterate links and adjust relays to match. We only propagate the top link
1102 * for now (XXX we want to propagate the top two).
1104 * The dmsg_relay_scan_cmp() function locates the first relay element
1105 * for any given node. The relay elements will be sub-sorted by dist.
1107 struct relay_scan_info {
1108 h2span_node_t *node;
1109 h2span_relay_t *relay;
1113 dmsg_relay_scan_cmp(h2span_relay_t *relay, void *arg)
1115 struct relay_scan_info *info = arg;
1117 if ((intptr_t)relay->source_rt->any.link->node < (intptr_t)info->node)
1119 if ((intptr_t)relay->source_rt->any.link->node > (intptr_t)info->node)
1125 dmsg_relay_scan_callback(h2span_relay_t *relay, void *arg)
1127 struct relay_scan_info *info = arg;
1129 info->relay = relay;
1134 dmsg_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn)
1136 struct relay_scan_info info;
1137 h2span_relay_t *relay;
1138 h2span_relay_t *next_relay;
1139 h2span_link_t *slink;
1140 dmsg_lnk_conn_t *lconn;
1141 dmsg_lnk_span_t *lspan;
1144 uint32_t lastdist = DMSG_SPAN_MAXDIST;
1145 uint32_t lastrnss = 0;
1151 * Locate the first related relay for the node on this connection.
1152 * relay will be NULL if there were none.
1154 RB_SCAN(h2span_relay_tree, &conn->tree,
1155 dmsg_relay_scan_cmp, dmsg_relay_scan_callback, &info);
1159 assert(relay->source_rt->any.link->node == node);
1161 if (DMsgDebugOpt > 8)
1162 fprintf(stderr, "relay scan for connection %p\n", conn);
1165 * Iterate the node's links (received SPANs) in distance order,
1166 * lowest (best) dist first.
1168 * PROPAGATE THE BEST LINKS OVER THE SPECIFIED CONNECTION.
1170 * Track relays while iterating the best links and construct
1171 * missing relays when necessary.
1173 * (If some prior better link was removed it would have also
1174 * removed the relay, so the relay can only match exactly or
1178 RB_FOREACH(slink, h2span_link_tree, &node->tree) {
1180 * Increment count of successful relays. This isn't
1181 * quite accurate if we break out but nothing after
1182 * the loop uses (count).
1184 * If count exceeds the maximum number of relays we desire
1185 * we normally want to break out. However, in order to
1186 * guarantee a symmetric path we have to continue if both
1187 * (dist) and (rnss) continue to match. Otherwise the SPAN
1188 * propagation in the reverse direction may choose different
1189 * routes and we will not have a symmetric path.
1191 * NOTE: Spanning tree does not have to be symmetrical so
1192 * this code is not currently enabled.
1194 if (++count >= maxcount) {
1195 #ifdef REQUIRE_SYMMETRICAL
1196 if (lastdist != slink->dist || lastrnss != slink->rnss)
1201 /* go beyond the nominal maximum desired relays */
1205 * Match, relay already in-place, get the next
1206 * relay to match against the next slink.
1208 if (relay && relay->source_rt->any.link == slink) {
1209 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
1214 * We might want this SLINK, if it passes our filters.
1216 * The spanning tree can cause closed loops so we have
1217 * to limit slink->dist.
1219 if (slink->dist > DMSG_SPAN_MAXDIST)
1223 * Don't bother transmitting a LNK_SPAN out the same
1224 * connection it came in on. Trivial optimization.
1226 if (slink->state->iocom == conn->state->iocom)
1230 * NOTE ON FILTERS: The protocol spec allows non-requested
1231 * SPANs to be transmitted, the other end is expected to
1232 * leave their transactions open but otherwise ignore them.
1234 * Don't bother transmitting if the remote connection
1235 * is not accepting this SPAN's peer_type.
1237 * pfs_mask is typically used so pure clients can filter
1238 * out receiving SPANs for other pure clients.
1240 lspan = &slink->state->msg->any.lnk_span;
1241 lconn = &conn->state->msg->any.lnk_conn;
1242 if (((1LLU << lspan->peer_type) & lconn->peer_mask) == 0)
1244 if (((1LLU << lspan->pfs_type) & lconn->pfs_mask) == 0)
1248 * Do not give pure clients visibility to other pure clients
1250 if (lconn->pfs_type == DMSG_PFSTYPE_CLIENT &&
1251 lspan->pfs_type == DMSG_PFSTYPE_CLIENT) {
1256 * Connection filter, if cluster uuid is not NULL it must
1257 * match the span cluster uuid. Only applies when the
1258 * peer_type matches.
1260 if (lspan->peer_type == lconn->peer_type &&
1261 !uuid_is_nil(&lconn->pfs_clid, NULL) &&
1262 uuid_compare(&slink->node->cls->pfs_clid,
1263 &lconn->pfs_clid, NULL)) {
1268 * Connection filter, if cluster label is not empty it must
1269 * match the span cluster label. Only applies when the
1270 * peer_type matches.
1272 if (lspan->peer_type == lconn->peer_type &&
1273 lconn->cl_label[0] &&
1274 strcmp(lconn->cl_label, slink->node->cls->cl_label)) {
1279 * NOTE! pfs_fsid differentiates nodes within the same cluster
1280 * so we obviously don't want to match those. Similarly
1285 * Ok, we've accepted this SPAN for relaying.
1287 assert(relay == NULL ||
1288 relay->source_rt->any.link->node != slink->node ||
1289 relay->source_rt->any.link->dist >= slink->dist);
1290 relay = dmsg_generate_relay(conn, slink);
1291 lastdist = slink->dist;
1292 lastrnss = slink->rnss;
1295 * Match (created new relay), get the next relay to
1296 * match against the next slink.
1298 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
1302 * Any remaining relay's belonging to this connection which match
1303 * the node are in excess of the current aggregate spanning state
1304 * and should be removed.
1306 while (relay && relay->source_rt->any.link->node == node) {
1307 next_relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
1308 fprintf(stderr, "RELAY DELETE FROM EXTRAS\n");
1309 dmsg_relay_delete(relay);
1315 * Helper function to generate missing relay.
1317 * cluster_mtx must be held
1321 dmsg_generate_relay(h2span_conn_t *conn, h2span_link_t *slink)
1323 h2span_relay_t *relay;
1324 h2span_node_t *node;
1329 relay = dmsg_alloc(sizeof(*relay));
1331 relay->source_rt = slink->state;
1332 /* relay->source_rt->any.link = slink; */
1335 * NOTE: relay->target_rt->any.relay set to relay by alloc.
1337 msg = dmsg_msg_alloc(&conn->state->iocom->circuit0,
1338 0, DMSG_LNK_SPAN | DMSGF_CREATE,
1339 dmsg_lnk_relay, relay);
1340 relay->target_rt = msg->state;
1342 msg->any.lnk_span = slink->state->msg->any.lnk_span;
1343 msg->any.lnk_span.dist = slink->dist + 1;
1344 msg->any.lnk_span.rnss = slink->rnss + dmsg_rnss();
1346 RB_INSERT(h2span_relay_tree, &conn->tree, relay);
1347 TAILQ_INSERT_TAIL(&slink->relayq, relay, entry);
1349 dmsg_msg_write(msg);
1355 * Messages received on relay SPANs. These are open transactions so it is
1356 * in fact possible for the other end to close the transaction.
1358 * XXX MPRACE on state structure
1361 dmsg_lnk_relay(dmsg_msg_t *msg)
1363 dmsg_state_t *state = msg->state;
1364 h2span_relay_t *relay;
1366 assert(msg->any.head.cmd & DMSGF_REPLY);
1368 if (msg->any.head.cmd & DMSGF_DELETE) {
1369 pthread_mutex_lock(&cluster_mtx);
1370 fprintf(stderr, "RELAY DELETE FROM LNK_RELAY MSG\n");
1371 if ((relay = state->any.relay) != NULL) {
1372 dmsg_relay_delete(relay);
1374 dmsg_state_reply(state, 0);
1376 pthread_mutex_unlock(&cluster_mtx);
1381 * cluster_mtx held by caller
1385 dmsg_relay_delete(h2span_relay_t *relay)
1388 "RELAY DELETE %p RELAY %p ON CLS=%p NODE=%p DIST=%d FD %d STATE %p\n",
1389 relay->source_rt->any.link,
1391 relay->source_rt->any.link->node->cls, relay->source_rt->any.link->node,
1392 relay->source_rt->any.link->dist,
1393 relay->conn->state->iocom->sock_fd, relay->target_rt);
1395 RB_REMOVE(h2span_relay_tree, &relay->conn->tree, relay);
1396 TAILQ_REMOVE(&relay->source_rt->any.link->relayq, relay, entry);
1398 if (relay->target_rt) {
1399 relay->target_rt->any.relay = NULL;
1400 dmsg_state_reply(relay->target_rt, 0);
1401 /* state invalid after reply */
1402 relay->target_rt = NULL;
1405 relay->source_rt = NULL;
1410 dmsg_volconf_thread(void *info)
1412 h2span_media_config_t *conf = info;
1414 pthread_mutex_lock(&cluster_mtx);
1415 while ((conf->ctl & H2CONFCTL_STOP) == 0) {
1416 if (conf->ctl & H2CONFCTL_UPDATE) {
1417 fprintf(stderr, "VOLCONF UPDATE\n");
1418 conf->ctl &= ~H2CONFCTL_UPDATE;
1419 if (bcmp(&conf->copy_run, &conf->copy_pend,
1420 sizeof(conf->copy_run)) == 0) {
1421 fprintf(stderr, "VOLCONF: no changes\n");
1425 * XXX TODO - auto reconnect on lookup failure or
1426 * connect failure or stream failure.
1429 pthread_mutex_unlock(&cluster_mtx);
1430 dmsg_volconf_stop(conf);
1431 conf->copy_run = conf->copy_pend;
1432 if (conf->copy_run.copyid != 0 &&
1433 strncmp(conf->copy_run.path, "span:", 5) == 0) {
1434 dmsg_volconf_start(conf,
1435 conf->copy_run.path + 5);
1437 pthread_mutex_lock(&cluster_mtx);
1438 fprintf(stderr, "VOLCONF UPDATE DONE state %d\n", conf->state);
1440 if (conf->state == H2MC_CONNECT) {
1441 dmsg_volconf_start(conf, conf->copy_run.path + 5);
1442 pthread_mutex_unlock(&cluster_mtx);
1444 pthread_mutex_lock(&cluster_mtx);
1446 pthread_cond_wait(&conf->cond, &cluster_mtx);
1449 pthread_mutex_unlock(&cluster_mtx);
1450 dmsg_volconf_stop(conf);
1454 static void dmsg_volconf_signal(dmsg_iocom_t *iocom);
1458 dmsg_volconf_start(h2span_media_config_t *conf, const char *hostname)
1460 dmsg_master_service_info_t *info;
1462 switch(conf->state) {
1465 conf->fd = dmsg_connect(hostname);
1467 fprintf(stderr, "Unable to connect to %s\n", hostname);
1468 conf->state = H2MC_CONNECT;
1469 } else if (pipe(conf->pipefd) < 0) {
1471 fprintf(stderr, "pipe() failed during volconf\n");
1472 conf->state = H2MC_CONNECT;
1474 fprintf(stderr, "VOLCONF CONNECT\n");
1475 info = malloc(sizeof(*info));
1476 bzero(info, sizeof(*info));
1477 info->fd = conf->fd;
1478 info->altfd = conf->pipefd[0];
1479 info->altmsg_callback = dmsg_volconf_signal;
1481 conf->state = H2MC_RUNNING;
1482 pthread_create(&conf->iocom_thread, NULL,
1483 dmsg_master_service, info);
1493 dmsg_volconf_stop(h2span_media_config_t *conf)
1495 switch(conf->state) {
1499 conf->state = H2MC_STOPPED;
1502 close(conf->pipefd[1]);
1503 conf->pipefd[1] = -1;
1504 pthread_join(conf->iocom_thread, NULL);
1505 conf->iocom_thread = NULL;
1506 conf->state = H2MC_STOPPED;
1513 dmsg_volconf_signal(dmsg_iocom_t *iocom)
1515 atomic_set_int(&iocom->flags, DMSG_IOCOMF_EOF);
1518 /************************************************************************
1519 * MESSAGE ROUTING AND SOURCE VALIDATION *
1520 ************************************************************************/
1523 dmsg_circuit_route(dmsg_msg_t *msg)
1525 dmsg_iocom_t *iocom = msg->iocom;
1526 dmsg_circuit_t *circ;
1527 dmsg_circuit_t *peer;
1528 dmsg_circuit_t dummy;
1532 * Relay occurs before any state processing, msg state should always
1535 assert(msg->state == NULL);
1538 * Lookup the circuit on the incoming iocom.
1540 pthread_mutex_lock(&iocom->mtx);
1542 dummy.msgid = msg->any.head.circuit;
1543 circ = RB_FIND(dmsg_circuit_tree, &iocom->circuit_tree, &dummy);
1546 dmsg_circuit_hold(peer);
1548 if (DMsgDebugOpt >= 4) {
1550 "CIRC relay %08x %p->%p\n",
1551 msg->any.head.cmd, circ, peer);
1554 msg->iocom = peer->iocom;
1555 msg->any.head.circuit = peer->msgid;
1556 dmsg_circuit_drop_locked(msg->circuit);
1557 msg->circuit = peer;
1559 pthread_mutex_unlock(&iocom->mtx);
1561 dmsg_msg_write(msg);
1562 error = DMSG_IOQ_ERROR_ROUTED;
1567 /************************************************************************
1568 * ROUTER AND MESSAGING HANDLES *
1569 ************************************************************************
1571 * Basically the idea here is to provide a stable data structure which
1572 * can be localized to the caller for higher level protocols to work with.
1573 * Depends on the context, these dmsg_handle's can be pooled by use-case
1574 * and remain persistent through a client (or mount point's) life.
1579 * Obtain a stable handle on a cluster given its uuid. This ties directly
1580 * into the global cluster topology, creating the structure if necessary
1581 * (even if the uuid does not exist or does not exist yet), and preventing
1582 * the structure from getting ripped out from under us while we hold a
1586 dmsg_cluster_get(uuid_t *pfs_clid)
1588 h2span_cluster_t dummy_cls;
1589 h2span_cluster_t *cls;
1591 dummy_cls.pfs_clid = *pfs_clid;
1592 pthread_mutex_lock(&cluster_mtx);
1593 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls);
1596 pthread_mutex_unlock(&cluster_mtx);
1601 dmsg_cluster_put(h2span_cluster_t *cls)
1603 pthread_mutex_lock(&cluster_mtx);
1604 assert(cls->refs > 0);
1606 if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
1607 RB_REMOVE(h2span_cluster_tree,
1608 &cluster_tree, cls);
1611 pthread_mutex_unlock(&cluster_mtx);
1615 * Obtain a stable handle to a specific cluster node given its uuid.
1616 * This handle does NOT lock in the route to the node and is typically
1617 * used as part of the dmsg_handle_*() API to obtain a set of
1621 dmsg_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid)
1628 * Dumps the spanning tree
1633 dmsg_shell_tree(dmsg_circuit_t *circuit, char *cmdbuf __unused)
1635 h2span_cluster_t *cls;
1636 h2span_node_t *node;
1637 h2span_link_t *slink;
1638 h2span_relay_t *relay;
1641 pthread_mutex_lock(&cluster_mtx);
1642 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
1643 dmsg_circuit_printf(circuit, "Cluster %s %s (%s)\n",
1644 dmsg_peer_type_to_str(cls->peer_type),
1645 dmsg_uuid_to_str(&cls->pfs_clid, &uustr),
1647 RB_FOREACH(node, h2span_node_tree, &cls->tree) {
1648 dmsg_circuit_printf(circuit, " Node %s %s (%s)\n",
1649 dmsg_pfs_type_to_str(node->pfs_type),
1650 dmsg_uuid_to_str(&node->pfs_fsid, &uustr),
1652 RB_FOREACH(slink, h2span_link_tree, &node->tree) {
1653 dmsg_circuit_printf(circuit,
1654 "\tSLink msgid %016jx "
1656 (intmax_t)slink->state->msgid,
1658 slink->state->iocom->sock_fd);
1659 TAILQ_FOREACH(relay, &slink->relayq, entry) {
1660 dmsg_circuit_printf(circuit,
1661 "\t Relay-out msgid %016jx "
1663 (intmax_t)relay->target_rt->msgid,
1664 relay->target_rt->iocom->sock_fd);
1669 pthread_mutex_unlock(&cluster_mtx);
1673 TAILQ_FOREACH(conn, &connq, entry) {
1681 * Locate the state representing an incoming LNK_SPAN given its msgid.
1684 dmsg_debug_findspan(uint64_t msgid, dmsg_state_t **statep)
1686 h2span_cluster_t *cls;
1687 h2span_node_t *node;
1688 h2span_link_t *slink;
1689 h2span_relay_t *relay;
1691 pthread_mutex_lock(&cluster_mtx);
1693 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
1694 RB_FOREACH(node, h2span_node_tree, &cls->tree) {
1695 RB_FOREACH(slink, h2span_link_tree, &node->tree) {
1696 if (slink->state->msgid == msgid) {
1697 *statep = slink->state;
1703 pthread_mutex_unlock(&cluster_mtx);
1707 pthread_mutex_unlock(&cluster_mtx);
1712 * Random number sub-sort value to add to SPAN rnss fields on relay.
1713 * This allows us to differentiate spans with the same <dist> field
1714 * for relaying purposes. We must normally limit the number of relays
1715 * for any given SPAN origination but we must also guarantee that a
1716 * symmetric reverse path exists, so we use the rnss field as a sub-sort
1717 * (since there can be thousands or millions if we only match on <dist>),
1718 * and if there STILL too many spans we go past the limit.
1724 if (DMsgRNSS == 0) {
1725 pthread_mutex_lock(&cluster_mtx);
1726 while (DMsgRNSS == 0) {
1728 DMsgRNSS = random();
1730 pthread_mutex_unlock(&cluster_mtx);