[ovs-dev] [PATCH 4/8] netdev-gre: Genericize GRE netdev.

Jesse Gross jesse at nicira.com
Wed Aug 18 07:08:48 UTC 2010


Since the GRE netdev doesn't actually implement any of the GRE
protocol, none of the code is really specific to GRE.  This commit
makes the netdev a little more generic so that additional tunnel
types can easily piggyback on it in the future.

Signed-off-by: Jesse Gross <jesse at nicira.com>
---
 lib/automake.mk      |    2 +-
 lib/netdev-gre.c     |  280 -------------------------------------------------
 lib/netdev-tunnel.c  |  285 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/vlog-modules.def |    2 +-
 4 files changed, 287 insertions(+), 282 deletions(-)
 delete mode 100644 lib/netdev-gre.c
 create mode 100644 lib/netdev-tunnel.c

diff --git a/lib/automake.mk b/lib/automake.mk
index f7a2422..801be72 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -175,9 +175,9 @@ endif
 if HAVE_NETLINK
 lib_libopenvswitch_a_SOURCES += \
 	lib/dpif-linux.c \
-	lib/netdev-gre.c \
 	lib/netdev-linux.c \
 	lib/netdev-patch.c \
+	lib/netdev-tunnel.c \
 	lib/netdev-vport.c \
 	lib/netdev-vport.h \
 	lib/netlink-protocol.h \
diff --git a/lib/netdev-gre.c b/lib/netdev-gre.c
deleted file mode 100644
index 9d9139f..0000000
--- a/lib/netdev-gre.c
+++ /dev/null
@@ -1,280 +0,0 @@
-/*
- * Copyright (c) 2010 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 <errno.h>
-#include <fcntl.h>
-#include <net/if.h>
-#include <sys/ioctl.h>
-
-#include "netdev-provider.h"
-#include "netdev-vport.h"
-#include "openflow/openflow.h"
-#include "openvswitch/datapath-protocol.h"
-#include "openvswitch/tunnel.h"
-#include "packets.h"
-#include "socket-util.h"
-#include "vlog.h"
-
-VLOG_DEFINE_THIS_MODULE(netdev_gre)
-
-struct netdev_dev_gre {
-    struct netdev_dev netdev_dev;
-};
-
-struct netdev_gre {
-    struct netdev netdev;
-};
-
-static struct netdev_dev_gre *
-netdev_dev_gre_cast(const struct netdev_dev *netdev_dev)
-{
-    netdev_dev_assert_class(netdev_dev, &netdev_gre_class);
-    return CONTAINER_OF(netdev_dev, struct netdev_dev_gre, netdev_dev);
-}
-
-static struct netdev_gre *
-netdev_gre_cast(const struct netdev *netdev)
-{
-    netdev_assert_class(netdev, &netdev_gre_class);
-    return CONTAINER_OF(netdev, struct netdev_gre, netdev);
-}
-
-static int
-parse_config(const char *name, const struct shash *args,
-             struct tnl_port_config *config)
-{
-    struct shash_node *node;
-
-    memset(config, 0, sizeof *config);
-
-    config->flags |= TNL_F_PMTUD;
-
-    SHASH_FOR_EACH (node, args) {
-        if (!strcmp(node->name, "remote_ip")) {
-            struct in_addr in_addr;
-            if (lookup_ip(node->data, &in_addr)) {
-                VLOG_WARN("%s: bad gre 'remote_ip'", name);
-            } else {
-                config->daddr = in_addr.s_addr;
-            }
-        } else if (!strcmp(node->name, "local_ip")) {
-            struct in_addr in_addr;
-            if (lookup_ip(node->data, &in_addr)) {
-                VLOG_WARN("%s: bad gre 'local_ip'", name);
-            } else {
-                config->saddr = in_addr.s_addr;
-            }
-        } else if (!strcmp(node->name, "key")) {
-            if (!strcmp(node->data, "flow")) {
-                config->flags |= TNL_F_IN_KEY_MATCH;
-                config->flags |= TNL_F_OUT_KEY_ACTION;
-            } else {
-                config->out_key = config->in_key = htonl(atoi(node->data));
-            }
-        } else if (!strcmp(node->name, "in_key")) {
-            if (!strcmp(node->data, "flow")) {
-                config->flags |= TNL_F_IN_KEY_MATCH;
-            } else {
-                config->in_key = htonl(atoi(node->data));
-            }
-        } else if (!strcmp(node->name, "out_key")) {
-            if (!strcmp(node->data, "flow")) {
-                config->flags |= TNL_F_OUT_KEY_ACTION;
-            } else {
-                config->out_key = htonl(atoi(node->data));
-            }
-        } else if (!strcmp(node->name, "tos")) {
-            if (!strcmp(node->data, "inherit")) {
-                config->flags |= TNL_F_TOS_INHERIT;
-            } else {
-                config->tos = atoi(node->data);
-            }
-        } else if (!strcmp(node->name, "ttl")) {
-            if (!strcmp(node->data, "inherit")) {
-                config->flags |= TNL_F_TTL_INHERIT;
-            } else {
-                config->ttl = atoi(node->data);
-            }
-        } else if (!strcmp(node->name, "csum")) {
-            if (!strcmp(node->data, "true")) {
-                config->flags |= TNL_F_CSUM;
-            }
-        } else if (!strcmp(node->name, "pmtud")) {
-            if (!strcmp(node->data, "false")) {
-                config->flags &= ~TNL_F_PMTUD;
-            }
-        } else {
-            VLOG_WARN("%s: unknown gre argument '%s'", name, node->name);
-        }
-    }
-
-    if (!config->daddr) {
-        VLOG_WARN("%s: gre type requires valid 'remote_ip' argument", name);
-        return EINVAL;
-    }
-
-    return 0;
-}
-
-static int
-netdev_gre_create(const char *name, const char *type OVS_UNUSED,
-                  const struct shash *args, struct netdev_dev **netdev_devp)
-{
-    int err;
-    struct odp_vport_add ova;
-    struct tnl_port_config port_config;
-    struct netdev_dev_gre *netdev_dev;
-
-    ovs_strlcpy(ova.port_type, "gre", sizeof ova.port_type);
-    ovs_strlcpy(ova.devname, name, sizeof ova.devname);
-    ova.config = &port_config;
-
-    err = parse_config(name, args, &port_config);
-    if (err) {
-        return err;
-    }
-
-    err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
-    if (err == EBUSY) {
-        VLOG_WARN("%s: destroying existing device", name);
-
-        err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
-        if (err) {
-            return err;
-        }
-
-        err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
-    }
-
-    if (err) {
-        return err;
-    }
-
-    netdev_dev = xmalloc(sizeof *netdev_dev);
-    netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
-
-    *netdev_devp = &netdev_dev->netdev_dev;
-    return 0;
-}
-
-static int
-netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
-{
-    const char *name = netdev_dev_get_name(netdev_dev_);
-    struct odp_vport_mod ovm;
-    struct tnl_port_config port_config;
-    int err;
-
-    ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
-    ovm.config = &port_config;
-
-    err = parse_config(name, args, &port_config);
-    if (err) {
-        return err;
-    }
-
-    return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
-}
-
-static void
-netdev_gre_destroy(struct netdev_dev *netdev_dev_)
-{
-    struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
-
-    netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
-    free(netdev_dev);
-}
-
-static int
-netdev_gre_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
-                struct netdev **netdevp)
-{
-    struct netdev_gre *netdev;
-
-    netdev = xmalloc(sizeof *netdev);
-    netdev_init(&netdev->netdev, netdev_dev_);
-
-    *netdevp = &netdev->netdev;
-    return 0;
-}
-
-static void
-netdev_gre_close(struct netdev *netdev_)
-{
-    struct netdev_gre *netdev = netdev_gre_cast(netdev_);
-    free(netdev);
-}
-
-const struct netdev_class netdev_gre_class = {
-    "gre",
-
-    NULL,                       /* init */
-    NULL,                       /* run */
-    NULL,                       /* wait */
-
-    netdev_gre_create,
-    netdev_gre_destroy,
-    netdev_gre_reconfigure,
-
-    netdev_gre_open,
-    netdev_gre_close,
-
-    NULL,                       /* enumerate */
-
-    NULL,                       /* recv */
-    NULL,                       /* recv_wait */
-    NULL,                       /* drain */
-
-    NULL,                       /* send */
-    NULL,                       /* send_wait */
-
-    netdev_vport_set_etheraddr,
-    netdev_vport_get_etheraddr,
-    netdev_vport_get_mtu,
-    NULL,                       /* get_ifindex */
-    netdev_vport_get_carrier,
-    netdev_vport_get_stats,
-    netdev_vport_set_stats,
-
-    NULL,                       /* get_features */
-    NULL,                       /* set_advertisements */
-    NULL,                       /* get_vlan_vid */
-
-    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,                       /* arp_lookup */
-
-    netdev_vport_update_flags,
-
-    netdev_vport_poll_add,
-    netdev_vport_poll_remove,
-};
diff --git a/lib/netdev-tunnel.c b/lib/netdev-tunnel.c
new file mode 100644
index 0000000..0497cbb
--- /dev/null
+++ b/lib/netdev-tunnel.c
@@ -0,0 +1,285 @@
+/*
+ * Copyright (c) 2010 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 <errno.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+#include "netdev-provider.h"
+#include "netdev-vport.h"
+#include "openflow/openflow.h"
+#include "openvswitch/datapath-protocol.h"
+#include "openvswitch/tunnel.h"
+#include "packets.h"
+#include "socket-util.h"
+#include "vlog.h"
+
+VLOG_DEFINE_THIS_MODULE(netdev_tunnel)
+
+struct netdev_dev_tunnel {
+    struct netdev_dev netdev_dev;
+};
+
+struct netdev_tunnel {
+    struct netdev netdev;
+};
+
+static int netdev_tunnel_create(const char *name, const char *type,
+                                const struct shash *args, struct netdev_dev **);
+
+static struct netdev_dev_tunnel *
+netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
+{
+    assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
+    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(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
+    return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
+}
+
+static int
+parse_config(const char *name, const char *type, const struct shash *args,
+             struct tnl_port_config *config)
+{
+    struct shash_node *node;
+
+    memset(config, 0, sizeof *config);
+
+    config->flags |= TNL_F_PMTUD;
+
+    SHASH_FOR_EACH (node, args) {
+        if (!strcmp(node->name, "remote_ip")) {
+            struct in_addr in_addr;
+            if (lookup_ip(node->data, &in_addr)) {
+                VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
+            } else {
+                config->daddr = in_addr.s_addr;
+            }
+        } else if (!strcmp(node->name, "local_ip")) {
+            struct in_addr in_addr;
+            if (lookup_ip(node->data, &in_addr)) {
+                VLOG_WARN("%s: bad %s 'local_ip'", name, type);
+            } else {
+                config->saddr = in_addr.s_addr;
+            }
+        } else if (!strcmp(node->name, "key")) {
+            if (!strcmp(node->data, "flow")) {
+                config->flags |= TNL_F_IN_KEY_MATCH;
+                config->flags |= TNL_F_OUT_KEY_ACTION;
+            } else {
+                config->out_key = config->in_key = htonl(atoi(node->data));
+            }
+        } else if (!strcmp(node->name, "in_key")) {
+            if (!strcmp(node->data, "flow")) {
+                config->flags |= TNL_F_IN_KEY_MATCH;
+            } else {
+                config->in_key = htonl(atoi(node->data));
+            }
+        } else if (!strcmp(node->name, "out_key")) {
+            if (!strcmp(node->data, "flow")) {
+                config->flags |= TNL_F_OUT_KEY_ACTION;
+            } else {
+                config->out_key = htonl(atoi(node->data));
+            }
+        } else if (!strcmp(node->name, "tos")) {
+            if (!strcmp(node->data, "inherit")) {
+                config->flags |= TNL_F_TOS_INHERIT;
+            } else {
+                config->tos = atoi(node->data);
+            }
+        } else if (!strcmp(node->name, "ttl")) {
+            if (!strcmp(node->data, "inherit")) {
+                config->flags |= TNL_F_TTL_INHERIT;
+            } else {
+                config->ttl = atoi(node->data);
+            }
+        } else if (!strcmp(node->name, "csum")) {
+            if (!strcmp(node->data, "true")) {
+                config->flags |= TNL_F_CSUM;
+            }
+        } else if (!strcmp(node->name, "pmtud")) {
+            if (!strcmp(node->data, "false")) {
+                config->flags &= ~TNL_F_PMTUD;
+            }
+        } else {
+            VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
+        }
+    }
+
+    if (!config->daddr) {
+        VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
+        return EINVAL;
+    }
+
+    return 0;
+}
+
+static int
+netdev_tunnel_create(const char *name, const char *type,
+                     const struct shash *args, struct netdev_dev **netdev_devp)
+{
+    int err;
+    struct odp_vport_add ova;
+    struct tnl_port_config port_config;
+    struct netdev_dev_tunnel *netdev_dev;
+
+    ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
+    ovs_strlcpy(ova.devname, name, sizeof ova.devname);
+    ova.config = &port_config;
+
+    err = parse_config(name, type, args, &port_config);
+    if (err) {
+        return err;
+    }
+
+    err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
+    if (err == EBUSY) {
+        VLOG_WARN("%s: destroying existing device", name);
+
+        err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
+        if (err) {
+            return err;
+        }
+
+        err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
+    }
+
+    if (err) {
+        return err;
+    }
+
+    netdev_dev = xmalloc(sizeof *netdev_dev);
+    netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
+
+    *netdev_devp = &netdev_dev->netdev_dev;
+    return 0;
+}
+
+static int
+netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
+{
+    const char *name = netdev_dev_get_name(netdev_dev_);
+    struct odp_vport_mod ovm;
+    struct tnl_port_config port_config;
+    int err;
+
+    ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
+    ovm.config = &port_config;
+
+    err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
+                       &port_config);
+    if (err) {
+        return err;
+    }
+
+    return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
+}
+
+static void
+netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
+{
+    struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
+
+    netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
+    free(netdev_dev);
+}
+
+static int
+netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
+                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);
+}
+
+const struct netdev_class netdev_gre_class = {
+    "gre",
+
+    NULL,                       /* init */
+    NULL,                       /* run */
+    NULL,                       /* wait */
+
+    netdev_tunnel_create,
+    netdev_tunnel_destroy,
+    netdev_tunnel_reconfigure,
+
+    netdev_tunnel_open,
+    netdev_tunnel_close,
+
+    NULL,                       /* enumerate */
+
+    NULL,                       /* recv */
+    NULL,                       /* recv_wait */
+    NULL,                       /* drain */
+
+    NULL,                       /* send */
+    NULL,                       /* send_wait */
+
+    netdev_vport_set_etheraddr,
+    netdev_vport_get_etheraddr,
+    netdev_vport_get_mtu,
+    NULL,                       /* get_ifindex */
+    netdev_vport_get_carrier,
+    netdev_vport_get_stats,
+    netdev_vport_set_stats,
+
+    NULL,                       /* get_features */
+    NULL,                       /* set_advertisements */
+    NULL,                       /* get_vlan_vid */
+
+    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,                       /* arp_lookup */
+
+    netdev_vport_update_flags,
+
+    netdev_vport_poll_add,
+    netdev_vport_poll_remove,
+};
diff --git a/lib/vlog-modules.def b/lib/vlog-modules.def
index b55472d..7e240cc 100644
--- a/lib/vlog-modules.def
+++ b/lib/vlog-modules.def
@@ -41,9 +41,9 @@ VLOG_MODULE(learning_switch)
 VLOG_MODULE(lockfile)
 VLOG_MODULE(mac_learning)
 VLOG_MODULE(netdev)
-VLOG_MODULE(netdev_gre)
 VLOG_MODULE(netdev_linux)
 VLOG_MODULE(netdev_patch)
+VLOG_MODULE(netdev_tunnel)
 VLOG_MODULE(netdev_vport)
 VLOG_MODULE(netflow)
 VLOG_MODULE(netlink)
-- 
1.7.0.4





More information about the dev mailing list