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 tc_default_ft(chains) ((chains)->tc_default_ft)
18 #define tc_end_ft(chains) ((chains)->tc_end_ft)
19 #define ns_to_chains_fs_prio(ns) ((ns) == MLX5_FLOW_NAMESPACE_FDB ? \
20 FDB_TC_OFFLOAD : MLX5E_TC_PRIO)
21 #define FT_TBL_SZ (64 * 1024)
23 struct mlx5_fs_chains {
24 struct mlx5_core_dev *dev;
26 struct rhashtable chains_ht;
27 struct rhashtable prios_ht;
28 /* Protects above chains_ht and prios_ht */
31 struct mlx5_flow_table *tc_default_ft;
32 struct mlx5_flow_table *tc_end_ft;
33 struct mapping_ctx *chains_mapping;
35 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 /* We should get here only for eswitch case */
128 return FDB_TC_MAX_PRIO;
131 static unsigned int mlx5_chains_get_level_range(struct mlx5_fs_chains *chains)
133 if (mlx5_chains_ignore_flow_level_supported(chains))
136 /* Same value for FDB and NIC RX tables */
137 return FDB_TC_LEVELS_PER_PRIO;
141 mlx5_chains_set_end_ft(struct mlx5_fs_chains *chains,
142 struct mlx5_flow_table *ft)
144 tc_end_ft(chains) = ft;
147 static struct mlx5_flow_table *
148 mlx5_chains_create_table(struct mlx5_fs_chains *chains,
149 u32 chain, u32 prio, u32 level)
151 struct mlx5_flow_table_attr ft_attr = {};
152 struct mlx5_flow_namespace *ns;
153 struct mlx5_flow_table *ft;
156 if (chains->flags & MLX5_CHAINS_FT_TUNNEL_SUPPORTED)
157 ft_attr.flags |= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
158 MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
160 sz = (chain == mlx5_chains_get_nf_ft_chain(chains)) ? FT_TBL_SZ : POOL_NEXT_SIZE;
161 ft_attr.max_fte = sz;
163 /* We use tc_default_ft(chains) as the table's next_ft till
164 * ignore_flow_level is allowed on FT creation and not just for FTEs.
165 * Instead caller should add an explicit miss rule if needed.
167 ft_attr.next_ft = tc_default_ft(chains);
169 /* The root table(chain 0, prio 1, level 0) is required to be
170 * connected to the previous fs_core managed prio.
171 * We always create it, as a managed table, in order to align with
174 if (!mlx5_chains_ignore_flow_level_supported(chains) ||
175 (chain == 0 && prio == 1 && level == 0)) {
176 ft_attr.level = level;
177 ft_attr.prio = prio - 1;
178 ns = (chains->ns == MLX5_FLOW_NAMESPACE_FDB) ?
179 mlx5_get_fdb_sub_ns(chains->dev, chain) :
180 mlx5_get_flow_namespace(chains->dev, chains->ns);
182 ft_attr.flags |= MLX5_FLOW_TABLE_UNMANAGED;
183 ft_attr.prio = ns_to_chains_fs_prio(chains->ns);
184 /* Firmware doesn't allow us to create another level 0 table,
185 * so we create all unmanaged tables as level 1.
187 * To connect them, we use explicit miss rules with
188 * ignore_flow_level. Caller is responsible to create
189 * these rules (if needed).
192 ns = mlx5_get_flow_namespace(chains->dev, chains->ns);
195 ft_attr.autogroup.num_reserved_entries = 2;
196 ft_attr.autogroup.max_num_groups = chains->group_num;
197 ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
199 mlx5_core_warn(chains->dev, "Failed to create chains table err %d (chain: %d, prio: %d, level: %d, size: %d)\n",
200 (int)PTR_ERR(ft), chain, prio, level, sz);
208 create_chain_restore(struct fs_chain *chain)
210 struct mlx5_eswitch *esw = chain->chains->dev->priv.eswitch;
211 char modact[MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto)];
212 struct mlx5_fs_chains *chains = chain->chains;
213 enum mlx5e_tc_attr_to_reg chain_to_reg;
214 struct mlx5_modify_hdr *mod_hdr;
218 if (chain->chain == mlx5_chains_get_nf_ft_chain(chains) ||
219 !mlx5_chains_prios_supported(chains))
222 err = mlx5_chains_get_chain_mapping(chains, chain->chain, &index);
225 if (index == MLX5_FS_DEFAULT_FLOW_TAG) {
226 /* we got the special default flow tag id, so we won't know
227 * if we actually marked the packet with the restore rule
230 * This case isn't possible with MLX5_FS_DEFAULT_FLOW_TAG = 0.
232 err = mlx5_chains_get_chain_mapping(chains, chain->chain, &index);
233 mapping_remove(chains->chains_mapping, MLX5_FS_DEFAULT_FLOW_TAG);
240 if (chains->ns == MLX5_FLOW_NAMESPACE_FDB) {
241 chain_to_reg = CHAIN_TO_REG;
242 chain->restore_rule = esw_add_restore_rule(esw, chain->id);
243 if (IS_ERR(chain->restore_rule)) {
244 err = PTR_ERR(chain->restore_rule);
247 } else if (chains->ns == MLX5_FLOW_NAMESPACE_KERNEL) {
248 /* For NIC RX we don't need a restore rule
249 * since we write the metadata to reg_b
250 * that is passed to SW directly.
252 chain_to_reg = NIC_CHAIN_TO_REG;
258 MLX5_SET(set_action_in, modact, action_type, MLX5_ACTION_TYPE_SET);
259 MLX5_SET(set_action_in, modact, field,
260 mlx5e_tc_attr_to_reg_mappings[chain_to_reg].mfield);
261 MLX5_SET(set_action_in, modact, offset,
262 mlx5e_tc_attr_to_reg_mappings[chain_to_reg].moffset);
263 MLX5_SET(set_action_in, modact, length,
264 mlx5e_tc_attr_to_reg_mappings[chain_to_reg].mlen == 32 ?
265 0 : mlx5e_tc_attr_to_reg_mappings[chain_to_reg].mlen);
266 MLX5_SET(set_action_in, modact, data, chain->id);
267 mod_hdr = mlx5_modify_header_alloc(chains->dev, chains->ns,
269 if (IS_ERR(mod_hdr)) {
270 err = PTR_ERR(mod_hdr);
273 chain->miss_modify_hdr = mod_hdr;
278 if (!IS_ERR_OR_NULL(chain->restore_rule))
279 mlx5_del_flow_rules(chain->restore_rule);
281 /* Datapath can't find this mapping, so we can safely remove it */
282 mapping_remove(chains->chains_mapping, chain->id);
286 static void destroy_chain_restore(struct fs_chain *chain)
288 struct mlx5_fs_chains *chains = chain->chains;
290 if (!chain->miss_modify_hdr)
293 if (chain->restore_rule)
294 mlx5_del_flow_rules(chain->restore_rule);
296 mlx5_modify_header_dealloc(chains->dev, chain->miss_modify_hdr);
297 mapping_remove(chains->chains_mapping, chain->id);
300 static struct fs_chain *
301 mlx5_chains_create_chain(struct mlx5_fs_chains *chains, u32 chain)
303 struct fs_chain *chain_s = NULL;
306 chain_s = kvzalloc(sizeof(*chain_s), GFP_KERNEL);
308 return ERR_PTR(-ENOMEM);
310 chain_s->chains = chains;
311 chain_s->chain = chain;
312 INIT_LIST_HEAD(&chain_s->prios_list);
314 err = create_chain_restore(chain_s);
318 err = rhashtable_insert_fast(&chains_ht(chains), &chain_s->node,
326 destroy_chain_restore(chain_s);
333 mlx5_chains_destroy_chain(struct fs_chain *chain)
335 struct mlx5_fs_chains *chains = chain->chains;
337 rhashtable_remove_fast(&chains_ht(chains), &chain->node,
340 destroy_chain_restore(chain);
344 static struct fs_chain *
345 mlx5_chains_get_chain(struct mlx5_fs_chains *chains, u32 chain)
347 struct fs_chain *chain_s;
349 chain_s = rhashtable_lookup_fast(&chains_ht(chains), &chain,
352 chain_s = mlx5_chains_create_chain(chains, chain);
362 static struct mlx5_flow_handle *
363 mlx5_chains_add_miss_rule(struct fs_chain *chain,
364 struct mlx5_flow_table *ft,
365 struct mlx5_flow_table *next_ft)
367 struct mlx5_fs_chains *chains = chain->chains;
368 struct mlx5_flow_destination dest = {};
369 struct mlx5_flow_act act = {};
371 act.flags = FLOW_ACT_NO_APPEND;
372 if (mlx5_chains_ignore_flow_level_supported(chain->chains))
373 act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
375 act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
376 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
379 if (next_ft == tc_end_ft(chains) &&
380 chain->chain != mlx5_chains_get_nf_ft_chain(chains) &&
381 mlx5_chains_prios_supported(chains)) {
382 act.modify_hdr = chain->miss_modify_hdr;
383 act.action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
386 return mlx5_add_flow_rules(ft, NULL, &act, &dest, 1);
390 mlx5_chains_update_prio_prevs(struct prio *prio,
391 struct mlx5_flow_table *next_ft)
393 struct mlx5_flow_handle *miss_rules[FDB_TC_LEVELS_PER_PRIO + 1] = {};
394 struct fs_chain *chain = prio->chain;
401 /* Iterate in reverse order until reaching the level 0 rule of
402 * the previous priority, adding all the miss rules first, so we can
403 * revert them if any of them fails.
406 list_for_each_entry_continue_reverse(pos,
409 miss_rules[n] = mlx5_chains_add_miss_rule(chain,
412 if (IS_ERR(miss_rules[n])) {
413 err = PTR_ERR(miss_rules[n]);
422 /* Success, delete old miss rules, and update the pointers. */
425 list_for_each_entry_continue_reverse(pos,
428 mlx5_del_flow_rules(pos->miss_rule);
430 pos->miss_rule = miss_rules[n];
431 pos->next_ft = next_ft;
442 mlx5_del_flow_rules(miss_rules[n]);
448 mlx5_chains_put_chain(struct fs_chain *chain)
450 if (--chain->ref == 0)
451 mlx5_chains_destroy_chain(chain);
455 mlx5_chains_create_prio(struct mlx5_fs_chains *chains,
456 u32 chain, u32 prio, u32 level)
458 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
459 struct mlx5_flow_handle *miss_rule;
460 struct mlx5_flow_group *miss_group;
461 struct mlx5_flow_table *next_ft;
462 struct mlx5_flow_table *ft;
463 struct fs_chain *chain_s;
464 struct list_head *pos;
469 chain_s = mlx5_chains_get_chain(chains, chain);
471 return ERR_CAST(chain_s);
473 prio_s = kvzalloc(sizeof(*prio_s), GFP_KERNEL);
474 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
475 if (!prio_s || !flow_group_in) {
480 /* Chain's prio list is sorted by prio and level.
481 * And all levels of some prio point to the next prio's level 0.
482 * Example list (prio, level):
483 * (3,0)->(3,1)->(5,0)->(5,1)->(6,1)->(7,0)
484 * In hardware, we will we have the following pointers:
485 * (3,0) -> (5,0) -> (7,0) -> Slow path
491 /* Default miss for each chain: */
492 next_ft = (chain == mlx5_chains_get_nf_ft_chain(chains)) ?
493 tc_default_ft(chains) :
495 list_for_each(pos, &chain_s->prios_list) {
496 struct prio *p = list_entry(pos, struct prio, list);
498 /* exit on first pos that is larger */
499 if (prio < p->key.prio || (prio == p->key.prio &&
500 level < p->key.level)) {
501 /* Get next level 0 table */
502 next_ft = p->key.level == 0 ? p->ft : p->next_ft;
507 ft = mlx5_chains_create_table(chains, chain, prio, level);
513 MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index,
515 MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
517 miss_group = mlx5_create_flow_group(ft, flow_group_in);
518 if (IS_ERR(miss_group)) {
519 err = PTR_ERR(miss_group);
523 /* Add miss rule to next_ft */
524 miss_rule = mlx5_chains_add_miss_rule(chain_s, ft, next_ft);
525 if (IS_ERR(miss_rule)) {
526 err = PTR_ERR(miss_rule);
530 prio_s->miss_group = miss_group;
531 prio_s->miss_rule = miss_rule;
532 prio_s->next_ft = next_ft;
533 prio_s->chain = chain_s;
534 prio_s->key.chain = chain;
535 prio_s->key.prio = prio;
536 prio_s->key.level = level;
539 err = rhashtable_insert_fast(&prios_ht(chains), &prio_s->node,
544 list_add(&prio_s->list, pos->prev);
546 /* Table is ready, connect it */
547 err = mlx5_chains_update_prio_prevs(prio_s, ft);
551 kvfree(flow_group_in);
555 list_del(&prio_s->list);
556 rhashtable_remove_fast(&prios_ht(chains), &prio_s->node,
559 mlx5_del_flow_rules(miss_rule);
561 mlx5_destroy_flow_group(miss_group);
563 mlx5_destroy_flow_table(ft);
567 kvfree(flow_group_in);
568 mlx5_chains_put_chain(chain_s);
573 mlx5_chains_destroy_prio(struct mlx5_fs_chains *chains,
576 struct fs_chain *chain = prio->chain;
578 WARN_ON(mlx5_chains_update_prio_prevs(prio,
581 list_del(&prio->list);
582 rhashtable_remove_fast(&prios_ht(chains), &prio->node,
584 mlx5_del_flow_rules(prio->miss_rule);
585 mlx5_destroy_flow_group(prio->miss_group);
586 mlx5_destroy_flow_table(prio->ft);
587 mlx5_chains_put_chain(chain);
591 struct mlx5_flow_table *
592 mlx5_chains_get_table(struct mlx5_fs_chains *chains, u32 chain, u32 prio,
595 struct mlx5_flow_table *prev_fts;
600 if ((chain > mlx5_chains_get_chain_range(chains) &&
601 chain != mlx5_chains_get_nf_ft_chain(chains)) ||
602 prio > mlx5_chains_get_prio_range(chains) ||
603 level > mlx5_chains_get_level_range(chains))
604 return ERR_PTR(-EOPNOTSUPP);
606 /* create earlier levels for correct fs_core lookup when
609 for (l = 0; l < level; l++) {
610 prev_fts = mlx5_chains_get_table(chains, chain, prio, l);
611 if (IS_ERR(prev_fts)) {
612 prio_s = ERR_CAST(prev_fts);
621 mutex_lock(&chains_lock(chains));
622 prio_s = rhashtable_lookup_fast(&prios_ht(chains), &key,
625 prio_s = mlx5_chains_create_prio(chains, chain,
628 goto err_create_prio;
632 mutex_unlock(&chains_lock(chains));
637 mutex_unlock(&chains_lock(chains));
640 mlx5_chains_put_table(chains, chain, prio, l);
641 return ERR_CAST(prio_s);
645 mlx5_chains_put_table(struct mlx5_fs_chains *chains, u32 chain, u32 prio,
655 mutex_lock(&chains_lock(chains));
656 prio_s = rhashtable_lookup_fast(&prios_ht(chains), &key,
661 if (--prio_s->ref == 0)
662 mlx5_chains_destroy_prio(chains, prio_s);
663 mutex_unlock(&chains_lock(chains));
666 mlx5_chains_put_table(chains, chain, prio, level);
671 mutex_unlock(&chains_lock(chains));
673 "Couldn't find table: (chain: %d prio: %d level: %d)",
677 struct mlx5_flow_table *
678 mlx5_chains_get_tc_end_ft(struct mlx5_fs_chains *chains)
680 return tc_end_ft(chains);
683 struct mlx5_flow_table *
684 mlx5_chains_create_global_table(struct mlx5_fs_chains *chains)
686 u32 chain, prio, level;
689 if (!mlx5_chains_ignore_flow_level_supported(chains)) {
692 mlx5_core_warn(chains->dev,
693 "Couldn't create global flow table, ignore_flow_level not supported.");
697 chain = mlx5_chains_get_chain_range(chains),
698 prio = mlx5_chains_get_prio_range(chains);
699 level = mlx5_chains_get_level_range(chains);
701 return mlx5_chains_create_table(chains, chain, prio, level);
708 mlx5_chains_destroy_global_table(struct mlx5_fs_chains *chains,
709 struct mlx5_flow_table *ft)
711 mlx5_destroy_flow_table(ft);
714 static struct mlx5_fs_chains *
715 mlx5_chains_init(struct mlx5_core_dev *dev, struct mlx5_chains_attr *attr)
717 struct mlx5_fs_chains *chains_priv;
718 u32 max_flow_counter;
721 chains_priv = kzalloc(sizeof(*chains_priv), GFP_KERNEL);
723 return ERR_PTR(-ENOMEM);
725 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
726 MLX5_CAP_GEN(dev, max_flow_counter_15_0);
729 "Init flow table chains, max counters(%d), groups(%d), max flow table size(%d)\n",
730 max_flow_counter, attr->max_grp_num, attr->max_ft_sz);
732 chains_priv->dev = dev;
733 chains_priv->flags = attr->flags;
734 chains_priv->ns = attr->ns;
735 chains_priv->group_num = attr->max_grp_num;
736 chains_priv->chains_mapping = attr->mapping;
737 tc_default_ft(chains_priv) = tc_end_ft(chains_priv) = attr->default_ft;
739 mlx5_core_info(dev, "Supported tc offload range - chains: %u, prios: %u\n",
740 mlx5_chains_get_chain_range(chains_priv),
741 mlx5_chains_get_prio_range(chains_priv));
743 err = rhashtable_init(&chains_ht(chains_priv), &chain_params);
745 goto init_chains_ht_err;
747 err = rhashtable_init(&prios_ht(chains_priv), &prio_params);
749 goto init_prios_ht_err;
751 mutex_init(&chains_lock(chains_priv));
756 rhashtable_destroy(&chains_ht(chains_priv));
763 mlx5_chains_cleanup(struct mlx5_fs_chains *chains)
765 mutex_destroy(&chains_lock(chains));
766 rhashtable_destroy(&prios_ht(chains));
767 rhashtable_destroy(&chains_ht(chains));
772 struct mlx5_fs_chains *
773 mlx5_chains_create(struct mlx5_core_dev *dev, struct mlx5_chains_attr *attr)
775 struct mlx5_fs_chains *chains;
777 chains = mlx5_chains_init(dev, attr);
783 mlx5_chains_destroy(struct mlx5_fs_chains *chains)
785 mlx5_chains_cleanup(chains);
789 mlx5_chains_get_chain_mapping(struct mlx5_fs_chains *chains, u32 chain,
792 struct mapping_ctx *ctx = chains->chains_mapping;
793 struct mlx5_mapped_obj mapped_obj = {};
795 mapped_obj.type = MLX5_MAPPED_OBJ_CHAIN;
796 mapped_obj.chain = chain;
797 return mapping_add(ctx, &mapped_obj, chain_mapping);
801 mlx5_chains_put_chain_mapping(struct mlx5_fs_chains *chains, u32 chain_mapping)
803 struct mapping_ctx *ctx = chains->chains_mapping;
805 return mapping_remove(ctx, chain_mapping);