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 rc = -EOPNOTSUPP; 691 goto release; 692 } 693 694 rc = efx_mae_match_check_caps(efx, &match.mask, NULL); 695 if (rc) 696 goto release; 697 698 if (efx_tc_match_is_encap(&match.mask)) { 699 enum efx_encap_type type; 700 701 type = efx_tc_indr_netdev_type(net_dev); 702 if (type == EFX_ENCAP_TYPE_NONE) { 703 NL_SET_ERR_MSG_MOD(extack, 704 "Egress encap match on unsupported tunnel device"); 705 rc = -EOPNOTSUPP; 706 goto release; 707 } 708 709 rc = efx_mae_check_encap_type_supported(efx, type); 710 if (rc) { 711 NL_SET_ERR_MSG_FMT_MOD(extack, 712 "Firmware reports no support for %s encap match", 713 efx_tc_encap_type_name(type)); 714 goto release; 715 } 716 717 rc = efx_tc_flower_record_encap_match(efx, &match, type, 718 EFX_TC_EM_DIRECT, 0, 0, 719 extack); 720 if (rc) 721 goto release; 722 } else { 723 /* This is not a tunnel decap rule, ignore it */ 724 netif_dbg(efx, drv, efx->net_dev, 725 "Ignoring foreign filter without encap match\n"); 726 rc = -EOPNOTSUPP; 727 goto release; 728 } 729 730 rule = kzalloc(sizeof(*rule), GFP_USER); 731 if (!rule) { 732 rc = -ENOMEM; 733 goto release; 734 } 735 INIT_LIST_HEAD(&rule->acts.list); 736 rule->cookie = tc->cookie; 737 old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht, 738 &rule->linkage, 739 efx_tc_match_action_ht_params); 740 if (old) { 741 netif_dbg(efx, drv, efx->net_dev, 742 "Ignoring already-offloaded rule (cookie %lx)\n", 743 tc->cookie); 744 rc = -EEXIST; 745 goto release; 746 } 747 748 act = kzalloc(sizeof(*act), GFP_USER); 749 if (!act) { 750 rc = -ENOMEM; 751 goto release; 752 } 753 754 /* Parse actions. For foreign rules we only support decap & redirect. 755 * See corresponding code in efx_tc_flower_replace() for theory of 756 * operation & how 'act' cursor is used. 757 */ 758 flow_action_for_each(i, fa, &fr->action) { 759 struct efx_tc_action_set save; 760 761 switch (fa->id) { 762 case FLOW_ACTION_REDIRECT: 763 case FLOW_ACTION_MIRRED: 764 /* See corresponding code in efx_tc_flower_replace() for 765 * long explanations of what's going on here. 766 */ 767 save = *act; 768 if (fa->hw_stats) { 769 struct efx_tc_counter_index *ctr; 770 771 if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) { 772 NL_SET_ERR_MSG_FMT_MOD(extack, 773 "hw_stats_type %u not supported (only 'delayed')", 774 fa->hw_stats); 775 rc = -EOPNOTSUPP; 776 goto release; 777 } 778 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) { 779 rc = -EOPNOTSUPP; 780 goto release; 781 } 782 783 ctr = efx_tc_flower_get_counter_index(efx, 784 tc->cookie, 785 EFX_TC_COUNTER_TYPE_AR); 786 if (IS_ERR(ctr)) { 787 rc = PTR_ERR(ctr); 788 NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter"); 789 goto release; 790 } 791 act->count = ctr; 792 } 793 794 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) { 795 /* can't happen */ 796 rc = -EOPNOTSUPP; 797 NL_SET_ERR_MSG_MOD(extack, 798 "Deliver action violates action order (can't happen)"); 799 goto release; 800 } 801 to_efv = efx_tc_flower_lookup_efv(efx, fa->dev); 802 /* PF implies egdev is us, in which case we really 803 * want to deliver to the uplink (because this is an 804 * ingress filter). If we don't recognise the egdev 805 * at all, then we'd better trap so SW can handle it. 806 */ 807 if (IS_ERR(to_efv)) 808 to_efv = EFX_EFV_PF; 809 if (to_efv == EFX_EFV_PF) { 810 if (uplinked) 811 break; 812 uplinked = true; 813 } 814 rc = efx_tc_flower_internal_mport(efx, to_efv); 815 if (rc < 0) { 816 NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port"); 817 goto release; 818 } 819 act->dest_mport = rc; 820 act->deliver = 1; 821 rc = efx_mae_alloc_action_set(efx, act); 822 if (rc) { 823 NL_SET_ERR_MSG_MOD(extack, 824 "Failed to write action set to hw (mirred)"); 825 goto release; 826 } 827 list_add_tail(&act->list, &rule->acts.list); 828 act = NULL; 829 if (fa->id == FLOW_ACTION_REDIRECT) 830 break; /* end of the line */ 831 /* Mirror, so continue on with saved act */ 832 act = kzalloc(sizeof(*act), GFP_USER); 833 if (!act) { 834 rc = -ENOMEM; 835 goto release; 836 } 837 *act = save; 838 break; 839 case FLOW_ACTION_TUNNEL_DECAP: 840 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DECAP)) { 841 rc = -EINVAL; 842 NL_SET_ERR_MSG_MOD(extack, "Decap action violates action order"); 843 goto release; 844 } 845 act->decap = 1; 846 /* If we previously delivered/trapped to uplink, now 847 * that we've decapped we'll want another copy if we 848 * try to deliver/trap to uplink again. 849 */ 850 uplinked = false; 851 break; 852 default: 853 NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u", 854 fa->id); 855 rc = -EOPNOTSUPP; 856 goto release; 857 } 858 } 859 860 if (act) { 861 if (!uplinked) { 862 /* Not shot/redirected, so deliver to default dest (which is 863 * the uplink, as this is an ingress filter) 864 */ 865 efx_mae_mport_uplink(efx, &act->dest_mport); 866 act->deliver = 1; 867 } 868 rc = efx_mae_alloc_action_set(efx, act); 869 if (rc) { 870 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)"); 871 goto release; 872 } 873 list_add_tail(&act->list, &rule->acts.list); 874 act = NULL; /* Prevent double-free in error path */ 875 } 876 877 rule->match = match; 878 879 netif_dbg(efx, drv, efx->net_dev, 880 "Successfully parsed foreign filter (cookie %lx)\n", 881 tc->cookie); 882 883 rc = efx_mae_alloc_action_set_list(efx, &rule->acts); 884 if (rc) { 885 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw"); 886 goto release; 887 } 888 rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC, 889 rule->acts.fw_id, &rule->fw_id); 890 if (rc) { 891 NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw"); 892 goto release_acts; 893 } 894 return 0; 895 896 release_acts: 897 efx_mae_free_action_set_list(efx, &rule->acts); 898 release: 899 /* We failed to insert the rule, so free up any entries we created in 900 * subsidiary tables. 901 */ 902 if (act) 903 efx_tc_free_action_set(efx, act, false); 904 if (rule) { 905 rhashtable_remove_fast(&efx->tc->match_action_ht, 906 &rule->linkage, 907 efx_tc_match_action_ht_params); 908 efx_tc_free_action_set_list(efx, &rule->acts, false); 909 } 910 kfree(rule); 911 if (match.encap) 912 efx_tc_flower_release_encap_match(efx, match.encap); 913 return rc; 914 } 915 916 static int efx_tc_flower_replace(struct efx_nic *efx, 917 struct net_device *net_dev, 918 struct flow_cls_offload *tc, 919 struct efx_rep *efv) 920 { 921 struct flow_rule *fr = flow_cls_offload_flow_rule(tc); 922 struct netlink_ext_ack *extack = tc->common.extack; 923 struct efx_tc_flow_rule *rule = NULL, *old; 924 struct efx_tc_action_set *act = NULL; 925 const struct flow_action_entry *fa; 926 struct efx_rep *from_efv, *to_efv; 927 struct efx_tc_match match; 928 s64 rc; 929 int i; 930 931 if (!tc_can_offload_extack(efx->net_dev, extack)) 932 return -EOPNOTSUPP; 933 if (WARN_ON(!efx->tc)) 934 return -ENETDOWN; 935 if (WARN_ON(!efx->tc->up)) 936 return -ENETDOWN; 937 938 from_efv = efx_tc_flower_lookup_efv(efx, net_dev); 939 if (IS_ERR(from_efv)) { 940 /* Not from our PF or representors, so probably a tunnel dev */ 941 return efx_tc_flower_replace_foreign(efx, net_dev, tc); 942 } 943 944 if (efv != from_efv) { 945 /* can't happen */ 946 NL_SET_ERR_MSG_FMT_MOD(extack, "for %s efv is %snull but from_efv is %snull (can't happen)", 947 netdev_name(net_dev), efv ? "non-" : "", 948 from_efv ? "non-" : ""); 949 return -EINVAL; 950 } 951 952 /* Parse match */ 953 memset(&match, 0, sizeof(match)); 954 rc = efx_tc_flower_external_mport(efx, from_efv); 955 if (rc < 0) { 956 NL_SET_ERR_MSG_MOD(extack, "Failed to identify ingress m-port"); 957 return rc; 958 } 959 match.value.ingress_port = rc; 960 match.mask.ingress_port = ~0; 961 rc = efx_tc_flower_parse_match(efx, fr, &match, extack); 962 if (rc) 963 return rc; 964 if (efx_tc_match_is_encap(&match.mask)) { 965 NL_SET_ERR_MSG_MOD(extack, "Ingress enc_key matches not supported"); 966 rc = -EOPNOTSUPP; 967 goto release; 968 } 969 970 if (tc->common.chain_index) { 971 NL_SET_ERR_MSG_MOD(extack, "No support for nonzero chain_index"); 972 return -EOPNOTSUPP; 973 } 974 match.mask.recirc_id = 0xff; 975 976 rc = efx_mae_match_check_caps(efx, &match.mask, extack); 977 if (rc) 978 return rc; 979 980 rule = kzalloc(sizeof(*rule), GFP_USER); 981 if (!rule) 982 return -ENOMEM; 983 INIT_LIST_HEAD(&rule->acts.list); 984 rule->cookie = tc->cookie; 985 old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht, 986 &rule->linkage, 987 efx_tc_match_action_ht_params); 988 if (old) { 989 netif_dbg(efx, drv, efx->net_dev, 990 "Already offloaded rule (cookie %lx)\n", tc->cookie); 991 rc = -EEXIST; 992 NL_SET_ERR_MSG_MOD(extack, "Rule already offloaded"); 993 goto release; 994 } 995 996 /* Parse actions */ 997 act = kzalloc(sizeof(*act), GFP_USER); 998 if (!act) { 999 rc = -ENOMEM; 1000 goto release; 1001 } 1002 1003 /** 1004 * DOC: TC action translation 1005 * 1006 * Actions in TC are sequential and cumulative, with delivery actions 1007 * potentially anywhere in the order. The EF100 MAE, however, takes 1008 * an 'action set list' consisting of 'action sets', each of which is 1009 * applied to the _original_ packet, and consists of a set of optional 1010 * actions in a fixed order with delivery at the end. 1011 * To translate between these two models, we maintain a 'cursor', @act, 1012 * which describes the cumulative effect of all the packet-mutating 1013 * actions encountered so far; on handling a delivery (mirred or drop) 1014 * action, once the action-set has been inserted into hardware, we 1015 * append @act to the action-set list (@rule->acts); if this is a pipe 1016 * action (mirred mirror) we then allocate a new @act with a copy of 1017 * the cursor state _before_ the delivery action, otherwise we set @act 1018 * to %NULL. 1019 * This ensures that every allocated action-set is either attached to 1020 * @rule->acts or pointed to by @act (and never both), and that only 1021 * those action-sets in @rule->acts exist in hardware. Consequently, 1022 * in the failure path, @act only needs to be freed in memory, whereas 1023 * for @rule->acts we remove each action-set from hardware before 1024 * freeing it (efx_tc_free_action_set_list()), even if the action-set 1025 * list itself is not in hardware. 1026 */ 1027 flow_action_for_each(i, fa, &fr->action) { 1028 struct efx_tc_action_set save; 1029 u16 tci; 1030 1031 if (!act) { 1032 /* more actions after a non-pipe action */ 1033 NL_SET_ERR_MSG_MOD(extack, "Action follows non-pipe action"); 1034 rc = -EINVAL; 1035 goto release; 1036 } 1037 1038 if ((fa->id == FLOW_ACTION_REDIRECT || 1039 fa->id == FLOW_ACTION_MIRRED || 1040 fa->id == FLOW_ACTION_DROP) && fa->hw_stats) { 1041 struct efx_tc_counter_index *ctr; 1042 1043 /* Currently the only actions that want stats are 1044 * mirred and gact (ok, shot, trap, goto-chain), which 1045 * means we want stats just before delivery. Also, 1046 * note that tunnel_key set shouldn't change the length 1047 * — it's only the subsequent mirred that does that, 1048 * and the stats are taken _before_ the mirred action 1049 * happens. 1050 */ 1051 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_COUNT)) { 1052 /* All supported actions that count either steal 1053 * (gact shot, mirred redirect) or clone act 1054 * (mirred mirror), so we should never get two 1055 * count actions on one action_set. 1056 */ 1057 NL_SET_ERR_MSG_MOD(extack, "Count-action conflict (can't happen)"); 1058 rc = -EOPNOTSUPP; 1059 goto release; 1060 } 1061 1062 if (!(fa->hw_stats & FLOW_ACTION_HW_STATS_DELAYED)) { 1063 NL_SET_ERR_MSG_FMT_MOD(extack, "hw_stats_type %u not supported (only 'delayed')", 1064 fa->hw_stats); 1065 rc = -EOPNOTSUPP; 1066 goto release; 1067 } 1068 1069 ctr = efx_tc_flower_get_counter_index(efx, tc->cookie, 1070 EFX_TC_COUNTER_TYPE_AR); 1071 if (IS_ERR(ctr)) { 1072 rc = PTR_ERR(ctr); 1073 NL_SET_ERR_MSG_MOD(extack, "Failed to obtain a counter"); 1074 goto release; 1075 } 1076 act->count = ctr; 1077 } 1078 1079 switch (fa->id) { 1080 case FLOW_ACTION_DROP: 1081 rc = efx_mae_alloc_action_set(efx, act); 1082 if (rc) { 1083 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (drop)"); 1084 goto release; 1085 } 1086 list_add_tail(&act->list, &rule->acts.list); 1087 act = NULL; /* end of the line */ 1088 break; 1089 case FLOW_ACTION_REDIRECT: 1090 case FLOW_ACTION_MIRRED: 1091 save = *act; 1092 1093 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_DELIVER)) { 1094 /* can't happen */ 1095 rc = -EOPNOTSUPP; 1096 NL_SET_ERR_MSG_MOD(extack, "Deliver action violates action order (can't happen)"); 1097 goto release; 1098 } 1099 1100 to_efv = efx_tc_flower_lookup_efv(efx, fa->dev); 1101 if (IS_ERR(to_efv)) { 1102 NL_SET_ERR_MSG_MOD(extack, "Mirred egress device not on switch"); 1103 rc = PTR_ERR(to_efv); 1104 goto release; 1105 } 1106 rc = efx_tc_flower_external_mport(efx, to_efv); 1107 if (rc < 0) { 1108 NL_SET_ERR_MSG_MOD(extack, "Failed to identify egress m-port"); 1109 goto release; 1110 } 1111 act->dest_mport = rc; 1112 act->deliver = 1; 1113 rc = efx_mae_alloc_action_set(efx, act); 1114 if (rc) { 1115 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (mirred)"); 1116 goto release; 1117 } 1118 list_add_tail(&act->list, &rule->acts.list); 1119 act = NULL; 1120 if (fa->id == FLOW_ACTION_REDIRECT) 1121 break; /* end of the line */ 1122 /* Mirror, so continue on with saved act */ 1123 save.count = NULL; 1124 act = kzalloc(sizeof(*act), GFP_USER); 1125 if (!act) { 1126 rc = -ENOMEM; 1127 goto release; 1128 } 1129 *act = save; 1130 break; 1131 case FLOW_ACTION_VLAN_POP: 1132 if (act->vlan_push) { 1133 act->vlan_push--; 1134 } else if (efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_POP)) { 1135 act->vlan_pop++; 1136 } else { 1137 NL_SET_ERR_MSG_MOD(extack, 1138 "More than two VLAN pops, or action order violated"); 1139 rc = -EINVAL; 1140 goto release; 1141 } 1142 break; 1143 case FLOW_ACTION_VLAN_PUSH: 1144 if (!efx_tc_flower_action_order_ok(act, EFX_TC_AO_VLAN_PUSH)) { 1145 rc = -EINVAL; 1146 NL_SET_ERR_MSG_MOD(extack, 1147 "More than two VLAN pushes, or action order violated"); 1148 goto release; 1149 } 1150 tci = fa->vlan.vid & VLAN_VID_MASK; 1151 tci |= fa->vlan.prio << VLAN_PRIO_SHIFT; 1152 act->vlan_tci[act->vlan_push] = cpu_to_be16(tci); 1153 act->vlan_proto[act->vlan_push] = fa->vlan.proto; 1154 act->vlan_push++; 1155 break; 1156 default: 1157 NL_SET_ERR_MSG_FMT_MOD(extack, "Unhandled action %u", 1158 fa->id); 1159 rc = -EOPNOTSUPP; 1160 goto release; 1161 } 1162 } 1163 1164 if (act) { 1165 /* Not shot/redirected, so deliver to default dest */ 1166 if (from_efv == EFX_EFV_PF) 1167 /* Rule applies to traffic from the wire, 1168 * and default dest is thus the PF 1169 */ 1170 efx_mae_mport_uplink(efx, &act->dest_mport); 1171 else 1172 /* Representor, so rule applies to traffic from 1173 * representee, and default dest is thus the rep. 1174 * All reps use the same mport for delivery 1175 */ 1176 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, 1177 &act->dest_mport); 1178 act->deliver = 1; 1179 rc = efx_mae_alloc_action_set(efx, act); 1180 if (rc) { 1181 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set to hw (deliver)"); 1182 goto release; 1183 } 1184 list_add_tail(&act->list, &rule->acts.list); 1185 act = NULL; /* Prevent double-free in error path */ 1186 } 1187 1188 netif_dbg(efx, drv, efx->net_dev, 1189 "Successfully parsed filter (cookie %lx)\n", 1190 tc->cookie); 1191 1192 rule->match = match; 1193 1194 rc = efx_mae_alloc_action_set_list(efx, &rule->acts); 1195 if (rc) { 1196 NL_SET_ERR_MSG_MOD(extack, "Failed to write action set list to hw"); 1197 goto release; 1198 } 1199 rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC, 1200 rule->acts.fw_id, &rule->fw_id); 1201 if (rc) { 1202 NL_SET_ERR_MSG_MOD(extack, "Failed to insert rule in hw"); 1203 goto release_acts; 1204 } 1205 return 0; 1206 1207 release_acts: 1208 efx_mae_free_action_set_list(efx, &rule->acts); 1209 release: 1210 /* We failed to insert the rule, so free up any entries we created in 1211 * subsidiary tables. 1212 */ 1213 if (act) 1214 efx_tc_free_action_set(efx, act, false); 1215 if (rule) { 1216 rhashtable_remove_fast(&efx->tc->match_action_ht, 1217 &rule->linkage, 1218 efx_tc_match_action_ht_params); 1219 efx_tc_free_action_set_list(efx, &rule->acts, false); 1220 } 1221 kfree(rule); 1222 return rc; 1223 } 1224 1225 static int efx_tc_flower_destroy(struct efx_nic *efx, 1226 struct net_device *net_dev, 1227 struct flow_cls_offload *tc) 1228 { 1229 struct netlink_ext_ack *extack = tc->common.extack; 1230 struct efx_tc_flow_rule *rule; 1231 1232 rule = rhashtable_lookup_fast(&efx->tc->match_action_ht, &tc->cookie, 1233 efx_tc_match_action_ht_params); 1234 if (!rule) { 1235 /* Only log a message if we're the ingress device. Otherwise 1236 * it's a foreign filter and we might just not have been 1237 * interested (e.g. we might not have been the egress device 1238 * either). 1239 */ 1240 if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev))) 1241 netif_warn(efx, drv, efx->net_dev, 1242 "Filter %lx not found to remove\n", tc->cookie); 1243 NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules"); 1244 return -ENOENT; 1245 } 1246 1247 /* Remove it from HW */ 1248 efx_tc_delete_rule(efx, rule); 1249 /* Delete it from SW */ 1250 rhashtable_remove_fast(&efx->tc->match_action_ht, &rule->linkage, 1251 efx_tc_match_action_ht_params); 1252 netif_dbg(efx, drv, efx->net_dev, "Removed filter %lx\n", rule->cookie); 1253 kfree(rule); 1254 return 0; 1255 } 1256 1257 static int efx_tc_flower_stats(struct efx_nic *efx, struct net_device *net_dev, 1258 struct flow_cls_offload *tc) 1259 { 1260 struct netlink_ext_ack *extack = tc->common.extack; 1261 struct efx_tc_counter_index *ctr; 1262 struct efx_tc_counter *cnt; 1263 u64 packets, bytes; 1264 1265 ctr = efx_tc_flower_find_counter_index(efx, tc->cookie); 1266 if (!ctr) { 1267 /* See comment in efx_tc_flower_destroy() */ 1268 if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev))) 1269 if (net_ratelimit()) 1270 netif_warn(efx, drv, efx->net_dev, 1271 "Filter %lx not found for stats\n", 1272 tc->cookie); 1273 NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules"); 1274 return -ENOENT; 1275 } 1276 if (WARN_ON(!ctr->cnt)) /* can't happen */ 1277 return -EIO; 1278 cnt = ctr->cnt; 1279 1280 spin_lock_bh(&cnt->lock); 1281 /* Report only new pkts/bytes since last time TC asked */ 1282 packets = cnt->packets; 1283 bytes = cnt->bytes; 1284 flow_stats_update(&tc->stats, bytes - cnt->old_bytes, 1285 packets - cnt->old_packets, 0, cnt->touched, 1286 FLOW_ACTION_HW_STATS_DELAYED); 1287 cnt->old_packets = packets; 1288 cnt->old_bytes = bytes; 1289 spin_unlock_bh(&cnt->lock); 1290 return 0; 1291 } 1292 1293 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev, 1294 struct flow_cls_offload *tc, struct efx_rep *efv) 1295 { 1296 int rc; 1297 1298 if (!efx->tc) 1299 return -EOPNOTSUPP; 1300 1301 mutex_lock(&efx->tc->mutex); 1302 switch (tc->command) { 1303 case FLOW_CLS_REPLACE: 1304 rc = efx_tc_flower_replace(efx, net_dev, tc, efv); 1305 break; 1306 case FLOW_CLS_DESTROY: 1307 rc = efx_tc_flower_destroy(efx, net_dev, tc); 1308 break; 1309 case FLOW_CLS_STATS: 1310 rc = efx_tc_flower_stats(efx, net_dev, tc); 1311 break; 1312 default: 1313 rc = -EOPNOTSUPP; 1314 break; 1315 } 1316 mutex_unlock(&efx->tc->mutex); 1317 return rc; 1318 } 1319 1320 static int efx_tc_configure_default_rule(struct efx_nic *efx, u32 ing_port, 1321 u32 eg_port, struct efx_tc_flow_rule *rule) 1322 { 1323 struct efx_tc_action_set_list *acts = &rule->acts; 1324 struct efx_tc_match *match = &rule->match; 1325 struct efx_tc_action_set *act; 1326 int rc; 1327 1328 match->value.ingress_port = ing_port; 1329 match->mask.ingress_port = ~0; 1330 act = kzalloc(sizeof(*act), GFP_KERNEL); 1331 if (!act) 1332 return -ENOMEM; 1333 act->deliver = 1; 1334 act->dest_mport = eg_port; 1335 rc = efx_mae_alloc_action_set(efx, act); 1336 if (rc) 1337 goto fail1; 1338 EFX_WARN_ON_PARANOID(!list_empty(&acts->list)); 1339 list_add_tail(&act->list, &acts->list); 1340 rc = efx_mae_alloc_action_set_list(efx, acts); 1341 if (rc) 1342 goto fail2; 1343 rc = efx_mae_insert_rule(efx, match, EFX_TC_PRIO_DFLT, 1344 acts->fw_id, &rule->fw_id); 1345 if (rc) 1346 goto fail3; 1347 return 0; 1348 fail3: 1349 efx_mae_free_action_set_list(efx, acts); 1350 fail2: 1351 list_del(&act->list); 1352 efx_mae_free_action_set(efx, act->fw_id); 1353 fail1: 1354 kfree(act); 1355 return rc; 1356 } 1357 1358 static int efx_tc_configure_default_rule_pf(struct efx_nic *efx) 1359 { 1360 struct efx_tc_flow_rule *rule = &efx->tc->dflt.pf; 1361 u32 ing_port, eg_port; 1362 1363 efx_mae_mport_uplink(efx, &ing_port); 1364 efx_mae_mport_wire(efx, &eg_port); 1365 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1366 } 1367 1368 static int efx_tc_configure_default_rule_wire(struct efx_nic *efx) 1369 { 1370 struct efx_tc_flow_rule *rule = &efx->tc->dflt.wire; 1371 u32 ing_port, eg_port; 1372 1373 efx_mae_mport_wire(efx, &ing_port); 1374 efx_mae_mport_uplink(efx, &eg_port); 1375 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1376 } 1377 1378 int efx_tc_configure_default_rule_rep(struct efx_rep *efv) 1379 { 1380 struct efx_tc_flow_rule *rule = &efv->dflt; 1381 struct efx_nic *efx = efv->parent; 1382 u32 ing_port, eg_port; 1383 1384 efx_mae_mport_mport(efx, efv->mport, &ing_port); 1385 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, &eg_port); 1386 return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule); 1387 } 1388 1389 void efx_tc_deconfigure_default_rule(struct efx_nic *efx, 1390 struct efx_tc_flow_rule *rule) 1391 { 1392 if (rule->fw_id != MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL) 1393 efx_tc_delete_rule(efx, rule); 1394 rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1395 } 1396 1397 static int efx_tc_configure_rep_mport(struct efx_nic *efx) 1398 { 1399 u32 rep_mport_label; 1400 int rc; 1401 1402 rc = efx_mae_allocate_mport(efx, &efx->tc->reps_mport_id, &rep_mport_label); 1403 if (rc) 1404 return rc; 1405 pci_dbg(efx->pci_dev, "created rep mport 0x%08x (0x%04x)\n", 1406 efx->tc->reps_mport_id, rep_mport_label); 1407 /* Use mport *selector* as vport ID */ 1408 efx_mae_mport_mport(efx, efx->tc->reps_mport_id, 1409 &efx->tc->reps_mport_vport_id); 1410 return 0; 1411 } 1412 1413 static void efx_tc_deconfigure_rep_mport(struct efx_nic *efx) 1414 { 1415 efx_mae_free_mport(efx, efx->tc->reps_mport_id); 1416 efx->tc->reps_mport_id = MAE_MPORT_SELECTOR_NULL; 1417 } 1418 1419 int efx_tc_insert_rep_filters(struct efx_nic *efx) 1420 { 1421 struct efx_filter_spec promisc, allmulti; 1422 int rc; 1423 1424 if (efx->type->is_vf) 1425 return 0; 1426 if (!efx->tc) 1427 return 0; 1428 efx_filter_init_rx(&promisc, EFX_FILTER_PRI_REQUIRED, 0, 0); 1429 efx_filter_set_uc_def(&promisc); 1430 efx_filter_set_vport_id(&promisc, efx->tc->reps_mport_vport_id); 1431 rc = efx_filter_insert_filter(efx, &promisc, false); 1432 if (rc < 0) 1433 return rc; 1434 efx->tc->reps_filter_uc = rc; 1435 efx_filter_init_rx(&allmulti, EFX_FILTER_PRI_REQUIRED, 0, 0); 1436 efx_filter_set_mc_def(&allmulti); 1437 efx_filter_set_vport_id(&allmulti, efx->tc->reps_mport_vport_id); 1438 rc = efx_filter_insert_filter(efx, &allmulti, false); 1439 if (rc < 0) 1440 return rc; 1441 efx->tc->reps_filter_mc = rc; 1442 return 0; 1443 } 1444 1445 void efx_tc_remove_rep_filters(struct efx_nic *efx) 1446 { 1447 if (efx->type->is_vf) 1448 return; 1449 if (!efx->tc) 1450 return; 1451 if (efx->tc->reps_filter_mc >= 0) 1452 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_mc); 1453 efx->tc->reps_filter_mc = -1; 1454 if (efx->tc->reps_filter_uc >= 0) 1455 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_uc); 1456 efx->tc->reps_filter_uc = -1; 1457 } 1458 1459 int efx_init_tc(struct efx_nic *efx) 1460 { 1461 int rc; 1462 1463 rc = efx_mae_get_caps(efx, efx->tc->caps); 1464 if (rc) 1465 return rc; 1466 if (efx->tc->caps->match_field_count > MAE_NUM_FIELDS) 1467 /* Firmware supports some match fields the driver doesn't know 1468 * about. Not fatal, unless any of those fields are required 1469 * (MAE_FIELD_SUPPORTED_MATCH_ALWAYS) but if so we don't know. 1470 */ 1471 netif_warn(efx, probe, efx->net_dev, 1472 "FW reports additional match fields %u\n", 1473 efx->tc->caps->match_field_count); 1474 if (efx->tc->caps->action_prios < EFX_TC_PRIO__NUM) { 1475 netif_err(efx, probe, efx->net_dev, 1476 "Too few action prios supported (have %u, need %u)\n", 1477 efx->tc->caps->action_prios, EFX_TC_PRIO__NUM); 1478 return -EIO; 1479 } 1480 rc = efx_tc_configure_default_rule_pf(efx); 1481 if (rc) 1482 return rc; 1483 rc = efx_tc_configure_default_rule_wire(efx); 1484 if (rc) 1485 return rc; 1486 rc = efx_tc_configure_rep_mport(efx); 1487 if (rc) 1488 return rc; 1489 efx->tc->up = true; 1490 rc = flow_indr_dev_register(efx_tc_indr_setup_cb, efx); 1491 if (rc) 1492 return rc; 1493 return 0; 1494 } 1495 1496 void efx_fini_tc(struct efx_nic *efx) 1497 { 1498 /* We can get called even if efx_init_struct_tc() failed */ 1499 if (!efx->tc) 1500 return; 1501 if (efx->tc->up) 1502 flow_indr_dev_unregister(efx_tc_indr_setup_cb, efx, efx_tc_block_unbind); 1503 efx_tc_deconfigure_rep_mport(efx); 1504 efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.pf); 1505 efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.wire); 1506 efx->tc->up = false; 1507 } 1508 1509 /* At teardown time, all TC filter rules (and thus all resources they created) 1510 * should already have been removed. If we find any in our hashtables, make a 1511 * cursory attempt to clean up the software side. 1512 */ 1513 static void efx_tc_encap_match_free(void *ptr, void *__unused) 1514 { 1515 struct efx_tc_encap_match *encap = ptr; 1516 1517 WARN_ON(refcount_read(&encap->ref)); 1518 kfree(encap); 1519 } 1520 1521 static void efx_tc_flow_free(void *ptr, void *arg) 1522 { 1523 struct efx_tc_flow_rule *rule = ptr; 1524 struct efx_nic *efx = arg; 1525 1526 netif_err(efx, drv, efx->net_dev, 1527 "tc rule %lx still present at teardown, removing\n", 1528 rule->cookie); 1529 1530 /* Also releases entries in subsidiary tables */ 1531 efx_tc_delete_rule(efx, rule); 1532 1533 kfree(rule); 1534 } 1535 1536 int efx_init_struct_tc(struct efx_nic *efx) 1537 { 1538 int rc; 1539 1540 if (efx->type->is_vf) 1541 return 0; 1542 1543 efx->tc = kzalloc(sizeof(*efx->tc), GFP_KERNEL); 1544 if (!efx->tc) 1545 return -ENOMEM; 1546 efx->tc->caps = kzalloc(sizeof(struct mae_caps), GFP_KERNEL); 1547 if (!efx->tc->caps) { 1548 rc = -ENOMEM; 1549 goto fail_alloc_caps; 1550 } 1551 INIT_LIST_HEAD(&efx->tc->block_list); 1552 1553 mutex_init(&efx->tc->mutex); 1554 init_waitqueue_head(&efx->tc->flush_wq); 1555 rc = efx_tc_init_counters(efx); 1556 if (rc < 0) 1557 goto fail_counters; 1558 rc = rhashtable_init(&efx->tc->encap_match_ht, &efx_tc_encap_match_ht_params); 1559 if (rc < 0) 1560 goto fail_encap_match_ht; 1561 rc = rhashtable_init(&efx->tc->match_action_ht, &efx_tc_match_action_ht_params); 1562 if (rc < 0) 1563 goto fail_match_action_ht; 1564 efx->tc->reps_filter_uc = -1; 1565 efx->tc->reps_filter_mc = -1; 1566 INIT_LIST_HEAD(&efx->tc->dflt.pf.acts.list); 1567 efx->tc->dflt.pf.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1568 INIT_LIST_HEAD(&efx->tc->dflt.wire.acts.list); 1569 efx->tc->dflt.wire.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL; 1570 efx->extra_channel_type[EFX_EXTRA_CHANNEL_TC] = &efx_tc_channel_type; 1571 return 0; 1572 fail_match_action_ht: 1573 rhashtable_destroy(&efx->tc->encap_match_ht); 1574 fail_encap_match_ht: 1575 efx_tc_destroy_counters(efx); 1576 fail_counters: 1577 mutex_destroy(&efx->tc->mutex); 1578 kfree(efx->tc->caps); 1579 fail_alloc_caps: 1580 kfree(efx->tc); 1581 efx->tc = NULL; 1582 return rc; 1583 } 1584 1585 void efx_fini_struct_tc(struct efx_nic *efx) 1586 { 1587 if (!efx->tc) 1588 return; 1589 1590 mutex_lock(&efx->tc->mutex); 1591 EFX_WARN_ON_PARANOID(efx->tc->dflt.pf.fw_id != 1592 MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL); 1593 EFX_WARN_ON_PARANOID(efx->tc->dflt.wire.fw_id != 1594 MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL); 1595 rhashtable_free_and_destroy(&efx->tc->match_action_ht, efx_tc_flow_free, 1596 efx); 1597 rhashtable_free_and_destroy(&efx->tc->encap_match_ht, 1598 efx_tc_encap_match_free, NULL); 1599 efx_tc_fini_counters(efx); 1600 mutex_unlock(&efx->tc->mutex); 1601 mutex_destroy(&efx->tc->mutex); 1602 kfree(efx->tc->caps); 1603 kfree(efx->tc); 1604 efx->tc = NULL; 1605 } 1606