]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
net/mlx5: Refactor mlx5_add_flow_rule
[karo-tx-linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.c
index b3de09f13425f39bc0afbc70e8966dcc36d49206..3261e8b1286e073dc64b05439054f695249fc240 100644 (file)
@@ -46,43 +46,65 @@ struct mlx5e_tc_flow {
        struct mlx5_flow_rule   *rule;
 };
 
-#define MLX5E_TC_FLOW_TABLE_NUM_ENTRIES 1024
-#define MLX5E_TC_FLOW_TABLE_NUM_GROUPS 4
+#define MLX5E_TC_TABLE_NUM_ENTRIES 1024
+#define MLX5E_TC_TABLE_NUM_GROUPS 4
 
 static struct mlx5_flow_rule *mlx5e_tc_add_flow(struct mlx5e_priv *priv,
-                                               u32 *match_c, u32 *match_v,
+                                               struct mlx5_flow_spec *spec,
                                                u32 action, u32 flow_tag)
 {
-       struct mlx5_flow_destination dest = {
-               .type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE,
-               {.ft = priv->fts.vlan.t},
-       };
+       struct mlx5_core_dev *dev = priv->mdev;
+       struct mlx5_flow_destination dest = { 0 };
+       struct mlx5_fc *counter = NULL;
        struct mlx5_flow_rule *rule;
        bool table_created = false;
 
-       if (IS_ERR_OR_NULL(priv->fts.tc.t)) {
-               priv->fts.tc.t =
-                       mlx5_create_auto_grouped_flow_table(priv->fts.ns, 0,
-                                                           MLX5E_TC_FLOW_TABLE_NUM_ENTRIES,
-                                                           MLX5E_TC_FLOW_TABLE_NUM_GROUPS);
-               if (IS_ERR(priv->fts.tc.t)) {
+       if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
+               dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
+               dest.ft = priv->fs.vlan.ft.t;
+       } else {
+               counter = mlx5_fc_create(dev, true);
+               if (IS_ERR(counter))
+                       return ERR_CAST(counter);
+
+               dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
+               dest.counter = counter;
+       }
+
+       if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
+               priv->fs.tc.t =
+                       mlx5_create_auto_grouped_flow_table(priv->fs.ns,
+                                                           MLX5E_TC_PRIO,
+                                                           MLX5E_TC_TABLE_NUM_ENTRIES,
+                                                           MLX5E_TC_TABLE_NUM_GROUPS,
+                                                           0);
+               if (IS_ERR(priv->fs.tc.t)) {
                        netdev_err(priv->netdev,
                                   "Failed to create tc offload table\n");
-                       return ERR_CAST(priv->fts.tc.t);
+                       rule = ERR_CAST(priv->fs.tc.t);
+                       goto err_create_ft;
                }
 
                table_created = true;
        }
 
-       rule = mlx5_add_flow_rule(priv->fts.tc.t, MLX5_MATCH_OUTER_HEADERS,
-                                 match_c, match_v,
+       spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
+       rule = mlx5_add_flow_rule(priv->fs.tc.t, spec,
                                  action, flow_tag,
-                                 action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ? &dest : NULL);
+                                 &dest);
+
+       if (IS_ERR(rule))
+               goto err_add_rule;
+
+       return rule;
 
-       if (IS_ERR(rule) && table_created) {
-               mlx5_destroy_flow_table(priv->fts.tc.t);
-               priv->fts.tc.t = NULL;
+err_add_rule:
+       if (table_created) {
+               mlx5_destroy_flow_table(priv->fs.tc.t);
+               priv->fs.tc.t = NULL;
        }
+err_create_ft:
+       mlx5_fc_destroy(dev, counter);
 
        return rule;
 }
@@ -90,20 +112,27 @@ static struct mlx5_flow_rule *mlx5e_tc_add_flow(struct mlx5e_priv *priv,
 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
                              struct mlx5_flow_rule *rule)
 {
+       struct mlx5_fc *counter = NULL;
+
+       counter = mlx5_flow_rule_counter(rule);
+
        mlx5_del_flow_rule(rule);
 
+       mlx5_fc_destroy(priv->mdev, counter);
+
        if (!mlx5e_tc_num_filters(priv)) {
-               mlx5_destroy_flow_table(priv->fts.tc.t);
-               priv->fts.tc.t = NULL;
+               mlx5_destroy_flow_table(priv->fs.tc.t);
+               priv->fs.tc.t = NULL;
        }
 }
 
-static int parse_cls_flower(struct mlx5e_priv *priv,
-                           u32 *match_c, u32 *match_v,
+static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
                            struct tc_cls_flower_offload *f)
 {
-       void *headers_c = MLX5_ADDR_OF(fte_match_param, match_c, outer_headers);
-       void *headers_v = MLX5_ADDR_OF(fte_match_param, match_v, outer_headers);
+       void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
+                                      outer_headers);
+       void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
+                                      outer_headers);
        u16 addr_type = 0;
        u8 ip_proto = 0;
 
@@ -284,6 +313,9 @@ static int parse_tc_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
 
                if (is_tcf_gact_shot(a)) {
                        *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
+                       if (MLX5_CAP_FLOWTABLE(priv->mdev,
+                                              flow_table_properties_nic_receive.flow_counter))
+                               *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
                        continue;
                }
 
@@ -310,13 +342,12 @@ static int parse_tc_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
                           struct tc_cls_flower_offload *f)
 {
-       struct mlx5e_tc_flow_table *tc = &priv->fts.tc;
-       u32 *match_c;
-       u32 *match_v;
+       struct mlx5e_tc_table *tc = &priv->fs.tc;
        int err = 0;
        u32 flow_tag;
        u32 action;
        struct mlx5e_tc_flow *flow;
+       struct mlx5_flow_spec *spec;
        struct mlx5_flow_rule *old = NULL;
 
        flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
@@ -326,16 +357,15 @@ int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
        else
                flow = kzalloc(sizeof(*flow), GFP_KERNEL);
 
-       match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       if (!match_c || !match_v || !flow) {
+       spec = mlx5_vzalloc(sizeof(*spec));
+       if (!spec || !flow) {
                err = -ENOMEM;
                goto err_free;
        }
 
        flow->cookie = f->cookie;
 
-       err = parse_cls_flower(priv, match_c, match_v, f);
+       err = parse_cls_flower(priv, spec, f);
        if (err < 0)
                goto err_free;
 
@@ -348,8 +378,7 @@ int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
        if (err)
                goto err_free;
 
-       flow->rule = mlx5e_tc_add_flow(priv, match_c, match_v, action,
-                                      flow_tag);
+       flow->rule = mlx5e_tc_add_flow(priv, spec, action, flow_tag);
        if (IS_ERR(flow->rule)) {
                err = PTR_ERR(flow->rule);
                goto err_hash_del;
@@ -367,8 +396,7 @@ err_free:
        if (!old)
                kfree(flow);
 out:
-       kfree(match_c);
-       kfree(match_v);
+       kvfree(spec);
        return err;
 }
 
@@ -376,7 +404,7 @@ int mlx5e_delete_flower(struct mlx5e_priv *priv,
                        struct tc_cls_flower_offload *f)
 {
        struct mlx5e_tc_flow *flow;
-       struct mlx5e_tc_flow_table *tc = &priv->fts.tc;
+       struct mlx5e_tc_table *tc = &priv->fs.tc;
 
        flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
                                      tc->ht_params);
@@ -392,6 +420,34 @@ int mlx5e_delete_flower(struct mlx5e_priv *priv,
        return 0;
 }
 
+int mlx5e_stats_flower(struct mlx5e_priv *priv,
+                      struct tc_cls_flower_offload *f)
+{
+       struct mlx5e_tc_table *tc = &priv->fs.tc;
+       struct mlx5e_tc_flow *flow;
+       struct tc_action *a;
+       struct mlx5_fc *counter;
+       u64 bytes;
+       u64 packets;
+       u64 lastuse;
+
+       flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
+                                     tc->ht_params);
+       if (!flow)
+               return -EINVAL;
+
+       counter = mlx5_flow_rule_counter(flow->rule);
+       if (!counter)
+               return 0;
+
+       mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
+
+       tc_for_each_action(a, f->exts)
+               tcf_action_stats_update(a, bytes, packets, lastuse);
+
+       return 0;
+}
+
 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
        .head_offset = offsetof(struct mlx5e_tc_flow, node),
        .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
@@ -401,7 +457,7 @@ static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
 
 int mlx5e_tc_init(struct mlx5e_priv *priv)
 {
-       struct mlx5e_tc_flow_table *tc = &priv->fts.tc;
+       struct mlx5e_tc_table *tc = &priv->fs.tc;
 
        tc->ht_params = mlx5e_tc_flow_ht_params;
        return rhashtable_init(&tc->ht, &tc->ht_params);
@@ -418,12 +474,12 @@ static void _mlx5e_tc_del_flow(void *ptr, void *arg)
 
 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
 {
-       struct mlx5e_tc_flow_table *tc = &priv->fts.tc;
+       struct mlx5e_tc_table *tc = &priv->fs.tc;
 
        rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
 
-       if (!IS_ERR_OR_NULL(priv->fts.tc.t)) {
-               mlx5_destroy_flow_table(priv->fts.tc.t);
-               priv->fts.tc.t = NULL;
+       if (!IS_ERR_OR_NULL(tc->t)) {
+               mlx5_destroy_flow_table(tc->t);
+               tc->t = NULL;
        }
 }