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 <linux/mutex.h>
12 #include <net/net_namespace.h>
13 #include <net/tc_act/tc_vlan.h>
17 #include "resources.h"
19 #include "core_acl_flex_keys.h"
20 #include "core_acl_flex_actions.h"
21 #include "spectrum_acl_tcam.h"
24 struct mlxsw_sp *mlxsw_sp;
25 struct mlxsw_afk *afk;
26 struct mlxsw_sp_fid *dummy_fid;
27 struct rhashtable ruleset_ht;
28 struct list_head rules;
29 struct mutex rules_lock; /* Protects rules list */
31 struct delayed_work dw;
32 unsigned long interval; /* ms */
33 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000
34 } rule_activity_update;
35 struct mlxsw_sp_acl_tcam tcam;
38 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl)
43 struct mlxsw_sp_acl_tcam *mlxsw_sp_acl_to_tcam(struct mlxsw_sp_acl *acl)
48 struct mlxsw_sp_acl_ruleset_ht_key {
49 struct mlxsw_sp_flow_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 int min_prio;
60 unsigned int max_prio;
62 /* priv has to be always the last item */
65 struct mlxsw_sp_acl_rule {
66 struct rhash_head ht_node; /* Member of rule HT */
67 struct list_head list;
68 unsigned long cookie; /* HT key */
69 struct mlxsw_sp_acl_ruleset *ruleset;
70 struct mlxsw_sp_acl_rule_info *rulei;
76 /* priv has to be always the last item */
79 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
80 .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
81 .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
82 .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
83 .automatic_shrinking = true,
86 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
87 .key_len = sizeof(unsigned long),
88 .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
89 .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
90 .automatic_shrinking = true,
93 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
95 return mlxsw_sp->acl->dummy_fid;
99 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
101 /* We hold a reference on ruleset ourselves */
102 return ruleset->ref_count == 2;
105 int mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
106 struct mlxsw_sp_flow_block *block,
107 struct mlxsw_sp_flow_block_binding *binding)
109 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
110 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
112 return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
113 binding->mlxsw_sp_port, binding->ingress);
116 void mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
117 struct mlxsw_sp_flow_block *block,
118 struct mlxsw_sp_flow_block_binding *binding)
120 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
121 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
123 ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
124 binding->mlxsw_sp_port, binding->ingress);
128 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
129 struct mlxsw_sp_acl_ruleset *ruleset,
130 struct mlxsw_sp_flow_block *block)
132 struct mlxsw_sp_flow_block_binding *binding;
135 block->ruleset_zero = ruleset;
136 list_for_each_entry(binding, &block->binding_list, list) {
137 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
144 list_for_each_entry_continue_reverse(binding, &block->binding_list,
146 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
147 block->ruleset_zero = NULL;
153 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
154 struct mlxsw_sp_acl_ruleset *ruleset,
155 struct mlxsw_sp_flow_block *block)
157 struct mlxsw_sp_flow_block_binding *binding;
159 list_for_each_entry(binding, &block->binding_list, list)
160 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
161 block->ruleset_zero = NULL;
164 static struct mlxsw_sp_acl_ruleset *
165 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
166 struct mlxsw_sp_flow_block *block, u32 chain_index,
167 const struct mlxsw_sp_acl_profile_ops *ops,
168 struct mlxsw_afk_element_usage *tmplt_elusage)
170 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
171 struct mlxsw_sp_acl_ruleset *ruleset;
175 alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
176 ruleset = kzalloc(alloc_size, GFP_KERNEL);
178 return ERR_PTR(-ENOMEM);
179 ruleset->ref_count = 1;
180 ruleset->ht_key.block = block;
181 ruleset->ht_key.chain_index = chain_index;
182 ruleset->ht_key.ops = ops;
184 err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
186 goto err_rhashtable_init;
188 err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
189 tmplt_elusage, &ruleset->min_prio,
192 goto err_ops_ruleset_add;
194 err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
195 mlxsw_sp_acl_ruleset_ht_params);
202 ops->ruleset_del(mlxsw_sp, ruleset->priv);
204 rhashtable_destroy(&ruleset->rule_ht);
210 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
211 struct mlxsw_sp_acl_ruleset *ruleset)
213 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
214 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
216 rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
217 mlxsw_sp_acl_ruleset_ht_params);
218 ops->ruleset_del(mlxsw_sp, ruleset->priv);
219 rhashtable_destroy(&ruleset->rule_ht);
223 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
225 ruleset->ref_count++;
228 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
229 struct mlxsw_sp_acl_ruleset *ruleset)
231 if (--ruleset->ref_count)
233 mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
236 static struct mlxsw_sp_acl_ruleset *
237 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
238 struct mlxsw_sp_flow_block *block, u32 chain_index,
239 const struct mlxsw_sp_acl_profile_ops *ops)
241 struct mlxsw_sp_acl_ruleset_ht_key ht_key;
243 memset(&ht_key, 0, sizeof(ht_key));
244 ht_key.block = block;
245 ht_key.chain_index = chain_index;
247 return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
248 mlxsw_sp_acl_ruleset_ht_params);
251 struct mlxsw_sp_acl_ruleset *
252 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
253 struct mlxsw_sp_flow_block *block, u32 chain_index,
254 enum mlxsw_sp_acl_profile profile)
256 const struct mlxsw_sp_acl_profile_ops *ops;
257 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
258 struct mlxsw_sp_acl_ruleset *ruleset;
260 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
262 return ERR_PTR(-EINVAL);
263 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
265 return ERR_PTR(-ENOENT);
269 struct mlxsw_sp_acl_ruleset *
270 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
271 struct mlxsw_sp_flow_block *block, u32 chain_index,
272 enum mlxsw_sp_acl_profile profile,
273 struct mlxsw_afk_element_usage *tmplt_elusage)
275 const struct mlxsw_sp_acl_profile_ops *ops;
276 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
277 struct mlxsw_sp_acl_ruleset *ruleset;
279 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
281 return ERR_PTR(-EINVAL);
283 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
285 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
288 return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
292 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
293 struct mlxsw_sp_acl_ruleset *ruleset)
295 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
298 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
300 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
302 return ops->ruleset_group_id(ruleset->priv);
305 void mlxsw_sp_acl_ruleset_prio_get(struct mlxsw_sp_acl_ruleset *ruleset,
306 unsigned int *p_min_prio,
307 unsigned int *p_max_prio)
309 *p_min_prio = ruleset->min_prio;
310 *p_max_prio = ruleset->max_prio;
313 struct mlxsw_sp_acl_rule_info *
314 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl,
315 struct mlxsw_afa_block *afa_block)
317 struct mlxsw_sp_acl_rule_info *rulei;
320 rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
325 rulei->act_block = afa_block;
329 rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa);
330 if (IS_ERR(rulei->act_block)) {
331 err = PTR_ERR(rulei->act_block);
332 goto err_afa_block_create;
334 rulei->action_created = 1;
337 err_afa_block_create:
342 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp *mlxsw_sp,
343 struct mlxsw_sp_acl_rule_info *rulei)
345 if (rulei->action_created)
346 mlxsw_afa_block_destroy(rulei->act_block);
347 if (rulei->src_port_range_reg_valid)
348 mlxsw_sp_port_range_reg_put(mlxsw_sp,
349 rulei->src_port_range_reg_index);
350 if (rulei->dst_port_range_reg_valid)
351 mlxsw_sp_port_range_reg_put(mlxsw_sp,
352 rulei->dst_port_range_reg_index);
356 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
358 return mlxsw_afa_block_commit(rulei->act_block);
361 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
362 unsigned int priority)
364 rulei->priority = priority;
367 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
368 enum mlxsw_afk_element element,
369 u32 key_value, u32 mask_value)
371 mlxsw_afk_values_add_u32(&rulei->values, element,
372 key_value, mask_value);
375 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
376 enum mlxsw_afk_element element,
377 const char *key_value,
378 const char *mask_value, unsigned int len)
380 mlxsw_afk_values_add_buf(&rulei->values, element,
381 key_value, mask_value, len);
384 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
386 return mlxsw_afa_block_continue(rulei->act_block);
389 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
392 return mlxsw_afa_block_jump(rulei->act_block, group_id);
395 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
397 return mlxsw_afa_block_terminate(rulei->act_block);
400 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei,
402 const struct flow_action_cookie *fa_cookie,
403 struct netlink_ext_ack *extack)
405 return mlxsw_afa_block_append_drop(rulei->act_block, ingress,
409 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
411 return mlxsw_afa_block_append_trap(rulei->act_block,
415 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
416 struct mlxsw_sp_acl_rule_info *rulei,
417 struct net_device *out_dev,
418 struct netlink_ext_ack *extack)
420 struct mlxsw_sp_port *mlxsw_sp_port;
425 if (!mlxsw_sp_port_dev_check(out_dev)) {
426 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
429 mlxsw_sp_port = netdev_priv(out_dev);
430 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp) {
431 NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
434 local_port = mlxsw_sp_port->local_port;
437 /* If out_dev is NULL, the caller wants to
438 * set forward to ingress port.
443 return mlxsw_afa_block_append_fwd(rulei->act_block,
444 local_port, in_port, extack);
447 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
448 struct mlxsw_sp_acl_rule_info *rulei,
449 struct mlxsw_sp_flow_block *block,
450 struct net_device *out_dev,
451 struct netlink_ext_ack *extack)
453 struct mlxsw_sp_flow_block_binding *binding;
454 struct mlxsw_sp_port *in_port;
456 if (!list_is_singular(&block->binding_list)) {
457 NL_SET_ERR_MSG_MOD(extack, "Only a single mirror source is allowed");
460 binding = list_first_entry(&block->binding_list,
461 struct mlxsw_sp_flow_block_binding, list);
462 in_port = binding->mlxsw_sp_port;
464 return mlxsw_afa_block_append_mirror(rulei->act_block,
471 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
472 struct mlxsw_sp_acl_rule_info *rulei,
473 u32 action, u16 vid, u16 proto, u8 prio,
474 struct netlink_ext_ack *extack)
478 if (action == FLOW_ACTION_VLAN_MANGLE) {
487 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN protocol");
488 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
493 return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
494 vid, prio, ethertype,
497 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN action");
498 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
503 int mlxsw_sp_acl_rulei_act_priority(struct mlxsw_sp *mlxsw_sp,
504 struct mlxsw_sp_acl_rule_info *rulei,
505 u32 prio, struct netlink_ext_ack *extack)
507 /* Even though both Linux and Spectrum switches support 16 priorities,
508 * spectrum_qdisc only processes the first eight priomap elements, and
509 * the DCB and PFC features are tied to 8 priorities as well. Therefore
510 * bounce attempts to prioritize packets to higher priorities.
512 if (prio >= IEEE_8021QAZ_MAX_TCS) {
513 NL_SET_ERR_MSG_MOD(extack, "Only priorities 0..7 are supported");
516 return mlxsw_afa_block_append_qos_switch_prio(rulei->act_block, prio,
520 struct mlxsw_sp_acl_mangle_action {
521 enum flow_action_mangle_base htype;
522 /* Offset is u32-aligned. */
524 /* Mask bits are unset for the modified field. */
526 /* Shift required to extract the set value. */
528 enum mlxsw_sp_acl_mangle_field field;
531 #define MLXSW_SP_ACL_MANGLE_ACTION(_htype, _offset, _mask, _shift, _field) \
537 .field = MLXSW_SP_ACL_MANGLE_FIELD_##_field, \
540 #define MLXSW_SP_ACL_MANGLE_ACTION_IP4(_offset, _mask, _shift, _field) \
541 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_IP4, \
542 _offset, _mask, _shift, _field)
544 #define MLXSW_SP_ACL_MANGLE_ACTION_IP6(_offset, _mask, _shift, _field) \
545 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_IP6, \
546 _offset, _mask, _shift, _field)
548 #define MLXSW_SP_ACL_MANGLE_ACTION_TCP(_offset, _mask, _shift, _field) \
549 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_TCP, _offset, _mask, _shift, _field)
551 #define MLXSW_SP_ACL_MANGLE_ACTION_UDP(_offset, _mask, _shift, _field) \
552 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_UDP, _offset, _mask, _shift, _field)
554 static struct mlxsw_sp_acl_mangle_action mlxsw_sp_acl_mangle_actions[] = {
555 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xff00ffff, 16, IP_DSFIELD),
556 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xff03ffff, 18, IP_DSCP),
557 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xfffcffff, 16, IP_ECN),
559 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xf00fffff, 20, IP_DSFIELD),
560 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xf03fffff, 22, IP_DSCP),
561 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xffcfffff, 20, IP_ECN),
563 MLXSW_SP_ACL_MANGLE_ACTION_TCP(0, 0x0000ffff, 16, IP_SPORT),
564 MLXSW_SP_ACL_MANGLE_ACTION_TCP(0, 0xffff0000, 0, IP_DPORT),
566 MLXSW_SP_ACL_MANGLE_ACTION_UDP(0, 0x0000ffff, 16, IP_SPORT),
567 MLXSW_SP_ACL_MANGLE_ACTION_UDP(0, 0xffff0000, 0, IP_DPORT),
569 MLXSW_SP_ACL_MANGLE_ACTION_IP4(12, 0x00000000, 0, IP4_SIP),
570 MLXSW_SP_ACL_MANGLE_ACTION_IP4(16, 0x00000000, 0, IP4_DIP),
572 MLXSW_SP_ACL_MANGLE_ACTION_IP6(8, 0x00000000, 0, IP6_SIP_1),
573 MLXSW_SP_ACL_MANGLE_ACTION_IP6(12, 0x00000000, 0, IP6_SIP_2),
574 MLXSW_SP_ACL_MANGLE_ACTION_IP6(16, 0x00000000, 0, IP6_SIP_3),
575 MLXSW_SP_ACL_MANGLE_ACTION_IP6(20, 0x00000000, 0, IP6_SIP_4),
576 MLXSW_SP_ACL_MANGLE_ACTION_IP6(24, 0x00000000, 0, IP6_DIP_1),
577 MLXSW_SP_ACL_MANGLE_ACTION_IP6(28, 0x00000000, 0, IP6_DIP_2),
578 MLXSW_SP_ACL_MANGLE_ACTION_IP6(32, 0x00000000, 0, IP6_DIP_3),
579 MLXSW_SP_ACL_MANGLE_ACTION_IP6(36, 0x00000000, 0, IP6_DIP_4),
583 mlxsw_sp_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp,
584 struct mlxsw_sp_acl_rule_info *rulei,
585 struct mlxsw_sp_acl_mangle_action *mact,
586 u32 val, struct netlink_ext_ack *extack)
588 switch (mact->field) {
589 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DSFIELD:
590 return mlxsw_afa_block_append_qos_dsfield(rulei->act_block,
592 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DSCP:
593 return mlxsw_afa_block_append_qos_dscp(rulei->act_block,
595 case MLXSW_SP_ACL_MANGLE_FIELD_IP_ECN:
596 return mlxsw_afa_block_append_qos_ecn(rulei->act_block,
603 static int mlxsw_sp1_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp,
604 struct mlxsw_sp_acl_rule_info *rulei,
605 struct mlxsw_sp_acl_mangle_action *mact,
606 u32 val, struct netlink_ext_ack *extack)
610 err = mlxsw_sp_acl_rulei_act_mangle_field(mlxsw_sp, rulei, mact, val, extack);
611 if (err != -EOPNOTSUPP)
614 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field");
619 mlxsw_sp2_acl_rulei_act_mangle_field_ip_odd(struct mlxsw_sp_acl_rule_info *rulei,
620 enum mlxsw_sp_acl_mangle_field field,
621 u32 val, struct netlink_ext_ack *extack)
623 if (!rulei->ipv6_valid) {
624 rulei->ipv6.prev_val = val;
625 rulei->ipv6_valid = true;
626 rulei->ipv6.prev_field = field;
630 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field order");
634 static int mlxsw_sp2_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp,
635 struct mlxsw_sp_acl_rule_info *rulei,
636 struct mlxsw_sp_acl_mangle_action *mact,
637 u32 val, struct netlink_ext_ack *extack)
641 err = mlxsw_sp_acl_rulei_act_mangle_field(mlxsw_sp, rulei, mact, val, extack);
642 if (err != -EOPNOTSUPP)
645 switch (mact->field) {
646 case MLXSW_SP_ACL_MANGLE_FIELD_IP_SPORT:
647 return mlxsw_afa_block_append_l4port(rulei->act_block, false, val, extack);
648 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DPORT:
649 return mlxsw_afa_block_append_l4port(rulei->act_block, true, val, extack);
651 case MLXSW_SP_ACL_MANGLE_FIELD_IP4_SIP:
652 return mlxsw_afa_block_append_ip(rulei->act_block, false,
653 true, val, 0, extack);
654 case MLXSW_SP_ACL_MANGLE_FIELD_IP4_DIP:
655 return mlxsw_afa_block_append_ip(rulei->act_block, true,
656 true, val, 0, extack);
658 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_1:
659 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_3:
660 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_1:
661 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_3:
662 return mlxsw_sp2_acl_rulei_act_mangle_field_ip_odd(rulei,
665 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_2:
666 if (rulei->ipv6_valid &&
667 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_1) {
668 rulei->ipv6_valid = false;
669 return mlxsw_afa_block_append_ip(rulei->act_block,
671 rulei->ipv6.prev_val,
675 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_4:
676 if (rulei->ipv6_valid &&
677 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_3) {
678 rulei->ipv6_valid = false;
679 return mlxsw_afa_block_append_ip(rulei->act_block,
681 rulei->ipv6.prev_val,
685 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_2:
686 if (rulei->ipv6_valid &&
687 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_1) {
688 rulei->ipv6_valid = false;
689 return mlxsw_afa_block_append_ip(rulei->act_block,
691 rulei->ipv6.prev_val,
695 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_4:
696 if (rulei->ipv6_valid &&
697 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_3) {
698 rulei->ipv6_valid = false;
699 return mlxsw_afa_block_append_ip(rulei->act_block,
701 rulei->ipv6.prev_val,
709 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field");
713 int mlxsw_sp_acl_rulei_act_mangle(struct mlxsw_sp *mlxsw_sp,
714 struct mlxsw_sp_acl_rule_info *rulei,
715 enum flow_action_mangle_base htype,
716 u32 offset, u32 mask, u32 val,
717 struct netlink_ext_ack *extack)
719 const struct mlxsw_sp_acl_rulei_ops *acl_rulei_ops = mlxsw_sp->acl_rulei_ops;
720 struct mlxsw_sp_acl_mangle_action *mact;
723 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_acl_mangle_actions); ++i) {
724 mact = &mlxsw_sp_acl_mangle_actions[i];
725 if (mact->htype == htype &&
726 mact->offset == offset &&
727 mact->mask == mask) {
729 return acl_rulei_ops->act_mangle_field(mlxsw_sp,
735 NL_SET_ERR_MSG_MOD(extack, "Unknown mangle field");
739 int mlxsw_sp_acl_rulei_act_police(struct mlxsw_sp *mlxsw_sp,
740 struct mlxsw_sp_acl_rule_info *rulei,
741 u32 index, u64 rate_bytes_ps,
742 u32 burst, struct netlink_ext_ack *extack)
746 err = mlxsw_afa_block_append_police(rulei->act_block, index,
747 rate_bytes_ps, burst,
748 &rulei->policer_index, extack);
752 rulei->policer_index_valid = true;
757 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
758 struct mlxsw_sp_acl_rule_info *rulei,
759 struct netlink_ext_ack *extack)
763 err = mlxsw_afa_block_append_counter(rulei->act_block,
764 &rulei->counter_index, extack);
767 rulei->counter_valid = true;
771 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
772 struct mlxsw_sp_acl_rule_info *rulei,
773 u16 fid, struct netlink_ext_ack *extack)
775 return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
778 int mlxsw_sp_acl_rulei_act_ignore(struct mlxsw_sp *mlxsw_sp,
779 struct mlxsw_sp_acl_rule_info *rulei,
780 bool disable_learning, bool disable_security)
782 return mlxsw_afa_block_append_ignore(rulei->act_block,
787 int mlxsw_sp_acl_rulei_act_sample(struct mlxsw_sp *mlxsw_sp,
788 struct mlxsw_sp_acl_rule_info *rulei,
789 struct mlxsw_sp_flow_block *block,
790 struct psample_group *psample_group, u32 rate,
791 u32 trunc_size, bool truncate,
792 struct netlink_ext_ack *extack)
794 struct mlxsw_sp_flow_block_binding *binding;
795 struct mlxsw_sp_port *mlxsw_sp_port;
797 if (!list_is_singular(&block->binding_list)) {
798 NL_SET_ERR_MSG_MOD(extack, "Only a single sampling source is allowed");
801 binding = list_first_entry(&block->binding_list,
802 struct mlxsw_sp_flow_block_binding, list);
803 mlxsw_sp_port = binding->mlxsw_sp_port;
805 return mlxsw_afa_block_append_sampler(rulei->act_block,
806 mlxsw_sp_port->local_port,
807 psample_group, rate, trunc_size,
808 truncate, binding->ingress,
812 struct mlxsw_sp_acl_rule *
813 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
814 struct mlxsw_sp_acl_ruleset *ruleset,
815 unsigned long cookie,
816 struct mlxsw_afa_block *afa_block,
817 struct netlink_ext_ack *extack)
819 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
820 struct mlxsw_sp_acl_rule *rule;
823 mlxsw_sp_acl_ruleset_ref_inc(ruleset);
824 rule = kzalloc(sizeof(*rule) + ops->rule_priv_size,
830 rule->cookie = cookie;
831 rule->ruleset = ruleset;
833 rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl, afa_block);
834 if (IS_ERR(rule->rulei)) {
835 err = PTR_ERR(rule->rulei);
836 goto err_rulei_create;
844 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
848 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
849 struct mlxsw_sp_acl_rule *rule)
851 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
853 mlxsw_sp_acl_rulei_destroy(mlxsw_sp, rule->rulei);
855 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
858 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
859 struct mlxsw_sp_acl_rule *rule)
861 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
862 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
863 struct mlxsw_sp_flow_block *block = ruleset->ht_key.block;
866 err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
870 err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
871 mlxsw_sp_acl_rule_ht_params);
873 goto err_rhashtable_insert;
875 if (!ruleset->ht_key.chain_index &&
876 mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
877 /* We only need ruleset with chain index 0, the implicit
878 * one, to be directly bound to device. The rest of the
879 * rulesets are bound by "Goto action set".
881 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset, block);
883 goto err_ruleset_block_bind;
886 mutex_lock(&mlxsw_sp->acl->rules_lock);
887 list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
888 mutex_unlock(&mlxsw_sp->acl->rules_lock);
890 block->ingress_blocker_rule_count += rule->rulei->ingress_bind_blocker;
891 block->egress_blocker_rule_count += rule->rulei->egress_bind_blocker;
894 err_ruleset_block_bind:
895 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
896 mlxsw_sp_acl_rule_ht_params);
897 err_rhashtable_insert:
898 ops->rule_del(mlxsw_sp, rule->priv);
902 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
903 struct mlxsw_sp_acl_rule *rule)
905 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
906 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
907 struct mlxsw_sp_flow_block *block = ruleset->ht_key.block;
909 block->egress_blocker_rule_count -= rule->rulei->egress_bind_blocker;
910 block->ingress_blocker_rule_count -= rule->rulei->ingress_bind_blocker;
912 mutex_lock(&mlxsw_sp->acl->rules_lock);
913 list_del(&rule->list);
914 mutex_unlock(&mlxsw_sp->acl->rules_lock);
915 if (!ruleset->ht_key.chain_index &&
916 mlxsw_sp_acl_ruleset_is_singular(ruleset))
917 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset, block);
918 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
919 mlxsw_sp_acl_rule_ht_params);
920 ops->rule_del(mlxsw_sp, rule->priv);
923 int mlxsw_sp_acl_rule_action_replace(struct mlxsw_sp *mlxsw_sp,
924 struct mlxsw_sp_acl_rule *rule,
925 struct mlxsw_afa_block *afa_block)
927 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
928 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
929 struct mlxsw_sp_acl_rule_info *rulei;
931 rulei = mlxsw_sp_acl_rule_rulei(rule);
932 rulei->act_block = afa_block;
934 return ops->rule_action_replace(mlxsw_sp, rule->priv, rule->rulei);
937 struct mlxsw_sp_acl_rule *
938 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
939 struct mlxsw_sp_acl_ruleset *ruleset,
940 unsigned long cookie)
942 return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
943 mlxsw_sp_acl_rule_ht_params);
946 struct mlxsw_sp_acl_rule_info *
947 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
952 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
953 struct mlxsw_sp_acl_rule *rule)
955 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
956 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
960 err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
964 rule->last_used = jiffies;
968 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
970 struct mlxsw_sp_acl_rule *rule;
973 mutex_lock(&acl->rules_lock);
974 list_for_each_entry(rule, &acl->rules, list) {
975 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
978 goto err_rule_update;
980 mutex_unlock(&acl->rules_lock);
984 mutex_unlock(&acl->rules_lock);
988 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
990 unsigned long interval = acl->rule_activity_update.interval;
992 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
993 msecs_to_jiffies(interval));
996 static void mlxsw_sp_acl_rule_activity_update_work(struct work_struct *work)
998 struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
999 rule_activity_update.dw.work);
1002 err = mlxsw_sp_acl_rules_activity_update(acl);
1004 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
1006 mlxsw_sp_acl_rule_activity_work_schedule(acl);
1009 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
1010 struct mlxsw_sp_acl_rule *rule,
1011 u64 *packets, u64 *bytes, u64 *drops,
1013 enum flow_action_hw_stats *used_hw_stats)
1016 enum mlxsw_sp_policer_type type = MLXSW_SP_POLICER_TYPE_SINGLE_RATE;
1017 struct mlxsw_sp_acl_rule_info *rulei;
1018 u64 current_packets = 0;
1019 u64 current_bytes = 0;
1020 u64 current_drops = 0;
1023 rulei = mlxsw_sp_acl_rule_rulei(rule);
1024 if (rulei->counter_valid) {
1025 err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
1030 *used_hw_stats = FLOW_ACTION_HW_STATS_IMMEDIATE;
1032 if (rulei->policer_index_valid) {
1033 err = mlxsw_sp_policer_drops_counter_get(mlxsw_sp, type,
1034 rulei->policer_index,
1039 *packets = current_packets - rule->last_packets;
1040 *bytes = current_bytes - rule->last_bytes;
1041 *drops = current_drops - rule->last_drops;
1042 *last_use = rule->last_used;
1044 rule->last_bytes = current_bytes;
1045 rule->last_packets = current_packets;
1046 rule->last_drops = current_drops;
1051 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
1053 struct mlxsw_sp_fid *fid;
1054 struct mlxsw_sp_acl *acl;
1058 alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
1059 acl = kzalloc(alloc_size, GFP_KERNEL);
1062 mlxsw_sp->acl = acl;
1063 acl->mlxsw_sp = mlxsw_sp;
1064 acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
1069 goto err_afk_create;
1072 err = rhashtable_init(&acl->ruleset_ht,
1073 &mlxsw_sp_acl_ruleset_ht_params);
1075 goto err_rhashtable_init;
1077 fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
1082 acl->dummy_fid = fid;
1084 INIT_LIST_HEAD(&acl->rules);
1085 mutex_init(&acl->rules_lock);
1086 err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
1088 goto err_acl_ops_init;
1090 /* Create the delayed work for the rule activity_update */
1091 INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
1092 mlxsw_sp_acl_rule_activity_update_work);
1093 acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
1094 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
1098 mutex_destroy(&acl->rules_lock);
1099 mlxsw_sp_fid_put(fid);
1101 rhashtable_destroy(&acl->ruleset_ht);
1102 err_rhashtable_init:
1103 mlxsw_afk_destroy(acl->afk);
1109 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
1111 struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
1113 cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
1114 mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
1115 mutex_destroy(&acl->rules_lock);
1116 WARN_ON(!list_empty(&acl->rules));
1117 mlxsw_sp_fid_put(acl->dummy_fid);
1118 rhashtable_destroy(&acl->ruleset_ht);
1119 mlxsw_afk_destroy(acl->afk);
1123 struct mlxsw_sp_acl_rulei_ops mlxsw_sp1_acl_rulei_ops = {
1124 .act_mangle_field = mlxsw_sp1_acl_rulei_act_mangle_field,
1127 struct mlxsw_sp_acl_rulei_ops mlxsw_sp2_acl_rulei_ops = {
1128 .act_mangle_field = mlxsw_sp2_acl_rulei_act_mangle_field,