ofproto: Bundle all controller-related settings into a struct.
[sliver-openvswitch.git] / utilities / ovs-openflowd.c
index 4e3f380..70430c0 100644 (file)
 #include "vlog.h"
 #define THIS_MODULE VLM_openflowd
 
-/* Behavior when the connection to the controller fails. */
-enum fail_mode {
-    FAIL_OPEN,                  /* Act as learning switch. */
-    FAIL_CLOSED                 /* Drop all packets. */
-};
-
 /* Settings that may be configured by the user. */
 struct ofsettings {
-    /* Overall mode of operation. */
-    bool discovery;           /* Discover the controller automatically? */
-    bool in_band;             /* Connect to controller in-band? */
+    /* Controller configuration. */
+    struct ofproto_controller controller;
 
     /* Datapath. */
     uint64_t datapath_id;       /* Datapath ID. */
@@ -74,23 +67,11 @@ struct ofsettings {
     const char *dp_desc;        /* Datapath description. */
 
     /* Related vconns and network devices. */
-    const char *controller_name; /* Controller (if not discovery mode). */
     struct svec listeners;       /* Listen for management connections. */
     struct svec snoops;          /* Listen for controller snooping conns. */
 
     /* Failure behavior. */
-    enum fail_mode fail_mode; /* Act as learning switch if no controller? */
     int max_idle;             /* Idle time for flows in fail-open mode. */
-    int probe_interval;       /* # seconds idle before sending echo request. */
-    int max_backoff;          /* Max # seconds between connection attempts. */
-
-    /* Packet-in rate-limiting. */
-    int rate_limit;           /* Tokens added to bucket per second. */
-    int burst_limit;          /* Maximum number token bucket size. */
-
-    /* Discovery behavior. */
-    const char *accept_controller_re; /* Controller vconns to accept. */
-    bool update_resolv_conf;          /* Update /etc/resolv.conf? */
 
     /* Spanning tree protocol. */
     bool enable_stp;
@@ -160,15 +141,6 @@ main(int argc, char *argv[])
     if (error) {
         ovs_fatal(error, "could not initialize openflow switch");
     }
-    error = ofproto_set_in_band(ofproto, s.in_band);
-    if (error) {
-        ovs_fatal(error, "failed to configure in-band control");
-    }
-    error = ofproto_set_discovery(ofproto, s.discovery, s.accept_controller_re,
-                                  s.update_resolv_conf);
-    if (error) {
-        ovs_fatal(error, "failed to configure controller discovery");
-    }
     if (s.datapath_id) {
         ofproto_set_datapath_id(ofproto, s.datapath_id);
     }
@@ -195,20 +167,11 @@ main(int argc, char *argv[])
     if (error) {
         ovs_fatal(error, "failed to configure NetFlow collectors");
     }
-    ofproto_set_failure(ofproto, s.fail_mode == FAIL_OPEN);
-    ofproto_set_probe_interval(ofproto, s.probe_interval);
-    ofproto_set_max_backoff(ofproto, s.max_backoff);
-    ofproto_set_rate_limit(ofproto, s.rate_limit, s.burst_limit);
     error = ofproto_set_stp(ofproto, s.enable_stp);
     if (error) {
         ovs_fatal(error, "failed to configure STP");
     }
-    if (!s.discovery) {
-        error = ofproto_set_controller(ofproto, s.controller_name);
-        if (error) {
-            ovs_fatal(error, "failed to configure controller");
-        }
-    }
+    ofproto_set_controller(ofproto, &s.controller);
 
     daemonize_complete();
 
@@ -305,6 +268,14 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
     char *short_options = long_options_to_short_options(long_options);
 
     /* Set defaults that we can figure out before parsing options. */
+    s->controller.max_backoff = 8;
+    s->controller.probe_interval = 5;
+    s->controller.fail = OFPROTO_FAIL_STANDALONE;
+    s->controller.band = OFPROTO_IN_BAND;
+    s->controller.accept_re = NULL;
+    s->controller.update_resolv_conf = true;
+    s->controller.rate_limit = 0;
+    s->controller.burst_limit = 0;
     s->datapath_id = 0;
     s->mfr_desc = NULL;
     s->hw_desc = NULL;
@@ -313,16 +284,8 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
     s->dp_desc = NULL;
     svec_init(&s->listeners);
     svec_init(&s->snoops);
-    s->fail_mode = FAIL_OPEN;
     s->max_idle = 0;
-    s->probe_interval = 0;
-    s->max_backoff = 8;
-    s->update_resolv_conf = true;
-    s->rate_limit = 0;
-    s->burst_limit = 0;
-    s->accept_controller_re = NULL;
     s->enable_stp = false;
-    s->in_band = true;
     svec_init(&s->netflow);
     svec_init(&s->ports);
     for (;;) {
@@ -362,26 +325,26 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
             break;
 
         case OPT_ACCEPT_VCONN:
-            s->accept_controller_re = optarg;
+            s->controller.accept_re = optarg;
             break;
 
         case OPT_NO_RESOLV_CONF:
-            s->update_resolv_conf = false;
+            s->controller.update_resolv_conf = false;
             break;
 
         case OPT_FAIL_MODE:
             if (!strcmp(optarg, "open")) {
-                s->fail_mode = FAIL_OPEN;
+                s->controller.fail = OFPROTO_FAIL_STANDALONE;
             } else if (!strcmp(optarg, "closed")) {
-                s->fail_mode = FAIL_CLOSED;
+                s->controller.fail = OFPROTO_FAIL_SECURE;
             } else {
                 ovs_fatal(0, "--fail argument must be \"open\" or \"closed\"");
             }
             break;
 
         case OPT_INACTIVITY_PROBE:
-            s->probe_interval = atoi(optarg);
-            if (s->probe_interval < 5) {
+            s->controller.probe_interval = atoi(optarg);
+            if (s->controller.probe_interval < 5) {
                 ovs_fatal(0, "--inactivity-probe argument must be at least 5");
             }
             break;
@@ -399,28 +362,28 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
             break;
 
         case OPT_MAX_BACKOFF:
-            s->max_backoff = atoi(optarg);
-            if (s->max_backoff < 1) {
+            s->controller.max_backoff = atoi(optarg);
+            if (s->controller.max_backoff < 1) {
                 ovs_fatal(0, "--max-backoff argument must be at least 1");
-            } else if (s->max_backoff > 3600) {
-                s->max_backoff = 3600;
+            } else if (s->controller.max_backoff > 3600) {
+                s->controller.max_backoff = 3600;
             }
             break;
 
         case OPT_RATE_LIMIT:
             if (optarg) {
-                s->rate_limit = atoi(optarg);
-                if (s->rate_limit < 1) {
+                s->controller.rate_limit = atoi(optarg);
+                if (s->controller.rate_limit < 1) {
                     ovs_fatal(0, "--rate-limit argument must be at least 1");
                 }
             } else {
-                s->rate_limit = 1000;
+                s->controller.rate_limit = 1000;
             }
             break;
 
         case OPT_BURST_LIMIT:
-            s->burst_limit = atoi(optarg);
-            if (s->burst_limit < 1) {
+            s->controller.burst_limit = atoi(optarg);
+            if (s->controller.burst_limit < 1) {
                 ovs_fatal(0, "--burst-limit argument must be at least 1");
             }
             break;
@@ -434,11 +397,11 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
             break;
 
         case OPT_OUT_OF_BAND:
-            s->in_band = false;
+            s->controller.band = OFPROTO_OUT_OF_BAND;
             break;
 
         case OPT_IN_BAND:
-            s->in_band = true;
+            s->controller.band = OFPROTO_IN_BAND;
             break;
 
         case OPT_NETFLOW:
@@ -497,23 +460,22 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
     /* Local and remote vconns. */
     dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
 
-    s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
+    s->controller.target = argc > 1 ? argv[1] : "discover";
+    if (!strcmp(s->controller.target, "discover")
+        && s->controller.band == OFPROTO_OUT_OF_BAND) {
+        ovs_fatal(0, "Cannot perform discovery with out-of-band control");
+    }
 
     /* Set accept_controller_regex. */
-    if (!s->accept_controller_re) {
-        s->accept_controller_re
+    if (!s->controller.accept_re) {
+        s->controller.accept_re
             = stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*";
     }
 
-    /* Mode of operation. */
-    s->discovery = s->controller_name == NULL;
-    if (s->discovery && !s->in_band) {
-        ovs_fatal(0, "Cannot perform discovery with out-of-band control");
-    }
-
     /* Rate limiting. */
-    if (s->rate_limit && s->rate_limit < 100) {
-        VLOG_WARN("Rate limit set to unusually low value %d", s->rate_limit);
+    if (s->controller.rate_limit && s->controller.rate_limit < 100) {
+        VLOG_WARN("Rate limit set to unusually low value %d",
+                  s->controller.rate_limit);
     }
 }