Announce fail-open mode more aggressively in secchan.
[openvswitch] / secchan / secchan.c
index d1e619c29e36ab66b272766697e63f5db61c0317..4fb4eac5827c8554e24f997d17dc177d31a47b60 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"
@@ -100,11 +101,15 @@ static uint8_t local_mac[ETH_ADDR_LEN];
 static struct mac_learning *local_ml;
 
 /* -f, --fail: Behavior when the connection to the controller fails. */
-static enum fail_mode fail_mode;
+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;
+/* --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;
 
 static void parse_options(int argc, char *argv[]);
 static void usage(void) NO_RETURN;
@@ -117,6 +122,7 @@ 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
@@ -124,6 +130,7 @@ 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;
@@ -141,7 +148,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) {
@@ -187,8 +194,12 @@ 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();
+
+    controller_relay = relay_create(rconn_new(argv[optind], 1, 0),
+                                    rconn_new(argv[optind + 1], 1,
+                                              probe_interval),
+                                    false);
     for (;;) {
         struct relay *r, *n;
 
@@ -209,6 +220,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) {
@@ -389,10 +401,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;
@@ -406,7 +415,8 @@ local_hook(struct relay *r)
 
     /* 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 +432,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 +447,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 +461,36 @@ 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 };
     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},
         {"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 +519,33 @@ 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 '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 +579,27 @@ 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 (default): drop all packets\n"
-           "                            open: 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"
+           "                            closed: drop all packets\n"
+           "                            open (default): act as learning switch\n"
+           "  --inactivity-probe=SECS time between inactivity probes\n"
+           "  --max-idle=SECS         max idle for flows set up by secchan\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  configure 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);
 }