- svec_destroy(&src_ports);
- svec_destroy(&dst_ports);
- free(pfx);
-}
-\f
-/* Spanning tree protocol. */
-
-static void brstp_update_port_state(struct port *);
-
-static void
-brstp_send_bpdu(struct ofpbuf *pkt, int port_no, void *br_)
-{
- struct bridge *br = br_;
- static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
- struct iface *iface = iface_from_dp_ifidx(br, port_no);
- if (!iface) {
- VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
- br->name, port_no);
- } else if (eth_addr_is_zero(iface->mac)) {
- VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d with unknown MAC",
- br->name, port_no);
- } else {
- union ofp_action action;
- struct eth_header *eth = pkt->l2;
- flow_t flow;
-
- memcpy(eth->eth_src, iface->mac, ETH_ADDR_LEN);
-
- memset(&action, 0, sizeof action);
- action.type = htons(OFPAT_OUTPUT);
- action.output.len = htons(sizeof action);
- action.output.port = htons(port_no);
-
- flow_extract(pkt, ODPP_NONE, &flow);
- ofproto_send_packet(br->ofproto, &flow, &action, 1, pkt);
- }
- ofpbuf_delete(pkt);
-}
-
-static void
-brstp_reconfigure(struct bridge *br)
-{
- size_t i;
-
- if (!cfg_get_bool(0, "stp.%s.enabled", br->name)) {
- if (br->stp) {
- stp_destroy(br->stp);
- br->stp = NULL;
-
- bridge_flush(br);
- }
- } else {
- uint64_t bridge_address, bridge_id;
- int bridge_priority;
-
- bridge_address = cfg_get_mac(0, "stp.%s.address", br->name);
- if (!bridge_address) {
- if (br->stp) {
- bridge_address = (stp_get_bridge_id(br->stp)
- & ((UINT64_C(1) << 48) - 1));
- } else {
- uint8_t mac[ETH_ADDR_LEN];
- eth_addr_random(mac);
- bridge_address = eth_addr_to_uint64(mac);
- }
- }
-
- if (cfg_is_valid(CFG_INT | CFG_REQUIRED, "stp.%s.priority",
- br->name)) {
- bridge_priority = cfg_get_int(0, "stp.%s.priority", br->name);
- } else {
- bridge_priority = STP_DEFAULT_BRIDGE_PRIORITY;
- }
-
- bridge_id = bridge_address | ((uint64_t) bridge_priority << 48);
- if (!br->stp) {
- br->stp = stp_create(br->name, bridge_id, brstp_send_bpdu, br);
- br->stp_last_tick = time_msec();
- bridge_flush(br);
- } else {
- if (bridge_id != stp_get_bridge_id(br->stp)) {
- stp_set_bridge_id(br->stp, bridge_id);
- bridge_flush(br);
- }
- }
-
- for (i = 0; i < br->n_ports; i++) {
- struct port *p = br->ports[i];
- int dp_ifidx;
- struct stp_port *sp;
- int path_cost, priority;
- bool enable;
-
- if (!p->n_ifaces) {
- continue;
- }
- dp_ifidx = p->ifaces[0]->dp_ifidx;
- if (dp_ifidx < 0 || dp_ifidx >= STP_MAX_PORTS) {
- continue;
- }
-
- sp = stp_get_port(br->stp, dp_ifidx);
- enable = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
- "stp.%s.port.%s.enabled",
- br->name, p->name)
- || cfg_get_bool(0, "stp.%s.port.%s.enabled",
- br->name, p->name));
- if (p->is_mirror_output_port) {
- enable = false;
- }
- if (enable != (stp_port_get_state(sp) != STP_DISABLED)) {
- bridge_flush(br); /* Might not be necessary. */
- if (enable) {
- stp_port_enable(sp);
- } else {
- stp_port_disable(sp);
- }
- }
-
- path_cost = cfg_get_int(0, "stp.%s.port.%s.path-cost",
- br->name, p->name);
- stp_port_set_path_cost(sp, path_cost ? path_cost : 19 /* XXX */);
-
- priority = (cfg_is_valid(CFG_INT | CFG_REQUIRED,
- "stp.%s.port.%s.priority",
- br->name, p->name)
- ? cfg_get_int(0, "stp.%s.port.%s.priority",
- br->name, p->name)
- : STP_DEFAULT_PORT_PRIORITY);
- stp_port_set_priority(sp, priority);
- }
-
- brstp_adjust_timers(br);
- }
- for (i = 0; i < br->n_ports; i++) {
- brstp_update_port_state(br->ports[i]);
- }
-}
-
-static void
-brstp_update_port_state(struct port *p)
-{
- struct bridge *br = p->bridge;
- enum stp_state state;
-
- /* Figure out new state. */
- state = STP_DISABLED;
- if (br->stp && p->n_ifaces > 0) {
- int dp_ifidx = p->ifaces[0]->dp_ifidx;
- if (dp_ifidx >= 0 && dp_ifidx < STP_MAX_PORTS) {
- state = stp_port_get_state(stp_get_port(br->stp, dp_ifidx));
- }
- }
-
- /* Update state. */
- if (p->stp_state != state) {
- static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
- VLOG_INFO_RL(&rl, "port %s: STP state changed from %s to %s",
- p->name, stp_state_name(p->stp_state),
- stp_state_name(state));
- if (p->stp_state == STP_DISABLED) {
- bridge_flush(br);
- } else {
- ofproto_revalidate(p->bridge->ofproto, p->stp_state_tag);
- }
- p->stp_state = state;
- p->stp_state_tag = (p->stp_state == STP_DISABLED ? 0
- : tag_create_random());
- }
-}
-
-static void
-brstp_adjust_timers(struct bridge *br)
-{
- int hello_time = cfg_get_int(0, "stp.%s.hello-time", br->name);
- int max_age = cfg_get_int(0, "stp.%s.max-age", br->name);
- int forward_delay = cfg_get_int(0, "stp.%s.forward-delay", br->name);
-
- stp_set_hello_time(br->stp, hello_time ? hello_time : 2000);
- stp_set_max_age(br->stp, max_age ? max_age : 20000);
- stp_set_forward_delay(br->stp, forward_delay ? forward_delay : 15000);
-}
-
-static void
-brstp_run(struct bridge *br)
-{
- if (br->stp) {
- long long int now = time_msec();
- long long int elapsed = now - br->stp_last_tick;
- struct stp_port *sp;
-
- if (elapsed > 0) {
- stp_tick(br->stp, MIN(INT_MAX, elapsed));
- br->stp_last_tick = now;
- }
- while (stp_get_changed_port(br->stp, &sp)) {
- struct port *p = port_from_dp_ifidx(br, stp_port_no(sp));
- if (p) {
- brstp_update_port_state(p);
- }
- }
- }
-}
-
-static void
-brstp_wait(struct bridge *br)
-{
- if (br->stp) {
- poll_timer_wait(1000);
- }