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 "fs_ft_pool.h"
42 #include "diag/fs_tracepoint.h"
43 #include "accel/ipsec.h"
44 #include "fpga/ipsec.h"
46 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
47 sizeof(struct init_tree_node))
49 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50 ...) {.type = FS_TYPE_PRIO,\
51 .min_ft_level = min_level_val,\
52 .num_levels = num_levels_val,\
53 .num_leaf_prios = num_prios_val,\
55 .children = (struct init_tree_node[]) {__VA_ARGS__},\
56 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
59 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
63 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
64 .def_miss_action = def_miss_act,\
65 .children = (struct init_tree_node[]) {__VA_ARGS__},\
66 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
69 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
72 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
74 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
75 .caps = (long[]) {__VA_ARGS__} }
77 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
78 FS_CAP(flow_table_properties_nic_receive.modify_root), \
79 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
80 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
82 #define FS_CHAINING_CAPS_EGRESS \
84 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
85 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
86 FS_CAP(flow_table_properties_nic_transmit \
87 .identified_miss_table_mode), \
88 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
90 #define FS_CHAINING_CAPS_RDMA_TX \
92 FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
93 FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root), \
94 FS_CAP(flow_table_properties_nic_transmit_rdma \
95 .identified_miss_table_mode), \
96 FS_CAP(flow_table_properties_nic_transmit_rdma \
99 #define LEFTOVERS_NUM_LEVELS 1
100 #define LEFTOVERS_NUM_PRIOS 1
102 #define BY_PASS_PRIO_NUM_LEVELS 1
103 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
106 #define ETHTOOL_PRIO_NUM_LEVELS 1
107 #define ETHTOOL_NUM_PRIOS 11
108 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
109 /* Promiscuous, Vlan, mac, ttc, inner ttc, {UDP/ANY/aRFS/accel/{esp, esp_err}} */
110 #define KERNEL_NIC_PRIO_NUM_LEVELS 7
111 #define KERNEL_NIC_NUM_PRIOS 1
112 /* One more level for tc */
113 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
115 #define KERNEL_NIC_TC_NUM_PRIOS 1
116 #define KERNEL_NIC_TC_NUM_LEVELS 2
118 #define ANCHOR_NUM_LEVELS 1
119 #define ANCHOR_NUM_PRIOS 1
120 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
122 #define OFFLOADS_MAX_FT 2
123 #define OFFLOADS_NUM_PRIOS 2
124 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
126 #define LAG_PRIO_NUM_LEVELS 1
127 #define LAG_NUM_PRIOS 1
128 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
130 #define KERNEL_TX_IPSEC_NUM_PRIOS 1
131 #define KERNEL_TX_IPSEC_NUM_LEVELS 1
132 #define KERNEL_TX_MIN_LEVEL (KERNEL_TX_IPSEC_NUM_LEVELS)
139 static struct init_tree_node {
140 enum fs_node_type type;
141 struct init_tree_node *children;
143 struct node_caps caps;
148 enum mlx5_flow_table_miss_action def_miss_action;
150 .type = FS_TYPE_NAMESPACE,
152 .children = (struct init_tree_node[]){
153 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
154 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
155 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
156 BY_PASS_PRIO_NUM_LEVELS))),
157 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
158 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
159 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
160 LAG_PRIO_NUM_LEVELS))),
161 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
162 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
163 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
165 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
166 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
167 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
168 ETHTOOL_PRIO_NUM_LEVELS))),
169 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
170 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
171 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
172 KERNEL_NIC_TC_NUM_LEVELS),
173 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
174 KERNEL_NIC_PRIO_NUM_LEVELS))),
175 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
176 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
177 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
178 LEFTOVERS_NUM_LEVELS))),
179 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
180 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
181 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
182 ANCHOR_NUM_LEVELS))),
186 static struct init_tree_node egress_root_fs = {
187 .type = FS_TYPE_NAMESPACE,
188 #ifdef CONFIG_MLX5_IPSEC
193 .children = (struct init_tree_node[]) {
194 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
195 FS_CHAINING_CAPS_EGRESS,
196 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
197 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
198 BY_PASS_PRIO_NUM_LEVELS))),
199 #ifdef CONFIG_MLX5_IPSEC
200 ADD_PRIO(0, KERNEL_TX_MIN_LEVEL, 0,
201 FS_CHAINING_CAPS_EGRESS,
202 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
203 ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
204 KERNEL_TX_IPSEC_NUM_LEVELS))),
209 #define RDMA_RX_BYPASS_PRIO 0
210 #define RDMA_RX_KERNEL_PRIO 1
211 static struct init_tree_node rdma_rx_root_fs = {
212 .type = FS_TYPE_NAMESPACE,
214 .children = (struct init_tree_node[]) {
215 [RDMA_RX_BYPASS_PRIO] =
216 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0,
218 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
219 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
220 BY_PASS_PRIO_NUM_LEVELS))),
221 [RDMA_RX_KERNEL_PRIO] =
222 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0,
224 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
225 ADD_MULTIPLE_PRIO(1, 1))),
229 static struct init_tree_node rdma_tx_root_fs = {
230 .type = FS_TYPE_NAMESPACE,
232 .children = (struct init_tree_node[]) {
233 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
234 FS_CHAINING_CAPS_RDMA_TX,
235 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
236 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
237 BY_PASS_PRIO_NUM_LEVELS))),
241 enum fs_i_lock_class {
247 static const struct rhashtable_params rhash_fte = {
248 .key_len = sizeof_field(struct fs_fte, val),
249 .key_offset = offsetof(struct fs_fte, val),
250 .head_offset = offsetof(struct fs_fte, hash),
251 .automatic_shrinking = true,
255 static const struct rhashtable_params rhash_fg = {
256 .key_len = sizeof_field(struct mlx5_flow_group, mask),
257 .key_offset = offsetof(struct mlx5_flow_group, mask),
258 .head_offset = offsetof(struct mlx5_flow_group, hash),
259 .automatic_shrinking = true,
264 static void del_hw_flow_table(struct fs_node *node);
265 static void del_hw_flow_group(struct fs_node *node);
266 static void del_hw_fte(struct fs_node *node);
267 static void del_sw_flow_table(struct fs_node *node);
268 static void del_sw_flow_group(struct fs_node *node);
269 static void del_sw_fte(struct fs_node *node);
270 static void del_sw_prio(struct fs_node *node);
271 static void del_sw_ns(struct fs_node *node);
272 /* Delete rule (destination) is special case that
273 * requires to lock the FTE for all the deletion process.
275 static void del_sw_hw_rule(struct fs_node *node);
276 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
277 struct mlx5_flow_destination *d2);
278 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
279 static struct mlx5_flow_rule *
280 find_flow_rule(struct fs_fte *fte,
281 struct mlx5_flow_destination *dest);
283 static void tree_init_node(struct fs_node *node,
284 void (*del_hw_func)(struct fs_node *),
285 void (*del_sw_func)(struct fs_node *))
287 refcount_set(&node->refcount, 1);
288 INIT_LIST_HEAD(&node->list);
289 INIT_LIST_HEAD(&node->children);
290 init_rwsem(&node->lock);
291 node->del_hw_func = del_hw_func;
292 node->del_sw_func = del_sw_func;
293 node->active = false;
296 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
299 refcount_inc(&parent->refcount);
300 node->parent = parent;
302 /* Parent is the root */
306 node->root = parent->root;
309 static int tree_get_node(struct fs_node *node)
311 return refcount_inc_not_zero(&node->refcount);
314 static void nested_down_read_ref_node(struct fs_node *node,
315 enum fs_i_lock_class class)
318 down_read_nested(&node->lock, class);
319 refcount_inc(&node->refcount);
323 static void nested_down_write_ref_node(struct fs_node *node,
324 enum fs_i_lock_class class)
327 down_write_nested(&node->lock, class);
328 refcount_inc(&node->refcount);
332 static void down_write_ref_node(struct fs_node *node, bool locked)
336 down_write(&node->lock);
337 refcount_inc(&node->refcount);
341 static void up_read_ref_node(struct fs_node *node)
343 refcount_dec(&node->refcount);
344 up_read(&node->lock);
347 static void up_write_ref_node(struct fs_node *node, bool locked)
349 refcount_dec(&node->refcount);
351 up_write(&node->lock);
354 static void tree_put_node(struct fs_node *node, bool locked)
356 struct fs_node *parent_node = node->parent;
358 if (refcount_dec_and_test(&node->refcount)) {
359 if (node->del_hw_func)
360 node->del_hw_func(node);
362 down_write_ref_node(parent_node, locked);
363 list_del_init(&node->list);
365 node->del_sw_func(node);
367 up_write_ref_node(parent_node, locked);
370 if (!node && parent_node)
371 tree_put_node(parent_node, locked);
374 static int tree_remove_node(struct fs_node *node, bool locked)
376 if (refcount_read(&node->refcount) > 1) {
377 refcount_dec(&node->refcount);
380 tree_put_node(node, locked);
384 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
387 struct fs_prio *iter_prio;
389 fs_for_each_prio(iter_prio, ns) {
390 if (iter_prio->prio == prio)
397 static bool is_fwd_next_action(u32 action)
399 return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
400 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
403 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
407 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
408 if (spec->match_value[i] & ~spec->match_criteria[i]) {
409 pr_warn("mlx5_core: match_value differs from match_criteria\n");
416 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
418 struct fs_node *root;
419 struct mlx5_flow_namespace *ns;
423 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
424 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
428 ns = container_of(root, struct mlx5_flow_namespace, node);
429 return container_of(ns, struct mlx5_flow_root_namespace, ns);
432 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
434 struct mlx5_flow_root_namespace *root = find_root(node);
437 return root->dev->priv.steering;
441 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
443 struct mlx5_flow_root_namespace *root = find_root(node);
450 static void del_sw_ns(struct fs_node *node)
455 static void del_sw_prio(struct fs_node *node)
460 static void del_hw_flow_table(struct fs_node *node)
462 struct mlx5_flow_root_namespace *root;
463 struct mlx5_flow_table *ft;
464 struct mlx5_core_dev *dev;
467 fs_get_obj(ft, node);
468 dev = get_dev(&ft->node);
469 root = find_root(&ft->node);
470 trace_mlx5_fs_del_ft(ft);
473 err = root->cmds->destroy_flow_table(root, ft);
475 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
479 static void del_sw_flow_table(struct fs_node *node)
481 struct mlx5_flow_table *ft;
482 struct fs_prio *prio;
484 fs_get_obj(ft, node);
486 rhltable_destroy(&ft->fgs_hash);
487 if (ft->node.parent) {
488 fs_get_obj(prio, ft->node.parent);
494 static void modify_fte(struct fs_fte *fte)
496 struct mlx5_flow_root_namespace *root;
497 struct mlx5_flow_table *ft;
498 struct mlx5_flow_group *fg;
499 struct mlx5_core_dev *dev;
502 fs_get_obj(fg, fte->node.parent);
503 fs_get_obj(ft, fg->node.parent);
504 dev = get_dev(&fte->node);
506 root = find_root(&ft->node);
507 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
510 "%s can't del rule fg id=%d fte_index=%d\n",
511 __func__, fg->id, fte->index);
512 fte->modify_mask = 0;
515 static void del_sw_hw_rule(struct fs_node *node)
517 struct mlx5_flow_rule *rule;
520 fs_get_obj(rule, node);
521 fs_get_obj(fte, rule->node.parent);
522 trace_mlx5_fs_del_rule(rule);
523 if (is_fwd_next_action(rule->sw_action)) {
524 mutex_lock(&rule->dest_attr.ft->lock);
525 list_del(&rule->next_ft);
526 mutex_unlock(&rule->dest_attr.ft->lock);
529 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
532 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
533 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
534 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
538 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT &&
540 fte->modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
541 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
545 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
548 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
554 static void del_hw_fte(struct fs_node *node)
556 struct mlx5_flow_root_namespace *root;
557 struct mlx5_flow_table *ft;
558 struct mlx5_flow_group *fg;
559 struct mlx5_core_dev *dev;
563 fs_get_obj(fte, node);
564 fs_get_obj(fg, fte->node.parent);
565 fs_get_obj(ft, fg->node.parent);
567 trace_mlx5_fs_del_fte(fte);
568 dev = get_dev(&ft->node);
569 root = find_root(&ft->node);
571 err = root->cmds->delete_fte(root, ft, fte);
574 "flow steering can't delete fte in index %d of flow group id %d\n",
576 node->active = false;
580 static void del_sw_fte(struct fs_node *node)
582 struct mlx5_flow_steering *steering = get_steering(node);
583 struct mlx5_flow_group *fg;
587 fs_get_obj(fte, node);
588 fs_get_obj(fg, fte->node.parent);
590 err = rhashtable_remove_fast(&fg->ftes_hash,
594 ida_free(&fg->fte_allocator, fte->index - fg->start_index);
595 kmem_cache_free(steering->ftes_cache, fte);
598 static void del_hw_flow_group(struct fs_node *node)
600 struct mlx5_flow_root_namespace *root;
601 struct mlx5_flow_group *fg;
602 struct mlx5_flow_table *ft;
603 struct mlx5_core_dev *dev;
605 fs_get_obj(fg, node);
606 fs_get_obj(ft, fg->node.parent);
607 dev = get_dev(&ft->node);
608 trace_mlx5_fs_del_fg(fg);
610 root = find_root(&ft->node);
611 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
612 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
616 static void del_sw_flow_group(struct fs_node *node)
618 struct mlx5_flow_steering *steering = get_steering(node);
619 struct mlx5_flow_group *fg;
620 struct mlx5_flow_table *ft;
623 fs_get_obj(fg, node);
624 fs_get_obj(ft, fg->node.parent);
626 rhashtable_destroy(&fg->ftes_hash);
627 ida_destroy(&fg->fte_allocator);
628 if (ft->autogroup.active &&
629 fg->max_ftes == ft->autogroup.group_size &&
630 fg->start_index < ft->autogroup.max_fte)
631 ft->autogroup.num_groups--;
632 err = rhltable_remove(&ft->fgs_hash,
636 kmem_cache_free(steering->fgs_cache, fg);
639 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
644 index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
648 fte->index = index + fg->start_index;
649 ret = rhashtable_insert_fast(&fg->ftes_hash,
655 tree_add_node(&fte->node, &fg->node);
656 list_add_tail(&fte->node.list, &fg->node.children);
660 ida_free(&fg->fte_allocator, index);
664 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
665 const struct mlx5_flow_spec *spec,
666 struct mlx5_flow_act *flow_act)
668 struct mlx5_flow_steering *steering = get_steering(&ft->node);
671 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
675 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
676 fte->node.type = FS_TYPE_FLOW_ENTRY;
677 fte->action = *flow_act;
678 fte->flow_context = spec->flow_context;
680 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
685 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
686 struct mlx5_flow_group *fg)
688 rhashtable_destroy(&fg->ftes_hash);
689 kmem_cache_free(steering->fgs_cache, fg);
692 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
693 u8 match_criteria_enable,
694 const void *match_criteria,
698 struct mlx5_flow_group *fg;
701 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
703 return ERR_PTR(-ENOMEM);
705 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
707 kmem_cache_free(steering->fgs_cache, fg);
711 ida_init(&fg->fte_allocator);
712 fg->mask.match_criteria_enable = match_criteria_enable;
713 memcpy(&fg->mask.match_criteria, match_criteria,
714 sizeof(fg->mask.match_criteria));
715 fg->node.type = FS_TYPE_FLOW_GROUP;
716 fg->start_index = start_index;
717 fg->max_ftes = end_index - start_index + 1;
722 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
723 u8 match_criteria_enable,
724 const void *match_criteria,
727 struct list_head *prev)
729 struct mlx5_flow_steering *steering = get_steering(&ft->node);
730 struct mlx5_flow_group *fg;
733 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
734 start_index, end_index);
738 /* initialize refcnt, add to parent list */
739 ret = rhltable_insert(&ft->fgs_hash,
743 dealloc_flow_group(steering, fg);
747 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
748 tree_add_node(&fg->node, &ft->node);
749 /* Add node to group list */
750 list_add(&fg->node.list, prev);
751 atomic_inc(&ft->node.version);
756 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport,
757 enum fs_flow_table_type table_type,
758 enum fs_flow_table_op_mod op_mod,
761 struct mlx5_flow_table *ft;
764 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
766 return ERR_PTR(-ENOMEM);
768 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
775 ft->node.type = FS_TYPE_FLOW_TABLE;
777 ft->type = table_type;
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;
1076 pr_err("mlx5: flow steering failed to find root of namespace\n");
1077 return ERR_PTR(-ENODEV);
1080 mutex_lock(&root->chain_lock);
1081 fs_prio = find_prio(ns, ft_attr->prio);
1087 /* The level is related to the
1088 * priority level range.
1090 if (ft_attr->level >= fs_prio->num_levels) {
1095 ft_attr->level += fs_prio->start_level;
1098 /* The level is related to the
1099 * priority level range.
1101 ft = alloc_flow_table(ft_attr->level,
1104 op_mod, ft_attr->flags);
1110 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1111 next_ft = unmanaged ? ft_attr->next_ft :
1112 find_next_chained_ft(fs_prio);
1113 ft->def_miss_action = ns->def_miss_action;
1115 err = root->cmds->create_flow_table(root, ft, ft_attr->max_fte, next_ft);
1120 err = connect_flow_table(root->dev, ft, fs_prio);
1125 ft->node.active = true;
1126 down_write_ref_node(&fs_prio->node, false);
1128 tree_add_node(&ft->node, &fs_prio->node);
1129 list_add_flow_table(ft, fs_prio);
1131 ft->node.root = fs_prio->node.root;
1134 up_write_ref_node(&fs_prio->node, false);
1135 mutex_unlock(&root->chain_lock);
1136 trace_mlx5_fs_add_ft(ft);
1139 root->cmds->destroy_flow_table(root, ft);
1141 rhltable_destroy(&ft->fgs_hash);
1144 mutex_unlock(&root->chain_lock);
1145 return ERR_PTR(err);
1148 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1149 struct mlx5_flow_table_attr *ft_attr)
1151 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1153 EXPORT_SYMBOL(mlx5_create_flow_table);
1155 struct mlx5_flow_table *
1156 mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1157 struct mlx5_flow_table_attr *ft_attr, u16 vport)
1159 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1162 struct mlx5_flow_table*
1163 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1164 int prio, u32 level)
1166 struct mlx5_flow_table_attr ft_attr = {};
1168 ft_attr.level = level;
1169 ft_attr.prio = prio;
1170 ft_attr.max_fte = 1;
1172 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1174 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1176 struct mlx5_flow_table*
1177 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1178 struct mlx5_flow_table_attr *ft_attr)
1180 int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1181 int max_num_groups = ft_attr->autogroup.max_num_groups;
1182 struct mlx5_flow_table *ft;
1183 int autogroups_max_fte;
1185 ft = mlx5_create_flow_table(ns, ft_attr);
1189 autogroups_max_fte = ft->max_fte - num_reserved_entries;
1190 if (max_num_groups > autogroups_max_fte)
1192 if (num_reserved_entries > ft->max_fte)
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);
1204 mlx5_destroy_flow_table(ft);
1205 return ERR_PTR(-ENOSPC);
1207 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1209 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1212 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1213 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1214 fg_in, match_criteria);
1215 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1217 match_criteria_enable);
1218 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1220 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1222 struct mlx5_flow_group *fg;
1225 if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1226 return ERR_PTR(-EPERM);
1228 down_write_ref_node(&ft->node, false);
1229 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1230 start_index, end_index,
1231 ft->node.children.prev);
1232 up_write_ref_node(&ft->node, false);
1236 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1238 tree_put_node(&fg->node, false);
1239 return ERR_PTR(err);
1241 trace_mlx5_fs_add_fg(fg);
1242 fg->node.active = true;
1246 EXPORT_SYMBOL(mlx5_create_flow_group);
1248 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1250 struct mlx5_flow_rule *rule;
1252 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1256 INIT_LIST_HEAD(&rule->next_ft);
1257 rule->node.type = FS_TYPE_FLOW_DEST;
1259 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1264 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1266 struct mlx5_flow_handle *handle;
1268 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1272 handle->num_rules = num_rules;
1277 static void destroy_flow_handle(struct fs_fte *fte,
1278 struct mlx5_flow_handle *handle,
1279 struct mlx5_flow_destination *dest,
1283 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1285 list_del(&handle->rule[i]->node.list);
1286 kfree(handle->rule[i]);
1292 static struct mlx5_flow_handle *
1293 create_flow_handle(struct fs_fte *fte,
1294 struct mlx5_flow_destination *dest,
1299 struct mlx5_flow_handle *handle;
1300 struct mlx5_flow_rule *rule = NULL;
1301 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1302 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1306 handle = alloc_handle((dest_num) ? dest_num : 1);
1308 return ERR_PTR(-ENOMEM);
1312 rule = find_flow_rule(fte, dest + i);
1314 refcount_inc(&rule->node.refcount);
1320 rule = alloc_rule(dest + i);
1324 /* Add dest to dests list- we need flow tables to be in the
1325 * end of the list for forward to next prio rules.
1327 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1329 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1330 list_add(&rule->node.list, &fte->node.children);
1332 list_add_tail(&rule->node.list, &fte->node.children);
1336 type = dest[i].type ==
1337 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1338 *modify_mask |= type ? count : dst;
1341 handle->rule[i] = rule;
1342 } while (++i < dest_num);
1347 destroy_flow_handle(fte, handle, dest, i);
1348 return ERR_PTR(-ENOMEM);
1351 /* fte should not be deleted while calling this function */
1352 static struct mlx5_flow_handle *
1353 add_rule_fte(struct fs_fte *fte,
1354 struct mlx5_flow_group *fg,
1355 struct mlx5_flow_destination *dest,
1359 struct mlx5_flow_root_namespace *root;
1360 struct mlx5_flow_handle *handle;
1361 struct mlx5_flow_table *ft;
1362 int modify_mask = 0;
1364 bool new_rule = false;
1366 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1368 if (IS_ERR(handle) || !new_rule)
1372 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1374 fs_get_obj(ft, fg->node.parent);
1375 root = find_root(&fg->node);
1376 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1377 err = root->cmds->create_fte(root, ft, fg, fte);
1379 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1383 fte->node.active = true;
1384 fte->status |= FS_FTE_STATUS_EXISTING;
1385 atomic_inc(&fg->node.version);
1391 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1392 return ERR_PTR(err);
1395 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1396 const struct mlx5_flow_spec *spec)
1398 struct list_head *prev = &ft->node.children;
1399 u32 max_fte = ft->autogroup.max_fte;
1400 unsigned int candidate_index = 0;
1401 unsigned int group_size = 0;
1402 struct mlx5_flow_group *fg;
1404 if (!ft->autogroup.active)
1405 return ERR_PTR(-ENOENT);
1407 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1408 group_size = ft->autogroup.group_size;
1410 /* max_fte == ft->autogroup.max_types */
1411 if (group_size == 0)
1414 /* sorted by start_index */
1415 fs_for_each_fg(fg, ft) {
1416 if (candidate_index + group_size > fg->start_index)
1417 candidate_index = fg->start_index + fg->max_ftes;
1420 prev = &fg->node.list;
1423 if (candidate_index + group_size > max_fte)
1424 return ERR_PTR(-ENOSPC);
1426 fg = alloc_insert_flow_group(ft,
1427 spec->match_criteria_enable,
1428 spec->match_criteria,
1430 candidate_index + group_size - 1,
1435 if (group_size == ft->autogroup.group_size)
1436 ft->autogroup.num_groups++;
1442 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1443 struct mlx5_flow_group *fg)
1445 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1446 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1447 void *match_criteria_addr;
1448 u8 src_esw_owner_mask_on;
1453 in = kvzalloc(inlen, GFP_KERNEL);
1457 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1458 fg->mask.match_criteria_enable);
1459 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1460 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1463 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1465 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1466 source_eswitch_owner_vhca_id);
1467 MLX5_SET(create_flow_group_in, in,
1468 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1470 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1471 in, match_criteria);
1472 memcpy(match_criteria_addr, fg->mask.match_criteria,
1473 sizeof(fg->mask.match_criteria));
1475 err = root->cmds->create_flow_group(root, ft, in, fg);
1477 fg->node.active = true;
1478 trace_mlx5_fs_add_fg(fg);
1485 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1486 struct mlx5_flow_destination *d2)
1488 if (d1->type == d2->type) {
1489 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1490 d1->vport.num == d2->vport.num &&
1491 d1->vport.flags == d2->vport.flags &&
1492 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1493 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1494 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1495 (d1->vport.pkt_reformat->id ==
1496 d2->vport.pkt_reformat->id) : true)) ||
1497 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1498 d1->ft == d2->ft) ||
1499 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1500 d1->tir_num == d2->tir_num) ||
1501 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1502 d1->ft_num == d2->ft_num))
1509 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1510 struct mlx5_flow_destination *dest)
1512 struct mlx5_flow_rule *rule;
1514 list_for_each_entry(rule, &fte->node.children, node.list) {
1515 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1521 static bool check_conflicting_actions(u32 action1, u32 action2)
1523 u32 xored_actions = action1 ^ action2;
1525 /* if one rule only wants to count, it's ok */
1526 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1527 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1530 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1531 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1532 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1533 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1534 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1535 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1536 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1537 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1543 static int check_conflicting_ftes(struct fs_fte *fte,
1544 const struct mlx5_flow_context *flow_context,
1545 const struct mlx5_flow_act *flow_act)
1547 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1548 mlx5_core_warn(get_dev(&fte->node),
1549 "Found two FTEs with conflicting actions\n");
1553 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1554 fte->flow_context.flow_tag != flow_context->flow_tag) {
1555 mlx5_core_warn(get_dev(&fte->node),
1556 "FTE flow tag %u already exists with different flow tag %u\n",
1557 fte->flow_context.flow_tag,
1558 flow_context->flow_tag);
1565 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1566 const struct mlx5_flow_spec *spec,
1567 struct mlx5_flow_act *flow_act,
1568 struct mlx5_flow_destination *dest,
1572 struct mlx5_flow_handle *handle;
1577 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1579 return ERR_PTR(ret);
1581 old_action = fte->action.action;
1582 fte->action.action |= flow_act->action;
1583 handle = add_rule_fte(fte, fg, dest, dest_num,
1584 old_action != flow_act->action);
1585 if (IS_ERR(handle)) {
1586 fte->action.action = old_action;
1589 trace_mlx5_fs_set_fte(fte, false);
1591 for (i = 0; i < handle->num_rules; i++) {
1592 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1593 tree_add_node(&handle->rule[i]->node, &fte->node);
1594 trace_mlx5_fs_add_rule(handle->rule[i]);
1600 static bool counter_is_valid(u32 action)
1602 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1603 MLX5_FLOW_CONTEXT_ACTION_ALLOW |
1604 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1607 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1608 struct mlx5_flow_act *flow_act,
1609 struct mlx5_flow_table *ft)
1611 bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
1612 u32 action = flow_act->action;
1614 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1615 return counter_is_valid(action);
1617 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1621 if (ft->type != FS_FT_FDB &&
1622 ft->type != FS_FT_NIC_RX)
1625 if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1626 ft->type != dest->ft->type)
1630 if (!dest || ((dest->type ==
1631 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1632 (dest->ft->level <= ft->level && !ignore_level)))
1638 struct list_head list;
1639 struct mlx5_flow_group *g;
1642 static void free_match_list(struct match_list *head, bool ft_locked)
1644 struct match_list *iter, *match_tmp;
1646 list_for_each_entry_safe(iter, match_tmp, &head->list,
1648 tree_put_node(&iter->g->node, ft_locked);
1649 list_del(&iter->list);
1654 static int build_match_list(struct match_list *match_head,
1655 struct mlx5_flow_table *ft,
1656 const struct mlx5_flow_spec *spec,
1659 struct rhlist_head *tmp, *list;
1660 struct mlx5_flow_group *g;
1664 INIT_LIST_HEAD(&match_head->list);
1665 /* Collect all fgs which has a matching match_criteria */
1666 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1667 /* RCU is atomic, we can't execute FW commands here */
1668 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1669 struct match_list *curr_match;
1671 if (unlikely(!tree_get_node(&g->node)))
1674 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1676 free_match_list(match_head, ft_locked);
1681 list_add_tail(&curr_match->list, &match_head->list);
1688 static u64 matched_fgs_get_version(struct list_head *match_head)
1690 struct match_list *iter;
1693 list_for_each_entry(iter, match_head, list)
1694 version += (u64)atomic_read(&iter->g->node.version);
1698 static struct fs_fte *
1699 lookup_fte_locked(struct mlx5_flow_group *g,
1700 const u32 *match_value,
1703 struct fs_fte *fte_tmp;
1706 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1708 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1709 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1711 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1715 if (!fte_tmp->node.active) {
1716 tree_put_node(&fte_tmp->node, false);
1721 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1724 up_write_ref_node(&g->node, false);
1726 up_read_ref_node(&g->node);
1730 static struct mlx5_flow_handle *
1731 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1732 struct list_head *match_head,
1733 const struct mlx5_flow_spec *spec,
1734 struct mlx5_flow_act *flow_act,
1735 struct mlx5_flow_destination *dest,
1739 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1740 struct mlx5_flow_group *g;
1741 struct mlx5_flow_handle *rule;
1742 struct match_list *iter;
1743 bool take_write = false;
1748 fte = alloc_fte(ft, spec, flow_act);
1750 return ERR_PTR(-ENOMEM);
1752 search_again_locked:
1753 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1755 version = matched_fgs_get_version(match_head);
1756 /* Try to find an fte with identical match value and attempt update its
1759 list_for_each_entry(iter, match_head, list) {
1760 struct fs_fte *fte_tmp;
1763 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1766 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
1767 /* No error check needed here, because insert_fte() is not called */
1768 up_write_ref_node(&fte_tmp->node, false);
1769 tree_put_node(&fte_tmp->node, false);
1770 kmem_cache_free(steering->ftes_cache, fte);
1775 /* No group with matching fte found, or we skipped the search.
1776 * Try to add a new fte to any matching fg.
1779 /* Check the ft version, for case that new flow group
1780 * was added while the fgs weren't locked
1782 if (atomic_read(&ft->node.version) != ft_version) {
1783 rule = ERR_PTR(-EAGAIN);
1787 /* Check the fgs version. If version have changed it could be that an
1788 * FTE with the same match value was added while the fgs weren't
1791 if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
1792 version != matched_fgs_get_version(match_head)) {
1794 goto search_again_locked;
1797 list_for_each_entry(iter, match_head, list) {
1800 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1802 if (!g->node.active) {
1803 up_write_ref_node(&g->node, false);
1807 err = insert_fte(g, fte);
1809 up_write_ref_node(&g->node, false);
1812 kmem_cache_free(steering->ftes_cache, fte);
1813 return ERR_PTR(err);
1816 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1817 up_write_ref_node(&g->node, false);
1818 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1819 up_write_ref_node(&fte->node, false);
1821 tree_put_node(&fte->node, false);
1824 rule = ERR_PTR(-ENOENT);
1826 kmem_cache_free(steering->ftes_cache, fte);
1830 static struct mlx5_flow_handle *
1831 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1832 const struct mlx5_flow_spec *spec,
1833 struct mlx5_flow_act *flow_act,
1834 struct mlx5_flow_destination *dest,
1838 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1839 struct mlx5_flow_handle *rule;
1840 struct match_list match_head;
1841 struct mlx5_flow_group *g;
1842 bool take_write = false;
1848 if (!check_valid_spec(spec))
1849 return ERR_PTR(-EINVAL);
1851 for (i = 0; i < dest_num; i++) {
1852 if (!dest_is_valid(&dest[i], flow_act, ft))
1853 return ERR_PTR(-EINVAL);
1855 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1856 search_again_locked:
1857 version = atomic_read(&ft->node.version);
1859 /* Collect all fgs which has a matching match_criteria */
1860 err = build_match_list(&match_head, ft, spec, take_write);
1863 up_write_ref_node(&ft->node, false);
1865 up_read_ref_node(&ft->node);
1866 return ERR_PTR(err);
1870 up_read_ref_node(&ft->node);
1872 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1874 free_match_list(&match_head, take_write);
1875 if (!IS_ERR(rule) ||
1876 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1878 up_write_ref_node(&ft->node, false);
1883 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1887 if (PTR_ERR(rule) == -EAGAIN ||
1888 version != atomic_read(&ft->node.version))
1889 goto search_again_locked;
1891 g = alloc_auto_flow_group(ft, spec);
1894 up_write_ref_node(&ft->node, false);
1898 fte = alloc_fte(ft, spec, flow_act);
1900 up_write_ref_node(&ft->node, false);
1905 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1906 up_write_ref_node(&ft->node, false);
1908 err = create_auto_flow_group(ft, g);
1910 goto err_release_fg;
1912 err = insert_fte(g, fte);
1914 goto err_release_fg;
1916 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1917 up_write_ref_node(&g->node, false);
1918 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1919 up_write_ref_node(&fte->node, false);
1921 tree_put_node(&fte->node, false);
1922 tree_put_node(&g->node, false);
1926 up_write_ref_node(&g->node, false);
1927 kmem_cache_free(steering->ftes_cache, fte);
1929 tree_put_node(&g->node, false);
1930 return ERR_PTR(err);
1933 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1935 return ((ft->type == FS_FT_NIC_RX) &&
1936 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1939 struct mlx5_flow_handle *
1940 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1941 const struct mlx5_flow_spec *spec,
1942 struct mlx5_flow_act *flow_act,
1943 struct mlx5_flow_destination *dest,
1946 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1947 static const struct mlx5_flow_spec zero_spec = {};
1948 struct mlx5_flow_destination *gen_dest = NULL;
1949 struct mlx5_flow_table *next_ft = NULL;
1950 struct mlx5_flow_handle *handle = NULL;
1951 u32 sw_action = flow_act->action;
1957 if (!is_fwd_next_action(sw_action))
1958 return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1960 if (!fwd_next_prio_supported(ft))
1961 return ERR_PTR(-EOPNOTSUPP);
1963 mutex_lock(&root->chain_lock);
1964 next_ft = find_next_fwd_ft(ft, flow_act);
1966 handle = ERR_PTR(-EOPNOTSUPP);
1970 gen_dest = kcalloc(num_dest + 1, sizeof(*dest),
1973 handle = ERR_PTR(-ENOMEM);
1976 for (i = 0; i < num_dest; i++)
1977 gen_dest[i] = dest[i];
1979 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1980 gen_dest[i].ft = next_ft;
1983 flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
1984 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
1985 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1986 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1990 if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
1991 mutex_lock(&next_ft->lock);
1992 list_add(&handle->rule[num_dest - 1]->next_ft,
1993 &next_ft->fwd_rules);
1994 mutex_unlock(&next_ft->lock);
1995 handle->rule[num_dest - 1]->sw_action = sw_action;
1996 handle->rule[num_dest - 1]->ft = ft;
1999 mutex_unlock(&root->chain_lock);
2003 EXPORT_SYMBOL(mlx5_add_flow_rules);
2005 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
2010 /* In order to consolidate the HW changes we lock the FTE for other
2011 * changes, and increase its refcount, in order not to perform the
2012 * "del" functions of the FTE. Will handle them here.
2013 * The removal of the rules is done under locked FTE.
2014 * After removing all the handle's rules, if there are remaining
2015 * rules, it means we just need to modify the FTE in FW, and
2016 * unlock/decrease the refcount we increased before.
2017 * Otherwise, it means the FTE should be deleted. First delete the
2018 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2019 * the FTE, which will handle the last decrease of the refcount, as
2020 * well as required handling of its parent.
2022 fs_get_obj(fte, handle->rule[0]->node.parent);
2023 down_write_ref_node(&fte->node, false);
2024 for (i = handle->num_rules - 1; i >= 0; i--)
2025 tree_remove_node(&handle->rule[i]->node, true);
2026 if (fte->dests_size) {
2027 if (fte->modify_mask)
2029 up_write_ref_node(&fte->node, false);
2030 } else if (list_empty(&fte->node.children)) {
2031 del_hw_fte(&fte->node);
2032 /* Avoid double call to del_hw_fte */
2033 fte->node.del_hw_func = NULL;
2034 up_write_ref_node(&fte->node, false);
2035 tree_put_node(&fte->node, false);
2039 EXPORT_SYMBOL(mlx5_del_flow_rules);
2041 /* Assuming prio->node.children(flow tables) is sorted by level */
2042 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2044 struct fs_prio *prio;
2046 fs_get_obj(prio, ft->node.parent);
2048 if (!list_is_last(&ft->node.list, &prio->node.children))
2049 return list_next_entry(ft, node.list);
2050 return find_next_chained_ft(prio);
2053 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2055 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2056 struct mlx5_ft_underlay_qp *uqp;
2057 struct mlx5_flow_table *new_root_ft = NULL;
2061 if (root->root_ft != ft)
2064 new_root_ft = find_next_ft(ft);
2066 root->root_ft = NULL;
2070 if (list_empty(&root->underlay_qpns)) {
2071 /* Don't set any QPN (zero) in case QPN list is empty */
2073 err = root->cmds->update_root_ft(root, new_root_ft,
2076 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2078 err = root->cmds->update_root_ft(root,
2087 mlx5_core_warn(root->dev,
2088 "Update root flow table of id(%u) qpn(%d) failed\n",
2091 root->root_ft = new_root_ft;
2096 /* Connect flow table from previous priority to
2097 * the next flow table.
2099 static int disconnect_flow_table(struct mlx5_flow_table *ft)
2101 struct mlx5_core_dev *dev = get_dev(&ft->node);
2102 struct mlx5_flow_table *next_ft;
2103 struct fs_prio *prio;
2106 err = update_root_ft_destroy(ft);
2110 fs_get_obj(prio, ft->node.parent);
2111 if (!(list_first_entry(&prio->node.children,
2112 struct mlx5_flow_table,
2116 next_ft = find_next_chained_ft(prio);
2117 err = connect_fwd_rules(dev, next_ft, ft);
2121 err = connect_prev_fts(dev, next_ft, prio);
2123 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2128 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2130 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2133 mutex_lock(&root->chain_lock);
2134 if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2135 err = disconnect_flow_table(ft);
2137 mutex_unlock(&root->chain_lock);
2140 if (tree_remove_node(&ft->node, false))
2141 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2143 mutex_unlock(&root->chain_lock);
2147 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2149 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2151 if (tree_remove_node(&fg->node, false))
2152 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2155 EXPORT_SYMBOL(mlx5_destroy_flow_group);
2157 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2160 struct mlx5_flow_steering *steering = dev->priv.steering;
2162 if (!steering || !steering->fdb_sub_ns)
2165 return steering->fdb_sub_ns[n];
2167 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2169 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2170 enum mlx5_flow_namespace_type type)
2172 struct mlx5_flow_steering *steering = dev->priv.steering;
2173 struct mlx5_flow_root_namespace *root_ns;
2175 struct fs_prio *fs_prio;
2176 struct mlx5_flow_namespace *ns;
2182 case MLX5_FLOW_NAMESPACE_FDB:
2183 if (steering->fdb_root_ns)
2184 return &steering->fdb_root_ns->ns;
2186 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2187 if (steering->sniffer_rx_root_ns)
2188 return &steering->sniffer_rx_root_ns->ns;
2190 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2191 if (steering->sniffer_tx_root_ns)
2192 return &steering->sniffer_tx_root_ns->ns;
2198 if (type == MLX5_FLOW_NAMESPACE_EGRESS ||
2199 type == MLX5_FLOW_NAMESPACE_EGRESS_KERNEL) {
2200 root_ns = steering->egress_root_ns;
2201 prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
2202 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2203 root_ns = steering->rdma_rx_root_ns;
2204 prio = RDMA_RX_BYPASS_PRIO;
2205 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2206 root_ns = steering->rdma_rx_root_ns;
2207 prio = RDMA_RX_KERNEL_PRIO;
2208 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
2209 root_ns = steering->rdma_tx_root_ns;
2210 } else { /* Must be NIC RX */
2211 root_ns = steering->root_ns;
2218 fs_prio = find_prio(&root_ns->ns, prio);
2222 ns = list_first_entry(&fs_prio->node.children,
2228 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2230 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2231 enum mlx5_flow_namespace_type type,
2234 struct mlx5_flow_steering *steering = dev->priv.steering;
2240 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2241 if (vport >= steering->esw_egress_acl_vports)
2243 if (steering->esw_egress_root_ns &&
2244 steering->esw_egress_root_ns[vport])
2245 return &steering->esw_egress_root_ns[vport]->ns;
2248 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2249 if (vport >= steering->esw_ingress_acl_vports)
2251 if (steering->esw_ingress_root_ns &&
2252 steering->esw_ingress_root_ns[vport])
2253 return &steering->esw_ingress_root_ns[vport]->ns;
2261 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2264 enum fs_node_type type)
2266 struct fs_prio *fs_prio;
2268 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2270 return ERR_PTR(-ENOMEM);
2272 fs_prio->node.type = type;
2273 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2274 tree_add_node(&fs_prio->node, &ns->node);
2275 fs_prio->num_levels = num_levels;
2276 fs_prio->prio = prio;
2277 list_add_tail(&fs_prio->node.list, &ns->node.children);
2282 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2286 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2289 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2290 unsigned int prio, int num_levels)
2292 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2295 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2298 ns->node.type = FS_TYPE_NAMESPACE;
2303 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2306 struct mlx5_flow_namespace *ns;
2308 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2310 return ERR_PTR(-ENOMEM);
2312 fs_init_namespace(ns);
2313 ns->def_miss_action = def_miss_act;
2314 tree_init_node(&ns->node, NULL, del_sw_ns);
2315 tree_add_node(&ns->node, &prio->node);
2316 list_add_tail(&ns->node.list, &prio->node.children);
2321 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2322 struct init_tree_node *prio_metadata)
2324 struct fs_prio *fs_prio;
2327 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2328 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2329 if (IS_ERR(fs_prio))
2330 return PTR_ERR(fs_prio);
2335 #define FLOW_TABLE_BIT_SZ 1
2336 #define GET_FLOW_TABLE_CAP(dev, offset) \
2337 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2339 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2340 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2344 for (i = 0; i < caps->arr_sz; i++) {
2345 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2351 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2352 struct init_tree_node *init_node,
2353 struct fs_node *fs_parent_node,
2354 struct init_tree_node *init_parent_node,
2357 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2358 flow_table_properties_nic_receive.
2360 struct mlx5_flow_namespace *fs_ns;
2361 struct fs_prio *fs_prio;
2362 struct fs_node *base;
2366 if (init_node->type == FS_TYPE_PRIO) {
2367 if ((init_node->min_ft_level > max_ft_level) ||
2368 !has_required_caps(steering->dev, &init_node->caps))
2371 fs_get_obj(fs_ns, fs_parent_node);
2372 if (init_node->num_leaf_prios)
2373 return create_leaf_prios(fs_ns, prio, init_node);
2374 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2375 if (IS_ERR(fs_prio))
2376 return PTR_ERR(fs_prio);
2377 base = &fs_prio->node;
2378 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2379 fs_get_obj(fs_prio, fs_parent_node);
2380 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2382 return PTR_ERR(fs_ns);
2383 base = &fs_ns->node;
2388 for (i = 0; i < init_node->ar_size; i++) {
2389 err = init_root_tree_recursive(steering, &init_node->children[i],
2390 base, init_node, prio);
2393 if (init_node->children[i].type == FS_TYPE_PRIO &&
2394 init_node->children[i].num_leaf_prios) {
2395 prio += init_node->children[i].num_leaf_prios;
2402 static int init_root_tree(struct mlx5_flow_steering *steering,
2403 struct init_tree_node *init_node,
2404 struct fs_node *fs_parent_node)
2409 for (i = 0; i < init_node->ar_size; i++) {
2410 err = init_root_tree_recursive(steering, &init_node->children[i],
2419 static void del_sw_root_ns(struct fs_node *node)
2421 struct mlx5_flow_root_namespace *root_ns;
2422 struct mlx5_flow_namespace *ns;
2424 fs_get_obj(ns, node);
2425 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
2426 mutex_destroy(&root_ns->chain_lock);
2430 static struct mlx5_flow_root_namespace
2431 *create_root_ns(struct mlx5_flow_steering *steering,
2432 enum fs_flow_table_type table_type)
2434 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2435 struct mlx5_flow_root_namespace *root_ns;
2436 struct mlx5_flow_namespace *ns;
2438 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2439 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2440 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2442 /* Create the root namespace */
2443 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
2447 root_ns->dev = steering->dev;
2448 root_ns->table_type = table_type;
2449 root_ns->cmds = cmds;
2451 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2454 fs_init_namespace(ns);
2455 mutex_init(&root_ns->chain_lock);
2456 tree_init_node(&ns->node, NULL, del_sw_root_ns);
2457 tree_add_node(&ns->node, NULL);
2462 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2464 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2466 struct fs_prio *prio;
2468 fs_for_each_prio(prio, ns) {
2469 /* This updates prio start_level and num_levels */
2470 set_prio_attrs_in_prio(prio, acc_level);
2471 acc_level += prio->num_levels;
2476 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2478 struct mlx5_flow_namespace *ns;
2479 int acc_level_ns = acc_level;
2481 prio->start_level = acc_level;
2482 fs_for_each_ns(ns, prio) {
2483 /* This updates start_level and num_levels of ns's priority descendants */
2484 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2486 /* If this a prio with chains, and we can jump from one chain
2487 * (namepsace) to another, so we accumulate the levels
2489 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2490 acc_level = acc_level_ns;
2493 if (!prio->num_levels)
2494 prio->num_levels = acc_level_ns - prio->start_level;
2495 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2498 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2500 struct mlx5_flow_namespace *ns = &root_ns->ns;
2501 struct fs_prio *prio;
2502 int start_level = 0;
2504 fs_for_each_prio(prio, ns) {
2505 set_prio_attrs_in_prio(prio, start_level);
2506 start_level += prio->num_levels;
2510 #define ANCHOR_PRIO 0
2511 #define ANCHOR_SIZE 1
2512 #define ANCHOR_LEVEL 0
2513 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2515 struct mlx5_flow_namespace *ns = NULL;
2516 struct mlx5_flow_table_attr ft_attr = {};
2517 struct mlx5_flow_table *ft;
2519 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2523 ft_attr.max_fte = ANCHOR_SIZE;
2524 ft_attr.level = ANCHOR_LEVEL;
2525 ft_attr.prio = ANCHOR_PRIO;
2527 ft = mlx5_create_flow_table(ns, &ft_attr);
2529 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2535 static int init_root_ns(struct mlx5_flow_steering *steering)
2539 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2540 if (!steering->root_ns)
2543 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2547 set_prio_attrs(steering->root_ns);
2548 err = create_anchor_flow_table(steering);
2555 cleanup_root_ns(steering->root_ns);
2556 steering->root_ns = NULL;
2560 static void clean_tree(struct fs_node *node)
2563 struct fs_node *iter;
2564 struct fs_node *temp;
2566 tree_get_node(node);
2567 list_for_each_entry_safe(iter, temp, &node->children, list)
2569 tree_put_node(node, false);
2570 tree_remove_node(node, false);
2574 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2579 clean_tree(&root_ns->ns.node);
2582 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2584 struct mlx5_flow_steering *steering = dev->priv.steering;
2586 cleanup_root_ns(steering->root_ns);
2587 cleanup_root_ns(steering->fdb_root_ns);
2588 steering->fdb_root_ns = NULL;
2589 kfree(steering->fdb_sub_ns);
2590 steering->fdb_sub_ns = NULL;
2591 cleanup_root_ns(steering->sniffer_rx_root_ns);
2592 cleanup_root_ns(steering->sniffer_tx_root_ns);
2593 cleanup_root_ns(steering->rdma_rx_root_ns);
2594 cleanup_root_ns(steering->rdma_tx_root_ns);
2595 cleanup_root_ns(steering->egress_root_ns);
2596 mlx5_cleanup_fc_stats(dev);
2597 kmem_cache_destroy(steering->ftes_cache);
2598 kmem_cache_destroy(steering->fgs_cache);
2599 mlx5_ft_pool_destroy(dev);
2603 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2605 struct fs_prio *prio;
2607 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2608 if (!steering->sniffer_tx_root_ns)
2611 /* Create single prio */
2612 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2613 return PTR_ERR_OR_ZERO(prio);
2616 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2618 struct fs_prio *prio;
2620 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2621 if (!steering->sniffer_rx_root_ns)
2624 /* Create single prio */
2625 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2626 return PTR_ERR_OR_ZERO(prio);
2629 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2633 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2634 if (!steering->rdma_rx_root_ns)
2637 err = init_root_tree(steering, &rdma_rx_root_fs,
2638 &steering->rdma_rx_root_ns->ns.node);
2642 set_prio_attrs(steering->rdma_rx_root_ns);
2647 cleanup_root_ns(steering->rdma_rx_root_ns);
2648 steering->rdma_rx_root_ns = NULL;
2652 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
2656 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
2657 if (!steering->rdma_tx_root_ns)
2660 err = init_root_tree(steering, &rdma_tx_root_fs,
2661 &steering->rdma_tx_root_ns->ns.node);
2665 set_prio_attrs(steering->rdma_tx_root_ns);
2670 cleanup_root_ns(steering->rdma_tx_root_ns);
2671 steering->rdma_tx_root_ns = NULL;
2675 /* FT and tc chains are stored in the same array so we can re-use the
2676 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2677 * When creating a new ns for each chain store it in the first available slot.
2678 * Assume tc chains are created and stored first and only then the FT chain.
2680 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2681 struct mlx5_flow_namespace *ns)
2685 while (steering->fdb_sub_ns[chain])
2688 steering->fdb_sub_ns[chain] = ns;
2691 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2692 struct fs_prio *maj_prio)
2694 struct mlx5_flow_namespace *ns;
2695 struct fs_prio *min_prio;
2698 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2702 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2703 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2704 if (IS_ERR(min_prio))
2705 return PTR_ERR(min_prio);
2708 store_fdb_sub_ns_prio_chain(steering, ns);
2713 static int create_fdb_chains(struct mlx5_flow_steering *steering,
2717 struct fs_prio *maj_prio;
2722 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
2723 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2726 if (IS_ERR(maj_prio))
2727 return PTR_ERR(maj_prio);
2729 for (chain = 0; chain < chains; chain++) {
2730 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
2738 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
2742 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
2743 sizeof(*steering->fdb_sub_ns),
2745 if (!steering->fdb_sub_ns)
2748 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
2752 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
2759 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2761 struct fs_prio *maj_prio;
2764 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2765 if (!steering->fdb_root_ns)
2768 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2770 if (IS_ERR(maj_prio)) {
2771 err = PTR_ERR(maj_prio);
2774 err = create_fdb_fast_path(steering);
2778 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
2779 if (IS_ERR(maj_prio)) {
2780 err = PTR_ERR(maj_prio);
2784 /* We put this priority last, knowing that nothing will get here
2785 * unless explicitly forwarded to. This is possible because the
2786 * slow path tables have catch all rules and nothing gets passed
2789 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
2790 if (IS_ERR(maj_prio)) {
2791 err = PTR_ERR(maj_prio);
2795 set_prio_attrs(steering->fdb_root_ns);
2799 cleanup_root_ns(steering->fdb_root_ns);
2800 kfree(steering->fdb_sub_ns);
2801 steering->fdb_sub_ns = NULL;
2802 steering->fdb_root_ns = NULL;
2806 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2808 struct fs_prio *prio;
2810 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2811 if (!steering->esw_egress_root_ns[vport])
2815 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2816 return PTR_ERR_OR_ZERO(prio);
2819 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2821 struct fs_prio *prio;
2823 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2824 if (!steering->esw_ingress_root_ns[vport])
2828 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2829 return PTR_ERR_OR_ZERO(prio);
2832 int mlx5_fs_egress_acls_init(struct mlx5_core_dev *dev, int total_vports)
2834 struct mlx5_flow_steering *steering = dev->priv.steering;
2838 steering->esw_egress_root_ns =
2839 kcalloc(total_vports,
2840 sizeof(*steering->esw_egress_root_ns),
2842 if (!steering->esw_egress_root_ns)
2845 for (i = 0; i < total_vports; i++) {
2846 err = init_egress_acl_root_ns(steering, i);
2848 goto cleanup_root_ns;
2850 steering->esw_egress_acl_vports = total_vports;
2854 for (i--; i >= 0; i--)
2855 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2856 kfree(steering->esw_egress_root_ns);
2857 steering->esw_egress_root_ns = NULL;
2861 void mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev *dev)
2863 struct mlx5_flow_steering *steering = dev->priv.steering;
2866 if (!steering->esw_egress_root_ns)
2869 for (i = 0; i < steering->esw_egress_acl_vports; i++)
2870 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2872 kfree(steering->esw_egress_root_ns);
2873 steering->esw_egress_root_ns = NULL;
2876 int mlx5_fs_ingress_acls_init(struct mlx5_core_dev *dev, int total_vports)
2878 struct mlx5_flow_steering *steering = dev->priv.steering;
2882 steering->esw_ingress_root_ns =
2883 kcalloc(total_vports,
2884 sizeof(*steering->esw_ingress_root_ns),
2886 if (!steering->esw_ingress_root_ns)
2889 for (i = 0; i < total_vports; i++) {
2890 err = init_ingress_acl_root_ns(steering, i);
2892 goto cleanup_root_ns;
2894 steering->esw_ingress_acl_vports = total_vports;
2898 for (i--; i >= 0; i--)
2899 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2900 kfree(steering->esw_ingress_root_ns);
2901 steering->esw_ingress_root_ns = NULL;
2905 void mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev *dev)
2907 struct mlx5_flow_steering *steering = dev->priv.steering;
2910 if (!steering->esw_ingress_root_ns)
2913 for (i = 0; i < steering->esw_ingress_acl_vports; i++)
2914 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2916 kfree(steering->esw_ingress_root_ns);
2917 steering->esw_ingress_root_ns = NULL;
2920 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2924 steering->egress_root_ns = create_root_ns(steering,
2926 if (!steering->egress_root_ns)
2929 err = init_root_tree(steering, &egress_root_fs,
2930 &steering->egress_root_ns->ns.node);
2933 set_prio_attrs(steering->egress_root_ns);
2936 cleanup_root_ns(steering->egress_root_ns);
2937 steering->egress_root_ns = NULL;
2941 int mlx5_init_fs(struct mlx5_core_dev *dev)
2943 struct mlx5_flow_steering *steering;
2946 err = mlx5_init_fc_stats(dev);
2950 err = mlx5_ft_pool_init(dev);
2954 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2957 steering->dev = dev;
2958 dev->priv.steering = steering;
2960 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2961 sizeof(struct mlx5_flow_group), 0,
2963 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2965 if (!steering->ftes_cache || !steering->fgs_cache) {
2970 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2971 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2972 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2973 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2974 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2975 err = init_root_ns(steering);
2980 if (MLX5_ESWITCH_MANAGER(dev)) {
2981 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2982 err = init_fdb_root_ns(steering);
2988 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2989 err = init_sniffer_rx_root_ns(steering);
2994 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2995 err = init_sniffer_tx_root_ns(steering);
3000 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
3001 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
3002 err = init_rdma_rx_root_ns(steering);
3007 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3008 err = init_rdma_tx_root_ns(steering);
3013 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE ||
3014 MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3015 err = init_egress_root_ns(steering);
3022 mlx5_cleanup_fs(dev);
3026 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3028 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3029 struct mlx5_ft_underlay_qp *new_uqp;
3032 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3036 mutex_lock(&root->chain_lock);
3038 if (!root->root_ft) {
3040 goto update_ft_fail;
3043 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3046 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3048 goto update_ft_fail;
3051 new_uqp->qpn = underlay_qpn;
3052 list_add_tail(&new_uqp->list, &root->underlay_qpns);
3054 mutex_unlock(&root->chain_lock);
3059 mutex_unlock(&root->chain_lock);
3063 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
3065 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3067 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3068 struct mlx5_ft_underlay_qp *uqp;
3072 mutex_lock(&root->chain_lock);
3073 list_for_each_entry(uqp, &root->underlay_qpns, list) {
3074 if (uqp->qpn == underlay_qpn) {
3081 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3087 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3090 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3093 list_del(&uqp->list);
3094 mutex_unlock(&root->chain_lock);
3100 mutex_unlock(&root->chain_lock);
3103 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
3105 static struct mlx5_flow_root_namespace
3106 *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
3108 struct mlx5_flow_namespace *ns;
3110 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3111 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3112 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3114 ns = mlx5_get_flow_namespace(dev, ns_type);
3118 return find_root(&ns->node);
3121 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3122 u8 ns_type, u8 num_actions,
3123 void *modify_actions)
3125 struct mlx5_flow_root_namespace *root;
3126 struct mlx5_modify_hdr *modify_hdr;
3129 root = get_root_namespace(dev, ns_type);
3131 return ERR_PTR(-EOPNOTSUPP);
3133 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3135 return ERR_PTR(-ENOMEM);
3137 modify_hdr->ns_type = ns_type;
3138 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3139 modify_actions, modify_hdr);
3142 return ERR_PTR(err);
3147 EXPORT_SYMBOL(mlx5_modify_header_alloc);
3149 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3150 struct mlx5_modify_hdr *modify_hdr)
3152 struct mlx5_flow_root_namespace *root;
3154 root = get_root_namespace(dev, modify_hdr->ns_type);
3157 root->cmds->modify_header_dealloc(root, modify_hdr);
3160 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3162 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3165 void *reformat_data,
3166 enum mlx5_flow_namespace_type ns_type)
3168 struct mlx5_pkt_reformat *pkt_reformat;
3169 struct mlx5_flow_root_namespace *root;
3172 root = get_root_namespace(dev, ns_type);
3174 return ERR_PTR(-EOPNOTSUPP);
3176 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3178 return ERR_PTR(-ENOMEM);
3180 pkt_reformat->ns_type = ns_type;
3181 pkt_reformat->reformat_type = reformat_type;
3182 err = root->cmds->packet_reformat_alloc(root, reformat_type, size,
3183 reformat_data, ns_type,
3186 kfree(pkt_reformat);
3187 return ERR_PTR(err);
3190 return pkt_reformat;
3192 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3194 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3195 struct mlx5_pkt_reformat *pkt_reformat)
3197 struct mlx5_flow_root_namespace *root;
3199 root = get_root_namespace(dev, pkt_reformat->ns_type);
3202 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3203 kfree(pkt_reformat);
3205 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
3207 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3208 struct mlx5_flow_root_namespace *peer_ns)
3210 if (peer_ns && ns->mode != peer_ns->mode) {
3211 mlx5_core_err(ns->dev,
3212 "Can't peer namespace of different steering mode\n");
3216 return ns->cmds->set_peer(ns, peer_ns);
3219 /* This function should be called only at init stage of the namespace.
3220 * It is not safe to call this function while steering operations
3221 * are executed in the namespace.
3223 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3224 enum mlx5_flow_steering_mode mode)
3226 struct mlx5_flow_root_namespace *root;
3227 const struct mlx5_flow_cmds *cmds;
3230 root = find_root(&ns->node);
3231 if (&root->ns != ns)
3232 /* Can't set cmds to non root namespace */
3235 if (root->table_type != FS_FT_FDB)
3238 if (root->mode == mode)
3241 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3242 cmds = mlx5_fs_cmd_get_dr_cmds();
3244 cmds = mlx5_fs_cmd_get_fw_cmds();
3248 err = cmds->create_ns(root);
3250 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3255 root->cmds->destroy_ns(root);