Merge branch 'mainstream'
authorGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Mon, 24 Sep 2012 13:26:21 +0000 (15:26 +0200)
committerGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Mon, 24 Sep 2012 13:26:21 +0000 (15:26 +0200)
20 files changed:
.gitignore
.non-distfiles [new file with mode: 0644]
Makefile.am
lib/automake.mk
lib/dpif-netdev.c
lib/dpif-provider.h
lib/dpif.c
lib/netdev-pltap.c [new file with mode: 0644]
lib/netdev-provider.h
lib/netdev-tunnel.c [new file with mode: 0644]
lib/netdev.c
lib/tunalloc.c [new file with mode: 0644]
lib/tunalloc.h [new file with mode: 0644]
planetlab/automake.mk [new file with mode: 0644]
planetlab/exp-tool/Makefile [new file with mode: 0644]
planetlab/exp-tool/README [new file with mode: 0644]
planetlab/exp-tool/conf.mk.example [new file with mode: 0644]
planetlab/exp-tool/showgraph [new file with mode: 0755]
planetlab/scripts/sliver-ovs.in [new file with mode: 0755]
sliver-openvswitch.spec [new file with mode: 0644]

index 702cc6c..cc8cb23 100644 (file)
@@ -49,3 +49,4 @@ Module.symvers
 TAGS
 cscope.*
 tags
+myexp/
diff --git a/.non-distfiles b/.non-distfiles
new file mode 100644 (file)
index 0000000..0c43af5
--- /dev/null
@@ -0,0 +1,7 @@
+planetlab/exp-tool
+planetlab/exp-tool/Makefile
+planetlab/exp-tool/showgraph
+planetlab/exp-tool/README
+sliver-openvswitch.spec
+.gitignore
+.non-distfiles
index 1b14871..887b1d7 100644 (file)
@@ -104,6 +104,7 @@ ro_shell = printf '\043 Generated automatically -- do not modify!    -*- buffer-
 
 SUFFIXES += .in
 .in:
+       @mkdir -p $$(dirname $@)
        $(PERL) $(srcdir)/build-aux/soexpand.pl -I$(srcdir) < $< | \
            sed \
                -e 's,[@]PKIDIR[@],$(PKIDIR),g' \
@@ -145,7 +146,7 @@ dist-hook-git: distfiles
          (cd datapath && $(MAKE) distfiles);                               \
          (cat distfiles; sed 's|^|datapath/|' datapath/distfiles) |        \
            sort -u > all-distfiles;                                        \
-         (cd $(srcdir) && git ls-files) | grep -v '\.gitignore$$' |        \
+         (cd $(srcdir) && git ls-files) | grep -vFf $(srcdir)/.non-distfiles |     \
            sort -u > all-gitfiles;                                         \
          comm -1 -3 all-distfiles all-gitfiles > missing-distfiles;        \
          if test -s missing-distfiles; then                                \
@@ -223,3 +224,4 @@ include rhel/automake.mk
 include xenserver/automake.mk
 include python/automake.mk
 include python/compat/automake.mk
+include planetlab/automake.mk
index 94b86f6..238ba34 100644 (file)
@@ -88,6 +88,8 @@ lib_libopenvswitch_a_SOURCES = \
        lib/multipath.c \
        lib/multipath.h \
        lib/netdev-dummy.c \
+       lib/netdev-tunnel.c \
+       lib/netdev-pltap.c \
        lib/netdev-provider.h \
        lib/netdev.c \
        lib/netdev.h \
@@ -181,6 +183,8 @@ lib_libopenvswitch_a_SOURCES = \
        lib/timeval.h \
        lib/token-bucket.c \
        lib/token-bucket.h \
+       lib/tunalloc.c \
+       lib/tunalloc.h \
        lib/type-props.h \
        lib/unaligned.h \
        lib/unicode.c \
index 144b6b6..28f64b1 100644 (file)
@@ -316,6 +316,15 @@ dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
     return 0;
 }
 
+static const char* internal_port_type(const struct dp_netdev* dp)
+{
+       if (dp->class == &dpif_netdev_class)
+               return "tap";
+       if (dp->class == &dpif_planetlab_class)
+               return "pltap";
+       return "dummy";
+}
+
 static int
 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
             uint16_t port_no)
@@ -329,9 +338,7 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
     /* XXX reject devices already in some dp_netdev. */
 
     /* Open and validate network device. */
-    open_type = (strcmp(type, "internal") ? type
-                 : dp->class != &dpif_netdev_class ? "dummy"
-                 : "tap");
+    open_type = (strcmp(type, "internal") ? type : internal_port_type(dp));
     error = netdev_open(devname, open_type, &netdev);
     if (error) {
         return error;
@@ -376,7 +383,9 @@ choose_port(struct dpif *dpif, struct netdev *netdev)
     struct dp_netdev *dp = get_dp_netdev(dpif);
     int port_no;
 
-    if (dpif->dpif_class != &dpif_netdev_class) {
+    if (dpif->dpif_class != &dpif_netdev_class &&
+        dpif->dpif_class != &dpif_planetlab_class)
+    {
         /* If the port name contains a number, try to assign that port number.
          * This can make writing unit tests easier because port numbers are
          * predictable. */
@@ -1265,40 +1274,48 @@ dp_netdev_execute_actions(struct dp_netdev *dp,
     }
 }
 
+#define DPIF_NETDEV_CLASS_FUNCTIONS                    \
+    dpif_netdev_enumerate,                             \
+    dpif_netdev_open,                                  \
+    dpif_netdev_close,                                 \
+    dpif_netdev_destroy,                               \
+    dpif_netdev_run,                                   \
+    dpif_netdev_wait,                                  \
+    dpif_netdev_get_stats,                             \
+    dpif_netdev_port_add,                              \
+    dpif_netdev_port_del,                              \
+    dpif_netdev_port_query_by_number,                  \
+    dpif_netdev_port_query_by_name,                    \
+    dpif_netdev_get_max_ports,                         \
+    NULL,                       /* port_get_pid */     \
+    dpif_netdev_port_dump_start,                       \
+    dpif_netdev_port_dump_next,                                \
+    dpif_netdev_port_dump_done,                                \
+    dpif_netdev_port_poll,                             \
+    dpif_netdev_port_poll_wait,                                \
+    dpif_netdev_flow_get,                              \
+    dpif_netdev_flow_put,                              \
+    dpif_netdev_flow_del,                              \
+    dpif_netdev_flow_flush,                            \
+    dpif_netdev_flow_dump_start,                       \
+    dpif_netdev_flow_dump_next,                                \
+    dpif_netdev_flow_dump_done,                                \
+    dpif_netdev_execute,                               \
+    NULL,                       /* operate */          \
+    dpif_netdev_recv_set,                              \
+    dpif_netdev_queue_to_priority,                     \
+    dpif_netdev_recv,                                  \
+    dpif_netdev_recv_wait,                             \
+    dpif_netdev_recv_purge,                            \
+
 const struct dpif_class dpif_netdev_class = {
     "netdev",
-    dpif_netdev_enumerate,
-    dpif_netdev_open,
-    dpif_netdev_close,
-    dpif_netdev_destroy,
-    dpif_netdev_run,
-    dpif_netdev_wait,
-    dpif_netdev_get_stats,
-    dpif_netdev_port_add,
-    dpif_netdev_port_del,
-    dpif_netdev_port_query_by_number,
-    dpif_netdev_port_query_by_name,
-    dpif_netdev_get_max_ports,
-    NULL,                       /* port_get_pid */
-    dpif_netdev_port_dump_start,
-    dpif_netdev_port_dump_next,
-    dpif_netdev_port_dump_done,
-    dpif_netdev_port_poll,
-    dpif_netdev_port_poll_wait,
-    dpif_netdev_flow_get,
-    dpif_netdev_flow_put,
-    dpif_netdev_flow_del,
-    dpif_netdev_flow_flush,
-    dpif_netdev_flow_dump_start,
-    dpif_netdev_flow_dump_next,
-    dpif_netdev_flow_dump_done,
-    dpif_netdev_execute,
-    NULL,                       /* operate */
-    dpif_netdev_recv_set,
-    dpif_netdev_queue_to_priority,
-    dpif_netdev_recv,
-    dpif_netdev_recv_wait,
-    dpif_netdev_recv_purge,
+    DPIF_NETDEV_CLASS_FUNCTIONS
+};
+
+const struct dpif_class dpif_planetlab_class = {
+    "planetlab",
+    DPIF_NETDEV_CLASS_FUNCTIONS
 };
 
 static void
@@ -1331,3 +1348,4 @@ dpif_dummy_register(bool override)
 
     dpif_dummy_register__("dummy");
 }
+
index 317e617..f264e1b 100644 (file)
@@ -340,6 +340,7 @@ struct dpif_class {
 
 extern const struct dpif_class dpif_linux_class;
 extern const struct dpif_class dpif_netdev_class;
+extern const struct dpif_class dpif_planetlab_class;
 
 #ifdef  __cplusplus
 }
index 2968966..7be7b2a 100644 (file)
@@ -62,6 +62,7 @@ static const struct dpif_class *base_dpif_classes[] = {
     &dpif_linux_class,
 #endif
     &dpif_netdev_class,
+    &dpif_planetlab_class,
 };
 
 struct registered_dpif_class {
diff --git a/lib/netdev-pltap.c b/lib/netdev-pltap.c
new file mode 100644 (file)
index 0000000..5122551
--- /dev/null
@@ -0,0 +1,625 @@
+/*
+ * Copyright (c) 2012 Giuseppe Lettieri
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <arpa/inet.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <linux/if_tun.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "flow.h"
+#include "list.h"
+#include "netdev-provider.h"
+#include "odp-util.h"
+#include "ofp-print.h"
+#include "ofpbuf.h"
+#include "packets.h"
+#include "poll-loop.h"
+#include "shash.h"
+#include "sset.h"
+#include "unixctl.h"
+#include "socket-util.h"
+#include "vlog.h"
+#include "tunalloc.h"
+
+VLOG_DEFINE_THIS_MODULE(netdev_pltap);
+
+struct netdev_dev_pltap {
+    struct netdev_dev netdev_dev;
+    char *real_name;
+    char *error;
+    struct netdev_stats stats;
+    enum netdev_flags flags;
+    int fd;
+    struct sockaddr_in local_addr;
+    int local_netmask;
+    bool valid_local_ip;
+    bool valid_local_netmask;
+    bool finalized;
+    unsigned int change_seq;
+};
+
+struct netdev_pltap {
+    struct netdev netdev;
+} ;
+
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+
+static struct shash pltap_netdev_devs = SHASH_INITIALIZER(&pltap_netdev_devs);
+
+static int netdev_pltap_create(const struct netdev_class *, const char *,
+                               struct netdev_dev **);
+
+static struct shash pltap_creating = SHASH_INITIALIZER(&pltap_creating);
+
+static void netdev_pltap_update_seq(struct netdev_dev_pltap *);
+
+static bool
+is_pltap_class(const struct netdev_class *class)
+{
+    return class->create == netdev_pltap_create;
+}
+
+static struct netdev_dev_pltap *
+netdev_dev_pltap_cast(const struct netdev_dev *netdev_dev)
+{
+    assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
+    return CONTAINER_OF(netdev_dev, struct netdev_dev_pltap, netdev_dev);
+}
+
+static struct netdev_pltap *
+netdev_pltap_cast(const struct netdev *netdev)
+{
+    struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
+    assert(is_pltap_class(netdev_dev_get_class(netdev_dev)));
+    return CONTAINER_OF(netdev, struct netdev_pltap, netdev);
+}
+
+static int
+netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name,
+                    struct netdev_dev **netdev_devp)
+{
+    struct netdev_dev_pltap *netdev_dev;
+    int error;
+
+    netdev_dev = xzalloc(sizeof *netdev_dev);
+
+    netdev_dev->real_name = xzalloc(IFNAMSIZ + 1);
+    netdev_dev->error = NULL;
+    memset(&netdev_dev->local_addr, 0, sizeof(netdev_dev->local_addr));
+    netdev_dev->valid_local_ip = false;
+    netdev_dev->valid_local_netmask = false;
+    netdev_dev->finalized = false;
+
+
+    /* Open tap device. */
+    netdev_dev->fd = tun_alloc(IFF_TAP, netdev_dev->real_name);
+    if (netdev_dev->fd < 0) {
+        error = errno;
+        VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, strerror(error));
+        goto cleanup;
+    }
+    VLOG_DBG("real_name = %s", netdev_dev->real_name);
+
+    /* Make non-blocking. */
+    error = set_nonblocking(netdev_dev->fd);
+    if (error) {
+        goto cleanup;
+    }
+
+    netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_pltap_class);
+    shash_add(&pltap_netdev_devs, name, netdev_dev);
+    *netdev_devp = &netdev_dev->netdev_dev;
+    return 0;
+
+cleanup:
+    free(netdev_dev);
+    return error;
+}
+
+static void
+netdev_pltap_destroy(struct netdev_dev *netdev_dev_)
+{
+    struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(netdev_dev_);
+
+    if (netdev_dev->fd != -1)
+       close(netdev_dev->fd);
+
+    shash_find_and_delete(&pltap_netdev_devs,
+                          netdev_dev_get_name(netdev_dev_));
+    free(netdev_dev);
+}
+
+static int
+netdev_pltap_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
+{
+    struct netdev_pltap *netdev;
+
+    netdev = xmalloc(sizeof *netdev);
+    netdev_init(&netdev->netdev, netdev_dev_);
+
+    *netdevp = &netdev->netdev;
+    return 0;
+}
+
+static void
+netdev_pltap_close(struct netdev *netdev_)
+{
+    struct netdev_pltap *netdev = netdev_pltap_cast(netdev_);
+    free(netdev);
+}
+
+static int
+netdev_pltap_create_finalize(struct netdev_dev_pltap *dev)
+{
+    int ifd = -1, ofd = -1, maxfd;
+    size_t bytes_to_write, bytes_to_read = 1024,
+           bytes_written = 0, bytes_read = 0;
+    int error = 0;
+    char *msg = NULL, *reply = NULL;
+
+    if (dev->finalized)
+        return 0;
+    if (!dev->valid_local_ip || !dev->valid_local_netmask)
+        return 0;
+    
+    ofd = open("/vsys/vif_up.out", O_RDONLY | O_NONBLOCK);
+    if (ofd < 0) {
+        VLOG_ERR("Cannot open vif_up.out: %s", strerror(errno));
+       error = errno;
+       goto cleanup;
+    }
+    ifd = open("/vsys/vif_up.in", O_WRONLY | O_NONBLOCK);
+    if (ifd < 0) {
+        VLOG_ERR("Cannot open vif_up.in: %s", strerror(errno));
+       error = errno;
+       goto cleanup;
+    }
+    maxfd = (ifd < ofd) ? ofd : ifd;
+
+    msg = xasprintf("%s\n"IP_FMT"\n%d\n",
+       dev->real_name,
+       IP_ARGS(&dev->local_addr.sin_addr),
+       dev->local_netmask);
+    reply = (char*)xmalloc(bytes_to_read);
+    if (!msg || !reply) {
+        VLOG_ERR("Out of memory");
+       error = ENOMEM;
+       goto cleanup;
+    }
+    bytes_to_write = strlen(msg);
+    while (bytes_to_write || bytes_to_read) {
+        fd_set readset, writeset, errorset;
+
+       FD_ZERO(&readset);
+       FD_ZERO(&writeset);
+       FD_ZERO(&errorset);
+       if (bytes_to_write) {
+           FD_SET(ifd, &writeset);
+           FD_SET(ifd, &errorset);
+       }
+       FD_SET(ofd, &readset);
+       FD_SET(ofd, &errorset);
+       if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
+           if (errno == EINTR)
+               continue;
+           VLOG_ERR("selec error: %s", strerror(errno));
+           error = errno;
+           goto cleanup;
+       }
+       if (FD_ISSET(ifd, &errorset) || FD_ISSET(ofd, &errorset)) {
+           VLOG_ERR("error condition on ifd or ofd");
+           goto cleanup;
+       }
+       if (FD_ISSET(ifd, &writeset)) {
+           ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);    
+           if (n < 0) {
+               if (errno != EAGAIN && errno != EINTR) {
+                   VLOG_ERR("write on vif_up.in: %s", strerror(errno));
+                   error = errno;
+                   goto cleanup;
+               }
+            } else {
+               bytes_written += n;
+               bytes_to_write -= n;
+               if (bytes_to_write == 0)
+                   close(ifd);
+           }
+       }
+       if (FD_ISSET(ofd, &readset)) {
+           ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);    
+           if (n < 0) {
+               if (errno != EAGAIN && errno != EINTR) {
+                   VLOG_ERR("read on vif_up.out: %s", strerror(errno));
+                   error = errno;
+                   goto cleanup;
+               }
+            } else if (n == 0) {
+               bytes_to_read = 0;
+            } else {
+               bytes_read += n;
+               bytes_to_read -= n;
+           }
+       }
+    }
+    if (bytes_read) {
+       reply[bytes_read] = '\0';
+        VLOG_ERR("vif_up returned: %s", reply);
+       dev->error = reply;
+       reply = NULL;
+       error = EAGAIN;
+       goto cleanup;
+    }
+    dev->finalized = true;
+    free(dev->error);
+    dev->error = NULL;
+    netdev_pltap_update_seq(dev);
+
+cleanup:
+    free(msg);
+    free(reply);
+    close(ifd);
+    close(ofd);
+    return error;
+}
+
+static int
+netdev_pltap_get_config(struct netdev_dev *dev_, struct smap *args)
+{
+    struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
+
+    if (netdev_dev->valid_local_ip)
+       smap_add_format(args, "local_ip", IP_FMT,
+            IP_ARGS(&netdev_dev->local_addr.sin_addr));
+    if (netdev_dev->valid_local_netmask)
+        smap_add_format(args, "local_netmask", "%"PRIu32,
+            ntohs(netdev_dev->local_netmask));
+    return netdev_pltap_create_finalize(netdev_dev);
+}
+
+static int
+netdev_pltap_set_config(struct netdev_dev *dev_, const struct smap *args)
+{
+    struct netdev_dev_pltap *netdev_dev = netdev_dev_pltap_cast(dev_);
+    struct shash_node *node;
+
+    VLOG_DBG("pltap_set_config(%s)", netdev_dev_get_name(dev_));
+    SMAP_FOR_EACH(node, args) {
+        VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
+       if (!strcmp(node->name, "local_ip")) {
+           struct in_addr addr;
+           if (lookup_ip(node->data, &addr)) {
+               VLOG_WARN("%s: bad 'local_ip'", node->name);
+           } else {
+               netdev_dev->local_addr.sin_addr = addr;
+               netdev_dev->valid_local_ip = true;
+           }
+       } else if (!strcmp(node->name, "local_netmask")) {
+           netdev_dev->local_netmask = atoi(node->data);
+           // XXX check valididy
+           netdev_dev->valid_local_netmask = true;
+       } else {
+           VLOG_WARN("%s: unknown argument '%s'", 
+               netdev_dev_get_name(dev_), node->name);
+       }
+    }
+    return netdev_pltap_create_finalize(netdev_dev);        
+}
+
+static int
+netdev_pltap_listen(struct netdev *netdev_ OVS_UNUSED)
+{
+    return 0;
+}
+
+static int
+netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size)
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev_));
+    if (!dev->finalized)
+        return -EAGAIN;
+    for (;;) {
+        ssize_t retval;
+        retval = read(dev->fd, buffer, size);
+       VLOG_DBG("%s: read(%"PRIxPTR", %"PRIu64") = %"PRId64,
+                netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
+        if (retval >= 0) {
+            if (retval <= size) {
+               return retval;
+           } else {
+               return -EMSGSIZE;
+           }
+        } else if (errno != EINTR) {
+            if (errno != EAGAIN) {
+                VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
+                    netdev_get_name(netdev_), strerror(errno));
+            }
+            return -errno;
+        }
+    }
+}
+
+static void
+netdev_pltap_recv_wait(struct netdev *netdev_)
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev_));
+    if (dev->finalized && dev->fd >= 0) {
+        poll_fd_wait(dev->fd, POLLIN);
+    }
+}
+
+static int
+netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev_));
+    if (dev->fd < 0 || !dev->finalized)
+        return EAGAIN;
+    for (;;) {
+        ssize_t retval;
+        retval = write(dev->fd, buffer, size);
+       VLOG_DBG("%s: write(%"PRIxPTR", %"PRIu64") = %"PRId64,
+                netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
+        if (retval >= 0) {
+           if (retval != size) {
+               VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRId64" bytes of "
+                            "%"PRIu64") on %s", retval, size, netdev_get_name(netdev_));
+           }
+            return 0;
+        } else if (errno != EINTR) {
+            if (errno != EAGAIN) {
+                VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
+                    netdev_get_name(netdev_), strerror(errno));
+            }
+            return errno;
+        }
+    }
+}
+
+static void
+netdev_pltap_send_wait(struct netdev *netdev_)
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev_));
+    if (dev->finalized && dev->fd >= 0) {
+        poll_fd_wait(dev->fd, POLLOUT);
+    }
+}
+
+static int
+netdev_pltap_drain(struct netdev *netdev_)
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev_));
+    char buffer[128];
+    int error;
+
+    if (dev->fd < 0 || !dev->finalized)
+       return 0;
+    for (;;) {
+       error = recv(dev->fd, buffer, 128, MSG_TRUNC);
+       if (error) {
+            if (error == -EAGAIN)
+               break;
+            else if (error != -EMSGSIZE)
+               return error;
+       }
+    }
+    return 0;
+}
+
+static int
+netdev_pltap_set_etheraddr(struct netdev *netdev,
+                           const uint8_t mac[ETH_ADDR_LEN])
+{
+    return ENOTSUP;
+}
+
+// XXX from netdev-linux.c
+static int
+get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
+{
+    struct ifreq ifr;
+    int hwaddr_family;
+    int af_inet_sock;
+
+    /* Create AF_INET socket. */
+    af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
+    if (af_inet_sock < 0) {
+        VLOG_ERR("failed to create inet socket: %s", strerror(errno));
+    }
+
+    memset(&ifr, 0, sizeof ifr);
+    ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
+    if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
+        /* ENODEV probably means that a vif disappeared asynchronously and
+         * hasn't been removed from the database yet, so reduce the log level
+         * to INFO for that case. */
+        VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
+             "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
+             netdev_name, strerror(errno));
+        return errno;
+    }
+    hwaddr_family = ifr.ifr_hwaddr.sa_family;
+    if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
+        VLOG_WARN("%s device has unknown hardware address family %d",
+                  netdev_name, hwaddr_family);
+    }
+    memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
+    return 0;
+}
+
+static int
+netdev_pltap_get_etheraddr(const struct netdev *netdev,
+                           uint8_t mac[ETH_ADDR_LEN])
+{
+    struct netdev_dev_pltap *dev = 
+       netdev_dev_pltap_cast(netdev_get_dev(netdev));
+    if (dev->fd < 0 || !dev->finalized)
+        return EAGAIN;
+    return get_etheraddr(dev->real_name, mac);
+}
+
+
+// XXX can we read stats in planetlab?
+static int
+netdev_pltap_get_stats(const struct netdev *netdev OVS_UNUSED, struct netdev_stats *stats OVS_UNUSED)
+{
+    return ENOTSUP;
+}
+
+static int
+netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_stats *stats OVS_UNUSED)
+{
+    return ENOTSUP;
+}
+
+static int
+netdev_pltap_update_flags(struct netdev *netdev,
+                          enum netdev_flags off, enum netdev_flags on,
+                          enum netdev_flags *old_flagsp)
+{
+    struct netdev_dev_pltap *dev =
+        netdev_dev_pltap_cast(netdev_get_dev(netdev));
+
+    if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
+        return EINVAL;
+    }
+
+    // XXX should we actually do something with these flags?
+    *old_flagsp = dev->flags;
+    dev->flags |= on;
+    dev->flags &= ~off;
+    if (*old_flagsp != dev->flags) {
+        netdev_pltap_update_seq(dev);
+    }
+    return 0;
+}
+
+static unsigned int
+netdev_pltap_change_seq(const struct netdev *netdev)
+{
+    return netdev_dev_pltap_cast(netdev_get_dev(netdev))->change_seq;
+}
+\f
+/* Helper functions. */
+
+static void
+netdev_pltap_update_seq(struct netdev_dev_pltap *dev)
+{
+    dev->change_seq++;
+    if (!dev->change_seq) {
+        dev->change_seq++;
+    }
+}
+
+static void
+netdev_pltap_get_real_name(struct unixctl_conn *conn,
+                     int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
+{
+    struct netdev_dev_pltap *pltap_dev;
+
+    pltap_dev = shash_find_data(&pltap_netdev_devs, argv[1]);
+    if (!pltap_dev) {
+        unixctl_command_reply_error(conn, "no such pltap netdev");
+        return;
+    }
+    if (pltap_dev->error) {
+       unixctl_command_reply_error(conn, pltap_dev->error);
+       return;
+    }
+
+    unixctl_command_reply(conn, pltap_dev->real_name);
+}
+
+static int
+netdev_pltap_init(void)
+{
+    unixctl_command_register("netdev-pltap/get-tapname", "port",
+                             1, 1, netdev_pltap_get_real_name, NULL);
+    return 0;
+}
+
+const struct netdev_class netdev_pltap_class = {
+    "pltap",
+    netdev_pltap_init,
+    NULL,  
+    NULL,            
+
+    netdev_pltap_create,
+    netdev_pltap_destroy,
+    netdev_pltap_get_config,
+    netdev_pltap_set_config, 
+
+    netdev_pltap_open,
+    netdev_pltap_close,
+
+    netdev_pltap_listen,
+    netdev_pltap_recv,
+    netdev_pltap_recv_wait,
+    netdev_pltap_drain,
+
+    netdev_pltap_send, 
+    netdev_pltap_send_wait,  
+
+    netdev_pltap_set_etheraddr,
+    netdev_pltap_get_etheraddr,
+    NULL,                      /* get_mtu */
+    NULL,                      /* set_mtu */
+    NULL,                       /* get_ifindex */
+    NULL,                      /* get_carrier */
+    NULL,                       /* get_carrier_resets */
+    NULL,                       /* get_miimon */
+    netdev_pltap_get_stats,
+    netdev_pltap_set_stats,
+
+    NULL,                       /* get_features */
+    NULL,                       /* set_advertisements */
+
+    NULL,                       /* set_policing */
+    NULL,                       /* get_qos_types */
+    NULL,                       /* get_qos_capabilities */
+    NULL,                       /* get_qos */
+    NULL,                       /* set_qos */
+    NULL,                       /* get_queue */
+    NULL,                       /* set_queue */
+    NULL,                       /* delete_queue */
+    NULL,                       /* get_queue_stats */
+    NULL,                       /* dump_queues */
+    NULL,                       /* dump_queue_stats */
+
+    NULL,                       /* get_in4 */
+    NULL,                       /* set_in4 */
+    NULL,                       /* get_in6 */
+    NULL,                       /* add_router */
+    NULL,                       /* get_next_hop */
+    NULL,                       /* get_drv_info */
+    NULL,                       /* arp_lookup */
+
+    netdev_pltap_update_flags,
+
+    netdev_pltap_change_seq
+};
index 94f60af..f1b123a 100644 (file)
@@ -600,6 +600,9 @@ extern const struct netdev_class netdev_tap_class;
 extern const struct netdev_class netdev_bsd_class;
 #endif
 
+extern const struct netdev_class netdev_tunnel_class;
+extern const struct netdev_class netdev_pltap_class;
+
 #ifdef  __cplusplus
 }
 #endif
diff --git a/lib/netdev-tunnel.c b/lib/netdev-tunnel.c
new file mode 100644 (file)
index 0000000..dec5cf8
--- /dev/null
@@ -0,0 +1,510 @@
+/*
+ * Copyright (c) 2010, 2011, 2012 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+
+#include "flow.h"
+#include "list.h"
+#include "netdev-provider.h"
+#include "odp-util.h"
+#include "ofp-print.h"
+#include "ofpbuf.h"
+#include "packets.h"
+#include "poll-loop.h"
+#include "shash.h"
+#include "sset.h"
+#include "unixctl.h"
+#include "socket-util.h"
+#include "vlog.h"
+
+VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
+
+struct netdev_dev_tunnel {
+    struct netdev_dev netdev_dev;
+    uint8_t hwaddr[ETH_ADDR_LEN];
+    struct netdev_stats stats;
+    enum netdev_flags flags;
+    int sockfd;
+    struct sockaddr_in local_addr;
+    struct sockaddr_in remote_addr;
+    bool valid_remote_ip;
+    bool valid_remote_port;
+    bool connected;
+    unsigned int change_seq;
+};
+
+struct netdev_tunnel {
+    struct netdev netdev;
+} ;
+
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+
+static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
+
+static int netdev_tunnel_create(const struct netdev_class *, const char *,
+                               struct netdev_dev **);
+static void netdev_tunnel_update_seq(struct netdev_dev_tunnel *);
+
+static bool
+is_tunnel_class(const struct netdev_class *class)
+{
+    return class->create == netdev_tunnel_create;
+}
+
+static struct netdev_dev_tunnel *
+netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
+{
+    assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
+    return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
+}
+
+static struct netdev_tunnel *
+netdev_tunnel_cast(const struct netdev *netdev)
+{
+    struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
+    assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
+    return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
+}
+
+static int
+netdev_tunnel_create(const struct netdev_class *class, const char *name,
+                    struct netdev_dev **netdev_devp)
+{
+    static unsigned int n = 0;
+    struct netdev_dev_tunnel *netdev_dev;
+    int error;
+
+    netdev_dev = xzalloc(sizeof *netdev_dev);
+    netdev_dev_init(&netdev_dev->netdev_dev, name, class);
+    netdev_dev->hwaddr[0] = 0xfe;
+    netdev_dev->hwaddr[1] = 0xff;
+    netdev_dev->hwaddr[2] = 0xff;
+    netdev_dev->hwaddr[3] = n >> 16;
+    netdev_dev->hwaddr[4] = n >> 8;
+    netdev_dev->hwaddr[5] = n;
+    netdev_dev->flags = 0;
+    netdev_dev->change_seq = 1;
+    memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
+    netdev_dev->valid_remote_ip = false;
+    netdev_dev->valid_remote_port = false;
+    netdev_dev->connected = false;
+
+
+    netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
+    if (netdev_dev->sockfd < 0) {
+       error = netdev_dev->sockfd;
+        goto error;
+    }
+
+
+    shash_add(&tunnel_netdev_devs, name, netdev_dev);
+
+    n++;
+
+    *netdev_devp = &netdev_dev->netdev_dev;
+
+    VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
+
+    return 0;
+
+error:
+    free(netdev_dev);
+    return error;
+}
+
+static void
+netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
+{
+    struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
+
+    if (netdev_dev->sockfd != -1)
+       close(netdev_dev->sockfd);
+
+    shash_find_and_delete(&tunnel_netdev_devs,
+                          netdev_dev_get_name(netdev_dev_));
+    free(netdev_dev);
+}
+
+static int
+netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
+{
+    struct netdev_tunnel *netdev;
+
+    netdev = xmalloc(sizeof *netdev);
+    netdev_init(&netdev->netdev, netdev_dev_);
+
+    *netdevp = &netdev->netdev;
+    return 0;
+}
+
+static void
+netdev_tunnel_close(struct netdev *netdev_)
+{
+    struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
+    free(netdev);
+}
+
+static int
+netdev_tunnel_get_config(struct netdev_dev *dev_, struct smap *args)
+{
+    struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
+
+    if (netdev_dev->valid_remote_ip)
+       smap_add_format(args, "remote_ip", IP_FMT,
+               IP_ARGS(&netdev_dev->remote_addr.sin_addr));
+    if (netdev_dev->valid_remote_port)
+        smap_add_format(args, "remote_port", "%"PRIu16,
+               ntohs(netdev_dev->remote_addr.sin_port));
+    return 0;
+}
+
+static int
+netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
+{
+    if (dev->sockfd < 0)
+        return EBADF;
+    if (!dev->valid_remote_ip || !dev->valid_remote_port)
+        return 0;
+    dev->remote_addr.sin_family = AF_INET;
+    if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
+        return errno;
+    }
+    dev->connected = true;
+    netdev_tunnel_update_seq(dev);
+    VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
+        inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
+    return 0;
+}
+
+static int
+netdev_tunnel_set_config(struct netdev_dev *dev_, const struct smap *args)
+{
+    struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
+    struct shash_node *node;
+
+    VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
+    SMAP_FOR_EACH(node, args) {
+        VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
+       if (!strcmp(node->name, "remote_ip")) {
+           struct in_addr addr;
+           if (lookup_ip(node->data, &addr)) {
+               VLOG_WARN("%s: bad 'remote_ip'", node->name);
+           } else {
+               netdev_dev->remote_addr.sin_addr = addr;
+               netdev_dev->valid_remote_ip = true;
+           }
+       } else if (!strcmp(node->name, "remote_port")) {
+           netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
+           netdev_dev->valid_remote_port = true;
+       } else {
+           VLOG_WARN("%s: unknown argument '%s'", 
+               netdev_dev_get_name(dev_), node->name);
+       }
+    }
+    return netdev_tunnel_connect(netdev_dev);        
+}
+
+static int
+netdev_tunnel_listen(struct netdev *netdev_ OVS_UNUSED)
+{
+    return 0;
+}
+
+static int
+netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
+{
+    struct netdev_dev_tunnel *dev = 
+       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    if (!dev->connected)
+        return -EAGAIN;
+    for (;;) {
+        ssize_t retval;
+        retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
+       VLOG_DBG("%s: recv(%"PRIxPTR", %"PRIu64", MSG_TRUNC) = %"PRId64,
+                netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
+        if (retval >= 0) {
+           dev->stats.rx_packets++;
+           dev->stats.rx_bytes += retval;
+            if (retval <= size) {
+               return retval;
+           } else {
+               dev->stats.rx_errors++;
+               dev->stats.rx_length_errors++;
+               return -EMSGSIZE;
+           }
+        } else if (errno != EINTR) {
+            if (errno != EAGAIN) {
+                VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
+                    netdev_get_name(netdev_), strerror(errno));
+               dev->stats.rx_errors++;
+            }
+            return -errno;
+        }
+    }
+}
+
+static void
+netdev_tunnel_recv_wait(struct netdev *netdev_)
+{
+    struct netdev_dev_tunnel *dev = 
+       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    if (dev->sockfd >= 0) {
+        poll_fd_wait(dev->sockfd, POLLIN);
+    }
+}
+
+static int
+netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
+{
+    struct netdev_dev_tunnel *dev = 
+       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    if (!dev->connected)
+        return EAGAIN;
+    for (;;) {
+        ssize_t retval;
+        retval = send(dev->sockfd, buffer, size, 0);
+       VLOG_DBG("%s: send(%"PRIxPTR", %"PRIu64") = %"PRId64,
+                netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
+        if (retval >= 0) {
+           dev->stats.tx_packets++;
+           dev->stats.tx_bytes++;
+           if (retval != size) {
+               VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRId64" bytes of "
+                            "%"PRIu64") on %s", retval, size, netdev_get_name(netdev_));
+               dev->stats.tx_errors++;
+           }
+            return 0;
+        } else if (errno != EINTR) {
+            if (errno != EAGAIN) {
+                VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
+                    netdev_get_name(netdev_), strerror(errno));
+               dev->stats.tx_errors++;
+            }
+            return errno;
+        }
+    }
+}
+
+static void
+netdev_tunnel_send_wait(struct netdev *netdev_)
+{
+    struct netdev_dev_tunnel *dev = 
+       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    if (dev->sockfd >= 0) {
+        poll_fd_wait(dev->sockfd, POLLOUT);
+    }
+}
+
+static int
+netdev_tunnel_drain(struct netdev *netdev_)
+{
+    struct netdev_dev_tunnel *dev = 
+       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    char buffer[128];
+    int error;
+
+    if (!dev->connected)
+       return 0;
+    for (;;) {
+       error = recv(dev->sockfd, buffer, 128, MSG_TRUNC);
+       if (error) {
+            if (error == -EAGAIN)
+               break;
+            else if (error != -EMSGSIZE)
+               return error;
+       }
+    }
+    return 0;
+}
+
+static int
+netdev_tunnel_set_etheraddr(struct netdev *netdev,
+                           const uint8_t mac[ETH_ADDR_LEN])
+{
+    struct netdev_dev_tunnel *dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+
+    if (!eth_addr_equals(dev->hwaddr, mac)) {
+        memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
+        netdev_tunnel_update_seq(dev);
+    }
+
+    return 0;
+}
+
+static int
+netdev_tunnel_get_etheraddr(const struct netdev *netdev,
+                           uint8_t mac[ETH_ADDR_LEN])
+{
+    const struct netdev_dev_tunnel *dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+
+    memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
+    return 0;
+}
+
+
+static int
+netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
+{
+    const struct netdev_dev_tunnel *dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+
+    *stats = dev->stats;
+    return 0;
+}
+
+static int
+netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
+{
+    struct netdev_dev_tunnel *dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+
+    dev->stats = *stats;
+    return 0;
+}
+
+static int
+netdev_tunnel_update_flags(struct netdev *netdev,
+                          enum netdev_flags off, enum netdev_flags on,
+                          enum netdev_flags *old_flagsp)
+{
+    struct netdev_dev_tunnel *dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+
+    if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
+        return EINVAL;
+    }
+
+    // XXX should we actually do something with this flags?
+    *old_flagsp = dev->flags;
+    dev->flags |= on;
+    dev->flags &= ~off;
+    if (*old_flagsp != dev->flags) {
+        netdev_tunnel_update_seq(dev);
+    }
+    return 0;
+}
+
+static unsigned int
+netdev_tunnel_change_seq(const struct netdev *netdev)
+{
+    return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
+}
+\f
+/* Helper functions. */
+
+static void
+netdev_tunnel_update_seq(struct netdev_dev_tunnel *dev)
+{
+    dev->change_seq++;
+    if (!dev->change_seq) {
+        dev->change_seq++;
+    }
+}
+
+static void
+netdev_tunnel_get_port(struct unixctl_conn *conn,
+                     int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
+{
+    struct netdev_dev_tunnel *tunnel_dev;
+    char buf[6];
+
+    tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
+    if (!tunnel_dev) {
+        unixctl_command_reply_error(conn, "no such tunnel netdev");
+        return;
+    }
+
+    sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
+    unixctl_command_reply(conn, buf);
+}
+
+
+static int
+netdev_tunnel_init(void)
+{
+    unixctl_command_register("netdev-tunnel/get-port", "NAME",
+                             1, 1, netdev_tunnel_get_port, NULL);
+    return 0;
+}
+
+const struct netdev_class netdev_tunnel_class = {
+    "tunnel",
+    netdev_tunnel_init,         /* init */
+    NULL,                       /* run */
+    NULL,                       /* wait */
+
+    netdev_tunnel_create,
+    netdev_tunnel_destroy,
+    netdev_tunnel_get_config,
+    netdev_tunnel_set_config, 
+
+    netdev_tunnel_open,
+    netdev_tunnel_close,
+
+    netdev_tunnel_listen,
+    netdev_tunnel_recv,
+    netdev_tunnel_recv_wait,
+    netdev_tunnel_drain,
+
+    netdev_tunnel_send, 
+    netdev_tunnel_send_wait,  
+
+    netdev_tunnel_set_etheraddr,
+    netdev_tunnel_get_etheraddr,
+    NULL,                      /* get_mtu */
+    NULL,                      /* set_mtu */
+    NULL,                       /* get_ifindex */
+    NULL,                      /* get_carrier */
+    NULL,                       /* get_carrier_resets */
+    NULL,                       /* get_miimon */
+    netdev_tunnel_get_stats,
+    netdev_tunnel_set_stats,
+
+    NULL,                       /* get_features */
+    NULL,                       /* set_advertisements */
+
+    NULL,                       /* set_policing */
+    NULL,                       /* get_qos_types */
+    NULL,                       /* get_qos_capabilities */
+    NULL,                       /* get_qos */
+    NULL,                       /* set_qos */
+    NULL,                       /* get_queue */
+    NULL,                       /* set_queue */
+    NULL,                       /* delete_queue */
+    NULL,                       /* get_queue_stats */
+    NULL,                       /* dump_queues */
+    NULL,                       /* dump_queue_stats */
+
+    NULL,                       /* get_in4 */
+    NULL,                       /* set_in4 */
+    NULL,                       /* get_in6 */
+    NULL,                       /* add_router */
+    NULL,                       /* get_next_hop */
+    NULL,                       /* get_drv_info */
+    NULL,                       /* arp_lookup */
+
+    netdev_tunnel_update_flags,
+
+    netdev_tunnel_change_seq
+};
index 394d895..f86068a 100644 (file)
@@ -85,6 +85,8 @@ netdev_initialize(void)
         netdev_register_provider(&netdev_tap_class);
         netdev_register_provider(&netdev_bsd_class);
 #endif
+       netdev_register_provider(&netdev_tunnel_class);
+       netdev_register_provider(&netdev_pltap_class);
     }
 }
 
diff --git a/lib/tunalloc.c b/lib/tunalloc.c
new file mode 100644 (file)
index 0000000..1f083e5
--- /dev/null
@@ -0,0 +1,101 @@
+/* Slice-side code to allocate tuntap interface in root slice
+ * Based on bmsocket.c
+ *  Thom Haddow - 08/10/09
+ *
+ * Call tun_alloc() with IFFTUN or IFFTAP as an argument to get back fd to
+ * new tuntap interface. Interface name can be acquired via TUNGETIFF ioctl.
+ */
+
+#include <sys/un.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+
+#define VSYS_TUNTAP "/vsys/fd_tuntap.control"
+
+
+int tun_alloc(int iftype, char *if_name);
+
+/* Reads vif FD from "fd", writes interface name to vif_name, and returns vif FD.
+ * vif_name should be IFNAMSIZ chars long. */
+static
+int receive_vif_fd(int fd, char *vif_name)
+{
+       struct msghdr msg;
+       struct iovec iov;
+       int rv;
+       size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
+       struct cmsghdr *cmsg;
+       unsigned char *data;
+
+    /* Use IOV to read interface name */
+       iov.iov_base = vif_name;
+       iov.iov_len = IFNAMSIZ;
+
+       msg.msg_name = 0;
+       msg.msg_namelen = 0;
+       msg.msg_iov = &iov;
+       msg.msg_iovlen = 1;
+       /* old BSD implementations should use msg_accrights instead of
+        * msg_control; the interface is different. */
+       msg.msg_control = ccmsg;
+       msg.msg_controllen = sizeof(ccmsg);
+
+       while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
+       if (rv == -1) {
+               perror("recvmsg");
+               return -1;
+       }
+       if(!rv) {
+               /* EOF */
+               return -1;
+       }
+
+       cmsg = CMSG_FIRSTHDR(&msg);
+       if (!cmsg->cmsg_type == SCM_RIGHTS) {
+               fprintf(stderr, "got control message of unknown type %d\n",
+                       cmsg->cmsg_type);
+               return -1;
+       }
+       data = CMSG_DATA(cmsg);
+       return *(int*)data;
+}
+
+
+int tun_alloc(int iftype, char *if_name)
+{
+    int control_fd;
+    struct sockaddr_un addr;
+    int remotefd;
+
+    control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (control_fd == -1) {
+        perror("Could not create UNIX socket\n");
+        exit(-1);
+    }
+
+    memset(&addr, 0, sizeof(struct sockaddr_un));
+    /* Clear structure */
+    addr.sun_family = AF_UNIX;
+    strncpy(addr.sun_path, VSYS_TUNTAP,
+            sizeof(addr.sun_path) - 1);
+
+    if (connect(control_fd, (struct sockaddr *) &addr,
+                sizeof(struct sockaddr_un)) == -1) {
+        perror("Could not connect to Vsys control socket");
+        exit(-1);
+    }
+
+    /* passing type param */
+    if (send(control_fd, &iftype, sizeof(iftype), 0) != sizeof(iftype)) {
+        perror("Could not send paramater to Vsys control socket");
+        exit(-1);
+    }
+
+    remotefd = receive_vif_fd(control_fd, if_name);
+    return remotefd;
+}
diff --git a/lib/tunalloc.h b/lib/tunalloc.h
new file mode 100644 (file)
index 0000000..3e5caae
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef _TUNALLOC_H
+#define _TUNALLOC_H
+
+int tun_alloc(int iftype, char *if_name);
+
+#endif
diff --git a/planetlab/automake.mk b/planetlab/automake.mk
new file mode 100644 (file)
index 0000000..f32a083
--- /dev/null
@@ -0,0 +1,9 @@
+
+# this Makefile is not intended to go on the sliver image - esp. not in /usr/sbin
+#      planetlab/scripts/Makefile
+# same goes for showgraph
+#      planetlab/scripts/showgraph
+dist_sbin_SCRIPTS += planetlab/scripts/sliver-ovs 
+
+EXTRA_DIST += \
+       planetlab/scripts/sliver-ovs.in
diff --git a/planetlab/exp-tool/Makefile b/planetlab/exp-tool/Makefile
new file mode 100644 (file)
index 0000000..a6b0103
--- /dev/null
@@ -0,0 +1,287 @@
+# see README
+# conf.mk is expected to define
+# HOST_<id> and IP_<id> for all nodes involved, as well as 
+# LINKS as a list of <node_id>-<node_id> elements
+
+# run make CONF=anotherconfig.mk if you need several configs
+
+CONF ?= conf.mk
+include $(CONF)
+
+# if undefined in the conf file, use single dash
+SEP?=-
+
+# bridge name (XXX the same on all nodes)
+BRIDGE?=$(SLICE)
+
+### helper functions
+# flip(1) = 2
+# flip(2) = 1
+flip=$(if $(findstring 1,$(1)),2,1)
+# cutsep (x-y)-> x y
+cutsep=$(subst $(SEP), ,$(1))
+# leftnode (x-y) -> x
+leftnode=$(word 1,$(call cutsep,$(1)))
+# rightnode (x-y) -> y
+rightnode=$(word 2,$(call cutsep,$(1)))
+# linkpart(x@y) = x
+linkpart=$(word 1,$(subst @, ,$(1)))
+# endpart(x@y) = y
+endpart=$(word 2,$(subst @, ,$(1)))
+# get(x-y@1) = x
+# get(x-y@2) = y
+get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
+# opp(x-y@1) = x-y@2
+# opp(x-y@2) = x-y@1
+opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
+# rget(x-y@1) = y
+# rget(x-y@2) = x
+rget=$(call get,$(call opp,$(1)))
+###
+solve=$(HOST_$(1))
+solve_ip=$(IP_$(1))
+# can be redefined in conf.mk if that's not the expected behaviour
+display?=host $(1) aka $(call solve,$(1))
+
+# log file name
+log=$(addprefix log/,$(notdir $(1)))
+
+#################### set variables after conf.mk
+ifeq "$(SSH_KEY)" ""
+SSH_KEY_OPTION ?=
+else
+SSH_KEY_OPTION ?= -i $(SSH_KEY)
+endif
+
+SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
+SSH = ssh $(SSH_OPTIONS)
+
+SUDO ?= sudo -S
+
+ALL_NODE_IDS=$(sort $(foreach link,$(LINKS),$(call leftnode,$(link))) $(foreach link,$(LINKS),$(call rightnode,$(link))))
+ALL_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
+CONF_NODE_IDS=$(subst HOST_,,$(filter HOST_%,$(.VARIABLES)))
+
+####################
+all+init: init all
+init:
+       @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
+       @[ -d log ] || (echo Creating tmp dir log; mkdir log)
+       @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
+.PHONY: all+init init
+
+FORCE:
+
+.SECONDARY:
+
+LINKTARGETS=$(addprefix L/,$(LINKS))
+all: $(LINKTARGETS)
+.PHONY: all
+
+# could also do make ++SLICE
+showslice: ++SLICE FORCE
+
+shownodes:
+       @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
+showips:
+       @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
+showlinks:
+       @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
+.PHONY: shownodes showips showlinks
+
+sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
+.PHONY: sshchecks
+
+DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
+dbs: $(DBS)
+.PHONY: dbs
+
+SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
+switchs: $(SWITCHS)
+.PHONY: switchs
+
+start: dbs switchs
+.PHONY: start
+
+stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
+.PHONY: stop
+
+status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
+.PHONY: status
+
+BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
+bridges: $(BRIDGES)
+.PHONY: bridges
+
+### node-oriented targets
+# check ssh connectivity
+cache/sshcheck.%: FORCE
+       @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
+        else echo "ssh on" $(call display,$*) "KO !!!"; fi
+
+# should probably replace sshcheck
+cache/status.%: FORCE
+       @echo "=== DB and SWITCH processes on $(call display,$*)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
+
+cache/host.%:
+       @echo "IP lookup for $(call display,$*)"
+       @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
+
+cache/db.%:
+       @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
+
+cache/switch.%: cache/db.%
+       @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
+
+cache/bridge.%: cache/switch.%
+       @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
+       @echo Created bridge with tap $$(cat $@) on $(call display,$*)
+
+# xxx this probably needs a more thorough cleanup in cache/
+cache/stop.%: del-bridge.%
+       @echo "Stopping switch & db on $(call display,$*)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
+
+### link-oriented targets
+# L/<nodeid>-<node_id>
+L/%: cache/link.%@1 cache/link.%@2
+       @touch $@
+       @echo "Created link $*"
+
+U/%: del-iface.%@1 del-iface.%@2
+       @rm -f L/$*
+       @echo "Deleted link $*"
+
+del-bridge.%: cache/db.%
+       @echo "Deleting bridge on $(call display,$*)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
+       @rm -f cache/bridge.$* \
+             cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
+             cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
+             L/$*$(SEP)*             L/*$(SEP)$*
+
+del-switch.%: del-bridge.%
+       @echo "Shutting down switch on $(call display,$*)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
+       @rm -f cache/switch.$* 
+
+del-db.%:
+       @echo "Shutting down db on $(call display,$*)"
+       @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
+       @rm -f cache/db.$*
+
+del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
+
+del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
+
+del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
+
+shutdown: del-switchs del-dbs
+
+.PHONY: del-links del-switchs del-dbs shutdown
+
+
+### snapshots
+remote-snapshot: remote-snapshot-ips remote-snapshot-links
+       @true
+
+remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
+       @cat /dev/null $^
+
+remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
+       @sort -u /dev/null $^
+
+.PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
+
+cache/rsnap.ip.%: 
+       @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
+               get interface $(BRIDGE) options:local_ip options:local_netmask |\
+               sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
+        { rm $@; exit 1; }
+
+cache/rsnap.links.%: 
+       @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
+        sed -n 's/^L/LINKS += /p' > $@ || \
+        { rm $@; exit 1; }
+
+.SECONDEXPANSION:
+
+del-iface.%: cache/db.$$(call get,%)
+       @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
+       @$(SSH) $(HOST_$(call get,$*)) \
+               $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
+       @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
+
+
+### '%' here is leftid-rightid@{1,2}
+# we retrieve % as $(*F)
+#linkid=$(call linkpart,%)
+#nodeid=$(call get,%)
+#bridgefile=cache/bridge.$(nodeid)
+cache/iface.%: cache/bridge.$$(call get,%)
+       @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+       @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
+               L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
+
+
+# linkid=$(call linkpart,%)
+# nodeid=$(call get,%)
+# iface1=cache/iface.%
+# iface2=cache/iface.$(call opp,%)
+cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
+       @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+       @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
+                       $$(cat cache/host.$(call rget,$(*F))) \
+                       $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
+        && touch $@
+
+####################
+CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
+clean: $(CLEANTARGETS)
+distclean:
+       rm -rf L U cache
+.PHONY: clean distclean
+
+####################
+graph.dot:
+       ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
+graph.ps: graph.dot
+       dot -Tps < $^ > $@      
+
+####################
+# 'virtual' targets in that there's no real file attached
+define node_shortcuts
+sshcheck.$(1): cache/sshcheck.$(1) FORCE
+db.$(1): cache/db.$(1) FORCE
+switch.$(1): cache/switch.$(1) FORCE
+start.$(1): cache/start.$(1) FORCE
+stop.$(1): cache/stop.$(1) FORCE
+status.$(1): cache/status.$(1) FORCE
+bridge.$(1): cache/bridge.$(1) FORCE
+host.$(1): cache/host.$(1) FORCE
+# switch already depends on db, but well
+cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
+endef
+
+$(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
+
+define link_shortcuts
+iface.%: cache/iface.%
+link.%: cache/link.%
+endef
+
+$(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
+
+#################### convenience, for debugging only
+# make +foo : prints the value of $(foo)
+# make ++foo : idem but verbose, i.e. foo=$(foo)
+++%: varname=$(subst +,,$@)
+++%:
+       @echo "$(varname)=$($(varname))"
++%: varname=$(subst +,,$@)
++%:
+       @echo "$($(varname))"
diff --git a/planetlab/exp-tool/README b/planetlab/exp-tool/README
new file mode 100644 (file)
index 0000000..641aca2
--- /dev/null
@@ -0,0 +1,169 @@
+* Introduction
+
+The Makefile contained in this directory can be used by an
+experimenter to dynamically create an overlay network in a PlanetLab
+slice, using the sliver-openvswitch distribution. At present, the
+Makefile only supports the creation of the basic topology (nodes and
+links). 
+All the additional configuration of the bridges/switches (in
+particular, connecting the switches to OpenFlow controllers or
+enabling the Spanning Tree Protocol aka STP) has to be done using the
+tools available in the Open vSwitch distribution. This may change in
+the future.
+
+The overlay network supported by the Makefile may consist of:
+
+- at most one Open vSwitch bridge per sliver;
+- at most a pair of tunnels between each pair of slivers.
+
+(Please note that these limitations are due to the simple naming scheme
+adopted by the Makefile, and are not limitations of sliver-openvswitch.)
+
+Each bridge is connected to a tap device in the sliver. The tap device
+has an IP address chosen by the experimenter. The idea is to connect
+all the tap devices through the overlay network made up of Open vSwitch
+bridges and tunnels among them.
+
+
+* Installation
+
+On each sliver we need to install sliver-openvswitch and obtain the
+following tags:
+
+NAME           VALUE
+vsys           fd_tuntap
+vsys           vif_up
+vsys           vif_down
+vsys_net       (some subnet)
+
+
+On the experimenter box we need:
+
+- GNU make
+- the openssh client
+- the host program (usually distributed in bind-tools)
+- (optionally) the dot program from the graphviz distribution
+
+Then, we can simply copy the Makefile in a working directory on the
+experimenter box. The directory must also contain subdirectories 'L'
+and 'cache':
+
+$ mkdir work
+$ cp /path/to/Makefile work
+$ cd work
+$ mkdir -p L cache
+
+
+* Example usage
+
+Assume we have a PlanetLab slice called 'myslice' which
+contains four nodes:
+
+1) onelab7.iet.unipi.it
+2) planet2.elte.hu
+3) planetlab2.ics.forth.gr
+4) planetlab2.urv.cat
+
+
+Assume we have reserverd subnet 10.0.9.0/24 using vsys_net.  We are
+goint to build the following overlay network:
+
+   10.0.9.1/24   10.0.9.2/24   10.0.9.3/24
+       1 ----------- 2 ------------ 3
+                     |
+                     |
+                      |
+                     4 
+                 10.0.9.4/24
+
+
+In the same directory were we have put the Makefile we create a 'conf.mk'
+file containing the following variables:
+
+----------
+SLICE=myslice
+HOST_1=onelab7.iet.unipi.it
+IP_1=10.0.9.1/24
+HOST_2=planet2.elte.hu
+IP_2=10.0.9.2/24
+HOST_3=planetlab2.ics.forth.gr
+IP_3=10.0.9.3/24
+HOST_4=planetlab2.urv.cat
+IP_4=10.0.9.4/24
+
+LINKS :=
+LINKS += 1-2
+LINKS += 2-3
+LINKS += 2-4
+----------
+
+NOTE. In this example we have chosen to use numbers (1,2,3,4) as ids
+for nodes, you can use any other name that is convenient for you.
+See the example files in this directory for an example of this.
+
+
+Then, we can just type:
+
+$ make -j
+
+Assuming everything has been setup correctly, this command Will start
+the Open vSwitch servers, create the bridges and setup the tunnels. We
+can test that network is up by logging into a node and pinging some
+other node using the private subnet addresses:
+
+$ source conf.mk
+$ ssh -l $SLICE $HOST_1 ping 10.0.9.4
+
+Links can be destroyed and created dynamically. Assume we now want the
+the topology to match the following one:
+
+   10.0.9.1/24   10.0.9.2/24 
+       1 ----------- 2 
+                     |
+                     |
+                      |
+                     4 ----------- 3 
+                 10.0.9.4/24  10.0.9.3/24
+
+
+We can issue the following commands:
+
+$ make -j U/2-3      # unlink nodes 2 and 3
+$ make -j L/4-3      # link nodes 4 and 3
+
+The current state of the links is represented as a set of files in the 'L'
+directory.  If dot is installed, we can obtain a graphical representation
+of the topology by typing:
+
+$ make graph.ps
+
+#### BEG WARNING xxx this feature is broken now that LINKS are defined in the same conf.mk file
+Or we can save the current state in the 'links' file (so that we can
+recreate it later):
+
+$ ls L > links
+#### END WARNING xxx this feature is broken now that LINKS are defined in the same conf.mk file
+
+
+* Command reference
+
+All targets can be issued with the '-j' flag to (greatly) speed up operations.
+It may also be useful to use the '-k' flag, so that errors on some nodes do not
+stop the setup on the other nodes.
+
+
+all:           do wathever is needed to setup all the links in the 'links' file.
+
+clean:         tear down all existing links
+
+L/N1-N2:       setup a link between nodes HOST_N1 and HOST_N2
+
+U/N1-N2:       tear down the link (if it exists) between nodes HOST_N1
+               and HOST_N2
+
+del-bridge.N:  delete the bridge running on node HOST_N (this also tears down
+               all links that have an endpoint in N)
+
+graph.ps       create a postscript file containing a (simple) graphical
+               representation
+               of the current topology
diff --git a/planetlab/exp-tool/conf.mk.example b/planetlab/exp-tool/conf.mk.example
new file mode 100644 (file)
index 0000000..f4d9a86
--- /dev/null
@@ -0,0 +1,35 @@
+# the slice that you're using
+SLICE=inri_sl1
+
+# optionnally, the related ssh (private) key to use
+SSH_KEY=key_user.rsa
+
+# optionally, you can change the way nodes are displayed
+display="$(call solve,$(1))"
+
+#################### the nodes to use
+HOST_SENDER=vnode09.pl.sophia.inria.fr
+HOST_MUX=vnode02.pl.sophia.inria.fr
+HOST_END1=vnode10.pl.sophia.inria.fr
+HOST_END2=vnode07.pl.sophia.inria.fr
+
+# and their related IP and netmask
+# not that all these MUST fall in the vsys_vnet tag as granted by your planetlab operator
+# (in this example it is 10.0.100.0/24)
+
+IP_SENDER=10.0.100.1/24
+IP_MUX=10.0.100.2/24
+IP_END1=10.0.100.3/24
+IP_END2=10.0.100.4/24
+
+#################### the links to create
+LINKS :=
+# add one from SENDER to MUX
+LINKS += SENDER-MUX 
+# one from MUX to each of the 2 receivers
+LINKS += MUX-END1
+LINKS += MUX-END2
+
+####################
+
+
diff --git a/planetlab/exp-tool/showgraph b/planetlab/exp-tool/showgraph
new file mode 100755 (executable)
index 0000000..85421cb
--- /dev/null
@@ -0,0 +1,7 @@
+make graph.ps
+gv -watch -spartan graph.ps >/dev/null 2>&1 &
+while :
+do
+       inotifywait -e CREATE -e DELETE L
+       make graph.ps 
+done >/dev/null 2>&1
diff --git a/planetlab/scripts/sliver-ovs.in b/planetlab/scripts/sliver-ovs.in
new file mode 100755 (executable)
index 0000000..dfd596f
--- /dev/null
@@ -0,0 +1,263 @@
+#!/bin/bash
+# -*-shell-mode-*-
+
+### expected to be run as root
+
+COMMAND=$0
+
+#################### global vars
+RUN_DIR=@RUNDIR@
+DB_CONF_FILE=@DBDIR@/conf.db
+DB_SCHEMA=@pkgdatadir@/vswitch.ovsschema
+DB_LOG=@LOGDIR@/ovs-db.log
+##
+DB_SOCKET=$RUN_DIR/db.sock
+##
+SWITCH_LOG=@LOGDIR@/ovs-switch.log
+
+#################### helper functions
+
+function kill_pltap_ovs () {
+    killall pltap-ovs 2>/dev/null || :
+}
+
+function error {
+    echo "$@" >&2
+    exit 1
+}
+
+function get_params {
+    params=$1; shift
+    err_msg="$COMMAND $SUBCOMMAND $(echo $params | perl -pe 's/\S+/<$&>/g')"
+    for p in $(echo $params); do
+        [[ -z "$@" ]] && error "$err_msg"
+        pname=$(echo -n $p|perl -pe 's/\W/_/g')
+        eval $pname="$1"; shift
+    done
+    [[ -n "$@" ]] && error "$err_msg"
+}
+
+function is_switch_running {
+    ovs-appctl --target=ovs-vswitchd version >& /dev/null
+}
+
+function is_db_running {
+    ovs-appctl --target=ovsdb-server version >& /dev/null
+}
+
+function tapname () {
+    IP=$1; shift
+    echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
+}
+    
+function wait_server () {
+    pid_file=$1; shift
+    server_name=$1; shift
+    timeout=$1; shift
+
+    expire=$(($(date +%s) + $timeout))
+
+    ## wait for it to be up - xxx todo - could use a timeout of some kind
+    while [ ! -f "$pid_file" ]; do
+       echo "Waiting for $server_name to start... $(($expire - $(date +%s)))s left" >&2
+       sleep 1;
+       [ $(date +%s) -ge $expire ] && return 1
+    done
+    cat "$pid_file"
+}
+
+function wait_device () {
+    tapname=$1; shift
+    timeout=$1; shift
+
+    expire=$(($(date +%s) + $timeout))
+
+    while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
+       echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
+       sleep 1
+       [ $(date +%s) -ge $expire ] && return 1
+    done
+    return 0
+}
+
+######################################## startup
+function start_db () {
+    get_params "" "$@"
+
+    ## init conf
+    conf_dir=$(dirname $DB_CONF_FILE)
+    [ -d $conf_dir ] || mkdir -p $conf_dir
+    [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
+
+    ## init run
+    [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
+
+    ## check 
+    [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
+    [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
+
+    ## run the stuff
+    if [ ! -f "$RUN_DIR/ovsdb-server.pid" ]; then
+       ovsdb-server $DB_CONF_FILE \
+           --remote=punix:$DB_SOCKET \
+           --remote=db:Open_vSwitch,manager_options \
+           --private-key=db:SSL,private_key \
+           --certificate=db:SSL,certificate \
+           --bootstrap-ca-cert=db:SSL,ca_cert \
+           --pidfile \
+           --log-file=$DB_LOG \
+           --detach >& /dev/null
+    else
+       echo 'ovsdb-server appears to be running already, *not* starting'
+    fi
+    wait_server $RUN_DIR/ovsdb-server.pid ovsdb-server 30
+}
+
+function start_switch () {
+    get_params "" "$@"
+
+    # ensure ovsdb-server is running
+    is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
+
+    if [ ! -f "$RUN_DIR/ovs-vswitchd.pid" ] ; then
+       ovs-vswitchd \
+           --pidfile \
+           --log-file=$SWITCH_LOG \
+           --detach \
+           unix:$DB_SOCKET >& /dev/null
+    else
+       echo 'ovs-vswitchd appears to be running already, *not* starting'
+    fi
+    wait_server $RUN_DIR/ovs-vswitchd.pid ovs-vswitchd 30
+}
+
+function stop_db () { 
+    ovs-appctl --target=ovsdb-server exit || :
+}
+
+function stop_switch () { 
+    ovs-appctl --target=ovs-vswitchd exit || :
+}
+
+function status () {
+    pids=$(pgrep '^ovs')
+    [ -n "$pids" ] && ps $pids
+}
+
+function start () {
+    start_db
+    start_switch
+}
+
+function stop () {
+    stop_switch
+    stop_db
+}
+
+#################### create functions
+function create_bridge () {
+    
+    get_params "bridge IP/PREFIX" "$@"
+
+    IP=${IP_PREFIX%/*}
+    PREFIX=${IP_PREFIX#*/}
+
+    # ensure ovs-vswitchd is running
+    is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
+
+    set -e
+    ovs-vsctl --db=unix:$DB_SOCKET -- --may-exist add-br "$bridge" \
+       -- set bridge "$bridge" datapath_type=planetlab \
+       -- set interface "$bridge" options:local_ip="$IP" option:local_netmask="$PREFIX"
+    ovs-appctl netdev-pltap/get-tapname "$bridge"
+}
+
+function create_port () {
+
+    get_params "bridge port" "$@"
+
+    # ensure ovs-vswitchd is running
+    is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
+
+    set -e
+    ovs-vsctl --db=unix:$DB_SOCKET -- --may-exist add-port "$bridge" "$port" -- set interface "$port" type=tunnel
+    ovs-appctl --target=ovs-vswitchd netdev-tunnel/get-port "$port"
+}
+
+function set_remote_endpoint () {
+
+    get_params "local_port remote_ip remote_UDP_port" "$@"
+
+    # ensure ovs-vswitchd is running
+    is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
+
+    set -e
+    ovs-vsctl --db=unix:$DB_SOCKET set interface $local_port \
+        options:remote_ip=$remote_ip \
+       options:remote_port=$remote_UDP_port
+}
+
+#################### del functions
+function del_bridge () {
+    
+    get_params "bridge_name" "$@"
+
+    W=
+    if ! is_switch_running; then
+       # we can delete the bridge even if ovs-vswitchd is not running,
+       # but we need a running ovsdb-server
+       is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+       W="--no-wait"
+    fi
+
+    ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-br $bridge_name
+}
+
+function del_port () {
+    
+    get_params "port" "$@"
+
+    W=
+    if ! is_switch_running; then
+       # we can delete the port even if ovs-vswitchd is not running,
+       # but we need a running ovsdb-server
+       is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+       W="--no-wait"
+    fi
+
+    ovs-vsctl --db=unix:$DB_SOCKET $W -- --if-exists del-port "$port"
+}
+
+function show () {
+
+    get_params "" "$@"
+
+    is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+
+    ovs-vsctl --db=unix:$DB_SOCKET show
+}
+
+####################
+SUPPORTED_SUBCOMMANDS="start stop status 
+start_db stop_db start_switch stop_switch
+create_bridge create_port del_bridge del_port
+show set_remote_endpoint"
+
+function main () {
+       message="Usage: $COMMAND <subcommand> ...
+Supported subcommands are (dash or underscore is the same):
+$SUPPORTED_SUBCOMMANDS"
+       [[ -z "$@" ]] && error "$message"
+
+       SUBCOMMAND=$1; shift
+       # support dashes instead of underscores
+       SUBCOMMAND=$(echo $SUBCOMMAND | sed -e s,-,_,g)
+        found=""
+        for supported in $SUPPORTED_SUBCOMMANDS; do [ "$SUBCOMMAND" = "$supported" ] && found=yes; done
+
+       [ -z "$found" ] && error $message
+
+       $SUBCOMMAND "$@"
+}
+
+main "$@"
diff --git a/sliver-openvswitch.spec b/sliver-openvswitch.spec
new file mode 100644 (file)
index 0000000..d9241aa
--- /dev/null
@@ -0,0 +1,51 @@
+%define name sliver-openvswitch
+# to check for any change:
+# grep AC_INIT configure.ac 
+%define version 1.6.90
+%define taglevel 1
+
+%define debug_package %{nil}
+
+%define release %{taglevel}%{?pldistro:.%{pldistro}}%{?date:.%{date}}
+
+Vendor: OneLab
+Packager: OneLab <support@planet-lab.eu>
+Distribution: PlanetLab %{plrelease}
+URL: %{SCMURL}
+#Requires: 
+
+Summary: Openvswitch modified for running from a PlanetLab sliver
+Name: %{name}
+Version: %{version}
+Release: %{release}
+License: GPL
+Group: System Environment/Applications
+BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
+Source0: sliver-openvswitch-%{version}.tar.gz
+
+%description
+Openvswitch tuned for running within a PlanetLab sliver
+
+%prep 
+%setup -q
+
+%build
+./boot.sh
+# let's be as close as the regular linux/fedora layout
+./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var
+make
+
+%install
+make install DESTDIR=$RPM_BUILD_ROOT
+
+%clean
+rm -rf $RPM_BUILD_ROOT
+
+%files
+/usr
+
+%post
+
+%postun
+
+%changelog