| 1 | /* |
| 2 | * Copyright (c) 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 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 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 |
| 16 | * distribution. |
| 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. |
| 20 | * |
| 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 |
| 32 | * SUCH DAMAGE. |
| 33 | */ |
| 34 | /* |
| 35 | * LNK_SPAN PROTOCOL SUPPORT FUNCTIONS |
| 36 | * |
| 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. |
| 41 | * |
| 42 | * -- |
| 43 | * |
| 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. |
| 48 | * |
| 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. |
| 53 | * |
| 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. |
| 63 | * |
| 64 | * -- |
| 65 | * |
| 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. |
| 72 | * |
| 73 | * The higher level protocols can then issue transactions to the nodes making |
| 74 | * up a cluster to perform all actions required. |
| 75 | * |
| 76 | * -- |
| 77 | * |
| 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. |
| 85 | * |
| 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. |
| 90 | * |
| 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. |
| 97 | * |
| 98 | * It is also important to note that several paths to the same PFS can be |
| 99 | * propagated along the same link, which allows concurrency and even |
| 100 | * redundancy over several network interfaces or via different routes through |
| 101 | * the topology. Any given transaction will use only a single route but busy |
| 102 | * servers will often have hundreds of transactions active simultaniously, |
| 103 | * so having multiple active paths through the network topology for A<->B |
| 104 | * will improve performance. |
| 105 | * |
| 106 | * -- |
| 107 | * |
| 108 | * Most protocols consolidate operations rather than simply relaying them. |
| 109 | * This is particularly true of LEAF protocols (such as strict HAMMER2 |
| 110 | * clients), of which there can be millions connecting into the cluster at |
| 111 | * various points. The SPAN protocol is not used for these LEAF elements. |
| 112 | * |
| 113 | * Instead the primary service they connect to implements a proxy for the |
| 114 | * client protocols so the core topology only has to propagate a couple of |
| 115 | * LNK_SPANs and not millions. LNK_SPANs are meant to be used only for |
| 116 | * core master nodes and satellite slaves and cache nodes. |
| 117 | */ |
| 118 | |
| 119 | #include "hammer2.h" |
| 120 | |
| 121 | /* |
| 122 | * RED-BLACK TREE DEFINITIONS |
| 123 | * |
| 124 | * We need to track: |
| 125 | * |
| 126 | * (1) shared fsid's (a cluster). |
| 127 | * (2) unique fsid's (a node in a cluster) <--- LNK_SPAN transactions. |
| 128 | * |
| 129 | * We need to aggegate all active LNK_SPANs, aggregate, and create our own |
| 130 | * outgoing LNK_SPAN transactions on each of our connections representing |
| 131 | * the aggregated state. |
| 132 | * |
| 133 | * h2span_connect - list of iocom connections who wish to receive SPAN |
| 134 | * propagation from other connections. Might contain |
| 135 | * a filter string. Only iocom's with an open |
| 136 | * LNK_CONN transactions are applicable for SPAN |
| 137 | * propagation. |
| 138 | * |
| 139 | * h2span_relay - List of links relayed (via SPAN). Essentially |
| 140 | * each relay structure represents a LNK_SPAN |
| 141 | * transaction that we initiated, verses h2span_link |
| 142 | * which is a LNK_SPAN transaction that we received. |
| 143 | * |
| 144 | * -- |
| 145 | * |
| 146 | * h2span_cluster - Organizes the shared fsid's. One structure for |
| 147 | * each cluster. |
| 148 | * |
| 149 | * h2span_node - Organizes the nodes in a cluster. One structure |
| 150 | * for each unique {cluster,node}, aka {fsid, pfs_fsid}. |
| 151 | * |
| 152 | * h2span_link - Organizes all incoming and outgoing LNK_SPAN message |
| 153 | * transactions related to a node. |
| 154 | * |
| 155 | * One h2span_link structure for each incoming LNK_SPAN |
| 156 | * transaction. Links selected for propagation back |
| 157 | * out are also where the outgoing LNK_SPAN messages |
| 158 | * are indexed into (so we can propagate changes). |
| 159 | * |
| 160 | * The h2span_link's use a red-black tree to sort the |
| 161 | * distance hop metric for the incoming LNK_SPAN. We |
| 162 | * then select the top N for outgoing. When the |
| 163 | * topology changes the top N may also change and cause |
| 164 | * new outgoing LNK_SPAN transactions to be opened |
| 165 | * and less desireable ones to be closed, causing |
| 166 | * transactional aborts within the message flow in |
| 167 | * the process. |
| 168 | * |
| 169 | * Also note - All outgoing LNK_SPAN message transactions are also |
| 170 | * entered into a red-black tree for use by the routing |
| 171 | * function. This is handled by msg.c in the state |
| 172 | * code, not here. |
| 173 | */ |
| 174 | |
| 175 | struct h2span_link; |
| 176 | struct h2span_relay; |
| 177 | TAILQ_HEAD(h2span_connect_queue, h2span_connect); |
| 178 | TAILQ_HEAD(h2span_relay_queue, h2span_relay); |
| 179 | |
| 180 | RB_HEAD(h2span_cluster_tree, h2span_cluster); |
| 181 | RB_HEAD(h2span_node_tree, h2span_node); |
| 182 | RB_HEAD(h2span_link_tree, h2span_link); |
| 183 | RB_HEAD(h2span_relay_tree, h2span_relay); |
| 184 | |
| 185 | /* |
| 186 | * Received LNK_CONN transaction enables SPAN protocol over connection. |
| 187 | * (may contain filter). |
| 188 | */ |
| 189 | struct h2span_connect { |
| 190 | TAILQ_ENTRY(h2span_connect) entry; |
| 191 | struct h2span_relay_tree tree; |
| 192 | hammer2_state_t *state; |
| 193 | }; |
| 194 | |
| 195 | /* |
| 196 | * All received LNK_SPANs are organized by cluster (pfs_clid), |
| 197 | * node (pfs_fsid), and link (received LNK_SPAN transaction). |
| 198 | */ |
| 199 | struct h2span_cluster { |
| 200 | RB_ENTRY(h2span_cluster) rbnode; |
| 201 | struct h2span_node_tree tree; |
| 202 | uuid_t pfs_clid; /* shared fsid */ |
| 203 | }; |
| 204 | |
| 205 | struct h2span_node { |
| 206 | RB_ENTRY(h2span_node) rbnode; |
| 207 | struct h2span_link_tree tree; |
| 208 | struct h2span_cluster *cls; |
| 209 | uuid_t pfs_fsid; /* unique fsid */ |
| 210 | }; |
| 211 | |
| 212 | struct h2span_link { |
| 213 | RB_ENTRY(h2span_link) rbnode; |
| 214 | hammer2_state_t *state; /* state<->link */ |
| 215 | struct h2span_node *node; /* related node */ |
| 216 | int32_t dist; |
| 217 | struct h2span_relay_queue relayq; /* relay out */ |
| 218 | }; |
| 219 | |
| 220 | /* |
| 221 | * Any LNK_SPAN transactions we receive which are relayed out other |
| 222 | * connections utilize this structure to track the LNK_SPAN transaction |
| 223 | * we initiate on the other connections, if selected for relay. |
| 224 | * |
| 225 | * In many respects this is the core of the protocol... actually figuring |
| 226 | * out what LNK_SPANs to relay. The spanid used for relaying is the |
| 227 | * address of the 'state' structure, which is why h2span_relay has to |
| 228 | * be entered into a RB-TREE based at h2span_connect (so we can look |
| 229 | * up the spanid to validate it). |
| 230 | */ |
| 231 | struct h2span_relay { |
| 232 | RB_ENTRY(h2span_relay) rbnode; /* from h2span_connect */ |
| 233 | TAILQ_ENTRY(h2span_relay) entry; /* from link */ |
| 234 | struct h2span_connect *conn; |
| 235 | hammer2_state_t *state; /* transmitted LNK_SPAN */ |
| 236 | struct h2span_link *link; /* received LNK_SPAN */ |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | typedef struct h2span_connect h2span_connect_t; |
| 241 | typedef struct h2span_cluster h2span_cluster_t; |
| 242 | typedef struct h2span_node h2span_node_t; |
| 243 | typedef struct h2span_link h2span_link_t; |
| 244 | typedef struct h2span_relay h2span_relay_t; |
| 245 | |
| 246 | static |
| 247 | int |
| 248 | h2span_cluster_cmp(h2span_cluster_t *cls1, h2span_cluster_t *cls2) |
| 249 | { |
| 250 | return(uuid_compare(&cls1->pfs_clid, &cls2->pfs_clid, NULL)); |
| 251 | } |
| 252 | |
| 253 | static |
| 254 | int |
| 255 | h2span_node_cmp(h2span_node_t *node1, h2span_node_t *node2) |
| 256 | { |
| 257 | return(uuid_compare(&node1->pfs_fsid, &node2->pfs_fsid, NULL)); |
| 258 | } |
| 259 | |
| 260 | static |
| 261 | int |
| 262 | h2span_link_cmp(h2span_link_t *link1, h2span_link_t *link2) |
| 263 | { |
| 264 | if (link1->dist < link2->dist) |
| 265 | return(-1); |
| 266 | if (link1->dist > link2->dist) |
| 267 | return(1); |
| 268 | if ((intptr_t)link1 < (intptr_t)link2) |
| 269 | return(-1); |
| 270 | if ((intptr_t)link1 > (intptr_t)link2) |
| 271 | return(1); |
| 272 | return(0); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Relay entries are sorted by node, subsorted by distance and link |
| 277 | * address (so we can match up the conn->tree relay topology with |
| 278 | * a node's link topology). |
| 279 | */ |
| 280 | static |
| 281 | int |
| 282 | h2span_relay_cmp(h2span_relay_t *relay1, h2span_relay_t *relay2) |
| 283 | { |
| 284 | if ((intptr_t)relay1->link->node < (intptr_t)relay2->link->node) |
| 285 | return(-1); |
| 286 | if ((intptr_t)relay1->link->node > (intptr_t)relay2->link->node) |
| 287 | return(1); |
| 288 | if ((intptr_t)relay1->link->dist < (intptr_t)relay2->link->dist) |
| 289 | return(-1); |
| 290 | if ((intptr_t)relay1->link->dist > (intptr_t)relay2->link->dist) |
| 291 | return(1); |
| 292 | if ((intptr_t)relay1->link < (intptr_t)relay2->link) |
| 293 | return(-1); |
| 294 | if ((intptr_t)relay1->link > (intptr_t)relay2->link) |
| 295 | return(1); |
| 296 | return(0); |
| 297 | } |
| 298 | |
| 299 | RB_PROTOTYPE_STATIC(h2span_cluster_tree, h2span_cluster, |
| 300 | rbnode, h2span_cluster_cmp); |
| 301 | RB_PROTOTYPE_STATIC(h2span_node_tree, h2span_node, |
| 302 | rbnode, h2span_node_cmp); |
| 303 | RB_PROTOTYPE_STATIC(h2span_link_tree, h2span_link, |
| 304 | rbnode, h2span_link_cmp); |
| 305 | RB_PROTOTYPE_STATIC(h2span_relay_tree, h2span_relay, |
| 306 | rbnode, h2span_relay_cmp); |
| 307 | |
| 308 | RB_GENERATE_STATIC(h2span_cluster_tree, h2span_cluster, |
| 309 | rbnode, h2span_cluster_cmp); |
| 310 | RB_GENERATE_STATIC(h2span_node_tree, h2span_node, |
| 311 | rbnode, h2span_node_cmp); |
| 312 | RB_GENERATE_STATIC(h2span_link_tree, h2span_link, |
| 313 | rbnode, h2span_link_cmp); |
| 314 | RB_GENERATE_STATIC(h2span_relay_tree, h2span_relay, |
| 315 | rbnode, h2span_relay_cmp); |
| 316 | |
| 317 | /* |
| 318 | * Global mutex protects cluster_tree lookups. |
| 319 | */ |
| 320 | static pthread_mutex_t cluster_mtx; |
| 321 | static struct h2span_cluster_tree cluster_tree = RB_INITIALIZER(cluster_tree); |
| 322 | static struct h2span_connect_queue connq = TAILQ_HEAD_INITIALIZER(connq); |
| 323 | |
| 324 | static void hammer2_lnk_span(hammer2_state_t *state, hammer2_msg_t *msg); |
| 325 | static void hammer2_lnk_conn(hammer2_state_t *state, hammer2_msg_t *msg); |
| 326 | static void hammer2_lnk_relay(hammer2_state_t *state, hammer2_msg_t *msg); |
| 327 | static void hammer2_relay_scan(h2span_node_t *node); |
| 328 | static void hammer2_relay_delete(h2span_relay_t *relay); |
| 329 | |
| 330 | /* |
| 331 | * Receive a HAMMER2_MSG_PROTO_LNK message. This only called for |
| 332 | * one-way and opening-transactions since state->func will be assigned |
| 333 | * in all other cases. |
| 334 | */ |
| 335 | void |
| 336 | hammer2_msg_lnk(hammer2_iocom_t *iocom, hammer2_msg_t *msg) |
| 337 | { |
| 338 | switch(msg->any.head.cmd & HAMMER2_MSGF_BASECMDMASK) { |
| 339 | case HAMMER2_LNK_CONN: |
| 340 | hammer2_lnk_conn(msg->state, msg); |
| 341 | break; |
| 342 | case HAMMER2_LNK_SPAN: |
| 343 | hammer2_lnk_span(msg->state, msg); |
| 344 | break; |
| 345 | default: |
| 346 | fprintf(stderr, |
| 347 | "MSG_PROTO_LNK: Unknown msg %08x\n", msg->any.head.cmd); |
| 348 | hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN); |
| 349 | /* state invalid after reply */ |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | hammer2_lnk_conn(hammer2_state_t *state, hammer2_msg_t *msg) |
| 356 | { |
| 357 | h2span_connect_t *conn; |
| 358 | h2span_relay_t *relay; |
| 359 | char *alloc = NULL; |
| 360 | |
| 361 | pthread_mutex_lock(&cluster_mtx); |
| 362 | |
| 363 | /* |
| 364 | * On transaction start we allocate a new h2span_connect and |
| 365 | * acknowledge the request, leaving the transaction open. |
| 366 | * We then relay priority-selected SPANs. |
| 367 | */ |
| 368 | if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) { |
| 369 | state->func = hammer2_lnk_conn; |
| 370 | |
| 371 | fprintf(stderr, "LNK_CONN(%016jx): %s/%s\n", |
| 372 | (intmax_t)msg->any.head.msgid, |
| 373 | hammer2_uuid_to_str(&msg->any.lnk_conn.pfs_clid, |
| 374 | &alloc), |
| 375 | msg->any.lnk_conn.label); |
| 376 | free(alloc); |
| 377 | |
| 378 | conn = hammer2_alloc(sizeof(*conn)); |
| 379 | |
| 380 | RB_INIT(&conn->tree); |
| 381 | conn->state = state; |
| 382 | state->any.conn = conn; |
| 383 | TAILQ_INSERT_TAIL(&connq, conn, entry); |
| 384 | |
| 385 | hammer2_msg_result(state->iocom, msg, 0); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * On transaction terminate we clean out our h2span_connect |
| 390 | * and acknowledge the request, closing the transaction. |
| 391 | */ |
| 392 | if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) { |
| 393 | fprintf(stderr, "LNK_CONN: Terminated\n"); |
| 394 | conn = state->any.conn; |
| 395 | assert(conn); |
| 396 | |
| 397 | /* |
| 398 | * Clean out all relays. This requires terminating each |
| 399 | * relay transaction. |
| 400 | */ |
| 401 | while ((relay = RB_ROOT(&conn->tree)) != NULL) { |
| 402 | hammer2_relay_delete(relay); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Clean out conn |
| 407 | */ |
| 408 | conn->state = NULL; |
| 409 | msg->state->any.conn = NULL; |
| 410 | TAILQ_REMOVE(&connq, conn, entry); |
| 411 | hammer2_free(conn); |
| 412 | |
| 413 | hammer2_msg_reply(state->iocom, msg, 0); |
| 414 | /* state invalid after reply */ |
| 415 | } |
| 416 | pthread_mutex_unlock(&cluster_mtx); |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | hammer2_lnk_span(hammer2_state_t *state, hammer2_msg_t *msg) |
| 421 | { |
| 422 | h2span_cluster_t dummy_cls; |
| 423 | h2span_node_t dummy_node; |
| 424 | h2span_cluster_t *cls; |
| 425 | h2span_node_t *node; |
| 426 | h2span_link_t *slink; |
| 427 | h2span_relay_t *relay; |
| 428 | char *alloc = NULL; |
| 429 | |
| 430 | pthread_mutex_lock(&cluster_mtx); |
| 431 | |
| 432 | /* |
| 433 | * On transaction start we initialize the tracking infrastructure |
| 434 | */ |
| 435 | if (msg->any.head.cmd & HAMMER2_MSGF_CREATE) { |
| 436 | state->func = hammer2_lnk_span; |
| 437 | |
| 438 | fprintf(stderr, "LNK_SPAN: %s/%s\n", |
| 439 | hammer2_uuid_to_str(&msg->any.lnk_span.pfs_clid, |
| 440 | &alloc), |
| 441 | msg->any.lnk_span.label); |
| 442 | free(alloc); |
| 443 | |
| 444 | /* |
| 445 | * Find the cluster |
| 446 | */ |
| 447 | dummy_cls.pfs_clid = msg->any.lnk_span.pfs_clid; |
| 448 | cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls); |
| 449 | if (cls == NULL) { |
| 450 | cls = hammer2_alloc(sizeof(*cls)); |
| 451 | cls->pfs_clid = msg->any.lnk_span.pfs_clid; |
| 452 | RB_INIT(&cls->tree); |
| 453 | RB_INSERT(h2span_cluster_tree, &cluster_tree, cls); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Find the node |
| 458 | */ |
| 459 | dummy_node.pfs_fsid = msg->any.lnk_span.pfs_fsid; |
| 460 | node = RB_FIND(h2span_node_tree, &cls->tree, &dummy_node); |
| 461 | if (node == NULL) { |
| 462 | node = hammer2_alloc(sizeof(*node)); |
| 463 | node->pfs_fsid = msg->any.lnk_span.pfs_fsid; |
| 464 | node->cls = cls; |
| 465 | RB_INIT(&node->tree); |
| 466 | RB_INSERT(h2span_node_tree, &cls->tree, node); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Create the link |
| 471 | */ |
| 472 | assert(state->any.link == NULL); |
| 473 | slink = hammer2_alloc(sizeof(*slink)); |
| 474 | TAILQ_INIT(&slink->relayq); |
| 475 | slink->node = node; |
| 476 | slink->dist = msg->any.lnk_span.dist; |
| 477 | slink->state = state; |
| 478 | state->any.link = slink; |
| 479 | RB_INSERT(h2span_link_tree, &node->tree, slink); |
| 480 | |
| 481 | hammer2_relay_scan(node); |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * On transaction terminate we remove the tracking infrastructure. |
| 486 | */ |
| 487 | if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) { |
| 488 | slink = state->any.link; |
| 489 | assert(slink != NULL); |
| 490 | node = slink->node; |
| 491 | cls = node->cls; |
| 492 | |
| 493 | /* |
| 494 | * Clean out all relays. This requires terminating each |
| 495 | * relay transaction. |
| 496 | */ |
| 497 | while ((relay = TAILQ_FIRST(&slink->relayq)) != NULL) { |
| 498 | hammer2_relay_delete(relay); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Clean out the topology |
| 503 | */ |
| 504 | RB_REMOVE(h2span_link_tree, &node->tree, slink); |
| 505 | if (RB_EMPTY(&node->tree)) { |
| 506 | RB_REMOVE(h2span_node_tree, &cls->tree, node); |
| 507 | if (RB_EMPTY(&cls->tree)) { |
| 508 | RB_REMOVE(h2span_cluster_tree, |
| 509 | &cluster_tree, cls); |
| 510 | hammer2_free(cls); |
| 511 | } |
| 512 | node->cls = NULL; |
| 513 | hammer2_free(node); |
| 514 | node = NULL; |
| 515 | } |
| 516 | state->any.link = NULL; |
| 517 | slink->state = NULL; |
| 518 | slink->node = NULL; |
| 519 | hammer2_free(slink); |
| 520 | |
| 521 | /* |
| 522 | * We have to terminate the transaction |
| 523 | */ |
| 524 | hammer2_state_reply(state, 0); |
| 525 | /* state invalid after reply */ |
| 526 | |
| 527 | /* |
| 528 | * If the node still exists issue any required updates. If |
| 529 | * it doesn't then all related relays have already been |
| 530 | * removed and there's nothing left to do. |
| 531 | */ |
| 532 | if (node) |
| 533 | hammer2_relay_scan(node); |
| 534 | } |
| 535 | |
| 536 | pthread_mutex_unlock(&cluster_mtx); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Messages received on relay SPANs. These are open transactions so it is |
| 541 | * in fact possible for the other end to close the transaction. |
| 542 | * |
| 543 | * XXX MPRACE on state structure |
| 544 | */ |
| 545 | static void |
| 546 | hammer2_lnk_relay(hammer2_state_t *state, hammer2_msg_t *msg) |
| 547 | { |
| 548 | h2span_relay_t *relay; |
| 549 | |
| 550 | if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) { |
| 551 | pthread_mutex_lock(&cluster_mtx); |
| 552 | if ((relay = state->any.relay) != NULL) { |
| 553 | hammer2_relay_delete(relay); |
| 554 | } else { |
| 555 | hammer2_state_reply(state, 0); |
| 556 | } |
| 557 | pthread_mutex_unlock(&cluster_mtx); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Update relay transactions for SPANs. |
| 563 | * |
| 564 | * Called with cluster_mtx held. |
| 565 | */ |
| 566 | static void hammer2_relay_scan_conn(h2span_node_t *node, |
| 567 | h2span_connect_t *conn); |
| 568 | |
| 569 | static void |
| 570 | hammer2_relay_scan(h2span_node_t *node) |
| 571 | { |
| 572 | h2span_cluster_t *cls; |
| 573 | h2span_connect_t *conn; |
| 574 | |
| 575 | if (node) { |
| 576 | /* |
| 577 | * Iterate specific node |
| 578 | */ |
| 579 | TAILQ_FOREACH(conn, &connq, entry) |
| 580 | hammer2_relay_scan_conn(node, conn); |
| 581 | } else { |
| 582 | /* |
| 583 | * Full iteration (not currently implemented) |
| 584 | * |
| 585 | * Iterate cluster ids |
| 586 | */ |
| 587 | assert(0); |
| 588 | RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) { |
| 589 | /* |
| 590 | * Iterate node ids |
| 591 | */ |
| 592 | RB_FOREACH(node, h2span_node_tree, &cls->tree) { |
| 593 | /* |
| 594 | * Synchronize the node's link (received SPANs) |
| 595 | * with each connection's relays. |
| 596 | */ |
| 597 | TAILQ_FOREACH(conn, &connq, entry) |
| 598 | hammer2_relay_scan_conn(node, conn); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Update the relay'd SPANs for this (node, conn). |
| 606 | * |
| 607 | * Iterate links and adjust relays to match. We only propagate the top link |
| 608 | * for now (XXX we want to propagate the top two). |
| 609 | * |
| 610 | * The hammer2_relay_scan_cmp() function locates the first relay element |
| 611 | * for any given node. The relay elements will be sub-sorted by dist. |
| 612 | */ |
| 613 | struct relay_scan_info { |
| 614 | h2span_node_t *node; |
| 615 | h2span_relay_t *relay; |
| 616 | }; |
| 617 | |
| 618 | static int |
| 619 | hammer2_relay_scan_cmp(h2span_relay_t *relay, void *arg) |
| 620 | { |
| 621 | struct relay_scan_info *info = arg; |
| 622 | |
| 623 | if ((intptr_t)relay->link->node < (intptr_t)info->node) |
| 624 | return(-1); |
| 625 | if ((intptr_t)relay->link->node > (intptr_t)info->node) |
| 626 | return(1); |
| 627 | return(0); |
| 628 | } |
| 629 | |
| 630 | static int |
| 631 | hammer2_relay_scan_callback(h2span_relay_t *relay, void *arg) |
| 632 | { |
| 633 | struct relay_scan_info *info = arg; |
| 634 | |
| 635 | info->relay = relay; |
| 636 | return(-1); |
| 637 | } |
| 638 | |
| 639 | static void |
| 640 | hammer2_relay_scan_conn(h2span_node_t *node, h2span_connect_t *conn) |
| 641 | { |
| 642 | struct relay_scan_info info; |
| 643 | h2span_relay_t *relay; |
| 644 | h2span_relay_t *next_relay; |
| 645 | h2span_link_t *slink; |
| 646 | int count = 2; |
| 647 | |
| 648 | info.node = node; |
| 649 | info.relay = NULL; |
| 650 | |
| 651 | /* |
| 652 | * Locate the first related relay for the connection. relay will |
| 653 | * be NULL if there were none. |
| 654 | */ |
| 655 | RB_SCAN(h2span_relay_tree, &conn->tree, |
| 656 | hammer2_relay_scan_cmp, hammer2_relay_scan_callback, &info); |
| 657 | relay = info.relay; |
| 658 | |
| 659 | fprintf(stderr, "relay scan for connection %p\n", conn); |
| 660 | |
| 661 | /* |
| 662 | * Iterate the node's links (received SPANs) in distance order, |
| 663 | * lowest (best) dist first. |
| 664 | */ |
| 665 | RB_FOREACH(slink, h2span_link_tree, &node->tree) { |
| 666 | /* |
| 667 | * PROPAGATE THE BEST RELAYS BY TRANSMITTING SPANs. |
| 668 | * |
| 669 | * Check for match against current best relay. |
| 670 | * |
| 671 | * A match failure means that the current best relay is not |
| 672 | * as good as the link, create a new relay for the link. |
| 673 | * |
| 674 | * (If some prior better link was removed it would have also |
| 675 | * removed the relay, so the relay can only match exactly or |
| 676 | * be worst). |
| 677 | */ |
| 678 | info.relay = relay; |
| 679 | if (relay == NULL || relay->link != slink) { |
| 680 | hammer2_msg_t *msg; |
| 681 | |
| 682 | assert(relay == NULL || |
| 683 | slink->dist <= relay->link->dist); |
| 684 | relay = hammer2_alloc(sizeof(*relay)); |
| 685 | relay->conn = conn; |
| 686 | relay->link = slink; |
| 687 | |
| 688 | msg = hammer2_msg_alloc(conn->state->iocom, 0, |
| 689 | HAMMER2_LNK_SPAN | |
| 690 | HAMMER2_MSGF_CREATE); |
| 691 | msg->any.lnk_span = slink->state->msg->any.lnk_span; |
| 692 | ++msg->any.lnk_span.dist; /* XXX add weighting */ |
| 693 | |
| 694 | hammer2_msg_write(conn->state->iocom, msg, |
| 695 | hammer2_lnk_relay, relay, |
| 696 | &relay->state); |
| 697 | fprintf(stderr, "RELAY SPAN ON CLS=%p NODE=%p FD %d state %p\n", |
| 698 | node->cls, node, |
| 699 | conn->state->iocom->sock_fd, relay->state); |
| 700 | |
| 701 | RB_INSERT(h2span_relay_tree, &conn->tree, relay); |
| 702 | TAILQ_INSERT_TAIL(&slink->relayq, relay, entry); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Iterate, figure out the next relay. |
| 707 | */ |
| 708 | relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay); |
| 709 | if (--count == 0) { |
| 710 | break; |
| 711 | continue; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Any remaining relay's belonging to this connection which match |
| 717 | * the node are in excess of the current aggregate spanning state |
| 718 | * and should be removed. |
| 719 | */ |
| 720 | while (relay && relay->link->node == node) { |
| 721 | next_relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay); |
| 722 | hammer2_relay_delete(relay); |
| 723 | relay = next_relay; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static |
| 728 | void |
| 729 | hammer2_relay_delete(h2span_relay_t *relay) |
| 730 | { |
| 731 | fprintf(stderr, "RELAY DELETE ON CLS=%p NODE=%p FD %d STATE %p\n", |
| 732 | relay->link->node->cls, relay->link->node, |
| 733 | relay->conn->state->iocom->sock_fd, relay->state); |
| 734 | fprintf(stderr, "RELAY TX %08x RX %08x\n", relay->state->txcmd, relay->state->rxcmd); |
| 735 | |
| 736 | RB_REMOVE(h2span_relay_tree, &relay->conn->tree, relay); |
| 737 | TAILQ_REMOVE(&relay->link->relayq, relay, entry); |
| 738 | |
| 739 | if (relay->state) { |
| 740 | relay->state->any.relay = NULL; |
| 741 | hammer2_state_reply(relay->state, 0); |
| 742 | /* state invalid after reply */ |
| 743 | relay->state = NULL; |
| 744 | } |
| 745 | relay->conn = NULL; |
| 746 | relay->link = NULL; |
| 747 | hammer2_free(relay); |
| 748 | } |