spectrum_flower.c 14.9 KB
Newer Older
1 2
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3 4 5 6

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
7
#include <net/net_namespace.h>
8 9 10 11
#include <net/flow_dissector.h>
#include <net/pkt_cls.h>
#include <net/tc_act/tc_gact.h>
#include <net/tc_act/tc_mirred.h>
12
#include <net/tc_act/tc_vlan.h>
13 14 15 16 17

#include "spectrum.h"
#include "core_acl_flex_keys.h"

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)
22
{
23
	const struct flow_action_entry *act;
24
	int err, i;
25

26
	if (!flow_action_has_entries(flow_action))
27 28
		return 0;

29
	/* Count action is inserted first */
30
	err = mlxsw_sp_acl_rulei_act_count(mlxsw_sp, rulei, extack);
31 32 33
	if (err)
		return err;

34 35 36
	flow_action_for_each(i, act, flow_action) {
		switch (act->id) {
		case FLOW_ACTION_ACCEPT:
37
			err = mlxsw_sp_acl_rulei_act_terminate(rulei);
38 39
			if (err) {
				NL_SET_ERR_MSG_MOD(extack, "Cannot append terminate action");
40
				return err;
41
			}
42 43
			break;
		case FLOW_ACTION_DROP:
44
			err = mlxsw_sp_acl_rulei_act_drop(rulei);
45 46
			if (err) {
				NL_SET_ERR_MSG_MOD(extack, "Cannot append drop action");
47
				return err;
48
			}
49 50
			break;
		case FLOW_ACTION_TRAP:
51
			err = mlxsw_sp_acl_rulei_act_trap(rulei);
52 53
			if (err) {
				NL_SET_ERR_MSG_MOD(extack, "Cannot append trap action");
54
				return err;
55
			}
56 57 58
			break;
		case FLOW_ACTION_GOTO: {
			u32 chain_index = act->chain_index;
59 60 61
			struct mlxsw_sp_acl_ruleset *ruleset;
			u16 group_id;

62
			ruleset = mlxsw_sp_acl_ruleset_lookup(mlxsw_sp, block,
63 64 65 66 67 68
							      chain_index,
							      MLXSW_SP_ACL_PROFILE_FLOWER);
			if (IS_ERR(ruleset))
				return PTR_ERR(ruleset);

			group_id = mlxsw_sp_acl_ruleset_group_id(ruleset);
69
			err = mlxsw_sp_acl_rulei_act_jump(rulei, group_id);
70 71
			if (err) {
				NL_SET_ERR_MSG_MOD(extack, "Cannot append jump action");
72
				return err;
73
			}
74 75 76
			}
			break;
		case FLOW_ACTION_REDIRECT: {
77
			struct net_device *out_dev;
78 79
			struct mlxsw_sp_fid *fid;
			u16 fid_index;
80

81 82
			fid = mlxsw_sp_acl_dummy_fid(mlxsw_sp);
			fid_index = mlxsw_sp_fid_index(fid);
83
			err = mlxsw_sp_acl_rulei_act_fid_set(mlxsw_sp, rulei,
84
							     fid_index, extack);
85 86 87
			if (err)
				return err;

88
			out_dev = act->dev;
89
			err = mlxsw_sp_acl_rulei_act_fwd(mlxsw_sp, rulei,
90
							 out_dev, extack);
91 92
			if (err)
				return err;
93 94 95 96
			}
			break;
		case FLOW_ACTION_MIRRED: {
			struct net_device *out_dev = act->dev;
97 98

			err = mlxsw_sp_acl_rulei_act_mirror(mlxsw_sp, rulei,
99 100
							    block, out_dev,
							    extack);
101 102
			if (err)
				return err;
103 104 105 106 107 108 109
			}
			break;
		case FLOW_ACTION_VLAN_PUSH:
		case FLOW_ACTION_VLAN_POP: {
			u16 proto = be16_to_cpu(act->vlan.proto);
			u8 prio = act->vlan.prio;
			u16 vid = act->vlan.vid;
110 111

			return mlxsw_sp_acl_rulei_act_vlan(mlxsw_sp, rulei,
112
							   act->id, vid,
113
							   proto, prio, extack);
114 115
			}
		default:
116
			NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
117 118 119 120 121 122 123 124 125 126
			dev_err(mlxsw_sp->bus_info->dev, "Unsupported action\n");
			return -EOPNOTSUPP;
		}
	}
	return 0;
}

static void mlxsw_sp_flower_parse_ipv4(struct mlxsw_sp_acl_rule_info *rulei,
				       struct tc_cls_flower_offload *f)
{
127 128 129
	struct flow_match_ipv4_addrs match;

	flow_rule_match_ipv4_addrs(f->rule, &match);
130

131
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_0_31,
132 133
				       (char *) &match.key->src,
				       (char *) &match.mask->src, 4);
134
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_0_31,
135 136
				       (char *) &match.key->dst,
				       (char *) &match.mask->dst, 4);
137 138 139 140 141
}

static void mlxsw_sp_flower_parse_ipv6(struct mlxsw_sp_acl_rule_info *rulei,
				       struct tc_cls_flower_offload *f)
{
142 143 144
	struct flow_match_ipv6_addrs match;

	flow_rule_match_ipv6_addrs(f->rule, &match);
145 146

	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_96_127,
147 148
				       &match.key->src.s6_addr[0x0],
				       &match.mask->src.s6_addr[0x0], 4);
149
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_64_95,
150 151
				       &match.key->src.s6_addr[0x4],
				       &match.mask->src.s6_addr[0x4], 4);
152
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_32_63,
153 154
				       &match.key->src.s6_addr[0x8],
				       &match.mask->src.s6_addr[0x8], 4);
155
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP_0_31,
156 157
				       &match.key->src.s6_addr[0xC],
				       &match.mask->src.s6_addr[0xC], 4);
158
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_96_127,
159 160
				       &match.key->dst.s6_addr[0x0],
				       &match.mask->dst.s6_addr[0x0], 4);
161
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_64_95,
162 163
				       &match.key->dst.s6_addr[0x4],
				       &match.mask->dst.s6_addr[0x4], 4);
164
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_32_63,
165 166
				       &match.key->dst.s6_addr[0x8],
				       &match.mask->dst.s6_addr[0x8], 4);
167
	mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP_0_31,
168 169
				       &match.key->dst.s6_addr[0xC],
				       &match.mask->dst.s6_addr[0xC], 4);
170 171 172 173 174 175 176
}

static int mlxsw_sp_flower_parse_ports(struct mlxsw_sp *mlxsw_sp,
				       struct mlxsw_sp_acl_rule_info *rulei,
				       struct tc_cls_flower_offload *f,
				       u8 ip_proto)
{
177 178
	const struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
	struct flow_match_ports match;
179

180
	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS))
181 182 183
		return 0;

	if (ip_proto != IPPROTO_TCP && ip_proto != IPPROTO_UDP) {
184
		NL_SET_ERR_MSG_MOD(f->common.extack, "Only UDP and TCP keys are supported");
185 186 187 188
		dev_err(mlxsw_sp->bus_info->dev, "Only UDP and TCP keys are supported\n");
		return -EINVAL;
	}

189
	flow_rule_match_ports(rule, &match);
190
	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_DST_L4_PORT,
191 192
				       ntohs(match.key->dst),
				       ntohs(match.mask->dst));
193
	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_SRC_L4_PORT,
194 195
				       ntohs(match.key->src),
				       ntohs(match.mask->src));
196 197 198
	return 0;
}

199 200 201 202 203
static int mlxsw_sp_flower_parse_tcp(struct mlxsw_sp *mlxsw_sp,
				     struct mlxsw_sp_acl_rule_info *rulei,
				     struct tc_cls_flower_offload *f,
				     u8 ip_proto)
{
204 205
	const struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
	struct flow_match_tcp match;
206

207
	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP))
208 209 210
		return 0;

	if (ip_proto != IPPROTO_TCP) {
211
		NL_SET_ERR_MSG_MOD(f->common.extack, "TCP keys supported only for TCP");
212 213 214 215
		dev_err(mlxsw_sp->bus_info->dev, "TCP keys supported only for TCP\n");
		return -EINVAL;
	}

216 217
	flow_rule_match_tcp(rule, &match);

218
	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_TCP_FLAGS,
219 220
				       ntohs(match.key->flags),
				       ntohs(match.mask->flags));
221 222 223
	return 0;
}

224 225 226 227 228
static int mlxsw_sp_flower_parse_ip(struct mlxsw_sp *mlxsw_sp,
				    struct mlxsw_sp_acl_rule_info *rulei,
				    struct tc_cls_flower_offload *f,
				    u16 n_proto)
{
229 230
	const struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
	struct flow_match_ip match;
231

232
	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP))
233 234 235
		return 0;

	if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6) {
236
		NL_SET_ERR_MSG_MOD(f->common.extack, "IP keys supported only for IPv4/6");
237 238 239 240
		dev_err(mlxsw_sp->bus_info->dev, "IP keys supported only for IPv4/6\n");
		return -EINVAL;
	}

241 242
	flow_rule_match_ip(rule, &match);

243
	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_TTL_,
244
				       match.key->ttl, match.mask->ttl);
245 246

	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_ECN,
247 248
				       match.key->tos & 0x3,
				       match.mask->tos & 0x3);
249 250

	mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_DSCP,
251 252
				       match.key->tos >> 6,
				       match.mask->tos >> 6);
253

254 255 256
	return 0;
}

257
static int mlxsw_sp_flower_parse(struct mlxsw_sp *mlxsw_sp,
258
				 struct mlxsw_sp_acl_block *block,
259 260 261
				 struct mlxsw_sp_acl_rule_info *rulei,
				 struct tc_cls_flower_offload *f)
{
262 263
	struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
	struct flow_dissector *dissector = rule->match.dissector;
264 265
	u16 n_proto_mask = 0;
	u16 n_proto_key = 0;
266 267 268 269
	u16 addr_type = 0;
	u8 ip_proto = 0;
	int err;

270
	if (dissector->used_keys &
271 272 273 274 275
	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
276
	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
277
	      BIT(FLOW_DISSECTOR_KEY_TCP) |
278
	      BIT(FLOW_DISSECTOR_KEY_IP) |
279
	      BIT(FLOW_DISSECTOR_KEY_VLAN))) {
280
		dev_err(mlxsw_sp->bus_info->dev, "Unsupported key\n");
281
		NL_SET_ERR_MSG_MOD(f->common.extack, "Unsupported key");
282 283 284
		return -EOPNOTSUPP;
	}

285
	mlxsw_sp_acl_rulei_priority(rulei, f->common.prio);
286

287 288 289 290 291
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
		struct flow_match_control match;

		flow_rule_match_control(rule, &match);
		addr_type = match.key->addr_type;
292 293
	}

294 295 296 297 298 299
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
		struct flow_match_basic match;

		flow_rule_match_basic(rule, &match);
		n_proto_key = ntohs(match.key->n_proto);
		n_proto_mask = ntohs(match.mask->n_proto);
300 301 302 303 304

		if (n_proto_key == ETH_P_ALL) {
			n_proto_key = 0;
			n_proto_mask = 0;
		}
305 306
		mlxsw_sp_acl_rulei_keymask_u32(rulei,
					       MLXSW_AFK_ELEMENT_ETHERTYPE,
307 308
					       n_proto_key, n_proto_mask);

309
		ip_proto = match.key->ip_proto;
310 311
		mlxsw_sp_acl_rulei_keymask_u32(rulei,
					       MLXSW_AFK_ELEMENT_IP_PROTO,
312 313
					       match.key->ip_proto,
					       match.mask->ip_proto);
314 315
	}

316 317
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
		struct flow_match_eth_addrs match;
318

319
		flow_rule_match_eth_addrs(rule, &match);
320
		mlxsw_sp_acl_rulei_keymask_buf(rulei,
321
					       MLXSW_AFK_ELEMENT_DMAC_32_47,
322 323
					       match.key->dst,
					       match.mask->dst, 2);
324 325
		mlxsw_sp_acl_rulei_keymask_buf(rulei,
					       MLXSW_AFK_ELEMENT_DMAC_0_31,
326 327
					       match.key->dst + 2,
					       match.mask->dst + 2, 4);
328 329
		mlxsw_sp_acl_rulei_keymask_buf(rulei,
					       MLXSW_AFK_ELEMENT_SMAC_32_47,
330 331
					       match.key->src,
					       match.mask->src, 2);
332
		mlxsw_sp_acl_rulei_keymask_buf(rulei,
333
					       MLXSW_AFK_ELEMENT_SMAC_0_31,
334 335
					       match.key->src + 2,
					       match.mask->src + 2, 4);
336 337
	}

338 339
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
		struct flow_match_vlan match;
340

341
		flow_rule_match_vlan(rule, &match);
342 343 344 345
		if (mlxsw_sp_acl_block_is_egress_bound(block)) {
			NL_SET_ERR_MSG_MOD(f->common.extack, "vlan_id key is not supported on egress");
			return -EOPNOTSUPP;
		}
346
		if (match.mask->vlan_id != 0)
347 348
			mlxsw_sp_acl_rulei_keymask_u32(rulei,
						       MLXSW_AFK_ELEMENT_VID,
349 350 351
						       match.key->vlan_id,
						       match.mask->vlan_id);
		if (match.mask->vlan_priority != 0)
352 353
			mlxsw_sp_acl_rulei_keymask_u32(rulei,
						       MLXSW_AFK_ELEMENT_PCP,
354 355
						       match.key->vlan_priority,
						       match.mask->vlan_priority);
356 357
	}

358 359 360 361 362 363 364
	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS)
		mlxsw_sp_flower_parse_ipv4(rulei, f);

	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS)
		mlxsw_sp_flower_parse_ipv6(rulei, f);

	err = mlxsw_sp_flower_parse_ports(mlxsw_sp, rulei, f, ip_proto);
365 366 367
	if (err)
		return err;
	err = mlxsw_sp_flower_parse_tcp(mlxsw_sp, rulei, f, ip_proto);
368 369 370
	if (err)
		return err;

371 372 373 374
	err = mlxsw_sp_flower_parse_ip(mlxsw_sp, rulei, f, n_proto_key & n_proto_mask);
	if (err)
		return err;

375 376
	return mlxsw_sp_flower_parse_actions(mlxsw_sp, block, rulei,
					     &f->rule->action,
377
					     f->common.extack);
378 379
}

380 381
int mlxsw_sp_flower_replace(struct mlxsw_sp *mlxsw_sp,
			    struct mlxsw_sp_acl_block *block,
382
			    struct tc_cls_flower_offload *f)
383 384 385 386 387 388
{
	struct mlxsw_sp_acl_rule_info *rulei;
	struct mlxsw_sp_acl_ruleset *ruleset;
	struct mlxsw_sp_acl_rule *rule;
	int err;

389
	ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
390
					   f->common.chain_index,
391
					   MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
392 393 394
	if (IS_ERR(ruleset))
		return PTR_ERR(ruleset);

395
	rule = mlxsw_sp_acl_rule_create(mlxsw_sp, ruleset, f->cookie, NULL,
396
					f->common.extack);
397 398 399 400 401 402
	if (IS_ERR(rule)) {
		err = PTR_ERR(rule);
		goto err_rule_create;
	}

	rulei = mlxsw_sp_acl_rule_rulei(rule);
403
	err = mlxsw_sp_flower_parse(mlxsw_sp, block, rulei, f);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	if (err)
		goto err_flower_parse;

	err = mlxsw_sp_acl_rulei_commit(rulei);
	if (err)
		goto err_rulei_commit;

	err = mlxsw_sp_acl_rule_add(mlxsw_sp, rule);
	if (err)
		goto err_rule_add;

	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
	return 0;

err_rule_add:
err_rulei_commit:
err_flower_parse:
	mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
err_rule_create:
	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
	return err;
}

427 428
void mlxsw_sp_flower_destroy(struct mlxsw_sp *mlxsw_sp,
			     struct mlxsw_sp_acl_block *block,
429 430 431 432 433
			     struct tc_cls_flower_offload *f)
{
	struct mlxsw_sp_acl_ruleset *ruleset;
	struct mlxsw_sp_acl_rule *rule;

434 435
	ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
					   f->common.chain_index,
436
					   MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
437
	if (IS_ERR(ruleset))
438 439 440
		return;

	rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
441
	if (rule) {
442 443 444 445 446 447
		mlxsw_sp_acl_rule_del(mlxsw_sp, rule);
		mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
	}

	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
}
448

449 450
int mlxsw_sp_flower_stats(struct mlxsw_sp *mlxsw_sp,
			  struct mlxsw_sp_acl_block *block,
451 452 453 454 455 456 457 458 459
			  struct tc_cls_flower_offload *f)
{
	struct mlxsw_sp_acl_ruleset *ruleset;
	struct mlxsw_sp_acl_rule *rule;
	u64 packets;
	u64 lastuse;
	u64 bytes;
	int err;

460 461
	ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
					   f->common.chain_index,
462
					   MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
463 464 465 466 467 468 469
	if (WARN_ON(IS_ERR(ruleset)))
		return -EINVAL;

	rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
	if (!rule)
		return -EINVAL;

470
	err = mlxsw_sp_acl_rule_get_stats(mlxsw_sp, rule, &packets, &bytes,
471 472 473 474
					  &lastuse);
	if (err)
		goto err_rule_get_stats;

475
	flow_stats_update(&f->stats, bytes, packets, lastuse);
476 477 478 479 480 481 482 483

	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
	return 0;

err_rule_get_stats:
	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
	return err;
}
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

int mlxsw_sp_flower_tmplt_create(struct mlxsw_sp *mlxsw_sp,
				 struct mlxsw_sp_acl_block *block,
				 struct tc_cls_flower_offload *f)
{
	struct mlxsw_sp_acl_ruleset *ruleset;
	struct mlxsw_sp_acl_rule_info rulei;
	int err;

	memset(&rulei, 0, sizeof(rulei));
	err = mlxsw_sp_flower_parse(mlxsw_sp, block, &rulei, f);
	if (err)
		return err;
	ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
					   f->common.chain_index,
					   MLXSW_SP_ACL_PROFILE_FLOWER,
					   &rulei.values.elusage);
501

502
	/* keep the reference to the ruleset */
503
	return PTR_ERR_OR_ZERO(ruleset);
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

void mlxsw_sp_flower_tmplt_destroy(struct mlxsw_sp *mlxsw_sp,
				   struct mlxsw_sp_acl_block *block,
				   struct tc_cls_flower_offload *f)
{
	struct mlxsw_sp_acl_ruleset *ruleset;

	ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, block,
					   f->common.chain_index,
					   MLXSW_SP_ACL_PROFILE_FLOWER, NULL);
	if (IS_ERR(ruleset))
		return;
	/* put the reference to the ruleset kept in create */
	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
	mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
}