Update copyright on all non-GPL files
[openvswitch] / controller / controller.c
index 6c89f0605fbb9234258fabcdb127cba209563e46..eae2f0a418003ac90b9980e2450ad3e1ba54cb50 100644 (file)
@@ -1,22 +1,34 @@
-/* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
+/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+ * Junior University
+ * 
+ * We are making the OpenFlow specification and associated documentation
+ * (Software) available for public use and benefit with the expectation
+ * that others will use, modify and enhance the Software and contribute
+ * those enhancements back to the community. However, since we would
+ * like to make the Software available for broadest use, with as few
+ * restrictions as possible permission is hereby granted, free of
+ * charge, to any person obtaining a copy of this Software to deal in
+ * the Software under the copyrights without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * 
+ * The name and trademarks of copyright holder(s) may NOT be used in
+ * advertising or publicity pertaining to the Software or any
+ * derivatives without specific, written prior permission.
  */
 
 #include <assert.h>
 #include "mac.h"
 #include "ofp-print.h"
 #include "openflow.h"
+#include "poll-loop.h"
+#include "queue.h"
 #include "time.h"
 #include "util.h"
+#include "vconn-ssl.h"
 #include "vconn.h"
 #include "vlog-socket.h"
 #include "xtoxll.h"
 struct switch_ {
     char *name;
     struct vconn *vconn;
-    struct pollfd *pollfd;
 
     uint64_t datapath_id;
     time_t last_control_hello;
 
-    int n_txq;
-    struct buffer *txq, *tx_tail;
+    struct queue txq;
 };
 
 /* -H, --hub: Use dumb hub instead of learning switch? */
@@ -94,8 +107,6 @@ int
 main(int argc, char *argv[])
 {
     struct switch_ *switches[MAX_SWITCHES];
-    struct pollfd pollfds[MAX_SWITCHES + 1];
-    struct vlog_server *vlog_server;
     int n_switches;
     int retval;
     int i;
@@ -113,7 +124,7 @@ main(int argc, char *argv[])
         fatal(0, "at least one vconn argument required; use --help for usage");
     }
 
-    retval = vlog_server_listen(NULL, &vlog_server);
+    retval = vlog_server_listen(NULL, NULL);
     if (retval) {
         fatal(retval, "Could not listen for vlog connections");
     }
@@ -131,99 +142,67 @@ main(int argc, char *argv[])
     if (n_switches == 0) {
         fatal(0, "could not connect to any switches");
     }
-    
-    while (n_switches > 0) {
-        size_t n_ready;
-        int retval;
-
-        /* Wait until there's something to do. */
-        n_ready = 0;
-        for (i = 0; i < n_switches; i++) {
-            struct switch_ *this = switches[i];
-            int want;
-
-            if (vconn_is_passive(this->vconn)) {
-                want = n_switches < MAX_SWITCHES ? WANT_ACCEPT : 0;
-            } else {
-                want = WANT_RECV;
-                if (this->n_txq) {
-                    want |= WANT_SEND;
-                }
-            }
-
-            this->pollfd = &pollfds[i];
-            this->pollfd->fd = -1;
-            this->pollfd->events = 0;
-            n_ready += vconn_prepoll(this->vconn, want, this->pollfd);
-        }
-        if (vlog_server) {
-            pollfds[n_switches].fd = vlog_server_get_fd(vlog_server);
-            pollfds[n_switches].events = POLLIN;
-        }
-        do {
-            retval = poll(pollfds, n_switches + (vlog_server != NULL),
-                          n_ready ? 0 : -1);
-        } while (retval < 0 && errno == EINTR);
-        if (retval < 0 || (retval == 0 && !n_ready)) {
-            fatal(retval < 0 ? errno : 0, "poll");
-        }
 
-        /* Let each connection deal with any pending operations. */
-        for (i = 0; i < n_switches; i++) {
-            struct switch_ *this = switches[i];
-            vconn_postpoll(this->vconn, &this->pollfd->revents);
-            if (this->pollfd->revents & POLLERR) {
-                this->pollfd->revents |= POLLIN | POLLOUT;
-            }
-        }
-        if (vlog_server && pollfds[n_switches].revents) {
-            vlog_server_poll(vlog_server);
-        }
-
-        for (i = 0; i < n_switches; ) {
-            struct switch_ *this = switches[i];
+    while (n_switches > 0) {
+        /* Do some work.  Limit the number of iterations so that callbacks
+         * registered with the poll loop don't starve. */
+        int iteration;
+        int i;
+        for (iteration = 0; iteration < 50; iteration++) {
+            bool progress = false;
+            for (i = 0; i < n_switches; ) {
+                struct switch_ *this = switches[i];
+                int retval;
 
-            if (this->pollfd) {
-                retval = 0;
                 if (vconn_is_passive(this->vconn)) {
-                    if (this->pollfd->revents & POLLIN) {
+                    retval = 0;
+                    while (n_switches < MAX_SWITCHES) {
                         struct vconn *new_vconn;
-                        while (n_switches < MAX_SWITCHES 
-                               && (retval = vconn_accept(this->vconn,
-                                                         &new_vconn)) == 0) {
-                            switches[n_switches++] = new_switch("tcp",
-                                                                new_vconn);
+                        retval = vconn_accept(this->vconn, &new_vconn);
+                        if (retval) {
+                            break;
                         }
+                        switches[n_switches++] = new_switch("tcp", new_vconn);
                     }
                 } else {
-                    bool may_read = this->pollfd->revents & POLLIN;
-                    bool may_write = this->pollfd->revents & POLLOUT;
-                    if (may_read) {
-                        retval = do_switch_recv(this);
-                        if (!retval || retval == EAGAIN) {
-                            retval = 0;
-
-                            /* Enable writing to avoid round trip through poll
-                             * in common case. */
-                            may_write = true;
-                        }
-                    }
-                    while ((!retval || retval == EAGAIN) && may_write) {
-                        retval = do_switch_send(this);
-                        may_write = !retval;
+                    retval = do_switch_recv(this);
+                    if (!retval || retval == EAGAIN) {
+                        do {
+                            retval = do_switch_send(this);
+                            if (!retval) {
+                                progress = true;
+                            }
+                        } while (!retval);
                     }
                 }
 
                 if (retval && retval != EAGAIN) {
                     close_switch(this);
                     switches[i] = switches[--n_switches];
-                    continue;
+                } else {
+                    i++;
+                }
+            }
+            if (!progress) {
+                break;
+            }
+        }
+
+        /* Wait for something to happen. */
+        for (i = 0; i < n_switches; i++) {
+            struct switch_ *this = switches[i];
+            if (vconn_is_passive(this->vconn)) {
+                if (n_switches < MAX_SWITCHES) {
+                    vconn_accept_wait(this->vconn);
                 }
             } else {
-                /* New switch that hasn't been polled yet. */
+                vconn_recv_wait(this->vconn);
+                if (this->txq.n) {
+                    vconn_send_wait(this->vconn);
+                }
             }
-            i++;
         }
+        poll_block();
     }
 
     return 0;
@@ -247,19 +226,13 @@ static int
 do_switch_send(struct switch_ *this) 
 {
     int retval = 0;
-    if (this->n_txq) {
-        struct buffer *next = this->txq->next;
-
-        retval = vconn_send(this->vconn, this->txq);
+    if (this->txq.n) {
+        struct buffer *next = this->txq.head->next;
+        retval = vconn_send(this->vconn, this->txq.head);
         if (retval) {
             return retval;
         }
-
-        this->txq = next;
-        if (this->txq == NULL) {
-            this->tx_tail = NULL;
-        }
-        this->n_txq--;
+        queue_advance_head(&this->txq, next);
         return 0;
     }
     return EAGAIN;
@@ -287,10 +260,7 @@ new_switch(const char *name, struct vconn *vconn)
     memset(this, 0, sizeof *this);
     this->name = xstrdup(name);
     this->vconn = vconn;
-    this->pollfd = NULL;
-    this->n_txq = 0;
-    this->txq = NULL;
-    this->tx_tail = NULL;
+    queue_init(&this->txq);
     this->last_control_hello = 0;
     if (!vconn_is_passive(vconn)) {
         send_control_hello(this);
@@ -302,14 +272,9 @@ static void
 close_switch(struct switch_ *this) 
 {
     if (this) {
-        struct buffer *cur, *next;
-
         free(this->name);
         vconn_close(this->vconn);
-        for (cur = this->txq; cur != NULL; cur = next) {
-            next = cur->next;
-            buffer_delete(cur);
-        }
+        queue_destroy(&this->txq);
         free(this);
     }
 }
@@ -337,40 +302,10 @@ send_control_hello(struct switch_ *this)
     }
 }
 
-static void
-check_txq(struct switch_ *this UNUSED)
-{
-#if 0
-    struct buffer *iter;
-    size_t n;
-
-    assert(this->n_txq == 0
-           ? this->txq == NULL && this->tx_tail == NULL
-           : this->txq != NULL && this->tx_tail != NULL);
-
-    n = 0;
-    for (iter = this->txq; iter != NULL; iter = iter->next) {
-        n++;
-        assert((iter->next != NULL) == (iter != this->tx_tail));
-    }
-    assert(n == this->n_txq);
-#endif
-}
-
 static void
 queue_tx(struct switch_ *this, struct buffer *b) 
 {
-    check_txq(this);
-
-    b->next = NULL;
-    if (this->n_txq++) {
-        this->tx_tail->next = b;
-    } else {
-        this->txq = b;
-    }
-    this->tx_tail = b;
-
-    check_txq(this);
+    queue_push_tail(&this->txq, b);
 }
 
 static void
@@ -408,7 +343,8 @@ process_packet(struct switch_ *sw, struct buffer *msg)
     }
 
     if (oh->type == OFPT_PACKET_IN) {
-        if (sw->n_txq >= MAX_TXQ) {
+        if (sw->txq.n >= MAX_TXQ) {
+            /* FIXME: ratelimit. */
             VLOG_WARN("%s: tx queue overflow", sw->name);
         } else if (noflow) {
             process_noflow(sw, msg->data);
@@ -695,7 +631,7 @@ usage(void)
            "usage: %s [OPTIONS] VCONN\n"
            "where VCONN is one of the following:\n"
            "  ptcp:[PORT]             listen to TCP PORT (default: %d)\n",
-           program_name, program_name);
+           program_name, program_name, OFP_TCP_PORT);
 #ifdef HAVE_NETLINK
     printf("  nl:DP_IDX               via netlink to local datapath DP_IDX\n");
 #endif
@@ -712,7 +648,6 @@ usage(void)
            "  -n, --noflow            pass traffic, but don't add flows\n"
            "  -v, --verbose           set maximum verbosity level\n"
            "  -h, --help              display this help message\n"
-           "  -V, --version           display version information\n",
-           OFP_TCP_PORT);
+           "  -V, --version           display version information\n");
     exit(EXIT_SUCCESS);
 }