1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/errno.h>
7 #include <linux/list.h>
8 #include <linux/string.h>
9 #include <linux/rhashtable.h>
10 #include <linux/netdevice.h>
11 #include <net/net_namespace.h>
12 #include <net/tc_act/tc_vlan.h>
16 #include "resources.h"
18 #include "core_acl_flex_keys.h"
19 #include "core_acl_flex_actions.h"
20 #include "spectrum_acl_tcam.h"
23 struct mlxsw_sp *mlxsw_sp;
24 struct mlxsw_afk *afk;
25 struct mlxsw_sp_fid *dummy_fid;
26 struct rhashtable ruleset_ht;
27 struct list_head rules;
29 struct delayed_work dw;
30 unsigned long interval; /* ms */
31 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000
32 } rule_activity_update;
33 struct mlxsw_sp_acl_tcam tcam;
36 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl)
41 struct mlxsw_sp_acl_block_binding {
42 struct list_head list;
43 struct net_device *dev;
44 struct mlxsw_sp_port *mlxsw_sp_port;
48 struct mlxsw_sp_acl_ruleset_ht_key {
49 struct mlxsw_sp_acl_block *block;
51 const struct mlxsw_sp_acl_profile_ops *ops;
54 struct mlxsw_sp_acl_ruleset {
55 struct rhash_head ht_node; /* Member of acl HT */
56 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
57 struct rhashtable rule_ht;
58 unsigned int ref_count;
59 unsigned long priv[0];
60 /* priv has to be always the last item */
63 struct mlxsw_sp_acl_rule {
64 struct rhash_head ht_node; /* Member of rule HT */
65 struct list_head list;
66 unsigned long cookie; /* HT key */
67 struct mlxsw_sp_acl_ruleset *ruleset;
68 struct mlxsw_sp_acl_rule_info *rulei;
72 unsigned long priv[0];
73 /* priv has to be always the last item */
76 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
77 .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
78 .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
79 .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
80 .automatic_shrinking = true,
83 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
84 .key_len = sizeof(unsigned long),
85 .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
86 .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
87 .automatic_shrinking = true,
90 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
92 return mlxsw_sp->acl->dummy_fid;
95 struct mlxsw_sp *mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block *block)
97 return block->mlxsw_sp;
100 unsigned int mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block *block)
102 return block ? block->rule_count : 0;
105 void mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block *block)
108 block->disable_count++;
111 void mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block *block)
114 block->disable_count--;
117 bool mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block *block)
119 return block->disable_count;
122 bool mlxsw_sp_acl_block_is_egress_bound(struct mlxsw_sp_acl_block *block)
124 struct mlxsw_sp_acl_block_binding *binding;
126 list_for_each_entry(binding, &block->binding_list, list) {
127 if (!binding->ingress)
134 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
136 /* We hold a reference on ruleset ourselves */
137 return ruleset->ref_count == 2;
141 mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
142 struct mlxsw_sp_acl_block *block,
143 struct mlxsw_sp_acl_block_binding *binding)
145 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
146 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
148 return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
149 binding->mlxsw_sp_port, binding->ingress);
153 mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
154 struct mlxsw_sp_acl_block *block,
155 struct mlxsw_sp_acl_block_binding *binding)
157 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
158 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
160 ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
161 binding->mlxsw_sp_port, binding->ingress);
164 static bool mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block *block)
166 return block->ruleset_zero;
170 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
171 struct mlxsw_sp_acl_ruleset *ruleset,
172 struct mlxsw_sp_acl_block *block)
174 struct mlxsw_sp_acl_block_binding *binding;
177 block->ruleset_zero = ruleset;
178 list_for_each_entry(binding, &block->binding_list, list) {
179 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
186 list_for_each_entry_continue_reverse(binding, &block->binding_list,
188 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
189 block->ruleset_zero = NULL;
195 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
196 struct mlxsw_sp_acl_ruleset *ruleset,
197 struct mlxsw_sp_acl_block *block)
199 struct mlxsw_sp_acl_block_binding *binding;
201 list_for_each_entry(binding, &block->binding_list, list)
202 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
203 block->ruleset_zero = NULL;
206 struct mlxsw_sp_acl_block *mlxsw_sp_acl_block_create(struct mlxsw_sp *mlxsw_sp,
209 struct mlxsw_sp_acl_block *block;
211 block = kzalloc(sizeof(*block), GFP_KERNEL);
214 INIT_LIST_HEAD(&block->binding_list);
215 block->mlxsw_sp = mlxsw_sp;
220 void mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block *block)
222 WARN_ON(!list_empty(&block->binding_list));
226 static struct mlxsw_sp_acl_block_binding *
227 mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block *block,
228 struct mlxsw_sp_port *mlxsw_sp_port, bool ingress)
230 struct mlxsw_sp_acl_block_binding *binding;
232 list_for_each_entry(binding, &block->binding_list, list)
233 if (binding->mlxsw_sp_port == mlxsw_sp_port &&
234 binding->ingress == ingress)
239 int mlxsw_sp_acl_block_bind(struct mlxsw_sp *mlxsw_sp,
240 struct mlxsw_sp_acl_block *block,
241 struct mlxsw_sp_port *mlxsw_sp_port,
244 struct mlxsw_sp_acl_block_binding *binding;
247 if (WARN_ON(mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress)))
250 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
253 binding->mlxsw_sp_port = mlxsw_sp_port;
254 binding->ingress = ingress;
256 if (mlxsw_sp_acl_ruleset_block_bound(block)) {
257 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
259 goto err_ruleset_bind;
262 list_add(&binding->list, &block->binding_list);
270 int mlxsw_sp_acl_block_unbind(struct mlxsw_sp *mlxsw_sp,
271 struct mlxsw_sp_acl_block *block,
272 struct mlxsw_sp_port *mlxsw_sp_port,
275 struct mlxsw_sp_acl_block_binding *binding;
277 binding = mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress);
281 list_del(&binding->list);
283 if (mlxsw_sp_acl_ruleset_block_bound(block))
284 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
290 static struct mlxsw_sp_acl_ruleset *
291 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
292 struct mlxsw_sp_acl_block *block, u32 chain_index,
293 const struct mlxsw_sp_acl_profile_ops *ops,
294 struct mlxsw_afk_element_usage *tmplt_elusage)
296 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
297 struct mlxsw_sp_acl_ruleset *ruleset;
301 alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
302 ruleset = kzalloc(alloc_size, GFP_KERNEL);
304 return ERR_PTR(-ENOMEM);
305 ruleset->ref_count = 1;
306 ruleset->ht_key.block = block;
307 ruleset->ht_key.chain_index = chain_index;
308 ruleset->ht_key.ops = ops;
310 err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
312 goto err_rhashtable_init;
314 err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
317 goto err_ops_ruleset_add;
319 err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
320 mlxsw_sp_acl_ruleset_ht_params);
327 ops->ruleset_del(mlxsw_sp, ruleset->priv);
329 rhashtable_destroy(&ruleset->rule_ht);
335 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
336 struct mlxsw_sp_acl_ruleset *ruleset)
338 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
339 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
341 rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
342 mlxsw_sp_acl_ruleset_ht_params);
343 ops->ruleset_del(mlxsw_sp, ruleset->priv);
344 rhashtable_destroy(&ruleset->rule_ht);
348 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
350 ruleset->ref_count++;
353 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
354 struct mlxsw_sp_acl_ruleset *ruleset)
356 if (--ruleset->ref_count)
358 mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
361 static struct mlxsw_sp_acl_ruleset *
362 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
363 struct mlxsw_sp_acl_block *block, u32 chain_index,
364 const struct mlxsw_sp_acl_profile_ops *ops)
366 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
368 memset(&ht_key, 0, sizeof(ht_key));
369 ht_key.block = block;
370 ht_key.chain_index = chain_index;
372 return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
373 mlxsw_sp_acl_ruleset_ht_params);
376 struct mlxsw_sp_acl_ruleset *
377 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
378 struct mlxsw_sp_acl_block *block, u32 chain_index,
379 enum mlxsw_sp_acl_profile profile)
381 const struct mlxsw_sp_acl_profile_ops *ops;
382 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
383 struct mlxsw_sp_acl_ruleset *ruleset;
385 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
387 return ERR_PTR(-EINVAL);
388 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
390 return ERR_PTR(-ENOENT);
394 struct mlxsw_sp_acl_ruleset *
395 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
396 struct mlxsw_sp_acl_block *block, u32 chain_index,
397 enum mlxsw_sp_acl_profile profile,
398 struct mlxsw_afk_element_usage *tmplt_elusage)
400 const struct mlxsw_sp_acl_profile_ops *ops;
401 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
402 struct mlxsw_sp_acl_ruleset *ruleset;
404 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
406 return ERR_PTR(-EINVAL);
408 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
410 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
413 return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
417 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
418 struct mlxsw_sp_acl_ruleset *ruleset)
420 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
423 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
425 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
427 return ops->ruleset_group_id(ruleset->priv);
430 struct mlxsw_sp_acl_rule_info *
431 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl,
432 struct mlxsw_afa_block *afa_block)
434 struct mlxsw_sp_acl_rule_info *rulei;
437 rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
442 rulei->act_block = afa_block;
446 rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa);
447 if (IS_ERR(rulei->act_block)) {
448 err = PTR_ERR(rulei->act_block);
449 goto err_afa_block_create;
451 rulei->action_created = 1;
454 err_afa_block_create:
459 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei)
461 if (rulei->action_created)
462 mlxsw_afa_block_destroy(rulei->act_block);
466 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
468 return mlxsw_afa_block_commit(rulei->act_block);
471 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
472 unsigned int priority)
474 rulei->priority = priority;
477 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
478 enum mlxsw_afk_element element,
479 u32 key_value, u32 mask_value)
481 mlxsw_afk_values_add_u32(&rulei->values, element,
482 key_value, mask_value);
485 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
486 enum mlxsw_afk_element element,
487 const char *key_value,
488 const char *mask_value, unsigned int len)
490 mlxsw_afk_values_add_buf(&rulei->values, element,
491 key_value, mask_value, len);
494 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
496 return mlxsw_afa_block_continue(rulei->act_block);
499 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
502 return mlxsw_afa_block_jump(rulei->act_block, group_id);
505 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
507 return mlxsw_afa_block_terminate(rulei->act_block);
510 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei)
512 return mlxsw_afa_block_append_drop(rulei->act_block);
515 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
517 return mlxsw_afa_block_append_trap(rulei->act_block,
521 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
522 struct mlxsw_sp_acl_rule_info *rulei,
523 struct net_device *out_dev,
524 struct netlink_ext_ack *extack)
526 struct mlxsw_sp_port *mlxsw_sp_port;
531 if (!mlxsw_sp_port_dev_check(out_dev)) {
532 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
535 mlxsw_sp_port = netdev_priv(out_dev);
536 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp) {
537 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
540 local_port = mlxsw_sp_port->local_port;
543 /* If out_dev is NULL, the caller wants to
544 * set forward to ingress port.
549 return mlxsw_afa_block_append_fwd(rulei->act_block,
550 local_port, in_port, extack);
553 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
554 struct mlxsw_sp_acl_rule_info *rulei,
555 struct mlxsw_sp_acl_block *block,
556 struct net_device *out_dev,
557 struct netlink_ext_ack *extack)
559 struct mlxsw_sp_acl_block_binding *binding;
560 struct mlxsw_sp_port *in_port;
562 if (!list_is_singular(&block->binding_list)) {
563 NL_SET_ERR_MSG_MOD(extack, "Only a single mirror source is allowed");
566 binding = list_first_entry(&block->binding_list,
567 struct mlxsw_sp_acl_block_binding, list);
568 in_port = binding->mlxsw_sp_port;
570 return mlxsw_afa_block_append_mirror(rulei->act_block,
577 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
578 struct mlxsw_sp_acl_rule_info *rulei,
579 u32 action, u16 vid, u16 proto, u8 prio,
580 struct netlink_ext_ack *extack)
584 if (action == FLOW_ACTION_VLAN_MANGLE) {
593 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN protocol");
594 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
599 return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
600 vid, prio, ethertype,
603 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN action");
604 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
609 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
610 struct mlxsw_sp_acl_rule_info *rulei,
611 struct netlink_ext_ack *extack)
613 return mlxsw_afa_block_append_counter(rulei->act_block,
614 &rulei->counter_index, extack);
617 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
618 struct mlxsw_sp_acl_rule_info *rulei,
619 u16 fid, struct netlink_ext_ack *extack)
621 return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
624 struct mlxsw_sp_acl_rule *
625 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
626 struct mlxsw_sp_acl_ruleset *ruleset,
627 unsigned long cookie,
628 struct mlxsw_afa_block *afa_block,
629 struct netlink_ext_ack *extack)
631 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
632 struct mlxsw_sp_acl_rule *rule;
635 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
636 rule = kzalloc(sizeof(*rule) + ops->rule_priv_size,
642 rule->cookie = cookie;
643 rule->ruleset = ruleset;
645 rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl, afa_block);
646 if (IS_ERR(rule->rulei)) {
647 err = PTR_ERR(rule->rulei);
648 goto err_rulei_create;
656 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
660 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
661 struct mlxsw_sp_acl_rule *rule)
663 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
665 mlxsw_sp_acl_rulei_destroy(rule->rulei);
667 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
670 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
671 struct mlxsw_sp_acl_rule *rule)
673 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
674 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
677 err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
681 err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
682 mlxsw_sp_acl_rule_ht_params);
684 goto err_rhashtable_insert;
686 if (!ruleset->ht_key.chain_index &&
687 mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
688 /* We only need ruleset with chain index 0, the implicit
689 * one, to be directly bound to device. The rest of the
690 * rulesets are bound by "Goto action set".
692 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset,
693 ruleset->ht_key.block);
695 goto err_ruleset_block_bind;
698 list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
699 ruleset->ht_key.block->rule_count++;
702 err_ruleset_block_bind:
703 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
704 mlxsw_sp_acl_rule_ht_params);
705 err_rhashtable_insert:
706 ops->rule_del(mlxsw_sp, rule->priv);
710 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
711 struct mlxsw_sp_acl_rule *rule)
713 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
714 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
716 ruleset->ht_key.block->rule_count--;
717 list_del(&rule->list);
718 if (!ruleset->ht_key.chain_index &&
719 mlxsw_sp_acl_ruleset_is_singular(ruleset))
720 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset,
721 ruleset->ht_key.block);
722 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
723 mlxsw_sp_acl_rule_ht_params);
724 ops->rule_del(mlxsw_sp, rule->priv);
727 int mlxsw_sp_acl_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
728 struct mlxsw_sp_acl_rule *rule,
729 struct mlxsw_afa_block *afa_block)
731 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
732 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
733 struct mlxsw_sp_acl_rule_info *rulei;
735 rulei = mlxsw_sp_acl_rule_rulei(rule);
736 rulei->act_block = afa_block;
738 return ops->rule_action_replace(mlxsw_sp, rule->priv, rule->rulei);
741 struct mlxsw_sp_acl_rule *
742 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
743 struct mlxsw_sp_acl_ruleset *ruleset,
744 unsigned long cookie)
746 return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
747 mlxsw_sp_acl_rule_ht_params);
750 struct mlxsw_sp_acl_rule_info *
751 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
756 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
757 struct mlxsw_sp_acl_rule *rule)
759 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
760 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
764 err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
768 rule->last_used = jiffies;
772 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
774 struct mlxsw_sp_acl_rule *rule;
777 /* Protect internal structures from changes */
779 list_for_each_entry(rule, &acl->rules, list) {
780 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
783 goto err_rule_update;
793 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
795 unsigned long interval = acl->rule_activity_update.interval;
797 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
798 msecs_to_jiffies(interval));
801 static void mlxsw_sp_acl_rule_activity_update_work(struct work_struct *work)
803 struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
804 rule_activity_update.dw.work);
807 err = mlxsw_sp_acl_rules_activity_update(acl);
809 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
811 mlxsw_sp_acl_rule_activity_work_schedule(acl);
814 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
815 struct mlxsw_sp_acl_rule *rule,
816 u64 *packets, u64 *bytes, u64 *last_use)
819 struct mlxsw_sp_acl_rule_info *rulei;
824 rulei = mlxsw_sp_acl_rule_rulei(rule);
825 err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
826 ¤t_packets, ¤t_bytes);
830 *packets = current_packets - rule->last_packets;
831 *bytes = current_bytes - rule->last_bytes;
832 *last_use = rule->last_used;
834 rule->last_bytes = current_bytes;
835 rule->last_packets = current_packets;
840 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
842 struct mlxsw_sp_fid *fid;
843 struct mlxsw_sp_acl *acl;
847 alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
848 acl = kzalloc(alloc_size, GFP_KERNEL);
852 acl->mlxsw_sp = mlxsw_sp;
853 acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
861 err = rhashtable_init(&acl->ruleset_ht,
862 &mlxsw_sp_acl_ruleset_ht_params);
864 goto err_rhashtable_init;
866 fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
871 acl->dummy_fid = fid;
873 INIT_LIST_HEAD(&acl->rules);
874 err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
876 goto err_acl_ops_init;
878 /* Create the delayed work for the rule activity_update */
879 INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
880 mlxsw_sp_acl_rule_activity_update_work);
881 acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
882 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
886 mlxsw_sp_fid_put(fid);
888 rhashtable_destroy(&acl->ruleset_ht);
890 mlxsw_afk_destroy(acl->afk);
896 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
898 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
900 cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
901 mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
902 WARN_ON(!list_empty(&acl->rules));
903 mlxsw_sp_fid_put(acl->dummy_fid);
904 rhashtable_destroy(&acl->ruleset_ht);
905 mlxsw_afk_destroy(acl->afk);
909 u32 mlxsw_sp_acl_region_rehash_intrvl_get(struct mlxsw_sp *mlxsw_sp)
911 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
913 return mlxsw_sp_acl_tcam_vregion_rehash_intrvl_get(mlxsw_sp,
917 int mlxsw_sp_acl_region_rehash_intrvl_set(struct mlxsw_sp *mlxsw_sp, u32 val)
919 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
921 return mlxsw_sp_acl_tcam_vregion_rehash_intrvl_set(mlxsw_sp,