Modify OpenFlow commands related to ports to be more expressive.
[sliver-openvswitch.git] / secchan / secchan.c
index 9233f0d..1451cd8 100644 (file)
@@ -110,6 +110,9 @@ struct settings {
     regex_t accept_controller_regex;  /* Controller vconns to accept. */
     const char *accept_controller_re; /* String version of regex. */
     bool update_resolv_conf;          /* Update /etc/resolv.conf? */
+
+    /* Spanning tree protocol. */
+    bool enable_stp;
 };
 
 struct half {
@@ -188,10 +191,11 @@ struct port_watcher;
 static struct hook port_watcher_create(struct rconn *local,
                                        struct rconn *remote,
                                        struct port_watcher **);
-static uint32_t port_watcher_get_flags(const struct port_watcher *,
+static uint32_t port_watcher_get_config(const struct port_watcher *,
                                        int port_no);
-static void port_watcher_set_flags(struct port_watcher *,
-                                   int port_no, uint32_t flags, uint32_t mask);
+static void port_watcher_set_flags(struct port_watcher *, int port_no, 
+                                   uint32_t config, uint32_t c_mask,
+                                   uint32_t state, uint32_t s_mask);
 
 static struct hook stp_hook_create(const struct settings *,
                                    struct port_watcher *,
@@ -288,7 +292,9 @@ main(int argc, char *argv[])
 
     /* Set up hooks. */
     hooks[n_hooks++] = port_watcher_create(local_rconn, remote_rconn, &pw);
-    hooks[n_hooks++] = stp_hook_create(&s, pw, local_rconn, remote_rconn);
+    if (s.enable_stp) {
+        hooks[n_hooks++] = stp_hook_create(&s, pw, local_rconn, remote_rconn);
+    }
     if (s.in_band) {
         hooks[n_hooks++] = in_band_hook_create(&s, switch_status,
                                                remote_rconn);
@@ -586,14 +592,12 @@ relay_destroy(struct relay *r)
 \f
 /* Port status watcher. */
 
-typedef void edit_port_cb_func(struct ofp_phy_port *port, void *aux);
 typedef void port_changed_cb_func(uint16_t port_no,
                                   const struct ofp_phy_port *old,
                                   const struct ofp_phy_port *new,
                                   void *aux);
 
 struct port_watcher_cb {
-    edit_port_cb_func *edit_port;
     port_changed_cb_func *port_changed;
     void *aux;
 };
@@ -613,13 +617,16 @@ struct port_watcher {
 static int
 opp_differs(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
 {
-    BUILD_ASSERT_DECL(sizeof *a == 36); /* Trips when we add or remove fields. */
+    BUILD_ASSERT_DECL(sizeof *a == 48); /* Trips when we add or remove fields. */
     return ((a->port_no != b->port_no)
             + (memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr) != 0)
             + (memcmp(a->name, b->name, sizeof a->name) != 0)
-            + (a->flags != b->flags)
-            + (a->speed != b->speed)
-            + (a->features != b->features));
+            + (a->config != b->config)
+            + (a->state != b->state)
+            + (a->curr != b->curr)
+            + (a->advertised != b->advertised)
+            + (a->supported != b->supported)
+            + (a->peer != b->peer));
 }
 
 static void
@@ -660,18 +667,6 @@ call_port_changed_callbacks(struct port_watcher *pw, int port_no,
     }
 }
 
-static void
-call_edit_port_callbacks(struct port_watcher *pw, struct ofp_phy_port *p)
-{
-    int i;
-    for (i = 0; i < pw->n_cbs; i++) {
-        edit_port_cb_func *edit_port = pw->cbs[i].edit_port;
-        if (edit_port) {
-            (edit_port)(p, pw->cbs[i].aux); 
-        }
-    }
-}
-
 static void
 update_phy_port(struct port_watcher *pw, struct ofp_phy_port *opp,
                 uint8_t reason, bool seen[OFPP_MAX + 1])
@@ -696,7 +691,7 @@ update_phy_port(struct port_watcher *pw, struct ofp_phy_port *opp,
     if (reason == OFPPR_DELETE) {
         memset(pw_opp, 0, sizeof *pw_opp);
         pw_opp->port_no = htons(OFPP_NONE);
-    } else if (reason == OFPPR_MOD || reason == OFPPR_ADD) {
+    } else if (reason == OFPPR_MODIFY || reason == OFPPR_ADD) {
         *pw_opp = *opp;
         sanitize_opp(pw_opp);
     }
@@ -725,8 +720,7 @@ port_watcher_local_packet_cb(struct relay *r, void *pw_)
                    / sizeof *osf->ports);
         for (i = 0; i < n_ports; i++) {
             struct ofp_phy_port *opp = &osf->ports[i];
-            call_edit_port_callbacks(pw, opp);
-            update_phy_port(pw, opp, OFPPR_MOD, seen);
+            update_phy_port(pw, opp, OFPPR_MODIFY, seen);
         }
 
         /* Delete all the ports not included in the message. */
@@ -738,7 +732,6 @@ port_watcher_local_packet_cb(struct relay *r, void *pw_)
     } else if (oh->type == OFPT_PORT_STATUS
                && msg->size >= sizeof(struct ofp_port_status)) {
         struct ofp_port_status *ops = msg->data;
-        call_edit_port_callbacks(pw, &ops->desc);
         update_phy_port(pw, &ops->desc, ops->reason, NULL);
     }
     return false;
@@ -754,14 +747,14 @@ port_watcher_remote_packet_cb(struct relay *r, void *pw_)
     if (oh->type == OFPT_PORT_MOD
         && msg->size >= sizeof(struct ofp_port_mod)) {
         struct ofp_port_mod *opm = msg->data;
-        uint16_t port_no = ntohs(opm->desc.port_no);
+        uint16_t port_no = ntohs(opm->port_no);
         int idx = port_no_to_pw_idx(port_no);
         if (idx >= 0) {
             struct ofp_phy_port *pw_opp = &pw->ports[idx];
             if (pw_opp->port_no != htons(OFPP_NONE)) {
                 struct ofp_phy_port old = *pw_opp;
-                pw_opp->flags = ((pw_opp->flags & ~opm->mask)
-                                 | (opm->desc.flags & opm->mask));
+                pw_opp->config = ((pw_opp->config & ~opm->mask)
+                                 | (opm->config & opm->mask));
                 call_port_changed_callbacks(pw, port_no, &old, pw_opp);
             }
         }
@@ -797,6 +790,31 @@ put_duplexes(struct ds *ds, const char *name, uint32_t features,
     }
 }
 
+static void
+put_features(struct ds *ds, const char *name, uint32_t features) {
+    if (features & (OFPPF_10MB_HD | OFPPF_10MB_FD
+                    | OFPPF_100MB_HD | OFPPF_100MB_FD
+                    | OFPPF_1GB_HD | OFPPF_1GB_FD | OFPPF_10GB_FD)) {
+        ds_put_cstr(ds, name);
+        put_duplexes(ds, "10M", features, OFPPF_10MB_HD, OFPPF_10MB_FD);
+        put_duplexes(ds, "100M", features,
+                     OFPPF_100MB_HD, OFPPF_100MB_FD);
+        put_duplexes(ds, "1G", features, OFPPF_100MB_HD, OFPPF_100MB_FD);
+        if (features & OFPPF_10GB_FD) {
+            ds_put_cstr(ds, " 10G");
+        }
+        if (features & OFPPF_AUTONEG) {
+            ds_put_cstr(ds, " AUTO_NEG");
+        }
+        if (features & OFPPF_PAUSE) {
+            ds_put_cstr(ds, " PAUSE");
+        }
+        if (features & OFPPF_PAUSE_ASYM) {
+            ds_put_cstr(ds, " PAUSE_ASYM");
+        }
+    }
+}
+
 static void
 log_port_status(uint16_t port_no,
                 const struct ofp_phy_port *old,
@@ -806,10 +824,12 @@ log_port_status(uint16_t port_no,
     if (VLOG_IS_DBG_ENABLED()) {
         bool was_enabled = old->port_no != htons(OFPP_NONE);
         bool now_enabled = new->port_no != htons(OFPP_NONE);
-        uint32_t features = ntohl(new->features);
+        uint32_t curr = ntohl(new->curr);
+        uint32_t supported = ntohl(new->supported);
         struct ds ds;
 
-        if (old->flags != new->flags && opp_differs(old, new) == 1) {
+        if (((old->config != new->config) || (old->state != new->state))
+                && opp_differs(old, new) == 1) {
             /* Don't care if only flags changed. */
             return;
         }
@@ -817,20 +837,11 @@ log_port_status(uint16_t port_no,
         ds_init(&ds);
         ds_put_format(&ds, "\"%s\", "ETH_ADDR_FMT, new->name,
                       ETH_ADDR_ARGS(new->hw_addr));
-        if (ntohl(new->speed)) {
-            ds_put_format(&ds, ", speed %"PRIu32, ntohl(new->speed));
+        if (curr) {
+            put_features(&ds, ", current", curr);
         }
-        if (features & (OFPPF_10MB_HD | OFPPF_10MB_FD
-                        | OFPPF_100MB_HD | OFPPF_100MB_FD
-                        | OFPPF_1GB_HD | OFPPF_1GB_FD | OFPPF_10GB_FD)) {
-            ds_put_cstr(&ds, ", supports");
-            put_duplexes(&ds, "10M", features, OFPPF_10MB_HD, OFPPF_10MB_FD);
-            put_duplexes(&ds, "100M", features,
-                         OFPPF_100MB_HD, OFPPF_100MB_FD);
-            put_duplexes(&ds, "1G", features, OFPPF_100MB_HD, OFPPF_100MB_FD);
-            if (features & OFPPF_10GB_FD) {
-                ds_put_cstr(&ds, " 10G");
-            }
+        if (supported) {
+            put_features(&ds, ", supports", supported);
         }
         if (was_enabled != now_enabled) {
             if (now_enabled) {
@@ -847,27 +858,26 @@ log_port_status(uint16_t port_no,
 
 static void
 port_watcher_register_callback(struct port_watcher *pw,
-                               edit_port_cb_func *edit_port,
                                port_changed_cb_func *port_changed,
                                void *aux)
 {
     assert(pw->n_cbs < ARRAY_SIZE(pw->cbs));
-    pw->cbs[pw->n_cbs].edit_port = edit_port;
     pw->cbs[pw->n_cbs].port_changed = port_changed;
     pw->cbs[pw->n_cbs].aux = aux;
     pw->n_cbs++;
 }
 
 static uint32_t
-port_watcher_get_flags(const struct port_watcher *pw, int port_no)
+port_watcher_get_config(const struct port_watcher *pw, int port_no)
 {
     int idx = port_no_to_pw_idx(port_no);
-    return idx >= 0 ? ntohl(pw->ports[idx].flags) : 0;
+    return idx >= 0 ? ntohl(pw->ports[idx].config) : 0;
 }
 
 static void
-port_watcher_set_flags(struct port_watcher *pw,
-                       int port_no, uint32_t flags, uint32_t mask)
+port_watcher_set_flags(struct port_watcher *pw, int port_no, 
+                       uint32_t config, uint32_t c_mask,
+                       uint32_t state, uint32_t s_mask)
 {
     struct ofp_phy_port old;
     struct ofp_phy_port *p;
@@ -882,24 +892,29 @@ port_watcher_set_flags(struct port_watcher *pw,
     }
 
     p = &pw->ports[idx];
-    if (!((ntohl(p->flags) ^ flags) & mask)) {
+    if (!((ntohl(p->state) ^ state) & s_mask) 
+            && (!((ntohl(p->config) ^ config) & c_mask))) {
         return;
     }
     old = *p;
 
     /* Update our idea of the flags. */
-    p->flags = htonl((ntohl(p->flags) & ~mask) | (flags & mask));
+    p->config = htonl((ntohl(p->config) & ~c_mask) | (config & c_mask));
+    p->state = htonl((ntohl(p->state) & ~s_mask) | (state & s_mask));
     call_port_changed_callbacks(pw, port_no, &old, p);
 
     /* Change the flags in the datapath. */
     opm = make_openflow(sizeof *opm, OFPT_PORT_MOD, &b);
-    opm->mask = htonl(mask);
-    opm->desc = *p;
+    opm->port_no = p->port_no;
+    memcpy(opm->hw_addr, p->hw_addr, OFP_ETH_ALEN);
+    opm->config = p->config;
+    opm->mask = htonl(c_mask);
+    opm->advertise = htonl(0);
     rconn_send(pw->local_rconn, b, NULL);
 
     /* Notify the controller that the flags changed. */
     ops = make_openflow(sizeof *ops, OFPT_PORT_STATUS, &b);
-    ops->reason = OFPPR_MOD;
+    ops->reason = OFPPR_MODIFY;
     ops->desc = *p;
     rconn_send(pw->remote_rconn, b, NULL);
 }
@@ -924,7 +939,7 @@ port_watcher_create(struct rconn *local_rconn, struct rconn *remote_rconn,
     for (i = 0; i < OFPP_MAX; i++) {
         pw->ports[i].port_no = htons(OFPP_NONE);
     }
-    port_watcher_register_callback(pw, NULL, log_port_status, NULL);
+    port_watcher_register_callback(pw, log_port_status, NULL);
     return make_hook(port_watcher_local_packet_cb,
                      port_watcher_remote_packet_cb,
                      port_watcher_periodic_cb, NULL, pw);
@@ -977,7 +992,7 @@ stp_local_packet_cb(struct relay *r, void *stp_)
         /* STP only supports 255 ports. */
         return false;
     }
-    if (port_watcher_get_flags(stp->pw, port_no) & OFPPFL_NO_STP) {
+    if (port_watcher_get_config(stp->pw, port_no) & OFPPC_NO_STP) {
         /* We're not doing STP on this port. */
         return false;
     }
@@ -1040,39 +1055,41 @@ stp_periodic_cb(void *stp_)
 
     while (stp_get_changed_port(stp->stp, &p)) {
         int port_no = stp_port_no(p);
-        enum stp_state state = stp_port_get_state(p);
+        enum stp_state s_state = stp_port_get_state(p);
 
-        if (state != STP_DISABLED) {
+        if (s_state != STP_DISABLED) {
             VLOG_WARN("STP: Port %d entered %s state",
-                      port_no, stp_state_name(state));
+                      port_no, stp_state_name(s_state));
         }
-        if (!(port_watcher_get_flags(stp->pw, port_no) & OFPPFL_NO_STP)) {
-            uint32_t flags;
-            switch (state) {
+        if (!(port_watcher_get_config(stp->pw, port_no) & OFPPC_NO_STP)) {
+            uint32_t p_config = 0;
+            uint32_t p_state;
+            switch (s_state) {
             case STP_LISTENING:
-                flags = OFPPFL_STP_LISTEN;
+                p_state = OFPPS_STP_LISTEN;
                 break;
             case STP_LEARNING:
-                flags = OFPPFL_STP_LEARN;
+                p_state = OFPPS_STP_LEARN;
                 break;
             case STP_DISABLED:
             case STP_FORWARDING:
-                flags = OFPPFL_STP_FORWARD;
+                p_state = OFPPS_STP_FORWARD;
                 break;
             case STP_BLOCKING:
-                flags = OFPPFL_STP_BLOCK;
+                p_state = OFPPS_STP_BLOCK;
                 break;
             default:
                 VLOG_DBG_RL(&vrl, "STP: Port %d has bad state %x",
-                            port_no, state);
-                flags = OFPPFL_STP_FORWARD;
+                            port_no, s_state);
+                p_state = OFPPS_STP_FORWARD;
                 break;
             }
-            if (!stp_forward_in_state(state)) {
-                flags |= OFPPFL_NO_FLOOD;
+            if (!stp_forward_in_state(s_state)) {
+                p_config = OFPPC_NO_FLOOD;
             }
-            port_watcher_set_flags(stp->pw, port_no, flags,
-                                   OFPPFL_STP_MASK | OFPPFL_NO_FLOOD);
+            port_watcher_set_flags(stp->pw, port_no, 
+                                   p_config, OFPPC_NO_FLOOD,
+                                   p_state, OFPPS_STP_MASK);
         } else {
             /* We don't own those flags. */
         }
@@ -1117,20 +1134,12 @@ send_bpdu(const void *bpdu, size_t bpdu_size, int port_no, void *stp_)
 static bool
 stp_is_port_supported(uint16_t port_no)
 {
-    /* STP only supports a maximum of 255 ports, one less than OpenFlow.  We
-     * don't support STP on OFPP_LOCAL, either.  */
+    /* We should be able to support STP on all possible OpenFlow physical
+     * ports.  (But we don't support STP on OFPP_LOCAL.)  */
+    BUILD_ASSERT_DECL(STP_MAX_PORTS >= OFPP_MAX);
     return port_no < STP_MAX_PORTS;
 }
 
-static void
-stp_edit_port_cb(struct ofp_phy_port *p, void *stp_ UNUSED)
-{
-    uint16_t port_no = ntohs(p->port_no);
-    if (stp_is_port_supported(port_no)) {
-        p->features |= htonl(OFPPF_STP);
-    }
-}
-
 static void
 stp_port_changed_cb(uint16_t port_no,
                     const struct ofp_phy_port *old,
@@ -1146,11 +1155,21 @@ stp_port_changed_cb(uint16_t port_no,
 
     p = stp_get_port(stp->stp, port_no);
     if (new->port_no == htons(OFPP_NONE)
-        || new->flags & htonl(OFPPFL_NO_STP)) {
+        || new->config & htonl(OFPPC_NO_STP)) {
         stp_port_disable(p);
     } else {
+        int speed = 0;
         stp_port_enable(p);
-        stp_port_set_speed(p, new->speed);
+        if (new->curr & (OFPPF_10MB_HD | OFPPF_10MB_FD)) {
+            speed = 10;
+        } else if (new->curr & (OFPPF_100MB_HD | OFPPF_100MB_FD)) {
+            speed = 100;
+        } else if (new->curr & (OFPPF_1GB_HD | OFPPF_1GB_FD)) {
+            speed = 1000;
+        } else if (new->curr & OFPPF_100MB_FD) {
+            speed = 10000;
+        }
+        stp_port_set_speed(p, speed);
     }
 }
 
@@ -1178,8 +1197,7 @@ stp_hook_create(const struct settings *s, struct port_watcher *pw,
     stp->remote_rconn = remote;
     stp->last_tick_256ths = time_256ths();
 
-    port_watcher_register_callback(pw, stp_edit_port_cb,
-                                   stp_port_changed_cb, stp);
+    port_watcher_register_callback(pw, stp_port_changed_cb, stp);
     return make_hook(stp_local_packet_cb, NULL,
                      stp_periodic_cb, stp_wait_cb, stp);
 }
@@ -1491,7 +1509,9 @@ fail_open_hook_create(const struct settings *s, struct switch_status *ss,
     fail_open->remote_rconn = remote_rconn;
     fail_open->lswitch = NULL;
     fail_open->boot_deadline = time_now() + s->probe_interval * 3;
-    fail_open->boot_deadline += STP_EXTRA_BOOT_TIME;
+    if (s->enable_stp) {
+        fail_open->boot_deadline += STP_EXTRA_BOOT_TIME;
+    }
     switch_status_register_category(ss, "fail-open",
                                     fail_open_status_cb, fail_open);
     return make_hook(fail_open_local_packet_cb, NULL,
@@ -2042,7 +2062,8 @@ parse_options(int argc, char *argv[], struct settings *s)
         OPT_MAX_BACKOFF,
         OPT_RATE_LIMIT,
         OPT_BURST_LIMIT,
-        OPT_BOOTSTRAP_CA_CERT
+        OPT_BOOTSTRAP_CA_CERT,
+        OPT_NO_STP
     };
     static struct option long_options[] = {
         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
@@ -2055,6 +2076,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         {"monitor",     required_argument, 0, 'm'},
         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
+        {"no-stp",      no_argument, 0, OPT_NO_STP},
         {"detach",      no_argument, 0, 'D'},
         {"force",       no_argument, 0, 'f'},
         {"pidfile",     optional_argument, 0, 'P'},
@@ -2081,6 +2103,7 @@ parse_options(int argc, char *argv[], struct settings *s)
     s->update_resolv_conf = true;
     s->rate_limit = 0;
     s->burst_limit = 0;
+    s->enable_stp = true;
     for (;;) {
         int c;
 
@@ -2155,6 +2178,10 @@ parse_options(int argc, char *argv[], struct settings *s)
             }
             break;
 
+        case OPT_NO_STP:
+            s->enable_stp = false;
+            break;
+
         case 'D':
             set_detach();
             break;
@@ -2309,6 +2336,7 @@ usage(void)
            "                          (a passive OpenFlow connection method)\n"
            "  -m, --monitor=METHOD    copy traffic to/from kernel to METHOD\n"
            "                          (a passive OpenFlow connection method)\n"
+           "  --no-stp                disable 802.1D Spanning Tree Protocol\n"
            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
            "  --burst-limit=BURST     limit on packet credit for idle time\n"