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_block {
49 struct list_head binding_list;
50 struct mlxsw_sp_acl_ruleset *ruleset_zero;
51 struct mlxsw_sp *mlxsw_sp;
52 unsigned int rule_count;
53 unsigned int disable_count;
56 struct mlxsw_sp_acl_ruleset_ht_key {
57 struct mlxsw_sp_acl_block *block;
59 const struct mlxsw_sp_acl_profile_ops *ops;
62 struct mlxsw_sp_acl_ruleset {
63 struct rhash_head ht_node; /* Member of acl HT */
64 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
65 struct rhashtable rule_ht;
66 unsigned int ref_count;
67 unsigned long priv[0];
68 /* priv has to be always the last item */
71 struct mlxsw_sp_acl_rule {
72 struct rhash_head ht_node; /* Member of rule HT */
73 struct list_head list;
74 unsigned long cookie; /* HT key */
75 struct mlxsw_sp_acl_ruleset *ruleset;
76 struct mlxsw_sp_acl_rule_info *rulei;
80 unsigned long priv[0];
81 /* priv has to be always the last item */
84 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
85 .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
86 .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
87 .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
88 .automatic_shrinking = true,
91 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
92 .key_len = sizeof(unsigned long),
93 .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
94 .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
95 .automatic_shrinking = true,
98 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
100 return mlxsw_sp->acl->dummy_fid;
103 struct mlxsw_sp *mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block *block)
105 return block->mlxsw_sp;
108 unsigned int mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block *block)
110 return block ? block->rule_count : 0;
113 void mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block *block)
116 block->disable_count++;
119 void mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block *block)
122 block->disable_count--;
125 bool mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block *block)
127 return block->disable_count;
130 bool mlxsw_sp_acl_block_is_egress_bound(struct mlxsw_sp_acl_block *block)
132 struct mlxsw_sp_acl_block_binding *binding;
134 list_for_each_entry(binding, &block->binding_list, list) {
135 if (!binding->ingress)
142 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
144 /* We hold a reference on ruleset ourselves */
145 return ruleset->ref_count == 2;
149 mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
150 struct mlxsw_sp_acl_block *block,
151 struct mlxsw_sp_acl_block_binding *binding)
153 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
154 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
156 return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
157 binding->mlxsw_sp_port, binding->ingress);
161 mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
162 struct mlxsw_sp_acl_block *block,
163 struct mlxsw_sp_acl_block_binding *binding)
165 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
166 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
168 ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
169 binding->mlxsw_sp_port, binding->ingress);
172 static bool mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block *block)
174 return block->ruleset_zero;
178 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
179 struct mlxsw_sp_acl_ruleset *ruleset,
180 struct mlxsw_sp_acl_block *block)
182 struct mlxsw_sp_acl_block_binding *binding;
185 block->ruleset_zero = ruleset;
186 list_for_each_entry(binding, &block->binding_list, list) {
187 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
194 list_for_each_entry_continue_reverse(binding, &block->binding_list,
196 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
197 block->ruleset_zero = NULL;
203 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
204 struct mlxsw_sp_acl_ruleset *ruleset,
205 struct mlxsw_sp_acl_block *block)
207 struct mlxsw_sp_acl_block_binding *binding;
209 list_for_each_entry(binding, &block->binding_list, list)
210 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
211 block->ruleset_zero = NULL;
214 struct mlxsw_sp_acl_block *mlxsw_sp_acl_block_create(struct mlxsw_sp *mlxsw_sp,
217 struct mlxsw_sp_acl_block *block;
219 block = kzalloc(sizeof(*block), GFP_KERNEL);
222 INIT_LIST_HEAD(&block->binding_list);
223 block->mlxsw_sp = mlxsw_sp;
227 void mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block *block)
229 WARN_ON(!list_empty(&block->binding_list));
233 static struct mlxsw_sp_acl_block_binding *
234 mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block *block,
235 struct mlxsw_sp_port *mlxsw_sp_port, bool ingress)
237 struct mlxsw_sp_acl_block_binding *binding;
239 list_for_each_entry(binding, &block->binding_list, list)
240 if (binding->mlxsw_sp_port == mlxsw_sp_port &&
241 binding->ingress == ingress)
246 int mlxsw_sp_acl_block_bind(struct mlxsw_sp *mlxsw_sp,
247 struct mlxsw_sp_acl_block *block,
248 struct mlxsw_sp_port *mlxsw_sp_port,
251 struct mlxsw_sp_acl_block_binding *binding;
254 if (WARN_ON(mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress)))
257 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
260 binding->mlxsw_sp_port = mlxsw_sp_port;
261 binding->ingress = ingress;
263 if (mlxsw_sp_acl_ruleset_block_bound(block)) {
264 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
266 goto err_ruleset_bind;
269 list_add(&binding->list, &block->binding_list);
277 int mlxsw_sp_acl_block_unbind(struct mlxsw_sp *mlxsw_sp,
278 struct mlxsw_sp_acl_block *block,
279 struct mlxsw_sp_port *mlxsw_sp_port,
282 struct mlxsw_sp_acl_block_binding *binding;
284 binding = mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress);
288 list_del(&binding->list);
290 if (mlxsw_sp_acl_ruleset_block_bound(block))
291 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
297 static struct mlxsw_sp_acl_ruleset *
298 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
299 struct mlxsw_sp_acl_block *block, u32 chain_index,
300 const struct mlxsw_sp_acl_profile_ops *ops,
301 struct mlxsw_afk_element_usage *tmplt_elusage)
303 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
304 struct mlxsw_sp_acl_ruleset *ruleset;
308 alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
309 ruleset = kzalloc(alloc_size, GFP_KERNEL);
311 return ERR_PTR(-ENOMEM);
312 ruleset->ref_count = 1;
313 ruleset->ht_key.block = block;
314 ruleset->ht_key.chain_index = chain_index;
315 ruleset->ht_key.ops = ops;
317 err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
319 goto err_rhashtable_init;
321 err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
324 goto err_ops_ruleset_add;
326 err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
327 mlxsw_sp_acl_ruleset_ht_params);
334 ops->ruleset_del(mlxsw_sp, ruleset->priv);
336 rhashtable_destroy(&ruleset->rule_ht);
342 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
343 struct mlxsw_sp_acl_ruleset *ruleset)
345 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
346 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
348 rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
349 mlxsw_sp_acl_ruleset_ht_params);
350 ops->ruleset_del(mlxsw_sp, ruleset->priv);
351 rhashtable_destroy(&ruleset->rule_ht);
355 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
357 ruleset->ref_count++;
360 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
361 struct mlxsw_sp_acl_ruleset *ruleset)
363 if (--ruleset->ref_count)
365 mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
368 static struct mlxsw_sp_acl_ruleset *
369 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
370 struct mlxsw_sp_acl_block *block, u32 chain_index,
371 const struct mlxsw_sp_acl_profile_ops *ops)
373 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
375 memset(&ht_key, 0, sizeof(ht_key));
376 ht_key.block = block;
377 ht_key.chain_index = chain_index;
379 return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
380 mlxsw_sp_acl_ruleset_ht_params);
383 struct mlxsw_sp_acl_ruleset *
384 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
385 struct mlxsw_sp_acl_block *block, u32 chain_index,
386 enum mlxsw_sp_acl_profile profile)
388 const struct mlxsw_sp_acl_profile_ops *ops;
389 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
390 struct mlxsw_sp_acl_ruleset *ruleset;
392 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
394 return ERR_PTR(-EINVAL);
395 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
397 return ERR_PTR(-ENOENT);
401 struct mlxsw_sp_acl_ruleset *
402 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
403 struct mlxsw_sp_acl_block *block, u32 chain_index,
404 enum mlxsw_sp_acl_profile profile,
405 struct mlxsw_afk_element_usage *tmplt_elusage)
407 const struct mlxsw_sp_acl_profile_ops *ops;
408 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
409 struct mlxsw_sp_acl_ruleset *ruleset;
411 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
413 return ERR_PTR(-EINVAL);
415 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
417 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
420 return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
424 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
425 struct mlxsw_sp_acl_ruleset *ruleset)
427 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
430 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
432 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
434 return ops->ruleset_group_id(ruleset->priv);
437 struct mlxsw_sp_acl_rule_info *
438 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl)
440 struct mlxsw_sp_acl_rule_info *rulei;
443 rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
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;
453 err_afa_block_create:
458 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei)
460 mlxsw_afa_block_destroy(rulei->act_block);
464 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
466 return mlxsw_afa_block_commit(rulei->act_block);
469 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
470 unsigned int priority)
472 rulei->priority = priority >> 16;
475 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
476 enum mlxsw_afk_element element,
477 u32 key_value, u32 mask_value)
479 mlxsw_afk_values_add_u32(&rulei->values, element,
480 key_value, mask_value);
483 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
484 enum mlxsw_afk_element element,
485 const char *key_value,
486 const char *mask_value, unsigned int len)
488 mlxsw_afk_values_add_buf(&rulei->values, element,
489 key_value, mask_value, len);
492 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
494 return mlxsw_afa_block_continue(rulei->act_block);
497 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
500 return mlxsw_afa_block_jump(rulei->act_block, group_id);
503 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
505 return mlxsw_afa_block_terminate(rulei->act_block);
508 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei)
510 return mlxsw_afa_block_append_drop(rulei->act_block);
513 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
515 return mlxsw_afa_block_append_trap(rulei->act_block,
519 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
520 struct mlxsw_sp_acl_rule_info *rulei,
521 struct net_device *out_dev,
522 struct netlink_ext_ack *extack)
524 struct mlxsw_sp_port *mlxsw_sp_port;
529 if (!mlxsw_sp_port_dev_check(out_dev)) {
530 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
533 mlxsw_sp_port = netdev_priv(out_dev);
534 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp) {
535 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
538 local_port = mlxsw_sp_port->local_port;
541 /* If out_dev is NULL, the caller wants to
542 * set forward to ingress port.
547 return mlxsw_afa_block_append_fwd(rulei->act_block,
548 local_port, in_port, extack);
551 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
552 struct mlxsw_sp_acl_rule_info *rulei,
553 struct mlxsw_sp_acl_block *block,
554 struct net_device *out_dev,
555 struct netlink_ext_ack *extack)
557 struct mlxsw_sp_acl_block_binding *binding;
558 struct mlxsw_sp_port *in_port;
560 if (!list_is_singular(&block->binding_list)) {
561 NL_SET_ERR_MSG_MOD(extack, "Only a single mirror source is allowed");
564 binding = list_first_entry(&block->binding_list,
565 struct mlxsw_sp_acl_block_binding, list);
566 in_port = binding->mlxsw_sp_port;
568 return mlxsw_afa_block_append_mirror(rulei->act_block,
575 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
576 struct mlxsw_sp_acl_rule_info *rulei,
577 u32 action, u16 vid, u16 proto, u8 prio,
578 struct netlink_ext_ack *extack)
582 if (action == TCA_VLAN_ACT_MODIFY) {
591 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN protocol");
592 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
597 return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
598 vid, prio, ethertype,
601 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN action");
602 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
607 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
608 struct mlxsw_sp_acl_rule_info *rulei,
609 struct netlink_ext_ack *extack)
611 return mlxsw_afa_block_append_counter(rulei->act_block,
612 &rulei->counter_index, extack);
615 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
616 struct mlxsw_sp_acl_rule_info *rulei,
617 u16 fid, struct netlink_ext_ack *extack)
619 return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
622 struct mlxsw_sp_acl_rule *
623 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
624 struct mlxsw_sp_acl_ruleset *ruleset,
625 unsigned long cookie,
626 struct netlink_ext_ack *extack)
628 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
629 struct mlxsw_sp_acl_rule *rule;
632 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
633 rule = kzalloc(sizeof(*rule) + ops->rule_priv_size(mlxsw_sp),
639 rule->cookie = cookie;
640 rule->ruleset = ruleset;
642 rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
643 if (IS_ERR(rule->rulei)) {
644 err = PTR_ERR(rule->rulei);
645 goto err_rulei_create;
653 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
657 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
658 struct mlxsw_sp_acl_rule *rule)
660 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
662 mlxsw_sp_acl_rulei_destroy(rule->rulei);
664 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
667 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
668 struct mlxsw_sp_acl_rule *rule)
670 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
671 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
674 err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
678 err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
679 mlxsw_sp_acl_rule_ht_params);
681 goto err_rhashtable_insert;
683 if (!ruleset->ht_key.chain_index &&
684 mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
685 /* We only need ruleset with chain index 0, the implicit
686 * one, to be directly bound to device. The rest of the
687 * rulesets are bound by "Goto action set".
689 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset,
690 ruleset->ht_key.block);
692 goto err_ruleset_block_bind;
695 list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
696 ruleset->ht_key.block->rule_count++;
699 err_ruleset_block_bind:
700 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
701 mlxsw_sp_acl_rule_ht_params);
702 err_rhashtable_insert:
703 ops->rule_del(mlxsw_sp, rule->priv);
707 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
708 struct mlxsw_sp_acl_rule *rule)
710 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
711 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
713 ruleset->ht_key.block->rule_count--;
714 list_del(&rule->list);
715 if (!ruleset->ht_key.chain_index &&
716 mlxsw_sp_acl_ruleset_is_singular(ruleset))
717 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset,
718 ruleset->ht_key.block);
719 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
720 mlxsw_sp_acl_rule_ht_params);
721 ops->rule_del(mlxsw_sp, rule->priv);
724 struct mlxsw_sp_acl_rule *
725 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
726 struct mlxsw_sp_acl_ruleset *ruleset,
727 unsigned long cookie)
729 return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
730 mlxsw_sp_acl_rule_ht_params);
733 struct mlxsw_sp_acl_rule_info *
734 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
739 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
740 struct mlxsw_sp_acl_rule *rule)
742 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
743 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
747 err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
751 rule->last_used = jiffies;
755 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
757 struct mlxsw_sp_acl_rule *rule;
760 /* Protect internal structures from changes */
762 list_for_each_entry(rule, &acl->rules, list) {
763 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
766 goto err_rule_update;
776 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
778 unsigned long interval = acl->rule_activity_update.interval;
780 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
781 msecs_to_jiffies(interval));
784 static void mlxsw_sp_acl_rul_activity_update_work(struct work_struct *work)
786 struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
787 rule_activity_update.dw.work);
790 err = mlxsw_sp_acl_rules_activity_update(acl);
792 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
794 mlxsw_sp_acl_rule_activity_work_schedule(acl);
797 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
798 struct mlxsw_sp_acl_rule *rule,
799 u64 *packets, u64 *bytes, u64 *last_use)
802 struct mlxsw_sp_acl_rule_info *rulei;
807 rulei = mlxsw_sp_acl_rule_rulei(rule);
808 err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
809 ¤t_packets, ¤t_bytes);
813 *packets = current_packets - rule->last_packets;
814 *bytes = current_bytes - rule->last_bytes;
815 *last_use = rule->last_used;
817 rule->last_bytes = current_bytes;
818 rule->last_packets = current_packets;
823 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
825 struct mlxsw_sp_fid *fid;
826 struct mlxsw_sp_acl *acl;
830 alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
831 acl = kzalloc(alloc_size, GFP_KERNEL);
835 acl->mlxsw_sp = mlxsw_sp;
836 acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
844 err = rhashtable_init(&acl->ruleset_ht,
845 &mlxsw_sp_acl_ruleset_ht_params);
847 goto err_rhashtable_init;
849 fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
854 acl->dummy_fid = fid;
856 INIT_LIST_HEAD(&acl->rules);
857 err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
859 goto err_acl_ops_init;
861 /* Create the delayed work for the rule activity_update */
862 INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
863 mlxsw_sp_acl_rul_activity_update_work);
864 acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
865 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
869 mlxsw_sp_fid_put(fid);
871 rhashtable_destroy(&acl->ruleset_ht);
873 mlxsw_afk_destroy(acl->afk);
879 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
881 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
883 cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
884 mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
885 WARN_ON(!list_empty(&acl->rules));
886 mlxsw_sp_fid_put(acl->dummy_fid);
887 rhashtable_destroy(&acl->ruleset_ht);
888 mlxsw_afk_destroy(acl->afk);