Merge remote-tracking branch 'ovs-dev/master'
[sliver-openvswitch.git] / lib / stp.c
index 62c2ea8..0b32cb5 100644 (file)
--- a/lib/stp.c
+++ b/lib/stp.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <assert.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include "byte-order.h"
 #include "ofpbuf.h"
 #include "packets.h"
+#include "unixctl.h"
 #include "util.h"
 #include "vlog.h"
 
@@ -39,11 +39,12 @@ VLOG_DEFINE_THIS_MODULE(stp);
 #define STP_TYPE_CONFIG 0x00
 #define STP_TYPE_TCN 0x80
 
+OVS_PACKED(
 struct stp_bpdu_header {
     ovs_be16 protocol_id;       /* STP_PROTOCOL_ID. */
     uint8_t protocol_version;   /* STP_PROTOCOL_VERSION. */
     uint8_t bpdu_type;          /* One of STP_TYPE_*. */
-} __attribute__((packed));
+});
 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
 
 enum stp_config_bpdu_flags {
@@ -51,6 +52,7 @@ enum stp_config_bpdu_flags {
     STP_CONFIG_TOPOLOGY_CHANGE = 0x01
 };
 
+OVS_PACKED(
 struct stp_config_bpdu {
     struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
     uint8_t flags;                 /* STP_CONFIG_* flags. */
@@ -62,12 +64,13 @@ struct stp_config_bpdu {
     ovs_be16 max_age;              /* 8.5.1.6: Timeout for received data. */
     ovs_be16 hello_time;           /* 8.5.1.7: Time between BPDU generation. */
     ovs_be16 forward_delay;        /* 8.5.1.8: State progression delay. */
-} __attribute__((packed));
+});
 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
 
+OVS_PACKED(
 struct stp_tcn_bpdu {
     struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
-} __attribute__((packed));
+});
 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
 
 struct stp_timer {
@@ -101,6 +104,8 @@ struct stp_port {
 };
 
 struct stp {
+    struct list node;               /* Node in all_stps list. */
+
     /* Static bridge data. */
     char *name;                     /* Human-readable name for log messages. */
     stp_identifier bridge_id;       /* 8.5.3.7: This bridge. */
@@ -131,11 +136,14 @@ struct stp {
     struct stp_port ports[STP_MAX_PORTS];
 
     /* Interface to client. */
+    bool fdb_needs_flush;          /* MAC learning tables needs flushing. */
     struct stp_port *first_changed_port;
     void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux);
     void *aux;
 };
 
+static struct list all_stps = LIST_INITIALIZER(&all_stps);
+
 #define FOR_EACH_ENABLED_PORT(PORT, STP)                        \
     for ((PORT) = stp_next_enabled_port((STP), (STP)->ports);   \
          (PORT);                                                \
@@ -145,7 +153,7 @@ stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
 {
     for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
         if (port->state != STP_DISABLED) {
-            return (struct stp_port *) port;
+            return CONST_CAST(struct stp_port *, port);
         }
     }
     return NULL;
@@ -192,13 +200,21 @@ static void stp_update_bridge_timers(struct stp *);
 
 static int clamp(int x, int min, int max);
 static int ms_to_timer(int ms);
-static int ms_to_timer_remainder(int ms);
 static int timer_to_ms(int timer);
 static void stp_start_timer(struct stp_timer *, int value);
 static void stp_stop_timer(struct stp_timer *);
 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
 
 static void stp_send_bpdu(struct stp_port *, const void *, size_t);
+static void stp_unixctl_tcn(struct unixctl_conn *, int argc,
+                            const char *argv[], void *aux);
+
+void
+stp_init(void)
+{
+    unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn,
+                             NULL);
+}
 
 /* Creates and returns a new STP instance that initially has no ports enabled.
  *
@@ -256,6 +272,7 @@ stp_create(const char *name, stp_identifier bridge_id,
         p->path_cost = 19;      /* Recommended default for 100 Mb/s link. */
         stp_initialize_port(p, STP_DISABLED);
     }
+    list_push_back(&all_stps, &stp->node);
     return stp;
 }
 
@@ -264,6 +281,7 @@ void
 stp_destroy(struct stp *stp)
 {
     if (stp) {
+        list_remove(&stp->node);
         free(stp->name);
         free(stp);
     }
@@ -281,7 +299,7 @@ stp_tick(struct stp *stp, int ms)
      * are called too frequently. */
     ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
     elapsed = ms_to_timer(ms);
-    stp->elapsed_remainder = ms_to_timer_remainder(ms);
+    stp->elapsed_remainder = ms - timer_to_ms(elapsed);
     if (!elapsed) {
         return;
     }
@@ -449,12 +467,23 @@ stp_get_forward_delay(const struct stp *stp)
     return timer_to_ms(stp->bridge_forward_delay);
 }
 
+/* Returns true if something has happened to 'stp' which necessitates flushing
+ * the client's MAC learning table.  Calling this function resets 'stp' so that
+ * future calls will return false until flushing is required again. */
+bool
+stp_check_and_reset_fdb_flush(struct stp *stp)
+{
+    bool needs_flush = stp->fdb_needs_flush;
+    stp->fdb_needs_flush = false;
+    return needs_flush;
+}
+
 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
  * STP_MAX_PORTS. */
 struct stp_port *
 stp_get_port(struct stp *stp, int port_no)
 {
-    assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
+    ovs_assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
     return &stp->ports[port_no];
 }
 
@@ -642,7 +671,7 @@ int
 stp_port_no(const struct stp_port *p)
 {
     struct stp *stp = p->stp;
-    assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
+    ovs_assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
     return p - stp->ports;
 }
 
@@ -1041,6 +1070,8 @@ stp_set_port_state(struct stp_port *p, enum stp_state state)
 static void
 stp_topology_change_detection(struct stp *stp)
 {
+    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+
     if (stp_is_root_bridge(stp)) {
         stp->topology_change = true;
         stp_start_timer(&stp->topology_change_timer, 0);
@@ -1048,7 +1079,9 @@ stp_topology_change_detection(struct stp *stp)
         stp_transmit_tcn(stp);
         stp_start_timer(&stp->tcn_timer, 0);
     }
+    stp->fdb_needs_flush = true;
     stp->topology_change_detected = true;
+    VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name);
 }
 
 static void
@@ -1096,6 +1129,9 @@ stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
                 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) {
                     stp_topology_change_acknowledged(stp);
                 }
+                if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) {
+                    stp->fdb_needs_flush = true;
+                }
             }
         } else if (stp_is_designated_port(p)) {
             stp_transmit_config(p);
@@ -1194,7 +1230,7 @@ stp_hold_timer_expiry(struct stp_port *p)
 static void
 stp_initialize_port(struct stp_port *p, enum stp_state state)
 {
-    assert(state & (STP_DISABLED | STP_BLOCKING));
+    ovs_assert(state & (STP_DISABLED | STP_BLOCKING));
     stp_become_designated_port(p);
     stp_set_port_state(p, state);
     p->topology_change_ack = false;
@@ -1253,14 +1289,6 @@ ms_to_timer(int ms)
     return ms * 0x100 / 1000;
 }
 
-/* Returns the number of leftover milliseconds when 'ms' is converted to STP
- * timer ticks. */
-static int
-ms_to_timer_remainder(int ms)
-{
-    return ms * 0x100 % 1000;
-}
-
 /* Returns the number of whole milliseconds in 'timer' STP timer ticks.  There
  * are 256 STP timer ticks per second. */
 static int
@@ -1321,3 +1349,41 @@ stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
     p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);
     p->tx_count++;
 }
+\f
+/* Unixctl. */
+
+static struct stp *
+stp_find(const char *name)
+{
+    struct stp *stp;
+
+    LIST_FOR_EACH (stp, node, &all_stps) {
+        if (!strcmp(stp->name, name)) {
+            return stp;
+        }
+    }
+    return NULL;
+}
+
+static void
+stp_unixctl_tcn(struct unixctl_conn *conn, int argc,
+                const char *argv[], void *aux OVS_UNUSED)
+{
+    if (argc > 1) {
+        struct stp *stp = stp_find(argv[1]);
+
+        if (!stp) {
+            unixctl_command_reply_error(conn, "no such stp object");
+            return;
+        }
+        stp_topology_change_detection(stp);
+    } else {
+        struct stp *stp;
+
+        LIST_FOR_EACH (stp, node, &all_stps) {
+            stp_topology_change_detection(stp);
+        }
+    }
+
+    unixctl_command_reply(conn, "OK");
+}