Rapid Spanning Tree Protocol (IEEE 802.1D).
[cascardo/ovs.git] / lib / rstp.h
1 /*
2  * Copyright (c) 2011-2014 M3S, Srl - Italy
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * Rapid Spanning Tree Protocol (IEEE 802.1D-2004) public interface (header
19  * file).
20  *
21  * Authors:
22  *         Martino Fornasa <mf@fornasa.it>
23  *         Daniele Venturino <daniele.venturino@m3s.it>
24  *
25  * References to IEEE 802.1D-2004 standard are enclosed in square brackets.
26  * E.g. [17.3], [Table 17-1], etc.
27  *
28  */
29
30 #ifndef RSTP_H
31 #define RSTP_H 1
32
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include "compiler.h"
36 #include "util.h"
37
38 #define RSTP_MAX_PORTS 4095
39
40 struct ofpbuf;
41
42 /* Bridge priority defaults [Table 17-2] */
43 #define RSTP_MIN_PRIORITY 0
44 #define RSTP_MAX_PRIORITY 61440
45 #define RSTP_PRIORITY_STEP 4096
46 #define RSTP_DEFAULT_PRIORITY 32768
47
48 /* Port priority defaults [Table 17-2] */
49 #define RSTP_MIN_PORT_PRIORITY 0
50 #define RSTP_MAX_PORT_PRIORITY 240
51 #define RSTP_STEP_PORT_PRIORITY 16
52 #define RSTP_DEFAULT_PORT_PRIORITY 128
53
54 /* Performance parameters defaults. [Table 7-5] and [Table 17-1]
55  * These values are expressed in seconds.
56  */
57 #define RSTP_DEFAULT_AGEING_TIME 300
58 #define RSTP_MIN_AGEING_TIME 10
59 #define RSTP_MAX_AGEING_TIME 1000000
60
61 #define RSTP_DEFAULT_BRIDGE_MAX_AGE 20
62 #define RSTP_MIN_BRIDGE_MAX_AGE 6
63 #define RSTP_MAX_BRIDGE_MAX_AGE 40
64
65 #define RSTP_DEFAULT_BRIDGE_FORWARD_DELAY 15
66 #define RSTP_MIN_BRIDGE_FORWARD_DELAY 4
67 #define RSTP_MAX_BRIDGE_FORWARD_DELAY 30
68
69 #define RSTP_DEFAULT_TRANSMIT_HOLD_COUNT 6
70 #define RSTP_MIN_TRANSMIT_HOLD_COUNT 1
71 #define RSTP_MAX_TRANSMIT_HOLD_COUNT 10
72
73 #define RSTP_BRIDGE_HELLO_TIME 2 /* Value is fixed [Table 17-1] */
74
75 #define RSTP_MIGRATE_TIME 3  /* Value is fixed [Table 17-1] */
76
77 /* Port path cost [Table 17-3] */
78 #define RSTP_MIN_PORT_PATH_COST 1
79 #define RSTP_MAX_PORT_PATH_COST 200000000
80 #define RSTP_DEFAULT_PORT_PATH_COST 200000
81
82 /* RSTP Bridge identifier [9.2.5].  Top four most significant bits are a
83  * priority value. The next most significant twelve bits are a locally
84  * assigned system ID extension. Bottom 48 bits are MAC address of bridge.
85  */
86 typedef uint64_t rstp_identifier;
87
88 #define RSTP_ID_FMT "%01"PRIx8".%03"PRIx16".%012"PRIx64
89 #define RSTP_ID_ARGS(rstp_id) \
90     (uint8_t)((rstp_id) >> 60), \
91     (uint16_t)(((rstp_id) & 0x0fff000000000000ULL) >> 48), \
92     (uint64_t)((rstp_id) & 0xffffffffffffULL)
93
94 #define RSTP_PORT_ID_FMT "%04"PRIx16
95
96 enum rstp_state {
97     RSTP_DISABLED,
98     RSTP_LEARNING,
99     RSTP_FORWARDING,
100     RSTP_DISCARDING
101 };
102
103 /* Force Protocol Version [17.13.4] */
104 enum rstp_force_protocol_version {
105     FPV_STP_COMPATIBILITY = 0,
106     FPV_DEFAULT = 2
107 };
108
109 enum rstp_port_role {
110     ROLE_ROOT,
111     ROLE_DESIGNATED,
112     ROLE_ALTERNATE,
113     ROLE_BACKUP,
114     ROLE_DISABLED
115 };
116
117 struct rstp;
118 struct rstp_port;
119 struct ofproto_rstp_settings;
120
121 const char *rstp_state_name(enum rstp_state);
122 bool rstp_forward_in_state(enum rstp_state);
123 bool rstp_learn_in_state(enum rstp_state);
124 bool rstp_should_manage_bpdu(enum rstp_state state);
125 const char *rstp_port_role_name(enum rstp_port_role);
126
127 void rstp_init(void);
128
129 struct rstp * rstp_create(const char *, rstp_identifier bridge_id,
130         void (*send_bpdu)(struct ofpbuf *, int port_no, void *),
131                 void *);
132 struct rstp *rstp_ref(struct rstp *);
133 void rstp_unref(struct rstp *);
134
135 /* Functions used outside RSTP, to call functions defined in
136    rstp-state-machines.h */
137 void rstp_tick_timers(struct rstp *);
138 void rstp_received_bpdu(struct rstp_port *, const void *, size_t);
139
140 bool rstp_check_and_reset_fdb_flush(struct rstp *);
141 bool rstp_get_changed_port(struct rstp *, struct rstp_port **);
142 void rstp_port_set_mac_operational(struct rstp_port *,
143                                    bool  new_mac_operational);
144 bool rstp_port_get_mac_operational(struct rstp_port *);
145
146 /* Bridge setters */
147 void rstp_set_bridge_address(struct rstp *, rstp_identifier bridge_address);
148 void rstp_set_bridge_priority(struct rstp *, int new_priority);
149 void rstp_set_bridge_ageing_time(struct rstp *, int new_ageing_time);
150 void rstp_set_bridge_force_protocol_version(struct rstp *,
151                 enum rstp_force_protocol_version new_force_protocol_version);
152 void rstp_set_bridge_hello_time(struct rstp *);
153 void rstp_set_bridge_max_age(struct rstp *, int new_max_age);
154 void rstp_set_bridge_forward_delay(struct rstp *, int new_forward_delay);
155 void rstp_set_bridge_transmit_hold_count(struct rstp *,
156                                         int new_transmit_hold_count);
157 void rstp_set_bridge_migrate_time(struct rstp *);
158 void rstp_set_bridge_times(struct rstp *, int new_forward_delay,
159                            int new_hello_time, int new_max_age,
160                            int new_message_age);
161
162 struct rstp_port * rstp_add_port(struct rstp *);
163 void reinitialize_port(struct rstp_port *p);
164 void rstp_delete_port(struct rstp_port *);
165 /* Port setters */
166 void rstp_port_set_priority(struct rstp_port *, int new_port_priority);
167 void rstp_port_set_port_number(struct rstp_port *, uint16_t new_port_number);
168 uint32_t rstp_convert_speed_to_cost(unsigned int speed);
169 void rstp_port_set_path_cost(struct rstp_port *, uint32_t new_port_path_cost);
170 void rstp_port_set_admin_edge(struct rstp_port *, bool new_admin_edge);
171 void rstp_port_set_auto_edge(struct rstp_port *, bool new_auto_edge);
172 void rstp_port_set_state(struct rstp_port *, enum rstp_state new_state);
173 void rstp_port_set_aux(struct rstp_port *, void *aux);
174 void rstp_port_set_administrative_bridge_port(struct rstp_port *, uint8_t);
175 void rstp_port_set_oper_point_to_point_mac(struct rstp_port *, uint8_t);
176 void rstp_port_set_mcheck(struct rstp_port *, bool new_mcheck);
177
178 /* Bridge getters */
179 const char * rstp_get_name(const struct rstp *);
180 rstp_identifier rstp_get_root_id(const struct rstp *);
181 rstp_identifier rstp_get_bridge_id(const struct rstp *);
182 rstp_identifier rstp_get_designated_id(const struct rstp *);
183 uint32_t rstp_get_root_path_cost(const struct rstp *);
184 uint16_t rstp_get_designated_port_id(const struct rstp *);
185 uint16_t rstp_get_bridge_port_id(const struct rstp *);
186 struct rstp_port * rstp_get_root_port(struct rstp *);
187 rstp_identifier rstp_get_designated_root(const struct rstp *);
188 bool rstp_is_root_bridge(const struct rstp *);
189
190 /* Port getters */
191 int rstp_port_number(const struct rstp_port *);
192 struct rstp_port *rstp_get_port(struct rstp *, int port_no);
193 uint16_t rstp_port_get_id(const struct rstp_port *);
194 enum rstp_state rstp_port_get_state(const struct rstp_port *);
195 enum rstp_port_role rstp_port_get_role(const struct rstp_port *);
196 void rstp_port_get_counts(const struct rstp_port *, int *tx_count,
197                           int *rx_count, int *error_count, int *uptime);
198 void * rstp_port_get_aux(struct rstp_port *);
199 #endif /* rstp.h */