Add back 802.1D Spanning Tree Protocol (STP) library code.
[sliver-openvswitch.git] / lib / stp.h
1 /*
2  * Copyright (c) 2008, 2011 Nicira Networks.
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 #ifndef STP_H
18 #define STP_H 1
19
20 /* This is an implementation of Spanning Tree Protocol as described in IEEE
21  * 802.1D-1998, clauses 8 and 9.  Section numbers refer to this standard.  */
22
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include "compiler.h"
26 #include "util.h"
27
28 struct ofpbuf;
29
30 /* LLC field values used for STP frames. */
31 #define STP_LLC_SSAP 0x42
32 #define STP_LLC_DSAP 0x42
33 #define STP_LLC_CNTL 0x03
34
35 /* Bridge and port priorities that should be used by default. */
36 #define STP_DEFAULT_BRIDGE_PRIORITY 32768
37 #define STP_DEFAULT_PORT_PRIORITY 128
38
39 /* Bridge identifier.  Top 16 bits are a priority value (numerically lower
40  * values are higher priorities).  Bottom 48 bits are MAC address of bridge. */
41 typedef uint64_t stp_identifier;
42
43 /* Basic STP functionality. */
44 #define STP_MAX_PORTS 255
45 struct stp *stp_create(const char *name, stp_identifier bridge_id,
46                        void (*send_bpdu)(struct ofpbuf *bpdu, int port_no,
47                                          void *aux),
48                        void *aux);
49 void stp_destroy(struct stp *);
50 void stp_tick(struct stp *, int ms);
51 void stp_set_bridge_id(struct stp *, stp_identifier bridge_id);
52 void stp_set_bridge_priority(struct stp *, uint16_t new_priority);
53 void stp_set_hello_time(struct stp *, int ms);
54 void stp_set_max_age(struct stp *, int ms);
55 void stp_set_forward_delay(struct stp *, int ms);
56
57 /* STP properties. */
58 const char *stp_get_name(const struct stp *);
59 stp_identifier stp_get_bridge_id(const struct stp *);
60 stp_identifier stp_get_designated_root(const struct stp *);
61 bool stp_is_root_bridge(const struct stp *);
62 int stp_get_root_path_cost(const struct stp *);
63 int stp_get_hello_time(const struct stp *);
64 int stp_get_max_age(const struct stp *);
65 int stp_get_forward_delay(const struct stp *);
66
67 /* Obtaining STP ports. */
68 struct stp_port *stp_get_port(struct stp *, int port_no);
69 struct stp_port *stp_get_root_port(struct stp *);
70 bool stp_get_changed_port(struct stp *, struct stp_port **portp);
71
72 /* State of an STP port.
73  *
74  * A port is in exactly one state at any given time, but distinct bits are used
75  * for states to allow testing for more than one state with a bit mask. */
76 enum stp_state {
77     STP_DISABLED = 1 << 0,       /* 8.4.5: Disabled by management. */
78     STP_LISTENING = 1 << 1,      /* 8.4.2: Not learning or relaying frames. */
79     STP_LEARNING = 1 << 2,       /* 8.4.3: Learning but not relaying frames. */
80     STP_FORWARDING = 1 << 3,     /* 8.4.4: Learning and relaying frames. */
81     STP_BLOCKING = 1 << 4        /* 8.4.1: Initial boot state. */
82 };
83 const char *stp_state_name(enum stp_state);
84 bool stp_forward_in_state(enum stp_state);
85 bool stp_learn_in_state(enum stp_state);
86
87 void stp_received_bpdu(struct stp_port *, const void *bpdu, size_t bpdu_size);
88
89 struct stp *stp_port_get_stp(struct stp_port *);
90 int stp_port_no(const struct stp_port *);
91 enum stp_state stp_port_get_state(const struct stp_port *);
92 void stp_port_enable(struct stp_port *);
93 void stp_port_disable(struct stp_port *);
94 void stp_port_set_priority(struct stp_port *, uint8_t new_priority);
95 void stp_port_set_path_cost(struct stp_port *, uint16_t path_cost);
96 void stp_port_set_speed(struct stp_port *, unsigned int speed);
97 void stp_port_enable_change_detection(struct stp_port *);
98 void stp_port_disable_change_detection(struct stp_port *);
99
100 #endif /* stp.h */