Prevent the secchan from dying due to SIGPIPE.
[sliver-openvswitch.git] / switch / switch.c
index f8f45bc..3a9c604 100644 (file)
  * derivatives without specific, written prior permission.
  */
 
+#include <config.h>
 #include <errno.h>
 #include <getopt.h>
+#include <limits.h>
+#include <signal.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "command-line.h"
+#include "daemon.h"
 #include "datapath.h"
 #include "fault.h"
 #include "openflow.h"
@@ -45,6 +49,7 @@
 #include "queue.h"
 #include "util.h"
 #include "rconn.h"
+#include "timeval.h"
 #include "vconn.h"
 #include "vconn-ssl.h"
 #include "vlog-socket.h"
@@ -60,23 +65,35 @@ static struct datapath *dp;
 static uint64_t dpid = UINT64_MAX;
 static char *port_list;
 
+/* --max-backoff: Maximum interval between controller connection attempts, in
+ * seconds. */
+static int max_backoff = 15;
+
 static void add_ports(struct datapath *dp, char *port_list);
 
 int
 main(int argc, char *argv[])
 {
+    struct rconn *rconn;
     int error;
 
     set_program_name(argv[0]);
     register_fault_handlers();
+    time_init();
     vlog_init();
     parse_options(argc, argv);
+    signal(SIGPIPE, SIG_IGN);
 
     if (argc - optind != 1) {
         fatal(0, "missing controller argument; use --help for usage");
     }
 
-    error = dp_new(&dp, dpid, rconn_new(argv[optind], 128));
+    rconn = rconn_create(60, max_backoff);
+    error = rconn_connect(rconn, argv[optind]);
+    if (error == EAFNOSUPPORT) {
+        fatal(0, "no support for %s vconn", argv[optind]);
+    }
+    error = dp_new(&dp, dpid, rconn);
     if (listen_vconn_name) {
         struct vconn *listen_vconn;
         int retval;
@@ -102,6 +119,8 @@ main(int argc, char *argv[])
         fatal(error, "could not listen for vlog connections");
     }
 
+    daemonize();
+
     for (;;) {
         dp_run(dp);
         dp_wait(dp);
@@ -132,10 +151,17 @@ add_ports(struct datapath *dp, char *port_list)
 static void
 parse_options(int argc, char *argv[])
 {
+    enum {
+        OPT_MAX_BACKOFF = UCHAR_MAX + 1
+    };
+
     static struct option long_options[] = {
         {"interfaces",  required_argument, 0, 'i'},
         {"datapath-id", required_argument, 0, 'd'},
+        {"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'},
@@ -173,6 +199,14 @@ parse_options(int argc, char *argv[])
             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
             exit(EXIT_SUCCESS);
 
+        case 'D':
+            set_detach();
+            break;
+
+        case 'P':
+            set_pidfile(optarg);
+            break;
+
         case 'v':
             vlog_set_verbosity(optarg);
             break;
@@ -185,6 +219,15 @@ parse_options(int argc, char *argv[])
             }
             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 'l':
             if (listen_vconn_name) {
                 fatal(0, "-l or --listen may be only specified once");
@@ -217,11 +260,17 @@ usage(void)
            "                          add specified initial switch ports\n"
            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
            "                          (ID must consist of 12 hex digits)\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/switch.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);
 }