Factor datapath common code into new function lookup_dp().
authorBen Pfaff <blp@nicira.com>
Tue, 30 Dec 2008 20:33:11 +0000 (12:33 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 30 Dec 2008 20:33:11 +0000 (12:33 -0800)
datapath/datapath.c

index 5e1e70adb3702c8f1bc40d05b829742cbc948c3a..a98a98fe58705c344c9f4a5cfa2d119c471d1204 100644 (file)
@@ -1147,31 +1147,42 @@ struct datapath *dp_get_by_name(const char *dp_name)
        return NULL;
 }
 
-static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
+static struct datapath *
+lookup_dp(struct genl_info *info)
 {
        int dp_idx = info->attrs[DP_GENL_A_DP_IDX] ?
                        nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]) : -1;
        const char *dp_name = info->attrs[DP_GENL_A_DP_NAME] ?
                        nla_data(info->attrs[DP_GENL_A_DP_NAME]) : NULL;
        struct datapath *dp = NULL;
-       int err;
 
        if ((dp_idx == -1) && (!dp_name))
-               return -EINVAL;
+               return ERR_PTR(-EINVAL);
 
-       if (dp_idx != -1)
+       if (dp_idx != -1) {
                dp = dp_get_by_idx(dp_idx);
+               if (!dp)
+                       return ERR_PTR(-ENOENT);
+       }
 
        if (dp_name) {
-               struct datapath *dp_n;
-               dp_n = dp_get_by_name(dp_name);
+               struct datapath *dp_n = dp_get_by_name(dp_name);
                if (dp && dp != dp_n)
-                       return -EINVAL;
+                       return ERR_PTR(-EINVAL);
                dp = dp_n;
        }
 
-       if (!dp)
-               err = -ENOENT;
+       return dp ? dp : ERR_PTR(-ENOENT);
+}
+
+static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
+{
+       struct datapath *dp;
+       int err;
+
+       dp = lookup_dp(info);
+       if (IS_ERR(dp))
+               err = PTR_ERR(dp);
        else {
                del_dp(dp);
                err = 0;
@@ -1202,23 +1213,12 @@ static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
 {
        struct datapath *dp;
        struct sk_buff *ans_skb = NULL;
-       int err = -ENOMEM;
-       int dp_idx = info->attrs[DP_GENL_A_DP_IDX] ? 
-                       nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]) : -1;
-       const char *dp_name = info->attrs[DP_GENL_A_DP_NAME] ?
-                       nla_data(info->attrs[DP_GENL_A_DP_NAME]) : NULL;
-
-       if ((dp_idx == -1) && (!dp_name))
-               return -EINVAL;
+       int err;
 
        rcu_read_lock();
-       if (dp_idx == -1)
-               dp = dp_get_by_name(dp_name);
-       else
-               dp = dp_get_by_idx(dp_idx);
-
-       if (!dp)
-               err = -ENOENT;
+       dp = lookup_dp(info);
+       if (IS_ERR(dp))
+               err = PTR_ERR(dp);
        else {
                void *data;
                ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
@@ -1226,12 +1226,11 @@ static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
                        err = -ENOMEM;
                        goto err;
                }
+               err = -ENOMEM;
                data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
                                         0, DP_GENL_C_QUERY_DP);
-               if (data == NULL) {
-                       err = -ENOMEM;
+               if (data == NULL)
                        goto err;
-               }
                NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
                NLA_PUT_STRING(ans_skb, DP_GENL_A_DP_NAME, dp->netdev->name);
                NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
@@ -1261,13 +1260,13 @@ static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
        struct net_device *port;
        int err;
 
-       if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
+       if (!info->attrs[DP_GENL_A_PORTNAME])
                return -EINVAL;
 
        /* Get datapath. */
-       dp = dp_get_by_idx(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
-       if (!dp) {
-               err = -ENOENT;
+       dp = lookup_dp(info);
+       if (IS_ERR(dp)) {
+               err = PTR_ERR(dp);
                goto out;
        }