Skip to content

Commit f0d22d1

Browse files
Maor Gottliebdavem330
authored andcommitted
net/mlx5_core: Introduce flow steering autogrouped flow table
When user add rule to autogrouped flow table, we search for flow group with the same match criteria, if we don't find such group then we create new flow group with the required match criteria and insert the rule to this group. We divide the flow table into required_groups + 1, in order to reserve a part of the flow table for rules which don't match any existing group. Signed-off-by: Maor Gottlieb <maorg@mellanox.com> Signed-off-by: Moni Shoua <monis@mellanox.com> Signed-off-by: Matan Barak <matanb@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 23c09c2 commit f0d22d1

3 files changed

Lines changed: 158 additions & 19 deletions

File tree

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

Lines changed: 147 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ static struct init_tree_node {
8585
}
8686
};
8787

88+
enum fs_i_mutex_lock_class {
89+
FS_MUTEX_GRANDPARENT,
90+
FS_MUTEX_PARENT,
91+
FS_MUTEX_CHILD
92+
};
93+
8894
static void del_rule(struct fs_node *node);
8995
static void del_flow_table(struct fs_node *node);
9096
static void del_flow_group(struct fs_node *node);
@@ -119,10 +125,11 @@ static void tree_get_node(struct fs_node *node)
119125
atomic_inc(&node->refcount);
120126
}
121127

122-
static void nested_lock_ref_node(struct fs_node *node)
128+
static void nested_lock_ref_node(struct fs_node *node,
129+
enum fs_i_mutex_lock_class class)
123130
{
124131
if (node) {
125-
mutex_lock_nested(&node->lock, SINGLE_DEPTH_NESTING);
132+
mutex_lock_nested(&node->lock, class);
126133
atomic_inc(&node->refcount);
127134
}
128135
}
@@ -481,18 +488,40 @@ struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
481488
list_add_tail(&ft->node.list, &fs_prio->node.children);
482489
fs_prio->num_ft++;
483490
unlock_ref_node(&fs_prio->node);
484-
485491
return ft;
486-
487492
free_ft:
488493
kfree(ft);
489494
unlock_prio:
490495
unlock_ref_node(&fs_prio->node);
491496
return ERR_PTR(err);
492497
}
493498

494-
struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
495-
u32 *fg_in)
499+
struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
500+
int prio,
501+
int num_flow_table_entries,
502+
int max_num_groups)
503+
{
504+
struct mlx5_flow_table *ft;
505+
506+
if (max_num_groups > num_flow_table_entries)
507+
return ERR_PTR(-EINVAL);
508+
509+
ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
510+
if (IS_ERR(ft))
511+
return ft;
512+
513+
ft->autogroup.active = true;
514+
ft->autogroup.required_groups = max_num_groups;
515+
516+
return ft;
517+
}
518+
519+
/* Flow table should be locked */
520+
static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
521+
u32 *fg_in,
522+
struct list_head
523+
*prev_fg,
524+
bool is_auto_fg)
496525
{
497526
struct mlx5_flow_group *fg;
498527
struct mlx5_core_dev *dev = get_dev(&ft->node);
@@ -505,18 +534,33 @@ struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
505534
if (IS_ERR(fg))
506535
return fg;
507536

508-
lock_ref_node(&ft->node);
509537
err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
510538
if (err) {
511539
kfree(fg);
512-
unlock_ref_node(&ft->node);
513540
return ERR_PTR(err);
514541
}
542+
543+
if (ft->autogroup.active)
544+
ft->autogroup.num_groups++;
515545
/* Add node to tree */
516-
tree_init_node(&fg->node, 1, del_flow_group);
546+
tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
517547
tree_add_node(&fg->node, &ft->node);
518548
/* Add node to group list */
519549
list_add(&fg->node.list, ft->node.children.prev);
550+
551+
return fg;
552+
}
553+
554+
struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
555+
u32 *fg_in)
556+
{
557+
struct mlx5_flow_group *fg;
558+
559+
if (ft->autogroup.active)
560+
return ERR_PTR(-EPERM);
561+
562+
lock_ref_node(&ft->node);
563+
fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
520564
unlock_ref_node(&ft->node);
521565

522566
return fg;
@@ -614,7 +658,63 @@ static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
614658
return fte;
615659
}
616660

617-
/* Assuming parent fg(flow table) is locked */
661+
static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
662+
u8 match_criteria_enable,
663+
u32 *match_criteria)
664+
{
665+
int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
666+
struct list_head *prev = &ft->node.children;
667+
unsigned int candidate_index = 0;
668+
struct mlx5_flow_group *fg;
669+
void *match_criteria_addr;
670+
unsigned int group_size = 0;
671+
u32 *in;
672+
673+
if (!ft->autogroup.active)
674+
return ERR_PTR(-ENOENT);
675+
676+
in = mlx5_vzalloc(inlen);
677+
if (!in)
678+
return ERR_PTR(-ENOMEM);
679+
680+
if (ft->autogroup.num_groups < ft->autogroup.required_groups)
681+
/* We save place for flow groups in addition to max types */
682+
group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
683+
684+
/* ft->max_fte == ft->autogroup.max_types */
685+
if (group_size == 0)
686+
group_size = 1;
687+
688+
/* sorted by start_index */
689+
fs_for_each_fg(fg, ft) {
690+
if (candidate_index + group_size > fg->start_index)
691+
candidate_index = fg->start_index + fg->max_ftes;
692+
else
693+
break;
694+
prev = &fg->node.list;
695+
}
696+
697+
if (candidate_index + group_size > ft->max_fte) {
698+
fg = ERR_PTR(-ENOSPC);
699+
goto out;
700+
}
701+
702+
MLX5_SET(create_flow_group_in, in, match_criteria_enable,
703+
match_criteria_enable);
704+
MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
705+
MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
706+
group_size - 1);
707+
match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
708+
in, match_criteria);
709+
memcpy(match_criteria_addr, match_criteria,
710+
MLX5_ST_SZ_BYTES(fte_match_param));
711+
712+
fg = create_flow_group_common(ft, in, prev, true);
713+
out:
714+
kvfree(in);
715+
return fg;
716+
}
717+
618718
static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
619719
u32 *match_value,
620720
u8 action,
@@ -626,9 +726,9 @@ static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
626726
struct mlx5_flow_table *ft;
627727
struct list_head *prev;
628728

629-
lock_ref_node(&fg->node);
729+
nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
630730
fs_for_each_fte(fte, fg) {
631-
nested_lock_ref_node(&fte->node);
731+
nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
632732
if (compare_match_value(&fg->mask, match_value, &fte->val) &&
633733
action == fte->action && flow_tag == fte->flow_tag) {
634734
rule = add_rule_fte(fte, fg, dest);
@@ -669,6 +769,33 @@ static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
669769
return rule;
670770
}
671771

772+
static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
773+
u8 match_criteria_enable,
774+
u32 *match_criteria,
775+
u32 *match_value,
776+
u8 action,
777+
u32 flow_tag,
778+
struct mlx5_flow_destination *dest)
779+
{
780+
struct mlx5_flow_rule *rule;
781+
struct mlx5_flow_group *g;
782+
783+
g = create_autogroup(ft, match_criteria_enable, match_criteria);
784+
if (IS_ERR(g))
785+
return (void *)g;
786+
787+
rule = add_rule_fg(g, match_value,
788+
action, flow_tag, dest);
789+
if (IS_ERR(rule)) {
790+
/* Remove assumes refcount > 0 and autogroup creates a group
791+
* with a refcount = 0.
792+
*/
793+
tree_get_node(&g->node);
794+
tree_remove_node(&g->node);
795+
}
796+
return rule;
797+
}
798+
672799
struct mlx5_flow_rule *
673800
mlx5_add_flow_rule(struct mlx5_flow_table *ft,
674801
u8 match_criteria_enable,
@@ -679,23 +806,24 @@ mlx5_add_flow_rule(struct mlx5_flow_table *ft,
679806
struct mlx5_flow_destination *dest)
680807
{
681808
struct mlx5_flow_group *g;
682-
struct mlx5_flow_rule *rule = ERR_PTR(-EINVAL);
809+
struct mlx5_flow_rule *rule;
683810

684-
tree_get_node(&ft->node);
685-
lock_ref_node(&ft->node);
811+
nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
686812
fs_for_each_fg(g, ft)
687813
if (compare_match_criteria(g->mask.match_criteria_enable,
688814
match_criteria_enable,
689815
g->mask.match_criteria,
690816
match_criteria)) {
691-
unlock_ref_node(&ft->node);
692817
rule = add_rule_fg(g, match_value,
693818
action, flow_tag, dest);
694-
goto put;
819+
if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
820+
goto unlock;
695821
}
822+
823+
rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
824+
match_value, action, flow_tag, dest);
825+
unlock:
696826
unlock_ref_node(&ft->node);
697-
put:
698-
tree_put_node(&ft->node);
699827
return rule;
700828
}
701829

‎drivers/net/ethernet/mellanox/mlx5/core/fs_core.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ struct mlx5_flow_table {
7777
unsigned int max_fte;
7878
unsigned int level;
7979
enum fs_flow_table_type type;
80+
struct {
81+
bool active;
82+
unsigned int required_groups;
83+
unsigned int num_groups;
84+
} autogroup;
8085
};
8186

8287
/* Type of children is mlx5_flow_rule */

‎include/linux/mlx5/fs.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ struct mlx5_flow_namespace *
6161
mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
6262
enum mlx5_flow_namespace_type type);
6363

64+
struct mlx5_flow_table *
65+
mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
66+
int prio,
67+
int num_flow_table_entries,
68+
int max_num_groups);
69+
6470
struct mlx5_flow_table *
6571
mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
6672
int prio,

0 commit comments

Comments
 (0)