Commit 893f139b authored by Jarno Rajahalme's avatar Jarno Rajahalme Committed by Pravin B Shelar

openvswitch: Minimize ovs_flow_cmd_new|set critical sections.

Signed-off-by: default avatarJarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: default avatarPravin B Shelar <pshelar@nicira.com>
parent 37bdc87b
...@@ -796,8 +796,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) ...@@ -796,8 +796,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
{ {
struct nlattr **a = info->attrs; struct nlattr **a = info->attrs;
struct ovs_header *ovs_header = info->userhdr; struct ovs_header *ovs_header = info->userhdr;
struct sw_flow_key key, masked_key; struct sw_flow *flow, *new_flow;
struct sw_flow *flow;
struct sw_flow_mask mask; struct sw_flow_mask mask;
struct sk_buff *reply; struct sk_buff *reply;
struct datapath *dp; struct datapath *dp;
...@@ -805,61 +804,77 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) ...@@ -805,61 +804,77 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
struct sw_flow_match match; struct sw_flow_match match;
int error; int error;
/* Extract key. */ /* Must have key and actions. */
error = -EINVAL; error = -EINVAL;
if (!a[OVS_FLOW_ATTR_KEY]) if (!a[OVS_FLOW_ATTR_KEY])
goto error; goto error;
if (!a[OVS_FLOW_ATTR_ACTIONS])
goto error;
ovs_match_init(&match, &key, &mask); /* Most of the time we need to allocate a new flow, do it before
* locking.
*/
new_flow = ovs_flow_alloc();
if (IS_ERR(new_flow)) {
error = PTR_ERR(new_flow);
goto error;
}
/* Extract key. */
ovs_match_init(&match, &new_flow->unmasked_key, &mask);
error = ovs_nla_get_match(&match, error = ovs_nla_get_match(&match,
a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
if (error) if (error)
goto error; goto err_kfree_flow;
/* Validate actions. */ ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
error = -EINVAL;
if (!a[OVS_FLOW_ATTR_ACTIONS])
goto error;
/* Validate actions. */
acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
error = PTR_ERR(acts); error = PTR_ERR(acts);
if (IS_ERR(acts)) if (IS_ERR(acts))
goto error; goto err_kfree_flow;
ovs_flow_mask_key(&masked_key, &key, &mask); error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], 0, &acts);
&masked_key, 0, &acts);
if (error) { if (error) {
OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
goto err_kfree; goto err_kfree_acts;
}
reply = ovs_flow_cmd_alloc_info(acts, info, false);
if (IS_ERR(reply)) {
error = PTR_ERR(reply);
goto err_kfree_acts;
} }
ovs_lock(); ovs_lock();
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
if (unlikely(!dp)) {
error = -ENODEV; error = -ENODEV;
if (!dp)
goto err_unlock_ovs;
/* Check if this is a duplicate flow */
flow = ovs_flow_tbl_lookup(&dp->table, &key);
if (!flow) {
/* Allocate flow. */
flow = ovs_flow_alloc();
if (IS_ERR(flow)) {
error = PTR_ERR(flow);
goto err_unlock_ovs; goto err_unlock_ovs;
} }
/* Check if this is a duplicate flow */
flow->key = masked_key; flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
flow->unmasked_key = key; if (likely(!flow)) {
rcu_assign_pointer(flow->sf_acts, acts); rcu_assign_pointer(new_flow->sf_acts, acts);
/* Put flow in bucket. */ /* Put flow in bucket. */
error = ovs_flow_tbl_insert(&dp->table, flow, &mask); error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
if (error) { if (unlikely(error)) {
acts = NULL; acts = NULL;
goto err_flow_free; goto err_unlock_ovs;
}
if (unlikely(reply)) {
error = ovs_flow_cmd_fill_info(new_flow,
ovs_header->dp_ifindex,
reply, info->snd_portid,
info->snd_seq, 0,
OVS_FLOW_CMD_NEW);
BUG_ON(error < 0);
} }
ovs_unlock();
} else { } else {
struct sw_flow_actions *old_acts; struct sw_flow_actions *old_acts;
...@@ -869,39 +884,45 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) ...@@ -869,39 +884,45 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
* request. We also accept NLM_F_EXCL in case that bug ever * request. We also accept NLM_F_EXCL in case that bug ever
* gets fixed. * gets fixed.
*/ */
if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
| NLM_F_EXCL))) {
error = -EEXIST; error = -EEXIST;
if (info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
goto err_unlock_ovs; goto err_unlock_ovs;
}
/* The unmasked key has to be the same for flow updates. */ /* The unmasked key has to be the same for flow updates. */
if (!ovs_flow_cmp_unmasked_key(flow, &match)) if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
error = -EEXIST;
goto err_unlock_ovs; goto err_unlock_ovs;
}
/* Update actions. */ /* Update actions. */
old_acts = ovsl_dereference(flow->sf_acts); old_acts = ovsl_dereference(flow->sf_acts);
rcu_assign_pointer(flow->sf_acts, acts); rcu_assign_pointer(flow->sf_acts, acts);
ovs_nla_free_flow_actions(old_acts);
}
reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, if (unlikely(reply)) {
info, OVS_FLOW_CMD_NEW, false); error = ovs_flow_cmd_fill_info(flow,
ovs_header->dp_ifindex,
reply, info->snd_portid,
info->snd_seq, 0,
OVS_FLOW_CMD_NEW);
BUG_ON(error < 0);
}
ovs_unlock(); ovs_unlock();
if (reply) { ovs_nla_free_flow_actions(old_acts);
if (!IS_ERR(reply)) ovs_flow_free(new_flow, false);
ovs_notify(&dp_flow_genl_family, reply, info);
else
netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0,
PTR_ERR(reply));
} }
if (reply)
ovs_notify(&dp_flow_genl_family, reply, info);
return 0; return 0;
err_flow_free:
ovs_flow_free(flow, false);
err_unlock_ovs: err_unlock_ovs:
ovs_unlock(); ovs_unlock();
err_kfree: kfree_skb(reply);
err_kfree_acts:
kfree(acts); kfree(acts);
err_kfree_flow:
ovs_flow_free(new_flow, false);
error: error:
return error; return error;
} }
...@@ -915,7 +936,7 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) ...@@ -915,7 +936,7 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
struct sw_flow_mask mask; struct sw_flow_mask mask;
struct sk_buff *reply = NULL; struct sk_buff *reply = NULL;
struct datapath *dp; struct datapath *dp;
struct sw_flow_actions *acts = NULL; struct sw_flow_actions *old_acts = NULL, *acts = NULL;
struct sw_flow_match match; struct sw_flow_match match;
int error; int error;
...@@ -942,56 +963,75 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) ...@@ -942,56 +963,75 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
&masked_key, 0, &acts); &masked_key, 0, &acts);
if (error) { if (error) {
OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
goto err_kfree; goto err_kfree_acts;
}
}
/* Can allocate before locking if have acts. */
if (acts) {
reply = ovs_flow_cmd_alloc_info(acts, info, false);
if (IS_ERR(reply)) {
error = PTR_ERR(reply);
goto err_kfree_acts;
} }
} }
ovs_lock(); ovs_lock();
dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
if (unlikely(!dp)) {
error = -ENODEV; error = -ENODEV;
if (!dp)
goto err_unlock_ovs; goto err_unlock_ovs;
}
/* Check that the flow exists. */ /* Check that the flow exists. */
flow = ovs_flow_tbl_lookup(&dp->table, &key); flow = ovs_flow_tbl_lookup(&dp->table, &key);
if (unlikely(!flow)) {
error = -ENOENT; error = -ENOENT;
if (!flow)
goto err_unlock_ovs; goto err_unlock_ovs;
}
/* The unmasked key has to be the same for flow updates. */ /* The unmasked key has to be the same for flow updates. */
if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
error = -EEXIST; error = -EEXIST;
if (!ovs_flow_cmp_unmasked_key(flow, &match))
goto err_unlock_ovs; goto err_unlock_ovs;
}
/* Update actions, if present. */ /* Update actions, if present. */
if (acts) { if (likely(acts)) {
struct sw_flow_actions *old_acts;
old_acts = ovsl_dereference(flow->sf_acts); old_acts = ovsl_dereference(flow->sf_acts);
rcu_assign_pointer(flow->sf_acts, acts); rcu_assign_pointer(flow->sf_acts, acts);
ovs_nla_free_flow_actions(old_acts);
}
if (unlikely(reply)) {
error = ovs_flow_cmd_fill_info(flow,
ovs_header->dp_ifindex,
reply, info->snd_portid,
info->snd_seq, 0,
OVS_FLOW_CMD_NEW);
BUG_ON(error < 0);
}
} else {
/* Could not alloc without acts before locking. */
reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
info, OVS_FLOW_CMD_NEW, false); info, OVS_FLOW_CMD_NEW, false);
if (unlikely(IS_ERR(reply))) {
error = PTR_ERR(reply);
goto err_unlock_ovs;
}
}
/* Clear stats. */ /* Clear stats. */
if (a[OVS_FLOW_ATTR_CLEAR]) if (a[OVS_FLOW_ATTR_CLEAR])
ovs_flow_stats_clear(flow); ovs_flow_stats_clear(flow);
ovs_unlock(); ovs_unlock();
if (reply) { if (reply)
if (!IS_ERR(reply))
ovs_notify(&dp_flow_genl_family, reply, info); ovs_notify(&dp_flow_genl_family, reply, info);
else if (old_acts)
genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0, ovs_nla_free_flow_actions(old_acts);
0, PTR_ERR(reply));
}
return 0; return 0;
err_unlock_ovs: err_unlock_ovs:
ovs_unlock(); ovs_unlock();
err_kfree: kfree_skb(reply);
err_kfree_acts:
kfree(acts); kfree(acts);
error: error:
return error; return error;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment