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>
35 #include <linux/mlx5/vport.h>
36 #include <linux/mlx5/eswitch.h>
38 #include "mlx5_core.h"
41 #include "diag/fs_tracepoint.h"
42 #include "accel/ipsec.h"
43 #include "fpga/ipsec.h"
45 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
46 sizeof(struct init_tree_node))
48 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
49 ...) {.type = FS_TYPE_PRIO,\
50 .min_ft_level = min_level_val,\
51 .num_levels = num_levels_val,\
52 .num_leaf_prios = num_prios_val,\
54 .children = (struct init_tree_node[]) {__VA_ARGS__},\
55 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
58 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
59 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
62 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
63 .def_miss_action = def_miss_act,\
64 .children = (struct init_tree_node[]) {__VA_ARGS__},\
65 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
68 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
71 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
73 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
74 .caps = (long[]) {__VA_ARGS__} }
76 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
77 FS_CAP(flow_table_properties_nic_receive.modify_root), \
78 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
79 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
81 #define FS_CHAINING_CAPS_EGRESS \
83 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
84 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
85 FS_CAP(flow_table_properties_nic_transmit \
86 .identified_miss_table_mode), \
87 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
89 #define FS_CHAINING_CAPS_RDMA_TX \
91 FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
92 FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root), \
93 FS_CAP(flow_table_properties_nic_transmit_rdma \
94 .identified_miss_table_mode), \
95 FS_CAP(flow_table_properties_nic_transmit_rdma \
98 #define LEFTOVERS_NUM_LEVELS 1
99 #define LEFTOVERS_NUM_PRIOS 1
101 #define BY_PASS_PRIO_NUM_LEVELS 1
102 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
105 #define ETHTOOL_PRIO_NUM_LEVELS 1
106 #define ETHTOOL_NUM_PRIOS 11
107 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
108 /* Promiscuous, Vlan, mac, ttc, inner ttc, {aRFS/accel and esp/esp_err} */
109 #define KERNEL_NIC_PRIO_NUM_LEVELS 7
110 #define KERNEL_NIC_NUM_PRIOS 1
111 /* One more level for tc */
112 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
114 #define KERNEL_NIC_TC_NUM_PRIOS 1
115 #define KERNEL_NIC_TC_NUM_LEVELS 2
117 #define ANCHOR_NUM_LEVELS 1
118 #define ANCHOR_NUM_PRIOS 1
119 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
121 #define OFFLOADS_MAX_FT 2
122 #define OFFLOADS_NUM_PRIOS 2
123 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
125 #define LAG_PRIO_NUM_LEVELS 1
126 #define LAG_NUM_PRIOS 1
127 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
129 #define KERNEL_TX_IPSEC_NUM_PRIOS 1
130 #define KERNEL_TX_IPSEC_NUM_LEVELS 1
131 #define KERNEL_TX_MIN_LEVEL (KERNEL_TX_IPSEC_NUM_LEVELS)
138 static struct init_tree_node {
139 enum fs_node_type type;
140 struct init_tree_node *children;
142 struct node_caps caps;
147 enum mlx5_flow_table_miss_action def_miss_action;
149 .type = FS_TYPE_NAMESPACE,
151 .children = (struct init_tree_node[]){
152 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
153 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
154 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
155 BY_PASS_PRIO_NUM_LEVELS))),
156 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
157 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
158 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
159 LAG_PRIO_NUM_LEVELS))),
160 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
161 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
162 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
164 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
165 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
166 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
167 ETHTOOL_PRIO_NUM_LEVELS))),
168 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
169 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
170 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
171 KERNEL_NIC_TC_NUM_LEVELS),
172 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
173 KERNEL_NIC_PRIO_NUM_LEVELS))),
174 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
175 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
176 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
177 LEFTOVERS_NUM_LEVELS))),
178 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
179 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
180 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
181 ANCHOR_NUM_LEVELS))),
185 static struct init_tree_node egress_root_fs = {
186 .type = FS_TYPE_NAMESPACE,
187 #ifdef CONFIG_MLX5_IPSEC
192 .children = (struct init_tree_node[]) {
193 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
194 FS_CHAINING_CAPS_EGRESS,
195 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
196 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
197 BY_PASS_PRIO_NUM_LEVELS))),
198 #ifdef CONFIG_MLX5_IPSEC
199 ADD_PRIO(0, KERNEL_TX_MIN_LEVEL, 0,
200 FS_CHAINING_CAPS_EGRESS,
201 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
202 ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
203 KERNEL_TX_IPSEC_NUM_LEVELS))),
208 #define RDMA_RX_BYPASS_PRIO 0
209 #define RDMA_RX_KERNEL_PRIO 1
210 static struct init_tree_node rdma_rx_root_fs = {
211 .type = FS_TYPE_NAMESPACE,
213 .children = (struct init_tree_node[]) {
214 [RDMA_RX_BYPASS_PRIO] =
215 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0,
217 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
218 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
219 BY_PASS_PRIO_NUM_LEVELS))),
220 [RDMA_RX_KERNEL_PRIO] =
221 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0,
223 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
224 ADD_MULTIPLE_PRIO(1, 1))),
228 static struct init_tree_node rdma_tx_root_fs = {
229 .type = FS_TYPE_NAMESPACE,
231 .children = (struct init_tree_node[]) {
232 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
233 FS_CHAINING_CAPS_RDMA_TX,
234 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
235 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
236 BY_PASS_PRIO_NUM_LEVELS))),
240 enum fs_i_lock_class {
246 static const struct rhashtable_params rhash_fte = {
247 .key_len = sizeof_field(struct fs_fte, val),
248 .key_offset = offsetof(struct fs_fte, val),
249 .head_offset = offsetof(struct fs_fte, hash),
250 .automatic_shrinking = true,
254 static const struct rhashtable_params rhash_fg = {
255 .key_len = sizeof_field(struct mlx5_flow_group, mask),
256 .key_offset = offsetof(struct mlx5_flow_group, mask),
257 .head_offset = offsetof(struct mlx5_flow_group, hash),
258 .automatic_shrinking = true,
263 static void del_hw_flow_table(struct fs_node *node);
264 static void del_hw_flow_group(struct fs_node *node);
265 static void del_hw_fte(struct fs_node *node);
266 static void del_sw_flow_table(struct fs_node *node);
267 static void del_sw_flow_group(struct fs_node *node);
268 static void del_sw_fte(struct fs_node *node);
269 static void del_sw_prio(struct fs_node *node);
270 static void del_sw_ns(struct fs_node *node);
271 /* Delete rule (destination) is special case that
272 * requires to lock the FTE for all the deletion process.
274 static void del_sw_hw_rule(struct fs_node *node);
275 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
276 struct mlx5_flow_destination *d2);
277 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
278 static struct mlx5_flow_rule *
279 find_flow_rule(struct fs_fte *fte,
280 struct mlx5_flow_destination *dest);
282 static void tree_init_node(struct fs_node *node,
283 void (*del_hw_func)(struct fs_node *),
284 void (*del_sw_func)(struct fs_node *))
286 refcount_set(&node->refcount, 1);
287 INIT_LIST_HEAD(&node->list);
288 INIT_LIST_HEAD(&node->children);
289 init_rwsem(&node->lock);
290 node->del_hw_func = del_hw_func;
291 node->del_sw_func = del_sw_func;
292 node->active = false;
295 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
298 refcount_inc(&parent->refcount);
299 node->parent = parent;
301 /* Parent is the root */
305 node->root = parent->root;
308 static int tree_get_node(struct fs_node *node)
310 return refcount_inc_not_zero(&node->refcount);
313 static void nested_down_read_ref_node(struct fs_node *node,
314 enum fs_i_lock_class class)
317 down_read_nested(&node->lock, class);
318 refcount_inc(&node->refcount);
322 static void nested_down_write_ref_node(struct fs_node *node,
323 enum fs_i_lock_class class)
326 down_write_nested(&node->lock, class);
327 refcount_inc(&node->refcount);
331 static void down_write_ref_node(struct fs_node *node, bool locked)
335 down_write(&node->lock);
336 refcount_inc(&node->refcount);
340 static void up_read_ref_node(struct fs_node *node)
342 refcount_dec(&node->refcount);
343 up_read(&node->lock);
346 static void up_write_ref_node(struct fs_node *node, bool locked)
348 refcount_dec(&node->refcount);
350 up_write(&node->lock);
353 static void tree_put_node(struct fs_node *node, bool locked)
355 struct fs_node *parent_node = node->parent;
357 if (refcount_dec_and_test(&node->refcount)) {
358 if (node->del_hw_func)
359 node->del_hw_func(node);
361 down_write_ref_node(parent_node, locked);
362 list_del_init(&node->list);
364 node->del_sw_func(node);
366 up_write_ref_node(parent_node, locked);
369 if (!node && parent_node)
370 tree_put_node(parent_node, locked);
373 static int tree_remove_node(struct fs_node *node, bool locked)
375 if (refcount_read(&node->refcount) > 1) {
376 refcount_dec(&node->refcount);
379 tree_put_node(node, locked);
383 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
386 struct fs_prio *iter_prio;
388 fs_for_each_prio(iter_prio, ns) {
389 if (iter_prio->prio == prio)
396 static bool is_fwd_next_action(u32 action)
398 return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
399 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
402 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
406 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
407 if (spec->match_value[i] & ~spec->match_criteria[i]) {
408 pr_warn("mlx5_core: match_value differs from match_criteria\n");
415 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
417 struct fs_node *root;
418 struct mlx5_flow_namespace *ns;
422 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
423 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
427 ns = container_of(root, struct mlx5_flow_namespace, node);
428 return container_of(ns, struct mlx5_flow_root_namespace, ns);
431 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
433 struct mlx5_flow_root_namespace *root = find_root(node);
436 return root->dev->priv.steering;
440 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
442 struct mlx5_flow_root_namespace *root = find_root(node);
449 static void del_sw_ns(struct fs_node *node)
454 static void del_sw_prio(struct fs_node *node)
459 static void del_hw_flow_table(struct fs_node *node)
461 struct mlx5_flow_root_namespace *root;
462 struct mlx5_flow_table *ft;
463 struct mlx5_core_dev *dev;
466 fs_get_obj(ft, node);
467 dev = get_dev(&ft->node);
468 root = find_root(&ft->node);
469 trace_mlx5_fs_del_ft(ft);
472 err = root->cmds->destroy_flow_table(root, ft);
474 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
478 static void del_sw_flow_table(struct fs_node *node)
480 struct mlx5_flow_table *ft;
481 struct fs_prio *prio;
483 fs_get_obj(ft, node);
485 rhltable_destroy(&ft->fgs_hash);
486 if (ft->node.parent) {
487 fs_get_obj(prio, ft->node.parent);
493 static void modify_fte(struct fs_fte *fte)
495 struct mlx5_flow_root_namespace *root;
496 struct mlx5_flow_table *ft;
497 struct mlx5_flow_group *fg;
498 struct mlx5_core_dev *dev;
501 fs_get_obj(fg, fte->node.parent);
502 fs_get_obj(ft, fg->node.parent);
503 dev = get_dev(&fte->node);
505 root = find_root(&ft->node);
506 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
509 "%s can't del rule fg id=%d fte_index=%d\n",
510 __func__, fg->id, fte->index);
511 fte->modify_mask = 0;
514 static void del_sw_hw_rule(struct fs_node *node)
516 struct mlx5_flow_rule *rule;
519 fs_get_obj(rule, node);
520 fs_get_obj(fte, rule->node.parent);
521 trace_mlx5_fs_del_rule(rule);
522 if (is_fwd_next_action(rule->sw_action)) {
523 mutex_lock(&rule->dest_attr.ft->lock);
524 list_del(&rule->next_ft);
525 mutex_unlock(&rule->dest_attr.ft->lock);
528 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
531 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
532 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
533 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
537 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT &&
539 fte->modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
540 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
544 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
547 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
553 static void del_hw_fte(struct fs_node *node)
555 struct mlx5_flow_root_namespace *root;
556 struct mlx5_flow_table *ft;
557 struct mlx5_flow_group *fg;
558 struct mlx5_core_dev *dev;
562 fs_get_obj(fte, node);
563 fs_get_obj(fg, fte->node.parent);
564 fs_get_obj(ft, fg->node.parent);
566 trace_mlx5_fs_del_fte(fte);
567 dev = get_dev(&ft->node);
568 root = find_root(&ft->node);
570 err = root->cmds->delete_fte(root, ft, fte);
573 "flow steering can't delete fte in index %d of flow group id %d\n",
579 static void del_sw_fte(struct fs_node *node)
581 struct mlx5_flow_steering *steering = get_steering(node);
582 struct mlx5_flow_group *fg;
586 fs_get_obj(fte, node);
587 fs_get_obj(fg, fte->node.parent);
589 err = rhashtable_remove_fast(&fg->ftes_hash,
593 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
594 kmem_cache_free(steering->ftes_cache, fte);
597 static void del_hw_flow_group(struct fs_node *node)
599 struct mlx5_flow_root_namespace *root;
600 struct mlx5_flow_group *fg;
601 struct mlx5_flow_table *ft;
602 struct mlx5_core_dev *dev;
604 fs_get_obj(fg, node);
605 fs_get_obj(ft, fg->node.parent);
606 dev = get_dev(&ft->node);
607 trace_mlx5_fs_del_fg(fg);
609 root = find_root(&ft->node);
610 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
611 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
615 static void del_sw_flow_group(struct fs_node *node)
617 struct mlx5_flow_steering *steering = get_steering(node);
618 struct mlx5_flow_group *fg;
619 struct mlx5_flow_table *ft;
622 fs_get_obj(fg, node);
623 fs_get_obj(ft, fg->node.parent);
625 rhashtable_destroy(&fg->ftes_hash);
626 ida_destroy(&fg->fte_allocator);
627 if (ft->autogroup.active &&
628 fg->max_ftes == ft->autogroup.group_size &&
629 fg->start_index < ft->autogroup.max_fte)
630 ft->autogroup.num_groups--;
631 err = rhltable_remove(&ft->fgs_hash,
635 kmem_cache_free(steering->fgs_cache, fg);
638 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
643 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
647 fte->index = index + fg->start_index;
648 ret = rhashtable_insert_fast(&fg->ftes_hash,
654 tree_add_node(&fte->node, &fg->node);
655 list_add_tail(&fte->node.list, &fg->node.children);
659 ida_simple_remove(&fg->fte_allocator, index);
663 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
664 const struct mlx5_flow_spec *spec,
665 struct mlx5_flow_act *flow_act)
667 struct mlx5_flow_steering *steering = get_steering(&ft->node);
670 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
672 return ERR_PTR(-ENOMEM);
674 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
675 fte->node.type = FS_TYPE_FLOW_ENTRY;
676 fte->action = *flow_act;
677 fte->flow_context = spec->flow_context;
679 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
684 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
685 struct mlx5_flow_group *fg)
687 rhashtable_destroy(&fg->ftes_hash);
688 kmem_cache_free(steering->fgs_cache, fg);
691 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
692 u8 match_criteria_enable,
693 const void *match_criteria,
697 struct mlx5_flow_group *fg;
700 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
702 return ERR_PTR(-ENOMEM);
704 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
706 kmem_cache_free(steering->fgs_cache, fg);
710 ida_init(&fg->fte_allocator);
711 fg->mask.match_criteria_enable = match_criteria_enable;
712 memcpy(&fg->mask.match_criteria, match_criteria,
713 sizeof(fg->mask.match_criteria));
714 fg->node.type = FS_TYPE_FLOW_GROUP;
715 fg->start_index = start_index;
716 fg->max_ftes = end_index - start_index + 1;
721 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
722 u8 match_criteria_enable,
723 const void *match_criteria,
726 struct list_head *prev)
728 struct mlx5_flow_steering *steering = get_steering(&ft->node);
729 struct mlx5_flow_group *fg;
732 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
733 start_index, end_index);
737 /* initialize refcnt, add to parent list */
738 ret = rhltable_insert(&ft->fgs_hash,
742 dealloc_flow_group(steering, fg);
746 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
747 tree_add_node(&fg->node, &ft->node);
748 /* Add node to group list */
749 list_add(&fg->node.list, prev);
750 atomic_inc(&ft->node.version);
755 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
756 enum fs_flow_table_type table_type,
757 enum fs_flow_table_op_mod op_mod,
760 struct mlx5_flow_table *ft;
763 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
765 return ERR_PTR(-ENOMEM);
767 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
774 ft->node.type = FS_TYPE_FLOW_TABLE;
776 ft->type = table_type;
778 ft->max_fte = max_fte;
780 INIT_LIST_HEAD(&ft->fwd_rules);
781 mutex_init(&ft->lock);
786 /* If reverse is false, then we search for the first flow table in the
787 * root sub-tree from start(closest from right), else we search for the
788 * last flow table in the root sub-tree till start(closest from left).
790 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
791 struct list_head *start,
794 #define list_advance_entry(pos, reverse) \
795 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
797 #define list_for_each_advance_continue(pos, head, reverse) \
798 for (pos = list_advance_entry(pos, reverse); \
799 &pos->list != (head); \
800 pos = list_advance_entry(pos, reverse))
802 struct fs_node *iter = list_entry(start, struct fs_node, list);
803 struct mlx5_flow_table *ft = NULL;
805 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
808 list_for_each_advance_continue(iter, &root->children, reverse) {
809 if (iter->type == FS_TYPE_FLOW_TABLE) {
810 fs_get_obj(ft, iter);
813 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
821 /* If reverse is false then return the first flow table in next priority of
822 * prio in the tree, else return the last flow table in the previous priority
823 * of prio in the tree.
825 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
827 struct mlx5_flow_table *ft = NULL;
828 struct fs_node *curr_node;
829 struct fs_node *parent;
831 parent = prio->node.parent;
832 curr_node = &prio->node;
833 while (!ft && parent) {
834 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
836 parent = curr_node->parent;
841 /* Assuming all the tree is locked by mutex chain lock */
842 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
844 return find_closest_ft(prio, false);
847 /* Assuming all the tree is locked by mutex chain lock */
848 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
850 return find_closest_ft(prio, true);
853 static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
854 struct mlx5_flow_act *flow_act)
856 struct fs_prio *prio;
859 next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
860 fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
862 return find_next_chained_ft(prio);
865 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
866 struct fs_prio *prio,
867 struct mlx5_flow_table *ft)
869 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
870 struct mlx5_flow_table *iter;
873 fs_for_each_ft(iter, prio) {
874 err = root->cmds->modify_flow_table(root, iter, ft);
877 "Failed to modify flow table id %d, type %d, err %d\n",
878 iter->id, iter->type, err);
879 /* The driver is out of sync with the FW */
886 /* Connect flow tables from previous priority of prio to ft */
887 static int connect_prev_fts(struct mlx5_core_dev *dev,
888 struct mlx5_flow_table *ft,
889 struct fs_prio *prio)
891 struct mlx5_flow_table *prev_ft;
893 prev_ft = find_prev_chained_ft(prio);
895 struct fs_prio *prev_prio;
897 fs_get_obj(prev_prio, prev_ft->node.parent);
898 return connect_fts_in_prio(dev, prev_prio, ft);
903 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
906 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
907 struct mlx5_ft_underlay_qp *uqp;
908 int min_level = INT_MAX;
913 min_level = root->root_ft->level;
915 if (ft->level >= min_level)
918 if (list_empty(&root->underlay_qpns)) {
919 /* Don't set any QPN (zero) in case QPN list is empty */
921 err = root->cmds->update_root_ft(root, ft, qpn, false);
923 list_for_each_entry(uqp, &root->underlay_qpns, list) {
925 err = root->cmds->update_root_ft(root, ft,
933 mlx5_core_warn(root->dev,
934 "Update root flow table of id(%u) qpn(%d) failed\n",
942 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
943 struct mlx5_flow_destination *dest)
945 struct mlx5_flow_root_namespace *root;
946 struct mlx5_flow_table *ft;
947 struct mlx5_flow_group *fg;
949 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
952 fs_get_obj(fte, rule->node.parent);
953 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
955 down_write_ref_node(&fte->node, false);
956 fs_get_obj(fg, fte->node.parent);
957 fs_get_obj(ft, fg->node.parent);
959 memcpy(&rule->dest_attr, dest, sizeof(*dest));
960 root = find_root(&ft->node);
961 err = root->cmds->update_fte(root, ft, fg,
963 up_write_ref_node(&fte->node, false);
968 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
969 struct mlx5_flow_destination *new_dest,
970 struct mlx5_flow_destination *old_dest)
975 if (handle->num_rules != 1)
977 return _mlx5_modify_rule_destination(handle->rule[0],
981 for (i = 0; i < handle->num_rules; i++) {
982 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
983 return _mlx5_modify_rule_destination(handle->rule[i],
990 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
991 static int connect_fwd_rules(struct mlx5_core_dev *dev,
992 struct mlx5_flow_table *new_next_ft,
993 struct mlx5_flow_table *old_next_ft)
995 struct mlx5_flow_destination dest = {};
996 struct mlx5_flow_rule *iter;
999 /* new_next_ft and old_next_ft could be NULL only
1000 * when we create/destroy the anchor flow table.
1002 if (!new_next_ft || !old_next_ft)
1005 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1006 dest.ft = new_next_ft;
1008 mutex_lock(&old_next_ft->lock);
1009 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1010 mutex_unlock(&old_next_ft->lock);
1011 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
1012 if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
1013 iter->ft->ns == new_next_ft->ns)
1016 err = _mlx5_modify_rule_destination(iter, &dest);
1018 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1024 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1025 struct fs_prio *prio)
1027 struct mlx5_flow_table *next_ft;
1030 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
1032 if (list_empty(&prio->node.children)) {
1033 err = connect_prev_fts(dev, ft, prio);
1037 next_ft = find_next_chained_ft(prio);
1038 err = connect_fwd_rules(dev, ft, next_ft);
1043 if (MLX5_CAP_FLOWTABLE(dev,
1044 flow_table_properties_nic_receive.modify_root))
1045 err = update_root_ft_create(ft, prio);
1049 static void list_add_flow_table(struct mlx5_flow_table *ft,
1050 struct fs_prio *prio)
1052 struct list_head *prev = &prio->node.children;
1053 struct mlx5_flow_table *iter;
1055 fs_for_each_ft(iter, prio) {
1056 if (iter->level > ft->level)
1058 prev = &iter->node.list;
1060 list_add(&ft->node.list, prev);
1063 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1064 struct mlx5_flow_table_attr *ft_attr,
1065 enum fs_flow_table_op_mod op_mod,
1068 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1069 bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
1070 struct mlx5_flow_table *next_ft;
1071 struct fs_prio *fs_prio = NULL;
1072 struct mlx5_flow_table *ft;
1077 pr_err("mlx5: flow steering failed to find root of namespace\n");
1078 return ERR_PTR(-ENODEV);
1081 mutex_lock(&root->chain_lock);
1082 fs_prio = find_prio(ns, ft_attr->prio);
1088 /* The level is related to the
1089 * priority level range.
1091 if (ft_attr->level >= fs_prio->num_levels) {
1096 ft_attr->level += fs_prio->start_level;
1099 /* The level is related to the
1100 * priority level range.
1102 ft = alloc_flow_table(ft_attr->level,
1104 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1106 op_mod, ft_attr->flags);
1112 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1113 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1114 next_ft = unmanaged ? ft_attr->next_ft :
1115 find_next_chained_ft(fs_prio);
1116 ft->def_miss_action = ns->def_miss_action;
1118 err = root->cmds->create_flow_table(root, ft, log_table_sz, next_ft);
1123 err = connect_flow_table(root->dev, ft, fs_prio);
1128 ft->node.active = true;
1129 down_write_ref_node(&fs_prio->node, false);
1131 tree_add_node(&ft->node, &fs_prio->node);
1132 list_add_flow_table(ft, fs_prio);
1134 ft->node.root = fs_prio->node.root;
1137 up_write_ref_node(&fs_prio->node, false);
1138 mutex_unlock(&root->chain_lock);
1139 trace_mlx5_fs_add_ft(ft);
1142 root->cmds->destroy_flow_table(root, ft);
1144 rhltable_destroy(&ft->fgs_hash);
1147 mutex_unlock(&root->chain_lock);
1148 return ERR_PTR(err);
1151 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1152 struct mlx5_flow_table_attr *ft_attr)
1154 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1156 EXPORT_SYMBOL(mlx5_create_flow_table);
1158 struct mlx5_flow_table *
1159 mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1160 struct mlx5_flow_table_attr *ft_attr, u16 vport)
1162 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1165 struct mlx5_flow_table*
1166 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1167 int prio, u32 level)
1169 struct mlx5_flow_table_attr ft_attr = {};
1171 ft_attr.level = level;
1172 ft_attr.prio = prio;
1173 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1175 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1177 struct mlx5_flow_table*
1178 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1179 struct mlx5_flow_table_attr *ft_attr)
1181 int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1182 int autogroups_max_fte = ft_attr->max_fte - num_reserved_entries;
1183 int max_num_groups = ft_attr->autogroup.max_num_groups;
1184 struct mlx5_flow_table *ft;
1186 if (max_num_groups > autogroups_max_fte)
1187 return ERR_PTR(-EINVAL);
1188 if (num_reserved_entries > ft_attr->max_fte)
1189 return ERR_PTR(-EINVAL);
1191 ft = mlx5_create_flow_table(ns, ft_attr);
1195 ft->autogroup.active = true;
1196 ft->autogroup.required_groups = max_num_groups;
1197 ft->autogroup.max_fte = autogroups_max_fte;
1198 /* We save place for flow groups in addition to max types */
1199 ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
1203 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1205 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1208 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1209 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1210 fg_in, match_criteria);
1211 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1213 match_criteria_enable);
1214 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1216 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1218 struct mlx5_flow_group *fg;
1221 if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1222 return ERR_PTR(-EPERM);
1224 down_write_ref_node(&ft->node, false);
1225 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1226 start_index, end_index,
1227 ft->node.children.prev);
1228 up_write_ref_node(&ft->node, false);
1232 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1234 tree_put_node(&fg->node, false);
1235 return ERR_PTR(err);
1237 trace_mlx5_fs_add_fg(fg);
1238 fg->node.active = true;
1242 EXPORT_SYMBOL(mlx5_create_flow_group);
1244 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1246 struct mlx5_flow_rule *rule;
1248 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1252 INIT_LIST_HEAD(&rule->next_ft);
1253 rule->node.type = FS_TYPE_FLOW_DEST;
1255 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1260 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1262 struct mlx5_flow_handle *handle;
1264 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1268 handle->num_rules = num_rules;
1273 static void destroy_flow_handle(struct fs_fte *fte,
1274 struct mlx5_flow_handle *handle,
1275 struct mlx5_flow_destination *dest,
1279 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1281 list_del(&handle->rule[i]->node.list);
1282 kfree(handle->rule[i]);
1288 static struct mlx5_flow_handle *
1289 create_flow_handle(struct fs_fte *fte,
1290 struct mlx5_flow_destination *dest,
1295 struct mlx5_flow_handle *handle;
1296 struct mlx5_flow_rule *rule = NULL;
1297 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1298 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1302 handle = alloc_handle((dest_num) ? dest_num : 1);
1304 return ERR_PTR(-ENOMEM);
1308 rule = find_flow_rule(fte, dest + i);
1310 refcount_inc(&rule->node.refcount);
1316 rule = alloc_rule(dest + i);
1320 /* Add dest to dests list- we need flow tables to be in the
1321 * end of the list for forward to next prio rules.
1323 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1325 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1326 list_add(&rule->node.list, &fte->node.children);
1328 list_add_tail(&rule->node.list, &fte->node.children);
1332 type = dest[i].type ==
1333 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1334 *modify_mask |= type ? count : dst;
1337 handle->rule[i] = rule;
1338 } while (++i < dest_num);
1343 destroy_flow_handle(fte, handle, dest, i);
1344 return ERR_PTR(-ENOMEM);
1347 /* fte should not be deleted while calling this function */
1348 static struct mlx5_flow_handle *
1349 add_rule_fte(struct fs_fte *fte,
1350 struct mlx5_flow_group *fg,
1351 struct mlx5_flow_destination *dest,
1355 struct mlx5_flow_root_namespace *root;
1356 struct mlx5_flow_handle *handle;
1357 struct mlx5_flow_table *ft;
1358 int modify_mask = 0;
1360 bool new_rule = false;
1362 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1364 if (IS_ERR(handle) || !new_rule)
1368 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1370 fs_get_obj(ft, fg->node.parent);
1371 root = find_root(&fg->node);
1372 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1373 err = root->cmds->create_fte(root, ft, fg, fte);
1375 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1379 fte->node.active = true;
1380 fte->status |= FS_FTE_STATUS_EXISTING;
1381 atomic_inc(&fg->node.version);
1387 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1388 return ERR_PTR(err);
1391 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1392 const struct mlx5_flow_spec *spec)
1394 struct list_head *prev = &ft->node.children;
1395 u32 max_fte = ft->autogroup.max_fte;
1396 unsigned int candidate_index = 0;
1397 unsigned int group_size = 0;
1398 struct mlx5_flow_group *fg;
1400 if (!ft->autogroup.active)
1401 return ERR_PTR(-ENOENT);
1403 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1404 group_size = ft->autogroup.group_size;
1406 /* max_fte == ft->autogroup.max_types */
1407 if (group_size == 0)
1410 /* sorted by start_index */
1411 fs_for_each_fg(fg, ft) {
1412 if (candidate_index + group_size > fg->start_index)
1413 candidate_index = fg->start_index + fg->max_ftes;
1416 prev = &fg->node.list;
1419 if (candidate_index + group_size > max_fte)
1420 return ERR_PTR(-ENOSPC);
1422 fg = alloc_insert_flow_group(ft,
1423 spec->match_criteria_enable,
1424 spec->match_criteria,
1426 candidate_index + group_size - 1,
1431 if (group_size == ft->autogroup.group_size)
1432 ft->autogroup.num_groups++;
1438 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1439 struct mlx5_flow_group *fg)
1441 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1442 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1443 void *match_criteria_addr;
1444 u8 src_esw_owner_mask_on;
1449 in = kvzalloc(inlen, GFP_KERNEL);
1453 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1454 fg->mask.match_criteria_enable);
1455 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1456 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1459 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1461 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1462 source_eswitch_owner_vhca_id);
1463 MLX5_SET(create_flow_group_in, in,
1464 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1466 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1467 in, match_criteria);
1468 memcpy(match_criteria_addr, fg->mask.match_criteria,
1469 sizeof(fg->mask.match_criteria));
1471 err = root->cmds->create_flow_group(root, ft, in, fg);
1473 fg->node.active = true;
1474 trace_mlx5_fs_add_fg(fg);
1481 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1482 struct mlx5_flow_destination *d2)
1484 if (d1->type == d2->type) {
1485 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1486 d1->vport.num == d2->vport.num &&
1487 d1->vport.flags == d2->vport.flags &&
1488 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1489 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1490 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1491 (d1->vport.pkt_reformat->id ==
1492 d2->vport.pkt_reformat->id) : true)) ||
1493 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1494 d1->ft == d2->ft) ||
1495 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1496 d1->tir_num == d2->tir_num) ||
1497 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1498 d1->ft_num == d2->ft_num))
1505 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1506 struct mlx5_flow_destination *dest)
1508 struct mlx5_flow_rule *rule;
1510 list_for_each_entry(rule, &fte->node.children, node.list) {
1511 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1517 static bool check_conflicting_actions(u32 action1, u32 action2)
1519 u32 xored_actions = action1 ^ action2;
1521 /* if one rule only wants to count, it's ok */
1522 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1523 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1526 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1527 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1528 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1529 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1530 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1531 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1532 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1533 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1539 static int check_conflicting_ftes(struct fs_fte *fte,
1540 const struct mlx5_flow_context *flow_context,
1541 const struct mlx5_flow_act *flow_act)
1543 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1544 mlx5_core_warn(get_dev(&fte->node),
1545 "Found two FTEs with conflicting actions\n");
1549 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1550 fte->flow_context.flow_tag != flow_context->flow_tag) {
1551 mlx5_core_warn(get_dev(&fte->node),
1552 "FTE flow tag %u already exists with different flow tag %u\n",
1553 fte->flow_context.flow_tag,
1554 flow_context->flow_tag);
1561 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1562 const struct mlx5_flow_spec *spec,
1563 struct mlx5_flow_act *flow_act,
1564 struct mlx5_flow_destination *dest,
1568 struct mlx5_flow_handle *handle;
1573 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1575 return ERR_PTR(ret);
1577 old_action = fte->action.action;
1578 fte->action.action |= flow_act->action;
1579 handle = add_rule_fte(fte, fg, dest, dest_num,
1580 old_action != flow_act->action);
1581 if (IS_ERR(handle)) {
1582 fte->action.action = old_action;
1585 trace_mlx5_fs_set_fte(fte, false);
1587 for (i = 0; i < handle->num_rules; i++) {
1588 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1589 tree_add_node(&handle->rule[i]->node, &fte->node);
1590 trace_mlx5_fs_add_rule(handle->rule[i]);
1596 static bool counter_is_valid(u32 action)
1598 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1599 MLX5_FLOW_CONTEXT_ACTION_ALLOW |
1600 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1603 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1604 struct mlx5_flow_act *flow_act,
1605 struct mlx5_flow_table *ft)
1607 bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
1608 u32 action = flow_act->action;
1610 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1611 return counter_is_valid(action);
1613 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1617 if (ft->type != FS_FT_FDB &&
1618 ft->type != FS_FT_NIC_RX)
1621 if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1622 ft->type != dest->ft->type)
1626 if (!dest || ((dest->type ==
1627 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1628 (dest->ft->level <= ft->level && !ignore_level)))
1634 struct list_head list;
1635 struct mlx5_flow_group *g;
1638 static void free_match_list(struct match_list *head, bool ft_locked)
1640 struct match_list *iter, *match_tmp;
1642 list_for_each_entry_safe(iter, match_tmp, &head->list,
1644 tree_put_node(&iter->g->node, ft_locked);
1645 list_del(&iter->list);
1650 static int build_match_list(struct match_list *match_head,
1651 struct mlx5_flow_table *ft,
1652 const struct mlx5_flow_spec *spec,
1655 struct rhlist_head *tmp, *list;
1656 struct mlx5_flow_group *g;
1660 INIT_LIST_HEAD(&match_head->list);
1661 /* Collect all fgs which has a matching match_criteria */
1662 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1663 /* RCU is atomic, we can't execute FW commands here */
1664 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1665 struct match_list *curr_match;
1667 if (unlikely(!tree_get_node(&g->node)))
1670 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1672 free_match_list(match_head, ft_locked);
1677 list_add_tail(&curr_match->list, &match_head->list);
1684 static u64 matched_fgs_get_version(struct list_head *match_head)
1686 struct match_list *iter;
1689 list_for_each_entry(iter, match_head, list)
1690 version += (u64)atomic_read(&iter->g->node.version);
1694 static struct fs_fte *
1695 lookup_fte_locked(struct mlx5_flow_group *g,
1696 const u32 *match_value,
1699 struct fs_fte *fte_tmp;
1702 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1704 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1705 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1707 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1711 if (!fte_tmp->node.active) {
1712 tree_put_node(&fte_tmp->node, false);
1717 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1720 up_write_ref_node(&g->node, false);
1722 up_read_ref_node(&g->node);
1726 static struct mlx5_flow_handle *
1727 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1728 struct list_head *match_head,
1729 const struct mlx5_flow_spec *spec,
1730 struct mlx5_flow_act *flow_act,
1731 struct mlx5_flow_destination *dest,
1735 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1736 struct mlx5_flow_group *g;
1737 struct mlx5_flow_handle *rule;
1738 struct match_list *iter;
1739 bool take_write = false;
1744 fte = alloc_fte(ft, spec, flow_act);
1746 return ERR_PTR(-ENOMEM);
1748 search_again_locked:
1749 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1751 version = matched_fgs_get_version(match_head);
1752 /* Try to find an fte with identical match value and attempt update its
1755 list_for_each_entry(iter, match_head, list) {
1756 struct fs_fte *fte_tmp;
1759 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1762 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
1763 up_write_ref_node(&fte_tmp->node, false);
1764 tree_put_node(&fte_tmp->node, false);
1765 kmem_cache_free(steering->ftes_cache, fte);
1770 /* No group with matching fte found, or we skipped the search.
1771 * Try to add a new fte to any matching fg.
1774 /* Check the ft version, for case that new flow group
1775 * was added while the fgs weren't locked
1777 if (atomic_read(&ft->node.version) != ft_version) {
1778 rule = ERR_PTR(-EAGAIN);
1782 /* Check the fgs version. If version have changed it could be that an
1783 * FTE with the same match value was added while the fgs weren't
1786 if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
1787 version != matched_fgs_get_version(match_head)) {
1789 goto search_again_locked;
1792 list_for_each_entry(iter, match_head, list) {
1795 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1797 if (!g->node.active) {
1798 up_write_ref_node(&g->node, false);
1802 err = insert_fte(g, fte);
1804 up_write_ref_node(&g->node, false);
1807 kmem_cache_free(steering->ftes_cache, fte);
1808 return ERR_PTR(err);
1811 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1812 up_write_ref_node(&g->node, false);
1813 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1814 up_write_ref_node(&fte->node, false);
1817 rule = ERR_PTR(-ENOENT);
1819 kmem_cache_free(steering->ftes_cache, fte);
1823 static struct mlx5_flow_handle *
1824 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1825 const struct mlx5_flow_spec *spec,
1826 struct mlx5_flow_act *flow_act,
1827 struct mlx5_flow_destination *dest,
1831 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1832 struct mlx5_flow_handle *rule;
1833 struct match_list match_head;
1834 struct mlx5_flow_group *g;
1835 bool take_write = false;
1841 if (!check_valid_spec(spec))
1842 return ERR_PTR(-EINVAL);
1844 for (i = 0; i < dest_num; i++) {
1845 if (!dest_is_valid(&dest[i], flow_act, ft))
1846 return ERR_PTR(-EINVAL);
1848 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1849 search_again_locked:
1850 version = atomic_read(&ft->node.version);
1852 /* Collect all fgs which has a matching match_criteria */
1853 err = build_match_list(&match_head, ft, spec, take_write);
1856 up_write_ref_node(&ft->node, false);
1858 up_read_ref_node(&ft->node);
1859 return ERR_PTR(err);
1863 up_read_ref_node(&ft->node);
1865 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1867 free_match_list(&match_head, take_write);
1868 if (!IS_ERR(rule) ||
1869 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1871 up_write_ref_node(&ft->node, false);
1876 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1880 if (PTR_ERR(rule) == -EAGAIN ||
1881 version != atomic_read(&ft->node.version))
1882 goto search_again_locked;
1884 g = alloc_auto_flow_group(ft, spec);
1887 up_write_ref_node(&ft->node, false);
1891 fte = alloc_fte(ft, spec, flow_act);
1893 up_write_ref_node(&ft->node, false);
1898 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1899 up_write_ref_node(&ft->node, false);
1901 err = create_auto_flow_group(ft, g);
1903 goto err_release_fg;
1905 err = insert_fte(g, fte);
1907 goto err_release_fg;
1909 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1910 up_write_ref_node(&g->node, false);
1911 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1912 up_write_ref_node(&fte->node, false);
1913 tree_put_node(&g->node, false);
1917 up_write_ref_node(&g->node, false);
1918 kmem_cache_free(steering->ftes_cache, fte);
1920 tree_put_node(&g->node, false);
1921 return ERR_PTR(err);
1924 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1926 return ((ft->type == FS_FT_NIC_RX) &&
1927 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1930 struct mlx5_flow_handle *
1931 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1932 const struct mlx5_flow_spec *spec,
1933 struct mlx5_flow_act *flow_act,
1934 struct mlx5_flow_destination *dest,
1937 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1938 static const struct mlx5_flow_spec zero_spec = {};
1939 struct mlx5_flow_destination *gen_dest = NULL;
1940 struct mlx5_flow_table *next_ft = NULL;
1941 struct mlx5_flow_handle *handle = NULL;
1942 u32 sw_action = flow_act->action;
1948 if (!is_fwd_next_action(sw_action))
1949 return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1951 if (!fwd_next_prio_supported(ft))
1952 return ERR_PTR(-EOPNOTSUPP);
1954 mutex_lock(&root->chain_lock);
1955 next_ft = find_next_fwd_ft(ft, flow_act);
1957 handle = ERR_PTR(-EOPNOTSUPP);
1961 gen_dest = kcalloc(num_dest + 1, sizeof(*dest),
1964 handle = ERR_PTR(-ENOMEM);
1967 for (i = 0; i < num_dest; i++)
1968 gen_dest[i] = dest[i];
1970 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1971 gen_dest[i].ft = next_ft;
1974 flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
1975 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
1976 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1977 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1981 if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
1982 mutex_lock(&next_ft->lock);
1983 list_add(&handle->rule[num_dest - 1]->next_ft,
1984 &next_ft->fwd_rules);
1985 mutex_unlock(&next_ft->lock);
1986 handle->rule[num_dest - 1]->sw_action = sw_action;
1987 handle->rule[num_dest - 1]->ft = ft;
1990 mutex_unlock(&root->chain_lock);
1994 EXPORT_SYMBOL(mlx5_add_flow_rules);
1996 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
2001 /* In order to consolidate the HW changes we lock the FTE for other
2002 * changes, and increase its refcount, in order not to perform the
2003 * "del" functions of the FTE. Will handle them here.
2004 * The removal of the rules is done under locked FTE.
2005 * After removing all the handle's rules, if there are remaining
2006 * rules, it means we just need to modify the FTE in FW, and
2007 * unlock/decrease the refcount we increased before.
2008 * Otherwise, it means the FTE should be deleted. First delete the
2009 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2010 * the FTE, which will handle the last decrease of the refcount, as
2011 * well as required handling of its parent.
2013 fs_get_obj(fte, handle->rule[0]->node.parent);
2014 down_write_ref_node(&fte->node, false);
2015 for (i = handle->num_rules - 1; i >= 0; i--)
2016 tree_remove_node(&handle->rule[i]->node, true);
2017 if (fte->dests_size) {
2018 if (fte->modify_mask)
2020 up_write_ref_node(&fte->node, false);
2021 } else if (list_empty(&fte->node.children)) {
2022 del_hw_fte(&fte->node);
2023 /* Avoid double call to del_hw_fte */
2024 fte->node.del_hw_func = NULL;
2025 up_write_ref_node(&fte->node, false);
2026 tree_put_node(&fte->node, false);
2030 EXPORT_SYMBOL(mlx5_del_flow_rules);
2032 /* Assuming prio->node.children(flow tables) is sorted by level */
2033 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2035 struct fs_prio *prio;
2037 fs_get_obj(prio, ft->node.parent);
2039 if (!list_is_last(&ft->node.list, &prio->node.children))
2040 return list_next_entry(ft, node.list);
2041 return find_next_chained_ft(prio);
2044 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2046 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2047 struct mlx5_ft_underlay_qp *uqp;
2048 struct mlx5_flow_table *new_root_ft = NULL;
2052 if (root->root_ft != ft)
2055 new_root_ft = find_next_ft(ft);
2057 root->root_ft = NULL;
2061 if (list_empty(&root->underlay_qpns)) {
2062 /* Don't set any QPN (zero) in case QPN list is empty */
2064 err = root->cmds->update_root_ft(root, new_root_ft,
2067 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2069 err = root->cmds->update_root_ft(root,
2078 mlx5_core_warn(root->dev,
2079 "Update root flow table of id(%u) qpn(%d) failed\n",
2082 root->root_ft = new_root_ft;
2087 /* Connect flow table from previous priority to
2088 * the next flow table.
2090 static int disconnect_flow_table(struct mlx5_flow_table *ft)
2092 struct mlx5_core_dev *dev = get_dev(&ft->node);
2093 struct mlx5_flow_table *next_ft;
2094 struct fs_prio *prio;
2097 err = update_root_ft_destroy(ft);
2101 fs_get_obj(prio, ft->node.parent);
2102 if (!(list_first_entry(&prio->node.children,
2103 struct mlx5_flow_table,
2107 next_ft = find_next_chained_ft(prio);
2108 err = connect_fwd_rules(dev, next_ft, ft);
2112 err = connect_prev_fts(dev, next_ft, prio);
2114 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2119 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2121 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2124 mutex_lock(&root->chain_lock);
2125 if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2126 err = disconnect_flow_table(ft);
2128 mutex_unlock(&root->chain_lock);
2131 if (tree_remove_node(&ft->node, false))
2132 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2134 mutex_unlock(&root->chain_lock);
2138 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2140 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2142 if (tree_remove_node(&fg->node, false))
2143 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2146 EXPORT_SYMBOL(mlx5_destroy_flow_group);
2148 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2151 struct mlx5_flow_steering *steering = dev->priv.steering;
2153 if (!steering || !steering->fdb_sub_ns)
2156 return steering->fdb_sub_ns[n];
2158 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2160 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2161 enum mlx5_flow_namespace_type type)
2163 struct mlx5_flow_steering *steering = dev->priv.steering;
2164 struct mlx5_flow_root_namespace *root_ns;
2166 struct fs_prio *fs_prio;
2167 struct mlx5_flow_namespace *ns;
2173 case MLX5_FLOW_NAMESPACE_FDB:
2174 if (steering->fdb_root_ns)
2175 return &steering->fdb_root_ns->ns;
2177 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2178 if (steering->sniffer_rx_root_ns)
2179 return &steering->sniffer_rx_root_ns->ns;
2181 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2182 if (steering->sniffer_tx_root_ns)
2183 return &steering->sniffer_tx_root_ns->ns;
2189 if (type == MLX5_FLOW_NAMESPACE_EGRESS ||
2190 type == MLX5_FLOW_NAMESPACE_EGRESS_KERNEL) {
2191 root_ns = steering->egress_root_ns;
2192 prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
2193 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2194 root_ns = steering->rdma_rx_root_ns;
2195 prio = RDMA_RX_BYPASS_PRIO;
2196 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2197 root_ns = steering->rdma_rx_root_ns;
2198 prio = RDMA_RX_KERNEL_PRIO;
2199 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
2200 root_ns = steering->rdma_tx_root_ns;
2201 } else { /* Must be NIC RX */
2202 root_ns = steering->root_ns;
2209 fs_prio = find_prio(&root_ns->ns, prio);
2213 ns = list_first_entry(&fs_prio->node.children,
2219 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2221 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2222 enum mlx5_flow_namespace_type type,
2225 struct mlx5_flow_steering *steering = dev->priv.steering;
2227 if (!steering || vport >= mlx5_eswitch_get_total_vports(dev))
2231 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2232 if (steering->esw_egress_root_ns &&
2233 steering->esw_egress_root_ns[vport])
2234 return &steering->esw_egress_root_ns[vport]->ns;
2237 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2238 if (steering->esw_ingress_root_ns &&
2239 steering->esw_ingress_root_ns[vport])
2240 return &steering->esw_ingress_root_ns[vport]->ns;
2248 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2251 enum fs_node_type type)
2253 struct fs_prio *fs_prio;
2255 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2257 return ERR_PTR(-ENOMEM);
2259 fs_prio->node.type = type;
2260 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2261 tree_add_node(&fs_prio->node, &ns->node);
2262 fs_prio->num_levels = num_levels;
2263 fs_prio->prio = prio;
2264 list_add_tail(&fs_prio->node.list, &ns->node.children);
2269 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2273 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2276 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2277 unsigned int prio, int num_levels)
2279 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2282 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2285 ns->node.type = FS_TYPE_NAMESPACE;
2290 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2293 struct mlx5_flow_namespace *ns;
2295 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2297 return ERR_PTR(-ENOMEM);
2299 fs_init_namespace(ns);
2300 ns->def_miss_action = def_miss_act;
2301 tree_init_node(&ns->node, NULL, del_sw_ns);
2302 tree_add_node(&ns->node, &prio->node);
2303 list_add_tail(&ns->node.list, &prio->node.children);
2308 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2309 struct init_tree_node *prio_metadata)
2311 struct fs_prio *fs_prio;
2314 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2315 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2316 if (IS_ERR(fs_prio))
2317 return PTR_ERR(fs_prio);
2322 #define FLOW_TABLE_BIT_SZ 1
2323 #define GET_FLOW_TABLE_CAP(dev, offset) \
2324 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2326 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2327 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2331 for (i = 0; i < caps->arr_sz; i++) {
2332 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2338 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2339 struct init_tree_node *init_node,
2340 struct fs_node *fs_parent_node,
2341 struct init_tree_node *init_parent_node,
2344 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2345 flow_table_properties_nic_receive.
2347 struct mlx5_flow_namespace *fs_ns;
2348 struct fs_prio *fs_prio;
2349 struct fs_node *base;
2353 if (init_node->type == FS_TYPE_PRIO) {
2354 if ((init_node->min_ft_level > max_ft_level) ||
2355 !has_required_caps(steering->dev, &init_node->caps))
2358 fs_get_obj(fs_ns, fs_parent_node);
2359 if (init_node->num_leaf_prios)
2360 return create_leaf_prios(fs_ns, prio, init_node);
2361 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2362 if (IS_ERR(fs_prio))
2363 return PTR_ERR(fs_prio);
2364 base = &fs_prio->node;
2365 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2366 fs_get_obj(fs_prio, fs_parent_node);
2367 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2369 return PTR_ERR(fs_ns);
2370 base = &fs_ns->node;
2375 for (i = 0; i < init_node->ar_size; i++) {
2376 err = init_root_tree_recursive(steering, &init_node->children[i],
2377 base, init_node, prio);
2380 if (init_node->children[i].type == FS_TYPE_PRIO &&
2381 init_node->children[i].num_leaf_prios) {
2382 prio += init_node->children[i].num_leaf_prios;
2389 static int init_root_tree(struct mlx5_flow_steering *steering,
2390 struct init_tree_node *init_node,
2391 struct fs_node *fs_parent_node)
2394 struct mlx5_flow_namespace *fs_ns;
2397 fs_get_obj(fs_ns, fs_parent_node);
2398 for (i = 0; i < init_node->ar_size; i++) {
2399 err = init_root_tree_recursive(steering, &init_node->children[i],
2408 static void del_sw_root_ns(struct fs_node *node)
2410 struct mlx5_flow_root_namespace *root_ns;
2411 struct mlx5_flow_namespace *ns;
2413 fs_get_obj(ns, node);
2414 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
2415 mutex_destroy(&root_ns->chain_lock);
2419 static struct mlx5_flow_root_namespace
2420 *create_root_ns(struct mlx5_flow_steering *steering,
2421 enum fs_flow_table_type table_type)
2423 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2424 struct mlx5_flow_root_namespace *root_ns;
2425 struct mlx5_flow_namespace *ns;
2427 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2428 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2429 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2431 /* Create the root namespace */
2432 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
2436 root_ns->dev = steering->dev;
2437 root_ns->table_type = table_type;
2438 root_ns->cmds = cmds;
2440 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2443 fs_init_namespace(ns);
2444 mutex_init(&root_ns->chain_lock);
2445 tree_init_node(&ns->node, NULL, del_sw_root_ns);
2446 tree_add_node(&ns->node, NULL);
2451 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2453 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2455 struct fs_prio *prio;
2457 fs_for_each_prio(prio, ns) {
2458 /* This updates prio start_level and num_levels */
2459 set_prio_attrs_in_prio(prio, acc_level);
2460 acc_level += prio->num_levels;
2465 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2467 struct mlx5_flow_namespace *ns;
2468 int acc_level_ns = acc_level;
2470 prio->start_level = acc_level;
2471 fs_for_each_ns(ns, prio) {
2472 /* This updates start_level and num_levels of ns's priority descendants */
2473 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2475 /* If this a prio with chains, and we can jump from one chain
2476 * (namepsace) to another, so we accumulate the levels
2478 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2479 acc_level = acc_level_ns;
2482 if (!prio->num_levels)
2483 prio->num_levels = acc_level_ns - prio->start_level;
2484 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2487 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2489 struct mlx5_flow_namespace *ns = &root_ns->ns;
2490 struct fs_prio *prio;
2491 int start_level = 0;
2493 fs_for_each_prio(prio, ns) {
2494 set_prio_attrs_in_prio(prio, start_level);
2495 start_level += prio->num_levels;
2499 #define ANCHOR_PRIO 0
2500 #define ANCHOR_SIZE 1
2501 #define ANCHOR_LEVEL 0
2502 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2504 struct mlx5_flow_namespace *ns = NULL;
2505 struct mlx5_flow_table_attr ft_attr = {};
2506 struct mlx5_flow_table *ft;
2508 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2512 ft_attr.max_fte = ANCHOR_SIZE;
2513 ft_attr.level = ANCHOR_LEVEL;
2514 ft_attr.prio = ANCHOR_PRIO;
2516 ft = mlx5_create_flow_table(ns, &ft_attr);
2518 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2524 static int init_root_ns(struct mlx5_flow_steering *steering)
2528 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2529 if (!steering->root_ns)
2532 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2536 set_prio_attrs(steering->root_ns);
2537 err = create_anchor_flow_table(steering);
2544 cleanup_root_ns(steering->root_ns);
2545 steering->root_ns = NULL;
2549 static void clean_tree(struct fs_node *node)
2552 struct fs_node *iter;
2553 struct fs_node *temp;
2555 tree_get_node(node);
2556 list_for_each_entry_safe(iter, temp, &node->children, list)
2558 tree_put_node(node, false);
2559 tree_remove_node(node, false);
2563 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2568 clean_tree(&root_ns->ns.node);
2571 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2573 struct mlx5_flow_steering *steering = dev->priv.steering;
2576 if (!steering->esw_egress_root_ns)
2579 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
2580 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2582 kfree(steering->esw_egress_root_ns);
2583 steering->esw_egress_root_ns = NULL;
2586 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2588 struct mlx5_flow_steering *steering = dev->priv.steering;
2591 if (!steering->esw_ingress_root_ns)
2594 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
2595 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2597 kfree(steering->esw_ingress_root_ns);
2598 steering->esw_ingress_root_ns = NULL;
2601 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2603 struct mlx5_flow_steering *steering = dev->priv.steering;
2605 cleanup_root_ns(steering->root_ns);
2606 cleanup_egress_acls_root_ns(dev);
2607 cleanup_ingress_acls_root_ns(dev);
2608 cleanup_root_ns(steering->fdb_root_ns);
2609 steering->fdb_root_ns = NULL;
2610 kfree(steering->fdb_sub_ns);
2611 steering->fdb_sub_ns = NULL;
2612 cleanup_root_ns(steering->sniffer_rx_root_ns);
2613 cleanup_root_ns(steering->sniffer_tx_root_ns);
2614 cleanup_root_ns(steering->rdma_rx_root_ns);
2615 cleanup_root_ns(steering->rdma_tx_root_ns);
2616 cleanup_root_ns(steering->egress_root_ns);
2617 mlx5_cleanup_fc_stats(dev);
2618 kmem_cache_destroy(steering->ftes_cache);
2619 kmem_cache_destroy(steering->fgs_cache);
2623 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2625 struct fs_prio *prio;
2627 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2628 if (!steering->sniffer_tx_root_ns)
2631 /* Create single prio */
2632 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2633 return PTR_ERR_OR_ZERO(prio);
2636 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2638 struct fs_prio *prio;
2640 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2641 if (!steering->sniffer_rx_root_ns)
2644 /* Create single prio */
2645 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2646 return PTR_ERR_OR_ZERO(prio);
2649 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2653 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2654 if (!steering->rdma_rx_root_ns)
2657 err = init_root_tree(steering, &rdma_rx_root_fs,
2658 &steering->rdma_rx_root_ns->ns.node);
2662 set_prio_attrs(steering->rdma_rx_root_ns);
2667 cleanup_root_ns(steering->rdma_rx_root_ns);
2668 steering->rdma_rx_root_ns = NULL;
2672 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
2676 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
2677 if (!steering->rdma_tx_root_ns)
2680 err = init_root_tree(steering, &rdma_tx_root_fs,
2681 &steering->rdma_tx_root_ns->ns.node);
2685 set_prio_attrs(steering->rdma_tx_root_ns);
2690 cleanup_root_ns(steering->rdma_tx_root_ns);
2691 steering->rdma_tx_root_ns = NULL;
2695 /* FT and tc chains are stored in the same array so we can re-use the
2696 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2697 * When creating a new ns for each chain store it in the first available slot.
2698 * Assume tc chains are created and stored first and only then the FT chain.
2700 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2701 struct mlx5_flow_namespace *ns)
2705 while (steering->fdb_sub_ns[chain])
2708 steering->fdb_sub_ns[chain] = ns;
2711 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2712 struct fs_prio *maj_prio)
2714 struct mlx5_flow_namespace *ns;
2715 struct fs_prio *min_prio;
2718 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2722 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2723 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2724 if (IS_ERR(min_prio))
2725 return PTR_ERR(min_prio);
2728 store_fdb_sub_ns_prio_chain(steering, ns);
2733 static int create_fdb_chains(struct mlx5_flow_steering *steering,
2737 struct fs_prio *maj_prio;
2742 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
2743 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2746 if (IS_ERR(maj_prio))
2747 return PTR_ERR(maj_prio);
2749 for (chain = 0; chain < chains; chain++) {
2750 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
2758 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
2762 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
2763 sizeof(*steering->fdb_sub_ns),
2765 if (!steering->fdb_sub_ns)
2768 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
2772 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
2779 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2781 struct fs_prio *maj_prio;
2784 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2785 if (!steering->fdb_root_ns)
2788 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2790 if (IS_ERR(maj_prio)) {
2791 err = PTR_ERR(maj_prio);
2794 err = create_fdb_fast_path(steering);
2798 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
2799 if (IS_ERR(maj_prio)) {
2800 err = PTR_ERR(maj_prio);
2804 /* We put this priority last, knowing that nothing will get here
2805 * unless explicitly forwarded to. This is possible because the
2806 * slow path tables have catch all rules and nothing gets passed
2809 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
2810 if (IS_ERR(maj_prio)) {
2811 err = PTR_ERR(maj_prio);
2815 set_prio_attrs(steering->fdb_root_ns);
2819 cleanup_root_ns(steering->fdb_root_ns);
2820 kfree(steering->fdb_sub_ns);
2821 steering->fdb_sub_ns = NULL;
2822 steering->fdb_root_ns = NULL;
2826 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2828 struct fs_prio *prio;
2830 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2831 if (!steering->esw_egress_root_ns[vport])
2835 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2836 return PTR_ERR_OR_ZERO(prio);
2839 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2841 struct fs_prio *prio;
2843 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2844 if (!steering->esw_ingress_root_ns[vport])
2848 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2849 return PTR_ERR_OR_ZERO(prio);
2852 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2854 struct mlx5_flow_steering *steering = dev->priv.steering;
2855 int total_vports = mlx5_eswitch_get_total_vports(dev);
2859 steering->esw_egress_root_ns =
2860 kcalloc(total_vports,
2861 sizeof(*steering->esw_egress_root_ns),
2863 if (!steering->esw_egress_root_ns)
2866 for (i = 0; i < total_vports; i++) {
2867 err = init_egress_acl_root_ns(steering, i);
2869 goto cleanup_root_ns;
2875 for (i--; i >= 0; i--)
2876 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2877 kfree(steering->esw_egress_root_ns);
2878 steering->esw_egress_root_ns = NULL;
2882 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2884 struct mlx5_flow_steering *steering = dev->priv.steering;
2885 int total_vports = mlx5_eswitch_get_total_vports(dev);
2889 steering->esw_ingress_root_ns =
2890 kcalloc(total_vports,
2891 sizeof(*steering->esw_ingress_root_ns),
2893 if (!steering->esw_ingress_root_ns)
2896 for (i = 0; i < total_vports; i++) {
2897 err = init_ingress_acl_root_ns(steering, i);
2899 goto cleanup_root_ns;
2905 for (i--; i >= 0; i--)
2906 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2907 kfree(steering->esw_ingress_root_ns);
2908 steering->esw_ingress_root_ns = NULL;
2912 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2916 steering->egress_root_ns = create_root_ns(steering,
2918 if (!steering->egress_root_ns)
2921 err = init_root_tree(steering, &egress_root_fs,
2922 &steering->egress_root_ns->ns.node);
2925 set_prio_attrs(steering->egress_root_ns);
2928 cleanup_root_ns(steering->egress_root_ns);
2929 steering->egress_root_ns = NULL;
2933 int mlx5_init_fs(struct mlx5_core_dev *dev)
2935 struct mlx5_flow_steering *steering;
2938 err = mlx5_init_fc_stats(dev);
2942 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2945 steering->dev = dev;
2946 dev->priv.steering = steering;
2948 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2949 sizeof(struct mlx5_flow_group), 0,
2951 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2953 if (!steering->ftes_cache || !steering->fgs_cache) {
2958 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2959 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2960 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2961 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2962 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2963 err = init_root_ns(steering);
2968 if (MLX5_ESWITCH_MANAGER(dev)) {
2969 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2970 err = init_fdb_root_ns(steering);
2974 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2975 err = init_egress_acls_root_ns(dev);
2979 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2980 err = init_ingress_acls_root_ns(dev);
2986 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2987 err = init_sniffer_rx_root_ns(steering);
2992 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2993 err = init_sniffer_tx_root_ns(steering);
2998 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
2999 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
3000 err = init_rdma_rx_root_ns(steering);
3005 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3006 err = init_rdma_tx_root_ns(steering);
3011 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE ||
3012 MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3013 err = init_egress_root_ns(steering);
3020 mlx5_cleanup_fs(dev);
3024 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3026 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3027 struct mlx5_ft_underlay_qp *new_uqp;
3030 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3034 mutex_lock(&root->chain_lock);
3036 if (!root->root_ft) {
3038 goto update_ft_fail;
3041 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3044 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3046 goto update_ft_fail;
3049 new_uqp->qpn = underlay_qpn;
3050 list_add_tail(&new_uqp->list, &root->underlay_qpns);
3052 mutex_unlock(&root->chain_lock);
3057 mutex_unlock(&root->chain_lock);
3061 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
3063 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3065 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3066 struct mlx5_ft_underlay_qp *uqp;
3070 mutex_lock(&root->chain_lock);
3071 list_for_each_entry(uqp, &root->underlay_qpns, list) {
3072 if (uqp->qpn == underlay_qpn) {
3079 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3085 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3088 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3091 list_del(&uqp->list);
3092 mutex_unlock(&root->chain_lock);
3098 mutex_unlock(&root->chain_lock);
3101 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
3103 static struct mlx5_flow_root_namespace
3104 *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
3106 struct mlx5_flow_namespace *ns;
3108 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3109 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3110 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3112 ns = mlx5_get_flow_namespace(dev, ns_type);
3116 return find_root(&ns->node);
3119 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3120 u8 ns_type, u8 num_actions,
3121 void *modify_actions)
3123 struct mlx5_flow_root_namespace *root;
3124 struct mlx5_modify_hdr *modify_hdr;
3127 root = get_root_namespace(dev, ns_type);
3129 return ERR_PTR(-EOPNOTSUPP);
3131 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3133 return ERR_PTR(-ENOMEM);
3135 modify_hdr->ns_type = ns_type;
3136 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3137 modify_actions, modify_hdr);
3140 return ERR_PTR(err);
3145 EXPORT_SYMBOL(mlx5_modify_header_alloc);
3147 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3148 struct mlx5_modify_hdr *modify_hdr)
3150 struct mlx5_flow_root_namespace *root;
3152 root = get_root_namespace(dev, modify_hdr->ns_type);
3155 root->cmds->modify_header_dealloc(root, modify_hdr);
3158 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3160 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3163 void *reformat_data,
3164 enum mlx5_flow_namespace_type ns_type)
3166 struct mlx5_pkt_reformat *pkt_reformat;
3167 struct mlx5_flow_root_namespace *root;
3170 root = get_root_namespace(dev, ns_type);
3172 return ERR_PTR(-EOPNOTSUPP);
3174 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3176 return ERR_PTR(-ENOMEM);
3178 pkt_reformat->ns_type = ns_type;
3179 pkt_reformat->reformat_type = reformat_type;
3180 err = root->cmds->packet_reformat_alloc(root, reformat_type, size,
3181 reformat_data, ns_type,
3184 kfree(pkt_reformat);
3185 return ERR_PTR(err);
3188 return pkt_reformat;
3190 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3192 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3193 struct mlx5_pkt_reformat *pkt_reformat)
3195 struct mlx5_flow_root_namespace *root;
3197 root = get_root_namespace(dev, pkt_reformat->ns_type);
3200 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3201 kfree(pkt_reformat);
3203 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
3205 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3206 struct mlx5_flow_root_namespace *peer_ns)
3208 if (peer_ns && ns->mode != peer_ns->mode) {
3209 mlx5_core_err(ns->dev,
3210 "Can't peer namespace of different steering mode\n");
3214 return ns->cmds->set_peer(ns, peer_ns);
3217 /* This function should be called only at init stage of the namespace.
3218 * It is not safe to call this function while steering operations
3219 * are executed in the namespace.
3221 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3222 enum mlx5_flow_steering_mode mode)
3224 struct mlx5_flow_root_namespace *root;
3225 const struct mlx5_flow_cmds *cmds;
3228 root = find_root(&ns->node);
3229 if (&root->ns != ns)
3230 /* Can't set cmds to non root namespace */
3233 if (root->table_type != FS_FT_FDB)
3236 if (root->mode == mode)
3239 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3240 cmds = mlx5_fs_cmd_get_dr_cmds();
3242 cmds = mlx5_fs_cmd_get_fw_cmds();
3246 err = cmds->create_ns(root);
3248 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3253 root->cmds->destroy_ns(root);