1 // SPDX-License-Identifier: GPL-2.0-only 2 /**************************************************************************** 3 * Driver for Solarflare network controllers and boards 4 * Copyright 2019 Solarflare Communications Inc. 5 * Copyright 2020-2022 Xilinx Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 as published 9 * by the Free Software Foundation, incorporated herein by reference. 10 */ 11 12 #include <net/pkt_cls.h> 13 #include <net/vxlan.h> 14 #include <net/geneve.h> 15 #include "tc.h" 16 #include "tc_bindings.h" 17 #include "mae.h" 18 #include "ef100_rep.h" 19 #include "efx.h" 20 21 static enum efx_encap_type efx_tc_indr_netdev_type(struct net_device *net_dev) 22 { 23 if (netif_is_vxlan(net_dev)) 24 return EFX_ENCAP_TYPE_VXLAN; 25 if (netif_is_geneve(net_dev)) 26 return EFX_ENCAP_TYPE_GENEVE; 27 28 return EFX_ENCAP_TYPE_NONE; 29 } 30 31 #define EFX_EFV_PF NULL 32 /* Look up the representor information (efv) for a device. 33 * May return NULL for the PF (us), or an error pointer for a device that 34 * isn't supported as a TC offload endpoint 35 */ 36 static struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx, 37 struct net_device *dev) 38 { 39 struct efx_rep *efv; 40 41 if (!dev) 42 return ERR_PTR(-EOPNOTSUPP); 43 /* Is it us (the PF)? */ 44 if (dev == efx->net_dev) 45 return EFX_EFV_PF; 46 /* Is it an efx vfrep at all? */ 47 if (dev->netdev_ops != &efx_ef100_rep_netdev_ops) 48 return ERR_PTR(-EOPNOTSUPP); 49 /* Is it ours? We don't support TC rules that include another 50 * EF100's netdevices (not even on another port of the same NIC). 51 */ 52 efv = netdev_priv(dev); 53 if (efv->parent != efx) 54 return ERR_PTR(-EOPNOTSUPP); 55 return efv; 56 } 57 58 /* Convert a driver-internal vport ID into an internal device (PF or VF) */ 59 static s64 efx_tc_flower_internal_mport(struct efx_nic *efx, struct efx_rep *efv) 60 { 61 u32 mport; 62 63 if (IS_ERR(efv)) 64 return PTR_ERR(efv); 65 if (!efv) /* device is PF (us) */ 66 efx_mae_mport_uplink(efx, &mport); 67 else /* device is repr */ 68 efx_mae_mport_mport(efx, efv->mport, &mport); 69 return mport; 70 } 71 72 /* Convert a driver-internal vport ID into an external device (wire or VF) */ 73 static s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv) 74 { 75 u32 mport; 76 77 if (IS_ERR(efv)) 78 return PTR_ERR(efv); 79 if (!efv) /* device is PF (us) */ 80 efx_mae_mport_wire(efx, &mport); 81 else /* device is repr */ 82 efx_mae_mport_mport(efx, efv->mport, &mport); 83 return mport; 84 } 85 86 static const struct rhashtable_params efx_tc_encap_match_ht_params = { 87 .key_len = offsetof(struct efx_tc_encap_match, linkage), 88 .key_offset = 0, 89 .head_offset = offsetof(struct efx_tc_encap_match, linkage), 90 }; 91 92 static const struct rhashtable_params efx_tc_match_action_ht_params = { 93 .key_len = sizeof(unsigned long), 94 .key_offset = offsetof(struct efx_tc_flow_rule, cookie), 95 .head_offset = offsetof(struct efx_tc_flow_rule, linkage), 96 }; 97 98 static void efx_tc_free_action_set(struct efx_nic *efx, 99 struct efx_tc_action_set *act, bool in_hw) 100 { 101 /* Failure paths calling this on the 'cursor' action set in_hw=false, 102 * because if the alloc had succeeded we'd've put it in acts.list and 103 * not still have it in act. 104 */ 105 if (in_hw) { 106 efx_mae_free_action_set(efx, act->fw_id); 107 /* in_hw is true iff we are on an acts.list; make sure to 108 * remove ourselves from that list before we are freed. 109 */ 110 list_del(&act->list); 111 } 112 if (act->count) 113 efx_tc_flower_put_counter_index(efx, act->count); 114 kfree(act); 115 } 116 117 static void efx_tc_free_action_set_list(struct efx_nic *efx, 118 struct efx_tc_action_set_list *acts, 119 bool in_hw) 120 { 121 struct efx_tc_action_set *act, *next; 122 123 /* Failure paths set in_hw=false, because usually the acts didn't get 124 * to efx_mae_alloc_action_set_list(); if they did, the failure tree 125 * has a separate efx_mae_free_action_set_list() before calling us. 126 */ 127 if (in_hw) 128 efx_mae_free_action_set_list(efx, acts); 129 /* Any act that's on the list will be in_hw even if the list isn't */ 130 list_for_each_entry_safe(act, next, &acts->list, list) 131 efx_tc_free_action_set(efx, act, true); 132 /* Don't kfree, as acts is embedded inside a struct efx_tc_flow_rule */ 133 } 134 135 /* Boilerplate for the simple 'copy a field' cases */ 136 #define _MAP_KEY_AND_MASK(_name, _type, _tcget, _tcfield, _field) \ 137 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_##_name)) { \ 138 struct flow_match_##_type fm; \ 139 \ 140 flow_rule_match_##_tcget(rule, &fm); \ 141 match->value._field = fm.key->_tcfield; \ 142 match->mask._field = fm.mask->_tcfield; \ 143 } 144 #define MAP_KEY_AND_MASK(_name, _type, _tcfield, _field) \ 145 _MAP_KEY_AND_MASK(_name, _type, _type, _tcfield, _field) 146 #define MAP_ENC_KEY_AND_MASK(_name, _type, _tcget, _tcfield, _field) \ 147 _MAP_KEY_AND_MASK(ENC_##_name, _type, _tcget, _tcfield, _field) 148 149 static int efx_tc_flower_parse_match(struct efx_nic *efx, 150 struct flow_rule *rule, 151 struct efx_tc_match *match, 152 struct netlink_ext_ack *extack) 153 { 154 struct flow_dissector *dissector = rule->match.dissector; 155 unsigned char ipv = 0; 156 157 /* Owing to internal TC infelicities, the IPV6_ADDRS key might be set 158 * even on IPv4 filters; so rather than relying on dissector->used_keys 159 * we check the addr_type in the CONTROL key. If we don't find it (or 160 * it's masked, which should never happen), we treat both IPV4_ADDRS 161 * and IPV6_ADDRS as absent. 162 */ 163 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) { 164 struct flow_match_control fm; 165 166 flow_rule_match_control(rule, &fm); 167 if (IS_ALL_ONES(fm.mask->addr_type)) 168 switch (fm.key->addr_type) { 169 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 170 ipv = 4; 171 break; 172 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 173 ipv = 6; 174 break; 175 default: 176 break; 177 } 178 179 if (fm.mask->flags & FLOW_DIS_IS_FRAGMENT) { 180 match->value.ip_frag = fm.key->flags & FLOW_DIS_IS_FRAGMENT; 181 match->mask.ip_frag = true; 182 } 183 if (fm.mask->flags & FLOW_DIS_FIRST_FRAG) { 184 match->value.ip_firstfrag = fm.key->flags & FLOW_DIS_FIRST_FRAG; 185 match->mask.ip_firstfrag = true; 186 } 187 if (fm.mask->flags & ~(FLOW_DIS_IS_FRAGMENT | FLOW_DIS_FIRST_FRAG)) { 188 NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported match on control.flags %#x", 189 fm.mask->flags); 190 return -EOPNOTSUPP; 191 } 192 } 193 if (dissector->used_keys & 194 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) | 195 BIT(FLOW_DISSECTOR_KEY_BASIC) | 196 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | 197 BIT(FLOW_DISSECTOR_KEY_VLAN) | 198 BIT(FLOW_DISSECTOR_KEY_CVLAN) | 199 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | 200 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | 201 BIT(FLOW_DISSECTOR_KEY_PORTS) | 202 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | 203 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | 204 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | 205 BIT(FLOW_DISSECTOR_KEY_ENC_IP) | 206 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | 207 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | 208 BIT(FLOW_DISSECTOR_KEY_TCP) | 209 BIT(FLOW_DISSECTOR_KEY_IP))) { 210 NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported flower keys %#x", 211 dissector->used_keys); 212 return -EOPNOTSUPP; 213 } 214 215 MAP_KEY_AND_MASK(BASIC, basic, n_proto, eth_proto); 216 /* Make sure we're IP if any L3/L4 keys used. */ 217 if (!IS_ALL_ONES(match->mask.eth_proto) || 218 !(match->value.eth_proto == htons(ETH_P_IP) || 219 match->value.eth_proto == htons(ETH_P_IPV6))) 220 if (dissector->used_keys & 221 (BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | 222 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | 223 BIT(FLOW_DISSECTOR_KEY_PORTS) | 224 BIT(FLOW_DISSECTOR_KEY_IP) | 225 BIT(FLOW_DISSECTOR_KEY_TCP))) { 226 NL_SET_ERR_MSG_FMT_MOD(extack, "L3/L4 flower keys %#x require protocol ipv[46]", 227 dissector->used_keys); 228 return -EINVAL; 229 } 230 231 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) { 232 struct flow_match_vlan fm; 233 234 flow_rule_match_vlan(rule, &fm); 235 if (fm.mask->vlan_id || fm.mask->vlan_priority || fm.mask->vlan_tpid) { 236 match->value.vlan_proto[0] = fm.key->vlan_tpid; 237 match->mask.vlan_proto[0] = fm.mask->vlan_tpid; 238 match->value.vlan_tci[0] = cpu_to_be16(fm.key->vlan_priority << 13 | 239 fm.key->vlan_id); 240 match->mask.vlan_tci[0] = cpu_to_be16(fm.mask->vlan_priority << 13 | 241 fm.mask->vlan_id); 242 } 243 } 244 245 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) { 246 struct flow_match_vlan fm; 247 248 flow_rule_match_cvlan(rule, &fm); 249 if (fm.mask->vlan_id || fm.mask->vlan_priority || fm.mask->vlan_tpid) { 250 match->value.vlan_proto[1] = fm.key->vlan_tpid; 251 match->mask.vlan_proto[1] = fm.mask->vlan_tpid; 252 match->value.vlan_tci[1] = cpu_to_be16(fm.key->vlan_priority << 13 | 253 fm.key->vlan_id); 254 match->mask.vlan_tci[1] = cpu_to_be16(fm.mask->vlan_priority << 13 | 255 fm.mask->vlan_id); 256 } 257 } 258 259 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 260 struct flow_match_eth_addrs fm; 261 262 flow_rule_match_eth_addrs(rule, &fm); 263 ether_addr_copy(match->value.eth_saddr, fm.key->src); 264 ether_addr_copy(match->value.eth_daddr, fm.key->dst); 265 ether_addr_copy(match->mask.eth_saddr, fm.mask->src); 266 ether_addr_copy(match->mask.eth_daddr, fm.mask->dst); 267 } 268 269 MAP_KEY_AND_MASK(BASIC, basic, ip_proto, ip_proto); 270 /* Make sure we're TCP/UDP if any L4 keys used. */ 271 if ((match->value.ip_proto != IPPROTO_UDP && 272 match->value.ip_proto != IPPROTO_TCP) || !IS_ALL_ONES(match->mask.ip_proto)) 273 if (dissector->used_keys & 274 (BIT(FLOW_DISSECTOR_KEY_PORTS) | 275 BIT(FLOW_DISSECTOR_KEY_TCP))) { 276 NL_SET_ERR_MSG_FMT_MOD(extack, "L4 flower keys %#x require ipproto udp or tcp", 277 dissector->used_keys); 278 return -EINVAL; 279 } 280 MAP_KEY_AND_MASK(IP, ip, tos, ip_tos); 281 MAP_KEY_AND_MASK(IP, ip, ttl, ip_ttl); 282 if (ipv == 4) { 283 MAP_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, src, src_ip); 284 MAP_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, dst, dst_ip); 285 } 286 #ifdef CONFIG_IPV6 287 else if (ipv == 6) { 288 MAP_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, src, src_ip6); 289 MAP_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, dst, dst_ip6); 290 } 291 #endif 292 MAP_KEY_AND_MASK(PORTS, ports, src, l4_sport); 293 MAP_KEY_AND_MASK(PORTS, ports, dst, l4_dport); 294 MAP_KEY_AND_MASK(TCP, tcp, flags, tcp_flags); 295 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) { 296 struct flow_match_control fm; 297 298 flow_rule_match_enc_control(rule, &fm); 299 if (fm.mask->flags) { 300 NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported match on enc_control.flags %#x", 301 fm.mask->flags); 302 return -EOPNOTSUPP; 303 } 304 if (!IS_ALL_ONES(fm.mask->addr_type)) { 305 NL_SET_ERR_MSG_FMT_MOD(extack, "Unsupported enc addr_type mask %u (key %u)", 306 fm.mask->addr_type, 307 fm.key->addr_type); 308 return -EOPNOTSUPP; 309 } 310 switch (fm.key->addr_type) { 311 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 312 MAP_ENC_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, enc_ipv4_addrs, 313 src, enc_src_ip); 314 MAP_ENC_KEY_AND_MASK(IPV4_ADDRS, ipv4_addrs, enc_ipv4_addrs, 315 dst, enc_dst_ip); 316 break; 317 #ifdef CONFIG_IPV6 318 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 319 MAP_ENC_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, enc_ipv6_addrs, 320 src, enc_src_ip6); 321 MAP_ENC_KEY_AND_MASK(IPV6_ADDRS, ipv6_addrs, enc_ipv6_addrs, 322 dst, enc_dst_ip6); 323 break; 324 #endif 325 default: 326 NL_SET_ERR_MSG_FMT_MOD(extack, 327 "Unsupported enc addr_type %u (supported are IPv4, IPv6)", 328 fm.key->addr_type); 329 return -EOPNOTSUPP; 330 } 331 MAP_ENC_KEY_AND_MASK(IP, ip, enc_ip, tos, enc_ip_tos); 332 MAP_ENC_KEY_AND_MASK(IP, ip, enc_ip, ttl, enc_ip_ttl); 333 MAP_ENC_KEY_AND_MASK(PORTS, ports, enc_ports, src, enc_sport); 334 MAP_ENC_KEY_AND_MASK(PORTS, ports, enc_ports, dst, enc_dport); 335 MAP_ENC_KEY_AND_MASK(KEYID, enc_keyid, enc_keyid, keyid, enc_keyid); 336 } else if (dissector->used_keys & 337 (BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | 338 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | 339 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | 340 BIT(FLOW_DISSECTOR_KEY_ENC_IP) | 341 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) { 342 NL_SET_ERR_MSG_FMT_MOD(extack, "Flower enc keys require enc_control (keys: %#x)", 343 dissector->used_keys); 344 return -EOPNOTSUPP; 345 } 346 347 return 0; 348 } 349 350 static void efx_tc_flower_release_encap_match(struct efx_nic *efx, 351 struct efx_tc_encap_match *encap) 352 { 353 int rc; 354 355 if (!refcount_dec_and_test(&encap->ref)) 356 return; /* still in use */ 357 358 if (encap->type == EFX_TC_EM_DIRECT) { 359 rc = efx_mae_unregister_encap_match(efx, encap); 360 if (rc) 361 /* Display message but carry on and remove entry from our 362 * SW tables, because there's not much we can do about it. 363 */ 364 netif_err(efx, drv, efx->net_dev, 365 "Failed to release encap match %#x, rc %d\n", 366 encap->fw_id, rc); 367 } 368 rhashtable_remove_fast(&efx->tc->encap_match_ht, &encap->linkage, 369 efx_tc_encap_match_ht_params); 370 if (encap->pseudo) 371 efx_tc_flower_release_encap_match(efx, encap->pseudo); 372 kfree(encap); 373 } 374 375 static int efx_tc_flower_record_encap_match(struct efx_nic *efx, 376 struct efx_tc_match *match, 377 enum efx_encap_type type, 378 enum efx_tc_em_pseudo_type em_type, 379 u8 child_ip_tos_mask, 380 __be16 child_udp_sport_mask, 381 struct netlink_ext_ack *extack) 382 { 383 struct efx_tc_encap_match *encap, *old, *pseudo = NULL; 384 bool ipv6 = false; 385 int rc; 386 387 /* We require that the socket-defining fields (IP addrs and UDP dest 388 * port) are present and exact-match. Other fields may only be used 389 * if the field-set (and any masks) are the same for all encap 390 * matches on the same <sip,dip,dport> tuple; this is enforced by 391 * pseudo encap matches. 392 */ 393 if (match->mask.enc_dst_ip | match->mask.enc_src_ip) { 394 if (!IS_ALL_ONES(match->mask.enc_dst_ip)) { 395 NL_SET_ERR_MSG_MOD(extack, 396 "Egress encap match is not exact on dst IP address"); 397 return -EOPNOTSUPP; 398 } 399 if (!IS_ALL_ONES(match->mask.enc_src_ip)) { 400 NL_SET_ERR_MSG_MOD(extack, 401 "Egress encap match is not exact on src IP address"); 402 return -EOPNOTSUPP; 403 } 404 #ifdef CONFIG_IPV6 405 if (!ipv6_addr_any(&match->mask.enc_dst_ip6) || 406 !ipv6_addr_any(&match->mask.enc_src_ip6)) { 407 NL_SET_ERR_MSG_MOD(extack, 408 "Egress encap match on both IPv4 and IPv6, don't understand"); 409 return -EOPNOTSUPP; 410 } 411 } else { 412 ipv6 = true; 413 if (!efx_ipv6_addr_all_ones(&match->mask.enc_dst_ip6)) { 414 NL_SET_ERR_MSG_MOD(extack, 415 "Egress encap match is not exact on dst IP address"); 416 return -EOPNOTSUPP; 417 } 418 if (!efx_ipv6_addr_all_ones(&match->mask.enc_src_ip6)) { 419 NL_SET_ERR_MSG_MOD(extack, 420 "Egress encap match is not exact on src IP address"); 421 return -EOPNOTSUPP; 422 } 423 #endif 424 } 425 if (!IS_ALL_ONES(match->mask.enc_dport)) { 426 NL_SET_ERR_MSG_MOD(extack, "Egress encap match is not exact on dst UDP port"); 427 return -EOPNOTSUPP; 428 } 429 if (match->mask.enc_sport || match->mask.enc_ip_tos) { 430 struct efx_tc_match pmatch = *match; 431 432 if (em_type == EFX_TC_EM_PSEUDO_MASK) { /* can't happen */ 433 NL_SET_ERR_MSG_MOD(extack, "Bad recursion in egress encap match handler"); 434 return -EOPNOTSUPP; 435 } 436 pmatch.value.enc_ip_tos = 0; 437 pmatch.mask.enc_ip_tos = 0; 438 pmatch.value.enc_sport = 0; 439 pmatch.mask.enc_sport = 0; 440 rc = efx_tc_flower_record_encap_match(efx, &pmatch, type, 441 EFX_TC_EM_PSEUDO_MASK, 442 match->mask.enc_ip_tos, 443 match->mask.enc_sport, 444 extack); 445 if (rc) 446 return rc; 447 pseudo = pmatch.encap; 448 } 449 if (match->mask.enc_ip_ttl) { 450 NL_SET_ERR_MSG_MOD(extack, "Egress encap match on IP TTL not supported"); 451 rc = -EOPNOTSUPP; 452 goto fail_pseudo; 453 } 454 455 rc = efx_mae_check_encap_match_caps(efx, ipv6, match->mask.enc_ip_tos, 456 match->mask.enc_sport, extack); 457 if (rc) 458 goto fail_pseudo; 459 460 encap = kzalloc(sizeof(*encap), GFP_USER); 461 if (!encap) { 462 rc = -ENOMEM; 463 goto fail_pseudo; 464 } 465 encap->src_ip = match->value.enc_src_ip; 466 encap->dst_ip = match->value.enc_dst_ip; 467 #ifdef CONFIG_IPV6 468 encap->src_ip6 = match->value.enc_src_ip6; 469 encap->dst_ip6 = match->value.enc_dst_ip6; 470 #endif 471 encap->udp_dport = match->value.enc_dport; 472 encap->tun_type = type; 473 encap->ip_tos = match->value.enc_ip_tos; 474 encap->ip_tos_mask = match->mask.enc_ip_tos; 475 encap->child_ip_tos_mask = child_ip_tos_mask; 476 encap->udp_sport = match->value.enc_sport; 477 encap->udp_sport_mask = match->mask.enc_sport; 478 encap->child_udp_sport_mask = child_udp_sport_mask; 479 encap->type = em_type; 480 encap->pseudo = pseudo; 481 old = rhashtable_lookup_get_insert_fast(&efx->tc->encap_match_ht, 482 &encap->linkage, 483 efx_tc_encap_match_ht_params); 484 if (old) { 485 /* don't need our new entry */ 486 kfree(encap); 487 if (pseudo) /* don't need our new pseudo either */ 488 efx_tc_flower_release_encap_match(efx, pseudo); 489 /* check old and new em_types are compatible */ 490 switch (old->type) { 491 case EFX_TC_EM_DIRECT: 492 /* old EM is in hardware, so mustn't overlap with a 493 * pseudo, but may be shared with another direct EM 494 */ 495 if (em_type == EFX_TC_EM_DIRECT) 496 break; 497 NL_SET_ERR_MSG_MOD(extack, "Pseudo encap match conflicts with existing direct entry"); 498 return -EEXIST; 499 case EFX_TC_EM_PSEUDO_MASK: 500 /* old EM is protecting a ToS- or src port-qualified 501 * filter, so may only be shared with another pseudo 502 * for the same ToS and src port masks. 503 */ 504 if (em_type != EFX_TC_EM_PSEUDO_MASK) { 505 NL_SET_ERR_MSG_FMT_MOD(extack, 506 "%s encap match conflicts with existing pseudo(MASK) entry", 507 em_type ? "Pseudo" : "Direct"); 508 return -EEXIST; 509 } 510 if (child_ip_tos_mask != old->child_ip_tos_mask) { 511 NL_SET_ERR_MSG_FMT_MOD(extack, 512 "Pseudo encap match for TOS mask %#04x conflicts with existing pseudo(MASK) entry for TOS mask %#04x", 513 child_ip_tos_mask, 514 old->child_ip_tos_mask); 515 return -EEXIST; 516 } 517 if (child_udp_sport_mask != old->child_udp_sport_mask) { 518 NL_SET_ERR_MSG_FMT_MOD(extack, 519 "Pseudo encap match for UDP src port mask %#x conflicts with existing pseudo(MASK) entry for mask %#x", 520 child_udp_sport_mask, 521 old->child_udp_sport_mask); 522 return -EEXIST; 523 } 524 break; 525 default: /* Unrecognised pseudo-type. Just say no */ 526 NL_SET_ERR_MSG_FMT_MOD(extack, 527 "%s encap match conflicts with existing pseudo(%d) entry", 528 em_type ? "Pseudo" : "Direct", 529 old->type); 530 return -EEXIST; 531 } 532 /* check old and new tun_types are compatible */ 533 if (old->tun_type != type) { 534 NL_SET_ERR_MSG_FMT_MOD(extack, 535 "Egress encap match with conflicting tun_type %u != %u", 536 old->tun_type, type); 537 return -EEXIST; 538 } 539 if (!refcount_inc_not_zero(&old->ref)) 540 return -EAGAIN; 541 /* existing entry found */ 542 encap = old; 543 } else { 544 if (em_type == EFX_TC_EM_DIRECT) { 545 rc = efx_mae_register_encap_match(efx, encap); 546 if (rc) { 547 NL_SET_ERR_MSG_MOD(extack, "Failed to record egress encap match in HW"); 548 goto fail; 549 } 550 } 551 refcount_set(&encap->ref, 1); 552 } 553 match->encap = encap; 554 return 0; 555 fail: 556 rhashtable_remove_fast(&efx->tc->encap_match_ht, &encap->linkage, 557 efx_tc_encap_match_ht_params); 558 kfree(encap); 559 fail_pseudo: 560 if (pseudo) 561 efx_tc_flower_release_encap_match(efx, pseudo); 562 return rc; 563 } 564 565 static void efx_tc_delete_rule(struct efx_nic *efx, struct efx_tc_flow_rule *rule) 566 { 567 efx_mae_delete_rule(efx, rule->fw_id); 568 569 /* Release entries in subsidiary tables */ 570 efx_tc_free_action_set_list(efx, &rule->acts, true); 571 if (rule->match.encap) 572 efx_tc_flower_release_encap_match(efx, rule->match.encap); 573 rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 574 } 575 576 static const char *efx_tc_encap_type_name(enum efx_encap_type typ) 577 { 578 switch (typ) { 579 case EFX_ENCAP_TYPE_NONE: 580 return "none"; 581 case EFX_ENCAP_TYPE_VXLAN: 582 return "vxlan"; 583 case EFX_ENCAP_TYPE_GENEVE: 584 return "geneve"; 585 default: 586 pr_warn_once("Unknown efx_encap_type %d encountered\n", typ); 587 return "unknown"; 588 } 589 } 590 591 /* For details of action order constraints refer to SF-123102-TC-1§12.6.1 */ 592 enum efx_tc_action_order { 593 EFX_TC_AO_DECAP, 594 EFX_TC_AO_VLAN_POP, 595 EFX_TC_AO_VLAN_PUSH, 596 EFX_TC_AO_COUNT, 597 EFX_TC_AO_DELIVER 598 }; 599 /* Determine whether we can add @new action without violating order */ 600 static bool efx_tc_flower_action_order_ok(const struct efx_tc_action_set *act, 601 enum efx_tc_action_order new) 602 { 603 switch (new) { 604 case EFX_TC_AO_DECAP: 605 if (act->decap) 606 return false; 607 fallthrough; 608 case EFX_TC_AO_VLAN_POP: 609 if (act->vlan_pop >= 2) 610 return false; 611 /* If we've already pushed a VLAN, we can't then pop it; 612 * the hardware would instead try to pop an existing VLAN 613 * before pushing the new one. 614 */ 615 if (act->vlan_push) 616 return false; 617 fallthrough; 618 case EFX_TC_AO_VLAN_PUSH: 619 if (act->vlan_push >= 2) 620 return false; 621 fallthrough; 622 case EFX_TC_AO_COUNT: 623 if (act->count) 624 return false; 625 fallthrough; 626 case EFX_TC_AO_DELIVER: 627 return !act->deliver; 628 default: 629 /* Bad caller. Whatever they wanted to do, say they can't. */ 630 WARN_ON_ONCE(1); 631 return false; 632 } 633 } 634 635 static int efx_tc_flower_replace_foreign(struct efx_nic *efx, 636 struct net_device *net_dev, 637 struct flow_cls_offload *tc) 638 { 639 struct flow_rule *fr = flow_cls_offload_flow_rule(tc); 640 struct netlink_ext_ack *extack = tc->common.extack; 641 struct efx_tc_flow_rule *rule = NULL, *old = NULL; 642 struct efx_tc_action_set *act = NULL; 643 bool found = false, uplinked = false; 644 const struct flow_action_entry *fa; 645 struct efx_tc_match match; 646 struct efx_rep *to_efv; 647 s64 rc; 648 int i; 649 650 /* Parse match */ 651 memset(&match, 0, sizeof(match)); 652 rc = efx_tc_flower_parse_match(efx, fr, &match, NULL); 653 if (rc) 654 return rc; 655 /* The rule as given to us doesn't specify a source netdevice. 656 * But, determining whether packets from a VF should match it is 657 * complicated, so leave those to the software slowpath: qualify 658 * the filter with source m-port == wire. 659 */ 660 rc = efx_tc_flower_external_mport(efx, EFX_EFV_PF); 661 if (rc < 0) { 662 NL_SET_ERR_MSG_MOD(extack, "Failed to identify ingress m-port for foreign filter"); 663 return rc; 664 } 665 match.value.ingress_port = rc; 666 match.mask.ingress_port = ~0; 667 668 if (tc->common.chain_index) { 669 NL_SET_ERR_MSG_MOD(extack, "No support for nonzero chain_index"); 670 return -EOPNOTSUPP; 671 } 672 match.mask.recirc_id = 0xff; 673 674 flow_action_for_each(i, fa, &fr->action) { 675 switch (fa->id) { 676 case FLOW_ACTION_REDIRECT: 677 case FLOW_ACTION_MIRRED: /* mirred means mirror here */ 678 to_efv = efx_tc_flower_lookup_efv(efx, fa->dev); 679 if (IS_ERR(to_efv)) 680 continue; 681 found = true; 682 break; 683 default: 684 break; 685 } 686 } 687 if (!found) { /* We don't care. */ 688 netif_dbg(efx, drv, efx->net_dev, 689 "Ignoring foreign filter that doesn't egdev us\n"); 690 return -EOPNOTSUPP; 691 } 692 693 rc = efx_mae_match_check_caps(efx, &match.mask, NULL); 694 if (rc) 695 return rc; 696 697 if (efx_tc_match_is_encap(&match.mask)) { 698 enum efx_encap_type type; 699 700 type = efx_tc_indr_netdev_type(net_dev); 701 if (type == EFX_ENCAP_TYPE_NONE) { 702 NL_SET_ERR_MSG_MOD(extack, 703 "Egress encap match on unsupported tunnel device"); 704 return -EOPNOTSUPP; 705 } 706 707 rc = efx_mae_check_encap_type_supported(efx, type); 708 if (rc) { 709 NL_SET_ERR_MSG_FMT_MOD(extack, 710 "Firmware reports no support for %s encap match", 711 efx_tc_encap_type_name(type)); 712 return rc; 713 } 714 715 rc = efx_tc_flower_record_encap_match(efx, &match, type, 716 EFX_TC_EM_DIRECT, 0, 0, 717 extack); 718 if (rc) 719 return rc; 720 } else { 721 /* This is not a tunnel decap rule, ignore it */ 722 netif_dbg(efx, drv, efx->net_dev, 723 "Ignoring foreign filter without encap match\n"); 724 return -EOPNOTSUPP; 725 } 726 727 rule = kzalloc(sizeof(*rule), GFP_USER); 728 if (!rule) { 729 rc = -ENOMEM; 730 goto out_free; 731 } 732 INIT_LIST_HEAD(&rule->acts.list); 733 rule->cookie = tc->cookie; 734 old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht, 735 &rule->linkage, 736 efx_tc_match_action_ht_params); 737 if (old) { 738 netif_dbg(efx, drv, efx->net_dev, 739 "Ignoring already-offloaded rule (cookie %lx)\n", 740 tc->cookie); 741 rc = -EEXIST; 742 goto out_free; 743 } 744 745 act = kzalloc(sizeof(*act), GFP_USER); 746 if (!act) { 747 rc = -ENOMEM; 748 goto release; 749 } 750 751 /* Parse actions. For foreign rules we only support decap & redirect. 752 * See corresponding code in efx_tc_flower_replace() for theory of 753 * operation & how 'act' cursor is used. 754 */ 755 flow_action_for_each(i, fa, &fr->action) { 756 struct efx_tc_action_set save; 757 758 switch (fa->id) { 759 case FLOW_ACTION_REDIRECT: 760 case FLOW_ACTION_MIRRED: 761 /* See corresponding code in efx_tc_flower_replace() for 762 * long explanations of what's going on here. 763 */ 764 save = *act; 765 if (fa->hw_stats) { 766 struct efx_tc_counter_index *ctr; 767 768 if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) { 769 NL_SET_ERR_MSG_FMT_MOD(extack, 770 "hw_stats_type %u not supported (only 'delayed')", 771 fa->hw_stats); 772 rc = -EOPNOTSUPP; 773 goto release; 774 } 775 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) { 776 rc = -EOPNOTSUPP; 777 goto release; 778 } 779 780 ctr = efx_tc_flower_get_counter_index(efx, 781 tc->cookie, 782 EFX_TC_COUNTER_TYPE_AR); 783 if (IS_ERR(ctr)) { 784 rc = PTR_ERR(ctr); 785 NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter"); 786 goto release; 787 } 788 act->count = ctr; 789 } 790 791 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) { 792 /* can't happen */ 793 rc = -EOPNOTSUPP; 794 NL_SET_ERR_MSG_MOD(extack, 795 "Deliver action violates action order (can't happen)"); 796 goto release; 797 } 798 to_efv = efx_tc_flower_lookup_efv(efx, fa->dev); 799 /* PF implies egdev is us, in which case we really 800 * want to deliver to the uplink (because this is an 801 * ingress filter). If we don't recognise the egdev 802 * at all, then we'd better trap so SW can handle it. 803 */ 804 if (IS_ERR(to_efv)) 805 to_efv = EFX_EFV_PF; 806 if (to_efv == EFX_EFV_PF) { 807 if (uplinked) 808 break; 809 uplinked = true; 810 } 811 rc = efx_tc_flower_internal_mport(efx, to_efv); 812 if (rc < 0) { 813 NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port"); 814 goto release; 815 } 816 act->dest_mport = rc; 817 act->deliver = 1; 818 rc = efx_mae_alloc_action_set(efx, act); 819 if (rc) { 820 NL_SET_ERR_MSG_MOD(extack, 821 "Failed to write action set to hw (mirred)"); 822 goto release; 823 } 824 list_add_tail(&act->list, &rule->acts.list); 825 act = NULL; 826 if (fa->id == FLOW_ACTION_REDIRECT) 827 break; /* end of the line */ 828 /* Mirror, so continue on with saved act */ 829 act = kzalloc(sizeof(*act), GFP_USER); 830 if (!act) { 831 rc = -ENOMEM; 832 goto release; 833 } 834 *act = save; 835 break; 836 case FLOW_ACTION_TUNNEL_DECAP: 837 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DECAP)) { 838 rc = -EINVAL; 839 NL_SET_ERR_MSG_MOD(extack, "Decap action violates action order"); 840 goto release; 841 } 842 act->decap = 1; 843 /* If we previously delivered/trapped to uplink, now 844 * that we've decapped we'll want another copy if we 845 * try to deliver/trap to uplink again. 846 */ 847 uplinked = false; 848 break; 849 default: 850 NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u", 851 fa->id); 852 rc = -EOPNOTSUPP; 853 goto release; 854 } 855 } 856 857 if (act) { 858 if (!uplinked) { 859 /* Not shot/redirected, so deliver to default dest (which is 860 * the uplink, as this is an ingress filter) 861 */ 862 efx_mae_mport_uplink(efx, &act->dest_mport); 863 act->deliver = 1; 864 } 865 rc = efx_mae_alloc_action_set(efx, act); 866 if (rc) { 867 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)"); 868 goto release; 869 } 870 list_add_tail(&act->list, &rule->acts.list); 871 act = NULL; /* Prevent double-free in error path */ 872 } 873 874 rule->match = match; 875 876 netif_dbg(efx, drv, efx->net_dev, 877 "Successfully parsed foreign filter (cookie %lx)\n", 878 tc->cookie); 879 880 rc = efx_mae_alloc_action_set_list(efx, &rule->acts); 881 if (rc) { 882 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw"); 883 goto release; 884 } 885 rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC, 886 rule->acts.fw_id, &rule->fw_id); 887 if (rc) { 888 NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw"); 889 goto release_acts; 890 } 891 return 0; 892 893 release_acts: 894 efx_mae_free_action_set_list(efx, &rule->acts); 895 release: 896 /* We failed to insert the rule, so free up any entries we created in 897 * subsidiary tables. 898 */ 899 if (act) 900 efx_tc_free_action_set(efx, act, false); 901 if (rule) { 902 rhashtable_remove_fast(&efx->tc->match_action_ht, 903 &rule->linkage, 904 efx_tc_match_action_ht_params); 905 efx_tc_free_action_set_list(efx, &rule->acts, false); 906 } 907 out_free: 908 kfree(rule); 909 if (match.encap) 910 efx_tc_flower_release_encap_match(efx, match.encap); 911 return rc; 912 } 913 914 static int efx_tc_flower_replace(struct efx_nic *efx, 915 struct net_device *net_dev, 916 struct flow_cls_offload *tc, 917 struct efx_rep *efv) 918 { 919 struct flow_rule *fr = flow_cls_offload_flow_rule(tc); 920 struct netlink_ext_ack *extack = tc->common.extack; 921 struct efx_tc_flow_rule *rule = NULL, *old; 922 struct efx_tc_action_set *act = NULL; 923 const struct flow_action_entry *fa; 924 struct efx_rep *from_efv, *to_efv; 925 struct efx_tc_match match; 926 s64 rc; 927 int i; 928 929 if (!tc_can_offload_extack(efx->net_dev, extack)) 930 return -EOPNOTSUPP; 931 if (WARN_ON(!efx->tc)) 932 return -ENETDOWN; 933 if (WARN_ON(!efx->tc->up)) 934 return -ENETDOWN; 935 936 from_efv = efx_tc_flower_lookup_efv(efx, net_dev); 937 if (IS_ERR(from_efv)) { 938 /* Not from our PF or representors, so probably a tunnel dev */ 939 return efx_tc_flower_replace_foreign(efx, net_dev, tc); 940 } 941 942 if (efv != from_efv) { 943 /* can't happen */ 944 NL_SET_ERR_MSG_FMT_MOD(extack, "for %s efv is %snull but from_efv is %snull (can't happen)", 945 netdev_name(net_dev), efv ? "non-" : "", 946 from_efv ? "non-" : ""); 947 return -EINVAL; 948 } 949 950 /* Parse match */ 951 memset(&match, 0, sizeof(match)); 952 rc = efx_tc_flower_external_mport(efx, from_efv); 953 if (rc < 0) { 954 NL_SET_ERR_MSG_MOD(extack, "Failed to identify ingress m-port"); 955 return rc; 956 } 957 match.value.ingress_port = rc; 958 match.mask.ingress_port = ~0; 959 rc = efx_tc_flower_parse_match(efx, fr, &match, extack); 960 if (rc) 961 return rc; 962 if (efx_tc_match_is_encap(&match.mask)) { 963 NL_SET_ERR_MSG_MOD(extack, "Ingress enc_key matches not supported"); 964 return -EOPNOTSUPP; 965 } 966 967 if (tc->common.chain_index) { 968 NL_SET_ERR_MSG_MOD(extack, "No support for nonzero chain_index"); 969 return -EOPNOTSUPP; 970 } 971 match.mask.recirc_id = 0xff; 972 973 rc = efx_mae_match_check_caps(efx, &match.mask, extack); 974 if (rc) 975 return rc; 976 977 rule = kzalloc(sizeof(*rule), GFP_USER); 978 if (!rule) 979 return -ENOMEM; 980 INIT_LIST_HEAD(&rule->acts.list); 981 rule->cookie = tc->cookie; 982 old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht, 983 &rule->linkage, 984 efx_tc_match_action_ht_params); 985 if (old) { 986 netif_dbg(efx, drv, efx->net_dev, 987 "Already offloaded rule (cookie %lx)\n", tc->cookie); 988 NL_SET_ERR_MSG_MOD(extack, "Rule already offloaded"); 989 kfree(rule); 990 return -EEXIST; 991 } 992 993 /* Parse actions */ 994 act = kzalloc(sizeof(*act), GFP_USER); 995 if (!act) { 996 rc = -ENOMEM; 997 goto release; 998 } 999 1000 /** 1001 * DOC: TC action translation 1002 * 1003 * Actions in TC are sequential and cumulative, with delivery actions 1004 * potentially anywhere in the order. The EF100 MAE, however, takes 1005 * an 'action set list' consisting of 'action sets', each of which is 1006 * applied to the _original_ packet, and consists of a set of optional 1007 * actions in a fixed order with delivery at the end. 1008 * To translate between these two models, we maintain a 'cursor', @act, 1009 * which describes the cumulative effect of all the packet-mutating 1010 * actions encountered so far; on handling a delivery (mirred or drop) 1011 * action, once the action-set has been inserted into hardware, we 1012 * append @act to the action-set list (@rule->acts); if this is a pipe 1013 * action (mirred mirror) we then allocate a new @act with a copy of 1014 * the cursor state _before_ the delivery action, otherwise we set @act 1015 * to %NULL. 1016 * This ensures that every allocated action-set is either attached to 1017 * @rule->acts or pointed to by @act (and never both), and that only 1018 * those action-sets in @rule->acts exist in hardware. Consequently, 1019 * in the failure path, @act only needs to be freed in memory, whereas 1020 * for @rule->acts we remove each action-set from hardware before 1021 * freeing it (efx_tc_free_action_set_list()), even if the action-set 1022 * list itself is not in hardware. 1023 */ 1024 flow_action_for_each(i, fa, &fr->action) { 1025 struct efx_tc_action_set save; 1026 u16 tci; 1027 1028 if (!act) { 1029 /* more actions after a non-pipe action */ 1030 NL_SET_ERR_MSG_MOD(extack, "Action follows non-pipe action"); 1031 rc = -EINVAL; 1032 goto release; 1033 } 1034 1035 if ((fa->id == FLOW_ACTION_REDIRECT || 1036 fa->id == FLOW_ACTION_MIRRED || 1037 fa->id == FLOW_ACTION_DROP) && fa->hw_stats) { 1038 struct efx_tc_counter_index *ctr; 1039 1040 /* Currently the only actions that want stats are 1041 * mirred and gact (ok, shot, trap, goto-chain), which 1042 * means we want stats just before delivery. Also, 1043 * note that tunnel_key set shouldn't change the length 1044 * — it's only the subsequent mirred that does that, 1045 * and the stats are taken _before_ the mirred action 1046 * happens. 1047 */ 1048 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) { 1049 /* All supported actions that count either steal 1050 * (gact shot, mirred redirect) or clone act 1051 * (mirred mirror), so we should never get two 1052 * count actions on one action_set. 1053 */ 1054 NL_SET_ERR_MSG_MOD(extack, "Count-action conflict (can't happen)"); 1055 rc = -EOPNOTSUPP; 1056 goto release; 1057 } 1058 1059 if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) { 1060 NL_SET_ERR_MSG_FMT_MOD(extack, "hw_stats_type %u not supported (only 'delayed')", 1061 fa->hw_stats); 1062 rc = -EOPNOTSUPP; 1063 goto release; 1064 } 1065 1066 ctr = efx_tc_flower_get_counter_index(efx, tc->cookie, 1067 EFX_TC_COUNTER_TYPE_AR); 1068 if (IS_ERR(ctr)) { 1069 rc = PTR_ERR(ctr); 1070 NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter"); 1071 goto release; 1072 } 1073 act->count = ctr; 1074 } 1075 1076 switch (fa->id) { 1077 case FLOW_ACTION_DROP: 1078 rc = efx_mae_alloc_action_set(efx, act); 1079 if (rc) { 1080 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (drop)"); 1081 goto release; 1082 } 1083 list_add_tail(&act->list, &rule->acts.list); 1084 act = NULL; /* end of the line */ 1085 break; 1086 case FLOW_ACTION_REDIRECT: 1087 case FLOW_ACTION_MIRRED: 1088 save = *act; 1089 1090 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) { 1091 /* can't happen */ 1092 rc = -EOPNOTSUPP; 1093 NL_SET_ERR_MSG_MOD(extack, "Deliver action violates action order (can't happen)"); 1094 goto release; 1095 } 1096 1097 to_efv = efx_tc_flower_lookup_efv(efx, fa->dev); 1098 if (IS_ERR(to_efv)) { 1099 NL_SET_ERR_MSG_MOD(extack, "Mirred egress device not on switch"); 1100 rc = PTR_ERR(to_efv); 1101 goto release; 1102 } 1103 rc = efx_tc_flower_external_mport(efx, to_efv); 1104 if (rc < 0) { 1105 NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port"); 1106 goto release; 1107 } 1108 act->dest_mport = rc; 1109 act->deliver = 1; 1110 rc = efx_mae_alloc_action_set(efx, act); 1111 if (rc) { 1112 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (mirred)"); 1113 goto release; 1114 } 1115 list_add_tail(&act->list, &rule->acts.list); 1116 act = NULL; 1117 if (fa->id == FLOW_ACTION_REDIRECT) 1118 break; /* end of the line */ 1119 /* Mirror, so continue on with saved act */ 1120 save.count = NULL; 1121 act = kzalloc(sizeof(*act), GFP_USER); 1122 if (!act) { 1123 rc = -ENOMEM; 1124 goto release; 1125 } 1126 *act = save; 1127 break; 1128 case FLOW_ACTION_VLAN_POP: 1129 if (act->vlan_push) { 1130 act->vlan_push--; 1131 } else if (efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_POP)) { 1132 act->vlan_pop++; 1133 } else { 1134 NL_SET_ERR_MSG_MOD(extack, 1135 "More than two VLAN pops, or action order violated"); 1136 rc = -EINVAL; 1137 goto release; 1138 } 1139 break; 1140 case FLOW_ACTION_VLAN_PUSH: 1141 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_PUSH)) { 1142 rc = -EINVAL; 1143 NL_SET_ERR_MSG_MOD(extack, 1144 "More than two VLAN pushes, or action order violated"); 1145 goto release; 1146 } 1147 tci = fa->vlan.vid & VLAN_VID_MASK; 1148 tci |= fa->vlan.prio << VLAN_PRIO_SHIFT; 1149 act->vlan_tci[act->vlan_push] = cpu_to_be16(tci); 1150 act->vlan_proto[act->vlan_push] = fa->vlan.proto; 1151 act->vlan_push++; 1152 break; 1153 default: 1154 NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u", 1155 fa->id); 1156 rc = -EOPNOTSUPP; 1157 goto release; 1158 } 1159 } 1160 1161 if (act) { 1162 /* Not shot/redirected, so deliver to default dest */ 1163 if (from_efv == EFX_EFV_PF) 1164 /* Rule applies to traffic from the wire, 1165 * and default dest is thus the PF 1166 */ 1167 efx_mae_mport_uplink(efx, &act->dest_mport); 1168 else 1169 /* Representor, so rule applies to traffic from 1170 * representee, and default dest is thus the rep. 1171 * All reps use the same mport for delivery 1172 */ 1173 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, 1174 &act->dest_mport); 1175 act->deliver = 1; 1176 rc = efx_mae_alloc_action_set(efx, act); 1177 if (rc) { 1178 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)"); 1179 goto release; 1180 } 1181 list_add_tail(&act->list, &rule->acts.list); 1182 act = NULL; /* Prevent double-free in error path */ 1183 } 1184 1185 netif_dbg(efx, drv, efx->net_dev, 1186 "Successfully parsed filter (cookie %lx)\n", 1187 tc->cookie); 1188 1189 rule->match = match; 1190 1191 rc = efx_mae_alloc_action_set_list(efx, &rule->acts); 1192 if (rc) { 1193 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw"); 1194 goto release; 1195 } 1196 rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC, 1197 rule->acts.fw_id, &rule->fw_id); 1198 if (rc) { 1199 NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw"); 1200 goto release_acts; 1201 } 1202 return 0; 1203 1204 release_acts: 1205 efx_mae_free_action_set_list(efx, &rule->acts); 1206 release: 1207 /* We failed to insert the rule, so free up any entries we created in 1208 * subsidiary tables. 1209 */ 1210 if (act) 1211 efx_tc_free_action_set(efx, act, false); 1212 if (rule) { 1213 rhashtable_remove_fast(&efx->tc->match_action_ht, 1214 &rule->linkage, 1215 efx_tc_match_action_ht_params); 1216 efx_tc_free_action_set_list(efx, &rule->acts, false); 1217 } 1218 kfree(rule); 1219 return rc; 1220 } 1221 1222 static int efx_tc_flower_destroy(struct efx_nic *efx, 1223 struct net_device *net_dev, 1224 struct flow_cls_offload *tc) 1225 { 1226 struct netlink_ext_ack *extack = tc->common.extack; 1227 struct efx_tc_flow_rule *rule; 1228 1229 rule = rhashtable_lookup_fast(&efx->tc->match_action_ht, &tc->cookie, 1230 efx_tc_match_action_ht_params); 1231 if (!rule) { 1232 /* Only log a message if we're the ingress device. Otherwise 1233 * it's a foreign filter and we might just not have been 1234 * interested (e.g. we might not have been the egress device 1235 * either). 1236 */ 1237 if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev))) 1238 netif_warn(efx, drv, efx->net_dev, 1239 "Filter %lx not found to remove\n", tc->cookie); 1240 NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules"); 1241 return -ENOENT; 1242 } 1243 1244 /* Remove it from HW */ 1245 efx_tc_delete_rule(efx, rule); 1246 /* Delete it from SW */ 1247 rhashtable_remove_fast(&efx->tc->match_action_ht, &rule->linkage, 1248 efx_tc_match_action_ht_params); 1249 netif_dbg(efx, drv, efx->net_dev, "Removed filter %lx\n", rule->cookie); 1250 kfree(rule); 1251 return 0; 1252 } 1253 1254 static int efx_tc_flower_stats(struct efx_nic *efx, struct net_device *net_dev, 1255 struct flow_cls_offload *tc) 1256 { 1257 struct netlink_ext_ack *extack = tc->common.extack; 1258 struct efx_tc_counter_index *ctr; 1259 struct efx_tc_counter *cnt; 1260 u64 packets, bytes; 1261 1262 ctr = efx_tc_flower_find_counter_index(efx, tc->cookie); 1263 if (!ctr) { 1264 /* See comment in efx_tc_flower_destroy() */ 1265 if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev))) 1266 if (net_ratelimit()) 1267 netif_warn(efx, drv, efx->net_dev, 1268 "Filter %lx not found for stats\n", 1269 tc->cookie); 1270 NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules"); 1271 return -ENOENT; 1272 } 1273 if (WARN_ON(!ctr->cnt)) /* can't happen */ 1274 return -EIO; 1275 cnt = ctr->cnt; 1276 1277 spin_lock_bh(&cnt->lock); 1278 /* Report only new pkts/bytes since last time TC asked */ 1279 packets = cnt->packets; 1280 bytes = cnt->bytes; 1281 flow_stats_update(&tc->stats, bytes - cnt->old_bytes, 1282 packets - cnt->old_packets, 0, cnt->touched, 1283 FLOW_ACTION_HW_STATS_DELAYED); 1284 cnt->old_packets = packets; 1285 cnt->old_bytes = bytes; 1286 spin_unlock_bh(&cnt->lock); 1287 return 0; 1288 } 1289 1290 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev, 1291 struct flow_cls_offload *tc, struct efx_rep *efv) 1292 { 1293 int rc; 1294 1295 if (!efx->tc) 1296 return -EOPNOTSUPP; 1297 1298 mutex_lock(&efx->tc->mutex); 1299 switch (tc->command) { 1300 case FLOW_CLS_REPLACE: 1301 rc = efx_tc_flower_replace(efx, net_dev, tc, efv); 1302 break; 1303 case FLOW_CLS_DESTROY: 1304 rc = efx_tc_flower_destroy(efx, net_dev, tc); 1305 break; 1306 case FLOW_CLS_STATS: 1307 rc = efx_tc_flower_stats(efx, net_dev, tc); 1308 break; 1309 default: 1310 rc = -EOPNOTSUPP; 1311 break; 1312 } 1313 mutex_unlock(&efx->tc->mutex); 1314 return rc; 1315 } 1316 1317 static int efx_tc_configure_default_rule(struct efx_nic *efx, u32 ing_port, 1318 u32 eg_port, struct efx_tc_flow_rule *rule) 1319 { 1320 struct efx_tc_action_set_list *acts = &rule->acts; 1321 struct efx_tc_match *match = &rule->match; 1322 struct efx_tc_action_set *act; 1323 int rc; 1324 1325 match->value.ingress_port = ing_port; 1326 match->mask.ingress_port = ~0; 1327 act = kzalloc(sizeof(*act), GFP_KERNEL); 1328 if (!act) 1329 return -ENOMEM; 1330 act->deliver = 1; 1331 act->dest_mport = eg_port; 1332 rc = efx_mae_alloc_action_set(efx, act); 1333 if (rc) 1334 goto fail1; 1335 EFX_WARN_ON_PARANOID(!list_empty(&acts->list)); 1336 list_add_tail(&act->list, &acts->list); 1337 rc = efx_mae_alloc_action_set_list(efx, acts); 1338 if (rc) 1339 goto fail2; 1340 rc = efx_mae_insert_rule(efx, match, EFX_TC_PRIO_DFLT, 1341 acts->fw_id, &rule->fw_id); 1342 if (rc) 1343 goto fail3; 1344 return 0; 1345 fail3: 1346 efx_mae_free_action_set_list(efx, acts); 1347 fail2: 1348 list_del(&act->list); 1349 efx_mae_free_action_set(efx, act->fw_id); 1350 fail1: 1351 kfree(act); 1352 return rc; 1353 } 1354 1355 static int efx_tc_configure_default_rule_pf(struct efx_nic *efx) 1356 { 1357 struct efx_tc_flow_rule *rule = &efx->tc->dflt.pf; 1358 u32 ing_port, eg_port; 1359 1360 efx_mae_mport_uplink(efx, &ing_port); 1361 efx_mae_mport_wire(efx, &eg_port); 1362 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1363 } 1364 1365 static int efx_tc_configure_default_rule_wire(struct efx_nic *efx) 1366 { 1367 struct efx_tc_flow_rule *rule = &efx->tc->dflt.wire; 1368 u32 ing_port, eg_port; 1369 1370 efx_mae_mport_wire(efx, &ing_port); 1371 efx_mae_mport_uplink(efx, &eg_port); 1372 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1373 } 1374 1375 int efx_tc_configure_default_rule_rep(struct efx_rep *efv) 1376 { 1377 struct efx_tc_flow_rule *rule = &efv->dflt; 1378 struct efx_nic *efx = efv->parent; 1379 u32 ing_port, eg_port; 1380 1381 efx_mae_mport_mport(efx, efv->mport, &ing_port); 1382 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, &eg_port); 1383 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1384 } 1385 1386 void efx_tc_deconfigure_default_rule(struct efx_nic *efx, 1387 struct efx_tc_flow_rule *rule) 1388 { 1389 if (rule->fw_id != MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL) 1390 efx_tc_delete_rule(efx, rule); 1391 rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1392 } 1393 1394 static int efx_tc_configure_rep_mport(struct efx_nic *efx) 1395 { 1396 u32 rep_mport_label; 1397 int rc; 1398 1399 rc = efx_mae_allocate_mport(efx, &efx->tc->reps_mport_id, &rep_mport_label); 1400 if (rc) 1401 return rc; 1402 pci_dbg(efx->pci_dev, "created rep mport 0x%08x (0x%04x)\n", 1403 efx->tc->reps_mport_id, rep_mport_label); 1404 /* Use mport *selector* as vport ID */ 1405 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, 1406 &efx->tc->reps_mport_vport_id); 1407 return 0; 1408 } 1409 1410 static void efx_tc_deconfigure_rep_mport(struct efx_nic *efx) 1411 { 1412 efx_mae_free_mport(efx, efx->tc->reps_mport_id); 1413 efx->tc->reps_mport_id = MAE_MPORT_SELECTOR_NULL; 1414 } 1415 1416 int efx_tc_insert_rep_filters(struct efx_nic *efx) 1417 { 1418 struct efx_filter_spec promisc, allmulti; 1419 int rc; 1420 1421 if (efx->type->is_vf) 1422 return 0; 1423 if (!efx->tc) 1424 return 0; 1425 efx_filter_init_rx(&promisc, EFX_FILTER_PRI_REQUIRED, 0, 0); 1426 efx_filter_set_uc_def(&promisc); 1427 efx_filter_set_vport_id(&promisc, efx->tc->reps_mport_vport_id); 1428 rc = efx_filter_insert_filter(efx, &promisc, false); 1429 if (rc < 0) 1430 return rc; 1431 efx->tc->reps_filter_uc = rc; 1432 efx_filter_init_rx(&allmulti, EFX_FILTER_PRI_REQUIRED, 0, 0); 1433 efx_filter_set_mc_def(&allmulti); 1434 efx_filter_set_vport_id(&allmulti, efx->tc->reps_mport_vport_id); 1435 rc = efx_filter_insert_filter(efx, &allmulti, false); 1436 if (rc < 0) 1437 return rc; 1438 efx->tc->reps_filter_mc = rc; 1439 return 0; 1440 } 1441 1442 void efx_tc_remove_rep_filters(struct efx_nic *efx) 1443 { 1444 if (efx->type->is_vf) 1445 return; 1446 if (!efx->tc) 1447 return; 1448 if (efx->tc->reps_filter_mc >= 0) 1449 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_mc); 1450 efx->tc->reps_filter_mc = -1; 1451 if (efx->tc->reps_filter_uc >= 0) 1452 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_uc); 1453 efx->tc->reps_filter_uc = -1; 1454 } 1455 1456 int efx_init_tc(struct efx_nic *efx) 1457 { 1458 int rc; 1459 1460 rc = efx_mae_get_caps(efx, efx->tc->caps); 1461 if (rc) 1462 return rc; 1463 if (efx->tc->caps->match_field_count > MAE_NUM_FIELDS) 1464 /* Firmware supports some match fields the driver doesn't know 1465 * about. Not fatal, unless any of those fields are required 1466 * (MAE_FIELD_SUPPORTED_MATCH_ALWAYS) but if so we don't know. 1467 */ 1468 netif_warn(efx, probe, efx->net_dev, 1469 "FW reports additional match fields %u\n", 1470 efx->tc->caps->match_field_count); 1471 if (efx->tc->caps->action_prios < EFX_TC_PRIO__NUM) { 1472 netif_err(efx, probe, efx->net_dev, 1473 "Too few action prios supported (have %u, need %u)\n", 1474 efx->tc->caps->action_prios, EFX_TC_PRIO__NUM); 1475 return -EIO; 1476 } 1477 rc = efx_tc_configure_default_rule_pf(efx); 1478 if (rc) 1479 return rc; 1480 rc = efx_tc_configure_default_rule_wire(efx); 1481 if (rc) 1482 return rc; 1483 rc = efx_tc_configure_rep_mport(efx); 1484 if (rc) 1485 return rc; 1486 efx->tc->up = true; 1487 rc = flow_indr_dev_register(efx_tc_indr_setup_cb, efx); 1488 if (rc) 1489 return rc; 1490 return 0; 1491 } 1492 1493 void efx_fini_tc(struct efx_nic *efx) 1494 { 1495 /* We can get called even if efx_init_struct_tc() failed */ 1496 if (!efx->tc) 1497 return; 1498 if (efx->tc->up) 1499 flow_indr_dev_unregister(efx_tc_indr_setup_cb, efx, efx_tc_block_unbind); 1500 efx_tc_deconfigure_rep_mport(efx); 1501 efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.pf); 1502 efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.wire); 1503 efx->tc->up = false; 1504 } 1505 1506 /* At teardown time, all TC filter rules (and thus all resources they created) 1507 * should already have been removed. If we find any in our hashtables, make a 1508 * cursory attempt to clean up the software side. 1509 */ 1510 static void efx_tc_encap_match_free(void *ptr, void *__unused) 1511 { 1512 struct efx_tc_encap_match *encap = ptr; 1513 1514 WARN_ON(refcount_read(&encap->ref)); 1515 kfree(encap); 1516 } 1517 1518 static void efx_tc_flow_free(void *ptr, void *arg) 1519 { 1520 struct efx_tc_flow_rule *rule = ptr; 1521 struct efx_nic *efx = arg; 1522 1523 netif_err(efx, drv, efx->net_dev, 1524 "tc rule %lx still present at teardown, removing\n", 1525 rule->cookie); 1526 1527 /* Also releases entries in subsidiary tables */ 1528 efx_tc_delete_rule(efx, rule); 1529 1530 kfree(rule); 1531 } 1532 1533 int efx_init_struct_tc(struct efx_nic *efx) 1534 { 1535 int rc; 1536 1537 if (efx->type->is_vf) 1538 return 0; 1539 1540 efx->tc = kzalloc(sizeof(*efx->tc), GFP_KERNEL); 1541 if (!efx->tc) 1542 return -ENOMEM; 1543 efx->tc->caps = kzalloc(sizeof(struct mae_caps), GFP_KERNEL); 1544 if (!efx->tc->caps) { 1545 rc = -ENOMEM; 1546 goto fail_alloc_caps; 1547 } 1548 INIT_LIST_HEAD(&efx->tc->block_list); 1549 1550 mutex_init(&efx->tc->mutex); 1551 init_waitqueue_head(&efx->tc->flush_wq); 1552 rc = efx_tc_init_counters(efx); 1553 if (rc < 0) 1554 goto fail_counters; 1555 rc = rhashtable_init(&efx->tc->encap_match_ht, &efx_tc_encap_match_ht_params); 1556 if (rc < 0) 1557 goto fail_encap_match_ht; 1558 rc = rhashtable_init(&efx->tc->match_action_ht, &efx_tc_match_action_ht_params); 1559 if (rc < 0) 1560 goto fail_match_action_ht; 1561 efx->tc->reps_filter_uc = -1; 1562 efx->tc->reps_filter_mc = -1; 1563 INIT_LIST_HEAD(&efx->tc->dflt.pf.acts.list); 1564 efx->tc->dflt.pf.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1565 INIT_LIST_HEAD(&efx->tc->dflt.wire.acts.list); 1566 efx->tc->dflt.wire.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1567 efx->extra_channel_type[EFX_EXTRA_CHANNEL_TC] = &efx_tc_channel_type; 1568 return 0; 1569 fail_match_action_ht: 1570 rhashtable_destroy(&efx->tc->encap_match_ht); 1571 fail_encap_match_ht: 1572 efx_tc_destroy_counters(efx); 1573 fail_counters: 1574 mutex_destroy(&efx->tc->mutex); 1575 kfree(efx->tc->caps); 1576 fail_alloc_caps: 1577 kfree(efx->tc); 1578 efx->tc = NULL; 1579 return rc; 1580 } 1581 1582 void efx_fini_struct_tc(struct efx_nic *efx) 1583 { 1584 if (!efx->tc) 1585 return; 1586 1587 mutex_lock(&efx->tc->mutex); 1588 EFX_WARN_ON_PARANOID(efx->tc->dflt.pf.fw_id != 1589 MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL); 1590 EFX_WARN_ON_PARANOID(efx->tc->dflt.wire.fw_id != 1591 MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL); 1592 rhashtable_free_and_destroy(&efx->tc->match_action_ht, efx_tc_flow_free, 1593 efx); 1594 rhashtable_free_and_destroy(&efx->tc->encap_match_ht, 1595 efx_tc_encap_match_free, NULL); 1596 efx_tc_fini_counters(efx); 1597 mutex_unlock(&efx->tc->mutex); 1598 mutex_destroy(&efx->tc->mutex); 1599 kfree(efx->tc->caps); 1600 kfree(efx->tc); 1601 efx->tc = NULL; 1602 } 1603