datapath: Merge "struct dp_port" into "struct vport".
[openvswitch] / lib / dpif-netdev.c
index 3cecde7112270ab0b1143a74534ebb66e0895b6e..9f78be44ab1cc5a8de5f93db4b005dc962a024b0 100644 (file)
@@ -32,7 +32,9 @@
 #include <unistd.h>
 
 #include "csum.h"
+#include "dpif.h"
 #include "dpif-provider.h"
+#include "dummy.h"
 #include "flow.h"
 #include "hmap.h"
 #include "list.h"
@@ -62,6 +64,7 @@ enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
 
 /* Datapath based on the network device interface from netdev.h. */
 struct dp_netdev {
+    const struct dpif_class *class;
     char *name;
     int open_cnt;
     bool destroyed;
@@ -88,7 +91,7 @@ struct dp_netdev_port {
     int port_no;                /* Index into dp_netdev's 'ports'. */
     struct list node;           /* Element in dp_netdev's 'port_list'. */
     struct netdev *netdev;
-    bool internal;              /* Internal port (as ODP_PORT_INTERNAL)? */
+    bool internal;              /* Internal port? */
 };
 
 /* A flow in dp_netdev's 'flow_table'. */
@@ -127,19 +130,23 @@ static int get_port_by_name(struct dp_netdev *, const char *devname,
                             struct dp_netdev_port **portp);
 static void dp_netdev_free(struct dp_netdev *);
 static void dp_netdev_flow_flush(struct dp_netdev *);
-static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
-                       uint16_t port_no);
+static int do_add_port(struct dp_netdev *, const char *devname,
+                       const char *type, uint16_t port_no);
 static int do_del_port(struct dp_netdev *, uint16_t port_no);
+static int dpif_netdev_open(const struct dpif_class *, const char *name,
+                            bool create, struct dpif **);
 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
                                     int queue_no, int port_no, uint32_t arg);
 static int dp_netdev_execute_actions(struct dp_netdev *,
                                      struct ofpbuf *, struct flow *,
                                      const union odp_action *, int n);
 
+static struct dpif_class dpif_dummy_class;
+
 static struct dpif_netdev *
 dpif_netdev_cast(const struct dpif *dpif)
 {
-    dpif_assert_class(dpif, &dpif_netdev_class);
+    assert(dpif->dpif_class->open == dpif_netdev_open);
     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
 }
 
@@ -158,8 +165,7 @@ create_dpif_netdev(struct dp_netdev *dp)
     dp->open_cnt++;
 
     dpif = xmalloc(sizeof *dpif);
-    dpif_init(&dpif->dpif, &dpif_netdev_class, dp->name,
-              netflow_id >> 8, netflow_id);
+    dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
     dpif->dp = dp;
     dpif->listen_mask = 0;
     dpif->dp_serial = dp->serial;
@@ -168,13 +174,15 @@ create_dpif_netdev(struct dp_netdev *dp)
 }
 
 static int
-create_dp_netdev(const char *name, struct dp_netdev **dpp)
+create_dp_netdev(const char *name, const struct dpif_class *class,
+                 struct dp_netdev **dpp)
 {
     struct dp_netdev *dp;
     int error;
     int i;
 
     dp = xzalloc(sizeof *dp);
+    dp->class = class;
     dp->name = xstrdup(name);
     dp->open_cnt = 0;
     dp->drop_frags = false;
@@ -183,7 +191,7 @@ create_dp_netdev(const char *name, struct dp_netdev **dpp)
     }
     hmap_init(&dp->flow_table);
     list_init(&dp->port_list);
-    error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
+    error = do_add_port(dp, name, "internal", ODPP_LOCAL);
     if (error) {
         dp_netdev_free(dp);
         return error;
@@ -196,7 +204,7 @@ create_dp_netdev(const char *name, struct dp_netdev **dpp)
 }
 
 static int
-dpif_netdev_open(const struct dpif_class *class OVS_UNUSED, const char *name,
+dpif_netdev_open(const struct dpif_class *class, const char *name,
                  bool create, struct dpif **dpifp)
 {
     struct dp_netdev *dp;
@@ -206,14 +214,16 @@ dpif_netdev_open(const struct dpif_class *class OVS_UNUSED, const char *name,
         if (!create) {
             return ENODEV;
         } else {
-            int error = create_dp_netdev(name, &dp);
+            int error = create_dp_netdev(name, class, &dp);
             if (error) {
                 return error;
             }
             assert(dp != NULL);
         }
     } else {
-        if (create) {
+        if (dp->class != class) {
+            return EINVAL;
+        } else if (create) {
             return EEXIST;
         }
     }
@@ -297,23 +307,33 @@ dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
 }
 
 static int
-do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
+do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
             uint16_t port_no)
 {
-    bool internal = (flags & ODP_PORT_INTERNAL) != 0;
     struct dp_netdev_port *port;
     struct netdev_options netdev_options;
     struct netdev *netdev;
+    bool internal;
     int mtu;
     int error;
 
     /* XXX reject devices already in some dp_netdev. */
+    if (type[0] == '\0' || !strcmp(type, "system")) {
+        internal = false;
+    } else if (!strcmp(type, "internal")) {
+        internal = true;
+    } else {
+        VLOG_WARN("%s: unsupported port type %s", devname, type);
+        return EINVAL;
+    }
 
     /* Open and validate network device. */
     memset(&netdev_options, 0, sizeof netdev_options);
     netdev_options.name = devname;
     netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
-    if (internal) {
+    if (dp->class == &dpif_dummy_class) {
+        netdev_options.type = "dummy";
+    } else if (internal) {
         netdev_options.type = "tap";
     }
 
@@ -349,7 +369,7 @@ do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
 }
 
 static int
-dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
+dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
                      uint16_t *port_nop)
 {
     struct dp_netdev *dp = get_dp_netdev(dpif);
@@ -358,7 +378,8 @@ dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
         if (!dp->ports[port_no]) {
             *port_nop = port_no;
-            return do_add_port(dp, devname, flags, port_no);
+            return do_add_port(dp, netdev_get_name(netdev),
+                               netdev_get_type(netdev), port_no);
         }
     }
     return EFBIG;
@@ -438,7 +459,7 @@ answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
     ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
                 sizeof odp_port->devname);
     odp_port->port = port->port_no;
-    odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
+    strcpy(odp_port->type, port->internal ? "internal" : "system");
 }
 
 static int
@@ -1249,3 +1270,13 @@ const struct dpif_class dpif_netdev_class = {
     dpif_netdev_recv,
     dpif_netdev_recv_wait,
 };
+
+void
+dpif_dummy_register(void)
+{
+    if (!dpif_dummy_class.type) {
+        dpif_dummy_class = dpif_netdev_class;
+        dpif_dummy_class.type = "dummy";
+        dp_register_provider(&dpif_dummy_class);
+    }
+}