Answer ARP requests from controller directly in secchan.
[openvswitch] / secchan / secchan.c
index 0ef6023c78843668fa6c515423879ea6456b468a..8c59b8aea7a6dd7c0efc2f3acd1ae4ee11970dbb 100644 (file)
@@ -103,6 +103,9 @@ static struct mac_learning *local_ml;
 /* -f, --fail: Behavior when the connection to the controller fails. */
 static enum fail_mode fail_mode = FAIL_OPEN;
 
+/* The OpenFlow virtual network device ofX. */
+static struct netdev *of_device;
+
 /* --inactivity-probe: Number of seconds without receiving a message from the
    controller before sending an inactivity probe. */
 static int probe_interval = 15;
@@ -122,13 +125,14 @@ static void relay_wait(struct relay *);
 static void relay_destroy(struct relay *);
 
 static bool local_hook(struct relay *r);
+static bool failing_open(struct relay *r);
 static bool fail_open_hook(struct relay *r);
 
 int
 main(int argc, char *argv[])
 {
     struct vconn *listen_vconn;
-    struct netdev *of_device;
+    struct relay *controller_relay;
     const char *nl_name;
     char of_name[16];
     int retval;
@@ -182,7 +186,6 @@ main(int argc, char *argv[])
         } else {
             error(retval, "Could not get flags for %s device", of_name);
         }
-        netdev_close(of_device);
     } else {
         error(retval, "Could not open %s device", of_name);
     }
@@ -194,9 +197,10 @@ main(int argc, char *argv[])
 
     daemonize();
 
-    relay_create(rconn_new(argv[optind], 1, 0),
-                 rconn_new(argv[optind + 1], 1, probe_interval),
-                 false);
+    controller_relay = relay_create(rconn_new(argv[optind], 1, 0),
+                                    rconn_new(argv[optind + 1], 1,
+                                              probe_interval),
+                                    false);
     for (;;) {
         struct relay *r, *n;
 
@@ -217,6 +221,7 @@ main(int argc, char *argv[])
                 new_management_connection(nl_name, new_remote);
             }
         }
+        failing_open(controller_relay);
 
         /* Wait for something to happen. */
         LIST_FOR_EACH (r, struct relay, node, &relays) {
@@ -362,6 +367,56 @@ relay_destroy(struct relay *r)
     free(r);
 }
 
+static bool
+is_controller_mac(const uint8_t dl_addr[ETH_ADDR_LEN],
+                  struct rconn *controller) 
+{
+    static uint32_t ip, last_nonzero_ip;
+    static uint8_t mac[ETH_ADDR_LEN], last_nonzero_mac[ETH_ADDR_LEN];
+    static time_t next_refresh = 0;
+
+    uint32_t last_ip = ip;
+
+    time_t now = time(0);
+
+    ip = rconn_get_ip(controller);
+    if (last_ip != ip || !next_refresh || now >= next_refresh) {
+        bool have_mac;
+
+        /* Look up MAC address. */
+        memset(mac, 0, sizeof mac);
+        if (ip) {
+            int retval = netdev_arp_lookup(of_device, ip, mac);
+            if (retval) {
+                VLOG_DBG("cannot look up controller hw address ("IP_FMT"): %s",
+                         IP_ARGS(&ip), strerror(retval));
+            }
+        }
+        have_mac = !eth_addr_is_zero(mac);
+
+        /* Log changes in IP, MAC addresses. */
+        if (ip && ip != last_nonzero_ip) {
+            VLOG_DBG("controller IP address changed from "IP_FMT
+                     " to "IP_FMT, IP_ARGS(&last_nonzero_ip), IP_ARGS(&ip));
+            last_nonzero_ip = ip;
+        }
+        if (have_mac && memcmp(last_nonzero_mac, mac, ETH_ADDR_LEN)) {
+            VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
+                     ETH_ADDR_FMT,
+                     ETH_ADDR_ARGS(last_nonzero_mac), ETH_ADDR_ARGS(mac));
+            memcpy(last_nonzero_mac, mac, ETH_ADDR_LEN);
+        }
+
+        /* Schedule next refresh.
+         *
+         * If we have an IP address but not a MAC address, then refresh
+         * quickly, since we probably will get a MAC address soon (via ARP).
+         * Otherwise, we can afford to wait a little while. */
+        next_refresh = now + (!ip || have_mac ? 10 : 1);
+    }
+    return !eth_addr_is_zero(mac) && eth_addr_equals(mac, dl_addr);
+}
+
 static bool
 local_hook(struct relay *r)
 {
@@ -405,6 +460,11 @@ local_hook(struct relay *r)
             VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
                      ETH_ADDR_ARGS(flow.dl_src), in_port);
         }
+    } else if (flow.dl_type == htons(ETH_TYPE_ARP)
+               && eth_addr_is_broadcast(flow.dl_dst)
+               && is_controller_mac(flow.dl_src,
+                                    r->halves[HALF_REMOTE].rconn)) {
+        out_port = OFPP_FLOOD;
     } else {
         return false;
     }
@@ -428,10 +488,11 @@ local_hook(struct relay *r)
     return true;
 }
 
+/* Causess 'r' to enter or leave fail-open mode, if appropriate.  Returns true
+ * if 'r' is in fail-open fail, false otherwise. */
 static bool
-fail_open_hook(struct relay *r)
+failing_open(struct relay *r)
 {
-    struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
     struct rconn *local = r->halves[HALF_LOCAL].rconn;
     struct rconn *remote = r->halves[HALF_REMOTE].rconn;
     int disconnected_duration;
@@ -458,13 +519,23 @@ fail_open_hook(struct relay *r)
                   "failing open", disconnected_duration);
         r->lswitch = lswitch_create(local, true, max_idle);
     }
-
-    /* Do switching. */
-    lswitch_process_packet(r->lswitch, local, msg);
-    rconn_run(local);
     return true;
 }
 
+static bool
+fail_open_hook(struct relay *r)
+{
+    if (!failing_open(r)) {
+        return false;
+    } else {
+        struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
+        struct rconn *local = r->halves[HALF_LOCAL].rconn;
+        lswitch_process_packet(r->lswitch, local, msg);
+        rconn_run(local);
+        return true;
+    }
+}
+
 static void
 parse_options(int argc, char *argv[]) 
 {