cxgb4_tc_flower.c 29.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
 *
 * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <net/tc_act/tc_mirred.h>
36 37
#include <net/tc_act/tc_pedit.h>
#include <net/tc_act/tc_gact.h>
38
#include <net/tc_act/tc_vlan.h>
39 40

#include "cxgb4.h"
41
#include "cxgb4_filter.h"
42 43
#include "cxgb4_tc_flower.h"

44 45
#define STATS_CHECK_PERIOD (HZ / 2)

W
Wei Yongjun 已提交
46
static struct ch_tc_pedit_fields pedits[] = {
47 48
	PEDIT_FIELDS(ETH_, DMAC_31_0, 4, dmac, 0),
	PEDIT_FIELDS(ETH_, DMAC_47_32, 2, dmac, 4),
49 50
	PEDIT_FIELDS(ETH_, SMAC_15_0, 2, smac, 0),
	PEDIT_FIELDS(ETH_, SMAC_47_16, 4, smac, 2),
51 52 53 54 55 56 57 58 59 60
	PEDIT_FIELDS(IP4_, SRC, 4, nat_fip, 0),
	PEDIT_FIELDS(IP4_, DST, 4, nat_lip, 0),
	PEDIT_FIELDS(IP6_, SRC_31_0, 4, nat_fip, 0),
	PEDIT_FIELDS(IP6_, SRC_63_32, 4, nat_fip, 4),
	PEDIT_FIELDS(IP6_, SRC_95_64, 4, nat_fip, 8),
	PEDIT_FIELDS(IP6_, SRC_127_96, 4, nat_fip, 12),
	PEDIT_FIELDS(IP6_, DST_31_0, 4, nat_lip, 0),
	PEDIT_FIELDS(IP6_, DST_63_32, 4, nat_lip, 4),
	PEDIT_FIELDS(IP6_, DST_95_64, 4, nat_lip, 8),
	PEDIT_FIELDS(IP6_, DST_127_96, 4, nat_lip, 12),
61 62
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static const struct cxgb4_natmode_config cxgb4_natmode_config_array[] = {
	/* Default supported NAT modes */
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_NONE,
		.natmode = NAT_MODE_NONE,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP,
		.natmode = NAT_MODE_DIP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT,
		.natmode = NAT_MODE_DIP_DP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
			 CXGB4_ACTION_NATMODE_SIP,
		.natmode = NAT_MODE_DIP_DP_SIP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
			 CXGB4_ACTION_NATMODE_SPORT,
		.natmode = NAT_MODE_DIP_DP_SP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_SIP | CXGB4_ACTION_NATMODE_SPORT,
		.natmode = NAT_MODE_SIP_SP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
			 CXGB4_ACTION_NATMODE_SPORT,
		.natmode = NAT_MODE_DIP_SIP_SP,
	},
	{
		.chip = CHELSIO_T5,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
			 CXGB4_ACTION_NATMODE_DPORT |
			 CXGB4_ACTION_NATMODE_SPORT,
		.natmode = NAT_MODE_ALL,
	},
	/* T6+ can ignore L4 ports when they're disabled. */
	{
		.chip = CHELSIO_T6,
		.flags = CXGB4_ACTION_NATMODE_SIP,
		.natmode = NAT_MODE_SIP_SP,
	},
	{
		.chip = CHELSIO_T6,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SPORT,
		.natmode = NAT_MODE_DIP_DP_SP,
	},
	{
		.chip = CHELSIO_T6,
		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP,
		.natmode = NAT_MODE_ALL,
	},
};

static void cxgb4_action_natmode_tweak(struct ch_filter_specification *fs,
				       u8 natmode_flags)
{
	u8 i = 0;

	/* Translate the enabled NAT 4-tuple fields to one of the
	 * hardware supported NAT mode configurations. This ensures
	 * that we pick a valid combination, where the disabled fields
	 * do not get overwritten to 0.
	 */
	for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
		if (cxgb4_natmode_config_array[i].flags == natmode_flags) {
			fs->nat_mode = cxgb4_natmode_config_array[i].natmode;
			return;
		}
	}
}

146 147 148
static struct ch_tc_flower_entry *allocate_flower_entry(void)
{
	struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
149 150
	if (new)
		spin_lock_init(&new->lock);
151 152 153 154 155 156 157
	return new;
}

/* Must be called with either RTNL or rcu_read_lock */
static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
						   unsigned long flower_cookie)
{
158 159
	return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
				      adap->flower_ht_params);
160 161 162
}

static void cxgb4_process_flow_match(struct net_device *dev,
163
				     struct flow_rule *rule,
164 165
				     struct ch_filter_specification *fs)
{
166 167 168 169 170 171 172 173 174 175 176 177 178
	u16 addr_type = 0;

	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;
	} else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
		addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
	} else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
		addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
	}

179 180 181 182 183 184 185
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
		struct flow_match_basic match;
		u16 ethtype_key, ethtype_mask;

		flow_rule_match_basic(rule, &match);
		ethtype_key = ntohs(match.key->n_proto);
		ethtype_mask = ntohs(match.mask->n_proto);
186 187 188 189 190 191

		if (ethtype_key == ETH_P_ALL) {
			ethtype_key = 0;
			ethtype_mask = 0;
		}

192 193 194
		if (ethtype_key == ETH_P_IPV6)
			fs->type = 1;

195 196
		fs->val.ethtype = ethtype_key;
		fs->mask.ethtype = ethtype_mask;
197 198
		fs->val.proto = match.key->ip_proto;
		fs->mask.proto = match.mask->ip_proto;
199 200
	}

201
	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
202 203 204
		struct flow_match_ipv4_addrs match;

		flow_rule_match_ipv4_addrs(rule, &match);
205
		fs->type = 0;
206 207 208 209
		memcpy(&fs->val.lip[0], &match.key->dst, sizeof(match.key->dst));
		memcpy(&fs->val.fip[0], &match.key->src, sizeof(match.key->src));
		memcpy(&fs->mask.lip[0], &match.mask->dst, sizeof(match.mask->dst));
		memcpy(&fs->mask.fip[0], &match.mask->src, sizeof(match.mask->src));
210 211

		/* also initialize nat_lip/fip to same values */
212 213
		memcpy(&fs->nat_lip[0], &match.key->dst, sizeof(match.key->dst));
		memcpy(&fs->nat_fip[0], &match.key->src, sizeof(match.key->src));
214 215
	}

216
	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
217
		struct flow_match_ipv6_addrs match;
218

219
		flow_rule_match_ipv6_addrs(rule, &match);
220
		fs->type = 1;
221 222 223 224 225 226 227 228
		memcpy(&fs->val.lip[0], match.key->dst.s6_addr,
		       sizeof(match.key->dst));
		memcpy(&fs->val.fip[0], match.key->src.s6_addr,
		       sizeof(match.key->src));
		memcpy(&fs->mask.lip[0], match.mask->dst.s6_addr,
		       sizeof(match.mask->dst));
		memcpy(&fs->mask.fip[0], match.mask->src.s6_addr,
		       sizeof(match.mask->src));
229 230

		/* also initialize nat_lip/fip to same values */
231 232 233 234
		memcpy(&fs->nat_lip[0], match.key->dst.s6_addr,
		       sizeof(match.key->dst));
		memcpy(&fs->nat_fip[0], match.key->src.s6_addr,
		       sizeof(match.key->src));
235 236
	}

237 238
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
		struct flow_match_ports match;
239

240
		flow_rule_match_ports(rule, &match);
241 242 243 244
		fs->val.lport = be16_to_cpu(match.key->dst);
		fs->mask.lport = be16_to_cpu(match.mask->dst);
		fs->val.fport = be16_to_cpu(match.key->src);
		fs->mask.fport = be16_to_cpu(match.mask->src);
245 246

		/* also initialize nat_lport/fport to same values */
247 248
		fs->nat_lport = fs->val.lport;
		fs->nat_fport = fs->val.fport;
249 250
	}

251 252 253 254 255 256
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
		struct flow_match_ip match;

		flow_rule_match_ip(rule, &match);
		fs->val.tos = match.key->tos;
		fs->mask.tos = match.mask->tos;
257 258
	}

259 260 261 262 263 264
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
		struct flow_match_enc_keyid match;

		flow_rule_match_enc_keyid(rule, &match);
		fs->val.vni = be32_to_cpu(match.key->keyid);
		fs->mask.vni = be32_to_cpu(match.mask->keyid);
265 266 267 268 269 270
		if (fs->mask.vni) {
			fs->val.encap_vld = 1;
			fs->mask.encap_vld = 1;
		}
	}

271 272
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
		struct flow_match_vlan match;
273 274
		u16 vlan_tci, vlan_tci_mask;

275 276 277 278 279
		flow_rule_match_vlan(rule, &match);
		vlan_tci = match.key->vlan_id | (match.key->vlan_priority <<
					       VLAN_PRIO_SHIFT);
		vlan_tci_mask = match.mask->vlan_id | (match.mask->vlan_priority <<
						     VLAN_PRIO_SHIFT);
280 281
		fs->val.ivlan = vlan_tci;
		fs->mask.ivlan = vlan_tci_mask;
282

283 284 285
		fs->val.ivlan_vld = 1;
		fs->mask.ivlan_vld = 1;

286 287 288 289 290 291 292 293 294 295 296 297 298
		/* Chelsio adapters use ivlan_vld bit to match vlan packets
		 * as 802.1Q. Also, when vlan tag is present in packets,
		 * ethtype match is used then to match on ethtype of inner
		 * header ie. the header following the vlan header.
		 * So, set the ivlan_vld based on ethtype info supplied by
		 * TC for vlan packets if its 802.1Q. And then reset the
		 * ethtype value else, hw will try to match the supplied
		 * ethtype value with ethtype of inner header.
		 */
		if (fs->val.ethtype == ETH_P_8021Q) {
			fs->val.ethtype = 0;
			fs->mask.ethtype = 0;
		}
299 300 301 302 303 304 305 306 307 308
	}

	/* Match only packets coming from the ingress port where this
	 * filter will be created.
	 */
	fs->val.iport = netdev2pinfo(dev)->port_id;
	fs->mask.iport = ~0;
}

static int cxgb4_validate_flow_match(struct net_device *dev,
309
				     struct flow_rule *rule)
310
{
311
	struct flow_dissector *dissector = rule->match.dissector;
312 313 314
	u16 ethtype_mask = 0;
	u16 ethtype_key = 0;

315
	if (dissector->used_keys &
316 317 318 319
	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
320
	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
321
	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
322
	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
323
	      BIT(FLOW_DISSECTOR_KEY_IP))) {
324
		netdev_warn(dev, "Unsupported key used: 0x%x\n",
325
			    dissector->used_keys);
326 327
		return -EOPNOTSUPP;
	}
328

329 330 331 332 333 334
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
		struct flow_match_basic match;

		flow_rule_match_basic(rule, &match);
		ethtype_key = ntohs(match.key->n_proto);
		ethtype_mask = ntohs(match.mask->n_proto);
335 336
	}

337
	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
338
		u16 eth_ip_type = ethtype_key & ethtype_mask;
339
		struct flow_match_ip match;
340 341 342 343 344 345

		if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
			netdev_err(dev, "IP Key supported only with IPv4/v6");
			return -EINVAL;
		}

346 347
		flow_rule_match_ip(rule, &match);
		if (match.mask->ttl) {
348 349 350 351 352
			netdev_warn(dev, "ttl match unsupported for offload");
			return -EOPNOTSUPP;
		}
	}

353 354 355
	return 0;
}

356 357 358 359
static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
			  u8 field)
{
	u32 set_val = val & ~mask;
360 361
	u32 offset = 0;
	u8 size = 1;
362 363 364 365 366 367 368 369 370 371 372 373 374
	int i;

	for (i = 0; i < ARRAY_SIZE(pedits); i++) {
		if (pedits[i].field == field) {
			offset = pedits[i].offset;
			size = pedits[i].size;
			break;
		}
	}
	memcpy((u8 *)fs + offset, &set_val, size);
}

static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
375 376
				u32 mask, u32 offset, u8 htype,
				u8 *natmode_flags)
377 378
{
	switch (htype) {
379
	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
380 381 382 383 384 385 386 387
		switch (offset) {
		case PEDIT_ETH_DMAC_31_0:
			fs->newdmac = 1;
			offload_pedit(fs, val, mask, ETH_DMAC_31_0);
			break;
		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
			if (~mask & PEDIT_ETH_DMAC_MASK)
				offload_pedit(fs, val, mask, ETH_DMAC_47_32);
388 389 390 391 392 393 394
			else
				offload_pedit(fs, val >> 16, mask >> 16,
					      ETH_SMAC_15_0);
			break;
		case PEDIT_ETH_SMAC_47_16:
			fs->newsmac = 1;
			offload_pedit(fs, val, mask, ETH_SMAC_47_16);
395
		}
396
		break;
397
	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
398 399 400
		switch (offset) {
		case PEDIT_IP4_SRC:
			offload_pedit(fs, val, mask, IP4_SRC);
401
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
402 403 404
			break;
		case PEDIT_IP4_DST:
			offload_pedit(fs, val, mask, IP4_DST);
405
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
406 407
		}
		break;
408
	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
409 410 411
		switch (offset) {
		case PEDIT_IP6_SRC_31_0:
			offload_pedit(fs, val, mask, IP6_SRC_31_0);
412
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
413 414 415
			break;
		case PEDIT_IP6_SRC_63_32:
			offload_pedit(fs, val, mask, IP6_SRC_63_32);
416
			*natmode_flags |=  CXGB4_ACTION_NATMODE_SIP;
417 418 419
			break;
		case PEDIT_IP6_SRC_95_64:
			offload_pedit(fs, val, mask, IP6_SRC_95_64);
420
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
421 422 423
			break;
		case PEDIT_IP6_SRC_127_96:
			offload_pedit(fs, val, mask, IP6_SRC_127_96);
424
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
425 426 427
			break;
		case PEDIT_IP6_DST_31_0:
			offload_pedit(fs, val, mask, IP6_DST_31_0);
428
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
429 430 431
			break;
		case PEDIT_IP6_DST_63_32:
			offload_pedit(fs, val, mask, IP6_DST_63_32);
432
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
433 434 435
			break;
		case PEDIT_IP6_DST_95_64:
			offload_pedit(fs, val, mask, IP6_DST_95_64);
436
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
437 438 439
			break;
		case PEDIT_IP6_DST_127_96:
			offload_pedit(fs, val, mask, IP6_DST_127_96);
440
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
441 442
		}
		break;
443
	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
444 445
		switch (offset) {
		case PEDIT_TCP_SPORT_DPORT:
446
			if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
447
				fs->nat_fport = val;
448 449
				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
			} else {
450
				fs->nat_lport = val >> 16;
451 452
				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
			}
453 454
		}
		break;
455
	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
456 457
		switch (offset) {
		case PEDIT_UDP_SPORT_DPORT:
458
			if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
459
				fs->nat_fport = val;
460 461
				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
			} else {
462
				fs->nat_lport = val >> 16;
463 464
				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
			}
465
		}
466
		break;
467 468 469
	}
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
static int cxgb4_action_natmode_validate(struct adapter *adap, u8 natmode_flags,
					 struct netlink_ext_ack *extack)
{
	u8 i = 0;

	/* Extract the NAT mode to enable based on what 4-tuple fields
	 * are enabled to be overwritten. This ensures that the
	 * disabled fields don't get overwritten to 0.
	 */
	for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
		const struct cxgb4_natmode_config *c;

		c = &cxgb4_natmode_config_array[i];
		if (CHELSIO_CHIP_VERSION(adap->params.chip) >= c->chip &&
		    natmode_flags == c->flags)
			return 0;
	}
	NL_SET_ERR_MSG_MOD(extack, "Unsupported NAT mode 4-tuple combination");
	return -EOPNOTSUPP;
}

491 492 493
void cxgb4_process_flow_actions(struct net_device *in,
				struct flow_action *actions,
				struct ch_filter_specification *fs)
494
{
495
	struct flow_action_entry *act;
496
	u8 natmode_flags = 0;
497
	int i;
498

499
	flow_action_for_each(i, act, actions) {
500 501
		switch (act->id) {
		case FLOW_ACTION_ACCEPT:
502
			fs->action = FILTER_PASS;
503 504
			break;
		case FLOW_ACTION_DROP:
505
			fs->action = FILTER_DROP;
506
			break;
507
		case FLOW_ACTION_MIRRED:
508 509
		case FLOW_ACTION_REDIRECT: {
			struct net_device *out = act->dev;
510 511 512 513
			struct port_info *pi = netdev_priv(out);

			fs->action = FILTER_SWITCH;
			fs->eport = pi->port_id;
514 515 516 517 518 519 520
			}
			break;
		case FLOW_ACTION_VLAN_POP:
		case FLOW_ACTION_VLAN_PUSH:
		case FLOW_ACTION_VLAN_MANGLE: {
			u8 prio = act->vlan.prio;
			u16 vid = act->vlan.vid;
521
			u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
522 523
			switch (act->id) {
			case FLOW_ACTION_VLAN_POP:
524 525
				fs->newvlan |= VLAN_REMOVE;
				break;
526
			case FLOW_ACTION_VLAN_PUSH:
527 528 529
				fs->newvlan |= VLAN_INSERT;
				fs->vlan = vlan_tci;
				break;
530
			case FLOW_ACTION_VLAN_MANGLE:
531 532 533 534 535 536
				fs->newvlan |= VLAN_REWRITE;
				fs->vlan = vlan_tci;
				break;
			default:
				break;
			}
537 538 539
			}
			break;
		case FLOW_ACTION_MANGLE: {
540 541 542
			u32 mask, val, offset;
			u8 htype;

543 544 545 546
			htype = act->mangle.htype;
			mask = act->mangle.mask;
			val = act->mangle.val;
			offset = act->mangle.offset;
547

548 549
			process_pedit_field(fs, val, mask, offset, htype,
					    &natmode_flags);
550
			}
551
			break;
552 553 554 555 556
		case FLOW_ACTION_QUEUE:
			fs->action = FILTER_PASS;
			fs->dirsteer = 1;
			fs->iq = act->queue.index;
			break;
557 558
		default:
			break;
559 560
		}
	}
561 562 563
	if (natmode_flags)
		cxgb4_action_natmode_tweak(fs, natmode_flags);

564 565
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579
static bool valid_l4_mask(u32 mask)
{
	u16 hi, lo;

	/* Either the upper 16-bits (SPORT) OR the lower
	 * 16-bits (DPORT) can be set, but NOT BOTH.
	 */
	hi = (mask >> 16) & 0xFFFF;
	lo = mask & 0xFFFF;

	return hi && lo ? false : true;
}

static bool valid_pedit_action(struct net_device *dev,
580 581
			       const struct flow_action_entry *act,
			       u8 *natmode_flags)
582 583
{
	u32 mask, offset;
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	u8 htype;

	htype = act->mangle.htype;
	mask = act->mangle.mask;
	offset = act->mangle.offset;

	switch (htype) {
	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
		switch (offset) {
		case PEDIT_ETH_DMAC_31_0:
		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
		case PEDIT_ETH_SMAC_47_16:
			break;
		default:
			netdev_err(dev, "%s: Unsupported pedit field\n",
599 600 601
				   __func__);
			return false;
		}
602 603 604 605
		break;
	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
		switch (offset) {
		case PEDIT_IP4_SRC:
606 607
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
			break;
608
		case PEDIT_IP4_DST:
609
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
610
			break;
611 612 613 614 615 616 617 618 619 620 621 622
		default:
			netdev_err(dev, "%s: Unsupported pedit field\n",
				   __func__);
			return false;
		}
		break;
	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
		switch (offset) {
		case PEDIT_IP6_SRC_31_0:
		case PEDIT_IP6_SRC_63_32:
		case PEDIT_IP6_SRC_95_64:
		case PEDIT_IP6_SRC_127_96:
623 624
			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
			break;
625 626 627 628
		case PEDIT_IP6_DST_31_0:
		case PEDIT_IP6_DST_63_32:
		case PEDIT_IP6_DST_95_64:
		case PEDIT_IP6_DST_127_96:
629
			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
630
			break;
631 632 633 634 635 636 637 638 639 640 641
		default:
			netdev_err(dev, "%s: Unsupported pedit field\n",
				   __func__);
			return false;
		}
		break;
	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
		switch (offset) {
		case PEDIT_TCP_SPORT_DPORT:
			if (!valid_l4_mask(~mask)) {
				netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
642 643 644
					   __func__);
				return false;
			}
645 646 647 648
			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
			else
				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
649
			break;
650 651 652 653 654 655 656 657 658 659 660
		default:
			netdev_err(dev, "%s: Unsupported pedit field\n",
				   __func__);
			return false;
		}
		break;
	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
		switch (offset) {
		case PEDIT_UDP_SPORT_DPORT:
			if (!valid_l4_mask(~mask)) {
				netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
661 662 663
					   __func__);
				return false;
			}
664 665 666 667
			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
			else
				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
668 669
			break;
		default:
670
			netdev_err(dev, "%s: Unsupported pedit field\n",
671 672
				   __func__);
			return false;
673
		}
674 675 676 677
		break;
	default:
		netdev_err(dev, "%s: Unsupported pedit type\n", __func__);
		return false;
678
	}
679
	return true;
680 681
}

682
int cxgb4_validate_flow_actions(struct net_device *dev,
683
				struct flow_action *actions,
684 685
				struct netlink_ext_ack *extack,
				u8 matchall_filter)
686
{
687
	struct adapter *adap = netdev2adap(dev);
688
	struct flow_action_entry *act;
689 690 691
	bool act_redir = false;
	bool act_pedit = false;
	bool act_vlan = false;
692
	u8 natmode_flags = 0;
693
	int i;
694

695
	if (!flow_action_basic_hw_stats_check(actions, extack))
696 697
		return -EOPNOTSUPP;

698
	flow_action_for_each(i, act, actions) {
699 700 701
		switch (act->id) {
		case FLOW_ACTION_ACCEPT:
		case FLOW_ACTION_DROP:
702
			/* Do nothing */
703
			break;
704
		case FLOW_ACTION_MIRRED:
705
		case FLOW_ACTION_REDIRECT: {
706
			struct net_device *n_dev, *target_dev;
707
			bool found = false;
708 709 710 711 712 713 714 715
			unsigned int i;

			if (act->id == FLOW_ACTION_MIRRED &&
			    !matchall_filter) {
				NL_SET_ERR_MSG_MOD(extack,
						   "Egress mirror action is only supported for tc-matchall");
				return -EOPNOTSUPP;
			}
716

717
			target_dev = act->dev;
718 719
			for_each_port(adap, i) {
				n_dev = adap->port[i];
720
				if (target_dev == n_dev) {
721 722 723 724 725 726 727 728 729 730 731 732 733
					found = true;
					break;
				}
			}

			/* If interface doesn't belong to our hw, then
			 * the provided output port is not valid
			 */
			if (!found) {
				netdev_err(dev, "%s: Out port invalid\n",
					   __func__);
				return -EINVAL;
			}
734
			act_redir = true;
735 736 737 738 739 740
			}
			break;
		case FLOW_ACTION_VLAN_POP:
		case FLOW_ACTION_VLAN_PUSH:
		case FLOW_ACTION_VLAN_MANGLE: {
			u16 proto = be16_to_cpu(act->vlan.proto);
741

742 743
			switch (act->id) {
			case FLOW_ACTION_VLAN_POP:
744
				break;
745 746
			case FLOW_ACTION_VLAN_PUSH:
			case FLOW_ACTION_VLAN_MANGLE:
747 748 749 750 751 752 753 754 755 756 757
				if (proto != ETH_P_8021Q) {
					netdev_err(dev, "%s: Unsupported vlan proto\n",
						   __func__);
					return -EOPNOTSUPP;
				}
				break;
			default:
				netdev_err(dev, "%s: Unsupported vlan action\n",
					   __func__);
				return -EOPNOTSUPP;
			}
758
			act_vlan = true;
759 760 761
			}
			break;
		case FLOW_ACTION_MANGLE: {
762 763
			bool pedit_valid = valid_pedit_action(dev, act,
							      &natmode_flags);
764

765 766
			if (!pedit_valid)
				return -EOPNOTSUPP;
767
			act_pedit = true;
768 769
			}
			break;
770 771 772
		case FLOW_ACTION_QUEUE:
			/* Do nothing. cxgb4_set_filter will validate */
			break;
773
		default:
774 775 776 777
			netdev_err(dev, "%s: Unsupported action\n", __func__);
			return -EOPNOTSUPP;
		}
	}
778 779 780 781 782 783 784

	if ((act_pedit || act_vlan) && !act_redir) {
		netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
			   __func__);
		return -EINVAL;
	}

785 786 787 788 789 790 791 792 793
	if (act_pedit) {
		int ret;

		ret = cxgb4_action_natmode_validate(adap, natmode_flags,
						    extack);
		if (ret)
			return ret;
	}

794 795 796
	return 0;
}

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
static void cxgb4_tc_flower_hash_prio_add(struct adapter *adap, u32 tc_prio)
{
	spin_lock_bh(&adap->tids.ftid_lock);
	if (adap->tids.tc_hash_tids_max_prio < tc_prio)
		adap->tids.tc_hash_tids_max_prio = tc_prio;
	spin_unlock_bh(&adap->tids.ftid_lock);
}

static void cxgb4_tc_flower_hash_prio_del(struct adapter *adap, u32 tc_prio)
{
	struct tid_info *t = &adap->tids;
	struct ch_tc_flower_entry *fe;
	struct rhashtable_iter iter;
	u32 found = 0;

	spin_lock_bh(&t->ftid_lock);
	/* Bail if the current rule is not the one with the max
	 * prio.
	 */
	if (t->tc_hash_tids_max_prio != tc_prio)
		goto out_unlock;

	/* Search for the next rule having the same or next lower
	 * max prio.
	 */
	rhashtable_walk_enter(&adap->flower_tbl, &iter);
	do {
		rhashtable_walk_start(&iter);

		fe = rhashtable_walk_next(&iter);
		while (!IS_ERR_OR_NULL(fe)) {
			if (fe->fs.hash &&
			    fe->fs.tc_prio <= t->tc_hash_tids_max_prio) {
				t->tc_hash_tids_max_prio = fe->fs.tc_prio;
				found++;

				/* Bail if we found another rule
				 * having the same prio as the
				 * current max one.
				 */
				if (fe->fs.tc_prio == tc_prio)
					break;
			}

			fe = rhashtable_walk_next(&iter);
		}

		rhashtable_walk_stop(&iter);
	} while (fe == ERR_PTR(-EAGAIN));
	rhashtable_walk_exit(&iter);

	if (!found)
		t->tc_hash_tids_max_prio = 0;

out_unlock:
	spin_unlock_bh(&t->ftid_lock);
}

855 856 857
int cxgb4_flow_rule_replace(struct net_device *dev, struct flow_rule *rule,
			    u32 tc_prio, struct netlink_ext_ack *extack,
			    struct ch_filter_specification *fs, u32 *tid)
858
{
859 860
	struct adapter *adap = netdev2adap(dev);
	struct filter_ctx ctx;
861
	u8 inet_family;
862
	int fidx, ret;
863

864
	if (cxgb4_validate_flow_actions(dev, &rule->action, extack, 0))
865 866
		return -EOPNOTSUPP;

867
	if (cxgb4_validate_flow_match(dev, rule))
868 869
		return -EOPNOTSUPP;

870
	cxgb4_process_flow_match(dev, rule, fs);
871
	cxgb4_process_flow_actions(dev, &rule->action, fs);
872

873
	fs->hash = is_filter_exact_match(adap, fs);
874
	inet_family = fs->type ? PF_INET6 : PF_INET;
875

876 877 878 879 880
	/* Get a free filter entry TID, where we can insert this new
	 * rule. Only insert rule if its prio doesn't conflict with
	 * existing rules.
	 */
	fidx = cxgb4_get_free_ftid(dev, inet_family, fs->hash,
881
				   tc_prio);
882 883 884
	if (fidx < 0) {
		NL_SET_ERR_MSG_MOD(extack,
				   "No free LETCAM index available");
885
		return -ENOMEM;
886
	}
887

888 889 890
	if (fidx < adap->tids.nhpftids) {
		fs->prio = 1;
		fs->hash = 0;
891 892
	}

893 894 895 896 897 898
	/* If the rule can be inserted into HASH region, then ignore
	 * the index to normal FILTER region.
	 */
	if (fs->hash)
		fidx = 0;

899
	fs->tc_prio = tc_prio;
900

901 902 903 904 905
	init_completion(&ctx.completion);
	ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
	if (ret) {
		netdev_err(dev, "%s: filter creation err %d\n",
			   __func__, ret);
906
		return ret;
907 908 909 910
	}

	/* Wait for reply */
	ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
911 912
	if (!ret)
		return -ETIMEDOUT;
913 914

	/* Check if hw returned error for filter creation */
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	if (ctx.result)
		return ctx.result;

	*tid = ctx.tid;

	if (fs->hash)
		cxgb4_tc_flower_hash_prio_add(adap, tc_prio);

	return 0;
}

int cxgb4_tc_flower_replace(struct net_device *dev,
			    struct flow_cls_offload *cls)
{
	struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
	struct netlink_ext_ack *extack = cls->common.extack;
	struct adapter *adap = netdev2adap(dev);
	struct ch_tc_flower_entry *ch_flower;
	struct ch_filter_specification *fs;
	int ret;

	ch_flower = allocate_flower_entry();
	if (!ch_flower) {
		netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
		return -ENOMEM;
	}

	fs = &ch_flower->fs;
	fs->hitcnts = 1;
	fs->tc_cookie = cls->cookie;

	ret = cxgb4_flow_rule_replace(dev, rule, cls->common.prio, extack, fs,
				      &ch_flower->filter_id);
948
	if (ret)
949 950 951
		goto free_entry;

	ch_flower->tc_flower_cookie = cls->cookie;
952 953 954 955
	ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
				     adap->flower_ht_params);
	if (ret)
		goto del_filter;
956

957 958 959
	return 0;

del_filter:
960 961 962
	if (fs->hash)
		cxgb4_tc_flower_hash_prio_del(adap, cls->common.prio);

963
	cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
964 965 966 967

free_entry:
	kfree(ch_flower);
	return ret;
968 969
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
int cxgb4_flow_rule_destroy(struct net_device *dev, u32 tc_prio,
			    struct ch_filter_specification *fs, int tid)
{
	struct adapter *adap = netdev2adap(dev);
	u8 hash;
	int ret;

	hash = fs->hash;

	ret = cxgb4_del_filter(dev, tid, fs);
	if (ret)
		return ret;

	if (hash)
		cxgb4_tc_flower_hash_prio_del(adap, tc_prio);

	return ret;
}

989
int cxgb4_tc_flower_destroy(struct net_device *dev,
990
			    struct flow_cls_offload *cls)
991
{
992 993 994 995 996 997 998 999
	struct adapter *adap = netdev2adap(dev);
	struct ch_tc_flower_entry *ch_flower;
	int ret;

	ch_flower = ch_flower_lookup(adap, cls->cookie);
	if (!ch_flower)
		return -ENOENT;

1000 1001 1002
	rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
			       adap->flower_ht_params);

1003 1004
	ret = cxgb4_flow_rule_destroy(dev, ch_flower->fs.tc_prio,
				      &ch_flower->fs, ch_flower->filter_id);
1005
	if (ret)
1006 1007
		netdev_err(dev, "Flow rule destroy failed for tid: %u, ret: %d",
			   ch_flower->filter_id, ret);
1008 1009 1010

	kfree_rcu(ch_flower, rcu);
	return ret;
1011 1012
}

1013
static void ch_flower_stats_handler(struct work_struct *work)
1014
{
1015 1016
	struct adapter *adap = container_of(work, struct adapter,
					    flower_stats_work);
1017 1018
	struct ch_tc_flower_entry *flower_entry;
	struct ch_tc_flower_stats *ofld_stats;
1019
	struct rhashtable_iter iter;
1020 1021 1022 1023
	u64 packets;
	u64 bytes;
	int ret;

1024 1025
	rhashtable_walk_enter(&adap->flower_tbl, &iter);
	do {
1026
		rhashtable_walk_start(&iter);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

		while ((flower_entry = rhashtable_walk_next(&iter)) &&
		       !IS_ERR(flower_entry)) {
			ret = cxgb4_get_filter_counters(adap->port[0],
							flower_entry->filter_id,
							&packets, &bytes,
							flower_entry->fs.hash);
			if (!ret) {
				spin_lock(&flower_entry->lock);
				ofld_stats = &flower_entry->stats;

				if (ofld_stats->prev_packet_count != packets) {
					ofld_stats->prev_packet_count = packets;
					ofld_stats->last_used = jiffies;
				}
				spin_unlock(&flower_entry->lock);
1043 1044
			}
		}
1045

1046
		rhashtable_walk_stop(&iter);
1047

1048 1049
	} while (flower_entry == ERR_PTR(-EAGAIN));
	rhashtable_walk_exit(&iter);
1050 1051 1052
	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
}

1053 1054 1055 1056 1057 1058 1059
static void ch_flower_stats_cb(struct timer_list *t)
{
	struct adapter *adap = from_timer(adap, t, flower_stats_timer);

	schedule_work(&adap->flower_stats_work);
}

1060
int cxgb4_tc_flower_stats(struct net_device *dev,
1061
			  struct flow_cls_offload *cls)
1062
{
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	struct adapter *adap = netdev2adap(dev);
	struct ch_tc_flower_stats *ofld_stats;
	struct ch_tc_flower_entry *ch_flower;
	u64 packets;
	u64 bytes;
	int ret;

	ch_flower = ch_flower_lookup(adap, cls->cookie);
	if (!ch_flower) {
		ret = -ENOENT;
		goto err;
	}

	ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
1077 1078
					&packets, &bytes,
					ch_flower->fs.hash);
1079 1080 1081 1082 1083 1084 1085 1086
	if (ret < 0)
		goto err;

	spin_lock_bh(&ch_flower->lock);
	ofld_stats = &ch_flower->stats;
	if (ofld_stats->packet_count != packets) {
		if (ofld_stats->prev_packet_count != packets)
			ofld_stats->last_used = jiffies;
1087
		flow_stats_update(&cls->stats, bytes - ofld_stats->byte_count,
1088
				  packets - ofld_stats->packet_count, 0,
1089 1090
				  ofld_stats->last_used,
				  FLOW_ACTION_HW_STATS_IMMEDIATE);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

		ofld_stats->packet_count = packets;
		ofld_stats->byte_count = bytes;
		ofld_stats->prev_packet_count = packets;
	}
	spin_unlock_bh(&ch_flower->lock);
	return 0;

err:
	return ret;
1101
}
1102

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
	.nelem_hint = 384,
	.head_offset = offsetof(struct ch_tc_flower_entry, node),
	.key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
	.key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
	.max_size = 524288,
	.min_size = 512,
	.automatic_shrinking = true
};

int cxgb4_init_tc_flower(struct adapter *adap)
1114
{
1115 1116
	int ret;

1117 1118 1119
	if (adap->tc_flower_initialized)
		return -EEXIST;

1120 1121 1122 1123 1124 1125
	adap->flower_ht_params = cxgb4_tc_flower_ht_params;
	ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
	if (ret)
		return ret;

	INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
1126
	timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
1127
	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
1128
	adap->tc_flower_initialized = true;
1129
	return 0;
1130 1131 1132 1133
}

void cxgb4_cleanup_tc_flower(struct adapter *adap)
{
1134 1135 1136
	if (!adap->tc_flower_initialized)
		return;

1137
	if (adap->flower_stats_timer.function)
1138
		timer_shutdown_sync(&adap->flower_stats_timer);
1139 1140
	cancel_work_sync(&adap->flower_stats_work);
	rhashtable_destroy(&adap->flower_tbl);
1141
	adap->tc_flower_initialized = false;
1142
}