1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2020 Mellanox Technologies.
4 #include <linux/mlx5/driver.h>
5 #include <linux/mlx5/mlx5_ifc.h>
6 #include <linux/mlx5/fs.h>
8 #include "lib/fs_chains.h"
9 #include "fs_ft_pool.h"
10 #include "en/mapping.h"
14 #define chains_lock(chains) ((chains)->lock)
15 #define chains_ht(chains) ((chains)->chains_ht)
16 #define prios_ht(chains) ((chains)->prios_ht)
17 #define chains_default_ft(chains) ((chains)->chains_default_ft)
18 #define chains_end_ft(chains) ((chains)->chains_end_ft)
19 #define FT_TBL_SZ (64 * 1024)
21 struct mlx5_fs_chains {
22 struct mlx5_core_dev *dev;
24 struct rhashtable chains_ht;
25 struct rhashtable prios_ht;
26 /* Protects above chains_ht and prios_ht */
29 struct mlx5_flow_table *chains_default_ft;
30 struct mlx5_flow_table *chains_end_ft;
31 struct mapping_ctx *chains_mapping;
33 enum mlx5_flow_namespace_type ns;
41 struct rhash_head node;
48 struct mlx5_fs_chains *chains;
49 struct list_head prios_list;
50 struct mlx5_flow_handle *restore_rule;
51 struct mlx5_modify_hdr *miss_modify_hdr;
61 struct rhash_head node;
62 struct list_head list;
68 struct fs_chain *chain;
69 struct mlx5_flow_table *ft;
70 struct mlx5_flow_table *next_ft;
71 struct mlx5_flow_group *miss_group;
72 struct mlx5_flow_handle *miss_rule;
75 static const struct rhashtable_params chain_params = {
76 .head_offset = offsetof(struct fs_chain, node),
77 .key_offset = offsetof(struct fs_chain, chain),
78 .key_len = sizeof_field(struct fs_chain, chain),
79 .automatic_shrinking = true,
82 static const struct rhashtable_params prio_params = {
83 .head_offset = offsetof(struct prio, node),
84 .key_offset = offsetof(struct prio, key),
85 .key_len = sizeof_field(struct prio, key),
86 .automatic_shrinking = true,
89 bool mlx5_chains_prios_supported(struct mlx5_fs_chains *chains)
91 return chains->flags & MLX5_CHAINS_AND_PRIOS_SUPPORTED;
94 bool mlx5_chains_ignore_flow_level_supported(struct mlx5_fs_chains *chains)
96 return chains->flags & MLX5_CHAINS_IGNORE_FLOW_LEVEL_SUPPORTED;
99 bool mlx5_chains_backwards_supported(struct mlx5_fs_chains *chains)
101 return mlx5_chains_prios_supported(chains) &&
102 mlx5_chains_ignore_flow_level_supported(chains);
105 u32 mlx5_chains_get_chain_range(struct mlx5_fs_chains *chains)
107 if (!mlx5_chains_prios_supported(chains))
110 if (mlx5_chains_ignore_flow_level_supported(chains))
113 /* We should get here only for eswitch case */
114 return FDB_TC_MAX_CHAIN;
117 u32 mlx5_chains_get_nf_ft_chain(struct mlx5_fs_chains *chains)
119 return mlx5_chains_get_chain_range(chains) + 1;
122 u32 mlx5_chains_get_prio_range(struct mlx5_fs_chains *chains)
124 if (mlx5_chains_ignore_flow_level_supported(chains))
127 if (!chains->dev->priv.eswitch ||
128 chains->dev->priv.eswitch->mode != MLX5_ESWITCH_OFFLOADS)
131 /* We should get here only for eswitch case */
132 return FDB_TC_MAX_PRIO;
135 static unsigned int mlx5_chains_get_level_range(struct mlx5_fs_chains *chains)
137 if (mlx5_chains_ignore_flow_level_supported(chains))
140 /* Same value for FDB and NIC RX tables */
141 return FDB_TC_LEVELS_PER_PRIO;
145 mlx5_chains_set_end_ft(struct mlx5_fs_chains *chains,
146 struct mlx5_flow_table *ft)
148 chains_end_ft(chains) = ft;
151 static struct mlx5_flow_table *
152 mlx5_chains_create_table(struct mlx5_fs_chains *chains,
153 u32 chain, u32 prio, u32 level)
155 struct mlx5_flow_table_attr ft_attr = {};
156 struct mlx5_flow_namespace *ns;
157 struct mlx5_flow_table *ft;
160 if (chains->flags & MLX5_CHAINS_FT_TUNNEL_SUPPORTED)
161 ft_attr.flags |= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
162 MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
164 sz = (chain == mlx5_chains_get_nf_ft_chain(chains)) ? FT_TBL_SZ : POOL_NEXT_SIZE;
165 ft_attr.max_fte = sz;
167 /* We use chains_default_ft(chains) as the table's next_ft till
168 * ignore_flow_level is allowed on FT creation and not just for FTEs.
169 * Instead caller should add an explicit miss rule if needed.
171 ft_attr.next_ft = chains_default_ft(chains);
173 /* The root table(chain 0, prio 1, level 0) is required to be
174 * connected to the previous fs_core managed prio.
175 * We always create it, as a managed table, in order to align with
178 if (!mlx5_chains_ignore_flow_level_supported(chains) ||
179 (chain == 0 && prio == 1 && level == 0)) {
180 ft_attr.level = chains->fs_base_level;
181 ft_attr.prio = chains->fs_base_prio;
182 ns = (chains->ns == MLX5_FLOW_NAMESPACE_FDB) ?
183 mlx5_get_fdb_sub_ns(chains->dev, chain) :
184 mlx5_get_flow_namespace(chains->dev, chains->ns);
186 ft_attr.flags |= MLX5_FLOW_TABLE_UNMANAGED;
187 ft_attr.prio = chains->fs_base_prio;
188 /* Firmware doesn't allow us to create another level 0 table,
189 * so we create all unmanaged tables as level 1 (base + 1).
191 * To connect them, we use explicit miss rules with
192 * ignore_flow_level. Caller is responsible to create
193 * these rules (if needed).
195 ft_attr.level = chains->fs_base_level + 1;
196 ns = mlx5_get_flow_namespace(chains->dev, chains->ns);
199 ft_attr.autogroup.num_reserved_entries = 2;
200 ft_attr.autogroup.max_num_groups = chains->group_num;
201 ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
203 mlx5_core_warn(chains->dev, "Failed to create chains table err %d (chain: %d, prio: %d, level: %d, size: %d)\n",
204 (int)PTR_ERR(ft), chain, prio, level, sz);
212 create_chain_restore(struct fs_chain *chain)
214 struct mlx5_eswitch *esw = chain->chains->dev->priv.eswitch;
215 u8 modact[MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto)] = {};
216 struct mlx5_fs_chains *chains = chain->chains;
217 enum mlx5e_tc_attr_to_reg mapped_obj_to_reg;
218 struct mlx5_modify_hdr *mod_hdr;
222 if (chain->chain == mlx5_chains_get_nf_ft_chain(chains) ||
223 !mlx5_chains_prios_supported(chains) ||
224 !chains->chains_mapping)
227 err = mlx5_chains_get_chain_mapping(chains, chain->chain, &index);
230 if (index == MLX5_FS_DEFAULT_FLOW_TAG) {
231 /* we got the special default flow tag id, so we won't know
232 * if we actually marked the packet with the restore rule
235 * This case isn't possible with MLX5_FS_DEFAULT_FLOW_TAG = 0.
237 err = mlx5_chains_get_chain_mapping(chains, chain->chain, &index);
238 mapping_remove(chains->chains_mapping, MLX5_FS_DEFAULT_FLOW_TAG);
245 if (chains->ns == MLX5_FLOW_NAMESPACE_FDB) {
246 mapped_obj_to_reg = MAPPED_OBJ_TO_REG;
247 chain->restore_rule = esw_add_restore_rule(esw, chain->id);
248 if (IS_ERR(chain->restore_rule)) {
249 err = PTR_ERR(chain->restore_rule);
252 } else if (chains->ns == MLX5_FLOW_NAMESPACE_KERNEL) {
253 /* For NIC RX we don't need a restore rule
254 * since we write the metadata to reg_b
255 * that is passed to SW directly.
257 mapped_obj_to_reg = NIC_MAPPED_OBJ_TO_REG;
263 MLX5_SET(set_action_in, modact, action_type, MLX5_ACTION_TYPE_SET);
264 MLX5_SET(set_action_in, modact, field,
265 mlx5e_tc_attr_to_reg_mappings[mapped_obj_to_reg].mfield);
266 MLX5_SET(set_action_in, modact, offset,
267 mlx5e_tc_attr_to_reg_mappings[mapped_obj_to_reg].moffset);
268 MLX5_SET(set_action_in, modact, length,
269 mlx5e_tc_attr_to_reg_mappings[mapped_obj_to_reg].mlen == 32 ?
270 0 : mlx5e_tc_attr_to_reg_mappings[mapped_obj_to_reg].mlen);
271 MLX5_SET(set_action_in, modact, data, chain->id);
272 mod_hdr = mlx5_modify_header_alloc(chains->dev, chains->ns,
274 if (IS_ERR(mod_hdr)) {
275 err = PTR_ERR(mod_hdr);
278 chain->miss_modify_hdr = mod_hdr;
283 if (!IS_ERR_OR_NULL(chain->restore_rule))
284 mlx5_del_flow_rules(chain->restore_rule);
286 /* Datapath can't find this mapping, so we can safely remove it */
287 mapping_remove(chains->chains_mapping, chain->id);
291 static void destroy_chain_restore(struct fs_chain *chain)
293 struct mlx5_fs_chains *chains = chain->chains;
295 if (!chain->miss_modify_hdr)
298 if (chain->restore_rule)
299 mlx5_del_flow_rules(chain->restore_rule);
301 mlx5_modify_header_dealloc(chains->dev, chain->miss_modify_hdr);
302 mapping_remove(chains->chains_mapping, chain->id);
305 static struct fs_chain *
306 mlx5_chains_create_chain(struct mlx5_fs_chains *chains, u32 chain)
308 struct fs_chain *chain_s = NULL;
311 chain_s = kvzalloc(sizeof(*chain_s), GFP_KERNEL);
313 return ERR_PTR(-ENOMEM);
315 chain_s->chains = chains;
316 chain_s->chain = chain;
317 INIT_LIST_HEAD(&chain_s->prios_list);
319 err = create_chain_restore(chain_s);
323 err = rhashtable_insert_fast(&chains_ht(chains), &chain_s->node,
331 destroy_chain_restore(chain_s);
338 mlx5_chains_destroy_chain(struct fs_chain *chain)
340 struct mlx5_fs_chains *chains = chain->chains;
342 rhashtable_remove_fast(&chains_ht(chains), &chain->node,
345 destroy_chain_restore(chain);
349 static struct fs_chain *
350 mlx5_chains_get_chain(struct mlx5_fs_chains *chains, u32 chain)
352 struct fs_chain *chain_s;
354 chain_s = rhashtable_lookup_fast(&chains_ht(chains), &chain,
357 chain_s = mlx5_chains_create_chain(chains, chain);
367 static struct mlx5_flow_handle *
368 mlx5_chains_add_miss_rule(struct fs_chain *chain,
369 struct mlx5_flow_table *ft,
370 struct mlx5_flow_table *next_ft)
372 struct mlx5_fs_chains *chains = chain->chains;
373 struct mlx5_flow_destination dest = {};
374 struct mlx5_flow_act act = {};
376 act.flags = FLOW_ACT_NO_APPEND;
377 if (mlx5_chains_ignore_flow_level_supported(chain->chains))
378 act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
380 act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
381 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
384 if (chains->chains_mapping && next_ft == chains_end_ft(chains) &&
385 chain->chain != mlx5_chains_get_nf_ft_chain(chains) &&
386 mlx5_chains_prios_supported(chains)) {
387 act.modify_hdr = chain->miss_modify_hdr;
388 act.action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
391 return mlx5_add_flow_rules(ft, NULL, &act, &dest, 1);
395 mlx5_chains_update_prio_prevs(struct prio *prio,
396 struct mlx5_flow_table *next_ft)
398 struct mlx5_flow_handle *miss_rules[FDB_TC_LEVELS_PER_PRIO + 1] = {};
399 struct fs_chain *chain = prio->chain;
406 /* Iterate in reverse order until reaching the level 0 rule of
407 * the previous priority, adding all the miss rules first, so we can
408 * revert them if any of them fails.
411 list_for_each_entry_continue_reverse(pos,
414 miss_rules[n] = mlx5_chains_add_miss_rule(chain,
417 if (IS_ERR(miss_rules[n])) {
418 err = PTR_ERR(miss_rules[n]);
427 /* Success, delete old miss rules, and update the pointers. */
430 list_for_each_entry_continue_reverse(pos,
433 mlx5_del_flow_rules(pos->miss_rule);
435 pos->miss_rule = miss_rules[n];
436 pos->next_ft = next_ft;
447 mlx5_del_flow_rules(miss_rules[n]);
453 mlx5_chains_put_chain(struct fs_chain *chain)
455 if (--chain->ref == 0)
456 mlx5_chains_destroy_chain(chain);
460 mlx5_chains_create_prio(struct mlx5_fs_chains *chains,
461 u32 chain, u32 prio, u32 level)
463 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
464 struct mlx5_flow_handle *miss_rule;
465 struct mlx5_flow_group *miss_group;
466 struct mlx5_flow_table *next_ft;
467 struct mlx5_flow_table *ft;
468 struct fs_chain *chain_s;
469 struct list_head *pos;
474 chain_s = mlx5_chains_get_chain(chains, chain);
476 return ERR_CAST(chain_s);
478 prio_s = kvzalloc(sizeof(*prio_s), GFP_KERNEL);
479 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
480 if (!prio_s || !flow_group_in) {
485 /* Chain's prio list is sorted by prio and level.
486 * And all levels of some prio point to the next prio's level 0.
487 * Example list (prio, level):
488 * (3,0)->(3,1)->(5,0)->(5,1)->(6,1)->(7,0)
489 * In hardware, we will we have the following pointers:
490 * (3,0) -> (5,0) -> (7,0) -> Slow path
496 /* Default miss for each chain: */
497 next_ft = (chain == mlx5_chains_get_nf_ft_chain(chains)) ?
498 chains_default_ft(chains) :
499 chains_end_ft(chains);
500 list_for_each(pos, &chain_s->prios_list) {
501 struct prio *p = list_entry(pos, struct prio, list);
503 /* exit on first pos that is larger */
504 if (prio < p->key.prio || (prio == p->key.prio &&
505 level < p->key.level)) {
506 /* Get next level 0 table */
507 next_ft = p->key.level == 0 ? p->ft : p->next_ft;
512 ft = mlx5_chains_create_table(chains, chain, prio, level);
518 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index,
520 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
522 miss_group = mlx5_create_flow_group(ft, flow_group_in);
523 if (IS_ERR(miss_group)) {
524 err = PTR_ERR(miss_group);
528 /* Add miss rule to next_ft */
529 miss_rule = mlx5_chains_add_miss_rule(chain_s, ft, next_ft);
530 if (IS_ERR(miss_rule)) {
531 err = PTR_ERR(miss_rule);
535 prio_s->miss_group = miss_group;
536 prio_s->miss_rule = miss_rule;
537 prio_s->next_ft = next_ft;
538 prio_s->chain = chain_s;
539 prio_s->key.chain = chain;
540 prio_s->key.prio = prio;
541 prio_s->key.level = level;
544 err = rhashtable_insert_fast(&prios_ht(chains), &prio_s->node,
549 list_add(&prio_s->list, pos->prev);
551 /* Table is ready, connect it */
552 err = mlx5_chains_update_prio_prevs(prio_s, ft);
556 kvfree(flow_group_in);
560 list_del(&prio_s->list);
561 rhashtable_remove_fast(&prios_ht(chains), &prio_s->node,
564 mlx5_del_flow_rules(miss_rule);
566 mlx5_destroy_flow_group(miss_group);
568 mlx5_destroy_flow_table(ft);
572 kvfree(flow_group_in);
573 mlx5_chains_put_chain(chain_s);
578 mlx5_chains_destroy_prio(struct mlx5_fs_chains *chains,
581 struct fs_chain *chain = prio->chain;
583 WARN_ON(mlx5_chains_update_prio_prevs(prio,
586 list_del(&prio->list);
587 rhashtable_remove_fast(&prios_ht(chains), &prio->node,
589 mlx5_del_flow_rules(prio->miss_rule);
590 mlx5_destroy_flow_group(prio->miss_group);
591 mlx5_destroy_flow_table(prio->ft);
592 mlx5_chains_put_chain(chain);
596 struct mlx5_flow_table *
597 mlx5_chains_get_table(struct mlx5_fs_chains *chains, u32 chain, u32 prio,
600 struct mlx5_flow_table *prev_fts;
605 if ((chain > mlx5_chains_get_chain_range(chains) &&
606 chain != mlx5_chains_get_nf_ft_chain(chains)) ||
607 prio > mlx5_chains_get_prio_range(chains) ||
608 level > mlx5_chains_get_level_range(chains))
609 return ERR_PTR(-EOPNOTSUPP);
611 /* create earlier levels for correct fs_core lookup when
614 for (l = 0; l < level; l++) {
615 prev_fts = mlx5_chains_get_table(chains, chain, prio, l);
616 if (IS_ERR(prev_fts)) {
617 prio_s = ERR_CAST(prev_fts);
626 mutex_lock(&chains_lock(chains));
627 prio_s = rhashtable_lookup_fast(&prios_ht(chains), &key,
630 prio_s = mlx5_chains_create_prio(chains, chain,
633 goto err_create_prio;
637 mutex_unlock(&chains_lock(chains));
642 mutex_unlock(&chains_lock(chains));
645 mlx5_chains_put_table(chains, chain, prio, l);
646 return ERR_CAST(prio_s);
650 mlx5_chains_put_table(struct mlx5_fs_chains *chains, u32 chain, u32 prio,
660 mutex_lock(&chains_lock(chains));
661 prio_s = rhashtable_lookup_fast(&prios_ht(chains), &key,
666 if (--prio_s->ref == 0)
667 mlx5_chains_destroy_prio(chains, prio_s);
668 mutex_unlock(&chains_lock(chains));
671 mlx5_chains_put_table(chains, chain, prio, level);
676 mutex_unlock(&chains_lock(chains));
678 "Couldn't find table: (chain: %d prio: %d level: %d)",
682 struct mlx5_flow_table *
683 mlx5_chains_get_tc_end_ft(struct mlx5_fs_chains *chains)
685 return chains_end_ft(chains);
688 struct mlx5_flow_table *
689 mlx5_chains_create_global_table(struct mlx5_fs_chains *chains)
691 u32 chain, prio, level;
694 if (!mlx5_chains_ignore_flow_level_supported(chains)) {
697 mlx5_core_warn(chains->dev,
698 "Couldn't create global flow table, ignore_flow_level not supported.");
702 chain = mlx5_chains_get_chain_range(chains),
703 prio = mlx5_chains_get_prio_range(chains);
704 level = mlx5_chains_get_level_range(chains);
706 return mlx5_chains_create_table(chains, chain, prio, level);
713 mlx5_chains_destroy_global_table(struct mlx5_fs_chains *chains,
714 struct mlx5_flow_table *ft)
716 mlx5_destroy_flow_table(ft);
719 static struct mlx5_fs_chains *
720 mlx5_chains_init(struct mlx5_core_dev *dev, struct mlx5_chains_attr *attr)
722 struct mlx5_fs_chains *chains;
725 chains = kzalloc(sizeof(*chains), GFP_KERNEL);
727 return ERR_PTR(-ENOMEM);
730 chains->flags = attr->flags;
731 chains->ns = attr->ns;
732 chains->group_num = attr->max_grp_num;
733 chains->chains_mapping = attr->mapping;
734 chains->fs_base_prio = attr->fs_base_prio;
735 chains->fs_base_level = attr->fs_base_level;
736 chains_default_ft(chains) = chains_end_ft(chains) = attr->default_ft;
738 err = rhashtable_init(&chains_ht(chains), &chain_params);
740 goto init_chains_ht_err;
742 err = rhashtable_init(&prios_ht(chains), &prio_params);
744 goto init_prios_ht_err;
746 mutex_init(&chains_lock(chains));
751 rhashtable_destroy(&chains_ht(chains));
758 mlx5_chains_cleanup(struct mlx5_fs_chains *chains)
760 mutex_destroy(&chains_lock(chains));
761 rhashtable_destroy(&prios_ht(chains));
762 rhashtable_destroy(&chains_ht(chains));
767 struct mlx5_fs_chains *
768 mlx5_chains_create(struct mlx5_core_dev *dev, struct mlx5_chains_attr *attr)
770 struct mlx5_fs_chains *chains;
772 chains = mlx5_chains_init(dev, attr);
778 mlx5_chains_destroy(struct mlx5_fs_chains *chains)
780 mlx5_chains_cleanup(chains);
784 mlx5_chains_get_chain_mapping(struct mlx5_fs_chains *chains, u32 chain,
787 struct mapping_ctx *ctx = chains->chains_mapping;
788 struct mlx5_mapped_obj mapped_obj = {};
790 mapped_obj.type = MLX5_MAPPED_OBJ_CHAIN;
791 mapped_obj.chain = chain;
792 return mapping_add(ctx, &mapped_obj, chain_mapping);
796 mlx5_chains_put_chain_mapping(struct mlx5_fs_chains *chains, u32 chain_mapping)
798 struct mapping_ctx *ctx = chains->chains_mapping;
800 return mapping_remove(ctx, chain_mapping);
804 mlx5_chains_print_info(struct mlx5_fs_chains *chains)
806 mlx5_core_dbg(chains->dev, "Flow table chains groups(%d)\n", chains->group_num);