Make facility and level optional in -v, --verbose options.
[openvswitch] / secchan / secchan.c
index 2a2eef6683d9c34cbceb5abe492c98279e73e106..886b2a2e90e110f897c2ba1be66e42f9bc108a53 100644 (file)
@@ -44,6 +44,7 @@
 #include "buffer.h"
 #include "command-line.h"
 #include "compiler.h"
+#include "daemon.h"
 #include "fault.h"
 #include "flow.h"
 #include "learning-switch.h"
@@ -102,9 +103,20 @@ static struct mac_learning *local_ml;
 /* -f, --fail: Behavior when the connection to the controller fails. */
 static enum fail_mode fail_mode = FAIL_OPEN;
 
-/* -d, --fail-open-delay: Number of seconds after which to fail open, when
- * fail_mode is FAIL_OPEN. */
-static int fail_open_delay = 30;
+/* 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;
+
+/* --max-idle: Idle time to assign to flows created by learning switch when in
+ * fail-open mode. */
+static int max_idle = 15;
+
+/* --max-backoff: Maximum interval between controller connection attempts, in
+ * seconds. */
+static int max_backoff = 15;
 
 static void parse_options(int argc, char *argv[]);
 static void usage(void) NO_RETURN;
@@ -117,13 +129,15 @@ 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 rconn *local_rconn, *remote_rconn;
     struct vconn *listen_vconn;
-    struct netdev *of_device;
+    struct relay *controller_relay;
     const char *nl_name;
     char of_name[16];
     int retval;
@@ -141,7 +155,7 @@ main(int argc, char *argv[])
     if (strncmp(nl_name, "nl:", 3)
         || strlen(nl_name) < 4
         || nl_name[strspn(nl_name + 3, "0123456789") + 3]) {
-        fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", nl_name);
+        fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", nl_name);
     }
 
     if (listen_vconn_name) {
@@ -157,7 +171,7 @@ main(int argc, char *argv[])
     }
 
     snprintf(of_name, sizeof of_name, "of%s", nl_name + 3);
-    retval = netdev_open(of_name, &of_device);
+    retval = netdev_open(of_name, NETDEV_ETH_TYPE_NONE, &of_device);
     if (!retval) {
         enum netdev_flags flags;
         retval = netdev_get_flags(of_device, &flags);
@@ -177,7 +191,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);
     }
@@ -187,8 +200,20 @@ main(int argc, char *argv[])
         fatal(retval, "Could not listen for vlog connections");
     }
 
-    relay_create(rconn_new(argv[optind], 1), rconn_new(argv[optind + 1], 1),
-                 false);
+    daemonize();
+
+    local_rconn = rconn_create(1, 0, max_backoff);
+    retval = rconn_connect(local_rconn, nl_name);
+    if (retval == EAFNOSUPPORT) {
+        fatal(0, "No support for %s vconn", nl_name);
+    }
+
+    remote_rconn = rconn_create(1, probe_interval, max_backoff);
+    retval = rconn_connect(remote_rconn, argv[optind + 1]);
+    if (retval == EAFNOSUPPORT) {
+        fatal(0, "No support for %s vconn", argv[optind + 1]);
+    }
+    controller_relay = relay_create(local_rconn, remote_rconn, false);
     for (;;) {
         struct relay *r, *n;
 
@@ -209,6 +234,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) {
@@ -354,6 +380,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)
 {
@@ -389,10 +465,7 @@ local_hook(struct relay *r)
     flow_extract(&pkt, in_port, &flow);
 
     /* Deal with local stuff. */
-    if (!rconn_is_connected(r->halves[HALF_REMOTE].rconn)
-        && eth_addr_is_broadcast(flow.dl_dst)) {
-        out_port = OFPP_FLOOD;
-    } else if (in_port == OFPP_LOCAL) {
+    if (in_port == OFPP_LOCAL) {
         out_port = mac_learning_lookup(local_ml, flow.dl_dst);
     } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
         out_port = OFPP_LOCAL;
@@ -400,13 +473,19 @@ 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;
     }
 
     /* Add new flow. */
     if (out_port != OFPP_FLOOD) {
-        b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port);
+        b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port,
+                                 max_idle);
         if (rconn_force_send(rc, b)) {
             buffer_delete(b);
         }
@@ -422,10 +501,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;
@@ -436,7 +516,7 @@ fail_open_hook(struct relay *r)
     }
 
     disconnected_duration = rconn_disconnected_duration(remote);
-    if (disconnected_duration < fail_open_delay) {
+    if (disconnected_duration < probe_interval * 3) {
         /* It's not time to fail open yet. */
         if (r->lswitch && rconn_is_connected(remote)) {
             /* We're connected, so drop the learning switch. */
@@ -450,22 +530,41 @@ fail_open_hook(struct relay *r)
     if (!r->lswitch) {
         VLOG_WARN("Could not connect to controller for %d seconds, "
                   "failing open", disconnected_duration);
-        r->lswitch = lswitch_create(local, true, true);
+        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[]) 
 {
+    enum {
+        OPT_INACTIVITY_PROBE = UCHAR_MAX + 1,
+        OPT_MAX_IDLE,
+        OPT_MAX_BACKOFF
+    };
     static struct option long_options[] = {
         {"fail",        required_argument, 0, 'f'},
-        {"fail-open-delay", required_argument, 0, 'd'},
+        {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
+        {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
+        {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
         {"listen",      required_argument, 0, 'l'},
+        {"detach",      no_argument, 0, 'D'},
+        {"pidfile",     optional_argument, 0, 'P'},
         {"verbose",     optional_argument, 0, 'v'},
         {"help",        no_argument, 0, 'h'},
         {"version",     no_argument, 0, 'V'},
@@ -494,14 +593,42 @@ parse_options(int argc, char *argv[])
             }
             break;
 
-        case 'd':
-            fail_open_delay = atoi(optarg);
-            if (fail_open_delay < 1) {
-                fatal(0,
-                      "-d or --fail-open-delay argument must be at least 1");
+        case OPT_INACTIVITY_PROBE:
+            probe_interval = atoi(optarg);
+            if (probe_interval < 5) {
+                fatal(0, "--inactivity-probe argument must be at least 5");
             }
             break;
 
+        case OPT_MAX_IDLE:
+            if (!strcmp(optarg, "permanent")) {
+                max_idle = OFP_FLOW_PERMANENT;
+            } else {
+                max_idle = atoi(optarg);
+                if (max_idle < 1 || max_idle > 65535) {
+                    fatal(0, "--max-idle argument must be between 1 and "
+                          "65535 or the word 'permanent'");
+                }
+            }
+            break;
+
+        case OPT_MAX_BACKOFF:
+            max_backoff = atoi(optarg);
+            if (max_backoff < 1) {
+                fatal(0, "--max-backoff argument must be at least 1");
+            } else if (max_backoff > 3600) {
+                max_backoff = 3600;
+            }
+            break;
+
+        case 'D':
+            set_detach();
+            break;
+
+        case 'P':
+            set_pidfile(optarg ? optarg : "secchan.pid");
+            break;
+
         case 'l':
             if (listen_vconn_name) {
                 fatal(0, "-l or --listen may be only specified once");
@@ -535,22 +662,29 @@ parse_options(int argc, char *argv[])
 static void
 usage(void)
 {
-    printf("%s: Secure Channel, a relay for OpenFlow messages.\n"
-           "usage: %s [OPTIONS] LOCAL REMOTE\n"
-           "where LOCAL and REMOTE are active OpenFlow connection methods.\n",
+    printf("%s: secure channel, a relay for OpenFlow messages.\n"
+           "usage: %s [OPTIONS] nl:DP_IDX CONTROLLER\n"
+           "where nl:DP_IDX is a datapath that has been added with dpctl\n"
+           "and CONTROLLER is an active OpenFlow connection method.\n",
            program_name, program_name);
     vconn_usage(true, true);
     printf("\nNetworking options:\n"
            "  -f, --fail=open|closed  when controller connection fails:\n"
            "                            closed: drop all packets\n"
            "                            open (default): act as learning switch\n"
-           "  -d, --fail-open-delay=SECS  number of seconds after which to\n"
-           "                          fail open if --fail=open (default: 30)\n"
+           "  --inactivity-probe=SECS time between inactivity probes\n"
+           "  --max-idle=SECS         max idle for flows set up by secchan\n"
+           "  --max-backoff=SECS      max time between controller connection\n"
+           "                          attempts (default: 15 seconds)\n"
            "  -l, --listen=METHOD     allow management connections on METHOD\n"
            "                          (a passive OpenFlow connection method)\n"
            "\nOther options:\n"
+           "  -D, --detach            run in background as daemon\n"
+           "  -P, --pidfile[=FILE]    create pidfile (default: %s/secchan.pid)\n"
+           "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
            "  -v, --verbose           set maximum verbosity level\n"
            "  -h, --help              display this help message\n"
-           "  -V, --version           display version information\n");
+           "  -V, --version           display version information\n",
+           RUNDIR);
     exit(EXIT_SUCCESS);
 }