Don't use separate asynchronous event connection for user datapath.
[sliver-openvswitch.git] / secchan / secchan.c
index f1ac6d2..bf1aaca 100644 (file)
@@ -61,6 +61,7 @@
 #ifdef SUPPORT_SNAT
 #include "snat.h"
 #endif
+#include "flow-end.h"
 #include "stp-secchan.h"
 #include "status.h"
 #include "timeval.h"
@@ -87,10 +88,12 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
 static void parse_options(int argc, char *argv[], struct settings *);
 static void usage(void) NO_RETURN;
 
+static char *vconn_name_without_subscription(const char *);
 static struct pvconn *open_passive_vconn(const char *name);
 static struct vconn *accept_vconn(struct pvconn *pvconn);
 
-static struct relay *relay_create(struct rconn *local, struct rconn *remote,
+static struct relay *relay_create(struct rconn *async,
+                                  struct rconn *local, struct rconn *remote,
                                   bool is_mgmt_conn);
 static struct relay *relay_accept(const struct settings *, struct pvconn *);
 static void relay_run(struct relay *, struct secchan *);
@@ -111,7 +114,8 @@ main(int argc, char *argv[])
     struct pvconn *listeners[MAX_MGMT];
     size_t n_listeners;
 
-    struct rconn *local_rconn, *remote_rconn;
+    char *local_rconn_name;
+    struct rconn *async_rconn, *local_rconn, *remote_rconn;
     struct relay *controller_relay;
     struct discovery *discovery;
     struct switch_status *switch_status;
@@ -140,26 +144,50 @@ main(int argc, char *argv[])
     /* Initialize switch status hook. */
     switch_status_start(&secchan, &s, &switch_status);
 
+    die_if_already_running();
+    daemonize();
+
     /* Start listening for vlogconf requests. */
     retval = vlog_server_listen(NULL, NULL);
     if (retval) {
         ofp_fatal(retval, "Could not listen for vlog connections");
     }
 
-    die_if_already_running();
-    daemonize();
-
-    VLOG_WARN("OpenFlow reference implementation version %s", VERSION);
+    VLOG_WARN("OpenFlow reference implementation version %s", VERSION BUILDNR);
     VLOG_WARN("OpenFlow protocol version 0x%02x", OFP_VERSION);
 
-    /* Connect to datapath. */
+    /* Check datapath name, to try to catch command-line invocation errors. */
     if (strncmp(s.dp_name, "nl:", 3) && strncmp(s.dp_name, "unix:", 5)
         && !s.controller_name) {
         VLOG_WARN("Controller not specified and datapath is not nl: or "
                   "unix:.  (Did you forget to specify the datapath?)");
     }
+
+    if (!strncmp(s.dp_name, "nl:", 3)) {
+        /* Connect to datapath with a subscription for asynchronous events.  By
+         * separating the connection for asynchronous events from that for
+         * request and replies we prevent the socket receive buffer from being
+         * filled up by received packet data, which in turn would prevent
+         * getting replies to any Netlink messages we send to the kernel. */
+        async_rconn = rconn_create(0, s.max_backoff);
+        rconn_connect(async_rconn, s.dp_name);
+        switch_status_register_category(switch_status, "async",
+                                        rconn_status_cb, async_rconn);
+    } else {
+        /* No need for a separate asynchronous connection: we must be connected
+         * to the user datapath, which is smart enough to discard packet events
+         * instead of message replies.  In fact, having a second connection
+         * would work against us since we'd get double copies of asynchronous
+         * event messages (the user datapath provides no way to turn off
+         * asynchronous events). */
+        async_rconn = NULL;
+    }
+
+    /* Connect to datapath without a subscription, for requests and replies. */
+    local_rconn_name = vconn_name_without_subscription(s.dp_name);
     local_rconn = rconn_create(0, s.max_backoff);
-    rconn_connect(local_rconn, s.dp_name);
+    rconn_connect(local_rconn, local_rconn_name);
+    free(local_rconn_name);
     switch_status_register_category(switch_status, "local",
                                     rconn_status_cb, local_rconn);
 
@@ -175,7 +203,8 @@ main(int argc, char *argv[])
                                     rconn_status_cb, remote_rconn);
 
     /* Start relaying. */
-    controller_relay = relay_create(local_rconn, remote_rconn, false);
+    controller_relay = relay_create(async_rconn, local_rconn, remote_rconn,
+                                    false);
     list_push_back(&relays, &controller_relay->node);
 
     /* Set up hooks. */
@@ -184,6 +213,7 @@ main(int argc, char *argv[])
 #ifdef SUPPORT_SNAT
     snat_start(&secchan, pw);
 #endif
+    flow_end_start(&secchan, s.netflow_dst, local_rconn, remote_rconn);
     if (s.enable_stp) {
         stp_start(&secchan, &s, pw, local_rconn, remote_rconn);
     }
@@ -195,8 +225,7 @@ main(int argc, char *argv[])
                         local_rconn, remote_rconn);
     }
     if (s.rate_limit) {
-        rate_limit_start(&secchan, &s, switch_status,
-                         local_rconn, remote_rconn);
+        rate_limit_start(&secchan, &s, switch_status, remote_rconn);
     }
     if (s.command_acl[0]) {
         executer_start(&secchan, &s);
@@ -222,6 +251,8 @@ main(int argc, char *argv[])
         if (monitor) {
             struct vconn *new = accept_vconn(monitor);
             if (new) {
+                /* XXX should monitor async_rconn too but rconn_add_monitor()
+                 * takes ownership of the vconn passed in. */
                 rconn_add_monitor(local_rconn, new);
             }
         }
@@ -342,36 +373,43 @@ get_ofp_packet_eth_header(struct relay *r, struct ofp_packet_in **opip,
 \f
 /* OpenFlow message relaying. */
 
-static struct relay *
-relay_accept(const struct settings *s, struct pvconn *pvconn)
+/* Returns a malloc'd string containing a copy of 'vconn_name' modified not to
+ * subscribe to asynchronous messages such as 'ofp_packet_in' events (if
+ * possible). */
+static char *
+vconn_name_without_subscription(const char *vconn_name)
 {
-    struct vconn *new_remote, *new_local;
-    struct rconn *r1, *r2;
-    char *vconn_name;
     int nl_index;
-    int retval;
-
-    new_remote = accept_vconn(pvconn);
-    if (!new_remote) {
-        return NULL;
-    }
-
-    if (sscanf(s->dp_name, "nl:%d", &nl_index) == 1) {
+    if (sscanf(vconn_name, "nl:%d", &nl_index) == 1) {
         /* nl:123 or nl:123:1 opens a netlink connection to local datapath 123.
          * nl:123:0 opens a netlink connection to local datapath 123 without
          * obtaining a subscription for ofp_packet_in or ofp_flow_expired
-         * messages.  That's what we want here; management connections should
-         * not receive those messages, at least by default. */
-        vconn_name = xasprintf("nl:%d:0", nl_index);
+         * messages. */
+        return xasprintf("nl:%d:0", nl_index);
     } else {
         /* We don't have a way to specify not to subscribe to those messages
          * for other transports.  (That's a defect: really this should be in
          * the OpenFlow protocol, not the Netlink transport). */
         VLOG_WARN_RL(&rl, "new management connection will receive "
                      "asynchronous messages");
-        vconn_name = xstrdup(s->dp_name);
+        return xstrdup(vconn_name);
     }
+}
 
+static struct relay *
+relay_accept(const struct settings *s, struct pvconn *pvconn)
+{
+    struct vconn *new_remote, *new_local;
+    struct rconn *r1, *r2;
+    char *vconn_name;
+    int retval;
+
+    new_remote = accept_vconn(pvconn);
+    if (!new_remote) {
+        return NULL;
+    }
+
+    vconn_name = vconn_name_without_subscription(s->dp_name);
     retval = vconn_open(vconn_name, OFP_VERSION, &new_local);
     if (retval) {
         VLOG_ERR_RL(&rl, "could not connect to %s (%s)",
@@ -389,16 +427,18 @@ relay_accept(const struct settings *s, struct pvconn *pvconn)
     r2 = rconn_create(0, 0);
     rconn_connect_unreliably(r2, "passive", new_remote);
 
-    return relay_create(r1, r2, true);
+    return relay_create(NULL, r1, r2, true);
 }
 
 static struct relay *
-relay_create(struct rconn *local, struct rconn *remote, bool is_mgmt_conn)
+relay_create(struct rconn *async, struct rconn *local, struct rconn *remote,
+             bool is_mgmt_conn)
 {
     struct relay *r = xcalloc(1, sizeof *r);
     r->halves[HALF_LOCAL].rconn = local;
     r->halves[HALF_REMOTE].rconn = remote;
     r->is_mgmt_conn = is_mgmt_conn;
+    r->async_rconn = async;
     return r;
 }
 
@@ -434,6 +474,9 @@ relay_run(struct relay *r, struct secchan *secchan)
     int iteration;
     int i;
 
+    if (r->async_rconn) {
+        rconn_run(r->async_rconn);
+    }
     for (i = 0; i < 2; i++) {
         rconn_run(r->halves[i].rconn);
     }
@@ -447,6 +490,9 @@ relay_run(struct relay *r, struct secchan *secchan)
 
             if (!this->rxbuf) {
                 this->rxbuf = rconn_recv(this->rconn);
+                if (!this->rxbuf && i == HALF_LOCAL && r->async_rconn) {
+                    this->rxbuf = rconn_recv(r->async_rconn);
+                }
                 if (this->rxbuf && (i == HALF_REMOTE || !r->is_mgmt_conn)) {
                     if (i == HALF_LOCAL
                         ? call_local_packet_cbs(secchan, r)
@@ -494,12 +540,18 @@ relay_wait(struct relay *r)
 {
     int i;
 
+    if (r->async_rconn) {
+        rconn_run_wait(r->async_rconn);
+    }
     for (i = 0; i < 2; i++) {
         struct half *this = &r->halves[i];
 
         rconn_run_wait(this->rconn);
         if (!this->rxbuf) {
             rconn_recv_wait(this->rconn);
+            if (i == HALF_LOCAL && r->async_rconn) {
+                rconn_recv_wait(r->async_rconn);
+            }
         }
     }
 }
@@ -510,6 +562,7 @@ relay_destroy(struct relay *r)
     int i;
 
     list_remove(&r->node);
+    rconn_destroy(r->async_rconn);
     for (i = 0; i < 2; i++) {
         struct half *this = &r->halves[i];
         rconn_destroy(this->rconn);
@@ -538,6 +591,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         OPT_IN_BAND,
         OPT_COMMAND_ACL,
         OPT_COMMAND_DIR,
+        OPT_NETFLOW,
         VLOG_OPTION_ENUMS
     };
     static struct option long_options[] = {
@@ -557,6 +611,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         {"in-band",     no_argument, 0, OPT_IN_BAND},
         {"command-acl", required_argument, 0, OPT_COMMAND_ACL},
         {"command-dir", required_argument, 0, OPT_COMMAND_DIR},
+        {"netflow",     required_argument, 0, OPT_NETFLOW},
         {"verbose",     optional_argument, 0, 'v'},
         {"help",        no_argument, 0, 'h'},
         {"version",     no_argument, 0, 'V'},
@@ -586,6 +641,7 @@ parse_options(int argc, char *argv[], struct settings *s)
     s->in_band = true;
     s->command_acl = "";
     s->command_dir = xasprintf("%s/commands", ofp_pkgdatadir);
+    s->netflow_dst = NULL;
     for (;;) {
         int c;
 
@@ -686,6 +742,13 @@ parse_options(int argc, char *argv[], struct settings *s)
             s->command_dir = optarg;
             break;
 
+        case OPT_NETFLOW:
+            if (s->netflow_dst) {
+                ofp_fatal(0, "--netflow may only be specified once");
+            }
+            s->netflow_dst = optarg;
+            break;
+
         case 'l':
             if (s->n_listeners >= MAX_MGMT) {
                 ofp_fatal(0,
@@ -706,7 +769,8 @@ parse_options(int argc, char *argv[], struct settings *s)
             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);
 
         DAEMON_OPTION_HANDLERS
@@ -779,8 +843,8 @@ static void
 usage(void)
 {
     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"
+           "usage: %s [OPTIONS] DATAPATH [CONTROLLER]\n"
+           "DATAPATH is an active connection method to a local datapath.\n"
            "CONTROLLER is an active OpenFlow connection method; if it is\n"
            "omitted, then secchan performs controller discovery.\n",
            program_name, program_name);
@@ -803,6 +867,7 @@ usage(void)
            "  --out-of-band           controller connection is out-of-band\n"
            "  --stp                   enable 802.1D Spanning Tree Protocol\n"
            "  --no-stp                disable 802.1D Spanning Tree Protocol\n"
+           "  --netflow=HOST:PORT     send NetFlow v5 messages when flows end\n"
            "\nRate-limiting of \"packet-in\" messages to the controller:\n"
            "  --rate-limit[=PACKETS]  max rate, in packets/s (default: 1000)\n"
            "  --burst-limit=BURST     limit on packet credit for idle time\n"