2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
36 #include "mlx5_core.h"
40 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
41 sizeof(struct init_tree_node))
43 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
44 ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .num_levels = num_levels_val,\
47 .num_leaf_prios = num_prios_val,\
49 .children = (struct init_tree_node[]) {__VA_ARGS__},\
50 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
53 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
54 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
57 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
58 .children = (struct init_tree_node[]) {__VA_ARGS__},\
59 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
62 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
65 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
67 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
68 .caps = (long[]) {__VA_ARGS__} }
70 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
71 FS_CAP(flow_table_properties_nic_receive.modify_root), \
72 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
73 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
75 #define LEFTOVERS_NUM_LEVELS 1
76 #define LEFTOVERS_NUM_PRIOS 1
78 #define BY_PASS_PRIO_NUM_LEVELS 1
79 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
82 #define ETHTOOL_PRIO_NUM_LEVELS 1
83 #define ETHTOOL_NUM_PRIOS 11
84 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
85 /* Vlan, mac, ttc, aRFS */
86 #define KERNEL_NIC_PRIO_NUM_LEVELS 4
87 #define KERNEL_NIC_NUM_PRIOS 1
88 /* One more level for tc */
89 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
91 #define ANCHOR_NUM_LEVELS 1
92 #define ANCHOR_NUM_PRIOS 1
93 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
95 #define OFFLOADS_MAX_FT 1
96 #define OFFLOADS_NUM_PRIOS 1
97 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
99 #define LAG_PRIO_NUM_LEVELS 1
100 #define LAG_NUM_PRIOS 1
101 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
107 static struct init_tree_node
{
108 enum fs_node_type type
;
109 struct init_tree_node
*children
;
111 struct node_caps caps
;
117 .type
= FS_TYPE_NAMESPACE
,
119 .children
= (struct init_tree_node
[]) {
120 ADD_PRIO(0, BY_PASS_MIN_LEVEL
, 0,
122 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS
,
123 BY_PASS_PRIO_NUM_LEVELS
))),
124 ADD_PRIO(0, LAG_MIN_LEVEL
, 0,
126 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS
,
127 LAG_PRIO_NUM_LEVELS
))),
128 ADD_PRIO(0, OFFLOADS_MIN_LEVEL
, 0, {},
129 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS
, OFFLOADS_MAX_FT
))),
130 ADD_PRIO(0, ETHTOOL_MIN_LEVEL
, 0,
132 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS
,
133 ETHTOOL_PRIO_NUM_LEVELS
))),
134 ADD_PRIO(0, KERNEL_MIN_LEVEL
, 0, {},
135 ADD_NS(ADD_MULTIPLE_PRIO(1, 1),
136 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS
,
137 KERNEL_NIC_PRIO_NUM_LEVELS
))),
138 ADD_PRIO(0, BY_PASS_MIN_LEVEL
, 0,
140 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS
, LEFTOVERS_NUM_LEVELS
))),
141 ADD_PRIO(0, ANCHOR_MIN_LEVEL
, 0, {},
142 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS
, ANCHOR_NUM_LEVELS
))),
146 enum fs_i_mutex_lock_class
{
147 FS_MUTEX_GRANDPARENT
,
152 static void del_rule(struct fs_node
*node
);
153 static void del_flow_table(struct fs_node
*node
);
154 static void del_flow_group(struct fs_node
*node
);
155 static void del_fte(struct fs_node
*node
);
156 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination
*d1
,
157 struct mlx5_flow_destination
*d2
);
158 static struct mlx5_flow_rule
*
159 find_flow_rule(struct fs_fte
*fte
,
160 struct mlx5_flow_destination
*dest
);
162 static void tree_init_node(struct fs_node
*node
,
163 unsigned int refcount
,
164 void (*remove_func
)(struct fs_node
*))
166 atomic_set(&node
->refcount
, refcount
);
167 INIT_LIST_HEAD(&node
->list
);
168 INIT_LIST_HEAD(&node
->children
);
169 mutex_init(&node
->lock
);
170 node
->remove_func
= remove_func
;
173 static void tree_add_node(struct fs_node
*node
, struct fs_node
*parent
)
176 atomic_inc(&parent
->refcount
);
177 node
->parent
= parent
;
179 /* Parent is the root */
183 node
->root
= parent
->root
;
186 static void tree_get_node(struct fs_node
*node
)
188 atomic_inc(&node
->refcount
);
191 static void nested_lock_ref_node(struct fs_node
*node
,
192 enum fs_i_mutex_lock_class
class)
195 mutex_lock_nested(&node
->lock
, class);
196 atomic_inc(&node
->refcount
);
200 static void lock_ref_node(struct fs_node
*node
)
203 mutex_lock(&node
->lock
);
204 atomic_inc(&node
->refcount
);
208 static void unlock_ref_node(struct fs_node
*node
)
211 atomic_dec(&node
->refcount
);
212 mutex_unlock(&node
->lock
);
216 static void tree_put_node(struct fs_node
*node
)
218 struct fs_node
*parent_node
= node
->parent
;
220 lock_ref_node(parent_node
);
221 if (atomic_dec_and_test(&node
->refcount
)) {
223 list_del_init(&node
->list
);
224 if (node
->remove_func
)
225 node
->remove_func(node
);
229 unlock_ref_node(parent_node
);
230 if (!node
&& parent_node
)
231 tree_put_node(parent_node
);
234 static int tree_remove_node(struct fs_node
*node
)
236 if (atomic_read(&node
->refcount
) > 1) {
237 atomic_dec(&node
->refcount
);
244 static struct fs_prio
*find_prio(struct mlx5_flow_namespace
*ns
,
247 struct fs_prio
*iter_prio
;
249 fs_for_each_prio(iter_prio
, ns
) {
250 if (iter_prio
->prio
== prio
)
257 static bool masked_memcmp(void *mask
, void *val1
, void *val2
, size_t size
)
261 for (i
= 0; i
< size
; i
++, mask
++, val1
++, val2
++)
262 if ((*((u8
*)val1
) & (*(u8
*)mask
)) !=
263 ((*(u8
*)val2
) & (*(u8
*)mask
)))
269 static bool compare_match_value(struct mlx5_flow_group_mask
*mask
,
270 void *fte_param1
, void *fte_param2
)
272 if (mask
->match_criteria_enable
&
273 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS
) {
274 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
275 fte_param1
, outer_headers
);
276 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
277 fte_param2
, outer_headers
);
278 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
279 mask
->match_criteria
, outer_headers
);
281 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
282 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4
)))
286 if (mask
->match_criteria_enable
&
287 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS
) {
288 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
289 fte_param1
, misc_parameters
);
290 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
291 fte_param2
, misc_parameters
);
292 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
293 mask
->match_criteria
, misc_parameters
);
295 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
296 MLX5_ST_SZ_BYTES(fte_match_set_misc
)))
300 if (mask
->match_criteria_enable
&
301 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS
) {
302 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
303 fte_param1
, inner_headers
);
304 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
305 fte_param2
, inner_headers
);
306 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
307 mask
->match_criteria
, inner_headers
);
309 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
310 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4
)))
316 static bool compare_match_criteria(u8 match_criteria_enable1
,
317 u8 match_criteria_enable2
,
318 void *mask1
, void *mask2
)
320 return match_criteria_enable1
== match_criteria_enable2
&&
321 !memcmp(mask1
, mask2
, MLX5_ST_SZ_BYTES(fte_match_param
));
324 static struct mlx5_flow_root_namespace
*find_root(struct fs_node
*node
)
326 struct fs_node
*root
;
327 struct mlx5_flow_namespace
*ns
;
331 if (WARN_ON(root
->type
!= FS_TYPE_NAMESPACE
)) {
332 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
336 ns
= container_of(root
, struct mlx5_flow_namespace
, node
);
337 return container_of(ns
, struct mlx5_flow_root_namespace
, ns
);
340 static inline struct mlx5_core_dev
*get_dev(struct fs_node
*node
)
342 struct mlx5_flow_root_namespace
*root
= find_root(node
);
349 static void del_flow_table(struct fs_node
*node
)
351 struct mlx5_flow_table
*ft
;
352 struct mlx5_core_dev
*dev
;
353 struct fs_prio
*prio
;
356 fs_get_obj(ft
, node
);
357 dev
= get_dev(&ft
->node
);
359 err
= mlx5_cmd_destroy_flow_table(dev
, ft
);
361 mlx5_core_warn(dev
, "flow steering can't destroy ft\n");
362 fs_get_obj(prio
, ft
->node
.parent
);
366 static void del_rule(struct fs_node
*node
)
368 struct mlx5_flow_rule
*rule
;
369 struct mlx5_flow_table
*ft
;
370 struct mlx5_flow_group
*fg
;
374 struct mlx5_core_dev
*dev
= get_dev(node
);
375 int match_len
= MLX5_ST_SZ_BYTES(fte_match_param
);
377 bool update_fte
= false;
379 match_value
= mlx5_vzalloc(match_len
);
381 mlx5_core_warn(dev
, "failed to allocate inbox\n");
385 fs_get_obj(rule
, node
);
386 fs_get_obj(fte
, rule
->node
.parent
);
387 fs_get_obj(fg
, fte
->node
.parent
);
388 memcpy(match_value
, fte
->val
, sizeof(fte
->val
));
389 fs_get_obj(ft
, fg
->node
.parent
);
390 list_del(&rule
->node
.list
);
391 if (rule
->sw_action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
392 mutex_lock(&rule
->dest_attr
.ft
->lock
);
393 list_del(&rule
->next_ft
);
394 mutex_unlock(&rule
->dest_attr
.ft
->lock
);
397 if (rule
->dest_attr
.type
== MLX5_FLOW_DESTINATION_TYPE_COUNTER
&&
399 modify_mask
= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION
);
400 fte
->action
&= ~MLX5_FLOW_CONTEXT_ACTION_COUNT
;
405 if ((fte
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
) &&
407 modify_mask
= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST
),
411 if (update_fte
&& fte
->dests_size
) {
412 err
= mlx5_cmd_update_fte(dev
, ft
, fg
->id
, modify_mask
, fte
);
415 "%s can't del rule fg id=%d fte_index=%d\n",
416 __func__
, fg
->id
, fte
->index
);
421 static void del_fte(struct fs_node
*node
)
423 struct mlx5_flow_table
*ft
;
424 struct mlx5_flow_group
*fg
;
425 struct mlx5_core_dev
*dev
;
429 fs_get_obj(fte
, node
);
430 fs_get_obj(fg
, fte
->node
.parent
);
431 fs_get_obj(ft
, fg
->node
.parent
);
433 dev
= get_dev(&ft
->node
);
434 err
= mlx5_cmd_delete_fte(dev
, ft
,
438 "flow steering can't delete fte in index %d of flow group id %d\n",
445 static void del_flow_group(struct fs_node
*node
)
447 struct mlx5_flow_group
*fg
;
448 struct mlx5_flow_table
*ft
;
449 struct mlx5_core_dev
*dev
;
451 fs_get_obj(fg
, node
);
452 fs_get_obj(ft
, fg
->node
.parent
);
453 dev
= get_dev(&ft
->node
);
455 if (ft
->autogroup
.active
)
456 ft
->autogroup
.num_groups
--;
458 if (mlx5_cmd_destroy_flow_group(dev
, ft
, fg
->id
))
459 mlx5_core_warn(dev
, "flow steering can't destroy fg %d of ft %d\n",
463 static struct fs_fte
*alloc_fte(struct mlx5_flow_act
*flow_act
,
469 fte
= kzalloc(sizeof(*fte
), GFP_KERNEL
);
471 return ERR_PTR(-ENOMEM
);
473 memcpy(fte
->val
, match_value
, sizeof(fte
->val
));
474 fte
->node
.type
= FS_TYPE_FLOW_ENTRY
;
475 fte
->flow_tag
= flow_act
->flow_tag
;
477 fte
->action
= flow_act
->action
;
478 fte
->encap_id
= flow_act
->encap_id
;
483 static struct mlx5_flow_group
*alloc_flow_group(u32
*create_fg_in
)
485 struct mlx5_flow_group
*fg
;
486 void *match_criteria
= MLX5_ADDR_OF(create_flow_group_in
,
487 create_fg_in
, match_criteria
);
488 u8 match_criteria_enable
= MLX5_GET(create_flow_group_in
,
490 match_criteria_enable
);
491 fg
= kzalloc(sizeof(*fg
), GFP_KERNEL
);
493 return ERR_PTR(-ENOMEM
);
495 fg
->mask
.match_criteria_enable
= match_criteria_enable
;
496 memcpy(&fg
->mask
.match_criteria
, match_criteria
,
497 sizeof(fg
->mask
.match_criteria
));
498 fg
->node
.type
= FS_TYPE_FLOW_GROUP
;
499 fg
->start_index
= MLX5_GET(create_flow_group_in
, create_fg_in
,
501 fg
->max_ftes
= MLX5_GET(create_flow_group_in
, create_fg_in
,
502 end_flow_index
) - fg
->start_index
+ 1;
506 static struct mlx5_flow_table
*alloc_flow_table(int level
, u16 vport
, int max_fte
,
507 enum fs_flow_table_type table_type
,
508 enum fs_flow_table_op_mod op_mod
,
511 struct mlx5_flow_table
*ft
;
513 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
518 ft
->node
.type
= FS_TYPE_FLOW_TABLE
;
520 ft
->type
= table_type
;
522 ft
->max_fte
= max_fte
;
524 INIT_LIST_HEAD(&ft
->fwd_rules
);
525 mutex_init(&ft
->lock
);
530 /* If reverse is false, then we search for the first flow table in the
531 * root sub-tree from start(closest from right), else we search for the
532 * last flow table in the root sub-tree till start(closest from left).
534 static struct mlx5_flow_table
*find_closest_ft_recursive(struct fs_node
*root
,
535 struct list_head
*start
,
538 #define list_advance_entry(pos, reverse) \
539 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
541 #define list_for_each_advance_continue(pos, head, reverse) \
542 for (pos = list_advance_entry(pos, reverse); \
543 &pos->list != (head); \
544 pos = list_advance_entry(pos, reverse))
546 struct fs_node
*iter
= list_entry(start
, struct fs_node
, list
);
547 struct mlx5_flow_table
*ft
= NULL
;
552 list_for_each_advance_continue(iter
, &root
->children
, reverse
) {
553 if (iter
->type
== FS_TYPE_FLOW_TABLE
) {
554 fs_get_obj(ft
, iter
);
557 ft
= find_closest_ft_recursive(iter
, &iter
->children
, reverse
);
565 /* If reverse if false then return the first flow table in next priority of
566 * prio in the tree, else return the last flow table in the previous priority
567 * of prio in the tree.
569 static struct mlx5_flow_table
*find_closest_ft(struct fs_prio
*prio
, bool reverse
)
571 struct mlx5_flow_table
*ft
= NULL
;
572 struct fs_node
*curr_node
;
573 struct fs_node
*parent
;
575 parent
= prio
->node
.parent
;
576 curr_node
= &prio
->node
;
577 while (!ft
&& parent
) {
578 ft
= find_closest_ft_recursive(parent
, &curr_node
->list
, reverse
);
580 parent
= curr_node
->parent
;
585 /* Assuming all the tree is locked by mutex chain lock */
586 static struct mlx5_flow_table
*find_next_chained_ft(struct fs_prio
*prio
)
588 return find_closest_ft(prio
, false);
591 /* Assuming all the tree is locked by mutex chain lock */
592 static struct mlx5_flow_table
*find_prev_chained_ft(struct fs_prio
*prio
)
594 return find_closest_ft(prio
, true);
597 static int connect_fts_in_prio(struct mlx5_core_dev
*dev
,
598 struct fs_prio
*prio
,
599 struct mlx5_flow_table
*ft
)
601 struct mlx5_flow_table
*iter
;
605 fs_for_each_ft(iter
, prio
) {
607 err
= mlx5_cmd_modify_flow_table(dev
,
611 mlx5_core_warn(dev
, "Failed to modify flow table %d\n",
613 /* The driver is out of sync with the FW */
622 /* Connect flow tables from previous priority of prio to ft */
623 static int connect_prev_fts(struct mlx5_core_dev
*dev
,
624 struct mlx5_flow_table
*ft
,
625 struct fs_prio
*prio
)
627 struct mlx5_flow_table
*prev_ft
;
629 prev_ft
= find_prev_chained_ft(prio
);
631 struct fs_prio
*prev_prio
;
633 fs_get_obj(prev_prio
, prev_ft
->node
.parent
);
634 return connect_fts_in_prio(dev
, prev_prio
, ft
);
639 static int update_root_ft_create(struct mlx5_flow_table
*ft
, struct fs_prio
642 struct mlx5_flow_root_namespace
*root
= find_root(&prio
->node
);
643 int min_level
= INT_MAX
;
647 min_level
= root
->root_ft
->level
;
649 if (ft
->level
>= min_level
)
652 err
= mlx5_cmd_update_root_ft(root
->dev
, ft
);
654 mlx5_core_warn(root
->dev
, "Update root flow table of id=%u failed\n",
662 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule
*rule
,
663 struct mlx5_flow_destination
*dest
)
665 struct mlx5_flow_table
*ft
;
666 struct mlx5_flow_group
*fg
;
668 int modify_mask
= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST
);
671 fs_get_obj(fte
, rule
->node
.parent
);
672 if (!(fte
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
))
674 lock_ref_node(&fte
->node
);
675 fs_get_obj(fg
, fte
->node
.parent
);
676 fs_get_obj(ft
, fg
->node
.parent
);
678 memcpy(&rule
->dest_attr
, dest
, sizeof(*dest
));
679 err
= mlx5_cmd_update_fte(get_dev(&ft
->node
),
683 unlock_ref_node(&fte
->node
);
688 int mlx5_modify_rule_destination(struct mlx5_flow_handle
*handle
,
689 struct mlx5_flow_destination
*new_dest
,
690 struct mlx5_flow_destination
*old_dest
)
695 if (handle
->num_rules
!= 1)
697 return _mlx5_modify_rule_destination(handle
->rule
[0],
701 for (i
= 0; i
< handle
->num_rules
; i
++) {
702 if (mlx5_flow_dests_cmp(new_dest
, &handle
->rule
[i
]->dest_attr
))
703 return _mlx5_modify_rule_destination(handle
->rule
[i
],
710 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
711 static int connect_fwd_rules(struct mlx5_core_dev
*dev
,
712 struct mlx5_flow_table
*new_next_ft
,
713 struct mlx5_flow_table
*old_next_ft
)
715 struct mlx5_flow_destination dest
;
716 struct mlx5_flow_rule
*iter
;
719 /* new_next_ft and old_next_ft could be NULL only
720 * when we create/destroy the anchor flow table.
722 if (!new_next_ft
|| !old_next_ft
)
725 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
726 dest
.ft
= new_next_ft
;
728 mutex_lock(&old_next_ft
->lock
);
729 list_splice_init(&old_next_ft
->fwd_rules
, &new_next_ft
->fwd_rules
);
730 mutex_unlock(&old_next_ft
->lock
);
731 list_for_each_entry(iter
, &new_next_ft
->fwd_rules
, next_ft
) {
732 err
= _mlx5_modify_rule_destination(iter
, &dest
);
734 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
740 static int connect_flow_table(struct mlx5_core_dev
*dev
, struct mlx5_flow_table
*ft
,
741 struct fs_prio
*prio
)
743 struct mlx5_flow_table
*next_ft
;
746 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
748 if (list_empty(&prio
->node
.children
)) {
749 err
= connect_prev_fts(dev
, ft
, prio
);
753 next_ft
= find_next_chained_ft(prio
);
754 err
= connect_fwd_rules(dev
, ft
, next_ft
);
759 if (MLX5_CAP_FLOWTABLE(dev
,
760 flow_table_properties_nic_receive
.modify_root
))
761 err
= update_root_ft_create(ft
, prio
);
765 static void list_add_flow_table(struct mlx5_flow_table
*ft
,
766 struct fs_prio
*prio
)
768 struct list_head
*prev
= &prio
->node
.children
;
769 struct mlx5_flow_table
*iter
;
771 fs_for_each_ft(iter
, prio
) {
772 if (iter
->level
> ft
->level
)
774 prev
= &iter
->node
.list
;
776 list_add(&ft
->node
.list
, prev
);
779 static struct mlx5_flow_table
*__mlx5_create_flow_table(struct mlx5_flow_namespace
*ns
,
780 enum fs_flow_table_op_mod op_mod
,
782 int max_fte
, u32 level
,
785 struct mlx5_flow_table
*next_ft
= NULL
;
786 struct mlx5_flow_table
*ft
;
789 struct mlx5_flow_root_namespace
*root
=
790 find_root(&ns
->node
);
791 struct fs_prio
*fs_prio
= NULL
;
794 pr_err("mlx5: flow steering failed to find root of namespace\n");
795 return ERR_PTR(-ENODEV
);
798 mutex_lock(&root
->chain_lock
);
799 fs_prio
= find_prio(ns
, prio
);
804 if (level
>= fs_prio
->num_levels
) {
808 /* The level is related to the
809 * priority level range.
811 level
+= fs_prio
->start_level
;
812 ft
= alloc_flow_table(level
,
814 max_fte
? roundup_pow_of_two(max_fte
) : 0,
822 tree_init_node(&ft
->node
, 1, del_flow_table
);
823 log_table_sz
= ft
->max_fte
? ilog2(ft
->max_fte
) : 0;
824 next_ft
= find_next_chained_ft(fs_prio
);
825 err
= mlx5_cmd_create_flow_table(root
->dev
, ft
->vport
, ft
->op_mod
, ft
->type
,
826 ft
->level
, log_table_sz
, next_ft
, &ft
->id
,
831 err
= connect_flow_table(root
->dev
, ft
, fs_prio
);
834 lock_ref_node(&fs_prio
->node
);
835 tree_add_node(&ft
->node
, &fs_prio
->node
);
836 list_add_flow_table(ft
, fs_prio
);
838 unlock_ref_node(&fs_prio
->node
);
839 mutex_unlock(&root
->chain_lock
);
842 mlx5_cmd_destroy_flow_table(root
->dev
, ft
);
846 mutex_unlock(&root
->chain_lock
);
850 struct mlx5_flow_table
*mlx5_create_flow_table(struct mlx5_flow_namespace
*ns
,
851 int prio
, int max_fte
,
855 return __mlx5_create_flow_table(ns
, FS_FT_OP_MOD_NORMAL
, 0, prio
,
856 max_fte
, level
, flags
);
859 struct mlx5_flow_table
*mlx5_create_vport_flow_table(struct mlx5_flow_namespace
*ns
,
860 int prio
, int max_fte
,
861 u32 level
, u16 vport
)
863 return __mlx5_create_flow_table(ns
, FS_FT_OP_MOD_NORMAL
, vport
, prio
,
867 struct mlx5_flow_table
*mlx5_create_lag_demux_flow_table(
868 struct mlx5_flow_namespace
*ns
,
871 return __mlx5_create_flow_table(ns
, FS_FT_OP_MOD_LAG_DEMUX
, 0, prio
, 0,
874 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table
);
876 struct mlx5_flow_table
*mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace
*ns
,
878 int num_flow_table_entries
,
883 struct mlx5_flow_table
*ft
;
885 if (max_num_groups
> num_flow_table_entries
)
886 return ERR_PTR(-EINVAL
);
888 ft
= mlx5_create_flow_table(ns
, prio
, num_flow_table_entries
, level
, flags
);
892 ft
->autogroup
.active
= true;
893 ft
->autogroup
.required_groups
= max_num_groups
;
897 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table
);
899 /* Flow table should be locked */
900 static struct mlx5_flow_group
*create_flow_group_common(struct mlx5_flow_table
*ft
,
906 struct mlx5_flow_group
*fg
;
907 struct mlx5_core_dev
*dev
= get_dev(&ft
->node
);
911 return ERR_PTR(-ENODEV
);
913 fg
= alloc_flow_group(fg_in
);
917 err
= mlx5_cmd_create_flow_group(dev
, ft
, fg_in
, &fg
->id
);
923 if (ft
->autogroup
.active
)
924 ft
->autogroup
.num_groups
++;
925 /* Add node to tree */
926 tree_init_node(&fg
->node
, !is_auto_fg
, del_flow_group
);
927 tree_add_node(&fg
->node
, &ft
->node
);
928 /* Add node to group list */
929 list_add(&fg
->node
.list
, prev_fg
);
934 struct mlx5_flow_group
*mlx5_create_flow_group(struct mlx5_flow_table
*ft
,
937 struct mlx5_flow_group
*fg
;
939 if (ft
->autogroup
.active
)
940 return ERR_PTR(-EPERM
);
942 lock_ref_node(&ft
->node
);
943 fg
= create_flow_group_common(ft
, fg_in
, ft
->node
.children
.prev
, false);
944 unlock_ref_node(&ft
->node
);
949 static struct mlx5_flow_rule
*alloc_rule(struct mlx5_flow_destination
*dest
)
951 struct mlx5_flow_rule
*rule
;
953 rule
= kzalloc(sizeof(*rule
), GFP_KERNEL
);
957 INIT_LIST_HEAD(&rule
->next_ft
);
958 rule
->node
.type
= FS_TYPE_FLOW_DEST
;
960 memcpy(&rule
->dest_attr
, dest
, sizeof(*dest
));
965 static struct mlx5_flow_handle
*alloc_handle(int num_rules
)
967 struct mlx5_flow_handle
*handle
;
969 handle
= kzalloc(sizeof(*handle
) + sizeof(handle
->rule
[0]) *
970 num_rules
, GFP_KERNEL
);
974 handle
->num_rules
= num_rules
;
979 static void destroy_flow_handle(struct fs_fte
*fte
,
980 struct mlx5_flow_handle
*handle
,
981 struct mlx5_flow_destination
*dest
,
985 if (atomic_dec_and_test(&handle
->rule
[i
]->node
.refcount
)) {
987 list_del(&handle
->rule
[i
]->node
.list
);
988 kfree(handle
->rule
[i
]);
994 static struct mlx5_flow_handle
*
995 create_flow_handle(struct fs_fte
*fte
,
996 struct mlx5_flow_destination
*dest
,
1001 struct mlx5_flow_handle
*handle
;
1002 struct mlx5_flow_rule
*rule
= NULL
;
1003 static int count
= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS
);
1004 static int dst
= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST
);
1008 handle
= alloc_handle((dest_num
) ? dest_num
: 1);
1010 return ERR_PTR(-ENOMEM
);
1014 rule
= find_flow_rule(fte
, dest
+ i
);
1016 atomic_inc(&rule
->node
.refcount
);
1022 rule
= alloc_rule(dest
+ i
);
1026 /* Add dest to dests list- we need flow tables to be in the
1027 * end of the list for forward to next prio rules.
1029 tree_init_node(&rule
->node
, 1, del_rule
);
1031 dest
[i
].type
!= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
)
1032 list_add(&rule
->node
.list
, &fte
->node
.children
);
1034 list_add_tail(&rule
->node
.list
, &fte
->node
.children
);
1038 type
= dest
[i
].type
==
1039 MLX5_FLOW_DESTINATION_TYPE_COUNTER
;
1040 *modify_mask
|= type
? count
: dst
;
1043 handle
->rule
[i
] = rule
;
1044 } while (++i
< dest_num
);
1049 destroy_flow_handle(fte
, handle
, dest
, i
);
1050 return ERR_PTR(-ENOMEM
);
1053 /* fte should not be deleted while calling this function */
1054 static struct mlx5_flow_handle
*
1055 add_rule_fte(struct fs_fte
*fte
,
1056 struct mlx5_flow_group
*fg
,
1057 struct mlx5_flow_destination
*dest
,
1061 struct mlx5_flow_handle
*handle
;
1062 struct mlx5_flow_table
*ft
;
1063 int modify_mask
= 0;
1065 bool new_rule
= false;
1067 handle
= create_flow_handle(fte
, dest
, dest_num
, &modify_mask
,
1069 if (IS_ERR(handle
) || !new_rule
)
1073 modify_mask
|= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION
);
1075 fs_get_obj(ft
, fg
->node
.parent
);
1076 if (!(fte
->status
& FS_FTE_STATUS_EXISTING
))
1077 err
= mlx5_cmd_create_fte(get_dev(&ft
->node
),
1080 err
= mlx5_cmd_update_fte(get_dev(&ft
->node
),
1081 ft
, fg
->id
, modify_mask
, fte
);
1085 fte
->status
|= FS_FTE_STATUS_EXISTING
;
1091 destroy_flow_handle(fte
, handle
, dest
, handle
->num_rules
);
1092 return ERR_PTR(err
);
1095 /* Assumed fg is locked */
1096 static unsigned int get_free_fte_index(struct mlx5_flow_group
*fg
,
1097 struct list_head
**prev
)
1100 unsigned int start
= fg
->start_index
;
1103 *prev
= &fg
->node
.children
;
1105 /* assumed list is sorted by index */
1106 fs_for_each_fte(fte
, fg
) {
1107 if (fte
->index
!= start
)
1111 *prev
= &fte
->node
.list
;
1117 /* prev is output, prev->next = new_fte */
1118 static struct fs_fte
*create_fte(struct mlx5_flow_group
*fg
,
1120 struct mlx5_flow_act
*flow_act
,
1121 struct list_head
**prev
)
1126 index
= get_free_fte_index(fg
, prev
);
1127 fte
= alloc_fte(flow_act
, match_value
, index
);
1134 static struct mlx5_flow_group
*create_autogroup(struct mlx5_flow_table
*ft
,
1135 u8 match_criteria_enable
,
1136 u32
*match_criteria
)
1138 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
1139 struct list_head
*prev
= ft
->node
.children
.prev
;
1140 unsigned int candidate_index
= 0;
1141 struct mlx5_flow_group
*fg
;
1142 void *match_criteria_addr
;
1143 unsigned int group_size
= 0;
1146 if (!ft
->autogroup
.active
)
1147 return ERR_PTR(-ENOENT
);
1149 in
= mlx5_vzalloc(inlen
);
1151 return ERR_PTR(-ENOMEM
);
1153 if (ft
->autogroup
.num_groups
< ft
->autogroup
.required_groups
)
1154 /* We save place for flow groups in addition to max types */
1155 group_size
= ft
->max_fte
/ (ft
->autogroup
.required_groups
+ 1);
1157 /* ft->max_fte == ft->autogroup.max_types */
1158 if (group_size
== 0)
1161 /* sorted by start_index */
1162 fs_for_each_fg(fg
, ft
) {
1163 if (candidate_index
+ group_size
> fg
->start_index
)
1164 candidate_index
= fg
->start_index
+ fg
->max_ftes
;
1167 prev
= &fg
->node
.list
;
1170 if (candidate_index
+ group_size
> ft
->max_fte
) {
1171 fg
= ERR_PTR(-ENOSPC
);
1175 MLX5_SET(create_flow_group_in
, in
, match_criteria_enable
,
1176 match_criteria_enable
);
1177 MLX5_SET(create_flow_group_in
, in
, start_flow_index
, candidate_index
);
1178 MLX5_SET(create_flow_group_in
, in
, end_flow_index
, candidate_index
+
1180 match_criteria_addr
= MLX5_ADDR_OF(create_flow_group_in
,
1181 in
, match_criteria
);
1182 memcpy(match_criteria_addr
, match_criteria
,
1183 MLX5_ST_SZ_BYTES(fte_match_param
));
1185 fg
= create_flow_group_common(ft
, in
, prev
, true);
1191 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination
*d1
,
1192 struct mlx5_flow_destination
*d2
)
1194 if (d1
->type
== d2
->type
) {
1195 if ((d1
->type
== MLX5_FLOW_DESTINATION_TYPE_VPORT
&&
1196 d1
->vport_num
== d2
->vport_num
) ||
1197 (d1
->type
== MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
&&
1198 d1
->ft
== d2
->ft
) ||
1199 (d1
->type
== MLX5_FLOW_DESTINATION_TYPE_TIR
&&
1200 d1
->tir_num
== d2
->tir_num
))
1207 static struct mlx5_flow_rule
*find_flow_rule(struct fs_fte
*fte
,
1208 struct mlx5_flow_destination
*dest
)
1210 struct mlx5_flow_rule
*rule
;
1212 list_for_each_entry(rule
, &fte
->node
.children
, node
.list
) {
1213 if (mlx5_flow_dests_cmp(&rule
->dest_attr
, dest
))
1219 static struct mlx5_flow_handle
*add_rule_fg(struct mlx5_flow_group
*fg
,
1221 struct mlx5_flow_act
*flow_act
,
1222 struct mlx5_flow_destination
*dest
,
1225 struct mlx5_flow_handle
*handle
;
1226 struct mlx5_flow_table
*ft
;
1227 struct list_head
*prev
;
1231 nested_lock_ref_node(&fg
->node
, FS_MUTEX_PARENT
);
1232 fs_for_each_fte(fte
, fg
) {
1233 nested_lock_ref_node(&fte
->node
, FS_MUTEX_CHILD
);
1234 if (compare_match_value(&fg
->mask
, match_value
, &fte
->val
) &&
1235 (flow_act
->action
& fte
->action
) &&
1236 flow_act
->flow_tag
== fte
->flow_tag
) {
1237 int old_action
= fte
->action
;
1239 fte
->action
|= flow_act
->action
;
1240 handle
= add_rule_fte(fte
, fg
, dest
, dest_num
,
1241 old_action
!= flow_act
->action
);
1242 if (IS_ERR(handle
)) {
1243 fte
->action
= old_action
;
1249 unlock_ref_node(&fte
->node
);
1251 fs_get_obj(ft
, fg
->node
.parent
);
1252 if (fg
->num_ftes
>= fg
->max_ftes
) {
1253 handle
= ERR_PTR(-ENOSPC
);
1257 fte
= create_fte(fg
, match_value
, flow_act
, &prev
);
1259 handle
= (void *)fte
;
1262 tree_init_node(&fte
->node
, 0, del_fte
);
1263 nested_lock_ref_node(&fte
->node
, FS_MUTEX_CHILD
);
1264 handle
= add_rule_fte(fte
, fg
, dest
, dest_num
, false);
1265 if (IS_ERR(handle
)) {
1266 unlock_ref_node(&fte
->node
);
1273 tree_add_node(&fte
->node
, &fg
->node
);
1274 list_add(&fte
->node
.list
, prev
);
1276 for (i
= 0; i
< handle
->num_rules
; i
++) {
1277 if (atomic_read(&handle
->rule
[i
]->node
.refcount
) == 1)
1278 tree_add_node(&handle
->rule
[i
]->node
, &fte
->node
);
1281 unlock_ref_node(&fte
->node
);
1283 unlock_ref_node(&fg
->node
);
1287 struct mlx5_fc
*mlx5_flow_rule_counter(struct mlx5_flow_handle
*handle
)
1289 struct mlx5_flow_rule
*dst
;
1292 fs_get_obj(fte
, handle
->rule
[0]->node
.parent
);
1294 fs_for_each_dst(dst
, fte
) {
1295 if (dst
->dest_attr
.type
== MLX5_FLOW_DESTINATION_TYPE_COUNTER
)
1296 return dst
->dest_attr
.counter
;
1302 static bool counter_is_valid(struct mlx5_fc
*counter
, u32 action
)
1304 if (!(action
& MLX5_FLOW_CONTEXT_ACTION_COUNT
))
1310 return (action
& (MLX5_FLOW_CONTEXT_ACTION_DROP
|
1311 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
));
1314 static bool dest_is_valid(struct mlx5_flow_destination
*dest
,
1316 struct mlx5_flow_table
*ft
)
1318 if (dest
&& (dest
->type
== MLX5_FLOW_DESTINATION_TYPE_COUNTER
))
1319 return counter_is_valid(dest
->counter
, action
);
1321 if (!(action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
))
1324 if (!dest
|| ((dest
->type
==
1325 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
) &&
1326 (dest
->ft
->level
<= ft
->level
)))
1331 static struct mlx5_flow_handle
*
1332 _mlx5_add_flow_rules(struct mlx5_flow_table
*ft
,
1333 struct mlx5_flow_spec
*spec
,
1334 struct mlx5_flow_act
*flow_act
,
1335 struct mlx5_flow_destination
*dest
,
1339 struct mlx5_flow_group
*g
;
1340 struct mlx5_flow_handle
*rule
;
1343 for (i
= 0; i
< dest_num
; i
++) {
1344 if (!dest_is_valid(&dest
[i
], flow_act
->action
, ft
))
1345 return ERR_PTR(-EINVAL
);
1348 nested_lock_ref_node(&ft
->node
, FS_MUTEX_GRANDPARENT
);
1349 fs_for_each_fg(g
, ft
)
1350 if (compare_match_criteria(g
->mask
.match_criteria_enable
,
1351 spec
->match_criteria_enable
,
1352 g
->mask
.match_criteria
,
1353 spec
->match_criteria
)) {
1354 rule
= add_rule_fg(g
, spec
->match_value
,
1355 flow_act
, dest
, dest_num
);
1356 if (!IS_ERR(rule
) || PTR_ERR(rule
) != -ENOSPC
)
1360 g
= create_autogroup(ft
, spec
->match_criteria_enable
,
1361 spec
->match_criteria
);
1367 rule
= add_rule_fg(g
, spec
->match_value
, flow_act
, dest
, dest_num
);
1369 /* Remove assumes refcount > 0 and autogroup creates a group
1370 * with a refcount = 0.
1372 unlock_ref_node(&ft
->node
);
1373 tree_get_node(&g
->node
);
1374 tree_remove_node(&g
->node
);
1378 unlock_ref_node(&ft
->node
);
1382 static bool fwd_next_prio_supported(struct mlx5_flow_table
*ft
)
1384 return ((ft
->type
== FS_FT_NIC_RX
) &&
1385 (MLX5_CAP_FLOWTABLE(get_dev(&ft
->node
), nic_rx_multi_path_tirs
)));
1388 struct mlx5_flow_handle
*
1389 mlx5_add_flow_rules(struct mlx5_flow_table
*ft
,
1390 struct mlx5_flow_spec
*spec
,
1391 struct mlx5_flow_act
*flow_act
,
1392 struct mlx5_flow_destination
*dest
,
1395 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1396 struct mlx5_flow_destination gen_dest
;
1397 struct mlx5_flow_table
*next_ft
= NULL
;
1398 struct mlx5_flow_handle
*handle
= NULL
;
1399 u32 sw_action
= flow_act
->action
;
1400 struct fs_prio
*prio
;
1402 fs_get_obj(prio
, ft
->node
.parent
);
1403 if (flow_act
->action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
1404 if (!fwd_next_prio_supported(ft
))
1405 return ERR_PTR(-EOPNOTSUPP
);
1407 return ERR_PTR(-EINVAL
);
1408 mutex_lock(&root
->chain_lock
);
1409 next_ft
= find_next_chained_ft(prio
);
1411 gen_dest
.type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
1412 gen_dest
.ft
= next_ft
;
1415 flow_act
->action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
1417 mutex_unlock(&root
->chain_lock
);
1418 return ERR_PTR(-EOPNOTSUPP
);
1422 handle
= _mlx5_add_flow_rules(ft
, spec
, flow_act
, dest
, dest_num
);
1424 if (sw_action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
1425 if (!IS_ERR_OR_NULL(handle
) &&
1426 (list_empty(&handle
->rule
[0]->next_ft
))) {
1427 mutex_lock(&next_ft
->lock
);
1428 list_add(&handle
->rule
[0]->next_ft
,
1429 &next_ft
->fwd_rules
);
1430 mutex_unlock(&next_ft
->lock
);
1431 handle
->rule
[0]->sw_action
= MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
;
1433 mutex_unlock(&root
->chain_lock
);
1437 EXPORT_SYMBOL(mlx5_add_flow_rules
);
1439 void mlx5_del_flow_rules(struct mlx5_flow_handle
*handle
)
1443 for (i
= handle
->num_rules
- 1; i
>= 0; i
--)
1444 tree_remove_node(&handle
->rule
[i
]->node
);
1447 EXPORT_SYMBOL(mlx5_del_flow_rules
);
1449 /* Assuming prio->node.children(flow tables) is sorted by level */
1450 static struct mlx5_flow_table
*find_next_ft(struct mlx5_flow_table
*ft
)
1452 struct fs_prio
*prio
;
1454 fs_get_obj(prio
, ft
->node
.parent
);
1456 if (!list_is_last(&ft
->node
.list
, &prio
->node
.children
))
1457 return list_next_entry(ft
, node
.list
);
1458 return find_next_chained_ft(prio
);
1461 static int update_root_ft_destroy(struct mlx5_flow_table
*ft
)
1463 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1464 struct mlx5_flow_table
*new_root_ft
= NULL
;
1466 if (root
->root_ft
!= ft
)
1469 new_root_ft
= find_next_ft(ft
);
1471 int err
= mlx5_cmd_update_root_ft(root
->dev
, new_root_ft
);
1474 mlx5_core_warn(root
->dev
, "Update root flow table of id=%u failed\n",
1479 root
->root_ft
= new_root_ft
;
1483 /* Connect flow table from previous priority to
1484 * the next flow table.
1486 static int disconnect_flow_table(struct mlx5_flow_table
*ft
)
1488 struct mlx5_core_dev
*dev
= get_dev(&ft
->node
);
1489 struct mlx5_flow_table
*next_ft
;
1490 struct fs_prio
*prio
;
1493 err
= update_root_ft_destroy(ft
);
1497 fs_get_obj(prio
, ft
->node
.parent
);
1498 if (!(list_first_entry(&prio
->node
.children
,
1499 struct mlx5_flow_table
,
1503 next_ft
= find_next_chained_ft(prio
);
1504 err
= connect_fwd_rules(dev
, next_ft
, ft
);
1508 err
= connect_prev_fts(dev
, next_ft
, prio
);
1510 mlx5_core_warn(dev
, "Failed to disconnect flow table %d\n",
1515 int mlx5_destroy_flow_table(struct mlx5_flow_table
*ft
)
1517 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1520 mutex_lock(&root
->chain_lock
);
1521 err
= disconnect_flow_table(ft
);
1523 mutex_unlock(&root
->chain_lock
);
1526 if (tree_remove_node(&ft
->node
))
1527 mlx5_core_warn(get_dev(&ft
->node
), "Flow table %d wasn't destroyed, refcount > 1\n",
1529 mutex_unlock(&root
->chain_lock
);
1533 EXPORT_SYMBOL(mlx5_destroy_flow_table
);
1535 void mlx5_destroy_flow_group(struct mlx5_flow_group
*fg
)
1537 if (tree_remove_node(&fg
->node
))
1538 mlx5_core_warn(get_dev(&fg
->node
), "Flow group %d wasn't destroyed, refcount > 1\n",
1542 struct mlx5_flow_namespace
*mlx5_get_flow_namespace(struct mlx5_core_dev
*dev
,
1543 enum mlx5_flow_namespace_type type
)
1545 struct mlx5_flow_steering
*steering
= dev
->priv
.steering
;
1546 struct mlx5_flow_root_namespace
*root_ns
;
1548 struct fs_prio
*fs_prio
;
1549 struct mlx5_flow_namespace
*ns
;
1555 case MLX5_FLOW_NAMESPACE_BYPASS
:
1556 case MLX5_FLOW_NAMESPACE_LAG
:
1557 case MLX5_FLOW_NAMESPACE_OFFLOADS
:
1558 case MLX5_FLOW_NAMESPACE_ETHTOOL
:
1559 case MLX5_FLOW_NAMESPACE_KERNEL
:
1560 case MLX5_FLOW_NAMESPACE_LEFTOVERS
:
1561 case MLX5_FLOW_NAMESPACE_ANCHOR
:
1564 case MLX5_FLOW_NAMESPACE_FDB
:
1565 if (steering
->fdb_root_ns
)
1566 return &steering
->fdb_root_ns
->ns
;
1569 case MLX5_FLOW_NAMESPACE_ESW_EGRESS
:
1570 if (steering
->esw_egress_root_ns
)
1571 return &steering
->esw_egress_root_ns
->ns
;
1574 case MLX5_FLOW_NAMESPACE_ESW_INGRESS
:
1575 if (steering
->esw_ingress_root_ns
)
1576 return &steering
->esw_ingress_root_ns
->ns
;
1579 case MLX5_FLOW_NAMESPACE_SNIFFER_RX
:
1580 if (steering
->sniffer_rx_root_ns
)
1581 return &steering
->sniffer_rx_root_ns
->ns
;
1584 case MLX5_FLOW_NAMESPACE_SNIFFER_TX
:
1585 if (steering
->sniffer_tx_root_ns
)
1586 return &steering
->sniffer_tx_root_ns
->ns
;
1593 root_ns
= steering
->root_ns
;
1597 fs_prio
= find_prio(&root_ns
->ns
, prio
);
1601 ns
= list_first_entry(&fs_prio
->node
.children
,
1607 EXPORT_SYMBOL(mlx5_get_flow_namespace
);
1609 static struct fs_prio
*fs_create_prio(struct mlx5_flow_namespace
*ns
,
1610 unsigned int prio
, int num_levels
)
1612 struct fs_prio
*fs_prio
;
1614 fs_prio
= kzalloc(sizeof(*fs_prio
), GFP_KERNEL
);
1616 return ERR_PTR(-ENOMEM
);
1618 fs_prio
->node
.type
= FS_TYPE_PRIO
;
1619 tree_init_node(&fs_prio
->node
, 1, NULL
);
1620 tree_add_node(&fs_prio
->node
, &ns
->node
);
1621 fs_prio
->num_levels
= num_levels
;
1622 fs_prio
->prio
= prio
;
1623 list_add_tail(&fs_prio
->node
.list
, &ns
->node
.children
);
1628 static struct mlx5_flow_namespace
*fs_init_namespace(struct mlx5_flow_namespace
1631 ns
->node
.type
= FS_TYPE_NAMESPACE
;
1636 static struct mlx5_flow_namespace
*fs_create_namespace(struct fs_prio
*prio
)
1638 struct mlx5_flow_namespace
*ns
;
1640 ns
= kzalloc(sizeof(*ns
), GFP_KERNEL
);
1642 return ERR_PTR(-ENOMEM
);
1644 fs_init_namespace(ns
);
1645 tree_init_node(&ns
->node
, 1, NULL
);
1646 tree_add_node(&ns
->node
, &prio
->node
);
1647 list_add_tail(&ns
->node
.list
, &prio
->node
.children
);
1652 static int create_leaf_prios(struct mlx5_flow_namespace
*ns
, int prio
,
1653 struct init_tree_node
*prio_metadata
)
1655 struct fs_prio
*fs_prio
;
1658 for (i
= 0; i
< prio_metadata
->num_leaf_prios
; i
++) {
1659 fs_prio
= fs_create_prio(ns
, prio
++, prio_metadata
->num_levels
);
1660 if (IS_ERR(fs_prio
))
1661 return PTR_ERR(fs_prio
);
1666 #define FLOW_TABLE_BIT_SZ 1
1667 #define GET_FLOW_TABLE_CAP(dev, offset) \
1668 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1670 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1671 static bool has_required_caps(struct mlx5_core_dev
*dev
, struct node_caps
*caps
)
1675 for (i
= 0; i
< caps
->arr_sz
; i
++) {
1676 if (!GET_FLOW_TABLE_CAP(dev
, caps
->caps
[i
]))
1682 static int init_root_tree_recursive(struct mlx5_flow_steering
*steering
,
1683 struct init_tree_node
*init_node
,
1684 struct fs_node
*fs_parent_node
,
1685 struct init_tree_node
*init_parent_node
,
1688 int max_ft_level
= MLX5_CAP_FLOWTABLE(steering
->dev
,
1689 flow_table_properties_nic_receive
.
1691 struct mlx5_flow_namespace
*fs_ns
;
1692 struct fs_prio
*fs_prio
;
1693 struct fs_node
*base
;
1697 if (init_node
->type
== FS_TYPE_PRIO
) {
1698 if ((init_node
->min_ft_level
> max_ft_level
) ||
1699 !has_required_caps(steering
->dev
, &init_node
->caps
))
1702 fs_get_obj(fs_ns
, fs_parent_node
);
1703 if (init_node
->num_leaf_prios
)
1704 return create_leaf_prios(fs_ns
, prio
, init_node
);
1705 fs_prio
= fs_create_prio(fs_ns
, prio
, init_node
->num_levels
);
1706 if (IS_ERR(fs_prio
))
1707 return PTR_ERR(fs_prio
);
1708 base
= &fs_prio
->node
;
1709 } else if (init_node
->type
== FS_TYPE_NAMESPACE
) {
1710 fs_get_obj(fs_prio
, fs_parent_node
);
1711 fs_ns
= fs_create_namespace(fs_prio
);
1713 return PTR_ERR(fs_ns
);
1714 base
= &fs_ns
->node
;
1719 for (i
= 0; i
< init_node
->ar_size
; i
++) {
1720 err
= init_root_tree_recursive(steering
, &init_node
->children
[i
],
1721 base
, init_node
, prio
);
1724 if (init_node
->children
[i
].type
== FS_TYPE_PRIO
&&
1725 init_node
->children
[i
].num_leaf_prios
) {
1726 prio
+= init_node
->children
[i
].num_leaf_prios
;
1733 static int init_root_tree(struct mlx5_flow_steering
*steering
,
1734 struct init_tree_node
*init_node
,
1735 struct fs_node
*fs_parent_node
)
1738 struct mlx5_flow_namespace
*fs_ns
;
1741 fs_get_obj(fs_ns
, fs_parent_node
);
1742 for (i
= 0; i
< init_node
->ar_size
; i
++) {
1743 err
= init_root_tree_recursive(steering
, &init_node
->children
[i
],
1752 static struct mlx5_flow_root_namespace
*create_root_ns(struct mlx5_flow_steering
*steering
,
1753 enum fs_flow_table_type
1756 struct mlx5_flow_root_namespace
*root_ns
;
1757 struct mlx5_flow_namespace
*ns
;
1759 /* Create the root namespace */
1760 root_ns
= mlx5_vzalloc(sizeof(*root_ns
));
1764 root_ns
->dev
= steering
->dev
;
1765 root_ns
->table_type
= table_type
;
1768 fs_init_namespace(ns
);
1769 mutex_init(&root_ns
->chain_lock
);
1770 tree_init_node(&ns
->node
, 1, NULL
);
1771 tree_add_node(&ns
->node
, NULL
);
1776 static void set_prio_attrs_in_prio(struct fs_prio
*prio
, int acc_level
);
1778 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace
*ns
, int acc_level
)
1780 struct fs_prio
*prio
;
1782 fs_for_each_prio(prio
, ns
) {
1783 /* This updates prio start_level and num_levels */
1784 set_prio_attrs_in_prio(prio
, acc_level
);
1785 acc_level
+= prio
->num_levels
;
1790 static void set_prio_attrs_in_prio(struct fs_prio
*prio
, int acc_level
)
1792 struct mlx5_flow_namespace
*ns
;
1793 int acc_level_ns
= acc_level
;
1795 prio
->start_level
= acc_level
;
1796 fs_for_each_ns(ns
, prio
)
1797 /* This updates start_level and num_levels of ns's priority descendants */
1798 acc_level_ns
= set_prio_attrs_in_ns(ns
, acc_level
);
1799 if (!prio
->num_levels
)
1800 prio
->num_levels
= acc_level_ns
- prio
->start_level
;
1801 WARN_ON(prio
->num_levels
< acc_level_ns
- prio
->start_level
);
1804 static void set_prio_attrs(struct mlx5_flow_root_namespace
*root_ns
)
1806 struct mlx5_flow_namespace
*ns
= &root_ns
->ns
;
1807 struct fs_prio
*prio
;
1808 int start_level
= 0;
1810 fs_for_each_prio(prio
, ns
) {
1811 set_prio_attrs_in_prio(prio
, start_level
);
1812 start_level
+= prio
->num_levels
;
1816 #define ANCHOR_PRIO 0
1817 #define ANCHOR_SIZE 1
1818 #define ANCHOR_LEVEL 0
1819 static int create_anchor_flow_table(struct mlx5_flow_steering
*steering
)
1821 struct mlx5_flow_namespace
*ns
= NULL
;
1822 struct mlx5_flow_table
*ft
;
1824 ns
= mlx5_get_flow_namespace(steering
->dev
, MLX5_FLOW_NAMESPACE_ANCHOR
);
1827 ft
= mlx5_create_flow_table(ns
, ANCHOR_PRIO
, ANCHOR_SIZE
, ANCHOR_LEVEL
, 0);
1829 mlx5_core_err(steering
->dev
, "Failed to create last anchor flow table");
1835 static int init_root_ns(struct mlx5_flow_steering
*steering
)
1838 steering
->root_ns
= create_root_ns(steering
, FS_FT_NIC_RX
);
1839 if (!steering
->root_ns
)
1842 if (init_root_tree(steering
, &root_fs
, &steering
->root_ns
->ns
.node
))
1845 set_prio_attrs(steering
->root_ns
);
1847 if (create_anchor_flow_table(steering
))
1853 mlx5_cleanup_fs(steering
->dev
);
1857 static void clean_tree(struct fs_node
*node
)
1860 struct fs_node
*iter
;
1861 struct fs_node
*temp
;
1863 list_for_each_entry_safe(iter
, temp
, &node
->children
, list
)
1865 tree_remove_node(node
);
1869 static void cleanup_root_ns(struct mlx5_flow_root_namespace
*root_ns
)
1874 clean_tree(&root_ns
->ns
.node
);
1877 void mlx5_cleanup_fs(struct mlx5_core_dev
*dev
)
1879 struct mlx5_flow_steering
*steering
= dev
->priv
.steering
;
1881 if (MLX5_CAP_GEN(dev
, port_type
) != MLX5_CAP_PORT_TYPE_ETH
)
1884 cleanup_root_ns(steering
->root_ns
);
1885 cleanup_root_ns(steering
->esw_egress_root_ns
);
1886 cleanup_root_ns(steering
->esw_ingress_root_ns
);
1887 cleanup_root_ns(steering
->fdb_root_ns
);
1888 cleanup_root_ns(steering
->sniffer_rx_root_ns
);
1889 cleanup_root_ns(steering
->sniffer_tx_root_ns
);
1890 mlx5_cleanup_fc_stats(dev
);
1894 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering
*steering
)
1896 struct fs_prio
*prio
;
1898 steering
->sniffer_tx_root_ns
= create_root_ns(steering
, FS_FT_SNIFFER_TX
);
1899 if (!steering
->sniffer_tx_root_ns
)
1902 /* Create single prio */
1903 prio
= fs_create_prio(&steering
->sniffer_tx_root_ns
->ns
, 0, 1);
1905 cleanup_root_ns(steering
->sniffer_tx_root_ns
);
1906 return PTR_ERR(prio
);
1911 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering
*steering
)
1913 struct fs_prio
*prio
;
1915 steering
->sniffer_rx_root_ns
= create_root_ns(steering
, FS_FT_SNIFFER_RX
);
1916 if (!steering
->sniffer_rx_root_ns
)
1919 /* Create single prio */
1920 prio
= fs_create_prio(&steering
->sniffer_rx_root_ns
->ns
, 0, 1);
1922 cleanup_root_ns(steering
->sniffer_rx_root_ns
);
1923 return PTR_ERR(prio
);
1928 static int init_fdb_root_ns(struct mlx5_flow_steering
*steering
)
1930 struct fs_prio
*prio
;
1932 steering
->fdb_root_ns
= create_root_ns(steering
, FS_FT_FDB
);
1933 if (!steering
->fdb_root_ns
)
1936 prio
= fs_create_prio(&steering
->fdb_root_ns
->ns
, 0, 1);
1940 prio
= fs_create_prio(&steering
->fdb_root_ns
->ns
, 1, 1);
1944 set_prio_attrs(steering
->fdb_root_ns
);
1948 cleanup_root_ns(steering
->fdb_root_ns
);
1949 steering
->fdb_root_ns
= NULL
;
1950 return PTR_ERR(prio
);
1953 static int init_ingress_acl_root_ns(struct mlx5_flow_steering
*steering
)
1955 struct fs_prio
*prio
;
1957 steering
->esw_egress_root_ns
= create_root_ns(steering
, FS_FT_ESW_EGRESS_ACL
);
1958 if (!steering
->esw_egress_root_ns
)
1962 prio
= fs_create_prio(&steering
->esw_egress_root_ns
->ns
, 0,
1963 MLX5_TOTAL_VPORTS(steering
->dev
));
1964 return PTR_ERR_OR_ZERO(prio
);
1967 static int init_egress_acl_root_ns(struct mlx5_flow_steering
*steering
)
1969 struct fs_prio
*prio
;
1971 steering
->esw_ingress_root_ns
= create_root_ns(steering
, FS_FT_ESW_INGRESS_ACL
);
1972 if (!steering
->esw_ingress_root_ns
)
1976 prio
= fs_create_prio(&steering
->esw_ingress_root_ns
->ns
, 0,
1977 MLX5_TOTAL_VPORTS(steering
->dev
));
1978 return PTR_ERR_OR_ZERO(prio
);
1981 int mlx5_init_fs(struct mlx5_core_dev
*dev
)
1983 struct mlx5_flow_steering
*steering
;
1986 if (MLX5_CAP_GEN(dev
, port_type
) != MLX5_CAP_PORT_TYPE_ETH
)
1989 err
= mlx5_init_fc_stats(dev
);
1993 steering
= kzalloc(sizeof(*steering
), GFP_KERNEL
);
1996 steering
->dev
= dev
;
1997 dev
->priv
.steering
= steering
;
1999 if (MLX5_CAP_GEN(dev
, nic_flow_table
) &&
2000 MLX5_CAP_FLOWTABLE_NIC_RX(dev
, ft_support
)) {
2001 err
= init_root_ns(steering
);
2006 if (MLX5_CAP_GEN(dev
, eswitch_flow_table
)) {
2007 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, ft_support
)) {
2008 err
= init_fdb_root_ns(steering
);
2012 if (MLX5_CAP_ESW_EGRESS_ACL(dev
, ft_support
)) {
2013 err
= init_egress_acl_root_ns(steering
);
2017 if (MLX5_CAP_ESW_INGRESS_ACL(dev
, ft_support
)) {
2018 err
= init_ingress_acl_root_ns(steering
);
2024 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev
, ft_support
)) {
2025 err
= init_sniffer_rx_root_ns(steering
);
2030 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev
, ft_support
)) {
2031 err
= init_sniffer_tx_root_ns(steering
);
2038 mlx5_cleanup_fs(dev
);