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
37 * This code supports the LNK_SPAN protocol. Essentially all PFS's
38 * clients and services rendezvous with the userland hammer2 service and
39 * open LNK_SPAN transactions using a message header linkid of 0,
40 * registering any PFS's they have connectivity to with us.
44 * Each registration maintains its own open LNK_SPAN message transaction.
45 * The SPANs are collected, aggregated, and retransmitted over available
46 * connections through the maintainance of additional LNK_SPAN message
47 * transactions on each link.
49 * The msgid for each active LNK_SPAN transaction we receive allows us to
50 * send a message to the target PFS (which might be one of many belonging
51 * to the same cluster), by specifying that msgid as the linkid in any
52 * message we send to the target PFS.
54 * Similarly the msgid we allocate for any LNK_SPAN transaction we transmit
55 * (and remember we will maintain multiple open LNK_SPAN transactions on
56 * each connection representing the topology span, so every node sees every
57 * other node as a separate open transaction). So, similarly the msgid for
58 * these active transactions which we initiated can be used by the other
59 * end to route messages through us to another node, ultimately winding up
60 * at the identified hammer2 PFS. We have to adjust the spanid in the message
61 * header at each hop to be representative of the outgoing LNK_SPAN we
62 * are forwarding the message through.
66 * If we were to retransmit every LNK_SPAN transaction we receive it would
67 * create a huge mess, so we have to aggregate all received LNK_SPAN
68 * transactions, sort them by the fsid (the cluster) and sub-sort them by
69 * the pfs_fsid (individual nodes in the cluster), and only retransmit
70 * (create outgoing transactions) for a subset of the nearest distance-hops
71 * for each individual node.
73 * The higher level protocols can then issue transactions to the nodes making
74 * up a cluster to perform all actions required.
78 * Since this is a large topology and a spanning tree protocol, links can
79 * go up and down all the time. Any time a link goes down its transaction
80 * is closed. The transaction has to be closed on both ends before we can
81 * delete (and potentially reuse) the related spanid. The LNK_SPAN being
82 * closed may have been propagated out to other connections and those related
83 * LNK_SPANs are also closed. Ultimately all routes via the lost LNK_SPAN
84 * go away, ultimately reaching all sources and all targets.
86 * Any messages in-transit using a route that goes away will be thrown away.
87 * Open transactions are only tracked at the two end-points. When a link
88 * failure propagates to an end-point the related open transactions lose
89 * their spanid and are automatically aborted.
91 * It is important to note that internal route nodes cannot just associate
92 * a lost LNK_SPAN transaction with another route to the same destination.
93 * Message transactions MUST be serialized and MUST be ordered. All messages
94 * for a transaction must run over the same route. So if the route used by
95 * an active transaction is lost, the related messages will be fully aborted
96 * and the higher protocol levels will retry as appropriate.
98 * FULLY ABORTING A ROUTED MESSAGE is handled via link-failure propagation
99 * back to the originator. Only the originator keeps tracks of a message.
100 * Routers just pass it through. If a route is lost during transit the
101 * message is simply thrown away.
103 * It is also important to note that several paths to the same PFS can be
104 * propagated along the same link, which allows concurrency and even
105 * redundancy over several network interfaces or via different routes through
106 * the topology. Any given transaction will use only a single route but busy
107 * servers will often have hundreds of transactions active simultaniously,
108 * so having multiple active paths through the network topology for A<->B
109 * will improve performance.
113 * Most protocols consolidate operations rather than simply relaying them.
114 * This is particularly true of LEAF protocols (such as strict HAMMER2
115 * clients), of which there can be millions connecting into the cluster at
116 * various points. The SPAN protocol is not used for these LEAF elements.
118 * Instead the primary service they connect to implements a proxy for the
119 * client protocols so the core topology only has to propagate a couple of
120 * LNK_SPANs and not millions. LNK_SPANs are meant to be used only for
121 * core master nodes and satellite slaves and cache nodes.
127 * Maximum spanning tree distance. This has the practical effect of
128 * stopping tail-chasing closed loops when a feeder span is lost.
130 #define HAMMER2_SPAN_MAXDIST 16
133 * RED-BLACK TREE DEFINITIONS
137 * (1) shared fsid's (a cluster).
138 * (2) unique fsid's (a node in a cluster) <--- LNK_SPAN transactions.
140 * We need to aggegate all active LNK_SPANs, aggregate, and create our own
141 * outgoing LNK_SPAN transactions on each of our connections representing
142 * the aggregated state.
144 * h2span_connect - list of iocom connections who wish to receive SPAN
145 * propagation from other connections. Might contain
146 * a filter string. Only iocom's with an open
147 * LNK_CONN transactions are applicable for SPAN
150 * h2span_relay - List of links relayed (via SPAN). Essentially
151 * each relay structure represents a LNK_SPAN
152 * transaction that we initiated, verses h2span_link
153 * which is a LNK_SPAN transaction that we received.
157 * h2span_cluster - Organizes the shared fsid's. One structure for
160 * h2span_node - Organizes the nodes in a cluster. One structure
161 * for each unique {cluster,node}, aka {fsid, pfs_fsid}.
163 * h2span_link - Organizes all incoming and outgoing LNK_SPAN message
164 * transactions related to a node.
166 * One h2span_link structure for each incoming LNK_SPAN
167 * transaction. Links selected for propagation back
168 * out are also where the outgoing LNK_SPAN messages
169 * are indexed into (so we can propagate changes).
171 * The h2span_link's use a red-black tree to sort the
172 * distance hop metric for the incoming LNK_SPAN. We
173 * then select the top N for outgoing. When the
174 * topology changes the top N may also change and cause
175 * new outgoing LNK_SPAN transactions to be opened
176 * and less desireable ones to be closed, causing
177 * transactional aborts within the message flow in
180 * Also note - All outgoing LNK_SPAN message transactions are also
181 * entered into a red-black tree for use by the routing
182 * function. This is handled by msg.c in the state
188 TAILQ_HEAD(h2span_connect_queue, h2span_connect);
189 TAILQ_HEAD(h2span_relay_queue, h2span_relay);
191 RB_HEAD(h2span_cluster_tree, h2span_cluster);
192 RB_HEAD(h2span_node_tree, h2span_node);
193 RB_HEAD(h2span_link_tree, h2span_link);
194 RB_HEAD(h2span_relay_tree, h2span_relay);
197 * Received LNK_CONN transaction enables SPAN protocol over connection.
198 * (may contain filter).
200 struct h2span_connect {
201 TAILQ_ENTRY(h2span_connect) entry;
202 struct h2span_relay_tree tree;
203 hammer2_state_t *state;
207 * All received LNK_SPANs are organized by cluster (pfs_clid),
208 * node (pfs_fsid), and link (received LNK_SPAN transaction).
210 struct h2span_cluster {
211 RB_ENTRY(h2span_cluster) rbnode;
212 struct h2span_node_tree tree;
213 uuid_t pfs_clid; /* shared fsid */
214 int refs; /* prevents destruction */
218 RB_ENTRY(h2span_node) rbnode;
219 struct h2span_link_tree tree;
220 struct h2span_cluster *cls;
221 uuid_t pfs_fsid; /* unique fsid */
226 RB_ENTRY(h2span_link) rbnode;
227 hammer2_state_t *state; /* state<->link */
228 struct h2span_node *node; /* related node */
230 struct h2span_relay_queue relayq; /* relay out */
231 struct hammer2_router *router; /* route out this link */
235 * Any LNK_SPAN transactions we receive which are relayed out other
236 * connections utilize this structure to track the LNK_SPAN transaction
237 * we initiate on the other connections, if selected for relay.
239 * In many respects this is the core of the protocol... actually figuring
240 * out what LNK_SPANs to relay. The spanid used for relaying is the
241 * address of the 'state' structure, which is why h2span_relay has to
242 * be entered into a RB-TREE based at h2span_connect (so we can look
243 * up the spanid to validate it).
245 * NOTE: Messages can be received via the LNK_SPAN transaction the
246 * relay maintains, and can be replied via relay->router, but
247 * messages are NOT initiated via a relay. Messages are initiated
248 * via incoming links (h2span_link's).
250 * relay->link represents the link being relayed, NOT the LNK_SPAN
251 * transaction the relay is holding open.
253 struct h2span_relay {
254 RB_ENTRY(h2span_relay) rbnode; /* from h2span_connect */
255 TAILQ_ENTRY(h2span_relay) entry; /* from link */
256 struct h2span_connect *conn;
257 hammer2_state_t *state; /* transmitted LNK_SPAN */
258 struct h2span_link *link; /* LNK_SPAN being relayed */
259 struct hammer2_router *router;/* route out this relay */
263 typedef struct h2span_connect h2span_connect_t;
264 typedef struct h2span_cluster h2span_cluster_t;
265 typedef struct h2span_node h2span_node_t;
266 typedef struct h2span_link h2span_link_t;
267 typedef struct h2span_relay h2span_relay_t;
271 h2span_cluster_cmp(h2span_cluster_t *cls1, h2span_cluster_t *cls2)
273 return(uuid_compare(&cls1->pfs_clid, &cls2->pfs_clid, NULL));
278 h2span_node_cmp(h2span_node_t *node1, h2span_node_t *node2)
280 return(uuid_compare(&node1->pfs_fsid, &node2->pfs_fsid, NULL));
284 * NOTE: Sort/subsort must match h2span_relay_cmp() under any given
289 h2span_link_cmp(h2span_link_t *link1, h2span_link_t *link2)
291 if (link1->dist < link2->dist)
293 if (link1->dist > link2->dist)
295 if (link1->state->msgid < link2->state->msgid)
297 if (link1->state->msgid > link2->state->msgid)
303 * Relay entries are sorted by node, subsorted by distance and link
304 * address (so we can match up the conn->tree relay topology with
305 * a node's link topology).
309 h2span_relay_cmp(h2span_relay_t *relay1, h2span_relay_t *relay2)
311 h2span_link_t *link1 = relay1->link;
312 h2span_link_t *link2 = relay2->link;
314 if ((intptr_t)link1->node < (intptr_t)link2->node)
316 if ((intptr_t)link1->node > (intptr_t)link2->node)
318 if (link1->dist < link2->dist)
320 if (link1->dist > link2->dist)
322 if (link1->state->msgid < link2->state->msgid)
324 if (link1->state->msgid > link2->state->msgid)
329 RB_PROTOTYPE_STATIC(h2span_cluster_tree, h2span_cluster,
330 rbnode, h2span_cluster_cmp);
331 RB_PROTOTYPE_STATIC(h2span_node_tree, h2span_node,
332 rbnode, h2span_node_cmp);
333 RB_PROTOTYPE_STATIC(h2span_link_tree, h2span_link,
334 rbnode, h2span_link_cmp);
335 RB_PROTOTYPE_STATIC(h2span_relay_tree, h2span_relay,
336 rbnode, h2span_relay_cmp);
338 RB_GENERATE_STATIC(h2span_cluster_tree, h2span_cluster,
339 rbnode, h2span_cluster_cmp);
340 RB_GENERATE_STATIC(h2span_node_tree, h2span_node,
341 rbnode, h2span_node_cmp);
342 RB_GENERATE_STATIC(h2span_link_tree, h2span_link,
343 rbnode, h2span_link_cmp);
344 RB_GENERATE_STATIC(h2span_relay_tree, h2span_relay,
345 rbnode, h2span_relay_cmp);
348 * Global mutex protects cluster_tree lookups.
350 static pthread_mutex_t cluster_mtx;
351 static struct h2span_cluster_tree cluster_tree = RB_INITIALIZER(cluster_tree);
352 static struct h2span_connect_queue connq = TAILQ_HEAD_INITIALIZER(connq);
354 static void hammer2_lnk_span(hammer2_msg_t *msg);
355 static void hammer2_lnk_conn(hammer2_msg_t *msg);
356 static void hammer2_lnk_relay(hammer2_msg_t *msg);
357 static void hammer2_relay_scan(h2span_connect_t *conn, h2span_node_t *node);
358 static void hammer2_relay_delete(h2span_relay_t *relay);
361 hammer2_msg_lnk_signal(hammer2_router_t *router __unused)
363 pthread_mutex_lock(&cluster_mtx);
364 hammer2_relay_scan(NULL, NULL);
365 pthread_mutex_unlock(&cluster_mtx);
369 * Receive a HAMMER2_MSG_PROTO_LNK message. This only called for
370 * one-way and opening-transactions since state->func will be assigned
371 * in all other cases.
374 hammer2_msg_lnk(hammer2_msg_t *msg)
376 switch(msg->any.head.cmd & HAMMER2_MSGF_BASECMDMASK) {
377 case HAMMER2_LNK_CONN:
378 hammer2_lnk_conn(msg);
380 case HAMMER2_LNK_SPAN:
381 hammer2_lnk_span(msg);
385 "MSG_PROTO_LNK: Unknown msg %08x\n", msg->any.head.cmd);
386 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
387 /* state invalid after reply */
393 hammer2_lnk_conn(hammer2_msg_t *msg)
395 hammer2_state_t *state = msg->state;
396 h2span_connect_t *conn;
397 h2span_relay_t *relay;
400 pthread_mutex_lock(&cluster_mtx);
403 * On transaction start we allocate a new h2span_connect and
404 * acknowledge the request, leaving the transaction open.
405 * We then relay priority-selected SPANs.
407 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
408 state->func = hammer2_lnk_conn;
410 fprintf(stderr, "LNK_CONN(%08x): %s/%s\n",
411 (uint32_t)msg->any.head.msgid,
412 hammer2_uuid_to_str(&msg->any.lnk_conn.pfs_clid,
414 msg->any.lnk_conn.label);
417 conn = hammer2_alloc(sizeof(*conn));
419 RB_INIT(&conn->tree);
421 state->any.conn = conn;
422 TAILQ_INSERT_TAIL(&connq, conn, entry);
424 hammer2_msg_result(msg, 0);
428 * Span-synchronize all nodes with the new connection
430 hammer2_relay_scan(conn, NULL);
432 hammer2_router_signal(msg->router);
436 * On transaction terminate we clean out our h2span_connect
437 * and acknowledge the request, closing the transaction.
439 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
440 fprintf(stderr, "LNK_CONN: Terminated\n");
441 conn = state->any.conn;
445 * Clean out all relays. This requires terminating each
448 while ((relay = RB_ROOT(&conn->tree)) != NULL) {
449 hammer2_relay_delete(relay);
456 msg->state->any.conn = NULL;
457 TAILQ_REMOVE(&connq, conn, entry);
460 hammer2_msg_reply(msg, 0);
461 /* state invalid after reply */
463 pthread_mutex_unlock(&cluster_mtx);
467 hammer2_lnk_span(hammer2_msg_t *msg)
469 hammer2_state_t *state = msg->state;
470 h2span_cluster_t dummy_cls;
471 h2span_node_t dummy_node;
472 h2span_cluster_t *cls;
474 h2span_link_t *slink;
475 h2span_relay_t *relay;
478 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
480 pthread_mutex_lock(&cluster_mtx);
483 * On transaction start we initialize the tracking infrastructure
485 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) {
486 assert(state->func == NULL);
487 state->func = hammer2_lnk_span;
489 msg->any.lnk_span.label[sizeof(msg->any.lnk_span.label)-1] = 0;
494 dummy_cls.pfs_clid = msg->any.lnk_span.pfs_clid;
495 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls);
497 cls = hammer2_alloc(sizeof(*cls));
498 cls->pfs_clid = msg->any.lnk_span.pfs_clid;
500 RB_INSERT(h2span_cluster_tree, &cluster_tree, cls);
506 dummy_node.pfs_fsid = msg->any.lnk_span.pfs_fsid;
507 node = RB_FIND(h2span_node_tree, &cls->tree, &dummy_node);
509 node = hammer2_alloc(sizeof(*node));
510 node->pfs_fsid = msg->any.lnk_span.pfs_fsid;
512 RB_INIT(&node->tree);
513 RB_INSERT(h2span_node_tree, &cls->tree, node);
514 snprintf(node->label, sizeof(node->label),
515 "%s", msg->any.lnk_span.label);
521 assert(state->any.link == NULL);
522 slink = hammer2_alloc(sizeof(*slink));
523 TAILQ_INIT(&slink->relayq);
525 slink->dist = msg->any.lnk_span.dist;
526 slink->state = state;
527 state->any.link = slink;
530 * Embedded router structure in link for message forwarding.
532 * The spanning id for the router is the message id of
533 * the SPAN link it is embedded in, allowing messages to
534 * be routed via &slink->router.
536 slink->router = hammer2_router_alloc();
537 slink->router->iocom = state->iocom;
538 slink->router->link = slink;
539 slink->router->spanid = state->msgid;
540 hammer2_router_connect(slink->router);
542 RB_INSERT(h2span_link_tree, &node->tree, slink);
544 fprintf(stderr, "LNK_SPAN(thr %p): %p %s/%s dist=%d\n",
547 hammer2_uuid_to_str(&msg->any.lnk_span.pfs_clid,
549 msg->any.lnk_span.label,
550 msg->any.lnk_span.dist);
553 hammer2_relay_scan(NULL, node);
555 hammer2_router_signal(msg->router);
559 * On transaction terminate we remove the tracking infrastructure.
561 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
562 slink = state->any.link;
563 assert(slink != NULL);
567 fprintf(stderr, "LNK_DELE(thr %p): %p %s/%s dist=%d\n",
570 hammer2_uuid_to_str(&cls->pfs_clid, &alloc),
571 state->msg->any.lnk_span.label,
572 state->msg->any.lnk_span.dist);
576 * Remove the router from consideration
578 hammer2_router_disconnect(&slink->router);
581 * Clean out all relays. This requires terminating each
584 while ((relay = TAILQ_FIRST(&slink->relayq)) != NULL) {
585 hammer2_relay_delete(relay);
589 * Clean out the topology
591 RB_REMOVE(h2span_link_tree, &node->tree, slink);
592 if (RB_EMPTY(&node->tree)) {
593 RB_REMOVE(h2span_node_tree, &cls->tree, node);
594 if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
595 RB_REMOVE(h2span_cluster_tree,
603 state->any.link = NULL;
609 * We have to terminate the transaction
611 hammer2_state_reply(state, 0);
612 /* state invalid after reply */
615 * If the node still exists issue any required updates. If
616 * it doesn't then all related relays have already been
617 * removed and there's nothing left to do.
621 hammer2_relay_scan(NULL, node);
624 hammer2_router_signal(msg->router);
627 pthread_mutex_unlock(&cluster_mtx);
631 * Messages received on relay SPANs. These are open transactions so it is
632 * in fact possible for the other end to close the transaction.
634 * XXX MPRACE on state structure
637 hammer2_lnk_relay(hammer2_msg_t *msg)
639 hammer2_state_t *state = msg->state;
640 h2span_relay_t *relay;
642 assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
644 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
645 pthread_mutex_lock(&cluster_mtx);
646 if ((relay = state->any.relay) != NULL) {
647 hammer2_relay_delete(relay);
649 hammer2_state_reply(state, 0);
651 pthread_mutex_unlock(&cluster_mtx);
656 * Update relay transactions for SPANs.
658 * Called with cluster_mtx held.
660 static void hammer2_relay_scan_specific(h2span_node_t *node,
661 h2span_connect_t *conn);
664 hammer2_relay_scan(h2span_connect_t *conn, h2span_node_t *node)
666 h2span_cluster_t *cls;
670 * Iterate specific node
672 TAILQ_FOREACH(conn, &connq, entry)
673 hammer2_relay_scan_specific(node, conn);
678 * Iterate cluster ids, nodes, and either a specific connection
679 * or all connections.
681 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
685 RB_FOREACH(node, h2span_node_tree, &cls->tree) {
687 * Synchronize the node's link (received SPANs)
688 * with each connection's relays.
691 hammer2_relay_scan_specific(node, conn);
693 TAILQ_FOREACH(conn, &connq, entry) {
694 hammer2_relay_scan_specific(node,
697 assert(conn == NULL);
705 * Update the relay'd SPANs for this (node, conn).
707 * Iterate links and adjust relays to match. We only propagate the top link
708 * for now (XXX we want to propagate the top two).
710 * The hammer2_relay_scan_cmp() function locates the first relay element
711 * for any given node. The relay elements will be sub-sorted by dist.
713 struct relay_scan_info {
715 h2span_relay_t *relay;
719 hammer2_relay_scan_cmp(h2span_relay_t *relay, void *arg)
721 struct relay_scan_info *info = arg;
723 if ((intptr_t)relay->link->node < (intptr_t)info->node)
725 if ((intptr_t)relay->link->node > (intptr_t)info->node)
731 hammer2_relay_scan_callback(h2span_relay_t *relay, void *arg)
733 struct relay_scan_info *info = arg;
740 hammer2_relay_scan_specific(h2span_node_t *node, h2span_connect_t *conn)
742 struct relay_scan_info info;
743 h2span_relay_t *relay;
744 h2span_relay_t *next_relay;
745 h2span_link_t *slink;
752 * Locate the first related relay for the node on this connection.
753 * relay will be NULL if there were none.
755 RB_SCAN(h2span_relay_tree, &conn->tree,
756 hammer2_relay_scan_cmp, hammer2_relay_scan_callback, &info);
760 assert(relay->link->node == node);
763 fprintf(stderr, "relay scan for connection %p\n", conn);
766 * Iterate the node's links (received SPANs) in distance order,
767 * lowest (best) dist first.
769 /* fprintf(stderr, "LOOP\n"); */
770 RB_FOREACH(slink, h2span_link_tree, &node->tree) {
772 fprintf(stderr, "SLINK %p RELAY %p(%p)\n",
773 slink, relay, relay ? relay->link : NULL);
776 * PROPAGATE THE BEST LINKS OVER THE SPECIFIED CONNECTION.
778 * Track relays while iterating the best links and construct
779 * missing relays when necessary.
781 * (If some prior better link was removed it would have also
782 * removed the relay, so the relay can only match exactly or
785 if (relay && relay->link == slink) {
787 * Match, relay already in-place, get the next
788 * relay to match against the next slink.
790 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
793 } else if (slink->dist > HAMMER2_SPAN_MAXDIST) {
795 * No match but span distance is too great,
796 * do not relay. This prevents endless closed
797 * loops with ever-incrementing distances when
798 * the seed span is lost in the graph.
800 * All later spans will also be too far away so
801 * we can break out of the loop.
806 * No match, distance is ok, construct a new relay.
807 * (slink is better than relay).
811 assert(relay == NULL ||
812 relay->link->node != slink->node ||
813 relay->link->dist >= slink->dist);
814 relay = hammer2_alloc(sizeof(*relay));
818 msg = hammer2_msg_alloc(conn->state->iocom->router, 0,
821 hammer2_lnk_relay, relay);
822 relay->state = msg->state;
823 relay->router = hammer2_router_alloc();
824 relay->router->iocom = relay->state->iocom;
825 relay->router->relay = relay;
826 relay->router->spanid = relay->state->msgid;
828 msg->any.lnk_span = slink->state->msg->any.lnk_span;
829 msg->any.lnk_span.dist = slink->dist + 1;
831 hammer2_router_connect(relay->router);
833 RB_INSERT(h2span_relay_tree, &conn->tree, relay);
834 TAILQ_INSERT_TAIL(&slink->relayq, relay, entry);
836 hammer2_msg_write(msg);
839 "RELAY SPAN %p RELAY %p ON CLS=%p NODE=%p DIST=%d "
843 node->cls, node, slink->dist,
844 conn->state->iocom->sock_fd, relay->state);
847 * Match (created new relay), get the next relay to
848 * match against the next slink.
850 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
857 * Any remaining relay's belonging to this connection which match
858 * the node are in excess of the current aggregate spanning state
859 * and should be removed.
861 while (relay && relay->link->node == node) {
862 next_relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay);
863 hammer2_relay_delete(relay);
870 hammer2_relay_delete(h2span_relay_t *relay)
873 "RELAY DELETE %p RELAY %p ON CLS=%p NODE=%p DIST=%d FD %d STATE %p\n",
876 relay->link->node->cls, relay->link->node,
878 relay->conn->state->iocom->sock_fd, relay->state);
880 hammer2_router_disconnect(&relay->router);
882 RB_REMOVE(h2span_relay_tree, &relay->conn->tree, relay);
883 TAILQ_REMOVE(&relay->link->relayq, relay, entry);
886 relay->state->any.relay = NULL;
887 hammer2_state_reply(relay->state, 0);
888 /* state invalid after reply */
896 /************************************************************************
897 * ROUTER AND MESSAGING HANDLES *
898 ************************************************************************
900 * Basically the idea here is to provide a stable data structure which
901 * can be localized to the caller for higher level protocols to work with.
902 * Depends on the context, these hammer2_handle's can be pooled by use-case
903 * and remain persistent through a client (or mount point's) life.
908 * Obtain a stable handle on a cluster given its uuid. This ties directly
909 * into the global cluster topology, creating the structure if necessary
910 * (even if the uuid does not exist or does not exist yet), and preventing
911 * the structure from getting ripped out from under us while we hold a
915 hammer2_cluster_get(uuid_t *pfs_clid)
917 h2span_cluster_t dummy_cls;
918 h2span_cluster_t *cls;
920 dummy_cls.pfs_clid = *pfs_clid;
921 pthread_mutex_lock(&cluster_mtx);
922 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls);
925 pthread_mutex_unlock(&cluster_mtx);
930 hammer2_cluster_put(h2span_cluster_t *cls)
932 pthread_mutex_lock(&cluster_mtx);
933 assert(cls->refs > 0);
935 if (RB_EMPTY(&cls->tree) && cls->refs == 0) {
936 RB_REMOVE(h2span_cluster_tree,
940 pthread_mutex_unlock(&cluster_mtx);
944 * Obtain a stable handle to a specific cluster node given its uuid.
945 * This handle does NOT lock in the route to the node and is typically
946 * used as part of the hammer2_handle_*() API to obtain a set of
950 hammer2_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid)
958 * Acquire a persistent router structure given the cluster and node ids.
959 * Messages can be transacted via this structure while held. If the route
960 * is lost messages will return failure.
963 hammer2_router_get(uuid_t *pfs_clid, uuid_t *pfs_fsid)
968 * Release previously acquired router.
971 hammer2_router_put(hammer2_router_t *router)
976 /************************************************************************
978 ************************************************************************/
980 * Dumps the spanning tree
983 shell_tree(hammer2_router_t *router, char *cmdbuf __unused)
985 h2span_cluster_t *cls;
987 h2span_link_t *slink;
990 pthread_mutex_lock(&cluster_mtx);
991 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) {
992 router_printf(router, "Cluster %s\n",
993 hammer2_uuid_to_str(&cls->pfs_clid, &uustr));
994 RB_FOREACH(node, h2span_node_tree, &cls->tree) {
995 router_printf(router, " Node %s (%s)\n",
996 hammer2_uuid_to_str(&node->pfs_fsid, &uustr),
998 RB_FOREACH(slink, h2span_link_tree, &node->tree) {
999 router_printf(router, "\tLink dist=%d via %d\n",
1001 slink->state->iocom->sock_fd);
1005 pthread_mutex_unlock(&cluster_mtx);
1009 TAILQ_FOREACH(conn, &connq, entry) {