Fix "make dist" by adding forgotten files to sources lists.
[sliver-openvswitch.git] / switch / switch.c
index abd76b6..fba8858 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"
+#include "openflow/openflow.h"
 #include "poll-loop.h"
 #include "queue.h"
 #include "util.h"
 #include "rconn.h"
+#include "timeval.h"
 #include "vconn.h"
 #include "vconn-ssl.h"
 #include "vlog-socket.h"
 #define THIS_MODULE VLM_switch
 #include "vlog.h"
 
+
+/* Strings to describe the manufacturer, hardware, and software.  This data 
+ * is queriable through the switch description stats message. */
+char mfr_desc[DESC_STR_LEN] = "Nicira Networks";
+char hw_desc[DESC_STR_LEN] = "Reference User-Space Switch";
+char sw_desc[DESC_STR_LEN] = VERSION;
+char serial_num[SERIAL_NUM_LEN] = "None";
+
 static void parse_options(int argc, char *argv[]);
 static void usage(void) NO_RETURN;
 
+static const char *listen_pvconn_name;
 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");
+        ofp_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) {
+        ofp_fatal(0, "no support for %s vconn", argv[optind]);
+    }
+    error = dp_new(&dp, dpid, rconn);
+    if (listen_pvconn_name) {
+        struct pvconn *listen_pvconn;
+        int retval;
+
+        retval = pvconn_open(listen_pvconn_name, &listen_pvconn);
+        if (retval && retval != EAGAIN) {
+            ofp_fatal(retval, "opening %s", listen_pvconn_name);
+        }
+        dp_add_listen_pvconn(dp, listen_pvconn);
+    }
     if (error) {
-        fatal(error, "could not create datapath");
+        ofp_fatal(error, "could not create datapath");
     }
     if (port_list) {
         add_ports(dp, port_list); 
     }
 
+    die_if_already_running();
+    daemonize();
+
     error = vlog_server_listen(NULL, NULL);
     if (error) {
-        fatal(error, "could not listen for vlog connections");
+        ofp_fatal(error, "could not listen for vlog connections");
     }
 
     for (;;) {
@@ -110,7 +149,7 @@ add_ports(struct datapath *dp, char *port_list)
          port = strtok_r(NULL, ",,", &save_ptr)) {
         int error = dp_add_port(dp, port);
         if (error) {
-            fatal(error, "failed to add port %s", port);
+            ofp_fatal(error, "failed to add port %s", port);
         }
     }
 }
@@ -118,13 +157,32 @@ add_ports(struct datapath *dp, char *port_list)
 static void
 parse_options(int argc, char *argv[])
 {
+    enum {
+        OPT_MAX_BACKOFF = UCHAR_MAX + 1,
+        OPT_MFR_DESC,
+        OPT_HW_DESC,
+        OPT_SW_DESC,
+        OPT_SERIAL_NUM,
+        OPT_BOOTSTRAP_CA_CERT
+    };
+
     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'},
         {"verbose",     optional_argument, 0, 'v'},
         {"help",        no_argument, 0, 'h'},
         {"version",     no_argument, 0, 'V'},
+        {"mfr-desc",    required_argument, 0, OPT_MFR_DESC},
+        {"hw-desc",     required_argument, 0, OPT_HW_DESC},
+        {"sw-desc",     required_argument, 0, OPT_SW_DESC},
+        {"serial_num",  required_argument, 0, OPT_SERIAL_NUM},
+        DAEMON_LONG_OPTIONS,
+#ifdef HAVE_OPENSSL
         VCONN_SSL_LONG_OPTIONS
+        {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
+#endif
         {0, 0, 0, 0},
     };
     char *short_options = long_options_to_short_options(long_options);
@@ -142,12 +200,13 @@ parse_options(int argc, char *argv[])
         case 'd':
             if (strlen(optarg) != 12
                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
-                fatal(0, "argument to -d or --datapath-id must be "
-                      "exactly 12 hex digits");
+                ofp_fatal(0, "argument to -d or --datapath-id must be "
+                          "exactly 12 hex digits");
             }
             dpid = strtoll(optarg, NULL, 16);
             if (!dpid) {
-                fatal(0, "argument to -d or --datapath-id must be nonzero");
+                ofp_fatal(0, "argument to -d or --datapath-id must "
+                          "be nonzero");
             }
             break;
 
@@ -155,7 +214,8 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
+            printf("%s %s compiled "__DATE__" "__TIME__"\n",
+                   program_name, VERSION BUILDNR);
             exit(EXIT_SUCCESS);
 
         case 'v':
@@ -170,8 +230,48 @@ parse_options(int argc, char *argv[])
             }
             break;
 
+        case OPT_MAX_BACKOFF:
+            max_backoff = atoi(optarg);
+            if (max_backoff < 1) {
+                ofp_fatal(0, "--max-backoff argument must be at least 1");
+            } else if (max_backoff > 3600) {
+                max_backoff = 3600;
+            }
+            break;
+
+        case OPT_MFR_DESC:
+            strncpy(mfr_desc, optarg, sizeof mfr_desc);
+            break;
+
+        case OPT_HW_DESC:
+            strncpy(hw_desc, optarg, sizeof hw_desc);
+            break;
+
+        case OPT_SW_DESC:
+            strncpy(sw_desc, optarg, sizeof sw_desc);
+            break;
+
+        case OPT_SERIAL_NUM:
+            strncpy(serial_num, optarg, sizeof serial_num);
+            break;
+
+        case 'l':
+            if (listen_pvconn_name) {
+                ofp_fatal(0, "-l or --listen may be only specified once");
+            }
+            listen_pvconn_name = optarg;
+            break;
+
+        DAEMON_OPTION_HANDLERS
+
+#ifdef HAVE_OPENSSL
         VCONN_SSL_OPTION_HANDLERS
 
+        case OPT_BOOTSTRAP_CA_CERT:
+            vconn_ssl_set_ca_cert_file(optarg, true);
+            break;
+#endif
+
         case '?':
             exit(EXIT_FAILURE);
 
@@ -189,13 +289,19 @@ usage(void)
            "usage: %s [OPTIONS] CONTROLLER\n"
            "where CONTROLLER is an active OpenFlow connection method.\n",
            program_name, program_name);
-    vconn_usage(true, false);
-    printf("\nOptions:\n"
+    vconn_usage(true, true, true);
+    printf("\nConfiguration options:\n"
            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
            "                          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"
-           "  -v, --verbose           set maximum verbosity level\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");
+    daemon_usage();
+    vlog_usage();
+    printf("\nOther options:\n"
            "  -h, --help              display this help message\n"
            "  -V, --version           display version information\n");
     exit(EXIT_SUCCESS);