1 /* 2 * Copyright (c) 2012-2014 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 - Please see sys/dmsg.h for an 36 * involved explanation of the protocol. 37 */ 38 39 #include "dmsg_local.h" 40 41 /* 42 * Maximum spanning tree distance. This has the practical effect of 43 * stopping tail-chasing closed loops when a feeder span is lost. 44 */ 45 #define DMSG_SPAN_MAXDIST 16 46 47 /* 48 * RED-BLACK TREE DEFINITIONS 49 * 50 * We need to track: 51 * 52 * (1) shared fsid's (a cluster). 53 * (2) unique fsid's (a node in a cluster) <--- LNK_SPAN transactions. 54 * 55 * We need to aggegate all active LNK_SPANs, aggregate, and create our own 56 * outgoing LNK_SPAN transactions on each of our connections representing 57 * the aggregated state. 58 * 59 * h2span_conn - list of iocom connections who wish to receive SPAN 60 * propagation from other connections. Might contain 61 * a filter string. Only iocom's with an open 62 * LNK_CONN transactions are applicable for SPAN 63 * propagation. 64 * 65 * h2span_relay - List of links relayed (via SPAN). Essentially 66 * each relay structure represents a LNK_SPAN 67 * transaction that we initiated, verses h2span_link 68 * which is a LNK_SPAN transaction that we received. 69 * 70 * -- 71 * 72 * h2span_cluster - Organizes the shared fsid's. One structure for 73 * each cluster. 74 * 75 * h2span_node - Organizes the nodes in a cluster. One structure 76 * for each unique {cluster,node}, aka {fsid, pfs_fsid}. 77 * 78 * h2span_link - Organizes all incoming and outgoing LNK_SPAN message 79 * transactions related to a node. 80 * 81 * One h2span_link structure for each incoming LNK_SPAN 82 * transaction. Links selected for propagation back 83 * out are also where the outgoing LNK_SPAN messages 84 * are indexed into (so we can propagate changes). 85 * 86 * The h2span_link's use a red-black tree to sort the 87 * distance hop metric for the incoming LNK_SPAN. We 88 * then select the top N for outgoing. When the 89 * topology changes the top N may also change and cause 90 * new outgoing LNK_SPAN transactions to be opened 91 * and less desireable ones to be closed, causing 92 * transactional aborts within the message flow in 93 * the process. 94 * 95 * Also note - All outgoing LNK_SPAN message transactions are also 96 * entered into a red-black tree for use by the routing 97 * function. This is handled by msg.c in the state 98 * code, not here. 99 */ 100 101 struct h2span_link; 102 struct h2span_relay; 103 TAILQ_HEAD(h2span_conn_queue, h2span_conn); 104 TAILQ_HEAD(h2span_relay_queue, h2span_relay); 105 106 RB_HEAD(h2span_cluster_tree, h2span_cluster); 107 RB_HEAD(h2span_node_tree, h2span_node); 108 RB_HEAD(h2span_link_tree, h2span_link); 109 RB_HEAD(h2span_relay_tree, h2span_relay); 110 uint32_t DMsgRNSS; 111 112 /* 113 * Received LNK_CONN transaction enables SPAN protocol over connection. 114 * (may contain filter). Typically one for each mount and several may 115 * share the same media. 116 */ 117 struct h2span_conn { 118 TAILQ_ENTRY(h2span_conn) entry; 119 struct h2span_relay_tree tree; 120 dmsg_state_t *state; 121 dmsg_lnk_conn_t lnk_conn; 122 }; 123 124 /* 125 * All received LNK_SPANs are organized by cluster (pfs_clid), 126 * node (pfs_fsid), and link (received LNK_SPAN transaction). 127 */ 128 struct h2span_cluster { 129 RB_ENTRY(h2span_cluster) rbnode; 130 struct h2span_node_tree tree; 131 uuid_t pfs_clid; /* shared fsid */ 132 uint8_t peer_type; 133 char cl_label[128]; /* cluster label (typ PEER_BLOCK) */ 134 int refs; /* prevents destruction */ 135 }; 136 137 struct h2span_node { 138 RB_ENTRY(h2span_node) rbnode; 139 struct h2span_link_tree tree; 140 struct h2span_cluster *cls; 141 uint8_t pfs_type; 142 uuid_t pfs_fsid; /* unique fsid */ 143 char fs_label[128]; /* fs label (typ PEER_HAMMER2) */ 144 void *opaque; 145 }; 146 147 struct h2span_link { 148 RB_ENTRY(h2span_link) rbnode; 149 dmsg_state_t *state; /* state<->link */ 150 struct h2span_node *node; /* related node */ 151 struct h2span_relay_queue relayq; /* relay out */ 152 dmsg_lnk_span_t lnk_span; 153 }; 154 155 /* 156 * Any LNK_SPAN transactions we receive which are relayed out other 157 * connections utilize this structure to track the LNK_SPAN transactions 158 * we initiate (relay out) on other connections. We only relay out 159 * LNK_SPANs on connections we have an open CONN transaction for. 160 * 161 * The relay structure points to the outgoing LNK_SPAN trans (out_state) 162 * and to the incoming LNK_SPAN transaction (in_state). The relay 163 * structure holds refs on the related states. 164 * 165 * In many respects this is the core of the protocol... actually figuring 166 * out what LNK_SPANs to relay. The spanid used for relaying is the 167 * address of the 'state' structure, which is why h2span_relay has to 168 * be entered into a RB-TREE based at h2span_conn (so we can look 169 * up the spanid to validate it). 170 */ 171 struct h2span_relay { 172 TAILQ_ENTRY(h2span_relay) entry; /* from link */ 173 RB_ENTRY(h2span_relay) rbnode; /* from h2span_conn */ 174 struct h2span_conn *conn; /* related CONN transaction */ 175 dmsg_state_t *source_rt; /* h2span_link state */ 176 dmsg_state_t *target_rt; /* h2span_relay state */ 177 }; 178 179 typedef struct h2span_conn h2span_conn_t; 180 typedef struct h2span_cluster h2span_cluster_t; 181 typedef struct h2span_node h2span_node_t; 182 typedef struct h2span_link h2span_link_t; 183 typedef struct h2span_relay h2span_relay_t; 184 185 #define dmsg_termstr(array) _dmsg_termstr((array), sizeof(array)) 186 187 static h2span_relay_t *dmsg_generate_relay(h2span_conn_t *conn, 188 h2span_link_t *slink); 189 static uint32_t dmsg_rnss(void); 190 191 static __inline 192 void 193 _dmsg_termstr(char *base, size_t size) 194 { 195 base[size-1] = 0; 196 } 197 198 /* 199 * Cluster peer_type, uuid, AND label must match for a match 200 */ 201 static 202 int 203 h2span_cluster_cmp(h2span_cluster_t *cls1, h2span_cluster_t *cls2) 204 { 205 int r; 206 207 if (cls1->peer_type < cls2->peer_type) 208 return(-1); 209 if (cls1->peer_type > cls2->peer_type) 210 return(1); 211 r = uuid_compare(&cls1->pfs_clid, &cls2->pfs_clid, NULL); 212 if (r == 0) 213 r = strcmp(cls1->cl_label, cls2->cl_label); 214 215 return r; 216 } 217 218 /* 219 * Match against fs_label/pfs_fsid. Together these two items represent a 220 * unique node. In most cases the primary differentiator is pfs_fsid but 221 * we also string-match fs_label. 222 */ 223 static 224 int 225 h2span_node_cmp(h2span_node_t *node1, h2span_node_t *node2) 226 { 227 int r; 228 229 r = strcmp(node1->fs_label, node2->fs_label); 230 if (r == 0) 231 r = uuid_compare(&node1->pfs_fsid, &node2->pfs_fsid, NULL); 232 return (r); 233 } 234 235 /* 236 * Sort/subsort must match h2span_relay_cmp() under any given node 237 * to make the aggregation algorithm easier, so the best links are 238 * in the same sorted order as the best relays. 239 * 240 * NOTE: We cannot use link*->state->msgid because this msgid is created 241 * by each remote host and thus might wind up being the same. 242 */ 243 static 244 int 245 h2span_link_cmp(h2span_link_t *link1, h2span_link_t *link2) 246 { 247 if (link1->lnk_span.dist < link2->lnk_span.dist) 248 return(-1); 249 if (link1->lnk_span.dist > link2->lnk_span.dist) 250 return(1); 251 if (link1->lnk_span.rnss < link2->lnk_span.rnss) 252 return(-1); 253 if (link1->lnk_span.rnss > link2->lnk_span.rnss) 254 return(1); 255 #if 1 256 if ((uintptr_t)link1->state < (uintptr_t)link2->state) 257 return(-1); 258 if ((uintptr_t)link1->state > (uintptr_t)link2->state) 259 return(1); 260 #else 261 if (link1->state->msgid < link2->state->msgid) 262 return(-1); 263 if (link1->state->msgid > link2->state->msgid) 264 return(1); 265 #endif 266 return(0); 267 } 268 269 /* 270 * Relay entries are sorted by node, subsorted by distance and link 271 * address (so we can match up the conn->tree relay topology with 272 * a node's link topology). 273 */ 274 static 275 int 276 h2span_relay_cmp(h2span_relay_t *relay1, h2span_relay_t *relay2) 277 { 278 h2span_link_t *link1 = relay1->source_rt->any.link; 279 h2span_link_t *link2 = relay2->source_rt->any.link; 280 281 if ((intptr_t)link1->node < (intptr_t)link2->node) 282 return(-1); 283 if ((intptr_t)link1->node > (intptr_t)link2->node) 284 return(1); 285 if (link1->lnk_span.dist < link2->lnk_span.dist) 286 return(-1); 287 if (link1->lnk_span.dist > link2->lnk_span.dist) 288 return(1); 289 if (link1->lnk_span.rnss < link2->lnk_span.rnss) 290 return(-1); 291 if (link1->lnk_span.rnss > link2->lnk_span.rnss) 292 return(1); 293 #if 1 294 if ((uintptr_t)link1->state < (uintptr_t)link2->state) 295 return(-1); 296 if ((uintptr_t)link1->state > (uintptr_t)link2->state) 297 return(1); 298 #else 299 if (link1->state->msgid < link2->state->msgid) 300 return(-1); 301 if (link1->state->msgid > link2->state->msgid) 302 return(1); 303 #endif 304 return(0); 305 } 306 307 RB_PROTOTYPE_STATIC(h2span_cluster_tree, h2span_cluster, 308 rbnode, h2span_cluster_cmp); 309 RB_PROTOTYPE_STATIC(h2span_node_tree, h2span_node, 310 rbnode, h2span_node_cmp); 311 RB_PROTOTYPE_STATIC(h2span_link_tree, h2span_link, 312 rbnode, h2span_link_cmp); 313 RB_PROTOTYPE_STATIC(h2span_relay_tree, h2span_relay, 314 rbnode, h2span_relay_cmp); 315 316 RB_GENERATE_STATIC(h2span_cluster_tree, h2span_cluster, 317 rbnode, h2span_cluster_cmp); 318 RB_GENERATE_STATIC(h2span_node_tree, h2span_node, 319 rbnode, h2span_node_cmp); 320 RB_GENERATE_STATIC(h2span_link_tree, h2span_link, 321 rbnode, h2span_link_cmp); 322 RB_GENERATE_STATIC(h2span_relay_tree, h2span_relay, 323 rbnode, h2span_relay_cmp); 324 325 /* 326 * Global mutex protects cluster_tree lookups, connq, mediaq. 327 */ 328 static pthread_mutex_t cluster_mtx; 329 static struct h2span_cluster_tree cluster_tree = RB_INITIALIZER(cluster_tree); 330 static struct h2span_conn_queue connq = TAILQ_HEAD_INITIALIZER(connq); 331 static struct dmsg_media_queue mediaq = TAILQ_HEAD_INITIALIZER(mediaq); 332 333 static void dmsg_lnk_span(dmsg_msg_t *msg); 334 static void dmsg_lnk_conn(dmsg_msg_t *msg); 335 static void dmsg_lnk_relay(dmsg_msg_t *msg); 336 static void dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node); 337 static void dmsg_relay_delete(h2span_relay_t *relay); 338 339 void 340 dmsg_msg_lnk_signal(dmsg_iocom_t *iocom __unused) 341 { 342 pthread_mutex_lock(&cluster_mtx); 343 dmsg_relay_scan(NULL, NULL); 344 pthread_mutex_unlock(&cluster_mtx); 345 } 346 347 /* 348 * DMSG_PROTO_LNK - Generic DMSG_PROTO_LNK. 349 * (incoming iocom lock not held) 350 * 351 * This function is typically called for one-way and opening-transactions 352 * since state->func is assigned after that, but it will also be called 353 * if no state->func is assigned on transaction-open. 354 */ 355 void 356 dmsg_msg_lnk(dmsg_msg_t *msg) 357 { 358 dmsg_iocom_t *iocom = msg->state->iocom; 359 360 switch(msg->tcmd & DMSGF_BASECMDMASK) { 361 case DMSG_LNK_CONN: 362 dmsg_lnk_conn(msg); 363 break; 364 case DMSG_LNK_SPAN: 365 dmsg_lnk_span(msg); 366 break; 367 default: 368 iocom->usrmsg_callback(msg, 1); 369 /* state invalid after reply */ 370 break; 371 } 372 } 373 374 /* 375 * LNK_CONN - iocom identify message reception. 376 * (incoming iocom lock not held) 377 * 378 * Remote node identifies itself to us, sets up a SPAN filter, and gives us 379 * the ok to start transmitting SPANs. 380 */ 381 void 382 dmsg_lnk_conn(dmsg_msg_t *msg) 383 { 384 dmsg_state_t *state = msg->state; 385 dmsg_iocom_t *iocom = state->iocom; 386 dmsg_media_t *media; 387 h2span_conn_t *conn; 388 h2span_relay_t *relay; 389 char *alloc = NULL; 390 391 pthread_mutex_lock(&cluster_mtx); 392 393 fprintf(stderr, 394 "dmsg_lnk_conn: msg %p cmd %08x state %p " 395 "txcmd %08x rxcmd %08x\n", 396 msg, msg->any.head.cmd, state, 397 state->txcmd, state->rxcmd); 398 399 switch(msg->any.head.cmd & DMSGF_TRANSMASK) { 400 case DMSG_LNK_CONN | DMSGF_CREATE: 401 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE: 402 /* 403 * On transaction start we allocate a new h2span_conn and 404 * acknowledge the request, leaving the transaction open. 405 * We then relay priority-selected SPANs. 406 */ 407 fprintf(stderr, "LNK_CONN(%08x): %s/%s/%s\n", 408 (uint32_t)msg->any.head.msgid, 409 dmsg_uuid_to_str(&msg->any.lnk_conn.pfs_clid, 410 &alloc), 411 msg->any.lnk_conn.cl_label, 412 msg->any.lnk_conn.fs_label); 413 free(alloc); 414 415 conn = dmsg_alloc(sizeof(*conn)); 416 assert(state->iocom->conn == NULL); 417 418 RB_INIT(&conn->tree); 419 state->iocom->conn = conn; /* XXX only one */ 420 state->iocom->conn_msgid = state->msgid; 421 conn->state = state; 422 state->func = dmsg_lnk_conn; 423 state->any.conn = conn; 424 TAILQ_INSERT_TAIL(&connq, conn, entry); 425 conn->lnk_conn = msg->any.lnk_conn; 426 427 /* 428 * Set up media 429 */ 430 TAILQ_FOREACH(media, &mediaq, entry) { 431 if (uuid_compare(&msg->any.lnk_conn.mediaid, 432 &media->mediaid, NULL) == 0) { 433 break; 434 } 435 } 436 if (media == NULL) { 437 media = dmsg_alloc(sizeof(*media)); 438 media->mediaid = msg->any.lnk_conn.mediaid; 439 TAILQ_INSERT_TAIL(&mediaq, media, entry); 440 } 441 state->media = media; 442 ++media->refs; 443 444 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) { 445 iocom->usrmsg_callback(msg, 0); 446 dmsg_msg_result(msg, 0); 447 dmsg_iocom_signal(iocom); 448 break; 449 } 450 /* FALL THROUGH */ 451 case DMSG_LNK_CONN | DMSGF_DELETE: 452 case DMSG_LNK_ERROR | DMSGF_DELETE: 453 /* 454 * On transaction terminate we clean out our h2span_conn 455 * and acknowledge the request, closing the transaction. 456 */ 457 fprintf(stderr, "LNK_CONN: Terminated\n"); 458 conn = state->any.conn; 459 assert(conn); 460 461 /* 462 * Adjust media refs 463 * 464 * Callback will clean out media config / user-opaque state 465 */ 466 media = state->media; 467 --media->refs; 468 if (media->refs == 0) { 469 fprintf(stderr, "Media shutdown\n"); 470 TAILQ_REMOVE(&mediaq, media, entry); 471 pthread_mutex_unlock(&cluster_mtx); 472 iocom->usrmsg_callback(msg, 0); 473 pthread_mutex_lock(&cluster_mtx); 474 dmsg_free(media); 475 } 476 state->media = NULL; 477 478 /* 479 * Clean out all relays. This requires terminating each 480 * relay transaction. 481 */ 482 while ((relay = RB_ROOT(&conn->tree)) != NULL) { 483 dmsg_relay_delete(relay); 484 } 485 486 /* 487 * Clean out conn 488 */ 489 conn->state = NULL; 490 msg->state->any.conn = NULL; 491 msg->state->iocom->conn = NULL; 492 TAILQ_REMOVE(&connq, conn, entry); 493 dmsg_free(conn); 494 495 dmsg_msg_reply(msg, 0); 496 /* state invalid after reply */ 497 break; 498 default: 499 iocom->usrmsg_callback(msg, 1); 500 #if 0 501 if (msg->any.head.cmd & DMSGF_DELETE) 502 goto deleteconn; 503 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 504 #endif 505 break; 506 } 507 pthread_mutex_unlock(&cluster_mtx); 508 } 509 510 /* 511 * LNK_SPAN - Spanning tree protocol message reception 512 * (incoming iocom lock not held) 513 * 514 * Receive a spanning tree transactional message, creating or destroying 515 * a SPAN and propagating it to other iocoms. 516 */ 517 void 518 dmsg_lnk_span(dmsg_msg_t *msg) 519 { 520 dmsg_state_t *state = msg->state; 521 dmsg_iocom_t *iocom = state->iocom; 522 h2span_cluster_t dummy_cls; 523 h2span_node_t dummy_node; 524 h2span_cluster_t *cls; 525 h2span_node_t *node; 526 h2span_link_t *slink; 527 h2span_relay_t *relay; 528 char *alloc = NULL; 529 530 /* 531 * Ignore reply to LNK_SPAN. The reply is needed to forge the 532 * return path for the transaction. 533 */ 534 if (msg->any.head.cmd & DMSGF_REPLY) { 535 printf("Ignore reply to LNK_SPAN\n"); 536 return; 537 } 538 539 pthread_mutex_lock(&cluster_mtx); 540 541 /* 542 * On transaction start we initialize the tracking infrastructure 543 */ 544 if (msg->any.head.cmd & DMSGF_CREATE) { 545 assert(state->func == NULL); 546 state->func = dmsg_lnk_span; 547 548 dmsg_termstr(msg->any.lnk_span.cl_label); 549 dmsg_termstr(msg->any.lnk_span.fs_label); 550 551 /* 552 * Find the cluster 553 */ 554 dummy_cls.pfs_clid = msg->any.lnk_span.pfs_clid; 555 dummy_cls.peer_type = msg->any.lnk_span.peer_type; 556 bcopy(msg->any.lnk_span.cl_label, 557 dummy_cls.cl_label, 558 sizeof(dummy_cls.cl_label)); 559 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls); 560 if (cls == NULL) { 561 cls = dmsg_alloc(sizeof(*cls)); 562 cls->pfs_clid = msg->any.lnk_span.pfs_clid; 563 cls->peer_type = msg->any.lnk_span.peer_type; 564 bcopy(msg->any.lnk_span.cl_label, 565 cls->cl_label, 566 sizeof(cls->cl_label)); 567 RB_INIT(&cls->tree); 568 RB_INSERT(h2span_cluster_tree, &cluster_tree, cls); 569 } 570 571 /* 572 * Find the node 573 */ 574 dummy_node.pfs_fsid = msg->any.lnk_span.pfs_fsid; 575 bcopy(msg->any.lnk_span.fs_label, dummy_node.fs_label, 576 sizeof(dummy_node.fs_label)); 577 node = RB_FIND(h2span_node_tree, &cls->tree, &dummy_node); 578 if (node == NULL) { 579 node = dmsg_alloc(sizeof(*node)); 580 node->pfs_fsid = msg->any.lnk_span.pfs_fsid; 581 node->pfs_type = msg->any.lnk_span.pfs_type; 582 bcopy(msg->any.lnk_span.fs_label, 583 node->fs_label, 584 sizeof(node->fs_label)); 585 node->cls = cls; 586 RB_INIT(&node->tree); 587 RB_INSERT(h2span_node_tree, &cls->tree, node); 588 if (iocom->node_handler) { 589 iocom->node_handler(&node->opaque, msg, 590 DMSG_NODEOP_ADD); 591 } 592 } 593 594 /* 595 * Create the link 596 */ 597 assert(state->any.link == NULL); 598 slink = dmsg_alloc(sizeof(*slink)); 599 TAILQ_INIT(&slink->relayq); 600 slink->node = node; 601 slink->state = state; 602 state->any.link = slink; 603 slink->lnk_span = msg->any.lnk_span; 604 605 RB_INSERT(h2span_link_tree, &node->tree, slink); 606 607 fprintf(stderr, 608 "LNK_SPAN(thr %p): %p %s cl=%s fs=%s dist=%d\n", 609 iocom, 610 slink, 611 dmsg_uuid_to_str(&msg->any.lnk_span.pfs_clid, &alloc), 612 msg->any.lnk_span.cl_label, 613 msg->any.lnk_span.fs_label, 614 msg->any.lnk_span.dist); 615 free(alloc); 616 #if 0 617 dmsg_relay_scan(NULL, node); 618 #endif 619 dmsg_iocom_signal(iocom); 620 } 621 622 /* 623 * On transaction terminate we remove the tracking infrastructure. 624 */ 625 if (msg->any.head.cmd & DMSGF_DELETE) { 626 slink = state->any.link; 627 assert(slink != NULL); 628 node = slink->node; 629 cls = node->cls; 630 631 fprintf(stderr, "LNK_DELE(thr %p): %p %s cl=%s fs=%s\n", 632 iocom, 633 slink, 634 dmsg_uuid_to_str(&cls->pfs_clid, &alloc), 635 cls->cl_label, 636 node->fs_label); 637 free(alloc); 638 639 /* 640 * Clean out all relays. This requires terminating each 641 * relay transaction. 642 */ 643 while ((relay = TAILQ_FIRST(&slink->relayq)) != NULL) { 644 dmsg_relay_delete(relay); 645 } 646 647 /* 648 * Clean out the topology 649 */ 650 RB_REMOVE(h2span_link_tree, &node->tree, slink); 651 if (RB_EMPTY(&node->tree)) { 652 RB_REMOVE(h2span_node_tree, &cls->tree, node); 653 if (iocom->node_handler) { 654 iocom->node_handler(&node->opaque, msg, 655 DMSG_NODEOP_DEL); 656 } 657 if (RB_EMPTY(&cls->tree) && cls->refs == 0) { 658 RB_REMOVE(h2span_cluster_tree, 659 &cluster_tree, cls); 660 dmsg_free(cls); 661 } 662 node->cls = NULL; 663 dmsg_free(node); 664 node = NULL; 665 } 666 state->any.link = NULL; 667 slink->state = NULL; 668 slink->node = NULL; 669 dmsg_free(slink); 670 671 /* 672 * We have to terminate the transaction 673 */ 674 dmsg_state_reply(state, 0); 675 /* state invalid after reply */ 676 677 /* 678 * If the node still exists issue any required updates. If 679 * it doesn't then all related relays have already been 680 * removed and there's nothing left to do. 681 */ 682 #if 0 683 if (node) 684 dmsg_relay_scan(NULL, node); 685 #endif 686 if (node) 687 dmsg_iocom_signal(iocom); 688 } 689 690 pthread_mutex_unlock(&cluster_mtx); 691 } 692 693 /* 694 * Update relay transactions for SPANs. 695 * 696 * Called with cluster_mtx held. 697 */ 698 static void dmsg_relay_scan_specific(h2span_node_t *node, 699 h2span_conn_t *conn); 700 701 static void 702 dmsg_relay_scan(h2span_conn_t *conn, h2span_node_t *node) 703 { 704 h2span_cluster_t *cls; 705 706 if (node) { 707 /* 708 * Iterate specific node 709 */ 710 TAILQ_FOREACH(conn, &connq, entry) 711 dmsg_relay_scan_specific(node, conn); 712 } else { 713 /* 714 * Full iteration. 715 * 716 * Iterate cluster ids, nodes, and either a specific connection 717 * or all connections. 718 */ 719 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) { 720 /* 721 * Iterate node ids 722 */ 723 RB_FOREACH(node, h2span_node_tree, &cls->tree) { 724 /* 725 * Synchronize the node's link (received SPANs) 726 * with each connection's relays. 727 */ 728 if (conn) { 729 dmsg_relay_scan_specific(node, conn); 730 } else { 731 TAILQ_FOREACH(conn, &connq, entry) { 732 dmsg_relay_scan_specific(node, 733 conn); 734 } 735 assert(conn == NULL); 736 } 737 } 738 } 739 } 740 } 741 742 /* 743 * Update the relay'd SPANs for this (node, conn). 744 * 745 * Iterate links and adjust relays to match. We only propagate the top link 746 * for now (XXX we want to propagate the top two). 747 * 748 * The dmsg_relay_scan_cmp() function locates the first relay element 749 * for any given node. The relay elements will be sub-sorted by dist. 750 */ 751 struct relay_scan_info { 752 h2span_node_t *node; 753 h2span_relay_t *relay; 754 }; 755 756 static int 757 dmsg_relay_scan_cmp(h2span_relay_t *relay, void *arg) 758 { 759 struct relay_scan_info *info = arg; 760 761 if ((intptr_t)relay->source_rt->any.link->node < (intptr_t)info->node) 762 return(-1); 763 if ((intptr_t)relay->source_rt->any.link->node > (intptr_t)info->node) 764 return(1); 765 return(0); 766 } 767 768 static int 769 dmsg_relay_scan_callback(h2span_relay_t *relay, void *arg) 770 { 771 struct relay_scan_info *info = arg; 772 773 info->relay = relay; 774 return(-1); 775 } 776 777 static void 778 dmsg_relay_scan_specific(h2span_node_t *node, h2span_conn_t *conn) 779 { 780 struct relay_scan_info info; 781 h2span_relay_t *relay; 782 h2span_relay_t *next_relay; 783 h2span_link_t *slink; 784 dmsg_lnk_conn_t *lconn; 785 dmsg_lnk_span_t *lspan; 786 int count; 787 int maxcount = 2; 788 #ifdef REQUIRE_SYMMETRICAL 789 uint32_t lastdist = DMSG_SPAN_MAXDIST; 790 uint32_t lastrnss = 0; 791 #endif 792 793 info.node = node; 794 info.relay = NULL; 795 796 /* 797 * Locate the first related relay for the node on this connection. 798 * relay will be NULL if there were none. 799 */ 800 RB_SCAN(h2span_relay_tree, &conn->tree, 801 dmsg_relay_scan_cmp, dmsg_relay_scan_callback, &info); 802 relay = info.relay; 803 info.relay = NULL; 804 if (relay) 805 assert(relay->source_rt->any.link->node == node); 806 807 if (DMsgDebugOpt > 8) 808 fprintf(stderr, "relay scan for connection %p\n", conn); 809 810 /* 811 * Iterate the node's links (received SPANs) in distance order, 812 * lowest (best) dist first. 813 * 814 * PROPAGATE THE BEST LINKS OVER THE SPECIFIED CONNECTION. 815 * 816 * Track relays while iterating the best links and construct 817 * missing relays when necessary. 818 * 819 * (If some prior better link was removed it would have also 820 * removed the relay, so the relay can only match exactly or 821 * be worse). 822 */ 823 count = 0; 824 RB_FOREACH(slink, h2span_link_tree, &node->tree) { 825 /* 826 * Increment count of successful relays. This isn't 827 * quite accurate if we break out but nothing after 828 * the loop uses (count). 829 * 830 * If count exceeds the maximum number of relays we desire 831 * we normally want to break out. However, in order to 832 * guarantee a symmetric path we have to continue if both 833 * (dist) and (rnss) continue to match. Otherwise the SPAN 834 * propagation in the reverse direction may choose different 835 * routes and we will not have a symmetric path. 836 * 837 * NOTE: Spanning tree does not have to be symmetrical so 838 * this code is not currently enabled. 839 */ 840 if (++count >= maxcount) { 841 #ifdef REQUIRE_SYMMETRICAL 842 if (lastdist != slink->lnk_span.dist || 843 lastrnss != slink->lnk_span.rnss) { 844 break; 845 } 846 #else 847 break; 848 #endif 849 /* go beyond the nominal maximum desired relays */ 850 } 851 852 /* 853 * Match, relay already in-place, get the next 854 * relay to match against the next slink. 855 */ 856 if (relay && relay->source_rt->any.link == slink) { 857 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay); 858 continue; 859 } 860 861 /* 862 * We might want this SLINK, if it passes our filters. 863 * 864 * The spanning tree can cause closed loops so we have 865 * to limit slink->dist. 866 */ 867 if (slink->lnk_span.dist > DMSG_SPAN_MAXDIST) 868 break; 869 870 /* 871 * Don't bother transmitting a LNK_SPAN out the same 872 * connection it came in on. Trivial optimization. 873 */ 874 if (slink->state->iocom == conn->state->iocom) 875 break; 876 877 /* 878 * NOTE ON FILTERS: The protocol spec allows non-requested 879 * SPANs to be transmitted, the other end is expected to 880 * leave their transactions open but otherwise ignore them. 881 * 882 * Don't bother transmitting if the remote connection 883 * is not accepting this SPAN's peer_type. 884 * 885 * pfs_mask is typically used so pure clients can filter 886 * out receiving SPANs for other pure clients. 887 */ 888 lspan = &slink->lnk_span; 889 lconn = &conn->lnk_conn; 890 if (((1LLU << lspan->peer_type) & lconn->peer_mask) == 0) 891 break; 892 if (((1LLU << lspan->pfs_type) & lconn->pfs_mask) == 0) 893 break; 894 895 /* 896 * Do not give pure clients visibility to other pure clients 897 */ 898 if (lconn->pfs_type == DMSG_PFSTYPE_CLIENT && 899 lspan->pfs_type == DMSG_PFSTYPE_CLIENT) { 900 break; 901 } 902 903 /* 904 * Connection filter, if cluster uuid is not NULL it must 905 * match the span cluster uuid. Only applies when the 906 * peer_type matches. 907 */ 908 if (lspan->peer_type == lconn->peer_type && 909 !uuid_is_nil(&lconn->pfs_clid, NULL) && 910 uuid_compare(&slink->node->cls->pfs_clid, 911 &lconn->pfs_clid, NULL)) { 912 break; 913 } 914 915 /* 916 * Connection filter, if cluster label is not empty it must 917 * match the span cluster label. Only applies when the 918 * peer_type matches. 919 */ 920 if (lspan->peer_type == lconn->peer_type && 921 lconn->cl_label[0] && 922 strcmp(lconn->cl_label, slink->node->cls->cl_label)) { 923 break; 924 } 925 926 /* 927 * NOTE! pfs_fsid differentiates nodes within the same cluster 928 * so we obviously don't want to match those. Similarly 929 * for fs_label. 930 */ 931 932 /* 933 * Ok, we've accepted this SPAN for relaying. 934 */ 935 assert(relay == NULL || 936 relay->source_rt->any.link->node != slink->node || 937 relay->source_rt->any.link->lnk_span.dist >= 938 slink->lnk_span.dist); 939 relay = dmsg_generate_relay(conn, slink); 940 #ifdef REQUIRE_SYMMETRICAL 941 lastdist = slink->lnk_span.dist; 942 lastrnss = slink->lnk_span.rnss; 943 #endif 944 945 /* 946 * Match (created new relay), get the next relay to 947 * match against the next slink. 948 */ 949 relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay); 950 } 951 952 /* 953 * Any remaining relay's belonging to this connection which match 954 * the node are in excess of the current aggregate spanning state 955 * and should be removed. 956 */ 957 while (relay && relay->source_rt->any.link->node == node) { 958 next_relay = RB_NEXT(h2span_relay_tree, &conn->tree, relay); 959 fprintf(stderr, "RELAY DELETE FROM EXTRAS\n"); 960 dmsg_relay_delete(relay); 961 relay = next_relay; 962 } 963 } 964 965 /* 966 * Find the slink associated with the msgid and return its state, 967 * so the caller can issue a transaction. 968 */ 969 dmsg_state_t * 970 dmsg_findspan(const char *label) 971 { 972 dmsg_state_t *state; 973 h2span_cluster_t *cls; 974 h2span_node_t *node; 975 h2span_link_t *slink; 976 uint64_t msgid = strtoull(label, NULL, 16); 977 978 pthread_mutex_lock(&cluster_mtx); 979 980 state = NULL; 981 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) { 982 RB_FOREACH(node, h2span_node_tree, &cls->tree) { 983 RB_FOREACH(slink, h2span_link_tree, &node->tree) { 984 if (slink->state->msgid == msgid) { 985 state = slink->state; 986 goto done; 987 } 988 } 989 } 990 } 991 done: 992 pthread_mutex_unlock(&cluster_mtx); 993 994 fprintf(stderr, "findspan: %p\n", state); 995 996 return state; 997 } 998 999 1000 /* 1001 * Helper function to generate missing relay on target connection. 1002 * 1003 * cluster_mtx must be held 1004 */ 1005 static 1006 h2span_relay_t * 1007 dmsg_generate_relay(h2span_conn_t *conn, h2span_link_t *slink) 1008 { 1009 h2span_relay_t *relay; 1010 dmsg_msg_t *msg; 1011 1012 relay = dmsg_alloc(sizeof(*relay)); 1013 relay->conn = conn; 1014 relay->source_rt = slink->state; 1015 /* relay->source_rt->any.link = slink; */ 1016 1017 /* 1018 * NOTE: relay->target_rt->any.relay set to relay by alloc. 1019 * 1020 * NOTE: LNK_SPAN is transmitted as a top-level transaction. 1021 */ 1022 msg = dmsg_msg_alloc(&conn->state->iocom->state0, 1023 0, DMSG_LNK_SPAN | DMSGF_CREATE, 1024 dmsg_lnk_relay, relay); 1025 relay->target_rt = msg->state; 1026 msg->state->flags |= DMSG_STATE_ROUTED; 1027 1028 msg->any.lnk_span = slink->lnk_span; 1029 msg->any.lnk_span.dist = slink->lnk_span.dist + 1; 1030 msg->any.lnk_span.rnss = slink->lnk_span.rnss + dmsg_rnss(); 1031 1032 RB_INSERT(h2span_relay_tree, &conn->tree, relay); 1033 TAILQ_INSERT_TAIL(&slink->relayq, relay, entry); 1034 1035 dmsg_msg_write(msg); 1036 1037 return (relay); 1038 } 1039 1040 /* 1041 * Messages received on relay SPANs. These are open transactions so it is 1042 * in fact possible for the other end to close the transaction. 1043 * 1044 * XXX MPRACE on state structure 1045 */ 1046 static void 1047 dmsg_lnk_relay(dmsg_msg_t *msg) 1048 { 1049 dmsg_state_t *state = msg->state; 1050 h2span_relay_t *relay; 1051 1052 assert(msg->any.head.cmd & DMSGF_REPLY); 1053 1054 if (msg->any.head.cmd & DMSGF_DELETE) { 1055 pthread_mutex_lock(&cluster_mtx); 1056 fprintf(stderr, "RELAY DELETE FROM LNK_RELAY MSG\n"); 1057 if ((relay = state->any.relay) != NULL) { 1058 dmsg_relay_delete(relay); 1059 } else { 1060 dmsg_state_reply(state, 0); 1061 } 1062 pthread_mutex_unlock(&cluster_mtx); 1063 } 1064 } 1065 1066 /* 1067 * cluster_mtx held by caller 1068 */ 1069 static 1070 void 1071 dmsg_relay_delete(h2span_relay_t *relay) 1072 { 1073 fprintf(stderr, 1074 "RELAY DELETE %p RELAY %p ON CLS=%p NODE=%p " 1075 "DIST=%d FD %d STATE %p\n", 1076 relay->source_rt->any.link, 1077 relay, 1078 relay->source_rt->any.link->node->cls, 1079 relay->source_rt->any.link->node, 1080 relay->source_rt->any.link->lnk_span.dist, 1081 relay->conn->state->iocom->sock_fd, 1082 relay->target_rt); 1083 1084 RB_REMOVE(h2span_relay_tree, &relay->conn->tree, relay); 1085 TAILQ_REMOVE(&relay->source_rt->any.link->relayq, relay, entry); 1086 1087 if (relay->target_rt) { 1088 relay->target_rt->any.relay = NULL; 1089 dmsg_state_reply(relay->target_rt, 0); 1090 /* state invalid after reply */ 1091 relay->target_rt = NULL; 1092 } 1093 relay->conn = NULL; 1094 relay->source_rt = NULL; 1095 dmsg_free(relay); 1096 } 1097 1098 /************************************************************************ 1099 * ROUTER AND MESSAGING HANDLES * 1100 ************************************************************************ 1101 * 1102 * Basically the idea here is to provide a stable data structure which 1103 * can be localized to the caller for higher level protocols to work with. 1104 * Depends on the context, these dmsg_handle's can be pooled by use-case 1105 * and remain persistent through a client (or mount point's) life. 1106 */ 1107 1108 #if 0 1109 /* 1110 * Obtain a stable handle on a cluster given its uuid. This ties directly 1111 * into the global cluster topology, creating the structure if necessary 1112 * (even if the uuid does not exist or does not exist yet), and preventing 1113 * the structure from getting ripped out from under us while we hold a 1114 * pointer to it. 1115 */ 1116 h2span_cluster_t * 1117 dmsg_cluster_get(uuid_t *pfs_clid) 1118 { 1119 h2span_cluster_t dummy_cls; 1120 h2span_cluster_t *cls; 1121 1122 dummy_cls.pfs_clid = *pfs_clid; 1123 pthread_mutex_lock(&cluster_mtx); 1124 cls = RB_FIND(h2span_cluster_tree, &cluster_tree, &dummy_cls); 1125 if (cls) 1126 ++cls->refs; 1127 pthread_mutex_unlock(&cluster_mtx); 1128 return (cls); 1129 } 1130 1131 void 1132 dmsg_cluster_put(h2span_cluster_t *cls) 1133 { 1134 pthread_mutex_lock(&cluster_mtx); 1135 assert(cls->refs > 0); 1136 --cls->refs; 1137 if (RB_EMPTY(&cls->tree) && cls->refs == 0) { 1138 RB_REMOVE(h2span_cluster_tree, 1139 &cluster_tree, cls); 1140 dmsg_free(cls); 1141 } 1142 pthread_mutex_unlock(&cluster_mtx); 1143 } 1144 1145 /* 1146 * Obtain a stable handle to a specific cluster node given its uuid. 1147 * This handle does NOT lock in the route to the node and is typically 1148 * used as part of the dmsg_handle_*() API to obtain a set of 1149 * stable nodes. 1150 */ 1151 h2span_node_t * 1152 dmsg_node_get(h2span_cluster_t *cls, uuid_t *pfs_fsid) 1153 { 1154 } 1155 1156 #endif 1157 1158 /* 1159 * Dumps the spanning tree 1160 * 1161 * DEBUG ONLY 1162 */ 1163 void 1164 dmsg_shell_tree(dmsg_iocom_t *iocom, char *cmdbuf __unused) 1165 { 1166 h2span_cluster_t *cls; 1167 h2span_node_t *node; 1168 h2span_link_t *slink; 1169 h2span_relay_t *relay; 1170 char *uustr = NULL; 1171 1172 pthread_mutex_lock(&cluster_mtx); 1173 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) { 1174 dmsg_printf(iocom, "Cluster %s %s (%s)\n", 1175 dmsg_peer_type_to_str(cls->peer_type), 1176 dmsg_uuid_to_str(&cls->pfs_clid, &uustr), 1177 cls->cl_label); 1178 RB_FOREACH(node, h2span_node_tree, &cls->tree) { 1179 dmsg_printf(iocom, " Node %02x %s (%s)\n", 1180 node->pfs_type, 1181 dmsg_uuid_to_str(&node->pfs_fsid, &uustr), 1182 node->fs_label); 1183 RB_FOREACH(slink, h2span_link_tree, &node->tree) { 1184 dmsg_printf(iocom, 1185 "\tSLink msgid %016jx " 1186 "dist=%d via %d\n", 1187 (intmax_t)slink->state->msgid, 1188 slink->lnk_span.dist, 1189 slink->state->iocom->sock_fd); 1190 TAILQ_FOREACH(relay, &slink->relayq, entry) { 1191 dmsg_printf(iocom, 1192 "\t Relay-out msgid %016jx " 1193 "via %d\n", 1194 (intmax_t)relay->target_rt->msgid, 1195 relay->target_rt->iocom->sock_fd); 1196 } 1197 } 1198 } 1199 } 1200 pthread_mutex_unlock(&cluster_mtx); 1201 if (uustr) 1202 free(uustr); 1203 #if 0 1204 TAILQ_FOREACH(conn, &connq, entry) { 1205 } 1206 #endif 1207 } 1208 1209 /* 1210 * DEBUG ONLY 1211 * 1212 * Locate the state representing an incoming LNK_SPAN given its msgid. 1213 */ 1214 int 1215 dmsg_debug_findspan(uint64_t msgid, dmsg_state_t **statep) 1216 { 1217 h2span_cluster_t *cls; 1218 h2span_node_t *node; 1219 h2span_link_t *slink; 1220 1221 pthread_mutex_lock(&cluster_mtx); 1222 RB_FOREACH(cls, h2span_cluster_tree, &cluster_tree) { 1223 RB_FOREACH(node, h2span_node_tree, &cls->tree) { 1224 RB_FOREACH(slink, h2span_link_tree, &node->tree) { 1225 if (slink->state->msgid == msgid) { 1226 *statep = slink->state; 1227 goto found; 1228 } 1229 } 1230 } 1231 } 1232 pthread_mutex_unlock(&cluster_mtx); 1233 *statep = NULL; 1234 return(ENOENT); 1235 found: 1236 pthread_mutex_unlock(&cluster_mtx); 1237 return(0); 1238 } 1239 1240 /* 1241 * Random number sub-sort value to add to SPAN rnss fields on relay. 1242 * This allows us to differentiate spans with the same <dist> field 1243 * for relaying purposes. We must normally limit the number of relays 1244 * for any given SPAN origination but we must also guarantee that a 1245 * symmetric reverse path exists, so we use the rnss field as a sub-sort 1246 * (since there can be thousands or millions if we only match on <dist>), 1247 * and if there STILL too many spans we go past the limit. 1248 */ 1249 static 1250 uint32_t 1251 dmsg_rnss(void) 1252 { 1253 if (DMsgRNSS == 0) { 1254 pthread_mutex_lock(&cluster_mtx); 1255 while (DMsgRNSS == 0) { 1256 srandomdev(); 1257 DMsgRNSS = random(); 1258 } 1259 pthread_mutex_unlock(&cluster_mtx); 1260 } 1261 return(DMsgRNSS); 1262 } 1263