2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_acl.c
3 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/list.h>
39 #include <linux/string.h>
40 #include <linux/rhashtable.h>
41 #include <linux/netdevice.h>
42 #include <net/net_namespace.h>
43 #include <net/tc_act/tc_vlan.h>
47 #include "resources.h"
49 #include "core_acl_flex_keys.h"
50 #include "core_acl_flex_actions.h"
51 #include "spectrum_acl_tcam.h"
54 struct mlxsw_sp *mlxsw_sp;
55 struct mlxsw_afk *afk;
56 struct mlxsw_sp_fid *dummy_fid;
57 struct rhashtable ruleset_ht;
58 struct list_head rules;
60 struct delayed_work dw;
61 unsigned long interval; /* ms */
62 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000
63 } rule_activity_update;
64 struct mlxsw_sp_acl_tcam tcam;
67 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl)
72 struct mlxsw_sp_acl_block_binding {
73 struct list_head list;
74 struct net_device *dev;
75 struct mlxsw_sp_port *mlxsw_sp_port;
79 struct mlxsw_sp_acl_block {
80 struct list_head binding_list;
81 struct mlxsw_sp_acl_ruleset *ruleset_zero;
82 struct mlxsw_sp *mlxsw_sp;
83 unsigned int rule_count;
84 unsigned int disable_count;
87 struct mlxsw_sp_acl_ruleset_ht_key {
88 struct mlxsw_sp_acl_block *block;
90 const struct mlxsw_sp_acl_profile_ops *ops;
93 struct mlxsw_sp_acl_ruleset {
94 struct rhash_head ht_node; /* Member of acl HT */
95 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
96 struct rhashtable rule_ht;
97 unsigned int ref_count;
98 unsigned long priv[0];
99 /* priv has to be always the last item */
102 struct mlxsw_sp_acl_rule {
103 struct rhash_head ht_node; /* Member of rule HT */
104 struct list_head list;
105 unsigned long cookie; /* HT key */
106 struct mlxsw_sp_acl_ruleset *ruleset;
107 struct mlxsw_sp_acl_rule_info *rulei;
111 unsigned long priv[0];
112 /* priv has to be always the last item */
115 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
116 .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
117 .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
118 .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
119 .automatic_shrinking = true,
122 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
123 .key_len = sizeof(unsigned long),
124 .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
125 .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
126 .automatic_shrinking = true,
129 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
131 return mlxsw_sp->acl->dummy_fid;
134 struct mlxsw_sp *mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block *block)
136 return block->mlxsw_sp;
139 unsigned int mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block *block)
141 return block ? block->rule_count : 0;
144 void mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block *block)
147 block->disable_count++;
150 void mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block *block)
153 block->disable_count--;
156 bool mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block *block)
158 return block->disable_count;
162 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
164 /* We hold a reference on ruleset ourselves */
165 return ruleset->ref_count == 2;
169 mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
170 struct mlxsw_sp_acl_block *block,
171 struct mlxsw_sp_acl_block_binding *binding)
173 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
174 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
176 return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
177 binding->mlxsw_sp_port, binding->ingress);
181 mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
182 struct mlxsw_sp_acl_block *block,
183 struct mlxsw_sp_acl_block_binding *binding)
185 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
186 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
188 ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
189 binding->mlxsw_sp_port, binding->ingress);
192 static bool mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block *block)
194 return block->ruleset_zero;
198 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
199 struct mlxsw_sp_acl_ruleset *ruleset,
200 struct mlxsw_sp_acl_block *block)
202 struct mlxsw_sp_acl_block_binding *binding;
205 block->ruleset_zero = ruleset;
206 list_for_each_entry(binding, &block->binding_list, list) {
207 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
214 list_for_each_entry_continue_reverse(binding, &block->binding_list,
216 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
217 block->ruleset_zero = NULL;
223 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
224 struct mlxsw_sp_acl_ruleset *ruleset,
225 struct mlxsw_sp_acl_block *block)
227 struct mlxsw_sp_acl_block_binding *binding;
229 list_for_each_entry(binding, &block->binding_list, list)
230 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
231 block->ruleset_zero = NULL;
234 struct mlxsw_sp_acl_block *mlxsw_sp_acl_block_create(struct mlxsw_sp *mlxsw_sp,
237 struct mlxsw_sp_acl_block *block;
239 block = kzalloc(sizeof(*block), GFP_KERNEL);
242 INIT_LIST_HEAD(&block->binding_list);
243 block->mlxsw_sp = mlxsw_sp;
247 void mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block *block)
249 WARN_ON(!list_empty(&block->binding_list));
253 static struct mlxsw_sp_acl_block_binding *
254 mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block *block,
255 struct mlxsw_sp_port *mlxsw_sp_port, bool ingress)
257 struct mlxsw_sp_acl_block_binding *binding;
259 list_for_each_entry(binding, &block->binding_list, list)
260 if (binding->mlxsw_sp_port == mlxsw_sp_port &&
261 binding->ingress == ingress)
266 int mlxsw_sp_acl_block_bind(struct mlxsw_sp *mlxsw_sp,
267 struct mlxsw_sp_acl_block *block,
268 struct mlxsw_sp_port *mlxsw_sp_port,
271 struct mlxsw_sp_acl_block_binding *binding;
274 if (WARN_ON(mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress)))
277 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
280 binding->mlxsw_sp_port = mlxsw_sp_port;
281 binding->ingress = ingress;
283 if (mlxsw_sp_acl_ruleset_block_bound(block)) {
284 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
286 goto err_ruleset_bind;
289 list_add(&binding->list, &block->binding_list);
297 int mlxsw_sp_acl_block_unbind(struct mlxsw_sp *mlxsw_sp,
298 struct mlxsw_sp_acl_block *block,
299 struct mlxsw_sp_port *mlxsw_sp_port,
302 struct mlxsw_sp_acl_block_binding *binding;
304 binding = mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress);
308 list_del(&binding->list);
310 if (mlxsw_sp_acl_ruleset_block_bound(block))
311 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
317 static struct mlxsw_sp_acl_ruleset *
318 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
319 struct mlxsw_sp_acl_block *block, u32 chain_index,
320 const struct mlxsw_sp_acl_profile_ops *ops,
321 struct mlxsw_afk_element_usage *tmplt_elusage)
323 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
324 struct mlxsw_sp_acl_ruleset *ruleset;
328 alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
329 ruleset = kzalloc(alloc_size, GFP_KERNEL);
331 return ERR_PTR(-ENOMEM);
332 ruleset->ref_count = 1;
333 ruleset->ht_key.block = block;
334 ruleset->ht_key.chain_index = chain_index;
335 ruleset->ht_key.ops = ops;
337 err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
339 goto err_rhashtable_init;
341 err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
344 goto err_ops_ruleset_add;
346 err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
347 mlxsw_sp_acl_ruleset_ht_params);
354 ops->ruleset_del(mlxsw_sp, ruleset->priv);
356 rhashtable_destroy(&ruleset->rule_ht);
362 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
363 struct mlxsw_sp_acl_ruleset *ruleset)
365 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
366 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
368 rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
369 mlxsw_sp_acl_ruleset_ht_params);
370 ops->ruleset_del(mlxsw_sp, ruleset->priv);
371 rhashtable_destroy(&ruleset->rule_ht);
375 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
377 ruleset->ref_count++;
380 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
381 struct mlxsw_sp_acl_ruleset *ruleset)
383 if (--ruleset->ref_count)
385 mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
388 static struct mlxsw_sp_acl_ruleset *
389 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
390 struct mlxsw_sp_acl_block *block, u32 chain_index,
391 const struct mlxsw_sp_acl_profile_ops *ops)
393 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
395 memset(&ht_key, 0, sizeof(ht_key));
396 ht_key.block = block;
397 ht_key.chain_index = chain_index;
399 return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
400 mlxsw_sp_acl_ruleset_ht_params);
403 struct mlxsw_sp_acl_ruleset *
404 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
405 struct mlxsw_sp_acl_block *block, u32 chain_index,
406 enum mlxsw_sp_acl_profile profile)
408 const struct mlxsw_sp_acl_profile_ops *ops;
409 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
410 struct mlxsw_sp_acl_ruleset *ruleset;
412 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
414 return ERR_PTR(-EINVAL);
415 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
417 return ERR_PTR(-ENOENT);
421 struct mlxsw_sp_acl_ruleset *
422 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
423 struct mlxsw_sp_acl_block *block, u32 chain_index,
424 enum mlxsw_sp_acl_profile profile,
425 struct mlxsw_afk_element_usage *tmplt_elusage)
427 const struct mlxsw_sp_acl_profile_ops *ops;
428 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
429 struct mlxsw_sp_acl_ruleset *ruleset;
431 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
433 return ERR_PTR(-EINVAL);
435 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
437 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
440 return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
444 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
445 struct mlxsw_sp_acl_ruleset *ruleset)
447 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
450 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
452 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
454 return ops->ruleset_group_id(ruleset->priv);
457 struct mlxsw_sp_acl_rule_info *
458 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl)
460 struct mlxsw_sp_acl_rule_info *rulei;
463 rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
466 rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa);
467 if (IS_ERR(rulei->act_block)) {
468 err = PTR_ERR(rulei->act_block);
469 goto err_afa_block_create;
473 err_afa_block_create:
478 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei)
480 mlxsw_afa_block_destroy(rulei->act_block);
484 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
486 return mlxsw_afa_block_commit(rulei->act_block);
489 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
490 unsigned int priority)
492 rulei->priority = priority >> 16;
495 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
496 enum mlxsw_afk_element element,
497 u32 key_value, u32 mask_value)
499 mlxsw_afk_values_add_u32(&rulei->values, element,
500 key_value, mask_value);
503 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
504 enum mlxsw_afk_element element,
505 const char *key_value,
506 const char *mask_value, unsigned int len)
508 mlxsw_afk_values_add_buf(&rulei->values, element,
509 key_value, mask_value, len);
512 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
514 return mlxsw_afa_block_continue(rulei->act_block);
517 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
520 return mlxsw_afa_block_jump(rulei->act_block, group_id);
523 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
525 return mlxsw_afa_block_terminate(rulei->act_block);
528 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei)
530 return mlxsw_afa_block_append_drop(rulei->act_block);
533 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
535 return mlxsw_afa_block_append_trap(rulei->act_block,
539 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
540 struct mlxsw_sp_acl_rule_info *rulei,
541 struct net_device *out_dev,
542 struct netlink_ext_ack *extack)
544 struct mlxsw_sp_port *mlxsw_sp_port;
549 if (!mlxsw_sp_port_dev_check(out_dev))
551 mlxsw_sp_port = netdev_priv(out_dev);
552 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp)
554 local_port = mlxsw_sp_port->local_port;
557 /* If out_dev is NULL, the caller wants to
558 * set forward to ingress port.
563 return mlxsw_afa_block_append_fwd(rulei->act_block,
564 local_port, in_port, extack);
567 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
568 struct mlxsw_sp_acl_rule_info *rulei,
569 struct mlxsw_sp_acl_block *block,
570 struct net_device *out_dev,
571 struct netlink_ext_ack *extack)
573 struct mlxsw_sp_acl_block_binding *binding;
574 struct mlxsw_sp_port *in_port;
576 if (!list_is_singular(&block->binding_list))
579 binding = list_first_entry(&block->binding_list,
580 struct mlxsw_sp_acl_block_binding, list);
581 in_port = binding->mlxsw_sp_port;
583 return mlxsw_afa_block_append_mirror(rulei->act_block,
590 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
591 struct mlxsw_sp_acl_rule_info *rulei,
592 u32 action, u16 vid, u16 proto, u8 prio,
593 struct netlink_ext_ack *extack)
597 if (action == TCA_VLAN_ACT_MODIFY) {
606 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
611 return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
612 vid, prio, ethertype,
615 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
620 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
621 struct mlxsw_sp_acl_rule_info *rulei,
622 struct netlink_ext_ack *extack)
624 return mlxsw_afa_block_append_counter(rulei->act_block,
625 &rulei->counter_index, extack);
628 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
629 struct mlxsw_sp_acl_rule_info *rulei,
630 u16 fid, struct netlink_ext_ack *extack)
632 return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
635 struct mlxsw_sp_acl_rule *
636 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
637 struct mlxsw_sp_acl_ruleset *ruleset,
638 unsigned long cookie,
639 struct netlink_ext_ack *extack)
641 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
642 struct mlxsw_sp_acl_rule *rule;
645 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
646 rule = kzalloc(sizeof(*rule) + ops->rule_priv_size(mlxsw_sp),
652 rule->cookie = cookie;
653 rule->ruleset = ruleset;
655 rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
656 if (IS_ERR(rule->rulei)) {
657 err = PTR_ERR(rule->rulei);
658 goto err_rulei_create;
666 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
670 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
671 struct mlxsw_sp_acl_rule *rule)
673 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
675 mlxsw_sp_acl_rulei_destroy(rule->rulei);
677 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
680 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
681 struct mlxsw_sp_acl_rule *rule)
683 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
684 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
687 err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
691 err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
692 mlxsw_sp_acl_rule_ht_params);
694 goto err_rhashtable_insert;
696 if (!ruleset->ht_key.chain_index &&
697 mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
698 /* We only need ruleset with chain index 0, the implicit
699 * one, to be directly bound to device. The rest of the
700 * rulesets are bound by "Goto action set".
702 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset,
703 ruleset->ht_key.block);
705 goto err_ruleset_block_bind;
708 list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
709 ruleset->ht_key.block->rule_count++;
712 err_ruleset_block_bind:
713 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
714 mlxsw_sp_acl_rule_ht_params);
715 err_rhashtable_insert:
716 ops->rule_del(mlxsw_sp, rule->priv);
720 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
721 struct mlxsw_sp_acl_rule *rule)
723 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
724 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
726 ruleset->ht_key.block->rule_count--;
727 list_del(&rule->list);
728 if (!ruleset->ht_key.chain_index &&
729 mlxsw_sp_acl_ruleset_is_singular(ruleset))
730 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset,
731 ruleset->ht_key.block);
732 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
733 mlxsw_sp_acl_rule_ht_params);
734 ops->rule_del(mlxsw_sp, rule->priv);
737 struct mlxsw_sp_acl_rule *
738 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
739 struct mlxsw_sp_acl_ruleset *ruleset,
740 unsigned long cookie)
742 return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
743 mlxsw_sp_acl_rule_ht_params);
746 struct mlxsw_sp_acl_rule_info *
747 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
752 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
753 struct mlxsw_sp_acl_rule *rule)
755 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
756 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
760 err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
764 rule->last_used = jiffies;
768 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
770 struct mlxsw_sp_acl_rule *rule;
773 /* Protect internal structures from changes */
775 list_for_each_entry(rule, &acl->rules, list) {
776 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
779 goto err_rule_update;
789 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
791 unsigned long interval = acl->rule_activity_update.interval;
793 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
794 msecs_to_jiffies(interval));
797 static void mlxsw_sp_acl_rul_activity_update_work(struct work_struct *work)
799 struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
800 rule_activity_update.dw.work);
803 err = mlxsw_sp_acl_rules_activity_update(acl);
805 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
807 mlxsw_sp_acl_rule_activity_work_schedule(acl);
810 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
811 struct mlxsw_sp_acl_rule *rule,
812 u64 *packets, u64 *bytes, u64 *last_use)
815 struct mlxsw_sp_acl_rule_info *rulei;
820 rulei = mlxsw_sp_acl_rule_rulei(rule);
821 err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
822 ¤t_packets, ¤t_bytes);
826 *packets = current_packets - rule->last_packets;
827 *bytes = current_bytes - rule->last_bytes;
828 *last_use = rule->last_used;
830 rule->last_bytes = current_bytes;
831 rule->last_packets = current_packets;
836 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
838 struct mlxsw_sp_fid *fid;
839 struct mlxsw_sp_acl *acl;
843 alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
844 acl = kzalloc(alloc_size, GFP_KERNEL);
848 acl->mlxsw_sp = mlxsw_sp;
849 acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
857 err = rhashtable_init(&acl->ruleset_ht,
858 &mlxsw_sp_acl_ruleset_ht_params);
860 goto err_rhashtable_init;
862 fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
867 acl->dummy_fid = fid;
869 INIT_LIST_HEAD(&acl->rules);
870 err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
872 goto err_acl_ops_init;
874 /* Create the delayed work for the rule activity_update */
875 INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
876 mlxsw_sp_acl_rul_activity_update_work);
877 acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
878 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
882 mlxsw_sp_fid_put(fid);
884 rhashtable_destroy(&acl->ruleset_ht);
886 mlxsw_afk_destroy(acl->afk);
892 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
894 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
896 cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
897 mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
898 WARN_ON(!list_empty(&acl->rules));
899 mlxsw_sp_fid_put(acl->dummy_fid);
900 rhashtable_destroy(&acl->ruleset_ht);
901 mlxsw_afk_destroy(acl->afk);