[ovs-dev] [PATCH] dpif: Refactor obj type from void pointer to dpif_class

Roi Dayan roid at mellanox.com
Tue Jul 25 05:28:41 UTC 2017


It's basically what is being passed today and passing a specific
type adds a compiler type check.

Signed-off-by: Roi Dayan <roid at mellanox.com>
Reviewed-by: Paul Blakey <paulb at mellanox.com>
---
 lib/dpif-netlink.c       | 15 ++++++------
 lib/dpif.c               |  6 ++---
 lib/dpif.h               |  2 --
 lib/netdev-tc-offloads.c |  3 +--
 lib/netdev.c             | 59 ++++++++++++++++++++++++++----------------------
 lib/netdev.h             | 21 +++++++++--------
 6 files changed, 56 insertions(+), 50 deletions(-)

diff --git a/lib/dpif-netlink.c b/lib/dpif-netlink.c
index 55effd1..dbaead7 100644
--- a/lib/dpif-netlink.c
+++ b/lib/dpif-netlink.c
@@ -1160,7 +1160,7 @@ dpif_netlink_flow_flush(struct dpif *dpif_)
     flow.dp_ifindex = dpif->dp_ifindex;
 
     if (netdev_is_flow_api_enabled()) {
-        netdev_ports_flow_flush(DPIF_HMAP_KEY(dpif_));
+        netdev_ports_flow_flush(dpif_->dpif_class);
     }
 
     return dpif_netlink_flow_transact(&flow, NULL, NULL);
@@ -1487,7 +1487,7 @@ start_netdev_dump(const struct dpif *dpif_,
     ovs_mutex_lock(&dump->netdev_lock);
     dump->netdev_current_dump = 0;
     dump->netdev_dumps
-        = netdev_ports_flow_dump_create(DPIF_HMAP_KEY(dpif_),
+        = netdev_ports_flow_dump_create(dpif_->dpif_class,
                                         &dump->netdev_dumps_num);
     ovs_mutex_unlock(&dump->netdev_lock);
 }
@@ -2047,7 +2047,7 @@ parse_flow_get(struct dpif_netlink *dpif, struct dpif_flow_get *get)
     int err;
 
     ofpbuf_use_stack(&buf, &act_buf, sizeof act_buf);
-    err = netdev_ports_flow_get(DPIF_HMAP_KEY(&dpif->dpif), &match,
+    err = netdev_ports_flow_get(dpif->dpif.dpif_class, &match,
                                 &actions, get->ufid, &stats, &buf);
     if (err) {
         return err;
@@ -2074,6 +2074,7 @@ static int
 parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
 {
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+    const struct dpif_class *dpif_class = dpif->dpif.dpif_class;
     struct match match;
     odp_port_t in_port;
     const struct nlattr *nla;
@@ -2100,7 +2101,7 @@ parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
     }
 
     in_port = match.flow.in_port.odp_port;
-    dev = netdev_ports_get(in_port, DPIF_HMAP_KEY(&dpif->dpif));
+    dev = netdev_ports_get(in_port, dpif_class);
     if (!dev) {
         return EOPNOTSUPP;
     }
@@ -2120,7 +2121,7 @@ parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
             }
 
             out_port = nl_attr_get_odp_port(nla);
-            outdev = netdev_ports_get(out_port, DPIF_HMAP_KEY(&dpif->dpif));
+            outdev = netdev_ports_get(out_port, dpif_class);
             if (!outdev) {
                 err = EOPNOTSUPP;
                 goto out;
@@ -2133,7 +2134,7 @@ parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put)
         }
     }
 
-    info.port_hmap_obj = DPIF_HMAP_KEY(&dpif->dpif);
+    info.dpif_class = dpif_class;
     info.tp_dst_port = dst_port;
     err = netdev_flow_put(dev, &match,
                           CONST_CAST(struct nlattr *, put->actions),
@@ -2211,7 +2212,7 @@ try_send_to_netdev(struct dpif_netlink *dpif, struct dpif_op *op)
         }
 
         log_flow_del_message(&dpif->dpif, &this_module, del, 0);
-        err = netdev_ports_flow_del(DPIF_HMAP_KEY(&dpif->dpif), del->ufid,
+        err = netdev_ports_flow_del(dpif->dpif.dpif_class, del->ufid,
                                     del->stats);
         break;
     }
diff --git a/lib/dpif.c b/lib/dpif.c
index 4d6ecf8..1af53f5 100644
--- a/lib/dpif.c
+++ b/lib/dpif.c
@@ -366,7 +366,7 @@ do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
             err = netdev_open(dpif_port.name, dpif_port.type, &netdev);
 
             if (!err) {
-                netdev_ports_insert(netdev, DPIF_HMAP_KEY(dpif), &dpif_port);
+                netdev_ports_insert(netdev, dpif->dpif_class, &dpif_port);
                 netdev_close(netdev);
             } else {
                 VLOG_WARN("could not open netdev %s type %s: %s",
@@ -573,7 +573,7 @@ dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop)
             dpif_port.type = CONST_CAST(char *, netdev_get_type(netdev));
             dpif_port.name = CONST_CAST(char *, netdev_name);
             dpif_port.port_no = port_no;
-            netdev_ports_insert(netdev, DPIF_HMAP_KEY(dpif), &dpif_port);
+            netdev_ports_insert(netdev, dpif->dpif_class, &dpif_port);
         }
     } else {
         VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
@@ -600,7 +600,7 @@ dpif_port_del(struct dpif *dpif, odp_port_t port_no)
         VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu32")",
                     dpif_name(dpif), port_no);
 
-        netdev_ports_remove(port_no, DPIF_HMAP_KEY(dpif));
+        netdev_ports_remove(port_no, dpif->dpif_class);
     } else {
         log_operation(dpif, "port_del", error);
     }
diff --git a/lib/dpif.h b/lib/dpif.h
index d1ac571..d9ded8b 100644
--- a/lib/dpif.h
+++ b/lib/dpif.h
@@ -401,8 +401,6 @@
 extern "C" {
 #endif
 
-#define DPIF_HMAP_KEY(x) ((x)->dpif_class)
-
 struct dpif;
 struct dpif_class;
 struct dpif_flow;
diff --git a/lib/netdev-tc-offloads.c b/lib/netdev-tc-offloads.c
index 6214023..c98c259 100644
--- a/lib/netdev-tc-offloads.c
+++ b/lib/netdev-tc-offloads.c
@@ -786,8 +786,7 @@ netdev_tc_flow_put(struct netdev *netdev, struct match *match,
     NL_ATTR_FOR_EACH(nla, left, actions, actions_len) {
         if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) {
             odp_port_t port = nl_attr_get_odp_port(nla);
-            struct netdev *outdev = netdev_ports_get(port,
-                                                     info->port_hmap_obj);
+            struct netdev *outdev = netdev_ports_get(port, info->dpif_class);
 
             flower.ifindex_out = netdev_get_ifindex(outdev);
             flower.set.tp_dst = info->tp_dst_port;
diff --git a/lib/netdev.c b/lib/netdev.c
index 0d5fad5..bc71ddb 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -2152,7 +2152,7 @@ struct port_to_netdev_data {
     struct hmap_node node;
     struct netdev *netdev;
     struct dpif_port dpif_port;
-    const void *obj;
+    const struct dpif_class *dpif_class;
 };
 
 struct ifindex_to_port_data {
@@ -2161,15 +2161,20 @@ struct ifindex_to_port_data {
     odp_port_t port;
 };
 
+#define NETDEV_PORTS_HASH_INT(port, dpif) \
+                        hash_int(odp_to_u32(port),\
+                            hash_pointer(dpif, 0));
+
 static struct port_to_netdev_data *
-netdev_ports_lookup(odp_port_t port_no, const void *obj)
+netdev_ports_lookup(odp_port_t port_no, const struct dpif_class *dpif_class)
     OVS_REQUIRES(netdev_hmap_mutex)
 {
-    size_t hash = hash_int(odp_to_u32(port_no), hash_pointer(obj, 0));
+    size_t hash = NETDEV_PORTS_HASH_INT(port_no, dpif_class);
     struct port_to_netdev_data *data;
 
     HMAP_FOR_EACH_WITH_HASH(data, node, hash, &port_to_netdev) {
-            if (data->obj == obj && data->dpif_port.port_no == port_no) {
+            if (data->dpif_class == dpif_class
+                && data->dpif_port.port_no == port_no) {
                 return data;
             }
     }
@@ -2177,11 +2182,10 @@ netdev_ports_lookup(odp_port_t port_no, const void *obj)
 }
 
 int
-netdev_ports_insert(struct netdev *netdev, const void *obj,
+netdev_ports_insert(struct netdev *netdev, const struct dpif_class *dpif_class,
                     struct dpif_port *dpif_port)
 {
-    size_t hash = hash_int(odp_to_u32(dpif_port->port_no),
-                           hash_pointer(obj, 0));
+    size_t hash = NETDEV_PORTS_HASH_INT(dpif_port->port_no, dpif_class);
     struct port_to_netdev_data *data;
     struct ifindex_to_port_data *ifidx;
     int ifindex = netdev_get_ifindex(netdev);
@@ -2194,13 +2198,13 @@ netdev_ports_insert(struct netdev *netdev, const void *obj,
     ifidx = xzalloc(sizeof *ifidx);
 
     ovs_mutex_lock(&netdev_hmap_mutex);
-    if (netdev_ports_lookup(dpif_port->port_no, obj)) {
+    if (netdev_ports_lookup(dpif_port->port_no, dpif_class)) {
         ovs_mutex_unlock(&netdev_hmap_mutex);
         return EEXIST;
     }
 
     data->netdev = netdev_ref(netdev);
-    data->obj = obj;
+    data->dpif_class = dpif_class;
     dpif_port_clone(&data->dpif_port, dpif_port);
 
     ifidx->ifindex = ifindex;
@@ -2216,13 +2220,13 @@ netdev_ports_insert(struct netdev *netdev, const void *obj,
 }
 
 struct netdev *
-netdev_ports_get(odp_port_t port_no, const void *obj)
+netdev_ports_get(odp_port_t port_no, const struct dpif_class *dpif_class)
 {
     struct port_to_netdev_data *data;
     struct netdev *ret = NULL;
 
     ovs_mutex_lock(&netdev_hmap_mutex);
-    data = netdev_ports_lookup(port_no, obj);
+    data = netdev_ports_lookup(port_no, dpif_class);
     if (data) {
         ret = netdev_ref(data->netdev);
     }
@@ -2232,14 +2236,14 @@ netdev_ports_get(odp_port_t port_no, const void *obj)
 }
 
 int
-netdev_ports_remove(odp_port_t port_no, const void *obj)
+netdev_ports_remove(odp_port_t port_no, const struct dpif_class *dpif_class)
 {
     struct port_to_netdev_data *data;
     int ret = ENOENT;
 
     ovs_mutex_lock(&netdev_hmap_mutex);
 
-    data = netdev_ports_lookup(port_no, obj);
+    data = netdev_ports_lookup(port_no, dpif_class);
 
     if (data) {
         dpif_port_destroy(&data->dpif_port);
@@ -2273,13 +2277,13 @@ netdev_ifindex_to_odp_port(int ifindex)
 }
 
 void
-netdev_ports_flow_flush(const void *obj)
+netdev_ports_flow_flush(const struct dpif_class *dpif_class)
 {
     struct port_to_netdev_data *data;
 
     ovs_mutex_lock(&netdev_hmap_mutex);
     HMAP_FOR_EACH(data, node, &port_to_netdev) {
-        if (data->obj == obj) {
+        if (data->dpif_class == dpif_class) {
             netdev_flow_flush(data->netdev);
         }
     }
@@ -2287,7 +2291,7 @@ netdev_ports_flow_flush(const void *obj)
 }
 
 struct netdev_flow_dump **
-netdev_ports_flow_dump_create(const void *obj, int *ports)
+netdev_ports_flow_dump_create(const struct dpif_class *dpif_class, int *ports)
 {
     struct port_to_netdev_data *data;
     struct netdev_flow_dump **dumps;
@@ -2296,7 +2300,7 @@ netdev_ports_flow_dump_create(const void *obj, int *ports)
 
     ovs_mutex_lock(&netdev_hmap_mutex);
     HMAP_FOR_EACH(data, node, &port_to_netdev) {
-        if (data->obj == obj) {
+        if (data->dpif_class == dpif_class) {
             count++;
         }
     }
@@ -2304,7 +2308,7 @@ netdev_ports_flow_dump_create(const void *obj, int *ports)
     dumps = count ? xzalloc(sizeof *dumps * count) : NULL;
 
     HMAP_FOR_EACH(data, node, &port_to_netdev) {
-        if (data->obj == obj) {
+        if (data->dpif_class == dpif_class) {
             if (netdev_flow_dump_create(data->netdev, &dumps[i])) {
                 continue;
             }
@@ -2320,14 +2324,16 @@ netdev_ports_flow_dump_create(const void *obj, int *ports)
 }
 
 int
-netdev_ports_flow_del(const void *obj, const ovs_u128 *ufid,
+netdev_ports_flow_del(const struct dpif_class *dpif_class,
+                      const ovs_u128 *ufid,
                       struct dpif_flow_stats *stats)
 {
     struct port_to_netdev_data *data;
 
     ovs_mutex_lock(&netdev_hmap_mutex);
     HMAP_FOR_EACH(data, node, &port_to_netdev) {
-        if (data->obj == obj && !netdev_flow_del(data->netdev, ufid, stats)) {
+        if (data->dpif_class == dpif_class
+            && !netdev_flow_del(data->netdev, ufid, stats)) {
             ovs_mutex_unlock(&netdev_hmap_mutex);
             return 0;
         }
@@ -2338,18 +2344,17 @@ netdev_ports_flow_del(const void *obj, const ovs_u128 *ufid,
 }
 
 int
-netdev_ports_flow_get(const void *obj, struct match *match,
-                      struct nlattr **actions,
-                      const ovs_u128 *ufid,
-                      struct dpif_flow_stats *stats,
-                      struct ofpbuf *buf)
+netdev_ports_flow_get(const struct dpif_class *dpif_class, struct match *match,
+                      struct nlattr **actions, const ovs_u128 *ufid,
+                      struct dpif_flow_stats *stats, struct ofpbuf *buf)
 {
     struct port_to_netdev_data *data;
 
     ovs_mutex_lock(&netdev_hmap_mutex);
     HMAP_FOR_EACH(data, node, &port_to_netdev) {
-        if (data->obj == obj && !netdev_flow_get(data->netdev, match, actions,
-                                                 ufid, stats, buf)) {
+        if (data->dpif_class == dpif_class
+            && !netdev_flow_get(data->netdev, match, actions,
+                                ufid, stats, buf)) {
             ovs_mutex_unlock(&netdev_hmap_mutex);
             return 0;
         }
diff --git a/lib/netdev.h b/lib/netdev.h
index 998f942..f8482f7 100644
--- a/lib/netdev.h
+++ b/lib/netdev.h
@@ -186,9 +186,10 @@ void netdev_send_wait(struct netdev *, int qid);
 
 /* Flow offloading. */
 struct offload_info {
-    const void *port_hmap_obj; /* To query ports info from netdev port map */
+    const struct dpif_class *dpif_class;
     ovs_be16 tp_dst_port; /* Destination port for tunnel in SET action */
 };
+struct dpif_class;
 struct netdev_flow_dump;
 int netdev_flow_flush(struct netdev *);
 int netdev_flow_dump_create(struct netdev *, struct netdev_flow_dump **dump);
@@ -210,16 +211,18 @@ bool netdev_is_flow_api_enabled(void);
 void netdev_set_flow_api_enabled(const struct smap *ovs_other_config);
 
 struct dpif_port;
-int netdev_ports_insert(struct netdev *, const void *obj, struct dpif_port *);
-struct netdev *netdev_ports_get(odp_port_t port, const void *obj);
-int netdev_ports_remove(odp_port_t port, const void *obj);
+int netdev_ports_insert(struct netdev *, const struct dpif_class *,
+                        struct dpif_port *);
+struct netdev *netdev_ports_get(odp_port_t port, const struct dpif_class *);
+int netdev_ports_remove(odp_port_t port, const struct dpif_class *);
 odp_port_t netdev_ifindex_to_odp_port(int ifindex);
-struct netdev_flow_dump **netdev_ports_flow_dump_create(const void *obj,
-                                                        int *ports);
-void netdev_ports_flow_flush(const void *obj);
-int netdev_ports_flow_del(const void *obj, const ovs_u128 *ufid,
+struct netdev_flow_dump **netdev_ports_flow_dump_create(
+                                        const struct dpif_class *,
+                                        int *ports);
+void netdev_ports_flow_flush(const struct dpif_class *);
+int netdev_ports_flow_del(const struct dpif_class *, const ovs_u128 *ufid,
                           struct dpif_flow_stats *stats);
-int netdev_ports_flow_get(const void *obj, struct match *match,
+int netdev_ports_flow_get(const struct dpif_class *, struct match *match,
                           struct nlattr **actions,
                           const ovs_u128 *ufid,
                           struct dpif_flow_stats *stats,
-- 
2.7.4



More information about the dev mailing list