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/errno.h>
6 #include <linux/netdevice.h>
7 #include <net/net_namespace.h>
8 #include <net/flow_dissector.h>
9 #include <net/pkt_cls.h>
10 #include <net/tc_act/tc_gact.h>
11 #include <net/tc_act/tc_mirred.h>
12 #include <net/tc_act/tc_vlan.h>
15 #include "core_acl_flex_keys.h"
17 static int mlxsw_sp_flower_parse_actions(struct mlxsw_sp *mlxsw_sp,
18 struct mlxsw_sp_acl_block *block,
19 struct mlxsw_sp_acl_rule_info *rulei,
20 struct flow_action *flow_action,
21 struct netlink_ext_ack *extack)
23 const struct flow_action_entry *act;
24 int mirror_act_count = 0;
27 if (!flow_action_has_entries(flow_action))
30 /* Count action is inserted first */
31 err = mlxsw_sp_acl_rulei_act_count(mlxsw_sp, rulei, extack);
35 flow_action_for_each(i, act, flow_action) {
37 case FLOW_ACTION_ACCEPT:
38 err = mlxsw_sp_acl_rulei_act_terminate(rulei);
40 NL_SET_ERR_MSG_MOD(extack, "Cannot append terminate action");
44 case FLOW_ACTION_DROP:
45 err = mlxsw_sp_acl_rulei_act_drop(rulei);
47 NL_SET_ERR_MSG_MOD(extack, "Cannot append drop action");
51 case FLOW_ACTION_TRAP:
52 err = mlxsw_sp_acl_rulei_act_trap(rulei);
54 NL_SET_ERR_MSG_MOD(extack, "Cannot append trap action");
58 case FLOW_ACTION_GOTO: {
59 u32 chain_index = act->chain_index;
60 struct mlxsw_sp_acl_ruleset *ruleset;
63 ruleset = mlxsw_sp_acl_ruleset_lookup(mlxsw_sp, block,
65 MLXSW_SP_ACL_PROFILE_FLOWER);
67 return PTR_ERR(ruleset);
69 group_id = mlxsw_sp_acl_ruleset_group_id(ruleset);
70 err = mlxsw_sp_acl_rulei_act_jump(rulei, group_id);
72 NL_SET_ERR_MSG_MOD(extack, "Cannot append jump action");
77 case FLOW_ACTION_REDIRECT: {
78 struct net_device *out_dev;
79 struct mlxsw_sp_fid *fid;
82 fid = mlxsw_sp_acl_dummy_fid(mlxsw_sp);
83 fid_index = mlxsw_sp_fid_index(fid);
84 err = mlxsw_sp_acl_rulei_act_fid_set(mlxsw_sp, rulei,
90 err = mlxsw_sp_acl_rulei_act_fwd(mlxsw_sp, rulei,
96 case FLOW_ACTION_MIRRED: {
97 struct net_device *out_dev = act->dev;
99 if (mirror_act_count++) {
100 NL_SET_ERR_MSG_MOD(extack, "Multiple mirror actions per rule are not supported");
104 err = mlxsw_sp_acl_rulei_act_mirror(mlxsw_sp, rulei,
111 case FLOW_ACTION_VLAN_MANGLE: {
112 u16 proto = be16_to_cpu(act->vlan.proto);
113 u8 prio = act->vlan.prio;
114 u16 vid = act->vlan.vid;
116 return mlxsw_sp_acl_rulei_act_vlan(mlxsw_sp, rulei,
118 proto, prio, extack);
121 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
122 dev_err(mlxsw_sp->bus_info->dev, "Unsupported action\n");
129 static int mlxsw_sp_flower_parse_meta(struct mlxsw_sp_acl_rule_info *rulei,
130 struct flow_cls_offload *f,
131 struct mlxsw_sp_acl_block *block)
133 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
134 struct mlxsw_sp_port *mlxsw_sp_port;
135 struct net_device *ingress_dev;
136 struct flow_match_meta match;
138 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META))
141 flow_rule_match_meta(rule, &match);
142 if (match.mask->ingress_ifindex != 0xFFFFFFFF) {
143 NL_SET_ERR_MSG_MOD(f->common.extack, "Unsupported ingress ifindex mask");
147 ingress_dev = __dev_get_by_index(block->net,
148 match.key->ingress_ifindex);
150 NL_SET_ERR_MSG_MOD(f->common.extack, "Can't find specified ingress port to match on");
154 if (!mlxsw_sp_port_dev_check(ingress_dev)) {
155 NL_SET_ERR_MSG_MOD(f->common.extack, "Can't match on non-mlxsw ingress port");
159 mlxsw_sp_port = netdev_priv(ingress_dev);
160 if (mlxsw_sp_port->mlxsw_sp != block->mlxsw_sp) {
161 NL_SET_ERR_MSG_MOD(f->common.extack, "Can't match on a port from different device");
165 mlxsw_sp_acl_rulei_keymask_u32(rulei,
166 MLXSW_AFK_ELEMENT_SRC_SYS_PORT,
167 mlxsw_sp_port->local_port,
172 static void mlxsw_sp_flower_parse_ipv4(struct mlxsw_sp_acl_rule_info *rulei,
173 struct flow_cls_offload *f)
175 struct flow_match_ipv4_addrs match;
177 flow_rule_match_ipv4_addrs(f->rule, &match);
179 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_0_31,
180 (char *) &match.key->src,
181 (char *) &match.mask->src, 4);
182 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_0_31,
183 (char *) &match.key->dst,
184 (char *) &match.mask->dst, 4);
187 static void mlxsw_sp_flower_parse_ipv6(struct mlxsw_sp_acl_rule_info *rulei,
188 struct flow_cls_offload *f)
190 struct flow_match_ipv6_addrs match;
192 flow_rule_match_ipv6_addrs(f->rule, &match);
194 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_96_127,
195 &match.key->src.s6_addr[0x0],
196 &match.mask->src.s6_addr[0x0], 4);
197 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_64_95,
198 &match.key->src.s6_addr[0x4],
199 &match.mask->src.s6_addr[0x4], 4);
200 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_32_63,
201 &match.key->src.s6_addr[0x8],
202 &match.mask->src.s6_addr[0x8], 4);
203 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_0_31,
204 &match.key->src.s6_addr[0xC],
205 &match.mask->src.s6_addr[0xC], 4);
206 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_96_127,
207 &match.key->dst.s6_addr[0x0],
208 &match.mask->dst.s6_addr[0x0], 4);
209 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_64_95,
210 &match.key->dst.s6_addr[0x4],
211 &match.mask->dst.s6_addr[0x4], 4);
212 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_32_63,
213 &match.key->dst.s6_addr[0x8],
214 &match.mask->dst.s6_addr[0x8], 4);
215 mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_0_31,
216 &match.key->dst.s6_addr[0xC],
217 &match.mask->dst.s6_addr[0xC], 4);
220 static int mlxsw_sp_flower_parse_ports(struct mlxsw_sp *mlxsw_sp,
221 struct mlxsw_sp_acl_rule_info *rulei,
222 struct flow_cls_offload *f,
225 const struct flow_rule *rule = flow_cls_offload_flow_rule(f);
226 struct flow_match_ports match;
228 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS))
231 if (ip_proto != IPPROTO_TCP && ip_proto != IPPROTO_UDP) {
232 NL_SET_ERR_MSG_MOD(f->common.extack, "Only UDP and TCP keys are supported");
233 dev_err(mlxsw_sp->bus_info->dev, "Only UDP and TCP keys are supported\n");
237 flow_rule_match_ports(rule, &match);
238 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_DST_L4_PORT,
239 ntohs(match.key->dst),
240 ntohs(match.mask->dst));
241 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_SRC_L4_PORT,
242 ntohs(match.key->src),
243 ntohs(match.mask->src));
247 static int mlxsw_sp_flower_parse_tcp(struct mlxsw_sp *mlxsw_sp,
248 struct mlxsw_sp_acl_rule_info *rulei,
249 struct flow_cls_offload *f,
252 const struct flow_rule *rule = flow_cls_offload_flow_rule(f);
253 struct flow_match_tcp match;
255 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP))
258 if (ip_proto != IPPROTO_TCP) {
259 NL_SET_ERR_MSG_MOD(f->common.extack, "TCP keys supported only for TCP");
260 dev_err(mlxsw_sp->bus_info->dev, "TCP keys supported only for TCP\n");
264 flow_rule_match_tcp(rule, &match);
266 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_TCP_FLAGS,
267 ntohs(match.key->flags),
268 ntohs(match.mask->flags));
272 static int mlxsw_sp_flower_parse_ip(struct mlxsw_sp *mlxsw_sp,
273 struct mlxsw_sp_acl_rule_info *rulei,
274 struct flow_cls_offload *f,
277 const struct flow_rule *rule = flow_cls_offload_flow_rule(f);
278 struct flow_match_ip match;
280 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP))
283 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6) {
284 NL_SET_ERR_MSG_MOD(f->common.extack, "IP keys supported only for IPv4/6");
285 dev_err(mlxsw_sp->bus_info->dev, "IP keys supported only for IPv4/6\n");
289 flow_rule_match_ip(rule, &match);
291 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_TTL_,
292 match.key->ttl, match.mask->ttl);
294 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_ECN,
295 match.key->tos & 0x3,
296 match.mask->tos & 0x3);
298 mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_DSCP,
300 match.mask->tos >> 2);
305 static int mlxsw_sp_flower_parse(struct mlxsw_sp *mlxsw_sp,
306 struct mlxsw_sp_acl_block *block,
307 struct mlxsw_sp_acl_rule_info *rulei,
308 struct flow_cls_offload *f)
310 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
311 struct flow_dissector *dissector = rule->match.dissector;
312 u16 n_proto_mask = 0;
318 if (dissector->used_keys &
319 ~(BIT(FLOW_DISSECTOR_KEY_META) |
320 BIT(FLOW_DISSECTOR_KEY_CONTROL) |
321 BIT(FLOW_DISSECTOR_KEY_BASIC) |
322 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
323 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
324 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
325 BIT(FLOW_DISSECTOR_KEY_PORTS) |
326 BIT(FLOW_DISSECTOR_KEY_TCP) |
327 BIT(FLOW_DISSECTOR_KEY_IP) |
328 BIT(FLOW_DISSECTOR_KEY_VLAN))) {
329 dev_err(mlxsw_sp->bus_info->dev, "Unsupported key\n");
330 NL_SET_ERR_MSG_MOD(f->common.extack, "Unsupported key");
334 mlxsw_sp_acl_rulei_priority(rulei, f->common.prio);
336 err = mlxsw_sp_flower_parse_meta(rulei, f, block);
340 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
341 struct flow_match_control match;
343 flow_rule_match_control(rule, &match);
344 addr_type = match.key->addr_type;
347 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
348 struct flow_match_basic match;
350 flow_rule_match_basic(rule, &match);
351 n_proto_key = ntohs(match.key->n_proto);
352 n_proto_mask = ntohs(match.mask->n_proto);
354 if (n_proto_key == ETH_P_ALL) {
358 mlxsw_sp_acl_rulei_keymask_u32(rulei,
359 MLXSW_AFK_ELEMENT_ETHERTYPE,
360 n_proto_key, n_proto_mask);
362 ip_proto = match.key->ip_proto;
363 mlxsw_sp_acl_rulei_keymask_u32(rulei,
364 MLXSW_AFK_ELEMENT_IP_PROTO,
366 match.mask->ip_proto);
369 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
370 struct flow_match_eth_addrs match;
372 flow_rule_match_eth_addrs(rule, &match);
373 mlxsw_sp_acl_rulei_keymask_buf(rulei,
374 MLXSW_AFK_ELEMENT_DMAC_32_47,
377 mlxsw_sp_acl_rulei_keymask_buf(rulei,
378 MLXSW_AFK_ELEMENT_DMAC_0_31,
380 match.mask->dst + 2, 4);
381 mlxsw_sp_acl_rulei_keymask_buf(rulei,
382 MLXSW_AFK_ELEMENT_SMAC_32_47,
385 mlxsw_sp_acl_rulei_keymask_buf(rulei,
386 MLXSW_AFK_ELEMENT_SMAC_0_31,
388 match.mask->src + 2, 4);
391 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
392 struct flow_match_vlan match;
394 flow_rule_match_vlan(rule, &match);
395 if (mlxsw_sp_acl_block_is_egress_bound(block)) {
396 NL_SET_ERR_MSG_MOD(f->common.extack, "vlan_id key is not supported on egress");
399 if (match.mask->vlan_id != 0)
400 mlxsw_sp_acl_rulei_keymask_u32(rulei,
401 MLXSW_AFK_ELEMENT_VID,
403 match.mask->vlan_id);
404 if (match.mask->vlan_priority != 0)
405 mlxsw_sp_acl_rulei_keymask_u32(rulei,
406 MLXSW_AFK_ELEMENT_PCP,
407 match.key->vlan_priority,
408 match.mask->vlan_priority);
411 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS)
412 mlxsw_sp_flower_parse_ipv4(rulei, f);
414 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS)
415 mlxsw_sp_flower_parse_ipv6(rulei, f);
417 err = mlxsw_sp_flower_parse_ports(mlxsw_sp, rulei, f, ip_proto);
420 err = mlxsw_sp_flower_parse_tcp(mlxsw_sp, rulei, f, ip_proto);
424 err = mlxsw_sp_flower_parse_ip(mlxsw_sp, rulei, f, n_proto_key & n_proto_mask);
428 return mlxsw_sp_flower_parse_actions(mlxsw_sp, block, rulei,
433 int mlxsw_sp_flower_replace(struct mlxsw_sp *mlxsw_sp,
434 struct mlxsw_sp_acl_block *block,
435 struct flow_cls_offload *f)
437 struct mlxsw_sp_acl_rule_info *rulei;
438 struct mlxsw_sp_acl_ruleset *ruleset;
439 struct mlxsw_sp_acl_rule *rule;
442 ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
443 f->common.chain_index,
444 MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
446 return PTR_ERR(ruleset);
448 rule = mlxsw_sp_acl_rule_create(mlxsw_sp, ruleset, f->cookie, NULL,
452 goto err_rule_create;
455 rulei = mlxsw_sp_acl_rule_rulei(rule);
456 err = mlxsw_sp_flower_parse(mlxsw_sp, block, rulei, f);
458 goto err_flower_parse;
460 err = mlxsw_sp_acl_rulei_commit(rulei);
462 goto err_rulei_commit;
464 err = mlxsw_sp_acl_rule_add(mlxsw_sp, rule);
468 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
474 mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
476 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
480 void mlxsw_sp_flower_destroy(struct mlxsw_sp *mlxsw_sp,
481 struct mlxsw_sp_acl_block *block,
482 struct flow_cls_offload *f)
484 struct mlxsw_sp_acl_ruleset *ruleset;
485 struct mlxsw_sp_acl_rule *rule;
487 ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
488 f->common.chain_index,
489 MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
493 rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
495 mlxsw_sp_acl_rule_del(mlxsw_sp, rule);
496 mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
499 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
502 int mlxsw_sp_flower_stats(struct mlxsw_sp *mlxsw_sp,
503 struct mlxsw_sp_acl_block *block,
504 struct flow_cls_offload *f)
506 struct mlxsw_sp_acl_ruleset *ruleset;
507 struct mlxsw_sp_acl_rule *rule;
513 ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
514 f->common.chain_index,
515 MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
516 if (WARN_ON(IS_ERR(ruleset)))
519 rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
523 err = mlxsw_sp_acl_rule_get_stats(mlxsw_sp, rule, &packets, &bytes,
526 goto err_rule_get_stats;
528 flow_stats_update(&f->stats, bytes, packets, lastuse);
530 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
534 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
538 int mlxsw_sp_flower_tmplt_create(struct mlxsw_sp *mlxsw_sp,
539 struct mlxsw_sp_acl_block *block,
540 struct flow_cls_offload *f)
542 struct mlxsw_sp_acl_ruleset *ruleset;
543 struct mlxsw_sp_acl_rule_info rulei;
546 memset(&rulei, 0, sizeof(rulei));
547 err = mlxsw_sp_flower_parse(mlxsw_sp, block, &rulei, f);
550 ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
551 f->common.chain_index,
552 MLXSW_SP_ACL_PROFILE_FLOWER,
553 &rulei.values.elusage);
555 /* keep the reference to the ruleset */
556 return PTR_ERR_OR_ZERO(ruleset);
559 void mlxsw_sp_flower_tmplt_destroy(struct mlxsw_sp *mlxsw_sp,
560 struct mlxsw_sp_acl_block *block,
561 struct flow_cls_offload *f)
563 struct mlxsw_sp_acl_ruleset *ruleset;
565 ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
566 f->common.chain_index,
567 MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
570 /* put the reference to the ruleset kept in create */
571 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
572 mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);