Skip to content

Commit 3c37745

Browse files
ogerlitzSaeed Mahameed
authored andcommitted
net/mlx5e: Properly deal with encap flows add/del under neigh update
Currently, the encap action offload is handled in the actions parse function and not in mlx5e_tc_add_fdb_flow() where we deal with all the other aspects of offloading actions (vlan, modify header) and the rule itself. When the neigh update code (mlx5e_tc_encap_flows_add()) recreates the encap entry and offloads the related flows, we wrongly call again into mlx5e_tc_add_fdb_flow(), this for itself would cause us to handle again the offloading of vlans and header re-write which puts things in non consistent state and step on freed memory (e.g the modify header parse buffer which is already freed). Since on error, mlx5e_tc_add_fdb_flow() detaches and may release the encap entry, it causes a corruption at the neigh update code which goes over the list of flows associated with this encap entry, or double free when the tc flow is later deleted by user-space. When neigh update (mlx5e_tc_encap_flows_del()) unoffloads the flows related to an encap entry which is now invalid, we do a partial repeat of the eswitch flow removal code which is wrong too. To fix things up we do the following: (1) handle the encap action offload in the eswitch flow add function mlx5e_tc_add_fdb_flow() as done for the other actions and the rule itself. (2) modify the neigh update code (mlx5e_tc_encap_flows_add/del) to only deal with the encap entry and rules delete/add and not with any of the other offloaded actions. Fixes: 232c001 ('net/mlx5e: Add support to neighbour update flow') Signed-off-by: Or Gerlitz <ogerlitz@mellanox.com> Reviewed-by: Paul Blakey <paulb@mellanox.com> Signed-off-by: Saeed Mahameed <saeedm@mellanox.com>
1 parent 4ca637a commit 3c37745

1 file changed

Lines changed: 54 additions & 35 deletions

File tree

  • drivers/net/ethernet/mellanox/mlx5/core

‎drivers/net/ethernet/mellanox/mlx5/core/en_tc.c‎

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ struct mlx5e_tc_flow {
7878
};
7979

8080
struct mlx5e_tc_flow_parse_attr {
81+
struct ip_tunnel_info tun_info;
8182
struct mlx5_flow_spec spec;
8283
int num_mod_hdr_actions;
8384
void *mod_hdr_actions;
85+
int mirred_ifindex;
8486
};
8587

8688
enum {
@@ -322,16 +324,40 @@ static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
322324
static void mlx5e_detach_encap(struct mlx5e_priv *priv,
323325
struct mlx5e_tc_flow *flow);
324326

327+
static int mlx5e_attach_encap(struct mlx5e_priv *priv,
328+
struct ip_tunnel_info *tun_info,
329+
struct net_device *mirred_dev,
330+
struct net_device **encap_dev,
331+
struct mlx5e_tc_flow *flow);
332+
325333
static struct mlx5_flow_handle *
326334
mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
327335
struct mlx5e_tc_flow_parse_attr *parse_attr,
328336
struct mlx5e_tc_flow *flow)
329337
{
330338
struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
331339
struct mlx5_esw_flow_attr *attr = flow->esw_attr;
332-
struct mlx5_flow_handle *rule;
340+
struct net_device *out_dev, *encap_dev = NULL;
341+
struct mlx5_flow_handle *rule = NULL;
342+
struct mlx5e_rep_priv *rpriv;
343+
struct mlx5e_priv *out_priv;
333344
int err;
334345

346+
if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
347+
out_dev = __dev_get_by_index(dev_net(priv->netdev),
348+
attr->parse_attr->mirred_ifindex);
349+
err = mlx5e_attach_encap(priv, &parse_attr->tun_info,
350+
out_dev, &encap_dev, flow);
351+
if (err) {
352+
rule = ERR_PTR(err);
353+
if (err != -EAGAIN)
354+
goto err_attach_encap;
355+
}
356+
out_priv = netdev_priv(encap_dev);
357+
rpriv = out_priv->ppriv;
358+
attr->out_rep = rpriv->rep;
359+
}
360+
335361
err = mlx5_eswitch_add_vlan_action(esw, attr);
336362
if (err) {
337363
rule = ERR_PTR(err);
@@ -347,10 +373,14 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
347373
}
348374
}
349375

350-
rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
351-
if (IS_ERR(rule))
352-
goto err_add_rule;
353-
376+
/* we get here if (1) there's no error (rule being null) or when
377+
* (2) there's an encap action and we're on -EAGAIN (no valid neigh)
378+
*/
379+
if (rule != ERR_PTR(-EAGAIN)) {
380+
rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
381+
if (IS_ERR(rule))
382+
goto err_add_rule;
383+
}
354384
return rule;
355385

356386
err_add_rule:
@@ -361,6 +391,7 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
361391
err_add_vlan:
362392
if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
363393
mlx5e_detach_encap(priv, flow);
394+
err_attach_encap:
364395
return rule;
365396
}
366397

@@ -389,6 +420,8 @@ static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
389420
void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
390421
struct mlx5e_encap_entry *e)
391422
{
423+
struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
424+
struct mlx5_esw_flow_attr *esw_attr;
392425
struct mlx5e_tc_flow *flow;
393426
int err;
394427

@@ -404,10 +437,9 @@ void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
404437
mlx5e_rep_queue_neigh_stats_work(priv);
405438

406439
list_for_each_entry(flow, &e->flows, encap) {
407-
flow->esw_attr->encap_id = e->encap_id;
408-
flow->rule = mlx5e_tc_add_fdb_flow(priv,
409-
flow->esw_attr->parse_attr,
410-
flow);
440+
esw_attr = flow->esw_attr;
441+
esw_attr->encap_id = e->encap_id;
442+
flow->rule = mlx5_eswitch_add_offloaded_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
411443
if (IS_ERR(flow->rule)) {
412444
err = PTR_ERR(flow->rule);
413445
mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
@@ -421,15 +453,13 @@ void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
421453
void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
422454
struct mlx5e_encap_entry *e)
423455
{
456+
struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
424457
struct mlx5e_tc_flow *flow;
425-
struct mlx5_fc *counter;
426458

427459
list_for_each_entry(flow, &e->flows, encap) {
428460
if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
429461
flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
430-
counter = mlx5_flow_rule_counter(flow->rule);
431-
mlx5_del_flow_rules(flow->rule);
432-
mlx5_fc_destroy(priv->mdev, counter);
462+
mlx5_eswitch_del_offloaded_rule(esw, flow->rule, flow->esw_attr);
433463
}
434464
}
435465

@@ -1942,7 +1972,7 @@ static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
19421972

19431973
if (is_tcf_mirred_egress_redirect(a)) {
19441974
int ifindex = tcf_mirred_ifindex(a);
1945-
struct net_device *out_dev, *encap_dev = NULL;
1975+
struct net_device *out_dev;
19461976
struct mlx5e_priv *out_priv;
19471977

19481978
out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
@@ -1955,17 +1985,13 @@ static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
19551985
rpriv = out_priv->ppriv;
19561986
attr->out_rep = rpriv->rep;
19571987
} else if (encap) {
1958-
err = mlx5e_attach_encap(priv, info,
1959-
out_dev, &encap_dev, flow);
1960-
if (err && err != -EAGAIN)
1961-
return err;
1988+
parse_attr->mirred_ifindex = ifindex;
1989+
parse_attr->tun_info = *info;
1990+
attr->parse_attr = parse_attr;
19621991
attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
19631992
MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
19641993
MLX5_FLOW_CONTEXT_ACTION_COUNT;
1965-
out_priv = netdev_priv(encap_dev);
1966-
rpriv = out_priv->ppriv;
1967-
attr->out_rep = rpriv->rep;
1968-
attr->parse_attr = parse_attr;
1994+
/* attr->out_rep is resolved when we handle encap */
19691995
} else {
19701996
pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
19711997
priv->netdev->name, out_dev->name);
@@ -2047,7 +2073,7 @@ int mlx5e_configure_flower(struct mlx5e_priv *priv,
20472073
if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
20482074
err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
20492075
if (err < 0)
2050-
goto err_handle_encap_flow;
2076+
goto err_free;
20512077
flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
20522078
} else {
20532079
err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
@@ -2058,10 +2084,13 @@ int mlx5e_configure_flower(struct mlx5e_priv *priv,
20582084

20592085
if (IS_ERR(flow->rule)) {
20602086
err = PTR_ERR(flow->rule);
2061-
goto err_free;
2087+
if (err != -EAGAIN)
2088+
goto err_free;
20622089
}
20632090

2064-
flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
2091+
if (err != -EAGAIN)
2092+
flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
2093+
20652094
err = rhashtable_insert_fast(&tc->ht, &flow->node,
20662095
tc->ht_params);
20672096
if (err)
@@ -2075,16 +2104,6 @@ int mlx5e_configure_flower(struct mlx5e_priv *priv,
20752104
err_del_rule:
20762105
mlx5e_tc_del_flow(priv, flow);
20772106

2078-
err_handle_encap_flow:
2079-
if (err == -EAGAIN) {
2080-
err = rhashtable_insert_fast(&tc->ht, &flow->node,
2081-
tc->ht_params);
2082-
if (err)
2083-
mlx5e_tc_del_flow(priv, flow);
2084-
else
2085-
return 0;
2086-
}
2087-
20882107
err_free:
20892108
kvfree(parse_attr);
20902109
kfree(flow);

0 commit comments

Comments
 (0)