2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
42 struct lan *ports[STP_MAX_PORTS];
46 struct bpdu rxq[RXQ_SIZE];
47 int rxq_head, rxq_tail;
51 struct bridge *bridge;
59 struct lan_conn conns[16];
64 struct bridge *bridges[16];
70 static const char *file_name;
71 static int line_number;
72 static char line[128];
73 static char *pos, *token;
74 static int n_warnings;
76 static struct test_case *
79 struct test_case *tc = xmalloc(sizeof *tc);
86 send_bpdu(struct ofpbuf *pkt, int port_no, void *b_)
88 struct bridge *b = b_;
91 assert(port_no < b->n_ports);
92 lan = b->ports[port_no];
94 const void *data = pkt->l3;
95 size_t size = (char *) ofpbuf_tail(pkt) - (char *) data;
98 for (i = 0; i < lan->n_conns; i++) {
99 struct lan_conn *conn = &lan->conns[i];
100 if (conn->bridge != b || conn->port_no != port_no) {
101 struct bridge *dst = conn->bridge;
102 struct bpdu *bpdu = &dst->rxq[dst->rxq_head++ % RXQ_SIZE];
103 assert(dst->rxq_head - dst->rxq_tail <= RXQ_SIZE);
104 bpdu->data = xmemdup(data, size);
106 bpdu->port_no = conn->port_no;
113 static struct bridge *
114 new_bridge(struct test_case *tc, int id)
116 struct bridge *b = xmalloc(sizeof *b);
120 snprintf(name, sizeof name, "stp%x", id);
121 b->stp = stp_create(name, id, send_bpdu, b);
122 assert(tc->n_bridges < ARRAY_SIZE(tc->bridges));
124 b->rxq_head = b->rxq_tail = 0;
125 tc->bridges[tc->n_bridges++] = b;
130 new_lan(struct test_case *tc, const char *name)
132 struct lan *lan = xmalloc(sizeof *lan);
134 lan->name = xstrdup(name);
136 assert(tc->n_lans < ARRAY_SIZE(tc->lans));
137 tc->lans[tc->n_lans++] = lan;
142 reconnect_port(struct bridge *b, int port_no, struct lan *new_lan)
147 assert(port_no < b->n_ports);
148 old_lan = b->ports[port_no];
149 if (old_lan == new_lan) {
153 /* Disconnect from old_lan. */
155 for (j = 0; j < old_lan->n_conns; j++) {
156 struct lan_conn *c = &old_lan->conns[j];
157 if (c->bridge == b && c->port_no == port_no) {
158 memmove(c, c + 1, sizeof *c * (old_lan->n_conns - j - 1));
165 /* Connect to new_lan. */
166 b->ports[port_no] = new_lan;
168 int conn_no = new_lan->n_conns++;
169 assert(conn_no < ARRAY_SIZE(new_lan->conns));
170 new_lan->conns[conn_no].bridge = b;
171 new_lan->conns[conn_no].port_no = port_no;
176 new_port(struct bridge *b, struct lan *lan, int path_cost)
178 int port_no = b->n_ports++;
179 struct stp_port *p = stp_get_port(b->stp, port_no);
180 assert(port_no < ARRAY_SIZE(b->ports));
181 b->ports[port_no] = NULL;
182 stp_port_set_path_cost(p, path_cost);
184 reconnect_port(b, port_no, lan);
188 dump(struct test_case *tc)
192 for (i = 0; i < tc->n_bridges; i++) {
193 struct bridge *b = tc->bridges[i];
194 struct stp *stp = b->stp;
197 printf("%s:", stp_get_name(stp));
198 if (stp_is_root_bridge(stp)) {
202 for (j = 0; j < b->n_ports; j++) {
203 struct stp_port *p = stp_get_port(stp, j);
204 enum stp_state state = stp_port_get_state(p);
206 printf("\tport %d", j);
208 printf(" (lan %s)", b->ports[j]->name);
210 printf(" (disconnected)");
212 printf(": %s", stp_state_name(state));
213 if (p == stp_get_root_port(stp)) {
214 printf(" (root port, root_path_cost=%u)", stp_get_root_path_cost(stp));
221 static void dump_lan_tree(struct test_case *, struct lan *, int level);
224 dump_bridge_tree(struct test_case *tc, struct bridge *b, int level)
232 for (i = 0; i < level; i++) {
235 printf("%s\n", stp_get_name(b->stp));
236 for (i = 0; i < b->n_ports; i++) {
237 struct lan *lan = b->ports[i];
238 struct stp_port *p = stp_get_port(b->stp, i);
239 if (stp_port_get_state(p) == STP_FORWARDING && lan) {
240 dump_lan_tree(tc, lan, level + 1);
246 dump_lan_tree(struct test_case *tc, struct lan *lan, int level)
254 for (i = 0; i < level; i++) {
257 printf("%s\n", lan->name);
258 for (i = 0; i < lan->n_conns; i++) {
259 struct bridge *b = lan->conns[i].bridge;
260 dump_bridge_tree(tc, b, level + 1);
265 tree(struct test_case *tc)
269 for (i = 0; i < tc->n_bridges; i++) {
270 struct bridge *b = tc->bridges[i];
273 for (i = 0; i < tc->n_lans; i++) {
274 struct lan *lan = tc->lans[i];
275 lan->reached = false;
277 for (i = 0; i < tc->n_bridges; i++) {
278 struct bridge *b = tc->bridges[i];
279 struct stp *stp = b->stp;
280 if (stp_is_root_bridge(stp)) {
281 dump_bridge_tree(tc, b, 0);
287 simulate(struct test_case *tc, int granularity)
291 for (time = 0; time < 1000 * 180; time += granularity) {
295 for (i = 0; i < tc->n_bridges; i++) {
296 stp_tick(tc->bridges[i]->stp, granularity);
298 for (round_trips = 0; round_trips < granularity; round_trips++) {
300 for (i = 0; i < tc->n_bridges; i++) {
301 struct bridge *b = tc->bridges[i];
302 for (; b->rxq_tail != b->rxq_head; b->rxq_tail++) {
303 struct bpdu *bpdu = &b->rxq[b->rxq_tail % RXQ_SIZE];
304 stp_received_bpdu(stp_get_port(b->stp, bpdu->port_no),
305 bpdu->data, bpdu->size);
318 err(const char *message, ...)
323 err(const char *message, ...)
327 fprintf(stderr, "%s:%d:%td: ", file_name, line_number, pos - line);
328 va_start(args, message);
329 vfprintf(stderr, message, args);
337 warn(const char *message, ...)
341 warn(const char *message, ...)
345 fprintf(stderr, "%s:%d: ", file_name, line_number);
346 va_start(args, message);
347 vfprintf(stderr, message, args);
359 while (isspace((unsigned char) *pos)) {
369 if (isalpha((unsigned char) *pos)) {
370 while (isalpha((unsigned char) *++pos)) {
373 } else if (isdigit((unsigned char) *pos)) {
374 if (*pos == '0' && (pos[1] == 'x' || pos[1] == 'X')) {
376 while (isxdigit((unsigned char) *pos)) {
380 while (isdigit((unsigned char) *++pos)) {
389 token = xmemdup0(start, pos - start);
396 char *save_pos = pos;
397 if (token && isdigit((unsigned char) *token)) {
398 *intp = strtol(token, NULL, 0);
408 match(const char *want)
410 if (token && !strcmp(want, token)) {
423 err("expected integer");
429 must_match(const char *want)
432 err("expected \"%s\"", want);
437 main(int argc, char *argv[])
439 struct test_case *tc;
444 ovs_fatal(0, "usage: test-stp INPUT.STP\n");
448 input_file = fopen(file_name, "r");
450 ovs_fatal(errno, "error opening \"%s\"", file_name);
453 tc = new_test_case();
454 for (i = 0; i < 26; i++) {
461 for (line_number = 1; fgets(line, sizeof line, input_file);
464 char *newline, *hash;
466 newline = strchr(line, '\n');
470 hash = strchr(line, '#');
479 if (match("bridge")) {
480 struct bridge *bridge;
481 int bridge_no, port_no;
483 bridge_no = must_get_int();
484 if (bridge_no < tc->n_bridges) {
485 bridge = tc->bridges[bridge_no];
486 } else if (bridge_no == tc->n_bridges) {
487 bridge = new_bridge(tc, must_get_int());
489 err("bridges must be numbered consecutively from 0");
492 stp_set_bridge_priority(bridge->stp, must_get_int());
496 for (port_no = 0; port_no < STP_MAX_PORTS; port_no++) {
497 struct stp_port *p = stp_get_port(bridge->stp, port_no);
498 if (!token || match("X")) {
500 } else if (match("_")) {
506 if (!strcmp(token, "0")) {
508 } else if (strlen(token) == 1
509 && islower((unsigned char)*token)) {
510 lan = tc->lans[*token - 'a'];
512 err("%s is not a valid LAN name "
513 "(0 or a lowercase letter)", token);
517 path_cost = match(":") ? must_get_int() : 10;
518 if (port_no < bridge->n_ports) {
519 stp_port_set_path_cost(p, path_cost);
521 reconnect_port(bridge, port_no, lan);
522 } else if (port_no == bridge->n_ports) {
523 new_port(bridge, lan, path_cost);
525 err("ports must be numbered consecutively");
528 stp_port_set_priority(p, must_get_int());
533 } else if (match("run")) {
534 simulate(tc, must_get_int());
535 } else if (match("dump")) {
537 } else if (match("tree")) {
539 } else if (match("check")) {
542 int bridge_no, port_no;
544 bridge_no = must_get_int();
545 if (bridge_no >= tc->n_bridges) {
546 err("no bridge numbered %d", bridge_no);
548 b = tc->bridges[bridge_no];
553 if (match("rootid")) {
556 rootid = must_get_int();
558 rootid |= (uint64_t) must_get_int() << 48;
560 rootid |= UINT64_C(0x8000) << 48;
562 if (stp_get_designated_root(stp) != rootid) {
563 warn("%s: root %"PRIx64", not %"PRIx64,
564 stp_get_name(stp), stp_get_designated_root(stp),
570 if (stp_get_root_path_cost(stp)) {
571 warn("%s: root path cost of root is %u but should be 0",
572 stp_get_name(stp), stp_get_root_path_cost(stp));
574 if (!stp_is_root_bridge(stp)) {
575 warn("%s: root is %"PRIx64", not %"PRIx64,
577 stp_get_designated_root(stp), stp_get_bridge_id(stp));
579 for (port_no = 0; port_no < b->n_ports; port_no++) {
580 struct stp_port *p = stp_get_port(stp, port_no);
581 enum stp_state state = stp_port_get_state(p);
582 if (!(state & (STP_DISABLED | STP_FORWARDING))) {
583 warn("%s: root port %d in state %s",
584 stp_get_name(b->stp), port_no,
585 stp_state_name(state));
589 for (port_no = 0; port_no < STP_MAX_PORTS; port_no++) {
590 struct stp_port *p = stp_get_port(stp, port_no);
591 enum stp_state state;
592 if (token == NULL || match("D")) {
593 state = STP_DISABLED;
594 } else if (match("B")) {
595 state = STP_BLOCKING;
596 } else if (match("Li")) {
597 state = STP_LISTENING;
598 } else if (match("Le")) {
599 state = STP_LEARNING;
600 } else if (match("F")) {
601 state = STP_FORWARDING;
602 } else if (match("_")) {
605 err("unknown port state %s", token);
607 if (stp_port_get_state(p) != state) {
608 warn("%s port %d: state is %s but should be %s",
609 stp_get_name(stp), port_no,
610 stp_state_name(stp_port_get_state(p)),
611 stp_state_name(state));
613 if (state == STP_FORWARDING) {
614 struct stp_port *root_port = stp_get_root_port(stp);
616 int root_path_cost = must_get_int();
617 if (p != root_port) {
618 warn("%s: port %d is not the root port",
619 stp_get_name(stp), port_no);
621 warn("%s: (there is no root port)",
624 warn("%s: (port %d is the root port)",
626 stp_port_no(root_port));
628 } else if (root_path_cost
629 != stp_get_root_path_cost(stp)) {
630 warn("%s: root path cost is %u, should be %d",
632 stp_get_root_path_cost(stp),
635 } else if (p == root_port) {
636 warn("%s: port %d is the root port but "
637 "not expected to be",
638 stp_get_name(stp), port_no);
648 err("trailing garbage on line");
653 for (i = 0; i < tc->n_lans; i++) {
654 struct lan *lan = tc->lans[i];
655 free((char *) lan->name);
658 for (i = 0; i < tc->n_bridges; i++) {
659 struct bridge *bridge = tc->bridges[i];
660 stp_destroy(bridge->stp);