2 * Copyright 2009 Sandia Corporation. Under the terms of Contract
3 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
4 * certain rights in this software.
5 * Copyright (c) 2009-2011 ZIH, TU Dresden, Federal Republic of Germany. All rights reserved.
6 * Copyright (c) 2010-2012 Mellanox Technologies LTD. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #define _WITH_GETLINE /* for getline() */
49 #endif /* HAVE_CONFIG_H */
51 #include <opensm/osm_file_ids.h>
52 #define FILE_ID OSM_FILE_TORUS_C
53 #include <opensm/osm_log.h>
54 #include <opensm/osm_port.h>
55 #include <opensm/osm_switch.h>
56 #include <opensm/osm_node.h>
57 #include <opensm/osm_opensm.h>
59 #define TORUS_MAX_DIM 3
60 #define PORTGRP_MAX_PORTS 16
61 #define SWITCH_MAX_PORTGRPS (1 + 2 * TORUS_MAX_DIM)
62 #define DEFAULT_MAX_CHANGES 32
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
66 typedef ib_net64_t guid_t;
69 * An endpoint terminates a link, and is one of three types:
70 * UNKNOWN - Uninitialized endpoint.
71 * SRCSINK - generates or consumes traffic, and thus has an associated LID;
72 * i.e. a CA or router port.
73 * PASSTHRU - Has no associated LID; i.e. a switch port.
75 * If it is possible to communicate in-band with a switch, it will require
76 * a port with a GUID in the switch to source/sink that traffic, but there
77 * will be no attached link. This code assumes there is only one such port.
79 * Here is an endpoint taxonomy:
82 * link == pointer to a valid struct link
83 * ==> This endpoint is a CA or router port connected via a link to
84 * either a switch or another CA/router. Thus:
85 * n_id ==> identifies the CA/router node GUID
87 * port ==> identifies the port on the CA/router this endpoint uses
91 * link == NULL pointer
92 * ==> This endpoint is the switch port used for in-band communication
93 * with the switch itself. Thus:
94 * n_id ==> identifies the node GUID used to talk to the switch
95 * containing this endpoint
96 * sw ==> pointer to valid struct switch containing this endpoint
97 * port ==> identifies the port on the switch this endpoint uses
98 * pgrp ==> NULL, or pointer to the valid struct port_grp holding
99 * the port in a t_switch.
102 * link == pointer to valid struct link
103 * ==> This endpoint is a switch port connected via a link to either
104 * another switch or a CA/router. Thus:
105 * n_id ==> identifies the node GUID used to talk to the switch
106 * containing this endpoint - since each switch is assumed
107 * to have only one in-band communication port, this is a
108 * convenient unique name for the switch itself.
109 * sw ==> pointer to valid struct switch containing this endpoint,
110 * or NULL, in the case of a fabric link that has been
111 * disconnected after being transferred to a torus link.
112 * port ==> identifies the port on the switch this endpoint uses.
113 * Note that in the special case of the coordinate direction
114 * links, the port value is -1, as those links aren't
115 * really connected to anything.
116 * pgrp ==> NULL, or pointer to the valid struct port_grp holding
117 * the port in a t_switch.
119 enum endpt_type { UNKNOWN = 0, SRCSINK, PASSTHRU };
125 enum endpt_type type;
127 guid_t n_id; /* IBA node GUID */
128 void *sw; /* void* can point to either switch type */
130 struct port_grp *pgrp;
133 * Note: osm_port is only guaranteed to contain a valid pointer
134 * when the call stack contains torus_build_lfts() or
135 * osm_port_relink_endpoint().
137 * Otherwise, the opensm core could have deleted an osm_port object
138 * without notifying us, invalidating the pointer we hold.
140 * When presented with a pointer to an osm_port_t, it is generally
141 * safe and required to cast osm_port_t:priv to struct endpoint, and
142 * check that the endpoint's osm_port is the same as the original
143 * osm_port_t pointer. Failure to do so means that invalidated
144 * pointers will go undetected.
146 struct osm_port *osm_port;
150 struct endpoint end[2];
154 * A port group is a collection of endpoints on a switch that share certain
155 * characteristics. All the endpoints in a port group must have the same
156 * type. Furthermore, if that type is PASSTHRU, then the connected links:
157 * 1) are parallel to a given coordinate direction
158 * 2) share the same two switches as endpoints.
160 * Torus-2QoS uses one master spanning tree for multicast, of which every
161 * multicast group spanning tree is a subtree. to_stree_root is a pointer
162 * to the next port_grp on the path to the master spanning tree root.
163 * to_stree_tip is a pointer to the next port_grp on the path to a master
164 * spanning tree branch tip.
166 * Each t_switch can have at most one port_grp with a non-NULL to_stree_root.
167 * Exactly one t_switch in the fabric will have all port_grp objects with
168 * to_stree_root NULL; it is the master spanning tree root.
170 * A t_switch with all port_grp objects where to_stree_tip is NULL is at a
171 * master spanning tree branch tip.
174 enum endpt_type type;
175 size_t port_cnt; /* number of attached ports in group */
176 size_t port_grp; /* what switch port_grp we're in */
177 unsigned sw_dlid_cnt; /* switch dlids routed through this group */
178 unsigned ca_dlid_cnt; /* CA dlids routed through this group */
179 struct t_switch *sw; /* what switch we're attached to */
180 struct port_grp *to_stree_root;
181 struct port_grp *to_stree_tip;
182 struct endpoint **port;
186 * A struct t_switch is used to represent a switch as placed in a torus.
188 * A t_switch used to build an N-dimensional torus will have 2N+1 port groups,
189 * used as follows, assuming 0 <= d < N:
190 * port_grp[2d] => links leaving in negative direction for coordinate d
191 * port_grp[2d+1] => links leaving in positive direction for coordinate d
192 * port_grp[2N] => endpoints local to switch; i.e., hosts on switch
194 * struct link objects referenced by a t_switch are assumed to be oriented:
195 * traversing a link from link.end[0] to link.end[1] is always in the positive
196 * coordinate direction.
199 guid_t n_id; /* IBA node GUID */
201 unsigned port_cnt; /* including management port */
205 * Note: osm_switch is only guaranteed to contain a valid pointer
206 * when the call stack contains torus_build_lfts().
208 * Otherwise, the opensm core could have deleted an osm_switch object
209 * without notifying us, invalidating the pointer we hold.
211 * When presented with a pointer to an osm_switch_t, it is generally
212 * safe and required to cast osm_switch_t:priv to struct t_switch, and
213 * check that the switch's osm_switch is the same as the original
214 * osm_switch_t pointer. Failure to do so means that invalidated
215 * pointers will go undetected.
217 struct osm_switch *osm_switch;
219 struct port_grp ptgrp[SWITCH_MAX_PORTGRPS];
220 struct endpoint **port;
224 * We'd like to be able to discover the torus topology in a pile of switch
225 * links if we can. We'll use a struct f_switch to store raw topology for a
226 * fabric description, then contruct the torus topology from struct t_switch
227 * objects as we process the fabric and recover it.
230 guid_t n_id; /* IBA node GUID */
231 unsigned port_cnt; /* including management port */
234 * Same rules apply here as for a struct t_switch member osm_switch.
236 struct osm_switch *osm_switch;
237 struct endpoint **port;
246 unsigned link_cnt_max;
247 unsigned switch_cnt_max;
250 struct f_switch **sw;
255 * These links define the coordinate directions for the torus.
256 * They are duplicates of links connected to switches. Each of
257 * these links must connect to a common switch.
259 * In the event that a failed switch was specified as one of these
260 * link endpoints, our algorithm would not be able to find the
261 * torus in the fabric. So, we'll allow multiple instances of
262 * this in the config file to allow improved resiliency.
264 struct link xm_link, ym_link, zm_link;
265 struct link xp_link, yp_link, zp_link;
267 * A torus dimension has coordinate values 0, 1, ..., radix - 1.
268 * The dateline, where we need to change VLs to avoid credit loops,
269 * for a torus dimension is always between coordinate values
270 * radix - 1 and 0. The following specify the dateline location
271 * relative to the coordinate links shared switch location.
273 * E.g. if the shared switch is at 0,0,0, the following are all
274 * zero; if the shared switch is at 1,1,1, the following are all
277 * Since our SL/VL assignment for a path depends on the position
278 * of the path endpoints relative to the torus datelines, we need
279 * this information to keep SL/VL assignment constant in the event
280 * one of the switches used to specify coordinate directions fails.
282 int x_dateline, y_dateline, z_dateline;
290 unsigned seed_cnt, seed_idx;
291 unsigned x_sz, y_sz, z_sz;
293 unsigned port_order[IB_NODE_NUM_PORTS_MAX+1];
296 unsigned link_pool_sz;
298 unsigned portgrp_sz; /* max ports for port groups in this torus */
300 struct fabric *fabric;
301 struct t_switch **sw_pool;
302 struct link *link_pool;
304 struct coord_dirs *seed;
305 struct t_switch ****sw;
306 struct t_switch *master_stree_root;
309 unsigned max_changes;
314 * Bits to use in torus.flags
316 #define X_MESH (1U << 0)
317 #define Y_MESH (1U << 1)
318 #define Z_MESH (1U << 2)
319 #define MSG_DEADLOCK (1U << 29)
320 #define NOTIFY_CHANGES (1U << 30)
322 #define ALL_MESH(flags) \
323 ((flags & (X_MESH | Y_MESH | Z_MESH)) == (X_MESH | Y_MESH | Z_MESH))
326 struct torus_context {
329 struct fabric fabric;
333 void teardown_fabric(struct fabric *f)
336 struct endpoint *port;
344 * Need to free switches, and also find/free the endpoints
345 * we allocated for switch management ports.
347 for (s = 0; s < f->switch_cnt; s++) {
352 for (p = 0; p < sw->port_cnt; p++) {
354 if (port && !port->link)
355 free(port); /* management port */
362 for (l = 0; l < f->link_cnt; l++)
368 memset(f, 0, sizeof(*f));
371 void teardown_torus(struct torus *t)
374 struct endpoint *port;
382 * Need to free switches, and also find/free the endpoints
383 * we allocated for switch management ports.
385 for (s = 0; s < t->switch_cnt; s++) {
390 for (p = 0; p < sw->port_cnt; p++) {
392 if (port && !port->link)
393 free(port); /* management port */
412 struct torus_context *torus_context_create(osm_opensm_t *osm)
414 struct torus_context *ctx;
416 ctx = calloc(1, sizeof(*ctx));
420 OSM_LOG(&osm->log, OSM_LOG_ERROR,
421 "ERR 4E01: calloc: %s\n", strerror(errno));
427 void torus_context_delete(void *context)
429 struct torus_context *ctx = context;
431 teardown_fabric(&ctx->fabric);
433 teardown_torus(ctx->torus);
438 bool grow_seed_array(struct torus *t, int new_seeds)
443 cnt = t->seed_cnt + new_seeds;
444 if (cnt > t->seed_sz) {
446 ptr = realloc(t->seed, cnt * sizeof(*t->seed));
451 memset(&t->seed[t->seed_cnt], 0,
452 (cnt - t->seed_cnt) * sizeof(*t->seed));
458 struct f_switch *find_f_sw(struct fabric *f, guid_t sw_guid)
464 for (s = 0; s < f->switch_cnt; s++) {
466 if (sw->n_id == sw_guid)
474 struct link *find_f_link(struct fabric *f,
475 guid_t guid0, int port0, guid_t guid1, int port1)
481 for (l = 0; l < f->link_cnt; l++) {
483 if ((link->end[0].n_id == guid0 &&
484 link->end[0].port == port0 &&
485 link->end[1].n_id == guid1 &&
486 link->end[1].port == port1) ||
487 (link->end[0].n_id == guid1 &&
488 link->end[0].port == port1 &&
489 link->end[1].n_id == guid0 &&
490 link->end[1].port == port0))
498 struct f_switch *alloc_fswitch(struct fabric *f,
499 guid_t sw_id, unsigned port_cnt)
503 struct f_switch *sw = NULL;
506 if (f->switch_cnt >= f->switch_cnt_max) {
508 cnt_max = 16 + 5 * f->switch_cnt_max / 4;
509 ptr = realloc(f->sw, cnt_max * sizeof(*f->sw));
511 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
512 "ERR 4E02: realloc: %s\n", strerror(errno));
516 f->switch_cnt_max = cnt_max;
517 memset(&f->sw[f->switch_cnt], 0,
518 (f->switch_cnt_max - f->switch_cnt)*sizeof(*f->sw));
520 new_sw_sz = sizeof(*sw) + port_cnt * sizeof(*sw->port);
521 sw = calloc(1, new_sw_sz);
523 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
524 "ERR 4E03: calloc: %s\n", strerror(errno));
527 sw->port = (void *)(sw + 1);
529 sw->port_cnt = port_cnt;
530 f->sw[f->switch_cnt++] = sw;
536 struct link *alloc_flink(struct fabric *f)
539 struct link *l = NULL;
542 if (f->link_cnt >= f->link_cnt_max) {
544 cnt_max = 16 + 5 * f->link_cnt_max / 4;
545 ptr = realloc(f->link, cnt_max * sizeof(*f->link));
547 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
548 "ERR 4E04: realloc: %s\n", strerror(errno));
552 f->link_cnt_max = cnt_max;
553 memset(&f->link[f->link_cnt], 0,
554 (f->link_cnt_max - f->link_cnt) * sizeof(*f->link));
556 l = calloc(1, sizeof(*l));
558 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
559 "ERR 4E05: calloc: %s\n", strerror(errno));
562 f->link[f->link_cnt++] = l;
568 * Caller must ensure osm_port points to a valid port which contains
569 * a valid osm_physp_t pointer for port 0, the switch management port.
572 bool build_sw_endpoint(struct fabric *f, osm_port_t *osm_port)
576 struct osm_switch *osm_sw;
579 bool success = false;
581 sw_port = osm_physp_get_port_num(osm_port->p_physp);
582 sw_guid = osm_node_get_node_guid(osm_port->p_node);
583 osm_sw = osm_port->p_node->sw;
586 * The switch must already exist.
588 sw = find_f_sw(f, sw_guid);
590 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
591 "ERR 4E06: missing switch w/GUID 0x%04"PRIx64"\n",
596 * The endpoint may already exist.
598 if (sw->port[sw_port]) {
599 if (sw->port[sw_port]->n_id == sw_guid) {
600 ep = sw->port[sw_port];
603 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
604 "ERR 4E07: switch port %d has id "
605 "0x%04"PRIx64", expected 0x%04"PRIx64"\n",
606 sw_port, cl_ntoh64(sw->port[sw_port]->n_id),
610 ep = calloc(1, sizeof(*ep));
612 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
613 "ERR 4E08: allocating endpoint: %s\n", strerror(errno));
622 sw->port[sw_port] = ep;
626 * Fabric objects are temporary, so don't set osm_sw/osm_port priv
627 * pointers using them. Wait until torus objects get constructed.
629 sw->osm_switch = osm_sw;
630 ep->osm_port = osm_port;
638 bool build_ca_link(struct fabric *f,
639 osm_port_t *osm_port_ca, guid_t sw_guid, int sw_port)
645 bool success = false;
647 ca_port = osm_physp_get_port_num(osm_port_ca->p_physp);
648 ca_guid = osm_node_get_node_guid(osm_port_ca->p_node);
651 * The link may already exist.
653 l = find_f_link(f, sw_guid, sw_port, ca_guid, ca_port);
659 * The switch must already exist.
661 sw = find_f_sw(f, sw_guid);
663 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
664 "ERR 4E09: missing switch w/GUID 0x%04"PRIx64"\n",
672 l->end[0].type = PASSTHRU;
673 l->end[0].port = sw_port;
674 l->end[0].n_id = sw_guid;
678 sw->port[sw_port] = &l->end[0];
680 l->end[1].type = SRCSINK;
681 l->end[1].port = ca_port;
682 l->end[1].n_id = ca_guid;
683 l->end[1].sw = NULL; /* Correct for a CA */
687 * Fabric objects are temporary, so don't set osm_sw/osm_port priv
688 * pointers using them. Wait until torus objects get constructed.
690 l->end[1].osm_port = osm_port_ca;
699 bool build_link(struct fabric *f,
700 guid_t sw_guid0, int sw_port0, guid_t sw_guid1, int sw_port1)
703 struct f_switch *sw0, *sw1;
704 bool success = false;
707 * The link may already exist.
709 l = find_f_link(f, sw_guid0, sw_port0, sw_guid1, sw_port1);
715 * The switches must already exist.
717 sw0 = find_f_sw(f, sw_guid0);
719 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
720 "ERR 4E0A: missing switch w/GUID 0x%04"PRIx64"\n",
721 cl_ntoh64(sw_guid0));
724 sw1 = find_f_sw(f, sw_guid1);
726 OSM_LOG(&f->osm->log, OSM_LOG_ERROR,
727 "ERR 4E0B: missing switch w/GUID 0x%04"PRIx64"\n",
728 cl_ntoh64(sw_guid1));
735 l->end[0].type = PASSTHRU;
736 l->end[0].port = sw_port0;
737 l->end[0].n_id = sw_guid0;
741 sw0->port[sw_port0] = &l->end[0];
743 l->end[1].type = PASSTHRU;
744 l->end[1].port = sw_port1;
745 l->end[1].n_id = sw_guid1;
749 sw1->port[sw_port1] = &l->end[1];
757 bool parse_size(unsigned *tsz, unsigned *tflags, unsigned mask,
758 const char *parse_sep)
760 char *val, *nextchar;
762 val = strtok(NULL, parse_sep);
765 *tsz = strtoul(val, &nextchar, 0);
767 if (*nextchar == 't' || *nextchar == 'T')
769 else if (*nextchar == 'm' || *nextchar == 'M')
772 * A torus of radix two is also a mesh of radix two
773 * with multiple links between switches in that direction.
775 * Make it so always, otherwise the failure case routing
776 * logic gets confused.
785 bool parse_torus(struct torus *t, const char *parse_sep)
787 unsigned i, j, k, cnt;
789 bool success = false;
792 * There can be only one. Ignore the imposters.
797 if (!parse_size(&t->x_sz, &t->flags, X_MESH, parse_sep))
800 if (!parse_size(&t->y_sz, &t->flags, Y_MESH, parse_sep))
803 if (!parse_size(&t->z_sz, &t->flags, Z_MESH, parse_sep))
807 * Set up a linear array of switch pointers big enough to hold
808 * all expected switches.
810 t->sw_pool_sz = t->x_sz * t->y_sz * t->z_sz;
811 t->sw_pool = calloc(t->sw_pool_sz, sizeof(*t->sw_pool));
813 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
814 "ERR 4E0C: Torus switch array calloc: %s\n",
819 * Set things up so that t->sw[i][j][k] can point to the i,j,k switch.
821 cnt = t->x_sz * (1 + t->y_sz * (1 + t->z_sz));
822 t->sw = malloc(cnt * sizeof(void *));
824 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
825 "ERR 4E0D: Torus switch array malloc: %s\n",
829 ptr = (void *)(t->sw);
831 ptr += t->x_sz * sizeof(void *);
832 for (i = 0; i < t->x_sz; i++) {
833 t->sw[i] = (void *)ptr;
834 ptr += t->y_sz * sizeof(void *);
836 for (i = 0; i < t->x_sz; i++)
837 for (j = 0; j < t->y_sz; j++) {
838 t->sw[i][j] = (void *)ptr;
839 ptr += t->z_sz * sizeof(void *);
842 for (i = 0; i < t->x_sz; i++)
843 for (j = 0; j < t->y_sz; j++)
844 for (k = 0; k < t->z_sz; k++)
845 t->sw[i][j][k] = NULL;
853 bool parse_unsigned(unsigned *result, const char *parse_sep)
855 char *val, *nextchar;
857 val = strtok(NULL, parse_sep);
860 *result = strtoul(val, &nextchar, 0);
865 bool parse_port_order(struct torus *t, const char *parse_sep)
869 for (i = 0; i < ARRAY_SIZE(t->port_order); i++) {
870 if (!parse_unsigned(&(t->port_order[i]), parse_sep))
873 for (j = 0; j < i; j++) {
874 if (t->port_order[j] == t->port_order[i]) {
875 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
876 "Ignored duplicate port %u in"
877 " port_order parsing\n",
879 i--; /* Ignore duplicate port number */
886 for (j = 0; j < ARRAY_SIZE(t->port_order); j++) {
887 for (k = 0; k < i; k++)
888 if (t->port_order[k] == j)
891 t->port_order[n++] = j;
898 bool parse_guid(struct torus *t, guid_t *guid, const char *parse_sep)
901 bool success = false;
903 val = strtok(NULL, parse_sep);
906 *guid = strtoull(val, NULL, 0);
907 *guid = cl_hton64(*guid);
915 bool parse_dir_link(int c_dir, struct torus *t, const char *parse_sep)
917 guid_t sw_guid0, sw_guid1;
919 bool success = false;
921 if (!parse_guid(t, &sw_guid0, parse_sep))
924 if (!parse_guid(t, &sw_guid1, parse_sep))
934 l = &t->seed[t->seed_cnt - 1].xm_link;
937 l = &t->seed[t->seed_cnt - 1].xp_link;
940 l = &t->seed[t->seed_cnt - 1].ym_link;
943 l = &t->seed[t->seed_cnt - 1].yp_link;
946 l = &t->seed[t->seed_cnt - 1].zm_link;
949 l = &t->seed[t->seed_cnt - 1].zp_link;
952 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
953 "ERR 4E0E: unknown link direction %d\n", c_dir);
956 l->end[0].type = PASSTHRU;
957 l->end[0].port = -1; /* We don't really connect. */
958 l->end[0].n_id = sw_guid0;
959 l->end[0].sw = NULL; /* Fix this up later. */
960 l->end[0].link = NULL; /* Fix this up later. */
962 l->end[1].type = PASSTHRU;
963 l->end[1].port = -1; /* We don't really connect. */
964 l->end[1].n_id = sw_guid1;
965 l->end[1].sw = NULL; /* Fix this up later. */
966 l->end[1].link = NULL; /* Fix this up later. */
974 bool parse_dir_dateline(int c_dir, struct torus *t, const char *parse_sep)
978 bool success = false;
980 val = strtok(NULL, parse_sep);
991 dl = &t->seed[t->seed_cnt - 1].x_dateline;
995 dl = &t->seed[t->seed_cnt - 1].y_dateline;
999 dl = &t->seed[t->seed_cnt - 1].z_dateline;
1003 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1004 "ERR 4E0F: unknown dateline direction %d\n", c_dir);
1007 *dl = strtol(val, NULL, 0);
1009 if ((*dl < 0 && *dl <= -max_dl) || *dl >= max_dl)
1010 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1011 "ERR 4E10: dateline value for coordinate direction %d "
1012 "must be %d < dl < %d\n",
1013 c_dir, -max_dl, max_dl);
1021 bool parse_config(const char *fn, struct fabric *f, struct torus *t)
1026 char *line_buf = NULL;
1027 const char *parse_sep = " \n\t\015";
1028 size_t line_buf_sz = 0;
1029 size_t line_cntr = 0;
1031 bool kw_success, success = true;
1033 if (!grow_seed_array(t, 2))
1036 for (i = 0; i < ARRAY_SIZE(t->port_order); i++)
1037 t->port_order[i] = i;
1039 fp = fopen(fn, "r");
1041 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1042 "ERR 4E11: Opening %s: %s\n", fn, strerror(errno));
1045 t->flags |= NOTIFY_CHANGES;
1046 t->portgrp_sz = PORTGRP_MAX_PORTS;
1047 t->max_changes = DEFAULT_MAX_CHANGES;
1050 llen = getline(&line_buf, &line_buf_sz, fp);
1056 keyword = strtok(line_buf, parse_sep);
1060 if (strcmp("torus", keyword) == 0) {
1061 kw_success = parse_torus(t, parse_sep);
1062 } else if (strcmp("mesh", keyword) == 0) {
1063 t->flags |= X_MESH | Y_MESH | Z_MESH;
1064 kw_success = parse_torus(t, parse_sep);
1065 } else if (strcmp("port_order", keyword) == 0) {
1066 kw_success = parse_port_order(t, parse_sep);
1067 } else if (strcmp("next_seed", keyword) == 0) {
1068 kw_success = grow_seed_array(t, 1);
1070 } else if (strcmp("portgroup_max_ports", keyword) == 0) {
1071 kw_success = parse_unsigned(&t->portgrp_sz, parse_sep);
1072 } else if (strcmp("xp_link", keyword) == 0) {
1075 kw_success = parse_dir_link(1, t, parse_sep);
1076 } else if (strcmp("xm_link", keyword) == 0) {
1079 kw_success = parse_dir_link(-1, t, parse_sep);
1080 } else if (strcmp("x_dateline", keyword) == 0) {
1083 kw_success = parse_dir_dateline(1, t, parse_sep);
1084 } else if (strcmp("yp_link", keyword) == 0) {
1087 kw_success = parse_dir_link(2, t, parse_sep);
1088 } else if (strcmp("ym_link", keyword) == 0) {
1091 kw_success = parse_dir_link(-2, t, parse_sep);
1092 } else if (strcmp("y_dateline", keyword) == 0) {
1095 kw_success = parse_dir_dateline(2, t, parse_sep);
1096 } else if (strcmp("zp_link", keyword) == 0) {
1099 kw_success = parse_dir_link(3, t, parse_sep);
1100 } else if (strcmp("zm_link", keyword) == 0) {
1103 kw_success = parse_dir_link(-3, t, parse_sep);
1104 } else if (strcmp("z_dateline", keyword) == 0) {
1107 kw_success = parse_dir_dateline(3, t, parse_sep);
1108 } else if (strcmp("max_changes", keyword) == 0) {
1109 kw_success = parse_unsigned(&t->max_changes, parse_sep);
1110 } else if (keyword[0] == '#')
1113 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1114 "ERR 4E12: no keyword found: line %u\n",
1115 (unsigned)line_cntr);
1119 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1120 "ERR 4E13: parsing '%s': line %u\n",
1121 keyword, (unsigned)line_cntr);
1123 success = success && kw_success;
1134 bool capture_fabric(struct fabric *fabric)
1136 osm_subn_t *subnet = &fabric->osm->subn;
1137 osm_switch_t *osm_sw;
1138 osm_physp_t *lphysp, *rphysp;
1140 osm_node_t *osm_node;
1141 cl_map_item_t *item;
1142 uint8_t ltype, rtype;
1145 bool success = true;
1147 OSM_LOG_ENTER(&fabric->osm->log);
1150 * On OpenSM data structures:
1152 * Apparently, every port in a fabric has an associated osm_physp_t,
1153 * but not every port has an associated osm_port_t. Apparently every
1154 * osm_port_t has an associated osm_physp_t.
1156 * So, in order to find the inter-switch links we need to walk the
1157 * switch list and examine each port, via its osm_physp_t object.
1159 * But, we need to associate our CA and switch management port
1160 * endpoints with the corresponding osm_port_t objects, in order
1161 * to simplify computation of LFT entries and perform SL lookup for
1162 * path records. Since it is apparently difficult to locate the
1163 * osm_port_t that corresponds to a given osm_physp_t, we also
1164 * need to walk the list of ports indexed by GUID to get access
1165 * to the appropriate osm_port_t objects.
1167 * Need to allocate our switches before we do anything else.
1169 item = cl_qmap_head(&subnet->sw_guid_tbl);
1170 while (item != cl_qmap_end(&subnet->sw_guid_tbl)) {
1172 osm_sw = (osm_switch_t *)item;
1173 item = cl_qmap_next(item);
1174 osm_sw->priv = NULL; /* avoid stale pointer dereferencing */
1175 osm_node = osm_sw->p_node;
1177 if (osm_node_get_type(osm_node) != IB_NODE_TYPE_SWITCH)
1180 port_cnt = osm_node_get_num_physp(osm_node);
1181 sw_guid = osm_node_get_node_guid(osm_node);
1183 success = alloc_fswitch(fabric, sw_guid, port_cnt);
1188 * Now build all our endpoints.
1190 item = cl_qmap_head(&subnet->port_guid_tbl);
1191 while (item != cl_qmap_end(&subnet->port_guid_tbl)) {
1193 lport = (osm_port_t *)item;
1194 item = cl_qmap_next(item);
1195 lport->priv = NULL; /* avoid stale pointer dereferencing */
1197 lphysp = lport->p_physp;
1198 if (!(lphysp && osm_physp_is_valid(lphysp)))
1201 ltype = osm_node_get_type(lphysp->p_node);
1203 * Switch management port is always port 0.
1205 if (lphysp->port_num == 0 && ltype == IB_NODE_TYPE_SWITCH) {
1206 success = build_sw_endpoint(fabric, lport);
1211 rphysp = lphysp->p_remote_physp;
1212 if (!(rphysp && osm_physp_is_valid(rphysp)))
1215 rtype = osm_node_get_type(rphysp->p_node);
1217 if ((ltype != IB_NODE_TYPE_CA &&
1218 ltype != IB_NODE_TYPE_ROUTER) ||
1219 rtype != IB_NODE_TYPE_SWITCH)
1223 build_ca_link(fabric, lport,
1224 osm_node_get_node_guid(rphysp->p_node),
1225 osm_physp_get_port_num(rphysp));
1230 * Lastly, build all our interswitch links.
1232 item = cl_qmap_head(&subnet->sw_guid_tbl);
1233 while (item != cl_qmap_end(&subnet->sw_guid_tbl)) {
1235 osm_sw = (osm_switch_t *)item;
1236 item = cl_qmap_next(item);
1238 port_cnt = osm_node_get_num_physp(osm_sw->p_node);
1239 for (p = 0; p < port_cnt; p++) {
1241 lphysp = osm_node_get_physp_ptr(osm_sw->p_node, p);
1242 if (!(lphysp && osm_physp_is_valid(lphysp)))
1245 rphysp = lphysp->p_remote_physp;
1246 if (!(rphysp && osm_physp_is_valid(rphysp)))
1249 if (lphysp == rphysp)
1250 continue; /* ignore loopbacks */
1252 ltype = osm_node_get_type(lphysp->p_node);
1253 rtype = osm_node_get_type(rphysp->p_node);
1255 if (ltype != IB_NODE_TYPE_SWITCH ||
1256 rtype != IB_NODE_TYPE_SWITCH)
1261 osm_node_get_node_guid(lphysp->p_node),
1262 osm_physp_get_port_num(lphysp),
1263 osm_node_get_node_guid(rphysp->p_node),
1264 osm_physp_get_port_num(rphysp));
1270 OSM_LOG_EXIT(&fabric->osm->log);
1275 * diagnose_fabric() is just intended to report on fabric elements that
1276 * could not be placed into the torus. We want to warn that there were
1277 * non-torus fabric elements, but they will be ignored for routing purposes.
1278 * Having them is not an error, and diagnose_fabric() thus has no return
1282 void diagnose_fabric(struct fabric *f)
1285 struct endpoint *ep;
1289 * Report on any links that didn't get transferred to the torus.
1291 for (k = 0; k < f->link_cnt; k++) {
1294 if (!(l->end[0].sw && l->end[1].sw))
1297 OSM_LOG(&f->osm->log, OSM_LOG_INFO,
1298 "Found non-torus fabric link:"
1299 " sw GUID 0x%04"PRIx64" port %d <->"
1300 " sw GUID 0x%04"PRIx64" port %d\n",
1301 cl_ntoh64(l->end[0].n_id), l->end[0].port,
1302 cl_ntoh64(l->end[1].n_id), l->end[1].port);
1305 * Report on any switches with ports using endpoints that didn't
1306 * get transferred to the torus.
1308 for (k = 0; k < f->switch_cnt; k++)
1309 for (p = 0; p < f->sw[k]->port_cnt; p++) {
1311 if (!f->sw[k]->port[p])
1314 ep = f->sw[k]->port[p];
1317 * We already reported on inter-switch links above.
1319 if (ep->type == PASSTHRU)
1322 OSM_LOG(&f->osm->log, OSM_LOG_INFO,
1323 "Found non-torus fabric port:"
1324 " sw GUID 0x%04"PRIx64" port %d\n",
1325 cl_ntoh64(f->sw[k]->n_id), p);
1330 struct t_switch *alloc_tswitch(struct torus *t, struct f_switch *fsw)
1334 struct t_switch *sw = NULL;
1340 if (t->switch_cnt >= t->sw_pool_sz) {
1342 * This should never happen, but occasionally a particularly
1343 * pathological fabric can induce it. So log an error.
1345 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1346 "ERR 4E14: unexpectedly requested too many switch "
1350 new_sw_sz = sizeof(*sw)
1351 + fsw->port_cnt * sizeof(*sw->port)
1352 + SWITCH_MAX_PORTGRPS * t->portgrp_sz * sizeof(*sw->ptgrp[0].port);
1353 sw = calloc(1, new_sw_sz);
1355 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1356 "ERR 4E15: calloc: %s\n", strerror(errno));
1359 sw->port = (void *)(sw + 1);
1360 sw->n_id = fsw->n_id;
1361 sw->port_cnt = fsw->port_cnt;
1365 ptr = &sw->port[sw->port_cnt];
1367 for (g = 0; g < SWITCH_MAX_PORTGRPS; g++) {
1368 sw->ptgrp[g].port_grp = g;
1369 sw->ptgrp[g].sw = sw;
1370 sw->ptgrp[g].port = ptr;
1371 ptr = &sw->ptgrp[g].port[t->portgrp_sz];
1373 t->sw_pool[t->switch_cnt++] = sw;
1379 * install_tswitch() expects the switch coordinates i,j,k to be canonicalized
1383 bool install_tswitch(struct torus *t,
1384 int i, int j, int k, struct f_switch *fsw)
1386 struct t_switch **sw = &t->sw[i][j][k];
1389 *sw = alloc_tswitch(t, fsw);
1400 struct link *alloc_tlink(struct torus *t)
1402 if (t->link_cnt >= t->link_pool_sz) {
1403 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1404 "ERR 4E16: unexpectedly out of pre-allocated link "
1408 return &t->link_pool[t->link_cnt++];
1412 int canonicalize(int v, int vmax)
1414 if (v >= 0 && v < vmax)
1418 v += vmax * (1 - v/vmax);
1424 unsigned set_fp_bit(bool present, int i, int j, int k)
1426 return (unsigned)(!present) << (i + 2 * j + 4 * k);
1430 * Returns an 11-bit fingerprint of what switches are absent in a cube of
1431 * neighboring switches. Each bit 0-7 corresponds to a corner of the cube;
1432 * if a bit is set the corresponding switch is absent.
1434 * Bits 8-10 distinguish between 2D and 3D cases. If bit 8+d is set,
1435 * for 0 <= d < 3; the d dimension of the desired torus has radix greater
1436 * than 1. Thus, if all bits 8-10 are set, the desired torus is 3D.
1439 unsigned fingerprint(struct torus *t, int i, int j, int k)
1443 int x_sz_gt1, y_sz_gt1, z_sz_gt1;
1445 x_sz_gt1 = t->x_sz > 1;
1446 y_sz_gt1 = t->y_sz > 1;
1447 z_sz_gt1 = t->z_sz > 1;
1449 ip1 = canonicalize(i + 1, t->x_sz);
1450 jp1 = canonicalize(j + 1, t->y_sz);
1451 kp1 = canonicalize(k + 1, t->z_sz);
1453 fp = set_fp_bit(t->sw[i][j][k], 0, 0, 0);
1454 fp |= set_fp_bit(t->sw[ip1][j][k], x_sz_gt1, 0, 0);
1455 fp |= set_fp_bit(t->sw[i][jp1][k], 0, y_sz_gt1, 0);
1456 fp |= set_fp_bit(t->sw[ip1][jp1][k], x_sz_gt1, y_sz_gt1, 0);
1457 fp |= set_fp_bit(t->sw[i][j][kp1], 0, 0, z_sz_gt1);
1458 fp |= set_fp_bit(t->sw[ip1][j][kp1], x_sz_gt1, 0, z_sz_gt1);
1459 fp |= set_fp_bit(t->sw[i][jp1][kp1], 0, y_sz_gt1, z_sz_gt1);
1460 fp |= set_fp_bit(t->sw[ip1][jp1][kp1], x_sz_gt1, y_sz_gt1, z_sz_gt1);
1462 fp |= x_sz_gt1 << 8;
1463 fp |= y_sz_gt1 << 9;
1464 fp |= z_sz_gt1 << 10;
1470 bool connect_tlink(struct port_grp *pg0, struct endpoint *f_ep0,
1471 struct port_grp *pg1, struct endpoint *f_ep1,
1475 bool success = false;
1477 if (pg0->port_cnt == t->portgrp_sz) {
1478 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1479 "ERR 4E17: exceeded port group max "
1480 "port count (%d): switch GUID 0x%04"PRIx64"\n",
1481 t->portgrp_sz, cl_ntoh64(pg0->sw->n_id));
1484 if (pg1->port_cnt == t->portgrp_sz) {
1485 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1486 "ERR 4E18: exceeded port group max "
1487 "port count (%d): switch GUID 0x%04"PRIx64"\n",
1488 t->portgrp_sz, cl_ntoh64(pg1->sw->n_id));
1495 l->end[0].type = f_ep0->type;
1496 l->end[0].port = f_ep0->port;
1497 l->end[0].n_id = f_ep0->n_id;
1498 l->end[0].sw = pg0->sw;
1500 l->end[0].pgrp = pg0;
1501 pg0->port[pg0->port_cnt++] = &l->end[0];
1502 pg0->sw->port[f_ep0->port] = &l->end[0];
1504 if (f_ep0->osm_port) {
1505 l->end[0].osm_port = f_ep0->osm_port;
1506 l->end[0].osm_port->priv = &l->end[0];
1507 f_ep0->osm_port = NULL;
1510 l->end[1].type = f_ep1->type;
1511 l->end[1].port = f_ep1->port;
1512 l->end[1].n_id = f_ep1->n_id;
1513 l->end[1].sw = pg1->sw;
1515 l->end[1].pgrp = pg1;
1516 pg1->port[pg1->port_cnt++] = &l->end[1];
1517 pg1->sw->port[f_ep1->port] = &l->end[1];
1519 if (f_ep1->osm_port) {
1520 l->end[1].osm_port = f_ep1->osm_port;
1521 l->end[1].osm_port->priv = &l->end[1];
1522 f_ep1->osm_port = NULL;
1525 * Disconnect fabric link, so that later we can see if any were
1526 * left unconnected in the torus.
1528 ((struct f_switch *)f_ep0->sw)->port[f_ep0->port] = NULL;
1532 ((struct f_switch *)f_ep1->sw)->port[f_ep1->port] = NULL;
1542 bool link_tswitches(struct torus *t, int cdir,
1543 struct t_switch *t_sw0, struct t_switch *t_sw1)
1546 struct port_grp *pg0, *pg1;
1547 struct f_switch *f_sw0, *f_sw1;
1548 const char *cdir_name = "unknown";
1550 int success = false;
1553 * If this is a 2D torus, it is possible for this function to be
1554 * called with its two switch arguments being the same switch, in
1555 * which case there are no links to install.
1557 if (t_sw0 == t_sw1 &&
1558 ((cdir == 0 && t->x_sz == 1) ||
1559 (cdir == 1 && t->y_sz == 1) ||
1560 (cdir == 2 && t->z_sz == 1))) {
1565 * Ensure that t_sw1 is in the positive cdir direction wrt. t_sw0.
1566 * ring_next_sw() relies on it.
1571 canonicalize(t_sw0->i + 1, t->x_sz) != t_sw1->i) {
1578 canonicalize(t_sw0->j + 1, t->y_sz) != t_sw1->j) {
1585 canonicalize(t_sw0->k + 1, t->z_sz) != t_sw1->k) {
1592 OSM_LOG(&t->osm->log, OSM_LOG_ERROR, "ERR 4E19: "
1593 "sw 0x%04"PRIx64" (%d,%d,%d) <--> "
1594 "sw 0x%04"PRIx64" (%d,%d,%d) "
1595 "invalid torus %s link orientation\n",
1596 cl_ntoh64(t_sw0->n_id), t_sw0->i, t_sw0->j, t_sw0->k,
1597 cl_ntoh64(t_sw1->n_id), t_sw1->i, t_sw1->j, t_sw1->k,
1605 if (!f_sw0 || !f_sw1) {
1606 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1607 "ERR 4E1A: missing fabric switches!\n"
1608 " switch GUIDs: 0x%04"PRIx64" 0x%04"PRIx64"\n",
1609 cl_ntoh64(t_sw0->n_id), cl_ntoh64(t_sw1->n_id));
1612 pg0 = &t_sw0->ptgrp[2*cdir + 1];
1613 pg0->type = PASSTHRU;
1615 pg1 = &t_sw1->ptgrp[2*cdir];
1616 pg1->type = PASSTHRU;
1618 port_cnt = f_sw0->port_cnt;
1620 * Find all the links between these two switches.
1622 for (p = 0; p < port_cnt; p++) {
1623 struct endpoint *f_ep0 = NULL, *f_ep1 = NULL;
1625 if (!f_sw0->port[p] || !f_sw0->port[p]->link)
1628 if (f_sw0->port[p]->link->end[0].n_id == t_sw0->n_id &&
1629 f_sw0->port[p]->link->end[1].n_id == t_sw1->n_id) {
1631 f_ep0 = &f_sw0->port[p]->link->end[0];
1632 f_ep1 = &f_sw0->port[p]->link->end[1];
1633 } else if (f_sw0->port[p]->link->end[1].n_id == t_sw0->n_id &&
1634 f_sw0->port[p]->link->end[0].n_id == t_sw1->n_id) {
1636 f_ep0 = &f_sw0->port[p]->link->end[1];
1637 f_ep1 = &f_sw0->port[p]->link->end[0];
1641 if (!(f_ep0->type == PASSTHRU && f_ep1->type == PASSTHRU)) {
1642 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1643 "ERR 4E1B: not interswitch "
1644 "link:\n 0x%04"PRIx64"/%d <-> 0x%04"PRIx64"/%d\n",
1645 cl_ntoh64(f_ep0->n_id), f_ep0->port,
1646 cl_ntoh64(f_ep1->n_id), f_ep1->port);
1650 * Skip over links that already have been established in the
1653 if (!(f_ep0->sw && f_ep1->sw))
1656 if (!connect_tlink(pg0, f_ep0, pg1, f_ep1, t))
1665 bool link_srcsink(struct torus *t, int i, int j, int k)
1667 struct endpoint *f_ep0;
1668 struct endpoint *f_ep1;
1669 struct t_switch *tsw;
1670 struct f_switch *fsw;
1671 struct port_grp *pg;
1672 struct link *fl, *tl;
1673 unsigned p, port_cnt;
1674 bool success = false;
1676 i = canonicalize(i, t->x_sz);
1677 j = canonicalize(j, t->y_sz);
1678 k = canonicalize(k, t->z_sz);
1680 tsw = t->sw[i][j][k];
1686 * link_srcsink is supposed to get called once for every switch in
1687 * the fabric. At this point every fsw we encounter must have a
1688 * non-null osm_switch. Otherwise something has gone horribly
1689 * wrong with topology discovery; the most likely reason is that
1690 * the fabric contains a radix-4 torus dimension, but the user gave
1691 * a config that didn't say so, breaking all the checking in
1692 * safe_x_perpendicular and friends.
1694 if (!(fsw && fsw->osm_switch)) {
1695 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1696 "ERR 4E1C: Invalid topology discovery. "
1697 "Verify torus-2QoS.conf contents.\n");
1701 pg = &tsw->ptgrp[2 * TORUS_MAX_DIM];
1703 tsw->osm_switch = fsw->osm_switch;
1704 tsw->osm_switch->priv = tsw;
1705 fsw->osm_switch = NULL;
1707 port_cnt = fsw->port_cnt;
1708 for (p = 0; p < port_cnt; p++) {
1713 if (fsw->port[p]->type == SRCSINK) {
1715 * If the endpoint is the switch port used for in-band
1716 * communication with the switch itself, move it to
1719 if (pg->port_cnt == t->portgrp_sz) {
1720 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1721 "ERR 4E1D: exceeded port group max port "
1722 "count (%d): switch GUID 0x%04"PRIx64"\n",
1723 t->portgrp_sz, cl_ntoh64(tsw->n_id));
1726 fsw->port[p]->sw = tsw;
1727 fsw->port[p]->pgrp = pg;
1728 tsw->port[p] = fsw->port[p];
1729 tsw->port[p]->osm_port->priv = tsw->port[p];
1730 pg->port[pg->port_cnt++] = fsw->port[p];
1731 fsw->port[p] = NULL;
1733 } else if (fsw->port[p]->link &&
1734 fsw->port[p]->type == PASSTHRU) {
1736 * If the endpoint is a link to a CA, create a new link
1737 * in the torus. Disconnect the fabric link.
1740 fl = fsw->port[p]->link;
1742 if (fl->end[0].sw == fsw) {
1743 f_ep0 = &fl->end[0];
1744 f_ep1 = &fl->end[1];
1745 } else if (fl->end[1].sw == fsw) {
1746 f_ep1 = &fl->end[0];
1747 f_ep0 = &fl->end[1];
1751 if (f_ep1->type != SRCSINK)
1754 if (pg->port_cnt == t->portgrp_sz) {
1755 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1756 "ERR 4E1E: exceeded port group max port "
1757 "count (%d): switch GUID 0x%04"PRIx64"\n",
1758 t->portgrp_sz, cl_ntoh64(tsw->n_id));
1762 * Switch ports connected to links don't get
1763 * associated with osm_port_t objects; see
1764 * capture_fabric(). So just check CA end.
1766 if (!f_ep1->osm_port) {
1767 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
1768 "ERR 4E1F: NULL osm_port->priv port "
1769 "GUID 0x%04"PRIx64"\n",
1770 cl_ntoh64(f_ep1->n_id));
1773 tl = alloc_tlink(t);
1777 tl->end[0].type = f_ep0->type;
1778 tl->end[0].port = f_ep0->port;
1779 tl->end[0].n_id = f_ep0->n_id;
1780 tl->end[0].sw = tsw;
1781 tl->end[0].link = tl;
1782 tl->end[0].pgrp = pg;
1783 pg->port[pg->port_cnt++] = &tl->end[0];
1784 pg->sw->port[f_ep0->port] = &tl->end[0];
1786 tl->end[1].type = f_ep1->type;
1787 tl->end[1].port = f_ep1->port;
1788 tl->end[1].n_id = f_ep1->n_id;
1789 tl->end[1].sw = NULL; /* Correct for a CA */
1790 tl->end[1].link = tl;
1791 tl->end[1].pgrp = NULL; /* Correct for a CA */
1793 tl->end[1].osm_port = f_ep1->osm_port;
1794 tl->end[1].osm_port->priv = &tl->end[1];
1795 f_ep1->osm_port = NULL;
1800 fsw->port[p] = NULL;
1809 struct f_switch *ffind_face_corner(struct f_switch *fsw0,
1810 struct f_switch *fsw1,
1811 struct f_switch *fsw2)
1815 struct endpoint *far_end;
1816 struct f_switch *fsw, *fsw3 = NULL;
1818 if (!(fsw0 && fsw1 && fsw2))
1821 for (p0 = 0; p0 < fsw0->port_cnt; p0++) {
1823 * Ignore everything except switch links that haven't
1824 * been installed into the torus.
1826 if (!(fsw0->port[p0] && fsw0->port[p0]->sw &&
1827 fsw0->port[p0]->type == PASSTHRU))
1830 l = fsw0->port[p0]->link;
1832 if (l->end[0].n_id == fsw0->n_id)
1833 far_end = &l->end[1];
1835 far_end = &l->end[0];
1840 if (!(far_end->type == PASSTHRU && far_end->sw))
1844 if (fsw3->n_id == fsw1->n_id) /* existing corner */
1847 for (p3 = 0; p3 < fsw3->port_cnt; p3++) {
1849 * Ignore everything except switch links that haven't
1850 * been installed into the torus.
1852 if (!(fsw3->port[p3] && fsw3->port[p3]->sw &&
1853 fsw3->port[p3]->type == PASSTHRU))
1856 l = fsw3->port[p3]->link;
1858 if (l->end[0].n_id == fsw3->n_id)
1859 far_end = &l->end[1];
1861 far_end = &l->end[0];
1866 if (!(far_end->type == PASSTHRU && far_end->sw))
1870 if (fsw->n_id == fsw2->n_id)
1880 struct f_switch *tfind_face_corner(struct t_switch *tsw0,
1881 struct t_switch *tsw1,
1882 struct t_switch *tsw2)
1884 if (!(tsw0 && tsw1 && tsw2))
1887 return ffind_face_corner(tsw0->tmp, tsw1->tmp, tsw2->tmp);
1891 * This code can break on any torus with a dimension that has radix four.
1893 * What is supposed to happen is that this code will find the
1894 * two faces whose shared edge is the desired perpendicular.
1896 * What actually happens is while searching we send two connected
1897 * edges that are colinear in a torus dimension with radix four to
1898 * ffind_face_corner(), which tries to complete a face by finding a
1901 * In the radix four torus case, it can find a 4-loop which is a ring in a
1902 * dimension with radix four, rather than the desired face. It thus returns
1903 * true when it shouldn't, so the wrong edge is returned as the perpendicular.
1905 * The appropriate instance of safe_N_perpendicular() (where N == x, y, z)
1906 * should be used to determine if it is safe to call ffind_perpendicular();
1907 * these functions will return false it there is a possibility of finding
1908 * a wrong perpendicular.
1910 struct f_switch *ffind_3d_perpendicular(struct f_switch *fsw0,
1911 struct f_switch *fsw1,
1912 struct f_switch *fsw2,
1913 struct f_switch *fsw3)
1917 struct endpoint *far_end;
1918 struct f_switch *fsw4 = NULL;
1920 if (!(fsw0 && fsw1 && fsw2 && fsw3))
1924 * Look at all the ports on the switch, fsw1, that is the base of
1925 * the perpendicular.
1927 for (p1 = 0; p1 < fsw1->port_cnt; p1++) {
1929 * Ignore everything except switch links that haven't
1930 * been installed into the torus.
1932 if (!(fsw1->port[p1] && fsw1->port[p1]->sw &&
1933 fsw1->port[p1]->type == PASSTHRU))
1936 l = fsw1->port[p1]->link;
1938 if (l->end[0].n_id == fsw1->n_id)
1939 far_end = &l->end[1];
1941 far_end = &l->end[0];
1945 if (!(far_end->type == PASSTHRU && far_end->sw))
1949 if (fsw4->n_id == fsw3->n_id) /* wrong perpendicular */
1952 if (ffind_face_corner(fsw0, fsw1, fsw4) &&
1953 ffind_face_corner(fsw2, fsw1, fsw4))
1960 struct f_switch *ffind_2d_perpendicular(struct f_switch *fsw0,
1961 struct f_switch *fsw1,
1962 struct f_switch *fsw2)
1966 struct endpoint *far_end;
1967 struct f_switch *fsw3 = NULL;
1969 if (!(fsw0 && fsw1 && fsw2))
1973 * Look at all the ports on the switch, fsw1, that is the base of
1974 * the perpendicular.
1976 for (p1 = 0; p1 < fsw1->port_cnt; p1++) {
1978 * Ignore everything except switch links that haven't
1979 * been installed into the torus.
1981 if (!(fsw1->port[p1] && fsw1->port[p1]->sw &&
1982 fsw1->port[p1]->type == PASSTHRU))
1985 l = fsw1->port[p1]->link;
1987 if (l->end[0].n_id == fsw1->n_id)
1988 far_end = &l->end[1];
1990 far_end = &l->end[0];
1994 if (!(far_end->type == PASSTHRU && far_end->sw))
1998 if (fsw3->n_id == fsw2->n_id) /* wrong perpendicular */
2001 if (ffind_face_corner(fsw0, fsw1, fsw3))
2010 struct f_switch *tfind_3d_perpendicular(struct t_switch *tsw0,
2011 struct t_switch *tsw1,
2012 struct t_switch *tsw2,
2013 struct t_switch *tsw3)
2015 if (!(tsw0 && tsw1 && tsw2 && tsw3))
2018 return ffind_3d_perpendicular(tsw0->tmp, tsw1->tmp,
2019 tsw2->tmp, tsw3->tmp);
2023 struct f_switch *tfind_2d_perpendicular(struct t_switch *tsw0,
2024 struct t_switch *tsw1,
2025 struct t_switch *tsw2)
2027 if (!(tsw0 && tsw1 && tsw2))
2030 return ffind_2d_perpendicular(tsw0->tmp, tsw1->tmp, tsw2->tmp);
2034 bool safe_x_ring(struct torus *t, int i, int j, int k)
2037 bool success = true;
2040 * If this x-direction radix-4 ring has at least two links
2041 * already installed into the torus, then this ring does not
2042 * prevent us from looking for y or z direction perpendiculars.
2044 * It is easier to check for the appropriate switches being installed
2045 * into the torus than it is to check for the links, so force the
2046 * link installation if the appropriate switches are installed.
2048 * Recall that canonicalize(n - 2, 4) == canonicalize(n + 2, 4).
2050 if (t->x_sz != 4 || t->flags & X_MESH)
2053 im1 = canonicalize(i - 1, t->x_sz);
2054 ip1 = canonicalize(i + 1, t->x_sz);
2055 ip2 = canonicalize(i + 2, t->x_sz);
2057 if (!!t->sw[im1][j][k] +
2058 !!t->sw[ip1][j][k] + !!t->sw[ip2][j][k] < 2) {
2062 if (t->sw[ip2][j][k] && t->sw[im1][j][k])
2063 success = link_tswitches(t, 0,
2068 if (t->sw[im1][j][k] && t->sw[i][j][k])
2069 success = link_tswitches(t, 0,
2074 if (t->sw[i][j][k] && t->sw[ip1][j][k])
2075 success = link_tswitches(t, 0,
2080 if (t->sw[ip1][j][k] && t->sw[ip2][j][k])
2081 success = link_tswitches(t, 0,
2090 bool safe_y_ring(struct torus *t, int i, int j, int k)
2093 bool success = true;
2096 * If this y-direction radix-4 ring has at least two links
2097 * already installed into the torus, then this ring does not
2098 * prevent us from looking for x or z direction perpendiculars.
2100 * It is easier to check for the appropriate switches being installed
2101 * into the torus than it is to check for the links, so force the
2102 * link installation if the appropriate switches are installed.
2104 * Recall that canonicalize(n - 2, 4) == canonicalize(n + 2, 4).
2106 if (t->y_sz != 4 || (t->flags & Y_MESH))
2109 jm1 = canonicalize(j - 1, t->y_sz);
2110 jp1 = canonicalize(j + 1, t->y_sz);
2111 jp2 = canonicalize(j + 2, t->y_sz);
2113 if (!!t->sw[i][jm1][k] +
2114 !!t->sw[i][jp1][k] + !!t->sw[i][jp2][k] < 2) {
2118 if (t->sw[i][jp2][k] && t->sw[i][jm1][k])
2119 success = link_tswitches(t, 1,
2124 if (t->sw[i][jm1][k] && t->sw[i][j][k])
2125 success = link_tswitches(t, 1,
2130 if (t->sw[i][j][k] && t->sw[i][jp1][k])
2131 success = link_tswitches(t, 1,
2136 if (t->sw[i][jp1][k] && t->sw[i][jp2][k])
2137 success = link_tswitches(t, 1,
2146 bool safe_z_ring(struct torus *t, int i, int j, int k)
2149 bool success = true;
2152 * If this z-direction radix-4 ring has at least two links
2153 * already installed into the torus, then this ring does not
2154 * prevent us from looking for x or y direction perpendiculars.
2156 * It is easier to check for the appropriate switches being installed
2157 * into the torus than it is to check for the links, so force the
2158 * link installation if the appropriate switches are installed.
2160 * Recall that canonicalize(n - 2, 4) == canonicalize(n + 2, 4).
2162 if (t->z_sz != 4 || t->flags & Z_MESH)
2165 km1 = canonicalize(k - 1, t->z_sz);
2166 kp1 = canonicalize(k + 1, t->z_sz);
2167 kp2 = canonicalize(k + 2, t->z_sz);
2169 if (!!t->sw[i][j][km1] +
2170 !!t->sw[i][j][kp1] + !!t->sw[i][j][kp2] < 2) {
2174 if (t->sw[i][j][kp2] && t->sw[i][j][km1])
2175 success = link_tswitches(t, 2,
2180 if (t->sw[i][j][km1] && t->sw[i][j][k])
2181 success = link_tswitches(t, 2,
2186 if (t->sw[i][j][k] && t->sw[i][j][kp1])
2187 success = link_tswitches(t, 2,
2192 if (t->sw[i][j][kp1] && t->sw[i][j][kp2])
2193 success = link_tswitches(t, 2,
2202 * These functions return true when it safe to call
2203 * tfind_3d_perpendicular()/ffind_3d_perpendicular().
2206 bool safe_x_perpendicular(struct torus *t, int i, int j, int k)
2209 * If the dimensions perpendicular to the search direction are
2210 * not radix 4 torus dimensions, it is always safe to search for
2213 * Here we are checking for enough appropriate links having been
2214 * installed into the torus to prevent an incorrect link from being
2215 * considered as a perpendicular candidate.
2217 return safe_y_ring(t, i, j, k) && safe_z_ring(t, i, j, k);
2221 bool safe_y_perpendicular(struct torus *t, int i, int j, int k)
2224 * If the dimensions perpendicular to the search direction are
2225 * not radix 4 torus dimensions, it is always safe to search for
2228 * Here we are checking for enough appropriate links having been
2229 * installed into the torus to prevent an incorrect link from being
2230 * considered as a perpendicular candidate.
2232 return safe_x_ring(t, i, j, k) && safe_z_ring(t, i, j, k);
2236 bool safe_z_perpendicular(struct torus *t, int i, int j, int k)
2239 * If the dimensions perpendicular to the search direction are
2240 * not radix 4 torus dimensions, it is always safe to search for
2243 * Implement this by checking for enough appropriate links having
2244 * been installed into the torus to prevent an incorrect link from
2245 * being considered as a perpendicular candidate.
2247 return safe_x_ring(t, i, j, k) && safe_y_ring(t, i, j, k);
2251 * Templates for determining 2D/3D case fingerprints. Recall that if
2252 * a fingerprint bit is set the corresponding switch is absent from
2253 * the all-switches-present template.
2255 * I.e., for the 2D case where the x,y dimensions have a radix greater
2256 * than one, and the z dimension has radix 1, fingerprint bits 4-7 are
2259 * For the 2D case where the x,z dimensions have a radix greater than
2260 * one, and the y dimension has radix 1, fingerprint bits 2,3,6,7 are
2263 * For the 2D case where the y,z dimensions have a radix greater than
2264 * one, and the x dimension has radix 1, fingerprint bits 1,3,5,7 are
2267 * Recall also that bits 8-10 distinguish between 2D and 3D cases.
2268 * If bit 8+d is set, for 0 <= d < 3; the d dimension of the desired
2269 * torus has radix greater than 1.
2274 * b0: t->sw[i ][j ][0 ]
2275 * b1: t->sw[i+1][j ][0 ]
2276 * b2: t->sw[i ][j+1][0 ]
2277 * b3: t->sw[i+1][j+1][0 ]
2280 * b0: t->sw[i ][0 ][k ] . .
2281 * b1: t->sw[i+1][0 ][k ] . .
2282 * b4: t->sw[i ][0 ][k+1] . .
2283 * b5: t->sw[i+1][0 ][k+1] . .
2286 * b0: t->sw[0 ][j ][k ]
2287 * b2: t->sw[0 ][j+1][k ]
2288 * b4: t->sw[0 ][j ][k+1]
2289 * b6: t->sw[0 ][j+1][k+1]
2295 * b0: t->sw[i ][j ][k ] . . .
2296 * b1: t->sw[i+1][j ][k ] . . .
2297 * b2: t->sw[i ][j+1][k ] . . .
2298 * b3: t->sw[i+1][j+1][k ] O . O
2299 * b4: t->sw[i ][j ][k+1] . . O . .
2300 * b5: t->sw[i+1][j ][k+1] . . . . . .
2301 * b6: t->sw[i ][j+1][k+1] . . . .
2302 * b7: t->sw[i+1][j+1][k+1] . . . . . .
2313 void log_no_crnr(struct torus *t, unsigned n,
2314 int case_i, int case_j, int case_k,
2315 int crnr_i, int crnr_j, int crnr_k)
2318 OSM_LOG(&t->osm->log, OSM_LOG_INFO, "Case 0x%03x "
2319 "@ %d %d %d: no corner @ %d %d %d\n",
2320 n, case_i, case_j, case_k, crnr_i, crnr_j, crnr_k);
2324 void log_no_perp(struct torus *t, unsigned n,
2325 int case_i, int case_j, int case_k,
2326 int perp_i, int perp_j, int perp_k)
2329 OSM_LOG(&t->osm->log, OSM_LOG_INFO, "Case 0x%03x "
2330 "@ %d %d %d: no perpendicular @ %d %d %d\n",
2331 n, case_i, case_j, case_k, perp_i, perp_j, perp_k);
2335 * Handle the 2D cases with a single existing edge.
2341 * b0: t->sw[i ][j ][0 ]
2342 * b1: t->sw[i+1][j ][0 ]
2347 * b0: t->sw[i ][0 ][k ]
2348 * b1: t->sw[i+1][0 ][k ]
2353 * b0: t->sw[0 ][j ][k ]
2354 * b2: t->sw[0 ][j+1][k ]
2359 bool handle_case_0x30c(struct torus *t, int i, int j, int k)
2361 int ip1 = canonicalize(i + 1, t->x_sz);
2362 int jm1 = canonicalize(j - 1, t->y_sz);
2363 int jp1 = canonicalize(j + 1, t->y_sz);
2365 if (safe_y_perpendicular(t, i, j, k) &&
2366 install_tswitch(t, i, jp1, k,
2367 tfind_2d_perpendicular(t->sw[ip1][j][k],
2369 t->sw[i][jm1][k]))) {
2372 log_no_perp(t, 0x30c, i, j, k, i, j, k);
2374 if (safe_y_perpendicular(t, ip1, j, k) &&
2375 install_tswitch(t, ip1, jp1, k,
2376 tfind_2d_perpendicular(t->sw[i][j][k],
2378 t->sw[ip1][jm1][k]))) {
2381 log_no_perp(t, 0x30c, i, j, k, ip1, j, k);
2386 bool handle_case_0x530(struct torus *t, int i, int j, int k)
2388 int ip1 = canonicalize(i + 1, t->x_sz);
2389 int km1 = canonicalize(k - 1, t->z_sz);
2390 int kp1 = canonicalize(k + 1, t->z_sz);
2392 if (safe_z_perpendicular(t, i, j, k) &&
2393 install_tswitch(t, i, j, kp1,
2394 tfind_2d_perpendicular(t->sw[ip1][j][k],
2396 t->sw[i][j][km1]))) {
2399 log_no_perp(t, 0x530, i, j, k, i, j, k);
2401 if (safe_z_perpendicular(t, ip1, j, k) &&
2402 install_tswitch(t, ip1, j, kp1,
2403 tfind_2d_perpendicular(t->sw[i][j][k],
2405 t->sw[ip1][j][km1]))) {
2408 log_no_perp(t, 0x530, i, j, k, ip1, j, k);
2413 bool handle_case_0x650(struct torus *t, int i, int j, int k)
2415 int jp1 = canonicalize(j + 1, t->y_sz);
2416 int km1 = canonicalize(k - 1, t->z_sz);
2417 int kp1 = canonicalize(k + 1, t->z_sz);
2419 if (safe_z_perpendicular(t, i, j, k) &&
2420 install_tswitch(t, i, j, kp1,
2421 tfind_2d_perpendicular(t->sw[i][jp1][k],
2423 t->sw[i][j][km1]))) {
2426 log_no_perp(t, 0x650, i, j, k, i, j, k);
2428 if (safe_z_perpendicular(t, i, jp1, k) &&
2429 install_tswitch(t, i, jp1, kp1,
2430 tfind_2d_perpendicular(t->sw[i][j][k],
2432 t->sw[i][jp1][km1]))) {
2435 log_no_perp(t, 0x650, i, j, k, i, jp1, k);
2442 * b1: t->sw[i+1][j ][0 ]
2444 * b3: t->sw[i+1][j+1][0 ]
2448 * b1: t->sw[i+1][0 ][k ] .
2450 * b5: t->sw[i+1][0 ][k+1] .
2454 * b2: t->sw[0 ][j+1][k ]
2456 * b6: t->sw[0 ][j+1][k+1]
2459 bool handle_case_0x305(struct torus *t, int i, int j, int k)
2461 int ip1 = canonicalize(i + 1, t->x_sz);
2462 int ip2 = canonicalize(i + 2, t->x_sz);
2463 int jp1 = canonicalize(j + 1, t->y_sz);
2465 if (safe_x_perpendicular(t, ip1, j, k) &&
2466 install_tswitch(t, i, j, k,
2467 tfind_2d_perpendicular(t->sw[ip1][jp1][k],
2469 t->sw[ip2][j][k]))) {
2472 log_no_perp(t, 0x305, i, j, k, ip1, j, k);
2474 if (safe_x_perpendicular(t, ip1, jp1, k) &&
2475 install_tswitch(t, i, jp1, k,
2476 tfind_2d_perpendicular(t->sw[ip1][j][k],
2478 t->sw[ip2][jp1][k]))) {
2481 log_no_perp(t, 0x305, i, j, k, ip1, jp1, k);
2486 bool handle_case_0x511(struct torus *t, int i, int j, int k)
2488 int ip1 = canonicalize(i + 1, t->x_sz);
2489 int ip2 = canonicalize(i + 2, t->x_sz);
2490 int kp1 = canonicalize(k + 1, t->z_sz);
2492 if (safe_x_perpendicular(t, ip1, j, k) &&
2493 install_tswitch(t, i, j, k,
2494 tfind_2d_perpendicular(t->sw[ip1][j][kp1],
2496 t->sw[ip2][j][k]))) {
2499 log_no_perp(t, 0x511, i, j, k, ip1, j, k);
2501 if (safe_x_perpendicular(t, ip1, j, kp1) &&
2502 install_tswitch(t, i, j, kp1,
2503 tfind_2d_perpendicular(t->sw[ip1][j][k],
2505 t->sw[ip2][j][kp1]))) {
2508 log_no_perp(t, 0x511, i, j, k, ip1, j, kp1);
2513 bool handle_case_0x611(struct torus *t, int i, int j, int k)
2515 int jp1 = canonicalize(j + 1, t->y_sz);
2516 int jp2 = canonicalize(j + 2, t->y_sz);
2517 int kp1 = canonicalize(k + 1, t->z_sz);
2519 if (safe_y_perpendicular(t, i, jp1, k) &&
2520 install_tswitch(t, i, j, k,
2521 tfind_2d_perpendicular(t->sw[i][jp1][kp1],
2523 t->sw[i][jp2][k]))) {
2526 log_no_perp(t, 0x611, i, j, k, i, jp1, k);
2528 if (safe_y_perpendicular(t, i, jp1, kp1) &&
2529 install_tswitch(t, i, j, kp1,
2530 tfind_2d_perpendicular(t->sw[i][jp1][k],
2532 t->sw[i][jp2][kp1]))) {
2535 log_no_perp(t, 0x611, i, j, k, i, jp1, kp1);
2543 * b2: t->sw[i ][j+1][0 ]
2544 * b3: t->sw[i+1][j+1][0 ]
2549 * b4: t->sw[i ][0 ][k+1]
2550 * b5: t->sw[i+1][0 ][k+1]
2555 * b4: t->sw[0 ][j ][k+1]
2556 * b6: t->sw[0 ][j+1][k+1]
2559 bool handle_case_0x303(struct torus *t, int i, int j, int k)
2561 int ip1 = canonicalize(i + 1, t->x_sz);
2562 int jp1 = canonicalize(j + 1, t->y_sz);
2563 int jp2 = canonicalize(j + 2, t->y_sz);
2565 if (safe_y_perpendicular(t, i, jp1, k) &&
2566 install_tswitch(t, i, j, k,
2567 tfind_2d_perpendicular(t->sw[ip1][jp1][k],
2569 t->sw[i][jp2][k]))) {
2572 log_no_perp(t, 0x303, i, j, k, i, jp1, k);
2574 if (safe_y_perpendicular(t, ip1, jp1, k) &&
2575 install_tswitch(t, ip1, j, k,
2576 tfind_2d_perpendicular(t->sw[i][jp1][k],
2578 t->sw[ip1][jp2][k]))) {
2581 log_no_perp(t, 0x303, i, j, k, ip1, jp1, k);
2586 bool handle_case_0x503(struct torus *t, int i, int j, int k)
2588 int ip1 = canonicalize(i + 1, t->x_sz);
2589 int kp1 = canonicalize(k + 1, t->z_sz);
2590 int kp2 = canonicalize(k + 2, t->z_sz);
2592 if (safe_z_perpendicular(t, i, j, kp1) &&
2593 install_tswitch(t, i, j, k,
2594 tfind_2d_perpendicular(t->sw[ip1][j][kp1],
2596 t->sw[i][j][kp2]))) {
2599 log_no_perp(t, 0x503, i, j, k, i, j, kp1);
2601 if (safe_z_perpendicular(t, ip1, j, kp1) &&
2602 install_tswitch(t, ip1, j, k,
2603 tfind_2d_perpendicular(t->sw[i][j][kp1],
2605 t->sw[ip1][j][kp2]))) {
2608 log_no_perp(t, 0x503, i, j, k, ip1, j, kp1);
2613 bool handle_case_0x605(struct torus *t, int i, int j, int k)
2615 int jp1 = canonicalize(j + 1, t->y_sz);
2616 int kp1 = canonicalize(k + 1, t->z_sz);
2617 int kp2 = canonicalize(k + 2, t->z_sz);
2619 if (safe_z_perpendicular(t, i, j, kp1) &&
2620 install_tswitch(t, i, j, k,
2621 tfind_2d_perpendicular(t->sw[i][jp1][kp1],
2623 t->sw[i][j][kp2]))) {
2626 log_no_perp(t, 0x605, i, j, k, i, j, kp1);
2628 if (safe_z_perpendicular(t, i, jp1, kp1) &&
2629 install_tswitch(t, i, jp1, k,
2630 tfind_2d_perpendicular(t->sw[i][j][kp1],
2632 t->sw[i][jp1][kp2]))) {
2635 log_no_perp(t, 0x605, i, j, k, i, jp1, kp1);
2641 * b0: t->sw[i ][j ][0 ]
2643 * b2: t->sw[i ][j+1][0 ]
2647 * b0: t->sw[i ][0 ][k ] .
2649 * b4: t->sw[i ][0 ][k+1] .
2653 * b0: t->sw[0 ][j ][k ]
2655 * b4: t->sw[0 ][j ][k+1]
2659 bool handle_case_0x30a(struct torus *t, int i, int j, int k)
2661 int im1 = canonicalize(i - 1, t->x_sz);
2662 int ip1 = canonicalize(i + 1, t->x_sz);
2663 int jp1 = canonicalize(j + 1, t->y_sz);
2665 if (safe_x_perpendicular(t, i, j, k) &&
2666 install_tswitch(t, ip1, j, k,
2667 tfind_2d_perpendicular(t->sw[i][jp1][k],
2669 t->sw[im1][j][k]))) {
2672 log_no_perp(t, 0x30a, i, j, k, i, j, k);
2674 if (safe_x_perpendicular(t, i, jp1, k) &&
2675 install_tswitch(t, ip1, jp1, k,
2676 tfind_2d_perpendicular(t->sw[i][j][k],
2678 t->sw[im1][jp1][k]))) {
2681 log_no_perp(t, 0x30a, i, j, k, i, jp1, k);
2686 bool handle_case_0x522(struct torus *t, int i, int j, int k)
2688 int im1 = canonicalize(i - 1, t->x_sz);
2689 int ip1 = canonicalize(i + 1, t->x_sz);
2690 int kp1 = canonicalize(k + 1, t->z_sz);
2692 if (safe_x_perpendicular(t, i, j, k) &&
2693 install_tswitch(t, ip1, j, k,
2694 tfind_2d_perpendicular(t->sw[i][j][kp1],
2696 t->sw[im1][j][k]))) {
2699 log_no_perp(t, 0x522, i, j, k, i, j, k);
2701 if (safe_x_perpendicular(t, i, j, kp1) &&
2702 install_tswitch(t, ip1, j, kp1,
2703 tfind_2d_perpendicular(t->sw[i][j][k],
2705 t->sw[im1][j][kp1]))) {
2708 log_no_perp(t, 0x522, i, j, k, i, j, kp1);
2713 bool handle_case_0x644(struct torus *t, int i, int j, int k)
2715 int jm1 = canonicalize(j - 1, t->y_sz);
2716 int jp1 = canonicalize(j + 1, t->y_sz);
2717 int kp1 = canonicalize(k + 1, t->z_sz);
2719 if (safe_y_perpendicular(t, i, j, k) &&
2720 install_tswitch(t, i, jp1, k,
2721 tfind_2d_perpendicular(t->sw[i][j][kp1],
2723 t->sw[i][jm1][k]))) {
2726 log_no_perp(t, 0x644, i, j, k, i, j, k);
2728 if (safe_y_perpendicular(t, i, j, kp1) &&
2729 install_tswitch(t, i, jp1, kp1,
2730 tfind_2d_perpendicular(t->sw[i][j][k],
2732 t->sw[i][jm1][kp1]))) {
2735 log_no_perp(t, 0x644, i, j, k, i, j, kp1);
2740 * Handle the 2D cases where two existing edges meet at a corner.
2747 * b1: t->sw[i+1][j ][0 ]
2748 * b2: t->sw[i ][j+1][0 ]
2749 * b3: t->sw[i+1][j+1][0 ]
2753 * b1: t->sw[i+1][0 ][k ] .
2754 * b4: t->sw[i ][0 ][k+1] .
2755 * b5: t->sw[i+1][0 ][k+1] .
2759 * b2: t->sw[0 ][j+1][k ]
2760 * b4: t->sw[0 ][j ][k+1]
2761 * b6: t->sw[0 ][j+1][k+1]
2764 bool handle_case_0x301(struct torus *t, int i, int j, int k)
2766 int ip1 = canonicalize(i + 1, t->x_sz);
2767 int jp1 = canonicalize(j + 1, t->y_sz);
2769 if (install_tswitch(t, i, j, k,
2770 tfind_face_corner(t->sw[ip1][j][k],
2772 t->sw[i][jp1][k]))) {
2775 log_no_crnr(t, 0x301, i, j, k, i, j, k);
2780 bool handle_case_0x501(struct torus *t, int i, int j, int k)
2782 int ip1 = canonicalize(i + 1, t->x_sz);
2783 int kp1 = canonicalize(k + 1, t->z_sz);
2785 if (install_tswitch(t, i, j, k,
2786 tfind_face_corner(t->sw[ip1][j][k],
2788 t->sw[i][j][kp1]))) {
2791 log_no_crnr(t, 0x501, i, j, k, i, j, k);
2796 bool handle_case_0x601(struct torus *t, int i, int j, int k)
2798 int jp1 = canonicalize(j + 1, t->y_sz);
2799 int kp1 = canonicalize(k + 1, t->z_sz);
2801 if (install_tswitch(t, i, j, k,
2802 tfind_face_corner(t->sw[i][jp1][k],
2804 t->sw[i][j][kp1]))) {
2807 log_no_crnr(t, 0x601, i, j, k, i, j, k);
2813 * b0: t->sw[i ][j ][0 ]
2815 * b2: t->sw[i ][j+1][0 ]
2816 * b3: t->sw[i+1][j+1][0 ]
2819 * b0: t->sw[i ][0 ][k ] .
2821 * b4: t->sw[i ][0 ][k+1] .
2822 * b5: t->sw[i+1][0 ][k+1] .
2825 * b0: t->sw[0 ][j ][k ]
2827 * b4: t->sw[0 ][j ][k+1]
2828 * b6: t->sw[0 ][j+1][k+1]
2831 bool handle_case_0x302(struct torus *t, int i, int j, int k)
2833 int ip1 = canonicalize(i + 1, t->x_sz);
2834 int jp1 = canonicalize(j + 1, t->y_sz);
2836 if (install_tswitch(t, ip1, j, k,
2837 tfind_face_corner(t->sw[i][j][k],
2839 t->sw[ip1][jp1][k]))) {
2842 log_no_crnr(t, 0x302, i, j, k, ip1, j, k);
2847 bool handle_case_0x502(struct torus *t, int i, int j, int k)
2849 int ip1 = canonicalize(i + 1, t->x_sz);
2850 int kp1 = canonicalize(k + 1, t->z_sz);
2852 if (install_tswitch(t, ip1, j, k,
2853 tfind_face_corner(t->sw[i][j][k],
2855 t->sw[ip1][j][kp1]))) {
2858 log_no_crnr(t, 0x502, i, j, k, ip1, j, k);
2863 bool handle_case_0x604(struct torus *t, int i, int j, int k)
2865 int jp1 = canonicalize(j + 1, t->y_sz);
2866 int kp1 = canonicalize(k + 1, t->z_sz);
2868 if (install_tswitch(t, i, jp1, k,
2869 tfind_face_corner(t->sw[i][j][k],
2871 t->sw[i][jp1][kp1]))) {
2874 log_no_crnr(t, 0x604, i, j, k, i, jp1, k);
2881 * b0: t->sw[i ][j ][0 ]
2882 * b1: t->sw[i+1][j ][0 ]
2883 * b2: t->sw[i ][j+1][0 ]
2887 * b0: t->sw[i ][0 ][k ] .
2888 * b1: t->sw[i+1][0 ][k ] .
2889 * b4: t->sw[i ][0 ][k+1] .
2893 * b0: t->sw[0 ][j ][k ]
2894 * b2: t->sw[0 ][j+1][k ]
2895 * b4: t->sw[0 ][j ][k+1]
2899 bool handle_case_0x308(struct torus *t, int i, int j, int k)
2901 int ip1 = canonicalize(i + 1, t->x_sz);
2902 int jp1 = canonicalize(j + 1, t->y_sz);
2904 if (install_tswitch(t, ip1, jp1, k,
2905 tfind_face_corner(t->sw[ip1][j][k],
2907 t->sw[i][jp1][k]))) {
2910 log_no_crnr(t, 0x308, i, j, k, ip1, jp1, k);
2915 bool handle_case_0x520(struct torus *t, int i, int j, int k)
2917 int ip1 = canonicalize(i + 1, t->x_sz);
2918 int kp1 = canonicalize(k + 1, t->z_sz);
2920 if (install_tswitch(t, ip1, j, kp1,
2921 tfind_face_corner(t->sw[ip1][j][k],
2923 t->sw[i][j][kp1]))) {
2926 log_no_crnr(t, 0x520, i, j, k, ip1, j, kp1);
2931 bool handle_case_0x640(struct torus *t, int i, int j, int k)
2933 int jp1 = canonicalize(j + 1, t->y_sz);
2934 int kp1 = canonicalize(k + 1, t->z_sz);
2936 if (install_tswitch(t, i, jp1, kp1,
2937 tfind_face_corner(t->sw[i][jp1][k],
2939 t->sw[i][j][kp1]))) {
2942 log_no_crnr(t, 0x640, i, j, k, i, jp1, kp1);
2948 * b0: t->sw[i ][j ][0 ]
2949 * b1: t->sw[i+1][j ][0 ]
2951 * b3: t->sw[i+1][j+1][0 ]
2954 * b0: t->sw[i ][0 ][k ] .
2955 * b1: t->sw[i+1][0 ][k ] .
2957 * b5: t->sw[i+1][0 ][k+1] .
2960 * b0: t->sw[0 ][j ][k ]
2961 * b2: t->sw[0 ][j+1][k ]
2963 * b6: t->sw[0 ][j+1][k+1]
2966 bool handle_case_0x304(struct torus *t, int i, int j, int k)
2968 int ip1 = canonicalize(i + 1, t->x_sz);
2969 int jp1 = canonicalize(j + 1, t->y_sz);
2971 if (install_tswitch(t, i, jp1, k,
2972 tfind_face_corner(t->sw[i][j][k],
2974 t->sw[ip1][jp1][k]))) {
2977 log_no_crnr(t, 0x304, i, j, k, i, jp1, k);
2982 bool handle_case_0x510(struct torus *t, int i, int j, int k)
2984 int ip1 = canonicalize(i + 1, t->x_sz);
2985 int kp1 = canonicalize(k + 1, t->z_sz);
2987 if (install_tswitch(t, i, j, kp1,
2988 tfind_face_corner(t->sw[i][j][k],
2990 t->sw[ip1][j][kp1]))) {
2993 log_no_crnr(t, 0x510, i, j, k, i, j, kp1);
2998 bool handle_case_0x610(struct torus *t, int i, int j, int k)
3000 int jp1 = canonicalize(j + 1, t->y_sz);
3001 int kp1 = canonicalize(k + 1, t->z_sz);
3003 if (install_tswitch(t, i, j, kp1,
3004 tfind_face_corner(t->sw[i][j][k],
3006 t->sw[i][jp1][kp1]))) {
3009 log_no_crnr(t, 0x610, i, j, k, i, j, kp1);
3014 * Handle the 3D cases where two existing edges meet at a corner.
3026 * b5: t->sw[i+1][j ][k+1]
3027 * b6: t->sw[i ][j+1][k+1]
3028 * b7: t->sw[i+1][j+1][k+1]
3038 bool handle_case_0x71f(struct torus *t, int i, int j, int k)
3040 int ip1 = canonicalize(i + 1, t->x_sz);
3041 int jp1 = canonicalize(j + 1, t->y_sz);
3042 int kp1 = canonicalize(k + 1, t->z_sz);
3043 int kp2 = canonicalize(k + 2, t->z_sz);
3045 if (safe_z_perpendicular(t, ip1, jp1, kp1) &&
3046 install_tswitch(t, ip1, jp1, k,
3047 tfind_3d_perpendicular(t->sw[ip1][j][kp1],
3048 t->sw[ip1][jp1][kp1],
3050 t->sw[ip1][jp1][kp2]))) {
3053 log_no_perp(t, 0x71f, i, j, k, ip1, jp1, kp1);
3064 * b4: t->sw[i ][j ][k+1] . O
3066 * b6: t->sw[i ][j+1][k+1] .
3067 * b7: t->sw[i+1][j+1][k+1] .
3077 bool handle_case_0x72f(struct torus *t, int i, int j, int k)
3079 int ip1 = canonicalize(i + 1, t->x_sz);
3080 int jp1 = canonicalize(j + 1, t->y_sz);
3081 int kp1 = canonicalize(k + 1, t->z_sz);
3082 int kp2 = canonicalize(k + 2, t->z_sz);
3084 if (safe_z_perpendicular(t, i, jp1, kp1) &&
3085 install_tswitch(t, i, jp1, k,
3086 tfind_3d_perpendicular(t->sw[i][j][kp1],
3088 t->sw[ip1][jp1][kp1],
3089 t->sw[i][jp1][kp2]))) {
3092 log_no_perp(t, 0x72f, i, j, k, i, jp1, kp1);
3102 * b3: t->sw[i+1][j+1][k ] O . O
3105 * b6: t->sw[i ][j+1][k+1]
3106 * b7: t->sw[i+1][j+1][k+1]
3116 bool handle_case_0x737(struct torus *t, int i, int j, int k)
3118 int ip1 = canonicalize(i + 1, t->x_sz);
3119 int jp1 = canonicalize(j + 1, t->y_sz);
3120 int jp2 = canonicalize(j + 2, t->y_sz);
3121 int kp1 = canonicalize(k + 1, t->z_sz);
3123 if (safe_y_perpendicular(t, ip1, jp1, kp1) &&
3124 install_tswitch(t, ip1, j, kp1,
3125 tfind_3d_perpendicular(t->sw[i][jp1][kp1],
3126 t->sw[ip1][jp1][kp1],
3128 t->sw[ip1][jp2][kp1]))) {
3131 log_no_perp(t, 0x737, i, j, k, ip1, jp1, kp1);
3140 * b2: t->sw[i ][j+1][k ] .
3144 * b6: t->sw[i ][j+1][k+1] .
3145 * b7: t->sw[i+1][j+1][k+1] .
3155 bool handle_case_0x73b(struct torus *t, int i, int j, int k)
3157 int ip1 = canonicalize(i + 1, t->x_sz);
3158 int jp1 = canonicalize(j + 1, t->y_sz);
3159 int jp2 = canonicalize(j + 2, t->y_sz);
3160 int kp1 = canonicalize(k + 1, t->z_sz);
3162 if (safe_y_perpendicular(t, i, jp1, kp1) &&
3163 install_tswitch(t, i, j, kp1,
3164 tfind_3d_perpendicular(t->sw[i][jp1][k],
3166 t->sw[ip1][jp1][kp1],
3167 t->sw[i][jp2][kp1]))) {
3170 log_no_perp(t, 0x73b, i, j, k, i, jp1, kp1);
3181 * b4: t->sw[i ][j ][k+1] O .
3182 * b5: t->sw[i+1][j ][k+1] .
3184 * b7: t->sw[i+1][j+1][k+1] .
3194 bool handle_case_0x74f(struct torus *t, int i, int j, int k)
3196 int ip1 = canonicalize(i + 1, t->x_sz);
3197 int jp1 = canonicalize(j + 1, t->y_sz);
3198 int kp1 = canonicalize(k + 1, t->z_sz);
3199 int kp2 = canonicalize(k + 2, t->z_sz);
3201 if (safe_z_perpendicular(t, ip1, j, kp1) &&
3202 install_tswitch(t, ip1, j, k,
3203 tfind_3d_perpendicular(t->sw[i][j][kp1],
3205 t->sw[ip1][jp1][kp1],
3206 t->sw[ip1][j][kp2]))) {
3209 log_no_perp(t, 0x74f, i, j, k, ip1, j, kp1);
3219 * b3: t->sw[i+1][j+1][k ] O . O
3221 * b5: t->sw[i+1][j ][k+1]
3223 * b7: t->sw[i+1][j+1][k+1]
3233 bool handle_case_0x757(struct torus *t, int i, int j, int k)
3235 int ip1 = canonicalize(i + 1, t->x_sz);
3236 int ip2 = canonicalize(i + 2, t->x_sz);
3237 int jp1 = canonicalize(j + 1, t->y_sz);
3238 int kp1 = canonicalize(k + 1, t->z_sz);
3240 if (safe_x_perpendicular(t, ip1, jp1, kp1) &&
3241 install_tswitch(t, i, jp1, kp1,
3242 tfind_3d_perpendicular(t->sw[ip1][j][kp1],
3243 t->sw[ip1][jp1][kp1],
3245 t->sw[ip2][jp1][kp1]))) {
3248 log_no_perp(t, 0x757, i, j, k, ip1, jp1, kp1);
3256 * b1: t->sw[i+1][j ][k ] .
3260 * b5: t->sw[i+1][j ][k+1] .
3262 * b7: t->sw[i+1][j+1][k+1] .
3272 bool handle_case_0x75d(struct torus *t, int i, int j, int k)
3274 int ip1 = canonicalize(i + 1, t->x_sz);
3275 int ip2 = canonicalize(i + 2, t->x_sz);
3276 int jp1 = canonicalize(j + 1, t->y_sz);
3277 int kp1 = canonicalize(k + 1, t->z_sz);
3279 if (safe_x_perpendicular(t, ip1, j, kp1) &&
3280 install_tswitch(t, i, j, kp1,
3281 tfind_3d_perpendicular(t->sw[ip1][j][k],
3283 t->sw[ip1][jp1][kp1],
3284 t->sw[ip2][j][kp1]))) {
3287 log_no_perp(t, 0x75d, i, j, k, ip1, j, kp1);
3296 * b2: t->sw[i ][j+1][k ] .
3297 * b3: t->sw[i+1][j+1][k ] O . O
3301 * b7: t->sw[i+1][j+1][k+1] .
3311 bool handle_case_0x773(struct torus *t, int i, int j, int k)
3313 int ip1 = canonicalize(i + 1, t->x_sz);
3314 int jp1 = canonicalize(j + 1, t->y_sz);
3315 int jp2 = canonicalize(j + 2, t->y_sz);
3316 int kp1 = canonicalize(k + 1, t->z_sz);
3318 if (safe_y_perpendicular(t, ip1, jp1, k) &&
3319 install_tswitch(t, ip1, j, k,
3320 tfind_3d_perpendicular(t->sw[i][jp1][k],
3322 t->sw[ip1][jp1][kp1],
3323 t->sw[ip1][jp2][k]))) {
3326 log_no_perp(t, 0x773, i, j, k, ip1, jp1, k);
3334 * b1: t->sw[i+1][j ][k ] .
3336 * b3: t->sw[i+1][j+1][k ] O . O
3340 * b7: t->sw[i+1][j+1][k+1] .
3350 bool handle_case_0x775(struct torus *t, int i, int j, int k)
3352 int ip1 = canonicalize(i + 1, t->x_sz);
3353 int ip2 = canonicalize(i + 2, t->x_sz);
3354 int jp1 = canonicalize(j + 1, t->y_sz);
3355 int kp1 = canonicalize(k + 1, t->z_sz);
3357 if (safe_x_perpendicular(t, ip1, jp1, k) &&
3358 install_tswitch(t, i, jp1, k,
3359 tfind_3d_perpendicular(t->sw[ip1][j][k],
3361 t->sw[ip1][jp1][kp1],
3362 t->sw[ip2][jp1][k]))) {
3365 log_no_perp(t, 0x775, i, j, k, ip1, jp1, k);
3376 * b4: t->sw[i ][j ][k+1] . O .
3377 * b5: t->sw[i+1][j ][k+1] . .
3378 * b6: t->sw[i ][j+1][k+1] . .
3389 bool handle_case_0x78f(struct torus *t, int i, int j, int k)
3391 int ip1 = canonicalize(i + 1, t->x_sz);
3392 int jp1 = canonicalize(j + 1, t->y_sz);
3393 int kp1 = canonicalize(k + 1, t->z_sz);
3394 int kp2 = canonicalize(k + 2, t->z_sz);
3396 if (safe_z_perpendicular(t, i, j, kp1) &&
3397 install_tswitch(t, i, j, k,
3398 tfind_3d_perpendicular(t->sw[ip1][j][kp1],
3401 t->sw[i][j][kp2]))) {
3404 log_no_perp(t, 0x78f, i, j, k, i, j, kp1);
3413 * b2: t->sw[i ][j+1][k ]
3415 * b4: t->sw[i ][j ][k+1] . . O
3417 * b6: t->sw[i ][j+1][k+1] . .
3428 bool handle_case_0x7ab(struct torus *t, int i, int j, int k)
3430 int im1 = canonicalize(i - 1, t->x_sz);
3431 int ip1 = canonicalize(i + 1, t->x_sz);
3432 int jp1 = canonicalize(j + 1, t->y_sz);
3433 int kp1 = canonicalize(k + 1, t->z_sz);
3435 if (safe_x_perpendicular(t, i, jp1, kp1) &&
3436 install_tswitch(t, ip1, jp1, kp1,
3437 tfind_3d_perpendicular(t->sw[i][j][kp1],
3440 t->sw[im1][jp1][kp1]))) {
3443 log_no_perp(t, 0x7ab, i, j, k, i, jp1, kp1);
3450 * b0: t->sw[i ][j ][k ]
3454 * b4: t->sw[i ][j ][k+1] . O
3456 * b6: t->sw[i ][j+1][k+1] .
3467 bool handle_case_0x7ae(struct torus *t, int i, int j, int k)
3469 int im1 = canonicalize(i - 1, t->x_sz);
3470 int ip1 = canonicalize(i + 1, t->x_sz);
3471 int jp1 = canonicalize(j + 1, t->y_sz);
3472 int kp1 = canonicalize(k + 1, t->z_sz);
3474 if (safe_x_perpendicular(t, i, j, kp1) &&
3475 install_tswitch(t, ip1, j, kp1,
3476 tfind_3d_perpendicular(t->sw[i][j][k],
3479 t->sw[im1][j][kp1]))) {
3482 log_no_perp(t, 0x7ae, i, j, k, i, j, kp1);
3491 * b2: t->sw[i ][j+1][k ]
3492 * b3: t->sw[i+1][j+1][k ] O O
3495 * b6: t->sw[i ][j+1][k+1] . .
3506 bool handle_case_0x7b3(struct torus *t, int i, int j, int k)
3508 int ip1 = canonicalize(i + 1, t->x_sz);
3509 int jp1 = canonicalize(j + 1, t->y_sz);
3510 int jp2 = canonicalize(j + 2, t->y_sz);
3511 int kp1 = canonicalize(k + 1, t->z_sz);
3513 if (safe_y_perpendicular(t, i, jp1, k) &&
3514 install_tswitch(t, i, j, k,
3515 tfind_3d_perpendicular(t->sw[i][jp1][kp1],
3518 t->sw[i][jp2][k]))) {
3521 log_no_perp(t, 0x7b3, i, j, k, i, jp1, k);
3528 * b0: t->sw[i ][j ][k ]
3530 * b2: t->sw[i ][j+1][k ]
3534 * b6: t->sw[i ][j+1][k+1] .
3545 bool handle_case_0x7ba(struct torus *t, int i, int j, int k)
3547 int im1 = canonicalize(i - 1, t->x_sz);
3548 int ip1 = canonicalize(i + 1, t->x_sz);
3549 int jp1 = canonicalize(j + 1, t->y_sz);
3550 int kp1 = canonicalize(k + 1, t->z_sz);
3552 if (safe_x_perpendicular(t, i, jp1, k) &&
3553 install_tswitch(t, ip1, jp1, k,
3554 tfind_3d_perpendicular(t->sw[i][j][k],
3557 t->sw[im1][jp1][k]))) {
3560 log_no_perp(t, 0x7ba, i, j, k, i, jp1, k);
3568 * b1: t->sw[i+1][j ][k ]
3571 * b4: t->sw[i ][j ][k+1] O . .
3572 * b5: t->sw[i+1][j ][k+1] . .
3584 bool handle_case_0x7cd(struct torus *t, int i, int j, int k)
3586 int ip1 = canonicalize(i + 1, t->x_sz);
3587 int jp1 = canonicalize(j + 1, t->y_sz);
3588 int jm1 = canonicalize(j - 1, t->y_sz);
3589 int kp1 = canonicalize(k + 1, t->z_sz);
3591 if (safe_y_perpendicular(t, ip1, j, kp1) &&
3592 install_tswitch(t, ip1, jp1, kp1,
3593 tfind_3d_perpendicular(t->sw[i][j][kp1],
3596 t->sw[ip1][jm1][kp1]))) {
3599 log_no_perp(t, 0x7cd, i, j, k, ip1, j, kp1);
3606 * b0: t->sw[i ][j ][k ]
3610 * b4: t->sw[i ][j ][k+1] O .
3611 * b5: t->sw[i+1][j ][k+1] .
3623 bool handle_case_0x7ce(struct torus *t, int i, int j, int k)
3625 int ip1 = canonicalize(i + 1, t->x_sz);
3626 int jp1 = canonicalize(j + 1, t->y_sz);
3627 int jm1 = canonicalize(j - 1, t->y_sz);
3628 int kp1 = canonicalize(k + 1, t->z_sz);
3630 if (safe_y_perpendicular(t, i, j, kp1) &&
3631 install_tswitch(t, i, jp1, kp1,
3632 tfind_3d_perpendicular(t->sw[i][j][k],
3635 t->sw[i][jm1][kp1]))) {
3638 log_no_perp(t, 0x7ce, i, j, k, i, j, kp1);
3646 * b1: t->sw[i+1][j ][k ]
3648 * b3: t->sw[i+1][j+1][k ] O O
3650 * b5: t->sw[i+1][j ][k+1] . .
3662 bool handle_case_0x7d5(struct torus *t, int i, int j, int k)
3664 int ip1 = canonicalize(i + 1, t->x_sz);
3665 int ip2 = canonicalize(i + 2, t->x_sz);
3666 int jp1 = canonicalize(j + 1, t->y_sz);
3667 int kp1 = canonicalize(k + 1, t->z_sz);
3669 if (safe_x_perpendicular(t, ip1, j, k) &&
3670 install_tswitch(t, i, j, k,
3671 tfind_3d_perpendicular(t->sw[ip1][j][kp1],
3674 t->sw[ip2][j][k]))) {
3677 log_no_perp(t, 0x7d5, i, j, k, ip1, j, k);
3684 * b0: t->sw[i ][j ][k ]
3685 * b1: t->sw[i+1][j ][k ]
3689 * b5: t->sw[i+1][j ][k+1] .
3701 bool handle_case_0x7dc(struct torus *t, int i, int j, int k)
3703 int ip1 = canonicalize(i + 1, t->x_sz);
3704 int jp1 = canonicalize(j + 1, t->y_sz);
3705 int jm1 = canonicalize(j - 1, t->y_sz);
3706 int kp1 = canonicalize(k + 1, t->z_sz);
3708 if (safe_y_perpendicular(t, ip1, j, k) &&
3709 install_tswitch(t, ip1, jp1, k,
3710 tfind_3d_perpendicular(t->sw[i][j][k],
3713 t->sw[ip1][jm1][k]))) {
3716 log_no_perp(t, 0x7dc, i, j, k, ip1, j, k);
3723 * b0: t->sw[i ][j ][k ]
3725 * b2: t->sw[i ][j+1][k ]
3727 * b4: t->sw[i ][j ][k+1] O
3740 bool handle_case_0x7ea(struct torus *t, int i, int j, int k)
3742 int im1 = canonicalize(i - 1, t->x_sz);
3743 int ip1 = canonicalize(i + 1, t->x_sz);
3744 int jp1 = canonicalize(j + 1, t->y_sz);
3745 int kp1 = canonicalize(k + 1, t->z_sz);
3747 if (safe_x_perpendicular(t, i, j, k) &&
3748 install_tswitch(t, ip1, j, k,
3749 tfind_3d_perpendicular(t->sw[i][j][kp1],
3752 t->sw[im1][j][k]))) {
3755 log_no_perp(t, 0x7ea, i, j, k, i, j, k);
3762 * b0: t->sw[i ][j ][k ]
3763 * b1: t->sw[i+1][j ][k ]
3766 * b4: t->sw[i ][j ][k+1] O
3779 bool handle_case_0x7ec(struct torus *t, int i, int j, int k)
3781 int ip1 = canonicalize(i + 1, t->x_sz);
3782 int jp1 = canonicalize(j + 1, t->y_sz);
3783 int jm1 = canonicalize(j - 1, t->y_sz);
3784 int kp1 = canonicalize(k + 1, t->z_sz);
3786 if (safe_y_perpendicular(t, i, j, k) &&
3787 install_tswitch(t, i, jp1, k,
3788 tfind_3d_perpendicular(t->sw[i][j][kp1],
3791 t->sw[i][jm1][k]))) {
3794 log_no_perp(t, 0x7ec, i, j, k, i, j, k);
3802 * b1: t->sw[i+1][j ][k ]
3803 * b2: t->sw[i ][j+1][k ]
3804 * b3: t->sw[i+1][j+1][k ] O O
3818 bool handle_case_0x7f1(struct torus *t, int i, int j, int k)
3820 int ip1 = canonicalize(i + 1, t->x_sz);
3821 int jp1 = canonicalize(j + 1, t->y_sz);
3822 int km1 = canonicalize(k - 1, t->z_sz);
3823 int kp1 = canonicalize(k + 1, t->z_sz);
3825 if (safe_z_perpendicular(t, ip1, jp1, k) &&
3826 install_tswitch(t, ip1, jp1, kp1,
3827 tfind_3d_perpendicular(t->sw[ip1][j][k],
3830 t->sw[ip1][jp1][km1]))) {
3833 log_no_perp(t, 0x7f1, i, j, k, ip1, jp1, k);
3840 * b0: t->sw[i ][j ][k ]
3842 * b2: t->sw[i ][j+1][k ]
3843 * b3: t->sw[i+1][j+1][k ] O O
3857 bool handle_case_0x7f2(struct torus *t, int i, int j, int k)
3859 int ip1 = canonicalize(i + 1, t->x_sz);
3860 int jp1 = canonicalize(j + 1, t->y_sz);
3861 int km1 = canonicalize(k - 1, t->z_sz);
3862 int kp1 = canonicalize(k + 1, t->z_sz);
3864 if (safe_z_perpendicular(t, i, jp1, k) &&
3865 install_tswitch(t, i, jp1, kp1,
3866 tfind_3d_perpendicular(t->sw[i][j][k],
3869 t->sw[i][jp1][km1]))) {
3872 log_no_perp(t, 0x7f2, i, j, k, i, jp1, k);
3879 * b0: t->sw[i ][j ][k ]
3880 * b1: t->sw[i+1][j ][k ]
3882 * b3: t->sw[i+1][j+1][k ] O O
3896 bool handle_case_0x7f4(struct torus *t, int i, int j, int k)
3898 int ip1 = canonicalize(i + 1, t->x_sz);
3899 int jp1 = canonicalize(j + 1, t->y_sz);
3900 int km1 = canonicalize(k - 1, t->z_sz);
3901 int kp1 = canonicalize(k + 1, t->z_sz);
3903 if (safe_z_perpendicular(t, ip1, j, k) &&
3904 install_tswitch(t, ip1, j, kp1,
3905 tfind_3d_perpendicular(t->sw[i][j][k],
3908 t->sw[ip1][j][km1]))) {
3911 log_no_perp(t, 0x7f4, i, j, k, ip1, j, k);
3918 * b0: t->sw[i ][j ][k ]
3919 * b1: t->sw[i+1][j ][k ]
3920 * b2: t->sw[i ][j+1][k ]
3935 bool handle_case_0x7f8(struct torus *t, int i, int j, int k)
3937 int ip1 = canonicalize(i + 1, t->x_sz);
3938 int jp1 = canonicalize(j + 1, t->y_sz);
3939 int km1 = canonicalize(k - 1, t->z_sz);
3940 int kp1 = canonicalize(k + 1, t->z_sz);
3942 if (safe_z_perpendicular(t, i, j, k) &&
3943 install_tswitch(t, i, j, kp1,
3944 tfind_3d_perpendicular(t->sw[ip1][j][k],
3947 t->sw[i][j][km1]))) {
3950 log_no_perp(t, 0x7f8, i, j, k, i, j, k);
3955 * Handle the cases where three existing edges meet at a corner.
3964 * b3: t->sw[i+1][j+1][k ] O . O
3966 * b5: t->sw[i+1][j ][k+1]
3967 * b6: t->sw[i ][j+1][k+1]
3968 * b7: t->sw[i+1][j+1][k+1]
3978 bool handle_case_0x717(struct torus *t, int i, int j, int k)
3980 int ip1 = canonicalize(i + 1, t->x_sz);
3981 int jp1 = canonicalize(j + 1, t->y_sz);
3982 int kp1 = canonicalize(k + 1, t->z_sz);
3984 if (install_tswitch(t, i, j, kp1,
3985 tfind_face_corner(t->sw[i][jp1][kp1],
3986 t->sw[ip1][jp1][kp1],
3987 t->sw[ip1][j][kp1]))) {
3990 log_no_crnr(t, 0x717, i, j, k, i, j, kp1);
3992 if (install_tswitch(t, ip1, j, k,
3993 tfind_face_corner(t->sw[ip1][jp1][k],
3994 t->sw[ip1][jp1][kp1],
3995 t->sw[ip1][j][kp1]))) {
3998 log_no_crnr(t, 0x717, i, j, k, ip1, j, k);
4000 if (install_tswitch(t, i, jp1, k,
4001 tfind_face_corner(t->sw[ip1][jp1][k],
4002 t->sw[ip1][jp1][kp1],
4003 t->sw[i][jp1][kp1]))) {
4006 log_no_crnr(t, 0x717, i, j, k, i, jp1, k);
4015 * b2: t->sw[i ][j+1][k ] .
4017 * b4: t->sw[i ][j ][k+1] . . O
4019 * b6: t->sw[i ][j+1][k+1] . .
4020 * b7: t->sw[i+1][j+1][k+1] . .
4030 bool handle_case_0x72b(struct torus *t, int i, int j, int k)
4032 int ip1 = canonicalize(i + 1, t->x_sz);
4033 int jp1 = canonicalize(j + 1, t->y_sz);
4034 int kp1 = canonicalize(k + 1, t->z_sz);
4036 if (install_tswitch(t, ip1, j, kp1,
4037 tfind_face_corner(t->sw[i][j][kp1],
4039 t->sw[ip1][jp1][kp1]))) {
4042 log_no_crnr(t, 0x72b, i, j, k, ip1, j, kp1);
4044 if (install_tswitch(t, i, j, k,
4045 tfind_face_corner(t->sw[i][jp1][k],
4047 t->sw[i][j][kp1]))) {
4050 log_no_crnr(t, 0x72b, i, j, k, i, j, k);
4052 if (install_tswitch(t, ip1, jp1, k,
4053 tfind_face_corner(t->sw[i][jp1][k],
4055 t->sw[ip1][jp1][kp1]))) {
4058 log_no_crnr(t, 0x72b, i, j, k, ip1, jp1, k);
4066 * b1: t->sw[i+1][j ][k ] .
4069 * b4: t->sw[i ][j ][k+1] O . .
4070 * b5: t->sw[i+1][j ][k+1] . .
4072 * b7: t->sw[i+1][j+1][k+1] . .
4082 bool handle_case_0x74d(struct torus *t, int i, int j, int k)
4084 int ip1 = canonicalize(i + 1, t->x_sz);
4085 int jp1 = canonicalize(j + 1, t->y_sz);
4086 int kp1 = canonicalize(k + 1, t->z_sz);
4088 if (install_tswitch(t, i, jp1, kp1,
4089 tfind_face_corner(t->sw[i][j][kp1],
4091 t->sw[ip1][jp1][kp1]))) {
4094 log_no_crnr(t, 0x74d, i, j, k, i, jp1, kp1);
4096 if (install_tswitch(t, i, j, k,
4097 tfind_face_corner(t->sw[ip1][j][k],
4099 t->sw[i][j][kp1]))) {
4102 log_no_crnr(t, 0x74d, i, j, k, i, j, k);
4104 if (install_tswitch(t, ip1, jp1, k,
4105 tfind_face_corner(t->sw[ip1][j][k],
4107 t->sw[ip1][jp1][kp1]))) {
4110 log_no_crnr(t, 0x74d, i, j, k, ip1, jp1, k);
4118 * b1: t->sw[i+1][j ][k ] .
4119 * b2: t->sw[i ][j+1][k ] .
4120 * b3: t->sw[i+1][j+1][k ] O . O
4124 * b7: t->sw[i+1][j+1][k+1] . .
4134 bool handle_case_0x771(struct torus *t, int i, int j, int k)
4136 int ip1 = canonicalize(i + 1, t->x_sz);
4137 int jp1 = canonicalize(j + 1, t->y_sz);
4138 int kp1 = canonicalize(k + 1, t->z_sz);
4140 if (install_tswitch(t, i, j, k,
4141 tfind_face_corner(t->sw[i][jp1][k],
4143 t->sw[ip1][j][k]))) {
4146 log_no_crnr(t, 0x771, i, j, k, i, j, k);
4148 if (install_tswitch(t, ip1, j, kp1,
4149 tfind_face_corner(t->sw[ip1][jp1][kp1],
4151 t->sw[ip1][j][k]))) {
4154 log_no_crnr(t, 0x771, i, j, k, ip1, j, kp1);
4156 if (install_tswitch(t, i, jp1, kp1,
4157 tfind_face_corner(t->sw[ip1][jp1][kp1],
4159 t->sw[i][jp1][k]))) {
4162 log_no_crnr(t, 0x771, i, j, k, i, jp1, kp1);
4169 * b0: t->sw[i ][j ][k ]
4173 * b4: t->sw[i ][j ][k+1] . O .
4174 * b5: t->sw[i+1][j ][k+1] . .
4175 * b6: t->sw[i ][j+1][k+1] . .
4186 bool handle_case_0x78e(struct torus *t, int i, int j, int k)
4188 int ip1 = canonicalize(i + 1, t->x_sz);
4189 int jp1 = canonicalize(j + 1, t->y_sz);
4190 int kp1 = canonicalize(k + 1, t->z_sz);
4192 if (install_tswitch(t, ip1, jp1, kp1,
4193 tfind_face_corner(t->sw[ip1][j][kp1],
4195 t->sw[i][jp1][kp1]))) {
4198 log_no_crnr(t, 0x78e, i, j, k, ip1, jp1, kp1);
4200 if (install_tswitch(t, ip1, j, k,
4201 tfind_face_corner(t->sw[i][j][k],
4203 t->sw[ip1][j][kp1]))) {
4206 log_no_crnr(t, 0x78e, i, j, k, ip1, j, k);
4208 if (install_tswitch(t, i, jp1, k,
4209 tfind_face_corner(t->sw[i][j][k],
4211 t->sw[i][jp1][kp1]))) {
4214 log_no_crnr(t, 0x78e, i, j, k, i, jp1, k);
4221 * b0: t->sw[i ][j ][k ]
4223 * b2: t->sw[i ][j+1][k ]
4224 * b3: t->sw[i+1][j+1][k ] O O
4227 * b6: t->sw[i ][j+1][k+1] . .
4238 bool handle_case_0x7b2(struct torus *t, int i, int j, int k)
4240 int ip1 = canonicalize(i + 1, t->x_sz);
4241 int jp1 = canonicalize(j + 1, t->y_sz);
4242 int kp1 = canonicalize(k + 1, t->z_sz);
4244 if (install_tswitch(t, ip1, j, k,
4245 tfind_face_corner(t->sw[i][j][k],
4247 t->sw[ip1][jp1][k]))) {
4250 log_no_crnr(t, 0x7b2, i, j, k, ip1, j, k);
4252 if (install_tswitch(t, ip1, jp1, kp1,
4253 tfind_face_corner(t->sw[i][jp1][kp1],
4255 t->sw[ip1][jp1][k]))) {
4258 log_no_crnr(t, 0x7b2, i, j, k, ip1, jp1, kp1);
4260 if (install_tswitch(t, i, j, kp1,
4261 tfind_face_corner(t->sw[i][jp1][kp1],
4266 log_no_crnr(t, 0x7b2, i, j, k, i, j, kp1);
4273 * b0: t->sw[i ][j ][k ]
4274 * b1: t->sw[i+1][j ][k ]
4276 * b3: t->sw[i+1][j+1][k ] O O
4278 * b5: t->sw[i+1][j ][k+1] . .
4290 bool handle_case_0x7d4(struct torus *t, int i, int j, int k)
4292 int ip1 = canonicalize(i + 1, t->x_sz);
4293 int jp1 = canonicalize(j + 1, t->y_sz);
4294 int kp1 = canonicalize(k + 1, t->z_sz);
4296 if (install_tswitch(t, i, jp1, k,
4297 tfind_face_corner(t->sw[i][j][k],
4299 t->sw[ip1][jp1][k]))) {
4302 log_no_crnr(t, 0x7d4, i, j, k, i, jp1, k);
4304 if (install_tswitch(t, i, j, kp1,
4305 tfind_face_corner(t->sw[ip1][j][kp1],
4310 log_no_crnr(t, 0x7d4, i, j, k, i, j, kp1);
4312 if (install_tswitch(t, ip1, jp1, kp1,
4313 tfind_face_corner(t->sw[ip1][j][kp1],
4315 t->sw[ip1][jp1][k]))) {
4318 log_no_crnr(t, 0x7d4, i, j, k, ip1, jp1, kp1);
4325 * b0: t->sw[i ][j ][k ]
4326 * b1: t->sw[i+1][j ][k ]
4327 * b2: t->sw[i ][j+1][k ]
4329 * b4: t->sw[i ][j ][k+1] O
4342 bool handle_case_0x7e8(struct torus *t, int i, int j, int k)
4344 int ip1 = canonicalize(i + 1, t->x_sz);
4345 int jp1 = canonicalize(j + 1, t->y_sz);
4346 int kp1 = canonicalize(k + 1, t->z_sz);
4348 if (install_tswitch(t, ip1, jp1, k,
4349 tfind_face_corner(t->sw[ip1][j][k],
4351 t->sw[i][jp1][k]))) {
4354 log_no_crnr(t, 0x7e8, i, j, k, ip1, jp1, k);
4356 if (install_tswitch(t, ip1, j, kp1,
4357 tfind_face_corner(t->sw[ip1][j][k],
4359 t->sw[i][j][kp1]))) {
4362 log_no_crnr(t, 0x7e8, i, j, k, ip1, j, kp1);
4364 if (install_tswitch(t, i, jp1, kp1,
4365 tfind_face_corner(t->sw[i][jp1][k],
4367 t->sw[i][j][kp1]))) {
4370 log_no_crnr(t, 0x7e8, i, j, k, i, jp1, kp1);
4375 * Handle the cases where four corners on a single face are missing.
4385 * b4: t->sw[i ][j ][k+1] . O .
4386 * b5: t->sw[i+1][j ][k+1] . .
4387 * b6: t->sw[i ][j+1][k+1] . .
4388 * b7: t->sw[i+1][j+1][k+1] . .
4398 bool handle_case_0x70f(struct torus *t, int i, int j, int k)
4400 if (handle_case_0x71f(t, i, j, k))
4403 if (handle_case_0x72f(t, i, j, k))
4406 if (handle_case_0x74f(t, i, j, k))
4409 return handle_case_0x78f(t, i, j, k);
4417 * b2: t->sw[i ][j+1][k ] . .
4418 * b3: t->sw[i+1][j+1][k ] O . O
4421 * b6: t->sw[i ][j+1][k+1] . .
4422 * b7: t->sw[i+1][j+1][k+1] . .
4432 bool handle_case_0x733(struct torus *t, int i, int j, int k)
4434 if (handle_case_0x737(t, i, j, k))
4437 if (handle_case_0x73b(t, i, j, k))
4440 if (handle_case_0x773(t, i, j, k))
4443 return handle_case_0x7b3(t, i, j, k);
4450 * b1: t->sw[i+1][j ][k ] . .
4452 * b3: t->sw[i+1][j+1][k ] O . O
4454 * b5: t->sw[i+1][j ][k+1] . .
4456 * b7: t->sw[i+1][j+1][k+1] . .
4466 bool handle_case_0x755(struct torus *t, int i, int j, int k)
4468 if (handle_case_0x757(t, i, j, k))
4471 if (handle_case_0x75d(t, i, j, k))
4474 if (handle_case_0x775(t, i, j, k))
4477 return handle_case_0x7d5(t, i, j, k);
4483 * b0: t->sw[i ][j ][k ]
4485 * b2: t->sw[i ][j+1][k ]
4487 * b4: t->sw[i ][j ][k+1] . . O
4489 * b6: t->sw[i ][j+1][k+1] . .
4500 bool handle_case_0x7aa(struct torus *t, int i, int j, int k)
4502 if (handle_case_0x7ab(t, i, j, k))
4505 if (handle_case_0x7ae(t, i, j, k))
4508 if (handle_case_0x7ba(t, i, j, k))
4511 return handle_case_0x7ea(t, i, j, k);
4517 * b0: t->sw[i ][j ][k ]
4518 * b1: t->sw[i+1][j ][k ]
4521 * b4: t->sw[i ][j ][k+1] O . .
4522 * b5: t->sw[i+1][j ][k+1] . .
4534 bool handle_case_0x7cc(struct torus *t, int i, int j, int k)
4536 if (handle_case_0x7cd(t, i, j, k))
4539 if (handle_case_0x7ce(t, i, j, k))
4542 if (handle_case_0x7dc(t, i, j, k))
4545 return handle_case_0x7ec(t, i, j, k);
4551 * b0: t->sw[i ][j ][k ]
4552 * b1: t->sw[i+1][j ][k ]
4553 * b2: t->sw[i ][j+1][k ]
4554 * b3: t->sw[i+1][j+1][k ] O O
4568 bool handle_case_0x7f0(struct torus *t, int i, int j, int k)
4570 if (handle_case_0x7f1(t, i, j, k))
4573 if (handle_case_0x7f2(t, i, j, k))
4576 if (handle_case_0x7f4(t, i, j, k))
4579 return handle_case_0x7f8(t, i, j, k);
4583 * Handle the cases where three corners on a single face are missing.
4593 * b3: t->sw[i+1][j+1][k ] O . O
4594 * b4: t->sw[i ][j ][k+1] . O .
4595 * b5: t->sw[i+1][j ][k+1] . .
4596 * b6: t->sw[i ][j+1][k+1] . .
4597 * b7: t->sw[i+1][j+1][k+1] . .
4607 bool handle_case_0x707(struct torus *t, int i, int j, int k)
4609 int ip1 = canonicalize(i + 1, t->x_sz);
4610 int jp1 = canonicalize(j + 1, t->y_sz);
4611 int kp1 = canonicalize(k + 1, t->z_sz);
4613 if (install_tswitch(t, ip1, j, k,
4614 tfind_face_corner(t->sw[ip1][jp1][k],
4615 t->sw[ip1][jp1][kp1],
4616 t->sw[ip1][j][kp1]))) {
4619 log_no_crnr(t, 0x707, i, j, k, ip1, j, k);
4621 if (install_tswitch(t, i, jp1, k,
4622 tfind_face_corner(t->sw[ip1][jp1][k],
4623 t->sw[ip1][jp1][kp1],
4624 t->sw[i][jp1][kp1]))) {
4627 log_no_crnr(t, 0x707, i, j, k, i, jp1, k);
4636 * b2: t->sw[i ][j+1][k ] . .
4638 * b4: t->sw[i ][j ][k+1] . . O .
4639 * b5: t->sw[i+1][j ][k+1] . . .
4640 * b6: t->sw[i ][j+1][k+1] . . .
4641 * b7: t->sw[i+1][j+1][k+1] . . .
4651 bool handle_case_0x70b(struct torus *t, int i, int j, int k)
4653 int ip1 = canonicalize(i + 1, t->x_sz);
4654 int jp1 = canonicalize(j + 1, t->y_sz);
4655 int kp1 = canonicalize(k + 1, t->z_sz);
4657 if (install_tswitch(t, i, j, k,
4658 tfind_face_corner(t->sw[i][jp1][k],
4660 t->sw[i][j][kp1]))) {
4663 log_no_crnr(t, 0x70b, i, j, k, i, j, k);
4665 if (install_tswitch(t, ip1, jp1, k,
4666 tfind_face_corner(t->sw[i][jp1][k],
4668 t->sw[ip1][jp1][kp1]))) {
4671 log_no_crnr(t, 0x70b, i, j, k, ip1, jp1, k);
4679 * b1: t->sw[i+1][j ][k ] . .
4682 * b4: t->sw[i ][j ][k+1] . O . .
4683 * b5: t->sw[i+1][j ][k+1] . . .
4684 * b6: t->sw[i ][j+1][k+1] . . .
4685 * b7: t->sw[i+1][j+1][k+1] . . .
4695 bool handle_case_0x70d(struct torus *t, int i, int j, int k)
4697 int ip1 = canonicalize(i + 1, t->x_sz);
4698 int jp1 = canonicalize(j + 1, t->y_sz);
4699 int kp1 = canonicalize(k + 1, t->z_sz);
4701 if (install_tswitch(t, i, j, k,
4702 tfind_face_corner(t->sw[ip1][j][k],
4704 t->sw[i][j][kp1]))) {
4707 log_no_crnr(t, 0x70d, i, j, k, i, j, k);
4709 if (install_tswitch(t, ip1, jp1, k,
4710 tfind_face_corner(t->sw[ip1][j][k],
4712 t->sw[ip1][jp1][kp1]))) {
4715 log_no_crnr(t, 0x70d, i, j, k, ip1, jp1, k);
4722 * b0: t->sw[i ][j ][k ] . .
4726 * b4: t->sw[i ][j ][k+1] . O .
4727 * b5: t->sw[i+1][j ][k+1] . .
4728 * b6: t->sw[i ][j+1][k+1] . .
4729 * b7: t->sw[i+1][j+1][k+1] . .
4739 bool handle_case_0x70e(struct torus *t, int i, int j, int k)
4741 int ip1 = canonicalize(i + 1, t->x_sz);
4742 int jp1 = canonicalize(j + 1, t->y_sz);
4743 int kp1 = canonicalize(k + 1, t->z_sz);
4745 if (install_tswitch(t, ip1, j, k,
4746 tfind_face_corner(t->sw[i][j][k],
4748 t->sw[ip1][j][kp1]))) {
4751 log_no_crnr(t, 0x70e, i, j, k, ip1, j, k);
4753 if (install_tswitch(t, i, jp1, k,
4754 tfind_face_corner(t->sw[i][j][k],
4756 t->sw[i][jp1][kp1]))) {
4759 log_no_crnr(t, 0x70e, i, j, k, i, jp1, k);
4768 * b2: t->sw[i ][j+1][k ] . . .
4769 * b3: t->sw[i+1][j+1][k ] O . O
4771 * b5: t->sw[i+1][j ][k+1] . .
4772 * b6: t->sw[i ][j+1][k+1] . .
4773 * b7: t->sw[i+1][j+1][k+1] . .
4783 bool handle_case_0x713(struct torus *t, int i, int j, int k)
4785 int ip1 = canonicalize(i + 1, t->x_sz);
4786 int jp1 = canonicalize(j + 1, t->y_sz);
4787 int kp1 = canonicalize(k + 1, t->z_sz);
4789 if (install_tswitch(t, ip1, j, k,
4790 tfind_face_corner(t->sw[ip1][jp1][k],
4791 t->sw[ip1][jp1][kp1],
4792 t->sw[ip1][j][kp1]))) {
4795 log_no_crnr(t, 0x713, i, j, k, ip1, j, k);
4797 if (install_tswitch(t, i, j, kp1,
4798 tfind_face_corner(t->sw[ip1][j][kp1],
4799 t->sw[ip1][jp1][kp1],
4800 t->sw[i][jp1][kp1]))) {
4803 log_no_crnr(t, 0x713, i, j, k, i, j, kp1);
4811 * b1: t->sw[i+1][j ][k ] . . .
4813 * b3: t->sw[i+1][j+1][k ] O . O
4815 * b5: t->sw[i+1][j ][k+1] . .
4816 * b6: t->sw[i ][j+1][k+1] . .
4817 * b7: t->sw[i+1][j+1][k+1] . .
4827 bool handle_case_0x715(struct torus *t, int i, int j, int k)
4829 int ip1 = canonicalize(i + 1, t->x_sz);
4830 int jp1 = canonicalize(j + 1, t->y_sz);
4831 int kp1 = canonicalize(k + 1, t->z_sz);
4833 if (install_tswitch(t, i, jp1, k,
4834 tfind_face_corner(t->sw[ip1][jp1][k],
4835 t->sw[ip1][jp1][kp1],
4836 t->sw[i][jp1][kp1]))) {
4839 log_no_crnr(t, 0x715, i, j, k, i, jp1, k);
4841 if (install_tswitch(t, i, j, kp1,
4842 tfind_face_corner(t->sw[ip1][j][kp1],
4843 t->sw[ip1][jp1][kp1],
4844 t->sw[i][jp1][kp1]))) {
4847 log_no_crnr(t, 0x715, i, j, k, i, j, kp1);
4856 * b2: t->sw[i ][j+1][k ] . .
4857 * b3: t->sw[i+1][j+1][k ] O . O
4858 * b4: t->sw[i ][j ][k+1] . . O
4860 * b6: t->sw[i ][j+1][k+1] . .
4861 * b7: t->sw[i+1][j+1][k+1] . . .
4871 bool handle_case_0x723(struct torus *t, int i, int j, int k)
4873 int ip1 = canonicalize(i + 1, t->x_sz);
4874 int jp1 = canonicalize(j + 1, t->y_sz);
4875 int kp1 = canonicalize(k + 1, t->z_sz);
4877 if (install_tswitch(t, i, j, k,
4878 tfind_face_corner(t->sw[i][jp1][k],
4880 t->sw[i][j][kp1]))) {
4883 log_no_crnr(t, 0x723, i, j, k, i, j, k);
4885 if (install_tswitch(t, ip1, j, kp1,
4886 tfind_face_corner(t->sw[i][j][kp1],
4888 t->sw[ip1][jp1][kp1]))) {
4891 log_no_crnr(t, 0x723, i, j, k, ip1, j, kp1);
4898 * b0: t->sw[i ][j ][k ] .
4900 * b2: t->sw[i ][j+1][k ] .
4902 * b4: t->sw[i ][j ][k+1] . . O
4904 * b6: t->sw[i ][j+1][k+1] . .
4905 * b7: t->sw[i+1][j+1][k+1] . .
4915 bool handle_case_0x72a(struct torus *t, int i, int j, int k)
4917 int ip1 = canonicalize(i + 1, t->x_sz);
4918 int jp1 = canonicalize(j + 1, t->y_sz);
4919 int kp1 = canonicalize(k + 1, t->z_sz);
4921 if (install_tswitch(t, ip1, jp1, k,
4922 tfind_face_corner(t->sw[i][jp1][k],
4924 t->sw[ip1][jp1][kp1]))) {
4927 log_no_crnr(t, 0x72a, i, j, k, ip1, jp1, k);
4929 if (install_tswitch(t, ip1, j, kp1,
4930 tfind_face_corner(t->sw[i][j][kp1],
4932 t->sw[ip1][jp1][kp1]))) {
4935 log_no_crnr(t, 0x72a, i, j, k, ip1, j, kp1);
4943 * b1: t->sw[i+1][j ][k ] . .
4944 * b2: t->sw[i ][j+1][k ] . .
4945 * b3: t->sw[i+1][j+1][k ] O . O
4948 * b6: t->sw[i ][j+1][k+1] . . .
4949 * b7: t->sw[i+1][j+1][k+1] . . .
4959 bool handle_case_0x731(struct torus *t, int i, int j, int k)
4961 int ip1 = canonicalize(i + 1, t->x_sz);
4962 int jp1 = canonicalize(j + 1, t->y_sz);
4963 int kp1 = canonicalize(k + 1, t->z_sz);
4965 if (install_tswitch(t, i, j, k,
4966 tfind_face_corner(t->sw[ip1][j][k],
4968 t->sw[i][jp1][k]))) {
4971 log_no_crnr(t, 0x731, i, j, k, i, j, k);
4973 if (install_tswitch(t, ip1, j, kp1,
4974 tfind_face_corner(t->sw[ip1][j][k],
4976 t->sw[ip1][jp1][kp1]))) {
4979 log_no_crnr(t, 0x731, i, j, k, ip1, j, kp1);
4986 * b0: t->sw[i ][j ][k ] . .
4988 * b2: t->sw[i ][j+1][k ] . .
4989 * b3: t->sw[i+1][j+1][k ] O . O
4992 * b6: t->sw[i ][j+1][k+1] . .
4993 * b7: t->sw[i+1][j+1][k+1] . .
5003 bool handle_case_0x732(struct torus *t, int i, int j, int k)
5005 int ip1 = canonicalize(i + 1, t->x_sz);
5006 int jp1 = canonicalize(j + 1, t->y_sz);
5007 int kp1 = canonicalize(k + 1, t->z_sz);
5009 if (install_tswitch(t, ip1, j, k,
5010 tfind_face_corner(t->sw[i][j][k],
5012 t->sw[ip1][jp1][k]))) {
5015 log_no_crnr(t, 0x732, i, j, k, ip1, j, k);
5017 if (install_tswitch(t, i, j, kp1,
5018 tfind_face_corner(t->sw[i][j][k],
5020 t->sw[i][jp1][kp1]))) {
5023 log_no_crnr(t, 0x732, i, j, k, i, j, kp1);
5031 * b1: t->sw[i+1][j ][k ] . .
5033 * b3: t->sw[i+1][j+1][k ] O . O
5034 * b4: t->sw[i ][j ][k+1] O . .
5035 * b5: t->sw[i+1][j ][k+1] . . .
5037 * b7: t->sw[i+1][j+1][k+1] . . .
5047 bool handle_case_0x745(struct torus *t, int i, int j, int k)
5049 int ip1 = canonicalize(i + 1, t->x_sz);
5050 int jp1 = canonicalize(j + 1, t->y_sz);
5051 int kp1 = canonicalize(k + 1, t->z_sz);
5053 if (install_tswitch(t, i, j, k,
5054 tfind_face_corner(t->sw[ip1][j][k],
5056 t->sw[i][j][kp1]))) {
5059 log_no_crnr(t, 0x745, i, j, k, i, j, k);
5061 if (install_tswitch(t, i, jp1, kp1,
5062 tfind_face_corner(t->sw[i][j][kp1],
5064 t->sw[ip1][jp1][kp1]))) {
5067 log_no_crnr(t, 0x745, i, j, k, i, jp1, kp1);
5074 * b0: t->sw[i ][j ][k ] .
5075 * b1: t->sw[i+1][j ][k ] .
5078 * b4: t->sw[i ][j ][k+1] O . .
5079 * b5: t->sw[i+1][j ][k+1] . .
5081 * b7: t->sw[i+1][j+1][k+1] . .
5091 bool handle_case_0x74c(struct torus *t, int i, int j, int k)
5093 int ip1 = canonicalize(i + 1, t->x_sz);
5094 int jp1 = canonicalize(j + 1, t->y_sz);
5095 int kp1 = canonicalize(k + 1, t->z_sz);
5097 if (install_tswitch(t, ip1, jp1, k,
5098 tfind_face_corner(t->sw[ip1][j][k],
5100 t->sw[ip1][jp1][kp1]))) {
5103 log_no_crnr(t, 0x74c, i, j, k, ip1, jp1, k);
5105 if (install_tswitch(t, i, jp1, kp1,
5106 tfind_face_corner(t->sw[i][j][kp1],
5108 t->sw[ip1][jp1][kp1]))) {
5111 log_no_crnr(t, 0x74c, i, j, k, i, jp1, kp1);
5119 * b1: t->sw[i+1][j ][k ] . .
5120 * b2: t->sw[i ][j+1][k ] . .
5121 * b3: t->sw[i+1][j+1][k ] O . O
5123 * b5: t->sw[i+1][j ][k+1] . . .
5125 * b7: t->sw[i+1][j+1][k+1] . . .
5135 bool handle_case_0x751(struct torus *t, int i, int j, int k)
5137 int ip1 = canonicalize(i + 1, t->x_sz);
5138 int jp1 = canonicalize(j + 1, t->y_sz);
5139 int kp1 = canonicalize(k + 1, t->z_sz);
5141 if (install_tswitch(t, i, j, k,
5142 tfind_face_corner(t->sw[ip1][j][k],
5144 t->sw[i][jp1][k]))) {
5147 log_no_crnr(t, 0x751, i, j, k, i, j, k);
5149 if (install_tswitch(t, i, jp1, kp1,
5150 tfind_face_corner(t->sw[i][jp1][k],
5152 t->sw[ip1][jp1][kp1]))) {
5155 log_no_crnr(t, 0x751, i, j, k, i, jp1, kp1);
5162 * b0: t->sw[i ][j ][k ] . .
5163 * b1: t->sw[i+1][j ][k ] . .
5165 * b3: t->sw[i+1][j+1][k ] O . O
5167 * b5: t->sw[i+1][j ][k+1] . .
5169 * b7: t->sw[i+1][j+1][k+1] . .
5179 bool handle_case_0x754(struct torus *t, int i, int j, int k)
5181 int ip1 = canonicalize(i + 1, t->x_sz);
5182 int jp1 = canonicalize(j + 1, t->y_sz);
5183 int kp1 = canonicalize(k + 1, t->z_sz);
5185 if (install_tswitch(t, i, jp1, k,
5186 tfind_face_corner(t->sw[i][j][k],
5188 t->sw[ip1][jp1][k]))) {
5191 log_no_crnr(t, 0x754, i, j, k, i, jp1, k);
5193 if (install_tswitch(t, i, j, kp1,
5194 tfind_face_corner(t->sw[i][j][k],
5196 t->sw[ip1][j][kp1]))) {
5199 log_no_crnr(t, 0x754, i, j, k, i, j, kp1);
5206 * b0: t->sw[i ][j ][k ] .
5207 * b1: t->sw[i+1][j ][k ] .
5208 * b2: t->sw[i ][j+1][k ] .
5209 * b3: t->sw[i+1][j+1][k ] O . O
5213 * b7: t->sw[i+1][j+1][k+1] . .
5223 bool handle_case_0x770(struct torus *t, int i, int j, int k)
5225 int ip1 = canonicalize(i + 1, t->x_sz);
5226 int jp1 = canonicalize(j + 1, t->y_sz);
5227 int kp1 = canonicalize(k + 1, t->z_sz);
5229 if (install_tswitch(t, ip1, j, kp1,
5230 tfind_face_corner(t->sw[ip1][j][k],
5232 t->sw[ip1][jp1][kp1]))) {
5235 log_no_crnr(t, 0x770, i, j, k, ip1, j, kp1);
5237 if (install_tswitch(t, i, jp1, kp1,
5238 tfind_face_corner(t->sw[i][jp1][k],
5240 t->sw[ip1][jp1][kp1]))) {
5243 log_no_crnr(t, 0x770, i, j, k, i, jp1, kp1);
5250 * b0: t->sw[i ][j ][k ]
5252 * b2: t->sw[i ][j+1][k ]
5254 * b4: t->sw[i ][j ][k+1] . . O .
5255 * b5: t->sw[i+1][j ][k+1] . . .
5256 * b6: t->sw[i ][j+1][k+1] . . .
5267 bool handle_case_0x78a(struct torus *t, int i, int j, int k)
5269 int ip1 = canonicalize(i + 1, t->x_sz);
5270 int jp1 = canonicalize(j + 1, t->y_sz);
5271 int kp1 = canonicalize(k + 1, t->z_sz);
5273 if (install_tswitch(t, ip1, j, k,
5274 tfind_face_corner(t->sw[i][j][k],
5276 t->sw[ip1][j][kp1]))) {
5279 log_no_crnr(t, 0x78a, i, j, k, ip1, j, k);
5281 if (install_tswitch(t, ip1, jp1, kp1,
5282 tfind_face_corner(t->sw[ip1][j][kp1],
5284 t->sw[i][jp1][kp1]))) {
5287 log_no_crnr(t, 0x78a, i, j, k, ip1, jp1, kp1);
5294 * b0: t->sw[i ][j ][k ]
5295 * b1: t->sw[i+1][j ][k ]
5298 * b4: t->sw[i ][j ][k+1] . O . .
5299 * b5: t->sw[i+1][j ][k+1] . . .
5300 * b6: t->sw[i ][j+1][k+1] . . .
5311 bool handle_case_0x78c(struct torus *t, int i, int j, int k)
5313 int ip1 = canonicalize(i + 1, t->x_sz);
5314 int jp1 = canonicalize(j + 1, t->y_sz);
5315 int kp1 = canonicalize(k + 1, t->z_sz);
5317 if (install_tswitch(t, i, jp1, k,
5318 tfind_face_corner(t->sw[i][j][k],
5320 t->sw[i][jp1][kp1]))) {
5323 log_no_crnr(t, 0x78c, i, j, k, i, jp1, k);
5325 if (install_tswitch(t, ip1, jp1, kp1,
5326 tfind_face_corner(t->sw[ip1][j][kp1],
5328 t->sw[i][jp1][kp1]))) {
5331 log_no_crnr(t, 0x78c, i, j, k, ip1, jp1, kp1);
5338 * b0: t->sw[i ][j ][k ]
5340 * b2: t->sw[i ][j+1][k ]
5341 * b3: t->sw[i+1][j+1][k ] O O
5342 * b4: t->sw[i ][j ][k+1] . . O
5344 * b6: t->sw[i ][j+1][k+1] . .
5355 bool handle_case_0x7a2(struct torus *t, int i, int j, int k)
5357 int ip1 = canonicalize(i + 1, t->x_sz);
5358 int jp1 = canonicalize(j + 1, t->y_sz);
5359 int kp1 = canonicalize(k + 1, t->z_sz);
5361 if (install_tswitch(t, ip1, j, k,
5362 tfind_face_corner(t->sw[i][j][k],
5364 t->sw[ip1][jp1][k]))) {
5367 log_no_crnr(t, 0x7a2, i, j, k, ip1, j, k);
5369 if (install_tswitch(t, ip1, jp1, kp1,
5370 tfind_face_corner(t->sw[i][jp1][kp1],
5372 t->sw[ip1][jp1][k]))) {
5375 log_no_crnr(t, 0x7a2, i, j, k, ip1, jp1, kp1);
5382 * b0: t->sw[i ][j ][k ]
5383 * b1: t->sw[ip1][j ][k ]
5384 * b2: t->sw[i ][j+1][k ]
5386 * b4: t->sw[i ][j ][k+1] . . O
5388 * b6: t->sw[i ][j+1][k+1] . .
5399 bool handle_case_0x7a8(struct torus *t, int i, int j, int k)
5401 int ip1 = canonicalize(i + 1, t->x_sz);
5402 int jp1 = canonicalize(j + 1, t->y_sz);
5403 int kp1 = canonicalize(k + 1, t->z_sz);
5405 if (install_tswitch(t, ip1, jp1, k,
5406 tfind_face_corner(t->sw[ip1][j][k],
5408 t->sw[i][jp1][k]))) {
5411 log_no_crnr(t, 0x7a8, i, j, k, ip1, jp1, k);
5413 if (install_tswitch(t, ip1, j, kp1,
5414 tfind_face_corner(t->sw[i][j][kp1],
5416 t->sw[ip1][j][k]))) {
5419 log_no_crnr(t, 0x7a8, i, j, k, ip1, j, kp1);
5426 * b0: t->sw[i ][j ][k ]
5427 * b1: t->sw[i+1][j ][k ]
5428 * b2: t->sw[i ][j+1][k ]
5429 * b3: t->sw[i+1][j+1][k ] O O
5432 * b6: t->sw[i ][j+1][k+1] . . .
5443 bool handle_case_0x7b0(struct torus *t, int i, int j, int k)
5445 int ip1 = canonicalize(i + 1, t->x_sz);
5446 int jp1 = canonicalize(j + 1, t->y_sz);
5447 int kp1 = canonicalize(k + 1, t->z_sz);
5449 if (install_tswitch(t, i, j, kp1,
5450 tfind_face_corner(t->sw[i][j][k],
5452 t->sw[i][jp1][kp1]))) {
5455 log_no_crnr(t, 0x7b0, i, j, k, i, j, kp1);
5457 if (install_tswitch(t, ip1, jp1, kp1,
5458 tfind_face_corner(t->sw[i][jp1][kp1],
5460 t->sw[ip1][jp1][k]))) {
5463 log_no_crnr(t, 0x7b0, i, j, k, ip1, jp1, kp1);
5470 * b0: t->sw[i ][j ][k ]
5471 * b1: t->sw[i+1][j ][k ]
5473 * b3: t->sw[i+1][j+1][k ] O O
5474 * b4: t->sw[i ][j ][k+1] O . .
5475 * b5: t->sw[i+1][j ][k+1] . . .
5487 bool handle_case_0x7c4(struct torus *t, int i, int j, int k)
5489 int ip1 = canonicalize(i + 1, t->x_sz);
5490 int jp1 = canonicalize(j + 1, t->y_sz);
5491 int kp1 = canonicalize(k + 1, t->z_sz);
5493 if (install_tswitch(t, i, jp1, k,
5494 tfind_face_corner(t->sw[i][j][k],
5496 t->sw[ip1][jp1][k]))) {
5499 log_no_crnr(t, 0x7c4, i, j, k, i, jp1, k);
5501 if (install_tswitch(t, ip1, jp1, kp1,
5502 tfind_face_corner(t->sw[ip1][j][kp1],
5504 t->sw[ip1][jp1][k]))) {
5507 log_no_crnr(t, 0x7c4, i, j, k, ip1, jp1, kp1);
5514 * b0: t->sw[i ][j ][k ]
5515 * b1: t->sw[i+1][j ][k ]
5516 * b2: t->sw[i ][j+1][k ]
5518 * b4: t->sw[i ][j ][k+1] O . .
5519 * b5: t->sw[i+1][j ][k+1] . .
5531 bool handle_case_0x7c8(struct torus *t, int i, int j, int k)
5533 int ip1 = canonicalize(i + 1, t->x_sz);
5534 int jp1 = canonicalize(j + 1, t->y_sz);
5535 int kp1 = canonicalize(k + 1, t->z_sz);
5537 if (install_tswitch(t, ip1, jp1, k,
5538 tfind_face_corner(t->sw[ip1][j][k],
5540 t->sw[i][jp1][k]))) {
5543 log_no_crnr(t, 0x7c8, i, j, k, ip1, jp1, k);
5545 if (install_tswitch(t, i, jp1, kp1,
5546 tfind_face_corner(t->sw[i][j][kp1],
5548 t->sw[i][jp1][k]))) {
5551 log_no_crnr(t, 0x7c8, i, j, k, i, jp1, kp1);
5558 * b0: t->sw[i ][j ][k ]
5559 * b1: t->sw[i+1][j ][k ]
5560 * b2: t->sw[i ][j+1][k ]
5561 * b3: t->sw[i+1][j+1][k ] O O
5563 * b5: t->sw[i+1][j ][k+1] . . .
5575 bool handle_case_0x7d0(struct torus *t, int i, int j, int k)
5577 int ip1 = canonicalize(i + 1, t->x_sz);
5578 int jp1 = canonicalize(j + 1, t->y_sz);
5579 int kp1 = canonicalize(k + 1, t->z_sz);
5581 if (install_tswitch(t, i, j, kp1,
5582 tfind_face_corner(t->sw[i][j][k],
5584 t->sw[ip1][j][kp1]))) {
5587 log_no_crnr(t, 0x7d0, i, j, k, i, j, kp1);
5589 if (install_tswitch(t, ip1, jp1, kp1,
5590 tfind_face_corner(t->sw[ip1][j][kp1],
5592 t->sw[ip1][jp1][k]))) {
5595 log_no_crnr(t, 0x7d0, i, j, k, ip1, jp1, kp1);
5602 * b0: t->sw[i ][j ][k ]
5603 * b1: t->sw[i+1][j ][k ]
5604 * b2: t->sw[i ][j+1][k ]
5605 * b3: t->sw[i+1][j+1][k ] O O
5606 * b4: t->sw[i ][j ][k+1] O
5619 bool handle_case_0x7e0(struct torus *t, int i, int j, int k)
5621 int ip1 = canonicalize(i + 1, t->x_sz);
5622 int jp1 = canonicalize(j + 1, t->y_sz);
5623 int kp1 = canonicalize(k + 1, t->z_sz);
5625 if (install_tswitch(t, ip1, j, kp1,
5626 tfind_face_corner(t->sw[i][j][kp1],
5628 t->sw[ip1][j][k]))) {
5631 log_no_crnr(t, 0x7e0, i, j, k, ip1, j, kp1);
5633 if (install_tswitch(t, i, jp1, kp1,
5634 tfind_face_corner(t->sw[i][j][kp1],
5636 t->sw[i][jp1][k]))) {
5639 log_no_crnr(t, 0x7e0, i, j, k, i, jp1, kp1);
5644 * Handle the cases where two corners on a single edge are missing.
5652 * b2: t->sw[i ][j+1][k ] . . .
5653 * b3: t->sw[i+1][j+1][k ] O . O
5654 * b4: t->sw[i ][j ][k+1] . . O .
5655 * b5: t->sw[i+1][j ][k+1] . . . .
5656 * b6: t->sw[i ][j+1][k+1] . . .
5657 * b7: t->sw[i+1][j+1][k+1] . . . .
5667 bool handle_case_0x703(struct torus *t, int i, int j, int k)
5669 int ip1 = canonicalize(i + 1, t->x_sz);
5670 int jp1 = canonicalize(j + 1, t->y_sz);
5671 int kp1 = canonicalize(k + 1, t->z_sz);
5673 if (install_tswitch(t, i, j, k,
5674 tfind_face_corner(t->sw[i][jp1][k],
5676 t->sw[i][j][kp1]))) {
5679 log_no_crnr(t, 0x703, i, j, k, i, j, k);
5681 if (install_tswitch(t, ip1, j, k,
5682 tfind_face_corner(t->sw[ip1][jp1][k],
5683 t->sw[ip1][jp1][kp1],
5684 t->sw[ip1][j][kp1]))) {
5687 log_no_crnr(t, 0x703, i, j, k, ip1, j, k);
5695 * b1: t->sw[i+1][j ][k ] . . .
5697 * b3: t->sw[i+1][j+1][k ] O . O
5698 * b4: t->sw[i ][j ][k+1] . O . .
5699 * b5: t->sw[i+1][j ][k+1] . . . .
5700 * b6: t->sw[i ][j+1][k+1] . . .
5701 * b7: t->sw[i+1][j+1][k+1] . . . .
5711 bool handle_case_0x705(struct torus *t, int i, int j, int k)
5713 int ip1 = canonicalize(i + 1, t->x_sz);
5714 int jp1 = canonicalize(j + 1, t->y_sz);
5715 int kp1 = canonicalize(k + 1, t->z_sz);
5717 if (install_tswitch(t, i, j, k,
5718 tfind_face_corner(t->sw[ip1][j][k],
5720 t->sw[i][j][kp1]))) {
5723 log_no_crnr(t, 0x705, i, j, k, i, j, k);
5725 if (install_tswitch(t, i, jp1, k,
5726 tfind_face_corner(t->sw[ip1][jp1][k],
5727 t->sw[ip1][jp1][kp1],
5728 t->sw[i][jp1][kp1]))) {
5731 log_no_crnr(t, 0x705, i, j, k, i, jp1, k);
5738 * b0: t->sw[i ][j ][k ] . .
5740 * b2: t->sw[i ][j+1][k ] . .
5742 * b4: t->sw[i ][j ][k+1] . . O .
5743 * b5: t->sw[i+1][j ][k+1] . . .
5744 * b6: t->sw[i ][j+1][k+1] . . .
5745 * b7: t->sw[i+1][j+1][k+1] . . .
5755 bool handle_case_0x70a(struct torus *t, int i, int j, int k)
5757 int ip1 = canonicalize(i + 1, t->x_sz);
5758 int jp1 = canonicalize(j + 1, t->y_sz);
5759 int kp1 = canonicalize(k + 1, t->z_sz);
5761 if (install_tswitch(t, ip1, j, k,
5762 tfind_face_corner(t->sw[i][j][k],
5764 t->sw[ip1][j][kp1]))) {
5767 log_no_crnr(t, 0x70a, i, j, k, ip1, j, k);
5769 if (install_tswitch(t, ip1, jp1, k,
5770 tfind_face_corner(t->sw[i][jp1][k],
5772 t->sw[ip1][jp1][kp1]))) {
5775 log_no_crnr(t, 0x70a, i, j, k, ip1, jp1, k);
5782 * b0: t->sw[i ][j ][k ] . .
5783 * b1: t->sw[i+1][j ][k ] . .
5786 * b4: t->sw[i ][j ][k+1] . O . .
5787 * b5: t->sw[i+1][j ][k+1] . . .
5788 * b6: t->sw[i ][j+1][k+1] . . .
5789 * b7: t->sw[i+1][j+1][k+1] . . .
5799 bool handle_case_0x70c(struct torus *t, int i, int j, int k)
5801 int ip1 = canonicalize(i + 1, t->x_sz);
5802 int jp1 = canonicalize(j + 1, t->y_sz);
5803 int kp1 = canonicalize(k + 1, t->z_sz);
5805 if (install_tswitch(t, i, jp1, k,
5806 tfind_face_corner(t->sw[i][j][k],
5808 t->sw[i][jp1][kp1]))) {
5811 log_no_crnr(t, 0x70c, i, j, k, i, jp1, k);
5813 if (install_tswitch(t, ip1, jp1, k,
5814 tfind_face_corner(t->sw[ip1][j][k],
5816 t->sw[ip1][jp1][kp1]))) {
5819 log_no_crnr(t, 0x70c, i, j, k, ip1, jp1, k);
5827 * b1: t->sw[i+1][j ][k ] . . .
5828 * b2: t->sw[i ][j+1][k ] . . .
5829 * b3: t->sw[i+1][j+1][k ] O . O
5831 * b5: t->sw[i+1][j ][k+1] . . . .
5832 * b6: t->sw[i ][j+1][k+1] . . . .
5833 * b7: t->sw[i+1][j+1][k+1] . . . .
5843 bool handle_case_0x711(struct torus *t, int i, int j, int k)
5845 int ip1 = canonicalize(i + 1, t->x_sz);
5846 int jp1 = canonicalize(j + 1, t->y_sz);
5847 int kp1 = canonicalize(k + 1, t->z_sz);
5849 if (install_tswitch(t, i, j, k,
5850 tfind_face_corner(t->sw[ip1][j][k],
5852 t->sw[i][jp1][k]))) {
5855 log_no_crnr(t, 0x711, i, j, k, i, j, k);
5857 if (install_tswitch(t, i, j, kp1,
5858 tfind_face_corner(t->sw[ip1][j][kp1],
5859 t->sw[ip1][jp1][kp1],
5860 t->sw[i][jp1][kp1]))) {
5863 log_no_crnr(t, 0x711, i, j, k, i, j, kp1);
5870 * b0: t->sw[i ][j ][k ] . .
5872 * b2: t->sw[i ][j+1][k ] . .
5873 * b3: t->sw[i+1][j+1][k ] O . O
5874 * b4: t->sw[i ][j ][k+1] . . O
5876 * b6: t->sw[i ][j+1][k+1] . .
5877 * b7: t->sw[i+1][j+1][k+1] . . .
5887 bool handle_case_0x722(struct torus *t, int i, int j, int k)
5889 int ip1 = canonicalize(i + 1, t->x_sz);
5890 int jp1 = canonicalize(j + 1, t->y_sz);
5891 int kp1 = canonicalize(k + 1, t->z_sz);
5893 if (install_tswitch(t, ip1, j, k,
5894 tfind_face_corner(t->sw[i][j][k],
5896 t->sw[ip1][jp1][k]))) {
5899 log_no_crnr(t, 0x722, i, j, k, ip1, j, k);
5901 if (install_tswitch(t, ip1, j, kp1,
5902 tfind_face_corner(t->sw[i][j][kp1],
5904 t->sw[ip1][jp1][kp1]))) {
5907 log_no_crnr(t, 0x722, i, j, k, ip1, j, kp1);
5914 * b0: t->sw[i ][j ][k ] . .
5915 * b1: t->sw[i+1][j ][k ] . .
5916 * b2: t->sw[i ][j+1][k ] . .
5917 * b3: t->sw[i+1][j+1][k ] O . O
5920 * b6: t->sw[i ][j+1][k+1] . . .
5921 * b7: t->sw[i+1][j+1][k+1] . . .
5931 bool handle_case_0x730(struct torus *t, int i, int j, int k)
5933 int ip1 = canonicalize(i + 1, t->x_sz);
5934 int jp1 = canonicalize(j + 1, t->y_sz);
5935 int kp1 = canonicalize(k + 1, t->z_sz);
5937 if (install_tswitch(t, i, j, kp1,
5938 tfind_face_corner(t->sw[i][j][k],
5940 t->sw[i][jp1][kp1]))) {
5943 log_no_crnr(t, 0x730, i, j, k, i, j, kp1);
5945 if (install_tswitch(t, ip1, j, kp1,
5946 tfind_face_corner(t->sw[ip1][j][k],
5948 t->sw[ip1][jp1][kp1]))) {
5951 log_no_crnr(t, 0x730, i, j, k, ip1, j, kp1);
5958 * b0: t->sw[i ][j ][k ] . .
5959 * b1: t->sw[i+1][j ][k ] . .
5961 * b3: t->sw[i+1][j+1][k ] O . O
5962 * b4: t->sw[i ][j ][k+1] O . .
5963 * b5: t->sw[i+1][j ][k+1] . . .
5965 * b7: t->sw[i+1][j+1][k+1] . . .
5975 bool handle_case_0x744(struct torus *t, int i, int j, int k)
5977 int ip1 = canonicalize(i + 1, t->x_sz);
5978 int jp1 = canonicalize(j + 1, t->y_sz);
5979 int kp1 = canonicalize(k + 1, t->z_sz);
5981 if (install_tswitch(t, i, jp1, k,
5982 tfind_face_corner(t->sw[i][j][k],
5984 t->sw[ip1][jp1][k]))) {
5987 log_no_crnr(t, 0x744, i, j, k, i, jp1, k);
5989 if (install_tswitch(t, i, jp1, kp1,
5990 tfind_face_corner(t->sw[i][j][kp1],
5992 t->sw[ip1][jp1][kp1]))) {
5995 log_no_crnr(t, 0x744, i, j, k, i, jp1, kp1);
6002 * b0: t->sw[i ][j ][k ] . .
6003 * b1: t->sw[i+1][j ][k ] . .
6004 * b2: t->sw[i ][j+1][k ] . .
6005 * b3: t->sw[i+1][j+1][k ] O . O
6007 * b5: t->sw[i+1][j ][k+1] . . .
6009 * b7: t->sw[i+1][j+1][k+1] . . .
6019 bool handle_case_0x750(struct torus *t, int i, int j, int k)
6021 int ip1 = canonicalize(i + 1, t->x_sz);
6022 int jp1 = canonicalize(j + 1, t->y_sz);
6023 int kp1 = canonicalize(k + 1, t->z_sz);
6025 if (install_tswitch(t, i, j, kp1,
6026 tfind_face_corner(t->sw[i][j][k],
6028 t->sw[ip1][j][kp1]))) {
6031 log_no_crnr(t, 0x750, i, j, k, i, j, kp1);
6033 if (install_tswitch(t, i, jp1, kp1,
6034 tfind_face_corner(t->sw[i][jp1][k],
6036 t->sw[ip1][jp1][kp1]))) {
6039 log_no_crnr(t, 0x750, i, j, k, i, jp1, kp1);
6046 * b0: t->sw[i ][j ][k ]
6047 * b1: t->sw[ip1][j ][k ]
6048 * b2: t->sw[i ][j+1][k ]
6050 * b4: t->sw[i ][j ][k+1] . . O . .
6051 * b5: t->sw[i+1][j ][k+1] . . . .
6052 * b6: t->sw[i ][j+1][k+1] . . . .
6063 bool handle_case_0x788(struct torus *t, int i, int j, int k)
6065 int ip1 = canonicalize(i + 1, t->x_sz);
6066 int jp1 = canonicalize(j + 1, t->y_sz);
6067 int kp1 = canonicalize(k + 1, t->z_sz);
6069 if (install_tswitch(t, ip1, jp1, k,
6070 tfind_face_corner(t->sw[ip1][j][k],
6072 t->sw[i][jp1][k]))) {
6075 log_no_crnr(t, 0x788, i, j, k, ip1, jp1, k);
6077 if (install_tswitch(t, ip1, jp1, kp1,
6078 tfind_face_corner(t->sw[ip1][j][kp1],
6080 t->sw[i][jp1][kp1]))) {
6083 log_no_crnr(t, 0x788, i, j, k, ip1, jp1, kp1);
6090 * b0: t->sw[i ][j ][k ]
6091 * b1: t->sw[i+1][j ][k ]
6092 * b2: t->sw[i ][j+1][k ]
6093 * b3: t->sw[i+1][j+1][k ] O O
6094 * b4: t->sw[i ][j ][k+1] . . O
6096 * b6: t->sw[i ][j+1][k+1] . . .
6107 bool handle_case_0x7a0(struct torus *t, int i, int j, int k)
6109 int ip1 = canonicalize(i + 1, t->x_sz);
6110 int jp1 = canonicalize(j + 1, t->y_sz);
6111 int kp1 = canonicalize(k + 1, t->z_sz);
6113 if (install_tswitch(t, ip1, j, kp1,
6114 tfind_face_corner(t->sw[i][j][kp1],
6116 t->sw[ip1][j][k]))) {
6119 log_no_crnr(t, 0x7a0, i, j, k, ip1, j, kp1);
6121 if (install_tswitch(t, ip1, jp1, kp1,
6122 tfind_face_corner(t->sw[i][jp1][kp1],
6124 t->sw[ip1][jp1][k]))) {
6127 log_no_crnr(t, 0x7a0, i, j, k, ip1, jp1, kp1);
6134 * b0: t->sw[i ][j ][k ]
6135 * b1: t->sw[i+1][j ][k ]
6136 * b2: t->sw[i ][j+1][k ]
6137 * b3: t->sw[i+1][j+1][k ] O O
6138 * b4: t->sw[i ][j ][k+1] O . .
6139 * b5: t->sw[i+1][j ][k+1] . . . .
6151 bool handle_case_0x7c0(struct torus *t, int i, int j, int k)
6153 int ip1 = canonicalize(i + 1, t->x_sz);
6154 int jp1 = canonicalize(j + 1, t->y_sz);
6155 int kp1 = canonicalize(k + 1, t->z_sz);
6157 if (install_tswitch(t, i, jp1, kp1,
6158 tfind_face_corner(t->sw[i][j][kp1],
6160 t->sw[i][jp1][k]))) {
6163 log_no_crnr(t, 0x7c0, i, j, k, i, jp1, kp1);
6165 if (install_tswitch(t, ip1, jp1, kp1,
6166 tfind_face_corner(t->sw[ip1][j][kp1],
6168 t->sw[ip1][jp1][k]))) {
6171 log_no_crnr(t, 0x7c0, i, j, k, ip1, jp1, kp1);
6176 * Handle the cases where a single corner is missing.
6183 * b1: t->sw[i+1][j ][k ] . . .
6184 * b2: t->sw[i ][j+1][k ] . . .
6185 * b3: t->sw[i+1][j+1][k ] O . O
6186 * b4: t->sw[i ][j ][k+1] . . O . .
6187 * b5: t->sw[i+1][j ][k+1] . . . . . .
6188 * b6: t->sw[i ][j+1][k+1] . . . .
6189 * b7: t->sw[i+1][j+1][k+1] . . . . . .
6199 bool handle_case_0x701(struct torus *t, int i, int j, int k)
6201 int ip1 = canonicalize(i + 1, t->x_sz);
6202 int jp1 = canonicalize(j + 1, t->y_sz);
6204 if (install_tswitch(t, i, j, k,
6205 tfind_face_corner(t->sw[i][jp1][k],
6207 t->sw[ip1][j][k]))) {
6210 log_no_crnr(t, 0x701, i, j, k, i, j, k);
6217 * b0: t->sw[i ][j ][k ] . . .
6219 * b2: t->sw[i ][j+1][k ] . . .
6220 * b3: t->sw[i+1][j+1][k ] O . O
6221 * b4: t->sw[i ][j ][k+1] . . O .
6222 * b5: t->sw[i+1][j ][k+1] . . . .
6223 * b6: t->sw[i ][j+1][k+1] . . .
6224 * b7: t->sw[i+1][j+1][k+1] . . . .
6234 bool handle_case_0x702(struct torus *t, int i, int j, int k)
6236 int ip1 = canonicalize(i + 1, t->x_sz);
6237 int kp1 = canonicalize(k + 1, t->z_sz);
6239 if (install_tswitch(t, ip1, j, k,
6240 tfind_face_corner(t->sw[i][j][k],
6242 t->sw[ip1][j][kp1]))) {
6245 log_no_crnr(t, 0x702, i, j, k, ip1, j, k);
6252 * b0: t->sw[i ][j ][k ] . . .
6253 * b1: t->sw[i+1][j ][k ] . . .
6255 * b3: t->sw[i+1][j+1][k ] O . O
6256 * b4: t->sw[i ][j ][k+1] . O . .
6257 * b5: t->sw[i+1][j ][k+1] . . . .
6258 * b6: t->sw[i ][j+1][k+1] . . .
6259 * b7: t->sw[i+1][j+1][k+1] . . . .
6269 bool handle_case_0x704(struct torus *t, int i, int j, int k)
6271 int jp1 = canonicalize(j + 1, t->y_sz);
6272 int kp1 = canonicalize(k + 1, t->z_sz);
6274 if (install_tswitch(t, i, jp1, k,
6275 tfind_face_corner(t->sw[i][j][k],
6277 t->sw[i][jp1][kp1]))) {
6280 log_no_crnr(t, 0x704, i, j, k, i, jp1, k);
6287 * b0: t->sw[i ][j ][k ] . .
6288 * b1: t->sw[i+1][j ][k ] . .
6289 * b2: t->sw[i ][j+1][k ] . .
6291 * b4: t->sw[i ][j ][k+1] . . O . .
6292 * b5: t->sw[i+1][j ][k+1] . . . .
6293 * b6: t->sw[i ][j+1][k+1] . . . .
6294 * b7: t->sw[i+1][j+1][k+1] . . . .
6304 bool handle_case_0x708(struct torus *t, int i, int j, int k)
6306 int ip1 = canonicalize(i + 1, t->x_sz);
6307 int jp1 = canonicalize(j + 1, t->y_sz);
6309 if (install_tswitch(t, ip1, jp1, k,
6310 tfind_face_corner(t->sw[i][jp1][k],
6312 t->sw[ip1][j][k]))) {
6315 log_no_crnr(t, 0x708, i, j, k, ip1, jp1, k);
6322 * b0: t->sw[i ][j ][k ] . . .
6323 * b1: t->sw[i+1][j ][k ] . . .
6324 * b2: t->sw[i ][j+1][k ] . . .
6325 * b3: t->sw[i+1][j+1][k ] O . O
6327 * b5: t->sw[i+1][j ][k+1] . . . .
6328 * b6: t->sw[i ][j+1][k+1] . . . .
6329 * b7: t->sw[i+1][j+1][k+1] . . . .
6339 bool handle_case_0x710(struct torus *t, int i, int j, int k)
6341 int ip1 = canonicalize(i + 1, t->x_sz);
6342 int kp1 = canonicalize(k + 1, t->z_sz);
6344 if (install_tswitch(t, i, j, kp1,
6345 tfind_face_corner(t->sw[i][j][k],
6347 t->sw[ip1][j][kp1]))) {
6350 log_no_crnr(t, 0x710, i, j, k, i, j, kp1);
6357 * b0: t->sw[i ][j ][k ] . .
6358 * b1: t->sw[i+1][j ][k ] . .
6359 * b2: t->sw[i ][j+1][k ] . .
6360 * b3: t->sw[i+1][j+1][k ] O . O
6361 * b4: t->sw[i ][j ][k+1] . . O
6363 * b6: t->sw[i ][j+1][k+1] . . .
6364 * b7: t->sw[i+1][j+1][k+1] . . . .
6374 bool handle_case_0x720(struct torus *t, int i, int j, int k)
6376 int ip1 = canonicalize(i + 1, t->x_sz);
6377 int kp1 = canonicalize(k + 1, t->z_sz);
6379 if (install_tswitch(t, ip1, j, kp1,
6380 tfind_face_corner(t->sw[ip1][j][k],
6382 t->sw[i][j][kp1]))) {
6385 log_no_crnr(t, 0x720, i, j, k, ip1, j, kp1);
6392 * b0: t->sw[i ][j ][k ] . .
6393 * b1: t->sw[i+1][j ][k ] . .
6394 * b2: t->sw[i ][j+1][k ] . .
6395 * b3: t->sw[i+1][j+1][k ] O . O
6396 * b4: t->sw[i ][j ][k+1] O . .
6397 * b5: t->sw[i+1][j ][k+1] . . . .
6399 * b7: t->sw[i+1][j+1][k+1] . . . .
6409 bool handle_case_0x740(struct torus *t, int i, int j, int k)
6411 int jp1 = canonicalize(j + 1, t->y_sz);
6412 int kp1 = canonicalize(k + 1, t->z_sz);
6414 if (install_tswitch(t, i, jp1, kp1,
6415 tfind_face_corner(t->sw[i][jp1][k],
6417 t->sw[i][j][kp1]))) {
6420 log_no_crnr(t, 0x740, i, j, k, i, jp1, kp1);
6427 * b0: t->sw[i ][j ][k ]
6428 * b1: t->sw[i+1][j ][k ]
6429 * b2: t->sw[i ][j+1][k ]
6430 * b3: t->sw[i+1][j+1][k ] O O
6431 * b4: t->sw[i ][j ][k+1] . . O . .
6432 * b5: t->sw[i+1][j ][k+1] . . . . . .
6433 * b6: t->sw[i ][j+1][k+1] . . . .
6444 bool handle_case_0x780(struct torus *t, int i, int j, int k)
6446 int ip1 = canonicalize(i + 1, t->x_sz);
6447 int jp1 = canonicalize(j + 1, t->y_sz);
6448 int kp1 = canonicalize(k + 1, t->z_sz);
6450 if (install_tswitch(t, ip1, jp1, kp1,
6451 tfind_face_corner(t->sw[i][jp1][kp1],
6453 t->sw[ip1][j][kp1]))) {
6456 log_no_crnr(t, 0x780, i, j, k, ip1, jp1, kp1);
6461 * Make sure links between all known torus/mesh switches are installed.
6463 * We don't have to worry about links that wrap on a mesh coordinate, as
6464 * there shouldn't be any; if there are it indicates an input error.
6467 void check_tlinks(struct torus *t, int i, int j, int k)
6469 struct t_switch ****sw = t->sw;
6470 int ip1 = canonicalize(i + 1, t->x_sz);
6471 int jp1 = canonicalize(j + 1, t->y_sz);
6472 int kp1 = canonicalize(k + 1, t->z_sz);
6475 * Don't waste time/code checking return status of link_tswitches()
6476 * here. It is unlikely to fail, and the result of any failure here
6477 * will be caught elsewhere anyway.
6479 if (sw[i][j][k] && sw[ip1][j][k])
6480 link_tswitches(t, 0, sw[i][j][k], sw[ip1][j][k]);
6482 if (sw[i][jp1][k] && sw[ip1][jp1][k])
6483 link_tswitches(t, 0, sw[i][jp1][k], sw[ip1][jp1][k]);
6485 if (sw[i][j][kp1] && sw[ip1][j][kp1])
6486 link_tswitches(t, 0, sw[i][j][kp1], sw[ip1][j][kp1]);
6488 if (sw[i][jp1][kp1] && sw[ip1][jp1][kp1])
6489 link_tswitches(t, 0, sw[i][jp1][kp1], sw[ip1][jp1][kp1]);
6492 if (sw[i][j][k] && sw[i][jp1][k])
6493 link_tswitches(t, 1, sw[i][j][k], sw[i][jp1][k]);
6495 if (sw[ip1][j][k] && sw[ip1][jp1][k])
6496 link_tswitches(t, 1, sw[ip1][j][k], sw[ip1][jp1][k]);
6498 if (sw[i][j][kp1] && sw[i][jp1][kp1])
6499 link_tswitches(t, 1, sw[i][j][kp1], sw[i][jp1][kp1]);
6501 if (sw[ip1][j][kp1] && sw[ip1][jp1][kp1])
6502 link_tswitches(t, 1, sw[ip1][j][kp1], sw[ip1][jp1][kp1]);
6505 if (sw[i][j][k] && sw[i][j][kp1])
6506 link_tswitches(t, 2, sw[i][j][k], sw[i][j][kp1]);
6508 if (sw[ip1][j][k] && sw[ip1][j][kp1])
6509 link_tswitches(t, 2, sw[ip1][j][k], sw[ip1][j][kp1]);
6511 if (sw[i][jp1][k] && sw[i][jp1][kp1])
6512 link_tswitches(t, 2, sw[i][jp1][k], sw[i][jp1][kp1]);
6514 if (sw[ip1][jp1][k] && sw[ip1][jp1][kp1])
6515 link_tswitches(t, 2, sw[ip1][jp1][k], sw[ip1][jp1][kp1]);
6519 void locate_sw(struct torus *t, int i, int j, int k)
6524 i = canonicalize(i, t->x_sz);
6525 j = canonicalize(j, t->y_sz);
6526 k = canonicalize(k, t->z_sz);
6529 * By definition, if a coordinate direction is meshed, we don't
6530 * allow it to wrap to zero.
6532 if (t->flags & X_MESH) {
6533 int ip1 = canonicalize(i + 1, t->x_sz);
6537 if (t->flags & Y_MESH) {
6538 int jp1 = canonicalize(j + 1, t->y_sz);
6542 if (t->flags & Z_MESH) {
6543 int kp1 = canonicalize(k + 1, t->z_sz);
6548 * There are various reasons that the links are not installed between
6549 * known torus switches. These include cases where the search for
6550 * new switches only partially succeeds due to missing switches, and
6551 * cases where we haven't processed this position yet, but processing
6552 * of multiple independent neighbor positions has installed switches
6553 * into corners of our case.
6555 * In any event, the topology assumptions made in handling the
6556 * fingerprint for this position require that all links be installed
6557 * between installed switches for this position.
6560 check_tlinks(t, i, j, k);
6561 fp = fingerprint(t, i, j, k);
6565 * When all switches are present, we are done. Otherwise, one of
6566 * the cases below will be unsuccessful, and we'll be done also.
6568 * Note that check_tlinks() above will ensure all links that are
6569 * present are connected, in the event that all our switches are
6570 * present due to successful case handling in the surrounding
6579 * Ignore the 2D cases where there isn't enough information to uniquely
6580 * locate/place a switch into the cube.
6582 case 0x30f: /* 0 corners available */
6583 case 0x533: /* 0 corners available */
6584 case 0x655: /* 0 corners available */
6585 case 0x30e: /* 1 corner available */
6586 case 0x532: /* 1 corner available */
6587 case 0x654: /* 1 corner available */
6588 case 0x30d: /* 1 corner available */
6589 case 0x531: /* 1 corner available */
6590 case 0x651: /* 1 corner available */
6591 case 0x30b: /* 1 corner available */
6592 case 0x523: /* 1 corner available */
6593 case 0x645: /* 1 corner available */
6594 case 0x307: /* 1 corner available */
6595 case 0x513: /* 1 corner available */
6596 case 0x615: /* 1 corner available */
6599 * Handle the 2D cases with a single existing edge.
6603 success = handle_case_0x30c(t, i, j, k);
6606 success = handle_case_0x303(t, i, j, k);
6609 success = handle_case_0x305(t, i, j, k);
6612 success = handle_case_0x30a(t, i, j, k);
6615 success = handle_case_0x503(t, i, j, k);
6618 success = handle_case_0x511(t, i, j, k);
6621 success = handle_case_0x522(t, i, j, k);
6624 success = handle_case_0x530(t, i, j, k);
6627 success = handle_case_0x605(t, i, j, k);
6630 success = handle_case_0x611(t, i, j, k);
6633 success = handle_case_0x644(t, i, j, k);
6636 success = handle_case_0x650(t, i, j, k);
6639 * Handle the 2D cases where two existing edges meet at a corner.
6642 success = handle_case_0x301(t, i, j, k);
6645 success = handle_case_0x302(t, i, j, k);
6648 success = handle_case_0x304(t, i, j, k);
6651 success = handle_case_0x308(t, i, j, k);
6654 success = handle_case_0x501(t, i, j, k);
6657 success = handle_case_0x502(t, i, j, k);
6660 success = handle_case_0x520(t, i, j, k);
6663 success = handle_case_0x510(t, i, j, k);
6666 success = handle_case_0x601(t, i, j, k);
6669 success = handle_case_0x604(t, i, j, k);
6672 success = handle_case_0x610(t, i, j, k);
6675 success = handle_case_0x640(t, i, j, k);
6678 * Ignore the 3D cases where there isn't enough information to uniquely
6679 * locate/place a switch into the cube.
6681 case 0x7ff: /* 0 corners available */
6682 case 0x7fe: /* 1 corner available */
6683 case 0x7fd: /* 1 corner available */
6684 case 0x7fb: /* 1 corner available */
6685 case 0x7f7: /* 1 corner available */
6686 case 0x7ef: /* 1 corner available */
6687 case 0x7df: /* 1 corner available */
6688 case 0x7bf: /* 1 corner available */
6689 case 0x77f: /* 1 corner available */
6690 case 0x7fc: /* 2 adj corners available */
6691 case 0x7fa: /* 2 adj corners available */
6692 case 0x7f5: /* 2 adj corners available */
6693 case 0x7f3: /* 2 adj corners available */
6694 case 0x7cf: /* 2 adj corners available */
6695 case 0x7af: /* 2 adj corners available */
6696 case 0x75f: /* 2 adj corners available */
6697 case 0x73f: /* 2 adj corners available */
6698 case 0x7ee: /* 2 adj corners available */
6699 case 0x7dd: /* 2 adj corners available */
6700 case 0x7bb: /* 2 adj corners available */
6701 case 0x777: /* 2 adj corners available */
6704 * Handle the 3D cases where two existing edges meet at a corner.
6708 success = handle_case_0x71f(t, i, j, k);
6711 success = handle_case_0x72f(t, i, j, k);
6714 success = handle_case_0x737(t, i, j, k);
6717 success = handle_case_0x73b(t, i, j, k);
6720 success = handle_case_0x74f(t, i, j, k);
6723 success = handle_case_0x757(t, i, j, k);
6726 success = handle_case_0x75d(t, i, j, k);
6729 success = handle_case_0x773(t, i, j, k);
6732 success = handle_case_0x775(t, i, j, k);
6735 success = handle_case_0x78f(t, i, j, k);
6738 success = handle_case_0x7ab(t, i, j, k);
6741 success = handle_case_0x7ae(t, i, j, k);
6744 success = handle_case_0x7b3(t, i, j, k);
6747 success = handle_case_0x7ba(t, i, j, k);
6750 success = handle_case_0x7cd(t, i, j, k);
6753 success = handle_case_0x7ce(t, i, j, k);
6756 success = handle_case_0x7d5(t, i, j, k);
6759 success = handle_case_0x7dc(t, i, j, k);
6762 success = handle_case_0x7ea(t, i, j, k);
6765 success = handle_case_0x7ec(t, i, j, k);
6768 success = handle_case_0x7f1(t, i, j, k);
6771 success = handle_case_0x7f2(t, i, j, k);
6774 success = handle_case_0x7f4(t, i, j, k);
6777 success = handle_case_0x7f8(t, i, j, k);
6780 * Handle the cases where three existing edges meet at a corner.
6784 success = handle_case_0x717(t, i, j, k);
6787 success = handle_case_0x72b(t, i, j, k);
6790 success = handle_case_0x74d(t, i, j, k);
6793 success = handle_case_0x771(t, i, j, k);
6796 success = handle_case_0x78e(t, i, j, k);
6799 success = handle_case_0x7b2(t, i, j, k);
6802 success = handle_case_0x7d4(t, i, j, k);
6805 success = handle_case_0x7e8(t, i, j, k);
6808 * Handle the cases where four corners on a single face are missing.
6811 success = handle_case_0x70f(t, i, j, k);
6814 success = handle_case_0x733(t, i, j, k);
6817 success = handle_case_0x755(t, i, j, k);
6820 success = handle_case_0x7aa(t, i, j, k);
6823 success = handle_case_0x7cc(t, i, j, k);
6826 success = handle_case_0x7f0(t, i, j, k);
6829 * Handle the cases where three corners on a single face are missing.
6832 success = handle_case_0x707(t, i, j, k);
6835 success = handle_case_0x70b(t, i, j, k);
6838 success = handle_case_0x70d(t, i, j, k);
6841 success = handle_case_0x70e(t, i, j, k);
6844 success = handle_case_0x713(t, i, j, k);
6847 success = handle_case_0x715(t, i, j, k);
6850 success = handle_case_0x723(t, i, j, k);
6853 success = handle_case_0x72a(t, i, j, k);
6856 success = handle_case_0x731(t, i, j, k);
6859 success = handle_case_0x732(t, i, j, k);
6862 success = handle_case_0x745(t, i, j, k);
6865 success = handle_case_0x74c(t, i, j, k);
6868 success = handle_case_0x751(t, i, j, k);
6871 success = handle_case_0x754(t, i, j, k);
6874 success = handle_case_0x770(t, i, j, k);
6877 success = handle_case_0x78a(t, i, j, k);
6880 success = handle_case_0x78c(t, i, j, k);
6883 success = handle_case_0x7a2(t, i, j, k);
6886 success = handle_case_0x7a8(t, i, j, k);
6889 success = handle_case_0x7b0(t, i, j, k);
6892 success = handle_case_0x7c4(t, i, j, k);
6895 success = handle_case_0x7c8(t, i, j, k);
6898 success = handle_case_0x7d0(t, i, j, k);
6901 success = handle_case_0x7e0(t, i, j, k);
6904 * Handle the cases where two corners on a single edge are missing.
6907 success = handle_case_0x703(t, i, j, k);
6910 success = handle_case_0x705(t, i, j, k);
6913 success = handle_case_0x70a(t, i, j, k);
6916 success = handle_case_0x70c(t, i, j, k);
6919 success = handle_case_0x711(t, i, j, k);
6922 success = handle_case_0x722(t, i, j, k);
6925 success = handle_case_0x730(t, i, j, k);
6928 success = handle_case_0x744(t, i, j, k);
6931 success = handle_case_0x750(t, i, j, k);
6934 success = handle_case_0x788(t, i, j, k);
6937 success = handle_case_0x7a0(t, i, j, k);
6940 success = handle_case_0x7c0(t, i, j, k);
6943 * Handle the cases where a single corner is missing.
6946 success = handle_case_0x701(t, i, j, k);
6949 success = handle_case_0x702(t, i, j, k);
6952 success = handle_case_0x704(t, i, j, k);
6955 success = handle_case_0x708(t, i, j, k);
6958 success = handle_case_0x710(t, i, j, k);
6961 success = handle_case_0x720(t, i, j, k);
6964 success = handle_case_0x740(t, i, j, k);
6967 success = handle_case_0x780(t, i, j, k);
6972 * There's lots of unhandled cases still, but it's not clear
6973 * we care. Let debugging show us what they are so we can
6977 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
6978 "Unhandled fingerprint 0x%03x @ %d %d %d\n",
6983 * If we successfully handled a case, we may be able to make more
6984 * progress at this position, so try again. Otherwise, even though
6985 * we didn't successfully handle a case, we may have installed a
6986 * switch into the torus/mesh, so try to install links as well.
6987 * Then we'll have another go at the next position.
6991 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
6992 "Success on fingerprint 0x%03x @ %d %d %d\n",
6996 check_tlinks(t, i, j, k);
6998 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
6999 "Failed on fingerprint 0x%03x @ %d %d %d\n",
7006 #define LINK_ERR_STR " direction link required for topology seed configuration since radix == 4! See torus-2QoS.conf(5).\n"
7007 #define LINK_ERR2_STR " direction link required for topology seed configuration! See torus-2QoS.conf(5).\n"
7008 #define SEED_ERR_STR " direction links for topology seed do not share a common switch! See torus-2QoS.conf(5).\n"
7011 bool verify_setup(struct torus *t, struct fabric *f)
7013 struct coord_dirs *o;
7014 struct f_switch *sw;
7015 unsigned p, s, n = 0;
7016 bool success = false;
7017 bool all_sw_present, need_seed = true;
7019 if (!(t->x_sz && t->y_sz && t->z_sz)) {
7020 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7021 "ERR 4E20: missing required torus size specification!\n");
7024 if (t->osm->subn.min_sw_data_vls < 2) {
7025 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7026 "ERR 4E48: Too few data VLs to support torus routing "
7027 "without credit loops (have switchport %d need 2)\n",
7028 (int)t->osm->subn.min_sw_data_vls);
7031 if (t->osm->subn.min_sw_data_vls < 4)
7032 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7033 "Warning: Too few data VLs to support torus routing "
7034 "with a failed switch without credit loops "
7035 "(have switchport %d need 4)\n",
7036 (int)t->osm->subn.min_sw_data_vls);
7037 if (t->osm->subn.min_sw_data_vls < 8)
7038 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7039 "Warning: Too few data VLs to support torus routing "
7040 "with two QoS levels (have switchport %d need 8)\n",
7041 (int)t->osm->subn.min_sw_data_vls);
7042 if (t->osm->subn.min_data_vls < 2)
7043 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7044 "Warning: Too few data VLs to support torus routing "
7045 "with two QoS levels (have endport %d need 2)\n",
7046 (int)t->osm->subn.min_data_vls);
7048 * Be sure all the switches in the torus support the port
7049 * ordering that might have been configured.
7051 for (s = 0; s < f->switch_cnt; s++) {
7053 for (p = 0; p < sw->port_cnt; p++) {
7054 if (t->port_order[p] >= sw->port_cnt) {
7055 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7056 "ERR 4E21: port_order configured using "
7057 "port %u, but only %u ports in "
7058 "switch w/ GUID 0x%04"PRIx64"\n",
7059 t->port_order[p], sw->port_cnt - 1,
7060 cl_ntoh64(sw->n_id));
7066 * Unfortunately, there is a problem with non-unique topology for any
7067 * torus dimension which has radix four. This problem requires extra
7068 * input, in the form of specifying both the positive and negative
7069 * coordinate directions from a common switch, for any torus dimension
7070 * with radix four (see also build_torus()).
7072 * Do the checking required to ensure that the required information
7073 * is present, but more than the needed information is not required.
7075 * So, verify that we learned the coordinate directions correctly for
7076 * the fabric. The coordinate direction links get an invalid port
7077 * set on their ends when parsed.
7080 all_sw_present = true;
7083 if (t->x_sz == 4 && !(t->flags & X_MESH)) {
7084 if (o->xp_link.end[0].port >= 0) {
7085 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7086 "ERR 4E22: Positive x" LINK_ERR_STR);
7089 if (o->xm_link.end[0].port >= 0) {
7090 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7091 "ERR 4E23: Negative x" LINK_ERR_STR);
7094 if (o->xp_link.end[0].n_id != o->xm_link.end[0].n_id) {
7095 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7096 "ERR 4E24: Positive/negative x" SEED_ERR_STR);
7100 if (t->y_sz == 4 && !(t->flags & Y_MESH)) {
7101 if (o->yp_link.end[0].port >= 0) {
7102 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7103 "ERR 4E25: Positive y" LINK_ERR_STR);
7106 if (o->ym_link.end[0].port >= 0) {
7107 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7108 "ERR 4E26: Negative y" LINK_ERR_STR);
7111 if (o->yp_link.end[0].n_id != o->ym_link.end[0].n_id) {
7112 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7113 "ERR 4E27: Positive/negative y" SEED_ERR_STR);
7117 if (t->z_sz == 4 && !(t->flags & Z_MESH)) {
7118 if (o->zp_link.end[0].port >= 0) {
7119 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7120 "ERR 4E28: Positive z" LINK_ERR_STR);
7123 if (o->zm_link.end[0].port >= 0) {
7124 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7125 "ERR 4E29: Negative z" LINK_ERR_STR);
7128 if (o->zp_link.end[0].n_id != o->zm_link.end[0].n_id) {
7129 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7130 "ERR 4E2A: Positive/negative z" SEED_ERR_STR);
7135 if (o->xp_link.end[0].port >= 0 &&
7136 o->xm_link.end[0].port >= 0) {
7137 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7138 "ERR 4E2B: Positive or negative x" LINK_ERR2_STR);
7141 if (o->xp_link.end[0].port < 0 &&
7142 !find_f_sw(f, o->xp_link.end[0].n_id))
7143 all_sw_present = false;
7145 if (o->xp_link.end[1].port < 0 &&
7146 !find_f_sw(f, o->xp_link.end[1].n_id))
7147 all_sw_present = false;
7149 if (o->xm_link.end[0].port < 0 &&
7150 !find_f_sw(f, o->xm_link.end[0].n_id))
7151 all_sw_present = false;
7153 if (o->xm_link.end[1].port < 0 &&
7154 !find_f_sw(f, o->xm_link.end[1].n_id))
7155 all_sw_present = false;
7158 if (o->zp_link.end[0].port >= 0 &&
7159 o->zm_link.end[0].port >= 0) {
7160 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7161 "ERR 4E2C: Positive or negative z" LINK_ERR2_STR);
7164 if ((o->xp_link.end[0].port < 0 &&
7165 o->zp_link.end[0].port < 0 &&
7166 o->zp_link.end[0].n_id != o->xp_link.end[0].n_id) ||
7168 (o->xp_link.end[0].port < 0 &&
7169 o->zm_link.end[0].port < 0 &&
7170 o->zm_link.end[0].n_id != o->xp_link.end[0].n_id) ||
7172 (o->xm_link.end[0].port < 0 &&
7173 o->zp_link.end[0].port < 0 &&
7174 o->zp_link.end[0].n_id != o->xm_link.end[0].n_id) ||
7176 (o->xm_link.end[0].port < 0 &&
7177 o->zm_link.end[0].port < 0 &&
7178 o->zm_link.end[0].n_id != o->xm_link.end[0].n_id)) {
7180 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7181 "ERR 4E2D: x and z" SEED_ERR_STR);
7184 if (o->zp_link.end[0].port < 0 &&
7185 !find_f_sw(f, o->zp_link.end[0].n_id))
7186 all_sw_present = false;
7188 if (o->zp_link.end[1].port < 0 &&
7189 !find_f_sw(f, o->zp_link.end[1].n_id))
7190 all_sw_present = false;
7192 if (o->zm_link.end[0].port < 0 &&
7193 !find_f_sw(f, o->zm_link.end[0].n_id))
7194 all_sw_present = false;
7196 if (o->zm_link.end[1].port < 0 &&
7197 !find_f_sw(f, o->zm_link.end[1].n_id))
7198 all_sw_present = false;
7201 if (o->yp_link.end[0].port >= 0 &&
7202 o->ym_link.end[0].port >= 0) {
7203 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7204 "ERR 4E2E: Positive or negative y" LINK_ERR2_STR);
7207 if ((o->xp_link.end[0].port < 0 &&
7208 o->yp_link.end[0].port < 0 &&
7209 o->yp_link.end[0].n_id != o->xp_link.end[0].n_id) ||
7211 (o->xp_link.end[0].port < 0 &&
7212 o->ym_link.end[0].port < 0 &&
7213 o->ym_link.end[0].n_id != o->xp_link.end[0].n_id) ||
7215 (o->xm_link.end[0].port < 0 &&
7216 o->yp_link.end[0].port < 0 &&
7217 o->yp_link.end[0].n_id != o->xm_link.end[0].n_id) ||
7219 (o->xm_link.end[0].port < 0 &&
7220 o->ym_link.end[0].port < 0 &&
7221 o->ym_link.end[0].n_id != o->xm_link.end[0].n_id)) {
7223 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7224 "ERR 4E2F: x and y" SEED_ERR_STR);
7227 if (o->yp_link.end[0].port < 0 &&
7228 !find_f_sw(f, o->yp_link.end[0].n_id))
7229 all_sw_present = false;
7231 if (o->yp_link.end[1].port < 0 &&
7232 !find_f_sw(f, o->yp_link.end[1].n_id))
7233 all_sw_present = false;
7235 if (o->ym_link.end[0].port < 0 &&
7236 !find_f_sw(f, o->ym_link.end[0].n_id))
7237 all_sw_present = false;
7239 if (o->ym_link.end[1].port < 0 &&
7240 !find_f_sw(f, o->ym_link.end[1].n_id))
7241 all_sw_present = false;
7243 if (all_sw_present && need_seed) {
7247 if (++n < t->seed_cnt)
7251 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7252 "ERR 4E30: Every configured torus seed has at "
7253 "least one switch missing in fabric! See "
7254 "torus-2QoS.conf(5) and TORUS TOPOLOGY DISCOVERY "
7255 "in torus-2QoS(8)\n");
7263 bool build_torus(struct fabric *f, struct torus *t)
7269 struct coord_dirs *o;
7270 struct f_switch *fsw0, *fsw1;
7271 struct t_switch ****sw = t->sw;
7272 bool success = true;
7274 t->link_pool_sz = f->link_cnt;
7275 t->link_pool = calloc(1, t->link_pool_sz * sizeof(*t->link_pool));
7276 if (!t->link_pool) {
7277 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7278 "ERR 4E31: Allocating torus link pool: %s\n",
7285 * Get things started by locating the up to seven switches that
7286 * define the torus "seed", coordinate directions, and datelines.
7288 o = &t->seed[t->seed_idx];
7290 i = canonicalize(-o->x_dateline, t->x_sz);
7291 j = canonicalize(-o->y_dateline, t->y_sz);
7292 k = canonicalize(-o->z_dateline, t->z_sz);
7294 if (o->xp_link.end[0].port < 0) {
7295 ip1 = canonicalize(1 - o->x_dateline, t->x_sz);
7296 fsw0 = find_f_sw(f, o->xp_link.end[0].n_id);
7297 fsw1 = find_f_sw(f, o->xp_link.end[1].n_id);
7299 install_tswitch(t, i, j, k, fsw0) &&
7300 install_tswitch(t, ip1, j, k, fsw1) && success;
7302 if (o->xm_link.end[0].port < 0) {
7303 im1 = canonicalize(-1 - o->x_dateline, t->x_sz);
7304 fsw0 = find_f_sw(f, o->xm_link.end[0].n_id);
7305 fsw1 = find_f_sw(f, o->xm_link.end[1].n_id);
7307 install_tswitch(t, i, j, k, fsw0) &&
7308 install_tswitch(t, im1, j, k, fsw1) && success;
7310 if (o->yp_link.end[0].port < 0) {
7311 jp1 = canonicalize(1 - o->y_dateline, t->y_sz);
7312 fsw0 = find_f_sw(f, o->yp_link.end[0].n_id);
7313 fsw1 = find_f_sw(f, o->yp_link.end[1].n_id);
7315 install_tswitch(t, i, j, k, fsw0) &&
7316 install_tswitch(t, i, jp1, k, fsw1) && success;
7318 if (o->ym_link.end[0].port < 0) {
7319 jm1 = canonicalize(-1 - o->y_dateline, t->y_sz);
7320 fsw0 = find_f_sw(f, o->ym_link.end[0].n_id);
7321 fsw1 = find_f_sw(f, o->ym_link.end[1].n_id);
7323 install_tswitch(t, i, j, k, fsw0) &&
7324 install_tswitch(t, i, jm1, k, fsw1) && success;
7326 if (o->zp_link.end[0].port < 0) {
7327 kp1 = canonicalize(1 - o->z_dateline, t->z_sz);
7328 fsw0 = find_f_sw(f, o->zp_link.end[0].n_id);
7329 fsw1 = find_f_sw(f, o->zp_link.end[1].n_id);
7331 install_tswitch(t, i, j, k, fsw0) &&
7332 install_tswitch(t, i, j, kp1, fsw1) && success;
7334 if (o->zm_link.end[0].port < 0) {
7335 km1 = canonicalize(-1 - o->z_dateline, t->z_sz);
7336 fsw0 = find_f_sw(f, o->zm_link.end[0].n_id);
7337 fsw1 = find_f_sw(f, o->zm_link.end[1].n_id);
7339 install_tswitch(t, i, j, k, fsw0) &&
7340 install_tswitch(t, i, j, km1, fsw1) && success;
7346 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7347 "Using torus seed configured as default "
7348 "(seed sw %d,%d,%d GUID 0x%04"PRIx64").\n",
7349 i, j, k, cl_ntoh64(sw[i][j][k]->n_id));
7351 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7352 "Using torus seed configured as backup #%u "
7353 "(seed sw %d,%d,%d GUID 0x%04"PRIx64").\n",
7354 t->seed_idx, i, j, k, cl_ntoh64(sw[i][j][k]->n_id));
7357 * Search the fabric and construct the expected torus topology.
7359 * The algorithm is to consider the "cube" formed by eight switch
7360 * locations bounded by the corners i, j, k and i+1, j+1, k+1.
7361 * For each such cube look at the topology of the switches already
7362 * placed in the torus, and deduce which new switches can be placed
7363 * into their proper locations in the torus. Examine each cube
7364 * multiple times, until the number of links moved into the torus
7365 * topology does not change.
7368 nlink = t->link_cnt;
7370 for (k = 0; k < (int)t->z_sz; k++)
7371 for (j = 0; j < (int)t->y_sz; j++)
7372 for (i = 0; i < (int)t->x_sz; i++)
7373 locate_sw(t, i, j, k);
7375 if (t->link_cnt != nlink)
7379 * Move all other endpoints into torus/mesh.
7381 for (k = 0; k < (int)t->z_sz; k++)
7382 for (j = 0; j < (int)t->y_sz; j++)
7383 for (i = 0; i < (int)t->x_sz; i++)
7384 if (!link_srcsink(t, i, j, k)) {
7393 * Returns a count of differences between old and new switches.
7396 unsigned tsw_changes(struct t_switch *nsw, struct t_switch *osw)
7398 unsigned p, cnt = 0, port_cnt;
7399 struct endpoint *npt, *opt;
7400 struct endpoint *rnpt, *ropt;
7404 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7405 "New torus switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7406 nsw->i, nsw->j, nsw->k, cl_ntoh64(nsw->n_id));
7411 OSM_LOG(&osw->torus->osm->log, OSM_LOG_INFO,
7412 "Lost torus switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7413 osw->i, osw->j, osw->k, cl_ntoh64(osw->n_id));
7419 if (nsw->n_id != osw->n_id) {
7421 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7422 "Torus switch %d,%d,%d GUID "
7423 "was 0x%04"PRIx64", now 0x%04"PRIx64"\n",
7424 nsw->i, nsw->j, nsw->k,
7425 cl_ntoh64(osw->n_id), cl_ntoh64(nsw->n_id));
7428 if (nsw->port_cnt != osw->port_cnt) {
7430 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7431 "Torus switch %d,%d,%d GUID 0x%04"PRIx64" "
7432 "had %d ports, now has %d\n",
7433 nsw->i, nsw->j, nsw->k, cl_ntoh64(nsw->n_id),
7434 osw->port_cnt, nsw->port_cnt);
7436 port_cnt = nsw->port_cnt;
7437 if (port_cnt > osw->port_cnt)
7438 port_cnt = osw->port_cnt;
7440 for (p = 0; p < port_cnt; p++) {
7444 if (npt && npt->link) {
7445 if (&npt->link->end[0] == npt)
7446 rnpt = &npt->link->end[1];
7448 rnpt = &npt->link->end[0];
7452 if (opt && opt->link) {
7453 if (&opt->link->end[0] == opt)
7454 ropt = &opt->link->end[1];
7456 ropt = &opt->link->end[0];
7460 if (rnpt && !ropt) {
7462 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7463 "Torus switch %d,%d,%d GUID 0x%04"PRIx64"[%d] "
7464 "remote now %s GUID 0x%04"PRIx64"[%d], "
7466 nsw->i, nsw->j, nsw->k, cl_ntoh64(nsw->n_id),
7467 p, rnpt->type == PASSTHRU ? "sw" : "node",
7468 cl_ntoh64(rnpt->n_id), rnpt->port);
7471 if (ropt && !rnpt) {
7473 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7474 "Torus switch %d,%d,%d GUID 0x%04"PRIx64"[%d] "
7475 "remote now missing, "
7476 "was %s GUID 0x%04"PRIx64"[%d]\n",
7477 osw->i, osw->j, osw->k, cl_ntoh64(nsw->n_id),
7478 p, ropt->type == PASSTHRU ? "sw" : "node",
7479 cl_ntoh64(ropt->n_id), ropt->port);
7482 if (!(rnpt && ropt))
7485 if (rnpt->n_id != ropt->n_id) {
7487 OSM_LOG(&nsw->torus->osm->log, OSM_LOG_INFO,
7488 "Torus switch %d,%d,%d GUID 0x%04"PRIx64"[%d] "
7489 "remote now %s GUID 0x%04"PRIx64"[%d], "
7490 "was %s GUID 0x%04"PRIx64"[%d]\n",
7491 nsw->i, nsw->j, nsw->k, cl_ntoh64(nsw->n_id),
7492 p, rnpt->type == PASSTHRU ? "sw" : "node",
7493 cl_ntoh64(rnpt->n_id), rnpt->port,
7494 ropt->type == PASSTHRU ? "sw" : "node",
7495 cl_ntoh64(ropt->n_id), ropt->port);
7504 void dump_torus(struct torus *t)
7507 unsigned x_sz = t->x_sz;
7508 unsigned y_sz = t->y_sz;
7509 unsigned z_sz = t->z_sz;
7513 snprintf(path, sizeof(path), "%s/%s", t->osm->subn.opt.dump_files_dir,
7514 "opensm-torus.dump");
7515 file = fopen(path, "w");
7517 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7518 "ERR 4E47: cannot create file \'%s\'\n", path);
7522 for (k = 0; k < z_sz; k++)
7523 for (j = 0; j < y_sz; j++)
7524 for (i = 0; i < x_sz; i++)
7526 fprintf(file, "switch %u,%u,%u GUID 0x%04"
7529 cl_ntoh64(t->sw[i][j][k]->n_id),
7530 t->sw[i][j][k]->osm_switch->p_node->print_desc);
7535 void report_torus_changes(struct torus *nt, struct torus *ot)
7539 unsigned x_sz = nt->x_sz;
7540 unsigned y_sz = nt->y_sz;
7541 unsigned z_sz = nt->z_sz;
7542 unsigned max_changes = nt->max_changes;
7544 if (OSM_LOG_IS_ACTIVE_V2(&nt->osm->log, OSM_LOG_ROUTING))
7550 if (x_sz != ot->x_sz) {
7552 OSM_LOG(&nt->osm->log, OSM_LOG_INFO,
7553 "Torus x radix was %d now %d\n",
7554 ot->x_sz, nt->x_sz);
7555 if (x_sz > ot->x_sz)
7558 if (y_sz != ot->y_sz) {
7560 OSM_LOG(&nt->osm->log, OSM_LOG_INFO,
7561 "Torus y radix was %d now %d\n",
7562 ot->y_sz, nt->y_sz);
7563 if (y_sz > ot->y_sz)
7566 if (z_sz != ot->z_sz) {
7568 OSM_LOG(&nt->osm->log, OSM_LOG_INFO,
7569 "Torus z radix was %d now %d\n",
7570 ot->z_sz, nt->z_sz);
7571 if (z_sz > ot->z_sz)
7575 for (k = 0; k < z_sz; k++)
7576 for (j = 0; j < y_sz; j++)
7577 for (i = 0; i < x_sz; i++) {
7578 cnt += tsw_changes(nt->sw[i][j][k],
7581 * Booting a big fabric will cause lots of
7582 * changes as hosts come up, so don't spew.
7583 * We want to log changes to learn more about
7584 * bouncing links, etc, so they can be fixed.
7586 if (cnt > max_changes) {
7587 OSM_LOG(&nt->osm->log, OSM_LOG_INFO,
7588 "Too many torus changes; "
7589 "stopping reporting early\n");
7596 void rpt_torus_missing(struct torus *t, int i, int j, int k,
7597 struct t_switch *sw, int *missing_z)
7603 * We can have multiple missing switches without deadlock
7604 * if and only if they are adajacent in the Z direction.
7606 if ((t->switch_cnt + 1) < t->sw_pool_sz) {
7607 if (t->sw[i][j][canonicalize(k - 1, t->z_sz)] &&
7608 t->sw[i][j][canonicalize(k + 1, t->z_sz)])
7609 t->flags |= MSG_DEADLOCK;
7612 * There can be only one such Z-column of missing switches.
7615 *missing_z = i + j * t->x_sz;
7616 else if (*missing_z != i + j * t->x_sz)
7617 t->flags |= MSG_DEADLOCK;
7619 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7620 "Missing torus switch at %d,%d,%d\n", i, j, k);
7623 guid_ho = cl_ntoh64(sw->n_id);
7625 if (!(sw->ptgrp[0].port_cnt || (t->x_sz == 1) ||
7626 ((t->flags & X_MESH) && i == 0)))
7627 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7628 "Missing torus -x link on "
7629 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7631 if (!(sw->ptgrp[1].port_cnt || (t->x_sz == 1) ||
7632 ((t->flags & X_MESH) && (i + 1) == t->x_sz)))
7633 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7634 "Missing torus +x link on "
7635 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7637 if (!(sw->ptgrp[2].port_cnt || (t->y_sz == 1) ||
7638 ((t->flags & Y_MESH) && j == 0)))
7639 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7640 "Missing torus -y link on "
7641 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7643 if (!(sw->ptgrp[3].port_cnt || (t->y_sz == 1) ||
7644 ((t->flags & Y_MESH) && (j + 1) == t->y_sz)))
7645 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7646 "Missing torus +y link on "
7647 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7649 if (!(sw->ptgrp[4].port_cnt || (t->z_sz == 1) ||
7650 ((t->flags & Z_MESH) && k == 0)))
7651 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7652 "Missing torus -z link on "
7653 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7655 if (!(sw->ptgrp[5].port_cnt || (t->z_sz == 1) ||
7656 ((t->flags & Z_MESH) && (k + 1) == t->z_sz)))
7657 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7658 "Missing torus +z link on "
7659 "switch %d,%d,%d GUID 0x%04"PRIx64"\n",
7664 * Returns true if the torus can be successfully routed, false otherwise.
7667 bool routable_torus(struct torus *t, struct fabric *f)
7669 int i, j, k, tmp = -1;
7670 unsigned b2g_cnt, g2b_cnt;
7671 bool success = true;
7673 t->flags &= ~MSG_DEADLOCK;
7675 if (t->link_cnt != f->link_cnt || t->switch_cnt != f->switch_cnt)
7676 OSM_LOG(&t->osm->log, OSM_LOG_INFO,
7677 "Warning: Could not construct torus using all "
7678 "known fabric switches and/or links.\n");
7680 for (k = 0; k < (int)t->z_sz; k++)
7681 for (j = 0; j < (int)t->y_sz; j++)
7682 for (i = 0; i < (int)t->x_sz; i++)
7683 rpt_torus_missing(t, i, j, k,
7684 t->sw[i][j][k], &tmp);
7686 * Check for multiple failures that create disjoint regions on a ring.
7688 for (k = 0; k < (int)t->z_sz; k++)
7689 for (j = 0; j < (int)t->y_sz; j++) {
7692 for (i = 0; i < (int)t->x_sz; i++) {
7694 if (!t->sw[i][j][k])
7697 if (!t->sw[i][j][k]->ptgrp[0].port_cnt)
7699 if (!t->sw[i][j][k]->ptgrp[1].port_cnt)
7702 if (b2g_cnt != g2b_cnt) {
7703 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7704 "ERR 4E32: strange failures in "
7705 "x ring at y=%d z=%d"
7706 " b2g_cnt %u g2b_cnt %u\n",
7707 j, k, b2g_cnt, g2b_cnt);
7711 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7712 "ERR 4E33: disjoint failures in "
7713 "x ring at y=%d z=%d\n", j, k);
7718 for (i = 0; i < (int)t->x_sz; i++)
7719 for (k = 0; k < (int)t->z_sz; k++) {
7722 for (j = 0; j < (int)t->y_sz; j++) {
7724 if (!t->sw[i][j][k])
7727 if (!t->sw[i][j][k]->ptgrp[2].port_cnt)
7729 if (!t->sw[i][j][k]->ptgrp[3].port_cnt)
7732 if (b2g_cnt != g2b_cnt) {
7733 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7734 "ERR 4E34: strange failures in "
7735 "y ring at x=%d z=%d"
7736 " b2g_cnt %u g2b_cnt %u\n",
7737 i, k, b2g_cnt, g2b_cnt);
7741 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7742 "ERR 4E35: disjoint failures in "
7743 "y ring at x=%d z=%d\n", i, k);
7748 for (j = 0; j < (int)t->y_sz; j++)
7749 for (i = 0; i < (int)t->x_sz; i++) {
7752 for (k = 0; k < (int)t->z_sz; k++) {
7754 if (!t->sw[i][j][k])
7757 if (!t->sw[i][j][k]->ptgrp[4].port_cnt)
7759 if (!t->sw[i][j][k]->ptgrp[5].port_cnt)
7762 if (b2g_cnt != g2b_cnt) {
7763 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7764 "ERR 4E36: strange failures in "
7765 "z ring at x=%d y=%d"
7766 " b2g_cnt %u g2b_cnt %u\n",
7767 i, j, b2g_cnt, g2b_cnt);
7771 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7772 "ERR 4E37: disjoint failures in "
7773 "z ring at x=%d y=%d\n", i, j);
7778 if (t->flags & MSG_DEADLOCK) {
7779 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
7780 "ERR 4E38: missing switch topology "
7781 "==> message deadlock!\n");
7788 * Use this function to re-establish the pointers between a torus endpoint
7789 * and an opensm osm_port_t.
7791 * Typically this is only needed when "opensm --ucast-cache" is used, and
7792 * a CA link bounces. When the CA port goes away, the osm_port_t object
7793 * is destroyed, invalidating the endpoint osm_port_t pointer. When the
7794 * link comes back, a new osm_port_t object is created with a NULL priv
7795 * member. Thus, when osm_get_torus_sl() is called it is missing the data
7796 * needed to do its work. Use this function to fix things up.
7799 struct endpoint *osm_port_relink_endpoint(const osm_port_t *osm_port)
7802 uint8_t port_num, r_port_num;
7803 struct t_switch *sw;
7804 struct endpoint *ep = NULL;
7805 osm_switch_t *osm_sw;
7806 osm_physp_t *osm_physp;
7807 osm_node_t *osm_node, *r_osm_node;
7810 * We need to find the torus endpoint that has the same GUID as
7811 * the osm_port. Rather than search the entire set of endpoints,
7812 * we'll try to follow pointers.
7814 osm_physp = osm_port->p_physp;
7815 osm_node = osm_port->p_node;
7816 port_num = osm_physp_get_port_num(osm_physp);
7817 node_guid = osm_node_get_node_guid(osm_node);
7819 * Switch management port?
7821 if (port_num == 0 &&
7822 osm_node_get_type(osm_node) == IB_NODE_TYPE_SWITCH) {
7824 osm_sw = osm_node->sw;
7825 if (osm_sw && osm_sw->priv) {
7827 if (sw->osm_switch == osm_sw &&
7828 sw->port[0]->n_id == node_guid) {
7836 * CA port? Try other end of link. This should also catch a
7837 * router port if it is connected to a switch.
7839 r_osm_node = osm_node_get_remote_node(osm_node, port_num, &r_port_num);
7843 osm_sw = r_osm_node->sw;
7848 if (!(sw && sw->osm_switch == osm_sw))
7851 ep = sw->port[r_port_num];
7852 if (!(ep && ep->link))
7855 if (ep->link->end[0].n_id == node_guid) {
7856 ep = &ep->link->end[0];
7859 if (ep->link->end[1].n_id == node_guid) {
7860 ep = &ep->link->end[1];
7868 * Unfortunately, we need to cast away const to rebuild the links
7869 * between the torus endpoint and the osm_port_t.
7871 * What is really needed is to check whether pr_rcv_get_path_parms()
7872 * needs its port objects to be const. If so, why, and whether
7873 * anything can be done about it.
7875 ((osm_port_t *)osm_port)->priv = ep;
7876 ep->osm_port = (osm_port_t *)osm_port;
7882 * Computing LFT entries and path SL values:
7884 * For a pristine torus, we compute LFT entries using XYZ DOR, and select
7885 * which direction to route on a ring (i.e., the 1-D torus for the coordinate
7886 * in question) based on shortest path. We compute the SL to use for the
7887 * path based on whether we crossed a dateline (where a ring coordinate
7888 * wraps to zero) for each coordinate.
7890 * When there is a link/switch failure, we want to compute LFT entries
7891 * to route around the failure, without changing the path SL. I.e., we
7892 * want the SL to reach a given destination from a given source to be
7893 * independent of the presence or number of failed components in the fabric.
7895 * In order to make this feasible, we will assume that no ring is broken
7896 * into disjoint pieces by multiple failures
7898 * We handle failure by attempting to take the long way around any ring
7899 * with connectivity interrupted by failed components, unless the path
7900 * requires a turn on a failed switch.
7902 * For paths that require a turn on a failed switch, we head towards the
7903 * failed switch, then turn when progress is blocked by a failure, using a
7904 * turn allowed under XYZ DOR. However, such a path will also require a turn
7905 * that is not a legal XYZ DOR turn, so we construct the SL2VL mapping tables
7906 * such that XYZ DOR turns use one set of VLs and ZYX DOR turns use a
7907 * separate set of VLs.
7909 * Under these rules the algorithm guarantees credit-loop-free routing for a
7910 * single failed switch, without any change in path SL values. We can also
7911 * guarantee credit-loop-free routing for failures of multiple switches, if
7912 * they are adjacent in the last DOR direction. Since we use XYZ-DOR,
7913 * that means failed switches at i,j,k and i,j,k+1 will not cause credit
7916 * These failure routing rules are intended to prevent paths that cross any
7917 * coordinate dateline twice (over and back), so we don't need to worry about
7918 * any ambiguity over which SL to use for such a case. Also, we cannot have
7919 * a ring deadlock when a ring is broken by failure and we route the long
7920 * way around, so we don't need to worry about the impact of such routing
7925 * Functions to set our SL bit encoding for routing/QoS info. Combine the
7926 * resuts of these functions with bitwise or to get final SL.
7928 * SL bits 0-2 encode whether we "looped" in a given direction
7929 * on the torus on the path from source to destination.
7931 * SL bit 3 encodes the QoS level. We only support two QoS levels.
7933 * Below we assume TORUS_MAX_DIM == 3 and 0 <= coord_dir < TORUS_MAX_DIM.
7936 unsigned sl_set_use_loop_vl(bool use_loop_vl, unsigned coord_dir)
7938 return (coord_dir < TORUS_MAX_DIM)
7939 ? ((unsigned)use_loop_vl << coord_dir) : 0;
7943 unsigned sl_set_qos(unsigned qos)
7945 return (unsigned)(!!qos) << TORUS_MAX_DIM;
7949 * Functions to crack our SL bit encoding for routing/QoS info.
7952 bool sl_get_use_loop_vl(unsigned sl, unsigned coord_dir)
7954 return (coord_dir < TORUS_MAX_DIM)
7955 ? (sl >> coord_dir) & 0x1 : false;
7959 unsigned sl_get_qos(unsigned sl)
7961 return (sl >> TORUS_MAX_DIM) & 0x1;
7965 * Functions to encode routing/QoS info into VL bits. Combine the resuts of
7966 * these functions with bitwise or to get final VL.
7968 * For interswitch links:
7969 * VL bit 0 encodes whether we need to leave on the "loop" VL.
7971 * VL bit 1 encodes whether turn is XYZ DOR or ZYX DOR. A 3d mesh/torus
7972 * has 6 turn types: x-y, y-z, x-z, y-x, z-y, z-x. The first three are
7973 * legal XYZ DOR turns, and the second three are legal ZYX DOR turns.
7974 * Straight-through (x-x, y-y, z-z) paths are legal in both DOR variants,
7975 * so we'll assign them to XYZ DOR VLs.
7977 * Note that delivery to switch-local ports (i.e. those that source/sink
7978 * traffic, rather than forwarding it) cannot cause a deadlock, so that
7979 * can also use either XYZ or ZYX DOR.
7981 * VL bit 2 encodes QoS level.
7983 * For end port links:
7984 * VL bit 0 encodes QoS level.
7986 * Note that if VL bit encodings are changed here, the available fabric VL
7987 * verification in verify_setup() needs to be updated as well.
7990 unsigned vl_set_loop_vl(bool use_loop_vl)
7996 unsigned vl_set_qos_vl(unsigned qos)
7998 return (qos & 0x1) << 2;
8002 unsigned vl_set_ca_qos_vl(unsigned qos)
8008 unsigned vl_set_turn_vl(unsigned in_coord_dir, unsigned out_coord_dir)
8012 if (in_coord_dir != TORUS_MAX_DIM &&
8013 out_coord_dir != TORUS_MAX_DIM)
8014 vl = (in_coord_dir > out_coord_dir)
8021 unsigned sl2vl_entry(struct torus *t, struct t_switch *sw,
8022 int input_pt, int output_pt, unsigned sl)
8024 unsigned id, od, vl, data_vls;
8026 if (sw && sw->port[input_pt])
8027 id = sw->port[input_pt]->pgrp->port_grp / 2;
8031 if (sw && sw->port[output_pt])
8032 od = sw->port[output_pt]->pgrp->port_grp / 2;
8037 data_vls = t->osm->subn.min_sw_data_vls;
8039 data_vls = t->osm->subn.min_data_vls;
8042 if (sw && od != TORUS_MAX_DIM) {
8044 vl |= vl_set_loop_vl(sl_get_use_loop_vl(sl, od));
8046 vl |= vl_set_turn_vl(id, od);
8048 vl |= vl_set_qos_vl(sl_get_qos(sl));
8051 vl |= vl_set_ca_qos_vl(sl_get_qos(sl));
8057 void torus_update_osm_sl2vl(void *context, osm_physp_t *osm_phys_port,
8058 uint8_t iport_num, uint8_t oport_num,
8059 ib_slvl_table_t *osm_oport_sl2vl)
8061 osm_node_t *node = osm_physp_get_node_ptr(osm_phys_port);
8062 struct torus_context *ctx = context;
8063 struct t_switch *sw = NULL;
8067 sw = node->sw->priv;
8068 if (sw && sw->osm_switch != node->sw) {
8069 osm_log_t *log = &ctx->osm->log;
8072 guid = osm_node_get_node_guid(node);
8073 OSM_LOG(log, OSM_LOG_INFO,
8074 "Note: osm_switch (GUID 0x%04"PRIx64") "
8075 "not in torus fabric description\n",
8080 for (sl = 0; sl < 16; sl++) {
8081 vl = sl2vl_entry(ctx->torus, sw, iport_num, oport_num, sl);
8082 ib_slvl_table_set(osm_oport_sl2vl, sl, vl);
8087 void torus_update_osm_vlarb(void *context, osm_physp_t *osm_phys_port,
8088 uint8_t port_num, ib_vl_arb_table_t *block,
8089 unsigned block_length, unsigned block_num)
8091 osm_node_t *node = osm_physp_get_node_ptr(osm_phys_port);
8092 struct torus_context *ctx = context;
8093 struct t_switch *sw = NULL;
8097 sw = node->sw->priv;
8098 if (sw && sw->osm_switch != node->sw) {
8099 osm_log_t *log = &ctx->osm->log;
8102 guid = osm_node_get_node_guid(node);
8103 OSM_LOG(log, OSM_LOG_INFO,
8104 "Note: osm_switch (GUID 0x%04"PRIx64") "
8105 "not in torus fabric description\n",
8112 * If osm_phys_port is a switch port that connects to a CA, then
8113 * we're using at most VL 0 (for QoS level 0) and VL 1 (for QoS
8114 * level 1). We've been passed the VLarb values for a switch
8115 * external port, so we need to fix them up to avoid unexpected
8116 * results depending on how the switch handles VLarb values for
8119 * For inter-switch links torus-2QoS uses VLs 0-3 to implement
8120 * QoS level 0, and VLs 4-7 to implement QoS level 1.
8122 * So, leave VL 0 alone, remap VL 4 to VL 1, zero out the rest,
8123 * and compress out the zero entries to the end.
8125 if (!sw || !port_num || !sw->port[port_num] ||
8126 sw->port[port_num]->pgrp->port_grp != 2 * TORUS_MAX_DIM)
8130 for (i = 0; i < block_length; i++) {
8131 switch (block->vl_entry[i].vl) {
8133 block->vl_entry[i].vl = 1;
8136 block->vl_entry[next].vl = block->vl_entry[i].vl;
8137 block->vl_entry[next].weight = block->vl_entry[i].weight;
8140 * If we didn't update vl_entry[i] in place,
8141 * fall through to zero it out.
8146 block->vl_entry[i].vl = 0;
8147 block->vl_entry[i].weight = 0;
8154 * Computes the path lengths *vl0_len and *vl1_len to get from src
8155 * to dst on a ring with count switches.
8157 * *vl0_len is the path length for a direct path; it corresponds to a path
8158 * that should be assigned to use VL0 in a switch. *vl1_len is the path
8159 * length for a path that wraps aroung the ring, i.e. where the ring index
8160 * goes from count to zero or from zero to count. It corresponds to the path
8161 * that should be assigned to use VL1 in a switch.
8164 void get_pathlen(unsigned src, unsigned dst, unsigned count,
8165 unsigned *vl0_len, unsigned *vl1_len)
8167 unsigned s, l; /* assume s < l */
8177 *vl1_len = s + count - l;
8181 * Returns a positive number if we should take the "positive" ring direction
8182 * to reach dst from src, a negative number if we should take the "negative"
8183 * ring direction, and 0 if src and dst are the same. The choice is strictly
8184 * based on which path is shorter.
8187 int ring_dir_idx(unsigned src, unsigned dst, unsigned count)
8190 unsigned vl0_len, vl1_len;
8195 get_pathlen(src, dst, count, &vl0_len, &vl1_len);
8198 r = vl0_len <= vl1_len ? 1 : -1;
8200 r = vl0_len <= vl1_len ? -1 : 1;
8206 * Returns true if the VL1 path should be used to reach src from dst on a
8207 * ring, based on which path is shorter.
8210 bool use_vl1(unsigned src, unsigned dst, unsigned count)
8212 unsigned vl0_len, vl1_len;
8214 get_pathlen(src, dst, count, &vl0_len, &vl1_len);
8216 return vl0_len <= vl1_len ? false : true;
8220 * Returns the next switch in the ring of switches along coordinate direction
8221 * cdir, in the positive ring direction if rdir is positive, and in the
8222 * negative ring direction if rdir is negative.
8224 * Returns NULL if rdir is zero, or there is no next switch.
8227 struct t_switch *ring_next_sw(struct t_switch *sw, unsigned cdir, int rdir)
8229 unsigned pt_grp, far_end = 0;
8234 * Recall that links are installed into the torus so that their 1 end
8235 * is in the "positive" coordinate direction relative to their 0 end
8236 * (see link_tswitches() and connect_tlink()). Recall also that for
8237 * interswitch links, all links in a given switch port group have the
8238 * same endpoints, so we just need to look at the first link.
8246 if (!sw->ptgrp[pt_grp].port_cnt)
8249 return sw->ptgrp[pt_grp].port[0]->link->end[far_end].sw;
8253 * Returns a positive number if we should take the "positive" ring direction
8254 * to reach dsw from ssw, a negative number if we should take the "negative"
8255 * ring direction, and 0 if src and dst are the same, or if dsw is not
8256 * reachable from ssw because the path is interrupted by failure.
8259 int ring_dir_path(struct torus *t, unsigned cdir,
8260 struct t_switch *ssw, struct t_switch *dsw)
8263 struct t_switch *sw;
8267 d = ring_dir_idx(ssw->i, dsw->i, t->x_sz);
8270 d = ring_dir_idx(ssw->j, dsw->j, t->y_sz);
8273 d = ring_dir_idx(ssw->k, dsw->k, t->z_sz);
8283 sw = ring_next_sw(sw, cdir, d);
8290 sw = ring_next_sw(sw, cdir, d);
8300 * Returns true, and sets *pt_grp to the port group index to use for the
8301 * next hop, if it is possible to make progress from ssw to dsw along the
8302 * coordinate direction cdir, taking into account whether there are
8303 * interruptions in the path.
8305 * This next hop result can be used without worrying about ring deadlocks -
8306 * if we don't choose the shortest path it is because there is a failure in
8307 * the ring, which removes the possibilility of a ring deadlock on that ring.
8310 bool next_hop_path(struct torus *t, unsigned cdir,
8311 struct t_switch *ssw, struct t_switch *dsw,
8314 struct t_switch *tsw = NULL;
8315 bool success = false;
8319 * If the path from ssw to dsw turns, this is the switch where the
8324 tsw = t->sw[dsw->i][ssw->j][ssw->k];
8327 tsw = t->sw[ssw->i][dsw->j][ssw->k];
8330 tsw = t->sw[ssw->i][ssw->j][dsw->k];
8336 d = ring_dir_path(t, cdir, ssw, tsw);
8351 * Returns true, and sets *pt_grp to the port group index to use for the
8352 * next hop, if it is possible to make progress from ssw to dsw along the
8353 * coordinate direction cdir. This decision is made strictly on a
8354 * shortest-path basis without regard for path availability.
8357 bool next_hop_idx(struct torus *t, unsigned cdir,
8358 struct t_switch *ssw, struct t_switch *dsw,
8363 bool success = false;
8367 d = ring_dir_idx(ssw->i, dsw->i, t->x_sz);
8370 d = ring_dir_idx(ssw->j, dsw->j, t->y_sz);
8373 d = ring_dir_idx(ssw->k, dsw->k, t->z_sz);
8387 if (!ssw->ptgrp[g].port_cnt)
8397 void warn_on_routing(const char *msg,
8398 struct t_switch *sw, struct t_switch *dsw)
8400 OSM_LOG(&sw->torus->osm->log, OSM_LOG_ERROR,
8401 "%s from sw 0x%04"PRIx64" (%d,%d,%d) "
8402 "to sw 0x%04"PRIx64" (%d,%d,%d)\n",
8403 msg, cl_ntoh64(sw->n_id), sw->i, sw->j, sw->k,
8404 cl_ntoh64(dsw->n_id), dsw->i, dsw->j, dsw->k);
8408 bool next_hop_x(struct torus *t,
8409 struct t_switch *ssw, struct t_switch *dsw, unsigned *pt_grp)
8411 if (t->sw[dsw->i][ssw->j][ssw->k])
8413 * The next turning switch on this path is available,
8414 * so head towards it by the shortest available path.
8416 return next_hop_path(t, 0, ssw, dsw, pt_grp);
8419 * The next turning switch on this path is not
8420 * available, so head towards it in the shortest
8423 return next_hop_idx(t, 0, ssw, dsw, pt_grp);
8427 bool next_hop_y(struct torus *t,
8428 struct t_switch *ssw, struct t_switch *dsw, unsigned *pt_grp)
8430 if (t->sw[ssw->i][dsw->j][ssw->k])
8432 * The next turning switch on this path is available,
8433 * so head towards it by the shortest available path.
8435 return next_hop_path(t, 1, ssw, dsw, pt_grp);
8438 * The next turning switch on this path is not
8439 * available, so head towards it in the shortest
8442 return next_hop_idx(t, 1, ssw, dsw, pt_grp);
8446 bool next_hop_z(struct torus *t,
8447 struct t_switch *ssw, struct t_switch *dsw, unsigned *pt_grp)
8449 return next_hop_path(t, 2, ssw, dsw, pt_grp);
8453 * Returns the port number on *sw to use to reach *dsw, or -1 if unable to
8457 int lft_port(struct torus *t,
8458 struct t_switch *sw, struct t_switch *dsw,
8459 bool update_port_cnt, bool ca)
8462 struct port_grp *pg;
8465 * The IBA does not provide a way to preserve path history for
8466 * routing decisions and VL assignment, and the only mechanism to
8467 * provide global fabric knowledge to the routing engine is via
8468 * the four SL bits. This severely constrains the ability to deal
8469 * with missing/dead switches.
8471 * Also, if routing a torus with XYZ-DOR, the only way to route
8472 * around a missing/dead switch is to introduce a turn that is
8473 * illegal under XYZ-DOR.
8475 * But here's what we can do:
8477 * We have a VL bit we use to flag illegal turns, thus putting the
8478 * hop directly after an illegal turn on a separate set of VLs.
8479 * Unfortunately, since there is no path history, the _second_
8480 * and subsequent hops after an illegal turn use the standard
8481 * XYZ-DOR VL set. This is enough to introduce credit loops in
8484 * To minimize the number of cases such illegal turns can introduce
8485 * credit loops, we try to introduce the illegal turn as late in a
8488 * Define a turning switch as a switch where a path turns from one
8489 * coordinate direction onto another. If a turning switch in a path
8490 * is missing, construct the LFT entries so that the path progresses
8491 * as far as possible on the shortest path to the turning switch.
8492 * When progress is not possible, turn onto the next coordinate
8495 * The next turn after that will be an illegal turn, after which
8496 * point the path will continue to use a standard XYZ-DOR path.
8498 if (dsw->i != sw->i) {
8500 if (next_hop_x(t, sw, dsw, &g))
8503 * This path has made as much progress in this direction as
8504 * is possible, so turn it now.
8506 if (dsw->j != sw->j && next_hop_y(t, sw, dsw, &g))
8509 if (dsw->k != sw->k && next_hop_z(t, sw, dsw, &g))
8512 warn_on_routing("Error: unable to route", sw, dsw);
8514 } else if (dsw->j != sw->j) {
8516 if (next_hop_y(t, sw, dsw, &g))
8519 if (dsw->k != sw->k && next_hop_z(t, sw, dsw, &g))
8522 warn_on_routing("Error: unable to route", sw, dsw);
8525 if (dsw->k == sw->k)
8526 warn_on_routing("Warning: bad routing", sw, dsw);
8528 if (next_hop_z(t, sw, dsw, &g))
8531 warn_on_routing("Error: unable to route", sw, dsw);
8539 if (update_port_cnt) {
8541 p = pg->ca_dlid_cnt++ % pg->port_cnt;
8543 p = pg->sw_dlid_cnt++ % pg->port_cnt;
8546 * If we're not updating port counts, then we're just running
8547 * routes for SL path checking, and it doesn't matter which
8548 * of several parallel links we use. Use the first one.
8552 p = pg->port[p]->port;
8558 * We can't get there from here.
8560 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
8561 "ERR 4E39: routing on sw 0x%04"PRIx64": sending "
8562 "traffic for dest sw 0x%04"PRIx64" to port %u\n",
8563 cl_ntoh64(sw->n_id), cl_ntoh64(dsw->n_id), OSM_NO_PATH);
8568 bool get_lid(struct port_grp *pg, unsigned p,
8569 uint16_t *dlid_base, uint8_t *dlid_lmc, bool *ca)
8571 struct endpoint *ep;
8572 osm_port_t *osm_port;
8574 if (p >= pg->port_cnt) {
8575 OSM_LOG(&pg->sw->torus->osm->log, OSM_LOG_ERROR,
8576 "ERR 4E3A: Port group index %u too large: sw "
8577 "0x%04"PRIx64" pt_grp %u pt_grp_cnt %u\n",
8578 p, cl_ntoh64(pg->sw->n_id),
8579 (unsigned)pg->port_grp, (unsigned)pg->port_cnt);
8582 if (pg->port[p]->type == SRCSINK) {
8586 } else if (pg->port[p]->type == PASSTHRU &&
8587 pg->port[p]->link->end[1].type == SRCSINK) {
8589 * If this port is connected via a link to a CA, then we
8590 * know link->end[0] is the switch end and link->end[1] is
8591 * the CA end; see build_ca_link() and link_srcsink().
8593 ep = &pg->port[p]->link->end[1];
8597 OSM_LOG(&pg->sw->torus->osm->log, OSM_LOG_ERROR,
8598 "ERR 4E3B: Switch 0x%04"PRIx64" port %d improperly connected\n",
8599 cl_ntoh64(pg->sw->n_id), pg->port[p]->port);
8602 osm_port = ep->osm_port;
8603 if (!(osm_port && osm_port->priv == ep)) {
8604 OSM_LOG(&pg->sw->torus->osm->log, OSM_LOG_ERROR,
8605 "ERR 4E3C: ep->osm_port->priv != ep "
8606 "for sw 0x%04"PRIx64" port %d\n",
8607 cl_ntoh64(((struct t_switch *)(ep->sw))->n_id), ep->port);
8610 *dlid_base = cl_ntoh16(osm_physp_get_base_lid(osm_port->p_physp));
8611 *dlid_lmc = osm_physp_get_lmc(osm_port->p_physp);
8617 bool torus_lft(struct torus *t, struct t_switch *sw)
8619 bool success = true;
8622 uint16_t l, dlid_base;
8625 struct port_grp *pgrp;
8626 struct t_switch *dsw;
8627 osm_switch_t *osm_sw;
8628 uint8_t order[IB_NODE_NUM_PORTS_MAX+1];
8630 if (!(sw->osm_switch && sw->osm_switch->priv == sw)) {
8631 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
8632 "ERR 4E3D: sw->osm_switch->priv != sw "
8633 "for sw 0x%04"PRIx64"\n", cl_ntoh64(sw->n_id));
8636 osm_sw = sw->osm_switch;
8637 memset(osm_sw->new_lft, OSM_NO_PATH, osm_sw->lft_size);
8639 for (s = 0; s < t->switch_cnt; s++) {
8641 dsw = t->sw_pool[s];
8642 pgrp = &dsw->ptgrp[2 * TORUS_MAX_DIM];
8644 memset(order, IB_INVALID_PORT_NUM, sizeof(order));
8645 for (p = 0; p < pgrp->port_cnt; p++)
8646 order[pgrp->port[p]->port] = p;
8648 for (p = 0; p < ARRAY_SIZE(order); p++) {
8650 uint8_t px = order[t->port_order[p]];
8652 if (px == IB_INVALID_PORT_NUM)
8655 if (!get_lid(pgrp, px, &dlid_base, &dlid_lmc, &ca))
8658 if (sw->n_id == dsw->n_id)
8659 dp = pgrp->port[px]->port;
8661 dp = lft_port(t, sw, dsw, true, ca);
8663 * LMC > 0 doesn't really make sense for torus-2QoS.
8664 * So, just make sure traffic gets delivered if
8665 * non-zero LMC is used.
8668 for (l = 0; l < (1U << dlid_lmc); l++)
8669 osm_sw->new_lft[dlid_base + l] = dp;
8678 osm_mtree_node_t *mcast_stree_branch(struct t_switch *sw, osm_switch_t *osm_sw,
8679 osm_mgrp_box_t *mgb, unsigned depth,
8680 unsigned *port_cnt, unsigned *max_depth)
8682 osm_mtree_node_t *mtn = NULL;
8683 osm_mcast_tbl_t *mcast_tbl, *ds_mcast_tbl;
8684 osm_node_t *ds_node;
8685 struct t_switch *ds_sw;
8686 struct port_grp *ptgrp;
8688 struct endpoint *port;
8690 unsigned mcast_fwd_ports = 0, mcast_end_ports = 0;
8694 if (osm_sw->priv != sw) {
8695 OSM_LOG(&sw->torus->osm->log, OSM_LOG_ERROR,
8696 "ERR 4E3E: osm_sw (GUID 0x%04"PRIx64") "
8697 "not in torus fabric description\n",
8698 cl_ntoh64(osm_node_get_node_guid(osm_sw->p_node)));
8701 if (!osm_switch_supports_mcast(osm_sw)) {
8702 OSM_LOG(&sw->torus->osm->log, OSM_LOG_ERROR,
8703 "ERR 4E3F: osm_sw (GUID 0x%04"PRIx64") "
8704 "does not support multicast\n",
8705 cl_ntoh64(osm_node_get_node_guid(osm_sw->p_node)));
8708 mtn = osm_mtree_node_new(osm_sw);
8710 OSM_LOG(&sw->torus->osm->log, OSM_LOG_ERROR,
8711 "ERR 4E46: Insufficient memory to build multicast tree\n");
8714 mcast_tbl = osm_switch_get_mcast_tbl_ptr(osm_sw);
8716 * Recurse to downstream switches, i.e. those closer to master
8717 * spanning tree branch tips.
8719 * Note that if there are multiple ports in this port group, i.e.,
8720 * multiple parallel links, we can pick any one of them to use for
8721 * any individual MLID without causing loops. Pick one based on MLID
8722 * for now, until someone turns up evidence we need to be smarter.
8724 * Also, it might be we got called in a window between a switch getting
8725 * removed from the fabric, and torus-2QoS getting to rebuild its
8726 * fabric representation. If that were to happen, our next hop
8727 * osm_switch pointer might be stale. Look it up via opensm's fabric
8728 * description to be sure it's not.
8730 for (g = 0; g < 2 * TORUS_MAX_DIM; g++) {
8731 ptgrp = &sw->ptgrp[g];
8732 if (!ptgrp->to_stree_tip)
8735 p = mgb->mlid % ptgrp->port_cnt;/* port # in port group */
8736 p = ptgrp->port[p]->port; /* now port # in switch */
8738 ds_node = osm_node_get_remote_node(osm_sw->p_node, p, NULL);
8739 ds_sw = ptgrp->to_stree_tip->sw;
8741 if (!(ds_node && ds_node->sw &&
8742 ds_sw->osm_switch == ds_node->sw)) {
8743 OSM_LOG(&sw->torus->osm->log, OSM_LOG_ERROR,
8744 "ERR 4E40: stale pointer to osm_sw "
8745 "(GUID 0x%04"PRIx64")\n", cl_ntoh64(ds_sw->n_id));
8748 mtn->child_array[p] =
8749 mcast_stree_branch(ds_sw, ds_node->sw, mgb,
8750 depth, port_cnt, max_depth);
8751 if (!mtn->child_array[p])
8754 osm_mcast_tbl_set(mcast_tbl, mgb->mlid, p);
8757 * Since we forward traffic for this multicast group on this
8758 * port, cause the switch on the other end of the link
8759 * to forward traffic back to us. Do it now since have at
8760 * hand the link used; otherwise it'll be hard to figure out
8761 * later, and if we get it wrong we get a MC routing loop.
8763 link = sw->port[p]->link;
8764 ds_mcast_tbl = osm_switch_get_mcast_tbl_ptr(ds_node->sw);
8766 if (&link->end[0] == sw->port[p])
8767 osm_mcast_tbl_set(ds_mcast_tbl, mgb->mlid,
8770 osm_mcast_tbl_set(ds_mcast_tbl, mgb->mlid,
8774 * Add any host ports marked as in mcast group into spanning tree.
8776 ptgrp = &sw->ptgrp[2 * TORUS_MAX_DIM];
8777 for (p = 0; p < ptgrp->port_cnt; p++) {
8778 port = ptgrp->port[p];
8781 mtn->child_array[port->port] = OSM_MTREE_LEAF;
8782 osm_mcast_tbl_set(mcast_tbl, mgb->mlid, port->port);
8786 if (!(mcast_end_ports || mcast_fwd_ports)) {
8787 osm_mtree_destroy(mtn);
8789 } else if (depth > *max_depth)
8792 *port_cnt += mcast_end_ports;
8798 osm_port_t *next_mgrp_box_port(osm_mgrp_box_t *mgb,
8799 cl_list_item_t **list_iterator,
8800 cl_map_item_t **map_iterator)
8803 osm_mcm_port_t *mcm_port;
8804 osm_port_t *osm_port = NULL;
8805 cl_map_item_t *m_item = *map_iterator;
8806 cl_list_item_t *l_item = *list_iterator;
8810 l_item = cl_qlist_head(&mgb->mgrp_list);
8811 if (l_item == cl_qlist_end(&mgb->mgrp_list)) {
8815 mgrp = cl_item_obj(l_item, mgrp, list_item);
8818 m_item = cl_qmap_head(&mgrp->mcm_port_tbl);
8819 if (m_item == cl_qmap_end(&mgrp->mcm_port_tbl)) {
8821 l_item = cl_qlist_next(l_item);
8824 mcm_port = cl_item_obj(m_item, mcm_port, map_item);
8825 m_item = cl_qmap_next(m_item);
8826 osm_port = mcm_port->port;
8828 *list_iterator = l_item;
8829 *map_iterator = m_item;
8834 ib_api_status_t torus_mcast_stree(void *context, osm_mgrp_box_t *mgb)
8836 struct torus_context *ctx = context;
8837 struct torus *t = ctx->torus;
8838 cl_map_item_t *m_item = NULL;
8839 cl_list_item_t *l_item = NULL;
8840 osm_port_t *osm_port;
8841 osm_switch_t *osm_sw;
8842 struct endpoint *port;
8843 unsigned port_cnt = 0, max_depth = 0;
8845 osm_purge_mtree(&ctx->osm->sm, mgb);
8848 * Build a spanning tree for a multicast group by first marking
8849 * the torus endpoints that are participating in the group.
8850 * Then do a depth-first search of the torus master spanning
8851 * tree to build up the spanning tree specific to this group.
8853 * Since the torus master spanning tree is constructed specifically
8854 * to guarantee that multicast will not deadlock against unicast
8855 * when they share VLs, we can be sure that any multicast group
8856 * spanning tree constructed this way has the same property.
8858 while ((osm_port = next_mgrp_box_port(mgb, &l_item, &m_item))) {
8859 port = osm_port->priv;
8860 if (!(port && port->osm_port == osm_port)) {
8861 port = osm_port_relink_endpoint(osm_port);
8864 id = osm_node_get_node_guid(osm_port->p_node);
8865 OSM_LOG(&ctx->osm->log, OSM_LOG_ERROR,
8866 "ERR 4E41: osm_port (GUID 0x%04"PRIx64") "
8867 "not in torus fabric description\n",
8873 * If this is a CA port, mark the switch port at the
8874 * other end of this port's link.
8876 * By definition, a CA port is connected to end[1] of a link,
8877 * and the switch port is end[0]. See build_ca_link() and
8881 port = &port->link->end[0];
8882 port->tmp = osm_port;
8885 * It might be we got called in a window between a switch getting
8886 * removed from the fabric, and torus-2QoS getting to rebuild its
8887 * fabric representation. If that were to happen, our
8888 * master_stree_root->osm_switch pointer might be stale. Look up
8889 * the osm_switch by GUID to be sure it's not.
8891 * Also, call into mcast_stree_branch with depth = -1, because
8892 * depth at root switch needs to be 0.
8894 osm_sw = (osm_switch_t *)cl_qmap_get(&ctx->osm->subn.sw_guid_tbl,
8895 t->master_stree_root->n_id);
8896 if (!(osm_sw && t->master_stree_root->osm_switch == osm_sw)) {
8897 OSM_LOG(&ctx->osm->log, OSM_LOG_ERROR,
8898 "ERR 4E42: stale pointer to osm_sw (GUID 0x%04"PRIx64")\n",
8899 cl_ntoh64(t->master_stree_root->n_id));
8902 mgb->root = mcast_stree_branch(t->master_stree_root, osm_sw,
8903 mgb, -1, &port_cnt, &max_depth);
8905 OSM_LOG(&ctx->osm->log, OSM_LOG_VERBOSE,
8906 "Configured MLID 0x%X for %u ports, max tree depth = %u\n",
8907 mgb->mlid, port_cnt, max_depth);
8913 bool good_xy_ring(struct torus *t, const int x, const int y, const int z)
8915 struct t_switch ****sw = t->sw;
8916 bool good_ring = true;
8919 for (x_tst = 0; x_tst < t->x_sz && good_ring; x_tst++)
8920 good_ring = sw[x_tst][y][z];
8922 for (y_tst = 0; y_tst < t->y_sz && good_ring; y_tst++)
8923 good_ring = sw[x][y_tst][z];
8929 struct t_switch *find_plane_mid(struct torus *t, const int z)
8931 int x, dx, xm = t->x_sz / 2;
8932 int y, dy, ym = t->y_sz / 2;
8933 struct t_switch ****sw = t->sw;
8935 if (good_xy_ring(t, xm, ym, z))
8936 return sw[xm][ym][z];
8938 for (dx = 1, dy = 1; dx <= xm && dy <= ym; dx++, dy++) {
8940 x = canonicalize(xm - dx, t->x_sz);
8941 y = canonicalize(ym - dy, t->y_sz);
8942 if (good_xy_ring(t, x, y, z))
8945 x = canonicalize(xm + dx, t->x_sz);
8946 y = canonicalize(ym + dy, t->y_sz);
8947 if (good_xy_ring(t, x, y, z))
8954 struct t_switch *find_stree_root(struct torus *t)
8956 int x, y, z, dz, zm = t->z_sz / 2;
8957 struct t_switch ****sw = t->sw;
8958 struct t_switch *root;
8962 * Look for a switch near the "center" (wrt. the datelines) of the
8963 * torus, as that will be the most optimum spanning tree root. Use
8964 * a search that is not exhaustive, on the theory that this routing
8965 * engine isn't useful anyway if too many switches are missing.
8967 * Also, want to pick an x-y plane with no missing switches, so that
8968 * the master spanning tree construction algorithm doesn't have to
8969 * deal with needing a turn on a missing switch.
8971 for (dz = 0; dz <= zm; dz++) {
8973 z = canonicalize(zm - dz, t->z_sz);
8975 for (y = 0; y < t->y_sz && good_plane; y++)
8976 for (x = 0; x < t->x_sz && good_plane; x++)
8977 good_plane = sw[x][y][z];
8980 root = find_plane_mid(t, z);
8987 z = canonicalize(zm + dz, t->z_sz);
8989 for (y = 0; y < t->y_sz && good_plane; y++)
8990 for (x = 0; x < t->x_sz && good_plane; x++)
8991 good_plane = sw[x][y][z];
8994 root = find_plane_mid(t, z);
9000 * Note that torus-2QoS can route a torus that is missing an entire
9001 * column (switches with x,y constant, for all z values) without
9004 * if we've reached this point, we must have a column of missing
9005 * switches, as routable_torus() would have returned false for
9006 * any other configuration of missing switches that made it through
9009 * So any switch in the mid-z plane will do as the root.
9011 root = find_plane_mid(t, zm);
9017 bool sw_in_master_stree(struct t_switch *sw)
9022 connected = sw == sw->torus->master_stree_root;
9023 for (g = 0; g < 2 * TORUS_MAX_DIM; g++)
9024 connected = connected || sw->ptgrp[g].to_stree_root;
9030 void grow_master_stree_branch(struct t_switch *root, struct t_switch *tip,
9031 unsigned to_root_pg, unsigned to_tip_pg)
9033 root->ptgrp[to_tip_pg].to_stree_tip = &tip->ptgrp[to_root_pg];
9034 tip->ptgrp[to_root_pg].to_stree_root = &root->ptgrp[to_tip_pg];
9038 void build_master_stree_branch(struct t_switch *branch_root, int cdir)
9040 struct t_switch *sw, *n_sw, *p_sw;
9041 unsigned l, idx, cnt, pg, ng;
9045 idx = branch_root->i;
9046 cnt = branch_root->torus->x_sz;
9049 idx = branch_root->j;
9050 cnt = branch_root->torus->y_sz;
9053 idx = branch_root->k;
9054 cnt = branch_root->torus->z_sz;
9060 * This algorithm intends that a spanning tree branch never crosses
9061 * a dateline unless the 1-D ring for which we're building the branch
9062 * is interrupted by failure. We need that guarantee to prevent
9063 * multicast/unicast credit loops.
9065 n_sw = branch_root; /* tip of negative cdir branch */
9066 ng = 2 * cdir; /* negative cdir port group index */
9067 p_sw = branch_root; /* tip of positive cdir branch */
9068 pg = 2 * cdir + 1; /* positive cdir port group index */
9070 for (l = idx; n_sw && l >= 1; l--) {
9071 sw = ring_next_sw(n_sw, cdir, -1);
9072 if (sw && !sw_in_master_stree(sw)) {
9073 grow_master_stree_branch(n_sw, sw, pg, ng);
9078 for (l = idx; p_sw && l < (cnt - 1); l++) {
9079 sw = ring_next_sw(p_sw, cdir, 1);
9080 if (sw && !sw_in_master_stree(sw)) {
9081 grow_master_stree_branch(p_sw, sw, ng, pg);
9089 * At least one branch couldn't grow to the dateline for this ring.
9090 * That means it is acceptable to grow the branch by crossing the
9093 for (l = 0; l < cnt; l++) {
9095 sw = ring_next_sw(n_sw, cdir, -1);
9096 if (sw && !sw_in_master_stree(sw)) {
9097 grow_master_stree_branch(n_sw, sw, pg, ng);
9103 sw = ring_next_sw(p_sw, cdir, 1);
9104 if (sw && !sw_in_master_stree(sw)) {
9105 grow_master_stree_branch(p_sw, sw, ng, pg);
9110 if (!(n_sw || p_sw))
9118 bool torus_master_stree(struct torus *t)
9121 bool success = false;
9122 struct t_switch *stree_root = find_stree_root(t);
9125 build_master_stree_branch(stree_root, 0);
9130 for (i = 0; i < t->x_sz; i++) {
9133 build_master_stree_branch(t->sw[i][j][k], 1);
9135 for (j = 0; j < t->y_sz; j++)
9137 build_master_stree_branch(t->sw[i][j][k], 2);
9139 t->master_stree_root = stree_root;
9141 * At this point we should have a master spanning tree that contains
9142 * every present switch, for all fabrics that torus-2QoS can route
9143 * without deadlocks. Make sure this is the case; otherwise warn
9144 * and return failure so we get bug reports.
9147 for (i = 0; i < t->x_sz; i++)
9148 for (j = 0; j < t->y_sz; j++)
9149 for (k = 0; k < t->z_sz; k++) {
9150 struct t_switch *sw = t->sw[i][j][k];
9151 if (!sw || sw_in_master_stree(sw))
9155 OSM_LOG(&t->osm->log, OSM_LOG_ERROR,
9156 "ERR 4E43: sw 0x%04"PRIx64" (%d,%d,%d) not in "
9157 "torus multicast master spanning tree\n",
9158 cl_ntoh64(sw->n_id), i, j, k);
9164 int route_torus(struct torus *t)
9167 bool success = true;
9169 for (s = 0; s < (int)t->switch_cnt; s++)
9170 success = torus_lft(t, t->sw_pool[s]) && success;
9172 success = success && torus_master_stree(t);
9174 return success ? 0 : -1;
9177 uint8_t torus_path_sl(void *context, uint8_t path_sl_hint,
9178 const ib_net16_t slid, const ib_net16_t dlid)
9180 struct torus_context *ctx = context;
9181 osm_opensm_t *p_osm = ctx->osm;
9182 osm_log_t *log = &p_osm->log;
9183 osm_port_t *osm_sport, *osm_dport;
9184 struct endpoint *sport, *dport;
9185 struct t_switch *ssw, *dsw;
9190 osm_sport = osm_get_port_by_lid(&p_osm->subn, slid);
9194 osm_dport = osm_get_port_by_lid(&p_osm->subn, dlid);
9198 sport = osm_sport->priv;
9199 if (!(sport && sport->osm_port == osm_sport)) {
9200 sport = osm_port_relink_endpoint(osm_sport);
9202 guid = osm_node_get_node_guid(osm_sport->p_node);
9203 OSM_LOG(log, OSM_LOG_INFO,
9204 "Note: osm_sport (GUID 0x%04"PRIx64") "
9205 "not in torus fabric description\n",
9210 dport = osm_dport->priv;
9211 if (!(dport && dport->osm_port == osm_dport)) {
9212 dport = osm_port_relink_endpoint(osm_dport);
9214 guid = osm_node_get_node_guid(osm_dport->p_node);
9215 OSM_LOG(log, OSM_LOG_INFO,
9216 "Note: osm_dport (GUID 0x%04"PRIx64") "
9217 "not in torus fabric description\n",
9223 * We're only supposed to be called for CA ports, and maybe
9224 * switch management ports.
9226 if (sport->type != SRCSINK) {
9227 guid = osm_node_get_node_guid(osm_sport->p_node);
9228 OSM_LOG(log, OSM_LOG_INFO,
9229 "Error: osm_sport (GUID 0x%04"PRIx64") "
9230 "not a data src/sink port\n", cl_ntoh64(guid));
9233 if (dport->type != SRCSINK) {
9234 guid = osm_node_get_node_guid(osm_dport->p_node);
9235 OSM_LOG(log, OSM_LOG_INFO,
9236 "Error: osm_dport (GUID 0x%04"PRIx64") "
9237 "not a data src/sink port\n", cl_ntoh64(guid));
9241 * By definition, a CA port is connected to end[1] of a link, and
9242 * the switch port is end[0]. See build_ca_link() and link_srcsink().
9245 ssw = sport->link->end[0].sw;
9250 dsw = dport->link->end[0].sw;
9256 sl = sl_set_use_loop_vl(use_vl1(ssw->i, dsw->i, t->x_sz), 0);
9257 sl |= sl_set_use_loop_vl(use_vl1(ssw->j, dsw->j, t->y_sz), 1);
9258 sl |= sl_set_use_loop_vl(use_vl1(ssw->k, dsw->k, t->z_sz), 2);
9259 sl |= sl_set_qos(sl_get_qos(path_sl_hint));
9265 void sum_vlarb_weights(const char *vlarb_str,
9266 unsigned total_weight[IB_MAX_NUM_VLS])
9268 unsigned i = 0, v, vl = 0;
9271 while (*vlarb_str && i++ < 2 * IB_NUM_VL_ARB_ELEMENTS_IN_BLOCK) {
9272 v = strtoul(vlarb_str, &end, 0);
9279 total_weight[vl] += v & 0xff;
9284 int uniform_vlarb_weight_value(unsigned *weight, unsigned count)
9286 int i, v = weight[0];
9288 for (i = 1; i < count; i++) {
9296 void check_vlarb_config(const char *vlarb_str, bool is_default,
9297 const char *str, const char *pri, osm_log_t *log)
9299 unsigned total_weight[IB_MAX_NUM_VLS] = {0,};
9301 sum_vlarb_weights(vlarb_str, total_weight);
9302 if (!(uniform_vlarb_weight_value(&total_weight[0], 4) >= 0 &&
9303 uniform_vlarb_weight_value(&total_weight[4], 4) >= 0))
9304 OSM_LOG(log, OSM_LOG_INFO,
9305 "Warning: torus-2QoS requires same VLarb weights for "
9306 "VLs 0-3; also for VLs 4-7: not true for %s "
9308 (is_default ? "default" : "configured"), str, pri);
9312 * Use this to check the qos_config for switch external ports.
9315 void check_qos_swe_config(osm_qos_options_t *opt,
9316 osm_qos_options_t *def, osm_log_t *log)
9318 const char *vlarb_str, *tstr;
9322 max_vls = def->max_vls;
9323 if (opt->max_vls > 0)
9324 max_vls = opt->max_vls;
9326 if (max_vls > 0 && max_vls < 8)
9327 OSM_LOG(log, OSM_LOG_INFO,
9328 "Warning: full torus-2QoS functionality not available "
9329 "for configured %s_max_vls = %d\n",
9330 (opt->max_vls > 0 ? "qos_swe" : "qos"), opt->max_vls);
9332 vlarb_str = opt->vlarb_high;
9336 vlarb_str = def->vlarb_high;
9340 vlarb_str = OSM_DEFAULT_QOS_VLARB_HIGH;
9343 check_vlarb_config(vlarb_str, is_default, tstr, "high", log);
9345 vlarb_str = opt->vlarb_low;
9349 vlarb_str = def->vlarb_low;
9353 vlarb_str = OSM_DEFAULT_QOS_VLARB_LOW;
9356 check_vlarb_config(vlarb_str, is_default, tstr, "low", log);
9359 OSM_LOG(log, OSM_LOG_INFO,
9360 "Warning: torus-2QoS must override configured "
9361 "qos_swe_sl2vl to generate deadlock-free routes\n");
9365 void check_ep_vlarb_config(const char *vlarb_str,
9366 bool is_default, bool is_specific,
9367 const char *str, const char *pri, osm_log_t *log)
9369 unsigned i, total_weight[IB_MAX_NUM_VLS] = {0,};
9372 sum_vlarb_weights(vlarb_str, total_weight);
9373 for (i = 2; i < 8; i++) {
9374 val += total_weight[i];
9380 OSM_LOG(log, OSM_LOG_INFO,
9381 "Warning: torus-2QoS recommends 0 VLarb weights"
9382 " for VLs 2-7 on endpoint links; not true for "
9383 " configured %s_vlarb_%s\n", str, pri);
9385 OSM_LOG(log, OSM_LOG_INFO,
9386 "Warning: torus-2QoS recommends 0 VLarb weights "
9387 "for VLs 2-7 on endpoint links; not true for %s "
9388 "qos_vlarb_%s values used for %s_vlarb_%s\n",
9389 (is_default ? "default" : "configured"), pri, str, pri);
9393 * Use this to check the qos_config for endports
9396 void check_qos_ep_config(osm_qos_options_t *opt, osm_qos_options_t *def,
9397 const char *str, osm_log_t *log)
9399 const char *vlarb_str;
9400 bool is_default, is_specific;
9403 max_vls = def->max_vls;
9404 if (opt->max_vls > 0)
9405 max_vls = opt->max_vls;
9407 if (max_vls > 0 && max_vls < 2)
9408 OSM_LOG(log, OSM_LOG_INFO,
9409 "Warning: full torus-2QoS functionality not available "
9410 "for configured %s_max_vls = %d\n",
9411 (opt->max_vls > 0 ? str : "qos"), opt->max_vls);
9413 vlarb_str = opt->vlarb_high;
9417 vlarb_str = def->vlarb_high;
9418 is_specific = false;
9421 vlarb_str = OSM_DEFAULT_QOS_VLARB_HIGH;
9424 check_ep_vlarb_config(vlarb_str, is_default, is_specific,
9427 vlarb_str = opt->vlarb_low;
9431 vlarb_str = def->vlarb_low;
9432 is_specific = false;
9435 vlarb_str = OSM_DEFAULT_QOS_VLARB_LOW;
9438 check_ep_vlarb_config(vlarb_str, is_default, is_specific,
9442 OSM_LOG(log, OSM_LOG_INFO,
9443 "Warning: torus-2QoS must override configured "
9444 "%s_sl2vl to generate deadlock-free routes\n", str);
9448 int torus_build_lfts(void *context)
9451 struct torus_context *ctx = context;
9452 struct fabric *fabric;
9453 struct torus *torus;
9455 if (!ctx->osm->subn.opt.qos) {
9456 OSM_LOG(&ctx->osm->log, OSM_LOG_ERROR,
9457 "ERR 4E44: Routing engine list contains torus-2QoS. "
9458 "Enable QoS for correct operation "
9459 "(-Q or 'qos TRUE' in opensm.conf).\n");
9463 fabric = &ctx->fabric;
9464 teardown_fabric(fabric);
9466 torus = calloc(1, sizeof(*torus));
9468 OSM_LOG(&ctx->osm->log, OSM_LOG_ERROR,
9469 "ERR 4E45: allocating torus: %s\n", strerror(errno));
9472 torus->osm = ctx->osm;
9473 fabric->osm = ctx->osm;
9475 if (!parse_config(ctx->osm->subn.opt.torus_conf_file,
9479 if (!capture_fabric(fabric))
9482 OSM_LOG(&torus->osm->log, OSM_LOG_INFO,
9483 "Found fabric w/ %d links, %d switches, %d CA ports, "
9484 "minimum data VLs: endport %d, switchport %d\n",
9485 (int)fabric->link_cnt, (int)fabric->switch_cnt,
9486 (int)fabric->ca_cnt, (int)ctx->osm->subn.min_data_vls,
9487 (int)ctx->osm->subn.min_sw_data_vls);
9489 if (!verify_setup(torus, fabric))
9492 OSM_LOG(&torus->osm->log, OSM_LOG_INFO,
9493 "Looking for %d x %d x %d %s\n",
9494 (int)torus->x_sz, (int)torus->y_sz, (int)torus->z_sz,
9495 (ALL_MESH(torus->flags) ? "mesh" : "torus"));
9497 if (!build_torus(fabric, torus)) {
9498 OSM_LOG(&torus->osm->log, OSM_LOG_ERROR, "ERR 4E57: "
9499 "build_torus finished with errors\n");
9503 OSM_LOG(&torus->osm->log, OSM_LOG_INFO,
9504 "Built %d x %d x %d %s w/ %d links, %d switches, %d CA ports\n",
9505 (int)torus->x_sz, (int)torus->y_sz, (int)torus->z_sz,
9506 (ALL_MESH(torus->flags) ? "mesh" : "torus"),
9507 (int)torus->link_cnt, (int)torus->switch_cnt,
9508 (int)torus->ca_cnt);
9510 diagnose_fabric(fabric);
9512 * Since we found some sort of torus fabric, report on any topology
9513 * changes vs. the last torus we found.
9515 if (torus->flags & NOTIFY_CHANGES)
9516 report_torus_changes(torus, ctx->torus);
9518 if (routable_torus(torus, fabric))
9519 status = route_torus(torus);
9522 if (status) { /* bad torus!! */
9524 teardown_torus(torus);
9526 osm_subn_opt_t *opt = &torus->osm->subn.opt;
9527 osm_log_t *log = &torus->osm->log;
9530 teardown_torus(ctx->torus);
9533 check_qos_swe_config(&opt->qos_swe_options, &opt->qos_options,
9536 check_qos_ep_config(&opt->qos_ca_options,
9537 &opt->qos_options, "qos_ca", log);
9538 check_qos_ep_config(&opt->qos_sw0_options,
9539 &opt->qos_options, "qos_sw0", log);
9540 check_qos_ep_config(&opt->qos_rtr_options,
9541 &opt->qos_options, "qos_rtr", log);
9543 teardown_fabric(fabric);
9547 int osm_ucast_torus2QoS_setup(struct osm_routing_engine *r,
9550 struct torus_context *ctx;
9552 ctx = torus_context_create(osm);
9557 r->ucast_build_fwd_tables = torus_build_lfts;
9558 r->build_lid_matrices = ucast_dummy_build_lid_matrices;
9559 r->update_sl2vl = torus_update_osm_sl2vl;
9560 r->update_vlarb = torus_update_osm_vlarb;
9561 r->path_sl = torus_path_sl;
9562 r->mcast_build_stree = torus_mcast_stree;
9563 r->destroy = torus_context_delete;